David LeBlanc's fixes!
authorDaniel Stenberg <daniel@haxx.se>
Mon, 29 May 2000 22:51:13 +0000 (22:51 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 29 May 2000 22:51:13 +0000 (22:51 +0000)
lib/Makefile.in
lib/ftp.c
lib/hostip.c
lib/hostip.h
lib/if2ip.c
lib/if2ip.h
lib/url.c

index aece159..fb7adcc 100644 (file)
@@ -117,7 +117,7 @@ all: all-redirect
 .SUFFIXES:
 .SUFFIXES: .S .c .o .s
 $(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) 
-       cd $(top_srcdir) && $(AUTOMAKE) --foreign --include-deps lib/Makefile
+       cd $(top_srcdir) && $(AUTOMAKE) --foreign lib/Makefile
 
 Makefile: $(srcdir)/Makefile.in  $(top_builddir)/config.status
        cd $(top_builddir) \
index 0844cc7..3fd782d 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
 #include <winsock.h>
 #else /* some kind of unix */
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#include <sys/types.h>
 #include <netinet/in.h>
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
@@ -69,6 +72,9 @@
 #include <errno.h>
 #endif
 
+#ifdef HAVE_INET_NTOA_R
+#include "inet_ntoa_r.h"
+#endif
 
 #include <curl/curl.h>
 #include "urldata.h"
@@ -241,26 +247,20 @@ int GetLastResponse(int sockfd, char *buf,
 }
 
 /* -- who are we? -- */
-char *getmyhost(void)
+char *getmyhost(char *buf, int buf_size)
 {
-  static char myhost[256];
-#ifdef HAVE_UNAME
+#if defined(HAVE_GETHOSTNAME)
+  gethostname(buf, buf_size);
+#elif defined(HAVE_UNAME)
   struct utsname ugnm;
-
-  if (uname(&ugnm) < 0)
-    return "localhost";
-
-  (void) strncpy(myhost, ugnm.nodename, 255);
-  myhost[255] = '\0';
-#endif
-#ifdef HAVE_GETHOSTNAME
-  gethostname(myhost, 256);
-#endif
-#if !defined(HAVE_UNAME) && !defined(HAVE_GETHOSTNAME)
+  strncpy(buf, uname(&ugnm) < 0 ? "localhost" : ugnm.nodename, buf_size - 1);
+  buf[buf_size - 1] = '\0';
+#else
   /* We have no means of finding the local host name! */
-  strcpy(myhost, "localhost");
+  strncpy(buf, "localhost", buf_size);
+  buf[buf_size - 1] = '\0';
 #endif
-  return myhost;
+  return buf;
 }
 
 #if 0
@@ -473,6 +473,10 @@ CURLcode _ftp(struct connectdata *conn)
   /* for the ftp PORT mode */
   int portsock=-1;
   struct sockaddr_in serv_addr;
+  char hostent_buf[512];
+#if defined (HAVE_INET_NTOA_R)
+  char ntoa_buf[64];
+#endif
 
   struct curl_slist *qitem; /* QUOTE item */
   /* the ftp struct is already inited in ftp_connect() */
@@ -542,24 +546,21 @@ CURLcode _ftp(struct connectdata *conn)
     struct hostent *h=NULL;
     size_t size;
     unsigned short porttouse;
+    char myhost[256] = "";
 
-    char *myhost=NULL;
-      
     if(data->ftpport) {
-      myhost = if2ip(data->ftpport);
-      if(myhost) {
-        h = GetHost(data, myhost);
+      if(if2ip(data->ftpport, myhost, sizeof(myhost))) {
+        h = GetHost(data, myhost, hostent_buf, sizeof(hostent_buf));
       }
       else {
         if(strlen(data->ftpport)>1)
-          h = GetHost(data, data->ftpport);
+          h = GetHost(data, data->ftpport, hostent_buf, sizeof(hostent_buf));
         if(h)
-          myhost=data->ftpport;
+          strcpy(myhost,data->ftpport);
       }
     }
-    if(!myhost) {
-      myhost = getmyhost();
-      h=GetHost(data, myhost);
+    if(! *myhost) {
+      h=GetHost(data, getmyhost(myhost,sizeof(myhost)), hostent_buf, sizeof(hostent_buf));
     }
     infof(data, "We connect from %s\n", myhost);
 
@@ -609,8 +610,13 @@ CURLcode _ftp(struct connectdata *conn)
       struct in_addr in;
       unsigned short ip[5];
       (void) memcpy(&in.s_addr, *h->h_addr_list, sizeof (in.s_addr));
+#if defined (HAVE_INET_NTOA_R)
+      sscanf( inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)), "%hu.%hu.%hu.%hu",
+              &ip[0], &ip[1], &ip[2], &ip[3]);
+#else
       sscanf( inet_ntoa(in), "%hu.%hu.%hu.%hu",
               &ip[0], &ip[1], &ip[2], &ip[3]);
+#endif
       sendf(data->firstsocket, data, "PORT %d,%d,%d,%d,%d,%d\n",
             ip[0], ip[1], ip[2], ip[3],
             porttouse >> 8,
@@ -640,7 +646,7 @@ CURLcode _ftp(struct connectdata *conn)
       unsigned short newport;
       char newhost[32];
       struct hostent *he;
-      char *str=buf;
+      char *str=buf,*ip_addr;
 
       /*
        * New 227-parser June 3rd 1999.
@@ -665,7 +671,7 @@ CURLcode _ftp(struct connectdata *conn)
         return CURLE_FTP_WEIRD_227_FORMAT;
       }
       sprintf(newhost, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
-      he = GetHost(data, newhost);
+      he = GetHost(data, newhost, hostent_buf, sizeof(hostent_buf));
       if(!he) {
         failf(data, "Can't resolve new host %s", newhost);
         return CURLE_FTP_CANT_GET_HOST;
@@ -682,25 +688,36 @@ CURLcode _ftp(struct connectdata *conn)
 
       if(data->bits.verbose) {
         struct in_addr in;
-#if 1
         struct hostent * answer;
 
-        unsigned long address;
 #if defined(HAVE_INET_ADDR)
+        unsigned long address;
+#if defined(HAVE_GETHOSTBYADDR_R)
+        int h_errnop;
+#endif
+
         address = inet_addr(newhost);
-        answer = gethostbyaddr((char *) &address, sizeof(address), 
-                               AF_INET);
+#if defined(HAVE_GETHOSTBYADDR_R)
+        answer = gethostbyaddr_r((char *) &address, sizeof(address), AF_INET,
+                                 (struct hostent *)hostent_buf,
+                                 hostent_buf + sizeof(*answer),
+                                 sizeof(hostent_buf) - sizeof(*answer),
+                                 &h_errnop);
+#else
+        answer = gethostbyaddr((char *) &address, sizeof(address), AF_INET);
+#endif
 #else
         answer = NULL;
 #endif
         (void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr));
         infof(data, "Connecting to %s (%s) port %u\n",
-              answer?answer->h_name:newhost, inet_ntoa(in), newport);
+              answer?answer->h_name:newhost,
+#if defined(HAVE_INET_NTOA_R)
+              ip_addr = inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)),
 #else
-        (void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr));
-        infof(data, "Connecting to %s (%s) port %u\n",
-              he->h_name, inet_ntoa(in), newport);
+              ip_addr = inet_ntoa(in),
 #endif
+              newport);
       }
        
       if (connect(data->secondarysocket, (struct sockaddr *) &serv_addr,
@@ -727,6 +744,7 @@ CURLcode _ftp(struct connectdata *conn)
 
   }
   /* we have the (new) data connection ready */
+  infof(data, "Connected!\n");
 
   /* change directory first */
 
index 453d8a3..ca8bf91 100644 (file)
@@ -39,6 +39,7 @@
  ****************************************************************************/
 
 #include <string.h>
+#include <malloc.h>
 
 #include "setup.h"
 
 #include "urldata.h"
 #include "sendf.h"
 
+#ifdef HAVE_INET_NTOA_R
+#include "inet_ntoa_r.h"
+#endif
+
 /* --- resolve name or IP-number --- */
 
-char *MakeIP(unsigned long num)
+char *MakeIP(unsigned long num,char *addr, int addr_len)
 {
-#ifdef HAVE_INET_NTOA
+#if defined(HAVE_INET_NTOA) || defined(HAVE_INET_NTOA_R)
   struct in_addr in;
-
   in.s_addr = htonl(num);
-  return (inet_ntoa(in));
+
+#if defined(HAVE_INET_NTOA_R)
+  inet_ntoa_r(in,addr,addr_len);
+#else
+  strncpy(addr,inet_ntoa(in),addr_len);
+#endif
 #else
-  static char addr[128];
   unsigned char *paddr;
 
   num = htonl(num);  /* htonl() added to avoid endian probs */
   paddr = (unsigned char *)&num;
   sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
-  return (addr);
 #endif
+  return (addr);
 }
 
-/* Stolen from Dancer source code, written by
-   Bjorn Reese <breese@imada.ou.dk> */
+/* The original code to this function was stolen from the Dancer source code,
+   written by Bjorn Reese, it has since been patched and modified. */
 #ifndef INADDR_NONE
 #define INADDR_NONE (unsigned long) ~0
 #endif
-struct hostent *GetHost(struct UrlData *data, char *hostname)
+struct hostent *GetHost(struct UrlData *data,
+                        char *hostname,
+                        char *buf,
+                        int buf_size )
 {
   struct hostent *h = NULL;
   unsigned long in;
-  static struct hostent he;
-  static char name[MAXHOSTNAMELEN];
-  static char *addrlist[2];
-  static struct in_addr addrentry;
 
   if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
-    addrentry.s_addr = in;
-    addrlist[0] = (char *)&addrentry;
-    addrlist[1] = NULL;
-    he.h_name = strncpy(name, MakeIP(ntohl(in)), MAXHOSTNAMELEN);
-    he.h_addrtype = AF_INET;
-    he.h_length = sizeof(struct in_addr);
-    he.h_addr_list = addrlist;
-    h = &he;
-  } else if ( (h=gethostbyname(hostname)) == NULL ) {
-    infof(data, "gethostbyname(2) failed for %s\n", hostname);
+    struct in_addr *addrentry;
+
+    h = (struct hostent*)buf;
+    h->h_addr_list = (char**)(buf + sizeof(*h));
+    addrentry = (struct in_addr*)(h->h_addr_list + 2);
+    addrentry->s_addr = in;
+    h->h_addr_list[0] = (char*)addrentry;
+    h->h_addr_list[1] = NULL;
+    h->h_addrtype = AF_INET;
+    h->h_length = sizeof(*addrentry);
+    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);
+#if defined(HAVE_GETHOSTBYNAME_R)
+  }
+  else {
+    int h_errnop;
+    memset(buf,0,buf_size);    /* workaround for gethostbyname_r bug in qnx nto */
+    if ((h = gethostbyname_r(hostname,
+                             (struct hostent *)buf,buf +
+                             sizeof(struct hostent),buf_size -
+                             sizeof(struct hostent),&h_errnop)) == NULL ) {
+      infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
+    }
+#else
+  }
+  else {
+    if ((h = gethostbyname(hostname)) == NULL ) {
+      infof(data, "gethostbyname(2) failed for %s\n", hostname);
+    }
+#endif
   }
   return (h);
 }
