1238106Sdes/* 2238106Sdes * services/cache/dns.h - Cache services for DNS using msg and rrset caches. 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains the DNS cache. 40238106Sdes */ 41238106Sdes 42238106Sdes#ifndef SERVICES_CACHE_DNS_H 43238106Sdes#define SERVICES_CACHE_DNS_H 44238106Sdes#include "util/storage/lruhash.h" 45238106Sdes#include "util/data/msgreply.h" 46238106Sdesstruct module_env; 47238106Sdesstruct query_info; 48238106Sdesstruct reply_info; 49238106Sdesstruct regional; 50238106Sdesstruct delegpt; 51238106Sdes 52238106Sdes/** 53238106Sdes * Region allocated message reply 54238106Sdes */ 55238106Sdesstruct dns_msg { 56238106Sdes /** query info */ 57238106Sdes struct query_info qinfo; 58238106Sdes /** reply info - ptr to packed repinfo structure */ 59238106Sdes struct reply_info *rep; 60238106Sdes}; 61238106Sdes 62238106Sdes/** 63238106Sdes * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 64238106Sdes * 65238106Sdes * @param env: environment, with alloc structure and dns cache. 66238106Sdes * @param qinf: query info, the query for which answer is stored. 67238106Sdes * this is allocated in a region, and will be copied to malloc area 68238106Sdes * before insertion. 69238106Sdes * @param rep: reply in dns_msg from dns_alloc_msg for example. 70238106Sdes * this is allocated in a region, and will be copied to malloc area 71238106Sdes * before insertion. 72238106Sdes * @param is_referral: If true, then the given message to be stored is a 73238106Sdes * referral. The cache implementation may use this as a hint. 74238106Sdes * It will store only the RRsets, not the message. 75238106Sdes * @param leeway: TTL value, if not 0, other rrsets are considered expired 76238106Sdes * that many seconds before actual TTL expiry. 77238106Sdes * @param pside: if true, information came from a server which was fetched 78238106Sdes * from the parentside of the zonecut. This means that the type NS 79238106Sdes * can be updated to full TTL even in prefetch situations. 80238106Sdes * @param region: region to allocate better entries from cache into. 81238106Sdes * (used when is_referral is false). 82285206Sdes * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 83238106Sdes * @return 0 on alloc error (out of memory). 84238106Sdes */ 85238106Sdesint dns_cache_store(struct module_env* env, struct query_info* qinf, 86269257Sdes struct reply_info* rep, int is_referral, time_t leeway, int pside, 87285206Sdes struct regional* region, uint16_t flags); 88238106Sdes 89238106Sdes/** 90238106Sdes * Store message in the cache. Stores in message cache and rrset cache. 91238106Sdes * Both qinfo and rep should be malloced and are put in the cache. 92238106Sdes * They should not be used after this call, as they are then in shared cache. 93238106Sdes * Does not return errors, they are logged and only lead to less cache. 94238106Sdes * 95238106Sdes * @param env: module environment with the DNS cache. 96238106Sdes * @param qinfo: query info 97238106Sdes * @param hash: hash over qinfo. 98238106Sdes * @param rep: reply info, together with qinfo makes up the message. 99238106Sdes * Adjusts the reply info TTLs to absolute time. 100238106Sdes * @param leeway: TTL value, if not 0, other rrsets are considered expired 101238106Sdes * that many seconds before actual TTL expiry. 102238106Sdes * @param pside: if true, information came from a server which was fetched 103238106Sdes * from the parentside of the zonecut. This means that the type NS 104238106Sdes * can be updated to full TTL even in prefetch situations. 105238106Sdes * @param qrep: message that can be altered with better rrs from cache. 106238106Sdes * @param region: to allocate into for qmsg. 107238106Sdes */ 108238106Sdesvoid dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 109269257Sdes hashvalue_t hash, struct reply_info* rep, time_t leeway, int pside, 110238106Sdes struct reply_info* qrep, struct regional* region); 111238106Sdes 112238106Sdes/** 113238106Sdes * Find a delegation from the cache. 114238106Sdes * @param env: module environment with the DNS cache. 115238106Sdes * @param qname: query name. 116238106Sdes * @param qnamelen: length of qname. 117238106Sdes * @param qtype: query type. 118238106Sdes * @param qclass: query class. 119238106Sdes * @param region: where to allocate result delegation. 120238106Sdes * @param msg: if not NULL, delegation message is returned here, synthesized 121238106Sdes * from the cache. 122238106Sdes * @param timenow: the time now, for checking if TTL on cache entries is OK. 123238106Sdes * @return new delegation or NULL on error or if not found in cache. 124238106Sdes */ 125238106Sdesstruct delegpt* dns_cache_find_delegation(struct module_env* env, 126238106Sdes uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 127269257Sdes struct regional* region, struct dns_msg** msg, time_t timenow); 128238106Sdes 129238106Sdes/** 130238106Sdes * Find cached message 131238106Sdes * @param env: module environment with the DNS cache. 132238106Sdes * @param qname: query name. 133238106Sdes * @param qnamelen: length of qname. 134238106Sdes * @param qtype: query type. 135238106Sdes * @param qclass: query class. 136285206Sdes * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 137238106Sdes * @param region: where to allocate result. 138238106Sdes * @param scratch: where to allocate temporary data. 139238106Sdes * @return new response message (alloced in region, rrsets do not have IDs). 140238106Sdes * or NULL on error or if not found in cache. 141238106Sdes * TTLs are made relative to the current time. 142238106Sdes */ 143238106Sdesstruct dns_msg* dns_cache_lookup(struct module_env* env, 144238106Sdes uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 145285206Sdes uint16_t flags, struct regional* region, struct regional* scratch); 146238106Sdes 147238106Sdes/** 148238106Sdes * find and add A and AAAA records for missing nameservers in delegpt 149238106Sdes * @param env: module environment with rrset cache 150238106Sdes * @param qclass: which class to look in. 151238106Sdes * @param region: where to store new dp info. 152238106Sdes * @param dp: delegation point to fill missing entries. 153238106Sdes * @return false on alloc failure. 154238106Sdes */ 155238106Sdesint cache_fill_missing(struct module_env* env, uint16_t qclass, 156238106Sdes struct regional* region, struct delegpt* dp); 157238106Sdes 158238106Sdes/** 159238106Sdes * Utility, create new, unpacked data structure for cache response. 160238106Sdes * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 161238106Sdes * @param qname: query section name 162238106Sdes * @param qnamelen: len of qname 163238106Sdes * @param qtype: query section type 164238106Sdes * @param qclass: query section class 165238106Sdes * @param region: where to alloc. 166238106Sdes * @param capacity: number of rrsets space to create in the array. 167238106Sdes * @return new dns_msg struct or NULL on mem fail. 168238106Sdes */ 169238106Sdesstruct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 170238106Sdes uint16_t qclass, struct regional* region, size_t capacity); 171238106Sdes 172238106Sdes/** 173238106Sdes * Add rrset to authority section in unpacked dns_msg message. Must have enough 174238106Sdes * space left, does not grow the array. 175238106Sdes * @param msg: msg to put it in. 176238106Sdes * @param region: region to alloc in 177238106Sdes * @param rrset: to add in authority section 178238106Sdes * @param now: now. 179238106Sdes * @return true if worked, false on fail 180238106Sdes */ 181238106Sdesint dns_msg_authadd(struct dns_msg* msg, struct regional* region, 182269257Sdes struct ub_packed_rrset_key* rrset, time_t now); 183238106Sdes 184285206Sdes/** 185285206Sdes * Adjust the prefetch_ttl for a cached message. This adds a value to the 186285206Sdes * prefetch ttl - postponing the time when it will be prefetched for future 187285206Sdes * incoming queries. 188285206Sdes * @param env: module environment with caches and time. 189285206Sdes * @param qinfo: query info for the query that needs adjustment. 190285206Sdes * @param adjust: time in seconds to add to the prefetch_leeway. 191285206Sdes * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 192285206Sdes * @return false if not in cache. true if added. 193285206Sdes */ 194285206Sdesint dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo, 195285206Sdes time_t adjust, uint16_t flags); 196285206Sdes 197238106Sdes#endif /* SERVICES_CACHE_DNS_H */ 198