3 /* Copyright 1998 by the Massachusetts Institute of Technology.
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
20 #if defined(WIN32) && !defined(WATT32)
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 #include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #include <arpa/nameser_compat.h>
44 #include "inet_net_pton.h"
46 #include "ares_private.h"
53 /* Arguments passed to ares_gethostbyname() */
56 ares_host_callback callback;
59 const char *remaining_lookups;
63 static void next_lookup(struct host_query *hquery, int status_code);
64 static void host_callback(void *arg, int status, int timeouts,
65 unsigned char *abuf, int alen);
66 static void end_hquery(struct host_query *hquery, int status,
67 struct hostent *host);
68 static int fake_hostent(const char *name, int family, ares_host_callback callback,
70 static int file_lookup(const char *name, int family, struct hostent **host);
71 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
73 static void sort6_addresses(struct hostent *host, struct apattern *sortlist,
75 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
77 static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
80 void ares_gethostbyname(ares_channel channel, const char *name, int family,
81 ares_host_callback callback, void *arg)
83 struct host_query *hquery;
85 /* Right now we only know how to look up Internet addresses. */
86 if (family != AF_INET && family != AF_INET6)
88 callback(arg, ARES_ENOTIMP, 0, NULL);
92 if (fake_hostent(name, family, callback, arg))
95 /* Allocate and fill in the host query structure. */
96 hquery = malloc(sizeof(struct host_query));
99 callback(arg, ARES_ENOMEM, 0, NULL);
102 hquery->channel = channel;
103 hquery->name = strdup(name);
104 hquery->family = family;
108 callback(arg, ARES_ENOMEM, 0, NULL);
111 hquery->callback = callback;
113 hquery->remaining_lookups = channel->lookups;
114 hquery->timeouts = 0;
116 /* Start performing lookups according to channel->lookups. */
117 next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
120 static void next_lookup(struct host_query *hquery, int status_code)
123 struct hostent *host;
124 int status = status_code;
126 for (p = hquery->remaining_lookups; *p; p++)
132 hquery->remaining_lookups = p + 1;
133 if (hquery->family == AF_INET6)
134 ares_search(hquery->channel, hquery->name, C_IN, T_AAAA,
135 host_callback, hquery);
137 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
142 /* Host file lookup */
143 status = file_lookup(hquery->name, hquery->family, &host);
145 /* this status check below previously checked for !ARES_ENOTFOUND,
146 but we should not assume that this single error code is the one
147 that can occur, as that is in fact no longer the case */
148 if (status == ARES_SUCCESS)
150 end_hquery(hquery, status, host);
153 status = status_code; /* Use original status code */
157 end_hquery(hquery, status, NULL);
160 static void host_callback(void *arg, int status, int timeouts,
161 unsigned char *abuf, int alen)
163 struct host_query *hquery = (struct host_query *) arg;
164 ares_channel channel = hquery->channel;
165 struct hostent *host = NULL;
167 hquery->timeouts += timeouts;
168 if (status == ARES_SUCCESS)
170 if (hquery->family == AF_INET)
172 status = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
173 if (host && channel->nsort)
174 sort_addresses(host, channel->sortlist, channel->nsort);
176 else if (hquery->family == AF_INET6)
178 status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
179 if (status == ARES_ENODATA)
181 /* The query returned something (e.g. CNAME) but there were no
182 AAAA records. Try looking up A instead. */
183 hquery->family = AF_INET;
184 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
188 if (host && channel->nsort)
189 sort6_addresses(host, channel->sortlist, channel->nsort);
191 end_hquery(hquery, status, host);
193 else if (status == ARES_ENODATA && hquery->family == AF_INET6)
195 /* There was no AAAA. Now lookup an A */
196 hquery->family = AF_INET;
197 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
200 else if (status == ARES_EDESTRUCTION)
201 end_hquery(hquery, status, NULL);
203 next_lookup(hquery, status);
206 static void end_hquery(struct host_query *hquery, int status,
207 struct hostent *host)
209 hquery->callback(hquery->arg, status, hquery->timeouts, host);
211 ares_free_hostent(host);
216 /* If the name looks like an IP address, fake up a host entry, end the
217 * query immediately, and return true. Otherwise return false.
219 static int fake_hostent(const char *name, int family, ares_host_callback callback,
222 struct hostent hostent;
223 char *aliases[1] = { NULL };
229 if (family == AF_INET)
231 /* It only looks like an IP address if it's all numbers and dots. */
234 for (p = name; *p; p++)
236 if (!ISDIGIT(*p) && *p != '.') {
238 } else if (*p == '.') {
243 /* if we don't have 3 dots, it is illegal
244 * (although inet_addr doesn't think so).
249 result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1);
251 else if (family == AF_INET6)
252 result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
257 if (family == AF_INET)
259 hostent.h_length = sizeof(struct in_addr);
260 addrs[0] = (char *)∈
262 else if (family == AF_INET6)
264 hostent.h_length = sizeof(struct in6_addr);
265 addrs[0] = (char *)&in6;
267 /* Duplicate the name, to avoid a constness violation. */
268 hostent.h_name = strdup(name);
271 callback(arg, ARES_ENOMEM, 0, NULL);
275 /* Fill in the rest of the host structure and terminate the query. */
277 hostent.h_aliases = aliases;
278 hostent.h_addrtype = family;
279 hostent.h_addr_list = addrs;
280 callback(arg, ARES_SUCCESS, 0, &hostent);
282 free((char *)(hostent.h_name));
286 static int file_lookup(const char *name, int family, struct hostent **host)
294 char PATH_HOSTS[MAX_PATH];
299 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
302 DWORD dwLength = MAX_PATH;
303 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
305 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
306 RegCloseKey(hkeyHosts);
310 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
312 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
314 #elif defined(WATT32)
315 extern const char *_w32_GetHostsFile (void);
316 const char *PATH_HOSTS = _w32_GetHostsFile();
319 return ARES_ENOTFOUND;
322 fp = fopen(PATH_HOSTS, "r");
330 return ARES_ENOTFOUND;
332 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
333 error, strerror(error)));
334 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
340 while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
342 if (strcasecmp((*host)->h_name, name) == 0)
344 for (alias = (*host)->h_aliases; *alias; alias++)
346 if (strcasecmp(*alias, name) == 0)
351 ares_free_hostent(*host);
354 if (status == ARES_EOF)
355 status = ARES_ENOTFOUND;
356 if (status != ARES_SUCCESS)
361 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
364 struct in_addr a1, a2;
365 int i1, i2, ind1, ind2;
367 /* This is a simple insertion sort, not optimized at all. i1 walks
368 * through the address list, with the loop invariant that everything
369 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
370 * back through the list (via i2) until it is in sorted order.
372 for (i1 = 0; host->h_addr_list[i1]; i1++)
374 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
375 ind1 = get_address_index(&a1, sortlist, nsort);
376 for (i2 = i1 - 1; i2 >= 0; i2--)
378 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
379 ind2 = get_address_index(&a2, sortlist, nsort);
382 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
384 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
388 /* Find the first entry in sortlist which matches addr. Return nsort
389 * if none of them match.
391 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
396 for (i = 0; i < nsort; i++)
398 if (sortlist[i].family != AF_INET)
400 if (sortlist[i].type == PATTERN_MASK)
402 if ((addr->s_addr & sortlist[i].mask.addr.addr4.s_addr)
403 == sortlist[i].addr.addr4.s_addr)
408 if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addr.addr4.s_addr,
409 sortlist[i].mask.bits))
416 static void sort6_addresses(struct hostent *host, struct apattern *sortlist,
419 struct in6_addr a1, a2;
420 int i1, i2, ind1, ind2;
422 /* This is a simple insertion sort, not optimized at all. i1 walks
423 * through the address list, with the loop invariant that everything
424 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
425 * back through the list (via i2) until it is in sorted order.
427 for (i1 = 0; host->h_addr_list[i1]; i1++)
429 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in6_addr));
430 ind1 = get6_address_index(&a1, sortlist, nsort);
431 for (i2 = i1 - 1; i2 >= 0; i2--)
433 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in6_addr));
434 ind2 = get6_address_index(&a2, sortlist, nsort);
437 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in6_addr));
439 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in6_addr));
443 /* Find the first entry in sortlist which matches addr. Return nsort
444 * if none of them match.
446 static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
451 for (i = 0; i < nsort; i++)
453 if (sortlist[i].family != AF_INET6)
455 if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addr.addr6.s6_addr, sortlist[i].mask.bits))