blob: e6f919ec26e22b4e2b350abdca1da84428c38d3f [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001/* $NetBSD: gethnamaddr.c,v 1.70 2006/03/22 00:03:51 christos Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#include <sys/cdefs.h>
55#include <sys/types.h>
56
57#include <sys/param.h>
58#include <sys/socket.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
61#include "arpa_nameser.h"
62#include "resolv_private.h"
63#include "resolv_cache.h"
64#include <assert.h>
65#include <ctype.h>
66#include <errno.h>
67#include <netdb.h>
68#include <stdarg.h>
69#include <stdio.h>
70#include <syslog.h>
71
72#ifndef LOG_AUTH
73# define LOG_AUTH 0
74#endif
75
76#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
77
78#include "nsswitch.h"
79#include <stdlib.h>
80#include <string.h>
81
82static const char const AskedForGot[] =
83 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
84
85#define MAXPACKET (64*1024)
86
87typedef union {
88 HEADER hdr;
89 u_char buf[MAXPACKET];
90} querybuf;
91
92typedef union {
93 int32_t al;
94 char ac;
95} align;
96
97#ifdef DEBUG
98static void dprintf(const char *, res_state, ...)
99 __attribute__((__format__(__printf__, 1, 3)));
100#endif
101static struct hostent *getanswer(const querybuf *, int, const char *, int,
102 res_state);
103static void map_v4v6_address(const char *, char *);
104static void map_v4v6_hostent(struct hostent *, char **, char *);
105static void addrsort(char **, int, res_state);
106
107void _sethtent(int);
108void _endhtent(void);
109struct hostent *_gethtent(void);
110void ht_sethostent(int);
111void ht_endhostent(void);
112struct hostent *ht_gethostbyname(char *);
113struct hostent *ht_gethostbyaddr(const char *, int, int);
114void dns_service(void);
115#undef dn_skipname
116int dn_skipname(const u_char *, const u_char *);
117int _gethtbyaddr(void *, void *, va_list);
118int _gethtbyname(void *, void *, va_list);
119struct hostent *_gethtbyname2(const char *, int);
120int _dns_gethtbyaddr(void *, void *, va_list);
121int _dns_gethtbyname(void *, void *, va_list);
122
123static struct hostent *gethostbyname_internal(const char *, int, res_state);
124
125static const ns_src default_dns_files[] = {
126 { NSSRC_FILES, NS_SUCCESS },
127 { NSSRC_DNS, NS_SUCCESS },
128 { 0, 0 }
129};
130
131
132#ifdef DEBUG
133static void
134dprintf(const char *msg, res_state res, ...)
135{
136 assert(msg != NULL);
137
138 if (res->options & RES_DEBUG) {
139 int save = errno;
140 va_list ap;
141
142 va_start (ap, res);
143 vprintf(msg, ap);
144 va_end (ap);
145
146 errno = save;
147 }
148}
149#else
150# define dprintf(msg, res, num) ((void)0) /*nada*/
151#endif
152
153#define BOUNDED_INCR(x) \
154 do { \
155 cp += (x); \
156 if (cp > eom) { \
157 h_errno = NO_RECOVERY; \
158 return NULL; \
159 } \
160 } while (/*CONSTCOND*/0)
161
162#define BOUNDS_CHECK(ptr, count) \
163 do { \
164 if ((ptr) + (count) > eom) { \
165 h_errno = NO_RECOVERY; \
166 return NULL; \
167 } \
168 } while (/*CONSTCOND*/0)
169
170static struct hostent *
171getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
172 res_state res)
173{
174 const HEADER *hp;
175 const u_char *cp;
176 int n;
177 const u_char *eom, *erdata;
178 char *bp, **ap, **hap, *ep;
179 int type, class, ancount, qdcount;
180 int haveanswer, had_error;
181 int toobig = 0;
182 char tbuf[MAXDNAME];
183 const char *tname;
184 int (*name_ok)(const char *);
185 res_static rs = __res_get_static();
186
187 assert(answer != NULL);
188 assert(qname != NULL);
189
190 tname = qname;
191 rs->host.h_name = NULL;
192 eom = answer->buf + anslen;
193 switch (qtype) {
194 case T_A:
195 case T_AAAA:
196 name_ok = res_hnok;
197 break;
198 case T_PTR:
199 name_ok = res_dnok;
200 break;
201 default:
202 return NULL; /* XXX should be abort(); */
203 }
204 /*
205 * find first satisfactory answer
206 */
207 hp = &answer->hdr;
208 ancount = ntohs(hp->ancount);
209 qdcount = ntohs(hp->qdcount);
210 bp = rs->hostbuf;
211 ep = rs->hostbuf + sizeof rs->hostbuf;
212 cp = answer->buf;
213 BOUNDED_INCR(HFIXEDSZ);
214 if (qdcount != 1) {
215 h_errno = NO_RECOVERY;
216 return NULL;
217 }
218 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
219 if ((n < 0) || !(*name_ok)(bp)) {
220 h_errno = NO_RECOVERY;
221 return NULL;
222 }
223 BOUNDED_INCR(n + QFIXEDSZ);
224 if (qtype == T_A || qtype == T_AAAA) {
225 /* res_send() has already verified that the query name is the
226 * same as the one we sent; this just gets the expanded name
227 * (i.e., with the succeeding search-domain tacked on).
228 */
229 n = strlen(bp) + 1; /* for the \0 */
230 if (n >= MAXHOSTNAMELEN) {
231 h_errno = NO_RECOVERY;
232 return NULL;
233 }
234 rs->host.h_name = bp;
235 bp += n;
236 /* The qname can be abbreviated, but h_name is now absolute. */
237 qname = rs->host.h_name;
238 }
239 ap = rs->host_aliases;
240 *ap = NULL;
241 rs->host.h_aliases = rs->host_aliases;
242 hap = rs->h_addr_ptrs;
243 *hap = NULL;
244 rs->host.h_addr_list = rs->h_addr_ptrs;
245 haveanswer = 0;
246 had_error = 0;
247 while (ancount-- > 0 && cp < eom && !had_error) {
248 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
249 if ((n < 0) || !(*name_ok)(bp)) {
250 had_error++;
251 continue;
252 }
253 cp += n; /* name */
254 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
255 type = _getshort(cp);
256 cp += INT16SZ; /* type */
257 class = _getshort(cp);
258 cp += INT16SZ + INT32SZ; /* class, TTL */
259 n = _getshort(cp);
260 cp += INT16SZ; /* len */
261 BOUNDS_CHECK(cp, n);
262 erdata = cp + n;
263 if (class != C_IN) {
264 /* XXX - debug? syslog? */
265 cp += n;
266 continue; /* XXX - had_error++ ? */
267 }
268 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
269 if (ap >= &rs->host_aliases[MAXALIASES-1])
270 continue;
271 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
272 if ((n < 0) || !(*name_ok)(tbuf)) {
273 had_error++;
274 continue;
275 }
276 cp += n;
277 if (cp != erdata) {
278 h_errno = NO_RECOVERY;
279 return NULL;
280 }
281 /* Store alias. */
282 *ap++ = bp;
283 n = strlen(bp) + 1; /* for the \0 */
284 if (n >= MAXHOSTNAMELEN) {
285 had_error++;
286 continue;
287 }
288 bp += n;
289 /* Get canonical name. */
290 n = strlen(tbuf) + 1; /* for the \0 */
291 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
292 had_error++;
293 continue;
294 }
295 strlcpy(bp, tbuf, (size_t)(ep - bp));
296 rs->host.h_name = bp;
297 bp += n;
298 continue;
299 }
300 if (qtype == T_PTR && type == T_CNAME) {
301 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
302 if (n < 0 || !res_dnok(tbuf)) {
303 had_error++;
304 continue;
305 }
306 cp += n;
307 if (cp != erdata) {
308 h_errno = NO_RECOVERY;
309 return NULL;
310 }
311 /* Get canonical name. */
312 n = strlen(tbuf) + 1; /* for the \0 */
313 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
314 had_error++;
315 continue;
316 }
317 strlcpy(bp, tbuf, (size_t)(ep - bp));
318 tname = bp;
319 bp += n;
320 continue;
321 }
322 if (type != qtype) {
323 if (type != T_KEY && type != T_SIG)
324 syslog(LOG_NOTICE|LOG_AUTH,
325 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
326 qname, p_class(C_IN), p_type(qtype),
327 p_type(type));
328 cp += n;
329 continue; /* XXX - had_error++ ? */
330 }
331 switch (type) {
332 case T_PTR:
333 if (strcasecmp(tname, bp) != 0) {
334 syslog(LOG_NOTICE|LOG_AUTH,
335 AskedForGot, qname, bp);
336 cp += n;
337 continue; /* XXX - had_error++ ? */
338 }
339 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
340 if ((n < 0) || !res_hnok(bp)) {
341 had_error++;
342 break;
343 }
344#if MULTI_PTRS_ARE_ALIASES
345 cp += n;
346 if (cp != erdata) {
347 h_errno = NO_RECOVERY;
348 return NULL;
349 }
350 if (!haveanswer)
351 rs->host.h_name = bp;
352 else if (ap < &rs->host_aliases[MAXALIASES-1])
353 *ap++ = bp;
354 else
355 n = -1;
356 if (n != -1) {
357 n = strlen(bp) + 1; /* for the \0 */
358 if (n >= MAXHOSTNAMELEN) {
359 had_error++;
360 break;
361 }
362 bp += n;
363 }
364 break;
365#else
366 rs->host.h_name = bp;
367 if (res->options & RES_USE_INET6) {
368 n = strlen(bp) + 1; /* for the \0 */
369 if (n >= MAXHOSTNAMELEN) {
370 had_error++;
371 break;
372 }
373 bp += n;
374 map_v4v6_hostent(&rs->host, &bp, ep);
375 }
376 h_errno = NETDB_SUCCESS;
377 return &rs->host;
378#endif
379 case T_A:
380 case T_AAAA:
381 if (strcasecmp(rs->host.h_name, bp) != 0) {
382 syslog(LOG_NOTICE|LOG_AUTH,
383 AskedForGot, rs->host.h_name, bp);
384 cp += n;
385 continue; /* XXX - had_error++ ? */
386 }
387 if (n != rs->host.h_length) {
388 cp += n;
389 continue;
390 }
391 if (type == T_AAAA) {
392 struct in6_addr in6;
393 memcpy(&in6, cp, IN6ADDRSZ);
394 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
395 cp += n;
396 continue;
397 }
398 }
399 if (!haveanswer) {
400 int nn;
401
402 rs->host.h_name = bp;
403 nn = strlen(bp) + 1; /* for the \0 */
404 bp += nn;
405 }
406
407 bp += sizeof(align) -
408 (size_t)((u_long)bp % sizeof(align));
409
410 if (bp + n >= &rs->hostbuf[sizeof rs->hostbuf]) {
411 dprintf("size (%d) too big\n", res, n);
412 had_error++;
413 continue;
414 }
415 if (hap >= &rs->h_addr_ptrs[MAXADDRS-1]) {
416 if (!toobig++)
417 dprintf("Too many addresses (%d)\n",
418 res, MAXADDRS);
419 cp += n;
420 continue;
421 }
422 (void)memcpy(*hap++ = bp, cp, (size_t)n);
423 bp += n;
424 cp += n;
425 if (cp != erdata) {
426 h_errno = NO_RECOVERY;
427 return NULL;
428 }
429 break;
430 default:
431 abort();
432 }
433 if (!had_error)
434 haveanswer++;
435 }
436 if (haveanswer) {
437 *ap = NULL;
438 *hap = NULL;
439 /*
440 * Note: we sort even if host can take only one address
441 * in its return structures - should give it the "best"
442 * address in that case, not some random one
443 */
444 if (res->nsort && haveanswer > 1 && qtype == T_A)
445 addrsort(rs->h_addr_ptrs, haveanswer, res);
446 if (!rs->host.h_name) {
447 n = strlen(qname) + 1; /* for the \0 */
448 if (n > ep - bp || n >= MAXHOSTNAMELEN)
449 goto no_recovery;
450 strlcpy(bp, qname, (size_t)(ep - bp));
451 rs->host.h_name = bp;
452 bp += n;
453 }
454 if (res->options & RES_USE_INET6)
455 map_v4v6_hostent(&rs->host, &bp, ep);
456 h_errno = NETDB_SUCCESS;
457 return &rs->host;
458 }
459 no_recovery:
460 h_errno = NO_RECOVERY;
461 return NULL;
462}
463
464int
465gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
466 struct hostent**result, int *errorp)
467{
468 struct hostent *res;
469
470 res = gethostbyname(name);
471 *errorp = h_errno;
472 if (res == NULL) {
473 *result = NULL;
474 return -1;
475 }
476 memcpy(hp, res, sizeof *hp);
477 *result = hp;
478 return 0;
479}
480
481struct hostent *
482gethostbyname(const char *name)
483{
484 struct hostent *hp;
485 res_state res = __res_get_state();
486
487 if (res == NULL)
488 return NULL;
489
490 assert(name != NULL);
491
492 if (res->options & RES_USE_INET6) {
493 hp = gethostbyname_internal(name, AF_INET6, res);
494 if (hp) {
495 __res_put_state(res);
496 return hp;
497 }
498 }
499 hp = gethostbyname_internal(name, AF_INET, res);
500 __res_put_state(res);
501 return hp;
502}
503
504struct hostent *
505gethostbyname2(const char *name, int af)
506{
507 struct hostent *hp;
508 res_state res = __res_get_state();
509
510 if (res == NULL)
511 return NULL;
512 hp = gethostbyname_internal(name, af, res);
513 __res_put_state(res);
514 return hp;
515}
516
517static struct hostent *
518gethostbyname_internal(const char *name, int af, res_state res)
519{
520 const char *cp;
521 char *bp, *ep;
522 int size;
523 struct hostent *hp;
524 struct resolv_cache* cache;
525 res_static rs = __res_get_static();
526
527 static const ns_dtab dtab[] = {
528 NS_FILES_CB(_gethtbyname, NULL)
529 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
530 { 0, 0, 0 }
531 };
532
533 assert(name != NULL);
534
535 switch (af) {
536 case AF_INET:
537 size = INADDRSZ;
538 break;
539 case AF_INET6:
540 size = IN6ADDRSZ;
541 break;
542 default:
543 h_errno = NETDB_INTERNAL;
544 errno = EAFNOSUPPORT;
545 return NULL;
546 }
547
548 rs->host.h_addrtype = af;
549 rs->host.h_length = size;
550
551 /*
552 * if there aren't any dots, it could be a user-level alias.
553 * this is also done in res_nquery() since we are not the only
554 * function that looks up host names.
555 */
556 if (!strchr(name, '.') && (cp = __hostalias(name)))
557 name = cp;
558
559 /*
560 * disallow names consisting only of digits/dots, unless
561 * they end in a dot.
562 */
563 if (isdigit((u_char) name[0]))
564 for (cp = name;; ++cp) {
565 if (!*cp) {
566 if (*--cp == '.')
567 break;
568 /*
569 * All-numeric, no dot at the end.
570 * Fake up a hostent as if we'd actually
571 * done a lookup.
572 */
573 if (inet_pton(af, name,
574 (char *)(void *)rs->host_addr) <= 0) {
575 h_errno = HOST_NOT_FOUND;
576 return NULL;
577 }
578 strncpy(rs->hostbuf, name, MAXDNAME);
579 rs->hostbuf[MAXDNAME] = '\0';
580 bp = rs->hostbuf + MAXDNAME;
581 ep = rs->hostbuf + sizeof rs->hostbuf;
582 rs->host.h_name = rs->hostbuf;
583 rs->host.h_aliases = rs->host_aliases;
584 rs->host_aliases[0] = NULL;
585 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
586 rs->h_addr_ptrs[1] = NULL;
587 rs->host.h_addr_list = rs->h_addr_ptrs;
588 if (res->options & RES_USE_INET6)
589 map_v4v6_hostent(&rs->host, &bp, ep);
590 h_errno = NETDB_SUCCESS;
591 return &rs->host;
592 }
593 if (!isdigit((u_char) *cp) && *cp != '.')
594 break;
595 }
596 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
597 name[0] == ':')
598 for (cp = name;; ++cp) {
599 if (!*cp) {
600 if (*--cp == '.')
601 break;
602 /*
603 * All-IPv6-legal, no dot at the end.
604 * Fake up a hostent as if we'd actually
605 * done a lookup.
606 */
607 if (inet_pton(af, name,
608 (char *)(void *)rs->host_addr) <= 0) {
609 h_errno = HOST_NOT_FOUND;
610 return NULL;
611 }
612 strncpy(rs->hostbuf, name, MAXDNAME);
613 rs->hostbuf[MAXDNAME] = '\0';
614 bp = rs->hostbuf + MAXDNAME;
615 ep = rs->hostbuf + sizeof rs->hostbuf;
616 rs->host.h_name = rs->hostbuf;
617 rs->host.h_aliases = rs->host_aliases;
618 rs->host_aliases[0] = NULL;
619 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
620 rs->h_addr_ptrs[1] = NULL;
621 rs->host.h_addr_list = rs->h_addr_ptrs;
622 h_errno = NETDB_SUCCESS;
623 return &rs->host;
624 }
625 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
626 break;
627 }
628
629#ifdef ANDROID_CHANGES
630 cache = __get_res_cache();
631 if (cache != NULL) {
632 hp = _resolv_cache_lookup( cache, name, af );
633 if (hp == _RESOLV_HOSTENT_NONE) {
634 h_errno = HOST_NOT_FOUND;
635 return NULL;
636 }
637 if (hp != NULL) {
638 h_errno = NETDB_SUCCESS;
639 return hp;
640 }
641 }
642#endif
643
644 hp = NULL;
645 h_errno = NETDB_INTERNAL;
646 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
647 default_dns_files, name, strlen(name), af) != NS_SUCCESS) {
648#ifdef ANDROID_CHANGES
649 /* cache negative DNS entry */
650 if (h_errno == HOST_NOT_FOUND && cache != NULL)
651 _resolv_cache_add( cache, name, af, _RESOLV_HOSTENT_NONE );
652#endif
653 return NULL;
654 }
655#ifdef ANDROID_CHANGES
656 if (cache != NULL) {
657 _resolv_cache_add( cache, name, af, hp );
658 }
659#endif
660 h_errno = NETDB_SUCCESS;
661 return hp;
662}
663
664struct hostent *
665gethostbyaddr(const char *addr, /* XXX should have been def'd as u_char! */
666 socklen_t len, int af)
667{
668 const u_char *uaddr = (const u_char *)addr;
669 socklen_t size;
670 struct hostent *hp;
671 static const ns_dtab dtab[] = {
672 NS_FILES_CB(_gethtbyaddr, NULL)
673 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
674 { 0, 0, 0 }
675 };
676
677 assert(addr != NULL);
678
679 if (af == AF_INET6 && len == IN6ADDRSZ &&
680 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) ||
681 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) {
682 h_errno = HOST_NOT_FOUND;
683 return NULL;
684 }
685 if (af == AF_INET6 && len == IN6ADDRSZ &&
686 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
687 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
688 /* Unmap. */
689 addr += IN6ADDRSZ - INADDRSZ;
690 uaddr += IN6ADDRSZ - INADDRSZ;
691 af = AF_INET;
692 len = INADDRSZ;
693 }
694 switch (af) {
695 case AF_INET:
696 size = INADDRSZ;
697 break;
698 case AF_INET6:
699 size = IN6ADDRSZ;
700 break;
701 default:
702 errno = EAFNOSUPPORT;
703 h_errno = NETDB_INTERNAL;
704 return NULL;
705 }
706 if (size != len) {
707 errno = EINVAL;
708 h_errno = NETDB_INTERNAL;
709 return NULL;
710 }
711 hp = NULL;
712 h_errno = NETDB_INTERNAL;
713 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
714 default_dns_files, uaddr, len, af) != NS_SUCCESS)
715 return NULL;
716 h_errno = NETDB_SUCCESS;
717 return hp;
718}
719
720void
721_sethtent(int f)
722{
723 res_static rs = __res_get_static();
724 if (rs == NULL) return;
725 if (!rs->hostf)
726 rs->hostf = fopen(_PATH_HOSTS, "r" );
727 else
728 rewind(rs->hostf);
729 rs->stayopen = f;
730}
731
732void
733_endhtent(void)
734{
735 res_static rs = __res_get_static();
736 if (rs == NULL) return;
737
738 if (rs->hostf && !rs->stayopen) {
739 (void) fclose(rs->hostf);
740 rs->hostf = NULL;
741 }
742}
743
744struct hostent *
745_gethtent(void)
746{
747 char *p;
748 char *cp, **q;
749 int af, len;
750 res_static rs = __res_get_static();
751
752 if (!rs->hostf && !(rs->hostf = fopen(_PATH_HOSTS, "r" ))) {
753 h_errno = NETDB_INTERNAL;
754 return NULL;
755 }
756 again:
757 if (!(p = fgets(rs->hostbuf, sizeof rs->hostbuf, rs->hostf))) {
758 h_errno = HOST_NOT_FOUND;
759 return NULL;
760 }
761 if (*p == '#')
762 goto again;
763 if (!(cp = strpbrk(p, "#\n")))
764 goto again;
765 *cp = '\0';
766 if (!(cp = strpbrk(p, " \t")))
767 goto again;
768 *cp++ = '\0';
769 if (inet_pton(AF_INET6, p, (char *)(void *)rs->host_addr) > 0) {
770 af = AF_INET6;
771 len = IN6ADDRSZ;
772 } else if (inet_pton(AF_INET, p, (char *)(void *)rs->host_addr) > 0) {
773 res_state res = __res_get_state();
774 if (res == NULL)
775 return NULL;
776 if (res->options & RES_USE_INET6) {
777 map_v4v6_address((char *)(void *)rs->host_addr,
778 (char *)(void *)rs->host_addr);
779 af = AF_INET6;
780 len = IN6ADDRSZ;
781 } else {
782 af = AF_INET;
783 len = INADDRSZ;
784 }
785 __res_put_state(res);
786 } else {
787 goto again;
788 }
789 /* if this is not something we're looking for, skip it. */
790 if (rs->host.h_addrtype != 0 && rs->host.h_addrtype != af)
791 goto again;
792 if (rs->host.h_length != 0 && rs->host.h_length != len)
793 goto again;
794 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
795 rs->h_addr_ptrs[1] = NULL;
796 rs->host.h_addr_list = rs->h_addr_ptrs;
797 rs->host.h_length = len;
798 rs->host.h_addrtype = af;
799 while (*cp == ' ' || *cp == '\t')
800 cp++;
801 rs->host.h_name = cp;
802 q = rs->host.h_aliases = rs->host_aliases;
803 if ((cp = strpbrk(cp, " \t")) != NULL)
804 *cp++ = '\0';
805 while (cp && *cp) {
806 if (*cp == ' ' || *cp == '\t') {
807 cp++;
808 continue;
809 }
810 if (q < &rs->host_aliases[MAXALIASES - 1])
811 *q++ = cp;
812 if ((cp = strpbrk(cp, " \t")) != NULL)
813 *cp++ = '\0';
814 }
815 *q = NULL;
816 h_errno = NETDB_SUCCESS;
817 return &rs->host;
818}
819
820/*ARGSUSED*/
821int
822_gethtbyname(void *rv, void *cb_data, va_list ap)
823{
824 struct hostent *hp;
825 const char *name;
826 int af;
827
828 assert(rv != NULL);
829
830 name = va_arg(ap, char *);
831 /* NOSTRICT skip len */(void)va_arg(ap, int);
832 af = va_arg(ap, int);
833
834 hp = NULL;
835#if 0
836 {
837 res_state res = __res_get_state();
838 if (res == NULL)
839 return NS_NOTFOUND;
840 if (res->options & RES_USE_INET6)
841 hp = _gethtbyname2(name, AF_INET6);
842 if (hp==NULL)
843 hp = _gethtbyname2(name, AF_INET);
844 __res_put_state(res);
845 }
846#else
847 hp = _gethtbyname2(name, af);
848#endif
849 *((struct hostent **)rv) = hp;
850 if (hp == NULL) {
851 h_errno = HOST_NOT_FOUND;
852 return NS_NOTFOUND;
853 }
854 return NS_SUCCESS;
855}
856
857struct hostent *
858_gethtbyname2(const char *name, int af)
859{
860 struct hostent *p;
861 char *tmpbuf, *ptr, **cp;
862 int num;
863 size_t len;
864 res_static rs = __res_get_static();
865
866 assert(name != NULL);
867
868 _sethtent(rs->stayopen);
869 ptr = tmpbuf = NULL;
870 num = 0;
871 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
872 if (p->h_addrtype != af)
873 continue;
874 if (strcasecmp(p->h_name, name) != 0) {
875 for (cp = p->h_aliases; *cp != NULL; cp++)
876 if (strcasecmp(*cp, name) == 0)
877 break;
878 if (*cp == NULL) continue;
879 }
880
881 if (num == 0) {
882 size_t bufsize;
883 char *src;
884
885 bufsize = strlen(p->h_name) + 2 +
886 MAXADDRS * p->h_length +
887 ALIGNBYTES;
888 for (cp = p->h_aliases; *cp != NULL; cp++)
889 bufsize += strlen(*cp) + 1;
890
891 if ((tmpbuf = malloc(bufsize)) == NULL) {
892 h_errno = NETDB_INTERNAL;
893 return NULL;
894 }
895
896 ptr = tmpbuf;
897 src = p->h_name;
898 while ((*ptr++ = *src++) != '\0');
899 for (cp = p->h_aliases; *cp != NULL; cp++) {
900 src = *cp;
901 while ((*ptr++ = *src++) != '\0');
902 }
903 *ptr++ = '\0';
904
905 ptr = (char *)(void *)ALIGN(ptr);
906 }
907
908 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
909 ptr += p->h_length;
910 num++;
911 }
912 _endhtent();
913 if (num == 0) return NULL;
914
915 len = ptr - tmpbuf;
916 if (len > (sizeof(rs->hostbuf) - ALIGNBYTES)) {
917 free(tmpbuf);
918 errno = ENOSPC;
919 h_errno = NETDB_INTERNAL;
920 return NULL;
921 }
922 ptr = memcpy((void *)ALIGN(rs->hostbuf), tmpbuf, len);
923 free(tmpbuf);
924
925 rs->host.h_name = ptr;
926 while (*ptr++);
927
928 cp = rs->host_aliases;
929 while (*ptr) {
930 *cp++ = ptr;
931 while (*ptr++);
932 }
933 ptr++;
934 *cp = NULL;
935
936 ptr = (char *)(void *)ALIGN(ptr);
937 cp = rs->h_addr_ptrs;
938 while (num--) {
939 *cp++ = ptr;
940 ptr += rs->host.h_length;
941 }
942 *cp = NULL;
943
944 return &rs->host;
945}
946
947/*ARGSUSED*/
948int
949_gethtbyaddr(void *rv, void *cb_data, va_list ap)
950{
951 struct hostent *p;
952 const unsigned char *addr;
953 int len, af;
954 res_static rs = __res_get_static();
955
956 assert(rv != NULL);
957
958 addr = va_arg(ap, unsigned char *);
959 len = va_arg(ap, int);
960 af = va_arg(ap, int);
961
962 rs->host.h_length = len;
963 rs->host.h_addrtype = af;
964
965 _sethtent(rs->stayopen);
966 while ((p = _gethtent()) != NULL)
967 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
968 (size_t)len))
969 break;
970 _endhtent();
971 *((struct hostent **)rv) = p;
972 if (p==NULL) {
973 h_errno = HOST_NOT_FOUND;
974 return NS_NOTFOUND;
975 }
976 return NS_SUCCESS;
977}
978
979static void
980map_v4v6_address(const char *src, char *dst)
981{
982 u_char *p = (u_char *)dst;
983 char tmp[INADDRSZ];
984 int i;
985
986 assert(src != NULL);
987 assert(dst != NULL);
988
989 /* Stash a temporary copy so our caller can update in place. */
990 (void)memcpy(tmp, src, INADDRSZ);
991 /* Mark this ipv6 addr as a mapped ipv4. */
992 for (i = 0; i < 10; i++)
993 *p++ = 0x00;
994 *p++ = 0xff;
995 *p++ = 0xff;
996 /* Retrieve the saved copy and we're done. */
997 (void)memcpy((void *)p, tmp, INADDRSZ);
998}
999
1000static void
1001map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1002{
1003 char **ap;
1004
1005 assert(hp != NULL);
1006 assert(bpp != NULL);
1007 assert(ep != NULL);
1008
1009 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1010 return;
1011 hp->h_addrtype = AF_INET6;
1012 hp->h_length = IN6ADDRSZ;
1013 for (ap = hp->h_addr_list; *ap; ap++) {
1014 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
1015
1016 if (ep - *bpp < (i + IN6ADDRSZ)) {
1017 /* Out of memory. Truncate address list here. XXX */
1018 *ap = NULL;
1019 return;
1020 }
1021 *bpp += i;
1022 map_v4v6_address(*ap, *bpp);
1023 *ap = *bpp;
1024 *bpp += IN6ADDRSZ;
1025 }
1026}
1027
1028static void
1029addrsort(char **ap, int num, res_state res)
1030{
1031 int i, j;
1032 char **p;
1033 short aval[MAXADDRS];
1034 int needsort = 0;
1035
1036 assert(ap != NULL);
1037
1038 p = ap;
1039 for (i = 0; i < num; i++, p++) {
1040 for (j = 0 ; (unsigned)j < res->nsort; j++)
1041 if (res->sort_list[j].addr.s_addr ==
1042 (((struct in_addr *)(void *)(*p))->s_addr &
1043 res->sort_list[j].mask))
1044 break;
1045 aval[i] = j;
1046 if (needsort == 0 && i > 0 && j < aval[i-1])
1047 needsort = i;
1048 }
1049 if (!needsort)
1050 return;
1051
1052 while (needsort < num) {
1053 for (j = needsort - 1; j >= 0; j--) {
1054 if (aval[j] > aval[j+1]) {
1055 char *hp;
1056
1057 i = aval[j];
1058 aval[j] = aval[j+1];
1059 aval[j+1] = i;
1060
1061 hp = ap[j];
1062 ap[j] = ap[j+1];
1063 ap[j+1] = hp;
1064 } else
1065 break;
1066 }
1067 needsort++;
1068 }
1069}
1070
1071struct hostent *
1072gethostent(void)
1073{
1074 res_static rs = __res_get_static();
1075 rs->host.h_addrtype = 0;
1076 rs->host.h_length = 0;
1077 return _gethtent();
1078}
1079
1080/*ARGSUSED*/
1081int
1082_dns_gethtbyname(void *rv, void *cb_data, va_list ap)
1083{
1084 querybuf *buf;
1085 int n, type;
1086 struct hostent *hp;
1087 const char *name;
1088 int af;
1089 res_state res;
1090
1091 assert(rv != NULL);
1092
1093 name = va_arg(ap, char *);
1094 /* NOSTRICT skip len */(void)va_arg(ap, int);
1095 af = va_arg(ap, int);
1096
1097 switch (af) {
1098 case AF_INET:
1099 type = T_A;
1100 break;
1101 case AF_INET6:
1102 type = T_AAAA;
1103 break;
1104 default:
1105 return NS_UNAVAIL;
1106 }
1107 buf = malloc(sizeof(*buf));
1108 if (buf == NULL) {
1109 h_errno = NETDB_INTERNAL;
1110 return NS_NOTFOUND;
1111 }
1112 res = __res_get_state();
1113 if (res == NULL) {
1114 free(buf);
1115 return NS_NOTFOUND;
1116 }
1117 n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf));
1118 if (n < 0) {
1119 free(buf);
1120 dprintf("res_nsearch failed (%d)\n", res, n);
1121 __res_put_state(res);
1122 return NS_NOTFOUND;
1123 }
1124 hp = getanswer(buf, n, name, type, res);
1125 free(buf);
1126 __res_put_state(res);
1127 if (hp == NULL)
1128 switch (h_errno) {
1129 case HOST_NOT_FOUND:
1130 return NS_NOTFOUND;
1131 case TRY_AGAIN:
1132 return NS_TRYAGAIN;
1133 default:
1134 return NS_UNAVAIL;
1135 }
1136 *((struct hostent **)rv) = hp;
1137 return NS_SUCCESS;
1138}
1139
1140/*ARGSUSED*/
1141int
1142_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1143{
1144 char qbuf[MAXDNAME + 1], *qp, *ep;
1145 int n;
1146 querybuf *buf;
1147 struct hostent *hp;
1148 const unsigned char *uaddr;
1149 int len, af, advance;
1150 res_state res;
1151 res_static rs = __res_get_static();
1152
1153 assert(rv != NULL);
1154
1155 uaddr = va_arg(ap, unsigned char *);
1156 len = va_arg(ap, int);
1157 af = va_arg(ap, int);
1158
1159 switch (af) {
1160 case AF_INET:
1161 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1162 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1163 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1164 break;
1165
1166 case AF_INET6:
1167 qp = qbuf;
1168 ep = qbuf + sizeof(qbuf) - 1;
1169 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1170 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1171 uaddr[n] & 0xf,
1172 ((unsigned int)uaddr[n] >> 4) & 0xf);
1173 if (advance > 0 && qp + advance < ep)
1174 qp += advance;
1175 else {
1176 h_errno = NETDB_INTERNAL;
1177 return NS_NOTFOUND;
1178 }
1179 }
1180 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1181 h_errno = NETDB_INTERNAL;
1182 return NS_NOTFOUND;
1183 }
1184 break;
1185 default:
1186 abort();
1187 }
1188
1189 buf = malloc(sizeof(*buf));
1190 if (buf == NULL) {
1191 h_errno = NETDB_INTERNAL;
1192 return NS_NOTFOUND;
1193 }
1194 res = __res_get_state();
1195 if (res == NULL) {
1196 free(buf);
1197 return NS_NOTFOUND;
1198 }
1199 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
1200 if (n < 0) {
1201 free(buf);
1202 dprintf("res_nquery failed (%d)\n", res, n);
1203 __res_put_state(res);
1204 return NS_NOTFOUND;
1205 }
1206 hp = getanswer(buf, n, qbuf, T_PTR, res);
1207 free(buf);
1208 if (hp == NULL) {
1209 __res_put_state(res);
1210 switch (h_errno) {
1211 case HOST_NOT_FOUND:
1212 return NS_NOTFOUND;
1213 case TRY_AGAIN:
1214 return NS_TRYAGAIN;
1215 default:
1216 return NS_UNAVAIL;
1217 }
1218 }
1219 hp->h_addrtype = af;
1220 hp->h_length = len;
1221 (void)memcpy(rs->host_addr, uaddr, (size_t)len);
1222 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
1223 rs->h_addr_ptrs[1] = NULL;
1224 if (af == AF_INET && (res->options & RES_USE_INET6)) {
1225 map_v4v6_address((char *)(void *)rs->host_addr,
1226 (char *)(void *)rs->host_addr);
1227 hp->h_addrtype = AF_INET6;
1228 hp->h_length = IN6ADDRSZ;
1229 }
1230
1231 __res_put_state(res);
1232 *((struct hostent **)rv) = hp;
1233 h_errno = NETDB_SUCCESS;
1234 return NS_SUCCESS;
1235}