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