hostip4.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #ifdef HAVE_NETINET_IN_H
00026 #include <netinet/in.h>
00027 #endif
00028 #ifdef HAVE_NETDB_H
00029 #include <netdb.h>
00030 #endif
00031 #ifdef HAVE_ARPA_INET_H
00032 #include <arpa/inet.h>
00033 #endif
00034 #ifdef __VMS
00035 #include <in.h>
00036 #include <inet.h>
00037 #endif
00038 
00039 #ifdef HAVE_PROCESS_H
00040 #include <process.h>
00041 #endif
00042 
00043 #include "urldata.h"
00044 #include "sendf.h"
00045 #include "hostip.h"
00046 #include "hash.h"
00047 #include "share.h"
00048 #include "strerror.h"
00049 #include "url.h"
00050 #include "inet_pton.h"
00051 /* The last 3 #include files should be in this order */
00052 #include "curl_printf.h"
00053 #include "curl_memory.h"
00054 #include "memdebug.h"
00055 
00056 /***********************************************************************
00057  * Only for plain IPv4 builds
00058  **********************************************************************/
00059 #ifdef CURLRES_IPV4 /* plain IPv4 code coming up */
00060 /*
00061  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
00062  * been set and returns TRUE if they are OK.
00063  */
00064 bool Curl_ipvalid(struct connectdata *conn)
00065 {
00066   if(conn->ip_version == CURL_IPRESOLVE_V6)
00067     /* An IPv6 address was requested and we can't get/use one */
00068     return FALSE;
00069 
00070   return TRUE; /* OK, proceed */
00071 }
00072 
00073 #ifdef CURLRES_SYNCH
00074 
00075 /*
00076  * Curl_getaddrinfo() - the IPv4 synchronous version.
00077  *
00078  * The original code to this function was from the Dancer source code, written
00079  * by Bjorn Reese, it has since been patched and modified considerably.
00080  *
00081  * gethostbyname_r() is the thread-safe version of the gethostbyname()
00082  * function. When we build for plain IPv4, we attempt to use this
00083  * function. There are _three_ different gethostbyname_r() versions, and we
00084  * detect which one this platform supports in the configure script and set up
00085  * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
00086  * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
00087  * has the corresponding rules. This is primarily on *nix. Note that some unix
00088  * flavours have thread-safe versions of the plain gethostbyname() etc.
00089  *
00090  */
00091 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
00092                                 const char *hostname,
00093                                 int port,
00094                                 int *waitp)
00095 {
00096   Curl_addrinfo *ai = NULL;
00097 
00098 #ifdef CURL_DISABLE_VERBOSE_STRINGS
00099   (void)conn;
00100 #endif
00101 
00102   *waitp = 0; /* synchronous response only */
00103 
00104   ai = Curl_ipv4_resolve_r(hostname, port);
00105   if(!ai)
00106     infof(conn->data, "Curl_ipv4_resolve_r failed for %s\n", hostname);
00107 
00108   return ai;
00109 }
00110 #endif /* CURLRES_SYNCH */
00111 #endif /* CURLRES_IPV4 */
00112 
00113 #if defined(CURLRES_IPV4) && !defined(CURLRES_ARES)
00114 
00115 /*
00116  * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function.
00117  *
00118  * This is used for both synchronous and asynchronous resolver builds,
00119  * implying that only threadsafe code and function calls may be used.
00120  *
00121  */
00122 Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname,
00123                                    int port)
00124 {
00125 #if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3)
00126   int res;
00127 #endif
00128   Curl_addrinfo *ai = NULL;
00129   struct hostent *h = NULL;
00130   struct in_addr in;
00131   struct hostent *buf = NULL;
00132 
00133   if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
00134     /* This is a dotted IP address 123.123.123.123-style */
00135     return Curl_ip2addr(AF_INET, &in, hostname, port);
00136 
00137 #if defined(HAVE_GETADDRINFO_THREADSAFE)
00138   else {
00139     struct addrinfo hints;
00140     char sbuf[12];
00141     char *sbufptr = NULL;
00142 
00143     memset(&hints, 0, sizeof(hints));
00144     hints.ai_family = PF_INET;
00145     hints.ai_socktype = SOCK_STREAM;
00146     if(port) {
00147       snprintf(sbuf, sizeof(sbuf), "%d", port);
00148       sbufptr = sbuf;
00149     }
00150 
00151     (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai);
00152 
00153 #elif defined(HAVE_GETHOSTBYNAME_R)
00154   /*
00155    * gethostbyname_r() is the preferred resolve function for many platforms.
00156    * Since there are three different versions of it, the following code is
00157    * somewhat #ifdef-ridden.
00158    */
00159   else {
00160     int h_errnop;
00161 
00162     buf = calloc(1, CURL_HOSTENT_SIZE);
00163     if(!buf)
00164       return NULL; /* major failure */
00165     /*
00166      * The clearing of the buffer is a workaround for a gethostbyname_r bug in
00167      * qnx nto and it is also _required_ for some of these functions on some
00168      * platforms.
00169      */
00170 
00171 #if defined(HAVE_GETHOSTBYNAME_R_5)
00172     /* Solaris, IRIX and more */
00173     h = gethostbyname_r(hostname,
00174                         (struct hostent *)buf,
00175                         (char *)buf + sizeof(struct hostent),
00176                         CURL_HOSTENT_SIZE - sizeof(struct hostent),
00177                         &h_errnop);
00178 
00179     /* If the buffer is too small, it returns NULL and sets errno to
00180      * ERANGE. The errno is thread safe if this is compiled with
00181      * -D_REENTRANT as then the 'errno' variable is a macro defined to get
00182      * used properly for threads.
00183      */
00184 
00185     if(h) {
00186       ;
00187     }
00188     else
00189 #elif defined(HAVE_GETHOSTBYNAME_R_6)
00190     /* Linux */
00191 
00192     (void)gethostbyname_r(hostname,
00193                         (struct hostent *)buf,
00194                         (char *)buf + sizeof(struct hostent),
00195                         CURL_HOSTENT_SIZE - sizeof(struct hostent),
00196                         &h, /* DIFFERENCE */
00197                         &h_errnop);
00198     /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
00199      * sudden this function returns EAGAIN if the given buffer size is too
00200      * small. Previous versions are known to return ERANGE for the same
00201      * problem.
00202      *
00203      * This wouldn't be such a big problem if older versions wouldn't
00204      * sometimes return EAGAIN on a common failure case. Alas, we can't
00205      * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
00206      * glibc.
00207      *
00208      * For now, we do that and thus we may call the function repeatedly and
00209      * fail for older glibc versions that return EAGAIN, until we run out of
00210      * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
00211      *
00212      * If anyone has a better fix, please tell us!
00213      *
00214      * -------------------------------------------------------------------
00215      *
00216      * On October 23rd 2003, Dan C dug up more details on the mysteries of
00217      * gethostbyname_r() in glibc:
00218      *
00219      * In glibc 2.2.5 the interface is different (this has also been
00220      * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
00221      * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
00222      * (shipped/upgraded by Redhat 7.2) don't show this behavior!
00223      *
00224      * In this "buggy" version, the return code is -1 on error and 'errno'
00225      * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
00226      * thread-safe variable.
00227      */
00228 
00229     if(!h) /* failure */
00230 #elif defined(HAVE_GETHOSTBYNAME_R_3)
00231     /* AIX, Digital Unix/Tru64, HPUX 10, more? */
00232 
00233     /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
00234      * the plain fact that it does not return unique full buffers on each
00235      * call, but instead several of the pointers in the hostent structs will
00236      * point to the same actual data! This have the unfortunate down-side that
00237      * our caching system breaks down horribly. Luckily for us though, AIX 4.3
00238      * and more recent versions have a "completely thread-safe"[*] libc where
00239      * all the data is stored in thread-specific memory areas making calls to
00240      * the plain old gethostbyname() work fine even for multi-threaded
00241      * programs.
00242      *
00243      * This AIX 4.3 or later detection is all made in the configure script.
00244      *
00245      * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
00246      *
00247      * [*] = much later we've found out that it isn't at all "completely
00248      * thread-safe", but at least the gethostbyname() function is.
00249      */
00250 
00251     if(CURL_HOSTENT_SIZE >=
00252        (sizeof(struct hostent)+sizeof(struct hostent_data))) {
00253 
00254       /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
00255        * that should work! September 20: Richard Prescott worked on the buffer
00256        * size dilemma.
00257        */
00258 
00259       res = gethostbyname_r(hostname,
00260                             (struct hostent *)buf,
00261                             (struct hostent_data *)((char *)buf +
00262                                                     sizeof(struct hostent)));
00263       h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
00264     }
00265     else
00266       res = -1; /* failure, too smallish buffer size */
00267 
00268     if(!res) { /* success */
00269 
00270       h = buf; /* result expected in h */
00271 
00272       /* This is the worst kind of the different gethostbyname_r() interfaces.
00273        * Since we don't know how big buffer this particular lookup required,
00274        * we can't realloc down the huge alloc without doing closer analysis of
00275        * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
00276        * name lookup. Fixing this would require an extra malloc() and then
00277        * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
00278        * memory area to the actually used amount.
00279        */
00280     }
00281     else
00282 #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */
00283     {
00284       h = NULL; /* set return code to NULL */
00285       free(buf);
00286     }
00287 #else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
00288     /*
00289      * Here is code for platforms that don't have a thread safe
00290      * getaddrinfo() nor gethostbyname_r() function or for which
00291      * gethostbyname() is the preferred one.
00292      */
00293   else {
00294     h = gethostbyname((void *)hostname);
00295 #endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
00296   }
00297 
00298   if(h) {
00299     ai = Curl_he2ai(h, port);
00300 
00301     if(buf) /* used a *_r() function */
00302       free(buf);
00303   }
00304 
00305   return ai;
00306 }
00307 #endif /* defined(CURLRES_IPV4) && !defined(CURLRES_ARES) */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04