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