XRootD
Loading...
Searching...
No Matches
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.
 
 ~XrdNetUtils ()
 Destructor.
 

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.

◆ 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.

◆ 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.
@ qryINIF
Only consider internet protocols via ifconfig.

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 944 of file XrdNetUtils.cc.

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

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

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

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

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

Referenced by XrdNetRegistry::GetAddrs(), XrdCl::Utils::GetHostAddresses(), NetConfig(), 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 275 of file XrdNetUtils.cc.

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

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

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

◆ 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 497 of file XrdNetUtils.cc.

498{
499 XrdNetSockAddr theIP;
500 XrdNetAddr ipAddr;
501 static const int fmtopts = XrdNetAddrInfo::noPortRaw
503 SOCKLEN_t addrSize = sizeof(theIP);
504 int rc;
505 unsigned short thePort;
506
507// The the address wanted
508//
509 rc = (fd > 0 ? getpeername( fd, &theIP.Addr, &addrSize)
510 : getsockname(-fd, &theIP.Addr, &addrSize));
511 if (rc) return -errno;
512
513// Set the address
514//
515 if (ipAddr.Set(&theIP.Addr)) return -EAFNOSUPPORT;
516
517// Establis the type of address we have
518//
519 if (ipAddr.isIPType(XrdNetAddrInfo::IPv4) || ipAddr.isMapped()) theType='4';
520 else theType = '6';
521
522// Now format the address
523//
524 if (theAddr && theALen > 0
525 && !ipAddr.Format(theAddr, theALen, XrdNetAddrInfo::fmtAddr, fmtopts))
526 return -EINVAL;
527
528// Get the port number and return it.
529//
530 thePort = htons((theIP.Addr.sa_family == AF_INET
531 ? theIP.v4.sin_port : theIP.v6.sin6_port));
532 return static_cast<int>(thePort);
533}
#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)

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 540 of file XrdNetUtils.cc.

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

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

Referenced by XrdCmsUtils::ParseMan(), and Singleton().

+ 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 583 of file XrdNetUtils.cc.

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

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

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

+ 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 601 of file XrdNetUtils.cc.

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

References XrdNetSockAddr::Addr, IPFormat(), opts, and SOCKLEN_t.

+ Here is the call graph for this function:

◆ 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 622 of file XrdNetUtils.cc.

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

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 666 of file XrdNetUtils.cc.

667{
668 const char *fqn = XrdNetIdentity::FQN(eText);
669
670// Return the appropriate result
671//
672 if (!fqn) fqn = eName;
673 return (fqn ? strdup(fqn) : 0);
674}
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 680 of file XrdNetUtils.cc.

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

References allIPv64, eMsg, GetAddrs(), XrdNetIF::GetIF(), hasIP64, hasIPv4, hasIPv6, hasNone, hasPub4, hasPub6, XrdNetIF::haveIPv4, XrdNetIF::haveIPv6, XrdNetIF::havePub4, XrdNetIF::havePub6, XrdNetAddrInfo::IPv4, XrdNetAddrInfo::IPv6, NoPortRaw, qryINET, and qryINIF.

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 743 of file XrdNetUtils.cc.

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

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 778 of file XrdNetUtils.cc.

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

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

Referenced by XrdNet::Bind(), and Hosts().

+ 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 801 of file XrdNetUtils.cc.

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

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 837 of file XrdNetUtils.cc.

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

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 878 of file XrdNetUtils.cc.

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

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

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 919 of file XrdNetUtils.cc.

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

References Hosts(), and XrdOucTList::next.

Referenced by XrdSsiClientProvider::GetService().

+ Here is the call graph for this function:
+ 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.

Referenced by IPFormat().

◆ NoPortRaw

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

Definition at line 132 of file XrdNetUtils.hh.

Referenced by NetConfig(), and XrdNetAddr::Register().

◆ oldFmt

const int XrdNetUtils::oldFmt = 2
static

Definition at line 256 of file XrdNetUtils.hh.

Referenced by XrdSecProtocolsss::getCredentials(), and IPFormat().

◆ PortInSpec

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

Definition at line 131 of file XrdNetUtils.hh.

Referenced by GetAddrs().


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