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.8 2006/03/10 00:17:21 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);
221 memput(pvt, sizeof *pvt);
222 memput(this, sizeof *this);
225 static struct hostent *
226 ho_byname(struct irs_ho *this, const char *name) {
227 struct pvt *pvt = (struct pvt *)this->private;
230 if (init(this) == -1)
233 if (pvt->res->options & RES_USE_INET6) {
234 hp = ho_byname2(this, name, AF_INET6);
238 return (ho_byname2(this, name, AF_INET));
241 static struct hostent *
242 ho_byname2(struct irs_ho *this, const char *name, int af)
244 struct pvt *pvt = (struct pvt *)this->private;
245 struct hostent *hp = NULL;
247 char tmp[NS_MAXDNAME];
250 struct dns_res_target *q, *p;
251 int querystate = RESQRY_FAIL;
253 if (init(this) == -1)
256 q = memget(sizeof(*q));
258 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
262 memset(q, 0, sizeof(*q));
269 q->answer = q->qbuf.buf;
270 q->anslen = sizeof(q->qbuf);
271 q->action = RESTGT_DOALWAYS;
277 q->answer = q->qbuf.buf;
278 q->anslen = sizeof(q->qbuf);
279 q->action = RESTGT_DOALWAYS;
282 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
283 errno = EAFNOSUPPORT;
289 * if there aren't any dots, it could be a user-level alias.
290 * this is also done in res_nquery() since we are not the only
291 * function that looks up host names.
293 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
297 for (p = q; p; p = p->next) {
299 case RESTGT_DOALWAYS:
301 case RESTGT_AFTERFAILURE:
302 if (querystate == RESQRY_SUCCESS)
309 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
310 p->answer, p->anslen)) < 0) {
311 querystate = RESQRY_FAIL;
315 memset(&ai, 0, sizeof(ai));
317 if ((hp = gethostans(this, p->answer, n, name, p->qtype,
319 (const struct addrinfo *)&ai)) != NULL)
320 goto cleanup; /* no more loop is necessary */
322 querystate = RESQRY_FAIL;
328 memput(q, sizeof(*q));
332 static struct hostent *
333 ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
335 struct pvt *pvt = (struct pvt *)this->private;
336 const u_char *uaddr = addr;
338 struct hostent *hp = NULL;
340 struct dns_res_target *q, *q2, *p;
342 int querystate = RESQRY_FAIL;
344 if (init(this) == -1)
347 q = memget(sizeof(*q));
348 q2 = memget(sizeof(*q2));
349 if (q == NULL || q2 == NULL) {
350 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
354 memset(q, 0, sizeof(*q));
355 memset(q2, 0, sizeof(*q2));
357 if (af == AF_INET6 && len == IN6ADDRSZ &&
358 (!memcmp(uaddr, mapped, sizeof mapped) ||
359 (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
360 memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
362 addr = (const char *)addr + sizeof mapped;
363 uaddr += sizeof mapped;
372 q->answer = q->qbuf.buf;
373 q->anslen = sizeof(q->qbuf);
374 q->action = RESTGT_DOALWAYS;
380 q->answer = q->qbuf.buf;
381 q->anslen = sizeof(q->qbuf);
383 q->action = RESTGT_DOALWAYS;
386 q2->answer = q2->qbuf.buf;
387 q2->anslen = sizeof(q2->qbuf);
388 if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
389 q2->action = RESTGT_IGNORE;
391 q2->action = RESTGT_AFTERFAILURE;
394 errno = EAFNOSUPPORT;
395 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
401 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
408 (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
415 if (q->action != RESTGT_IGNORE) {
416 const char *nibsuff = res_get_nibblesuffix(pvt->res);
418 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
419 i = SPRINTF((qp, "%x.%x.",
421 (uaddr[n] >> 4) & 0xf));
426 if (strlen(q->qname) + strlen(nibsuff) + 1 >
428 errno = ENAMETOOLONG;
429 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
433 strcpy(qp, nibsuff); /* (checked) */
435 if (q2->action != RESTGT_IGNORE) {
436 const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
438 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
439 i = SPRINTF((qp, "%x.%x.",
441 (uaddr[n] >> 4) & 0xf));
446 if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
448 errno = ENAMETOOLONG;
449 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
453 strcpy(qp, nibsuff2); /* (checked) */
460 for (p = q; p; p = p->next) {
462 case RESTGT_DOALWAYS:
464 case RESTGT_AFTERFAILURE:
465 if (querystate == RESQRY_SUCCESS)
472 if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
473 p->answer, p->anslen)) < 0) {
474 querystate = RESQRY_FAIL;
478 memset(&ai, 0, sizeof(ai));
480 hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
481 NULL, (const struct addrinfo *)&ai);
483 querystate = RESQRY_FAIL;
487 memcpy(pvt->host_addr, addr, len);
488 pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
489 pvt->h_addr_ptrs[1] = NULL;
490 if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
491 map_v4v6_address((char*)pvt->host_addr,
492 (char*)pvt->host_addr);
493 pvt->host.h_addrtype = AF_INET6;
494 pvt->host.h_length = IN6ADDRSZ;
497 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
498 goto cleanup; /* no more loop is necessary. */
500 hp = NULL; /* H_ERRNO was set by subroutines */
504 memput(q, sizeof(*q));
506 memput(q2, sizeof(*q2));
510 static struct hostent *
511 ho_next(struct irs_ho *this) {
519 ho_rewind(struct irs_ho *this) {
527 ho_minimize(struct irs_ho *this) {
528 struct pvt *pvt = (struct pvt *)this->private;
531 res_nclose(pvt->res);
534 static struct __res_state *
535 ho_res_get(struct irs_ho *this) {
536 struct pvt *pvt = (struct pvt *)this->private;
539 struct __res_state *res;
540 res = (struct __res_state *)malloc(sizeof *res);
545 memset(res, 0, sizeof *res);
546 ho_res_set(this, res, free);
553 extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
556 static struct addrinfo *
557 ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
559 struct pvt *pvt = (struct pvt *)this->private;
561 char tmp[NS_MAXDNAME];
563 struct dns_res_target *q, *q2, *p;
564 struct addrinfo sentinel, *cur;
565 int querystate = RESQRY_FAIL;
567 if (init(this) == -1)
570 memset(&sentinel, 0, sizeof(sentinel));
573 q = memget(sizeof(*q));
574 q2 = memget(sizeof(*q2));
575 if (q == NULL || q2 == NULL) {
576 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
580 memset(q, 0, sizeof(*q2));
581 memset(q2, 0, sizeof(*q2));
583 switch (pai->ai_family) {
588 q->answer = q->qbuf.buf;
589 q->anslen = sizeof(q->qbuf);
591 q->action = RESTGT_DOALWAYS;
594 q2->answer = q2->qbuf.buf;
595 q2->anslen = sizeof(q2->qbuf);
596 q2->action = RESTGT_DOALWAYS;
601 q->answer = q->qbuf.buf;
602 q->anslen = sizeof(q->qbuf);
603 q->action = RESTGT_DOALWAYS;
608 q->answer = q->qbuf.buf;
609 q->anslen = sizeof(q->qbuf);
610 q->action = RESTGT_DOALWAYS;
613 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /* better error? */
618 * if there aren't any dots, it could be a user-level alias.
619 * this is also done in res_nquery() since we are not the only
620 * function that looks up host names.
622 if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
626 for (p = q; p; p = p->next) {
630 case RESTGT_DOALWAYS:
632 case RESTGT_AFTERFAILURE:
633 if (querystate == RESQRY_SUCCESS)
640 if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
641 p->answer, p->anslen)) < 0) {
642 querystate = RESQRY_FAIL;
645 (void)gethostans(this, p->answer, n, name, p->qtype,
646 pai->ai_family, /* XXX: meaningless */
649 querystate = RESQRY_SUCCESS;
654 querystate = RESQRY_FAIL;
659 memput(q, sizeof(*q));
661 memput(q2, sizeof(*q2));
662 return(sentinel.ai_next);
666 ho_res_set(struct irs_ho *this, struct __res_state *res,
667 void (*free_res)(void *)) {
668 struct pvt *pvt = (struct pvt *)this->private;
670 if (pvt->res && pvt->free_res) {
671 res_nclose(pvt->res);
672 (*pvt->free_res)(pvt->res);
676 pvt->free_res = free_res;
681 static struct hostent *
682 gethostans(struct irs_ho *this,
683 const u_char *ansbuf, int anslen, const char *qname, int qtype,
684 int af, int size, /* meaningless for addrinfo cases */
685 struct addrinfo **ret_aip, const struct addrinfo *pai)
687 struct pvt *pvt = (struct pvt *)this->private;
688 int type, class, ancount, qdcount, n, haveanswer, had_error;
689 int error = NETDB_SUCCESS;
690 int (*name_ok)(const char *);
697 char *bp, *ep, **ap, **hap;
698 char tbuf[MAXDNAME+1];
699 struct addrinfo sentinel, *cur, ai;
701 if (pai == NULL) abort();
704 memset(&sentinel, 0, sizeof(sentinel));
708 eom = ansbuf + anslen;
712 case T_ANY: /* use T_ANY only for T_A/T_AAAA lookup */
722 pvt->host.h_addrtype = af;
723 pvt->host.h_length = size;
724 hname = pvt->host.h_name = NULL;
727 * Find first satisfactory answer.
729 if (ansbuf + HFIXEDSZ > eom) {
730 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
733 hp = (const HEADER *)ansbuf;
734 ancount = ntohs(hp->ancount);
735 qdcount = ntohs(hp->qdcount);
737 ep = pvt->hostbuf + sizeof(pvt->hostbuf);
738 cp = ansbuf + HFIXEDSZ;
740 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
743 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
744 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
745 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
750 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
753 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
754 /* res_nsend() has already verified that the query name is the
755 * same as the one we sent; this just gets the expanded name
756 * (i.e., with the succeeding search-domain tacked on).
758 n = strlen(bp) + 1; /* for the \0 */
759 if (n > MAXHOSTNAMELEN) {
760 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
763 pvt->host.h_name = bp;
766 /* The qname can be abbreviated, but hname is now absolute. */
767 qname = pvt->host.h_name;
769 ap = pvt->host_aliases;
771 pvt->host.h_aliases = pvt->host_aliases;
772 hap = pvt->h_addr_ptrs;
774 pvt->host.h_addr_list = pvt->h_addr_ptrs;
777 while (ancount-- > 0 && cp < eom && !had_error) {
778 n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
779 if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
784 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
786 cp += INT16SZ; /* type */
787 class = ns_get16(cp);
788 cp += INT16SZ + INT32SZ; /* class, TTL */
790 cp += INT16SZ; /* len */
797 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
800 int level = LOG_CRIT;
802 level |= LOG_SECURITY;
805 "gethostans: possible attempt to exploit buffer overflow while looking up %s",
806 *qname ? qname : ".");
808 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
809 if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
815 if (ap >= &pvt->host_aliases[MAXALIASES-1])
818 n = strlen(bp) + 1; /* for the \0 */
820 /* Get canonical name. */
821 n = strlen(tbuf) + 1; /* for the \0 */
822 if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
826 strcpy(bp, tbuf); /* (checked) */
827 pvt->host.h_name = bp;
832 if (qtype == T_PTR && type == T_CNAME) {
833 n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
834 if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
840 if ((pvt->res->options & RES_USE_DNAME) != 0U)
844 * We may be able to check this regardless
845 * of the USE_DNAME bit, but we add the check
846 * for now since the DNAME support is
849 if (ns_samename(tname, bp) != 1)
852 /* Get canonical name. */
853 n = strlen(tbuf) + 1; /* for the \0 */
858 strcpy(bp, tbuf); /* (checked) */
863 if (qtype == T_ANY) {
864 if (!(type == T_A || type == T_AAAA)) {
868 } else if (type != qtype) {
874 if (ret_aip != NULL) {
875 /* addrinfo never needs T_PTR */
879 if (ns_samename(tname, bp) != 1) {
883 n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
884 if (n < 0 || !maybe_hnok(pvt->res, bp) ||
885 n >= MAXHOSTNAMELEN) {
891 pvt->host.h_name = bp;
894 else if (ap < &pvt->host_aliases[MAXALIASES-1])
899 n = strlen(bp) + 1; /* for the \0 */
905 if (ns_samename(hname, bp) != 1) {
909 if (type == T_A && n != INADDRSZ) {
913 if (type == T_AAAA && n != IN6ADDRSZ) {
918 /* make addrinfo. don't overwrite constant PAI */
920 ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
921 cur->ai_next = addr2addrinfo(
922 (const struct addrinfo *)&ai,
924 if (cur->ai_next == NULL)
930 nn = strlen(bp) + 1; /* for the \0 */
931 if (nn >= MAXHOSTNAMELEN) {
936 pvt->host.h_name = bp;
940 /* Ensure alignment. */
941 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
942 ~(sizeof(align) - 1));
943 /* Avoid overflows. */
944 if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) {
948 if (ret_aip) { /* need addrinfo. keep it. */
951 } else if (cur->ai_next) { /* need hostent */
952 struct addrinfo *aip = cur->ai_next;
954 for (aip = cur->ai_next; aip;
955 aip = aip->ai_next) {
958 m = add_hostent(pvt, bp, hap, aip);
965 if (hap < &pvt->h_addr_ptrs[MAXADDRS-1])
971 freeaddrinfo(cur->ai_next);
983 if (ret_aip == NULL) {
987 if (pvt->res->nsort && haveanswer > 1 && qtype == T_A)
988 addrsort(pvt->res, pvt->h_addr_ptrs,
990 if (pvt->host.h_name == NULL) {
991 n = strlen(qname) + 1; /* for the \0 */
992 if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
994 strcpy(bp, qname); /* (checked) */
995 pvt->host.h_name = bp;
998 if (pvt->res->options & RES_USE_INET6)
999 map_v4v6_hostent(&pvt->host, &bp, ep);
1000 RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
1001 return (&pvt->host);
1003 if ((pai->ai_flags & AI_CANONNAME) != 0) {
1004 if (pvt->host.h_name == NULL) {
1005 sentinel.ai_next->ai_canonname =
1009 sentinel.ai_next->ai_canonname =
1010 strdup(pvt->host.h_name);
1013 *ret_aip = sentinel.ai_next;
1018 if (sentinel.ai_next) {
1019 /* this should be impossible, but check it for safety */
1020 freeaddrinfo(sentinel.ai_next);
1022 if (error == NETDB_SUCCESS)
1023 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
1025 RES_SET_H_ERRNO(pvt->res, error);
1030 add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
1037 switch(ai->ai_addr->sa_family) {
1039 addrlen = IN6ADDRSZ;
1040 addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1044 addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
1047 return(-1); /* abort? */
1050 /* Ensure alignment. */
1051 bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
1052 ~(sizeof(align) - 1));
1053 /* Avoid overflows. */
1054 if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf])
1056 if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1])
1057 return(0); /* fail, but not treat it as an error. */
1059 /* Suppress duplicates. */
1060 for (tap = (const char **)pvt->h_addr_ptrs;
1063 if (memcmp(*tap, addrp, addrlen) == 0)
1068 memcpy(*hap = bp, addrp, addrlen);
1069 return((bp + addrlen) - obp);
1073 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
1076 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1078 hp->h_addrtype = AF_INET6;
1079 hp->h_length = IN6ADDRSZ;
1080 for (ap = hp->h_addr_list; *ap; ap++) {
1081 int i = (u_long)*bpp % sizeof(align);
1084 i = sizeof(align) - i;
1086 if ((ep - *bpp) < (i + IN6ADDRSZ)) {
1087 /* Out of memory. Truncate address list here. */
1092 map_v4v6_address(*ap, *bpp);
1099 addrsort(res_state statp, char **ap, int num) {
1100 int i, j, needsort = 0, aval[MAXADDRS];
1104 for (i = 0; i < num; i++, p++) {
1105 for (j = 0 ; (unsigned)j < statp->nsort; j++)
1106 if (statp->sort_list[j].addr.s_addr ==
1107 (((struct in_addr *)(*p))->s_addr &
1108 statp->sort_list[j].mask))
1111 if (needsort == 0 && i > 0 && j < aval[i-1])
1117 while (needsort < num) {
1118 for (j = needsort - 1; j >= 0; j--) {
1119 if (aval[j] > aval[j+1]) {
1123 aval[j] = aval[j+1];
1138 init(struct irs_ho *this) {
1139 struct pvt *pvt = (struct pvt *)this->private;
1141 if (!pvt->res && !ho_res_get(this))
1143 if (((pvt->res->options & RES_INIT) == 0U) &&
1144 res_ninit(pvt->res) == -1)