getaddrinfo.c revision 1.24 1 1.24 itojun /* $NetBSD: getaddrinfo.c,v 1.24 2000/01/26 06:51:29 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 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
34 1.1 itojun *
35 1.1 itojun * Issues to be discussed:
36 1.1 itojun * - Thread safe-ness must be checked.
37 1.1 itojun * - Return values. There are nonstandard return values defined and used
38 1.14 itojun * in the source code. This is because RFC2553 is silent about which error
39 1.1 itojun * code must be returned for which situation.
40 1.14 itojun * Note:
41 1.14 itojun * - We use getipnodebyname() just for thread-safeness. There's no intent
42 1.14 itojun * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
43 1.14 itojun * getipnodebyname().
44 1.14 itojun * - The code filters out AFs that are not supported by the kernel,
45 1.22 itojun * when globbing NULL hostname (to loopback, or wildcard). Is it the right
46 1.14 itojun * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
47 1.14 itojun * in ai_flags?
48 1.1 itojun */
49 1.1 itojun
50 1.14 itojun #include <sys/types.h>
51 1.1 itojun #include <sys/param.h>
52 1.1 itojun #include <sys/socket.h>
53 1.14 itojun #include <net/if.h>
54 1.1 itojun #include <netinet/in.h>
55 1.1 itojun #include <arpa/inet.h>
56 1.1 itojun #include <arpa/nameser.h>
57 1.1 itojun #include <netdb.h>
58 1.1 itojun #include <resolv.h>
59 1.14 itojun #include <string.h>
60 1.14 itojun #include <stdlib.h>
61 1.12 lukem #include <stddef.h>
62 1.14 itojun #include <ctype.h>
63 1.1 itojun #include <unistd.h>
64 1.14 itojun #include <stdio.h>
65 1.14 itojun #include <errno.h>
66 1.14 itojun
67 1.1 itojun #define SUCCESS 0
68 1.1 itojun #define ANY 0
69 1.1 itojun #define YES 1
70 1.1 itojun #define NO 0
71 1.1 itojun
72 1.1 itojun #ifdef FAITH
73 1.1 itojun static int translate = NO;
74 1.1 itojun static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
75 1.1 itojun #endif
76 1.1 itojun
77 1.1 itojun static const char in_addrany[] = { 0, 0, 0, 0 };
78 1.1 itojun static const char in6_addrany[] = {
79 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
80 1.1 itojun };
81 1.1 itojun static const char in_loopback[] = { 127, 0, 0, 1 };
82 1.1 itojun static const char in6_loopback[] = {
83 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
84 1.1 itojun };
85 1.1 itojun
86 1.1 itojun struct sockinet {
87 1.1 itojun u_char si_len;
88 1.1 itojun u_char si_family;
89 1.1 itojun u_short si_port;
90 1.14 itojun u_int32_t si_scope_id;
91 1.1 itojun };
92 1.1 itojun
93 1.14 itojun static const struct afd {
94 1.1 itojun int a_af;
95 1.1 itojun int a_addrlen;
96 1.1 itojun int a_socklen;
97 1.1 itojun int a_off;
98 1.1 itojun const char *a_addrany;
99 1.1 itojun const char *a_loopback;
100 1.14 itojun int a_scoped;
101 1.1 itojun } afdl [] = {
102 1.1 itojun #ifdef INET6
103 1.1 itojun {PF_INET6, sizeof(struct in6_addr),
104 1.1 itojun sizeof(struct sockaddr_in6),
105 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr),
106 1.14 itojun in6_addrany, in6_loopback, 1},
107 1.1 itojun #endif
108 1.1 itojun {PF_INET, sizeof(struct in_addr),
109 1.1 itojun sizeof(struct sockaddr_in),
110 1.1 itojun offsetof(struct sockaddr_in, sin_addr),
111 1.14 itojun in_addrany, in_loopback, 0},
112 1.14 itojun {0, 0, 0, 0, NULL, NULL, 0},
113 1.14 itojun };
114 1.14 itojun
115 1.14 itojun struct explore {
116 1.14 itojun int e_af;
117 1.14 itojun int e_socktype;
118 1.14 itojun int e_protocol;
119 1.14 itojun const char *e_protostr;
120 1.14 itojun int e_wild;
121 1.14 itojun #define WILD_AF(ex) ((ex)->e_wild & 0x01)
122 1.14 itojun #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
123 1.14 itojun #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
124 1.14 itojun };
125 1.14 itojun
126 1.14 itojun static const struct explore explore[] = {
127 1.14 itojun #if 0
128 1.14 itojun { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
129 1.14 itojun #endif
130 1.14 itojun #ifdef INET6
131 1.14 itojun { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
132 1.14 itojun { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
133 1.14 itojun { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
134 1.14 itojun #endif
135 1.14 itojun { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
136 1.14 itojun { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
137 1.14 itojun { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
138 1.14 itojun { -1, 0, 0, NULL, 0 },
139 1.1 itojun };
140 1.1 itojun
141 1.1 itojun #ifdef INET6
142 1.1 itojun #define PTON_MAX 16
143 1.1 itojun #else
144 1.1 itojun #define PTON_MAX 4
145 1.1 itojun #endif
146 1.1 itojun
147 1.1 itojun
148 1.1 itojun static int str_isnumber __P((const char *));
149 1.14 itojun static int explore_fqdn __P((const struct addrinfo *, const char *,
150 1.14 itojun const char *, struct addrinfo **));
151 1.20 mycroft static int explore_null __P((const struct addrinfo *,
152 1.14 itojun const char *, struct addrinfo **));
153 1.14 itojun static int explore_numeric __P((const struct addrinfo *, const char *,
154 1.14 itojun const char *, struct addrinfo **));
155 1.14 itojun static int explore_numeric_scope __P((const struct addrinfo *, const char *,
156 1.14 itojun const char *, struct addrinfo **));
157 1.14 itojun static int get_canonname __P((const struct addrinfo *,
158 1.14 itojun struct addrinfo *, const char *));
159 1.14 itojun static struct addrinfo *get_ai __P((const struct addrinfo *,
160 1.14 itojun const struct afd *, const char *));
161 1.14 itojun static int get_portmatch __P((const struct addrinfo *, const char *));
162 1.14 itojun static int get_port __P((struct addrinfo *, const char *, int));
163 1.14 itojun static const struct afd *find_afd __P((int));
164 1.14 itojun
165 1.1 itojun static char *ai_errlist[] = {
166 1.7 lukem "Success",
167 1.7 lukem "Address family for hostname not supported", /* EAI_ADDRFAMILY */
168 1.7 lukem "Temporary failure in name resolution", /* EAI_AGAIN */
169 1.7 lukem "Invalid value for ai_flags", /* EAI_BADFLAGS */
170 1.7 lukem "Non-recoverable failure in name resolution", /* EAI_FAIL */
171 1.2 lukem "ai_family not supported", /* EAI_FAMILY */
172 1.7 lukem "Memory allocation failure", /* EAI_MEMORY */
173 1.7 lukem "No address associated with hostname", /* EAI_NODATA */
174 1.14 itojun "hostname nor servname provided, or not known", /* EAI_NONAME */
175 1.2 lukem "servname not supported for ai_socktype", /* EAI_SERVICE */
176 1.2 lukem "ai_socktype not supported", /* EAI_SOCKTYPE */
177 1.7 lukem "System error returned in errno", /* EAI_SYSTEM */
178 1.7 lukem "Invalid value for hints", /* EAI_BADHINTS */
179 1.7 lukem "Resolved protocol is unknown", /* EAI_PROTOCOL */
180 1.7 lukem "Unknown error", /* EAI_MAX */
181 1.1 itojun };
182 1.1 itojun
183 1.14 itojun /* XXX macros that make external reference is BAD. */
184 1.14 itojun
185 1.14 itojun #define GET_AI(ai, afd, addr) \
186 1.14 itojun do { \
187 1.14 itojun /* external reference: pai, error, and label free */ \
188 1.14 itojun (ai) = get_ai(pai, (afd), (addr)); \
189 1.14 itojun if ((ai) == NULL) { \
190 1.14 itojun error = EAI_MEMORY; \
191 1.14 itojun goto free; \
192 1.14 itojun } \
193 1.20 mycroft } while (/*CONSTCOND*/0)
194 1.14 itojun
195 1.14 itojun #define GET_PORT(ai, serv) \
196 1.14 itojun do { \
197 1.14 itojun /* external reference: error and label free */ \
198 1.14 itojun error = get_port((ai), (serv), 0); \
199 1.14 itojun if (error != 0) \
200 1.14 itojun goto free; \
201 1.20 mycroft } while (/*CONSTCOND*/0)
202 1.14 itojun
203 1.1 itojun #define GET_CANONNAME(ai, str) \
204 1.14 itojun do { \
205 1.14 itojun /* external reference: pai, error and label free */ \
206 1.14 itojun error = get_canonname(pai, (ai), (str)); \
207 1.14 itojun if (error != 0) \
208 1.14 itojun goto free; \
209 1.20 mycroft } while (/*CONSTCOND*/0)
210 1.14 itojun
211 1.14 itojun #define ERR(err) \
212 1.14 itojun do { \
213 1.14 itojun /* external reference: error, and label bad */ \
214 1.14 itojun error = (err); \
215 1.14 itojun goto bad; \
216 1.20 mycroft } while (/*CONSTCOND*/0)
217 1.14 itojun
218 1.14 itojun #define MATCH_FAMILY(x, y, w) \
219 1.20 mycroft ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
220 1.14 itojun #define MATCH(x, y, w) \
221 1.20 mycroft ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
222 1.1 itojun
223 1.1 itojun char *
224 1.1 itojun gai_strerror(ecode)
225 1.1 itojun int ecode;
226 1.1 itojun {
227 1.1 itojun if (ecode < 0 || ecode > EAI_MAX)
228 1.1 itojun ecode = EAI_MAX;
229 1.1 itojun return ai_errlist[ecode];
230 1.1 itojun }
231 1.1 itojun
232 1.1 itojun void
233 1.1 itojun freeaddrinfo(ai)
234 1.1 itojun struct addrinfo *ai;
235 1.1 itojun {
236 1.1 itojun struct addrinfo *next;
237 1.1 itojun
238 1.1 itojun do {
239 1.1 itojun next = ai->ai_next;
240 1.1 itojun if (ai->ai_canonname)
241 1.1 itojun free(ai->ai_canonname);
242 1.1 itojun /* no need to free(ai->ai_addr) */
243 1.1 itojun free(ai);
244 1.1 itojun } while ((ai = next) != NULL);
245 1.1 itojun }
246 1.1 itojun
247 1.1 itojun static int
248 1.1 itojun str_isnumber(p)
249 1.1 itojun const char *p;
250 1.1 itojun {
251 1.20 mycroft const char *q = (const char *)p;
252 1.1 itojun while (*q) {
253 1.20 mycroft if (!isdigit(*q))
254 1.1 itojun return NO;
255 1.1 itojun q++;
256 1.1 itojun }
257 1.1 itojun return YES;
258 1.1 itojun }
259 1.1 itojun
260 1.1 itojun int
261 1.1 itojun getaddrinfo(hostname, servname, hints, res)
262 1.1 itojun const char *hostname, *servname;
263 1.1 itojun const struct addrinfo *hints;
264 1.1 itojun struct addrinfo **res;
265 1.1 itojun {
266 1.1 itojun struct addrinfo sentinel;
267 1.1 itojun struct addrinfo *cur;
268 1.14 itojun int error = 0;
269 1.1 itojun struct addrinfo ai;
270 1.14 itojun struct addrinfo ai0;
271 1.1 itojun struct addrinfo *pai;
272 1.14 itojun const struct afd *afd;
273 1.14 itojun const struct explore *ex;
274 1.1 itojun
275 1.1 itojun #ifdef FAITH
276 1.1 itojun static int firsttime = 1;
277 1.1 itojun
278 1.1 itojun if (firsttime) {
279 1.1 itojun /* translator hack */
280 1.14 itojun char *q = getenv("GAI");
281 1.14 itojun if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
282 1.14 itojun translate = YES;
283 1.1 itojun firsttime = 0;
284 1.1 itojun }
285 1.1 itojun #endif
286 1.1 itojun
287 1.1 itojun sentinel.ai_next = NULL;
288 1.1 itojun cur = &sentinel;
289 1.1 itojun pai = &ai;
290 1.1 itojun pai->ai_flags = 0;
291 1.1 itojun pai->ai_family = PF_UNSPEC;
292 1.1 itojun pai->ai_socktype = ANY;
293 1.1 itojun pai->ai_protocol = ANY;
294 1.1 itojun pai->ai_addrlen = 0;
295 1.1 itojun pai->ai_canonname = NULL;
296 1.1 itojun pai->ai_addr = NULL;
297 1.1 itojun pai->ai_next = NULL;
298 1.1 itojun
299 1.1 itojun if (hostname == NULL && servname == NULL)
300 1.1 itojun return EAI_NONAME;
301 1.1 itojun if (hints) {
302 1.1 itojun /* error check for hints */
303 1.1 itojun if (hints->ai_addrlen || hints->ai_canonname ||
304 1.1 itojun hints->ai_addr || hints->ai_next)
305 1.1 itojun ERR(EAI_BADHINTS); /* xxx */
306 1.1 itojun if (hints->ai_flags & ~AI_MASK)
307 1.1 itojun ERR(EAI_BADFLAGS);
308 1.1 itojun switch (hints->ai_family) {
309 1.1 itojun case PF_UNSPEC:
310 1.1 itojun case PF_INET:
311 1.1 itojun #ifdef INET6
312 1.1 itojun case PF_INET6:
313 1.1 itojun #endif
314 1.1 itojun break;
315 1.1 itojun default:
316 1.1 itojun ERR(EAI_FAMILY);
317 1.1 itojun }
318 1.1 itojun memcpy(pai, hints, sizeof(*pai));
319 1.14 itojun
320 1.14 itojun /*
321 1.14 itojun * if both socktype/protocol are specified, check if they
322 1.14 itojun * are meaningful combination.
323 1.14 itojun */
324 1.14 itojun if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
325 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
326 1.14 itojun if (pai->ai_family != ex->e_af)
327 1.14 itojun continue;
328 1.14 itojun if (ex->e_socktype == ANY)
329 1.14 itojun continue;
330 1.14 itojun if (ex->e_protocol == ANY)
331 1.14 itojun continue;
332 1.14 itojun if (pai->ai_socktype == ex->e_socktype
333 1.14 itojun && pai->ai_protocol != ex->e_protocol) {
334 1.14 itojun ERR(EAI_BADHINTS);
335 1.14 itojun }
336 1.1 itojun }
337 1.1 itojun }
338 1.1 itojun }
339 1.1 itojun
340 1.1 itojun /*
341 1.14 itojun * check for special cases. (1) numeric servname is disallowed if
342 1.14 itojun * socktype/protocol are left unspecified. (2) servname is disallowed
343 1.14 itojun * for raw and other inet{,6} sockets.
344 1.1 itojun */
345 1.14 itojun if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
346 1.14 itojun || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)) {
347 1.23 itojun ai0 = *pai; /* backup *pai */
348 1.21 mycroft
349 1.14 itojun if (pai->ai_family == PF_UNSPEC)
350 1.14 itojun pai->ai_family = PF_INET6;
351 1.14 itojun error = get_portmatch(pai, servname);
352 1.14 itojun if (error)
353 1.14 itojun ERR(error);
354 1.21 mycroft
355 1.21 mycroft *pai = ai0;
356 1.14 itojun }
357 1.14 itojun
358 1.14 itojun ai0 = *pai;
359 1.14 itojun
360 1.14 itojun /* NULL hostname, or numeric hostname */
361 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
362 1.14 itojun *pai = ai0;
363 1.14 itojun
364 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
365 1.14 itojun continue;
366 1.14 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
367 1.14 itojun continue;
368 1.14 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
369 1.14 itojun continue;
370 1.14 itojun
371 1.14 itojun if (pai->ai_family == PF_UNSPEC)
372 1.14 itojun pai->ai_family = ex->e_af;
373 1.14 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
374 1.14 itojun pai->ai_socktype = ex->e_socktype;
375 1.14 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
376 1.14 itojun pai->ai_protocol = ex->e_protocol;
377 1.14 itojun
378 1.14 itojun if (hostname == NULL)
379 1.20 mycroft error = explore_null(pai, servname, &cur->ai_next);
380 1.14 itojun else
381 1.14 itojun error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
382 1.14 itojun
383 1.14 itojun if (error)
384 1.14 itojun goto free;
385 1.14 itojun
386 1.14 itojun while (cur && cur->ai_next)
387 1.14 itojun cur = cur->ai_next;
388 1.14 itojun }
389 1.14 itojun
390 1.14 itojun /*
391 1.14 itojun * XXX
392 1.14 itojun * If numreic representation of AF1 can be interpreted as FQDN
393 1.14 itojun * representation of AF2, we need to think again about the code below.
394 1.14 itojun */
395 1.14 itojun if (sentinel.ai_next)
396 1.14 itojun goto good;
397 1.14 itojun
398 1.14 itojun if (pai->ai_flags & AI_NUMERICHOST)
399 1.14 itojun ERR(EAI_NONAME);
400 1.14 itojun if (hostname == NULL)
401 1.14 itojun ERR(EAI_NONAME);
402 1.14 itojun
403 1.14 itojun /*
404 1.14 itojun * hostname as alphabetical name.
405 1.14 itojun * we would like to prefer AF_INET6 than AF_INET, so we'll make a
406 1.14 itojun * outer loop by AFs.
407 1.14 itojun */
408 1.14 itojun for (afd = afdl; afd->a_af; afd++) {
409 1.14 itojun *pai = ai0;
410 1.14 itojun
411 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
412 1.14 itojun continue;
413 1.14 itojun
414 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
415 1.14 itojun *pai = ai0;
416 1.14 itojun
417 1.14 itojun if (pai->ai_family == PF_UNSPEC)
418 1.14 itojun pai->ai_family = afd->a_af;
419 1.1 itojun
420 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
421 1.14 itojun continue;
422 1.14 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype,
423 1.14 itojun WILD_SOCKTYPE(ex))) {
424 1.14 itojun continue;
425 1.1 itojun }
426 1.14 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol,
427 1.14 itojun WILD_PROTOCOL(ex))) {
428 1.14 itojun continue;
429 1.1 itojun }
430 1.14 itojun
431 1.14 itojun if (pai->ai_family == PF_UNSPEC)
432 1.14 itojun pai->ai_family = ex->e_af;
433 1.14 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
434 1.14 itojun pai->ai_socktype = ex->e_socktype;
435 1.14 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
436 1.14 itojun pai->ai_protocol = ex->e_protocol;
437 1.14 itojun
438 1.14 itojun error = explore_fqdn(pai, hostname, servname,
439 1.14 itojun &cur->ai_next);
440 1.14 itojun
441 1.14 itojun while (cur && cur->ai_next)
442 1.14 itojun cur = cur->ai_next;
443 1.1 itojun }
444 1.1 itojun }
445 1.14 itojun
446 1.14 itojun /* XXX */
447 1.14 itojun if (sentinel.ai_next)
448 1.14 itojun error = 0;
449 1.14 itojun
450 1.14 itojun if (error)
451 1.14 itojun goto free;
452 1.14 itojun if (error == 0) {
453 1.14 itojun if (sentinel.ai_next) {
454 1.14 itojun good:
455 1.14 itojun *res = sentinel.ai_next;
456 1.14 itojun return SUCCESS;
457 1.14 itojun } else
458 1.14 itojun error = EAI_FAIL;
459 1.14 itojun }
460 1.14 itojun free:
461 1.14 itojun bad:
462 1.14 itojun if (sentinel.ai_next)
463 1.14 itojun freeaddrinfo(sentinel.ai_next);
464 1.14 itojun *res = NULL;
465 1.14 itojun return error;
466 1.14 itojun }
467 1.14 itojun
468 1.14 itojun /*
469 1.14 itojun * FQDN hostname, DNS lookup
470 1.14 itojun */
471 1.14 itojun static int
472 1.14 itojun explore_fqdn(pai, hostname, servname, res)
473 1.14 itojun const struct addrinfo *pai;
474 1.14 itojun const char *hostname;
475 1.14 itojun const char *servname;
476 1.14 itojun struct addrinfo **res;
477 1.14 itojun {
478 1.14 itojun struct hostent *hp;
479 1.14 itojun int h_error;
480 1.14 itojun int af;
481 1.16 itojun char **aplist = NULL, *apbuf = NULL;
482 1.16 itojun char *ap;
483 1.14 itojun struct addrinfo sentinel, *cur;
484 1.14 itojun int i;
485 1.16 itojun #ifndef USE_GETIPNODEBY
486 1.16 itojun int naddrs;
487 1.16 itojun #endif
488 1.14 itojun const struct afd *afd;
489 1.14 itojun int error;
490 1.14 itojun
491 1.14 itojun *res = NULL;
492 1.14 itojun sentinel.ai_next = NULL;
493 1.14 itojun cur = &sentinel;
494 1.14 itojun
495 1.14 itojun /*
496 1.22 itojun * Do not filter unsupported AFs here. We need to honor content of
497 1.22 itojun * databases (/etc/hosts, DNS and others). Otherwise we cannot
498 1.22 itojun * replace gethostbyname() by getaddrinfo().
499 1.14 itojun */
500 1.14 itojun
501 1.14 itojun /*
502 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
503 1.14 itojun */
504 1.14 itojun if (get_portmatch(pai, servname) != 0)
505 1.14 itojun return 0;
506 1.14 itojun
507 1.14 itojun afd = find_afd(pai->ai_family);
508 1.14 itojun
509 1.1 itojun /*
510 1.14 itojun * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
511 1.14 itojun * rather than hardcoding it. we may need to add AI_ADDRCONFIG
512 1.14 itojun * handling code by ourselves in case we don't have getipnodebyname().
513 1.14 itojun */
514 1.14 itojun #ifdef USE_GETIPNODEBY
515 1.14 itojun hp = getipnodebyname(hostname, pai->ai_family, AI_ADDRCONFIG, &h_error);
516 1.14 itojun #else
517 1.14 itojun hp = gethostbyname2(hostname, pai->ai_family);
518 1.14 itojun h_error = h_errno;
519 1.14 itojun #endif
520 1.14 itojun
521 1.14 itojun if (hp == NULL) {
522 1.14 itojun switch (h_error) {
523 1.14 itojun case HOST_NOT_FOUND:
524 1.14 itojun case NO_DATA:
525 1.14 itojun error = EAI_NODATA;
526 1.14 itojun break;
527 1.14 itojun case TRY_AGAIN:
528 1.14 itojun error = EAI_AGAIN;
529 1.14 itojun break;
530 1.14 itojun case NO_RECOVERY:
531 1.14 itojun case NETDB_INTERNAL:
532 1.14 itojun default:
533 1.14 itojun error = EAI_FAIL;
534 1.14 itojun break;
535 1.14 itojun }
536 1.14 itojun } else if ((hp->h_name == NULL) || (hp->h_name[0] == 0)
537 1.14 itojun || (hp->h_addr_list[0] == NULL)) {
538 1.14 itojun #ifdef USE_GETIPNODEBY
539 1.14 itojun freehostent(hp);
540 1.14 itojun #endif
541 1.14 itojun hp = NULL;
542 1.14 itojun error = EAI_FAIL;
543 1.14 itojun }
544 1.14 itojun
545 1.14 itojun if (hp == NULL)
546 1.14 itojun goto free;
547 1.14 itojun
548 1.16 itojun #ifdef USE_GETIPNODEBY
549 1.16 itojun aplist = hp->h_addr_list;
550 1.16 itojun #else
551 1.16 itojun /*
552 1.16 itojun * hp will be overwritten if we use gethostbyname2().
553 1.16 itojun * always deep copy for simplification.
554 1.16 itojun */
555 1.16 itojun for (naddrs = 0; hp->h_addr_list[naddrs] != NULL; naddrs++)
556 1.16 itojun ;
557 1.16 itojun naddrs++;
558 1.16 itojun aplist = (char **)malloc(sizeof(aplist[0]) * naddrs);
559 1.20 mycroft apbuf = (char *)malloc((size_t)hp->h_length * naddrs);
560 1.16 itojun if (aplist == NULL || apbuf == NULL) {
561 1.15 itojun error = EAI_MEMORY;
562 1.15 itojun goto free;
563 1.15 itojun }
564 1.16 itojun memset(aplist, 0, sizeof(aplist[0]) * naddrs);
565 1.16 itojun for (i = 0; i < naddrs; i++) {
566 1.16 itojun if (hp->h_addr_list[i] == NULL) {
567 1.16 itojun aplist[i] = NULL;
568 1.16 itojun continue;
569 1.16 itojun }
570 1.16 itojun memcpy(&apbuf[i * hp->h_length], hp->h_addr_list[i],
571 1.20 mycroft (size_t)hp->h_length);
572 1.16 itojun aplist[i] = &apbuf[i * hp->h_length];
573 1.16 itojun }
574 1.16 itojun #endif
575 1.15 itojun
576 1.16 itojun for (i = 0; aplist[i] != NULL; i++) {
577 1.14 itojun af = hp->h_addrtype;
578 1.16 itojun ap = aplist[i];
579 1.14 itojun if (af == AF_INET6
580 1.14 itojun && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
581 1.14 itojun af = AF_INET;
582 1.14 itojun ap = ap + sizeof(struct in6_addr)
583 1.14 itojun - sizeof(struct in_addr);
584 1.14 itojun }
585 1.14 itojun
586 1.14 itojun if (af != pai->ai_family)
587 1.14 itojun continue;
588 1.1 itojun
589 1.24 itojun GET_AI(cur->ai_next, afd, ap);
590 1.24 itojun GET_PORT(cur->ai_next, servname);
591 1.24 itojun if ((pai->ai_flags & AI_CANONNAME) != 0) {
592 1.1 itojun /*
593 1.24 itojun * RFC2553 says that ai_canonname will be set only for
594 1.24 itojun * the first element. we do it for all the elements,
595 1.24 itojun * just for convenience.
596 1.1 itojun */
597 1.24 itojun GET_CANONNAME(cur->ai_next, hp->h_name);
598 1.14 itojun }
599 1.1 itojun
600 1.14 itojun while (cur && cur->ai_next)
601 1.1 itojun cur = cur->ai_next;
602 1.1 itojun }
603 1.14 itojun
604 1.14 itojun *res = sentinel.ai_next;
605 1.14 itojun return 0;
606 1.14 itojun
607 1.14 itojun free:
608 1.14 itojun #ifdef USE_GETIPNODEBY
609 1.14 itojun if (hp)
610 1.14 itojun freehostent(hp);
611 1.14 itojun #endif
612 1.16 itojun if (aplist)
613 1.16 itojun free(aplist);
614 1.16 itojun if (apbuf)
615 1.16 itojun free(apbuf);
616 1.14 itojun if (sentinel.ai_next)
617 1.14 itojun freeaddrinfo(sentinel.ai_next);
618 1.14 itojun return error;
619 1.14 itojun }
620 1.14 itojun
621 1.14 itojun /*
622 1.14 itojun * hostname == NULL.
623 1.14 itojun * passive socket -> anyaddr (0.0.0.0 or ::)
624 1.14 itojun * non-passive socket -> localhost (127.0.0.1 or ::1)
625 1.14 itojun */
626 1.14 itojun static int
627 1.20 mycroft explore_null(pai, servname, res)
628 1.14 itojun const struct addrinfo *pai;
629 1.14 itojun const char *servname;
630 1.14 itojun struct addrinfo **res;
631 1.14 itojun {
632 1.14 itojun int s;
633 1.14 itojun const struct afd *afd;
634 1.14 itojun struct addrinfo *cur;
635 1.14 itojun struct addrinfo sentinel;
636 1.14 itojun int error;
637 1.14 itojun
638 1.14 itojun *res = NULL;
639 1.14 itojun sentinel.ai_next = NULL;
640 1.14 itojun cur = &sentinel;
641 1.14 itojun
642 1.14 itojun /*
643 1.14 itojun * filter out AFs that are not supported by the kernel
644 1.14 itojun * XXX errno?
645 1.14 itojun */
646 1.14 itojun s = socket(pai->ai_family, SOCK_DGRAM, 0);
647 1.14 itojun if (s < 0) {
648 1.14 itojun if (errno != EMFILE)
649 1.14 itojun return 0;
650 1.14 itojun } else
651 1.14 itojun close(s);
652 1.14 itojun
653 1.14 itojun /*
654 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
655 1.14 itojun */
656 1.14 itojun if (get_portmatch(pai, servname) != 0)
657 1.14 itojun return 0;
658 1.14 itojun
659 1.14 itojun afd = find_afd(pai->ai_family);
660 1.14 itojun
661 1.14 itojun if (pai->ai_flags & AI_PASSIVE) {
662 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_addrany);
663 1.14 itojun /* xxx meaningless?
664 1.14 itojun * GET_CANONNAME(cur->ai_next, "anyaddr");
665 1.14 itojun */
666 1.14 itojun GET_PORT(cur->ai_next, servname);
667 1.14 itojun } else {
668 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_loopback);
669 1.14 itojun /* xxx meaningless?
670 1.14 itojun * GET_CANONNAME(cur->ai_next, "localhost");
671 1.14 itojun */
672 1.14 itojun GET_PORT(cur->ai_next, servname);
673 1.14 itojun }
674 1.14 itojun cur = cur->ai_next;
675 1.14 itojun
676 1.14 itojun *res = sentinel.ai_next;
677 1.14 itojun return 0;
678 1.14 itojun
679 1.14 itojun free:
680 1.14 itojun if (sentinel.ai_next)
681 1.14 itojun freeaddrinfo(sentinel.ai_next);
682 1.14 itojun return error;
683 1.14 itojun }
684 1.14 itojun
685 1.14 itojun /*
686 1.14 itojun * numeric hostname
687 1.14 itojun */
688 1.14 itojun static int
689 1.14 itojun explore_numeric(pai, hostname, servname, res)
690 1.14 itojun const struct addrinfo *pai;
691 1.14 itojun const char *hostname;
692 1.14 itojun const char *servname;
693 1.14 itojun struct addrinfo **res;
694 1.14 itojun {
695 1.14 itojun const struct afd *afd;
696 1.14 itojun struct addrinfo *cur;
697 1.14 itojun struct addrinfo sentinel;
698 1.14 itojun int error;
699 1.14 itojun char pton[PTON_MAX];
700 1.14 itojun int flags;
701 1.14 itojun
702 1.14 itojun *res = NULL;
703 1.14 itojun sentinel.ai_next = NULL;
704 1.14 itojun cur = &sentinel;
705 1.14 itojun
706 1.14 itojun /*
707 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
708 1.14 itojun */
709 1.14 itojun if (get_portmatch(pai, servname) != 0)
710 1.14 itojun return 0;
711 1.14 itojun
712 1.14 itojun afd = find_afd(pai->ai_family);
713 1.14 itojun flags = pai->ai_flags;
714 1.14 itojun
715 1.14 itojun if (inet_pton(afd->a_af, hostname, pton) == 1) {
716 1.14 itojun if (pai->ai_family == afd->a_af ||
717 1.14 itojun pai->ai_family == PF_UNSPEC /*?*/) {
718 1.24 itojun GET_AI(cur->ai_next, afd, pton);
719 1.24 itojun GET_PORT(cur->ai_next, servname);
720 1.14 itojun while (cur && cur->ai_next)
721 1.14 itojun cur = cur->ai_next;
722 1.14 itojun } else
723 1.14 itojun ERR(EAI_FAMILY); /*xxx*/
724 1.1 itojun }
725 1.1 itojun
726 1.14 itojun *res = sentinel.ai_next;
727 1.14 itojun return 0;
728 1.14 itojun
729 1.14 itojun free:
730 1.14 itojun bad:
731 1.14 itojun if (sentinel.ai_next)
732 1.14 itojun freeaddrinfo(sentinel.ai_next);
733 1.14 itojun return error;
734 1.14 itojun }
735 1.14 itojun
736 1.14 itojun /*
737 1.14 itojun * numeric hostname with scope
738 1.14 itojun */
739 1.14 itojun static int
740 1.14 itojun explore_numeric_scope(pai, hostname, servname, res)
741 1.14 itojun const struct addrinfo *pai;
742 1.14 itojun const char *hostname;
743 1.14 itojun const char *servname;
744 1.14 itojun struct addrinfo **res;
745 1.14 itojun {
746 1.14 itojun #ifndef SCOPE_DELIMITER
747 1.14 itojun return explore_numeric(pai, hostname, servname, res);
748 1.14 itojun #else
749 1.14 itojun const struct afd *afd;
750 1.14 itojun struct addrinfo *cur;
751 1.14 itojun int error;
752 1.14 itojun char *cp, *hostname2 = NULL;
753 1.14 itojun int scope;
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.14 itojun /*
771 1.14 itojun * Handle special case of <scoped_address><delimiter><scope id>
772 1.14 itojun */
773 1.14 itojun hostname2 = strdup(hostname);
774 1.14 itojun if (hostname2 == NULL)
775 1.14 itojun return EAI_MEMORY;
776 1.14 itojun /* terminate at the delimiter */
777 1.14 itojun hostname2[cp - hostname] = '\0';
778 1.14 itojun
779 1.14 itojun cp++;
780 1.14 itojun switch (pai->ai_family) {
781 1.14 itojun #ifdef INET6
782 1.14 itojun case AF_INET6:
783 1.14 itojun scope = if_nametoindex(cp);
784 1.17 itojun if (scope == 0) {
785 1.17 itojun free(hostname2);
786 1.17 itojun return (EAI_NONAME);
787 1.17 itojun }
788 1.14 itojun break;
789 1.14 itojun #endif
790 1.14 itojun }
791 1.1 itojun
792 1.14 itojun error = explore_numeric(pai, hostname2, servname, res);
793 1.1 itojun if (error == 0) {
794 1.14 itojun for (cur = *res; cur; cur = cur->ai_next) {
795 1.14 itojun if (cur->ai_family != AF_INET6)
796 1.14 itojun continue;
797 1.14 itojun sin6 = (struct sockaddr_in6 *)cur->ai_addr;
798 1.14 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
799 1.14 itojun IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
800 1.14 itojun sin6->sin6_scope_id = scope;
801 1.14 itojun }
802 1.1 itojun }
803 1.14 itojun
804 1.14 itojun free(hostname2);
805 1.14 itojun
806 1.1 itojun return error;
807 1.14 itojun #endif
808 1.1 itojun }
809 1.1 itojun
810 1.1 itojun static int
811 1.14 itojun get_canonname(pai, ai, str)
812 1.14 itojun const struct addrinfo *pai;
813 1.14 itojun struct addrinfo *ai;
814 1.14 itojun const char *str;
815 1.1 itojun {
816 1.14 itojun if ((pai->ai_flags & AI_CANONNAME) != 0) {
817 1.14 itojun ai->ai_canonname = (char *)malloc(strlen(str) + 1);
818 1.14 itojun if (ai->ai_canonname == NULL)
819 1.14 itojun return EAI_MEMORY;
820 1.14 itojun strcpy(ai->ai_canonname, str);
821 1.14 itojun }
822 1.14 itojun return 0;
823 1.14 itojun }
824 1.1 itojun
825 1.14 itojun static struct addrinfo *
826 1.14 itojun get_ai(pai, afd, addr)
827 1.14 itojun const struct addrinfo *pai;
828 1.14 itojun const struct afd *afd;
829 1.14 itojun const char *addr;
830 1.14 itojun {
831 1.14 itojun char *p;
832 1.14 itojun struct addrinfo *ai;
833 1.12 lukem
834 1.14 itojun ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
835 1.14 itojun + (afd->a_socklen));
836 1.14 itojun if (ai == NULL)
837 1.14 itojun return NULL;
838 1.14 itojun
839 1.14 itojun memcpy(ai, pai, sizeof(struct addrinfo));
840 1.14 itojun ai->ai_addr = (struct sockaddr *)(ai + 1);
841 1.20 mycroft memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
842 1.14 itojun ai->ai_addr->sa_len = afd->a_socklen;
843 1.14 itojun ai->ai_addrlen = afd->a_socklen;
844 1.14 itojun ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
845 1.14 itojun p = (char *)(ai->ai_addr);
846 1.20 mycroft memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
847 1.14 itojun return ai;
848 1.14 itojun }
849 1.1 itojun
850 1.14 itojun static int
851 1.14 itojun get_portmatch(ai, servname)
852 1.14 itojun const struct addrinfo *ai;
853 1.14 itojun const char *servname;
854 1.14 itojun {
855 1.4 itojun
856 1.14 itojun /* get_port does not touch first argument. when matchonly == 1. */
857 1.14 itojun return get_port((struct addrinfo *)ai, servname, 1);
858 1.1 itojun }
859 1.1 itojun
860 1.1 itojun static int
861 1.14 itojun get_port(ai, servname, matchonly)
862 1.14 itojun struct addrinfo *ai;
863 1.14 itojun const char *servname;
864 1.14 itojun int matchonly;
865 1.1 itojun {
866 1.14 itojun const char *proto;
867 1.14 itojun struct servent *sp;
868 1.14 itojun int port;
869 1.14 itojun int allownumeric;
870 1.12 lukem
871 1.14 itojun if (servname == NULL)
872 1.14 itojun return 0;
873 1.23 itojun switch (ai->ai_family) {
874 1.23 itojun case AF_INET:
875 1.23 itojun case AF_INET6:
876 1.23 itojun break;
877 1.23 itojun default:
878 1.14 itojun return 0;
879 1.23 itojun }
880 1.1 itojun
881 1.14 itojun switch (ai->ai_socktype) {
882 1.14 itojun case SOCK_RAW:
883 1.14 itojun return EAI_SERVICE;
884 1.14 itojun case SOCK_DGRAM:
885 1.14 itojun case SOCK_STREAM:
886 1.14 itojun allownumeric = 1;
887 1.14 itojun break;
888 1.14 itojun case ANY:
889 1.14 itojun allownumeric = 0;
890 1.14 itojun break;
891 1.14 itojun default:
892 1.14 itojun return EAI_SOCKTYPE;
893 1.1 itojun }
894 1.14 itojun
895 1.14 itojun if (str_isnumber(servname)) {
896 1.14 itojun if (!allownumeric)
897 1.14 itojun return EAI_SERVICE;
898 1.14 itojun port = htons(atoi(servname));
899 1.14 itojun if (port < 0 || port > 65535)
900 1.14 itojun return EAI_SERVICE;
901 1.14 itojun } else {
902 1.14 itojun switch (ai->ai_socktype) {
903 1.14 itojun case SOCK_DGRAM:
904 1.14 itojun proto = "udp";
905 1.1 itojun break;
906 1.14 itojun case SOCK_STREAM:
907 1.14 itojun proto = "tcp";
908 1.1 itojun break;
909 1.1 itojun default:
910 1.14 itojun proto = NULL;
911 1.1 itojun break;
912 1.1 itojun }
913 1.14 itojun
914 1.14 itojun if ((sp = getservbyname(servname, proto)) == NULL)
915 1.14 itojun return EAI_SERVICE;
916 1.14 itojun port = sp->s_port;
917 1.1 itojun }
918 1.1 itojun
919 1.14 itojun if (!matchonly) {
920 1.14 itojun switch (ai->ai_family) {
921 1.1 itojun case AF_INET:
922 1.14 itojun ((struct sockaddr_in *)ai->ai_addr)->sin_port = port;
923 1.1 itojun break;
924 1.1 itojun #ifdef INET6
925 1.14 itojun case AF_INET6:
926 1.14 itojun ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port;
927 1.1 itojun break;
928 1.1 itojun #endif
929 1.1 itojun }
930 1.14 itojun }
931 1.14 itojun
932 1.14 itojun return 0;
933 1.14 itojun }
934 1.14 itojun
935 1.14 itojun static const struct afd *
936 1.14 itojun find_afd(af)
937 1.14 itojun int af;
938 1.14 itojun {
939 1.14 itojun const struct afd *afd;
940 1.1 itojun
941 1.14 itojun if (af == PF_UNSPEC)
942 1.14 itojun return NULL;
943 1.14 itojun for (afd = afdl; afd->a_af; afd++) {
944 1.14 itojun if (afd->a_af == af)
945 1.14 itojun return afd;
946 1.1 itojun }
947 1.14 itojun return NULL;
948 1.1 itojun }
949