1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
39 #if defined(USE_THREADS_POSIX)
40 # ifdef HAVE_PTHREAD_H
43 #elif defined(USE_THREADS_WIN32)
44 # ifdef HAVE_PROCESS_H
49 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
51 #define in_addr_t unsigned long
54 #ifdef HAVE_GETADDRINFO
55 # define RESOLVER_ENOMEM EAI_MEMORY
57 # define RESOLVER_ENOMEM ENOMEM
68 #include "inet_pton.h"
69 #include "inet_ntop.h"
70 #include "curl_threads.h"
72 /* The last 3 #include files should be in this order */
73 #include "curl_printf.h"
74 #include "curl_memory.h"
77 /***********************************************************************
78 * Only for threaded name resolves builds
79 **********************************************************************/
80 #ifdef CURLRES_THREADED
83 * Curl_resolver_global_init()
84 * Called from curl_global_init() to initialize global resolver environment.
87 int Curl_resolver_global_init(void)
93 * Curl_resolver_global_cleanup()
94 * Called from curl_global_cleanup() to destroy global resolver environment.
97 void Curl_resolver_global_cleanup(void)
102 * Curl_resolver_init()
103 * Called from curl_easy_init() -> Curl_open() to initialize resolver
104 * URL-state specific environment ('resolver' member of the UrlState
105 * structure). Does nothing here.
107 CURLcode Curl_resolver_init(void **resolver)
114 * Curl_resolver_cleanup()
115 * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
116 * URL-state specific environment ('resolver' member of the UrlState
117 * structure). Does nothing here.
119 void Curl_resolver_cleanup(void *resolver)
125 * Curl_resolver_duphandle()
126 * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
127 * environment ('resolver' member of the UrlState structure). Does nothing
130 int Curl_resolver_duphandle(void **to, void *from)
137 static void destroy_async_data(struct Curl_async *);
140 * Cancel all possibly still on-going resolves for this connection.
142 void Curl_resolver_cancel(struct connectdata *conn)
144 destroy_async_data(&conn->async);
147 /* This function is used to init a threaded resolve */
148 static bool init_resolve_thread(struct connectdata *conn,
149 const char *hostname, int port,
150 const struct addrinfo *hints);
153 /* Data for synchronization between resolver thread and its parent */
154 struct thread_sync_data {
158 char * hostname; /* hostname to resolve, Curl_async.hostname
163 #ifdef HAVE_GETADDRINFO
164 struct addrinfo hints;
166 struct thread_data *td; /* for thread-self cleanup */
170 curl_thread_t thread_hnd;
171 unsigned int poll_interval;
173 struct thread_sync_data tsd;
176 static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
178 return &(((struct thread_data *)conn->async.os_specific)->tsd);
181 #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
183 /* Destroy resolver thread synchronization data */
185 void destroy_thread_sync_data(struct thread_sync_data * tsd)
188 Curl_mutex_destroy(tsd->mtx);
195 Curl_freeaddrinfo(tsd->res);
197 memset(tsd, 0, sizeof(*tsd));
200 /* Initialize resolver thread synchronization data */
202 int init_thread_sync_data(struct thread_data * td,
203 const char * hostname,
205 const struct addrinfo *hints)
207 struct thread_sync_data *tsd = &td->tsd;
209 memset(tsd, 0, sizeof(*tsd));
213 #ifdef HAVE_GETADDRINFO
220 tsd->mtx = malloc(sizeof(curl_mutex_t));
224 Curl_mutex_init(tsd->mtx);
226 tsd->sock_error = CURL_ASYNC_SUCCESS;
228 /* Copying hostname string because original can be destroyed by parent
229 * thread during gethostbyname execution.
231 tsd->hostname = strdup(hostname);
238 /* Memory allocation failed */
239 destroy_thread_sync_data(tsd);
243 static int getaddrinfo_complete(struct connectdata *conn)
245 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
248 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
249 /* The tsd->res structure has been copied to async.dns and perhaps the DNS
250 cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
258 #ifdef HAVE_GETADDRINFO
261 * getaddrinfo_thread() resolves a name and then exits.
263 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
266 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
268 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
269 struct thread_data *td = tsd->td;
273 snprintf(service, sizeof(service), "%d", tsd->port);
275 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
278 tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
279 if(tsd->sock_error == 0)
280 tsd->sock_error = RESOLVER_ENOMEM;
283 Curl_addrinfo_set_port(tsd->res, tsd->port);
286 Curl_mutex_acquire(tsd->mtx);
288 /* too late, gotta clean up the mess */
289 Curl_mutex_release(tsd->mtx);
290 destroy_thread_sync_data(tsd);
295 Curl_mutex_release(tsd->mtx);
301 #else /* HAVE_GETADDRINFO */
304 * gethostbyname_thread() resolves a name and then exits.
306 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
308 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
309 struct thread_data *td = tsd->td;
311 tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
314 tsd->sock_error = SOCKERRNO;
315 if(tsd->sock_error == 0)
316 tsd->sock_error = RESOLVER_ENOMEM;
319 Curl_mutex_acquire(tsd->mtx);
321 /* too late, gotta clean up the mess */
322 Curl_mutex_release(tsd->mtx);
323 destroy_thread_sync_data(tsd);
328 Curl_mutex_release(tsd->mtx);
334 #endif /* HAVE_GETADDRINFO */
337 * destroy_async_data() cleans up async resolver data and thread handle.
339 static void destroy_async_data (struct Curl_async *async)
341 if(async->os_specific) {
342 struct thread_data *td = (struct thread_data*) async->os_specific;
346 * if the thread is still blocking in the resolve syscall, detach it and
347 * let the thread do the cleanup...
349 Curl_mutex_acquire(td->tsd.mtx);
352 Curl_mutex_release(td->tsd.mtx);
355 Curl_thread_destroy(td->thread_hnd);
358 if(td->thread_hnd != curl_thread_t_null)
359 Curl_thread_join(&td->thread_hnd);
361 destroy_thread_sync_data(&td->tsd);
363 free(async->os_specific);
366 async->os_specific = NULL;
368 free(async->hostname);
369 async->hostname = NULL;
373 * init_resolve_thread() starts a new thread that performs the actual
374 * resolve. This function returns before the resolve is done.
376 * Returns FALSE in case of failure, otherwise TRUE.
378 static bool init_resolve_thread (struct connectdata *conn,
379 const char *hostname, int port,
380 const struct addrinfo *hints)
382 struct thread_data *td = calloc(1, sizeof(struct thread_data));
383 int err = RESOLVER_ENOMEM;
385 conn->async.os_specific = (void*) td;
389 conn->async.port = port;
390 conn->async.done = FALSE;
391 conn->async.status = 0;
392 conn->async.dns = NULL;
393 td->thread_hnd = curl_thread_t_null;
395 if(!init_thread_sync_data(td, hostname, port, hints))
398 free(conn->async.hostname);
399 conn->async.hostname = strdup(hostname);
400 if(!conn->async.hostname)
403 #ifdef HAVE_GETADDRINFO
404 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
406 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
409 if(!td->thread_hnd) {
419 destroy_async_data(&conn->async);
427 * resolver_error() calls failf() with the appropriate message after a resolve
431 static CURLcode resolver_error(struct connectdata *conn)
433 const char *host_or_proxy;
436 if(conn->bits.httpproxy) {
437 host_or_proxy = "proxy";
438 result = CURLE_COULDNT_RESOLVE_PROXY;
441 host_or_proxy = "host";
442 result = CURLE_COULDNT_RESOLVE_HOST;
445 failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
446 conn->async.hostname);
452 * Curl_resolver_wait_resolv()
454 * waits for a resolve to finish. This function should be avoided since using
455 * this risk getting the multi interface to "hang".
457 * If 'entry' is non-NULL, make it point to the resolved dns entry
459 * This is the version for resolves-in-a-thread.
461 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
462 struct Curl_dns_entry **entry)
464 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
465 CURLcode result = CURLE_OK;
467 DEBUGASSERT(conn && td);
469 /* wait for the thread to resolve the name */
470 if(Curl_thread_join(&td->thread_hnd))
471 result = getaddrinfo_complete(conn);
475 conn->async.done = TRUE;
478 *entry = conn->async.dns;
481 /* a name was not resolved, report error */
482 result = resolver_error(conn);
484 destroy_async_data(&conn->async);
487 connclose(conn, "asynch resolve failed");
493 * Curl_resolver_is_resolved() is called repeatedly to check if a previous
494 * name resolve request has completed. It should also make sure to time-out if
495 * the operation seems to take too long.
497 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
498 struct Curl_dns_entry **entry)
500 struct Curl_easy *data = conn->data;
501 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
508 return CURLE_COULDNT_RESOLVE_HOST;
511 Curl_mutex_acquire(td->tsd.mtx);
513 Curl_mutex_release(td->tsd.mtx);
516 getaddrinfo_complete(conn);
518 if(!conn->async.dns) {
519 CURLcode result = resolver_error(conn);
520 destroy_async_data(&conn->async);
523 destroy_async_data(&conn->async);
524 *entry = conn->async.dns;
527 /* poll for name lookup done with exponential backoff up to 250ms */
528 long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
532 if(td->poll_interval == 0)
533 /* Start at 1ms poll interval */
534 td->poll_interval = 1;
535 else if(elapsed >= td->interval_end)
536 /* Back-off exponentially if last interval expired */
537 td->poll_interval *= 2;
539 if(td->poll_interval > 250)
540 td->poll_interval = 250;
542 td->interval_end = elapsed + td->poll_interval;
543 Curl_expire(conn->data, td->poll_interval);
549 int Curl_resolver_getsock(struct connectdata *conn,
550 curl_socket_t *socks,
559 #ifndef HAVE_GETADDRINFO
561 * Curl_getaddrinfo() - for platforms without getaddrinfo
563 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
564 const char *hostname,
570 *waitp = 0; /* default to synchronous response */
572 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
573 /* This is a dotted IP address 123.123.123.123-style */
574 return Curl_ip2addr(AF_INET, &in, hostname, port);
576 /* fire up a new resolver thread! */
577 if(init_resolve_thread(conn, hostname, port, NULL)) {
578 *waitp = 1; /* expect asynchronous response */
582 /* fall-back to blocking version */
583 return Curl_ipv4_resolve_r(hostname, port);
586 #else /* !HAVE_GETADDRINFO */
589 * Curl_resolver_getaddrinfo() - for getaddrinfo
591 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
592 const char *hostname,
596 struct addrinfo hints;
604 #endif /* CURLRES_IPV6 */
606 *waitp = 0; /* default to synchronous response */
608 #ifndef USE_RESOLVE_ON_IPS
609 /* First check if this is an IPv4 address string */
610 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
611 /* This is a dotted IP address 123.123.123.123-style */
612 return Curl_ip2addr(AF_INET, &in, hostname, port);
615 /* check if this is an IPv6 address string */
616 if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
617 /* This is an IPv6 address literal */
618 return Curl_ip2addr(AF_INET6, &in6, hostname, port);
619 #endif /* CURLRES_IPV6 */
620 #endif /* !USE_RESOLVE_ON_IPS */
624 * Check if a limited name resolve has been requested.
626 switch(conn->ip_version) {
627 case CURL_IPRESOLVE_V4:
630 case CURL_IPRESOLVE_V6:
638 if((pf != PF_INET) && !Curl_ipv6works())
639 /* The stack seems to be a non-IPv6 one */
641 #endif /* CURLRES_IPV6 */
643 memset(&hints, 0, sizeof(hints));
644 hints.ai_family = pf;
645 hints.ai_socktype = conn->socktype;
647 snprintf(sbuf, sizeof(sbuf), "%d", port);
649 /* fire up a new resolver thread! */
650 if(init_resolve_thread(conn, hostname, port, &hints)) {
651 *waitp = 1; /* expect asynchronous response */
655 /* fall-back to blocking version */
656 infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
657 hostname, Curl_strerror(conn, ERRNO));
659 error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
661 infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
662 hostname, port, Curl_strerror(conn, SOCKERRNO));
666 Curl_addrinfo_set_port(res, port);
672 #endif /* !HAVE_GETADDRINFO */
674 CURLcode Curl_set_dns_servers(struct Curl_easy *data,
679 return CURLE_NOT_BUILT_IN;
683 CURLcode Curl_set_dns_interface(struct Curl_easy *data,
688 return CURLE_NOT_BUILT_IN;
691 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
692 const char *local_ip4)
696 return CURLE_NOT_BUILT_IN;
699 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
700 const char *local_ip6)
704 return CURLE_NOT_BUILT_IN;
707 #endif /* CURLRES_THREADED */