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