dns.h revision 266114
165793Smsmith/* 265793Smsmith * services/cache/dns.h - Cache services for DNS using msg and rrset caches. 381082Sscottl * 465793Smsmith * Copyright (c) 2007, NLnet Labs. All rights reserved. 581150Sscottl * 665793Smsmith * This software is open source. 765793Smsmith * 865793Smsmith * Redistribution and use in source and binary forms, with or without 965793Smsmith * modification, are permitted provided that the following conditions 1065793Smsmith * are met: 1165793Smsmith * 1265793Smsmith * Redistributions of source code must retain the above copyright notice, 1365793Smsmith * this list of conditions and the following disclaimer. 1465793Smsmith * 1565793Smsmith * Redistributions in binary form must reproduce the above copyright notice, 1665793Smsmith * this list of conditions and the following disclaimer in the documentation 1765793Smsmith * and/or other materials provided with the distribution. 1865793Smsmith * 1965793Smsmith * Neither the name of the NLNET LABS nor the names of its contributors may 2065793Smsmith * be used to endorse or promote products derived from this software without 2165793Smsmith * specific prior written permission. 2265793Smsmith * 2365793Smsmith * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2465793Smsmith * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2565793Smsmith * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2665793Smsmith * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2765793Smsmith * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2865793Smsmith * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2965793Smsmith * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30119418Sobrien * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31119418Sobrien * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32119418Sobrien * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3365793Smsmith * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3465793Smsmith */ 3565793Smsmith 3665793Smsmith/** 3781151Sscottl * \file 3881151Sscottl * 3965793Smsmith * This file contains the DNS cache. 4065793Smsmith */ 4165793Smsmith 42129879Sphk#ifndef SERVICES_CACHE_DNS_H 4365793Smsmith#define SERVICES_CACHE_DNS_H 44111691Sscottl#include "util/storage/lruhash.h" 4565793Smsmith#include "util/data/msgreply.h" 4665793Smsmithstruct module_env; 4765793Smsmithstruct query_info; 4865793Smsmithstruct reply_info; 4965793Smsmithstruct regional; 5065793Smsmithstruct delegpt; 5165793Smsmith 5265793Smsmith/** 53119273Simp * Region allocated message reply 54119273Simp */ 5565793Smsmithstruct dns_msg { 5665793Smsmith /** query info */ 57138635Sscottl struct query_info qinfo; 5865793Smsmith /** reply info - ptr to packed repinfo structure */ 5965793Smsmith struct reply_info *rep; 6083114Sscottl}; 6183114Sscottl 6265793Smsmith/** 63247570Smarius * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 64247570Smarius * 65247570Smarius * @param env: environment, with alloc structure and dns cache. 66247570Smarius * @param qinf: query info, the query for which answer is stored. 67247570Smarius * this is allocated in a region, and will be copied to malloc area 6865793Smsmith * before insertion. 6983114Sscottl * @param rep: reply in dns_msg from dns_alloc_msg for example. 7083114Sscottl * this is allocated in a region, and will be copied to malloc area 7183114Sscottl * before insertion. 7283114Sscottl * @param is_referral: If true, then the given message to be stored is a 7383114Sscottl * referral. The cache implementation may use this as a hint. 7483114Sscottl * It will store only the RRsets, not the message. 7565793Smsmith * @param leeway: TTL value, if not 0, other rrsets are considered expired 76227843Smarius * that many seconds before actual TTL expiry. 7765793Smsmith * @param pside: if true, information came from a server which was fetched 7865793Smsmith * from the parentside of the zonecut. This means that the type NS 7965793Smsmith * can be updated to full TTL even in prefetch situations. 8065793Smsmith * @param region: region to allocate better entries from cache into. 8165793Smsmith * (used when is_referral is false). 8265793Smsmith * @return 0 on alloc error (out of memory). 8365793Smsmith */ 8465793Smsmithint dns_cache_store(struct module_env* env, struct query_info* qinf, 8589112Smsmith struct reply_info* rep, int is_referral, time_t leeway, int pside, 8689112Smsmith struct regional* region); 87247570Smarius 88165102Smjacob/** 8965793Smsmith * Store message in the cache. Stores in message cache and rrset cache. 90247570Smarius * Both qinfo and rep should be malloced and are put in the cache. 9165793Smsmith * They should not be used after this call, as they are then in shared cache. 9283114Sscottl * Does not return errors, they are logged and only lead to less cache. 9383114Sscottl * 9483114Sscottl * @param env: module environment with the DNS cache. 9583114Sscottl * @param qinfo: query info 9683114Sscottl * @param hash: hash over qinfo. 9795536Sscottl * @param rep: reply info, together with qinfo makes up the message. 98247570Smarius * Adjusts the reply info TTLs to absolute time. 9965793Smsmith * @param leeway: TTL value, if not 0, other rrsets are considered expired 100112679Sscottl * that many seconds before actual TTL expiry. 10195536Sscottl * @param pside: if true, information came from a server which was fetched 102112679Sscottl * from the parentside of the zonecut. This means that the type NS 10395536Sscottl * can be updated to full TTL even in prefetch situations. 104112679Sscottl * @param qrep: message that can be altered with better rrs from cache. 10595536Sscottl * @param region: to allocate into for qmsg. 106112679Sscottl */ 10795536Sscottlvoid dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 108112679Sscottl hashvalue_t hash, struct reply_info* rep, time_t leeway, int pside, 10995536Sscottl struct reply_info* qrep, struct regional* region); 110112679Sscottl 11195536Sscottl/** 112112679Sscottl * Find a delegation from the cache. 11395536Sscottl * @param env: module environment with the DNS cache. 114112679Sscottl * @param qname: query name. 11595536Sscottl * @param qnamelen: length of qname. 116112679Sscottl * @param qtype: query type. 11795536Sscottl * @param qclass: query class. 118112679Sscottl * @param region: where to allocate result delegation. 11995536Sscottl * @param msg: if not NULL, delegation message is returned here, synthesized 120117361Sscottl * from the cache. 121117361Sscottl * @param timenow: the time now, for checking if TTL on cache entries is OK. 122112679Sscottl * @return new delegation or NULL on error or if not found in cache. 123112679Sscottl */ 124112679Sscottlstruct delegpt* dns_cache_find_delegation(struct module_env* env, 125107797Sscottl uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 126112679Sscottl struct regional* region, struct dns_msg** msg, time_t timenow); 127112679Sscottl 128112679Sscottl/** 129112679Sscottl * Find cached message 130112679Sscottl * @param env: module environment with the DNS cache. 131112679Sscottl * @param qname: query name. 132115409Sscottl * @param qnamelen: length of qname. 133159702Sbrueffer * @param qtype: query type. 134127242Sscottl * @param qclass: query class. 135127242Sscottl * @param region: where to allocate result. 136127242Sscottl * @param scratch: where to allocate temporary data. 137159702Sbrueffer * @return new response message (alloced in region, rrsets do not have IDs). 138136756Sscottl * or NULL on error or if not found in cache. 139159702Sbrueffer * TTLs are made relative to the current time. 140148866Sps */ 141148866Spsstruct dns_msg* dns_cache_lookup(struct module_env* env, 142254004Smarius uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 143138884Sscottl struct regional* region, struct regional* scratch); 144133606Sscottl 145133606Sscottl/** 146151086Sscottl * find and add A and AAAA records for missing nameservers in delegpt 147151086Sscottl * @param env: module environment with rrset cache 148151086Sscottl * @param qclass: which class to look in. 149151086Sscottl * @param region: where to store new dp info. 150151086Sscottl * @param dp: delegation point to fill missing entries. 151151086Sscottl * @return false on alloc failure. 152151086Sscottl */ 153151086Sscottlint cache_fill_missing(struct module_env* env, uint16_t qclass, 154151086Sscottl struct regional* region, struct delegpt* dp); 155151086Sscottl 156151086Sscottl/** 157151086Sscottl * Utility, create new, unpacked data structure for cache response. 158151086Sscottl * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 159151086Sscottl * @param qname: query section name 160254004Smarius * @param qnamelen: len of qname 161151086Sscottl * @param qtype: query section type 162151086Sscottl * @param qclass: query section class 163151086Sscottl * @param region: where to alloc. 164151086Sscottl * @param capacity: number of rrsets space to create in the array. 165151086Sscottl * @return new dns_msg struct or NULL on mem fail. 166151086Sscottl */ 167159702Sbruefferstruct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 168151086Sscottl uint16_t qclass, struct regional* region, size_t capacity); 169159702Sbrueffer 170151086Sscottl/** 171151086Sscottl * Add rrset to authority section in unpacked dns_msg message. Must have enough 172151086Sscottl * space left, does not grow the array. 173151086Sscottl * @param msg: msg to put it in. 174151086Sscottl * @param region: region to alloc in 175151086Sscottl * @param rrset: to add in authority section 176151086Sscottl * @param now: now. 177151086Sscottl * @return true if worked, false on fail 178172653Semaste */ 179172653Semasteint dns_msg_authadd(struct dns_msg* msg, struct regional* region, 180151086Sscottl struct ub_packed_rrset_key* rrset, time_t now); 181206540Semaste 182151086Sscottl#endif /* SERVICES_CACHE_DNS_H */ 183151086Sscottl