xref: /freebsd/contrib/unbound/iterator/iter_fwd.h (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*
2  * iterator/iter_fwd.h - iterative resolver module forward zones.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to assist the iterator module.
40  * Keep track of forward zones, and read those from config.
41  */
42 
43 #ifndef ITERATOR_ITER_FWD_H
44 #define ITERATOR_ITER_FWD_H
45 #include "util/rbtree.h"
46 struct config_file;
47 struct delegpt;
48 
49 /**
50  * Iterator forward zones structure
51  */
52 struct iter_forwards {
53 	/**
54 	 * Zones are stored in this tree. Sort order is specially chosen.
55 	 * first sorted on qclass. Then on dname in nsec-like order, so that
56 	 * a lookup on class, name will return an exact match or the closest
57 	 * match which gives the ancestor needed.
58 	 * contents of type iter_forward_zone.
59 	 */
60 	rbtree_type* tree;
61 };
62 
63 /**
64  * Iterator forward servers for a particular zone.
65  */
66 struct iter_forward_zone {
67 	/** redblacktree node, key is this structure: class and name */
68 	rbnode_type node;
69 	/** name */
70 	uint8_t* name;
71 	/** length of name */
72 	size_t namelen;
73 	/** number of labels in name */
74 	int namelabs;
75 	/** delegation point with forward server information for this zone.
76 	 * If NULL then this forward entry is used to indicate that a
77 	 * stub-zone with the same name exists, and should be used.
78 	 * This delegation point is malloced.
79 	 */
80 	struct delegpt* dp;
81 	/** pointer to parent in tree (or NULL if none) */
82 	struct iter_forward_zone* parent;
83 	/** class. host order. */
84 	uint16_t dclass;
85 };
86 
87 /**
88  * Create forwards
89  * @return new forwards or NULL on error.
90  */
91 struct iter_forwards* forwards_create(void);
92 
93 /**
94  * Delete forwards.
95  * @param fwd: to delete.
96  */
97 void forwards_delete(struct iter_forwards* fwd);
98 
99 /**
100  * Process forwards config.
101  * @param fwd: where to store.
102  * @param cfg: config options.
103  * @return 0 on error.
104  */
105 int forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg);
106 
107 /**
108  * Find forward zone exactly by name
109  * @param fwd: forward storage.
110  * @param qname: The qname of the query.
111  * @param qclass: The qclass of the query.
112  * @return: A delegation point or null.
113  */
114 struct delegpt* forwards_find(struct iter_forwards* fwd, uint8_t* qname,
115 	uint16_t qclass);
116 
117 /**
118  * Find forward zone information
119  * For this qname/qclass find forward zone information, returns delegation
120  * point with server names and addresses, or NULL if no forwarding is needed.
121  *
122  * @param fwd: forward storage.
123  * @param qname: The qname of the query.
124  * @param qclass: The qclass of the query.
125  * @return: A delegation point if the query has to be forwarded to that list,
126  *         otherwise null.
127  */
128 struct delegpt* forwards_lookup(struct iter_forwards* fwd,
129 	uint8_t* qname, uint16_t qclass);
130 
131 /**
132  * Same as forwards_lookup, but for the root only
133  * @param fwd: forward storage.
134  * @param qclass: The qclass of the query.
135  * @return: A delegation point if root forward exists, otherwise null.
136  */
137 struct delegpt* forwards_lookup_root(struct iter_forwards* fwd,
138 	uint16_t qclass);
139 
140 /**
141  * Find next root item in forwards lookup tree.
142  * @param fwd: the forward storage
143  * @param qclass: class to look at next, or higher.
144  * @return false if none found, or if true stored in qclass.
145  */
146 int forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass);
147 
148 /**
149  * Get memory in use by forward storage
150  * @param fwd: forward storage.
151  * @return bytes in use
152  */
153 size_t forwards_get_mem(struct iter_forwards* fwd);
154 
155 /** compare two fwd entries */
156 int fwd_cmp(const void* k1, const void* k2);
157 
158 /**
159  * Add zone to forward structure. For external use since it recalcs
160  * the tree parents.
161  * @param fwd: the forward data structure
162  * @param c: class of zone
163  * @param dp: delegation point with name and target nameservers for new
164  *	forward zone. malloced.
165  * @return false on failure (out of memory);
166  */
167 int forwards_add_zone(struct iter_forwards* fwd, uint16_t c,
168 	struct delegpt* dp);
169 
170 /**
171  * Remove zone from forward structure. For external use since it
172  * recalcs the tree parents.
173  * @param fwd: the forward data structure
174  * @param c: class of zone
175  * @param nm: name of zone (in uncompressed wireformat).
176  */
177 void forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm);
178 
179 /**
180  * Add stub hole (empty entry in forward table, that makes resolution skip
181  * a forward-zone because the stub zone should override the forward zone).
182  * Does not add one if not necessary.
183  * @param fwd: the forward data structure
184  * @param c: class of zone
185  * @param nm: name of zone (in uncompressed wireformat).
186  * @return false on failure (out of memory);
187  */
188 int forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm);
189 
190 /**
191  * Remove stub hole, if one exists.
192  * @param fwd: the forward data structure
193  * @param c: class of zone
194  * @param nm: name of zone (in uncompressed wireformat).
195  */
196 void forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c,
197 	uint8_t* nm);
198 
199 #endif /* ITERATOR_ITER_FWD_H */
200