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>
37 #include <TargetConditionals.h>
38 #include "netdb_async.h"
39 #include "si_module.h"
42 #define IPPROTO_UNSPEC 0
44 #define IPV6_ADDR_LEN 16
45 #define IPV4_ADDR_LEN 4
47 #define WANT_NOTHING 0
48 #define WANT_A4_ONLY 1
49 #define WANT_A6_ONLY 2
50 #define WANT_A6_PLUS_MAPPED_A4 3
51 #define WANT_A6_OR_MAPPED_A4_IF_NO_A6 4
61 freeaddrinfo(struct addrinfo
*a
)
63 struct addrinfo
*next
;
68 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
69 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
76 gai_strerror(int32_t err
)
80 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
81 case EAI_AGAIN
: return "Temporary failure in name resolution";
82 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
83 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
84 case EAI_FAMILY
: return "ai_family not supported";
85 case EAI_MEMORY
: return "Memory allocation failure";
86 case EAI_NODATA
: return "No address associated with nodename";
87 case EAI_NONAME
: return "nodename nor servname provided, or not known";
88 case EAI_SERVICE
: return "servname not supported for ai_socktype";
89 case EAI_SOCKTYPE
: return "ai_socktype not supported";
90 case EAI_SYSTEM
: return "System error";
91 case EAI_BADHINTS
: return "Bad hints";
92 case EAI_PROTOCOL
: return "ai_protocol not supported";
93 case EAI_OVERFLOW
: return "argument buffer overflow";
96 return "Unknown error";
102 * We handle some "trival" cases locally. If the caller passes
103 * NI_NUMERICHOST (only), then this call turns into a getservbyport
104 * to get the service name + inet_pton() to create a host string.
105 * If the caller passes NI_NUMERICSERV (only), then we zero out the port
106 * number, complete the getnameinfo, and use printf() to create a service
107 * string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
108 * we inet_ntop() and printf() and return the results.
110 __private_extern__ si_item_t
*
111 si_nameinfo(si_mod_t
*si
, const struct sockaddr
*sa
, int flags
, const char *interface
, uint32_t *err
)
113 si_item_t
*out
= NULL
;
114 const struct sockaddr
*lookup_sa
;
115 struct sockaddr_in s4
;
118 const uint64_t unused
= 0;
125 int do_host_lookup
= ((flags
& NI_NUMERICHOST
) == 0);
126 int do_serv_lookup
= ((flags
& NI_NUMERICSERV
) == 0);
129 if ((si
== NULL
) || (sa
== NULL
))
131 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
135 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
139 if (sa
->sa_family
== AF_INET
)
141 struct sockaddr_in
*s4
= (struct sockaddr_in
*)sa
;
142 memcpy(&a4
, &s4
->sin_addr
, sizeof(a4
));
146 else if (sa
->sa_family
== AF_INET6
)
148 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)sa
;
149 memcpy(&a6
, &s6
->sin6_addr
, sizeof(a6
));
150 port
= s6
->sin6_port
;
152 /* Look for link-local IPv6 scope id */
153 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
155 ifnum
= ntohs(a6
.__u6_addr
.__u6_addr16
[1]);
158 ifnum
= s6
->sin6_scope_id
;
159 a6
.__u6_addr
.__u6_addr16
[1] = htons(ifnum
);
162 if (ifnum
!= s6
->sin6_scope_id
&&
163 s6
->sin6_scope_id
!= 0)
165 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
170 /* v4 mapped and compat addresses are converted to plain v4 */
171 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
) || IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
))
173 memcpy(&a4
, &s6
->sin6_addr
.s6_addr
[12], sizeof(a4
));
175 memset(&s4
, 0, sizeof(s4
));
176 s4
.sin_len
= sizeof(s4
);
177 s4
.sin_family
= AF_INET
;
179 memcpy(&s4
.sin_addr
, &a4
, sizeof(s4
.sin_addr
));
180 lookup_sa
= (const struct sockaddr
*)&s4
;
189 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
193 if (do_host_lookup
== 1)
196 if ((do_serv_lookup
== 1) && (si
->sim_nameinfo
!= NULL
))
198 return si
->sim_nameinfo(si
, lookup_sa
, flags
, interface
, err
);
201 si_item_t
*item
= si_host_byaddr(si
, addr
, lookup_sa
->sa_family
, interface
, NULL
);
205 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
206 host
= strdup(h
->h_name
);
207 si_item_release(item
);
210 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
216 if ((do_serv_lookup
== 1) && (port
!= 0))
218 si_item_t
*item
= si_service_byport(si
, port
, NULL
);
222 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
223 serv
= strdup(s
->s_name
);
224 si_item_release(item
);
228 if (err
!= NULL
) *err
= SI_STATUS_EAI_MEMORY
;
235 * Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
236 * if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
238 if (host
== NULL
&& (flags
& NI_NAMEREQD
) == 0)
240 char tmp
[INET6_ADDRSTRLEN
+ 1 + IF_NAMESIZE
+ 1];
242 if (sa
->sa_family
== AF_INET
)
244 char buf
[INET_ADDRSTRLEN
];
245 if (inet_ntop(AF_INET
, &a4
, buf
, sizeof(buf
)) != 0)
250 else if (sa
->sa_family
== AF_INET6
)
252 char buf
[INET6_ADDRSTRLEN
];
254 /* zero the embedded scope ID */
257 a6
.__u6_addr
.__u6_addr16
[1] = 0;
260 if (inet_ntop(AF_INET6
, &a6
, buf
, sizeof(buf
)) != 0)
264 char ifname
[IF_NAMESIZE
];
265 if (if_indextoname(ifnum
, ifname
) != NULL
)
267 asprintf(&host
, "%s%%%s", buf
, ifname
);
272 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
284 /* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
287 asprintf(&serv
, "%hu", ntohs(port
));
290 if ((host
== NULL
) || (serv
== NULL
))
294 if ((flags
& NI_NAMEREQD
) != 0)
296 *err
= SI_STATUS_EAI_NONAME
;
300 *err
= SI_STATUS_EAI_MEMORY
;
306 out
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_NAMEINFO
, 1, unused
, unused
, host
, serv
);
315 _gai_numericserv(const char *serv
, uint16_t *port
)
330 num
= strtol(serv
, &endptr
, 10);
331 if ((serv
[0] != '\0') && (*endptr
== '\0') && (num
>= 0) && (num
<= UINT16_MAX
))
334 if (port
!= NULL
) *port
= (uint16_t)num
;
341 __private_extern__
int
342 _gai_serv_to_port(const char *serv
, uint32_t proto
, uint16_t *port
)
346 const char *protoname
= NULL
;
348 if (_gai_numericserv(serv
, port
)) return 0;
350 if (proto
== IPPROTO_UDP
) protoname
= "udp";
351 if (proto
== IPPROTO_TCP
) protoname
= "tcp";
353 item
= si_service_byname(si_search(), serv
, protoname
);
354 if (item
== NULL
) return -1;
356 s
= (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
357 if (port
) *port
= ntohs(s
->s_port
);
358 si_item_release(item
);
363 __private_extern__ si_item_t
*
364 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
)
366 socket_data_t sockdata
;
367 struct sockaddr_in
*sa
;
372 len
= sizeof(struct sockaddr_in
);
373 memset(&sockdata
, 0, sizeof(socket_data_t
));
374 sa
= (struct sockaddr_in
*)&sockdata
;
377 sa
->sin_family
= AF_INET
;
378 sa
->sin_port
= htons(port
);
379 memcpy(&sa
->sin_addr
, addr
, sizeof(sa
->sin_addr
));
381 /* Kludge: Jam the interface number into sin_zero (4 bytes). */
383 memmove(sa
->sin_zero
, &v32
, sizeof(uint32_t));
385 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET
, sock
, proto
, len
, sockdata
, cname
);
388 __private_extern__ si_item_t
*
389 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
)
391 socket_data_t sockdata
;
392 struct sockaddr_in6
*sa
;
397 len
= sizeof(struct sockaddr_in6
);
398 memset(&sockdata
, 0, sizeof(socket_data_t
));
399 sa
= (struct sockaddr_in6
*)&sockdata
;
402 sa
->sin6_family
= AF_INET6
;
403 sa
->sin6_port
= htons(port
);
404 memcpy(&sa
->sin6_addr
, addr
, sizeof(sa
->sin6_addr
));
406 /* sin6_scope_id is in host byte order */
407 sa
->sin6_scope_id
= iface
;
409 if (IN6_IS_ADDR_LINKLOCAL(&sa
->sin6_addr
))
411 /* check for embedded scopeid */
412 uint16_t esid
= ntohs(sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
415 sa
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
416 if (iface
== 0) sa
->sin6_scope_id
= esid
;
420 return (si_item_t
*)LI_ils_create("L448844444Ss", (unsigned long)si
, CATEGORY_ADDRINFO
, 1, unused
, unused
, flags
, AF_INET6
, sock
, proto
, len
, sockdata
, cname
);
423 __private_extern__ si_list_t
*
424 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
)
426 si_item_t
*item
= NULL
;
427 si_list_t
*out4
= NULL
, *out6
= NULL
;
430 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
432 item
= si_addrinfo_v4(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, cname4
);
433 out4
= si_list_add(out4
, item
);
434 si_item_release(item
);
437 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
439 item
= si_addrinfo_v4(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, cname4
);
440 out4
= si_list_add(out4
, item
);
441 si_item_release(item
);
444 if (proto
== IPPROTO_ICMP
)
446 item
= si_addrinfo_v4(si
, 0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, cname4
);
447 out4
= si_list_add(out4
, item
);
448 si_item_release(item
);
454 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
456 item
= si_addrinfo_v6(si
, 0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, scopeid
, cname6
);
457 out6
= si_list_add(out6
, item
);
458 si_item_release(item
);
461 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
463 item
= si_addrinfo_v6(si
, 0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, scopeid
, cname6
);
464 out6
= si_list_add(out6
, item
);
465 si_item_release(item
);
468 if (proto
== IPPROTO_ICMPV6
)
470 item
= si_addrinfo_v6(si
, 0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, scopeid
, cname6
);
471 out6
= si_list_add(out6
, item
);
472 si_item_release(item
);
476 out6
= si_list_concat(out6
, out4
);
477 si_list_release(out4
);
484 * Determines whether the given host name is a numeric IPv4 or IPv6 address,
485 * based on the address family input value. If the input addres family is
486 * unspecified, a more specific value will be provided on output if possible.
487 * Returns 1 if host name is numeric or 0 if not, or -1 on error.
490 _gai_numerichost(const char* nodename
, uint32_t *family
, int flags
, struct in_addr
*a4
, struct in6_addr
*a6
)
492 int numerichost
, passive
;
496 if (nodename
== NULL
)
498 /* return loopback or passive addresses */
499 passive
= (flags
& AI_PASSIVE
);
501 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET
))
503 if (passive
) a4
->s_addr
= 0;
504 else a4
->s_addr
= htonl(INADDR_LOOPBACK
);
507 if ((*family
== AF_UNSPEC
) || (*family
== AF_INET6
))
509 memset(a6
, 0, sizeof(*a6
));
510 if (!passive
) a6
->__u6_addr
.__u6_addr32
[3] = htonl(1);
517 /* numeric IPv4 host valid for AF_UNSPEC and AF_INET */
518 numerichost
= inet_pton(AF_INET
, nodename
, a4
);
519 if (numerichost
== 1)
521 if (*family
== AF_UNSPEC
) *family
= AF_INET
;
522 else if (*family
== AF_INET6
) numerichost
= -1;
526 /* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
527 numerichost
= inet_pton(AF_INET6
, nodename
, a6
);
528 if (numerichost
== 1)
530 if (*family
== AF_UNSPEC
) *family
= AF_INET6
;
531 else if (*family
== AF_INET
) numerichost
= -1;
539 /* si_addrinfo_list_from_hostent
540 * Returns an addrinfo list from IPv4 and IPv6 hostent entries
542 __private_extern__ si_list_t
*
543 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
)
546 si_list_t
*out
= NULL
;
549 if ((h6
!= NULL
) && (h6
->h_addr_list
!= NULL
))
551 for (i
= 0; h6
->h_addr_list
[i
] != NULL
; i
++)
554 memcpy(&a6
, h6
->h_addr_list
[i
], h6
->h_length
);
555 list
= si_addrinfo_list(si
, socktype
, proto
, NULL
, &a6
, port
, scope
, NULL
, h6
->h_name
);
556 out
= si_list_concat(out
, list
);
557 si_list_release(list
);
561 if ((h4
!= NULL
) && (h4
->h_addr_list
!= NULL
))
563 for (i
= 0; h4
->h_addr_list
[i
] != NULL
; i
++)
566 memcpy(&a4
, h4
->h_addr_list
[i
], h4
->h_length
);
567 list
= si_addrinfo_list(si
, socktype
, proto
, &a4
, NULL
, port
, 0, h4
->h_name
, NULL
);
568 out
= si_list_concat(out
, list
);
569 si_list_release(list
);
577 * Simple lookup via gethostbyname2(3) mechanism.
580 _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
)
582 si_item_t
*h4_item
= NULL
, *h6_item
= NULL
;
583 struct hostent
*h4
= NULL
, *h6
= NULL
;
584 si_list_t
*out
= NULL
;
587 if ((flags
& AI_NUMERICSERV
) != 0)
589 port
= *(uint16_t*)servptr
;
593 if (_gai_serv_to_port(servptr
, proto
, &port
) != 0)
595 if (err
) *err
= SI_STATUS_EAI_NONAME
;
600 if ((flags
& AI_NUMERICHOST
) != 0)
602 if (family
== AF_INET
)
604 h4_item
= si_host_byaddr(si
, nodeptr
, AF_INET
, interface
, NULL
);
606 else if (family
== AF_INET6
)
608 h6_item
= si_host_byaddr(si
, nodeptr
, AF_INET6
, interface
, NULL
);
613 if ((family
== AF_INET
) || (family
== AF_UNSPEC
))
615 h4_item
= si_host_byname(si
, nodeptr
, AF_INET
, interface
, NULL
);
618 if ((family
== AF_INET6
) || (family
== AF_UNSPEC
))
620 h6_item
= si_host_byname(si
, nodeptr
, AF_INET6
, interface
, NULL
);
626 h4
= (struct hostent
*)((uintptr_t)h4_item
+ sizeof(si_item_t
));
631 h6
= (struct hostent
*)((uintptr_t)h6_item
+ sizeof(si_item_t
));
634 out
= si_addrinfo_list_from_hostent(si
, socktype
, proto
, port
, 0, h4
, h6
);
635 si_item_release(h4_item
);
636 si_item_release(h6_item
);
641 __private_extern__ si_list_t
*
642 si_srv_byname(si_mod_t
*si
, const char *qname
, const char *interface
, uint32_t *err
)
644 if (si
== NULL
) return 0;
645 if (si
->sim_srv_byname
== NULL
) return 0;
647 return si
->sim_srv_byname(si
, qname
, interface
, err
);
650 __private_extern__
int
651 si_wants_addrinfo(si_mod_t
*si
)
653 if (si
== NULL
) return 0;
654 if (si
->sim_addrinfo
== NULL
) return 0;
655 if (si
->sim_wants_addrinfo
== NULL
) return 0;
657 return si
->sim_wants_addrinfo(si
);
661 _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
)
668 si_list_t
*list
= NULL
;
669 si_list_t
*result
= NULL
;
671 /* Minimum SRV priority is zero. Start below that. */
675 if (node
== NULL
|| serv
== NULL
) return NULL
;
677 asprintf(&qname
, "%s.%s", serv
, node
);
678 list
= si_srv_byname(si
, qname
, interface
, err
);
681 /* Iterate the SRV records starting at lowest priority and attempt to
682 * lookup the target host name. Returns the first successful lookup.
683 * It's an O(n^2) algorithm but data sets are small (less than 100) and
684 * sorting overhead is dwarfed by network I/O for each element.
686 while (list
!= NULL
&& result
== NULL
)
688 /* Find the next lowest priority level. */
689 /* Maximum SRV priority is UINT16_MAX. Start above that. */
692 for (i
= 0; i
< list
->count
; ++i
)
694 item
= list
->entry
[i
];
695 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
697 if (srv
->priority
> lastprio
&& srv
->priority
< currprio
)
699 currprio
= srv
->priority
;
703 if (currprio
== INT_MAX
)
705 /* All priorities have been evaluated. Done. */
713 /* Lookup hosts at the current priority level. Return first match. */
714 for (i
= 0; i
< list
->count
; ++i
)
716 item
= list
->entry
[i
];
717 srv
= (si_srv_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
719 if (srv
->priority
== currprio
)
721 /* So that _gai_simple expects an integer service. */
722 flags
|= AI_NUMERICSERV
;
724 result
= _gai_simple(si
, srv
->target
, &srv
->port
, family
, socktype
, proto
, flags
, interface
, err
);
735 si_list_release(list
);
741 __private_extern__ si_list_t
*
742 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
)
744 int numerichost
, numericserv
= 0;
745 const void *nodeptr
= NULL
, *servptr
= NULL
;
747 struct in_addr a4
, *p4
;
748 struct in6_addr a6
, *p6
;
751 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
755 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAIL
;
759 /* treat empty strings as NULL */
760 if ((node
!= NULL
) && (node
[0] == '\0')) node
= NULL
;
761 if ((serv
!= NULL
) && (serv
[0] == '\0')) serv
= NULL
;
763 /* make sure we have a query */
764 if ((node
== NULL
) && (serv
== NULL
))
766 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
770 /* verify family is supported */
778 if (err
!= NULL
) *err
= SI_STATUS_EAI_FAMILY
;
782 /* verify socket type is supported */
791 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
795 /* verify protocol is supported */
805 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
809 /* verify socket type compatible with protocol */
810 if (((socktype
== SOCK_DGRAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_UDP
)) || ((socktype
== SOCK_STREAM
) && (proto
!= IPPROTO_UNSPEC
) && (proto
!= IPPROTO_TCP
)))
812 if (err
!= NULL
) *err
= SI_STATUS_EAI_BADHINTS
;
816 memset(&a4
, 0, sizeof(struct in_addr
));
817 memset(&a6
, 0, sizeof(struct in6_addr
));
819 /* determine the protocol if possible */
820 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_DGRAM
)) proto
= IPPROTO_UDP
;
821 if ((proto
== IPPROTO_UNSPEC
) && (socktype
== SOCK_STREAM
)) proto
= IPPROTO_TCP
;
825 if ((flags
& AI_SRV
) != 0)
828 return _gai_srv(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
832 /* Usual service lookup */
833 numericserv
= _gai_numericserv(serv
, &port
);
836 if ((flags
& AI_NUMERICSERV
) && (numericserv
== 0))
838 /* FreeBSD returns SI_STATUS_EAI_SERVICE */
839 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
845 if (numericserv
== 1)
847 flags
|= AI_NUMERICSERV
;
856 numerichost
= _gai_numerichost(node
, &family
, flags
, &a4
, &a6
);
857 if ((numerichost
== -1) || ((flags
& AI_NUMERICHOST
) && (numerichost
== 0)))
859 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
863 if (numerichost
== 1)
865 flags
|= AI_NUMERICHOST
;
866 if (family
== AF_INET
)
870 else if (family
== AF_INET6
)
880 if ((numerichost
== 1) && (numericserv
== 0))
882 /* only service lookup needed. convert to port and perform a trivial getaddrinfo */
883 if (_gai_serv_to_port(serv
, proto
, &port
) != 0)
885 if (err
!= NULL
) *err
= SI_STATUS_EAI_NONAME
;
890 flags
|= AI_NUMERICSERV
;
896 if ((numerichost
== 1) && (numericserv
== 1))
902 if (family
== AF_INET
) p6
= NULL
;
903 if (family
== AF_INET6
) p4
= NULL
;
904 if (node
== NULL
) cname
= "localhost";
906 /* handle trivial questions */
907 return si_addrinfo_list(si
, socktype
, proto
, p4
, p6
, port
, 0, cname
, cname
);
909 else if ((si
->sim_wants_addrinfo
!= NULL
) && si
->sim_wants_addrinfo(si
))
911 /* or let the current module handle the host lookups intelligently */
912 return si
->sim_addrinfo(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
915 /* fall back to a default path */
916 return _gai_simple(si
, nodeptr
, servptr
, family
, socktype
, proto
, flags
, interface
, err
);
919 static struct addrinfo
*
920 si_item_to_addrinfo(si_item_t
*item
)
923 struct addrinfo
*out
;
925 if (item
== NULL
) return NULL
;
927 a
= (si_addrinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
929 out
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
930 if (out
== NULL
) return NULL
;
932 out
->ai_flags
= a
->ai_flags
;
933 out
->ai_family
= a
->ai_family
;
934 out
->ai_socktype
= a
->ai_socktype
;
935 out
->ai_protocol
= a
->ai_protocol
;
936 out
->ai_addrlen
= a
->ai_addrlen
;
938 out
->ai_addr
= (struct sockaddr
*)calloc(1, out
->ai_addrlen
);
939 if (out
->ai_addr
== NULL
)
945 memcpy(out
->ai_addr
, a
->ai_addr
.x
, out
->ai_addrlen
);
947 if (a
->ai_canonname
!= NULL
)
949 out
->ai_canonname
= strdup(a
->ai_canonname
);
950 if (out
->ai_canonname
== NULL
)
960 __private_extern__
struct addrinfo
*
961 si_list_to_addrinfo(si_list_t
*list
)
963 struct addrinfo
*tail
, *out
;
966 if (list
== NULL
) return NULL
;
967 if (list
->count
== 0) return NULL
;
971 out
= si_item_to_addrinfo(list
->entry
[i
]);
974 for (i
--; i
>= 0; i
--)
976 out
= si_item_to_addrinfo(list
->entry
[i
]);
990 /* getipnodebyname */
993 make_hostent(si_mod_t
*si
, const char *name
, struct in_addr addr
)
999 if (name
== NULL
) return NULL
;
1003 addrs
[0] = (char *)&(addr
.s_addr
);
1007 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
);
1011 make_hostent6(si_mod_t
*si
, const char *name
, struct in6_addr addr
)
1017 if (name
== NULL
) return NULL
;
1021 addrs
[0] = (char *)&(addr
.__u6_addr
.__u6_addr32
[0]);
1025 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
);
1029 lower_case(const char *s
)
1034 if (s
== NULL
) return NULL
;
1036 t
= malloc(strlen(s
) + 1);
1038 for (i
= 0; s
[i
] != '\0'; i
++)
1040 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
1050 merge_alias(const char *name
, build_hostent_t
*h
)
1054 if (name
== NULL
) return 0;
1055 if (h
== NULL
) return 0;
1057 if ((h
->host
.h_name
!= NULL
) && (string_equal(name
, h
->host
.h_name
))) return 0;
1059 for (i
= 0; i
< h
->alias_count
; i
++)
1061 if (string_equal(name
, h
->host
.h_aliases
[i
])) return 0;
1064 if (h
->alias_count
== 0) h
->host
.h_aliases
= (char **)calloc(2, sizeof(char *));
1065 else h
->host
.h_aliases
= (char **)reallocf(h
->host
.h_aliases
, (h
->alias_count
+ 2) * sizeof(char *));
1067 if (h
->host
.h_aliases
== NULL
)
1073 h
->host
.h_aliases
[h
->alias_count
] = lower_case(name
);
1075 h
->host
.h_aliases
[h
->alias_count
] = NULL
;
1081 append_addr(const char *addr
, uint32_t len
, build_hostent_t
*h
)
1083 if (addr
== NULL
) return 0;
1084 if (h
== NULL
) return 0;
1086 if (h
->addr_count
== 0) h
->host
.h_addr_list
= (char **)calloc(2, sizeof(char *));
1087 else h
->host
.h_addr_list
= (char **)reallocf(h
->host
.h_addr_list
, (h
->addr_count
+ 2) * sizeof(char *));
1089 if (h
->host
.h_addr_list
== NULL
)
1095 h
->host
.h_addr_list
[h
->addr_count
] = malloc(len
);
1096 if (h
->host
.h_addr_list
[h
->addr_count
] == NULL
) return -1;
1098 memcpy(h
->host
.h_addr_list
[h
->addr_count
], addr
, len
);
1100 h
->host
.h_addr_list
[h
->addr_count
] = NULL
;
1106 free_build_hostent(build_hostent_t
*h
)
1111 if (h
== NULL
) return;
1113 if (h
->host
.h_name
!= NULL
) free(h
->host
.h_name
);
1114 h
->host
.h_name
= NULL
;
1116 aliases
= h
->host
.h_aliases
;
1117 if (aliases
!= NULL
)
1119 while (*aliases
!= NULL
) free(*aliases
++);
1120 free(h
->host
.h_aliases
);
1123 h
->host
.h_aliases
= NULL
;
1125 if (h
->host
.h_addr_list
!= NULL
)
1127 for (i
= 0; h
->host
.h_addr_list
[i
] != NULL
; i
++) free(h
->host
.h_addr_list
[i
]);
1128 free(h
->host
.h_addr_list
);
1131 h
->host
.h_addr_list
= NULL
;
1135 __private_extern__ si_item_t
*
1136 si_ipnode_byname(si_mod_t
*si
, const char *name
, int family
, int flags
, const char *interface
, uint32_t *err
)
1138 int i
, status
, want
, if4
, if6
;
1139 struct ifaddrs
*ifa
, *ifap
;
1140 struct in_addr addr4
;
1141 struct in6_addr addr6
;
1142 si_item_t
*item4
, *item6
;
1143 build_hostent_t
*out
;
1147 memset(&addr4
, 0, sizeof(struct in_addr
));
1148 memset(&addr6
, 0, sizeof(struct in6_addr
));
1150 if (err
!= NULL
) *err
= 0;
1152 if (family
== AF_INET
)
1154 status
= inet_aton(name
, &addr4
);
1157 /* create a host entry */
1158 item4
= make_hostent(si
, name
, addr4
);
1161 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1168 else if (family
== AF_INET6
)
1170 status
= inet_pton(family
, name
, &addr6
);
1173 /* create a host entry */
1174 item6
= make_hostent6(si
, name
, addr6
);
1177 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1184 status
= inet_aton(name
, &addr4
);
1187 if (!(flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
)))
1189 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1193 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1194 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1195 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1196 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), &(addr4
.s_addr
), IPV4_ADDR_LEN
);
1198 /* create a host entry */
1199 item6
= make_hostent6(si
, name
, addr6
);
1202 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1211 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1216 * IF AI_ADDRCONFIG is set, we need to know what interface flavors we really have.
1222 if (flags
& AI_ADDRCONFIG
)
1224 if (getifaddrs(&ifa
) < 0)
1226 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1230 for (ifap
= ifa
; ifap
!= NULL
; ifap
= ifap
->ifa_next
)
1232 if (ifap
->ifa_addr
== NULL
) continue;
1233 if ((ifap
->ifa_flags
& IFF_UP
) == 0) continue;
1234 if (ifap
->ifa_addr
->sa_family
== AF_INET
) if4
++;
1235 else if (ifap
->ifa_addr
->sa_family
== AF_INET6
) if6
++;
1240 /* Bail out if there are no interfaces */
1241 if ((if4
== 0) && (if6
== 0))
1243 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1249 * Figure out what we want.
1250 * If user asked for AF_INET, we only want V4 addresses.
1252 want
= WANT_A4_ONLY
;
1254 if (family
== AF_INET
)
1256 if ((flags
& AI_ADDRCONFIG
) && (if4
== 0))
1258 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1262 else if (family
== AF_INET6
)
1264 /* family == AF_INET6 */
1265 want
= WANT_A6_ONLY
;
1267 if (flags
& (AI_V4MAPPED
| AI_V4MAPPED_CFG
))
1271 want
= WANT_A6_PLUS_MAPPED_A4
;
1275 want
= WANT_A6_OR_MAPPED_A4_IF_NO_A6
;
1280 if ((flags
& AI_ADDRCONFIG
) && (if6
== 0))
1282 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1289 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1296 /* fetch IPv6 data if required */
1297 if ((want
== WANT_A6_ONLY
) || (want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) || (want
== WANT_A6_PLUS_MAPPED_A4
))
1299 item6
= si_host_byname(si
, name
, AF_INET6
, interface
, (uint32_t *)err
);
1302 /* fetch IPv4 data if required */
1303 if ((want
== WANT_A4_ONLY
) || (want
== WANT_A6_PLUS_MAPPED_A4
) || ((want
== WANT_A6_OR_MAPPED_A4_IF_NO_A6
) && (item6
== NULL
)))
1305 item4
= si_host_byname(si
, name
, AF_INET
, interface
, (uint32_t *)err
);
1308 if (want
== WANT_A4_ONLY
)
1310 si_item_release(item6
);
1311 if ((item4
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1315 if (want
== WANT_A6_ONLY
)
1317 si_item_release(item4
);
1318 if ((item6
== NULL
) && (err
!= NULL
)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1322 if ((item6
== NULL
) && (item4
== NULL
))
1324 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
1328 /* output item will have IPv6 + mapped IPv4 addresses */
1330 out
= (build_hostent_t
*)calloc(1, sizeof(build_hostent_t
));
1333 si_item_release(item4
);
1334 si_item_release(item6
);
1335 if (err
!= NULL
) *err
= SI_STATUS_H_ERRNO_NO_RECOVERY
;
1341 h
= (struct hostent
*)((uintptr_t)item4
+ sizeof(si_item_t
));
1343 out
->host
.h_name
= lower_case(h
->h_name
);
1345 if (h
->h_aliases
!= NULL
)
1347 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1350 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
1352 addr6
.__u6_addr
.__u6_addr32
[0] = 0x00000000;
1353 addr6
.__u6_addr
.__u6_addr32
[1] = 0x00000000;
1354 addr6
.__u6_addr
.__u6_addr32
[2] = htonl(0x0000ffff);
1355 memmove(&(addr6
.__u6_addr
.__u6_addr32
[3]), h
->h_addr_list
[i
], IPV4_ADDR_LEN
);
1356 append_addr((const char *)&addr6
, IPV6_ADDR_LEN
, out
);
1362 h
= (struct hostent
*)((uintptr_t)item6
+ sizeof(si_item_t
));
1364 if (out
->host
.h_name
== NULL
) out
->host
.h_name
= lower_case(h
->h_name
);
1366 if (h
->h_aliases
!= NULL
)
1368 for (i
= 0; h
->h_aliases
[i
] != NULL
; i
++) merge_alias(h
->h_aliases
[i
], out
);
1371 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++) append_addr(h
->h_addr_list
[i
], IPV6_ADDR_LEN
, out
);
1374 si_item_release(item4
);
1375 si_item_release(item6
);
1379 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
);
1381 free_build_hostent(out
);