2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
31 #include <printerdb.h>
32 #include <sys/param.h>
33 #include <sys/syscall.h>
35 #include <arpa/inet.h>
36 #include <netinet/if_ether.h>
37 #include "si_module.h"
39 #include <thread_data.h>
40 #include <sys/kauth.h>
41 #include "netdb_async.h"
42 #include <dispatch/dispatch.h>
43 #include <mach-o/dyld_priv.h>
46 #define IPPROTO_UNSPEC 0
47 #define IPV6_ADDR_LEN 16
48 #define IPV4_ADDR_LEN 4
51 extern int __initgroups(u_int gidsetsize
, gid_t
*gidset
, int gmuid
);
53 /* SPI from long ago */
56 extern struct addrinfo
*si_list_to_addrinfo(si_list_t
*list
);
57 extern int getnameinfo_link(const struct sockaddr
*sa
, socklen_t salen
, char *host
, size_t hostlen
, char *serv
, size_t servlen
, int flags
);
58 __private_extern__
void search_set_flags(si_mod_t
*si
, const char *name
, uint32_t flag
);
61 * Impedence matching for async calls.
63 * This layer holds on to the caller's callback and context in this
64 * structure, which gets passed to the si_module async routines along
65 * with a callbac in this layer. When this layer gets a callback,
66 * it can save the item or list in thread-specific memory and then
67 * invoke the caller's callback with the appropriate data type.
81 static si_mod_t
*search
= NULL
;
83 if (search
== NULL
) search
= si_module_with_name("search");
89 si_search_module_set_flags(const char *name
, uint32_t flag
)
91 search_set_flags(si_search(), name
, flag
);
95 si_libinfo_general_callback(si_item_t
*item
, uint32_t status
, void *ctx
)
103 struct grouplist_s
*l
;
112 if (ctx
== NULL
) return;
114 sictx
= (si_context_t
*)ctx
;
116 if ((sictx
->orig_callback
== NULL
) || (status
== SI_STATUS_CALL_CANCELLED
))
119 si_item_release(item
);
123 if (sictx
->key_offset
>= 0)
125 LI_set_thread_item(sictx
->cat
+ sictx
->key_offset
, item
);
129 if (item
!= NULL
) res
.x
= (char*)((uintptr_t)item
+ sizeof(si_item_t
));
135 ((si_user_async_callback
)(sictx
->orig_callback
))(res
.u
, sictx
->orig_context
);
140 ((si_group_async_callback
)(sictx
->orig_callback
))(res
.g
, sictx
->orig_context
);
143 case CATEGORY_GROUPLIST
:
145 ((si_grouplist_async_callback
)(sictx
->orig_callback
))(res
.l
, sictx
->orig_context
);
148 case CATEGORY_HOST_IPV4
:
149 case CATEGORY_HOST_IPV6
:
151 ((si_host_async_callback
)(sictx
->orig_callback
))(res
.h
, sictx
->orig_context
);
154 case CATEGORY_NETWORK
:
156 ((si_network_async_callback
)(sictx
->orig_callback
))(res
.n
, sictx
->orig_context
);
159 case CATEGORY_SERVICE
:
161 ((si_service_async_callback
)(sictx
->orig_callback
))(res
.s
, sictx
->orig_context
);
164 case CATEGORY_PROTOCOL
:
166 ((si_protocol_async_callback
)(sictx
->orig_callback
))(res
.p
, sictx
->orig_context
);
171 ((si_rpc_async_callback
)(sictx
->orig_callback
))(res
.r
, sictx
->orig_context
);
176 ((si_fs_async_callback
)(sictx
->orig_callback
))(res
.f
, sictx
->orig_context
);
187 getpwnam(const char *name
)
192 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
195 item
= si_user_byname(si_search(), name
);
196 LI_set_thread_item(CATEGORY_USER
+ 100, item
);
198 if (item
== NULL
) return NULL
;
199 return (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
203 getpwnam_async_call(const char *name
, si_user_async_callback callback
, void *context
)
208 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
211 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
212 if (sictx
== NULL
) return MACH_PORT_NULL
;
214 sictx
->orig_callback
= callback
;
215 sictx
->orig_context
= context
;
216 sictx
->cat
= CATEGORY_USER
;
217 sictx
->key_offset
= 100;
219 return si_async_call(si_search(), SI_CALL_USER_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
223 getpwnam_async_handle_reply(mach_msg_header_t
*msg
)
226 fprintf(stderr
, "<< %s\n", __func__
);
229 si_async_handle_reply(msg
);
238 fprintf(stderr
, "-> %s %d\n", __func__
, uid
);
241 item
= si_user_byuid(si_search(), uid
);
242 LI_set_thread_item(CATEGORY_USER
+ 200, item
);
244 if (item
== NULL
) return NULL
;
245 return (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
249 getpwuid_async_call(uid_t uid
, si_user_async_callback callback
, void *context
)
254 fprintf(stderr
, ">> %s %d\n", __func__
, uid
);
257 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
258 if (sictx
== NULL
) return MACH_PORT_NULL
;
260 sictx
->orig_callback
= callback
;
261 sictx
->orig_context
= context
;
262 sictx
->cat
= CATEGORY_USER
;
263 sictx
->key_offset
= 200;
265 return si_async_call(si_search(), SI_CALL_USER_BYUID
, NULL
, NULL
, NULL
, (uint32_t)uid
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
269 getpwuid_async_handle_reply(mach_msg_header_t
*msg
)
272 fprintf(stderr
, "<< %s\n", __func__
);
275 si_async_handle_reply(msg
);
279 getpwuuid(uuid_t uuid
)
284 uuid_string_t uuidstr
;
285 uuid_unparse_upper(uuid
, uuidstr
);
286 fprintf(stderr
, "-> %s %s\n", __func__
, uuidstr
);
289 item
= si_user_byuuid(si_search(), uuid
);
290 LI_set_thread_item(CATEGORY_USER
+ 300, item
);
292 if (item
== NULL
) return NULL
;
293 return (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
300 fprintf(stderr
, "-- %s\n", __func__
);
303 LI_set_thread_list(CATEGORY_USER
, NULL
);
313 fprintf(stderr
, "-> %s\n", __func__
);
316 list
= LI_get_thread_list(CATEGORY_USER
);
319 list
= si_user_all(si_search());
320 LI_set_thread_list(CATEGORY_USER
, list
);
323 item
= si_list_next(list
);
324 if (item
== NULL
) return NULL
;
326 return (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
333 fprintf(stderr
, "-- %s\n", __func__
);
336 LI_set_thread_list(CATEGORY_USER
, NULL
);
340 setpassent(int ignored
)
345 fprintf(stderr
, "-> %s\n", __func__
);
348 list
= LI_get_thread_list(CATEGORY_USER
);
351 if (list
== NULL
) return 0;
358 getgrnam(const char *name
)
363 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
366 item
= si_group_byname(si_search(), name
);
367 LI_set_thread_item(CATEGORY_GROUP
+ 100, item
);
369 if (item
== NULL
) return NULL
;
370 return (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
374 getgrnam_async_call(const char *name
, si_group_async_callback callback
, void *context
)
379 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
382 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
383 if (sictx
== NULL
) return MACH_PORT_NULL
;
385 sictx
->orig_callback
= callback
;
386 sictx
->orig_context
= context
;
387 sictx
->cat
= CATEGORY_GROUP
;
388 sictx
->key_offset
= 100;
390 return si_async_call(si_search(), SI_CALL_GROUP_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
394 getgrnam_async_handle_reply(mach_msg_header_t
*msg
)
397 fprintf(stderr
, "<< %s\n", __func__
);
400 si_async_handle_reply(msg
);
409 fprintf(stderr
, "-> %s %d\n", __func__
, gid
);
412 item
= si_group_bygid(si_search(), gid
);
413 LI_set_thread_item(CATEGORY_GROUP
+ 200, item
);
415 if (item
== NULL
) return NULL
;
416 return (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
420 getgrgid_async_call(gid_t gid
, si_group_async_callback callback
, void *context
)
425 fprintf(stderr
, ">> %s %d\n", __func__
, gid
);
428 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
429 if (sictx
== NULL
) return MACH_PORT_NULL
;
431 sictx
->orig_callback
= callback
;
432 sictx
->orig_context
= context
;
433 sictx
->cat
= CATEGORY_GROUP
;
434 sictx
->key_offset
= 200;
436 return si_async_call(si_search(), SI_CALL_GROUP_BYGID
, NULL
, NULL
, NULL
, (uint32_t)gid
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
440 getgruid_async_handle_reply(mach_msg_header_t
*msg
)
443 fprintf(stderr
, "<< %s\n", __func__
);
446 si_async_handle_reply(msg
);
450 getgruuid(uuid_t uuid
)
455 uuid_string_t uuidstr
;
456 uuid_unparse_upper(uuid
, uuidstr
);
457 fprintf(stderr
, "-> %s %s\n", __func__
, uuidstr
);
460 item
= si_group_byuuid(si_search(), uuid
);
461 LI_set_thread_item(CATEGORY_GROUP
+ 300, item
);
463 if (item
== NULL
) return NULL
;
464 return (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
471 fprintf(stderr
, "-- %s\n", __func__
);
474 LI_set_thread_list(CATEGORY_GROUP
, NULL
);
484 fprintf(stderr
, "-> %s\n", __func__
);
487 list
= LI_get_thread_list(CATEGORY_GROUP
);
490 list
= si_group_all(si_search());
491 LI_set_thread_list(CATEGORY_GROUP
, list
);
494 item
= si_list_next(list
);
495 if (item
== NULL
) return NULL
;
497 return (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
504 fprintf(stderr
, "-- %s\n", __func__
);
507 LI_set_thread_list(CATEGORY_GROUP
, NULL
);
511 setgroupent(int ignored
)
516 fprintf(stderr
, "-> %s\n", __func__
);
519 list
= LI_get_thread_list(CATEGORY_GROUP
);
522 if (list
== NULL
) return 0;
528 innetgr(const char *group
, const char *host
, const char *user
, const char *domain
)
532 fprintf(stderr
, "-> %s %s %s %s %s\n", __func__
, group
, host
, user
, domain
);
535 res
= si_in_netgroup(si_search(), group
, host
, user
, domain
);
538 fprintf(stderr
, "<- %s %d\n", __func__
, res
);
544 /* N.B. there is no async innetgr */
547 * setnetgrent is really more like a getXXXbyname routine than a
548 * setXXXent routine, since we are looking up a netgroup by name.
551 setnetgrent(const char *name
)
556 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
559 list
= si_netgroup_byname(si_search(), name
);
560 LI_set_thread_list(CATEGORY_NETGROUP
, list
);
563 /* N.B. there is no async getnetgrent */
566 getnetgrent(char **host
, char **user
, char **domain
)
570 struct netgrent_s
*ng
;
573 fprintf(stderr
, "-> %s\n", __func__
);
576 list
= LI_get_thread_list(CATEGORY_NETGROUP
);
577 item
= si_list_next(list
);
578 if (item
== NULL
) return 0;
580 ng
= (struct netgrent_s
*)((uintptr_t)item
+ sizeof(si_item_t
));
584 *domain
= ng
->ng_domain
;
593 fprintf(stderr
, "-- %s\n", __func__
);
596 LI_set_thread_list(CATEGORY_NETGROUP
, NULL
);
601 _check_groups(const char *function
, int32_t ngroups
)
603 static dispatch_once_t once
;
605 if (ngroups
> 0 && ngroups
< NGROUPS_MAX
) {
609 /* only log once per process */
610 dispatch_once(&once
, ^(void) {
611 const char *proc_name
= getprogname();
612 if (strcmp(proc_name
, "id") != 0 && strcmp(proc_name
, "smbd") != 0 && strcmp(proc_name
, "rpcsvchost") != 0) {
613 aslmsg msg
= asl_new(ASL_TYPE_MSG
);
616 snprintf(buffer
, sizeof(buffer
), "%d", (ngroups
== 0 ? INT_MAX
: ngroups
));
617 asl_set(msg
, "com.apple.message.value", buffer
);
619 asl_set(msg
, "com.apple.message.domain", "com.apple.system.libinfo");
620 asl_set(msg
, "com.apple.message.result", "noop");
621 asl_set(msg
, "com.apple.message.signature", function
);
623 asl_log(NULL
, msg
, ASL_LEVEL_NOTICE
, "%s called triggering group enumeration", function
);
634 getgrouplist_internal(const char *name
, int basegid
, gid_t
*groups
, uint32_t *ngroups
)
636 int i
, j
, x
, g
, add
, max
;
641 * On input, ngroups specifies the size of the groups array.
642 * On output, it is set to the number of groups that are being returned.
643 * Returns -1 if the size is too small to fit all the groups that were found.
647 fprintf(stderr
, "-> %s %s %d\n", __func__
, name
, basegid
);
650 if (name
== NULL
) return 0;
651 if (groups
== NULL
) return 0;
652 if (ngroups
== NULL
) return 0;
656 if (max
<= 0) return 0;
661 item
= si_grouplist(si_search(), name
, max
);
662 LI_set_thread_item(CATEGORY_GROUPLIST
, item
);
663 if (item
== NULL
) return 0;
665 gl
= (si_grouplist_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
669 for (i
= 0; i
< gl
->gl_count
; i
++)
673 for (j
= 0; j
< x
; j
++) {
674 if (groups
[j
] == g
) {
679 if (add
== 0) continue;
681 if (x
>= max
) return -1;
691 getgrouplist(const char *name
, int basegid
, int *groups
, int *ngroups
)
694 _check_groups("getgrouplist", *ngroups
);
697 return getgrouplist_internal(name
, basegid
, (gid_t
*)groups
, (uint32_t *)ngroups
);
701 merge_gid(gid_t
*list
, gid_t g
, int32_t *count
)
707 for (i
= 0; i
< cnt
; i
++) {
708 if (list
[i
] == g
) return;
716 _getgrouplist_2_internal(const char *name
, gid_t basegid
, gid_t
**groups
)
723 item
= si_grouplist(si_search(), name
, INT_MAX
);
724 LI_set_thread_item(CATEGORY_GROUPLIST
, item
);
725 if (item
== NULL
) return -1;
727 gl
= (si_grouplist_t
*) ((uintptr_t) item
+ sizeof(si_item_t
));
730 * we can allocate enough up-front, we'll only use what we need
731 * we add one to the count that was found in case the basegid is not there
733 gids
= calloc(gl
->gl_count
+ 1, sizeof(gid_t
));
736 merge_gid(gids
, basegid
, &count
);
737 if (gl
->gl_gid
!= NULL
) {
738 for (i
= 0; i
< gl
->gl_count
; i
++) {
739 merge_gid(gids
, gl
->gl_gid
[i
], &count
);
749 getgrouplist_2(const char *name
, gid_t basegid
, gid_t
**groups
)
752 * Passes back a gid_t list containing all the users groups (and basegid).
753 * Caller must free the list.
754 * Returns the number of gids in the list or -1 on failure.
758 fprintf(stderr
, "-> %s %s %d\n", __func__
, name
, basegid
);
761 if (name
== NULL
) return 0;
762 if (groups
== NULL
) return 0;
765 _check_groups("getgrouplist_2", INT_MAX
);
768 return _getgrouplist_2_internal(name
, basegid
, groups
);
772 getgroupcount(const char *name
, gid_t basegid
)
778 fprintf(stderr
, "-> %s %s %d\n", __func__
, name
, basegid
);
782 _check_groups("getgroupcount", INT_MAX
);
786 count
= _getgrouplist_2_internal(name
, basegid
, &groups
);
787 if (groups
!= NULL
) free(groups
);
792 /* XXX to do: async getgrouplist_2 */
795 initgroups(const char *name
, int basegid
)
799 gid_t groups
[NGROUPS
];
807 fprintf(stderr
, "-> %s %s %d\n", __func__
, name
, basegid
);
810 /* KAUTH_UID_NONE tells the kernel not to fetch supplementary groups from DirectoryService */
811 uid
= KAUTH_UID_NONE
;
814 /* get the UID for this user */
815 item
= si_user_byname(si_search(), name
);
818 p
= (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
820 si_item_release(item
);
828 * A failure either means that user belongs to more than NGROUPS groups
829 * or no groups at all.
832 (void) getgrouplist_internal(name
, basegid
, groups
, &ngroups
);
834 status
= __initgroups(ngroups
, groups
, uid
);
835 if (status
< 0) return -1;
843 alias_getbyname(const char *name
)
848 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
851 item
= si_alias_byname(si_search(), name
);
852 LI_set_thread_item(CATEGORY_ALIAS
+ 100, item
);
853 if (item
== NULL
) return NULL
;
855 return (struct aliasent
*)((uintptr_t)item
+ sizeof(si_item_t
));
859 alias_getbyname_async_call(const char *name
, si_alias_async_callback callback
, void *context
)
864 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
867 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
868 if (sictx
== NULL
) return MACH_PORT_NULL
;
870 sictx
->orig_callback
= callback
;
871 sictx
->orig_context
= context
;
872 sictx
->cat
= CATEGORY_ALIAS
;
873 sictx
->key_offset
= 100;
875 return si_async_call(si_search(), SI_CALL_ALIAS_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
879 alias_getbyname_async_handle_reply(mach_msg_header_t
*msg
)
882 fprintf(stderr
, "<< %s\n", __func__
);
885 si_async_handle_reply(msg
);
892 fprintf(stderr
, "-> %s\n", __func__
);
895 LI_set_thread_list(CATEGORY_ALIAS
, NULL
);
905 fprintf(stderr
, "-> %s\n", __func__
);
908 list
= LI_get_thread_list(CATEGORY_ALIAS
);
911 list
= si_alias_all(si_search());
912 LI_set_thread_list(CATEGORY_ALIAS
, list
);
915 item
= si_list_next(list
);
916 if (item
== NULL
) return NULL
;
918 return (struct aliasent
*)((uintptr_t)item
+ sizeof(si_item_t
));
925 fprintf(stderr
, "-- %s\n", __func__
);
928 LI_set_thread_list(CATEGORY_ALIAS
, NULL
);
934 freehostent(struct hostent
*h
)
936 if (h
== NULL
) return;
938 si_item_t
*item
= (si_item_t
*)((uintptr_t)h
- sizeof(si_item_t
));
939 si_item_release(item
);
943 gethostbynameerrno(const char *name
, int *err
)
947 struct in_addr addr4
;
950 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
953 memset(&addr4
, 0, sizeof(struct in_addr
));
954 status
= SI_STATUS_NO_ERROR
;
957 if (inet_aton(name
, &addr4
) == 1) item
= si_ipnode_byname(si_search(), name
, AF_INET
, 0, NULL
, &status
);
958 else item
= si_host_byname(si_search(), name
, AF_INET
, NULL
, &status
);
960 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
961 if (err
!= NULL
) *err
= status
;
963 LI_set_thread_item(CATEGORY_HOST
+ 100, item
);
964 if (item
== NULL
) return NULL
;
966 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
970 gethostbyname(const char *name
)
974 struct in_addr addr4
;
977 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
980 memset(&addr4
, 0, sizeof(struct in_addr
));
981 status
= SI_STATUS_NO_ERROR
;
984 if (inet_aton(name
, &addr4
) == 1) item
= si_ipnode_byname(si_search(), name
, AF_INET
, 0, NULL
, &status
);
985 else item
= si_host_byname(si_search(), name
, AF_INET
, NULL
, &status
);
987 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
990 LI_set_thread_item(CATEGORY_HOST
+ 100, item
);
991 if (item
== NULL
) return NULL
;
993 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
997 gethostbyname_async_call(const char *name
, si_host_async_callback callback
, void *context
)
1002 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
1005 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1006 if (sictx
== NULL
) return MACH_PORT_NULL
;
1008 sictx
->orig_callback
= callback
;
1009 sictx
->orig_context
= context
;
1010 sictx
->cat
= CATEGORY_HOST
;
1011 sictx
->key_offset
= 100;
1013 return si_async_call(si_search(), SI_CALL_HOST_BYNAME
, name
, NULL
, NULL
, AF_INET
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1017 gethostbyname_async_start(const char *name
, si_host_async_callback callback
, void *context
)
1019 return gethostbyname_async_call(name
, callback
, context
);
1023 gethostbyname_async_cancel(mach_port_t p
)
1026 fprintf(stderr
, "-- %s\n", __func__
);
1034 gethostbyname_async_handle_reply(void *param
)
1036 mach_msg_header_t
*msg
;
1039 fprintf(stderr
, "<< %s\n", __func__
);
1042 msg
= (mach_msg_header_t
*)param
;
1043 si_async_handle_reply(msg
);
1048 gethostbyname_async_handleReply(void *param
)
1050 mach_msg_header_t
*msg
;
1053 fprintf(stderr
, "<< %s\n", __func__
);
1056 msg
= (mach_msg_header_t
*)param
;
1057 si_async_handle_reply(msg
);
1061 gethostbyname2(const char *name
, int af
)
1065 struct in_addr addr4
;
1066 struct in6_addr addr6
;
1067 si_mod_t
*search
= si_search();
1070 fprintf(stderr
, "-> %s %s %d\n", __func__
, name
, af
);
1073 memset(&addr4
, 0, sizeof(struct in_addr
));
1074 memset(&addr6
, 0, sizeof(struct in6_addr
));
1075 status
= SI_STATUS_NO_ERROR
;
1078 if (((af
== AF_INET
) && (inet_aton(name
, &addr4
) == 1)) || ((af
== AF_INET6
) && (inet_pton(af
, name
, &addr6
) == 1)))
1080 item
= si_ipnode_byname(search
, name
, (uint32_t)af
, 0, NULL
, &status
);
1084 item
= si_host_byname(search
, name
, (uint32_t)af
, NULL
, &status
);
1087 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
1090 LI_set_thread_item(CATEGORY_HOST
+ 100, item
);
1091 if (item
== NULL
) return NULL
;
1093 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1097 gethostbyname2_async_call(const char *name
, int af
, si_group_async_callback callback
, void *context
)
1099 si_context_t
*sictx
;
1102 fprintf(stderr
, ">> %s %s %d\n", __func__
, name
, af
);
1105 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1106 if (sictx
== NULL
) return MACH_PORT_NULL
;
1108 sictx
->orig_callback
= callback
;
1109 sictx
->orig_context
= context
;
1110 sictx
->cat
= CATEGORY_HOST
;
1111 sictx
->key_offset
= 100;
1113 return si_async_call(si_search(), SI_CALL_HOST_BYNAME
, name
, NULL
, NULL
, (uint32_t)af
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1117 gethostbyname2_async_cancel(mach_port_t p
)
1120 fprintf(stderr
, "-- %s\n", __func__
);
1127 gethostbyname2_async_handle_reply(mach_msg_header_t
*msg
)
1130 fprintf(stderr
, "<< %s\n", __func__
);
1133 si_async_handle_reply(msg
);
1137 gethostbyaddr(const void *addr
, socklen_t len
, int type
)
1143 fprintf(stderr
, "-> %s %s\n", __func__
, (type
== AF_INET
) ? inet_ntoa(*(struct in_addr
*)addr
) : "-IPv6-");
1146 status
= SI_STATUS_NO_ERROR
;
1148 item
= si_host_byaddr(si_search(), addr
, (uint32_t)type
, NULL
, &status
);
1149 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
1152 LI_set_thread_item(CATEGORY_HOST
+ 200, item
);
1153 if (item
== NULL
) return NULL
;
1155 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1159 gethostbyaddr_async_call(const void *addr
, socklen_t len
, int type
, si_host_async_callback callback
, void *context
)
1161 si_context_t
*sictx
;
1165 fprintf(stderr
, ">> %s %s\n", __func__
, (type
== AF_INET
) ? inet_ntoa(*(struct in_addr
*)addr
) : "-IPv6-");
1168 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1169 if (sictx
== NULL
) return MACH_PORT_NULL
;
1171 sictx
->orig_callback
= callback
;
1172 sictx
->orig_context
= context
;
1173 sictx
->cat
= CATEGORY_HOST
;
1174 sictx
->key_offset
= 200;
1176 /* addr is not a C string - pass length in num3 */
1178 return si_async_call(si_search(), SI_CALL_HOST_BYADDR
, addr
, NULL
, NULL
, (uint32_t)type
, 0, addrlen
, 0, (void *)si_libinfo_general_callback
, sictx
);
1182 gethostbyaddr_async_start(const char *addr
, int len
, int family
, si_host_async_callback callback
, void *context
)
1184 socklen_t slen
= len
;
1186 return gethostbyaddr_async_call(addr
, slen
, family
, callback
, context
);
1190 gethostbyaddr_async_cancel(mach_port_t p
)
1193 fprintf(stderr
, "-- %s\n", __func__
);
1201 gethostbyaddr_async_handle_reply(void *param
)
1204 mach_msg_header_t
*msg
;
1207 fprintf(stderr
, "<< %s\n", __func__
);
1210 msg
= (mach_msg_header_t
*)param
;
1211 si_async_handle_reply(msg
);
1216 gethostbyaddr_async_handleReply(void *param
)
1218 mach_msg_header_t
*msg
;
1221 fprintf(stderr
, "<< %s\n", __func__
);
1224 msg
= (mach_msg_header_t
*)param
;
1225 si_async_handle_reply(msg
);
1229 getipnodebyname(const char *name
, int family
, int flags
, int *err
)
1235 fprintf(stderr
, "-> %s %s %d 0x%08x\n", __func__
, name
, family
, flags
);
1238 status
= SI_STATUS_NO_ERROR
;
1240 item
= si_ipnode_byname(si_search(), name
, family
, flags
, NULL
, &status
);
1241 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
1242 if (err
!= NULL
) *err
= status
;
1244 if (item
== NULL
) return NULL
;
1246 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1251 getipnodebyname_async_call(const char *name
, int family
, int flags
, int *err
, si_host_async_callback callback
, void *context
)
1253 si_context_t
*sictx
;
1256 fprintf(stderr
, ">> %s %s %d 0x%08x\n", __func__
, name
, family
, flags
);
1259 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1260 if (sictx
== NULL
) return MACH_PORT_NULL
;
1262 sictx
->orig_callback
= callback
;
1263 sictx
->orig_context
= context
;
1264 sictx
->cat
= CATEGORY_HOST
;
1265 sictx
->key_offset
= -1;
1267 return si_async_call(si_search(), SI_CALL_IPNODE_BYNAME
, name
, NULL
, NULL
, (uint32_t)family
, (uint32_t)flags
, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1271 getipnodebyname_async_start(const char *name
, int family
, int flags
, int *err
, si_host_async_callback callback
, void *context
)
1273 return getipnodebyname_async_call(name
, family
, flags
, err
, callback
, context
);
1277 getipnodebyname_async_cancel(mach_port_t p
)
1280 fprintf(stderr
, "-- %s\n", __func__
);
1287 getipnodebyname_async_handle_reply(mach_msg_header_t
*msg
)
1290 fprintf(stderr
, "<< %s\n", __func__
);
1293 si_async_handle_reply(msg
);
1297 getipnodebyname_async_handleReply(mach_msg_header_t
*msg
)
1300 fprintf(stderr
, "<< %s\n", __func__
);
1303 si_async_handle_reply(msg
);
1308 is_a4_mapped(const char *s
)
1313 if (s
== NULL
) return 0;
1315 for (i
= 0; i
< 10; i
++)
1318 if (c
!= 0x0) return 0;
1321 for (i
= 10; i
< 12; i
++)
1324 if (c
!= 0xff) return 0;
1331 is_a4_compat(const char *s
)
1336 if (s
== NULL
) return 0;
1338 for (i
= 0; i
< 12; i
++)
1341 if (c
!= 0x0) return 0;
1344 /* Check for :: and ::1 */
1345 for (i
= 13; i
< 15; i
++)
1347 /* anything non-zero in these 3 bytes means it's a V4 address */
1349 if (c
!= 0x0) return 1;
1352 /* Leading 15 bytes are all zero */
1354 if (c
== 0x0) return 0;
1355 if (c
== 0x1) return 0;
1361 getipnodebyaddr(const void *src
, size_t len
, int family
, int *err
)
1366 fprintf(stderr
, "-> %s %s\n", __func__
, (family
== AF_INET
) ? inet_ntoa(*(struct in_addr
*)src
) : "-IPv6-");
1369 if ((family
== AF_INET6
) && (len
== IPV6_ADDR_LEN
) && (is_a4_mapped((const char *)src
) || is_a4_compat((const char *)src
)))
1376 item
= si_host_byaddr(si_search(), src
, family
, NULL
, (uint32_t *)err
);
1377 if (item
== NULL
) return NULL
;
1379 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1384 si_libinfo_ipnode_callback(si_item_t
*item
, uint32_t status
, void *ctx
)
1386 si_context_t
*sictx
;
1389 if (ctx
== NULL
) return;
1391 sictx
= (si_context_t
*)ctx
;
1393 if ((sictx
->orig_callback
== NULL
) || (status
== SI_STATUS_CALL_CANCELLED
))
1396 si_item_release(item
);
1400 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
1404 ((si_ipnode_async_callback
)(sictx
->orig_callback
))(NULL
, status
, sictx
->orig_context
);
1408 h
= (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1409 ((si_ipnode_async_callback
)(sictx
->orig_callback
))(h
, status
, sictx
->orig_context
);
1415 getipnodebyaddr_async_call(const void *src
, socklen_t len
, int family
, int *err
, si_ipnode_async_callback callback
, void *context
)
1417 si_context_t
*sictx
;
1421 fprintf(stderr
, ">> %s %s\n", __func__
, (family
== AF_INET
) ? inet_ntoa(*(struct in_addr
*)src
) : "-IPv6-");
1424 if ((family
== AF_INET6
) && (len
== IPV6_ADDR_LEN
) && (is_a4_mapped((const char *)src
) || is_a4_compat((const char *)src
)))
1431 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1432 if (sictx
== NULL
) return MACH_PORT_NULL
;
1434 sictx
->orig_callback
= callback
;
1435 sictx
->orig_context
= context
;
1436 sictx
->cat
= CATEGORY_HOST
;
1437 sictx
->key_offset
= -1;
1439 /* src is not a C string - pass length in num3 */
1441 return si_async_call(si_search(), SI_CALL_HOST_BYADDR
, src
, NULL
, NULL
, (uint32_t)family
, 0, srclen
, 0, (void *)si_libinfo_ipnode_callback
, sictx
);
1445 getipnodebyaddr_async_start(const void *addr
, size_t len
, int family
, int *error
, si_ipnode_async_callback callback
, void *context
)
1447 socklen_t slen
= len
;
1449 return getipnodebyaddr_async_call(addr
, slen
, family
, error
, callback
, context
);
1453 getipnodebyaddr_async_cancel(mach_port_t p
)
1456 fprintf(stderr
, "-- %s\n", __func__
);
1463 getipnodebyaddr_async_handle_reply(mach_msg_header_t
*msg
)
1466 fprintf(stderr
, "<< %s\n", __func__
);
1469 si_async_handle_reply(msg
);
1473 getipnodebyaddr_async_handleReply(mach_msg_header_t
*msg
)
1476 fprintf(stderr
, "<< %s\n", __func__
);
1479 si_async_handle_reply(msg
);
1484 sethostent(int ignored
)
1487 fprintf(stderr
, "-- %s\n", __func__
);
1490 LI_set_thread_list(CATEGORY_HOST
, NULL
);
1500 fprintf(stderr
, "-> %s\n", __func__
);
1503 list
= LI_get_thread_list(CATEGORY_HOST
);
1506 list
= si_host_all(si_search());
1507 LI_set_thread_list(CATEGORY_HOST
, list
);
1510 item
= si_list_next(list
);
1511 if (item
== NULL
) return NULL
;
1513 return (struct hostent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1520 fprintf(stderr
, "-- %s\n", __func__
);
1523 LI_set_thread_list(CATEGORY_HOST
, NULL
);
1529 ether_hostton(const char *name
, struct ether_addr
*e
)
1537 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
1540 if (name
== NULL
) return -1;
1541 if (e
== NULL
) return -1;
1543 item
= si_mac_byname(si_search(), name
);
1544 LI_set_thread_item(CATEGORY_MAC
+ 100, item
);
1545 if (item
== NULL
) return -1;
1547 mac
= (si_mac_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1549 i
= sscanf(mac
->mac
, " %x:%x:%x:%x:%x:%x", &t
[0], &t
[1], &t
[2], &t
[3], &t
[4], &t
[5]);
1550 if (i
!= 6) return -1;
1552 for (i
= 0; i
< 6; i
++) e
->ether_addr_octet
[i
] = t
[i
];
1556 /* XXX to do? async ether_hostton */
1559 ether_ntohost(char *name
, const struct ether_addr
*e
)
1566 if (name
== NULL
) return -1;
1567 if (e
== NULL
) return -1;
1569 for (i
= 0; i
< 6; i
++) x
[i
] = e
->ether_addr_octet
[i
];
1570 snprintf(str
, sizeof(str
), "%x:%x:%x:%x:%x:%x", x
[0], x
[1], x
[2], x
[3], x
[4], x
[5]);
1573 fprintf(stderr
, "-> %s %s\n", __func__
, str
);
1576 item
= si_mac_bymac(si_search(), str
);
1577 LI_set_thread_item(CATEGORY_MAC
+ 200, item
);
1578 if (item
== NULL
) return -1;
1580 mac
= (si_mac_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
1582 memcpy(name
, mac
->host
, strlen(mac
->host
) + 1);
1586 /* XXX to do? async ether_ntohost */
1591 getnetbyname(const char *name
)
1596 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
1599 item
= si_network_byname(si_search(), name
);
1600 LI_set_thread_item(CATEGORY_NETWORK
+ 100, item
);
1601 if (item
== NULL
) return NULL
;
1603 return (struct netent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1607 getnetbyname_async_call(const char *name
, si_network_async_callback callback
, void *context
)
1609 si_context_t
*sictx
;
1612 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
1615 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1616 if (sictx
== NULL
) return MACH_PORT_NULL
;
1618 sictx
->orig_callback
= callback
;
1619 sictx
->orig_context
= context
;
1620 sictx
->cat
= CATEGORY_NETWORK
;
1621 sictx
->key_offset
= 100;
1623 return si_async_call(si_search(), SI_CALL_NETWORK_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1627 getnetbyname_async_handle_reply(mach_msg_header_t
*msg
)
1630 fprintf(stderr
, "<< %s\n", __func__
);
1633 si_async_handle_reply(msg
);
1637 getnetbyaddr(uint32_t net
, int type
)
1642 fprintf(stderr
, "-> %s 0x%08x\n", __func__
, net
);
1645 if (type
!= AF_INET
) return NULL
;
1647 item
= si_network_byaddr(si_search(), net
);
1648 LI_set_thread_item(CATEGORY_NETWORK
+ 200, item
);
1649 if (item
== NULL
) return NULL
;
1651 return (struct netent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1655 getnetbyaddr_async_call(uint32_t net
, int type
, si_group_async_callback callback
, void *context
)
1657 si_context_t
*sictx
;
1660 fprintf(stderr
, ">> %s 0x%08x\n", __func__
, net
);
1663 if (type
!= AF_INET
) return MACH_PORT_NULL
;
1665 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1666 if (sictx
== NULL
) return MACH_PORT_NULL
;
1668 sictx
->orig_callback
= callback
;
1669 sictx
->orig_context
= context
;
1670 sictx
->cat
= CATEGORY_NETWORK
;
1671 sictx
->key_offset
= 200;
1673 return si_async_call(si_search(), SI_CALL_NETWORK_BYADDR
, NULL
, NULL
, NULL
, net
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1677 getnetbyaddr_async_handle_reply(mach_msg_header_t
*msg
)
1680 fprintf(stderr
, "<< %s\n", __func__
);
1683 si_async_handle_reply(msg
);
1687 setnetent(int ignored
)
1690 fprintf(stderr
, "-- %s\n", __func__
);
1693 LI_set_thread_list(CATEGORY_NETWORK
, NULL
);
1703 fprintf(stderr
, "-> %s\n", __func__
);
1706 list
= LI_get_thread_list(CATEGORY_NETWORK
);
1709 list
= si_network_all(si_search());
1710 LI_set_thread_list(CATEGORY_NETWORK
, list
);
1713 item
= si_list_next(list
);
1714 if (item
== NULL
) return NULL
;
1716 return (struct netent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1723 fprintf(stderr
, "-- %s\n", __func__
);
1726 LI_set_thread_list(CATEGORY_NETWORK
, NULL
);
1732 getservbyname(const char *name
, const char *proto
)
1737 fprintf(stderr
, "-> %s %s %s\n", __func__
, name
, proto
);
1740 item
= si_service_byname(si_search(), name
, proto
);
1741 LI_set_thread_item(CATEGORY_SERVICE
+ 100, item
);
1742 if (item
== NULL
) return NULL
;
1744 return (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1748 getservbyname_async_call(const char *name
, const char *proto
, si_service_async_callback callback
, void *context
)
1750 si_context_t
*sictx
;
1753 fprintf(stderr
, ">> %s %s %s\n", __func__
, name
, proto
);
1756 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1757 if (sictx
== NULL
) return MACH_PORT_NULL
;
1759 sictx
->orig_callback
= callback
;
1760 sictx
->orig_context
= context
;
1761 sictx
->cat
= CATEGORY_SERVICE
;
1762 sictx
->key_offset
= 100;
1764 return si_async_call(si_search(), SI_CALL_SERVICE_BYNAME
, name
, proto
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1768 getservbyname_async_handle_reply(mach_msg_header_t
*msg
)
1771 fprintf(stderr
, "<< %s\n", __func__
);
1774 si_async_handle_reply(msg
);
1778 getservbyport(int port
, const char *proto
)
1783 fprintf(stderr
, "-> %s %d %s\n", __func__
, ntohs((uint16_t)port
), proto
);
1786 item
= si_service_byport(si_search(), port
, proto
);
1787 LI_set_thread_item(CATEGORY_SERVICE
+ 200, item
);
1788 if (item
== NULL
) return NULL
;
1790 return (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1794 getservbyport_async_call(int port
, const char *proto
, si_group_async_callback callback
, void *context
)
1796 si_context_t
*sictx
;
1799 fprintf(stderr
, ">> %s %d %s\n", __func__
, port
, proto
);
1802 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1803 if (sictx
== NULL
) return MACH_PORT_NULL
;
1805 sictx
->orig_callback
= callback
;
1806 sictx
->orig_context
= context
;
1807 sictx
->cat
= CATEGORY_SERVICE
;
1808 sictx
->key_offset
= 200;
1810 return si_async_call(si_search(), SI_CALL_SERVICE_BYPORT
, NULL
, proto
, NULL
, port
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1814 getservbyport_async_handle_reply(mach_msg_header_t
*msg
)
1817 fprintf(stderr
, "<< %s\n", __func__
);
1820 si_async_handle_reply(msg
);
1824 setservent(int ignored
)
1827 fprintf(stderr
, "-- %s\n", __func__
);
1830 LI_set_thread_list(CATEGORY_SERVICE
, NULL
);
1840 fprintf(stderr
, "-> %s\n", __func__
);
1843 list
= LI_get_thread_list(CATEGORY_SERVICE
);
1846 list
= si_service_all(si_search());
1847 LI_set_thread_list(CATEGORY_SERVICE
, list
);
1850 item
= si_list_next(list
);
1851 if (item
== NULL
) return NULL
;
1853 return (struct servent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1860 fprintf(stderr
, "-- %s\n", __func__
);
1863 LI_set_thread_list(CATEGORY_SERVICE
, NULL
);
1869 getprotobyname(const char *name
)
1874 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
1877 item
= si_protocol_byname(si_search(), name
);
1878 LI_set_thread_item(CATEGORY_PROTOCOL
+ 100, item
);
1879 if (item
== NULL
) return NULL
;
1881 return (struct protoent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1885 getprotobyname_async_call(const char *name
, si_protocol_async_callback callback
, void *context
)
1887 si_context_t
*sictx
;
1890 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
1893 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1894 if (sictx
== NULL
) return MACH_PORT_NULL
;
1896 sictx
->orig_callback
= callback
;
1897 sictx
->orig_context
= context
;
1898 sictx
->cat
= CATEGORY_PROTOCOL
;
1899 sictx
->key_offset
= 100;
1901 return si_async_call(si_search(), SI_CALL_PROTOCOL_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1905 getprotobyname_async_handle_reply(mach_msg_header_t
*msg
)
1908 fprintf(stderr
, "<< %s\n", __func__
);
1911 si_async_handle_reply(msg
);
1915 getprotobynumber(int number
)
1920 fprintf(stderr
, "-> %s %d\n", __func__
, number
);
1923 item
= si_protocol_bynumber(si_search(), number
);
1924 LI_set_thread_item(CATEGORY_PROTOCOL
+ 200, item
);
1925 if (item
== NULL
) return NULL
;
1927 return (struct protoent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1931 getprotobynumber_async_call(int number
, si_group_async_callback callback
, void *context
)
1933 si_context_t
*sictx
;
1936 fprintf(stderr
, ">> %s %d\n", __func__
, number
);
1939 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
1940 if (sictx
== NULL
) return MACH_PORT_NULL
;
1942 sictx
->orig_callback
= callback
;
1943 sictx
->orig_context
= context
;
1944 sictx
->cat
= CATEGORY_PROTOCOL
;
1945 sictx
->key_offset
= 200;
1947 return si_async_call(si_search(), SI_CALL_PROTOCOL_BYNUMBER
, NULL
, NULL
, NULL
, number
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
1951 getprotobynumber_async_handle_reply(mach_msg_header_t
*msg
)
1954 fprintf(stderr
, "<< %s\n", __func__
);
1957 si_async_handle_reply(msg
);
1961 setprotoent(int ignored
)
1964 fprintf(stderr
, "-- %s\n", __func__
);
1967 LI_set_thread_list(CATEGORY_PROTOCOL
, NULL
);
1977 fprintf(stderr
, "-> %s\n", __func__
);
1980 list
= LI_get_thread_list(CATEGORY_PROTOCOL
);
1983 list
= si_protocol_all(si_search());
1984 LI_set_thread_list(CATEGORY_PROTOCOL
, list
);
1987 item
= si_list_next(list
);
1988 if (item
== NULL
) return NULL
;
1990 return (struct protoent
*)((uintptr_t)item
+ sizeof(si_item_t
));
1997 fprintf(stderr
, "-- %s\n", __func__
);
2000 LI_set_thread_list(CATEGORY_PROTOCOL
, NULL
);
2006 getrpcbyname(const char *name
)
2011 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
2014 item
= si_rpc_byname(si_search(), name
);
2015 LI_set_thread_item(CATEGORY_RPC
+ 100, item
);
2016 if (item
== NULL
) return NULL
;
2018 return (struct rpcent
*)((uintptr_t)item
+ sizeof(si_item_t
));
2022 getrpcbyname_async_call(const char *name
, si_rpc_async_callback callback
, void *context
)
2024 si_context_t
*sictx
;
2027 fprintf(stderr
, ">> %s %s\n", __func__
, name
);
2030 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2031 if (sictx
== NULL
) return MACH_PORT_NULL
;
2033 sictx
->orig_callback
= callback
;
2034 sictx
->orig_context
= context
;
2035 sictx
->cat
= CATEGORY_RPC
;
2036 sictx
->key_offset
= 100;
2038 return si_async_call(si_search(), SI_CALL_RPC_BYNAME
, name
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
2042 getrpcbyname_async_handle_reply(mach_msg_header_t
*msg
)
2045 fprintf(stderr
, "<< %s\n", __func__
);
2048 si_async_handle_reply(msg
);
2064 fprintf(stderr
, "-> %s %ld\n", __func__
, (long int)number
);
2067 item
= si_rpc_bynumber(si_search(), number
);
2068 LI_set_thread_item(CATEGORY_RPC
+ 200, item
);
2069 if (item
== NULL
) return NULL
;
2071 return (struct rpcent
*)((uintptr_t)item
+ sizeof(si_item_t
));
2075 getrpcbynumber_async_call(int number
, si_group_async_callback callback
, void *context
)
2077 si_context_t
*sictx
;
2080 fprintf(stderr
, ">> %s %d\n", __func__
, number
);
2083 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2084 if (sictx
== NULL
) return MACH_PORT_NULL
;
2086 sictx
->orig_callback
= callback
;
2087 sictx
->orig_context
= context
;
2088 sictx
->cat
= CATEGORY_RPC
;
2089 sictx
->key_offset
= 200;
2091 return si_async_call(si_search(), SI_CALL_RPC_BYNUMBER
, NULL
, NULL
, NULL
, number
, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
2095 getrpcbynumber_async_handle_reply(mach_msg_header_t
*msg
)
2098 fprintf(stderr
, "<< %s\n", __func__
);
2101 si_async_handle_reply(msg
);
2105 setrpcent(int ignored
)
2108 fprintf(stderr
, "-- %s\n", __func__
);
2111 LI_set_thread_list(CATEGORY_RPC
, NULL
);
2121 fprintf(stderr
, "-> %s\n", __func__
);
2124 list
= LI_get_thread_list(CATEGORY_RPC
);
2127 list
= si_rpc_all(si_search());
2128 LI_set_thread_list(CATEGORY_RPC
, list
);
2131 item
= si_list_next(list
);
2132 if (item
== NULL
) return NULL
;
2134 return (struct rpcent
*)((uintptr_t)item
+ sizeof(si_item_t
));
2141 fprintf(stderr
, "-- %s\n", __func__
);
2144 LI_set_thread_list(CATEGORY_RPC
, NULL
);
2150 getfsspec(const char *spec
)
2155 fprintf(stderr
, "-> %s %s\n", __func__
, spec
);
2158 item
= si_fs_byspec(si_search(), spec
);
2159 LI_set_thread_item(CATEGORY_FS
+ 100, item
);
2160 if (item
== NULL
) return NULL
;
2162 return (struct fstab
*)((uintptr_t)item
+ sizeof(si_item_t
));
2166 getfsbyname(const char *name
)
2169 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
2172 return getfsspec(name
);
2176 getfsspec_async_call(const char *spec
, si_fs_async_callback callback
, void *context
)
2178 si_context_t
*sictx
;
2181 fprintf(stderr
, ">> %s %s\n", __func__
, spec
);
2184 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2185 if (sictx
== NULL
) return MACH_PORT_NULL
;
2187 sictx
->orig_callback
= callback
;
2188 sictx
->orig_context
= context
;
2189 sictx
->cat
= CATEGORY_FS
;
2190 sictx
->key_offset
= 100;
2192 return si_async_call(si_search(), SI_CALL_FS_BYSPEC
, spec
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
2196 getfsspec_async_handle_reply(mach_msg_header_t
*msg
)
2199 fprintf(stderr
, "<< %s\n", __func__
);
2202 si_async_handle_reply(msg
);
2206 getfsfile(const char *file
)
2211 fprintf(stderr
, "-> %s %s\n", __func__
, file
);
2214 item
= si_fs_byfile(si_search(), file
);
2215 LI_set_thread_item(CATEGORY_FS
+ 200, item
);
2216 if (item
== NULL
) return NULL
;
2218 return (struct fstab
*)((uintptr_t)item
+ sizeof(si_item_t
));
2222 getfsfile_async_call(const char *file
, si_fs_async_callback callback
, void *context
)
2224 si_context_t
*sictx
;
2227 fprintf(stderr
, ">> %s %s\n", __func__
, file
);
2230 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2231 if (sictx
== NULL
) return MACH_PORT_NULL
;
2233 sictx
->orig_callback
= callback
;
2234 sictx
->orig_context
= context
;
2235 sictx
->cat
= CATEGORY_FS
;
2236 sictx
->key_offset
= 200;
2238 return si_async_call(si_search(), SI_CALL_FS_BYFILE
, file
, NULL
, NULL
, 0, 0, 0, 0, (void *)si_libinfo_general_callback
, sictx
);
2242 getfsfile_async_handle_reply(mach_msg_header_t
*msg
)
2245 fprintf(stderr
, "<< %s\n", __func__
);
2248 si_async_handle_reply(msg
);
2255 fprintf(stderr
, "-> %s\n", __func__
);
2258 LI_set_thread_list(CATEGORY_FS
, NULL
);
2269 fprintf(stderr
, "-> %s\n", __func__
);
2272 list
= LI_get_thread_list(CATEGORY_FS
);
2275 list
= si_fs_all(si_search());
2276 LI_set_thread_list(CATEGORY_FS
, list
);
2279 item
= si_list_next(list
);
2280 if (item
== NULL
) return NULL
;
2282 return (struct fstab
*)((uintptr_t)item
+ sizeof(si_item_t
));
2289 fprintf(stderr
, "-- %s\n", __func__
);
2292 LI_set_thread_list(CATEGORY_FS
, NULL
);
2298 _getaddrinfo_internal(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, const char *interface
, struct addrinfo
**res
)
2301 uint32_t family
, socktype
, protocol
, flags
, status
;
2302 struct addrinfo
*ai
;
2305 socktype
= SOCK_UNSPEC
;
2306 protocol
= IPPROTO_UNSPEC
;
2308 status
= SI_STATUS_NO_ERROR
;
2310 if (res
== NULL
) return 0;
2315 family
= hints
->ai_family
;
2316 socktype
= hints
->ai_socktype
;
2317 protocol
= hints
->ai_protocol
;
2318 flags
= hints
->ai_flags
;
2322 fprintf(stderr
, "-> %s %s %s %u %u %u 0x%08x %s\n", __func__
, nodename
, servname
, family
, socktype
, protocol
, flags
, (interface
== NULL
) ? "" : interface
);
2325 list
= si_addrinfo(si_search(), nodename
, servname
, family
, socktype
, protocol
, flags
, interface
, &status
);
2326 if ((status
!= SI_STATUS_NO_ERROR
) || (list
== NULL
) || (list
->count
== 0))
2328 si_list_release(list
);
2330 if (status
== SI_STATUS_NO_ERROR
) return EAI_NONAME
;
2331 else if (status
<= SI_STATUS_EAI_PLUS_100
) status
= EAI_FAIL
;
2332 else if (status
>= SI_STATUS_ERRNO_PLUS_200
) status
= EAI_FAIL
;
2333 else status
= status
- SI_STATUS_EAI_PLUS_100
;
2337 *res
= si_list_to_addrinfo(list
);
2338 si_list_release(list
);
2339 if (*res
== NULL
) status
= EAI_MEMORY
;
2341 /* don't return the canonical name unless asked */
2342 if ((flags
& AI_CANONNAME
) == 0)
2344 for (ai
= *res
; ai
!= NULL
; ai
= ai
->ai_next
)
2346 free(ai
->ai_canonname
);
2347 ai
->ai_canonname
= NULL
;
2355 getaddrinfo(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, struct addrinfo
**res
)
2357 return _getaddrinfo_internal(nodename
, servname
, hints
, NULL
, res
);
2363 socket_name(int sock
)
2365 static char str
[16];
2369 case SOCK_UNSPEC
: return "SOCK_UNSPEC";
2370 case SOCK_STREAM
: return "SOCK_STREAM";
2371 case SOCK_DGRAM
: return "SOCK_DGRAM";
2374 sprintf(str
, "%d", sock
);
2381 static char str
[16];
2385 case PF_UNSPEC
: return "PF_UNSPEC";
2386 case PF_INET
: return "PF_INET";
2387 case PF_INET6
: return "PF_INET6";
2390 sprintf(str
, "%d", pf
);
2395 protocol_name(int p
)
2397 static char str
[16];
2401 case IPPROTO_UNSPEC
: return "IPPROTO_UNSPEC";
2402 case IPPROTO_TCP
: return "IPPROTO_TCP";
2403 case IPPROTO_UDP
: return "IPPROTO_UDP";
2406 sprintf(str
, "%d", p
);
2411 _gai_inet_ntop(struct in6_addr a
)
2413 static char buf
[128];
2419 memset(buf
, 0, 128);
2421 p
= (char *)&a
.__u6_addr
.__u6_addr32
;
2422 for (i
= 0; i
< 8; i
++, x
+= 1)
2426 sprintf(t
, "%hx", x
);
2428 if (i
< 7) strcat(buf
, ":");
2435 fprint_addrinfo(FILE *f
, struct addrinfo
*a
)
2439 struct sockaddr_in
*s4
;
2440 struct sockaddr_in6
*s6
;
2442 if (a
== NULL
) return;
2444 if (a
->ai_flags
!= 0)
2446 fprintf(f
, "flags =");
2447 if (a
->ai_flags
& AI_PASSIVE
) fprintf(f
, " AI_PASSIVE");
2448 if (a
->ai_flags
& AI_CANONNAME
) fprintf(f
, " AI_CANONNAME");
2449 if (a
->ai_flags
& AI_NUMERICHOST
) fprintf(f
, " AI_NUMERICHOST");
2450 if (a
->ai_flags
& AI_NUMERICSERV
) fprintf(f
, " AI_NUMERICSERV");
2454 fprintf(f
, "family = %s\n", family_name(a
->ai_family
));
2455 fprintf(f
, "socktype = %s\n", socket_name(a
->ai_socktype
));
2456 fprintf(f
, "protocol = %s\n", protocol_name(a
->ai_protocol
));
2458 fprintf(f
, "canonical name = ");
2459 if (a
->ai_canonname
== NULL
) fprintf(f
, "NULL\n");
2460 else fprintf(f
, "\"%s\"\n", a
->ai_canonname
);
2462 fprintf(f
, "addrlen = %ld\n", (long int)a
->ai_addrlen
);
2464 if (a
->ai_addr
== NULL
) fprintf(f
, "sockaddr = NULL\n");
2467 if (a
->ai_family
== PF_INET
)
2469 s4
= (struct sockaddr_in
*)a
->ai_addr
;
2471 fprintf(f
, "sockaddr_in len = %d\n", s4
->sin_len
);
2472 fprintf(f
, "sockaddr_in family = %s\n", family_name(s4
->sin_family
));
2473 fprintf(f
, "sockaddr_in port = %hu\n", ntohs(s4
->sin_port
));
2474 fprintf(f
, "sockaddr_in address = %s\n", inet_ntoa(s4
->sin_addr
));
2476 else if (a
->ai_family
== PF_INET6
)
2478 s6
= (struct sockaddr_in6
*)a
->ai_addr
;
2480 fprintf(f
, "sockaddr_in6 len = %d\n", s6
->sin6_len
);
2481 fprintf(f
, "sockaddr_in6 family = %s\n", family_name(s6
->sin6_family
));
2482 fprintf(f
, "sockaddr_in6 port = %hu\n", ntohs(s6
->sin6_port
));
2483 fprintf(f
, "sockaddr_in6 flowinfo = %d\n", s6
->sin6_flowinfo
);
2484 fprintf(f
, "sockaddr_in6 address = %s\n", _gai_inet_ntop(s6
->sin6_addr
));
2485 fprintf(f
, "sockaddr_in6 scope_id = %d\n", s6
->sin6_scope_id
);
2489 fprintf(f
, "sockaddr len = %d\n", a
->ai_addr
->sa_len
);
2490 fprintf(f
, "sockaddr family = %s\n", family_name(a
->ai_addr
->sa_family
));
2491 fprintf(f
, "sockaddr data = ");
2492 for (i
= 0; i
< a
->ai_addr
->sa_len
- 2; i
++)
2494 v
= a
->ai_addr
->sa_data
[i
];
2495 fprintf(f
, "%02x", v
);
2501 if (a
->ai_next
!= NULL
)
2503 fprintf(f
, "NEXT --->\n");
2504 fprint_addrinfo(f
, a
->ai_next
);
2511 si_libinfo_addrinfo_callback(si_list_t
*list
, uint32_t status
, void *ctx
)
2513 si_context_t
*sictx
;
2514 struct addrinfo
*out
;
2519 fprintf(stderr
, " %s error no context\n", __func__
);
2521 si_list_release(list
);
2525 sictx
= (si_context_t
*)ctx
;
2527 if ((sictx
->orig_callback
== NULL
) || (status
== SI_STATUS_CALL_CANCELLED
))
2530 fprintf(stderr
, " %s error no callback\n", __func__
);
2532 si_list_release(list
);
2537 if (status
!= SI_STATUS_NO_ERROR
)
2540 fprintf(stderr
, " %s original status %d\n", __func__
, status
);
2542 if (status
<= SI_STATUS_EAI_PLUS_100
) status
= EAI_FAIL
;
2543 else if (status
>= SI_STATUS_ERRNO_PLUS_200
) status
= EAI_FAIL
;
2544 else status
= status
- SI_STATUS_EAI_PLUS_100
;
2550 fprintf(stderr
, " %s result NULL status %d (returning EAI_NONAME)\n", __func__
, status
);
2552 ((si_addrinfo_async_callback
)(sictx
->orig_callback
))(EAI_NONAME
, NULL
, sictx
->orig_context
);
2557 out
= si_list_to_addrinfo(list
);
2558 si_list_release(list
);
2562 fprintf(stderr
, " %s result conversion failed returning NULL status %d (returning EAI_MEMORY)\n", __func__
, status
);
2564 ((si_addrinfo_async_callback
)(sictx
->orig_callback
))(EAI_MEMORY
, NULL
, sictx
->orig_context
);
2570 fprintf(stderr
, " %s %d\n", __func__
, status
);
2571 fprint_addrinfo(stderr
, out
);
2573 ((si_addrinfo_async_callback
)(sictx
->orig_callback
))(status
, out
, sictx
->orig_context
);
2580 _getaddrinfo_interface_async_call(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, const char *interface
, si_addrinfo_async_callback callback
, void *context
)
2582 si_context_t
*sictx
;
2583 uint32_t family
, socktype
, protocol
, flags
;
2586 socktype
= SOCK_UNSPEC
;
2587 protocol
= IPPROTO_UNSPEC
;
2592 family
= hints
->ai_family
;
2593 socktype
= hints
->ai_socktype
;
2594 protocol
= hints
->ai_protocol
;
2595 flags
= hints
->ai_flags
;
2599 fprintf(stderr
, ">> %s %s %s %u %u %u 0x%08x\n", __func__
, nodename
, servname
, family
, socktype
, protocol
, flags
);
2602 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2603 if (sictx
== NULL
) return MACH_PORT_NULL
;
2605 sictx
->orig_callback
= callback
;
2606 sictx
->orig_context
= context
;
2607 sictx
->cat
= CATEGORY_ADDRINFO
;
2608 sictx
->key_offset
= 0;
2610 return si_async_call(si_search(), SI_CALL_ADDRINFO
, nodename
, servname
, interface
, family
, socktype
, protocol
, flags
, (void *)si_libinfo_addrinfo_callback
, sictx
);
2614 getaddrinfo_async_call(const char *nodename
, const char *servname
, const struct addrinfo
*hints
, si_addrinfo_async_callback callback
, void *context
)
2616 return _getaddrinfo_interface_async_call(nodename
, servname
, hints
, NULL
, callback
, context
);
2620 getaddrinfo_async_start(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
, si_addrinfo_async_callback callback
, void *context
)
2622 if (p
== NULL
) return EAI_SYSTEM
;
2624 *p
= getaddrinfo_async_call(nodename
, servname
, hints
, callback
, context
);
2626 if (*p
== MACH_PORT_NULL
) return EAI_SYSTEM
;
2631 getaddrinfo_async_send(mach_port_t
*p
, const char *nodename
, const char *servname
, const struct addrinfo
*hints
)
2633 return getaddrinfo_async_start(p
, nodename
, servname
, hints
, NULL
, NULL
);
2637 getaddrinfo_async_receive(mach_port_t p
, struct addrinfo
**res
)
2639 /* unsupported Leopard SPI */
2644 getaddrinfo_async_cancel(mach_port_t p
)
2647 fprintf(stderr
, "-- %s\n", __func__
);
2654 getaddrinfo_async_handle_reply(void *param
)
2656 mach_msg_header_t
*msg
;
2659 fprintf(stderr
, "<< %s\n", __func__
);
2662 msg
= (mach_msg_header_t
*)param
;
2663 si_async_handle_reply(msg
);
2671 _getnameinfo_interface_internal(const struct sockaddr
*sa
, socklen_t salen
, char *node
, socklen_t nodelen
, char *service
, socklen_t servicelen
, int flags
, const char *interface
)
2675 uint32_t status
, len
, wantn
, wants
;
2678 fprintf(stderr
, "-> %s\n", __func__
);
2681 status
= SI_STATUS_NO_ERROR
;
2684 if ((node
!= NULL
) && (nodelen
> 0)) wantn
= 1;
2687 if ((service
!= NULL
) && (servicelen
> 0)) wants
= 1;
2689 if ((wantn
== 0) && (wants
== 0)) return status
;
2691 if (wantn
== 0) flags
|= NI_NUMERICHOST
;
2692 if (wants
== 0) flags
|= NI_NUMERICSERV
;
2694 item
= si_nameinfo(si_search(), sa
, flags
, interface
, &status
);
2695 if ((status
!= SI_STATUS_NO_ERROR
) || (item
== NULL
))
2697 si_item_release(item
);
2699 if (status
== SI_STATUS_NO_ERROR
) status
= EAI_NONAME
;
2700 else if (status
<= SI_STATUS_EAI_PLUS_100
) status
= EAI_FAIL
;
2701 else if (status
>= SI_STATUS_ERRNO_PLUS_200
) status
= EAI_FAIL
;
2702 else status
= status
- SI_STATUS_EAI_PLUS_100
;
2706 ni
= (si_nameinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
2709 if (ni
->ni_node
!= NULL
) len
= strlen(ni
->ni_node
) + 1;
2710 if ((wantn
== 1) && (len
> 0))
2714 si_item_release(item
);
2715 return EAI_OVERFLOW
;
2718 memset(node
, 0, nodelen
);
2719 memcpy(node
, ni
->ni_node
, len
);
2723 if (ni
->ni_serv
!= NULL
) len
= strlen(ni
->ni_serv
) + 1;
2724 if ((wants
== 1) && (len
> 0))
2726 if (len
> servicelen
)
2728 si_item_release(item
);
2729 return EAI_OVERFLOW
;
2732 memset(service
, 0, servicelen
);
2733 memcpy(service
, ni
->ni_serv
, len
);
2736 si_item_release(item
);
2741 getnameinfo(const struct sockaddr
*sa
, socklen_t salen
, char *node
, socklen_t nodelen
, char *service
, socklen_t servicelen
, int flags
)
2743 if (sa
== NULL
) return EAI_FAIL
;
2745 if (sa
->sa_family
== AF_LINK
) return getnameinfo_link(sa
, salen
, node
, nodelen
, service
, servicelen
, flags
);
2746 return _getnameinfo_interface_internal(sa
, salen
, node
, nodelen
, service
, servicelen
, flags
, NULL
);
2750 si_libinfo_nameinfo_callback(si_item_t
*item
, uint32_t status
, void *ctx
)
2752 si_context_t
*sictx
;
2756 if (ctx
== NULL
) return;
2758 sictx
= (si_context_t
*)ctx
;
2760 if ((sictx
->orig_callback
== NULL
) || (status
== SI_STATUS_CALL_CANCELLED
))
2762 si_item_release(item
);
2767 if (status
!= SI_STATUS_NO_ERROR
)
2769 if (status
<= SI_STATUS_EAI_PLUS_100
) status
= EAI_FAIL
;
2770 else if (status
>= SI_STATUS_ERRNO_PLUS_200
) status
= EAI_FAIL
;
2771 else status
= status
- SI_STATUS_EAI_PLUS_100
;
2776 ((si_nameinfo_async_callback
)(sictx
->orig_callback
))(status
, NULL
, NULL
, sictx
->orig_context
);
2781 LI_set_thread_item(CATEGORY_NAMEINFO
, item
);
2786 ni
= (si_nameinfo_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
2787 if (ni
->ni_node
!= NULL
) node
= strdup(ni
->ni_node
);
2788 if (ni
->ni_serv
!= NULL
) serv
= strdup(ni
->ni_serv
);
2790 ((si_nameinfo_async_callback
)(sictx
->orig_callback
))(status
, node
, serv
, sictx
->orig_context
);
2796 _getnameinfo_interface_async_call(const struct sockaddr
*sa
, size_t len
, int flags
, const char *interface
, si_nameinfo_async_callback callback
, void *context
)
2798 si_context_t
*sictx
;
2802 fprintf(stderr
, ">> %s\n", __func__
);
2805 sictx
= (si_context_t
*)calloc(1, sizeof(si_context_t
));
2806 if (sictx
== NULL
) return MACH_PORT_NULL
;
2808 sictx
->orig_callback
= callback
;
2809 sictx
->orig_context
= context
;
2810 sictx
->cat
= CATEGORY_ADDRINFO
;
2811 sictx
->key_offset
= 0;
2813 /* sa is not a C string - pass length in num3 */
2815 return si_async_call(si_search(), SI_CALL_NAMEINFO
, (const char *)sa
, NULL
, interface
, flags
, 0, salen
, 0, (void *)si_libinfo_nameinfo_callback
, sictx
);
2819 getnameinfo_async_call(const struct sockaddr
*sa
, size_t len
, int flags
, si_nameinfo_async_callback callback
, void *context
)
2821 return _getnameinfo_interface_async_call(sa
, len
, flags
, NULL
, callback
, context
);
2825 getnameinfo_async_start(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
, si_nameinfo_async_callback callback
, void *context
)
2827 if (p
== NULL
) return EAI_SYSTEM
;
2828 *p
= getnameinfo_async_call(sa
, salen
, flags
, callback
, context
);
2830 if (*p
== MACH_PORT_NULL
) return EAI_SYSTEM
;
2835 getnameinfo_async_send(mach_port_t
*p
, const struct sockaddr
*sa
, size_t salen
, int flags
)
2837 return getnameinfo_async_start(p
, sa
, salen
, flags
, NULL
, NULL
);
2841 getnameinfo_async_cancel(mach_port_t p
)
2844 fprintf(stderr
, "-- %s\n", __func__
);
2851 getnameinfo_async_handle_reply(void *param
)
2853 mach_msg_header_t
*msg
;
2856 fprintf(stderr
, "<< %s\n", __func__
);
2859 msg
= (mach_msg_header_t
*)param
;
2860 si_async_handle_reply(msg
);
2865 /* getpwXXX_r and getgrXXX_r */
2868 copy_user_r(struct passwd
*in
, struct passwd
*out
, char *buffer
, int buflen
)
2873 if (in
== NULL
) return -1;
2874 if (out
== NULL
) return -1;
2876 if (buffer
== NULL
) buflen
= 0;
2878 /* Calculate size of input */
2880 if (in
->pw_name
!= NULL
) hsize
+= (strlen(in
->pw_name
) + 1);
2881 if (in
->pw_passwd
!= NULL
) hsize
+= (strlen(in
->pw_passwd
) + 1);
2882 if (in
->pw_class
!= NULL
) hsize
+= (strlen(in
->pw_class
) + 1);
2883 if (in
->pw_gecos
!= NULL
) hsize
+= (strlen(in
->pw_gecos
) + 1);
2884 if (in
->pw_dir
!= NULL
) hsize
+= (strlen(in
->pw_dir
) + 1);
2885 if (in
->pw_shell
!= NULL
) hsize
+= (strlen(in
->pw_shell
) + 1);
2887 /* Check buffer space */
2888 if (hsize
> buflen
) return -1;
2890 /* Copy result into caller's struct passwd, using buffer for memory */
2893 out
->pw_name
= NULL
;
2894 if (in
->pw_name
!= NULL
)
2897 hsize
= strlen(in
->pw_name
) + 1;
2898 memmove(bp
, in
->pw_name
, hsize
);
2902 out
->pw_passwd
= NULL
;
2903 if (in
->pw_passwd
!= NULL
)
2905 out
->pw_passwd
= bp
;
2906 hsize
= strlen(in
->pw_passwd
) + 1;
2907 memmove(bp
, in
->pw_passwd
, hsize
);
2911 out
->pw_uid
= in
->pw_uid
;
2913 out
->pw_gid
= in
->pw_gid
;
2915 out
->pw_change
= in
->pw_change
;
2917 out
->pw_class
= NULL
;
2918 if (in
->pw_class
!= NULL
)
2921 hsize
= strlen(in
->pw_class
) + 1;
2922 memmove(bp
, in
->pw_class
, hsize
);
2926 out
->pw_gecos
= NULL
;
2927 if (in
->pw_gecos
!= NULL
)
2930 hsize
= strlen(in
->pw_gecos
) + 1;
2931 memmove(bp
, in
->pw_gecos
, hsize
);
2936 if (in
->pw_dir
!= NULL
)
2939 hsize
= strlen(in
->pw_dir
) + 1;
2940 memmove(bp
, in
->pw_dir
, hsize
);
2944 out
->pw_shell
= NULL
;
2945 if (in
->pw_shell
!= NULL
)
2948 hsize
= strlen(in
->pw_shell
) + 1;
2949 memmove(bp
, in
->pw_shell
, hsize
);
2953 out
->pw_expire
= in
->pw_expire
;
2959 copy_group_r(struct group
*in
, struct group
*out
, char *buffer
, int buflen
)
2965 if (in
== NULL
) return -1;
2966 if (out
== NULL
) return -1;
2968 if (buffer
== NULL
) buflen
= 0;
2970 /* Calculate size of input */
2972 if (in
->gr_name
!= NULL
) hsize
+= (strlen(in
->gr_name
) + 1);
2973 if (in
->gr_passwd
!= NULL
) hsize
+= (strlen(in
->gr_passwd
) + 1);
2975 /* NULL pointer at end of list */
2976 hsize
+= sizeof(char *);
2979 if (in
->gr_mem
!= NULL
)
2981 for (len
= 0; in
->gr_mem
[len
] != NULL
; len
++)
2983 hsize
+= sizeof(char *);
2984 hsize
+= (strlen(in
->gr_mem
[len
]) + 1);
2988 /* Check buffer space */
2989 if (hsize
> buflen
) return -1;
2991 /* Copy result into caller's struct group, using buffer for memory */
2994 out
->gr_name
= NULL
;
2995 if (in
->gr_name
!= NULL
)
2998 hsize
= strlen(in
->gr_name
) + 1;
2999 memmove(bp
, in
->gr_name
, hsize
);
3003 out
->gr_passwd
= NULL
;
3004 if (in
->gr_passwd
!= NULL
)
3006 out
->gr_passwd
= bp
;
3007 hsize
= strlen(in
->gr_passwd
) + 1;
3008 memmove(bp
, in
->gr_passwd
, hsize
);
3012 out
->gr_gid
= in
->gr_gid
;
3015 ap
= bp
+ ((len
+ 1) * sizeof(char *));
3017 if (in
->gr_mem
!= NULL
)
3019 out
->gr_mem
= (char **)bp
;
3020 for (i
= 0; i
< len
; i
++)
3022 addr
= (unsigned long)ap
;
3023 memmove(bp
, &addr
, sizeof(unsigned long));
3024 bp
+= sizeof(unsigned long);
3026 hsize
= strlen(in
->gr_mem
[i
]) + 1;
3027 memmove(ap
, in
->gr_mem
[i
], hsize
);
3032 memset(bp
, 0, sizeof(unsigned long));
3039 getgrnam_r(const char *name
, struct group
*grp
, char *buffer
, size_t bufsize
, struct group
**result
)
3046 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
3049 if (result
!= NULL
) *result
= NULL
;
3051 if ((grp
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3053 item
= si_group_byname(si_search(), name
);
3054 if (item
== NULL
) return 0;
3056 g
= (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
3058 status
= copy_group_r(g
, grp
, buffer
, bufsize
);
3059 si_item_release(item
);
3061 if (status
!= 0) return ERANGE
;
3068 getgrgid_r(gid_t gid
, struct group
*grp
, char *buffer
, size_t bufsize
, struct group
**result
)
3075 fprintf(stderr
, "-> %s %d\n", __func__
, gid
);
3078 if (result
!= NULL
) *result
= NULL
;
3080 if ((grp
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3082 item
= si_group_bygid(si_search(), gid
);
3083 if (item
== NULL
) return 0;
3085 g
= (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
3087 status
= copy_group_r(g
, grp
, buffer
, bufsize
);
3088 si_item_release(item
);
3090 if (status
!= 0) return ERANGE
;
3097 getgruuid_r(uuid_t uuid
, struct group
*grp
, char *buffer
, size_t bufsize
, struct group
**result
)
3104 uuid_string_t uuidstr
;
3105 uuid_unparse_upper(uuid
, uuidstr
);
3106 fprintf(stderr
, "-> %s %s\n", __func__
, uuidstr
);
3109 if (result
!= NULL
) *result
= NULL
;
3111 if ((grp
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3113 item
= si_group_byuuid(si_search(), uuid
);
3114 if (item
== NULL
) return 0;
3116 g
= (struct group
*)((uintptr_t)item
+ sizeof(si_item_t
));
3118 status
= copy_group_r(g
, grp
, buffer
, bufsize
);
3119 si_item_release(item
);
3121 if (status
!= 0) return ERANGE
;
3128 getpwnam_r(const char *name
, struct passwd
*pw
, char *buffer
, size_t bufsize
, struct passwd
**result
)
3135 fprintf(stderr
, "-> %s %s\n", __func__
, name
);
3138 if (result
!= NULL
) *result
= NULL
;
3140 if ((pw
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3142 item
= si_user_byname(si_search(), name
);
3143 if (item
== NULL
) return 0;
3145 p
= (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
3147 status
= copy_user_r(p
, pw
, buffer
, bufsize
);
3148 si_item_release(item
);
3150 if (status
!= 0) return ERANGE
;
3157 getpwuid_r(uid_t uid
, struct passwd
*pw
, char *buffer
, size_t bufsize
, struct passwd
**result
)
3164 fprintf(stderr
, "-> %s %d\n", __func__
, uid
);
3167 if (result
!= NULL
) *result
= NULL
;
3169 if ((pw
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3171 item
= si_user_byuid(si_search(), uid
);
3172 if (item
== NULL
) return 0;
3174 p
= (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
3176 status
= copy_user_r(p
, pw
, buffer
, bufsize
);
3177 si_item_release(item
);
3179 if (status
!= 0) return ERANGE
;
3186 getpwuuid_r(uuid_t uuid
, struct passwd
*pw
, char *buffer
, size_t bufsize
, struct passwd
**result
)
3193 uuid_string_t uuidstr
;
3194 uuid_unparse_upper(uuid
, uuidstr
);
3195 fprintf(stderr
, "-> %s %s\n", __func__
, uuidstr
);
3198 if (result
!= NULL
) *result
= NULL
;
3200 if ((pw
== NULL
) || (buffer
== NULL
) || (result
== NULL
) || (bufsize
== 0)) return ERANGE
;
3202 item
= si_user_byuuid(si_search(), uuid
);
3203 if (item
== NULL
) return 0;
3205 p
= (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
3207 status
= copy_user_r(p
, pw
, buffer
, bufsize
);
3208 si_item_release(item
);
3210 if (status
!= 0) return ERANGE
;
3219 user_from_uid(uid_t uid
, int nouser
)
3222 static char buf
[16];
3225 if (pw
!= NULL
) return pw
->pw_name
;
3227 if (nouser
) return NULL
;
3229 snprintf(buf
, sizeof(buf
), "%u", uid
);
3234 group_from_gid(gid_t gid
, int nogroup
)
3237 static char buf
[16];
3240 if (gr
!= NULL
) return gr
->gr_name
;
3242 if (nogroup
) return NULL
;
3244 snprintf(buf
, sizeof(buf
), "%u", gid
);
3248 /* no longer supported */
3251 prdb_getbyname(const char *name
)
3254 fprintf(stderr
, "~~ %s\n", __func__
);
3263 fprintf(stderr
, "~~ %s\n", __func__
);
3269 prdb_set(const char *name
)
3272 fprintf(stderr
, "~~ %s\n", __func__
);
3280 fprintf(stderr
, "~~ %s\n", __func__
);
3284 struct bootparamsent
*
3285 bootparams_getbyname(const char *name
)
3288 fprintf(stderr
, "~~ %s\n", __func__
);
3293 struct bootparamsent
*
3294 bootparams_getent(void)
3297 fprintf(stderr
, "~~ %s\n", __func__
);
3303 bootparams_setent(void)
3306 fprintf(stderr
, "~~ %s\n", __func__
);
3311 bootparams_endent(void)
3314 fprintf(stderr
, "~~ %s\n", __func__
);
3319 bootp_getbyether(struct ether_addr
*enaddr
, char **name
,struct in_addr
*ipaddr
, char **bootfile
)
3322 fprintf(stderr
, "~~ %s\n", __func__
);
3328 bootp_getbyip(struct ether_addr
*enaddr
, char **name
, struct in_addr
*ipaddr
, char **bootfile
)
3331 fprintf(stderr
, "~~ %s\n", __func__
);