xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdNetIF.hh
Go to the documentation of this file.
1 #ifndef __XRDNETIF_HH__
2 #define __XRDNETIF_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t I F . 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 <cstdlib>
34 #include <cstring>
35 #include <string>
36 
37 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 
43 class XrdNetAddrInfo;
44 class XrdOucTList;
45 class XrdSysError;
46 
47 struct sockaddr;
48 
49 class XrdNetIF
50 {
51 public:
52 
53 //------------------------------------------------------------------------------
57 //------------------------------------------------------------------------------
58 
59  void Display(const char *pfx="=====> ");
60 
61 //------------------------------------------------------------------------------
63 //------------------------------------------------------------------------------
64 
65  enum ifType {PublicV4 = 0, //<! Public IPv4 network
66  PrivateV4 = 1, //<! Private IPv4 network
67  PublicV6 = 2, //<! Public IPv6 network
68  PrivateV6 = 3, //<! Private IPv6 network
69  PrivateIF = 1, //<! Bit to change PublicVx -> PrivateVx
70  ifNum = 4, //<! Count of actual interface types
71  Public46 = 4, //<! Public v4|6 network (dual stack)
72  Private46 = 5, //<! Private v4|6 network (dual stack)
73  Public64 = 6, //<! Public v6|4 network (dual stack)
74  Private64 = 7, //<! Private v6|4 network (dual stack)
75  ifMax = 8, //<! Total elements in if vector
76  ifAny = 8}; //<! Used to select any avilable i/f
77 
78 //------------------------------------------------------------------------------
89 //------------------------------------------------------------------------------
90 
91  int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false);
92 
93 //------------------------------------------------------------------------------
101 //------------------------------------------------------------------------------
102 
103  int GetPublicDest(char *dest, size_t dlen);
104 
105 //------------------------------------------------------------------------------
113 //------------------------------------------------------------------------------
114 
115 inline int GetName(const char *&name, ifType ifT=PublicV6)
116  {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail);
117  name = ifName[ifT]->iVal;
118  return ifName[ifT]->iLen;
119  }
120 
121 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 
133 inline int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
134  {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail);
135  strcpy(nbuff, ifName[ifT]->iVal); nport = ifPort;
136  return ifName[ifT]->iLen;
137  }
138 
139 //------------------------------------------------------------------------------
149 //------------------------------------------------------------------------------
150 int GetPublicName(char *nbuff, int &nport)
151 {
152  strcpy(nbuff, m_PublicName.c_str());
153  nport = ifPort;
154  return m_PublicName.size();
155 }
156 
157 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 bool SetPublicName(const std::string &name)
165 {
166  // TODO: Actually validate this is an acceptable DNS name.
167  if (name.size() > 255) return false;
168  m_PublicName = name;
169  return true;
170 }
171 
172 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 
193 static
194 const int haveNoGI = 0;
195 static
196 const int haveIPv4 = 1;
197 static
198 const int haveIPv6 = 2;
199 static
200 const int havePrv4 = 4;
201 static
202 const int havePrv6 = 8;
203 static
204 const int havePub4 =16;
205 static
206 const int havePub6 =32;
207 
208 static int GetIF(XrdOucTList **ifList, const char **eText=0);
209 
210 //------------------------------------------------------------------------------
221 //------------------------------------------------------------------------------
222 
223 static int GetIF(char *buff, int blen, const char **eText=0, bool show=false);
224 
225 //------------------------------------------------------------------------------
236 //------------------------------------------------------------------------------
237 
238 static int GetIF(char *&ifline, const char **eText=0, bool show=false);
239 
240 //------------------------------------------------------------------------------
248 //------------------------------------------------------------------------------
249 
250 static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
251  {ifType ifT;
252  if (conIPv4) ifT = (hasIP64 ? Public46 : PublicV4);
253  else ifT = (hasIP64 ? Public64 : PublicV6);
254  if (pvtIP) Privatize(ifT);
255  return ifT;
256  }
257 
258 //------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------
266 
267 inline bool HasDest(ifType ifT=PublicV6)
268  {return ifT >= ifAny || ifDest[ifT]->iLen != 0;}
269 
270 //------------------------------------------------------------------------------
278 //------------------------------------------------------------------------------
279 
280 static bool InDomain(XrdNetAddrInfo *epaddr);
281 
282 //------------------------------------------------------------------------------
286 //------------------------------------------------------------------------------
287 
288  char Mask() {return ifMask;}
289 
290 //------------------------------------------------------------------------------
296 //------------------------------------------------------------------------------
297 
298 static char Mask(ifType ifT)
299  {if (ifT >= ifAny) return 0x0f;
300  return ifMaskVec[ifT];
301  }
302 
303 //------------------------------------------------------------------------------
310 //------------------------------------------------------------------------------
311 static
312 const char *Name(ifType ifT) {if (ifT >= ifAny) return "any";
313  return ifTName[ifT];
314  }
315 
316 //------------------------------------------------------------------------------
320 //------------------------------------------------------------------------------
321 
322 inline int Port() {return ifPort;}
323 
324 //------------------------------------------------------------------------------
328 //------------------------------------------------------------------------------
329 
330 static void Privatize(ifType &x) {x = ifType(x | PrivateIF);}
331 
332 //------------------------------------------------------------------------------
338 //------------------------------------------------------------------------------
339 
340  int Port(int pnum);
341 
342 //------------------------------------------------------------------------------
346 //------------------------------------------------------------------------------
347 
348 static void PortDefault(int pnum=1094);
349 
350 //------------------------------------------------------------------------------
362 //------------------------------------------------------------------------------
363 
365 
366 //------------------------------------------------------------------------------
370 //------------------------------------------------------------------------------
371 
372 static void Routing(netType nettype);
373 
374 //------------------------------------------------------------------------------
391 //------------------------------------------------------------------------------
392 
393  bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0,
394  netType nettype=netDefault, const char *xName=0);
395 
396 //------------------------------------------------------------------------------
404 //------------------------------------------------------------------------------
405 
406 static bool SetIFNames(char *ifnames);
407 
408 //------------------------------------------------------------------------------
414 //------------------------------------------------------------------------------
415 
416 static void SetMsgs(XrdSysError *erp);
417 
418 //------------------------------------------------------------------------------
423 //------------------------------------------------------------------------------
424 
425 static void SetRPIPA(bool rval);
426 
427 //------------------------------------------------------------------------------
429 //------------------------------------------------------------------------------
430 
431  XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {}
432 
433  ~XrdNetIF() {if (ifBuff) free(ifBuff);}
434 
435 private:
436 
437 struct ifAddrs
438  {short hALen;
439  short hDLen;
440  bool ipV6;
441  bool prvt;
442  char hAddr[64]; // address
443  char hDest[64]; // address possibly in deprecated format
444  };
445 
446 bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src);
447 bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6);
448 bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0);
449 static const
450 char *GetDomain();
451 static
452 bool IsOkName(const char *ifn, short &ifIdx);
453 void SetIFPP();
454 bool SetIF64(bool retVal);
455 static
456 bool V4LinkLocal(struct sockaddr *saP);
457 
458 struct ifData
459 {
460  short iLen;
461  char iVal[6]; // Actually of size iLen
462 
463  ifData() : iLen(0) {*iVal = 0;}
464  ~ifData() {}
465 };
466 
469 bool ifxDNS[ifMax];
470 char *ifBuff;
471 
472 struct pInfo {char len;
473  char val[7]; // Contains ":12345\0"
474  pInfo() : len(0) {*val = 0;}
475  } portSfx;
476 
477 int ifPort;
478 short ifRoute;
479 char ifMask;
480 char ifAvail;
481 
482 // The public hostname of the interface.
483 std::string m_PublicName;
484 
485 static
487 static
488 const char *myDomain;
489 static char *ifCfg[2];
490 static
491 const char *ifTName[ifMax];
492 static
493 const char *ifMaskVec;
494 static
496 static int dfPort;
497 static ifData ifNull;
498 static bool rPIPA;
499 };
500 #endif
Definition: XrdNetIF.hh:73
int GetPublicName(char *nbuff, int &nport)
Definition: XrdNetIF.hh:150
bool prvt
Definition: XrdNetIF.hh:441
char Mask()
Definition: XrdNetIF.hh:288
static int GetIF(XrdOucTList **ifList, const char **eText=0)
int GetName(const char *&name, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:115
ifData()
Definition: XrdNetIF.hh:463
static const int havePub4
ifList == 0 &amp;&amp; public ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:204
~XrdNetIF()
Definition: XrdNetIF.hh:433
static XrdSysError * eDest
Definition: XrdNetIF.hh:486
netType
Definition: XrdNetIF.hh:364
static netType netRoutes
Definition: XrdNetIF.hh:495
char ifAvail
Definition: XrdNetIF.hh:480
Definition: XrdNetIF.hh:364
static void SetRPIPA(bool rval)
short hDLen
Definition: XrdNetIF.hh:439
static const int haveNoGI
ifList == 0 &amp;&amp; getifaddrs() is not supported
Definition: XrdNetIF.hh:194
int ifPort
Definition: XrdNetIF.hh:477
static char Mask(ifType ifT)
Definition: XrdNetIF.hh:298
bool SetIF64(bool retVal)
Definition: XrdNetIF.hh:67
int GetPublicDest(char *dest, size_t dlen)
Definition: XrdNetIF.hh:75
bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, netType nettype=netDefault, const char *xName=0)
Definition: XrdNetIF.hh:472
bool ifxDNS[ifMax]
Definition: XrdNetIF.hh:469
Definition: XrdNetIF.hh:49
static bool InDomain(XrdNetAddrInfo *epaddr)
pInfo()
Definition: XrdNetIF.hh:474
Definition: XrdNetIF.hh:74
char hAddr[64]
Definition: XrdNetIF.hh:442
bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src)
XrdNetIF()
Constructor and Destructor.
Definition: XrdNetIF.hh:431
Definition: XrdSysError.hh:89
char iVal[6]
Definition: XrdNetIF.hh:461
static bool V4LinkLocal(struct sockaddr *saP)
int Port()
Definition: XrdNetIF.hh:322
static ifData ifNull
Definition: XrdNetIF.hh:497
static const char * GetDomain()
static const int havePrv6
ifList == 0 &amp;&amp; private ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:202
static const int havePrv4
ifList == 0 &amp;&amp; private ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:200
static const int havePub6
ifList == 0 &amp;&amp; public ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:206
ifData * ifName[ifMax]
Definition: XrdNetIF.hh:467
ifType
The enum that is used to index into ifData to get appropriate interface.
Definition: XrdNetIF.hh:65
bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0)
static bool IsOkName(const char *ifn, short &ifIdx)
short ifRoute
Definition: XrdNetIF.hh:478
Definition: XrdOucTList.hh:41
char ifMask
Definition: XrdNetIF.hh:479
Definition: XrdNetIF.hh:71
char len
Definition: XrdNetIF.hh:472
std::string m_PublicName
Definition: XrdNetIF.hh:483
Definition: XrdNetIF.hh:65
char * ifBuff
Definition: XrdNetIF.hh:470
Definition: XrdNetAddrInfo.hh:53
~ifData()
Definition: XrdNetIF.hh:464
int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:133
static void SetMsgs(XrdSysError *erp)
static const char * myDomain
Definition: XrdNetIF.hh:488
static const int haveIPv4
ifList == 0 &amp;&amp; non-local ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:196
short hALen
Definition: XrdNetIF.hh:438
bool HasDest(ifType ifT=PublicV6)
Definition: XrdNetIF.hh:267
static void PortDefault(int pnum=1094)
Definition: XrdNetIF.hh:364
char val[7]
Definition: XrdNetIF.hh:473
static int dfPort
Definition: XrdNetIF.hh:496
Definition: XrdNetIF.hh:76
Definition: XrdNetIF.hh:437
Definition: XrdNetIF.hh:69
Definition: XrdNetIF.hh:72
ifData * ifDest[ifMax]
Definition: XrdNetIF.hh:468
struct XrdNetIF::pInfo portSfx
static bool rPIPA
Definition: XrdNetIF.hh:498
static char * ifCfg[2]
Definition: XrdNetIF.hh:489
static const char * ifTName[ifMax]
Definition: XrdNetIF.hh:491
Definition: XrdNetIF.hh:364
static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
Definition: XrdNetIF.hh:250
void Display(const char *pfx="=====> ")
Definition: XrdNetIF.hh:364
bool SetPublicName(const std::string &name)
Definition: XrdNetIF.hh:164
Definition: XrdNetIF.hh:70
void SetIFPP()
short iLen
Definition: XrdNetIF.hh:460
static void Routing(netType nettype)
bool ipV6
Definition: XrdNetIF.hh:440
static const int haveIPv6
ifList == 0 &amp;&amp; non-local ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:198
static bool SetIFNames(char *ifnames)
Definition: XrdNetIF.hh:68
int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false)
static const char * ifMaskVec
Definition: XrdNetIF.hh:493
char hDest[64]
Definition: XrdNetIF.hh:443
Definition: XrdNetIF.hh:66
static void Privatize(ifType &x)
Definition: XrdNetIF.hh:330
static const char * Name(ifType ifT)
Definition: XrdNetIF.hh:312
Definition: XrdNetIF.hh:458