Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 301 lines (270 sloc) 8.717 kB
46774e5 Detabified. Added CVS id.
Gisle Vanem authored
1
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 *
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.
15 */
a82a8fb @yangtse Renamed c-ares setup.h to ares_setup.h
yangtse authored
16 #include "ares_setup.h"
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
17
f2582d6 @yangtse include header file only when available
yangtse authored
18 #ifdef HAVE_SYS_SOCKET_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
19 # include <sys/socket.h>
f2582d6 @yangtse include header file only when available
yangtse authored
20 #endif
21 #ifdef HAVE_NETINET_IN_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
22 # include <netinet/in.h>
f2582d6 @yangtse include header file only when available
yangtse authored
23 #endif
24 #ifdef HAVE_NETDB_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
25 # include <netdb.h>
f2582d6 @yangtse include header file only when available
yangtse authored
26 #endif
27 #ifdef HAVE_ARPA_INET_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
28 # include <arpa/inet.h>
f2582d6 @yangtse include header file only when available
yangtse authored
29 #endif
30 #ifdef HAVE_ARPA_NAMESER_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
31 # include <arpa/nameser.h>
32 #else
33 # include "nameser.h"
f2582d6 @yangtse include header file only when available
yangtse authored
34 #endif
b41effd @bagder Tupone Alfredo fixed includes of arpa/nameser_compat.h to build fine …
authored
35 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
f214b58 @yangtse adjust inclusion of "nameser.h"
yangtse authored
36 # include <arpa/nameser_compat.h>
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
0f51a17 More patches for Watt-32 on Win32; don't include "nameser.h".
Gisle Vanem authored
42
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
43 #include "ares.h"
00b7a55 Added preliminary IPv6 support to ares_gethostbyname
Dominick Meglio authored
44 #include "inet_net_pton.h"
2cb5cbb @yangtse move inclusion of ares_private.h last
yangtse authored
45 #include "ares_private.h"
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
46
0f51a17 More patches for Watt-32 on Win32; don't include "nameser.h".
Gisle Vanem authored
47 #ifdef WATT32
48 #undef WIN32
49 #endif
50
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
51 struct addr_query {
52 /* Arguments passed to ares_gethostbyaddr() */
53 ares_channel channel;
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
54 struct ares_addr addr;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
55 ares_host_callback callback;
56 void *arg;
57
58 const char *remaining_lookups;
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
59 int timeouts;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
60 };
61
62 static void next_lookup(struct addr_query *aquery);
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
63 static void addr_callback(void *arg, int status, int timeouts,
64 unsigned char *abuf, int alen);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
65 static void end_aquery(struct addr_query *aquery, int status,
994f16d @bagder untabified
authored
66 struct hostent *host);
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
67 static int file_lookup(struct ares_addr *addr, struct hostent **host);
f94afa4 Constified some arguments in local functions.
Gisle Vanem authored
68 static void ptr_rr_name(char *name, const struct ares_addr *addr);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
69
70 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
994f16d @bagder untabified
authored
71 int family, ares_host_callback callback, void *arg)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
72 {
73 struct addr_query *aquery;
74
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
75 if (family != AF_INET && family != AF_INET6)
76 {
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
77 callback(arg, ARES_ENOTIMP, 0, NULL);
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
78 return;
79 }
80
8fe746f @yangtse Added IPv6 name servers support
yangtse authored
81 if ((family == AF_INET && addrlen != sizeof(aquery->addr.addrV4)) ||
82 (family == AF_INET6 && addrlen != sizeof(aquery->addr.addrV6)))
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
83 {
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
84 callback(arg, ARES_ENOTIMP, 0, NULL);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
85 return;
86 }
87
88 aquery = malloc(sizeof(struct addr_query));
89 if (!aquery)
90 {
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
91 callback(arg, ARES_ENOMEM, 0, NULL);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
92 return;
93 }
94 aquery->channel = channel;
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
95 if (family == AF_INET)
8fe746f @yangtse Added IPv6 name servers support
yangtse authored
96 memcpy(&aquery->addr.addrV4, addr, sizeof(aquery->addr.addrV4));
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
97 else
8fe746f @yangtse Added IPv6 name servers support
yangtse authored
98 memcpy(&aquery->addr.addrV6, addr, sizeof(aquery->addr.addrV6));
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
99 aquery->addr.family = family;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
100 aquery->callback = callback;
101 aquery->arg = arg;
102 aquery->remaining_lookups = channel->lookups;
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
103 aquery->timeouts = 0;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
104
105 next_lookup(aquery);
106 }
107
108 static void next_lookup(struct addr_query *aquery)
109 {
110 const char *p;
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
111 char name[128];
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
112 int status;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
113 struct hostent *host;
114
115 for (p = aquery->remaining_lookups; *p; p++)
116 {
117 switch (*p)
994f16d @bagder untabified
authored
118 {
119 case 'b':
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
120 ptr_rr_name(name, &aquery->addr);
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
121 aquery->remaining_lookups = p + 1;
122 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
123 aquery);
994f16d @bagder untabified
authored
124 return;
125 case 'f':
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
126 status = file_lookup(&aquery->addr, &host);
9c143e4 @bagder the gethostbyname fix applied here as well
authored
127
128 /* this status check below previously checked for !ARES_ENOTFOUND,
129 but we should not assume that this single error code is the one
130 that can occur, as that is in fact no longer the case */
131 if (status == ARES_SUCCESS)
994f16d @bagder untabified
authored
132 {
133 end_aquery(aquery, status, host);
134 return;
135 }
136 break;
137 }
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
138 }
139 end_aquery(aquery, ARES_ENOTFOUND, NULL);
140 }
141
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
142 static void addr_callback(void *arg, int status, int timeouts,
143 unsigned char *abuf, int alen)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
144 {
145 struct addr_query *aquery = (struct addr_query *) arg;
146 struct hostent *host;
737707b @yangtse fix compiler warning: implicit conversion shortens 64-bit value into …
yangtse authored
147 size_t addrlen;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
148
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
149 aquery->timeouts += timeouts;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
150 if (status == ARES_SUCCESS)
151 {
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
152 if (aquery->addr.family == AF_INET)
737707b @yangtse fix compiler warning: implicit conversion shortens 64-bit value into …
yangtse authored
153 {
8fe746f @yangtse Added IPv6 name servers support
yangtse authored
154 addrlen = sizeof(aquery->addr.addrV4);
737707b @yangtse fix compiler warning: implicit conversion shortens 64-bit value into …
yangtse authored
155 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
156 (int)addrlen, AF_INET, &host);
157 }
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
158 else
737707b @yangtse fix compiler warning: implicit conversion shortens 64-bit value into …
yangtse authored
159 {
8fe746f @yangtse Added IPv6 name servers support
yangtse authored
160 addrlen = sizeof(aquery->addr.addrV6);
737707b @yangtse fix compiler warning: implicit conversion shortens 64-bit value into …
yangtse authored
161 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6,
162 (int)addrlen, AF_INET6, &host);
163 }
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
164 end_aquery(aquery, status, host);
165 }
166 else if (status == ARES_EDESTRUCTION)
167 end_aquery(aquery, status, NULL);
168 else
169 next_lookup(aquery);
170 }
171
172 static void end_aquery(struct addr_query *aquery, int status,
994f16d @bagder untabified
authored
173 struct hostent *host)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
174 {
50ba81c @sesse Make the query callbacks return the number of timeouts that happened …
sesse authored
175 aquery->callback(aquery->arg, status, aquery->timeouts, host);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
176 if (host)
177 ares_free_hostent(host);
178 free(aquery);
179 }
180
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
181 static int file_lookup(struct ares_addr *addr, struct hostent **host)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
182 {
183 FILE *fp;
184 int status;
45b2661 @yangtse compiler warning fix
yangtse authored
185 int error;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
186
187 #ifdef WIN32
188 char PATH_HOSTS[MAX_PATH];
bd066ab @yangtse build: find out windows platform using GetVersionEx()
yangtse authored
189 win_platform platform;
190
191 PATH_HOSTS[0] = '\0';
192
193 platform = getplatform();
194
195 if (platform == WIN_NT) {
6010f45 @bagder typecase to fix win32 compiler warning (and intended as other code is)
authored
196 char tmp[MAX_PATH];
197 HKEY hkeyHosts;
994f16d @bagder untabified
authored
198
0e8dc6f @bagder style: make code less than 80 columns wide
authored
199 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
200 &hkeyHosts) == ERROR_SUCCESS)
6010f45 @bagder typecase to fix win32 compiler warning (and intended as other code is)
authored
201 {
202 DWORD dwLength = MAX_PATH;
203 RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
204 &dwLength);
205 ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
206 RegCloseKey(hkeyHosts);
207 }
3e748f4 @bagder removed tabs and trailing whitespace from source
authored
208 }
bd066ab @yangtse build: find out windows platform using GetVersionEx()
yangtse authored
209 else if (platform == WIN_9X)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
210 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
bd066ab @yangtse build: find out windows platform using GetVersionEx()
yangtse authored
211 else
212 return ARES_ENOTFOUND;
2bf866b @bagder Dominick Meglio host file path discovery patch for windows
authored
213
214 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
215
e7293e0 @bagder djgpp fixes by Gisle
authored
216 #elif defined(WATT32)
217 extern const char *_w32_GetHostsFile (void);
218 const char *PATH_HOSTS = _w32_GetHostsFile();
219
220 if (!PATH_HOSTS)
221 return ARES_ENOTFOUND;
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
222 #endif
223
224 fp = fopen(PATH_HOSTS, "r");
225 if (!fp)
8595ddf @yangtse add debug messages for fopen() failures
yangtse authored
226 {
227 error = ERRNO;
a627160 Removed inclusion of <sys/types.h> in .c-files
Gisle Vanem authored
228 switch(error)
8595ddf @yangtse add debug messages for fopen() failures
yangtse authored
229 {
230 case ENOENT:
679a675 @yangtse add debug messages for initialization failures
yangtse authored
231 case ESRCH:
8595ddf @yangtse add debug messages for fopen() failures
yangtse authored
232 return ARES_ENOTFOUND;
233 default:
234 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
235 error, strerror(error)));
a627160 Removed inclusion of <sys/types.h> in .c-files
Gisle Vanem authored
236 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
8595ddf @yangtse add debug messages for fopen() failures
yangtse authored
237 PATH_HOSTS));
238 *host = NULL;
239 return ARES_EFILE;
240 }
241 }
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
242 while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS)
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
243 {
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
244 if (addr->family != (*host)->h_addrtype)
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
245 {
246 ares_free_hostent(*host);
247 continue;
248 }
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
249 if (addr->family == AF_INET)
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
250 {
0e8dc6f @bagder style: make code less than 80 columns wide
authored
251 if (memcmp((*host)->h_addr, &addr->addrV4,
252 sizeof(addr->addrV4)) == 0)
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
253 break;
254 }
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
255 else if (addr->family == AF_INET6)
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
256 {
0e8dc6f @bagder style: make code less than 80 columns wide
authored
257 if (memcmp((*host)->h_addr, &addr->addrV6,
258 sizeof(addr->addrV6)) == 0)
29d71bd Made ares_gethostbyaddr support IPv6 by specifying AF_INET6 as the fa…
Dominick Meglio authored
259 break;
260 }
5e847a1 @bagder ares 1.1.1 with collected applied patches
authored
261 ares_free_hostent(*host);
262 }
263 fclose(fp);
264 if (status == ARES_EOF)
265 status = ARES_ENOTFOUND;
266 if (status != ARES_SUCCESS)
267 *host = NULL;
268 return status;
269 }
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
270
f94afa4 Constified some arguments in local functions.
Gisle Vanem authored
271 static void ptr_rr_name(char *name, const struct ares_addr *addr)
6606c99 @yangtse Fix three issues previous cleanup introduces.
yangtse authored
272 {
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
273 if (addr->family == AF_INET)
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
274 {
aa1534e @yangtse In preparation for the upcomming IPv6 nameservers patch, the internal
yangtse authored
275 unsigned long laddr = ntohl(addr->addrV4.s_addr);
fb4df6f @yangtse ares_gethostbyaddr: fix compiler warning: conversion may lose signifi…
yangtse authored
276 unsigned long a1 = (laddr >> 24UL) & 0xFFUL;
277 unsigned long a2 = (laddr >> 16UL) & 0xFFUL;
278 unsigned long a3 = (laddr >> 8UL) & 0xFFUL;
279 unsigned long a4 = laddr & 0xFFUL;
280 sprintf(name, "%lu.%lu.%lu.%lu.in-addr.arpa", a4, a3, a2, a1);
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
281 }
282 else
283 {
e51156c @yangtse removed usage of 's6_addr', fixing compilation issue triggered with no
yangtse authored
284 unsigned char *bytes = (unsigned char *)&addr->addrV6;
daa44aa @dfandrich C89 compilers (like Minix' ACK) only need to handle 31 functions argu…
dfandrich authored
285 /* There are too many arguments to do this in one line using
f94afa4 Constified some arguments in local functions.
Gisle Vanem authored
286 * minimally C89-compliant compilers */
6606c99 @yangtse Fix three issues previous cleanup introduces.
yangtse authored
287 sprintf(name,
daa44aa @dfandrich C89 compilers (like Minix' ACK) only need to handle 31 functions argu…
dfandrich authored
288 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.",
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
289 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
290 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
291 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
daa44aa @dfandrich C89 compilers (like Minix' ACK) only need to handle 31 functions argu…
dfandrich authored
292 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4);
293 sprintf(name+strlen(name),
294 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
aae9630 @bagder Erik Kline cleaned up ares_gethostbyaddr.c:next_lookup() somewhat
authored
295 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
296 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
297 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
298 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);
299 }
300 }
Something went wrong with that request. Please try again.