]> granicus.if.org Git - curl/commitdiff
Constantine Sapuntzakis refactoring of async callbacks, allowing
authorYang Tse <yangsita@gmail.com>
Fri, 22 Jan 2010 06:36:52 +0000 (06:36 +0000)
committerYang Tse <yangsita@gmail.com>
Fri, 22 Jan 2010 06:36:52 +0000 (06:36 +0000)
removal of Curl_addrinfo_copy(), Curl_addrinfo6_callback(), and
Curl_addrinfo4_callback()

lib/hostares.c
lib/hostasyn.c
lib/hostip.c
lib/hostip.h
lib/hostip6.c
lib/hostthre.c

index 3b729315e09b37b00e636d64b745e21f1638fc49..94240a6ab9d802f3a57a4e7d4ccfa388f7df34dd 100644 (file)
@@ -304,6 +304,31 @@ CURLcode Curl_wait_for_resolv(struct connectdata *conn,
   return rc;
 }
 
+/*
+ * ares_query_completed_cb() is the callback that ares will call when
+ * 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,
+#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
+                                    int timeouts,
+#endif
+                                    struct hostent *hostent)
+{
+  struct connectdata *conn = (struct connectdata *)arg;
+  struct Curl_addrinfo * ai = NULL;
+    
+#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
+  (void)timeouts; /* ignored */
+#endif
+
+  if (status == CURL_ASYNC_SUCCESS) {
+    ai = Curl_he2ai(hostent, conn->async.port);
+  }
+  (void)Curl_addrinfo_callback(arg, status, ai);
+}
 
 /*
  * Curl_getaddrinfo() - when using ares
@@ -369,7 +394,7 @@ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
 
     /* areschannel is already setup in the Curl_open() function */
     ares_gethostbyname(data->state.areschannel, hostname, family,
-                       (ares_host_callback)Curl_addrinfo4_callback, conn);
+                       (ares_host_callback)ares_query_completed_cb, conn);
 
     *waitp = TRUE; /* please wait for the response */
   }
index 715b0da4bebd93f713fb8ca35891dcdf35bdea6f..c1cab550448605506f2e33c6d6234b30fd2a4ae4 100644 (file)
  * Only for builds using asynchronous name resolves
  **********************************************************************/
 #ifdef CURLRES_ASYNCH
+
 /*
- * addrinfo_callback() gets called by ares, gethostbyname_thread() or
- * getaddrinfo_thread() when we got the name resolved (or not!).
- *
- * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
- * address field since it might be freed when this function returns. This
- * operation stores the resolved data in the DNS cache.
+ * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
+ * or getaddrinfo_thread() when we got the name resolved (or not!).
  *
- * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
- * pointer it is given as argument!
+ * If the status argument is CURL_ASYNC_SUCCESS, this function takes
+ * ownership of the Curl_addrinfo passed, storing the resolved data
+ * in the DNS cache.
  *
  * The storage operation locks and unlocks the DNS cache.
  */
-static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
-                                  int status,
-                                  void *addr)
+CURLcode Curl_addrinfo_callback(struct connectdata * conn,
+                                int status,
+                                struct Curl_addrinfo *ai)
 {
-  struct connectdata *conn = (struct connectdata *)arg;
   struct Curl_dns_entry *dns = NULL;
   CURLcode rc = CURLE_OK;
 
   conn->async.status = status;
 
   if(CURL_ASYNC_SUCCESS == status) {
-
-    /*
-     * IPv4/ares: Curl_addrinfo_copy() copies the address and returns an
-     * allocated version.
-     *
-     * IPv6: Curl_addrinfo_copy() returns the input pointer!
-     */
-    Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
     if(ai) {
       struct SessionHandle *data = conn->data;
 
@@ -138,35 +127,4 @@ static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
   return rc;
 }
 
-CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
-                                 int status,
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-                                 int timeouts,
-#endif
-                                 struct hostent *hostent)
-{
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-  (void)timeouts; /* ignored */
-#endif
-  return addrinfo_callback(arg, status, hostent);
-}
-
-#ifdef CURLRES_IPV6
-CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
-                                 int status,
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-                                 int timeouts,
-#endif
-                                 Curl_addrinfo *ai)
-{
- /* NOTE: for CURLRES_ARES, the 'ai' argument is really a
-  * 'struct hostent' pointer.
-  */
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-  (void)timeouts; /* ignored */
-#endif
-  return addrinfo_callback(arg, status, ai);
-}
-#endif
-
-#endif /* CURLRES_ASYNC */
+#endif /* CURLRES_ASYNCH */
index fc17de6730619632737137790295e20160745930..7c72c05ed3613f35dc37eb5bf2ae0fe547c8ff04 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2010, 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
@@ -712,20 +712,4 @@ struct curl_hash *Curl_mk_dnscache(void)
   return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
 }
 
-#ifdef CURLRES_ADDRINFO_COPY
 
-/* align on even 64bit boundaries */
-#define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
-
-/*
- * Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
- * returns a pointer to the malloc()ed copy. You need to call free() on the
- * returned buffer when you're done with it.
- */
-Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
-{
-  const struct hostent *orig = org;
-
-  return Curl_he2ai(orig, port);
-}
-#endif /* CURLRES_ADDRINFO_COPY */
index b6aef623a5b25f44e2e7ec65b7d7fe893053972b..e987f301edefb344f74ff12e231f3abc5d601e70 100644 (file)
@@ -7,7 +7,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2010, 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
 #define CURLRES_IPV4
 #endif
 
-#if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
-#if !defined(HAVE_GETHOSTBYNAME_R) || defined(CURLRES_ASYNCH)
-/* If built for ipv4 and missing gethostbyname_r(), or if using async name
-   resolve, we need the Curl_addrinfo_copy() function (which itself needs the
-   Curl_he2ai() function)) */
-#define CURLRES_ADDRINFO_COPY
-#endif
-#endif /* IPv4/ares-only */
-
 #ifndef CURLRES_ASYNCH
 #define CURLRES_SYNCH
 #endif
 
-#ifndef USE_LIBIDN
-#define CURLRES_IDN
-#endif
-
 /* Allocate enough memory to hold the full name information structs and
  * everything. OSF1 is known to require at least 8872 bytes. The buffer
  * required for storing all possible aliases and IP numbers is according to
@@ -203,26 +190,15 @@ int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
                        int line, const char *source);
 #endif
 
-/* This is the callback function that is used when we build with asynch
-   resolve, ipv4 */
-CURLcode Curl_addrinfo4_callback(void *arg,
-                                 int status,
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-                                 int timeouts,
-#endif
-                                 struct hostent *hostent);
-/* This is the callback function that is used when we build with asynch
-   resolve, ipv6 */
-CURLcode Curl_addrinfo6_callback(void *arg,
-                                 int status,
-#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
-                                 int timeouts,
-#endif
-                                 Curl_addrinfo *ai);
-
-
-/* Clone a Curl_addrinfo struct, works protocol independently */
-Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port);
+/*
+ * Curl_addrinfo_callback() is used when we build with any asynch specialty.
+ * Handles end of async request processing. Inserts ai into hostcache when
+ * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
+ * request completed wether successfull or failed.
+ */
+CURLcode Curl_addrinfo_callback(struct connectdata *conn,
+                                int status,
+                                Curl_addrinfo *ai);
 
 /*
  * Curl_printable_address() returns a printable version of the 1st address
index 518133365566d955f82dbf1e51da50cea2359a25..ba50a0360b6b69c04e1f319683a03684a9961940 100644 (file)
  **********************************************************************/
 #ifdef CURLRES_IPV6
 
-#ifndef CURLRES_ARES
-#ifdef CURLRES_ASYNCH
-/*
- * Curl_addrinfo_copy() is used by the asynch callback to copy a given
- * address. But this is an ipv6 build and then we don't copy the address, we
- * just return the same pointer!
- */
-Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port)
-{
-  (void) port;
-  return (Curl_addrinfo*)orig;
-}
-#endif  /* CURLRES_ASYNCH */
-#endif  /* CURLRES_ARES */
 
 #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
 /* These are strictly for memory tracing and are using the same style as the
@@ -249,5 +235,5 @@ Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
   return res;
 }
 #endif /* !USE_THREADING_GETADDRINFO && !CURLRES_ARES */
-#endif /* ipv6 */
+#endif /* CURLRES_IPV6 */
 
index e41653b27d35c53b44402960b3a380c7cc9683da..32e946ccb1ab4fbc6d2f33e965054e079e11a984 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2010, 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
@@ -219,7 +219,7 @@ void release_thread_sync(struct thread_sync_data * tsd)
 
 #if defined(CURLRES_IPV4)
 /*
- * gethostbyname_thread() resolves a name, calls the Curl_addrinfo4_callback
+ * gethostbyname_thread() resolves a name, calls the Curl_addrinfo_callback
  * and then exits.
  *
  * For builds without ARES/ENABLE_IPV6, create a resolver thread and wait on
@@ -254,14 +254,16 @@ static unsigned __stdcall gethostbyname_thread (void *arg)
 
   /* is parent thread waiting for us and are we able to access conn members? */
   if(acquire_thread_sync(&tsd)) {
+    Curl_addrinfo *ai = Curl_he2ai(he, conn->async.port);
+
     /* Mark that we have obtained the information, and that we are calling
      * back with it. */
     SetEvent(td->event_resolved);
-    if(he) {
-      rc = Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
+    if(ai) {
+      rc = Curl_addrinfo_callback(conn, CURL_ASYNC_SUCCESS, ai);
     }
     else {
-      rc = Curl_addrinfo4_callback(conn, SOCKERRNO, NULL);
+      rc = Curl_addrinfo_callback(conn, SOCKERRNO, NULL);
     }
     release_thread_sync(&tsd);
   }
@@ -276,7 +278,7 @@ static unsigned __stdcall gethostbyname_thread (void *arg)
 #elif defined(CURLRES_IPV6)
 
 /*
- * getaddrinfo_thread() resolves a name, calls Curl_addrinfo6_callback and then
+ * getaddrinfo_thread() resolves a name, calls Curl_addrinfo_callback and then
  * exits.
  *
  * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
@@ -320,10 +322,10 @@ static unsigned __stdcall getaddrinfo_thread (void *arg)
     SetEvent(td->event_resolved);
 
     if(rc == 0) {
-      rc = Curl_addrinfo6_callback(conn, CURL_ASYNC_SUCCESS, res);
+      rc = Curl_addrinfo_callback(conn, CURL_ASYNC_SUCCESS, res);
     }
     else {
-      rc = Curl_addrinfo6_callback(conn, SOCKERRNO, NULL);
+      rc = Curl_addrinfo_callback(conn, SOCKERRNO, NULL);
     }
     release_thread_sync(&tsd);
   }