]> CyberLeo.Net >> Repos - FreeBSD/releng/10.1.git/blob - contrib/unbound/util/data/msgreply.h
Copy stable/10@r272459 to releng/10.1 as part of
[FreeBSD/releng/10.1.git] / contrib / unbound / util / data / msgreply.h
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 struct sldns_buffer;
47 struct comm_reply;
48 struct alloc_cache;
49 struct iovec;
50 struct regional;
51 struct edns_data;
52 struct msg_parse;
53 struct rrset_parse;
54
55 /** calculate the prefetch TTL as 90% of original. Calculation
56  * without numerical overflow (uin32_t) */
57 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
58
59 /**
60  * Structure to store query information that makes answers to queries
61  * different.
62  */
63 struct query_info {
64         /** 
65          * Salient data on the query: qname, in wireformat. 
66          * can be allocated or a pointer to outside buffer.
67          * User has to keep track on the status of this.
68          */
69         uint8_t* qname;
70         /** length of qname (including last 0 octet) */
71         size_t qname_len;
72         /** qtype, host byte order */
73         uint16_t qtype;
74         /** qclass, host byte order */
75         uint16_t qclass;
76 };
77
78 /**
79  * Information to reference an rrset
80  */
81 struct rrset_ref {
82         /** the key with lock, and ptr to packed data. */
83         struct ub_packed_rrset_key* key;
84         /** id needed */
85         rrset_id_t id;
86 };
87
88 /**
89  * Structure to store DNS query and the reply packet.
90  * To use it, copy over the flags from reply and modify using flags from
91  * the query (RD,CD if not AA). prepend ID. 
92  *
93  * Memory layout is:
94  *      o struct
95  *      o rrset_ref array
96  *      o packed_rrset_key* array.
97  *
98  * Memory layout is sometimes not packed, when the message is synthesized,
99  * for easy of the generation. It is allocated packed when it is copied
100  * from the region allocation to the malloc allocation.
101  */
102 struct reply_info {
103         /** the flags for the answer, host byte order. */
104         uint16_t flags;
105
106         /**
107          * This flag informs unbound the answer is authoritative and 
108          * the AA flag should be preserved. 
109          */
110         uint8_t authoritative;
111
112         /**
113          * Number of RRs in the query section.
114          * If qdcount is not 0, then it is 1, and the data that appears
115          * in the reply is the same as the query_info.
116          * Host byte order.
117          */
118         uint8_t qdcount;
119
120         /** 32 bit padding to pad struct member alignment to 64 bits. */
121         uint32_t padding;
122
123         /** 
124          * TTL of the entire reply (for negative caching).
125          * only for use when there are 0 RRsets in this message.
126          * if there are RRsets, check those instead.
127          */
128         time_t ttl;
129
130         /**
131          * TTL for prefetch. After it has expired, a prefetch is suitable.
132          * Smaller than the TTL, otherwise the prefetch would not happen.
133          */
134         time_t prefetch_ttl;
135
136         /**
137          * The security status from DNSSEC validation of this message.
138          */
139         enum sec_status security;
140
141         /**
142          * Number of RRsets in each section.
143          * The answer section. Add up the RRs in every RRset to calculate
144          * the number of RRs, and the count for the dns packet. 
145          * The number of RRs in RRsets can change due to RRset updates.
146          */
147         size_t an_numrrsets;
148
149         /** Count of authority section RRsets */
150         size_t ns_numrrsets; 
151         /** Count of additional section RRsets */
152         size_t ar_numrrsets;
153
154         /** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
155         size_t rrset_count;
156
157         /** 
158          * List of pointers (only) to the rrsets in the order in which 
159          * they appear in the reply message.  
160          * Number of elements is ancount+nscount+arcount RRsets.
161          * This is a pointer to that array. 
162          * Use the accessor function for access.
163          */
164         struct ub_packed_rrset_key** rrsets;
165
166         /** 
167          * Packed array of ids (see counts) and pointers to packed_rrset_key.
168          * The number equals ancount+nscount+arcount RRsets. 
169          * These are sorted in ascending pointer, the locking order. So
170          * this list can be locked (and id, ttl checked), to see if 
171          * all the data is available and recent enough.
172          *
173          * This is defined as an array of size 1, so that the compiler 
174          * associates the identifier with this position in the structure.
175          * Array bound overflow on this array then gives access to the further
176          * elements of the array, which are allocated after the main structure.
177          *
178          * It could be more pure to define as array of size 0, ref[0].
179          * But ref[1] may be less confusing for compilers.
180          * Use the accessor function for access.
181          */
182         struct rrset_ref ref[1];
183 };
184
185 /**
186  * Structure to keep hash table entry for message replies.
187  */
188 struct msgreply_entry {
189         /** the hash table key */
190         struct query_info key;
191         /** the hash table entry, data is struct reply_info* */
192         struct lruhash_entry entry;
193 };
194
195 /** 
196  * Parse wire query into a queryinfo structure, return 0 on parse error. 
197  * initialises the (prealloced) queryinfo structure as well.
198  * This query structure contains a pointer back info the buffer!
199  * This pointer avoids memory allocation. allocqname does memory allocation.
200  * @param m: the prealloced queryinfo structure to put query into.
201  *    must be unused, or _clear()ed.
202  * @param query: the wireformat packet query. starts with ID.
203  * @return: 0 on format error.
204  */
205 int query_info_parse(struct query_info* m, struct sldns_buffer* query);
206
207 /**
208  * Parse query reply.
209  * Fills in preallocated query_info structure (with ptr into buffer).
210  * Allocates reply_info and packed_rrsets. These are not yet added to any
211  * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
212  * @param pkt: the packet buffer. Must be positioned after the query section.
213  * @param alloc: creates packed rrset key structures.
214  * @param rep: allocated reply_info is returned (only on no error).
215  * @param qinf: query_info is returned (only on no error).
216  * @param region: where to store temporary data (for parsing).
217  * @param edns: where to store edns information, does not need to be inited.
218  * @return: zero is OK, or DNS error code in case of error
219  *      o FORMERR for parse errors.
220  *      o SERVFAIL for memory allocation errors.
221  */
222 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc,
223         struct query_info* qinf, struct reply_info** rep, 
224         struct regional* region, struct edns_data* edns);
225
226 /**
227  * Allocate and decompress parsed message and rrsets.
228  * @param pkt: for name decompression.
229  * @param msg: parsed message in scratch region.
230  * @param alloc: alloc cache for special rrset key structures.
231  *      Not used if region!=NULL, it can be NULL in that case.
232  * @param qinf: where to store query info.
233  *      qinf itself is allocated by the caller.
234  * @param rep: reply info is allocated and returned.
235  * @param region: if this parameter is NULL then malloc and the alloc is used.
236  *      otherwise, everything is allocated in this region.
237  *      In a region, no special rrset key structures are needed (not shared),
238  *      and no rrset_ref array in the reply is built up.
239  * @return 0 if allocation failed.
240  */
241 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
242         struct alloc_cache* alloc, struct query_info* qinf,
243         struct reply_info** rep, struct regional* region);
244
245 /**
246  * Sorts the ref array.
247  * @param rep: reply info. rrsets must be filled in.
248  */
249 void reply_info_sortref(struct reply_info* rep);
250
251 /**
252  * Set TTLs inside the replyinfo to absolute values.
253  * @param rep: reply info. rrsets must be filled in. 
254  *      Also refs must be filled in.
255  * @param timenow: the current time.
256  */
257 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
258
259 /** 
260  * Delete reply_info and packed_rrsets (while they are not yet added to the
261  * hashtables.). Returns rrsets to the alloc cache.
262  * @param rep: reply_info to delete.
263  * @param alloc: where to return rrset structures to.
264  */
265 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
266
267 /**
268  * Compare two queryinfo structures, on query and type, class. 
269  * It is _not_ sorted in canonical ordering.
270  * @param m1: struct query_info* , void* here to ease use as function pointer.
271  * @param m2: struct query_info* , void* here to ease use as function pointer.
272  * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
273  */
274 int query_info_compare(void* m1, void* m2);
275
276 /** clear out query info structure */
277 void query_info_clear(struct query_info* m);
278
279 /** calculate size of struct query_info + reply_info */
280 size_t msgreply_sizefunc(void* k, void* d);
281
282 /** delete msgreply_entry key structure */
283 void query_entry_delete(void *q, void* arg);
284
285 /** delete reply_info data structure */
286 void reply_info_delete(void* d, void* arg);
287
288 /** calculate hash value of query_info, lowercases the qname */
289 hashvalue_t query_info_hash(struct query_info *q);
290
291 /**
292  * Setup query info entry
293  * @param q: query info to copy. Emptied as if clear is called.
294  * @param r: reply to init data.
295  * @param h: hash value.
296  * @return: newly allocated message reply cache item.
297  */
298 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
299         struct reply_info* r, hashvalue_t h);
300
301 /**
302  * Copy reply_info and all rrsets in it and allocate.
303  * @param rep: what to copy, probably inside region, no ref[] array in it.
304  * @param alloc: how to allocate rrset keys.
305  *      Not used if region!=NULL, it can be NULL in that case.
306  * @param region: if this parameter is NULL then malloc and the alloc is used.
307  *      otherwise, everything is allocated in this region.
308  *      In a region, no special rrset key structures are needed (not shared),
309  *      and no rrset_ref array in the reply is built up.
310  * @return new reply info or NULL on memory error.
311  */
312 struct reply_info* reply_info_copy(struct reply_info* rep, 
313         struct alloc_cache* alloc, struct regional* region);
314
315 /**
316  * Copy a parsed rrset into given key, decompressing and allocating rdata.
317  * @param pkt: packet for decompression
318  * @param msg: the parser message (for flags for trust).
319  * @param pset: the parsed rrset to copy.
320  * @param region: if NULL - malloc, else data is allocated in this region.
321  * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
322  *      will be set on return.
323  *      Note that TTL will still be relative on return.
324  * @return false on alloc failure.
325  */
326 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
327         struct rrset_parse *pset, struct regional* region, 
328         struct ub_packed_rrset_key* pk);
329
330 /**
331  * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
332  * @param qinfo: what to start with.
333  * @param rep: looks in answer section of this message.
334  * @return: pointer dname, or NULL if not found.
335  */
336 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
337         struct reply_info* rep);
338
339 /**
340  * Check if cname chain in cached reply is still valid.
341  * @param rep: reply to check.
342  * @return: true if valid, false if invalid.
343  */
344 int reply_check_cname_chain(struct reply_info* rep);
345
346 /**
347  * Check security status of all RRs in the message.
348  * @param rep: reply to check
349  * @return: true if all RRs are secure. False if not.
350  *    True if there are zero RRs.
351  */
352 int reply_all_rrsets_secure(struct reply_info* rep);
353
354 /**
355  * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
356  * result may have a different owner name.
357  * @param qinfo: what to look for.
358  * @param rep: looks in answer section of this message.
359  * @return: pointer to rrset, or NULL if not found.
360  */
361 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
362         struct reply_info* rep);
363
364 /**
365  * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
366  * @param rep: looks in answer section of this message.
367  * @param name: what to look for.
368  * @param namelen: length of name.
369  * @param type: looks for (host order).
370  * @param dclass: looks for (host order).
371  * @return: pointer to rrset, or NULL if not found.
372  */
373 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
374         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
375
376 /**
377  * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
378  * @param rep: looks in authority section of this message.
379  * @param name: what to look for.
380  * @param namelen: length of name.
381  * @param type: looks for (host order).
382  * @param dclass: looks for (host order).
383  * @return: pointer to rrset, or NULL if not found.
384  */
385 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
386         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
387
388 /**
389  * Find rrset in reply, inside any section. Does not follow CNAMEs.
390  * @param rep: looks in answer,authority and additional section of this message.
391  * @param name: what to look for.
392  * @param namelen: length of name.
393  * @param type: looks for (host order).
394  * @param dclass: looks for (host order).
395  * @return: pointer to rrset, or NULL if not found.
396  */
397 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
398         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
399
400 /**
401  * Debug send the query info and reply info to the log in readable form.
402  * @param str: descriptive string printed with packet content.
403  * @param qinfo: query section.
404  * @param rep: rest of message.
405  */
406 void log_dns_msg(const char* str, struct query_info* qinfo, 
407         struct reply_info* rep);
408
409 /**
410  * Print string with neat domain name, type, class from query info.
411  * @param v: at what verbosity level to print this.
412  * @param str: string of message.
413  * @param qinf: query info structure with name, type and class.
414  */
415 void log_query_info(enum verbosity_value v, const char* str, 
416         struct query_info* qinf);
417
418 #endif /* UTIL_DATA_MSGREPLY_H */