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.
19 #if defined(WIN32) && !defined(WATT32)
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
27 #include <arpa/nameser_compat.h>
36 #include "inet_net_pton.h"
37 #include "ares_private.h"
44 /* Arguments passed to ares_gethostbyaddr() */
48 ares_host_callback callback;
51 const char *remaining_lookups;
55 static void next_lookup(struct addr_query *aquery);
56 static void addr_callback(void *arg, int status, int timeouts,
57 unsigned char *abuf, int alen);
58 static void end_aquery(struct addr_query *aquery, int status,
59 struct hostent *host);
60 static int file_lookup(union ares_addr *addr, int family, struct hostent **host);
61 static void ptr_rr_name(char *name, int family, union ares_addr *addr);
63 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
64 int family, ares_host_callback callback, void *arg)
66 struct addr_query *aquery;
68 if (family != AF_INET && family != AF_INET6)
70 callback(arg, ARES_ENOTIMP, 0, NULL);
74 if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
75 (family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
77 callback(arg, ARES_ENOTIMP, 0, NULL);
81 aquery = malloc(sizeof(struct addr_query));
84 callback(arg, ARES_ENOMEM, 0, NULL);
87 aquery->channel = channel;
88 if (family == AF_INET)
89 memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr));
91 memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr));
92 aquery->family = family;
93 aquery->callback = callback;
95 aquery->remaining_lookups = channel->lookups;
101 static void next_lookup(struct addr_query *aquery)
106 struct hostent *host;
108 for (p = aquery->remaining_lookups; *p; p++)
113 ptr_rr_name(name, aquery->family, &aquery->addr);
114 aquery->remaining_lookups = p + 1;
115 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
119 status = file_lookup(&aquery->addr, aquery->family, &host);
121 /* this status check below previously checked for !ARES_ENOTFOUND,
122 but we should not assume that this single error code is the one
123 that can occur, as that is in fact no longer the case */
124 if (status == ARES_SUCCESS)
126 end_aquery(aquery, status, host);
132 end_aquery(aquery, ARES_ENOTFOUND, NULL);
135 static void addr_callback(void *arg, int status, int timeouts,
136 unsigned char *abuf, int alen)
138 struct addr_query *aquery = (struct addr_query *) arg;
139 struct hostent *host;
141 aquery->timeouts += timeouts;
142 if (status == ARES_SUCCESS)
144 if (aquery->family == AF_INET)
145 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4,
146 sizeof(struct in_addr), AF_INET, &host);
148 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr6,
149 sizeof(struct in6_addr), AF_INET6, &host);
150 end_aquery(aquery, status, host);
152 else if (status == ARES_EDESTRUCTION)
153 end_aquery(aquery, status, NULL);
158 static void end_aquery(struct addr_query *aquery, int status,
159 struct hostent *host)
161 aquery->callback(aquery->arg, status, aquery->timeouts, host);
163 ares_free_hostent(host);
167 static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
174 char PATH_HOSTS[MAX_PATH];
179 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
182 DWORD dwLength = MAX_PATH;
183 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
185 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
186 RegCloseKey(hkeyHosts);
190 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
192 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
194 #elif defined(WATT32)
195 extern const char *_w32_GetHostsFile (void);
196 const char *PATH_HOSTS = _w32_GetHostsFile();
199 return ARES_ENOTFOUND;
202 fp = fopen(PATH_HOSTS, "r");
210 return ARES_ENOTFOUND;
212 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
213 error, strerror(error)));
214 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
220 while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
222 if (family != (*host)->h_addrtype)
224 ares_free_hostent(*host);
227 if (family == AF_INET)
229 if (memcmp((*host)->h_addr, &addr->addr4, sizeof(struct in_addr)) == 0)
232 else if (family == AF_INET6)
234 if (memcmp((*host)->h_addr, &addr->addr6, sizeof(struct in6_addr)) == 0)
237 ares_free_hostent(*host);
240 if (status == ARES_EOF)
241 status = ARES_ENOTFOUND;
242 if (status != ARES_SUCCESS)
247 static void ptr_rr_name(char *name, int family, union ares_addr *addr)
249 if (family == AF_INET)
251 unsigned long laddr = ntohl(addr->addr4.s_addr);
252 int a1 = (int)((laddr >> 24) & 0xff);
253 int a2 = (int)((laddr >> 16) & 0xff);
254 int a3 = (int)((laddr >> 8) & 0xff);
255 int a4 = (int)(laddr & 0xff);
256 sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
260 unsigned char *bytes = (unsigned char *)&addr->addr6.s6_addr;
262 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
263 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
264 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
265 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
266 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
267 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4,
268 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
269 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
270 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
271 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);