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_t 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 * Find cached message 131 * @param env: module environment with the DNS cache. 132 * @param qname: query name. 133 * @param qnamelen: length of qname. 134 * @param qtype: query type. 135 * @param qclass: query class. 136 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 137 * @param region: where to allocate result. 138 * @param scratch: where to allocate temporary data. 139 * @return new response message (alloced in region, rrsets do not have IDs). 140 * or NULL on error or if not found in cache. 141 * TTLs are made relative to the current time. 142 */ 143 struct dns_msg* dns_cache_lookup(struct module_env* env, 144 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 145 uint16_t flags, struct regional* region, struct regional* scratch); 146 147 /** 148 * find and add A and AAAA records for missing nameservers in delegpt 149 * @param env: module environment with rrset cache 150 * @param qclass: which class to look in. 151 * @param region: where to store new dp info. 152 * @param dp: delegation point to fill missing entries. 153 * @return false on alloc failure. 154 */ 155 int cache_fill_missing(struct module_env* env, uint16_t qclass, 156 struct regional* region, struct delegpt* dp); 157 158 /** 159 * Utility, create new, unpacked data structure for cache response. 160 * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 161 * @param qname: query section name 162 * @param qnamelen: len of qname 163 * @param qtype: query section type 164 * @param qclass: query section class 165 * @param region: where to alloc. 166 * @param capacity: number of rrsets space to create in the array. 167 * @return new dns_msg struct or NULL on mem fail. 168 */ 169 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 170 uint16_t qclass, struct regional* region, size_t capacity); 171 172 /** 173 * Add rrset to authority section in unpacked dns_msg message. Must have enough 174 * space left, does not grow the array. 175 * @param msg: msg to put it in. 176 * @param region: region to alloc in 177 * @param rrset: to add in authority section 178 * @param now: now. 179 * @return true if worked, false on fail 180 */ 181 int dns_msg_authadd(struct dns_msg* msg, struct regional* region, 182 struct ub_packed_rrset_key* rrset, time_t now); 183 184 /** 185 * Adjust the prefetch_ttl for a cached message. This adds a value to the 186 * prefetch ttl - postponing the time when it will be prefetched for future 187 * incoming queries. 188 * @param env: module environment with caches and time. 189 * @param qinfo: query info for the query that needs adjustment. 190 * @param adjust: time in seconds to add to the prefetch_leeway. 191 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 192 * @return false if not in cache. true if added. 193 */ 194 int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo, 195 time_t adjust, uint16_t flags); 196 197 #endif /* SERVICES_CACHE_DNS_H */ 198