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
;
65 int _inet_aton_check(const char *cp
, struct in_addr
*addr
, int strict
);
74 __private_extern__
int
75 si_inet_config(uint32_t *inet4
, uint32_t *inet6
)
78 struct ifaddrs
*ifa
, *ifap
;
80 pthread_mutex_lock(&net_config_mutex
);
84 if (net_config_token
< 0)
86 status
= notify_register_check(kNotifySCNetworkChange
, &net_config_token
);
87 if (status
!= 0) net_config_token
= -1;
90 if (net_config_token
>= 0)
92 status
= notify_check(net_config_token
, &checkit
);
93 if (status
!= 0) checkit
= 1;
100 if (getifaddrs(&ifa
) < 0)
109 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
111 if (ifap
->ifa_addr
== NULL
) continue;
112 if ((ifap
->ifa_flags
& IFF_UP
) == 0) continue;
114 if (ifap
->ifa_addr
->sa_family
== AF_INET
)
118 else if (ifap
->ifa_addr
->sa_family
== AF_INET6
)
128 if (inet4
!= NULL
) *inet4
= net_v4_count
;
129 if (inet6
!= NULL
) *inet6
= net_v6_count
;
131 pthread_mutex_unlock(&net_config_mutex
);
137 freeaddrinfo(struct addrinfo
*a
)
139 struct addrinfo
*next
;
144 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
145 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
152 gai_strerror(int32_t err
)
156 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
157 case EAI_AGAIN
: return "Temporary failure in name resolution";
158 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
159 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
160 case EAI_FAMILY
: return "ai_family not supported";
161 case EAI_MEMORY
: return "Memory allocation failure";
162 case EAI_NODATA
: return "No address associated with nodename";
163 case EAI_NONAME
: return "nodename nor servname provided, or not known";
164 case EAI_SERVICE
: return "servname not supported for ai_socktype";
165 case EAI_SOCKTYPE
: return "ai_socktype not supported";
166 case EAI_SYSTEM
: return "System error";
167 case EAI_BADHINTS
: return "Bad hints";
168 case EAI_PROTOCOL
: return "ai_protocol not supported";
169 case EAI_OVERFLOW
: return "argument buffer overflow";
172 return "Unknown error";
178 * We handle some "trival" cases locally. If the caller passes
179 * NI_NUMERICHOST (only), then this call turns into a getservbyport
180 * to get the service name + inet_pton() to create a host string.
181 * If the caller passes NI_NUMERICSERV (only), then we zero out the port
182 * number, complete the getnameinfo, and use printf() to create a service
183 * string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
184 * we inet_ntop() and printf() and return the results.
187 si_nameinfo(si_mod_t
*si
, const struct sockaddr
*sa
, int flags
, const char *interface
, uint32_t *err
)
189 si_item_t
*out
= NULL
;
190 const struct sockaddr
*lookup_sa
;
191 struct sockaddr_in s4
;
194 const uint64_t unused
= 0;
201 int do_host_lookup
= ((flags
& NI_NUMERICHOST
) == 0);
202 int do_serv_lookup
= ((flags
& NI_NUMERICSERV
) == 0);
205 if ((si
== NULL
) || (sa
== NULL
))
207 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
211 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
215 if (sa
->sa_family
== AF_INET
)
217 struct sockaddr_in
*s4
= (struct sockaddr_in
*)sa
;
218 memcpy(&a4
, &s4
->sin_addr
, sizeof(a4
));
222 else if (sa
->sa_family
== AF_INET6
)
224 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)sa
;
225 memcpy(&a6
, &s6
->sin6_addr
, sizeof(a6
));
226 port
= s6
->sin6_port
;
228 /* Look for scope id in IPv6 Link Local, Multicast Node Local, and Multicast Link Local */
229 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_NODELOCAL(&s6
->sin6_addr
) || IN6_IS_ADDR_MC_LINKLOCAL(&s6
->sin6_addr
))
231 ifnum
= ntohs(a6
.__u6_addr
.__u6_addr16
[1]);
234 ifnum
= s6
->sin6_scope_id
;
235 a6
.__u6_addr
.__u6_addr16
[1] = htons(ifnum
);
238 if ((ifnum
!= s6
->sin6_scope_id
) && (s6
->sin6_scope_id
!= 0))
240 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
245 /* v4 mapped and compat addresses are converted to plain v4 */
246 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
) || IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
))
248 memcpy(&a4
, &s6
->sin6_addr
.s6_addr
[12], sizeof(a4
));
250 memset(&s4
, 0, sizeof(s4
));
251 s4
.sin_len
= sizeof(s4
);
252 s4
.sin_family
= AF_INET
;
254 memcpy(&s4
.sin_addr
, &a4
, sizeof(s4
.sin_addr
));
255 lookup_sa
= (const struct sockaddr
*)&s4
;
264 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
268 if (do_host_lookup
== 1)
270 si_item_t
*item
= si_host_byaddr(si
, addr
, lookup_sa
->sa_family
, interface
, NULL
);
274 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
275 host
= strdup(h
->h_name
);
276 si_item_release(item
);
279 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
285 if ((do_serv_lookup
== 1) && (port
!= 0))
287 si_item_t
*item
= si_service_byport(si
, port
, NULL
);
291 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
292 serv
= strdup(s
->s_name
);
293 si_item_release(item
);
297 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
304 * Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
305 * if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
307 if ((host
== NULL
) && ((flags
& NI_NAMEREQD
) == 0))
309 char tmp
[INET6_ADDRSTRLEN
+ 1 + IF_NAMESIZE
+ 1];
311 if (sa
->sa_family
== AF_INET
)
313 char buf
[INET_ADDRSTRLEN
];
314 if (inet_ntop(AF_INET
, &a4
, buf
, sizeof(buf
)) != 0)
319 else if (sa
->sa_family
== AF_INET6
)
321 char buf
[INET6_ADDRSTRLEN
];
323 /* zero the embedded scope ID */
326 a6
.__u6_addr
.__u6_addr16
[1] = 0;
329 if (inet_ntop(AF_INET6
, &a6
, buf
, sizeof(buf
)) != 0)
333 char ifname
[IF_NAMESIZE
];
334 if (if_indextoname(ifnum
, ifname
) != NULL
)
336 asprintf(&host
, "%s%%%s", buf
, ifname
);
341 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
353 /* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
356 asprintf(&serv
, "%hu", ntohs(port
));
359 if ((host
== NULL
) || (serv
== NULL
))
363 if ((flags
& NI_NAMEREQD
) != 0)
365 *err
= SI_STATUS_EAI_NONAME
;
369 *err
= SI_STATUS_EAI_MEMORY
;
375 out
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_NAMEINFO
, 1, unused
, unused
, host
, serv
);
384 _gai_numericserv(const char *serv
, uint16_t *port
)
399 num
= strtol(serv
, &endptr
, 10);
400 if ((serv
[0] != '\0') && (*endptr
== '\0') && (num
>= 0) && (num
<= UINT16_MAX
))
403 if (port
!= NULL
) *port
= (uint16_t)num
;
411 _gai_serv_to_port(const char *serv
, uint32_t proto
, uint16_t *port
)
415 const char *protoname
= NULL
;
417 if (_gai_numericserv(serv
, port
)) return 0;
419 if (proto
== IPPROTO_UDP
) protoname
= "udp";
420 if (proto
== IPPROTO_TCP
) protoname
= "tcp";
422 item
= si_service_byname(si_search(), serv
, protoname
);
423 if (item
== NULL
) return -1;
425 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
426 if (port
) *port
= ntohs(s
->s_port
);
427 si_item_release(item
);
433 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
)
435 socket_data_t sockdata
;
436 struct sockaddr_in
*sa
;
441 len
= sizeof(struct sockaddr_in
);
442 memset(&sockdata
, 0, sizeof(socket_data_t
));
443 sa
= (struct sockaddr_in
*)&sockdata
;
446 sa
->sin_family
= AF_INET
;
447 sa
->sin_port
= htons(port
);
448 memcpy(&sa
->sin_addr
, addr
, sizeof(sa
->sin_addr
));
450 /* Kludge: Jam the interface number into sin_zero (4 bytes). */
452 memmove(sa
->sin_zero
, &v32
, sizeof(uint32_t));
454 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET
, sock
, proto
, len
, sockdata
, cname
);
458 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
)
460 socket_data_t sockdata
;
461 struct sockaddr_in6
*sa
;
466 len
= sizeof(struct sockaddr_in6
);
467 memset(&sockdata
, 0, sizeof(socket_data_t
));
468 sa
= (struct sockaddr_in6
*)&sockdata
;
471 sa
->sin6_family
= AF_INET6
;
472 sa
->sin6_port
= htons(port
);
473 memset(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[10]), 0xff, 2);
474 memcpy(&(sa
->sin6_addr
.__u6_addr
.__u6_addr8
[12]), addr
, sizeof(struct in_addr
));
476 /* sin6_scope_id is in host byte order */
477 sa
->sin6_scope_id
= iface
;
479 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
484 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
)
486 socket_data_t sockdata
;
487 struct sockaddr_in6
*sa
;
492 len
= sizeof(struct sockaddr_in6
);
493 memset(&sockdata
, 0, sizeof(socket_data_t
));
494 sa
= (struct sockaddr_in6
*)&sockdata
;
497 sa
->sin6_family
= AF_INET6
;
498 sa
->sin6_port
= htons(port
);
499 memcpy(&sa
->sin6_addr
, addr
, sizeof(sa
->sin6_addr
));
501 /* sin6_scope_id is in host byte order */
502 sa
->sin6_scope_id
= iface
;
504 if (IN6_IS_ADDR_LINKLOCAL(&sa
->sin6_addr
))
506 /* check for embedded scopeid */
507 uint16_t esid
= ntohs(sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
510 sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
511 if (iface
== 0) sa
->sin6_scope_id
= esid
;
515 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
519 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
)
522 si_item_t
*item
= NULL
;
523 si_list_t
*out4
= NULL
, *out6
= NULL
;
525 if ((flags
& AI_V4MAPPED
) && ((flags
& AI_ALL
) || (a6
== NULL
))) do_map
= 1;
529 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
531 item
= si_addrinfo_v6(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, scopeid
, cname6
);
532 out6
= si_list_add(out6
, item
);
533 si_item_release(item
);
536 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
538 item
= si_addrinfo_v6(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, scopeid
, cname6
);
539 out6
= si_list_add(out6
, item
);
540 si_item_release(item
);
543 if (proto
== IPPROTO_ICMPV6
)
545 item
= si_addrinfo_v6(si
, 0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, scopeid
, cname6
);
546 out6
= si_list_add(out6
, item
);
547 si_item_release(item
);
553 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
557 item
= si_addrinfo_v4(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
558 out4
= si_list_add(out4
, item
);
562 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
563 out6
= si_list_add(out6
, item
);
566 si_item_release(item
);
569 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
573 item
= si_addrinfo_v4(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
574 out4
= si_list_add(out4
, item
);
578 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
579 out6
= si_list_add(out6
, item
);
582 si_item_release(item
);
585 if (proto
== IPPROTO_ICMP
)
589 item
= si_addrinfo_v4(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
590 out4
= si_list_add(out4
, item
);
594 item
= si_addrinfo_v4_mapped(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
595 out6
= si_list_add(out6
, item
);
598 si_item_release(item
);
602 out6
= si_list_concat(out6
, out4
);
603 si_list_release(out4
);
610 * Determines whether the given host name is a numeric IPv4 or IPv6 address,
611 * based on the address family input value. If the input addres family is
612 * unspecified, a more specific value will be provided on output if possible.
613 * Returns 1 if host name is numeric or 0 if not, or -1 on error.
616 _gai_numerichost(const char* nodename
, uint32_t *family
, int flags
, struct in_addr
*a4
, struct in6_addr
*a6
, int *scope
)
618 int numerichost
, passive
;
622 if (nodename
== NULL
)
624 /* return loopback or passive addresses */
625 passive
= (flags
& AI_PASSIVE
);
627 if (((*family
== AF_UNSPEC
) || (*family
== AF_INET
)) || ((*family
== AF_INET6
) && (flags
& AI_V4MAPPED
) && (flags
& AI_ALL
)))
629 if (passive
) a4
->s_addr
= 0;
630 else a4
->s_addr
= htonl(INADDR_LOOPBACK
);
633 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET6
))
635 memset(a6
, 0, sizeof(*a6
));
636 if (!passive
) a6
->__u6_addr
.__u6_addr32
[3] = htonl(1);
644 * numeric IPv4 host valid for AF_UNSPEC and AF_INET
645 * also valid for AF_INET6 with AI_V4MAPPED
647 numerichost
= inet_pton(AF_INET
, nodename
, a4
);
648 if (numerichost
== 0)
650 /* inet_pton doesn't allow "a", "a.b", or "a.b.c" forms, so we re-check */
651 numerichost
= _inet_aton_check(nodename
, a4
, 1);
654 if (numerichost
== 1)
656 if (*family
== AF_UNSPEC
)
660 else if (*family
== AF_INET6
)
662 if (flags
& AI_V4MAPPED
)
664 memset(a6
, 0, sizeof(struct in6_addr
));
665 memset(&(a6
->__u6_addr
.__u6_addr8
[10]), 0xff, 2);
666 memcpy(&(a6
->__u6_addr
.__u6_addr8
[12]), a4
, sizeof(struct in_addr
));
677 /* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
678 numerichost
= inet_pton(AF_INET6
, nodename
, a6
);
679 if (numerichost
== 1)
681 /* check for scope/zone id */
682 char *p
= strrchr(nodename
, SCOPE_DELIMITER
);
690 for (x
= p
; (*x
!= '\0') && (d
== 1); x
++)
696 if (d
== 1) *scope
= atoi(p
);
697 else *scope
= if_nametoindex(p
);
700 if (*family
== AF_UNSPEC
) *family
= AF_INET6
;
701 else if (*family
== AF_INET
) numerichost
= -1;
710 /* si_addrinfo_list_from_hostent
711 * Returns an addrinfo list from IPv4 and IPv6 hostent entries
714 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
)
717 si_list_t
*out
= NULL
;
720 if ((h6
!= NULL
) && (h6
->h_addr_list
!= NULL
))
722 for (i
= 0; h6
->h_addr_list
[i
] != NULL
; i
++)
725 memcpy(&a6
, h6
->h_addr_list
[i
], h6
->h_length
);
726 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, NULL
, &a6
, port
, scope
, NULL
, h6
->h_name
);
727 out
= si_list_concat(out
, list
);
728 si_list_release(list
);
732 if ((h4
!= NULL
) && (h4
->h_addr_list
!= NULL
))
734 for (i
= 0; h4
->h_addr_list
[i
] != NULL
; i
++)
737 memcpy(&a4
, h4
->h_addr_list
[i
], h4
->h_length
);
738 list
= si_addrinfo_list(si
, flags
, socktype
, proto
, &a4
, NULL
, port
, 0, h4
->h_name
, NULL
);
739 out
= si_list_concat(out
, list
);
740 si_list_release(list
);
748 _gai_addr_sort(const void *a
, const void *b
)
750 si_item_t
**item_a
, **item_b
;
751 si_addrinfo_t
*p
, *q
;
752 struct sockaddr
*sp
, *sq
;
754 item_a
= (si_item_t
**)a
;
755 item_b
= (si_item_t
**)b
;
757 p
= (si_addrinfo_t
*)((uintptr_t)*item_a
+ sizeof(si_item_t
));
758 q
= (si_addrinfo_t
*)((uintptr_t)*item_b
+ sizeof(si_item_t
));
760 sp
= (struct sockaddr
*)p
->ai_addr
.x
;
761 sq
= (struct sockaddr
*)q
->ai_addr
.x
;
764 * sa_dst_compare(A,B) returns -1 if A is less desirable than B,
765 * 0 if they are equally desirable, and 1 if A is more desirable.
766 * qsort() expects the inverse, so we swap sp and sq.
768 return sa_dst_compare(sq
, sp
, 0);
772 _gai_sort_list(si_list_t
*in
, uint32_t flags
)
777 uint32_t v4mapped_count
= 0;
778 uint32_t v6_count
= 0;
781 if (in
== NULL
) return NULL
;
783 for (i
= 0; i
< in
->count
; i
++)
785 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
786 if (a
->ai_family
== AF_INET6
)
788 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
789 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
))) v4mapped_count
++;
795 if ((flags
& AI_V4MAPPED
) && ((v6_count
== 0) || (flags
& AI_ALL
))) filter_mapped
= 0;
799 if ((filter_mapped
== 1) && (v4mapped_count
> 0))
801 i
= in
->count
- v4mapped_count
;
802 if (i
== 0) return NULL
;
804 out
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
805 if (out
== NULL
) return in
;
808 out
->refcount
= in
->refcount
;
810 out
->entry
= (si_item_t
**)calloc(out
->count
, sizeof(si_item_t
*));
811 if (out
->entry
== NULL
)
819 for (i
= 0; i
< in
->count
; i
++)
821 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
822 if (a
->ai_family
== AF_INET6
)
824 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)a
->ai_addr
.x
;
825 if (IN6_IS_ADDR_V4MAPPED(&(s6
->sin6_addr
)))
827 si_item_release(in
->entry
[i
]);
832 out
->entry
[out
->curr
++] = in
->entry
[i
];
841 qsort(&out
->entry
[0], out
->count
, sizeof(si_item_t
*), _gai_addr_sort
);
846 * Simple lookup via gethostbyname2(3) mechanism.
849 _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
)
851 si_item_t
*h4_item
= NULL
, *h6_item
= NULL
;
852 struct hostent
*h4
= NULL
, *h6
= NULL
;
853 si_list_t
*out
= NULL
;
856 if ((flags
& AI_NUMERICSERV
) != 0)
858 port
= *(uint16_t*)servptr
;
862 if (_gai_serv_to_port(servptr
, proto
, &port
) != 0)
864 if (err
) *err
= SI_STATUS_EAI_NONAME
;
869 if ((flags
& AI_NUMERICHOST
) != 0)
871 if (family
== AF_INET
)
873 h4_item
= si_host_byaddr(si
, nodeptr
, AF_INET
, interface
, NULL
);
875 else if (family
== AF_INET6
)
877 h6_item
= si_host_byaddr(si
, nodeptr
, AF_INET6
, interface
, NULL
);
882 if ((family
== AF_INET
) || (family
== AF_UNSPEC
))
884 h4_item
= si_host_byname(si
, nodeptr
, AF_INET
, interface
, NULL
);
887 if ((family
== AF_INET6
) || (family
== AF_UNSPEC
))
889 h6_item
= si_host_byname(si
, nodeptr
, AF_INET6
, interface
, NULL
);
895 h4
= (struct hostent
*)((uintptr_t)h4_item
+ sizeof(si_item_t
));
900 h6
= (struct hostent
*)((uintptr_t)h6_item
+ sizeof(si_item_t
));
903 out
= si_addrinfo_list_from_hostent(si
, flags
, socktype
, proto
, port
, 0, h4
, h6
);
904 si_item_release(h4_item
);
905 si_item_release(h6_item
);
907 return _gai_sort_list(out
, flags
);
911 si_srv_byname(si_mod_t
*si
, const char *qname
, const char *interface
, uint32_t *err
)
913 if (si
== NULL
) return 0;
914 if (si
->vtable
->sim_srv_byname
== NULL
) return 0;
916 return si
->vtable
->sim_srv_byname(si
, qname
, interface
, err
);
920 si_wants_addrinfo(si_mod_t
*si
)
922 if (si
== NULL
) return 0;
923 if (si
->vtable
->sim_addrinfo
== NULL
) return 0;
924 if (si
->vtable
->sim_wants_addrinfo
== NULL
) return 0;
926 return si
->vtable
->sim_wants_addrinfo(si
);
930 _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
)
937 si_list_t
*list
= NULL
;
938 si_list_t
*result
= NULL
;
940 /* Minimum SRV priority is zero. Start below that. */
944 if (node
== NULL
|| serv
== NULL
) return NULL
;
946 asprintf(&qname
, "%s.%s", serv
, node
);
947 list
= si_srv_byname(si
, qname
, interface
, err
);
950 /* Iterate the SRV records starting at lowest priority and attempt to
951 * lookup the target host name. Returns the first successful lookup.
952 * It's an O(n^2) algorithm but data sets are small (less than 100) and
953 * sorting overhead is dwarfed by network I/O for each element.
955 while (list
!= NULL
&& result
== NULL
)
957 /* Find the next lowest priority level. */
958 /* Maximum SRV priority is UINT16_MAX. Start above that. */
961 for (i
= 0; i
< list
->count
; ++i
)
963 item
= list
->entry
[i
];
964 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
966 if (srv
->priority
> lastprio
&& srv
->priority
< currprio
)
968 currprio
= srv
->priority
;
972 if (currprio
== INT_MAX
)
974 /* All priorities have been evaluated. Done. */
982 /* Lookup hosts at the current priority level. Return first match. */
983 for (i
= 0; i
< list
->count
; ++i
)
985 item
= list
->entry
[i
];
986 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
988 if (srv
->priority
== currprio
)
990 /* So that _gai_simple expects an integer service. */
991 flags
|= AI_NUMERICSERV
;
993 result
= _gai_simple(si
, srv
->target
, &srv
->port
, family
, socktype
, proto
, flags
, interface
, err
);
1004 si_list_release(list
);
1011 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
)
1013 int numerichost
, numericserv
= 0;
1015 const void *nodeptr
= NULL
, *servptr
= NULL
;
1017 struct in_addr a4
, *p4
;
1018 struct in6_addr a6
, *p6
;
1022 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
1026 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
1030 /* treat empty strings as NULL */
1031 if ((node
!= NULL
) && (node
[0] == '\0')) node
= NULL
;
1032 if ((serv
!= NULL
) && (serv
[0] == '\0')) serv
= NULL
;
1034 /* make sure we have a query */
1035 if ((node
== NULL
) && (serv
== NULL
))
1037 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1041 /* verify family is supported */
1049 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
1053 /* verify socket type is supported */
1062 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1066 /* verify protocol is supported */
1069 case IPPROTO_UNSPEC
:
1073 case IPPROTO_ICMPV6
:
1076 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1080 /* verify socket type compatible with protocol */
1081 if (((socktype
== SOCK_DGRAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_UDP
)) || ((socktype
== SOCK_STREAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_TCP
)))
1083 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
1087 /* check AI_V4MAPPED and AI_ALL */
1088 if (family
!= AF_INET6
)
1090 /* unset AI_V4MAPPED and AI_ALL unless family is AF_INET6 */
1091 flags
&= ~(AI_V4MAPPED
| AI_ALL
);
1093 else if ((flags
& AI_V4MAPPED
) == 0)
1095 /* unset AI_ALL unless family is AF_INET6 and AI_V4MAPPED is set */
1099 memset(&a4
, 0, sizeof(struct in_addr
));
1100 memset(&a6
, 0, sizeof(struct in6_addr
));
1102 /* determine the protocol if possible */
1103 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_DGRAM
)) proto
= IPPROTO_UDP
;
1104 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_STREAM
)) proto
= IPPROTO_TCP
;
1108 if ((flags
& AI_SRV
) != 0)
1111 out
= _gai_srv(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
1112 return _gai_sort_list(out
, flags
);
1116 /* Usual service lookup */
1117 numericserv
= _gai_numericserv(serv
, &port
);
1120 if ((flags
& AI_NUMERICSERV
) && (numericserv
== 0))
1122 /* FreeBSD returns SI_STATUS_EAI_SERVICE */
1123 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1127 if ((serv
!= NULL
) && (strcmp(serv
, "0") != 0))
1129 if (numericserv
== 1)
1131 flags
|= AI_NUMERICSERV
;
1140 numerichost
= _gai_numerichost(node
, &family
, flags
, &a4
, &a6
, &scope
);
1141 if ((numerichost
== -1) || ((flags
& AI_NUMERICHOST
) && (numerichost
== 0)))
1143 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1147 if (numerichost
== 1)
1149 flags
|= AI_NUMERICHOST
;
1150 if (family
== AF_INET
)
1154 else if (family
== AF_INET6
)
1164 if ((numerichost
== 1) && (numericserv
== 0))
1166 /* only service lookup needed. convert to port and perform a trivial getaddrinfo */
1167 if (_gai_serv_to_port(serv
, proto
, &port
) != 0)
1169 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1174 flags
|= AI_NUMERICSERV
;
1180 if ((numerichost
== 1) && (numericserv
== 1))
1186 if (family
== AF_INET
) p6
= NULL
;
1187 if (family
== AF_INET6
) p4
= NULL
;
1188 if (node
== NULL
) cname
= "localhost";
1190 /* allow nodename to be a mapped IPv4 address, e.g. "::ffff:10.0.0.1" */
1191 if (p6
!= NULL
) flags
|= AI_V4MAPPED
;
1193 /* handle trivial questions */
1194 out
= si_addrinfo_list(si
, flags
, socktype
, proto
, p4
, p6
, port
, scope
, cname
, cname
);
1195 return _gai_sort_list(out
, flags
);
1197 else if (si_wants_addrinfo(si
))
1199 /* or let the current module handle the host lookups intelligently */
1200 out
= si
->vtable
->sim_addrinfo(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1201 return _gai_sort_list(out
, flags
);
1204 /* fall back to a default path */
1205 out
= _gai_simple(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1206 return _gai_sort_list(out
, flags
);
1209 static struct addrinfo
*
1210 si_item_to_addrinfo(si_item_t
*item
)
1213 struct addrinfo
*out
;
1215 if (item
== NULL
) return NULL
;
1217 a
= (si_addrinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1219 out
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
1220 if (out
== NULL
) return NULL
;
1222 out
->ai_flags
= a
->ai_flags
;
1223 out
->ai_family
= a
->ai_family
;
1224 out
->ai_socktype
= a
->ai_socktype
;
1225 out
->ai_protocol
= a
->ai_protocol
;
1226 out
->ai_addrlen
= a
->ai_addrlen
;
1228 out
->ai_addr
= (struct sockaddr
*)calloc(1, out
->ai_addrlen
);
1229 if (out
->ai_addr
== NULL
)
1235 memcpy(out
->ai_addr
, a
->ai_addr
.x
, out
->ai_addrlen
);
1237 if (a
->ai_canonname
!= NULL
)
1239 out
->ai_canonname
= strdup(a
->ai_canonname
);
1240 if (out
->ai_canonname
== NULL
)
1251 si_list_to_addrinfo(si_list_t
*list
)
1253 struct addrinfo
*tail
, *out
;
1256 if (list
== NULL
) return NULL
;
1257 if (list
->count
== 0) return NULL
;
1259 i
= list
->count
- 1;
1261 out
= si_item_to_addrinfo(list
->entry
[i
]);
1264 for (i
--; i
>= 0; i
--)
1266 out
= si_item_to_addrinfo(list
->entry
[i
]);
1273 out
->ai_next
= tail
;
1280 /* getipnodebyname */
1283 make_hostent(si_mod_t
*si
, const char *name
, struct in_addr addr
)
1289 if (name
== NULL
) return NULL
;
1293 addrs
[0] = (char *)&(addr
.s_addr
);
1297 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
);
1301 make_hostent6(si_mod_t
*si
, const char *name
, struct in6_addr addr
)
1307 if (name
== NULL
) return NULL
;
1311 addrs
[0] = (char *)&(addr
.__u6_addr
.__u6_addr32
[0]);
1315 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
);
1319 lower_case(const char *s
)
1324 if (s
== NULL
) return NULL
;
1326 t
= malloc(strlen(s
) + 1);
1328 for (i
= 0; s
[i
] != '\0'; i
++)
1330 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
1340 merge_alias(const char *name
, build_hostent_t
*h
)
1344 if (name
== NULL
) return 0;
1345 if (h
== NULL
) return 0;
1347 if ((h
->host
.h_name
!= NULL
) && (string_equal(name
, h
->host
.h_name
))) return 0;
1349 for (i
= 0; i
< h
->alias_count
; i
++)
1351 if (string_equal(name
, h
->host
.h_aliases
[i
])) return 0;
1354 if (h
->alias_count
== 0) h
->host
.h_aliases
= (char **)calloc(2, sizeof(char *));
1355 else h
->host
.h_aliases
= (char **)reallocf(h
->host
.h_aliases
, (h
->alias_count
+ 2) * sizeof(char *));
1357 if (h
->host
.h_aliases
== NULL
)
1363 h
->host
.h_aliases
[h
->alias_count
] = lower_case(name
);
1365 h
->host
.h_aliases
[h
->alias_count
] = NULL
;
1371 append_addr(const char *addr
, uint32_t len
, build_hostent_t
*h
)
1373 if (addr
== NULL
) return 0;
1374 if (h
== NULL
) return 0;
1376 if (h
->addr_count
== 0) h
->host
.h_addr_list
= (char **)calloc(2, sizeof(char *));
1377 else h
->host
.h_addr_list
= (char **)reallocf(h
->host
.h_addr_list
, (h
->addr_count
+ 2) * sizeof(char *));
1379 if (h
->host
.h_addr_list
== NULL
)
1385 h
->host
.h_addr_list
[h
->addr_count
] = malloc(len
);
1386 if (h
->host
.h_addr_list
[h
->addr_count
] == NULL
) return -1;
1388 memcpy(h
->host
.h_addr_list
[h
->addr_count
], addr
, len
);
1390 h
->host
.h_addr_list
[h
->addr_count
] = NULL
;
1396 free_build_hostent(build_hostent_t
*h
)
1401 if (h
== NULL
) return;
1403 if (h
->host
.h_name
!= NULL
) free(h
->host
.h_name
);
1404 h
->host
.h_name
= NULL
;
1406 aliases
= h
->host
.h_aliases
;
1407 if (aliases
!= NULL
)
1409 while (*aliases
!= NULL
) free(*aliases
++);
1410 free(h
->host
.h_aliases
);
1413 h
->host
.h_aliases
= NULL
;
1415 if (h
->host
.h_addr_list
!= NULL
)
1417 for (i
= 0; h
->host
.h_addr_list
[i
] != NULL
; i
++) free(h
->host
.h_addr_list
[i
]);
1418 free(h
->host
.h_addr_list
);
1421 h
->host
.h_addr_list
= NULL
;
1426 si_ipnode_byname(si_mod_t
*si
, const char *name
, int family
, int flags
, const char *interface
, uint32_t *err
)
1428 int i
, status
, want
;
1430 struct in_addr addr4
;
1431 struct in6_addr addr6
;
1432 si_item_t
*item4
, *item6
;
1433 build_hostent_t
*out
;
1437 memset(&addr4
, 0, sizeof(struct in_addr
));
1438 memset(&addr6
, 0, sizeof(struct in6_addr
));
1440 if (err
!= NULL
) *err
= 0;
1442 if (family
== AF_INET
)
1444 status
= inet_aton(name
, &addr4
);
1447 /* create a host entry */
1448 item4
= make_hostent(si
, name
, addr4
);
1451 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1458 else if (family
== AF_INET6
)
1460 status
= inet_pton(family
, name
, &addr6
);
1463 /* create a host entry */
1464 item6
= make_hostent6(si
, name
, addr6
);
1467 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1474 status
= inet_aton(name
, &addr4
);
1477 if (!(flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
)))
1479 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1483 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1484 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1485 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1486 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), &(addr4
.s_addr
), IPV4_ADDR_LEN
);
1488 /* create a host entry */
1489 item6
= make_hostent6(si
, name
, addr6
);
1492 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1501 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1506 * IF AI_ADDRCONFIG is set, we need to know what interface flavors we really have.
1512 if (flags
& AI_ADDRCONFIG
)
1514 if (si_inet_config(&if4
, &if6
) < 0)
1516 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1520 /* Bail out if there are no interfaces */
1521 if ((if4
== 0) && (if6
== 0))
1523 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1529 * Figure out what we want.
1530 * If user asked for AF_INET, we only want V4 addresses.
1532 want
= WANT_A4_ONLY
;
1534 if (family
== AF_INET
)
1536 if ((flags
& AI_ADDRCONFIG
) && (if4
== 0))
1538 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1542 else if (family
== AF_INET6
)
1544 /* family == AF_INET6 */
1545 want
= WANT_A6_ONLY
;
1547 if (flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
))
1551 want
= WANT_A6_PLUS_MAPPED_A4
;
1555 want
= WANT_A6_OR_MAPPED_A4_IF_NO_A6
;
1560 if ((flags
& AI_ADDRCONFIG
) && (if6
== 0))
1562 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1569 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1576 /* fetch IPv6 data if required */
1577 if ((want
== WANT_A6_ONLY
) || (want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) || (want
== WANT_A6_PLUS_MAPPED_A4
))
1579 item6
= si_host_byname(si
, name
, AF_INET6
, interface
, (uint32_t *)err
);
1582 /* fetch IPv4 data if required */
1583 if ((want
== WANT_A4_ONLY
) || (want
== WANT_A6_PLUS_MAPPED_A4
) || ((want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) && (item6
== NULL
)))
1585 item4
= si_host_byname(si
, name
, AF_INET
, interface
, (uint32_t *)err
);
1588 if (want
== WANT_A4_ONLY
)
1590 si_item_release(item6
);
1591 if ((item4
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1595 if (want
== WANT_A6_ONLY
)
1597 si_item_release(item4
);
1598 if ((item6
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1602 if ((item6
== NULL
) && (item4
== NULL
))
1604 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1608 /* output item will have IPv6 + mapped IPv4 addresses */
1610 out
= (build_hostent_t
*)calloc(1, sizeof(build_hostent_t
));
1613 si_item_release(item4
);
1614 si_item_release(item6
);
1615 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1621 h
= (struct hostent
*)((uintptr_t)item4
+ sizeof(si_item_t
));
1623 out
->host
.h_name
= lower_case(h
->h_name
);
1625 if (h
->h_aliases
!= NULL
)
1627 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1630 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
1632 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1633 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1634 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1635 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), h
->h_addr_list
[i
], IPV4_ADDR_LEN
);
1636 append_addr((const char *)&addr6
, IPV6_ADDR_LEN
, out
);
1642 h
= (struct hostent
*)((uintptr_t)item6
+ sizeof(si_item_t
));
1644 if (out
->host
.h_name
== NULL
) out
->host
.h_name
= lower_case(h
->h_name
);
1646 if (h
->h_aliases
!= NULL
)
1648 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1651 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++) append_addr(h
->h_addr_list
[i
], IPV6_ADDR_LEN
, out
);
1654 si_item_release(item4
);
1655 si_item_release(item6
);
1659 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
);
1661 free_build_hostent(out
);