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