00001 #ifndef __XRDNETADDRINFO_HH__ 00002 #define __XRDNETADDRINFO_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d N e t A d d r I n f o . 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 <inttypes.h> 00034 #include <stdlib.h> 00035 #include <string.h> 00036 #include <netinet/in.h> 00037 #include <sys/socket.h> 00038 #include <sys/un.h> 00039 00040 #include "XrdNet/XrdNetSockAddr.hh" 00041 #include "XrdSys/XrdSysPlatform.hh" 00042 00043 //------------------------------------------------------------------------------ 00048 //------------------------------------------------------------------------------ 00049 00050 struct addrinfo; 00051 class XrdNetCache; 00052 00053 class XrdNetAddrInfo 00054 { 00055 public: 00056 00057 //------------------------------------------------------------------------------ 00063 //------------------------------------------------------------------------------ 00064 00065 const char *Dialect() {return protName;} 00066 00067 //------------------------------------------------------------------------------ 00072 //------------------------------------------------------------------------------ 00073 00074 int Family() const {return static_cast<int>(IP.Addr.sa_family);} 00075 00076 //------------------------------------------------------------------------------ 00097 //------------------------------------------------------------------------------ 00098 00099 enum fmtUse {fmtAuto=0, 00100 fmtName, 00101 fmtAddr, 00102 fmtAdv6}; 00103 00104 static const int noPort = 0x0000001; 00105 static const int noPortRaw = 0x0000002; 00106 static const int old6Map4 = 0x0000004; 00107 static const int prefipv4 = 0x0000008; 00108 00109 int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0); 00110 00111 //------------------------------------------------------------------------------ 00117 //------------------------------------------------------------------------------ 00118 00119 bool isLoopback(); 00120 00121 //------------------------------------------------------------------------------ 00131 //------------------------------------------------------------------------------ 00132 00133 static bool isHostName(const char *name); 00134 00135 //------------------------------------------------------------------------------ 00142 //------------------------------------------------------------------------------ 00143 00144 enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX}; 00145 00146 bool isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;} 00147 00148 //------------------------------------------------------------------------------ 00153 //------------------------------------------------------------------------------ 00154 00155 bool isMapped() const {return IP.Addr.sa_family == AF_INET6 00156 && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr); 00157 } 00158 00159 //------------------------------------------------------------------------------ 00164 //------------------------------------------------------------------------------ 00165 00166 bool isPrivate(); 00167 00168 //------------------------------------------------------------------------------ 00173 //------------------------------------------------------------------------------ 00174 00175 bool isRegistered(); 00176 00177 //------------------------------------------------------------------------------ 00182 //------------------------------------------------------------------------------ 00183 00184 bool isUsingTLS(); 00185 00186 //------------------------------------------------------------------------------ 00192 //------------------------------------------------------------------------------ 00193 00194 struct LocInfo 00195 {unsigned char Country[2]; 00196 unsigned char Region; 00197 unsigned char Locale; 00198 char TimeZone; 00199 char Flags; 00200 short Speed; 00201 int Latitude; 00202 int Longtitude; 00203 00204 LocInfo() : Region(0), Locale(0), TimeZone(-128), Flags(0), 00205 Speed(0), Latitude(0), Longtitude(0) {*Country = 0;} 00206 00207 }; 00208 00209 const struct 00210 LocInfo *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);} 00211 00212 //------------------------------------------------------------------------------ 00224 //------------------------------------------------------------------------------ 00225 00226 const char *Name(const char *eName=0, const char **eText=0); 00227 00228 //------------------------------------------------------------------------------ 00234 //------------------------------------------------------------------------------ 00235 00236 const 00237 XrdNetSockAddr *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);} 00238 00239 //------------------------------------------------------------------------------ 00244 //------------------------------------------------------------------------------ 00245 00246 int Port(); 00247 00248 //------------------------------------------------------------------------------ 00253 //------------------------------------------------------------------------------ 00254 00255 int Protocol() {return static_cast<int>(protType);} 00256 00257 //------------------------------------------------------------------------------ 00266 //-----------------------------------------------------------------------------sav- 00267 00268 int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false); 00269 00270 //------------------------------------------------------------------------------ 00275 //------------------------------------------------------------------------------ 00276 00277 const 00278 sockaddr *SockAddr() {return sockAddr;} 00279 00280 //------------------------------------------------------------------------------ 00285 //------------------------------------------------------------------------------ 00286 00287 SOCKLEN_t SockSize() {return static_cast<SOCKLEN_t>(addrSize);} 00288 00289 //------------------------------------------------------------------------------ 00293 //------------------------------------------------------------------------------ 00294 00295 int SockFD() {return (sockNum ? sockNum : -1);} 00296 00297 //------------------------------------------------------------------------------ 00299 //------------------------------------------------------------------------------ 00300 00301 XrdNetAddrInfo &operator=(XrdNetAddrInfo const &rhs) 00302 {if (&rhs != this) 00303 {memmove(&IP, &rhs.IP, sizeof(IP)); 00304 addrSize = rhs.addrSize; sockNum = rhs.sockNum; 00305 protType = rhs.protType; 00306 protFlgs = rhs.protFlgs; 00307 protName = rhs.protName; 00308 if (hostName) free(hostName); 00309 hostName = (rhs.hostName ? strdup(rhs.hostName):0); 00310 addrLoc = rhs.addrLoc; 00311 if (rhs.sockAddr != &rhs.IP.Addr) 00312 {if (!unixPipe || sockAddr == &IP.Addr) 00313 unixPipe = new sockaddr_un; 00314 memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un)); 00315 } else sockAddr = &IP.Addr; 00316 } 00317 return *this; 00318 } 00319 00320 //------------------------------------------------------------------------------ 00322 //------------------------------------------------------------------------------ 00323 00324 XrdNetAddrInfo(XrdNetAddrInfo const &oP) 00325 {hostName = 0; 00326 unixPipe = 0; 00327 *this = oP; 00328 } 00329 00330 //------------------------------------------------------------------------------ 00332 //------------------------------------------------------------------------------ 00333 00334 XrdNetAddrInfo() : hostName(0), addrSize(0), protType(0), 00335 protFlgs(0), sockNum(0), protName(0) 00336 {IP.Addr.sa_family = 0; 00337 sockAddr = &IP.Addr; 00338 } 00339 00340 XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;} 00341 00342 //------------------------------------------------------------------------------ 00344 //------------------------------------------------------------------------------ 00345 00346 ~XrdNetAddrInfo() {if (hostName) free(hostName); 00347 if (sockAddr != &IP.Addr) delete unixPipe; 00348 } 00349 00350 protected: 00351 char *LowCase(char *str); 00352 int QFill(char *bAddr, int bLen); 00353 int Resolve(); 00354 00355 static XrdNetCache *dnsCache; 00356 00357 XrdNetSockAddr IP; 00358 union {struct sockaddr *sockAddr; 00359 struct sockaddr_un *unixPipe; 00360 }; 00361 char *hostName; 00362 LocInfo addrLoc; 00363 unsigned short addrSize; 00364 unsigned char protType; 00365 unsigned char protFlgs; 00366 int sockNum; 00367 const char *protName; 00368 00369 // Flag settings in protFlgs 00370 // 00371 static const char isTLS = 0x01; 00372 }; 00373 #endif