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
;
55 * This type determines processing for queries that did not match
56 * local-data directly.
61 /** answer with error */
63 /** answer nxdomain or nodata */
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 */
71 /** remove default AS112 blocking contents for zone
72 * nodefault is used in config not during service. */
77 * Authoritative local zones storage, shared.
80 /** lock on the localzone tree */
82 /** rbtree of struct local_zone */
87 * Local zone. A locally served authoritative zone.
90 /** rbtree node, key is name and class */
92 /** parent zone, if any. */
93 struct local_zone
* parent
;
95 /** zone name, in uncompressed wireformat */
97 /** length of zone name */
99 /** number of labels in zone name */
101 /** the class of this zone.
102 * uses 'dclass' to not conflict with c++ keyword class. */
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) */
111 /** how to process zone */
112 enum localzone_type type
;
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 */
120 /** if data contains zone apex SOA data, this is a ptr to it. */
121 struct ub_packed_rrset_key
* soa
;
125 * Local data. One domain name, and the RRs to go with it.
128 /** rbtree node, key is name only */
132 /** length of name */
134 /** number of labels in name */
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
;
146 struct local_rrset
* next
;
147 /** RRset data item */
148 struct ub_packed_rrset_key
* rrset
;
152 * Create local zones storage
153 * @return new struct or NULL on error.
155 struct local_zones
* local_zones_create(void);
158 * Delete local zones storage
159 * @param zones: to delete.
161 void local_zones_delete(struct local_zones
* zones
);
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.
170 int local_zones_apply_cfg(struct local_zones
* zones
, struct config_file
* cfg
);
173 * Compare two local_zone entries in rbtree. Sort hierarchical but not
177 * @return: -1, 0, +1 comparison value.
179 int local_zone_cmp(const void* z1
, const void* z2
);
182 * Compare two local_data entries in rbtree. Sort canonical.
185 * @return: -1, 0, +1 comparison value.
187 int local_data_cmp(const void* d1
, const void* d2
);
191 * @param z: to delete.
193 void local_zone_delete(struct local_zone
* z
);
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.
205 struct local_zone
* local_zones_lookup(struct local_zones
* zones
,
206 uint8_t* name
, size_t len
, int labs
, uint16_t dclass
);
209 * Debug helper. Print all zones
210 * Takes care of locking.
211 * @param zones: the zones tree
213 void local_zones_print(struct local_zones
* zones
);
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).
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
);
231 * Parse the string into localzone type.
233 * @param str: string to parse
234 * @param t: local zone type returned here.
235 * @return 0 on parse error.
237 int local_zone_str2type(const char* str
, enum localzone_type
* t
);
240 * Print localzone type to a string. Pointer to a constant string.
242 * @param t: local zone type.
243 * @return constant string that describes type.
245 const char* local_zone_type2str(enum localzone_type t
);
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.
257 struct local_zone
* local_zones_find(struct local_zones
* zones
,
258 uint8_t* name
, size_t len
, int labs
, uint16_t dclass
);
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.
270 * @return local_zone or NULL on error, caller must printout memory error.
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
);
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.
282 void local_zones_del_zone(struct local_zones
* zones
, struct local_zone
* zone
);
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.
292 int local_zones_add_RR(struct local_zones
* zones
, const char* rr
);
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.
303 void local_zones_del_data(struct local_zones
* zones
,
304 uint8_t* name
, size_t len
, int labs
, uint16_t dclass
);
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.
315 int parse_dname(const char* str
, uint8_t** res
, size_t* len
, int* labs
);
317 #endif /* SERVICES_LOCALZONE_H */