Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / lookup.subproj / si_module.h
1 /*
2 * Copyright (c) 2008-2018 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 #ifndef __SI_MODULE_H__
25 #define __SI_MODULE_H__
26
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <uuid/uuid.h>
35 #include <netdb.h>
36 #include <aliasdb.h>
37 #include <fstab.h>
38 #include <mach/mach.h>
39 #include <si_data.h>
40 #include <ils.h>
41
42 #define forever for(;;)
43 #define string_equal(A,B) (strcmp(A,B)==0)
44 #define string_not_equal(A,B) (strcmp(A,B)!=0)
45
46 #define SI_HAS_NAT64_SYNTHESIS 1
47
48 #define SI_CALL_USER_BYNAME 0
49 #define SI_CALL_USER_BYUID 1
50 #define SI_CALL_USER_ALL 2
51 #define SI_CALL_GROUP_BYNAME 3
52 #define SI_CALL_GROUP_BYGID 4
53 #define SI_CALL_GROUP_ALL 5
54 #define SI_CALL_NETGROUP_BYNAME 6
55 #define SI_CALL_IN_NETGROUP 7
56 #define SI_CALL_GROUPLIST 8
57 #define SI_CALL_ALIAS_BYNAME 9
58 #define SI_CALL_ALIAS_ALL 10
59 #define SI_CALL_HOST_BYNAME 11
60 #define SI_CALL_HOST_BYADDR 12
61 #define SI_CALL_HOST_ALL 13
62 #define SI_CALL_NETWORK_BYNAME 14
63 #define SI_CALL_NETWORK_BYADDR 15
64 #define SI_CALL_NETWORK_ALL 16
65 #define SI_CALL_SERVICE_BYNAME 17
66 #define SI_CALL_SERVICE_BYPORT 18
67 #define SI_CALL_SERVICE_ALL 19
68 #define SI_CALL_PROTOCOL_BYNAME 20
69 #define SI_CALL_PROTOCOL_BYNUMBER 21
70 #define SI_CALL_PROTOCOL_ALL 22
71 #define SI_CALL_RPC_BYNAME 23
72 #define SI_CALL_RPC_BYNUMBER 24
73 #define SI_CALL_RPC_ALL 25
74 #define SI_CALL_FS_BYSPEC 26
75 #define SI_CALL_FS_BYFILE 27
76 #define SI_CALL_FS_ALL 28
77 #define SI_CALL_ADDRINFO 29
78 #define SI_CALL_NAMEINFO 30
79 #define SI_CALL_IPNODE_BYNAME 31
80 #define SI_CALL_MAC_BYNAME 32
81 #define SI_CALL_MAC_BYMAC 33
82 #define SI_CALL_MAC_ALL 34
83 #define SI_CALL_DNS_QUERY 35
84 #define SI_CALL_DNS_SEARCH 36
85
86 #define si_call_returns_list(A) \
87 ((A==SI_CALL_USER_ALL)||(A==SI_CALL_GROUP_ALL)||(A==SI_CALL_HOST_ALL)||(A==SI_CALL_NETWORK_ALL)||\
88 (A==SI_CALL_SERVICE_ALL)||(A==SI_CALL_PROTOCOL_ALL)||(A==SI_CALL_RPC_ALL)||(A==SI_CALL_FS_ALL)||\
89 (A==SI_CALL_ALIAS_ALL)||(A==SI_CALL_NETGROUP_BYNAME)||(A==SI_CALL_ADDRINFO)||(A==SI_CALL_MAC_ALL))
90
91 #define si_call_str1_is_buffer(A) \
92 ((A==SI_CALL_HOST_BYADDR)||(A==SI_CALL_NAMEINFO))
93
94 #define CATEGORY_INVALID (-1)
95 #define CATEGORY_DEFAULT 0
96 #define CATEGORY_USER 1
97 #define CATEGORY_GROUP 2
98 #define CATEGORY_GROUPLIST 3
99 #define CATEGORY_NETGROUP 4
100 #define CATEGORY_ALIAS 5
101 #define CATEGORY_HOST_IPV4 6
102 #define CATEGORY_HOST_IPV6 7
103 #define CATEGORY_NETWORK 8
104 #define CATEGORY_SERVICE 9
105 #define CATEGORY_PROTOCOL 10
106 #define CATEGORY_RPC 11
107 #define CATEGORY_FS 12
108 #define CATEGORY_MAC 13
109 #define CATEGORY_NAMEINFO 14
110 #define CATEGORY_ADDRINFO 15
111 #define CATEGORY_DNSPACKET 16
112 #define CATEGORY_SRV 17
113 #define CATEGORY_COUNT 18
114
115 /* convenience */
116 #define CATEGORY_HOST CATEGORY_HOST_IPV4
117
118 #define SEL_ALL 0
119 #define SEL_NAME 1
120 #define SEL_NUMBER 2
121
122 /* host, getaddrinfo, and getnameinfo status codes */
123 #define SI_STATUS_NO_ERROR 0
124 #define SI_STATUS_H_ERRNO_HOST_NOT_FOUND 1
125 #define SI_STATUS_H_ERRNO_TRY_AGAIN 2
126 #define SI_STATUS_H_ERRNO_NO_RECOVERY 3
127 #define SI_STATUS_H_ERRNO_NO_DATA 4
128 #define SI_STATUS_INTERNAL 10
129 #define SI_STATUS_WORKITEM_NOT_FOUND 11
130 #define SI_STATUS_RETURNS_ITEM 12
131 #define SI_STATUS_RETURNS_LIST 13
132 #define SI_STATUS_CALL_IN_PROGRESS 14
133 #define SI_STATUS_CALL_CANCELLED 15
134 #define SI_STATUS_EAI_PLUS_100 100
135 #define SI_STATUS_EAI_ADDRFAMILY 101
136 #define SI_STATUS_EAI_AGAIN 102
137 #define SI_STATUS_EAI_BADFLAGS 103
138 #define SI_STATUS_EAI_FAIL 104
139 #define SI_STATUS_EAI_FAMILY 105
140 #define SI_STATUS_EAI_MEMORY 106
141 #define SI_STATUS_EAI_NODATA 107
142 #define SI_STATUS_EAI_NONAME 108
143 #define SI_STATUS_EAI_SERVICE 109
144 #define SI_STATUS_EAI_SOCKTYPE 110
145 #define SI_STATUS_EAI_SYSTEM 111
146 #define SI_STATUS_EAI_BADHINTS 112
147 #define SI_STATUS_EAI_PROTOCOL 113
148 #define SI_STATUS_EAI_OVERFLOW 114
149 #define SI_STATUS_ERRNO_PLUS_200 200
150
151 typedef void (*item_async_callback)(si_item_t *, uint32_t, void *);
152 typedef void (*list_async_callback)(si_list_t *, uint32_t, void *);
153
154 typedef struct grouplist_s
155 {
156 char *gl_user;
157 int gl_count;
158 int gl_gid_siz;
159 gid_t *gl_gid;
160 } si_grouplist_t;
161
162 typedef struct addrinfo_s
163 {
164 int ai_flags;
165 int ai_family;
166 int ai_socktype;
167 int ai_protocol;
168 uint32_t ai_addrlen;
169 socket_data_t ai_addr;
170 char *ai_canonname;
171 } si_addrinfo_t;
172
173 typedef struct nameinfo_s
174 {
175 char *ni_node;
176 char *ni_serv;
177 } si_nameinfo_t;
178
179 typedef struct mac_s
180 {
181 char *host;
182 char *mac;
183 } si_mac_t;
184
185 typedef struct netgrent_s
186 {
187 char *ng_host;
188 char *ng_user;
189 char *ng_domain;
190 } si_netgrent_t;
191
192 typedef struct dnspacket_s
193 {
194 int dns_packet_len;
195 char *dns_packet;
196 int dns_server_len;
197 struct sockaddr *dns_server;
198 } si_dnspacket_t;
199
200 typedef struct si_srv_s
201 {
202 uint16_t priority;
203 uint16_t weight;
204 uint16_t port;
205 char *target;
206 } si_srv_t;
207
208 struct si_mod_s;
209
210 struct si_mod_vtable_s
211 {
212 void (*sim_close)(struct si_mod_s *si);
213
214 int (*sim_is_valid)(struct si_mod_s *si, si_item_t *item);
215
216 si_item_t *(*sim_user_byname)(struct si_mod_s *si, const char *name);
217 si_item_t *(*sim_user_byuid)(struct si_mod_s *si, uid_t uid);
218 si_item_t *(*sim_user_byuuid)(struct si_mod_s *si, uuid_t uuid);
219 si_list_t *(*sim_user_all)(struct si_mod_s *si);
220
221 si_item_t *(*sim_group_byname)(struct si_mod_s *si, const char *name);
222 si_item_t *(*sim_group_bygid)(struct si_mod_s *si, gid_t gid);
223 si_item_t *(*sim_group_byuuid)(struct si_mod_s *si, uuid_t uuid);
224 si_list_t *(*sim_group_all)(struct si_mod_s *si);
225
226 si_item_t *(*sim_grouplist)(struct si_mod_s *si, const char *name, uint32_t count);
227
228 si_list_t *(*sim_netgroup_byname)(struct si_mod_s *si, const char *name);
229 int (*sim_in_netgroup)(struct si_mod_s *si, const char *name, const char *host, const char *user, const char *domain);
230
231 si_item_t *(*sim_alias_byname)(struct si_mod_s *si, const char *name);
232 si_list_t *(*sim_alias_all)(struct si_mod_s *si);
233
234 si_item_t *(*sim_host_byname)(struct si_mod_s *si, const char *name, int af, const char *interface, uint32_t *err);
235 si_item_t *(*sim_host_byaddr)(struct si_mod_s *si, const void *addr, int af, const char *interface, uint32_t *err);
236 si_list_t *(*sim_host_all)(struct si_mod_s *si);
237
238 si_item_t *(*sim_network_byname)(struct si_mod_s *si, const char *name);
239 si_item_t *(*sim_network_byaddr)(struct si_mod_s *si, uint32_t addr);
240 si_list_t *(*sim_network_all)(struct si_mod_s *si);
241
242 si_item_t *(*sim_service_byname)(struct si_mod_s *si, const char *name, const char *proto);
243 si_item_t *(*sim_service_byport)(struct si_mod_s *si, int port, const char *proto);
244 si_list_t *(*sim_service_all)(struct si_mod_s *si);
245
246 si_item_t *(*sim_protocol_byname)(struct si_mod_s *si, const char *name);
247 si_item_t *(*sim_protocol_bynumber)(struct si_mod_s *si, int number);
248 si_list_t *(*sim_protocol_all)(struct si_mod_s *si);
249
250 si_item_t *(*sim_rpc_byname)(struct si_mod_s *si, const char *name);
251 si_item_t *(*sim_rpc_bynumber)(struct si_mod_s *si, int number);
252 si_list_t *(*sim_rpc_all)(struct si_mod_s *si);
253
254 si_item_t *(*sim_fs_byspec)(struct si_mod_s *si, const char *spec);
255 si_item_t *(*sim_fs_byfile)(struct si_mod_s *si, const char *file);
256 si_list_t *(*sim_fs_all)(struct si_mod_s *si);
257
258 si_item_t *(*sim_mac_byname)(struct si_mod_s *si, const char *name);
259 si_item_t *(*sim_mac_bymac)(struct si_mod_s *si, const char *mac);
260 si_list_t *(*sim_mac_all)(struct si_mod_s *si);
261
262 si_list_t *(*sim_addrinfo)(struct si_mod_s *si, const void *node, const void *serv, uint32_t family, uint32_t socktype, uint32_t protocol, uint32_t flags, const char *interface, uint32_t *err);
263 int (*sim_wants_addrinfo)(struct si_mod_s *si);
264
265 si_item_t *(*sim_nameinfo)(struct si_mod_s *si, const struct sockaddr *sa, int flags, const char *interface, uint32_t *err);
266
267 si_list_t *(*sim_srv_byname)(struct si_mod_s *si, const char *qname, const char *interface, uint32_t *err);
268
269 si_item_t *(*sim_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);
270 si_list_t *(*sim_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);
271
272 mach_port_t (*sim_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);
273 void (*sim_async_cancel)(mach_port_t p);
274 void (*sim_async_handle_reply)(mach_msg_header_t *msg);
275 };
276
277 #define SI_MOD_FLAG_STATIC 0x00000001
278
279 typedef struct si_mod_s
280 {
281 char *name;
282 uint32_t vers;
283 int32_t refcount;
284 uint32_t flags;
285
286 void *bundle;
287 void *private;
288
289 const struct si_mod_vtable_s *vtable;
290 } si_mod_t;
291
292 si_mod_t *si_module_with_name(const char *name);
293 si_mod_t *si_module_retain(si_mod_t *si);
294 void si_module_release(si_mod_t *si);
295 const char *si_module_name(si_mod_t *si);
296 int si_module_vers(si_mod_t *si);
297
298 si_mod_t *si_search(void);
299
300 int si_item_match(si_item_t *item, int cat, const void *name, uint32_t num, int which);
301 int si_item_is_valid(si_item_t *item);
302
303 si_item_t *si_user_byname(si_mod_t *si, const char *name);
304 si_item_t *si_user_byuid(si_mod_t *si, uid_t uid);
305 si_item_t *si_user_byuuid(si_mod_t *si, uuid_t uuid);
306 si_list_t *si_user_all(si_mod_t *si);
307
308 si_item_t *si_group_byname(si_mod_t *si, const char *name);
309 si_item_t *si_group_bygid(si_mod_t *si, gid_t gid);
310 si_item_t *si_group_byuuid(si_mod_t *si, uuid_t uuid);
311 si_list_t *si_group_all(si_mod_t *si);
312
313 si_item_t *si_grouplist(si_mod_t *si, const char *name, uint32_t count);
314
315 int si_in_netgroup(struct si_mod_s *si, const char *name, const char *host, const char *user, const char *domain);
316 si_list_t *si_netgroup_byname(struct si_mod_s *si, const char *name);
317
318 si_item_t *si_alias_byname(struct si_mod_s *si, const char *name);
319 si_list_t *si_alias_all(struct si_mod_s *si);
320
321 si_item_t *si_host_byname(si_mod_t *si, const char *name, int af, const char *interface, uint32_t *err);
322 si_item_t *si_host_byaddr(si_mod_t *si, const void *addr, int af, const char *interface, uint32_t *err);
323 si_list_t *si_host_all(si_mod_t *si);
324
325 si_item_t *si_mac_byname(struct si_mod_s *si, const char *name);
326 si_item_t *si_mac_bymac(struct si_mod_s *si, const char *mac);
327 si_list_t *si_mac_all(struct si_mod_s *si);
328
329 si_item_t *si_network_byname(si_mod_t *si, const char *name);
330 si_item_t *si_network_byaddr(si_mod_t *si, uint32_t addr);
331 si_list_t *si_network_all(si_mod_t *si);
332
333 si_item_t *si_service_byname(si_mod_t *si, const char *name, const char *proto);
334 si_item_t *si_service_byport(si_mod_t *si, int port, const char *proto);
335 si_list_t *si_service_all(si_mod_t *si);
336
337 si_item_t *si_protocol_byname(si_mod_t *si, const char *name);
338 si_item_t *si_protocol_bynumber(si_mod_t *si, uint32_t number);
339 si_list_t *si_protocol_all(si_mod_t *si);
340
341 si_item_t *si_rpc_byname(si_mod_t *si, const char *name);
342 si_item_t *si_rpc_bynumber(si_mod_t *si, int number);
343 si_list_t *si_rpc_all(si_mod_t *si);
344
345 si_item_t *si_fs_byspec(si_mod_t *si, const char *spec);
346 si_item_t *si_fs_byfile(si_mod_t *si, const char *file);
347 si_list_t *si_fs_all(si_mod_t *si);
348
349 int si_wants_addrinfo(si_mod_t *s);
350 si_list_t *si_addrinfo(si_mod_t *si, const char *node, const char *serv, uint32_t family, uint32_t socktype, uint32_t protocol, uint32_t flags, const char *interface, uint32_t *err);
351
352 si_item_t *si_nameinfo(si_mod_t *si, const struct sockaddr *sa, int flags, const char *interface, uint32_t *err);
353 si_item_t *si_ipnode_byname(si_mod_t *si, const char *name, int family, int flags, const char *interface, uint32_t *err);
354
355 si_list_t *si_srv_byname(si_mod_t *si, const char *qname, const char *interface, uint32_t *err);
356
357 si_item_t *si_item_call(si_mod_t *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t *err);
358 si_list_t *si_list_call(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, uint32_t *err);
359
360 extern mach_port_t si_async_call(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);
361 extern void si_async_cancel(mach_port_t p);
362 extern void si_async_handle_reply(mach_msg_header_t *msg);
363
364 char *si_standardize_mac_address(const char *addr);
365 si_item_t *si_addrinfo_v4(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname);
366 si_item_t *si_addrinfo_v6(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in6_addr *addr, uint16_t iface, const char *cname);
367 si_item_t *si_addrinfo_v4_mapped(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname);
368 si_list_t *si_addrinfo_list(si_mod_t *si, uint32_t flags, int socktype, int proto, struct in_addr *a4, struct in6_addr *a6, int port, int scopeid, const char *cname4, const char *cname6);
369 si_list_t *si_addrinfo_list_from_hostent(si_mod_t *si, uint32_t flags, uint32_t socktype, uint32_t proto, uint16_t port, uint16_t scope, const struct hostent *h4, const struct hostent *h6);
370
371 int _gai_serv_to_port(const char *serv, uint32_t proto, uint16_t *port);
372 si_list_t *_gai_simple(si_mod_t *si, const void *nodeptr, const void *servptr, uint32_t family, uint32_t socktype, uint32_t proto, uint32_t flags, const char *interface, uint32_t *err);
373 #if (!defined(LIBINFO_INSTALL_API) || !LIBINFO_INSTALL_API)
374 int si_inet_config(uint32_t *inet4, uint32_t *inet6);
375 #endif
376
377 bool _gai_nat64_can_v4_address_be_synthesized(const struct in_addr *ipv4_addr);
378 void si_set_nat64_v4_requires_synthesis(bool (*nat64_v4_requires_synthesis)(const struct in_addr *ipv4_addr));
379 void si_set_nat64_v4_synthesize(int (*nat64_v4_synthesize)(uint32_t *index, const struct in_addr *ipv4, struct in6_addr **out_ipv6_addrs));
380
381 #endif /* ! __SI_MODULE_H__ */