getaddrinfo.c revision 1.12 1 1.12 lukem /* $NetBSD: getaddrinfo.c,v 1.12 1999/09/16 11:45:11 lukem 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.1 itojun * in the source code. This is because RFC2133 is silent about which error
39 1.1 itojun * code must be returned for which situation.
40 1.1 itojun * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
41 1.1 itojun */
42 1.1 itojun
43 1.11 kleink #include "namespace.h"
44 1.1 itojun #include <sys/param.h>
45 1.1 itojun #include <sys/sysctl.h>
46 1.1 itojun #include <sys/socket.h>
47 1.12 lukem
48 1.1 itojun #include <netinet/in.h>
49 1.1 itojun #include <arpa/inet.h>
50 1.1 itojun #include <arpa/nameser.h>
51 1.12 lukem
52 1.12 lukem #include <assert.h>
53 1.12 lukem #include <ctype.h>
54 1.12 lukem #include <errno.h>
55 1.1 itojun #include <netdb.h>
56 1.1 itojun #include <resolv.h>
57 1.12 lukem #include <stddef.h>
58 1.12 lukem #include <stdlib.h>
59 1.1 itojun #include <string.h>
60 1.1 itojun #include <unistd.h>
61 1.1 itojun
62 1.1 itojun #if 0 /*quickhack*/
63 1.1 itojun #if defined(__KAME__) && defined(INET6)
64 1.1 itojun # define FAITH
65 1.1 itojun #endif
66 1.1 itojun #endif
67 1.1 itojun
68 1.1 itojun #define SUCCESS 0
69 1.1 itojun #define ANY 0
70 1.1 itojun #define YES 1
71 1.1 itojun #define NO 0
72 1.1 itojun
73 1.1 itojun #ifdef FAITH
74 1.1 itojun static int translate = NO;
75 1.1 itojun static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
76 1.1 itojun #endif
77 1.1 itojun
78 1.1 itojun static const char in_addrany[] = { 0, 0, 0, 0 };
79 1.1 itojun static const char in6_addrany[] = {
80 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
81 1.1 itojun };
82 1.1 itojun static const char in_loopback[] = { 127, 0, 0, 1 };
83 1.1 itojun static const char in6_loopback[] = {
84 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
85 1.1 itojun };
86 1.1 itojun
87 1.1 itojun struct sockinet {
88 1.1 itojun u_char si_len;
89 1.1 itojun u_char si_family;
90 1.1 itojun u_short si_port;
91 1.1 itojun };
92 1.1 itojun
93 1.1 itojun static 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.1 itojun } afdl [] = {
101 1.1 itojun #ifdef INET6
102 1.1 itojun #define N_INET6 0
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.1 itojun in6_addrany, in6_loopback},
107 1.1 itojun #define N_INET 1
108 1.1 itojun #else
109 1.1 itojun #define N_INET 0
110 1.1 itojun #endif
111 1.1 itojun {PF_INET, sizeof(struct in_addr),
112 1.1 itojun sizeof(struct sockaddr_in),
113 1.1 itojun offsetof(struct sockaddr_in, sin_addr),
114 1.1 itojun in_addrany, in_loopback},
115 1.1 itojun {0, 0, 0, 0, NULL, NULL},
116 1.1 itojun };
117 1.1 itojun
118 1.1 itojun #ifdef INET6
119 1.1 itojun #define PTON_MAX 16
120 1.1 itojun #else
121 1.1 itojun #define PTON_MAX 4
122 1.1 itojun #endif
123 1.1 itojun
124 1.1 itojun
125 1.1 itojun static int get_name __P((const char *, struct afd *,
126 1.1 itojun struct addrinfo **, char *, struct addrinfo *,
127 1.1 itojun int));
128 1.1 itojun static int get_addr __P((const char *, int, struct addrinfo **,
129 1.1 itojun struct addrinfo *, int));
130 1.1 itojun static int get_addr0 __P((const char *, int, struct addrinfo **,
131 1.1 itojun struct addrinfo *, int));
132 1.1 itojun static int str_isnumber __P((const char *));
133 1.1 itojun
134 1.1 itojun static char *ai_errlist[] = {
135 1.7 lukem "Success",
136 1.7 lukem "Address family for hostname not supported", /* EAI_ADDRFAMILY */
137 1.7 lukem "Temporary failure in name resolution", /* EAI_AGAIN */
138 1.7 lukem "Invalid value for ai_flags", /* EAI_BADFLAGS */
139 1.7 lukem "Non-recoverable failure in name resolution", /* EAI_FAIL */
140 1.2 lukem "ai_family not supported", /* EAI_FAMILY */
141 1.7 lukem "Memory allocation failure", /* EAI_MEMORY */
142 1.7 lukem "No address associated with hostname", /* EAI_NODATA */
143 1.2 lukem "hostname nor servname provided, or not known",/* EAI_NONAME */
144 1.2 lukem "servname not supported for ai_socktype", /* EAI_SERVICE */
145 1.2 lukem "ai_socktype not supported", /* EAI_SOCKTYPE */
146 1.7 lukem "System error returned in errno", /* EAI_SYSTEM */
147 1.7 lukem "Invalid value for hints", /* EAI_BADHINTS */
148 1.7 lukem "Resolved protocol is unknown", /* EAI_PROTOCOL */
149 1.7 lukem "Unknown error", /* EAI_MAX */
150 1.1 itojun };
151 1.1 itojun
152 1.1 itojun #define GET_CANONNAME(ai, str) \
153 1.1 itojun if (pai->ai_flags & AI_CANONNAME) {\
154 1.1 itojun if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
155 1.1 itojun strcpy((ai)->ai_canonname, (str));\
156 1.1 itojun } else {\
157 1.1 itojun error = EAI_MEMORY;\
158 1.1 itojun goto free;\
159 1.1 itojun }\
160 1.1 itojun }
161 1.1 itojun
162 1.1 itojun #define GET_AI(ai, afd, addr, port) {\
163 1.1 itojun char *p;\
164 1.1 itojun if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
165 1.1 itojun ((afd)->a_socklen)))\
166 1.1 itojun == NULL) {\
167 1.1 itojun error = EAI_MEMORY;\
168 1.1 itojun goto free;\
169 1.1 itojun }\
170 1.1 itojun memcpy(ai, pai, sizeof(struct addrinfo));\
171 1.1 itojun (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
172 1.1 itojun memset((ai)->ai_addr, 0, (afd)->a_socklen);\
173 1.1 itojun (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (afd)->a_socklen;\
174 1.1 itojun (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
175 1.1 itojun ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
176 1.1 itojun p = (char *)((ai)->ai_addr);\
177 1.1 itojun memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
178 1.1 itojun }
179 1.1 itojun
180 1.1 itojun #define ERR(err) { error = (err); goto bad; }
181 1.1 itojun
182 1.1 itojun char *
183 1.1 itojun gai_strerror(ecode)
184 1.1 itojun int ecode;
185 1.1 itojun {
186 1.1 itojun if (ecode < 0 || ecode > EAI_MAX)
187 1.1 itojun ecode = EAI_MAX;
188 1.1 itojun return ai_errlist[ecode];
189 1.1 itojun }
190 1.1 itojun
191 1.1 itojun void
192 1.1 itojun freeaddrinfo(ai)
193 1.1 itojun struct addrinfo *ai;
194 1.1 itojun {
195 1.1 itojun struct addrinfo *next;
196 1.1 itojun
197 1.12 lukem _DIAGASSERT(ai != NULL);
198 1.12 lukem #ifdef _DIAGNOSTIC
199 1.12 lukem if (ai == NULL)
200 1.12 lukem return;
201 1.12 lukem #endif
202 1.12 lukem
203 1.1 itojun do {
204 1.1 itojun next = ai->ai_next;
205 1.1 itojun if (ai->ai_canonname)
206 1.1 itojun free(ai->ai_canonname);
207 1.1 itojun /* no need to free(ai->ai_addr) */
208 1.1 itojun free(ai);
209 1.1 itojun } while ((ai = next) != NULL);
210 1.1 itojun }
211 1.1 itojun
212 1.1 itojun static int
213 1.1 itojun str_isnumber(p)
214 1.1 itojun const char *p;
215 1.1 itojun {
216 1.1 itojun char *q = (char *)p;
217 1.12 lukem
218 1.12 lukem _DIAGASSERT(p != NULL);
219 1.12 lukem
220 1.1 itojun while (*q) {
221 1.1 itojun if (! isdigit(*q))
222 1.1 itojun return NO;
223 1.1 itojun q++;
224 1.1 itojun }
225 1.1 itojun return YES;
226 1.1 itojun }
227 1.1 itojun
228 1.1 itojun int
229 1.1 itojun getaddrinfo(hostname, servname, hints, res)
230 1.1 itojun const char *hostname, *servname;
231 1.1 itojun const struct addrinfo *hints;
232 1.1 itojun struct addrinfo **res;
233 1.1 itojun {
234 1.1 itojun struct addrinfo sentinel;
235 1.1 itojun struct addrinfo *top = NULL;
236 1.1 itojun struct addrinfo *cur;
237 1.1 itojun int i, error = 0;
238 1.1 itojun char pton[PTON_MAX];
239 1.1 itojun struct addrinfo ai;
240 1.1 itojun struct addrinfo *pai;
241 1.1 itojun u_short port;
242 1.1 itojun
243 1.1 itojun #ifdef FAITH
244 1.1 itojun static int firsttime = 1;
245 1.1 itojun
246 1.1 itojun if (firsttime) {
247 1.1 itojun /* translator hack */
248 1.1 itojun {
249 1.1 itojun char *q = getenv("GAI");
250 1.1 itojun if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
251 1.1 itojun translate = YES;
252 1.1 itojun }
253 1.1 itojun firsttime = 0;
254 1.1 itojun }
255 1.1 itojun #endif
256 1.1 itojun
257 1.12 lukem /* hostname may be NULL */
258 1.12 lukem /* servname may be NULL */
259 1.12 lukem /* hints may be NULL */
260 1.12 lukem _DIAGASSERT(res != NULL);
261 1.12 lukem #ifdef _DIAGNOSTIC
262 1.12 lukem if (res == NULL) {
263 1.12 lukem errno = EFAULT;
264 1.12 lukem return EAI_SYSTEM;
265 1.12 lukem }
266 1.12 lukem #endif
267 1.12 lukem
268 1.1 itojun /* initialize file static vars */
269 1.1 itojun sentinel.ai_next = NULL;
270 1.1 itojun cur = &sentinel;
271 1.1 itojun pai = &ai;
272 1.1 itojun pai->ai_flags = 0;
273 1.1 itojun pai->ai_family = PF_UNSPEC;
274 1.1 itojun pai->ai_socktype = ANY;
275 1.1 itojun pai->ai_protocol = ANY;
276 1.1 itojun pai->ai_addrlen = 0;
277 1.1 itojun pai->ai_canonname = NULL;
278 1.1 itojun pai->ai_addr = NULL;
279 1.1 itojun pai->ai_next = NULL;
280 1.1 itojun port = ANY;
281 1.1 itojun
282 1.1 itojun if (hostname == NULL && servname == NULL)
283 1.1 itojun return EAI_NONAME;
284 1.1 itojun if (hints) {
285 1.1 itojun /* error check for hints */
286 1.1 itojun if (hints->ai_addrlen || hints->ai_canonname ||
287 1.1 itojun hints->ai_addr || hints->ai_next)
288 1.1 itojun ERR(EAI_BADHINTS); /* xxx */
289 1.1 itojun if (hints->ai_flags & ~AI_MASK)
290 1.1 itojun ERR(EAI_BADFLAGS);
291 1.1 itojun switch (hints->ai_family) {
292 1.1 itojun case PF_UNSPEC:
293 1.1 itojun case PF_INET:
294 1.1 itojun #ifdef INET6
295 1.1 itojun case PF_INET6:
296 1.1 itojun #endif
297 1.1 itojun break;
298 1.1 itojun default:
299 1.1 itojun ERR(EAI_FAMILY);
300 1.1 itojun }
301 1.1 itojun memcpy(pai, hints, sizeof(*pai));
302 1.1 itojun switch (pai->ai_socktype) {
303 1.1 itojun case ANY:
304 1.1 itojun switch (pai->ai_protocol) {
305 1.1 itojun case ANY:
306 1.1 itojun break;
307 1.1 itojun case IPPROTO_UDP:
308 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
309 1.1 itojun break;
310 1.1 itojun case IPPROTO_TCP:
311 1.1 itojun pai->ai_socktype = SOCK_STREAM;
312 1.1 itojun break;
313 1.1 itojun default:
314 1.1 itojun pai->ai_socktype = SOCK_RAW;
315 1.1 itojun break;
316 1.1 itojun }
317 1.1 itojun break;
318 1.1 itojun case SOCK_RAW:
319 1.1 itojun break;
320 1.1 itojun case SOCK_DGRAM:
321 1.1 itojun if (pai->ai_protocol != IPPROTO_UDP &&
322 1.1 itojun pai->ai_protocol != ANY)
323 1.1 itojun ERR(EAI_BADHINTS); /*xxx*/
324 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
325 1.1 itojun break;
326 1.1 itojun case SOCK_STREAM:
327 1.1 itojun if (pai->ai_protocol != IPPROTO_TCP &&
328 1.1 itojun pai->ai_protocol != ANY)
329 1.1 itojun ERR(EAI_BADHINTS); /*xxx*/
330 1.1 itojun pai->ai_protocol = IPPROTO_TCP;
331 1.1 itojun break;
332 1.1 itojun default:
333 1.1 itojun ERR(EAI_SOCKTYPE);
334 1.1 itojun break;
335 1.1 itojun }
336 1.1 itojun }
337 1.1 itojun
338 1.1 itojun /*
339 1.1 itojun * service port
340 1.1 itojun */
341 1.1 itojun if (servname) {
342 1.1 itojun if (str_isnumber(servname)) {
343 1.1 itojun if (pai->ai_socktype == ANY) {
344 1.1 itojun /* caller accept *ANY* socktype */
345 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
346 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
347 1.1 itojun }
348 1.1 itojun port = htons(atoi(servname));
349 1.1 itojun } else {
350 1.1 itojun struct servent *sp;
351 1.1 itojun char *proto;
352 1.1 itojun
353 1.1 itojun proto = NULL;
354 1.1 itojun switch (pai->ai_socktype) {
355 1.1 itojun case ANY:
356 1.1 itojun proto = NULL;
357 1.1 itojun break;
358 1.1 itojun case SOCK_DGRAM:
359 1.1 itojun proto = "udp";
360 1.1 itojun break;
361 1.1 itojun case SOCK_STREAM:
362 1.1 itojun proto = "tcp";
363 1.1 itojun break;
364 1.1 itojun default:
365 1.1 itojun fprintf(stderr, "panic!\n");
366 1.1 itojun break;
367 1.1 itojun }
368 1.1 itojun if ((sp = getservbyname(servname, proto)) == NULL)
369 1.1 itojun ERR(EAI_SERVICE);
370 1.1 itojun port = sp->s_port;
371 1.1 itojun if (pai->ai_socktype == ANY) {
372 1.1 itojun if (strcmp(sp->s_proto, "udp") == 0) {
373 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
374 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
375 1.1 itojun } else if (strcmp(sp->s_proto, "tcp") == 0) {
376 1.1 itojun pai->ai_socktype = SOCK_STREAM;
377 1.1 itojun pai->ai_protocol = IPPROTO_TCP;
378 1.1 itojun } else
379 1.1 itojun ERR(EAI_PROTOCOL); /*xxx*/
380 1.1 itojun }
381 1.1 itojun }
382 1.1 itojun }
383 1.1 itojun
384 1.1 itojun /*
385 1.1 itojun * hostname == NULL.
386 1.1 itojun * passive socket -> anyaddr (0.0.0.0 or ::)
387 1.1 itojun * non-passive socket -> localhost (127.0.0.1 or ::1)
388 1.1 itojun */
389 1.1 itojun if (hostname == NULL) {
390 1.1 itojun struct afd *afd;
391 1.1 itojun int s;
392 1.1 itojun
393 1.1 itojun for (afd = &afdl[0]; afd->a_af; afd++) {
394 1.1 itojun if (!(pai->ai_family == PF_UNSPEC
395 1.1 itojun || pai->ai_family == afd->a_af)) {
396 1.1 itojun continue;
397 1.1 itojun }
398 1.1 itojun
399 1.1 itojun /*
400 1.1 itojun * filter out AFs that are not supported by the kernel
401 1.1 itojun * XXX errno?
402 1.1 itojun */
403 1.1 itojun s = socket(afd->a_af, SOCK_DGRAM, 0);
404 1.1 itojun if (s < 0)
405 1.1 itojun continue;
406 1.1 itojun close(s);
407 1.1 itojun
408 1.1 itojun if (pai->ai_flags & AI_PASSIVE) {
409 1.1 itojun GET_AI(cur->ai_next, afd, afd->a_addrany, port);
410 1.1 itojun /* xxx meaningless?
411 1.1 itojun * GET_CANONNAME(cur->ai_next, "anyaddr");
412 1.1 itojun */
413 1.1 itojun } else {
414 1.1 itojun GET_AI(cur->ai_next, afd, afd->a_loopback,
415 1.1 itojun port);
416 1.1 itojun /* xxx meaningless?
417 1.1 itojun * GET_CANONNAME(cur->ai_next, "localhost");
418 1.1 itojun */
419 1.1 itojun }
420 1.1 itojun cur = cur->ai_next;
421 1.1 itojun }
422 1.1 itojun top = sentinel.ai_next;
423 1.1 itojun if (top)
424 1.1 itojun goto good;
425 1.1 itojun else
426 1.1 itojun ERR(EAI_FAMILY);
427 1.1 itojun }
428 1.1 itojun
429 1.1 itojun /* hostname as numeric name */
430 1.1 itojun for (i = 0; afdl[i].a_af; i++) {
431 1.1 itojun if (inet_pton(afdl[i].a_af, hostname, pton)) {
432 1.1 itojun u_long v4a;
433 1.1 itojun u_char pfx;
434 1.1 itojun
435 1.1 itojun switch (afdl[i].a_af) {
436 1.1 itojun case AF_INET:
437 1.1 itojun v4a = ntohl(((struct in_addr *)pton)->s_addr);
438 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
439 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
440 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
441 1.1 itojun if (v4a == 0 || v4a == IN_LOOPBACKNET)
442 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
443 1.1 itojun break;
444 1.1 itojun #ifdef INET6
445 1.1 itojun case AF_INET6:
446 1.1 itojun pfx = ((struct in6_addr *)pton)->s6_addr8[0];
447 1.1 itojun if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
448 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
449 1.1 itojun break;
450 1.1 itojun #endif
451 1.1 itojun }
452 1.1 itojun
453 1.1 itojun if (pai->ai_family == afdl[i].a_af ||
454 1.1 itojun pai->ai_family == PF_UNSPEC) {
455 1.1 itojun if (! (pai->ai_flags & AI_CANONNAME)) {
456 1.1 itojun GET_AI(top, &afdl[i], pton, port);
457 1.1 itojun goto good;
458 1.1 itojun }
459 1.1 itojun /*
460 1.1 itojun * if AI_CANONNAME and if reverse lookup
461 1.1 itojun * fail, return ai anyway to pacify
462 1.1 itojun * calling application.
463 1.1 itojun *
464 1.1 itojun * XXX getaddrinfo() is a name->address
465 1.1 itojun * translation function, and it looks strange
466 1.1 itojun * that we do addr->name translation here.
467 1.1 itojun */
468 1.1 itojun get_name(pton, &afdl[i], &top, pton, pai, port);
469 1.1 itojun goto good;
470 1.1 itojun } else
471 1.1 itojun ERR(EAI_FAMILY); /*xxx*/
472 1.1 itojun }
473 1.1 itojun }
474 1.1 itojun
475 1.1 itojun if (pai->ai_flags & AI_NUMERICHOST)
476 1.1 itojun ERR(EAI_NONAME);
477 1.1 itojun
478 1.1 itojun /* hostname as alphabetical name */
479 1.1 itojun error = get_addr(hostname, pai->ai_family, &top, pai, port);
480 1.1 itojun if (error == 0) {
481 1.1 itojun if (top) {
482 1.1 itojun good:
483 1.1 itojun *res = top;
484 1.1 itojun return SUCCESS;
485 1.1 itojun } else
486 1.1 itojun error = EAI_FAIL;
487 1.1 itojun }
488 1.1 itojun free:
489 1.1 itojun if (top)
490 1.1 itojun freeaddrinfo(top);
491 1.1 itojun bad:
492 1.1 itojun *res = NULL;
493 1.1 itojun return error;
494 1.1 itojun }
495 1.1 itojun
496 1.1 itojun static int
497 1.1 itojun get_name(addr, afd, res, numaddr, pai, port0)
498 1.1 itojun const char *addr;
499 1.1 itojun struct afd *afd;
500 1.1 itojun struct addrinfo **res;
501 1.1 itojun char *numaddr;
502 1.1 itojun struct addrinfo *pai;
503 1.1 itojun int port0;
504 1.1 itojun {
505 1.1 itojun u_short port = port0 & 0xffff;
506 1.1 itojun struct hostent *hp;
507 1.1 itojun struct addrinfo *cur;
508 1.1 itojun int error = 0;
509 1.1 itojun #ifdef USE_GETIPNODEBY
510 1.1 itojun int h_error;
511 1.12 lukem #endif
512 1.1 itojun
513 1.12 lukem _DIAGASSERT(addr != NULL);
514 1.12 lukem _DIAGASSERT(afd != NULL);
515 1.12 lukem _DIAGASSERT(res != NULL);
516 1.12 lukem _DIAGASSERT(numaddr != NULL);
517 1.12 lukem _DIAGASSERT(pai != NULL);
518 1.12 lukem
519 1.12 lukem #ifdef USE_GETIPNODEBY
520 1.1 itojun hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
521 1.1 itojun #else
522 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
523 1.1 itojun #endif
524 1.1 itojun if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
525 1.1 itojun GET_AI(cur, afd, hp->h_addr_list[0], port);
526 1.1 itojun GET_CANONNAME(cur, hp->h_name);
527 1.1 itojun } else
528 1.1 itojun GET_AI(cur, afd, numaddr, port);
529 1.1 itojun
530 1.1 itojun #ifdef USE_GETIPNODEBY
531 1.1 itojun if (hp)
532 1.1 itojun freehostent(hp);
533 1.1 itojun #endif
534 1.1 itojun *res = cur;
535 1.1 itojun return SUCCESS;
536 1.1 itojun free:
537 1.1 itojun if (cur)
538 1.1 itojun freeaddrinfo(cur);
539 1.1 itojun #ifdef USE_GETIPNODEBY
540 1.1 itojun if (hp)
541 1.1 itojun freehostent(hp);
542 1.1 itojun #endif
543 1.1 itojun /* bad: */
544 1.1 itojun *res = NULL;
545 1.1 itojun return error;
546 1.1 itojun }
547 1.1 itojun
548 1.1 itojun static int
549 1.4 itojun get_addr(hostname, af, res0, pai, port0)
550 1.1 itojun const char *hostname;
551 1.1 itojun int af;
552 1.4 itojun struct addrinfo **res0;
553 1.1 itojun struct addrinfo *pai;
554 1.1 itojun int port0;
555 1.1 itojun {
556 1.12 lukem #ifndef USE_GETIPNODEBY
557 1.4 itojun int i, error, ekeep;
558 1.1 itojun struct addrinfo *cur;
559 1.4 itojun struct addrinfo **res;
560 1.9 itojun int retry;
561 1.9 itojun int s;
562 1.12 lukem #endif
563 1.1 itojun
564 1.12 lukem _DIAGASSERT(hostname != NULL);
565 1.12 lukem _DIAGASSERT(res != NULL);
566 1.12 lukem _DIAGASSERT(pai != NULL);
567 1.12 lukem
568 1.12 lukem #ifdef USE_GETIPNODEBY
569 1.12 lukem return get_addr0(hostname, af, res0, pai, port0);
570 1.12 lukem #else
571 1.4 itojun res = res0;
572 1.4 itojun ekeep = 0;
573 1.10 itojun error = 0;
574 1.1 itojun for (i = 0; afdl[i].a_af; i++) {
575 1.9 itojun retry = 0;
576 1.9 itojun if (af == AF_UNSPEC) {
577 1.9 itojun /*
578 1.9 itojun * filter out AFs that are not supported by the kernel
579 1.9 itojun * XXX errno?
580 1.9 itojun */
581 1.9 itojun s = socket(afdl[i].a_af, SOCK_DGRAM, 0);
582 1.9 itojun if (s < 0)
583 1.9 itojun continue;
584 1.9 itojun close(s);
585 1.9 itojun } else {
586 1.9 itojun if (af != afdl[i].a_af)
587 1.9 itojun continue;
588 1.9 itojun }
589 1.4 itojun /* It is WRONG, we need getipnodebyname(). */
590 1.4 itojun again:
591 1.1 itojun error = get_addr0(hostname, afdl[i].a_af, res, pai, port0);
592 1.4 itojun switch (error) {
593 1.4 itojun case EAI_AGAIN:
594 1.9 itojun if (++retry < 3)
595 1.9 itojun goto again;
596 1.9 itojun /* FALL THROUGH*/
597 1.4 itojun default:
598 1.4 itojun if (ekeep == 0)
599 1.4 itojun ekeep = error;
600 1.4 itojun break;
601 1.4 itojun }
602 1.1 itojun if (*res) {
603 1.1 itojun /* make chain of addrs */
604 1.1 itojun for (cur = *res;
605 1.1 itojun cur && cur->ai_next;
606 1.1 itojun cur = cur->ai_next)
607 1.1 itojun ;
608 1.1 itojun if (!cur)
609 1.1 itojun return EAI_FAIL;
610 1.1 itojun res = &cur->ai_next;
611 1.1 itojun }
612 1.1 itojun }
613 1.1 itojun
614 1.4 itojun /* if we got something, it's okay */
615 1.4 itojun if (*res0)
616 1.4 itojun return 0;
617 1.4 itojun
618 1.4 itojun return error ? error : ekeep;
619 1.1 itojun #endif
620 1.1 itojun }
621 1.1 itojun
622 1.1 itojun static int
623 1.1 itojun get_addr0(hostname, af, res, pai, port0)
624 1.1 itojun const char *hostname;
625 1.1 itojun int af;
626 1.1 itojun struct addrinfo **res;
627 1.1 itojun struct addrinfo *pai;
628 1.1 itojun int port0;
629 1.1 itojun {
630 1.1 itojun u_short port = port0 & 0xffff;
631 1.1 itojun struct addrinfo sentinel;
632 1.1 itojun struct hostent *hp;
633 1.1 itojun struct addrinfo *top, *cur;
634 1.1 itojun struct afd *afd;
635 1.1 itojun int i, error = 0, h_error;
636 1.1 itojun char *ap;
637 1.1 itojun #ifndef USE_GETIPNODEBY
638 1.1 itojun extern int h_errno;
639 1.1 itojun #endif
640 1.12 lukem
641 1.12 lukem _DIAGASSERT(hostname != NULL);
642 1.12 lukem _DIAGASSERT(res != NULL);
643 1.12 lukem _DIAGASSERT(pai != NULL);
644 1.1 itojun
645 1.1 itojun top = NULL;
646 1.1 itojun sentinel.ai_next = NULL;
647 1.1 itojun cur = &sentinel;
648 1.1 itojun #ifdef USE_GETIPNODEBY
649 1.1 itojun if (af == AF_UNSPEC) {
650 1.1 itojun hp = getipnodebyname(hostname, AF_INET6,
651 1.1 itojun AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
652 1.1 itojun } else
653 1.1 itojun hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
654 1.1 itojun #else
655 1.1 itojun if (af == AF_UNSPEC) {
656 1.1 itojun error = EAI_FAIL;
657 1.1 itojun goto bad;
658 1.1 itojun }
659 1.1 itojun hp = gethostbyname2(hostname, af);
660 1.1 itojun h_error = h_errno;
661 1.1 itojun #endif
662 1.1 itojun if (hp == NULL) {
663 1.1 itojun switch (h_error) {
664 1.1 itojun case HOST_NOT_FOUND:
665 1.1 itojun case NO_DATA:
666 1.1 itojun error = EAI_NODATA;
667 1.1 itojun break;
668 1.1 itojun case TRY_AGAIN:
669 1.1 itojun error = EAI_AGAIN;
670 1.1 itojun break;
671 1.1 itojun case NO_RECOVERY:
672 1.9 itojun case NETDB_INTERNAL:
673 1.1 itojun default:
674 1.1 itojun error = EAI_FAIL;
675 1.1 itojun break;
676 1.1 itojun }
677 1.1 itojun goto bad;
678 1.1 itojun }
679 1.1 itojun
680 1.1 itojun if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
681 1.1 itojun (hp->h_addr_list[0] == NULL))
682 1.1 itojun ERR(EAI_FAIL);
683 1.1 itojun
684 1.1 itojun for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
685 1.1 itojun switch (af) {
686 1.1 itojun #ifdef INET6
687 1.1 itojun case AF_INET6:
688 1.1 itojun afd = &afdl[N_INET6];
689 1.1 itojun break;
690 1.1 itojun #endif
691 1.1 itojun #ifndef INET6
692 1.1 itojun default: /* AF_UNSPEC */
693 1.1 itojun #endif
694 1.1 itojun case AF_INET:
695 1.1 itojun afd = &afdl[N_INET];
696 1.1 itojun break;
697 1.1 itojun #ifdef INET6
698 1.1 itojun default: /* AF_UNSPEC */
699 1.1 itojun if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
700 1.1 itojun ap += sizeof(struct in6_addr) -
701 1.1 itojun sizeof(struct in_addr);
702 1.1 itojun afd = &afdl[N_INET];
703 1.1 itojun } else
704 1.1 itojun afd = &afdl[N_INET6];
705 1.1 itojun break;
706 1.1 itojun #endif
707 1.1 itojun }
708 1.1 itojun #ifdef FAITH
709 1.1 itojun if (translate && afd->a_af == AF_INET) {
710 1.1 itojun struct in6_addr *in6;
711 1.1 itojun
712 1.1 itojun GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
713 1.1 itojun in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
714 1.1 itojun memcpy(&in6->s6_addr32[0], &faith_prefix,
715 1.1 itojun sizeof(struct in6_addr) - sizeof(struct in_addr));
716 1.1 itojun memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
717 1.1 itojun } else
718 1.1 itojun #endif /* FAITH */
719 1.1 itojun GET_AI(cur->ai_next, afd, ap, port);
720 1.1 itojun if (cur == &sentinel) {
721 1.1 itojun top = cur->ai_next;
722 1.1 itojun GET_CANONNAME(top, hp->h_name);
723 1.1 itojun }
724 1.1 itojun cur = cur->ai_next;
725 1.1 itojun }
726 1.1 itojun #ifdef USE_GETIPNODEBY
727 1.1 itojun freehostent(hp);
728 1.1 itojun #endif
729 1.1 itojun *res = top;
730 1.1 itojun return SUCCESS;
731 1.1 itojun free:
732 1.1 itojun if (top)
733 1.1 itojun freeaddrinfo(top);
734 1.1 itojun #ifdef USE_GETIPNODEBY
735 1.1 itojun if (hp)
736 1.1 itojun freehostent(hp);
737 1.1 itojun #endif
738 1.1 itojun bad:
739 1.1 itojun *res = NULL;
740 1.1 itojun return error;
741 1.1 itojun }
742