XRootD
XrdNetUtils Class Reference

#include <XrdNetUtils.hh>

+ Collaboration diagram for XrdNetUtils:

Public Types

enum  AddrOpts {
  allIPMap = 0 ,
  allIPv64 = 1 ,
  allV4Map = 2 ,
  onlyIPv6 = 3 ,
  onlyIPv4 = 4 ,
  prefIPv6 = 8 ,
  prefAuto = 16 ,
  order46 = 32 ,
  order64 = 64 ,
  onlyUDP =128
}
 
enum  NetProt {
  hasNone = 0 ,
  hasIPv4 = 1 ,
  hasIPv6 = 2 ,
  hasIP64 = 3 ,
  hasPub4 = 4 ,
  hasPub6 = 8
}
 
enum  NetType {
  qryINET = 0 ,
  qryINIF = 1
}
 

Public Member Functions

 XrdNetUtils ()
 Constructor. More...
 
 ~XrdNetUtils ()
 Destructor. More...
 

Static Public Member Functions

static bool ConnectWithTimeout (int sockfd, const struct sockaddr *clientAddr, size_t clientAddrLen, uint32_t timeout_sec, std::stringstream &errMsg)
 
static int Decode (XrdNetSockAddr *sadr, const char *buff, int blen)
 
static int Encode (const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1)
 
