2 * Copyright (C) 2004-2007 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/or 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.8 2007/09/13 23:45:58 tbox Exp $ */
27 #include <isc/string.h>
29 #include <lwres/lwres.h>
30 #include <lwres/net.h>
31 #include <lwres/netdb.h>
32 #include <lwres/stdlib.h>
34 #define SA(addr) ((struct sockaddr *)(addr))
35 #define SIN(addr) ((struct sockaddr_in *)(addr))
36 #define SIN6(addr) ((struct sockaddr_in6 *)(addr))
37 #define SUN(addr) ((struct sockaddr_un *)(addr))
39 static struct addrinfo
40 *ai_reverse(struct addrinfo *oai),
41 *ai_clone(struct addrinfo *oai, int family),
42 *ai_alloc(int family, int addrlen);
44 static int get_local(const char *name, int socktype, struct addrinfo **res);
47 static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
48 int socktype, int port);
49 static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
50 int socktype, int port);
51 static void set_order(int, int (**)(const char *, int, struct addrinfo **,
54 #define FOUND_IPV4 0x1
55 #define FOUND_IPV6 0x2
58 #define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
61 lwres_getaddrinfo(const char *hostname, const char *servname,
62 const struct addrinfo *hints, struct addrinfo **res)
66 int family, socktype, flags, protocol;
67 struct addrinfo *ai, *ai_list;
69 int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
72 if (hostname == NULL && servname == NULL)
77 if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
78 return (EAI_BADFLAGS);
79 if (hints->ai_addrlen || hints->ai_canonname ||
80 hints->ai_addr || hints->ai_next) {
84 family = hints->ai_family;
85 socktype = hints->ai_socktype;
86 protocol = hints->ai_protocol;
87 flags = hints->ai_flags;
90 switch (hints->ai_socktype) {
101 switch (hints->ai_socktype) {
113 return (EAI_SOCKTYPE);
118 switch (hints->ai_socktype) {
126 return (EAI_SOCKTYPE);
142 * First, deal with AF_LOCAL. If the family was not set,
143 * then assume AF_LOCAL if the first character of the
144 * hostname/servname is '/'.
147 if (hostname != NULL &&
148 (family == AF_LOCAL || (family == 0 && *hostname == '/')))
149 return (get_local(hostname, socktype, res));
151 if (servname != NULL &&
152 (family == AF_LOCAL || (family == 0 && *servname == '/')))
153 return (get_local(servname, socktype, res));
157 * Ok, only AF_INET and AF_INET6 left.
162 * First, look up the service name (port) if it was
163 * requested. If the socket type wasn't specified, then
164 * try and figure it out.
166 if (servname != NULL) {
169 port = strtol(servname, &e, 10);
172 return (EAI_SOCKTYPE);
173 if (port < 0 || port > 65535)
174 return (EAI_SERVICE);
175 port = htons((unsigned short) port);
177 sp = getservbyname(servname, proto);
179 return (EAI_SERVICE);
182 if (strcmp(sp->s_proto, "tcp") == 0)
183 socktype = SOCK_STREAM;
184 else if (strcmp(sp->s_proto, "udp") == 0)
185 socktype = SOCK_DGRAM;
192 * Next, deal with just a service name, and no hostname.
193 * (we verified that one of them was non-null up above).
195 if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
196 if (family == AF_INET || family == 0) {
197 ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
200 ai->ai_socktype = socktype;
201 ai->ai_protocol = protocol;
202 SIN(ai->ai_addr)->sin_port = port;
203 ai->ai_next = ai_list;
207 if (family == AF_INET6 || family == 0) {
208 ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
210 lwres_freeaddrinfo(ai_list);
213 ai->ai_socktype = socktype;
214 ai->ai_protocol = protocol;
215 SIN6(ai->ai_addr)->sin6_port = port;
216 ai->ai_next = ai_list;
225 * If the family isn't specified or AI_NUMERICHOST specified,
226 * check first to see if it is a numeric address.
227 * Though the gethostbyname2() routine
228 * will recognize numeric addresses, it will only recognize
229 * the format that it is being called for. Thus, a numeric
230 * AF_INET address will be treated by the AF_INET6 call as
231 * a domain name, and vice versa. Checking for both numerics
234 if (hostname != NULL &&
235 (family == 0 || (flags & AI_NUMERICHOST) != 0)) {
236 char abuf[sizeof(struct in6_addr)];
237 char nbuf[NI_MAXHOST];
238 int addrsize, addroff;
239 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
241 char ntmp[NI_MAXHOST];
242 lwres_uint32_t scopeid;
245 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
247 * Scope identifier portion.
250 if (strchr(hostname, '%') != NULL) {
251 strncpy(ntmp, hostname, sizeof(ntmp) - 1);
252 ntmp[sizeof(ntmp) - 1] = '\0';
253 p = strchr(ntmp, '%');
257 * Vendors may want to support non-numeric
258 * scopeid around here.
262 scopeid = (lwres_uint32_t)strtoul(p + 1,
264 if (p != NULL && ep != NULL && ep[0] == '\0')
274 if (lwres_net_pton(AF_INET, hostname, (struct in_addr *)abuf)
277 if (family == AF_INET6) {
279 * Convert to a V4 mapped address.
281 struct in6_addr *a6 = (struct in6_addr *)abuf;
282 memcpy(&a6->s6_addr[12], &a6->s6_addr[0], 4);
283 memset(&a6->s6_addr[10], 0xff, 2);
284 memset(&a6->s6_addr[0], 0, 10);
287 addrsize = sizeof(struct in_addr);
288 addroff = (char *)(&SIN(0)->sin_addr) - (char *)0;
291 #ifdef LWRES_HAVE_SIN6_SCOPE_ID
292 } else if (ntmp[0] != '\0' &&
293 lwres_net_pton(AF_INET6, ntmp, abuf) == 1)
295 if (family && family != AF_INET6)
297 addrsize = sizeof(struct in6_addr);
298 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
302 } else if (lwres_net_pton(AF_INET6, hostname, abuf) == 1) {
303 if (family != 0 && family != AF_INET6)
306 addrsize = sizeof(struct in6_addr);
307 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
311 ai = ai_clone(ai_list, family);
315 ai->ai_socktype = socktype;
316 SIN(ai->ai_addr)->sin_port = port;
317 memcpy((char *)ai->ai_addr + addroff, abuf, addrsize);
318 if (flags & AI_CANONNAME) {
319 #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
320 if (ai->ai_family == AF_INET6)
321 SIN6(ai->ai_addr)->sin6_scope_id =
324 if (lwres_getnameinfo(ai->ai_addr,
325 ai->ai_addrlen, nbuf, sizeof(nbuf),
327 NI_NUMERICHOST) == 0) {
328 ai->ai_canonname = strdup(nbuf);
329 if (ai->ai_canonname == NULL) {
330 lwres_freeaddrinfo(ai_list);
334 /* XXX raise error? */
335 ai->ai_canonname = NULL;
339 } else if ((flags & AI_NUMERICHOST) != 0) {
344 set_order(family, net_order);
345 for (i = 0; i < FOUND_MAX; i++) {
346 if (net_order[i] == NULL)
348 err = (net_order[i])(hostname, flags, &ai_list,
358 ai_list = ai_reverse(ai_list);
365 lwres_strsep(char **stringp, const char *delim) {
366 char *string = *stringp;
374 for (s = string; *s != '\0'; s++) {
376 for (d = delim; (dc = *d) != '\0'; d++)
388 set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
397 *net_order++ = add_ipv4;
400 *net_order++ = add_ipv6;
404 order = getenv("NET_ORDER");
406 while (order != NULL) {
408 * We ignore any unknown names.
410 tok = lwres_strsep(&order, ":");
411 if (strcasecmp(tok, "inet6") == 0) {
412 if ((found & FOUND_IPV6) == 0)
413 *net_order++ = add_ipv6;
415 } else if (strcasecmp(tok, "inet") == 0 ||
416 strcasecmp(tok, "inet4") == 0) {
417 if ((found & FOUND_IPV4) == 0)
418 *net_order++ = add_ipv4;
424 * Add in anything that we didn't find.
426 if ((found & FOUND_IPV4) == 0)
427 *net_order++ = add_ipv4;
428 if ((found & FOUND_IPV6) == 0)
429 *net_order++ = add_ipv6;
435 static char v4_loop[4] = { 127, 0, 0, 1 };
438 * The test against 0 is there to keep the Solaris compiler
439 * from complaining about "end-of-loop code not reached".
441 #define SETERROR(code) \
442 do { result = (code); \
443 if (result != 0) goto cleanup; \
447 add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
448 int socktype, int port)
451 lwres_context_t *lwrctx = NULL;
452 lwres_gabnresponse_t *by = NULL;
454 lwres_result_t lwres;
457 lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
458 if (lwres != LWRES_R_SUCCESS)
460 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
461 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
462 ai = ai_clone(*aip, AF_INET);
464 lwres_freeaddrinfo(*aip);
465 SETERROR(EAI_MEMORY);
469 ai->ai_socktype = socktype;
470 SIN(ai->ai_addr)->sin_port = port;
471 memcpy(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
473 lwres = lwres_getaddrsbyname(lwrctx, hostname,
474 LWRES_ADDRTYPE_V4, &by);
475 if (lwres != LWRES_R_SUCCESS) {
476 if (lwres == LWRES_R_NOTFOUND)
481 addr = LWRES_LIST_HEAD(by->addrs);
482 while (addr != NULL) {
483 ai = ai_clone(*aip, AF_INET);
485 lwres_freeaddrinfo(*aip);
486 SETERROR(EAI_MEMORY);
489 ai->ai_socktype = socktype;
490 SIN(ai->ai_addr)->sin_port = port;
491 memcpy(&SIN(ai->ai_addr)->sin_addr,
493 if (flags & AI_CANONNAME) {
494 ai->ai_canonname = strdup(by->realname);
495 if (ai->ai_canonname == NULL)
496 SETERROR(EAI_MEMORY);
498 addr = LWRES_LIST_NEXT(addr, link);
503 lwres_gabnresponse_free(lwrctx, &by);
504 if (lwrctx != NULL) {
505 lwres_conf_clear(lwrctx);
506 lwres_context_destroy(&lwrctx);
511 static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
514 add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
515 int socktype, int port)
518 lwres_context_t *lwrctx = NULL;
519 lwres_gabnresponse_t *by = NULL;
521 lwres_result_t lwres;
524 lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
525 if (lwres != LWRES_R_SUCCESS)
527 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
529 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
530 ai = ai_clone(*aip, AF_INET6);
532 lwres_freeaddrinfo(*aip);
533 SETERROR(EAI_MEMORY);
537 ai->ai_socktype = socktype;
538 SIN6(ai->ai_addr)->sin6_port = port;
539 memcpy(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
541 lwres = lwres_getaddrsbyname(lwrctx, hostname,
542 LWRES_ADDRTYPE_V6, &by);
543 if (lwres != LWRES_R_SUCCESS) {
544 if (lwres == LWRES_R_NOTFOUND)
549 addr = LWRES_LIST_HEAD(by->addrs);
550 while (addr != NULL) {
551 ai = ai_clone(*aip, AF_INET6);
553 lwres_freeaddrinfo(*aip);
554 SETERROR(EAI_MEMORY);
557 ai->ai_socktype = socktype;
558 SIN6(ai->ai_addr)->sin6_port = port;
559 memcpy(&SIN6(ai->ai_addr)->sin6_addr,
561 if (flags & AI_CANONNAME) {
562 ai->ai_canonname = strdup(by->realname);
563 if (ai->ai_canonname == NULL)
564 SETERROR(EAI_MEMORY);
566 addr = LWRES_LIST_NEXT(addr, link);
571 lwres_gabnresponse_free(lwrctx, &by);
572 if (lwrctx != NULL) {
573 lwres_conf_clear(lwrctx);
574 lwres_context_destroy(&lwrctx);
580 lwres_freeaddrinfo(struct addrinfo *ai) {
581 struct addrinfo *ai_next;
584 ai_next = ai->ai_next;
585 if (ai->ai_addr != NULL)
587 if (ai->ai_canonname)
588 free(ai->ai_canonname);
596 get_local(const char *name, int socktype, struct addrinfo **res) {
598 struct sockaddr_un *sun;
601 return (EAI_SOCKTYPE);
603 ai = ai_alloc(AF_LOCAL, sizeof(*sun));
607 sun = SUN(ai->ai_addr);
608 strncpy(sun->sun_path, name, sizeof(sun->sun_path));
610 ai->ai_socktype = socktype;
612 * ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
613 * and ai->ai_next were initialized to zero.
622 * Allocate an addrinfo structure, and a sockaddr structure
623 * of the specificed length. We initialize:
628 * ai_addr->sa_len (LWRES_PLATFORM_HAVESALEN)
629 * and everything else is initialized to zero.
631 static struct addrinfo *
632 ai_alloc(int family, int addrlen) {
635 ai = (struct addrinfo *)calloc(1, sizeof(*ai));
639 ai->ai_addr = SA(calloc(1, addrlen));
640 if (ai->ai_addr == NULL) {
644 ai->ai_addrlen = addrlen;
645 ai->ai_family = family;
646 ai->ai_addr->sa_family = family;
647 #ifdef LWRES_PLATFORM_HAVESALEN
648 ai->ai_addr->sa_len = addrlen;
653 static struct addrinfo *
654 ai_clone(struct addrinfo *oai, int family) {
657 ai = ai_alloc(family, ((family == AF_INET6) ?
658 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
661 lwres_freeaddrinfo(oai);
667 ai->ai_flags = oai->ai_flags;
668 ai->ai_socktype = oai->ai_socktype;
669 ai->ai_protocol = oai->ai_protocol;
670 ai->ai_canonname = NULL;
675 static struct addrinfo *
676 ai_reverse(struct addrinfo *oai) {
677 struct addrinfo *nai, *tai;
681 while (oai != NULL) {
683 * Grab one off the old list.
688 * Put it on the front of the new list.