xref: /freebsd/contrib/unbound/cachedb/cachedb.c (revision 103ba509e72e3949d22485666949e9705d4af8cd)
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"
53e2d15004SDag-Erling Smørgrav #include "validator/val_neg.h"
54e2d15004SDag-Erling Smørgrav #include "validator/val_secalgo.h"
55e2d15004SDag-Erling Smørgrav #include "iterator/iter_utils.h"
56e2d15004SDag-Erling Smørgrav #include "sldns/parseutil.h"
57e2d15004SDag-Erling Smørgrav #include "sldns/wire2str.h"
58e2d15004SDag-Erling Smørgrav #include "sldns/sbuffer.h"
59e2d15004SDag-Erling Smørgrav 
600fb34990SDag-Erling Smørgrav /* header file for htobe64 */
610fb34990SDag-Erling Smørgrav #ifdef HAVE_ENDIAN_H
620fb34990SDag-Erling Smørgrav #  include <endian.h>
630fb34990SDag-Erling Smørgrav #endif
640fb34990SDag-Erling Smørgrav #ifdef HAVE_SYS_ENDIAN_H
650fb34990SDag-Erling Smørgrav #  include <sys/endian.h>
660fb34990SDag-Erling Smørgrav #endif
67091e9e46SCy Schubert 
68091e9e46SCy Schubert #ifndef HAVE_HTOBE64
690fb34990SDag-Erling Smørgrav #  ifdef HAVE_LIBKERN_OSBYTEORDER_H
700fb34990SDag-Erling Smørgrav      /* In practice this is specific to MacOS X.  We assume it doesn't have
710fb34990SDag-Erling Smørgrav       * htobe64/be64toh but has alternatives with a different name. */
720fb34990SDag-Erling Smørgrav #    include <libkern/OSByteOrder.h>
730fb34990SDag-Erling Smørgrav #    define htobe64(x) OSSwapHostToBigInt64(x)
740fb34990SDag-Erling Smørgrav #    define be64toh(x) OSSwapBigToHostInt64(x)
75091e9e46SCy Schubert #  else
76091e9e46SCy Schubert      /* not OSX */
77e86b9096SDag-Erling Smørgrav      /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */
78e86b9096SDag-Erling Smørgrav #    if __BIG_ENDIAN__
79e86b9096SDag-Erling Smørgrav #      define be64toh(n) (n)
80e86b9096SDag-Erling Smørgrav #      define htobe64(n) (n)
81e86b9096SDag-Erling Smørgrav #    else
82e86b9096SDag-Erling Smørgrav #      define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
83e86b9096SDag-Erling Smørgrav #      define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
84091e9e46SCy Schubert #    endif /* _ENDIAN */
85091e9e46SCy Schubert #  endif /* HAVE_LIBKERN_OSBYTEORDER_H */
86091e9e46SCy Schubert #endif /* HAVE_BE64TOH */
87e86b9096SDag-Erling Smørgrav 
88e2d15004SDag-Erling Smørgrav /** the unit test testframe for cachedb, its module state contains
89e2d15004SDag-Erling Smørgrav  * a cache for a couple queries (in memory). */
90e2d15004SDag-Erling Smørgrav struct testframe_moddata {
91971980c3SDag-Erling Smørgrav 	/** lock for mutex */
92971980c3SDag-Erling Smørgrav 	lock_basic_type lock;
93e2d15004SDag-Erling Smørgrav 	/** key for single stored data element, NULL if none */
94e2d15004SDag-Erling Smørgrav 	char* stored_key;
95e2d15004SDag-Erling Smørgrav 	/** data for single stored data element, NULL if none */
96e2d15004SDag-Erling Smørgrav 	uint8_t* stored_data;
97e2d15004SDag-Erling Smørgrav 	/** length of stored data */
98e2d15004SDag-Erling Smørgrav 	size_t stored_datalen;
99e2d15004SDag-Erling Smørgrav };
100e2d15004SDag-Erling Smørgrav 
101e2d15004SDag-Erling Smørgrav static int
102e2d15004SDag-Erling Smørgrav testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
103e2d15004SDag-Erling Smørgrav {
104971980c3SDag-Erling Smørgrav 	struct testframe_moddata* d;
105e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_init");
106971980c3SDag-Erling Smørgrav 	d = (struct testframe_moddata*)calloc(1,
107e2d15004SDag-Erling Smørgrav 		sizeof(struct testframe_moddata));
108971980c3SDag-Erling Smørgrav 	cachedb_env->backend_data = (void*)d;
109e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend_data) {
110e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
111e2d15004SDag-Erling Smørgrav 		return 0;
112e2d15004SDag-Erling Smørgrav 	}
1138f76bb7dSCy Schubert 	/* Register an EDNS option (65534) to bypass the worker cache lookup
1148f76bb7dSCy Schubert 	 * for testing */
1158f76bb7dSCy Schubert 	if(!edns_register_option(LDNS_EDNS_UNBOUND_CACHEDB_TESTFRAME_TEST,
1168f76bb7dSCy Schubert 		1 /* bypass cache */,
1178f76bb7dSCy Schubert 		0 /* no aggregation */, env)) {
1188f76bb7dSCy Schubert 		log_err("testframe_init, could not register test opcode");
1198f76bb7dSCy Schubert 		free(d);
1208f76bb7dSCy Schubert 		return 0;
1218f76bb7dSCy Schubert 	}
122971980c3SDag-Erling Smørgrav 	lock_basic_init(&d->lock);
123971980c3SDag-Erling Smørgrav 	lock_protect(&d->lock, d, sizeof(*d));
124e2d15004SDag-Erling Smørgrav 	return 1;
125e2d15004SDag-Erling Smørgrav }
126e2d15004SDag-Erling Smørgrav 
127e2d15004SDag-Erling Smørgrav static void
128e2d15004SDag-Erling Smørgrav testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
129e2d15004SDag-Erling Smørgrav {
130e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
131e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
132e2d15004SDag-Erling Smørgrav 	(void)env;
133e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_deinit");
134e2d15004SDag-Erling Smørgrav 	if(!d)
135e2d15004SDag-Erling Smørgrav 		return;
136971980c3SDag-Erling Smørgrav 	lock_basic_destroy(&d->lock);
137e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
138e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
139e2d15004SDag-Erling Smørgrav 	free(d);
140e2d15004SDag-Erling Smørgrav }
141e2d15004SDag-Erling Smørgrav 
142e2d15004SDag-Erling Smørgrav static int
143e2d15004SDag-Erling Smørgrav testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
144e2d15004SDag-Erling Smørgrav 	char* key, struct sldns_buffer* result_buffer)
145e2d15004SDag-Erling Smørgrav {
146e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
147e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
148e2d15004SDag-Erling Smørgrav 	(void)env;
149e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
150971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
151e2d15004SDag-Erling Smørgrav 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
152971980c3SDag-Erling Smørgrav 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
153971980c3SDag-Erling Smørgrav 			lock_basic_unlock(&d->lock);
154e2d15004SDag-Erling Smørgrav 			return 0; /* too large */
155971980c3SDag-Erling Smørgrav 		}
156e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
157e2d15004SDag-Erling Smørgrav 			(int)d->stored_datalen);
158e2d15004SDag-Erling Smørgrav 		sldns_buffer_clear(result_buffer);
159e2d15004SDag-Erling Smørgrav 		sldns_buffer_write(result_buffer, d->stored_data,
160e2d15004SDag-Erling Smørgrav 			d->stored_datalen);
161e2d15004SDag-Erling Smørgrav 		sldns_buffer_flip(result_buffer);
162971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
163e2d15004SDag-Erling Smørgrav 		return 1;
164e2d15004SDag-Erling Smørgrav 	}
165971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
166e2d15004SDag-Erling Smørgrav 	return 0;
167e2d15004SDag-Erling Smørgrav }
168e2d15004SDag-Erling Smørgrav 
169e2d15004SDag-Erling Smørgrav static void
170e2d15004SDag-Erling Smørgrav testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
17125039b37SCy Schubert 	char* key, uint8_t* data, size_t data_len, time_t ATTR_UNUSED(ttl))
172e2d15004SDag-Erling Smørgrav {
173e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
174e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
175e2d15004SDag-Erling Smørgrav 	(void)env;
176971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
177e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
178e2d15004SDag-Erling Smørgrav 
179e2d15004SDag-Erling Smørgrav 	/* free old data element (if any) */
180e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
181e2d15004SDag-Erling Smørgrav 	d->stored_key = NULL;
182e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
183e2d15004SDag-Erling Smørgrav 	d->stored_data = NULL;
184e2d15004SDag-Erling Smørgrav 	d->stored_datalen = 0;
185e2d15004SDag-Erling Smørgrav 
186e2d15004SDag-Erling Smørgrav 	d->stored_data = memdup(data, data_len);
187e2d15004SDag-Erling Smørgrav 	if(!d->stored_data) {
188971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
189e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
190e2d15004SDag-Erling Smørgrav 		return;
191e2d15004SDag-Erling Smørgrav 	}
192e2d15004SDag-Erling Smørgrav 	d->stored_datalen = data_len;
193e2d15004SDag-Erling Smørgrav 	d->stored_key = strdup(key);
194e2d15004SDag-Erling Smørgrav 	if(!d->stored_key) {
195e2d15004SDag-Erling Smørgrav 		free(d->stored_data);
196e2d15004SDag-Erling Smørgrav 		d->stored_data = NULL;
197e2d15004SDag-Erling Smørgrav 		d->stored_datalen = 0;
198971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
199e2d15004SDag-Erling Smørgrav 		return;
200e2d15004SDag-Erling Smørgrav 	}
201971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
202e2d15004SDag-Erling Smørgrav 	/* (key,data) successfully stored */
203e2d15004SDag-Erling Smørgrav }
204e2d15004SDag-Erling Smørgrav 
205e2d15004SDag-Erling Smørgrav /** The testframe backend is for unit tests */
206e2d15004SDag-Erling Smørgrav static struct cachedb_backend testframe_backend = { "testframe",
207e2d15004SDag-Erling Smørgrav 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
208e2d15004SDag-Erling Smørgrav };
209e2d15004SDag-Erling Smørgrav 
210e2d15004SDag-Erling Smørgrav /** find a particular backend from possible backends */
211e2d15004SDag-Erling Smørgrav static struct cachedb_backend*
212e2d15004SDag-Erling Smørgrav cachedb_find_backend(const char* str)
213e2d15004SDag-Erling Smørgrav {
2140fb34990SDag-Erling Smørgrav #ifdef USE_REDIS
2150fb34990SDag-Erling Smørgrav 	if(strcmp(str, redis_backend.name) == 0)
2160fb34990SDag-Erling Smørgrav 		return &redis_backend;
2170fb34990SDag-Erling Smørgrav #endif
218e2d15004SDag-Erling Smørgrav 	if(strcmp(str, testframe_backend.name) == 0)
219e2d15004SDag-Erling Smørgrav 		return &testframe_backend;
220e2d15004SDag-Erling Smørgrav 	/* TODO add more backends here */
221e2d15004SDag-Erling Smørgrav 	return NULL;
222e2d15004SDag-Erling Smørgrav }
223e2d15004SDag-Erling Smørgrav 
224e2d15004SDag-Erling Smørgrav /** apply configuration to cachedb module 'global' state */
225e2d15004SDag-Erling Smørgrav static int
226e2d15004SDag-Erling Smørgrav cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
227e2d15004SDag-Erling Smørgrav {
228971980c3SDag-Erling Smørgrav 	const char* backend_str = cfg->cachedb_backend;
2298f76bb7dSCy Schubert 	if(!backend_str || *backend_str==0)
2308f76bb7dSCy Schubert 		return 1;
231e2d15004SDag-Erling Smørgrav 	cachedb_env->backend = cachedb_find_backend(backend_str);
232e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend) {
233971980c3SDag-Erling Smørgrav 		log_err("cachedb: cannot find backend name '%s'", backend_str);
234c7f4d7adSDag-Erling Smørgrav 		return 0;
235e2d15004SDag-Erling Smørgrav 	}
236971980c3SDag-Erling Smørgrav 
237e2d15004SDag-Erling Smørgrav 	/* TODO see if more configuration needs to be applied or not */
238e2d15004SDag-Erling Smørgrav 	return 1;
239e2d15004SDag-Erling Smørgrav }
240e2d15004SDag-Erling Smørgrav 
241e2d15004SDag-Erling Smørgrav int
242e2d15004SDag-Erling Smørgrav cachedb_init(struct module_env* env, int id)
243e2d15004SDag-Erling Smørgrav {
244e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
245e2d15004SDag-Erling Smørgrav 		sizeof(struct cachedb_env));
246e2d15004SDag-Erling Smørgrav 	if(!cachedb_env) {
247e2d15004SDag-Erling Smørgrav 		log_err("malloc failure");
248e2d15004SDag-Erling Smørgrav 		return 0;
249e2d15004SDag-Erling Smørgrav 	}
250e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = (void*)cachedb_env;
251e2d15004SDag-Erling Smørgrav 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
252e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not apply configuration settings.");
2530eefd307SCy Schubert 		free(cachedb_env);
2540eefd307SCy Schubert 		env->modinfo[id] = NULL;
255e2d15004SDag-Erling Smørgrav 		return 0;
256e2d15004SDag-Erling Smørgrav 	}
257e2d15004SDag-Erling Smørgrav 	/* see if a backend is selected */
258e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend || !cachedb_env->backend->name)
259e2d15004SDag-Erling Smørgrav 		return 1;
260e2d15004SDag-Erling Smørgrav 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
261e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not init %s backend",
262e2d15004SDag-Erling Smørgrav 			cachedb_env->backend->name);
2630eefd307SCy Schubert 		free(cachedb_env);
2640eefd307SCy Schubert 		env->modinfo[id] = NULL;
265e2d15004SDag-Erling Smørgrav 		return 0;
266e2d15004SDag-Erling Smørgrav 	}
267e2d15004SDag-Erling Smørgrav 	cachedb_env->enabled = 1;
268*103ba509SCy Schubert 	if(env->cfg->serve_expired && env->cfg->serve_expired_reply_ttl)
269091e9e46SCy Schubert 		log_warn(
270091e9e46SCy Schubert 			"cachedb: serve-expired-reply-ttl is set but not working for data "
271*103ba509SCy Schubert 			"originating from the external cache; 0 TTL is used for those.");
272*103ba509SCy Schubert 	if(env->cfg->serve_expired && env->cfg->serve_expired_client_timeout)
273091e9e46SCy Schubert 		log_warn(
274091e9e46SCy Schubert 			"cachedb: serve-expired-client-timeout is set but not working for "
275091e9e46SCy Schubert 			"data originating from the external cache; expired data are used "
276091e9e46SCy Schubert 			"in the reply without first trying to refresh the data.");
277e2d15004SDag-Erling Smørgrav 	return 1;
278e2d15004SDag-Erling Smørgrav }
279e2d15004SDag-Erling Smørgrav 
280e2d15004SDag-Erling Smørgrav void
281e2d15004SDag-Erling Smørgrav cachedb_deinit(struct module_env* env, int id)
282e2d15004SDag-Erling Smørgrav {
283e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env;
284e2d15004SDag-Erling Smørgrav 	if(!env || !env->modinfo[id])
285e2d15004SDag-Erling Smørgrav 		return;
286e2d15004SDag-Erling Smørgrav 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
287e2d15004SDag-Erling Smørgrav 	if(cachedb_env->enabled) {
288e2d15004SDag-Erling Smørgrav 		(*cachedb_env->backend->deinit)(env, cachedb_env);
289e2d15004SDag-Erling Smørgrav 	}
290e2d15004SDag-Erling Smørgrav 	free(cachedb_env);
291e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = NULL;
292e2d15004SDag-Erling Smørgrav }
293e2d15004SDag-Erling Smørgrav 
294e2d15004SDag-Erling Smørgrav /** new query for cachedb */
295e2d15004SDag-Erling Smørgrav static int
296e2d15004SDag-Erling Smørgrav cachedb_new(struct module_qstate* qstate, int id)
297e2d15004SDag-Erling Smørgrav {
298e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
299e2d15004SDag-Erling Smørgrav 		qstate->region, sizeof(struct cachedb_qstate));
300e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = iq;
301e2d15004SDag-Erling Smørgrav 	if(!iq)
302e2d15004SDag-Erling Smørgrav 		return 0;
303e2d15004SDag-Erling Smørgrav 	memset(iq, 0, sizeof(*iq));
304e2d15004SDag-Erling Smørgrav 	/* initialise it */
305e2d15004SDag-Erling Smørgrav 	/* TODO */
306e2d15004SDag-Erling Smørgrav 
307e2d15004SDag-Erling Smørgrav 	return 1;
308e2d15004SDag-Erling Smørgrav }
309e2d15004SDag-Erling Smørgrav 
310e2d15004SDag-Erling Smørgrav /**
311e2d15004SDag-Erling Smørgrav  * Return an error
312e2d15004SDag-Erling Smørgrav  * @param qstate: our query state
313e2d15004SDag-Erling Smørgrav  * @param id: module id
314e2d15004SDag-Erling Smørgrav  * @param rcode: error code (DNS errcode).
315e2d15004SDag-Erling Smørgrav  * @return: 0 for use by caller, to make notation easy, like:
316e2d15004SDag-Erling Smørgrav  * 	return error_response(..).
317e2d15004SDag-Erling Smørgrav  */
318e2d15004SDag-Erling Smørgrav static int
319e2d15004SDag-Erling Smørgrav error_response(struct module_qstate* qstate, int id, int rcode)
320e2d15004SDag-Erling Smørgrav {
321e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "return error response %s",
322e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)?
323e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
324e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = rcode;
325e2d15004SDag-Erling Smørgrav 	qstate->return_msg = NULL;
326e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
327e2d15004SDag-Erling Smørgrav 	return 0;
328e2d15004SDag-Erling Smørgrav }
329e2d15004SDag-Erling Smørgrav 
330e2d15004SDag-Erling Smørgrav /**
331e2d15004SDag-Erling Smørgrav  * Hash the query name, type, class and dbacess-secret into lookup buffer.
332e2d15004SDag-Erling Smørgrav  * @param qstate: query state with query info
333e2d15004SDag-Erling Smørgrav  * 	and env->cfg with secret.
334e2d15004SDag-Erling Smørgrav  * @param buf: returned buffer with hash to lookup
335e2d15004SDag-Erling Smørgrav  * @param len: length of the buffer.
336e2d15004SDag-Erling Smørgrav  */
337e2d15004SDag-Erling Smørgrav static void
338e2d15004SDag-Erling Smørgrav calc_hash(struct module_qstate* qstate, char* buf, size_t len)
339e2d15004SDag-Erling Smørgrav {
340e2d15004SDag-Erling Smørgrav 	uint8_t clear[1024];
341e2d15004SDag-Erling Smørgrav 	size_t clen = 0;
342e2d15004SDag-Erling Smørgrav 	uint8_t hash[CACHEDB_HASHSIZE/8];
343e2d15004SDag-Erling Smørgrav 	const char* hex = "0123456789ABCDEF";
344091e9e46SCy Schubert 	const char* secret = qstate->env->cfg->cachedb_secret;
345e2d15004SDag-Erling Smørgrav 	size_t i;
346e2d15004SDag-Erling Smørgrav 
347e2d15004SDag-Erling Smørgrav 	/* copy the hash info into the clear buffer */
348e2d15004SDag-Erling Smørgrav 	if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
349e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, qstate->qinfo.qname,
350e2d15004SDag-Erling Smørgrav 			qstate->qinfo.qname_len);
351e2d15004SDag-Erling Smørgrav 		clen += qstate->qinfo.qname_len;
352e2d15004SDag-Erling Smørgrav 	}
353e2d15004SDag-Erling Smørgrav 	if(clen + 4 < sizeof(clear)) {
354e2d15004SDag-Erling Smørgrav 		uint16_t t = htons(qstate->qinfo.qtype);
355e2d15004SDag-Erling Smørgrav 		uint16_t c = htons(qstate->qinfo.qclass);
356e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, &t, 2);
357e2d15004SDag-Erling Smørgrav 		memmove(clear+clen+2, &c, 2);
358e2d15004SDag-Erling Smørgrav 		clen += 4;
359e2d15004SDag-Erling Smørgrav 	}
360e2d15004SDag-Erling Smørgrav 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
361e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, secret, strlen(secret));
362e2d15004SDag-Erling Smørgrav 		clen += strlen(secret);
363e2d15004SDag-Erling Smørgrav 	}
364e2d15004SDag-Erling Smørgrav 
365e2d15004SDag-Erling Smørgrav 	/* hash the buffer */
366e2d15004SDag-Erling Smørgrav 	secalgo_hash_sha256(clear, clen, hash);
3670eefd307SCy Schubert #ifdef HAVE_EXPLICIT_BZERO
3680eefd307SCy Schubert 	explicit_bzero(clear, clen);
3690eefd307SCy Schubert #else
370e2d15004SDag-Erling Smørgrav 	memset(clear, 0, clen);
3710eefd307SCy Schubert #endif
372e2d15004SDag-Erling Smørgrav 
373e2d15004SDag-Erling Smørgrav 	/* hex encode output for portability (some online dbs need
374e2d15004SDag-Erling Smørgrav 	 * no nulls, no control characters, and so on) */
375e2d15004SDag-Erling Smørgrav 	log_assert(len >= sizeof(hash)*2 + 1);
376e2d15004SDag-Erling Smørgrav 	(void)len;
377e2d15004SDag-Erling Smørgrav 	for(i=0; i<sizeof(hash); i++) {
378e2d15004SDag-Erling Smørgrav 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
379e2d15004SDag-Erling Smørgrav 		buf[i*2+1] = hex[hash[i]&0x0f];
380e2d15004SDag-Erling Smørgrav 	}
381e2d15004SDag-Erling Smørgrav 	buf[sizeof(hash)*2] = 0;
382e2d15004SDag-Erling Smørgrav }
383e2d15004SDag-Erling Smørgrav 
384e2d15004SDag-Erling Smørgrav /** convert data from return_msg into the data buffer */
385e2d15004SDag-Erling Smørgrav static int
386e2d15004SDag-Erling Smørgrav prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
387e2d15004SDag-Erling Smørgrav {
388e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
389e2d15004SDag-Erling Smørgrav 	size_t oldlim;
390e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
391e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
392e2d15004SDag-Erling Smørgrav 	edns.edns_present = 1;
393e2d15004SDag-Erling Smørgrav 	edns.bits = EDNS_DO;
394e2d15004SDag-Erling Smørgrav 	edns.ext_rcode = 0;
395e2d15004SDag-Erling Smørgrav 	edns.edns_version = EDNS_ADVERTISED_VERSION;
396e2d15004SDag-Erling Smørgrav 	edns.udp_size = EDNS_ADVERTISED_SIZE;
397e2d15004SDag-Erling Smørgrav 
398e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg || !qstate->return_msg->rep)
399e2d15004SDag-Erling Smørgrav 		return 0;
4001838dec3SCy Schubert 	/* do not store failures like SERVFAIL in the cachedb, this avoids
4011838dec3SCy Schubert 	 * overwriting expired, valid, content with broken content. */
4021838dec3SCy Schubert 	if(FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
4031838dec3SCy Schubert 		LDNS_RCODE_NOERROR &&
4041838dec3SCy Schubert 	   FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
4051838dec3SCy Schubert 		LDNS_RCODE_NXDOMAIN &&
4061838dec3SCy Schubert 	   FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
4071838dec3SCy Schubert 		LDNS_RCODE_YXDOMAIN)
4081838dec3SCy Schubert 		return 0;
4098a384985SDag-Erling Smørgrav 	/* We don't store the reply if its TTL is 0 unless serve-expired is
4108a384985SDag-Erling Smørgrav 	 * enabled.  Such a reply won't be reusable and simply be a waste for
4118a384985SDag-Erling Smørgrav 	 * the backend.  It's also compatible with the default behavior of
4128a384985SDag-Erling Smørgrav 	 * dns_cache_store_msg(). */
4138a384985SDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl == 0 &&
4148a384985SDag-Erling Smørgrav 		!qstate->env->cfg->serve_expired)
4158a384985SDag-Erling Smørgrav 		return 0;
4168f76bb7dSCy Schubert 
4178f76bb7dSCy Schubert 	/* The EDE is added to the out-list so it is encoded in the cached message */
4188f76bb7dSCy Schubert 	if (qstate->env->cfg->ede && qstate->return_msg->rep->reason_bogus != LDNS_EDE_NONE) {
4198f76bb7dSCy Schubert 		edns_opt_list_append_ede(&edns.opt_list_out, qstate->env->scratch,
4208f76bb7dSCy Schubert 					qstate->return_msg->rep->reason_bogus,
4218f76bb7dSCy Schubert 					qstate->return_msg->rep->reason_bogus_str);
4228f76bb7dSCy Schubert 	}
4238f76bb7dSCy Schubert 
424e2d15004SDag-Erling Smørgrav 	if(verbosity >= VERB_ALGO)
425e2d15004SDag-Erling Smørgrav 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
426e2d15004SDag-Erling Smørgrav 	                qstate->return_msg->rep);
427e2d15004SDag-Erling Smørgrav 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
428e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->query_flags,
429e2d15004SDag-Erling Smørgrav 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
430e2d15004SDag-Erling Smørgrav 		return 0;
431e2d15004SDag-Erling Smørgrav 
432e2d15004SDag-Erling Smørgrav 	/* TTLs in the return_msg are relative to time(0) so we have to
433e2d15004SDag-Erling Smørgrav 	 * store that, we also store the smallest ttl in the packet+time(0)
434e2d15004SDag-Erling Smørgrav 	 * as the packet expiry time */
435e2d15004SDag-Erling Smørgrav 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
436e2d15004SDag-Erling Smørgrav 	timestamp = (uint64_t)*qstate->env->now;
437e2d15004SDag-Erling Smørgrav 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
438e2d15004SDag-Erling Smørgrav 	timestamp = htobe64(timestamp);
439e2d15004SDag-Erling Smørgrav 	expiry = htobe64(expiry);
440e2d15004SDag-Erling Smørgrav 	oldlim = sldns_buffer_limit(buf);
441e2d15004SDag-Erling Smørgrav 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
442e2d15004SDag-Erling Smørgrav 		sldns_buffer_capacity(buf))
443e2d15004SDag-Erling Smørgrav 		return 0; /* doesn't fit. */
444e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
445e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
446e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
447e2d15004SDag-Erling Smørgrav 		sizeof(expiry));
448e2d15004SDag-Erling Smørgrav 
449e2d15004SDag-Erling Smørgrav 	return 1;
450e2d15004SDag-Erling Smørgrav }
451e2d15004SDag-Erling Smørgrav 
452e2d15004SDag-Erling Smørgrav /** check expiry, return true if matches OK */
453e2d15004SDag-Erling Smørgrav static int
454e2d15004SDag-Erling Smørgrav good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
455e2d15004SDag-Erling Smørgrav {
456e2d15004SDag-Erling Smørgrav 	uint64_t expiry;
457e2d15004SDag-Erling Smørgrav 	/* the expiry time is the last bytes of the buffer */
458e2d15004SDag-Erling Smørgrav 	if(sldns_buffer_limit(buf) < sizeof(expiry))
459e2d15004SDag-Erling Smørgrav 		return 0;
460e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
461e2d15004SDag-Erling Smørgrav 		&expiry, sizeof(expiry));
462e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
463e2d15004SDag-Erling Smørgrav 
464091e9e46SCy Schubert 	/* Check if we are allowed to return expired entries:
465091e9e46SCy Schubert 	 * - serve_expired needs to be set
466091e9e46SCy Schubert 	 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
467091e9e46SCy Schubert 	 *   than that. */
4688a384985SDag-Erling Smørgrav 	if((time_t)expiry < *qstate->env->now &&
469091e9e46SCy Schubert 		(!qstate->env->cfg->serve_expired ||
470091e9e46SCy Schubert 			(SERVE_EXPIRED_TTL &&
471091e9e46SCy Schubert 			*qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
472e2d15004SDag-Erling Smørgrav 		return 0;
473e2d15004SDag-Erling Smørgrav 
474e2d15004SDag-Erling Smørgrav 	return 1;
475e2d15004SDag-Erling Smørgrav }
476e2d15004SDag-Erling Smørgrav 
4778a384985SDag-Erling Smørgrav /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
4788a384985SDag-Erling Smørgrav  * negative, set the TTL to 0. */
479c7f4d7adSDag-Erling Smørgrav static void
480c7f4d7adSDag-Erling Smørgrav packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
481c7f4d7adSDag-Erling Smørgrav {
482c7f4d7adSDag-Erling Smørgrav 	size_t i;
483c7f4d7adSDag-Erling Smørgrav 	size_t total = data->count + data->rrsig_count;
4848a384985SDag-Erling Smørgrav 	if(subtract >= 0 && data->ttl > subtract)
485c7f4d7adSDag-Erling Smørgrav 		data->ttl -= subtract;
486c7f4d7adSDag-Erling Smørgrav 	else	data->ttl = 0;
487c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<total; i++) {
4888a384985SDag-Erling Smørgrav 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
489c7f4d7adSDag-Erling Smørgrav 			data->rr_ttl[i] -= subtract;
490c7f4d7adSDag-Erling Smørgrav 		else	data->rr_ttl[i] = 0;
491c7f4d7adSDag-Erling Smørgrav 	}
492f44e67d1SCy Schubert 	data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0;
493c7f4d7adSDag-Erling Smørgrav }
494c7f4d7adSDag-Erling Smørgrav 
4958a384985SDag-Erling Smørgrav /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
4968a384985SDag-Erling Smørgrav  * negative, set the TTLs to 0. */
497c7f4d7adSDag-Erling Smørgrav static void
498c7f4d7adSDag-Erling Smørgrav adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
499c7f4d7adSDag-Erling Smørgrav {
500c7f4d7adSDag-Erling Smørgrav 	size_t i;
5018a384985SDag-Erling Smørgrav 	if(adjust >= 0 && msg->rep->ttl > adjust)
502c7f4d7adSDag-Erling Smørgrav 		msg->rep->ttl -= adjust;
503091e9e46SCy Schubert 	else
504091e9e46SCy Schubert 		msg->rep->ttl = 0;
505c7f4d7adSDag-Erling Smørgrav 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
5064c75e3aaSDag-Erling Smørgrav 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
507c7f4d7adSDag-Erling Smørgrav 
508c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<msg->rep->rrset_count; i++) {
509c7f4d7adSDag-Erling Smørgrav 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
510c7f4d7adSDag-Erling Smørgrav 			rep->rrsets[i]->entry.data, adjust);
511c7f4d7adSDag-Erling Smørgrav 	}
512c7f4d7adSDag-Erling Smørgrav }
513c7f4d7adSDag-Erling Smørgrav 
514e2d15004SDag-Erling Smørgrav /** convert dns message in buffer to return_msg */
515e2d15004SDag-Erling Smørgrav static int
516e2d15004SDag-Erling Smørgrav parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
517e2d15004SDag-Erling Smørgrav {
518e2d15004SDag-Erling Smørgrav 	struct msg_parse* prs;
519e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
5208f76bb7dSCy Schubert 	struct edns_option* ede;
521e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
522e2d15004SDag-Erling Smørgrav 	time_t adjust;
523e2d15004SDag-Erling Smørgrav 	size_t lim = sldns_buffer_limit(buf);
524e2d15004SDag-Erling Smørgrav 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
525e2d15004SDag-Erling Smørgrav 		return 0; /* too short */
526e2d15004SDag-Erling Smørgrav 
527e2d15004SDag-Erling Smørgrav 	/* remove timestamp and expiry from end */
528e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
529e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
530e2d15004SDag-Erling Smørgrav 		&timestamp, sizeof(timestamp));
531e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
532e2d15004SDag-Erling Smørgrav 	timestamp = be64toh(timestamp);
533e2d15004SDag-Erling Smørgrav 
534e2d15004SDag-Erling Smørgrav 	/* parse DNS packet */
535e2d15004SDag-Erling Smørgrav 	regional_free_all(qstate->env->scratch);
536e2d15004SDag-Erling Smørgrav 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
537e2d15004SDag-Erling Smørgrav 		sizeof(struct msg_parse));
538e2d15004SDag-Erling Smørgrav 	if(!prs)
539e2d15004SDag-Erling Smørgrav 		return 0; /* out of memory */
540e2d15004SDag-Erling Smørgrav 	memset(prs, 0, sizeof(*prs));
541e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
542e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
543e2d15004SDag-Erling Smørgrav 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
544e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
545e2d15004SDag-Erling Smørgrav 		return 0;
546e2d15004SDag-Erling Smørgrav 	}
54724e36522SCy Schubert 	if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) !=
548e2d15004SDag-Erling Smørgrav 		LDNS_RCODE_NOERROR) {
549e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
550e2d15004SDag-Erling Smørgrav 		return 0;
551e2d15004SDag-Erling Smørgrav 	}
552e2d15004SDag-Erling Smørgrav 
553e2d15004SDag-Erling Smørgrav 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
554e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim);
555e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
556e2d15004SDag-Erling Smørgrav 		return 0;
557e2d15004SDag-Erling Smørgrav 
5588f76bb7dSCy Schubert 	/* We find the EDE in the in-list after parsing */
5598f76bb7dSCy Schubert 	if(qstate->env->cfg->ede &&
5608f76bb7dSCy Schubert 		(ede = edns_opt_list_find(edns.opt_list_in, LDNS_EDNS_EDE))) {
5618f76bb7dSCy Schubert 		if(ede->opt_len >= 2) {
5628f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus =
5638f76bb7dSCy Schubert 				sldns_read_uint16(ede->opt_data);
5648f76bb7dSCy Schubert 		}
5658f76bb7dSCy Schubert 		/* allocate space and store the error string and it's size */
5668f76bb7dSCy Schubert 		if(ede->opt_len > 2) {
5678f76bb7dSCy Schubert 			size_t ede_len = ede->opt_len - 2;
5688f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus_str = regional_alloc(
5698f76bb7dSCy Schubert 				qstate->region, sizeof(char) * (ede_len+1));
5708f76bb7dSCy Schubert 			memcpy(qstate->return_msg->rep->reason_bogus_str,
5718f76bb7dSCy Schubert 				ede->opt_data+2, ede_len);
5728f76bb7dSCy Schubert 			qstate->return_msg->rep->reason_bogus_str[ede_len] = 0;
5738f76bb7dSCy Schubert 		}
5748f76bb7dSCy Schubert 	}
5758f76bb7dSCy Schubert 
576e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
577e2d15004SDag-Erling Smørgrav 
578e2d15004SDag-Erling Smørgrav 	/* see how much of the TTL expired, and remove it */
579c7f4d7adSDag-Erling Smørgrav 	if(*qstate->env->now <= (time_t)timestamp) {
580c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
581c7f4d7adSDag-Erling Smørgrav 		return 1; /* message from the future (clock skew?) */
582e2d15004SDag-Erling Smørgrav 	}
583c7f4d7adSDag-Erling Smørgrav 	adjust = *qstate->env->now - (time_t)timestamp;
584c7f4d7adSDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl < adjust) {
585c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg expired");
5868a384985SDag-Erling Smørgrav 		/* If serve-expired is enabled, we still use an expired message
5878a384985SDag-Erling Smørgrav 		 * setting the TTL to 0. */
5881838dec3SCy Schubert 		if(!qstate->env->cfg->serve_expired ||
5891838dec3SCy Schubert 			(FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
5901838dec3SCy Schubert 			!= LDNS_RCODE_NOERROR &&
5911838dec3SCy Schubert 			FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
5921838dec3SCy Schubert 			!= LDNS_RCODE_NXDOMAIN &&
5931838dec3SCy Schubert 			FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
5941838dec3SCy Schubert 			!= LDNS_RCODE_YXDOMAIN))
595c7f4d7adSDag-Erling Smørgrav 			return 0; /* message expired */
5961838dec3SCy Schubert 		else
5971838dec3SCy Schubert 			adjust = -1;
598c7f4d7adSDag-Erling Smørgrav 	}
599c7f4d7adSDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
600c7f4d7adSDag-Erling Smørgrav 	adjust_msg_ttl(qstate->return_msg, adjust);
6018a384985SDag-Erling Smørgrav 
6028a384985SDag-Erling Smørgrav 	/* Similar to the unbound worker, if serve-expired is enabled and
6038a384985SDag-Erling Smørgrav 	 * the msg would be considered to be expired, mark the state so a
6048a384985SDag-Erling Smørgrav 	 * refetch will be scheduled.  The comparison between 'expiry' and
6058a384985SDag-Erling Smørgrav 	 * 'now' should be redundant given how these values were calculated,
6068a384985SDag-Erling Smørgrav 	 * but we check it just in case as does good_expiry_and_qinfo(). */
6078a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired &&
6088a384985SDag-Erling Smørgrav 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
6098a384985SDag-Erling Smørgrav 		qstate->need_refetch = 1;
6108a384985SDag-Erling Smørgrav 	}
6118a384985SDag-Erling Smørgrav 
612c7f4d7adSDag-Erling Smørgrav 	return 1;
613e2d15004SDag-Erling Smørgrav }
614e2d15004SDag-Erling Smørgrav 
615e2d15004SDag-Erling Smørgrav /**
616e2d15004SDag-Erling Smørgrav  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
617e2d15004SDag-Erling Smørgrav  * return true if lookup was successful.
618e2d15004SDag-Erling Smørgrav  */
619e2d15004SDag-Erling Smørgrav static int
620e2d15004SDag-Erling Smørgrav cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
621e2d15004SDag-Erling Smørgrav {
622e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
623e2d15004SDag-Erling Smørgrav 	calc_hash(qstate, key, sizeof(key));
624e2d15004SDag-Erling Smørgrav 
625e2d15004SDag-Erling Smørgrav 	/* call backend to fetch data for key into scratch buffer */
626e2d15004SDag-Erling Smørgrav 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
627e2d15004SDag-Erling Smørgrav 		qstate->env->scratch_buffer)) {
628e2d15004SDag-Erling Smørgrav 		return 0;
629e2d15004SDag-Erling Smørgrav 	}
630e2d15004SDag-Erling Smørgrav 
631e2d15004SDag-Erling Smørgrav 	/* check expiry date and check if query-data matches */
632e2d15004SDag-Erling Smørgrav 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
633e2d15004SDag-Erling Smørgrav 		return 0;
634e2d15004SDag-Erling Smørgrav 	}
635e2d15004SDag-Erling Smørgrav 
636e2d15004SDag-Erling Smørgrav 	/* parse dns message into return_msg */
637e2d15004SDag-Erling Smørgrav 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
638e2d15004SDag-Erling Smørgrav 		return 0;
639e2d15004SDag-Erling Smørgrav 	}
640e2d15004SDag-Erling Smørgrav 	return 1;
641e2d15004SDag-Erling Smørgrav }
642e2d15004SDag-Erling Smørgrav 
643e2d15004SDag-Erling Smørgrav /**
644e2d15004SDag-Erling Smørgrav  * Store the qstate.return_msg in extcache for key qstate.info
645e2d15004SDag-Erling Smørgrav  */
646e2d15004SDag-Erling Smørgrav static void
647e2d15004SDag-Erling Smørgrav cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
648e2d15004SDag-Erling Smørgrav {
649e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
650e2d15004SDag-Erling Smørgrav 	calc_hash(qstate, key, sizeof(key));
651e2d15004SDag-Erling Smørgrav 
652e2d15004SDag-Erling Smørgrav 	/* prepare data in scratch buffer */
653e2d15004SDag-Erling Smørgrav 	if(!prep_data(qstate, qstate->env->scratch_buffer))
654e2d15004SDag-Erling Smørgrav 		return;
655e2d15004SDag-Erling Smørgrav 
656e2d15004SDag-Erling Smørgrav 	/* call backend */
657e2d15004SDag-Erling Smørgrav 	(*ie->backend->store)(qstate->env, ie, key,
658e2d15004SDag-Erling Smørgrav 		sldns_buffer_begin(qstate->env->scratch_buffer),
65925039b37SCy Schubert 		sldns_buffer_limit(qstate->env->scratch_buffer),
66025039b37SCy Schubert 		qstate->return_msg->rep->ttl);
661e2d15004SDag-Erling Smørgrav }
662e2d15004SDag-Erling Smørgrav 
663e2d15004SDag-Erling Smørgrav /**
664e2d15004SDag-Erling Smørgrav  * See if unbound's internal cache can answer the query
665e2d15004SDag-Erling Smørgrav  */
666e2d15004SDag-Erling Smørgrav static int
6678f76bb7dSCy Schubert cachedb_intcache_lookup(struct module_qstate* qstate, struct cachedb_env* cde)
668e2d15004SDag-Erling Smørgrav {
6695469a995SCy Schubert 	uint8_t* dpname=NULL;
6705469a995SCy Schubert 	size_t dpnamelen=0;
671e2d15004SDag-Erling Smørgrav 	struct dns_msg* msg;
6728f76bb7dSCy Schubert 	/* for testframe bypass this lookup */
6738f76bb7dSCy Schubert 	if(cde->backend == &testframe_backend) {
6748f76bb7dSCy Schubert 		return 0;
6758f76bb7dSCy Schubert 	}
6765469a995SCy Schubert 	if(iter_stub_fwd_no_cache(qstate, &qstate->qinfo,
6775469a995SCy Schubert 		&dpname, &dpnamelen))
6785469a995SCy Schubert 		return 0; /* no cache for these queries */
679e2d15004SDag-Erling Smørgrav 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
680e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
681e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qclass, qstate->query_flags,
68257bddd21SDag-Erling Smørgrav 		qstate->region, qstate->env->scratch,
6835469a995SCy Schubert 		1, /* no partial messages with only a CNAME */
6845469a995SCy Schubert 		dpname, dpnamelen
68557bddd21SDag-Erling Smørgrav 		);
6860fb34990SDag-Erling Smørgrav 	if(!msg && qstate->env->neg_cache &&
6870fb34990SDag-Erling Smørgrav 		iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
688e2d15004SDag-Erling Smørgrav 		/* lookup in negative cache; may result in
689e2d15004SDag-Erling Smørgrav 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
690e2d15004SDag-Erling Smørgrav 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
691e2d15004SDag-Erling Smørgrav 			qstate->region, qstate->env->rrset_cache,
692e2d15004SDag-Erling Smørgrav 			qstate->env->scratch_buffer,
69357bddd21SDag-Erling Smørgrav 			*qstate->env->now, 1/*add SOA*/, NULL,
69457bddd21SDag-Erling Smørgrav 			qstate->env->cfg);
695e2d15004SDag-Erling Smørgrav 	}
696e2d15004SDag-Erling Smørgrav 	if(!msg)
697e2d15004SDag-Erling Smørgrav 		return 0;
698e2d15004SDag-Erling Smørgrav 	/* this is the returned msg */
699e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
700e2d15004SDag-Erling Smørgrav 	qstate->return_msg = msg;
701e2d15004SDag-Erling Smørgrav 	return 1;
702e2d15004SDag-Erling Smørgrav }
703e2d15004SDag-Erling Smørgrav 
704e2d15004SDag-Erling Smørgrav /**
705e2d15004SDag-Erling Smørgrav  * Store query into the internal cache of unbound.
706e2d15004SDag-Erling Smørgrav  */
707e2d15004SDag-Erling Smørgrav static void
708e2d15004SDag-Erling Smørgrav cachedb_intcache_store(struct module_qstate* qstate)
709e2d15004SDag-Erling Smørgrav {
7108a384985SDag-Erling Smørgrav 	uint32_t store_flags = qstate->query_flags;
7118a384985SDag-Erling Smørgrav 
7128a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired)
7138a384985SDag-Erling Smørgrav 		store_flags |= DNSCACHE_STORE_ZEROTTL;
714e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
715e2d15004SDag-Erling Smørgrav 		return;
716e2d15004SDag-Erling Smørgrav 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
717e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
718790c6b24SCy Schubert 		qstate->region, store_flags, qstate->qstarttime);
719e2d15004SDag-Erling Smørgrav }
720e2d15004SDag-Erling Smørgrav 
721e2d15004SDag-Erling Smørgrav /**
722e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a query
723e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
724e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
725e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
726e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
727e2d15004SDag-Erling Smørgrav  * @param id: module id.
728e2d15004SDag-Erling Smørgrav  */
729e2d15004SDag-Erling Smørgrav static void
730e2d15004SDag-Erling Smørgrav cachedb_handle_query(struct module_qstate* qstate,
731e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq),
732e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie, int id)
733e2d15004SDag-Erling Smørgrav {
7348f76bb7dSCy Schubert 	qstate->is_cachedb_answer = 0;
735e2d15004SDag-Erling Smørgrav 	/* check if we are enabled, and skip if so */
736e2d15004SDag-Erling Smørgrav 	if(!ie->enabled) {
737e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
738e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
739e2d15004SDag-Erling Smørgrav 		return;
740e2d15004SDag-Erling Smørgrav 	}
741e2d15004SDag-Erling Smørgrav 
742bc892140SDag-Erling Smørgrav 	if(qstate->blacklist || qstate->no_cache_lookup) {
743bc892140SDag-Erling Smørgrav 		/* cache is blacklisted or we are instructed from edns to not look */
744e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
745e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
746e2d15004SDag-Erling Smørgrav 		return;
747e2d15004SDag-Erling Smørgrav 	}
748e2d15004SDag-Erling Smørgrav 
749091e9e46SCy Schubert 	/* lookup inside unbound's internal cache.
750091e9e46SCy Schubert 	 * This does not look for expired entries. */
7518f76bb7dSCy Schubert 	if(cachedb_intcache_lookup(qstate, ie)) {
752bc892140SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO) {
753bc892140SDag-Erling Smørgrav 			if(qstate->return_msg->rep)
754e2d15004SDag-Erling Smørgrav 				log_dns_msg("cachedb internal cache lookup",
755e2d15004SDag-Erling Smørgrav 					&qstate->return_msg->qinfo,
756e2d15004SDag-Erling Smørgrav 					qstate->return_msg->rep);
757bc892140SDag-Erling Smørgrav 			else log_info("cachedb internal cache lookup: rcode %s",
758091e9e46SCy Schubert 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
759091e9e46SCy Schubert 				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
760091e9e46SCy Schubert 				:"??");
761bc892140SDag-Erling Smørgrav 		}
762e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
763e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
764e2d15004SDag-Erling Smørgrav 		return;
765e2d15004SDag-Erling Smørgrav 	}
766e2d15004SDag-Erling Smørgrav 
767e2d15004SDag-Erling Smørgrav 	/* ask backend cache to see if we have data */
768e2d15004SDag-Erling Smørgrav 	if(cachedb_extcache_lookup(qstate, ie)) {
769e2d15004SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO)
770e2d15004SDag-Erling Smørgrav 			log_dns_msg(ie->backend->name,
771e2d15004SDag-Erling Smørgrav 				&qstate->return_msg->qinfo,
772e2d15004SDag-Erling Smørgrav 				qstate->return_msg->rep);
773e2d15004SDag-Erling Smørgrav 		/* store this result in internal cache */
774e2d15004SDag-Erling Smørgrav 		cachedb_intcache_store(qstate);
775091e9e46SCy Schubert 		/* In case we have expired data but there is a client timer for expired
776091e9e46SCy Schubert 		 * answers, pass execution to next module in order to try updating the
777091e9e46SCy Schubert 		 * data first.
778091e9e46SCy Schubert 		 * TODO: this needs revisit. The expired data stored from cachedb has
779091e9e46SCy Schubert 		 * 0 TTL which is picked up by iterator later when looking in the cache.
780091e9e46SCy Schubert 		 * Document that ext cachedb does not work properly with
781091e9e46SCy Schubert 		 * serve_stale_reply_ttl yet. */
782091e9e46SCy Schubert 		if(qstate->need_refetch && qstate->serve_expired_data &&
783091e9e46SCy Schubert 			qstate->serve_expired_data->timer) {
784091e9e46SCy Schubert 				qstate->return_msg = NULL;
785091e9e46SCy Schubert 				qstate->ext_state[id] = module_wait_module;
786091e9e46SCy Schubert 				return;
787091e9e46SCy Schubert 		}
7888f76bb7dSCy Schubert 		qstate->is_cachedb_answer = 1;
789e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
790e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
791e2d15004SDag-Erling Smørgrav 		return;
792e2d15004SDag-Erling Smørgrav 	}
793e2d15004SDag-Erling Smørgrav 
794e2d15004SDag-Erling Smørgrav 	/* no cache fetches */
795e2d15004SDag-Erling Smørgrav 	/* pass request to next module */
796e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_module;
797e2d15004SDag-Erling Smørgrav }
798e2d15004SDag-Erling Smørgrav 
799e2d15004SDag-Erling Smørgrav /**
800e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a response from the iterator.
801e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
802e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
803e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
804e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
805e2d15004SDag-Erling Smørgrav  * @param id: module id.
806e2d15004SDag-Erling Smørgrav  */
807e2d15004SDag-Erling Smørgrav static void
808e2d15004SDag-Erling Smørgrav cachedb_handle_response(struct module_qstate* qstate,
809e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
810e2d15004SDag-Erling Smørgrav {
8118f76bb7dSCy Schubert 	qstate->is_cachedb_answer = 0;
812bc892140SDag-Erling Smørgrav 	/* check if we are not enabled or instructed to not cache, and skip */
813bc892140SDag-Erling Smørgrav 	if(!ie->enabled || qstate->no_cache_store) {
814e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
815e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
816e2d15004SDag-Erling Smørgrav 		return;
817e2d15004SDag-Erling Smørgrav 	}
818*103ba509SCy Schubert 	if(qstate->env->cfg->cachedb_no_store) {
819*103ba509SCy Schubert 		/* do not store the item in the external cache */
820*103ba509SCy Schubert 		qstate->ext_state[id] = module_finished;
821*103ba509SCy Schubert 		return;
822*103ba509SCy Schubert 	}
823e2d15004SDag-Erling Smørgrav 
824e2d15004SDag-Erling Smørgrav 	/* store the item into the backend cache */
825e2d15004SDag-Erling Smørgrav 	cachedb_extcache_store(qstate, ie);
826e2d15004SDag-Erling Smørgrav 
827e2d15004SDag-Erling Smørgrav 	/* we are done with the query */
828e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
829e2d15004SDag-Erling Smørgrav }
830e2d15004SDag-Erling Smørgrav 
831e2d15004SDag-Erling Smørgrav void
832e2d15004SDag-Erling Smørgrav cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
833e2d15004SDag-Erling Smørgrav 	struct outbound_entry* outbound)
834e2d15004SDag-Erling Smørgrav {
835e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
836e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
837e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
838e2d15004SDag-Erling Smørgrav 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
839e2d15004SDag-Erling Smørgrav 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
840e2d15004SDag-Erling Smørgrav 		&qstate->qinfo);
841e2d15004SDag-Erling Smørgrav 
842e2d15004SDag-Erling Smørgrav 	/* perform cachedb state machine */
843e2d15004SDag-Erling Smørgrav 	if((event == module_event_new || event == module_event_pass) &&
844e2d15004SDag-Erling Smørgrav 		iq == NULL) {
845e2d15004SDag-Erling Smørgrav 		if(!cachedb_new(qstate, id)) {
846e2d15004SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
847e2d15004SDag-Erling Smørgrav 			return;
848e2d15004SDag-Erling Smørgrav 		}
849e2d15004SDag-Erling Smørgrav 		iq = (struct cachedb_qstate*)qstate->minfo[id];
850e2d15004SDag-Erling Smørgrav 	}
851e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_pass || event == module_event_new)) {
852e2d15004SDag-Erling Smørgrav 		cachedb_handle_query(qstate, iq, ie, id);
853e2d15004SDag-Erling Smørgrav 		return;
854e2d15004SDag-Erling Smørgrav 	}
855e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_moddone)) {
856e2d15004SDag-Erling Smørgrav 		cachedb_handle_response(qstate, iq, ie, id);
857e2d15004SDag-Erling Smørgrav 		return;
858e2d15004SDag-Erling Smørgrav 	}
859e2d15004SDag-Erling Smørgrav 	if(iq && outbound) {
860e2d15004SDag-Erling Smørgrav 		/* cachedb does not need to process responses at this time
861e2d15004SDag-Erling Smørgrav 		 * ignore it.
862e2d15004SDag-Erling Smørgrav 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
863e2d15004SDag-Erling Smørgrav 		*/
864e2d15004SDag-Erling Smørgrav 		return;
865e2d15004SDag-Erling Smørgrav 	}
866e2d15004SDag-Erling Smørgrav 	if(event == module_event_error) {
867e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "got called with event error, giving up");
868e2d15004SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
869e2d15004SDag-Erling Smørgrav 		return;
870e2d15004SDag-Erling Smørgrav 	}
871bc892140SDag-Erling Smørgrav 	if(!iq && (event == module_event_moddone)) {
872bc892140SDag-Erling Smørgrav 		/* during priming, module done but we never started */
873bc892140SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
874bc892140SDag-Erling Smørgrav 		return;
875bc892140SDag-Erling Smørgrav 	}
876e2d15004SDag-Erling Smørgrav 
877e2d15004SDag-Erling Smørgrav 	log_err("bad event for cachedb");
878e2d15004SDag-Erling Smørgrav 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
879e2d15004SDag-Erling Smørgrav }
880e2d15004SDag-Erling Smørgrav 
881e2d15004SDag-Erling Smørgrav void
882e2d15004SDag-Erling Smørgrav cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
883e2d15004SDag-Erling Smørgrav 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
884e2d15004SDag-Erling Smørgrav {
885e2d15004SDag-Erling Smørgrav 	/* cachedb does not use subordinate requests at this time */
886e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb inform_super was called");
887e2d15004SDag-Erling Smørgrav }
888e2d15004SDag-Erling Smørgrav 
889e2d15004SDag-Erling Smørgrav void
890e2d15004SDag-Erling Smørgrav cachedb_clear(struct module_qstate* qstate, int id)
891e2d15004SDag-Erling Smørgrav {
892e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq;
893e2d15004SDag-Erling Smørgrav 	if(!qstate)
894e2d15004SDag-Erling Smørgrav 		return;
895e2d15004SDag-Erling Smørgrav 	iq = (struct cachedb_qstate*)qstate->minfo[id];
896e2d15004SDag-Erling Smørgrav 	if(iq) {
897e2d15004SDag-Erling Smørgrav 		/* free contents of iq */
898e2d15004SDag-Erling Smørgrav 		/* TODO */
899e2d15004SDag-Erling Smørgrav 	}
900e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = NULL;
901e2d15004SDag-Erling Smørgrav }
902e2d15004SDag-Erling Smørgrav 
903e2d15004SDag-Erling Smørgrav size_t
904e2d15004SDag-Erling Smørgrav cachedb_get_mem(struct module_env* env, int id)
905e2d15004SDag-Erling Smørgrav {
906e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
907e2d15004SDag-Erling Smørgrav 	if(!ie)
908e2d15004SDag-Erling Smørgrav 		return 0;
909e2d15004SDag-Erling Smørgrav 	return sizeof(*ie); /* TODO - more mem */
910e2d15004SDag-Erling Smørgrav }
911e2d15004SDag-Erling Smørgrav 
912e2d15004SDag-Erling Smørgrav /**
913e2d15004SDag-Erling Smørgrav  * The cachedb function block
914e2d15004SDag-Erling Smørgrav  */
915e2d15004SDag-Erling Smørgrav static struct module_func_block cachedb_block = {
916e2d15004SDag-Erling Smørgrav 	"cachedb",
917e2d15004SDag-Erling Smørgrav 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
918e2d15004SDag-Erling Smørgrav 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
919e2d15004SDag-Erling Smørgrav };
920e2d15004SDag-Erling Smørgrav 
921e2d15004SDag-Erling Smørgrav struct module_func_block*
922e2d15004SDag-Erling Smørgrav cachedb_get_funcblock(void)
923e2d15004SDag-Erling Smørgrav {
924e2d15004SDag-Erling Smørgrav 	return &cachedb_block;
925e2d15004SDag-Erling Smørgrav }
926e2d15004SDag-Erling Smørgrav #endif /* USE_CACHEDB */
927