summaryrefslogtreecommitdiff
path: root/ares_gethostbyname.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2007-06-04 21:33:02 +0000
committerDaniel Stenberg <daniel@haxx.se>2007-06-04 21:33:02 +0000
commit9089e0c26c02b83218a52a11ace43711cbdaa042 (patch)
treeb3c007dd5a49da8657bfc2c8ac35dcf988b8685d /ares_gethostbyname.c
parent30294b0a67136e172bd7c0f0c4bd9ba296442315 (diff)
downloadc-ares-9089e0c26c02b83218a52a11ace43711cbdaa042.tar.gz
c-ares-9089e0c26c02b83218a52a11ace43711cbdaa042.tar.bz2
c-ares-9089e0c26c02b83218a52a11ace43711cbdaa042.zip
Revered Ashish Sharma's multiple entries patch, as it caused memory madness
Diffstat (limited to 'ares_gethostbyname.c')
-rw-r--r--ares_gethostbyname.c139
1 files changed, 20 insertions, 119 deletions
diff --git a/ares_gethostbyname.c b/ares_gethostbyname.c
index 5170c07..c0fa474 100644
--- a/ares_gethostbyname.c
+++ b/ares_gethostbyname.c
@@ -116,7 +116,7 @@ static void next_lookup(struct host_query *hquery)
{
int status;
const char *p;
- struct hostent *host = NULL;
+ struct hostent *host;
for (p = hquery->remaining_lookups; *p; p++)
{
@@ -151,7 +151,7 @@ static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
{
struct host_query *hquery = (struct host_query *) arg;
ares_channel channel = hquery->channel;
- struct hostent *host = NULL;
+ struct hostent *host;
if (status == ARES_SUCCESS)
{
@@ -242,86 +242,12 @@ static int fake_hostent(const char *name, int family, ares_host_callback callbac
return 1;
}
-static int add_host(struct hostent *hostent, struct hostent **host)
-{
- char **p;
- char **h_addr_list = NULL;
- struct hostent *hostptr = *host;
- int count = 0;
- int index = 0;
-
- if (hostptr == NULL)
- {
- *host = hostent;
- return 0;
- }
-
- for (p = hostptr->h_addr_list; *p; p++)
- {
- count++;
- }
-
- for (p = hostent->h_addr_list; *p; p++)
- {
- count++;
- }
-
- h_addr_list = malloc((count+1) * sizeof(char *));
- if (!h_addr_list)
- {
- *host = NULL;
- return -1;
- }
-
- for (p = hostptr->h_addr_list; *p; p++)
- {
- h_addr_list[index] = malloc(sizeof(struct in_addr));
- if (h_addr_list[index])
- {
- memcpy(h_addr_list[index], *p, sizeof(struct in_addr));
- }
- else
- {
- free(h_addr_list);
- return -1;
- }
- index++;
- }
-
- for(p = hostent->h_addr_list; *p; p++)
- {
- h_addr_list[index] = malloc(sizeof(struct in_addr));
- if (h_addr_list[index])
- {
- memcpy(h_addr_list[index], *p, sizeof(struct in_addr));
- }
- else
- {
- free(h_addr_list);
- return -1;
- }
- index++;
- }
-
- h_addr_list[index] = NULL;
-
- for (p = hostptr->h_addr_list; *p; p++)
- {
- free(*p);
- }
- free(hostptr->h_addr_list);
- hostptr->h_addr_list = h_addr_list;
- return 0;
-}
-
static int file_lookup(const char *name, int family, struct hostent **host)
{
FILE *fp;
char **alias;
int status;
int error;
- int match;
- struct hostent *hostent = NULL;
#ifdef WIN32
char PATH_HOSTS[MAX_PATH];
@@ -357,60 +283,35 @@ static int file_lookup(const char *name, int family, struct hostent **host)
{
error = ERRNO;
switch(error)
- {
- case ENOENT:
- case ESRCH:
- return ARES_ENOTFOUND;
- default:
- DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
- error, strerror(error)));
- DEBUGF(fprintf(stderr, "Error opening file: %s\n",
- PATH_HOSTS));
- *host = NULL;
- return ARES_EFILE;
- }
+ {
+ case ENOENT:
+ case ESRCH:
+ return ARES_ENOTFOUND;
+ default:
+ DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
+ error, strerror(error)));
+ DEBUGF(fprintf(stderr, "Error opening file: %s\n",
+ PATH_HOSTS));
+ *host = NULL;
+ return ARES_EFILE;
+ }
}
while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
{
- match = 0;
- hostent = *host;
if (strcasecmp((*host)->h_name, name) == 0)
- {
- match = 1;
- }
- else
- {
- for (alias = (*host)->h_aliases; *alias; alias++)
+ break;
+ for (alias = (*host)->h_aliases; *alias; alias++)
{
if (strcasecmp(*alias, name) == 0)
- {
- match = 1;
break;
- }
}
- }
- if (match)
- {
- if(!add_host(hostent, host))
- ares_free_hostent(hostent);
- else {
- *host = NULL;
- }
- }
- else
- {
- ares_free_hostent(*host);
- *host = NULL;
- }
+ if (*alias)
+ break;
+ ares_free_hostent(*host);
}
fclose(fp);
if (status == ARES_EOF)
- {
- if ( *host)
- status = ARES_SUCCESS;
- else
- status = ARES_ENOTFOUND;
- }
+ status = ARES_ENOTFOUND;
if (status != ARES_SUCCESS)
*host = NULL;
return status;