/* * ares_query_completed_cb() is the callback that ares will call when * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(), * when using ares, is completed either successfully or with failure. */ static void query_completed_cb(void *arg, /* (struct connectdata *) */ int status, #ifdef HAVE_CARES_CALLBACK_TIMEOUTS int timeouts, #endif struct hostent *hostent) { struct connectdata *conn = (struct connectdata *)arg; struct ResolverResults *res; #ifdef HAVE_CARES_CALLBACK_TIMEOUTS (void)timeouts; /* ignored */ #endif if(ARES_EDESTRUCTION == status) /* when this ares handle is getting destroyed, the 'arg' pointer may not be valid so only defer it when we know the 'status' says its fine! */ return; res = (struct ResolverResults *)conn->async.os_specific; res->num_pending--; if(CURL_ASYNC_SUCCESS == status) { Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port); if(ai) { compound_results(res, ai); } } /* A successful result overwrites any previous error */ if(res->last_status != ARES_SUCCESS) res->last_status = status; }
/* * Curl_getaddrinfo() - for Windows threading without ENABLE_IPV6. */ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, const char *hostname, int port, int *waitp) { struct hostent *h = NULL; struct SessionHandle *data = conn->data; in_addr_t in; *waitp = 0; /* don't wait, we act synchronously */ in = inet_addr(hostname); if (in != CURL_INADDR_NONE) /* This is a dotted IP address 123.123.123.123-style */ return Curl_ip2addr(in, hostname, port); /* fire up a new resolver thread! */ if (init_resolve_thread(conn, hostname, port, NULL)) { *waitp = TRUE; /* please wait for the response */ return NULL; } /* fall-back to blocking version */ infof(data, "init_resolve_thread() failed for %s; %s\n", hostname, Curl_strerror(conn, ERRNO)); h = gethostbyname(hostname); if (!h) { infof(data, "gethostbyname(2) failed for %s:%d; %s\n", hostname, port, Curl_strerror(conn, SOCKERRNO)); return NULL; } return Curl_he2ai(h, port); }
/* * Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter * together with a pointer to the string version of the address, and it * returns a Curl_addrinfo chain filled in correctly with information for this * address/host. * * The input parameters ARE NOT checked for validity but they are expected * to have been checked already when this is called. */ Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port) { Curl_addrinfo *ai; #if defined(VMS) && \ defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64) #pragma pointer_size save #pragma pointer_size short #pragma message disable PTRMISMATCH #endif struct hostent *h; struct in_addr *addrentry; struct namebuf buffer; struct namebuf *buf = &buffer; h = &buf->hostentry; h->h_addr_list = &buf->h_addr_list[0]; addrentry = &buf->addrentry; #ifdef _CRAYC /* On UNICOS, s_addr is a bit field and for some reason assigning to it * doesn't work. There must be a better fix than this ugly hack. */ memcpy(addrentry, &num, SIZEOF_in_addr); #else addrentry->s_addr = num; #endif h->h_addr_list[0] = (char*)addrentry; h->h_addr_list[1] = NULL; h->h_addrtype = AF_INET; h->h_length = sizeof(*addrentry); h->h_name = &buf->h_name[0]; h->h_aliases = NULL; /* Now store the dotted version of the address */ snprintf((char *)h->h_name, 16, "%s", hostname); #if defined(VMS) && \ defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64) #pragma pointer_size restore #pragma message enable PTRMISMATCH #endif ai = Curl_he2ai(h, port); return ai; }
static void ares_query_completed_cb(void *arg, /* (struct connectdata *) */ int status, #ifdef HAVE_CARES_CALLBACK_TIMEOUTS int timeouts, #endif struct hostent *hostent) { struct connectdata *conn = (struct connectdata *)arg; struct Curl_addrinfo * ai = NULL; #ifdef HAVE_CARES_CALLBACK_TIMEOUTS (void)timeouts; /* ignored */ #endif if (status == CURL_ASYNC_SUCCESS) { ai = Curl_he2ai(hostent, conn->async.port); } (void)Curl_addrinfo_callback(arg, status, ai); }
/* * Curl_getaddrinfo() - the ipv4 synchronous version. * * The original code to this function was from the Dancer source code, written * by Bjorn Reese, it has since been patched and modified considerably. * * gethostbyname_r() is the thread-safe version of the gethostbyname() * function. When we build for plain IPv4, we attempt to use this * function. There are _three_ different gethostbyname_r() versions, and we * detect which one this platform supports in the configure script and set up * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME * has the corresponding rules. This is primarily on *nix. Note that some unix * flavours have thread-safe versions of the plain gethostbyname() etc. * */ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, const char *hostname, int port, int *waitp) { #if defined(HAVE_GETHOSTBYNAME_R_3) int res; #endif Curl_addrinfo *ai = NULL; struct hostent *h = NULL; struct in_addr in; struct hostent *buf = NULL; #ifdef CURL_DISABLE_VERBOSE_STRINGS (void)conn; #endif *waitp = 0; /* don't wait, we act synchronously */ if(Curl_inet_pton(AF_INET, hostname, &in) > 0) /* This is a dotted IP address 123.123.123.123-style */ return Curl_ip2addr(AF_INET, &in, hostname, port); #if defined(HAVE_GETHOSTBYNAME_R) /* * gethostbyname_r() is the preferred resolve function for many platforms. * Since there are three different versions of it, the following code is * somewhat #ifdef-ridden. */ else { int h_errnop; buf = calloc(1, CURL_HOSTENT_SIZE); if(!buf) return NULL; /* major failure */ /* * The clearing of the buffer is a workaround for a gethostbyname_r bug in * qnx nto and it is also _required_ for some of these functions on some * platforms. */ #ifdef HAVE_GETHOSTBYNAME_R_5 /* Solaris, IRIX and more */ h = gethostbyname_r(hostname, (struct hostent *)buf, (char *)buf + sizeof(struct hostent), CURL_HOSTENT_SIZE - sizeof(struct hostent), &h_errnop); /* If the buffer is too small, it returns NULL and sets errno to * ERANGE. The errno is thread safe if this is compiled with * -D_REENTRANT as then the 'errno' variable is a macro defined to get * used properly for threads. */ if(h) { ; } else #endif /* HAVE_GETHOSTBYNAME_R_5 */ #ifdef HAVE_GETHOSTBYNAME_R_6 /* Linux */ (void)gethostbyname_r(hostname, (struct hostent *)buf, (char *)buf + sizeof(struct hostent), CURL_HOSTENT_SIZE - sizeof(struct hostent), &h, /* DIFFERENCE */ &h_errnop); /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a * sudden this function returns EAGAIN if the given buffer size is too * small. Previous versions are known to return ERANGE for the same * problem. * * This wouldn't be such a big problem if older versions wouldn't * sometimes return EAGAIN on a common failure case. Alas, we can't * assume that EAGAIN *or* ERANGE means ERANGE for any given version of * glibc. * * For now, we do that and thus we may call the function repeatedly and * fail for older glibc versions that return EAGAIN, until we run out of * buffer size (step_size grows beyond CURL_HOSTENT_SIZE). * * If anyone has a better fix, please tell us! * * ------------------------------------------------------------------- * * On October 23rd 2003, Dan C dug up more details on the mysteries of * gethostbyname_r() in glibc: * * In glibc 2.2.5 the interface is different (this has also been * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32 * (shipped/upgraded by Redhat 7.2) don't show this behavior! * * In this "buggy" version, the return code is -1 on error and 'errno' * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a * thread-safe variable. */ if(!h) /* failure */ #endif/* HAVE_GETHOSTBYNAME_R_6 */ #ifdef HAVE_GETHOSTBYNAME_R_3 /* AIX, Digital Unix/Tru64, HPUX 10, more? */ /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of * the plain fact that it does not return unique full buffers on each * call, but instead several of the pointers in the hostent structs will * point to the same actual data! This have the unfortunate down-side that * our caching system breaks down horribly. Luckily for us though, AIX 4.3 * and more recent versions have a "completely thread-safe"[*] libc where * all the data is stored in thread-specific memory areas making calls to * the plain old gethostbyname() work fine even for multi-threaded * programs. * * This AIX 4.3 or later detection is all made in the configure script. * * Troels Walsted Hansen helped us work this out on March 3rd, 2003. * * [*] = much later we've found out that it isn't at all "completely * thread-safe", but at least the gethostbyname() function is. */ if(CURL_HOSTENT_SIZE >= (sizeof(struct hostent)+sizeof(struct hostent_data))) { /* August 22nd, 2000: Albert Chin-A-Young brought an updated version * that should work! September 20: Richard Prescott worked on the buffer * size dilemma. */ res = gethostbyname_r(hostname, (struct hostent *)buf, (struct hostent_data *)((char *)buf + sizeof(struct hostent))); h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */ } else res = -1; /* failure, too smallish buffer size */ if(!res) { /* success */ h = buf; /* result expected in h */ /* This is the worst kind of the different gethostbyname_r() interfaces. * Since we don't know how big buffer this particular lookup required, * we can't realloc down the huge alloc without doing closer analysis of * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every * name lookup. Fixing this would require an extra malloc() and then * calling Curl_addrinfo_copy() that subsequent realloc()s down the new * memory area to the actually used amount. */ } else #endif /* HAVE_GETHOSTBYNAME_R_3 */ { infof(conn->data, "gethostbyname_r(2) failed for %s\n", hostname); h = NULL; /* set return code to NULL */ free(buf); } #else /* HAVE_GETHOSTBYNAME_R */ /* * Here is code for platforms that don't have gethostbyname_r() or for * which the gethostbyname() is the preferred() function. */ else { #if (defined(NETWARE) && !defined(__NOVELL_LIBC__)) h = gethostbyname((char*)hostname); #else h = gethostbyname(hostname); #endif if(!h) infof(conn->data, "gethostbyname(2) failed for %s\n", hostname); #endif /*HAVE_GETHOSTBYNAME_R */ } if(h) { ai = Curl_he2ai(h, port); if(buf) /* used a *_r() function */ free(buf); } return ai; }
/* * Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and * returns a pointer to the malloc()ed copy. You need to call free() on the * returned buffer when you're done with it. */ Curl_addrinfo *Curl_addrinfo_copy(void *org, int port) { struct hostent *orig = org; return Curl_he2ai(orig, port); }
Curl_addrinfo * Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port) { Curl_addrinfo *ai; #if defined(__VMS) && \ defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64) #pragma pointer_size save #pragma pointer_size short #pragma message disable PTRMISMATCH #endif struct hostent *h; struct namebuff *buf; char *addrentry; char *hoststr; size_t addrsize; DEBUGASSERT(inaddr && hostname); buf = malloc(sizeof(struct namebuff)); if(!buf) return NULL; hoststr = strdup(hostname); if(!hoststr) { free(buf); return NULL; } switch(af) { case AF_INET: addrsize = sizeof(struct in_addr); addrentry = (void *)&buf->addrentry.ina4; memcpy(addrentry, inaddr, sizeof(struct in_addr)); break; #ifdef ENABLE_IPV6 case AF_INET6: addrsize = sizeof(struct in6_addr); addrentry = (void *)&buf->addrentry.ina6; memcpy(addrentry, inaddr, sizeof(struct in6_addr)); break; #endif default: free(hoststr); free(buf); return NULL; } h = &buf->hostentry; h->h_name = hoststr; h->h_aliases = NULL; h->h_addrtype = (short)af; h->h_length = (short)addrsize; h->h_addr_list = &buf->h_addr_list[0]; h->h_addr_list[0] = addrentry; h->h_addr_list[1] = NULL; /* terminate list of entries */ #if defined(__VMS) && \ defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64) #pragma pointer_size restore #pragma message enable PTRMISMATCH #endif ai = Curl_he2ai(h, port); free(hoststr); free(buf); return ai; }
/* * ares_query_completed_cb() is the callback that ares will call when * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(), * when using ares, is completed either successfully or with failure. */ static void query_completed_cb(void *arg, /* (struct connectdata *) */ int status, #ifdef HAVE_CARES_CALLBACK_TIMEOUTS int timeouts, #endif struct hostent *hostent) { struct connectdata *conn = (struct connectdata *)arg; struct ResolverResults *res; #ifdef HAVE_CARES_CALLBACK_TIMEOUTS (void)timeouts; /* ignored */ #endif if(ARES_EDESTRUCTION == status) /* when this ares handle is getting destroyed, the 'arg' pointer may not be valid so only defer it when we know the 'status' says its fine! */ return; res = (struct ResolverResults *)conn->async.os_specific; if(res) { res->num_pending--; if(CURL_ASYNC_SUCCESS == status) { Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port); if(ai) { compound_results(res, ai); } } /* A successful result overwrites any previous error */ if(res->last_status != ARES_SUCCESS) res->last_status = status; /* If there are responses still pending, we presume they must be the complementary IPv4 or IPv6 lookups that we started in parallel in Curl_resolver_getaddrinfo() (for Happy Eyeballs). If we've got a "definitive" response from one of a set of parallel queries, we need to think about how long we're willing to wait for more responses. */ if(res->num_pending /* Only these c-ares status values count as "definitive" for these purposes. For example, ARES_ENODATA is what we expect when there is no IPv6 entry for a domain name, and that's not a reason to get more aggressive in our timeouts for the other response. Other errors are either a result of bad input (which should affect all parallel requests), local or network conditions, non-definitive server responses, or us cancelling the request. */ && (status == ARES_SUCCESS || status == ARES_ENOTFOUND)) { /* Right now, there can only be up to two parallel queries, so don't bother handling any other cases. */ DEBUGASSERT(res->num_pending == 1); /* It's possible that one of these parallel queries could succeed quickly, but the other could always fail or timeout (when we're talking to a pool of DNS servers that can only successfully resolve IPv4 address, for example). It's also possible that the other request could always just take longer because it needs more time or only the second DNS server can fulfill it successfully. But, to align with the philosophy of Happy Eyeballs, we don't want to wait _too_ long or users will think requests are slow when IPv6 lookups don't actually work (but IPv4 ones do). So, now that we have a usable answer (some IPv4 addresses, some IPv6 addresses, or "no such domain"), we start a timeout for the remaining pending responses. Even though it is typical that this resolved request came back quickly, that needn't be the case. It might be that this completing request didn't get a result from the first DNS server or even the first round of the whole DNS server pool. So it could already be quite some time after we issued the DNS queries in the first place. Without modifying c-ares, we can't know exactly where in its retry cycle we are. We could guess based on how much time has gone by, but it doesn't really matter. Happy Eyeballs tells us that, given usable information in hand, we simply don't want to wait "too much longer" after we get a result. We simply wait an additional amount of time equal to the default c-ares query timeout. That is enough time for a typical parallel response to arrive without being "too long". Even on a network where one of the two types of queries is failing or timing out constantly, this will usually mean we wait a total of the default c-ares timeout (5 seconds) plus the round trip time for the successful request, which seems bearable. The downside is that c-ares might race with us to issue one more retry just before we give up, but it seems better to "waste" that request instead of trying to guess the perfect timeout to prevent it. After all, we don't even know where in the c-ares retry cycle each request is. */ res->happy_eyeballs_dns_time = Curl_now(); Curl_expire( conn->data, HAPPY_EYEBALLS_DNS_TIMEOUT, EXPIRE_HAPPY_EYEBALLS_DNS); } } }