getaddrinfo.c revision 1.2 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun /*
31 1.1 itojun * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
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.1 itojun * in the source code. This is because RFC2133 is silent about which error
37 1.1 itojun * code must be returned for which situation.
38 1.1 itojun * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
39 1.1 itojun */
40 1.1 itojun
41 1.1 itojun #include <sys/types.h>
42 1.1 itojun #include <sys/param.h>
43 1.1 itojun #include <sys/sysctl.h>
44 1.1 itojun #include <sys/socket.h>
45 1.1 itojun #include <netinet/in.h>
46 1.1 itojun #include <arpa/inet.h>
47 1.1 itojun #include <arpa/nameser.h>
48 1.1 itojun #include <netdb.h>
49 1.1 itojun #include <resolv.h>
50 1.1 itojun #include <string.h>
51 1.1 itojun #include <stdlib.h>
52 1.1 itojun #include <stddef.h>
53 1.1 itojun #include <ctype.h>
54 1.1 itojun #include <unistd.h>
55 1.1 itojun
56 1.1 itojun #if 0 /*quickhack*/
57 1.1 itojun #if defined(__KAME__) && defined(INET6)
58 1.1 itojun # define FAITH
59 1.1 itojun #endif
60 1.1 itojun #endif
61 1.1 itojun
62 1.1 itojun #define SUCCESS 0
63 1.1 itojun #define ANY 0
64 1.1 itojun #define YES 1
65 1.1 itojun #define NO 0
66 1.1 itojun
67 1.1 itojun #ifdef FAITH
68 1.1 itojun static int translate = NO;
69 1.1 itojun static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
70 1.1 itojun #endif
71 1.1 itojun
72 1.1 itojun static const char in_addrany[] = { 0, 0, 0, 0 };
73 1.1 itojun static const char in6_addrany[] = {
74 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
75 1.1 itojun };
76 1.1 itojun static const char in_loopback[] = { 127, 0, 0, 1 };
77 1.1 itojun static const char in6_loopback[] = {
78 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
79 1.1 itojun };
80 1.1 itojun
81 1.1 itojun struct sockinet {
82 1.1 itojun u_char si_len;
83 1.1 itojun u_char si_family;
84 1.1 itojun u_short si_port;
85 1.1 itojun };
86 1.1 itojun
87 1.1 itojun static struct afd {
88 1.1 itojun int a_af;
89 1.1 itojun int a_addrlen;
90 1.1 itojun int a_socklen;
91 1.1 itojun int a_off;
92 1.1 itojun const char *a_addrany;
93 1.1 itojun const char *a_loopback;
94 1.1 itojun } afdl [] = {
95 1.1 itojun #ifdef INET6
96 1.1 itojun #define N_INET6 0
97 1.1 itojun {PF_INET6, sizeof(struct in6_addr),
98 1.1 itojun sizeof(struct sockaddr_in6),
99 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr),
100 1.1 itojun in6_addrany, in6_loopback},
101 1.1 itojun #define N_INET 1
102 1.1 itojun #else
103 1.1 itojun #define N_INET 0
104 1.1 itojun #endif
105 1.1 itojun {PF_INET, sizeof(struct in_addr),
106 1.1 itojun sizeof(struct sockaddr_in),
107 1.1 itojun offsetof(struct sockaddr_in, sin_addr),
108 1.1 itojun in_addrany, in_loopback},
109 1.1 itojun {0, 0, 0, 0, NULL, NULL},
110 1.1 itojun };
111 1.1 itojun
112 1.1 itojun #ifdef INET6
113 1.1 itojun #define PTON_MAX 16
114 1.1 itojun #else
115 1.1 itojun #define PTON_MAX 4
116 1.1 itojun #endif
117 1.1 itojun
118 1.1 itojun
119 1.1 itojun static int get_name __P((const char *, struct afd *,
120 1.1 itojun struct addrinfo **, char *, struct addrinfo *,
121 1.1 itojun int));
122 1.1 itojun static int get_addr __P((const char *, int, struct addrinfo **,
123 1.1 itojun struct addrinfo *, int));
124 1.1 itojun static int get_addr0 __P((const char *, int, struct addrinfo **,
125 1.1 itojun struct addrinfo *, int));
126 1.1 itojun static int str_isnumber __P((const char *));
127 1.1 itojun
128 1.1 itojun static char *ai_errlist[] = {
129 1.2 lukem "success",
130 1.2 lukem "address family for hostname not supported", /* EAI_ADDRFAMILY */
131 1.2 lukem "temporary failure in name resolution", /* EAI_AGAIN */
132 1.2 lukem "invalid value for ai_flags", /* EAI_BADFLAGS */
133 1.2 lukem "non-recoverable failure in name resolution", /* EAI_FAIL */
134 1.2 lukem "ai_family not supported", /* EAI_FAMILY */
135 1.2 lukem "memory allocation failure", /* EAI_MEMORY */
136 1.2 lukem "no address associated with hostname", /* EAI_NODATA */
137 1.2 lukem "hostname nor servname provided, or not known",/* EAI_NONAME */
138 1.2 lukem "servname not supported for ai_socktype", /* EAI_SERVICE */
139 1.2 lukem "ai_socktype not supported", /* EAI_SOCKTYPE */
140 1.2 lukem "system error returned in errno", /* EAI_SYSTEM */
141 1.2 lukem "invalid value for hints", /* EAI_BADHINTS */
142 1.2 lukem "resolved protocol is unknown", /* EAI_PROTOCOL */
143 1.2 lukem "unknown error", /* EAI_MAX */
144 1.1 itojun };
145 1.1 itojun
146 1.1 itojun #define GET_CANONNAME(ai, str) \
147 1.1 itojun if (pai->ai_flags & AI_CANONNAME) {\
148 1.1 itojun if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
149 1.1 itojun strcpy((ai)->ai_canonname, (str));\
150 1.1 itojun } else {\
151 1.1 itojun error = EAI_MEMORY;\
152 1.1 itojun goto free;\
153 1.1 itojun }\
154 1.1 itojun }
155 1.1 itojun
156 1.1 itojun #define GET_AI(ai, afd, addr, port) {\
157 1.1 itojun char *p;\
158 1.1 itojun if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
159 1.1 itojun ((afd)->a_socklen)))\
160 1.1 itojun == NULL) {\
161 1.1 itojun error = EAI_MEMORY;\
162 1.1 itojun goto free;\
163 1.1 itojun }\
164 1.1 itojun memcpy(ai, pai, sizeof(struct addrinfo));\
165 1.1 itojun (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
166 1.1 itojun memset((ai)->ai_addr, 0, (afd)->a_socklen);\
167 1.1 itojun (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (afd)->a_socklen;\
168 1.1 itojun (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
169 1.1 itojun ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
170 1.1 itojun p = (char *)((ai)->ai_addr);\
171 1.1 itojun memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
172 1.1 itojun }
173 1.1 itojun
174 1.1 itojun #define ERR(err) { error = (err); goto bad; }
175 1.1 itojun
176 1.1 itojun char *
177 1.1 itojun gai_strerror(ecode)
178 1.1 itojun int ecode;
179 1.1 itojun {
180 1.1 itojun if (ecode < 0 || ecode > EAI_MAX)
181 1.1 itojun ecode = EAI_MAX;
182 1.1 itojun return ai_errlist[ecode];
183 1.1 itojun }
184 1.1 itojun
185 1.1 itojun void
186 1.1 itojun freeaddrinfo(ai)
187 1.1 itojun struct addrinfo *ai;
188 1.1 itojun {
189 1.1 itojun struct addrinfo *next;
190 1.1 itojun
191 1.1 itojun do {
192 1.1 itojun next = ai->ai_next;
193 1.1 itojun if (ai->ai_canonname)
194 1.1 itojun free(ai->ai_canonname);
195 1.1 itojun /* no need to free(ai->ai_addr) */
196 1.1 itojun free(ai);
197 1.1 itojun } while ((ai = next) != NULL);
198 1.1 itojun }
199 1.1 itojun
200 1.1 itojun static int
201 1.1 itojun str_isnumber(p)
202 1.1 itojun const char *p;
203 1.1 itojun {
204 1.1 itojun char *q = (char *)p;
205 1.1 itojun while (*q) {
206 1.1 itojun if (! isdigit(*q))
207 1.1 itojun return NO;
208 1.1 itojun q++;
209 1.1 itojun }
210 1.1 itojun return YES;
211 1.1 itojun }
212 1.1 itojun
213 1.1 itojun int
214 1.1 itojun getaddrinfo(hostname, servname, hints, res)
215 1.1 itojun const char *hostname, *servname;
216 1.1 itojun const struct addrinfo *hints;
217 1.1 itojun struct addrinfo **res;
218 1.1 itojun {
219 1.1 itojun struct addrinfo sentinel;
220 1.1 itojun struct addrinfo *top = NULL;
221 1.1 itojun struct addrinfo *cur;
222 1.1 itojun int i, error = 0;
223 1.1 itojun char pton[PTON_MAX];
224 1.1 itojun struct addrinfo ai;
225 1.1 itojun struct addrinfo *pai;
226 1.1 itojun u_short port;
227 1.1 itojun
228 1.1 itojun #ifdef FAITH
229 1.1 itojun static int firsttime = 1;
230 1.1 itojun
231 1.1 itojun if (firsttime) {
232 1.1 itojun /* translator hack */
233 1.1 itojun {
234 1.1 itojun char *q = getenv("GAI");
235 1.1 itojun if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
236 1.1 itojun translate = YES;
237 1.1 itojun }
238 1.1 itojun firsttime = 0;
239 1.1 itojun }
240 1.1 itojun #endif
241 1.1 itojun
242 1.1 itojun /* initialize file static vars */
243 1.1 itojun sentinel.ai_next = NULL;
244 1.1 itojun cur = &sentinel;
245 1.1 itojun pai = &ai;
246 1.1 itojun pai->ai_flags = 0;
247 1.1 itojun pai->ai_family = PF_UNSPEC;
248 1.1 itojun pai->ai_socktype = ANY;
249 1.1 itojun pai->ai_protocol = ANY;
250 1.1 itojun pai->ai_addrlen = 0;
251 1.1 itojun pai->ai_canonname = NULL;
252 1.1 itojun pai->ai_addr = NULL;
253 1.1 itojun pai->ai_next = NULL;
254 1.1 itojun port = ANY;
255 1.1 itojun
256 1.1 itojun if (hostname == NULL && servname == NULL)
257 1.1 itojun return EAI_NONAME;
258 1.1 itojun if (hints) {
259 1.1 itojun /* error check for hints */
260 1.1 itojun if (hints->ai_addrlen || hints->ai_canonname ||
261 1.1 itojun hints->ai_addr || hints->ai_next)
262 1.1 itojun ERR(EAI_BADHINTS); /* xxx */
263 1.1 itojun if (hints->ai_flags & ~AI_MASK)
264 1.1 itojun ERR(EAI_BADFLAGS);
265 1.1 itojun switch (hints->ai_family) {
266 1.1 itojun case PF_UNSPEC:
267 1.1 itojun case PF_INET:
268 1.1 itojun #ifdef INET6
269 1.1 itojun case PF_INET6:
270 1.1 itojun #endif
271 1.1 itojun break;
272 1.1 itojun default:
273 1.1 itojun ERR(EAI_FAMILY);
274 1.1 itojun }
275 1.1 itojun memcpy(pai, hints, sizeof(*pai));
276 1.1 itojun switch (pai->ai_socktype) {
277 1.1 itojun case ANY:
278 1.1 itojun switch (pai->ai_protocol) {
279 1.1 itojun case ANY:
280 1.1 itojun break;
281 1.1 itojun case IPPROTO_UDP:
282 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
283 1.1 itojun break;
284 1.1 itojun case IPPROTO_TCP:
285 1.1 itojun pai->ai_socktype = SOCK_STREAM;
286 1.1 itojun break;
287 1.1 itojun default:
288 1.1 itojun pai->ai_socktype = SOCK_RAW;
289 1.1 itojun break;
290 1.1 itojun }
291 1.1 itojun break;
292 1.1 itojun case SOCK_RAW:
293 1.1 itojun break;
294 1.1 itojun case SOCK_DGRAM:
295 1.1 itojun if (pai->ai_protocol != IPPROTO_UDP &&
296 1.1 itojun pai->ai_protocol != ANY)
297 1.1 itojun ERR(EAI_BADHINTS); /*xxx*/
298 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
299 1.1 itojun break;
300 1.1 itojun case SOCK_STREAM:
301 1.1 itojun if (pai->ai_protocol != IPPROTO_TCP &&
302 1.1 itojun pai->ai_protocol != ANY)
303 1.1 itojun ERR(EAI_BADHINTS); /*xxx*/
304 1.1 itojun pai->ai_protocol = IPPROTO_TCP;
305 1.1 itojun break;
306 1.1 itojun default:
307 1.1 itojun ERR(EAI_SOCKTYPE);
308 1.1 itojun break;
309 1.1 itojun }
310 1.1 itojun }
311 1.1 itojun
312 1.1 itojun /*
313 1.1 itojun * service port
314 1.1 itojun */
315 1.1 itojun if (servname) {
316 1.1 itojun if (str_isnumber(servname)) {
317 1.1 itojun if (pai->ai_socktype == ANY) {
318 1.1 itojun /* caller accept *ANY* socktype */
319 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
320 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
321 1.1 itojun }
322 1.1 itojun port = htons(atoi(servname));
323 1.1 itojun } else {
324 1.1 itojun struct servent *sp;
325 1.1 itojun char *proto;
326 1.1 itojun
327 1.1 itojun proto = NULL;
328 1.1 itojun switch (pai->ai_socktype) {
329 1.1 itojun case ANY:
330 1.1 itojun proto = NULL;
331 1.1 itojun break;
332 1.1 itojun case SOCK_DGRAM:
333 1.1 itojun proto = "udp";
334 1.1 itojun break;
335 1.1 itojun case SOCK_STREAM:
336 1.1 itojun proto = "tcp";
337 1.1 itojun break;
338 1.1 itojun default:
339 1.1 itojun fprintf(stderr, "panic!\n");
340 1.1 itojun break;
341 1.1 itojun }
342 1.1 itojun if ((sp = getservbyname(servname, proto)) == NULL)
343 1.1 itojun ERR(EAI_SERVICE);
344 1.1 itojun port = sp->s_port;
345 1.1 itojun if (pai->ai_socktype == ANY) {
346 1.1 itojun if (strcmp(sp->s_proto, "udp") == 0) {
347 1.1 itojun pai->ai_socktype = SOCK_DGRAM;
348 1.1 itojun pai->ai_protocol = IPPROTO_UDP;
349 1.1 itojun } else if (strcmp(sp->s_proto, "tcp") == 0) {
350 1.1 itojun pai->ai_socktype = SOCK_STREAM;
351 1.1 itojun pai->ai_protocol = IPPROTO_TCP;
352 1.1 itojun } else
353 1.1 itojun ERR(EAI_PROTOCOL); /*xxx*/
354 1.1 itojun }
355 1.1 itojun }
356 1.1 itojun }
357 1.1 itojun
358 1.1 itojun /*
359 1.1 itojun * hostname == NULL.
360 1.1 itojun * passive socket -> anyaddr (0.0.0.0 or ::)
361 1.1 itojun * non-passive socket -> localhost (127.0.0.1 or ::1)
362 1.1 itojun */
363 1.1 itojun if (hostname == NULL) {
364 1.1 itojun struct afd *afd;
365 1.1 itojun int s;
366 1.1 itojun
367 1.1 itojun for (afd = &afdl[0]; afd->a_af; afd++) {
368 1.1 itojun if (!(pai->ai_family == PF_UNSPEC
369 1.1 itojun || pai->ai_family == afd->a_af)) {
370 1.1 itojun continue;
371 1.1 itojun }
372 1.1 itojun
373 1.1 itojun /*
374 1.1 itojun * filter out AFs that are not supported by the kernel
375 1.1 itojun * XXX errno?
376 1.1 itojun */
377 1.1 itojun s = socket(afd->a_af, SOCK_DGRAM, 0);
378 1.1 itojun if (s < 0)
379 1.1 itojun continue;
380 1.1 itojun close(s);
381 1.1 itojun
382 1.1 itojun if (pai->ai_flags & AI_PASSIVE) {
383 1.1 itojun GET_AI(cur->ai_next, afd, afd->a_addrany, port);
384 1.1 itojun /* xxx meaningless?
385 1.1 itojun * GET_CANONNAME(cur->ai_next, "anyaddr");
386 1.1 itojun */
387 1.1 itojun } else {
388 1.1 itojun GET_AI(cur->ai_next, afd, afd->a_loopback,
389 1.1 itojun port);
390 1.1 itojun /* xxx meaningless?
391 1.1 itojun * GET_CANONNAME(cur->ai_next, "localhost");
392 1.1 itojun */
393 1.1 itojun }
394 1.1 itojun cur = cur->ai_next;
395 1.1 itojun }
396 1.1 itojun top = sentinel.ai_next;
397 1.1 itojun if (top)
398 1.1 itojun goto good;
399 1.1 itojun else
400 1.1 itojun ERR(EAI_FAMILY);
401 1.1 itojun }
402 1.1 itojun
403 1.1 itojun /* hostname as numeric name */
404 1.1 itojun for (i = 0; afdl[i].a_af; i++) {
405 1.1 itojun if (inet_pton(afdl[i].a_af, hostname, pton)) {
406 1.1 itojun u_long v4a;
407 1.1 itojun u_char pfx;
408 1.1 itojun
409 1.1 itojun switch (afdl[i].a_af) {
410 1.1 itojun case AF_INET:
411 1.1 itojun v4a = ntohl(((struct in_addr *)pton)->s_addr);
412 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
413 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
414 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
415 1.1 itojun if (v4a == 0 || v4a == IN_LOOPBACKNET)
416 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
417 1.1 itojun break;
418 1.1 itojun #ifdef INET6
419 1.1 itojun case AF_INET6:
420 1.1 itojun pfx = ((struct in6_addr *)pton)->s6_addr8[0];
421 1.1 itojun if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
422 1.1 itojun pai->ai_flags &= ~AI_CANONNAME;
423 1.1 itojun break;
424 1.1 itojun #endif
425 1.1 itojun }
426 1.1 itojun
427 1.1 itojun if (pai->ai_family == afdl[i].a_af ||
428 1.1 itojun pai->ai_family == PF_UNSPEC) {
429 1.1 itojun if (! (pai->ai_flags & AI_CANONNAME)) {
430 1.1 itojun GET_AI(top, &afdl[i], pton, port);
431 1.1 itojun goto good;
432 1.1 itojun }
433 1.1 itojun /*
434 1.1 itojun * if AI_CANONNAME and if reverse lookup
435 1.1 itojun * fail, return ai anyway to pacify
436 1.1 itojun * calling application.
437 1.1 itojun *
438 1.1 itojun * XXX getaddrinfo() is a name->address
439 1.1 itojun * translation function, and it looks strange
440 1.1 itojun * that we do addr->name translation here.
441 1.1 itojun */
442 1.1 itojun get_name(pton, &afdl[i], &top, pton, pai, port);
443 1.1 itojun goto good;
444 1.1 itojun } else
445 1.1 itojun ERR(EAI_FAMILY); /*xxx*/
446 1.1 itojun }
447 1.1 itojun }
448 1.1 itojun
449 1.1 itojun if (pai->ai_flags & AI_NUMERICHOST)
450 1.1 itojun ERR(EAI_NONAME);
451 1.1 itojun
452 1.1 itojun /* hostname as alphabetical name */
453 1.1 itojun error = get_addr(hostname, pai->ai_family, &top, pai, port);
454 1.1 itojun if (error == 0) {
455 1.1 itojun if (top) {
456 1.1 itojun good:
457 1.1 itojun *res = top;
458 1.1 itojun return SUCCESS;
459 1.1 itojun } else
460 1.1 itojun error = EAI_FAIL;
461 1.1 itojun }
462 1.1 itojun free:
463 1.1 itojun if (top)
464 1.1 itojun freeaddrinfo(top);
465 1.1 itojun bad:
466 1.1 itojun *res = NULL;
467 1.1 itojun return error;
468 1.1 itojun }
469 1.1 itojun
470 1.1 itojun static int
471 1.1 itojun get_name(addr, afd, res, numaddr, pai, port0)
472 1.1 itojun const char *addr;
473 1.1 itojun struct afd *afd;
474 1.1 itojun struct addrinfo **res;
475 1.1 itojun char *numaddr;
476 1.1 itojun struct addrinfo *pai;
477 1.1 itojun int port0;
478 1.1 itojun {
479 1.1 itojun u_short port = port0 & 0xffff;
480 1.1 itojun struct hostent *hp;
481 1.1 itojun struct addrinfo *cur;
482 1.1 itojun int error = 0;
483 1.1 itojun #ifdef USE_GETIPNODEBY
484 1.1 itojun int h_error;
485 1.1 itojun
486 1.1 itojun hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
487 1.1 itojun #else
488 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
489 1.1 itojun #endif
490 1.1 itojun if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
491 1.1 itojun GET_AI(cur, afd, hp->h_addr_list[0], port);
492 1.1 itojun GET_CANONNAME(cur, hp->h_name);
493 1.1 itojun } else
494 1.1 itojun GET_AI(cur, afd, numaddr, port);
495 1.1 itojun
496 1.1 itojun #ifdef USE_GETIPNODEBY
497 1.1 itojun if (hp)
498 1.1 itojun freehostent(hp);
499 1.1 itojun #endif
500 1.1 itojun *res = cur;
501 1.1 itojun return SUCCESS;
502 1.1 itojun free:
503 1.1 itojun if (cur)
504 1.1 itojun freeaddrinfo(cur);
505 1.1 itojun #ifdef USE_GETIPNODEBY
506 1.1 itojun if (hp)
507 1.1 itojun freehostent(hp);
508 1.1 itojun #endif
509 1.1 itojun /* bad: */
510 1.1 itojun *res = NULL;
511 1.1 itojun return error;
512 1.1 itojun }
513 1.1 itojun
514 1.1 itojun static int
515 1.1 itojun get_addr(hostname, af, res, pai, port0)
516 1.1 itojun const char *hostname;
517 1.1 itojun int af;
518 1.1 itojun struct addrinfo **res;
519 1.1 itojun struct addrinfo *pai;
520 1.1 itojun int port0;
521 1.1 itojun {
522 1.1 itojun int i, error;
523 1.1 itojun struct addrinfo *cur;
524 1.1 itojun
525 1.1 itojun #ifdef USE_GETIPNODEBY
526 1.1 itojun return get_addr0(hostname, af, res, pai, port0);
527 1.1 itojun #else
528 1.1 itojun for (i = 0; afdl[i].a_af; i++) {
529 1.1 itojun if (af == AF_UNSPEC || af == afdl[i].a_af)
530 1.1 itojun ;
531 1.1 itojun else
532 1.1 itojun continue;
533 1.1 itojun error = get_addr0(hostname, afdl[i].a_af, res, pai, port0);
534 1.1 itojun if (error == EAI_FAIL)
535 1.1 itojun return error;
536 1.1 itojun else
537 1.1 itojun error = 0;
538 1.1 itojun if (*res) {
539 1.1 itojun /* make chain of addrs */
540 1.1 itojun for (cur = *res;
541 1.1 itojun cur && cur->ai_next;
542 1.1 itojun cur = cur->ai_next)
543 1.1 itojun ;
544 1.1 itojun if (!cur)
545 1.1 itojun return EAI_FAIL;
546 1.1 itojun res = &cur->ai_next;
547 1.1 itojun }
548 1.1 itojun }
549 1.1 itojun
550 1.1 itojun return error;
551 1.1 itojun #endif
552 1.1 itojun }
553 1.1 itojun
554 1.1 itojun static int
555 1.1 itojun get_addr0(hostname, af, res, pai, port0)
556 1.1 itojun const char *hostname;
557 1.1 itojun int af;
558 1.1 itojun struct addrinfo **res;
559 1.1 itojun struct addrinfo *pai;
560 1.1 itojun int port0;
561 1.1 itojun {
562 1.1 itojun u_short port = port0 & 0xffff;
563 1.1 itojun struct addrinfo sentinel;
564 1.1 itojun struct hostent *hp;
565 1.1 itojun struct addrinfo *top, *cur;
566 1.1 itojun struct afd *afd;
567 1.1 itojun int i, error = 0, h_error;
568 1.1 itojun char *ap;
569 1.1 itojun #ifndef USE_GETIPNODEBY
570 1.1 itojun extern int h_errno;
571 1.1 itojun #endif
572 1.1 itojun
573 1.1 itojun top = NULL;
574 1.1 itojun sentinel.ai_next = NULL;
575 1.1 itojun cur = &sentinel;
576 1.1 itojun #ifdef USE_GETIPNODEBY
577 1.1 itojun if (af == AF_UNSPEC) {
578 1.1 itojun hp = getipnodebyname(hostname, AF_INET6,
579 1.1 itojun AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
580 1.1 itojun } else
581 1.1 itojun hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
582 1.1 itojun #else
583 1.1 itojun if (af == AF_UNSPEC) {
584 1.1 itojun error = EAI_FAIL;
585 1.1 itojun goto bad;
586 1.1 itojun }
587 1.1 itojun hp = gethostbyname2(hostname, af);
588 1.1 itojun h_error = h_errno;
589 1.1 itojun #endif
590 1.1 itojun if (hp == NULL) {
591 1.1 itojun switch (h_error) {
592 1.1 itojun case HOST_NOT_FOUND:
593 1.1 itojun case NO_DATA:
594 1.1 itojun error = EAI_NODATA;
595 1.1 itojun break;
596 1.1 itojun case TRY_AGAIN:
597 1.1 itojun error = EAI_AGAIN;
598 1.1 itojun break;
599 1.1 itojun case NO_RECOVERY:
600 1.1 itojun default:
601 1.1 itojun error = EAI_FAIL;
602 1.1 itojun break;
603 1.1 itojun }
604 1.1 itojun goto bad;
605 1.1 itojun }
606 1.1 itojun
607 1.1 itojun if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
608 1.1 itojun (hp->h_addr_list[0] == NULL))
609 1.1 itojun ERR(EAI_FAIL);
610 1.1 itojun
611 1.1 itojun for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
612 1.1 itojun switch (af) {
613 1.1 itojun #ifdef INET6
614 1.1 itojun case AF_INET6:
615 1.1 itojun afd = &afdl[N_INET6];
616 1.1 itojun break;
617 1.1 itojun #endif
618 1.1 itojun #ifndef INET6
619 1.1 itojun default: /* AF_UNSPEC */
620 1.1 itojun #endif
621 1.1 itojun case AF_INET:
622 1.1 itojun afd = &afdl[N_INET];
623 1.1 itojun break;
624 1.1 itojun #ifdef INET6
625 1.1 itojun default: /* AF_UNSPEC */
626 1.1 itojun if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
627 1.1 itojun ap += sizeof(struct in6_addr) -
628 1.1 itojun sizeof(struct in_addr);
629 1.1 itojun afd = &afdl[N_INET];
630 1.1 itojun } else
631 1.1 itojun afd = &afdl[N_INET6];
632 1.1 itojun break;
633 1.1 itojun #endif
634 1.1 itojun }
635 1.1 itojun #ifdef FAITH
636 1.1 itojun if (translate && afd->a_af == AF_INET) {
637 1.1 itojun struct in6_addr *in6;
638 1.1 itojun
639 1.1 itojun GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
640 1.1 itojun in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
641 1.1 itojun memcpy(&in6->s6_addr32[0], &faith_prefix,
642 1.1 itojun sizeof(struct in6_addr) - sizeof(struct in_addr));
643 1.1 itojun memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
644 1.1 itojun } else
645 1.1 itojun #endif /* FAITH */
646 1.1 itojun GET_AI(cur->ai_next, afd, ap, port);
647 1.1 itojun if (cur == &sentinel) {
648 1.1 itojun top = cur->ai_next;
649 1.1 itojun GET_CANONNAME(top, hp->h_name);
650 1.1 itojun }
651 1.1 itojun cur = cur->ai_next;
652 1.1 itojun }
653 1.1 itojun #ifdef USE_GETIPNODEBY
654 1.1 itojun freehostent(hp);
655 1.1 itojun #endif
656 1.1 itojun *res = top;
657 1.1 itojun return SUCCESS;
658 1.1 itojun free:
659 1.1 itojun if (top)
660 1.1 itojun freeaddrinfo(top);
661 1.1 itojun #ifdef USE_GETIPNODEBY
662 1.1 itojun if (hp)
663 1.1 itojun freehostent(hp);
664 1.1 itojun #endif
665 1.1 itojun bad:
666 1.1 itojun *res = NULL;
667 1.1 itojun return error;
668 1.1 itojun }
669