getaddrinfo.c revision 1.59 1 /* $NetBSD: getaddrinfo.c,v 1.59 2002/07/01 07:42:49 itojun Exp $ */
2 /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked.
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 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
40 * says to use inet_aton() to convert IPv4 numeric to binary (alows
41 * classful form as a result).
42 * current code - disallow classful form for IPv4 (due to use of inet_pton).
43 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
44 * invalid.
45 * current code - SEGV on freeaddrinfo(NULL)
46 * Note:
47 * - We use getipnodebyname() just for thread-safeness. There's no intent
48 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
49 * getipnodebyname().
50 * - The code filters out AFs that are not supported by the kernel,
51 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
52 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
53 * in ai_flags?
54 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
55 * (1) what should we do against numeric hostname (2) what should we do
56 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
57 * non-loopback address configured? global address configured?
58 * - To avoid search order issue, we have a big amount of code duplicate
59 * from gethnamaddr.c and some other places. The issues that there's no
60 * lower layer function to lookup "IPv4 or IPv6" record. Calling
61 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
62 * follows:
63 * - The code makes use of following calls when asked to resolver with
64 * ai_family = PF_UNSPEC:
65 * getipnodebyname(host, AF_INET6);
66 * getipnodebyname(host, AF_INET);
67 * This will result in the following queries if the node is configure to
68 * prefer /etc/hosts than DNS:
69 * lookup /etc/hosts for IPv6 address
70 * lookup DNS for IPv6 address
71 * lookup /etc/hosts for IPv4 address
72 * lookup DNS for IPv4 address
73 * which may not meet people's requirement.
74 * The right thing to happen is to have underlying layer which does
75 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
76 * This would result in a bit of code duplicate with _dns_ghbyname() and
77 * friends.
78 */
79
80 #include <sys/cdefs.h>
81 #if defined(LIBC_SCCS) && !defined(lint)
82 __RCSID("$NetBSD: getaddrinfo.c,v 1.59 2002/07/01 07:42:49 itojun Exp $");
83 #endif /* LIBC_SCCS and not lint */
84
85 #include "namespace.h"
86 #include <sys/types.h>
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 #include <net/if.h>
90 #include <netinet/in.h>
91 #include <arpa/inet.h>
92 #include <arpa/nameser.h>
93 #include <assert.h>
94 #include <ctype.h>
95 #include <errno.h>
96 #include <netdb.h>
97 #include <resolv.h>
98 #include <stddef.h>
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #include <unistd.h>
103
104 #include <syslog.h>
105 #include <stdarg.h>
106 #include <nsswitch.h>
107
108 #ifdef YP
109 #include <rpc/rpc.h>
110 #include <rpcsvc/yp_prot.h>
111 #include <rpcsvc/ypclnt.h>
112 #endif
113
114 #ifdef __weak_alias
115 __weak_alias(getaddrinfo,_getaddrinfo)
116 __weak_alias(freeaddrinfo,_freeaddrinfo)
117 __weak_alias(gai_strerror,_gai_strerror)
118 #endif
119
120 #define SUCCESS 0
121 #define ANY 0
122 #define YES 1
123 #define NO 0
124
125 static const char in_addrany[] = { 0, 0, 0, 0 };
126 static const char in_loopback[] = { 127, 0, 0, 1 };
127 #ifdef INET6
128 static const char in6_addrany[] = {
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
130 };
131 static const char in6_loopback[] = {
132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
133 };
134 #endif
135
136 static const struct afd {
137 int a_af;
138 int a_addrlen;
139 int a_socklen;
140 int a_off;
141 const char *a_addrany;
142 const char *a_loopback;
143 int a_scoped;
144 } afdl [] = {
145 #ifdef INET6
146 {PF_INET6, sizeof(struct in6_addr),
147 sizeof(struct sockaddr_in6),
148 offsetof(struct sockaddr_in6, sin6_addr),
149 in6_addrany, in6_loopback, 1},
150 #endif
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},
156 };
157
158 struct explore {
159 int e_af;
160 int e_socktype;
161 int e_protocol;
162 const char *e_protostr;
163 int e_wild;
164 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
165 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
166 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
167 };
168
169 static const struct explore explore[] = {
170 #if 0
171 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
172 #endif
173 #ifdef INET6
174 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
175 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
176 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
177 #endif
178 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
179 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
180 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
181 { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
182 { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
183 { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
184 { -1, 0, 0, NULL, 0 },
185 };
186
187 #ifdef INET6
188 #define PTON_MAX 16
189 #else
190 #define PTON_MAX 4
191 #endif
192
193 static const ns_src default_dns_files[] = {
194 { NSSRC_FILES, NS_SUCCESS },
195 { NSSRC_DNS, NS_SUCCESS },
196 { 0 }
197 };
198
199 #if PACKETSZ > 1024
200 #define MAXPACKET PACKETSZ
201 #else
202 #define MAXPACKET 1024
203 #endif
204
205 typedef union {
206 HEADER hdr;
207 u_char buf[MAXPACKET];
208 } querybuf;
209
210 struct res_target {
211 struct res_target *next;
212 const char *name; /* domain name */
213 int qclass, qtype; /* class and type of query */
214 u_char *answer; /* buffer to put answer */
215 int anslen; /* size of answer buffer */
216 int n; /* result length */
217 };
218
219 static int str_isnumber __P((const char *));
220 static int explore_fqdn __P((const struct addrinfo *, const char *,
221 const char *, struct addrinfo **));
222 static int explore_null __P((const struct addrinfo *,
223 const char *, struct addrinfo **));
224 static int explore_numeric __P((const struct addrinfo *, const char *,
225 const char *, struct addrinfo **));
226 static int explore_numeric_scope __P((const struct addrinfo *, const char *,
227 const char *, struct addrinfo **));
228 static int get_canonname __P((const struct addrinfo *,
229 struct addrinfo *, const char *));
230 static struct addrinfo *get_ai __P((const struct addrinfo *,
231 const struct afd *, const char *));
232 static int get_portmatch __P((const struct addrinfo *, const char *));
233 static int get_port __P((struct addrinfo *, const char *, int));
234 static const struct afd *find_afd __P((int));
235 #if 0
236 static int addrconfig __P((const struct addrinfo *));
237 #endif
238 #ifdef INET6
239 static u_int32_t ip6_str2scopeid __P((char *, struct sockaddr_in6 *));
240 #endif
241
242 static struct addrinfo *getanswer __P((const querybuf *, int, const char *, int,
243 const struct addrinfo *));
244 static int _dns_getaddrinfo __P((void *, void *, va_list));
245 static void _sethtent __P((void));
246 static void _endhtent __P((void));
247 static struct addrinfo *_gethtent __P((const char *, const struct addrinfo *));
248 static int _files_getaddrinfo __P((void *, void *, va_list));
249 #ifdef YP
250 static struct addrinfo *_yphostent __P((char *, const struct addrinfo *));
251 static int _yp_getaddrinfo __P((void *, void *, va_list));
252 #endif
253
254 static int res_queryN __P((const char *, struct res_target *));
255 static int res_searchN __P((const char *, struct res_target *));
256 static int res_querydomainN __P((const char *, const char *,
257 struct res_target *));
258
259 static const char * const ai_errlist[] = {
260 "Success",
261 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
262 "Temporary failure in name resolution", /* EAI_AGAIN */
263 "Invalid value for ai_flags", /* EAI_BADFLAGS */
264 "Non-recoverable failure in name resolution", /* EAI_FAIL */
265 "ai_family not supported", /* EAI_FAMILY */
266 "Memory allocation failure", /* EAI_MEMORY */
267 "No address associated with hostname", /* EAI_NODATA */
268 "hostname nor servname provided, or not known", /* EAI_NONAME */
269 "servname not supported for ai_socktype", /* EAI_SERVICE */
270 "ai_socktype not supported", /* EAI_SOCKTYPE */
271 "System error returned in errno", /* EAI_SYSTEM */
272 "Invalid value for hints", /* EAI_BADHINTS */
273 "Resolved protocol is unknown", /* EAI_PROTOCOL */
274 "Unknown error", /* EAI_MAX */
275 };
276
277 /* XXX macros that make external reference is BAD. */
278
279 #define GET_AI(ai, afd, addr) \
280 do { \
281 /* external reference: pai, error, and label free */ \
282 (ai) = get_ai(pai, (afd), (addr)); \
283 if ((ai) == NULL) { \
284 error = EAI_MEMORY; \
285 goto free; \
286 } \
287 } while (/*CONSTCOND*/0)
288
289 #define GET_PORT(ai, serv) \
290 do { \
291 /* external reference: error and label free */ \
292 error = get_port((ai), (serv), 0); \
293 if (error != 0) \
294 goto free; \
295 } while (/*CONSTCOND*/0)
296
297 #define GET_CANONNAME(ai, str) \
298 do { \
299 /* external reference: pai, error and label free */ \
300 error = get_canonname(pai, (ai), (str)); \
301 if (error != 0) \
302 goto free; \
303 } while (/*CONSTCOND*/0)
304
305 #define ERR(err) \
306 do { \
307 /* external reference: error, and label bad */ \
308 error = (err); \
309 goto bad; \
310 /*NOTREACHED*/ \
311 } while (/*CONSTCOND*/0)
312
313 #define MATCH_FAMILY(x, y, w) \
314 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
315 #define MATCH(x, y, w) \
316 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
317
318 char *
319 gai_strerror(ecode)
320 int ecode;
321 {
322 if (ecode < 0 || ecode > EAI_MAX)
323 ecode = EAI_MAX;
324 /* LINTED const castaway */
325 return (char *) ai_errlist[ecode];
326 }
327
328 void
329 freeaddrinfo(ai)
330 struct addrinfo *ai;
331 {
332 struct addrinfo *next;
333
334 _DIAGASSERT(ai != NULL);
335
336 do {
337 next = ai->ai_next;
338 if (ai->ai_canonname)
339 free(ai->ai_canonname);
340 /* no need to free(ai->ai_addr) */
341 free(ai);
342 ai = next;
343 } while (ai);
344 }
345
346 static int
347 str_isnumber(p)
348 const char *p;
349 {
350 char *ep;
351
352 _DIAGASSERT(p != NULL);
353
354 if (*p == '\0')
355 return NO;
356 ep = NULL;
357 errno = 0;
358 (void)strtoul(p, &ep, 10);
359 if (errno == 0 && ep && *ep == '\0')
360 return YES;
361 else
362 return NO;
363 }
364
365 int
366 getaddrinfo(hostname, servname, hints, res)
367 const char *hostname, *servname;
368 const struct addrinfo *hints;
369 struct addrinfo **res;
370 {
371 struct addrinfo sentinel;
372 struct addrinfo *cur;
373 int error = 0;
374 struct addrinfo ai;
375 struct addrinfo ai0;
376 struct addrinfo *pai;
377 const struct explore *ex;
378
379 /* hostname is allowed to be NULL */
380 /* servname is allowed to be NULL */
381 /* hints is allowed to be NULL */
382 _DIAGASSERT(res != NULL);
383
384 memset(&sentinel, 0, sizeof(sentinel));
385 cur = &sentinel;
386 pai = &ai;
387 pai->ai_flags = 0;
388 pai->ai_family = PF_UNSPEC;
389 pai->ai_socktype = ANY;
390 pai->ai_protocol = ANY;
391 pai->ai_addrlen = 0;
392 pai->ai_canonname = NULL;
393 pai->ai_addr = NULL;
394 pai->ai_next = NULL;
395
396 if (hostname == NULL && servname == NULL)
397 return EAI_NONAME;
398 if (hints) {
399 /* error check for hints */
400 if (hints->ai_addrlen || hints->ai_canonname ||
401 hints->ai_addr || hints->ai_next)
402 ERR(EAI_BADHINTS); /* xxx */
403 if (hints->ai_flags & ~AI_MASK)
404 ERR(EAI_BADFLAGS);
405 switch (hints->ai_family) {
406 case PF_UNSPEC:
407 case PF_INET:
408 #ifdef INET6
409 case PF_INET6:
410 #endif
411 break;
412 default:
413 ERR(EAI_FAMILY);
414 }
415 memcpy(pai, hints, sizeof(*pai));
416
417 /*
418 * if both socktype/protocol are specified, check if they
419 * are meaningful combination.
420 */
421 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
422 for (ex = explore; ex->e_af >= 0; ex++) {
423 if (pai->ai_family != ex->e_af)
424 continue;
425 if (ex->e_socktype == ANY)
426 continue;
427 if (ex->e_protocol == ANY)
428 continue;
429 if (pai->ai_socktype == ex->e_socktype
430 && pai->ai_protocol != ex->e_protocol) {
431 ERR(EAI_BADHINTS);
432 }
433 }
434 }
435 }
436
437 /*
438 * check for special cases. (1) numeric servname is disallowed if
439 * socktype/protocol are left unspecified. (2) servname is disallowed
440 * for raw and other inet{,6} sockets.
441 */
442 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
443 #ifdef PF_INET6
444 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
445 #endif
446 ) {
447 ai0 = *pai; /* backup *pai */
448
449 if (pai->ai_family == PF_UNSPEC) {
450 #ifdef PF_INET6
451 pai->ai_family = PF_INET6;
452 #else
453 pai->ai_family = PF_INET;
454 #endif
455 }
456 error = get_portmatch(pai, servname);
457 if (error)
458 ERR(error);
459
460 *pai = ai0;
461 }
462
463 ai0 = *pai;
464
465 /* NULL hostname, or numeric hostname */
466 for (ex = explore; ex->e_af >= 0; ex++) {
467 *pai = ai0;
468
469 /* PF_UNSPEC entries are prepared for DNS queries only */
470 if (ex->e_af == PF_UNSPEC)
471 continue;
472
473 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
474 continue;
475 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
476 continue;
477 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
478 continue;
479
480 if (pai->ai_family == PF_UNSPEC)
481 pai->ai_family = ex->e_af;
482 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
483 pai->ai_socktype = ex->e_socktype;
484 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
485 pai->ai_protocol = ex->e_protocol;
486
487 if (hostname == NULL)
488 error = explore_null(pai, servname, &cur->ai_next);
489 else
490 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
491
492 if (error)
493 goto free;
494
495 while (cur && cur->ai_next)
496 cur = cur->ai_next;
497 }
498
499 /*
500 * XXX
501 * If numreic representation of AF1 can be interpreted as FQDN
502 * representation of AF2, we need to think again about the code below.
503 */
504 if (sentinel.ai_next)
505 goto good;
506
507 if (pai->ai_flags & AI_NUMERICHOST)
508 ERR(EAI_NODATA);
509 if (hostname == NULL)
510 ERR(EAI_NODATA);
511
512 /*
513 * hostname as alphabetical name.
514 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
515 * outer loop by AFs.
516 */
517 for (ex = explore; ex->e_af >= 0; ex++) {
518 *pai = ai0;
519
520 /* require exact match for family field */
521 if (pai->ai_family != ex->e_af)
522 continue;
523
524 if (!MATCH(pai->ai_socktype, ex->e_socktype,
525 WILD_SOCKTYPE(ex))) {
526 continue;
527 }
528 if (!MATCH(pai->ai_protocol, ex->e_protocol,
529 WILD_PROTOCOL(ex))) {
530 continue;
531 }
532
533 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
534 pai->ai_socktype = ex->e_socktype;
535 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
536 pai->ai_protocol = ex->e_protocol;
537
538 error = explore_fqdn(pai, hostname, servname,
539 &cur->ai_next);
540
541 while (cur && cur->ai_next)
542 cur = cur->ai_next;
543 }
544
545 /* XXX */
546 if (sentinel.ai_next)
547 error = 0;
548
549 if (error)
550 goto free;
551 if (error == 0) {
552 if (sentinel.ai_next) {
553 good:
554 *res = sentinel.ai_next;
555 return SUCCESS;
556 } else
557 error = EAI_FAIL;
558 }
559 free:
560 bad:
561 if (sentinel.ai_next)
562 freeaddrinfo(sentinel.ai_next);
563 *res = NULL;
564 return error;
565 }
566
567 /*
568 * FQDN hostname, DNS lookup
569 */
570 static int
571 explore_fqdn(pai, hostname, servname, res)
572 const struct addrinfo *pai;
573 const char *hostname;
574 const char *servname;
575 struct addrinfo **res;
576 {
577 struct addrinfo *result;
578 struct addrinfo *cur;
579 int error = 0;
580 static const ns_dtab dtab[] = {
581 NS_FILES_CB(_files_getaddrinfo, NULL)
582 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
583 NS_NIS_CB(_yp_getaddrinfo, NULL)
584 { 0 }
585 };
586
587 _DIAGASSERT(pai != NULL);
588 /* hostname may be NULL */
589 /* servname may be NULL */
590 _DIAGASSERT(res != NULL);
591
592 result = NULL;
593
594 #if 0
595 /*
596 * If AI_ADDRCONFIG is specified, check if we are expected to
597 * return the address family or not.
598 * XXX does not handle PF_UNSPEC - should filter out result from
599 * nsdispatch()
600 */
601 if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(pai))
602 return 0;
603 #endif
604
605 /*
606 * if the servname does not match socktype/protocol, ignore it.
607 */
608 if (get_portmatch(pai, servname) != 0)
609 return 0;
610
611 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
612 default_dns_files, hostname, pai)) {
613 case NS_TRYAGAIN:
614 error = EAI_AGAIN;
615 goto free;
616 case NS_UNAVAIL:
617 error = EAI_FAIL;
618 goto free;
619 case NS_NOTFOUND:
620 error = EAI_NODATA;
621 goto free;
622 case NS_SUCCESS:
623 error = 0;
624 for (cur = result; cur; cur = cur->ai_next) {
625 GET_PORT(cur, servname);
626 /* canonname should be filled already */
627 }
628 break;
629 }
630
631 *res = result;
632
633 return 0;
634
635 free:
636 if (result)
637 freeaddrinfo(result);
638 return error;
639 }
640
641 /*
642 * hostname == NULL.
643 * passive socket -> anyaddr (0.0.0.0 or ::)
644 * non-passive socket -> localhost (127.0.0.1 or ::1)
645 */
646 static int
647 explore_null(pai, servname, res)
648 const struct addrinfo *pai;
649 const char *servname;
650 struct addrinfo **res;
651 {
652 int s;
653 const struct afd *afd;
654 struct addrinfo *cur;
655 struct addrinfo sentinel;
656 int error;
657
658 _DIAGASSERT(pai != NULL);
659 /* servname may be NULL */
660 _DIAGASSERT(res != NULL);
661
662 *res = NULL;
663 sentinel.ai_next = NULL;
664 cur = &sentinel;
665
666 /*
667 * filter out AFs that are not supported by the kernel
668 * XXX errno?
669 */
670 s = socket(pai->ai_family, SOCK_DGRAM, 0);
671 if (s < 0) {
672 if (errno != EMFILE)
673 return 0;
674 } else
675 close(s);
676
677 /*
678 * if the servname does not match socktype/protocol, ignore it.
679 */
680 if (get_portmatch(pai, servname) != 0)
681 return 0;
682
683 afd = find_afd(pai->ai_family);
684 if (afd == NULL)
685 return 0;
686
687 if (pai->ai_flags & AI_PASSIVE) {
688 GET_AI(cur->ai_next, afd, afd->a_addrany);
689 /* xxx meaningless?
690 * GET_CANONNAME(cur->ai_next, "anyaddr");
691 */
692 GET_PORT(cur->ai_next, servname);
693 } else {
694 GET_AI(cur->ai_next, afd, afd->a_loopback);
695 /* xxx meaningless?
696 * GET_CANONNAME(cur->ai_next, "localhost");
697 */
698 GET_PORT(cur->ai_next, servname);
699 }
700 cur = cur->ai_next;
701
702 *res = sentinel.ai_next;
703 return 0;
704
705 free:
706 if (sentinel.ai_next)
707 freeaddrinfo(sentinel.ai_next);
708 return error;
709 }
710
711 /*
712 * numeric hostname
713 */
714 static int
715 explore_numeric(pai, hostname, servname, res)
716 const struct addrinfo *pai;
717 const char *hostname;
718 const char *servname;
719 struct addrinfo **res;
720 {
721 const struct afd *afd;
722 struct addrinfo *cur;
723 struct addrinfo sentinel;
724 int error;
725 char pton[PTON_MAX];
726
727 _DIAGASSERT(pai != NULL);
728 /* hostname may be NULL */
729 /* servname may be NULL */
730 _DIAGASSERT(res != NULL);
731
732 *res = NULL;
733 sentinel.ai_next = NULL;
734 cur = &sentinel;
735
736 /*
737 * if the servname does not match socktype/protocol, ignore it.
738 */
739 if (get_portmatch(pai, servname) != 0)
740 return 0;
741
742 afd = find_afd(pai->ai_family);
743 if (afd == NULL)
744 return 0;
745
746 switch (afd->a_af) {
747 #if 0 /*X/Open spec*/
748 case AF_INET:
749 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
750 if (pai->ai_family == afd->a_af ||
751 pai->ai_family == PF_UNSPEC /*?*/) {
752 GET_AI(cur->ai_next, afd, pton);
753 GET_PORT(cur->ai_next, servname);
754 while (cur && cur->ai_next)
755 cur = cur->ai_next;
756 } else
757 ERR(EAI_FAMILY); /*xxx*/
758 }
759 break;
760 #endif
761 default:
762 if (inet_pton(afd->a_af, hostname, pton) == 1) {
763 if (pai->ai_family == afd->a_af ||
764 pai->ai_family == PF_UNSPEC /*?*/) {
765 GET_AI(cur->ai_next, afd, pton);
766 GET_PORT(cur->ai_next, servname);
767 while (cur && cur->ai_next)
768 cur = cur->ai_next;
769 } else
770 ERR(EAI_FAMILY); /*xxx*/
771 }
772 break;
773 }
774
775 *res = sentinel.ai_next;
776 return 0;
777
778 free:
779 bad:
780 if (sentinel.ai_next)
781 freeaddrinfo(sentinel.ai_next);
782 return error;
783 }
784
785 /*
786 * numeric hostname with scope
787 */
788 static int
789 explore_numeric_scope(pai, hostname, servname, res)
790 const struct addrinfo *pai;
791 const char *hostname;
792 const char *servname;
793 struct addrinfo **res;
794 {
795 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
796 return explore_numeric(pai, hostname, servname, res);
797 #else
798 const struct afd *afd;
799 struct addrinfo *cur;
800 int error;
801 char *cp, *hostname2 = NULL, *scope, *addr;
802 struct sockaddr_in6 *sin6;
803
804 _DIAGASSERT(pai != NULL);
805 /* hostname may be NULL */
806 /* servname may be NULL */
807 _DIAGASSERT(res != NULL);
808
809 /*
810 * if the servname does not match socktype/protocol, ignore it.
811 */
812 if (get_portmatch(pai, servname) != 0)
813 return 0;
814
815 afd = find_afd(pai->ai_family);
816 if (afd == NULL)
817 return 0;
818
819 if (!afd->a_scoped)
820 return explore_numeric(pai, hostname, servname, res);
821
822 cp = strchr(hostname, SCOPE_DELIMITER);
823 if (cp == NULL)
824 return explore_numeric(pai, hostname, servname, res);
825
826 #if 0
827 /*
828 * Handle special case of <scope id><delimiter><scoped_address>
829 */
830 hostname2 = strdup(hostname);
831 if (hostname2 == NULL)
832 return EAI_MEMORY;
833 /* terminate at the delimiter */
834 hostname2[cp - hostname] = '\0';
835 scope = hostname2;
836 addr = cp + 1;
837 #else
838 /*
839 * Handle special case of <scoped_address><delimiter><scope id>
840 */
841 hostname2 = strdup(hostname);
842 if (hostname2 == NULL)
843 return EAI_MEMORY;
844 /* terminate at the delimiter */
845 hostname2[cp - hostname] = '\0';
846 addr = hostname2;
847 scope = cp + 1;
848 #endif
849
850 error = explore_numeric(pai, addr, servname, res);
851 if (error == 0) {
852 u_int32_t scopeid;
853
854 for (cur = *res; cur; cur = cur->ai_next) {
855 if (cur->ai_family != AF_INET6)
856 continue;
857 sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
858 if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) {
859 free(hostname2);
860 return(EAI_NODATA); /* XXX: is return OK? */
861 }
862 sin6->sin6_scope_id = scopeid;
863 }
864 }
865
866 free(hostname2);
867
868 return error;
869 #endif
870 }
871
872 static int
873 get_canonname(pai, ai, str)
874 const struct addrinfo *pai;
875 struct addrinfo *ai;
876 const char *str;
877 {
878
879 _DIAGASSERT(pai != NULL);
880 _DIAGASSERT(ai != NULL);
881 _DIAGASSERT(str != NULL);
882
883 if ((pai->ai_flags & AI_CANONNAME) != 0) {
884 ai->ai_canonname = (char *)malloc(strlen(str) + 1);
885 if (ai->ai_canonname == NULL)
886 return EAI_MEMORY;
887 strcpy(ai->ai_canonname, str);
888 }
889 return 0;
890 }
891
892 static struct addrinfo *
893 get_ai(pai, afd, addr)
894 const struct addrinfo *pai;
895 const struct afd *afd;
896 const char *addr;
897 {
898 char *p;
899 struct addrinfo *ai;
900
901 _DIAGASSERT(pai != NULL);
902 _DIAGASSERT(afd != NULL);
903 _DIAGASSERT(addr != NULL);
904
905 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
906 + (afd->a_socklen));
907 if (ai == NULL)
908 return NULL;
909
910 memcpy(ai, pai, sizeof(struct addrinfo));
911 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
912 memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
913 ai->ai_addr->sa_len = afd->a_socklen;
914 ai->ai_addrlen = afd->a_socklen;
915 #if defined (__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
916 ai->__ai_pad0 = 0;
917 #endif
918 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
919 p = (char *)(void *)(ai->ai_addr);
920 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
921 return ai;
922 }
923
924 static int
925 get_portmatch(ai, servname)
926 const struct addrinfo *ai;
927 const char *servname;
928 {
929
930 _DIAGASSERT(ai != NULL);
931 /* servname may be NULL */
932
933 /* get_port does not touch first argument. when matchonly == 1. */
934 /* LINTED const cast */
935 return get_port((struct addrinfo *)ai, servname, 1);
936 }
937
938 static int
939 get_port(ai, servname, matchonly)
940 struct addrinfo *ai;
941 const char *servname;
942 int matchonly;
943 {
944 const char *proto;
945 struct servent *sp;
946 int port;
947 int allownumeric;
948
949 _DIAGASSERT(ai != NULL);
950 /* servname may be NULL */
951
952 if (servname == NULL)
953 return 0;
954 switch (ai->ai_family) {
955 case AF_INET:
956 #ifdef AF_INET6
957 case AF_INET6:
958 #endif
959 break;
960 default:
961 return 0;
962 }
963
964 switch (ai->ai_socktype) {
965 case SOCK_RAW:
966 return EAI_SERVICE;
967 case SOCK_DGRAM:
968 case SOCK_STREAM:
969 allownumeric = 1;
970 break;
971 case ANY:
972 allownumeric = 0;
973 break;
974 default:
975 return EAI_SOCKTYPE;
976 }
977
978 if (str_isnumber(servname)) {
979 if (!allownumeric)
980 return EAI_SERVICE;
981 port = atoi(servname);
982 if (port < 0 || port > 65535)
983 return EAI_SERVICE;
984 port = htons(port);
985 } else {
986 switch (ai->ai_socktype) {
987 case SOCK_DGRAM:
988 proto = "udp";
989 break;
990 case SOCK_STREAM:
991 proto = "tcp";
992 break;
993 default:
994 proto = NULL;
995 break;
996 }
997
998 if ((sp = getservbyname(servname, proto)) == NULL)
999 return EAI_SERVICE;
1000 port = sp->s_port;
1001 }
1002
1003 if (!matchonly) {
1004 switch (ai->ai_family) {
1005 case AF_INET:
1006 ((struct sockaddr_in *)(void *)
1007 ai->ai_addr)->sin_port = port;
1008 break;
1009 #ifdef INET6
1010 case AF_INET6:
1011 ((struct sockaddr_in6 *)(void *)
1012 ai->ai_addr)->sin6_port = port;
1013 break;
1014 #endif
1015 }
1016 }
1017
1018 return 0;
1019 }
1020
1021 static const struct afd *
1022 find_afd(af)
1023 int af;
1024 {
1025 const struct afd *afd;
1026
1027 if (af == PF_UNSPEC)
1028 return NULL;
1029 for (afd = afdl; afd->a_af; afd++) {
1030 if (afd->a_af == af)
1031 return afd;
1032 }
1033 return NULL;
1034 }
1035
1036 #if 0
1037 /*
1038 * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend
1039 * will take care of it.
1040 * the semantics of AI_ADDRCONFIG is not defined well. we are not sure
1041 * if the code is right or not.
1042 */
1043 static int
1044 addrconfig(pai)
1045 const struct addrinfo *pai;
1046 {
1047 int s;
1048
1049 _DIAGASSERT(pai != NULL);
1050
1051 /* XXX errno */
1052 s = socket(pai->ai_family, SOCK_DGRAM, 0);
1053 if (s < 0)
1054 return 0;
1055 close(s);
1056 return 1;
1057 }
1058 #endif
1059
1060 #ifdef INET6
1061 /* convert a string to a scope identifier. XXX: IPv6 specific */
1062 static u_int32_t
1063 ip6_str2scopeid(scope, sin6)
1064 char *scope;
1065 struct sockaddr_in6 *sin6;
1066 {
1067 u_int32_t scopeid;
1068 u_long lscopeid;
1069 struct in6_addr *a6;
1070 char *ep;
1071
1072 _DIAGASSERT(scope != NULL);
1073 _DIAGASSERT(sin6 != NULL);
1074
1075 a6 = &sin6->sin6_addr;
1076
1077 /* empty scopeid portion is invalid */
1078 if (*scope == '\0')
1079 return -1;
1080
1081 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1082 /*
1083 * We currently assume a one-to-one mapping between links
1084 * and interfaces, so we simply use interface indices for
1085 * like-local scopes.
1086 */
1087 scopeid = if_nametoindex(scope);
1088 if (scopeid == 0)
1089 goto trynumeric;
1090 return(scopeid);
1091 }
1092
1093 /* still unclear about literal, allow numeric only - placeholder */
1094 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1095 goto trynumeric;
1096 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1097 goto trynumeric;
1098 else
1099 goto trynumeric; /* global */
1100
1101 /* try to convert to a numeric id as a last resort */
1102 trynumeric:
1103 errno = 0;
1104 lscopeid = strtoul(scope, &ep, 10);
1105 scopeid = lscopeid & 0xffffffff;
1106 if (errno == 0 && ep && *ep == '\0' && scopeid == lscopeid)
1107 return scopeid;
1108 else
1109 return -1;
1110 }
1111 #endif
1112
1113 /* code duplicate with gethnamaddr.c */
1114
1115 static const char AskedForGot[] =
1116 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1117 static FILE *hostf = NULL;
1118
1119 static struct addrinfo *
1120 getanswer(answer, anslen, qname, qtype, pai)
1121 const querybuf *answer;
1122 int anslen;
1123 const char *qname;
1124 int qtype;
1125 const struct addrinfo *pai;
1126 {
1127 struct addrinfo sentinel, *cur;
1128 struct addrinfo ai;
1129 const struct afd *afd;
1130 char *canonname;
1131 const HEADER *hp;
1132 const u_char *cp;
1133 int n;
1134 const u_char *eom;
1135 char *bp, *ep;
1136 int type, class, ancount, qdcount;
1137 int haveanswer, had_error;
1138 char tbuf[MAXDNAME];
1139 int (*name_ok) __P((const char *));
1140 char hostbuf[8*1024];
1141
1142 _DIAGASSERT(answer != NULL);
1143 _DIAGASSERT(qname != NULL);
1144 _DIAGASSERT(pai != NULL);
1145
1146 memset(&sentinel, 0, sizeof(sentinel));
1147 cur = &sentinel;
1148
1149 canonname = NULL;
1150 eom = answer->buf + anslen;
1151 switch (qtype) {
1152 case T_A:
1153 case T_AAAA:
1154 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1155 name_ok = res_hnok;
1156 break;
1157 default:
1158 return (NULL); /* XXX should be abort(); */
1159 }
1160 /*
1161 * find first satisfactory answer
1162 */
1163 hp = &answer->hdr;
1164 ancount = ntohs(hp->ancount);
1165 qdcount = ntohs(hp->qdcount);
1166 bp = hostbuf;
1167 ep = hostbuf + sizeof hostbuf;
1168 cp = answer->buf + HFIXEDSZ;
1169 if (qdcount != 1) {
1170 h_errno = NO_RECOVERY;
1171 return (NULL);
1172 }
1173 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1174 if ((n < 0) || !(*name_ok)(bp)) {
1175 h_errno = NO_RECOVERY;
1176 return (NULL);
1177 }
1178 cp += n + QFIXEDSZ;
1179 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1180 /* res_send() has already verified that the query name is the
1181 * same as the one we sent; this just gets the expanded name
1182 * (i.e., with the succeeding search-domain tacked on).
1183 */
1184 n = strlen(bp) + 1; /* for the \0 */
1185 if (n >= MAXHOSTNAMELEN) {
1186 h_errno = NO_RECOVERY;
1187 return (NULL);
1188 }
1189 canonname = bp;
1190 bp += n;
1191 /* The qname can be abbreviated, but h_name is now absolute. */
1192 qname = canonname;
1193 }
1194 haveanswer = 0;
1195 had_error = 0;
1196 while (ancount-- > 0 && cp < eom && !had_error) {
1197 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1198 if ((n < 0) || !(*name_ok)(bp)) {
1199 had_error++;
1200 continue;
1201 }
1202 cp += n; /* name */
1203 type = _getshort(cp);
1204 cp += INT16SZ; /* type */
1205 class = _getshort(cp);
1206 cp += INT16SZ + INT32SZ; /* class, TTL */
1207 n = _getshort(cp);
1208 cp += INT16SZ; /* len */
1209 if (class != C_IN) {
1210 /* XXX - debug? syslog? */
1211 cp += n;
1212 continue; /* XXX - had_error++ ? */
1213 }
1214 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1215 type == T_CNAME) {
1216 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1217 if ((n < 0) || !(*name_ok)(tbuf)) {
1218 had_error++;
1219 continue;
1220 }
1221 cp += n;
1222 /* Get canonical name. */
1223 n = strlen(tbuf) + 1; /* for the \0 */
1224 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1225 had_error++;
1226 continue;
1227 }
1228 strcpy(bp, tbuf);
1229 canonname = bp;
1230 bp += n;
1231 continue;
1232 }
1233 if (qtype == T_ANY) {
1234 if (!(type == T_A || type == T_AAAA)) {
1235 cp += n;
1236 continue;
1237 }
1238 } else if (type != qtype) {
1239 if (type != T_KEY && type != T_SIG)
1240 syslog(LOG_NOTICE|LOG_AUTH,
1241 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1242 qname, p_class(C_IN), p_type(qtype),
1243 p_type(type));
1244 cp += n;
1245 continue; /* XXX - had_error++ ? */
1246 }
1247 switch (type) {
1248 case T_A:
1249 case T_AAAA:
1250 if (strcasecmp(canonname, bp) != 0) {
1251 syslog(LOG_NOTICE|LOG_AUTH,
1252 AskedForGot, canonname, bp);
1253 cp += n;
1254 continue; /* XXX - had_error++ ? */
1255 }
1256 if (type == T_A && n != INADDRSZ) {
1257 cp += n;
1258 continue;
1259 }
1260 if (type == T_AAAA && n != IN6ADDRSZ) {
1261 cp += n;
1262 continue;
1263 }
1264 if (!haveanswer) {
1265 int nn;
1266
1267 canonname = bp;
1268 nn = strlen(bp) + 1; /* for the \0 */
1269 bp += nn;
1270 }
1271
1272 /* don't overwrite pai */
1273 ai = *pai;
1274 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1275 afd = find_afd(ai.ai_family);
1276 if (afd == NULL) {
1277 cp += n;
1278 continue;
1279 }
1280 cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1281 if (cur->ai_next == NULL)
1282 had_error++;
1283 while (cur && cur->ai_next)
1284 cur = cur->ai_next;
1285 cp += n;
1286 break;
1287 default:
1288 abort();
1289 }
1290 if (!had_error)
1291 haveanswer++;
1292 }
1293 if (haveanswer) {
1294 if (!canonname)
1295 (void)get_canonname(pai, sentinel.ai_next, qname);
1296 else
1297 (void)get_canonname(pai, sentinel.ai_next, canonname);
1298 h_errno = NETDB_SUCCESS;
1299 return sentinel.ai_next;
1300 }
1301
1302 h_errno = NO_RECOVERY;
1303 return NULL;
1304 }
1305
1306 /*ARGSUSED*/
1307 static int
1308 _dns_getaddrinfo(rv, cb_data, ap)
1309 void *rv;
1310 void *cb_data;
1311 va_list ap;
1312 {
1313 struct addrinfo *ai;
1314 querybuf buf, buf2;
1315 const char *name;
1316 const struct addrinfo *pai;
1317 struct addrinfo sentinel, *cur;
1318 struct res_target q, q2;
1319
1320 name = va_arg(ap, char *);
1321 pai = va_arg(ap, const struct addrinfo *);
1322
1323 memset(&q, 0, sizeof(q2));
1324 memset(&q2, 0, sizeof(q2));
1325 memset(&sentinel, 0, sizeof(sentinel));
1326 cur = &sentinel;
1327
1328 switch (pai->ai_family) {
1329 case AF_UNSPEC:
1330 /* prefer IPv6 */
1331 q.name = name;
1332 q.qclass = C_IN;
1333 q.qtype = T_AAAA;
1334 q.answer = buf.buf;
1335 q.anslen = sizeof(buf);
1336 q.next = &q2;
1337 q2.name = name;
1338 q2.qclass = C_IN;
1339 q2.qtype = T_A;
1340 q2.answer = buf2.buf;
1341 q2.anslen = sizeof(buf2);
1342 break;
1343 case AF_INET:
1344 q.name = name;
1345 q.qclass = C_IN;
1346 q.qtype = T_A;
1347 q.answer = buf.buf;
1348 q.anslen = sizeof(buf);
1349 break;
1350 case AF_INET6:
1351 q.name = name;
1352 q.qclass = C_IN;
1353 q.qtype = T_AAAA;
1354 q.answer = buf.buf;
1355 q.anslen = sizeof(buf);
1356 break;
1357 default:
1358 return NS_UNAVAIL;
1359 }
1360 if (res_searchN(name, &q) < 0)
1361 return NS_NOTFOUND;
1362 ai = getanswer(&buf, q.n, q.name, q.qtype, pai);
1363 if (ai) {
1364 cur->ai_next = ai;
1365 while (cur && cur->ai_next)
1366 cur = cur->ai_next;
1367 }
1368 if (q.next) {
1369 ai = getanswer(&buf2, q2.n, q2.name, q2.qtype, pai);
1370 if (ai)
1371 cur->ai_next = ai;
1372 }
1373 if (sentinel.ai_next == NULL)
1374 switch (h_errno) {
1375 case HOST_NOT_FOUND:
1376 return NS_NOTFOUND;
1377 case TRY_AGAIN:
1378 return NS_TRYAGAIN;
1379 default:
1380 return NS_UNAVAIL;
1381 }
1382 *((struct addrinfo **)rv) = sentinel.ai_next;
1383 return NS_SUCCESS;
1384 }
1385
1386 static void
1387 _sethtent()
1388 {
1389
1390 if (!hostf)
1391 hostf = fopen(_PATH_HOSTS, "r" );
1392 else
1393 rewind(hostf);
1394 }
1395
1396 static void
1397 _endhtent()
1398 {
1399
1400 if (hostf) {
1401 (void) fclose(hostf);
1402 hostf = NULL;
1403 }
1404 }
1405
1406 static struct addrinfo *
1407 _gethtent(name, pai)
1408 const char *name;
1409 const struct addrinfo *pai;
1410 {
1411 char *p;
1412 char *cp, *tname, *cname;
1413 struct addrinfo hints, *res0, *res;
1414 int error;
1415 const char *addr;
1416 char hostbuf[8*1024];
1417
1418 _DIAGASSERT(name != NULL);
1419 _DIAGASSERT(pai != NULL);
1420
1421 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))
1422 return (NULL);
1423 again:
1424 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
1425 return (NULL);
1426 if (*p == '#')
1427 goto again;
1428 if (!(cp = strpbrk(p, "#\n")))
1429 goto again;
1430 *cp = '\0';
1431 if (!(cp = strpbrk(p, " \t")))
1432 goto again;
1433 *cp++ = '\0';
1434 addr = p;
1435 /* if this is not something we're looking for, skip it. */
1436 cname = NULL;
1437 while (cp && *cp) {
1438 if (*cp == ' ' || *cp == '\t') {
1439 cp++;
1440 continue;
1441 }
1442 if (!cname)
1443 cname = cp;
1444 tname = cp;
1445 if ((cp = strpbrk(cp, " \t")) != NULL)
1446 *cp++ = '\0';
1447 if (strcasecmp(name, tname) == 0)
1448 goto found;
1449 }
1450 goto again;
1451
1452 found:
1453 hints = *pai;
1454 hints.ai_flags = AI_NUMERICHOST;
1455 error = getaddrinfo(addr, NULL, &hints, &res0);
1456 if (error)
1457 goto again;
1458 for (res = res0; res; res = res->ai_next) {
1459 /* cover it up */
1460 res->ai_flags = pai->ai_flags;
1461
1462 if (pai->ai_flags & AI_CANONNAME) {
1463 if (get_canonname(pai, res, cname) != 0) {
1464 freeaddrinfo(res0);
1465 goto again;
1466 }
1467 }
1468 }
1469 return res0;
1470 }
1471
1472 /*ARGSUSED*/
1473 static int
1474 _files_getaddrinfo(rv, cb_data, ap)
1475 void *rv;
1476 void *cb_data;
1477 va_list ap;
1478 {
1479 const char *name;
1480 const struct addrinfo *pai;
1481 struct addrinfo sentinel, *cur;
1482 struct addrinfo *p;
1483
1484 name = va_arg(ap, char *);
1485 pai = va_arg(ap, struct addrinfo *);
1486
1487 memset(&sentinel, 0, sizeof(sentinel));
1488 cur = &sentinel;
1489
1490 _sethtent();
1491 while ((p = _gethtent(name, pai)) != NULL) {
1492 cur->ai_next = p;
1493 while (cur && cur->ai_next)
1494 cur = cur->ai_next;
1495 }
1496 _endhtent();
1497
1498 *((struct addrinfo **)rv) = sentinel.ai_next;
1499 if (sentinel.ai_next == NULL)
1500 return NS_NOTFOUND;
1501 return NS_SUCCESS;
1502 }
1503
1504 #ifdef YP
1505 static char *__ypdomain;
1506
1507 /*ARGSUSED*/
1508 static struct addrinfo *
1509 _yphostent(line, pai)
1510 char *line;
1511 const struct addrinfo *pai;
1512 {
1513 struct addrinfo sentinel, *cur;
1514 struct addrinfo hints, *res, *res0;
1515 int error;
1516 char *p;
1517 const char *addr, *canonname;
1518 char *nextline;
1519 char *cp;
1520
1521 _DIAGASSERT(line != NULL);
1522 _DIAGASSERT(pai != NULL);
1523
1524 p = line;
1525 addr = canonname = NULL;
1526
1527 memset(&sentinel, 0, sizeof(sentinel));
1528 cur = &sentinel;
1529
1530 nextline:
1531 /* terminate line */
1532 cp = strchr(p, '\n');
1533 if (cp) {
1534 *cp++ = '\0';
1535 nextline = cp;
1536 } else
1537 nextline = NULL;
1538
1539 cp = strpbrk(p, " \t");
1540 if (cp == NULL) {
1541 if (canonname == NULL)
1542 return (NULL);
1543 else
1544 goto done;
1545 }
1546 *cp++ = '\0';
1547
1548 addr = p;
1549
1550 while (cp && *cp) {
1551 if (*cp == ' ' || *cp == '\t') {
1552 cp++;
1553 continue;
1554 }
1555 if (!canonname)
1556 canonname = cp;
1557 if ((cp = strpbrk(cp, " \t")) != NULL)
1558 *cp++ = '\0';
1559 }
1560
1561 hints = *pai;
1562 hints.ai_flags = AI_NUMERICHOST;
1563 error = getaddrinfo(addr, NULL, &hints, &res0);
1564 if (error == 0) {
1565 for (res = res0; res; res = res->ai_next) {
1566 /* cover it up */
1567 res->ai_flags = pai->ai_flags;
1568
1569 if (pai->ai_flags & AI_CANONNAME)
1570 (void)get_canonname(pai, res, canonname);
1571 }
1572 } else
1573 res0 = NULL;
1574 if (res0) {
1575 cur->ai_next = res0;
1576 while (cur && cur->ai_next)
1577 cur = cur->ai_next;
1578 }
1579
1580 if (nextline) {
1581 p = nextline;
1582 goto nextline;
1583 }
1584
1585 done:
1586 return sentinel.ai_next;
1587 }
1588
1589 /*ARGSUSED*/
1590 static int
1591 _yp_getaddrinfo(rv, cb_data, ap)
1592 void *rv;
1593 void *cb_data;
1594 va_list ap;
1595 {
1596 struct addrinfo sentinel, *cur;
1597 struct addrinfo *ai = NULL;
1598 static char *__ypcurrent;
1599 int __ypcurrentlen, r;
1600 const char *name;
1601 const struct addrinfo *pai;
1602
1603 name = va_arg(ap, char *);
1604 pai = va_arg(ap, const struct addrinfo *);
1605
1606 memset(&sentinel, 0, sizeof(sentinel));
1607 cur = &sentinel;
1608
1609 if (!__ypdomain) {
1610 if (_yp_check(&__ypdomain) == 0)
1611 return NS_UNAVAIL;
1612 }
1613 if (__ypcurrent)
1614 free(__ypcurrent);
1615 __ypcurrent = NULL;
1616
1617 /* hosts.byname is only for IPv4 (Solaris8) */
1618 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
1619 r = yp_match(__ypdomain, "hosts.byname", name,
1620 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1621 if (r == 0) {
1622 struct addrinfo ai4;
1623
1624 ai4 = *pai;
1625 ai4.ai_family = AF_INET;
1626 ai = _yphostent(__ypcurrent, &ai4);
1627 if (ai) {
1628 cur->ai_next = ai;
1629 while (cur && cur->ai_next)
1630 cur = cur->ai_next;
1631 }
1632 }
1633 }
1634
1635 /* ipnodes.byname can hold both IPv4/v6 */
1636 r = yp_match(__ypdomain, "ipnodes.byname", name,
1637 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1638 if (r == 0) {
1639 ai = _yphostent(__ypcurrent, pai);
1640 if (ai) {
1641 cur->ai_next = ai;
1642 while (cur && cur->ai_next)
1643 cur = cur->ai_next;
1644 }
1645 }
1646
1647 if (sentinel.ai_next == NULL) {
1648 h_errno = HOST_NOT_FOUND;
1649 return NS_NOTFOUND;
1650 }
1651 *((struct addrinfo **)rv) = sentinel.ai_next;
1652 return NS_SUCCESS;
1653 }
1654 #endif
1655
1656 /* resolver logic */
1657
1658 /*
1659 * Formulate a normal query, send, and await answer.
1660 * Returned answer is placed in supplied buffer "answer".
1661 * Perform preliminary check of answer, returning success only
1662 * if no error is indicated and the answer count is nonzero.
1663 * Return the size of the response on success, -1 on error.
1664 * Error number is left in h_errno.
1665 *
1666 * Caller must parse answer and determine whether it answers the question.
1667 */
1668 static int
1669 res_queryN(name, target)
1670 const char *name; /* domain name */
1671 struct res_target *target;
1672 {
1673 u_char buf[MAXPACKET];
1674 HEADER *hp;
1675 int n;
1676 struct res_target *t;
1677 int rcode;
1678 int ancount;
1679
1680 _DIAGASSERT(name != NULL);
1681 /* XXX: target may be NULL??? */
1682
1683 rcode = NOERROR;
1684 ancount = 0;
1685
1686 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1687 h_errno = NETDB_INTERNAL;
1688 return (-1);
1689 }
1690
1691 for (t = target; t; t = t->next) {
1692 int class, type;
1693 u_char *answer;
1694 int anslen;
1695
1696 hp = (HEADER *)(void *)t->answer;
1697 hp->rcode = NOERROR; /* default */
1698
1699 /* make it easier... */
1700 class = t->qclass;
1701 type = t->qtype;
1702 answer = t->answer;
1703 anslen = t->anslen;
1704 #ifdef DEBUG
1705 if (_res.options & RES_DEBUG)
1706 printf(";; res_query(%s, %d, %d)\n", name, class, type);
1707 #endif
1708
1709 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
1710 buf, sizeof(buf));
1711 #ifdef RES_USE_EDNS0
1712 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
1713 n = res_opt(n, buf, sizeof(buf), anslen);
1714 #endif
1715 if (n <= 0) {
1716 #ifdef DEBUG
1717 if (_res.options & RES_DEBUG)
1718 printf(";; res_query: mkquery failed\n");
1719 #endif
1720 h_errno = NO_RECOVERY;
1721 return (n);
1722 }
1723 n = res_send(buf, n, answer, anslen);
1724 #if 0
1725 if (n < 0) {
1726 #ifdef DEBUG
1727 if (_res.options & RES_DEBUG)
1728 printf(";; res_query: send error\n");
1729 #endif
1730 h_errno = TRY_AGAIN;
1731 return (n);
1732 }
1733 #endif
1734
1735 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1736 rcode = hp->rcode; /* record most recent error */
1737 #ifdef DEBUG
1738 if (_res.options & RES_DEBUG)
1739 printf(";; rcode = %u, ancount=%u\n", hp->rcode,
1740 ntohs(hp->ancount));
1741 #endif
1742 continue;
1743 }
1744
1745 ancount += ntohs(hp->ancount);
1746
1747 t->n = n;
1748 }
1749
1750 if (ancount == 0) {
1751 switch (rcode) {
1752 case NXDOMAIN:
1753 h_errno = HOST_NOT_FOUND;
1754 break;
1755 case SERVFAIL:
1756 h_errno = TRY_AGAIN;
1757 break;
1758 case NOERROR:
1759 h_errno = NO_DATA;
1760 break;
1761 case FORMERR:
1762 case NOTIMP:
1763 case REFUSED:
1764 default:
1765 h_errno = NO_RECOVERY;
1766 break;
1767 }
1768 return (-1);
1769 }
1770 return (ancount);
1771 }
1772
1773 /*
1774 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1775 * Return the size of the response on success, -1 on error.
1776 * If enabled, implement search rules until answer or unrecoverable failure
1777 * is detected. Error code, if any, is left in h_errno.
1778 */
1779 static int
1780 res_searchN(name, target)
1781 const char *name; /* domain name */
1782 struct res_target *target;
1783 {
1784 const char *cp, * const *domain;
1785 HEADER *hp;
1786 u_int dots;
1787 int trailing_dot, ret, saved_herrno;
1788 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1789
1790 _DIAGASSERT(name != NULL);
1791 _DIAGASSERT(target != NULL);
1792
1793 hp = (HEADER *)(void *)target->answer; /*XXX*/
1794
1795 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1796 h_errno = NETDB_INTERNAL;
1797 return (-1);
1798 }
1799
1800 errno = 0;
1801 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1802 dots = 0;
1803 for (cp = name; *cp; cp++)
1804 dots += (*cp == '.');
1805 trailing_dot = 0;
1806 if (cp > name && *--cp == '.')
1807 trailing_dot++;
1808
1809 /*
1810 * if there aren't any dots, it could be a user-level alias
1811 */
1812 if (!dots && (cp = __hostalias(name)) != NULL)
1813 return (res_queryN(cp, target));
1814
1815 /*
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.
1818 */
1819 saved_herrno = -1;
1820 if (dots >= _res.ndots) {
1821 ret = res_querydomainN(name, NULL, target);
1822 if (ret > 0)
1823 return (ret);
1824 saved_herrno = h_errno;
1825 tried_as_is++;
1826 }
1827
1828 /*
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.
1833 */
1834 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1835 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1836 int done = 0;
1837
1838 for (domain = (const char * const *)_res.dnsrch;
1839 *domain && !done;
1840 domain++) {
1841
1842 ret = res_querydomainN(name, *domain, target);
1843 if (ret > 0)
1844 return (ret);
1845
1846 /*
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
1857 * fully-qualified.
1858 */
1859 if (errno == ECONNREFUSED) {
1860 h_errno = TRY_AGAIN;
1861 return (-1);
1862 }
1863
1864 switch (h_errno) {
1865 case NO_DATA:
1866 got_nodata++;
1867 /* FALLTHROUGH */
1868 case HOST_NOT_FOUND:
1869 /* keep trying */
1870 break;
1871 case TRY_AGAIN:
1872 if (hp->rcode == SERVFAIL) {
1873 /* try next search element, if any */
1874 got_servfail++;
1875 break;
1876 }
1877 /* FALLTHROUGH */
1878 default:
1879 /* anything else implies that we're done */
1880 done++;
1881 }
1882 /*
1883 * if we got here for some reason other than DNSRCH,
1884 * we only wanted one iteration of the loop, so stop.
1885 */
1886 if (!(_res.options & RES_DNSRCH))
1887 done++;
1888 }
1889 }
1890
1891 /*
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.
1895 */
1896 if (!tried_as_is) {
1897 ret = res_querydomainN(name, NULL, target);
1898 if (ret > 0)
1899 return (ret);
1900 }
1901
1902 /*
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.
1909 */
1910 if (saved_herrno != -1)
1911 h_errno = saved_herrno;
1912 else if (got_nodata)
1913 h_errno = NO_DATA;
1914 else if (got_servfail)
1915 h_errno = TRY_AGAIN;
1916 return (-1);
1917 }
1918
1919 /*
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.
1922 */
1923 static int
1924 res_querydomainN(name, domain, target)
1925 const char *name, *domain;
1926 struct res_target *target;
1927 {
1928 char nbuf[MAXDNAME];
1929 const char *longname = nbuf;
1930 size_t n, d;
1931
1932 _DIAGASSERT(name != NULL);
1933 /* XXX: target may be NULL??? */
1934
1935 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1936 h_errno = NETDB_INTERNAL;
1937 return (-1);
1938 }
1939 #ifdef DEBUG
1940 if (_res.options & RES_DEBUG)
1941 printf(";; res_querydomain(%s, %s)\n",
1942 name, domain?domain:"<Nil>");
1943 #endif
1944 if (domain == NULL) {
1945 /*
1946 * Check for trailing '.';
1947 * copy without '.' if present.
1948 */
1949 n = strlen(name);
1950 if (n >= MAXDNAME) {
1951 h_errno = NO_RECOVERY;
1952 return (-1);
1953 }
1954 if (n > 0 && name[--n] == '.') {
1955 strncpy(nbuf, name, n);
1956 nbuf[n] = '\0';
1957 } else
1958 longname = name;
1959 } else {
1960 n = strlen(name);
1961 d = strlen(domain);
1962 if (n + d + 1 >= MAXDNAME) {
1963 h_errno = NO_RECOVERY;
1964 return (-1);
1965 }
1966 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1967 }
1968 return (res_queryN(longname, target));
1969 }
1970