]> git.saurik.com Git - apple/network_cmds.git/blob - unbound/services/localzone.h
788fbfb3ba2b196c34d1b64dfe075c0242100f4d
[apple/network_cmds.git] / unbound / services / localzone.h
1 /*
2 * services/localzone.h - local zones authority service.
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 enable local zone authority service.
40 */
41
42 #ifndef SERVICES_LOCALZONE_H
43 #define SERVICES_LOCALZONE_H
44 #include "util/rbtree.h"
45 #include "util/locks.h"
46 struct ub_packed_rrset_key;
47 struct regional;
48 struct config_file;
49 struct edns_data;
50 struct query_info;
51 struct sldns_buffer;
52
53 /**
54 * Local zone type
55 * This type determines processing for queries that did not match
56 * local-data directly.
57 */
58 enum localzone_type {
59 /** drop query */
60 local_zone_deny = 0,
61 /** answer with error */
62 local_zone_refuse,
63 /** answer nxdomain or nodata */
64 local_zone_static,
65 /** resolve normally */
66 local_zone_transparent,
67 /** do not block types at localdata names */
68 local_zone_typetransparent,
69 /** answer with data at zone apex */
70 local_zone_redirect,
71 /** remove default AS112 blocking contents for zone
72 * nodefault is used in config not during service. */
73 local_zone_nodefault
74 };
75
76 /**
77 * Authoritative local zones storage, shared.
78 */
79 struct local_zones {
80 /** lock on the localzone tree */
81 lock_rw_t lock;
82 /** rbtree of struct local_zone */
83 rbtree_t ztree;
84 };
85
86 /**
87 * Local zone. A locally served authoritative zone.
88 */
89 struct local_zone {
90 /** rbtree node, key is name and class */
91 rbnode_t node;
92 /** parent zone, if any. */
93 struct local_zone* parent;
94
95 /** zone name, in uncompressed wireformat */
96 uint8_t* name;
97 /** length of zone name */
98 size_t namelen;
99 /** number of labels in zone name */
100 int namelabs;
101 /** the class of this zone.
102 * uses 'dclass' to not conflict with c++ keyword class. */
103 uint16_t dclass;
104
105 /** lock on the data in the structure
106 * For the node, parent, name, namelen, namelabs, dclass, you
107 * need to also hold the zones_tree lock to change them (or to
108 * delete this zone) */
109 lock_rw_t lock;
110
111 /** how to process zone */
112 enum localzone_type type;
113
114 /** in this region the zone's data is allocated.
115 * the struct local_zone itself is malloced. */
116 struct regional* region;
117 /** local data for this zone
118 * rbtree of struct local_data */
119 rbtree_t data;
120 /** if data contains zone apex SOA data, this is a ptr to it. */
121 struct ub_packed_rrset_key* soa;
122 };
123
124 /**
125 * Local data. One domain name, and the RRs to go with it.
126 */
127 struct local_data {
128 /** rbtree node, key is name only */
129 rbnode_t node;
130 /** domain name */
131 uint8_t* name;
132 /** length of name */
133 size_t namelen;
134 /** number of labels in name */
135 int namelabs;
136 /** the data rrsets, with different types, linked list.
137 * If this list is NULL, the node is an empty non-terminal. */
138 struct local_rrset* rrsets;
139 };
140
141 /**
142 * A local data RRset
143 */
144 struct local_rrset {
145 /** next in list */
146 struct local_rrset* next;
147 /** RRset data item */
148 struct ub_packed_rrset_key* rrset;
149 };
150
151 /**
152 * Create local zones storage
153 * @return new struct or NULL on error.
154 */
155 struct local_zones* local_zones_create(void);
156
157 /**
158 * Delete local zones storage
159 * @param zones: to delete.
160 */
161 void local_zones_delete(struct local_zones* zones);
162
163 /**
164 * Apply config settings; setup the local authoritative data.
165 * Takes care of locking.
166 * @param zones: is set up.
167 * @param cfg: config data.
168 * @return false on error.
169 */
170 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
171
172 /**
173 * Compare two local_zone entries in rbtree. Sort hierarchical but not
174 * canonical
175 * @param z1: zone 1
176 * @param z2: zone 2
177 * @return: -1, 0, +1 comparison value.
178 */
179 int local_zone_cmp(const void* z1, const void* z2);
180
181 /**
182 * Compare two local_data entries in rbtree. Sort canonical.
183 * @param d1: data 1
184 * @param d2: data 2
185 * @return: -1, 0, +1 comparison value.
186 */
187 int local_data_cmp(const void* d1, const void* d2);
188
189 /**
190 * Delete one zone
191 * @param z: to delete.
192 */
193 void local_zone_delete(struct local_zone* z);
194
195 /**
196 * Lookup zone that contains the given name, class.
197 * User must lock the tree or result zone.
198 * @param zones: the zones tree
199 * @param name: dname to lookup
200 * @param len: length of name.
201 * @param labs: labelcount of name.
202 * @param dclass: class to lookup.
203 * @return closest local_zone or NULL if no covering zone is found.
204 */
205 struct local_zone* local_zones_lookup(struct local_zones* zones,
206 uint8_t* name, size_t len, int labs, uint16_t dclass);
207
208 /**
209 * Debug helper. Print all zones
210 * Takes care of locking.
211 * @param zones: the zones tree
212 */
213 void local_zones_print(struct local_zones* zones);
214
215 /**
216 * Answer authoritatively for local zones.
217 * Takes care of locking.
218 * @param zones: the stored zones (shared, read only).
219 * @param qinfo: query info (parsed).
220 * @param edns: edns info (parsed).
221 * @param buf: buffer with query ID and flags, also for reply.
222 * @param temp: temporary storage region.
223 * @return true if answer is in buffer. false if query is not answered
224 * by authority data. If the reply should be dropped altogether, the return
225 * value is true, but the buffer is cleared (empty).
226 */
227 int local_zones_answer(struct local_zones* zones, struct query_info* qinfo,
228 struct edns_data* edns, struct sldns_buffer* buf, struct regional* temp);
229
230 /**
231 * Parse the string into localzone type.
232 *
233 * @param str: string to parse
234 * @param t: local zone type returned here.
235 * @return 0 on parse error.
236 */
237 int local_zone_str2type(const char* str, enum localzone_type* t);
238
239 /**
240 * Print localzone type to a string. Pointer to a constant string.
241 *
242 * @param t: local zone type.
243 * @return constant string that describes type.
244 */
245 const char* local_zone_type2str(enum localzone_type t);
246
247 /**
248 * Find zone that with exactly given name, class.
249 * User must lock the tree or result zone.
250 * @param zones: the zones tree
251 * @param name: dname to lookup
252 * @param len: length of name.
253 * @param labs: labelcount of name.
254 * @param dclass: class to lookup.
255 * @return the exact local_zone or NULL.
256 */
257 struct local_zone* local_zones_find(struct local_zones* zones,
258 uint8_t* name, size_t len, int labs, uint16_t dclass);
259
260 /**
261 * Add a new zone. Caller must hold the zones lock.
262 * Adjusts the other zones as well (parent pointers) after insertion.
263 * The zone must NOT exist (returns NULL and logs error).
264 * @param zones: the zones tree
265 * @param name: dname to add
266 * @param len: length of name.
267 * @param labs: labelcount of name.
268 * @param dclass: class to add.
269 * @param tp: type.
270 * @return local_zone or NULL on error, caller must printout memory error.
271 */
272 struct local_zone* local_zones_add_zone(struct local_zones* zones,
273 uint8_t* name, size_t len, int labs, uint16_t dclass,
274 enum localzone_type tp);
275
276 /**
277 * Delete a zone. Caller must hold the zones lock.
278 * Adjusts the other zones as well (parent pointers) after insertion.
279 * @param zones: the zones tree
280 * @param zone: the zone to delete from tree. Also deletes zone from memory.
281 */
282 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
283
284 /**
285 * Add RR data into the localzone data.
286 * Looks up the zone, if no covering zone, a transparent zone with the
287 * name of the RR is created.
288 * @param zones: the zones tree. Not locked by caller.
289 * @param rr: string with on RR.
290 * @return false on failure.
291 */
292 int local_zones_add_RR(struct local_zones* zones, const char* rr);
293
294 /**
295 * Remove data from domain name in the tree.
296 * All types are removed. No effect if zone or name does not exist.
297 * @param zones: zones tree.
298 * @param name: dname to remove
299 * @param len: length of name.
300 * @param labs: labelcount of name.
301 * @param dclass: class to remove.
302 */
303 void local_zones_del_data(struct local_zones* zones,
304 uint8_t* name, size_t len, int labs, uint16_t dclass);
305
306
307 /**
308 * Form wireformat from text format domain name.
309 * @param str: the domain name in text "www.example.com"
310 * @param res: resulting wireformat is stored here with malloc.
311 * @param len: length of resulting wireformat.
312 * @param labs: number of labels in resulting wireformat.
313 * @return false on error, syntax or memory. Also logged.
314 */
315 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
316
317 #endif /* SERVICES_LOCALZONE_H */