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>
19 #if defined(WIN32) && !defined(WATT32)
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
38 #include "ares_private.h"
45 /* Arguments passed to ares_gethostbyname() */
48 ares_host_callback callback;
51 const char *remaining_lookups;
54 static void next_lookup(struct host_query *hquery);
55 static void host_callback(void *arg, int status, unsigned char *abuf,
57 static void end_hquery(struct host_query *hquery, int status,
58 struct hostent *host);
59 static int fake_hostent(const char *name, ares_host_callback callback,
61 static int file_lookup(const char *name, struct hostent **host);
62 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
64 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
67 void ares_gethostbyname(ares_channel channel, const char *name, int family,
68 ares_host_callback callback, void *arg)
70 struct host_query *hquery;
72 /* Right now we only know how to look up Internet addresses. */
73 if (family != AF_INET)
75 callback(arg, ARES_ENOTIMP, NULL);
79 if (fake_hostent(name, callback, arg))
82 /* Allocate and fill in the host query structure. */
83 hquery = malloc(sizeof(struct host_query));
86 callback(arg, ARES_ENOMEM, NULL);
89 hquery->channel = channel;
90 hquery->name = strdup(name);
94 callback(arg, ARES_ENOMEM, NULL);
97 hquery->callback = callback;
99 hquery->remaining_lookups = channel->lookups;
101 /* Start performing lookups according to channel->lookups. */
105 static void next_lookup(struct host_query *hquery)
109 struct hostent *host;
111 for (p = hquery->remaining_lookups; *p; p++)
117 hquery->remaining_lookups = p + 1;
118 ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
123 /* Host file lookup */
124 status = file_lookup(hquery->name, &host);
125 if (status != ARES_ENOTFOUND)
127 end_hquery(hquery, status, host);
133 end_hquery(hquery, ARES_ENOTFOUND, NULL);
136 static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
138 struct host_query *hquery = (struct host_query *) arg;
139 ares_channel channel = hquery->channel;
140 struct hostent *host;
142 if (status == ARES_SUCCESS)
144 status = ares_parse_a_reply(abuf, alen, &host);
145 if (host && channel->nsort)
146 sort_addresses(host, channel->sortlist, channel->nsort);
147 end_hquery(hquery, status, host);
149 else if (status == ARES_EDESTRUCTION)
150 end_hquery(hquery, status, NULL);
155 static void end_hquery(struct host_query *hquery, int status,
156 struct hostent *host)
158 hquery->callback(hquery->arg, status, host);
160 ares_free_hostent(host);
165 /* If the name looks like an IP address, fake up a host entry, end the
166 * query immediately, and return true. Otherwise return false.
168 static int fake_hostent(const char *name, ares_host_callback callback,
172 struct hostent hostent;
174 char *aliases[1] = { NULL };
177 /* It only looks like an IP address if it's all numbers and dots. */
178 for (p = name; *p; p++)
180 if (!isdigit((unsigned char)*p) && *p != '.')
184 /* It also only looks like an IP address if it's non-zero-length and
185 * doesn't end with a dot.
187 if (p == name || *(p - 1) == '.')
190 /* It looks like an IP address. Figure out what IP address it is. */
191 addr.s_addr = inet_addr(name);
192 if (addr.s_addr == INADDR_NONE)
194 callback(arg, ARES_EBADNAME, NULL);
198 /* Duplicate the name, to avoid a constness violation. */
199 hostent.h_name = strdup(name);
202 callback(arg, ARES_ENOMEM, NULL);
206 /* Fill in the rest of the host structure and terminate the query. */
207 addrs[0] = (char *) &addr;
209 hostent.h_aliases = aliases;
210 hostent.h_addrtype = AF_INET;
211 hostent.h_length = sizeof(struct in_addr);
212 hostent.h_addr_list = addrs;
213 callback(arg, ARES_SUCCESS, &hostent);
215 free(hostent.h_name);
219 static int file_lookup(const char *name, struct hostent **host)
226 char PATH_HOSTS[MAX_PATH];
231 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
234 DWORD dwLength = MAX_PATH;
235 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
237 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
238 RegCloseKey(hkeyHosts);
242 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
244 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
246 #elif defined(WATT32)
247 extern const char *_w32_GetHostsFile (void);
248 const char *PATH_HOSTS = _w32_GetHostsFile();
251 return ARES_ENOTFOUND;
254 fp = fopen(PATH_HOSTS, "r");
256 return ARES_ENOTFOUND;
258 while ((status = ares__get_hostent(fp, AF_INET, host)) == ARES_SUCCESS)
260 if (strcasecmp((*host)->h_name, name) == 0)
262 for (alias = (*host)->h_aliases; *alias; alias++)
264 if (strcasecmp(*alias, name) == 0)
269 ares_free_hostent(*host);
272 if (status == ARES_EOF)
273 status = ARES_ENOTFOUND;
274 if (status != ARES_SUCCESS)
279 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
282 struct in_addr a1, a2;
283 int i1, i2, ind1, ind2;
285 /* This is a simple insertion sort, not optimized at all. i1 walks
286 * through the address list, with the loop invariant that everything
287 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
288 * back through the list (via i2) until it is in sorted order.
290 for (i1 = 0; host->h_addr_list[i1]; i1++)
292 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
293 ind1 = get_address_index(&a1, sortlist, nsort);
294 for (i2 = i1 - 1; i2 >= 0; i2--)
296 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
297 ind2 = get_address_index(&a2, sortlist, nsort);
300 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
302 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
306 /* Find the first entry in sortlist which matches addr. Return nsort
307 * if none of them match.
309 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
314 for (i = 0; i < nsort; i++)
316 if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)