xref: /freebsd/contrib/unbound/iterator/iter_hints.h (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * iterator/iter_hints.h - iterative resolver module stub and root hints.
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 stub and root hints, and read those from config.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef ITERATOR_ITER_HINTS_H
44b7579f77SDag-Erling Smørgrav #define ITERATOR_ITER_HINTS_H
45b7579f77SDag-Erling Smørgrav #include "util/storage/dnstree.h"
46335c7cdaSCy Schubert #include "util/locks.h"
47b7579f77SDag-Erling Smørgrav struct iter_env;
48b7579f77SDag-Erling Smørgrav struct config_file;
49b7579f77SDag-Erling Smørgrav struct delegpt;
50b7579f77SDag-Erling Smørgrav 
51b7579f77SDag-Erling Smørgrav /**
52b7579f77SDag-Erling Smørgrav  * Iterator hints structure
53b7579f77SDag-Erling Smørgrav  */
54b7579f77SDag-Erling Smørgrav struct iter_hints {
55335c7cdaSCy Schubert 	/** lock on the forwards tree.
56335c7cdaSCy Schubert 	 * When grabbing both this lock and the anchors.lock, this lock
57335c7cdaSCy Schubert 	 * is grabbed first. */
58335c7cdaSCy Schubert 	lock_rw_type lock;
59b7579f77SDag-Erling Smørgrav 	/**
60b7579f77SDag-Erling Smørgrav 	 * Hints 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_hints_stub. The class IN root is in here.
65b7579f77SDag-Erling Smørgrav 	 * uses name_tree_node from dnstree.h.
66b7579f77SDag-Erling Smørgrav 	 */
673005e0a3SDag-Erling Smørgrav 	rbtree_type tree;
68b7579f77SDag-Erling Smørgrav };
69b7579f77SDag-Erling Smørgrav 
70b7579f77SDag-Erling Smørgrav /**
71b7579f77SDag-Erling Smørgrav  * Iterator hints for a particular stub.
72b7579f77SDag-Erling Smørgrav  */
73b7579f77SDag-Erling Smørgrav struct iter_hints_stub {
74b7579f77SDag-Erling Smørgrav 	/** tree sorted by name, class */
75b7579f77SDag-Erling Smørgrav 	struct name_tree_node node;
76b7579f77SDag-Erling Smørgrav 	/** delegation point with hint information for this stub. malloced. */
77b7579f77SDag-Erling Smørgrav 	struct delegpt* dp;
78b7579f77SDag-Erling Smørgrav 	/** does the stub need to forego priming (like on other ports) */
79b7579f77SDag-Erling Smørgrav 	uint8_t noprime;
80b7579f77SDag-Erling Smørgrav };
81b7579f77SDag-Erling Smørgrav 
82b7579f77SDag-Erling Smørgrav /**
83b7579f77SDag-Erling Smørgrav  * Create hints
84b7579f77SDag-Erling Smørgrav  * @return new hints or NULL on error.
85b7579f77SDag-Erling Smørgrav  */
86b7579f77SDag-Erling Smørgrav struct iter_hints* hints_create(void);
87b7579f77SDag-Erling Smørgrav 
88b7579f77SDag-Erling Smørgrav /**
89b7579f77SDag-Erling Smørgrav  * Delete hints.
90b7579f77SDag-Erling Smørgrav  * @param hints: to delete.
91b7579f77SDag-Erling Smørgrav  */
92b7579f77SDag-Erling Smørgrav void hints_delete(struct iter_hints* hints);
93b7579f77SDag-Erling Smørgrav 
94b7579f77SDag-Erling Smørgrav /**
95b7579f77SDag-Erling Smørgrav  * Process hints config. Sets default values for root hints if no config.
96b7579f77SDag-Erling Smørgrav  * @param hints: where to store.
97b7579f77SDag-Erling Smørgrav  * @param cfg: config options.
98b7579f77SDag-Erling Smørgrav  * @return 0 on error.
99b7579f77SDag-Erling Smørgrav  */
100b7579f77SDag-Erling Smørgrav int hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg);
101b7579f77SDag-Erling Smørgrav 
102b7579f77SDag-Erling Smørgrav /**
103335c7cdaSCy Schubert  * Find hints for the given class.
104335c7cdaSCy Schubert  * The return value is contents of the hints structure.
105335c7cdaSCy Schubert  * Caller should lock and unlock a readlock on the hints structure if nolock
106335c7cdaSCy Schubert  * is set.
107335c7cdaSCy Schubert  * Otherwise caller should unlock the readlock on the hints structure if a
108335c7cdaSCy Schubert  * value was returned.
109b7579f77SDag-Erling Smørgrav  * @param hints: hint storage.
110335c7cdaSCy Schubert  * @param qname: the qname that generated the delegation point.
111b7579f77SDag-Erling Smørgrav  * @param qclass: class for which root hints are requested. host order.
112335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
113b7579f77SDag-Erling Smørgrav  * @return: NULL if no hints, or a ptr to stored hints.
114b7579f77SDag-Erling Smørgrav  */
115335c7cdaSCy Schubert struct delegpt* hints_find(struct iter_hints* hints, uint8_t* qname,
116335c7cdaSCy Schubert 	uint16_t qclass, int nolock);
117335c7cdaSCy Schubert 
118335c7cdaSCy Schubert /**
119335c7cdaSCy Schubert  * Same as hints_lookup, but for the root only.
120335c7cdaSCy Schubert  * @param hints: hint storage.
121335c7cdaSCy Schubert  * @param qclass: class for which root hints are requested. host order.
122335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
123335c7cdaSCy Schubert  * @return: NULL if no hints, or a ptr to stored hints.
124335c7cdaSCy Schubert  */
125335c7cdaSCy Schubert struct delegpt* hints_find_root(struct iter_hints* hints,
126335c7cdaSCy Schubert 	uint16_t qclass, int nolock);
127b7579f77SDag-Erling Smørgrav 
128b7579f77SDag-Erling Smørgrav /**
129b7579f77SDag-Erling Smørgrav  * Find next root hints (to cycle through all root hints).
130335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
131335c7cdaSCy Schubert  * should lock and unlock a readlock on the hints structure.
132b7579f77SDag-Erling Smørgrav  * @param hints: hint storage
133b7579f77SDag-Erling Smørgrav  * @param qclass: class for which root hints are sought.
134b7579f77SDag-Erling Smørgrav  * 	0 means give the first available root hints class.
135b7579f77SDag-Erling Smørgrav  * 	x means, give class x or a higher class if any.
136b7579f77SDag-Erling Smørgrav  * 	returns the found class in this variable.
137335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
138b7579f77SDag-Erling Smørgrav  * @return true if a root hint class is found.
139b7579f77SDag-Erling Smørgrav  * 	false if not root hint class is found (qclass may have been changed).
140b7579f77SDag-Erling Smørgrav  */
141335c7cdaSCy Schubert int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock);
142b7579f77SDag-Erling Smørgrav 
143b7579f77SDag-Erling Smørgrav /**
144b7579f77SDag-Erling Smørgrav  * Given a qname/qclass combination, and the delegation point from the cache
145b7579f77SDag-Erling Smørgrav  * for this qname/qclass, determine if this combination indicates that a
146b7579f77SDag-Erling Smørgrav  * stub hint exists and must be primed.
147335c7cdaSCy Schubert  * The return value is contents of the hints structure.
148335c7cdaSCy Schubert  * Caller should lock and unlock a readlock on the hints structure if nolock
149335c7cdaSCy Schubert  * is set.
150335c7cdaSCy Schubert  * Otherwise caller should unlock the readlock on the hints structure if a
151335c7cdaSCy Schubert  * value was returned.
152b7579f77SDag-Erling Smørgrav  *
153b7579f77SDag-Erling Smørgrav  * @param hints: hint storage.
154b7579f77SDag-Erling Smørgrav  * @param qname: The qname that generated the delegation point.
155b7579f77SDag-Erling Smørgrav  * @param qclass: The qclass that generated the delegation point.
156b7579f77SDag-Erling Smørgrav  * @param dp: The cache generated delegation point.
157335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
158b7579f77SDag-Erling Smørgrav  * @return: A priming delegation point if there is a stub hint that must
159b7579f77SDag-Erling Smørgrav  *         be primed, otherwise null.
160b7579f77SDag-Erling Smørgrav  */
161b7579f77SDag-Erling Smørgrav struct iter_hints_stub* hints_lookup_stub(struct iter_hints* hints,
162335c7cdaSCy Schubert 	uint8_t* qname, uint16_t qclass, struct delegpt* dp, int nolock);
163b7579f77SDag-Erling Smørgrav 
164b7579f77SDag-Erling Smørgrav /**
165b7579f77SDag-Erling Smørgrav  * Get memory in use by hints
166335c7cdaSCy Schubert  * Locks and unlocks the structure.
167b7579f77SDag-Erling Smørgrav  * @param hints: hint storage.
168b7579f77SDag-Erling Smørgrav  * @return bytes in use
169b7579f77SDag-Erling Smørgrav  */
170b7579f77SDag-Erling Smørgrav size_t hints_get_mem(struct iter_hints* hints);
171b7579f77SDag-Erling Smørgrav 
172b7579f77SDag-Erling Smørgrav /**
173b7579f77SDag-Erling Smørgrav  * Add stub to hints structure. For external use since it recalcs
174b7579f77SDag-Erling Smørgrav  * the tree parents.
175335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
176335c7cdaSCy Schubert  * should lock and unlock a writelock on the hints structure.
177b7579f77SDag-Erling Smørgrav  * @param hints: the hints data structure
178b7579f77SDag-Erling Smørgrav  * @param c: class of zone
179b7579f77SDag-Erling Smørgrav  * @param dp: delegation point with name and target nameservers for new
180b7579f77SDag-Erling Smørgrav  *	hints stub. malloced.
181b7579f77SDag-Erling Smørgrav  * @param noprime: set noprime option to true or false on new hint stub.
182335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
183b7579f77SDag-Erling Smørgrav  * @return false on failure (out of memory);
184b7579f77SDag-Erling Smørgrav  */
185b7579f77SDag-Erling Smørgrav int hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
186335c7cdaSCy Schubert 	int noprime, int nolock);
187b7579f77SDag-Erling Smørgrav 
188b7579f77SDag-Erling Smørgrav /**
189b7579f77SDag-Erling Smørgrav  * Remove stub from hints structure. For external use since it
190b7579f77SDag-Erling Smørgrav  * recalcs the tree parents.
191335c7cdaSCy Schubert  * Handles its own locking unless nolock is set. In that case the caller
192335c7cdaSCy Schubert  * should lock and unlock a writelock on the hints structure.
193b7579f77SDag-Erling Smørgrav  * @param hints: the hints data structure
194b7579f77SDag-Erling Smørgrav  * @param c: class of stub zone
195b7579f77SDag-Erling Smørgrav  * @param nm: name of stub zone (in uncompressed wireformat).
196335c7cdaSCy Schubert  * @param nolock: Skip locking, locking is handled by the caller.
197b7579f77SDag-Erling Smørgrav  */
198335c7cdaSCy Schubert void hints_delete_stub(struct iter_hints* hints, uint16_t c,
199335c7cdaSCy Schubert 	uint8_t* nm, int nolock);
200b7579f77SDag-Erling Smørgrav 
201*be771a7bSCy Schubert /**
202*be771a7bSCy Schubert  * Swap internal tree with preallocated entries. Caller should manage
203*be771a7bSCy Schubert  * the locks.
204*be771a7bSCy Schubert  * @param hints: the hints data structure.
205*be771a7bSCy Schubert  * @param data: the data structure used to take elements from. This contains
206*be771a7bSCy Schubert  * 	the old elements on return.
207*be771a7bSCy Schubert  */
208*be771a7bSCy Schubert void hints_swap_tree(struct iter_hints* hints, struct iter_hints* data);
209*be771a7bSCy Schubert 
210b7579f77SDag-Erling Smørgrav #endif /* ITERATOR_ITER_HINTS_H */
211