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 //------------------------------------------------------------------------------ 00345 //------------------------------------------------------------------------------ 00346 00347 bool SetIF(XrdNetAddrInfo *src, const char *ifList, int port=0, 00348 netType nettype=netDefault); 00349 00350 //------------------------------------------------------------------------------ 00358 //------------------------------------------------------------------------------ 00359 00360 static bool SetIFNames(char *ifnames); 00361 00362 //------------------------------------------------------------------------------ 00368 //------------------------------------------------------------------------------ 00369 00370 static void SetMsgs(XrdSysError *erp); 00371 00372 //------------------------------------------------------------------------------ 00377 //------------------------------------------------------------------------------ 00378 00379 static void SetRPIPA(bool rval); 00380 00381 //------------------------------------------------------------------------------ 00383 //------------------------------------------------------------------------------ 00384 00385 XrdNetIF() : ifBuff(0), ifMask(0), ifAvail(0) {} 00386 00387 ~XrdNetIF() {if (ifBuff) free(ifBuff);} 00388 00389 private: 00390 00391 struct ifAddrs 00392 {short hALen; 00393 short hDLen; 00394 bool ipV6; 00395 bool prvt; 00396 char hAddr[64]; // address 00397 char hDest[64]; // address possibly in deprecated format 00398 }; 00399 00400 bool GenAddrs(ifAddrs &ifTab, XrdNetAddrInfo *src); 00401 bool GenAddrs(ifAddrs &ifTab, const char *hName, bool wantV6); 00402 bool GenIF(XrdNetAddrInfo **src, int srcnum); 00403 static 00404 bool IsOkName(const char *ifn, short &ifIdx); 00405 static 00406 char *SetDomain(); 00407 void SetIFPP(); 00408 bool SetIF64(bool retVal); 00409 static 00410 bool V4LinkLocal(struct sockaddr *saP); 00411 00412 struct ifData 00413 { 00414 short iLen; 00415 char iVal[6]; // Actually of size iLen 00416 00417 ifData() : iLen(0) {*iVal = 0;} 00418 ~ifData() {} 00419 }; 00420 00421 ifData *ifName[ifMax]; 00422 ifData *ifDest[ifMax]; 00423 bool ifxDNS[ifMax]; 00424 char *ifBuff; 00425 00426 struct pInfo {char len; 00427 char val[7]; // Contains ":12345\0" 00428 pInfo() : len(0) {*val = 0;} 00429 } portSfx; 00430 00431 int ifPort; 00432 short ifRoute; 00433 char ifMask; 00434 char ifAvail; 00435 00436 static 00437 XrdSysError *eDest; 00438 static char *myDomain; 00439 static char *ifCfg[2]; 00440 static 00441 const char *ifTName[ifMax]; 00442 static 00443 const char *ifMaskVec; 00444 static 00445 netType netRoutes; 00446 static int dfPort; 00447 static ifData ifNull; 00448 static bool rPIPA; 00449 }; 00450 #endif