summaryrefslogtreecommitdiff
path: root/libs/libcurl/src/asyn-ares.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/libcurl/src/asyn-ares.c')
-rw-r--r--libs/libcurl/src/asyn-ares.c851
1 files changed, 427 insertions, 424 deletions
diff --git a/libs/libcurl/src/asyn-ares.c b/libs/libcurl/src/asyn-ares.c
index 1ce5d78a4f..e97810c1b9 100644
--- a/libs/libcurl/src/asyn-ares.c
+++ b/libs/libcurl/src/asyn-ares.c
@@ -24,14 +24,14 @@
#include "curl_setup.h"
+#ifdef CURLRES_ARES
+
/***********************************************************************
* Only for ares-enabled builds
* And only for functions that fulfill the asynch resolver backend API
* as defined in asyn.h, nothing else belongs in this file!
**********************************************************************/
-#ifdef CURLRES_ARES
-
#include <limits.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
@@ -54,25 +54,18 @@
#include "share.h"
#include "url.h"
#include "multiif.h"
-#include "inet_pton.h"
+#include "curlx/inet_pton.h"
#include "connect.h"
#include "select.h"
#include "progress.h"
-#include "timediff.h"
+#include "curlx/timediff.h"
+#include "httpsrr.h"
+#include "strdup.h"
-#if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
- defined(_WIN32)
-# define CARES_STATICLIB
-#endif
#include <ares.h>
#include <ares_version.h> /* really old c-ares did not include this by
itself */
-#if ARES_VERSION >= 0x010500
-/* c-ares 1.5.0 or later, the callback proto is modified */
-#define HAVE_CARES_CALLBACK_TIMEOUTS 1
-#endif
-
#if ARES_VERSION >= 0x010601
/* IPv6 supported since 1.6.1 */
#define HAVE_CARES_IPV6 1
@@ -93,22 +86,18 @@
#define HAVE_CARES_GETADDRINFO 1
#endif
+#ifdef USE_HTTPSRR
+#if ARES_VERSION < 0x011c00
+#error "requires c-ares 1.28.0 or newer for HTTPSRR"
+#endif
+#define HTTPSRR_WORKS
+#endif
+
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
-struct thread_data {
- int num_pending; /* number of outstanding c-ares requests */
- struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares
- parts */
- int last_status;
-#ifndef HAVE_CARES_GETADDRINFO
- struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */
-#endif
- char hostname[1];
-};
-
/* How long we are willing to wait for additional parallel responses after
obtaining a "definitive" one. For old c-ares without getaddrinfo.
@@ -124,12 +113,15 @@ struct thread_data {
static int ares_ver = 0;
+static CURLcode async_ares_set_dns_servers(struct Curl_easy *data,
+ bool reset_on_null);
+
/*
- * Curl_resolver_global_init() - the generic low-level asynchronous name
+ * Curl_async_global_init() - the generic low-level asynchronous name
* resolve API. Called from curl_global_init() to initialize global resolver
* environment. Initializes ares library.
*/
-int Curl_resolver_global_init(void)
+int Curl_async_global_init(void)
{
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
if(ares_library_init(ARES_LIB_INIT_ALL)) {
@@ -141,12 +133,12 @@ int Curl_resolver_global_init(void)
}
/*
- * Curl_resolver_global_cleanup()
+ * Curl_async_global_cleanup()
*
* Called from curl_global_cleanup() to destroy global resolver environment.
* Deinitializes ares library.
*/
-void Curl_resolver_global_cleanup(void)
+void Curl_async_global_cleanup(void)
{
#ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
ares_library_cleanup();
@@ -160,25 +152,22 @@ static void sock_state_cb(void *data, ares_socket_t socket_fd,
struct Curl_easy *easy = data;
if(!readable && !writable) {
DEBUGASSERT(easy);
- Curl_multi_closed(easy, socket_fd);
+ Curl_multi_will_close(easy, socket_fd);
}
}
-/*
- * Curl_resolver_init()
- *
- * Called from curl_easy_init() -> Curl_open() to initialize resolver
- * URL-state specific environment ('resolver' member of the UrlState
- * structure). Fills the passed pointer by the initialized ares_channel.
- */
-CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
+static CURLcode async_ares_init(struct Curl_easy *data)
{
+ struct async_ares_ctx *ares = &data->state.async.ares;
int status;
struct ares_options options;
int optmask = ARES_OPT_SOCK_STATE_CB;
+ CURLcode rc = CURLE_OK;
+
options.sock_state_cb = sock_state_cb;
- options.sock_state_cb_data = easy;
+ options.sock_state_cb_data = data;
+ DEBUGASSERT(!ares->channel);
/*
if c ares < 1.20.0: curl set timeout to CARES_TIMEOUT_PER_ATTEMPT (2s)
@@ -194,240 +183,193 @@ CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
optmask |= ARES_OPT_TIMEOUTMS;
}
- status = ares_init_options((ares_channel*)resolver, &options, optmask);
+ status = ares_init_options(&ares->channel, &options, optmask);
if(status != ARES_SUCCESS) {
- if(status == ARES_ENOMEM)
- return CURLE_OUT_OF_MEMORY;
- else
- return CURLE_FAILED_INIT;
+ ares->channel = NULL;
+ rc = (status == ARES_ENOMEM) ?
+ CURLE_OUT_OF_MEMORY : CURLE_FAILED_INIT;
+ goto out;
}
- return CURLE_OK;
- /* make sure that all other returns from this function should destroy the
- ares channel before returning error! */
+
+ rc = async_ares_set_dns_servers(data, FALSE);
+ if(rc && rc != CURLE_NOT_BUILT_IN)
+ goto out;
+
+ rc = Curl_async_ares_set_dns_interface(data);
+ if(rc && rc != CURLE_NOT_BUILT_IN)
+ goto out;
+
+ rc = Curl_async_ares_set_dns_local_ip4(data);
+ if(rc && rc != CURLE_NOT_BUILT_IN)
+ goto out;
+
+ rc = Curl_async_ares_set_dns_local_ip6(data);
+ if(rc && rc != CURLE_NOT_BUILT_IN)
+ goto out;
+
+ rc = CURLE_OK;
+
+out:
+ if(rc && ares->channel) {
+ ares_destroy(ares->channel);
+ ares->channel = NULL;
+ }
+ return rc;
}
-/*
- * Curl_resolver_cleanup()
- *
- * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
- * URL-state specific environment ('resolver' member of the UrlState
- * structure). Destroys the ares channel.
- */
-void Curl_resolver_cleanup(void *resolver)
+static CURLcode async_ares_init_lazy(struct Curl_easy *data)
{
- ares_destroy((ares_channel)resolver);
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ if(!ares->channel)
+ return async_ares_init(data);
+ return CURLE_OK;
}
-/*
- * Curl_resolver_duphandle()
- *
- * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
- * environment ('resolver' member of the UrlState structure). Duplicates the
- * 'from' ares channel and passes the resulting channel to the 'to' pointer.
- */
-CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
+CURLcode Curl_async_get_impl(struct Curl_easy *data, void **impl)
{
- (void)from;
- /*
- * it would be better to call ares_dup instead, but right now
- * it is not possible to set 'sock_state_cb_data' outside of
- * ares_init_options
- */
- return Curl_resolver_init(easy, to);
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ CURLcode result = CURLE_OK;
+ if(!ares->channel) {
+ result = async_ares_init(data);
+ }
+ *impl = ares->channel;
+ return result;
}
-static void destroy_async_data(struct Curl_async *async);
+static void async_ares_cleanup(struct Curl_easy *data);
-/*
- * Cancel all possibly still on-going resolves for this connection.
- */
-void Curl_resolver_cancel(struct Curl_easy *data)
+void Curl_async_ares_shutdown(struct Curl_easy *data)
{
- DEBUGASSERT(data);
- if(data->state.async.resolver)
- ares_cancel((ares_channel)data->state.async.resolver);
- destroy_async_data(&data->state.async);
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ if(ares->channel)
+ ares_cancel(ares->channel);
+ async_ares_cleanup(data);
}
-/*
- * We are equivalent to Curl_resolver_cancel() for the c-ares resolver. We
- * never block.
- */
-void Curl_resolver_kill(struct Curl_easy *data)
+void Curl_async_ares_destroy(struct Curl_easy *data)
{
- /* We do not need to check the resolver state because we can be called safely
- at any time and we always do the same thing. */
- Curl_resolver_cancel(data);
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ Curl_async_ares_shutdown(data);
+ if(ares->channel) {
+ ares_destroy(ares->channel);
+ ares->channel = NULL;
+ }
}
/*
- * destroy_async_data() cleans up async resolver data.
+ * async_ares_cleanup() cleans up async resolver data.
*/
-static void destroy_async_data(struct Curl_async *async)
+static void async_ares_cleanup(struct Curl_easy *data)
{
- if(async->tdata) {
- struct thread_data *res = async->tdata;
- if(res) {
- if(res->temp_ai) {
- Curl_freeaddrinfo(res->temp_ai);
- res->temp_ai = NULL;
- }
- free(res);
- }
- async->tdata = NULL;
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ if(ares->temp_ai) {
+ Curl_freeaddrinfo(ares->temp_ai);
+ ares->temp_ai = NULL;
}
+#ifdef USE_HTTPSRR
+ Curl_httpsrr_cleanup(&ares->hinfo);
+#endif
}
/*
- * Curl_resolver_getsock() is called when someone from the outside world
- * (using curl_multi_fdset()) wants to get our fd_set setup and we are talking
- * with ares. The caller must make sure that this function is only called when
- * we have a working ares channel.
- *
- * Returns: sockets-in-use-bitmap
+ * Curl_async_getsock() is called when someone from the outside world
+ * (using curl_multi_fdset()) wants to get our fd_set setup.
*/
-int Curl_resolver_getsock(struct Curl_easy *data,
- curl_socket_t *socks)
+int Curl_async_getsock(struct Curl_easy *data, curl_socket_t *socks)
{
- struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 };
- struct timeval timebuf;
- int max = ares_getsock((ares_channel)data->state.async.resolver,
- (ares_socket_t *)socks, MAX_SOCKSPEREASYHANDLE);
- struct timeval *timeout =
- ares_timeout((ares_channel)data->state.async.resolver, &maxtime, &timebuf);
- timediff_t milli = curlx_tvtoms(timeout);
- Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
- return max;
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ DEBUGASSERT(ares->channel);
+ return Curl_ares_getsock(data, ares->channel, socks);
}
/*
- * waitperform()
- *
- * 1) Ask ares what sockets it currently plays with, then
- * 2) wait for the timeout period to check for action on ares' sockets.
- * 3) tell ares to act on all the sockets marked as "with action"
- *
- * return number of sockets it worked on, or -1 on error
- */
-
-static int waitperform(struct Curl_easy *data, timediff_t timeout_ms)
-{
- int nfds;
- int bitmask;
- ares_socket_t socks[ARES_GETSOCK_MAXNUM];
- struct pollfd pfd[ARES_GETSOCK_MAXNUM];
- int i;
- int num = 0;
-
- bitmask = ares_getsock((ares_channel)data->state.async.resolver, socks,
- ARES_GETSOCK_MAXNUM);
-
- for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
- pfd[i].events = 0;
- pfd[i].revents = 0;
- if(ARES_GETSOCK_READABLE(bitmask, i)) {
- pfd[i].fd = socks[i];
- pfd[i].events |= POLLRDNORM|POLLIN;
- }
- if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
- pfd[i].fd = socks[i];
- pfd[i].events |= POLLWRNORM|POLLOUT;
- }
- if(pfd[i].events)
- num++;
- else
- break;
- }
-
- if(num) {
- nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
- if(nfds < 0)
- return -1;
- }
- else
- nfds = 0;
-
- if(!nfds)
- /* Call ares_process() unconditionally here, even if we simply timed out
- above, as otherwise the ares name resolve will not timeout! */
- ares_process_fd((ares_channel)data->state.async.resolver, ARES_SOCKET_BAD,
- ARES_SOCKET_BAD);
- else {
- /* move through the descriptors and ask for processing on them */
- for(i = 0; i < num; i++)
- ares_process_fd((ares_channel)data->state.async.resolver,
- (pfd[i].revents & (POLLRDNORM|POLLIN)) ?
- pfd[i].fd : ARES_SOCKET_BAD,
- (pfd[i].revents & (POLLWRNORM|POLLOUT)) ?
- pfd[i].fd : ARES_SOCKET_BAD);
- }
- return nfds;
-}
-
-/*
- * Curl_resolver_is_resolved() is called repeatedly to check if a previous
+ * Curl_async_is_resolved() is called repeatedly to check if a previous
* name resolve request has completed. It should also make sure to time-out if
* the operation seems to take too long.
*
* Returns normal CURLcode errors.
*/
-CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
- struct Curl_dns_entry **dns)
+CURLcode Curl_async_is_resolved(struct Curl_easy *data,
+ struct Curl_dns_entry **dns)
{
- struct thread_data *res = data->state.async.tdata;
+ struct async_ares_ctx *ares = &data->state.async.ares;
CURLcode result = CURLE_OK;
DEBUGASSERT(dns);
*dns = NULL;
- if(waitperform(data, 0) < 0)
+ if(data->state.async.done) {
+ *dns = data->state.async.dns;
+ return CURLE_OK;
+ }
+
+ if(Curl_ares_perform(ares->channel, 0) < 0)
return CURLE_UNRECOVERABLE_POLL;
#ifndef HAVE_CARES_GETADDRINFO
/* Now that we have checked for any last minute results above, see if there
are any responses still pending when the EXPIRE_HAPPY_EYEBALLS_DNS timer
expires. */
- if(res
- && res->num_pending
+ if(ares->num_pending
/* This is only set to non-zero if the timer was started. */
- && (res->happy_eyeballs_dns_time.tv_sec
- || res->happy_eyeballs_dns_time.tv_usec)
- && (Curl_timediff(Curl_now(), res->happy_eyeballs_dns_time)
+ && (ares->happy_eyeballs_dns_time.tv_sec
+ || ares->happy_eyeballs_dns_time.tv_usec)
+ && (curlx_timediff(curlx_now(), ares->happy_eyeballs_dns_time)
>= HAPPY_EYEBALLS_DNS_TIMEOUT)) {
/* Remember that the EXPIRE_HAPPY_EYEBALLS_DNS timer is no longer
running. */
- memset(
- &res->happy_eyeballs_dns_time, 0, sizeof(res->happy_eyeballs_dns_time));
+ memset(&ares->happy_eyeballs_dns_time, 0,
+ sizeof(ares->happy_eyeballs_dns_time));
/* Cancel the raw c-ares request, which will fire query_completed_cb() with
ARES_ECANCELLED synchronously for all pending responses. This will
leave us with res->num_pending == 0, which is perfect for the next
block. */
- ares_cancel((ares_channel)data->state.async.resolver);
- DEBUGASSERT(res->num_pending == 0);
+ ares_cancel(ares->channel);
+ DEBUGASSERT(ares->num_pending == 0);
}
#endif
- if(res && !res->num_pending) {
- (void)Curl_addrinfo_callback(data, res->last_status, res->temp_ai);
- /* temp_ai ownership is moved to the connection, so we need not free-up
- them */
- res->temp_ai = NULL;
-
- if(!data->state.async.dns)
+ if(!ares->num_pending) {
+ /* all c-ares operations done, what is the result to report? */
+ Curl_resolv_unlink(data, &data->state.async.dns);
+ data->state.async.done = TRUE;
+ result = ares->result;
+ if(ares->last_status == CURL_ASYNC_SUCCESS && !result) {
+ data->state.async.dns =
+ Curl_dnscache_mk_entry(data, ares->temp_ai,
+ data->state.async.hostname, 0,
+ data->state.async.port, FALSE);
+ ares->temp_ai = NULL; /* temp_ai now owned by entry */
+#ifdef HTTPSRR_WORKS
+ if(data->state.async.dns) {
+ struct Curl_https_rrinfo *lhrr = Curl_httpsrr_dup_move(&ares->hinfo);
+ if(!lhrr)
+ result = CURLE_OUT_OF_MEMORY;
+ else
+ data->state.async.dns->hinfo = lhrr;
+ }
+#endif
+ if(!result && data->state.async.dns)
+ result = Curl_dnscache_add(data, data->state.async.dns);
+ }
+ /* if we have not found anything, report the proper
+ * CURLE_COULDNT_RESOLVE_* code */
+ if(!result && !data->state.async.dns)
result = Curl_resolver_error(data);
- else
- *dns = data->state.async.dns;
-
- destroy_async_data(&data->state.async);
+ if(result)
+ Curl_resolv_unlink(data, &data->state.async.dns);
+ *dns = data->state.async.dns;
+ CURL_TRC_DNS(data, "is_resolved() result=%d, dns=%sfound",
+ result, *dns ? "" : "not ");
+ async_ares_cleanup(data);
}
-
return result;
}
/*
- * Curl_resolver_wait_resolv()
+ * Curl_async_await()
*
* Waits for a resolve to finish. This function should be avoided since using
* this risk getting the multi interface to "hang".
@@ -437,12 +379,13 @@ CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
* CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
*/
-CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
- struct Curl_dns_entry **entry)
+CURLcode Curl_async_await(struct Curl_easy *data,
+ struct Curl_dns_entry **entry)
{
+ struct async_ares_ctx *ares = &data->state.async.ares;
CURLcode result = CURLE_OK;
timediff_t timeout;
- struct curltime now = Curl_now();
+ struct curltime now = curlx_now();
DEBUGASSERT(entry);
*entry = NULL; /* clear on entry */
@@ -471,7 +414,7 @@ CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
store.tv_sec = itimeout/1000;
store.tv_usec = (itimeout%1000)*1000;
- tvp = ares_timeout((ares_channel)data->state.async.resolver, &store, &tv);
+ tvp = ares_timeout(ares->channel, &store, &tv);
/* use the timeout period ares returned to us above if less than one
second is left, otherwise just use 1000ms to make sure the progress
@@ -481,18 +424,18 @@ CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
else
timeout_ms = 1000;
- if(waitperform(data, timeout_ms) < 0)
+ if(Curl_ares_perform(ares->channel, timeout_ms) < 0)
return CURLE_UNRECOVERABLE_POLL;
- result = Curl_resolver_is_resolved(data, entry);
+ result = Curl_async_is_resolved(data, entry);
if(result || data->state.async.done)
break;
if(Curl_pgrsUpdate(data))
result = CURLE_ABORTED_BY_CALLBACK;
else {
- struct curltime now2 = Curl_now();
- timediff_t timediff = Curl_timediff(now2, now); /* spent time */
+ struct curltime now2 = curlx_now();
+ timediff_t timediff = curlx_timediff(now2, now); /* spent time */
if(timediff <= 0)
timeout -= 1; /* always deduct at least 1 */
else if(timediff > timeout)
@@ -504,163 +447,155 @@ CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
if(timeout < 0)
result = CURLE_OPERATION_TIMEDOUT;
}
- if(result)
- /* failure, so we cancel the ares operation */
- ares_cancel((ares_channel)data->state.async.resolver);
/* Operation complete, if the lookup was successful we now have the entry
in the cache. */
+ data->state.async.done = TRUE;
if(entry)
*entry = data->state.async.dns;
if(result)
- /* close the connection, since we cannot return failure here without
- cleaning up this connection properly. */
- connclose(data->conn, "c-ares resolve failed");
-
+ ares_cancel(ares->channel);
return result;
}
#ifndef HAVE_CARES_GETADDRINFO
/* Connects results to the list */
-static void compound_results(struct thread_data *res,
- struct Curl_addrinfo *ai)
+static void async_addr_concat(struct Curl_addrinfo **pbase,
+ struct Curl_addrinfo *ai)
{
if(!ai)
return;
+ /* When adding `ai` to an existing address list, we prefer ipv6
+ * to be in front. */
#ifdef USE_IPV6 /* CURLRES_IPV6 */
- if(res->temp_ai && res->temp_ai->ai_family == PF_INET6) {
- /* We have results already, put the new IPv6 entries at the head of the
- list. */
- struct Curl_addrinfo *temp_ai_tail = res->temp_ai;
-
- while(temp_ai_tail->ai_next)
- temp_ai_tail = temp_ai_tail->ai_next;
-
- temp_ai_tail->ai_next = ai;
+ if(*pbase && (*pbase)->ai_family == PF_INET6) {
+ /* ipv6 already in front, append `ai` */
+ struct Curl_addrinfo *tail = *pbase;
+ while(tail->ai_next)
+ tail = tail->ai_next;
+ tail->ai_next = ai;
}
else
#endif /* CURLRES_IPV6 */
{
- /* Add the new results to the list of old results. */
- struct Curl_addrinfo *ai_tail = ai;
- while(ai_tail->ai_next)
- ai_tail = ai_tail->ai_next;
-
- ai_tail->ai_next = res->temp_ai;
- res->temp_ai = ai;
+ /* prepend to the (possibly) existing list. */
+ struct Curl_addrinfo *tail = ai;
+ while(tail->ai_next)
+ tail = tail->ai_next;
+ tail->ai_next = *pbase;
+ *pbase = 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.
+ * the host query initiated by ares_gethostbyname() from
+ * Curl_async_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)
+static void async_ares_hostbyname_cb(void *user_data,
+ int status,
+ int timeouts,
+ struct hostent *hostent)
{
- struct Curl_easy *data = (struct Curl_easy *)arg;
- struct thread_data *res;
+ struct Curl_easy *data = (struct Curl_easy *)user_data;
+ struct async_ares_ctx *ares = &data->state.async.ares;
-#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 = data->state.async.tdata;
- if(res) {
- res->num_pending--;
+ if(CURL_ASYNC_SUCCESS == status) {
+ ares->last_status = status; /* one success overrules any error */
+ async_addr_concat(&ares->temp_ai,
+ Curl_he2ai(hostent, data->state.async.port));
+ }
+ else if(ares->last_status != ARES_SUCCESS) {
+ /* no success so far, remember error */
+ ares->last_status = status;
+ }
- if(CURL_ASYNC_SUCCESS == status) {
- struct Curl_addrinfo *ai = Curl_he2ai(hostent, data->state.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 have got a
- "definitive" response from one of a set of parallel queries, we need to
- think about how long we are 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 is 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 do not
- bother handling any other cases. */
- DEBUGASSERT(res->num_pending == 1);
-
- /* it is possible that one of these parallel queries could succeed
- quickly, but the other could always fail or timeout (when we are
- talking to a pool of DNS servers that can only successfully resolve
- IPv4 address, for example).
-
- it is 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 do not want to wait _too_ long or users will think
- requests are slow when IPv6 lookups do not 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 did not 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 cannot know exactly
- where in its retry cycle we are. We could guess based on how much
- time has gone by, but it does not really matter. Happy Eyeballs tells
- us that, given usable information in hand, we simply do not 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 do not even know where in the
- c-ares retry cycle each request is.
- */
- res->happy_eyeballs_dns_time = Curl_now();
- Curl_expire(data, HAPPY_EYEBALLS_DNS_TIMEOUT,
- EXPIRE_HAPPY_EYEBALLS_DNS);
- }
+ ares->num_pending--;
+
+ CURL_TRC_DNS(data, "ares: hostbyname done, status=%d, pending=%d, "
+ "addr=%sfound",
+ status, ares->num_pending, ares->temp_ai ? "" : "not ");
+ /* If there are responses still pending, we presume they must be the
+ complementary IPv4 or IPv6 lookups that we started in parallel in
+ Curl_async_getaddrinfo() (for Happy Eyeballs). If we have got a
+ "definitive" response from one of a set of parallel queries, we need to
+ think about how long we are willing to wait for more responses. */
+ if(ares->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 is 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 do not
+ bother handling any other cases. */
+ DEBUGASSERT(ares->num_pending == 1);
+
+ /* it is possible that one of these parallel queries could succeed
+ quickly, but the other could always fail or timeout (when we are
+ talking to a pool of DNS servers that can only successfully resolve
+ IPv4 address, for example).
+
+ it is 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 do not want to wait _too_ long or users will think
+ requests are slow when IPv6 lookups do not 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 did not 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 cannot know exactly
+ where in its retry cycle we are. We could guess based on how much
+ time has gone by, but it does not really matter. Happy Eyeballs tells
+ us that, given usable information in hand, we simply do not 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 do not even know where in the
+ c-ares retry cycle each request is.
+ */
+ ares->happy_eyeballs_dns_time = curlx_now();
+ Curl_expire(data, HAPPY_EYEBALLS_DNS_TIMEOUT,
+ EXPIRE_HAPPY_EYEBALLS_DNS);
}
}
+
#else
/* c-ares 1.16.0 or later */
/*
- * ares2addr() converts an address list provided by c-ares to an internal
- * libcurl compatible list
+ * async_ares_node2addr() converts an address list provided by c-ares
+ * to an internal libcurl compatible list.
*/
-static struct Curl_addrinfo *ares2addr(struct ares_addrinfo_node *node)
+static struct Curl_addrinfo *
+async_ares_node2addr(struct ares_addrinfo_node *node)
{
/* traverse the ares_addrinfo_node list */
struct ares_addrinfo_node *ai;
@@ -730,129 +665,183 @@ static struct Curl_addrinfo *ares2addr(struct ares_addrinfo_node *node)
return cafirst;
}
-static void addrinfo_cb(void *arg, int status, int timeouts,
- struct ares_addrinfo *result)
+static void async_ares_addrinfo_cb(void *user_data, int status, int timeouts,
+ struct ares_addrinfo *result)
{
- struct Curl_easy *data = (struct Curl_easy *)arg;
- struct thread_data *res = data->state.async.tdata;
+ struct Curl_easy *data = (struct Curl_easy *)user_data;
+ struct async_ares_ctx *ares = &data->state.async.ares;
(void)timeouts;
+ CURL_TRC_DNS(data, "asyn-ares: addrinfo callback, status=%d", status);
if(ARES_SUCCESS == status) {
- res->temp_ai = ares2addr(result->nodes);
- res->last_status = CURL_ASYNC_SUCCESS;
+ ares->temp_ai = async_ares_node2addr(result->nodes);
+ ares->last_status = CURL_ASYNC_SUCCESS;
ares_freeaddrinfo(result);
}
- res->num_pending--;
+ ares->num_pending--;
+ CURL_TRC_DNS(data, "ares: addrinfo done, status=%d, pending=%d, "
+ "addr=%sfound",
+ status, ares->num_pending, ares->temp_ai ? "" : "not ");
}
#endif
+
+#ifdef USE_HTTPSRR
+static void async_ares_rr_done(void *user_data, ares_status_t status,
+ size_t timeouts,
+ const ares_dns_record_t *dnsrec)
+{
+ struct Curl_easy *data = user_data;
+ struct async_ares_ctx *ares = &data->state.async.ares;
+
+ (void)timeouts;
+ --ares->num_pending;
+ CURL_TRC_DNS(data, "ares: httpsrr done, status=%d, pending=%d, "
+ "dnsres=%sfound",
+ status, ares->num_pending,
+ (dnsrec &&
+ ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER)) ?
+ "" : "not ");
+ if((ARES_SUCCESS != status) || !dnsrec)
+ return;
+ ares->result = Curl_httpsrr_from_ares(data, dnsrec, &ares->hinfo);
+}
+#endif /* USE_HTTPSRR */
+
/*
- * Curl_resolver_getaddrinfo() - when using ares
+ * Curl_async_getaddrinfo() - when using ares
*
* Returns name information about the given hostname and port number. If
* successful, the 'hostent' is returned and the fourth argument will point to
* memory we need to free after use. That memory *MUST* be freed with
* Curl_freeaddrinfo(), nothing else.
*/
-struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
- const char *hostname,
- int port,
- int *waitp)
+struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,
+ const char *hostname,
+ int port,
+ int ip_version,
+ int *waitp)
{
- struct thread_data *res = NULL;
- size_t namelen = strlen(hostname);
+ struct async_ares_ctx *ares = &data->state.async.ares;
*waitp = 0; /* default to synchronous response */
- res = calloc(1, sizeof(struct thread_data) + namelen);
- if(res) {
- strcpy(res->hostname, hostname);
- data->state.async.hostname = res->hostname;
- data->state.async.port = port;
- data->state.async.done = FALSE; /* not done */
- data->state.async.status = 0; /* clear */
- data->state.async.dns = NULL; /* clear */
- data->state.async.tdata = res;
+ if(async_ares_init_lazy(data))
+ return NULL;
- /* initial status - failed */
- res->last_status = ARES_ENOTFOUND;
+ data->state.async.done = FALSE; /* not done */
+ data->state.async.dns = NULL; /* clear */
+ data->state.async.port = port;
+ data->state.async.ip_version = ip_version;
+ data->state.async.hostname = strdup(hostname);
+ if(!data->state.async.hostname)
+ return NULL;
+
+ /* initial status - failed */
+ ares->last_status = ARES_ENOTFOUND;
#ifdef HAVE_CARES_GETADDRINFO
- {
- struct ares_addrinfo_hints hints;
- char service[12];
- int pf = PF_INET;
- memset(&hints, 0, sizeof(hints));
+ {
+ struct ares_addrinfo_hints hints;
+ char service[12];
+ int pf = PF_INET;
+ memset(&hints, 0, sizeof(hints));
#ifdef CURLRES_IPV6
- if((data->conn->ip_version != CURL_IPRESOLVE_V4) &&
- Curl_ipv6works(data)) {
- /* The stack seems to be IPv6-enabled */
- if(data->conn->ip_version == CURL_IPRESOLVE_V6)
- pf = PF_INET6;
- else
- pf = PF_UNSPEC;
- }
-#endif /* CURLRES_IPV6 */
- hints.ai_family = pf;
- hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
- SOCK_STREAM : SOCK_DGRAM;
- /* Since the service is a numerical one, set the hint flags
- * accordingly to save a call to getservbyname in inside C-Ares
- */
- hints.ai_flags = ARES_AI_NUMERICSERV;
- msnprintf(service, sizeof(service), "%d", port);
- res->num_pending = 1;
- ares_getaddrinfo((ares_channel)data->state.async.resolver, hostname,
- service, &hints, addrinfo_cb, data);
+ if((ip_version != CURL_IPRESOLVE_V4) &&
+ Curl_ipv6works(data)) {
+ /* The stack seems to be IPv6-enabled */
+ if(ip_version == CURL_IPRESOLVE_V6)
+ pf = PF_INET6;
+ else
+ pf = PF_UNSPEC;
}
+#endif /* CURLRES_IPV6 */
+ CURL_TRC_DNS(data, "asyn-ares: fire off getaddrinfo for %s",
+ (pf == PF_UNSPEC) ? "A+AAAA" :
+ ((pf == PF_INET) ? "A" : "AAAA"));
+ hints.ai_family = pf;
+ hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
+ SOCK_STREAM : SOCK_DGRAM;
+ /* Since the service is a numerical one, set the hint flags
+ * accordingly to save a call to getservbyname in inside C-Ares
+ */
+ hints.ai_flags = ARES_AI_NUMERICSERV;
+ msnprintf(service, sizeof(service), "%d", port);
+ ares->num_pending = 1;
+ ares_getaddrinfo(ares->channel, data->state.async.hostname,
+ service, &hints, async_ares_addrinfo_cb, data);
+ }
#else
#ifdef HAVE_CARES_IPV6
- if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
- /* The stack seems to be IPv6-enabled */
- res->num_pending = 2;
-
- /* areschannel is already setup in the Curl_open() function */
- ares_gethostbyname((ares_channel)data->state.async.resolver, hostname,
- PF_INET, query_completed_cb, data);
- ares_gethostbyname((ares_channel)data->state.async.resolver, hostname,
- PF_INET6, query_completed_cb, data);
- }
- else
+ if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
+ /* The stack seems to be IPv6-enabled */
+ /* areschannel is already setup in the Curl_open() function */
+ CURL_TRC_DNS(data, "asyn-ares: fire off query for A");
+ ares_gethostbyname(ares->channel, hostname, PF_INET,
+ async_ares_hostbyname_cb, data);
+ CURL_TRC_DNS(data, "asyn-ares: fire off query for AAAA");
+ ares->num_pending = 2;
+ ares_gethostbyname(ares->channel, data->state.async.hostname, PF_INET6,
+ async_ares_hostbyname_cb, data);
+ }
+ else
#endif
- {
- res->num_pending = 1;
-
- /* areschannel is already setup in the Curl_open() function */
- ares_gethostbyname((ares_channel)data->state.async.resolver,
- hostname, PF_INET,
- query_completed_cb, data);
- }
+ {
+ /* areschannel is already setup in the Curl_open() function */
+ CURL_TRC_DNS(data, "asyn-ares: fire off query for A");
+ ares->num_pending = 1;
+ ares_gethostbyname(ares->channel, data->state.async.hostname, PF_INET,
+ async_ares_hostbyname_cb, data);
+ }
#endif
- *waitp = 1; /* expect asynchronous response */
+#ifdef USE_HTTPSRR
+ {
+ CURL_TRC_DNS(data, "asyn-ares: fire off query for HTTPSRR");
+ memset(&ares->hinfo, 0, sizeof(ares->hinfo));
+ ares->hinfo.port = -1;
+ ares->num_pending++; /* one more */
+ ares_query_dnsrec(ares->channel, data->state.async.hostname,
+ ARES_CLASS_IN, ARES_REC_TYPE_HTTPS,
+ async_ares_rr_done, data, NULL);
}
+#endif
+ *waitp = 1; /* expect asynchronous response */
+
return NULL; /* no struct yet */
}
-CURLcode Curl_set_dns_servers(struct Curl_easy *data,
- char *servers)
+/* Set what DNS server are is to use. This is called in 2 situations:
+ * 1. when the application does 'CURLOPT_DNS_SERVERS' and passing NULL
+ * means any previous set value should be unset. Which means
+ * we need to destroy and create the are channel anew, if there is one.
+ * 2. When we lazy init the ares channel and NULL means that there
+ * are no preferences and we do not reset any existing channel. */
+static CURLcode async_ares_set_dns_servers(struct Curl_easy *data,
+ bool reset_on_null)
{
+ struct async_ares_ctx *ares = &data->state.async.ares;
CURLcode result = CURLE_NOT_BUILT_IN;
- int ares_result;
-
- /* If server is NULL or empty, this would purge all DNS servers
- * from ares library, which will cause any and all queries to fail.
- * So, just return OK if none are configured and do not actually make
- * any changes to c-ares. This lets c-ares use its defaults, which
- * it gets from the OS (for instance from /etc/resolv.conf on Linux).
- */
- if(!(servers && servers[0]))
+ const char *servers = data->set.str[STRING_DNS_SERVERS];
+ int ares_result = ARES_SUCCESS;
+
+#if defined(CURLDEBUG) && defined(HAVE_CARES_SERVERS_CSV)
+ if(getenv("CURL_DNS_SERVER"))
+ servers = getenv("CURL_DNS_SERVER");
+#endif
+
+ if(!servers) {
+ if(reset_on_null) {
+ Curl_async_destroy(data);
+ }
return CURLE_OK;
+ }
#ifdef HAVE_CARES_SERVERS_CSV
+ /* if channel is not there, this is just a parameter check */
+ if(ares->channel)
#ifdef HAVE_CARES_PORTS_CSV
- ares_result = ares_set_servers_ports_csv(data->state.async.resolver,
- servers);
+ ares_result = ares_set_servers_ports_csv(ares->channel, servers);
#else
- ares_result = ares_set_servers_csv(data->state.async.resolver, servers);
+ ares_result = ares_set_servers_csv(ares->channel, servers);
#endif
switch(ares_result) {
case ARES_SUCCESS:
@@ -876,14 +865,23 @@ CURLcode Curl_set_dns_servers(struct Curl_easy *data,
return result;
}
-CURLcode Curl_set_dns_interface(struct Curl_easy *data,
- const char *interf)
+CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data)
+{
+ return async_ares_set_dns_servers(data, TRUE);
+}
+
+CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data)
{
#ifdef HAVE_CARES_LOCAL_DEV
+ struct async_ares_ctx *ares = &data->state.async.ares;
+ const char *interf = data->set.str[STRING_DNS_INTERFACE];
+
if(!interf)
interf = "";
- ares_set_local_dev((ares_channel)data->state.async.resolver, interf);
+ /* if channel is not there, this is just a parameter check */
+ if(ares->channel)
+ ares_set_local_dev(ares->channel, interf);
return CURLE_OK;
#else /* c-ares version too old! */
@@ -893,24 +891,26 @@ CURLcode Curl_set_dns_interface(struct Curl_easy *data,
#endif
}
-CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
- const char *local_ip4)
+CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data)
{
#ifdef HAVE_CARES_SET_LOCAL
+ struct async_ares_ctx *ares = &data->state.async.ares;
struct in_addr a4;
+ const char *local_ip4 = data->set.str[STRING_DNS_LOCAL_IP4];
if((!local_ip4) || (local_ip4[0] == 0)) {
a4.s_addr = 0; /* disabled: do not bind to a specific address */
}
else {
- if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
+ if(curlx_inet_pton(AF_INET, local_ip4, &a4) != 1) {
DEBUGF(infof(data, "bad DNS IPv4 address"));
return CURLE_BAD_FUNCTION_ARGUMENT;
}
}
- ares_set_local_ip4((ares_channel)data->state.async.resolver,
- ntohl(a4.s_addr));
+ /* if channel is not there yet, this is just a parameter check */
+ if(ares->channel)
+ ares_set_local_ip4(ares->channel, ntohl(a4.s_addr));
return CURLE_OK;
#else /* c-ares version too old! */
@@ -920,30 +920,33 @@ CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
#endif
}
-CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
- const char *local_ip6)
+CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data)
{
#if defined(HAVE_CARES_SET_LOCAL) && defined(USE_IPV6)
+ struct async_ares_ctx *ares = &data->state.async.ares;
unsigned char a6[INET6_ADDRSTRLEN];
+ const char *local_ip6 = data->set.str[STRING_DNS_LOCAL_IP6];
if((!local_ip6) || (local_ip6[0] == 0)) {
/* disabled: do not bind to a specific address */
memset(a6, 0, sizeof(a6));
}
else {
- if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
+ if(curlx_inet_pton(AF_INET6, local_ip6, a6) != 1) {
DEBUGF(infof(data, "bad DNS IPv6 address"));
return CURLE_BAD_FUNCTION_ARGUMENT;
}
}
- ares_set_local_ip6((ares_channel)data->state.async.resolver, a6);
+ /* if channel is not there, this is just a parameter check */
+ if(ares->channel)
+ ares_set_local_ip6(ares->channel, a6);
return CURLE_OK;
#else /* c-ares version too old! */
(void)data;
- (void)local_ip6;
return CURLE_NOT_BUILT_IN;
#endif
}
+
#endif /* CURLRES_ARES */