2 * services/localzone.h - local zones authority service.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions to enable local zone authority service.
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;
56 * This type determines processing for queries that did not match
57 * local-data directly.
62 /** answer with error */
64 /** answer nxdomain or nodata */
66 /** resolve normally */
67 local_zone_transparent,
68 /** do not block types at localdata names */
69 local_zone_typetransparent,
70 /** answer with data at zone apex */
72 /** remove default AS112 blocking contents for zone
73 * nodefault is used in config not during service. */
75 /** log client address, but no block (transparent) */
80 * Authoritative local zones storage, shared.
83 /** lock on the localzone tree */
85 /** rbtree of struct local_zone */
90 * Local zone. A locally served authoritative zone.
93 /** rbtree node, key is name and class */
95 /** parent zone, if any. */
96 struct local_zone* parent;
98 /** zone name, in uncompressed wireformat */
100 /** length of zone name */
102 /** number of labels in zone name */
104 /** the class of this zone.
105 * uses 'dclass' to not conflict with c++ keyword class. */
108 /** lock on the data in the structure
109 * For the node, parent, name, namelen, namelabs, dclass, you
110 * need to also hold the zones_tree lock to change them (or to
111 * delete this zone) */
114 /** how to process zone */
115 enum localzone_type type;
117 /** in this region the zone's data is allocated.
118 * the struct local_zone itself is malloced. */
119 struct regional* region;
120 /** local data for this zone
121 * rbtree of struct local_data */
123 /** if data contains zone apex SOA data, this is a ptr to it. */
124 struct ub_packed_rrset_key* soa;
128 * Local data. One domain name, and the RRs to go with it.
131 /** rbtree node, key is name only */
135 /** length of name */
137 /** number of labels in name */
139 /** the data rrsets, with different types, linked list.
140 * If this list is NULL, the node is an empty non-terminal. */
141 struct local_rrset* rrsets;
149 struct local_rrset* next;
150 /** RRset data item */
151 struct ub_packed_rrset_key* rrset;
155 * Create local zones storage
156 * @return new struct or NULL on error.
158 struct local_zones* local_zones_create(void);
161 * Delete local zones storage
162 * @param zones: to delete.
164 void local_zones_delete(struct local_zones* zones);
167 * Apply config settings; setup the local authoritative data.
168 * Takes care of locking.
169 * @param zones: is set up.
170 * @param cfg: config data.
171 * @return false on error.
173 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
176 * Compare two local_zone entries in rbtree. Sort hierarchical but not
180 * @return: -1, 0, +1 comparison value.
182 int local_zone_cmp(const void* z1, const void* z2);
185 * Compare two local_data entries in rbtree. Sort canonical.
188 * @return: -1, 0, +1 comparison value.
190 int local_data_cmp(const void* d1, const void* d2);
194 * @param z: to delete.
196 void local_zone_delete(struct local_zone* z);
199 * Lookup zone that contains the given name, class.
200 * User must lock the tree or result zone.
201 * @param zones: the zones tree
202 * @param name: dname to lookup
203 * @param len: length of name.
204 * @param labs: labelcount of name.
205 * @param dclass: class to lookup.
206 * @return closest local_zone or NULL if no covering zone is found.
208 struct local_zone* local_zones_lookup(struct local_zones* zones,
209 uint8_t* name, size_t len, int labs, uint16_t dclass);
212 * Debug helper. Print all zones
213 * Takes care of locking.
214 * @param zones: the zones tree
216 void local_zones_print(struct local_zones* zones);
219 * Answer authoritatively for local zones.
220 * Takes care of locking.
221 * @param zones: the stored zones (shared, read only).
222 * @param qinfo: query info (parsed).
223 * @param edns: edns info (parsed).
224 * @param buf: buffer with query ID and flags, also for reply.
225 * @param temp: temporary storage region.
226 * @param repinfo: source address for checks. may be NULL.
227 * @return true if answer is in buffer. false if query is not answered
228 * by authority data. If the reply should be dropped altogether, the return
229 * value is true, but the buffer is cleared (empty).
231 int local_zones_answer(struct local_zones* zones, struct query_info* qinfo,
232 struct edns_data* edns, struct sldns_buffer* buf, struct regional* temp,
233 struct comm_reply* repinfo);
236 * Parse the string into localzone type.
238 * @param str: string to parse
239 * @param t: local zone type returned here.
240 * @return 0 on parse error.
242 int local_zone_str2type(const char* str, enum localzone_type* t);
245 * Print localzone type to a string. Pointer to a constant string.
247 * @param t: local zone type.
248 * @return constant string that describes type.
250 const char* local_zone_type2str(enum localzone_type t);
253 * Find zone that with exactly given name, class.
254 * User must lock the tree or result zone.
255 * @param zones: the zones tree
256 * @param name: dname to lookup
257 * @param len: length of name.
258 * @param labs: labelcount of name.
259 * @param dclass: class to lookup.
260 * @return the exact local_zone or NULL.
262 struct local_zone* local_zones_find(struct local_zones* zones,
263 uint8_t* name, size_t len, int labs, uint16_t dclass);
266 * Add a new zone. Caller must hold the zones lock.
267 * Adjusts the other zones as well (parent pointers) after insertion.
268 * The zone must NOT exist (returns NULL and logs error).
269 * @param zones: the zones tree
270 * @param name: dname to add
271 * @param len: length of name.
272 * @param labs: labelcount of name.
273 * @param dclass: class to add.
275 * @return local_zone or NULL on error, caller must printout memory error.
277 struct local_zone* local_zones_add_zone(struct local_zones* zones,
278 uint8_t* name, size_t len, int labs, uint16_t dclass,
279 enum localzone_type tp);
282 * Delete a zone. Caller must hold the zones lock.
283 * Adjusts the other zones as well (parent pointers) after insertion.
284 * @param zones: the zones tree
285 * @param zone: the zone to delete from tree. Also deletes zone from memory.
287 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
290 * Add RR data into the localzone data.
291 * Looks up the zone, if no covering zone, a transparent zone with the
292 * name of the RR is created.
293 * @param zones: the zones tree. Not locked by caller.
294 * @param rr: string with on RR.
295 * @return false on failure.
297 int local_zones_add_RR(struct local_zones* zones, const char* rr);
300 * Remove data from domain name in the tree.
301 * All types are removed. No effect if zone or name does not exist.
302 * @param zones: zones tree.
303 * @param name: dname to remove
304 * @param len: length of name.
305 * @param labs: labelcount of name.
306 * @param dclass: class to remove.
308 void local_zones_del_data(struct local_zones* zones,
309 uint8_t* name, size_t len, int labs, uint16_t dclass);
313 * Form wireformat from text format domain name.
314 * @param str: the domain name in text "www.example.com"
315 * @param res: resulting wireformat is stored here with malloc.
316 * @param len: length of resulting wireformat.
317 * @param labs: number of labels in resulting wireformat.
318 * @return false on error, syntax or memory. Also logged.
320 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
322 #endif /* SERVICES_LOCALZONE_H */