1
/* Copyright 1998 by the Massachusetts Institute of Technology.
3
* Permission to use, copy, modify, and distribute this
4
* software and its documentation for any purpose and without
5
* fee is hereby granted, provided that the above copyright
6
* notice appear in all copies and that both that copyright
7
* notice and this permission notice appear in supporting
8
* documentation, and that the name of M.I.T. not be used in
9
* advertising or publicity pertaining to distribution of the
10
* software without specific, written prior permission.
11
* M.I.T. makes no representations about the suitability of
12
* this software for any purpose. It is provided "as is"
13
* without express or implied warranty.
17
#include <sys/types.h>
24
#include <sys/socket.h>
25
#include <netinet/in.h>
26
#include <arpa/inet.h>
28
# include <arpa/nameser.h>
34
#include "ares_private.h"
35
#include "ares_local.h"
38
/* Arguments passed to ares_gethostbyname() */
41
ares_host_callback callback;
44
const char *remaining_lookups;
47
static void next_lookup(struct host_query *hquery);
48
static void host_callback(void *arg, int status, unsigned char *abuf,
50
static void end_hquery(struct host_query *hquery, int status,
51
struct hostent *host);
52
static int fake_hostent(const char *name, ares_host_callback callback,
54
static int file_lookup(const char *name, struct hostent **host);
55
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
57
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
61
extern char w32hostspath[];
64
void ares_gethostbyname(ares_channel channel, const char *name, int family,
65
ares_host_callback callback, void *arg)
67
struct host_query *hquery;
69
/* See if request can be handled by local pseudo-domain DNS */
70
if (ares_local_gethostbyname(channel, name, family, callback, arg))
75
/* Right now we only know how to look up Internet addresses. */
76
if (family != AF_INET)
78
callback(arg, ARES_ENOTIMP, NULL);
82
if (fake_hostent(name, callback, arg))
85
/* Allocate and fill in the host query structure. */
86
hquery = malloc(sizeof(struct host_query));
89
callback(arg, ARES_ENOMEM, NULL);
92
hquery->channel = channel;
93
hquery->name = strdup(name);
97
callback(arg, ARES_ENOMEM, NULL);
100
hquery->callback = callback;
102
hquery->remaining_lookups = channel->lookups;
104
/* Start performing lookups according to channel->lookups. */
108
static void next_lookup(struct host_query *hquery)
112
struct hostent *host;
114
for (p = hquery->remaining_lookups; *p; p++)
120
hquery->remaining_lookups = p + 1;
121
ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
126
/* Host file lookup */
127
status = file_lookup(hquery->name, &host);
128
if (status != ARES_ENOTFOUND)
130
end_hquery(hquery, status, host);
136
end_hquery(hquery, ARES_ENOTFOUND, NULL);
139
static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
141
struct host_query *hquery = (struct host_query *) arg;
142
ares_channel channel = hquery->channel;
143
struct hostent *host;
145
if (status == ARES_SUCCESS)
147
status = ares_parse_a_reply(abuf, alen, &host);
148
if (host && channel->nsort)
149
sort_addresses(host, channel->sortlist, channel->nsort);
150
end_hquery(hquery, status, host);
152
else if (status == ARES_EDESTRUCTION)
153
end_hquery(hquery, status, NULL);
158
static void end_hquery(struct host_query *hquery, int status,
159
struct hostent *host)
161
hquery->callback(hquery->arg, status, host);
163
ares_free_hostent(host);
168
/* If the name looks like an IP address, fake up a host entry, end the
169
* query immediately, and return true. Otherwise return false.
171
static int fake_hostent(const char *name, ares_host_callback callback,
175
struct hostent hostent;
177
char *aliases[1] = { NULL };
180
/* It only looks like an IP address if it's all numbers and dots. */
181
for (p = name; *p; p++)
183
if (!isdigit((unsigned char)*p) && *p != '.')
187
/* It also only looks like an IP address if it's non-zero-length and
188
* doesn't end with a dot.
190
if (p == name || *(p - 1) == '.')
193
/* It looks like an IP address. Figure out what IP address it is. */
194
addr.s_addr = inet_addr(name);
195
if (addr.s_addr == INADDR_NONE)
197
callback(arg, ARES_EBADNAME, NULL);
201
/* Duplicate the name, to avoid a constness violation. */
202
hostent.h_name = strdup(name);
205
callback(arg, ARES_ENOMEM, NULL);
209
/* Fill in the rest of the host structure and terminate the query. */
210
addrs[0] = (char *) &addr;
212
hostent.h_aliases = aliases;
213
hostent.h_addrtype = AF_INET;
214
hostent.h_length = sizeof(struct in_addr);
215
hostent.h_addr_list = addrs;
216
callback(arg, ARES_SUCCESS, &hostent);
218
free(hostent.h_name);
222
int hostfile_lookup(const char *name, struct hostent **host)
224
return file_lookup(name, host);
227
static int file_lookup(const char *name, struct hostent **host)
234
fp = fopen(w32hostspath, "r");
236
fp = fopen(PATH_HOSTS, "r");
239
return ARES_ENOTFOUND;
241
while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
243
if (strcasecmp((*host)->h_name, name) == 0)
245
for (alias = (*host)->h_aliases; *alias; alias++)
247
if (strcasecmp(*alias, name) == 0)
252
ares_free_hostent(*host);
255
if (status == ARES_EOF)
256
status = ARES_ENOTFOUND;
257
if (status != ARES_SUCCESS)
262
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
265
struct in_addr a1, a2;
266
int i1, i2, ind1, ind2;
268
/* This is a simple insertion sort, not optimized at all. i1 walks
269
* through the address list, with the loop invariant that everything
270
* to the left of i1 is sorted. In the loop body, the value at i1 is moved
271
* back through the list (via i2) until it is in sorted order.
273
for (i1 = 0; host->h_addr_list[i1]; i1++)
275
memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
276
ind1 = get_address_index(&a1, sortlist, nsort);
277
for (i2 = i1 - 1; i2 >= 0; i2--)
279
memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
280
ind2 = get_address_index(&a2, sortlist, nsort);
283
memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
285
memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
289
/* Find the first entry in sortlist which matches addr. Return nsort
290
* if none of them match.
292
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
297
for (i = 0; i < nsort; i++)
299
if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)