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 <stdlib.h>
34 #include <string.h>
35 
36 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
42 class XrdNetAddrInfo;
43 class XrdOucTList;
44 class XrdSysError;
45 
46 struct sockaddr;
47 
48 class XrdNetIF
49 {
50 public:
51 
52 //------------------------------------------------------------------------------
56 //------------------------------------------------------------------------------
57 
58  void Display(const char *pfx="=====> ");
59 
60 //------------------------------------------------------------------------------
62 //------------------------------------------------------------------------------
63 
64  enum ifType {PublicV4 = 0, //<! Public IPv4 network
65  PrivateV4 = 1, //<! Private IPv4 network
66  PublicV6 = 2, //<! Public IPv6 network
67  PrivateV6 = 3, //<! Private IPv6 network
68  PrivateIF = 1, //<! Bit to change PublicVx -> PrivateVx
69  ifNum = 4, //<! Count of actual interface types
70  Public46 = 4, //<! Public v4|6 network (dual stack)
71  Private46 = 5, //<! Private v4|6 network (dual stack)
72  Public64 = 6, //<! Public v6|4 network (dual stack)
73  Private64 = 7, //<! Private v6|4 network (dual stack)
74  ifMax = 8, //<! Total elements in if vector
75  ifAny = 8}; //<! Used to select any avilable i/f
76 
77 //------------------------------------------------------------------------------
88 //------------------------------------------------------------------------------
89 
90  int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false);
91 
92 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
101 
102 inline int GetName(const char *&name, ifType ifT=PublicV6)
103  {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail);
104  name = ifName[ifT]->iVal;
105  return ifName[ifT]->iLen;
106  }
107 
108 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 
120 inline int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
121  {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail);
122  strcpy(nbuff, ifName[ifT]->iVal); nport = ifPort;
123  return ifName[ifT]->iLen;
124  }
125 
126 //------------------------------------------------------------------------------
145 //------------------------------------------------------------------------------
146 
147 static
148 const int haveNoGI = 0;
149 static
150 const int haveIPv4 = 1;
151 static
152 const int haveIPv6 = 2;
153 static
154 const int havePrv4 = 4;
155 static
156 const int havePrv6 = 8;
157 static
158 const int havePub4 =16;
159 static
160 const int havePub6 =32;
161 
162 static int GetIF(XrdOucTList **ifList, const char **eText=0);
163 
164 //------------------------------------------------------------------------------
175 //------------------------------------------------------------------------------
176 
177 static int GetIF(char *buff, int blen, const char **eText=0, bool show=false);
178 
179 //------------------------------------------------------------------------------
190 //------------------------------------------------------------------------------
191 
192 static int GetIF(char *&ifline, const char **eText=0, bool show=false);
193 
194 //------------------------------------------------------------------------------
202 //------------------------------------------------------------------------------
203 
204 static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
205  {ifType ifT;
206  if (conIPv4) ifT = (hasIP64 ? Public46 : PublicV4);
207  else ifT = (hasIP64 ? Public64 : PublicV6);
208  if (pvtIP) Privatize(ifT);
209  return ifT;
210  }
211 
212 //------------------------------------------------------------------------------
219 //------------------------------------------------------------------------------
220 
221 inline bool HasDest(ifType ifT=PublicV6)
222  {return ifT >= ifAny || ifDest[ifT]->iLen != 0;}
223 
224 //------------------------------------------------------------------------------
232 //------------------------------------------------------------------------------
233 
234 static bool InDomain(XrdNetAddrInfo *epaddr);
235 
236 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 
242  char Mask() {return ifMask;}
243 
244 //------------------------------------------------------------------------------
250 //------------------------------------------------------------------------------
251 
252 static char Mask(ifType ifT)
253  {if (ifT >= ifAny) return 0x0f;
254  return ifMaskVec[ifT];
255  }
256 
257 //------------------------------------------------------------------------------
264 //------------------------------------------------------------------------------
265 static
266 const char *Name(ifType ifT) {if (ifT >= ifAny) return "any";
267  return ifTName[ifT];
268  }
269 
270 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
275 
276 inline int Port() {return ifPort;}
277 
278 //------------------------------------------------------------------------------
282 //------------------------------------------------------------------------------
283 
284 static void Privatize(ifType &x) {x = ifType(x | PrivateIF);}
285 
286 //------------------------------------------------------------------------------
292 //------------------------------------------------------------------------------
293 
294  int Port(int pnum);
295 
296 //------------------------------------------------------------------------------
302 //------------------------------------------------------------------------------
303 
304 static void PortDefault(int pnum=1094);
305 
306 //------------------------------------------------------------------------------
318 //------------------------------------------------------------------------------
319 
321 
322 //------------------------------------------------------------------------------
326 //------------------------------------------------------------------------------
327 
328 static void Routing(netType nettype);
329 
330 //------------------------------------------------------------------------------
347 //------------------------------------------------------------------------------
348 
349  bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0,
350  netType nettype=netDefault, const char *xName=0);
351 
352 //------------------------------------------------------------------------------
360 //------------------------------------------------------------------------------
361 
362 static bool SetIFNames(char *ifnames);
363 
364 //------------------------------------------------------------------------------
370 //------------------------------------------------------------------------------
371 
372 static void SetMsgs(XrdSysError *erp);
373 
374 //------------------------------------------------------------------------------
379 //------------------------------------------------------------------------------
380 
381 static void SetRPIPA(bool rval);
382 
383 //------------------------------------------------------------------------------
385 //------------------------------------------------------------------------------
386 
387  XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {}
388 
389  ~XrdNetIF() {if (ifBuff) free(ifBuff);}
390 
391 private:
392 
393 struct ifAddrs
394  {short hALen;
395  short hDLen;
396  bool ipV6;
397  bool prvt;
398  char hAddr[64]; // address
399  char hDest[64]; // address possibly in deprecated format
400  };
401 
402 bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src);
403 bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6);
404 bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0);
405 static
406 bool IsOkName(const char *ifn, short &ifIdx);
407 static
408 char *SetDomain();
409 void SetIFPP();
410 bool SetIF64(bool retVal);
411 static
412 bool V4LinkLocal(struct sockaddr *saP);
413 
414 struct ifData
415 {
416  short iLen;
417  char iVal[6]; // Actually of size iLen
418 
419  ifData() : iLen(0) {*iVal = 0;}
420  ~ifData() {}
421 };
422 
425 bool ifxDNS[ifMax];
426 char *ifBuff;
427 
428 struct pInfo {char len;
429  char val[7]; // Contains ":12345\0"
430  pInfo() : len(0) {*val = 0;}
431  } portSfx;
432 
433 int ifPort;
434 short ifRoute;
435 char ifMask;
436 char ifAvail;
437 
438 static
440 static char *myDomain;
441 static char *ifCfg[2];
442 static
443 const char *ifTName[ifMax];
444 static
445 const char *ifMaskVec;
446 static
448 static int dfPort;
449 static ifData ifNull;
450 static bool rPIPA;
451 };
452 #endif
Definition: XrdNetIF.hh:72
bool prvt
Definition: XrdNetIF.hh:397
char Mask()
Definition: XrdNetIF.hh:242
static int GetIF(XrdOucTList **ifList, const char **eText=0)
int GetName(const char *&name, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:102
ifData()
Definition: XrdNetIF.hh:419
static const int havePub4
ifList == 0 && public ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:158
~XrdNetIF()
Definition: XrdNetIF.hh:389
static XrdSysError * eDest
Definition: XrdNetIF.hh:439
netType
Definition: XrdNetIF.hh:320
static netType netRoutes
Definition: XrdNetIF.hh:447
char ifAvail
Definition: XrdNetIF.hh:436
Definition: XrdNetIF.hh:320
static void SetRPIPA(bool rval)
short hDLen
Definition: XrdNetIF.hh:395
static const int haveNoGI
ifList == 0 && getifaddrs() is not supported
Definition: XrdNetIF.hh:148
int ifPort
Definition: XrdNetIF.hh:433
static char Mask(ifType ifT)
Definition: XrdNetIF.hh:252
bool SetIF64(bool retVal)
Definition: XrdNetIF.hh:66
Definition: XrdNetIF.hh:74
bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, netType nettype=netDefault, const char *xName=0)
Definition: XrdNetIF.hh:428
bool ifxDNS[ifMax]
Definition: XrdNetIF.hh:425
Definition: XrdNetIF.hh:48
static bool InDomain(XrdNetAddrInfo *epaddr)
pInfo()
Definition: XrdNetIF.hh:430
Definition: XrdNetIF.hh:73
char hAddr[64]
Definition: XrdNetIF.hh:398
static char * SetDomain()
bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src)
XrdNetIF()
Constructor and Destructor.
Definition: XrdNetIF.hh:387
Definition: XrdSysError.hh:89
char iVal[6]
Definition: XrdNetIF.hh:417
static bool V4LinkLocal(struct sockaddr *saP)
int Port()
Definition: XrdNetIF.hh:276
static ifData ifNull
Definition: XrdNetIF.hh:449
static const int havePrv6
ifList == 0 && private ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:156
static const int havePrv4
ifList == 0 && private ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:154
static const int havePub6
ifList == 0 && public ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:160
ifData * ifName[ifMax]
Definition: XrdNetIF.hh:423
ifType
The enum that is used to index into ifData to get appropriate interface.
Definition: XrdNetIF.hh:64
bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0)
static bool IsOkName(const char *ifn, short &ifIdx)
short ifRoute
Definition: XrdNetIF.hh:434
Definition: XrdOucTList.hh:41
char ifMask
Definition: XrdNetIF.hh:435
Definition: XrdNetIF.hh:70
char len
Definition: XrdNetIF.hh:428
Definition: XrdNetIF.hh:64
char * ifBuff
Definition: XrdNetIF.hh:426
Definition: XrdNetAddrInfo.hh:53
~ifData()
Definition: XrdNetIF.hh:420
int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
Definition: XrdNetIF.hh:120
static void SetMsgs(XrdSysError *erp)
static const int haveIPv4
ifList == 0 && non-local ipv4 i/f found (or&#39;d)
Definition: XrdNetIF.hh:150
short hALen
Definition: XrdNetIF.hh:394
bool HasDest(ifType ifT=PublicV6)
Definition: XrdNetIF.hh:221
static void PortDefault(int pnum=1094)
Definition: XrdNetIF.hh:320
char val[7]
Definition: XrdNetIF.hh:429
static int dfPort
Definition: XrdNetIF.hh:448
Definition: XrdNetIF.hh:75
Definition: XrdNetIF.hh:393
Definition: XrdNetIF.hh:68
Definition: XrdNetIF.hh:71
ifData * ifDest[ifMax]
Definition: XrdNetIF.hh:424
struct XrdNetIF::pInfo portSfx
static bool rPIPA
Definition: XrdNetIF.hh:450
static char * ifCfg[2]
Definition: XrdNetIF.hh:441
static char * myDomain
Definition: XrdNetIF.hh:440
static const char * ifTName[ifMax]
Definition: XrdNetIF.hh:443
Definition: XrdNetIF.hh:320
static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
Definition: XrdNetIF.hh:204
void Display(const char *pfx="=====> ")
Definition: XrdNetIF.hh:320
Definition: XrdNetIF.hh:69
void SetIFPP()
short iLen
Definition: XrdNetIF.hh:416
static void Routing(netType nettype)
bool ipV6
Definition: XrdNetIF.hh:396
static const int haveIPv6
ifList == 0 && non-local ipv6 i/f found (or&#39;d)
Definition: XrdNetIF.hh:152
static bool SetIFNames(char *ifnames)
Definition: XrdNetIF.hh:67
int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false)
static const char * ifMaskVec
Definition: XrdNetIF.hh:445
char hDest[64]
Definition: XrdNetIF.hh:399
Definition: XrdNetIF.hh:65
static void Privatize(ifType &x)
Definition: XrdNetIF.hh:284
static const char * Name(ifType ifT)
Definition: XrdNetIF.hh:266
Definition: XrdNetIF.hh:414