1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

David LeBlanc's fixes!

This commit is contained in:
Daniel Stenberg 2000-05-29 22:51:13 +00:00
parent abc751ae13
commit a0ce95e155
7 changed files with 140 additions and 82 deletions

View File

@ -117,7 +117,7 @@ all: all-redirect
.SUFFIXES: .SUFFIXES:
.SUFFIXES: .S .c .o .s .SUFFIXES: .S .c .o .s
$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) $(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 Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) \ cd $(top_builddir) \

View File

@ -56,7 +56,10 @@
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
#include <winsock.h> #include <winsock.h>
#else /* some kind of unix */ #else /* some kind of unix */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> #include <sys/socket.h>
#endif
#include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h> #include <arpa/inet.h>
@ -69,6 +72,9 @@
#include <errno.h> #include <errno.h>
#endif #endif
#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif
#include <curl/curl.h> #include <curl/curl.h>
#include "urldata.h" #include "urldata.h"
@ -241,26 +247,20 @@ int GetLastResponse(int sockfd, char *buf,
} }
/* -- who are we? -- */ /* -- who are we? -- */
char *getmyhost(void) char *getmyhost(char *buf, int buf_size)
{ {
static char myhost[256]; #if defined(HAVE_GETHOSTNAME)
#ifdef HAVE_UNAME gethostname(buf, buf_size);
#elif defined(HAVE_UNAME)
struct utsname ugnm; struct utsname ugnm;
strncpy(buf, uname(&ugnm) < 0 ? "localhost" : ugnm.nodename, buf_size - 1);
if (uname(&ugnm) < 0) buf[buf_size - 1] = '\0';
return "localhost"; #else
(void) strncpy(myhost, ugnm.nodename, 255);
myhost[255] = '\0';
#endif
#ifdef HAVE_GETHOSTNAME
gethostname(myhost, 256);
#endif
#if !defined(HAVE_UNAME) && !defined(HAVE_GETHOSTNAME)
/* We have no means of finding the local host name! */ /* We have no means of finding the local host name! */
strcpy(myhost, "localhost"); strncpy(buf, "localhost", buf_size);
buf[buf_size - 1] = '\0';
#endif #endif
return myhost; return buf;
} }
#if 0 #if 0
@ -473,6 +473,10 @@ CURLcode _ftp(struct connectdata *conn)
/* for the ftp PORT mode */ /* for the ftp PORT mode */
int portsock=-1; int portsock=-1;
struct sockaddr_in serv_addr; 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 */ struct curl_slist *qitem; /* QUOTE item */
/* the ftp struct is already inited in ftp_connect() */ /* the ftp struct is already inited in ftp_connect() */
@ -542,24 +546,21 @@ CURLcode _ftp(struct connectdata *conn)
struct hostent *h=NULL; struct hostent *h=NULL;
size_t size; size_t size;
unsigned short porttouse; unsigned short porttouse;
char myhost[256] = "";
char *myhost=NULL;
if(data->ftpport) { if(data->ftpport) {
myhost = if2ip(data->ftpport); if(if2ip(data->ftpport, myhost, sizeof(myhost))) {
if(myhost) { h = GetHost(data, myhost, hostent_buf, sizeof(hostent_buf));
h = GetHost(data, myhost);
} }
else { else {
if(strlen(data->ftpport)>1) if(strlen(data->ftpport)>1)
h = GetHost(data, data->ftpport); h = GetHost(data, data->ftpport, hostent_buf, sizeof(hostent_buf));
if(h) if(h)
myhost=data->ftpport; strcpy(myhost,data->ftpport);
} }
} }
if(!myhost) { if(! *myhost) {
myhost = getmyhost(); h=GetHost(data, getmyhost(myhost,sizeof(myhost)), hostent_buf, sizeof(hostent_buf));
h=GetHost(data, myhost);
} }
infof(data, "We connect from %s\n", myhost); infof(data, "We connect from %s\n", myhost);
@ -609,8 +610,13 @@ CURLcode _ftp(struct connectdata *conn)
struct in_addr in; struct in_addr in;
unsigned short ip[5]; unsigned short ip[5];
(void) memcpy(&in.s_addr, *h->h_addr_list, sizeof (in.s_addr)); (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", sscanf( inet_ntoa(in), "%hu.%hu.%hu.%hu",
&ip[0], &ip[1], &ip[2], &ip[3]); &ip[0], &ip[1], &ip[2], &ip[3]);
#endif
sendf(data->firstsocket, data, "PORT %d,%d,%d,%d,%d,%d\n", sendf(data->firstsocket, data, "PORT %d,%d,%d,%d,%d,%d\n",
ip[0], ip[1], ip[2], ip[3], ip[0], ip[1], ip[2], ip[3],
porttouse >> 8, porttouse >> 8,
@ -640,7 +646,7 @@ CURLcode _ftp(struct connectdata *conn)
unsigned short newport; unsigned short newport;
char newhost[32]; char newhost[32];
struct hostent *he; struct hostent *he;
char *str=buf; char *str=buf,*ip_addr;
/* /*
* New 227-parser June 3rd 1999. * New 227-parser June 3rd 1999.
@ -665,7 +671,7 @@ CURLcode _ftp(struct connectdata *conn)
return CURLE_FTP_WEIRD_227_FORMAT; return CURLE_FTP_WEIRD_227_FORMAT;
} }
sprintf(newhost, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); 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) { if(!he) {
failf(data, "Can't resolve new host %s", newhost); failf(data, "Can't resolve new host %s", newhost);
return CURLE_FTP_CANT_GET_HOST; return CURLE_FTP_CANT_GET_HOST;
@ -682,25 +688,36 @@ CURLcode _ftp(struct connectdata *conn)
if(data->bits.verbose) { if(data->bits.verbose) {
struct in_addr in; struct in_addr in;
#if 1
struct hostent * answer; struct hostent * answer;
unsigned long address;
#if defined(HAVE_INET_ADDR) #if defined(HAVE_INET_ADDR)
unsigned long address;
#if defined(HAVE_GETHOSTBYADDR_R)
int h_errnop;
#endif
address = inet_addr(newhost); address = inet_addr(newhost);
answer = gethostbyaddr((char *) &address, sizeof(address), #if defined(HAVE_GETHOSTBYADDR_R)
AF_INET); 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 #else
answer = NULL; answer = NULL;
#endif #endif
(void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr)); (void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr));
infof(data, "Connecting to %s (%s) port %u\n", 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 #else
(void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr)); ip_addr = inet_ntoa(in),
infof(data, "Connecting to %s (%s) port %u\n",
he->h_name, inet_ntoa(in), newport);
#endif #endif
newport);
} }
if (connect(data->secondarysocket, (struct sockaddr *) &serv_addr, if (connect(data->secondarysocket, (struct sockaddr *) &serv_addr,
@ -727,6 +744,7 @@ CURLcode _ftp(struct connectdata *conn)
} }
/* we have the (new) data connection ready */ /* we have the (new) data connection ready */
infof(data, "Connected!\n");
/* change directory first */ /* change directory first */

View File

@ -39,6 +39,7 @@
****************************************************************************/ ****************************************************************************/
#include <string.h> #include <string.h>
#include <malloc.h>
#include "setup.h" #include "setup.h"
@ -61,51 +62,77 @@
#include "urldata.h" #include "urldata.h"
#include "sendf.h" #include "sendf.h"
#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif
/* --- resolve name or IP-number --- */ /* --- 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; struct in_addr in;
in.s_addr = htonl(num); 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 #else
static char addr[128];
unsigned char *paddr; unsigned char *paddr;
num = htonl(num); /* htonl() added to avoid endian probs */ num = htonl(num); /* htonl() added to avoid endian probs */
paddr = (unsigned char *)&num; paddr = (unsigned char *)&num;
sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]); sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
return (addr);
#endif #endif
return (addr);
} }
/* Stolen from Dancer source code, written by /* The original code to this function was stolen from the Dancer source code,
Bjorn Reese <breese@imada.ou.dk> */ written by Bjorn Reese, it has since been patched and modified. */
#ifndef INADDR_NONE #ifndef INADDR_NONE
#define INADDR_NONE (unsigned long) ~0 #define INADDR_NONE (unsigned long) ~0
#endif #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; struct hostent *h = NULL;
unsigned long in; 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 ) { if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
addrentry.s_addr = in; struct in_addr *addrentry;
addrlist[0] = (char *)&addrentry;
addrlist[1] = NULL; h = (struct hostent*)buf;
he.h_name = strncpy(name, MakeIP(ntohl(in)), MAXHOSTNAMELEN); h->h_addr_list = (char**)(buf + sizeof(*h));
he.h_addrtype = AF_INET; addrentry = (struct in_addr*)(h->h_addr_list + 2);
he.h_length = sizeof(struct in_addr); addrentry->s_addr = in;
he.h_addr_list = addrlist; h->h_addr_list[0] = (char*)addrentry;
h = &he; h->h_addr_list[1] = NULL;
} else if ( (h=gethostbyname(hostname)) == NULL ) { h->h_addrtype = AF_INET;
infof(data, "gethostbyname(2) failed for %s\n", hostname); 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); return (h);
} }

