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).
82238106Sdes * @return 0 on alloc error (out of memory).
83238106Sdes */
84238106Sdesint dns_cache_store(struct module_env* env, struct query_info* qinf,
85269257Sdes        struct reply_info* rep, int is_referral, time_t leeway, int pside,
86238106Sdes	struct regional* region);
87238106Sdes
88238106Sdes/**
89238106Sdes * Store message in the cache. Stores in message cache and rrset cache.
90238106Sdes * Both qinfo and rep should be malloced and are put in the cache.
91238106Sdes * They should not be used after this call, as they are then in shared cache.
92238106Sdes * Does not return errors, they are logged and only lead to less cache.
93238106Sdes *
94238106Sdes * @param env: module environment with the DNS cache.
95238106Sdes * @param qinfo: query info
96238106Sdes * @param hash: hash over qinfo.
97238106Sdes * @param rep: reply info, together with qinfo makes up the message.
98238106Sdes *	Adjusts the reply info TTLs to absolute time.
99238106Sdes * @param leeway: TTL value, if not 0, other rrsets are considered expired
100238106Sdes *	that many seconds before actual TTL expiry.
101238106Sdes * @param pside: if true, information came from a server which was fetched
102238106Sdes * 	from the parentside of the zonecut.  This means that the type NS
103238106Sdes * 	can be updated to full TTL even in prefetch situations.
104238106Sdes * @param qrep: message that can be altered with better rrs from cache.
105238106Sdes * @param region: to allocate into for qmsg.
106238106Sdes */
107238106Sdesvoid dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
108269257Sdes	hashvalue_t hash, struct reply_info* rep, time_t leeway, int pside,
109238106Sdes	struct reply_info* qrep, struct regional* region);
110238106Sdes
111238106Sdes/**
112238106Sdes * Find a delegation from the cache.
113238106Sdes * @param env: module environment with the DNS cache.
114238106Sdes * @param qname: query name.
115238106Sdes * @param qnamelen: length of qname.
116238106Sdes * @param qtype: query type.
117238106Sdes * @param qclass: query class.
118238106Sdes * @param region: where to allocate result delegation.
119238106Sdes * @param msg: if not NULL, delegation message is returned here, synthesized
120238106Sdes *	from the cache.
121238106Sdes * @param timenow: the time now, for checking if TTL on cache entries is OK.
122238106Sdes * @return new delegation or NULL on error or if not found in cache.
123238106Sdes */
124238106Sdesstruct delegpt* dns_cache_find_delegation(struct module_env* env,
125238106Sdes	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
126269257Sdes	struct regional* region, struct dns_msg** msg, time_t timenow);
127238106Sdes
128238106Sdes/**
129238106Sdes * Find cached message
130238106Sdes * @param env: module environment with the DNS cache.
131238106Sdes * @param qname: query name.
132238106Sdes * @param qnamelen: length of qname.
133238106Sdes * @param qtype: query type.
134238106Sdes * @param qclass: query class.
135238106Sdes * @param region: where to allocate result.
136238106Sdes * @param scratch: where to allocate temporary data.
137238106Sdes * @return new response message (alloced in region, rrsets do not have IDs).
138238106Sdes * 	or NULL on error or if not found in cache.
139238106Sdes *	TTLs are made relative to the current time.
140238106Sdes */
141238106Sdesstruct dns_msg* dns_cache_lookup(struct module_env* env,
142238106Sdes	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
143238106Sdes	struct regional* region, struct regional* scratch);
144238106Sdes
145238106Sdes/**
146238106Sdes * find and add A and AAAA records for missing nameservers in delegpt
147238106Sdes * @param env: module environment with rrset cache
148238106Sdes * @param qclass: which class to look in.
149238106Sdes * @param region: where to store new dp info.
150238106Sdes * @param dp: delegation point to fill missing entries.
151238106Sdes * @return false on alloc failure.
152238106Sdes */
153238106Sdesint cache_fill_missing(struct module_env* env, uint16_t qclass,
154238106Sdes	struct regional* region, struct delegpt* dp);
155238106Sdes
156238106Sdes/**
157238106Sdes * Utility, create new, unpacked data structure for cache response.
158238106Sdes * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
159238106Sdes * @param qname: query section name
160238106Sdes * @param qnamelen: len of qname
161238106Sdes * @param qtype: query section type
162238106Sdes * @param qclass: query section class
163238106Sdes * @param region: where to alloc.
164238106Sdes * @param capacity: number of rrsets space to create in the array.
165238106Sdes * @return new dns_msg struct or NULL on mem fail.
166238106Sdes */
167238106Sdesstruct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
168238106Sdes	uint16_t qclass, struct regional* region, size_t capacity);
169238106Sdes
170238106Sdes/**
171238106Sdes * Add rrset to authority section in unpacked dns_msg message. Must have enough
172238106Sdes * space left, does not grow the array.
173238106Sdes * @param msg: msg to put it in.
174238106Sdes * @param region: region to alloc in
175238106Sdes * @param rrset: to add in authority section
176238106Sdes * @param now: now.
177238106Sdes * @return true if worked, false on fail
178238106Sdes */
179238106Sdesint dns_msg_authadd(struct dns_msg* msg, struct regional* region,
180269257Sdes	struct ub_packed_rrset_key* rrset, time_t now);
181238106Sdes
182238106Sdes#endif /* SERVICES_CACHE_DNS_H */
183