getaddrinfo.c revision 1.7 1 /* $NetBSD: getaddrinfo.c,v 1.7 1999/07/04 02:02:29 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 <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <arpa/nameser.h>
50 #include <netdb.h>
51 #include <resolv.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <stddef.h>
55 #include <ctype.h>
56 #include <unistd.h>
57
58 #if 0 /*quickhack*/
59 #if defined(__KAME__) && defined(INET6)
60 # define FAITH
61 #endif
62 #endif
63
64 #define SUCCESS 0
65 #define ANY 0
66 #define YES 1
67 #define NO 0
68
69 #ifdef FAITH
70 static int translate = NO;
71 static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
72 #endif
73
74 static const char in_addrany[] = { 0, 0, 0, 0 };
75 static const char in6_addrany[] = {
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
77 };
78 static const char in_loopback[] = { 127, 0, 0, 1 };
79 static const char in6_loopback[] = {
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
81 };
82
83 struct sockinet {
84 u_char si_len;
85 u_char si_family;
86 u_short si_port;
87 };
88
89 static struct 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 } 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
120
121 static int get_name __P((const char *, struct afd *,
122 struct addrinfo **, char *, struct addrinfo *,
123 int));
124 static int get_addr __P((const char *, int, struct addrinfo **,
125 struct addrinfo *, int));
126 static int get_addr0 __P((const char *, int, struct addrinfo **,
127 struct addrinfo *, int));
128 static int str_isnumber __P((const char *));
129
130 static char *ai_errlist[] = {
131 "Success",
132 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
133 "Temporary failure in name resolution", /* EAI_AGAIN */
134 "Invalid value for ai_flags", /* EAI_BADFLAGS */
135 "Non-recoverable failure in name resolution", /* EAI_FAIL */
136 "ai_family not supported", /* EAI_FAMILY */
137 "Memory allocation failure", /* EAI_MEMORY */
138 "No address associated with hostname", /* EAI_NODATA */
139 "hostname nor servname provided, or not known",/* EAI_NONAME */
140 "servname not supported for ai_socktype", /* EAI_SERVICE */
141 "ai_socktype not supported", /* EAI_SOCKTYPE */
142 "System error returned in errno", /* EAI_SYSTEM */
143 "Invalid value for hints", /* EAI_BADHINTS */
144 "Resolved protocol is unknown", /* EAI_PROTOCOL */
145 "Unknown error", /* EAI_MAX */
146 };
147
148 #define GET_CANONNAME(ai, str) \
149 if (pai->ai_flags & AI_CANONNAME) {\
150 if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
151 strcpy((ai)->ai_canonname, (str));\
152 } else {\
153 error = EAI_MEMORY;\
154 goto free;\
155 }\
156 }
157
158 #define GET_AI(ai, afd, addr, port) {\
159 char *p;\
160 if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
161 ((afd)->a_socklen)))\
162 == NULL) {\
163 error = EAI_MEMORY;\
164 goto free;\
165 }\
166 memcpy(ai, pai, sizeof(struct addrinfo));\
167 (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
168 memset((ai)->ai_addr, 0, (afd)->a_socklen);\
169 (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (afd)->a_socklen;\
170 (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
171 ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
172 p = (char *)((ai)->ai_addr);\
173 memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
174 }
175
176 #define ERR(err) { error = (err); goto bad; }
177
178 char *
179 gai_strerror(ecode)
180 int ecode;
181 {
182 if (ecode < 0 || ecode > EAI_MAX)
183 ecode = EAI_MAX;
184 return ai_errlist[ecode];
185 }
186
187 void
188 freeaddrinfo(ai)
189 struct addrinfo *ai;
190 {
191 struct addrinfo *next;
192
193 do {
194 next = ai->ai_next;
195 if (ai->ai_canonname)
196 free(ai->ai_canonname);
197 /* no need to free(ai->ai_addr) */
198 free(ai);
199 } while ((ai = next) != NULL);
200 }
201
202 static int
203 str_isnumber(p)
204 const char *p;
205 {
206 char *q = (char *)p;
207 while (*q) {
208 if (! isdigit(*q))
209 return NO;
210 q++;
211 }
212 return YES;
213 }
214
215 int
216 getaddrinfo(hostname, servname, hints, res)
217 const char *hostname, *servname;
218 const struct addrinfo *hints;
219 struct addrinfo **res;
220 {
221 struct addrinfo sentinel;
222 struct addrinfo *top = NULL;
223 struct addrinfo *cur;
224 int i, error = 0;
225 char pton[PTON_MAX];
226 struct addrinfo ai;
227 struct addrinfo *pai;
228 u_short port;
229
230 #ifdef FAITH
231 static int firsttime = 1;
232
233 if (firsttime) {
234 /* translator hack */
235 {
236 char *q = getenv("GAI");
237 if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
238 translate = YES;
239 }
240 firsttime = 0;
241 }
242 #endif
243
244 /* initialize file static vars */
245 sentinel.ai_next = NULL;
246 cur = &sentinel;
247 pai = &ai;
248 pai->ai_flags = 0;
249 pai->ai_family = PF_UNSPEC;
250 pai->ai_socktype = ANY;
251 pai->ai_protocol = ANY;
252 pai->ai_addrlen = 0;
253 pai->ai_canonname = NULL;
254 pai->ai_addr = NULL;
255 pai->ai_next = NULL;
256 port = ANY;
257
258 if (hostname == NULL && servname == NULL)
259 return EAI_NONAME;
260 if (hints) {
261 /* error check for hints */
262 if (hints->ai_addrlen || hints->ai_canonname ||
263 hints->ai_addr || hints->ai_next)
264 ERR(EAI_BADHINTS); /* xxx */
265 if (hints->ai_flags & ~AI_MASK)
266 ERR(EAI_BADFLAGS);
267 switch (hints->ai_family) {
268 case PF_UNSPEC:
269 case PF_INET:
270 #ifdef INET6
271 case PF_INET6:
272 #endif
273 break;
274 default:
275 ERR(EAI_FAMILY);
276 }
277 memcpy(pai, hints, sizeof(*pai));
278 switch (pai->ai_socktype) {
279 case ANY:
280 switch (pai->ai_protocol) {
281 case ANY:
282 break;
283 case IPPROTO_UDP:
284 pai->ai_socktype = SOCK_DGRAM;
285 break;
286 case IPPROTO_TCP:
287 pai->ai_socktype = SOCK_STREAM;
288 break;
289 default:
290 pai->ai_socktype = SOCK_RAW;
291 break;
292 }
293 break;
294 case SOCK_RAW:
295 break;
296 case SOCK_DGRAM:
297 if (pai->ai_protocol != IPPROTO_UDP &&
298 pai->ai_protocol != ANY)
299 ERR(EAI_BADHINTS); /*xxx*/
300 pai->ai_protocol = IPPROTO_UDP;
301 break;
302 case SOCK_STREAM:
303 if (pai->ai_protocol != IPPROTO_TCP &&
304 pai->ai_protocol != ANY)
305 ERR(EAI_BADHINTS); /*xxx*/
306 pai->ai_protocol = IPPROTO_TCP;
307 break;
308 default:
309 ERR(EAI_SOCKTYPE);
310 break;
311 }
312 }
313
314 /*
315 * service port
316 */
317 if (servname) {
318 if (str_isnumber(servname)) {
319 if (pai->ai_socktype == ANY) {
320 /* caller accept *ANY* socktype */
321 pai->ai_socktype = SOCK_DGRAM;
322 pai->ai_protocol = IPPROTO_UDP;
323 }
324 port = htons(atoi(servname));
325 } else {
326 struct servent *sp;
327 char *proto;
328
329 proto = NULL;
330 switch (pai->ai_socktype) {
331 case ANY:
332 proto = NULL;
333 break;
334 case SOCK_DGRAM:
335 proto = "udp";
336 break;
337 case SOCK_STREAM:
338 proto = "tcp";
339 break;
340 default:
341 fprintf(stderr, "panic!\n");
342 break;
343 }
344 if ((sp = getservbyname(servname, proto)) == NULL)
345 ERR(EAI_SERVICE);
346 port = sp->s_port;
347 if (pai->ai_socktype == ANY) {
348 if (strcmp(sp->s_proto, "udp") == 0) {
349 pai->ai_socktype = SOCK_DGRAM;
350 pai->ai_protocol = IPPROTO_UDP;
351 } else if (strcmp(sp->s_proto, "tcp") == 0) {
352 pai->ai_socktype = SOCK_STREAM;
353 pai->ai_protocol = IPPROTO_TCP;
354 } else
355 ERR(EAI_PROTOCOL); /*xxx*/
356 }
357 }
358 }
359
360 /*
361 * hostname == NULL.
362 * passive socket -> anyaddr (0.0.0.0 or ::)
363 * non-passive socket -> localhost (127.0.0.1 or ::1)
364 */
365 if (hostname == NULL) {
366 struct afd *afd;
367 int s;
368
369 for (afd = &afdl[0]; afd->a_af; afd++) {
370 if (!(pai->ai_family == PF_UNSPEC
371 || pai->ai_family == afd->a_af)) {
372 continue;
373 }
374
375 /*
376 * filter out AFs that are not supported by the kernel
377 * XXX errno?
378 */
379 s = socket(afd->a_af, SOCK_DGRAM, 0);
380 if (s < 0)
381 continue;
382 close(s);
383
384 if (pai->ai_flags & AI_PASSIVE) {
385 GET_AI(cur->ai_next, afd, afd->a_addrany, port);
386 /* xxx meaningless?
387 * GET_CANONNAME(cur->ai_next, "anyaddr");
388 */
389 } else {
390 GET_AI(cur->ai_next, afd, afd->a_loopback,
391 port);
392 /* xxx meaningless?
393 * GET_CANONNAME(cur->ai_next, "localhost");
394 */
395 }
396 cur = cur->ai_next;
397 }
398 top = sentinel.ai_next;
399 if (top)
400 goto good;
401 else
402 ERR(EAI_FAMILY);
403 }
404
405 /* hostname as numeric name */
406 for (i = 0; afdl[i].a_af; i++) {
407 if (inet_pton(afdl[i].a_af, hostname, pton)) {
408 u_long v4a;
409 u_char pfx;
410
411 switch (afdl[i].a_af) {
412 case AF_INET:
413 v4a = ntohl(((struct in_addr *)pton)->s_addr);
414 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
415 pai->ai_flags &= ~AI_CANONNAME;
416 v4a >>= IN_CLASSA_NSHIFT;
417 if (v4a == 0 || v4a == IN_LOOPBACKNET)
418 pai->ai_flags &= ~AI_CANONNAME;
419 break;
420 #ifdef INET6
421 case AF_INET6:
422 pfx = ((struct in6_addr *)pton)->s6_addr8[0];
423 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
424 pai->ai_flags &= ~AI_CANONNAME;
425 break;
426 #endif
427 }
428
429 if (pai->ai_family == afdl[i].a_af ||
430 pai->ai_family == PF_UNSPEC) {
431 if (! (pai->ai_flags & AI_CANONNAME)) {
432 GET_AI(top, &afdl[i], pton, port);
433 goto good;
434 }
435 /*
436 * if AI_CANONNAME and if reverse lookup
437 * fail, return ai anyway to pacify
438 * calling application.
439 *
440 * XXX getaddrinfo() is a name->address
441 * translation function, and it looks strange
442 * that we do addr->name translation here.
443 */
444 get_name(pton, &afdl[i], &top, pton, pai, port);
445 goto good;
446 } else
447 ERR(EAI_FAMILY); /*xxx*/
448 }
449 }
450
451 if (pai->ai_flags & AI_NUMERICHOST)
452 ERR(EAI_NONAME);
453
454 /* hostname as alphabetical name */
455 error = get_addr(hostname, pai->ai_family, &top, pai, port);
456 if (error == 0) {
457 if (top) {
458 good:
459 *res = top;
460 return SUCCESS;
461 } else
462 error = EAI_FAIL;
463 }
464 free:
465 if (top)
466 freeaddrinfo(top);
467 bad:
468 *res = NULL;
469 return error;
470 }
471
472 static int
473 get_name(addr, afd, res, numaddr, pai, port0)
474 const char *addr;
475 struct afd *afd;
476 struct addrinfo **res;
477 char *numaddr;
478 struct addrinfo *pai;
479 int port0;
480 {
481 u_short port = port0 & 0xffff;
482 struct hostent *hp;
483 struct addrinfo *cur;
484 int error = 0;
485 #ifdef USE_GETIPNODEBY
486 int h_error;
487
488 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
489 #else
490 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
491 #endif
492 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
493 GET_AI(cur, afd, hp->h_addr_list[0], port);
494 GET_CANONNAME(cur, hp->h_name);
495 } else
496 GET_AI(cur, afd, numaddr, port);
497
498 #ifdef USE_GETIPNODEBY
499 if (hp)
500 freehostent(hp);
501 #endif
502 *res = cur;
503 return SUCCESS;
504 free:
505 if (cur)
506 freeaddrinfo(cur);
507 #ifdef USE_GETIPNODEBY
508 if (hp)
509 freehostent(hp);
510 #endif
511 /* bad: */
512 *res = NULL;
513 return error;
514 }
515
516 static int
517 get_addr(hostname, af, res0, pai, port0)
518 const char *hostname;
519 int af;
520 struct addrinfo **res0;
521 struct addrinfo *pai;
522 int port0;
523 {
524 #ifdef USE_GETIPNODEBY
525 return get_addr0(hostname, af, res0, pai, port0);
526 #else
527 int i, error, ekeep;
528 struct addrinfo *cur;
529 struct addrinfo **res;
530
531 res = res0;
532 ekeep = 0;
533 for (i = 0; afdl[i].a_af; i++) {
534 if (af == AF_UNSPEC || af == afdl[i].a_af)
535 ;
536 else
537 continue;
538 /* It is WRONG, we need getipnodebyname(). */
539 again:
540 error = get_addr0(hostname, afdl[i].a_af, res, pai, port0);
541 switch (error) {
542 case EAI_AGAIN:
543 goto again;
544 case EAI_FAIL:
545 return error;
546 default:
547 if (ekeep == 0)
548 ekeep = error;
549 break;
550 }
551 if (*res) {
552 /* make chain of addrs */
553 for (cur = *res;
554 cur && cur->ai_next;
555 cur = cur->ai_next)
556 ;
557 if (!cur)
558 return EAI_FAIL;
559 res = &cur->ai_next;
560 }
561 }
562
563 /* if we got something, it's okay */
564 if (*res0)
565 return 0;
566
567 return error ? error : ekeep;
568 #endif
569 }
570
571 static int
572 get_addr0(hostname, af, res, pai, port0)
573 const char *hostname;
574 int af;
575 struct addrinfo **res;
576 struct addrinfo *pai;
577 int port0;
578 {
579 u_short port = port0 & 0xffff;
580 struct addrinfo sentinel;
581 struct hostent *hp;
582 struct addrinfo *top, *cur;
583 struct afd *afd;
584 int i, error = 0, h_error;
585 char *ap;
586 #ifndef USE_GETIPNODEBY
587 extern int h_errno;
588 #endif
589
590 top = NULL;
591 sentinel.ai_next = NULL;
592 cur = &sentinel;
593 #ifdef USE_GETIPNODEBY
594 if (af == AF_UNSPEC) {
595 hp = getipnodebyname(hostname, AF_INET6,
596 AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
597 } else
598 hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
599 #else
600 if (af == AF_UNSPEC) {
601 error = EAI_FAIL;
602 goto bad;
603 }
604 hp = gethostbyname2(hostname, af);
605 h_error = h_errno;
606 #endif
607 if (hp == NULL) {
608 switch (h_error) {
609 case HOST_NOT_FOUND:
610 case NO_DATA:
611 error = EAI_NODATA;
612 break;
613 case TRY_AGAIN:
614 error = EAI_AGAIN;
615 break;
616 case NO_RECOVERY:
617 default:
618 error = EAI_FAIL;
619 break;
620 }
621 goto bad;
622 }
623
624 if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
625 (hp->h_addr_list[0] == NULL))
626 ERR(EAI_FAIL);
627
628 for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
629 switch (af) {
630 #ifdef INET6
631 case AF_INET6:
632 afd = &afdl[N_INET6];
633 break;
634 #endif
635 #ifndef INET6
636 default: /* AF_UNSPEC */
637 #endif
638 case AF_INET:
639 afd = &afdl[N_INET];
640 break;
641 #ifdef INET6
642 default: /* AF_UNSPEC */
643 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
644 ap += sizeof(struct in6_addr) -
645 sizeof(struct in_addr);
646 afd = &afdl[N_INET];
647 } else
648 afd = &afdl[N_INET6];
649 break;
650 #endif
651 }
652 #ifdef FAITH
653 if (translate && afd->a_af == AF_INET) {
654 struct in6_addr *in6;
655
656 GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
657 in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
658 memcpy(&in6->s6_addr32[0], &faith_prefix,
659 sizeof(struct in6_addr) - sizeof(struct in_addr));
660 memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
661 } else
662 #endif /* FAITH */
663 GET_AI(cur->ai_next, afd, ap, port);
664 if (cur == &sentinel) {
665 top = cur->ai_next;
666 GET_CANONNAME(top, hp->h_name);
667 }
668 cur = cur->ai_next;
669 }
670 #ifdef USE_GETIPNODEBY
671 freehostent(hp);
672 #endif
673 *res = top;
674 return SUCCESS;
675 free:
676 if (top)
677 freeaddrinfo(top);
678 #ifdef USE_GETIPNODEBY
679 if (hp)
680 freehostent(hp);
681 #endif
682 bad:
683 *res = NULL;
684 return error;
685 }
686