xref: /freebsd/contrib/unbound/util/data/msgreply.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*
2  * util/data/msgreply.h - store message and reply data.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains a data structure to store a message and its reply.
40  */
41 
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"
46 #include "sldns/rrdef.h"
47 struct sldns_buffer;
48 struct comm_reply;
49 struct alloc_cache;
50 struct iovec;
51 struct regional;
52 struct edns_data;
53 struct edns_option;
54 struct inplace_cb;
55 struct module_qstate;
56 struct module_env;
57 struct msg_parse;
58 struct rrset_parse;
59 struct local_rrset;
60 struct dns_msg;
61 
62 /** calculate the prefetch TTL as 90% of original. Calculation
63  * without numerical overflow (uin32_t) */
64 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
65 
66 /**
67  * Structure to store query information that makes answers to queries
68  * different.
69  */
70 struct query_info {
71 	/**
72 	 * Salient data on the query: qname, in wireformat.
73 	 * can be allocated or a pointer to outside buffer.
74 	 * User has to keep track on the status of this.
75 	 */
76 	uint8_t* qname;
77 	/** length of qname (including last 0 octet) */
78 	size_t qname_len;
79 	/** qtype, host byte order */
80 	uint16_t qtype;
81 	/** qclass, host byte order */
82 	uint16_t qclass;
83 	/**
84 	 * Alias local answer(s) for the qname.  If 'qname' is an alias defined
85 	 * in a local zone, this field will be set to the corresponding local
86 	 * RRset when the alias is determined.
87 	 * In the initial implementation this can only be a single CNAME RR
88 	 * (or NULL), but it could possibly be extended to be a DNAME or a
89 	 * chain of aliases.
90 	 * Users of this structure are responsible to initialize this field
91 	 * to be NULL; otherwise other part of query handling code may be
92 	 * confused.
93 	 * Users also have to be careful about the lifetime of data.  On return
94 	 * from local zone lookup, it may point to data derived from
95 	 * configuration that may be dynamically invalidated or data allocated
96 	 * in an ephemeral regional allocator.  A deep copy of the data may
97 	 * have to be generated if it has to be kept during iterative
98 	 * resolution. */
99 	struct local_rrset* local_alias;
100 };
101 
102 /**
103  * Information to reference an rrset
104  */
105 struct rrset_ref {
106 	/** the key with lock, and ptr to packed data. */
107 	struct ub_packed_rrset_key* key;
108 	/** id needed */
109 	rrset_id_type id;
110 };
111 
112 /**
113  * Structure to store DNS query and the reply packet.
114  * To use it, copy over the flags from reply and modify using flags from
115  * the query (RD,CD if not AA). prepend ID.
116  *
117  * Memory layout is:
118  *	o struct
119  *	o rrset_ref array
120  *	o packed_rrset_key* array.
121  *
122  * Memory layout is sometimes not packed, when the message is synthesized,
123  * for easy of the generation. It is allocated packed when it is copied
124  * from the region allocation to the malloc allocation.
125  */
126 struct reply_info {
127 	/** the flags for the answer, host byte order. */
128 	uint16_t flags;
129 
130 	/**
131 	 * This flag informs unbound the answer is authoritative and
132 	 * the AA flag should be preserved.
133 	 */
134 	uint8_t authoritative;
135 
136 	/**
137 	 * Number of RRs in the query section.
138 	 * If qdcount is not 0, then it is 1, and the data that appears
139 	 * in the reply is the same as the query_info.
140 	 * Host byte order.
141 	 */
142 	uint8_t qdcount;
143 
144 	/** 32 bit padding to pad struct member alignment to 64 bits. */
145 	uint32_t padding;
146 
147 	/**
148 	 * TTL of the entire reply (for negative caching).
149 	 * only for use when there are 0 RRsets in this message.
150 	 * if there are RRsets, check those instead.
151 	 */
152 	time_t ttl;
153 
154 	/**
155 	 * TTL for prefetch. After it has expired, a prefetch is suitable.
156 	 * Smaller than the TTL, otherwise the prefetch would not happen.
157 	 */
158 	time_t prefetch_ttl;
159 
160 	/**
161 	 * Reply TTL extended with serve expired TTL, to limit time to serve
162 	 * expired message.
163 	 */
164 	time_t serve_expired_ttl;
165 
166 	/**
167 	 * The security status from DNSSEC validation of this message.
168 	 */
169 	enum sec_status security;
170 
171 	/**
172 	 * EDE (rfc8914) code with reason for DNSSEC bogus status.
173 	 */
174 	sldns_ede_code reason_bogus;
175 
176 	/**
177 	 * Number of RRsets in each section.
178 	 * The answer section. Add up the RRs in every RRset to calculate
179 	 * the number of RRs, and the count for the dns packet.
180 	 * The number of RRs in RRsets can change due to RRset updates.
181 	 */
182 	size_t an_numrrsets;
183 
184 	/** Count of authority section RRsets */
185 	size_t ns_numrrsets;
186 	/** Count of additional section RRsets */
187 	size_t ar_numrrsets;
188 
189 	/** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
190 	size_t rrset_count;
191 
192 	/**
193 	 * List of pointers (only) to the rrsets in the order in which
194 	 * they appear in the reply message.
195 	 * Number of elements is ancount+nscount+arcount RRsets.
196 	 * This is a pointer to that array.
197 	 * Use the accessor function for access.
198 	 */
199 	struct ub_packed_rrset_key** rrsets;
200 
201 	/**
202 	 * Packed array of ids (see counts) and pointers to packed_rrset_key.
203 	 * The number equals ancount+nscount+arcount RRsets.
204 	 * These are sorted in ascending pointer, the locking order. So
205 	 * this list can be locked (and id, ttl checked), to see if
206 	 * all the data is available and recent enough.
207 	 *
208 	 * This is defined as an array of size 1, so that the compiler
209 	 * associates the identifier with this position in the structure.
210 	 * Array bound overflow on this array then gives access to the further
211 	 * elements of the array, which are allocated after the main structure.
212 	 *
213 	 * It could be more pure to define as array of size 0, ref[0].
214 	 * But ref[1] may be less confusing for compilers.
215 	 * Use the accessor function for access.
216 	 */
217 	struct rrset_ref ref[1];
218 };
219 
220 /**
221  * Structure to keep hash table entry for message replies.
222  */
223 struct msgreply_entry {
224 	/** the hash table key */
225 	struct query_info key;
226 	/** the hash table entry, data is struct reply_info* */
227 	struct lruhash_entry entry;
228 };
229 
230 /**
231  * Constructor for replyinfo.
232  * @param region: where to allocate the results, pass NULL to use malloc.
233  * @param flags: flags for the replyinfo.
234  * @param qd: qd count
235  * @param ttl: TTL of replyinfo
236  * @param prettl: prefetch ttl
237  * @param expttl: serve expired ttl
238  * @param an: an count
239  * @param ns: ns count
240  * @param ar: ar count
241  * @param total: total rrset count (presumably an+ns+ar).
242  * @param sec: security status of the reply info.
243  * @return the reply_info base struct with the array for putting the rrsets
244  * in.  The array has been zeroed.  Returns NULL on malloc failure.
245  */
246 struct reply_info*
247 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
248 		time_t ttl, time_t prettl, time_t expttl, size_t an, size_t ns,
249 		size_t ar, size_t total, enum sec_status sec);
250 
251 /**
252  * Parse wire query into a queryinfo structure, return 0 on parse error.
253  * initialises the (prealloced) queryinfo structure as well.
254  * This query structure contains a pointer back info the buffer!
255  * This pointer avoids memory allocation. allocqname does memory allocation.
256  * @param m: the prealloced queryinfo structure to put query into.
257  *    must be unused, or _clear()ed.
258  * @param query: the wireformat packet query. starts with ID.
259  * @return: 0 on format error.
260  */
261 int query_info_parse(struct query_info* m, struct sldns_buffer* query);
262 
263 /**
264  * Parse query reply.
265  * Fills in preallocated query_info structure (with ptr into buffer).
266  * Allocates reply_info and packed_rrsets. These are not yet added to any
267  * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
268  * @param pkt: the packet buffer. Must be positioned after the query section.
269  * @param alloc: creates packed rrset key structures.
270  * @param rep: allocated reply_info is returned (only on no error).
271  * @param qinf: query_info is returned (only on no error).
272  * @param region: where to store temporary data (for parsing).
273  * @param edns: where to store edns information, does not need to be inited.
274  * @return: zero is OK, or DNS error code in case of error
275  *	o FORMERR for parse errors.
276  *	o SERVFAIL for memory allocation errors.
277  */
278 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc,
279 	struct query_info* qinf, struct reply_info** rep,
280 	struct regional* region, struct edns_data* edns);
281 
282 /**
283  * Allocate and decompress parsed message and rrsets.
284  * @param pkt: for name decompression.
285  * @param msg: parsed message in scratch region.
286  * @param alloc: alloc cache for special rrset key structures.
287  *	Not used if region!=NULL, it can be NULL in that case.
288  * @param qinf: where to store query info.
289  *	qinf itself is allocated by the caller.
290  * @param rep: reply info is allocated and returned.
291  * @param region: if this parameter is NULL then malloc and the alloc is used.
292  *	otherwise, everything is allocated in this region.
293  *	In a region, no special rrset key structures are needed (not shared),
294  *	and no rrset_ref array in the reply is built up.
295  * @return 0 if allocation failed.
296  */
297 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
298         struct alloc_cache* alloc, struct query_info* qinf,
299 	struct reply_info** rep, struct regional* region);
300 
301 /** get msg reply struct (in temp region) */
302 struct reply_info* parse_reply_in_temp_region(struct sldns_buffer* pkt,
303 	struct regional* region, struct query_info* qi);
304 
305 /**
306  * Sorts the ref array.
307  * @param rep: reply info. rrsets must be filled in.
308  */
309 void reply_info_sortref(struct reply_info* rep);
310 
311 /**
312  * Set TTLs inside the replyinfo to absolute values.
313  * @param rep: reply info. rrsets must be filled in.
314  *	Also refs must be filled in.
315  * @param timenow: the current time.
316  */
317 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
318 
319 /**
320  * Delete reply_info and packed_rrsets (while they are not yet added to the
321  * hashtables.). Returns rrsets to the alloc cache.
322  * @param rep: reply_info to delete.
323  * @param alloc: where to return rrset structures to.
324  */
325 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
326 
327 /**
328  * Compare two queryinfo structures, on query and type, class.
329  * It is _not_ sorted in canonical ordering.
330  * @param m1: struct query_info* , void* here to ease use as function pointer.
331  * @param m2: struct query_info* , void* here to ease use as function pointer.
332  * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
333  */
334 int query_info_compare(void* m1, void* m2);
335 
336 /** clear out query info structure */
337 void query_info_clear(struct query_info* m);
338 
339 /** calculate size of struct query_info + reply_info */
340 size_t msgreply_sizefunc(void* k, void* d);
341 
342 /** delete msgreply_entry key structure */
343 void query_entry_delete(void *q, void* arg);
344 
345 /** delete reply_info data structure */
346 void reply_info_delete(void* d, void* arg);
347 
348 /** calculate hash value of query_info, lowercases the qname,
349  * uses CD flag for AAAA qtype */
350 hashvalue_type query_info_hash(struct query_info *q, uint16_t flags);
351 
352 /**
353  * Setup query info entry
354  * @param q: query info to copy. Emptied as if clear is called.
355  * @param r: reply to init data.
356  * @param h: hash value.
357  * @return: newly allocated message reply cache item.
358  */
359 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
360 	struct reply_info* r, hashvalue_type h);
361 
362 /**
363  * Copy reply_info and all rrsets in it and allocate.
364  * @param rep: what to copy, probably inside region, no ref[] array in it.
365  * @param alloc: how to allocate rrset keys.
366  *	Not used if region!=NULL, it can be NULL in that case.
367  * @param region: if this parameter is NULL then malloc and the alloc is used.
368  *	otherwise, everything is allocated in this region.
369  *	In a region, no special rrset key structures are needed (not shared),
370  *	and no rrset_ref array in the reply is built up.
371  * @return new reply info or NULL on memory error.
372  */
373 struct reply_info* reply_info_copy(struct reply_info* rep,
374 	struct alloc_cache* alloc, struct regional* region);
375 
376 /**
377  * Allocate (special) rrset keys.
378  * @param rep: reply info in which the rrset keys to be allocated, rrset[]
379  *	array should have bee allocated with NULL pointers.
380  * @param alloc: how to allocate rrset keys.
381  *	Not used if region!=NULL, it can be NULL in that case.
382  * @param region: if this parameter is NULL then the alloc is used.
383  *	otherwise, rrset keys are allocated in this region.
384  *	In a region, no special rrset key structures are needed (not shared).
385  *	and no rrset_ref array in the reply needs to be built up.
386  * @return 1 on success, 0 on error
387  */
388 int reply_info_alloc_rrset_keys(struct reply_info* rep,
389 	struct alloc_cache* alloc, struct regional* region);
390 
391 /*
392  * Create a new reply_info based on 'rep'.  The new info is based on
393  * the passed 'rep', but ignores any rrsets except for the first 'an_numrrsets'
394  * RRsets in the answer section.  These answer rrsets are copied to the
395  * new info, up to 'copy_rrsets' rrsets (which must not be larger than
396  * 'an_numrrsets').  If an_numrrsets > copy_rrsets, the remaining rrsets array
397  * entries will be kept empty so the caller can fill them later.  When rrsets
398  * are copied, they are shallow copied.  The caller must ensure that the
399  * copied rrsets are valid throughout its lifetime and must provide appropriate
400  * mutex if it can be shared by multiple threads.
401  */
402 struct reply_info *
403 make_new_reply_info(const struct reply_info* rep, struct regional* region,
404 	size_t an_numrrsets, size_t copy_rrsets);
405 
406 /**
407  * Copy a parsed rrset into given key, decompressing and allocating rdata.
408  * @param pkt: packet for decompression
409  * @param msg: the parser message (for flags for trust).
410  * @param pset: the parsed rrset to copy.
411  * @param region: if NULL - malloc, else data is allocated in this region.
412  * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
413  *	will be set on return.
414  *	Note that TTL will still be relative on return.
415  * @return false on alloc failure.
416  */
417 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
418 	struct rrset_parse *pset, struct regional* region,
419 	struct ub_packed_rrset_key* pk);
420 
421 /**
422  * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
423  * @param qinfo: what to start with.
424  * @param rep: looks in answer section of this message.
425  * @return: pointer dname, or NULL if not found.
426  */
427 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
428 	struct reply_info* rep);
429 
430 /**
431  * Check if cname chain in cached reply is still valid.
432  * @param qinfo: query info with query name.
433  * @param rep: reply to check.
434  * @return: true if valid, false if invalid.
435  */
436 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep);
437 
438 /**
439  * Check security status of all RRs in the message.
440  * @param rep: reply to check
441  * @return: true if all RRs are secure. False if not.
442  *    True if there are zero RRs.
443  */
444 int reply_all_rrsets_secure(struct reply_info* rep);
445 
446 /**
447  * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
448  * result may have a different owner name.
449  * @param qinfo: what to look for.
450  * @param rep: looks in answer section of this message.
451  * @return: pointer to rrset, or NULL if not found.
452  */
453 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
454 	struct reply_info* rep);
455 
456 /**
457  * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
458  * @param rep: looks in answer section of this message.
459  * @param name: what to look for.
460  * @param namelen: length of name.
461  * @param type: looks for (host order).
462  * @param dclass: looks for (host order).
463  * @return: pointer to rrset, or NULL if not found.
464  */
465 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
466 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
467 
468 /**
469  * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
470  * @param rep: looks in authority section of this message.
471  * @param name: what to look for.
472  * @param namelen: length of name.
473  * @param type: looks for (host order).
474  * @param dclass: looks for (host order).
475  * @return: pointer to rrset, or NULL if not found.
476  */
477 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
478 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
479 
480 /**
481  * Find rrset in reply, inside any section. Does not follow CNAMEs.
482  * @param rep: looks in answer,authority and additional section of this message.
483  * @param name: what to look for.
484  * @param namelen: length of name.
485  * @param type: looks for (host order).
486  * @param dclass: looks for (host order).
487  * @return: pointer to rrset, or NULL if not found.
488  */
489 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
490 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
491 
492 /**
493  * Debug send the query info and reply info to the log in readable form.
494  * @param str: descriptive string printed with packet content.
495  * @param qinfo: query section.
496  * @param rep: rest of message.
497  */
498 void log_dns_msg(const char* str, struct query_info* qinfo,
499 	struct reply_info* rep);
500 
501 /**
502  * Print string with neat domain name, type, class,
503  * status code from, and size of a query response.
504  *
505  * @param v: at what verbosity level to print this.
506  * @param qinf: query section.
507  * @param addr: address of the client.
508  * @param addrlen: length of the client address.
509  * @param dur: how long it took to complete the query.
510  * @param cached: whether or not the reply is coming from
511  *                    the cache, or an outside network.
512  * @param rmsg: sldns buffer packet.
513  */
514 void log_reply_info(enum verbosity_value v, struct query_info *qinf,
515 	struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
516 	int cached, struct sldns_buffer *rmsg);
517 
518 /**
519  * Print string with neat domain name, type, class from query info.
520  * @param v: at what verbosity level to print this.
521  * @param str: string of message.
522  * @param qinf: query info structure with name, type and class.
523  */
524 void log_query_info(enum verbosity_value v, const char* str,
525 	struct query_info* qinf);
526 
527 /**
528  * Append edns option to edns option list
529  * @param list: the edns option list to append the edns option to.
530  * @param code: the edns option's code.
531  * @param len: the edns option's length.
532  * @param data: the edns option's data.
533  * @param region: region to allocate the new edns option.
534  * @return false on failure.
535  */
536 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
537         uint8_t* data, struct regional* region);
538 
539 /**
540  * Append edns EDE option to edns options list
541  * @param LIST: the edns option list to append the edns option to.
542  * @param REGION: region to allocate the new edns option.
543  * @param CODE: the EDE code.
544  * @param TXT: Additional text for the option
545  */
546 #define EDNS_OPT_LIST_APPEND_EDE(LIST, REGION, CODE, TXT) 		\
547 	do {								\
548 		struct {						\
549 			uint16_t code;					\
550 			char text[sizeof(TXT) - 1];			\
551 		} ede = { htons(CODE), TXT };				\
552                 verbose(VERB_ALGO, "attached EDE code: %d with"		\
553                         " message: %s", CODE, TXT);			\
554 		edns_opt_list_append((LIST), LDNS_EDNS_EDE, 		\
555 			sizeof(uint16_t) + sizeof(TXT) - 1,		\
556 			(void *)&ede, (REGION));			\
557 	} while(0)
558 
559 /**
560  * Append edns EDE option to edns options list
561  * @param list: the edns option list to append the edns option to.
562  * @param region: region to allocate the new edns option.
563  * @param code: the EDE code.
564  * @param txt: Additional text for the option
565  * @return false on failure.
566  */
567 int edns_opt_list_append_ede(struct edns_option** list, struct regional* region,
568 	sldns_ede_code code, const char *txt);
569 
570 /**
571  * Remove any option found on the edns option list that matches the code.
572  * @param list: the list of edns options.
573  * @param code: the opt code to remove.
574  * @return true when at least one edns option was removed, false otherwise.
575  */
576 int edns_opt_list_remove(struct edns_option** list, uint16_t code);
577 
578 /**
579  * Find edns option in edns list
580  * @param list: list of edns options (eg. edns.opt_list)
581  * @param code: opt code to find.
582  * @return NULL or the edns_option element.
583  */
584 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code);
585 
586 /**
587  * Call the registered functions in the inplace_cb_reply linked list.
588  * This function is going to get called while answering with a resolved query.
589  * @param env: module environment.
590  * @param qinfo: query info.
591  * @param qstate: module qstate.
592  * @param rep: Reply info. Could be NULL.
593  * @param rcode: return code.
594  * @param edns: edns data of the reply.
595  * @param repinfo: comm_reply. Reply information for a communication point.
596  * @param region: region to store data.
597  * @param start_time: the start time of recursion, when the packet arrived,
598  * 	or the current time for cache responses.
599  * @return false on failure (a callback function returned an error).
600  */
601 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
602 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
603 	struct edns_data* edns, struct comm_reply* repinfo, struct regional* region,
604 	struct timeval* start_time);
605 
606 /**
607  * Call the registered functions in the inplace_cb_reply_cache linked list.
608  * This function is going to get called while answering from cache.
609  * @param env: module environment.
610  * @param qinfo: query info.
611  * @param qstate: module qstate. NULL when replying from cache.
612  * @param rep: Reply info.
613  * @param rcode: return code.
614  * @param edns: edns data of the reply. Edns input can be found here.
615  * @param repinfo: comm_reply. Reply information for a communication point.
616  * @param region: region to store data.
617  * @param start_time: the start time of recursion, when the packet arrived,
618  * 	or the current time for cache responses.
619  * @return false on failure (a callback function returned an error).
620  */
621 int inplace_cb_reply_cache_call(struct module_env* env,
622 	struct query_info* qinfo, struct module_qstate* qstate,
623 	struct reply_info* rep, int rcode, struct edns_data* edns,
624 	struct comm_reply* repinfo, struct regional* region,
625 	struct timeval* start_time);
626 
627 /**
628  * Call the registered functions in the inplace_cb_reply_local linked list.
629  * This function is going to get called while answering with local data.
630  * @param env: module environment.
631  * @param qinfo: query info.
632  * @param qstate: module qstate. NULL when replying from cache.
633  * @param rep: Reply info.
634  * @param rcode: return code.
635  * @param edns: edns data of the reply. Edns input can be found here.
636  * @param repinfo: comm_reply. Reply information for a communication point.
637  * @param region: region to store data.
638  * @param start_time: the start time of recursion, when the packet arrived,
639  * 	or the current time for cache responses.
640  * @return false on failure (a callback function returned an error).
641  */
642 int inplace_cb_reply_local_call(struct module_env* env,
643 	struct query_info* qinfo, struct module_qstate* qstate,
644 	struct reply_info* rep, int rcode, struct edns_data* edns,
645 	struct comm_reply* repinfo, struct regional* region,
646 	struct timeval* start_time);
647 
648 /**
649  * Call the registered functions in the inplace_cb_reply linked list.
650  * This function is going to get called while answering with a servfail.
651  * @param env: module environment.
652  * @param qinfo: query info.
653  * @param qstate: module qstate. Contains the edns option lists. Could be NULL.
654  * @param rep: Reply info. NULL when servfail.
655  * @param rcode: return code. LDNS_RCODE_SERVFAIL.
656  * @param edns: edns data of the reply. Edns input can be found here if qstate
657  *	is NULL.
658  * @param repinfo: comm_reply. Reply information for a communication point.
659  * @param region: region to store data.
660  * @param start_time: the start time of recursion, when the packet arrived,
661  * 	or the current time for cache responses.
662  * @return false on failure (a callback function returned an error).
663  */
664 int inplace_cb_reply_servfail_call(struct module_env* env,
665 	struct query_info* qinfo, struct module_qstate* qstate,
666 	struct reply_info* rep, int rcode, struct edns_data* edns,
667 	struct comm_reply* repinfo, struct regional* region,
668 	struct timeval* start_time);
669 
670 /**
671  * Call the registered functions in the inplace_cb_query linked list.
672  * This function is going to get called just before sending a query to a
673  * nameserver.
674  * @param env: module environment.
675  * @param qinfo: query info.
676  * @param flags: flags of the query.
677  * @param addr: to which server to send the query.
678  * @param addrlen: length of addr.
679  * @param zone: name of the zone of the delegation point. wireformat dname.
680  *	This is the delegation point name for which the server is deemed
681  *	authoritative.
682  * @param zonelen: length of zone.
683  * @param qstate: module qstate.
684  * @param region: region to store data.
685  * @return false on failure (a callback function returned an error).
686  */
687 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
688 	uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
689 	uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
690 	struct regional* region);
691 
692 /**
693  * Call the registered functions in the inplace_cb_edns_back_parsed linked list.
694  * This function is going to get called after parsing the EDNS data on the
695  * reply from a nameserver.
696  * @param env: module environment.
697  * @param qstate: module qstate.
698  * @return false on failure (a callback function returned an error).
699  */
700 int inplace_cb_edns_back_parsed_call(struct module_env* env,
701 	struct module_qstate* qstate);
702 
703 /**
704  * Call the registered functions in the inplace_cb_query_response linked list.
705  * This function is going to get called after receiving a reply from a
706  * nameserver.
707  * @param env: module environment.
708  * @param qstate: module qstate.
709  * @param response: received response
710  * @return false on failure (a callback function returned an error).
711  */
712 int inplace_cb_query_response_call(struct module_env* env,
713 	struct module_qstate* qstate, struct dns_msg* response);
714 
715 /**
716  * Copy edns option list allocated to the new region
717  */
718 struct edns_option* edns_opt_copy_region(struct edns_option* list,
719 	struct regional* region);
720 
721 /**
722  * Copy edns option list allocated with malloc
723  */
724 struct edns_option* edns_opt_copy_alloc(struct edns_option* list);
725 
726 /**
727  * Free edns option list allocated with malloc
728  */
729 void edns_opt_list_free(struct edns_option* list);
730 
731 /**
732  * Compare an edns option. (not entire list).  Also compares contents.
733  */
734 int edns_opt_compare(struct edns_option* p, struct edns_option* q);
735 
736 /**
737  * Compare edns option lists, also the order and contents of edns-options.
738  */
739 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q);
740 
741 #endif /* UTIL_DATA_MSGREPLY_H */
742