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