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