1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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 #define _MPRINTF_REPLACE /* use our functions only */
73 #include <curl/mprintf.h>
75 #include "curl_memory.h"
76 /* The last #include file should be: */
79 /***********************************************************************
80 * Only for threaded name resolves builds
81 **********************************************************************/
82 #ifdef CURLRES_THREADED
85 * Curl_resolver_global_init()
86 * Called from curl_global_init() to initialize global resolver environment.
89 int Curl_resolver_global_init(void)
95 * Curl_resolver_global_cleanup()
96 * Called from curl_global_cleanup() to destroy global resolver environment.
99 void Curl_resolver_global_cleanup(void)
104 * Curl_resolver_init()
105 * Called from curl_easy_init() -> Curl_open() to initialize resolver
106 * URL-state specific environment ('resolver' member of the UrlState
107 * structure). Does nothing here.
109 CURLcode Curl_resolver_init(void **resolver)
116 * Curl_resolver_cleanup()
117 * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
118 * URL-state specific environment ('resolver' member of the UrlState
119 * structure). Does nothing here.
121 void Curl_resolver_cleanup(void *resolver)
127 * Curl_resolver_duphandle()
128 * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
129 * environment ('resolver' member of the UrlState structure). Does nothing
132 int Curl_resolver_duphandle(void **to, void *from)
139 static void destroy_async_data(struct Curl_async *);
142 * Cancel all possibly still on-going resolves for this connection.
144 void Curl_resolver_cancel(struct connectdata *conn)
146 destroy_async_data(&conn->async);
149 /* This function is used to init a threaded resolve */
150 static bool init_resolve_thread(struct connectdata *conn,
151 const char *hostname, int port,
152 const struct addrinfo *hints);
155 /* Data for synchronization between resolver thread and its parent */
156 struct thread_sync_data {
160 char * hostname; /* hostname to resolve, Curl_async.hostname
165 #ifdef HAVE_GETADDRINFO
166 struct addrinfo hints;
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);
197 Curl_freeaddrinfo(tsd->res);
199 memset(tsd,0,sizeof(*tsd));
202 /* Initialize resolver thread synchronization data */
204 int init_thread_sync_data(struct thread_sync_data * tsd,
205 const char * hostname,
207 const struct addrinfo *hints)
209 memset(tsd, 0, sizeof(*tsd));
219 tsd->mtx = malloc(sizeof(curl_mutex_t));
223 Curl_mutex_init(tsd->mtx);
225 tsd->sock_error = CURL_ASYNC_SUCCESS;
227 /* Copying hostname string because original can be destroyed by parent
228 * thread during gethostbyname execution.
230 tsd->hostname = strdup(hostname);
237 /* Memory allocation failed */
238 destroy_thread_sync_data(tsd);
242 static int getaddrinfo_complete(struct connectdata *conn)
244 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
247 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
248 /* The tsd->res structure has been copied to async.dns and perhaps the DNS
249 cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
257 #ifdef HAVE_GETADDRINFO
260 * getaddrinfo_thread() resolves a name and then exits.
262 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
265 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
267 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
268 char service [NI_MAXSERV];
271 snprintf(service, sizeof(service), "%d", tsd->port);
273 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
276 tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
277 if(tsd->sock_error == 0)
278 tsd->sock_error = RESOLVER_ENOMEM;
281 Curl_mutex_acquire(tsd->mtx);
283 Curl_mutex_release(tsd->mtx);
288 #else /* HAVE_GETADDRINFO */
291 * gethostbyname_thread() resolves a name and then exits.
293 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
295 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
297 tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
300 tsd->sock_error = SOCKERRNO;
301 if(tsd->sock_error == 0)
302 tsd->sock_error = RESOLVER_ENOMEM;
305 Curl_mutex_acquire(tsd->mtx);
307 Curl_mutex_release(tsd->mtx);
312 #endif /* HAVE_GETADDRINFO */
315 * destroy_async_data() cleans up async resolver data and thread handle.
317 static void destroy_async_data (struct Curl_async *async)
320 free(async->hostname);
322 if(async->os_specific) {
323 struct thread_data *td = (struct thread_data*) async->os_specific;
325 if(td->thread_hnd != curl_thread_t_null)
326 Curl_thread_join(&td->thread_hnd);
328 destroy_thread_sync_data(&td->tsd);
330 free(async->os_specific);
332 async->hostname = NULL;
333 async->os_specific = NULL;
337 * init_resolve_thread() starts a new thread that performs the actual
338 * resolve. This function returns before the resolve is done.
340 * Returns FALSE in case of failure, otherwise TRUE.
342 static bool init_resolve_thread (struct connectdata *conn,
343 const char *hostname, int port,
344 const struct addrinfo *hints)
346 struct thread_data *td = calloc(1, sizeof(struct thread_data));
347 int err = RESOLVER_ENOMEM;
349 conn->async.os_specific = (void*) td;
353 conn->async.port = port;
354 conn->async.done = FALSE;
355 conn->async.status = 0;
356 conn->async.dns = NULL;
357 td->thread_hnd = curl_thread_t_null;
359 if(!init_thread_sync_data(&td->tsd, hostname, port, hints))
362 Curl_safefree(conn->async.hostname);
363 conn->async.hostname = strdup(hostname);
364 if(!conn->async.hostname)
367 #ifdef HAVE_GETADDRINFO
368 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
370 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
373 if(!td->thread_hnd) {
383 destroy_async_data(&conn->async);
391 * resolver_error() calls failf() with the appropriate message after a resolve
395 static CURLcode resolver_error(struct connectdata *conn)
397 const char *host_or_proxy;
399 if(conn->bits.httpproxy) {
400 host_or_proxy = "proxy";
401 rc = CURLE_COULDNT_RESOLVE_PROXY;
404 host_or_proxy = "host";
405 rc = CURLE_COULDNT_RESOLVE_HOST;
408 failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
409 conn->async.hostname);
414 * Curl_resolver_wait_resolv()
416 * waits for a resolve to finish. This function should be avoided since using
417 * this risk getting the multi interface to "hang".
419 * If 'entry' is non-NULL, make it point to the resolved dns entry
421 * This is the version for resolves-in-a-thread.
423 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
424 struct Curl_dns_entry **entry)
426 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
427 CURLcode rc = CURLE_OK;
429 DEBUGASSERT(conn && td);
431 /* wait for the thread to resolve the name */
432 if(Curl_thread_join(&td->thread_hnd))
433 rc = getaddrinfo_complete(conn);
437 conn->async.done = TRUE;
440 *entry = conn->async.dns;
443 /* a name was not resolved, report error */
444 rc = resolver_error(conn);
446 destroy_async_data(&conn->async);
449 conn->bits.close = TRUE;
455 * Curl_resolver_is_resolved() is called repeatedly to check if a previous
456 * name resolve request has completed. It should also make sure to time-out if
457 * the operation seems to take too long.
459 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
460 struct Curl_dns_entry **entry)
462 struct SessionHandle *data = conn->data;
463 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
470 return CURLE_COULDNT_RESOLVE_HOST;
473 Curl_mutex_acquire(td->tsd.mtx);
475 Curl_mutex_release(td->tsd.mtx);
478 getaddrinfo_complete(conn);
480 if(!conn->async.dns) {
481 CURLcode rc = resolver_error(conn);
482 destroy_async_data(&conn->async);
485 destroy_async_data(&conn->async);
486 *entry = conn->async.dns;
489 /* poll for name lookup done with exponential backoff up to 250ms */
490 long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
494 if(td->poll_interval == 0)
495 /* Start at 1ms poll interval */
496 td->poll_interval = 1;
497 else if(elapsed >= td->interval_end)
498 /* Back-off exponentially if last interval expired */
499 td->poll_interval *= 2;
501 if(td->poll_interval > 250)
502 td->poll_interval = 250;
504 td->interval_end = elapsed + td->poll_interval;
505 Curl_expire(conn->data, td->poll_interval);
511 int Curl_resolver_getsock(struct connectdata *conn,
512 curl_socket_t *socks,
521 #ifndef HAVE_GETADDRINFO
523 * Curl_getaddrinfo() - for platforms without getaddrinfo
525 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
526 const char *hostname,
532 *waitp = 0; /* default to synchronous response */
534 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
535 /* This is a dotted IP address 123.123.123.123-style */
536 return Curl_ip2addr(AF_INET, &in, hostname, port);
538 /* fire up a new resolver thread! */
539 if(init_resolve_thread(conn, hostname, port, NULL)) {
540 *waitp = 1; /* expect asynchronous response */
544 /* fall-back to blocking version */
545 return Curl_ipv4_resolve_r(hostname, port);
548 #else /* !HAVE_GETADDRINFO */
551 * Curl_resolver_getaddrinfo() - for getaddrinfo
553 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
554 const char *hostname,
558 struct addrinfo hints;
562 char sbuf[NI_MAXSERV];
566 #endif /* CURLRES_IPV6 */
568 *waitp = 0; /* default to synchronous response */
570 /* First check if this is an IPv4 address string */
571 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
572 /* This is a dotted IP address 123.123.123.123-style */
573 return Curl_ip2addr(AF_INET, &in, hostname, port);
576 /* check if this is an IPv6 address string */
577 if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
578 /* This is an IPv6 address literal */
579 return Curl_ip2addr(AF_INET6, &in6, hostname, port);
582 * Check if a limited name resolve has been requested.
584 switch(conn->ip_version) {
585 case CURL_IPRESOLVE_V4:
588 case CURL_IPRESOLVE_V6:
596 if((pf != PF_INET) && !Curl_ipv6works())
597 /* the stack seems to be a non-ipv6 one */
600 #endif /* CURLRES_IPV6 */
602 memset(&hints, 0, sizeof(hints));
603 hints.ai_family = pf;
604 hints.ai_socktype = conn->socktype;
606 snprintf(sbuf, sizeof(sbuf), "%d", port);
608 /* fire up a new resolver thread! */
609 if(init_resolve_thread(conn, hostname, port, &hints)) {
610 *waitp = 1; /* expect asynchronous response */
614 /* fall-back to blocking version */
615 infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
616 hostname, Curl_strerror(conn, ERRNO));
618 error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
620 infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
621 hostname, port, Curl_strerror(conn, SOCKERRNO));
627 #endif /* !HAVE_GETADDRINFO */
629 CURLcode Curl_set_dns_servers(struct SessionHandle *data,
634 return CURLE_NOT_BUILT_IN;
638 #endif /* CURLRES_THREADED */