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