]>
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
);
560 * FQDN hostname, DNS lookup
563 explore_fqdn(const struct addrinfo
*pai
, const char *hostname
,
564 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
566 struct addrinfo
*result
;
567 struct addrinfo
*cur
;
569 static const ns_dtab dtab
[] = {
570 NS_FILES_CB(_files_getaddrinfo
, NULL
)
571 { NSSRC_DNS
, _dns_getaddrinfo
, NULL
}, /* force -DHESIOD */
572 NS_NIS_CB(_yp_getaddrinfo
, NULL
)
576 _DIAGASSERT(pai
!= NULL
);
577 /* hostname may be NULL */
578 /* servname may be NULL */
579 _DIAGASSERT(res
!= NULL
);
584 * if the servname does not match socktype/protocol, ignore it.
586 if (get_portmatch(pai
, servname
, svd
) != 0)
589 switch (nsdispatch(&result
, dtab
, NSDB_HOSTS
, "getaddrinfo",
590 default_dns_files
, hostname
, pai
)) {
602 for (cur
= result
; cur
; cur
= cur
->ai_next
) {
603 GET_PORT(cur
, servname
, svd
);
604 /* canonname should be filled already */
615 freeaddrinfo(result
);
621 * passive socket -> anyaddr (0.0.0.0 or ::)
622 * non-passive socket -> localhost (127.0.0.1 or ::1)
625 explore_null(const struct addrinfo
*pai
, const char *servname
,
626 struct addrinfo
**res
, struct servent_data
*svd
)
629 const struct afd
*afd
;
630 struct addrinfo
*cur
;
631 struct addrinfo sentinel
;
634 _DIAGASSERT(pai
!= NULL
);
635 /* servname may be NULL */
636 _DIAGASSERT(res
!= NULL
);
639 sentinel
.ai_next
= NULL
;
643 * filter out AFs that are not supported by the kernel
646 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
654 * if the servname does not match socktype/protocol, ignore it.
656 if (get_portmatch(pai
, servname
, svd
) != 0)
659 afd
= find_afd(pai
->ai_family
);
663 if (pai
->ai_flags
& AI_PASSIVE
) {
664 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
666 * GET_CANONNAME(cur->ai_next, "anyaddr");
668 GET_PORT(cur
->ai_next
, servname
, svd
);
670 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
672 * GET_CANONNAME(cur->ai_next, "localhost");
674 GET_PORT(cur
->ai_next
, servname
, svd
);
678 *res
= sentinel
.ai_next
;
682 if (sentinel
.ai_next
)
683 freeaddrinfo(sentinel
.ai_next
);
691 explore_numeric(const struct addrinfo
*pai
, const char *hostname
,
692 const char *servname
, struct addrinfo
**res
, const char *canonname
,
693 struct servent_data
*svd
)
695 const struct afd
*afd
;
696 struct addrinfo
*cur
;
697 struct addrinfo sentinel
;
701 _DIAGASSERT(pai
!= NULL
);
702 /* hostname may be NULL */
703 /* servname may be NULL */
704 _DIAGASSERT(res
!= NULL
);
707 sentinel
.ai_next
= NULL
;
711 * if the servname does not match socktype/protocol, ignore it.
713 if (get_portmatch(pai
, servname
, svd
) != 0)
716 afd
= find_afd(pai
->ai_family
);
721 #if 0 /*X/Open spec*/
723 if (inet_aton(hostname
, (struct in_addr
*)pton
) == 1) {
724 if (pai
->ai_family
== afd
->a_af
||
725 pai
->ai_family
== PF_UNSPEC
/*?*/) {
726 GET_AI(cur
->ai_next
, afd
, pton
);
727 GET_PORT(cur
->ai_next
, servname
, svd
);
728 if ((pai
->ai_flags
& AI_CANONNAME
)) {
730 * Set the numeric address itself as
731 * the canonical name, based on a
732 * clarification in rfc2553bis-03.
734 GET_CANONNAME(cur
->ai_next
, canonname
);
736 while (cur
&& cur
->ai_next
)
739 ERR(EAI_FAMILY
); /*xxx*/
744 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
745 if (pai
->ai_family
== afd
->a_af
||
746 pai
->ai_family
== PF_UNSPEC
/*?*/) {
747 GET_AI(cur
->ai_next
, afd
, pton
);
748 GET_PORT(cur
->ai_next
, servname
, svd
);
749 if ((pai
->ai_flags
& AI_CANONNAME
)) {
751 * Set the numeric address itself as
752 * the canonical name, based on a
753 * clarification in rfc2553bis-03.
755 GET_CANONNAME(cur
->ai_next
, canonname
);
760 ERR(EAI_FAMILY
); /*xxx*/
765 *res
= sentinel
.ai_next
;
770 if (sentinel
.ai_next
)
771 freeaddrinfo(sentinel
.ai_next
);
776 * numeric hostname with scope
779 explore_numeric_scope(const struct addrinfo
*pai
, const char *hostname
,
780 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
782 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
783 return explore_numeric(pai
, hostname
, servname
, res
, hostname
, svd
);
785 const struct afd
*afd
;
786 struct addrinfo
*cur
;
788 char *cp
, *hostname2
= NULL
, *scope
, *addr
;
789 struct sockaddr_in6
*sin6
;
791 _DIAGASSERT(pai
!= NULL
);
792 /* hostname may be NULL */
793 /* servname may be NULL */
794 _DIAGASSERT(res
!= NULL
);
797 * if the servname does not match socktype/protocol, ignore it.
799 if (get_portmatch(pai
, servname
, svd
) != 0)
802 afd
= find_afd(pai
->ai_family
);
807 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
810 cp
= strchr(hostname
, SCOPE_DELIMITER
);
812 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
816 * Handle special case of <scoped_address><delimiter><scope id>
818 hostname2
= strdup(hostname
);
819 if (hostname2
== NULL
)
821 /* terminate at the delimiter */
822 hostname2
[cp
- hostname
] = '\0';
826 error
= explore_numeric(pai
, addr
, servname
, res
, hostname
, svd
);
830 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
831 if (cur
->ai_family
!= AF_INET6
)
833 sin6
= (struct sockaddr_in6
*)(void *)cur
->ai_addr
;
834 if (ip6_str2scopeid(scope
, sin6
, &scopeid
) == -1) {
836 return(EAI_NODATA
); /* XXX: is return OK? */
838 sin6
->sin6_scope_id
= scopeid
;
849 get_canonname(const struct addrinfo
*pai
, struct addrinfo
*ai
, const char *str
)
852 _DIAGASSERT(pai
!= NULL
);
853 _DIAGASSERT(ai
!= NULL
);
854 _DIAGASSERT(str
!= NULL
);
856 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
857 ai
->ai_canonname
= strdup(str
);
858 if (ai
->ai_canonname
== NULL
)
864 static struct addrinfo
*
865 get_ai(const struct addrinfo
*pai
, const struct afd
*afd
, const char *addr
)
870 _DIAGASSERT(pai
!= NULL
);
871 _DIAGASSERT(afd
!= NULL
);
872 _DIAGASSERT(addr
!= NULL
);
874 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
)
879 memcpy(ai
, pai
, sizeof(struct addrinfo
));
880 ai
->ai_addr
= (struct sockaddr
*)(void *)(ai
+ 1);
881 memset(ai
->ai_addr
, 0, (size_t)afd
->a_socklen
);
882 ai
->ai_addr
->sa_len
= (uint8_t)afd
->a_socklen
;
883 ai
->ai_addrlen
= afd
->a_socklen
;
884 ai
->ai_family
= afd
->a_af
;
885 ai
->ai_addr
->sa_family
= (sa_family_t
)ai
->ai_family
;
886 p
= (char *)(void *)(ai
->ai_addr
);
887 memcpy(p
+ afd
->a_off
, addr
, (size_t)afd
->a_addrlen
);
892 get_portmatch(const struct addrinfo
*ai
, const char *servname
,
893 struct servent_data
*svd
)
896 _DIAGASSERT(ai
!= NULL
);
897 /* servname may be NULL */
899 return get_port(ai
, servname
, 1, svd
);
903 get_port(const struct addrinfo
*ai
, const char *servname
, int matchonly
,
904 struct servent_data
*svd
)
911 _DIAGASSERT(ai
!= NULL
);
912 /* servname may be NULL */
914 if (servname
== NULL
)
916 switch (ai
->ai_family
) {
926 switch (ai
->ai_socktype
) {
935 * This was 0. It is now 1 so that queries specifying
936 * a NULL hint, or hint without socktype (but, hopefully,
937 * with protocol) and numeric address actually work.
945 port
= str2number(servname
);
949 if (port
< 0 || port
> 65535)
953 // struct servent sv;
954 if (ai
->ai_flags
& AI_NUMERICSERV
)
957 switch (ai
->ai_socktype
) {
969 // sp = getservbyname_r(servname, proto, &sv, svd);
970 sp
= getservbyname ( servname
, proto
);
977 switch (ai
->ai_family
) {
979 ((struct sockaddr_in
*)(void *)
980 ai
->ai_addr
)->sin_port
= (in_port_t
)port
;
984 ((struct sockaddr_in6
*)(void *)
985 ai
->ai_addr
)->sin6_port
= (in_port_t
)port
;
994 static const struct afd
*
997 const struct afd
*afd
;
1001 for (afd
= afdl
; afd
->a_af
; afd
++) {
1002 if (afd
->a_af
== af
)
1009 /* convert a string to a scope identifier. XXX: IPv6 specific */
1011 ip6_str2scopeid(char *scope
, struct sockaddr_in6
*sin6
, u_int32_t
*scopeid
)
1014 struct in6_addr
*a6
;
1017 _DIAGASSERT(scope
!= NULL
);
1018 _DIAGASSERT(sin6
!= NULL
);
1019 _DIAGASSERT(scopeid
!= NULL
);
1021 a6
= &sin6
->sin6_addr
;
1023 /* empty scopeid portion is invalid */
1027 if (IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) {
1029 * We currently assume a one-to-one mapping between links
1030 * and interfaces, so we simply use interface indices for
1031 * like-local scopes.
1034 *scopeid = if_nametoindex(scope);
1042 /* still unclear about literal, allow numeric only - placeholder */
1043 if (IN6_IS_ADDR_SITELOCAL(a6
) || IN6_IS_ADDR_MC_SITELOCAL(a6
))
1045 if (IN6_IS_ADDR_MC_ORGLOCAL(a6
))
1048 goto trynumeric
; /* global */
1050 /* try to convert to a numeric id as a last resort */
1053 lscopeid
= strtoul(scope
, &ep
, 10);
1054 *scopeid
= (u_int32_t
)(lscopeid
& 0xffffffffUL
);
1055 if (errno
== 0 && ep
&& *ep
== '\0' && *scopeid
== lscopeid
)
1062 /* code duplicate with gethnamaddr.c */
1064 static const char AskedForGot
[] =
1065 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1067 static struct addrinfo
*
1068 getanswer(const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
1069 const struct addrinfo
*pai
)
1071 struct addrinfo sentinel
, *cur
;
1073 const struct afd
*afd
;
1080 int type
, class, ancount
, qdcount
;
1081 int haveanswer
, had_error
;
1082 char tbuf
[MAXDNAME
];
1083 int (*name_ok
) (const char *);
1084 static char hostbuf
[8*1024];
1086 _DIAGASSERT(answer
!= NULL
);
1087 _DIAGASSERT(qname
!= NULL
);
1088 _DIAGASSERT(pai
!= NULL
);
1090 memset(&sentinel
, 0, sizeof(sentinel
));
1094 eom
= answer
->buf
+ anslen
;
1098 case T_ANY
: /*use T_ANY only for T_A/T_AAAA lookup*/
1102 return NULL
; /* XXX should be abort(); */
1105 * find first satisfactory answer
1108 ancount
= ntohs(hp
->ancount
);
1109 qdcount
= ntohs(hp
->qdcount
);
1111 ep
= hostbuf
+ sizeof hostbuf
;
1112 cp
= answer
->buf
+ HFIXEDSZ
;
1114 h_errno
= NO_RECOVERY
;
1117 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1118 if ((n
< 0) || !(*name_ok
)(bp
)) {
1119 h_errno
= NO_RECOVERY
;
1123 if (qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) {
1124 /* res_send() has already verified that the query name is the
1125 * same as the one we sent; this just gets the expanded name
1126 * (i.e., with the succeeding search-domain tacked on).
1128 n
= (int)strlen(bp
) + 1; /* for the \0 */
1129 if (n
>= MAXHOSTNAMELEN
) {
1130 h_errno
= NO_RECOVERY
;
1135 /* The qname can be abbreviated, but h_name is now absolute. */
1140 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
1141 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1142 if ((n
< 0) || !(*name_ok
)(bp
)) {
1147 type
= _getshort(cp
);
1148 cp
+= INT16SZ
; /* type */
1149 class = _getshort(cp
);
1150 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
1152 cp
+= INT16SZ
; /* len */
1153 if (class != C_IN
) {
1154 /* XXX - debug? syslog? */
1156 continue; /* XXX - had_error++ ? */
1158 if ((qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) &&
1160 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
1161 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
1166 /* Get canonical name. */
1167 n
= (int)strlen(tbuf
) + 1; /* for the \0 */
1168 if (n
> ep
- bp
|| n
>= MAXHOSTNAMELEN
) {
1172 strlcpy(bp
, tbuf
, (size_t)(ep
- bp
));
1177 if (qtype
== T_ANY
) {
1178 if (!(type
== T_A
|| type
== T_AAAA
)) {
1182 } else if (type
!= qtype
) {
1183 if (type
!= T_KEY
&& type
!= T_SIG
) {
1184 #ifdef _ORG_FREEBSD_
1185 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1186 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1187 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1188 qname
, p_class(C_IN
), p_type(qtype
),
1193 continue; /* XXX - had_error++ ? */
1198 if (strcasecmp(canonname
, bp
) != 0) {
1199 #ifdef _ORG_FREEBSD_
1200 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1201 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1202 AskedForGot
, canonname
, bp
);
1205 continue; /* XXX - had_error++ ? */
1207 if (type
== T_A
&& n
!= INADDRSZ
) {
1211 if (type
== T_AAAA
&& n
!= IN6ADDRSZ
) {
1215 if (type
== T_AAAA
) {
1216 struct in6_addr in6
;
1217 memcpy(&in6
, cp
, IN6ADDRSZ
);
1218 if (IN6_IS_ADDR_V4MAPPED(&in6
)) {
1227 nn
= (int)strlen(bp
) + 1; /* for the \0 */
1231 /* don't overwrite pai */
1233 ai
.ai_family
= (type
== T_A
) ? AF_INET
: AF_INET6
;
1234 afd
= find_afd(ai
.ai_family
);
1239 cur
->ai_next
= get_ai(&ai
, afd
, (const char *)cp
);
1240 if (cur
->ai_next
== NULL
)
1242 while (cur
&& cur
->ai_next
)
1254 (void)get_canonname(pai
, sentinel
.ai_next
, qname
);
1256 (void)get_canonname(pai
, sentinel
.ai_next
, canonname
);
1257 h_errno
= NETDB_SUCCESS
;
1258 return sentinel
.ai_next
;
1261 h_errno
= NO_RECOVERY
;
1265 #define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
1266 #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
1269 aisort(struct addrinfo
*s
, res_state res
)
1271 struct addrinfo head
, *t
, *p
;
1274 head
.ai_next
= NULL
;
1277 for (i
= 0; i
< (int)res
->nsort
; i
++) {
1279 while (p
->ai_next
) {
1280 if ((p
->ai_next
->ai_family
!= AF_INET
)
1281 || SORTMATCH(p
, res
->sort_list
[i
])) {
1282 t
->ai_next
= p
->ai_next
;
1284 p
->ai_next
= p
->ai_next
->ai_next
;
1291 /* add rest of list and reset s to the new list*/
1292 t
->ai_next
= s
->ai_next
;
1293 s
->ai_next
= head
.ai_next
;
1298 _dns_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1300 struct addrinfo
*ai
;
1301 querybuf
*buf
, *buf2
;
1303 const struct addrinfo
*pai
;
1304 struct addrinfo sentinel
, *cur
;
1305 struct res_target q
, q2
;
1308 name
= va_arg(ap
, char *);
1309 pai
= va_arg(ap
, const struct addrinfo
*);
1311 memset(&q
, 0, sizeof(q
));
1312 memset(&q2
, 0, sizeof(q2
));
1313 memset(&sentinel
, 0, sizeof(sentinel
));
1316 buf
= malloc(sizeof(*buf
));
1318 h_errno
= NETDB_INTERNAL
;
1321 buf2
= malloc(sizeof(*buf2
));
1324 h_errno
= NETDB_INTERNAL
;
1328 switch (pai
->ai_family
) {
1334 q
.answer
= buf
->buf
;
1335 q
.anslen
= sizeof(buf
->buf
);
1340 q2
.answer
= buf2
->buf
;
1341 q2
.anslen
= sizeof(buf2
->buf
);
1347 q
.answer
= buf
->buf
;
1348 q
.anslen
= sizeof(buf
->buf
);
1354 q
.answer
= buf
->buf
;
1355 q
.anslen
= sizeof(buf
->buf
);
1363 res
= __res_get_state();
1370 if (res_searchN(name
, &q
, res
) < 0) {
1371 __res_put_state(res
);
1376 ai
= getanswer(buf
, q
.n
, q
.name
, q
.qtype
, pai
);
1379 while (cur
&& cur
->ai_next
)
1383 ai
= getanswer(buf2
, q2
.n
, q2
.name
, q2
.qtype
, pai
);
1389 if (sentinel
.ai_next
== NULL
) {
1390 __res_put_state(res
);
1392 case HOST_NOT_FOUND
:
1402 aisort(&sentinel
, res
);
1404 __res_put_state(res
);
1406 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1411 _sethtent(FILE **hostf
)
1415 *hostf
= fopen(_PATH_HOSTS
, "r" );
1421 _endhtent(FILE **hostf
)
1425 (void) fclose(*hostf
);
1430 static struct addrinfo
*
1431 _gethtent(FILE **hostf
, const char *name
, const struct addrinfo
*pai
)
1434 char *cp
, *tname
, *cname
;
1435 struct addrinfo hints
, *res0
, *res
;
1438 static char hostbuf
[8*1024];
1440 _DIAGASSERT(name
!= NULL
);
1441 _DIAGASSERT(pai
!= NULL
);
1443 if (!*hostf
&& ( NULL
== (*hostf
= fopen(_PATH_HOSTS
, "r" ))))
1446 if ( NULL
== (p
= fgets(hostbuf
, sizeof hostbuf
, *hostf
)))
1450 if ( NULL
== (cp
= strpbrk(p
, "#\n")))
1453 if ( NULL
== (cp
= strpbrk(p
, " \t")))
1457 /* if this is not something we're looking for, skip it. */
1460 if (*cp
== ' ' || *cp
== '\t') {
1467 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1469 if (strcasecmp(name
, tname
) == 0)
1476 hints
.ai_flags
= AI_NUMERICHOST
;
1477 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1480 for (res
= res0
; res
; res
= res
->ai_next
) {
1482 res
->ai_flags
= pai
->ai_flags
;
1484 if (pai
->ai_flags
& AI_CANONNAME
) {
1485 if (get_canonname(pai
, res
, cname
) != 0) {
1496 _files_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1499 const struct addrinfo
*pai
;
1500 struct addrinfo sentinel
, *cur
;
1507 name
= va_arg(ap
, char *);
1508 pai
= va_arg(ap
, const struct addrinfo
*);
1510 memset(&sentinel
, 0, sizeof(sentinel
));
1514 while ((p
= _gethtent(&hostf
, name
, pai
)) != NULL
) {
1516 while (cur
&& cur
->ai_next
)
1521 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1522 if (sentinel
.ai_next
== NULL
)
1529 static struct addrinfo
*
1530 _yphostent(char *line
, const struct addrinfo
*pai
)
1532 struct addrinfo sentinel
, *cur
;
1533 struct addrinfo hints
, *res
, *res0
;
1536 const char *addr
, *canonname
;
1540 _DIAGASSERT(line
!= NULL
);
1541 _DIAGASSERT(pai
!= NULL
);
1544 addr
= canonname
= NULL
;
1546 memset(&sentinel
, 0, sizeof(sentinel
));
1550 /* terminate line */
1551 cp
= strchr(p
, '\n');
1558 cp
= strpbrk(p
, " \t");
1560 if (canonname
== NULL
)
1570 if (*cp
== ' ' || *cp
== '\t') {
1576 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1581 hints
.ai_flags
= AI_NUMERICHOST
;
1582 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1584 for (res
= res0
; res
; res
= res
->ai_next
) {
1586 res
->ai_flags
= pai
->ai_flags
;
1588 if (pai
->ai_flags
& AI_CANONNAME
)
1589 (void)get_canonname(pai
, res
, canonname
);
1594 cur
->ai_next
= res0
;
1595 while (cur
->ai_next
)
1605 return sentinel
.ai_next
;
1610 _yp_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1612 struct addrinfo sentinel
, *cur
;
1613 struct addrinfo
*ai
= NULL
;
1617 const struct addrinfo
*pai
;
1620 if (_yp_check(&ypdomain
) == 0)
1623 name
= va_arg(ap
, char *);
1624 pai
= va_arg(ap
, const struct addrinfo
*);
1626 memset(&sentinel
, 0, sizeof(sentinel
));
1629 /* hosts.byname is only for IPv4 (Solaris8) */
1630 if (pai
->ai_family
== PF_UNSPEC
|| pai
->ai_family
== PF_INET
) {
1631 r
= yp_match(ypdomain
, "hosts.byname", name
,
1632 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1634 struct addrinfo ai4
;
1637 ai4
.ai_family
= AF_INET
;
1638 ai
= _yphostent(ypbuf
, &ai4
);
1641 while (cur
&& cur
->ai_next
)
1648 /* ipnodes.byname can hold both IPv4/v6 */
1649 r
= yp_match(ypdomain
, "ipnodes.byname", name
,
1650 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1652 ai
= _yphostent(ypbuf
, pai
);
1658 if (sentinel
.ai_next
== NULL
) {
1659 h_errno
= HOST_NOT_FOUND
;
1662 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1667 /* resolver logic */
1670 * Formulate a normal query, send, and await answer.
1671 * Returned answer is placed in supplied buffer "answer".
1672 * Perform preliminary check of answer, returning success only
1673 * if no error is indicated and the answer count is nonzero.
1674 * Return the size of the response on success, -1 on error.
1675 * Error number is left in h_errno.
1677 * Caller must parse answer and determine whether it answers the question.
1680 res_queryN(const char *name
, /* domain name */ struct res_target
*target
,
1683 static u_char buf
[MAXPACKET
];
1686 struct res_target
*t
;
1690 _DIAGASSERT(name
!= NULL
);
1691 /* XXX: target may be NULL??? */
1696 for (t
= target
; t
; t
= t
->next
) {
1701 hp
= (HEADER
*)(void *)t
->answer
;
1702 hp
->rcode
= NOERROR
; /* default */
1704 /* make it easier... */
1710 if (res
->options
& RES_DEBUG
)
1711 printf(";; res_nquery(%s, %d, %d)\n", name
, class, type
);
1714 n
= res_nmkquery(res
, QUERY
, name
, class, type
, NULL
, 0, NULL
,
1716 #ifdef RES_USE_EDNS0
1717 if (n
> 0 && (res
->options
& RES_USE_EDNS0
) != 0)
1718 n
= res_nopt(res
, n
, buf
, sizeof(buf
), anslen
);
1722 if (res
->options
& RES_DEBUG
)
1723 printf(";; res_nquery: mkquery failed\n");
1725 h_errno
= NO_RECOVERY
;
1728 n
= res_nsend(res
, buf
, n
, answer
, anslen
);
1732 if (res
->options
& RES_DEBUG
)
1733 printf(";; res_query: send error\n");
1735 h_errno
= TRY_AGAIN
;
1740 if (n
< 0 || hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
1741 rcode
= hp
->rcode
; /* record most recent error */
1743 if (res
->options
& RES_DEBUG
)
1744 printf(";; rcode = %u, ancount=%u\n", hp
->rcode
,
1745 ntohs(hp
->ancount
));
1750 ancount
+= ntohs(hp
->ancount
);
1758 h_errno
= HOST_NOT_FOUND
;
1761 h_errno
= TRY_AGAIN
;
1770 h_errno
= NO_RECOVERY
;
1779 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1780 * Return the size of the response on success, -1 on error.
1781 * If enabled, implement search rules until answer or unrecoverable failure
1782 * is detected. Error code, if any, is left in h_errno.
1785 res_searchN(const char *name
, struct res_target
*target
, res_state res
)
1787 const char *cp
, * const *domain
;
1790 int trailing_dot
, ret
, saved_herrno
;
1791 int got_nodata
= 0, got_servfail
= 0, tried_as_is
= 0;
1793 _DIAGASSERT(name
!= NULL
);
1794 _DIAGASSERT(target
!= NULL
);
1796 hp
= (HEADER
*)(void *)target
->answer
; /*XXX*/
1799 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
1801 for (cp
= name
; *cp
; cp
++)
1802 dots
+= (*cp
== '.');
1804 if (cp
> name
&& *--cp
== '.')
1808 * if there aren't any dots, it could be a user-level alias
1810 if (!dots
&& (cp
= __hostalias(name
)) != NULL
) {
1811 ret
= res_queryN(cp
, target
, res
);
1816 * If there are dots in the name already, let's just give it a try
1817 * 'as is'. The threshold can be set with the "ndots" option.
1820 if (dots
>= res
->ndots
) {
1821 ret
= res_querydomainN(name
, NULL
, target
, res
);
1824 saved_herrno
= h_errno
;
1829 * We do at least one level of search if
1830 * - there is no dot and RES_DEFNAME is set, or
1831 * - there is at least one dot, there is no trailing dot,
1832 * and RES_DNSRCH is set.
1834 if ((!dots
&& (res
->options
& RES_DEFNAMES
)) ||
1835 (dots
&& !trailing_dot
&& (res
->options
& RES_DNSRCH
))) {
1838 for (domain
= (const char * const *)res
->dnsrch
;
1842 ret
= res_querydomainN(name
, *domain
, target
, res
);
1847 * If no server present, give up.
1848 * If name isn't found in this domain,
1849 * keep trying higher domains in the search list
1850 * (if that's enabled).
1851 * On a NO_DATA error, keep trying, otherwise
1852 * a wildcard entry of another type could keep us
1853 * from finding this entry higher in the domain.
1854 * If we get some other error (negative answer or
1855 * server failure), then stop searching up,
1856 * but try the input name below in case it's
1859 if (errno
== ECONNREFUSED
) {
1860 h_errno
= TRY_AGAIN
;
1868 case HOST_NOT_FOUND
:
1872 if (hp
->rcode
== SERVFAIL
) {
1873 /* try next search element, if any */
1879 /* anything else implies that we're done */
1883 * if we got here for some reason other than DNSRCH,
1884 * we only wanted one iteration of the loop, so stop.
1886 if (!(res
->options
& RES_DNSRCH
))
1892 * if we have not already tried the name "as is", do that now.
1893 * note that we do this regardless of how many dots were in the
1894 * name or whether it ends with a dot.
1897 ret
= res_querydomainN(name
, NULL
, target
, res
);
1903 * if we got here, we didn't satisfy the search.
1904 * if we did an initial full query, return that query's h_errno
1905 * (note that we wouldn't be here if that query had succeeded).
1906 * else if we ever got a nodata, send that back as the reason.
1907 * else send back meaningless h_errno, that being the one from
1908 * the last DNSRCH we did.
1910 if (saved_herrno
!= -1)
1911 h_errno
= saved_herrno
;
1912 else if (got_nodata
)
1914 else if (got_servfail
)
1915 h_errno
= TRY_AGAIN
;
1920 * Perform a call on res_query on the concatenation of name and domain,
1921 * removing a trailing dot from name if domain is NULL.
1924 res_querydomainN(const char *name
, const char *domain
,
1925 struct res_target
*target
, res_state res
)
1927 char nbuf
[MAXDNAME
];
1928 const char *longname
= nbuf
;
1931 _DIAGASSERT(name
!= NULL
);
1932 /* XXX: target may be NULL??? */
1935 if (res
->options
& RES_DEBUG
)
1936 printf(";; res_querydomain(%s, %s)\n",
1937 name
, domain
?domain
:"<Nil>");
1939 if (domain
== NULL
) {
1941 * Check for trailing '.';
1942 * copy without '.' if present.
1945 if (n
+ 1 > sizeof(nbuf
)) {
1946 h_errno
= NO_RECOVERY
;
1949 if (n
> 0 && name
[--n
] == '.') {
1950 strncpy(nbuf
, name
, n
);
1957 if (n
+ 1 + d
+ 1 > sizeof(nbuf
)) {
1958 h_errno
= NO_RECOVERY
;
1961 snprintf(nbuf
, sizeof(nbuf
), "%s.%s", name
, domain
);
1963 return res_queryN(longname
, target
, res
);