]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/services/cache/dns.h
Upgrade Unbound to 1.6.4. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / services / cache / dns.h
1 /*
2  * services/cache/dns.h - Cache services for DNS using msg and rrset caches.
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 the DNS cache.
40  */
41
42 #ifndef SERVICES_CACHE_DNS_H
43 #define SERVICES_CACHE_DNS_H
44 #include "util/storage/lruhash.h"
45 #include "util/data/msgreply.h"
46 struct module_env;
47 struct query_info;
48 struct reply_info;
49 struct regional;
50 struct delegpt;
51
52 /**
53  * Region allocated message reply
54  */
55 struct dns_msg {
56         /** query info */
57         struct query_info qinfo;
58         /** reply info - ptr to packed repinfo structure */
59         struct reply_info *rep;
60 };
61
62 /**
63  * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
64  *
65  * @param env: environment, with alloc structure and dns cache.
66  * @param qinf: query info, the query for which answer is stored.
67  *      this is allocated in a region, and will be copied to malloc area
68  *      before insertion.
69  * @param rep: reply in dns_msg from dns_alloc_msg for example.
70  *      this is allocated in a region, and will be copied to malloc area
71  *      before insertion.
72  * @param is_referral: If true, then the given message to be stored is a
73  *      referral. The cache implementation may use this as a hint.
74  *      It will store only the RRsets, not the message.
75  * @param leeway: TTL value, if not 0, other rrsets are considered expired
76  *      that many seconds before actual TTL expiry.
77  * @param pside: if true, information came from a server which was fetched
78  *      from the parentside of the zonecut.  This means that the type NS
79  *      can be updated to full TTL even in prefetch situations.
80  * @param region: region to allocate better entries from cache into.
81  *   (used when is_referral is false).
82  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
83  * @return 0 on alloc error (out of memory).
84  */
85 int dns_cache_store(struct module_env* env, struct query_info* qinf,
86         struct reply_info* rep, int is_referral, time_t leeway, int pside,
87         struct regional* region, uint16_t flags); 
88
89 /**
90  * Store message in the cache. Stores in message cache and rrset cache.
91  * Both qinfo and rep should be malloced and are put in the cache.
92  * They should not be used after this call, as they are then in shared cache.
93  * Does not return errors, they are logged and only lead to less cache.
94  *
95  * @param env: module environment with the DNS cache.
96  * @param qinfo: query info
97  * @param hash: hash over qinfo.
98  * @param rep: reply info, together with qinfo makes up the message.
99  *      Adjusts the reply info TTLs to absolute time.
100  * @param leeway: TTL value, if not 0, other rrsets are considered expired
101  *      that many seconds before actual TTL expiry.
102  * @param pside: if true, information came from a server which was fetched
103  *      from the parentside of the zonecut.  This means that the type NS
104  *      can be updated to full TTL even in prefetch situations.
105  * @param qrep: message that can be altered with better rrs from cache.
106  * @param region: to allocate into for qmsg.
107  */
108 void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
109         hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside,
110         struct reply_info* qrep, struct regional* region);
111
112 /**
113  * Find a delegation from the cache.
114  * @param env: module environment with the DNS cache.
115  * @param qname: query name.
116  * @param qnamelen: length of qname.
117  * @param qtype: query type.
118  * @param qclass: query class.
119  * @param region: where to allocate result delegation.
120  * @param msg: if not NULL, delegation message is returned here, synthesized
121  *      from the cache.
122  * @param timenow: the time now, for checking if TTL on cache entries is OK.
123  * @return new delegation or NULL on error or if not found in cache.
124  */
125 struct delegpt* dns_cache_find_delegation(struct module_env* env, 
126         uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 
127         struct regional* region, struct dns_msg** msg, time_t timenow);
128
129 /**
130  * generate dns_msg from cached message
131  * @param env: module environment with the DNS cache. NULL if the LRU from cache
132  *      does not need to be touched.
133  * @param q: query info, contains qname that will make up the dns message.
134  * @param r: reply info that, together with qname, will make up the dns message.
135  * @param region: where to allocate dns message.
136  * @param now: the time now, for check if TTL on cache entry is ok.
137  * @param scratch: where to allocate temporary data.
138  * */
139 struct dns_msg* tomsg(struct module_env* env, struct query_info* q,
140         struct reply_info* r, struct regional* region, time_t now,
141         struct regional* scratch);
142
143 /** 
144  * Find cached message 
145  * @param env: module environment with the DNS cache.
146  * @param qname: query name.
147  * @param qnamelen: length of qname.
148  * @param qtype: query type.
149  * @param qclass: query class.
150  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
151  * @param region: where to allocate result.
152  * @param scratch: where to allocate temporary data.
153  * @return new response message (alloced in region, rrsets do not have IDs).
154  *      or NULL on error or if not found in cache.
155  *      TTLs are made relative to the current time.
156  */
157 struct dns_msg* dns_cache_lookup(struct module_env* env,
158         uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
159         uint16_t flags, struct regional* region, struct regional* scratch);
160
161 /** 
162  * find and add A and AAAA records for missing nameservers in delegpt 
163  * @param env: module environment with rrset cache
164  * @param qclass: which class to look in.
165  * @param region: where to store new dp info.
166  * @param dp: delegation point to fill missing entries.
167  * @return false on alloc failure.
168  */
169 int cache_fill_missing(struct module_env* env, uint16_t qclass, 
170         struct regional* region, struct delegpt* dp);
171
172 /**
173  * Utility, create new, unpacked data structure for cache response.
174  * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
175  * @param qname: query section name
176  * @param qnamelen: len of qname
177  * @param qtype: query section type
178  * @param qclass: query section class
179  * @param region: where to alloc.
180  * @param capacity: number of rrsets space to create in the array.
181  * @return new dns_msg struct or NULL on mem fail.
182  */
183 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 
184         uint16_t qclass, struct regional* region, size_t capacity);
185
186 /**
187  * Add rrset to authority section in unpacked dns_msg message. Must have enough
188  * space left, does not grow the array.
189  * @param msg: msg to put it in.
190  * @param region: region to alloc in
191  * @param rrset: to add in authority section
192  * @param now: now.
193  * @return true if worked, false on fail
194  */
195 int dns_msg_authadd(struct dns_msg* msg, struct regional* region, 
196         struct ub_packed_rrset_key* rrset, time_t now);
197
198 /**
199  * Adjust the prefetch_ttl for a cached message.  This adds a value to the
200  * prefetch ttl - postponing the time when it will be prefetched for future
201  * incoming queries.
202  * @param env: module environment with caches and time.
203  * @param qinfo: query info for the query that needs adjustment.
204  * @param adjust: time in seconds to add to the prefetch_leeway.
205  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
206  * @return false if not in cache. true if added.
207  */
208 int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
209         time_t adjust, uint16_t flags);
210
211 /** lookup message in message cache
212  * the returned nonNULL entry is locked and has to be unlocked by the caller */
213 struct msgreply_entry* msg_cache_lookup(struct module_env* env,
214         uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
215         uint16_t flags, time_t now, int wr);
216
217 #endif /* SERVICES_CACHE_DNS_H */