1 /* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
35 * Issues to be discussed:
36 * - Return values. There are nonstandard return values defined and used
37 * in the source code. This is because RFC2553 is silent about which error
38 * code must be returned for which situation.
39 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
40 * invalid. current code - SEGV on freeaddrinfo(NULL)
43 * - The code filters out AFs that are not supported by the kernel,
44 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
45 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
47 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
48 * (1) what should we do against numeric hostname (2) what should we do
49 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
50 * non-loopback address configured? global address configured?
52 * OS specific notes for freebsd4:
53 * - FreeBSD supported $GAI. The code does not.
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include "namespace.h"
60 #include <sys/types.h>
61 #include <sys/param.h>
62 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <net/if_types.h>
67 #include <sys/queue.h>
69 #include <net/if_var.h>
70 #include <sys/sysctl.h>
71 #include <sys/ioctl.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/nd6.h>
75 #include <arpa/inet.h>
76 #include <arpa/nameser.h>
78 #include <rpcsvc/yp_prot.h>
79 #include <rpcsvc/ypclnt.h>
90 #include "res_config.h"
98 #include "un-namespace.h"
99 #include "netdb_private.h"
100 #include "libc_private.h"
105 #if defined(__KAME__) && defined(INET6)
113 static const char in_addrany[] = { 0, 0, 0, 0 };
114 static const char in_loopback[] = { 127, 0, 0, 1 };
116 static const char in6_addrany[] = {
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
119 static const char in6_loopback[] = {
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
125 TAILQ_ENTRY(policyqueue) pc_entry;
127 struct in6_addrpolicy pc_policy;
130 TAILQ_HEAD(policyhead, policyqueue);
132 static const struct afd {
137 const char *a_addrany;
138 const char *a_loopback;
143 {PF_INET6, sizeof(struct in6_addr),
144 sizeof(struct sockaddr_in6),
145 offsetof(struct sockaddr_in6, sin6_addr),
146 in6_addrany, in6_loopback, 1},
151 {PF_INET, sizeof(struct in_addr),
152 sizeof(struct sockaddr_in),
153 offsetof(struct sockaddr_in, sin_addr),
154 in_addrany, in_loopback, 0},
155 {0, 0, 0, 0, NULL, NULL, 0},
163 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
164 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
165 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
168 static const struct explore explore[] = {
170 { PF_LOCAL, ANY, ANY, 0x01 },
173 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07 },
174 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07 },
175 { PF_INET6, SOCK_STREAM, IPPROTO_SCTP, 0x03 },
176 { PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, 0x07 },
177 { PF_INET6, SOCK_DGRAM, IPPROTO_UDPLITE, 0x03 },
178 { PF_INET6, SOCK_RAW, ANY, 0x05 },
180 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07 },
181 { PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07 },
182 { PF_INET, SOCK_STREAM, IPPROTO_SCTP, 0x03 },
183 { PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP, 0x07 },
184 { PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE, 0x03 },
185 { PF_INET, SOCK_RAW, ANY, 0x05 },
195 #define AIO_SRCFLAG_DEPRECATED 0x1
199 struct sockaddr_storage aiou_ss;
200 struct sockaddr aiou_sa;
202 #define aio_srcsa aio_src_un.aiou_sa
203 u_int32_t aio_srcflag;
206 struct policyqueue *aio_srcpolicy;
207 struct policyqueue *aio_dstpolicy;
208 struct addrinfo *aio_ai;
212 static const ns_src default_dns_files[] = {
213 { NSSRC_FILES, NS_SUCCESS },
214 { NSSRC_DNS, NS_SUCCESS },
219 struct res_target *next;
220 const char *name; /* domain name */
221 int qclass, qtype; /* class and type of query */
222 u_char *answer; /* buffer to put answer */
223 int anslen; /* size of answer buffer */
224 int n; /* result length */
227 #define MAXPACKET (64*1024)
231 u_char buf[MAXPACKET];
234 static int str2number(const char *, int *);
235 static int explore_copy(const struct addrinfo *, const struct addrinfo *,
237 static int explore_null(const struct addrinfo *,
238 const char *, struct addrinfo **);
239 static int explore_numeric(const struct addrinfo *, const char *,
240 const char *, struct addrinfo **, const char *);
241 static int explore_numeric_scope(const struct addrinfo *, const char *,
242 const char *, struct addrinfo **);
243 static int get_canonname(const struct addrinfo *,
244 struct addrinfo *, const char *);
245 static struct addrinfo *get_ai(const struct addrinfo *,
246 const struct afd *, const char *);
247 static struct addrinfo *copy_ai(const struct addrinfo *);
248 static int get_portmatch(const struct addrinfo *, const char *);
249 static int get_port(struct addrinfo *, const char *, int);
250 static const struct afd *find_afd(int);
251 static int addrconfig(struct addrinfo *);
253 static int is_ifdisabled(char *);
255 static void set_source(struct ai_order *, struct policyhead *);
256 static int comp_dst(const void *, const void *);
258 static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
260 static int gai_addr2scopetype(struct sockaddr *);
262 static int explore_fqdn(const struct addrinfo *, const char *,
263 const char *, struct addrinfo **);
265 static int reorder(struct addrinfo *);
266 static int get_addrselectpolicy(struct policyhead *);
267 static void free_addrselectpolicy(struct policyhead *);
268 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
269 struct policyhead *);
270 static int matchlen(struct sockaddr *, struct sockaddr *);
272 static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
273 const struct addrinfo *, res_state);
274 #if defined(RESOLVSORT)
275 static int addr4sort(struct addrinfo *, res_state);
277 static int _dns_getaddrinfo(void *, void *, va_list);
278 static void _sethtent(FILE **);
279 static void _endhtent(FILE **);
280 static struct addrinfo *_gethtent(FILE **, const char *,
281 const struct addrinfo *);
282 static int _files_getaddrinfo(void *, void *, va_list);
284 static struct addrinfo *_yphostent(char *, const struct addrinfo *);
285 static int _yp_getaddrinfo(void *, void *, va_list);
288 static int addrinfo_id_func(char *, size_t *, va_list, void *);
289 static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *);
290 static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *);
293 static int res_queryN(const char *, struct res_target *, res_state);
294 static int res_searchN(const char *, struct res_target *, res_state);
295 static int res_querydomainN(const char *, const char *,
296 struct res_target *, res_state);
298 /* XXX macros that make external reference is BAD. */
300 #define GET_AI(ai, afd, addr) \
302 /* external reference: pai, error, and label free */ \
303 (ai) = get_ai(pai, (afd), (addr)); \
304 if ((ai) == NULL) { \
305 error = EAI_MEMORY; \
308 } while (/*CONSTCOND*/0)
310 #define GET_PORT(ai, serv) \
312 /* external reference: error and label free */ \
313 error = get_port((ai), (serv), 0); \
316 } while (/*CONSTCOND*/0)
318 #define GET_CANONNAME(ai, str) \
320 /* external reference: pai, error and label free */ \
321 error = get_canonname(pai, (ai), (str)); \
324 } while (/*CONSTCOND*/0)
328 /* external reference: error, and label bad */ \
332 } while (/*CONSTCOND*/0)
334 #define MATCH_FAMILY(x, y, w) \
335 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
336 #define MATCH(x, y, w) \
337 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
340 freeaddrinfo(struct addrinfo *ai)
342 struct addrinfo *next;
346 if (ai->ai_canonname)
347 free(ai->ai_canonname);
348 /* no need to free(ai->ai_addr) */
355 str2number(const char *p, int *portp)
364 v = strtoul(p, &ep, 10);
365 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) {
373 getaddrinfo(const char *hostname, const char *servname,
374 const struct addrinfo *hints, struct addrinfo **res)
376 struct addrinfo sentinel;
377 struct addrinfo *cur;
379 struct addrinfo ai, ai0, *afai;
380 struct addrinfo *pai;
381 const struct afd *afd;
382 const struct explore *ex;
383 struct addrinfo *afailist[sizeof(afdl)/sizeof(afdl[0])];
384 struct addrinfo *afai_unspec;
388 /* ensure we return NULL on errors */
391 memset(&ai, 0, sizeof(ai));
393 memset(afailist, 0, sizeof(afailist));
396 memset(&sentinel, 0, sizeof(sentinel));
400 pai->ai_family = PF_UNSPEC;
401 pai->ai_socktype = ANY;
402 pai->ai_protocol = ANY;
404 pai->ai_canonname = NULL;
408 if (hostname == NULL && servname == NULL)
411 /* error check for hints */
412 if (hints->ai_addrlen || hints->ai_canonname ||
413 hints->ai_addr || hints->ai_next)
414 ERR(EAI_BADHINTS); /* xxx */
415 if (hints->ai_flags & ~AI_MASK)
417 switch (hints->ai_family) {
427 memcpy(pai, hints, sizeof(*pai));
430 * if both socktype/protocol are specified, check if they
431 * are meaningful combination.
433 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
434 for (ex = explore; ex->e_af >= 0; ex++) {
435 if (!MATCH_FAMILY(pai->ai_family, ex->e_af,
438 if (!MATCH(pai->ai_socktype, ex->e_socktype,
441 if (!MATCH(pai->ai_protocol, ex->e_protocol,
455 * RFC 3493: AI_ALL and AI_V4MAPPED are effective only against
456 * AF_INET6 query. They need to be ignored if specified in other
459 switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
461 case AI_ALL | AI_V4MAPPED:
463 if (pai->ai_family != AF_INET6)
464 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
468 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
473 * check for special cases. (1) numeric servname is disallowed if
474 * socktype/protocol are left unspecified. (2) servname is disallowed
475 * for raw and other inet{,6} sockets.
477 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
479 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
482 ai0 = *pai; /* backup *pai */
484 if (pai->ai_family == PF_UNSPEC) {
486 pai->ai_family = PF_INET6;
488 pai->ai_family = PF_INET;
491 error = get_portmatch(pai, servname);
501 * NULL hostname, or numeric hostname.
502 * If numeric representation of AF1 can be interpreted as FQDN
503 * representation of AF2, we need to think again about the code below.
506 for (afd = afdl; afd->a_af; afd++) {
509 if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
512 if (pai->ai_family == PF_UNSPEC)
513 pai->ai_family = afd->a_af;
515 if (hostname == NULL) {
516 error = explore_null(pai, servname,
517 &afailist[afd - afdl]);
520 * Errors from explore_null should be unexpected and
521 * be caught to avoid returning an incomplete result.
526 error = explore_numeric_scope(pai, hostname, servname,
527 &afailist[afd - afdl]);
530 * explore_numeric_scope returns an error for address
531 * families that do not match that of hostname.
532 * Thus we should not catch the error at this moment.
536 if (!error && afailist[afd - afdl])
544 if (hostname == NULL)
545 ERR(EAI_NONAME); /* used to be EAI_NODATA */
546 if (pai->ai_flags & AI_NUMERICHOST)
549 if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
553 * hostname as alphabetical name.
556 error = explore_fqdn(pai, hostname, servname, &afai_unspec);
559 for (ex = explore; ex->e_af >= 0; ex++) {
562 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
564 if (!MATCH(pai->ai_socktype, ex->e_socktype,
567 if (!MATCH(pai->ai_protocol, ex->e_protocol,
571 if (pai->ai_family == PF_UNSPEC)
572 pai->ai_family = ex->e_af;
573 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
574 pai->ai_socktype = ex->e_socktype;
575 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
576 pai->ai_protocol = ex->e_protocol;
579 * if the servname does not match socktype/protocol, ignore it.
581 if (get_portmatch(pai, servname) != 0)
587 if ((afd = find_afd(pai->ai_family)) == NULL)
589 /* XXX assumes that afd points inside afdl[] */
590 afai = afailist[afd - afdl];
595 error = explore_copy(pai, afai, &cur->ai_next);
599 while (cur && cur->ai_next)
604 * ensure we return either:
605 * - error == 0, non-NULL *res
606 * - error != 0, NULL *res
609 if (sentinel.ai_next) {
611 * If the returned entry is for an active connection,
612 * and the given name is not numeric, reorder the
613 * list, so that the application would try the list
614 * in the most efficient order. Since the head entry
615 * of the original list may contain ai_canonname and
616 * that entry may be moved elsewhere in the new list,
617 * we keep the pointer and will restore it in the new
618 * head entry. (Note that RFC3493 requires the head
619 * entry store it when requested by the caller).
621 if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
626 sentinel.ai_next->ai_canonname;
627 sentinel.ai_next->ai_canonname = NULL;
628 (void)reorder(&sentinel);
629 if (sentinel.ai_next->ai_canonname ==
631 sentinel.ai_next->ai_canonname
633 } else if (canonname != NULL)
637 *res = sentinel.ai_next;
644 freeaddrinfo(afai_unspec);
645 for (afd = afdl; afd->a_af; afd++) {
646 if (afailist[afd - afdl])
647 freeaddrinfo(afailist[afd - afdl]);
650 if (sentinel.ai_next)
651 freeaddrinfo(sentinel.ai_next);
657 reorder(struct addrinfo *sentinel)
659 struct addrinfo *ai, **aip;
660 struct ai_order *aio;
662 struct policyhead policyhead;
664 /* count the number of addrinfo elements for sorting. */
665 for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
669 * If the number is small enough, we can skip the reordering process.
674 /* allocate a temporary array for sort and initialization of it. */
675 if ((aio = malloc(sizeof(*aio) * n)) == NULL)
676 return(n); /* give up reordering */
677 memset(aio, 0, sizeof(*aio) * n);
679 /* retrieve address selection policy from the kernel */
680 TAILQ_INIT(&policyhead);
681 if (!get_addrselectpolicy(&policyhead)) {
682 /* no policy is installed into kernel, we don't sort. */
687 for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
689 aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
690 aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
692 set_source(&aio[i], &policyhead);
695 /* perform sorting. */
696 qsort(aio, n, sizeof(*aio), comp_dst);
698 /* reorder the addrinfo chain. */
699 for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
700 *aip = aio[i].aio_ai;
701 aip = &aio[i].aio_ai->ai_next;
705 /* cleanup and return */
707 free_addrselectpolicy(&policyhead);
712 get_addrselectpolicy(struct policyhead *head)
715 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
718 struct in6_addrpolicy *pol, *ep;
720 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
724 if ((buf = malloc(l)) == NULL)
726 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
731 ep = (struct in6_addrpolicy *)(buf + l);
732 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
733 struct policyqueue *new;
735 if ((new = malloc(sizeof(*new))) == NULL) {
736 free_addrselectpolicy(head); /* make the list empty */
739 new->pc_policy = *pol;
740 TAILQ_INSERT_TAIL(head, new, pc_entry);
751 free_addrselectpolicy(struct policyhead *head)
753 struct policyqueue *ent, *nent;
755 for (ent = TAILQ_FIRST(head); ent; ent = nent) {
756 nent = TAILQ_NEXT(ent, pc_entry);
757 TAILQ_REMOVE(head, ent, pc_entry);
762 static struct policyqueue *
763 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
766 struct policyqueue *ent, *bestent = NULL;
767 struct in6_addrpolicy *pol;
768 int matchlen, bestmatchlen = -1;
769 u_char *mp, *ep, *k, *p, m;
770 struct sockaddr_in6 key;
772 switch(addr->sa_family) {
774 key = *(struct sockaddr_in6 *)addr;
777 /* convert the address into IPv4-mapped IPv6 address. */
778 memset(&key, 0, sizeof(key));
779 key.sin6_family = AF_INET6;
780 key.sin6_len = sizeof(key);
782 (char *)&((struct sockaddr_in *)addr)->sin_addr,
783 (char *)&key.sin6_addr);
789 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
790 pol = &ent->pc_policy;
793 mp = (u_char *)&pol->addrmask.sin6_addr;
794 ep = mp + 16; /* XXX: scope field? */
795 k = (u_char *)&key.sin6_addr;
796 p = (u_char *)&pol->addr.sin6_addr;
797 for (; mp < ep && *mp; mp++, k++, p++) {
800 goto next; /* not match */
801 if (m == 0xff) /* short cut for a typical case */
811 /* matched. check if this is better than the current best. */
812 if (matchlen > bestmatchlen) {
814 bestmatchlen = matchlen;
829 set_source(struct ai_order *aio, struct policyhead *ph)
831 struct addrinfo ai = *aio->aio_ai;
832 struct sockaddr_storage ss;
836 /* set unspec ("no source is available"), just in case */
837 aio->aio_srcsa.sa_family = AF_UNSPEC;
838 aio->aio_srcscope = -1;
840 switch(ai.ai_family) {
846 default: /* ignore unsupported AFs explicitly */
850 /* XXX: make a dummy addrinfo to call connect() */
851 ai.ai_socktype = SOCK_DGRAM;
852 ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
854 memset(&ss, 0, sizeof(ss));
855 memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
856 ai.ai_addr = (struct sockaddr *)&ss;
857 get_port(&ai, "1", 0);
859 /* open a socket to get the source address for the given dst */
860 if ((s = _socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC,
861 ai.ai_protocol)) < 0)
862 return; /* give up */
864 if (ai.ai_family == AF_INET6) {
865 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai.ai_addr;
868 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
869 (void)_setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
870 (char *)&off, sizeof(off));
873 if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
875 srclen = ai.ai_addrlen;
876 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
877 aio->aio_srcsa.sa_family = AF_UNSPEC;
880 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
881 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
882 aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
884 if (ai.ai_family == AF_INET6) {
885 struct in6_ifreq ifr6;
888 memset(&ifr6, 0, sizeof(ifr6));
889 memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
890 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
891 flags6 = ifr6.ifr_ifru.ifru_flags6;
892 if ((flags6 & IN6_IFF_DEPRECATED))
893 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
904 matchlen(struct sockaddr *src, struct sockaddr *dst)
911 switch (src->sa_family) {
914 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
915 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
916 addrlen = sizeof(struct in6_addr);
921 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
922 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
923 addrlen = sizeof(struct in_addr);
931 if ((r = (*d++ ^ *s++)) != 0) {
932 while (r < addrlen * 8) {
943 comp_dst(const void *arg1, const void *arg2)
945 const struct ai_order *dst1 = arg1, *dst2 = arg2;
948 * Rule 1: Avoid unusable destinations.
949 * XXX: we currently do not consider if an appropriate route exists.
951 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
952 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
955 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
956 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
960 /* Rule 2: Prefer matching scope. */
961 if (dst1->aio_dstscope == dst1->aio_srcscope &&
962 dst2->aio_dstscope != dst2->aio_srcscope) {
965 if (dst1->aio_dstscope != dst1->aio_srcscope &&
966 dst2->aio_dstscope == dst2->aio_srcscope) {
970 /* Rule 3: Avoid deprecated addresses. */
971 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
972 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
973 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
974 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
977 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
978 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
983 /* Rule 4: Prefer home addresses. */
984 /* XXX: not implemented yet */
986 /* Rule 5: Prefer matching label. */
988 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
989 dst1->aio_srcpolicy->pc_policy.label ==
990 dst1->aio_dstpolicy->pc_policy.label &&
991 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
992 dst2->aio_srcpolicy->pc_policy.label !=
993 dst2->aio_dstpolicy->pc_policy.label)) {
996 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
997 dst2->aio_srcpolicy->pc_policy.label ==
998 dst2->aio_dstpolicy->pc_policy.label &&
999 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1000 dst1->aio_srcpolicy->pc_policy.label !=
1001 dst1->aio_dstpolicy->pc_policy.label)) {
1006 /* Rule 6: Prefer higher precedence. */
1008 if (dst1->aio_dstpolicy &&
1009 (dst2->aio_dstpolicy == NULL ||
1010 dst1->aio_dstpolicy->pc_policy.preced >
1011 dst2->aio_dstpolicy->pc_policy.preced)) {
1014 if (dst2->aio_dstpolicy &&
1015 (dst1->aio_dstpolicy == NULL ||
1016 dst2->aio_dstpolicy->pc_policy.preced >
1017 dst1->aio_dstpolicy->pc_policy.preced)) {
1022 /* Rule 7: Prefer native transport. */
1023 /* XXX: not implemented yet */
1025 /* Rule 8: Prefer smaller scope. */
1026 if (dst1->aio_dstscope >= 0 &&
1027 dst1->aio_dstscope < dst2->aio_dstscope) {
1030 if (dst2->aio_dstscope >= 0 &&
1031 dst2->aio_dstscope < dst1->aio_dstscope) {
1036 * Rule 9: Use longest matching prefix.
1037 * We compare the match length in a same AF only.
1039 if (dst1->aio_ai->ai_addr->sa_family ==
1040 dst2->aio_ai->ai_addr->sa_family &&
1041 dst1->aio_ai->ai_addr->sa_family != AF_INET) {
1042 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1045 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1050 /* Rule 10: Otherwise, leave the order unchanged. */
1055 * Copy from scope.c.
1056 * XXX: we should standardize the functions and link them as standard
1060 gai_addr2scopetype(struct sockaddr *sa)
1063 struct sockaddr_in6 *sa6;
1065 struct sockaddr_in *sa4;
1067 switch(sa->sa_family) {
1070 sa6 = (struct sockaddr_in6 *)sa;
1071 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1072 /* just use the scope field of the multicast address */
1073 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1076 * Unicast addresses: map scope type to corresponding scope
1077 * value defined for multcast addresses.
1078 * XXX: hardcoded scope type values are bad...
1080 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1081 return(1); /* node local scope */
1082 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1083 return(2); /* link-local scope */
1084 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1085 return(5); /* site-local scope */
1086 return(14); /* global scope */
1091 * IPv4 pseudo scoping according to RFC 3484.
1093 sa4 = (struct sockaddr_in *)sa;
1094 /* IPv4 autoconfiguration addresses have link-local scope. */
1095 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1096 ((u_char *)&sa4->sin_addr)[1] == 254)
1098 /* Private addresses have site-local scope. */
1099 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1100 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1101 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1102 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1103 ((u_char *)&sa4->sin_addr)[1] == 168))
1104 return(14); /* XXX: It should be 5 unless NAT */
1105 /* Loopback addresses have link-local scope. */
1106 if (((u_char *)&sa4->sin_addr)[0] == 127)
1111 errno = EAFNOSUPPORT; /* is this a good error? */
1117 explore_copy(const struct addrinfo *pai, const struct addrinfo *src0,
1118 struct addrinfo **res)
1121 struct addrinfo sentinel, *cur;
1122 const struct addrinfo *src;
1125 sentinel.ai_next = NULL;
1128 for (src = src0; src != NULL; src = src->ai_next) {
1129 if (src->ai_family != pai->ai_family)
1132 cur->ai_next = copy_ai(src);
1133 if (!cur->ai_next) {
1138 cur->ai_next->ai_socktype = pai->ai_socktype;
1139 cur->ai_next->ai_protocol = pai->ai_protocol;
1143 *res = sentinel.ai_next;
1147 freeaddrinfo(sentinel.ai_next);
1153 * passive socket -> anyaddr (0.0.0.0 or ::)
1154 * non-passive socket -> localhost (127.0.0.1 or ::1)
1157 explore_null(const struct addrinfo *pai, const char *servname,
1158 struct addrinfo **res)
1161 const struct afd *afd;
1162 struct addrinfo *ai;
1169 * filter out AFs that are not supported by the kernel
1172 s = _socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1174 if (errno != EMFILE)
1179 afd = find_afd(pai->ai_family);
1183 if (pai->ai_flags & AI_PASSIVE) {
1184 GET_AI(ai, afd, afd->a_addrany);
1185 GET_PORT(ai, servname);
1187 GET_AI(ai, afd, afd->a_loopback);
1188 GET_PORT(ai, servname);
1204 explore_numeric(const struct addrinfo *pai, const char *hostname,
1205 const char *servname, struct addrinfo **res, const char *canonname)
1207 const struct afd *afd;
1208 struct addrinfo *ai, ai0;
1210 char pton[PTON_MAX];
1215 afd = find_afd(pai->ai_family);
1219 switch (afd->a_af) {
1222 * RFC3493 requires getaddrinfo() to accept AF_INET formats
1223 * that are accepted by inet_addr() and its family. The
1224 * accepted forms includes the "classful" one, which inet_pton
1225 * does not accept. So we need to separate the case for
1228 if (inet_aton(hostname, (struct in_addr *)pton) != 1)
1232 if (inet_pton(afd->a_af, hostname, pton) != 1) {
1233 if (pai->ai_family != AF_INET6 ||
1234 (pai->ai_flags & AI_V4MAPPED) != AI_V4MAPPED)
1236 if (inet_aton(hostname, (struct in_addr *)pton) != 1)
1238 afd = &afdl[N_INET];
1240 ai0.ai_family = AF_INET;
1246 if (pai->ai_family == afd->a_af) {
1247 GET_AI(ai, afd, pton);
1248 GET_PORT(ai, servname);
1249 if ((pai->ai_flags & AI_CANONNAME)) {
1251 * Set the numeric address itself as the canonical
1252 * name, based on a clarification in RFC3493.
1254 GET_CANONNAME(ai, canonname);
1258 * XXX: This should not happen since we already matched the AF
1275 * numeric hostname with scope
1278 explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
1279 const char *servname, struct addrinfo **res)
1281 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
1282 return explore_numeric(pai, hostname, servname, res, hostname);
1284 const struct afd *afd;
1285 struct addrinfo *cur;
1287 char *cp, *hostname2 = NULL, *scope, *addr;
1288 struct sockaddr_in6 *sin6;
1290 afd = find_afd(pai->ai_family);
1295 return explore_numeric(pai, hostname, servname, res, hostname);
1297 cp = strchr(hostname, SCOPE_DELIMITER);
1299 return explore_numeric(pai, hostname, servname, res, hostname);
1302 * Handle special case of <scoped_address><delimiter><scope id>
1304 hostname2 = strdup(hostname);
1305 if (hostname2 == NULL)
1307 /* terminate at the delimiter */
1308 hostname2[cp - hostname] = '\0';
1312 error = explore_numeric(pai, addr, servname, res, hostname);
1316 for (cur = *res; cur; cur = cur->ai_next) {
1317 if (cur->ai_family != AF_INET6)
1319 sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1320 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1324 return(EAI_NONAME); /* XXX: is return OK? */
1326 sin6->sin6_scope_id = scopeid;
1332 if (error && *res) {
1341 get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
1343 if ((pai->ai_flags & AI_CANONNAME) != 0) {
1344 ai->ai_canonname = strdup(str);
1345 if (ai->ai_canonname == NULL)
1351 static struct addrinfo *
1352 get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
1355 struct addrinfo *ai;
1357 struct in6_addr faith_prefix;
1362 struct in6_addr mapaddr;
1367 * Transfrom an IPv4 addr into a special IPv6 addr format for
1368 * IPv6->IPv4 translation gateway. (only TCP is supported now)
1370 * +-----------------------------------+------------+
1371 * | faith prefix part (12 bytes) | embedded |
1372 * | | IPv4 addr part (4 bytes)
1373 * +-----------------------------------+------------+
1375 * faith prefix part is specified as ascii IPv6 addr format
1376 * in environmental variable GAI.
1377 * For FAITH to work correctly, routing to faith prefix must be
1378 * setup toward a machine where a FAITH daemon operates.
1379 * Also, the machine must enable some mechanizm
1380 * (e.g. faith interface hack) to divert those packet with
1381 * faith prefixed destination addr to user-land FAITH daemon.
1383 fp_str = getenv("GAI");
1384 if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
1385 afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
1389 memcpy(&v4a, addr, sizeof v4a);
1390 v4a_top = v4a >> IN_CLASSA_NSHIFT;
1391 if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
1392 v4a_top != 0 && v4a != IN_LOOPBACKNET) {
1393 afd = &afdl[N_INET6];
1394 memcpy(&faith_prefix.s6_addr[12], addr,
1395 sizeof(struct in_addr));
1402 if (afd->a_af == AF_INET && (pai->ai_flags & AI_V4MAPPED) != 0) {
1403 afd = &afdl[N_INET6];
1404 _map_v4v6_address(addr, (char *)&mapaddr);
1405 addr = (char *)&mapaddr;
1409 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
1410 + (afd->a_socklen));
1414 memcpy(ai, pai, sizeof(struct addrinfo));
1415 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1416 memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
1417 ai->ai_addr->sa_len = afd->a_socklen;
1418 ai->ai_addrlen = afd->a_socklen;
1419 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1420 p = (char *)(void *)(ai->ai_addr);
1423 memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
1426 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1430 /* XXX need to malloc() the same way we do from other functions! */
1431 static struct addrinfo *
1432 copy_ai(const struct addrinfo *pai)
1434 struct addrinfo *ai;
1437 l = sizeof(*ai) + pai->ai_addrlen;
1438 if ((ai = (struct addrinfo *)malloc(l)) == NULL)
1441 memcpy(ai, pai, sizeof(*ai));
1442 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1443 memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen);
1445 if (pai->ai_canonname) {
1446 l = strlen(pai->ai_canonname) + 1;
1447 if ((ai->ai_canonname = malloc(l)) == NULL) {
1451 strlcpy(ai->ai_canonname, pai->ai_canonname, l);
1453 /* just to make sure */
1454 ai->ai_canonname = NULL;
1463 get_portmatch(const struct addrinfo *ai, const char *servname)
1466 /* get_port does not touch first argument when matchonly == 1. */
1467 /* LINTED const cast */
1468 return get_port((struct addrinfo *)ai, servname, 1);
1472 get_port(struct addrinfo *ai, const char *servname, int matchonly)
1479 if (servname == NULL)
1481 switch (ai->ai_family) {
1491 switch (ai->ai_socktype) {
1496 case SOCK_SEQPACKET:
1500 switch (ai->ai_family) {
1513 return EAI_SOCKTYPE;
1516 error = str2number(servname, &port);
1520 if (port < 0 || port > 65535)
1524 if (ai->ai_flags & AI_NUMERICSERV)
1527 switch (ai->ai_protocol) {
1537 case IPPROTO_UDPLITE:
1545 if ((sp = getservbyname(servname, proto)) == NULL)
1551 switch (ai->ai_family) {
1553 ((struct sockaddr_in *)(void *)
1554 ai->ai_addr)->sin_port = port;
1558 ((struct sockaddr_in6 *)(void *)
1559 ai->ai_addr)->sin6_port = port;
1568 static const struct afd *
1571 const struct afd *afd;
1573 if (af == PF_UNSPEC)
1575 for (afd = afdl; afd->a_af; afd++) {
1576 if (afd->a_af == af)
1583 * RFC 3493: AI_ADDRCONFIG check. Determines which address families are
1584 * configured on the local system and correlates with pai->ai_family value.
1585 * If an address family is not configured on the system, it will not be
1586 * queried for. For this purpose, loopback addresses are not considered
1587 * configured addresses.
1589 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1593 addrconfig(struct addrinfo *pai)
1595 struct ifaddrs *ifaddrs, *ifa;
1596 struct sockaddr_in *sin;
1598 struct sockaddr_in6 *sin6;
1600 int seen_inet = 0, seen_inet6 = 0;
1602 if (getifaddrs(&ifaddrs) != 0)
1605 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
1606 if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0)
1608 switch (ifa->ifa_addr->sa_family) {
1612 sin = (struct sockaddr_in *)(ifa->ifa_addr);
1613 if (htonl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
1621 sin6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
1622 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
1624 if ((ifa->ifa_flags & IFT_LOOP) != 0 &&
1625 IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1627 if (is_ifdisabled(ifa->ifa_name))
1634 freeifaddrs(ifaddrs);
1636 switch(pai->ai_family) {
1638 return (seen_inet6);
1642 if (seen_inet == seen_inet6)
1644 pai->ai_family = seen_inet ? AF_INET : AF_INET6;
1652 is_ifdisabled(char *name)
1654 struct in6_ndireq nd;
1657 if ((fd = _socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
1659 memset(&nd, 0, sizeof(nd));
1660 strlcpy(nd.ifname, name, sizeof(nd.ifname));
1661 if (_ioctl(fd, SIOCGIFINFO_IN6, &nd) < 0) {
1666 return ((nd.ndi.flags & ND6_IFF_IFDISABLED) != 0);
1669 /* convert a string to a scope identifier. XXX: IPv6 specific */
1671 ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1674 struct in6_addr *a6;
1677 a6 = &sin6->sin6_addr;
1679 /* empty scopeid portion is invalid */
1683 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
1684 IN6_IS_ADDR_MC_NODELOCAL(a6)) {
1686 * We currently assume a one-to-one mapping between links
1687 * and interfaces, so we simply use interface indices for
1688 * like-local scopes.
1690 *scopeid = if_nametoindex(scope);
1696 /* still unclear about literal, allow numeric only - placeholder */
1697 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1699 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1702 goto trynumeric; /* global */
1704 /* try to convert to a numeric id as a last resort */
1707 lscopeid = strtoul(scope, &ep, 10);
1708 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1709 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1719 addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap,
1725 const int op_id = 0; /* identifies the getaddrinfo for the cache */
1727 struct addrinfo *hints;
1730 int ai_flags, ai_family, ai_socktype, ai_protocol;
1731 size_t desired_size, size;
1733 statp = __res_state();
1734 res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
1735 RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
1737 hostname = va_arg(ap, char *);
1738 hints = va_arg(ap, struct addrinfo *);
1740 desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4;
1741 if (hostname != NULL) {
1742 size = strlen(hostname);
1743 desired_size += size + 1;
1747 if (desired_size > *buffer_size) {
1748 *buffer_size = desired_size;
1753 ai_flags = ai_family = ai_socktype = ai_protocol = 0;
1755 ai_flags = hints->ai_flags;
1756 ai_family = hints->ai_family;
1757 ai_socktype = hints->ai_socktype;
1758 ai_protocol = hints->ai_protocol;
1762 memcpy(p, &res_options, sizeof(res_options));
1763 p += sizeof(res_options);
1765 memcpy(p, &op_id, sizeof(int));
1768 memcpy(p, &ai_flags, sizeof(int));
1771 memcpy(p, &ai_family, sizeof(int));
1774 memcpy(p, &ai_socktype, sizeof(int));
1777 memcpy(p, &ai_protocol, sizeof(int));
1780 if (hostname != NULL)
1781 memcpy(p, hostname, size);
1783 *buffer_size = desired_size;
1784 return (NS_SUCCESS);
1788 addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval,
1789 va_list ap, void *cache_mdata)
1791 struct addrinfo *ai, *cai;
1793 size_t desired_size, size, ai_size;
1795 ai = *((struct addrinfo **)retval);
1797 desired_size = sizeof(size_t);
1799 for (cai = ai; cai != NULL; cai = cai->ai_next) {
1800 desired_size += sizeof(struct addrinfo) + cai->ai_addrlen;
1801 if (cai->ai_canonname != NULL)
1802 desired_size += sizeof(size_t) +
1803 strlen(cai->ai_canonname);
1807 if (desired_size > *buffer_size) {
1808 /* this assignment is here for future use */
1810 *buffer_size = desired_size;
1814 memset(buffer, 0, desired_size);
1817 memcpy(p, &ai_size, sizeof(size_t));
1818 p += sizeof(size_t);
1819 for (cai = ai; cai != NULL; cai = cai->ai_next) {
1820 memcpy(p, cai, sizeof(struct addrinfo));
1821 p += sizeof(struct addrinfo);
1823 memcpy(p, cai->ai_addr, cai->ai_addrlen);
1824 p += cai->ai_addrlen;
1826 if (cai->ai_canonname != NULL) {
1827 size = strlen(cai->ai_canonname);
1828 memcpy(p, &size, sizeof(size_t));
1829 p += sizeof(size_t);
1831 memcpy(p, cai->ai_canonname, size);
1836 return (NS_SUCCESS);
1840 addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
1841 va_list ap, void *cache_mdata)
1843 struct addrinfo new_ai, *result, *sentinel, *lasts;
1846 size_t ai_size, ai_i, size;
1849 memcpy(&ai_size, p, sizeof(size_t));
1850 p += sizeof(size_t);
1854 for (ai_i = 0; ai_i < ai_size; ++ai_i) {
1855 memcpy(&new_ai, p, sizeof(struct addrinfo));
1856 p += sizeof(struct addrinfo);
1857 size = new_ai.ai_addrlen + sizeof(struct addrinfo) +
1860 sentinel = (struct addrinfo *)malloc(size);
1861 memset(sentinel, 0, size);
1863 memcpy(sentinel, &new_ai, sizeof(struct addrinfo));
1864 sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel +
1865 sizeof(struct addrinfo));
1867 memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen);
1868 p += new_ai.ai_addrlen;
1870 if (new_ai.ai_canonname != NULL) {
1871 memcpy(&size, p, sizeof(size_t));
1872 p += sizeof(size_t);
1874 sentinel->ai_canonname = (char *)malloc(size + 1);
1875 memset(sentinel->ai_canonname, 0, size + 1);
1877 memcpy(sentinel->ai_canonname, p, size);
1881 if (result == NULL) {
1885 lasts->ai_next = sentinel;
1890 *((struct addrinfo **)retval) = result;
1891 return (NS_SUCCESS);
1893 #endif /* NS_CACHING */
1896 * FQDN hostname, DNS lookup
1899 explore_fqdn(const struct addrinfo *pai, const char *hostname,
1900 const char *servname, struct addrinfo **res)
1902 struct addrinfo *result;
1903 struct addrinfo *cur;
1907 static const nss_cache_info cache_info =
1908 NS_COMMON_CACHE_INFO_INITIALIZER(
1909 hosts, NULL, addrinfo_id_func, addrinfo_marshal_func,
1910 addrinfo_unmarshal_func);
1912 static const ns_dtab dtab[] = {
1913 NS_FILES_CB(_files_getaddrinfo, NULL)
1914 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
1915 NS_NIS_CB(_yp_getaddrinfo, NULL)
1917 NS_CACHE_CB(&cache_info)
1925 * if the servname does not match socktype/protocol, ignore it.
1927 if (get_portmatch(pai, servname) != 0)
1930 switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1931 default_dns_files, hostname, pai)) {
1943 for (cur = result; cur; cur = cur->ai_next) {
1944 GET_PORT(cur, servname);
1945 /* canonname should be filled already */
1956 freeaddrinfo(result);
1961 static const char AskedForGot[] =
1962 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1965 static struct addrinfo *
1966 getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
1967 const struct addrinfo *pai, res_state res)
1969 struct addrinfo sentinel, *cur;
1971 const struct afd *afd;
1978 int type, class, ancount, qdcount;
1979 int haveanswer, had_error;
1980 char tbuf[MAXDNAME];
1981 int (*name_ok)(const char *);
1982 char hostbuf[8*1024];
1984 memset(&sentinel, 0, sizeof(sentinel));
1988 eom = answer->buf + anslen;
1992 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1996 return (NULL); /* XXX should be abort(); */
1999 * find first satisfactory answer
2002 ancount = ntohs(hp->ancount);
2003 qdcount = ntohs(hp->qdcount);
2005 ep = hostbuf + sizeof hostbuf;
2006 cp = answer->buf + HFIXEDSZ;
2008 RES_SET_H_ERRNO(res, NO_RECOVERY);
2011 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
2012 if ((n < 0) || !(*name_ok)(bp)) {
2013 RES_SET_H_ERRNO(res, NO_RECOVERY);
2017 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
2018 /* res_send() has already verified that the query name is the
2019 * same as the one we sent; this just gets the expanded name
2020 * (i.e., with the succeeding search-domain tacked on).
2022 n = strlen(bp) + 1; /* for the \0 */
2023 if (n >= MAXHOSTNAMELEN) {
2024 RES_SET_H_ERRNO(res, NO_RECOVERY);
2029 /* The qname can be abbreviated, but h_name is now absolute. */
2034 while (ancount-- > 0 && cp < eom && !had_error) {
2035 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
2036 if ((n < 0) || !(*name_ok)(bp)) {
2041 type = _getshort(cp);
2042 cp += INT16SZ; /* type */
2043 class = _getshort(cp);
2044 cp += INT16SZ + INT32SZ; /* class, TTL */
2046 cp += INT16SZ; /* len */
2047 if (class != C_IN) {
2048 /* XXX - debug? syslog? */
2050 continue; /* XXX - had_error++ ? */
2052 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
2054 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
2055 if ((n < 0) || !(*name_ok)(tbuf)) {
2060 /* Get canonical name. */
2061 n = strlen(tbuf) + 1; /* for the \0 */
2062 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
2066 strlcpy(bp, tbuf, ep - bp);
2071 if (qtype == T_ANY) {
2072 if (!(type == T_A || type == T_AAAA)) {
2076 } else if (type != qtype) {
2078 if (type != T_KEY && type != T_SIG &&
2080 syslog(LOG_NOTICE|LOG_AUTH,
2081 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
2082 qname, p_class(C_IN), p_type(qtype),
2086 continue; /* XXX - had_error++ ? */
2091 if (strcasecmp(canonname, bp) != 0) {
2093 syslog(LOG_NOTICE|LOG_AUTH,
2094 AskedForGot, canonname, bp);
2097 continue; /* XXX - had_error++ ? */
2099 if (type == T_A && n != INADDRSZ) {
2103 if (type == T_AAAA && n != IN6ADDRSZ) {
2107 #ifdef FILTER_V4MAPPED
2108 if (type == T_AAAA) {
2109 struct in6_addr in6;
2110 memcpy(&in6, cp, sizeof(in6));
2111 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
2121 nn = strlen(bp) + 1; /* for the \0 */
2125 /* don't overwrite pai */
2127 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
2128 afd = find_afd(ai.ai_family);
2133 cur->ai_next = get_ai(&ai, afd, (const char *)cp);
2134 if (cur->ai_next == NULL)
2136 while (cur && cur->ai_next)
2147 #if defined(RESOLVSORT)
2149 * We support only IPv4 address for backward
2150 * compatibility against gethostbyname(3).
2152 if (res->nsort && qtype == T_A) {
2153 if (addr4sort(&sentinel, res) < 0) {
2154 freeaddrinfo(sentinel.ai_next);
2155 RES_SET_H_ERRNO(res, NO_RECOVERY);
2159 #endif /*RESOLVSORT*/
2161 (void)get_canonname(pai, sentinel.ai_next, qname);
2163 (void)get_canonname(pai, sentinel.ai_next, canonname);
2164 RES_SET_H_ERRNO(res, NETDB_SUCCESS);
2165 return sentinel.ai_next;
2169 * We could have walked a CNAME chain, but the ultimate target
2170 * may not have what we looked for.
2172 RES_SET_H_ERRNO(res, ntohs(hp->ancount) > 0 ? NO_DATA : NO_RECOVERY);
2178 struct addrinfo *ai;
2183 addr4sort(struct addrinfo *sentinel, res_state res)
2185 struct addrinfo *ai;
2186 struct addr_ptr *addrs, addr;
2187 struct sockaddr_in *sin;
2194 for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
2197 return 0; /* We don't need sorting. */
2198 if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
2201 for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
2202 sin = (struct sockaddr_in *)ai->ai_addr;
2203 for (j = 0; (unsigned)j < res->nsort; j++) {
2204 if (res->sort_list[j].addr.s_addr ==
2205 (sin->sin_addr.s_addr & res->sort_list[j].mask))
2210 if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
2219 while (needsort < naddrs) {
2220 for (j = needsort - 1; j >= 0; j--) {
2221 if (addrs[j].aval > addrs[j+1].aval) {
2223 addrs[j] = addrs[j + 1];
2224 addrs[j + 1] = addr;
2232 for (i = 0; i < naddrs; ++i) {
2233 ai->ai_next = addrs[i].ai;
2240 #endif /*RESOLVSORT*/
2244 _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2246 struct addrinfo *ai, ai0;
2247 querybuf *buf, *buf2;
2248 const char *hostname;
2249 const struct addrinfo *pai;
2250 struct addrinfo sentinel, *cur;
2251 struct res_target q, q2;
2256 hostname = va_arg(ap, char *);
2257 pai = va_arg(ap, const struct addrinfo *);
2259 memset(&q, 0, sizeof(q));
2260 memset(&q2, 0, sizeof(q2));
2261 memset(&sentinel, 0, sizeof(sentinel));
2264 res = __res_state();
2266 buf = malloc(sizeof(*buf));
2268 RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2271 buf2 = malloc(sizeof(*buf2));
2274 RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2278 if (pai->ai_family == AF_INET6 &&
2279 (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) {
2281 ai0.ai_family = AF_UNSPEC;
2285 switch (pai->ai_family) {
2290 q.answer = buf->buf;
2291 q.anslen = sizeof(buf->buf);
2296 q2.answer = buf2->buf;
2297 q2.anslen = sizeof(buf2->buf);
2303 q.answer = buf->buf;
2304 q.anslen = sizeof(buf->buf);
2310 q.answer = buf->buf;
2311 q.anslen = sizeof(buf->buf);
2319 if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
2320 RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2326 if (res_searchN(hostname, &q, res) < 0) {
2333 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
2336 while (cur && cur->ai_next)
2340 if (ai == NULL || pai->ai_family != AF_UNSPEC ||
2341 (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) != AI_V4MAPPED) {
2342 ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
2348 if (sentinel.ai_next == NULL)
2349 switch (res->res_h_errno) {
2350 case HOST_NOT_FOUND:
2358 *((struct addrinfo **)rv) = sentinel.ai_next;
2363 _sethtent(FILE **hostf)
2366 *hostf = fopen(_PATH_HOSTS, "re");
2372 _endhtent(FILE **hostf)
2375 (void) fclose(*hostf);
2380 static struct addrinfo *
2381 _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2384 char *cp, *tname, *cname;
2385 struct addrinfo hints, *res0, *res;
2388 char hostbuf[8*1024];
2390 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
2393 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2397 cp = strpbrk(p, "#\n");
2400 if (!(cp = strpbrk(p, " \t")))
2405 /* if this is not something we're looking for, skip it. */
2407 if (*cp == ' ' || *cp == '\t') {
2414 if ((cp = strpbrk(cp, " \t")) != NULL)
2416 if (strcasecmp(name, tname) == 0)
2422 /* we should not glob socktype/protocol here */
2423 memset(&hints, 0, sizeof(hints));
2424 hints.ai_family = pai->ai_family;
2425 hints.ai_socktype = SOCK_DGRAM;
2426 hints.ai_protocol = 0;
2427 hints.ai_flags = AI_NUMERICHOST;
2428 if (pai->ai_family == AF_INET6 &&
2429 (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
2430 hints.ai_flags |= AI_V4MAPPED;
2431 error = getaddrinfo(addr, "0", &hints, &res0);
2434 #ifdef FILTER_V4MAPPED
2435 /* XXX should check all items in the chain */
2436 if (res0->ai_family == AF_INET6 &&
2437 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2442 for (res = res0; res; res = res->ai_next) {
2444 res->ai_flags = pai->ai_flags;
2445 res->ai_socktype = pai->ai_socktype;
2446 res->ai_protocol = pai->ai_protocol;
2448 if (pai->ai_flags & AI_CANONNAME) {
2449 if (get_canonname(pai, res, cname) != 0) {
2458 static struct addrinfo *
2459 _getht(FILE **hostf, const char *name, const struct addrinfo *pai,
2460 struct addrinfo *cur)
2464 while ((p = _gethtent(hostf, name, pai)) != NULL) {
2466 while (cur && cur->ai_next)
2474 _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2477 const struct addrinfo *pai;
2478 struct addrinfo sentinel, *cur;
2481 name = va_arg(ap, char *);
2482 pai = va_arg(ap, struct addrinfo *);
2484 memset(&sentinel, 0, sizeof(sentinel));
2488 if (pai->ai_family == AF_INET6 &&
2489 (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) == AI_V4MAPPED) {
2490 struct addrinfo ai0 = *pai;
2492 ai0.ai_flags &= ~AI_V4MAPPED;
2493 cur = _getht(&hostf, name, &ai0, cur);
2494 if (sentinel.ai_next == NULL) {
2496 ai0.ai_flags |= AI_V4MAPPED;
2497 cur = _getht(&hostf, name, &ai0, cur);
2500 cur = _getht(&hostf, name, pai, cur);
2503 *((struct addrinfo **)rv) = sentinel.ai_next;
2504 if (sentinel.ai_next == NULL)
2511 static struct addrinfo *
2512 _yphostent(char *line, const struct addrinfo *pai)
2514 struct addrinfo sentinel, *cur;
2515 struct addrinfo hints, *res, *res0;
2518 const char *addr, *canonname;
2522 addr = canonname = NULL;
2524 memset(&sentinel, 0, sizeof(sentinel));
2528 /* terminate line */
2529 cp = strchr(p, '\n');
2536 cp = strpbrk(p, " \t");
2538 if (canonname == NULL)
2548 if (*cp == ' ' || *cp == '\t') {
2554 if ((cp = strpbrk(cp, " \t")) != NULL)
2559 hints.ai_flags = AI_NUMERICHOST;
2560 if (pai->ai_family == AF_INET6 &&
2561 (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED)
2562 hints.ai_flags |= AI_V4MAPPED;
2563 error = getaddrinfo(addr, NULL, &hints, &res0);
2565 for (res = res0; res; res = res->ai_next) {
2567 res->ai_flags = pai->ai_flags;
2569 if (pai->ai_flags & AI_CANONNAME)
2570 (void)get_canonname(pai, res, canonname);
2575 cur->ai_next = res0;
2576 while (cur && cur->ai_next)
2586 return sentinel.ai_next;
2591 _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2593 struct addrinfo sentinel, *cur;
2594 struct addrinfo *ai = NULL;
2598 const struct addrinfo *pai;
2601 if (_yp_check(&ypdomain) == 0)
2604 name = va_arg(ap, char *);
2605 pai = va_arg(ap, const struct addrinfo *);
2607 memset(&sentinel, 0, sizeof(sentinel));
2610 /* ipnodes.byname can hold both IPv4/v6 */
2611 r = yp_match(ypdomain, "ipnodes.byname", name,
2612 (int)strlen(name), &ypbuf, &ypbuflen);
2614 ai = _yphostent(ypbuf, pai);
2617 while (cur && cur->ai_next)
2624 struct sockaddr_in6 *sin6;
2626 switch (ai->ai_family) {
2630 sin6 = (struct sockaddr_in6 *)ai->ai_addr;
2631 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2637 /* hosts.byname is only for IPv4 (Solaris8) */
2638 if (pai->ai_family == AF_UNSPEC || pai->ai_family == AF_INET ||
2639 ((pai->ai_family == AF_INET6 &&
2640 (pai->ai_flags & AI_V4MAPPED) == AI_V4MAPPED) &&
2641 (ai == NULL || (pai->ai_flags & AI_ALL) == AI_ALL))) {
2642 r = yp_match(ypdomain, "hosts.byname", name,
2643 (int)strlen(name), &ypbuf, &ypbuflen);
2645 struct addrinfo ai4;
2648 if (pai->ai_family == AF_UNSPEC)
2649 ai4.ai_family = AF_INET;
2650 ai = _yphostent(ypbuf, &ai4);
2653 while (cur && cur->ai_next)
2661 if (sentinel.ai_next == NULL) {
2662 RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2665 *((struct addrinfo **)rv) = sentinel.ai_next;
2670 /* resolver logic */
2673 * Formulate a normal query, send, and await answer.
2674 * Returned answer is placed in supplied buffer "answer".
2675 * Perform preliminary check of answer, returning success only
2676 * if no error is indicated and the answer count is nonzero.
2677 * Return the size of the response on success, -1 on error.
2678 * Error number is left in h_errno.
2680 * Caller must parse answer and determine whether it answers the question.
2683 res_queryN(const char *name, struct res_target *target, res_state res)
2689 struct res_target *t;
2696 buf = malloc(MAXPACKET);
2698 RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2702 for (t = target; t; t = t->next) {
2707 hp = (HEADER *)(void *)t->answer;
2709 /* make it easier... */
2715 oflags = res->_flags;
2718 hp->rcode = NOERROR; /* default */
2721 if (res->options & RES_DEBUG)
2722 printf(";; res_query(%s, %d, %d)\n", name, class, type);
2725 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
2727 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
2728 (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
2729 n = res_nopt(res, n, buf, MAXPACKET, anslen);
2732 if (res->options & RES_DEBUG)
2733 printf(";; res_query: mkquery failed\n");
2736 RES_SET_H_ERRNO(res, NO_RECOVERY);
2739 n = res_nsend(res, buf, n, answer, anslen);
2742 * if the query choked with EDNS0, retry
2745 if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC))
2747 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
2748 res->_flags |= RES_F_EDNS0ERR;
2749 if (res->options & RES_DEBUG)
2750 printf(";; res_nquery: retry without EDNS0\n");
2753 rcode = hp->rcode; /* record most recent error */
2755 if (res->options & RES_DEBUG)
2756 printf(";; res_query: send error\n");
2762 hp->rcode = FORMERR; /* XXX not very informative */
2763 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2764 rcode = hp->rcode; /* record most recent error */
2766 if (res->options & RES_DEBUG)
2767 printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2768 ntohs(hp->ancount));
2773 ancount += ntohs(hp->ancount);
2783 RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2786 RES_SET_H_ERRNO(res, TRY_AGAIN);
2789 RES_SET_H_ERRNO(res, NO_DATA);
2795 RES_SET_H_ERRNO(res, NO_RECOVERY);
2804 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2805 * Return the size of the response on success, -1 on error.
2806 * If enabled, implement search rules until answer or unrecoverable failure
2807 * is detected. Error code, if any, is left in h_errno.
2810 res_searchN(const char *name, struct res_target *target, res_state res)
2812 const char *cp, * const *domain;
2813 HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/
2815 int trailing_dot, ret, saved_herrno;
2816 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2817 int tried_as_is = 0;
2819 char abuf[MAXDNAME];
2822 RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2824 for (cp = name; *cp; cp++)
2825 dots += (*cp == '.');
2827 if (cp > name && *--cp == '.')
2831 * if there aren't any dots, it could be a user-level alias
2834 (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
2835 return (res_queryN(cp, target, res));
2838 * If there are enough dots in the name, let's just give it a
2839 * try 'as is'. The threshold can be set with the "ndots" option.
2840 * Also, query 'as is', if there is a trailing dot in the name.
2843 if (dots >= res->ndots || trailing_dot) {
2844 ret = res_querydomainN(name, NULL, target, res);
2845 if (ret > 0 || trailing_dot)
2847 if (errno == ECONNREFUSED) {
2848 RES_SET_H_ERRNO(res, TRY_AGAIN);
2851 switch (res->res_h_errno) {
2853 case HOST_NOT_FOUND:
2856 if (hp->rcode == SERVFAIL)
2862 saved_herrno = res->res_h_errno;
2867 * We do at least one level of search if
2868 * - there is no dot and RES_DEFNAME is set, or
2869 * - there is at least one dot, there is no trailing dot,
2870 * and RES_DNSRCH is set.
2872 if ((!dots && (res->options & RES_DEFNAMES)) ||
2873 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2876 for (domain = (const char * const *)res->dnsrch;
2881 if (domain[0][0] == '\0' ||
2882 (domain[0][0] == '.' && domain[0][1] == '\0'))
2885 if (root_on_list && tried_as_is)
2888 ret = res_querydomainN(name, *domain, target, res);
2893 * If no server present, give up.
2894 * If name isn't found in this domain,
2895 * keep trying higher domains in the search list
2896 * (if that's enabled).
2897 * On a NO_DATA error, keep trying, otherwise
2898 * a wildcard entry of another type could keep us
2899 * from finding this entry higher in the domain.
2900 * If we get some other error (negative answer or
2901 * server failure), then stop searching up,
2902 * but try the input name below in case it's
2905 if (errno == ECONNREFUSED) {
2906 RES_SET_H_ERRNO(res, TRY_AGAIN);
2910 switch (res->res_h_errno) {
2914 case HOST_NOT_FOUND:
2919 if (hp->rcode == SERVFAIL) {
2920 /* try next search element, if any */
2925 /* anything else implies that we're done */
2929 * if we got here for some reason other than DNSRCH,
2930 * we only wanted one iteration of the loop, so stop.
2932 if (!(res->options & RES_DNSRCH))
2937 switch (res->res_h_errno) {
2939 case HOST_NOT_FOUND:
2942 if (hp->rcode == SERVFAIL)
2950 * If the query has not already been tried as is then try it
2951 * unless RES_NOTLDQUERY is set and there were no dots.
2953 if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
2954 !(tried_as_is || root_on_list)) {
2955 ret = res_querydomainN(name, NULL, target, res);
2961 * if we got here, we didn't satisfy the search.
2962 * if we did an initial full query, return that query's h_errno
2963 * (note that we wouldn't be here if that query had succeeded).
2964 * else if we ever got a nodata, send that back as the reason.
2965 * else send back meaningless h_errno, that being the one from
2966 * the last DNSRCH we did.
2969 if (saved_herrno != -1)
2970 RES_SET_H_ERRNO(res, saved_herrno);
2971 else if (got_nodata)
2972 RES_SET_H_ERRNO(res, NO_DATA);
2973 else if (got_servfail)
2974 RES_SET_H_ERRNO(res, TRY_AGAIN);
2979 * Perform a call on res_query on the concatenation of name and domain,
2980 * removing a trailing dot from name if domain is NULL.
2983 res_querydomainN(const char *name, const char *domain,
2984 struct res_target *target, res_state res)
2986 char nbuf[MAXDNAME];
2987 const char *longname = nbuf;
2991 if (res->options & RES_DEBUG)
2992 printf(";; res_querydomain(%s, %s)\n",
2993 name, domain?domain:"<Nil>");
2995 if (domain == NULL) {
2997 * Check for trailing '.';
2998 * copy without '.' if present.
3001 if (n >= MAXDNAME) {
3002 RES_SET_H_ERRNO(res, NO_RECOVERY);
3005 if (n > 0 && name[--n] == '.') {
3006 strncpy(nbuf, name, n);
3013 if (n + d + 1 >= MAXDNAME) {
3014 RES_SET_H_ERRNO(res, NO_RECOVERY);
3017 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
3019 return (res_queryN(longname, target, res));