From a0ce95e155de68bd5a088a7a539f45aa7134b00b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 29 May 2000 22:51:13 +0000 Subject: [PATCH] David LeBlanc's fixes! --- lib/Makefile.in | 2 +- lib/ftp.c | 90 +++++++++++++++++++++++++++++-------------------- lib/hostip.c | 73 ++++++++++++++++++++++++++------------- lib/hostip.h | 4 +-- lib/if2ip.c | 13 +++++-- lib/if2ip.h | 4 +-- lib/url.c | 36 +++++++++++--------- 7 files changed, 140 insertions(+), 82 deletions(-) diff --git a/lib/Makefile.in b/lib/Makefile.in index aece159e1..fb7adccee 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -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) \ diff --git a/lib/ftp.c b/lib/ftp.c index 0844cc78a..3fd782de0 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -56,7 +56,10 @@ #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) #include #else /* some kind of unix */ +#ifdef HAVE_SYS_SOCKET_H #include +#endif +#include #include #ifdef HAVE_ARPA_INET_H #include @@ -69,6 +72,9 @@ #include #endif +#ifdef HAVE_INET_NTOA_R +#include "inet_ntoa_r.h" +#endif #include #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 */ diff --git a/lib/hostip.c b/lib/hostip.c index 453d8a387..ca8bf916a 100644 --- a/lib/hostip.c +++ b/lib/hostip.c @@ -39,6 +39,7 @@ ****************************************************************************/ #include +#include #include "setup.h" @@ -61,51 +62,77 @@ #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 *)# 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 */ +/* 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); } diff --git a/lib/hostip.h b/lib/hostip.h index 8753e3975..480a85ede 100644 --- a/lib/hostip.h +++ b/lib/hostip.h @@ -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 diff --git a/lib/if2ip.c b/lib/if2ip.c index 5fa70a68c..557b6fb8c 100644 --- a/lib/if2ip.c +++ b/lib/if2ip.c @@ -74,9 +74,13 @@ #include #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); } diff --git a/lib/if2ip.h b/lib/if2ip.h index 0b658f9d2..e6f73a54a 100644 --- a/lib/if2ip.h +++ b/lib/if2ip.h @@ -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 diff --git a/lib/url.c b/lib/url.c index 6594afbe6..5dd33a59d 100644 --- 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; }