2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <network/sa_compare.h>
33 #include <arpa/inet.h>
37 #include <sys/param.h>
39 #include <notify_keys.h>
41 #include <TargetConditionals.h>
42 #include "netdb_async.h"
43 #include "si_module.h"
46 #define IPPROTO_UNSPEC 0
48 #define IPV6_ADDR_LEN 16
49 #define IPV4_ADDR_LEN 4
51 #define WANT_NOTHING 0
52 #define WANT_A4_ONLY 1
53 #define WANT_A6_ONLY 2
54 #define WANT_A6_PLUS_MAPPED_A4 3
55 #define WANT_A6_OR_MAPPED_A4_IF_NO_A6 4
57 #define V6TO4_PREFIX_16 0x2002
59 static int net_config_token
= -1;
60 static uint32_t net_v4_count
= 0;
61 static uint32_t net_v6_count
= 0; // includes 6to4 addresses
62 static pthread_mutex_t net_config_mutex
= PTHREAD_MUTEX_INITIALIZER
;
71 __private_extern__
int
72 si_inet_config(uint32_t *inet4
, uint32_t *inet6
)
75 struct ifaddrs
*ifa
, *ifap
;
77 pthread_mutex_lock(&net_config_mutex
);
81 if (net_config_token
< 0)
83 status
= notify_register_check(kNotifySCNetworkChange
, &net_config_token
);
84 if (status
!= 0) net_config_token
= -1;
87 if (net_config_token
>= 0)
89 status
= notify_check(net_config_token
, &checkit
);
90 if (status
!= 0) checkit
= 1;
97 if (getifaddrs(&ifa
) < 0)
106 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
108 if (ifap
->ifa_addr
== NULL
) continue;
109 if ((ifap
->ifa_flags
& IFF_UP
) == 0) continue;
111 if (ifap
->ifa_addr
->sa_family
== AF_INET
)
115 else if (ifap
->ifa_addr
->sa_family
== AF_INET6
)
125 if (inet4
!= NULL
) *inet4
= net_v4_count
;
126 if (inet6
!= NULL
) *inet6
= net_v6_count
;
128 pthread_mutex_unlock(&net_config_mutex
);
134 freeaddrinfo(struct addrinfo
*a
)
136 struct addrinfo
*next
;
141 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
142 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
149 gai_strerror(int32_t err
)
153 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
154 case EAI_AGAIN
: return "Temporary failure in name resolution";
155 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
156 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
157 case EAI_FAMILY
: return "ai_family not supported";
158 case EAI_MEMORY
: return "Memory allocation failure";
159 case EAI_NODATA
: return "No address associated with nodename";
160 case EAI_NONAME
: return "nodename nor servname provided, or not known";
161 case EAI_SERVICE
: return "servname not supported for ai_socktype";
162 case EAI_SOCKTYPE
: return "ai_socktype not supported";
163 case EAI_SYSTEM
: return "System error";
164 case EAI_BADHINTS
: return "Bad hints";
165 case EAI_PROTOCOL
: return "ai_protocol not supported";
166 case EAI_OVERFLOW
: return "argument buffer overflow";
169 return "Unknown error";
175 * We handle some "trival" cases locally. If the caller passes
176 * NI_NUMERICHOST (only), then this call turns into a getservbyport
177 * to get the service name + inet_pton() to create a host string.
178 * If the caller passes NI_NUMERICSERV (only), then we zero out the port
179 * number, complete the getnameinfo, and use printf() to create a service
180 * string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
181 * we inet_ntop() and printf() and return the results.
184 si_nameinfo(si_mod_t
*si
, const struct sockaddr
*sa
, int flags
, const char *interface
, uint32_t *err
)
186 si_item_t
*out
= NULL
;
187 const struct sockaddr
*lookup_sa
;
188 struct sockaddr_in s4
;
191 const uint64_t unused
= 0;
198 int do_host_lookup
= ((flags
& NI_NUMERICHOST
) == 0);
199 int do_serv_lookup
= ((flags
& NI_NUMERICSERV
) == 0);
202 if ((si
== NULL
) || (sa
== NULL
))
204 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
208 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
212 if (sa
->sa_family
== AF_INET
)
214 struct sockaddr_in
*s4
= (struct sockaddr_in
*)sa
;
215 memcpy(&a4
, &s4
->sin_addr
, sizeof(a4
));
219 else if (sa
->sa_family
== AF_INET6
)
221 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)sa
;
222 memcpy(&a6
, &s6
->sin6_addr
, sizeof(a6
));
223 port
= s6
->sin6_port
;
225 /* Look for scope id in IPv6 Link Local, Multicast Node Local, and Multicast Link Local */
226 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_NODELOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_LINKLOCAL(&s6
->sin6_addr
))
228 ifnum
= ntohs(a6
.__u6_addr
.__u6_addr16
[1]);
231 ifnum
= s6
->sin6_scope_id
;
232 a6
.__u6_addr
.__u6_addr16
[1] = htons(ifnum
);
235 if ((ifnum
!= s6
->sin6_scope_id
) && (s6
->sin6_scope_id
!= 0))
237 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
242 /* v4 mapped and compat addresses are converted to plain v4 */
243 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
) || IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
))
245 memcpy(&a4
, &s6
->sin6_addr
.s6_addr
[12], sizeof(a4
));
247 memset(&s4
, 0, sizeof(s4
));
248 s4
.sin_len
= sizeof(s4
);
249 s4
.sin_family
= AF_INET
;
251 memcpy(&s4
.sin_addr
, &a4
, sizeof(s4
.sin_addr
));
252 lookup_sa
= (const struct sockaddr
*)&s4
;
261 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
265 if (do_host_lookup
== 1)
267 si_item_t
*item
= si_host_byaddr(si
, addr
, lookup_sa
->sa_family
, interface
, NULL
);
271 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
272 host
= strdup(h
->h_name
);
273 si_item_release(item
);
276 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
282 if ((do_serv_lookup
== 1) && (port
!= 0))
284 si_item_t
*item
= si_service_byport(si
, port
, NULL
);
288 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
289 serv
= strdup(s
->s_name
);
290 si_item_release(item
);
294 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
301 * Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
302 * if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
304 if ((host
== NULL
) && ((flags
& NI_NAMEREQD
) == 0))
306 char tmp
[INET6_ADDRSTRLEN
+ 1 + IF_NAMESIZE
+ 1];
308 if (sa
->sa_family
== AF_INET
)
310 char buf
[INET_ADDRSTRLEN
];
311 if (inet_ntop(AF_INET
, &a4
, buf
, sizeof(buf
)) != 0)
316 else if (sa
->sa_family
== AF_INET6
)
318 char buf
[INET6_ADDRSTRLEN
];
320 /* zero the embedded scope ID */
323 a6
.__u6_addr
.__u6_addr16
[1] = 0;
326 if (inet_ntop(AF_INET6
, &a6
, buf
, sizeof(buf
)) != 0)
330 char ifname
[IF_NAMESIZE
];
331 if (if_indextoname(ifnum
, ifname
) != NULL
)
333 asprintf(&host
, "%s%%%s", buf
, ifname
);
338 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
350 /* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
353 asprintf(&serv
, "%hu", ntohs(port
));
356 if ((host
== NULL
) || (serv
== NULL
))
360 if ((flags
& NI_NAMEREQD
) != 0)
362 *err
= SI_STATUS_EAI_NONAME
;
366 *err
= SI_STATUS_EAI_MEMORY
;
372 out
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_NAMEINFO
, 1, unused
, unused
, host
, serv
);
381 _gai_numericserv(const char *serv
, uint16_t *port
)
396 num
= strtol(serv
, &endptr
, 10);
397 if ((serv
[0] != '\0') && (*endptr
== '\0') && (num
>= 0) && (num
<= UINT16_MAX
))
400 if (port
!= NULL
) *port
= (uint16_t)num
;
408 _gai_serv_to_port(const char *serv
, uint32_t proto
, uint16_t *port
)
412 const char *protoname
= NULL
;
414 if (_gai_numericserv(serv
, port
)) return 0;
416 if (proto
== IPPROTO_UDP
) protoname
= "udp";
417 if (proto
== IPPROTO_TCP
) protoname
= "tcp";
419 item
= si_service_byname(si_search(), serv
, protoname
);
420 if (item
== NULL
) return -1;
422 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
423 if (port
) *port
= ntohs(s
->s_port
);
424 si_item_release(item
);
430 si_addrinfo_v4(si_mod_t
*si
, int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in_addr
*addr
, uint16_t iface
, const char *cname
)
432 socket_data_t sockdata
;
433 struct sockaddr_in
*sa
;
438 len
= sizeof(struct sockaddr_in
);
439 memset(&sockdata
, 0, sizeof(socket_data_t
));
440 sa
= (struct sockaddr_in
*)&sockdata
;
443 sa
->sin_family
= AF_INET
;
444 sa
->sin_port
= htons(port
);
445 memcpy(&sa
->sin_addr
, addr
, sizeof(sa
->sin_addr
));
447 /* Kludge: Jam the interface number into sin_zero (4 bytes). */
449 memmove(sa
->sin_zero
, &v32
, sizeof(uint32_t));
451 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET
, sock
, proto
, len
, sockdata
, cname
);
455 si_addrinfo_v4_mapped(si_mod_t
*si
, int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in_addr
*addr
, uint16_t iface
, const char *cname
)
457 socket_data_t sockdata
;
458 struct sockaddr_in6
*sa
;
463 len
= sizeof(struct sockaddr_in6
);
464 memset(&sockdata
, 0, sizeof(socket_data_t
));
465 sa
= (struct sockaddr_in6
*)&sockdata
;
468 sa
->sin6_family
= AF_INET6
;
469 sa
->sin6_port
= htons(port
);
470 memset(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[10]), 0xff, 2);
471 memcpy(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[12]), addr
, sizeof(struct in_addr
));
473 /* sin6_scope_id is in host byte order */
474 sa
->sin6_scope_id
= iface
;
476 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
481 si_addrinfo_v6(si_mod_t
*si
, int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in6_addr
*addr
, uint16_t iface
, const char *cname
)
483 socket_data_t sockdata
;
484 struct sockaddr_in6
*sa
;
489 len
= sizeof(struct sockaddr_in6
);
490 memset(&sockdata
, 0, sizeof(socket_data_t
));
491 sa
= (struct sockaddr_in6
*)&sockdata
;
494 sa
->sin6_family
= AF_INET6
;
495 sa
->sin6_port
= htons(port
);
496 memcpy(&sa
->sin6_addr
, addr
, sizeof(sa
->sin6_addr
));
498 /* sin6_scope_id is in host byte order */
499 sa
->sin6_scope_id
= iface
;
501 if (IN6_IS_ADDR_LINKLOCAL(&sa
->sin6_addr
))
503 /* check for embedded scopeid */
504 uint16_t esid
= ntohs(sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
507 sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
508 if (iface
== 0) sa
->sin6_scope_id
= esid
;
512 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
516 si_addrinfo_list(si_mod_t
*si
, uint32_t flags
, int socktype
, int proto
, struct in_addr
*a4
, struct in6_addr
*a6
, int port
, int scopeid
, const char *cname4
, const char *cname6
)
519 si_item_t
*item
= NULL
;
520 si_list_t
*out4
= NULL
, *out6
= NULL
;
522 if ((flags
& AI_V4MAPPED
) && ((flags
& AI_ALL
) || (a6
== NULL
))) do_map
= 1;
526 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
528 item
= si_addrinfo_v6(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, scopeid
, cname6
);
529 out6
= si_list_add(out6
, item
);
530 si_item_release(item
);
533 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
535 item
= si_addrinfo_v6(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, scopeid
, cname6
);
536 out6
= si_list_add(out6
, item
);
537 si_item_release(item
);
540 if (proto
== IPPROTO_ICMPV6
)
542 item
= si_addrinfo_v6(si
, 0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, scopeid
, cname6
);
543 out6
= si_list_add(out6
, item
);
544 si_item_release(item
);
550 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
554 item
= si_addrinfo_v4(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
555 out4
= si_list_add(out4
, item
);
559 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
560 out6
= si_list_add(out6
, item
);
563 si_item_release(item
);
566 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
570 item
= si_addrinfo_v4(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
571 out4
= si_list_add(out4
, item
);
575 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
576 out6
= si_list_add(out6
, item
);
579 si_item_release(item
);
582 if (proto
== IPPROTO_ICMP
)
586 item
= si_addrinfo_v4(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
587 out4
= si_list_add(out4
, item
);
591 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
592 out6
= si_list_add(out6
, item
);
595 si_item_release(item
);
599 out6
= si_list_concat(out6
, out4
);
600 si_list_release(out4
);
607 * Determines whether the given host name is a numeric IPv4 or IPv6 address,
608 * based on the address family input value. If the input addres family is
609 * unspecified, a more specific value will be provided on output if possible.
610 * Returns 1 if host name is numeric or 0 if not, or -1 on error.
613 _gai_numerichost(const char* nodename
, uint32_t *family
, int flags
, struct in_addr
*a4
, struct in6_addr
*a6
, int *scope
)
615 int numerichost
, passive
;
620 if (nodename
== NULL
)
622 /* return loopback or passive addresses */
623 passive
= (flags
& AI_PASSIVE
);
625 if (((*family
== AF_UNSPEC
) || (*family
== AF_INET
)) || ((*family
== AF_INET6
) && (flags
& AI_V4MAPPED
) && (flags
& AI_ALL
)))
627 if (passive
) a4
->s_addr
= 0;
628 else a4
->s_addr
= htonl(INADDR_LOOPBACK
);
631 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET6
))
633 memset(a6
, 0, sizeof(*a6
));
634 if (!passive
) a6
->__u6_addr
.__u6_addr32
[3] = htonl(1);
642 * numeric IPv4 host valid for AF_UNSPEC and AF_INET
643 * also valid for AF_INET6 with AI_V4MAPPED
645 numerichost
= inet_pton(AF_INET
, nodename
, a4
);
646 if (numerichost
== 0)
648 test
= inet_addr(nodename
);
649 if (test
!= (in_addr_t
)-1)
656 if (numerichost
== 1)
658 if (*family
== AF_UNSPEC
)
662 else if (*family
== AF_INET6
)
664 if (flags
& AI_V4MAPPED
)
666 memset(a6
, 0, sizeof(struct in6_addr
));
667 memset(&(a6
->__u6_addr
.__u6_addr8
[10]), 0xff, 2);
668 memcpy(&(a6
->__u6_addr
.__u6_addr8
[12]), a4
, sizeof(struct in_addr
));
679 /* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
680 numerichost
= inet_pton(AF_INET6
, nodename
, a6
);
681 if (numerichost
== 1)
683 /* check for scope/zone id */
684 char *p
= strrchr(nodename
, SCOPE_DELIMITER
);
692 for (x
= p
; (*x
!= '\0') && (d
== 1); x
++)
698 if (d
== 1) *scope
= atoi(p
);
699 else *scope
= if_nametoindex(p
);
702 if (*family
== AF_UNSPEC
) *family
= AF_INET6
;
703 else if (*family
== AF_INET
) numerichost
= -1;
712 /* si_addrinfo_list_from_hostent
713 * Returns an addrinfo list from IPv4 and IPv6 hostent entries
716 si_addrinfo_list_from_hostent(si_mod_t
*si
, uint32_t flags
, uint32_t socktype
, uint32_t proto
, uint16_t port
, uint16_t scope
, const struct hostent
*h4
, const struct hostent
*h6
)
719 si_list_t
*out
= NULL
;
722 if ((h6
!= NULL
) && (h6
->h_addr_list
!= NULL
))
724 for (i
= 0; h6
->h_addr_list
[i
] != NULL
; i
++)
727 memcpy(&a6
, h6
->h_addr_list
[i
], h6
->h_length
);
728 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, NULL
, &a6
, port
, scope
, NULL
, h6
->h_name
);
729 out
= si_list_concat(out
, list
);
730 si_list_release(list
);
734 if ((h4
!= NULL
) && (h4
->h_addr_list
!= NULL
))
736 for (i
= 0; h4
->h_addr_list
[i
] != NULL
; i
++)
739 memcpy(&a4
, h4
->h_addr_list
[i
], h4
->h_length
);
740 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, &a4
, NULL
, port
, 0, h4
->h_name
, NULL
);
741 out
= si_list_concat(out
, list
);
742 si_list_release(list
);
750 _gai_addr_sort(const void *a
, const void *b
)
752 si_item_t
**item_a
, **item_b
;
753 si_addrinfo_t
*p
, *q
;
754 struct sockaddr
*sp
, *sq
;
756 item_a
= (si_item_t
**)a
;
757 item_b
= (si_item_t
**)b
;
759 p
= (si_addrinfo_t
*)((uintptr_t)*item_a
+ sizeof(si_item_t
));
760 q
= (si_addrinfo_t
*)((uintptr_t)*item_b
+ sizeof(si_item_t
));
762 sp
= (struct sockaddr
*)p
->ai_addr
.x
;
763 sq
= (struct sockaddr
*)q
->ai_addr
.x
;
766 * sa_dst_compare(A,B) returns -1 if A is less desirable than B,
767 * 0 if they are equally desirable, and 1 if A is more desirable.
768 * qsort() expects the inverse, so we swap sp and sq.
770 return sa_dst_compare(sq
, sp
, 0);
774 _gai_sort_list(si_list_t
*in
, uint32_t flags
)
779 uint32_t v4mapped_count
= 0;
780 uint32_t v6_count
= 0;
783 if (in
== NULL
) return NULL
;
785 for (i
= 0; i
< in
->count
; i
++)
787 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
788 if (a
->ai_family
== AF_INET6
)
790 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
791 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
))) v4mapped_count
++;
797 if ((flags
& AI_V4MAPPED
) && ((v6_count
== 0) || (flags
& AI_ALL
))) filter_mapped
= 0;
801 if ((filter_mapped
== 1) && (v4mapped_count
> 0))
803 i
= in
->count
- v4mapped_count
;
804 if (i
== 0) return NULL
;
806 out
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
807 if (out
== NULL
) return in
;
810 out
->refcount
= in
->refcount
;
812 out
->entry
= (si_item_t
**)calloc(out
->count
, sizeof(si_item_t
*));
813 if (out
->entry
== NULL
)
821 for (i
= 0; i
< in
->count
; i
++)
823 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
824 if (a
->ai_family
== AF_INET6
)
826 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
827 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
)))
829 si_item_release(in
->entry
[i
]);
834 out
->entry
[out
->curr
++] = in
->entry
[i
];
843 qsort(&out
->entry
[0], out
->count
, sizeof(si_item_t
*), _gai_addr_sort
);
848 * Simple lookup via gethostbyname2(3) mechanism.
851 _gai_simple(si_mod_t
*si
, const void *nodeptr
, const void *servptr
, uint32_t family
, uint32_t socktype
, uint32_t proto
, uint32_t flags
, const char *interface
, uint32_t *err
)
853 si_item_t
*h4_item
= NULL
, *h6_item
= NULL
;
854 struct hostent
*h4
= NULL
, *h6
= NULL
;
855 si_list_t
*out
= NULL
;
858 if ((flags
& AI_NUMERICSERV
) != 0)
860 port
= *(uint16_t*)servptr
;
864 if (_gai_serv_to_port(servptr
, proto
, &port
) != 0)
866 if (err
) *err
= SI_STATUS_EAI_NONAME
;
871 if ((flags
& AI_NUMERICHOST
) != 0)
873 if (family
== AF_INET
)
875 h4_item
= si_host_byaddr(si
, nodeptr
, AF_INET
, interface
, NULL
);
877 else if (family
== AF_INET6
)
879 h6_item
= si_host_byaddr(si
, nodeptr
, AF_INET6
, interface
, NULL
);
884 if ((family
== AF_INET
) || (family
== AF_UNSPEC
))
886 h4_item
= si_host_byname(si
, nodeptr
, AF_INET
, interface
, NULL
);
889 if ((family
== AF_INET6
) || (family
== AF_UNSPEC
))
891 h6_item
= si_host_byname(si
, nodeptr
, AF_INET6
, interface
, NULL
);
897 h4
= (struct hostent
*)((uintptr_t)h4_item
+ sizeof(si_item_t
));
902 h6
= (struct hostent
*)((uintptr_t)h6_item
+ sizeof(si_item_t
));
905 out
= si_addrinfo_list_from_hostent(si
, flags
, socktype
, proto
, port
, 0, h4
, h6
);
906 si_item_release(h4_item
);
907 si_item_release(h6_item
);
909 return _gai_sort_list(out
, flags
);
913 si_srv_byname(si_mod_t
*si
, const char *qname
, const char *interface
, uint32_t *err
)
915 if (si
== NULL
) return 0;
916 if (si
->vtable
->sim_srv_byname
== NULL
) return 0;
918 return si
->vtable
->sim_srv_byname(si
, qname
, interface
, err
);
922 si_wants_addrinfo(si_mod_t
*si
)
924 if (si
== NULL
) return 0;
925 if (si
->vtable
->sim_addrinfo
== NULL
) return 0;
926 if (si
->vtable
->sim_wants_addrinfo
== NULL
) return 0;
928 return si
->vtable
->sim_wants_addrinfo(si
);
932 _gai_srv(si_mod_t
*si
, const char *node
, const char *serv
, uint32_t family
, uint32_t socktype
, uint32_t proto
, uint32_t flags
, const char *interface
, uint32_t *err
)
939 si_list_t
*list
= NULL
;
940 si_list_t
*result
= NULL
;
942 /* Minimum SRV priority is zero. Start below that. */
946 if (node
== NULL
|| serv
== NULL
) return NULL
;
948 asprintf(&qname
, "%s.%s", serv
, node
);
949 list
= si_srv_byname(si
, qname
, interface
, err
);
952 /* Iterate the SRV records starting at lowest priority and attempt to
953 * lookup the target host name. Returns the first successful lookup.
954 * It's an O(n^2) algorithm but data sets are small (less than 100) and
955 * sorting overhead is dwarfed by network I/O for each element.
957 while (list
!= NULL
&& result
== NULL
)
959 /* Find the next lowest priority level. */
960 /* Maximum SRV priority is UINT16_MAX. Start above that. */
963 for (i
= 0; i
< list
->count
; ++i
)
965 item
= list
->entry
[i
];
966 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
968 if (srv
->priority
> lastprio
&& srv
->priority
< currprio
)
970 currprio
= srv
->priority
;
974 if (currprio
== INT_MAX
)
976 /* All priorities have been evaluated. Done. */
984 /* Lookup hosts at the current priority level. Return first match. */
985 for (i
= 0; i
< list
->count
; ++i
)
987 item
= list
->entry
[i
];
988 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
990 if (srv
->priority
== currprio
)
992 /* So that _gai_simple expects an integer service. */
993 flags
|= AI_NUMERICSERV
;
995 result
= _gai_simple(si
, srv
->target
, &srv
->port
, family
, socktype
, proto
, flags
, interface
, err
);
1006 si_list_release(list
);
1013 si_addrinfo(si_mod_t
*si
, const char *node
, const char *serv
, uint32_t family
, uint32_t socktype
, uint32_t proto
, uint32_t flags
, const char *interface
, uint32_t *err
)
1015 int numerichost
, numericserv
= 0;
1017 const void *nodeptr
= NULL
, *servptr
= NULL
;
1019 struct in_addr a4
, *p4
;
1020 struct in6_addr a6
, *p6
;
1024 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
1028 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
1032 /* treat empty strings as NULL */
1033 if ((node
!= NULL
) && (node
[0] == '\0')) node
= NULL
;
1034 if ((serv
!= NULL
) && (serv
[0] == '\0')) serv
= NULL
;
1036 /* make sure we have a query */
1037 if ((node
== NULL
) && (serv
== NULL
))
1039 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1043 /* verify family is supported */
1051 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
1055 /* verify socket type is supported */
1064 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1068 /* verify protocol is supported */
1071 case IPPROTO_UNSPEC
:
1075 case IPPROTO_ICMPV6
:
1078 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1082 /* verify socket type compatible with protocol */
1083 if (((socktype
== SOCK_DGRAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_UDP
)) || ((socktype
== SOCK_STREAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_TCP
)))
1085 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1089 /* check AI_V4MAPPED and AI_ALL */
1090 if (family
!= AF_INET6
)
1092 /* unset AI_V4MAPPED and AI_ALL unless family is AF_INET6 */
1093 flags
&= ~(AI_V4MAPPED
| AI_ALL
);
1095 else if ((flags
& AI_V4MAPPED
) == 0)
1097 /* unset AI_ALL unless family is AF_INET6 and AI_V4MAPPED is set */
1101 memset(&a4
, 0, sizeof(struct in_addr
));
1102 memset(&a6
, 0, sizeof(struct in6_addr
));
1104 /* determine the protocol if possible */
1105 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_DGRAM
)) proto
= IPPROTO_UDP
;
1106 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_STREAM
)) proto
= IPPROTO_TCP
;
1110 if ((flags
& AI_SRV
) != 0)
1113 out
= _gai_srv(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
1114 return _gai_sort_list(out
, flags
);
1118 /* Usual service lookup */
1119 numericserv
= _gai_numericserv(serv
, &port
);
1122 if ((flags
& AI_NUMERICSERV
) && (numericserv
== 0))
1124 /* FreeBSD returns SI_STATUS_EAI_SERVICE */
1125 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1129 if ((serv
!= NULL
) && (strcmp(serv
, "0") != 0))
1131 if (numericserv
== 1)
1133 flags
|= AI_NUMERICSERV
;
1142 numerichost
= _gai_numerichost(node
, &family
, flags
, &a4
, &a6
, &scope
);
1143 if ((numerichost
== -1) || ((flags
& AI_NUMERICHOST
) && (numerichost
== 0)))
1145 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1149 if (numerichost
== 1)
1151 flags
|= AI_NUMERICHOST
;
1152 if (family
== AF_INET
)
1156 else if (family
== AF_INET6
)
1166 if ((numerichost
== 1) && (numericserv
== 0))
1168 /* only service lookup needed. convert to port and perform a trivial getaddrinfo */
1169 if (_gai_serv_to_port(serv
, proto
, &port
) != 0)
1171 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1176 flags
|= AI_NUMERICSERV
;
1182 if ((numerichost
== 1) && (numericserv
== 1))
1188 if (family
== AF_INET
) p6
= NULL
;
1189 if (family
== AF_INET6
) p4
= NULL
;
1190 if (node
== NULL
) cname
= "localhost";
1192 /* allow nodename to be a mapped IPv4 address, e.g. "::ffff:10.0.0.1" */
1193 if (p6
!= NULL
) flags
|= AI_V4MAPPED
;
1195 /* handle trivial questions */
1196 out
= si_addrinfo_list(si
, flags
, socktype
, proto
, p4
, p6
, port
, scope
, cname
, cname
);
1197 return _gai_sort_list(out
, flags
);
1199 else if (si_wants_addrinfo(si
))
1201 /* or let the current module handle the host lookups intelligently */
1202 out
= si
->vtable
->sim_addrinfo(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1203 return _gai_sort_list(out
, flags
);
1206 /* fall back to a default path */
1207 out
= _gai_simple(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1208 return _gai_sort_list(out
, flags
);
1211 static struct addrinfo
*
1212 si_item_to_addrinfo(si_item_t
*item
)
1215 struct addrinfo
*out
;
1217 if (item
== NULL
) return NULL
;
1219 a
= (si_addrinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1221 out
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
1222 if (out
== NULL
) return NULL
;
1224 out
->ai_flags
= a
->ai_flags
;
1225 out
->ai_family
= a
->ai_family
;
1226 out
->ai_socktype
= a
->ai_socktype
;
1227 out
->ai_protocol
= a
->ai_protocol
;
1228 out
->ai_addrlen
= a
->ai_addrlen
;
1230 out
->ai_addr
= (struct sockaddr
*)calloc(1, out
->ai_addrlen
);
1231 if (out
->ai_addr
== NULL
)
1237 memcpy(out
->ai_addr
, a
->ai_addr
.x
, out
->ai_addrlen
);
1239 if (a
->ai_canonname
!= NULL
)
1241 out
->ai_canonname
= strdup(a
->ai_canonname
);
1242 if (out
->ai_canonname
== NULL
)
1253 si_list_to_addrinfo(si_list_t
*list
)
1255 struct addrinfo
*tail
, *out
;
1258 if (list
== NULL
) return NULL
;
1259 if (list
->count
== 0) return NULL
;
1261 i
= list
->count
- 1;
1263 out
= si_item_to_addrinfo(list
->entry
[i
]);
1266 for (i
--; i
>= 0; i
--)
1268 out
= si_item_to_addrinfo(list
->entry
[i
]);
1275 out
->ai_next
= tail
;
1282 /* getipnodebyname */
1285 make_hostent(si_mod_t
*si
, const char *name
, struct in_addr addr
)
1291 if (name
== NULL
) return NULL
;
1295 addrs
[0] = (char *)&(addr
.s_addr
);
1299 return (si_item_t
*)LI_ils_create("L4488s*44a", (unsigned long)si
, CATEGORY_HOST_IPV4
, 1, unused
, unused
, name
, aliases
, AF_INET
, IPV4_ADDR_LEN
, addrs
);
1303 make_hostent6(si_mod_t
*si
, const char *name
, struct in6_addr addr
)
1309 if (name
== NULL
) return NULL
;
1313 addrs
[0] = (char *)&(addr
.__u6_addr
.__u6_addr32
[0]);
1317 return (si_item_t
*)LI_ils_create("L4488s*44c", (unsigned long)si
, CATEGORY_HOST_IPV6
, 1, unused
, unused
, name
, aliases
, AF_INET6
, IPV6_ADDR_LEN
, addrs
);
1321 lower_case(const char *s
)
1326 if (s
== NULL
) return NULL
;
1328 t
= malloc(strlen(s
) + 1);
1330 for (i
= 0; s
[i
] != '\0'; i
++)
1332 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
1342 merge_alias(const char *name
, build_hostent_t
*h
)
1346 if (name
== NULL
) return 0;
1347 if (h
== NULL
) return 0;
1349 if ((h
->host
.h_name
!= NULL
) && (string_equal(name
, h
->host
.h_name
))) return 0;
1351 for (i
= 0; i
< h
->alias_count
; i
++)
1353 if (string_equal(name
, h
->host
.h_aliases
[i
])) return 0;
1356 if (h
->alias_count
== 0) h
->host
.h_aliases
= (char **)calloc(2, sizeof(char *));
1357 else h
->host
.h_aliases
= (char **)reallocf(h
->host
.h_aliases
, (h
->alias_count
+ 2) * sizeof(char *));
1359 if (h
->host
.h_aliases
== NULL
)
1365 h
->host
.h_aliases
[h
->alias_count
] = lower_case(name
);
1367 h
->host
.h_aliases
[h
->alias_count
] = NULL
;
1373 append_addr(const char *addr
, uint32_t len
, build_hostent_t
*h
)
1375 if (addr
== NULL
) return 0;
1376 if (h
== NULL
) return 0;
1378 if (h
->addr_count
== 0) h
->host
.h_addr_list
= (char **)calloc(2, sizeof(char *));
1379 else h
->host
.h_addr_list
= (char **)reallocf(h
->host
.h_addr_list
, (h
->addr_count
+ 2) * sizeof(char *));
1381 if (h
->host
.h_addr_list
== NULL
)
1387 h
->host
.h_addr_list
[h
->addr_count
] = malloc(len
);
1388 if (h
->host
.h_addr_list
[h
->addr_count
] == NULL
) return -1;
1390 memcpy(h
->host
.h_addr_list
[h
->addr_count
], addr
, len
);
1392 h
->host
.h_addr_list
[h
->addr_count
] = NULL
;
1398 free_build_hostent(build_hostent_t
*h
)
1403 if (h
== NULL
) return;
1405 if (h
->host
.h_name
!= NULL
) free(h
->host
.h_name
);
1406 h
->host
.h_name
= NULL
;
1408 aliases
= h
->host
.h_aliases
;
1409 if (aliases
!= NULL
)
1411 while (*aliases
!= NULL
) free(*aliases
++);
1412 free(h
->host
.h_aliases
);
1415 h
->host
.h_aliases
= NULL
;
1417 if (h
->host
.h_addr_list
!= NULL
)
1419 for (i
= 0; h
->host
.h_addr_list
[i
] != NULL
; i
++) free(h
->host
.h_addr_list
[i
]);
1420 free(h
->host
.h_addr_list
);
1423 h
->host
.h_addr_list
= NULL
;
1428 si_ipnode_byname(si_mod_t
*si
, const char *name
, int family
, int flags
, const char *interface
, uint32_t *err
)
1430 int i
, status
, want
;
1432 struct in_addr addr4
;
1433 struct in6_addr addr6
;
1434 si_item_t
*item4
, *item6
;
1435 build_hostent_t
*out
;
1439 memset(&addr4
, 0, sizeof(struct in_addr
));
1440 memset(&addr6
, 0, sizeof(struct in6_addr
));
1442 if (err
!= NULL
) *err
= 0;
1444 if (family
== AF_INET
)
1446 status
= inet_aton(name
, &addr4
);
1449 /* create a host entry */
1450 item4
= make_hostent(si
, name
, addr4
);
1453 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1460 else if (family
== AF_INET6
)
1462 status
= inet_pton(family
, name
, &addr6
);
1465 /* create a host entry */
1466 item6
= make_hostent6(si
, name
, addr6
);
1469 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1476 status
= inet_aton(name
, &addr4
);
1479 if (!(flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
)))
1481 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1485 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1486 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1487 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1488 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), &(addr4
.s_addr
), IPV4_ADDR_LEN
);
1490 /* create a host entry */
1491 item6
= make_hostent6(si
, name
, addr6
);
1494 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1503 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1508 * IF AI_ADDRCONFIG is set, we need to know what interface flavors we really have.
1514 if (flags
& AI_ADDRCONFIG
)
1516 if (si_inet_config(&if4
, &if6
) < 0)
1518 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1522 /* Bail out if there are no interfaces */
1523 if ((if4
== 0) && (if6
== 0))
1525 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1531 * Figure out what we want.
1532 * If user asked for AF_INET, we only want V4 addresses.
1534 want
= WANT_A4_ONLY
;
1536 if (family
== AF_INET
)
1538 if ((flags
& AI_ADDRCONFIG
) && (if4
== 0))
1540 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1544 else if (family
== AF_INET6
)
1546 /* family == AF_INET6 */
1547 want
= WANT_A6_ONLY
;
1549 if (flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
))
1553 want
= WANT_A6_PLUS_MAPPED_A4
;
1557 want
= WANT_A6_OR_MAPPED_A4_IF_NO_A6
;
1562 if ((flags
& AI_ADDRCONFIG
) && (if6
== 0))
1564 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1571 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1578 /* fetch IPv6 data if required */
1579 if ((want
== WANT_A6_ONLY
) || (want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) || (want
== WANT_A6_PLUS_MAPPED_A4
))
1581 item6
= si_host_byname(si
, name
, AF_INET6
, interface
, (uint32_t *)err
);
1584 /* fetch IPv4 data if required */
1585 if ((want
== WANT_A4_ONLY
) || (want
== WANT_A6_PLUS_MAPPED_A4
) || ((want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) && (item6
== NULL
)))
1587 item4
= si_host_byname(si
, name
, AF_INET
, interface
, (uint32_t *)err
);
1590 if (want
== WANT_A4_ONLY
)
1592 si_item_release(item6
);
1593 if ((item4
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1597 if (want
== WANT_A6_ONLY
)
1599 si_item_release(item4
);
1600 if ((item6
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1604 if ((item6
== NULL
) && (item4
== NULL
))
1606 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1610 /* output item will have IPv6 + mapped IPv4 addresses */
1612 out
= (build_hostent_t
*)calloc(1, sizeof(build_hostent_t
));
1615 si_item_release(item4
);
1616 si_item_release(item6
);
1617 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1623 h
= (struct hostent
*)((uintptr_t)item4
+ sizeof(si_item_t
));
1625 out
->host
.h_name
= lower_case(h
->h_name
);
1627 if (h
->h_aliases
!= NULL
)
1629 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1632 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
1634 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1635 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1636 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1637 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), h
->h_addr_list
[i
], IPV4_ADDR_LEN
);
1638 append_addr((const char *)&addr6
, IPV6_ADDR_LEN
, out
);
1644 h
= (struct hostent
*)((uintptr_t)item6
+ sizeof(si_item_t
));
1646 if (out
->host
.h_name
== NULL
) out
->host
.h_name
= lower_case(h
->h_name
);
1648 if (h
->h_aliases
!= NULL
)
1650 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1653 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++) append_addr(h
->h_addr_list
[i
], IPV6_ADDR_LEN
, out
);
1656 si_item_release(item4
);
1657 si_item_release(item6
);
1661 item6
= (si_item_t
*)LI_ils_create("L4488s*44c", (unsigned long)si
, CATEGORY_HOST_IPV6
, 1, unused
, unused
, out
->host
.h_name
, out
->host
.h_aliases
, AF_INET6
, IPV6_ADDR_LEN
, out
->host
.h_addr_list
);
1663 free_build_hostent(out
);