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