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 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. 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 region: region to allocate better entries from cache into. 78 * (used when is_referral is false). 79 * @return 0 on alloc error (out of memory). 80 */ 81 int dns_cache_store(struct module_env* env, struct query_info* qinf, 82 struct reply_info* rep, int is_referral, uint32_t leeway, 83 struct regional* region); 84 85 /** 86 * Store message in the cache. Stores in message cache and rrset cache. 87 * Both qinfo and rep should be malloced and are put in the cache. 88 * They should not be used after this call, as they are then in shared cache. 89 * Does not return errors, they are logged and only lead to less cache. 90 * 91 * @param env: module environment with the DNS cache. 92 * @param qinfo: query info 93 * @param hash: hash over qinfo. 94 * @param rep: reply info, together with qinfo makes up the message. 95 * Adjusts the reply info TTLs to absolute time. 96 * @param leeway: TTL value, if not 0, other rrsets are considered expired 97 * that many seconds before actual TTL expiry. 98 * @param qrep: message that can be altered with better rrs from cache. 99 * @param region: to allocate into for qmsg. 100 */ 101 void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 102 hashvalue_t hash, struct reply_info* rep, uint32_t leeway, 103 struct reply_info* qrep, struct regional* region); 104 105 /** 106 * Find a delegation from the cache. 107 * @param env: module environment with the DNS cache. 108 * @param qname: query name. 109 * @param qnamelen: length of qname. 110 * @param qtype: query type. 111 * @param qclass: query class. 112 * @param region: where to allocate result delegation. 113 * @param msg: if not NULL, delegation message is returned here, synthesized 114 * from the cache. 115 * @param timenow: the time now, for checking if TTL on cache entries is OK. 116 * @return new delegation or NULL on error or if not found in cache. 117 */ 118 struct delegpt* dns_cache_find_delegation(struct module_env* env, 119 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 120 struct regional* region, struct dns_msg** msg, uint32_t timenow); 121 122 /** 123 * Find cached message 124 * @param env: module environment with the DNS cache. 125 * @param qname: query name. 126 * @param qnamelen: length of qname. 127 * @param qtype: query type. 128 * @param qclass: query class. 129 * @param region: where to allocate result. 130 * @param scratch: where to allocate temporary data. 131 * @return new response message (alloced in region, rrsets do not have IDs). 132 * or NULL on error or if not found in cache. 133 * TTLs are made relative to the current time. 134 */ 135 struct dns_msg* dns_cache_lookup(struct module_env* env, 136 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 137 struct regional* region, struct regional* scratch); 138 139 /** 140 * find and add A and AAAA records for missing nameservers in delegpt 141 * @param env: module environment with rrset cache 142 * @param qclass: which class to look in. 143 * @param region: where to store new dp info. 144 * @param dp: delegation point to fill missing entries. 145 * @return false on alloc failure. 146 */ 147 int cache_fill_missing(struct module_env* env, uint16_t qclass, 148 struct regional* region, struct delegpt* dp); 149 150 /** 151 * Utility, create new, unpacked data structure for cache response. 152 * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 153 * @param qname: query section name 154 * @param qnamelen: len of qname 155 * @param qtype: query section type 156 * @param qclass: query section class 157 * @param region: where to alloc. 158 * @param capacity: number of rrsets space to create in the array. 159 * @return new dns_msg struct or NULL on mem fail. 160 */ 161 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 162 uint16_t qclass, struct regional* region, size_t capacity); 163 164 /** 165 * Add rrset to authority section in unpacked dns_msg message. Must have enough 166 * space left, does not grow the array. 167 * @param msg: msg to put it in. 168 * @param region: region to alloc in 169 * @param rrset: to add in authority section 170 * @param now: now. 171 * @return true if worked, false on fail 172 */ 173 int dns_msg_authadd(struct dns_msg* msg, struct regional* region, 174 struct ub_packed_rrset_key* rrset, uint32_t now); 175 176 #endif /* SERVICES_CACHE_DNS_H */ 177