XRootD
Loading...
Searching...
No Matches
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 <cinttypes>
34#include <cstdlib>
35#include <cstring>
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//------------------------------------------------------------------------------
63//------------------------------------------------------------------------------
64
65const char *Dialect() {return protName;}
66
67//------------------------------------------------------------------------------
72//------------------------------------------------------------------------------
73
74int Family() const {return static_cast<int>(IP.Addr.sa_family);}
75
76//------------------------------------------------------------------------------
97//------------------------------------------------------------------------------
98
99enum fmtUse {fmtAuto=0,
103
104static const int noPort = 0x0000001;
105static const int noPortRaw = 0x0000002;
106static const int old6Map4 = 0x0000004;
107static const int prefipv4 = 0x0000008;
108
109int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0);
110
111//------------------------------------------------------------------------------
117//------------------------------------------------------------------------------
118
119bool isLoopback();
120
121//------------------------------------------------------------------------------
131//------------------------------------------------------------------------------
132
133static bool isHostName(const char *name);
134
135//------------------------------------------------------------------------------
142//------------------------------------------------------------------------------
143
144enum IPType {IPv4 = AF_INET, IPv6 = AF_INET6, IPuX = AF_UNIX};
145
146bool isIPType(IPType ipType) const {return IP.Addr.sa_family == ipType;}
147
148//------------------------------------------------------------------------------
153//------------------------------------------------------------------------------
154
155bool isLocal();
156
157//------------------------------------------------------------------------------
162//------------------------------------------------------------------------------
163
164bool isMapped() const {return IP.Addr.sa_family == AF_INET6
165 && IN6_IS_ADDR_V4MAPPED(&IP.v6.sin6_addr);
166 }
167
168//------------------------------------------------------------------------------
173//------------------------------------------------------------------------------
174
175bool isPrivate();
176
177//------------------------------------------------------------------------------
182//------------------------------------------------------------------------------
183
184bool isRegistered();
185
186//------------------------------------------------------------------------------
191//------------------------------------------------------------------------------
192
193bool isUsingTLS();
194
195//------------------------------------------------------------------------------
201//------------------------------------------------------------------------------
202
204 {unsigned char Country[2];
205 unsigned char Region;
206 unsigned char Locale;
207 char TimeZone;
208 char Flags;
209 short Speed;
212
213 LocInfo() : Region(0), Locale(0), TimeZone(-128), Flags(0),
214 Speed(0), Latitude(0), Longtitude(0) {*Country = 0;}
215
216 };
217
218const struct
219LocInfo *Location() {return (addrLoc.Country[0] ? &addrLoc : 0);}
220
221//------------------------------------------------------------------------------
233//------------------------------------------------------------------------------
234
235const char *Name(const char *eName=0, const char **eText=0);
236
237//------------------------------------------------------------------------------
243//------------------------------------------------------------------------------
244
245const
246XrdNetSockAddr *NetAddr() {return (sockAddr == (void *)&IP ? &IP : 0);}
247
248//------------------------------------------------------------------------------
253//------------------------------------------------------------------------------
254
255int Port();
256
257//------------------------------------------------------------------------------
262//------------------------------------------------------------------------------
263
264int Protocol() {return static_cast<int>(protType);}
265
266//------------------------------------------------------------------------------
277//------------------------------------------------------------------------------
278
279int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false);
280
281//------------------------------------------------------------------------------
286//------------------------------------------------------------------------------
287
288const
289sockaddr *SockAddr() {return sockAddr;}
290
291//------------------------------------------------------------------------------
296//------------------------------------------------------------------------------
297
298SOCKLEN_t SockSize() {return static_cast<SOCKLEN_t>(addrSize);}
299
300//------------------------------------------------------------------------------
304//------------------------------------------------------------------------------
305
306int SockFD() {return (sockNum ? sockNum : -1);}
307
308//------------------------------------------------------------------------------
310//------------------------------------------------------------------------------
311
313 {if (&rhs != this)
314 {memmove(&IP, &rhs.IP, sizeof(IP));
315 addrSize = rhs.addrSize; sockNum = rhs.sockNum;
316 protType = rhs.protType;
317 protFlgs = rhs.protFlgs;
318 protName = rhs.protName;
319 if (hostName) free(hostName);
320 hostName = (rhs.hostName ? strdup(rhs.hostName):0);
321 addrLoc = rhs.addrLoc;
322 if (rhs.sockAddr != &rhs.IP.Addr)
323 {if (!unixPipe || sockAddr == &IP.Addr)
324 unixPipe = new sockaddr_un;
325 memcpy(unixPipe, rhs.unixPipe, sizeof(sockaddr_un));
326 } else sockAddr = &IP.Addr;
327 }
328 return *this;
329 }
330
331//------------------------------------------------------------------------------
333//------------------------------------------------------------------------------
334
336 {hostName = 0;
337 unixPipe = 0;
338 *this = oP;
339 }
340
341//------------------------------------------------------------------------------
343//------------------------------------------------------------------------------
344
346 protFlgs(0), sockNum(0), protName(0)
347 {IP.Addr.sa_family = 0;
348 sockAddr = &IP.Addr;
349 }
350
351 XrdNetAddrInfo(const XrdNetAddrInfo *addr) : hostName(0) {*this = *addr;}
352
353//------------------------------------------------------------------------------
355//------------------------------------------------------------------------------
356
358 if (sockAddr != &IP.Addr) delete unixPipe;
359 }
360
361protected:
362 char *LowCase(char *str);
363 int QFill(char *bAddr, int bLen);
364 int Resolve();
365
367
369union {struct sockaddr *sockAddr;
370 struct sockaddr_un *unixPipe;
371 };
374unsigned short addrSize;
375unsigned char protType;
376unsigned char protFlgs;
378const char *protName;
379
380// Flag settings in protFlgs
381//
382static const char isTLS = 0x01;
383};
384#endif
struct sockaddr Addr
#define SOCKLEN_t
const char * protName
static XrdNetCache * dnsCache
static const int noPort
Do not add port number.
static const int old6Map4
Use deprecated IPV6 mapped format.
bool isMapped() const
~XrdNetAddrInfo()
Destructor.
static bool isHostName(const char *name)
XrdNetAddrInfo()
Constructor.
unsigned char protFlgs
XrdNetSockAddr IP
const sockaddr * SockAddr()
static const int noPortRaw
Use raw address format (no port)
int Same(const XrdNetAddrInfo *ipAddr, bool plusPort=false)
static const int prefipv4
Use if mapped IPV4 actual format.
bool isIPType(IPType ipType) const
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
unsigned char protType
XrdNetAddrInfo(XrdNetAddrInfo const &oP)
Copy constructor.
char * LowCase(char *str)
const struct LocInfo * Location()
int QFill(char *bAddr, int bLen)
const char * Dialect()
@ fmtAddr
Address using suitable ipv4 or ipv6 format.
@ fmtName
Hostname if it is resolvable o/w use fmtAddr.
@ fmtAuto
Hostname if already resolved o/w use fmtAddr.
SOCKLEN_t SockSize()
static const char isTLS
Location using TLS.
unsigned short addrSize
const char * Name(const char *eName=0, const char **eText=0)
XrdNetAddrInfo(const XrdNetAddrInfo *addr)
int Family() const
const XrdNetSockAddr * NetAddr()
XrdNetAddrInfo & operator=(XrdNetAddrInfo const &rhs)
Assignment operator.
int Latitude
Degrees +/- xx.xxxxxx (not supported)
unsigned char Region
Region (may combine adjacent countries)
char TimeZone
+/- hours from GMT (-128 if not set)
unsigned char Country[2]
Two letter TLD country code.
short Speed
I/F speed (Gb*1024/100)(not supported)
unsigned char Locale
Locale (may combine adjacent regions)
int Longtitude
Degrees +/- xx.xxxxxx (not supported)