Home | History | Annotate | Line # | Download | only in includes
      1 /*	$NetBSD: tree.h,v 1.3 2022/04/03 01:10:58 christos Exp $	*/
      2 
      3 /* tree.h
      4 
      5    Definitions for address trees... */
      6 
      7 /*
      8  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
      9  * Copyright (c) 1996-2003 by Internet Software Consortium
     10  *
     11  * This Source Code Form is subject to the terms of the Mozilla Public
     12  * License, v. 2.0. If a copy of the MPL was not distributed with this
     13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     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  *   PO Box 360
     25  *   Newmarket, NH 03857 USA
     26  *   <info (at) 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 	expr_concat_dclist
    197 };
    198 
    199 struct expression {
    200 	int refcnt;
    201 	enum expr_op op;
    202 	union expr_union {
    203 		struct {
    204 			struct expression *expr;
    205 			struct expression *offset;
    206 			struct expression *len;
    207 		} substring;
    208 		struct expression *equal [2];
    209 		struct expression *and [2];
    210 		struct expression *or [2];
    211 		struct expression *not;
    212 		struct expression *add;
    213 		struct expression *subtract;
    214 		struct expression *multiply;
    215 		struct expression *divide;
    216 		struct expression *remainder;
    217 		struct collection *check;
    218 		struct {
    219 			struct expression *expr;
    220 			struct expression *len;
    221 		} suffix;
    222 		struct expression *lcase;
    223 		struct expression *ucase;
    224 		struct option *option;
    225 		struct option *config_option;
    226 		struct {
    227 			struct expression *offset;
    228 			struct expression *len;
    229 		} packet;
    230 		struct data_string const_data;
    231 		struct expression *extract_int;
    232 		struct expression *encode_int;
    233 		unsigned long const_int;
    234 		struct expression *concat [2];
    235 		struct dns_host_entry *host_lookup;
    236 		struct option *exists;
    237 		struct data_string encapsulate;
    238 		struct {
    239 			struct expression *base;
    240 			struct expression *width;
    241 			struct expression *separator;
    242 			struct expression *buffer;
    243 		} b2a;
    244 		struct {
    245 			struct expression *width;
    246 			struct expression *buffer;
    247 		} reverse;
    248 		struct {
    249 			struct expression *car;
    250 			struct expression *cdr;
    251 		} pick_first_value;
    252 		struct {
    253 			struct expression *car;
    254 			struct expression *cdr;
    255 		} dns_transaction;
    256  		struct {
    257 			unsigned rrclass;
    258 			unsigned rrtype;
    259  			struct expression *rrname;
    260  			struct expression *rrdata;
    261  			struct expression *ttl;
    262  		} ns_add;
    263  		struct {
    264 			unsigned rrclass;
    265 			unsigned rrtype;
    266  			struct expression *rrname;
    267  			struct expression *rrdata;
    268  		} ns_delete, ns_exists, ns_not_exists;
    269 		char *variable;
    270 		struct {
    271 			struct expression *val;
    272 			struct expression *next;
    273 		} arg;
    274 		struct {
    275 			char *name;
    276 			struct expression *arglist;
    277 		} funcall;
    278 		struct fundef *func;
    279 		struct {
    280 			struct expression *relay;
    281 			struct expression *roption;
    282 		} v6relay;
    283 	} data;
    284 	int flags;
    285 #	define EXPR_EPHEMERAL	1
    286 };
    287 
    288 /* DNS host entry structure... */
    289 struct dns_host_entry {
    290 	int refcnt;
    291 	TIME timeout;
    292 	struct data_string data;
    293 	char hostname [1];
    294 };
    295 
    296 struct option_cache; /* forward */
    297 struct packet; /* forward */
    298 struct option_state; /* forward */
    299 struct decoded_option_state; /* forward */
    300 struct lease; /* forward */
    301 struct client_state; /* forward */
    302 
    303 struct universe {
    304 	const char *name;
    305 	struct option_cache *(*lookup_func) (struct universe *,
    306 					     struct option_state *,
    307 					     unsigned);
    308 	void (*save_func) (struct universe *, struct option_state *,
    309 			   struct option_cache *, isc_boolean_t);
    310 	void (*foreach) (struct packet *,
    311 			 struct lease *, struct client_state *,
    312 			 struct option_state *, struct option_state *,
    313 			 struct binding_scope **, struct universe *, void *,
    314 			 void (*) (struct option_cache *, struct packet *,
    315 				   struct lease *, struct client_state *,
    316 				   struct option_state *,
    317 				   struct option_state *,
    318 				   struct binding_scope **,
    319 				   struct universe *, void *));
    320 	void (*delete_func) (struct universe *universe,
    321 			     struct option_state *, int);
    322 	int (*option_state_dereference) (struct universe *,
    323 					 struct option_state *,
    324 					 const char *, int);
    325 	int (*decode) (struct option_state *,
    326 		       const unsigned char *, unsigned, struct universe *);
    327 	int (*encapsulate) (struct data_string *, struct packet *,
    328 			    struct lease *, struct client_state *,
    329 			    struct option_state *, struct option_state *,
    330 			    struct binding_scope **,
    331 			    struct universe *);
    332 	u_int32_t (*get_tag) (const unsigned char *);
    333 	void (*store_tag) (unsigned char *, u_int32_t);
    334 	u_int32_t (*get_length) (const unsigned char *);
    335 	void (*store_length) (unsigned char *, u_int32_t);
    336 	int tag_size, length_size;
    337 	unsigned site_code_min, end;
    338 	option_name_hash_t *name_hash;
    339 	option_code_hash_t *code_hash;
    340 	struct option *enc_opt;
    341 	int index;
    342 
    343 	/* Flags should probably become condensed. */
    344 	int concat_duplicates;
    345 };
    346 
    347 struct option {
    348 	const char *name;
    349 	const char *format;
    350 	struct universe *universe;
    351 	unsigned code;
    352 	int refcnt;
    353 };
    354