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
;
471 char str
[64], *cname
;
475 proto
= IPPROTO_UNSPEC
;
476 socktype
= SOCK_UNSPEC
;
483 family
= hints
->ai_family
;
484 if (hints
->ai_flags
& AI_NUMERICHOST
) numerichost
= 1;
485 if (hints
->ai_flags
& AI_CANONNAME
) canonname
= 1;
486 if ((hints
->ai_flags
& AI_PASSIVE
) == 1) passive
= 1;
488 proto
= hints
->ai_protocol
;
489 if (hints
->ai_socktype
== SOCK_DGRAM
)
491 socktype
= SOCK_DGRAM
;
494 if (hints
->ai_socktype
== SOCK_STREAM
)
496 socktype
= SOCK_STREAM
;
501 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
503 /* Attribute count */
505 if (nodename
!= NULL
) na
++;
506 if (servname
!= NULL
) na
++;
507 if (proto
!= IPPROTO_UNSPEC
) na
++;
508 if (socktype
!= SOCK_UNSPEC
) na
++;
509 if (family
!= PF_UNSPEC
) na
++;
510 if (canonname
!= 0) na
++;
511 if (passive
!= 0) na
++;
512 if (numerichost
!= 0) na
++;
514 if (!xdr_int(&outxdr
, &na
))
516 xdr_destroy(&outxdr
);
520 if (nodename
!= NULL
)
522 if (encode_kv(&outxdr
, "name", nodename
) != 0)
524 xdr_destroy(&outxdr
);
529 if (servname
!= NULL
)
531 if (encode_kv(&outxdr
, "service", servname
) != 0)
533 xdr_destroy(&outxdr
);
538 if (proto
!= IPPROTO_UNSPEC
)
540 snprintf(str
, 64, "%u", proto
);
541 if (encode_kv(&outxdr
, "protocol", str
) != 0)
543 xdr_destroy(&outxdr
);
548 if (socktype
!= SOCK_UNSPEC
)
550 snprintf(str
, 64, "%u", socktype
);
551 if (encode_kv(&outxdr
, "socktype", str
) != 0)
553 xdr_destroy(&outxdr
);
558 if (family
!= PF_UNSPEC
)
560 snprintf(str
, 64, "%u", family
);
561 if (encode_kv(&outxdr
, "family", str
) != 0)
563 xdr_destroy(&outxdr
);
570 if (encode_kv(&outxdr
, "canonname", "1") != 0)
572 xdr_destroy(&outxdr
);
579 if (encode_kv(&outxdr
, "passive", "1") != 0)
581 xdr_destroy(&outxdr
);
586 if (numerichost
!= 0)
588 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
590 xdr_destroy(&outxdr
);
595 *len
= xdr_getpos(&outxdr
);
597 xdr_destroy(&outxdr
);
603 is_a_number(const char *s
)
607 if (s
== NULL
) return 0;
610 for (i
= 0; i
< len
; i
++)
612 if (isdigit(s
[i
]) == 0) return 0;
619 gai_files(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
621 int32_t i
, numericserv
, numerichost
, family
, proto
, wantv4
, wantv6
;
625 char *protoname
, *loopv4
, *loopv6
;
631 if (servname
!= NULL
) numericserv
= is_a_number(servname
);
634 if (hints
!= NULL
) family
= hints
->ai_family
;
638 if (nodename
== NULL
)
642 loopv4
= "127.0.0.1";
643 loopv6
= "0:0:0:0:0:0:0:1";
645 if ((hints
!= NULL
) && ((hints
->ai_flags
& AI_PASSIVE
) == 1))
648 loopv6
= "0:0:0:0:0:0:0:0";
651 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
653 inet_pton(AF_INET
, loopv4
, &a4
);
656 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
658 inet_pton(AF_INET6
, loopv6
, &a6
);
663 if ((family
== PF_UNSPEC
) || (family
== PF_INET
))
665 numerichost
= inet_pton(AF_INET
, nodename
, &a4
);
666 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET
;
669 if ((family
== PF_UNSPEC
) || (family
== PF_INET6
))
671 numerichost
= inet_pton(AF_INET6
, nodename
, &a6
);
672 if ((numerichost
== 1) && (family
== PF_UNSPEC
)) family
= PF_INET6
;
678 if (family
== PF_INET6
) wantv4
= 0;
679 if (family
== PF_INET
) wantv6
= 0;
681 proto
= IPPROTO_UNSPEC
;
686 proto
= hints
->ai_protocol
;
687 if (proto
== IPPROTO_UNSPEC
)
689 if (hints
->ai_socktype
== SOCK_DGRAM
) proto
= IPPROTO_UDP
;
690 else if (hints
->ai_socktype
== SOCK_STREAM
) proto
= IPPROTO_TCP
;
694 if (proto
== IPPROTO_UDP
) protoname
= "udp";
695 else if (proto
== IPPROTO_TCP
) protoname
= "tcp";
700 if (numericserv
!= 0)
702 port
= htons(atoi(servname
));
704 else if (servname
!= NULL
)
706 s
= getservbyname(servname
, protoname
);
707 if (s
!= NULL
) port
= s
->s_port
;
710 /* new_addrinfo_v4 and new_addrinfo_v6 expect port in host byte order */
713 if (numerichost
!= 0)
717 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
719 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
720 append_addrinfo(res
, a
);
723 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
725 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
726 append_addrinfo(res
, a
);
732 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
734 a
= new_addrinfo_v6(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a6
, 0, NULL
);
735 append_addrinfo(res
, a
);
738 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
740 a
= new_addrinfo_v6(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a6
, 0, NULL
);
741 append_addrinfo(res
, a
);
750 h
= gethostbyname(nodename
);
751 if (h
== NULL
) return 0;
753 for (i
= 0; h
->h_addr_list
[i
] != 0; i
++)
755 memmove((void *)&a4
.s_addr
, h
->h_addr_list
[i
], h
->h_length
);
757 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_UDP
))
759 a
= new_addrinfo_v4(0, SOCK_DGRAM
, IPPROTO_UDP
, port
, a4
, 0, NULL
);
760 append_addrinfo(res
, a
);
763 if ((proto
== IPPROTO_UNSPEC
) || (proto
== IPPROTO_TCP
))
765 a
= new_addrinfo_v4(0, SOCK_STREAM
, IPPROTO_TCP
, port
, a4
, 0, NULL
);
766 append_addrinfo(res
, a
);
775 gai_lookupd(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
777 uint32_t n
, i
, qlen
, rlen
;
779 char qbuf
[LU_QBUF_SIZE
];
782 mach_port_t server_port
;
783 kern_return_t status
;
786 server_port
= MACH_PORT_NULL
;
787 if (_lu_running()) server_port
= _lookupd_port(0);
788 if (server_port
== MACH_PORT_NULL
)
790 /* lookupd isn't available - fall back to the flat files */
791 return gai_files(nodename
, servname
, hints
, res
);
796 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
797 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
801 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
802 if (i
!= 0) return EAI_SYSTEM
;
804 qlen
/= BYTES_PER_XDR_UNIT
;
808 status
= _lookup_all(server_port
, gai_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
809 if (status
!= KERN_SUCCESS
) return EAI_NODATA
;
811 rlen
*= BYTES_PER_XDR_UNIT
;
813 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
815 if (!xdr_int(&inxdr
, &n
))
822 for (i
= 0; i
< n
; i
++)
824 a
= gai_lookupd_process_dictionary(&inxdr
);
825 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
826 append_addrinfo(res
, a
);
830 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
832 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
834 res
[0]->ai_canonname
= strdup(cname
);
841 gai_checkhints(const struct addrinfo
*hints
)
843 if (hints
== NULL
) return 0;
844 if (hints
->ai_addrlen
!= 0) return EAI_BADHINTS
;
845 if (hints
->ai_canonname
!= NULL
) return EAI_BADHINTS
;
846 if (hints
->ai_addr
!= NULL
) return EAI_BADHINTS
;
847 if (hints
->ai_next
!= NULL
) return EAI_BADHINTS
;
849 /* Check for supported protocol family */
850 if (gai_family_type_check(hints
->ai_family
) != 0) return EAI_FAMILY
;
852 /* Check for supported socket */
853 if (gai_socket_type_check(hints
->ai_socktype
) != 0) return EAI_BADHINTS
;
855 /* Check for supported protocol */
856 if (gai_protocol_type_check(hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
858 /* Check that socket type is compatible with protocol */
859 if (gai_socket_protocol_type_check(hints
->ai_socktype
, hints
->ai_protocol
) != 0) return EAI_BADHINTS
;
865 getaddrinfo(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
867 int32_t status
, nodenull
, servnull
;
869 if (res
== NULL
) return 0;
874 if ((nodename
== NULL
) || (nodename
[0] == '\0')) nodenull
= 1;
877 if ((servname
== NULL
) || (servname
[0] == '\0')) servnull
= 1;
879 if ((nodenull
== 1) && (servnull
== 1)) return EAI_NONAME
;
880 status
= gai_checkhints(hints
);
881 if (status
!= 0) return status
;
883 if (nodenull
== 1) status
= gai_lookupd(NULL
, servname
, hints
, res
);
884 else if (servnull
== 1) status
= gai_lookupd(nodename
, NULL
, hints
, res
);
885 else status
= gai_lookupd(nodename
, servname
, hints
, res
);
887 if ((status
== 0) && (*res
== NULL
)) status
= EAI_NODATA
;
893 getaddrinfo_async_start(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
, getaddrinfo_async_callback callback
, void *context
)
897 char qbuf
[LU_QBUF_SIZE
];
898 mach_port_t server_port
;
902 if ((nodename
== NULL
) && (servname
== NULL
)) return EAI_NONAME
;
904 status
= gai_checkhints(hints
);
905 if (status
!= 0) return EAI_BADHINTS
;
907 server_port
= MACH_PORT_NULL
;
908 if (_lu_running()) server_port
= _lookupd_port(0);
909 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
913 status
= _lookup_link(server_port
, "getaddrinfo", &gai_proc
);
914 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
918 i
= gai_make_query(nodename
, servname
, hints
, qbuf
, &qlen
);
919 if (i
!= 0) return EAI_SYSTEM
;
921 qlen
/= BYTES_PER_XDR_UNIT
;
923 return lu_async_start(p
, gai_proc
, qbuf
, qlen
, (void *)callback
, context
);
927 getaddrinfo_async_send(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
)
929 return getaddrinfo_async_start(p
, nodename
, servname
, hints
, NULL
, NULL
);
933 gai_extract_data(char *buf
, uint32_t len
, struct addrinfo
**res
)
942 if (buf
== NULL
) return EAI_NODATA
;
943 if (len
== 0) return EAI_NODATA
;
945 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
947 if (!xdr_int(&xdr
, &n
))
954 for (i
= 0; i
< n
; i
++)
956 a
= gai_lookupd_process_dictionary(&xdr
);
957 if (a
== NULL
) break;
959 if ((cname
== NULL
) && (a
->ai_canonname
!= NULL
)) cname
= a
->ai_canonname
;
960 append_addrinfo(res
, a
);
965 if ((cname
!= NULL
) && (res
!= NULL
) && (res
[0] != NULL
) && (res
[0]->ai_canonname
== NULL
))
967 res
[0]->ai_canonname
= strdup(cname
);
970 if (*res
== NULL
) return EAI_NODATA
;
975 getaddrinfo_async_receive(mach_port_t p
, struct addrinfo
**res
)
977 kern_return_t status
;
981 if (res
== NULL
) return 0;
987 status
= lu_async_receive(p
, &buf
, &len
);
988 if (status
< 0) return EAI_FAIL
;
990 status
= gai_extract_data(buf
, len
, res
);
991 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
992 if (status
!= 0) return status
;
994 if (*res
== NULL
) return EAI_NODATA
;
1000 getaddrinfo_async_handle_reply(void *msg
)
1002 getaddrinfo_async_callback callback
;
1007 struct addrinfo
*res
;
1009 callback
= (getaddrinfo_async_callback
)NULL
;
1015 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1016 if (status
!= KERN_SUCCESS
)
1018 if (status
== MIG_REPLY_MISMATCH
) return 0;
1019 if (callback
!= NULL
) callback(EAI_SYSTEM
, NULL
, context
);
1023 status
= gai_extract_data(buf
, len
, &res
);
1024 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1027 if (callback
!= NULL
) callback(status
, NULL
, context
);
1033 callback(EAI_NODATA
, NULL
, context
);
1037 callback(0, res
, context
);
1046 * getnameinfo support in lookupd
1047 * Input dict may contain the following
1049 * ip_address: node address
1050 * ipv6_address: node address
1051 * port: service number
1052 * protocol: [tcp] | udp
1054 * numerichost: [0] | 1
1055 * name_required: [0] | 1
1056 * numericserv: [0] | 1
1058 * Output dictionary may contain the following
1059 * All values are encoded as strings.
1066 gni_lookupd_process_dictionary(XDR
*inxdr
, char **host
, char **serv
)
1068 int32_t i
, nkeys
, nvals
;
1071 if ((host
== NULL
) || (serv
== NULL
)) return EAI_SYSTEM
;
1072 if (!xdr_int(inxdr
, &nkeys
)) return EAI_SYSTEM
;
1074 for (i
= 0; i
< nkeys
; i
++)
1079 if (!xdr_string(inxdr
, &key
, LONG_STRING_LENGTH
)) return EAI_SYSTEM
;
1080 if (!xdr_int(inxdr
, &nvals
))
1092 if (!xdr_string(inxdr
, &val
, LONG_STRING_LENGTH
))
1098 if (!strcmp(key
, "name"))
1104 else if (!strcmp(key
, "service"))
1110 if (key
!= NULL
) free(key
);
1111 if (val
!= NULL
) free(val
);
1118 gni_make_query(const struct sockaddr
*sa
, size_t salen
, int wanthost
, int wantserv
, int flags
, char *buf
, uint32_t *len
)
1122 char str
[_LU_MAXLUSTRLEN
], *key
, ifname
[IF_NAMESIZE
];
1123 uint32_t a4
, ifnum
, offset
, na
, proto
, fqdn
, numerichost
, numericserv
, name_req
;
1124 struct sockaddr_in6
*s6
;
1126 if (sa
== NULL
) return EAI_FAIL
;
1127 if (sa
->sa_len
!= salen
) return EAI_FAMILY
;
1129 proto
= IPPROTO_TCP
;
1135 offset
= INET_NTOP_AF_INET_OFFSET
;
1139 if (sa
->sa_family
== PF_INET
)
1141 a4
= ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
1142 if (IN_MULTICAST(a4
) || IN_EXPERIMENTAL(a4
)) flags
|= NI_NUMERICHOST
;
1143 a4
>>= IN_CLASSA_NSHIFT
;
1144 if (a4
== 0) flags
|= NI_NUMERICHOST
;
1146 port
= ((struct sockaddr_in
*)sa
)->sin_port
;
1148 else if (sa
->sa_family
== PF_INET6
)
1150 s6
= (struct sockaddr_in6
*)sa
;
1151 switch (s6
->sin6_addr
.s6_addr
[0])
1154 if (IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
))
1157 else if (IN6_IS_ADDR_LOOPBACK(&s6
->sin6_addr
))
1162 flags
|= NI_NUMERICHOST
;
1166 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
))
1168 flags
|= NI_NUMERICHOST
;
1170 else if (IN6_IS_ADDR_MULTICAST(&s6
->sin6_addr
))
1172 flags
|= NI_NUMERICHOST
;
1177 offset
= INET_NTOP_AF_INET6_OFFSET
;
1178 key
= "ipv6_address";
1179 port
= s6
->sin6_port
;
1188 if (wanthost
!= 0) na
++;
1189 if (wantserv
!= 0) na
++;
1191 if (flags
& NI_NOFQDN
)
1197 if (flags
& NI_NUMERICHOST
)
1203 if (flags
& NI_NUMERICSERV
)
1209 if (flags
& NI_NAMEREQD
)
1215 if (flags
& NI_DGRAM
)
1217 proto
= IPPROTO_UDP
;
1221 xdrmem_create(&outxdr
, buf
, *len
, XDR_ENCODE
);
1223 if (!xdr_int(&outxdr
, &na
))
1225 xdr_destroy(&outxdr
);
1231 inet_ntop(sa
->sa_family
, (char *)(sa
) + offset
, str
, _LU_MAXLUSTRLEN
);
1233 if ((flags
& NI_WITHSCOPEID
) && (sa
->sa_family
== AF_INET6
))
1235 ifnum
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
1236 if (if_indextoname(ifnum
, ifname
) != NULL
)
1239 strcat(str
, ifname
);
1243 if (encode_kv(&outxdr
, key
, str
) != 0)
1245 xdr_destroy(&outxdr
);
1252 snprintf(str
, _LU_MAXLUSTRLEN
, "%hu", port
);
1253 if (encode_kv(&outxdr
, "port", str
) != 0)
1255 xdr_destroy(&outxdr
);
1260 if (proto
== IPPROTO_UDP
)
1262 if (encode_kv(&outxdr
, "protocol", "udp") != 0)
1264 xdr_destroy(&outxdr
);
1271 if (encode_kv(&outxdr
, "fqdn", "0") != 0)
1273 xdr_destroy(&outxdr
);
1278 if (numerichost
== 1)
1280 if (encode_kv(&outxdr
, "numerichost", "1") != 0)
1282 xdr_destroy(&outxdr
);
1287 if (numericserv
== 1)
1289 if (encode_kv(&outxdr
, "numericserv", "1") != 0)
1291 xdr_destroy(&outxdr
);
1298 if (encode_kv(&outxdr
, "name_required", "1") != 0)
1300 xdr_destroy(&outxdr
);
1305 *len
= xdr_getpos(&outxdr
);
1307 xdr_destroy(&outxdr
);
1313 getnameinfo(const struct sockaddr
*sa
, socklen_t salen
, char *host
, size_t hostlen
, char *serv
, size_t servlen
, int flags
)
1315 uint32_t n
, i
, qlen
, rlen
;
1318 char qbuf
[LU_QBUF_SIZE
];
1319 char *rbuf
, *hval
, *sval
;
1320 mach_port_t server_port
;
1321 kern_return_t status
;
1322 struct sockaddr_in
*s4
;
1323 struct sockaddr_in6
*s6
;
1326 if (sa
== NULL
) return EAI_FAIL
;
1328 /* V4 mapped and compat addresses are converted to plain V4 */
1329 if (sa
->sa_family
== AF_INET6
)
1331 s6
= (struct sockaddr_in6
*)sa
;
1332 if ((IN6_IS_ADDR_V4MAPPED(&s6
->sin6_addr
)) || (IN6_IS_ADDR_V4COMPAT(&s6
->sin6_addr
)))
1334 s4
= (struct sockaddr_in
*)calloc(1, sizeof(struct sockaddr_in
));
1335 s4
->sin_len
= sizeof(struct sockaddr_in
);
1336 s4
->sin_family
= AF_INET
;
1337 s4
->sin_port
= s6
->sin6_port
;
1338 memcpy(&(s4
->sin_addr
.s_addr
), &(s6
->sin6_addr
.s6_addr
[12]), 4);
1340 i
= getnameinfo((const struct sockaddr
*)s4
, s4
->sin_len
, host
, hostlen
, serv
, servlen
, flags
);
1347 if ((host
!= NULL
) && (hostlen
!= 0)) wanth
= 1;
1350 if ((serv
!= NULL
) && (servlen
!= 0)) wants
= 1;
1352 if ((wanth
== 0) && (wants
== 0)) return 0;
1355 * Special cases handled by the library
1357 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
))
1359 i
= INET_NTOP_AF_INET_OFFSET
;
1360 if (sa
->sa_family
== PF_INET6
) i
= INET_NTOP_AF_INET6_OFFSET
;
1361 if (inet_ntop(sa
->sa_family
, (char *)(sa
) + i
, host
, hostlen
) == NULL
) return EAI_FAIL
;
1363 if (wants
== 0) return 0;
1366 if ((wants
== 1) && (flags
& NI_NUMERICSERV
))
1368 if (sa
->sa_family
== PF_INET
)
1370 s4
= (struct sockaddr_in
*)sa
;
1371 n
= snprintf(serv
, servlen
, "%hu", ntohs(s4
->sin_port
));
1372 if (n
>= servlen
) return EAI_FAIL
;
1374 else if (sa
->sa_family
== PF_INET6
)
1376 s6
= (struct sockaddr_in6
*)sa
;
1377 n
= snprintf(serv
, servlen
, "%hu", ntohs(s6
->sin6_port
));
1378 if (n
>= servlen
) return EAI_FAIL
;
1380 else return EAI_FAMILY
;
1382 if (wanth
== 0) return 0;
1385 if ((wanth
== 1) && (flags
& NI_NUMERICHOST
) && (wants
== 1) && (flags
& NI_NUMERICSERV
)) return 0;
1390 server_port
= MACH_PORT_NULL
;
1391 if (_lu_running()) server_port
= _lookupd_port(0);
1392 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1396 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1397 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1400 qlen
= LU_QBUF_SIZE
;
1401 i
= gni_make_query(sa
, salen
, wanth
, wants
, flags
, qbuf
, &qlen
);
1402 if (i
!= 0) return EAI_SYSTEM
;
1404 qlen
/= BYTES_PER_XDR_UNIT
;
1408 status
= _lookup_all(server_port
, gni_proc
, (unit
*)qbuf
, qlen
, &rbuf
, &rlen
);
1409 if (status
!= KERN_SUCCESS
) return EAI_NONAME
;
1411 rlen
*= BYTES_PER_XDR_UNIT
;
1413 xdrmem_create(&inxdr
, rbuf
, rlen
, XDR_DECODE
);
1415 if (!xdr_int(&inxdr
, &n
))
1417 xdr_destroy(&inxdr
);
1423 xdr_destroy(&inxdr
);
1430 i
= gni_lookupd_process_dictionary(&inxdr
, &hval
, &sval
);
1432 xdr_destroy(&inxdr
);
1433 if (rbuf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)rbuf
, rlen
);
1435 if (i
!= 0) return i
;
1438 if (hval
!= NULL
) i
= strlen(hval
) + 1;
1439 if ((host
!= NULL
) && (hostlen
!= 0) && (i
!= 0))
1441 if (i
> hostlen
) return EAI_FAIL
;
1442 memcpy(host
, hval
, i
);
1447 if (sval
!= NULL
) i
= strlen(sval
) + 1;
1448 if ((serv
!= NULL
) && (servlen
!= 0) && (i
!= 0))
1450 if (i
> servlen
) return EAI_FAIL
;
1451 memcpy(serv
, sval
, i
);
1459 getnameinfo_async_start(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
, getnameinfo_async_callback callback
, void *context
)
1462 char qbuf
[LU_QBUF_SIZE
];
1463 mach_port_t server_port
;
1464 kern_return_t status
;
1467 if (sa
== NULL
) return EAI_FAIL
;
1469 server_port
= MACH_PORT_NULL
;
1470 if (_lu_running()) server_port
= _lookupd_port(0);
1471 if (server_port
== MACH_PORT_NULL
) return EAI_SYSTEM
;
1475 status
= _lookup_link(server_port
, "getnameinfo", &gni_proc
);
1476 if (status
!= KERN_SUCCESS
) return EAI_SYSTEM
;
1479 qlen
= LU_QBUF_SIZE
;
1480 i
= gni_make_query(sa
, salen
, 1, 1, flags
, qbuf
, &qlen
);
1481 if (i
!= 0) return EAI_SYSTEM
;
1483 qlen
/= BYTES_PER_XDR_UNIT
;
1485 return lu_async_start(p
, gni_proc
, qbuf
, qlen
, (void *)callback
, context
);
1489 getnameinfo_async_send(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
)
1491 return getnameinfo_async_start(p
, sa
, salen
, flags
, NULL
, NULL
);
1495 gni_extract_data(char *buf
, uint32_t len
, char **host
, char **serv
)
1503 if (buf
== NULL
) return EAI_NODATA
;
1504 if (len
== 0) return EAI_NODATA
;
1506 xdrmem_create(&xdr
, buf
, len
, XDR_DECODE
);
1508 if (!xdr_int(&xdr
, &n
))
1520 return gni_lookupd_process_dictionary(&xdr
, host
, serv
);
1524 getnameinfo_async_receive(mach_port_t p
, char **host
, char **serv
)
1526 kern_return_t status
;
1533 status
= lu_async_receive(p
, &buf
, &len
);
1534 if (status
< 0) return EAI_FAIL
;
1536 status
= gni_extract_data(buf
, len
, host
, serv
);
1537 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1538 if (status
!= 0) return status
;
1544 getnameinfo_async_handle_reply(void *msg
)
1546 getnameinfo_async_callback callback
;
1548 char *buf
, *hval
, *sval
;
1552 callback
= (getnameinfo_async_callback
)NULL
;
1557 status
= lu_async_handle_reply(msg
, &buf
, &len
, (void **)&callback
, &context
);
1558 if (status
!= KERN_SUCCESS
)
1560 if (status
== MIG_REPLY_MISMATCH
) return 0;
1561 if (callback
!= NULL
) callback(EAI_SYSTEM
, NULL
, NULL
, context
);
1568 status
= gni_extract_data(buf
, len
, &hval
, &sval
);
1569 if (buf
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)buf
, len
);
1572 if (callback
!= NULL
) callback(status
, NULL
, NULL
, context
);
1576 callback(0, hval
, sval
, context
);