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