00001 #ifndef __XRDNETDNS__ 00002 #define __XRDNETDNS__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d N e t 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 00013 #include <sys/types.h> 00014 #ifndef WIN32 00015 #include <sys/socket.h> 00016 #else 00017 #include <Winsock2.h> 00018 #endif 00019 00020 // $Id: XrdNetDNS.hh 25932 2008-10-23 10:58:11Z ganis $ 00021 00022 class XrdNetDNS 00023 { 00024 public: 00025 00026 // Note: Most methods allow the reason for failure to be returned via an errtxt 00027 // argument. The string returned in errtxt is static and must neither be 00028 // modified not freed. 00029 00030 // getHostAddr() translates an host name or an ascii host ip address to the 00031 // binary address suitable for use in network system calls. The 00032 // host name or address must be registered in the DNS for the 00033 // translation to be successful. Upon success the either the 00034 // primary address (1st form) or a list of addresses (2nd form) 00035 // up to maxipa is returned. The return values are: 00036 // 0 -> Host name could not be translated, the error text 00037 // is placed in errtxt, if an address is supplied. 00038 // > 0 -> The number of addresses returned. 00039 // 00040 static int getHostAddr(const char *InetName, 00041 struct sockaddr &InetAddr, 00042 char **errtxt=0) 00043 {return getHostAddr(InetName, &InetAddr, 1, errtxt);} 00044 00045 static int getHostAddr(const char *InetName, 00046 struct sockaddr InetAddr[], 00047 int maxipa=1, 00048 char **errtxt=0); 00049 00050 // getHostID() returns the ASCII string corresponding to the IP address 00051 // InetAddr. If a translation is successful, the address 00052 // of an strdup'd null terminated name is returned (it must be 00053 // released using free()). Otherwise, an strdup of '0.0.0.0' is 00054 // returned (which must also be freed). 00055 // 00056 static char *getHostID(struct sockaddr &InetAddr); 00057 00058 // getAddrName() finds addresses and names associated with an host name or 00059 // an ascii host ip address. The host name or address must be 00060 // registered in the DNS for the translation to be successful. 00061 // Upon success a list of addresses and names up to maxipa is 00062 // returned in the arrays haddr and hname. The arrays must be 00063 // previously allocated by the caller for at least maxipa 00064 // 'char *'. The returned char arrays are allocated inside and 00065 // must be freed by the caller. The return values are: 00066 // 0 -> Host name could not be translated, the error text 00067 // is placed in errtxt, if an address is supplied. 00068 // > 0 -> The number of addresses returned. 00069 // 00070 static int getAddrName(const char *InetName, 00071 int maxipa, 00072 char **haddr, 00073 char **hname, 00074 char **errtxt=0); 00075 00076 // getHostName() returns the fully qualified name of a host. If no partial 00077 // host name is specified (or specifiied as 0), the fully 00078 // qualified name of this host is returned. The name is returned 00079 // as an strdup'd string which must be released using free(). 00080 // If errtxt is supplied, it is set to zero. 00081 // Upon failure, strdup("0.0.0.0") is returned and the error 00082 // text is placed in errtxt if an address is supplied. 00083 // 00084 static char *getHostName(const char *InetName=0, 00085 char **errtxt=0); 00086 00087 // getHostName() returns the primary name of the host associated with the IP 00088 // address InetAddr. If a translation is successful, the address 00089 // of an strdup'd null terminated name is returned (it must be 00090 // released using free()) and errtxt, of supplied, is set to 0. 00091 // Upon failure, the ascii text version of the address is 00092 // returned and the error text is placed in errtxt if an 00093 // address is supplied. 00094 // 00095 static char *getHostName(struct sockaddr &InetAddr, 00096 char **errtxt=0); 00097 00098 // getHostName() returns the names of the host associated with the IP address 00099 // InetAddr. The first name is the primary name of the host. 00100 // Upon success, the address of each null terminated name is 00101 // placed in InetName[i]. Up to maxipn names are returned. The 00102 // array must be large enough to hold maxipn entries, Each 00103 // name is returned as an strdup'd string, which must be 00104 // released using free(). Return values are: 00105 // 0 -> No names could be returned; the error text is placed 00106 // in errtxt if an address is supplied. 00107 // >0 -> Number of names returned. 00108 // 00109 static int getHostName(struct sockaddr &InetAddr, 00110 char *InetName[], 00111 int maxipn, 00112 char **errtxt=0); 00113 00114 // getPort() returns the port number of the service corresponding to the 00115 // supplied name and service type (i.e., "tcp" or "udp"). If the port 00116 // cannot be found, zero is returned and the error text is placed 00117 // in errtxt if an address is supplied. 00118 // 00119 static int getPort(const char *servname, 00120 const char *servtype, 00121 char **errtxt=0); 00122 00123 // getPort() variant returns the port number associated with the specified 00124 // file descriptor. If an error occurs, a negative errno is returned, 00125 // and errtxt is set if supplied. 00126 // 00127 static int getPort(int fd, char **errtxt=0); 00128 00129 // getProtoID() returns the protocol number associated with the protocol name 00130 // passed as a parameter. No failures can occur since TCP is 00131 // returned if the protocol cannot be found. 00132 // 00133 static int getProtoID(const char *pname); 00134 00135 // Host2Dest() returns a sockaddr structure suitable for socket operations 00136 // built from the "host:port" specified in InetName. It returns 00137 // 1 upon success and 0 upon failure with the reason placed in 00138 // errtxt, if as address is supplied. 00139 // 00140 static int Host2Dest(const char *InetName, 00141 struct sockaddr &DestAddr, 00142 char **errtxt=0); 00143 00144 // Host2IP() converts a host name passed in InetName to an IPV4 address, 00145 // returned in ipaddr (unless it is zero, in which only a conversion 00146 // check is performed). 1 is returned upon success, 0 upon failure. 00147 // 00148 static int Host2IP(const char *InetName, 00149 unsigned int *ipaddr=0); 00150 00151 // IP2String() converts an IPV4 version of the address to ascii dot notation 00152 // If port > 0 then the results is <ipaddr>:<port>. The return 00153 // value is the number of characters placed in the buffer. 00154 // 00155 static int IP2String(unsigned int ipaddr, int port, char *buff, int blen); 00156 00157 // IPAddr() returns the IPV4 version of the address in the address argument 00158 // 00159 static unsigned int IPAddr(struct sockaddr *InetAddr); 00160 00161 // isDomain() returns true if the domain portion of the hostname matches 00162 // the specified domain name. 00163 // 00164 static int isDomain(const char *Hostname, const char *Domname, int Domlen); 00165 00166 // isLoopback() returns true if the address in InetAddr is the loopback address. 00167 // This test is used to discover IP address spoofing in UDP packets. 00168 // 00169 static int isLoopback(struct sockaddr &InetAddr); 00170 00171 // isMatch() returns true if the HostName matches the host pattern HostPat. 00172 // Patterns are formed as {[<pfx>][*][<sfx>] | <name>+} 00173 // 00174 static int isMatch(const char *HostNme, char *HostPat); 00175 00176 // Peername() returns the strdupp'd string name (and optionally the address) of 00177 // the host associated with the socket passed as the first parameter. 00178 // The string must be released using free(). If the host cannot be 00179 // determined, 0 is returned and the error text is placed in errtxt 00180 // if an address is supplied. 00181 // 00182 static char *Peername( int snum, 00183 struct sockaddr *sap=0, 00184 char **errtxt=0); 00185 00186 // setPort() sets the port number InetAddr. If anyaddr is true,, InetAddr is 00187 // initialized to the network defined "any" IP address. 00188 // 00189 static void setPort(struct sockaddr &InetAddr, int port, int anyaddr=0); 00190 00191 XrdNetDNS() {} 00192 ~XrdNetDNS() {} 00193 00194 private: 00195 00196 static char *LowCase(char *str); 00197 static int setET(char **errtxt, int rc); 00198 static int setETni(char **errtxt, int rc); 00199 }; 00200 #endif