Home | History | Annotate | Line # | Download | only in data
      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 #ifdef __QNX__
     48 /* For struct timeval */
     49 #include <sys/time.h>
     50 #endif /* __QNX__ */
     51 struct sldns_buffer;
     52 struct comm_reply;
     53 struct alloc_cache;
     54 struct iovec;
     55 struct regional;
     56 struct edns_data;
     57 struct edns_option;
     58 struct inplace_cb;
     59 struct module_qstate;
     60 struct module_env;
     61 struct msg_parse;
     62 struct rrset_parse;
     63 struct local_rrset;
     64 struct dns_msg;
     65 enum comm_point_type;
     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  * Set TTLs inside the replyinfo to the given absolute values.
    345  * @param rep: reply info. rrsets must be filled in.
    346  *	Also refs must be filled in.
    347  * @param ttl: absolute ttl value to be set.
    348  * @param ttl_add: the current time to be used verbatim for ttl_add in the rrsets.
    349  */
    350 void reply_info_absolute_ttls(struct reply_info* rep, time_t ttl, time_t ttl_add);
    351 
    352 /**
    353  * Delete reply_info and packed_rrsets (while they are not yet added to the
    354  * hashtables.). Returns rrsets to the alloc cache.
    355  * @param rep: reply_info to delete.
    356  * @param alloc: where to return rrset structures to.
    357  */
    358 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
    359 
    360 /**
    361  * Compare two queryinfo structures, on query and type, class.
    362  * It is _not_ sorted in canonical ordering.
    363  * @param m1: struct query_info* , void* here to ease use as function pointer.
    364  * @param m2: struct query_info* , void* here to ease use as function pointer.
    365  * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
    366  */
    367 int query_info_compare(void* m1, void* m2);
    368 
    369 /** clear out query info structure */
    370 void query_info_clear(struct query_info* m);
    371 
    372 /** calculate size of struct query_info + reply_info */
    373 size_t msgreply_sizefunc(void* k, void* d);
    374 
    375 /** delete msgreply_entry key structure */
    376 void query_entry_delete(void *q, void* arg);
    377 
    378 /** delete reply_info data structure */
    379 void reply_info_delete(void* d, void* arg);
    380 
    381 /** calculate hash value of query_info, lowercases the qname,
    382  * uses CD flag for AAAA qtype */
    383 hashvalue_type query_info_hash(struct query_info *q, uint16_t flags);
    384 
    385 /**
    386  * Setup query info entry
    387  * @param q: query info to copy. Emptied as if clear is called.
    388  * @param r: reply to init data.
    389  * @param h: hash value.
    390  * @return: newly allocated message reply cache item.
    391  */
    392 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
    393 	struct reply_info* r, hashvalue_type h);
    394 
    395 /**
    396  * Copy reply_info and all rrsets in it and allocate.
    397  * @param rep: what to copy, probably inside region, no ref[] array in it.
    398  * @param alloc: how to allocate rrset keys.
    399  *	Not used if region!=NULL, it can be NULL in that case.
    400  * @param region: if this parameter is NULL then malloc and the alloc is used.
    401  *	otherwise, everything is allocated in this region.
    402  *	In a region, no special rrset key structures are needed (not shared),
    403  *	and no rrset_ref array in the reply is built up.
    404  * @return new reply info or NULL on memory error.
    405  */
    406 struct reply_info* reply_info_copy(struct reply_info* rep,
    407 	struct alloc_cache* alloc, struct regional* region);
    408 
    409 /**
    410  * Allocate (special) rrset keys.
    411  * @param rep: reply info in which the rrset keys to be allocated, rrset[]
    412  *	array should have bee allocated with NULL pointers.
    413  * @param alloc: how to allocate rrset keys.
    414  *	Not used if region!=NULL, it can be NULL in that case.
    415  * @param region: if this parameter is NULL then the alloc is used.
    416  *	otherwise, rrset keys are allocated in this region.
    417  *	In a region, no special rrset key structures are needed (not shared).
    418  *	and no rrset_ref array in the reply needs to be built up.
    419  * @return 1 on success, 0 on error
    420  */
    421 int reply_info_alloc_rrset_keys(struct reply_info* rep,
    422 	struct alloc_cache* alloc, struct regional* region);
    423 
    424 /**
    425  * Check if an *expired* (checked by the caller already) reply info can be used
    426  * as an expired answer.
    427  * @param rep: expired reply info to check.
    428  * @param timenow: the current time.
    429  * @return 1 if it can be used as an answer, 0 otherwise.
    430  */
    431 int reply_info_can_answer_expired(struct reply_info* rep, time_t timenow);
    432 
    433 /**
    434  * Check if an *expired* (checked by the caller already) reply info could be
    435  * useful data to stay in the cache.
    436  * @param rep: expired reply info to check.
    437  * @param timenow: the current time.
    438  * @return 1 if it is useful, 0 otherwise.
    439  */
    440 int reply_info_could_use_expired(struct reply_info* rep, time_t timenow);
    441 
    442 /*
    443  * Create a new reply_info based on 'rep'.  The new info is based on
    444  * the passed 'rep', but ignores any rrsets except for the first 'an_numrrsets'
    445  * RRsets in the answer section.  These answer rrsets are copied to the
    446  * new info, up to 'copy_rrsets' rrsets (which must not be larger than
    447  * 'an_numrrsets').  If an_numrrsets > copy_rrsets, the remaining rrsets array
    448  * entries will be kept empty so the caller can fill them later.  When rrsets
    449  * are copied, they are shallow copied.  The caller must ensure that the
    450  * copied rrsets are valid throughout its lifetime and must provide appropriate
    451  * mutex if it can be shared by multiple threads.
    452  */
    453 struct reply_info *
    454 make_new_reply_info(const struct reply_info* rep, struct regional* region,
    455 	size_t an_numrrsets, size_t copy_rrsets);
    456 
    457 /**
    458  * Copy a parsed rrset into given key, decompressing and allocating rdata.
    459  * @param pkt: packet for decompression
    460  * @param msg: the parser message (for flags for trust).
    461  * @param pset: the parsed rrset to copy.
    462  * @param region: if NULL - malloc, else data is allocated in this region.
    463  * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
    464  *	will be set on return.
    465  *	Note that TTL will still be relative on return.
    466  * @return false on alloc failure.
    467  */
    468 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
    469 	struct rrset_parse *pset, struct regional* region,
    470 	struct ub_packed_rrset_key* pk);
    471 
    472 /**
    473  * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
    474  * @param qinfo: what to start with.
    475  * @param rep: looks in answer section of this message.
    476  * @return: pointer dname, or NULL if not found.
    477  */
    478 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
    479 	struct reply_info* rep);
    480 
    481 /**
    482  * Check if cname chain in cached reply is still valid.
    483  * @param qinfo: query info with query name.
    484  * @param rep: reply to check.
    485  * @return: true if valid, false if invalid.
    486  */
    487 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep);
    488 
    489 /**
    490  * Check security status of all RRs in the message.
    491  * @param rep: reply to check
    492  * @return: true if all RRs are secure. False if not.
    493  *    True if there are zero RRs.
    494  */
    495 int reply_all_rrsets_secure(struct reply_info* rep);
    496 
    497 /**
    498  * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
    499  * result may have a different owner name.
    500  * @param qinfo: what to look for.
    501  * @param rep: looks in answer section of this message.
    502  * @return: pointer to rrset, or NULL if not found.
    503  */
    504 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
    505 	struct reply_info* rep);
    506 
    507 /**
    508  * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
    509  * @param rep: looks in answer section of this message.
    510  * @param name: what to look for.
    511  * @param namelen: length of name.
    512  * @param type: looks for (host order).
    513  * @param dclass: looks for (host order).
    514  * @return: pointer to rrset, or NULL if not found.
    515  */
    516 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
    517 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
    518 
    519 /**
    520  * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
    521  * @param rep: looks in authority section of this message.
    522  * @param name: what to look for.
    523  * @param namelen: length of name.
    524  * @param type: looks for (host order).
    525  * @param dclass: looks for (host order).
    526  * @return: pointer to rrset, or NULL if not found.
    527  */
    528 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
    529 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
    530 
    531 /**
    532  * Find rrset in reply, inside any section. Does not follow CNAMEs.
    533  * @param rep: looks in answer,authority and additional section of this message.
    534  * @param name: what to look for.
    535  * @param namelen: length of name.
    536  * @param type: looks for (host order).
    537  * @param dclass: looks for (host order).
    538  * @return: pointer to rrset, or NULL if not found.
    539  */
    540 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
    541 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
    542 
    543 /**
    544  * Debug send the query info and reply info to the log in readable form.
    545  * @param str: descriptive string printed with packet content.
    546  * @param qinfo: query section.
    547  * @param rep: rest of message.
    548  */
    549 void log_dns_msg(const char* str, struct query_info* qinfo,
    550 	struct reply_info* rep);
    551 
    552 /**
    553  * Print string with neat domain name, type, class,
    554  * status code from, and size of a query response.
    555  *
    556  * @param v: at what verbosity level to print this.
    557  * @param qinf: query section.
    558  * @param addr: address of the client.
    559  * @param addrlen: length of the client address.
    560  * @param dur: how long it took to complete the query.
    561  * @param cached: whether or not the reply is coming from
    562  *                    the cache, or an outside network.
    563  * @param rmsg: sldns buffer packet.
    564  * @param daddr: if not NULL, the destination address and port are logged.
    565  * @param tp: type of the comm point for logging destination connection type.
    566  * @param ssl: the SSL pointer of the connection, to see if the connection
    567  *	type is tcp or dot.
    568  */
    569 void log_reply_info(enum verbosity_value v, struct query_info *qinf,
    570 	struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
    571 	int cached, struct sldns_buffer *rmsg, struct sockaddr_storage* daddr,
    572 	enum comm_point_type tp, void* ssl);
    573 
    574 /**
    575  * Print string with neat domain name, type, class from query info.
    576  * @param v: at what verbosity level to print this.
    577  * @param str: string of message.
    578  * @param qinf: query info structure with name, type and class.
    579  */
    580 void log_query_info(enum verbosity_value v, const char* str,
    581 	struct query_info* qinf);
    582 
    583 /**
    584  * Append edns option to edns option list.
    585  * @param list: the edns option list to append the edns option to.
    586  * @param code: the edns option's code.
    587  * @param len: the edns option's length.
    588  * @param data: the edns option's data.
    589  * @param region: region to allocate the new edns option.
    590  * @return false on failure.
    591  */
    592 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
    593         uint8_t* data, struct regional* region);
    594 
    595 /**
    596  * Append edns EDE option to edns options list.
    597  * We need ATTR_NONSTRING because we are trimming the trailing \0 of static
    598  * string (TXT) when assigning to ede.text; it silences compiler nonstring
    599  * warnings.
    600  * @param LIST: the edns option list to append the edns option to.
    601  * @param REGION: region to allocate the new edns option.
    602  * @param CODE: the EDE code.
    603  * @param TXT: Additional text for the option.
    604  */
    605 #define EDNS_OPT_LIST_APPEND_EDE(LIST, REGION, CODE, TXT) 		\
    606 	do {								\
    607 		struct {						\
    608 			uint16_t code;					\
    609 			char ATTR_NONSTRING(text[sizeof(TXT) - 1]) ;	\
    610 		} ede = { htons(CODE), TXT };				\
    611                 verbose(VERB_ALGO, "attached EDE code: %d with"		\
    612                         " message: '%s'", CODE, TXT);			\
    613 		edns_opt_list_append((LIST), LDNS_EDNS_EDE, 		\
    614 			sizeof(uint16_t) + sizeof(TXT) - 1,		\
    615 			(void *)&ede, (REGION));			\
    616 	} while(0)
    617 
    618 /**
    619  * Append edns EDE option to edns options list
    620  * @param list: the edns option list to append the edns option to.
    621  * @param region: region to allocate the new edns option.
    622  * @param code: the EDE code.
    623  * @param txt: Additional text for the option
    624  * @return false on failure.
    625  */
    626 int edns_opt_list_append_ede(struct edns_option** list, struct regional* region,
    627 	sldns_ede_code code, const char *txt);
    628 
    629 /**
    630  * Append edns keep alive option to edns options list
    631  * @param list: the edns option list to append the edns option to.
    632  * @param msec: the duration in msecs for the keep alive.
    633  * @param region: region to allocate the new edns option.
    634  * @return false on failure.
    635  */
    636 int edns_opt_list_append_keepalive(struct edns_option** list, int msec,
    637 	struct regional* region);
    638 
    639 /**
    640  * Remove any option found on the edns option list that matches the code.
    641  * @param list: the list of edns options.
    642  * @param code: the opt code to remove.
    643  * @return true when at least one edns option was removed, false otherwise.
    644  */
    645 int edns_opt_list_remove(struct edns_option** list, uint16_t code);
    646 
    647 /**
    648  * Find edns option in edns list
    649  * @param list: list of edns options (eg. edns.opt_list)
    650  * @param code: opt code to find.
    651  * @return NULL or the edns_option element.
    652  */
    653 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code);
    654 
    655 /**
    656  * Call the registered functions in the inplace_cb_reply linked list.
    657  * This function is going to get called while answering with a resolved query.
    658  * @param env: module environment.
    659  * @param qinfo: query info.
    660  * @param qstate: module qstate.
    661  * @param rep: Reply info. Could be NULL.
    662  * @param rcode: return code.
    663  * @param edns: edns data of the reply.
    664  * @param repinfo: comm_reply. Reply information for a communication point.
    665  * @param region: region to store data.
    666  * @param start_time: the start time of recursion, when the packet arrived,
    667  * 	or the current time for cache responses.
    668  * @return false on failure (a callback function returned an error).
    669  */
    670 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
    671 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
    672 	struct edns_data* edns, struct comm_reply* repinfo, struct regional* region,
    673 	struct timeval* start_time);
    674 
    675 /**
    676  * Call the registered functions in the inplace_cb_reply_cache linked list.
    677  * This function is going to get called while answering from cache.
    678  * @param env: module environment.
    679  * @param qinfo: query info.
    680  * @param qstate: module qstate. NULL when replying from cache.
    681  * @param rep: Reply info.
    682  * @param rcode: return code.
    683  * @param edns: edns data of the reply. Edns input can be found here.
    684  * @param repinfo: comm_reply. Reply information for a communication point.
    685  * @param region: region to store data.
    686  * @param start_time: the start time of recursion, when the packet arrived,
    687  * 	or the current time for cache responses.
    688  * @return false on failure (a callback function returned an error).
    689  */
    690 int inplace_cb_reply_cache_call(struct module_env* env,
    691 	struct query_info* qinfo, struct module_qstate* qstate,
    692 	struct reply_info* rep, int rcode, struct edns_data* edns,
    693 	struct comm_reply* repinfo, struct regional* region,
    694 	struct timeval* start_time);
    695 
    696 /**
    697  * Call the registered functions in the inplace_cb_reply_local linked list.
    698  * This function is going to get called while answering with local data.
    699  * @param env: module environment.
    700  * @param qinfo: query info.
    701  * @param qstate: module qstate. NULL when replying from cache.
    702  * @param rep: Reply info.
    703  * @param rcode: return code.
    704  * @param edns: edns data of the reply. Edns input can be found here.
    705  * @param repinfo: comm_reply. Reply information for a communication point.
    706  * @param region: region to store data.
    707  * @param start_time: the start time of recursion, when the packet arrived,
    708  * 	or the current time for cache responses.
    709  * @return false on failure (a callback function returned an error).
    710  */
    711 int inplace_cb_reply_local_call(struct module_env* env,
    712 	struct query_info* qinfo, struct module_qstate* qstate,
    713 	struct reply_info* rep, int rcode, struct edns_data* edns,
    714 	struct comm_reply* repinfo, struct regional* region,
    715 	struct timeval* start_time);
    716 
    717 /**
    718  * Call the registered functions in the inplace_cb_reply linked list.
    719  * This function is going to get called while answering with a servfail.
    720  * @param env: module environment.
    721  * @param qinfo: query info.
    722  * @param qstate: module qstate. Contains the edns option lists. Could be NULL.
    723  * @param rep: Reply info. NULL when servfail.
    724  * @param rcode: return code. LDNS_RCODE_SERVFAIL.
    725  * @param edns: edns data of the reply. Edns input can be found here if qstate
    726  *	is NULL.
    727  * @param repinfo: comm_reply. Reply information for a communication point.
    728  * @param region: region to store data.
    729  * @param start_time: the start time of recursion, when the packet arrived,
    730  * 	or the current time for cache responses.
    731  * @return false on failure (a callback function returned an error).
    732  */
    733 int inplace_cb_reply_servfail_call(struct module_env* env,
    734 	struct query_info* qinfo, struct module_qstate* qstate,
    735 	struct reply_info* rep, int rcode, struct edns_data* edns,
    736 	struct comm_reply* repinfo, struct regional* region,
    737 	struct timeval* start_time);
    738 
    739 /**
    740  * Call the registered functions in the inplace_cb_query linked list.
    741  * This function is going to get called just before sending a query to a
    742  * nameserver.
    743  * @param env: module environment.
    744  * @param qinfo: query info.
    745  * @param flags: flags of the query.
    746  * @param addr: to which server to send the query.
    747  * @param addrlen: length of addr.
    748  * @param zone: name of the zone of the delegation point. wireformat dname.
    749  *	This is the delegation point name for which the server is deemed
    750  *	authoritative.
    751  * @param zonelen: length of zone.
    752  * @param qstate: module qstate.
    753  * @param region: region to store data.
    754  * @return false on failure (a callback function returned an error).
    755  */
    756 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
    757 	uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
    758 	uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
    759 	struct regional* region);
    760 
    761 /**
    762  * Call the registered functions in the inplace_cb_edns_back_parsed linked list.
    763  * This function is going to get called after parsing the EDNS data on the
    764  * reply from a nameserver.
    765  * @param env: module environment.
    766  * @param qstate: module qstate.
    767  * @return false on failure (a callback function returned an error).
    768  */
    769 int inplace_cb_edns_back_parsed_call(struct module_env* env,
    770 	struct module_qstate* qstate);
    771 
    772 /**
    773  * Call the registered functions in the inplace_cb_query_response linked list.
    774  * This function is going to get called after receiving a reply from a
    775  * nameserver.
    776  * @param env: module environment.
    777  * @param qstate: module qstate.
    778  * @param response: received response
    779  * @return false on failure (a callback function returned an error).
    780  */
    781 int inplace_cb_query_response_call(struct module_env* env,
    782 	struct module_qstate* qstate, struct dns_msg* response);
    783 
    784 /**
    785  * Copy edns option list allocated to the new region
    786  */
    787 struct edns_option* edns_opt_copy_region(struct edns_option* list,
    788 	struct regional* region);
    789 
    790 /**
    791  * Copy a filtered edns option list allocated to the new region
    792  */
    793 struct edns_option* edns_opt_copy_filter_region(struct edns_option* list,
    794 	uint16_t* filter_list, size_t filter_list_len, struct regional* region);
    795 
    796 /**
    797  * Copy edns option list allocated with malloc
    798  */
    799 struct edns_option* edns_opt_copy_alloc(struct edns_option* list);
    800 
    801 /**
    802  * Free edns option list allocated with malloc
    803  */
    804 void edns_opt_list_free(struct edns_option* list);
    805 
    806 /**
    807  * Compare an edns option. (not entire list).  Also compares contents.
    808  */
    809 int edns_opt_compare(struct edns_option* p, struct edns_option* q);
    810 
    811 /**
    812  * Compare edns option lists, also the order and contents of edns-options.
    813  */
    814 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q);
    815 
    816 /**
    817  * Swallow copy the local_alias into the given qname and qname_len.
    818  * @param local_alias: the local_alias.
    819  * @param qname: the qname to copy to.
    820  * @param qname_len: the qname_len to copy to.
    821  * @return false on current local_alias assumptions, true otherwise.
    822  */
    823 int local_alias_shallow_copy_qname(struct local_rrset* local_alias, uint8_t** qname,
    824 	size_t* qname_len);
    825 
    826 #endif /* UTIL_DATA_MSGREPLY_H */
    827