static const char * GetAddrs (const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
 
static const char * GetAddrs (const std::string &hSpec, std::vector< XrdNetAddr > &aVec, int *ordn=0, AddrOpts opts=allIPMap, int pNum=PortInSpec)
 
static const char * GetAddrs (std::vector< std::string > &hSVec, std::vector< XrdNetAddr > &aVec, int *ordn=0, AddrOpts opts=allIPMap, unsigned int rotNum=0, bool force=false)
 
static int GetSokInfo (int fd, char *theAddr, int theALen, char &theType)
 
static XrdOucTListHosts (const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0, const char **eText=0)
 
static int IPFormat (const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
 
static int IPFormat (int fd, char *bP, int bL, int opts=0)
 
static bool Match (const char *hName, const char *pattern)
 
static char * MyHostName (const char *eName="*unknown*", const char **eText=0)
 
static NetProt NetConfig (NetType netquery=qryINET, const char **eText=0)
 
static bool Parse (const char *hSpec, const char **hName, const char **hNend, const char **hPort, const char **hPend)
 
static int Port (int fd, const char **eText=0)
 
static int ProtoID (const char *pName)
 
static int ServPort (const char *sName, bool isUDP=false, const char **eText=0)
 
static int SetAuto (AddrOpts aOpts=allIPMap)
 
static bool Singleton (const char *hSpec, const char **eText=0)
 

Static Public Attributes

static const int noPort = 1
 
static const int NoPortRaw = (int)0xC0000000
 
static const int oldFmt = 2
 
static const int PortInSpec = (int)0x80000000
 

Detailed Description

Definition at line 46 of file XrdNetUtils.hh.

Member Enumeration Documentation

◆ AddrOpts

Version 1: Return multiple addresses associated with a host or IP address.

Parameters
hSpec-> convert specification to addresses. Valid formats: IP.v4: nnn.nnn.nnn.nnn[:<port>] IP.v6: [ipv6_addr][:<port>] IP.xx: name[:port] xx is determined by getaddrinfo()
aListPplace where the pointer to the returned array of XrdNetAddr objects is to be placed. Set to zero if none returned. The caller must delete this array when no longer needed.
aListNplace where the number of elements in aListP are to be returned.
optsOptions on what to return. Choose one of: allIPMap - all IPv6 and mapped IPv4 addrs (default) allIPv64 - all IPv6 and unmapped IPv4 addrs allV4Map - all mapped IPV4 addrs. onlyIPv6 - only IPv6 addrs onlyIPv4 - only unmapped IPv4 addrs prefIPv6 - only IPv6 addrs; if none, mapped IPv4 addrs prefAuto - Returns addresses based on configured non-local interfaces. The returned addresses will be normally useable on this host and may be IPv4, IPv6, mapped IPv4, or a mixture. The above may be or'd with one or more of the following: onlyUDP - only addrs valid for UDP connections else TCP order46 - List IPv4 addresses (mapped or native) first. order64 - List IPv6 addresses first.
pNum>= 0 uses the value as the port number regardless of what is in hSpec, should it be supplied. However, if is present, it must be a valid port number. < 0 uses the positive value as the port number if the port number has not been specified in hSpec. **** When set to PortInSpec(the default, see below) the port number/name must be specified in hSpec. If it is not, an error is returned. **** When set to NoPortRaw then hSpec does not contain a port number and is a host name, IPv4 address, or an IPv6 address without surrounding brackets.
Returns
Success: 0 with aListN set to the number of elements in aListP. Failure: the error message text describing the error and aListP and aListN is set to zero.
Enumerator
allIPMap 
allIPv64 
allV4Map 
onlyIPv6 
onlyIPv4 
prefIPv6 
prefAuto 
order46 
order64 
onlyUDP 

Definition at line 125 of file XrdNetUtils.hh.

125  {allIPMap= 0, allIPv64= 1, allV4Map= 2,
126  onlyIPv6= 3, onlyIPv4= 4, prefIPv6= 8,
127  prefAuto= 16, order46 = 32, order64 = 64,
128  onlyUDP =128
129  };

◆ NetProt

Get the supported network protocols.

Parameters
netqryAn NetType enum specifying the protocol to inspect.
eTextWhen not nil, is where to place error message text.
Returns
One the the NetProt enums (see below). When hasNone is returned and eText is not nill it will point to a static string that gives the reason. If the reason is a null string, the query was successful but returned no matching protocols.
Enumerator
hasNone 

Unable to determine available protocols.

hasIPv4 
hasIPv6 
hasIP64 
hasPub4 
hasPub6 

Definition at line 333 of file XrdNetUtils.hh.

333  {hasNone = 0,
334  hasIPv4 = 1, //<! Has only IPv4 capability
335  hasIPv6 = 2, //<! Has only IPv6 capability
336  hasIP64 = 3, //<! Has IPv4 IPv6 capability (dual stack)
337  hasPub4 = 4, //<! Has IPv4 public address (or'd with above)
338  hasPub6 = 8 //<! Has IPv6 public address (or'd with above)
339  };
@ hasNone
Unable to determine available protocols.
Definition: XrdNetUtils.hh:333

◆ NetType

Enumerator
qryINET 

Only consider internet protocols via DNS.

qryINIF 

Only consider internet protocols via ifconfig.

Definition at line 341 of file XrdNetUtils.hh.

341  {qryINET = 0,
342  qryINIF = 1
343  };
@ qryINET
Only consider internet protocols via DNS.
Definition: XrdNetUtils.hh:341
@ qryINIF
Only consider internet protocols via ifconfig.
Definition: XrdNetUtils.hh:342

Constructor & Destructor Documentation

◆ XrdNetUtils()

XrdNetUtils::XrdNetUtils ( )
inline

Constructor.

Definition at line 436 of file XrdNetUtils.hh.

436 {}

◆ ~XrdNetUtils()

XrdNetUtils::~XrdNetUtils ( )
inline

Destructor.

Definition at line 442 of file XrdNetUtils.hh.

442 {}

Member Function Documentation

◆ ConnectWithTimeout()

bool XrdNetUtils::ConnectWithTimeout ( int  sockfd,
const struct sockaddr *  clientAddr,
size_t  clientAddrLen,
uint32_t  timeout_sec,
std::stringstream &  errMsg 
)
static

Definition at line 946 of file XrdNetUtils.cc.

946  {
947 
948  if (!SetSockBlocking(sockfd, false, errMsg)) { // Set to non-blocking
949  close(sockfd);
950  return false;
951  }
952 
953  int result = connect(sockfd, clientAddr, clientAddrLen);
954  if (result == 0) {
955  //We've managed to connect immediately
956  // Set the socket back to blocking
957  if(!SetSockBlocking(sockfd, true, errMsg)) {
958  ::close(sockfd);
959  return false;
960  }
961  return true;
962  } else if (errno != EINPROGRESS) {
963  errMsg << "Connection failed: " << strerror(errno);
964  ::close(sockfd);
965  return false;
966  }
967 
968  struct pollfd fds;
969  fds.fd = sockfd;
970  fds.events = POLLOUT; // Wait for the socket to be ready to write
971 
972  result = poll(&fds, 1, timeout_sec * 1000); //Timeout in milliseconds
973 
974  if (result < 0) {
975  errMsg << "Poll error: " << strerror(errno);
976  ::close(sockfd);
977  return false;
978  } else if (result == 0) {
979  errMsg << "Connection timed out";
980  ::close(sockfd);
981  return false;
982  }
983  // Polling succeeded
984  if (!(fds.revents & POLLOUT)) {
985  // We should normally not reach this code
986  errMsg << "Poll returned without error but the corresponding socket (" << sockfd << ") is not ready to write";
987  ::close(sockfd);
988  return false;
989  }
990  // Check for potential errors on the socket after polling
991  int so_error;
992  socklen_t len = sizeof(so_error);
993  getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
994  if (so_error != 0) {
995  errMsg << "Connection failed after poll: " << strerror(so_error);
996  ::close(sockfd);
997  return false;
998  }
999  // Everything succeeded, set the socket back to blocking
1000  if(!SetSockBlocking(sockfd, true, errMsg)) {
1001  ::close(sockfd);
1002  return false;
1003  }
1004  return true;
1005 }
#define close(a)
Definition: XrdPosix.hh:43

References close.

Referenced by XrdTpc::PMarkManager::connect().

+ Here is the caller graph for this function:

◆ Decode()

int XrdNetUtils::Decode ( XrdNetSockAddr sadr,
const char *  buff,
int  blen 
)
static

Decode an "encoded" ipv6/4 address and place it "sockaddr" type structure.

Parameters
sadraddress of the union that will hold the results.
buffaddress of buffer that holds the encoding.
blenlength of the string (it need not be null terminated).
Returns
> 0 the port number in host byte order. = 0 the port number was not set. < 0 the encoding was not correct.

Definition at line 70 of file XrdNetUtils.cc.

71 {
72  static const int ipv4Sz = sizeof(struct in_addr)*2+4;
73  static const int ipv6Sz = sizeof(struct in6_addr)*2+4;
74  char bval[sizeof(struct in6_addr)+2];
75  int isv6, n, i = 0, Odd = 0;
76 
77 // Determine if this will be IPV4 or IPV6 (only ones allowed)
78 //
79  if (blen == ipv6Sz) isv6 = 1;
80  else if (blen == ipv4Sz) isv6 = 0;
81  else return -1;
82 
83 // Convert the whole string to a temporary
84 //
85  while(blen--)
86  { if (*buff >= '0' && *buff <= '9') n = *buff-48;
87  else if (*buff >= 'a' && *buff <= 'f') n = *buff-87;
88  else if (*buff >= 'A' && *buff <= 'F') n = *buff-55;
89  else return -1;
90  if (Odd) bval[i++] |= n;
91  else bval[i ] = n << 4;
92  buff++; Odd = ~Odd;
93  }
94 
95 // Clear the address
96 //
97  memset(sadr, 0, sizeof(XrdNetSockAddr));
98 
99 // Copy out the data, as needed
100 //
101  if (isv6)
102  {sadr->v6.sin6_family = AF_INET6;
103  memcpy(&(sadr->v6.sin6_port), bval, 2);
104  memcpy(&(sadr->v6.sin6_addr), &bval[2], sizeof(struct in6_addr));
105  } else {
106  sadr->v4.sin_family = AF_INET;
107  memcpy(&(sadr->v4.sin_port), bval, 2);
108  memcpy(&(sadr->v4.sin_addr), &bval[2], sizeof(struct in_addr));
109  }
110 
111 // Return the converted port (it's the same for v4/v6)
112 //
113  return static_cast<int>(ntohs(sadr->v6.sin6_port));
114 }
struct sockaddr_in6 v6
struct sockaddr_in v4

References XrdNetSockAddr::v4, and XrdNetSockAddr::v6.

Referenced by XrdOucReqID::isMine().

+ Here is the caller graph for this function:

◆ Encode()

int XrdNetUtils::Encode ( const XrdNetSockAddr sadr,
char *  buff,
int  blen,
int  port = -1 
)
static

Encode the address and return it in a supplied buffer.

Parameters
sadraddress of the union that holds the IPV4/6 address.
buffaddress of buffer to hold the null terminated encoding.
blenlength of the buffer. It6 should be at least 40 bytes.
portoptional port value to use as opposed to the one present in sockaddr sadr. The port must be in host order.
Returns
> 0 the length of the encoding less the null byte. = 0 current address format not supported for encoding. < 0 buffer is too small; abs(retval) bytes needed.

Definition at line 120 of file XrdNetUtils.cc.

122 {
123  static const char *hv = "0123456789abcdef";
124  char *src, bval[sizeof(struct in6_addr)+2];
125  int asz, i, j = 0;
126 
127 // Compute the size we need for the buffer (note we only support IP4/6)
128 //
129  if (sadr->Addr.sa_family == AF_INET6)
130  {src = (char *)&(sadr->v6.sin6_addr); asz = sizeof(struct in6_addr);}
131  else if (sadr->Addr.sa_family == AF_INET)
132  {src = (char *)&(sadr->v4.sin_addr); asz = sizeof(struct in_addr); }
133  else return 0;
134  if (blen < (asz*2)+5) return -((asz*2)+5);
135 
136 // Get the port value in the first two bytes followed by the address.
137 //
138  if (port < 0) memcpy(bval, &(sadr->v6.sin6_port), 2);
139  else {short sPort = htons(static_cast<short>(port));
140  memcpy(bval, &sPort, 2);
141  }
142  memcpy(&bval[2], src, asz);
143  asz += 2;
144 
145 // Now convert to hex
146 //
147  for (i = 0; i < asz; i++)
148  {buff[j++] = hv[(bval[i] >> 4) & 0x0f];
149  buff[j++] = hv[ bval[i] & 0x0f];
150  }
151  buff[j] = '\0';
152 
153 // All done
154 //
155  return asz*2;
156 }
struct sockaddr Addr

References XrdNetSockAddr::Addr, XrdNetSockAddr::v4, and XrdNetSockAddr::v6.

Referenced by XrdOucReqID::XrdOucReqID(), and XrdOssPath::InitPrefix().

+ Here is the caller graph for this function:

◆ GetAddrs() [1/3]

const char * XrdNetUtils::GetAddrs ( const char *  hSpec,
XrdNetAddr aListP[],
int &  aListN,
XrdNetUtils::AddrOpts  opts = allIPMap,
int  pNum = PortInSpec 
)
static

Definition at line 239 of file XrdNetUtils.cc.

242 {
243  const char *eText;
244  hpSpec aInfo(opts);
245 
246 // Prep the returned fields
247 //
248  *aVec = 0;
249  aVsz = 0;
250 
251 // Parse the options
252 //
253  GetHints(aInfo, opts);
254 
255 // Parse the host specification and get addresses
256 //
257  if ((eText = GetHostPort(aInfo, hSpec, pNum))
258  || (eText = GetAInfo(aInfo))) return eText;
259 
260 // If we have any addresses, resize the vector with that many netaddr objects
261 // and then initialze each one of them.
262 //
263  if (aInfo.aNum4 || aInfo.aNum6)
264  {aVsz = aInfo.aNum4 + aInfo.aNum6;
265  *aVec = new XrdNetAddr[(unsigned int)aVsz];
266  FillAddr(aInfo, *aVec);
267  }
268 
269 // All done
270 //
271  return 0;
272 }
struct myOpts opts

References XrdNetSpace::hpSpec::aNum4, XrdNetSpace::hpSpec::aNum6, and opts.

Referenced by XrdNetRegistry::GetAddrs(), XrdCl::Utils::GetHostAddresses(), XrdNetAddr::Register(), and XrdNetIF::SetIF().

+ Here is the caller graph for this function:

◆ GetAddrs() [2/3]

const char * XrdNetUtils::GetAddrs ( const std::string &  hSpec,
std::vector< XrdNetAddr > &  aVec,
int *  ordn = 0,
AddrOpts  opts = allIPMap,
int  pNum = PortInSpec 
)
static

Version 2: Return multiple addresses associated with a host or IP address.

Parameters
hSpecReference to address specification (see version 1).
aVecReference to the vector to contain addresses.
ordnPointer to where the partition ordinal is to be stored.
optsOptions on what to return (see version 1).
pNumPort number argument (see version 1).
Returns
Success: 0 is returned. When ordn is not nil, the number of IPv4 entries (for order46) or IPv6 (for order64) entries that appear in the front of the vector. If ordering is not specified, the value is set to the size of the vector. Failure: the error message text describing the error and aVec is cleared (i.e. has no elements).

Definition at line 276 of file XrdNetUtils.cc.

279 {
280 // If this references a registered name, process it as such.
281 //
282  if (*(hSpec.c_str()) == XrdNetRegistry::pfx)
283  return XrdNetRegistry::GetAddrs(hSpec, aVec, ordn, opts, pNum);
284 
285 // Start up!
286 //
287  const char *eText;
288  hpSpec aInfo(opts);
289 
290 // Clear the result vector
291 //
292  aVec.clear();
293  if (ordn) *ordn = 0;
294 
295 // Parse the options
296 //
297  GetHints(aInfo, opts);
298 
299 // Parse the host specification and get address info
300 //
301  if ((eText = GetHostPort(aInfo, hSpec.c_str(), pNum))
302  || (eText = GetAInfo(aInfo))) return eText;
303 
304 // If we have any addresses, resize the vector with that many netaddr objects
305 // and then initialze each one of them.
306 //
307  if (aInfo.aNum4 || aInfo.aNum6)
308  {aVec.resize(aInfo.aNum4 + aInfo.aNum6);
309  FillAddr(aInfo, aVec.data(), ordn);
310  }
311 
312 // All done
313 //
314  return 0;
315 }
static const char * GetAddrs(const std::string &hSpec, std::vector< XrdNetAddr > &aVec, int *ordn=0, XrdNetUtils::AddrOpts opts=XrdNetUtils::allIPMap, int pNum=XrdNetUtils::PortInSpec)
static const char pfx
Registry names must start with this character.

References XrdNetSpace::hpSpec::aNum4, XrdNetSpace::hpSpec::aNum6, XrdNetRegistry::GetAddrs(), opts, and XrdNetRegistry::pfx.

+ Here is the call graph for this function:

◆ GetAddrs() [3/3]

const char * XrdNetUtils::GetAddrs ( std::vector< std::string > &  hSVec,
std::vector< XrdNetAddr > &  aVec,
int *  ordn = 0,
AddrOpts  opts = allIPMap,
unsigned int  rotNum = 0,
bool  force = false 
)
static

Version 3: Return multiple addresses associated with a list of host or IP addresses.

Parameters
hSVecvector of address specification (see version 1). Note that this version requires hSVec entries to have a port number.
aVecReference to the vector to contain addresses.
ordnPointer to where the partition ordinal is to be stored.
optsOptions on what to return (see version 1).
rotNumThe rotation factor to order addresses in the result.
forceWhen true resolution errors are ignored.
Returns
Success: 0 is returned. When ordn is not nil, the number of IPv4 entries (for order46) or IPv6 (for order64) entries that appear in the front of the vector. If ordering is not specified, the value is set to the size of the vector. Failure: the error message text describing the error and aVec is cleared (i.e. has no elements).

Definition at line 319 of file XrdNetUtils.cc.

323 {
324  const char *eText;
325  hpSpec aInfo(opts);
326 
327 // Clear the result vector and make sure we have something to do
328 //
329  aVec.clear();
330  if (ordn) *ordn = 0;
331  if (!hSVec.size()) return 0;
332 
333 // Parse the options
334 //
335  GetHints(aInfo, opts);
336 
337 // Process each specification
338 //
339  for (int i = 0; i < (int)hSVec.size(); i++)
340  {if (((eText = GetHostPort(aInfo, hSVec[i].c_str(), PortInSpec))
341  || (eText = GetAInfo(aInfo))) && !force) return eText;
342  }
343 
344 // Size the vector and fill it in
345 //
346  if (aInfo.aNum4 || aInfo.aNum6)
347  {aVec.resize(aInfo.aNum4 + aInfo.aNum6);
348  FillAddr(aInfo, aVec.data(), ordn, rotNum);
349  }
350 
351 // All done
352 //
353  return 0;
354 }
static const int PortInSpec
Definition: XrdNetUtils.hh:131

References XrdNetSpace::hpSpec::aNum4, XrdNetSpace::hpSpec::aNum6, and opts.

◆ GetSokInfo()

int XrdNetUtils::GetSokInfo ( int  fd,
char *  theAddr,
int  theALen,
char &  theType 
)
static

Obtain connection information from a socket.

Parameters
fdThe file descriptor of the socket whose address is to be converted. The sign of the fd indicates which address: fd > 0 the peer address is used (i.e. getpeername) fd < 0 the local address is used (i.e. getsockname)
theAddrpointer to a buffer of theAlen bytes where the text version of the IP address is to be returned. The text uses the actual native address format. If theAddr is nil or theAlen is not positive, only the port and address type are returned.
theALenlength of the theAddr buffer.
theTypeeither the character 4 (IPv4) or 6 (IPv6) is returned. corrresponding to the address family. Note that should be AF_INET6 but the address is mapped, '4' is returned.
Returns
Success: >= 0 corresponding to the port number.
Failure: < 0 corresponding to -errno.

Definition at line 498 of file XrdNetUtils.cc.

499 {
500  XrdNetSockAddr theIP;
501  XrdNetAddr ipAddr;
502  static const int fmtopts = XrdNetAddrInfo::noPortRaw
504  SOCKLEN_t addrSize = sizeof(theIP);
505  int rc;
506  unsigned short thePort;
507 
508 // The the address wanted
509 //
510  rc = (fd > 0 ? getpeername( fd, &theIP.Addr, &addrSize)
511  : getsockname(-fd, &theIP.Addr, &addrSize));
512  if (rc) return -errno;
513 
514 // Set the address
515 //
516  if (ipAddr.Set(&theIP.Addr)) return -EAFNOSUPPORT;
517 
518 // Establis the type of address we have
519 //
520  if (ipAddr.isIPType(XrdNetAddrInfo::IPv4) || ipAddr.isMapped()) theType='4';
521  else theType = '6';
522 
523 // Now format the address
524 //
525  if (theAddr && theALen > 0
526  && !ipAddr.Format(theAddr, theALen, XrdNetAddrInfo::fmtAddr, fmtopts))
527  return -EINVAL;
528 
529 // Get the port number and return it.
530 //
531  thePort = htons((theIP.Addr.sa_family == AF_INET
532  ? theIP.v4.sin_port : theIP.v6.sin6_port));
533  return static_cast<int>(thePort);
534 }
#define SOCKLEN_t
bool isMapped() const
static const int noPortRaw
Use raw address format (no port)
static const int prefipv4
Use if mapped IPV4 actual format.
bool isIPType(IPType ipType) const
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
@ fmtAddr
Address using suitable ipv4 or ipv6 format.
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216

References XrdNetSockAddr::Addr, XrdNetAddrInfo::fmtAddr, XrdNetAddrInfo::Format(), XrdNetAddrInfo::IPv4, XrdNetAddrInfo::isIPType(), XrdNetAddrInfo::isMapped(), XrdNetAddrInfo::noPortRaw, XrdNetAddrInfo::prefipv4, XrdNetAddr::Set(), SOCKLEN_t, XrdNetSockAddr::v4, and XrdNetSockAddr::v6.

Referenced by XrdNetPMarkFF::Start().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Hosts()

XrdOucTList * XrdNetUtils::Hosts ( const char *  hSpec,
int  hPort = -1,
int  hWant = 8,
int *  sPort = 0,
const char **  eText = 0 
)
static

Obtain an easily digestable list of hosts. This is the list of up to eight unique aliases (i.e. with different addresses) assigned to a base hostname.

Parameters
hSpecthe host specification suitable for XrdNetAddr.Set().
hPortWhen >= 0 specified the port to use regardless of hSpec. When < 0 the port must be present in hSpec.
hWantMaximum number of list entries wanted. If hWant is greater that eight it is set eigth.
sPortIf not nil, the *sPort will be set to hPort if and only if the IP address in one of the entries matches the host address. Otherwise, the value is unchanged.
eTextWhen not nil, is where to place error message text.
Returns
Success: Pointer to a list of XrdOucTList objects where p->val is the port number p->text is the host name. The list of objects belongs to the caller. Failure: A nil pointer is returned. If eText is supplied, the error message, in persistent storage, is returned.

Definition at line 541 of file XrdNetUtils.cc.

543 {
544  static const int hMax = 8;
545  XrdNetAddr myAddr(0), aList[hMax];
546  XrdOucTList *tList = 0;
547  const char *etext, *hName;
548  int numIP, i, k;
549 
550 // Check if the port must be in the spec and set maximum
551 //
552  if (hPort < 0) hPort = XrdNetAddr::PortInSpec;
553  if (hWant > hMax) hWant = hMax;
554  else if (hWant < 1) hWant = 1;
555 
556 // Initialze the list of addresses
557 //
558  if ((etext = aList[0].Set(hSpec, numIP, hWant, hPort)))
559  {if (eText) *eText = etext;
560  return 0;
561  }
562 
563 // Create the tlist object list without duplicates. We may have duplicates as
564 // this may be a multi-homed node and we don't want to show that here.
565 //
566  for (i = 0; i < numIP; i++)
567  {if (sPort && myAddr.Same(&aList[i]))
568  {*sPort = aList[i].Port(); sPort = 0;}
569  hName = aList[i].Name("");
570  for (k = 0; k < i; k++) {if (!strcmp(hName, aList[k].Name(""))) break;}
571  if (k >= i) tList = new XrdOucTList(hName, aList[i].Port(), tList);
572  }
573 
574 // All done, return the result
575 //
576  if (eText) *eText = (tList ? 0 : "unknown processing error");
577  return tList;
578 }
const char * Name(const char *eName=0, const char **eText=0)
int Port(int pNum=-1)
Definition: XrdNetAddr.cc:156
static const int PortInSpec
Definition: XrdNetAddr.hh:112
static int Port(int fd, const char **eText=0)
Definition: XrdNetUtils.cc:780

References XrdNetAddrInfo::Name(), XrdNetAddr::Port(), and XrdNetAddr::PortInSpec.

Referenced by XrdCmsUtils::ParseMan().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IPFormat() [1/2]

int XrdNetUtils::IPFormat ( const struct sockaddr *  sAddr,
char *  bP,
int  bL,
int  opts = 0 
)
static

Definition at line 584 of file XrdNetUtils.cc.

586 {
587  XrdNetAddr theAddr;
588  int fmtopts = (opts & oldFmt ? XrdNetAddrInfo::old6Map4 : 0);
589 
590 // Set the address
591 //
592  if (theAddr.Set(sAddr)) return 0;
593 
594 // Now format the address
595 //
596  if (opts & noPort) fmtopts |= XrdNetAddrInfo::noPort;
597  return theAddr.Format(bP, bL, XrdNetAddrInfo::fmtAdv6, fmtopts);
598 }
static const int noPort
Do not add port number.
static const int old6Map4
Use deprecated IPV6 mapped format.
static const int oldFmt
Definition: XrdNetUtils.hh:256
static const int noPort
Definition: XrdNetUtils.hh:255

References XrdNetAddrInfo::fmtAdv6, XrdNetAddrInfo::Format(), XrdNetAddrInfo::noPort, XrdNetAddrInfo::old6Map4, opts, and XrdNetAddr::Set().

Referenced by XrdSecProtocolsss::getCredentials(), XrdCl::Socket::GetPeerName(), and XrdCl::Socket::GetSockName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IPFormat() [2/2]

int XrdNetUtils::IPFormat ( int  fd,
char *  bP,
int  bL,
int  opts = 0 
)
static

Convert an IP socket address/port (V4 or V6) into the standard V6 RFC ASCII representation: "[address]:port".

Parameters
fdThe file descriptor of the socket whose address is to be converted. The sign of the fd indicates which address: fd > 0 the peer address is used (i.e. getpeername) fd < 0 the local address is used (i.e. getsockname)
bPpoints to a buffer large enough to hold the result. A buffer 64 characters long will always be big enough.
bLthe actual size of the buffer.
optsFormating options: noPort - does not suffix the port number with ":port". oldFmt - use the deprecated format for an IPV4 mapped address: [::d.d.d.d] vs [::ffff:d.d.d.d].
Returns
Success: The length of the formatted address is returned.
Failure: Zero is returned and the buffer state is undefined. Failure occurs when the buffer is too small or the file descriptor does not refer to an open socket.

Definition at line 602 of file XrdNetUtils.cc.

603 {
604  XrdNetSockAddr theIP;
605  SOCKLEN_t addrSize = sizeof(theIP);
606  int rc;
607 
608 // The the address wanted
609 //
610  rc = (fd > 0 ? getpeername( fd, &theIP.Addr, &addrSize)
611  : getsockname(-fd, &theIP.Addr, &addrSize));
612  if (rc) return 0;
613 
614 // Now format it
615 //
616  return IPFormat(&theIP.Addr, bP, bL, opts);
617 }
static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
Definition: XrdNetUtils.cc:584

References XrdNetSockAddr::Addr, opts, and SOCKLEN_t.

◆ Match()

bool XrdNetUtils::Match ( const char *  hName,
const char *  pattern 
)
static

Determine if a hostname matches a pattern.

Parameters
hNamethe name of the host.
patternthe pattern to match against. The pattern may contain one If the pattern contains a single asterisk, then the prefix of hName is compared with the characters before the '*' and the suffix of hName is compared with the character after. If the pattern ends with a plus, the all then pattern is taken as a hostname (less '+') and expanded to all possible hostnames and each one is compared with hName. If the pattern contains both, the asterisk rule is used first. If it contains neither then strict equality is used.
Returns
Success: True, the pattern matches. Failure: False, no match found.

Definition at line 623 of file XrdNetUtils.cc.

624 {
625  static const int maxIP = 16;
626  const char *mval;
627  int i, j, k;
628 
629 // First check if this will match right away
630 //
631  if (!strcmp(HostPat, HostName)) return true;
632 
633 // Check for an asterisk do prefix/suffix match
634 //
635  if ((mval = index(HostPat, (int)'*')))
636  { i = mval - HostPat; mval++;
637  k = strlen(HostName); j = strlen(mval);
638  if ((i+j) > k
639  || strncmp(HostName, HostPat,i)
640  || strncmp((HostName+k-j),mval,j)) return false;
641  return 1;
642  }
643 
644 // Now check for host expansion
645 //
646  i = strlen(HostPat);
647  if (i && HostPat[i-1] == '+')
648  {XrdNetAddr InetAddr[maxIP];
649  char hBuff[264];
650  if (i >= (int)sizeof(hBuff)) return false;
651  memcpy(hBuff, HostPat, i-1);
652  hBuff[i-1] = 0;
653  if (InetAddr[0].Set(hBuff, i, maxIP, 0)) return false;
654  while(i--) if ((mval = InetAddr[i].Name()) && !strcmp(mval, HostName))
655  return true;
656  }
657 
658 // No matches
659 //
660  return false;
661 }

Referenced by XrdOucUtils::doIf().

+ Here is the caller graph for this function:

◆ MyHostName()

char * XrdNetUtils::MyHostName ( const char *  eName = "*unknown*",
const char **  eText = 0 
)
static

Get the fully qualified name of the current host.

Parameters
eNameThe name to be returned when the host name or its true address could not be returned. The pointer may be nil.
eTextWhen supplied will hold 0 if no errors occurred or error message text, in persistent storage, describing why the error-triggered alternate name was returned. If it contains neither then strict equality is used.
Returns
An strdup() copy of the host name, address , or eName; unless eName is nil, in which case a nil pointer is returned. The caller is responsible for freeing any returned string using free().

Definition at line 667 of file XrdNetUtils.cc.

668 {
669  const char *fqn = XrdNetIdentity::FQN(eText);
670 
671 // Return the appropriate result
672 //
673  if (!fqn) fqn = eName;
674  return (fqn ? strdup(fqn) : 0);
675 }
static const char * FQN(const char **etext=0)

References XrdNetIdentity::FQN().

Referenced by XrdNetPMarkCfg::Config(), XrdSecProtocolsss::Load_Client(), and XrdSecProtocolkrb5Init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ NetConfig()

XrdNetUtils::NetProt XrdNetUtils::NetConfig ( XrdNetUtils::NetType  netquery = qryINET,
const char **  eText = 0 
)
static

Definition at line 681 of file XrdNetUtils.cc.

683 {
684  XrdNetAddr *myAddrs;
685  const char *eMsg;
686  char buff[1024];
687  NetProt hasProt = hasNone;
688  int aCnt, ifType;
689 
690 // Make sure we support this query
691 //
692  if (netquery != qryINET && netquery != qryINIF)
693  {if (eText) *eText = "unsupported NetType query";
694  return hasNone;
695  }
696 
697 // We base the nonfig of the interface addresses unless we can't query the if's
698 //
699  if (netquery == qryINIF && (ifType = XrdNetIF::GetIF((XrdOucTList **)0,0)))
700  {if (ifType & XrdNetIF::haveIPv4) hasProt = NetProt(hasProt | hasIPv4);
701  if (ifType & XrdNetIF::haveIPv6) hasProt = NetProt(hasProt | hasIPv6);
702  if (ifType & XrdNetIF::havePub4) hasProt = NetProt(hasProt | hasPub4);
703  if (ifType & XrdNetIF::havePub6) hasProt = NetProt(hasProt | hasPub6);
704  return hasProt;
705  }
706 
707 // Get our host name and initialize this object with it
708 //
709  gethostname(buff, sizeof(buff));
710  XrdOucUtils::toLower(buff);
711 
712 // Now get all of the addresses associated with this hostname
713 //
714  if ((eMsg = GetAddrs(buff, &myAddrs, aCnt, allIPv64, NoPortRaw)))
715  {if (eText) *eText = eMsg;
716  return hasNone;
717  }
718 
719 // Now run through all of the addresses to see what we have
720 //
721  for (int i = 0; i < aCnt && hasProt != hasIP64; i++)
722  { if (myAddrs[i].isIPType(XrdNetAddrInfo::IPv6))
723  {hasProt = NetProt(hasProt | hasIPv6);
724  if (!myAddrs[i].isPrivate())
725  hasProt = NetProt(hasProt | hasPub6);
726  }
727  else if (myAddrs[i].isIPType(XrdNetAddrInfo::IPv4))
728  {hasProt = NetProt(hasProt | hasIPv4);
729  if (!myAddrs[i].isPrivate())
730  hasProt = NetProt(hasProt | hasPub4);
731  }
732  }
733 
734 // Delete the array and return what we have
735 //
736  delete [] myAddrs;
737  if (hasProt == hasNone && eText) *eText = "";
738  return hasProt;
739 }
#define eMsg(x)
static const int haveIPv4
ifList == 0 && non-local ipv4 i/f found (or'd)
Definition: XrdNetIF.hh:196
static int GetIF(XrdOucTList **ifList, const char **eText=0)
Definition: XrdNetIF.cc:429
static const int havePub6
ifList == 0 && public ipv6 i/f found (or'd)
Definition: XrdNetIF.hh:206
static const int havePub4
ifList == 0 && public ipv4 i/f found (or'd)
Definition: XrdNetIF.hh:204
static const int haveIPv6
ifList == 0 && non-local ipv6 i/f found (or'd)
Definition: XrdNetIF.hh:198
static const int NoPortRaw
Definition: XrdNetUtils.hh:132
static const char * GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
Definition: XrdNetUtils.cc:239
static void toLower(char *str)

References eMsg, XrdNetIF::GetIF(), XrdNetIF::haveIPv4, XrdNetIF::haveIPv6, XrdNetIF::havePub4, XrdNetIF::havePub6, XrdNetAddrInfo::IPv4, XrdNetAddrInfo::IPv6, and XrdOucUtils::toLower().

Referenced by XrdCl::Stream::Stream().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Parse()

bool XrdNetUtils::Parse ( const char *  hSpec,
const char **  hName,
const char **  hNend,
const char **  hPort,
const char **  hPend 
)
static

Parse an IP or host name specification.

Parameters
hSpecthe name or IP address of the host. As one of the following "[<ipv6>]:<port>", "<ipv4>:<port>", or "<name>:<port>".
hNameplace where the starting address of the host is placed.
hNendplace where the ending address+1 is placed. This will point to either ']', ':', or a null byte.
hPortplace where the starting address of the port is placed. If no ":port" was found, this will contain *hNend.
hPendplace where the ending address+1 is placed. If no port If no ":port" was found, this will contain *hNend.
Returns
Success: True. Failure: False, hSpec is not valid. Some output parameters may have been set but shlould be ignored.

Definition at line 745 of file XrdNetUtils.cc.

748 {
749  const char *asep = 0;
750 
751 // Parse the specification
752 //
753  if (*hSpec == '[')
754  {if (!(*hNend = index(hSpec+1, ']'))) return false;
755  *hName = hSpec+1; asep = (*hNend)+1;
756  } else {
757  *hName = hSpec;
758  if (!(*hNend = index(hSpec, ':'))) *hNend = hSpec + strlen(hSpec);
759  else asep = *hNend;
760  }
761 
762 // See if we have a port to parse. We stop on a non-alphameric.
763 //
764  if (asep && *asep == ':')
765  {*hPort = ++asep;
766  while(isalnum(*asep)) asep++;
767  if (*hPort == asep) return false;
768  *hPend = asep;
769  } else *hPort = *hPend = *hNend;
770 
771 // All done
772 //
773  return true;
774 }

Referenced by XrdOucUtils::isFWD(), XrdNetAddr::Set(), and XrdFfsMisc_get_list_of_data_servers().

+ Here is the caller graph for this function:

◆ Port()

int XrdNetUtils::Port ( int  fd,
const char **  eText = 0 
)
static

Obtain the numeric port associated with a file descriptor.

Parameters
fdthe file descriptor number.
eTextwhen not null, the reason for a failure is returned.
Returns
Success: The positive port number. Failure: 0 is returned and if eText is not null, the error message.

Definition at line 780 of file XrdNetUtils.cc.

781 {
782  XrdNetSockAddr Inet;
783  SOCKLEN_t slen = (socklen_t)sizeof(Inet);
784  int rc;
785 
786  if ((rc = getsockname(fd, &Inet.Addr, &slen)))
787  {rc = errno;
788  if (eText) setET(eText, errno);
789  return -rc;
790  }
791 
792  return static_cast<int>(ntohs(Inet.v6.sin6_port));
793 }

References XrdNetSockAddr::Addr, SOCKLEN_t, and XrdNetSockAddr::v6.

Referenced by XrdNet::Bind().

+ Here is the caller graph for this function:

◆ ProtoID()

int XrdNetUtils::ProtoID ( const char *  pName)
static

Obtain the protocol identifier.

Parameters
pNamethe name of the protocol (e.g. "tcp").
Returns
The protocol identifier.

Definition at line 803 of file XrdNetUtils.cc.

804 {
805 #ifdef HAVE_PROTOR
806  struct protoent pp;
807  char buff[1024];
808 #else
809  static XrdSysMutex protomutex;
810  struct protoent *pp;
811  int protoid;
812 #endif
813 
814 // Note that POSIX did include getprotobyname_r() in the last minute. Many
815 // platforms do not document this variant but just about all include it.
816 //
817 #ifdef __solaris__
818  if (!getprotobyname_r(pname, &pp, buff, sizeof(buff)))
819  return IPPROTO_TCP;
820  return pp.p_proto;
821 #elif !defined(HAVE_PROTOR)
822  protomutex.Lock();
823  if (!(pp = getprotobyname(pname))) protoid = IPPROTO_TCP;
824  else protoid = pp->p_proto;
825  protomutex.UnLock();
826  return protoid;
827 #else
828  struct protoent *ppp;
829  if (getprotobyname_r(pname, &pp, buff, sizeof(buff), &ppp))
830  return IPPROTO_TCP;
831  return pp.p_proto;
832 #endif
833 }
#define IPPROTO_TCP
Definition: XrdNetUtils.cc:800

References IPPROTO_TCP, XrdSysMutex::Lock(), and XrdSysMutex::UnLock().

Referenced by XrdNetSocket::setOpts().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ServPort()

int XrdNetUtils::ServPort ( const char *  sName,
bool  isUDP = false,
const char **  eText = 0 
)
static

Obtain the numeric port corresponding to a symbolic name.

Parameters
sNamethe name of the service or a numeric port number.
isUDPif true, returns the UDP service port o/w the TCP service
eTextwhen not null, the reason for a failure is returned.
Returns
Success: The positive port number. Failure: 0 is returned and if eText is not null, the error message.

Definition at line 839 of file XrdNetUtils.cc.

840 {
841  struct addrinfo *rP = 0, myHints;
842  int rc, portnum = 0;
843 
844 // First check if this is a plain number
845 //
846  if (isdigit(*sName))
847  {char *send;
848  portnum = strtol(sName, &send, 10);
849  if (portnum > 0 && portnum < 65536 && *send == 0) return portnum;
850  if (eText) *eText = "invalid port number";
851  return 0;
852  }
853 
854 // Fill out the hints
855 //
856  memset(&myHints, 0, sizeof(myHints));
857  myHints.ai_socktype = (isUDP ? SOCK_DGRAM : SOCK_STREAM);
858 
859 // Try to find the port number
860 //
861  rc = getaddrinfo(0, sName, &myHints, &rP);
862  if (rc || !rP)
863  {if (eText) *eText = (rc ? gai_strerror(rc) : "service not found");
864  if (rP) freeaddrinfo(rP);
865  return 0;
866  }
867 
868 // Return the port number
869 //
870  portnum = int(ntohs(SIN_PORT(rP))) & 0x0000ffff;
871  freeaddrinfo(rP);
872  if (!portnum && eText) *eText = "service has no port";
873  return portnum;
874 }
#define SIN_PORT(x)
Definition: XrdNetUtils.cc:164

References SIN_PORT.

Referenced by XrdOuca2x::a2p(), XrdCmsUtils::ParseMan(), and XrdNetAddr::Set().

+ Here is the caller graph for this function:

◆ SetAuto()

int XrdNetUtils::SetAuto ( XrdNetUtils::AddrOpts  aOpts = allIPMap)
static

Set the family and hints to be used in GetAddrs() with prefAuto. This is used within this class and by XrdNetAddr when the IP mode changes. It is meant for internal use only.

Parameters
aOptsIs one of the following from the AddrOpts enum: allIPMap - Use IPv6 and mapped IPv4 addrs (default) onlyIPv4 - Use only IPv4 addresses. prefAuto - Determine proper options based on configuration.
Returns
The getaddrinfo() hints value that should be used.

Definition at line 880 of file XrdNetUtils.cc.

881 {
882 
883 // If a specific family is not specified, then determine which families to use
884 //
885  if (aOpts != onlyIPv4 && aOpts != allIPMap)
886  {int ifTypes = XrdNetIF::GetIF(0);
887  if (ifTypes & XrdNetIF::haveIPv6) aOpts = allIPMap;
888  else if (ifTypes & XrdNetIF::haveIPv4) aOpts = onlyIPv4;
889  else {autoFamily = AF_UNSPEC; autoHints = AI_V4MAPPED | AI_ADDRCONFIG;
890  return AI_V4MAPPED | AI_ADDRCONFIG;
891  }
892  }
893 
894 // If this is forced IPv4 then we know how to set the hints
895 //
896  if (aOpts == onlyIPv4)
897  {autoFamily = AF_INET; autoHints = 0; return 0;}
898 
899 // So, this is IPv6. Be as flexible as possible.
900 //
901  autoFamily = AF_INET6;
902  autoHints = AI_V4MAPPED | AI_ALL;
903  return AI_V4MAPPED | AI_ALL;
904 }

References XrdNetIF::GetIF(), XrdNetIF::haveIPv4, and XrdNetIF::haveIPv6.

Referenced by XrdNetAddr::SetIPV4(), and XrdNetAddr::SetIPV6().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Singleton()

bool XrdNetUtils::Singleton ( const char *  hSpec,
const char **  eText = 0 
)
static

Check if whether or not a host name represents more than one unique host.

Parameters
hSpecthe host specification suitable for XrdNetAddr.Set().
eTextWhen not nil, is where to place error message text.
Returns
True is this is a simple single host. False if the name represensts more than one single host.

Definition at line 921 of file XrdNetUtils.cc.

922 {
923  XrdOucTList *hList, *hNow;
924  bool isSingle;
925 
926 // Obtain a list of unique hostnames associated with this host
927 //
928  hList = Hosts(hSpec, 1234, 2, 0, eText);
929 
930 // If this is none or only one then this is a singleton
931 //
932  isSingle = !hList || hList->next == 0;
933 
934 // Free up the list of hosts
935 //
936  while((hNow = hList))
937  {hList = hList->next;
938  delete hNow;
939  };
940 
941 // All done
942 //
943  return isSingle;
944 }
static XrdOucTList * Hosts(const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0, const char **eText=0)
Definition: XrdNetUtils.cc:541
XrdOucTList * next
Definition: XrdOucTList.hh:45

References XrdOucTList::next.

Referenced by XrdSsiClientProvider::GetService().

+ Here is the caller graph for this function:

Member Data Documentation

◆ noPort

const int XrdNetUtils::noPort = 1
static

Convert an IP address/port (V4 or V6) into the standard V6 RFC ASCII representation: "[address]:port".

Parameters
sAddrAddress to convert. This is either sockaddr_in or sockaddr_in6 cast to struct sockaddr.
bPpoints to a buffer large enough to hold the result. A buffer 64 characters long will always be big enough.
bLthe actual size of the buffer.
optsFormating options: noPort - does not suffix the port number with ":port". oldFmt - use the deprecated format for an IPV4 mapped address: [::d.d.d.d] vs [::ffff:d.d.d.d].
Returns
Success: The length of the formatted address is returned.
Failure: Zero is returned and the buffer state is undefined. Failure occurs when the buffer is too small or the address family (sAddr->sa_family) is neither AF_INET nor AF_INET6.

Definition at line 255 of file XrdNetUtils.hh.

◆ NoPortRaw

const int XrdNetUtils::NoPortRaw = (int)0xC0000000
static

Definition at line 132 of file XrdNetUtils.hh.

Referenced by XrdNetAddr::Register().

◆ oldFmt

const int XrdNetUtils::oldFmt = 2
static

Definition at line 256 of file XrdNetUtils.hh.

Referenced by XrdSecProtocolsss::getCredentials().

◆ PortInSpec

const int XrdNetUtils::PortInSpec = (int)0x80000000
static

Definition at line 131 of file XrdNetUtils.hh.


The documentation for this class was generated from the following files: