2 * Copyright (c) 2008-2018 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@
24 #include "libinfo_common.h"
28 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include <arpa/inet.h>
40 #include <sys/param.h>
42 #include <notify_keys.h>
44 #include <TargetConditionals.h>
45 #include "netdb_async.h"
46 #include "si_compare.h"
47 #include "si_module.h"
50 #define IPPROTO_UNSPEC 0
52 #define IPV6_ADDR_LEN 16
53 #define IPV4_ADDR_LEN 4
55 #define WANT_NOTHING 0
56 #define WANT_A4_ONLY 1
57 #define WANT_A6_ONLY 2
58 #define WANT_A6_PLUS_MAPPED_A4 3
59 #define WANT_A6_OR_MAPPED_A4_IF_NO_A6 4
61 #define V6TO4_PREFIX_16 0x2002
63 static int net_config_token
= -1;
64 static uint32_t net_v4_count
= 0;
65 static uint32_t net_v6_count
= 0; // includes 6to4 addresses
66 static pthread_mutex_t net_config_mutex
= PTHREAD_MUTEX_INITIALIZER
;
67 static os_log_t _gai_log
= OS_LOG_DEFAULT
;
70 int _inet_aton_check(const char *cp
, struct in_addr
*addr
, int strict
);
82 _gai_log
= os_log_create("com.apple.network.libinfo", "getaddrinfo");
88 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
89 pthread_once(&once
, gai_log_init
);
93 __private_extern__
int
94 si_inet_config(uint32_t *inet4
, uint32_t *inet6
)
97 struct ifaddrs
*ifa
, *ifap
;
99 pthread_mutex_lock(&net_config_mutex
);
103 if (net_config_token
< 0)
105 status
= notify_register_check(kNotifySCNetworkChange
, &net_config_token
);
106 if (status
!= 0) net_config_token
= -1;
109 if (net_config_token
>= 0)
111 status
= notify_check(net_config_token
, &checkit
);
112 if (status
!= 0) checkit
= 1;
119 if (getifaddrs(&ifa
) < 0)
128 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
130 if (ifap
->ifa_addr
== NULL
) continue;
131 if ((ifap
->ifa_flags
& IFF_UP
) == 0) continue;
133 if (ifap
->ifa_addr
->sa_family
== AF_INET
)
137 else if (ifap
->ifa_addr
->sa_family
== AF_INET6
)
147 if (inet4
!= NULL
) *inet4
= net_v4_count
;
148 if (inet6
!= NULL
) *inet6
= net_v6_count
;
150 pthread_mutex_unlock(&net_config_mutex
);
157 freeaddrinfo(struct addrinfo
*a
)
159 struct addrinfo
*next
;
164 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
165 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
173 gai_strerror(int32_t err
)
177 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
178 case EAI_AGAIN
: return "Temporary failure in name resolution";
179 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
180 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
181 case EAI_FAMILY
: return "ai_family not supported";
182 case EAI_MEMORY
: return "Memory allocation failure";
183 case EAI_NODATA
: return "No address associated with nodename";
184 case EAI_NONAME
: return "nodename nor servname provided, or not known";
185 case EAI_SERVICE
: return "servname not supported for ai_socktype";
186 case EAI_SOCKTYPE
: return "ai_socktype not supported";
187 case EAI_SYSTEM
: return "System error";
188 case EAI_BADHINTS
: return "Bad hints";
189 case EAI_PROTOCOL
: return "ai_protocol not supported";
190 case EAI_OVERFLOW
: return "argument buffer overflow";
193 return "Unknown error";
199 * We handle some "trival" cases locally. If the caller passes
200 * NI_NUMERICHOST (only), then this call turns into a getservbyport
201 * to get the service name + inet_pton() to create a host string.
202 * If the caller passes NI_NUMERICSERV (only), then we zero out the port
203 * number, complete the getnameinfo, and use printf() to create a service
204 * string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
205 * we inet_ntop() and printf() and return the results.
209 si_nameinfo(si_mod_t
*si
, const struct sockaddr
*sa
, int flags
, const char *interface
, uint32_t *err
)
211 si_item_t
*out
= NULL
;
212 const struct sockaddr
*lookup_sa
;
213 struct sockaddr_in s4
;
216 const uint64_t unused
= 0;
223 int do_host_lookup
= ((flags
& NI_NUMERICHOST
) == 0);
224 int do_serv_lookup
= ((flags
& NI_NUMERICSERV
) == 0);
227 if ((si
== NULL
) || (sa
== NULL
))
229 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
233 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
237 if (sa
->sa_family
== AF_INET
)
239 struct sockaddr_in
*s4
= (struct sockaddr_in
*)sa
;
240 memcpy(&a4
, &s4
->sin_addr
, sizeof(a4
));
244 else if (sa
->sa_family
== AF_INET6
)
246 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)sa
;
247 memcpy(&a6
, &s6
->sin6_addr
, sizeof(a6
));
248 port
= s6
->sin6_port
;
250 /* Look for scope id in IPv6 Link Local, Multicast Node Local, and Multicast Link Local */
251 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_NODELOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_LINKLOCAL(&s6
->sin6_addr
))
253 ifnum
= ntohs(a6
.__u6_addr
.__u6_addr16
[1]);
256 ifnum
= s6
->sin6_scope_id
;
257 a6
.__u6_addr
.__u6_addr16
[1] = htons(ifnum
);
260 if ((ifnum
!= s6
->sin6_scope_id
) && (s6
->sin6_scope_id
!= 0))
262 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
267 /* v4 mapped and compat addresses are converted to plain v4 */
268 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
) || IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
))
270 memcpy(&a4
, &s6
->sin6_addr
.s6_addr
[12], sizeof(a4
));
272 memset(&s4
, 0, sizeof(s4
));
273 s4
.sin_len
= sizeof(s4
);
274 s4
.sin_family
= AF_INET
;
276 memcpy(&s4
.sin_addr
, &a4
, sizeof(s4
.sin_addr
));
277 lookup_sa
= (const struct sockaddr
*)&s4
;
286 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
290 if (do_host_lookup
== 1)
292 si_item_t
*item
= si_host_byaddr(si
, addr
, lookup_sa
->sa_family
, interface
, NULL
);
296 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
297 if (h
->h_name
== NULL
)
299 si_item_release(item
);
300 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
304 host
= strdup(h
->h_name
);
305 si_item_release(item
);
308 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
314 if ((do_serv_lookup
== 1) && (port
!= 0))
316 si_item_t
*item
= si_service_byport(si
, port
, NULL
);
320 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
321 if (s
->s_name
== NULL
)
323 si_item_release(item
);
325 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
329 serv
= strdup(s
->s_name
);
330 si_item_release(item
);
334 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
341 * Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
342 * if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
344 if ((host
== NULL
) && ((flags
& NI_NAMEREQD
) == 0))
346 char tmp
[INET6_ADDRSTRLEN
+ 1 + IF_NAMESIZE
+ 1];
348 if (sa
->sa_family
== AF_INET
)
350 char buf
[INET_ADDRSTRLEN
];
351 if (inet_ntop(AF_INET
, &a4
, buf
, sizeof(buf
)) != 0)
356 else if (sa
->sa_family
== AF_INET6
)
358 char buf
[INET6_ADDRSTRLEN
];
360 /* zero the embedded scope ID */
363 a6
.__u6_addr
.__u6_addr16
[1] = 0;
366 if (inet_ntop(AF_INET6
, &a6
, buf
, sizeof(buf
)) != 0)
370 char ifname
[IF_NAMESIZE
];
371 if (if_indextoname(ifnum
, ifname
) != NULL
)
373 asprintf(&host
, "%s%%%s", buf
, ifname
);
378 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
391 /* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
394 asprintf(&serv
, "%hu", ntohs(port
));
397 if ((host
== NULL
) || (serv
== NULL
))
401 if ((flags
& NI_NAMEREQD
) != 0)
403 *err
= SI_STATUS_EAI_NONAME
;
407 *err
= SI_STATUS_EAI_MEMORY
;
413 out
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_NAMEINFO
, 1, unused
, unused
, host
, serv
);
422 _gai_numericserv(const char *serv
, uint16_t *port
)
437 num
= strtol(serv
, &endptr
, 10);
438 if ((serv
[0] != '\0') && (*endptr
== '\0') && (num
>= 0) && (num
<= UINT16_MAX
))
441 if (port
!= NULL
) *port
= (uint16_t)num
;
450 _gai_serv_to_port(const char *serv
, uint32_t proto
, uint16_t *port
)
454 const char *protoname
= NULL
;
456 if (_gai_numericserv(serv
, port
)) return 0;
458 if (proto
== IPPROTO_UDP
) protoname
= "udp";
459 if (proto
== IPPROTO_TCP
) protoname
= "tcp";
461 item
= si_service_byname(si_search(), serv
, protoname
);
462 if (item
== NULL
) return -1;
464 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
465 if (port
) *port
= ntohs(s
->s_port
);
466 si_item_release(item
);
473 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
)
475 socket_data_t sockdata
;
476 struct sockaddr_in
*sa
;
481 len
= sizeof(struct sockaddr_in
);
482 memset(&sockdata
, 0, sizeof(socket_data_t
));
483 sa
= (struct sockaddr_in
*)&sockdata
;
486 sa
->sin_family
= AF_INET
;
487 sa
->sin_port
= htons(port
);
488 memcpy(&sa
->sin_addr
, addr
, sizeof(sa
->sin_addr
));
490 /* Kludge: Jam the interface number into sin_zero (4 bytes). */
492 memmove(sa
->sin_zero
, &v32
, sizeof(uint32_t));
494 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET
, sock
, proto
, len
, sockdata
, cname
);
499 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
)
501 socket_data_t sockdata
;
502 struct sockaddr_in6
*sa
;
507 len
= sizeof(struct sockaddr_in6
);
508 memset(&sockdata
, 0, sizeof(socket_data_t
));
509 sa
= (struct sockaddr_in6
*)&sockdata
;
512 sa
->sin6_family
= AF_INET6
;
513 sa
->sin6_port
= htons(port
);
514 memset(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[10]), 0xff, 2);
515 memcpy(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[12]), addr
, sizeof(struct in_addr
));
517 /* sin6_scope_id is in host byte order */
518 sa
->sin6_scope_id
= iface
;
520 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
526 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
)
528 socket_data_t sockdata
;
529 struct sockaddr_in6
*sa
;
534 len
= sizeof(struct sockaddr_in6
);
535 memset(&sockdata
, 0, sizeof(socket_data_t
));
536 sa
= (struct sockaddr_in6
*)&sockdata
;
539 sa
->sin6_family
= AF_INET6
;
540 sa
->sin6_port
= htons(port
);
541 memcpy(&sa
->sin6_addr
, addr
, sizeof(sa
->sin6_addr
));
543 /* sin6_scope_id is in host byte order */
544 sa
->sin6_scope_id
= iface
;
546 if (IN6_IS_ADDR_LINKLOCAL(&sa
->sin6_addr
))
548 /* check for embedded scopeid */
549 uint16_t esid
= ntohs(sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
552 sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
553 if (iface
== 0) sa
->sin6_scope_id
= esid
;
557 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
562 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
)
565 si_item_t
*item
= NULL
;
566 si_list_t
*out4
= NULL
, *out6
= NULL
;
568 if ((flags
& AI_V4MAPPED
) && ((flags
& AI_ALL
) || (a6
== NULL
))) do_map
= 1;
572 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
574 item
= si_addrinfo_v6(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, scopeid
, cname6
);
575 out6
= si_list_add(out6
, item
);
576 si_item_release(item
);
579 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
581 item
= si_addrinfo_v6(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, scopeid
, cname6
);
582 out6
= si_list_add(out6
, item
);
583 si_item_release(item
);
586 if (proto
== IPPROTO_ICMPV6
)
588 item
= si_addrinfo_v6(si
, 0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, scopeid
, cname6
);
589 out6
= si_list_add(out6
, item
);
590 si_item_release(item
);
596 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
600 item
= si_addrinfo_v4(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
601 out4
= si_list_add(out4
, item
);
605 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
606 out6
= si_list_add(out6
, item
);
609 si_item_release(item
);
612 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
616 item
= si_addrinfo_v4(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
617 out4
= si_list_add(out4
, item
);
621 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
622 out6
= si_list_add(out6
, item
);
625 si_item_release(item
);
628 if (proto
== IPPROTO_ICMP
)
632 item
= si_addrinfo_v4(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
633 out4
= si_list_add(out4
, item
);
637 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
638 out6
= si_list_add(out6
, item
);
641 si_item_release(item
);
645 out6
= si_list_concat(out6
, out4
);
646 si_list_release(out4
);
653 * Determines whether the given host name is a numeric IPv4 or IPv6 address,
654 * based on the address family input value. If the input addres family is
655 * unspecified, a more specific value will be provided on output if possible.
656 * Returns 1 if host name is numeric or 0 if not, or -1 on error.
659 _gai_numerichost(const char* nodename
, uint32_t *family
, int flags
, struct in_addr
*a4
, struct in6_addr
*a6
, int *scope
)
661 int numerichost
, passive
;
665 if (nodename
== NULL
)
667 /* return loopback or passive addresses */
668 passive
= (flags
& AI_PASSIVE
);
670 if (((*family
== AF_UNSPEC
) || (*family
== AF_INET
)) || ((*family
== AF_INET6
) && (flags
& AI_V4MAPPED
) && (flags
& AI_ALL
)))
672 if (passive
) a4
->s_addr
= 0;
673 else a4
->s_addr
= htonl(INADDR_LOOPBACK
);
676 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET6
))
678 memset(a6
, 0, sizeof(*a6
));
679 if (!passive
) a6
->__u6_addr
.__u6_addr32
[3] = htonl(1);
687 * numeric IPv4 host valid for AF_UNSPEC and AF_INET
688 * also valid for AF_INET6 with AI_V4MAPPED
690 numerichost
= inet_pton(AF_INET
, nodename
, a4
);
691 if (numerichost
== 0)
693 /* inet_pton doesn't allow "a", "a.b", or "a.b.c" forms, so we re-check */
694 numerichost
= _inet_aton_check(nodename
, a4
, 1);
697 if (numerichost
== 1)
699 if (*family
== AF_UNSPEC
)
703 else if (*family
== AF_INET6
)
705 if (flags
& AI_V4MAPPED
)
707 memset(a6
, 0, sizeof(struct in6_addr
));
708 memset(&(a6
->__u6_addr
.__u6_addr8
[10]), 0xff, 2);
709 memcpy(&(a6
->__u6_addr
.__u6_addr8
[12]), a4
, sizeof(struct in_addr
));
720 /* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
721 numerichost
= inet_pton(AF_INET6
, nodename
, a6
);
722 if (numerichost
== 1)
724 /* check for scope/zone id */
725 char *p
= strrchr(nodename
, SCOPE_DELIMITER
);
733 for (x
= p
; (*x
!= '\0') && (d
== 1); x
++)
739 if (d
== 1) *scope
= atoi(p
);
740 else *scope
= if_nametoindex(p
);
743 if (*family
== AF_UNSPEC
) *family
= AF_INET6
;
744 else if (*family
== AF_INET
) numerichost
= -1;
753 /* si_addrinfo_list_from_hostent
754 * Returns an addrinfo list from IPv4 and IPv6 hostent entries
758 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
)
761 si_list_t
*out
= NULL
;
764 if ((h6
!= NULL
) && (h6
->h_addr_list
!= NULL
))
766 for (i
= 0; h6
->h_addr_list
[i
] != NULL
; i
++)
769 memcpy(&a6
, h6
->h_addr_list
[i
], h6
->h_length
);
770 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, NULL
, &a6
, port
, scope
, NULL
, h6
->h_name
);
771 out
= si_list_concat(out
, list
);
772 si_list_release(list
);
776 if ((h4
!= NULL
) && (h4
->h_addr_list
!= NULL
))
778 for (i
= 0; h4
->h_addr_list
[i
] != NULL
; i
++)
781 memcpy(&a4
, h4
->h_addr_list
[i
], h4
->h_length
);
782 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, &a4
, NULL
, port
, 0, h4
->h_name
, NULL
);
783 out
= si_list_concat(out
, list
);
784 si_list_release(list
);
792 _gai_addr_sort(const void *a
, const void *b
)
794 si_item_t
**item_a
, **item_b
;
795 si_addrinfo_t
*p
, *q
;
796 struct sockaddr
*sp
, *sq
;
798 item_a
= (si_item_t
**)a
;
799 item_b
= (si_item_t
**)b
;
801 p
= (si_addrinfo_t
*)((uintptr_t)*item_a
+ sizeof(si_item_t
));
802 q
= (si_addrinfo_t
*)((uintptr_t)*item_b
+ sizeof(si_item_t
));
804 sp
= (struct sockaddr
*)p
->ai_addr
.x
;
805 sq
= (struct sockaddr
*)q
->ai_addr
.x
;
808 * si_destination_compare(A,B) returns -1 if A is less desirable than B,
809 * 0 if they are equally desirable, and 1 if A is more desirable.
810 * qsort() expects the inverse, so we swap sp and sq.
812 return si_destination_compare(sq
, 0, sp
, 0, true);
816 _gai_sort_list(si_list_t
*in
, uint32_t flags
)
821 uint32_t v4mapped_count
= 0;
822 uint32_t v6_count
= 0;
825 if (in
== NULL
) return NULL
;
827 for (i
= 0; i
< in
->count
; i
++)
829 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
830 if (a
->ai_family
== AF_INET6
)
832 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
833 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
))) v4mapped_count
++;
839 if ((flags
& AI_V4MAPPED
) && ((v6_count
== 0) || (flags
& AI_ALL
))) filter_mapped
= 0;
843 if ((filter_mapped
== 1) && (v4mapped_count
> 0))
845 i
= in
->count
- v4mapped_count
;
846 if (i
== 0) return NULL
;
848 out
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
849 if (out
== NULL
) return in
;
852 out
->refcount
= in
->refcount
;
854 out
->entry
= (si_item_t
**)calloc(out
->count
, sizeof(si_item_t
*));
855 if (out
->entry
== NULL
)
863 for (i
= 0; i
< in
->count
; i
++)
865 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
866 if (a
->ai_family
== AF_INET6
)
868 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
869 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
)))
871 si_item_release(in
->entry
[i
]);
876 out
->entry
[out
->curr
++] = in
->entry
[i
];
885 qsort(&out
->entry
[0], out
->count
, sizeof(si_item_t
*), _gai_addr_sort
);
890 * Simple lookup via gethostbyname2(3) mechanism.
894 _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
)
896 si_item_t
*h4_item
= NULL
, *h6_item
= NULL
;
897 struct hostent
*h4
= NULL
, *h6
= NULL
;
898 si_list_t
*out
= NULL
;
901 if ((flags
& AI_NUMERICSERV
) != 0)
905 if (err
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
908 port
= *(uint16_t*)servptr
;
912 if (_gai_serv_to_port(servptr
, proto
, &port
) != 0)
914 if (err
) *err
= SI_STATUS_EAI_NONAME
;
919 if ((flags
& AI_NUMERICHOST
) != 0)
921 if (family
== AF_INET
)
923 h4_item
= si_host_byaddr(si
, nodeptr
, AF_INET
, interface
, NULL
);
925 else if (family
== AF_INET6
)
927 h6_item
= si_host_byaddr(si
, nodeptr
, AF_INET6
, interface
, NULL
);
932 if ((family
== AF_INET
) || (family
== AF_UNSPEC
))
934 h4_item
= si_host_byname(si
, nodeptr
, AF_INET
, interface
, NULL
);
937 if ((family
== AF_INET6
) || (family
== AF_UNSPEC
))
939 h6_item
= si_host_byname(si
, nodeptr
, AF_INET6
, interface
, NULL
);
945 h4
= (struct hostent
*)((uintptr_t)h4_item
+ sizeof(si_item_t
));
950 h6
= (struct hostent
*)((uintptr_t)h6_item
+ sizeof(si_item_t
));
953 out
= si_addrinfo_list_from_hostent(si
, flags
, socktype
, proto
, port
, 0, h4
, h6
);
954 si_item_release(h4_item
);
955 si_item_release(h6_item
);
957 return _gai_sort_list(out
, flags
);
962 si_srv_byname(si_mod_t
*si
, const char *qname
, const char *interface
, uint32_t *err
)
964 if (si
== NULL
) return 0;
965 if (si
->vtable
->sim_srv_byname
== NULL
) return 0;
967 return si
->vtable
->sim_srv_byname(si
, qname
, interface
, err
);
972 si_wants_addrinfo(si_mod_t
*si
)
974 if (si
== NULL
) return 0;
975 if (si
->vtable
->sim_addrinfo
== NULL
) return 0;
976 if (si
->vtable
->sim_wants_addrinfo
== NULL
) return 0;
978 return si
->vtable
->sim_wants_addrinfo(si
);
982 _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
)
989 si_list_t
*list
= NULL
;
990 si_list_t
*result
= NULL
;
992 /* Minimum SRV priority is zero. Start below that. */
996 if (node
== NULL
|| serv
== NULL
) return NULL
;
998 asprintf(&qname
, "%s.%s", serv
, node
);
999 list
= si_srv_byname(si
, qname
, interface
, err
);
1002 /* Iterate the SRV records starting at lowest priority and attempt to
1003 * lookup the target host name. Returns the first successful lookup.
1004 * It's an O(n^2) algorithm but data sets are small (less than 100) and
1005 * sorting overhead is dwarfed by network I/O for each element.
1007 while (list
!= NULL
&& result
== NULL
)
1009 /* Find the next lowest priority level. */
1010 /* Maximum SRV priority is UINT16_MAX. Start above that. */
1013 for (i
= 0; i
< list
->count
; ++i
)
1015 item
= list
->entry
[i
];
1016 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1018 if (srv
->priority
> lastprio
&& srv
->priority
< currprio
)
1020 currprio
= srv
->priority
;
1024 if (currprio
== INT_MAX
)
1026 /* All priorities have been evaluated. Done. */
1031 lastprio
= currprio
;
1034 /* Lookup hosts at the current priority level. Return first match. */
1035 for (i
= 0; i
< list
->count
; ++i
)
1037 item
= list
->entry
[i
];
1038 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1040 if (srv
->priority
== currprio
)
1042 /* So that _gai_simple expects an integer service. */
1043 flags
|= AI_NUMERICSERV
;
1045 result
= _gai_simple(si
, srv
->target
, &srv
->port
, family
, socktype
, proto
, flags
, interface
, err
);
1056 si_list_release(list
);
1062 #pragma mark -- NAT64 --
1064 static bool (*nat64_v4_requires_synthesis
)(const struct in_addr
*ipv4_addr
) = NULL
;
1065 static int (*nat64_v4_synthesize
)(uint32_t *index
, const struct in_addr
*ipv4
, struct in6_addr
**out_ipv6_addrs
) = NULL
;
1067 #if !TARGET_OS_SIMULATOR
1068 static void _gai_load_libnetwork_once(void)
1070 // If the function pointers are already loaded, we don't need to call dlopen
1071 if (nat64_v4_requires_synthesis
!= NULL
&& nat64_v4_synthesize
!= NULL
) {
1075 // Using dlopen will trigger libnetwork's init functions which should call
1076 // si_set_nat64_v4_synthesize and si_set_nat64_v4_requires_synthesis
1077 static void *handle
;
1078 os_log_debug(gai_log(), "Opening libnetwork.dylib");
1079 handle
= dlopen("/usr/lib/libnetwork.dylib", RTLD_LAZY
| RTLD_LOCAL
);
1080 if (handle
== NULL
) {
1081 const char *error_description
= dlerror();
1082 os_log_error(gai_log(), "dlopen(\"...libnetwork.dylib\") failed: %{public}s",
1083 error_description ? error_description
: "?");
1085 if (nat64_v4_requires_synthesis
== NULL
) {
1086 os_log_error(gai_log(), "libnetwork.dylib did not set nat64_v4_requires_synthesis");
1088 if (nat64_v4_synthesize
== NULL
) {
1089 os_log_error(gai_log(), "libnetwork.dylib did not set nat64_v4_synthesize");
1094 static void _gai_load_libnetwork(void)
1096 static pthread_once_t load_once
= PTHREAD_ONCE_INIT
;
1097 pthread_once(&load_once
, _gai_load_libnetwork_once
);
1100 static void _gai_load_libnetwork(void)
1105 static bool _gai_nat64_v4_address_requires_synthesis(const struct in_addr
*ipv4_addr
)
1107 _gai_load_libnetwork();
1108 if (nat64_v4_requires_synthesis
== NULL
) {
1111 bool result
= nat64_v4_requires_synthesis(ipv4_addr
);
1112 os_log_debug(gai_log(), "nat64_v4_requires_synthesis(%{network:in_addr}d) == %{bool}d", ipv4_addr
->s_addr
, result
);
1116 static int _gai_nat64_v4_synthesize(uint32_t *index
, const struct in_addr
*ipv4
, struct in6_addr
**out_ipv6_addrs
)
1118 _gai_load_libnetwork();
1119 if (nat64_v4_synthesize
== NULL
) {
1122 int result
= nat64_v4_synthesize(index
, ipv4
, out_ipv6_addrs
);
1123 os_log_debug(gai_log(), "nat64_v4_synthesize(%d, %{network:in_addr}d, ...) returned %d", index
!= NULL ?
*index
: 0,
1124 ipv4
->s_addr
, result
);
1125 return nat64_v4_synthesize(index
, ipv4
, out_ipv6_addrs
);
1129 void si_set_nat64_v4_requires_synthesis(bool (*new_requires_synthesis
)(const struct in_addr
*ipv4_addr
))
1131 if (new_requires_synthesis
== NULL
) {
1132 os_log_fault(gai_log(), "new_requires_synthesis is NULL");
1135 nat64_v4_requires_synthesis
= new_requires_synthesis
;
1139 void si_set_nat64_v4_synthesize(int (*new_synthesize
)(uint32_t *index
, const struct in_addr
*ipv4
,
1140 struct in6_addr
**out_ipv6_addrs
))
1142 if (new_synthesize
== NULL
) {
1143 os_log_fault(gai_log(), "new_synthesize is NULL");
1146 nat64_v4_synthesize
= new_synthesize
;
1150 bool _gai_nat64_can_v4_address_be_synthesized(const struct in_addr
*ipv4_addr
)
1152 if (ipv4_addr
== NULL
) {
1153 os_log_fault(gai_log(), "ipv4_addr is NULL");
1157 const in_addr_t addr_hbo
= ntohl(ipv4_addr
->s_addr
); // host byte order
1159 if (IN_ZERONET(addr_hbo
) || // 0.0.0.0/8 Source hosts on local network
1160 IN_LOOPBACK(addr_hbo
) || // 127.0.0.0/8 Loopback
1161 IN_LINKLOCAL(addr_hbo
) || // 169.254.0.0/16 Link Local
1162 IN_DS_LITE(addr_hbo
) || // 192.0.0.0/29 DS-Lite
1163 IN_6TO4_RELAY_ANYCAST(addr_hbo
) || // 192.88.99.0/24 6to4 Relay Anycast
1164 IN_MULTICAST(addr_hbo
) || // 224.0.0.0/4 Multicast
1165 INADDR_BROADCAST
== addr_hbo
) { // 255.255.255.255/32 Limited Broadcast
1173 _gai_nat64_synthesis(si_mod_t
*si
, const char *node
, const void *servptr
, int numericserv
,
1174 uint32_t family
, uint32_t socktype
, uint32_t proto
, uint32_t flags
, const char *interface
)
1181 /* validate AI_NUMERICHOST */
1182 if ((flags
& AI_NUMERICHOST
) != 0)
1187 /* validate family */
1188 if ((AF_UNSPEC
!= family
) && (AF_INET6
!= family
))
1193 /* validate that node is an IPv4 address */
1195 if (1 != inet_pton(AF_INET
, node
, &a4
))
1200 /* validate that IPv4 address is eligible for NAT64 synthesis */
1201 if (!_gai_nat64_can_v4_address_be_synthesized(&a4
)) {
1205 /* validate that there is at least an IPv6 address configured */
1206 uint32_t num_inet6
= 0;
1207 if ((si_inet_config(NULL
, &num_inet6
) < 0) || (0 == num_inet6
))
1212 /* validate interface name and convert to index */
1213 uint32_t ifindex
= 0;
1214 if (NULL
!= interface
)
1216 ifindex
= if_nametoindex(interface
);
1223 /* validate serv and convert to port */
1225 if (0 == numericserv
)
1227 if (_gai_serv_to_port((const char *)servptr
, proto
, &port
) != 0)
1233 flags
|= AI_NUMERICSERV
;
1236 else if (NULL
!= servptr
)
1238 port
= *((const uint16_t *)servptr
);
1241 /* query NAT64 prefixes */
1242 struct in6_addr
*synthesized
= NULL
;
1243 const size_t count
= _gai_nat64_v4_synthesize(&ifindex
, &a4
, &synthesized
);
1244 if (count
<= 0 || (NULL
== synthesized
)) {
1248 /* add every address to results */
1249 si_list_t
*out_list
= NULL
;
1250 for (size_t i
= 0; i
< count
; i
++)
1252 si_list_t
*temp_list
= si_addrinfo_list(si
, flags
, socktype
, proto
, NULL
, &synthesized
[i
], port
, 0, NULL
, NULL
);
1253 if (NULL
== temp_list
)
1257 if (NULL
!= out_list
)
1259 out_list
= si_list_concat(out_list
, temp_list
);
1260 si_list_release(temp_list
);
1264 out_list
= temp_list
;
1270 /* return to standard code path if no NAT64 addresses could be synthesized */
1271 if (NULL
== out_list
)
1276 /* add IPv4 addresses and IPv4-mapped IPv6 addresses if appropriate */
1277 if (((AF_UNSPEC
== family
) && ((flags
& AI_ADDRCONFIG
) == 0)) ||
1278 ((AF_INET6
== family
) && ((flags
& AI_ALL
) != 0) && ((flags
& AI_V4MAPPED
) != 0)))
1280 si_list_t
*list4
= si_addrinfo_list(si
, flags
, socktype
, proto
, &a4
, NULL
, port
, 0, NULL
, NULL
);
1283 out_list
= si_list_concat(out_list
, list4
);
1284 si_list_release(list4
);
1288 return _gai_sort_list(out_list
, flags
);
1292 _gai_nat64_second_pass(si_list_t
*out
, si_mod_t
*si
, const char *serv
, uint32_t family
, uint32_t socktype
,
1293 uint32_t proto
, uint32_t flags
, const char *interface
)
1295 if (out
== NULL
|| out
->count
== 0)
1300 /* validate AI_NUMERICHOST */
1301 if ((flags
& AI_NUMERICHOST
) != 0)
1306 /* validate family */
1307 if ((AF_UNSPEC
!= family
) && (AF_INET6
!= family
))
1312 /* skip if we already have an IPv6 address (unless it is v4-mapped) */
1313 for (uint32_t i
= 0; i
< out
->count
; i
++)
1315 si_addrinfo_t
*a
= (si_addrinfo_t
*)((uintptr_t)out
->entry
[i
] + sizeof(si_item_t
));
1316 if (a
->ai_family
== AF_INET6
)
1318 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
1319 if (!IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
))
1326 si_list_t
*out_list
= NULL
;
1327 const uint32_t flags2
= flags
& (~(AI_V4MAPPED
| AI_V4MAPPED_CFG
));
1329 for (uint32_t i
= 0; i
< out
->count
; i
++)
1331 si_addrinfo_t
*a
= (si_addrinfo_t
*)((uintptr_t)out
->entry
[i
] + sizeof(si_item_t
));
1332 struct in_addr
*addr4
= NULL
;
1333 if (a
->ai_family
== AF_INET
)
1335 addr4
= &((struct sockaddr_in
*)a
->ai_addr
.x
)->sin_addr
;
1337 else if (a
->ai_family
== AF_INET6
)
1339 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
1340 addr4
= (struct in_addr
*)&(s6
->sin6_addr
.__u6_addr
.__u6_addr32
[3]);
1347 if (!_gai_nat64_v4_address_requires_synthesis(addr4
))
1352 char v4_str
[INET_ADDRSTRLEN
] = {0};
1353 if (NULL
== inet_ntop(AF_INET
, addr4
, v4_str
, sizeof(v4_str
)))
1358 uint32_t err
= SI_STATUS_NO_ERROR
;
1359 si_list_t
*temp_list
= si_addrinfo(si
, v4_str
, serv
, AF_INET6
, socktype
, proto
, flags2
, interface
, &err
);
1360 if (NULL
== temp_list
)
1364 if (err
!= SI_STATUS_NO_ERROR
)
1366 si_list_release(temp_list
);
1369 if (NULL
!= out_list
)
1371 out_list
= si_list_concat(out_list
, temp_list
);
1372 si_list_release(temp_list
);
1376 out_list
= temp_list
;
1382 #pragma mark -- /NAT64 --
1386 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
)
1388 const uint32_t family_ori
= family
, flags_ori
= flags
;
1389 int numerichost
, numericserv
= 0;
1391 const void *nodeptr
= NULL
, *servptr
= NULL
;
1393 struct in_addr a4
, *p4
;
1394 struct in6_addr a6
, *p6
;
1398 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
1402 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
1406 /* treat empty strings as NULL */
1407 if ((node
!= NULL
) && (node
[0] == '\0')) node
= NULL
;
1408 if ((serv
!= NULL
) && (serv
[0] == '\0')) serv
= NULL
;
1410 /* make sure we have a query */
1411 if ((node
== NULL
) && (serv
== NULL
))
1413 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1417 /* verify family is supported */
1425 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
1429 /* verify socket type is supported */
1438 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1442 /* verify protocol is supported */
1445 case IPPROTO_UNSPEC
:
1449 case IPPROTO_ICMPV6
:
1452 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1456 /* verify socket type compatible with protocol */
1457 if (((socktype
== SOCK_DGRAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_UDP
)) || ((socktype
== SOCK_STREAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_TCP
)))
1459 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1463 /* replace AI_V4MAPPED_CFG with AI_V4MAPPED */
1464 if ((flags
& AI_V4MAPPED_CFG
) != 0)
1466 flags
= (flags
& ~AI_V4MAPPED_CFG
) | AI_V4MAPPED
;
1469 /* check AI_V4MAPPED and AI_ALL */
1470 if (family
!= AF_INET6
)
1472 /* unset AI_V4MAPPED and AI_ALL unless family is AF_INET6 */
1473 flags
&= ~(AI_V4MAPPED
| AI_ALL
);
1475 else if ((flags
& AI_V4MAPPED
) == 0)
1477 /* unset AI_ALL unless family is AF_INET6 and AI_V4MAPPED is set */
1481 memset(&a4
, 0, sizeof(struct in_addr
));
1482 memset(&a6
, 0, sizeof(struct in6_addr
));
1484 /* determine the protocol if possible */
1485 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_DGRAM
)) proto
= IPPROTO_UDP
;
1486 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_STREAM
)) proto
= IPPROTO_TCP
;
1490 if ((flags
& AI_SRV
) != 0)
1493 out
= _gai_srv(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
1494 return _gai_sort_list(out
, flags
);
1498 /* Usual service lookup */
1499 numericserv
= _gai_numericserv(serv
, &port
);
1502 if ((flags
& AI_NUMERICSERV
) && (numericserv
== 0))
1504 /* FreeBSD returns SI_STATUS_EAI_SERVICE */
1505 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1509 if ((serv
!= NULL
) && (strcmp(serv
, "0") != 0))
1511 if (numericserv
== 1)
1513 flags
|= AI_NUMERICSERV
;
1522 /* NAT64 IPv6 address synthesis support */
1523 si_list_t
*nat64_list
= _gai_nat64_synthesis(si
, node
, servptr
, numericserv
, family
, socktype
, proto
, flags
, interface
);
1524 if (NULL
!= nat64_list
)
1529 numerichost
= _gai_numerichost(node
, &family
, flags
, &a4
, &a6
, &scope
);
1530 if ((numerichost
== -1) || ((flags
& AI_NUMERICHOST
) && (numerichost
== 0)))
1532 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1536 if (numerichost
== 1)
1538 flags
|= AI_NUMERICHOST
;
1539 if (family
== AF_INET
)
1543 else if (family
== AF_INET6
)
1553 if ((numerichost
== 1) && (numericserv
== 0))
1555 /* only service lookup needed. convert to port and perform a trivial getaddrinfo */
1556 if (_gai_serv_to_port(serv
, proto
, &port
) != 0)
1558 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1563 flags
|= AI_NUMERICSERV
;
1569 if ((numerichost
== 1) && (numericserv
== 1))
1575 if (family
== AF_INET
) p6
= NULL
;
1576 if (family
== AF_INET6
) p4
= NULL
;
1577 if (node
== NULL
) cname
= "localhost";
1579 /* allow nodename to be a mapped IPv4 address, e.g. "::ffff:10.0.0.1" */
1580 if (p6
!= NULL
) flags
|= AI_V4MAPPED
;
1582 /* handle trivial questions */
1583 out
= si_addrinfo_list(si
, flags
, socktype
, proto
, p4
, p6
, port
, scope
, cname
, cname
);
1584 return _gai_sort_list(out
, flags
);
1586 else if (si_wants_addrinfo(si
))
1588 /* or let the current module handle the host lookups intelligently */
1589 out
= si
->vtable
->sim_addrinfo(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1591 /* run a second NAT64 pass in case a hostname was resolved over VPN to an IPv4 address
1592 and it needs to be synthesized in order to be used on IPv6-only cellular */
1593 si_list_t
*nat64_list2
= _gai_nat64_second_pass(out
, si
, serv
, family_ori
, socktype
,
1594 proto
, flags_ori
, interface
);
1595 if (nat64_list2
!= NULL
)
1597 out
= si_list_concat(out
, nat64_list2
);
1600 return _gai_sort_list(out
, flags
);
1603 /* fall back to a default path */
1604 out
= _gai_simple(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1605 return _gai_sort_list(out
, flags
);
1608 static struct addrinfo
*
1609 si_item_to_addrinfo(si_item_t
*item
)
1612 struct addrinfo
*out
;
1614 if (item
== NULL
) return NULL
;
1616 a
= (si_addrinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1618 out
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
1619 if (out
== NULL
) return NULL
;
1621 out
->ai_flags
= a
->ai_flags
;
1622 out
->ai_family
= a
->ai_family
;
1623 out
->ai_socktype
= a
->ai_socktype
;
1624 out
->ai_protocol
= a
->ai_protocol
;
1625 out
->ai_addrlen
= a
->ai_addrlen
;
1627 out
->ai_addr
= (struct sockaddr
*)calloc(1, out
->ai_addrlen
);
1628 if (out
->ai_addr
== NULL
)
1634 memcpy(out
->ai_addr
, a
->ai_addr
.x
, out
->ai_addrlen
);
1636 if (a
->ai_canonname
!= NULL
)
1638 out
->ai_canonname
= strdup(a
->ai_canonname
);
1639 if (out
->ai_canonname
== NULL
)
1650 si_list_to_addrinfo(si_list_t
*list
)
1652 struct addrinfo
*tail
, *out
;
1655 if (list
== NULL
) return NULL
;
1656 if (list
->count
== 0) return NULL
;
1658 i
= list
->count
- 1;
1660 out
= si_item_to_addrinfo(list
->entry
[i
]);
1663 for (i
--; i
>= 0; i
--)
1665 out
= si_item_to_addrinfo(list
->entry
[i
]);
1672 out
->ai_next
= tail
;
1679 /* getipnodebyname */
1682 make_hostent(si_mod_t
*si
, const char *name
, struct in_addr addr
)
1688 if (name
== NULL
) return NULL
;
1692 addrs
[0] = (char *)&(addr
.s_addr
);
1696 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
);
1700 make_hostent6(si_mod_t
*si
, const char *name
, struct in6_addr addr
)
1706 if (name
== NULL
) return NULL
;
1710 addrs
[0] = (char *)&(addr
.__u6_addr
.__u6_addr32
[0]);
1714 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
);
1718 lower_case(const char *s
)
1723 if (s
== NULL
) return NULL
;
1725 t
= malloc(strlen(s
) + 1);
1726 if (t
== NULL
) return NULL
;
1728 for (i
= 0; s
[i
] != '\0'; i
++)
1730 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
1740 merge_alias(const char *name
, build_hostent_t
*h
)
1744 if (name
== NULL
) return 0;
1745 if (h
== NULL
) return 0;
1746 if (h
->host
.h_name
== NULL
) return 0;
1748 if ((h
->host
.h_name
!= NULL
) && (string_equal(name
, h
->host
.h_name
))) return 0;
1750 for (i
= 0; i
< h
->alias_count
; i
++)
1752 if (string_equal(name
, h
->host
.h_aliases
[i
])) return 0;
1755 if (h
->alias_count
== 0) h
->host
.h_aliases
= (char **)calloc(2, sizeof(char *));
1756 else h
->host
.h_aliases
= (char **)reallocf(h
->host
.h_aliases
, (h
->alias_count
+ 2) * sizeof(char *));
1758 if (h
->host
.h_aliases
== NULL
)
1764 h
->host
.h_aliases
[h
->alias_count
] = lower_case(name
);
1766 h
->host
.h_aliases
[h
->alias_count
] = NULL
;
1772 append_addr(const char *addr
, uint32_t len
, build_hostent_t
*h
)
1774 if (addr
== NULL
) return 0;
1775 if (h
== NULL
) return 0;
1777 if (h
->addr_count
== 0) h
->host
.h_addr_list
= (char **)calloc(2, sizeof(char *));
1778 else h
->host
.h_addr_list
= (char **)reallocf(h
->host
.h_addr_list
, (h
->addr_count
+ 2) * sizeof(char *));
1780 if (h
->host
.h_addr_list
== NULL
)
1786 h
->host
.h_addr_list
[h
->addr_count
] = malloc(len
);
1787 if (h
->host
.h_addr_list
[h
->addr_count
] == NULL
) return -1;
1789 memcpy(h
->host
.h_addr_list
[h
->addr_count
], addr
, len
);
1791 h
->host
.h_addr_list
[h
->addr_count
] = NULL
;
1797 free_build_hostent(build_hostent_t
*h
)
1802 if (h
== NULL
) return;
1804 if (h
->host
.h_name
!= NULL
) free(h
->host
.h_name
);
1805 h
->host
.h_name
= NULL
;
1807 aliases
= h
->host
.h_aliases
;
1808 if (aliases
!= NULL
)
1810 while (*aliases
!= NULL
) free(*aliases
++);
1811 free(h
->host
.h_aliases
);
1814 h
->host
.h_aliases
= NULL
;
1816 if (h
->host
.h_addr_list
!= NULL
)
1818 for (i
= 0; h
->host
.h_addr_list
[i
] != NULL
; i
++) free(h
->host
.h_addr_list
[i
]);
1819 free(h
->host
.h_addr_list
);
1822 h
->host
.h_addr_list
= NULL
;
1828 si_ipnode_byname(si_mod_t
*si
, const char *name
, int family
, int flags
, const char *interface
, uint32_t *err
)
1830 int i
, status
, want
;
1832 struct in_addr addr4
;
1833 struct in6_addr addr6
;
1834 si_item_t
*item4
, *item6
;
1835 build_hostent_t
*out
;
1839 memset(&addr4
, 0, sizeof(struct in_addr
));
1840 memset(&addr6
, 0, sizeof(struct in6_addr
));
1842 if (err
!= NULL
) *err
= 0;
1844 if (family
== AF_INET
)
1846 status
= inet_aton(name
, &addr4
);
1849 /* create a host entry */
1850 item4
= make_hostent(si
, name
, addr4
);
1853 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1860 else if (family
== AF_INET6
)
1862 status
= inet_pton(family
, name
, &addr6
);
1865 /* create a host entry */
1866 item6
= make_hostent6(si
, name
, addr6
);
1869 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1876 status
= inet_aton(name
, &addr4
);
1879 if (!(flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
)))
1881 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1885 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1886 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1887 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1888 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), &(addr4
.s_addr
), IPV4_ADDR_LEN
);
1890 /* create a host entry */
1891 item6
= make_hostent6(si
, name
, addr6
);
1894 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1903 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1908 * IF AI_ADDRCONFIG is set, we need to know what interface flavors we really have.
1914 if (flags
& AI_ADDRCONFIG
)
1916 if (si_inet_config(&if4
, &if6
) < 0)
1918 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1922 /* Bail out if there are no interfaces */
1923 if ((if4
== 0) && (if6
== 0))
1925 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1931 * Figure out what we want.
1932 * If user asked for AF_INET, we only want V4 addresses.
1934 want
= WANT_A4_ONLY
;
1936 if (family
== AF_INET
)
1938 if ((flags
& AI_ADDRCONFIG
) && (if4
== 0))
1940 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1944 else if (family
== AF_INET6
)
1946 /* family == AF_INET6 */
1947 want
= WANT_A6_ONLY
;
1949 if (flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
))
1953 want
= WANT_A6_PLUS_MAPPED_A4
;
1957 want
= WANT_A6_OR_MAPPED_A4_IF_NO_A6
;
1962 if ((flags
& AI_ADDRCONFIG
) && (if6
== 0))
1964 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1971 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1978 /* fetch IPv6 data if required */
1979 if ((want
== WANT_A6_ONLY
) || (want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) || (want
== WANT_A6_PLUS_MAPPED_A4
))
1981 item6
= si_host_byname(si
, name
, AF_INET6
, interface
, (uint32_t *)err
);
1984 /* fetch IPv4 data if required */
1985 if ((want
== WANT_A4_ONLY
) || (want
== WANT_A6_PLUS_MAPPED_A4
) || ((want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) && (item6
== NULL
)))
1987 item4
= si_host_byname(si
, name
, AF_INET
, interface
, (uint32_t *)err
);
1990 if (want
== WANT_A4_ONLY
)
1992 si_item_release(item6
);
1993 if ((item4
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1997 if (want
== WANT_A6_ONLY
)
1999 si_item_release(item4
);
2000 if ((item6
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
2004 if ((item6
== NULL
) && (item4
== NULL
))
2006 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
2010 /* output item will have IPv6 + mapped IPv4 addresses */
2012 out
= (build_hostent_t
*)calloc(1, sizeof(build_hostent_t
));
2015 si_item_release(item4
);
2016 si_item_release(item6
);
2017 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
2023 h
= (struct hostent
*)((uintptr_t)item4
+ sizeof(si_item_t
));
2025 out
->host
.h_name
= lower_case(h
->h_name
);
2027 if (h
->h_aliases
!= NULL
)
2029 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
2032 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
2034 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
2035 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
2036 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
2037 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), h
->h_addr_list
[i
], IPV4_ADDR_LEN
);
2038 append_addr((const char *)&addr6
, IPV6_ADDR_LEN
, out
);
2044 h
= (struct hostent
*)((uintptr_t)item6
+ sizeof(si_item_t
));
2046 if (out
->host
.h_name
== NULL
) out
->host
.h_name
= lower_case(h
->h_name
);
2048 if (h
->h_aliases
!= NULL
)
2050 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
2053 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++) append_addr(h
->h_addr_list
[i
], IPV6_ADDR_LEN
, out
);
2056 si_item_release(item4
);
2057 si_item_release(item6
);
2061 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
);
2063 free_build_hostent(out
);