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#include <string>
36
37//------------------------------------------------------------------------------
41//------------------------------------------------------------------------------
42
43class XrdNetAddrInfo;
44class XrdOucTList;
45class XrdSysError;
46
47struct sockaddr;
48
50{
51public:
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
115inline 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
133inline 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//------------------------------------------------------------------------------
150int 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//------------------------------------------------------------------------------
164bool 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
193static
194const int haveNoGI = 0;
195static
196const int haveIPv4 = 1;
197static
198const int haveIPv6 = 2;
199static
200const int havePrv4 = 4;
201static
202const int havePrv6 = 8;
203static
204const int havePub4 =16;
205static
206const int havePub6 =32;
207
208static int GetIF(XrdOucTList **ifList, const char **eText=0);
209
210//------------------------------------------------------------------------------
221//------------------------------------------------------------------------------
222
223static int GetIF(char *buff, int blen, const char **eText=0, bool show=false);
224
225//------------------------------------------------------------------------------
236//------------------------------------------------------------------------------
237
238static int GetIF(char *&ifline, const char **eText=0, bool show=false);
239
240//------------------------------------------------------------------------------
248//------------------------------------------------------------------------------
249
250static 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
267inline bool HasDest(ifType ifT=PublicV6)
268 {return ifT >= ifAny || ifDest[ifT]->iLen != 0;}
269
270//------------------------------------------------------------------------------
278//------------------------------------------------------------------------------
279
280static bool InDomain(XrdNetAddrInfo *epaddr);
281
282//------------------------------------------------------------------------------
286//------------------------------------------------------------------------------
287
288 char Mask() {return ifMask;}
289
290//------------------------------------------------------------------------------
296//------------------------------------------------------------------------------
297
298static char Mask(ifType ifT)
299 {if (ifT >= ifAny) return 0x0f;
300 return ifMaskVec[ifT];
301 }
302
303//------------------------------------------------------------------------------
310//------------------------------------------------------------------------------
311static
312const char *Name(ifType ifT) {if (ifT >= ifAny) return "any";
313 return ifTName[ifT];
314 }
315
316//------------------------------------------------------------------------------
320//------------------------------------------------------------------------------
321
322inline int Port() {return ifPort;}
323
324//------------------------------------------------------------------------------
328//------------------------------------------------------------------------------
329
330static void Privatize(ifType &x) {x = ifType(x | PrivateIF);}
331
332//------------------------------------------------------------------------------
338//------------------------------------------------------------------------------
339
340 int Port(int pnum);
341
342//------------------------------------------------------------------------------
346//------------------------------------------------------------------------------
347
348static void PortDefault(int pnum=1094);
349
350//------------------------------------------------------------------------------
362//------------------------------------------------------------------------------
363
365
366//------------------------------------------------------------------------------
370//------------------------------------------------------------------------------
371
372static 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
406static bool SetIFNames(char *ifnames);
407
408//------------------------------------------------------------------------------
414//------------------------------------------------------------------------------
415
416static void SetMsgs(XrdSysError *erp);
417
418//------------------------------------------------------------------------------
423//------------------------------------------------------------------------------
424
425static void SetRPIPA(bool rval);
426
427//------------------------------------------------------------------------------
429//------------------------------------------------------------------------------
430
431 XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {}
432
433 ~XrdNetIF() {if (ifBuff) free(ifBuff);}
434
435private:
436
437struct 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
446bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src);
447bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6);
448bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0);
449static const
450char *GetDomain();
451static
452bool IsOkName(const char *ifn, short &ifIdx);
453void SetIFPP();
454bool SetIF64(bool retVal);
455static
456bool V4LinkLocal(struct sockaddr *saP);
457
458struct ifData
459{
460 short iLen;
461 char iVal[6]; // Actually of size iLen
462
463 ifData() : iLen(0) {*iVal = 0;}
464 ~ifData() {}
465};
466
467ifData *ifName[ifMax];
468ifData *ifDest[ifMax];
469bool ifxDNS[ifMax];
470char *ifBuff;
471
472struct pInfo {char len;
473 char val[7]; // Contains ":12345\0"
474 pInfo() : len(0) {*val = 0;}
475 } portSfx;
476
477int ifPort;
478short ifRoute;
479char ifMask;
480char ifAvail;
481
482// The public hostname of the interface.
483std::string m_PublicName;
484
485static
486XrdSysError *eDest;
487static
488const char *myDomain;
489static char *ifCfg[2];
490static
491const char *ifTName[ifMax];
492static
493const char *ifMaskVec;
494static
495netType netRoutes;
496static int dfPort;
497static ifData ifNull;
498static bool rPIPA;
499};
500#endif
void Display()
Definition XrdCks.cc:56
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
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
static const char * Name(ifType ifT)
Definition XrdNetIF.hh:312
int Port()
Definition XrdNetIF.hh:322
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
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
@ Private64
Definition XrdNetIF.hh:74
@ PrivateIF
Definition XrdNetIF.hh:69
@ Private46
Definition XrdNetIF.hh:72
@ PrivateV6
Definition XrdNetIF.hh:68
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
static const int havePrv4
ifList == 0 && private ipv4 i/f found (or'd)
Definition XrdNetIF.hh:200