2 * services/cache/dns.h - Cache services for DNS using msg and rrset caches.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains the DNS cache.
42 #ifndef SERVICES_CACHE_DNS_H
43 #define SERVICES_CACHE_DNS_H
44 #include "util/storage/lruhash.h"
45 #include "util/data/msgreply.h"
52 /** Flags to control behavior of dns_cache_store() and dns_cache_store_msg().
53 * Must be an unsigned 32-bit value larger than 0xffff */
55 /** Allow caching a DNS message with a zero TTL. */
56 #define DNSCACHE_STORE_ZEROTTL 0x100000
59 * Region allocated message reply
63 struct query_info qinfo;
64 /** reply info - ptr to packed repinfo structure */
65 struct reply_info *rep;
69 * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
71 * @param env: environment, with alloc structure and dns cache.
72 * @param qinf: query info, the query for which answer is stored.
73 * this is allocated in a region, and will be copied to malloc area
75 * @param rep: reply in dns_msg from dns_alloc_msg for example.
76 * this is allocated in a region, and will be copied to malloc area
78 * @param is_referral: If true, then the given message to be stored is a
79 * referral. The cache implementation may use this as a hint.
80 * It will store only the RRsets, not the message.
81 * @param leeway: TTL value, if not 0, other rrsets are considered expired
82 * that many seconds before actual TTL expiry.
83 * @param pside: if true, information came from a server which was fetched
84 * from the parentside of the zonecut. This means that the type NS
85 * can be updated to full TTL even in prefetch situations.
86 * @param region: region to allocate better entries from cache into.
87 * (used when is_referral is false).
88 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
89 * The higher 16 bits are used internally to customize the cache policy.
90 * (See DNSCACHE_STORE_xxx flags).
91 * @return 0 on alloc error (out of memory).
93 int dns_cache_store(struct module_env* env, struct query_info* qinf,
94 struct reply_info* rep, int is_referral, time_t leeway, int pside,
95 struct regional* region, uint32_t flags);
98 * Store message in the cache. Stores in message cache and rrset cache.
99 * Both qinfo and rep should be malloced and are put in the cache.
100 * They should not be used after this call, as they are then in shared cache.
101 * Does not return errors, they are logged and only lead to less cache.
103 * @param env: module environment with the DNS cache.
104 * @param qinfo: query info
105 * @param hash: hash over qinfo.
106 * @param rep: reply info, together with qinfo makes up the message.
107 * Adjusts the reply info TTLs to absolute time.
108 * @param leeway: TTL value, if not 0, other rrsets are considered expired
109 * that many seconds before actual TTL expiry.
110 * @param pside: if true, information came from a server which was fetched
111 * from the parentside of the zonecut. This means that the type NS
112 * can be updated to full TTL even in prefetch situations.
113 * @param qrep: message that can be altered with better rrs from cache.
114 * @param flags: customization flags for the cache policy.
115 * @param region: to allocate into for qmsg.
117 void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
118 hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside,
119 struct reply_info* qrep, uint32_t flags, struct regional* region);
122 * Find a delegation from the cache.
123 * @param env: module environment with the DNS cache.
124 * @param qname: query name.
125 * @param qnamelen: length of qname.
126 * @param qtype: query type.
127 * @param qclass: query class.
128 * @param region: where to allocate result delegation.
129 * @param msg: if not NULL, delegation message is returned here, synthesized
131 * @param timenow: the time now, for checking if TTL on cache entries is OK.
132 * @return new delegation or NULL on error or if not found in cache.
134 struct delegpt* dns_cache_find_delegation(struct module_env* env,
135 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
136 struct regional* region, struct dns_msg** msg, time_t timenow);
139 * generate dns_msg from cached message
140 * @param env: module environment with the DNS cache. NULL if the LRU from cache
141 * does not need to be touched.
142 * @param q: query info, contains qname that will make up the dns message.
143 * @param r: reply info that, together with qname, will make up the dns message.
144 * @param region: where to allocate dns message.
145 * @param now: the time now, for check if TTL on cache entry is ok.
146 * @param scratch: where to allocate temporary data.
148 struct dns_msg* tomsg(struct module_env* env, struct query_info* q,
149 struct reply_info* r, struct regional* region, time_t now,
150 struct regional* scratch);
153 * Find cached message
154 * @param env: module environment with the DNS cache.
155 * @param qname: query name.
156 * @param qnamelen: length of qname.
157 * @param qtype: query type.
158 * @param qclass: query class.
159 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
160 * @param region: where to allocate result.
161 * @param scratch: where to allocate temporary data.
162 * @return new response message (alloced in region, rrsets do not have IDs).
163 * or NULL on error or if not found in cache.
164 * TTLs are made relative to the current time.
166 struct dns_msg* dns_cache_lookup(struct module_env* env,
167 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
168 uint16_t flags, struct regional* region, struct regional* scratch);
171 * find and add A and AAAA records for missing nameservers in delegpt
172 * @param env: module environment with rrset cache
173 * @param qclass: which class to look in.
174 * @param region: where to store new dp info.
175 * @param dp: delegation point to fill missing entries.
176 * @return false on alloc failure.
178 int cache_fill_missing(struct module_env* env, uint16_t qclass,
179 struct regional* region, struct delegpt* dp);
182 * Utility, create new, unpacked data structure for cache response.
183 * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
184 * @param qname: query section name
185 * @param qnamelen: len of qname
186 * @param qtype: query section type
187 * @param qclass: query section class
188 * @param region: where to alloc.
189 * @param capacity: number of rrsets space to create in the array.
190 * @return new dns_msg struct or NULL on mem fail.
192 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
193 uint16_t qclass, struct regional* region, size_t capacity);
196 * Add rrset to authority section in unpacked dns_msg message. Must have enough
197 * space left, does not grow the array.
198 * @param msg: msg to put it in.
199 * @param region: region to alloc in
200 * @param rrset: to add in authority section
202 * @return true if worked, false on fail
204 int dns_msg_authadd(struct dns_msg* msg, struct regional* region,
205 struct ub_packed_rrset_key* rrset, time_t now);
208 * Adjust the prefetch_ttl for a cached message. This adds a value to the
209 * prefetch ttl - postponing the time when it will be prefetched for future
211 * @param env: module environment with caches and time.
212 * @param qinfo: query info for the query that needs adjustment.
213 * @param adjust: time in seconds to add to the prefetch_leeway.
214 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
215 * @return false if not in cache. true if added.
217 int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
218 time_t adjust, uint16_t flags);
220 /** lookup message in message cache
221 * the returned nonNULL entry is locked and has to be unlocked by the caller */
222 struct msgreply_entry* msg_cache_lookup(struct module_env* env,
223 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
224 uint16_t flags, time_t now, int wr);
226 #endif /* SERVICES_CACHE_DNS_H */