xref: /openbsd-src/sbin/unwind/libunbound/iterator/iter_hints.h (revision 096314fef8a8f610abc29edd0a9e7e61b7ff5976)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * iterator/iter_hints.h - iterative resolver module stub and root hints.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains functions to assist the iterator module.
40ae8c6e27Sflorian  * Keep track of stub and root hints, and read those from config.
41ae8c6e27Sflorian  */
42ae8c6e27Sflorian 
43ae8c6e27Sflorian #ifndef ITERATOR_ITER_HINTS_H
44ae8c6e27Sflorian #define ITERATOR_ITER_HINTS_H
45ae8c6e27Sflorian #include "util/storage/dnstree.h"
46*096314feSflorian #include "util/locks.h"
47ae8c6e27Sflorian struct iter_env;
48ae8c6e27Sflorian struct config_file;
49ae8c6e27Sflorian struct delegpt;
50ae8c6e27Sflorian 
51ae8c6e27Sflorian /**
52ae8c6e27Sflorian  * Iterator hints structure
53ae8c6e27Sflorian  */
54ae8c6e27Sflorian struct iter_hints {
55*096314feSflorian 	/** lock on the forwards tree.
56*096314feSflorian 	 * When grabbing both this lock and the anchors.lock, this lock
57*096314feSflorian 	 * is grabbed first. */
58*096314feSflorian 	lock_rw_type lock;
59ae8c6e27Sflorian 	/**
60ae8c6e27Sflorian 	 * Hints are stored in this tree. Sort order is specially chosen.
61ae8c6e27Sflorian 	 * first sorted on qclass. Then on dname in nsec-like order, so that
62ae8c6e27Sflorian 	 * a lookup on class, name will return an exact match or the closest
63ae8c6e27Sflorian 	 * match which gives the ancestor needed.
64ae8c6e27Sflorian 	 * contents of type iter_hints_stub. The class IN root is in here.
65ae8c6e27Sflorian 	 * uses name_tree_node from dnstree.h.
66ae8c6e27Sflorian 	 */
67ae8c6e27Sflorian 	rbtree_type tree;
68ae8c6e27Sflorian };
69ae8c6e27Sflorian 
70ae8c6e27Sflorian /**
71ae8c6e27Sflorian  * Iterator hints for a particular stub.
72ae8c6e27Sflorian  */
73ae8c6e27Sflorian struct iter_hints_stub {
74ae8c6e27Sflorian 	/** tree sorted by name, class */
75ae8c6e27Sflorian 	struct name_tree_node node;
76ae8c6e27Sflorian 	/** delegation point with hint information for this stub. malloced. */
77ae8c6e27Sflorian 	struct delegpt* dp;
78ae8c6e27Sflorian 	/** does the stub need to forego priming (like on other ports) */
79ae8c6e27Sflorian 	uint8_t noprime;
80ae8c6e27Sflorian };
81ae8c6e27Sflorian 
82ae8c6e27Sflorian /**
83ae8c6e27Sflorian  * Create hints
84ae8c6e27Sflorian  * @return new hints or NULL on error.
85ae8c6e27Sflorian  */
86ae8c6e27Sflorian struct iter_hints* hints_create(void);
87ae8c6e27Sflorian 
88ae8c6e27Sflorian /**
89ae8c6e27Sflorian  * Delete hints.
90ae8c6e27Sflorian  * @param hints: to delete.
91ae8c6e27Sflorian  */
92ae8c6e27Sflorian void hints_delete(struct iter_hints* hints);
93ae8c6e27Sflorian 
94ae8c6e27Sflorian /**
95ae8c6e27Sflorian  * Process hints config. Sets default values for root hints if no config.
96ae8c6e27Sflorian  * @param hints: where to store.
97ae8c6e27Sflorian  * @param cfg: config options.
98ae8c6e27Sflorian  * @return 0 on error.
99ae8c6e27Sflorian  */
100ae8c6e27Sflorian int hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg);
101ae8c6e27Sflorian 
102ae8c6e27Sflorian /**
103*096314feSflorian  * Find hints for the given class.
104*096314feSflorian  * The return value is contents of the hints structure.
105*096314feSflorian  * Caller should lock and unlock a readlock on the hints structure if nolock
106*096314feSflorian  * is set.
107*096314feSflorian  * Otherwise caller should unlock the readlock on the hints structure if a
108*096314feSflorian  * value was returned.
109ae8c6e27Sflorian  * @param hints: hint storage.
110*096314feSflorian  * @param qname: the qname that generated the delegation point.
111ae8c6e27Sflorian  * @param qclass: class for which root hints are requested. host order.
112*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
113ae8c6e27Sflorian  * @return: NULL if no hints, or a ptr to stored hints.
114ae8c6e27Sflorian  */
115*096314feSflorian struct delegpt* hints_find(struct iter_hints* hints, uint8_t* qname,
116*096314feSflorian 	uint16_t qclass, int nolock);
117*096314feSflorian 
118*096314feSflorian /**
119*096314feSflorian  * Same as hints_lookup, but for the root only.
120*096314feSflorian  * @param hints: hint storage.
121*096314feSflorian  * @param qclass: class for which root hints are requested. host order.
122*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
123*096314feSflorian  * @return: NULL if no hints, or a ptr to stored hints.
124*096314feSflorian  */
125*096314feSflorian struct delegpt* hints_find_root(struct iter_hints* hints,
126*096314feSflorian 	uint16_t qclass, int nolock);
127ae8c6e27Sflorian 
128ae8c6e27Sflorian /**
129ae8c6e27Sflorian  * Find next root hints (to cycle through all root hints).
130*096314feSflorian  * Handles its own locking unless nolock is set. In that case the caller
131*096314feSflorian  * should lock and unlock a readlock on the hints structure.
132ae8c6e27Sflorian  * @param hints: hint storage
133ae8c6e27Sflorian  * @param qclass: class for which root hints are sought.
134ae8c6e27Sflorian  * 	0 means give the first available root hints class.
135ae8c6e27Sflorian  * 	x means, give class x or a higher class if any.
136ae8c6e27Sflorian  * 	returns the found class in this variable.
137*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
138ae8c6e27Sflorian  * @return true if a root hint class is found.
139ae8c6e27Sflorian  * 	false if not root hint class is found (qclass may have been changed).
140ae8c6e27Sflorian  */
141*096314feSflorian int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock);
142ae8c6e27Sflorian 
143ae8c6e27Sflorian /**
144ae8c6e27Sflorian  * Given a qname/qclass combination, and the delegation point from the cache
145ae8c6e27Sflorian  * for this qname/qclass, determine if this combination indicates that a
146ae8c6e27Sflorian  * stub hint exists and must be primed.
147*096314feSflorian  * The return value is contents of the hints structure.
148*096314feSflorian  * Caller should lock and unlock a readlock on the hints structure if nolock
149*096314feSflorian  * is set.
150*096314feSflorian  * Otherwise caller should unlock the readlock on the hints structure if a
151*096314feSflorian  * value was returned.
152ae8c6e27Sflorian  *
153ae8c6e27Sflorian  * @param hints: hint storage.
154ae8c6e27Sflorian  * @param qname: The qname that generated the delegation point.
155ae8c6e27Sflorian  * @param qclass: The qclass that generated the delegation point.
156ae8c6e27Sflorian  * @param dp: The cache generated delegation point.
157*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
158ae8c6e27Sflorian  * @return: A priming delegation point if there is a stub hint that must
159ae8c6e27Sflorian  *         be primed, otherwise null.
160ae8c6e27Sflorian  */
161ae8c6e27Sflorian struct iter_hints_stub* hints_lookup_stub(struct iter_hints* hints,
162*096314feSflorian 	uint8_t* qname, uint16_t qclass, struct delegpt* dp, int nolock);
163ae8c6e27Sflorian 
164ae8c6e27Sflorian /**
165ae8c6e27Sflorian  * Get memory in use by hints
166*096314feSflorian  * Locks and unlocks the structure.
167ae8c6e27Sflorian  * @param hints: hint storage.
168ae8c6e27Sflorian  * @return bytes in use
169ae8c6e27Sflorian  */
170ae8c6e27Sflorian size_t hints_get_mem(struct iter_hints* hints);
171ae8c6e27Sflorian 
172ae8c6e27Sflorian /**
173ae8c6e27Sflorian  * Add stub to hints structure. For external use since it recalcs
174ae8c6e27Sflorian  * the tree parents.
175*096314feSflorian  * Handles its own locking unless nolock is set. In that case the caller
176*096314feSflorian  * should lock and unlock a writelock on the hints structure.
177ae8c6e27Sflorian  * @param hints: the hints data structure
178ae8c6e27Sflorian  * @param c: class of zone
179ae8c6e27Sflorian  * @param dp: delegation point with name and target nameservers for new
180ae8c6e27Sflorian  *	hints stub. malloced.
181ae8c6e27Sflorian  * @param noprime: set noprime option to true or false on new hint stub.
182*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
183ae8c6e27Sflorian  * @return false on failure (out of memory);
184ae8c6e27Sflorian  */
185ae8c6e27Sflorian int hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
186*096314feSflorian 	int noprime, int nolock);
187ae8c6e27Sflorian 
188ae8c6e27Sflorian /**
189ae8c6e27Sflorian  * Remove stub from hints structure. For external use since it
190ae8c6e27Sflorian  * recalcs the tree parents.
191*096314feSflorian  * Handles its own locking unless nolock is set. In that case the caller
192*096314feSflorian  * should lock and unlock a writelock on the hints structure.
193ae8c6e27Sflorian  * @param hints: the hints data structure
194ae8c6e27Sflorian  * @param c: class of stub zone
195ae8c6e27Sflorian  * @param nm: name of stub zone (in uncompressed wireformat).
196*096314feSflorian  * @param nolock: Skip locking, locking is handled by the caller.
197ae8c6e27Sflorian  */
198*096314feSflorian void hints_delete_stub(struct iter_hints* hints, uint16_t c,
199*096314feSflorian 	uint8_t* nm, int nolock);
200ae8c6e27Sflorian 
201ae8c6e27Sflorian #endif /* ITERATOR_ITER_HINTS_H */
202