From aa8a2fbde304b985ef3647249b0dd1c51c40fa45 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 21 Sep 2000 08:47:48 +0000 Subject: GetHost() changed function arguments --- lib/hostip.c | 43 ++++++++++++++++++++++++++++++------------- lib/hostip.h | 3 +-- 2 files changed, 31 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/hostip.c b/lib/hostip.c index e5eecf705..4989314bb 100644 --- a/lib/hostip.c +++ b/lib/hostip.c @@ -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 } diff --git a/lib/hostip.h b/lib/hostip.h index ccdb4cb0b..9fae68323 100644 --- a/lib/hostip.h +++ b/lib/hostip.h @@ -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 -- cgit v1.2.3