aboutsummaryrefslogtreecommitdiff
path: root/lib/hostip.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2000-05-29 22:51:13 +0000
committerDaniel Stenberg <daniel@haxx.se>2000-05-29 22:51:13 +0000
commita0ce95e155de68bd5a088a7a539f45aa7134b00b (patch)
tree4e8c3919e90ce83dbc502b6eeb82668693a46297 /lib/hostip.c
parentabc751ae1300c7131eb2878a13cec0e24ea2840d (diff)
David LeBlanc's fixes!
Diffstat (limited to 'lib/hostip.c')
-rw-r--r--lib/hostip.c73
1 files changed, 50 insertions, 23 deletions
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 <string.h>
+#include <malloc.h>
#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 *)&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);
}