2 * Copyright (c) 2008-2009 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@
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <net/ethernet.h>
27 #include <libkern/OSAtomic.h>
33 #include <mach/mach.h>
35 #include "si_module.h"
37 #define PLUGIN_DIR_PATH "/usr/lib/info"
38 #define PLUGIN_BUNDLE_SUFFIX "so"
40 #define WORKUNIT_CALL_ACTIVE 0x00000001
41 #define WORKUNIT_THREAD_ACTIVE 0x00000002
42 #define WORKUNIT_RETURNS_LIST 0x00000004
44 typedef struct si_async_workunit_s
55 /* async support below */
63 struct si_async_workunit_s
*next
;
64 } si_async_workunit_t
;
66 static si_mod_t
**module_list
= NULL
;
67 static uint32_t module_count
= 0;
68 static pthread_mutex_t module_mutex
= PTHREAD_MUTEX_INITIALIZER
;
69 static si_async_workunit_t
*si_async_worklist
= NULL
;
71 __private_extern__ si_mod_t
*si_module_static_search();
72 __private_extern__ si_mod_t
*si_module_static_file();
73 __private_extern__ si_mod_t
*si_module_static_cache();
74 __private_extern__ si_mod_t
*si_module_static_mdns();
76 __private_extern__ si_mod_t
*si_module_static_ds();
80 si_mod_dlsym(void *so
, const char *name
, const char *sym
)
85 if ((so
== NULL
) || (name
== NULL
) || (sym
== NULL
)) return NULL
;
88 asprintf(&str
, "%s_%s", name
, sym
);
89 if (str
== NULL
) return NULL
;
97 si_mod_static(const char *name
)
99 if (name
== NULL
) return NULL
;
101 if (string_equal(name
, "search")) return si_module_static_search();
102 if (string_equal(name
, "file")) return si_module_static_file();
103 if (string_equal(name
, "cache")) return si_module_static_cache();
104 if (string_equal(name
, "mdns")) return si_module_static_mdns();
106 if (string_equal(name
, "ds")) return si_module_static_ds();
111 __private_extern__ si_mod_t
*
112 si_module_with_path(const char *path
, const char *name
)
115 int (*si_sym_init
)(si_mod_t
*);
116 void (*si_sym_close
)(si_mod_t
*);
121 if ((path
== NULL
) || (name
== NULL
))
127 so
= dlopen(path
, RTLD_LOCAL
);
128 if (so
== NULL
) return NULL
;
130 si_sym_init
= si_mod_dlsym(so
, name
, "init");
131 if (si_sym_init
== NULL
)
134 errno
= ECONNREFUSED
;
138 si_sym_close
= si_mod_dlsym(so
, name
, "close");
139 if (si_sym_close
== NULL
)
142 errno
= ECONNREFUSED
;
146 out
= (si_mod_t
*)calloc(1, sizeof(si_mod_t
));
147 outname
= strdup(name
);
149 if ((out
== NULL
) || (outname
== NULL
))
151 if (out
!= NULL
) free(out
);
152 if (outname
!= NULL
) free(outname
);
162 status
= si_sym_init(out
);
168 errno
= ECONNREFUSED
;
176 si_module_with_name(const char *name
)
188 pthread_mutex_lock(&module_mutex
);
190 for (i
= 0; i
< module_count
; i
++)
192 if (string_equal(module_list
[i
]->name
, name
))
194 si_module_retain(module_list
[i
]);
195 pthread_mutex_unlock(&module_mutex
);
196 return module_list
[i
];
200 pthread_mutex_unlock(&module_mutex
);
201 out
= si_mod_static(name
);
202 pthread_mutex_lock(&module_mutex
);
208 /* mdns lives in dns.so */
209 asprintf(&path
, "%s/%s.%s", PLUGIN_DIR_PATH
, name
, PLUGIN_BUNDLE_SUFFIX
);
214 pthread_mutex_unlock(&module_mutex
);
218 out
= si_module_with_path(path
, name
);
224 pthread_mutex_unlock(&module_mutex
);
228 /* add out to module_list */
229 if (module_count
== 0)
231 module_list
= (si_mod_t
**)calloc(1, sizeof(si_mod_t
*));
235 module_list
= (si_mod_t
**)reallocf(module_list
, (module_count
+ 1) * sizeof(si_mod_t
*));
238 if (module_list
== NULL
)
240 pthread_mutex_unlock(&module_mutex
);
244 module_list
[module_count
] = out
;
247 pthread_mutex_unlock(&module_mutex
);
252 __private_extern__ si_mod_t
*
253 si_module_retain(si_mod_t
*si
)
255 if (si
== NULL
) return NULL
;
257 OSAtomicIncrement32Barrier(&si
->refcount
);
263 si_module_release(si_mod_t
*si
)
267 if (si
== NULL
) return;
269 i
= OSAtomicDecrement32Barrier(&si
->refcount
);
272 pthread_mutex_lock(&module_mutex
);
274 for (i
= 0; (i
< module_count
) && (module_list
[i
] != si
); i
++);
275 if (i
>= module_count
)
277 pthread_mutex_unlock(&module_mutex
);
281 if (module_count
== 1)
286 pthread_mutex_unlock(&module_mutex
);
290 for (i
++; i
< module_count
; i
++) module_list
[i
- 1] = module_list
[i
];
292 module_list
= (si_mod_t
**)reallocf(module_list
, module_count
* sizeof(si_mod_t
*));
293 if (module_list
== NULL
) module_count
= 0;
295 pthread_mutex_unlock(&module_mutex
);
297 if (si
->sim_close
!= NULL
) si
->sim_close(si
);
298 if (si
->bundle
!= NULL
) dlclose(si
->bundle
);
299 if (si
->name
!= NULL
) free(si
->name
);
303 __private_extern__
const char *
304 si_module_name(si_mod_t
*si
)
306 if (si
== NULL
) return NULL
;
311 __private_extern__
int
312 si_module_vers(si_mod_t
*si
)
314 if (si
== NULL
) return 0;
319 __private_extern__
int
320 si_item_match(si_item_t
*item
, int cat
, const void *name
, uint32_t num
, int which
)
328 struct grouplist_s
*l
;
339 if (item
== NULL
) return 0;
340 if (which
== SEL_ALL
) return 1;
341 if ((which
== SEL_NAME
) && (name
== NULL
)) return 0;
343 ent
.x
= (char *)((uintptr_t)item
+ sizeof(si_item_t
));
349 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.u
->pw_name
))) return 1;
350 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.u
->pw_uid
)) return 1;
355 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.g
->gr_name
))) return 1;
356 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.g
->gr_gid
)) return 1;
359 case CATEGORY_GROUPLIST
:
361 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.l
->gl_user
))) return 1;
366 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.a
->alias_name
))) return 1;
369 case CATEGORY_HOST_IPV4
:
370 case CATEGORY_HOST_IPV6
:
372 /* N.B. address family is passed in num variable */
373 if (ent
.h
->h_addrtype
!= num
) return 0;
374 if (which
== SEL_NAME
)
376 if (string_equal(name
, ent
.h
->h_name
)) return 1;
377 if (ent
.h
->h_aliases
!= NULL
)
379 for (i
= 0; ent
.h
->h_aliases
[i
] != NULL
; i
++)
381 if (string_equal(name
, ent
.h
->h_aliases
[i
])) return 1;
385 else if (which
== SEL_NUMBER
)
387 if (memcmp(name
, ent
.h
->h_addr_list
[0], ent
.h
->h_length
) == 0) return 1;
391 case CATEGORY_NETWORK
:
393 if (which
== SEL_NAME
)
395 if (string_equal(name
, ent
.n
->n_name
)) return 1;
396 if (ent
.n
->n_aliases
!= NULL
)
398 for (i
= 0; ent
.n
->n_aliases
[i
] != NULL
; i
++)
400 if (string_equal(name
, ent
.n
->n_aliases
[i
])) return 1;
404 else if (which
== SEL_NUMBER
)
406 if (num
== ent
.n
->n_net
) return 1;
410 case CATEGORY_SERVICE
:
412 if (which
== SEL_NAME
)
414 /* N.B. for SEL_NAME, num is 0 (don't care), 1 (udp) or 2 (tcp) */
415 if ((num
== 1) && (string_not_equal("udp", ent
.s
->s_proto
))) return 0;
416 if ((num
== 2) && (string_not_equal("tcp", ent
.s
->s_proto
))) return 0;
418 if (string_equal(name
, ent
.s
->s_name
)) return 1;
419 if (ent
.s
->s_aliases
!= NULL
)
421 for (i
= 0; ent
.s
->s_aliases
[i
] != NULL
; i
++)
423 if (string_equal(name
, ent
.s
->s_aliases
[i
])) return 1;
427 else if (which
== SEL_NUMBER
)
429 /* N.B. for SEL_NUMBER, protocol is sent in name variable */
430 if ((name
!= NULL
) && (string_not_equal(name
, ent
.s
->s_proto
))) return 0;
431 if (num
== ent
.s
->s_port
) return 1;
435 case CATEGORY_PROTOCOL
:
437 if (which
== SEL_NAME
)
439 if (string_equal(name
, ent
.p
->p_name
)) return 1;
440 if (ent
.p
->p_aliases
!= NULL
)
442 for (i
= 0; ent
.p
->p_aliases
[i
] != NULL
; i
++)
444 if (string_equal(name
, ent
.p
->p_aliases
[i
])) return 1;
448 else if (which
== SEL_NUMBER
)
450 if (num
== ent
.p
->p_proto
) return 1;
456 if (which
== SEL_NAME
)
458 if (string_equal(name
, ent
.r
->r_name
)) return 1;
459 if (ent
.r
->r_aliases
!= NULL
)
461 for (i
= 0; ent
.r
->r_aliases
[i
] != NULL
; i
++)
463 if (string_equal(name
, ent
.r
->r_aliases
[i
])) return 1;
467 else if (which
== SEL_NUMBER
)
469 if (num
== ent
.r
->r_number
) return 1;
475 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.f
->fs_spec
))) return 1;
476 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.f
->fs_file
))) return 1;
481 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.m
->host
))) return 1;
482 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.m
->mac
))) return 1;
491 __private_extern__
int
492 si_item_is_valid(si_item_t
*item
)
496 if (item
== NULL
) return 0;
500 if (si
== NULL
) return 0;
501 if (si
->sim_is_valid
== NULL
) return 0;
503 return si
->sim_is_valid(si
, item
);
506 __private_extern__ si_item_t
*
507 si_user_byname(si_mod_t
*si
, const char *name
)
509 if (si
== NULL
) return NULL
;
510 if (si
->sim_user_byname
== NULL
) return NULL
;
511 return si
->sim_user_byname(si
, name
);
514 __private_extern__ si_item_t
*
515 si_user_byuid(si_mod_t
*si
, uid_t uid
)
517 if (si
== NULL
) return NULL
;
518 if (si
->sim_user_byuid
== NULL
) return NULL
;
519 return si
->sim_user_byuid(si
, uid
);
522 __private_extern__ si_list_t
*
523 si_user_all(si_mod_t
*si
)
525 if (si
== NULL
) return NULL
;
526 if (si
->sim_user_all
== NULL
) return NULL
;
527 return si
->sim_user_all(si
);
530 __private_extern__ si_item_t
*
531 si_group_byname(si_mod_t
*si
, const char *name
)
533 if (si
== NULL
) return NULL
;
534 if (si
->sim_group_byname
== NULL
) return NULL
;
535 return si
->sim_group_byname(si
, name
);
538 __private_extern__ si_item_t
*
539 si_group_bygid(si_mod_t
*si
, gid_t gid
)
541 if (si
== NULL
) return NULL
;
542 if (si
->sim_group_bygid
== NULL
) return NULL
;
543 return si
->sim_group_bygid(si
, gid
);
546 __private_extern__ si_list_t
*
547 si_group_all(si_mod_t
*si
)
549 if (si
== NULL
) return NULL
;
550 if (si
->sim_group_all
== NULL
) return NULL
;
551 return si
->sim_group_all(si
);
554 __private_extern__ si_item_t
*
555 si_grouplist(si_mod_t
*si
, const char *name
)
557 if (si
== NULL
) return NULL
;
558 if (si
->sim_grouplist
== NULL
) return NULL
;
559 return si
->sim_grouplist(si
, name
);
562 __private_extern__ si_list_t
*
563 si_netgroup_byname(struct si_mod_s
*si
, const char *name
)
565 if (si
== NULL
) return NULL
;
566 if (si
->sim_netgroup_byname
== NULL
) return NULL
;
567 return si
->sim_netgroup_byname(si
, name
);
570 __private_extern__
int
571 si_in_netgroup(struct si_mod_s
*si
, const char *name
, const char *host
, const char *user
, const char *domain
)
573 if (si
== NULL
) return 0;
574 if (si
->sim_in_netgroup
== NULL
) return 0;
575 return si
->sim_in_netgroup(si
, name
, host
, user
, domain
);
578 __private_extern__ si_item_t
*
579 si_alias_byname(si_mod_t
*si
, const char *name
)
581 if (si
== NULL
) return NULL
;
582 if (si
->sim_alias_byname
== NULL
) return NULL
;
583 return si
->sim_alias_byname(si
, name
);
586 __private_extern__ si_list_t
*
587 si_alias_all(si_mod_t
*si
)
589 if (si
== NULL
) return NULL
;
590 if (si
->sim_alias_all
== NULL
) return NULL
;
591 return si
->sim_alias_all(si
);
594 __private_extern__ si_item_t
*
595 si_host_byname(si_mod_t
*si
, const char *name
, int af
, uint32_t *err
)
597 if (si
== NULL
) return NULL
;
598 if (si
->sim_host_byname
== NULL
) return NULL
;
599 return si
->sim_host_byname(si
, name
, af
, err
);
602 __private_extern__ si_item_t
*
603 si_host_byaddr(si_mod_t
*si
, const void *addr
, int af
, uint32_t *err
)
605 if (si
== NULL
) return NULL
;
606 if (si
->sim_host_byaddr
== NULL
) return NULL
;
607 return si
->sim_host_byaddr(si
, addr
, af
, err
);
610 __private_extern__ si_list_t
*
611 si_host_all(si_mod_t
*si
)
613 if (si
== NULL
) return NULL
;
614 if (si
->sim_host_all
== NULL
) return NULL
;
615 return si
->sim_host_all(si
);
618 __private_extern__ si_item_t
*
619 si_mac_byname(struct si_mod_s
*si
, const char *name
)
621 if (si
== NULL
) return NULL
;
622 if (si
->sim_mac_byname
== NULL
) return NULL
;
623 return si
->sim_mac_byname(si
, name
);
626 __private_extern__ si_item_t
*
627 si_mac_bymac(struct si_mod_s
*si
, const char *mac
)
629 if (si
== NULL
) return NULL
;
630 if (si
->sim_mac_bymac
== NULL
) return NULL
;
631 return si
->sim_mac_bymac(si
, mac
);
634 __private_extern__ si_list_t
*
635 si_mac_all(si_mod_t
*si
)
637 if (si
== NULL
) return NULL
;
638 if (si
->sim_mac_all
== NULL
) return NULL
;
639 return si
->sim_mac_all(si
);
642 __private_extern__ si_item_t
*
643 si_network_byname(si_mod_t
*si
, const char *name
)
645 if (si
== NULL
) return NULL
;
646 if (si
->sim_network_byname
== NULL
) return NULL
;
647 return si
->sim_network_byname(si
, name
);
650 __private_extern__ si_item_t
*
651 si_network_byaddr(si_mod_t
*si
, uint32_t addr
)
653 if (si
== NULL
) return NULL
;
654 if (si
->sim_network_byaddr
== NULL
) return NULL
;
655 return si
->sim_network_byaddr(si
, addr
);
658 __private_extern__ si_list_t
*
659 si_network_all(si_mod_t
*si
)
661 if (si
== NULL
) return NULL
;
662 if (si
->sim_network_all
== NULL
) return NULL
;
663 return si
->sim_network_all(si
);
666 __private_extern__ si_item_t
*
667 si_service_byname(si_mod_t
*si
, const char *name
, const char *proto
)
669 if (si
== NULL
) return NULL
;
670 if (si
->sim_service_byname
== NULL
) return NULL
;
671 return si
->sim_service_byname(si
, name
, proto
);
674 __private_extern__ si_item_t
*
675 si_service_byport(si_mod_t
*si
, int port
, const char *proto
)
677 if (si
== NULL
) return NULL
;
678 if (si
->sim_service_byport
== NULL
) return NULL
;
679 return si
->sim_service_byport(si
, port
, proto
);
682 __private_extern__ si_list_t
*
683 si_service_all(si_mod_t
*si
)
685 if (si
== NULL
) return NULL
;
686 if (si
->sim_service_all
== NULL
) return NULL
;
687 return si
->sim_service_all(si
);
690 __private_extern__ si_item_t
*
691 si_protocol_byname(si_mod_t
*si
, const char *name
)
693 if (si
== NULL
) return NULL
;
694 if (si
->sim_protocol_byname
== NULL
) return NULL
;
695 return si
->sim_protocol_byname(si
, name
);
698 __private_extern__ si_item_t
*
699 si_protocol_bynumber(si_mod_t
*si
, uint32_t number
)
701 if (si
== NULL
) return NULL
;
702 if (si
->sim_protocol_bynumber
== NULL
) return NULL
;
703 return si
->sim_protocol_bynumber(si
, number
);
706 __private_extern__ si_list_t
*
707 si_protocol_all(si_mod_t
*si
)
709 if (si
== NULL
) return NULL
;
710 if (si
->sim_protocol_all
== NULL
) return NULL
;
711 return si
->sim_protocol_all(si
);
714 __private_extern__ si_item_t
*
715 si_rpc_byname(si_mod_t
*si
, const char *name
)
717 if (si
== NULL
) return NULL
;
718 if (si
->sim_rpc_byname
== NULL
) return NULL
;
719 return si
->sim_rpc_byname(si
, name
);
722 __private_extern__ si_item_t
*
723 si_rpc_bynumber(si_mod_t
*si
, int number
)
725 if (si
== NULL
) return NULL
;
726 if (si
->sim_rpc_bynumber
== NULL
) return NULL
;
727 return si
->sim_rpc_bynumber(si
, number
);
730 __private_extern__ si_list_t
*
731 si_rpc_all(si_mod_t
*si
)
733 if (si
== NULL
) return NULL
;
734 if (si
->sim_rpc_all
== NULL
) return NULL
;
735 return si
->sim_rpc_all(si
);
738 __private_extern__ si_item_t
*
739 si_fs_byspec(si_mod_t
*si
, const char *spec
)
741 if (si
== NULL
) return NULL
;
742 if (si
->sim_fs_byspec
== NULL
) return NULL
;
743 return si
->sim_fs_byspec(si
, spec
);
746 __private_extern__ si_item_t
*
747 si_fs_byfile(si_mod_t
*si
, const char *file
)
749 if (si
== NULL
) return NULL
;
750 if (si
->sim_fs_byfile
== NULL
) return NULL
;
751 return si
->sim_fs_byfile(si
, file
);
754 __private_extern__ si_list_t
*
755 si_fs_all(si_mod_t
*si
)
757 if (si
== NULL
) return NULL
;
758 if (si
->sim_fs_all
== NULL
) return NULL
;
759 return si
->sim_fs_all(si
);
762 __private_extern__ si_item_t
*
763 si_item_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, uint32_t num1
, uint32_t num2
, uint32_t *err
)
765 if (si
== NULL
) return NULL
;
769 case SI_CALL_USER_BYNAME
: return si_user_byname(si
, str1
);
770 case SI_CALL_USER_BYUID
: return si_user_byuid(si
, (uid_t
)num1
);
771 case SI_CALL_GROUP_BYNAME
: return si_group_byname(si
, str1
);
772 case SI_CALL_GROUP_BYGID
: return si_group_bygid(si
, (gid_t
)num1
);
773 case SI_CALL_GROUPLIST
: return si_grouplist(si
, str1
);
774 case SI_CALL_ALIAS_BYNAME
: return si_alias_byname(si
, str1
);
775 case SI_CALL_HOST_BYNAME
: return si_host_byname(si
, str1
, num1
, err
);
776 case SI_CALL_HOST_BYADDR
: return si_host_byaddr(si
, (void *)str1
, num1
, err
);
777 case SI_CALL_NETWORK_BYNAME
: return si_network_byname(si
, str1
);
778 case SI_CALL_NETWORK_BYADDR
: return si_network_byaddr(si
, num1
);
779 case SI_CALL_SERVICE_BYNAME
: return si_service_byname(si
, str1
, str2
);
780 case SI_CALL_SERVICE_BYPORT
: return si_service_byport(si
, num1
, str2
);
781 case SI_CALL_PROTOCOL_BYNAME
: return si_protocol_byname(si
, str1
);
782 case SI_CALL_PROTOCOL_BYNUMBER
: return si_protocol_bynumber(si
, num1
);
783 case SI_CALL_RPC_BYNAME
: return si_network_byname(si
, str1
);
784 case SI_CALL_RPC_BYNUMBER
: return si_rpc_bynumber(si
, num1
);
785 case SI_CALL_FS_BYSPEC
: return si_fs_byspec(si
, str1
);
786 case SI_CALL_FS_BYFILE
: return si_fs_byfile(si
, str1
);
787 case SI_CALL_NAMEINFO
: return si_nameinfo(si
, (const struct sockaddr
*)str1
, num1
, err
);
788 case SI_CALL_IPNODE_BYNAME
: return si_ipnode_byname(si
, (const char *)str1
, num1
, num2
, err
);
789 case SI_CALL_MAC_BYNAME
: return si_mac_byname(si
, (const char *)str1
);
790 case SI_CALL_MAC_BYMAC
: return si_mac_bymac(si
, (const char *)str1
);
792 /* Support for DNS async calls */
793 case SI_CALL_DNS_QUERY
:
794 case SI_CALL_DNS_SEARCH
:
796 if (si
->sim_item_call
== NULL
) return NULL
;
797 return si
->sim_item_call(si
, call
, str1
, str2
, num1
, num2
, err
);
800 default: return NULL
;
806 __private_extern__ si_list_t
*
807 si_list_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, uint32_t *err
)
809 if (si
== NULL
) return NULL
;
813 case SI_CALL_USER_ALL
: return si_user_all(si
);
814 case SI_CALL_GROUP_ALL
: return si_group_all(si
);
815 case SI_CALL_ALIAS_ALL
: return si_alias_all(si
);
816 case SI_CALL_HOST_ALL
: return si_host_all(si
);
817 case SI_CALL_NETWORK_ALL
: return si_network_all(si
);
818 case SI_CALL_SERVICE_ALL
: return si_service_all(si
);
819 case SI_CALL_PROTOCOL_ALL
: return si_protocol_all(si
);
820 case SI_CALL_RPC_ALL
: return si_rpc_all(si
);
821 case SI_CALL_FS_ALL
: return si_fs_all(si
);
822 case SI_CALL_MAC_ALL
: return si_mac_all(si
);
823 case SI_CALL_ADDRINFO
: return si_addrinfo(si
, str1
, str2
, num1
, num2
, num3
, num4
, err
);
824 default: return NULL
;
831 si_async_worklist_add_unit(si_async_workunit_t
*r
)
833 pthread_mutex_lock(&module_mutex
);
834 r
->next
= si_async_worklist
;
835 si_async_worklist
= r
;
836 pthread_mutex_unlock(&module_mutex
);
840 si_async_worklist_remove_unit(si_async_workunit_t
*r
)
842 si_async_workunit_t
*x
;
844 pthread_mutex_lock(&module_mutex
);
845 if (si_async_worklist
== r
)
847 si_async_worklist
= r
->next
;
851 for (x
= si_async_worklist
; (x
!= NULL
) && (x
->next
!= r
); x
= x
->next
) {;}
852 if (x
!= NULL
) x
->next
= r
->next
;
854 pthread_mutex_unlock(&module_mutex
);
857 static si_async_workunit_t
*
858 si_async_worklist_find_unit(mach_port_t p
)
860 si_async_workunit_t
*r
;
862 pthread_mutex_lock(&module_mutex
);
863 for (r
= si_async_worklist
; (r
!= NULL
) && (r
->port
!= p
); r
= r
->next
) {;}
864 pthread_mutex_unlock(&module_mutex
);
869 static si_async_workunit_t
*
870 si_async_workunit_create(si_mod_t
*si
, int call
, const char *str1
, const char *str2
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, void *callback
, void *context
)
872 si_async_workunit_t
*r
;
873 kern_return_t status
;
874 mach_port_t reply
, send
;
875 mach_msg_type_name_t type
;
881 if (si_call_str1_is_buffer(call
))
885 s1
= calloc(1, num3
);
886 if (s1
== NULL
) return NULL
;
887 memcpy(s1
, str1
, num3
);
890 else if (str1
!= NULL
)
893 if (s1
== NULL
) return NULL
;
901 if (s1
!= NULL
) free(s1
);
906 r
= (si_async_workunit_t
*)calloc(1, sizeof(si_async_workunit_t
));
909 if (s1
!= NULL
) free(s1
);
910 if (s2
!= NULL
) free(s2
);
914 reply
= MACH_PORT_NULL
;
915 send
= MACH_PORT_NULL
;
918 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &reply
);
919 if (status
== KERN_SUCCESS
) status
= mach_port_extract_right(mach_task_self(), reply
, MACH_MSG_TYPE_MAKE_SEND_ONCE
, &send
, &type
);
920 if (status
!= KERN_SUCCESS
)
922 if (reply
!= MACH_PORT_NULL
) mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
923 if (s1
!= NULL
) free(s1
);
924 if (s2
!= NULL
) free(s2
);
938 r
->flags
= WORKUNIT_CALL_ACTIVE
| WORKUNIT_THREAD_ACTIVE
;
939 if (si_call_returns_list(call
)) r
->flags
|= WORKUNIT_RETURNS_LIST
;
941 r
->callback
= callback
;
942 r
->context
= context
;
946 si_async_worklist_add_unit(r
);
952 si_async_workunit_release(si_async_workunit_t
*r
)
954 if (r
== NULL
) return;
956 if (r
->flags
& (WORKUNIT_THREAD_ACTIVE
| WORKUNIT_CALL_ACTIVE
)) return;
958 si_async_worklist_remove_unit(r
);
960 if (r
->resitem
!= NULL
) si_item_release(r
->resitem
);
961 if (r
->reslist
!= NULL
) si_list_release(r
->reslist
);
963 if (r
->str1
!= NULL
) free(r
->str1
);
964 if (r
->str2
!= NULL
) free(r
->str2
);
970 si_async_launchpad(void *x
)
972 si_async_workunit_t
*r
;
973 kern_return_t status
;
974 mach_msg_empty_send_t msg
;
976 if (x
== NULL
) pthread_exit(NULL
);
977 r
= (si_async_workunit_t
*)x
;
979 if (r
->flags
& WORKUNIT_RETURNS_LIST
) r
->reslist
= si_list_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->num1
, r
->num2
, r
->num3
, r
->num4
, &(r
->err
));
980 else r
->resitem
= si_item_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->num1
, r
->num2
, &(r
->err
));
982 memset(&msg
, 0, sizeof(mach_msg_empty_send_t
));
984 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, MACH_MSGH_BITS_ZERO
);
985 msg
.header
.msgh_remote_port
= r
->send
;
986 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
987 msg
.header
.msgh_size
= sizeof(mach_msg_empty_send_t
);
988 msg
.header
.msgh_id
= r
->call
;
990 OSAtomicAnd32Barrier(~WORKUNIT_THREAD_ACTIVE
, &r
->flags
);
992 si_async_workunit_release(r
);
994 status
= mach_msg(&(msg
.header
), MACH_SEND_MSG
, msg
.header
.msgh_size
, 0, MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
995 if (status
!= MACH_MSG_SUCCESS
)
997 /* receiver failed - clean up to avoid a port leak */
998 mach_msg_destroy(&(msg
.header
));
1008 si_async_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, void *callback
, void *context
)
1010 si_async_workunit_t
*req
;
1011 pthread_attr_t attr
;
1014 if (si
== NULL
) return MACH_PORT_NULL
;
1015 if (callback
== NULL
) return MACH_PORT_NULL
;
1017 /* if module does async on it's own, hand off the call */
1018 if (si
->sim_async_call
!= NULL
)
1020 return si
->sim_async_call(si
, call
, str1
, str2
, num1
, num2
, num3
, num4
, callback
, context
);
1023 req
= si_async_workunit_create(si
, call
, str1
, str2
, num1
, num2
, num3
, num4
, callback
, context
);
1024 if (req
== NULL
) return MACH_PORT_NULL
;
1026 /* start a new thread to do the work */
1027 pthread_attr_init(&attr
);
1028 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1029 pthread_create(&t
, &attr
, si_async_launchpad
, req
);
1030 pthread_attr_destroy(&attr
);
1036 si_async_cancel(mach_port_t p
)
1038 si_async_workunit_t
*r
;
1040 r
= si_async_worklist_find_unit(p
);
1041 if (r
== NULL
) return;
1043 OSAtomicAnd32Barrier(~WORKUNIT_CALL_ACTIVE
, &r
->flags
);
1045 if (r
->callback
!= NULL
)
1047 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1048 else ((item_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1051 si_async_workunit_release(r
);
1053 mach_port_mod_refs(mach_task_self(), p
, MACH_PORT_RIGHT_RECEIVE
, -1);
1057 si_async_handle_reply(mach_msg_header_t
*msg
)
1059 si_async_workunit_t
*r
;
1060 mach_port_t reply
= msg
->msgh_local_port
;
1062 r
= si_async_worklist_find_unit(reply
);
1066 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1071 if (r
->flags
& WORKUNIT_THREAD_ACTIVE
)
1074 fprintf(stderr
, "** %s workunit thread is still active\n", __func__
);
1079 if (r
->callback
!= NULL
)
1081 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(r
->reslist
, r
->err
, r
->context
);
1082 else ((item_async_callback
)(r
->callback
))(r
->resitem
, r
->err
, r
->context
);
1090 fprintf(stderr
, "** %s workunit has no callback\n", __func__
);
1094 OSAtomicAnd32Barrier(~WORKUNIT_CALL_ACTIVE
, &r
->flags
);
1096 si_async_workunit_release(r
);
1098 mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
1101 __private_extern__
char *
1102 si_canonical_mac_address(const char *addr
)
1106 struct ether_addr
*ether
;
1109 if (addr
== NULL
) return NULL
;
1111 /* ether_aton isn't thread-safe */
1112 pthread_mutex_lock(&module_mutex
);
1114 ether
= ether_aton(addr
);
1117 pthread_mutex_unlock(&module_mutex
);
1121 for (i
= 0, bit
= 1; i
< 6; i
++, bit
*= 2)
1123 if ((i
& bit
) && (ether
->ether_addr_octet
[i
] <= 15))
1125 sprintf(e
[i
], "0%x", ether
->ether_addr_octet
[i
]);
1129 sprintf(e
[i
], "%x", ether
->ether_addr_octet
[i
]);
1133 pthread_mutex_unlock(&module_mutex
);
1136 asprintf(&out
, "%s:%s:%s:%s:%s:%s", e
[0], e
[1], e
[2], e
[3], e
[4], e
[5]);