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.6 2005/10/11 00:48:14 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;
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);
739 ep = pvt->hostbuf + sizeof(pvt->hostbuf);
740 cp = ansbuf + HFIXEDSZ;
742 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
745 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
746 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
747 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
752 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
755 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
756 /* res_nsend() has already verified that the query name is the
757 * same as the one we sent; this just gets the expanded name
758 * (i.e., with the succeeding search-domain tacked on).
760 n = strlen(bp) + 1; /* for the \0 */
761 if (n > MAXHOSTNAMELEN) {
762 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
765 pvt->host.h_name = bp;
768 /* The qname can be abbreviated, but hname is now absolute. */
769 qname = pvt->host.h_name;
771 ap = pvt->host_aliases;
773 pvt->host.h_aliases = pvt->host_aliases;
774 hap = pvt->h_addr_ptrs;
776 pvt->host.h_addr_list = pvt->h_addr_ptrs;
779 while (ancount-- > 0 && cp < eom && !had_error) {
780 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
781 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
786 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
788 cp += INT16SZ; /* type */
789 class = ns_get16(cp);
790 cp += INT16SZ + INT32SZ; /* class, TTL */
792 cp += INT16SZ; /* len */
799 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
802 int level = LOG_CRIT;
804 level |= LOG_SECURITY;
807 "gethostans: possible attempt to exploit buffer overflow while looking up %s",
808 *qname ? qname : ".");
810 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
811 if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
817 if (ap >= &pvt->host_aliases[MAXALIASES-1])
820 n = strlen(bp) + 1; /* for the \0 */
822 /* Get canonical name. */
823 n = strlen(tbuf) + 1; /* for the \0 */
824 if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
828 strcpy(bp, tbuf); /* (checked) */
829 pvt->host.h_name = bp;
834 if (qtype == T_PTR && type == T_CNAME) {
835 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
836 if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
842 if ((pvt->res->options & RES_USE_DNAME) != 0U)
846 * We may be able to check this regardless
847 * of the USE_DNAME bit, but we add the check
848 * for now since the DNAME support is
851 if (ns_samename(tname, bp) != 1)
854 /* Get canonical name. */
855 n = strlen(tbuf) + 1; /* for the \0 */
860 strcpy(bp, tbuf); /* (checked) */
865 if (qtype == T_ANY) {
866 if (!(type == T_A || type == T_AAAA)) {
870 } else if (type != qtype) {
876 if (ret_aip != NULL) {
877 /* addrinfo never needs T_PTR */
881 if (ns_samename(tname, bp) != 1) {
885 n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
886 if (n < 0 || !maybe_hnok(pvt->res, bp) ||
887 n >= MAXHOSTNAMELEN) {
893 pvt->host.h_name = bp;
896 else if (ap < &pvt->host_aliases[MAXALIASES-1])
901 n = strlen(bp) + 1; /* for the \0 */
907 if (ns_samename(hname, bp) != 1) {
911 if (type == T_A && n != INADDRSZ) {
915 if (type == T_AAAA && n != IN6ADDRSZ) {
920 /* make addrinfo. don't overwrite constant PAI */
922 ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
923 cur->ai_next = addr2addrinfo(
924 (const struct addrinfo *)&ai,
926 if (cur->ai_next == NULL)
932 nn = strlen(bp) + 1; /* for the \0 */
933 if (nn >= MAXHOSTNAMELEN) {
938 pvt->host.h_name = bp;
942 /* Ensure alignment. */
943 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
944 ~(sizeof(align) - 1));
945 /* Avoid overflows. */
946 if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) {
950 if (ret_aip) { /* need addrinfo. keep it. */
951 while (cur && cur->ai_next)
953 } else if (cur->ai_next) { /* need hostent */
954 struct addrinfo *aip = cur->ai_next;
956 for (aip = cur->ai_next; aip;
957 aip = aip->ai_next) {
960 m = add_hostent(pvt, bp, hap, aip);
967 if (hap < &pvt->h_addr_ptrs[MAXADDRS-1])
973 freeaddrinfo(cur->ai_next);
985 if (ret_aip == NULL) {
989 if (pvt->res->nsort && haveanswer > 1 && qtype == T_A)
990 addrsort(pvt->res, pvt->h_addr_ptrs,
992 if (pvt->host.h_name == NULL) {
993 n = strlen(qname) + 1; /* for the \0 */
994 if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
996 strcpy(bp, qname); /* (checked) */
997 pvt->host.h_name = bp;
1000 if (pvt->res->options & RES_USE_INET6)
1001 map_v4v6_hostent(&pvt->host, &bp, ep);
1002 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
1003 return (&pvt->host);
1005 if ((pai->ai_flags & AI_CANONNAME) != 0) {
1006 if (pvt->host.h_name == NULL) {
1007 sentinel.ai_next->ai_canonname =
1011 sentinel.ai_next->ai_canonname =
1012 strdup(pvt->host.h_name);
1015 *ret_aip = sentinel.ai_next;
1020 if (sentinel.ai_next) {
1021 /* this should be impossible, but check it for safety */
1022 freeaddrinfo(sentinel.ai_next);
1024 if (error == NETDB_SUCCESS)
1025 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
1027 RES_SET_H_ERRNO(pvt->res, error);
1032 add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
1039 switch(ai->ai_addr->sa_family) {
1041 addrlen = IN6ADDRSZ;
1042 addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1046 addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
1049 return(-1); /* abort? */
1052 /* Ensure alignment. */
1053 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
1054 ~(sizeof(align) - 1));
1055 /* Avoid overflows. */
1056 if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf])
1058 if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1])
1059 return(0); /* fail, but not treat it as an error. */
1061 /* Suppress duplicates. */
1062 for (tap = (const char **)pvt->h_addr_ptrs;
1065 if (memcmp(*tap, addrp, addrlen) == 0)
1070 memcpy(*hap = bp, addrp, addrlen);
1071 return((bp + addrlen) - obp);
1075 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
1078 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1080 hp->h_addrtype = AF_INET6;
1081 hp->h_length = IN6ADDRSZ;
1082 for (ap = hp->h_addr_list; *ap; ap++) {
1083 int i = (u_long)*bpp % sizeof(align);
1086 i = sizeof(align) - i;
1088 if ((ep - *bpp) < (i + IN6ADDRSZ)) {
1089 /* Out of memory. Truncate address list here. */
1094 map_v4v6_address(*ap, *bpp);
1101 addrsort(res_state statp, char **ap, int num) {
1102 int i, j, needsort = 0, aval[MAXADDRS];
1106 for (i = 0; i < num; i++, p++) {
1107 for (j = 0 ; (unsigned)j < statp->nsort; j++)
1108 if (statp->sort_list[j].addr.s_addr ==
1109 (((struct in_addr *)(*p))->s_addr &
1110 statp->sort_list[j].mask))
1113 if (needsort == 0 && i > 0 && j < aval[i-1])
1119 while (needsort < num) {
1120 for (j = needsort - 1; j >= 0; j--) {
1121 if (aval[j] > aval[j+1]) {
1125 aval[j] = aval[j+1];
1140 init(struct irs_ho *this) {
1141 struct pvt *pvt = (struct pvt *)this->private;
1143 if (!pvt->res && !ho_res_get(this))
1145 if (((pvt->res->options & RES_INIT) == 0U) &&
1146 res_ninit(pvt->res) == -1)