Updated MSpec source to 1c3ee1c8.
[rbx.git] / stdlib / ext / socket / getaddrinfo.c
blob9fb4ebcb06af40c230efe88f053364beca75eed1
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 "config.h"
42 #include <sys/types.h>
43 #if !defined(_WIN32) && !defined(__VMS)
44 #include <sys/param.h>
45 #if defined(__BEOS__)
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 #elif defined(__VMS )
66 #include <socket.h>
67 #include <inet.h>
68 #include <in.h>
69 #include <netdb.h>
70 #else
71 #include <winsock2.h>
72 #include <io.h>
73 #endif
74 #include <string.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <stddef.h>
78 #include <ctype.h>
80 #ifdef SOCKS5
81 #include <socks.h>
82 #endif
84 #include "addrinfo.h"
85 #include "sockport.h"
87 #if defined(__KAME__) && defined(INET6)
88 # define FAITH
89 #endif
91 #define SUCCESS 0
92 #define ANY 0
93 #define YES 1
94 #define NO 0
96 #ifdef FAITH
97 static int translate = NO;
98 static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
99 #endif
101 static const char in_addrany[] = { 0, 0, 0, 0 };
102 static const char in6_addrany[] = {
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
105 static const char in_loopback[] = { 127, 0, 0, 1 };
106 static const char in6_loopback[] = {
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
110 struct sockinet {
111 u_char si_len;
112 u_char si_family;
113 u_short si_port;
116 static const struct afd {
117 int a_af;
118 int a_addrlen;
119 int a_socklen;
120 int a_off;
121 const char *a_addrany;
122 const char *a_loopback;
123 } afdl [] = {
124 #ifdef INET6
125 #define N_INET6 0
126 {PF_INET6, sizeof(struct in6_addr),
127 sizeof(struct sockaddr_in6),
128 offsetof(struct sockaddr_in6, sin6_addr),
129 in6_addrany, in6_loopback},
130 #define N_INET 1
131 #else
132 #define N_INET 0
133 #endif
134 {PF_INET, sizeof(struct in_addr),
135 sizeof(struct sockaddr_in),
136 offsetof(struct sockaddr_in, sin_addr),
137 in_addrany, in_loopback},
138 {0, 0, 0, 0, NULL, NULL},
141 #ifdef INET6
142 #define PTON_MAX 16
143 #else
144 #define PTON_MAX 4
145 #endif
147 static int get_name __P((const char *, const struct afd *,
148 struct addrinfo **, char *, struct addrinfo *,
149 int));
150 static int get_addr __P((const char *, int, struct addrinfo **,
151 struct addrinfo *, int));
152 static int str_isnumber __P((const char *));
154 static const char *const ai_errlist[] = {
155 "success.",
156 "address family for hostname not supported.", /* EAI_ADDRFAMILY */
157 "temporary failure in name resolution.", /* EAI_AGAIN */
158 "invalid value for ai_flags.", /* EAI_BADFLAGS */
159 "non-recoverable failure in name resolution.", /* EAI_FAIL */
160 "ai_family not supported.", /* EAI_FAMILY */
161 "memory allocation failure.", /* EAI_MEMORY */
162 "no address associated with hostname.", /* EAI_NODATA */
163 "hostname nor servname provided, or not known.",/* EAI_NONAME */
164 "servname not supported for ai_socktype.", /* EAI_SERVICE */
165 "ai_socktype not supported.", /* EAI_SOCKTYPE */
166 "system error returned in errno.", /* EAI_SYSTEM */
167 "invalid value for hints.", /* EAI_BADHINTS */
168 "resolved protocol is unknown.", /* EAI_PROTOCOL */
169 "unknown error.", /* EAI_MAX */
172 #define GET_CANONNAME(ai, str) \
173 if (pai->ai_flags & AI_CANONNAME) {\
174 if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
175 strcpy((ai)->ai_canonname, (str));\
176 } else {\
177 error = EAI_MEMORY;\
178 goto free;\
182 #define GET_AI(ai, afd, addr, port) {\
183 char *p;\
184 if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
185 ((afd)->a_socklen)))\
186 == NULL) {\
187 error = EAI_MEMORY;\
188 goto free;\
190 memcpy(ai, pai, sizeof(struct addrinfo));\
191 (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
192 memset((ai)->ai_addr, 0, (afd)->a_socklen);\
193 SET_SA_LEN((ai)->ai_addr, (ai)->ai_addrlen = (afd)->a_socklen);\
194 (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
195 ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
196 p = (char *)((ai)->ai_addr);\
197 memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
200 #define ERR(err) { error = (err); goto bad; }
202 #if defined __UCLIBC__
203 const
204 #endif
205 char *
206 gai_strerror(ecode)
207 int ecode;
209 if (ecode < 0 || ecode > EAI_MAX)
210 ecode = EAI_MAX;
211 return (char *)ai_errlist[ecode];
214 void
215 freeaddrinfo(ai)
216 struct addrinfo *ai;
218 struct addrinfo *next;
220 do {
221 next = ai->ai_next;
222 if (ai->ai_canonname)
223 free(ai->ai_canonname);
224 /* no need to free(ai->ai_addr) */
225 free(ai);
226 } while ((ai = next) != NULL);
229 static int
230 str_isnumber(p)
231 const char *p;
233 char *q = (char *)p;
234 while (*q) {
235 if (! isdigit(*q))
236 return NO;
237 q++;
239 return YES;
242 #ifndef HAVE_INET_PTON
244 static int
245 inet_pton(af, hostname, pton)
246 int af;
247 const char *hostname;
248 void *pton;
250 struct in_addr in;
252 #ifdef HAVE_INET_ATON
253 if (!inet_aton(hostname, &in))
254 return 0;
255 #else
256 int d1, d2, d3, d4;
257 char ch;
259 if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
260 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
261 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
262 in.s_addr = htonl(
263 ((long) d1 << 24) | ((long) d2 << 16) |
264 ((long) d3 << 8) | ((long) d4 << 0));
266 else {
267 return 0;
269 #endif
270 memcpy(pton, &in, sizeof(in));
271 return 1;
273 #endif
276 getaddrinfo(hostname, servname, hints, res)
277 const char *hostname, *servname;
278 const struct addrinfo *hints;
279 struct addrinfo **res;
281 struct addrinfo sentinel;
282 struct addrinfo *top = NULL;
283 struct addrinfo *cur;
284 int i, error = 0;
285 char pton[PTON_MAX];
286 struct addrinfo ai;
287 struct addrinfo *pai;
288 u_short port;
290 #ifdef FAITH
291 static int firsttime = 1;
293 if (firsttime) {
294 /* translator hack */
296 char *q = getenv("GAI");
297 if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
298 translate = YES;
300 firsttime = 0;
302 #endif
304 /* initialize file static vars */
305 sentinel.ai_next = NULL;
306 cur = &sentinel;
307 pai = &ai;
308 pai->ai_flags = 0;
309 pai->ai_family = PF_UNSPEC;
310 pai->ai_socktype = ANY;
311 pai->ai_protocol = ANY;
312 pai->ai_addrlen = 0;
313 pai->ai_canonname = NULL;
314 pai->ai_addr = NULL;
315 pai->ai_next = NULL;
316 port = ANY;
318 if (hostname == NULL && servname == NULL)
319 return EAI_NONAME;
320 if (hints) {
321 /* error check for hints */
322 if (hints->ai_addrlen || hints->ai_canonname ||
323 hints->ai_addr || hints->ai_next)
324 ERR(EAI_BADHINTS); /* xxx */
325 if (hints->ai_flags & ~AI_MASK)
326 ERR(EAI_BADFLAGS);
327 switch (hints->ai_family) {
328 case PF_UNSPEC:
329 case PF_INET:
330 #ifdef INET6
331 case PF_INET6:
332 #endif
333 break;
334 default:
335 ERR(EAI_FAMILY);
337 memcpy(pai, hints, sizeof(*pai));
338 switch (pai->ai_socktype) {
339 case ANY:
340 switch (pai->ai_protocol) {
341 case ANY:
342 break;
343 case IPPROTO_UDP:
344 pai->ai_socktype = SOCK_DGRAM;
345 break;
346 case IPPROTO_TCP:
347 pai->ai_socktype = SOCK_STREAM;
348 break;
349 default:
350 #if defined(SOCK_RAW)
351 pai->ai_socktype = SOCK_RAW;
352 #endif
353 break;
355 break;
356 #if defined(SOCK_RAW)
357 case SOCK_RAW:
358 break;
359 #endif
360 case SOCK_DGRAM:
361 if (pai->ai_protocol != IPPROTO_UDP &&
362 pai->ai_protocol != ANY)
363 ERR(EAI_BADHINTS); /*xxx*/
364 pai->ai_protocol = IPPROTO_UDP;
365 break;
366 case SOCK_STREAM:
367 if (pai->ai_protocol != IPPROTO_TCP &&
368 pai->ai_protocol != ANY)
369 ERR(EAI_BADHINTS); /*xxx*/
370 pai->ai_protocol = IPPROTO_TCP;
371 break;
372 default:
373 ERR(EAI_SOCKTYPE);
374 break;
379 * service port
381 if (servname) {
382 if (str_isnumber(servname)) {
383 if (pai->ai_socktype == ANY) {
384 /* caller accept *ANY* socktype */
385 pai->ai_socktype = SOCK_DGRAM;
386 pai->ai_protocol = IPPROTO_UDP;
388 port = htons((unsigned short)atoi(servname));
389 } else {
390 struct servent *sp;
391 char *proto;
393 proto = NULL;
394 switch (pai->ai_socktype) {
395 case ANY:
396 proto = NULL;
397 break;
398 case SOCK_DGRAM:
399 proto = "udp";
400 break;
401 case SOCK_STREAM:
402 proto = "tcp";
403 break;
404 default:
405 fprintf(stderr, "panic!\n");
406 break;
408 if ((sp = getservbyname((char*)servname, proto)) == NULL)
409 ERR(EAI_SERVICE);
410 port = sp->s_port;
411 if (pai->ai_socktype == ANY)
412 if (strcmp(sp->s_proto, "udp") == 0) {
413 pai->ai_socktype = SOCK_DGRAM;
414 pai->ai_protocol = IPPROTO_UDP;
415 } else if (strcmp(sp->s_proto, "tcp") == 0) {
416 pai->ai_socktype = SOCK_STREAM;
417 pai->ai_protocol = IPPROTO_TCP;
418 } else
419 ERR(EAI_PROTOCOL); /*xxx*/
424 * hostname == NULL.
425 * passive socket -> anyaddr (0.0.0.0 or ::)
426 * non-passive socket -> localhost (127.0.0.1 or ::1)
428 if (hostname == NULL) {
429 const struct afd *afd;
430 int s;
432 for (afd = &afdl[0]; afd->a_af; afd++) {
433 if (!(pai->ai_family == PF_UNSPEC
434 || pai->ai_family == afd->a_af)) {
435 continue;
439 * filter out AFs that are not supported by the kernel
440 * XXX errno?
442 s = socket(afd->a_af, SOCK_DGRAM, 0);
443 if (s < 0)
444 continue;
445 #if defined(HAVE_CLOSESOCKET)
446 closesocket(s);
447 #else
448 close(s);
449 #endif
451 if (pai->ai_flags & AI_PASSIVE) {
452 GET_AI(cur->ai_next, afd, afd->a_addrany, port);
453 /* xxx meaningless?
454 * GET_CANONNAME(cur->ai_next, "anyaddr");
456 } else {
457 GET_AI(cur->ai_next, afd, afd->a_loopback,
458 port);
459 /* xxx meaningless?
460 * GET_CANONNAME(cur->ai_next, "localhost");
463 cur = cur->ai_next;
465 top = sentinel.ai_next;
466 if (top)
467 goto good;
468 else
469 ERR(EAI_FAMILY);
472 /* hostname as numeric name */
473 for (i = 0; afdl[i].a_af; i++) {
474 if (inet_pton(afdl[i].a_af, hostname, pton)) {
475 u_long v4a;
476 #ifdef INET6
477 u_char pfx;
478 #endif
480 switch (afdl[i].a_af) {
481 case AF_INET:
482 v4a = ((struct in_addr *)pton)->s_addr;
483 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
484 pai->ai_flags &= ~AI_CANONNAME;
485 v4a >>= IN_CLASSA_NSHIFT;
486 if (v4a == 0 || v4a == IN_LOOPBACKNET)
487 pai->ai_flags &= ~AI_CANONNAME;
488 break;
489 #ifdef INET6
490 case AF_INET6:
491 #ifdef HAVE_ADDR8
492 pfx = ((struct in6_addr *)pton)->s6_addr8[0];
493 #else
494 pfx = ((struct in6_addr *)pton)->s6_addr[0];
495 #endif
496 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
497 pai->ai_flags &= ~AI_CANONNAME;
498 break;
499 #endif
502 if (pai->ai_family == afdl[i].a_af ||
503 pai->ai_family == PF_UNSPEC) {
504 if (! (pai->ai_flags & AI_CANONNAME)) {
505 GET_AI(top, &afdl[i], pton, port);
506 goto good;
509 * if AI_CANONNAME and if reverse lookup
510 * fail, return ai anyway to pacify
511 * calling application.
513 * XXX getaddrinfo() is a name->address
514 * translation function, and it looks strange
515 * that we do addr->name translation here.
517 get_name(pton, &afdl[i], &top, pton, pai, port);
518 goto good;
519 } else
520 ERR(EAI_FAMILY); /*xxx*/
524 if (pai->ai_flags & AI_NUMERICHOST)
525 ERR(EAI_NONAME);
527 /* hostname as alphabetical name */
528 error = get_addr(hostname, pai->ai_family, &top, pai, port);
529 if (error == 0) {
530 if (top) {
531 good:
532 *res = top;
533 return SUCCESS;
534 } else
535 error = EAI_FAIL;
537 free:
538 if (top)
539 freeaddrinfo(top);
540 bad:
541 *res = NULL;
542 return error;
545 static int
546 get_name(addr, afd, res, numaddr, pai, port0)
547 const char *addr;
548 const struct afd *afd;
549 struct addrinfo **res;
550 char *numaddr;
551 struct addrinfo *pai;
552 int port0;
554 u_short port = port0 & 0xffff;
555 struct hostent *hp;
556 struct addrinfo *cur;
557 int error = 0;
558 #ifdef INET6
559 int h_error;
560 #endif
562 #ifdef INET6
563 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
564 #else
565 hp = gethostbyaddr((char*)addr, afd->a_addrlen, AF_INET);
566 #endif
567 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
568 GET_AI(cur, afd, hp->h_addr_list[0], port);
569 GET_CANONNAME(cur, hp->h_name);
570 } else
571 GET_AI(cur, afd, numaddr, port);
573 #ifdef INET6
574 if (hp)
575 freehostent(hp);
576 #endif
577 *res = cur;
578 return SUCCESS;
579 free:
580 if (cur)
581 freeaddrinfo(cur);
582 #ifdef INET6
583 if (hp)
584 freehostent(hp);
585 #endif
586 /* bad: */
587 *res = NULL;
588 return error;
591 static int
592 get_addr(hostname, af, res, pai, port0)
593 const char *hostname;
594 int af;
595 struct addrinfo **res;
596 struct addrinfo *pai;
597 int port0;
599 u_short port = port0 & 0xffff;
600 struct addrinfo sentinel;
601 struct hostent *hp;
602 struct addrinfo *top, *cur;
603 const struct afd *afd;
604 int i, error = 0, h_error;
605 char *ap;
607 top = NULL;
608 sentinel.ai_next = NULL;
609 cur = &sentinel;
610 #ifdef INET6
611 if (af == AF_UNSPEC) {
612 hp = getipnodebyname(hostname, AF_INET6,
613 AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
614 } else
615 hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
616 #else
617 hp = gethostbyname((char*)hostname);
618 h_error = h_errno;
619 #endif
620 if (hp == NULL) {
621 switch (h_error) {
622 case HOST_NOT_FOUND:
623 case NO_DATA:
624 error = EAI_NODATA;
625 break;
626 case TRY_AGAIN:
627 error = EAI_AGAIN;
628 break;
629 case NO_RECOVERY:
630 default:
631 error = EAI_FAIL;
632 break;
634 goto bad;
637 if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
638 (hp->h_addr_list[0] == NULL))
639 ERR(EAI_FAIL);
641 for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
642 switch (af) {
643 #ifdef INET6
644 case AF_INET6:
645 afd = &afdl[N_INET6];
646 break;
647 #endif
648 #ifndef INET6
649 default: /* AF_UNSPEC */
650 #endif
651 case AF_INET:
652 afd = &afdl[N_INET];
653 break;
654 #ifdef INET6
655 default: /* AF_UNSPEC */
656 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
657 ap += sizeof(struct in6_addr) -
658 sizeof(struct in_addr);
659 afd = &afdl[N_INET];
660 } else
661 afd = &afdl[N_INET6];
662 break;
663 #endif
665 #ifdef FAITH
666 if (translate && afd->a_af == AF_INET) {
667 struct in6_addr *in6;
669 GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
670 in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
671 memcpy(&in6->s6_addr32[0], &faith_prefix,
672 sizeof(struct in6_addr) - sizeof(struct in_addr));
673 memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
674 } else
675 #endif /* FAITH */
676 GET_AI(cur->ai_next, afd, ap, port);
677 if (cur == &sentinel) {
678 top = cur->ai_next;
679 GET_CANONNAME(top, hp->h_name);
681 cur = cur->ai_next;
683 #ifdef INET6
684 freehostent(hp);
685 #endif
686 *res = top;
687 return SUCCESS;
688 free:
689 if (top)
690 freeaddrinfo(top);
691 #ifdef INET6
692 if (hp)
693 freehostent(hp);
694 #endif
695 bad:
696 *res = NULL;
697 return error;