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