2 * util/data/packed_rrset.h - data storage for a set of resource records.
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 the data storage for RRsets.
42 #ifndef UTIL_DATA_PACKED_RRSET_H
43 #define UTIL_DATA_PACKED_RRSET_H
44 #include "util/storage/lruhash.h"
48 /** type used to uniquely identify rrsets. Cannot be reused without
49 * clearing the cache. */
50 typedef uint64_t rrset_id_t;
52 /** this rrset is NSEC and is at zone apex (at child side of zonecut) */
53 #define PACKED_RRSET_NSEC_AT_APEX 0x1
54 /** this rrset is A/AAAA and is in-zone-glue (from parent side of zonecut) */
55 #define PACKED_RRSET_PARENT_SIDE 0x2
56 /** this rrset is SOA and has the negative ttl (from nxdomain or nodata),
57 * this is set on SOA rrsets in the authority section, to keep its TTL separate
58 * from the SOA in the answer section from a direct SOA query or ANY query. */
59 #define PACKED_RRSET_SOA_NEG 0x4
61 /** number of rrs and rrsets for integer overflow protection. More than
62 * this is not really possible (64K packet has much less RRs and RRsets) in
63 * a message. And this is small enough that also multiplied there is no
64 * integer overflow. */
65 #define RR_COUNT_MAX 0xffffff
68 * The identifying information for an RRset.
70 struct packed_rrset_key {
72 * The domain name. If not null (for id=0) it is allocated, and
73 * contains the wireformat domain name.
74 * This dname is not canonicalized.
78 * Length of the domain name, including last 0 root octet.
82 * Flags. 32bit to be easy for hashing:
83 * o PACKED_RRSET_NSEC_AT_APEX
84 * o PACKED_RRSET_PARENT_SIDE
85 * o PACKED_RRSET_SOA_NEG
88 /** the rrset type in network format */
90 /** the rrset class in network format */
95 * This structure contains an RRset. A set of resource records that
96 * share the same domain name, type and class.
98 * Due to memory management and threading, the key structure cannot be
99 * deleted, although the data can be. The id can be set to 0 to store and the
100 * structure can be recycled with a new id.
102 struct ub_packed_rrset_key {
104 * entry into hashtable. Note the lock is never destroyed,
105 * even when this key is retired to the cache.
106 * the data pointer (if not null) points to a struct packed_rrset.
108 struct lruhash_entry entry;
110 * the ID of this rrset. unique, based on threadid + sequenceno.
111 * ids are not reused, except after flushing the cache.
112 * zero is an unused entry, and never a valid id.
113 * Check this value after getting entry.lock.
114 * The other values in this struct may only be altered after changing
115 * the id (which needs a writelock on entry.lock).
118 /** key data: dname, type and class */
119 struct packed_rrset_key rk;
123 * RRset trustworthiness. Bigger value is more trust. RFC 2181.
124 * The rrset_trust_add_noAA, rrset_trust_auth_noAA, rrset_trust_add_AA,
125 * are mentioned as the same trustworthiness in 2181, but split up here
126 * for ease of processing.
128 * rrset_trust_nonauth_ans_AA, rrset_trust_ans_noAA
129 * are also mentioned as the same trustworthiness in 2181, but split up here
130 * for ease of processing.
132 * Added trust_none for a sane initial value, smaller than anything else.
133 * Added validated and ultimate trust for keys and rrsig validated content.
136 /** initial value for trust */
137 rrset_trust_none = 0,
138 /** Additional information from non-authoritative answers */
139 rrset_trust_add_noAA,
140 /** Data from the authority section of a non-authoritative answer */
141 rrset_trust_auth_noAA,
142 /** Additional information from an authoritative answer */
144 /** non-authoritative data from the answer section of authoritative
146 rrset_trust_nonauth_ans_AA,
147 /** Data from the answer section of a non-authoritative answer */
148 rrset_trust_ans_noAA,
149 /** Glue from a primary zone, or glue from a zone transfer */
151 /** Data from the authority section of an authoritative answer */
153 /** The authoritative data included in the answer section of an
154 * authoritative reply */
156 /** Data from a zone transfer, other than glue */
157 rrset_trust_sec_noglue,
158 /** Data from a primary zone file, other than glue data */
159 rrset_trust_prim_noglue,
160 /** DNSSEC(rfc4034) validated with trusted keys */
161 rrset_trust_validated,
162 /** ultimately trusted, no more trust is possible;
163 * trusted keys from the unbound configuration setup. */
168 * Security status from validation for data.
169 * The order is significant; more secure, more proven later.
172 /** UNCHECKED means that object has yet to be validated. */
173 sec_status_unchecked = 0,
174 /** BOGUS means that the object (RRset or message) failed to validate
175 * (according to local policy), but should have validated. */
177 /** INDETERMINATE means that the object is insecure, but not
178 * authoritatively so. Generally this means that the RRset is not
179 * below a configured trust anchor. */
180 sec_status_indeterminate,
181 /** INSECURE means that the object is authoritatively known to be
182 * insecure. Generally this means that this RRset is below a trust
183 * anchor, but also below a verified, insecure delegation. */
185 /** SECURE means that the object (RRset or message) validated
186 * according to local policy. */
193 * The data is packed, stored contiguously in memory.
196 * o rr_len size_t array
197 * o rr_data uint8_t* array
198 * o rr_ttl time_t array (after size_t and ptrs because those may be
199 * 64bit and this array before those would make them unaligned).
200 * Since the stuff before is 32/64bit, rr_ttl is 32 bit aligned.
201 * o rr_data rdata wireformats
202 * o rrsig_data rdata wireformat(s)
204 * Rdata is stored in wireformat. The dname is stored in wireformat.
205 * TTLs are stored as absolute values (and could be expired).
207 * RRSIGs are stored in the arrays after the regular rrs.
209 * You need the packed_rrset_key to know dname, type, class of the
210 * resource records in this RRset. (if signed the rrsig gives the type too).
212 * On the wire an RR is:
213 * name, type, class, ttl, rdlength, rdata.
214 * So we need to send the following per RR:
215 * key.dname, ttl, rr_data[i].
216 * since key.dname ends with type and class.
217 * and rr_data starts with the rdlength.
218 * the ttl value to send changes due to time.
220 struct packed_rrset_data {
221 /** TTL (in seconds like time()) of the rrset.
222 * Same for all RRs see rfc2181(5.2). */
224 /** number of rrs. */
226 /** number of rrsigs, if 0 no rrsigs */
228 /** the trustworthiness of the rrset data */
229 enum rrset_trust trust;
230 /** security status of the rrset data */
231 enum sec_status security;
232 /** length of every rr's rdata, rr_len[i] is size of rr_data[i]. */
234 /** ttl of every rr. rr_ttl[i] ttl of rr i. */
237 * Array of pointers to every rr's rdata.
238 * The rr_data[i] rdata is stored in uncompressed wireformat.
239 * The first uint16_t of rr_data[i] is network format rdlength.
241 * rr_data[count] to rr_data[count+rrsig_count] contain the rrsig data.
247 * An RRset can be represented using both key and data together.
248 * Split into key and data structures to simplify implementation of
251 struct packed_rrset {
252 /** domain name, type and class */
253 struct packed_rrset_key* k;
254 /** ttl, count and rdatas (and rrsig) */
255 struct packed_rrset_data* d;
259 * list of packed rrsets
261 struct packed_rrset_list {
263 struct packed_rrset_list* next;
264 /** rrset key and data */
265 struct packed_rrset rrset;
269 * Delete packed rrset key and data, not entered in hashtables yet.
270 * Used during parsing.
271 * @param pkey: rrset key structure with locks, key and data pointers.
272 * @param alloc: where to return the unfree-able key structure.
274 void ub_packed_rrset_parsedelete(struct ub_packed_rrset_key* pkey,
275 struct alloc_cache* alloc);
278 * Memory size of rrset data. RRset data must be filled in correctly.
279 * @param data: data to examine.
280 * @return size in bytes.
282 size_t packed_rrset_sizeof(struct packed_rrset_data* data);
285 * Get TTL of rrset. RRset data must be filled in correctly.
286 * @param key: rrset key, with data to examine.
289 time_t ub_packed_rrset_ttl(struct ub_packed_rrset_key* key);
292 * Calculate memory size of rrset entry. For hash table usage.
293 * @param key: struct ub_packed_rrset_key*.
294 * @param data: struct packed_rrset_data*.
295 * @return size in bytes.
297 size_t ub_rrset_sizefunc(void* key, void* data);
300 * compares two rrset keys.
301 * @param k1: struct ub_packed_rrset_key*.
302 * @param k2: struct ub_packed_rrset_key*.
303 * @return 0 if equal.
305 int ub_rrset_compare(void* k1, void* k2);
308 * compare two rrset data structures.
309 * Compared rdata and rrsigdata, not the trust or ttl value.
310 * @param d1: data to compare.
311 * @param d2: data to compare.
312 * @return 1 if equal.
314 int rrsetdata_equal(struct packed_rrset_data* d1, struct packed_rrset_data* d2);
317 * Old key to be deleted. RRset keys are recycled via alloc.
318 * The id is set to 0. So that other threads, after acquiring a lock always
319 * get the correct value, in this case the 0 deleted-special value.
320 * @param key: struct ub_packed_rrset_key*.
321 * @param userdata: alloc structure to use for recycling.
323 void ub_rrset_key_delete(void* key, void* userdata);
326 * Old data to be deleted.
327 * @param data: what to delete.
328 * @param userdata: user data ptr.
330 void rrset_data_delete(void* data, void* userdata);
333 * Calculate hash value for a packed rrset key.
334 * @param key: the rrset key with name, type, class, flags.
335 * @return hash value.
337 hashvalue_t rrset_key_hash(struct packed_rrset_key* key);
340 * Fixup pointers in fixed data packed_rrset_data blob.
341 * After a memcpy of the data for example. Will set internal pointers right.
342 * @param data: rrset data structure. Otherwise correctly filled in.
344 void packed_rrset_ptr_fixup(struct packed_rrset_data* data);
347 * Fixup TTLs in fixed data packed_rrset_data blob.
348 * @param data: rrset data structure. Otherwise correctly filled in.
349 * @param add: how many seconds to add, pass time(0) for example.
351 void packed_rrset_ttl_add(struct packed_rrset_data* data, time_t add);
354 * Utility procedure to extract CNAME target name from its rdata.
355 * Failsafes; it will change passed dname to a valid dname or do nothing.
356 * @param rrset: the rrset structure. Must be a CNAME.
357 * Only first RR is used (multiple RRs are technically illegal anyway).
358 * Also works on type DNAME. Returns target name.
359 * @param dname: this pointer is updated to point into the cname rdata.
360 * If a failsafe fails, nothing happens to the pointer (such as the
361 * rdata was not a valid dname, not a CNAME, ...).
362 * @param dname_len: length of dname is returned.
364 void get_cname_target(struct ub_packed_rrset_key* rrset, uint8_t** dname,
368 * Get a printable string for a rrset trust value
369 * @param s: rrset trust value
370 * @return printable string.
372 const char* rrset_trust_to_string(enum rrset_trust s);
375 * Get a printable string for a security status value
376 * @param s: security status
377 * @return printable string.
379 const char* sec_status_to_string(enum sec_status s);
382 * Print string with neat domain name, type, class from rrset.
383 * @param v: at what verbosity level to print this.
384 * @param str: string of message.
385 * @param rrset: structure with name, type and class.
387 void log_rrset_key(enum verbosity_value v, const char* str,
388 struct ub_packed_rrset_key* rrset);
391 * Convert RR from RRset to string.
392 * @param rrset: structure with data.
393 * @param i: index of rr or RRSIG.
394 * @param now: time that is subtracted from ttl before printout. Can be 0.
395 * @param dest: destination string buffer. Must be nonNULL.
396 * @param dest_len: length of dest buffer (>0).
397 * @return false on failure.
399 int packed_rr_to_string(struct ub_packed_rrset_key* rrset, size_t i,
400 time_t now, char* dest, size_t dest_len);
403 * Print the string with prefix, one rr per line.
404 * @param v: at what verbosity level to print this.
405 * @param str: string of message.
406 * @param rrset: with name, and rdata, and rrsigs.
408 void log_packed_rrset(enum verbosity_value v, const char* str,
409 struct ub_packed_rrset_key* rrset);
412 * Allocate rrset in region - no more locks needed
413 * @param key: a (just from rrset cache looked up) rrset key + valid,
414 * packed data record.
415 * @param region: where to alloc the copy
416 * @param now: adjust the TTLs to be relative (subtract from all TTLs).
417 * @return new region-alloced rrset key or NULL on alloc failure.
419 struct ub_packed_rrset_key* packed_rrset_copy_region(
420 struct ub_packed_rrset_key* key, struct regional* region,
424 * Allocate rrset with malloc (from region or you are holding the lock).
425 * @param key: key with data entry.
426 * @param alloc: alloc_cache to create rrset_keys
427 * @param now: adjust the TTLs to be absolute (add to all TTLs).
428 * @return new region-alloced rrset key or NULL on alloc failure.
430 struct ub_packed_rrset_key* packed_rrset_copy_alloc(
431 struct ub_packed_rrset_key* key, struct alloc_cache* alloc,
434 #endif /* UTIL_DATA_PACKED_RRSET_H */