1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 /***********************************************************************
26 * Only for threaded name resolves builds
27 **********************************************************************/
28 #ifdef CURLRES_THREADED
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
44 #if defined(USE_THREADS_POSIX)
45 # ifdef HAVE_PTHREAD_H
48 #elif defined(USE_THREADS_WIN32)
49 # ifdef HAVE_PROCESS_H
54 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
56 #define in_addr_t unsigned long
59 #ifdef HAVE_GETADDRINFO
60 # define RESOLVER_ENOMEM EAI_MEMORY
62 # define RESOLVER_ENOMEM ENOMEM
73 #include "inet_pton.h"
74 #include "inet_ntop.h"
75 #include "curl_threads.h"
77 /* The last 3 #include files should be in this order */
78 #include "curl_printf.h"
79 #include "curl_memory.h"
83 struct curltime start;
87 * Curl_resolver_global_init()
88 * Called from curl_global_init() to initialize global resolver environment.
91 int Curl_resolver_global_init(void)
97 * Curl_resolver_global_cleanup()
98 * Called from curl_global_cleanup() to destroy global resolver environment.
101 void Curl_resolver_global_cleanup(void)
106 * Curl_resolver_init()
107 * Called from curl_easy_init() -> Curl_open() to initialize resolver
108 * URL-state specific environment ('resolver' member of the UrlState
111 CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
114 *resolver = calloc(1, sizeof(struct resdata));
116 return CURLE_OUT_OF_MEMORY;
121 * Curl_resolver_cleanup()
122 * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
123 * URL-state specific environment ('resolver' member of the UrlState
126 void Curl_resolver_cleanup(void *resolver)
132 * Curl_resolver_duphandle()
133 * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
134 * environment ('resolver' member of the UrlState structure).
136 CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
139 return Curl_resolver_init(easy, to);
142 static void destroy_async_data(struct Curl_async *);
145 * Cancel all possibly still on-going resolves for this connection.
147 void Curl_resolver_cancel(struct connectdata *conn)
149 destroy_async_data(&conn->async);
152 /* This function is used to init a threaded resolve */
153 static bool init_resolve_thread(struct connectdata *conn,
154 const char *hostname, int port,
155 const struct addrinfo *hints);
158 /* Data for synchronization between resolver thread and its parent */
159 struct thread_sync_data {
163 char *hostname; /* hostname to resolve, Curl_async.hostname
168 #ifdef HAVE_GETADDRINFO
169 struct addrinfo hints;
171 struct thread_data *td; /* for thread-self cleanup */
175 curl_thread_t thread_hnd;
176 unsigned int poll_interval;
178 struct thread_sync_data tsd;
181 static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
183 return &(((struct thread_data *)conn->async.os_specific)->tsd);
186 /* Destroy resolver thread synchronization data */
188 void destroy_thread_sync_data(struct thread_sync_data * tsd)
191 Curl_mutex_destroy(tsd->mtx);
198 Curl_freeaddrinfo(tsd->res);
200 memset(tsd, 0, sizeof(*tsd));
203 /* Initialize resolver thread synchronization data */
205 int init_thread_sync_data(struct thread_data * td,
206 const char *hostname,
208 const struct addrinfo *hints)
210 struct thread_sync_data *tsd = &td->tsd;
212 memset(tsd, 0, sizeof(*tsd));
216 /* Treat the request as done until the thread actually starts so any early
217 * cleanup gets done properly.
220 #ifdef HAVE_GETADDRINFO
227 tsd->mtx = malloc(sizeof(curl_mutex_t));
231 Curl_mutex_init(tsd->mtx);
233 tsd->sock_error = CURL_ASYNC_SUCCESS;
235 /* Copying hostname string because original can be destroyed by parent
236 * thread during gethostbyname execution.
238 tsd->hostname = strdup(hostname);
245 /* Memory allocation failed */
246 destroy_thread_sync_data(tsd);
250 static int getaddrinfo_complete(struct connectdata *conn)
252 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
255 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
256 /* The tsd->res structure has been copied to async.dns and perhaps the DNS
257 cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
265 #ifdef HAVE_GETADDRINFO
268 * getaddrinfo_thread() resolves a name and then exits.
270 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
273 static unsigned int CURL_STDCALL getaddrinfo_thread(void *arg)
275 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
276 struct thread_data *td = tsd->td;
280 snprintf(service, sizeof(service), "%d", tsd->port);
282 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
285 tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
286 if(tsd->sock_error == 0)
287 tsd->sock_error = RESOLVER_ENOMEM;
290 Curl_addrinfo_set_port(tsd->res, tsd->port);
293 Curl_mutex_acquire(tsd->mtx);
295 /* too late, gotta clean up the mess */
296 Curl_mutex_release(tsd->mtx);
297 destroy_thread_sync_data(tsd);
302 Curl_mutex_release(tsd->mtx);
308 #else /* HAVE_GETADDRINFO */
311 * gethostbyname_thread() resolves a name and then exits.
313 static unsigned int CURL_STDCALL gethostbyname_thread(void *arg)
315 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
316 struct thread_data *td = tsd->td;
318 tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
321 tsd->sock_error = SOCKERRNO;
322 if(tsd->sock_error == 0)
323 tsd->sock_error = RESOLVER_ENOMEM;
326 Curl_mutex_acquire(tsd->mtx);
328 /* too late, gotta clean up the mess */
329 Curl_mutex_release(tsd->mtx);
330 destroy_thread_sync_data(tsd);
335 Curl_mutex_release(tsd->mtx);
341 #endif /* HAVE_GETADDRINFO */
344 * destroy_async_data() cleans up async resolver data and thread handle.
346 static void destroy_async_data(struct Curl_async *async)
348 if(async->os_specific) {
349 struct thread_data *td = (struct thread_data*) async->os_specific;
353 * if the thread is still blocking in the resolve syscall, detach it and
354 * let the thread do the cleanup...
356 Curl_mutex_acquire(td->tsd.mtx);
359 Curl_mutex_release(td->tsd.mtx);
362 Curl_thread_destroy(td->thread_hnd);
365 if(td->thread_hnd != curl_thread_t_null)
366 Curl_thread_join(&td->thread_hnd);
368 destroy_thread_sync_data(&td->tsd);
370 free(async->os_specific);
373 async->os_specific = NULL;
375 free(async->hostname);
376 async->hostname = NULL;
380 * init_resolve_thread() starts a new thread that performs the actual
381 * resolve. This function returns before the resolve is done.
383 * Returns FALSE in case of failure, otherwise TRUE.
385 static bool init_resolve_thread(struct connectdata *conn,
386 const char *hostname, int port,
387 const struct addrinfo *hints)
389 struct thread_data *td = calloc(1, sizeof(struct thread_data));
392 conn->async.os_specific = (void *)td;
396 conn->async.port = port;
397 conn->async.done = FALSE;
398 conn->async.status = 0;
399 conn->async.dns = NULL;
400 td->thread_hnd = curl_thread_t_null;
402 if(!init_thread_sync_data(td, hostname, port, hints)) {
403 conn->async.os_specific = NULL;
408 free(conn->async.hostname);
409 conn->async.hostname = strdup(hostname);
410 if(!conn->async.hostname)
413 /* The thread will set this to 1 when complete. */
416 #ifdef HAVE_GETADDRINFO
417 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
419 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
422 if(!td->thread_hnd) {
423 /* The thread never started, so mark it as done here for proper cleanup. */
432 destroy_async_data(&conn->async);
440 * resolver_error() calls failf() with the appropriate message after a resolve
444 static CURLcode resolver_error(struct connectdata *conn)
446 const char *host_or_proxy;
449 if(conn->bits.httpproxy) {
450 host_or_proxy = "proxy";
451 result = CURLE_COULDNT_RESOLVE_PROXY;
454 host_or_proxy = "host";
455 result = CURLE_COULDNT_RESOLVE_HOST;
458 failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
459 conn->async.hostname);
465 * Until we gain a way to signal the resolver threads to stop early, we must
466 * simply wait for them and ignore their results.
468 void Curl_resolver_kill(struct connectdata *conn)
470 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
472 /* If we're still resolving, we must wait for the threads to fully clean up,
473 unfortunately. Otherwise, we can simply cancel to clean up any resolver
475 if(td && td->thread_hnd != curl_thread_t_null)
476 (void)Curl_resolver_wait_resolv(conn, NULL);
478 Curl_resolver_cancel(conn);
482 * Curl_resolver_wait_resolv()
484 * Waits for a resolve to finish. This function should be avoided since using
485 * this risk getting the multi interface to "hang".
487 * If 'entry' is non-NULL, make it point to the resolved dns entry
489 * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
490 * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
492 * This is the version for resolves-in-a-thread.
494 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
495 struct Curl_dns_entry **entry)
497 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
498 CURLcode result = CURLE_OK;
500 DEBUGASSERT(conn && td);
501 DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
503 /* wait for the thread to resolve the name */
504 if(Curl_thread_join(&td->thread_hnd)) {
506 result = getaddrinfo_complete(conn);
511 conn->async.done = TRUE;
514 *entry = conn->async.dns;
517 /* a name was not resolved, report error */
518 result = resolver_error(conn);
520 destroy_async_data(&conn->async);
523 connclose(conn, "asynch resolve failed");
529 * Curl_resolver_is_resolved() is called repeatedly to check if a previous
530 * name resolve request has completed. It should also make sure to time-out if
531 * the operation seems to take too long.
533 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
534 struct Curl_dns_entry **entry)
536 struct Curl_easy *data = conn->data;
537 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
544 return CURLE_COULDNT_RESOLVE_HOST;
547 Curl_mutex_acquire(td->tsd.mtx);
549 Curl_mutex_release(td->tsd.mtx);
552 getaddrinfo_complete(conn);
554 if(!conn->async.dns) {
555 CURLcode result = resolver_error(conn);
556 destroy_async_data(&conn->async);
559 destroy_async_data(&conn->async);
560 *entry = conn->async.dns;
563 /* poll for name lookup done with exponential backoff up to 250ms */
564 timediff_t elapsed = Curl_timediff(Curl_now(),
565 data->progress.t_startsingle);
569 if(td->poll_interval == 0)
570 /* Start at 1ms poll interval */
571 td->poll_interval = 1;
572 else if(elapsed >= td->interval_end)
573 /* Back-off exponentially if last interval expired */
574 td->poll_interval *= 2;
576 if(td->poll_interval > 250)
577 td->poll_interval = 250;
579 td->interval_end = elapsed + td->poll_interval;
580 Curl_expire(conn->data, td->poll_interval, EXPIRE_ASYNC_NAME);
586 int Curl_resolver_getsock(struct connectdata *conn,
587 curl_socket_t *socks,
592 struct Curl_easy *data = conn->data;
593 struct resdata *reslv = (struct resdata *)data->state.resolver;
596 ms = Curl_timediff(Curl_now(), reslv->start);
605 Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
609 #ifndef HAVE_GETADDRINFO
611 * Curl_getaddrinfo() - for platforms without getaddrinfo
613 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
614 const char *hostname,
619 struct Curl_easy *data = conn->data;
620 struct resdata *reslv = (struct resdata *)data->state.resolver;
622 *waitp = 0; /* default to synchronous response */
624 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
625 /* This is a dotted IP address 123.123.123.123-style */
626 return Curl_ip2addr(AF_INET, &in, hostname, port);
628 reslv->start = Curl_now();
630 /* fire up a new resolver thread! */
631 if(init_resolve_thread(conn, hostname, port, NULL)) {
632 *waitp = 1; /* expect asynchronous response */
636 failf(conn->data, "getaddrinfo() thread failed\n");
641 #else /* !HAVE_GETADDRINFO */
644 * Curl_resolver_getaddrinfo() - for getaddrinfo
646 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
647 const char *hostname,
651 struct addrinfo hints;
654 struct Curl_easy *data = conn->data;
655 struct resdata *reslv = (struct resdata *)data->state.resolver;
657 *waitp = 0; /* default to synchronous response */
659 #ifndef USE_RESOLVE_ON_IPS
662 /* First check if this is an IPv4 address string */
663 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
664 /* This is a dotted IP address 123.123.123.123-style */
665 return Curl_ip2addr(AF_INET, &in, hostname, port);
670 /* check if this is an IPv6 address string */
671 if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
672 /* This is an IPv6 address literal */
673 return Curl_ip2addr(AF_INET6, &in6, hostname, port);
675 #endif /* CURLRES_IPV6 */
676 #endif /* !USE_RESOLVE_ON_IPS */
680 * Check if a limited name resolve has been requested.
682 switch(conn->ip_version) {
683 case CURL_IPRESOLVE_V4:
686 case CURL_IPRESOLVE_V6:
694 if((pf != PF_INET) && !Curl_ipv6works())
695 /* The stack seems to be a non-IPv6 one */
697 #endif /* CURLRES_IPV6 */
699 memset(&hints, 0, sizeof(hints));
700 hints.ai_family = pf;
701 hints.ai_socktype = conn->socktype;
703 snprintf(sbuf, sizeof(sbuf), "%d", port);
705 reslv->start = Curl_now();
706 /* fire up a new resolver thread! */
707 if(init_resolve_thread(conn, hostname, port, &hints)) {
708 *waitp = 1; /* expect asynchronous response */
712 failf(data, "getaddrinfo() thread failed to start\n");
717 #endif /* !HAVE_GETADDRINFO */
719 CURLcode Curl_set_dns_servers(struct Curl_easy *data,
724 return CURLE_NOT_BUILT_IN;
728 CURLcode Curl_set_dns_interface(struct Curl_easy *data,
733 return CURLE_NOT_BUILT_IN;
736 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
737 const char *local_ip4)
741 return CURLE_NOT_BUILT_IN;
744 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
745 const char *local_ip6)
749 return CURLE_NOT_BUILT_IN;
752 #endif /* CURLRES_THREADED */