XRootD
XrdNetUtils.hh
Go to the documentation of this file.
1 #ifndef __XRDNETUTILS_HH__
2 #define __XRDNETUTILS_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t U t i l s . h h */
6 /* */
7 /* (c) 2025 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <string>
34 #include <vector>
35 #include <sstream>
36 #include <cstdint>
37 
38 #include "XrdOuc/XrdOucEnum.hh"
39 
40 class XrdOucTList;
41 class XrdNetAddr;
42 union XrdNetSockAddr;
43 
44 namespace XrdNetSpace {struct hpSpec;}
45 
47 {
48 public:
49 
50 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 
64 
65 static
66 IPComp Compare(XrdNetSockAddr& ip1, XrdNetSockAddr& ip2, bool* psame=0);
67 
68 //------------------------------------------------------------------------------
78 //------------------------------------------------------------------------------
79 
80 static int Decode(XrdNetSockAddr *sadr, const char *buff, int blen);
81 
82 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 
96 static int Encode(const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1);
97 
98 
99 //------------------------------------------------------------------------------
141 //------------------------------------------------------------------------------
142 
145  prefAuto= 16, order46 = 32, order64 = 64,
146  onlyUDP =128
147  };
148 
149 static const int PortInSpec = (int)0x80000000;
150 static const int NoPortRaw = (int)0xC0000000;
151 
152 static
153 const char *GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN,
154  AddrOpts opts=allIPMap, int pNum=PortInSpec);
155 
156 //------------------------------------------------------------------------------
171 //------------------------------------------------------------------------------
172 
173 static
174 const char *GetAddrs(const std::string &hSpec, std::vector<XrdNetAddr> &aVec,
175  int *ordn=0, AddrOpts opts=allIPMap, int pNum=PortInSpec);
176 
177 //------------------------------------------------------------------------------
195 //------------------------------------------------------------------------------
196 
197 static
198 const char *GetAddrs(std::vector<std::string> &hSVec,
199  std::vector<XrdNetAddr> &aVec,
200  int *ordn=0, AddrOpts opts=allIPMap,
201  unsigned int rotNum=0, bool force=false);
202 
203 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 
224 static
225 int GetSokInfo(int fd, char *theAddr, int theALen, char &theType);
226 
227 //------------------------------------------------------------------------------
247 //------------------------------------------------------------------------------
248 
249 static
250 XrdOucTList *Hosts(const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0,
251  const char **eText=0);
252 
253 //------------------------------------------------------------------------------
271 //------------------------------------------------------------------------------
272 
273 static const int noPort = 1;
274 static const int oldFmt = 2;
275 
276 static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0);
277 
278 //------------------------------------------------------------------------------
298 //------------------------------------------------------------------------------
299 
300 static int IPFormat(int fd, char *bP, int bL, int opts=0);
301 
302 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 
320 static bool Match(const char *hName, const char *pattern);
321 
322 //------------------------------------------------------------------------------
335 //------------------------------------------------------------------------------
336 
337 static char *MyHostName(const char *eName="*unknown*", const char **eText=0);
338 
339 //------------------------------------------------------------------------------
349 //------------------------------------------------------------------------------
350 
351 enum NetProt {hasNone = 0,
352  hasIPv4 = 1, //<! Has only IPv4 capability
353  hasIPv6 = 2, //<! Has only IPv6 capability
354  hasIP64 = 3, //<! Has IPv4 IPv6 capability (dual stack)
355  hasPub4 = 4, //<! Has IPv4 public address (or'd with above)
356  hasPub6 = 8 //<! Has IPv6 public address (or'd with above)
357  };
358 
359 enum NetType {qryINET = 0,
360  qryINIF = 1
361  };
362 
363 static NetProt NetConfig(NetType netquery=qryINET, const char **eText=0);
364 
365 //------------------------------------------------------------------------------
381 //------------------------------------------------------------------------------
382 
383 static bool Parse(const char *hSpec, const char **hName, const char **hNend,
384  const char **hPort, const char **hPend);
385 
386 //------------------------------------------------------------------------------
394 //------------------------------------------------------------------------------
395 
396 static int Port(int fd, const char **eText=0);
397 
398 //------------------------------------------------------------------------------
404 //------------------------------------------------------------------------------
405 
406 static int ProtoID(const char *pName);
407 
408 //------------------------------------------------------------------------------
417 //------------------------------------------------------------------------------
418 
419 static int ServPort(const char *sName, bool isUDP=false, const char **eText=0);
420 
421 //------------------------------------------------------------------------------
432 //------------------------------------------------------------------------------
433 
434 static int SetAuto(AddrOpts aOpts=allIPMap);
435 
436 //------------------------------------------------------------------------------
444 //------------------------------------------------------------------------------
445 
446 static bool Singleton(const char *hSpec, const char **eText=0);
447 
448 static bool ConnectWithTimeout(int sockfd, const struct sockaddr* clientAddr, size_t clientAddrLen,uint32_t timeout_sec, std::stringstream & errMsg);
449 
450 //------------------------------------------------------------------------------
452 //------------------------------------------------------------------------------
453 
455 
456 //------------------------------------------------------------------------------
458 //------------------------------------------------------------------------------
459 
461 private:
462 
463 static void FillAddr(XrdNetSpace::hpSpec &aBuff, XrdNetAddr *aVec,
464  int *ordn=0, unsigned int rotNum=0);
465 static
466 const char *GetAInfo(XrdNetSpace::hpSpec &aBuff);
467 static void GetHints(XrdNetSpace::hpSpec &aBuff, AddrOpts opts);
468 static
469 const char *GetHostPort(XrdNetSpace::hpSpec &aBuff, const char *hSpec, int pNum);
470 static
471 const char *getMyFQN(const char *&myDom);
472 static int setET(const char **errtxt, int rc);
473 static bool SetSockBlocking(int sockfd, bool blocking, std::stringstream & errMsg);
474 static int autoFamily;
475 static int autoHints;
476 };
477 
479 
480 #endif
#define XRDOUC_ENUM_OPERATORS(T)
Definition: XrdOucEnum.hh:22
struct myOpts opts
static const int NoPortRaw
Definition: XrdNetUtils.hh:150
static bool Match(const char *hName, const char *pattern)
Definition: XrdNetUtils.cc:658
static IPComp Compare(XrdNetSockAddr &ip1, XrdNetSockAddr &ip2, bool *psame=0)
Definition: XrdNetUtils.cc:70
@ qryINET
Only consider internet protocols via DNS.
Definition: XrdNetUtils.hh:359
@ qryINIF
Only consider internet protocols via ifconfig.
Definition: XrdNetUtils.hh:360
static char * MyHostName(const char *eName="*unknown*", const char **eText=0)
Definition: XrdNetUtils.cc:702
static int GetSokInfo(int fd, char *theAddr, int theALen, char &theType)
Definition: XrdNetUtils.cc:533
static int Encode(const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1)
Definition: XrdNetUtils.cc:155
@ hasNone
Unable to determine available protocols.
Definition: XrdNetUtils.hh:351
static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
Definition: XrdNetUtils.cc:619
static bool Singleton(const char *hSpec, const char **eText=0)
Definition: XrdNetUtils.cc:956
static const char * GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
Definition: XrdNetUtils.cc:274
static int Decode(XrdNetSockAddr *sadr, const char *buff, int blen)
Definition: XrdNetUtils.cc:105
static NetProt NetConfig(NetType netquery=qryINET, const char **eText=0)
Definition: XrdNetUtils.cc:716
static const int PortInSpec
Definition: XrdNetUtils.hh:149
static bool ConnectWithTimeout(int sockfd, const struct sockaddr *clientAddr, size_t clientAddrLen, uint32_t timeout_sec, std::stringstream &errMsg)
Definition: XrdNetUtils.cc:981
static int Port(int fd, const char **eText=0)
Definition: XrdNetUtils.cc:815
~XrdNetUtils()
Destructor.
Definition: XrdNetUtils.hh:460
static const int oldFmt
Definition: XrdNetUtils.hh:274
static int ProtoID(const char *pName)
Definition: XrdNetUtils.cc:838
static int ServPort(const char *sName, bool isUDP=false, const char **eText=0)
Definition: XrdNetUtils.cc:874
static XrdOucTList * Hosts(const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0, const char **eText=0)
Definition: XrdNetUtils.cc:576
XrdNetUtils()
Constructor.
Definition: XrdNetUtils.hh:454
static const int noPort
Definition: XrdNetUtils.hh:273
static bool Parse(const char *hSpec, const char **hName, const char **hNend, const char **hPort, const char **hPend)
Definition: XrdNetUtils.cc:780
static int SetAuto(AddrOpts aOpts=allIPMap)
Definition: XrdNetUtils.cc:915