2007-03-12 10:19:14 -04:00
|
|
|
/*
|
|
|
|
|
* util/data/msgreply.h - store message and reply data.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This software is open source.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
|
* specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
*
|
|
|
|
|
* This file contains a data structure to store a message and its reply.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef UTIL_DATA_MSGREPLY_H
|
|
|
|
|
#define UTIL_DATA_MSGREPLY_H
|
2007-03-22 12:26:14 -04:00
|
|
|
#include "util/storage/lruhash.h"
|
2007-04-04 09:02:13 -04:00
|
|
|
#include "util/data/packed_rrset.h"
|
2007-03-28 11:40:12 -04:00
|
|
|
struct comm_reply;
|
2007-04-04 09:02:13 -04:00
|
|
|
struct alloc_cache;
|
2007-04-17 06:18:39 -04:00
|
|
|
struct iovec;
|
|
|
|
|
struct region;
|
2007-05-07 09:17:27 -04:00
|
|
|
struct edns_data;
|
2007-03-12 10:19:14 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Structure to store query information that makes answers to queries
|
|
|
|
|
* different.
|
|
|
|
|
*/
|
|
|
|
|
struct query_info {
|
2007-04-05 10:53:32 -04:00
|
|
|
/**
|
|
|
|
|
* Salient data on the query: qname, in wireformat.
|
|
|
|
|
* can be allocated or a pointer to outside buffer.
|
|
|
|
|
* User has to keep track on the status of this.
|
|
|
|
|
*/
|
2007-03-12 10:19:14 -04:00
|
|
|
uint8_t* qname;
|
|
|
|
|
/** length of qname (including last 0 octet) */
|
|
|
|
|
size_t qnamesize;
|
2007-04-25 07:36:51 -04:00
|
|
|
/** qtype, host byte order */
|
2007-03-12 10:19:14 -04:00
|
|
|
uint16_t qtype;
|
2007-04-25 07:36:51 -04:00
|
|
|
/** qclass, host byte order */
|
2007-03-12 10:19:14 -04:00
|
|
|
uint16_t qclass;
|
|
|
|
|
/** does query have CD bit set */
|
|
|
|
|
int has_cd;
|
|
|
|
|
};
|
|
|
|
|
|
2007-04-04 09:02:13 -04:00
|
|
|
/**
|
|
|
|
|
* Information to reference an rrset
|
|
|
|
|
*/
|
|
|
|
|
struct rrset_ref {
|
|
|
|
|
/** the key with lock, and ptr to packed data. */
|
2007-04-10 03:11:57 -04:00
|
|
|
struct ub_packed_rrset_key* key;
|
2007-04-04 09:02:13 -04:00
|
|
|
/** id needed */
|
|
|
|
|
rrset_id_t id;
|
|
|
|
|
};
|
|
|
|
|
|
2007-03-12 10:19:14 -04:00
|
|
|
/**
|
|
|
|
|
* Structure to store DNS query and the reply packet.
|
|
|
|
|
* To use it, copy over the flags from reply and modify using flags from
|
|
|
|
|
* the query (RD,CD if not AA). prepend ID.
|
2007-04-04 09:02:13 -04:00
|
|
|
*
|
|
|
|
|
* Memory layout is:
|
|
|
|
|
* o struct
|
|
|
|
|
* o rrset_ref array
|
|
|
|
|
* o packed_rrset_key* array.
|
2007-03-12 10:19:14 -04:00
|
|
|
*/
|
2007-03-22 12:26:14 -04:00
|
|
|
struct reply_info {
|
2007-04-05 10:53:32 -04:00
|
|
|
/** the flags for the answer, host byte order. */
|
2007-03-29 06:00:10 -04:00
|
|
|
uint16_t flags;
|
2007-04-04 09:02:13 -04:00
|
|
|
|
2007-04-10 01:39:12 -04:00
|
|
|
/**
|
|
|
|
|
* Number of RRs in the query section.
|
|
|
|
|
* If qdcount is not 0, then it is 1, and the data that appears
|
|
|
|
|
* in the reply is the same as the query_info.
|
|
|
|
|
* Host byte order.
|
|
|
|
|
*/
|
|
|
|
|
uint16_t qdcount;
|
|
|
|
|
|
2007-04-05 10:53:32 -04:00
|
|
|
/**
|
|
|
|
|
* TTL of the entire reply (for negative caching).
|
|
|
|
|
* only for use when there are 0 RRsets in this message.
|
|
|
|
|
* if there are RRsets, check those instead.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t ttl;
|
|
|
|
|
|
2007-04-10 01:39:12 -04:00
|
|
|
/**
|
|
|
|
|
* Number of RRsets in each section.
|
|
|
|
|
* The answer section. Add up the RRs in every RRset to calculate
|
|
|
|
|
* the number of RRs, and the count for the dns packet.
|
|
|
|
|
* The number of RRs in RRsets can change due to RRset updates.
|
2007-04-04 09:02:13 -04:00
|
|
|
*/
|
2007-04-10 01:39:12 -04:00
|
|
|
size_t an_numrrsets;
|
2007-04-04 09:02:13 -04:00
|
|
|
|
2007-04-10 01:39:12 -04:00
|
|
|
/** Count of authority section RRsets */
|
|
|
|
|
size_t ns_numrrsets;
|
|
|
|
|
|
|
|
|
|
/** Count of additional section RRsets */
|
|
|
|
|
size_t ar_numrrsets;
|
2007-04-04 09:02:13 -04:00
|
|
|
|
2007-04-10 03:11:57 -04:00
|
|
|
/** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
|
|
|
|
|
size_t rrset_count;
|
|
|
|
|
|
2007-04-04 09:02:13 -04:00
|
|
|
/**
|
|
|
|
|
* List of pointers (only) to the rrsets in the order in which
|
2007-04-05 10:53:32 -04:00
|
|
|
* they appear in the reply message.
|
2007-04-10 01:39:12 -04:00
|
|
|
* Number of elements is ancount+nscount+arcount RRsets.
|
2007-04-05 10:53:32 -04:00
|
|
|
* This is a pointer to that array.
|
|
|
|
|
* Use the accessor function for access.
|
2007-04-04 09:02:13 -04:00
|
|
|
*/
|
2007-04-10 03:11:57 -04:00
|
|
|
struct ub_packed_rrset_key** rrsets;
|
2007-04-04 09:02:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Packed array of ids (see counts) and pointers to packed_rrset_key.
|
2007-04-10 01:39:12 -04:00
|
|
|
* The number equals ancount+nscount+arcount RRsets.
|
2007-04-04 09:02:13 -04:00
|
|
|
* These are sorted in ascending pointer, the locking order. So
|
|
|
|
|
* this list can be locked (and id, ttl checked), to see if
|
|
|
|
|
* all the data is available and recent enough.
|
2007-04-05 10:53:32 -04:00
|
|
|
*
|
|
|
|
|
* This is defined as an array of size 1, so that the compiler
|
|
|
|
|
* associates the identifier with this position in the structure.
|
|
|
|
|
* Array bound overflow on this array then gives access to the further
|
|
|
|
|
* elements of the array, which are allocated after the main structure.
|
|
|
|
|
*
|
|
|
|
|
* It could be more pure to define as array of size 0, ref[0].
|
|
|
|
|
* But ref[1] may be less confusing for compilers.
|
|
|
|
|
* Use the accessor function for access.
|
2007-04-04 09:02:13 -04:00
|
|
|
*/
|
|
|
|
|
struct rrset_ref ref[1];
|
2007-03-12 10:19:14 -04:00
|
|
|
};
|
|
|
|
|
|
2007-03-22 12:26:14 -04:00
|
|
|
/**
|
|
|
|
|
* Structure to keep hash table entry for message replies.
|
|
|
|
|
*/
|
|
|
|
|
struct msgreply_entry {
|
|
|
|
|
/** the hash table key */
|
|
|
|
|
struct query_info key;
|
|
|
|
|
/** the hash table entry, data is struct reply_info* */
|
|
|
|
|
struct lruhash_entry entry;
|
|
|
|
|
};
|
|
|
|
|
|
2007-03-12 10:19:14 -04:00
|
|
|
/**
|
|
|
|
|
* Parse wire query into a queryinfo structure, return 0 on parse error.
|
2007-04-02 09:58:02 -04:00
|
|
|
* initialises the (prealloced) queryinfo structure as well.
|
2007-03-12 10:19:14 -04:00
|
|
|
* This query structure contains a pointer back info the buffer!
|
2007-04-02 09:58:02 -04:00
|
|
|
* This pointer avoids memory allocation. allocqname does memory allocation.
|
2007-03-12 10:19:14 -04:00
|
|
|
* @param m: the prealloced queryinfo structure to put query into.
|
|
|
|
|
* must be unused, or _clear()ed.
|
|
|
|
|
* @param query: the wireformat packet query. starts with ID.
|
|
|
|
|
* @return: 0 on format error.
|
|
|
|
|
*/
|
|
|
|
|
int query_info_parse(struct query_info* m, ldns_buffer* query);
|
|
|
|
|
|
2007-04-04 09:02:13 -04:00
|
|
|
/**
|
|
|
|
|
* Parse query reply.
|
|
|
|
|
* Fills in preallocated query_info structure (with ptr into buffer).
|
|
|
|
|
* Allocates reply_info and packed_rrsets. These are not yet added to any
|
|
|
|
|
* caches or anything, this is only parsing. Returns formerror on qdcount > 1.
|
|
|
|
|
* @param pkt: the packet buffer. Must be positioned after the query section.
|
|
|
|
|
* @param alloc: creates packed rrset key structures.
|
|
|
|
|
* @param rep: allocated reply_info is returned (only on no error).
|
|
|
|
|
* @param qinf: query_info is returned (only on no error).
|
2007-05-04 08:35:01 -04:00
|
|
|
* @param region: where to store temporary data (for parsing).
|
2007-05-07 09:17:27 -04:00
|
|
|
* @param edns: where to store edns information, does not need to be inited.
|
2007-04-10 03:11:57 -04:00
|
|
|
* @return: zero is OK, or DNS error code in case of error
|
|
|
|
|
* o FORMERR for parse errors.
|
|
|
|
|
* o SERVFAIL for memory allocation errors.
|
2007-04-04 09:02:13 -04:00
|
|
|
*/
|
|
|
|
|
int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
|
2007-05-04 08:35:01 -04:00
|
|
|
struct query_info* qinf, struct reply_info** rep,
|
2007-05-07 09:17:27 -04:00
|
|
|
struct region* region, struct edns_data* edns);
|
2007-04-04 09:02:13 -04:00
|
|
|
|
2007-05-03 11:34:03 -04:00
|
|
|
/**
|
2007-05-04 06:10:52 -04:00
|
|
|
* Sorts the ref array.
|
2007-05-03 11:34:03 -04:00
|
|
|
* @param rep: reply info. rrsets must be filled in.
|
|
|
|
|
*/
|
2007-05-04 06:10:52 -04:00
|
|
|
void reply_info_sortref(struct reply_info* rep);
|
2007-05-03 11:34:03 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set TTLs inside the replyinfo to absolute values.
|
|
|
|
|
* @param rep: reply info. rrsets must be filled in.
|
|
|
|
|
* @param timenow: the current time.
|
|
|
|
|
*/
|
|
|
|
|
void reply_info_set_ttls(struct reply_info* rep, uint32_t timenow);
|
|
|
|
|
|
2007-04-04 09:02:13 -04:00
|
|
|
/**
|
|
|
|
|
* Delete reply_info and packed_rrsets (while they are not yet added to the
|
|
|
|
|
* hashtables.). Returns rrsets to the alloc cache.
|
|
|
|
|
* @param rep: reply_info to delete.
|
|
|
|
|
* @param alloc: where to return rrset structures to.
|
|
|
|
|
*/
|
|
|
|
|
void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
|
|
|
|
|
|
2007-03-27 11:21:21 -04:00
|
|
|
/**
|
|
|
|
|
* Allocate and copy the qname (obtained from query_info_parse()).
|
|
|
|
|
* @param m: the queryinfo structure.
|
|
|
|
|
* @return: 0 on alloc failure.
|
|
|
|
|
*/
|
|
|
|
|
int query_info_allocqname(struct query_info* m);
|
|
|
|
|
|
2007-03-12 10:19:14 -04:00
|
|
|
/**
|
2007-04-02 09:58:02 -04:00
|
|
|
* Compare two queryinfo structures, on query and type, class.
|
|
|
|
|
* It is _not_ sorted in canonical ordering.
|
2007-03-12 10:19:14 -04:00
|
|
|
* @param m1: struct query_info* , void* here to ease use as function pointer.
|
|
|
|
|
* @param m2: struct query_info* , void* here to ease use as function pointer.
|
|
|
|
|
* @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
|
|
|
|
|
*/
|
|
|
|
|
int query_info_compare(void* m1, void* m2);
|
|
|
|
|
|
|
|
|
|
/** clear out query info structure. */
|
|
|
|
|
void query_info_clear(struct query_info* m);
|
|
|
|
|
|
2007-03-22 12:26:14 -04:00
|
|
|
/** calculate size of struct query_info + reply_info */
|
|
|
|
|
size_t msgreply_sizefunc(void* k, void* d);
|
|
|
|
|
|
2007-03-23 08:41:38 -04:00
|
|
|
/** delete msgreply_entry key structure */
|
|
|
|
|
void query_entry_delete(void *q, void* arg);
|
2007-03-22 12:26:14 -04:00
|
|
|
|
|
|
|
|
/** delete reply_info data structure */
|
|
|
|
|
void reply_info_delete(void* d, void* arg);
|
|
|
|
|
|
2007-04-02 09:58:02 -04:00
|
|
|
/** calculate hash value of query_info, lowercases the qname. */
|
2007-03-22 12:26:14 -04:00
|
|
|
hashvalue_t query_info_hash(struct query_info *q);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate answer from reply_info.
|
2007-05-03 11:34:03 -04:00
|
|
|
* @param qinf: query information that provides query section in packet.
|
2007-03-22 12:26:14 -04:00
|
|
|
* @param rep: reply to fill in.
|
2007-05-03 11:34:03 -04:00
|
|
|
* @param id: id word from the query.
|
2007-03-22 12:26:14 -04:00
|
|
|
* @param qflags: flags word from the query.
|
2007-05-03 11:34:03 -04:00
|
|
|
* @param dest: buffer to put message into; will truncate if it does not fit.
|
|
|
|
|
* @param timenow: time to subtract.
|
|
|
|
|
* @param cached: set true if a cached reply (so no AA bit).
|
|
|
|
|
* set false for the first reply.
|
2007-05-04 08:35:01 -04:00
|
|
|
* @param region: where to allocate temp variables (for compression).
|
2007-05-07 09:17:27 -04:00
|
|
|
* @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
|
|
|
|
|
* @param edns: EDNS data included in the answer, NULL for none.
|
|
|
|
|
* or if edns_present = 0, it is not included.
|
2007-05-03 11:34:03 -04:00
|
|
|
* @return: 0 on error (server failure).
|
2007-03-22 12:26:14 -04:00
|
|
|
*/
|
2007-05-03 11:34:03 -04:00
|
|
|
int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
|
|
|
|
|
uint16_t id, uint16_t qflags, ldns_buffer* dest, uint32_t timenow,
|
2007-05-07 09:17:27 -04:00
|
|
|
int cached, struct region* region, uint16_t udpsize,
|
|
|
|
|
struct edns_data* edns);
|
2007-03-22 12:26:14 -04:00
|
|
|
|
2007-04-17 06:18:39 -04:00
|
|
|
/**
|
2007-04-27 08:42:03 -04:00
|
|
|
* Regenerate the wireformat from the stored msg reply.
|
2007-05-02 11:31:32 -04:00
|
|
|
* If the buffer is too small then the message is truncated at a whole
|
|
|
|
|
* rrset and the TC bit set, or whole rrsets are left out of the additional
|
|
|
|
|
* and the TC bit is not set.
|
2007-04-17 06:18:39 -04:00
|
|
|
* @param qinfo: query info to store.
|
|
|
|
|
* @param rep: reply to store.
|
|
|
|
|
* @param id: id value to store, network order.
|
|
|
|
|
* @param flags: flags value to store, host order.
|
2007-04-27 08:42:03 -04:00
|
|
|
* @param buffer: buffer to store the packet into.
|
2007-04-17 06:18:39 -04:00
|
|
|
* @param timenow: time now, to adjust ttl values.
|
|
|
|
|
* @param region: to store temporary data in.
|
2007-05-07 09:17:27 -04:00
|
|
|
* @param udpsize: size of the answer, 512, from EDNS, or 64k for TCP.
|
2007-04-27 08:42:03 -04:00
|
|
|
* @return: nonzero is success, or
|
2007-05-02 11:31:32 -04:00
|
|
|
* 0 on error: malloc failure (no log_err has been done).
|
2007-04-17 06:18:39 -04:00
|
|
|
*/
|
2007-04-27 08:42:03 -04:00
|
|
|
int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
|
|
|
|
|
uint16_t id, uint16_t flags, ldns_buffer* buffer, uint32_t timenow,
|
2007-05-07 09:17:27 -04:00
|
|
|
struct region* region, uint16_t udpsize);
|
2007-04-17 06:18:39 -04:00
|
|
|
|
2007-03-22 12:26:14 -04:00
|
|
|
/**
|
|
|
|
|
* Setup query info entry
|
|
|
|
|
* @param q: query info to copy. Emptied as if clear is called.
|
|
|
|
|
* @param r: reply to init data.
|
|
|
|
|
* @param h: hash value.
|
|
|
|
|
* @return: newly allocated message reply cache item.
|
|
|
|
|
*/
|
|
|
|
|
struct msgreply_entry* query_info_entrysetup(struct query_info* q,
|
|
|
|
|
struct reply_info* r, hashvalue_t h);
|
2007-03-12 10:19:14 -04:00
|
|
|
|
|
|
|
|
#endif /* UTIL_DATA_MSGREPLY_H */
|