View File

@ -40,7 +40,7 @@
* ------------------------------------------------------------ * ------------------------------------------------------------
****************************************************************************/ ****************************************************************************/
struct hostent *GetHost(struct UrlData *data, char *hostname); extern struct hostent *GetHost(struct UrlData *data, char *hostname, char *buf, int buf_size );
char *MakeIP(unsigned long num); extern char *MakeIP(unsigned long num,char *addr, int addr_len);
#endif #endif

View File

@ -74,9 +74,13 @@
#include <sys/sockio.h> #include <sys/sockio.h>
#endif #endif
#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif
#define SYS_ERROR -1 #define SYS_ERROR -1
char *if2ip(char *interface) char *if2ip(char *interface, char *buf, int buf_size)
{ {
int dummy; int dummy;
char *ip=NULL; char *ip=NULL;
@ -101,7 +105,12 @@ char *if2ip(char *interface)
struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr; struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
memcpy(&in, &(s->sin_addr.s_addr), sizeof(in)); 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); close(dummy);
} }

View File

@ -42,9 +42,9 @@
#include "setup.h" #include "setup.h"
#if ! defined(WIN32) && ! defined(__BEOS__) #if ! defined(WIN32) && ! defined(__BEOS__)
char *if2ip(char *interface); extern char *if2ip(char *interface, char *buf, int buf_size);
#else #else
#define if2ip(x) NULL #define if2ip(a,b,c) NULL
#endif #endif
#endif #endif

