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) 2013 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 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 
62 static int Decode(XrdNetSockAddr *sadr, const char *buff, int blen);
63 
64 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
77 
78 static int Encode(const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1);
79 
80 
81 //------------------------------------------------------------------------------
123 //------------------------------------------------------------------------------
124 
127  prefAuto= 16, order46 = 32, order64 = 64,
128  onlyUDP =128
129  };
130 
131 static const int PortInSpec = (int)0x80000000;
132 static const int NoPortRaw = (int)0xC0000000;
133 
134 static
135 const char *GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN,
136  AddrOpts opts=allIPMap, int pNum=PortInSpec);
137 
138 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 
155 static
156 const char *GetAddrs(const std::string &hSpec, std::vector<XrdNetAddr> &aVec,
157  int *ordn=0, AddrOpts opts=allIPMap, int pNum=PortInSpec);
158 
159 //------------------------------------------------------------------------------
177 //------------------------------------------------------------------------------
178 
179 static
180 const char *GetAddrs(std::vector<std::string> &hSVec,
181  std::vector<XrdNetAddr> &aVec,
182  int *ordn=0, AddrOpts opts=allIPMap,
183  unsigned int rotNum=0, bool force=false);
184 
185 //------------------------------------------------------------------------------
204 //------------------------------------------------------------------------------
205 
206 static
207 int GetSokInfo(int fd, char *theAddr, int theALen, char &theType);
208 
209 //------------------------------------------------------------------------------
229 //------------------------------------------------------------------------------
230 
231 static
232 XrdOucTList *Hosts(const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0,
233  const char **eText=0);
234 
235 //------------------------------------------------------------------------------
253 //------------------------------------------------------------------------------
254 
255 static const int noPort = 1;
256 static const int oldFmt = 2;
257 
258 static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0);
259 
260 //------------------------------------------------------------------------------
280 //------------------------------------------------------------------------------
281 
282 static int IPFormat(int fd, char *bP, int bL, int opts=0);
283 
284 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 
302 static bool Match(const char *hName, const char *pattern);
303 
304 //------------------------------------------------------------------------------
317 //------------------------------------------------------------------------------
318 
319 static char *MyHostName(const char *eName="*unknown*", const char **eText=0);
320 
321 //------------------------------------------------------------------------------
331 //------------------------------------------------------------------------------
332 
333 enum NetProt {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  };
340 
341 enum NetType {qryINET = 0,
342  qryINIF = 1
343  };
344 
345 static NetProt NetConfig(NetType netquery=qryINET, const char **eText=0);
346 
347 //------------------------------------------------------------------------------
363 //------------------------------------------------------------------------------
364 
365 static bool Parse(const char *hSpec, const char **hName, const char **hNend,
366  const char **hPort, const char **hPend);
367 
368 //------------------------------------------------------------------------------
376 //------------------------------------------------------------------------------
377 
378 static int Port(int fd, const char **eText=0);
379 
380 //------------------------------------------------------------------------------
386 //------------------------------------------------------------------------------
387 
388 static int ProtoID(const char *pName);
389 
390 //------------------------------------------------------------------------------
399 //------------------------------------------------------------------------------
400 
401 static int ServPort(const char *sName, bool isUDP=false, const char **eText=0);
402 
403 //------------------------------------------------------------------------------
414 //------------------------------------------------------------------------------
415 
416 static int SetAuto(AddrOpts aOpts=allIPMap);
417 
418 //------------------------------------------------------------------------------
426 //------------------------------------------------------------------------------
427 
428 static bool Singleton(const char *hSpec, const char **eText=0);
429 
430 static bool ConnectWithTimeout(int sockfd, const struct sockaddr* clientAddr, size_t clientAddrLen,uint32_t timeout_sec, std::stringstream & errMsg);
431 
432 //------------------------------------------------------------------------------
434 //------------------------------------------------------------------------------
435 
437 
438 //------------------------------------------------------------------------------
440 //------------------------------------------------------------------------------
441 
443 private:
444 
445 static void FillAddr(XrdNetSpace::hpSpec &aBuff, XrdNetAddr *aVec,
446  int *ordn=0, unsigned int rotNum=0);
447 static
448 const char *GetAInfo(XrdNetSpace::hpSpec &aBuff);
449 static void GetHints(XrdNetSpace::hpSpec &aBuff, AddrOpts opts);
450 static
451 const char *GetHostPort(XrdNetSpace::hpSpec &aBuff, const char *hSpec, int pNum);
452 static
453 const char *getMyFQN(const char *&myDom);
454 static int setET(const char **errtxt, int rc);
455 static bool SetSockBlocking(int sockfd, bool blocking, std::stringstream & errMsg);
456 static int autoFamily;
457 static int autoHints;
458 };
459 
461 
462 #endif
#define XRDOUC_ENUM_OPERATORS(T)
Definition: XrdOucEnum.hh:22
struct myOpts opts
static const int NoPortRaw
Definition: XrdNetUtils.hh:132
static bool Match(const char *hName, const char *pattern)
Definition: XrdNetUtils.cc:623
@ qryINET
Only consider internet protocols via DNS.
Definition: XrdNetUtils.hh:341
@ qryINIF
Only consider internet protocols via ifconfig.
Definition: XrdNetUtils.hh:342
static char * MyHostName(const char *eName="*unknown*", const char **eText=0)
Definition: XrdNetUtils.cc:667
static int GetSokInfo(int fd, char *theAddr, int theALen, char &theType)
Definition: XrdNetUtils.cc:498
static int Encode(const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1)
Definition: XrdNetUtils.cc:120
@ hasNone
Unable to determine available protocols.
Definition: XrdNetUtils.hh:333
static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int opts=0)
Definition: XrdNetUtils.cc:584
static bool Singleton(const char *hSpec, const char **eText=0)
Definition: XrdNetUtils.cc:921
static const char * GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
Definition: XrdNetUtils.cc:239
static int Decode(XrdNetSockAddr *sadr, const char *buff, int blen)
Definition: XrdNetUtils.cc:70
static NetProt NetConfig(NetType netquery=qryINET, const char **eText=0)
Definition: XrdNetUtils.cc:681
static const int PortInSpec
Definition: XrdNetUtils.hh:131
static bool ConnectWithTimeout(int sockfd, const struct sockaddr *clientAddr, size_t clientAddrLen, uint32_t timeout_sec, std::stringstream &errMsg)
Definition: XrdNetUtils.cc:946
static int Port(int fd, const char **eText=0)
Definition: XrdNetUtils.cc:780
~XrdNetUtils()
Destructor.
Definition: XrdNetUtils.hh:442
static const int oldFmt
Definition: XrdNetUtils.hh:256
static int ProtoID(const char *pName)
Definition: XrdNetUtils.cc:803
static int ServPort(const char *sName, bool isUDP=false, const char **eText=0)
Definition: XrdNetUtils.cc:839
static XrdOucTList * Hosts(const char *hSpec, int hPort=-1, int hWant=8, int *sPort=0, const char **eText=0)
Definition: XrdNetUtils.cc:541
XrdNetUtils()
Constructor.
Definition: XrdNetUtils.hh:436
static const int noPort
Definition: XrdNetUtils.hh:255
static bool Parse(const char *hSpec, const char **hName, const char **hNend, const char **hPort, const char **hPend)
Definition: XrdNetUtils.cc:745
static int SetAuto(AddrOpts aOpts=allIPMap)
Definition: XrdNetUtils.cc:880