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