index 8753e39..480a85e 100644 (file)
@@ -40,7 +40,7 @@
  * ------------------------------------------------------------
  ****************************************************************************/
 
-struct hostent *GetHost(struct UrlData *data, char *hostname);
-char *MakeIP(unsigned long num);
+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);
 
 #endif
index 5fa70a6..557b6fb 100644 (file)
 #include <sys/sockio.h>
 #endif
 
+#ifdef HAVE_INET_NTOA_R
+#include "inet_ntoa_r.h"
+#endif
+
 #define SYS_ERROR -1
 
-char *if2ip(char *interface)
+char *if2ip(char *interface, char *buf, int buf_size)
 {
   int dummy;
   char *ip=NULL;
@@ -101,7 +105,12 @@ char *if2ip(char *interface)
 
       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
-      ip = (char *)strdup(inet_ntoa(in));
+#if defined(HAVE_INET_NTOA_R)
+      ip = inet_ntoa_r(in,buf,buf_size);
+#else
+      ip = strncpy(buf,inet_ntoa(in),buf_size);
+      ip[buf_size - 1] = 0;
+#endif
     }
     close(dummy);
   }
index 0b658f9..e6f73a5 100644 (file)
@@ -42,9 +42,9 @@
 #include "setup.h"
 
 #if ! defined(WIN32) && ! defined(__BEOS__)
