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.5.2.7.4.5 2004/08/24 00:32:15 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 */
100 #define BOUNDS_CHECK(ptr, count) \
101 if ((ptr) + (count) > eom) { \
108 u_char buf[MAXPACKET];
111 struct dns_res_target {
112 struct dns_res_target *next;
113 querybuf qbuf; /* query buffer */
114 u_char *answer; /* buffer to put answer */
115 int anslen; /* size of answer buffer */
116 int qclass, qtype; /* class and type of query */
117 int action; /* condition whether query is really issued */
118 char qname[MAXDNAME +1]; /* domain name */
120 int n; /* result length */
123 enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
124 enum {RESQRY_SUCCESS, RESQRY_FAIL};
128 char * h_addr_ptrs[MAXADDRS + 1];
129 char * host_aliases[MAXALIASES];
130 char hostbuf[8*1024];
131 u_char host_addr[16]; /* IPv4 or IPv6 */
132 struct __res_state *res;
133 void (*free_res)(void *);
141 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
142 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
143 /* Note: the IPv6 loopback address is in the "tunnel" space */
144 static const u_char v6local[] = { 0,0, 0,1 }; /* last 4 bytes of IPv6 addr */
148 static void ho_close(struct irs_ho *this);
149 static struct hostent * ho_byname(struct irs_ho *this, const char *name);
150 static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
152 static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
154 static struct hostent * ho_next(struct irs_ho *this);
155 static void ho_rewind(struct irs_ho *this);
156 static void ho_minimize(struct irs_ho *this);
157 static struct __res_state * ho_res_get(struct irs_ho *this);
158 static void ho_res_set(struct irs_ho *this,
159 struct __res_state *res,
160 void (*free_res)(void *));
161 static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
162 const struct addrinfo *pai);
164 static void map_v4v6_hostent(struct hostent *hp, char **bp,
166 static void addrsort(res_state, char **, int);
167 static struct hostent * gethostans(struct irs_ho *this,
168 const u_char *ansbuf, int anslen,
169 const char *qname, int qtype,
171 struct addrinfo **ret_aip,
172 const struct addrinfo *pai);
173 static int add_hostent(struct pvt *pvt, char *bp, char **hap,
174 struct addrinfo *ai);
175 static int init(struct irs_ho *this);
180 irs_dns_ho(struct irs_acc *this) {
186 if (!(pvt = memget(sizeof *pvt))) {
190 memset(pvt, 0, sizeof *pvt);
192 if (!(ho = memget(sizeof *ho))) {
193 memput(pvt, sizeof *pvt);
197 memset(ho, 0x5e, sizeof *ho);
199 ho->close = ho_close;
200 ho->byname = ho_byname;
201 ho->byname2 = ho_byname2;
202 ho->byaddr = ho_byaddr;
204 ho->rewind = ho_rewind;
205 ho->minimize = ho_minimize;
206 ho->res_get = ho_res_get;
207 ho->res_set = ho_res_set;
208 ho->addrinfo = ho_addrinfo;
215 ho_close(struct irs_ho *this) {
216 struct pvt *pvt = (struct pvt *)this->private;
219 if (pvt->res && pvt->free_res)
220 (*pvt->free_res)(pvt->res);
222 memput(pvt, sizeof *pvt);
223 memput(this, sizeof *this);
226 static struct hostent *
227 ho_byname(struct irs_ho *this, const char *name) {
228 struct pvt *pvt = (struct pvt *)this->private;
231 if (init(this) == -1)
234 if (pvt->res->options & RES_USE_INET6) {
235 hp = ho_byname2(this, name, AF_INET6);
239 return (ho_byname2(this, name, AF_INET));
242 static struct hostent *
243 ho_byname2(struct irs_ho *this, const char *name, int af)
245 struct pvt *pvt = (struct pvt *)this->private;
246 struct hostent *hp = NULL;
248 char tmp[NS_MAXDNAME];
251 struct dns_res_target *q, *p;
252 int querystate = RESQRY_FAIL;
254 if (init(this) == -1)
257 q = memget(sizeof(*q));
259 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
263 memset(q, 0, sizeof(q));
270 q->answer = q->qbuf.buf;
271 q->anslen = sizeof(q->qbuf);
272 q->action = RESTGT_DOALWAYS;
278 q->answer = q->qbuf.buf;
279 q->anslen = sizeof(q->qbuf);
280 q->action = RESTGT_DOALWAYS;
283 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
284 errno = EAFNOSUPPORT;
290 * if there aren't any dots, it could be a user-level alias.
291 * this is also done in res_nquery() since we are not the only
292 * function that looks up host names.
294 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
298 for (p = q; p; p = p->next) {
300 case RESTGT_DOALWAYS:
302 case RESTGT_AFTERFAILURE:
303 if (querystate == RESQRY_SUCCESS)
310 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
311 p->answer, p->anslen)) < 0) {
312 querystate = RESQRY_FAIL;
316 memset(&ai, 0, sizeof(ai));
318 if ((hp = gethostans(this, p->answer, n, name, p->qtype,
320 (const struct addrinfo *)&ai)) != NULL)
321 goto cleanup; /* no more loop is necessary */
323 querystate = RESQRY_FAIL;
329 memput(q, sizeof(*q));
333 static struct hostent *
334 ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
336 struct pvt *pvt = (struct pvt *)this->private;
337 const u_char *uaddr = addr;
339 struct hostent *hp = NULL;
341 struct dns_res_target *q, *q2, *p;
343 int querystate = RESQRY_FAIL;
345 if (init(this) == -1)
348 q = memget(sizeof(*q));
349 q2 = memget(sizeof(*q2));
350 if (q == NULL || q2 == NULL) {
351 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
355 memset(q, 0, sizeof(q));
356 memset(q2, 0, sizeof(q2));
358 if (af == AF_INET6 && len == IN6ADDRSZ &&
359 (!memcmp(uaddr, mapped, sizeof mapped) ||
360 (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
361 memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
363 addr = (const char *)addr + sizeof mapped;
364 uaddr += sizeof mapped;
373 q->answer = q->qbuf.buf;
374 q->anslen = sizeof(q->qbuf);
375 q->action = RESTGT_DOALWAYS;
381 q->answer = q->qbuf.buf;
382 q->anslen = sizeof(q->qbuf);
384 q->action = RESTGT_DOALWAYS;
387 q2->answer = q2->qbuf.buf;
388 q2->anslen = sizeof(q2->qbuf);
389 if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
390 q2->action = RESTGT_IGNORE;
392 q2->action = RESTGT_AFTERFAILURE;
395 errno = EAFNOSUPPORT;
396 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
402 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
409 (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
416 if (q->action != RESTGT_IGNORE) {
417 const char *nibsuff = res_get_nibblesuffix(pvt->res);
419 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
420 i = SPRINTF((qp, "%x.%x.",
422 (uaddr[n] >> 4) & 0xf));
427 if (strlen(q->qname) + strlen(nibsuff) + 1 >
429 errno = ENAMETOOLONG;
430 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
434 strcpy(qp, nibsuff); /* (checked) */
436 if (q2->action != RESTGT_IGNORE) {
437 const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
439 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
440 i = SPRINTF((qp, "%x.%x.",
442 (uaddr[n] >> 4) & 0xf));
447 if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
449 errno = ENAMETOOLONG;
450 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
454 strcpy(qp, nibsuff2); /* (checked) */
461 for (p = q; p; p = p->next) {
463 case RESTGT_DOALWAYS:
465 case RESTGT_AFTERFAILURE:
466 if (querystate == RESQRY_SUCCESS)
473 if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
474 p->answer, p->anslen)) < 0) {
475 querystate = RESQRY_FAIL;
479 memset(&ai, 0, sizeof(ai));
481 hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
482 NULL, (const struct addrinfo *)&ai);
484 querystate = RESQRY_FAIL;
488 memcpy(pvt->host_addr, addr, len);
489 pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
490 pvt->h_addr_ptrs[1] = NULL;
491 if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
492 map_v4v6_address((char*)pvt->host_addr,
493 (char*)pvt->host_addr);
494 pvt->host.h_addrtype = AF_INET6;
495 pvt->host.h_length = IN6ADDRSZ;
498 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
499 goto cleanup; /* no more loop is necessary. */
501 hp = NULL; /* H_ERRNO was set by subroutines */
505 memput(q, sizeof(*q));
507 memput(q2, sizeof(*q2));
511 static struct hostent *
512 ho_next(struct irs_ho *this) {
520 ho_rewind(struct irs_ho *this) {
528 ho_minimize(struct irs_ho *this) {
529 struct pvt *pvt = (struct pvt *)this->private;
532 res_nclose(pvt->res);
535 static struct __res_state *
536 ho_res_get(struct irs_ho *this) {
537 struct pvt *pvt = (struct pvt *)this->private;
540 struct __res_state *res;
541 res = (struct __res_state *)malloc(sizeof *res);
546 memset(res, 0, sizeof *res);
547 ho_res_set(this, res, free);
554 extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
557 static struct addrinfo *
558 ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
560 struct pvt *pvt = (struct pvt *)this->private;
562 char tmp[NS_MAXDNAME];
564 struct dns_res_target *q, *q2, *p;
565 struct addrinfo sentinel, *cur;
566 int querystate = RESQRY_FAIL;
568 if (init(this) == -1)
571 memset(&sentinel, 0, sizeof(sentinel));
574 q = memget(sizeof(*q));
575 q2 = memget(sizeof(*q2));
576 if (q == NULL || q2 == NULL) {
577 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
581 memset(q, 0, sizeof(q2));
582 memset(q2, 0, sizeof(q2));
584 switch (pai->ai_family) {
589 q->answer = q->qbuf.buf;
590 q->anslen = sizeof(q->qbuf);
592 q->action = RESTGT_DOALWAYS;
595 q2->answer = q2->qbuf.buf;
596 q2->anslen = sizeof(q2->qbuf);
597 q2->action = RESTGT_DOALWAYS;
602 q->answer = q->qbuf.buf;
603 q->anslen = sizeof(q->qbuf);
604 q->action = RESTGT_DOALWAYS;
609 q->answer = q->qbuf.buf;
610 q->anslen = sizeof(q->qbuf);
611 q->action = RESTGT_DOALWAYS;
614 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /* better error? */
619 * if there aren't any dots, it could be a user-level alias.
620 * this is also done in res_nquery() since we are not the only
621 * function that looks up host names.
623 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
627 for (p = q; p; p = p->next) {
631 case RESTGT_DOALWAYS:
633 case RESTGT_AFTERFAILURE:
634 if (querystate == RESQRY_SUCCESS)
641 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
642 p->answer, p->anslen)) < 0) {
643 querystate = RESQRY_FAIL;
646 (void)gethostans(this, p->answer, n, name, p->qtype,
647 pai->ai_family, /* XXX: meaningless */
650 querystate = RESQRY_SUCCESS;
652 while (cur && cur->ai_next)
656 querystate = RESQRY_FAIL;
661 memput(q, sizeof(*q));
663 memput(q2, sizeof(*q2));
664 return(sentinel.ai_next);
668 ho_res_set(struct irs_ho *this, struct __res_state *res,
669 void (*free_res)(void *)) {
670 struct pvt *pvt = (struct pvt *)this->private;
672 if (pvt->res && pvt->free_res) {
673 res_nclose(pvt->res);
674 (*pvt->free_res)(pvt->res);
678 pvt->free_res = free_res;
683 static struct hostent *
684 gethostans(struct irs_ho *this,
685 const u_char *ansbuf, int anslen, const char *qname, int qtype,
686 int af, int size, /* meaningless for addrinfo cases */
687 struct addrinfo **ret_aip, const struct addrinfo *pai)
689 struct pvt *pvt = (struct pvt *)this->private;
690 int type, class, ancount, qdcount, n, haveanswer, had_error;
691 int error = NETDB_SUCCESS, arcount;
692 int (*name_ok)(const char *);
699 char *bp, *ep, **ap, **hap;
700 char tbuf[MAXDNAME+1];
701 struct addrinfo sentinel, *cur, ai;
703 if (pai == NULL) abort();
706 memset(&sentinel, 0, sizeof(sentinel));
710 eom = ansbuf + anslen;
714 case T_ANY: /* use T_ANY only for T_A/T_AAAA lookup */
724 pvt->host.h_addrtype = af;
725 pvt->host.h_length = size;
726 hname = pvt->host.h_name = NULL;
729 * Find first satisfactory answer.
731 if (ansbuf + HFIXEDSZ > eom) {
732 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
735 hp = (const HEADER *)ansbuf;
736 ancount = ntohs(hp->ancount);
737 qdcount = ntohs(hp->qdcount);
738 arcount = ntohs(hp->arcount);
740 ep = pvt->hostbuf + sizeof(pvt->hostbuf);
741 cp = ansbuf + HFIXEDSZ;
743 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
746 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
747 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
748 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
753 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
756 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
757 /* res_nsend() has already verified that the query name is the
758 * same as the one we sent; this just gets the expanded name
759 * (i.e., with the succeeding search-domain tacked on).
761 n = strlen(bp) + 1; /* for the \0 */
762 if (n > MAXHOSTNAMELEN) {
763 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
766 pvt->host.h_name = bp;
769 /* The qname can be abbreviated, but hname is now absolute. */
770 qname = pvt->host.h_name;
772 ap = pvt->host_aliases;
774 pvt->host.h_aliases = pvt->host_aliases;
775 hap = pvt->h_addr_ptrs;
777 pvt->host.h_addr_list = pvt->h_addr_ptrs;
780 while (ancount-- > 0 && cp < eom && !had_error) {
781 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
782 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
787 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
789 cp += INT16SZ; /* type */
790 class = ns_get16(cp);
791 cp += INT16SZ + INT32SZ; /* class, TTL */
793 cp += INT16SZ; /* len */
800 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
803 int level = LOG_CRIT;
805 level |= LOG_SECURITY;
808 "gethostans: possible attempt to exploit buffer overflow while looking up %s",
809 *qname ? qname : ".");
811 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
812 if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
818 if (ap >= &pvt->host_aliases[MAXALIASES-1])
821 n = strlen(bp) + 1; /* for the \0 */
823 /* Get canonical name. */
824 n = strlen(tbuf) + 1; /* for the \0 */
825 if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
829 strcpy(bp, tbuf); /* (checked) */
830 pvt->host.h_name = bp;
835 if (qtype == T_PTR && type == T_CNAME) {
836 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
837 if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
843 if ((pvt->res->options & RES_USE_DNAME) != 0U)
847 * We may be able to check this regardless
848 * of the USE_DNAME bit, but we add the check
849 * for now since the DNAME support is
852 if (ns_samename(tname, bp) != 1)
855 /* Get canonical name. */
856 n = strlen(tbuf) + 1; /* for the \0 */
861 strcpy(bp, tbuf); /* (checked) */
866 if (qtype == T_ANY) {
867 if (!(type == T_A || type == T_AAAA)) {
871 } else if (type != qtype) {
877 if (ret_aip != NULL) {
878 /* addrinfo never needs T_PTR */
882 if (ns_samename(tname, bp) != 1) {
886 n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
887 if (n < 0 || !maybe_hnok(pvt->res, bp) ||
888 n >= MAXHOSTNAMELEN) {
894 pvt->host.h_name = bp;
897 else if (ap < &pvt->host_aliases[MAXALIASES-1])
902 n = strlen(bp) + 1; /* for the \0 */
908 if (ns_samename(hname, bp) != 1) {
912 if (type == T_A && n != INADDRSZ) {
916 if (type == T_AAAA && n != IN6ADDRSZ) {
921 /* make addrinfo. don't overwrite constant PAI */
923 ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
924 cur->ai_next = addr2addrinfo(
925 (const struct addrinfo *)&ai,
927 if (cur->ai_next == NULL)
933 nn = strlen(bp) + 1; /* for the \0 */
934 if (nn >= MAXHOSTNAMELEN) {
939 pvt->host.h_name = bp;
943 /* Ensure alignment. */
944 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
945 ~(sizeof(align) - 1));
946 /* Avoid overflows. */
947 if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) {
951 if (ret_aip) { /* need addrinfo. keep it. */
952 while (cur && cur->ai_next)
954 } else if (cur->ai_next) { /* need hostent */
955 struct addrinfo *aip = cur->ai_next;
957 for (aip = cur->ai_next; aip;
958 aip = aip->ai_next) {
961 m = add_hostent(pvt, bp, hap, aip);
968 if (hap < &pvt->h_addr_ptrs[MAXADDRS-1])
974 freeaddrinfo(cur->ai_next);
986 if (ret_aip == NULL) {
990 if (pvt->res->nsort && haveanswer > 1 && qtype == T_A)
991 addrsort(pvt->res, pvt->h_addr_ptrs,
993 if (pvt->host.h_name == NULL) {
994 n = strlen(qname) + 1; /* for the \0 */
995 if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
997 strcpy(bp, qname); /* (checked) */
998 pvt->host.h_name = bp;
1001 if (pvt->res->options & RES_USE_INET6)
1002 map_v4v6_hostent(&pvt->host, &bp, ep);
1003 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
1004 return (&pvt->host);
1006 if ((pai->ai_flags & AI_CANONNAME) != 0) {
1007 if (pvt->host.h_name == NULL) {
1008 sentinel.ai_next->ai_canonname =
1012 sentinel.ai_next->ai_canonname =
1013 strdup(pvt->host.h_name);
1016 *ret_aip = sentinel.ai_next;
1021 if (sentinel.ai_next) {
1022 /* this should be impossible, but check it for safety */
1023 freeaddrinfo(sentinel.ai_next);
1025 if (error == NETDB_SUCCESS)
1026 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
1028 RES_SET_H_ERRNO(pvt->res, error);
1033 add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
1040 switch(ai->ai_addr->sa_family) {
1042 addrlen = IN6ADDRSZ;
1043 addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1047 addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
1050 return(-1); /* abort? */
1053 /* Ensure alignment. */
1054 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
1055 ~(sizeof(align) - 1));
1056 /* Avoid overflows. */
1057 if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf])
1059 if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1])
1060 return(0); /* fail, but not treat it as an error. */
1062 /* Suppress duplicates. */
1063 for (tap = (const char **)pvt->h_addr_ptrs;
1066 if (memcmp(*tap, addrp, addrlen) == 0)
1071 memcpy(*hap = bp, addrp, addrlen);
1072 return((bp + addrlen) - obp);
1076 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
1079 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1081 hp->h_addrtype = AF_INET6;
1082 hp->h_length = IN6ADDRSZ;
1083 for (ap = hp->h_addr_list; *ap; ap++) {
1084 int i = (u_long)*bpp % sizeof(align);
1087 i = sizeof(align) - i;
1089 if ((ep - *bpp) < (i + IN6ADDRSZ)) {
1090 /* Out of memory. Truncate address list here. */
1095 map_v4v6_address(*ap, *bpp);
1102 addrsort(res_state statp, char **ap, int num) {
1103 int i, j, needsort = 0, aval[MAXADDRS];
1107 for (i = 0; i < num; i++, p++) {
1108 for (j = 0 ; (unsigned)j < statp->nsort; j++)
1109 if (statp->sort_list[j].addr.s_addr ==
1110 (((struct in_addr *)(*p))->s_addr &
1111 statp->sort_list[j].mask))
1114 if (needsort == 0 && i > 0 && j < aval[i-1])
1120 while (needsort < num) {
1121 for (j = needsort - 1; j >= 0; j--) {
1122 if (aval[j] > aval[j+1]) {
1126 aval[j] = aval[j+1];
1141 init(struct irs_ho *this) {
1142 struct pvt *pvt = (struct pvt *)this->private;
1144 if (!pvt->res && !ho_res_get(this))
1146 if (((pvt->res->options & RES_INIT) == 0U) &&
1147 res_ninit(pvt->res) == -1)