2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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.
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
35 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 /* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */
52 /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static const char rcsid[] = "$Id: dns_ho.c,v 1.14.18.8 2008/09/24 05:59:50 marka Exp $";
56 #endif /* LIBC_SCCS and not lint */
60 #include "port_before.h"
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
79 #include <isc/memcluster.h>
82 #include "port_after.h"
88 # define SPRINTF(x) strlen(sprintf/**/x)
90 # define SPRINTF(x) sprintf x
98 #define MAXPACKET (65535) /*%< Maximum TCP message size */
99 #define BOUNDS_CHECK(ptr, count) \
100 if ((ptr) + (count) > eom) { \
107 u_char buf[MAXPACKET];
110 struct dns_res_target {
111 struct dns_res_target *next;
112 querybuf qbuf; /*%< query buffer */
113 u_char *answer; /*%< buffer to put answer */
114 int anslen; /*%< size of answer buffer */
115 int qclass, qtype; /*%< class and type of query */
116 int action; /*%< condition whether query is really issued */
117 char qname[MAXDNAME +1]; /*%< domain name */
119 int n; /*%< result length */
122 enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
123 enum {RESQRY_SUCCESS, RESQRY_FAIL};
127 char * h_addr_ptrs[MAXADDRS + 1];
128 char * host_aliases[MAXALIASES];
129 char hostbuf[8*1024];
130 u_char host_addr[16]; /*%< IPv4 or IPv6 */
131 struct __res_state *res;
132 void (*free_res)(void *);
140 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
141 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
142 /* Note: the IPv6 loopback address is in the "tunnel" space */
143 static const u_char v6local[] = { 0,0, 0,1 }; /*%< last 4 bytes of IPv6 addr */
146 static void ho_close(struct irs_ho *this);
147 static struct hostent * ho_byname(struct irs_ho *this, const char *name);
148 static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
150 static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
152 static struct hostent * ho_next(struct irs_ho *this);
153 static void ho_rewind(struct irs_ho *this);
154 static void ho_minimize(struct irs_ho *this);
155 static struct __res_state * ho_res_get(struct irs_ho *this);
156 static void ho_res_set(struct irs_ho *this,
157 struct __res_state *res,
158 void (*free_res)(void *));
159 static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
160 const struct addrinfo *pai);
162 static void map_v4v6_hostent(struct hostent *hp, char **bp,
164 static void addrsort(res_state, char **, int);
165 static struct hostent * gethostans(struct irs_ho *this,
166 const u_char *ansbuf, int anslen,
167 const char *qname, int qtype,
169 struct addrinfo **ret_aip,
170 const struct addrinfo *pai);
171 static int add_hostent(struct pvt *pvt, char *bp, char **hap,
172 struct addrinfo *ai);
173 static int init(struct irs_ho *this);
178 irs_dns_ho(struct irs_acc *this) {
184 if (!(pvt = memget(sizeof *pvt))) {
188 memset(pvt, 0, sizeof *pvt);
190 if (!(ho = memget(sizeof *ho))) {
191 memput(pvt, sizeof *pvt);
195 memset(ho, 0x5e, sizeof *ho);
197 ho->close = ho_close;
198 ho->byname = ho_byname;
199 ho->byname2 = ho_byname2;
200 ho->byaddr = ho_byaddr;
202 ho->rewind = ho_rewind;
203 ho->minimize = ho_minimize;
204 ho->res_get = ho_res_get;
205 ho->res_set = ho_res_set;
206 ho->addrinfo = ho_addrinfo;
213 ho_close(struct irs_ho *this) {
214 struct pvt *pvt = (struct pvt *)this->private;
217 if (pvt->res && pvt->free_res)
218 (*pvt->free_res)(pvt->res);
219 memput(pvt, sizeof *pvt);
220 memput(this, sizeof *this);
223 static struct hostent *
224 ho_byname(struct irs_ho *this, const char *name) {
225 struct pvt *pvt = (struct pvt *)this->private;
228 if (init(this) == -1)
231 if (pvt->res->options & RES_USE_INET6) {
232 hp = ho_byname2(this, name, AF_INET6);
236 return (ho_byname2(this, name, AF_INET));
239 static struct hostent *
240 ho_byname2(struct irs_ho *this, const char *name, int af)
242 struct pvt *pvt = (struct pvt *)this->private;
243 struct hostent *hp = NULL;
245 char tmp[NS_MAXDNAME];
248 struct dns_res_target *q, *p;
249 int querystate = RESQRY_FAIL;
251 if (init(this) == -1)
254 q = memget(sizeof(*q));
256 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
260 memset(q, 0, sizeof(*q));
267 q->answer = q->qbuf.buf;
268 q->anslen = sizeof(q->qbuf);
269 q->action = RESTGT_DOALWAYS;
275 q->answer = q->qbuf.buf;
276 q->anslen = sizeof(q->qbuf);
277 q->action = RESTGT_DOALWAYS;
280 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
281 errno = EAFNOSUPPORT;
287 * if there aren't any dots, it could be a user-level alias.
288 * this is also done in res_nquery() since we are not the only
289 * function that looks up host names.
291 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
295 for (p = q; p; p = p->next) {
297 case RESTGT_DOALWAYS:
299 case RESTGT_AFTERFAILURE:
300 if (querystate == RESQRY_SUCCESS)
307 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
308 p->answer, p->anslen)) < 0) {
309 querystate = RESQRY_FAIL;
313 memset(&ai, 0, sizeof(ai));
315 if ((hp = gethostans(this, p->answer, n, name, p->qtype,
317 (const struct addrinfo *)&ai)) != NULL)
318 goto cleanup; /*%< no more loop is necessary */
319 querystate = RESQRY_FAIL;
325 memput(q, sizeof(*q));
329 static struct hostent *
330 ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
332 struct pvt *pvt = (struct pvt *)this->private;
333 const u_char *uaddr = addr;
335 struct hostent *hp = NULL;
337 struct dns_res_target *q, *q2, *p;
339 int querystate = RESQRY_FAIL;
341 if (init(this) == -1)
344 q = memget(sizeof(*q));
345 q2 = memget(sizeof(*q2));
346 if (q == NULL || q2 == NULL) {
347 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
351 memset(q, 0, sizeof(*q));
352 memset(q2, 0, sizeof(*q2));
354 if (af == AF_INET6 && len == IN6ADDRSZ &&
355 (!memcmp(uaddr, mapped, sizeof mapped) ||
356 (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
357 memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
359 addr = (const char *)addr + sizeof mapped;
360 uaddr += sizeof mapped;
369 q->answer = q->qbuf.buf;
370 q->anslen = sizeof(q->qbuf);
371 q->action = RESTGT_DOALWAYS;
377 q->answer = q->qbuf.buf;
378 q->anslen = sizeof(q->qbuf);
380 q->action = RESTGT_DOALWAYS;
383 q2->answer = q2->qbuf.buf;
384 q2->anslen = sizeof(q2->qbuf);
385 if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
386 q2->action = RESTGT_IGNORE;
388 q2->action = RESTGT_AFTERFAILURE;
391 errno = EAFNOSUPPORT;
392 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
398 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
405 (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
412 if (q->action != RESTGT_IGNORE) {
413 const char *nibsuff = res_get_nibblesuffix(pvt->res);
415 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
416 i = SPRINTF((qp, "%x.%x.",
418 (uaddr[n] >> 4) & 0xf));
423 if (strlen(q->qname) + strlen(nibsuff) + 1 >
425 errno = ENAMETOOLONG;
426 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
430 strcpy(qp, nibsuff); /* (checked) */
432 if (q2->action != RESTGT_IGNORE) {
433 const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
435 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
436 i = SPRINTF((qp, "%x.%x.",
438 (uaddr[n] >> 4) & 0xf));
443 if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
445 errno = ENAMETOOLONG;
446 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
450 strcpy(qp, nibsuff2); /* (checked) */
457 for (p = q; p; p = p->next) {
459 case RESTGT_DOALWAYS:
461 case RESTGT_AFTERFAILURE:
462 if (querystate == RESQRY_SUCCESS)
469 if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
470 p->answer, p->anslen)) < 0) {
471 querystate = RESQRY_FAIL;
475 memset(&ai, 0, sizeof(ai));
477 hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
478 NULL, (const struct addrinfo *)&ai);
480 querystate = RESQRY_FAIL;
484 memcpy(pvt->host_addr, addr, len);
485 pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
486 pvt->h_addr_ptrs[1] = NULL;
487 if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
488 map_v4v6_address((char*)pvt->host_addr,
489 (char*)pvt->host_addr);
490 pvt->host.h_addrtype = AF_INET6;
491 pvt->host.h_length = IN6ADDRSZ;
494 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
495 goto cleanup; /*%< no more loop is necessary. */
497 hp = NULL; /*%< H_ERRNO was set by subroutines */
500 memput(q, sizeof(*q));
502 memput(q2, sizeof(*q2));
506 static struct hostent *
507 ho_next(struct irs_ho *this) {
515 ho_rewind(struct irs_ho *this) {
523 ho_minimize(struct irs_ho *this) {
524 struct pvt *pvt = (struct pvt *)this->private;
527 res_nclose(pvt->res);
530 static struct __res_state *
531 ho_res_get(struct irs_ho *this) {
532 struct pvt *pvt = (struct pvt *)this->private;
535 struct __res_state *res;
536 res = (struct __res_state *)malloc(sizeof *res);
541 memset(res, 0, sizeof *res);
542 ho_res_set(this, res, free);
549 extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
552 static struct addrinfo *
553 ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
555 struct pvt *pvt = (struct pvt *)this->private;
557 char tmp[NS_MAXDNAME];
559 struct dns_res_target *q, *q2, *p;
560 struct addrinfo sentinel, *cur;
561 int querystate = RESQRY_FAIL;
563 if (init(this) == -1)
566 memset(&sentinel, 0, sizeof(sentinel));
569 q = memget(sizeof(*q));
570 q2 = memget(sizeof(*q2));
571 if (q == NULL || q2 == NULL) {
572 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
576 memset(q, 0, sizeof(*q2));
577 memset(q2, 0, sizeof(*q2));
579 switch (pai->ai_family) {
584 q->answer = q->qbuf.buf;
585 q->anslen = sizeof(q->qbuf);
587 q->action = RESTGT_DOALWAYS;
590 q2->answer = q2->qbuf.buf;
591 q2->anslen = sizeof(q2->qbuf);
592 q2->action = RESTGT_DOALWAYS;
597 q->answer = q->qbuf.buf;
598 q->anslen = sizeof(q->qbuf);
599 q->action = RESTGT_DOALWAYS;
604 q->answer = q->qbuf.buf;
605 q->anslen = sizeof(q->qbuf);
606 q->action = RESTGT_DOALWAYS;
609 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< better error? */
614 * if there aren't any dots, it could be a user-level alias.
615 * this is also done in res_nquery() since we are not the only
616 * function that looks up host names.
618 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
622 for (p = q; p; p = p->next) {
626 case RESTGT_DOALWAYS:
628 case RESTGT_AFTERFAILURE:
629 if (querystate == RESQRY_SUCCESS)
636 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
637 p->answer, p->anslen)) < 0) {
638 querystate = RESQRY_FAIL;
641 (void)gethostans(this, p->answer, n, name, p->qtype,
642 pai->ai_family, /*%< XXX: meaningless */
645 querystate = RESQRY_SUCCESS;
650 querystate = RESQRY_FAIL;
655 memput(q, sizeof(*q));
657 memput(q2, sizeof(*q2));
658 return(sentinel.ai_next);
662 ho_res_set(struct irs_ho *this, struct __res_state *res,
663 void (*free_res)(void *)) {
664 struct pvt *pvt = (struct pvt *)this->private;
666 if (pvt->res && pvt->free_res) {
667 res_nclose(pvt->res);
668 (*pvt->free_res)(pvt->res);
672 pvt->free_res = free_res;
677 static struct hostent *
678 gethostans(struct irs_ho *this,
679 const u_char *ansbuf, int anslen, const char *qname, int qtype,
680 int af, int size, /*!< meaningless for addrinfo cases */
681 struct addrinfo **ret_aip, const struct addrinfo *pai)
683 struct pvt *pvt = (struct pvt *)this->private;
684 int type, class, ancount, qdcount, n, haveanswer, had_error;
685 int error = NETDB_SUCCESS;
686 int (*name_ok)(const char *);
693 char *bp, *ep, **ap, **hap;
694 char tbuf[MAXDNAME+1];
695 struct addrinfo sentinel, *cur, ai;
697 if (pai == NULL) abort();
700 memset(&sentinel, 0, sizeof(sentinel));
704 eom = ansbuf + anslen;
708 case T_ANY: /*%< use T_ANY only for T_A/T_AAAA lookup */
718 pvt->host.h_addrtype = af;
719 pvt->host.h_length = size;
720 hname = pvt->host.h_name = NULL;
723 * Find first satisfactory answer.
725 if (ansbuf + HFIXEDSZ > eom) {
726 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
729 hp = (const HEADER *)ansbuf;
730 ancount = ntohs(hp->ancount);
731 qdcount = ntohs(hp->qdcount);
733 ep = pvt->hostbuf + sizeof(pvt->hostbuf);
734 cp = ansbuf + HFIXEDSZ;
736 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
739 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
740 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
741 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
746 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
749 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
750 /* res_nsend() has already verified that the query name is the
751 * same as the one we sent; this just gets the expanded name
752 * (i.e., with the succeeding search-domain tacked on).
754 n = strlen(bp) + 1; /*%< for the \\0 */
755 if (n > MAXHOSTNAMELEN) {
756 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
759 pvt->host.h_name = bp;
762 /* The qname can be abbreviated, but hname is now absolute. */
763 qname = pvt->host.h_name;
765 ap = pvt->host_aliases;
767 pvt->host.h_aliases = pvt->host_aliases;
768 hap = pvt->h_addr_ptrs;
770 pvt->host.h_addr_list = pvt->h_addr_ptrs;
773 while (ancount-- > 0 && cp < eom && !had_error) {
774 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
775 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
779 cp += n; /*%< name */
780 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
782 cp += INT16SZ; /*%< type */
783 class = ns_get16(cp);
784 cp += INT16SZ + INT32SZ; /*%< class, TTL */
786 cp += INT16SZ; /*%< len */
793 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
796 int level = LOG_CRIT;
798 level |= LOG_SECURITY;
801 "gethostans: possible attempt to exploit buffer overflow while looking up %s",
802 *qname ? qname : ".");
804 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
805 if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
811 if (ap >= &pvt->host_aliases[MAXALIASES-1])
814 n = strlen(bp) + 1; /*%< for the \\0 */
816 /* Get canonical name. */
817 n = strlen(tbuf) + 1; /*%< for the \\0 */
818 if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
822 strcpy(bp, tbuf); /* (checked) */
823 pvt->host.h_name = bp;
828 if (qtype == T_PTR && type == T_CNAME) {
829 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
830 if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
836 if ((pvt->res->options & RES_USE_DNAME) != 0U)
840 * We may be able to check this regardless
841 * of the USE_DNAME bit, but we add the check
842 * for now since the DNAME support is
845 if (ns_samename(tname, bp) != 1)
848 /* Get canonical name. */
849 n = strlen(tbuf) + 1; /*%< for the \\0 */
854 strcpy(bp, tbuf); /* (checked) */
859 if (qtype == T_ANY) {
860 if (!(type == T_A || type == T_AAAA)) {
864 } else if (type != qtype) {
870 if (ret_aip != NULL) {
871 /* addrinfo never needs T_PTR */
875 if (ns_samename(tname, bp) != 1) {
879 n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
880 if (n < 0 || !maybe_hnok(pvt->res, bp) ||
881 n >= MAXHOSTNAMELEN) {
887 pvt->host.h_name = bp;
890 else if (ap < &pvt->host_aliases[MAXALIASES-1])
895 n = strlen(bp) + 1; /*%< for the \\0 */
901 if (ns_samename(hname, bp) != 1) {
905 if (type == T_A && n != INADDRSZ) {
909 if (type == T_AAAA && n != IN6ADDRSZ) {
914 /* make addrinfo. don't overwrite constant PAI */
916 ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
917 cur->ai_next = addr2addrinfo(
918 (const struct addrinfo *)&ai,
920 if (cur->ai_next == NULL)
926 nn = strlen(bp) + 1; /*%< for the \\0 */
927 if (nn >= MAXHOSTNAMELEN) {
932 pvt->host.h_name = bp;
936 /* Ensure alignment. */
937 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
938 ~(sizeof(align) - 1));
939 /* Avoid overflows. */
940 if (bp + n > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) {
944 if (ret_aip) { /*%< need addrinfo. keep it. */
947 } else if (cur->ai_next) { /*%< need hostent */
948 struct addrinfo *aip = cur->ai_next;
950 for (aip = cur->ai_next; aip;
951 aip = aip->ai_next) {
954 m = add_hostent(pvt, bp, hap, aip);
961 if (hap < &pvt->h_addr_ptrs[MAXADDRS])
967 freeaddrinfo(cur->ai_next);
979 if (ret_aip == NULL) {
983 if (pvt->res->nsort && hap != pvt->h_addr_ptrs &&
985 addrsort(pvt->res, pvt->h_addr_ptrs,
986 hap - pvt->h_addr_ptrs);
987 if (pvt->host.h_name == NULL) {
988 n = strlen(qname) + 1; /*%< for the \\0 */
989 if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
991 strcpy(bp, qname); /* (checked) */
992 pvt->host.h_name = bp;
995 if (pvt->res->options & RES_USE_INET6)
996 map_v4v6_hostent(&pvt->host, &bp, ep);
997 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
1000 if ((pai->ai_flags & AI_CANONNAME) != 0) {
1001 if (pvt->host.h_name == NULL) {
1002 sentinel.ai_next->ai_canonname =
1006 sentinel.ai_next->ai_canonname =
1007 strdup(pvt->host.h_name);
1010 *ret_aip = sentinel.ai_next;
1015 if (sentinel.ai_next) {
1016 /* this should be impossible, but check it for safety */
1017 freeaddrinfo(sentinel.ai_next);
1019 if (error == NETDB_SUCCESS)
1020 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
1022 RES_SET_H_ERRNO(pvt->res, error);
1027 add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
1034 switch(ai->ai_addr->sa_family) {
1036 addrlen = IN6ADDRSZ;
1037 addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1041 addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
1044 return(-1); /*%< abort? */
1047 /* Ensure alignment. */
1048 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
1049 ~(sizeof(align) - 1));
1050 /* Avoid overflows. */
1051 if (bp + addrlen > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1])
1053 if (hap >= &pvt->h_addr_ptrs[MAXADDRS])
1054 return(0); /*%< fail, but not treat it as an error. */
1055 /* Suppress duplicates. */
1056 for (tap = (const char **)pvt->h_addr_ptrs;
1059 if (memcmp(*tap, addrp, addrlen) == 0)
1064 memcpy(*hap = bp, addrp, addrlen);
1065 return((bp + addrlen) - obp);
1069 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
1072 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1074 hp->h_addrtype = AF_INET6;
1075 hp->h_length = IN6ADDRSZ;
1076 for (ap = hp->h_addr_list; *ap; ap++) {
1077 int i = (u_long)*bpp % sizeof(align);
1080 i = sizeof(align) - i;
1082 if ((ep - *bpp) < (i + IN6ADDRSZ)) {
1083 /* Out of memory. Truncate address list here. */
1088 map_v4v6_address(*ap, *bpp);
1095 addrsort(res_state statp, char **ap, int num) {
1096 int i, j, needsort = 0, aval[MAXADDRS];
1100 for (i = 0; i < num; i++, p++) {
1101 for (j = 0 ; (unsigned)j < statp->nsort; j++)
1102 if (statp->sort_list[j].addr.s_addr ==
1103 (((struct in_addr *)(*p))->s_addr &
1104 statp->sort_list[j].mask))
1107 if (needsort == 0 && i > 0 && j < aval[i-1])
1113 while (needsort < num) {
1114 for (j = needsort - 1; j >= 0; j--) {
1115 if (aval[j] > aval[j+1]) {
1119 aval[j] = aval[j+1];
1134 init(struct irs_ho *this) {
1135 struct pvt *pvt = (struct pvt *)this->private;
1137 if (!pvt->res && !ho_res_get(this))
1139 if (((pvt->res->options & RES_INIT) == 0U) &&
1140 res_ninit(pvt->res) == -1)