Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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