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);
289 * Sorts the ref array.
290 * @param rep: reply info. rrsets must be filled in.
292 void reply_info_sortref(struct reply_info* rep);
295 * Set TTLs inside the replyinfo to absolute values.
296 * @param rep: reply info. rrsets must be filled in.
297 * Also refs must be filled in.
298 * @param timenow: the current time.
300 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
303 * Delete reply_info and packed_rrsets (while they are not yet added to the
304 * hashtables.). Returns rrsets to the alloc cache.
305 * @param rep: reply_info to delete.
306 * @param alloc: where to return rrset structures to.
308 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
311 * Compare two queryinfo structures, on query and type, class.
312 * It is _not_ sorted in canonical ordering.
313 * @param m1: struct query_info* , void* here to ease use as function pointer.
314 * @param m2: struct query_info* , void* here to ease use as function pointer.
315 * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
317 int query_info_compare(void* m1, void* m2);
319 /** clear out query info structure */
320 void query_info_clear(struct query_info* m);
322 /** calculate size of struct query_info + reply_info */
323 size_t msgreply_sizefunc(void* k, void* d);
325 /** delete msgreply_entry key structure */
326 void query_entry_delete(void *q, void* arg);
328 /** delete reply_info data structure */
329 void reply_info_delete(void* d, void* arg);
331 /** calculate hash value of query_info, lowercases the qname,
332 * uses CD flag for AAAA qtype */
333 hashvalue_type query_info_hash(struct query_info *q, uint16_t flags);
336 * Setup query info entry
337 * @param q: query info to copy. Emptied as if clear is called.
338 * @param r: reply to init data.
339 * @param h: hash value.
340 * @return: newly allocated message reply cache item.
342 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
343 struct reply_info* r, hashvalue_type h);
346 * Copy reply_info and all rrsets in it and allocate.
347 * @param rep: what to copy, probably inside region, no ref[] array in it.
348 * @param alloc: how to allocate rrset keys.
349 * Not used if region!=NULL, it can be NULL in that case.
350 * @param region: if this parameter is NULL then malloc and the alloc is used.
351 * otherwise, everything is allocated in this region.
352 * In a region, no special rrset key structures are needed (not shared),
353 * and no rrset_ref array in the reply is built up.
354 * @return new reply info or NULL on memory error.
356 struct reply_info* reply_info_copy(struct reply_info* rep,
357 struct alloc_cache* alloc, struct regional* region);
360 * Allocate (special) rrset keys.
361 * @param rep: reply info in which the rrset keys to be allocated, rrset[]
362 * array should have bee allocated with NULL pointers.
363 * @param alloc: how to allocate rrset keys.
364 * Not used if region!=NULL, it can be NULL in that case.
365 * @param region: if this parameter is NULL then the alloc is used.
366 * otherwise, rrset keys are allocated in this region.
367 * In a region, no special rrset key structures are needed (not shared).
368 * and no rrset_ref array in the reply needs to be built up.
369 * @return 1 on success, 0 on error
371 int reply_info_alloc_rrset_keys(struct reply_info* rep,
372 struct alloc_cache* alloc, struct regional* region);
375 * Copy a parsed rrset into given key, decompressing and allocating rdata.
376 * @param pkt: packet for decompression
377 * @param msg: the parser message (for flags for trust).
378 * @param pset: the parsed rrset to copy.
379 * @param region: if NULL - malloc, else data is allocated in this region.
380 * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
381 * will be set on return.
382 * Note that TTL will still be relative on return.
383 * @return false on alloc failure.
385 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
386 struct rrset_parse *pset, struct regional* region,
387 struct ub_packed_rrset_key* pk);
390 * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
391 * @param qinfo: what to start with.
392 * @param rep: looks in answer section of this message.
393 * @return: pointer dname, or NULL if not found.
395 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
396 struct reply_info* rep);
399 * Check if cname chain in cached reply is still valid.
400 * @param qinfo: query info with query name.
401 * @param rep: reply to check.
402 * @return: true if valid, false if invalid.
404 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep);
407 * Check security status of all RRs in the message.
408 * @param rep: reply to check
409 * @return: true if all RRs are secure. False if not.
410 * True if there are zero RRs.
412 int reply_all_rrsets_secure(struct reply_info* rep);
415 * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
416 * result may have a different owner name.
417 * @param qinfo: what to look for.
418 * @param rep: looks in answer section of this message.
419 * @return: pointer to rrset, or NULL if not found.
421 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
422 struct reply_info* rep);
425 * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
426 * @param rep: looks in answer section of this message.
427 * @param name: what to look for.
428 * @param namelen: length of name.
429 * @param type: looks for (host order).
430 * @param dclass: looks for (host order).
431 * @return: pointer to rrset, or NULL if not found.
433 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
434 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
437 * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
438 * @param rep: looks in authority section of this message.
439 * @param name: what to look for.
440 * @param namelen: length of name.
441 * @param type: looks for (host order).
442 * @param dclass: looks for (host order).
443 * @return: pointer to rrset, or NULL if not found.
445 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
446 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
449 * Find rrset in reply, inside any section. Does not follow CNAMEs.
450 * @param rep: looks in answer,authority and additional section of this message.
451 * @param name: what to look for.
452 * @param namelen: length of name.
453 * @param type: looks for (host order).
454 * @param dclass: looks for (host order).
455 * @return: pointer to rrset, or NULL if not found.
457 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
458 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
461 * Debug send the query info and reply info to the log in readable form.
462 * @param str: descriptive string printed with packet content.
463 * @param qinfo: query section.
464 * @param rep: rest of message.
466 void log_dns_msg(const char* str, struct query_info* qinfo,
467 struct reply_info* rep);
470 * Print string with neat domain name, type, class,
471 * status code from, and size of a query response.
473 * @param v: at what verbosity level to print this.
474 * @param qinf: query section.
475 * @param addr: address of the client.
476 * @param addrlen: length of the client address.
477 * @param dur: how long it took to complete the query.
478 * @param cached: whether or not the reply is coming from
479 * the cache, or an outside network.
480 * @param rmsg: sldns buffer packet.
482 void log_reply_info(enum verbosity_value v, struct query_info *qinf,
483 struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
484 int cached, struct sldns_buffer *rmsg);
487 * Print string with neat domain name, type, class from query info.
488 * @param v: at what verbosity level to print this.
489 * @param str: string of message.
490 * @param qinf: query info structure with name, type and class.
492 void log_query_info(enum verbosity_value v, const char* str,
493 struct query_info* qinf);
496 * Append edns option to edns data structure
497 * @param edns: the edns data structure to append the edns option to.
498 * @param region: region to allocate the new edns option.
499 * @param code: the edns option's code.
500 * @param len: the edns option's length.
501 * @param data: the edns option's data.
502 * @return false on failure.
504 int edns_opt_append(struct edns_data* edns, struct regional* region,
505 uint16_t code, size_t len, uint8_t* data);
508 * Append edns option to edns option list
509 * @param list: the edns option list to append the edns option to.
510 * @param code: the edns option's code.
511 * @param len: the edns option's length.
512 * @param data: the edns option's data.
513 * @param region: region to allocate the new edns option.
514 * @return false on failure.
516 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
517 uint8_t* data, struct regional* region);
520 * Remove any option found on the edns option list that matches the code.
521 * @param list: the list of edns options.
522 * @param code: the opt code to remove.
523 * @return true when at least one edns option was removed, false otherwise.
525 int edns_opt_list_remove(struct edns_option** list, uint16_t code);
528 * Find edns option in edns list
529 * @param list: list of edns options (eg. edns.opt_list)
530 * @param code: opt code to find.
531 * @return NULL or the edns_option element.
533 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code);
536 * Call the registered functions in the inplace_cb_reply linked list.
537 * This function is going to get called while answering with a resolved query.
538 * @param env: module environment.
539 * @param qinfo: query info.
540 * @param qstate: module qstate.
541 * @param rep: Reply info. Could be NULL.
542 * @param rcode: return code.
543 * @param edns: edns data of the reply.
544 * @param region: region to store data.
545 * @return false on failure (a callback function returned an error).
547 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
548 struct module_qstate* qstate, struct reply_info* rep, int rcode,
549 struct edns_data* edns, struct regional* region);
552 * Call the registered functions in the inplace_cb_reply_cache linked list.
553 * This function is going to get called while answering from cache.
554 * @param env: module environment.
555 * @param qinfo: query info.
556 * @param qstate: module qstate. NULL when replying from cache.
557 * @param rep: Reply info.
558 * @param rcode: return code.
559 * @param edns: edns data of the reply. Edns input can be found here.
560 * @param region: region to store data.
561 * @return false on failure (a callback function returned an error).
563 int inplace_cb_reply_cache_call(struct module_env* env,
564 struct query_info* qinfo, struct module_qstate* qstate,
565 struct reply_info* rep, int rcode, struct edns_data* edns,
566 struct regional* region);
569 * Call the registered functions in the inplace_cb_reply_local linked list.
570 * This function is going to get called while answering with local data.
571 * @param env: module environment.
572 * @param qinfo: query info.
573 * @param qstate: module qstate. NULL when replying from cache.
574 * @param rep: Reply info.
575 * @param rcode: return code.
576 * @param edns: edns data of the reply. Edns input can be found here.
577 * @param region: region to store data.
578 * @return false on failure (a callback function returned an error).
580 int inplace_cb_reply_local_call(struct module_env* env,
581 struct query_info* qinfo, struct module_qstate* qstate,
582 struct reply_info* rep, int rcode, struct edns_data* edns,
583 struct regional* region);
586 * Call the registered functions in the inplace_cb_reply linked list.
587 * This function is going to get called while answering with a servfail.
588 * @param env: module environment.
589 * @param qinfo: query info.
590 * @param qstate: module qstate. Contains the edns option lists. Could be NULL.
591 * @param rep: Reply info. NULL when servfail.
592 * @param rcode: return code. LDNS_RCODE_SERVFAIL.
593 * @param edns: edns data of the reply. Edns input can be found here if qstate
595 * @param region: region to store data.
596 * @return false on failure (a callback function returned an error).
598 int inplace_cb_reply_servfail_call(struct module_env* env,
599 struct query_info* qinfo, struct module_qstate* qstate,
600 struct reply_info* rep, int rcode, struct edns_data* edns,
601 struct regional* region);
604 * Call the registered functions in the inplace_cb_query linked list.
605 * This function is going to get called just before sending a query to a
607 * @param env: module environment.
608 * @param qinfo: query info.
609 * @param flags: flags of the query.
610 * @param addr: to which server to send the query.
611 * @param addrlen: length of addr.
612 * @param zone: name of the zone of the delegation point. wireformat dname.
613 * This is the delegation point name for which the server is deemed
615 * @param zonelen: length of zone.
616 * @param qstate: module qstate.
617 * @param region: region to store data.
618 * @return false on failure (a callback function returned an error).
620 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
621 uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
622 uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
623 struct regional* region);
626 * Call the registered functions in the inplace_cb_edns_back_parsed linked list.
627 * This function is going to get called after parsing the EDNS data on the
628 * reply from a nameserver.
629 * @param env: module environment.
630 * @param qstate: module qstate.
631 * @return false on failure (a callback function returned an error).
633 int inplace_cb_edns_back_parsed_call(struct module_env* env,
634 struct module_qstate* qstate);
637 * Call the registered functions in the inplace_cb_query_reponse linked list.
638 * This function is going to get called after receiving a reply from a
640 * @param env: module environment.
641 * @param qstate: module qstate.
642 * @param response: received response
643 * @return false on failure (a callback function returned an error).
645 int inplace_cb_query_response_call(struct module_env* env,
646 struct module_qstate* qstate, struct dns_msg* response);
649 * Copy edns option list allocated to the new region
651 struct edns_option* edns_opt_copy_region(struct edns_option* list,
652 struct regional* region);
655 * Copy edns option list allocated with malloc
657 struct edns_option* edns_opt_copy_alloc(struct edns_option* list);
660 * Free edns option list allocated with malloc
662 void edns_opt_list_free(struct edns_option* list);
665 * Compare an edns option. (not entire list). Also compares contents.
667 int edns_opt_compare(struct edns_option* p, struct edns_option* q);
670 * Compare edns option lists, also the order and contents of edns-options.
672 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q);
674 #endif /* UTIL_DATA_MSGREPLY_H */