xref: /freebsd/contrib/unbound/iterator/iter_fwd.h (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * iterator/iter_fwd.h - iterative resolver module forward zones.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains functions to assist the iterator module.
40b7579f77SDag-Erling Smørgrav  * Keep track of forward zones, and read those from config.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef ITERATOR_ITER_FWD_H
44b7579f77SDag-Erling Smørgrav #define ITERATOR_ITER_FWD_H
45b7579f77SDag-Erling Smørgrav #include "util/rbtree.h"
46335c7cdaSCy Schubert #include "util/locks.h"
47b7579f77SDag-Erling Smørgrav struct config_file;
48b7579f77SDag-Erling Smørgrav struct delegpt;
49b7579f77SDag-Erling Smørgrav 
50b7579f77SDag-Erling Smørgrav /**
51b7579f77SDag-Erling Smørgrav  * Iterator forward zones structure
52b7579f77SDag-Erling Smørgrav  */
53b7579f77SDag-Erling Smørgrav struct iter_forwards {
54335c7cdaSCy Schubert 	/** lock on the forwards tree.
55335c7cdaSCy Schubert 	 * When grabbing both this lock and the anchors.lock, this lock
56335c7cdaSCy Schubert 	 * is grabbed first. When grabbing both this lock and the hints.lock
57335c7cdaSCy Schubert 	 * this lock is grabbed first. */
58335c7cdaSCy Schubert 	lock_rw_type lock;
59b7579f77SDag-Erling Smørgrav 	/**
60b7579f77SDag-Erling Smørgrav 	 * Zones are stored in this tree. Sort order is specially chosen.
61b7579f77SDag-Erling Smørgrav 	 * first sorted on qclass. Then on dname in nsec-like order, so that
62b7579f77SDag-Erling Smørgrav 	 * a lookup on class, name will return an exact match or the closest
63b7579f77SDag-Erling Smørgrav 	 * match which gives the ancestor needed.
64b7579f77SDag-Erling Smørgrav 	 * contents of type iter_forward_zone.
65b7579f77SDag-Erling Smørgrav 	 */
663005e0a3SDag-Erling Smørgrav 	rbtree_type* tree;
67b7579f77SDag-Erling Smørgrav };
68b7579f77SDag-Erling Smørgrav 
69b7579f77SDag-Erling Smørgrav /**
70b7579f77SDag-Erling Smørgrav  * Iterator forward servers for a particular zone.
71b7579f77SDag-Erling Smørgrav  */
72b7579f77SDag-Erling Smørgrav struct iter_forward_zone {
73b7579f77SDag-Erling Smørgrav 	/** redblacktree node, key is this structure: class and name */
743005e0a3SDag-Erling Smørgrav 	rbnode_type node;
75b7579f77SDag-Erling Smørgrav 	/** name */
76b7579f77SDag-Erling Smørgrav 	uint8_t* name;
77b7579f77SDag-Erling Smørgrav 	/** length of name */
78b7579f77SDag-Erling Smørgrav 	size_t namelen;
79b7579f77SDag-Erling Smørgrav 	/** number of labels in name */
80b7579f77SDag-Erling Smørgrav 	int namelabs;
81b7579f77SDag-Erling Smørgrav 	/** delegation point with forward server information for this zone.
82b7579f77SDag-Erling Smørgrav 	 * If NULL then this forward entry is used to indicate that a
83b7579f77SDag-Erling Smørgrav 	 * stub-zone with the same name exists, and should be used.
84b7579f77SDag-Erling Smørgrav 	 * This delegation point is malloced.
85b7579f77SDag-Erling Smørgrav 	 */
86b7579f77SDag-Erling Smørgrav 	struct delegpt* dp;
87b7579f77SDag-Erling Smørgrav 	/** pointer to parent in tree (or NULL if none) */
88b7579f77SDag-Erling Smørgrav 	struct iter_forward_zone* parent;
89b7579f77SDag-Erling Smørgrav 	/** class. host order. */
90b7579f77SDag-Erling Smørgrav 	uint16_t dclass;
91b7579f77SDag-Erling Smørgrav };
92b7579f77SDag-Erling Smørgrav 
93b7579f77SDag-Erling Smørgrav /**
94b7579f77SDag-Erling Smørgrav  * Create forwards
95b7579f77SDag-Erling Smørgrav  * @return new forwards or NULL on error.
96b7579f77SDag-Erling Smørgrav  */
97b7579f77SDag-Erling Smørgrav struct iter_forwards* forwards_create(void);
98b7579f77SDag-Erling Smørgrav 
99b7579f77SDag-Erling Smørgrav /**
100b7579f77SDag-Erling Smørgrav  * Delete forwards.
101b7579f77SDag-Erling Smørgrav  * @param fwd: to delete.
102b7579f77SDag-Erling Smørgrav  */
103b7579f77SDag-Erling Smørgrav void forwards_delete(struct iter_forwards* fwd);
104b7579f77SDag-Erling Smørgrav 
105b7579f77SDag-Erling Smørgrav /**
106b7579f77SDag-Erling Smørgrav  * Process forwards config.
107b7579f77SDag-Erling Smørgrav  * @param fwd: where to store.
108b7579f77SDag-Erling Smørgrav  * @param cfg: config options.
109b7579f77SDag-Erling Smørgrav  * @return 0 on error.
110b7579f77SDag-Erling Smørgrav  */
111b7579f77SDag-Erling Smørgrav int forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg);
112b7579f77SDag-Erling Smørgrav 
113b7579f77SDag-Erling Smørgrav /**
11417d15b25SDag-Erling Smørgrav  * Find forward zone exactly by name
115335c7cdaSCy Schubert  * The return value is contents of the forwards structure.
116335c7cdaSCy Schubert  * Caller should lock and unlock a readlock on the forwards structure if nolock
117335c7cdaSCy Schubert  * is set.
118335c7cdaSCy Schubert  * Otherwise caller should unlock the readlock on the forwards structure if a
119335c7cdaSCy Schubert  * value was returned.
12017d15b25SDag-Erling Smørgrav  * @param fwd: forward storage.
12117d15b25SDag-Erling Smørgrav  * @param qname: The qname of the query.
12217d15b25SDag-Erling Smørgrav  * @param qclass: The qclass of the query.
123335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
12417d15b25SDag-Erling Smørgrav  * @return: A delegation point or null.
12517d15b25SDag-Erling Smørgrav  */
12617d15b25SDag-Erling Smørgrav struct delegpt* forwards_find(struct iter_forwards* fwd, uint8_t* qname,
127335c7cdaSCy Schubert 	uint16_t qclass, int nolock);
12817d15b25SDag-Erling Smørgrav 
12917d15b25SDag-Erling Smørgrav /**
130b7579f77SDag-Erling Smørgrav  * Find forward zone information
131b7579f77SDag-Erling Smørgrav  * For this qname/qclass find forward zone information, returns delegation
132b7579f77SDag-Erling Smørgrav  * point with server names and addresses, or NULL if no forwarding is needed.
133335c7cdaSCy Schubert  * The return value is contents of the forwards structure.
134335c7cdaSCy Schubert  * Caller should lock and unlock a readlock on the forwards structure if nolock
135335c7cdaSCy Schubert  * is set.
136335c7cdaSCy Schubert  * Otherwise caller should unlock the readlock on the forwards structure if a
137335c7cdaSCy Schubert  * value was returned.
138b7579f77SDag-Erling Smørgrav  *
139b7579f77SDag-Erling Smørgrav  * @param fwd: forward storage.
140b7579f77SDag-Erling Smørgrav  * @param qname: The qname of the query.
141b7579f77SDag-Erling Smørgrav  * @param qclass: The qclass of the query.
142335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
143b7579f77SDag-Erling Smørgrav  * @return: A delegation point if the query has to be forwarded to that list,
144b7579f77SDag-Erling Smørgrav  *         otherwise null.
145b7579f77SDag-Erling Smørgrav  */
146b7579f77SDag-Erling Smørgrav struct delegpt* forwards_lookup(struct iter_forwards* fwd,
147335c7cdaSCy Schubert 	uint8_t* qname, uint16_t qclass, int nolock);
148b7579f77SDag-Erling Smørgrav 
149b7579f77SDag-Erling Smørgrav /**
150b7579f77SDag-Erling Smørgrav  * Same as forwards_lookup, but for the root only
151b7579f77SDag-Erling Smørgrav  * @param fwd: forward storage.
152b7579f77SDag-Erling Smørgrav  * @param qclass: The qclass of the query.
153335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
154b7579f77SDag-Erling Smørgrav  * @return: A delegation point if root forward exists, otherwise null.
155b7579f77SDag-Erling Smørgrav  */
156b7579f77SDag-Erling Smørgrav struct delegpt* forwards_lookup_root(struct iter_forwards* fwd,
157335c7cdaSCy Schubert 	uint16_t qclass, int nolock);
158b7579f77SDag-Erling Smørgrav 
159b7579f77SDag-Erling Smørgrav /**
160b7579f77SDag-Erling Smørgrav  * Find next root item in forwards lookup tree.
161335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
162335c7cdaSCy Schubert  * should lock and unlock a readlock on the forwards structure.
163b7579f77SDag-Erling Smørgrav  * @param fwd: the forward storage
164b7579f77SDag-Erling Smørgrav  * @param qclass: class to look at next, or higher.
165335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
166b7579f77SDag-Erling Smørgrav  * @return false if none found, or if true stored in qclass.
167b7579f77SDag-Erling Smørgrav  */
168335c7cdaSCy Schubert int forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass,
169335c7cdaSCy Schubert 	int nolock);
170b7579f77SDag-Erling Smørgrav 
171b7579f77SDag-Erling Smørgrav /**
172b7579f77SDag-Erling Smørgrav  * Get memory in use by forward storage
173335c7cdaSCy Schubert  * Locks and unlocks the structure.
174b7579f77SDag-Erling Smørgrav  * @param fwd: forward storage.
175b7579f77SDag-Erling Smørgrav  * @return bytes in use
176b7579f77SDag-Erling Smørgrav  */
177b7579f77SDag-Erling Smørgrav size_t forwards_get_mem(struct iter_forwards* fwd);
178b7579f77SDag-Erling Smørgrav 
179b7579f77SDag-Erling Smørgrav /** compare two fwd entries */
180b7579f77SDag-Erling Smørgrav int fwd_cmp(const void* k1, const void* k2);
181b7579f77SDag-Erling Smørgrav 
182b7579f77SDag-Erling Smørgrav /**
183b7579f77SDag-Erling Smørgrav  * Add zone to forward structure. For external use since it recalcs
184b7579f77SDag-Erling Smørgrav  * the tree parents.
185335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
186335c7cdaSCy Schubert  * should lock and unlock a writelock on the forwards structure.
187b7579f77SDag-Erling Smørgrav  * @param fwd: the forward data structure
188b7579f77SDag-Erling Smørgrav  * @param c: class of zone
189b7579f77SDag-Erling Smørgrav  * @param dp: delegation point with name and target nameservers for new
190b7579f77SDag-Erling Smørgrav  *	forward zone. malloced.
191335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
192b7579f77SDag-Erling Smørgrav  * @return false on failure (out of memory);
193b7579f77SDag-Erling Smørgrav  */
194b7579f77SDag-Erling Smørgrav int forwards_add_zone(struct iter_forwards* fwd, uint16_t c,
195335c7cdaSCy Schubert 	struct delegpt* dp, int nolock);
196b7579f77SDag-Erling Smørgrav 
197b7579f77SDag-Erling Smørgrav /**
198b7579f77SDag-Erling Smørgrav  * Remove zone from forward structure. For external use since it
199b7579f77SDag-Erling Smørgrav  * recalcs the tree parents.
200335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
201335c7cdaSCy Schubert  * should lock and unlock a writelock on the forwards structure.
202b7579f77SDag-Erling Smørgrav  * @param fwd: the forward data structure
203b7579f77SDag-Erling Smørgrav  * @param c: class of zone
204b7579f77SDag-Erling Smørgrav  * @param nm: name of zone (in uncompressed wireformat).
205335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
206b7579f77SDag-Erling Smørgrav  */
207335c7cdaSCy Schubert void forwards_delete_zone(struct iter_forwards* fwd, uint16_t c,
208335c7cdaSCy Schubert 	uint8_t* nm, int nolock);
209b7579f77SDag-Erling Smørgrav 
210b7579f77SDag-Erling Smørgrav /**
211b7579f77SDag-Erling Smørgrav  * Add stub hole (empty entry in forward table, that makes resolution skip
212b7579f77SDag-Erling Smørgrav  * a forward-zone because the stub zone should override the forward zone).
213b7579f77SDag-Erling Smørgrav  * Does not add one if not necessary.
214335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
215335c7cdaSCy Schubert  * should lock and unlock a writelock on the forwards structure.
216b7579f77SDag-Erling Smørgrav  * @param fwd: the forward data structure
217b7579f77SDag-Erling Smørgrav  * @param c: class of zone
218b7579f77SDag-Erling Smørgrav  * @param nm: name of zone (in uncompressed wireformat).
219335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
220b7579f77SDag-Erling Smørgrav  * @return false on failure (out of memory);
221b7579f77SDag-Erling Smørgrav  */
222335c7cdaSCy Schubert int forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c,
223335c7cdaSCy Schubert 	uint8_t* nm, int nolock);
224b7579f77SDag-Erling Smørgrav 
225b7579f77SDag-Erling Smørgrav /**
226b7579f77SDag-Erling Smørgrav  * Remove stub hole, if one exists.
227335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
228335c7cdaSCy Schubert  * should lock and unlock a writelock on the forwards structure.
229b7579f77SDag-Erling Smørgrav  * @param fwd: the forward data structure
230b7579f77SDag-Erling Smørgrav  * @param c: class of zone
231b7579f77SDag-Erling Smørgrav  * @param nm: name of zone (in uncompressed wireformat).
232335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
233b7579f77SDag-Erling Smørgrav  */
234b7579f77SDag-Erling Smørgrav void forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c,
235335c7cdaSCy Schubert 	uint8_t* nm, int nolock);
236b7579f77SDag-Erling Smørgrav 
237*be771a7bSCy Schubert /**
238*be771a7bSCy Schubert  * Swap internal tree with preallocated entries. Caller should manage
239*be771a7bSCy Schubert  * the locks.
240*be771a7bSCy Schubert  * @param fwd: the forward data structure.
241*be771a7bSCy Schubert  * @param data: the data structure used to take elements from. This contains
242*be771a7bSCy Schubert  * 	the old elements on return.
243*be771a7bSCy Schubert  */
244*be771a7bSCy Schubert void forwards_swap_tree(struct iter_forwards* fwd, struct iter_forwards* data);
245*be771a7bSCy Schubert 
246b7579f77SDag-Erling Smørgrav #endif /* ITERATOR_ITER_FWD_H */
247