getaddrinfo.c revision 1.55.2.1 1 /* $NetBSD: getaddrinfo.c,v 1.55.2.1 2002/06/29 22:48:11 lukem 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.55.2.1 2002/06/29 22:48:11 lukem 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 = atoi(servname);
981 if (port < 0 || port > 65535)
982 return EAI_SERVICE;
983 port = htons(port);
984 } else {
985 switch (ai->ai_socktype) {
986 case SOCK_DGRAM:
987 proto = "udp";
988 break;
989 case SOCK_STREAM:
990 proto = "tcp";
991 break;
992 default:
993 proto = NULL;
994 break;
995 }
996
997 if ((sp = getservbyname(servname, proto)) == NULL)
998 return EAI_SERVICE;
999 port = sp->s_port;
1000 }
1001
1002 if (!matchonly) {
1003 switch (ai->ai_family) {
1004 case AF_INET:
1005 ((struct sockaddr_in *)(void *)
1006 ai->ai_addr)->sin_port = port;
1007 break;
1008 #ifdef INET6
1009 case AF_INET6:
1010 ((struct sockaddr_in6 *)(void *)
1011 ai->ai_addr)->sin6_port = port;
1012 break;
1013 #endif
1014 }
1015 }
1016
1017 return 0;
1018 }
1019
1020 static const struct afd *
1021 find_afd(af)
1022 int af;
1023 {
1024 const struct afd *afd;
1025
1026 if (af == PF_UNSPEC)
1027 return NULL;
1028 for (afd = afdl; afd->a_af; afd++) {
1029 if (afd->a_af == af)
1030 return afd;
1031 }
1032 return NULL;
1033 }
1034
1035 #if 0
1036 /*
1037 * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend
1038 * will take care of it.
1039 * the semantics of AI_ADDRCONFIG is not defined well. we are not sure
1040 * if the code is right or not.
1041 */
1042 static int
1043 addrconfig(pai)
1044 const struct addrinfo *pai;
1045 {
1046 int s;
1047
1048 _DIAGASSERT(pai != NULL);
1049
1050 /* XXX errno */
1051 s = socket(pai->ai_family, SOCK_DGRAM, 0);
1052 if (s < 0)
1053 return 0;
1054 close(s);
1055 return 1;
1056 }
1057 #endif
1058
1059 #ifdef INET6
1060 /* convert a string to a scope identifier. XXX: IPv6 specific */
1061 static int
1062 ip6_str2scopeid(scope, sin6)
1063 char *scope;
1064 struct sockaddr_in6 *sin6;
1065 {
1066 int scopeid;
1067 struct in6_addr *a6;
1068 char *ep;
1069
1070 _DIAGASSERT(scope != NULL);
1071 _DIAGASSERT(sin6 != NULL);
1072
1073 a6 = &sin6->sin6_addr;
1074
1075 /* empty scopeid portion is invalid */
1076 if (*scope == '\0')
1077 return -1;
1078
1079 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1080 /*
1081 * We currently assume a one-to-one mapping between links
1082 * and interfaces, so we simply use interface indices for
1083 * like-local scopes.
1084 */
1085 scopeid = if_nametoindex(scope);
1086 if (scopeid == 0)
1087 goto trynumeric;
1088 return(scopeid);
1089 }
1090
1091 /* still unclear about literal, allow numeric only - placeholder */
1092 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1093 goto trynumeric;
1094 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1095 goto trynumeric;
1096 else
1097 goto trynumeric; /* global */
1098
1099 /* try to convert to a numeric id as a last resort */
1100 trynumeric:
1101 scopeid = (int)strtoul(scope, &ep, 10);
1102 if (*ep == '\0')
1103 return scopeid;
1104 else
1105 return -1;
1106 }
1107 #endif
1108
1109 /* code duplicate with gethnamaddr.c */
1110
1111 static const char AskedForGot[] =
1112 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1113 static FILE *hostf = NULL;
1114
1115 static struct addrinfo *
1116 getanswer(answer, anslen, qname, qtype, pai)
1117 const querybuf *answer;
1118 int anslen;
1119 const char *qname;
1120 int qtype;
1121 const struct addrinfo *pai;
1122 {
1123 struct addrinfo sentinel, *cur;
1124 struct addrinfo ai;
1125 const struct afd *afd;
1126 char *canonname;
1127 const HEADER *hp;
1128 const u_char *cp;
1129 int n;
1130 const u_char *eom;
1131 char *bp;
1132 int type, class, buflen, ancount, qdcount;
1133 int haveanswer, had_error;
1134 char tbuf[MAXDNAME];
1135 int (*name_ok) __P((const char *));
1136 char hostbuf[8*1024];
1137
1138 _DIAGASSERT(answer != NULL);
1139 _DIAGASSERT(qname != NULL);
1140 _DIAGASSERT(pai != NULL);
1141
1142 memset(&sentinel, 0, sizeof(sentinel));
1143 cur = &sentinel;
1144
1145 canonname = NULL;
1146 eom = answer->buf + anslen;
1147 switch (qtype) {
1148 case T_A:
1149 case T_AAAA:
1150 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1151 name_ok = res_hnok;
1152 break;
1153 default:
1154 return (NULL); /* XXX should be abort(); */
1155 }
1156 /*
1157 * find first satisfactory answer
1158 */
1159 hp = &answer->hdr;
1160 ancount = ntohs(hp->ancount);
1161 qdcount = ntohs(hp->qdcount);
1162 bp = hostbuf;
1163 buflen = sizeof hostbuf;
1164 cp = answer->buf + HFIXEDSZ;
1165 if (qdcount != 1) {
1166 h_errno = NO_RECOVERY;
1167 return (NULL);
1168 }
1169 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1170 if ((n < 0) || !(*name_ok)(bp)) {
1171 h_errno = NO_RECOVERY;
1172 return (NULL);
1173 }
1174 cp += n + QFIXEDSZ;
1175 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1176 /* res_send() has already verified that the query name is the
1177 * same as the one we sent; this just gets the expanded name
1178 * (i.e., with the succeeding search-domain tacked on).
1179 */
1180 n = strlen(bp) + 1; /* for the \0 */
1181 if (n >= MAXHOSTNAMELEN) {
1182 h_errno = NO_RECOVERY;
1183 return (NULL);
1184 }
1185 canonname = bp;
1186 bp += n;
1187 buflen -= n;
1188 /* The qname can be abbreviated, but h_name is now absolute. */
1189 qname = canonname;
1190 }
1191 haveanswer = 0;
1192 had_error = 0;
1193 while (ancount-- > 0 && cp < eom && !had_error) {
1194 n = dn_expand(answer->buf, eom, cp, bp, buflen);
1195 if ((n < 0) || !(*name_ok)(bp)) {
1196 had_error++;
1197 continue;
1198 }
1199 cp += n; /* name */
1200 type = _getshort(cp);
1201 cp += INT16SZ; /* type */
1202 class = _getshort(cp);
1203 cp += INT16SZ + INT32SZ; /* class, TTL */
1204 n = _getshort(cp);
1205 cp += INT16SZ; /* len */
1206 if (class != C_IN) {
1207 /* XXX - debug? syslog? */
1208 cp += n;
1209 continue; /* XXX - had_error++ ? */
1210 }
1211 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1212 type == T_CNAME) {
1213 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1214 if ((n < 0) || !(*name_ok)(tbuf)) {
1215 had_error++;
1216 continue;
1217 }
1218 cp += n;
1219 /* Get canonical name. */
1220 n = strlen(tbuf) + 1; /* for the \0 */
1221 if (n > buflen || n >= MAXHOSTNAMELEN) {
1222 had_error++;
1223 continue;
1224 }
1225 strcpy(bp, tbuf);
1226 canonname = bp;
1227 bp += n;
1228 buflen -= n;
1229 continue;
1230 }
1231 if (qtype == T_ANY) {
1232 if (!(type == T_A || type == T_AAAA)) {
1233 cp += n;
1234 continue;
1235 }
1236 } else if (type != qtype) {
1237 if (type != T_KEY && type != T_SIG)
1238 syslog(LOG_NOTICE|LOG_AUTH,
1239 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1240 qname, p_class(C_IN), p_type(qtype),
1241 p_type(type));
1242 cp += n;
1243 continue; /* XXX - had_error++ ? */
1244 }
1245 switch (type) {
1246 case T_A:
1247 case T_AAAA:
1248 if (strcasecmp(canonname, bp) != 0) {
1249 syslog(LOG_NOTICE|LOG_AUTH,
1250 AskedForGot, canonname, bp);
1251 cp += n;
1252 continue; /* XXX - had_error++ ? */
1253 }
1254 if (type == T_A && n != INADDRSZ) {
1255 cp += n;
1256 continue;
1257 }
1258 if (type == T_AAAA && n != IN6ADDRSZ) {
1259 cp += n;
1260 continue;
1261 }
1262 if (!haveanswer) {
1263 int nn;
1264
1265 canonname = bp;
1266 nn = strlen(bp) + 1; /* for the \0 */
1267 bp += nn;
1268 buflen -= nn;
1269 }
1270
1271 /* don't overwrite pai */
1272 ai = *pai;
1273 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1274 afd = find_afd(ai.ai_family);
1275 if (afd == NULL) {
1276 cp += n;
1277 continue;
1278 }
1279 cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1280 if (cur->ai_next == NULL)
1281 had_error++;
1282 while (cur && cur->ai_next)
1283 cur = cur->ai_next;
1284 cp += n;
1285 break;
1286 default:
1287 abort();
1288 }
1289 if (!had_error)
1290 haveanswer++;
1291 }
1292 if (haveanswer) {
1293 if (!canonname)
1294 (void)get_canonname(pai, sentinel.ai_next, qname);
1295 else
1296 (void)get_canonname(pai, sentinel.ai_next, canonname);
1297 h_errno = NETDB_SUCCESS;
1298 return sentinel.ai_next;
1299 }
1300
1301 h_errno = NO_RECOVERY;
1302 return NULL;
1303 }
1304
1305 /*ARGSUSED*/
1306 static int
1307 _dns_getaddrinfo(rv, cb_data, ap)
1308 void *rv;
1309 void *cb_data;
1310 va_list ap;
1311 {
1312 struct addrinfo *ai;
1313 querybuf buf, buf2;
1314 const char *name;
1315 const struct addrinfo *pai;
1316 struct addrinfo sentinel, *cur;
1317 struct res_target q, q2;
1318
1319 name = va_arg(ap, char *);
1320 pai = va_arg(ap, const struct addrinfo *);
1321
1322 memset(&q, 0, sizeof(q2));
1323 memset(&q2, 0, sizeof(q2));
1324 memset(&sentinel, 0, sizeof(sentinel));
1325 cur = &sentinel;
1326
1327 switch (pai->ai_family) {
1328 case AF_UNSPEC:
1329 /* prefer IPv6 */
1330 q.name = name;
1331 q.qclass = C_IN;
1332 q.qtype = T_AAAA;
1333 q.answer = buf.buf;
1334 q.anslen = sizeof(buf);
1335 q.next = &q2;
1336 q2.name = name;
1337 q2.qclass = C_IN;
1338 q2.qtype = T_A;
1339 q2.answer = buf2.buf;
1340 q2.anslen = sizeof(buf2);
1341 break;
1342 case AF_INET:
1343 q.name = name;
1344 q.qclass = C_IN;
1345 q.qtype = T_A;
1346 q.answer = buf.buf;
1347 q.anslen = sizeof(buf);
1348 break;
1349 case AF_INET6:
1350 q.name = name;
1351 q.qclass = C_IN;
1352 q.qtype = T_AAAA;
1353 q.answer = buf.buf;
1354 q.anslen = sizeof(buf);
1355 break;
1356 default:
1357 return NS_UNAVAIL;
1358 }
1359 if (res_searchN(name, &q) < 0)
1360 return NS_NOTFOUND;
1361 ai = getanswer(&buf, q.n, q.name, q.qtype, pai);
1362 if (ai) {
1363 cur->ai_next = ai;
1364 while (cur && cur->ai_next)
1365 cur = cur->ai_next;
1366 }
1367 if (q.next) {
1368 ai = getanswer(&buf2, q2.n, q2.name, q2.qtype, pai);
1369 if (ai)
1370 cur->ai_next = ai;
1371 }
1372 if (sentinel.ai_next == NULL)
1373 switch (h_errno) {
1374 case HOST_NOT_FOUND:
1375 return NS_NOTFOUND;
1376 case TRY_AGAIN:
1377 return NS_TRYAGAIN;
1378 default:
1379 return NS_UNAVAIL;
1380 }
1381 *((struct addrinfo **)rv) = sentinel.ai_next;
1382 return NS_SUCCESS;
1383 }
1384
1385 static void
1386 _sethtent()
1387 {
1388
1389 if (!hostf)
1390 hostf = fopen(_PATH_HOSTS, "r" );
1391 else
1392 rewind(hostf);
1393 }
1394
1395 static void
1396 _endhtent()
1397 {
1398
1399 if (hostf) {
1400 (void) fclose(hostf);
1401 hostf = NULL;
1402 }
1403 }
1404
1405 static struct addrinfo *
1406 _gethtent(name, pai)
1407 const char *name;
1408 const struct addrinfo *pai;
1409 {
1410 char *p;
1411 char *cp, *tname, *cname;
1412 struct addrinfo hints, *res0, *res;
1413 int error;
1414 const char *addr;
1415 char hostbuf[8*1024];
1416
1417 _DIAGASSERT(name != NULL);
1418 _DIAGASSERT(pai != NULL);
1419
1420 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))
1421 return (NULL);
1422 again:
1423 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
1424 return (NULL);
1425 if (*p == '#')
1426 goto again;
1427 if (!(cp = strpbrk(p, "#\n")))
1428 goto again;
1429 *cp = '\0';
1430 if (!(cp = strpbrk(p, " \t")))
1431 goto again;
1432 *cp++ = '\0';
1433 addr = p;
1434 /* if this is not something we're looking for, skip it. */
1435 cname = NULL;
1436 while (cp && *cp) {
1437 if (*cp == ' ' || *cp == '\t') {
1438 cp++;
1439 continue;
1440 }
1441 if (!cname)
1442 cname = cp;
1443 tname = cp;
1444 if ((cp = strpbrk(cp, " \t")) != NULL)
1445 *cp++ = '\0';
1446 if (strcasecmp(name, tname) == 0)
1447 goto found;
1448 }
1449 goto again;
1450
1451 found:
1452 hints = *pai;
1453 hints.ai_flags = AI_NUMERICHOST;
1454 error = getaddrinfo(addr, NULL, &hints, &res0);
1455 if (error)
1456 goto again;
1457 for (res = res0; res; res = res->ai_next) {
1458 /* cover it up */
1459 res->ai_flags = pai->ai_flags;
1460
1461 if (pai->ai_flags & AI_CANONNAME) {
1462 if (get_canonname(pai, res, cname) != 0) {
1463 freeaddrinfo(res0);
1464 goto again;
1465 }
1466 }
1467 }
1468 return res0;
1469 }
1470
1471 /*ARGSUSED*/
1472 static int
1473 _files_getaddrinfo(rv, cb_data, ap)
1474 void *rv;
1475 void *cb_data;
1476 va_list ap;
1477 {
1478 const char *name;
1479 const struct addrinfo *pai;
1480 struct addrinfo sentinel, *cur;
1481 struct addrinfo *p;
1482
1483 name = va_arg(ap, char *);
1484 pai = va_arg(ap, struct addrinfo *);
1485
1486 memset(&sentinel, 0, sizeof(sentinel));
1487 cur = &sentinel;
1488
1489 _sethtent();
1490 while ((p = _gethtent(name, pai)) != NULL) {
1491 cur->ai_next = p;
1492 while (cur && cur->ai_next)
1493 cur = cur->ai_next;
1494 }
1495 _endhtent();
1496
1497 *((struct addrinfo **)rv) = sentinel.ai_next;
1498 if (sentinel.ai_next == NULL)
1499 return NS_NOTFOUND;
1500 return NS_SUCCESS;
1501 }
1502
1503 #ifdef YP
1504 static char *__ypdomain;
1505
1506 /*ARGSUSED*/
1507 static struct addrinfo *
1508 _yphostent(line, pai)
1509 char *line;
1510 const struct addrinfo *pai;
1511 {
1512 struct addrinfo sentinel, *cur;
1513 struct addrinfo hints, *res, *res0;
1514 int error;
1515 char *p;
1516 const char *addr, *canonname;
1517 char *nextline;
1518 char *cp;
1519
1520 _DIAGASSERT(line != NULL);
1521 _DIAGASSERT(pai != NULL);
1522
1523 p = line;
1524 addr = canonname = NULL;
1525
1526 memset(&sentinel, 0, sizeof(sentinel));
1527 cur = &sentinel;
1528
1529 nextline:
1530 /* terminate line */
1531 cp = strchr(p, '\n');
1532 if (cp) {
1533 *cp++ = '\0';
1534 nextline = cp;
1535 } else
1536 nextline = NULL;
1537
1538 cp = strpbrk(p, " \t");
1539 if (cp == NULL) {
1540 if (canonname == NULL)
1541 return (NULL);
1542 else
1543 goto done;
1544 }
1545 *cp++ = '\0';
1546
1547 addr = p;
1548
1549 while (cp && *cp) {
1550 if (*cp == ' ' || *cp == '\t') {
1551 cp++;
1552 continue;
1553 }
1554 if (!canonname)
1555 canonname = cp;
1556 if ((cp = strpbrk(cp, " \t")) != NULL)
1557 *cp++ = '\0';
1558 }
1559
1560 hints = *pai;
1561 hints.ai_flags = AI_NUMERICHOST;
1562 error = getaddrinfo(addr, NULL, &hints, &res0);
1563 if (error == 0) {
1564 for (res = res0; res; res = res->ai_next) {
1565 /* cover it up */
1566 res->ai_flags = pai->ai_flags;
1567
1568 if (pai->ai_flags & AI_CANONNAME)
1569 (void)get_canonname(pai, res, canonname);
1570 }
1571 } else
1572 res0 = NULL;
1573 if (res0) {
1574 cur->ai_next = res0;
1575 while (cur && cur->ai_next)
1576 cur = cur->ai_next;
1577 }
1578
1579 if (nextline) {
1580 p = nextline;
1581 goto nextline;
1582 }
1583
1584 done:
1585 return sentinel.ai_next;
1586 }
1587
1588 /*ARGSUSED*/
1589 static int
1590 _yp_getaddrinfo(rv, cb_data, ap)
1591 void *rv;
1592 void *cb_data;
1593 va_list ap;
1594 {
1595 struct addrinfo sentinel, *cur;
1596 struct addrinfo *ai = NULL;
1597 static char *__ypcurrent;
1598 int __ypcurrentlen, r;
1599 const char *name;
1600 const struct addrinfo *pai;
1601
1602 name = va_arg(ap, char *);
1603 pai = va_arg(ap, const struct addrinfo *);
1604
1605 memset(&sentinel, 0, sizeof(sentinel));
1606 cur = &sentinel;
1607
1608 if (!__ypdomain) {
1609 if (_yp_check(&__ypdomain) == 0)
1610 return NS_UNAVAIL;
1611 }
1612 if (__ypcurrent)
1613 free(__ypcurrent);
1614 __ypcurrent = NULL;
1615
1616 /* hosts.byname is only for IPv4 (Solaris8) */
1617 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
1618 r = yp_match(__ypdomain, "hosts.byname", name,
1619 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1620 if (r == 0) {
1621 struct addrinfo ai4;
1622
1623 ai4 = *pai;
1624 ai4.ai_family = AF_INET;
1625 ai = _yphostent(__ypcurrent, &ai4);
1626 if (ai) {
1627 cur->ai_next = ai;
1628 while (cur && cur->ai_next)
1629 cur = cur->ai_next;
1630 }
1631 }
1632 }
1633
1634 /* ipnodes.byname can hold both IPv4/v6 */
1635 r = yp_match(__ypdomain, "ipnodes.byname", name,
1636 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1637 if (r == 0) {
1638 ai = _yphostent(__ypcurrent, pai);
1639 if (ai) {
1640 cur->ai_next = ai;
1641 while (cur && cur->ai_next)
1642 cur = cur->ai_next;
1643 }
1644 }
1645
1646 if (sentinel.ai_next == NULL) {
1647 h_errno = HOST_NOT_FOUND;
1648 return NS_NOTFOUND;
1649 }
1650 *((struct addrinfo **)rv) = sentinel.ai_next;
1651 return NS_SUCCESS;
1652 }
1653 #endif
1654
1655 /* resolver logic */
1656
1657 /*
1658 * Formulate a normal query, send, and await answer.
1659 * Returned answer is placed in supplied buffer "answer".
1660 * Perform preliminary check of answer, returning success only
1661 * if no error is indicated and the answer count is nonzero.
1662 * Return the size of the response on success, -1 on error.
1663 * Error number is left in h_errno.
1664 *
1665 * Caller must parse answer and determine whether it answers the question.
1666 */
1667 static int
1668 res_queryN(name, target)
1669 const char *name; /* domain name */
1670 struct res_target *target;
1671 {
1672 u_char buf[MAXPACKET];
1673 HEADER *hp;
1674 int n;
1675 struct res_target *t;
1676 int rcode;
1677 int ancount;
1678
1679 _DIAGASSERT(name != NULL);
1680 /* XXX: target may be NULL??? */
1681
1682 rcode = NOERROR;
1683 ancount = 0;
1684
1685 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1686 h_errno = NETDB_INTERNAL;
1687 return (-1);
1688 }
1689
1690 for (t = target; t; t = t->next) {
1691 int class, type;
1692 u_char *answer;
1693 int anslen;
1694
1695 hp = (HEADER *)(void *)t->answer;
1696 hp->rcode = NOERROR; /* default */
1697
1698 /* make it easier... */
1699 class = t->qclass;
1700 type = t->qtype;
1701 answer = t->answer;
1702 anslen = t->anslen;
1703 #ifdef DEBUG
1704 if (_res.options & RES_DEBUG)
1705 printf(";; res_query(%s, %d, %d)\n", name, class, type);
1706 #endif
1707
1708 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
1709 buf, sizeof(buf));
1710 #ifdef RES_USE_EDNS0
1711 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
1712 n = res_opt(n, buf, sizeof(buf), anslen);
1713 #endif
1714 if (n <= 0) {
1715 #ifdef DEBUG
1716 if (_res.options & RES_DEBUG)
1717 printf(";; res_query: mkquery failed\n");
1718 #endif
1719 h_errno = NO_RECOVERY;
1720 return (n);
1721 }
1722 n = res_send(buf, n, answer, anslen);
1723 #if 0
1724 if (n < 0) {
1725 #ifdef DEBUG
1726 if (_res.options & RES_DEBUG)
1727 printf(";; res_query: send error\n");
1728 #endif
1729 h_errno = TRY_AGAIN;
1730 return (n);
1731 }
1732 #endif
1733
1734 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1735 rcode = hp->rcode; /* record most recent error */
1736 #ifdef DEBUG
1737 if (_res.options & RES_DEBUG)
1738 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
1739 ntohs(hp->ancount));
1740 #endif
1741 continue;
1742 }
1743
1744 ancount += ntohs(hp->ancount);
1745
1746 t->n = n;
1747 }
1748
1749 if (ancount == 0) {
1750 switch (rcode) {
1751 case NXDOMAIN:
1752 h_errno = HOST_NOT_FOUND;
1753 break;
1754 case SERVFAIL:
1755 h_errno = TRY_AGAIN;
1756 break;
1757 case NOERROR:
1758 h_errno = NO_DATA;
1759 break;
1760 case FORMERR:
1761 case NOTIMP:
1762 case REFUSED:
1763 default:
1764 h_errno = NO_RECOVERY;
1765 break;
1766 }
1767 return (-1);
1768 }
1769 return (ancount);
1770 }
1771
1772 /*
1773 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1774 * Return the size of the response on success, -1 on error.
1775 * If enabled, implement search rules until answer or unrecoverable failure
1776 * is detected. Error code, if any, is left in h_errno.
1777 */
1778 static int
1779 res_searchN(name, target)
1780 const char *name; /* domain name */
1781 struct res_target *target;
1782 {
1783 const char *cp, * const *domain;
1784 HEADER *hp;
1785 u_int dots;
1786 int trailing_dot, ret, saved_herrno;
1787 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1788
1789 _DIAGASSERT(name != NULL);
1790 _DIAGASSERT(target != NULL);
1791
1792 hp = (HEADER *)(void *)target->answer; /*XXX*/
1793
1794 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1795 h_errno = NETDB_INTERNAL;
1796 return (-1);
1797 }
1798
1799 errno = 0;
1800 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1801 dots = 0;
1802 for (cp = name; *cp; cp++)
1803 dots += (*cp == '.');
1804 trailing_dot = 0;
1805 if (cp > name && *--cp == '.')
1806 trailing_dot++;
1807
1808 /*
1809 * if there aren't any dots, it could be a user-level alias
1810 */
1811 if (!dots && (cp = __hostalias(name)) != NULL)
1812 return (res_queryN(cp, target));
1813
1814 /*
1815 * If there are dots in the name already, let's just give it a try
1816 * 'as is'. The threshold can be set with the "ndots" option.
1817 */
1818 saved_herrno = -1;
1819 if (dots >= _res.ndots) {
1820 ret = res_querydomainN(name, NULL, target);
1821 if (ret > 0)
1822 return (ret);
1823 saved_herrno = h_errno;
1824 tried_as_is++;
1825 }
1826
1827 /*
1828 * We do at least one level of search if
1829 * - there is no dot and RES_DEFNAME is set, or
1830 * - there is at least one dot, there is no trailing dot,
1831 * and RES_DNSRCH is set.
1832 */
1833 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1834 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1835 int done = 0;
1836
1837 for (domain = (const char * const *)_res.dnsrch;
1838 *domain && !done;
1839 domain++) {
1840
1841 ret = res_querydomainN(name, *domain, target);
1842 if (ret > 0)
1843 return (ret);
1844
1845 /*
1846 * If no server present, give up.
1847 * If name isn't found in this domain,
1848 * keep trying higher domains in the search list
1849 * (if that's enabled).
1850 * On a NO_DATA error, keep trying, otherwise
1851 * a wildcard entry of another type could keep us
1852 * from finding this entry higher in the domain.
1853 * If we get some other error (negative answer or
1854 * server failure), then stop searching up,
1855 * but try the input name below in case it's
1856 * fully-qualified.
1857 */
1858 if (errno == ECONNREFUSED) {
1859 h_errno = TRY_AGAIN;
1860 return (-1);
1861 }
1862
1863 switch (h_errno) {
1864 case NO_DATA:
1865 got_nodata++;
1866 /* FALLTHROUGH */
1867 case HOST_NOT_FOUND:
1868 /* keep trying */
1869 break;
1870 case TRY_AGAIN:
1871 if (hp->rcode == SERVFAIL) {
1872 /* try next search element, if any */
1873 got_servfail++;
1874 break;
1875 }
1876 /* FALLTHROUGH */
1877 default:
1878 /* anything else implies that we're done */
1879 done++;
1880 }
1881 /*
1882 * if we got here for some reason other than DNSRCH,
1883 * we only wanted one iteration of the loop, so stop.
1884 */
1885 if (!(_res.options & RES_DNSRCH))
1886 done++;
1887 }
1888 }
1889
1890 /*
1891 * if we have not already tried the name "as is", do that now.
1892 * note that we do this regardless of how many dots were in the
1893 * name or whether it ends with a dot.
1894 */
1895 if (!tried_as_is) {
1896 ret = res_querydomainN(name, NULL, target);
1897 if (ret > 0)
1898 return (ret);
1899 }
1900
1901 /*
1902 * if we got here, we didn't satisfy the search.
1903 * if we did an initial full query, return that query's h_errno
1904 * (note that we wouldn't be here if that query had succeeded).
1905 * else if we ever got a nodata, send that back as the reason.
1906 * else send back meaningless h_errno, that being the one from
1907 * the last DNSRCH we did.
1908 */
1909 if (saved_herrno != -1)
1910 h_errno = saved_herrno;
1911 else if (got_nodata)
1912 h_errno = NO_DATA;
1913 else if (got_servfail)
1914 h_errno = TRY_AGAIN;
1915 return (-1);
1916 }
1917
1918 /*
1919 * Perform a call on res_query on the concatenation of name and domain,
1920 * removing a trailing dot from name if domain is NULL.
1921 */
1922 static int
1923 res_querydomainN(name, domain, target)
1924 const char *name, *domain;
1925 struct res_target *target;
1926 {
1927 char nbuf[MAXDNAME];
1928 const char *longname = nbuf;
1929 size_t n, d;
1930
1931 _DIAGASSERT(name != NULL);
1932 /* XXX: target may be NULL??? */
1933
1934 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1935 h_errno = NETDB_INTERNAL;
1936 return (-1);
1937 }
1938 #ifdef DEBUG
1939 if (_res.options & RES_DEBUG)
1940 printf(";; res_querydomain(%s, %s)\n",
1941 name, domain?domain:"<Nil>");
1942 #endif
1943 if (domain == NULL) {
1944 /*
1945 * Check for trailing '.';
1946 * copy without '.' if present.
1947 */
1948 n = strlen(name);
1949 if (n >= MAXDNAME) {
1950 h_errno = NO_RECOVERY;
1951 return (-1);
1952 }
1953 if (n > 0 && name[--n] == '.') {
1954 strncpy(nbuf, name, n);
1955 nbuf[n] = '\0';
1956 } else
1957 longname = name;
1958 } else {
1959 n = strlen(name);
1960 d = strlen(domain);
1961 if (n + d + 1 >= MAXDNAME) {
1962 h_errno = NO_RECOVERY;
1963 return (-1);
1964 }
1965 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1966 }
1967 return (res_queryN(longname, target));
1968 }
1969