00001 #ifndef __XRDNETIF_HH__ 00002 #define __XRDNETIF_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d N e t I F . h h */ 00006 /* */ 00007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 #include <stdlib.h> 00034 #include <string.h> 00035 00036 //------------------------------------------------------------------------------ 00040 //------------------------------------------------------------------------------ 00041 00042 class XrdNetAddrInfo; 00043 class XrdOucTList; 00044 class XrdSysError; 00045 00046 struct sockaddr; 00047 00048 class XrdNetIF 00049 { 00050 public: 00051 00052 //------------------------------------------------------------------------------ 00056 //------------------------------------------------------------------------------ 00057 00058 void Display(const char *pfx="=====> "); 00059 00060 //------------------------------------------------------------------------------ 00062 //------------------------------------------------------------------------------ 00063 00064 enum ifType {PublicV4 = 0, //<! Public IPv4 network 00065 PrivateV4 = 1, //<! Private IPv4 network 00066 PublicV6 = 2, //<! Public IPv6 network 00067 PrivateV6 = 3, //<! Private IPv6 network 00068 PrivateIF = 1, //<! Bit to change PublicVx -> PrivateVx 00069 ifNum = 4, //<! Count of actual interface types 00070 Public46 = 4, //<! Public v4|6 network (dual stack) 00071 Private46 = 5, //<! Private v4|6 network (dual stack) 00072 Public64 = 6, //<! Public v6|4 network (dual stack) 00073 Private64 = 7, //<! Private v6|4 network (dual stack) 00074 ifMax = 8, //<! Total elements in if vector 00075 ifAny = 8}; //<! Used to select any avilable i/f 00076 00077 //------------------------------------------------------------------------------ 00088 //------------------------------------------------------------------------------ 00089 00090 int GetDest(char *dest, int dlen, ifType ifT=PublicV6, bool prefn=false); 00091 00092 //------------------------------------------------------------------------------ 00100 //------------------------------------------------------------------------------ 00101 00102 inline int GetName(const char *&name, ifType ifT=PublicV6) 00103 {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail); 00104 name = ifName[ifT]->iVal; 00105 return ifName[ifT]->iLen; 00106 } 00107 00108 //------------------------------------------------------------------------------ 00118 //------------------------------------------------------------------------------ 00119 00120 inline int GetName(char *nbuff, int &nport, ifType ifT=PublicV6) 00121 {if (ifT >= ifAny) ifT = static_cast<ifType>(ifAvail); 00122 strcpy(nbuff, ifName[ifT]->iVal); nport = ifPort; 00123 return ifName[ifT]->iLen; 00124 } 00125 00126 //------------------------------------------------------------------------------ 00145 //------------------------------------------------------------------------------ 00146 00147 static 00148 const int haveNoGI = 0; 00149 static 00150 const int haveIPv4 = 1; 00151 static 00152 const int haveIPv6 = 2; 00153 static 00154 const int havePrv4 = 4; 00155 static 00156 const int havePrv6 = 8; 00157 static 00158 const int havePub4 =16; 00159 static 00160 const int havePub6 =32; 00161 00162 static int GetIF(XrdOucTList **ifList, const char **eText=0); 00163 00164 //------------------------------------------------------------------------------ 00175 //------------------------------------------------------------------------------ 00176 00177 static int GetIF(char *buff, int blen, const char **eText=0, bool show=false); 00178 00179 //------------------------------------------------------------------------------ 00190 //------------------------------------------------------------------------------ 00191 00192 static int GetIF(char *&ifline, const char **eText=0, bool show=false); 00193 00194 //------------------------------------------------------------------------------ 00202 //------------------------------------------------------------------------------ 00203 00204 static ifType GetIFType(bool conIPv4, bool hasIP64, bool pvtIP) 00205 {ifType ifT; 00206 if (conIPv4) ifT = (hasIP64 ? Public46 : PublicV4); 00207 else ifT = (hasIP64 ? Public64 : PublicV6); 00208 if (pvtIP) Privatize(ifT); 00209 return ifT; 00210 } 00211 00212 //------------------------------------------------------------------------------ 00219 //------------------------------------------------------------------------------ 00220 00221 inline bool HasDest(ifType ifT=PublicV6) 00222 {return ifT >= ifAny || ifDest[ifT]->iLen != 0;} 00223 00224 //------------------------------------------------------------------------------ 00232 //------------------------------------------------------------------------------ 00233 00234 static bool InDomain(XrdNetAddrInfo *epaddr); 00235 00236 //------------------------------------------------------------------------------ 00240 //------------------------------------------------------------------------------ 00241 00242 char Mask() {return ifMask;} 00243 00244 //------------------------------------------------------------------------------ 00250 //------------------------------------------------------------------------------ 00251 00252 static char Mask(ifType ifT) 00253 {if (ifT >= ifAny) return 0x0f; 00254 return ifMaskVec[ifT]; 00255 } 00256 00257 //------------------------------------------------------------------------------ 00264 //------------------------------------------------------------------------------ 00265 static 00266 const char *Name(ifType ifT) {if (ifT >= ifAny) return "any"; 00267 return ifTName[ifT]; 00268 } 00269 00270 //------------------------------------------------------------------------------ 00274 //------------------------------------------------------------------------------ 00275 00276 inline int Port() {return ifPort;} 00277 00278 //------------------------------------------------------------------------------ 00282 //------------------------------------------------------------------------------ 00283 00284 static void Privatize(ifType &x) {x = ifType(x | PrivateIF);} 00285 00286 //------------------------------------------------------------------------------ 00292 //------------------------------------------------------------------------------ 00293 00294 int Port(int pnum); 00295 00296 //------------------------------------------------------------------------------ 00302 //------------------------------------------------------------------------------ 00303 00304 static void PortDefault(int pnum=1094); 00305 00306 //------------------------------------------------------------------------------ 00318 //------------------------------------------------------------------------------ 00319 00320 enum netType {netDefault = 0, netSplit, netCommon, netLocal}; 00321 00322 //------------------------------------------------------------------------------ 00326 //------------------------------------------------------------------------------ 00327 00328 static void Routing(netType nettype); 00329 00330 //------------------------------------------------------------------------------ 00347 //------------------------------------------------------------------------------ 00348 00349 bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, 00350 netType nettype=netDefault, const char *xName=0); 00351 00352 //------------------------------------------------------------------------------ 00360 //------------------------------------------------------------------------------ 00361 00362 static bool SetIFNames(char *ifnames); 00363 00364 //------------------------------------------------------------------------------ 00370 //------------------------------------------------------------------------------ 00371 00372 static void SetMsgs(XrdSysError *erp); 00373 00374 //------------------------------------------------------------------------------ 00379 //------------------------------------------------------------------------------ 00380 00381 static void SetRPIPA(bool rval); 00382 00383 //------------------------------------------------------------------------------ 00385 //------------------------------------------------------------------------------ 00386 00387 XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {} 00388 00389 ~XrdNetIF() {if (ifBuff) free(ifBuff);} 00390 00391 private: 00392 00393 struct ifAddrs 00394 {short hALen; 00395 short hDLen; 00396 bool ipV6; 00397 bool prvt; 00398 char hAddr[64]; // address 00399 char hDest[64]; // address possibly in deprecated format 00400 }; 00401 00402 bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src); 00403 bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6); 00404 bool GenIF(XrdNetAddrInfo **src, int srcnum, const char *xName=0); 00405 static 00406 bool IsOkName(const char *ifn, short &ifIdx); 00407 static 00408 char *SetDomain(); 00409 void SetIFPP(); 00410 bool SetIF64(bool retVal); 00411 static 00412 bool V4LinkLocal(struct sockaddr *saP); 00413 00414 struct ifData 00415 { 00416 short iLen; 00417 char iVal[6]; // Actually of size iLen 00418 00419 ifData() : iLen(0) {*iVal = 0;} 00420 ~ifData() {} 00421 }; 00422 00423 ifData *ifName[ifMax]; 00424 ifData *ifDest[ifMax]; 00425 bool ifxDNS[ifMax]; 00426 char *ifBuff; 00427 00428 struct pInfo {char len; 00429 char val[7]; // Contains ":12345\0" 00430 pInfo() : len(0) {*val = 0;} 00431 } portSfx; 00432 00433 int ifPort; 00434 short ifRoute; 00435 char ifMask; 00436 char ifAvail; 00437 00438 static 00439 XrdSysError *eDest; 00440 static char *myDomain; 00441 static char *ifCfg[2]; 00442 static 00443 const char *ifTName[ifMax]; 00444 static 00445 const char *ifMaskVec; 00446 static 00447 netType netRoutes; 00448 static int dfPort; 00449 static ifData ifNull; 00450 static bool rPIPA; 00451 }; 00452 #endif