2 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 #include "ares_setup.h"
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
21 #ifdef HAVE_NETINET_IN_H
22 # include <netinet/in.h>
27 #ifdef HAVE_ARPA_INET_H
28 # include <arpa/inet.h>
30 #ifdef HAVE_ARPA_NAMESER_H
31 # include <arpa/nameser.h>
35 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
36 # include <arpa/nameser_compat.h>
44 #include "inet_net_pton.h"
45 #include "ares_platform.h"
46 #include "ares_private.h"
53 /* Arguments passed to ares_gethostbyaddr() */
55 struct ares_addr addr;
56 ares_host_callback callback;
59 const char *remaining_lookups;
63 static void next_lookup(struct addr_query *aquery);
64 static void addr_callback(void *arg, int status, int timeouts,
65 unsigned char *abuf, int alen);
66 static void end_aquery(struct addr_query *aquery, int status,
67 struct hostent *host);
68 static int file_lookup(struct ares_addr *addr, struct hostent **host);
69 static void ptr_rr_name(char *name, const struct ares_addr *addr);
71 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
72 int family, ares_host_callback callback, void *arg)
74 struct addr_query *aquery;
76 if (family != AF_INET && family != AF_INET6)
78 callback(arg, ARES_ENOTIMP, 0, NULL);
82 if ((family == AF_INET && addrlen != sizeof(aquery->addr.addrV4)) ||
83 (family == AF_INET6 && addrlen != sizeof(aquery->addr.addrV6)))
85 callback(arg, ARES_ENOTIMP, 0, NULL);
89 aquery = malloc(sizeof(struct addr_query));
92 callback(arg, ARES_ENOMEM, 0, NULL);
95 aquery->channel = channel;
96 if (family == AF_INET)
97 memcpy(&aquery->addr.addrV4, addr, sizeof(aquery->addr.addrV4));
99 memcpy(&aquery->addr.addrV6, addr, sizeof(aquery->addr.addrV6));
100 aquery->addr.family = family;
101 aquery->callback = callback;
103 aquery->remaining_lookups = channel->lookups;
104 aquery->timeouts = 0;
109 static void next_lookup(struct addr_query *aquery)
114 struct hostent *host;
116 for (p = aquery->remaining_lookups; *p; p++)
121 ptr_rr_name(name, &aquery->addr);
122 aquery->remaining_lookups = p + 1;
123 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
127 status = file_lookup(&aquery->addr, &host);
129 /* this status check below previously checked for !ARES_ENOTFOUND,
130 but we should not assume that this single error code is the one
131 that can occur, as that is in fact no longer the case */
132 if (status == ARES_SUCCESS)
134 end_aquery(aquery, status, host);
140 end_aquery(aquery, ARES_ENOTFOUND, NULL);
143 static void addr_callback(void *arg, int status, int timeouts,
144 unsigned char *abuf, int alen)
146 struct addr_query *aquery = (struct addr_query *) arg;
147 struct hostent *host;
150 aquery->timeouts += timeouts;
151 if (status == ARES_SUCCESS)
153 if (aquery->addr.family == AF_INET)
155 addrlen = sizeof(aquery->addr.addrV4);
156 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
157 (int)addrlen, AF_INET, &host);
161 addrlen = sizeof(aquery->addr.addrV6);
162 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6,
163 (int)addrlen, AF_INET6, &host);
165 end_aquery(aquery, status, host);
167 else if (status == ARES_EDESTRUCTION)
168 end_aquery(aquery, status, NULL);
173 static void end_aquery(struct addr_query *aquery, int status,
174 struct hostent *host)
176 aquery->callback(aquery->arg, status, aquery->timeouts, host);
178 ares_free_hostent(host);
182 static int file_lookup(struct ares_addr *addr, struct hostent **host)
189 char PATH_HOSTS[MAX_PATH];
190 win_platform platform;
192 PATH_HOSTS[0] = '\0';
194 platform = ares__getplatform();
196 if (platform == WIN_NT) {
200 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
201 &hkeyHosts) == ERROR_SUCCESS)
203 DWORD dwLength = MAX_PATH;
204 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
206 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
207 RegCloseKey(hkeyHosts);
210 else if (platform == WIN_9X)
211 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
213 return ARES_ENOTFOUND;
215 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
217 #elif defined(WATT32)
218 extern const char *_w32_GetHostsFile (void);
219 const char *PATH_HOSTS = _w32_GetHostsFile();
222 return ARES_ENOTFOUND;
225 fp = fopen(PATH_HOSTS, "r");
233 return ARES_ENOTFOUND;
235 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
236 error, strerror(error)));
237 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
243 while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS)
245 if (addr->family != (*host)->h_addrtype)
247 ares_free_hostent(*host);
250 if (addr->family == AF_INET)
252 if (memcmp((*host)->h_addr, &addr->addrV4,
253 sizeof(addr->addrV4)) == 0)
256 else if (addr->family == AF_INET6)
258 if (memcmp((*host)->h_addr, &addr->addrV6,
259 sizeof(addr->addrV6)) == 0)
262 ares_free_hostent(*host);
265 if (status == ARES_EOF)
266 status = ARES_ENOTFOUND;
267 if (status != ARES_SUCCESS)
272 static void ptr_rr_name(char *name, const struct ares_addr *addr)
274 if (addr->family == AF_INET)
276 unsigned long laddr = ntohl(addr->addrV4.s_addr);
277 unsigned long a1 = (laddr >> 24UL) & 0xFFUL;
278 unsigned long a2 = (laddr >> 16UL) & 0xFFUL;
279 unsigned long a3 = (laddr >> 8UL) & 0xFFUL;
280 unsigned long a4 = laddr & 0xFFUL;
281 sprintf(name, "%lu.%lu.%lu.%lu.in-addr.arpa", a4, a3, a2, a1);
285 unsigned char *bytes = (unsigned char *)&addr->addrV6;
286 /* There are too many arguments to do this in one line using
287 * minimally C89-compliant compilers */
289 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.",
290 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
291 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
292 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
293 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4);
294 sprintf(name+strlen(name),
295 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
296 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
297 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
298 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
299 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);