xref: /openbsd-src/usr.sbin/unbound/iterator/iter_hints.h (revision 2bdc0ed15d5bcb82703283d48e5e85ed47cc34d3)
1933707f3Ssthen /*
2933707f3Ssthen  * iterator/iter_hints.h - iterative resolver module stub and root hints.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, 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 functions to assist the iterator module.
40933707f3Ssthen  * Keep track of stub and root hints, and read those from config.
41933707f3Ssthen  */
42933707f3Ssthen 
43933707f3Ssthen #ifndef ITERATOR_ITER_HINTS_H
44933707f3Ssthen #define ITERATOR_ITER_HINTS_H
45933707f3Ssthen #include "util/storage/dnstree.h"
46*2bdc0ed1Ssthen #include "util/locks.h"
47933707f3Ssthen struct iter_env;
48933707f3Ssthen struct config_file;
49933707f3Ssthen struct delegpt;
50933707f3Ssthen 
51933707f3Ssthen /**
52933707f3Ssthen  * Iterator hints structure
53933707f3Ssthen  */
54933707f3Ssthen struct iter_hints {
55*2bdc0ed1Ssthen 	/** lock on the forwards tree.
56*2bdc0ed1Ssthen 	 * When grabbing both this lock and the anchors.lock, this lock
57*2bdc0ed1Ssthen 	 * is grabbed first. */
58*2bdc0ed1Ssthen 	lock_rw_type lock;
59933707f3Ssthen 	/**
60933707f3Ssthen 	 * Hints are stored in this tree. Sort order is specially chosen.
61933707f3Ssthen 	 * first sorted on qclass. Then on dname in nsec-like order, so that
62933707f3Ssthen 	 * a lookup on class, name will return an exact match or the closest
63933707f3Ssthen 	 * match which gives the ancestor needed.
64933707f3Ssthen 	 * contents of type iter_hints_stub. The class IN root is in here.
65933707f3Ssthen 	 * uses name_tree_node from dnstree.h.
66933707f3Ssthen 	 */
6777079be7Ssthen 	rbtree_type tree;
68933707f3Ssthen };
69933707f3Ssthen 
70933707f3Ssthen /**
71933707f3Ssthen  * Iterator hints for a particular stub.
72933707f3Ssthen  */
73933707f3Ssthen struct iter_hints_stub {
74933707f3Ssthen 	/** tree sorted by name, class */
75933707f3Ssthen 	struct name_tree_node node;
76d8d14d0cSsthen 	/** delegation point with hint information for this stub. malloced. */
77933707f3Ssthen 	struct delegpt* dp;
78933707f3Ssthen 	/** does the stub need to forego priming (like on other ports) */
79933707f3Ssthen 	uint8_t noprime;
80933707f3Ssthen };
81933707f3Ssthen 
82933707f3Ssthen /**
83933707f3Ssthen  * Create hints
84933707f3Ssthen  * @return new hints or NULL on error.
85933707f3Ssthen  */
86933707f3Ssthen struct iter_hints* hints_create(void);
87933707f3Ssthen 
88933707f3Ssthen /**
89933707f3Ssthen  * Delete hints.
90933707f3Ssthen  * @param hints: to delete.
91933707f3Ssthen  */
92933707f3Ssthen void hints_delete(struct iter_hints* hints);
93933707f3Ssthen 
94933707f3Ssthen /**
95933707f3Ssthen  * Process hints config. Sets default values for root hints if no config.
96933707f3Ssthen  * @param hints: where to store.
97933707f3Ssthen  * @param cfg: config options.
98933707f3Ssthen  * @return 0 on error.
99933707f3Ssthen  */
100933707f3Ssthen int hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg);
101933707f3Ssthen 
102933707f3Ssthen /**
103*2bdc0ed1Ssthen  * Find hints for the given class.
104*2bdc0ed1Ssthen  * The return value is contents of the hints structure.
105*2bdc0ed1Ssthen  * Caller should lock and unlock a readlock on the hints structure if nolock
106*2bdc0ed1Ssthen  * is set.
107*2bdc0ed1Ssthen  * Otherwise caller should unlock the readlock on the hints structure if a
108*2bdc0ed1Ssthen  * value was returned.
109933707f3Ssthen  * @param hints: hint storage.
110*2bdc0ed1Ssthen  * @param qname: the qname that generated the delegation point.
111933707f3Ssthen  * @param qclass: class for which root hints are requested. host order.
112*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
113933707f3Ssthen  * @return: NULL if no hints, or a ptr to stored hints.
114933707f3Ssthen  */
115*2bdc0ed1Ssthen struct delegpt* hints_find(struct iter_hints* hints, uint8_t* qname,
116*2bdc0ed1Ssthen 	uint16_t qclass, int nolock);
117*2bdc0ed1Ssthen 
118*2bdc0ed1Ssthen /**
119*2bdc0ed1Ssthen  * Same as hints_lookup, but for the root only.
120*2bdc0ed1Ssthen  * @param hints: hint storage.
121*2bdc0ed1Ssthen  * @param qclass: class for which root hints are requested. host order.
122*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
123*2bdc0ed1Ssthen  * @return: NULL if no hints, or a ptr to stored hints.
124*2bdc0ed1Ssthen  */
125*2bdc0ed1Ssthen struct delegpt* hints_find_root(struct iter_hints* hints,
126*2bdc0ed1Ssthen 	uint16_t qclass, int nolock);
127933707f3Ssthen 
128933707f3Ssthen /**
129933707f3Ssthen  * Find next root hints (to cycle through all root hints).
130*2bdc0ed1Ssthen  * Handles its own locking unless nolock is set. In that case the caller
131*2bdc0ed1Ssthen  * should lock and unlock a readlock on the hints structure.
132933707f3Ssthen  * @param hints: hint storage
133933707f3Ssthen  * @param qclass: class for which root hints are sought.
134933707f3Ssthen  * 	0 means give the first available root hints class.
135933707f3Ssthen  * 	x means, give class x or a higher class if any.
136933707f3Ssthen  * 	returns the found class in this variable.
137*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
138933707f3Ssthen  * @return true if a root hint class is found.
139933707f3Ssthen  * 	false if not root hint class is found (qclass may have been changed).
140933707f3Ssthen  */
141*2bdc0ed1Ssthen int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock);
142933707f3Ssthen 
143933707f3Ssthen /**
144933707f3Ssthen  * Given a qname/qclass combination, and the delegation point from the cache
145933707f3Ssthen  * for this qname/qclass, determine if this combination indicates that a
146933707f3Ssthen  * stub hint exists and must be primed.
147*2bdc0ed1Ssthen  * The return value is contents of the hints structure.
148*2bdc0ed1Ssthen  * Caller should lock and unlock a readlock on the hints structure if nolock
149*2bdc0ed1Ssthen  * is set.
150*2bdc0ed1Ssthen  * Otherwise caller should unlock the readlock on the hints structure if a
151*2bdc0ed1Ssthen  * value was returned.
152933707f3Ssthen  *
153933707f3Ssthen  * @param hints: hint storage.
154933707f3Ssthen  * @param qname: The qname that generated the delegation point.
155933707f3Ssthen  * @param qclass: The qclass that generated the delegation point.
156933707f3Ssthen  * @param dp: The cache generated delegation point.
157*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
158933707f3Ssthen  * @return: A priming delegation point if there is a stub hint that must
159933707f3Ssthen  *         be primed, otherwise null.
160933707f3Ssthen  */
161933707f3Ssthen struct iter_hints_stub* hints_lookup_stub(struct iter_hints* hints,
162*2bdc0ed1Ssthen 	uint8_t* qname, uint16_t qclass, struct delegpt* dp, int nolock);
163933707f3Ssthen 
164933707f3Ssthen /**
165933707f3Ssthen  * Get memory in use by hints
166*2bdc0ed1Ssthen  * Locks and unlocks the structure.
167933707f3Ssthen  * @param hints: hint storage.
168933707f3Ssthen  * @return bytes in use
169933707f3Ssthen  */
170933707f3Ssthen size_t hints_get_mem(struct iter_hints* hints);
171933707f3Ssthen 
172d8d14d0cSsthen /**
173d8d14d0cSsthen  * Add stub to hints structure. For external use since it recalcs
174d8d14d0cSsthen  * the tree parents.
175*2bdc0ed1Ssthen  * Handles its own locking unless nolock is set. In that case the caller
176*2bdc0ed1Ssthen  * should lock and unlock a writelock on the hints structure.
177d8d14d0cSsthen  * @param hints: the hints data structure
178d8d14d0cSsthen  * @param c: class of zone
179d8d14d0cSsthen  * @param dp: delegation point with name and target nameservers for new
180d8d14d0cSsthen  *	hints stub. malloced.
181d8d14d0cSsthen  * @param noprime: set noprime option to true or false on new hint stub.
182*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
183d8d14d0cSsthen  * @return false on failure (out of memory);
184d8d14d0cSsthen  */
185d8d14d0cSsthen int hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
186*2bdc0ed1Ssthen 	int noprime, int nolock);
187d8d14d0cSsthen 
188d8d14d0cSsthen /**
189d8d14d0cSsthen  * Remove stub from hints structure. For external use since it
190d8d14d0cSsthen  * recalcs the tree parents.
191*2bdc0ed1Ssthen  * Handles its own locking unless nolock is set. In that case the caller
192*2bdc0ed1Ssthen  * should lock and unlock a writelock on the hints structure.
193d8d14d0cSsthen  * @param hints: the hints data structure
194d8d14d0cSsthen  * @param c: class of stub zone
195d8d14d0cSsthen  * @param nm: name of stub zone (in uncompressed wireformat).
196*2bdc0ed1Ssthen  * @param nolock: Skip locking, locking is handled by the caller.
197d8d14d0cSsthen  */
198*2bdc0ed1Ssthen void hints_delete_stub(struct iter_hints* hints, uint16_t c,
199*2bdc0ed1Ssthen 	uint8_t* nm, int nolock);
200d8d14d0cSsthen 
201933707f3Ssthen #endif /* ITERATOR_ITER_HINTS_H */
202