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