1933707f3Ssthen /*
2933707f3Ssthen  * util/storage/dnstree.c - support for rbtree types suitable for DNS code.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2008, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains structures combining types and functions to
40933707f3Ssthen  * manipulate those structures that help building DNS lookup trees.
41933707f3Ssthen  */
42933707f3Ssthen #include "config.h"
43933707f3Ssthen #include "util/storage/dnstree.h"
44933707f3Ssthen #include "util/data/dname.h"
45933707f3Ssthen #include "util/net_help.h"
46933707f3Ssthen 
name_tree_compare(const void * k1,const void * k2)47933707f3Ssthen int name_tree_compare(const void* k1, const void* k2)
48933707f3Ssthen {
49933707f3Ssthen         struct name_tree_node* x = (struct name_tree_node*)k1;
50933707f3Ssthen         struct name_tree_node* y = (struct name_tree_node*)k2;
51933707f3Ssthen         int m;
52933707f3Ssthen         if(x->dclass != y->dclass) {
53933707f3Ssthen                 if(x->dclass < y->dclass)
54933707f3Ssthen                         return -1;
55933707f3Ssthen                 return 1;
56933707f3Ssthen         }
57933707f3Ssthen         return dname_lab_cmp(x->name, x->labs, y->name, y->labs, &m);
58933707f3Ssthen }
59933707f3Ssthen 
addr_tree_compare(const void * k1,const void * k2)60933707f3Ssthen int addr_tree_compare(const void* k1, const void* k2)
61933707f3Ssthen {
62933707f3Ssthen         struct addr_tree_node* n1 = (struct addr_tree_node*)k1;
63933707f3Ssthen         struct addr_tree_node* n2 = (struct addr_tree_node*)k2;
64933707f3Ssthen         int r = sockaddr_cmp_addr(&n1->addr, n1->addrlen, &n2->addr,
65933707f3Ssthen                 n2->addrlen);
66933707f3Ssthen         if(r != 0) return r;
67933707f3Ssthen         if(n1->net < n2->net)
68933707f3Ssthen                 return -1;
69933707f3Ssthen         if(n1->net > n2->net)
70933707f3Ssthen                 return 1;
71933707f3Ssthen         return 0;
72933707f3Ssthen }
73933707f3Ssthen 
addr_tree_addrport_compare(const void * k1,const void * k2)74*45872187Ssthen int addr_tree_addrport_compare(const void* k1, const void* k2)
75*45872187Ssthen {
76*45872187Ssthen 	struct addr_tree_node* n1 = (struct addr_tree_node*)k1;
77*45872187Ssthen 	struct addr_tree_node* n2 = (struct addr_tree_node*)k2;
78*45872187Ssthen 	return sockaddr_cmp(&n1->addr, n1->addrlen, &n2->addr,
79*45872187Ssthen 		n2->addrlen);
80*45872187Ssthen }
81*45872187Ssthen 
name_tree_init(rbtree_type * tree)8277079be7Ssthen void name_tree_init(rbtree_type* tree)
83933707f3Ssthen {
84933707f3Ssthen 	rbtree_init(tree, &name_tree_compare);
85933707f3Ssthen }
86933707f3Ssthen 
addr_tree_init(rbtree_type * tree)8777079be7Ssthen void addr_tree_init(rbtree_type* tree)
88933707f3Ssthen {
89933707f3Ssthen 	rbtree_init(tree, &addr_tree_compare);
90933707f3Ssthen }
91933707f3Ssthen 
addr_tree_addrport_init(rbtree_type * tree)92*45872187Ssthen void addr_tree_addrport_init(rbtree_type* tree)
93*45872187Ssthen {
94*45872187Ssthen 	rbtree_init(tree, &addr_tree_addrport_compare);
95*45872187Ssthen }
96*45872187Ssthen 
name_tree_insert(rbtree_type * tree,struct name_tree_node * node,uint8_t * name,size_t len,int labs,uint16_t dclass)9777079be7Ssthen int name_tree_insert(rbtree_type* tree, struct name_tree_node* node,
98933707f3Ssthen         uint8_t* name, size_t len, int labs, uint16_t dclass)
99933707f3Ssthen {
100933707f3Ssthen 	node->node.key = node;
101933707f3Ssthen 	node->name = name;
102933707f3Ssthen 	node->len = len;
103933707f3Ssthen 	node->labs = labs;
104933707f3Ssthen 	node->dclass = dclass;
105933707f3Ssthen 	node->parent = NULL;
106933707f3Ssthen 	return rbtree_insert(tree, &node->node) != NULL;
107933707f3Ssthen }
108933707f3Ssthen 
addr_tree_insert(rbtree_type * tree,struct addr_tree_node * node,struct sockaddr_storage * addr,socklen_t addrlen,int net)10977079be7Ssthen int addr_tree_insert(rbtree_type* tree, struct addr_tree_node* node,
110933707f3Ssthen         struct sockaddr_storage* addr, socklen_t addrlen, int net)
111933707f3Ssthen {
112933707f3Ssthen 	node->node.key = node;
113933707f3Ssthen 	memcpy(&node->addr, addr, addrlen);
114933707f3Ssthen 	node->addrlen = addrlen;
115933707f3Ssthen 	node->net = net;
116933707f3Ssthen 	node->parent = NULL;
117933707f3Ssthen 	return rbtree_insert(tree, &node->node) != NULL;
118933707f3Ssthen }
119933707f3Ssthen 
addr_tree_init_parents_node(struct addr_tree_node * node)120eaf2578eSsthen void addr_tree_init_parents_node(struct addr_tree_node* node)
121933707f3Ssthen {
122eaf2578eSsthen 	struct addr_tree_node* prev = NULL, *p;
123933707f3Ssthen         int m;
124eaf2578eSsthen 	for(; (rbnode_type*)node != RBTREE_NULL;
125eaf2578eSsthen 		node = (struct addr_tree_node*)rbtree_next((rbnode_type*)node)) {
126933707f3Ssthen                 node->parent = NULL;
127933707f3Ssthen                 if(!prev || prev->addrlen != node->addrlen) {
128933707f3Ssthen                         prev = node;
129933707f3Ssthen                         continue;
130933707f3Ssthen                 }
131933707f3Ssthen                 m = addr_in_common(&prev->addr, prev->net, &node->addr,
132933707f3Ssthen                         node->net, node->addrlen);
133933707f3Ssthen                 /* sort order like: ::/0, 1::/2, 1::/4, ... 2::/2 */
134933707f3Ssthen                 /* find the previous, or parent-parent-parent */
135933707f3Ssthen                 for(p = prev; p; p = p->parent)
136933707f3Ssthen                         if(p->net <= m) {
137933707f3Ssthen                                 /* ==: since prev matched m, this is closest*/
138933707f3Ssthen                                 /* <: prev matches more, but is not a parent,
139933707f3Ssthen 				 * this one is a (grand)parent */
140933707f3Ssthen                                 node->parent = p;
141933707f3Ssthen                                 break;
142933707f3Ssthen                         }
143933707f3Ssthen                 prev = node;
144933707f3Ssthen         }
145933707f3Ssthen }
146933707f3Ssthen 
addr_tree_init_parents(rbtree_type * tree)147eaf2578eSsthen void addr_tree_init_parents(rbtree_type* tree)
148eaf2578eSsthen {
149eaf2578eSsthen 	addr_tree_init_parents_node(
150eaf2578eSsthen 			(struct addr_tree_node*)rbtree_first(tree));
151eaf2578eSsthen }
152eaf2578eSsthen 
name_tree_init_parents(rbtree_type * tree)15377079be7Ssthen void name_tree_init_parents(rbtree_type* tree)
154933707f3Ssthen {
155933707f3Ssthen         struct name_tree_node* node, *prev = NULL, *p;
156933707f3Ssthen         int m;
157933707f3Ssthen         RBTREE_FOR(node, struct name_tree_node*, tree) {
158933707f3Ssthen                 node->parent = NULL;
159933707f3Ssthen                 if(!prev || prev->dclass != node->dclass) {
160933707f3Ssthen                         prev = node;
161933707f3Ssthen                         continue;
162933707f3Ssthen                 }
163933707f3Ssthen                 (void)dname_lab_cmp(prev->name, prev->labs, node->name,
164933707f3Ssthen                         node->labs, &m); /* we know prev is smaller */
165933707f3Ssthen 		/* sort order like: . com. bla.com. zwb.com. net. */
166933707f3Ssthen                 /* find the previous, or parent-parent-parent */
167933707f3Ssthen                 for(p = prev; p; p = p->parent)
168933707f3Ssthen                         if(p->labs <= m) {
169933707f3Ssthen                                 /* ==: since prev matched m, this is closest*/
170933707f3Ssthen                                 /* <: prev matches more, but is not a parent,
171933707f3Ssthen 				 * this one is a (grand)parent */
172933707f3Ssthen                                 node->parent = p;
173933707f3Ssthen                                 break;
174933707f3Ssthen                         }
175933707f3Ssthen                 prev = node;
176933707f3Ssthen         }
177933707f3Ssthen }
178933707f3Ssthen 
name_tree_find(rbtree_type * tree,uint8_t * name,size_t len,int labs,uint16_t dclass)17977079be7Ssthen struct name_tree_node* name_tree_find(rbtree_type* tree, uint8_t* name,
180933707f3Ssthen         size_t len, int labs, uint16_t dclass)
181933707f3Ssthen {
182933707f3Ssthen 	struct name_tree_node key;
183933707f3Ssthen 	key.node.key = &key;
184933707f3Ssthen 	key.name = name;
185933707f3Ssthen 	key.len = len;
186933707f3Ssthen 	key.labs = labs;
187933707f3Ssthen 	key.dclass = dclass;
188933707f3Ssthen 	return (struct name_tree_node*)rbtree_search(tree, &key);
189933707f3Ssthen }
190933707f3Ssthen 
name_tree_lookup(rbtree_type * tree,uint8_t * name,size_t len,int labs,uint16_t dclass)19177079be7Ssthen struct name_tree_node* name_tree_lookup(rbtree_type* tree, uint8_t* name,
192933707f3Ssthen         size_t len, int labs, uint16_t dclass)
193933707f3Ssthen {
19477079be7Ssthen         rbnode_type* res = NULL;
195933707f3Ssthen         struct name_tree_node *result;
196933707f3Ssthen         struct name_tree_node key;
197933707f3Ssthen         key.node.key = &key;
198933707f3Ssthen         key.name = name;
199933707f3Ssthen         key.len = len;
200933707f3Ssthen         key.labs = labs;
201933707f3Ssthen         key.dclass = dclass;
202933707f3Ssthen         if(rbtree_find_less_equal(tree, &key, &res)) {
203933707f3Ssthen                 /* exact */
204933707f3Ssthen                 result = (struct name_tree_node*)res;
205933707f3Ssthen         } else {
206933707f3Ssthen                 /* smaller element (or no element) */
207933707f3Ssthen                 int m;
208933707f3Ssthen                 result = (struct name_tree_node*)res;
209933707f3Ssthen                 if(!result || result->dclass != dclass)
210933707f3Ssthen                         return NULL;
211933707f3Ssthen                 /* count number of labels matched */
212933707f3Ssthen                 (void)dname_lab_cmp(result->name, result->labs, key.name,
213933707f3Ssthen                         key.labs, &m);
214933707f3Ssthen                 while(result) { /* go up until qname is subdomain of stub */
215933707f3Ssthen                         if(result->labs <= m)
216933707f3Ssthen                                 break;
217933707f3Ssthen                         result = result->parent;
218933707f3Ssthen                 }
219933707f3Ssthen         }
220933707f3Ssthen 	return result;
221933707f3Ssthen }
222933707f3Ssthen 
addr_tree_lookup(rbtree_type * tree,struct sockaddr_storage * addr,socklen_t addrlen)22377079be7Ssthen struct addr_tree_node* addr_tree_lookup(rbtree_type* tree,
224933707f3Ssthen         struct sockaddr_storage* addr, socklen_t addrlen)
225933707f3Ssthen {
22677079be7Ssthen         rbnode_type* res = NULL;
227933707f3Ssthen         struct addr_tree_node* result;
228933707f3Ssthen         struct addr_tree_node key;
229933707f3Ssthen         key.node.key = &key;
230933707f3Ssthen         memcpy(&key.addr, addr, addrlen);
231933707f3Ssthen         key.addrlen = addrlen;
232933707f3Ssthen         key.net = (addr_is_ip6(addr, addrlen)?128:32);
233933707f3Ssthen         if(rbtree_find_less_equal(tree, &key, &res)) {
234933707f3Ssthen                 /* exact */
235933707f3Ssthen                 return (struct addr_tree_node*)res;
236933707f3Ssthen         } else {
237933707f3Ssthen                 /* smaller element (or no element) */
238933707f3Ssthen                 int m;
239933707f3Ssthen                 result = (struct addr_tree_node*)res;
240933707f3Ssthen                 if(!result || result->addrlen != addrlen)
241933707f3Ssthen                         return 0;
242933707f3Ssthen                 /* count number of bits matched */
243933707f3Ssthen                 m = addr_in_common(&result->addr, result->net, addr,
244933707f3Ssthen                         key.net, addrlen);
245933707f3Ssthen                 while(result) { /* go up until addr is inside netblock */
246933707f3Ssthen                         if(result->net <= m)
247933707f3Ssthen                                 break;
248933707f3Ssthen                         result = result->parent;
249933707f3Ssthen                 }
250933707f3Ssthen         }
251933707f3Ssthen         return result;
252933707f3Ssthen }
253933707f3Ssthen 
addr_tree_find(rbtree_type * tree,struct sockaddr_storage * addr,socklen_t addrlen,int net)25477079be7Ssthen struct addr_tree_node* addr_tree_find(rbtree_type* tree,
25577079be7Ssthen         struct sockaddr_storage* addr, socklen_t addrlen, int net)
25677079be7Ssthen {
25777079be7Ssthen         rbnode_type* res = NULL;
25877079be7Ssthen         struct addr_tree_node key;
25977079be7Ssthen         key.node.key = &key;
26077079be7Ssthen         memcpy(&key.addr, addr, addrlen);
26177079be7Ssthen         key.addrlen = addrlen;
26277079be7Ssthen         key.net = net;
26377079be7Ssthen 	res = rbtree_search(tree, &key);
26477079be7Ssthen 	return (struct addr_tree_node*)res;
26577079be7Ssthen }
26677079be7Ssthen 
267933707f3Ssthen int
name_tree_next_root(rbtree_type * tree,uint16_t * dclass)26877079be7Ssthen name_tree_next_root(rbtree_type* tree, uint16_t* dclass)
269933707f3Ssthen {
270933707f3Ssthen 	struct name_tree_node key;
27177079be7Ssthen 	rbnode_type* n;
272933707f3Ssthen 	struct name_tree_node* p;
273933707f3Ssthen 	if(*dclass == 0) {
274933707f3Ssthen 		/* first root item is first item in tree */
275933707f3Ssthen 		n = rbtree_first(tree);
276933707f3Ssthen 		if(n == RBTREE_NULL)
277933707f3Ssthen 			return 0;
278933707f3Ssthen 		p = (struct name_tree_node*)n;
279933707f3Ssthen 		if(dname_is_root(p->name)) {
280933707f3Ssthen 			*dclass = p->dclass;
281933707f3Ssthen 			return 1;
282933707f3Ssthen 		}
283933707f3Ssthen 		/* root not first item? search for higher items */
284933707f3Ssthen 		*dclass = p->dclass + 1;
285933707f3Ssthen 		return name_tree_next_root(tree, dclass);
286933707f3Ssthen 	}
287933707f3Ssthen 	/* find class n in tree, we may get a direct hit, or if we don't
288933707f3Ssthen 	 * this is the last item of the previous class so rbtree_next() takes
289933707f3Ssthen 	 * us to the next root (if any) */
290933707f3Ssthen 	key.node.key = &key;
291933707f3Ssthen 	key.name = (uint8_t*)"\000";
292933707f3Ssthen 	key.len = 1;
293933707f3Ssthen 	key.labs = 0;
294933707f3Ssthen 	key.dclass = *dclass;
295933707f3Ssthen 	n = NULL;
296933707f3Ssthen 	if(rbtree_find_less_equal(tree, &key, &n)) {
297933707f3Ssthen 		/* exact */
298933707f3Ssthen 		return 1;
299933707f3Ssthen 	} else {
300933707f3Ssthen 		/* smaller element */
301933707f3Ssthen 		if(!n || n == RBTREE_NULL)
302933707f3Ssthen 			return 0; /* nothing found */
303933707f3Ssthen 		n = rbtree_next(n);
304933707f3Ssthen 		if(n == RBTREE_NULL)
305933707f3Ssthen 			return 0; /* no higher */
306933707f3Ssthen 		p = (struct name_tree_node*)n;
307933707f3Ssthen 		if(dname_is_root(p->name)) {
308933707f3Ssthen 			*dclass = p->dclass;
309933707f3Ssthen 			return 1;
310933707f3Ssthen 		}
311933707f3Ssthen 		/* not a root node, return next higher item */
312933707f3Ssthen 		*dclass = p->dclass+1;
313933707f3Ssthen 		return name_tree_next_root(tree, dclass);
314933707f3Ssthen 	}
315933707f3Ssthen }
316