xrootd
XrdNetAddrInfo.hh
Go to the documentation of this file.
1 #ifndef __XRDNETADDRINFO_HH__
2 #define __XRDNETADDRINFO_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t A d d r I n f o . 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 <inttypes.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <netinet/in.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 
40 #include "XrdNet/XrdNetSockAddr.hh"
41 #include "XrdSys/XrdSysPlatform.hh"
42 
43 //------------------------------------------------------------------------------
48 //------------------------------------------------------------------------------
49 
50 struct addrinfo;
51 class XrdNetCache;
52 
54 {
55 public:
56 
57 //------------------------------------------------------------------------------
63 //------------------------------------------------------------------------------
64 
65 const char *Dialect() {return protName;}
66 
67 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 
74 int Family() const {return static_cast<int>(IP.Addr.sa_family);}
75 
76 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
98 
99 enum fmtUse {fmtAuto=0,
103 
104 static const int noPort = 0x0000001;
105 static const int noPortRaw = 0x0000002;
106 static const int old6Map4 = 0x0000004;
107 static const int prefipv4 = 0x0000008;
108 
109 int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0);
110 
111 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 
119 bool isLoopback();
120 
121 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 
133 static bool isHostName(const char *name);
134 
135 //------------------------------------------------------------------------------
142 //------------------------------------------------------------------------------
143 
144 enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX};
145 
146 bool isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;}
147 
148 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 
155 bool isMapped() const {return IP.Addr.sa_family == AF_INET6
156  && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr);
157  }
158 
159 //------------------------------------------------------------------------------
164 //------------------------------------------------------------------------------
165 
166 bool isPrivate();
167 
168 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 
175 bool isRegistered();
176 
177 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 
184 bool isUsingTLS();
185 
186 //------------------------------------------------------------------------------
192 //------------------------------------------------------------------------------
193 
194 struct LocInfo
195  {unsigned char Country[2];
196  unsigned char Region;
197  unsigned char Locale;
198  char TimeZone;
199  char Flags;
200  short Speed;
201  int Latitude;
203 
204  LocInfo() : Region(0), Locale(0), TimeZone(-128), Flags(0),
205  Speed(0), Latitude(0), Longtitude(0) {*Country = 0;}
206 
207  };
208 
209 const struct
210 LocInfo *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);}
211 
212 //------------------------------------------------------------------------------
224 //------------------------------------------------------------------------------
225 
226 const char *Name(const char *eName=0, const char **eText=0);
227 
228 //------------------------------------------------------------------------------
234 //------------------------------------------------------------------------------
235 
236 const
237 XrdNetSockAddr *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);}
238 
239 //------------------------------------------------------------------------------
244 //------------------------------------------------------------------------------
245 
246 int Port();
247 
248 //------------------------------------------------------------------------------
253 //------------------------------------------------------------------------------
254 
255 int Protocol() {return static_cast<int>(protType);}
256 
257 //------------------------------------------------------------------------------
268 //------------------------------------------------------------------------------
269 
270 int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false);
271 
272 //------------------------------------------------------------------------------
277 //------------------------------------------------------------------------------
278 
279 const
280 sockaddr *SockAddr() {return sockAddr;}
281 
282 //------------------------------------------------------------------------------
287 //------------------------------------------------------------------------------
288 
289 SOCKLEN_t SockSize() {return static_cast<SOCKLEN_t>(addrSize);}
290 
291 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 
297 int SockFD() {return (sockNum ? sockNum : -1);}
298 
299 //------------------------------------------------------------------------------
301 //------------------------------------------------------------------------------
302 
304  {if (&rhs != this)
305  {memmove(&IP, &rhs.IP, sizeof(IP));
306  addrSize = rhs.addrSize; sockNum = rhs.sockNum;
307  protType = rhs.protType;
308  protFlgs = rhs.protFlgs;
309  protName = rhs.protName;
310  if (hostName) free(hostName);
311  hostName = (rhs.hostName ? strdup(rhs.hostName):0);
312  addrLoc = rhs.addrLoc;
313  if (rhs.sockAddr != &rhs.IP.Addr)
314  {if (!unixPipe || sockAddr == &IP.Addr)
315  unixPipe = new sockaddr_un;
316  memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un));
317  } else sockAddr = &IP.Addr;
318  }
319  return *this;
320  }
321 
322 //------------------------------------------------------------------------------
324 //------------------------------------------------------------------------------
325 
327  {hostName = 0;
328  unixPipe = 0;
329  *this = oP;
330  }
331 
332 //------------------------------------------------------------------------------
334 //------------------------------------------------------------------------------
335 
337  protFlgs(0), sockNum(0), protName(0)
338  {IP.Addr.sa_family = 0;
339  sockAddr = &IP.Addr;
340  }
341 
342  XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;}
343 
344 //------------------------------------------------------------------------------
346 //------------------------------------------------------------------------------
347 
349  if (sockAddr != &IP.Addr) delete unixPipe;
350  }
351 
352 protected:
353  char *LowCase(char *str);
354  int QFill(char *bAddr, int bLen);
355  int Resolve();
356 
358 
360 union {struct sockaddr *sockAddr;
361  struct sockaddr_un *unixPipe;
362  };
363 char *hostName;
365 unsigned short addrSize;
366 unsigned char protType;
367 unsigned char protFlgs;
369 const char *protName;
370 
371 // Flag settings in protFlgs
372 //
373 static const char isTLS = 0x01;
374 };
375 #endif
int SockFD()
Definition: XrdNetAddrInfo.hh:297
static XrdNetCache * dnsCache
Definition: XrdNetAddrInfo.hh:357
const XrdNetSockAddr * NetAddr()
Definition: XrdNetAddrInfo.hh:237
XrdNetSockAddr IP
Definition: XrdNetAddrInfo.hh:359
Definition: XrdNetAddrInfo.hh:144
Definition: XrdNetAddrInfo.hh:102
IPType
Definition: XrdNetAddrInfo.hh:144
struct sockaddr_in6 v6
Definition: XrdNetSockAddr.hh:43
XrdNetAddrInfo()
Constructor.
Definition: XrdNetAddrInfo.hh:336
LocInfo addrLoc
Definition: XrdNetAddrInfo.hh:364
int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false)
char * LowCase(char *str)
unsigned char Country[2]
Two letter TLD country code.
Definition: XrdNetAddrInfo.hh:195
struct sockaddr * sockAddr
Definition: XrdNetAddrInfo.hh:360
const struct LocInfo * Location()
Definition: XrdNetAddrInfo.hh:210
static const int noPort
Do not add port number.
Definition: XrdNetAddrInfo.hh:104
struct sockaddr Addr
Definition: XrdNetSockAddr.hh:45
short Speed
I/F speed (Gb*1024/100)(not supported)
Definition: XrdNetAddrInfo.hh:200
static const int old6Map4
Use deprecated IPV6 mapped format.
Definition: XrdNetAddrInfo.hh:106
const char * Dialect()
Definition: XrdNetAddrInfo.hh:65
struct sockaddr_un * unixPipe
Definition: XrdNetAddrInfo.hh:361
Definition: XrdNetSockAddr.hh:43
XrdNetAddrInfo & operator=(XrdNetAddrInfo const &rhs)
Assignment operator.
Definition: XrdNetAddrInfo.hh:303
unsigned short addrSize
Definition: XrdNetAddrInfo.hh:365
unsigned char protFlgs
Definition: XrdNetAddrInfo.hh:367
const sockaddr * SockAddr()
Definition: XrdNetAddrInfo.hh:280
int sockNum
Definition: XrdNetAddrInfo.hh:368
char * hostName
Definition: XrdNetAddrInfo.hh:363
unsigned char Region
Region (may combine adjacent countries)
Definition: XrdNetAddrInfo.hh:196
char TimeZone
+/- hours from GMT (-128 if not set)
Definition: XrdNetAddrInfo.hh:198
static const char isTLS
Location using TLS.
Definition: XrdNetAddrInfo.hh:373
int Latitude
Degrees +/- xx.xxxxxx (not supported)
Definition: XrdNetAddrInfo.hh:201
Definition: XrdNetAddrInfo.hh:194
Definition: XrdNetCache.hh:41
bool isRegistered()
bool isMapped() const
Definition: XrdNetAddrInfo.hh:155
Definition: XrdNetAddrInfo.hh:53
Hostname if already resolved o/w use fmtAddr.
Definition: XrdNetAddrInfo.hh:99
fmtUse
Definition: XrdNetAddrInfo.hh:99
XrdNetAddrInfo(const XrdNetAddrInfo *addr)
Definition: XrdNetAddrInfo.hh:342
int Family() const
Definition: XrdNetAddrInfo.hh:74
const char * protName
Definition: XrdNetAddrInfo.hh:369
LocInfo()
Definition: XrdNetAddrInfo.hh:204
SOCKLEN_t SockSize()
Definition: XrdNetAddrInfo.hh:289
~XrdNetAddrInfo()
Destructor.
Definition: XrdNetAddrInfo.hh:348
Definition: XrdNetAddrInfo.hh:144
#define SOCKLEN_t
Definition: XrdSysPlatform.hh:253
bool isIPType(IPType ipType) const
Definition: XrdNetAddrInfo.hh:146
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
int Longtitude
Degrees +/- xx.xxxxxx (not supported)
Definition: XrdNetAddrInfo.hh:202
Definition: XrdNetAddrInfo.hh:144
int QFill(char *bAddr, int bLen)
static bool isHostName(const char *name)
const char * Name(const char *eName=0, const char **eText=0)
static const int noPortRaw
Use raw address format (no port)
Definition: XrdNetAddrInfo.hh:105
static const int prefipv4
Use if mapped IPV4 actual format.
Definition: XrdNetAddrInfo.hh:107
unsigned char Locale
Locale (may combine adjacent regions)
Definition: XrdNetAddrInfo.hh:197
int Protocol()
Definition: XrdNetAddrInfo.hh:255
Hostname if it is resolvable o/w use fmtAddr.
Definition: XrdNetAddrInfo.hh:100
Address using suitable ipv4 or ipv6 format.
Definition: XrdNetAddrInfo.hh:101
XrdNetAddrInfo(XrdNetAddrInfo const &oP)
Copy constructor.
Definition: XrdNetAddrInfo.hh:326
unsigned char protType
Definition: XrdNetAddrInfo.hh:366
char Flags
Flags.
Definition: XrdNetAddrInfo.hh:199