2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
37 #include <rpc/types.h>
39 #include <mach/mach.h>
40 #include <servers/bootstrap.h>
43 #include "netdb_async.h"
46 #define IPPROTO_UNSPEC 0
48 #define LONG_STRING_LENGTH 8192
49 #define _LU_MAXLUSTRLEN 256
50 #define LU_QBUF_SIZE 8192
52 #define MAX_LOOKUP_ATTEMPTS 10
54 #define INET_NTOP_AF_INET_OFFSET 4
55 #define INET_NTOP_AF_INET6_OFFSET 8
57 extern mach_port_t
_lookupd_port();
59 static int gai_proc
= -1;
60 static int gni_proc
= -1;
62 static int32_t supported_family
[] =
68 static int32_t supported_family_count
= 3;
70 static int32_t supported_socket
[] =
77 static int32_t supported_socket_count
= 4;
79 static int32_t supported_protocol
[] =
86 static int32_t supported_protocol_count
= 4;
88 static int32_t supported_socket_protocol_pair
[] =
90 SOCK_RAW
, IPPROTO_UNSPEC
,
91 SOCK_RAW
, IPPROTO_UDP
,
92 SOCK_RAW
, IPPROTO_TCP
,
93 SOCK_RAW
, IPPROTO_ICMPV6
,
94 SOCK_UNSPEC
, IPPROTO_UNSPEC
,
95 SOCK_UNSPEC
, IPPROTO_UDP
,
96 SOCK_UNSPEC
, IPPROTO_TCP
,
97 SOCK_UNSPEC
, IPPROTO_ICMPV6
,
98 SOCK_DGRAM
, IPPROTO_UNSPEC
,
99 SOCK_DGRAM
, IPPROTO_UDP
,
100 SOCK_STREAM
, IPPROTO_UNSPEC
,
101 SOCK_STREAM
, IPPROTO_TCP
103 static int32_t supported_socket_protocol_pair_count
= 12;
106 gai_family_type_check(int32_t f
)
110 for (i
= 0; i
< supported_family_count
; i
++)
112 if (f
== supported_family
[i
]) return 0;
119 gai_socket_type_check(int32_t s
)
123 for (i
= 0; i
< supported_socket_count
; i
++)
125 if (s
== supported_socket
[i
]) return 0;
132 gai_protocol_type_check(int32_t p
)
136 for (i
= 0; i
< supported_protocol_count
; i
++)
138 if (p
== supported_protocol
[i
]) return 0;
145 gai_socket_protocol_type_check(int32_t s
, int32_t p
)
147 int32_t i
, j
, ss
, sp
;
149 for (i
= 0, j
= 0; i
< supported_socket_protocol_pair_count
; i
++, j
+=2)
151 ss
= supported_socket_protocol_pair
[j
];
152 sp
= supported_socket_protocol_pair
[j
+1];
153 if ((s
== ss
) && (p
== sp
)) return 0;
160 gai_strerror(int32_t err
)
164 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
165 case EAI_AGAIN
: return "Temporary failure in name resolution";
166 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
167 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
168 case EAI_FAMILY
: return "ai_family not supported";
169 case EAI_MEMORY
: return "Memory allocation failure";
170 case EAI_NODATA
: return "No address associated with nodename";
171 case EAI_NONAME
: return "nodename nor servname provided, or not known";
172 case EAI_SERVICE
: return "servname not supported for ai_socktype";
173 case EAI_SOCKTYPE
: return "ai_socktype not supported";
174 case EAI_SYSTEM
: return "System error";
175 case EAI_BADHINTS
: return "Bad hints";
176 case EAI_PROTOCOL
: return "ai_protocol not supported";
179 return "Unknown error";
183 append_addrinfo(struct addrinfo
**l
, struct addrinfo
*a
)
187 if (l
== NULL
) return;
188 if (a
== NULL
) return;
198 if (a
->ai_family
== PF_INET6
)
200 if (x
->ai_family
== PF_INET
)
207 while ((x
->ai_next
!= NULL
) && (x
->ai_next
->ai_family
!= PF_INET
)) x
= x
->ai_next
;
208 a
->ai_next
= x
->ai_next
;
213 while (x
->ai_next
!= NULL
) x
= x
->ai_next
;
220 encode_kv(XDR
*x
, const char *k
, const char *v
)
224 if (!xdr_string(x
, (char **)&k
, _LU_MAXLUSTRLEN
)) return 1;
225 if (!xdr_int(x
, &n
)) return 1;
226 if (!xdr_string(x
, (char **)&v
, _LU_MAXLUSTRLEN
)) return 1;
232 freeaddrinfo(struct addrinfo
*a
)
234 struct addrinfo
*next
;
239 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
240 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
246 static struct addrinfo
*
247 new_addrinfo_v4(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in_addr addr
, uint32_t iface
, char *cname
)
250 struct sockaddr_in
*sa
;
253 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
254 if (a
== NULL
) return NULL
;
259 a
->ai_family
= PF_INET
;
260 a
->ai_socktype
= sock
;
261 a
->ai_protocol
= proto
;
263 a
->ai_addrlen
= sizeof(struct sockaddr_in
);
265 sa
= (struct sockaddr_in
*)calloc(1, a
->ai_addrlen
);
272 sa
->sin_len
= a
->ai_addrlen
;
273 sa
->sin_family
= PF_INET
;
274 sa
->sin_port
= htons(port
);
277 /* Kludge: Jam the interface number into sin_zero. */
278 memmove(sa
->sin_zero
, &iface
, sizeof(uint32_t));
280 a
->ai_addr
= (struct sockaddr
*)sa
;
284 len
= strlen(cname
) + 1;
285 a
->ai_canonname
= malloc(len
);
286 memmove(a
->ai_canonname
, cname
, len
);
292 static struct addrinfo
*
293 new_addrinfo_v6(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in6_addr addr
, uint32_t scopeid
, char *cname
)
296 struct sockaddr_in6
*sa
;
299 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
300 if (a
== NULL
) return NULL
;
305 a
->ai_family
= PF_INET6
;
306 a
->ai_socktype
= sock
;
307 a
->ai_protocol
= proto
;
309 a
->ai_addrlen
= sizeof(struct sockaddr_in6
);
311 sa
= (struct sockaddr_in6
*)calloc(1, a
->ai_addrlen
);
318 sa
->sin6_len
= a
->ai_addrlen
;
319 sa
->sin6_family
= PF_INET6
;
320 sa
->sin6_port
= htons(port
);
321 sa
->sin6_addr
= addr
;
322 sa
->sin6_scope_id
= scopeid
;
323 a
->ai_addr
= (struct sockaddr
*)sa
;
327 len
= strlen(cname
) + 1;
328 a
->ai_canonname
= malloc(len
);
329 memmove(a
->ai_canonname
, cname
, len
);
336 * getaddrinfo support in lookupd
337 * Input dict may contain the following
341 * protocol: [IPPROTO_UNSPEC] | IPPROTO_UDP | IPPROTO_TCP
342 * socktype: [SOCK_UNSPEC] | SOCK_DGRAM | SOCK_STREAM
343 * family: [PF_UNSPEC] | PF_INET | PF_INET6
346 * numerichost: [0] | 1
348 * Output dictionary may contain the following
349 * All values are encoded as strings.
351 * flags: unsigned long
352 * family: unsigned long
353 * socktype: unsigned long
354 * protocol: unsigned long
355 * port: unsigned long
357 * scopeid: unsigned long
362 static struct addrinfo
*
363 gai_lookupd_process_dictionary(XDR
*inxdr
)
365 int32_t i
, nkeys
, nvals
;
367 uint32_t flags
, family
, socktype
, protocol
, longport
, scopeid
;
369 char *addr
, *canonname
;
376 socktype
= SOCK_UNSPEC
;
377 protocol
= IPPROTO_UNSPEC
;
383 if (!xdr_int(inxdr
, &nkeys
)) return NULL
;
385 for (i
= 0; i
< nkeys
; i
++)
390 if (!xdr_string(inxdr
, &key
, LONG_STRING_LENGTH
)) return NULL
;
391 if (!xdr_int(inxdr
, &nvals
))
403 if (!xdr_string(inxdr
, &val
, LONG_STRING_LENGTH
))
409 if (!strcmp(key
, "flags"))
413 else if (!strcmp(key
, "family"))
417 else if (!strcmp(key
, "socktype"))
419 socktype
= atoi(val
);
421 else if (!strcmp(key
, "protocol"))
423 protocol
= atoi(val
);
425 else if (!strcmp(key
, "port"))
427 longport
= atoi(val
);
430 else if (!strcmp(key
, "scopeid"))
434 else if (!strcmp(key
, "address")) addr
= strdup(val
);
435 else if (!strcmp(key
, "canonname")) canonname
= strdup(val
);
440 if (family
== PF_UNSPEC
)
442 if (addr
!= NULL
) free(addr
);
443 if (canonname
!= NULL
) free(canonname
);
448 if (family
== PF_INET
)
450 inet_aton(addr
, &a4
);
451 a
= new_addrinfo_v4(flags
, socktype
, protocol
, port
, a4
, scopeid
, canonname
);
453 else if (family
== PF_INET6
)
455 inet_pton(AF_INET6
, addr
, &a6
);
456 a
= new_addrinfo_v6(flags
, socktype
, protocol
, port
, a6
, scopeid
, canonname
);
459 if (addr
!= NULL
) free(addr
);
460 if (canonname
!= NULL
) free(canonname
);
466 gai_make_query(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, char *buf
, uint32_t *len
)
468 int32_t numerichost
, family
, proto
, socktype
, canonname
, passive
, parallel
;
471 char str
[64], *cname
;
475 proto
= IPPROTO_UNSPEC
;
476 socktype
= SOCK_UNSPEC
;
484 family
= hints
->ai_family
;
485 if (hints
->ai_flags
& AI_NUMERICHOST
) numerichost
= 1;
486 if (hints
->ai_flags
& AI_CANONNAME
) canonname
= 1;
487 if (hints
->ai_flags
& AI_PASSIVE
) passive
= 1;
488 if (hints
->ai_flags
& AI_PARALLEL
) parallel
= 1;
490 proto
= hints
->ai_protocol
;
491 if (hints
->ai_socktype
== SOCK_DGRAM
)
493 socktype
= SOCK_DGRAM
;
496 if (hints
->ai_socktype
== SOCK_STREAM
)
498 socktype
= SOCK_STREAM
;
503 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
505 /* Attribute count */
507 if (nodename
!= NULL
) na
++;
508 if (servname
!= NULL
) na
++;
509 if (proto
!= IPPROTO_UNSPEC
) na
++;
510 if (socktype
!= SOCK_UNSPEC
) na
++;
511 if (family
!= PF_UNSPEC
) na
++;
512 if (canonname
!= 0) na
++;
513 if (passive
!= 0) na
++;
514 if (parallel
!= 0) na
++;
515 if (numerichost
!= 0) na
++;
517 if (!xdr_int(&outxdr
, (int32_t *)&na
))
519 xdr_destroy(&outxdr
);
523 if (nodename
!= NULL
)
525 if (encode_kv(&outxdr
, "name", nodename
) != 0)
527 xdr_destroy(&outxdr
);
532 if (servname
!= NULL
)
534 if (encode_kv(&outxdr
, "service", servname
) != 0)
536 xdr_destroy(&outxdr
);
541 if (proto
!= IPPROTO_UNSPEC
)
543 snprintf(str
, 64, "%u", proto
);
544 if (encode_kv(&outxdr
, "protocol", str
) != 0)
546 xdr_destroy(&outxdr
);
551 if (socktype
!= SOCK_UNSPEC
)
553 snprintf(str
, 64, "%u", socktype
);
554 if (encode_kv(&outxdr
, "socktype", str
) != 0)
556 xdr_destroy(&outxdr
);
561 if (family
!= PF_UNSPEC
)
563 snprintf(str
, 64, "%u", family
);
564 if (encode_kv(&outxdr
, "family", str
) != 0)
566 xdr_destroy(&outxdr
);
573 if (encode_kv(&outxdr
, "canonname", "1") != 0)
575 xdr_destroy(&outxdr
);
582 if (encode_kv(&outxdr
, "passive", "1") != 0)
584 xdr_destroy(&outxdr
);
591 if (encode_kv(&outxdr
, "parallel", "1") != 0)
593 xdr_destroy(&outxdr
);
598 if (numerichost
!= 0)
600 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
602 xdr_destroy(&outxdr
);
607 *len
= xdr_getpos(&outxdr
);
609 xdr_destroy(&outxdr
);
615 is_a_number(const char *s
)
619 if (s
== NULL
) return 0;
622 for (i
= 0; i
< len
; i
++)
624 if (isdigit(s
[i
]) == 0) return 0;
631 gai_files(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
633 int32_t i
, numericserv
, numerichost
, family
, proto
, wantv4
, wantv6
;
637 char *protoname
, *loopv4
, *loopv6
;
643 if (servname
!= NULL
) numericserv
= is_a_number(servname
);
646 if (hints
!= NULL
) family
= hints
->ai_family
;
650 if (nodename
== NULL
)
654 loopv4
= "127.0.0.1";
655 loopv6
= "0:0:0:0:0:0:0:1";
657 if ((hints
!= NULL
) && ((hints
->ai_flags
& AI_PASSIVE
) == 1))
660 loopv6
= "0:0:0:0:0:0:0:0";
663 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
665 inet_pton(AF_INET
, loopv4
, &a4
);
668 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
670 inet_pton(AF_INET6
, loopv6
, &a6
);
675 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
677 numerichost
= inet_pton(AF_INET
, nodename
, &a4
);
678 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET
;
681 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
683 numerichost
= inet_pton(AF_INET6
, nodename
, &a6
);
684 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET6
;
690 if (family
== PF_INET6
) wantv4
= 0;
691 if (family
== PF_INET
) wantv6
= 0;
693 proto
= IPPROTO_UNSPEC
;
698 proto
= hints
->ai_protocol
;
699 if (proto
== IPPROTO_UNSPEC
)
701 if (hints
->ai_socktype
== SOCK_DGRAM
) proto
= IPPROTO_UDP
;
702 else if (hints
->ai_socktype
== SOCK_STREAM
) proto
= IPPROTO_TCP
;
706 if (proto
== IPPROTO_UDP
) protoname
= "udp";
707 else if (proto
== IPPROTO_TCP
) protoname
= "tcp";
712 if (numericserv
!= 0)
714 port
= htons(atoi(servname
));
716 else if (servname
!= NULL
)
718 s
= getservbyname(servname
, protoname
);
719 if (s
!= NULL
) port
= s
->s_port
;
722 /* new_addrinfo_v4 and new_addrinfo_v6 expect port in host byte order */
725 if (numerichost
!= 0)
729 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
731 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
732 append_addrinfo(res
, a
);
735 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
737 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
738 append_addrinfo(res
, a
);
744 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
746 a
= new_addrinfo_v6(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, 0, NULL
);
747 append_addrinfo(res
, a
);
750 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
752 a
= new_addrinfo_v6(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, 0, NULL
);
753 append_addrinfo(res
, a
);
762 h
= gethostbyname(nodename
);
763 if (h
== NULL
) return 0;
765 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
767 memmove((void *)&a4
.s_addr
, h
->h_addr_list
[i
], h
->h_length
);
769 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
771 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
772 append_addrinfo(res
, a
);
775 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
777 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
778 append_addrinfo(res
, a
);
787 gai_lookupd(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
789 uint32_t n
, i
, qlen
, rlen
;
791 char qbuf
[LU_QBUF_SIZE
];
794 mach_port_t server_port
;
795 kern_return_t status
;
798 server_port
= MACH_PORT_NULL
;
799 if (_lu_running()) server_port
= _lookupd_port(0);
800 if (server_port
== MACH_PORT_NULL
)
802 /* lookupd isn't available - fall back to the flat files */
803 return gai_files(nodename
, servname
, hints
, res
);
808 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
809 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
813 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
814 if (i
!= 0) return EAI_SYSTEM
;
816 qlen
/= BYTES_PER_XDR_UNIT
;
820 status
= _lookup_all(server_port
, gai_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
821 if (status
!= KERN_SUCCESS
) return EAI_NODATA
;
823 rlen
*= BYTES_PER_XDR_UNIT
;
825 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
827 if (!xdr_int(&inxdr
, (int32_t *)&n
))
834 for (i
= 0; i
< n
; i
++)
836 a
= gai_lookupd_process_dictionary(&inxdr
);
837 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
838 append_addrinfo(res
, a
);
842 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
844 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
846 res
[0]->ai_canonname
= strdup(cname
);
853 gai_checkhints(const struct addrinfo
*hints
)
855 if (hints
== NULL
) return 0;
856 if (hints
->ai_addrlen
!= 0) return EAI_BADHINTS
;
857 if (hints
->ai_canonname
!= NULL
) return EAI_BADHINTS
;
858 if (hints
->ai_addr
!= NULL
) return EAI_BADHINTS
;
859 if (hints
->ai_next
!= NULL
) return EAI_BADHINTS
;
861 /* Check for supported protocol family */
862 if (gai_family_type_check(hints
->ai_family
) != 0) return EAI_FAMILY
;
864 /* Check for supported socket */
865 if (gai_socket_type_check(hints
->ai_socktype
) != 0) return EAI_BADHINTS
;
867 /* Check for supported protocol */
868 if (gai_protocol_type_check(hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
870 /* Check that socket type is compatible with protocol */
871 if (gai_socket_protocol_type_check(hints
->ai_socktype
, hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
877 getaddrinfo(const char * __restrict nodename
, const char * __restrict servname
, const struct addrinfo
* __restrict hints
, struct addrinfo
** __restrict res
)
879 int32_t status
, nodenull
, servnull
;
881 if (res
== NULL
) return 0;
886 if ((nodename
== NULL
) || (nodename
[0] == '\0')) nodenull
= 1;
889 if ((servname
== NULL
) || (servname
[0] == '\0')) servnull
= 1;
891 if ((nodenull
== 1) && (servnull
== 1)) return EAI_NONAME
;
892 status
= gai_checkhints(hints
);
893 if (status
!= 0) return status
;
895 if (nodenull
== 1) status
= gai_lookupd(NULL
, servname
, hints
, res
);
896 else if (servnull
== 1) status
= gai_lookupd(nodename
, NULL
, hints
, res
);
897 else status
= gai_lookupd(nodename
, servname
, hints
, res
);
899 if ((status
== 0) && (*res
== NULL
)) status
= EAI_NODATA
;
905 getaddrinfo_async_start(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
, getaddrinfo_async_callback callback
, void *context
)
909 char qbuf
[LU_QBUF_SIZE
];
910 mach_port_t server_port
;
914 if ((nodename
== NULL
) && (servname
== NULL
)) return EAI_NONAME
;
916 status
= gai_checkhints(hints
);
917 if (status
!= 0) return EAI_BADHINTS
;
919 server_port
= MACH_PORT_NULL
;
920 if (_lu_running()) server_port
= _lookupd_port(0);
921 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
925 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
926 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
930 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
931 if (i
!= 0) return EAI_SYSTEM
;
933 qlen
/= BYTES_PER_XDR_UNIT
;
935 return lu_async_start(p
, gai_proc
, qbuf
, qlen
, (void *)callback
, context
);
939 getaddrinfo_async_send(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
)
941 return getaddrinfo_async_start(p
, nodename
, servname
, hints
, NULL
, NULL
);
945 gai_extract_data(char *buf
, uint32_t len
, struct addrinfo
**res
)
954 if (buf
== NULL
) return EAI_NODATA
;
955 if (len
== 0) return EAI_NODATA
;
957 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
959 if (!xdr_int(&xdr
, (int32_t *)&n
))
966 for (i
= 0; i
< n
; i
++)
968 a
= gai_lookupd_process_dictionary(&xdr
);
969 if (a
== NULL
) break;
971 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
972 append_addrinfo(res
, a
);
977 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
979 res
[0]->ai_canonname
= strdup(cname
);
982 if (*res
== NULL
) return EAI_NODATA
;
987 getaddrinfo_async_receive(mach_port_t p
, struct addrinfo
**res
)
989 kern_return_t status
;
993 if (res
== NULL
) return 0;
999 status
= lu_async_receive(p
, &buf
, &len
);
1000 if (status
< 0) return EAI_FAIL
;
1002 status
= gai_extract_data(buf
, len
, res
);
1003 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1004 if (status
!= 0) return status
;
1006 if (*res
== NULL
) return EAI_NODATA
;
1012 getaddrinfo_async_handle_reply(void *msg
)
1014 getaddrinfo_async_callback callback
;
1019 struct addrinfo
*res
;
1021 callback
= (getaddrinfo_async_callback
)NULL
;
1027 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1028 if (status
!= KERN_SUCCESS
)
1030 if (status
== MIG_REPLY_MISMATCH
) return 0;
1031 if (callback
!= NULL
) callback(EAI_NODATA
, NULL
, context
);
1035 status
= gai_extract_data(buf
, len
, &res
);
1036 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1039 if (callback
!= NULL
) callback(status
, NULL
, context
);
1045 callback(EAI_NODATA
, NULL
, context
);
1049 callback(0, res
, context
);
1058 * getnameinfo support in lookupd
1059 * Input dict may contain the following
1061 * ip_address: node address
1062 * ipv6_address: node address
1063 * port: service number
1064 * protocol: [tcp] | udp
1066 * numerichost: [0] | 1
1067 * name_required: [0] | 1
1068 * numericserv: [0] | 1
1070 * Output dictionary may contain the following
1071 * All values are encoded as strings.
1078 gni_lookupd_process_dictionary(XDR
*inxdr
, char **host
, char **serv
)
1080 int32_t i
, j
, nkeys
, nvals
, status
;
1083 if ((host
== NULL
) || (serv
== NULL
)) return EAI_SYSTEM
;
1085 if (!xdr_int(inxdr
, &nkeys
)) return EAI_SYSTEM
;
1090 for (i
= 0; i
< nkeys
; i
++)
1096 status
= _lu_xdr_attribute(inxdr
, &key
, &vals
, (uint32_t *)&nvals
);
1097 if (status
< 0) return EAI_SYSTEM
;
1105 if ((*host
== NULL
) && (!strcmp("name", key
)))
1108 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1111 else if ((*serv
== NULL
) && (!strcmp(key
, "service")))
1114 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1117 if (key
!= NULL
) free(key
);
1125 gni_make_query(const struct sockaddr
*sa
, size_t salen
, int wanthost
, int wantserv
, int flags
, char *buf
, uint32_t *len
)
1129 char str
[_LU_MAXLUSTRLEN
], *key
, ifname
[IF_NAMESIZE
];
1130 uint32_t a4
, ifnum
, offset
, na
, proto
, fqdn
, numerichost
, numericserv
, name_req
, isll
, issl
;
1131 struct sockaddr_in6
*s6
;
1133 if (sa
== NULL
) return EAI_FAIL
;
1134 if (sa
->sa_len
!= salen
) return EAI_FAMILY
;
1136 proto
= IPPROTO_TCP
;
1144 offset
= INET_NTOP_AF_INET_OFFSET
;
1148 if (sa
->sa_family
== PF_INET
)
1150 a4
= ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
1151 if (IN_MULTICAST(a4
) || IN_EXPERIMENTAL(a4
)) flags
|= NI_NUMERICHOST
;
1152 a4
>>= IN_CLASSA_NSHIFT
;
1153 if (a4
== 0) flags
|= NI_NUMERICHOST
;
1155 port
= ((struct sockaddr_in
*)sa
)->sin_port
;
1157 else if (sa
->sa_family
== PF_INET6
)
1159 s6
= (struct sockaddr_in6
*)sa
;
1160 switch (s6
->sin6_addr
.s6_addr
[0])
1163 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
))
1166 else if (IN6_IS_ADDR_LOOPBACK(&s6
->sin6_addr
))
1171 flags
|= NI_NUMERICHOST
;
1175 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
1179 else if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
))
1183 else if (IN6_IS_ADDR_MULTICAST(&s6
->sin6_addr
))
1185 flags
|= NI_NUMERICHOST
;
1190 if ((isll
!= 0) || (issl
!= 0))
1192 ifnum
= s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1];
1193 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1194 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1196 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1197 s6
->sin6_scope_id
= ifnum
;
1198 if ((ifnum
!= 0) && (flags
& NI_NUMERICHOST
)) flags
|= NI_WITHSCOPEID
;
1201 offset
= INET_NTOP_AF_INET6_OFFSET
;
1202 key
= "ipv6_address";
1203 port
= s6
->sin6_port
;
1212 if (wanthost
!= 0) na
++;
1213 if (wantserv
!= 0) na
++;
1215 if (flags
& NI_NOFQDN
)
1221 if (flags
& NI_NUMERICHOST
)
1227 if (flags
& NI_NUMERICSERV
)
1233 if (flags
& NI_NAMEREQD
)
1239 if (flags
& NI_DGRAM
)
1241 proto
= IPPROTO_UDP
;
1245 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
1247 if (!xdr_int(&outxdr
, (int32_t *)&na
))
1249 xdr_destroy(&outxdr
);
1255 inet_ntop(sa
->sa_family
, (char *)(sa
) + offset
, str
, _LU_MAXLUSTRLEN
);
1257 if ((flags
& NI_WITHSCOPEID
) && (sa
->sa_family
== AF_INET6
))
1259 ifnum
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
1260 if (if_indextoname(ifnum
, ifname
) != NULL
)
1263 strcat(str
, ifname
);
1267 if (encode_kv(&outxdr
, key
, str
) != 0)
1269 xdr_destroy(&outxdr
);
1276 snprintf(str
, _LU_MAXLUSTRLEN
, "%hu", port
);
1277 if (encode_kv(&outxdr
, "port", str
) != 0)
1279 xdr_destroy(&outxdr
);
1284 if (proto
== IPPROTO_UDP
)
1286 if (encode_kv(&outxdr
, "protocol", "udp") != 0)
1288 xdr_destroy(&outxdr
);
1295 if (encode_kv(&outxdr
, "fqdn", "0") != 0)
1297 xdr_destroy(&outxdr
);
1302 if (numerichost
== 1)
1304 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
1306 xdr_destroy(&outxdr
);
1311 if (numericserv
== 1)
1313 if (encode_kv(&outxdr
, "numericserv", "1") != 0)
1315 xdr_destroy(&outxdr
);
1322 if (encode_kv(&outxdr
, "name_required", "1") != 0)
1324 xdr_destroy(&outxdr
);
1329 *len
= xdr_getpos(&outxdr
);
1331 xdr_destroy(&outxdr
);
1337 getnameinfo(const struct sockaddr
* __restrict sa
, socklen_t salen
, char * __restrict host
, socklen_t hostlen
, char * __restrict serv
, socklen_t servlen
, int flags
)
1339 uint32_t n
, i
, qlen
, rlen
;
1341 int wanth
, wants
, isll
, issl
;
1343 char qbuf
[LU_QBUF_SIZE
], ifname
[IF_NAMESIZE
];
1344 char *rbuf
, *hval
, *sval
;
1345 mach_port_t server_port
;
1346 kern_return_t status
;
1347 struct sockaddr_in
*s4
;
1348 struct sockaddr_in6
*s6
;
1351 if (sa
== NULL
) return EAI_FAIL
;
1357 if (sa
->sa_family
== AF_INET6
)
1359 s6
= (struct sockaddr_in6
*)sa
;
1361 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) isll
= 1;
1362 if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
)) issl
= 1;
1365 * Link-local and site-local IPv6 addresses may have a scope id
1366 * in s6->sin6_addr.__u6_addr.__u6_addr16[1] as well as in s6->sin6_scope_id.
1367 * If they are both non-zero, they must be equal.
1368 * We zero s6->sin6_addr.__u6_addr.__u6_addr16[1] and set s6->sin6_scope_id.
1370 if ((isll
!= 0) || (issl
!= 0))
1372 ifnum
= ntohs(s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
1373 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1374 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1376 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1377 s6
->sin6_scope_id
= ifnum
;
1380 /* V4 mapped and compat addresses are converted to plain V4 */
1381 if ((IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
)) || (IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
)))
1383 s4
= (struct sockaddr_in
*)calloc(1, sizeof(struct sockaddr_in
));
1384 s4
->sin_len
= sizeof(struct sockaddr_in
);
1385 s4
->sin_family
= AF_INET
;
1386 s4
->sin_port
= s6
->sin6_port
;
1387 memcpy(&(s4
->sin_addr
.s_addr
), &(s6
->sin6_addr
.s6_addr
[12]), 4);
1389 i
= getnameinfo((const struct sockaddr
*)s4
, s4
->sin_len
, host
, hostlen
, serv
, servlen
, flags
);
1396 if ((host
!= NULL
) && (hostlen
!= 0)) wanth
= 1;
1399 if ((serv
!= NULL
) && (servlen
!= 0)) wants
= 1;
1401 if ((wanth
== 0) && (wants
== 0)) return 0;
1404 * Special cases handled by the library
1406 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
))
1408 i
= INET_NTOP_AF_INET_OFFSET
;
1409 if (sa
->sa_family
== PF_INET6
) i
= INET_NTOP_AF_INET6_OFFSET
;
1410 if (inet_ntop(sa
->sa_family
, (char *)(sa
) + i
, host
, hostlen
) == NULL
) return EAI_FAIL
;
1412 if (((isll
!= 0) || (issl
!= 0)) && (ifnum
!= 0))
1414 /* append interface name */
1415 if (if_indextoname(ifnum
, ifname
) != NULL
)
1418 strcat(host
, ifname
);
1422 if (wants
== 0) return 0;
1425 if ((wants
== 1) && (flags
& NI_NUMERICSERV
))
1427 if (sa
->sa_family
== PF_INET
)
1429 s4
= (struct sockaddr_in
*)sa
;
1430 n
= snprintf(serv
, servlen
, "%hu", ntohs(s4
->sin_port
));
1431 if (n
>= servlen
) return EAI_FAIL
;
1433 else if (sa
->sa_family
== PF_INET6
)
1435 s6
= (struct sockaddr_in6
*)sa
;
1436 n
= snprintf(serv
, servlen
, "%hu", ntohs(s6
->sin6_port
));
1437 if (n
>= servlen
) return EAI_FAIL
;
1439 else return EAI_FAMILY
;
1441 if (wanth
== 0) return 0;
1444 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
) && (wants
== 1) && (flags
& NI_NUMERICSERV
)) return 0;
1449 server_port
= MACH_PORT_NULL
;
1450 if (_lu_running()) server_port
= _lookupd_port(0);
1451 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1455 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1456 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1459 qlen
= LU_QBUF_SIZE
;
1460 i
= gni_make_query(sa
, salen
, wanth
, wants
, flags
, qbuf
, &qlen
);
1461 if (i
!= 0) return i
;
1463 qlen
/= BYTES_PER_XDR_UNIT
;
1467 status
= _lookup_all(server_port
, gni_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
1468 if (status
!= KERN_SUCCESS
) return EAI_NONAME
;
1470 rlen
*= BYTES_PER_XDR_UNIT
;
1472 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
1474 if (!xdr_int(&inxdr
, (int32_t *)&n
))
1476 xdr_destroy(&inxdr
);
1482 xdr_destroy(&inxdr
);
1489 i
= gni_lookupd_process_dictionary(&inxdr
, &hval
, &sval
);
1491 xdr_destroy(&inxdr
);
1492 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
1494 if (i
!= 0) return i
;
1497 if (hval
!= NULL
) i
= strlen(hval
) + 1;
1498 if ((host
!= NULL
) && (hostlen
!= 0) && (i
!= 0))
1500 if (i
> hostlen
) return EAI_FAIL
;
1501 memcpy(host
, hval
, i
);
1506 if (sval
!= NULL
) i
= strlen(sval
) + 1;
1507 if ((serv
!= NULL
) && (servlen
!= 0) && (i
!= 0))
1509 if (i
> servlen
) return EAI_FAIL
;
1510 memcpy(serv
, sval
, i
);
1518 getnameinfo_async_start(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
, getnameinfo_async_callback callback
, void *context
)
1521 char qbuf
[LU_QBUF_SIZE
];
1522 mach_port_t server_port
;
1523 kern_return_t status
;
1526 if (sa
== NULL
) return EAI_FAIL
;
1528 server_port
= MACH_PORT_NULL
;
1529 if (_lu_running()) server_port
= _lookupd_port(0);
1530 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1534 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1535 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1538 qlen
= LU_QBUF_SIZE
;
1539 i
= gni_make_query(sa
, salen
, 1, 1, flags
, qbuf
, &qlen
);
1540 if (i
!= 0) return i
;
1542 qlen
/= BYTES_PER_XDR_UNIT
;
1544 return lu_async_start(p
, gni_proc
, qbuf
, qlen
, (void *)callback
, context
);
1548 getnameinfo_async_send(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
)
1550 return getnameinfo_async_start(p
, sa
, salen
, flags
, NULL
, NULL
);
1554 gni_extract_data(char *buf
, uint32_t len
, char **host
, char **serv
)
1563 if (buf
== NULL
) return EAI_NODATA
;
1564 if (len
== 0) return EAI_NODATA
;
1566 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
1568 if (!xdr_int(&xdr
, (int32_t *)&n
))
1580 i
= gni_lookupd_process_dictionary(&xdr
, host
, serv
);
1586 getnameinfo_async_receive(mach_port_t p
, char **host
, char **serv
)
1588 kern_return_t status
;
1595 status
= lu_async_receive(p
, &buf
, &len
);
1596 if (status
< 0) return EAI_FAIL
;
1598 status
= gni_extract_data(buf
, len
, host
, serv
);
1599 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1600 if (status
!= 0) return status
;
1606 getnameinfo_async_handle_reply(void *msg
)
1608 getnameinfo_async_callback callback
;
1610 char *buf
, *hval
, *sval
;
1614 callback
= (getnameinfo_async_callback
)NULL
;
1619 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1620 if (status
!= KERN_SUCCESS
)
1622 if (status
== MIG_REPLY_MISMATCH
) return 0;
1623 if (callback
!= NULL
) callback(EAI_NONAME
, NULL
, NULL
, context
);
1630 status
= gni_extract_data(buf
, len
, &hval
, &sval
);
1631 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1634 if (callback
!= NULL
) callback(status
, NULL
, NULL
, context
);
1638 callback(0, hval
, sval
, context
);