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