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