diff options
author | Daniel Stenberg <daniel@haxx.se> | 2011-01-30 01:00:52 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2011-04-25 19:47:16 +0200 |
commit | 24d84da0731f680a9b19653a450d4843f190d6b8 (patch) | |
tree | b6a554ace49210a949d623bf7c97c8564bb9e41d | |
parent | ca015f1a45c68aa1d641678cfc13ce0df0c58fe0 (diff) | |
download | curl-24d84da0731f680a9b19653a450d4843f190d6b8.tar.gz |
asynch resolvers: cleanup
Fixed indents, coding conventions and white space edits.
Modified the c-ares completion callback function to again NOT read the
conn data when the ares handle is being taken down as then it may have
been freed already.
-rw-r--r-- | lib/easy.c | 7 | ||||
-rw-r--r-- | lib/hostares.c | 141 | ||||
-rw-r--r-- | lib/hostip.h | 40 | ||||
-rw-r--r-- | lib/hostsyn.c | 34 | ||||
-rw-r--r-- | lib/hostthre.c | 40 | ||||
-rw-r--r-- | lib/url.c | 3 |
6 files changed, 135 insertions, 130 deletions
diff --git a/lib/easy.c b/lib/easy.c index 9ce80d086..cfe7224a4 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -269,7 +269,7 @@ CURLcode curl_global_init(long flags) idna_init(); #endif - if( Curl_resolver_global_init() != CURLE_OK ) { + if(Curl_resolver_global_init() != CURLE_OK) { DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n")); return CURLE_FAILED_INIT; } @@ -673,8 +673,9 @@ CURL *curl_easy_duphandle(CURL *incurl) } /* Clone the resolver handle, if present, for the new handle */ - if( Curl_resolver_duphandle(&outcurl->state.resolver, data->state.resolver) != CURLE_OK ) - goto fail; + if( Curl_resolver_duphandle(&outcurl->state.resolver, + data->state.resolver) != CURLE_OK ) + goto fail; Curl_convert_setup(outcurl); diff --git a/lib/hostares.c b/lib/hostares.c index 1b6978d51..cc60016e1 100644 --- a/lib/hostares.c +++ b/lib/hostares.c @@ -104,11 +104,11 @@ struct ResolverResults { }; /* - * Curl_resolver_global_init() - the generic low-level asynchronous name resolve API. - * Called from curl_global_init() to initialize global resolver environment. - * Initializes ares library. + * Curl_resolver_global_init() - the generic low-level asynchronous name + * resolve API. Called from curl_global_init() to initialize global resolver + * environment. Initializes ares library. */ -int Curl_resolver_global_init() +int Curl_resolver_global_init(void) { #ifdef CARES_HAVE_ARES_LIBRARY_INIT if(ares_library_init(ARES_LIB_INIT_ALL)) { @@ -119,11 +119,12 @@ int Curl_resolver_global_init() } /* - * Curl_resolver_global_cleanup() - the generic low-level asynchronous name resolve API. + * Curl_resolver_global_cleanup() + * * Called from curl_global_cleanup() to destroy global resolver environment. * Deinitializes ares library. */ -void Curl_resolver_global_cleanup() +void Curl_resolver_global_cleanup(void) { #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP ares_library_cleanup(); @@ -131,10 +132,11 @@ void Curl_resolver_global_cleanup() } /* - * Curl_resolver_init() - the generic low-level name resolve API. - * Called from curl_easy_init() -> Curl_open() to initialize resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Fills the passed pointer by the initialized ares_channel. + * Curl_resolver_init() + * + * Called from curl_easy_init() -> Curl_open() to initialize resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Fills the passed pointer by the initialized ares_channel. */ int Curl_resolver_init(void **resolver) { @@ -151,10 +153,11 @@ int Curl_resolver_init(void **resolver) } /* - * Curl_resolver_cleanup() - the generic low-level name resolve API. - * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Destroys the ares channel. + * Curl_resolver_cleanup() + * + * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Destroys the ares channel. */ void Curl_resolver_cleanup(void *resolver) { @@ -162,10 +165,11 @@ void Curl_resolver_cleanup(void *resolver) } /* - * Curl_resolver_duphandle() - the generic low-level name resolve API. - * Called from curl_easy_duphandle() to duplicate resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Duplicates the 'from' ares channel and passes the resulting channel to the 'to' pointer. + * Curl_resolver_duphandle() + * + * Called from curl_easy_duphandle() to duplicate resolver URL-state specific + * environment ('resolver' member of the UrlState structure). Duplicates the + * 'from' ares channel and passes the resulting channel to the 'to' pointer. */ int Curl_resolver_duphandle(void **to, void *from) { @@ -197,11 +201,11 @@ static void destroy_async_data (struct Curl_async *async) if(async->os_specific) { struct ResolverResults *res = (struct ResolverResults *)async->os_specific; if( res ) { - if( res->temp_ai ) { - Curl_freeaddrinfo(res->temp_ai); - res->temp_ai = NULL; - } - free(res); + if( res->temp_ai ) { + Curl_freeaddrinfo(res->temp_ai); + res->temp_ai = NULL; + } + free(res); } async->os_specific = NULL; } @@ -233,7 +237,8 @@ int Curl_resolv_getsock(struct connectdata *conn, maxtime.tv_sec = CURL_TIMEOUT_RESOLVE; maxtime.tv_usec = 0; - timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime, &timebuf); + timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime, + &timebuf); Curl_expire(conn->data, (timeout->tv_sec * 1000) + (timeout->tv_usec/1000)); @@ -261,7 +266,8 @@ static int waitperform(struct connectdata *conn, int timeout_ms) int i; int num = 0; - bitmask = ares_getsock((ares_channel)data->state.resolver, socks, ARES_GETSOCK_MAXNUM); + bitmask = ares_getsock((ares_channel)data->state.resolver, socks, + ARES_GETSOCK_MAXNUM); for(i=0; i < ARES_GETSOCK_MAXNUM; i++) { pfd[i].events = 0; @@ -288,7 +294,8 @@ static int waitperform(struct connectdata *conn, int timeout_ms) if(!nfds) /* Call ares_process() unconditonally here, even if we simply timed out above, as otherwise the ares name resolve won't timeout! */ - ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD, ARES_SOCKET_BAD); + ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD, + ARES_SOCKET_BAD); else { /* move through the descriptors and ask for processing on them */ for(i=0; i < num; i++) @@ -312,7 +319,8 @@ CURLcode Curl_is_resolved(struct connectdata *conn, struct Curl_dns_entry **dns) { struct SessionHandle *data = conn->data; - struct ResolverResults *res = (struct ResolverResults *)conn->async.os_specific; + struct ResolverResults *res = (struct ResolverResults *) + conn->async.os_specific; *dns = NULL; @@ -320,7 +328,8 @@ CURLcode Curl_is_resolved(struct connectdata *conn, if( res && !res->num_pending ) { (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai); - /* temp_ai ownership is moved to the connection, so we need not free-up them */ + /* temp_ai ownership is moved to the connection, so we need not free-up + them */ res->temp_ai = NULL; destroy_async_data(&conn->async); if(!conn->async.dns) { @@ -443,19 +452,20 @@ CURLcode Curl_wait_for_resolv(struct connectdata *conn, } /* Connects results to the list */ -static void ares_compound_results(struct ResolverResults *res, Curl_addrinfo *ai) +static void compound_results(struct ResolverResults *res, + Curl_addrinfo *ai) { - Curl_addrinfo *ai_tail; - if( !ai ) - return; - ai_tail = ai; + Curl_addrinfo *ai_tail; + if(!ai) + return; + ai_tail = ai; - while (ai_tail->ai_next) - ai_tail = ai_tail->ai_next; + while (ai_tail->ai_next) + ai_tail = ai_tail->ai_next; - /* Add the new results to the list of old results. */ - ai_tail->ai_next = res->temp_ai; - res->temp_ai = ai; + /* Add the new results to the list of old results. */ + ai_tail->ai_next = res->temp_ai; + res->temp_ai = ai; } /* @@ -463,50 +473,35 @@ static void ares_compound_results(struct ResolverResults *res, Curl_addrinfo *ai * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(), * when using ares, is completed either successfully or with failure. */ -static void ares_query_completed_cb(void *arg, /* (struct connectdata *) */ - int status, +static void query_completed_cb(void *arg, /* (struct connectdata *) */ + int status, #ifdef HAVE_CARES_CALLBACK_TIMEOUTS - int timeouts, + int timeouts, #endif - struct hostent *hostent) + struct hostent *hostent) { struct connectdata *conn = (struct connectdata *)arg; - struct ResolverResults *res = (struct ResolverResults *)conn->async.os_specific; - - if( !conn->data ) { - /* Immediately return just because the handle is destroying */ - return; - } - - if( !conn->data->magic ) { - /* Immediately return just because the handle is destroying */ - return; - } - - if( !res ) { - /* Immediately return just because the results are destroyed for some reason */ - return; - } + struct ResolverResults *res; #ifdef HAVE_CARES_CALLBACK_TIMEOUTS (void)timeouts; /* ignored */ #endif + if(ARES_EDESTRUCTION == status) + /* when this ares handle is getting destroyed, the 'arg' pointer may not + be valid so only defer it when we know the 'status' says its fine! */ + return; + + res = (struct ResolverResults *)conn->async.os_specific; res->num_pending--; - switch(status) { - case CURL_ASYNC_SUCCESS: - ares_compound_results(res,Curl_he2ai(hostent, conn->async.port)); - break; - /* this ares handle is getting destroyed, the 'arg' pointer may not be - valid! */ - /* conn->magic check instead - case ARES_EDESTRUCTION: - return; */ - default: - break; + if(CURL_ASYNC_SUCCESS == status) { + Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port); + if(ai) { + compound_results(res, ai); + } } - /* The successfull result empties any error */ + /* A successful result overwrites any previous error */ if( res->last_status != ARES_SUCCESS ) res->last_status = status; } @@ -589,9 +584,9 @@ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, /* areschannel is already setup in the Curl_open() function */ ares_gethostbyname((ares_channel)data->state.resolver, hostname, PF_INET, - ares_query_completed_cb, conn); + query_completed_cb, conn); ares_gethostbyname((ares_channel)data->state.resolver, hostname, PF_INET6, - ares_query_completed_cb, conn); + query_completed_cb, conn); } else #endif /* CURLRES_IPV6 */ @@ -600,7 +595,7 @@ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, /* areschannel is already setup in the Curl_open() function */ ares_gethostbyname((ares_channel)data->state.resolver, hostname, family, - ares_query_completed_cb, conn); + query_completed_cb, conn); } *waitp = 1; /* expect asynchronous response */ diff --git a/lib/hostip.h b/lib/hostip.h index 70ce7cd74..5c9250991 100644 --- a/lib/hostip.h +++ b/lib/hostip.h @@ -104,45 +104,53 @@ bool Curl_ipv6works(void); */ bool Curl_ipvalid(struct connectdata *conn); +/********* functions in the internal asynch resolver interface ****** */ + /* - * Curl_resolver_global_init() - the generic low-level name resolver API. + * Curl_resolver_global_init() + * * Called from curl_global_init() to initialize global resolver environment. * Returning anything else than CURLE_OK fails curl_global_init(). */ int Curl_resolver_global_init(void); /* - * Curl_resolver_global_cleanup() - the generic low-level name resolver API. + * Curl_resolver_global_cleanup() * Called from curl_global_cleanup() to destroy global resolver environment. */ void Curl_resolver_global_cleanup(void); /* - * Curl_resolver_init() - the generic low-level name resolve API. - * Called from curl_easy_init() -> Curl_open() to initialize resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Should fill the passed pointer by the initialized handler. - * Returning anything else than CURLE_OK fails curl_easy_init() with the correspondent code. + * Curl_resolver_init() + * Called from curl_easy_init() -> Curl_open() to initialize resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Should fill the passed pointer by the initialized handler. + * Returning anything else than CURLE_OK fails curl_easy_init() with the + * correspondent code. */ int Curl_resolver_init(void **resolver); /* - * Curl_resolver_cleanup() - the generic low-level name resolve API. - * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Should destroy the handler and free all resources connected to it. + * Curl_resolver_cleanup() + * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver + * URL-state specific environment ('resolver' member of the UrlState + * structure). Should destroy the handler and free all resources connected to + * it. */ void Curl_resolver_cleanup(void *resolver); /* - * Curl_resolver_duphandle() - the generic low-level name resolve API. - * Called from curl_easy_duphandle() to duplicate resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Should duplicate the 'from' handle and pass the resulting handle to the 'to' pointer. - * Returning anything else than CURLE_OK causes failed curl_easy_duphandle() call. + * Curl_resolver_duphandle() + * Called from curl_easy_duphandle() to duplicate resolver URL-state specific + * environment ('resolver' member of the UrlState structure). Should + * duplicate the 'from' handle and pass the resulting handle to the 'to' + * pointer. Returning anything else than CURLE_OK causes failed + * curl_easy_duphandle() call. */ int Curl_resolver_duphandle(void **to, void *from); +/********** end of generic resolver interface functions *****************/ + /* * Curl_getaddrinfo() is the generic low-level name resolve API within this * source file. There are several versions of this function - for different diff --git a/lib/hostsyn.c b/lib/hostsyn.c index 799aa6991..01d028979 100644 --- a/lib/hostsyn.c +++ b/lib/hostsyn.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -73,29 +73,29 @@ #ifdef CURLRES_SYNCH /* - * Curl_resolver_global_init() - the generic low-level name resolve API. + * Curl_resolver_global_init() * Called from curl_global_init() to initialize global resolver environment. * Does nothing here. */ -int Curl_resolver_global_init() +int Curl_resolver_global_init(void) { return CURLE_OK; } /* - * Curl_resolver_global_cleanup() - the generic low-level name resolve API. + * 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_global_cleanup(void) { } /* - * Curl_resolver_init() - the generic low-level name resolve API. - * Called from curl_easy_init() -> Curl_open() to initialize resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { @@ -104,10 +104,10 @@ int Curl_resolver_init(void **resolver) } /* - * Curl_resolver_cleanup() - the generic low-level name resolve API. - * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { @@ -115,10 +115,10 @@ void Curl_resolver_cleanup(void *resolver) } /* - * Curl_resolver_duphandle() - the generic low-level name resolve API. - * Called from curl_easy_duphandle() to duplicate resolver URL state-specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { diff --git a/lib/hostthre.c b/lib/hostthre.c index d91a5d77c..2313546dd 100644 --- a/lib/hostthre.c +++ b/lib/hostthre.c @@ -89,29 +89,29 @@ #ifdef CURLRES_THREADED /* - * Curl_resolver_global_init() - the generic low-level name resolve API. + * Curl_resolver_global_init() * Called from curl_global_init() to initialize global resolver environment. * Does nothing here. */ -int Curl_resolver_global_init() +int Curl_resolver_global_init(void) { return CURLE_OK; } /* - * Curl_resolver_global_cleanup() - the generic low-level name resolve API. + * 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_global_cleanup(void) { } /* - * Curl_resolver_init() - the generic low-level name resolve API. - * Called from curl_easy_init() -> Curl_open() to initialize resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { @@ -120,10 +120,10 @@ int Curl_resolver_init(void **resolver) } /* - * Curl_resolver_cleanup() - the generic low-level name resolve API. - * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver URL-state specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { @@ -131,10 +131,10 @@ void Curl_resolver_cleanup(void *resolver) } /* - * Curl_resolver_duphandle() - the generic low-level name resolve API. - * Called from curl_easy_duphandle() to duplicate resolver URL state-specific environment - * ('resolver' member of the UrlState structure). - * Does nothing here. + * 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) { @@ -181,7 +181,7 @@ struct thread_data { struct thread_sync_data tsd; }; -static struct thread_sync_data * conn_thread_sync_data(struct connectdata *conn) +static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn) { return &(((struct thread_data *)conn->async.os_specific)->tsd); } @@ -250,9 +250,9 @@ static int getaddrinfo_complete(struct connectdata *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. - */ + /* 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; @@ -778,7 +778,8 @@ CURLcode Curl_open(struct SessionHandle **curl) data->magic = CURLEASY_MAGIC_NUMBER; - if( (status=Curl_resolver_init(&data->state.resolver)) != CURLE_OK ) { + status = Curl_resolver_init(&data->state.resolver); + if(status) { DEBUGF(fprintf(stderr, "Error: resolver_init failed\n")); free(data); return status; |