View File

@ -303,48 +303,48 @@ CURLcode curl_setopt(CURL *curl, CURLoption option, ...)
switch(option) { switch(option) {
case CURLOPT_VERBOSE: case CURLOPT_VERBOSE:
data->bits.verbose = va_arg(param, long); data->bits.verbose = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_HEADER: case CURLOPT_HEADER:
data->bits.http_include_header = va_arg(param, long); data->bits.http_include_header = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_NOPROGRESS: 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) if(data->bits.hide_progress)
data->progress.flags |= PGRS_HIDE; data->progress.flags |= PGRS_HIDE;
break; break;
case CURLOPT_NOBODY: case CURLOPT_NOBODY:
data->bits.no_body = va_arg(param, long); data->bits.no_body = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_FAILONERROR: 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; break;
case CURLOPT_UPLOAD: case CURLOPT_UPLOAD:
data->bits.upload = va_arg(param, long); data->bits.upload = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_POST: case CURLOPT_POST:
data->bits.http_post = va_arg(param, long); data->bits.http_post = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_FTPLISTONLY: case CURLOPT_FTPLISTONLY:
data->bits.ftp_list_only = va_arg(param, long); data->bits.ftp_list_only = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_FTPAPPEND: case CURLOPT_FTPAPPEND:
data->bits.ftp_append = va_arg(param, long); data->bits.ftp_append = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_NETRC: case CURLOPT_NETRC:
data->bits.use_netrc = va_arg(param, long); data->bits.use_netrc = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_FOLLOWLOCATION: case CURLOPT_FOLLOWLOCATION:
data->bits.http_follow_location = va_arg(param, long); data->bits.http_follow_location = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_FTPASCII: case CURLOPT_FTPASCII:
data->bits.ftp_ascii = va_arg(param, long); data->bits.ftp_ascii = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_PUT: case CURLOPT_PUT:
data->bits.http_put = va_arg(param, long); data->bits.http_put = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_MUTE: case CURLOPT_MUTE:
data->bits.mute = va_arg(param, long); data->bits.mute = va_arg(param, long)?TRUE:FALSE;
break; break;
case CURLOPT_TIMECONDITION: case CURLOPT_TIMECONDITION:
@ -627,6 +627,10 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
struct UrlData *data = curl; struct UrlData *data = curl;
struct connectdata *conn; 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)) if(!data || (data->handle != STRUCT_OPEN))
return CURLE_BAD_FUNCTION_ARGUMENT; /* TBD: make error codes */ 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 */ /* 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); failf(data, "Couldn't resolv host '%s'", conn->name);
return CURLE_COULDNT_RESOLVE_HOST; return CURLE_COULDNT_RESOLVE_HOST;
} }
@ -1086,7 +1090,7 @@ CURLcode curl_connect(CURL *curl, CURLconnect **in_connect)
} }
/* connect to proxy */ /* 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); failf(data, "Couldn't resolv proxy '%s'", proxyptr);
return CURLE_COULDNT_RESOLVE_PROXY; return CURLE_COULDNT_RESOLVE_PROXY;
} }