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