addrtree.h revision 1.1 1 1.1 christos /*
2 1.1 christos * edns-subnet/addrtree.h -- radix tree for edns subnet cache.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2013, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos * The addrtree is a radix tree designed for edns subnet. Most notable
39 1.1 christos * is the addition of 'scope' to a node. Scope is only relevant for
40 1.1 christos * nodes with elem set, it indicates the number of bits the authority
41 1.1 christos * desires.
42 1.1 christos *
43 1.1 christos * For retrieving data one needs an address and address length
44 1.1 christos * (sourcemask). While traversing the tree the first matching node is
45 1.1 christos * returned. A node matches when
46 1.1 christos * node.scope<=sourcemask && node.elem!=NULL
47 1.1 christos * (This is the most specific answer the authority has.)
48 1.1 christos * or
49 1.1 christos * node.sourcemask==sourcemask && node.elem!=NULL
50 1.1 christos * (This is the most specific question the client can ask.)
51 1.1 christos *
52 1.1 christos * Insertion needs an address, sourcemask and scope. The length of the
53 1.1 christos * address is capped by min(sourcemask, scope). While traversing the
54 1.1 christos * tree the scope of all visited nodes is updated. This ensures we are
55 1.1 christos * always able to find the most specific answer available.
56 1.1 christos */
57 1.1 christos
58 1.1 christos #ifndef ADDRTREE_H
59 1.1 christos #define ADDRTREE_H
60 1.1 christos
61 1.1 christos typedef uint8_t addrlen_t;
62 1.1 christos typedef uint8_t addrkey_t;
63 1.1 christos #define KEYWIDTH 8
64 1.1 christos
65 1.1 christos struct addrtree {
66 1.1 christos struct addrnode *root;
67 1.1 christos /** Number of elements in the tree (not always equal to number of
68 1.1 christos * nodes) */
69 1.1 christos unsigned int node_count;
70 1.1 christos /** Maximum number of allowed nodes, will be enforced by LRU list.
71 1.1 christos * Excluding the root node, 0 for unlimited */
72 1.1 christos unsigned int max_node_count;
73 1.1 christos /** Size of tree in bytes */
74 1.1 christos size_t size_bytes;
75 1.1 christos /** Maximum prefix length we are willing to cache. */
76 1.1 christos addrlen_t max_depth;
77 1.1 christos /** External function to delete elem. Called as
78 1.1 christos * delfunc(addrnode->elem, addrtree->env) */
79 1.1 christos void (*delfunc)(void *, void *);
80 1.1 christos /** Environment for delfunc */
81 1.1 christos void *env;
82 1.1 christos /** External function returning size of elem. Called as
83 1.1 christos * sizefunc(addrnode->elem) */
84 1.1 christos size_t (*sizefunc)(void *);
85 1.1 christos /** first node in LRU list, first candidate to go */
86 1.1 christos struct addrnode* first;
87 1.1 christos /** last node in LRU list, last candidate to go */
88 1.1 christos struct addrnode *last;
89 1.1 christos };
90 1.1 christos
91 1.1 christos struct addrnode {
92 1.1 christos /** Payload of node, may be NULL */
93 1.1 christos void *elem;
94 1.1 christos /** Abs time in seconds in which elem is meaningful */
95 1.1 christos time_t ttl;
96 1.1 christos /** Number of significant bits in address. */
97 1.1 christos addrlen_t scope;
98 1.1 christos /** A node can have 0-2 edges, set to NULL for unused */
99 1.1 christos struct addredge *edge[2];
100 1.1 christos /** edge between this node and parent */
101 1.1 christos struct addredge *parent_edge;
102 1.1 christos /** previous node in LRU list */
103 1.1 christos struct addrnode *prev;
104 1.1 christos /** next node in LRU list */
105 1.1 christos struct addrnode *next;
106 1.1 christos };
107 1.1 christos
108 1.1 christos struct addredge {
109 1.1 christos /** address of connected node */
110 1.1 christos addrkey_t *str;
111 1.1 christos /** length in bits of str */
112 1.1 christos addrlen_t len;
113 1.1 christos /** child node this edge is connected to */
114 1.1 christos struct addrnode *node;
115 1.1 christos /** Parent node this ege is connected to */
116 1.1 christos struct addrnode *parent_node;
117 1.1 christos /** Index of this edge in parent_node */
118 1.1 christos int parent_index;
119 1.1 christos };
120 1.1 christos
121 1.1 christos /**
122 1.1 christos * Size of tree in bytes.
123 1.1 christos * @param tree: Tree.
124 1.1 christos * @return size of tree in bytes.
125 1.1 christos */
126 1.1 christos size_t addrtree_size(const struct addrtree *tree);
127 1.1 christos
128 1.1 christos /**
129 1.1 christos * Create a new tree.
130 1.1 christos * @param max_depth: Tree will cap keys to this length.
131 1.1 christos * @param delfunc: f(element, env) delete element.
132 1.1 christos * @param sizefunc: f(element) returning the size of element.
133 1.1 christos * @param env: Module environment for alloc information.
134 1.1 christos * @param max_node_count: Maximum size of this data structure in nodes.
135 1.1 christos * 0 for unlimited.
136 1.1 christos * @return new addrtree or NULL on failure.
137 1.1 christos */
138 1.1 christos struct addrtree *
139 1.1 christos addrtree_create(addrlen_t max_depth, void (*delfunc)(void *, void *),
140 1.1 christos size_t (*sizefunc)(void *), void *env, unsigned int max_node_count);
141 1.1 christos
142 1.1 christos /**
143 1.1 christos * Free tree and all nodes below.
144 1.1 christos * @param tree: Tree to be freed.
145 1.1 christos */
146 1.1 christos void addrtree_delete(struct addrtree *tree);
147 1.1 christos
148 1.1 christos /**
149 1.1 christos * Insert an element in the tree. Failures are silent. Sourcemask and
150 1.1 christos * scope might be changed according to local policy. Caller should no
151 1.1 christos * longer access elem, it could be free'd now or later during future
152 1.1 christos * inserts.
153 1.1 christos *
154 1.1 christos * @param tree: Tree insert elem in.
155 1.1 christos * @param addr: key for element lookup.
156 1.1 christos * @param sourcemask: Length of addr in bits.
157 1.1 christos * @param scope: Number of significant bits in addr.
158 1.1 christos * @param elem: data to store in the tree.
159 1.1 christos * @param ttl: elem is valid up to this time, seconds.
160 1.1 christos * @param now: Current time in seconds.
161 1.1 christos */
162 1.1 christos void addrtree_insert(struct addrtree *tree, const addrkey_t *addr,
163 1.1 christos addrlen_t sourcemask, addrlen_t scope, void *elem, time_t ttl,
164 1.1 christos time_t now);
165 1.1 christos
166 1.1 christos /**
167 1.1 christos * Find a node containing an element in the tree.
168 1.1 christos *
169 1.1 christos * @param tree: Tree to search.
170 1.1 christos * @param addr: key for element lookup.
171 1.1 christos * @param sourcemask: Length of addr in bits.
172 1.1 christos * @param now: Current time in seconds.
173 1.1 christos * @return addrnode or NULL on miss.
174 1.1 christos */
175 1.1 christos struct addrnode * addrtree_find(struct addrtree *tree,
176 1.1 christos const addrkey_t *addr, addrlen_t sourcemask, time_t now);
177 1.1 christos
178 1.1 christos /** Wrappers for static functions to unit test */
179 1.1 christos int unittest_wrapper_addrtree_cmpbit(const addrkey_t *key1,
180 1.1 christos const addrkey_t *key2, addrlen_t n);
181 1.1 christos addrlen_t unittest_wrapper_addrtree_bits_common(const addrkey_t *s1,
182 1.1 christos addrlen_t l1, const addrkey_t *s2, addrlen_t l2, addrlen_t skip);
183 1.1 christos int unittest_wrapper_addrtree_getbit(const addrkey_t *addr,
184 1.1 christos addrlen_t addrlen, addrlen_t n);
185 1.1 christos int unittest_wrapper_addrtree_issub(const addrkey_t *s1, addrlen_t l1,
186 1.1 christos const addrkey_t *s2, addrlen_t l2, addrlen_t skip);
187 1.1 christos #endif /* ADDRTREE_H */
188