XRootD
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 
467 ifData *ifName[ifMax];
468 ifData *ifDest[ifMax];
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
486 XrdSysError *eDest;
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
495 netType netRoutes;
496 static int dfPort;
497 static ifData ifNull;
498 static bool rPIPA;
499 };
500 #endif
static const int haveNoGI
ifList == 0 && getifaddrs() is not supported
Definition: XrdNetIF.hh:194
char Mask()
Definition: XrdNetIF.hh:288
bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, netType nettype=netDefault, const char *xName=0)
Definition: XrdNetIF.cc:704
bool SetPublicName(const std::string &name)
Definition: XrdNetIF.hh:164
static const int haveIPv4
ifList == 0 && non-local ipv4 i/f found (or'd)
Definition: XrdNetIF.hh:196
static void SetRPIPA(bool rval)
Definition: XrdNetIF.cc:892
int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:133
static bool InDomain(XrdNetAddrInfo *epaddr)
Definition: XrdNetIF.cc:622
XrdNetIF()
Constructor and Destructor.
Definition: XrdNetIF.hh:431
void Display(const char *pfx="=====> ")
Definition: XrdNetIF.cc:142
int GetPublicName(char *nbuff, int &nport)
Definition: XrdNetIF.hh:150
static int GetIF(XrdOucTList **ifList, const char **eText=0)
Definition: XrdNetIF.cc:429
static void Routing(netType nettype)
Definition: XrdNetIF.cc:686
static char Mask(ifType ifT)
Definition: XrdNetIF.hh:298
int Port()
Definition: XrdNetIF.hh:322
~XrdNetIF()
Definition: XrdNetIF.hh:433
static void SetMsgs(XrdSysError *erp)
Definition: XrdNetIF.cc:886
bool HasDest(ifType ifT=PublicV6)
Definition: XrdNetIF.hh:267
static void PortDefault(int pnum=1094)
Definition: XrdNetIF.cc:680
static const int havePub6
ifList == 0 && public ipv6 i/f found (or'd)
Definition: XrdNetIF.hh:206
static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
Definition: XrdNetIF.hh:250
static bool SetIFNames(char *ifnames)
Definition: XrdNetIF.cc:779
static const char * Name(ifType ifT)
Definition: XrdNetIF.hh:312
int GetPublicDest(char *dest, size_t dlen)
Definition: XrdNetIF.cc:412
int GetName(const char *&name, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:115
static const int havePub4
ifList == 0 && public ipv4 i/f found (or'd)
Definition: XrdNetIF.hh:204
int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false)
Definition: XrdNetIF.cc:389
static const int haveIPv6
ifList == 0 && non-local ipv6 i/f found (or'd)
Definition: XrdNetIF.hh:198
ifType
The enum that is used to index into ifData to get appropriate interface.
Definition: XrdNetIF.hh:65
@ PrivateV4
Definition: XrdNetIF.hh:66
@ Public46
Definition: XrdNetIF.hh:71
@ PublicV4
Definition: XrdNetIF.hh:65
@ Private64
Definition: XrdNetIF.hh:74
@ Public64
Definition: XrdNetIF.hh:73
@ PrivateIF
Definition: XrdNetIF.hh:69
@ Private46
Definition: XrdNetIF.hh:72
@ PrivateV6
Definition: XrdNetIF.hh:68
@ PublicV6
Definition: XrdNetIF.hh:67
static const int havePrv6
ifList == 0 && private ipv6 i/f found (or'd)
Definition: XrdNetIF.hh:202
static void Privatize(ifType &x)
Definition: XrdNetIF.hh:330
@ netSplit
Definition: XrdNetIF.hh:364
@ netDefault
Definition: XrdNetIF.hh:364
@ netCommon
Definition: XrdNetIF.hh:364
@ netLocal
Definition: XrdNetIF.hh:364
static const int havePrv4
ifList == 0 && private ipv4 i/f found (or'd)
Definition: XrdNetIF.hh:200