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.
16 static const char rcsid[] = "$Id: ares_gethostbyname.c,v 1.7 1999/10/23 19:28:13 danw Exp $";
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <arpa/nameser.h>
29 #include "ares_private.h"
32 /* Arguments passed to ares_gethostbyname() */
35 ares_host_callback callback;
38 const char *remaining_lookups;
41 static void next_lookup(struct host_query *hquery);
42 static void host_callback(void *arg, int status, unsigned char *abuf,
44 static void end_hquery(struct host_query *hquery, int status,
45 struct hostent *host);
46 static int fake_hostent(const char *name, ares_host_callback callback,
48 static int file_lookup(const char *name, struct hostent **host);
49 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
51 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
54 void ares_gethostbyname(ares_channel channel, const char *name, int family,
55 ares_host_callback callback, void *arg)
57 struct host_query *hquery;
59 /* Right now we only know how to look up Internet addresses. */
60 if (family != AF_INET)
62 callback(arg, ARES_ENOTIMP, NULL);
66 if (fake_hostent(name, callback, arg))
69 /* Allocate and fill in the host query structure. */
70 hquery = malloc(sizeof(struct host_query));
73 callback(arg, ARES_ENOMEM, NULL);
76 hquery->channel = channel;
77 hquery->name = strdup(name);
81 callback(arg, ARES_ENOMEM, NULL);
84 hquery->callback = callback;
86 hquery->remaining_lookups = channel->lookups;
88 /* Start performing lookups according to channel->lookups. */
92 static void next_lookup(struct host_query *hquery)
98 for (p = hquery->remaining_lookups; *p; p++)
104 hquery->remaining_lookups = p + 1;
105 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
110 /* Host file lookup */
111 status = file_lookup(hquery->name, &host);
112 if (status != ARES_ENOTFOUND)
114 end_hquery(hquery, status, host);
120 end_hquery(hquery, ARES_ENOTFOUND, NULL);
123 static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
125 struct host_query *hquery = (struct host_query *) arg;
126 ares_channel channel = hquery->channel;
127 struct hostent *host;
129 if (status == ARES_SUCCESS)
131 status = ares_parse_a_reply(abuf, alen, &host);
132 if (host && channel->nsort)
133 sort_addresses(host, channel->sortlist, channel->nsort);
134 end_hquery(hquery, status, host);
136 else if (status == ARES_EDESTRUCTION)
137 end_hquery(hquery, status, NULL);
142 static void end_hquery(struct host_query *hquery, int status,
143 struct hostent *host)
145 hquery->callback(hquery->arg, status, host);
147 ares_free_hostent(host);
152 /* If the name looks like an IP address, fake up a host entry, end the
153 * query immediately, and return true. Otherwise return false.
155 static int fake_hostent(const char *name, ares_host_callback callback,
159 struct hostent hostent;
161 char *aliases[1] = { NULL };
164 /* It only looks like an IP address if it's all numbers and dots. */
165 for (p = name; *p; p++)
167 if (!isdigit((unsigned char)*p) && *p != '.')
171 /* It also only looks like an IP address if it's non-zero-length and
172 * doesn't end with a dot.
174 if (p == name || *(p - 1) == '.')
177 /* It looks like an IP address. Figure out what IP address it is. */
178 addr.s_addr = inet_addr(name);
179 if (addr.s_addr == INADDR_NONE)
181 callback(arg, ARES_EBADNAME, NULL);
185 /* Duplicate the name, to avoid a constness violation. */
186 hostent.h_name = strdup(name);
189 callback(arg, ARES_ENOMEM, NULL);
193 /* Fill in the rest of the host structure and terminate the query. */
194 addrs[0] = (char *) &addr;
196 hostent.h_aliases = aliases;
197 hostent.h_addrtype = AF_INET;
198 hostent.h_length = sizeof(struct in_addr);
199 hostent.h_addr_list = addrs;
200 callback(arg, ARES_SUCCESS, &hostent);
202 free(hostent.h_name);
206 static int file_lookup(const char *name, struct hostent **host)
212 fp = fopen(PATH_HOSTS, "r");
214 return ARES_ENOTFOUND;
216 while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
218 if (strcasecmp((*host)->h_name, name) == 0)
220 for (alias = (*host)->h_aliases; *alias; alias++)
222 if (strcasecmp(*alias, name) == 0)
227 ares_free_hostent(*host);
230 if (status == ARES_EOF)
231 status = ARES_ENOTFOUND;
232 if (status != ARES_SUCCESS)
237 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
240 struct in_addr a1, a2;
241 int i1, i2, ind1, ind2;
243 /* This is a simple insertion sort, not optimized at all. i1 walks
244 * through the address list, with the loop invariant that everything
245 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
246 * back through the list (via i2) until it is in sorted order.
248 for (i1 = 0; host->h_addr_list[i1]; i1++)
250 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
251 ind1 = get_address_index(&a1, sortlist, nsort);
252 for (i2 = i1 - 1; i2 >= 0; i2--)
254 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
255 ind2 = get_address_index(&a2, sortlist, nsort);
258 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
260 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
264 /* Find the first entry in sortlist which matches addr. Return nsort
265 * if none of them match.
267 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
272 for (i = 0; i < nsort; i++)
274 if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)