xrootd
Loading...
Searching...
No Matches
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
36//------------------------------------------------------------------------------
40//------------------------------------------------------------------------------
41
42class XrdNetAddrInfo;
43class XrdOucTList;
44class XrdSysError;
45
46struct sockaddr;
47
49{
50public:
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
102inline 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
120inline 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
147static
148const int haveNoGI = 0;
149static
150const int haveIPv4 = 1;
151static
152const int haveIPv6 = 2;
153static
154const int havePrv4 = 4;
155static
156const int havePrv6 = 8;
157static
158const int havePub4 =16;
159static
160const int havePub6 =32;
161
162static int GetIF(XrdOucTList **ifList, const char **eText=0);
163
164//------------------------------------------------------------------------------
175//------------------------------------------------------------------------------
176
177static int GetIF(char *buff, int blen, const char **eText=0, bool show=false);
178
179//------------------------------------------------------------------------------
190//------------------------------------------------------------------------------
191
192static int GetIF(char *&ifline, const char **eText=0, bool show=false);
193
194//------------------------------------------------------------------------------
202//------------------------------------------------------------------------------
203
204static 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
221inline bool HasDest(ifType ifT=PublicV6)
222 {return ifT >= ifAny || ifDest[ifT]->iLen != 0;}
223
224//------------------------------------------------------------------------------
232//------------------------------------------------------------------------------
233
234static bool InDomain(XrdNetAddrInfo *epaddr);
235
236//------------------------------------------------------------------------------
240//------------------------------------------------------------------------------
241
242 char Mask() {return ifMask;}
243
244//------------------------------------------------------------------------------
250//------------------------------------------------------------------------------
251
252static char Mask(ifType ifT)
253 {if (ifT >= ifAny) return 0x0f;
254 return ifMaskVec[ifT];
255 }
256
257//------------------------------------------------------------------------------
264//------------------------------------------------------------------------------
265static
266const char *Name(ifType ifT) {if (ifT >= ifAny) return "any";
267 return ifTName[ifT];
268 }
269
270//------------------------------------------------------------------------------
274//------------------------------------------------------------------------------
275
276inline int Port() {return ifPort;}
277
278//------------------------------------------------------------------------------
282//------------------------------------------------------------------------------
283
284static void Privatize(ifType &x) {x = ifType(x | PrivateIF);}
285
286//------------------------------------------------------------------------------
292//------------------------------------------------------------------------------
293
294 int Port(int pnum);
295
296//------------------------------------------------------------------------------
300//------------------------------------------------------------------------------
301
302static void PortDefault(int pnum=1094);
303
304//------------------------------------------------------------------------------
316//------------------------------------------------------------------------------
317
319
320//------------------------------------------------------------------------------
324//------------------------------------------------------------------------------
325
326static void Routing(netType nettype);
327
328//------------------------------------------------------------------------------
345//------------------------------------------------------------------------------
346
347 bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0,
348 netType nettype=netDefault, const char *xName=0);
349
350//------------------------------------------------------------------------------
358//------------------------------------------------------------------------------
359
360static bool SetIFNames(char *ifnames);
361
362//------------------------------------------------------------------------------
368//------------------------------------------------------------------------------
369
370static void SetMsgs(XrdSysError *erp);
371
372//------------------------------------------------------------------------------
377//------------------------------------------------------------------------------
378
379static void SetRPIPA(bool rval);
380
381//------------------------------------------------------------------------------
383//------------------------------------------------------------------------------
384
385 XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {}
386
387 ~XrdNetIF() {if (ifBuff) free(ifBuff);}
388
389private:
390
392 {short hALen;
393 short hDLen;
394 bool ipV6;
395 bool prvt;
396 char hAddr[64]; // address
397 char hDest[64]; // address possibly in deprecated format
398 };
399
400bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src);
401bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6);
402bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0);
403static const
404char *GetDomain();
405static
406bool IsOkName(const char *ifn, short &ifIdx);
407void SetIFPP();
408bool SetIF64(bool retVal);
409static
410bool V4LinkLocal(struct sockaddr *saP);
411
412struct ifData
413{
414 short iLen;
415 char iVal[6]; // Actually of size iLen
416
417 ifData() : iLen(0) {*iVal = 0;}
419};
420
424char *ifBuff;
425
426struct pInfo {char len;
427 char val[7]; // Contains ":12345\0"
428 pInfo() : len(0) {*val = 0;}
430
435
436static
438static
439const char *myDomain;
440static char *ifCfg[2];
441static
442const char *ifTName[ifMax];
443static
444const char *ifMaskVec;
445static
447static int dfPort;
449static bool rPIPA;
450};
451#endif
Definition XrdNetAddrInfo.hh:54
Definition XrdNetIF.hh:49
char * ifBuff
Definition XrdNetIF.hh:424
static char * ifCfg[2]
Definition XrdNetIF.hh:440
static const int haveNoGI
ifList == 0 && getifaddrs() is not supported
Definition XrdNetIF.hh:148
bool ifxDNS[ifMax]
Definition XrdNetIF.hh:423
static void SetRPIPA(bool rval)
char Mask()
Definition XrdNetIF.hh:242
bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, netType nettype=netDefault, const char *xName=0)
static bool V4LinkLocal(struct sockaddr *saP)
static const int haveIPv4
ifList == 0 && non-local ipv4 i/f found (or'd)
Definition XrdNetIF.hh:150
int GetName(char *nbuff, int &nport, ifType ifT=PublicV6)
Definition XrdNetIF.hh:120
static int dfPort
Definition XrdNetIF.hh:447
ifData * ifDest[ifMax]
Definition XrdNetIF.hh:422
struct XrdNetIF::pInfo portSfx
XrdNetIF()
Constructor and Destructor.
Definition XrdNetIF.hh:385
static bool IsOkName(const char *ifn, short &ifIdx)
static XrdSysError * eDest
Definition XrdNetIF.hh:437
void Display(const char *pfx="=====> ")
static const char * ifTName[ifMax]
Definition XrdNetIF.hh:442
static const char * ifMaskVec
Definition XrdNetIF.hh:444
static void Routing(netType nettype)
int Port(int pnum)
static void PortDefault(int pnum=1094)
char ifAvail
Definition XrdNetIF.hh:434
static char Mask(ifType ifT)
Definition XrdNetIF.hh:252
int ifPort
Definition XrdNetIF.hh:431
static const char * Name(ifType ifT)
Definition XrdNetIF.hh:266
int Port()
Definition XrdNetIF.hh:276
~XrdNetIF()
Definition XrdNetIF.hh:387
static bool InDomain(XrdNetAddrInfo *epaddr)
bool HasDest(ifType ifT=PublicV6)
Definition XrdNetIF.hh:221
static const char * myDomain
Definition XrdNetIF.hh:439
ifData * ifName[ifMax]
Definition XrdNetIF.hh:421
static const char * GetDomain()
static const int havePub6
ifList == 0 && public ipv6 i/f found (or'd)
Definition XrdNetIF.hh:160
static int GetIF(char *&ifline, const char **eText=0, bool show=false)
static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP)
Definition XrdNetIF.hh:204
static int GetIF(XrdOucTList **ifList, const char **eText=0)
static netType netRoutes
Definition XrdNetIF.hh:446
bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src)
int GetName(const char *&name, ifType ifT=PublicV6)
Definition XrdNetIF.hh:102
static const int havePub4
ifList == 0 && public ipv4 i/f found (or'd)
Definition XrdNetIF.hh:158
bool SetIF64(bool retVal)
short ifRoute
Definition XrdNetIF.hh:432
bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6)
int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false)
static const int haveIPv6
ifList == 0 && non-local ipv6 i/f found (or'd)
Definition XrdNetIF.hh:152
static void SetMsgs(XrdSysError *erp)
ifType
The enum that is used to index into ifData to get appropriate interface.
Definition XrdNetIF.hh:64
@ PrivateV4
Definition XrdNetIF.hh:65
@ ifMax
Definition XrdNetIF.hh:74
@ Public46
Definition XrdNetIF.hh:70
@ PublicV4
Definition XrdNetIF.hh:64
@ Private64
Definition XrdNetIF.hh:73
@ ifAny
Definition XrdNetIF.hh:75
@ Public64
Definition XrdNetIF.hh:72
@ PrivateIF
Definition XrdNetIF.hh:68
@ ifNum
Definition XrdNetIF.hh:69
@ Private46
Definition XrdNetIF.hh:71
@ PrivateV6
Definition XrdNetIF.hh:67
@ PublicV6
Definition XrdNetIF.hh:66
char ifMask
Definition XrdNetIF.hh:433
static const int havePrv6
ifList == 0 && private ipv6 i/f found (or'd)
Definition XrdNetIF.hh:156
static bool SetIFNames(char *ifnames)
static void Privatize(ifType &x)
Definition XrdNetIF.hh:284
bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0)
static ifData ifNull
Definition XrdNetIF.hh:448
netType
Definition XrdNetIF.hh:318
@ netSplit
Definition XrdNetIF.hh:318
@ netDefault
Definition XrdNetIF.hh:318
@ netCommon
Definition XrdNetIF.hh:318
@ netLocal
Definition XrdNetIF.hh:318
void SetIFPP()
static const int havePrv4
ifList == 0 && private ipv4 i/f found (or'd)
Definition XrdNetIF.hh:154
static bool rPIPA
Definition XrdNetIF.hh:449
static int GetIF(char *buff, int blen, const char **eText=0, bool show=false)
Definition XrdOucTList.hh:42
Definition XrdSysError.hh:90
Definition XrdNetIF.hh:392
bool prvt
Definition XrdNetIF.hh:395
char hDest[64]
Definition XrdNetIF.hh:397
char hAddr[64]
Definition XrdNetIF.hh:396
bool ipV6
Definition XrdNetIF.hh:394
short hDLen
Definition XrdNetIF.hh:393
short hALen
Definition XrdNetIF.hh:392
Definition XrdNetIF.hh:413
~ifData()
Definition XrdNetIF.hh:418
char iVal[6]
Definition XrdNetIF.hh:415
ifData()
Definition XrdNetIF.hh:417
short iLen
Definition XrdNetIF.hh:414
Definition XrdNetIF.hh:426
char val[7]
Definition XrdNetIF.hh:427
pInfo()
Definition XrdNetIF.hh:428
char len
Definition XrdNetIF.hh:426