]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* |
2 | * ++Copyright++ 1985, 1988, 1993 | |
3 | * - | |
4 | * Copyright (c) 1985, 1988, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | |
fa0bc87c | 6 | * |
28f540f4 RM |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
28f540f4 RM |
15 | * 4. Neither the name of the University nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
fa0bc87c | 18 | * |
28f540f4 RM |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | * - | |
31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
fa0bc87c | 32 | * |
28f540f4 RM |
33 | * Permission to use, copy, modify, and distribute this software for any |
34 | * purpose with or without fee is hereby granted, provided that the above | |
35 | * copyright notice and this permission notice appear in all copies, and that | |
36 | * the name of Digital Equipment Corporation not be used in advertising or | |
37 | * publicity pertaining to distribution of the document or software without | |
38 | * specific, written prior permission. | |
fa0bc87c | 39 | * |
28f540f4 RM |
40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
47 | * SOFTWARE. | |
48 | * - | |
49 | * --Copyright-- | |
50 | */ | |
51 | ||
e1e041c4 SP |
52 | /* XXX This file is not used by any of the resolver functions implemented by |
53 | glibc (i.e. get*info and gethostby*). It cannot be removed however because | |
54 | it exports symbols in the libresolv ABI. The file is not maintained any | |
55 | more, nor are these functions. */ | |
56 | ||
e7eceec0 FW |
57 | #include <shlib-compat.h> |
58 | #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_25) | |
59 | ||
60 | # include <sys/types.h> | |
61 | # include <sys/param.h> | |
62 | # include <sys/socket.h> | |
63 | # include <netinet/in.h> | |
64 | # include <arpa/inet.h> | |
65 | # include <arpa/nameser.h> | |
66 | ||
67 | # include <stdio.h> | |
68 | # include <netdb.h> | |
69 | # include <resolv.h> | |
70 | # include <ctype.h> | |
71 | # include <errno.h> | |
72 | # include <stdlib.h> | |
73 | # include <string.h> | |
74 | ||
75 | # define MAXALIASES 35 | |
76 | # define MAXADDRS 35 | |
28f540f4 | 77 | |
28f540f4 RM |
78 | static char *h_addr_ptrs[MAXADDRS + 1]; |
79 | ||
80 | static struct hostent host; | |
81 | static char *host_aliases[MAXALIASES]; | |
5f0e6fc7 | 82 | static char hostbuf[8*1024]; |
fa0bc87c | 83 | static u_char host_addr[16]; /* IPv4 or IPv6 */ |
28f540f4 RM |
84 | static FILE *hostf = NULL; |
85 | static int stayopen = 0; | |
86 | ||
79937577 UD |
87 | static void map_v4v6_address (const char *src, char *dst) __THROW; |
88 | static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW; | |
fa0bc87c | 89 | |
79937577 | 90 | extern void addrsort (char **, int) __THROW; |
28f540f4 | 91 | |
e7eceec0 FW |
92 | # if PACKETSZ > 65536 |
93 | # define MAXPACKET PACKETSZ | |
94 | # else | |
95 | # define MAXPACKET 65536 | |
96 | # endif | |
28f540f4 | 97 | |
1522c368 | 98 | /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */ |
e7eceec0 FW |
99 | # ifdef MAXHOSTNAMELEN |
100 | # undef MAXHOSTNAMELEN | |
101 | # endif | |
102 | # define MAXHOSTNAMELEN 256 | |
1522c368 | 103 | |
28f540f4 RM |
104 | typedef union { |
105 | HEADER hdr; | |
106 | u_char buf[MAXPACKET]; | |
107 | } querybuf; | |
108 | ||
109 | typedef union { | |
110 | int32_t al; | |
111 | char ac; | |
112 | } align; | |
113 | ||
e7eceec0 | 114 | # ifndef h_errno |
28f540f4 | 115 | extern int h_errno; |
e7eceec0 | 116 | # endif |
28f540f4 | 117 | |
e7eceec0 | 118 | # ifdef DEBUG |
28f540f4 | 119 | static void |
9d46370c | 120 | Dprintf (char *msg, int num) |
28f540f4 RM |
121 | { |
122 | if (_res.options & RES_DEBUG) { | |
123 | int save = errno; | |
124 | ||
125 | printf(msg, num); | |
a68b0d31 | 126 | __set_errno (save); |
28f540f4 RM |
127 | } |
128 | } | |
e7eceec0 FW |
129 | # else |
130 | # define Dprintf(msg, num) /*nada*/ | |
131 | # endif | |
28f540f4 | 132 | |
e7eceec0 | 133 | # define BOUNDED_INCR(x) \ |
66715f83 UD |
134 | do { \ |
135 | cp += x; \ | |
136 | if (cp > eom) { \ | |
137 | __set_h_errno (NO_RECOVERY); \ | |
138 | return (NULL); \ | |
139 | } \ | |
140 | } while (0) | |
141 | ||
e7eceec0 | 142 | # define BOUNDS_CHECK(ptr, count) \ |
66715f83 UD |
143 | do { \ |
144 | if ((ptr) + (count) > eom) { \ | |
145 | __set_h_errno (NO_RECOVERY); \ | |
146 | return (NULL); \ | |
147 | } \ | |
148 | } while (0) | |
149 | ||
150 | ||
28f540f4 | 151 | static struct hostent * |
db0a00d3 | 152 | getanswer (const querybuf *answer, int anslen, const char *qname, int qtype) |
28f540f4 | 153 | { |
2e09a79a JM |
154 | const HEADER *hp; |
155 | const u_char *cp; | |
156 | int n; | |
66715f83 | 157 | const u_char *eom, *erdata; |
28f540f4 RM |
158 | char *bp, **ap, **hap; |
159 | int type, class, buflen, ancount, qdcount; | |
160 | int haveanswer, had_error; | |
161 | int toobig = 0; | |
845dcb57 | 162 | char tbuf[MAXDNAME]; |
3cf595e5 | 163 | const char *tname; |
0cf12a6a | 164 | int (*name_ok) (const char *); |
28f540f4 | 165 | |
3cf595e5 | 166 | tname = qname; |
28f540f4 RM |
167 | host.h_name = NULL; |
168 | eom = answer->buf + anslen; | |
55707265 RM |
169 | switch (qtype) { |
170 | case T_A: | |
fa0bc87c | 171 | case T_AAAA: |
55707265 RM |
172 | name_ok = res_hnok; |
173 | break; | |
174 | case T_PTR: | |
fa0bc87c | 175 | name_ok = res_dnok; |
55707265 RM |
176 | break; |
177 | default: | |
fa0bc87c | 178 | return (NULL); /* XXX should be abort(); */ |
55707265 | 179 | } |
28f540f4 RM |
180 | /* |
181 | * find first satisfactory answer | |
182 | */ | |
183 | hp = &answer->hdr; | |
184 | ancount = ntohs(hp->ancount); | |
185 | qdcount = ntohs(hp->qdcount); | |
186 | bp = hostbuf; | |
187 | buflen = sizeof hostbuf; | |
66715f83 UD |
188 | cp = answer->buf; |
189 | BOUNDED_INCR(HFIXEDSZ); | |
28f540f4 | 190 | if (qdcount != 1) { |
a68b0d31 | 191 | __set_h_errno (NO_RECOVERY); |
28f540f4 RM |
192 | return (NULL); |
193 | } | |
55707265 RM |
194 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
195 | if ((n < 0) || !(*name_ok)(bp)) { | |
a68b0d31 | 196 | __set_h_errno (NO_RECOVERY); |
28f540f4 RM |
197 | return (NULL); |
198 | } | |
66715f83 | 199 | BOUNDED_INCR(n + QFIXEDSZ); |
fa0bc87c | 200 | if (qtype == T_A || qtype == T_AAAA) { |
28f540f4 RM |
201 | /* res_send() has already verified that the query name is the |
202 | * same as the one we sent; this just gets the expanded name | |
203 | * (i.e., with the succeeding search-domain tacked on). | |
204 | */ | |
205 | n = strlen(bp) + 1; /* for the \0 */ | |
76b87c03 UD |
206 | if (n >= MAXHOSTNAMELEN) { |
207 | __set_h_errno (NO_RECOVERY); | |
208 | return (NULL); | |
209 | } | |
28f540f4 RM |
210 | host.h_name = bp; |
211 | bp += n; | |
212 | buflen -= n; | |
213 | /* The qname can be abbreviated, but h_name is now absolute. */ | |
214 | qname = host.h_name; | |
215 | } | |
216 | ap = host_aliases; | |
217 | *ap = NULL; | |
218 | host.h_aliases = host_aliases; | |
219 | hap = h_addr_ptrs; | |
220 | *hap = NULL; | |
28f540f4 | 221 | host.h_addr_list = h_addr_ptrs; |
28f540f4 RM |
222 | haveanswer = 0; |
223 | had_error = 0; | |
224 | while (ancount-- > 0 && cp < eom && !had_error) { | |
225 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | |
55707265 | 226 | if ((n < 0) || !(*name_ok)(bp)) { |
28f540f4 RM |
227 | had_error++; |
228 | continue; | |
229 | } | |
230 | cp += n; /* name */ | |
66715f83 | 231 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
b43b13ac | 232 | type = ns_get16(cp); |
16b66062 | 233 | cp += INT16SZ; /* type */ |
b43b13ac | 234 | class = ns_get16(cp); |
16b66062 | 235 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
b43b13ac | 236 | n = ns_get16(cp); |
28f540f4 | 237 | cp += INT16SZ; /* len */ |
66715f83 UD |
238 | BOUNDS_CHECK(cp, n); |
239 | erdata = cp + n; | |
fa0bc87c | 240 | if (class != C_IN) { |
28f540f4 RM |
241 | /* XXX - debug? syslog? */ |
242 | cp += n; | |
243 | continue; /* XXX - had_error++ ? */ | |
244 | } | |
fa0bc87c | 245 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
28f540f4 RM |
246 | if (ap >= &host_aliases[MAXALIASES-1]) |
247 | continue; | |
248 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | |
55707265 | 249 | if ((n < 0) || !(*name_ok)(tbuf)) { |
28f540f4 RM |
250 | had_error++; |
251 | continue; | |
252 | } | |
253 | cp += n; | |
66715f83 UD |
254 | if (cp != erdata) { |
255 | __set_h_errno (NO_RECOVERY); | |
256 | return (NULL); | |
257 | } | |
28f540f4 RM |
258 | /* Store alias. */ |
259 | *ap++ = bp; | |
260 | n = strlen(bp) + 1; /* for the \0 */ | |
76b87c03 UD |
261 | if (n >= MAXHOSTNAMELEN) { |
262 | had_error++; | |
263 | continue; | |
264 | } | |
28f540f4 RM |
265 | bp += n; |
266 | buflen -= n; | |
267 | /* Get canonical name. */ | |
268 | n = strlen(tbuf) + 1; /* for the \0 */ | |
76b87c03 | 269 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
28f540f4 RM |
270 | had_error++; |
271 | continue; | |
272 | } | |
273 | strcpy(bp, tbuf); | |
274 | host.h_name = bp; | |
275 | bp += n; | |
276 | buflen -= n; | |
277 | continue; | |
278 | } | |
3cf595e5 RM |
279 | if (qtype == T_PTR && type == T_CNAME) { |
280 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | |
da2d1bc5 | 281 | if (n < 0 || !res_dnok(tbuf)) { |
3cf595e5 RM |
282 | had_error++; |
283 | continue; | |
284 | } | |
285 | cp += n; | |
66715f83 UD |
286 | if (cp != erdata) { |
287 | __set_h_errno (NO_RECOVERY); | |
288 | return (NULL); | |
289 | } | |
3cf595e5 RM |
290 | /* Get canonical name. */ |
291 | n = strlen(tbuf) + 1; /* for the \0 */ | |
76b87c03 | 292 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
3cf595e5 RM |
293 | had_error++; |
294 | continue; | |
295 | } | |
296 | strcpy(bp, tbuf); | |
297 | tname = bp; | |
298 | bp += n; | |
299 | buflen -= n; | |
300 | continue; | |
301 | } | |
28f540f4 | 302 | if (type != qtype) { |
3e3002ff SP |
303 | /* Log a low priority message if we get an unexpected |
304 | * record, but skip it if we are using DNSSEC since it | |
305 | * uses many different types in responses that do not | |
306 | * match QTYPE. | |
307 | */ | |
28f540f4 RM |
308 | cp += n; |
309 | continue; /* XXX - had_error++ ? */ | |
310 | } | |
311 | switch (type) { | |
312 | case T_PTR: | |
3cf595e5 | 313 | if (strcasecmp(tname, bp) != 0) { |
28f540f4 RM |
314 | cp += n; |
315 | continue; /* XXX - had_error++ ? */ | |
316 | } | |
317 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | |
55707265 | 318 | if ((n < 0) || !res_hnok(bp)) { |
28f540f4 RM |
319 | had_error++; |
320 | break; | |
321 | } | |
28f540f4 | 322 | cp += n; |
66715f83 UD |
323 | if (cp != erdata) { |
324 | __set_h_errno (NO_RECOVERY); | |
325 | return (NULL); | |
326 | } | |
28f540f4 RM |
327 | if (!haveanswer) |
328 | host.h_name = bp; | |
329 | else if (ap < &host_aliases[MAXALIASES-1]) | |
330 | *ap++ = bp; | |
331 | else | |
332 | n = -1; | |
333 | if (n != -1) { | |
334 | n = strlen(bp) + 1; /* for the \0 */ | |
76b87c03 UD |
335 | if (n >= MAXHOSTNAMELEN) { |
336 | had_error++; | |
337 | break; | |
338 | } | |
28f540f4 RM |
339 | bp += n; |
340 | buflen -= n; | |
341 | } | |
342 | break; | |
28f540f4 | 343 | case T_A: |
fa0bc87c | 344 | case T_AAAA: |
28f540f4 | 345 | if (strcasecmp(host.h_name, bp) != 0) { |
28f540f4 RM |
346 | cp += n; |
347 | continue; /* XXX - had_error++ ? */ | |
348 | } | |
1f64ac13 UD |
349 | if (n != host.h_length) { |
350 | cp += n; | |
351 | continue; | |
352 | } | |
353 | if (!haveanswer) { | |
2e09a79a | 354 | int nn; |
28f540f4 | 355 | |
28f540f4 RM |
356 | host.h_name = bp; |
357 | nn = strlen(bp) + 1; /* for the \0 */ | |
358 | bp += nn; | |
359 | buflen -= nn; | |
360 | } | |
361 | ||
31a13ab3 UD |
362 | /* XXX: when incrementing bp, we have to decrement |
363 | * buflen by the same amount --okir */ | |
364 | buflen -= sizeof(align) - ((u_long)bp % sizeof(align)); | |
365 | ||
28f540f4 RM |
366 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); |
367 | ||
368 | if (bp + n >= &hostbuf[sizeof hostbuf]) { | |
020a9a23 | 369 | Dprintf("size (%d) too big\n", n); |
28f540f4 RM |
370 | had_error++; |
371 | continue; | |
372 | } | |
373 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { | |
dfd2257a | 374 | if (!toobig++) { |
020a9a23 | 375 | Dprintf("Too many addresses (%d)\n", |
28f540f4 | 376 | MAXADDRS); |
dfd2257a | 377 | } |
28f540f4 RM |
378 | cp += n; |
379 | continue; | |
380 | } | |
9596d0dd | 381 | memmove(*hap++ = bp, cp, n); |
28f540f4 | 382 | bp += n; |
df21c858 | 383 | buflen -= n; |
28f540f4 | 384 | cp += n; |
66715f83 UD |
385 | if (cp != erdata) { |
386 | __set_h_errno (NO_RECOVERY); | |
387 | return (NULL); | |
388 | } | |
28f540f4 RM |
389 | break; |
390 | default: | |
fa0bc87c RM |
391 | abort(); |
392 | } | |
28f540f4 RM |
393 | if (!had_error) |
394 | haveanswer++; | |
fa0bc87c | 395 | } |
28f540f4 RM |
396 | if (haveanswer) { |
397 | *ap = NULL; | |
398 | *hap = NULL; | |
28f540f4 RM |
399 | /* |
400 | * Note: we sort even if host can take only one address | |
401 | * in its return structures - should give it the "best" | |
402 | * address in that case, not some random one | |
403 | */ | |
fa0bc87c | 404 | if (_res.nsort && haveanswer > 1 && qtype == T_A) |
28f540f4 | 405 | addrsort(h_addr_ptrs, haveanswer); |
28f540f4 RM |
406 | if (!host.h_name) { |
407 | n = strlen(qname) + 1; /* for the \0 */ | |
76b87c03 UD |
408 | if (n > buflen || n >= MAXHOSTNAMELEN) |
409 | goto no_recovery; | |
28f540f4 RM |
410 | strcpy(bp, qname); |
411 | host.h_name = bp; | |
fa0bc87c RM |
412 | bp += n; |
413 | buflen -= n; | |
28f540f4 | 414 | } |
fa0bc87c RM |
415 | if (_res.options & RES_USE_INET6) |
416 | map_v4v6_hostent(&host, &bp, &buflen); | |
a68b0d31 | 417 | __set_h_errno (NETDB_SUCCESS); |
28f540f4 | 418 | return (&host); |
28f540f4 | 419 | } |
76b87c03 UD |
420 | no_recovery: |
421 | __set_h_errno (NO_RECOVERY); | |
fa0bc87c | 422 | return (NULL); |
28f540f4 RM |
423 | } |
424 | ||
e7eceec0 FW |
425 | extern struct hostent *res_gethostbyname2(const char *name, int af); |
426 | libresolv_hidden_proto (res_gethostbyname2) | |
6f9d8e68 | 427 | |
28f540f4 | 428 | struct hostent * |
e7eceec0 | 429 | res_gethostbyname (const char *name) |
3cf595e5 RM |
430 | { |
431 | struct hostent *hp; | |
432 | ||
87bb6b6c | 433 | if (__res_maybe_init (&_res, 0) == -1) { |
a68b0d31 | 434 | __set_h_errno (NETDB_INTERNAL); |
845dcb57 | 435 | return (NULL); |
87bb6b6c | 436 | } |
fa0bc87c | 437 | if (_res.options & RES_USE_INET6) { |
e7eceec0 | 438 | hp = res_gethostbyname2(name, AF_INET6); |
3cf595e5 RM |
439 | if (hp) |
440 | return (hp); | |
441 | } | |
e7eceec0 | 442 | return (res_gethostbyname2(name, AF_INET)); |
3cf595e5 | 443 | } |
e7eceec0 | 444 | compat_symbol (libresolv, res_gethostbyname, res_gethostbyname, GLIBC_2_0); |
3cf595e5 RM |
445 | |
446 | struct hostent * | |
e7eceec0 | 447 | res_gethostbyname2 (const char *name, int af) |
28f540f4 | 448 | { |
16b66062 AJ |
449 | union |
450 | { | |
451 | querybuf *buf; | |
452 | u_char *ptr; | |
453 | } buf; | |
454 | querybuf *origbuf; | |
2e09a79a | 455 | const char *cp; |
fa0bc87c | 456 | char *bp; |
0420d888 | 457 | int n, size, type, len; |
6166815d | 458 | struct hostent *ret; |
28f540f4 | 459 | |
87bb6b6c | 460 | if (__res_maybe_init (&_res, 0) == -1) { |
a68b0d31 | 461 | __set_h_errno (NETDB_INTERNAL); |
3d61b63c RM |
462 | return (NULL); |
463 | } | |
464 | ||
fa0bc87c RM |
465 | switch (af) { |
466 | case AF_INET: | |
467 | size = INADDRSZ; | |
468 | type = T_A; | |
469 | break; | |
470 | case AF_INET6: | |
471 | size = IN6ADDRSZ; | |
472 | type = T_AAAA; | |
473 | break; | |
474 | default: | |
a68b0d31 | 475 | __set_h_errno (NETDB_INTERNAL); |
ba1ffaa1 | 476 | __set_errno (EAFNOSUPPORT); |
fa0bc87c RM |
477 | return (NULL); |
478 | } | |
479 | ||
480 | host.h_addrtype = af; | |
481 | host.h_length = size; | |
482 | ||
28f540f4 RM |
483 | /* |
484 | * if there aren't any dots, it could be a user-level alias. | |
485 | * this is also done in res_query() since we are not the only | |
486 | * function that looks up host names. | |
487 | */ | |
488 | if (!strchr(name, '.') && (cp = __hostalias(name))) | |
489 | name = cp; | |
490 | ||
491 | /* | |
492 | * disallow names consisting only of digits/dots, unless | |
493 | * they end in a dot. | |
494 | */ | |
495 | if (isdigit(name[0])) | |
496 | for (cp = name;; ++cp) { | |
497 | if (!*cp) { | |
498 | if (*--cp == '.') | |
499 | break; | |
500 | /* | |
501 | * All-numeric, no dot at the end. | |
502 | * Fake up a hostent as if we'd actually | |
503 | * done a lookup. | |
504 | */ | |
613a76ff | 505 | if (inet_pton(af, name, host_addr) <= 0) { |
a68b0d31 | 506 | __set_h_errno (HOST_NOT_FOUND); |
28f540f4 RM |
507 | return (NULL); |
508 | } | |
3d61b63c RM |
509 | strncpy(hostbuf, name, MAXDNAME); |
510 | hostbuf[MAXDNAME] = '\0'; | |
fa0bc87c RM |
511 | bp = hostbuf + MAXDNAME; |
512 | len = sizeof hostbuf - MAXDNAME; | |
3d61b63c | 513 | host.h_name = hostbuf; |
28f540f4 RM |
514 | host.h_aliases = host_aliases; |
515 | host_aliases[0] = NULL; | |
613a76ff | 516 | h_addr_ptrs[0] = (char *)host_addr; |
28f540f4 | 517 | h_addr_ptrs[1] = NULL; |
28f540f4 | 518 | host.h_addr_list = h_addr_ptrs; |
fa0bc87c RM |
519 | if (_res.options & RES_USE_INET6) |
520 | map_v4v6_hostent(&host, &bp, &len); | |
a68b0d31 | 521 | __set_h_errno (NETDB_SUCCESS); |
28f540f4 RM |
522 | return (&host); |
523 | } | |
fa0bc87c | 524 | if (!isdigit(*cp) && *cp != '.') |
28f540f4 | 525 | break; |
312be3f9 | 526 | } |
76b87c03 UD |
527 | if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || |
528 | name[0] == ':') | |
845dcb57 UD |
529 | for (cp = name;; ++cp) { |
530 | if (!*cp) { | |
531 | if (*--cp == '.') | |
532 | break; | |
533 | /* | |
534 | * All-IPv6-legal, no dot at the end. | |
535 | * Fake up a hostent as if we'd actually | |
536 | * done a lookup. | |
537 | */ | |
538 | if (inet_pton(af, name, host_addr) <= 0) { | |
a68b0d31 | 539 | __set_h_errno (HOST_NOT_FOUND); |
845dcb57 UD |
540 | return (NULL); |
541 | } | |
542 | strncpy(hostbuf, name, MAXDNAME); | |
543 | hostbuf[MAXDNAME] = '\0'; | |
544 | bp = hostbuf + MAXDNAME; | |
545 | len = sizeof hostbuf - MAXDNAME; | |
546 | host.h_name = hostbuf; | |
547 | host.h_aliases = host_aliases; | |
548 | host_aliases[0] = NULL; | |
549 | h_addr_ptrs[0] = (char *)host_addr; | |
550 | h_addr_ptrs[1] = NULL; | |
551 | host.h_addr_list = h_addr_ptrs; | |
a68b0d31 | 552 | __set_h_errno (NETDB_SUCCESS); |
845dcb57 UD |
553 | return (&host); |
554 | } | |
555 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') | |
556 | break; | |
28f540f4 RM |
557 | } |
558 | ||
16b66062 | 559 | buf.buf = origbuf = (querybuf *) alloca (1024); |
6166815d | 560 | |
16b66062 | 561 | if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024, |
ab09bf61 | 562 | &buf.ptr, NULL, NULL, NULL, NULL)) < 0) { |
16b66062 AJ |
563 | if (buf.buf != origbuf) |
564 | free (buf.buf); | |
020a9a23 | 565 | Dprintf("res_nsearch failed (%d)\n", n); |
5f0e6fc7 RM |
566 | if (errno == ECONNREFUSED) |
567 | return (_gethtbyname2(name, af)); | |
568 | return (NULL); | |
28f540f4 | 569 | } |
16b66062 AJ |
570 | ret = getanswer(buf.buf, n, name, type); |
571 | if (buf.buf != origbuf) | |
572 | free (buf.buf); | |
6166815d | 573 | return ret; |
28f540f4 | 574 | } |
e7eceec0 FW |
575 | libresolv_hidden_def (res_gethostbyname2) |
576 | compat_symbol (libresolv, res_gethostbyname2, res_gethostbyname2, GLIBC_2_0); | |
28f540f4 RM |
577 | |
578 | struct hostent * | |
e7eceec0 | 579 | res_gethostbyaddr (const void *addr, socklen_t len, int af) |
28f540f4 | 580 | { |
fa0bc87c RM |
581 | const u_char *uaddr = (const u_char *)addr; |
582 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; | |
583 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; | |
0420d888 | 584 | int n; |
160bb409 | 585 | socklen_t size; |
16b66062 AJ |
586 | union |
587 | { | |
588 | querybuf *buf; | |
589 | u_char *ptr; | |
590 | } buf; | |
591 | querybuf *orig_buf; | |
2e09a79a | 592 | struct hostent *hp; |
3db04c6f | 593 | char qbuf[MAXDNAME+1], *qp = NULL; |
fa0bc87c | 594 | |
87bb6b6c | 595 | if (__res_maybe_init (&_res, 0) == -1) { |
a68b0d31 | 596 | __set_h_errno (NETDB_INTERNAL); |
3d61b63c RM |
597 | return (NULL); |
598 | } | |
fa0bc87c | 599 | if (af == AF_INET6 && len == IN6ADDRSZ && |
1c6e6f23 RM |
600 | (!memcmp(uaddr, mapped, sizeof mapped) || |
601 | !memcmp(uaddr, tunnelled, sizeof tunnelled))) { | |
fa0bc87c RM |
602 | /* Unmap. */ |
603 | addr += sizeof mapped; | |
604 | uaddr += sizeof mapped; | |
605 | af = AF_INET; | |
606 | len = INADDRSZ; | |
607 | } | |
608 | switch (af) { | |
609 | case AF_INET: | |
610 | size = INADDRSZ; | |
611 | break; | |
612 | case AF_INET6: | |
613 | size = IN6ADDRSZ; | |
614 | break; | |
615 | default: | |
a68b0d31 UD |
616 | __set_errno (EAFNOSUPPORT); |
617 | __set_h_errno (NETDB_INTERNAL); | |
28f540f4 RM |
618 | return (NULL); |
619 | } | |
fa0bc87c | 620 | if (size != len) { |
a68b0d31 UD |
621 | __set_errno (EINVAL); |
622 | __set_h_errno (NETDB_INTERNAL); | |
28f540f4 RM |
623 | return (NULL); |
624 | } | |
5f0e6fc7 RM |
625 | switch (af) { |
626 | case AF_INET: | |
627 | (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", | |
628 | (uaddr[3] & 0xff), | |
629 | (uaddr[2] & 0xff), | |
630 | (uaddr[1] & 0xff), | |
631 | (uaddr[0] & 0xff)); | |
632 | break; | |
633 | case AF_INET6: | |
634 | qp = qbuf; | |
635 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { | |
e01eef67 FW |
636 | qp += sprintf(qp, "%x.%x.", |
637 | uaddr[n] & 0xf, | |
638 | (uaddr[n] >> 4) & 0xf); | |
fa0bc87c | 639 | } |
ca6c7389 | 640 | strcpy(qp, "ip6.arpa"); |
5f0e6fc7 RM |
641 | break; |
642 | default: | |
643 | abort(); | |
644 | } | |
6166815d | 645 | |
16b66062 | 646 | buf.buf = orig_buf = (querybuf *) alloca (1024); |
6166815d | 647 | |
16b66062 | 648 | n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024, |
ab09bf61 | 649 | &buf.ptr, NULL, NULL, NULL, NULL); |
5f0e6fc7 | 650 | if (n < 0) { |
16b66062 AJ |
651 | if (buf.buf != orig_buf) |
652 | free (buf.buf); | |
020a9a23 | 653 | Dprintf("res_nquery failed (%d)\n", n); |
5f0e6fc7 RM |
654 | if (errno == ECONNREFUSED) |
655 | return (_gethtbyaddr(addr, len, af)); | |
656 | return (NULL); | |
28f540f4 | 657 | } |
16b66062 AJ |
658 | hp = getanswer(buf.buf, n, qbuf, T_PTR); |
659 | if (buf.buf != orig_buf) | |
660 | free (buf.buf); | |
6166815d | 661 | if (!hp) |
5f0e6fc7 | 662 | return (NULL); /* h_errno was set by getanswer() */ |
5f0e6fc7 RM |
663 | hp->h_addrtype = af; |
664 | hp->h_length = len; | |
9596d0dd | 665 | memmove(host_addr, addr, len); |
5f0e6fc7 RM |
666 | h_addr_ptrs[0] = (char *)host_addr; |
667 | h_addr_ptrs[1] = NULL; | |
668 | if (af == AF_INET && (_res.options & RES_USE_INET6)) { | |
669 | map_v4v6_address((char*)host_addr, (char*)host_addr); | |
670 | hp->h_addrtype = AF_INET6; | |
671 | hp->h_length = IN6ADDRSZ; | |
672 | } | |
a68b0d31 | 673 | __set_h_errno (NETDB_SUCCESS); |
5f0e6fc7 | 674 | return (hp); |
28f540f4 | 675 | } |
e7eceec0 | 676 | compat_symbol (libresolv, res_gethostbyaddr, res_gethostbyaddr, GLIBC_2_0); |
28f540f4 RM |
677 | |
678 | void | |
9d46370c | 679 | _sethtent (int f) |
28f540f4 RM |
680 | { |
681 | if (!hostf) | |
312be3f9 | 682 | hostf = fopen(_PATH_HOSTS, "rce" ); |
28f540f4 RM |
683 | else |
684 | rewind(hostf); | |
685 | stayopen = f; | |
686 | } | |
6f9d8e68 | 687 | libresolv_hidden_def (_sethtent) |
e7eceec0 | 688 | compat_symbol (libresolv, _sethtent, _sethtent, GLIBC_2_0); |
28f540f4 | 689 | |
e7eceec0 | 690 | static void |
60d2f8f3 | 691 | _endhtent (void) |
28f540f4 RM |
692 | { |
693 | if (hostf && !stayopen) { | |
694 | (void) fclose(hostf); | |
695 | hostf = NULL; | |
696 | } | |
697 | } | |
698 | ||
699 | struct hostent * | |
60d2f8f3 | 700 | _gethtent (void) |
28f540f4 RM |
701 | { |
702 | char *p; | |
2e09a79a | 703 | char *cp, **q; |
fa0bc87c | 704 | int af, len; |
28f540f4 | 705 | |
312be3f9 | 706 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) { |
a68b0d31 | 707 | __set_h_errno (NETDB_INTERNAL); |
28f540f4 RM |
708 | return (NULL); |
709 | } | |
fa0bc87c | 710 | again: |
28f540f4 | 711 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { |
a68b0d31 | 712 | __set_h_errno (HOST_NOT_FOUND); |
28f540f4 RM |
713 | return (NULL); |
714 | } | |
715 | if (*p == '#') | |
716 | goto again; | |
717 | if (!(cp = strpbrk(p, "#\n"))) | |
718 | goto again; | |
719 | *cp = '\0'; | |
720 | if (!(cp = strpbrk(p, " \t"))) | |
721 | goto again; | |
722 | *cp++ = '\0'; | |
76b87c03 | 723 | if (inet_pton(AF_INET6, p, host_addr) > 0) { |
fa0bc87c RM |
724 | af = AF_INET6; |
725 | len = IN6ADDRSZ; | |
613a76ff | 726 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { |
fa0bc87c | 727 | if (_res.options & RES_USE_INET6) { |
613a76ff | 728 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
fa0bc87c RM |
729 | af = AF_INET6; |
730 | len = IN6ADDRSZ; | |
731 | } else { | |
732 | af = AF_INET; | |
733 | len = INADDRSZ; | |
734 | } | |
735 | } else { | |
28f540f4 | 736 | goto again; |
fa0bc87c | 737 | } |
613a76ff | 738 | h_addr_ptrs[0] = (char *)host_addr; |
28f540f4 | 739 | h_addr_ptrs[1] = NULL; |
28f540f4 | 740 | host.h_addr_list = h_addr_ptrs; |
fa0bc87c RM |
741 | host.h_length = len; |
742 | host.h_addrtype = af; | |
28f540f4 RM |
743 | while (*cp == ' ' || *cp == '\t') |
744 | cp++; | |
745 | host.h_name = cp; | |
746 | q = host.h_aliases = host_aliases; | |
6796bc80 | 747 | if ((cp = strpbrk(cp, " \t"))) |
28f540f4 RM |
748 | *cp++ = '\0'; |
749 | while (cp && *cp) { | |
750 | if (*cp == ' ' || *cp == '\t') { | |
751 | cp++; | |
752 | continue; | |
753 | } | |
754 | if (q < &host_aliases[MAXALIASES - 1]) | |
755 | *q++ = cp; | |
6796bc80 | 756 | if ((cp = strpbrk(cp, " \t"))) |
28f540f4 RM |
757 | *cp++ = '\0'; |
758 | } | |
759 | *q = NULL; | |
a68b0d31 | 760 | __set_h_errno (NETDB_SUCCESS); |
28f540f4 RM |
761 | return (&host); |
762 | } | |
6f9d8e68 | 763 | libresolv_hidden_def (_gethtent) |
e7eceec0 | 764 | compat_symbol (libresolv, _gethtent, _gethtent, GLIBC_2_0); |
28f540f4 RM |
765 | |
766 | struct hostent * | |
9d46370c | 767 | _gethtbyname (const char *name) |
fa0bc87c | 768 | { |
fa0bc87c RM |
769 | struct hostent *hp; |
770 | ||
771 | if (_res.options & RES_USE_INET6) { | |
772 | hp = _gethtbyname2(name, AF_INET6); | |
773 | if (hp) | |
774 | return (hp); | |
775 | } | |
776 | return (_gethtbyname2(name, AF_INET)); | |
777 | } | |
e7eceec0 | 778 | compat_symbol (libresolv, _gethtbyname, _gethtbyname, GLIBC_2_0); |
fa0bc87c RM |
779 | |
780 | struct hostent * | |
9d46370c | 781 | _gethtbyname2 (const char *name, int af) |
28f540f4 | 782 | { |
2e09a79a JM |
783 | struct hostent *p; |
784 | char **cp; | |
fa0bc87c | 785 | |
28f540f4 | 786 | _sethtent(0); |
6796bc80 | 787 | while ((p = _gethtent())) { |
5f0e6fc7 RM |
788 | if (p->h_addrtype != af) |
789 | continue; | |
790 | if (strcasecmp(p->h_name, name) == 0) | |
791 | break; | |
792 | for (cp = p->h_aliases; *cp != 0; cp++) | |
793 | if (strcasecmp(*cp, name) == 0) | |
794 | goto found; | |
28f540f4 | 795 | } |
fa0bc87c | 796 | found: |
28f540f4 RM |
797 | _endhtent(); |
798 | return (p); | |
799 | } | |
6f9d8e68 | 800 | libresolv_hidden_def (_gethtbyname2) |
e7eceec0 | 801 | compat_symbol (libresolv, _gethtbyname2, _gethtbyname2, GLIBC_2_0); |
28f540f4 RM |
802 | |
803 | struct hostent * | |
9d46370c | 804 | _gethtbyaddr (const char *addr, size_t len, int af) |
28f540f4 | 805 | { |
2e09a79a | 806 | struct hostent *p; |
28f540f4 RM |
807 | |
808 | _sethtent(0); | |
6796bc80 | 809 | while ((p = _gethtent())) |
1c6e6f23 | 810 | if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len)) |
28f540f4 RM |
811 | break; |
812 | _endhtent(); | |
813 | return (p); | |
814 | } | |
6f9d8e68 | 815 | libresolv_hidden_def (_gethtbyaddr) |
e7eceec0 | 816 | compat_symbol (libresolv, _gethtbyaddr, _gethtbyaddr, GLIBC_2_0); |
28f540f4 | 817 | |
fa0bc87c | 818 | static void |
9d46370c | 819 | map_v4v6_address (const char *src, char *dst) |
fa0bc87c RM |
820 | { |
821 | u_char *p = (u_char *)dst; | |
822 | char tmp[INADDRSZ]; | |
823 | int i; | |
824 | ||
825 | /* Stash a temporary copy so our caller can update in place. */ | |
9596d0dd | 826 | memcpy(tmp, src, INADDRSZ); |
fa0bc87c RM |
827 | /* Mark this ipv6 addr as a mapped ipv4. */ |
828 | for (i = 0; i < 10; i++) | |
829 | *p++ = 0x00; | |
830 | *p++ = 0xff; | |
831 | *p++ = 0xff; | |
832 | /* Retrieve the saved copy and we're done. */ | |
9596d0dd | 833 | memcpy((void*)p, tmp, INADDRSZ); |
fa0bc87c RM |
834 | } |
835 | ||
836 | static void | |
9d46370c | 837 | map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp) |
fa0bc87c RM |
838 | { |
839 | char **ap; | |
840 | ||
841 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) | |
842 | return; | |
843 | hp->h_addrtype = AF_INET6; | |
844 | hp->h_length = IN6ADDRSZ; | |
845 | for (ap = hp->h_addr_list; *ap; ap++) { | |
846 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); | |
847 | ||
848 | if (*lenp < (i + IN6ADDRSZ)) { | |
849 | /* Out of memory. Truncate address list here. XXX */ | |
850 | *ap = NULL; | |
851 | return; | |
852 | } | |
853 | *bpp += i; | |
854 | *lenp -= i; | |
855 | map_v4v6_address(*ap, *bpp); | |
856 | *ap = *bpp; | |
857 | *bpp += IN6ADDRSZ; | |
858 | *lenp -= IN6ADDRSZ; | |
859 | } | |
860 | } | |
861 | ||
03e4219e | 862 | extern void |
9d46370c | 863 | addrsort (char **ap, int num) |
28f540f4 RM |
864 | { |
865 | int i, j; | |
866 | char **p; | |
867 | short aval[MAXADDRS]; | |
868 | int needsort = 0; | |
869 | ||
870 | p = ap; | |
871 | for (i = 0; i < num; i++, p++) { | |
872 | for (j = 0 ; (unsigned)j < _res.nsort; j++) | |
fa0bc87c | 873 | if (_res.sort_list[j].addr.s_addr == |
28f540f4 RM |
874 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) |
875 | break; | |
876 | aval[i] = j; | |
877 | if (needsort == 0 && i > 0 && j < aval[i-1]) | |
878 | needsort = i; | |
879 | } | |
880 | if (!needsort) | |
881 | return; | |
882 | ||
883 | while (needsort < num) { | |
884 | for (j = needsort - 1; j >= 0; j--) { | |
885 | if (aval[j] > aval[j+1]) { | |
886 | char *hp; | |
887 | ||
888 | i = aval[j]; | |
889 | aval[j] = aval[j+1]; | |
890 | aval[j+1] = i; | |
891 | ||
892 | hp = ap[j]; | |
893 | ap[j] = ap[j+1]; | |
894 | ap[j+1] = hp; | |
895 | ||
896 | } else | |
897 | break; | |
898 | } | |
899 | needsort++; | |
900 | } | |
901 | } | |
e7eceec0 FW |
902 | |
903 | #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */ |