2 * Copyright (c) 2008-2009 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 <arpa/inet.h>
36 #include <sys/param.h>
38 #include <notify_keys.h>
40 #include <TargetConditionals.h>
41 #include "netdb_async.h"
42 #include "si_module.h"
45 #define IPPROTO_UNSPEC 0
47 #define IPV6_ADDR_LEN 16
48 #define IPV4_ADDR_LEN 4
50 #define WANT_NOTHING 0
51 #define WANT_A4_ONLY 1
52 #define WANT_A6_ONLY 2
53 #define WANT_A6_PLUS_MAPPED_A4 3
54 #define WANT_A6_OR_MAPPED_A4_IF_NO_A6 4
56 #define V6TO4_PREFIX_16 0x2002
58 static int net_config_token
= -1;
59 static uint32_t net_v4_count
= 0;
60 static uint32_t net_v6_count
= 0; // includes 6to4 addresses
61 static uint32_t net_v6to4_count
= 0;
62 static pthread_mutex_t net_config_mutex
= PTHREAD_MUTEX_INITIALIZER
;
72 _si_netconfig(uint32_t *v4
, uint32_t *v6
, uint32_t *v6to4
)
75 struct ifaddrs
*ifa
, *ifap
;
76 struct sockaddr_in6
*sa6
;
78 pthread_mutex_lock(&net_config_mutex
);
82 if (net_config_token
< 0)
84 status
= notify_register_check(kNotifySCNetworkChange
, &net_config_token
);
85 if (status
!= 0) net_config_token
= -1;
88 if (net_config_token
>= 0)
90 status
= notify_check(net_config_token
, &checkit
);
91 if (status
!= 0) checkit
= 1;
98 if (getifaddrs(&ifa
) < 0)
107 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
109 if (ifap
->ifa_addr
== NULL
) continue;
110 if ((ifap
->ifa_flags
& IFF_UP
) == 0) continue;
112 if (ifap
->ifa_addr
->sa_family
== AF_INET
)
116 else if (ifap
->ifa_addr
->sa_family
== AF_INET6
)
119 sa6
= (struct sockaddr_in6
*)ifap
->ifa_addr
;
120 if (sa6
->sin6_addr
.__u6_addr
.__u6_addr16
[0] == ntohs(V6TO4_PREFIX_16
)) net_v6to4_count
++;
128 if (v4
!= NULL
) *v4
= net_v4_count
;
129 if (v6
!= NULL
) *v6
= net_v6_count
;
130 if (v6to4
!= NULL
) *v6to4
= net_v6to4_count
;
132 pthread_mutex_unlock(&net_config_mutex
);
138 freeaddrinfo(struct addrinfo
*a
)
140 struct addrinfo
*next
;
145 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
146 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
153 gai_strerror(int32_t err
)
157 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
158 case EAI_AGAIN
: return "Temporary failure in name resolution";
159 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
160 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
161 case EAI_FAMILY
: return "ai_family not supported";
162 case EAI_MEMORY
: return "Memory allocation failure";
163 case EAI_NODATA
: return "No address associated with nodename";
164 case EAI_NONAME
: return "nodename nor servname provided, or not known";
165 case EAI_SERVICE
: return "servname not supported for ai_socktype";
166 case EAI_SOCKTYPE
: return "ai_socktype not supported";
167 case EAI_SYSTEM
: return "System error";
168 case EAI_BADHINTS
: return "Bad hints";
169 case EAI_PROTOCOL
: return "ai_protocol not supported";
170 case EAI_OVERFLOW
: return "argument buffer overflow";
173 return "Unknown error";
179 * We handle some "trival" cases locally. If the caller passes
180 * NI_NUMERICHOST (only), then this call turns into a getservbyport
181 * to get the service name + inet_pton() to create a host string.
182 * If the caller passes NI_NUMERICSERV (only), then we zero out the port
183 * number, complete the getnameinfo, and use printf() to create a service
184 * string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
185 * we inet_ntop() and printf() and return the results.
187 __private_extern__ si_item_t
*
188 si_nameinfo(si_mod_t
*si
, const struct sockaddr
*sa
, int flags
, const char *interface
, uint32_t *err
)
190 si_item_t
*out
= NULL
;
191 const struct sockaddr
*lookup_sa
;
192 struct sockaddr_in s4
;
195 const uint64_t unused
= 0;
202 int do_host_lookup
= ((flags
& NI_NUMERICHOST
) == 0);
203 int do_serv_lookup
= ((flags
& NI_NUMERICSERV
) == 0);
206 if ((si
== NULL
) || (sa
== NULL
))
208 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
212 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
216 if (sa
->sa_family
== AF_INET
)
218 struct sockaddr_in
*s4
= (struct sockaddr_in
*)sa
;
219 memcpy(&a4
, &s4
->sin_addr
, sizeof(a4
));
223 else if (sa
->sa_family
== AF_INET6
)
225 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)sa
;
226 memcpy(&a6
, &s6
->sin6_addr
, sizeof(a6
));
227 port
= s6
->sin6_port
;
229 /* Look for link-local IPv6 scope id */
230 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
232 ifnum
= ntohs(a6
.__u6_addr
.__u6_addr16
[1]);
235 ifnum
= s6
->sin6_scope_id
;
236 a6
.__u6_addr
.__u6_addr16
[1] = htons(ifnum
);
239 if (ifnum
!= s6
->sin6_scope_id
&&
240 s6
->sin6_scope_id
!= 0)
242 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
247 /* v4 mapped and compat addresses are converted to plain v4 */
248 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
) || IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
))
250 memcpy(&a4
, &s6
->sin6_addr
.s6_addr
[12], sizeof(a4
));
252 memset(&s4
, 0, sizeof(s4
));
253 s4
.sin_len
= sizeof(s4
);
254 s4
.sin_family
= AF_INET
;
256 memcpy(&s4
.sin_addr
, &a4
, sizeof(s4
.sin_addr
));
257 lookup_sa
= (const struct sockaddr
*)&s4
;
266 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
270 if (do_host_lookup
== 1)
273 if ((do_serv_lookup
== 1) && (si
->sim_nameinfo
!= NULL
))
275 return si
->sim_nameinfo(si
, lookup_sa
, flags
, interface
, err
);
278 si_item_t
*item
= si_host_byaddr(si
, addr
, lookup_sa
->sa_family
, interface
, NULL
);
282 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
283 host
= strdup(h
->h_name
);
284 si_item_release(item
);
287 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
293 if ((do_serv_lookup
== 1) && (port
!= 0))
295 si_item_t
*item
= si_service_byport(si
, port
, NULL
);
299 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
300 serv
= strdup(s
->s_name
);
301 si_item_release(item
);
305 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
312 * Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
313 * if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
315 if (host
== NULL
&& (flags
& NI_NAMEREQD
) == 0)
317 char tmp
[INET6_ADDRSTRLEN
+ 1 + IF_NAMESIZE
+ 1];
319 if (sa
->sa_family
== AF_INET
)
321 char buf
[INET_ADDRSTRLEN
];
322 if (inet_ntop(AF_INET
, &a4
, buf
, sizeof(buf
)) != 0)
327 else if (sa
->sa_family
== AF_INET6
)
329 char buf
[INET6_ADDRSTRLEN
];
331 /* zero the embedded scope ID */
334 a6
.__u6_addr
.__u6_addr16
[1] = 0;
337 if (inet_ntop(AF_INET6
, &a6
, buf
, sizeof(buf
)) != 0)
341 char ifname
[IF_NAMESIZE
];
342 if (if_indextoname(ifnum
, ifname
) != NULL
)
344 asprintf(&host
, "%s%%%s", buf
, ifname
);
349 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
361 /* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
364 asprintf(&serv
, "%hu", ntohs(port
));
367 if ((host
== NULL
) || (serv
== NULL
))
371 if ((flags
& NI_NAMEREQD
) != 0)
373 *err
= SI_STATUS_EAI_NONAME
;
377 *err
= SI_STATUS_EAI_MEMORY
;
383 out
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_NAMEINFO
, 1, unused
, unused
, host
, serv
);
392 _gai_numericserv(const char *serv
, uint16_t *port
)
407 num
= strtol(serv
, &endptr
, 10);
408 if ((serv
[0] != '\0') && (*endptr
== '\0') && (num
>= 0) && (num
<= UINT16_MAX
))
411 if (port
!= NULL
) *port
= (uint16_t)num
;
418 __private_extern__
int
419 _gai_serv_to_port(const char *serv
, uint32_t proto
, uint16_t *port
)
423 const char *protoname
= NULL
;
425 if (_gai_numericserv(serv
, port
)) return 0;
427 if (proto
== IPPROTO_UDP
) protoname
= "udp";
428 if (proto
== IPPROTO_TCP
) protoname
= "tcp";
430 item
= si_service_byname(si_search(), serv
, protoname
);
431 if (item
== NULL
) return -1;
433 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
434 if (port
) *port
= ntohs(s
->s_port
);
435 si_item_release(item
);
440 __private_extern__ si_item_t
*
441 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
)
443 socket_data_t sockdata
;
444 struct sockaddr_in
*sa
;
449 len
= sizeof(struct sockaddr_in
);
450 memset(&sockdata
, 0, sizeof(socket_data_t
));
451 sa
= (struct sockaddr_in
*)&sockdata
;
454 sa
->sin_family
= AF_INET
;
455 sa
->sin_port
= htons(port
);
456 memcpy(&sa
->sin_addr
, addr
, sizeof(sa
->sin_addr
));
458 /* Kludge: Jam the interface number into sin_zero (4 bytes). */
460 memmove(sa
->sin_zero
, &v32
, sizeof(uint32_t));
462 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET
, sock
, proto
, len
, sockdata
, cname
);
465 __private_extern__ si_item_t
*
466 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
)
468 socket_data_t sockdata
;
469 struct sockaddr_in6
*sa
;
474 len
= sizeof(struct sockaddr_in6
);
475 memset(&sockdata
, 0, sizeof(socket_data_t
));
476 sa
= (struct sockaddr_in6
*)&sockdata
;
479 sa
->sin6_family
= AF_INET6
;
480 sa
->sin6_port
= htons(port
);
481 memcpy(&sa
->sin6_addr
, addr
, sizeof(sa
->sin6_addr
));
483 /* sin6_scope_id is in host byte order */
484 sa
->sin6_scope_id
= iface
;
486 if (IN6_IS_ADDR_LINKLOCAL(&sa
->sin6_addr
))
488 /* check for embedded scopeid */
489 uint16_t esid
= ntohs(sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
492 sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
493 if (iface
== 0) sa
->sin6_scope_id
= esid
;
497 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
500 __private_extern__ si_list_t
*
501 si_addrinfo_list(si_mod_t
*si
, int socktype
, int proto
, struct in_addr
*a4
, struct in6_addr
*a6
, int port
, int scopeid
, const char *cname4
, const char *cname6
)
503 si_item_t
*item
= NULL
;
504 si_list_t
*out4
= NULL
, *out6
= NULL
;
507 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
509 item
= si_addrinfo_v4(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
510 out4
= si_list_add(out4
, item
);
511 si_item_release(item
);
514 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
516 item
= si_addrinfo_v4(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
517 out4
= si_list_add(out4
, item
);
518 si_item_release(item
);
521 if (proto
== IPPROTO_ICMP
)
523 item
= si_addrinfo_v4(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
524 out4
= si_list_add(out4
, item
);
525 si_item_release(item
);
531 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
533 item
= si_addrinfo_v6(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, scopeid
, cname6
);
534 out6
= si_list_add(out6
, item
);
535 si_item_release(item
);
538 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
540 item
= si_addrinfo_v6(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, scopeid
, cname6
);
541 out6
= si_list_add(out6
, item
);
542 si_item_release(item
);
545 if (proto
== IPPROTO_ICMPV6
)
547 item
= si_addrinfo_v6(si
, 0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, scopeid
, cname6
);
548 out6
= si_list_add(out6
, item
);
549 si_item_release(item
);
553 out6
= si_list_concat(out6
, out4
);
554 si_list_release(out4
);
561 * Determines whether the given host name is a numeric IPv4 or IPv6 address,
562 * based on the address family input value. If the input addres family is
563 * unspecified, a more specific value will be provided on output if possible.
564 * Returns 1 if host name is numeric or 0 if not, or -1 on error.
567 _gai_numerichost(const char* nodename
, uint32_t *family
, int flags
, struct in_addr
*a4
, struct in6_addr
*a6
)
569 int numerichost
, passive
;
573 if (nodename
== NULL
)
575 /* return loopback or passive addresses */
576 passive
= (flags
& AI_PASSIVE
);
578 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET
))
580 if (passive
) a4
->s_addr
= 0;
581 else a4
->s_addr
= htonl(INADDR_LOOPBACK
);
584 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET6
))
586 memset(a6
, 0, sizeof(*a6
));
587 if (!passive
) a6
->__u6_addr
.__u6_addr32
[3] = htonl(1);
594 /* numeric IPv4 host valid for AF_UNSPEC and AF_INET */
595 numerichost
= inet_pton(AF_INET
, nodename
, a4
);
596 if (numerichost
== 1)
598 if (*family
== AF_UNSPEC
) *family
= AF_INET
;
599 else if (*family
== AF_INET6
) numerichost
= -1;
603 /* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
604 numerichost
= inet_pton(AF_INET6
, nodename
, a6
);
605 if (numerichost
== 1)
607 if (*family
== AF_UNSPEC
) *family
= AF_INET6
;
608 else if (*family
== AF_INET
) numerichost
= -1;
616 /* si_addrinfo_list_from_hostent
617 * Returns an addrinfo list from IPv4 and IPv6 hostent entries
619 __private_extern__ si_list_t
*
620 si_addrinfo_list_from_hostent(si_mod_t
*si
, uint32_t socktype
, uint32_t proto
, uint16_t port
, uint16_t scope
, const struct hostent
*h4
, const struct hostent
*h6
)
623 si_list_t
*out
= NULL
;
626 if ((h6
!= NULL
) && (h6
->h_addr_list
!= NULL
))
628 for (i
= 0; h6
->h_addr_list
[i
] != NULL
; i
++)
631 memcpy(&a6
, h6
->h_addr_list
[i
], h6
->h_length
);
632 list
= si_addrinfo_list(si
, socktype
, proto
, NULL
, &a6
, port
, scope
, NULL
, h6
->h_name
);
633 out
= si_list_concat(out
, list
);
634 si_list_release(list
);
638 if ((h4
!= NULL
) && (h4
->h_addr_list
!= NULL
))
640 for (i
= 0; h4
->h_addr_list
[i
] != NULL
; i
++)
643 memcpy(&a4
, h4
->h_addr_list
[i
], h4
->h_length
);
644 list
= si_addrinfo_list(si
, socktype
, proto
, &a4
, NULL
, port
, 0, h4
->h_name
, NULL
);
645 out
= si_list_concat(out
, list
);
646 si_list_release(list
);
654 _gai_sort_list(si_list_t
*in
)
661 if (in
== NULL
) return NULL
;
664 if (_si_netconfig(NULL
, NULL
, &v6to4
) < 0) v6to4
= 0;
666 out
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
667 if (out
== NULL
) return in
;
669 out
->refcount
= in
->refcount
;
670 out
->count
= in
->count
;
671 out
->entry
= (si_item_t
**)calloc(in
->count
, sizeof(si_item_t
*));
672 if (out
->entry
== NULL
)
679 if (v6to4
> 0) lead
= AF_INET
;
683 for (i
= 0; i
< in
->count
; i
++)
685 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
686 if (a
->ai_family
== lead
) out
->entry
[out
->curr
++] = in
->entry
[i
];
689 for (i
= 0; i
< in
->count
; i
++)
691 a
= (si_addrinfo_t
*)((uintptr_t)in
->entry
[i
] + sizeof(si_item_t
));
692 if (a
->ai_family
!= lead
) out
->entry
[out
->curr
++] = in
->entry
[i
];
704 * Simple lookup via gethostbyname2(3) mechanism.
706 __private_extern__ si_list_t
*
707 _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
)
709 si_item_t
*h4_item
= NULL
, *h6_item
= NULL
;
710 struct hostent
*h4
= NULL
, *h6
= NULL
;
711 si_list_t
*out
= NULL
;
714 if ((flags
& AI_NUMERICSERV
) != 0)
716 port
= *(uint16_t*)servptr
;
720 if (_gai_serv_to_port(servptr
, proto
, &port
) != 0)
722 if (err
) *err
= SI_STATUS_EAI_NONAME
;
727 if ((flags
& AI_NUMERICHOST
) != 0)
729 if (family
== AF_INET
)
731 h4_item
= si_host_byaddr(si
, nodeptr
, AF_INET
, interface
, NULL
);
733 else if (family
== AF_INET6
)
735 h6_item
= si_host_byaddr(si
, nodeptr
, AF_INET6
, interface
, NULL
);
740 if ((family
== AF_INET
) || (family
== AF_UNSPEC
))
742 h4_item
= si_host_byname(si
, nodeptr
, AF_INET
, interface
, NULL
);
745 if ((family
== AF_INET6
) || (family
== AF_UNSPEC
))
747 h6_item
= si_host_byname(si
, nodeptr
, AF_INET6
, interface
, NULL
);
753 h4
= (struct hostent
*)((uintptr_t)h4_item
+ sizeof(si_item_t
));
758 h6
= (struct hostent
*)((uintptr_t)h6_item
+ sizeof(si_item_t
));
761 out
= si_addrinfo_list_from_hostent(si
, socktype
, proto
, port
, 0, h4
, h6
);
762 si_item_release(h4_item
);
763 si_item_release(h6_item
);
765 return _gai_sort_list(out
);
768 __private_extern__ si_list_t
*
769 si_srv_byname(si_mod_t
*si
, const char *qname
, const char *interface
, uint32_t *err
)
771 if (si
== NULL
) return 0;
772 if (si
->sim_srv_byname
== NULL
) return 0;
774 return si
->sim_srv_byname(si
, qname
, interface
, err
);
777 __private_extern__
int
778 si_wants_addrinfo(si_mod_t
*si
)
780 if (si
== NULL
) return 0;
781 if (si
->sim_addrinfo
== NULL
) return 0;
782 if (si
->sim_wants_addrinfo
== NULL
) return 0;
784 return si
->sim_wants_addrinfo(si
);
788 _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
)
795 si_list_t
*list
= NULL
;
796 si_list_t
*result
= NULL
;
798 /* Minimum SRV priority is zero. Start below that. */
802 if (node
== NULL
|| serv
== NULL
) return NULL
;
804 asprintf(&qname
, "%s.%s", serv
, node
);
805 list
= si_srv_byname(si
, qname
, interface
, err
);
808 /* Iterate the SRV records starting at lowest priority and attempt to
809 * lookup the target host name. Returns the first successful lookup.
810 * It's an O(n^2) algorithm but data sets are small (less than 100) and
811 * sorting overhead is dwarfed by network I/O for each element.
813 while (list
!= NULL
&& result
== NULL
)
815 /* Find the next lowest priority level. */
816 /* Maximum SRV priority is UINT16_MAX. Start above that. */
819 for (i
= 0; i
< list
->count
; ++i
)
821 item
= list
->entry
[i
];
822 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
824 if (srv
->priority
> lastprio
&& srv
->priority
< currprio
)
826 currprio
= srv
->priority
;
830 if (currprio
== INT_MAX
)
832 /* All priorities have been evaluated. Done. */
840 /* Lookup hosts at the current priority level. Return first match. */
841 for (i
= 0; i
< list
->count
; ++i
)
843 item
= list
->entry
[i
];
844 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
846 if (srv
->priority
== currprio
)
848 /* So that _gai_simple expects an integer service. */
849 flags
|= AI_NUMERICSERV
;
851 result
= _gai_simple(si
, srv
->target
, &srv
->port
, family
, socktype
, proto
, flags
, interface
, err
);
862 si_list_release(list
);
868 __private_extern__ si_list_t
*
869 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
)
871 int numerichost
, numericserv
= 0;
872 const void *nodeptr
= NULL
, *servptr
= NULL
;
874 struct in_addr a4
, *p4
;
875 struct in6_addr a6
, *p6
;
879 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
883 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
887 /* treat empty strings as NULL */
888 if ((node
!= NULL
) && (node
[0] == '\0')) node
= NULL
;
889 if ((serv
!= NULL
) && (serv
[0] == '\0')) serv
= NULL
;
891 /* make sure we have a query */
892 if ((node
== NULL
) && (serv
== NULL
))
894 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
898 /* verify family is supported */
906 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
910 /* verify socket type is supported */
919 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
923 /* verify protocol is supported */
933 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
937 /* verify socket type compatible with protocol */
938 if (((socktype
== SOCK_DGRAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_UDP
)) || ((socktype
== SOCK_STREAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_TCP
)))
940 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
944 memset(&a4
, 0, sizeof(struct in_addr
));
945 memset(&a6
, 0, sizeof(struct in6_addr
));
947 /* determine the protocol if possible */
948 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_DGRAM
)) proto
= IPPROTO_UDP
;
949 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_STREAM
)) proto
= IPPROTO_TCP
;
953 if ((flags
& AI_SRV
) != 0)
956 out
= _gai_srv(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
957 return _gai_sort_list(out
);
961 /* Usual service lookup */
962 numericserv
= _gai_numericserv(serv
, &port
);
965 if ((flags
& AI_NUMERICSERV
) && (numericserv
== 0))
967 /* FreeBSD returns SI_STATUS_EAI_SERVICE */
968 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
974 if (numericserv
== 1)
976 flags
|= AI_NUMERICSERV
;
985 numerichost
= _gai_numerichost(node
, &family
, flags
, &a4
, &a6
);
986 if ((numerichost
== -1) || ((flags
& AI_NUMERICHOST
) && (numerichost
== 0)))
988 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
992 if (numerichost
== 1)
994 flags
|= AI_NUMERICHOST
;
995 if (family
== AF_INET
)
999 else if (family
== AF_INET6
)
1009 if ((numerichost
== 1) && (numericserv
== 0))
1011 /* only service lookup needed. convert to port and perform a trivial getaddrinfo */
1012 if (_gai_serv_to_port(serv
, proto
, &port
) != 0)
1014 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
1019 flags
|= AI_NUMERICSERV
;
1025 if ((numerichost
== 1) && (numericserv
== 1))
1031 if (family
== AF_INET
) p6
= NULL
;
1032 if (family
== AF_INET6
) p4
= NULL
;
1033 if (node
== NULL
) cname
= "localhost";
1035 /* handle trivial questions */
1036 out
= si_addrinfo_list(si
, socktype
, proto
, p4
, p6
, port
, 0, cname
, cname
);
1037 return _gai_sort_list(out
);
1039 else if ((si
->sim_wants_addrinfo
!= NULL
) && si
->sim_wants_addrinfo(si
))
1041 /* or let the current module handle the host lookups intelligently */
1042 out
= si
->sim_addrinfo(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1043 return _gai_sort_list(out
);
1046 /* fall back to a default path */
1047 out
= _gai_simple(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
1048 return _gai_sort_list(out
);
1051 static struct addrinfo
*
1052 si_item_to_addrinfo(si_item_t
*item
)
1055 struct addrinfo
*out
;
1057 if (item
== NULL
) return NULL
;
1059 a
= (si_addrinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1061 out
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
1062 if (out
== NULL
) return NULL
;
1064 out
->ai_flags
= a
->ai_flags
;
1065 out
->ai_family
= a
->ai_family
;
1066 out
->ai_socktype
= a
->ai_socktype
;
1067 out
->ai_protocol
= a
->ai_protocol
;
1068 out
->ai_addrlen
= a
->ai_addrlen
;
1070 out
->ai_addr
= (struct sockaddr
*)calloc(1, out
->ai_addrlen
);
1071 if (out
->ai_addr
== NULL
)
1077 memcpy(out
->ai_addr
, a
->ai_addr
.x
, out
->ai_addrlen
);
1079 if (a
->ai_canonname
!= NULL
)
1081 out
->ai_canonname
= strdup(a
->ai_canonname
);
1082 if (out
->ai_canonname
== NULL
)
1092 __private_extern__
struct addrinfo
*
1093 si_list_to_addrinfo(si_list_t
*list
)
1095 struct addrinfo
*tail
, *out
;
1098 if (list
== NULL
) return NULL
;
1099 if (list
->count
== 0) return NULL
;
1101 i
= list
->count
- 1;
1103 out
= si_item_to_addrinfo(list
->entry
[i
]);
1106 for (i
--; i
>= 0; i
--)
1108 out
= si_item_to_addrinfo(list
->entry
[i
]);
1115 out
->ai_next
= tail
;
1122 /* getipnodebyname */
1125 make_hostent(si_mod_t
*si
, const char *name
, struct in_addr addr
)
1131 if (name
== NULL
) return NULL
;
1135 addrs
[0] = (char *)&(addr
.s_addr
);
1139 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
);
1143 make_hostent6(si_mod_t
*si
, const char *name
, struct in6_addr addr
)
1149 if (name
== NULL
) return NULL
;
1153 addrs
[0] = (char *)&(addr
.__u6_addr
.__u6_addr32
[0]);
1157 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
);
1161 lower_case(const char *s
)
1166 if (s
== NULL
) return NULL
;
1168 t
= malloc(strlen(s
) + 1);
1170 for (i
= 0; s
[i
] != '\0'; i
++)
1172 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
1182 merge_alias(const char *name
, build_hostent_t
*h
)
1186 if (name
== NULL
) return 0;
1187 if (h
== NULL
) return 0;
1189 if ((h
->host
.h_name
!= NULL
) && (string_equal(name
, h
->host
.h_name
))) return 0;
1191 for (i
= 0; i
< h
->alias_count
; i
++)
1193 if (string_equal(name
, h
->host
.h_aliases
[i
])) return 0;
1196 if (h
->alias_count
== 0) h
->host
.h_aliases
= (char **)calloc(2, sizeof(char *));
1197 else h
->host
.h_aliases
= (char **)reallocf(h
->host
.h_aliases
, (h
->alias_count
+ 2) * sizeof(char *));
1199 if (h
->host
.h_aliases
== NULL
)
1205 h
->host
.h_aliases
[h
->alias_count
] = lower_case(name
);
1207 h
->host
.h_aliases
[h
->alias_count
] = NULL
;
1213 append_addr(const char *addr
, uint32_t len
, build_hostent_t
*h
)
1215 if (addr
== NULL
) return 0;
1216 if (h
== NULL
) return 0;
1218 if (h
->addr_count
== 0) h
->host
.h_addr_list
= (char **)calloc(2, sizeof(char *));
1219 else h
->host
.h_addr_list
= (char **)reallocf(h
->host
.h_addr_list
, (h
->addr_count
+ 2) * sizeof(char *));
1221 if (h
->host
.h_addr_list
== NULL
)
1227 h
->host
.h_addr_list
[h
->addr_count
] = malloc(len
);
1228 if (h
->host
.h_addr_list
[h
->addr_count
] == NULL
) return -1;
1230 memcpy(h
->host
.h_addr_list
[h
->addr_count
], addr
, len
);
1232 h
->host
.h_addr_list
[h
->addr_count
] = NULL
;
1238 free_build_hostent(build_hostent_t
*h
)
1243 if (h
== NULL
) return;
1245 if (h
->host
.h_name
!= NULL
) free(h
->host
.h_name
);
1246 h
->host
.h_name
= NULL
;
1248 aliases
= h
->host
.h_aliases
;
1249 if (aliases
!= NULL
)
1251 while (*aliases
!= NULL
) free(*aliases
++);
1252 free(h
->host
.h_aliases
);
1255 h
->host
.h_aliases
= NULL
;
1257 if (h
->host
.h_addr_list
!= NULL
)
1259 for (i
= 0; h
->host
.h_addr_list
[i
] != NULL
; i
++) free(h
->host
.h_addr_list
[i
]);
1260 free(h
->host
.h_addr_list
);
1263 h
->host
.h_addr_list
= NULL
;
1267 __private_extern__ si_item_t
*
1268 si_ipnode_byname(si_mod_t
*si
, const char *name
, int family
, int flags
, const char *interface
, uint32_t *err
)
1270 int i
, status
, want
;
1272 struct in_addr addr4
;
1273 struct in6_addr addr6
;
1274 si_item_t
*item4
, *item6
;
1275 build_hostent_t
*out
;
1279 memset(&addr4
, 0, sizeof(struct in_addr
));
1280 memset(&addr6
, 0, sizeof(struct in6_addr
));
1282 if (err
!= NULL
) *err
= 0;
1284 if (family
== AF_INET
)
1286 status
= inet_aton(name
, &addr4
);
1289 /* create a host entry */
1290 item4
= make_hostent(si
, name
, addr4
);
1293 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1300 else if (family
== AF_INET6
)
1302 status
= inet_pton(family
, name
, &addr6
);
1305 /* create a host entry */
1306 item6
= make_hostent6(si
, name
, addr6
);
1309 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1316 status
= inet_aton(name
, &addr4
);
1319 if (!(flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
)))
1321 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1325 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1326 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1327 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1328 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), &(addr4
.s_addr
), IPV4_ADDR_LEN
);
1330 /* create a host entry */
1331 item6
= make_hostent6(si
, name
, addr6
);
1334 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1343 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1348 * IF AI_ADDRCONFIG is set, we need to know what interface flavors we really have.
1354 if (flags
& AI_ADDRCONFIG
)
1356 if (_si_netconfig(&if4
, &if6
, NULL
) < 0)
1358 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1362 /* Bail out if there are no interfaces */
1363 if ((if4
== 0) && (if6
== 0))
1365 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1371 * Figure out what we want.
1372 * If user asked for AF_INET, we only want V4 addresses.
1374 want
= WANT_A4_ONLY
;
1376 if (family
== AF_INET
)
1378 if ((flags
& AI_ADDRCONFIG
) && (if4
== 0))
1380 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1384 else if (family
== AF_INET6
)
1386 /* family == AF_INET6 */
1387 want
= WANT_A6_ONLY
;
1389 if (flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
))
1393 want
= WANT_A6_PLUS_MAPPED_A4
;
1397 want
= WANT_A6_OR_MAPPED_A4_IF_NO_A6
;
1402 if ((flags
& AI_ADDRCONFIG
) && (if6
== 0))
1404 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1411 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1418 /* fetch IPv6 data if required */
1419 if ((want
== WANT_A6_ONLY
) || (want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) || (want
== WANT_A6_PLUS_MAPPED_A4
))
1421 item6
= si_host_byname(si
, name
, AF_INET6
, interface
, (uint32_t *)err
);
1424 /* fetch IPv4 data if required */
1425 if ((want
== WANT_A4_ONLY
) || (want
== WANT_A6_PLUS_MAPPED_A4
) || ((want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) && (item6
== NULL
)))
1427 item4
= si_host_byname(si
, name
, AF_INET
, interface
, (uint32_t *)err
);
1430 if (want
== WANT_A4_ONLY
)
1432 si_item_release(item6
);
1433 if ((item4
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1437 if (want
== WANT_A6_ONLY
)
1439 si_item_release(item4
);
1440 if ((item6
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1444 if ((item6
== NULL
) && (item4
== NULL
))
1446 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1450 /* output item will have IPv6 + mapped IPv4 addresses */
1452 out
= (build_hostent_t
*)calloc(1, sizeof(build_hostent_t
));
1455 si_item_release(item4
);
1456 si_item_release(item6
);
1457 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1463 h
= (struct hostent
*)((uintptr_t)item4
+ sizeof(si_item_t
));
1465 out
->host
.h_name
= lower_case(h
->h_name
);
1467 if (h
->h_aliases
!= NULL
)
1469 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1472 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
1474 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1475 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1476 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1477 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), h
->h_addr_list
[i
], IPV4_ADDR_LEN
);
1478 append_addr((const char *)&addr6
, IPV6_ADDR_LEN
, out
);
1484 h
= (struct hostent
*)((uintptr_t)item6
+ sizeof(si_item_t
));
1486 if (out
->host
.h_name
== NULL
) out
->host
.h_name
= lower_case(h
->h_name
);
1488 if (h
->h_aliases
!= NULL
)
1490 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1493 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++) append_addr(h
->h_addr_list
[i
], IPV6_ADDR_LEN
, out
);
1496 si_item_release(item4
);
1497 si_item_release(item6
);
1501 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
);
1503 free_build_hostent(out
);