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_NETINET_IN_H
19 # include <netinet/in.h>
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
32 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
33 # include <arpa/nameser_compat.h>
37 #include "ares_inet_net_pton.h"
38 #include "ares_platform.h"
39 #include "ares_private.h"
46 /* Arguments passed to ares_gethostbyaddr() */
48 struct ares_addr addr;
49 ares_host_callback callback;
52 const char *remaining_lookups;
56 static void next_lookup(struct addr_query *aquery);
57 static void addr_callback(void *arg, int status, int timeouts,
58 unsigned char *abuf, int alen);
59 static void end_aquery(struct addr_query *aquery, int status,
60 struct hostent *host);
61 static int file_lookup(struct ares_addr *addr, struct hostent **host);
62 static void ptr_rr_name(char *name, const struct ares_addr *addr);
64 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
65 int family, ares_host_callback callback, void *arg)
67 struct addr_query *aquery;
69 if (family != AF_INET && family != AF_INET6)
71 callback(arg, ARES_ENOTIMP, 0, NULL);
75 if ((family == AF_INET && addrlen != sizeof(aquery->addr.addrV4)) ||
76 (family == AF_INET6 && addrlen != sizeof(aquery->addr.addrV6)))
78 callback(arg, ARES_ENOTIMP, 0, NULL);
82 aquery = malloc(sizeof(struct addr_query));
85 callback(arg, ARES_ENOMEM, 0, NULL);
88 aquery->channel = channel;
89 if (family == AF_INET)
90 memcpy(&aquery->addr.addrV4, addr, sizeof(aquery->addr.addrV4));
92 memcpy(&aquery->addr.addrV6, addr, sizeof(aquery->addr.addrV6));
93 aquery->addr.family = family;
94 aquery->callback = callback;
96 aquery->remaining_lookups = channel->lookups;
102 static void next_lookup(struct addr_query *aquery)
107 struct hostent *host;
109 for (p = aquery->remaining_lookups; *p; p++)
114 ptr_rr_name(name, &aquery->addr);
115 aquery->remaining_lookups = p + 1;
116 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
120 status = file_lookup(&aquery->addr, &host);
122 /* this status check below previously checked for !ARES_ENOTFOUND,
123 but we should not assume that this single error code is the one
124 that can occur, as that is in fact no longer the case */
125 if (status == ARES_SUCCESS)
127 end_aquery(aquery, status, host);
133 end_aquery(aquery, ARES_ENOTFOUND, NULL);
136 static void addr_callback(void *arg, int status, int timeouts,
137 unsigned char *abuf, int alen)
139 struct addr_query *aquery = (struct addr_query *) arg;
140 struct hostent *host;
143 aquery->timeouts += timeouts;
144 if (status == ARES_SUCCESS)
146 if (aquery->addr.family == AF_INET)
148 addrlen = sizeof(aquery->addr.addrV4);
149 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
150 (int)addrlen, AF_INET, &host);
154 addrlen = sizeof(aquery->addr.addrV6);
155 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6,
156 (int)addrlen, AF_INET6, &host);
158 end_aquery(aquery, status, host);
160 else if (status == ARES_EDESTRUCTION)
161 end_aquery(aquery, status, NULL);
166 static void end_aquery(struct addr_query *aquery, int status,
167 struct hostent *host)
169 aquery->callback(aquery->arg, status, aquery->timeouts, host);
171 ares_free_hostent(host);
175 static int file_lookup(struct ares_addr *addr, struct hostent **host)
182 char PATH_HOSTS[MAX_PATH];
183 win_platform platform;
185 PATH_HOSTS[0] = '\0';
187 platform = ares__getplatform();
189 if (platform == WIN_NT) {
193 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
194 &hkeyHosts) == ERROR_SUCCESS)
196 DWORD dwLength = MAX_PATH;
197 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
199 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
200 RegCloseKey(hkeyHosts);
203 else if (platform == WIN_9X)
204 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
206 return ARES_ENOTFOUND;
208 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
210 #elif defined(WATT32)
211 extern const char *_w32_GetHostsFile (void);
212 const char *PATH_HOSTS = _w32_GetHostsFile();
215 return ARES_ENOTFOUND;
218 fp = fopen(PATH_HOSTS, "r");
226 return ARES_ENOTFOUND;
228 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
229 error, strerror(error)));
230 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
236 while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS)
238 if (addr->family != (*host)->h_addrtype)
240 ares_free_hostent(*host);
243 if (addr->family == AF_INET)
245 if (memcmp((*host)->h_addr, &addr->addrV4,
246 sizeof(addr->addrV4)) == 0)
249 else if (addr->family == AF_INET6)
251 if (memcmp((*host)->h_addr, &addr->addrV6,
252 sizeof(addr->addrV6)) == 0)
255 ares_free_hostent(*host);
258 if (status == ARES_EOF)
259 status = ARES_ENOTFOUND;
260 if (status != ARES_SUCCESS)
265 static void ptr_rr_name(char *name, const struct ares_addr *addr)
267 if (addr->family == AF_INET)
269 unsigned long laddr = ntohl(addr->addrV4.s_addr);
270 unsigned long a1 = (laddr >> 24UL) & 0xFFUL;
271 unsigned long a2 = (laddr >> 16UL) & 0xFFUL;
272 unsigned long a3 = (laddr >> 8UL) & 0xFFUL;
273 unsigned long a4 = laddr & 0xFFUL;
274 sprintf(name, "%lu.%lu.%lu.%lu.in-addr.arpa", a4, a3, a2, a1);
278 unsigned char *bytes = (unsigned char *)&addr->addrV6;
279 /* There are too many arguments to do this in one line using
280 * minimally C89-compliant compilers */
282 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.",
283 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
284 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
285 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
286 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4);
287 sprintf(name+strlen(name),
288 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
289 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
290 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
291 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
292 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);