xrootd
Main Page
Namespaces
Classes
Files
File List
File Members
src
XrdSys
XrdSysDNS.hh
Go to the documentation of this file.
1
#ifndef __XRDSYSDNS__
2
#define __XRDSYSDNS__
3
/******************************************************************************/
4
/* */
5
/* X r d S y s D N S . h h */
6
/* */
7
/* (c) 2004 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
13
#include <sys/types.h>
14
#ifndef WIN32
15
#include <sys/socket.h>
16
#else
17
#include <Winsock2.h>
18
#endif
19
20
class
XrdSysDNS
21
{
22
public
:
23
24
// Note: Most methods allow the reason for failure to be returned via an errtxt
25
// argument. The string returned in errtxt is static and must neither be
26
// modified not freed.
27
28
// getHostAddr() translates an host name or an ascii host ip address to the
29
// binary address suitable for use in network system calls. The
30
// host name or address must be registered in the DNS for the
31
// translation to be successful. Upon success the either the
32
// primary address (1st form) or a list of addresses (2nd form)
33
// up to maxipa is returned. The return values are:
34
// 0 -> Host name could not be translated, the error text
35
// is placed in errtxt, if an address is supplied.
36
// > 0 -> The number of addresses returned.
37
//
38
static
int
getHostAddr
(
const
char
*InetName,
39
struct
sockaddr &InetAddr,
40
char
**errtxt=0)
41
{
return
getHostAddr
(InetName, &InetAddr, 1, errtxt);}
42
43
static
int
getHostAddr
(
const
char
*InetName,
44
struct
sockaddr InetAddr[],
45
int
maxipa=1,
46
char
**errtxt=0);
47
48
// getHostID() returns the ASCII string corresponding to the IP address
49
// InetAddr. If a translation is successful, the address
50
// of an strdup'd null terminated name is returned (it must be
51
// released using free()). Otherwise, an strdup of '0.0.0.0' is
52
// returned (which must also be freed).
53
//
54
static
char
*
getHostID
(
struct
sockaddr &InetAddr);
55
56
// getAddrName() finds addresses and names associated with an host name or
57
// an ascii host ip address. The host name or address must be
58
// registered in the DNS for the translation to be successful.
59
// Upon success a list of addresses and names up to maxipa is
60
// returned in the arrays haddr and hname. The arrays must be
61
// previously allocated by the caller for at least maxipa
62
// 'char *'. The returned char arrays are allocated inside and
63
// must be freed by the caller. The return values are:
64
// 0 -> Host name could not be translated, the error text
65
// is placed in errtxt, if an address is supplied.
66
// > 0 -> The number of addresses returned.
67
//
68
static
int
getAddrName
(
const
char
*InetName,
69
int
maxipa,
70
char
**haddr,
71
char
**hname,
72
char
**errtxt=0);
73
74
// getHostName() returns the fully qualified name of a host. If no partial
75
// host name is specified (or specifiied as 0), the fully
76
// qualified name of this host is returned. The name is returned
77
// as an strdup'd string which must be released using free().
78
// If errtxt is supplied, it is set to zero.
79
// Upon failure, strdup("0.0.0.0") is returned and the error
80
// text is placed in errtxt if an address is supplied.
81
//
82
static
char
*
getHostName
(
const
char
*InetName=0,
83
char
**errtxt=0);
84
85
// getHostName() returns the primary name of the host associated with the IP
86
// address InetAddr. If a translation is successful, the address
87
// of an strdup'd null terminated name is returned (it must be
88
// released using free()) and errtxt, of supplied, is set to 0.
89
// Upon failure, the ascii text version of the address is
90
// returned and the error text is placed in errtxt if an
91
// address is supplied.
92
//
93
static
char
*
getHostName
(
struct
sockaddr &InetAddr,
94
char
**errtxt=0);
95
96
// getHostName() returns the names of the host associated with the IP address
97
// InetAddr. The first name is the primary name of the host.
98
// Upon success, the address of each null terminated name is
99
// placed in InetName[i]. Up to maxipn names are returned. The
100
// array must be large enough to hold maxipn entries, Each
101
// name is returned as an strdup'd string, which must be
102
// released using free(). Return values are:
103
// 0 -> No names could be returned; the error text is placed
104
// in errtxt if an address is supplied.
105
// >0 -> Number of names returned.
106
//
107
static
int
getHostName
(
struct
sockaddr &InetAddr,
108
char
*InetName[],
109
int
maxipn,
110
char
**errtxt=0);
111
112
// getPort() returns the port number of the service corresponding to the
113
// supplied name and service type (i.e., "tcp" or "udp"). If the port
114
// cannot be found, zero is returned and the error text is placed
115
// in errtxt if an address is supplied.
116
//
117
static
int
getPort
(
const
char
*servname,
118
const
char
*servtype,
119
char
**errtxt=0);
120
121
// getPort() variant returns the port number associated with the specified
122
// file descriptor. If an error occurs, a negative errno is returned,
123
// and errtxt is set if supplied.
124
//
125
static
int
getPort
(
int
fd,
char
**errtxt=0);
126
127
// getProtoID() returns the protocol number associated with the protocol name
128
// passed as a parameter. No failures can occur since TCP is
129
// returned if the protocol cannot be found.
130
//
131
static
int
getProtoID
(
const
char
*pname);
132
133
// Host2Dest() returns a sockaddr structure suitable for socket operations
134
// built from the "host:port" specified in InetName. It returns
135
// 1 upon success and 0 upon failure with the reason placed in
136
// errtxt, if as address is supplied.
137
//
138
static
int
Host2Dest
(
const
char
*InetName,
139
struct
sockaddr &DestAddr,
140
char
**errtxt=0);
141
142
// Host2IP() converts a host name passed in InetName to an IPV4 address,
143
// returned in ipaddr (unless it is zero, in which only a conversion
144
// check is performed). 1 is returned upon success, 0 upon failure.
145
//
146
static
int
Host2IP
(
const
char
*InetName,
147
unsigned
int
*ipaddr=0);
148
149
// IPFormat() converts an IP address/port (V4 or V6) into the standard V6 RFC
150
// ASCII representation: "[address]:port".
151
152
// Input: sAddr - Address to convert. This is either sockaddr_in or
153
// sockaddr_in6 cast to struct sockaddr.
154
// bP - points to a buffer large enough to hold the result.
155
// A buffer 64 characters long will always be big enough.
156
// bL - the actual size of the buffer.
157
// fP - When true (the default) will format sAddr->sin_port
158
// (or sin6_port) as ":port" at the end of the address.
159
// When false the colon and port number is omitted.
160
//
161
// Output: Upon success the length of the formatted address is returned.
162
// Upon failure zero is returned and the buffer state is undefined.
163
// Failure occurs when the buffer is too small or the address family
164
// (sAddr->sa_family) is neither AF_INET nor AF_INET6.
165
//
166
static
int
IPFormat
(
const
struct
sockaddr *sAddr,
char
*bP,
int
bL,
int
fP=1);
167
168
// IP2String() converts an IPV4 version of the address to ascii dot notation
169
// If port > 0 then the results is <ipaddr>:<port>. The return
170
// value is the number of characters placed in the buffer.
171
//
172
static
int
IP2String
(
unsigned
int
ipaddr,
int
port
,
char
*buff,
int
blen);
173
174
// IPAddr() returns the IPV4 version of the address in the address argument
175
//
176
static
unsigned
int
IPAddr
(
struct
sockaddr *InetAddr);
177
178
// isDomain() returns true if the domain portion of the hostname matches
179
// the specified domain name.
180
//
181
static
int
isDomain
(
const
char
*Hostname,
const
char
*Domname,
int
Domlen);
182
183
// isLoopback() returns true if the address in InetAddr is the loopback address.
184
// This test is used to discover IP address spoofing in UDP packets.
185
//
186
static
int
isLoopback
(
struct
sockaddr &InetAddr);
187
188
// isMatch() returns true if the HostName matches the host pattern HostPat.
189
// Patterns are formed as {[<pfx>][*][<sfx>] | <name>+}
190
//
191
static
int
isMatch
(
const
char
*HostNme,
char
*HostPat);
192
193
// Peername() returns the strdupp'd string name (and optionally the address) of
194
// the host associated with the socket passed as the first parameter.
195
// The string must be released using free(). If the host cannot be
196
// determined, 0 is returned and the error text is placed in errtxt
197
// if an address is supplied.
198
//
199
static
char
*
Peername
(
int
snum,
200
struct
sockaddr *sap=0,
201
char
**errtxt=0);
202
203
// setPort() sets the port number InetAddr. If anyaddr is true,, InetAddr is
204
// initialized to the network defined "any" IP address.
205
//
206
static
void
setPort
(
struct
sockaddr &InetAddr,
int
port
,
int
anyaddr=0);
207
208
XrdSysDNS
() {}
209
~XrdSysDNS
() {}
210
211
private
:
212
213
static
char
*
LowCase
(
char
*str);
214
static
int
setET
(
char
**errtxt,
int
rc);
215
static
int
setETni
(
char
**errtxt,
int
rc);
216
};
217
#endif
Generated by
1.8.1.2