-char *if2ip(char *interface);
+extern char *if2ip(char *interface, char *buf, int buf_size);
 #else
-#define if2ip(x) NULL
+#define if2ip(a,b,c) NULL
 #endif
 
 #endif
index 6594afb..5dd33a5 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -303,48 +303,48 @@ CURLcode curl_setopt(CURL *curl, CURLoption option, ...)
 
   switch(option) {
   case CURLOPT_VERBOSE:
-    data->bits.verbose = va_arg(param, long);
+    data->bits.verbose = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_HEADER:
-    data->bits.http_include_header = va_arg(param, long);
+    data->bits.http_include_header = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_NOPROGRESS:
-    data->bits.hide_progress = va_arg(param, long);
+    data->bits.hide_progress = va_arg(param, long)?TRUE:FALSE;
     if(data->bits.hide_progress)
       data->progress.flags |= PGRS_HIDE;
     break;
   case CURLOPT_NOBODY:
-    data->bits.no_body = va_arg(param, long);
+    data->bits.no_body = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_FAILONERROR:
-    data->bits.http_fail_on_error = va_arg(param, long);
+    data->bits.http_fail_on_error = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_UPLOAD:
-    data->bits.upload = va_arg(param, long);
+    data->bits.upload = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_POST:
-    data->bits.http_post = va_arg(param, long);
+    data->bits.http_post = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_FTPLISTONLY:
-    data->bits.ftp_list_only = va_arg(param, long);
+    data->bits.ftp_list_only = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_FTPAPPEND:
-    data->bits.ftp_append = va_arg(param, long);
+    data->bits.ftp_append = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_NETRC:
-    data->bits.use_netrc = va_arg(param, long);
+    data->bits.use_netrc = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_FOLLOWLOCATION:
-    data->bits.http_follow_location = va_arg(param, long);
+    data->bits.http_follow_location = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_FTPASCII:
-    data->bits.ftp_ascii = va_arg(param, long);
+    data->bits.ftp_ascii = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_PUT:
-    data->bits.http_put = va_arg(param, long);
+    data->bits.http_put = va_arg(param, long)?TRUE:FALSE;
     break;
   case CURLOPT_MUTE:
-    data->bits.mute = va_arg(param, long);
+    data->bits.mute = va_arg(param, long)?TRUE:FALSE;
     break;
 
   case CURLOPT_TIMECONDITION:
@@ -627,6 +627,10 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
   struct UrlData *data = curl;
   struct connectdata *conn;
 
+  /* I believe the longest possible name in a DNS is set to 255 letters, FQDN
+     so this should be safe: */
+  char hostent_buf[512];
+
   if(!data || (data->handle != STRUCT_OPEN))
     return CURLE_BAD_FUNCTION_ARGUMENT; /* TBD: make error codes */
 
@@ -1036,7 +1040,7 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
     }
     
     /* Connect to target host right on */
-    if(!(conn->hp = GetHost(data, conn->name))) {
+    if(!(conn->hp = GetHost(data, conn->name, hostent_buf, sizeof(hostent_buf)))) {
       failf(data, "Couldn't resolv host '%s'", conn->name);
       return CURLE_COULDNT_RESOLVE_HOST;
     }
@@ -1086,7 +1090,7 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
     }
 
     /* connect to proxy */
-    if(!(conn->hp = GetHost(data, proxyptr))) {
+    if(!(conn->hp = GetHost(data, proxyptr, hostent_buf, sizeof(hostent_buf)))) {
       failf(data, "Couldn't resolv proxy '%s'", proxyptr);
       return CURLE_COULDNT_RESOLVE_PROXY;
     }