]> git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/si_module.c
Libinfo-392.tar.gz
[apple/libinfo.git] / lookup.subproj / si_module.c
1 /*
2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <net/ethernet.h>
27 #include <libkern/OSAtomic.h>
28 #include <dlfcn.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <pthread.h>
33 #include <mach/mach.h>
34 #include <dispatch/dispatch.h>
35
36 #include "si_module.h"
37
38 #define PLUGIN_DIR_PATH "/usr/lib/info"
39 #define PLUGIN_BUNDLE_SUFFIX "so"
40
41 #define WORKUNIT_CANCELLED 0x00000001
42 #define WORKUNIT_RETURNS_LIST 0x00000002
43
44 #ifdef __BIG_ENDIAN__
45 #define WORKUNIT_CANCELLED_BIT_ADDRESS 31
46 #else
47 #define WORKUNIT_CANCELLED_BIT_ADDRESS 7
48 #endif
49
50 typedef struct si_async_workunit_s
51 {
52 si_mod_t *si;
53 uint32_t call;
54 char *str1;
55 char *str2;
56 char *str3;
57 uint32_t num1;
58 uint32_t num2;
59 uint32_t num3;
60 uint32_t num4;
61 uint32_t err;
62 /* async support below */
63 uint32_t flags;
64 int32_t refcount;
65 void *callback;
66 void *context;
67 mach_port_t port;
68 mach_port_t send;
69 si_item_t *resitem;
70 si_list_t *reslist;
71 struct si_async_workunit_s *next;
72 } si_async_workunit_t;
73
74 static si_mod_t **module_list = NULL;
75 static uint32_t module_count = 0;
76 static pthread_mutex_t module_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static si_async_workunit_t *si_async_worklist = NULL;
78
79 si_mod_t *si_module_static_search(void);
80 si_mod_t *si_module_static_cache(void);
81 si_mod_t *si_module_static_file(void);
82 #ifdef DS_AVAILABLE
83 si_mod_t *si_module_static_ds(void);
84 #endif
85 si_mod_t *si_module_static_mdns(void);
86
87 static void *
88 si_mod_dlsym(void *so, const char *name, const char *sym)
89 {
90 char *str;
91 void *out;
92
93 if ((so == NULL) || (name == NULL) || (sym == NULL)) return NULL;
94
95 str = NULL;
96 asprintf(&str, "%s_%s", name, sym);
97 if (str == NULL) return NULL;
98
99 out = dlsym(so, str);
100 free(str);
101 return out;
102 }
103
104 si_mod_t *
105 si_module_with_path(const char *path, const char *name)
106 {
107 void *so;
108 int (*si_sym_init)(si_mod_t *);
109 void (*si_sym_close)(si_mod_t *);
110 int status;
111 si_mod_t *out;
112 char *outname;
113
114 if ((path == NULL) || (name == NULL))
115 {
116 errno = EINVAL;
117 return NULL;
118 }
119
120 so = dlopen(path, RTLD_LOCAL);
121 if (so == NULL) return NULL;
122
123 si_sym_init = si_mod_dlsym(so, name, "init");
124 if (si_sym_init == NULL)
125 {
126 dlclose(so);
127 errno = ECONNREFUSED;
128 return NULL;
129 }
130
131 si_sym_close = si_mod_dlsym(so, name, "close");
132 if (si_sym_close == NULL)
133 {
134 dlclose(so);
135 errno = ECONNREFUSED;
136 return NULL;
137 }
138
139 out = (si_mod_t *)calloc(1, sizeof(si_mod_t));
140 outname = strdup(name);
141
142 if ((out == NULL) || (outname == NULL))
143 {
144 free(out);
145 free(outname);
146 dlclose(so);
147 errno = ENOMEM;
148 return NULL;
149 }
150
151 out->name = outname;
152 out->refcount = 1;
153 out->flags = 0;
154 out->bundle = so;
155
156 status = si_sym_init(out);
157 if (status != 0)
158 {
159 dlclose(so);
160 free(out);
161 free(outname);
162 errno = ECONNREFUSED;
163 return NULL;
164 }
165
166 return out;
167 }
168
169 si_mod_t *
170 si_module_with_name(const char *name)
171 {
172 static struct
173 {
174 const char *name;
175 si_mod_t *(*init)(void);
176 si_mod_t *module;
177 } modules[] =
178 {
179 { "search", si_module_static_search, NULL },
180 { "cache", si_module_static_cache, NULL },
181 { "file", si_module_static_file, NULL },
182 #ifdef DS_AVAILABLE
183 { "ds", si_module_static_ds, NULL },
184 #endif
185 { "mdns", si_module_static_mdns, NULL },
186 { NULL, NULL },
187 };
188
189 si_mod_t *si = NULL;
190 int i;
191
192 /*
193 * We don't need to worry about locking during initialization
194 * because all modules init routine returns static storage.
195 */
196
197 /* Find the module by name */
198 for (i = 0; modules[i].name != NULL; ++i)
199 {
200 if (string_equal(name, modules[i].name))
201 {
202 si = modules[i].module;
203 if (si == NULL)
204 {
205 si = modules[i].init();
206 modules[i].module = si;
207 }
208
209 break;
210 }
211 }
212
213 if (si != NULL) return si;
214
215 pthread_mutex_lock(&module_mutex);
216 char *path = NULL;
217
218 asprintf(&path, "%s/%s.%s", PLUGIN_DIR_PATH, name, PLUGIN_BUNDLE_SUFFIX);
219
220 if (path == NULL)
221 {
222 errno = ENOMEM;
223 pthread_mutex_unlock(&module_mutex);
224 return NULL;
225 }
226
227 si = si_module_with_path(path, name);
228 free(path);
229
230 if (si == NULL)
231 {
232 pthread_mutex_unlock(&module_mutex);
233 return NULL;
234 }
235
236 /* add out to module_list */
237 module_list = (si_mod_t **)reallocf(module_list, (module_count + 1) * sizeof(si_mod_t *));
238 if (module_list == NULL)
239 {
240 pthread_mutex_unlock(&module_mutex);
241 return si;
242 }
243
244 module_list[module_count] = si;
245 module_count++;
246
247 pthread_mutex_unlock(&module_mutex);
248
249 return si;
250 }
251
252 si_mod_t *
253 si_module_retain(si_mod_t *si)
254 {
255 if (si == NULL) return NULL;
256 if (si->flags & SI_MOD_FLAG_STATIC) return si;
257
258 OSAtomicIncrement32Barrier(&si->refcount);
259
260 return si;
261 }
262
263 void
264 si_module_release(si_mod_t *si)
265 {
266 int32_t i;
267
268 if (si == NULL) return;
269 if (si->flags & SI_MOD_FLAG_STATIC) return;
270
271 i = OSAtomicDecrement32Barrier(&si->refcount);
272 if (i > 0) return;
273
274 pthread_mutex_lock(&module_mutex);
275
276 for (i = 0; (i < module_count) && (module_list[i] != si); i++);
277 if (i >= module_count)
278 {
279 pthread_mutex_unlock(&module_mutex);
280 return;
281 }
282
283 if (module_count == 1)
284 {
285 free(module_list);
286 module_list = NULL;
287 module_count = 0;
288 pthread_mutex_unlock(&module_mutex);
289 return;
290 }
291
292 for (i++; i < module_count; i++) module_list[i - 1] = module_list[i];
293 module_count--;
294 module_list = (si_mod_t **)reallocf(module_list, module_count * sizeof(si_mod_t *));
295 if (module_list == NULL) module_count = 0;
296
297 pthread_mutex_unlock(&module_mutex);
298
299 if (si->vtable->sim_close != NULL) si->vtable->sim_close(si);
300 if (si->bundle != NULL) dlclose(si->bundle);
301 free(si->name);
302 free(si);
303 }
304
305 const char *
306 si_module_name(si_mod_t *si)
307 {
308 if (si == NULL) return NULL;
309
310 return (const char *)si->name;
311 }
312
313 int
314 si_module_vers(si_mod_t *si)
315 {
316 if (si == NULL) return 0;
317
318 return si->vers;
319 }
320
321 int
322 si_item_match(si_item_t *item, int cat, const void *name, uint32_t num, int which)
323 {
324 int i;
325 union
326 {
327 char *x;
328 struct passwd *u;
329 struct group *g;
330 struct grouplist_s *l;
331 struct aliasent *a;
332 struct hostent *h;
333 struct netent *n;
334 struct servent *s;
335 struct protoent *p;
336 struct rpcent *r;
337 struct fstab *f;
338 struct mac_s *m;
339 } ent;
340
341 if (item == NULL) return 0;
342 if (which == SEL_ALL) return 1;
343 if ((which == SEL_NAME) && (name == NULL)) return 0;
344
345 ent.x = (char *)((uintptr_t)item + sizeof(si_item_t));
346
347 switch (cat)
348 {
349 case CATEGORY_USER:
350 {
351 if ((which == SEL_NAME) && (string_equal(name, ent.u->pw_name))) return 1;
352 else if ((which == SEL_NUMBER) && (num == (uint32_t)ent.u->pw_uid)) return 1;
353 return 0;
354 }
355 case CATEGORY_GROUP:
356 {
357 if ((which == SEL_NAME) && (string_equal(name, ent.g->gr_name))) return 1;
358 else if ((which == SEL_NUMBER) && (num == (uint32_t)ent.g->gr_gid)) return 1;
359 return 0;
360 }
361 case CATEGORY_GROUPLIST:
362 {
363 if ((which == SEL_NAME) && (string_equal(name, ent.l->gl_user))) return 1;
364 return 0;
365 }
366 case CATEGORY_ALIAS:
367 {
368 if ((which == SEL_NAME) && (string_equal(name, ent.a->alias_name))) return 1;
369 return 0;
370 }
371 case CATEGORY_HOST_IPV4:
372 case CATEGORY_HOST_IPV6:
373 {
374 /* N.B. address family is passed in num variable */
375 if (ent.h->h_addrtype != num) return 0;
376 if (which == SEL_NAME)
377 {
378 if (string_equal(name, ent.h->h_name)) return 1;
379 if (ent.h->h_aliases != NULL)
380 {
381 for (i = 0; ent.h->h_aliases[i] != NULL; i++)
382 {
383 if (string_equal(name, ent.h->h_aliases[i])) return 1;
384 }
385 }
386 }
387 else if (which == SEL_NUMBER)
388 {
389 if (memcmp(name, ent.h->h_addr_list[0], ent.h->h_length) == 0) return 1;
390 }
391 return 0;
392 }
393 case CATEGORY_NETWORK:
394 {
395 if (which == SEL_NAME)
396 {
397 if (string_equal(name, ent.n->n_name)) return 1;
398 if (ent.n->n_aliases != NULL)
399 {
400 for (i = 0; ent.n->n_aliases[i] != NULL; i++)
401 {
402 if (string_equal(name, ent.n->n_aliases[i])) return 1;
403 }
404 }
405 }
406 else if (which == SEL_NUMBER)
407 {
408 if (num == ent.n->n_net) return 1;
409 }
410 return 0;
411 }
412 case CATEGORY_SERVICE:
413 {
414 if (which == SEL_NAME)
415 {
416 /* N.B. for SEL_NAME, num is 0 (don't care), 1 (udp) or 2 (tcp) */
417 if ((num == 1) && (string_not_equal("udp", ent.s->s_proto))) return 0;
418 if ((num == 2) && (string_not_equal("tcp", ent.s->s_proto))) return 0;
419
420 if (string_equal(name, ent.s->s_name)) return 1;
421 if (ent.s->s_aliases != NULL)
422 {
423 for (i = 0; ent.s->s_aliases[i] != NULL; i++)
424 {
425 if (string_equal(name, ent.s->s_aliases[i])) return 1;
426 }
427 }
428 }
429 else if (which == SEL_NUMBER)
430 {
431 /* N.B. for SEL_NUMBER, protocol is sent in name variable */
432 if ((name != NULL) && (string_not_equal(name, ent.s->s_proto))) return 0;
433 if (num == ent.s->s_port) return 1;
434 }
435 return 0;
436 }
437 case CATEGORY_PROTOCOL:
438 {
439 if (which == SEL_NAME)
440 {
441 if (string_equal(name, ent.p->p_name)) return 1;
442 if (ent.p->p_aliases != NULL)
443 {
444 for (i = 0; ent.p->p_aliases[i] != NULL; i++)
445 {
446 if (string_equal(name, ent.p->p_aliases[i])) return 1;
447 }
448 }
449 }
450 else if (which == SEL_NUMBER)
451 {
452 if (num == ent.p->p_proto) return 1;
453 }
454 return 0;
455 }
456 case CATEGORY_RPC:
457 {
458 if (which == SEL_NAME)
459 {
460 if (string_equal(name, ent.r->r_name)) return 1;
461 if (ent.r->r_aliases != NULL)
462 {
463 for (i = 0; ent.r->r_aliases[i] != NULL; i++)
464 {
465 if (string_equal(name, ent.r->r_aliases[i])) return 1;
466 }
467 }
468 }
469 else if (which == SEL_NUMBER)
470 {
471 if (num == ent.r->r_number) return 1;
472 }
473 return 0;
474 }
475 case CATEGORY_FS:
476 {
477 if ((which == SEL_NAME) && (string_equal(name, ent.f->fs_spec))) return 1;
478 if ((which == SEL_NUMBER) && (string_equal(name, ent.f->fs_file))) return 1;
479 return 0;
480 }
481 case CATEGORY_MAC:
482 {
483 if ((which == SEL_NAME) && (string_equal(name, ent.m->host))) return 1;
484 if ((which == SEL_NUMBER) && (string_equal(name, ent.m->mac))) return 1;
485 return 0;
486 }
487 default: return 0;
488 }
489
490 return 0;
491 }
492
493 int
494 si_item_is_valid(si_item_t *item)
495 {
496 si_mod_t *si;
497
498 if (item == NULL) return 0;
499
500 si = item->src;
501
502 if (si == NULL) return 0;
503 if (si->vtable->sim_is_valid == NULL) return 0;
504
505 return si->vtable->sim_is_valid(si, item);
506 }
507
508 si_item_t *
509 si_user_byname(si_mod_t *si, const char *name)
510 {
511 if (si == NULL) return NULL;
512 if (si->vtable->sim_user_byname == NULL) return NULL;
513 return si->vtable->sim_user_byname(si, name);
514 }
515
516 si_item_t *
517 si_user_byuid(si_mod_t *si, uid_t uid)
518 {
519 if (si == NULL) return NULL;
520 if (si->vtable->sim_user_byuid == NULL) return NULL;
521 return si->vtable->sim_user_byuid(si, uid);
522 }
523
524 si_list_t *
525 si_user_all(si_mod_t *si)
526 {
527 if (si == NULL) return NULL;
528 if (si->vtable->sim_user_all == NULL) return NULL;
529 return si->vtable->sim_user_all(si);
530 }
531
532 si_item_t *
533 si_group_byname(si_mod_t *si, const char *name)
534 {
535 if (si == NULL) return NULL;
536 if (si->vtable->sim_group_byname == NULL) return NULL;
537 return si->vtable->sim_group_byname(si, name);
538 }
539
540 si_item_t *
541 si_group_bygid(si_mod_t *si, gid_t gid)
542 {
543 if (si == NULL) return NULL;
544 if (si->vtable->sim_group_bygid == NULL) return NULL;
545 return si->vtable->sim_group_bygid(si, gid);
546 }
547
548 si_list_t *
549 si_group_all(si_mod_t *si)
550 {
551 if (si == NULL) return NULL;
552 if (si->vtable->sim_group_all == NULL) return NULL;
553 return si->vtable->sim_group_all(si);
554 }
555
556 si_item_t *
557 si_grouplist(si_mod_t *si, const char *name)
558 {
559 if (si == NULL) return NULL;
560 if (si->vtable->sim_grouplist == NULL) return NULL;
561 return si->vtable->sim_grouplist(si, name);
562 }
563
564 si_list_t *
565 si_netgroup_byname(struct si_mod_s *si, const char *name)
566 {
567 if (si == NULL) return NULL;
568 if (si->vtable->sim_netgroup_byname == NULL) return NULL;
569 return si->vtable->sim_netgroup_byname(si, name);
570 }
571
572 int
573 si_in_netgroup(struct si_mod_s *si, const char *name, const char *host, const char *user, const char *domain)
574 {
575 if (si == NULL) return 0;
576 if (si->vtable->sim_in_netgroup == NULL) return 0;
577 return si->vtable->sim_in_netgroup(si, name, host, user, domain);
578 }
579
580 si_item_t *
581 si_alias_byname(si_mod_t *si, const char *name)
582 {
583 if (si == NULL) return NULL;
584 if (si->vtable->sim_alias_byname == NULL) return NULL;
585 return si->vtable->sim_alias_byname(si, name);
586 }
587
588 si_list_t *
589 si_alias_all(si_mod_t *si)
590 {
591 if (si == NULL) return NULL;
592 if (si->vtable->sim_alias_all == NULL) return NULL;
593 return si->vtable->sim_alias_all(si);
594 }
595
596 si_item_t *
597 si_host_byname(si_mod_t *si, const char *name, int af, const char *interface, uint32_t *err)
598 {
599 if (si == NULL) return NULL;
600 if (si->vtable->sim_host_byname == NULL) return NULL;
601 return si->vtable->sim_host_byname(si, name, af, interface, err);
602 }
603
604 si_item_t *
605 si_host_byaddr(si_mod_t *si, const void *addr, int af, const char *interface, uint32_t *err)
606 {
607 if (si == NULL) return NULL;
608 if (si->vtable->sim_host_byaddr == NULL) return NULL;
609 return si->vtable->sim_host_byaddr(si, addr, af, interface, err);
610 }
611
612 si_list_t *
613 si_host_all(si_mod_t *si)
614 {
615 if (si == NULL) return NULL;
616 if (si->vtable->sim_host_all == NULL) return NULL;
617 return si->vtable->sim_host_all(si);
618 }
619
620 si_item_t *
621 si_mac_byname(struct si_mod_s *si, const char *name)
622 {
623 if (si == NULL) return NULL;
624 if (si->vtable->sim_mac_byname == NULL) return NULL;
625 return si->vtable->sim_mac_byname(si, name);
626 }
627
628 si_item_t *
629 si_mac_bymac(struct si_mod_s *si, const char *mac)
630 {
631 if (si == NULL) return NULL;
632 if (si->vtable->sim_mac_bymac == NULL) return NULL;
633 return si->vtable->sim_mac_bymac(si, mac);
634 }
635
636 si_list_t *
637 si_mac_all(si_mod_t *si)
638 {
639 if (si == NULL) return NULL;
640 if (si->vtable->sim_mac_all == NULL) return NULL;
641 return si->vtable->sim_mac_all(si);
642 }
643
644 si_item_t *
645 si_network_byname(si_mod_t *si, const char *name)
646 {
647 if (si == NULL) return NULL;
648 if (si->vtable->sim_network_byname == NULL) return NULL;
649 return si->vtable->sim_network_byname(si, name);
650 }
651
652 si_item_t *
653 si_network_byaddr(si_mod_t *si, uint32_t addr)
654 {
655 if (si == NULL) return NULL;
656 if (si->vtable->sim_network_byaddr == NULL) return NULL;
657 return si->vtable->sim_network_byaddr(si, addr);
658 }
659
660 si_list_t *
661 si_network_all(si_mod_t *si)
662 {
663 if (si == NULL) return NULL;
664 if (si->vtable->sim_network_all == NULL) return NULL;
665 return si->vtable->sim_network_all(si);
666 }
667
668 si_item_t *
669 si_service_byname(si_mod_t *si, const char *name, const char *proto)
670 {
671 if (si == NULL) return NULL;
672 if (si->vtable->sim_service_byname == NULL) return NULL;
673 return si->vtable->sim_service_byname(si, name, proto);
674 }
675
676 si_item_t *
677 si_service_byport(si_mod_t *si, int port, const char *proto)
678 {
679 if (si == NULL) return NULL;
680 if (si->vtable->sim_service_byport == NULL) return NULL;
681 return si->vtable->sim_service_byport(si, port, proto);
682 }
683
684 si_list_t *
685 si_service_all(si_mod_t *si)
686 {
687 if (si == NULL) return NULL;
688 if (si->vtable->sim_service_all == NULL) return NULL;
689 return si->vtable->sim_service_all(si);
690 }
691
692 si_item_t *
693 si_protocol_byname(si_mod_t *si, const char *name)
694 {
695 if (si == NULL) return NULL;
696 if (si->vtable->sim_protocol_byname == NULL) return NULL;
697 return si->vtable->sim_protocol_byname(si, name);
698 }
699
700 si_item_t *
701 si_protocol_bynumber(si_mod_t *si, uint32_t number)
702 {
703 if (si == NULL) return NULL;
704 if (si->vtable->sim_protocol_bynumber == NULL) return NULL;
705 return si->vtable->sim_protocol_bynumber(si, number);
706 }
707
708 si_list_t *
709 si_protocol_all(si_mod_t *si)
710 {
711 if (si == NULL) return NULL;
712 if (si->vtable->sim_protocol_all == NULL) return NULL;
713 return si->vtable->sim_protocol_all(si);
714 }
715
716 si_item_t *
717 si_rpc_byname(si_mod_t *si, const char *name)
718 {
719 if (si == NULL) return NULL;
720 if (si->vtable->sim_rpc_byname == NULL) return NULL;
721 return si->vtable->sim_rpc_byname(si, name);
722 }
723
724 si_item_t *
725 si_rpc_bynumber(si_mod_t *si, int number)
726 {
727 if (si == NULL) return NULL;
728 if (si->vtable->sim_rpc_bynumber == NULL) return NULL;
729 return si->vtable->sim_rpc_bynumber(si, number);
730 }
731
732 si_list_t *
733 si_rpc_all(si_mod_t *si)
734 {
735 if (si == NULL) return NULL;
736 if (si->vtable->sim_rpc_all == NULL) return NULL;
737 return si->vtable->sim_rpc_all(si);
738 }
739
740 si_item_t *
741 si_fs_byspec(si_mod_t *si, const char *spec)
742 {
743 if (si == NULL) return NULL;
744 if (si->vtable->sim_fs_byspec == NULL) return NULL;
745 return si->vtable->sim_fs_byspec(si, spec);
746 }
747
748 si_item_t *
749 si_fs_byfile(si_mod_t *si, const char *file)
750 {
751 if (si == NULL) return NULL;
752 if (si->vtable->sim_fs_byfile == NULL) return NULL;
753 return si->vtable->sim_fs_byfile(si, file);
754 }
755
756 si_list_t *
757 si_fs_all(si_mod_t *si)
758 {
759 if (si == NULL) return NULL;
760 if (si->vtable->sim_fs_all == NULL) return NULL;
761 return si->vtable->sim_fs_all(si);
762 }
763
764 si_item_t *
765 si_item_call(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t *err)
766 {
767 if (si == NULL) return NULL;
768
769 switch (call)
770 {
771 case SI_CALL_USER_BYNAME: return si_user_byname(si, str1);
772 case SI_CALL_USER_BYUID: return si_user_byuid(si, (uid_t)num1);
773 case SI_CALL_GROUP_BYNAME: return si_group_byname(si, str1);
774 case SI_CALL_GROUP_BYGID: return si_group_bygid(si, (gid_t)num1);
775 case SI_CALL_GROUPLIST: return si_grouplist(si, str1);
776 case SI_CALL_ALIAS_BYNAME: return si_alias_byname(si, str1);
777 case SI_CALL_HOST_BYNAME: return si_host_byname(si, str1, num1, str3, err);
778 case SI_CALL_HOST_BYADDR: return si_host_byaddr(si, (void *)str1, num1, str3, err);
779 case SI_CALL_NETWORK_BYNAME: return si_network_byname(si, str1);
780 case SI_CALL_NETWORK_BYADDR: return si_network_byaddr(si, num1);
781 case SI_CALL_SERVICE_BYNAME: return si_service_byname(si, str1, str2);
782 case SI_CALL_SERVICE_BYPORT: return si_service_byport(si, num1, str2);
783 case SI_CALL_PROTOCOL_BYNAME: return si_protocol_byname(si, str1);
784 case SI_CALL_PROTOCOL_BYNUMBER: return si_protocol_bynumber(si, num1);
785 case SI_CALL_RPC_BYNAME: return si_network_byname(si, str1);
786 case SI_CALL_RPC_BYNUMBER: return si_rpc_bynumber(si, num1);
787 case SI_CALL_FS_BYSPEC: return si_fs_byspec(si, str1);
788 case SI_CALL_FS_BYFILE: return si_fs_byfile(si, str1);
789 case SI_CALL_NAMEINFO: return si_nameinfo(si, (const struct sockaddr *)str1, num1, str3, err);
790 case SI_CALL_IPNODE_BYNAME: return si_ipnode_byname(si, (const char *)str1, num1, num2, str3, err);
791 case SI_CALL_MAC_BYNAME: return si_mac_byname(si, (const char *)str1);
792 case SI_CALL_MAC_BYMAC: return si_mac_bymac(si, (const char *)str1);
793
794 /* Support for DNS async calls */
795 case SI_CALL_DNS_QUERY:
796 case SI_CALL_DNS_SEARCH:
797 {
798 if (si->vtable->sim_item_call == NULL) return NULL;
799 return si->vtable->sim_item_call(si, call, str1, str2, str3, num1, num2, err);
800 }
801
802 default: return NULL;
803 }
804
805 return NULL;
806 }
807
808 si_list_t *
809 si_list_call(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, uint32_t *err)
810 {
811 if (si == NULL) return NULL;
812
813 switch (call)
814 {
815 case SI_CALL_USER_ALL: return si_user_all(si);
816 case SI_CALL_GROUP_ALL: return si_group_all(si);
817 case SI_CALL_ALIAS_ALL: return si_alias_all(si);
818 case SI_CALL_HOST_ALL: return si_host_all(si);
819 case SI_CALL_NETWORK_ALL: return si_network_all(si);
820 case SI_CALL_SERVICE_ALL: return si_service_all(si);
821 case SI_CALL_PROTOCOL_ALL: return si_protocol_all(si);
822 case SI_CALL_RPC_ALL: return si_rpc_all(si);
823 case SI_CALL_FS_ALL: return si_fs_all(si);
824 case SI_CALL_MAC_ALL: return si_mac_all(si);
825 case SI_CALL_ADDRINFO: return si_addrinfo(si, str1, str2, num1, num2, num3, num4, str3, err);
826 default: return NULL;
827 }
828
829 return NULL;
830 }
831
832 static void
833 si_async_worklist_add_unit(si_async_workunit_t *r)
834 {
835 pthread_mutex_lock(&module_mutex);
836 r->next = si_async_worklist;
837 si_async_worklist = r;
838 pthread_mutex_unlock(&module_mutex);
839 }
840
841 static void
842 si_async_worklist_remove_unit(si_async_workunit_t *r)
843 {
844 si_async_workunit_t *x;
845
846 pthread_mutex_lock(&module_mutex);
847 if (si_async_worklist == r)
848 {
849 si_async_worklist = r->next;
850 }
851 else
852 {
853 for (x = si_async_worklist; (x != NULL) && (x->next != r); x = x->next) {;}
854 if (x != NULL) x->next = r->next;
855 }
856 pthread_mutex_unlock(&module_mutex);
857 }
858
859 static si_async_workunit_t*
860 si_async_worklist_find_unit(mach_port_t p)
861 {
862 si_async_workunit_t *r;
863
864 pthread_mutex_lock(&module_mutex);
865 for (r = si_async_worklist; (r != NULL) && (r->port != p); r = r->next) {;}
866 pthread_mutex_unlock(&module_mutex);
867
868 return r;
869 }
870
871 static si_async_workunit_t *
872 si_async_workunit_create(si_mod_t *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, void *callback, void *context)
873 {
874 si_async_workunit_t *r;
875 kern_return_t status;
876 mach_port_t reply, send;
877 mach_msg_type_name_t type;
878 char *s1, *s2, *s3;
879
880 s1 = NULL;
881 s2 = NULL;
882 s3 = NULL;
883
884 if (si_call_str1_is_buffer(call))
885 {
886 if (num3 > 0)
887 {
888 s1 = calloc(1, num3);
889 if (s1 == NULL) return NULL;
890 memcpy(s1, str1, num3);
891 }
892 }
893 else if (str1 != NULL)
894 {
895 s1 = strdup(str1);
896 if (s1 == NULL) return NULL;
897 }
898
899 if (str2 != NULL)
900 {
901 s2 = strdup(str2);
902 if (s2 == NULL)
903 {
904 if (s1 != NULL) free(s1);
905 return NULL;
906 }
907 }
908
909 if (str3 != NULL)
910 {
911 s3 = strdup(str3);
912 if (s3 == NULL)
913 {
914 if (s1 != NULL) free(s1);
915 if (s2 != NULL) free(s2);
916 return NULL;
917 }
918 }
919
920 r = (si_async_workunit_t *)calloc(1, sizeof(si_async_workunit_t));
921 if (r == NULL)
922 {
923 if (s1 != NULL) free(s1);
924 if (s2 != NULL) free(s2);
925 if (s3 != NULL) free(s3);
926 return NULL;
927 }
928
929 reply = MACH_PORT_NULL;
930 send = MACH_PORT_NULL;
931 type = 0;
932
933 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &reply);
934 if (status == KERN_SUCCESS) status = mach_port_extract_right(mach_task_self(), reply, MACH_MSG_TYPE_MAKE_SEND_ONCE, &send, &type);
935 if (status != KERN_SUCCESS)
936 {
937 if (reply != MACH_PORT_NULL) mach_port_mod_refs(mach_task_self(), reply, MACH_PORT_RIGHT_RECEIVE, -1);
938 if (s1 != NULL) free(s1);
939 if (s2 != NULL) free(s2);
940 if (s3 != NULL) free(s3);
941 free(r);
942 return NULL;
943 }
944
945 r->si = si;
946 r->call = call;
947 r->str1 = s1;
948 r->str2 = s2;
949 r->str3 = s3;
950 r->num1 = num1;
951 r->num2 = num2;
952 r->num3 = num3;
953 r->num4 = num4;
954
955 r->refcount = 2;
956 r->flags = 0;
957 if (si_call_returns_list(call)) r->flags |= WORKUNIT_RETURNS_LIST;
958
959 r->callback = callback;
960 r->context = context;
961 r->port = reply;
962 r->send = send;
963
964 si_async_worklist_add_unit(r);
965
966 return r;
967 }
968
969 static void
970 si_async_workunit_release(si_async_workunit_t *r)
971 {
972 if (r == NULL) return;
973
974 if (OSAtomicDecrement32Barrier(&(r->refcount)) != 0) return;
975
976 #ifdef CALL_TRACE
977 fprintf(stderr, "** %s freeing worklist item %p\n", __func__, r);
978 #endif
979
980 si_async_worklist_remove_unit(r);
981
982 if (r->resitem != NULL) si_item_release(r->resitem);
983 if (r->reslist != NULL) si_list_release(r->reslist);
984
985 if (r->str1 != NULL) free(r->str1);
986 if (r->str2 != NULL) free(r->str2);
987 if (r->str3 != NULL) free(r->str3);
988
989 free(r);
990 }
991
992 static void
993 si_async_launchpad(si_async_workunit_t *r)
994 {
995 kern_return_t status;
996 mach_msg_empty_send_t msg;
997
998 #ifdef CALL_TRACE
999 fprintf(stderr, "** %s starting worklist item %p\n", __func__, r);
1000 #endif
1001
1002 if (r->flags & WORKUNIT_CANCELLED)
1003 {
1004 si_async_workunit_release(r);
1005 #ifdef CALL_TRACE
1006 fprintf(stderr, "** %s worklist item %p was cancelled early\n", __func__, r);
1007 #endif
1008 return;
1009 }
1010
1011 if (r->flags & WORKUNIT_RETURNS_LIST) r->reslist = si_list_call(r->si, r->call, r->str1, r->str2, r->str3, r->num1, r->num2, r->num3, r->num4, &(r->err));
1012 else r->resitem = si_item_call(r->si, r->call, r->str1, r->str2, r->str3, r->num1, r->num2, &(r->err));
1013
1014 /*
1015 * Test and set the cancelled flag.
1016 * If it was set, then this work item was cancelled.
1017 * Otherwise, setting it here prevents si_async_cancel from cancelling:
1018 * too late to cancel now!
1019 */
1020 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS, &(r->flags)) == 1)
1021 {
1022 si_async_workunit_release(r);
1023 #ifdef CALL_TRACE
1024 fprintf(stderr, "** %s worklist item %p was cancelled in flight\n", __func__, r);
1025 #endif
1026 return;
1027 }
1028 #ifdef CALL_TRACE
1029 else fprintf(stderr, "** %s worklist item %p flags are now 0x%08x\n", __func__, r, r->flags);
1030 #endif
1031
1032 memset(&msg, 0, sizeof(mach_msg_empty_send_t));
1033
1034 msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, MACH_MSGH_BITS_ZERO);
1035 msg.header.msgh_remote_port = r->send;
1036 msg.header.msgh_local_port = MACH_PORT_NULL;
1037 msg.header.msgh_size = sizeof(mach_msg_empty_send_t);
1038 msg.header.msgh_id = r->call;
1039
1040 status = mach_msg(&(msg.header), MACH_SEND_MSG, msg.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1041 if (status != MACH_MSG_SUCCESS)
1042 {
1043 /* receiver failed - clean up to avoid a port leak */
1044 mach_msg_destroy(&(msg.header));
1045 #ifdef CALL_TRACE
1046 fprintf(stderr, "** %s mach message send failed for worklist item %p\n", __func__, r);
1047 #endif
1048 }
1049
1050 si_async_workunit_release(r);
1051
1052 /*
1053 * The client is now responsible for calling si_async_handle_reply,
1054 * which will invoke the client's callback and then release the workunit.
1055 */
1056
1057 #ifdef CALL_TRACE
1058 fprintf(stderr, "** %s completed async worklist item %p\n", __func__, r);
1059 #endif
1060 }
1061
1062 mach_port_t
1063 si_async_call(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, void *callback, void *context)
1064 {
1065 si_async_workunit_t *req;
1066
1067 if (si == NULL) return MACH_PORT_NULL;
1068 if (callback == NULL) return MACH_PORT_NULL;
1069
1070 /* if module does async on it's own, hand off the call */
1071 if (si->vtable->sim_async_call != NULL)
1072 {
1073 return si->vtable->sim_async_call(si, call, str1, str2, str3, num1, num2, num3, num4, callback, context);
1074 }
1075
1076 req = si_async_workunit_create(si, call, str1, str2, str3, num1, num2, num3, num4, callback, context);
1077 if (req == NULL) return MACH_PORT_NULL;
1078
1079 /* queue the work on the global low-priority dispatch queue */
1080 #ifdef CALL_TRACE
1081 fprintf(stderr, "** %s dispatching worklist item %p\n", __func__, req);
1082 #endif
1083 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_OVERCOMMIT), ^{ si_async_launchpad(req); });
1084
1085 return req->port;
1086 }
1087
1088 void
1089 si_async_cancel(mach_port_t p)
1090 {
1091 si_async_workunit_t *r;
1092
1093 r = si_async_worklist_find_unit(p);
1094 if (r == NULL)
1095 {
1096 #ifdef CALL_TRACE
1097 fprintf(stderr, "** %s can't find worklist item\n", __func__);
1098 #endif
1099 return;
1100 }
1101
1102 /*
1103 * Test and set the WORKUNIT_CANCELLED flag.
1104 * If it was already set, this work item has been executed - too late to cancel.
1105 */
1106 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS, &(r->flags)) == 1)
1107 {
1108 /* already executed */
1109 #ifdef CALL_TRACE
1110 fprintf(stderr, "** %s worklist item %p has executed\n", __func__, r);
1111 #endif
1112 return;
1113 }
1114
1115 #ifdef CALL_TRACE
1116 fprintf(stderr, "** %s calling worklist item %p callback SI_STATUS_CALL_CANCELLED\n", __func__, r);
1117 #endif
1118
1119 if (r->callback != NULL)
1120 {
1121 if (r->flags & WORKUNIT_RETURNS_LIST) ((list_async_callback)(r->callback))(NULL, SI_STATUS_CALL_CANCELLED, r->context);
1122 else ((item_async_callback)(r->callback))(NULL, SI_STATUS_CALL_CANCELLED, r->context);
1123 }
1124
1125 si_async_workunit_release(r);
1126
1127 mach_port_mod_refs(mach_task_self(), p, MACH_PORT_RIGHT_RECEIVE, -1);
1128 }
1129
1130 void
1131 si_async_handle_reply(mach_msg_header_t *msg)
1132 {
1133 si_async_workunit_t *r;
1134 mach_port_t reply = msg->msgh_local_port;
1135
1136 r = si_async_worklist_find_unit(reply);
1137 if (r == NULL)
1138 {
1139 #ifdef CALL_TRACE
1140 fprintf(stderr, "** %s can't find worklist item\n", __func__);
1141 #endif
1142 return;
1143 }
1144
1145 #ifdef CALL_TRACE
1146 fprintf(stderr, "** %s worklist item %p flags are 0x%08x\n", __func__, r, r->flags);
1147 #endif
1148 if ((r->flags & WORKUNIT_CANCELLED) == 0)
1149 {
1150 #ifdef CALL_TRACE
1151 fprintf(stderr, "** %s workunit thread is still active\n", __func__);
1152 #endif
1153 return;
1154 }
1155
1156 if (r->callback != NULL)
1157 {
1158 if (r->flags & WORKUNIT_RETURNS_LIST) ((list_async_callback)(r->callback))(r->reslist, r->err, r->context);
1159 else ((item_async_callback)(r->callback))(r->resitem, r->err, r->context);
1160
1161 r->reslist = NULL;
1162 r->resitem = NULL;
1163 }
1164 else
1165 {
1166 #ifdef CALL_TRACE
1167 fprintf(stderr, "** %s workunit has no callback\n", __func__);
1168 #endif
1169 }
1170
1171 si_async_workunit_release(r);
1172
1173 mach_port_mod_refs(mach_task_self(), reply, MACH_PORT_RIGHT_RECEIVE, -1);
1174 }
1175
1176 char *
1177 si_standardize_mac_address(const char *addr)
1178 {
1179 char e[6][3];
1180 char *out;
1181 struct ether_addr *ether;
1182 int i;
1183
1184 if (addr == NULL) return NULL;
1185
1186 /* ether_aton isn't thread-safe */
1187 pthread_mutex_lock(&module_mutex);
1188
1189 ether = ether_aton(addr);
1190 if (ether == NULL)
1191 {
1192 pthread_mutex_unlock(&module_mutex);
1193 return NULL;
1194 }
1195
1196 for (i = 0; i < 6; i++)
1197 {
1198 if (ether->ether_addr_octet[i] <= 15)
1199 {
1200 sprintf(e[i], "0%x", ether->ether_addr_octet[i]);
1201 }
1202 else
1203 {
1204 sprintf(e[i], "%x", ether->ether_addr_octet[i]);
1205 }
1206 }
1207
1208 pthread_mutex_unlock(&module_mutex);
1209
1210 out = NULL;
1211 asprintf(&out, "%s:%s:%s:%s:%s:%s", e[0], e[1], e[2], e[3], e[4], e[5]);
1212 return out;
1213 }