2 * util/data/msgreply.h - store message and reply data.
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 a data structure to store a message and its reply.
42 #ifndef UTIL_DATA_MSGREPLY_H
43 #define UTIL_DATA_MSGREPLY_H
44 #include "util/storage/lruhash.h"
45 #include "util/data/packed_rrset.h"
55 /** calculate the prefetch TTL as 90% of original. Calculation
56 * without numerical overflow (uin32_t) */
57 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
60 * Structure to store query information that makes answers to queries
65 * Salient data on the query: qname, in wireformat.
66 * can be allocated or a pointer to outside buffer.
67 * User has to keep track on the status of this.
70 /** length of qname (including last 0 octet) */
72 /** qtype, host byte order */
74 /** qclass, host byte order */
79 * Information to reference an rrset
82 /** the key with lock, and ptr to packed data. */
83 struct ub_packed_rrset_key* key;
89 * Structure to store DNS query and the reply packet.
90 * To use it, copy over the flags from reply and modify using flags from
91 * the query (RD,CD if not AA). prepend ID.
96 * o packed_rrset_key* array.
98 * Memory layout is sometimes not packed, when the message is synthesized,
99 * for easy of the generation. It is allocated packed when it is copied
100 * from the region allocation to the malloc allocation.
103 /** the flags for the answer, host byte order. */
107 * This flag informs unbound the answer is authoritative and
108 * the AA flag should be preserved.
110 uint8_t authoritative;
113 * Number of RRs in the query section.
114 * If qdcount is not 0, then it is 1, and the data that appears
115 * in the reply is the same as the query_info.
120 /** 32 bit padding to pad struct member alignment to 64 bits. */
124 * TTL of the entire reply (for negative caching).
125 * only for use when there are 0 RRsets in this message.
126 * if there are RRsets, check those instead.
131 * TTL for prefetch. After it has expired, a prefetch is suitable.
132 * Smaller than the TTL, otherwise the prefetch would not happen.
137 * The security status from DNSSEC validation of this message.
139 enum sec_status security;
142 * Number of RRsets in each section.
143 * The answer section. Add up the RRs in every RRset to calculate
144 * the number of RRs, and the count for the dns packet.
145 * The number of RRs in RRsets can change due to RRset updates.
149 /** Count of authority section RRsets */
151 /** Count of additional section RRsets */
154 /** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
158 * List of pointers (only) to the rrsets in the order in which
159 * they appear in the reply message.
160 * Number of elements is ancount+nscount+arcount RRsets.
161 * This is a pointer to that array.
162 * Use the accessor function for access.
164 struct ub_packed_rrset_key** rrsets;
167 * Packed array of ids (see counts) and pointers to packed_rrset_key.
168 * The number equals ancount+nscount+arcount RRsets.
169 * These are sorted in ascending pointer, the locking order. So
170 * this list can be locked (and id, ttl checked), to see if
171 * all the data is available and recent enough.
173 * This is defined as an array of size 1, so that the compiler
174 * associates the identifier with this position in the structure.
175 * Array bound overflow on this array then gives access to the further
176 * elements of the array, which are allocated after the main structure.
178 * It could be more pure to define as array of size 0, ref[0].
179 * But ref[1] may be less confusing for compilers.
180 * Use the accessor function for access.
182 struct rrset_ref ref[1];
186 * Structure to keep hash table entry for message replies.
188 struct msgreply_entry {
189 /** the hash table key */
190 struct query_info key;
191 /** the hash table entry, data is struct reply_info* */
192 struct lruhash_entry entry;
196 * Parse wire query into a queryinfo structure, return 0 on parse error.
197 * initialises the (prealloced) queryinfo structure as well.
198 * This query structure contains a pointer back info the buffer!
199 * This pointer avoids memory allocation. allocqname does memory allocation.
200 * @param m: the prealloced queryinfo structure to put query into.
201 * must be unused, or _clear()ed.
202 * @param query: the wireformat packet query. starts with ID.
203 * @return: 0 on format error.
205 int query_info_parse(struct query_info* m, struct sldns_buffer* query);
209 * Fills in preallocated query_info structure (with ptr into buffer).
210 * Allocates reply_info and packed_rrsets. These are not yet added to any
211 * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
212 * @param pkt: the packet buffer. Must be positioned after the query section.
213 * @param alloc: creates packed rrset key structures.
214 * @param rep: allocated reply_info is returned (only on no error).
215 * @param qinf: query_info is returned (only on no error).
216 * @param region: where to store temporary data (for parsing).
217 * @param edns: where to store edns information, does not need to be inited.
218 * @return: zero is OK, or DNS error code in case of error
219 * o FORMERR for parse errors.
220 * o SERVFAIL for memory allocation errors.
222 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc,
223 struct query_info* qinf, struct reply_info** rep,
224 struct regional* region, struct edns_data* edns);
227 * Allocate and decompress parsed message and rrsets.
228 * @param pkt: for name decompression.
229 * @param msg: parsed message in scratch region.
230 * @param alloc: alloc cache for special rrset key structures.
231 * Not used if region!=NULL, it can be NULL in that case.
232 * @param qinf: where to store query info.
233 * qinf itself is allocated by the caller.
234 * @param rep: reply info is allocated and returned.
235 * @param region: if this parameter is NULL then malloc and the alloc is used.
236 * otherwise, everything is allocated in this region.
237 * In a region, no special rrset key structures are needed (not shared),
238 * and no rrset_ref array in the reply is built up.
239 * @return 0 if allocation failed.
241 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
242 struct alloc_cache* alloc, struct query_info* qinf,
243 struct reply_info** rep, struct regional* region);
246 * Sorts the ref array.
247 * @param rep: reply info. rrsets must be filled in.
249 void reply_info_sortref(struct reply_info* rep);
252 * Set TTLs inside the replyinfo to absolute values.
253 * @param rep: reply info. rrsets must be filled in.
254 * Also refs must be filled in.
255 * @param timenow: the current time.
257 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
260 * Delete reply_info and packed_rrsets (while they are not yet added to the
261 * hashtables.). Returns rrsets to the alloc cache.
262 * @param rep: reply_info to delete.
263 * @param alloc: where to return rrset structures to.
265 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
268 * Compare two queryinfo structures, on query and type, class.
269 * It is _not_ sorted in canonical ordering.
270 * @param m1: struct query_info* , void* here to ease use as function pointer.
271 * @param m2: struct query_info* , void* here to ease use as function pointer.
272 * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
274 int query_info_compare(void* m1, void* m2);
276 /** clear out query info structure */
277 void query_info_clear(struct query_info* m);
279 /** calculate size of struct query_info + reply_info */
280 size_t msgreply_sizefunc(void* k, void* d);
282 /** delete msgreply_entry key structure */
283 void query_entry_delete(void *q, void* arg);
285 /** delete reply_info data structure */
286 void reply_info_delete(void* d, void* arg);
288 /** calculate hash value of query_info, lowercases the qname */
289 hashvalue_t query_info_hash(struct query_info *q);
292 * Setup query info entry
293 * @param q: query info to copy. Emptied as if clear is called.
294 * @param r: reply to init data.
295 * @param h: hash value.
296 * @return: newly allocated message reply cache item.
298 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
299 struct reply_info* r, hashvalue_t h);
302 * Copy reply_info and all rrsets in it and allocate.
303 * @param rep: what to copy, probably inside region, no ref[] array in it.
304 * @param alloc: how to allocate rrset keys.
305 * Not used if region!=NULL, it can be NULL in that case.
306 * @param region: if this parameter is NULL then malloc and the alloc is used.
307 * otherwise, everything is allocated in this region.
308 * In a region, no special rrset key structures are needed (not shared),
309 * and no rrset_ref array in the reply is built up.
310 * @return new reply info or NULL on memory error.
312 struct reply_info* reply_info_copy(struct reply_info* rep,
313 struct alloc_cache* alloc, struct regional* region);
316 * Copy a parsed rrset into given key, decompressing and allocating rdata.
317 * @param pkt: packet for decompression
318 * @param msg: the parser message (for flags for trust).
319 * @param pset: the parsed rrset to copy.
320 * @param region: if NULL - malloc, else data is allocated in this region.
321 * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
322 * will be set on return.
323 * Note that TTL will still be relative on return.
324 * @return false on alloc failure.
326 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
327 struct rrset_parse *pset, struct regional* region,
328 struct ub_packed_rrset_key* pk);
331 * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
332 * @param qinfo: what to start with.
333 * @param rep: looks in answer section of this message.
334 * @return: pointer dname, or NULL if not found.
336 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
337 struct reply_info* rep);
340 * Check if cname chain in cached reply is still valid.
341 * @param rep: reply to check.
342 * @return: true if valid, false if invalid.
344 int reply_check_cname_chain(struct reply_info* rep);
347 * Check security status of all RRs in the message.
348 * @param rep: reply to check
349 * @return: true if all RRs are secure. False if not.
350 * True if there are zero RRs.
352 int reply_all_rrsets_secure(struct reply_info* rep);
355 * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
356 * result may have a different owner name.
357 * @param qinfo: what to look for.
358 * @param rep: looks in answer section of this message.
359 * @return: pointer to rrset, or NULL if not found.
361 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
362 struct reply_info* rep);
365 * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
366 * @param rep: looks in answer section of this message.
367 * @param name: what to look for.
368 * @param namelen: length of name.
369 * @param type: looks for (host order).
370 * @param dclass: looks for (host order).
371 * @return: pointer to rrset, or NULL if not found.
373 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
374 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
377 * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
378 * @param rep: looks in authority section of this message.
379 * @param name: what to look for.
380 * @param namelen: length of name.
381 * @param type: looks for (host order).
382 * @param dclass: looks for (host order).
383 * @return: pointer to rrset, or NULL if not found.
385 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
386 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
389 * Find rrset in reply, inside any section. Does not follow CNAMEs.
390 * @param rep: looks in answer,authority and additional section of this message.
391 * @param name: what to look for.
392 * @param namelen: length of name.
393 * @param type: looks for (host order).
394 * @param dclass: looks for (host order).
395 * @return: pointer to rrset, or NULL if not found.
397 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
398 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
401 * Debug send the query info and reply info to the log in readable form.
402 * @param str: descriptive string printed with packet content.
403 * @param qinfo: query section.
404 * @param rep: rest of message.
406 void log_dns_msg(const char* str, struct query_info* qinfo,
407 struct reply_info* rep);
410 * Print string with neat domain name, type, class from query info.
411 * @param v: at what verbosity level to print this.
412 * @param str: string of message.
413 * @param qinf: query info structure with name, type and class.
415 void log_query_info(enum verbosity_value v, const char* str,
416 struct query_info* qinf);
418 #endif /* UTIL_DATA_MSGREPLY_H */