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