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"
61 /** calculate the prefetch TTL as 90% of original. Calculation
62 * without numerical overflow (uin32_t) */
63 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
66 * Structure to store query information that makes answers to queries
71 * Salient data on the query: qname, in wireformat.
72 * can be allocated or a pointer to outside buffer.
73 * User has to keep track on the status of this.
76 /** length of qname (including last 0 octet) */
78 /** qtype, host byte order */
80 /** qclass, host byte order */
83 * Alias local answer(s) for the qname. If 'qname' is an alias defined
84 * in a local zone, this field will be set to the corresponding local
85 * RRset when the alias is determined.
86 * In the initial implementation this can only be a single CNAME RR
87 * (or NULL), but it could possibly be extended to be a DNAME or a
89 * Users of this structure are responsible to initialize this field
90 * to be NULL; otherwise other part of query handling code may be
92 * Users also have to be careful about the lifetime of data. On return
93 * from local zone lookup, it may point to data derived from
94 * configuration that may be dynamically invalidated or data allocated
95 * in an ephemeral regional allocator. A deep copy of the data may
96 * have to be generated if it has to be kept during iterative
98 struct local_rrset* local_alias;
102 * Information to reference an rrset
105 /** the key with lock, and ptr to packed data. */
106 struct ub_packed_rrset_key* key;
112 * Structure to store DNS query and the reply packet.
113 * To use it, copy over the flags from reply and modify using flags from
114 * the query (RD,CD if not AA). prepend ID.
119 * o packed_rrset_key* array.
121 * Memory layout is sometimes not packed, when the message is synthesized,
122 * for easy of the generation. It is allocated packed when it is copied
123 * from the region allocation to the malloc allocation.
126 /** the flags for the answer, host byte order. */
130 * This flag informs unbound the answer is authoritative and
131 * the AA flag should be preserved.
133 uint8_t authoritative;
136 * Number of RRs in the query section.
137 * If qdcount is not 0, then it is 1, and the data that appears
138 * in the reply is the same as the query_info.
143 /** 32 bit padding to pad struct member alignment to 64 bits. */
147 * TTL of the entire reply (for negative caching).
148 * only for use when there are 0 RRsets in this message.
149 * if there are RRsets, check those instead.
154 * TTL for prefetch. After it has expired, a prefetch is suitable.
155 * Smaller than the TTL, otherwise the prefetch would not happen.
160 * The security status from DNSSEC validation of this message.
162 enum sec_status security;
165 * Number of RRsets in each section.
166 * The answer section. Add up the RRs in every RRset to calculate
167 * the number of RRs, and the count for the dns packet.
168 * The number of RRs in RRsets can change due to RRset updates.
172 /** Count of authority section RRsets */
174 /** Count of additional section RRsets */
177 /** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
181 * List of pointers (only) to the rrsets in the order in which
182 * they appear in the reply message.
183 * Number of elements is ancount+nscount+arcount RRsets.
184 * This is a pointer to that array.
185 * Use the accessor function for access.
187 struct ub_packed_rrset_key** rrsets;
190 * Packed array of ids (see counts) and pointers to packed_rrset_key.
191 * The number equals ancount+nscount+arcount RRsets.
192 * These are sorted in ascending pointer, the locking order. So
193 * this list can be locked (and id, ttl checked), to see if
194 * all the data is available and recent enough.
196 * This is defined as an array of size 1, so that the compiler
197 * associates the identifier with this position in the structure.
198 * Array bound overflow on this array then gives access to the further
199 * elements of the array, which are allocated after the main structure.
201 * It could be more pure to define as array of size 0, ref[0].
202 * But ref[1] may be less confusing for compilers.
203 * Use the accessor function for access.
205 struct rrset_ref ref[1];
209 * Structure to keep hash table entry for message replies.
211 struct msgreply_entry {
212 /** the hash table key */
213 struct query_info key;
214 /** the hash table entry, data is struct reply_info* */
215 struct lruhash_entry entry;
219 * Constructor for replyinfo.
220 * @param region: where to allocate the results, pass NULL to use malloc.
221 * @param flags: flags for the replyinfo.
222 * @param qd: qd count
223 * @param ttl: TTL of replyinfo
224 * @param prettl: prefetch ttl
225 * @param an: an count
226 * @param ns: ns count
227 * @param ar: ar count
228 * @param total: total rrset count (presumably an+ns+ar).
229 * @param sec: security status of the reply info.
230 * @return the reply_info base struct with the array for putting the rrsets
231 * in. The array has been zeroed. Returns NULL on malloc failure.
234 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
235 time_t ttl, time_t prettl, size_t an, size_t ns, size_t ar,
236 size_t total, enum sec_status sec);
239 * Parse wire query into a queryinfo structure, return 0 on parse error.
240 * initialises the (prealloced) queryinfo structure as well.
241 * This query structure contains a pointer back info the buffer!
242 * This pointer avoids memory allocation. allocqname does memory allocation.
243 * @param m: the prealloced queryinfo structure to put query into.
244 * must be unused, or _clear()ed.
245 * @param query: the wireformat packet query. starts with ID.
246 * @return: 0 on format error.
248 int query_info_parse(struct query_info* m, struct sldns_buffer* query);
252 * Fills in preallocated query_info structure (with ptr into buffer).
253 * Allocates reply_info and packed_rrsets. These are not yet added to any
254 * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
255 * @param pkt: the packet buffer. Must be positioned after the query section.
256 * @param alloc: creates packed rrset key structures.
257 * @param rep: allocated reply_info is returned (only on no error).
258 * @param qinf: query_info is returned (only on no error).
259 * @param region: where to store temporary data (for parsing).
260 * @param edns: where to store edns information, does not need to be inited.
261 * @return: zero is OK, or DNS error code in case of error
262 * o FORMERR for parse errors.
263 * o SERVFAIL for memory allocation errors.
265 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc,
266 struct query_info* qinf, struct reply_info** rep,
267 struct regional* region, struct edns_data* edns);
270 * Allocate and decompress parsed message and rrsets.
271 * @param pkt: for name decompression.
272 * @param msg: parsed message in scratch region.
273 * @param alloc: alloc cache for special rrset key structures.
274 * Not used if region!=NULL, it can be NULL in that case.
275 * @param qinf: where to store query info.
276 * qinf itself is allocated by the caller.
277 * @param rep: reply info is allocated and returned.
278 * @param region: if this parameter is NULL then malloc and the alloc is used.
279 * otherwise, everything is allocated in this region.
280 * In a region, no special rrset key structures are needed (not shared),
281 * and no rrset_ref array in the reply is built up.
282 * @return 0 if allocation failed.
284 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
285 struct alloc_cache* alloc, struct query_info* qinf,
286 struct reply_info** rep, struct regional* region);
288 /** get msg reply struct (in temp region) */
289 struct reply_info* parse_reply_in_temp_region(struct sldns_buffer* pkt,
290 struct regional* region, struct query_info* qi);
293 * Sorts the ref array.
294 * @param rep: reply info. rrsets must be filled in.
296 void reply_info_sortref(struct reply_info* rep);
299 * Set TTLs inside the replyinfo to absolute values.
300 * @param rep: reply info. rrsets must be filled in.
301 * Also refs must be filled in.
302 * @param timenow: the current time.
304 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
307 * Delete reply_info and packed_rrsets (while they are not yet added to the
308 * hashtables.). Returns rrsets to the alloc cache.
309 * @param rep: reply_info to delete.
310 * @param alloc: where to return rrset structures to.
312 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
315 * Compare two queryinfo structures, on query and type, class.
316 * It is _not_ sorted in canonical ordering.
317 * @param m1: struct query_info* , void* here to ease use as function pointer.
318 * @param m2: struct query_info* , void* here to ease use as function pointer.
319 * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
321 int query_info_compare(void* m1, void* m2);
323 /** clear out query info structure */
324 void query_info_clear(struct query_info* m);
326 /** calculate size of struct query_info + reply_info */
327 size_t msgreply_sizefunc(void* k, void* d);
329 /** delete msgreply_entry key structure */
330 void query_entry_delete(void *q, void* arg);
332 /** delete reply_info data structure */
333 void reply_info_delete(void* d, void* arg);
335 /** calculate hash value of query_info, lowercases the qname,
336 * uses CD flag for AAAA qtype */
337 hashvalue_type query_info_hash(struct query_info *q, uint16_t flags);
340 * Setup query info entry
341 * @param q: query info to copy. Emptied as if clear is called.
342 * @param r: reply to init data.
343 * @param h: hash value.
344 * @return: newly allocated message reply cache item.
346 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
347 struct reply_info* r, hashvalue_type h);
350 * Copy reply_info and all rrsets in it and allocate.
351 * @param rep: what to copy, probably inside region, no ref[] array in it.
352 * @param alloc: how to allocate rrset keys.
353 * Not used if region!=NULL, it can be NULL in that case.
354 * @param region: if this parameter is NULL then malloc and the alloc is used.
355 * otherwise, everything is allocated in this region.
356 * In a region, no special rrset key structures are needed (not shared),
357 * and no rrset_ref array in the reply is built up.
358 * @return new reply info or NULL on memory error.
360 struct reply_info* reply_info_copy(struct reply_info* rep,
361 struct alloc_cache* alloc, struct regional* region);
364 * Allocate (special) rrset keys.
365 * @param rep: reply info in which the rrset keys to be allocated, rrset[]
366 * array should have bee allocated with NULL pointers.
367 * @param alloc: how to allocate rrset keys.
368 * Not used if region!=NULL, it can be NULL in that case.
369 * @param region: if this parameter is NULL then the alloc is used.
370 * otherwise, rrset keys are allocated in this region.
371 * In a region, no special rrset key structures are needed (not shared).
372 * and no rrset_ref array in the reply needs to be built up.
373 * @return 1 on success, 0 on error
375 int reply_info_alloc_rrset_keys(struct reply_info* rep,
376 struct alloc_cache* alloc, struct regional* region);
379 * Copy a parsed rrset into given key, decompressing and allocating rdata.
380 * @param pkt: packet for decompression
381 * @param msg: the parser message (for flags for trust).
382 * @param pset: the parsed rrset to copy.
383 * @param region: if NULL - malloc, else data is allocated in this region.
384 * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
385 * will be set on return.
386 * Note that TTL will still be relative on return.
387 * @return false on alloc failure.
389 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
390 struct rrset_parse *pset, struct regional* region,
391 struct ub_packed_rrset_key* pk);
394 * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
395 * @param qinfo: what to start with.
396 * @param rep: looks in answer section of this message.
397 * @return: pointer dname, or NULL if not found.
399 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
400 struct reply_info* rep);
403 * Check if cname chain in cached reply is still valid.
404 * @param qinfo: query info with query name.
405 * @param rep: reply to check.
406 * @return: true if valid, false if invalid.
408 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep);
411 * Check security status of all RRs in the message.
412 * @param rep: reply to check
413 * @return: true if all RRs are secure. False if not.
414 * True if there are zero RRs.
416 int reply_all_rrsets_secure(struct reply_info* rep);
419 * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
420 * result may have a different owner name.
421 * @param qinfo: what to look for.
422 * @param rep: looks in answer section of this message.
423 * @return: pointer to rrset, or NULL if not found.
425 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
426 struct reply_info* rep);
429 * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
430 * @param rep: looks in answer section of this message.
431 * @param name: what to look for.
432 * @param namelen: length of name.
433 * @param type: looks for (host order).
434 * @param dclass: looks for (host order).
435 * @return: pointer to rrset, or NULL if not found.
437 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
438 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
441 * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
442 * @param rep: looks in authority section of this message.
443 * @param name: what to look for.
444 * @param namelen: length of name.
445 * @param type: looks for (host order).
446 * @param dclass: looks for (host order).
447 * @return: pointer to rrset, or NULL if not found.
449 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
450 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
453 * Find rrset in reply, inside any section. Does not follow CNAMEs.
454 * @param rep: looks in answer,authority and additional section of this message.
455 * @param name: what to look for.
456 * @param namelen: length of name.
457 * @param type: looks for (host order).
458 * @param dclass: looks for (host order).
459 * @return: pointer to rrset, or NULL if not found.
461 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
462 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
465 * Debug send the query info and reply info to the log in readable form.
466 * @param str: descriptive string printed with packet content.
467 * @param qinfo: query section.
468 * @param rep: rest of message.
470 void log_dns_msg(const char* str, struct query_info* qinfo,
471 struct reply_info* rep);
474 * Print string with neat domain name, type, class,
475 * status code from, and size of a query response.
477 * @param v: at what verbosity level to print this.
478 * @param qinf: query section.
479 * @param addr: address of the client.
480 * @param addrlen: length of the client address.
481 * @param dur: how long it took to complete the query.
482 * @param cached: whether or not the reply is coming from
483 * the cache, or an outside network.
484 * @param rmsg: sldns buffer packet.
486 void log_reply_info(enum verbosity_value v, struct query_info *qinf,
487 struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
488 int cached, struct sldns_buffer *rmsg);
491 * Print string with neat domain name, type, class from query info.
492 * @param v: at what verbosity level to print this.
493 * @param str: string of message.
494 * @param qinf: query info structure with name, type and class.
496 void log_query_info(enum verbosity_value v, const char* str,
497 struct query_info* qinf);
500 * Append edns option to edns data structure
501 * @param edns: the edns data structure to append the edns option to.
502 * @param region: region to allocate the new edns option.
503 * @param code: the edns option's code.
504 * @param len: the edns option's length.
505 * @param data: the edns option's data.
506 * @return false on failure.
508 int edns_opt_append(struct edns_data* edns, struct regional* region,
509 uint16_t code, size_t len, uint8_t* data);
512 * Append edns option to edns option list
513 * @param list: the edns option list to append the edns option to.
514 * @param code: the edns option's code.
515 * @param len: the edns option's length.
516 * @param data: the edns option's data.
517 * @param region: region to allocate the new edns option.
518 * @return false on failure.
520 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
521 uint8_t* data, struct regional* region);
524 * Remove any option found on the edns option list that matches the code.
525 * @param list: the list of edns options.
526 * @param code: the opt code to remove.
527 * @return true when at least one edns option was removed, false otherwise.
529 int edns_opt_list_remove(struct edns_option** list, uint16_t code);
532 * Find edns option in edns list
533 * @param list: list of edns options (eg. edns.opt_list)
534 * @param code: opt code to find.
535 * @return NULL or the edns_option element.
537 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code);
540 * Call the registered functions in the inplace_cb_reply linked list.
541 * This function is going to get called while answering with a resolved query.
542 * @param env: module environment.
543 * @param qinfo: query info.
544 * @param qstate: module qstate.
545 * @param rep: Reply info. Could be NULL.
546 * @param rcode: return code.
547 * @param edns: edns data of the reply.
548 * @param region: region to store data.
549 * @return false on failure (a callback function returned an error).
551 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
552 struct module_qstate* qstate, struct reply_info* rep, int rcode,
553 struct edns_data* edns, struct regional* region);
556 * Call the registered functions in the inplace_cb_reply_cache linked list.
557 * This function is going to get called while answering from cache.
558 * @param env: module environment.
559 * @param qinfo: query info.
560 * @param qstate: module qstate. NULL when replying from cache.
561 * @param rep: Reply info.
562 * @param rcode: return code.
563 * @param edns: edns data of the reply. Edns input can be found here.
564 * @param region: region to store data.
565 * @return false on failure (a callback function returned an error).
567 int inplace_cb_reply_cache_call(struct module_env* env,
568 struct query_info* qinfo, struct module_qstate* qstate,
569 struct reply_info* rep, int rcode, struct edns_data* edns,
570 struct regional* region);
573 * Call the registered functions in the inplace_cb_reply_local linked list.
574 * This function is going to get called while answering with local data.
575 * @param env: module environment.
576 * @param qinfo: query info.
577 * @param qstate: module qstate. NULL when replying from cache.
578 * @param rep: Reply info.
579 * @param rcode: return code.
580 * @param edns: edns data of the reply. Edns input can be found here.
581 * @param region: region to store data.
582 * @return false on failure (a callback function returned an error).
584 int inplace_cb_reply_local_call(struct module_env* env,
585 struct query_info* qinfo, struct module_qstate* qstate,
586 struct reply_info* rep, int rcode, struct edns_data* edns,
587 struct regional* region);
590 * Call the registered functions in the inplace_cb_reply linked list.
591 * This function is going to get called while answering with a servfail.
592 * @param env: module environment.
593 * @param qinfo: query info.
594 * @param qstate: module qstate. Contains the edns option lists. Could be NULL.
595 * @param rep: Reply info. NULL when servfail.
596 * @param rcode: return code. LDNS_RCODE_SERVFAIL.
597 * @param edns: edns data of the reply. Edns input can be found here if qstate
599 * @param region: region to store data.
600 * @return false on failure (a callback function returned an error).
602 int inplace_cb_reply_servfail_call(struct module_env* env,
603 struct query_info* qinfo, struct module_qstate* qstate,
604 struct reply_info* rep, int rcode, struct edns_data* edns,
605 struct regional* region);
608 * Call the registered functions in the inplace_cb_query linked list.
609 * This function is going to get called just before sending a query to a
611 * @param env: module environment.
612 * @param qinfo: query info.
613 * @param flags: flags of the query.
614 * @param addr: to which server to send the query.
615 * @param addrlen: length of addr.
616 * @param zone: name of the zone of the delegation point. wireformat dname.
617 * This is the delegation point name for which the server is deemed
619 * @param zonelen: length of zone.
620 * @param qstate: module qstate.
621 * @param region: region to store data.
622 * @return false on failure (a callback function returned an error).
624 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
625 uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
626 uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
627 struct regional* region);
630 * Call the registered functions in the inplace_cb_edns_back_parsed linked list.
631 * This function is going to get called after parsing the EDNS data on the
632 * reply from a nameserver.
633 * @param env: module environment.
634 * @param qstate: module qstate.
635 * @return false on failure (a callback function returned an error).
637 int inplace_cb_edns_back_parsed_call(struct module_env* env,
638 struct module_qstate* qstate);
641 * Call the registered functions in the inplace_cb_query_response linked list.
642 * This function is going to get called after receiving a reply from a
644 * @param env: module environment.
645 * @param qstate: module qstate.
646 * @param response: received response
647 * @return false on failure (a callback function returned an error).
649 int inplace_cb_query_response_call(struct module_env* env,
650 struct module_qstate* qstate, struct dns_msg* response);
653 * Copy edns option list allocated to the new region
655 struct edns_option* edns_opt_copy_region(struct edns_option* list,
656 struct regional* region);
659 * Copy edns option list allocated with malloc
661 struct edns_option* edns_opt_copy_alloc(struct edns_option* list);
664 * Free edns option list allocated with malloc
666 void edns_opt_list_free(struct edns_option* list);
669 * Compare an edns option. (not entire list). Also compares contents.
671 int edns_opt_compare(struct edns_option* p, struct edns_option* q);
674 * Compare edns option lists, also the order and contents of edns-options.
676 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q);
678 #endif /* UTIL_DATA_MSGREPLY_H */