Home | History | Annotate | Line # | Download | only in cache
dns.h revision 1.1.1.6.2.1
      1          1.1  christos /*
      2          1.1  christos  * services/cache/dns.h - Cache services for DNS using msg and rrset caches.
      3          1.1  christos  *
      4          1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5          1.1  christos  *
      6          1.1  christos  * This software is open source.
      7          1.1  christos  *
      8          1.1  christos  * Redistribution and use in source and binary forms, with or without
      9          1.1  christos  * modification, are permitted provided that the following conditions
     10          1.1  christos  * are met:
     11          1.1  christos  *
     12          1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13          1.1  christos  * this list of conditions and the following disclaimer.
     14          1.1  christos  *
     15          1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16          1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17          1.1  christos  * and/or other materials provided with the distribution.
     18          1.1  christos  *
     19          1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20          1.1  christos  * be used to endorse or promote products derived from this software without
     21          1.1  christos  * specific prior written permission.
     22          1.1  christos  *
     23          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27          1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28          1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29          1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30          1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31          1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32          1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33          1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34          1.1  christos  */
     35          1.1  christos 
     36          1.1  christos /**
     37          1.1  christos  * \file
     38          1.1  christos  *
     39          1.1  christos  * This file contains the DNS cache.
     40          1.1  christos  */
     41          1.1  christos 
     42          1.1  christos #ifndef SERVICES_CACHE_DNS_H
     43          1.1  christos #define SERVICES_CACHE_DNS_H
     44          1.1  christos #include "util/storage/lruhash.h"
     45          1.1  christos #include "util/data/msgreply.h"
     46          1.1  christos struct module_env;
     47          1.1  christos struct query_info;
     48          1.1  christos struct reply_info;
     49          1.1  christos struct regional;
     50          1.1  christos struct delegpt;
     51          1.1  christos 
     52      1.1.1.2  christos /** Flags to control behavior of dns_cache_store() and dns_cache_store_msg().
     53      1.1.1.2  christos  *  Must be an unsigned 32-bit value larger than 0xffff */
     54      1.1.1.2  christos 
     55      1.1.1.2  christos /** Allow caching a DNS message with a zero TTL. */
     56      1.1.1.2  christos #define DNSCACHE_STORE_ZEROTTL 0x100000
     57      1.1.1.2  christos 
     58          1.1  christos /**
     59          1.1  christos  * Region allocated message reply
     60          1.1  christos  */
     61          1.1  christos struct dns_msg {
     62          1.1  christos 	/** query info */
     63          1.1  christos 	struct query_info qinfo;
     64          1.1  christos 	/** reply info - ptr to packed repinfo structure */
     65          1.1  christos 	struct reply_info *rep;
     66          1.1  christos };
     67          1.1  christos 
     68          1.1  christos /**
     69          1.1  christos  * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
     70          1.1  christos  *
     71          1.1  christos  * @param env: environment, with alloc structure and dns cache.
     72          1.1  christos  * @param qinf: query info, the query for which answer is stored.
     73          1.1  christos  * 	this is allocated in a region, and will be copied to malloc area
     74          1.1  christos  * 	before insertion.
     75          1.1  christos  * @param rep: reply in dns_msg from dns_alloc_msg for example.
     76          1.1  christos  * 	this is allocated in a region, and will be copied to malloc area
     77          1.1  christos  * 	before insertion.
     78          1.1  christos  * @param is_referral: If true, then the given message to be stored is a
     79          1.1  christos  *      referral. The cache implementation may use this as a hint.
     80          1.1  christos  *      It will store only the RRsets, not the message.
     81          1.1  christos  * @param leeway: TTL value, if not 0, other rrsets are considered expired
     82          1.1  christos  *	that many seconds before actual TTL expiry.
     83          1.1  christos  * @param pside: if true, information came from a server which was fetched
     84          1.1  christos  * 	from the parentside of the zonecut.  This means that the type NS
     85          1.1  christos  * 	can be updated to full TTL even in prefetch situations.
     86          1.1  christos  * @param region: region to allocate better entries from cache into.
     87          1.1  christos  *   (used when is_referral is false).
     88          1.1  christos  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
     89      1.1.1.2  christos  *   The higher 16 bits are used internally to customize the cache policy.
     90      1.1.1.2  christos  *   (See DNSCACHE_STORE_xxx flags).
     91      1.1.1.6  christos  * @param qstarttime: time when the query was started, and thus when the
     92      1.1.1.6  christos  * 	delegations were looked up.
     93          1.1  christos  * @return 0 on alloc error (out of memory).
     94          1.1  christos  */
     95          1.1  christos int dns_cache_store(struct module_env* env, struct query_info* qinf,
     96          1.1  christos         struct reply_info* rep, int is_referral, time_t leeway, int pside,
     97      1.1.1.6  christos 	struct regional* region, uint32_t flags, time_t qstarttime);
     98          1.1  christos 
     99          1.1  christos /**
    100          1.1  christos  * Store message in the cache. Stores in message cache and rrset cache.
    101          1.1  christos  * Both qinfo and rep should be malloced and are put in the cache.
    102          1.1  christos  * They should not be used after this call, as they are then in shared cache.
    103          1.1  christos  * Does not return errors, they are logged and only lead to less cache.
    104          1.1  christos  *
    105          1.1  christos  * @param env: module environment with the DNS cache.
    106          1.1  christos  * @param qinfo: query info
    107          1.1  christos  * @param hash: hash over qinfo.
    108          1.1  christos  * @param rep: reply info, together with qinfo makes up the message.
    109          1.1  christos  *	Adjusts the reply info TTLs to absolute time.
    110          1.1  christos  * @param leeway: TTL value, if not 0, other rrsets are considered expired
    111          1.1  christos  *	that many seconds before actual TTL expiry.
    112          1.1  christos  * @param pside: if true, information came from a server which was fetched
    113          1.1  christos  * 	from the parentside of the zonecut.  This means that the type NS
    114          1.1  christos  * 	can be updated to full TTL even in prefetch situations.
    115          1.1  christos  * @param qrep: message that can be altered with better rrs from cache.
    116      1.1.1.2  christos  * @param flags: customization flags for the cache policy.
    117      1.1.1.6  christos  * @param qstarttime: time when the query was started, and thus when the
    118      1.1.1.6  christos  * 	delegations were looked up.
    119          1.1  christos  * @param region: to allocate into for qmsg.
    120          1.1  christos  */
    121          1.1  christos void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
    122      1.1.1.2  christos 	hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside,
    123      1.1.1.6  christos 	struct reply_info* qrep, uint32_t flags, struct regional* region,
    124      1.1.1.6  christos 	time_t qstarttime);
    125          1.1  christos 
    126          1.1  christos /**
    127          1.1  christos  * Find a delegation from the cache.
    128          1.1  christos  * @param env: module environment with the DNS cache.
    129          1.1  christos  * @param qname: query name.
    130          1.1  christos  * @param qnamelen: length of qname.
    131          1.1  christos  * @param qtype: query type.
    132          1.1  christos  * @param qclass: query class.
    133          1.1  christos  * @param region: where to allocate result delegation.
    134          1.1  christos  * @param msg: if not NULL, delegation message is returned here, synthesized
    135          1.1  christos  *	from the cache.
    136          1.1  christos  * @param timenow: the time now, for checking if TTL on cache entries is OK.
    137      1.1.1.6  christos  * @param noexpiredabove: if set, no expired NS rrsets above the one found
    138      1.1.1.6  christos  * 	are tolerated. It only returns delegations where the delegations above
    139      1.1.1.6  christos  * 	it are valid.
    140      1.1.1.6  christos  * @param expiretop: if not NULL, name where check for expiry ends for
    141      1.1.1.6  christos  * 	noexpiredabove.
    142      1.1.1.6  christos  * @param expiretoplen: length of expiretop dname.
    143          1.1  christos  * @return new delegation or NULL on error or if not found in cache.
    144          1.1  christos  */
    145          1.1  christos struct delegpt* dns_cache_find_delegation(struct module_env* env,
    146          1.1  christos 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
    147      1.1.1.6  christos 	struct regional* region, struct dns_msg** msg, time_t timenow,
    148      1.1.1.6  christos 	int noexpiredabove, uint8_t* expiretop, size_t expiretoplen);
    149          1.1  christos 
    150      1.1.1.2  christos /**
    151      1.1.1.2  christos  * generate dns_msg from cached message
    152      1.1.1.2  christos  * @param env: module environment with the DNS cache. NULL if the LRU from cache
    153      1.1.1.2  christos  * 	does not need to be touched.
    154      1.1.1.2  christos  * @param q: query info, contains qname that will make up the dns message.
    155      1.1.1.2  christos  * @param r: reply info that, together with qname, will make up the dns message.
    156      1.1.1.2  christos  * @param region: where to allocate dns message.
    157      1.1.1.2  christos  * @param now: the time now, for check if TTL on cache entry is ok.
    158      1.1.1.5  christos  * @param allow_expired: if true and serve-expired is enabled, it will allow
    159      1.1.1.5  christos  *	for expired dns_msg to be generated based on the configured serve-expired
    160      1.1.1.5  christos  *	logic.
    161      1.1.1.2  christos  * @param scratch: where to allocate temporary data.
    162      1.1.1.2  christos  * */
    163      1.1.1.2  christos struct dns_msg* tomsg(struct module_env* env, struct query_info* q,
    164      1.1.1.2  christos 	struct reply_info* r, struct regional* region, time_t now,
    165      1.1.1.5  christos 	int allow_expired, struct regional* scratch);
    166      1.1.1.2  christos 
    167  1.1.1.6.2.1    martin /**
    168  1.1.1.6.2.1    martin  * Deep copy a dns_msg to a region.
    169  1.1.1.6.2.1    martin  * @param origin: the dns_msg to copy.
    170  1.1.1.6.2.1    martin  * @param region: the region to copy all the data to.
    171  1.1.1.6.2.1    martin  * @return the new dns_msg or NULL on malloc error.
    172  1.1.1.6.2.1    martin  */
    173  1.1.1.6.2.1    martin struct dns_msg* dns_msg_deepcopy_region(struct dns_msg* origin,
    174  1.1.1.6.2.1    martin 	struct regional* region);
    175  1.1.1.6.2.1    martin 
    176          1.1  christos /**
    177          1.1  christos  * Find cached message
    178          1.1  christos  * @param env: module environment with the DNS cache.
    179          1.1  christos  * @param qname: query name.
    180          1.1  christos  * @param qnamelen: length of qname.
    181          1.1  christos  * @param qtype: query type.
    182          1.1  christos  * @param qclass: query class.
    183          1.1  christos  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
    184          1.1  christos  * @param region: where to allocate result.
    185          1.1  christos  * @param scratch: where to allocate temporary data.
    186      1.1.1.3  christos  * @param no_partial: if true, only complete messages and not a partial
    187      1.1.1.5  christos  *	one (with only the start of the CNAME chain and not the rest).
    188      1.1.1.6  christos  * @param dpname: if not NULL, do not return NXDOMAIN above this name.
    189      1.1.1.6  christos  * @param dpnamelen: length of dpname.
    190          1.1  christos  * @return new response message (alloced in region, rrsets do not have IDs).
    191          1.1  christos  * 	or NULL on error or if not found in cache.
    192          1.1  christos  *	TTLs are made relative to the current time.
    193          1.1  christos  */
    194          1.1  christos struct dns_msg* dns_cache_lookup(struct module_env* env,
    195          1.1  christos 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
    196      1.1.1.3  christos 	uint16_t flags, struct regional* region, struct regional* scratch,
    197      1.1.1.6  christos 	int no_partial, uint8_t* dpname, size_t dpnamelen);
    198          1.1  christos 
    199          1.1  christos /**
    200          1.1  christos  * find and add A and AAAA records for missing nameservers in delegpt
    201          1.1  christos  * @param env: module environment with rrset cache
    202          1.1  christos  * @param qclass: which class to look in.
    203          1.1  christos  * @param region: where to store new dp info.
    204          1.1  christos  * @param dp: delegation point to fill missing entries.
    205          1.1  christos  * @return false on alloc failure.
    206          1.1  christos  */
    207          1.1  christos int cache_fill_missing(struct module_env* env, uint16_t qclass,
    208          1.1  christos 	struct regional* region, struct delegpt* dp);
    209          1.1  christos 
    210          1.1  christos /**
    211          1.1  christos  * Utility, create new, unpacked data structure for cache response.
    212          1.1  christos  * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
    213          1.1  christos  * @param qname: query section name
    214          1.1  christos  * @param qnamelen: len of qname
    215          1.1  christos  * @param qtype: query section type
    216          1.1  christos  * @param qclass: query section class
    217          1.1  christos  * @param region: where to alloc.
    218          1.1  christos  * @param capacity: number of rrsets space to create in the array.
    219          1.1  christos  * @return new dns_msg struct or NULL on mem fail.
    220          1.1  christos  */
    221          1.1  christos struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
    222          1.1  christos 	uint16_t qclass, struct regional* region, size_t capacity);
    223          1.1  christos 
    224          1.1  christos /**
    225          1.1  christos  * Add rrset to authority section in unpacked dns_msg message. Must have enough
    226          1.1  christos  * space left, does not grow the array.
    227          1.1  christos  * @param msg: msg to put it in.
    228          1.1  christos  * @param region: region to alloc in
    229          1.1  christos  * @param rrset: to add in authority section
    230          1.1  christos  * @param now: now.
    231          1.1  christos  * @return true if worked, false on fail
    232          1.1  christos  */
    233          1.1  christos int dns_msg_authadd(struct dns_msg* msg, struct regional* region,
    234          1.1  christos 	struct ub_packed_rrset_key* rrset, time_t now);
    235          1.1  christos 
    236          1.1  christos /**
    237      1.1.1.3  christos  * Add rrset to authority section in unpacked dns_msg message. Must have enough
    238      1.1.1.3  christos  * space left, does not grow the array.
    239      1.1.1.3  christos  * @param msg: msg to put it in.
    240      1.1.1.3  christos  * @param region: region to alloc in
    241      1.1.1.3  christos  * @param rrset: to add in authority section
    242      1.1.1.3  christos  * @param now: now.
    243      1.1.1.3  christos  * @return true if worked, false on fail
    244      1.1.1.3  christos  */
    245      1.1.1.3  christos int dns_msg_ansadd(struct dns_msg* msg, struct regional* region,
    246      1.1.1.3  christos 	struct ub_packed_rrset_key* rrset, time_t now);
    247      1.1.1.3  christos 
    248      1.1.1.3  christos /**
    249          1.1  christos  * Adjust the prefetch_ttl for a cached message.  This adds a value to the
    250          1.1  christos  * prefetch ttl - postponing the time when it will be prefetched for future
    251          1.1  christos  * incoming queries.
    252          1.1  christos  * @param env: module environment with caches and time.
    253          1.1  christos  * @param qinfo: query info for the query that needs adjustment.
    254          1.1  christos  * @param adjust: time in seconds to add to the prefetch_leeway.
    255          1.1  christos  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
    256          1.1  christos  * @return false if not in cache. true if added.
    257          1.1  christos  */
    258          1.1  christos int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
    259          1.1  christos         time_t adjust, uint16_t flags);
    260          1.1  christos 
    261      1.1.1.2  christos /** lookup message in message cache
    262      1.1.1.2  christos  * the returned nonNULL entry is locked and has to be unlocked by the caller */
    263      1.1.1.2  christos struct msgreply_entry* msg_cache_lookup(struct module_env* env,
    264      1.1.1.2  christos 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
    265      1.1.1.2  christos 	uint16_t flags, time_t now, int wr);
    266      1.1.1.2  christos 
    267      1.1.1.4  christos /**
    268      1.1.1.4  christos  * Remove entry from the message cache.  For unwanted entries.
    269      1.1.1.4  christos  * @param env: with message cache.
    270      1.1.1.4  christos  * @param qname: query name, in wireformat
    271      1.1.1.4  christos  * @param qnamelen: length of qname, including terminating 0.
    272      1.1.1.4  christos  * @param qtype: query type, host order.
    273      1.1.1.4  christos  * @param qclass: query class, host order.
    274      1.1.1.4  christos  * @param flags: flags
    275      1.1.1.4  christos  */
    276      1.1.1.4  christos void msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen,
    277      1.1.1.4  christos 	uint16_t qtype, uint16_t qclass, uint16_t flags);
    278      1.1.1.4  christos 
    279          1.1  christos #endif /* SERVICES_CACHE_DNS_H */
    280