1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 #include "curl_printf.h"
73 #include "curl_memory.h"
75 /* The last #include file should be: */
78 /***********************************************************************
79 * Only for threaded name resolves builds
80 **********************************************************************/
81 #ifdef CURLRES_THREADED
84 * Curl_resolver_global_init()
85 * Called from curl_global_init() to initialize global resolver environment.
88 int Curl_resolver_global_init(void)
94 * Curl_resolver_global_cleanup()
95 * Called from curl_global_cleanup() to destroy global resolver environment.
98 void Curl_resolver_global_cleanup(void)
103 * Curl_resolver_init()
104 * Called from curl_easy_init() -> Curl_open() to initialize resolver
105 * URL-state specific environment ('resolver' member of the UrlState
106 * structure). Does nothing here.
108 CURLcode Curl_resolver_init(void **resolver)
115 * Curl_resolver_cleanup()
116 * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
117 * URL-state specific environment ('resolver' member of the UrlState
118 * structure). Does nothing here.
120 void Curl_resolver_cleanup(void *resolver)
126 * Curl_resolver_duphandle()
127 * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
128 * environment ('resolver' member of the UrlState structure). Does nothing
131 int Curl_resolver_duphandle(void **to, void *from)
138 static void destroy_async_data(struct Curl_async *);
141 * Cancel all possibly still on-going resolves for this connection.
143 void Curl_resolver_cancel(struct connectdata *conn)
145 destroy_async_data(&conn->async);
148 /* This function is used to init a threaded resolve */
149 static bool init_resolve_thread(struct connectdata *conn,
150 const char *hostname, int port,
151 const struct addrinfo *hints);
154 /* Data for synchronization between resolver thread and its parent */
155 struct thread_sync_data {
159 char * hostname; /* hostname to resolve, Curl_async.hostname
164 #ifdef HAVE_GETADDRINFO
165 struct addrinfo hints;
167 struct thread_data *td; /* for thread-self cleanup */
171 curl_thread_t thread_hnd;
172 unsigned int poll_interval;
174 struct thread_sync_data tsd;
177 static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
179 return &(((struct thread_data *)conn->async.os_specific)->tsd);
182 #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
184 /* Destroy resolver thread synchronization data */
186 void destroy_thread_sync_data(struct thread_sync_data * tsd)
189 Curl_mutex_destroy(tsd->mtx);
196 Curl_freeaddrinfo(tsd->res);
198 memset(tsd, 0, sizeof(*tsd));
201 /* Initialize resolver thread synchronization data */
203 int init_thread_sync_data(struct thread_data * td,
204 const char * hostname,
206 const struct addrinfo *hints)
208 struct thread_sync_data *tsd = &td->tsd;
210 memset(tsd, 0, sizeof(*tsd));
214 #ifdef HAVE_GETADDRINFO
221 tsd->mtx = malloc(sizeof(curl_mutex_t));
225 Curl_mutex_init(tsd->mtx);
227 tsd->sock_error = CURL_ASYNC_SUCCESS;
229 /* Copying hostname string because original can be destroyed by parent
230 * thread during gethostbyname execution.
232 tsd->hostname = strdup(hostname);
239 /* Memory allocation failed */
240 destroy_thread_sync_data(tsd);
244 static int getaddrinfo_complete(struct connectdata *conn)
246 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
249 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
250 /* The tsd->res structure has been copied to async.dns and perhaps the DNS
251 cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
259 #ifdef HAVE_GETADDRINFO
262 * getaddrinfo_thread() resolves a name and then exits.
264 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
267 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
269 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
270 struct thread_data *td = tsd->td;
274 snprintf(service, sizeof(service), "%d", tsd->port);
276 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
279 tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
280 if(tsd->sock_error == 0)
281 tsd->sock_error = RESOLVER_ENOMEM;
284 Curl_mutex_acquire(tsd->mtx);
286 /* too late, gotta clean up the mess */
287 Curl_mutex_release(tsd->mtx);
288 destroy_thread_sync_data(tsd);
293 Curl_mutex_release(tsd->mtx);
299 #else /* HAVE_GETADDRINFO */
302 * gethostbyname_thread() resolves a name and then exits.
304 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
306 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
307 struct thread_data *td = tsd->td;
309 tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
312 tsd->sock_error = SOCKERRNO;
313 if(tsd->sock_error == 0)
314 tsd->sock_error = RESOLVER_ENOMEM;
317 Curl_mutex_acquire(tsd->mtx);
319 /* too late, gotta clean up the mess */
320 Curl_mutex_release(tsd->mtx);
321 destroy_thread_sync_data(tsd);
326 Curl_mutex_release(tsd->mtx);
332 #endif /* HAVE_GETADDRINFO */
335 * destroy_async_data() cleans up async resolver data and thread handle.
337 static void destroy_async_data (struct Curl_async *async)
339 if(async->os_specific) {
340 struct thread_data *td = (struct thread_data*) async->os_specific;
344 * if the thread is still blocking in the resolve syscall, detach it and
345 * let the thread do the cleanup...
347 Curl_mutex_acquire(td->tsd.mtx);
350 Curl_mutex_release(td->tsd.mtx);
353 Curl_thread_destroy(td->thread_hnd);
356 if(td->thread_hnd != curl_thread_t_null)
357 Curl_thread_join(&td->thread_hnd);
359 destroy_thread_sync_data(&td->tsd);
361 free(async->os_specific);
364 async->os_specific = NULL;
366 free(async->hostname);
367 async->hostname = NULL;
371 * init_resolve_thread() starts a new thread that performs the actual
372 * resolve. This function returns before the resolve is done.
374 * Returns FALSE in case of failure, otherwise TRUE.
376 static bool init_resolve_thread (struct connectdata *conn,
377 const char *hostname, int port,
378 const struct addrinfo *hints)
380 struct thread_data *td = calloc(1, sizeof(struct thread_data));
381 int err = RESOLVER_ENOMEM;
383 conn->async.os_specific = (void*) td;
387 conn->async.port = port;
388 conn->async.done = FALSE;
389 conn->async.status = 0;
390 conn->async.dns = NULL;
391 td->thread_hnd = curl_thread_t_null;
393 if(!init_thread_sync_data(td, hostname, port, hints))
396 free(conn->async.hostname);
397 conn->async.hostname = strdup(hostname);
398 if(!conn->async.hostname)
401 #ifdef HAVE_GETADDRINFO
402 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
404 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
407 if(!td->thread_hnd) {
417 destroy_async_data(&conn->async);
425 * resolver_error() calls failf() with the appropriate message after a resolve
429 static CURLcode resolver_error(struct connectdata *conn)
431 const char *host_or_proxy;
434 if(conn->bits.httpproxy) {
435 host_or_proxy = "proxy";
436 result = CURLE_COULDNT_RESOLVE_PROXY;
439 host_or_proxy = "host";
440 result = CURLE_COULDNT_RESOLVE_HOST;
443 failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
444 conn->async.hostname);
450 * Curl_resolver_wait_resolv()
452 * waits for a resolve to finish. This function should be avoided since using
453 * this risk getting the multi interface to "hang".
455 * If 'entry' is non-NULL, make it point to the resolved dns entry
457 * This is the version for resolves-in-a-thread.
459 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
460 struct Curl_dns_entry **entry)
462 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
463 CURLcode result = CURLE_OK;
465 DEBUGASSERT(conn && td);
467 /* wait for the thread to resolve the name */
468 if(Curl_thread_join(&td->thread_hnd))
469 result = getaddrinfo_complete(conn);
473 conn->async.done = TRUE;
476 *entry = conn->async.dns;
479 /* a name was not resolved, report error */
480 result = resolver_error(conn);
482 destroy_async_data(&conn->async);
485 connclose(conn, "asynch resolve failed");
491 * Curl_resolver_is_resolved() is called repeatedly to check if a previous
492 * name resolve request has completed. It should also make sure to time-out if
493 * the operation seems to take too long.
495 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
496 struct Curl_dns_entry **entry)
498 struct SessionHandle *data = conn->data;
499 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
506 return CURLE_COULDNT_RESOLVE_HOST;
509 Curl_mutex_acquire(td->tsd.mtx);
511 Curl_mutex_release(td->tsd.mtx);
514 getaddrinfo_complete(conn);
516 if(!conn->async.dns) {
517 CURLcode result = resolver_error(conn);
518 destroy_async_data(&conn->async);
521 destroy_async_data(&conn->async);
522 *entry = conn->async.dns;
525 /* poll for name lookup done with exponential backoff up to 250ms */
526 long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
530 if(td->poll_interval == 0)
531 /* Start at 1ms poll interval */
532 td->poll_interval = 1;
533 else if(elapsed >= td->interval_end)
534 /* Back-off exponentially if last interval expired */
535 td->poll_interval *= 2;
537 if(td->poll_interval > 250)
538 td->poll_interval = 250;
540 td->interval_end = elapsed + td->poll_interval;
541 Curl_expire(conn->data, td->poll_interval);
547 int Curl_resolver_getsock(struct connectdata *conn,
548 curl_socket_t *socks,
557 #ifndef HAVE_GETADDRINFO
559 * Curl_getaddrinfo() - for platforms without getaddrinfo
561 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
562 const char *hostname,
568 *waitp = 0; /* default to synchronous response */
570 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
571 /* This is a dotted IP address 123.123.123.123-style */
572 return Curl_ip2addr(AF_INET, &in, hostname, port);
574 /* fire up a new resolver thread! */
575 if(init_resolve_thread(conn, hostname, port, NULL)) {
576 *waitp = 1; /* expect asynchronous response */
580 /* fall-back to blocking version */
581 return Curl_ipv4_resolve_r(hostname, port);
584 #else /* !HAVE_GETADDRINFO */
587 * Curl_resolver_getaddrinfo() - for getaddrinfo
589 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
590 const char *hostname,
594 struct addrinfo hints;
602 #endif /* CURLRES_IPV6 */
604 *waitp = 0; /* default to synchronous response */
606 /* First check if this is an IPv4 address string */
607 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
608 /* This is a dotted IP address 123.123.123.123-style */
609 return Curl_ip2addr(AF_INET, &in, hostname, port);
612 /* check if this is an IPv6 address string */
613 if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
614 /* This is an IPv6 address literal */
615 return Curl_ip2addr(AF_INET6, &in6, hostname, port);
618 * Check if a limited name resolve has been requested.
620 switch(conn->ip_version) {
621 case CURL_IPRESOLVE_V4:
624 case CURL_IPRESOLVE_V6:
632 if((pf != PF_INET) && !Curl_ipv6works())
633 /* The stack seems to be a non-IPv6 one */
636 #endif /* CURLRES_IPV6 */
638 memset(&hints, 0, sizeof(hints));
639 hints.ai_family = pf;
640 hints.ai_socktype = conn->socktype;
642 snprintf(sbuf, sizeof(sbuf), "%d", port);
644 /* fire up a new resolver thread! */
645 if(init_resolve_thread(conn, hostname, port, &hints)) {
646 *waitp = 1; /* expect asynchronous response */
650 /* fall-back to blocking version */
651 infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
652 hostname, Curl_strerror(conn, ERRNO));
654 error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
656 infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
657 hostname, port, Curl_strerror(conn, SOCKERRNO));
663 #endif /* !HAVE_GETADDRINFO */
665 CURLcode Curl_set_dns_servers(struct SessionHandle *data,
670 return CURLE_NOT_BUILT_IN;
674 CURLcode Curl_set_dns_interface(struct SessionHandle *data,
679 return CURLE_NOT_BUILT_IN;
682 CURLcode Curl_set_dns_local_ip4(struct SessionHandle *data,
683 const char *local_ip4)
687 return CURLE_NOT_BUILT_IN;
690 CURLcode Curl_set_dns_local_ip6(struct SessionHandle *data,
691 const char *local_ip6)
695 return CURLE_NOT_BUILT_IN;
698 #endif /* CURLRES_THREADED */