XrdSysDNS.hh

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

Generated on 16 Jan 2014 for xrootd by  doxygen 1.4.7