]> granicus.if.org Git - curl/commitdiff
GetHost() changed function arguments
authorDaniel Stenberg <daniel@haxx.se>
Thu, 21 Sep 2000 08:47:48 +0000 (08:47 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 21 Sep 2000 08:47:48 +0000 (08:47 +0000)
lib/hostip.c
lib/hostip.h

index e5eecf705fbb1d8064780695150dc5bb3b925553..4989314bba6d480fb0e6a7f3e5a28c03731e0287 100644 (file)
@@ -103,13 +103,22 @@ char *MakeIP(unsigned long num,char *addr, int addr_len)
 #endif
 struct hostent *GetHost(struct UrlData *data,
                         char *hostname,
-                        char *buf,
-                        int buf_size )
+                        char **bufp)
 {
   struct hostent *h = NULL;
   unsigned long in;
   int ret;
 
+#define CURL_NAMELOOKUP_SIZE 9000
+
+  /* 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
+   * Stevens' Unix Network Programming 2nd editor, p. 304: 8192 bytes! */
+  char *buf = (char *)malloc(CURL_NAMELOOKUP_SIZE);
+  if(!buf)
+    return NULL; /* major failure */
+
   if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
     struct in_addr *addrentry;
 
@@ -123,18 +132,20 @@ struct hostent *GetHost(struct UrlData *data,
     h->h_length = sizeof(*addrentry);
     h->h_name = *(h->h_addr_list) + h->h_length;
     /* bad one h->h_name = (char*)(h->h_addr_list + h->h_length); */
-    MakeIP(ntohl(in),h->h_name,buf_size - (long)(h->h_name) + (long)buf);
+    MakeIP(ntohl(in),h->h_name, CURL_NAMELOOKUP_SIZE - (long)(h->h_name) + (long)buf);
   }
 #if defined(HAVE_GETHOSTBYNAME_R)
   else {
     int h_errnop;
-    memset(buf,0,buf_size);    /* workaround for gethostbyname_r bug in qnx nto */
+     /* Workaround for gethostbyname_r bug in qnx nto. It is also _required_
+        for some of these functions. */
+    memset(buf, 0, CURL_NAMELOOKUP_SIZE);
 #ifdef HAVE_GETHOSTBYNAME_R_5
     /* Solaris, IRIX and more */
     if ((h = gethostbyname_r(hostname,
                              (struct hostent *)buf,
                              buf + sizeof(struct hostent),
-                             buf_size - sizeof(struct hostent),
+                             CURL_NAMELOOKUP_SIZE - sizeof(struct hostent),
                              &h_errnop)) == NULL )
 #endif
 #ifdef HAVE_GETHOSTBYNAME_R_6
@@ -142,22 +153,26 @@ struct hostent *GetHost(struct UrlData *data,
     if( gethostbyname_r(hostname,
                         (struct hostent *)buf,
                         buf + sizeof(struct hostent),
-                        buf_size - sizeof(struct hostent),
+                        CURL_NAMELOOKUP_SIZE - sizeof(struct hostent),
                         &h, /* DIFFERENCE */
                         &h_errnop))
 #endif
 #ifdef HAVE_GETHOSTBYNAME_R_3
     /* AIX, Digital Unix, HPUX 10, more? */
 
-    /* August 4th, 2000. I don't have any such system around so I write this
-       blindly in hope it might work or that someone else will help me fix
-       this. August 22nd, 2000: Albert Chin-A-Young brought an updated version
-       that should work! */
+    if(CURL_NAMELOOKUP_SIZE >=
+       (sizeof(struct hostent)+sizeof(struct hostent_data)))
 
-    ret = gethostbyname_r(hostname,
-                        (struct hostent *)buf,
-                        (struct hostent_data *)(buf + sizeof(struct hostent)));
+      /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
+       * that should work! September 20: Richard Prescott worked on the buffer
+       * size dilemma. */
 
+      ret = gethostbyname_r(hostname,
+                          (struct hostent *)buf,
+                          (struct hostent_data *)(buf + sizeof(struct hostent)));
+    else
+      ret = -1; /* failure, too smallish buffer size */
+    
     /* result expected in h */
     h = (struct hostent*)buf;
     h_errnop= errno; /* we don't deal with this, but set it anyway */
@@ -166,11 +181,13 @@ struct hostent *GetHost(struct UrlData *data,
       {
       infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
       h = NULL; /* set return code to NULL */
+      free(buf);
     }
 #else
   else {
     if ((h = gethostbyname(hostname)) == NULL ) {
       infof(data, "gethostbyname(2) failed for %s\n", hostname);
+      free(buf);
     }
 #endif
   }
index ccdb4cb0b8ca9455be12000a290bf9f9803f87e4..9fae683237e75a2f90cc672680a05e5d7cc1fc8c 100644 (file)
@@ -40,7 +40,6 @@
  * ------------------------------------------------------------
  ****************************************************************************/
 
-extern struct hostent *GetHost(struct UrlData *data, char *hostname, char *buf, int buf_size );
-extern char *MakeIP(unsigned long num,char *addr, int addr_len);
+struct hostent *GetHost(struct UrlData *data, char *hostname, char **bufp );
 
 #endif