]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
getaddrinfo() cleanups
authorDaniel Stenberg <daniel@haxx.se>
Thu, 4 Oct 2001 13:25:12 +0000 (13:25 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 4 Oct 2001 13:25:12 +0000 (13:25 +0000)
lib/ftp.c
lib/hostip.c
lib/hostip.h
lib/memdebug.h
lib/url.c

index 4fde250590bd01e7360786284330979a88c836c9..28e0479e7919e001a21e9869b79f45ab66c4c2fd 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -906,7 +906,8 @@ CURLcode ftp_use_port(struct connectdata *conn)
     2.1.X doesn't do*/
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_PASSIVE;
-  if (getaddrinfo(hbuf, "0", &hints, &res))
+
+  if (getaddrinfo(hbuf, (char *)"0", &hints, &res))
     return CURLE_FTP_PORT_FAILED;
   
   portsock = -1;
@@ -929,16 +930,15 @@ CURLcode ftp_use_port(struct connectdata *conn)
     
     break;
   }
+  freeaddrinfo(res);
   if (portsock < 0) {
     failf(data, strerror(errno));
-    freeaddrinfo(res);
     return CURLE_FTP_PORT_FAILED;
   }
 
   sslen = sizeof(ss);
   if (getsockname(portsock, sa, &sslen) < 0) {
     failf(data, strerror(errno));
-    freeaddrinfo(res);
     return CURLE_FTP_PORT_FAILED;
   }
 
@@ -1047,7 +1047,6 @@ CURLcode ftp_use_port(struct connectdata *conn)
   
   if (!*modep) {
     sclose(portsock);
-    freeaddrinfo(res);
     return CURLE_FTP_PORT_FAILED;
   }
   /* we set the secondary socket variable to this for now, it
@@ -1134,7 +1133,7 @@ CURLcode ftp_use_port(struct connectdata *conn)
     }
     if(hostdataptr)
       /* free the memory used for name lookup */
-      free(hostdataptr);
+      Curl_freeaddrinfo(hostdataptr);
   }
   else {
     failf(data, "could't find my own IP address (%s)", myhost);
@@ -1285,7 +1284,7 @@ CURLcode ftp_use_pasv(struct connectdata *conn)
       ftp_pasv_verbose(conn, conninfo, newhost, connectport);
        
     if(hostdataptr)
-      free(hostdataptr);
+      Curl_freeaddrinfo(hostdataptr);
 
     if(CURLE_OK != result)
       return result;
index 4fb25bd471f89e5ab2deefdefd757a4507710910..5bea046b558b401b1ec523a52eb620abd4e56152 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___ 
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 2001, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * In order to be useful for every potential user, curl and libcurl are
  * dual-licensed under the MPL and the MIT/X-derivate licenses.
 /* --- resolve name or IP-number --- */
 
 #ifdef ENABLE_IPV6
+
+#ifdef MALLOCDEBUG
+/* These two are strictly for memory tracing and are using the same
+ * style as the family otherwise present in memdebug.c. I put these ones
+ * here since they require a bunch of struct types I didn't wanna include
+ * in memdebug.c
+ */
+int curl_getaddrinfo(char *hostname, char *service,
+                     struct addrinfo *hints,
+                     struct addrinfo **result,
+                     int line, const char *source)
+{
+  int res=(getaddrinfo)(hostname, service, hints, result);
+  if(0 == res)
+    /* success */
+    fprintf(logfile?logfile:stderr, "ADDR %s:%d getaddrinfo() = %p\n",
+            source, line, *result);
+  else
+    fprintf(logfile?logfile:stderr, "ADDR %s:%d getaddrinfo() failed\n",
+            source, line);
+  return res;
+}
+void curl_freeaddrinfo(struct addrinfo *freethis,
+                       int line, const char *source)
+{
+  (freeaddrinfo)(freethis);
+  fprintf(logfile?logfile:stderr, "ADDR %s:%d freeaddrinfo(%p)\n",
+          source, line, freethis);
+}
+
+#endif
+
+/*
+ * This is a wrapper function for freeing name information in a protocol
+ * independent way. This takes care of using the appropriate underlaying
+ * proper function.
+ */
+void Curl_freeaddrinfo(void *freethis)
+{
+#ifdef ENABLE_IPV6
+  freeaddrinfo(freethis);
+#else
+  free(freethis);
+#endif
+}
+
+/*
+ * Return name information about the given hostname and port number. If
+ * successful, the 'addrinfo' is returned and the forth argument will point to
+ * memory we need to free after use. That meory *MUST* be freed with
+ * Curl_freeaddrinfo(), nothing else.
+ */
 Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
                                 char *hostname,
                                 int port,
@@ -78,8 +130,6 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
   int error;
   char sbuf[NI_MAXSERV];
 
-  *bufp=NULL; /* pointer unused with IPv6 */
-
   memset(&hints, 0, sizeof(hints));
   hints.ai_family = PF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
@@ -87,9 +137,11 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
   snprintf(sbuf, sizeof(sbuf), "%d", port);
   error = getaddrinfo(hostname, sbuf, &hints, &res);
   if (error) {
-    infof(data, "getaddrinfo(3) failed for %s\n", hostname);
+    infof(data, "getaddrinfo(3) failed for %s\n", hostname);    
     return NULL;
   }
+  *bufp=(char *)res; /* make it point to the result struct */
+
   return res;
 }
 #else /* following code is IPv4-only */
