From 24d84da0731f680a9b19653a450d4843f190d6b8 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Sun, 30 Jan 2011 01:00:52 +0100
Subject: [PATCH] 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.
---
 lib/easy.c     |   7 +--
 lib/hostares.c | 141 ++++++++++++++++++++++++-------------------------
 lib/hostip.h   |  40 ++++++++------
 lib/hostsyn.c  |  34 ++++++------
 lib/hostthre.c |  40 +++++++-------
 lib/url.c      |   3 +-
 6 files changed, 135 insertions(+), 130 deletions(-)

diff --git a/lib/easy.c b/lib/easy.c
index 9ce80d0867..cfe7224a4b 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 1b6978d517..cc60016e10 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 70ce7cd743..5c92509915 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 799aa69912..01d0289792 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 d91a5d77ca..2313546dd2 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;
diff --git a/lib/url.c b/lib/url.c
index 12c70975d0..592763f898 100644
--- a/lib/url.c
+++ b/lib/url.c
@@ -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;
-- 
GitLab