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.
18 #include <sys/types.h>
20 #if defined(WIN32) && !defined(WATT32)
23 #include <sys/socket.h>
24 #include <netinet/in.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
37 #include "ares_private.h"
38 #include "inet_net_pton.h"
45 /* Arguments passed to ares_gethostbyaddr() */
49 ares_host_callback callback;
52 const char *remaining_lookups;
55 static void next_lookup(struct addr_query *aquery);
56 static void addr_callback(void *arg, int status, unsigned char *abuf,
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);
62 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
63 int family, ares_host_callback callback, void *arg)
65 struct addr_query *aquery;
67 if (family != AF_INET && family != AF_INET6)
69 callback(arg, ARES_ENOTIMP, NULL);
73 if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
74 (family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
76 callback(arg, ARES_ENOTIMP, NULL);
80 aquery = malloc(sizeof(struct addr_query));
83 callback(arg, ARES_ENOMEM, NULL);
86 aquery->channel = channel;
87 if (family == AF_INET)
88 memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr));
90 memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr));
91 aquery->family = family;
92 aquery->callback = callback;
94 aquery->remaining_lookups = channel->lookups;
99 static void next_lookup(struct addr_query *aquery)
103 int a1, a2, a3, a4, status;
104 struct hostent *host;
107 for (p = aquery->remaining_lookups; *p; p++)
112 if (aquery->family == AF_INET)
114 addr = ntohl(aquery->addr.addr4.s_addr);
115 a1 = (int)((addr >> 24) & 0xff);
116 a2 = (int)((addr >> 16) & 0xff);
117 a3 = (int)((addr >> 8) & 0xff);
118 a4 = (int)(addr & 0xff);
119 sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
120 aquery->remaining_lookups = p + 1;
121 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
126 unsigned char *bytes;
127 bytes = (unsigned char *)&aquery->addr.addr6.s6_addr;
128 sprintf(name, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
129 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
130 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
131 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
132 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4,
133 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
134 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
135 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
136 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);
137 aquery->remaining_lookups = p + 1;
138 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
143 status = file_lookup(&aquery->addr, aquery->family, &host);
144 if (status != ARES_ENOTFOUND)
146 end_aquery(aquery, status, host);
152 end_aquery(aquery, ARES_ENOTFOUND, NULL);
155 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
157 struct addr_query *aquery = (struct addr_query *) arg;
158 struct hostent *host;
160 if (status == ARES_SUCCESS)
162 if (aquery->family == AF_INET)
163 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4,
164 sizeof(struct in_addr), AF_INET, &host);
166 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr6,
167 sizeof(struct in6_addr), AF_INET6, &host);
168 end_aquery(aquery, status, host);
170 else if (status == ARES_EDESTRUCTION)
171 end_aquery(aquery, status, NULL);
176 static void end_aquery(struct addr_query *aquery, int status,
177 struct hostent *host)
179 aquery->callback(aquery->arg, status, host);
181 ares_free_hostent(host);
185 static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
191 char PATH_HOSTS[MAX_PATH];
196 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
199 DWORD dwLength = MAX_PATH;
200 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
202 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
203 RegCloseKey(hkeyHosts);
207 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
209 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
211 #elif defined(WATT32)
212 extern const char *_w32_GetHostsFile (void);
213 const char *PATH_HOSTS = _w32_GetHostsFile();
216 return ARES_ENOTFOUND;
219 fp = fopen(PATH_HOSTS, "r");
221 return ARES_ENOTFOUND;
222 while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
224 if (family != (*host)->h_addrtype)
226 ares_free_hostent(*host);
229 if (family == AF_INET)
231 if (memcmp((*host)->h_addr, &addr->addr4, sizeof(struct in_addr)) == 0)
234 else if (family == AF_INET6)
236 if (memcmp((*host)->h_addr, &addr->addr6, sizeof(struct in6_addr)) == 0)
239 ares_free_hostent(*host);
242 if (status == ARES_EOF)
243 status = ARES_ENOTFOUND;
244 if (status != ARES_SUCCESS)