xref: /freebsd/contrib/unbound/cachedb/cachedb.c (revision 46d2f61818f594174cafe31ee338c6e083fa1876)
1e2d15004SDag-Erling Smørgrav /*
2e2d15004SDag-Erling Smørgrav  * cachedb/cachedb.c - cache from a database external to the program module
3e2d15004SDag-Erling Smørgrav  *
4e2d15004SDag-Erling Smørgrav  * Copyright (c) 2016, NLnet Labs. All rights reserved.
5e2d15004SDag-Erling Smørgrav  *
6e2d15004SDag-Erling Smørgrav  * This software is open source.
7e2d15004SDag-Erling Smørgrav  *
8e2d15004SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9e2d15004SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10e2d15004SDag-Erling Smørgrav  * are met:
11e2d15004SDag-Erling Smørgrav  *
12e2d15004SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13e2d15004SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14e2d15004SDag-Erling Smørgrav  *
15e2d15004SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16e2d15004SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17e2d15004SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18e2d15004SDag-Erling Smørgrav  *
19e2d15004SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20e2d15004SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21e2d15004SDag-Erling Smørgrav  * specific prior written permission.
22e2d15004SDag-Erling Smørgrav  *
23e2d15004SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24e2d15004SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25e2d15004SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26e2d15004SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27e2d15004SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28e2d15004SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29e2d15004SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30e2d15004SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31e2d15004SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32e2d15004SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33e2d15004SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34e2d15004SDag-Erling Smørgrav  */
35e2d15004SDag-Erling Smørgrav 
36e2d15004SDag-Erling Smørgrav /**
37e2d15004SDag-Erling Smørgrav  * \file
38e2d15004SDag-Erling Smørgrav  *
39e2d15004SDag-Erling Smørgrav  * This file contains a module that uses an external database to cache
40e2d15004SDag-Erling Smørgrav  * dns responses.
41e2d15004SDag-Erling Smørgrav  */
42e2d15004SDag-Erling Smørgrav 
43e2d15004SDag-Erling Smørgrav #include "config.h"
44e2d15004SDag-Erling Smørgrav #ifdef USE_CACHEDB
45e2d15004SDag-Erling Smørgrav #include "cachedb/cachedb.h"
460fb34990SDag-Erling Smørgrav #include "cachedb/redis.h"
47e2d15004SDag-Erling Smørgrav #include "util/regional.h"
48e2d15004SDag-Erling Smørgrav #include "util/net_help.h"
49e2d15004SDag-Erling Smørgrav #include "util/config_file.h"
50e2d15004SDag-Erling Smørgrav #include "util/data/msgreply.h"
51e2d15004SDag-Erling Smørgrav #include "util/data/msgencode.h"
52e2d15004SDag-Erling Smørgrav #include "services/cache/dns.h"
53335c7cdaSCy Schubert #include "services/mesh.h"
54335c7cdaSCy Schubert #include "services/modstack.h"
55e2d15004SDag-Erling Smørgrav #include "validator/val_neg.h"
56e2d15004SDag-Erling Smørgrav #include "validator/val_secalgo.h"
57e2d15004SDag-Erling Smørgrav #include "iterator/iter_utils.h"
58e2d15004SDag-Erling Smørgrav #include "sldns/parseutil.h"
59e2d15004SDag-Erling Smørgrav #include "sldns/wire2str.h"
60e2d15004SDag-Erling Smørgrav #include "sldns/sbuffer.h"
61e2d15004SDag-Erling Smørgrav 
620fb34990SDag-Erling Smørgrav /* header file for htobe64 */
630fb34990SDag-Erling Smørgrav #ifdef HAVE_ENDIAN_H
640fb34990SDag-Erling Smørgrav #  include <endian.h>
650fb34990SDag-Erling Smørgrav #endif
660fb34990SDag-Erling Smørgrav #ifdef HAVE_SYS_ENDIAN_H
670fb34990SDag-Erling Smørgrav #  include <sys/endian.h>
680fb34990SDag-Erling Smørgrav #endif
69091e9e46SCy Schubert 
70091e9e46SCy Schubert #ifndef HAVE_HTOBE64
710fb34990SDag-Erling Smørgrav #  ifdef HAVE_LIBKERN_OSBYTEORDER_H
720fb34990SDag-Erling Smørgrav      /* In practice this is specific to MacOS X.  We assume it doesn't have
730fb34990SDag-Erling Smørgrav       * htobe64/be64toh but has alternatives with a different name. */
740fb34990SDag-Erling Smørgrav #    include <libkern/OSByteOrder.h>
750fb34990SDag-Erling Smørgrav #    define htobe64(x) OSSwapHostToBigInt64(x)
760fb34990SDag-Erling Smørgrav #    define be64toh(x) OSSwapBigToHostInt64(x)
77091e9e46SCy Schubert #  else
78091e9e46SCy Schubert      /* not OSX */
79e86b9096SDag-Erling Smørgrav      /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */
80e86b9096SDag-Erling Smørgrav #    if __BIG_ENDIAN__
81e86b9096SDag-Erling Smørgrav #      define be64toh(n) (n)
82e86b9096SDag-Erling Smørgrav #      define htobe64(n) (n)
83e86b9096SDag-Erling Smørgrav #    else
84e86b9096SDag-Erling Smørgrav #      define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
85e86b9096SDag-Erling Smørgrav #      define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
86091e9e46SCy Schubert #    endif /* _ENDIAN */
87091e9e46SCy Schubert #  endif /* HAVE_LIBKERN_OSBYTEORDER_H */
88091e9e46SCy Schubert #endif /* HAVE_BE64TOH */
89e86b9096SDag-Erling Smørgrav 
90e2d15004SDag-Erling Smørgrav /** the unit test testframe for cachedb, its module state contains
91e2d15004SDag-Erling Smørgrav  * a cache for a couple queries (in memory). */
92e2d15004SDag-Erling Smørgrav struct testframe_moddata {
93971980c3SDag-Erling Smørgrav 	/** lock for mutex */
94971980c3SDag-Erling Smørgrav 	lock_basic_type lock;
95e2d15004SDag-Erling Smørgrav 	/** key for single stored data element, NULL if none */
96e2d15004SDag-Erling Smørgrav 	char* stored_key;
97e2d15004SDag-Erling Smørgrav 	/** data for single stored data element, NULL if none */
98e2d15004SDag-Erling Smørgrav 	uint8_t* stored_data;
99e2d15004SDag-Erling Smørgrav 	/** length of stored data */
100e2d15004SDag-Erling Smørgrav 	size_t stored_datalen;
101e2d15004SDag-Erling Smørgrav };
102e2d15004SDag-Erling Smørgrav 
103e2d15004SDag-Erling Smørgrav static int
testframe_init(struct module_env * env,struct cachedb_env * cachedb_env)104e2d15004SDag-Erling Smørgrav testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
105e2d15004SDag-Erling Smørgrav {
106971980c3SDag-Erling Smørgrav 	struct testframe_moddata* d;
107e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_init");
108971980c3SDag-Erling Smørgrav 	d = (struct testframe_moddata*)calloc(1,
109e2d15004SDag-Erling Smørgrav 		sizeof(struct testframe_moddata));
110971980c3SDag-Erling Smørgrav 	cachedb_env->backend_data = (void*)d;
111e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend_data) {
112e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
113e2d15004SDag-Erling Smørgrav 		return 0;
114e2d15004SDag-Erling Smørgrav 	}
1158f76bb7dSCy Schubert 	/* Register an EDNS option (65534) to bypass the worker cache lookup
1168f76bb7dSCy Schubert 	 * for testing */
1178f76bb7dSCy Schubert 	if(!edns_register_option(LDNS_EDNS_UNBOUND_CACHEDB_TESTFRAME_TEST,
1188f76bb7dSCy Schubert 		1 /* bypass cache */,
1198f76bb7dSCy Schubert 		0 /* no aggregation */, env)) {
1208f76bb7dSCy Schubert 		log_err("testframe_init, could not register test opcode");
1218f76bb7dSCy Schubert 		free(d);
1228f76bb7dSCy Schubert 		return 0;
1238f76bb7dSCy Schubert 	}
124971980c3SDag-Erling Smørgrav 	lock_basic_init(&d->lock);
125971980c3SDag-Erling Smørgrav 	lock_protect(&d->lock, d, sizeof(*d));
126e2d15004SDag-Erling Smørgrav 	return 1;
127e2d15004SDag-Erling Smørgrav }
128e2d15004SDag-Erling Smørgrav 
129e2d15004SDag-Erling Smørgrav static void
testframe_deinit(struct module_env * env,struct cachedb_env * cachedb_env)130e2d15004SDag-Erling Smørgrav testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
131e2d15004SDag-Erling Smørgrav {
132e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
133e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
134e2d15004SDag-Erling Smørgrav 	(void)env;
135e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_deinit");
136e2d15004SDag-Erling Smørgrav 	if(!d)
137e2d15004SDag-Erling Smørgrav 		return;
138971980c3SDag-Erling Smørgrav 	lock_basic_destroy(&d->lock);
139e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
140e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
141e2d15004SDag-Erling Smørgrav 	free(d);
142e2d15004SDag-Erling Smørgrav }
143e2d15004SDag-Erling Smørgrav 
144e2d15004SDag-Erling Smørgrav static int
testframe_lookup(struct module_env * env,struct cachedb_env * cachedb_env,char * key,struct sldns_buffer * result_buffer)145e2d15004SDag-Erling Smørgrav testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
146e2d15004SDag-Erling Smørgrav 	char* key, struct sldns_buffer* result_buffer)
147e2d15004SDag-Erling Smørgrav {
148e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
149e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
150e2d15004SDag-Erling Smørgrav 	(void)env;
151e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
152971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
153e2d15004SDag-Erling Smørgrav 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
154971980c3SDag-Erling Smørgrav 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
155971980c3SDag-Erling Smørgrav 			lock_basic_unlock(&d->lock);
156e2d15004SDag-Erling Smørgrav 			return 0; /* too large */
157971980c3SDag-Erling Smørgrav 		}
158e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
159e2d15004SDag-Erling Smørgrav 			(int)d->stored_datalen);
160e2d15004SDag-Erling Smørgrav 		sldns_buffer_clear(result_buffer);
161e2d15004SDag-Erling Smørgrav 		sldns_buffer_write(result_buffer, d->stored_data,
162e2d15004SDag-Erling Smørgrav 			d->stored_datalen);
163e2d15004SDag-Erling Smørgrav 		sldns_buffer_flip(result_buffer);
164971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
165e2d15004SDag-Erling Smørgrav 		return 1;
166e2d15004SDag-Erling Smørgrav 	}
167971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
168e2d15004SDag-Erling Smørgrav 	return 0;
169e2d15004SDag-Erling Smørgrav }
170e2d15004SDag-Erling Smørgrav 
171e2d15004SDag-Erling Smørgrav static void
testframe_store(struct module_env * env,struct cachedb_env * cachedb_env,char * key,uint8_t * data,size_t data_len,time_t ATTR_UNUSED (ttl))172e2d15004SDag-Erling Smørgrav testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
17325039b37SCy Schubert 	char* key, uint8_t* data, size_t data_len, time_t ATTR_UNUSED(ttl))
174e2d15004SDag-Erling Smørgrav {
175e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
176e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
177e2d15004SDag-Erling Smørgrav 	(void)env;
178971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
179e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
180e2d15004SDag-Erling Smørgrav 
181e2d15004SDag-Erling Smørgrav 	/* free old data element (if any) */
182e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
183e2d15004SDag-Erling Smørgrav 	d->stored_key = NULL;
184e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
185e2d15004SDag-Erling Smørgrav 	d->stored_data = NULL;
186e2d15004SDag-Erling Smørgrav 	d->stored_datalen = 0;
187e2d15004SDag-Erling Smørgrav 
188e2d15004SDag-Erling Smørgrav 	d->stored_data = memdup(data, data_len);
189e2d15004SDag-Erling Smørgrav 	if(!d->stored_data) {
190971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
191e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
192e2d15004SDag-Erling Smørgrav 		return;
193e2d15004SDag-Erling Smørgrav 	}
194e2d15004SDag-Erling Smørgrav 	d->stored_datalen = data_len;
195e2d15004SDag-Erling Smørgrav 	d->stored_key = strdup(key);
196e2d15004SDag-Erling Smørgrav 	if(!d->stored_key) {
197e2d15004SDag-Erling Smørgrav 		free(d->stored_data);
198e2d15004SDag-Erling Smørgrav 		d->stored_data = NULL;
199e2d15004SDag-Erling Smørgrav 		d->stored_datalen = 0;
200971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
201e2d15004SDag-Erling Smørgrav 		return;
202e2d15004SDag-Erling Smørgrav 	}
203971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
204e2d15004SDag-Erling Smørgrav 	/* (key,data) successfully stored */
205e2d15004SDag-Erling Smørgrav }
206e2d15004SDag-Erling Smørgrav 
207e2d15004SDag-Erling Smørgrav /** The testframe backend is for unit tests */
208e2d15004SDag-Erling Smørgrav static struct cachedb_backend testframe_backend = { "testframe",
209e2d15004SDag-Erling Smørgrav 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
210e2d15004SDag-Erling Smørgrav };
211e2d15004SDag-Erling Smørgrav 
212e2d15004SDag-Erling Smørgrav /** find a particular backend from possible backends */
213e2d15004SDag-Erling Smørgrav static struct cachedb_backend*
cachedb_find_backend(const char * str)214e2d15004SDag-Erling Smørgrav cachedb_find_backend(const char* str)
215e2d15004SDag-Erling Smørgrav {
2160fb34990SDag-Erling Smørgrav #ifdef USE_REDIS
2170fb34990SDag-Erling Smørgrav 	if(strcmp(str, redis_backend.name) == 0)
2180fb34990SDag-Erling Smørgrav 		return &redis_backend;
2190fb34990SDag-Erling Smørgrav #endif
220e2d15004SDag-Erling Smørgrav 	if(strcmp(str, testframe_backend.name) == 0)
221e2d15004SDag-Erling Smørgrav 		return &testframe_backend;
222e2d15004SDag-Erling Smørgrav 	/* TODO add more backends here */
223e2d15004SDag-Erling Smørgrav 	return NULL;
224e2d15004SDag-Erling Smørgrav }
225e2d15004SDag-Erling Smørgrav 
226e2d15004SDag-Erling Smørgrav /** apply configuration to cachedb module 'global' state */
227e2d15004SDag-Erling Smørgrav static int
cachedb_apply_cfg(struct cachedb_env * cachedb_env,struct config_file * cfg)228e2d15004SDag-Erling Smørgrav cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
229e2d15004SDag-Erling Smørgrav {
230971980c3SDag-Erling Smørgrav 	const char* backend_str = cfg->cachedb_backend;
2318f76bb7dSCy Schubert 	if(!backend_str || *backend_str==0)
2328f76bb7dSCy Schubert 		return 1;
233e2d15004SDag-Erling Smørgrav 	cachedb_env->backend = cachedb_find_backend(backend_str);
234e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend) {
235971980c3SDag-Erling Smørgrav 		log_err("cachedb: cannot find backend name '%s'", backend_str);
236c7f4d7adSDag-Erling Smørgrav 		return 0;
237e2d15004SDag-Erling Smørgrav 	}
238971980c3SDag-Erling Smørgrav 
239e2d15004SDag-Erling Smørgrav 	/* TODO see if more configuration needs to be applied or not */
240e2d15004SDag-Erling Smørgrav 	return 1;
241e2d15004SDag-Erling Smørgrav }
242e2d15004SDag-Erling Smørgrav 
243e2d15004SDag-Erling Smørgrav int
cachedb_init(struct module_env * env,int id)244e2d15004SDag-Erling Smørgrav cachedb_init(struct module_env* env, int id)
245e2d15004SDag-Erling Smørgrav {
246e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
247e2d15004SDag-Erling Smørgrav 		sizeof(struct cachedb_env));
248e2d15004SDag-Erling Smørgrav 	if(!cachedb_env) {
249e2d15004SDag-Erling Smørgrav 		log_err("malloc failure");
250e2d15004SDag-Erling Smørgrav 		return 0;
251e2d15004SDag-Erling Smørgrav 	}
252e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = (void*)cachedb_env;
253e2d15004SDag-Erling Smørgrav 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
254e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not apply configuration settings.");
2550eefd307SCy Schubert 		free(cachedb_env);
2560eefd307SCy Schubert 		env->modinfo[id] = NULL;
257e2d15004SDag-Erling Smørgrav 		return 0;
258e2d15004SDag-Erling Smørgrav 	}
259e2d15004SDag-Erling Smørgrav 	/* see if a backend is selected */
260e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend || !cachedb_env->backend->name)
261e2d15004SDag-Erling Smørgrav 		return 1;
262e2d15004SDag-Erling Smørgrav 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
263e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not init %s backend",
264e2d15004SDag-Erling Smørgrav 			cachedb_env->backend->name);
2650eefd307SCy Schubert 		free(cachedb_env);
2660eefd307SCy Schubert 		env->modinfo[id] = NULL;
267e2d15004SDag-Erling Smørgrav 		return 0;
268e2d15004SDag-Erling Smørgrav 	}
269e2d15004SDag-Erling Smørgrav 	cachedb_env->enabled = 1;
270e2d15004SDag-Erling Smørgrav 	return 1;
271e2d15004SDag-Erling Smørgrav }
272e2d15004SDag-Erling Smørgrav 
273e2d15004SDag-Erling Smørgrav void
cachedb_deinit(struct module_env * env,int id)274e2d15004SDag-Erling Smørgrav cachedb_deinit(struct module_env* env, int id)
275e2d15004SDag-Erling Smørgrav {
276e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env;
277e2d15004SDag-Erling Smørgrav 	if(!env || !env->modinfo[id])
278e2d15004SDag-Erling Smørgrav 		return;
279e2d15004SDag-Erling Smørgrav 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
280e2d15004SDag-Erling Smørgrav 	if(cachedb_env->enabled) {
281e2d15004SDag-Erling Smørgrav 		(*cachedb_env->backend->deinit)(env, cachedb_env);
282e2d15004SDag-Erling Smørgrav 	}
283e2d15004SDag-Erling Smørgrav 	free(cachedb_env);
284e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = NULL;
285e2d15004SDag-Erling Smørgrav }
286e2d15004SDag-Erling Smørgrav 
287e2d15004SDag-Erling Smørgrav /** new query for cachedb */
288e2d15004SDag-Erling Smørgrav static int
cachedb_new(struct module_qstate * qstate,int id)289e2d15004SDag-Erling Smørgrav cachedb_new(struct module_qstate* qstate, int id)
290e2d15004SDag-Erling Smørgrav {
291e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
292e2d15004SDag-Erling Smørgrav 		qstate->region, sizeof(struct cachedb_qstate));
293e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = iq;
294e2d15004SDag-Erling Smørgrav 	if(!iq)
295e2d15004SDag-Erling Smørgrav 		return 0;
296e2d15004SDag-Erling Smørgrav 	memset(iq, 0, sizeof(*iq));
297e2d15004SDag-Erling Smørgrav 	/* initialise it */
298e2d15004SDag-Erling Smørgrav 	/* TODO */
299e2d15004SDag-Erling Smørgrav 
300e2d15004SDag-Erling Smørgrav 	return 1;
301e2d15004SDag-Erling Smørgrav }
302e2d15004SDag-Erling Smørgrav 
303e2d15004SDag-Erling Smørgrav /**
304e2d15004SDag-Erling Smørgrav  * Return an error
305e2d15004SDag-Erling Smørgrav  * @param qstate: our query state
306e2d15004SDag-Erling Smørgrav  * @param id: module id
307e2d15004SDag-Erling Smørgrav  * @param rcode: error code (DNS errcode).
308e2d15004SDag-Erling Smørgrav  * @return: 0 for use by caller, to make notation easy, like:
309e2d15004SDag-Erling Smørgrav  * 	return error_response(..).
310e2d15004SDag-Erling Smørgrav  */
311e2d15004SDag-Erling Smørgrav static int
error_response(struct module_qstate * qstate,int id,int rcode)312e2d15004SDag-Erling Smørgrav error_response(struct module_qstate* qstate, int id, int rcode)
313e2d15004SDag-Erling Smørgrav {
314e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "return error response %s",
315e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)?
316e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
317e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = rcode;
318e2d15004SDag-Erling Smørgrav 	qstate->return_msg = NULL;
319e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
320e2d15004SDag-Erling Smørgrav 	return 0;
321e2d15004SDag-Erling Smørgrav }
322e2d15004SDag-Erling Smørgrav 
323e2d15004SDag-Erling Smørgrav /**
324e2d15004SDag-Erling Smørgrav  * Hash the query name, type, class and dbacess-secret into lookup buffer.
32556850988SCy Schubert  * @param qinfo: query info
32656850988SCy Schubert  * @param env: with env->cfg with secret.
327e2d15004SDag-Erling Smørgrav  * @param buf: returned buffer with hash to lookup
328e2d15004SDag-Erling Smørgrav  * @param len: length of the buffer.
329e2d15004SDag-Erling Smørgrav  */
330e2d15004SDag-Erling Smørgrav static void
calc_hash(struct query_info * qinfo,struct module_env * env,char * buf,size_t len)33156850988SCy Schubert calc_hash(struct query_info* qinfo, struct module_env* env, char* buf,
33256850988SCy Schubert 	size_t len)
333e2d15004SDag-Erling Smørgrav {
334e2d15004SDag-Erling Smørgrav 	uint8_t clear[1024];
335e2d15004SDag-Erling Smørgrav 	size_t clen = 0;
336e2d15004SDag-Erling Smørgrav 	uint8_t hash[CACHEDB_HASHSIZE/8];
337e2d15004SDag-Erling Smørgrav 	const char* hex = "0123456789ABCDEF";
33856850988SCy Schubert 	const char* secret = env->cfg->cachedb_secret;
339e2d15004SDag-Erling Smørgrav 	size_t i;
340e2d15004SDag-Erling Smørgrav 
341e2d15004SDag-Erling Smørgrav 	/* copy the hash info into the clear buffer */
34256850988SCy Schubert 	if(clen + qinfo->qname_len < sizeof(clear)) {
34356850988SCy Schubert 		memmove(clear+clen, qinfo->qname, qinfo->qname_len);
34456850988SCy Schubert 		clen += qinfo->qname_len;
345e2d15004SDag-Erling Smørgrav 	}
346e2d15004SDag-Erling Smørgrav 	if(clen + 4 < sizeof(clear)) {
34756850988SCy Schubert 		uint16_t t = htons(qinfo->qtype);
34856850988SCy Schubert 		uint16_t c = htons(qinfo->qclass);
349e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, &t, 2);
350e2d15004SDag-Erling Smørgrav 		memmove(clear+clen+2, &c, 2);
351e2d15004SDag-Erling Smørgrav 		clen += 4;
352e2d15004SDag-Erling Smørgrav 	}
353e2d15004SDag-Erling Smørgrav 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
354e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, secret, strlen(secret));
355e2d15004SDag-Erling Smørgrav 		clen += strlen(secret);
356e2d15004SDag-Erling Smørgrav 	}
357e2d15004SDag-Erling Smørgrav 
358e2d15004SDag-Erling Smørgrav 	/* hash the buffer */
359e2d15004SDag-Erling Smørgrav 	secalgo_hash_sha256(clear, clen, hash);
3600eefd307SCy Schubert #ifdef HAVE_EXPLICIT_BZERO
3610eefd307SCy Schubert 	explicit_bzero(clear, clen);
3620eefd307SCy Schubert #else
363e2d15004SDag-Erling Smørgrav 	memset(clear, 0, clen);
3640eefd307SCy Schubert #endif
365e2d15004SDag-Erling Smørgrav 
366e2d15004SDag-Erling Smørgrav 	/* hex encode output for portability (some online dbs need
367e2d15004SDag-Erling Smørgrav 	 * no nulls, no control characters, and so on) */
368e2d15004SDag-Erling Smørgrav 	log_assert(len >= sizeof(hash)*2 + 1);
369e2d15004SDag-Erling Smørgrav 	(void)len;
370e2d15004SDag-Erling Smørgrav 	for(i=0; i<sizeof(hash); i++) {
371e2d15004SDag-Erling Smørgrav 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
372e2d15004SDag-Erling Smørgrav 		buf[i*2+1] = hex[hash[i]&0x0f];
373e2d15004SDag-Erling Smørgrav 	}
374e2d15004SDag-Erling Smørgrav 	buf[sizeof(hash)*2] = 0;
375e2d15004SDag-Erling Smørgrav }
376e2d15004SDag-Erling Smørgrav 
377e2d15004SDag-Erling Smørgrav /** convert data from return_msg into the data buffer */
378e2d15004SDag-Erling Smørgrav static int
prep_data(struct module_qstate * qstate,struct sldns_buffer * buf)379e2d15004SDag-Erling Smørgrav prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
380e2d15004SDag-Erling Smørgrav {
381e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
382e2d15004SDag-Erling Smørgrav 	size_t oldlim;
383e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
384e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
385e2d15004SDag-Erling Smørgrav 	edns.edns_present = 1;
386e2d15004SDag-Erling Smørgrav 	edns.bits = EDNS_DO;
387e2d15004SDag-Erling Smørgrav 	edns.ext_rcode = 0;
388e2d15004SDag-Erling Smørgrav 	edns.edns_version = EDNS_ADVERTISED_VERSION;
389e2d15004SDag-Erling Smørgrav 	edns.udp_size = EDNS_ADVERTISED_SIZE;
390e2d15004SDag-Erling Smørgrav 
391e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg || !qstate->return_msg->rep)
392e2d15004SDag-Erling Smørgrav 		return 0;
3931838dec3SCy Schubert 	/* do not store failures like SERVFAIL in the cachedb, this avoids
3941838dec3SCy Schubert 	 * overwriting expired, valid, content with broken content. */
3951838dec3SCy Schubert 	if(FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
3961838dec3SCy Schubert 		LDNS_RCODE_NOERROR &&
3971838dec3SCy Schubert 	   FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
3981838dec3SCy Schubert 		LDNS_RCODE_NXDOMAIN &&
3991838dec3SCy Schubert 	   FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
4001838dec3SCy Schubert 		LDNS_RCODE_YXDOMAIN)
4011838dec3SCy Schubert 		return 0;
4028a384985SDag-Erling Smørgrav 	/* We don't store the reply if its TTL is 0 unless serve-expired is
4038a384985SDag-Erling Smørgrav 	 * enabled.  Such a reply won't be reusable and simply be a waste for
4048a384985SDag-Erling Smørgrav 	 * the backend.  It's also compatible with the default behavior of
4058a384985SDag-Erling Smørgrav 	 * dns_cache_store_msg(). */
4068a384985SDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl == 0 &&
4078a384985SDag-Erling Smørgrav 		!qstate->env->cfg->serve_expired)
4088a384985SDag-Erling Smørgrav 		return 0;
4098f76bb7dSCy Schubert 
4108f76bb7dSCy Schubert 	/* The EDE is added to the out-list so it is encoded in the cached message */
4118f76bb7dSCy Schubert 	if (qstate->env->cfg->ede && qstate->return_msg->rep->reason_bogus != LDNS_EDE_NONE) {
4128f76bb7dSCy Schubert 		edns_opt_list_append_ede(&edns.opt_list_out, qstate->env->scratch,
4138f76bb7dSCy Schubert 					qstate->return_msg->rep->reason_bogus,
4148f76bb7dSCy Schubert 					qstate->return_msg->rep->reason_bogus_str);
4158f76bb7dSCy Schubert 	}
4168f76bb7dSCy Schubert 
417e2d15004SDag-Erling Smørgrav 	if(verbosity >= VERB_ALGO)
418e2d15004SDag-Erling Smørgrav 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
419e2d15004SDag-Erling Smørgrav 	                qstate->return_msg->rep);
420e2d15004SDag-Erling Smørgrav 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
421e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->query_flags,
422e2d15004SDag-Erling Smørgrav 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
423e2d15004SDag-Erling Smørgrav 		return 0;
424e2d15004SDag-Erling Smørgrav 
425e2d15004SDag-Erling Smørgrav 	/* TTLs in the return_msg are relative to time(0) so we have to
426e2d15004SDag-Erling Smørgrav 	 * store that, we also store the smallest ttl in the packet+time(0)
427e2d15004SDag-Erling Smørgrav 	 * as the packet expiry time */
428e2d15004SDag-Erling Smørgrav 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
429e2d15004SDag-Erling Smørgrav 	timestamp = (uint64_t)*qstate->env->now;
430e2d15004SDag-Erling Smørgrav 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
431e2d15004SDag-Erling Smørgrav 	timestamp = htobe64(timestamp);
432e2d15004SDag-Erling Smørgrav 	expiry = htobe64(expiry);
433e2d15004SDag-Erling Smørgrav 	oldlim = sldns_buffer_limit(buf);
434e2d15004SDag-Erling Smørgrav 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
435e2d15004SDag-Erling Smørgrav 		sldns_buffer_capacity(buf))
436e2d15004SDag-Erling Smørgrav 		return 0; /* doesn't fit. */
437e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
438e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
439e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
440e2d15004SDag-Erling Smørgrav 		sizeof(expiry));
441e2d15004SDag-Erling Smørgrav 
442e2d15004SDag-Erling Smørgrav 	return 1;
443e2d15004SDag-Erling Smørgrav }
444e2d15004SDag-Erling Smørgrav 
445e2d15004SDag-Erling Smørgrav /** check expiry, return true if matches OK */
446e2d15004SDag-Erling Smørgrav static int
good_expiry_and_qinfo(struct module_qstate * qstate,struct sldns_buffer * buf)447e2d15004SDag-Erling Smørgrav good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
448e2d15004SDag-Erling Smørgrav {
449e2d15004SDag-Erling Smørgrav 	uint64_t expiry;
450e2d15004SDag-Erling Smørgrav 	/* the expiry time is the last bytes of the buffer */
451e2d15004SDag-Erling Smørgrav 	if(sldns_buffer_limit(buf) < sizeof(expiry))
452e2d15004SDag-Erling Smørgrav 		return 0;
453e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
454e2d15004SDag-Erling Smørgrav 		&expiry, sizeof(expiry));
455e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
456e2d15004SDag-Erling Smørgrav 
457091e9e46SCy Schubert 	/* Check if we are allowed to return expired entries:
458091e9e46SCy Schubert 	 * - serve_expired needs to be set
459091e9e46SCy Schubert 	 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
460091e9e46SCy Schubert 	 *   than that. */
4618a384985SDag-Erling Smørgrav 	if((time_t)expiry < *qstate->env->now &&
462091e9e46SCy Schubert 		(!qstate->env->cfg->serve_expired ||
463091e9e46SCy Schubert 			(SERVE_EXPIRED_TTL &&
464091e9e46SCy Schubert 			*qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
465e2d15004SDag-Erling Smørgrav 		return 0;
466e2d15004SDag-Erling Smørgrav 
467e2d15004SDag-Erling Smørgrav 	return 1;
468e2d15004SDag-Erling Smørgrav }
469e2d15004SDag-Erling Smørgrav 
4708a384985SDag-Erling Smørgrav /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
4718a384985SDag-Erling Smørgrav  * negative, set the TTL to 0. */
472c7f4d7adSDag-Erling Smørgrav static void
packed_rrset_ttl_subtract(struct packed_rrset_data * data,time_t subtract)473c7f4d7adSDag-Erling Smørgrav packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
474c7f4d7adSDag-Erling Smørgrav {
475c7f4d7adSDag-Erling Smørgrav 	size_t i;
476c7f4d7adSDag-Erling Smørgrav 	size_t total = data->count + data->rrsig_count;
4778a384985SDag-Erling Smørgrav 	if(subtract >= 0 && data->ttl > subtract)
478c7f4d7adSDag-Erling Smørgrav 		data->ttl -= subtract;
479c7f4d7adSDag-Erling Smørgrav 	else	data->ttl = 0;
480c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<total; i++) {
4818a384985SDag-Erling Smørgrav 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
482c7f4d7adSDag-Erling Smørgrav 			data->rr_ttl[i] -= subtract;
483c7f4d7adSDag-Erling Smørgrav 		else	data->rr_ttl[i] = 0;
484c7f4d7adSDag-Erling Smørgrav 	}
485f44e67d1SCy Schubert 	data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0;
486c7f4d7adSDag-Erling Smørgrav }
487c7f4d7adSDag-Erling Smørgrav 
4888a384985SDag-Erling Smørgrav /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
4898a384985SDag-Erling Smørgrav  * negative, set the TTLs to 0. */
490c7f4d7adSDag-Erling Smørgrav static void
adjust_msg_ttl(struct dns_msg * msg,time_t adjust)491c7f4d7adSDag-Erling Smørgrav adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
492c7f4d7adSDag-Erling Smørgrav {
493c7f4d7adSDag-Erling Smørgrav 	size_t i;
4948a384985SDag-Erling Smørgrav 	if(adjust >= 0 && msg->rep->ttl > adjust)
495c7f4d7adSDag-Erling Smørgrav 		msg->rep->ttl -= adjust;
496091e9e46SCy Schubert 	else
497091e9e46SCy Schubert 		msg->rep->ttl = 0;
498c7f4d7adSDag-Erling Smørgrav 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
4994c75e3aaSDag-Erling Smørgrav 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
500c7f4d7adSDag-Erling Smørgrav 
501c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<msg->rep->rrset_count; i++) {
502c7f4d7adSDag-Erling Smørgrav 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
503c7f4d7adSDag-Erling Smørgrav 			rep->rrsets[i]->entry.data, adjust);
504c7f4d7adSDag-Erling Smørgrav 	}
505c7f4d7adSDag-Erling Smørgrav }
506c7f4d7adSDag-Erling Smørgrav 
507335c7cdaSCy Schubert /* Set the TTL of the given RRset to fixed value. */
508335c7cdaSCy Schubert static void
packed_rrset_ttl_set(struct packed_rrset_data * data,time_t ttl)509335c7cdaSCy Schubert packed_rrset_ttl_set(struct packed_rrset_data* data, time_t ttl)
510335c7cdaSCy Schubert {
511335c7cdaSCy Schubert 	size_t i;
512335c7cdaSCy Schubert 	size_t total = data->count + data->rrsig_count;
513335c7cdaSCy Schubert 	data->ttl = ttl;
514335c7cdaSCy Schubert 	for(i=0; i<total; i++) {
515335c7cdaSCy Schubert 		data->rr_ttl[i] = ttl;
516335c7cdaSCy Schubert 	}
517335c7cdaSCy Schubert 	data->ttl_add = 0;
518335c7cdaSCy Schubert }
519335c7cdaSCy Schubert 
520335c7cdaSCy Schubert /* Set the TTL of a DNS message and its RRs by to a fixed value. */
521335c7cdaSCy Schubert static void
set_msg_ttl(struct dns_msg * msg,time_t ttl)522335c7cdaSCy Schubert set_msg_ttl(struct dns_msg* msg, time_t ttl)
523335c7cdaSCy Schubert {
524335c7cdaSCy Schubert 	size_t i;
525335c7cdaSCy Schubert 	msg->rep->ttl = ttl;
526335c7cdaSCy Schubert 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
527335c7cdaSCy Schubert 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
528335c7cdaSCy Schubert 
529335c7cdaSCy Schubert 	for(i=0; i<msg->rep->rrset_count; i++) {
530335c7cdaSCy Schubert 		packed_rrset_ttl_set((struct packed_rrset_data*)msg->
531335c7cdaSCy Schubert 			rep->rrsets[i]->entry.data, ttl);
532335c7cdaSCy Schubert 	}
533335c7cdaSCy Schubert }
534335c7cdaSCy Schubert 
535e2d15004SDag-Erling Smørgrav /** convert dns message in buffer to return_msg */
536e2d15004SDag-Erling Smørgrav static int
parse_data(struct module_qstate * qstate,struct sldns_buffer * buf,int * msg_expired)537335c7cdaSCy Schubert parse_data(struct module_qstate* qstate, struct sldns_buffer* buf,
538335c7cdaSCy Schubert 	int* msg_expired)
539e2d15004SDag-Erling Smørgrav {
540e2d15004SDag-Erling Smørgrav 	struct msg_parse* prs;
541e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
5428f76bb7dSCy Schubert 	struct edns_option* ede;
543e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
544e2d15004SDag-Erling Smørgrav 	time_t adjust;
545e2d15004SDag-Erling Smørgrav 	size_t lim = sldns_buffer_limit(buf);
546e2d15004SDag-Erling Smørgrav 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
547e2d15004SDag-Erling Smørgrav 		return 0; /* too short */
548e2d15004SDag-Erling Smørgrav 
549e2d15004SDag-Erling Smørgrav 	/* remove timestamp and expiry from end */
550e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
551e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
552e2d15004SDag-Erling Smørgrav 		&timestamp, sizeof(timestamp));
553e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
554e2d15004SDag-Erling Smørgrav 	timestamp = be64toh(timestamp);
555e2d15004SDag-Erling Smørgrav 
556e2d15004SDag-Erling Smørgrav 	/* parse DNS packet */
557e2d15004SDag-Erling Smørgrav 	regional_free_all(qstate->env->scratch);
558e2d15004SDag-Erling Smørgrav 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
559e2d15004SDag-Erling Smørgrav 		sizeof(struct msg_parse));
560e2d15004SDag-Erling Smørgrav 	if(!prs)
561e2d15004SDag-Erling Smørgrav 		return 0; /* out of memory */
562e2d15004SDag-Erling Smørgrav 	memset(prs, 0, sizeof(*prs));
563e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
564e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
565e2d15004SDag-Erling Smørgrav 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
566e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
567e2d15004SDag-Erling Smørgrav 		return 0;
568e2d15004SDag-Erling Smørgrav 	}
56924e36522SCy Schubert 	if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) !=
570e2d15004SDag-Erling Smørgrav 		LDNS_RCODE_NOERROR) {
571e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
572e2d15004SDag-Erling Smørgrav 		return 0;
573e2d15004SDag-Erling Smørgrav 	}
574e2d15004SDag-Erling Smørgrav 
575e2d15004SDag-Erling Smørgrav 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
576e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim);
577e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
578e2d15004SDag-Erling Smørgrav 		return 0;
579e2d15004SDag-Erling Smørgrav 
5808f76bb7dSCy Schubert 	/* We find the EDE in the in-list after parsing */
5818f76bb7dSCy Schubert 	if(qstate->env->cfg->ede &&
5828f76bb7dSCy Schubert 		(ede = edns_opt_list_find(edns.opt_list_in, LDNS_EDNS_EDE))) {
5838f76bb7dSCy Schubert 		if(ede->opt_len >= 2) {
5848f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus =
5858f76bb7dSCy Schubert 				sldns_read_uint16(ede->opt_data);
5868f76bb7dSCy Schubert 		}
5878f76bb7dSCy Schubert 		/* allocate space and store the error string and it's size */
5888f76bb7dSCy Schubert 		if(ede->opt_len > 2) {
5898f76bb7dSCy Schubert 			size_t ede_len = ede->opt_len - 2;
5908f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus_str = regional_alloc(
5918f76bb7dSCy Schubert 				qstate->region, sizeof(char) * (ede_len+1));
5928f76bb7dSCy Schubert 			memcpy(qstate->return_msg->rep->reason_bogus_str,
5938f76bb7dSCy Schubert 				ede->opt_data+2, ede_len);
5948f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus_str[ede_len] = 0;
5958f76bb7dSCy Schubert 		}
5968f76bb7dSCy Schubert 	}
5978f76bb7dSCy Schubert 
598e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
599e2d15004SDag-Erling Smørgrav 
600e2d15004SDag-Erling Smørgrav 	/* see how much of the TTL expired, and remove it */
601c7f4d7adSDag-Erling Smørgrav 	if(*qstate->env->now <= (time_t)timestamp) {
602c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
603c7f4d7adSDag-Erling Smørgrav 		return 1; /* message from the future (clock skew?) */
604e2d15004SDag-Erling Smørgrav 	}
605c7f4d7adSDag-Erling Smørgrav 	adjust = *qstate->env->now - (time_t)timestamp;
606c7f4d7adSDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl < adjust) {
607c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg expired");
608335c7cdaSCy Schubert 		*msg_expired = 1;
6098a384985SDag-Erling Smørgrav 		/* If serve-expired is enabled, we still use an expired message
6108a384985SDag-Erling Smørgrav 		 * setting the TTL to 0. */
6111838dec3SCy Schubert 		if(!qstate->env->cfg->serve_expired ||
6121838dec3SCy Schubert 			(FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
6131838dec3SCy Schubert 			!= LDNS_RCODE_NOERROR &&
6141838dec3SCy Schubert 			FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
6151838dec3SCy Schubert 			!= LDNS_RCODE_NXDOMAIN &&
6161838dec3SCy Schubert 			FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
6171838dec3SCy Schubert 			!= LDNS_RCODE_YXDOMAIN))
618c7f4d7adSDag-Erling Smørgrav 			return 0; /* message expired */
6191838dec3SCy Schubert 		else
6201838dec3SCy Schubert 			adjust = -1;
621c7f4d7adSDag-Erling Smørgrav 	}
622c7f4d7adSDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
623c7f4d7adSDag-Erling Smørgrav 	adjust_msg_ttl(qstate->return_msg, adjust);
624*46d2f618SCy Schubert 	if(qstate->env->cfg->aggressive_nsec) {
625*46d2f618SCy Schubert 		limit_nsec_ttl(qstate->return_msg);
626*46d2f618SCy Schubert 	}
6278a384985SDag-Erling Smørgrav 
6288a384985SDag-Erling Smørgrav 	/* Similar to the unbound worker, if serve-expired is enabled and
6298a384985SDag-Erling Smørgrav 	 * the msg would be considered to be expired, mark the state so a
6308a384985SDag-Erling Smørgrav 	 * refetch will be scheduled.  The comparison between 'expiry' and
6318a384985SDag-Erling Smørgrav 	 * 'now' should be redundant given how these values were calculated,
6328a384985SDag-Erling Smørgrav 	 * but we check it just in case as does good_expiry_and_qinfo(). */
6338a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired &&
634335c7cdaSCy Schubert 		!qstate->env->cfg->serve_expired_client_timeout &&
6358a384985SDag-Erling Smørgrav 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
6368a384985SDag-Erling Smørgrav 		qstate->need_refetch = 1;
6378a384985SDag-Erling Smørgrav 	}
6388a384985SDag-Erling Smørgrav 
639c7f4d7adSDag-Erling Smørgrav 	return 1;
640e2d15004SDag-Erling Smørgrav }
641e2d15004SDag-Erling Smørgrav 
642e2d15004SDag-Erling Smørgrav /**
643e2d15004SDag-Erling Smørgrav  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
644e2d15004SDag-Erling Smørgrav  * return true if lookup was successful.
645e2d15004SDag-Erling Smørgrav  */
646e2d15004SDag-Erling Smørgrav static int
cachedb_extcache_lookup(struct module_qstate * qstate,struct cachedb_env * ie,int * msg_expired)647335c7cdaSCy Schubert cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie,
648335c7cdaSCy Schubert 	int* msg_expired)
649e2d15004SDag-Erling Smørgrav {
650e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
65156850988SCy Schubert 	calc_hash(&qstate->qinfo, qstate->env, key, sizeof(key));
652e2d15004SDag-Erling Smørgrav 
653e2d15004SDag-Erling Smørgrav 	/* call backend to fetch data for key into scratch buffer */
654e2d15004SDag-Erling Smørgrav 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
655e2d15004SDag-Erling Smørgrav 		qstate->env->scratch_buffer)) {
656e2d15004SDag-Erling Smørgrav 		return 0;
657e2d15004SDag-Erling Smørgrav 	}
658e2d15004SDag-Erling Smørgrav 
659e2d15004SDag-Erling Smørgrav 	/* check expiry date and check if query-data matches */
660e2d15004SDag-Erling Smørgrav 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
661e2d15004SDag-Erling Smørgrav 		return 0;
662e2d15004SDag-Erling Smørgrav 	}
663e2d15004SDag-Erling Smørgrav 
664e2d15004SDag-Erling Smørgrav 	/* parse dns message into return_msg */
665335c7cdaSCy Schubert 	if( !parse_data(qstate, qstate->env->scratch_buffer, msg_expired) ) {
666e2d15004SDag-Erling Smørgrav 		return 0;
667e2d15004SDag-Erling Smørgrav 	}
668e2d15004SDag-Erling Smørgrav 	return 1;
669e2d15004SDag-Erling Smørgrav }
670e2d15004SDag-Erling Smørgrav 
671e2d15004SDag-Erling Smørgrav /**
672e2d15004SDag-Erling Smørgrav  * Store the qstate.return_msg in extcache for key qstate.info
673e2d15004SDag-Erling Smørgrav  */
674e2d15004SDag-Erling Smørgrav static void
cachedb_extcache_store(struct module_qstate * qstate,struct cachedb_env * ie)675e2d15004SDag-Erling Smørgrav cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
676e2d15004SDag-Erling Smørgrav {
677e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
67856850988SCy Schubert 	calc_hash(&qstate->qinfo, qstate->env, key, sizeof(key));
679e2d15004SDag-Erling Smørgrav 
680e2d15004SDag-Erling Smørgrav 	/* prepare data in scratch buffer */
681e2d15004SDag-Erling Smørgrav 	if(!prep_data(qstate, qstate->env->scratch_buffer))
682e2d15004SDag-Erling Smørgrav 		return;
683e2d15004SDag-Erling Smørgrav 
684e2d15004SDag-Erling Smørgrav 	/* call backend */
685e2d15004SDag-Erling Smørgrav 	(*ie->backend->store)(qstate->env, ie, key,
686e2d15004SDag-Erling Smørgrav 		sldns_buffer_begin(qstate->env->scratch_buffer),
68725039b37SCy Schubert 		sldns_buffer_limit(qstate->env->scratch_buffer),
68825039b37SCy Schubert 		qstate->return_msg->rep->ttl);
689e2d15004SDag-Erling Smørgrav }
690e2d15004SDag-Erling Smørgrav 
691e2d15004SDag-Erling Smørgrav /**
692e2d15004SDag-Erling Smørgrav  * See if unbound's internal cache can answer the query
693e2d15004SDag-Erling Smørgrav  */
694e2d15004SDag-Erling Smørgrav static int
cachedb_intcache_lookup(struct module_qstate * qstate,struct cachedb_env * cde)6958f76bb7dSCy Schubert cachedb_intcache_lookup(struct module_qstate* qstate, struct cachedb_env* cde)
696e2d15004SDag-Erling Smørgrav {
697335c7cdaSCy Schubert 	uint8_t dpname_storage[LDNS_MAX_DOMAINLEN+1];
6985469a995SCy Schubert 	uint8_t* dpname=NULL;
6995469a995SCy Schubert 	size_t dpnamelen=0;
700e2d15004SDag-Erling Smørgrav 	struct dns_msg* msg;
7018f76bb7dSCy Schubert 	/* for testframe bypass this lookup */
7028f76bb7dSCy Schubert 	if(cde->backend == &testframe_backend) {
7038f76bb7dSCy Schubert 		return 0;
7048f76bb7dSCy Schubert 	}
7055469a995SCy Schubert 	if(iter_stub_fwd_no_cache(qstate, &qstate->qinfo,
706335c7cdaSCy Schubert 		&dpname, &dpnamelen, dpname_storage, sizeof(dpname_storage)))
7075469a995SCy Schubert 		return 0; /* no cache for these queries */
708e2d15004SDag-Erling Smørgrav 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
709e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
710e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qclass, qstate->query_flags,
71157bddd21SDag-Erling Smørgrav 		qstate->region, qstate->env->scratch,
7125469a995SCy Schubert 		1, /* no partial messages with only a CNAME */
7135469a995SCy Schubert 		dpname, dpnamelen
71457bddd21SDag-Erling Smørgrav 		);
7150fb34990SDag-Erling Smørgrav 	if(!msg && qstate->env->neg_cache &&
7160fb34990SDag-Erling Smørgrav 		iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
717e2d15004SDag-Erling Smørgrav 		/* lookup in negative cache; may result in
718e2d15004SDag-Erling Smørgrav 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
719e2d15004SDag-Erling Smørgrav 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
720e2d15004SDag-Erling Smørgrav 			qstate->region, qstate->env->rrset_cache,
721e2d15004SDag-Erling Smørgrav 			qstate->env->scratch_buffer,
72257bddd21SDag-Erling Smørgrav 			*qstate->env->now, 1/*add SOA*/, NULL,
72357bddd21SDag-Erling Smørgrav 			qstate->env->cfg);
724e2d15004SDag-Erling Smørgrav 	}
725e2d15004SDag-Erling Smørgrav 	if(!msg)
726e2d15004SDag-Erling Smørgrav 		return 0;
727e2d15004SDag-Erling Smørgrav 	/* this is the returned msg */
728e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
729e2d15004SDag-Erling Smørgrav 	qstate->return_msg = msg;
730e2d15004SDag-Erling Smørgrav 	return 1;
731e2d15004SDag-Erling Smørgrav }
732e2d15004SDag-Erling Smørgrav 
733e2d15004SDag-Erling Smørgrav /**
734e2d15004SDag-Erling Smørgrav  * Store query into the internal cache of unbound.
735e2d15004SDag-Erling Smørgrav  */
736e2d15004SDag-Erling Smørgrav static void
cachedb_intcache_store(struct module_qstate * qstate,int msg_expired)737335c7cdaSCy Schubert cachedb_intcache_store(struct module_qstate* qstate, int msg_expired)
738e2d15004SDag-Erling Smørgrav {
7398a384985SDag-Erling Smørgrav 	uint32_t store_flags = qstate->query_flags;
740335c7cdaSCy Schubert 	int serve_expired = qstate->env->cfg->serve_expired;
7418a384985SDag-Erling Smørgrav 
7428a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired)
7438a384985SDag-Erling Smørgrav 		store_flags |= DNSCACHE_STORE_ZEROTTL;
744e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
745e2d15004SDag-Erling Smørgrav 		return;
746335c7cdaSCy Schubert 	if(serve_expired && msg_expired) {
747335c7cdaSCy Schubert 		/* Set TTLs to a value such that value + *env->now is
748335c7cdaSCy Schubert 		 * going to be now-3 seconds. Making it expired
749335c7cdaSCy Schubert 		 * in the cache. */
750335c7cdaSCy Schubert 		set_msg_ttl(qstate->return_msg, (time_t)-3);
75156850988SCy Schubert 		/* The expired entry does not get checked by the validator
75256850988SCy Schubert 		 * and we need a validation value for it. */
75356850988SCy Schubert 		if(qstate->env->cfg->cachedb_check_when_serve_expired)
75456850988SCy Schubert 			qstate->return_msg->rep->security = sec_status_insecure;
755335c7cdaSCy Schubert 	}
756e2d15004SDag-Erling Smørgrav 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
757e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
758790c6b24SCy Schubert 		qstate->region, store_flags, qstate->qstarttime);
759335c7cdaSCy Schubert 	if(serve_expired && msg_expired) {
760335c7cdaSCy Schubert 		if(qstate->env->cfg->serve_expired_client_timeout) {
761335c7cdaSCy Schubert 			/* No expired response from the query state, the
762335c7cdaSCy Schubert 			 * query resolution needs to continue and it can
763335c7cdaSCy Schubert 			 * pick up the expired result after the timer out
764335c7cdaSCy Schubert 			 * of cache. */
765335c7cdaSCy Schubert 			return;
766335c7cdaSCy Schubert 		}
767335c7cdaSCy Schubert 		/* set TTLs to zero again */
768335c7cdaSCy Schubert 		adjust_msg_ttl(qstate->return_msg, -1);
769335c7cdaSCy Schubert 		/* Send serve expired responses based on the cachedb
770335c7cdaSCy Schubert 		 * returned message, that was just stored in the cache.
771335c7cdaSCy Schubert 		 * It can then continue to work on this query. */
772335c7cdaSCy Schubert 		mesh_respond_serve_expired(qstate->mesh_info);
773335c7cdaSCy Schubert 	}
774e2d15004SDag-Erling Smørgrav }
775e2d15004SDag-Erling Smørgrav 
776e2d15004SDag-Erling Smørgrav /**
777e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a query
778e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
779e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
780e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
781e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
782e2d15004SDag-Erling Smørgrav  * @param id: module id.
783e2d15004SDag-Erling Smørgrav  */
784e2d15004SDag-Erling Smørgrav static void
cachedb_handle_query(struct module_qstate * qstate,struct cachedb_qstate * ATTR_UNUSED (iq),struct cachedb_env * ie,int id)785e2d15004SDag-Erling Smørgrav cachedb_handle_query(struct module_qstate* qstate,
786e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq),
787e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie, int id)
788e2d15004SDag-Erling Smørgrav {
789335c7cdaSCy Schubert 	int msg_expired = 0;
7908f76bb7dSCy Schubert 	qstate->is_cachedb_answer = 0;
791e2d15004SDag-Erling Smørgrav 	/* check if we are enabled, and skip if so */
792e2d15004SDag-Erling Smørgrav 	if(!ie->enabled) {
793e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
794e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
795e2d15004SDag-Erling Smørgrav 		return;
796e2d15004SDag-Erling Smørgrav 	}
797e2d15004SDag-Erling Smørgrav 
798bc892140SDag-Erling Smørgrav 	if(qstate->blacklist || qstate->no_cache_lookup) {
799bc892140SDag-Erling Smørgrav 		/* cache is blacklisted or we are instructed from edns to not look */
800e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
801e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
802e2d15004SDag-Erling Smørgrav 		return;
803e2d15004SDag-Erling Smørgrav 	}
804e2d15004SDag-Erling Smørgrav 
805091e9e46SCy Schubert 	/* lookup inside unbound's internal cache.
806091e9e46SCy Schubert 	 * This does not look for expired entries. */
8078f76bb7dSCy Schubert 	if(cachedb_intcache_lookup(qstate, ie)) {
808bc892140SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO) {
809bc892140SDag-Erling Smørgrav 			if(qstate->return_msg->rep)
810e2d15004SDag-Erling Smørgrav 				log_dns_msg("cachedb internal cache lookup",
811e2d15004SDag-Erling Smørgrav 					&qstate->return_msg->qinfo,
812e2d15004SDag-Erling Smørgrav 					qstate->return_msg->rep);
813bc892140SDag-Erling Smørgrav 			else log_info("cachedb internal cache lookup: rcode %s",
814091e9e46SCy Schubert 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
815091e9e46SCy Schubert 				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
816091e9e46SCy Schubert 				:"??");
817bc892140SDag-Erling Smørgrav 		}
818e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
819e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
820e2d15004SDag-Erling Smørgrav 		return;
821e2d15004SDag-Erling Smørgrav 	}
822e2d15004SDag-Erling Smørgrav 
823e2d15004SDag-Erling Smørgrav 	/* ask backend cache to see if we have data */
824335c7cdaSCy Schubert 	if(cachedb_extcache_lookup(qstate, ie, &msg_expired)) {
825e2d15004SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO)
826e2d15004SDag-Erling Smørgrav 			log_dns_msg(ie->backend->name,
827e2d15004SDag-Erling Smørgrav 				&qstate->return_msg->qinfo,
828e2d15004SDag-Erling Smørgrav 				qstate->return_msg->rep);
829e2d15004SDag-Erling Smørgrav 		/* store this result in internal cache */
830335c7cdaSCy Schubert 		cachedb_intcache_store(qstate, msg_expired);
831091e9e46SCy Schubert 		/* In case we have expired data but there is a client timer for expired
832091e9e46SCy Schubert 		 * answers, pass execution to next module in order to try updating the
833091e9e46SCy Schubert 		 * data first.
834335c7cdaSCy Schubert 		 */
835335c7cdaSCy Schubert 		if(qstate->env->cfg->serve_expired && msg_expired) {
836335c7cdaSCy Schubert 			qstate->return_msg = NULL;
837335c7cdaSCy Schubert 			qstate->ext_state[id] = module_wait_module;
838335c7cdaSCy Schubert 			/* The expired reply is sent with
839335c7cdaSCy Schubert 			 * mesh_respond_serve_expired, and so
840335c7cdaSCy Schubert 			 * the need_refetch is not used. */
841335c7cdaSCy Schubert 			qstate->need_refetch = 0;
842335c7cdaSCy Schubert 			return;
843335c7cdaSCy Schubert 		}
844091e9e46SCy Schubert 		if(qstate->need_refetch && qstate->serve_expired_data &&
845091e9e46SCy Schubert 			qstate->serve_expired_data->timer) {
846091e9e46SCy Schubert 				qstate->return_msg = NULL;
847091e9e46SCy Schubert 				qstate->ext_state[id] = module_wait_module;
848091e9e46SCy Schubert 				return;
849091e9e46SCy Schubert 		}
8508f76bb7dSCy Schubert 		qstate->is_cachedb_answer = 1;
851e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
852e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
853e2d15004SDag-Erling Smørgrav 		return;
854e2d15004SDag-Erling Smørgrav 	}
855e2d15004SDag-Erling Smørgrav 
856335c7cdaSCy Schubert 	if(qstate->serve_expired_data &&
857335c7cdaSCy Schubert 		qstate->env->cfg->cachedb_check_when_serve_expired &&
858335c7cdaSCy Schubert 		!qstate->env->cfg->serve_expired_client_timeout) {
859335c7cdaSCy Schubert 		/* Reply with expired data if any to client, because cachedb
860335c7cdaSCy Schubert 		 * also has no useful, current data */
861335c7cdaSCy Schubert 		mesh_respond_serve_expired(qstate->mesh_info);
862335c7cdaSCy Schubert 	}
863335c7cdaSCy Schubert 
864e2d15004SDag-Erling Smørgrav 	/* no cache fetches */
865e2d15004SDag-Erling Smørgrav 	/* pass request to next module */
866e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_module;
867e2d15004SDag-Erling Smørgrav }
868e2d15004SDag-Erling Smørgrav 
869e2d15004SDag-Erling Smørgrav /**
870e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a response from the iterator.
871e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
872e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
873e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
874e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
875e2d15004SDag-Erling Smørgrav  * @param id: module id.
876e2d15004SDag-Erling Smørgrav  */
877e2d15004SDag-Erling Smørgrav static void
cachedb_handle_response(struct module_qstate * qstate,struct cachedb_qstate * ATTR_UNUSED (iq),struct cachedb_env * ie,int id)878e2d15004SDag-Erling Smørgrav cachedb_handle_response(struct module_qstate* qstate,
879e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
880e2d15004SDag-Erling Smørgrav {
8818f76bb7dSCy Schubert 	qstate->is_cachedb_answer = 0;
882bc892140SDag-Erling Smørgrav 	/* check if we are not enabled or instructed to not cache, and skip */
883bc892140SDag-Erling Smørgrav 	if(!ie->enabled || qstate->no_cache_store) {
884e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
885e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
886e2d15004SDag-Erling Smørgrav 		return;
887e2d15004SDag-Erling Smørgrav 	}
888103ba509SCy Schubert 	if(qstate->env->cfg->cachedb_no_store) {
889103ba509SCy Schubert 		/* do not store the item in the external cache */
890103ba509SCy Schubert 		qstate->ext_state[id] = module_finished;
891103ba509SCy Schubert 		return;
892103ba509SCy Schubert 	}
893e2d15004SDag-Erling Smørgrav 
894e2d15004SDag-Erling Smørgrav 	/* store the item into the backend cache */
895e2d15004SDag-Erling Smørgrav 	cachedb_extcache_store(qstate, ie);
896e2d15004SDag-Erling Smørgrav 
897e2d15004SDag-Erling Smørgrav 	/* we are done with the query */
898e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
899e2d15004SDag-Erling Smørgrav }
900e2d15004SDag-Erling Smørgrav 
901e2d15004SDag-Erling Smørgrav void
cachedb_operate(struct module_qstate * qstate,enum module_ev event,int id,struct outbound_entry * outbound)902e2d15004SDag-Erling Smørgrav cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
903e2d15004SDag-Erling Smørgrav 	struct outbound_entry* outbound)
904e2d15004SDag-Erling Smørgrav {
905e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
906e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
907e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
908e2d15004SDag-Erling Smørgrav 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
909e2d15004SDag-Erling Smørgrav 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
910e2d15004SDag-Erling Smørgrav 		&qstate->qinfo);
911e2d15004SDag-Erling Smørgrav 
912e2d15004SDag-Erling Smørgrav 	/* perform cachedb state machine */
913e2d15004SDag-Erling Smørgrav 	if((event == module_event_new || event == module_event_pass) &&
914e2d15004SDag-Erling Smørgrav 		iq == NULL) {
915e2d15004SDag-Erling Smørgrav 		if(!cachedb_new(qstate, id)) {
916e2d15004SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
917e2d15004SDag-Erling Smørgrav 			return;
918e2d15004SDag-Erling Smørgrav 		}
919e2d15004SDag-Erling Smørgrav 		iq = (struct cachedb_qstate*)qstate->minfo[id];
920e2d15004SDag-Erling Smørgrav 	}
921e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_pass || event == module_event_new)) {
922e2d15004SDag-Erling Smørgrav 		cachedb_handle_query(qstate, iq, ie, id);
923e2d15004SDag-Erling Smørgrav 		return;
924e2d15004SDag-Erling Smørgrav 	}
925e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_moddone)) {
926e2d15004SDag-Erling Smørgrav 		cachedb_handle_response(qstate, iq, ie, id);
927e2d15004SDag-Erling Smørgrav 		return;
928e2d15004SDag-Erling Smørgrav 	}
929e2d15004SDag-Erling Smørgrav 	if(iq && outbound) {
930e2d15004SDag-Erling Smørgrav 		/* cachedb does not need to process responses at this time
931e2d15004SDag-Erling Smørgrav 		 * ignore it.
932e2d15004SDag-Erling Smørgrav 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
933e2d15004SDag-Erling Smørgrav 		*/
934e2d15004SDag-Erling Smørgrav 		return;
935e2d15004SDag-Erling Smørgrav 	}
936e2d15004SDag-Erling Smørgrav 	if(event == module_event_error) {
937e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "got called with event error, giving up");
938e2d15004SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
939e2d15004SDag-Erling Smørgrav 		return;
940e2d15004SDag-Erling Smørgrav 	}
941bc892140SDag-Erling Smørgrav 	if(!iq && (event == module_event_moddone)) {
942bc892140SDag-Erling Smørgrav 		/* during priming, module done but we never started */
943bc892140SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
944bc892140SDag-Erling Smørgrav 		return;
945bc892140SDag-Erling Smørgrav 	}
946e2d15004SDag-Erling Smørgrav 
947e2d15004SDag-Erling Smørgrav 	log_err("bad event for cachedb");
948e2d15004SDag-Erling Smørgrav 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
949e2d15004SDag-Erling Smørgrav }
950e2d15004SDag-Erling Smørgrav 
951e2d15004SDag-Erling Smørgrav void
cachedb_inform_super(struct module_qstate * ATTR_UNUSED (qstate),int ATTR_UNUSED (id),struct module_qstate * ATTR_UNUSED (super))952e2d15004SDag-Erling Smørgrav cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
953e2d15004SDag-Erling Smørgrav 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
954e2d15004SDag-Erling Smørgrav {
955e2d15004SDag-Erling Smørgrav 	/* cachedb does not use subordinate requests at this time */
956e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb inform_super was called");
957e2d15004SDag-Erling Smørgrav }
958e2d15004SDag-Erling Smørgrav 
959e2d15004SDag-Erling Smørgrav void
cachedb_clear(struct module_qstate * qstate,int id)960e2d15004SDag-Erling Smørgrav cachedb_clear(struct module_qstate* qstate, int id)
961e2d15004SDag-Erling Smørgrav {
962e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq;
963e2d15004SDag-Erling Smørgrav 	if(!qstate)
964e2d15004SDag-Erling Smørgrav 		return;
965e2d15004SDag-Erling Smørgrav 	iq = (struct cachedb_qstate*)qstate->minfo[id];
966e2d15004SDag-Erling Smørgrav 	if(iq) {
967e2d15004SDag-Erling Smørgrav 		/* free contents of iq */
968e2d15004SDag-Erling Smørgrav 		/* TODO */
969e2d15004SDag-Erling Smørgrav 	}
970e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = NULL;
971e2d15004SDag-Erling Smørgrav }
972e2d15004SDag-Erling Smørgrav 
973e2d15004SDag-Erling Smørgrav size_t
cachedb_get_mem(struct module_env * env,int id)974e2d15004SDag-Erling Smørgrav cachedb_get_mem(struct module_env* env, int id)
975e2d15004SDag-Erling Smørgrav {
976e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
977e2d15004SDag-Erling Smørgrav 	if(!ie)
978e2d15004SDag-Erling Smørgrav 		return 0;
979e2d15004SDag-Erling Smørgrav 	return sizeof(*ie); /* TODO - more mem */
980e2d15004SDag-Erling Smørgrav }
981e2d15004SDag-Erling Smørgrav 
982e2d15004SDag-Erling Smørgrav /**
983e2d15004SDag-Erling Smørgrav  * The cachedb function block
984e2d15004SDag-Erling Smørgrav  */
985e2d15004SDag-Erling Smørgrav static struct module_func_block cachedb_block = {
986e2d15004SDag-Erling Smørgrav 	"cachedb",
98756850988SCy Schubert 	NULL, NULL, &cachedb_init, &cachedb_deinit, &cachedb_operate,
988e2d15004SDag-Erling Smørgrav 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
989e2d15004SDag-Erling Smørgrav };
990e2d15004SDag-Erling Smørgrav 
991e2d15004SDag-Erling Smørgrav struct module_func_block*
cachedb_get_funcblock(void)992e2d15004SDag-Erling Smørgrav cachedb_get_funcblock(void)
993e2d15004SDag-Erling Smørgrav {
994e2d15004SDag-Erling Smørgrav 	return &cachedb_block;
995e2d15004SDag-Erling Smørgrav }
996335c7cdaSCy Schubert 
997335c7cdaSCy Schubert int
cachedb_is_enabled(struct module_stack * mods,struct module_env * env)998335c7cdaSCy Schubert cachedb_is_enabled(struct module_stack* mods, struct module_env* env)
999335c7cdaSCy Schubert {
1000335c7cdaSCy Schubert 	struct cachedb_env* ie;
1001335c7cdaSCy Schubert 	int id = modstack_find(mods, "cachedb");
1002335c7cdaSCy Schubert 	if(id == -1)
1003335c7cdaSCy Schubert 		return 0;
1004335c7cdaSCy Schubert 	ie = (struct cachedb_env*)env->modinfo[id];
1005335c7cdaSCy Schubert 	if(ie && ie->enabled)
1006335c7cdaSCy Schubert 		return 1;
1007335c7cdaSCy Schubert 	return 0;
1008335c7cdaSCy Schubert }
1009335c7cdaSCy Schubert 
cachedb_msg_remove(struct module_qstate * qstate)1010335c7cdaSCy Schubert void cachedb_msg_remove(struct module_qstate* qstate)
1011335c7cdaSCy Schubert {
101256850988SCy Schubert 	cachedb_msg_remove_qinfo(qstate->env, &qstate->qinfo);
101356850988SCy Schubert }
1014335c7cdaSCy Schubert 
cachedb_msg_remove_qinfo(struct module_env * env,struct query_info * qinfo)101556850988SCy Schubert void cachedb_msg_remove_qinfo(struct module_env* env, struct query_info* qinfo)
101656850988SCy Schubert {
101756850988SCy Schubert 	char key[(CACHEDB_HASHSIZE/8)*2+1];
101856850988SCy Schubert 	int id = modstack_find(env->modstack, "cachedb");
101956850988SCy Schubert 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
102056850988SCy Schubert 
102156850988SCy Schubert 	log_query_info(VERB_ALGO, "cachedb msg remove", qinfo);
102256850988SCy Schubert 	calc_hash(qinfo, env, key, sizeof(key));
102356850988SCy Schubert 	sldns_buffer_clear(env->scratch_buffer);
102456850988SCy Schubert 	sldns_buffer_write_u32(env->scratch_buffer, 0);
102556850988SCy Schubert 	sldns_buffer_flip(env->scratch_buffer);
1026335c7cdaSCy Schubert 
1027335c7cdaSCy Schubert 	/* call backend */
102856850988SCy Schubert 	(*ie->backend->store)(env, ie, key,
102956850988SCy Schubert 		sldns_buffer_begin(env->scratch_buffer),
103056850988SCy Schubert 		sldns_buffer_limit(env->scratch_buffer),
1031335c7cdaSCy Schubert 		0);
1032335c7cdaSCy Schubert }
1033e2d15004SDag-Erling Smørgrav #endif /* USE_CACHEDB */
1034