1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
41 #include <stdlib.h> /* required for free() prototypes */
44 #include <unistd.h> /* for the close() proto */
52 #if defined(USE_THREADS_POSIX)
53 # ifdef HAVE_PTHREAD_H
56 #elif defined(USE_THREADS_WIN32)
57 # ifdef HAVE_PROCESS_H
62 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
64 #define in_addr_t unsigned long
67 #ifdef HAVE_GETADDRINFO
68 # define RESOLVER_ENOMEM EAI_MEMORY
70 # define RESOLVER_ENOMEM ENOMEM
81 #include "inet_pton.h"
82 #include "inet_ntop.h"
83 #include "curl_threads.h"
85 #define _MPRINTF_REPLACE /* use our functions only */
86 #include <curl/mprintf.h>
88 #include "curl_memory.h"
89 /* The last #include file should be: */
92 /***********************************************************************
93 * Only for threaded name resolves builds
94 **********************************************************************/
95 #ifdef CURLRES_THREADED
98 * Curl_resolver_global_init()
99 * Called from curl_global_init() to initialize global resolver environment.
102 int Curl_resolver_global_init(void)
108 * Curl_resolver_global_cleanup()
109 * Called from curl_global_cleanup() to destroy global resolver environment.
112 void Curl_resolver_global_cleanup(void)
117 * Curl_resolver_init()
118 * Called from curl_easy_init() -> Curl_open() to initialize resolver
119 * URL-state specific environment ('resolver' member of the UrlState
120 * structure). Does nothing here.
122 int Curl_resolver_init(void **resolver)
129 * Curl_resolver_cleanup()
130 * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
131 * URL-state specific environment ('resolver' member of the UrlState
132 * structure). Does nothing here.
134 void Curl_resolver_cleanup(void *resolver)
140 * Curl_resolver_duphandle()
141 * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
142 * environment ('resolver' member of the UrlState structure). Does nothing
145 int Curl_resolver_duphandle(void **to, void *from)
152 static void destroy_async_data(struct Curl_async *);
155 * Cancel all possibly still on-going resolves for this connection.
157 void Curl_resolver_cancel(struct connectdata *conn)
159 destroy_async_data(&conn->async);
162 /* This function is used to init a threaded resolve */
163 static bool init_resolve_thread(struct connectdata *conn,
164 const char *hostname, int port,
165 const struct addrinfo *hints);
168 /* Data for synchronization between resolver thread and its parent */
169 struct thread_sync_data {
173 char * hostname; /* hostname to resolve, Curl_async.hostname
178 #ifdef HAVE_GETADDRINFO
179 struct addrinfo hints;
184 curl_thread_t thread_hnd;
185 curl_socket_t dummy_sock;
186 unsigned int poll_interval;
188 struct thread_sync_data tsd;
191 static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
193 return &(((struct thread_data *)conn->async.os_specific)->tsd);
196 #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
198 /* Destroy resolver thread synchronization data */
200 void destroy_thread_sync_data(struct thread_sync_data * tsd)
203 Curl_mutex_destroy(tsd->mtx);
211 Curl_freeaddrinfo(tsd->res);
213 memset(tsd,0,sizeof(*tsd));
216 /* Initialize resolver thread synchronization data */
218 int init_thread_sync_data(struct thread_sync_data * tsd,
219 const char * hostname,
221 const struct addrinfo *hints)
223 memset(tsd, 0, sizeof(*tsd));
233 tsd->mtx = malloc(sizeof(curl_mutex_t));
237 Curl_mutex_init(tsd->mtx);
239 tsd->sock_error = CURL_ASYNC_SUCCESS;
241 /* Copying hostname string because original can be destroyed by parent
242 * thread during gethostbyname execution.
244 tsd->hostname = strdup(hostname);
251 /* Memory allocation failed */
252 destroy_thread_sync_data(tsd);
256 static int getaddrinfo_complete(struct connectdata *conn)
258 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
261 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
262 /* The tsd->res structure has been copied to async.dns and perhaps the DNS
263 cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
271 #ifdef HAVE_GETADDRINFO
274 * getaddrinfo_thread() resolves a name and then exits.
276 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
279 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
281 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
282 char service [NI_MAXSERV];
285 snprintf(service, sizeof(service), "%d", tsd->port);
287 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
290 tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
291 if(tsd->sock_error == 0)
292 tsd->sock_error = RESOLVER_ENOMEM;
295 Curl_mutex_acquire(tsd->mtx);
297 Curl_mutex_release(tsd->mtx);
302 #else /* HAVE_GETADDRINFO */
305 * gethostbyname_thread() resolves a name and then exits.
307 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
309 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
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 Curl_mutex_release(tsd->mtx);
326 #endif /* HAVE_GETADDRINFO */
329 * destroy_async_data() cleans up async resolver data and thread handle.
331 static void destroy_async_data (struct Curl_async *async)
334 free(async->hostname);
336 if(async->os_specific) {
337 struct thread_data *td = (struct thread_data*) async->os_specific;
339 if(td->dummy_sock != CURL_SOCKET_BAD)
340 sclose(td->dummy_sock);
342 if(td->thread_hnd != curl_thread_t_null)
343 Curl_thread_join(&td->thread_hnd);
345 destroy_thread_sync_data(&td->tsd);
347 free(async->os_specific);
349 async->hostname = NULL;
350 async->os_specific = NULL;
354 * init_resolve_thread() starts a new thread that performs the actual
355 * resolve. This function returns before the resolve is done.
357 * Returns FALSE in case of failure, otherwise TRUE.
359 static bool init_resolve_thread (struct connectdata *conn,
360 const char *hostname, int port,
361 const struct addrinfo *hints)
363 struct thread_data *td = calloc(1, sizeof(struct thread_data));
364 int err = RESOLVER_ENOMEM;
366 conn->async.os_specific = (void*) td;
370 conn->async.port = port;
371 conn->async.done = FALSE;
372 conn->async.status = 0;
373 conn->async.dns = NULL;
374 td->dummy_sock = CURL_SOCKET_BAD;
375 td->thread_hnd = curl_thread_t_null;
377 if(!init_thread_sync_data(&td->tsd, hostname, port, hints))
380 Curl_safefree(conn->async.hostname);
381 conn->async.hostname = strdup(hostname);
382 if(!conn->async.hostname)
386 /* This socket is only to keep Curl_resolver_fdset() and select() happy;
387 * should never become signalled for read since it's unbound but
388 * Windows needs at least 1 socket in select().
390 td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
391 if(td->dummy_sock == CURL_SOCKET_BAD)
395 #ifdef HAVE_GETADDRINFO
396 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
398 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
401 if(!td->thread_hnd) {
411 destroy_async_data(&conn->async);
418 #if defined(HAVE_GETADDRINFO) && !defined(HAVE_GAI_STRERROR) && !defined(WIN32)
419 /* NetWare has getaddrinfo but lacks gai_strerror.
420 Windows has a gai_strerror but it is bad (not thread-safe) and the generic
421 socket error string function can be used for this pupose. */
422 static const char *gai_strerror(int ecode)
426 return "The name could not be resolved at this time";
428 return "The flags parameter had an invalid value";
430 return "A non-recoverable error occurred when attempting to "
433 return "The address family was not recognized";
435 return "Out of memory";
437 return "The name does not resolve for the supplied parameters";
439 return "The service passed was not recognized for the "
440 "specified socket type"
442 return "The intended socket type was not recognized"
444 return "A system error occurred";
446 return "An argument buffer overflowed";
448 return "Unknown error";
450 /* define this now as this is a private implementation of said function */
451 #define HAVE_GAI_STRERROR
457 * resolver_error() calls failf() with the appropriate message after a resolve
461 static void resolver_error(struct connectdata *conn, const char *host_or_proxy)
463 failf(conn->data, "Could not resolve %s: %s; %s", host_or_proxy,
464 conn->async.hostname,
465 #ifdef HAVE_GAI_STRERROR
466 /* NetWare doesn't have gai_strerror and on Windows it isn't deemed
468 gai_strerror(conn->async.status)
470 Curl_strerror(conn, conn->async.status)
476 * Curl_resolver_wait_resolv()
478 * waits for a resolve to finish. This function should be avoided since using
479 * this risk getting the multi interface to "hang".
481 * If 'entry' is non-NULL, make it point to the resolved dns entry
483 * This is the version for resolves-in-a-thread.
485 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
486 struct Curl_dns_entry **entry)
488 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
489 struct SessionHandle *data = conn->data;
490 CURLcode rc = CURLE_OK;
492 DEBUGASSERT(conn && td);
494 /* wait for the thread to resolve the name */
495 if(Curl_thread_join(&td->thread_hnd))
496 rc = getaddrinfo_complete(conn);
500 conn->async.done = TRUE;
503 *entry = conn->async.dns;
505 if(!conn->async.dns) {
506 /* a name was not resolved */
507 if(conn->bits.httpproxy) {
508 resolver_error(conn, "proxy");
509 rc = CURLE_COULDNT_RESOLVE_PROXY;
512 resolver_error(conn, "host");
513 rc = CURLE_COULDNT_RESOLVE_HOST;
517 destroy_async_data(&conn->async);
520 conn->bits.close = TRUE;
526 * Curl_resolver_is_resolved() is called repeatedly to check if a previous
527 * name resolve request has completed. It should also make sure to time-out if
528 * the operation seems to take too long.
530 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
531 struct Curl_dns_entry **entry)
533 struct SessionHandle *data = conn->data;
534 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
541 return CURLE_COULDNT_RESOLVE_HOST;
544 Curl_mutex_acquire(td->tsd.mtx);
546 Curl_mutex_release(td->tsd.mtx);
549 getaddrinfo_complete(conn);
550 destroy_async_data(&conn->async);
552 if(!conn->async.dns) {
553 resolver_error(conn, "host");
554 return CURLE_COULDNT_RESOLVE_HOST;
556 *entry = conn->async.dns;
559 /* poll for name lookup done with exponential backoff up to 250ms */
560 int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
564 if(td->poll_interval == 0)
565 /* Start at 1ms poll interval */
566 td->poll_interval = 1;
567 else if(elapsed >= td->interval_end)
568 /* Back-off exponentially if last interval expired */
569 td->poll_interval *= 2;
571 if(td->poll_interval > 250)
572 td->poll_interval = 250;
574 td->interval_end = elapsed + td->poll_interval;
575 Curl_expire(conn->data, td->poll_interval);
581 int Curl_resolver_getsock(struct connectdata *conn,
582 curl_socket_t *socks,
585 const struct thread_data *td =
586 (const struct thread_data *) conn->async.os_specific;
588 if(td && td->dummy_sock != CURL_SOCKET_BAD) {
590 /* return one socket waiting for readable, even though this is just
592 socks[0] = td->dummy_sock;
593 return GETSOCK_READSOCK(0);
599 #ifndef HAVE_GETADDRINFO
601 * Curl_getaddrinfo() - for platforms without getaddrinfo
603 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
604 const char *hostname,
610 *waitp = 0; /* default to synchronous response */
612 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
613 /* This is a dotted IP address 123.123.123.123-style */
614 return Curl_ip2addr(AF_INET, &in, hostname, port);
616 /* fire up a new resolver thread! */
617 if(init_resolve_thread(conn, hostname, port, NULL)) {
618 *waitp = 1; /* expect asynchronous response */
622 /* fall-back to blocking version */
623 return Curl_ipv4_resolve_r(hostname, port);
626 #else /* !HAVE_GETADDRINFO */
629 * Curl_resolver_getaddrinfo() - for getaddrinfo
631 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
632 const char *hostname,
636 struct addrinfo hints;
639 char sbuf[NI_MAXSERV];
641 struct SessionHandle *data = conn->data;
643 *waitp = 0; /* default to synchronous response */
647 * Check if a limited name resolve has been requested.
649 switch(conn->ip_version) {
650 case CURL_IPRESOLVE_V4:
653 case CURL_IPRESOLVE_V6:
661 if((pf != PF_INET) && !Curl_ipv6works())
662 /* the stack seems to be a non-ipv6 one */
665 #endif /* !CURLRES_IPV4 */
667 memset(&hints, 0, sizeof(hints));
668 hints.ai_family = pf;
669 hints.ai_socktype = conn->socktype;
671 snprintf(sbuf, sizeof(sbuf), "%d", port);
673 /* fire up a new resolver thread! */
674 if(init_resolve_thread(conn, hostname, port, &hints)) {
675 *waitp = 1; /* expect asynchronous response */
679 /* fall-back to blocking version */
680 infof(data, "init_resolve_thread() failed for %s; %s\n",
681 hostname, Curl_strerror(conn, ERRNO));
683 error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
685 infof(data, "getaddrinfo() failed for %s:%d; %s\n",
686 hostname, port, Curl_strerror(conn, SOCKERRNO));
692 #endif /* !HAVE_GETADDRINFO */
694 #endif /* CURLRES_THREADED */