From 7de2f9271c68c10ee7057c10741b0406bca6c156 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 31 Jan 2011 00:10:35 +0100 Subject: async resolvers: further cleanups asyn-ares.c and asyn-thread.c are two separate backends that implement the same (internal) async resolver API for libcurl to use. Backend is specified at build time. The internal resolver API is defined in asyn.h for asynch resolvers. --- lib/asyn-thread.c | 632 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 632 insertions(+) create mode 100644 lib/asyn-thread.c (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c new file mode 100644 index 000000000..a3cd4d896 --- /dev/null +++ b/lib/asyn-thread.c @@ -0,0 +1,632 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ + +#include "setup.h" + +#include +#include + +#ifdef HAVE_SYS_SOCKET_H +#include +#endif +#ifdef HAVE_NETINET_IN_H +#include +#endif +#ifdef HAVE_NETDB_H +#include +#endif +#ifdef HAVE_ARPA_INET_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include /* required for free() prototypes */ +#endif +#ifdef HAVE_UNISTD_H +#include /* for the close() proto */ +#endif +#ifdef __VMS +#include +#include +#include +#endif + +#if defined(USE_THREADS_POSIX) +# ifdef HAVE_PTHREAD_H +# include +# endif +#elif defined(USE_THREADS_WIN32) +# ifdef HAVE_PROCESS_H +# include +# endif +#endif + +#if (defined(NETWARE) && defined(__NOVELL_LIBC__)) +#undef in_addr_t +#define in_addr_t unsigned long +#endif + +#include "urldata.h" +#include "sendf.h" +#include "hostip.h" +#include "hash.h" +#include "share.h" +#include "strerror.h" +#include "url.h" +#include "multiif.h" +#include "inet_pton.h" +#include "inet_ntop.h" +#include "curl_threads.h" + +#define _MPRINTF_REPLACE /* use our functions only */ +#include + +#include "curl_memory.h" +/* The last #include file should be: */ +#include "memdebug.h" + +/*********************************************************************** + * Only for threaded name resolves builds + **********************************************************************/ +#ifdef CURLRES_THREADED + +/* + * Curl_resolver_global_init() + * Called from curl_global_init() to initialize global resolver environment. + * Does nothing here. + */ +int Curl_resolver_global_init(void) +{ + return CURLE_OK; +} + +/* + * Curl_resolver_global_cleanup() + * Called from curl_global_cleanup() to destroy global resolver environment. + * Does nothing here. + */ +void Curl_resolver_global_cleanup(void) +{ +} + +/* + * Curl_resolver_init() + * Called from curl_easy_init() -> Curl_open() to initialize resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Does nothing here. + */ +int Curl_resolver_init(void **resolver) +{ + (void)resolver; + return CURLE_OK; +} + +/* + * Curl_resolver_cleanup() + * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Does nothing here. + */ +void Curl_resolver_cleanup(void *resolver) +{ + (void)resolver; +} + +/* + * Curl_resolver_duphandle() + * Called from curl_easy_duphandle() to duplicate resolver URL state-specific + * environment ('resolver' member of the UrlState structure). Does nothing + * here. + */ +int Curl_resolver_duphandle(void **to, void *from) +{ + (void)to; + (void)from; + return CURLE_OK; +} + +static void destroy_async_data(struct Curl_async *); + +/* + * Cancel all possibly still on-going resolves for this connection. + */ +void Curl_resolver_cancel(struct connectdata *conn) +{ + destroy_async_data(&conn->async); +} + +/* This function is used to init a threaded resolve */ +static bool init_resolve_thread(struct connectdata *conn, + const char *hostname, int port, + const struct addrinfo *hints); + + +/* Data for synchronization between resolver thread and its parent */ +struct thread_sync_data { + curl_mutex_t * mtx; + int done; + + char * hostname; /* hostname to resolve, Curl_async.hostname + duplicate */ + int port; + int sock_error; + Curl_addrinfo *res; +#ifdef HAVE_GETADDRINFO + struct addrinfo hints; +#endif +}; + +struct thread_data { + curl_thread_t thread_hnd; + curl_socket_t dummy_sock; + unsigned int poll_interval; + int interval_end; + struct thread_sync_data tsd; +}; + +static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn) +{ + return &(((struct thread_data *)conn->async.os_specific)->tsd); +} + +#define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd); + +/* Destroy resolver thread synchronization data */ +static +void destroy_thread_sync_data(struct thread_sync_data * tsd) +{ + if (tsd->mtx) { + Curl_mutex_destroy(tsd->mtx); + free(tsd->mtx); + } + + if(tsd->hostname) + free(tsd->hostname); + + if (tsd->res) + Curl_freeaddrinfo(tsd->res); + + memset(tsd,0,sizeof(*tsd)); +} + +/* Initialize resolver thread synchronization data */ +static +int init_thread_sync_data(struct thread_sync_data * tsd, + const char * hostname, + int port, + const struct addrinfo *hints) +{ + memset(tsd, 0, sizeof(*tsd)); + + tsd->port = port; +#ifdef CURLRES_IPV6 + DEBUGASSERT(hints); + tsd->hints = *hints; +#else + (void) hints; +#endif + + tsd->mtx = malloc(sizeof(curl_mutex_t)); + if (tsd->mtx == NULL) goto err_exit; + + Curl_mutex_init(tsd->mtx); + + tsd->sock_error = CURL_ASYNC_SUCCESS; + + /* Copying hostname string because original can be destroyed by parent + * thread during gethostbyname execution. + */ + tsd->hostname = strdup(hostname); + if (!tsd->hostname) goto err_exit; + + return 1; + + err_exit: + /* Memory allocation failed */ + destroy_thread_sync_data(tsd); + return 0; +} + +static int getaddrinfo_complete(struct connectdata *conn) +{ + struct thread_sync_data *tsd = conn_thread_sync_data(conn); + int rc; + + rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res); + /* The tsd->res structure has been copied to async.dns and perhaps the DNS + cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it. + */ + tsd->res = NULL; + + return rc; +} + + +#ifdef HAVE_GETADDRINFO + +/* + * getaddrinfo_thread() resolves a name and then exits. + * + * For builds without ARES, but with ENABLE_IPV6, create a resolver thread + * and wait on it. + */ +static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg) +{ + struct thread_sync_data *tsd = (struct thread_sync_data*)arg; + char service [NI_MAXSERV]; + int rc; + + snprintf(service, sizeof(service), "%d", tsd->port); + + rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res); + + if (rc != 0) { + tsd->sock_error = SOCKERRNO; + if (tsd->sock_error == 0) + tsd->sock_error = ENOMEM; + } + + Curl_mutex_acquire(tsd->mtx); + tsd->done = 1; + Curl_mutex_release(tsd->mtx); + + return 0; +} + +#else /* HAVE_GETADDRINFO */ + +/* + * gethostbyname_thread() resolves a name and then exits. + */ +static unsigned int CURL_STDCALL gethostbyname_thread (void *arg) +{ + struct thread_sync_data *tsd = (struct thread_sync_data *)arg; + + tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port); + + if (!tsd->res) { + tsd->sock_error = SOCKERRNO; + if (tsd->sock_error == 0) + tsd->sock_error = ENOMEM; + } + + Curl_mutex_acquire(tsd->mtx); + tsd->done = 1; + Curl_mutex_release(tsd->mtx); + + return 0; +} + +#endif /* HAVE_GETADDRINFO */ + +/* + * destroy_async_data() cleans up async resolver data and thread handle. + */ +static void destroy_async_data (struct Curl_async *async) +{ + if(async->hostname) + free(async->hostname); + + if(async->os_specific) { + struct thread_data *td = (struct thread_data*) async->os_specific; + + if (td->dummy_sock != CURL_SOCKET_BAD) + sclose(td->dummy_sock); + + if (td->thread_hnd != curl_thread_t_null) + Curl_thread_join(&td->thread_hnd); + + destroy_thread_sync_data(&td->tsd); + + free(async->os_specific); + } + async->hostname = NULL; + async->os_specific = NULL; +} + +/* + * init_resolve_thread() starts a new thread that performs the actual + * resolve. This function returns before the resolve is done. + * + * Returns FALSE in case of failure, otherwise TRUE. + */ +static bool init_resolve_thread (struct connectdata *conn, + const char *hostname, int port, + const struct addrinfo *hints) +{ + struct thread_data *td = calloc(1, sizeof(struct thread_data)); + int err = ENOMEM; + + conn->async.os_specific = (void*) td; + if(!td) + goto err_exit; + + conn->async.port = port; + conn->async.done = FALSE; + conn->async.status = 0; + conn->async.dns = NULL; + td->dummy_sock = CURL_SOCKET_BAD; + td->thread_hnd = curl_thread_t_null; + + if (!init_thread_sync_data(&td->tsd, hostname, port, hints)) + goto err_exit; + + Curl_safefree(conn->async.hostname); + conn->async.hostname = strdup(hostname); + if(!conn->async.hostname) + goto err_exit; + +#ifdef WIN32 + /* This socket is only to keep Curl_resolver_fdset() and select() happy; + * should never become signalled for read since it's unbound but + * Windows needs at least 1 socket in select(). + */ + td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0); + if (td->dummy_sock == CURL_SOCKET_BAD) + goto err_exit; +#endif + +#ifdef HAVE_GETADDRINFO + td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd); +#else + td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd); +#endif + + if(!td->thread_hnd) { +#ifndef _WIN32_WCE + err = errno; +#endif + goto err_exit; + } + + return TRUE; + + err_exit: + destroy_async_data(&conn->async); + + SET_ERRNO(err); + + return FALSE; +} + + +/* + * Curl_resolver_wait_resolv() + * + * waits for a resolve to finish. This function should be avoided since using + * this risk getting the multi interface to "hang". + * + * If 'entry' is non-NULL, make it point to the resolved dns entry + * + * This is the version for resolves-in-a-thread. + */ +CURLcode Curl_resolver_wait_resolv(struct connectdata *conn, + struct Curl_dns_entry **entry) +{ + struct thread_data *td = (struct thread_data*) conn->async.os_specific; + struct SessionHandle *data = conn->data; + CURLcode rc = CURLE_OK; + + DEBUGASSERT(conn && td); + + /* wait for the thread to resolve the name */ + if (Curl_thread_join(&td->thread_hnd)) { + rc = getaddrinfo_complete(conn); + } else { + DEBUGASSERT(0); + } + + conn->async.done = TRUE; + + if(entry) + *entry = conn->async.dns; + + if(!conn->async.dns) { + /* a name was not resolved */ + if (conn->bits.httpproxy) { + failf(data, "Could not resolve proxy: %s; %s", + conn->async.hostname, Curl_strerror(conn, conn->async.status)); + rc = CURLE_COULDNT_RESOLVE_PROXY; + } else { + failf(data, "Could not resolve host: %s; %s", + conn->async.hostname, Curl_strerror(conn, conn->async.status)); + rc = CURLE_COULDNT_RESOLVE_HOST; + } + } + + destroy_async_data(&conn->async); + + if(!conn->async.dns) + conn->bits.close = TRUE; + + return (rc); +} + +/* + * Curl_resolver_is_resolved() is called repeatedly to check if a previous + * name resolve request has completed. It should also make sure to time-out if + * the operation seems to take too long. + */ +CURLcode Curl_resolver_is_resolved(struct connectdata *conn, + struct Curl_dns_entry **entry) +{ + struct SessionHandle *data = conn->data; + struct thread_data *td = (struct thread_data*) conn->async.os_specific; + int done = 0; + + *entry = NULL; + + if (!td) { + DEBUGASSERT(td); + return CURLE_COULDNT_RESOLVE_HOST; + } + + Curl_mutex_acquire(td->tsd.mtx); + done = td->tsd.done; + Curl_mutex_release(td->tsd.mtx); + + if (done) { + getaddrinfo_complete(conn); + destroy_async_data(&conn->async); + + if(!conn->async.dns) { + failf(data, "Could not resolve host: %s; %s", + conn->host.name, Curl_strerror(conn, conn->async.status)); + return CURLE_COULDNT_RESOLVE_HOST; + } + *entry = conn->async.dns; + } else { + /* poll for name lookup done with exponential backoff up to 250ms */ + int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle); + if (elapsed < 0) + elapsed = 0; + + if (td->poll_interval == 0) + /* Start at 1ms poll interval */ + td->poll_interval = 1; + else if (elapsed >= td->interval_end) + /* Back-off exponentially if last interval expired */ + td->poll_interval *= 2; + + if (td->poll_interval > 250) + td->poll_interval = 250; + + td->interval_end = elapsed + td->poll_interval; + Curl_expire(conn->data, td->poll_interval); + } + + return CURLE_OK; +} + +int Curl_resolver_getsock(struct connectdata *conn, + curl_socket_t *socks, + int numsocks) +{ + const struct thread_data *td = + (const struct thread_data *) conn->async.os_specific; + + if(td && td->dummy_sock != CURL_SOCKET_BAD) { + if(numsocks) { + /* return one socket waiting for readable, even though this is just + a dummy */ + socks[0] = td->dummy_sock; + return GETSOCK_READSOCK(0); + } + } + return 0; +} + +#ifndef HAVE_GETADDRINFO +/* + * Curl_getaddrinfo() - for platforms without getaddrinfo + */ +Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, + const char *hostname, + int port, + int *waitp) +{ + struct in_addr in; + + *waitp = 0; /* default to synchronous response */ + + if(Curl_inet_pton(AF_INET, hostname, &in) > 0) + /* This is a dotted IP address 123.123.123.123-style */ + return Curl_ip2addr(AF_INET, &in, hostname, port); + + /* fire up a new resolver thread! */ + if(init_resolve_thread(conn, hostname, port, NULL)) { + *waitp = 1; /* expect asynchronous response */ + return NULL; + } + + /* fall-back to blocking version */ + return Curl_ipv4_resolve_r(hostname, port); +} + +#else /* !HAVE_GETADDRINFO */ + +/* + * Curl_resolver_getaddrinfo() - for getaddrinfo + */ +Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, + const char *hostname, + int port, + int *waitp) +{ + struct addrinfo hints; + Curl_addrinfo *res; + int error; + char sbuf[NI_MAXSERV]; + int pf = PF_INET; + struct SessionHandle *data = conn->data; + + *waitp = 0; /* default to synchronous response */ + +#ifndef CURLRES_IPV4 + /* + * Check if a limited name resolve has been requested. + */ + switch(conn->ip_version) { + case CURL_IPRESOLVE_V4: + pf = PF_INET; + break; + case CURL_IPRESOLVE_V6: + pf = PF_INET6; + break; + default: + pf = PF_UNSPEC; + break; + } + + if((pf != PF_INET) && !Curl_ipv6works()) + /* the stack seems to be a non-ipv6 one */ + pf = PF_INET; + +#endif /* !CURLRES_IPV4 */ + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = pf; + hints.ai_socktype = conn->socktype; + + snprintf(sbuf, sizeof(sbuf), "%d", port); + + /* fire up a new resolver thread! */ + if(init_resolve_thread(conn, hostname, port, &hints)) { + *waitp = 1; /* expect asynchronous response */ + return NULL; + } + + /* fall-back to blocking version */ + infof(data, "init_resolve_thread() failed for %s; %s\n", + hostname, Curl_strerror(conn, ERRNO)); + + error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res); + if(error) { + infof(data, "getaddrinfo() failed for %s:%d; %s\n", + hostname, port, Curl_strerror(conn, SOCKERRNO)); + return NULL; + } + return res; +} + +#endif /* !HAVE_GETADDRINFO */ + +#endif /* CURLRES_THREADED */ -- cgit v1.2.1 From b903186fa0189ff241d756d25d07fdfe9885ae49 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Apr 2011 15:17:42 +0200 Subject: source cleanup: unify look, style and indent levels By the use of a the new lib/checksrc.pl script that checks that our basic source style rules are followed. --- lib/asyn-thread.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index a3cd4d896..be3b2b51e 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -193,7 +193,7 @@ static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn) static void destroy_thread_sync_data(struct thread_sync_data * tsd) { - if (tsd->mtx) { + if(tsd->mtx) { Curl_mutex_destroy(tsd->mtx); free(tsd->mtx); } @@ -201,7 +201,7 @@ void destroy_thread_sync_data(struct thread_sync_data * tsd) if(tsd->hostname) free(tsd->hostname); - if (tsd->res) + if(tsd->res) Curl_freeaddrinfo(tsd->res); memset(tsd,0,sizeof(*tsd)); @@ -225,7 +225,8 @@ int init_thread_sync_data(struct thread_sync_data * tsd, #endif tsd->mtx = malloc(sizeof(curl_mutex_t)); - if (tsd->mtx == NULL) goto err_exit; + if(tsd->mtx == NULL) + goto err_exit; Curl_mutex_init(tsd->mtx); @@ -235,7 +236,8 @@ int init_thread_sync_data(struct thread_sync_data * tsd, * thread during gethostbyname execution. */ tsd->hostname = strdup(hostname); - if (!tsd->hostname) goto err_exit; + if(!tsd->hostname) + goto err_exit; return 1; @@ -278,9 +280,9 @@ static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg) rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res); - if (rc != 0) { + if(rc != 0) { tsd->sock_error = SOCKERRNO; - if (tsd->sock_error == 0) + if(tsd->sock_error == 0) tsd->sock_error = ENOMEM; } @@ -302,9 +304,9 @@ static unsigned int CURL_STDCALL gethostbyname_thread (void *arg) tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port); - if (!tsd->res) { + if(!tsd->res) { tsd->sock_error = SOCKERRNO; - if (tsd->sock_error == 0) + if(tsd->sock_error == 0) tsd->sock_error = ENOMEM; } @@ -328,10 +330,10 @@ static void destroy_async_data (struct Curl_async *async) if(async->os_specific) { struct thread_data *td = (struct thread_data*) async->os_specific; - if (td->dummy_sock != CURL_SOCKET_BAD) + if(td->dummy_sock != CURL_SOCKET_BAD) sclose(td->dummy_sock); - if (td->thread_hnd != curl_thread_t_null) + if(td->thread_hnd != curl_thread_t_null) Curl_thread_join(&td->thread_hnd); destroy_thread_sync_data(&td->tsd); @@ -366,7 +368,7 @@ static bool init_resolve_thread (struct connectdata *conn, td->dummy_sock = CURL_SOCKET_BAD; td->thread_hnd = curl_thread_t_null; - if (!init_thread_sync_data(&td->tsd, hostname, port, hints)) + if(!init_thread_sync_data(&td->tsd, hostname, port, hints)) goto err_exit; Curl_safefree(conn->async.hostname); @@ -380,7 +382,7 @@ static bool init_resolve_thread (struct connectdata *conn, * Windows needs at least 1 socket in select(). */ td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0); - if (td->dummy_sock == CURL_SOCKET_BAD) + if(td->dummy_sock == CURL_SOCKET_BAD) goto err_exit; #endif @@ -428,11 +430,10 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn, DEBUGASSERT(conn && td); /* wait for the thread to resolve the name */ - if (Curl_thread_join(&td->thread_hnd)) { + if(Curl_thread_join(&td->thread_hnd)) rc = getaddrinfo_complete(conn); - } else { + else DEBUGASSERT(0); - } conn->async.done = TRUE; @@ -441,11 +442,12 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn, if(!conn->async.dns) { /* a name was not resolved */ - if (conn->bits.httpproxy) { + if(conn->bits.httpproxy) { failf(data, "Could not resolve proxy: %s; %s", conn->async.hostname, Curl_strerror(conn, conn->async.status)); rc = CURLE_COULDNT_RESOLVE_PROXY; - } else { + } + else { failf(data, "Could not resolve host: %s; %s", conn->async.hostname, Curl_strerror(conn, conn->async.status)); rc = CURLE_COULDNT_RESOLVE_HOST; @@ -474,7 +476,7 @@ CURLcode Curl_resolver_is_resolved(struct connectdata *conn, *entry = NULL; - if (!td) { + if(!td) { DEBUGASSERT(td); return CURLE_COULDNT_RESOLVE_HOST; } @@ -483,7 +485,7 @@ CURLcode Curl_resolver_is_resolved(struct connectdata *conn, done = td->tsd.done; Curl_mutex_release(td->tsd.mtx); - if (done) { + if(done) { getaddrinfo_complete(conn); destroy_async_data(&conn->async); @@ -493,20 +495,21 @@ CURLcode Curl_resolver_is_resolved(struct connectdata *conn, return CURLE_COULDNT_RESOLVE_HOST; } *entry = conn->async.dns; - } else { + } + else { /* poll for name lookup done with exponential backoff up to 250ms */ int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle); - if (elapsed < 0) + if(elapsed < 0) elapsed = 0; - if (td->poll_interval == 0) + if(td->poll_interval == 0) /* Start at 1ms poll interval */ td->poll_interval = 1; - else if (elapsed >= td->interval_end) + else if(elapsed >= td->interval_end) /* Back-off exponentially if last interval expired */ td->poll_interval *= 2; - if (td->poll_interval > 250) + if(td->poll_interval > 250) td->poll_interval = 250; td->interval_end = elapsed + td->poll_interval; -- cgit v1.2.1 From c33aee1667bd037068674bd354ffd0feb1d13de1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 27 Apr 2011 13:07:49 +0200 Subject: treaded-resolver: better error messages Now use gai_strerror() to get proper error messages when getaddrinfo() has failed. Detect the function in configure. Code based on work and suggestions by Jeff Pohlmeyer and Guenter Knauf --- lib/asyn-thread.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index be3b2b51e..689eb9583 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -64,6 +64,12 @@ #define in_addr_t unsigned long #endif +#ifdef HAVE_GETADDRINFO +# define RESOLVER_ENOMEM EAI_MEMORY +#else +# define RESOLVER_ENOMEM ENOMEM +#endif + #include "urldata.h" #include "sendf.h" #include "hostip.h" @@ -281,9 +287,9 @@ static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg) rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res); if(rc != 0) { - tsd->sock_error = SOCKERRNO; + tsd->sock_error = SOCKERRNO?SOCKERRNO:rc; if(tsd->sock_error == 0) - tsd->sock_error = ENOMEM; + tsd->sock_error = RESOLVER_ENOMEM; } Curl_mutex_acquire(tsd->mtx); @@ -307,7 +313,7 @@ static unsigned int CURL_STDCALL gethostbyname_thread (void *arg) if(!tsd->res) { tsd->sock_error = SOCKERRNO; if(tsd->sock_error == 0) - tsd->sock_error = ENOMEM; + tsd->sock_error = RESOLVER_ENOMEM; } Curl_mutex_acquire(tsd->mtx); @@ -355,7 +361,7 @@ static bool init_resolve_thread (struct connectdata *conn, const struct addrinfo *hints) { struct thread_data *td = calloc(1, sizeof(struct thread_data)); - int err = ENOMEM; + int err = RESOLVER_ENOMEM; conn->async.os_specific = (void*) td; if(!td) @@ -409,6 +415,24 @@ static bool init_resolve_thread (struct connectdata *conn, return FALSE; } +/* + * resolver_error() calls failf() with the appropriate message after a resolve + * error + */ + +static void resolver_error(struct connectdata *conn, const char *host_or_proxy) +{ + failf(conn->data, "Could not resolve %s: %s; %s", host_or_proxy, + conn->async.hostname, +#ifdef HAVE_GAI_STRERROR + /* NetWare doesn't have gai_strerror and on Windows it isn't deemed + thread-safe */ + gai_strerror(conn->async.status) +#else + Curl_strerror(conn, conn->async.status); +#endif + ); +} /* * Curl_resolver_wait_resolv() @@ -443,13 +467,11 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn, if(!conn->async.dns) { /* a name was not resolved */ if(conn->bits.httpproxy) { - failf(data, "Could not resolve proxy: %s; %s", - conn->async.hostname, Curl_strerror(conn, conn->async.status)); + resolver_error(conn, "proxy"); rc = CURLE_COULDNT_RESOLVE_PROXY; } else { - failf(data, "Could not resolve host: %s; %s", - conn->async.hostname, Curl_strerror(conn, conn->async.status)); + resolver_error(conn, "host"); rc = CURLE_COULDNT_RESOLVE_HOST; } } @@ -490,8 +512,7 @@ CURLcode Curl_resolver_is_resolved(struct connectdata *conn, destroy_async_data(&conn->async); if(!conn->async.dns) { - failf(data, "Could not resolve host: %s; %s", - conn->host.name, Curl_strerror(conn, conn->async.status)); + resolver_error(conn, "host"); return CURLE_COULDNT_RESOLVE_HOST; } *entry = conn->async.dns; -- cgit v1.2.1 From 5b7e1f9efede93b7bf97b31052bc0908f184db51 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 27 Apr 2011 13:20:27 +0200 Subject: gai_strerror: provide private implementation There are systems (like NetWare) without its own gai_strerror() function. --- lib/asyn-thread.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 689eb9583..da9dacc43 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -415,6 +415,44 @@ static bool init_resolve_thread (struct connectdata *conn, return FALSE; } +#if defined(HAVE_GETADDRINFO) && !defined(HAVE_GAI_STRERROR) && !defined(WIN32) +/* NetWare has getaddrinfo but lacks gai_strerror. + Windows has a gai_strerror but it is bad (not thread-safe) and the generic + socket error string function can be used for this pupose. */ +static const char *gai_strerror(int ecode) +{ + switch (ecode){ + case EAI_AGAIN: + return "The name could not be resolved at this time"; + case EAI_BADFLAGS: + return "The flags parameter had an invalid value"; + case EAI_FAIL: + return "A non-recoverable error occurred when attempting to " + "resolve the name"; + case EAI_FAMILY: + return "The address family was not recognized"; + case EAI_MEMORY: + return "Out of memory"; + case EAI_NONAME: + return "The name does not resolve for the supplied parameters"; + case EAI_SERVICE: + return "The service passed was not recognized for the " + "specified socket type" + case EAI_SOCKTYPE: + return "The intended socket type was not recognized" + case EAI_SYSTEM: + return "A system error occurred"; + case EAI_OVERFLOW: + return "An argument buffer overflowed"; + default: + return "Unknown error"; + +/* define this now as this is a private implementation of said function */ +#define HAVE_GAI_STRERROR +} +#endif + + /* * resolver_error() calls failf() with the appropriate message after a resolve * error -- cgit v1.2.1 From 3440f4d3742d913863cedd0865916c0dd47063a6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 29 Apr 2011 16:33:45 +0200 Subject: resolver_error: remove bad semicolon --- lib/asyn-thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index da9dacc43..e073d3a58 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -467,7 +467,7 @@ static void resolver_error(struct connectdata *conn, const char *host_or_proxy) thread-safe */ gai_strerror(conn->async.status) #else - Curl_strerror(conn, conn->async.status); + Curl_strerror(conn, conn->async.status) #endif ); } -- cgit v1.2.1 From fce7276f5446e7fc1003b98a9a752a3a42a0a247 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 21 May 2011 14:10:17 +0200 Subject: compiler warning: fix Fix compiler warning: enumerated type mixed with another type --- lib/asyn-thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index e073d3a58..cc444da4c 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -119,7 +119,7 @@ void Curl_resolver_global_cleanup(void) * URL-state specific environment ('resolver' member of the UrlState * structure). Does nothing here. */ -int Curl_resolver_init(void **resolver) +CURLcode Curl_resolver_init(void **resolver) { (void)resolver; return CURLE_OK; -- cgit v1.2.1 From d9e71809cbc85ec591ba78013acf0b01628275f8 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 21 Jun 2011 16:06:56 +0200 Subject: asyn-thread: fix compiler warning compiler warning: variable is initialized but not referenced --- lib/asyn-thread.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index cc444da4c..3341686d9 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -486,7 +486,6 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn, struct Curl_dns_entry **entry) { struct thread_data *td = (struct thread_data*) conn->async.os_specific; - struct SessionHandle *data = conn->data; CURLcode rc = CURLE_OK; DEBUGASSERT(conn && td); @@ -638,7 +637,6 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, int error; char sbuf[NI_MAXSERV]; int pf = PF_INET; - struct SessionHandle *data = conn->data; *waitp = 0; /* default to synchronous response */ @@ -677,12 +675,12 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, } /* fall-back to blocking version */ - infof(data, "init_resolve_thread() failed for %s; %s\n", + infof(conn->data, "init_resolve_thread() failed for %s; %s\n", hostname, Curl_strerror(conn, ERRNO)); error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res); if(error) { - infof(data, "getaddrinfo() failed for %s:%d; %s\n", + infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n", hostname, port, Curl_strerror(conn, SOCKERRNO)); return NULL; } -- cgit v1.2.1 From b680fd180bf31dd29d84f43d91b6ee56e62d452f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 4 Jul 2011 22:10:32 +0200 Subject: code style: space between close paren and open brace --- lib/asyn-thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 3341686d9..8cb92d76e 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -421,7 +421,7 @@ static bool init_resolve_thread (struct connectdata *conn, socket error string function can be used for this pupose. */ static const char *gai_strerror(int ecode) { - switch (ecode){ + switch (ecode) { case EAI_AGAIN: return "The name could not be resolved at this time"; case EAI_BADFLAGS: -- cgit v1.2.1 From ef2176109fca302ed89193716b62c3a7113552a3 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Jul 2011 04:39:43 +0200 Subject: errno.h inclusion conditionally done in setup_once.h --- lib/asyn-thread.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 8cb92d76e..0fcbc49ae 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -23,7 +23,6 @@ #include "setup.h" #include -#include #ifdef HAVE_SYS_SOCKET_H #include -- cgit v1.2.1 From f1586cb4775681810afd8e6626e7842d459f3b85 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 26 Jul 2011 17:23:27 +0200 Subject: stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h --- lib/asyn-thread.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 0fcbc49ae..d2a6dde49 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -22,8 +22,6 @@ #include "setup.h" -#include - #ifdef HAVE_SYS_SOCKET_H #include #endif @@ -36,16 +34,12 @@ #ifdef HAVE_ARPA_INET_H #include #endif -#ifdef HAVE_STDLIB_H -#include /* required for free() prototypes */ -#endif #ifdef HAVE_UNISTD_H #include /* for the close() proto */ #endif #ifdef __VMS #include #include -#include #endif #if defined(USE_THREADS_POSIX) -- cgit v1.2.1 From 704dc1844073fbd775cc67f0abe8892ef0f8c340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4gele?= Date: Sat, 6 Aug 2011 15:21:42 +0200 Subject: asyn-thread: check for dotted addresses before thread starts --- lib/asyn-thread.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index d2a6dde49..38cde5df7 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -626,14 +626,28 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, int *waitp) { struct addrinfo hints; + struct in_addr in; Curl_addrinfo *res; int error; char sbuf[NI_MAXSERV]; int pf = PF_INET; +#ifdef CURLRES_IPV6 + struct in6_addr in6; +#endif /* CURLRES_IPV6 */ *waitp = 0; /* default to synchronous response */ -#ifndef CURLRES_IPV4 + /* First check if this is an IPv4 address string */ + if(Curl_inet_pton(AF_INET, hostname, &in) > 0) + /* This is a dotted IP address 123.123.123.123-style */ + return Curl_ip2addr(AF_INET, &in, hostname, port); + +#ifdef CURLRES_IPV6 + /* check if this is an IPv6 address string */ + if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0) + /* This is an IPv6 address literal */ + return Curl_ip2addr(AF_INET6, &in6, hostname, port); + /* * Check if a limited name resolve has been requested. */ @@ -653,7 +667,7 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, /* the stack seems to be a non-ipv6 one */ pf = PF_INET; -#endif /* !CURLRES_IPV4 */ +#endif /* CURLRES_IPV6 */ memset(&hints, 0, sizeof(hints)); hints.ai_family = pf; -- cgit v1.2.1 From 8d0a504f0d34c2471393ef23fb2345c73c5d4746 Mon Sep 17 00:00:00 2001 From: Jason Glasgow Date: Tue, 12 Apr 2011 11:34:28 -0400 Subject: CURLOPT_DNS_SERVERS: set name servers if possible --- lib/asyn-thread.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 38cde5df7..cd035dc2f 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -696,4 +696,13 @@ Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn, #endif /* !HAVE_GETADDRINFO */ +CURLcode Curl_set_dns_servers(struct SessionHandle *data, + char *servers) +{ + (void)data; + (void)servers; + return CURLE_NOT_BUILT_IN; + +} + #endif /* CURLRES_THREADED */ -- cgit v1.2.1 From 4897f4e517c96797dc4333746fbf24f1cb183c7e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 3 Jan 2012 16:22:50 +0100 Subject: win32-threaded-resolver: stop using a dummy socket Previously the code would create a dummy socket while resolving just to have curl_multi_fdset() return something but the non-win32 version doesn't do it this way and the creation and use of a socket that isn't made with the common create-socket callback can be confusing to apps using the multi_socket API etc. This change removes the dummy socket and thus will cause curl_multi_fdset() to return with maxfd == -1 more often. --- lib/asyn-thread.c | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) (limited to 'lib/asyn-thread.c') diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index cd035dc2f..b02714477 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2012, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -175,7 +175,6 @@ struct thread_sync_data { struct thread_data { curl_thread_t thread_hnd; - curl_socket_t dummy_sock; unsigned int poll_interval; int interval_end; struct thread_sync_data tsd; @@ -329,9 +328,6 @@ static void destroy_async_data (struct Curl_async *async) if(async->os_specific) { struct thread_data *td = (struct thread_data*) async->os_specific; - if(td->dummy_sock != CURL_SOCKET_BAD) - sclose(td->dummy_sock); - if(td->thread_hnd != curl_thread_t_null) Curl_thread_join(&td->thread_hnd); @@ -364,7 +360,6 @@ static bool init_resolve_thread (struct connectdata *conn, conn->async.done = FALSE; conn->async.status = 0; conn->async.dns = NULL; - td->dummy_sock = CURL_SOCKET_BAD; td->thread_hnd = curl_thread_t_null; if(!init_thread_sync_data(&td->tsd, hostname, port, hints)) @@ -375,16 +370,6 @@ static bool init_resolve_thread (struct connectdata *conn, if(!conn->async.hostname) goto err_exit; -#ifdef WIN32 - /* This socket is only to keep Curl_resolver_fdset() and select() happy; - * should never become signalled for read since it's unbound but - * Windows needs at least 1 socket in select(). - */ - td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0); - if(td->dummy_sock == CURL_SOCKET_BAD) - goto err_exit; -#endif - #ifdef HAVE_GETADDRINFO td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd); #else @@ -574,17 +559,9 @@ int Curl_resolver_getsock(struct connectdata *conn, curl_socket_t *socks, int numsocks) { - const struct thread_data *td = - (const struct thread_data *) conn->async.os_specific; - - if(td && td->dummy_sock != CURL_SOCKET_BAD) { - if(numsocks) { - /* return one socket waiting for readable, even though this is just - a dummy */ - socks[0] = td->dummy_sock; - return GETSOCK_READSOCK(0); - } - } + (void)conn; + (void)socks; + (void)numsocks; return 0; } -- cgit v1.2.1