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