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 const int32_t supported_family
[] =
68 static const int32_t supported_family_count
= (sizeof(supported_family
) / sizeof(supported_family
[0]));
70 static const int32_t supported_socket
[] =
77 static const int32_t supported_socket_count
= (sizeof(supported_socket
) / sizeof(supported_socket
[0]));
79 static const int32_t supported_protocol
[] =
87 static const int32_t supported_protocol_count
= (sizeof(supported_protocol
) / sizeof(supported_protocol
[0]));
89 static const int32_t supported_socket_protocol_pair
[] =
91 SOCK_RAW
, IPPROTO_UNSPEC
,
92 SOCK_RAW
, IPPROTO_UDP
,
93 SOCK_RAW
, IPPROTO_TCP
,
94 SOCK_RAW
, IPPROTO_ICMP
,
95 SOCK_RAW
, IPPROTO_ICMPV6
,
96 SOCK_UNSPEC
, IPPROTO_UNSPEC
,
97 SOCK_UNSPEC
, IPPROTO_UDP
,
98 SOCK_UNSPEC
, IPPROTO_TCP
,
99 SOCK_UNSPEC
, IPPROTO_ICMP
,
100 SOCK_UNSPEC
, IPPROTO_ICMPV6
,
101 SOCK_DGRAM
, IPPROTO_UNSPEC
,
102 SOCK_DGRAM
, IPPROTO_UDP
,
103 SOCK_STREAM
, IPPROTO_UNSPEC
,
104 SOCK_STREAM
, IPPROTO_TCP
106 static const int32_t supported_socket_protocol_pair_count
= (sizeof(supported_socket_protocol_pair
) / (sizeof(supported_socket_protocol_pair
[0]) * 2));
109 gai_family_type_check(int32_t f
)
113 for (i
= 0; i
< supported_family_count
; i
++)
115 if (f
== supported_family
[i
]) return 0;
122 gai_socket_type_check(int32_t s
)
126 for (i
= 0; i
< supported_socket_count
; i
++)
128 if (s
== supported_socket
[i
]) return 0;
135 gai_protocol_type_check(int32_t p
)
139 for (i
= 0; i
< supported_protocol_count
; i
++)
141 if (p
== supported_protocol
[i
]) return 0;
148 gai_socket_protocol_type_check(int32_t s
, int32_t p
)
150 int32_t i
, j
, ss
, sp
;
152 for (i
= 0, j
= 0; i
< supported_socket_protocol_pair_count
; i
++, j
+=2)
154 ss
= supported_socket_protocol_pair
[j
];
155 sp
= supported_socket_protocol_pair
[j
+1];
156 if ((s
== ss
) && (p
== sp
)) return 0;
163 gai_strerror(int32_t err
)
167 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
168 case EAI_AGAIN
: return "Temporary failure in name resolution";
169 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
170 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
171 case EAI_FAMILY
: return "ai_family not supported";
172 case EAI_MEMORY
: return "Memory allocation failure";
173 case EAI_NODATA
: return "No address associated with nodename";
174 case EAI_NONAME
: return "nodename nor servname provided, or not known";
175 case EAI_SERVICE
: return "servname not supported for ai_socktype";
176 case EAI_SOCKTYPE
: return "ai_socktype not supported";
177 case EAI_SYSTEM
: return "System error";
178 case EAI_BADHINTS
: return "Bad hints";
179 case EAI_PROTOCOL
: return "ai_protocol not supported";
182 return "Unknown error";
186 append_addrinfo(struct addrinfo
**l
, struct addrinfo
*a
)
190 if (l
== NULL
) return;
191 if (a
== NULL
) return;
201 if (a
->ai_family
== PF_INET6
)
203 if (x
->ai_family
== PF_INET
)
210 while ((x
->ai_next
!= NULL
) && (x
->ai_next
->ai_family
!= PF_INET
)) x
= x
->ai_next
;
211 a
->ai_next
= x
->ai_next
;
216 while (x
->ai_next
!= NULL
) x
= x
->ai_next
;
223 encode_kv(XDR
*x
, const char *k
, const char *v
)
227 if (!xdr_string(x
, (char **)&k
, _LU_MAXLUSTRLEN
)) return 1;
228 if (!xdr_int(x
, &n
)) return 1;
229 if (!xdr_string(x
, (char **)&v
, _LU_MAXLUSTRLEN
)) return 1;
235 freeaddrinfo(struct addrinfo
*a
)
237 struct addrinfo
*next
;
242 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
243 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
249 static struct addrinfo
*
250 new_addrinfo_v4(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in_addr addr
, uint32_t iface
, char *cname
)
253 struct sockaddr_in
*sa
;
256 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
257 if (a
== NULL
) return NULL
;
262 a
->ai_family
= PF_INET
;
263 a
->ai_socktype
= sock
;
264 a
->ai_protocol
= proto
;
266 a
->ai_addrlen
= sizeof(struct sockaddr_in
);
268 sa
= (struct sockaddr_in
*)calloc(1, a
->ai_addrlen
);
275 sa
->sin_len
= a
->ai_addrlen
;
276 sa
->sin_family
= PF_INET
;
277 sa
->sin_port
= htons(port
);
280 /* Kludge: Jam the interface number into sin_zero. */
281 memmove(sa
->sin_zero
, &iface
, sizeof(uint32_t));
283 a
->ai_addr
= (struct sockaddr
*)sa
;
287 len
= strlen(cname
) + 1;
288 a
->ai_canonname
= malloc(len
);
289 memmove(a
->ai_canonname
, cname
, len
);
295 static struct addrinfo
*
296 new_addrinfo_v6(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in6_addr addr
, uint32_t scopeid
, char *cname
)
299 struct sockaddr_in6
*sa
;
302 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
303 if (a
== NULL
) return NULL
;
308 a
->ai_family
= PF_INET6
;
309 a
->ai_socktype
= sock
;
310 a
->ai_protocol
= proto
;
312 a
->ai_addrlen
= sizeof(struct sockaddr_in6
);
314 sa
= (struct sockaddr_in6
*)calloc(1, a
->ai_addrlen
);
321 sa
->sin6_len
= a
->ai_addrlen
;
322 sa
->sin6_family
= PF_INET6
;
323 sa
->sin6_port
= htons(port
);
324 sa
->sin6_addr
= addr
;
325 sa
->sin6_scope_id
= scopeid
;
326 a
->ai_addr
= (struct sockaddr
*)sa
;
330 len
= strlen(cname
) + 1;
331 a
->ai_canonname
= malloc(len
);
332 memmove(a
->ai_canonname
, cname
, len
);
339 * getaddrinfo support in lookupd
340 * Input dict may contain the following
344 * protocol: [IPPROTO_UNSPEC] | IPPROTO_UDP | IPPROTO_TCP
345 * socktype: [SOCK_UNSPEC] | SOCK_DGRAM | SOCK_STREAM
346 * family: [PF_UNSPEC] | PF_INET | PF_INET6
349 * numerichost: [0] | 1
351 * Output dictionary may contain the following
352 * All values are encoded as strings.
354 * flags: unsigned long
355 * family: unsigned long
356 * socktype: unsigned long
357 * protocol: unsigned long
358 * port: unsigned long
360 * scopeid: unsigned long
365 static struct addrinfo
*
366 gai_lookupd_process_dictionary(XDR
*inxdr
)
368 int32_t i
, nkeys
, nvals
;
370 uint32_t flags
, family
, socktype
, protocol
, longport
, scopeid
;
372 char *addr
, *canonname
;
379 socktype
= SOCK_UNSPEC
;
380 protocol
= IPPROTO_UNSPEC
;
386 if (!xdr_int(inxdr
, &nkeys
)) return NULL
;
388 for (i
= 0; i
< nkeys
; i
++)
393 if (!xdr_string(inxdr
, &key
, LONG_STRING_LENGTH
)) return NULL
;
394 if (!xdr_int(inxdr
, &nvals
))
406 if (!xdr_string(inxdr
, &val
, LONG_STRING_LENGTH
))
412 if (!strcmp(key
, "flags"))
416 else if (!strcmp(key
, "family"))
420 else if (!strcmp(key
, "socktype"))
422 socktype
= atoi(val
);
424 else if (!strcmp(key
, "protocol"))
426 protocol
= atoi(val
);
428 else if (!strcmp(key
, "port"))
430 longport
= atoi(val
);
433 else if (!strcmp(key
, "scopeid"))
437 else if (!strcmp(key
, "address")) addr
= strdup(val
);
438 else if (!strcmp(key
, "canonname")) canonname
= strdup(val
);
443 if (family
== PF_UNSPEC
)
445 if (addr
!= NULL
) free(addr
);
446 if (canonname
!= NULL
) free(canonname
);
451 if (family
== PF_INET
)
453 inet_aton(addr
, &a4
);
454 a
= new_addrinfo_v4(flags
, socktype
, protocol
, port
, a4
, scopeid
, canonname
);
456 else if (family
== PF_INET6
)
458 inet_pton(AF_INET6
, addr
, &a6
);
459 a
= new_addrinfo_v6(flags
, socktype
, protocol
, port
, a6
, scopeid
, canonname
);
462 if (addr
!= NULL
) free(addr
);
463 if (canonname
!= NULL
) free(canonname
);
469 gai_make_query(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, char *buf
, uint32_t *len
)
471 int32_t numerichost
, family
, proto
, socktype
, canonname
, passive
, parallel
;
474 char str
[64], *cname
;
478 proto
= IPPROTO_UNSPEC
;
479 socktype
= SOCK_UNSPEC
;
487 family
= hints
->ai_family
;
488 if (hints
->ai_flags
& AI_NUMERICHOST
) numerichost
= 1;
489 if (hints
->ai_flags
& AI_CANONNAME
) canonname
= 1;
490 if (hints
->ai_flags
& AI_PASSIVE
) passive
= 1;
491 if (hints
->ai_flags
& AI_PARALLEL
) parallel
= 1;
493 proto
= hints
->ai_protocol
;
494 if (hints
->ai_socktype
== SOCK_DGRAM
)
496 socktype
= SOCK_DGRAM
;
499 if (hints
->ai_socktype
== SOCK_STREAM
)
501 socktype
= SOCK_STREAM
;
506 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
508 /* Attribute count */
510 if (nodename
!= NULL
) na
++;
511 if (servname
!= NULL
) na
++;
512 if (proto
!= IPPROTO_UNSPEC
) na
++;
513 if (socktype
!= SOCK_UNSPEC
) na
++;
514 if (family
!= PF_UNSPEC
) na
++;
515 if (canonname
!= 0) na
++;
516 if (passive
!= 0) na
++;
517 if (parallel
!= 0) na
++;
518 if (numerichost
!= 0) na
++;
520 if (!xdr_int(&outxdr
, (int32_t *)&na
))
522 xdr_destroy(&outxdr
);
526 if (nodename
!= NULL
)
528 if (encode_kv(&outxdr
, "name", nodename
) != 0)
530 xdr_destroy(&outxdr
);
535 if (servname
!= NULL
)
537 if (encode_kv(&outxdr
, "service", servname
) != 0)
539 xdr_destroy(&outxdr
);
544 if (proto
!= IPPROTO_UNSPEC
)
546 snprintf(str
, 64, "%u", proto
);
547 if (encode_kv(&outxdr
, "protocol", str
) != 0)
549 xdr_destroy(&outxdr
);
554 if (socktype
!= SOCK_UNSPEC
)
556 snprintf(str
, 64, "%u", socktype
);
557 if (encode_kv(&outxdr
, "socktype", str
) != 0)
559 xdr_destroy(&outxdr
);
564 if (family
!= PF_UNSPEC
)
566 snprintf(str
, 64, "%u", family
);
567 if (encode_kv(&outxdr
, "family", str
) != 0)
569 xdr_destroy(&outxdr
);
576 if (encode_kv(&outxdr
, "canonname", "1") != 0)
578 xdr_destroy(&outxdr
);
585 if (encode_kv(&outxdr
, "passive", "1") != 0)
587 xdr_destroy(&outxdr
);
594 if (encode_kv(&outxdr
, "parallel", "1") != 0)
596 xdr_destroy(&outxdr
);
601 if (numerichost
!= 0)
603 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
605 xdr_destroy(&outxdr
);
610 *len
= xdr_getpos(&outxdr
);
612 xdr_destroy(&outxdr
);
618 is_a_number(const char *s
)
622 if (s
== NULL
) return 0;
625 for (i
= 0; i
< len
; i
++)
627 if (isdigit(s
[i
]) == 0) return 0;
634 gai_trivial(struct in_addr
*in4
, struct in6_addr
*in6
, int16_t port
, const struct addrinfo
*hints
, struct addrinfo
**res
)
636 int32_t family
, wantv4
, wantv6
, proto
;
637 char *loopv4
, *loopv6
;
643 if (hints
!= NULL
) family
= hints
->ai_family
;
648 if (family
== PF_INET6
) wantv4
= 0;
649 if (family
== PF_INET
) wantv6
= 0;
651 memset(&a4
, 0, sizeof(struct in_addr
));
652 memset(&a6
, 0, sizeof(struct in6_addr
));
654 if ((in4
== NULL
) && (in6
== NULL
))
656 loopv4
= "127.0.0.1";
657 loopv6
= "0:0:0:0:0:0:0:1";
659 if ((hints
!= NULL
) && ((hints
->ai_flags
& AI_PASSIVE
) == 1))
662 loopv6
= "0:0:0:0:0:0:0:0";
665 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
667 inet_pton(AF_INET
, loopv4
, &a4
);
670 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
672 inet_pton(AF_INET6
, loopv6
, &a6
);
675 else if (in4
== NULL
)
677 if (family
== PF_INET
) return EAI_BADHINTS
;
680 memcpy(&a6
, in6
, sizeof(struct in6_addr
));
682 else if (in6
== NULL
)
684 if (family
== PF_INET6
) return EAI_BADHINTS
;
687 memcpy(&a4
, in4
, sizeof(struct in_addr
));
694 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
;
708 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
710 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
711 append_addrinfo(res
, a
);
714 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
716 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
717 append_addrinfo(res
, a
);
720 if (proto
== IPPROTO_ICMP
)
722 a
= new_addrinfo_v4(0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, NULL
);
723 append_addrinfo(res
, a
);
729 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
731 a
= new_addrinfo_v6(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, 0, NULL
);
732 append_addrinfo(res
, a
);
735 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
737 a
= new_addrinfo_v6(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, 0, NULL
);
738 append_addrinfo(res
, a
);
741 if (proto
== IPPROTO_ICMPV6
)
743 a
= new_addrinfo_v6(0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, 0, NULL
);
744 append_addrinfo(res
, a
);
752 gai_files(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
754 int32_t i
, status
, numericserv
, numerichost
, family
, proto
, wantv4
, wantv6
;
758 char *protoname
, *loopv4
, *loopv6
;
764 if (servname
!= NULL
) numericserv
= is_a_number(servname
);
767 if (hints
!= NULL
) family
= hints
->ai_family
;
771 if (nodename
== NULL
)
775 loopv4
= "127.0.0.1";
776 loopv6
= "0:0:0:0:0:0:0:1";
778 if ((hints
!= NULL
) && ((hints
->ai_flags
& AI_PASSIVE
) == 1))
781 loopv6
= "0:0:0:0:0:0:0:0";
784 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
786 inet_pton(AF_INET
, loopv4
, &a4
);
789 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
791 inet_pton(AF_INET6
, loopv6
, &a6
);
796 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
798 status
= inet_pton(AF_INET
, nodename
, &a4
);
802 if (family
== PF_UNSPEC
) family
= PF_INET
;
806 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
808 status
= inet_pton(AF_INET6
, nodename
, &a6
);
812 if (family
== PF_UNSPEC
) family
= PF_INET6
;
819 if (family
== PF_INET6
) wantv4
= 0;
820 if (family
== PF_INET
) wantv6
= 0;
822 proto
= IPPROTO_UNSPEC
;
827 proto
= hints
->ai_protocol
;
828 if (proto
== IPPROTO_UNSPEC
)
830 if (hints
->ai_socktype
== SOCK_DGRAM
) proto
= IPPROTO_UDP
;
831 else if (hints
->ai_socktype
== SOCK_STREAM
) proto
= IPPROTO_TCP
;
835 if (proto
== IPPROTO_UDP
) protoname
= "udp";
836 else if (proto
== IPPROTO_TCP
) protoname
= "tcp";
841 if (numericserv
!= 0)
843 port
= htons(atoi(servname
));
845 else if (servname
!= NULL
)
847 s
= getservbyname(servname
, protoname
);
848 if (s
!= NULL
) port
= s
->s_port
;
851 /* new_addrinfo_v4 and new_addrinfo_v6 expect port in host byte order */
854 if (numerichost
!= 0)
858 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
860 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
861 append_addrinfo(res
, a
);
864 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
866 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
867 append_addrinfo(res
, a
);
870 if (proto
== IPPROTO_ICMP
)
872 a
= new_addrinfo_v4(0, SOCK_RAW
, IPPROTO_ICMP
, port
, a4
, 0, NULL
);
873 append_addrinfo(res
, a
);
879 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
881 a
= new_addrinfo_v6(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, 0, NULL
);
882 append_addrinfo(res
, a
);
885 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
887 a
= new_addrinfo_v6(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, 0, NULL
);
888 append_addrinfo(res
, a
);
891 if (proto
== IPPROTO_ICMPV6
)
893 a
= new_addrinfo_v6(0, SOCK_RAW
, IPPROTO_ICMPV6
, port
, a6
, 0, NULL
);
894 append_addrinfo(res
, a
);
903 h
= gethostbyname(nodename
);
904 if (h
== NULL
) return 0;
906 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
908 memmove((void *)&a4
.s_addr
, h
->h_addr_list
[i
], h
->h_length
);
910 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
912 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
913 append_addrinfo(res
, a
);
916 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
918 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
919 append_addrinfo(res
, a
);
928 gai_lookupd(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
930 uint32_t n
, i
, qlen
, rlen
;
932 char qbuf
[LU_QBUF_SIZE
];
935 mach_port_t server_port
;
936 kern_return_t status
;
939 server_port
= MACH_PORT_NULL
;
940 if (_lu_running()) server_port
= _lookupd_port(0);
941 if (server_port
== MACH_PORT_NULL
)
943 /* lookupd isn't available - fall back to the flat files */
944 return gai_files(nodename
, servname
, hints
, res
);
949 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
950 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
954 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
955 if (i
!= 0) return EAI_SYSTEM
;
957 qlen
/= BYTES_PER_XDR_UNIT
;
961 status
= _lookup_all(server_port
, gai_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
962 if (status
!= KERN_SUCCESS
) return EAI_NODATA
;
964 rlen
*= BYTES_PER_XDR_UNIT
;
966 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
968 if (!xdr_int(&inxdr
, (int32_t *)&n
))
975 for (i
= 0; i
< n
; i
++)
977 a
= gai_lookupd_process_dictionary(&inxdr
);
978 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
979 append_addrinfo(res
, a
);
983 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
985 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
987 res
[0]->ai_canonname
= strdup(cname
);
994 gai_checkhints(const struct addrinfo
*hints
)
996 if (hints
== NULL
) return 0;
997 if (hints
->ai_addrlen
!= 0) return EAI_BADHINTS
;
998 if (hints
->ai_canonname
!= NULL
) return EAI_BADHINTS
;
999 if (hints
->ai_addr
!= NULL
) return EAI_BADHINTS
;
1000 if (hints
->ai_next
!= NULL
) return EAI_BADHINTS
;
1002 /* Check for supported protocol family */
1003 if (gai_family_type_check(hints
->ai_family
) != 0) return EAI_FAMILY
;
1005 /* Check for supported socket */
1006 if (gai_socket_type_check(hints
->ai_socktype
) != 0) return EAI_BADHINTS
;
1008 /* Check for supported protocol */
1009 if (gai_protocol_type_check(hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
1011 /* Check that socket type is compatible with protocol */
1012 if (gai_socket_protocol_type_check(hints
->ai_socktype
, hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
1018 getaddrinfo(const char * __restrict nodename
, const char * __restrict servname
, const struct addrinfo
* __restrict hints
, struct addrinfo
** __restrict res
)
1020 int32_t status
, nodenull
, servnull
;
1021 int32_t numericserv
, numerichost
, family
;
1023 struct in_addr a4
, *p4
;
1024 struct in6_addr a6
, *p6
;
1026 if (res
== NULL
) return 0;
1031 if ((nodename
== NULL
) || (nodename
[0] == '\0')) nodenull
= 1;
1034 if ((servname
== NULL
) || (servname
[0] == '\0')) servnull
= 1;
1036 if ((nodenull
== 1) && (servnull
== 1)) return EAI_NONAME
;
1038 status
= gai_checkhints(hints
);
1039 if (status
!= 0) return status
;
1042 * Trap the "trivial" cases that can be answered without a query.
1043 * (nodename == numeric) && (servname == NULL)
1044 * (nodename == numeric) && (servname == numeric)
1045 * (nodename == NULL) && (servname == numeric)
1050 memset(&a4
, 0, sizeof(struct in_addr
));
1051 memset(&a6
, 0, sizeof(struct in6_addr
));
1055 if (servnull
== 0) numericserv
= is_a_number(servname
);
1056 if (numericserv
== 1) port
= atoi(servname
);
1059 if (hints
!= NULL
) family
= hints
->ai_family
;
1064 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
1066 status
= inet_pton(AF_INET
, nodename
, &a4
);
1074 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
1076 status
= inet_pton(AF_INET6
, nodename
, &a6
);
1085 if ((nodenull
== 1) && (numericserv
== 1)) return gai_trivial(NULL
, NULL
, port
, hints
, res
);
1086 if ((numerichost
== 1) && (numericserv
== 1)) return gai_trivial(p4
, p6
, port
, hints
, res
);
1087 if ((numerichost
== 1) && (servnull
== 1)) return gai_trivial(p4
, p6
, 0, hints
, res
);
1089 if (nodenull
== 1) status
= gai_lookupd(NULL
, servname
, hints
, res
);
1090 else if (servnull
== 1) status
= gai_lookupd(nodename
, NULL
, hints
, res
);
1091 else status
= gai_lookupd(nodename
, servname
, hints
, res
);
1093 if ((status
== 0) && (*res
== NULL
)) status
= EAI_NODATA
;
1099 getaddrinfo_async_start(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
, getaddrinfo_async_callback callback
, void *context
)
1103 char qbuf
[LU_QBUF_SIZE
];
1104 mach_port_t server_port
;
1106 *p
= MACH_PORT_NULL
;
1108 if ((nodename
== NULL
) && (servname
== NULL
)) return EAI_NONAME
;
1110 status
= gai_checkhints(hints
);
1111 if (status
!= 0) return EAI_BADHINTS
;
1113 server_port
= MACH_PORT_NULL
;
1114 if (_lu_running()) server_port
= _lookupd_port(0);
1115 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1119 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
1120 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1123 qlen
= LU_QBUF_SIZE
;
1124 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
1125 if (i
!= 0) return EAI_SYSTEM
;
1127 qlen
/= BYTES_PER_XDR_UNIT
;
1129 return lu_async_start(p
, gai_proc
, qbuf
, qlen
, (void *)callback
, context
);
1133 getaddrinfo_async_send(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
)
1135 return getaddrinfo_async_start(p
, nodename
, servname
, hints
, NULL
, NULL
);
1139 gai_extract_data(char *buf
, uint32_t len
, struct addrinfo
**res
)
1148 if (buf
== NULL
) return EAI_NODATA
;
1149 if (len
== 0) return EAI_NODATA
;
1151 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
1153 if (!xdr_int(&xdr
, (int32_t *)&n
))
1160 for (i
= 0; i
< n
; i
++)
1162 a
= gai_lookupd_process_dictionary(&xdr
);
1163 if (a
== NULL
) break;
1165 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
1166 append_addrinfo(res
, a
);
1171 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
1173 res
[0]->ai_canonname
= strdup(cname
);
1176 if (*res
== NULL
) return EAI_NODATA
;
1181 getaddrinfo_async_receive(mach_port_t p
, struct addrinfo
**res
)
1183 kern_return_t status
;
1187 if (res
== NULL
) return 0;
1193 status
= lu_async_receive(p
, &buf
, &len
);
1194 if (status
< 0) return EAI_FAIL
;
1196 status
= gai_extract_data(buf
, len
, res
);
1197 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1198 if (status
!= 0) return status
;
1200 if (*res
== NULL
) return EAI_NODATA
;
1206 getaddrinfo_async_handle_reply(void *msg
)
1208 getaddrinfo_async_callback callback
;
1213 struct addrinfo
*res
;
1215 callback
= (getaddrinfo_async_callback
)NULL
;
1221 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1222 if (status
!= KERN_SUCCESS
)
1224 if (status
== MIG_REPLY_MISMATCH
) return 0;
1225 if (callback
!= NULL
) callback(EAI_NODATA
, NULL
, context
);
1229 status
= gai_extract_data(buf
, len
, &res
);
1230 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1233 if (callback
!= NULL
) callback(status
, NULL
, context
);
1239 callback(EAI_NODATA
, NULL
, context
);
1243 callback(0, res
, context
);
1252 * getnameinfo support in lookupd
1253 * Input dict may contain the following
1255 * ip_address: node address
1256 * ipv6_address: node address
1257 * port: service number
1258 * protocol: [tcp] | udp
1260 * numerichost: [0] | 1
1261 * name_required: [0] | 1
1262 * numericserv: [0] | 1
1264 * Output dictionary may contain the following
1265 * All values are encoded as strings.
1272 gni_lookupd_process_dictionary(XDR
*inxdr
, char **host
, char **serv
)
1274 int32_t i
, j
, nkeys
, nvals
, status
;
1277 if ((host
== NULL
) || (serv
== NULL
)) return EAI_SYSTEM
;
1279 if (!xdr_int(inxdr
, &nkeys
)) return EAI_SYSTEM
;
1284 for (i
= 0; i
< nkeys
; i
++)
1290 status
= _lu_xdr_attribute(inxdr
, &key
, &vals
, (uint32_t *)&nvals
);
1291 if (status
< 0) return EAI_SYSTEM
;
1299 if ((*host
== NULL
) && (!strcmp("name", key
)))
1302 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1305 else if ((*serv
== NULL
) && (!strcmp(key
, "service")))
1308 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1311 if (key
!= NULL
) free(key
);
1319 gni_make_query(const struct sockaddr
*sa
, size_t salen
, int wanthost
, int wantserv
, int flags
, char *buf
, uint32_t *len
)
1323 char str
[_LU_MAXLUSTRLEN
], *key
, ifname
[IF_NAMESIZE
];
1324 uint32_t a4
, ifnum
, offset
, na
, proto
, fqdn
, numerichost
, numericserv
, name_req
, isll
, issl
;
1325 struct sockaddr_in6
*s6
;
1327 if (sa
== NULL
) return EAI_FAIL
;
1328 if (sa
->sa_len
!= salen
) return EAI_FAMILY
;
1330 proto
= IPPROTO_TCP
;
1338 offset
= INET_NTOP_AF_INET_OFFSET
;
1342 if (sa
->sa_family
== PF_INET
)
1344 a4
= ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
1345 if (IN_MULTICAST(a4
) || IN_EXPERIMENTAL(a4
)) flags
|= NI_NUMERICHOST
;
1346 a4
>>= IN_CLASSA_NSHIFT
;
1347 if (a4
== 0) flags
|= NI_NUMERICHOST
;
1349 port
= ((struct sockaddr_in
*)sa
)->sin_port
;
1351 else if (sa
->sa_family
== PF_INET6
)
1353 s6
= (struct sockaddr_in6
*)sa
;
1354 switch (s6
->sin6_addr
.s6_addr
[0])
1357 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
))
1360 else if (IN6_IS_ADDR_LOOPBACK(&s6
->sin6_addr
))
1365 flags
|= NI_NUMERICHOST
;
1369 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
1373 else if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
))
1377 else if (IN6_IS_ADDR_MULTICAST(&s6
->sin6_addr
))
1379 flags
|= NI_NUMERICHOST
;
1384 if ((isll
!= 0) || (issl
!= 0))
1386 ifnum
= s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1];
1387 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1388 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1390 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1391 s6
->sin6_scope_id
= ifnum
;
1392 if ((ifnum
!= 0) && (flags
& NI_NUMERICHOST
)) flags
|= NI_WITHSCOPEID
;
1395 offset
= INET_NTOP_AF_INET6_OFFSET
;
1396 key
= "ipv6_address";
1397 port
= s6
->sin6_port
;
1406 if (wanthost
!= 0) na
++;
1407 if (wantserv
!= 0) na
++;
1409 if (flags
& NI_NOFQDN
)
1415 if (flags
& NI_NUMERICHOST
)
1421 if (flags
& NI_NUMERICSERV
)
1427 if (flags
& NI_NAMEREQD
)
1433 if (flags
& NI_DGRAM
)
1435 proto
= IPPROTO_UDP
;
1439 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
1441 if (!xdr_int(&outxdr
, (int32_t *)&na
))
1443 xdr_destroy(&outxdr
);
1449 inet_ntop(sa
->sa_family
, (char *)(sa
) + offset
, str
, _LU_MAXLUSTRLEN
);
1451 if ((flags
& NI_WITHSCOPEID
) && (sa
->sa_family
== AF_INET6
))
1453 ifnum
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
1454 if (if_indextoname(ifnum
, ifname
) != NULL
)
1457 strcat(str
, ifname
);
1461 if (encode_kv(&outxdr
, key
, str
) != 0)
1463 xdr_destroy(&outxdr
);
1470 snprintf(str
, _LU_MAXLUSTRLEN
, "%hu", port
);
1471 if (encode_kv(&outxdr
, "port", str
) != 0)
1473 xdr_destroy(&outxdr
);
1478 if (proto
== IPPROTO_UDP
)
1480 if (encode_kv(&outxdr
, "protocol", "udp") != 0)
1482 xdr_destroy(&outxdr
);
1489 if (encode_kv(&outxdr
, "fqdn", "0") != 0)
1491 xdr_destroy(&outxdr
);
1496 if (numerichost
== 1)
1498 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
1500 xdr_destroy(&outxdr
);
1505 if (numericserv
== 1)
1507 if (encode_kv(&outxdr
, "numericserv", "1") != 0)
1509 xdr_destroy(&outxdr
);
1516 if (encode_kv(&outxdr
, "name_required", "1") != 0)
1518 xdr_destroy(&outxdr
);
1523 *len
= xdr_getpos(&outxdr
);
1525 xdr_destroy(&outxdr
);
1531 getnameinfo(const struct sockaddr
* __restrict sa
, socklen_t salen
, char * __restrict host
, socklen_t hostlen
, char * __restrict serv
, socklen_t servlen
, int flags
)
1533 uint32_t n
, i
, qlen
, rlen
;
1535 int wanth
, wants
, isll
, issl
;
1537 char qbuf
[LU_QBUF_SIZE
], ifname
[IF_NAMESIZE
];
1538 char *rbuf
, *hval
, *sval
;
1539 mach_port_t server_port
;
1540 kern_return_t status
;
1541 struct sockaddr_in
*s4
;
1542 struct sockaddr_in6
*s6
;
1545 if (sa
== NULL
) return EAI_FAIL
;
1551 if (sa
->sa_family
== AF_INET6
)
1553 s6
= (struct sockaddr_in6
*)sa
;
1555 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) isll
= 1;
1556 if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
)) issl
= 1;
1559 * Link-local and site-local IPv6 addresses may have a scope id
1560 * in s6->sin6_addr.__u6_addr.__u6_addr16[1] as well as in s6->sin6_scope_id.
1561 * If they are both non-zero, they must be equal.
1562 * We zero s6->sin6_addr.__u6_addr.__u6_addr16[1] and set s6->sin6_scope_id.
1564 if ((isll
!= 0) || (issl
!= 0))
1566 ifnum
= ntohs(s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
1567 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1568 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1570 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1571 s6
->sin6_scope_id
= ifnum
;
1574 /* V4 mapped and compat addresses are converted to plain V4 */
1575 if ((IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
)) || (IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
)))
1577 s4
= (struct sockaddr_in
*)calloc(1, sizeof(struct sockaddr_in
));
1578 s4
->sin_len
= sizeof(struct sockaddr_in
);
1579 s4
->sin_family
= AF_INET
;
1580 s4
->sin_port
= s6
->sin6_port
;
1581 memcpy(&(s4
->sin_addr
.s_addr
), &(s6
->sin6_addr
.s6_addr
[12]), 4);
1583 i
= getnameinfo((const struct sockaddr
*)s4
, s4
->sin_len
, host
, hostlen
, serv
, servlen
, flags
);
1590 if ((host
!= NULL
) && (hostlen
!= 0)) wanth
= 1;
1593 if ((serv
!= NULL
) && (servlen
!= 0)) wants
= 1;
1595 if ((wanth
== 0) && (wants
== 0)) return 0;
1598 * Special cases handled by the library
1600 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
))
1602 i
= INET_NTOP_AF_INET_OFFSET
;
1603 if (sa
->sa_family
== PF_INET6
) i
= INET_NTOP_AF_INET6_OFFSET
;
1604 if (inet_ntop(sa
->sa_family
, (char *)(sa
) + i
, host
, hostlen
) == NULL
) return EAI_FAIL
;
1606 if (((isll
!= 0) || (issl
!= 0)) && (ifnum
!= 0))
1608 /* append interface name */
1609 if (if_indextoname(ifnum
, ifname
) != NULL
)
1612 strcat(host
, ifname
);
1616 if (wants
== 0) return 0;
1619 if ((wants
== 1) && (flags
& NI_NUMERICSERV
))
1621 if (sa
->sa_family
== PF_INET
)
1623 s4
= (struct sockaddr_in
*)sa
;
1624 n
= snprintf(serv
, servlen
, "%hu", ntohs(s4
->sin_port
));
1625 if (n
>= servlen
) return EAI_FAIL
;
1627 else if (sa
->sa_family
== PF_INET6
)
1629 s6
= (struct sockaddr_in6
*)sa
;
1630 n
= snprintf(serv
, servlen
, "%hu", ntohs(s6
->sin6_port
));
1631 if (n
>= servlen
) return EAI_FAIL
;
1633 else return EAI_FAMILY
;
1635 if (wanth
== 0) return 0;
1638 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
) && (wants
== 1) && (flags
& NI_NUMERICSERV
)) return 0;
1643 server_port
= MACH_PORT_NULL
;
1644 if (_lu_running()) server_port
= _lookupd_port(0);
1645 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1649 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1650 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1653 qlen
= LU_QBUF_SIZE
;
1654 i
= gni_make_query(sa
, salen
, wanth
, wants
, flags
, qbuf
, &qlen
);
1655 if (i
!= 0) return i
;
1657 qlen
/= BYTES_PER_XDR_UNIT
;
1661 status
= _lookup_all(server_port
, gni_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
1662 if (status
!= KERN_SUCCESS
) return EAI_NONAME
;
1664 rlen
*= BYTES_PER_XDR_UNIT
;
1666 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
1668 if (!xdr_int(&inxdr
, (int32_t *)&n
))
1670 xdr_destroy(&inxdr
);
1676 xdr_destroy(&inxdr
);
1683 i
= gni_lookupd_process_dictionary(&inxdr
, &hval
, &sval
);
1685 xdr_destroy(&inxdr
);
1686 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
1688 if (i
!= 0) return i
;
1691 if (hval
!= NULL
) i
= strlen(hval
) + 1;
1692 if ((host
!= NULL
) && (hostlen
!= 0) && (i
!= 0))
1694 if (i
> hostlen
) return EAI_FAIL
;
1695 memcpy(host
, hval
, i
);
1700 if (sval
!= NULL
) i
= strlen(sval
) + 1;
1701 if ((serv
!= NULL
) && (servlen
!= 0) && (i
!= 0))
1703 if (i
> servlen
) return EAI_FAIL
;
1704 memcpy(serv
, sval
, i
);
1712 getnameinfo_async_start(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
, getnameinfo_async_callback callback
, void *context
)
1715 char qbuf
[LU_QBUF_SIZE
];
1716 mach_port_t server_port
;
1717 kern_return_t status
;
1720 if (sa
== NULL
) return EAI_FAIL
;
1722 server_port
= MACH_PORT_NULL
;
1723 if (_lu_running()) server_port
= _lookupd_port(0);
1724 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1728 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1729 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1732 qlen
= LU_QBUF_SIZE
;
1733 i
= gni_make_query(sa
, salen
, 1, 1, flags
, qbuf
, &qlen
);
1734 if (i
!= 0) return i
;
1736 qlen
/= BYTES_PER_XDR_UNIT
;
1738 return lu_async_start(p
, gni_proc
, qbuf
, qlen
, (void *)callback
, context
);
1742 getnameinfo_async_send(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
)
1744 return getnameinfo_async_start(p
, sa
, salen
, flags
, NULL
, NULL
);
1748 gni_extract_data(char *buf
, uint32_t len
, char **host
, char **serv
)
1757 if (buf
== NULL
) return EAI_NODATA
;
1758 if (len
== 0) return EAI_NODATA
;
1760 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
1762 if (!xdr_int(&xdr
, (int32_t *)&n
))
1774 i
= gni_lookupd_process_dictionary(&xdr
, host
, serv
);
1780 getnameinfo_async_receive(mach_port_t p
, char **host
, char **serv
)
1782 kern_return_t status
;
1789 status
= lu_async_receive(p
, &buf
, &len
);
1790 if (status
< 0) return EAI_FAIL
;
1792 status
= gni_extract_data(buf
, len
, host
, serv
);
1793 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1794 if (status
!= 0) return status
;
1800 getnameinfo_async_handle_reply(void *msg
)
1802 getnameinfo_async_callback callback
;
1804 char *buf
, *hval
, *sval
;
1808 callback
= (getnameinfo_async_callback
)NULL
;
1813 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1814 if (status
!= KERN_SUCCESS
)
1816 if (status
== MIG_REPLY_MISMATCH
) return 0;
1817 if (callback
!= NULL
) callback(EAI_NONAME
, NULL
, NULL
, context
);
1824 status
= gni_extract_data(buf
, len
, &hval
, &sval
);
1825 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1828 if (callback
!= NULL
) callback(status
, NULL
, NULL
, context
);
1832 callback(0, hval
, sval
, context
);