* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / ext / socket / getaddrinfo.c
blobe99c4e462bedb6396ac10636e1e52848e10b9167
1 /*
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.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * 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 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 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
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.
41 #include "ruby/config.h"
42 #include <sys/types.h>
43 #ifndef _WIN32
44 #include <sys/param.h>
45 #if defined(__BEOS__) && !defined(__HAIKU__)
46 # include <net/socket.h>
47 #else
48 # include <sys/socket.h>
49 #endif
50 #include <netinet/in.h>
51 #if defined(HAVE_ARPA_INET_H)
52 #include <arpa/inet.h>
53 #endif
54 #if defined(HAVE_ARPA_NAMESER_H)
55 #include <arpa/nameser.h>
56 #endif
57 #include <netdb.h>
58 #if defined(HAVE_RESOLV_H)
59 #ifdef _SX
60 #include <stdio.h>
61 #endif
62 #include <resolv.h>
63 #endif
64 #include <unistd.h>
65 #else
66 #include <winsock2.h>
67 #include <io.h>
68 #endif
69 #include <string.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <stddef.h>
73 #include <ctype.h>
75 #ifdef SOCKS5
76 #include <socks.h>
77 #endif
79 #include "addrinfo.h"
80 #include "sockport.h"
82 #if defined(__KAME__) && defined(INET6)
83 # define FAITH
84 #endif
86 #define SUCCESS 0
87 #define ANY 0
88 #define YES 1
89 #define NO 0
91 #ifdef FAITH
92 static int translate = NO;
93 static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
94 #endif
96 static const char in_addrany[] = { 0, 0, 0, 0 };
97 static const char in6_addrany[] = {
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
100 static const char in_loopback[] = { 127, 0, 0, 1 };
101 static const char in6_loopback[] = {
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
105 struct sockinet {
106 u_char si_len;
107 u_char si_family;
108 u_short si_port;
111 static const struct afd {
112 int a_af;
113 int a_addrlen;
114 int a_socklen;
115 int a_off;
116 const char *a_addrany;
117 const char *a_loopback;
118 } afdl [] = {
119 #ifdef INET6
120 #define N_INET6 0
121 {PF_INET6, sizeof(struct in6_addr),
122 sizeof(struct sockaddr_in6),
123 offsetof(struct sockaddr_in6, sin6_addr),
124 in6_addrany, in6_loopback},
125 #define N_INET 1
126 #else
127 #define N_INET 0
128 #endif
129 {PF_INET, sizeof(struct in_addr),
130 sizeof(struct sockaddr_in),
131 offsetof(struct sockaddr_in, sin_addr),
132 in_addrany, in_loopback},
133 {0, 0, 0, 0, NULL, NULL},
136 #ifdef INET6
137 #define PTON_MAX 16
138 #else
139 #define PTON_MAX 4
140 #endif
142 static int get_name __P((const char *, const struct afd *,
143 struct addrinfo **, char *, struct addrinfo *,
144 int));
145 static int get_addr __P((const char *, int, struct addrinfo **,
146 struct addrinfo *, int));
147 static int str_isnumber __P((const char *));
149 static const char *const ai_errlist[] = {
150 "success.",
151 "address family for hostname not supported.", /* EAI_ADDRFAMILY */
152 "temporary failure in name resolution.", /* EAI_AGAIN */
153 "invalid value for ai_flags.", /* EAI_BADFLAGS */
154 "non-recoverable failure in name resolution.", /* EAI_FAIL */
155 "ai_family not supported.", /* EAI_FAMILY */
156 "memory allocation failure.", /* EAI_MEMORY */
157 "no address associated with hostname.", /* EAI_NODATA */
158 "hostname nor servname provided, or not known.",/* EAI_NONAME */
159 "servname not supported for ai_socktype.", /* EAI_SERVICE */
160 "ai_socktype not supported.", /* EAI_SOCKTYPE */
161 "system error returned in errno.", /* EAI_SYSTEM */
162 "invalid value for hints.", /* EAI_BADHINTS */
163 "resolved protocol is unknown.", /* EAI_PROTOCOL */
164 "unknown error.", /* EAI_MAX */
167 #define GET_CANONNAME(ai, str) \
168 if (pai->ai_flags & AI_CANONNAME) {\
169 if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
170 strcpy((ai)->ai_canonname, (str));\
171 } else {\
172 error = EAI_MEMORY;\
173 goto free;\
177 #define GET_AI(ai, afd, addr, port) {\
178 char *p;\
179 if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
180 ((afd)->a_socklen)))\
181 == NULL) {\
182 error = EAI_MEMORY;\
183 goto free;\
185 memcpy(ai, pai, sizeof(struct addrinfo));\
186 (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
187 memset((ai)->ai_addr, 0, (afd)->a_socklen);\
188 SET_SA_LEN((ai)->ai_addr, (ai)->ai_addrlen = (afd)->a_socklen);\
189 (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
190 ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
191 p = (char *)((ai)->ai_addr);\
192 memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
195 #define ERR(err) { error = (err); goto bad; }
197 #ifndef __HAIKU__
198 #if defined __UCLIBC__
199 const
200 #endif
201 char *
202 gai_strerror(int ecode)
204 if (ecode < 0 || ecode > EAI_MAX)
205 ecode = EAI_MAX;
206 return (char *)ai_errlist[ecode];
208 #endif
210 void
211 freeaddrinfo(struct addrinfo *ai)
213 struct addrinfo *next;
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);
224 static int
225 str_isnumber(const char *p)
227 char *q = (char *)p;
228 while (*q) {
229 if (! isdigit(*q))
230 return NO;
231 q++;
233 return YES;
236 #ifndef HAVE_INET_PTON
238 static int
239 inet_pton(int af, const char *hostname, void *pton)
241 struct in_addr in;
243 #ifdef HAVE_INET_ATON
244 if (!inet_aton(hostname, &in))
245 return 0;
246 #else
247 int d1, d2, d3, d4;
248 char ch;
250 if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
251 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
252 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
253 in.s_addr = htonl(
254 ((long) d1 << 24) | ((long) d2 << 16) |
255 ((long) d3 << 8) | ((long) d4 << 0));
257 else {
258 return 0;
260 #endif
261 memcpy(pton, &in, sizeof(in));
262 return 1;
264 #endif
267 getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
269 struct addrinfo sentinel;
270 struct addrinfo *top = NULL;
271 struct addrinfo *cur;
272 int i, error = 0;
273 char pton[PTON_MAX];
274 struct addrinfo ai;
275 struct addrinfo *pai;
276 u_short port;
278 #ifdef FAITH
279 static int firsttime = 1;
281 if (firsttime) {
282 /* translator hack */
284 char *q = getenv("GAI");
285 if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
286 translate = YES;
288 firsttime = 0;
290 #endif
292 /* initialize file static vars */
293 sentinel.ai_next = NULL;
294 cur = &sentinel;
295 pai = &ai;
296 pai->ai_flags = 0;
297 pai->ai_family = PF_UNSPEC;
298 pai->ai_socktype = ANY;
299 pai->ai_protocol = ANY;
300 pai->ai_addrlen = 0;
301 pai->ai_canonname = NULL;
302 pai->ai_addr = NULL;
303 pai->ai_next = NULL;
304 port = ANY;
306 if (hostname == NULL && servname == NULL)
307 return EAI_NONAME;
308 if (hints) {
309 /* error check for hints */
310 if (hints->ai_addrlen || hints->ai_canonname ||
311 hints->ai_addr || hints->ai_next)
312 ERR(EAI_BADHINTS); /* xxx */
313 if (hints->ai_flags & ~AI_MASK)
314 ERR(EAI_BADFLAGS);
315 switch (hints->ai_family) {
316 case PF_UNSPEC:
317 case PF_INET:
318 #ifdef INET6
319 case PF_INET6:
320 #endif
321 break;
322 default:
323 ERR(EAI_FAMILY);
325 memcpy(pai, hints, sizeof(*pai));
326 switch (pai->ai_socktype) {
327 case ANY:
328 switch (pai->ai_protocol) {
329 case ANY:
330 break;
331 case IPPROTO_UDP:
332 pai->ai_socktype = SOCK_DGRAM;
333 break;
334 case IPPROTO_TCP:
335 pai->ai_socktype = SOCK_STREAM;
336 break;
337 default:
338 #if defined(SOCK_RAW)
339 pai->ai_socktype = SOCK_RAW;
340 #endif
341 break;
343 break;
344 #if defined(SOCK_RAW)
345 case SOCK_RAW:
346 break;
347 #endif
348 case SOCK_DGRAM:
349 if (pai->ai_protocol != IPPROTO_UDP &&
350 pai->ai_protocol != ANY)
351 ERR(EAI_BADHINTS); /*xxx*/
352 pai->ai_protocol = IPPROTO_UDP;
353 break;
354 case SOCK_STREAM:
355 if (pai->ai_protocol != IPPROTO_TCP &&
356 pai->ai_protocol != ANY)
357 ERR(EAI_BADHINTS); /*xxx*/
358 pai->ai_protocol = IPPROTO_TCP;
359 break;
360 default:
361 ERR(EAI_SOCKTYPE);
362 break;
367 * service port
369 if (servname) {
370 if (str_isnumber(servname)) {
371 if (pai->ai_socktype == ANY) {
372 /* caller accept *ANY* socktype */
373 pai->ai_socktype = SOCK_DGRAM;
374 pai->ai_protocol = IPPROTO_UDP;
376 port = htons((unsigned short)atoi(servname));
377 } else {
378 struct servent *sp;
379 const char *proto;
381 proto = NULL;
382 switch (pai->ai_socktype) {
383 case ANY:
384 proto = NULL;
385 break;
386 case SOCK_DGRAM:
387 proto = "udp";
388 break;
389 case SOCK_STREAM:
390 proto = "tcp";
391 break;
392 default:
393 fprintf(stderr, "panic!\n");
394 break;
396 if ((sp = getservbyname((char*)servname, proto)) == NULL)
397 ERR(EAI_SERVICE);
398 port = sp->s_port;
399 if (pai->ai_socktype == ANY)
400 if (strcmp(sp->s_proto, "udp") == 0) {
401 pai->ai_socktype = SOCK_DGRAM;
402 pai->ai_protocol = IPPROTO_UDP;
403 } else if (strcmp(sp->s_proto, "tcp") == 0) {
404 pai->ai_socktype = SOCK_STREAM;
405 pai->ai_protocol = IPPROTO_TCP;
406 } else
407 ERR(EAI_PROTOCOL); /*xxx*/
412 * hostname == NULL.
413 * passive socket -> anyaddr (0.0.0.0 or ::)
414 * non-passive socket -> localhost (127.0.0.1 or ::1)
416 if (hostname == NULL) {
417 const struct afd *afd;
418 int s;
420 for (afd = &afdl[0]; afd->a_af; afd++) {
421 if (!(pai->ai_family == PF_UNSPEC
422 || pai->ai_family == afd->a_af)) {
423 continue;
427 * filter out AFs that are not supported by the kernel
428 * XXX errno?
430 s = socket(afd->a_af, SOCK_DGRAM, 0);
431 if (s < 0)
432 continue;
433 #if defined(__BEOS__)
434 closesocket(s);
435 #else
436 close(s);
437 #endif
439 if (pai->ai_flags & AI_PASSIVE) {
440 GET_AI(cur->ai_next, afd, afd->a_addrany, port);
441 /* xxx meaningless?
442 * GET_CANONNAME(cur->ai_next, "anyaddr");
444 } else {
445 GET_AI(cur->ai_next, afd, afd->a_loopback,
446 port);
447 /* xxx meaningless?
448 * GET_CANONNAME(cur->ai_next, "localhost");
451 cur = cur->ai_next;
453 top = sentinel.ai_next;
454 if (top)
455 goto good;
456 else
457 ERR(EAI_FAMILY);
460 /* hostname as numeric name */
461 for (i = 0; afdl[i].a_af; i++) {
462 if (inet_pton(afdl[i].a_af, hostname, pton)) {
463 u_long v4a;
464 #ifdef INET6
465 u_char pfx;
466 #endif
468 switch (afdl[i].a_af) {
469 case AF_INET:
470 v4a = ((struct in_addr *)pton)->s_addr;
471 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
472 pai->ai_flags &= ~AI_CANONNAME;
473 v4a >>= IN_CLASSA_NSHIFT;
474 if (v4a == 0 || v4a == IN_LOOPBACKNET)
475 pai->ai_flags &= ~AI_CANONNAME;
476 break;
477 #ifdef INET6
478 case AF_INET6:
479 #ifdef HAVE_ADDR8
480 pfx = ((struct in6_addr *)pton)->s6_addr8[0];
481 #else
482 pfx = ((struct in6_addr *)pton)->s6_addr[0];
483 #endif
484 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
485 pai->ai_flags &= ~AI_CANONNAME;
486 break;
487 #endif
490 if (pai->ai_family == afdl[i].a_af ||
491 pai->ai_family == PF_UNSPEC) {
492 if (! (pai->ai_flags & AI_CANONNAME)) {
493 GET_AI(top, &afdl[i], pton, port);
494 goto good;
497 * if AI_CANONNAME and if reverse lookup
498 * fail, return ai anyway to pacify
499 * calling application.
501 * XXX getaddrinfo() is a name->address
502 * translation function, and it looks strange
503 * that we do addr->name translation here.
505 get_name(pton, &afdl[i], &top, pton, pai, port);
506 goto good;
507 } else
508 ERR(EAI_FAMILY); /*xxx*/
512 if (pai->ai_flags & AI_NUMERICHOST)
513 ERR(EAI_NONAME);
515 /* hostname as alphabetical name */
516 error = get_addr(hostname, pai->ai_family, &top, pai, port);
517 if (error == 0) {
518 if (top) {
519 good:
520 *res = top;
521 return SUCCESS;
522 } else
523 error = EAI_FAIL;
525 free:
526 if (top)
527 freeaddrinfo(top);
528 bad:
529 *res = NULL;
530 return error;
533 static int
534 get_name(const char *addr, const struct afd *afd, struct addrinfo **res, char *numaddr, struct addrinfo *pai, int port0)
536 u_short port = port0 & 0xffff;
537 struct hostent *hp;
538 struct addrinfo *cur;
539 int error = 0;
540 #ifdef INET6
541 int h_error;
542 #endif
544 #ifdef INET6
545 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
546 #else
547 hp = gethostbyaddr((char*)addr, afd->a_addrlen, AF_INET);
548 #endif
549 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
550 GET_AI(cur, afd, hp->h_addr_list[0], port);
551 GET_CANONNAME(cur, hp->h_name);
552 } else
553 GET_AI(cur, afd, numaddr, port);
555 #ifdef INET6
556 if (hp)
557 freehostent(hp);
558 #endif
559 *res = cur;
560 return SUCCESS;
561 free:
562 if (cur)
563 freeaddrinfo(cur);
564 #ifdef INET6
565 if (hp)
566 freehostent(hp);
567 #endif
568 /* bad: */
569 *res = NULL;
570 return error;
573 static int
574 get_addr(const char *hostname, int af, struct addrinfo **res, struct addrinfo *pai, int port0)
576 u_short port = port0 & 0xffff;
577 struct addrinfo sentinel;
578 struct hostent *hp;
579 struct addrinfo *top, *cur;
580 const struct afd *afd;
581 int i, error = 0, h_error;
582 char *ap;
584 top = NULL;
585 sentinel.ai_next = NULL;
586 cur = &sentinel;
587 #ifdef INET6
588 if (af == AF_UNSPEC) {
589 hp = getipnodebyname(hostname, AF_INET6,
590 AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
591 } else
592 hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
593 #else
594 hp = gethostbyname((char*)hostname);
595 h_error = h_errno;
596 #endif
597 if (hp == NULL) {
598 switch (h_error) {
599 case HOST_NOT_FOUND:
600 case NO_DATA:
601 error = EAI_NODATA;
602 break;
603 case TRY_AGAIN:
604 error = EAI_AGAIN;
605 break;
606 case NO_RECOVERY:
607 default:
608 error = EAI_FAIL;
609 break;
611 goto bad;
614 if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
615 (hp->h_addr_list[0] == NULL))
616 ERR(EAI_FAIL);
618 for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
619 switch (af) {
620 #ifdef INET6
621 case AF_INET6:
622 afd = &afdl[N_INET6];
623 break;
624 #endif
625 #ifndef INET6
626 default: /* AF_UNSPEC */
627 #endif
628 case AF_INET:
629 afd = &afdl[N_INET];
630 break;
631 #ifdef INET6
632 default: /* AF_UNSPEC */
633 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
634 ap += sizeof(struct in6_addr) -
635 sizeof(struct in_addr);
636 afd = &afdl[N_INET];
637 } else
638 afd = &afdl[N_INET6];
639 break;
640 #endif
642 #ifdef FAITH
643 if (translate && afd->a_af == AF_INET) {
644 struct in6_addr *in6;
646 GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
647 in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
648 memcpy(&in6->s6_addr32[0], &faith_prefix,
649 sizeof(struct in6_addr) - sizeof(struct in_addr));
650 memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
651 } else
652 #endif /* FAITH */
653 GET_AI(cur->ai_next, afd, ap, port);
654 if (cur == &sentinel) {
655 top = cur->ai_next;
656 GET_CANONNAME(top, hp->h_name);
658 cur = cur->ai_next;
660 #ifdef INET6
661 freehostent(hp);
662 #endif
663 *res = top;
664 return SUCCESS;
665 free:
666 if (top)
667 freeaddrinfo(top);
668 #ifdef INET6
669 if (hp)
670 freehostent(hp);
671 #endif
672 bad:
673 *res = NULL;
674 return error;