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