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 #ifdef HAVE_SYS_SOCKET_H
24 #include <sys/socket.h>
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
32 #ifdef HAVE_ARPA_INET_H
33 #include <arpa/inet.h>
35 #ifdef HAVE_ARPA_NAMESER_H
36 #include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_H
39 #include <arpa/nameser.h>
41 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
42 #include <arpa/nameser_compat.h>
55 #include "inet_net_pton.h"
57 #include "ares_private.h"
64 /* Arguments passed to ares_gethostbyname() */
67 ares_host_callback callback;
70 const char *remaining_lookups;
74 static void next_lookup(struct host_query *hquery, int status_code);
75 static void host_callback(void *arg, int status, int timeouts,
76 unsigned char *abuf, int alen);
77 static void end_hquery(struct host_query *hquery, int status,
78 struct hostent *host);
79 static int fake_hostent(const char *name, int family, ares_host_callback callback,
81 static int file_lookup(const char *name, int family, struct hostent **host);
82 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
84 static void sort6_addresses(struct hostent *host, struct apattern *sortlist,
86 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
88 static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
91 void ares_gethostbyname(ares_channel channel, const char *name, int family,
92 ares_host_callback callback, void *arg)
94 struct host_query *hquery;
96 /* Right now we only know how to look up Internet addresses. */
97 if (family != AF_INET && family != AF_INET6)
99 callback(arg, ARES_ENOTIMP, 0, NULL);
103 if (fake_hostent(name, family, callback, arg))
106 /* Allocate and fill in the host query structure. */
107 hquery = malloc(sizeof(struct host_query));
110 callback(arg, ARES_ENOMEM, 0, NULL);
113 hquery->channel = channel;
114 hquery->name = strdup(name);
115 hquery->family = family;
119 callback(arg, ARES_ENOMEM, 0, NULL);
122 hquery->callback = callback;
124 hquery->remaining_lookups = channel->lookups;
125 hquery->timeouts = 0;
127 /* Start performing lookups according to channel->lookups. */
128 next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
131 static void next_lookup(struct host_query *hquery, int status_code)
134 struct hostent *host;
135 int status = status_code;
137 for (p = hquery->remaining_lookups; *p; p++)
143 hquery->remaining_lookups = p + 1;
144 if (hquery->family == AF_INET6)
145 ares_search(hquery->channel, hquery->name, C_IN, T_AAAA,
146 host_callback, hquery);
148 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
153 /* Host file lookup */
154 status = file_lookup(hquery->name, hquery->family, &host);
156 /* this status check below previously checked for !ARES_ENOTFOUND,
157 but we should not assume that this single error code is the one
158 that can occur, as that is in fact no longer the case */
159 if (status == ARES_SUCCESS)
161 end_hquery(hquery, status, host);
164 status = status_code; /* Use original status code */
168 end_hquery(hquery, status, NULL);
171 static void host_callback(void *arg, int status, int timeouts,
172 unsigned char *abuf, int alen)
174 struct host_query *hquery = (struct host_query *) arg;
175 ares_channel channel = hquery->channel;
176 struct hostent *host = NULL;
178 hquery->timeouts += timeouts;
179 if (status == ARES_SUCCESS)
181 if (hquery->family == AF_INET)
183 status = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
184 if (host && channel->nsort)
185 sort_addresses(host, channel->sortlist, channel->nsort);
187 else if (hquery->family == AF_INET6)
189 status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
190 if (status == ARES_ENODATA)
192 /* The query returned something (e.g. CNAME) but there were no
193 AAAA records. Try looking up A instead. */
194 hquery->family = AF_INET;
195 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
199 if (host && channel->nsort)
200 sort6_addresses(host, channel->sortlist, channel->nsort);
202 end_hquery(hquery, status, host);
204 else if (status == ARES_ENODATA && hquery->family == AF_INET6)
206 /* There was no AAAA. Now lookup an A */
207 hquery->family = AF_INET;
208 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
211 else if (status == ARES_EDESTRUCTION)
212 end_hquery(hquery, status, NULL);
214 next_lookup(hquery, status);
217 static void end_hquery(struct host_query *hquery, int status,
218 struct hostent *host)
220 hquery->callback(hquery->arg, status, hquery->timeouts, host);
222 ares_free_hostent(host);
227 /* If the name looks like an IP address, fake up a host entry, end the
228 * query immediately, and return true. Otherwise return false.
230 static int fake_hostent(const char *name, int family, ares_host_callback callback,
233 struct hostent hostent;
234 char *aliases[1] = { NULL };
240 if (family == AF_INET)
242 /* It only looks like an IP address if it's all numbers and dots. */
245 for (p = name; *p; p++)
247 if (!ISDIGIT(*p) && *p != '.') {
249 } else if (*p == '.') {
254 /* if we don't have 3 dots, it is illegal
255 * (although inet_addr doesn't think so).
260 result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1);
262 else if (family == AF_INET6)
263 result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
268 if (family == AF_INET)
270 hostent.h_length = sizeof(struct in_addr);
271 addrs[0] = (char *)∈
273 else if (family == AF_INET6)
275 hostent.h_length = sizeof(struct in6_addr);
276 addrs[0] = (char *)&in6;
278 /* Duplicate the name, to avoid a constness violation. */
279 hostent.h_name = strdup(name);
282 callback(arg, ARES_ENOMEM, 0, NULL);
286 /* Fill in the rest of the host structure and terminate the query. */
288 hostent.h_aliases = aliases;
289 hostent.h_addrtype = family;
290 hostent.h_addr_list = addrs;
291 callback(arg, ARES_SUCCESS, 0, &hostent);
293 free((char *)(hostent.h_name));
297 static int file_lookup(const char *name, int family, struct hostent **host)
305 char PATH_HOSTS[MAX_PATH];
310 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
313 DWORD dwLength = MAX_PATH;
314 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
316 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
317 RegCloseKey(hkeyHosts);
321 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
323 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
325 #elif defined(WATT32)
326 extern const char *_w32_GetHostsFile (void);
327 const char *PATH_HOSTS = _w32_GetHostsFile();
330 return ARES_ENOTFOUND;
333 fp = fopen(PATH_HOSTS, "r");
341 return ARES_ENOTFOUND;
343 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
344 error, strerror(error)));
345 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
351 while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
353 if (strcasecmp((*host)->h_name, name) == 0)
355 for (alias = (*host)->h_aliases; *alias; alias++)
357 if (strcasecmp(*alias, name) == 0)
362 ares_free_hostent(*host);
365 if (status == ARES_EOF)
366 status = ARES_ENOTFOUND;
367 if (status != ARES_SUCCESS)
372 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
375 struct in_addr a1, a2;
376 int i1, i2, ind1, ind2;
378 /* This is a simple insertion sort, not optimized at all. i1 walks
379 * through the address list, with the loop invariant that everything
380 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
381 * back through the list (via i2) until it is in sorted order.
383 for (i1 = 0; host->h_addr_list[i1]; i1++)
385 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
386 ind1 = get_address_index(&a1, sortlist, nsort);
387 for (i2 = i1 - 1; i2 >= 0; i2--)
389 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
390 ind2 = get_address_index(&a2, sortlist, nsort);
393 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
395 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
399 /* Find the first entry in sortlist which matches addr. Return nsort
400 * if none of them match.
402 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
407 for (i = 0; i < nsort; i++)
409 if (sortlist[i].family != AF_INET)
411 if (sortlist[i].type == PATTERN_MASK)
413 if ((addr->s_addr & sortlist[i].mask.addr.addr4.s_addr)
414 == sortlist[i].addr.addr4.s_addr)
419 if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addr.addr4.s_addr,
420 sortlist[i].mask.bits))
427 static void sort6_addresses(struct hostent *host, struct apattern *sortlist,
430 struct in6_addr a1, a2;
431 int i1, i2, ind1, ind2;
433 /* This is a simple insertion sort, not optimized at all. i1 walks
434 * through the address list, with the loop invariant that everything
435 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
436 * back through the list (via i2) until it is in sorted order.
438 for (i1 = 0; host->h_addr_list[i1]; i1++)
440 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in6_addr));
441 ind1 = get6_address_index(&a1, sortlist, nsort);
442 for (i2 = i1 - 1; i2 >= 0; i2--)
444 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in6_addr));
445 ind2 = get6_address_index(&a2, sortlist, nsort);
448 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in6_addr));
450 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in6_addr));
454 /* Find the first entry in sortlist which matches addr. Return nsort
455 * if none of them match.
457 static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
462 for (i = 0; i < nsort; i++)
464 if (sortlist[i].family != AF_INET6)
466 if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addr.addr6.s6_addr, sortlist[i].mask.bits))