1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * util/data/msgparse.h - parse wireformat DNS messages. 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav /** 36b7579f77SDag-Erling Smørgrav * \file 37b7579f77SDag-Erling Smørgrav * Contains message parsing data structures. 38b7579f77SDag-Erling Smørgrav * These point back into the packet buffer. 39b7579f77SDag-Erling Smørgrav * 40b7579f77SDag-Erling Smørgrav * During parsing RRSIGS are put together with the rrsets they (claim to) sign. 41b7579f77SDag-Erling Smørgrav * This process works as follows: 42b7579f77SDag-Erling Smørgrav * o if RRSIG follows the data rrset, it is added to the rrset rrsig list. 43b7579f77SDag-Erling Smørgrav * o if no matching data rrset is found, the RRSIG becomes a new rrset. 44b7579f77SDag-Erling Smørgrav * o If the data rrset later follows the RRSIG 45b7579f77SDag-Erling Smørgrav * o See if the RRSIG rrset contains multiple types, and needs to 46b7579f77SDag-Erling Smørgrav * have the rrsig(s) for that data type split off. 47b7579f77SDag-Erling Smørgrav * o Put the data rr as data type in the rrset and rrsig in list. 48b7579f77SDag-Erling Smørgrav * o RRSIGs are allowed to move to a different section. The section of 49b7579f77SDag-Erling Smørgrav * the data item is used for the final rrset. 50b7579f77SDag-Erling Smørgrav * o multiple signatures over an RRset are possible. 51b7579f77SDag-Erling Smørgrav * 52b7579f77SDag-Erling Smørgrav * For queries of qtype=RRSIG, some special handling is needed, to avoid 53b7579f77SDag-Erling Smørgrav * splitting the RRSIG in the answer section. 54b7579f77SDag-Erling Smørgrav * o duplicate, not split, RRSIGs from the answer section, if qtype=RRSIG. 55b7579f77SDag-Erling Smørgrav * o check for doubles in the rrsig list when adding an RRSIG to data, 56b7579f77SDag-Erling Smørgrav * so that a data rrset is signed by RRSIGs with different rdata. 57b7579f77SDag-Erling Smørgrav * when qtype=RRSIG. 58b7579f77SDag-Erling Smørgrav * This will move the RRSIG from the answer section to sign the data further 59b7579f77SDag-Erling Smørgrav * in the packet (if possible). If then after that, more RRSIGs are found 60b7579f77SDag-Erling Smørgrav * that sign the data as well, doubles are removed. 61b7579f77SDag-Erling Smørgrav */ 62b7579f77SDag-Erling Smørgrav 63b7579f77SDag-Erling Smørgrav #ifndef UTIL_DATA_MSGPARSE_H 64b7579f77SDag-Erling Smørgrav #define UTIL_DATA_MSGPARSE_H 65b7579f77SDag-Erling Smørgrav #include "util/storage/lruhash.h" 6609a3aaf3SDag-Erling Smørgrav #include "sldns/pkthdr.h" 6709a3aaf3SDag-Erling Smørgrav #include "sldns/rrdef.h" 6817d15b25SDag-Erling Smørgrav struct sldns_buffer; 69b7579f77SDag-Erling Smørgrav struct rrset_parse; 70b7579f77SDag-Erling Smørgrav struct rr_parse; 71b7579f77SDag-Erling Smørgrav struct regional; 72e2d15004SDag-Erling Smørgrav struct edns_option; 7324e36522SCy Schubert struct config_file; 7424e36522SCy Schubert struct comm_point; 758f76bb7dSCy Schubert struct comm_reply; 76b7579f77SDag-Erling Smørgrav 77b7579f77SDag-Erling Smørgrav /** number of buckets in parse rrset hash table. Must be power of 2. */ 78b7579f77SDag-Erling Smørgrav #define PARSE_TABLE_SIZE 32 79b7579f77SDag-Erling Smørgrav /** Maximum TTL that is allowed. */ 8017d15b25SDag-Erling Smørgrav extern time_t MAX_TTL; 81b7579f77SDag-Erling Smørgrav /** Minimum TTL that is allowed. */ 8217d15b25SDag-Erling Smørgrav extern time_t MIN_TTL; 8309a3aaf3SDag-Erling Smørgrav /** Maximum Negative TTL that is allowed */ 8409a3aaf3SDag-Erling Smørgrav extern time_t MAX_NEG_TTL; 85*335c7cdaSCy Schubert /** Minimum Negative TTL that is allowed */ 86*335c7cdaSCy Schubert extern time_t MIN_NEG_TTL; 87091e9e46SCy Schubert /** If we serve expired entries and prefetch them */ 88091e9e46SCy Schubert extern int SERVE_EXPIRED; 894c75e3aaSDag-Erling Smørgrav /** Time to serve records after expiration */ 904c75e3aaSDag-Erling Smørgrav extern time_t SERVE_EXPIRED_TTL; 91091e9e46SCy Schubert /** TTL to use for expired records */ 92091e9e46SCy Schubert extern time_t SERVE_EXPIRED_REPLY_TTL; 93b7579f77SDag-Erling Smørgrav /** Negative cache time (for entries without any RRs.) */ 94b7579f77SDag-Erling Smørgrav #define NORR_TTL 5 /* seconds */ 95f44e67d1SCy Schubert /** If we serve the original TTL or decrementing TTLs */ 96f44e67d1SCy Schubert extern int SERVE_ORIGINAL_TTL; 97b7579f77SDag-Erling Smørgrav 98b7579f77SDag-Erling Smørgrav /** 99b7579f77SDag-Erling Smørgrav * Data stored in scratch pad memory during parsing. 100b7579f77SDag-Erling Smørgrav * Stores the data that will enter into the msgreply and packet result. 101b7579f77SDag-Erling Smørgrav */ 102b7579f77SDag-Erling Smørgrav struct msg_parse { 103b7579f77SDag-Erling Smørgrav /** id from message, network format. */ 104b7579f77SDag-Erling Smørgrav uint16_t id; 105b7579f77SDag-Erling Smørgrav /** flags from message, host format. */ 106b7579f77SDag-Erling Smørgrav uint16_t flags; 107b7579f77SDag-Erling Smørgrav /** count of RRs, host format */ 108b7579f77SDag-Erling Smørgrav uint16_t qdcount; 109b7579f77SDag-Erling Smørgrav /** count of RRs, host format */ 110b7579f77SDag-Erling Smørgrav uint16_t ancount; 111b7579f77SDag-Erling Smørgrav /** count of RRs, host format */ 112b7579f77SDag-Erling Smørgrav uint16_t nscount; 113b7579f77SDag-Erling Smørgrav /** count of RRs, host format */ 114b7579f77SDag-Erling Smørgrav uint16_t arcount; 115b7579f77SDag-Erling Smørgrav /** count of RRsets per section. */ 116b7579f77SDag-Erling Smørgrav size_t an_rrsets; 117b7579f77SDag-Erling Smørgrav /** count of RRsets per section. */ 118b7579f77SDag-Erling Smørgrav size_t ns_rrsets; 119b7579f77SDag-Erling Smørgrav /** count of RRsets per section. */ 120b7579f77SDag-Erling Smørgrav size_t ar_rrsets; 121b7579f77SDag-Erling Smørgrav /** total number of rrsets found. */ 122b7579f77SDag-Erling Smørgrav size_t rrset_count; 123b7579f77SDag-Erling Smørgrav 124b7579f77SDag-Erling Smørgrav /** query dname (pointer to start location in packet, NULL if none */ 125b7579f77SDag-Erling Smørgrav uint8_t* qname; 126b7579f77SDag-Erling Smørgrav /** length of query dname in octets, 0 if none */ 127b7579f77SDag-Erling Smørgrav size_t qname_len; 128b7579f77SDag-Erling Smørgrav /** query type, host order. 0 if qdcount=0 */ 129b7579f77SDag-Erling Smørgrav uint16_t qtype; 130b7579f77SDag-Erling Smørgrav /** query class, host order. 0 if qdcount=0 */ 131b7579f77SDag-Erling Smørgrav uint16_t qclass; 132b7579f77SDag-Erling Smørgrav 133b7579f77SDag-Erling Smørgrav /** 134b7579f77SDag-Erling Smørgrav * Hash table array used during parsing to lookup rrset types. 135b7579f77SDag-Erling Smørgrav * Based on name, type, class. Same hash value as in rrset cache. 136b7579f77SDag-Erling Smørgrav */ 137b7579f77SDag-Erling Smørgrav struct rrset_parse* hashtable[PARSE_TABLE_SIZE]; 138b7579f77SDag-Erling Smørgrav 139b7579f77SDag-Erling Smørgrav /** linked list of rrsets that have been found (in order). */ 140b7579f77SDag-Erling Smørgrav struct rrset_parse* rrset_first; 141b7579f77SDag-Erling Smørgrav /** last element of rrset list. */ 142b7579f77SDag-Erling Smørgrav struct rrset_parse* rrset_last; 143b7579f77SDag-Erling Smørgrav }; 144b7579f77SDag-Erling Smørgrav 145b7579f77SDag-Erling Smørgrav /** 146b7579f77SDag-Erling Smørgrav * Data stored for an rrset during parsing. 147b7579f77SDag-Erling Smørgrav */ 148b7579f77SDag-Erling Smørgrav struct rrset_parse { 149b7579f77SDag-Erling Smørgrav /** next in hash bucket */ 150b7579f77SDag-Erling Smørgrav struct rrset_parse* rrset_bucket_next; 151b7579f77SDag-Erling Smørgrav /** next in list of all rrsets */ 152b7579f77SDag-Erling Smørgrav struct rrset_parse* rrset_all_next; 153b7579f77SDag-Erling Smørgrav /** hash value of rrset */ 1543005e0a3SDag-Erling Smørgrav hashvalue_type hash; 155b7579f77SDag-Erling Smørgrav /** which section was it found in: one of 156b7579f77SDag-Erling Smørgrav * LDNS_SECTION_ANSWER, LDNS_SECTION_AUTHORITY, LDNS_SECTION_ADDITIONAL 157b7579f77SDag-Erling Smørgrav */ 15817d15b25SDag-Erling Smørgrav sldns_pkt_section section; 159b7579f77SDag-Erling Smørgrav /** start of (possibly compressed) dname in packet */ 160b7579f77SDag-Erling Smørgrav uint8_t* dname; 161b7579f77SDag-Erling Smørgrav /** length of the dname uncompressed wireformat */ 162b7579f77SDag-Erling Smørgrav size_t dname_len; 163b7579f77SDag-Erling Smørgrav /** type, host order. */ 164b7579f77SDag-Erling Smørgrav uint16_t type; 165b7579f77SDag-Erling Smørgrav /** class, network order. var name so that it is not a c++ keyword. */ 166b7579f77SDag-Erling Smørgrav uint16_t rrset_class; 167b7579f77SDag-Erling Smørgrav /** the flags for the rrset, like for packedrrset */ 168b7579f77SDag-Erling Smørgrav uint32_t flags; 169b7579f77SDag-Erling Smørgrav /** number of RRs in the rr list */ 170b7579f77SDag-Erling Smørgrav size_t rr_count; 171b7579f77SDag-Erling Smørgrav /** sum of RR rdata sizes */ 172b7579f77SDag-Erling Smørgrav size_t size; 173b7579f77SDag-Erling Smørgrav /** linked list of RRs in this rrset. */ 174b7579f77SDag-Erling Smørgrav struct rr_parse* rr_first; 175b7579f77SDag-Erling Smørgrav /** last in list of RRs in this rrset. */ 176b7579f77SDag-Erling Smørgrav struct rr_parse* rr_last; 177b7579f77SDag-Erling Smørgrav /** number of RRSIGs over this rrset. */ 178b7579f77SDag-Erling Smørgrav size_t rrsig_count; 179b7579f77SDag-Erling Smørgrav /** linked list of RRsig RRs over this rrset. */ 180b7579f77SDag-Erling Smørgrav struct rr_parse* rrsig_first; 181b7579f77SDag-Erling Smørgrav /** last in list of RRSIG RRs over this rrset. */ 182b7579f77SDag-Erling Smørgrav struct rr_parse* rrsig_last; 183b7579f77SDag-Erling Smørgrav }; 184b7579f77SDag-Erling Smørgrav 185b7579f77SDag-Erling Smørgrav /** 186b7579f77SDag-Erling Smørgrav * Data stored for an RR during parsing. 187b7579f77SDag-Erling Smørgrav */ 188b7579f77SDag-Erling Smørgrav struct rr_parse { 189b7579f77SDag-Erling Smørgrav /** 190b7579f77SDag-Erling Smørgrav * Pointer to the RR. Points to start of TTL value in the packet. 191b7579f77SDag-Erling Smørgrav * Rdata length and rdata follow it. 192b7579f77SDag-Erling Smørgrav * its dname, type and class are the same and stored for the rrset. 193b7579f77SDag-Erling Smørgrav */ 194b7579f77SDag-Erling Smørgrav uint8_t* ttl_data; 195b7579f77SDag-Erling Smørgrav /** true if ttl_data is not part of the packet, but elsewhere in mem. 196b7579f77SDag-Erling Smørgrav * Set for generated CNAMEs for DNAMEs. */ 197b7579f77SDag-Erling Smørgrav int outside_packet; 198b7579f77SDag-Erling Smørgrav /** the length of the rdata if allocated (with no dname compression)*/ 199b7579f77SDag-Erling Smørgrav size_t size; 200b7579f77SDag-Erling Smørgrav /** next in list of RRs. */ 201b7579f77SDag-Erling Smørgrav struct rr_parse* next; 202b7579f77SDag-Erling Smørgrav }; 203b7579f77SDag-Erling Smørgrav 204b7579f77SDag-Erling Smørgrav /** Check if label length is first octet of a compression pointer, pass u8. */ 205b7579f77SDag-Erling Smørgrav #define LABEL_IS_PTR(x) ( ((x)&0xc0) == 0xc0 ) 206b7579f77SDag-Erling Smørgrav /** Calculate destination offset of a compression pointer. pass first and 207b7579f77SDag-Erling Smørgrav * second octets of the compression pointer. */ 208b7579f77SDag-Erling Smørgrav #define PTR_OFFSET(x, y) ( ((x)&0x3f)<<8 | (y) ) 209b7579f77SDag-Erling Smørgrav /** create a compression pointer to the given offset. */ 210b7579f77SDag-Erling Smørgrav #define PTR_CREATE(offset) ((uint16_t)(0xc000 | (offset))) 211b7579f77SDag-Erling Smørgrav 212b7579f77SDag-Erling Smørgrav /** error codes, extended with EDNS, so > 15. */ 213b7579f77SDag-Erling Smørgrav #define EDNS_RCODE_BADVERS 16 /** bad EDNS version */ 214b7579f77SDag-Erling Smørgrav /** largest valid compression offset */ 215b7579f77SDag-Erling Smørgrav #define PTR_MAX_OFFSET 0x3fff 216b7579f77SDag-Erling Smørgrav 217b7579f77SDag-Erling Smørgrav /** 218b7579f77SDag-Erling Smørgrav * EDNS data storage 219e2d15004SDag-Erling Smørgrav * rdata is parsed in a list (has accessor functions). allocated in a 220e2d15004SDag-Erling Smørgrav * region. 221b7579f77SDag-Erling Smørgrav */ 222b7579f77SDag-Erling Smørgrav struct edns_data { 223b7579f77SDag-Erling Smørgrav /** Extended RCODE */ 224b7579f77SDag-Erling Smørgrav uint8_t ext_rcode; 225b7579f77SDag-Erling Smørgrav /** The EDNS version number */ 226b7579f77SDag-Erling Smørgrav uint8_t edns_version; 227b7579f77SDag-Erling Smørgrav /** the EDNS bits field from ttl (host order): Z */ 228b7579f77SDag-Erling Smørgrav uint16_t bits; 229b7579f77SDag-Erling Smørgrav /** UDP reassembly size. */ 230b7579f77SDag-Erling Smørgrav uint16_t udp_size; 23124e36522SCy Schubert /** rdata element list of options of an incoming packet created at 23224e36522SCy Schubert * parse time, or NULL if none */ 23324e36522SCy Schubert struct edns_option* opt_list_in; 23424e36522SCy Schubert /** rdata element list of options to encode for outgoing packets, 23524e36522SCy Schubert * or NULL if none */ 23624e36522SCy Schubert struct edns_option* opt_list_out; 23724e36522SCy Schubert /** rdata element list of outgoing edns options from modules 23824e36522SCy Schubert * or NULL if none */ 23924e36522SCy Schubert struct edns_option* opt_list_inplace_cb_out; 240f44e67d1SCy Schubert /** block size to pad */ 241f44e67d1SCy Schubert uint16_t padding_block_size; 2428f76bb7dSCy Schubert /** if EDNS OPT record was present */ 2438f76bb7dSCy Schubert unsigned int edns_present : 1; 2448f76bb7dSCy Schubert /** if a cookie was present */ 2458f76bb7dSCy Schubert unsigned int cookie_present : 1; 2468f76bb7dSCy Schubert /** if the cookie validated */ 2478f76bb7dSCy Schubert unsigned int cookie_valid : 1; 2488f76bb7dSCy Schubert /** if the cookie holds only the client part */ 2498f76bb7dSCy Schubert unsigned int cookie_client : 1; 250e2d15004SDag-Erling Smørgrav }; 251e2d15004SDag-Erling Smørgrav 252e2d15004SDag-Erling Smørgrav /** 253e2d15004SDag-Erling Smørgrav * EDNS option 254e2d15004SDag-Erling Smørgrav */ 255e2d15004SDag-Erling Smørgrav struct edns_option { 256e2d15004SDag-Erling Smørgrav /** next item in list */ 257e2d15004SDag-Erling Smørgrav struct edns_option* next; 258e2d15004SDag-Erling Smørgrav /** type of this edns option */ 259e2d15004SDag-Erling Smørgrav uint16_t opt_code; 260e2d15004SDag-Erling Smørgrav /** length of this edns option (cannot exceed uint16 in encoding) */ 261e2d15004SDag-Erling Smørgrav size_t opt_len; 262e2d15004SDag-Erling Smørgrav /** data of this edns option; allocated in region, or NULL if len=0 */ 263e2d15004SDag-Erling Smørgrav uint8_t* opt_data; 264b7579f77SDag-Erling Smørgrav }; 265b7579f77SDag-Erling Smørgrav 266b7579f77SDag-Erling Smørgrav /** 267b7579f77SDag-Erling Smørgrav * Obtain size in the packet of an rr type, that is before dname type. 268b7579f77SDag-Erling Smørgrav * Do TYPE_DNAME, and type STR, yourself. Gives size for most regular types. 269b7579f77SDag-Erling Smørgrav * @param rdf: the rdf type from the descriptor. 270b7579f77SDag-Erling Smørgrav * @return: size in octets. 0 on failure. 271b7579f77SDag-Erling Smørgrav */ 27217d15b25SDag-Erling Smørgrav size_t get_rdf_size(sldns_rdf_type rdf); 273b7579f77SDag-Erling Smørgrav 274b7579f77SDag-Erling Smørgrav /** 275b7579f77SDag-Erling Smørgrav * Parse the packet. 276b7579f77SDag-Erling Smørgrav * @param pkt: packet, position at call must be at start of packet. 277b7579f77SDag-Erling Smørgrav * at end position is after packet. 278b7579f77SDag-Erling Smørgrav * @param msg: where to store results. 279b7579f77SDag-Erling Smørgrav * @param region: how to alloc results. 280b7579f77SDag-Erling Smørgrav * @return: 0 if OK, or rcode on error. 281b7579f77SDag-Erling Smørgrav */ 28217d15b25SDag-Erling Smørgrav int parse_packet(struct sldns_buffer* pkt, struct msg_parse* msg, 283b7579f77SDag-Erling Smørgrav struct regional* region); 284b7579f77SDag-Erling Smørgrav 285b7579f77SDag-Erling Smørgrav /** 286b7579f77SDag-Erling Smørgrav * After parsing the packet, extract EDNS data from packet. 287b7579f77SDag-Erling Smørgrav * If not present this is noted in the data structure. 288b7579f77SDag-Erling Smørgrav * If a parse error happens, an error code is returned. 289b7579f77SDag-Erling Smørgrav * 290b7579f77SDag-Erling Smørgrav * Quirks: 291b7579f77SDag-Erling Smørgrav * o ignores OPT rdata. 292b7579f77SDag-Erling Smørgrav * o ignores OPT owner name. 293b7579f77SDag-Erling Smørgrav * o ignores extra OPT records, except the last one in the packet. 294b7579f77SDag-Erling Smørgrav * 295b7579f77SDag-Erling Smørgrav * @param msg: parsed message structure. Modified on exit, if EDNS was present 296b7579f77SDag-Erling Smørgrav * it is removed from the additional section. 297b7579f77SDag-Erling Smørgrav * @param edns: the edns data is stored here. Does not have to be initialised. 298e2d15004SDag-Erling Smørgrav * @param region: region to alloc results in (edns option contents) 299b7579f77SDag-Erling Smørgrav * @return: 0 on success. or an RCODE on an error. 300b7579f77SDag-Erling Smørgrav * RCODE formerr if OPT in wrong section, and so on. 301b7579f77SDag-Erling Smørgrav */ 30224e36522SCy Schubert int parse_extract_edns_from_response_msg(struct msg_parse* msg, 30324e36522SCy Schubert struct edns_data* edns, struct regional* region); 304b7579f77SDag-Erling Smørgrav 305b7579f77SDag-Erling Smørgrav /** 306a39a5a69SCy Schubert * Skip RRs from packet 307a39a5a69SCy Schubert * @param pkt: the packet. position at start must be right after the query 308a39a5a69SCy Schubert * section. At end, right after EDNS data or no movement if failed. 309a39a5a69SCy Schubert * @param num: Limit of the number of records we want to parse. 310a39a5a69SCy Schubert * @return: 0 on success, 1 on failure. 311a39a5a69SCy Schubert */ 312a39a5a69SCy Schubert int skip_pkt_rrs(struct sldns_buffer* pkt, int num); 313a39a5a69SCy Schubert 314a39a5a69SCy Schubert /** 315b7579f77SDag-Erling Smørgrav * If EDNS data follows a query section, extract it and initialize edns struct. 316b7579f77SDag-Erling Smørgrav * @param pkt: the packet. position at start must be right after the query 317b7579f77SDag-Erling Smørgrav * section. At end, right after EDNS data or no movement if failed. 318b7579f77SDag-Erling Smørgrav * @param edns: the edns data allocated by the caller. Does not have to be 319b7579f77SDag-Erling Smørgrav * initialised. 32024e36522SCy Schubert * @param cfg: the configuration (with nsid value etc.) 32124e36522SCy Schubert * @param c: commpoint to determine transport (if needed) 3228f76bb7dSCy Schubert * @param repinfo: commreply to determine the client address 3238f76bb7dSCy Schubert * @param now: current time 324e2d15004SDag-Erling Smørgrav * @param region: region to alloc results in (edns option contents) 325b7579f77SDag-Erling Smørgrav * @return: 0 on success, or an RCODE on error. 326b7579f77SDag-Erling Smørgrav * RCODE formerr if OPT is badly formatted and so on. 327b7579f77SDag-Erling Smørgrav */ 32824e36522SCy Schubert int parse_edns_from_query_pkt(struct sldns_buffer* pkt, struct edns_data* edns, 3298f76bb7dSCy Schubert struct config_file* cfg, struct comm_point* c, 3308f76bb7dSCy Schubert struct comm_reply* repinfo, time_t now, struct regional* region); 331b7579f77SDag-Erling Smørgrav 332b7579f77SDag-Erling Smørgrav /** 333b7579f77SDag-Erling Smørgrav * Calculate hash value for rrset in packet. 334b7579f77SDag-Erling Smørgrav * @param pkt: the packet. 335b7579f77SDag-Erling Smørgrav * @param dname: pointer to uncompressed dname, or compressed dname in packet. 336b7579f77SDag-Erling Smørgrav * @param type: rrset type in host order. 337b7579f77SDag-Erling Smørgrav * @param dclass: rrset class in network order. 338b7579f77SDag-Erling Smørgrav * @param rrset_flags: rrset flags (same as packed_rrset flags). 339b7579f77SDag-Erling Smørgrav * @return hash value 340b7579f77SDag-Erling Smørgrav */ 3413005e0a3SDag-Erling Smørgrav hashvalue_type pkt_hash_rrset(struct sldns_buffer* pkt, uint8_t* dname, 3423005e0a3SDag-Erling Smørgrav uint16_t type, uint16_t dclass, uint32_t rrset_flags); 343b7579f77SDag-Erling Smørgrav 344b7579f77SDag-Erling Smørgrav /** 345b7579f77SDag-Erling Smørgrav * Lookup in msg hashtable to find a rrset. 346b7579f77SDag-Erling Smørgrav * @param msg: with the hashtable. 347b7579f77SDag-Erling Smørgrav * @param pkt: packet for compressed names. 348b7579f77SDag-Erling Smørgrav * @param h: hash value 349b7579f77SDag-Erling Smørgrav * @param rrset_flags: flags of rrset sought for. 350b7579f77SDag-Erling Smørgrav * @param dname: name of rrset sought for. 351b7579f77SDag-Erling Smørgrav * @param dnamelen: len of dname. 352b7579f77SDag-Erling Smørgrav * @param type: rrset type, host order. 353b7579f77SDag-Erling Smørgrav * @param dclass: rrset class, network order. 354b7579f77SDag-Erling Smørgrav * @return NULL or the rrset_parse if found. 355b7579f77SDag-Erling Smørgrav */ 356b7579f77SDag-Erling Smørgrav struct rrset_parse* msgparse_hashtable_lookup(struct msg_parse* msg, 3573005e0a3SDag-Erling Smørgrav struct sldns_buffer* pkt, hashvalue_type h, uint32_t rrset_flags, 358b7579f77SDag-Erling Smørgrav uint8_t* dname, size_t dnamelen, uint16_t type, uint16_t dclass); 359b7579f77SDag-Erling Smørgrav 360b7579f77SDag-Erling Smørgrav /** 361b7579f77SDag-Erling Smørgrav * Remove rrset from hash table. 362b7579f77SDag-Erling Smørgrav * @param msg: with hashtable. 363b7579f77SDag-Erling Smørgrav * @param rrset: with hash value and id info. 364b7579f77SDag-Erling Smørgrav */ 365b7579f77SDag-Erling Smørgrav void msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset); 366b7579f77SDag-Erling Smørgrav 367bc892140SDag-Erling Smørgrav /** 368bc892140SDag-Erling Smørgrav * Log the edns options in the edns option list. 369bc892140SDag-Erling Smørgrav * @param level: the verbosity level. 370bc892140SDag-Erling Smørgrav * @param info_str: the informational string to be printed before the options. 371bc892140SDag-Erling Smørgrav * @param list: the edns option list. 372bc892140SDag-Erling Smørgrav */ 373bc892140SDag-Erling Smørgrav void log_edns_opt_list(enum verbosity_value level, const char* info_str, 374bc892140SDag-Erling Smørgrav struct edns_option* list); 375bc892140SDag-Erling Smørgrav 376103ba509SCy Schubert /** 377103ba509SCy Schubert * Remove RR from msgparse RRset. 378103ba509SCy Schubert * @param str: this string is used for logging if verbose. If NULL, there is 379103ba509SCy Schubert * no logging of the remove. 380103ba509SCy Schubert * @param pkt: packet in buffer that is removed from. Used to log the name 381103ba509SCy Schubert * of the item removed. 382103ba509SCy Schubert * @param rrset: RRset that the RR is removed from. 383103ba509SCy Schubert * @param prev: previous RR in list, or NULL. 384103ba509SCy Schubert * @param rr: RR that is removed. 385103ba509SCy Schubert * @param addr: address used for logging, if verbose, or NULL then it is not 386103ba509SCy Schubert * used. 387103ba509SCy Schubert * @param addrlen: length of addr, if that is not NULL. 388103ba509SCy Schubert * @return true if rrset is entirely bad, it would then need to be removed. 389103ba509SCy Schubert */ 390103ba509SCy Schubert int msgparse_rrset_remove_rr(const char* str, struct sldns_buffer* pkt, 391103ba509SCy Schubert struct rrset_parse* rrset, struct rr_parse* prev, struct rr_parse* rr, 392103ba509SCy Schubert struct sockaddr_storage* addr, socklen_t addrlen); 393103ba509SCy Schubert 394b7579f77SDag-Erling Smørgrav #endif /* UTIL_DATA_MSGPARSE_H */ 395