]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/getaddrinfo.c
1 /* $NetBSD: getaddrinfo.c,v 1.91.6.1 2009/01/26 00:27:34 snj Exp $ */
2 /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Issues to be discussed:
35 * - Return values. There are nonstandard return values defined and used
36 * in the source code. This is because RFC2553 is silent about which error
37 * code must be returned for which situation.
38 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
39 * says to use inet_aton() to convert IPv4 numeric to binary (alows
40 * classful form as a result).
41 * current code - disallow classful form for IPv4 (due to use of inet_pton).
42 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
44 * current code - SEGV on freeaddrinfo(NULL)
46 * - The code filters out AFs that are not supported by the kernel,
47 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
48 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
50 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
51 * (1) what should we do against numeric hostname (2) what should we do
52 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
53 * non-loopback address configured? global address configured?
56 #include <sys/cdefs.h>
57 #if defined(LIBC_SCCS) && !defined(lint)
58 __RCSID("$NetBSD: getaddrinfo.c,v 1.91.6.1 2009/01/26 00:27:34 snj Exp $");
59 #endif /* LIBC_SCCS and not lint */
63 #include "namespace.h"
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/socket.h>
68 #include <netinet/in.h>
69 #include <arpa/inet.h>
70 #include <arpa/nameser.h>
87 //#include <rpc/rpc.h>
88 //#include <rpcsvc/yp_prot.h>
89 //#include <rpcsvc/ypclnt.h>
92 #include <net/servent.h>
94 #define endservent_r(svd) endservent()
95 #define nsdispatch(pResult,dtab,database,routine,files,hostname,pai) NS_NOTFOUND
96 #define res_nmkquery(state,op,dname,class,type,data,datalen,newrr_in,buf,buflen) res_mkquery( op, dname, class, type, data, datalen, newrr_in, buf, buflen )
97 #define res_nsend(state,buf,buflen,ans,anssiz) res_send ( buf, buflen, ans, anssiz )
99 /* Things involving an internal (static) resolver context. */
101 #define __res_get_state() (( 0 != _res.nscount ) ? &_res : NULL )
102 #define __res_put_state(state)
103 #define __res_state() _res
107 __weak_alias(getaddrinfo
,_getaddrinfo
)
108 __weak_alias(freeaddrinfo
,_freeaddrinfo
)
109 __weak_alias(gai_strerror
,_gai_strerror
)
117 static const char in_addrany
[] = { 0, 0, 0, 0 };
118 static const char in_loopback
[] = { 127, 0, 0, 1 };
120 static const char in6_addrany
[] = {
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
123 static const char in6_loopback
[] = {
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
128 static const struct afd
{
133 const char *a_addrany
;
134 const char *a_loopback
;
138 {PF_INET6
, sizeof(struct in6_addr
),
139 sizeof(struct sockaddr_in6
),
140 offsetof(struct sockaddr_in6
, sin6_addr
),
141 in6_addrany
, in6_loopback
, 1},
143 {PF_INET
, sizeof(struct in_addr
),
144 sizeof(struct sockaddr_in
),
145 offsetof(struct sockaddr_in
, sin_addr
),
146 in_addrany
, in_loopback
, 0},
147 {0, 0, 0, 0, NULL
, NULL
, 0},
154 const char *e_protostr
;
156 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
157 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
158 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
161 static const struct explore explore
[] = {
163 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
166 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
167 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
168 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
170 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
171 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
172 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
173 { PF_UNSPEC
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
174 { PF_UNSPEC
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
175 { PF_UNSPEC
, SOCK_RAW
, ANY
, NULL
, 0x05 },
176 { -1, 0, 0, NULL
, 0 },
185 static const ns_src default_dns_files
[] = {
186 { NSSRC_FILES
, NS_SUCCESS
},
187 { NSSRC_DNS
, NS_SUCCESS
},
191 #define MAXPACKET (64*1024)
195 u_char buf
[MAXPACKET
];
199 struct res_target
*next
;
200 const char *name
; /* domain name */
201 int qclass
, qtype
; /* class and type of query */
202 u_char
*answer
; /* buffer to put answer */
203 int anslen
; /* size of answer buffer */
204 int n
; /* result length */
207 static int str2number(const char *);
208 static int explore_fqdn(const struct addrinfo
*, const char *,
209 const char *, struct addrinfo
**, struct servent_data
*);
210 static int explore_null(const struct addrinfo
*,
211 const char *, struct addrinfo
**, struct servent_data
*);
212 static int explore_numeric(const struct addrinfo
*, const char *,
213 const char *, struct addrinfo
**, const char *, struct servent_data
*);
214 static int explore_numeric_scope(const struct addrinfo
*, const char *,
215 const char *, struct addrinfo
**, struct servent_data
*);
216 static int get_canonname(const struct addrinfo
*,
217 struct addrinfo
*, const char *);
218 static struct addrinfo
*get_ai(const struct addrinfo
*,
219 const struct afd
*, const char *);
220 static int get_portmatch(const struct addrinfo
*, const char *,
221 struct servent_data
*);
222 static int get_port(const struct addrinfo
*, const char *, int,
223 struct servent_data
*);
224 static const struct afd
*find_afd(int);
226 static int ip6_str2scopeid(char *, struct sockaddr_in6
*, u_int32_t
*);
229 static struct addrinfo
*getanswer(const querybuf
*, int, const char *, int,
230 const struct addrinfo
*);
231 static void aisort(struct addrinfo
*s
, res_state res
);
232 static int _dns_getaddrinfo(void *, void *, va_list);
233 static void _sethtent(FILE **);
234 static void _endhtent(FILE **);
235 static struct addrinfo
*_gethtent(FILE **, const char *,
236 const struct addrinfo
*);
237 static int _files_getaddrinfo(void *, void *, va_list);
239 static struct addrinfo
*_yphostent(char *, const struct addrinfo
*);
240 static int _yp_getaddrinfo(void *, void *, va_list);
243 static int res_queryN(const char *, struct res_target
*, res_state
);
244 static int res_searchN(const char *, struct res_target
*, res_state
);
245 static int res_querydomainN(const char *, const char *,
246 struct res_target
*, res_state
);
248 static const char * const ai_errlist
[] = {
250 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
251 "Temporary failure in name resolution", /* EAI_AGAIN */
252 "Invalid value for ai_flags", /* EAI_BADFLAGS */
253 "Non-recoverable failure in name resolution", /* EAI_FAIL */
254 "ai_family not supported", /* EAI_FAMILY */
255 "Memory allocation failure", /* EAI_MEMORY */
256 "No address associated with hostname", /* EAI_NODATA */
257 "hostname nor servname provided, or not known", /* EAI_NONAME */
258 "servname not supported for ai_socktype", /* EAI_SERVICE */
259 "ai_socktype not supported", /* EAI_SOCKTYPE */
260 "System error returned in errno", /* EAI_SYSTEM */
261 "Invalid value for hints", /* EAI_BADHINTS */
262 "Resolved protocol is unknown", /* EAI_PROTOCOL */
263 "Argument buffer overflow", /* EAI_OVERFLOW */
264 "Unknown error", /* EAI_MAX */
267 /* XXX macros that make external reference is BAD. */
269 #define GET_AI(ai, afd, addr) \
271 /* external reference: pai, error, and label free */ \
272 (ai) = get_ai(pai, (afd), (addr)); \
273 if ((ai) == NULL) { \
274 error = EAI_MEMORY; \
277 } while (/*CONSTCOND*/0)
279 #define GET_PORT(ai, serv, svd) \
281 /* external reference: error and label free */ \
282 error = get_port((ai), (serv), 0, (svd)); \
285 } while (/*CONSTCOND*/0)
287 #define GET_CANONNAME(ai, str) \
289 /* external reference: pai, error and label free */ \
290 error = get_canonname(pai, (ai), (str)); \
293 } while (/*CONSTCOND*/0)
297 /* external reference: error, and label bad */ \
301 } while (/*CONSTCOND*/0)
303 #define MATCH_FAMILY(x, y, w) \
304 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \
306 #define MATCH(x, y, w) \
307 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
310 gai_strerror(int ecode
)
312 if (ecode
< 0 || ecode
> EAI_MAX
)
314 return ai_errlist
[ecode
];
318 freeaddrinfo(struct addrinfo
*ai
)
320 struct addrinfo
*next
;
322 _DIAGASSERT(ai
!= NULL
);
326 if (ai
->ai_canonname
)
327 free(ai
->ai_canonname
);
328 /* no need to free(ai->ai_addr) */
335 str2number(const char *p
)
340 _DIAGASSERT(p
!= NULL
);
346 v
= strtoul(p
, &ep
, 10);
347 if (errno
== 0 && ep
&& *ep
== '\0' && v
<= UINT_MAX
)
354 getaddrinfo(const char *hostname
, const char *servname
,
355 const struct addrinfo
*hints
, struct addrinfo
**res
)
357 struct addrinfo sentinel
;
358 struct addrinfo
*cur
;
362 struct addrinfo
*pai
;
363 const struct explore
*ex
;
364 struct servent_data svd
;
366 /* hostname is allowed to be NULL */
367 /* servname is allowed to be NULL */
368 /* hints is allowed to be NULL */
369 _DIAGASSERT(res
!= NULL
);
371 (void)memset(&svd
, 0, sizeof(svd
));
372 memset(&sentinel
, 0, sizeof(sentinel
));
374 memset(&ai
, 0, sizeof(ai
));
377 pai
->ai_family
= PF_UNSPEC
;
378 pai
->ai_socktype
= ANY
;
379 pai
->ai_protocol
= ANY
;
381 pai
->ai_canonname
= NULL
;
385 if (hostname
== NULL
&& servname
== NULL
)
388 /* error check for hints */
389 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
390 hints
->ai_addr
|| hints
->ai_next
)
391 ERR(EAI_BADHINTS
); /* xxx */
392 if (hints
->ai_flags
& ~AI_MASK
)
394 switch (hints
->ai_family
) {
404 memcpy(pai
, hints
, sizeof(*pai
));
407 * if both socktype/protocol are specified, check if they
408 * are meaningful combination.
410 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
411 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
412 if (pai
->ai_family
!= ex
->e_af
)
414 if (ex
->e_socktype
== ANY
)
416 if (ex
->e_protocol
== ANY
)
418 if (pai
->ai_socktype
== ex
->e_socktype
419 && pai
->ai_protocol
!= ex
->e_protocol
) {
427 * check for special cases. (1) numeric servname is disallowed if
428 * socktype/protocol are left unspecified. (2) servname is disallowed
429 * for raw and other inet{,6} sockets.
431 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
433 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
436 ai0
= *pai
; /* backup *pai */
438 if (pai
->ai_family
== PF_UNSPEC
) {
440 pai
->ai_family
= PF_INET6
;
442 pai
->ai_family
= PF_INET
;
445 error
= get_portmatch(pai
, servname
, &svd
);
454 /* NULL hostname, or numeric hostname */
455 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
458 /* PF_UNSPEC entries are prepared for DNS queries only */
459 if (ex
->e_af
== PF_UNSPEC
)
462 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
464 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
466 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
469 if (pai
->ai_family
== PF_UNSPEC
)
470 pai
->ai_family
= ex
->e_af
;
471 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
472 pai
->ai_socktype
= ex
->e_socktype
;
473 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
474 pai
->ai_protocol
= ex
->e_protocol
;
476 if (hostname
== NULL
)
477 error
= explore_null(pai
, servname
, &cur
->ai_next
,
480 error
= explore_numeric_scope(pai
, hostname
, servname
,
481 &cur
->ai_next
, &svd
);
492 * If numeric representation of AF1 can be interpreted as FQDN
493 * representation of AF2, we need to think again about the code below.
495 if (sentinel
.ai_next
)
498 if (hostname
== NULL
)
500 if (pai
->ai_flags
& AI_NUMERICHOST
)
504 * hostname as alphabetical name.
505 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
508 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
511 /* require exact match for family field */
512 if (pai
->ai_family
!= ex
->e_af
)
515 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
516 WILD_SOCKTYPE(ex
))) {
519 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
520 WILD_PROTOCOL(ex
))) {
524 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
525 pai
->ai_socktype
= ex
->e_socktype
;
526 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
527 pai
->ai_protocol
= ex
->e_protocol
;
529 error
= explore_fqdn(pai
, hostname
, servname
, &cur
->ai_next
,
532 while (cur
&& cur
->ai_next
)
537 if (sentinel
.ai_next
)
543 if (sentinel
.ai_next
) {
546 *res
= sentinel
.ai_next
;
553 if (sentinel
.ai_next
)
554 freeaddrinfo(sentinel
.ai_next
);
559 static const ns_dtab dtab
[] = {
560 NS_FILES_CB(((nss_method
)_files_getaddrinfo
), NULL
)
561 { NSSRC_DNS
, ((nss_method
)_dns_getaddrinfo
), NULL
}, /* force -DHESIOD */
562 NS_NIS_CB(_yp_getaddrinfo
, NULL
)
567 * FQDN hostname, DNS lookup
570 explore_fqdn(const struct addrinfo
*pai
, const char *hostname
,
571 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
573 struct addrinfo
*result
;
574 struct addrinfo
*cur
;
577 _DIAGASSERT(pai
!= NULL
);
578 /* hostname may be NULL */
579 /* servname may be NULL */
580 _DIAGASSERT(res
!= NULL
);
585 * if the servname does not match socktype/protocol, ignore it.
587 if (get_portmatch(pai
, servname
, svd
) != 0)
590 switch (nsdispatch(&result
, dtab
, NSDB_HOSTS
, "getaddrinfo",
591 default_dns_files
, hostname
, pai
)) {
603 for (cur
= result
; cur
; cur
= cur
->ai_next
) {
604 GET_PORT(cur
, servname
, svd
);
605 /* canonname should be filled already */
616 freeaddrinfo(result
);
622 * passive socket -> anyaddr (0.0.0.0 or ::)
623 * non-passive socket -> localhost (127.0.0.1 or ::1)
626 explore_null(const struct addrinfo
*pai
, const char *servname
,
627 struct addrinfo
**res
, struct servent_data
*svd
)
630 const struct afd
*afd
;
631 struct addrinfo
*cur
;
632 struct addrinfo sentinel
;
635 _DIAGASSERT(pai
!= NULL
);
636 /* servname may be NULL */
637 _DIAGASSERT(res
!= NULL
);
640 sentinel
.ai_next
= NULL
;
644 * filter out AFs that are not supported by the kernel
647 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
655 * if the servname does not match socktype/protocol, ignore it.
657 if (get_portmatch(pai
, servname
, svd
) != 0)
660 afd
= find_afd(pai
->ai_family
);
664 if (pai
->ai_flags
& AI_PASSIVE
) {
665 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
667 * GET_CANONNAME(cur->ai_next, "anyaddr");
669 GET_PORT(cur
->ai_next
, servname
, svd
);
671 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
673 * GET_CANONNAME(cur->ai_next, "localhost");
675 GET_PORT(cur
->ai_next
, servname
, svd
);
679 *res
= sentinel
.ai_next
;
683 if (sentinel
.ai_next
)
684 freeaddrinfo(sentinel
.ai_next
);
692 explore_numeric(const struct addrinfo
*pai
, const char *hostname
,
693 const char *servname
, struct addrinfo
**res
, const char *canonname
,
694 struct servent_data
*svd
)
696 const struct afd
*afd
;
697 struct addrinfo
*cur
;
698 struct addrinfo sentinel
;
702 _DIAGASSERT(pai
!= NULL
);
703 /* hostname may be NULL */
704 /* servname may be NULL */
705 _DIAGASSERT(res
!= NULL
);
708 sentinel
.ai_next
= NULL
;
712 * if the servname does not match socktype/protocol, ignore it.
714 if (get_portmatch(pai
, servname
, svd
) != 0)
717 afd
= find_afd(pai
->ai_family
);
722 #if 0 /*X/Open spec*/
724 if (inet_aton(hostname
, (struct in_addr
*)pton
) == 1) {
725 if (pai
->ai_family
== afd
->a_af
||
726 pai
->ai_family
== PF_UNSPEC
/*?*/) {
727 GET_AI(cur
->ai_next
, afd
, pton
);
728 GET_PORT(cur
->ai_next
, servname
, svd
);
729 if ((pai
->ai_flags
& AI_CANONNAME
)) {
731 * Set the numeric address itself as
732 * the canonical name, based on a
733 * clarification in rfc2553bis-03.
735 GET_CANONNAME(cur
->ai_next
, canonname
);
737 while (cur
&& cur
->ai_next
)
740 ERR(EAI_FAMILY
); /*xxx*/
745 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
746 if (pai
->ai_family
== afd
->a_af
||
747 pai
->ai_family
== PF_UNSPEC
/*?*/) {
748 GET_AI(cur
->ai_next
, afd
, pton
);
749 GET_PORT(cur
->ai_next
, servname
, svd
);
750 if ((pai
->ai_flags
& AI_CANONNAME
)) {
752 * Set the numeric address itself as
753 * the canonical name, based on a
754 * clarification in rfc2553bis-03.
756 GET_CANONNAME(cur
->ai_next
, canonname
);
761 ERR(EAI_FAMILY
); /*xxx*/
766 *res
= sentinel
.ai_next
;
771 if (sentinel
.ai_next
)
772 freeaddrinfo(sentinel
.ai_next
);
777 * numeric hostname with scope
780 explore_numeric_scope(const struct addrinfo
*pai
, const char *hostname
,
781 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
783 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
784 return explore_numeric(pai
, hostname
, servname
, res
, hostname
, svd
);
786 const struct afd
*afd
;
787 struct addrinfo
*cur
;
789 char *cp
, *hostname2
= NULL
, *scope
, *addr
;
790 struct sockaddr_in6
*sin6
;
792 _DIAGASSERT(pai
!= NULL
);
793 /* hostname may be NULL */
794 /* servname may be NULL */
795 _DIAGASSERT(res
!= NULL
);
798 * if the servname does not match socktype/protocol, ignore it.
800 if (get_portmatch(pai
, servname
, svd
) != 0)
803 afd
= find_afd(pai
->ai_family
);
808 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
811 cp
= strchr(hostname
, SCOPE_DELIMITER
);
813 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
817 * Handle special case of <scoped_address><delimiter><scope id>
819 hostname2
= strdup(hostname
);
820 if (hostname2
== NULL
)
822 /* terminate at the delimiter */
823 hostname2
[cp
- hostname
] = '\0';
827 error
= explore_numeric(pai
, addr
, servname
, res
, hostname
, svd
);
831 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
832 if (cur
->ai_family
!= AF_INET6
)
834 sin6
= (struct sockaddr_in6
*)(void *)cur
->ai_addr
;
835 if (ip6_str2scopeid(scope
, sin6
, &scopeid
) == -1) {
837 return(EAI_NODATA
); /* XXX: is return OK? */
839 sin6
->sin6_scope_id
= scopeid
;
850 get_canonname(const struct addrinfo
*pai
, struct addrinfo
*ai
, const char *str
)
853 _DIAGASSERT(pai
!= NULL
);
854 _DIAGASSERT(ai
!= NULL
);
855 _DIAGASSERT(str
!= NULL
);
857 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
858 ai
->ai_canonname
= strdup(str
);
859 if (ai
->ai_canonname
== NULL
)
865 static struct addrinfo
*
866 get_ai(const struct addrinfo
*pai
, const struct afd
*afd
, const char *addr
)
871 _DIAGASSERT(pai
!= NULL
);
872 _DIAGASSERT(afd
!= NULL
);
873 _DIAGASSERT(addr
!= NULL
);
875 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
880 memcpy(ai
, pai
, sizeof(struct addrinfo
));
881 ai
->ai_addr
= (struct sockaddr
*)(void *)(ai
+ 1);
882 memset(ai
->ai_addr
, 0, (size_t)afd
->a_socklen
);
883 ai
->ai_addr
->sa_len
= (uint8_t)afd
->a_socklen
;
884 ai
->ai_addrlen
= afd
->a_socklen
;
885 ai
->ai_family
= afd
->a_af
;
886 ai
->ai_addr
->sa_family
= (sa_family_t
)ai
->ai_family
;
887 p
= (char *)(void *)(ai
->ai_addr
);
888 memcpy(p
+ afd
->a_off
, addr
, (size_t)afd
->a_addrlen
);
893 get_portmatch(const struct addrinfo
*ai
, const char *servname
,
894 struct servent_data
*svd
)
897 _DIAGASSERT(ai
!= NULL
);
898 /* servname may be NULL */
900 return get_port(ai
, servname
, 1, svd
);
904 get_port(const struct addrinfo
*ai
, const char *servname
, int matchonly
,
905 struct servent_data
*svd
)
912 _DIAGASSERT(ai
!= NULL
);
913 /* servname may be NULL */
915 if (servname
== NULL
)
917 switch (ai
->ai_family
) {
927 switch (ai
->ai_socktype
) {
936 * This was 0. It is now 1 so that queries specifying
937 * a NULL hint, or hint without socktype (but, hopefully,
938 * with protocol) and numeric address actually work.
946 port
= str2number(servname
);
950 if (port
< 0 || port
> 65535)
954 // struct servent sv;
955 if (ai
->ai_flags
& AI_NUMERICSERV
)
958 switch (ai
->ai_socktype
) {
970 // sp = getservbyname_r(servname, proto, &sv, svd);
971 sp
= getservbyname ( servname
, proto
);
978 switch (ai
->ai_family
) {
980 ((struct sockaddr_in
*)(void *)
981 ai
->ai_addr
)->sin_port
= (in_port_t
)port
;
985 ((struct sockaddr_in6
*)(void *)
986 ai
->ai_addr
)->sin6_port
= (in_port_t
)port
;
995 static const struct afd
*
998 const struct afd
*afd
;
1000 if (af
== PF_UNSPEC
)
1002 for (afd
= afdl
; afd
->a_af
; afd
++) {
1003 if (afd
->a_af
== af
)
1010 /* convert a string to a scope identifier. XXX: IPv6 specific */
1012 ip6_str2scopeid(char *scope
, struct sockaddr_in6
*sin6
, u_int32_t
*scopeid
)
1015 struct in6_addr
*a6
;
1018 _DIAGASSERT(scope
!= NULL
);
1019 _DIAGASSERT(sin6
!= NULL
);
1020 _DIAGASSERT(scopeid
!= NULL
);
1022 a6
= &sin6
->sin6_addr
;
1024 /* empty scopeid portion is invalid */
1028 if (IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) {
1030 * We currently assume a one-to-one mapping between links
1031 * and interfaces, so we simply use interface indices for
1032 * like-local scopes.
1035 *scopeid = if_nametoindex(scope);
1043 /* still unclear about literal, allow numeric only - placeholder */
1044 if (IN6_IS_ADDR_SITELOCAL(a6
) || IN6_IS_ADDR_MC_SITELOCAL(a6
))
1046 if (IN6_IS_ADDR_MC_ORGLOCAL(a6
))
1049 goto trynumeric
; /* global */
1051 /* try to convert to a numeric id as a last resort */
1054 lscopeid
= strtoul(scope
, &ep
, 10);
1055 *scopeid
= (u_int32_t
)(lscopeid
& 0xffffffffUL
);
1056 if (errno
== 0 && ep
&& *ep
== '\0' && *scopeid
== lscopeid
)
1063 /* code duplicate with gethnamaddr.c */
1065 static const char AskedForGot
[] =
1066 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1068 static struct addrinfo
*
1069 getanswer(const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
1070 const struct addrinfo
*pai
)
1072 struct addrinfo sentinel
, *cur
;
1074 const struct afd
*afd
;
1081 int type
, class, ancount
, qdcount
;
1082 int haveanswer
, had_error
;
1083 char tbuf
[MAXDNAME
];
1084 int (*name_ok
) (const char *);
1085 static char hostbuf
[8*1024];
1087 _DIAGASSERT(answer
!= NULL
);
1088 _DIAGASSERT(qname
!= NULL
);
1089 _DIAGASSERT(pai
!= NULL
);
1091 memset(&sentinel
, 0, sizeof(sentinel
));
1095 eom
= answer
->buf
+ anslen
;
1099 case T_ANY
: /*use T_ANY only for T_A/T_AAAA lookup*/
1103 return NULL
; /* XXX should be abort(); */
1106 * find first satisfactory answer
1109 ancount
= ntohs(hp
->ancount
);
1110 qdcount
= ntohs(hp
->qdcount
);
1112 ep
= hostbuf
+ sizeof hostbuf
;
1113 cp
= answer
->buf
+ HFIXEDSZ
;
1115 h_errno
= NO_RECOVERY
;
1118 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1119 if ((n
< 0) || !(*name_ok
)(bp
)) {
1120 h_errno
= NO_RECOVERY
;
1124 if (qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) {
1125 /* res_send() has already verified that the query name is the
1126 * same as the one we sent; this just gets the expanded name
1127 * (i.e., with the succeeding search-domain tacked on).
1129 n
= (int)strlen(bp
) + 1; /* for the \0 */
1130 if (n
>= MAXHOSTNAMELEN
) {
1131 h_errno
= NO_RECOVERY
;
1136 /* The qname can be abbreviated, but h_name is now absolute. */
1141 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
1142 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1143 if ((n
< 0) || !(*name_ok
)(bp
)) {
1148 type
= _getshort(cp
);
1149 cp
+= INT16SZ
; /* type */
1150 class = _getshort(cp
);
1151 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
1153 cp
+= INT16SZ
; /* len */
1154 if (class != C_IN
) {
1155 /* XXX - debug? syslog? */
1157 continue; /* XXX - had_error++ ? */
1159 if ((qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) &&
1161 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
1162 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
1167 /* Get canonical name. */
1168 n
= (int)strlen(tbuf
) + 1; /* for the \0 */
1169 if (n
> ep
- bp
|| n
>= MAXHOSTNAMELEN
) {
1173 strlcpy(bp
, tbuf
, (size_t)(ep
- bp
));
1178 if (qtype
== T_ANY
) {
1179 if (!(type
== T_A
|| type
== T_AAAA
)) {
1183 } else if (type
!= qtype
) {
1184 if (type
!= T_KEY
&& type
!= T_SIG
) {
1185 #ifdef _ORG_FREEBSD_
1186 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1187 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1188 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1189 qname
, p_class(C_IN
), p_type(qtype
),
1194 continue; /* XXX - had_error++ ? */
1199 if (strcasecmp(canonname
, bp
) != 0) {
1200 #ifdef _ORG_FREEBSD_
1201 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1202 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1203 AskedForGot
, canonname
, bp
);
1206 continue; /* XXX - had_error++ ? */
1208 if (type
== T_A
&& n
!= INADDRSZ
) {
1212 if (type
== T_AAAA
&& n
!= IN6ADDRSZ
) {
1216 if (type
== T_AAAA
) {
1217 struct in6_addr in6
;
1218 memcpy(&in6
, cp
, IN6ADDRSZ
);
1219 if (IN6_IS_ADDR_V4MAPPED(&in6
)) {
1228 nn
= (int)strlen(bp
) + 1; /* for the \0 */
1232 /* don't overwrite pai */
1234 ai
.ai_family
= (type
== T_A
) ? AF_INET
: AF_INET6
;
1235 afd
= find_afd(ai
.ai_family
);
1240 cur
->ai_next
= get_ai(&ai
, afd
, (const char *)cp
);
1241 if (cur
->ai_next
== NULL
)
1243 while (cur
&& cur
->ai_next
)
1255 (void)get_canonname(pai
, sentinel
.ai_next
, qname
);
1257 (void)get_canonname(pai
, sentinel
.ai_next
, canonname
);
1258 h_errno
= NETDB_SUCCESS
;
1259 return sentinel
.ai_next
;
1262 h_errno
= NO_RECOVERY
;
1266 #define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
1267 #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
1270 aisort(struct addrinfo
*s
, res_state res
)
1272 struct addrinfo head
, *t
, *p
;
1275 head
.ai_next
= NULL
;
1278 for (i
= 0; i
< (int)res
->nsort
; i
++) {
1280 while (p
->ai_next
) {
1281 if ((p
->ai_next
->ai_family
!= AF_INET
)
1282 || SORTMATCH(p
, res
->sort_list
[i
])) {
1283 t
->ai_next
= p
->ai_next
;
1285 p
->ai_next
= p
->ai_next
->ai_next
;
1292 /* add rest of list and reset s to the new list*/
1293 t
->ai_next
= s
->ai_next
;
1294 s
->ai_next
= head
.ai_next
;
1299 _dns_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1301 struct addrinfo
*ai
;
1302 querybuf
*buf
, *buf2
;
1304 const struct addrinfo
*pai
;
1305 struct addrinfo sentinel
, *cur
;
1306 struct res_target q
, q2
;
1309 name
= va_arg(ap
, char *);
1310 pai
= va_arg(ap
, const struct addrinfo
*);
1312 memset(&q
, 0, sizeof(q
));
1313 memset(&q2
, 0, sizeof(q2
));
1314 memset(&sentinel
, 0, sizeof(sentinel
));
1317 buf
= malloc(sizeof(*buf
));
1319 h_errno
= NETDB_INTERNAL
;
1322 buf2
= malloc(sizeof(*buf2
));
1325 h_errno
= NETDB_INTERNAL
;
1329 switch (pai
->ai_family
) {
1335 q
.answer
= buf
->buf
;
1336 q
.anslen
= sizeof(buf
->buf
);
1341 q2
.answer
= buf2
->buf
;
1342 q2
.anslen
= sizeof(buf2
->buf
);
1348 q
.answer
= buf
->buf
;
1349 q
.anslen
= sizeof(buf
->buf
);
1355 q
.answer
= buf
->buf
;
1356 q
.anslen
= sizeof(buf
->buf
);
1364 res
= __res_get_state();
1371 if (res_searchN(name
, &q
, res
) < 0) {
1372 __res_put_state(res
);
1377 ai
= getanswer(buf
, q
.n
, q
.name
, q
.qtype
, pai
);
1380 while (cur
&& cur
->ai_next
)
1384 ai
= getanswer(buf2
, q2
.n
, q2
.name
, q2
.qtype
, pai
);
1390 if (sentinel
.ai_next
== NULL
) {
1391 __res_put_state(res
);
1393 case HOST_NOT_FOUND
:
1403 aisort(&sentinel
, res
);
1405 __res_put_state(res
);
1407 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1412 _sethtent(FILE **hostf
)
1416 *hostf
= fopen(_PATH_HOSTS
, "r" );
1422 _endhtent(FILE **hostf
)
1426 (void) fclose(*hostf
);
1431 static struct addrinfo
*
1432 _gethtent(FILE **hostf
, const char *name
, const struct addrinfo
*pai
)
1435 char *cp
, *tname
, *cname
;
1436 struct addrinfo hints
, *res0
, *res
;
1439 static char hostbuf
[8*1024];
1441 _DIAGASSERT(name
!= NULL
);
1442 _DIAGASSERT(pai
!= NULL
);
1444 if (!*hostf
&& ( NULL
== (*hostf
= fopen(_PATH_HOSTS
, "r" ))))
1447 if ( NULL
== (p
= fgets(hostbuf
, sizeof hostbuf
, *hostf
)))
1451 if ( NULL
== (cp
= strpbrk(p
, "#\n")))
1454 if ( NULL
== (cp
= strpbrk(p
, " \t")))
1458 /* if this is not something we're looking for, skip it. */
1461 if (*cp
== ' ' || *cp
== '\t') {
1468 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1470 if (strcasecmp(name
, tname
) == 0)
1477 hints
.ai_flags
= AI_NUMERICHOST
;
1478 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1481 for (res
= res0
; res
; res
= res
->ai_next
) {
1483 res
->ai_flags
= pai
->ai_flags
;
1485 if (pai
->ai_flags
& AI_CANONNAME
) {
1486 if (get_canonname(pai
, res
, cname
) != 0) {
1497 _files_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1500 const struct addrinfo
*pai
;
1501 struct addrinfo sentinel
, *cur
;
1508 name
= va_arg(ap
, char *);
1509 pai
= va_arg(ap
, const struct addrinfo
*);
1511 memset(&sentinel
, 0, sizeof(sentinel
));
1515 while ((p
= _gethtent(&hostf
, name
, pai
)) != NULL
) {
1517 while (cur
&& cur
->ai_next
)
1522 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1523 if (sentinel
.ai_next
== NULL
)
1530 static struct addrinfo
*
1531 _yphostent(char *line
, const struct addrinfo
*pai
)
1533 struct addrinfo sentinel
, *cur
;
1534 struct addrinfo hints
, *res
, *res0
;
1537 const char *addr
, *canonname
;
1541 _DIAGASSERT(line
!= NULL
);
1542 _DIAGASSERT(pai
!= NULL
);
1545 addr
= canonname
= NULL
;
1547 memset(&sentinel
, 0, sizeof(sentinel
));
1551 /* terminate line */
1552 cp
= strchr(p
, '\n');
1559 cp
= strpbrk(p
, " \t");
1561 if (canonname
== NULL
)
1571 if (*cp
== ' ' || *cp
== '\t') {
1577 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1582 hints
.ai_flags
= AI_NUMERICHOST
;
1583 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1585 for (res
= res0
; res
; res
= res
->ai_next
) {
1587 res
->ai_flags
= pai
->ai_flags
;
1589 if (pai
->ai_flags
& AI_CANONNAME
)
1590 (void)get_canonname(pai
, res
, canonname
);
1595 cur
->ai_next
= res0
;
1596 while (cur
->ai_next
)
1606 return sentinel
.ai_next
;
1611 _yp_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1613 struct addrinfo sentinel
, *cur
;
1614 struct addrinfo
*ai
= NULL
;
1618 const struct addrinfo
*pai
;
1621 if (_yp_check(&ypdomain
) == 0)
1624 name
= va_arg(ap
, char *);
1625 pai
= va_arg(ap
, const struct addrinfo
*);
1627 memset(&sentinel
, 0, sizeof(sentinel
));
1630 /* hosts.byname is only for IPv4 (Solaris8) */
1631 if (pai
->ai_family
== PF_UNSPEC
|| pai
->ai_family
== PF_INET
) {
1632 r
= yp_match(ypdomain
, "hosts.byname", name
,
1633 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1635 struct addrinfo ai4
;
1638 ai4
.ai_family
= AF_INET
;
1639 ai
= _yphostent(ypbuf
, &ai4
);
1642 while (cur
&& cur
->ai_next
)
1649 /* ipnodes.byname can hold both IPv4/v6 */
1650 r
= yp_match(ypdomain
, "ipnodes.byname", name
,
1651 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1653 ai
= _yphostent(ypbuf
, pai
);
1659 if (sentinel
.ai_next
== NULL
) {
1660 h_errno
= HOST_NOT_FOUND
;
1663 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1668 /* resolver logic */
1671 * Formulate a normal query, send, and await answer.
1672 * Returned answer is placed in supplied buffer "answer".
1673 * Perform preliminary check of answer, returning success only
1674 * if no error is indicated and the answer count is nonzero.
1675 * Return the size of the response on success, -1 on error.
1676 * Error number is left in h_errno.
1678 * Caller must parse answer and determine whether it answers the question.
1681 res_queryN(const char *name
, /* domain name */ struct res_target
*target
,
1684 static u_char buf
[MAXPACKET
];
1687 struct res_target
*t
;
1691 _DIAGASSERT(name
!= NULL
);
1692 /* XXX: target may be NULL??? */
1697 for (t
= target
; t
; t
= t
->next
) {
1702 hp
= (HEADER
*)(void *)t
->answer
;
1703 hp
->rcode
= NOERROR
; /* default */
1705 /* make it easier... */
1711 if (res
->options
& RES_DEBUG
)
1712 printf(";; res_nquery(%s, %d, %d)\n", name
, class, type
);
1715 n
= res_nmkquery(res
, QUERY
, name
, class, type
, NULL
, 0, NULL
,
1717 #ifdef RES_USE_EDNS0
1718 if (n
> 0 && (res
->options
& RES_USE_EDNS0
) != 0)
1719 n
= res_nopt(res
, n
, buf
, sizeof(buf
), anslen
);
1723 if (res
->options
& RES_DEBUG
)
1724 printf(";; res_nquery: mkquery failed\n");
1726 h_errno
= NO_RECOVERY
;
1729 n
= res_nsend(res
, buf
, n
, answer
, anslen
);
1733 if (res
->options
& RES_DEBUG
)
1734 printf(";; res_query: send error\n");
1736 h_errno
= TRY_AGAIN
;
1741 if (n
< 0 || hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
1742 rcode
= hp
->rcode
; /* record most recent error */
1744 if (res
->options
& RES_DEBUG
)
1745 printf(";; rcode = %u, ancount=%u\n", hp
->rcode
,
1746 ntohs(hp
->ancount
));
1751 ancount
+= ntohs(hp
->ancount
);
1759 h_errno
= HOST_NOT_FOUND
;
1762 h_errno
= TRY_AGAIN
;
1771 h_errno
= NO_RECOVERY
;
1780 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1781 * Return the size of the response on success, -1 on error.
1782 * If enabled, implement search rules until answer or unrecoverable failure
1783 * is detected. Error code, if any, is left in h_errno.
1786 res_searchN(const char *name
, struct res_target
*target
, res_state res
)
1788 const char *cp
, * const *domain
;
1791 int trailing_dot
, ret
, saved_herrno
;
1792 int got_nodata
= 0, got_servfail
= 0, tried_as_is
= 0;
1794 _DIAGASSERT(name
!= NULL
);
1795 _DIAGASSERT(target
!= NULL
);
1797 hp
= (HEADER
*)(void *)target
->answer
; /*XXX*/
1800 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
1802 for (cp
= name
; *cp
; cp
++)
1803 dots
+= (*cp
== '.');
1805 if (cp
> name
&& *--cp
== '.')
1809 * if there aren't any dots, it could be a user-level alias
1811 if (!dots
&& (cp
= __hostalias(name
)) != NULL
) {
1812 ret
= res_queryN(cp
, target
, res
);
1817 * If there are dots in the name already, let's just give it a try
1818 * 'as is'. The threshold can be set with the "ndots" option.
1821 if (dots
>= res
->ndots
) {
1822 ret
= res_querydomainN(name
, NULL
, target
, res
);
1825 saved_herrno
= h_errno
;
1830 * We do at least one level of search if
1831 * - there is no dot and RES_DEFNAME is set, or
1832 * - there is at least one dot, there is no trailing dot,
1833 * and RES_DNSRCH is set.
1835 if ((!dots
&& (res
->options
& RES_DEFNAMES
)) ||
1836 (dots
&& !trailing_dot
&& (res
->options
& RES_DNSRCH
))) {
1839 for (domain
= (const char * const *)res
->dnsrch
;
1843 ret
= res_querydomainN(name
, *domain
, target
, res
);
1848 * If no server present, give up.
1849 * If name isn't found in this domain,
1850 * keep trying higher domains in the search list
1851 * (if that's enabled).
1852 * On a NO_DATA error, keep trying, otherwise
1853 * a wildcard entry of another type could keep us
1854 * from finding this entry higher in the domain.
1855 * If we get some other error (negative answer or
1856 * server failure), then stop searching up,
1857 * but try the input name below in case it's
1860 if (errno
== ECONNREFUSED
) {
1861 h_errno
= TRY_AGAIN
;
1869 case HOST_NOT_FOUND
:
1873 if (hp
->rcode
== SERVFAIL
) {
1874 /* try next search element, if any */
1880 /* anything else implies that we're done */
1884 * if we got here for some reason other than DNSRCH,
1885 * we only wanted one iteration of the loop, so stop.
1887 if (!(res
->options
& RES_DNSRCH
))
1893 * if we have not already tried the name "as is", do that now.
1894 * note that we do this regardless of how many dots were in the
1895 * name or whether it ends with a dot.
1898 ret
= res_querydomainN(name
, NULL
, target
, res
);
1904 * if we got here, we didn't satisfy the search.
1905 * if we did an initial full query, return that query's h_errno
1906 * (note that we wouldn't be here if that query had succeeded).
1907 * else if we ever got a nodata, send that back as the reason.
1908 * else send back meaningless h_errno, that being the one from
1909 * the last DNSRCH we did.
1911 if (saved_herrno
!= -1)
1912 h_errno
= saved_herrno
;
1913 else if (got_nodata
)
1915 else if (got_servfail
)
1916 h_errno
= TRY_AGAIN
;
1921 * Perform a call on res_query on the concatenation of name and domain,
1922 * removing a trailing dot from name if domain is NULL.
1925 res_querydomainN(const char *name
, const char *domain
,
1926 struct res_target
*target
, res_state res
)
1928 char nbuf
[MAXDNAME
];
1929 const char *longname
= nbuf
;
1932 _DIAGASSERT(name
!= NULL
);
1933 /* XXX: target may be NULL??? */
1936 if (res
->options
& RES_DEBUG
)
1937 printf(";; res_querydomain(%s, %s)\n",
1938 name
, domain
?domain
:"<Nil>");
1940 if (domain
== NULL
) {
1942 * Check for trailing '.';
1943 * copy without '.' if present.
1946 if (n
+ 1 > sizeof(nbuf
)) {
1947 h_errno
= NO_RECOVERY
;
1950 if (n
> 0 && name
[--n
] == '.') {
1951 strncpy(nbuf
, name
, n
);
1958 if (n
+ 1 + d
+ 1 > sizeof(nbuf
)) {
1959 h_errno
= NO_RECOVERY
;
1962 snprintf(nbuf
, sizeof(nbuf
), "%s.%s", name
, domain
);
1964 return res_queryN(longname
, target
, res
);