1 | /* |
2 | * ++Copyright++ 1985, 1988, 1993 |
3 | * - |
4 | * Copyright (c) 1985, 1988, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
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. |
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. |
18 | * |
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. |
32 | * |
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. |
39 | * |
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 | |
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 | |
57 | #if defined(LIBC_SCCS) && !defined(lint) |
58 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93" ; |
59 | #endif /* LIBC_SCCS and not lint */ |
60 | |
61 | #include <sys/types.h> |
62 | #include <sys/param.h> |
63 | #include <sys/socket.h> |
64 | #include <netinet/in.h> |
65 | #include <arpa/inet.h> |
66 | #include <arpa/nameser.h> |
67 | |
68 | #include <stdio.h> |
69 | #include <netdb.h> |
70 | #include <resolv.h> |
71 | #include <ctype.h> |
72 | #include <errno.h> |
73 | |
74 | #define RESOLVSORT |
75 | |
76 | #ifndef LOG_AUTH |
77 | # define LOG_AUTH 0 |
78 | #endif |
79 | |
80 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ |
81 | |
82 | #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6) |
83 | # include <stdlib.h> |
84 | # include <string.h> |
85 | #else |
86 | # include "../conf/portability.h" |
87 | #endif |
88 | |
89 | #if defined(USE_OPTIONS_H) |
90 | # include <../conf/options.h> |
91 | #endif |
92 | |
93 | #ifdef SPRINTF_CHAR |
94 | # define SPRINTF(x) strlen(sprintf/**/x) |
95 | #else |
96 | # define SPRINTF(x) ((size_t)sprintf x) |
97 | #endif |
98 | |
99 | #define MAXALIASES 35 |
100 | #define MAXADDRS 35 |
101 | |
102 | static char *h_addr_ptrs[MAXADDRS + 1]; |
103 | |
104 | static struct hostent host; |
105 | static char *host_aliases[MAXALIASES]; |
106 | static char hostbuf[8*1024]; |
107 | static u_char host_addr[16]; /* IPv4 or IPv6 */ |
108 | static FILE *hostf = NULL; |
109 | static int stayopen = 0; |
110 | |
111 | static void map_v4v6_address (const char *src, char *dst) __THROW; |
112 | static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW; |
113 | |
114 | #ifdef RESOLVSORT |
115 | extern void addrsort (char **, int) __THROW; |
116 | #endif |
117 | |
118 | #if PACKETSZ > 65536 |
119 | #define MAXPACKET PACKETSZ |
120 | #else |
121 | #define MAXPACKET 65536 |
122 | #endif |
123 | |
124 | /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */ |
125 | #ifdef MAXHOSTNAMELEN |
126 | # undef MAXHOSTNAMELEN |
127 | #endif |
128 | #define MAXHOSTNAMELEN 256 |
129 | |
130 | typedef union { |
131 | HEADER hdr; |
132 | u_char buf[MAXPACKET]; |
133 | } querybuf; |
134 | |
135 | typedef union { |
136 | int32_t al; |
137 | char ac; |
138 | } align; |
139 | |
140 | #ifndef h_errno |
141 | extern int h_errno; |
142 | #endif |
143 | |
144 | #ifdef DEBUG |
145 | static void |
146 | Dprintf (char *msg, int num) |
147 | { |
148 | if (_res.options & RES_DEBUG) { |
149 | int save = errno; |
150 | |
151 | printf(msg, num); |
152 | __set_errno (save); |
153 | } |
154 | } |
155 | #else |
156 | # define Dprintf(msg, num) /*nada*/ |
157 | #endif |
158 | |
159 | #define BOUNDED_INCR(x) \ |
160 | do { \ |
161 | cp += x; \ |
162 | if (cp > eom) { \ |
163 | __set_h_errno (NO_RECOVERY); \ |
164 | return (NULL); \ |
165 | } \ |
166 | } while (0) |
167 | |
168 | #define BOUNDS_CHECK(ptr, count) \ |
169 | do { \ |
170 | if ((ptr) + (count) > eom) { \ |
171 | __set_h_errno (NO_RECOVERY); \ |
172 | return (NULL); \ |
173 | } \ |
174 | } while (0) |
175 | |
176 | |
177 | static struct hostent * |
178 | getanswer (const querybuf *answer, int anslen, const char *qname, int qtype) |
179 | { |
180 | const HEADER *hp; |
181 | const u_char *cp; |
182 | int n; |
183 | const u_char *eom, *erdata; |
184 | char *bp, **ap, **hap; |
185 | int type, class, buflen, ancount, qdcount; |
186 | int haveanswer, had_error; |
187 | int toobig = 0; |
188 | char tbuf[MAXDNAME]; |
189 | const char *tname; |
190 | int (*name_ok) (const char *); |
191 | |
192 | tname = qname; |
193 | host.h_name = NULL; |
194 | eom = answer->buf + anslen; |
195 | switch (qtype) { |
196 | case T_A: |
197 | case T_AAAA: |
198 | name_ok = res_hnok; |
199 | break; |
200 | case T_PTR: |
201 | name_ok = res_dnok; |
202 | break; |
203 | default: |
204 | return (NULL); /* XXX should be abort(); */ |
205 | } |
206 | /* |
207 | * find first satisfactory answer |
208 | */ |
209 | hp = &answer->hdr; |
210 | ancount = ntohs(hp->ancount); |
211 | qdcount = ntohs(hp->qdcount); |
212 | bp = hostbuf; |
213 | buflen = sizeof hostbuf; |
214 | cp = answer->buf; |
215 | BOUNDED_INCR(HFIXEDSZ); |
216 | if (qdcount != 1) { |
217 | __set_h_errno (NO_RECOVERY); |
218 | return (NULL); |
219 | } |
220 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
221 | if ((n < 0) || !(*name_ok)(bp)) { |
222 | __set_h_errno (NO_RECOVERY); |
223 | return (NULL); |
224 | } |
225 | BOUNDED_INCR(n + QFIXEDSZ); |
226 | if (qtype == T_A || qtype == T_AAAA) { |
227 | /* res_send() has already verified that the query name is the |
228 | * same as the one we sent; this just gets the expanded name |
229 | * (i.e., with the succeeding search-domain tacked on). |
230 | */ |
231 | n = strlen(bp) + 1; /* for the \0 */ |
232 | if (n >= MAXHOSTNAMELEN) { |
233 | __set_h_errno (NO_RECOVERY); |
234 | return (NULL); |
235 | } |
236 | host.h_name = bp; |
237 | bp += n; |
238 | buflen -= n; |
239 | /* The qname can be abbreviated, but h_name is now absolute. */ |
240 | qname = host.h_name; |
241 | } |
242 | ap = host_aliases; |
243 | *ap = NULL; |
244 | host.h_aliases = host_aliases; |
245 | hap = h_addr_ptrs; |
246 | *hap = NULL; |
247 | host.h_addr_list = h_addr_ptrs; |
248 | haveanswer = 0; |
249 | had_error = 0; |
250 | while (ancount-- > 0 && cp < eom && !had_error) { |
251 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
252 | if ((n < 0) || !(*name_ok)(bp)) { |
253 | had_error++; |
254 | continue; |
255 | } |
256 | cp += n; /* name */ |
257 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
258 | type = ns_get16(cp); |
259 | cp += INT16SZ; /* type */ |
260 | class = ns_get16(cp); |
261 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
262 | n = ns_get16(cp); |
263 | cp += INT16SZ; /* len */ |
264 | BOUNDS_CHECK(cp, n); |
265 | erdata = cp + n; |
266 | if (class != C_IN) { |
267 | /* XXX - debug? syslog? */ |
268 | cp += n; |
269 | continue; /* XXX - had_error++ ? */ |
270 | } |
271 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
272 | if (ap >= &host_aliases[MAXALIASES-1]) |
273 | continue; |
274 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
275 | if ((n < 0) || !(*name_ok)(tbuf)) { |
276 | had_error++; |
277 | continue; |
278 | } |
279 | cp += n; |
280 | if (cp != erdata) { |
281 | __set_h_errno (NO_RECOVERY); |
282 | return (NULL); |
283 | } |
284 | /* Store alias. */ |
285 | *ap++ = bp; |
286 | n = strlen(bp) + 1; /* for the \0 */ |
287 | if (n >= MAXHOSTNAMELEN) { |
288 | had_error++; |
289 | continue; |
290 | } |
291 | bp += n; |
292 | buflen -= n; |
293 | /* Get canonical name. */ |
294 | n = strlen(tbuf) + 1; /* for the \0 */ |
295 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
296 | had_error++; |
297 | continue; |
298 | } |
299 | strcpy(bp, tbuf); |
300 | host.h_name = bp; |
301 | bp += n; |
302 | buflen -= n; |
303 | continue; |
304 | } |
305 | if (qtype == T_PTR && type == T_CNAME) { |
306 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
307 | if (n < 0 || !res_dnok(tbuf)) { |
308 | had_error++; |
309 | continue; |
310 | } |
311 | cp += n; |
312 | if (cp != erdata) { |
313 | __set_h_errno (NO_RECOVERY); |
314 | return (NULL); |
315 | } |
316 | /* Get canonical name. */ |
317 | n = strlen(tbuf) + 1; /* for the \0 */ |
318 | if (n > buflen || n >= MAXHOSTNAMELEN) { |
319 | had_error++; |
320 | continue; |
321 | } |
322 | strcpy(bp, tbuf); |
323 | tname = bp; |
324 | bp += n; |
325 | buflen -= n; |
326 | continue; |
327 | } |
328 | if (type != qtype) { |
329 | /* Log a low priority message if we get an unexpected |
330 | * record, but skip it if we are using DNSSEC since it |
331 | * uses many different types in responses that do not |
332 | * match QTYPE. |
333 | */ |
334 | cp += n; |
335 | continue; /* XXX - had_error++ ? */ |
336 | } |
337 | switch (type) { |
338 | case T_PTR: |
339 | if (strcasecmp(tname, bp) != 0) { |
340 | cp += n; |
341 | continue; /* XXX - had_error++ ? */ |
342 | } |
343 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
344 | if ((n < 0) || !res_hnok(bp)) { |
345 | had_error++; |
346 | break; |
347 | } |
348 | #if MULTI_PTRS_ARE_ALIASES |
349 | cp += n; |
350 | if (cp != erdata) { |
351 | __set_h_errno (NO_RECOVERY); |
352 | return (NULL); |
353 | } |
354 | if (!haveanswer) |
355 | host.h_name = bp; |
356 | else if (ap < &host_aliases[MAXALIASES-1]) |
357 | *ap++ = bp; |
358 | else |
359 | n = -1; |
360 | if (n != -1) { |
361 | n = strlen(bp) + 1; /* for the \0 */ |
362 | if (n >= MAXHOSTNAMELEN) { |
363 | had_error++; |
364 | break; |
365 | } |
366 | bp += n; |
367 | buflen -= n; |
368 | } |
369 | break; |
370 | #else |
371 | host.h_name = bp; |
372 | if (_res.options & RES_USE_INET6) { |
373 | n = strlen(bp) + 1; /* for the \0 */ |
374 | if (n >= MAXHOSTNAMELEN) { |
375 | had_error++; |
376 | break; |
377 | } |
378 | bp += n; |
379 | buflen -= n; |
380 | map_v4v6_hostent(&host, &bp, &buflen); |
381 | } |
382 | __set_h_errno (NETDB_SUCCESS); |
383 | return (&host); |
384 | #endif |
385 | case T_A: |
386 | case T_AAAA: |
387 | if (strcasecmp(host.h_name, bp) != 0) { |
388 | cp += n; |
389 | continue; /* XXX - had_error++ ? */ |
390 | } |
391 | if (n != host.h_length) { |
392 | cp += n; |
393 | continue; |
394 | } |
395 | if (!haveanswer) { |
396 | int nn; |
397 | |
398 | host.h_name = bp; |
399 | nn = strlen(bp) + 1; /* for the \0 */ |
400 | bp += nn; |
401 | buflen -= nn; |
402 | } |
403 | |
404 | /* XXX: when incrementing bp, we have to decrement |
405 | * buflen by the same amount --okir */ |
406 | buflen -= sizeof(align) - ((u_long)bp % sizeof(align)); |
407 | |
408 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); |
409 | |
410 | if (bp + n >= &hostbuf[sizeof hostbuf]) { |
411 | Dprintf("size (%d) too big\n" , n); |
412 | had_error++; |
413 | continue; |
414 | } |
415 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { |
416 | if (!toobig++) { |
417 | Dprintf("Too many addresses (%d)\n" , |
418 | MAXADDRS); |
419 | } |
420 | cp += n; |
421 | continue; |
422 | } |
423 | memmove(*hap++ = bp, cp, n); |
424 | bp += n; |
425 | buflen -= n; |
426 | cp += n; |
427 | if (cp != erdata) { |
428 | __set_h_errno (NO_RECOVERY); |
429 | return (NULL); |
430 | } |
431 | break; |
432 | default: |
433 | abort(); |
434 | } |
435 | if (!had_error) |
436 | haveanswer++; |
437 | } |
438 | if (haveanswer) { |
439 | *ap = NULL; |
440 | *hap = NULL; |
441 | # if defined(RESOLVSORT) |
442 | /* |
443 | * Note: we sort even if host can take only one address |
444 | * in its return structures - should give it the "best" |
445 | * address in that case, not some random one |
446 | */ |
447 | if (_res.nsort && haveanswer > 1 && qtype == T_A) |
448 | addrsort(h_addr_ptrs, haveanswer); |
449 | # endif /*RESOLVSORT*/ |
450 | if (!host.h_name) { |
451 | n = strlen(qname) + 1; /* for the \0 */ |
452 | if (n > buflen || n >= MAXHOSTNAMELEN) |
453 | goto no_recovery; |
454 | strcpy(bp, qname); |
455 | host.h_name = bp; |
456 | bp += n; |
457 | buflen -= n; |
458 | } |
459 | if (_res.options & RES_USE_INET6) |
460 | map_v4v6_hostent(&host, &bp, &buflen); |
461 | __set_h_errno (NETDB_SUCCESS); |
462 | return (&host); |
463 | } |
464 | no_recovery: |
465 | __set_h_errno (NO_RECOVERY); |
466 | return (NULL); |
467 | } |
468 | |
469 | extern struct hostent *gethostbyname2(const char *name, int af); |
470 | libresolv_hidden_proto (gethostbyname2) |
471 | |
472 | struct hostent * |
473 | gethostbyname (const char *name) |
474 | { |
475 | struct hostent *hp; |
476 | |
477 | if (__res_maybe_init (&_res, 0) == -1) { |
478 | __set_h_errno (NETDB_INTERNAL); |
479 | return (NULL); |
480 | } |
481 | if (_res.options & RES_USE_INET6) { |
482 | hp = gethostbyname2(name, AF_INET6); |
483 | if (hp) |
484 | return (hp); |
485 | } |
486 | return (gethostbyname2(name, AF_INET)); |
487 | } |
488 | |
489 | struct hostent * |
490 | gethostbyname2 (const char *name, int af) |
491 | { |
492 | union |
493 | { |
494 | querybuf *buf; |
495 | u_char *ptr; |
496 | } buf; |
497 | querybuf *origbuf; |
498 | const char *cp; |
499 | char *bp; |
500 | int n, size, type, len; |
501 | struct hostent *ret; |
502 | |
503 | if (__res_maybe_init (&_res, 0) == -1) { |
504 | __set_h_errno (NETDB_INTERNAL); |
505 | return (NULL); |
506 | } |
507 | |
508 | switch (af) { |
509 | case AF_INET: |
510 | size = INADDRSZ; |
511 | type = T_A; |
512 | break; |
513 | case AF_INET6: |
514 | size = IN6ADDRSZ; |
515 | type = T_AAAA; |
516 | break; |
517 | default: |
518 | __set_h_errno (NETDB_INTERNAL); |
519 | __set_errno (EAFNOSUPPORT); |
520 | return (NULL); |
521 | } |
522 | |
523 | host.h_addrtype = af; |
524 | host.h_length = size; |
525 | |
526 | /* |
527 | * if there aren't any dots, it could be a user-level alias. |
528 | * this is also done in res_query() since we are not the only |
529 | * function that looks up host names. |
530 | */ |
531 | if (!strchr(name, '.') && (cp = __hostalias(name))) |
532 | name = cp; |
533 | |
534 | /* |
535 | * disallow names consisting only of digits/dots, unless |
536 | * they end in a dot. |
537 | */ |
538 | if (isdigit(name[0])) |
539 | for (cp = name;; ++cp) { |
540 | if (!*cp) { |
541 | if (*--cp == '.') |
542 | break; |
543 | /* |
544 | * All-numeric, no dot at the end. |
545 | * Fake up a hostent as if we'd actually |
546 | * done a lookup. |
547 | */ |
548 | if (inet_pton(af, name, host_addr) <= 0) { |
549 | __set_h_errno (HOST_NOT_FOUND); |
550 | return (NULL); |
551 | } |
552 | strncpy(hostbuf, name, MAXDNAME); |
553 | hostbuf[MAXDNAME] = '\0'; |
554 | bp = hostbuf + MAXDNAME; |
555 | len = sizeof hostbuf - MAXDNAME; |
556 | host.h_name = hostbuf; |
557 | host.h_aliases = host_aliases; |
558 | host_aliases[0] = NULL; |
559 | h_addr_ptrs[0] = (char *)host_addr; |
560 | h_addr_ptrs[1] = NULL; |
561 | host.h_addr_list = h_addr_ptrs; |
562 | if (_res.options & RES_USE_INET6) |
563 | map_v4v6_hostent(&host, &bp, &len); |
564 | __set_h_errno (NETDB_SUCCESS); |
565 | return (&host); |
566 | } |
567 | if (!isdigit(*cp) && *cp != '.') |
568 | break; |
569 | } |
570 | if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || |
571 | name[0] == ':') |
572 | for (cp = name;; ++cp) { |
573 | if (!*cp) { |
574 | if (*--cp == '.') |
575 | break; |
576 | /* |
577 | * All-IPv6-legal, no dot at the end. |
578 | * Fake up a hostent as if we'd actually |
579 | * done a lookup. |
580 | */ |
581 | if (inet_pton(af, name, host_addr) <= 0) { |
582 | __set_h_errno (HOST_NOT_FOUND); |
583 | return (NULL); |
584 | } |
585 | strncpy(hostbuf, name, MAXDNAME); |
586 | hostbuf[MAXDNAME] = '\0'; |
587 | bp = hostbuf + MAXDNAME; |
588 | len = sizeof hostbuf - MAXDNAME; |
589 | host.h_name = hostbuf; |
590 | host.h_aliases = host_aliases; |
591 | host_aliases[0] = NULL; |
592 | h_addr_ptrs[0] = (char *)host_addr; |
593 | h_addr_ptrs[1] = NULL; |
594 | host.h_addr_list = h_addr_ptrs; |
595 | __set_h_errno (NETDB_SUCCESS); |
596 | return (&host); |
597 | } |
598 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') |
599 | break; |
600 | } |
601 | |
602 | buf.buf = origbuf = (querybuf *) alloca (1024); |
603 | |
604 | if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024, |
605 | &buf.ptr, NULL, NULL, NULL, NULL)) < 0) { |
606 | if (buf.buf != origbuf) |
607 | free (buf.buf); |
608 | Dprintf("res_nsearch failed (%d)\n" , n); |
609 | if (errno == ECONNREFUSED) |
610 | return (_gethtbyname2(name, af)); |
611 | return (NULL); |
612 | } |
613 | ret = getanswer(buf.buf, n, name, type); |
614 | if (buf.buf != origbuf) |
615 | free (buf.buf); |
616 | return ret; |
617 | } |
618 | libresolv_hidden_def (gethostbyname2) |
619 | |
620 | struct hostent * |
621 | gethostbyaddr (const void *addr, socklen_t len, int af) |
622 | { |
623 | const u_char *uaddr = (const u_char *)addr; |
624 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; |
625 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; |
626 | int n; |
627 | socklen_t size; |
628 | union |
629 | { |
630 | querybuf *buf; |
631 | u_char *ptr; |
632 | } buf; |
633 | querybuf *orig_buf; |
634 | struct hostent *hp; |
635 | char qbuf[MAXDNAME+1], *qp = NULL; |
636 | #ifdef SUNSECURITY |
637 | struct hostent *rhp; |
638 | char **haddr; |
639 | u_long old_options; |
640 | char hname2[MAXDNAME+1]; |
641 | #endif /*SUNSECURITY*/ |
642 | |
643 | if (__res_maybe_init (&_res, 0) == -1) { |
644 | __set_h_errno (NETDB_INTERNAL); |
645 | return (NULL); |
646 | } |
647 | if (af == AF_INET6 && len == IN6ADDRSZ && |
648 | (!memcmp(uaddr, mapped, sizeof mapped) || |
649 | !memcmp(uaddr, tunnelled, sizeof tunnelled))) { |
650 | /* Unmap. */ |
651 | addr += sizeof mapped; |
652 | uaddr += sizeof mapped; |
653 | af = AF_INET; |
654 | len = INADDRSZ; |
655 | } |
656 | switch (af) { |
657 | case AF_INET: |
658 | size = INADDRSZ; |
659 | break; |
660 | case AF_INET6: |
661 | size = IN6ADDRSZ; |
662 | break; |
663 | default: |
664 | __set_errno (EAFNOSUPPORT); |
665 | __set_h_errno (NETDB_INTERNAL); |
666 | return (NULL); |
667 | } |
668 | if (size != len) { |
669 | __set_errno (EINVAL); |
670 | __set_h_errno (NETDB_INTERNAL); |
671 | return (NULL); |
672 | } |
673 | switch (af) { |
674 | case AF_INET: |
675 | (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa" , |
676 | (uaddr[3] & 0xff), |
677 | (uaddr[2] & 0xff), |
678 | (uaddr[1] & 0xff), |
679 | (uaddr[0] & 0xff)); |
680 | break; |
681 | case AF_INET6: |
682 | qp = qbuf; |
683 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { |
684 | qp += SPRINTF((qp, "%x.%x." , |
685 | uaddr[n] & 0xf, |
686 | (uaddr[n] >> 4) & 0xf)); |
687 | } |
688 | strcpy(qp, "ip6.arpa" ); |
689 | break; |
690 | default: |
691 | abort(); |
692 | } |
693 | |
694 | buf.buf = orig_buf = (querybuf *) alloca (1024); |
695 | |
696 | n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024, |
697 | &buf.ptr, NULL, NULL, NULL, NULL); |
698 | if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) { |
699 | strcpy(qp, "ip6.int" ); |
700 | n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, |
701 | buf.buf != orig_buf ? MAXPACKET : 1024, |
702 | &buf.ptr, NULL, NULL, NULL, NULL); |
703 | } |
704 | if (n < 0) { |
705 | if (buf.buf != orig_buf) |
706 | free (buf.buf); |
707 | Dprintf("res_nquery failed (%d)\n" , n); |
708 | if (errno == ECONNREFUSED) |
709 | return (_gethtbyaddr(addr, len, af)); |
710 | return (NULL); |
711 | } |
712 | hp = getanswer(buf.buf, n, qbuf, T_PTR); |
713 | if (buf.buf != orig_buf) |
714 | free (buf.buf); |
715 | if (!hp) |
716 | return (NULL); /* h_errno was set by getanswer() */ |
717 | #ifdef SUNSECURITY |
718 | if (af == AF_INET) { |
719 | /* |
720 | * turn off search as the name should be absolute, |
721 | * 'localhost' should be matched by defnames |
722 | */ |
723 | strncpy(hname2, hp->h_name, MAXDNAME); |
724 | hname2[MAXDNAME] = '\0'; |
725 | old_options = _res.options; |
726 | _res.options &= ~RES_DNSRCH; |
727 | _res.options |= RES_DEFNAMES; |
728 | if (!(rhp = gethostbyname(hname2))) { |
729 | _res.options = old_options; |
730 | __set_h_errno (HOST_NOT_FOUND); |
731 | return (NULL); |
732 | } |
733 | _res.options = old_options; |
734 | for (haddr = rhp->h_addr_list; *haddr; haddr++) |
735 | if (!memcmp(*haddr, addr, INADDRSZ)) |
736 | break; |
737 | if (!*haddr) { |
738 | __set_h_errno (HOST_NOT_FOUND); |
739 | return (NULL); |
740 | } |
741 | } |
742 | #endif /*SUNSECURITY*/ |
743 | hp->h_addrtype = af; |
744 | hp->h_length = len; |
745 | memmove(host_addr, addr, len); |
746 | h_addr_ptrs[0] = (char *)host_addr; |
747 | h_addr_ptrs[1] = NULL; |
748 | if (af == AF_INET && (_res.options & RES_USE_INET6)) { |
749 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
750 | hp->h_addrtype = AF_INET6; |
751 | hp->h_length = IN6ADDRSZ; |
752 | } |
753 | __set_h_errno (NETDB_SUCCESS); |
754 | return (hp); |
755 | } |
756 | |
757 | void |
758 | _sethtent (int f) |
759 | { |
760 | if (!hostf) |
761 | hostf = fopen(_PATH_HOSTS, "rce" ); |
762 | else |
763 | rewind(hostf); |
764 | stayopen = f; |
765 | } |
766 | libresolv_hidden_def (_sethtent) |
767 | |
768 | void |
769 | _endhtent (void) |
770 | { |
771 | if (hostf && !stayopen) { |
772 | (void) fclose(hostf); |
773 | hostf = NULL; |
774 | } |
775 | } |
776 | |
777 | struct hostent * |
778 | _gethtent (void) |
779 | { |
780 | char *p; |
781 | char *cp, **q; |
782 | int af, len; |
783 | |
784 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) { |
785 | __set_h_errno (NETDB_INTERNAL); |
786 | return (NULL); |
787 | } |
788 | again: |
789 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { |
790 | __set_h_errno (HOST_NOT_FOUND); |
791 | return (NULL); |
792 | } |
793 | if (*p == '#') |
794 | goto again; |
795 | if (!(cp = strpbrk(p, "#\n" ))) |
796 | goto again; |
797 | *cp = '\0'; |
798 | if (!(cp = strpbrk(p, " \t" ))) |
799 | goto again; |
800 | *cp++ = '\0'; |
801 | if (inet_pton(AF_INET6, p, host_addr) > 0) { |
802 | af = AF_INET6; |
803 | len = IN6ADDRSZ; |
804 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { |
805 | if (_res.options & RES_USE_INET6) { |
806 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
807 | af = AF_INET6; |
808 | len = IN6ADDRSZ; |
809 | } else { |
810 | af = AF_INET; |
811 | len = INADDRSZ; |
812 | } |
813 | } else { |
814 | goto again; |
815 | } |
816 | h_addr_ptrs[0] = (char *)host_addr; |
817 | h_addr_ptrs[1] = NULL; |
818 | host.h_addr_list = h_addr_ptrs; |
819 | host.h_length = len; |
820 | host.h_addrtype = af; |
821 | while (*cp == ' ' || *cp == '\t') |
822 | cp++; |
823 | host.h_name = cp; |
824 | q = host.h_aliases = host_aliases; |
825 | if ((cp = strpbrk(cp, " \t" ))) |
826 | *cp++ = '\0'; |
827 | while (cp && *cp) { |
828 | if (*cp == ' ' || *cp == '\t') { |
829 | cp++; |
830 | continue; |
831 | } |
832 | if (q < &host_aliases[MAXALIASES - 1]) |
833 | *q++ = cp; |
834 | if ((cp = strpbrk(cp, " \t" ))) |
835 | *cp++ = '\0'; |
836 | } |
837 | *q = NULL; |
838 | __set_h_errno (NETDB_SUCCESS); |
839 | return (&host); |
840 | } |
841 | libresolv_hidden_def (_gethtent) |
842 | |
843 | struct hostent * |
844 | _gethtbyname (const char *name) |
845 | { |
846 | struct hostent *hp; |
847 | |
848 | if (_res.options & RES_USE_INET6) { |
849 | hp = _gethtbyname2(name, AF_INET6); |
850 | if (hp) |
851 | return (hp); |
852 | } |
853 | return (_gethtbyname2(name, AF_INET)); |
854 | } |
855 | |
856 | struct hostent * |
857 | _gethtbyname2 (const char *name, int af) |
858 | { |
859 | struct hostent *p; |
860 | char **cp; |
861 | |
862 | _sethtent(0); |
863 | while ((p = _gethtent())) { |
864 | if (p->h_addrtype != af) |
865 | continue; |
866 | if (strcasecmp(p->h_name, name) == 0) |
867 | break; |
868 | for (cp = p->h_aliases; *cp != 0; cp++) |
869 | if (strcasecmp(*cp, name) == 0) |
870 | goto found; |
871 | } |
872 | found: |
873 | _endhtent(); |
874 | return (p); |
875 | } |
876 | libresolv_hidden_def (_gethtbyname2) |
877 | |
878 | struct hostent * |
879 | _gethtbyaddr (const char *addr, size_t len, int af) |
880 | { |
881 | struct hostent *p; |
882 | |
883 | _sethtent(0); |
884 | while ((p = _gethtent())) |
885 | if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len)) |
886 | break; |
887 | _endhtent(); |
888 | return (p); |
889 | } |
890 | libresolv_hidden_def (_gethtbyaddr) |
891 | |
892 | static void |
893 | map_v4v6_address (const char *src, char *dst) |
894 | { |
895 | u_char *p = (u_char *)dst; |
896 | char tmp[INADDRSZ]; |
897 | int i; |
898 | |
899 | /* Stash a temporary copy so our caller can update in place. */ |
900 | memcpy(tmp, src, INADDRSZ); |
901 | /* Mark this ipv6 addr as a mapped ipv4. */ |
902 | for (i = 0; i < 10; i++) |
903 | *p++ = 0x00; |
904 | *p++ = 0xff; |
905 | *p++ = 0xff; |
906 | /* Retrieve the saved copy and we're done. */ |
907 | memcpy((void*)p, tmp, INADDRSZ); |
908 | } |
909 | |
910 | static void |
911 | map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp) |
912 | { |
913 | char **ap; |
914 | |
915 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) |
916 | return; |
917 | hp->h_addrtype = AF_INET6; |
918 | hp->h_length = IN6ADDRSZ; |
919 | for (ap = hp->h_addr_list; *ap; ap++) { |
920 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); |
921 | |
922 | if (*lenp < (i + IN6ADDRSZ)) { |
923 | /* Out of memory. Truncate address list here. XXX */ |
924 | *ap = NULL; |
925 | return; |
926 | } |
927 | *bpp += i; |
928 | *lenp -= i; |
929 | map_v4v6_address(*ap, *bpp); |
930 | *ap = *bpp; |
931 | *bpp += IN6ADDRSZ; |
932 | *lenp -= IN6ADDRSZ; |
933 | } |
934 | } |
935 | |
936 | #ifdef RESOLVSORT |
937 | extern void |
938 | addrsort (char **ap, int num) |
939 | { |
940 | int i, j; |
941 | char **p; |
942 | short aval[MAXADDRS]; |
943 | int needsort = 0; |
944 | |
945 | p = ap; |
946 | for (i = 0; i < num; i++, p++) { |
947 | for (j = 0 ; (unsigned)j < _res.nsort; j++) |
948 | if (_res.sort_list[j].addr.s_addr == |
949 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) |
950 | break; |
951 | aval[i] = j; |
952 | if (needsort == 0 && i > 0 && j < aval[i-1]) |
953 | needsort = i; |
954 | } |
955 | if (!needsort) |
956 | return; |
957 | |
958 | while (needsort < num) { |
959 | for (j = needsort - 1; j >= 0; j--) { |
960 | if (aval[j] > aval[j+1]) { |
961 | char *hp; |
962 | |
963 | i = aval[j]; |
964 | aval[j] = aval[j+1]; |
965 | aval[j+1] = i; |
966 | |
967 | hp = ap[j]; |
968 | ap[j] = ap[j+1]; |
969 | ap[j+1] = hp; |
970 | |
971 | } else |
972 | break; |
973 | } |
974 | needsort++; |
975 | } |
976 | } |
977 | #endif |
978 | |
979 | #if defined(BSD43_BSD43_NFS) || defined(sun) |
980 | /* some libc's out there are bound internally to these names (UMIPS) */ |
981 | void |
982 | ht_sethostent (int stayopen) |
983 | { |
984 | _sethtent(stayopen); |
985 | } |
986 | |
987 | void |
988 | ht_endhostent (void) |
989 | { |
990 | _endhtent(); |
991 | } |
992 | |
993 | struct hostent * |
994 | ht_gethostbyname (char *name) |
995 | { |
996 | return (_gethtbyname(name)); |
997 | } |
998 | |
999 | struct hostent * |
1000 | ht_gethostbyaddr (const char *addr, size_t len, int af) |
1001 | { |
1002 | return (_gethtbyaddr(addr, len, af)); |
1003 | } |
1004 | |
1005 | struct hostent * |
1006 | gethostent (void) |
1007 | { |
1008 | return (_gethtent()); |
1009 | } |
1010 | |
1011 | void |
1012 | dns_service (void) |
1013 | { |
1014 | return; |
1015 | } |
1016 | |
1017 | #undef dn_skipname |
1018 | dn_skipname(comp_dn, eom) |
1019 | const u_char *comp_dn, *eom; |
1020 | { |
1021 | return (__dn_skipname(comp_dn, eom)); |
1022 | } |
1023 | #endif /*old-style libc with yp junk in it*/ |
1024 | |