index 86272b30ea10ddc06cfebc96600ed2cc7acfe0e8..291b9cc71b9fb9c7111c6d1b1f5c71e23931c0b9 100644 (file)
@@ -27,9 +27,21 @@ struct addrinfo;
 struct hostent;
 struct SessionHandle;
 
+/* Get name info */
 Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
                                 char *hostname,
                                 int port,
                                 char **bufp);
+/* free name info */
+void Curl_freeaddrinfo(void *freethis);
+
+#ifdef MALLOCDEBUG
+void curl_freeaddrinfo(struct addrinfo *freethis,
+                       int line, const char *source);
+int curl_getaddrinfo(char *hostname, char *service,
+                     struct addrinfo *hints,
+                     struct addrinfo **result,
+                     int line, const char *source);
+#endif
 
 #endif
index 2a6a35ac2e7f9f3965010b6dcdc4913a3620881b..ebb240928e8aa9514d30ae303a7820666fb46d50 100644 (file)
@@ -6,6 +6,8 @@
 #include <memory.h>
 #endif
 
+extern FILE *logfile;
+
 /* memory functions */
 void *curl_domalloc(size_t size, int line, const char *source);
 void *curl_dorealloc(void *ptr, size_t size, int line, const char *source);
@@ -35,6 +37,11 @@ int curl_fclose(FILE *file, int line, const char *source);
 #define accept(sock,addr,len)\
  curl_accept(sock,addr,len,__LINE__,__FILE__)
 
+#define getaddrinfo(host,serv,hint,res) \
+  curl_getaddrinfo(host,serv,hint,res,__LINE__,__FILE__)
+#define freeaddrinfo(data) \
+  curl_freeaddrinfo(data,__LINE__,__FILE__)
+
 /* sclose is probably already defined, redefine it! */
 #undef sclose
 #define sclose(sockfd) curl_sclose(sockfd,__LINE__,__FILE__)
index 198170dce99ba67be619fbb3851e9f2c0df731a5..ddbdc3b16aa481149f1e667d17d8769e0b3d71ee 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -879,13 +879,8 @@ CURLcode Curl_disconnect(struct connectdata *conn)
   if(conn->proto.generic)
     free(conn->proto.generic);
 
-#ifdef ENABLE_IPV6
-  if(conn->hostaddr) /* host name info */
-    freeaddrinfo(conn->hostaddr);
-#else
   if(conn->hostent_buf) /* host name info */
-    free(conn->hostent_buf);
-#endif
+    Curl_freeaddrinfo(conn->hostent_buf);
 
   if(conn->newurl)
     free(conn->newurl);