00001 #ifndef __XRDSYSDNS__ 00002 #define __XRDSYSDNS__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S y s D N S . h h */ 00006 /* */ 00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00034 /* This class is deprecated and essentially OBSOLETE and no longer mainatined.*/ 00035 /* */ 00036 /* This class only supports IPV4 addresses and contexts. Please use classes */ 00037 /* XrdNetAddr, XrdNetAddrInfo, and XrdNetUtils that provide IP address format */ 00038 /* agnostic replacement methods. SysDNS will be removed the next major release*/ 00041 #include <sys/types.h> 00042 #ifndef WIN32 00043 #include <sys/socket.h> 00044 #else 00045 #include <Winsock2.h> 00046 #endif 00047 00048 class XrdSysDNS 00049 { 00050 public: 00051 00052 // Note: Most methods allow the reason for failure to be returned via an errtxt 00053 // argument. The string returned in errtxt is static and must neither be 00054 // modified not freed. 00055 00056 // getHostAddr() translates an host name or an ascii host ip address to the 00057 // binary address suitable for use in network system calls. The 00058 // host name or address must be registered in the DNS for the 00059 // translation to be successful. Upon success the either the 00060 // primary address (1st form) or a list of addresses (2nd form) 00061 // up to maxipa is returned. The return values are: 00062 // 0 -> Host name could not be translated, the error text 00063 // is placed in errtxt, if an address is supplied. 00064 // > 0 -> The number of addresses returned. 00065 // 00066 static int getHostAddr(const char *InetName, 00067 struct sockaddr &InetAddr, 00068 const char **errtxt=0) 00069 {return getHostAddr(InetName, &InetAddr, 1, errtxt);} 00070 00071 static int getHostAddr(const char *InetName, 00072 struct sockaddr InetAddr[], 00073 int maxipa=1, 00074 const char **errtxt=0); 00075 00076 // getHostID() returns the ASCII string corresponding to the IP address 00077 // InetAddr. If a translation is successful, the address 00078 // of an strdup'd null terminated name is returned (it must be 00079 // released using free()). Otherwise, an strdup of '0.0.0.0' is 00080 // returned (which must also be freed). 00081 // 00082 static char *getHostID(struct sockaddr &InetAddr); 00083 00084 // getAddrName() finds addresses and names associated with an host name or 00085 // an ascii host ip address. The host name or address must be 00086 // registered in the DNS for the translation to be successful. 00087 // Upon success a list of addresses and names up to maxipa is 00088 // returned in the arrays haddr and hname. The arrays must be 00089 // previously allocated by the caller for at least maxipa 00090 // 'char *'. The returned char arrays are allocated inside and 00091 // must be freed by the caller. The return values are: 00092 // 0 -> Host name could not be translated, the error text 00093 // is placed in errtxt, if an address is supplied. 00094 // > 0 -> The number of addresses returned. 00095 // 00096 static int getAddrName(const char *InetName, 00097 int maxipa, 00098 char **haddr, 00099 char **hname, 00100 const char **errtxt=0); 00101 00102 // getHostName() returns the fully qualified name of a host. If no partial 00103 // host name is specified (or specifiied as 0), the fully 00104 // qualified name of this host is returned. The name is returned 00105 // as an strdup'd string which must be released using free(). 00106 // If errtxt is supplied, it is set to zero. 00107 // Upon failure, strdup("0.0.0.0") is returned and the error 00108 // text is placed in errtxt if an address is supplied. 00109 // 00110 static char *getHostName(const char *InetName=0, 00111 const char **errtxt=0); 00112 00113 // getHostName() returns the primary name of the host associated with the IP 00114 // address InetAddr. If a translation is successful, the address 00115 // of an strdup'd null terminated name is returned (it must be 00116 // released using free()) and errtxt, of supplied, is set to 0. 00117 // Upon failure, the ascii text version of the address is 00118 // returned and the error text is placed in errtxt if an 00119 // address is supplied. 00120 // 00121 static char *getHostName(struct sockaddr &InetAddr, 00122 const char **errtxt=0); 00123 00124 // getHostName() returns the names of the host associated with the IP address 00125 // InetAddr. The first name is the primary name of the host. 00126 // Upon success, the address of each null terminated name is 00127 // placed in InetName[i]. Up to maxipn names are returned. The 00128 // array must be large enough to hold maxipn entries, Each 00129 // name is returned as an strdup'd string, which must be 00130 // released using free(). Return values are: 00131 // 0 -> No names could be returned; the error text is placed 00132 // in errtxt if an address is supplied. 00133 // >0 -> Number of names returned. 00134 // 00135 static int getHostName(struct sockaddr &InetAddr, 00136 char *InetName[], 00137 int maxipn, 00138 const char **errtxt=0); 00139 00140 // getPort() returns the port number of the service corresponding to the 00141 // supplied name and service type (i.e., "tcp" or "udp"). If the port 00142 // cannot be found, zero is returned and the error text is placed 00143 // in errtxt if an address is supplied. 00144 // 00145 static int getPort(const char *servname, 00146 const char *servtype, 00147 const char **errtxt=0); 00148 00149 // getPort() variant returns the port number associated with the specified 00150 // file descriptor. If an error occurs, a negative errno is returned, 00151 // and errtxt is set if supplied. 00152 // 00153 static int getPort(int fd, const char **errtxt=0); 00154 00155 // getProtoID() returns the protocol number associated with the protocol name 00156 // passed as a parameter. No failures can occur since TCP is 00157 // returned if the protocol cannot be found. 00158 // 00159 static int getProtoID(const char *pname); 00160 00161 // Host2Dest() returns a sockaddr structure suitable for socket operations 00162 // built from the "host:port" specified in InetName. It returns 00163 // 1 upon success and 0 upon failure with the reason placed in 00164 // errtxt, if as address is supplied. 00165 // 00166 static int Host2Dest(const char *InetName, 00167 struct sockaddr &DestAddr, 00168 const char **errtxt=0); 00169 00170 // Host2IP() converts a host name passed in InetName to an IPV4 address, 00171 // returned in ipaddr (unless it is zero, in which only a conversion 00172 // check is performed). 1 is returned upon success, 0 upon failure. 00173 // 00174 static int Host2IP(const char *InetName, 00175 unsigned int *ipaddr=0); 00176 00177 // IPFormat() converts an IP address/port (V4 or V6) into the standard V6 RFC 00178 // ASCII representation: "[address]:port". 00179 00180 // Input: sAddr - Address to convert. This is either sockaddr_in or 00181 // sockaddr_in6 cast to struct sockaddr. 00182 // bP - points to a buffer large enough to hold the result. 00183 // A buffer 64 characters long will always be big enough. 00184 // bL - the actual size of the buffer. 00185 // fP - When true (the default) will format sAddr->sin_port 00186 // (or sin6_port) as ":port" at the end of the address. 00187 // When false the colon and port number is omitted. 00188 // 00189 // Output: Upon success the length of the formatted address is returned. 00190 // Upon failure zero is returned and the buffer state is undefined. 00191 // Failure occurs when the buffer is too small or the address family 00192 // (sAddr->sa_family) is neither AF_INET nor AF_INET6. 00193 // 00194 static int IPFormat(const struct sockaddr *sAddr, char *bP, int bL, int fP=1); 00195 00196 // IP2String() converts an IPV4 version of the address to ascii dot notation 00197 // If port > 0 then the results is <ipaddr>:<port>. The return 00198 // value is the number of characters placed in the buffer. 00199 // 00200 static int IP2String(unsigned int ipaddr, int port, char *buff, int blen); 00201 00202 // IPAddr() returns the IPV4 version of the address in the address argument 00203 // 00204 static unsigned int IPAddr(struct sockaddr *InetAddr); 00205 00206 // isDomain() returns true if the domain portion of the hostname matches 00207 // the specified domain name. 00208 // 00209 static int isDomain(const char *Hostname, const char *Domname, int Domlen); 00210 00211 // isLoopback() returns true if the address in InetAddr is the loopback address. 00212 // This test is used to discover IP address spoofing in UDP packets. 00213 // 00214 static int isLoopback(struct sockaddr &InetAddr); 00215 00216 // isMatch() returns true if the HostName matches the host pattern HostPat. 00217 // Patterns are formed as {[<pfx>][*][<sfx>] | <name>+} 00218 // 00219 static int isMatch(const char *HostNme, char *HostPat); 00220 00221 // Peername() returns the strdupp'd string name (and optionally the address) of 00222 // the host associated with the socket passed as the first parameter. 00223 // The string must be released using free(). If the host cannot be 00224 // determined, 0 is returned and the error text is placed in errtxt 00225 // if an address is supplied. 00226 // 00227 static char *Peername( int snum, 00228 struct sockaddr *sap=0, 00229 const char **errtxt=0); 00230 00231 // setPort() sets the port number InetAddr. If anyaddr is true,, InetAddr is 00232 // initialized to the network defined "any" IP address. 00233 // 00234 static void setPort(struct sockaddr &InetAddr, int port, int anyaddr=0); 00235 00236 XrdSysDNS() {} 00237 ~XrdSysDNS() {} 00238 00239 private: 00240 00241 static char *LowCase(char *str); 00242 static int setET(const char **errtxt, int rc); 00243 static int setETni(const char **errtxt, int rc); 00244 }; 00245 #endif