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>
38 #include <rpc/types.h>
40 #include <mach/mach.h>
41 #include <servers/bootstrap.h>
44 #include "netdb_async.h"
47 #define IPPROTO_UNSPEC 0
49 #define LONG_STRING_LENGTH 8192
50 #define _LU_MAXLUSTRLEN 256
51 #define LU_QBUF_SIZE 8192
53 #define MAX_LOOKUP_ATTEMPTS 10
55 #define INET_NTOP_AF_INET_OFFSET 4
56 #define INET_NTOP_AF_INET6_OFFSET 8
58 extern mach_port_t
_lookupd_port();
60 static int gai_proc
= -1;
61 static int gni_proc
= -1;
63 static int32_t supported_family
[] =
69 static int32_t supported_family_count
= 3;
71 static int32_t supported_socket
[] =
78 static int32_t supported_socket_count
= 4;
80 static int32_t supported_protocol
[] =
87 static int32_t supported_protocol_count
= 4;
89 static 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_ICMPV6
,
95 SOCK_UNSPEC
, IPPROTO_UNSPEC
,
96 SOCK_UNSPEC
, IPPROTO_UDP
,
97 SOCK_UNSPEC
, IPPROTO_TCP
,
98 SOCK_UNSPEC
, IPPROTO_ICMPV6
,
99 SOCK_DGRAM
, IPPROTO_UNSPEC
,
100 SOCK_DGRAM
, IPPROTO_UDP
,
101 SOCK_STREAM
, IPPROTO_UNSPEC
,
102 SOCK_STREAM
, IPPROTO_TCP
104 static int32_t supported_socket_protocol_pair_count
= 12;
107 gai_family_type_check(int32_t f
)
111 for (i
= 0; i
< supported_family_count
; i
++)
113 if (f
== supported_family
[i
]) return 0;
120 gai_socket_type_check(int32_t s
)
124 for (i
= 0; i
< supported_socket_count
; i
++)
126 if (s
== supported_socket
[i
]) return 0;
133 gai_protocol_type_check(int32_t p
)
137 for (i
= 0; i
< supported_protocol_count
; i
++)
139 if (p
== supported_protocol
[i
]) return 0;
146 gai_socket_protocol_type_check(int32_t s
, int32_t p
)
148 int32_t i
, j
, ss
, sp
;
150 for (i
= 0, j
= 0; i
< supported_socket_protocol_pair_count
; i
++, j
+=2)
152 ss
= supported_socket_protocol_pair
[j
];
153 sp
= supported_socket_protocol_pair
[j
+1];
154 if ((s
== ss
) && (p
== sp
)) return 0;
161 gai_strerror(int32_t err
)
165 case EAI_ADDRFAMILY
: return "Address family for nodename not supported";
166 case EAI_AGAIN
: return "Temporary failure in name resolution";
167 case EAI_BADFLAGS
: return "Invalid value for ai_flags";
168 case EAI_FAIL
: return "Non-recoverable failure in name resolution";
169 case EAI_FAMILY
: return "ai_family not supported";
170 case EAI_MEMORY
: return "Memory allocation failure";
171 case EAI_NODATA
: return "No address associated with nodename";
172 case EAI_NONAME
: return "nodename nor servname provided, or not known";
173 case EAI_SERVICE
: return "servname not supported for ai_socktype";
174 case EAI_SOCKTYPE
: return "ai_socktype not supported";
175 case EAI_SYSTEM
: return "System error";
176 case EAI_BADHINTS
: return "Bad hints";
177 case EAI_PROTOCOL
: return "ai_protocol not supported";
180 return "Unknown error";
184 append_addrinfo(struct addrinfo
**l
, struct addrinfo
*a
)
188 if (l
== NULL
) return;
189 if (a
== NULL
) return;
199 if (a
->ai_family
== PF_INET6
)
201 if (x
->ai_family
== PF_INET
)
208 while ((x
->ai_next
!= NULL
) && (x
->ai_next
->ai_family
!= PF_INET
)) x
= x
->ai_next
;
209 a
->ai_next
= x
->ai_next
;
214 while (x
->ai_next
!= NULL
) x
= x
->ai_next
;
221 encode_kv(XDR
*x
, const char *k
, const char *v
)
225 if (!xdr_string(x
, (char **)&k
, _LU_MAXLUSTRLEN
)) return 1;
226 if (!xdr_int(x
, &n
)) return 1;
227 if (!xdr_string(x
, (char **)&v
, _LU_MAXLUSTRLEN
)) return 1;
233 freeaddrinfo(struct addrinfo
*a
)
235 struct addrinfo
*next
;
240 if (a
->ai_addr
!= NULL
) free(a
->ai_addr
);
241 if (a
->ai_canonname
!= NULL
) free(a
->ai_canonname
);
247 static struct addrinfo
*
248 new_addrinfo_v4(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in_addr addr
, uint32_t iface
, char *cname
)
251 struct sockaddr_in
*sa
;
254 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
255 if (a
== NULL
) return NULL
;
260 a
->ai_family
= PF_INET
;
261 a
->ai_socktype
= sock
;
262 a
->ai_protocol
= proto
;
264 a
->ai_addrlen
= sizeof(struct sockaddr_in
);
266 sa
= (struct sockaddr_in
*)calloc(1, a
->ai_addrlen
);
273 sa
->sin_len
= a
->ai_addrlen
;
274 sa
->sin_family
= PF_INET
;
275 sa
->sin_port
= htons(port
);
278 /* Kludge: Jam the interface number into sin_zero. */
279 memmove(sa
->sin_zero
, &iface
, sizeof(uint32_t));
281 a
->ai_addr
= (struct sockaddr
*)sa
;
285 len
= strlen(cname
) + 1;
286 a
->ai_canonname
= malloc(len
);
287 memmove(a
->ai_canonname
, cname
, len
);
293 static struct addrinfo
*
294 new_addrinfo_v6(int32_t flags
, int32_t sock
, int32_t proto
, uint16_t port
, struct in6_addr addr
, uint32_t scopeid
, char *cname
)
297 struct sockaddr_in6
*sa
;
300 a
= (struct addrinfo
*)calloc(1, sizeof(struct addrinfo
));
301 if (a
== NULL
) return NULL
;
306 a
->ai_family
= PF_INET6
;
307 a
->ai_socktype
= sock
;
308 a
->ai_protocol
= proto
;
310 a
->ai_addrlen
= sizeof(struct sockaddr_in6
);
312 sa
= (struct sockaddr_in6
*)calloc(1, a
->ai_addrlen
);
319 sa
->sin6_len
= a
->ai_addrlen
;
320 sa
->sin6_family
= PF_INET6
;
321 sa
->sin6_port
= htons(port
);
322 sa
->sin6_addr
= addr
;
323 sa
->sin6_scope_id
= scopeid
;
324 a
->ai_addr
= (struct sockaddr
*)sa
;
328 len
= strlen(cname
) + 1;
329 a
->ai_canonname
= malloc(len
);
330 memmove(a
->ai_canonname
, cname
, len
);
337 * getaddrinfo support in lookupd
338 * Input dict may contain the following
342 * protocol: [IPPROTO_UNSPEC] | IPPROTO_UDP | IPPROTO_TCP
343 * socktype: [SOCK_UNSPEC] | SOCK_DGRAM | SOCK_STREAM
344 * family: [PF_UNSPEC] | PF_INET | PF_INET6
347 * numerichost: [0] | 1
349 * Output dictionary may contain the following
350 * All values are encoded as strings.
352 * flags: unsigned long
353 * family: unsigned long
354 * socktype: unsigned long
355 * protocol: unsigned long
356 * port: unsigned long
358 * scopeid: unsigned long
363 static struct addrinfo
*
364 gai_lookupd_process_dictionary(XDR
*inxdr
)
366 int32_t i
, nkeys
, nvals
;
368 uint32_t flags
, family
, socktype
, protocol
, longport
, scopeid
;
370 char *addr
, *canonname
;
377 socktype
= SOCK_UNSPEC
;
378 protocol
= IPPROTO_UNSPEC
;
384 if (!xdr_int(inxdr
, &nkeys
)) return NULL
;
386 for (i
= 0; i
< nkeys
; i
++)
391 if (!xdr_string(inxdr
, &key
, LONG_STRING_LENGTH
)) return NULL
;
392 if (!xdr_int(inxdr
, &nvals
))
404 if (!xdr_string(inxdr
, &val
, LONG_STRING_LENGTH
))
410 if (!strcmp(key
, "flags"))
414 else if (!strcmp(key
, "family"))
418 else if (!strcmp(key
, "socktype"))
420 socktype
= atoi(val
);
422 else if (!strcmp(key
, "protocol"))
424 protocol
= atoi(val
);
426 else if (!strcmp(key
, "port"))
428 longport
= atoi(val
);
431 else if (!strcmp(key
, "scopeid"))
435 else if (!strcmp(key
, "address")) addr
= strdup(val
);
436 else if (!strcmp(key
, "canonname")) canonname
= strdup(val
);
441 if (family
== PF_UNSPEC
)
443 if (addr
!= NULL
) free(addr
);
444 if (canonname
!= NULL
) free(canonname
);
449 if (family
== PF_INET
)
451 inet_aton(addr
, &a4
);
452 a
= new_addrinfo_v4(flags
, socktype
, protocol
, port
, a4
, scopeid
, canonname
);
454 else if (family
== PF_INET6
)
456 inet_pton(AF_INET6
, addr
, &a6
);
457 a
= new_addrinfo_v6(flags
, socktype
, protocol
, port
, a6
, scopeid
, canonname
);
460 if (addr
!= NULL
) free(addr
);
461 if (canonname
!= NULL
) free(canonname
);
467 gai_make_query(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, char *buf
, uint32_t *len
)
469 int32_t numerichost
, family
, proto
, socktype
, canonname
, passive
, parallel
;
472 char str
[64], *cname
;
476 proto
= IPPROTO_UNSPEC
;
477 socktype
= SOCK_UNSPEC
;
485 family
= hints
->ai_family
;
486 if (hints
->ai_flags
& AI_NUMERICHOST
) numerichost
= 1;
487 if (hints
->ai_flags
& AI_CANONNAME
) canonname
= 1;
488 if (hints
->ai_flags
& AI_PASSIVE
) passive
= 1;
489 if (hints
->ai_flags
& AI_PARALLEL
) parallel
= 1;
491 proto
= hints
->ai_protocol
;
492 if (hints
->ai_socktype
== SOCK_DGRAM
)
494 socktype
= SOCK_DGRAM
;
497 if (hints
->ai_socktype
== SOCK_STREAM
)
499 socktype
= SOCK_STREAM
;
504 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
506 /* Attribute count */
508 if (nodename
!= NULL
) na
++;
509 if (servname
!= NULL
) na
++;
510 if (proto
!= IPPROTO_UNSPEC
) na
++;
511 if (socktype
!= SOCK_UNSPEC
) na
++;
512 if (family
!= PF_UNSPEC
) na
++;
513 if (canonname
!= 0) na
++;
514 if (passive
!= 0) na
++;
515 if (parallel
!= 0) na
++;
516 if (numerichost
!= 0) na
++;
518 if (!xdr_int(&outxdr
, (int32_t *)&na
))
520 xdr_destroy(&outxdr
);
525 if (nodename
!= NULL
)
527 if (encode_kv(&outxdr
, "name", nodename
) != 0)
529 xdr_destroy(&outxdr
);
535 if (servname
!= NULL
)
537 if (encode_kv(&outxdr
, "service", servname
) != 0)
539 xdr_destroy(&outxdr
);
545 if (proto
!= IPPROTO_UNSPEC
)
547 snprintf(str
, 64, "%u", proto
);
548 if (encode_kv(&outxdr
, "protocol", str
) != 0)
550 xdr_destroy(&outxdr
);
556 if (socktype
!= SOCK_UNSPEC
)
558 snprintf(str
, 64, "%u", socktype
);
559 if (encode_kv(&outxdr
, "socktype", str
) != 0)
561 xdr_destroy(&outxdr
);
567 if (family
!= PF_UNSPEC
)
569 snprintf(str
, 64, "%u", family
);
570 if (encode_kv(&outxdr
, "family", str
) != 0)
572 xdr_destroy(&outxdr
);
580 if (encode_kv(&outxdr
, "canonname", "1") != 0)
582 xdr_destroy(&outxdr
);
590 if (encode_kv(&outxdr
, "passive", "1") != 0)
592 xdr_destroy(&outxdr
);
600 if (encode_kv(&outxdr
, "parallel", "1") != 0)
602 xdr_destroy(&outxdr
);
608 if (numerichost
!= 0)
610 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
612 xdr_destroy(&outxdr
);
618 *len
= xdr_getpos(&outxdr
);
620 xdr_destroy(&outxdr
);
626 is_a_number(const char *s
)
630 if (s
== NULL
) return 0;
633 for (i
= 0; i
< len
; i
++)
635 if (isdigit(s
[i
]) == 0) return 0;
642 gai_files(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
644 int32_t i
, numericserv
, numerichost
, family
, proto
, wantv4
, wantv6
;
648 char *protoname
, *loopv4
, *loopv6
;
654 if (servname
!= NULL
) numericserv
= is_a_number(servname
);
657 if (hints
!= NULL
) family
= hints
->ai_family
;
661 if (nodename
== NULL
)
665 loopv4
= "127.0.0.1";
666 loopv6
= "0:0:0:0:0:0:0:1";
668 if ((hints
!= NULL
) && ((hints
->ai_flags
& AI_PASSIVE
) == 1))
671 loopv6
= "0:0:0:0:0:0:0:0";
674 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
676 inet_pton(AF_INET
, loopv4
, &a4
);
679 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
681 inet_pton(AF_INET6
, loopv6
, &a6
);
686 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
688 numerichost
= inet_pton(AF_INET
, nodename
, &a4
);
689 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET
;
692 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
694 numerichost
= inet_pton(AF_INET6
, nodename
, &a6
);
695 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET6
;
701 if (family
== PF_INET6
) wantv4
= 0;
702 if (family
== PF_INET
) wantv6
= 0;
704 proto
= IPPROTO_UNSPEC
;
709 proto
= hints
->ai_protocol
;
710 if (proto
== IPPROTO_UNSPEC
)
712 if (hints
->ai_socktype
== SOCK_DGRAM
) proto
= IPPROTO_UDP
;
713 else if (hints
->ai_socktype
== SOCK_STREAM
) proto
= IPPROTO_TCP
;
717 if (proto
== IPPROTO_UDP
) protoname
= "udp";
718 else if (proto
== IPPROTO_TCP
) protoname
= "tcp";
723 if (numericserv
!= 0)
725 port
= htons(atoi(servname
));
727 else if (servname
!= NULL
)
729 s
= getservbyname(servname
, protoname
);
730 if (s
!= NULL
) port
= s
->s_port
;
733 /* new_addrinfo_v4 and new_addrinfo_v6 expect port in host byte order */
736 if (numerichost
!= 0)
740 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
742 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
743 append_addrinfo(res
, a
);
746 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
748 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
749 append_addrinfo(res
, a
);
755 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
757 a
= new_addrinfo_v6(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, 0, NULL
);
758 append_addrinfo(res
, a
);
761 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
763 a
= new_addrinfo_v6(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, 0, NULL
);
764 append_addrinfo(res
, a
);
773 h
= gethostbyname(nodename
);
774 if (h
== NULL
) return 0;
776 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
778 memmove((void *)&a4
.s_addr
, h
->h_addr_list
[i
], h
->h_length
);
780 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
782 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
783 append_addrinfo(res
, a
);
786 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
788 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
789 append_addrinfo(res
, a
);
798 gai_lookupd(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
800 uint32_t n
, i
, qlen
, rlen
;
802 char qbuf
[LU_QBUF_SIZE
];
805 mach_port_t server_port
;
806 kern_return_t status
;
809 server_port
= MACH_PORT_NULL
;
810 if (_lu_running()) server_port
= _lookupd_port(0);
811 if (server_port
== MACH_PORT_NULL
)
813 /* lookupd isn't available - fall back to the flat files */
814 return gai_files(nodename
, servname
, hints
, res
);
819 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
820 if (status
!= KERN_SUCCESS
)
822 errno
= ECONNREFUSED
;
829 /* gai_make_query sets errno if it fails */
830 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
831 if (i
!= 0) return EAI_SYSTEM
;
833 qlen
/= BYTES_PER_XDR_UNIT
;
837 status
= _lookup_all(server_port
, gai_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
838 if (status
!= KERN_SUCCESS
) return EAI_NODATA
;
840 rlen
*= BYTES_PER_XDR_UNIT
;
842 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
844 if (!xdr_int(&inxdr
, (int32_t *)&n
))
852 for (i
= 0; i
< n
; i
++)
854 a
= gai_lookupd_process_dictionary(&inxdr
);
855 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
856 append_addrinfo(res
, a
);
860 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
862 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
864 res
[0]->ai_canonname
= strdup(cname
);
871 gai_checkhints(const struct addrinfo
*hints
)
873 if (hints
== NULL
) return 0;
874 if (hints
->ai_addrlen
!= 0) return EAI_BADHINTS
;
875 if (hints
->ai_canonname
!= NULL
) return EAI_BADHINTS
;
876 if (hints
->ai_addr
!= NULL
) return EAI_BADHINTS
;
877 if (hints
->ai_next
!= NULL
) return EAI_BADHINTS
;
879 /* Check for supported protocol family */
880 if (gai_family_type_check(hints
->ai_family
) != 0) return EAI_FAMILY
;
882 /* Check for supported socket */
883 if (gai_socket_type_check(hints
->ai_socktype
) != 0) return EAI_BADHINTS
;
885 /* Check for supported protocol */
886 if (gai_protocol_type_check(hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
888 /* Check that socket type is compatible with protocol */
889 if (gai_socket_protocol_type_check(hints
->ai_socktype
, hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
895 getaddrinfo(const char * __restrict nodename
, const char * __restrict servname
, const struct addrinfo
* __restrict hints
, struct addrinfo
** __restrict res
)
897 int32_t status
, nodenull
, servnull
;
899 if (res
== NULL
) return 0;
904 if ((nodename
== NULL
) || (nodename
[0] == '\0')) nodenull
= 1;
907 if ((servname
== NULL
) || (servname
[0] == '\0')) servnull
= 1;
909 if ((nodenull
== 1) && (servnull
== 1)) return EAI_NONAME
;
910 status
= gai_checkhints(hints
);
911 if (status
!= 0) return status
;
913 if (nodenull
== 1) status
= gai_lookupd(NULL
, servname
, hints
, res
);
914 else if (servnull
== 1) status
= gai_lookupd(nodename
, NULL
, hints
, res
);
915 else status
= gai_lookupd(nodename
, servname
, hints
, res
);
917 if ((status
== 0) && (*res
== NULL
)) status
= EAI_NODATA
;
923 getaddrinfo_async_start(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
, getaddrinfo_async_callback callback
, void *context
)
927 char qbuf
[LU_QBUF_SIZE
];
928 mach_port_t server_port
;
932 if ((nodename
== NULL
) && (servname
== NULL
)) return EAI_NONAME
;
934 status
= gai_checkhints(hints
);
935 if (status
!= 0) return EAI_BADHINTS
;
937 server_port
= MACH_PORT_NULL
;
938 if (_lu_running()) server_port
= _lookupd_port(0);
939 if (server_port
== MACH_PORT_NULL
)
941 errno
= ECONNREFUSED
;
947 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
948 if (status
!= KERN_SUCCESS
)
950 errno
= ECONNREFUSED
;
957 /* gai_make_query sets errno if it fails */
958 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
959 if (i
!= 0) return EAI_SYSTEM
;
961 qlen
/= BYTES_PER_XDR_UNIT
;
963 status
= lu_async_start(p
, gai_proc
, qbuf
, qlen
, (void *)callback
, context
);
966 errno
= ECONNREFUSED
;
974 getaddrinfo_async_send(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
)
976 return getaddrinfo_async_start(p
, nodename
, servname
, hints
, NULL
, NULL
);
980 gai_extract_data(char *buf
, uint32_t len
, struct addrinfo
**res
)
989 if (buf
== NULL
) return EAI_NODATA
;
990 if (len
== 0) return EAI_NODATA
;
992 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
994 if (!xdr_int(&xdr
, (int32_t *)&n
))
1002 for (i
= 0; i
< n
; i
++)
1004 a
= gai_lookupd_process_dictionary(&xdr
);
1005 if (a
== NULL
) break;
1007 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
1008 append_addrinfo(res
, a
);
1013 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
1015 res
[0]->ai_canonname
= strdup(cname
);
1018 if (*res
== NULL
) return EAI_NODATA
;
1023 getaddrinfo_async_receive(mach_port_t p
, struct addrinfo
**res
)
1025 kern_return_t status
;
1029 if (res
== NULL
) return 0;
1035 status
= lu_async_receive(p
, &buf
, &len
);
1036 if (status
< 0) return EAI_FAIL
;
1038 status
= gai_extract_data(buf
, len
, res
);
1039 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1040 if (status
!= 0) return status
;
1042 if (*res
== NULL
) return EAI_NODATA
;
1048 getaddrinfo_async_handle_reply(void *msg
)
1050 getaddrinfo_async_callback callback
;
1055 struct addrinfo
*res
;
1057 callback
= (getaddrinfo_async_callback
)NULL
;
1063 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1064 if (status
!= KERN_SUCCESS
)
1066 if (status
== MIG_REPLY_MISMATCH
) return 0;
1067 if (callback
!= NULL
) callback(EAI_NODATA
, NULL
, context
);
1071 status
= gai_extract_data(buf
, len
, &res
);
1072 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1075 if (callback
!= NULL
) callback(status
, NULL
, context
);
1081 callback(EAI_NODATA
, NULL
, context
);
1085 callback(0, res
, context
);
1094 * getnameinfo support in lookupd
1095 * Input dict may contain the following
1097 * ip_address: node address
1098 * ipv6_address: node address
1099 * port: service number
1100 * protocol: [tcp] | udp
1102 * numerichost: [0] | 1
1103 * name_required: [0] | 1
1104 * numericserv: [0] | 1
1106 * Output dictionary may contain the following
1107 * All values are encoded as strings.
1114 gni_lookupd_process_dictionary(XDR
*inxdr
, char **host
, char **serv
)
1116 int32_t i
, j
, nkeys
, nvals
, status
;
1119 if ((host
== NULL
) || (serv
== NULL
))
1125 if (!xdr_int(inxdr
, &nkeys
))
1134 for (i
= 0; i
< nkeys
; i
++)
1140 status
= _lu_xdr_attribute(inxdr
, &key
, &vals
, (uint32_t *)&nvals
);
1153 if ((*host
== NULL
) && (!strcmp("name", key
)))
1156 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1159 else if ((*serv
== NULL
) && (!strcmp(key
, "service")))
1162 for (j
= 1; j
< nvals
; j
++) free(vals
[j
]);
1165 if (key
!= NULL
) free(key
);
1173 gni_make_query(const struct sockaddr
*sa
, size_t salen
, int wanthost
, int wantserv
, int flags
, char *buf
, uint32_t *len
)
1177 char str
[_LU_MAXLUSTRLEN
], *key
, ifname
[IF_NAMESIZE
];
1178 uint32_t a4
, ifnum
, offset
, na
, proto
, fqdn
, numerichost
, numericserv
, name_req
, isll
, issl
;
1179 struct sockaddr_in6
*s6
;
1181 if (sa
== NULL
) return EAI_FAIL
;
1182 if (sa
->sa_len
!= salen
) return EAI_FAMILY
;
1184 proto
= IPPROTO_TCP
;
1192 offset
= INET_NTOP_AF_INET_OFFSET
;
1196 if (sa
->sa_family
== PF_INET
)
1198 a4
= ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
1199 if (IN_MULTICAST(a4
) || IN_EXPERIMENTAL(a4
)) flags
|= NI_NUMERICHOST
;
1200 a4
>>= IN_CLASSA_NSHIFT
;
1201 if (a4
== 0) flags
|= NI_NUMERICHOST
;
1203 port
= ((struct sockaddr_in
*)sa
)->sin_port
;
1205 else if (sa
->sa_family
== PF_INET6
)
1207 s6
= (struct sockaddr_in6
*)sa
;
1208 switch (s6
->sin6_addr
.s6_addr
[0])
1211 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
))
1214 else if (IN6_IS_ADDR_LOOPBACK(&s6
->sin6_addr
))
1219 flags
|= NI_NUMERICHOST
;
1223 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
1227 else if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
))
1231 else if (IN6_IS_ADDR_MULTICAST(&s6
->sin6_addr
))
1233 flags
|= NI_NUMERICHOST
;
1238 if ((isll
!= 0) || (issl
!= 0))
1240 ifnum
= s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1];
1241 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1242 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1244 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1245 s6
->sin6_scope_id
= ifnum
;
1246 if ((ifnum
!= 0) && (flags
& NI_NUMERICHOST
)) flags
|= NI_WITHSCOPEID
;
1249 offset
= INET_NTOP_AF_INET6_OFFSET
;
1250 key
= "ipv6_address";
1251 port
= s6
->sin6_port
;
1260 if (wanthost
!= 0) na
++;
1261 if (wantserv
!= 0) na
++;
1263 if (flags
& NI_NOFQDN
)
1269 if (flags
& NI_NUMERICHOST
)
1275 if (flags
& NI_NUMERICSERV
)
1281 if (flags
& NI_NAMEREQD
)
1287 if (flags
& NI_DGRAM
)
1289 proto
= IPPROTO_UDP
;
1293 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
1295 if (!xdr_int(&outxdr
, (int32_t *)&na
))
1297 xdr_destroy(&outxdr
);
1304 inet_ntop(sa
->sa_family
, (char *)(sa
) + offset
, str
, _LU_MAXLUSTRLEN
);
1306 if ((flags
& NI_WITHSCOPEID
) && (sa
->sa_family
== AF_INET6
))
1308 ifnum
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
1309 if (if_indextoname(ifnum
, ifname
) != NULL
)
1312 strcat(str
, ifname
);
1316 if (encode_kv(&outxdr
, key
, str
) != 0)
1318 xdr_destroy(&outxdr
);
1326 snprintf(str
, _LU_MAXLUSTRLEN
, "%hu", port
);
1327 if (encode_kv(&outxdr
, "port", str
) != 0)
1329 xdr_destroy(&outxdr
);
1335 if (proto
== IPPROTO_UDP
)
1337 if (encode_kv(&outxdr
, "protocol", "udp") != 0)
1339 xdr_destroy(&outxdr
);
1347 if (encode_kv(&outxdr
, "fqdn", "0") != 0)
1349 xdr_destroy(&outxdr
);
1355 if (numerichost
== 1)
1357 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
1359 xdr_destroy(&outxdr
);
1365 if (numericserv
== 1)
1367 if (encode_kv(&outxdr
, "numericserv", "1") != 0)
1369 xdr_destroy(&outxdr
);
1377 if (encode_kv(&outxdr
, "name_required", "1") != 0)
1379 xdr_destroy(&outxdr
);
1385 *len
= xdr_getpos(&outxdr
);
1387 xdr_destroy(&outxdr
);
1393 getnameinfo(const struct sockaddr
* __restrict sa
, socklen_t salen
, char * __restrict host
, socklen_t hostlen
, char * __restrict serv
, socklen_t servlen
, int flags
)
1395 uint32_t n
, i
, qlen
, rlen
;
1397 int wanth
, wants
, isll
, issl
;
1399 char qbuf
[LU_QBUF_SIZE
], ifname
[IF_NAMESIZE
];
1400 char *rbuf
, *hval
, *sval
;
1401 mach_port_t server_port
;
1402 kern_return_t status
;
1403 struct sockaddr_in
*s4
;
1404 struct sockaddr_in6
*s6
;
1407 if (sa
== NULL
) return EAI_FAIL
;
1413 if (sa
->sa_family
== AF_INET6
)
1415 s6
= (struct sockaddr_in6
*)sa
;
1417 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) isll
= 1;
1418 if (IN6_IS_ADDR_SITELOCAL(&s6
->sin6_addr
)) issl
= 1;
1421 * Link-local and site-local IPv6 addresses may have a scope id
1422 * in s6->sin6_addr.__u6_addr.__u6_addr16[1] as well as in s6->sin6_scope_id.
1423 * If they are both non-zero, they must be equal.
1424 * We zero s6->sin6_addr.__u6_addr.__u6_addr16[1] and set s6->sin6_scope_id.
1426 if ((isll
!= 0) || (issl
!= 0))
1428 ifnum
= ntohs(s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1]);
1429 if (ifnum
== 0) ifnum
= s6
->sin6_scope_id
;
1430 else if ((s6
->sin6_scope_id
!= 0) && (ifnum
!= s6
->sin6_scope_id
)) return EAI_FAIL
;
1432 s6
->sin6_addr
.__u6_addr
.__u6_addr16
[1] = 0;
1433 s6
->sin6_scope_id
= ifnum
;
1436 /* V4 mapped and compat addresses are converted to plain V4 */
1437 if ((IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
)) || (IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
)))
1439 s4
= (struct sockaddr_in
*)calloc(1, sizeof(struct sockaddr_in
));
1440 s4
->sin_len
= sizeof(struct sockaddr_in
);
1441 s4
->sin_family
= AF_INET
;
1442 s4
->sin_port
= s6
->sin6_port
;
1443 memcpy(&(s4
->sin_addr
.s_addr
), &(s6
->sin6_addr
.s6_addr
[12]), 4);
1445 i
= getnameinfo((const struct sockaddr
*)s4
, s4
->sin_len
, host
, hostlen
, serv
, servlen
, flags
);
1452 if ((host
!= NULL
) && (hostlen
!= 0)) wanth
= 1;
1455 if ((serv
!= NULL
) && (servlen
!= 0)) wants
= 1;
1457 if ((wanth
== 0) && (wants
== 0)) return 0;
1460 * Special cases handled by the library
1462 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
))
1464 i
= INET_NTOP_AF_INET_OFFSET
;
1465 if (sa
->sa_family
== PF_INET6
) i
= INET_NTOP_AF_INET6_OFFSET
;
1466 if (inet_ntop(sa
->sa_family
, (char *)(sa
) + i
, host
, hostlen
) == NULL
) return EAI_FAIL
;
1468 if (((isll
!= 0) || (issl
!= 0)) && (ifnum
!= 0))
1470 /* append interface name */
1471 if (if_indextoname(ifnum
, ifname
) != NULL
)
1474 strcat(host
, ifname
);
1478 if (wants
== 0) return 0;
1481 if ((wants
== 1) && (flags
& NI_NUMERICSERV
))
1483 if (sa
->sa_family
== PF_INET
)
1485 s4
= (struct sockaddr_in
*)sa
;
1486 n
= snprintf(serv
, servlen
, "%hu", ntohs(s4
->sin_port
));
1487 if (n
>= servlen
) return EAI_FAIL
;
1489 else if (sa
->sa_family
== PF_INET6
)
1491 s6
= (struct sockaddr_in6
*)sa
;
1492 n
= snprintf(serv
, servlen
, "%hu", ntohs(s6
->sin6_port
));
1493 if (n
>= servlen
) return EAI_FAIL
;
1495 else return EAI_FAMILY
;
1497 if (wanth
== 0) return 0;
1500 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
) && (wants
== 1) && (flags
& NI_NUMERICSERV
)) return 0;
1505 server_port
= MACH_PORT_NULL
;
1506 if (_lu_running()) server_port
= _lookupd_port(0);
1507 if (server_port
== MACH_PORT_NULL
)
1509 errno
= ECONNREFUSED
;
1515 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1516 if (status
!= KERN_SUCCESS
)
1518 errno
= ECONNREFUSED
;
1523 qlen
= LU_QBUF_SIZE
;
1524 i
= gni_make_query(sa
, salen
, wanth
, wants
, flags
, qbuf
, &qlen
);
1525 if (i
!= 0) return i
;
1527 qlen
/= BYTES_PER_XDR_UNIT
;
1531 status
= _lookup_all(server_port
, gni_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
1532 if (status
!= KERN_SUCCESS
) return EAI_NONAME
;
1534 rlen
*= BYTES_PER_XDR_UNIT
;
1536 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
1538 if (!xdr_int(&inxdr
, (int32_t *)&n
))
1540 xdr_destroy(&inxdr
);
1547 xdr_destroy(&inxdr
);
1554 i
= gni_lookupd_process_dictionary(&inxdr
, &hval
, &sval
);
1556 xdr_destroy(&inxdr
);
1557 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
1559 if (i
!= 0) return i
;
1562 if (hval
!= NULL
) i
= strlen(hval
) + 1;
1563 if ((host
!= NULL
) && (hostlen
!= 0) && (i
!= 0))
1565 if (i
> hostlen
) return EAI_FAIL
;
1566 memcpy(host
, hval
, i
);
1571 if (sval
!= NULL
) i
= strlen(sval
) + 1;
1572 if ((serv
!= NULL
) && (servlen
!= 0) && (i
!= 0))
1574 if (i
> servlen
) return EAI_FAIL
;
1575 memcpy(serv
, sval
, i
);
1583 getnameinfo_async_start(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
, getnameinfo_async_callback callback
, void *context
)
1586 char qbuf
[LU_QBUF_SIZE
];
1587 mach_port_t server_port
;
1588 kern_return_t status
;
1591 if (sa
== NULL
) return EAI_FAIL
;
1593 server_port
= MACH_PORT_NULL
;
1594 if (_lu_running()) server_port
= _lookupd_port(0);
1595 if (server_port
== MACH_PORT_NULL
)
1597 errno
= ECONNREFUSED
;
1603 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1604 if (status
!= KERN_SUCCESS
)
1606 errno
= ECONNREFUSED
;
1611 qlen
= LU_QBUF_SIZE
;
1612 i
= gni_make_query(sa
, salen
, 1, 1, flags
, qbuf
, &qlen
);
1613 if (i
!= 0) return i
;
1615 qlen
/= BYTES_PER_XDR_UNIT
;
1617 status
= lu_async_start(p
, gni_proc
, qbuf
, qlen
, (void *)callback
, context
);
1620 errno
= ECONNREFUSED
;
1628 getnameinfo_async_send(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
)
1630 return getnameinfo_async_start(p
, sa
, salen
, flags
, NULL
, NULL
);
1634 gni_extract_data(char *buf
, uint32_t len
, char **host
, char **serv
)
1643 if (buf
== NULL
) return EAI_NODATA
;
1644 if (len
== 0) return EAI_NODATA
;
1646 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
1648 if (!xdr_int(&xdr
, (int32_t *)&n
))
1661 i
= gni_lookupd_process_dictionary(&xdr
, host
, serv
);
1667 getnameinfo_async_receive(mach_port_t p
, char **host
, char **serv
)
1669 kern_return_t status
;
1676 status
= lu_async_receive(p
, &buf
, &len
);
1677 if (status
< 0) return EAI_FAIL
;
1679 status
= gni_extract_data(buf
, len
, host
, serv
);
1680 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1681 if (status
!= 0) return status
;
1687 getnameinfo_async_handle_reply(void *msg
)
1689 getnameinfo_async_callback callback
;
1691 char *buf
, *hval
, *sval
;
1695 callback
= (getnameinfo_async_callback
)NULL
;
1700 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1701 if (status
!= KERN_SUCCESS
)
1703 if (status
== MIG_REPLY_MISMATCH
) return 0;
1704 if (callback
!= NULL
) callback(EAI_NONAME
, NULL
, NULL
, context
);
1711 status
= gni_extract_data(buf
, len
, &hval
, &sval
);
1712 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1715 if (callback
!= NULL
) callback(status
, NULL
, NULL
, context
);
1719 callback(0, hval
, sval
, context
);