1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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
75 #include "inet_pton.h"
76 #include "inet_ntop.h"
77 #include "curl_threads.h"
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
82 #include "curl_memory.h"
83 /* The last #include file should be: */
86 /***********************************************************************
87 * Only for threaded name resolves builds
88 **********************************************************************/
89 #ifdef CURLRES_THREADED
91 /* This function is used to init a threaded resolve */
92 static bool init_resolve_thread(struct connectdata *conn,
93 const char *hostname, int port,
94 const struct addrinfo *hints);
97 /* Data for synchronization between resolver thread and its parent */
98 struct thread_sync_data {
102 char * hostname; /* hostname to resolve, Curl_async.hostname
107 #ifdef HAVE_GETADDRINFO
108 struct addrinfo hints;
113 curl_thread_t thread_hnd;
114 curl_socket_t dummy_sock;
115 unsigned int poll_interval;
117 struct thread_sync_data tsd;
120 static struct thread_sync_data * conn_thread_sync_data(struct connectdata *conn)
122 return &(((struct thread_data *)conn->async.os_specific)->tsd);
125 #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
127 /* Destroy resolver thread synchronization data */
129 void destroy_thread_sync_data(struct thread_sync_data * tsd)
132 Curl_mutex_destroy(tsd->mtx);
140 Curl_freeaddrinfo(tsd->res);
142 memset(tsd,0,sizeof(*tsd));
145 /* Initialize resolver thread synchronization data */
147 int init_thread_sync_data(struct thread_sync_data * tsd,
148 const char * hostname,
150 const struct addrinfo *hints)
152 memset(tsd, 0, sizeof(*tsd));
162 tsd->mtx = malloc(sizeof(curl_mutex_t));
163 if (tsd->mtx == NULL) goto err_exit;
165 Curl_mutex_init(tsd->mtx);
167 tsd->sock_error = CURL_ASYNC_SUCCESS;
169 /* Copying hostname string because original can be destroyed by parent
170 * thread during gethostbyname execution.
172 tsd->hostname = strdup(hostname);
173 if (!tsd->hostname) goto err_exit;
178 /* Memory allocation failed */
179 destroy_thread_sync_data(tsd);
183 static int getaddrinfo_complete(struct connectdata *conn)
185 struct thread_sync_data *tsd = conn_thread_sync_data(conn);
188 rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
189 /* The tsd->res structure has been copied to async.dns and perhaps the DNS cache.
190 Set our copy to NULL so destroy_thread_sync_data doesn't free it.
198 #ifdef HAVE_GETADDRINFO
201 * getaddrinfo_thread() resolves a name and then exits.
203 * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
206 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
208 struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
209 char service [NI_MAXSERV];
212 snprintf(service, sizeof(service), "%d", tsd->port);
214 rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
217 tsd->sock_error = SOCKERRNO;
218 if (tsd->sock_error == 0)
219 tsd->sock_error = ENOMEM;
222 Curl_mutex_acquire(tsd->mtx);
224 Curl_mutex_release(tsd->mtx);
229 #else /* HAVE_GETADDRINFO */
232 * gethostbyname_thread() resolves a name and then exits.
234 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
236 struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
238 tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
241 tsd->sock_error = SOCKERRNO;
242 if (tsd->sock_error == 0)
243 tsd->sock_error = ENOMEM;
246 Curl_mutex_acquire(tsd->mtx);
248 Curl_mutex_release(tsd->mtx);
253 #endif /* HAVE_GETADDRINFO */
256 * Curl_destroy_thread_data() cleans up async resolver data and thread handle.
257 * Complementary of ares_destroy.
259 void Curl_destroy_thread_data (struct Curl_async *async)
262 free(async->hostname);
264 if(async->os_specific) {
265 struct thread_data *td = (struct thread_data*) async->os_specific;
267 if (td->dummy_sock != CURL_SOCKET_BAD)
268 sclose(td->dummy_sock);
270 if (td->thread_hnd != curl_thread_t_null)
271 Curl_thread_join(&td->thread_hnd);
273 destroy_thread_sync_data(&td->tsd);
275 free(async->os_specific);
277 async->hostname = NULL;
278 async->os_specific = NULL;
282 * init_resolve_thread() starts a new thread that performs the actual
283 * resolve. This function returns before the resolve is done.
285 * Returns FALSE in case of failure, otherwise TRUE.
287 static bool init_resolve_thread (struct connectdata *conn,
288 const char *hostname, int port,
289 const struct addrinfo *hints)
291 struct thread_data *td = calloc(1, sizeof(struct thread_data));
294 conn->async.os_specific = (void*) td;
298 conn->async.port = port;
299 conn->async.done = FALSE;
300 conn->async.status = 0;
301 conn->async.dns = NULL;
302 td->dummy_sock = CURL_SOCKET_BAD;
303 td->thread_hnd = curl_thread_t_null;
305 if (!init_thread_sync_data(&td->tsd, hostname, port, hints))
308 Curl_safefree(conn->async.hostname);
309 conn->async.hostname = strdup(hostname);
310 if(!conn->async.hostname)
314 /* This socket is only to keep Curl_resolv_fdset() and select() happy;
315 * should never become signalled for read since it's unbound but
316 * Windows needs at least 1 socket in select().
318 td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
319 if (td->dummy_sock == CURL_SOCKET_BAD)
323 #ifdef HAVE_GETADDRINFO
324 td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
326 td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
329 if(!td->thread_hnd) {
339 Curl_destroy_thread_data(&conn->async);
348 * Curl_wait_for_resolv() waits for a resolve to finish. This function should
349 * be avoided since using this risk getting the multi interface to "hang".
351 * If 'entry' is non-NULL, make it point to the resolved dns entry
353 * This is the version for resolves-in-a-thread.
355 CURLcode Curl_wait_for_resolv(struct connectdata *conn,
356 struct Curl_dns_entry **entry)
358 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
359 struct SessionHandle *data = conn->data;
360 CURLcode rc = CURLE_OK;
362 DEBUGASSERT(conn && td);
364 /* wait for the thread to resolve the name */
365 if (Curl_thread_join(&td->thread_hnd)) {
366 rc = getaddrinfo_complete(conn);
371 conn->async.done = TRUE;
374 *entry = conn->async.dns;
376 if(!conn->async.dns) {
377 /* a name was not resolved */
378 if (conn->bits.httpproxy) {
379 failf(data, "Could not resolve proxy: %s; %s",
380 conn->async.hostname, Curl_strerror(conn, conn->async.status));
381 rc = CURLE_COULDNT_RESOLVE_PROXY;
383 failf(data, "Could not resolve host: %s; %s",
384 conn->async.hostname, Curl_strerror(conn, conn->async.status));
385 rc = CURLE_COULDNT_RESOLVE_HOST;
389 Curl_destroy_thread_data(&conn->async);
392 conn->bits.close = TRUE;
398 * Curl_is_resolved() is called repeatedly to check if a previous name resolve
399 * request has completed. It should also make sure to time-out if the
400 * operation seems to take too long.
402 CURLcode Curl_is_resolved(struct connectdata *conn,
403 struct Curl_dns_entry **entry)
405 struct SessionHandle *data = conn->data;
406 struct thread_data *td = (struct thread_data*) conn->async.os_specific;
413 return CURLE_COULDNT_RESOLVE_HOST;
416 Curl_mutex_acquire(td->tsd.mtx);
418 Curl_mutex_release(td->tsd.mtx);
421 getaddrinfo_complete(conn);
422 Curl_destroy_thread_data(&conn->async);
424 if(!conn->async.dns) {
425 failf(data, "Could not resolve host: %s; %s",
426 conn->host.name, Curl_strerror(conn, conn->async.status));
427 return CURLE_COULDNT_RESOLVE_HOST;
429 *entry = conn->async.dns;
431 /* poll for name lookup done with exponential backoff up to 250ms */
432 int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
436 if (td->poll_interval == 0)
437 /* Start at 1ms poll interval */
438 td->poll_interval = 1;
439 else if (elapsed >= td->interval_end)
440 /* Back-off exponentially if last interval expired */
441 td->poll_interval *= 2;
443 if (td->poll_interval > 250)
444 td->poll_interval = 250;
446 td->interval_end = elapsed + td->poll_interval;
447 Curl_expire(conn->data, td->poll_interval);
453 int Curl_resolv_getsock(struct connectdata *conn,
454 curl_socket_t *socks,
457 const struct thread_data *td =
458 (const struct thread_data *) conn->async.os_specific;
460 if(td && td->dummy_sock != CURL_SOCKET_BAD) {
462 /* return one socket waiting for readable, even though this is just
464 socks[0] = td->dummy_sock;
465 return GETSOCK_READSOCK(0);
471 #ifndef HAVE_GETADDRINFO
473 * Curl_getaddrinfo() - for platforms without getaddrinfo
475 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
476 const char *hostname,
482 *waitp = 0; /* default to synchronous response */
484 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
485 /* This is a dotted IP address 123.123.123.123-style */
486 return Curl_ip2addr(AF_INET, &in, hostname, port);
488 /* fire up a new resolver thread! */
489 if(init_resolve_thread(conn, hostname, port, NULL)) {
490 *waitp = 1; /* expect asynchronous response */
494 /* fall-back to blocking version */
495 return Curl_ipv4_resolve_r(hostname, port);
498 #else /* !HAVE_GETADDRINFO */
501 * Curl_getaddrinfo() - for getaddrinfo
503 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
504 const char *hostname,
508 struct addrinfo hints;
511 char sbuf[NI_MAXSERV];
513 struct SessionHandle *data = conn->data;
515 *waitp = 0; /* default to synchronous response */
519 * Check if a limited name resolve has been requested.
521 switch(conn->ip_version) {
522 case CURL_IPRESOLVE_V4:
525 case CURL_IPRESOLVE_V6:
534 /* see if we have an IPv6 stack */
535 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
536 if(s == CURL_SOCKET_BAD) {
537 /* Some non-IPv6 stacks have been found to make very slow name resolves
538 * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
539 * the stack seems to be a non-ipv6 one. */
544 /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
545 * possible checks. And close the socket again.
550 #endif /* !CURLRES_IPV4 */
552 memset(&hints, 0, sizeof(hints));
553 hints.ai_family = pf;
554 hints.ai_socktype = conn->socktype;
556 snprintf(sbuf, sizeof(sbuf), "%d", port);
558 /* fire up a new resolver thread! */
559 if(init_resolve_thread(conn, hostname, port, &hints)) {
560 *waitp = 1; /* expect asynchronous response */
564 /* fall-back to blocking version */
565 infof(data, "init_resolve_thread() failed for %s; %s\n",
566 hostname, Curl_strerror(conn, ERRNO));
568 error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
570 infof(data, "getaddrinfo() failed for %s:%d; %s\n",
571 hostname, port, Curl_strerror(conn, SOCKERRNO));
577 #endif /* !HAVE_GETADDRINFO */
579 #endif /* CURLRES_THREADED */