xref: /minix/external/bsd/dhcp/dist/includes/tree.h (revision 83ee113e)
1 /*	$NetBSD: tree.h,v 1.1.1.3 2014/07/12 11:57:56 spz Exp $	*/
2 /* tree.h
3 
4    Definitions for address trees... */
5 
6 /*
7  * Copyright (c) 2011,2013,2014 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 2004,2007-2009 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1996-2003 by Internet Software Consortium
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  *   Internet Systems Consortium, Inc.
24  *   950 Charter Street
25  *   Redwood City, CA 94063
26  *   <info@isc.org>
27  *   https://www.isc.org/
28  *
29  */
30 
31 /* A pair of pointers, suitable for making a linked list. */
32 typedef struct _pair {
33 	caddr_t car;
34 	struct _pair *cdr;
35 } *pair;
36 
37 struct option_chain_head {
38 	int refcnt;
39 	pair first;
40 };
41 
42 struct enumeration_value {
43 	const char *name;
44 	u_int8_t value;
45 };
46 
47 struct enumeration {
48 	struct enumeration *next;
49 	const char *name;
50 	unsigned width;
51 	struct enumeration_value *values;
52 };
53 
54 /* Tree node types... */
55 #define TREE_CONCAT		1
56 #define TREE_HOST_LOOKUP	2
57 #define TREE_CONST		3
58 #define TREE_LIMIT		4
59 #define TREE_DATA_EXPR		5
60 
61 /* A data buffer with a reference count. */
62 struct buffer {
63 	int refcnt;
64 	unsigned char data [1];
65 };
66 
67 /* XXX The mechanism by which data strings are returned is currently
68    XXX broken: rather than returning an ephemeral pointer, we create
69    XXX a reference to the data in the caller's space, which the caller
70    XXX then has to dereference - instead, the reference should be
71    XXX ephemeral by default and be made a persistent reference explicitly. */
72 /* XXX on the other hand, it seems to work pretty nicely, so maybe the
73    XXX above comment is meshuggenah. */
74 /* XXX I think the above comment tries to say this:
75    XXX    http://tinyurl.com/2tjqre */
76 
77 /* A string of data bytes, possibly accompanied by a larger buffer. */
78 struct data_string {
79 	struct buffer *buffer;
80 	const unsigned char *data;
81 	unsigned len;	/* Does not include NUL terminator, if any. */
82 	int terminated;
83 };
84 
85 enum expression_context {
86 	context_any, /* indefinite */
87 	context_boolean,
88 	context_data,
89 	context_numeric,
90 	context_dns,
91 	context_data_or_numeric, /* indefinite */
92 	context_function
93 };
94 
95 struct fundef {
96 	int refcnt;
97 	struct string_list *args;
98 	struct executable_statement *statements;
99 };
100 
101 struct binding_value {
102 	int refcnt;
103 	enum {
104 		binding_boolean,
105 		binding_data,
106 		binding_numeric,
107 		binding_dns,
108 		binding_function
109 	} type;
110 	union value {
111 		struct data_string data;
112 		unsigned long intval;
113 		int boolean;
114 		struct fundef *fundef;
115 		struct binding_value *bv;
116 	} value;
117 };
118 
119 struct binding {
120 	struct binding *next;
121 	char *name;
122 	struct binding_value *value;
123 };
124 
125 struct binding_scope {
126 	int refcnt;
127 	struct binding_scope *outer;
128 	struct binding *bindings;
129 };
130 
131 /* Expression tree structure. */
132 
133 enum expr_op {
134 	expr_none,
135 	expr_match,
136 	expr_check,
137 	expr_equal,
138 	expr_substring,
139 	expr_suffix,
140 	expr_concat,
141 	expr_host_lookup,
142 	expr_and,
143 	expr_or,
144 	expr_not,
145 	expr_option,
146 	expr_hardware,
147 	expr_packet,
148 	expr_const_data,
149 	expr_extract_int8,
150 	expr_extract_int16,
151 	expr_extract_int32,
152 	expr_encode_int8,
153 	expr_encode_int16,
154 	expr_encode_int32,
155 	expr_const_int,
156 	expr_exists,
157 	expr_encapsulate,
158 	expr_known,
159 	expr_reverse,
160 	expr_leased_address,
161 	expr_binary_to_ascii,
162 	expr_config_option,
163 	expr_host_decl_name,
164 	expr_pick_first_value,
165  	expr_lease_time,
166  	expr_dns_transaction,
167 	expr_static,
168 	expr_ns_add,
169  	expr_ns_delete,
170  	expr_ns_exists,
171  	expr_ns_not_exists,
172 	expr_not_equal,
173 	expr_null,
174 	expr_variable_exists,
175 	expr_variable_reference,
176 	expr_filename,
177  	expr_sname,
178 	expr_arg,
179 	expr_funcall,
180 	expr_function,
181 	expr_add,
182 	expr_subtract,
183 	expr_multiply,
184 	expr_divide,
185 	expr_remainder,
186 	expr_binary_and,
187 	expr_binary_or,
188 	expr_binary_xor,
189 	expr_client_state,
190 	expr_ucase,
191 	expr_lcase,
192 	expr_regex_match,
193 	expr_iregex_match,
194 	expr_gethostname,
195 	expr_v6relay
196 };
197 
198 struct expression {
199 	int refcnt;
200 	enum expr_op op;
201 	union expr_union {
202 		struct {
203 			struct expression *expr;
204 			struct expression *offset;
205 			struct expression *len;
206 		} substring;
207 		struct expression *equal [2];
208 		struct expression *and [2];
209 		struct expression *or [2];
210 		struct expression *not;
211 		struct expression *add;
212 		struct expression *subtract;
213 		struct expression *multiply;
214 		struct expression *divide;
215 		struct expression *remainder;
216 		struct collection *check;
217 		struct {
218 			struct expression *expr;
219 			struct expression *len;
220 		} suffix;
221 		struct expression *lcase;
222 		struct expression *ucase;
223 		struct option *option;
224 		struct option *config_option;
225 		struct {
226 			struct expression *offset;
227 			struct expression *len;
228 		} packet;
229 		struct data_string const_data;
230 		struct expression *extract_int;
231 		struct expression *encode_int;
232 		unsigned long const_int;
233 		struct expression *concat [2];
234 		struct dns_host_entry *host_lookup;
235 		struct option *exists;
236 		struct data_string encapsulate;
237 		struct {
238 			struct expression *base;
239 			struct expression *width;
240 			struct expression *separator;
241 			struct expression *buffer;
242 		} b2a;
243 		struct {
244 			struct expression *width;
245 			struct expression *buffer;
246 		} reverse;
247 		struct {
248 			struct expression *car;
249 			struct expression *cdr;
250 		} pick_first_value;
251 		struct {
252 			struct expression *car;
253 			struct expression *cdr;
254 		} dns_transaction;
255  		struct {
256 			unsigned rrclass;
257 			unsigned rrtype;
258  			struct expression *rrname;
259  			struct expression *rrdata;
260  			struct expression *ttl;
261  		} ns_add;
262  		struct {
263 			unsigned rrclass;
264 			unsigned rrtype;
265  			struct expression *rrname;
266  			struct expression *rrdata;
267  		} ns_delete, ns_exists, ns_not_exists;
268 		char *variable;
269 		struct {
270 			struct expression *val;
271 			struct expression *next;
272 		} arg;
273 		struct {
274 			char *name;
275 			struct expression *arglist;
276 		} funcall;
277 		struct fundef *func;
278 		struct {
279 			struct expression *relay;
280 			struct expression *roption;
281 		} v6relay;
282 	} data;
283 	int flags;
284 #	define EXPR_EPHEMERAL	1
285 };
286 
287 /* DNS host entry structure... */
288 struct dns_host_entry {
289 	int refcnt;
290 	TIME timeout;
291 	struct data_string data;
292 	char hostname [1];
293 };
294 
295 struct option_cache; /* forward */
296 struct packet; /* forward */
297 struct option_state; /* forward */
298 struct decoded_option_state; /* forward */
299 struct lease; /* forward */
300 struct client_state; /* forward */
301 
302 struct universe {
303 	const char *name;
304 	struct option_cache *(*lookup_func) (struct universe *,
305 					     struct option_state *,
306 					     unsigned);
307 	void (*save_func) (struct universe *, struct option_state *,
308 			   struct option_cache *, isc_boolean_t);
309 	void (*foreach) (struct packet *,
310 			 struct lease *, struct client_state *,
311 			 struct option_state *, struct option_state *,
312 			 struct binding_scope **, struct universe *, void *,
313 			 void (*) (struct option_cache *, struct packet *,
314 				   struct lease *, struct client_state *,
315 				   struct option_state *,
316 				   struct option_state *,
317 				   struct binding_scope **,
318 				   struct universe *, void *));
319 	void (*delete_func) (struct universe *universe,
320 			     struct option_state *, int);
321 	int (*option_state_dereference) (struct universe *,
322 					 struct option_state *,
323 					 const char *, int);
324 	int (*decode) (struct option_state *,
325 		       const unsigned char *, unsigned, struct universe *);
326 	int (*encapsulate) (struct data_string *, struct packet *,
327 			    struct lease *, struct client_state *,
328 			    struct option_state *, struct option_state *,
329 			    struct binding_scope **,
330 			    struct universe *);
331 	u_int32_t (*get_tag) (const unsigned char *);
332 	void (*store_tag) (unsigned char *, u_int32_t);
333 	u_int32_t (*get_length) (const unsigned char *);
334 	void (*store_length) (unsigned char *, u_int32_t);
335 	int tag_size, length_size;
336 	unsigned site_code_min, end;
337 	option_name_hash_t *name_hash;
338 	option_code_hash_t *code_hash;
339 	struct option *enc_opt;
340 	int index;
341 
342 	/* Flags should probably become condensed. */
343 	int concat_duplicates;
344 };
345 
346 struct option {
347 	const char *name;
348 	const char *format;
349 	struct universe *universe;
350 	unsigned code;
351 	int refcnt;
352 };
353