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