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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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"
53 * Region allocated message reply
57 struct query_info qinfo;
58 /** reply info - ptr to packed repinfo structure */
59 struct reply_info *rep;
63 * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
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
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
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 * @return 0 on alloc error (out of memory).
84 int dns_cache_store(struct module_env* env, struct query_info* qinf,
85 struct reply_info* rep, int is_referral, uint32_t leeway, int pside,
86 struct regional* region);
89 * Store message in the cache. Stores in message cache and rrset cache.
90 * Both qinfo and rep should be malloced and are put in the cache.
91 * They should not be used after this call, as they are then in shared cache.
92 * Does not return errors, they are logged and only lead to less cache.
94 * @param env: module environment with the DNS cache.
95 * @param qinfo: query info
96 * @param hash: hash over qinfo.
97 * @param rep: reply info, together with qinfo makes up the message.
98 * Adjusts the reply info TTLs to absolute time.
99 * @param leeway: TTL value, if not 0, other rrsets are considered expired
100 * that many seconds before actual TTL expiry.
101 * @param pside: if true, information came from a server which was fetched
102 * from the parentside of the zonecut. This means that the type NS
103 * can be updated to full TTL even in prefetch situations.
104 * @param qrep: message that can be altered with better rrs from cache.
105 * @param region: to allocate into for qmsg.
107 void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
108 hashvalue_t hash, struct reply_info* rep, uint32_t leeway, int pside,
109 struct reply_info* qrep, struct regional* region);
112 * Find a delegation from the cache.
113 * @param env: module environment with the DNS cache.
114 * @param qname: query name.
115 * @param qnamelen: length of qname.
116 * @param qtype: query type.
117 * @param qclass: query class.
118 * @param region: where to allocate result delegation.
119 * @param msg: if not NULL, delegation message is returned here, synthesized
121 * @param timenow: the time now, for checking if TTL on cache entries is OK.
122 * @return new delegation or NULL on error or if not found in cache.
124 struct delegpt* dns_cache_find_delegation(struct module_env* env,
125 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
126 struct regional* region, struct dns_msg** msg, uint32_t timenow);
129 * Find cached message
130 * @param env: module environment with the DNS cache.
131 * @param qname: query name.
132 * @param qnamelen: length of qname.
133 * @param qtype: query type.
134 * @param qclass: query class.
135 * @param region: where to allocate result.
136 * @param scratch: where to allocate temporary data.
137 * @return new response message (alloced in region, rrsets do not have IDs).
138 * or NULL on error or if not found in cache.
139 * TTLs are made relative to the current time.
141 struct dns_msg* dns_cache_lookup(struct module_env* env,
142 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
143 struct regional* region, struct regional* scratch);
146 * find and add A and AAAA records for missing nameservers in delegpt
147 * @param env: module environment with rrset cache
148 * @param qclass: which class to look in.
149 * @param region: where to store new dp info.
150 * @param dp: delegation point to fill missing entries.
151 * @return false on alloc failure.
153 int cache_fill_missing(struct module_env* env, uint16_t qclass,
154 struct regional* region, struct delegpt* dp);
157 * Utility, create new, unpacked data structure for cache response.
158 * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
159 * @param qname: query section name
160 * @param qnamelen: len of qname
161 * @param qtype: query section type
162 * @param qclass: query section class
163 * @param region: where to alloc.
164 * @param capacity: number of rrsets space to create in the array.
165 * @return new dns_msg struct or NULL on mem fail.
167 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
168 uint16_t qclass, struct regional* region, size_t capacity);
171 * Add rrset to authority section in unpacked dns_msg message. Must have enough
172 * space left, does not grow the array.
173 * @param msg: msg to put it in.
174 * @param region: region to alloc in
175 * @param rrset: to add in authority section
177 * @return true if worked, false on fail
179 int dns_msg_authadd(struct dns_msg* msg, struct regional* region,
180 struct ub_packed_rrset_key* rrset, uint32_t now);
182 #endif /* SERVICES_CACHE_DNS_H */