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 //------------------------------------------------------------------------------ 00062 //------------------------------------------------------------------------------ 00063 00064 inline int Family() const {return static_cast<int>(IP.Addr.sa_family);} 00065 00066 //------------------------------------------------------------------------------ 00087 //------------------------------------------------------------------------------ 00088 00089 enum fmtUse {fmtAuto=0, 00090 fmtName, 00091 fmtAddr, 00092 fmtAdv6}; 00093 00094 static const int noPort = 0x0000001; 00095 static const int noPortRaw = 0x0000002; 00096 static const int old6Map4 = 0x0000004; 00097 static const int prefipv4 = 0x0000008; 00098 00099 int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0); 00100 00101 //------------------------------------------------------------------------------ 00107 //------------------------------------------------------------------------------ 00108 00109 bool isLoopback(); 00110 00111 //------------------------------------------------------------------------------ 00121 //------------------------------------------------------------------------------ 00122 00123 static bool isHostName(const char *name); 00124 00125 //------------------------------------------------------------------------------ 00132 //------------------------------------------------------------------------------ 00133 00134 enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX}; 00135 00136 inline bool isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;} 00137 00138 //------------------------------------------------------------------------------ 00143 //------------------------------------------------------------------------------ 00144 00145 inline bool isMapped() const {return IP.Addr.sa_family == AF_INET6 00146 && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr); 00147 } 00148 00149 //------------------------------------------------------------------------------ 00154 //------------------------------------------------------------------------------ 00155 00156 bool isPrivate(); 00157 00158 //------------------------------------------------------------------------------ 00163 //------------------------------------------------------------------------------ 00164 00165 bool isRegistered(); 00166 00167 //------------------------------------------------------------------------------ 00173 //------------------------------------------------------------------------------ 00174 00175 struct LocInfo 00176 {unsigned char Country[2]; 00177 unsigned char Region; 00178 unsigned char Locale; 00179 char TimeZone; 00180 char Reserved; 00181 short Speed; 00182 int Latitude; 00183 int Longtitude; 00184 00185 LocInfo() : Region(0), Locale(0), TimeZone(-128), Reserved(0), 00186 Speed(0), Latitude(0), Longtitude(0) {*Country = 0;} 00187 }; 00188 00189 const struct 00190 LocInfo *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);} 00191 00192 //------------------------------------------------------------------------------ 00204 //------------------------------------------------------------------------------ 00205 00206 const char *Name(const char *eName=0, const char **eText=0); 00207 00208 //------------------------------------------------------------------------------ 00214 //------------------------------------------------------------------------------ 00215 00216 inline const 00217 XrdNetSockAddr *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);} 00218 00219 //------------------------------------------------------------------------------ 00224 //------------------------------------------------------------------------------ 00225 00226 int Port(); 00227 00228 //------------------------------------------------------------------------------ 00233 //------------------------------------------------------------------------------ 00234 00235 inline int Protocol() {return static_cast<int>(protType);} 00236 00237 //------------------------------------------------------------------------------ 00246 //-----------------------------------------------------------------------------sav- 00247 00248 int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false); 00249 00250 //------------------------------------------------------------------------------ 00255 //------------------------------------------------------------------------------ 00256 00257 inline const 00258 sockaddr *SockAddr() {return sockAddr;} 00259 00260 //------------------------------------------------------------------------------ 00265 //------------------------------------------------------------------------------ 00266 00267 inline 00268 SOCKLEN_t SockSize() {return addrSize;} 00269 00270 //------------------------------------------------------------------------------ 00274 //------------------------------------------------------------------------------ 00275 00276 inline int SockFD() {return (sockNum ? static_cast<int>(sockNum) : -1);} 00277 00278 //------------------------------------------------------------------------------ 00280 //------------------------------------------------------------------------------ 00281 00282 XrdNetAddrInfo &operator=(XrdNetAddrInfo const &rhs) 00283 {if (&rhs != this) 00284 {memmove(&IP, &rhs.IP, sizeof(IP)); 00285 addrSize = rhs.addrSize; sockNum = rhs.sockNum; 00286 protType = rhs.protType; 00287 if (hostName) free(hostName); 00288 hostName = (rhs.hostName ? strdup(rhs.hostName):0); 00289 addrLoc = rhs.addrLoc; 00290 if (rhs.sockAddr != &rhs.IP.Addr) 00291 {if (!unixPipe || sockAddr == &IP.Addr) 00292 unixPipe = new sockaddr_un; 00293 memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un)); 00294 } else sockAddr = &IP.Addr; 00295 } 00296 return *this; 00297 } 00298 00299 //------------------------------------------------------------------------------ 00301 //------------------------------------------------------------------------------ 00302 00303 XrdNetAddrInfo(XrdNetAddrInfo const &oP) 00304 {hostName = 0; 00305 unixPipe = 0; 00306 *this = oP; 00307 } 00308 00309 //------------------------------------------------------------------------------ 00311 //------------------------------------------------------------------------------ 00312 00313 XrdNetAddrInfo() : hostName(0), addrSize(0), protType(0), sockNum(0) 00314 {IP.Addr.sa_family = 0; 00315 sockAddr = &IP.Addr; 00316 } 00317 00318 XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;} 00319 00320 //------------------------------------------------------------------------------ 00322 //------------------------------------------------------------------------------ 00323 00324 ~XrdNetAddrInfo() {if (hostName) free(hostName); 00325 if (sockAddr != &IP.Addr) delete unixPipe; 00326 } 00327 00328 protected: 00329 char *LowCase(char *str); 00330 int QFill(char *bAddr, int bLen); 00331 int Resolve(); 00332 00333 static XrdNetCache *dnsCache; 00334 00335 XrdNetSockAddr IP; 00336 union {struct sockaddr *sockAddr; 00337 struct sockaddr_un *unixPipe; 00338 }; 00339 char *hostName; 00340 LocInfo addrLoc; 00341 SOCKLEN_t addrSize; 00342 short protType; 00343 unsigned short sockNum; 00344 }; 00345 #endif