Import bind-9.3.4
[dragonfly.git] / contrib / bind-9.3 / lib / lwres / getaddrinfo.c
blob9ad10dfd7eb39c4524370742b45a09a06fdcffa7
1 /*
2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * This code is derived from software contributed to ISC by
6 * Berkeley Software Design, Inc.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND BERKELEY SOFTWARE DESIGN, INC.
13 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
15 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
18 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 /* $Id: getaddrinfo.c,v 1.41.206.6 2006/11/13 11:57:41 marka Exp $ */
23 #include <config.h>
25 #include <string.h>
26 #include <errno.h>
28 #include <lwres/lwres.h>
29 #include <lwres/net.h>
30 #include <lwres/netdb.h>
31 #include <lwres/stdlib.h>
33 #define SA(addr) ((struct sockaddr *)(addr))
34 #define SIN(addr) ((struct sockaddr_in *)(addr))
35 #define SIN6(addr) ((struct sockaddr_in6 *)(addr))
36 #define SUN(addr) ((struct sockaddr_un *)(addr))
38 static struct addrinfo
39 *ai_reverse(struct addrinfo *oai),
40 *ai_clone(struct addrinfo *oai, int family),
41 *ai_alloc(int family, int addrlen);
42 #ifdef AF_LOCAL
43 static int get_local(const char *name, int socktype, struct addrinfo **res);
44 #endif
46 static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
47 int socktype, int port);
48 static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
49 int socktype, int port);
50 static void set_order(int, int (**)(const char *, int, struct addrinfo **,
51 int, int));
53 #define FOUND_IPV4 0x1
54 #define FOUND_IPV6 0x2
55 #define FOUND_MAX 2
57 #define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
59 int
60 lwres_getaddrinfo(const char *hostname, const char *servname,
61 const struct addrinfo *hints, struct addrinfo **res)
63 struct servent *sp;
64 const char *proto;
65 int family, socktype, flags, protocol;
66 struct addrinfo *ai, *ai_list;
67 int port, err, i;
68 int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
69 int, int);
71 if (hostname == NULL && servname == NULL)
72 return (EAI_NONAME);
74 proto = NULL;
75 if (hints != NULL) {
76 if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
77 return (EAI_BADFLAGS);
78 if (hints->ai_addrlen || hints->ai_canonname ||
79 hints->ai_addr || hints->ai_next) {
80 errno = EINVAL;
81 return (EAI_SYSTEM);
83 family = hints->ai_family;
84 socktype = hints->ai_socktype;
85 protocol = hints->ai_protocol;
86 flags = hints->ai_flags;
87 switch (family) {
88 case AF_UNSPEC:
89 switch (hints->ai_socktype) {
90 case SOCK_STREAM:
91 proto = "tcp";
92 break;
93 case SOCK_DGRAM:
94 proto = "udp";
95 break;
97 break;
98 case AF_INET:
99 case AF_INET6:
100 switch (hints->ai_socktype) {
101 case 0:
102 break;
103 case SOCK_STREAM:
104 proto = "tcp";
105 break;
106 case SOCK_DGRAM:
107 proto = "udp";
108 break;
109 case SOCK_RAW:
110 break;
111 default:
112 return (EAI_SOCKTYPE);
114 break;
115 #ifdef AF_LOCAL
116 case AF_LOCAL:
117 switch (hints->ai_socktype) {
118 case 0:
119 break;
120 case SOCK_STREAM:
121 break;
122 case SOCK_DGRAM:
123 break;
124 default:
125 return (EAI_SOCKTYPE);
127 break;
128 #endif
129 default:
130 return (EAI_FAMILY);
132 } else {
133 protocol = 0;
134 family = 0;
135 socktype = 0;
136 flags = 0;
139 #ifdef AF_LOCAL
141 * First, deal with AF_LOCAL. If the family was not set,
142 * then assume AF_LOCAL if the first character of the
143 * hostname/servname is '/'.
146 if (hostname != NULL &&
147 (family == AF_LOCAL || (family == 0 && *hostname == '/')))
148 return (get_local(hostname, socktype, res));
150 if (servname != NULL &&
151 (family == AF_LOCAL || (family == 0 && *servname == '/')))
152 return (get_local(servname, socktype, res));
153 #endif
156 * Ok, only AF_INET and AF_INET6 left.
158 ai_list = NULL;
161 * First, look up the service name (port) if it was
162 * requested. If the socket type wasn't specified, then
163 * try and figure it out.
165 if (servname != NULL) {
166 char *e;
168 port = strtol(servname, &e, 10);
169 if (*e == '\0') {
170 if (socktype == 0)
171 return (EAI_SOCKTYPE);
172 if (port < 0 || port > 65535)
173 return (EAI_SERVICE);
174 port = htons((unsigned short) port);
175 } else {
176 sp = getservbyname(servname, proto);
177 if (sp == NULL)
178 return (EAI_SERVICE);
179 port = sp->s_port;
180 if (socktype == 0) {
181 if (strcmp(sp->s_proto, "tcp") == 0)
182 socktype = SOCK_STREAM;
183 else if (strcmp(sp->s_proto, "udp") == 0)
184 socktype = SOCK_DGRAM;
187 } else
188 port = 0;
191 * Next, deal with just a service name, and no hostname.
192 * (we verified that one of them was non-null up above).
194 if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
195 if (family == AF_INET || family == 0) {
196 ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
197 if (ai == NULL)
198 return (EAI_MEMORY);
199 ai->ai_socktype = socktype;
200 ai->ai_protocol = protocol;
201 SIN(ai->ai_addr)->sin_port = port;
202 ai->ai_next = ai_list;
203 ai_list = ai;
206 if (family == AF_INET6 || family == 0) {
207 ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
208 if (ai == NULL) {
209 lwres_freeaddrinfo(ai_list);
210 return (EAI_MEMORY);
212 ai->ai_socktype = socktype;
213 ai->ai_protocol = protocol;
214 SIN6(ai->ai_addr)->sin6_port = port;
215 ai->ai_next = ai_list;
216 ai_list = ai;
219 *res = ai_list;
220 return (0);
224 * If the family isn't specified or AI_NUMERICHOST specified,
225 * check first to see if it is a numeric address.
226 * Though the gethostbyname2() routine
227 * will recognize numeric addresses, it will only recognize
228 * the format that it is being called for. Thus, a numeric
229 * AF_INET address will be treated by the AF_INET6 call as
230 * a domain name, and vice versa. Checking for both numerics
231 * here avoids that.
233 if (hostname != NULL &&
234 (family == 0 || (flags & AI_NUMERICHOST) != 0)) {
235 char abuf[sizeof(struct in6_addr)];
236 char nbuf[NI_MAXHOST];
237 int addrsize, addroff;
238 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
239 char *p, *ep;
240 char ntmp[NI_MAXHOST];
241 lwres_uint32_t scopeid;
242 #endif
244 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
246 * Scope identifier portion.
248 ntmp[0] = '\0';
249 if (strchr(hostname, '%') != NULL) {
250 strncpy(ntmp, hostname, sizeof(ntmp) - 1);
251 ntmp[sizeof(ntmp) - 1] = '\0';
252 p = strchr(ntmp, '%');
253 ep = NULL;
256 * Vendors may want to support non-numeric
257 * scopeid around here.
260 if (p != NULL)
261 scopeid = (lwres_uint32_t)strtoul(p + 1,
262 &ep, 10);
263 if (p != NULL && ep != NULL && ep[0] == '\0')
264 *p = '\0';
265 else {
266 ntmp[0] = '\0';
267 scopeid = 0;
269 } else
270 scopeid = 0;
271 #endif
273 if (lwres_net_pton(AF_INET, hostname, (struct in_addr *)abuf)
274 == 1)
276 if (family == AF_INET6) {
278 * Convert to a V4 mapped address.
280 struct in6_addr *a6 = (struct in6_addr *)abuf;
281 memcpy(&a6->s6_addr[12], &a6->s6_addr[0], 4);
282 memset(&a6->s6_addr[10], 0xff, 2);
283 memset(&a6->s6_addr[0], 0, 10);
284 goto inet6_addr;
286 addrsize = sizeof(struct in_addr);
287 addroff = (char *)(&SIN(0)->sin_addr) - (char *)0;
288 family = AF_INET;
289 goto common;
290 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
291 } else if (ntmp[0] != '\0' &&
292 lwres_net_pton(AF_INET6, ntmp, abuf) == 1)
294 if (family && family != AF_INET6)
295 return (EAI_NONAME);
296 addrsize = sizeof(struct in6_addr);
297 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
298 family = AF_INET6;
299 goto common;
300 #endif
301 } else if (lwres_net_pton(AF_INET6, hostname, abuf) == 1) {
302 if (family != 0 && family != AF_INET6)
303 return (EAI_NONAME);
304 inet6_addr:
305 addrsize = sizeof(struct in6_addr);
306 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
307 family = AF_INET6;
309 common:
310 ai = ai_clone(ai_list, family);
311 if (ai == NULL)
312 return (EAI_MEMORY);
313 ai_list = ai;
314 ai->ai_socktype = socktype;
315 SIN(ai->ai_addr)->sin_port = port;
316 memcpy((char *)ai->ai_addr + addroff, abuf, addrsize);
317 if (flags & AI_CANONNAME) {
318 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
319 if (ai->ai_family == AF_INET6)
320 SIN6(ai->ai_addr)->sin6_scope_id =
321 scopeid;
322 #endif
323 if (lwres_getnameinfo(ai->ai_addr,
324 ai->ai_addrlen, nbuf, sizeof(nbuf),
325 NULL, 0,
326 NI_NUMERICHOST) == 0) {
327 ai->ai_canonname = strdup(nbuf);
328 if (ai->ai_canonname == NULL) {
329 lwres_freeaddrinfo(ai_list);
330 return (EAI_MEMORY);
332 } else {
333 /* XXX raise error? */
334 ai->ai_canonname = NULL;
337 goto done;
338 } else if ((flags & AI_NUMERICHOST) != 0) {
339 return (EAI_NONAME);
343 set_order(family, net_order);
344 for (i = 0; i < FOUND_MAX; i++) {
345 if (net_order[i] == NULL)
346 break;
347 err = (net_order[i])(hostname, flags, &ai_list,
348 socktype, port);
349 if (err != 0)
350 return (err);
353 if (ai_list == NULL)
354 return (EAI_NODATA);
356 done:
357 ai_list = ai_reverse(ai_list);
359 *res = ai_list;
360 return (0);
363 static char *
364 lwres_strsep(char **stringp, const char *delim) {
365 char *string = *stringp;
366 char *s;
367 const char *d;
368 char sc, dc;
370 if (string == NULL)
371 return (NULL);
373 for (s = string; *s != '\0'; s++) {
374 sc = *s;
375 for (d = delim; (dc = *d) != '\0'; d++)
376 if (sc == dc) {
377 *s++ = '\0';
378 *stringp = s;
379 return (string);
382 *stringp = NULL;
383 return (string);
386 static void
387 set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
388 int, int))
390 char *order, *tok;
391 int found;
393 if (family) {
394 switch (family) {
395 case AF_INET:
396 *net_order++ = add_ipv4;
397 break;
398 case AF_INET6:
399 *net_order++ = add_ipv6;
400 break;
402 } else {
403 order = getenv("NET_ORDER");
404 found = 0;
405 while (order != NULL) {
407 * We ignore any unknown names.
409 tok = lwres_strsep(&order, ":");
410 if (strcasecmp(tok, "inet6") == 0) {
411 if ((found & FOUND_IPV6) == 0)
412 *net_order++ = add_ipv6;
413 found |= FOUND_IPV6;
414 } else if (strcasecmp(tok, "inet") == 0 ||
415 strcasecmp(tok, "inet4") == 0) {
416 if ((found & FOUND_IPV4) == 0)
417 *net_order++ = add_ipv4;
418 found |= FOUND_IPV4;
423 * Add in anything that we didn't find.
425 if ((found & FOUND_IPV4) == 0)
426 *net_order++ = add_ipv4;
427 if ((found & FOUND_IPV6) == 0)
428 *net_order++ = add_ipv6;
430 *net_order = NULL;
431 return;
434 static char v4_loop[4] = { 127, 0, 0, 1 };
437 * The test against 0 is there to keep the Solaris compiler
438 * from complaining about "end-of-loop code not reached".
440 #define SETERROR(code) \
441 do { result = (code); \
442 if (result != 0) goto cleanup; \
443 } while (0)
445 static int
446 add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
447 int socktype, int port)
449 struct addrinfo *ai;
450 lwres_context_t *lwrctx = NULL;
451 lwres_gabnresponse_t *by = NULL;
452 lwres_addr_t *addr;
453 lwres_result_t lwres;
454 int result = 0;
456 lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
457 if (lwres != LWRES_R_SUCCESS)
458 SETERROR(EAI_FAIL);
459 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
460 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
461 ai = ai_clone(*aip, AF_INET);
462 if (ai == NULL) {
463 lwres_freeaddrinfo(*aip);
464 SETERROR(EAI_MEMORY);
467 *aip = ai;
468 ai->ai_socktype = socktype;
469 SIN(ai->ai_addr)->sin_port = port;
470 memcpy(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
471 } else {
472 lwres = lwres_getaddrsbyname(lwrctx, hostname,
473 LWRES_ADDRTYPE_V4, &by);
474 if (lwres != LWRES_R_SUCCESS) {
475 if (lwres == LWRES_R_NOTFOUND)
476 goto cleanup;
477 else
478 SETERROR(EAI_FAIL);
480 addr = LWRES_LIST_HEAD(by->addrs);
481 while (addr != NULL) {
482 ai = ai_clone(*aip, AF_INET);
483 if (ai == NULL) {
484 lwres_freeaddrinfo(*aip);
485 SETERROR(EAI_MEMORY);
487 *aip = ai;
488 ai->ai_socktype = socktype;
489 SIN(ai->ai_addr)->sin_port = port;
490 memcpy(&SIN(ai->ai_addr)->sin_addr,
491 addr->address, 4);
492 if (flags & AI_CANONNAME) {
493 ai->ai_canonname = strdup(by->realname);
494 if (ai->ai_canonname == NULL)
495 SETERROR(EAI_MEMORY);
497 addr = LWRES_LIST_NEXT(addr, link);
500 cleanup:
501 if (by != NULL)
502 lwres_gabnresponse_free(lwrctx, &by);
503 if (lwrctx != NULL) {
504 lwres_conf_clear(lwrctx);
505 lwres_context_destroy(&lwrctx);
507 return (result);
510 static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
512 static int
513 add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
514 int socktype, int port)
516 struct addrinfo *ai;
517 lwres_context_t *lwrctx = NULL;
518 lwres_gabnresponse_t *by = NULL;
519 lwres_addr_t *addr;
520 lwres_result_t lwres;
521 int result = 0;
523 lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
524 if (lwres != LWRES_R_SUCCESS)
525 SETERROR(EAI_FAIL);
526 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
528 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
529 ai = ai_clone(*aip, AF_INET6);
530 if (ai == NULL) {
531 lwres_freeaddrinfo(*aip);
532 SETERROR(EAI_MEMORY);
535 *aip = ai;
536 ai->ai_socktype = socktype;
537 SIN6(ai->ai_addr)->sin6_port = port;
538 memcpy(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
539 } else {
540 lwres = lwres_getaddrsbyname(lwrctx, hostname,
541 LWRES_ADDRTYPE_V6, &by);
542 if (lwres != LWRES_R_SUCCESS) {
543 if (lwres == LWRES_R_NOTFOUND)
544 goto cleanup;
545 else
546 SETERROR(EAI_FAIL);
548 addr = LWRES_LIST_HEAD(by->addrs);
549 while (addr != NULL) {
550 ai = ai_clone(*aip, AF_INET6);
551 if (ai == NULL) {
552 lwres_freeaddrinfo(*aip);
553 SETERROR(EAI_MEMORY);
555 *aip = ai;
556 ai->ai_socktype = socktype;
557 SIN6(ai->ai_addr)->sin6_port = port;
558 memcpy(&SIN6(ai->ai_addr)->sin6_addr,
559 addr->address, 16);
560 if (flags & AI_CANONNAME) {
561 ai->ai_canonname = strdup(by->realname);
562 if (ai->ai_canonname == NULL)
563 SETERROR(EAI_MEMORY);
565 addr = LWRES_LIST_NEXT(addr, link);
568 cleanup:
569 if (by != NULL)
570 lwres_gabnresponse_free(lwrctx, &by);
571 if (lwrctx != NULL) {
572 lwres_conf_clear(lwrctx);
573 lwres_context_destroy(&lwrctx);
575 return (result);
578 void
579 lwres_freeaddrinfo(struct addrinfo *ai) {
580 struct addrinfo *ai_next;
582 while (ai != NULL) {
583 ai_next = ai->ai_next;
584 if (ai->ai_addr != NULL)
585 free(ai->ai_addr);
586 if (ai->ai_canonname)
587 free(ai->ai_canonname);
588 free(ai);
589 ai = ai_next;
593 #ifdef AF_LOCAL
594 static int
595 get_local(const char *name, int socktype, struct addrinfo **res) {
596 struct addrinfo *ai;
597 struct sockaddr_un *sun;
599 if (socktype == 0)
600 return (EAI_SOCKTYPE);
602 ai = ai_alloc(AF_LOCAL, sizeof(*sun));
603 if (ai == NULL)
604 return (EAI_MEMORY);
606 sun = SUN(ai->ai_addr);
607 strncpy(sun->sun_path, name, sizeof(sun->sun_path));
609 ai->ai_socktype = socktype;
611 * ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
612 * and ai->ai_next were initialized to zero.
615 *res = ai;
616 return (0);
618 #endif
621 * Allocate an addrinfo structure, and a sockaddr structure
622 * of the specificed length. We initialize:
623 * ai_addrlen
624 * ai_family
625 * ai_addr
626 * ai_addr->sa_family
627 * ai_addr->sa_len (LWRES_PLATFORM_HAVESALEN)
628 * and everything else is initialized to zero.
630 static struct addrinfo *
631 ai_alloc(int family, int addrlen) {
632 struct addrinfo *ai;
634 ai = (struct addrinfo *)calloc(1, sizeof(*ai));
635 if (ai == NULL)
636 return (NULL);
638 ai->ai_addr = SA(calloc(1, addrlen));
639 if (ai->ai_addr == NULL) {
640 free(ai);
641 return (NULL);
643 ai->ai_addrlen = addrlen;
644 ai->ai_family = family;
645 ai->ai_addr->sa_family = family;
646 #ifdef LWRES_PLATFORM_HAVESALEN
647 ai->ai_addr->sa_len = addrlen;
648 #endif
649 return (ai);
652 static struct addrinfo *
653 ai_clone(struct addrinfo *oai, int family) {
654 struct addrinfo *ai;
656 ai = ai_alloc(family, ((family == AF_INET6) ?
657 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
659 if (ai == NULL) {
660 lwres_freeaddrinfo(oai);
661 return (NULL);
663 if (oai == NULL)
664 return (ai);
666 ai->ai_flags = oai->ai_flags;
667 ai->ai_socktype = oai->ai_socktype;
668 ai->ai_protocol = oai->ai_protocol;
669 ai->ai_canonname = NULL;
670 ai->ai_next = oai;
671 return (ai);
674 static struct addrinfo *
675 ai_reverse(struct addrinfo *oai) {
676 struct addrinfo *nai, *tai;
678 nai = NULL;
680 while (oai != NULL) {
682 * Grab one off the old list.
684 tai = oai;
685 oai = oai->ai_next;
687 * Put it on the front of the new list.
689 tai->ai_next = nai;
690 nai = tai;
692 return (nai);