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