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
42
43//------------------------------------------------------------------------------
48//------------------------------------------------------------------------------
49
50struct addrinfo;
51class XrdNetCache;
52
54{
55public:
56
57//------------------------------------------------------------------------------
62//------------------------------------------------------------------------------
63
64inline int Family() const {return static_cast<int>(IP.Addr.sa_family);}
65
66//------------------------------------------------------------------------------
87//------------------------------------------------------------------------------
88
89enum fmtUse {fmtAuto=0,
93
94static const int noPort = 0x0000001;
95static const int noPortRaw = 0x0000002;
96static const int old6Map4 = 0x0000004;
97static const int prefipv4 = 0x0000008;
98
99int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0);
100
101//------------------------------------------------------------------------------
107//------------------------------------------------------------------------------
108
110
111//------------------------------------------------------------------------------
121//------------------------------------------------------------------------------
122
123static bool isHostName(const char *name);
124
125//------------------------------------------------------------------------------
132//------------------------------------------------------------------------------
133
134enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX};
135
136inline bool isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;}
137
138//------------------------------------------------------------------------------
143//------------------------------------------------------------------------------
144
145inline bool isMapped() const {return IP.Addr.sa_family == AF_INET6
146 && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr);
147 }
148
149//------------------------------------------------------------------------------
154//------------------------------------------------------------------------------
155
157
158//------------------------------------------------------------------------------
163//------------------------------------------------------------------------------
164
166
167//------------------------------------------------------------------------------
173//------------------------------------------------------------------------------
174
176 {unsigned char Country[2];
177 unsigned char Region;
178 unsigned char Locale;
179 char TimeZone;
181 short Speed;
184
185 LocInfo() : Region(0), Locale(0), TimeZone(-128), Reserved(0),
186 Speed(0), Latitude(0), Longtitude(0) {*Country = 0;}
187 };
188
189const struct
190LocInfo *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);}
191
192//------------------------------------------------------------------------------
204//------------------------------------------------------------------------------
205
206const char *Name(const char *eName=0, const char **eText=0);
207
208//------------------------------------------------------------------------------
214//------------------------------------------------------------------------------
215
216inline const
217XrdNetSockAddr *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);}
218
219//------------------------------------------------------------------------------
224//------------------------------------------------------------------------------
225
226int Port();
227
228//------------------------------------------------------------------------------
233//------------------------------------------------------------------------------
234
235inline int Protocol() {return static_cast<int>(protType);}
236
237//------------------------------------------------------------------------------
246//-----------------------------------------------------------------------------sav-
247
248int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false);
249
250//------------------------------------------------------------------------------
255//------------------------------------------------------------------------------
256
257inline const
258sockaddr *SockAddr() {return sockAddr;}
259
260//------------------------------------------------------------------------------
265//------------------------------------------------------------------------------
266
267inline
269
270//------------------------------------------------------------------------------
274//------------------------------------------------------------------------------
275
276inline int SockFD() {return (sockNum ? static_cast<int>(sockNum) : -1);}
277
278//------------------------------------------------------------------------------
280//------------------------------------------------------------------------------
281
283 {if (&rhs != this)
284 {memmove(&IP, &rhs.IP, sizeof(IP));
285 addrSize = rhs.addrSize; sockNum = rhs.sockNum;
286 protType = rhs.protType;
287 if (hostName) free(hostName);
288 hostName = (rhs.hostName ? strdup(rhs.hostName):0);
289 addrLoc = rhs.addrLoc;
290 if (rhs.sockAddr != &rhs.IP.Addr)
291 {if (!unixPipe || sockAddr == &IP.Addr)
292 unixPipe = new sockaddr_un;
293 memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un));
294 } else sockAddr = &IP.Addr;
295 }
296 return *this;
297 }
298
299//------------------------------------------------------------------------------
301//------------------------------------------------------------------------------
302
304 {hostName = 0;
305 unixPipe = 0;
306 *this = oP;
307 }
308
309//------------------------------------------------------------------------------
311//------------------------------------------------------------------------------
312
314 {IP.Addr.sa_family = 0;
315 sockAddr = &IP.Addr;
316 }
317
318 XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;}
319
320//------------------------------------------------------------------------------
322//------------------------------------------------------------------------------
323
325 if (sockAddr != &IP.Addr) delete unixPipe;
326 }
327
328protected:
329 char *LowCase(char *str);
330 int QFill(char *bAddr, int bLen);
331 int Resolve();
332
334
336union {struct sockaddr *sockAddr;
337 struct sockaddr_un *unixPipe;
338 };
343unsigned short sockNum;
344};
345#endif
#define SOCKLEN_t
Definition: XrdSysPlatform.hh:235
Definition: XrdNetAddrInfo.hh:54
SOCKLEN_t addrSize
Definition: XrdNetAddrInfo.hh:341
static const int noPort
Do not add port number.
Definition: XrdNetAddrInfo.hh:94
static const int old6Map4
Use deprecated IPV6 mapped format.
Definition: XrdNetAddrInfo.hh:96
struct sockaddr_un * unixPipe
Definition: XrdNetAddrInfo.hh:337
bool isMapped() const
Definition: XrdNetAddrInfo.hh:145
char * hostName
Definition: XrdNetAddrInfo.hh:339
~XrdNetAddrInfo()
Destructor.
Definition: XrdNetAddrInfo.hh:324
XrdNetAddrInfo()
Constructor.
Definition: XrdNetAddrInfo.hh:313
IPType
Definition: XrdNetAddrInfo.hh:134
@ IPv4
Definition: XrdNetAddrInfo.hh:134
@ IPv6
Definition: XrdNetAddrInfo.hh:134
@ IPuX
Definition: XrdNetAddrInfo.hh:134
XrdNetSockAddr IP
Definition: XrdNetAddrInfo.hh:335
const sockaddr * SockAddr()
Definition: XrdNetAddrInfo.hh:258
LocInfo addrLoc
Definition: XrdNetAddrInfo.hh:340
static const int noPortRaw
Use raw address format (no port)
Definition: XrdNetAddrInfo.hh:95
int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false)
bool isRegistered()
static const int prefipv4
Use if mapped IPV4 actual format.
Definition: XrdNetAddrInfo.hh:97
static bool isHostName(const char *name)
int Protocol()
Definition: XrdNetAddrInfo.hh:235
bool isIPType(IPType ipType) const
Definition: XrdNetAddrInfo.hh:136
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
XrdNetAddrInfo(XrdNetAddrInfo const &oP)
Copy constructor.
Definition: XrdNetAddrInfo.hh:303
char * LowCase(char *str)
const struct LocInfo * Location()
Definition: XrdNetAddrInfo.hh:190
unsigned short sockNum
Definition: XrdNetAddrInfo.hh:343
int QFill(char *bAddr, int bLen)
short protType
Definition: XrdNetAddrInfo.hh:342
fmtUse
Definition: XrdNetAddrInfo.hh:89
@ fmtAddr
Address using suitable ipv4 or ipv6 format.
Definition: XrdNetAddrInfo.hh:91
@ fmtName
Hostname if it is resolvable o/w use fmtAddr.
Definition: XrdNetAddrInfo.hh:90
@ fmtAuto
Hostname if already resolved o/w use fmtAddr.
Definition: XrdNetAddrInfo.hh:89
@ fmtAdv6
Definition: XrdNetAddrInfo.hh:92
SOCKLEN_t SockSize()
Definition: XrdNetAddrInfo.hh:268
const char * Name(const char *eName=0, const char **eText=0)
XrdNetAddrInfo(const XrdNetAddrInfo *addr)
Definition: XrdNetAddrInfo.hh:318
int SockFD()
Definition: XrdNetAddrInfo.hh:276
int Family() const
Definition: XrdNetAddrInfo.hh:64
struct sockaddr * sockAddr
Definition: XrdNetAddrInfo.hh:336
const XrdNetSockAddr * NetAddr()
Definition: XrdNetAddrInfo.hh:217
XrdNetAddrInfo & operator=(XrdNetAddrInfo const &rhs)
Assignment operator.
Definition: XrdNetAddrInfo.hh:282
static XrdNetCache * dnsCache
Definition: XrdNetAddrInfo.hh:333
Definition: XrdNetCache.hh:42
Definition: XrdNetAddrInfo.hh:176
int Latitude
Degrees +/- xx.xxxxxx (not supported)
Definition: XrdNetAddrInfo.hh:182
char Reserved
Definition: XrdNetAddrInfo.hh:180
unsigned char Region
Region (may combine adjacent countries)
Definition: XrdNetAddrInfo.hh:177
char TimeZone
+/- hours from GMT (-128 if not set)
Definition: XrdNetAddrInfo.hh:179
unsigned char Country[2]
Two letter TLD country code.
Definition: XrdNetAddrInfo.hh:176
short Speed
I/F speed (Gb*1024/100)(not supported)
Definition: XrdNetAddrInfo.hh:181
unsigned char Locale
Locale (may combine adjacent regions)
Definition: XrdNetAddrInfo.hh:178
LocInfo()
Definition: XrdNetAddrInfo.hh:185
int Longtitude
Degrees +/- xx.xxxxxx (not supported)
Definition: XrdNetAddrInfo.hh:183
Definition: XrdNetSockAddr.hh:43
struct sockaddr_in6 v6
Definition: XrdNetSockAddr.hh:43
struct sockaddr Addr
Definition: XrdNetSockAddr.hh:45