1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 16:18:48 -05:00

Jacky Lam's adjust resolve-buffer size patch applied. Slightly edited

by Daniel.
This commit is contained in:
Daniel Stenberg 2002-04-22 13:31:16 +00:00
parent 381f77756d
commit 21fc402c01

View File

@ -358,7 +358,7 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
* *
* Keith McGuigan * Keith McGuigan
* 10/3/2001 */ * 10/3/2001 */
static struct hostent* pack_hostent(char* buf, struct hostent* orig) static struct hostent* pack_hostent(char** buf, struct hostent* orig)
{ {
char* bufptr; char* bufptr;
struct hostent* copy; struct hostent* copy;
@ -367,7 +367,7 @@ static struct hostent* pack_hostent(char* buf, struct hostent* orig)
char* str; char* str;
int len; int len;
bufptr = buf; bufptr = *buf;
copy = (struct hostent*)bufptr; copy = (struct hostent*)bufptr;
bufptr += sizeof(struct hostent); bufptr += sizeof(struct hostent);
@ -425,6 +425,7 @@ static struct hostent* pack_hostent(char* buf, struct hostent* orig)
} }
copy->h_addr_list[i] = NULL; copy->h_addr_list[i] = NULL;
*buf=(char *)realloc(*buf, (int)bufptr-(int)(*buf));
return copy; return copy;
} }
#endif #endif
@ -472,16 +473,15 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
* everything. OSF1 is known to require at least 8872 bytes. The buffer * everything. OSF1 is known to require at least 8872 bytes. The buffer
* required for storing all possible aliases and IP numbers is according to * required for storing all possible aliases and IP numbers is according to
* Stevens' Unix Network Programming 2nd editor, p. 304: 8192 bytes! */ * Stevens' Unix Network Programming 2nd editor, p. 304: 8192 bytes! */
int *buf = (int *)malloc(CURL_NAMELOOKUP_SIZE);
if(!buf)
return NULL; /* major failure */
*bufp = (char *)buf;
port=0; /* unused in IPv4 code */ port=0; /* unused in IPv4 code */
ret = 0; /* to prevent the compiler warning */ ret = 0; /* to prevent the compiler warning */
if ( (in=inet_addr(hostname)) != INADDR_NONE ) { if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
struct in_addr *addrentry; struct in_addr *addrentry;
int *buf = (int *)malloc(128);
if(!buf)
return NULL; /* major failure */
*bufp = (char *)buf;
h = (struct hostent*)buf; h = (struct hostent*)buf;
h->h_addr_list = (char**)(buf + sizeof(*h)); h->h_addr_list = (char**)(buf + sizeof(*h));
@ -493,30 +493,70 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
h->h_length = sizeof(*addrentry); h->h_length = sizeof(*addrentry);
h->h_name = *(h->h_addr_list) + h->h_length; h->h_name = *(h->h_addr_list) + h->h_length;
/* bad one h->h_name = (char*)(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, CURL_NAMELOOKUP_SIZE - (long)(h->h_name) + (long)buf); MakeIP(ntohl(in),h->h_name, 128 - (long)(h->h_name) + (long)buf);
} }
#if defined(HAVE_GETHOSTBYNAME_R) #if defined(HAVE_GETHOSTBYNAME_R)
else { else {
int h_errnop; int h_errnop;
int res=ERANGE;
int step_size=200;
int *buf = (int *)malloc(CURL_NAMELOOKUP_SIZE);
if(!buf)
return NULL; /* major failure */
*bufp=(char *)buf;
/* Workaround for gethostbyname_r bug in qnx nto. It is also _required_ /* Workaround for gethostbyname_r bug in qnx nto. It is also _required_
for some of these functions. */ for some of these functions. */
memset(buf, 0, CURL_NAMELOOKUP_SIZE); memset(buf, 0, CURL_NAMELOOKUP_SIZE);
#ifdef HAVE_GETHOSTBYNAME_R_5 #ifdef HAVE_GETHOSTBYNAME_R_5
/* Solaris, IRIX and more */ /* Solaris, IRIX and more */
if ((h = gethostbyname_r(hostname, while(!h) {
(struct hostent *)buf, h = gethostbyname_r(hostname,
(char *)buf + sizeof(struct hostent), (struct hostent *)buf,
CURL_NAMELOOKUP_SIZE - sizeof(struct hostent), (char *)buf + sizeof(struct hostent),
&h_errnop)) == NULL ) step_size - sizeof(struct hostent),
&h_errnop);
/* If the buffer is too small, it returns NULL and sets errno to
ERANGE. The errno is thread safe if this is compiled with
-D_REENTRANT as then the 'errno' variable is a macro defined to
get used properly for threads. */
if(h || (errno != ERANGE))
break;
step_size+=200;
}
#ifdef MALLOCDEBUG
infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
#endif
if(h) {
buf=(int *)realloc(buf, step_size);
*bufp=(char *)buf;
}
else
#endif #endif
#ifdef HAVE_GETHOSTBYNAME_R_6 #ifdef HAVE_GETHOSTBYNAME_R_6
/* Linux */ /* Linux */
if( gethostbyname_r(hostname, while((res=gethostbyname_r(hostname,
(struct hostent *)buf, (struct hostent *)buf,
(char *)buf + sizeof(struct hostent), (char *)buf + sizeof(struct hostent),
CURL_NAMELOOKUP_SIZE - sizeof(struct hostent), step_size - sizeof(struct hostent),
&h, /* DIFFERENCE */ &h, /* DIFFERENCE */
&h_errnop)) &h_errnop))==ERANGE) {
step_size+=200;
}
#ifdef MALLOCDEBUG
infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
#endif
if(!res) {
buf=(int *)realloc(buf, step_size);
*bufp=(char *)buf;
}
else
#endif #endif
#ifdef HAVE_GETHOSTBYNAME_R_3 #ifdef HAVE_GETHOSTBYNAME_R_3
/* AIX, Digital Unix, HPUX 10, more? */ /* AIX, Digital Unix, HPUX 10, more? */
@ -529,8 +569,8 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
* size dilemma. */ * size dilemma. */
ret = gethostbyname_r(hostname, ret = gethostbyname_r(hostname,
(struct hostent *)buf, (struct hostent *)buf,
(struct hostent_data *)(buf + sizeof(struct hostent))); (struct hostent_data *)(buf + sizeof(struct hostent)));
else else
ret = -1; /* failure, too smallish buffer size */ ret = -1; /* failure, too smallish buffer size */
@ -549,14 +589,17 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
else { else {
if ((h = gethostbyname(hostname)) == NULL ) { if ((h = gethostbyname(hostname)) == NULL ) {
infof(data, "gethostbyname(2) failed for %s\n", hostname); infof(data, "gethostbyname(2) failed for %s\n", hostname);
free(buf);
*bufp=NULL; *bufp=NULL;
} }
else else
{
char *buf=(char *)malloc(CURL_NAMELOOKUP_SIZE);
/* we make a copy of the hostent right now, right here, as the /* we make a copy of the hostent right now, right here, as the
static one we got a pointer to might get removed when we don't static one we got a pointer to might get removed when we don't
want/expect that */ want/expect that */
h = pack_hostent((char *)buf, h); h = pack_hostent(&buf, h);
*bufp=(char *)buf;
}
#endif #endif
} }
return (h); return (h);