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