xref: /openbsd-src/usr.sbin/unbound/cachedb/cachedb.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /*
2  * cachedb/cachedb.c - cache from a database external to the program module
3  *
4  * Copyright (c) 2016, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains a module that uses an external database to cache
40  * dns responses.
41  */
42 
43 #include "config.h"
44 #ifdef USE_CACHEDB
45 #include "cachedb/cachedb.h"
46 #include "util/regional.h"
47 #include "util/net_help.h"
48 #include "util/config_file.h"
49 #include "util/data/msgreply.h"
50 #include "util/data/msgencode.h"
51 #include "services/cache/dns.h"
52 #include "validator/val_neg.h"
53 #include "validator/val_secalgo.h"
54 #include "iterator/iter_utils.h"
55 #include "sldns/parseutil.h"
56 #include "sldns/wire2str.h"
57 #include "sldns/sbuffer.h"
58 
59 #define CACHEDB_HASHSIZE 256 /* bit hash */
60 
61 /** the unit test testframe for cachedb, its module state contains
62  * a cache for a couple queries (in memory). */
63 struct testframe_moddata {
64 	/** lock for mutex */
65 	lock_basic_type lock;
66 	/** key for single stored data element, NULL if none */
67 	char* stored_key;
68 	/** data for single stored data element, NULL if none */
69 	uint8_t* stored_data;
70 	/** length of stored data */
71 	size_t stored_datalen;
72 };
73 
74 static int
75 testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
76 {
77 	struct testframe_moddata* d;
78 	(void)env;
79 	verbose(VERB_ALGO, "testframe_init");
80 	d = (struct testframe_moddata*)calloc(1,
81 		sizeof(struct testframe_moddata));
82 	cachedb_env->backend_data = (void*)d;
83 	if(!cachedb_env->backend_data) {
84 		log_err("out of memory");
85 		return 0;
86 	}
87 	lock_basic_init(&d->lock);
88 	lock_protect(&d->lock, d, sizeof(*d));
89 	return 1;
90 }
91 
92 static void
93 testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
94 {
95 	struct testframe_moddata* d = (struct testframe_moddata*)
96 		cachedb_env->backend_data;
97 	(void)env;
98 	verbose(VERB_ALGO, "testframe_deinit");
99 	if(!d)
100 		return;
101 	lock_basic_destroy(&d->lock);
102 	free(d->stored_key);
103 	free(d->stored_data);
104 	free(d);
105 }
106 
107 static int
108 testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
109 	char* key, struct sldns_buffer* result_buffer)
110 {
111 	struct testframe_moddata* d = (struct testframe_moddata*)
112 		cachedb_env->backend_data;
113 	(void)env;
114 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
115 	lock_basic_lock(&d->lock);
116 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
117 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
118 			lock_basic_unlock(&d->lock);
119 			return 0; /* too large */
120 		}
121 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
122 			(int)d->stored_datalen);
123 		sldns_buffer_clear(result_buffer);
124 		sldns_buffer_write(result_buffer, d->stored_data,
125 			d->stored_datalen);
126 		sldns_buffer_flip(result_buffer);
127 		lock_basic_unlock(&d->lock);
128 		return 1;
129 	}
130 	lock_basic_unlock(&d->lock);
131 	return 0;
132 }
133 
134 static void
135 testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
136 	char* key, uint8_t* data, size_t data_len)
137 {
138 	struct testframe_moddata* d = (struct testframe_moddata*)
139 		cachedb_env->backend_data;
140 	(void)env;
141 	lock_basic_lock(&d->lock);
142 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
143 
144 	/* free old data element (if any) */
145 	free(d->stored_key);
146 	d->stored_key = NULL;
147 	free(d->stored_data);
148 	d->stored_data = NULL;
149 	d->stored_datalen = 0;
150 
151 	d->stored_data = memdup(data, data_len);
152 	if(!d->stored_data) {
153 		lock_basic_unlock(&d->lock);
154 		log_err("out of memory");
155 		return;
156 	}
157 	d->stored_datalen = data_len;
158 	d->stored_key = strdup(key);
159 	if(!d->stored_key) {
160 		free(d->stored_data);
161 		d->stored_data = NULL;
162 		d->stored_datalen = 0;
163 		lock_basic_unlock(&d->lock);
164 		return;
165 	}
166 	lock_basic_unlock(&d->lock);
167 	/* (key,data) successfully stored */
168 }
169 
170 /** The testframe backend is for unit tests */
171 static struct cachedb_backend testframe_backend = { "testframe",
172 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
173 };
174 
175 /** find a particular backend from possible backends */
176 static struct cachedb_backend*
177 cachedb_find_backend(const char* str)
178 {
179 	if(strcmp(str, testframe_backend.name) == 0)
180 		return &testframe_backend;
181 	/* TODO add more backends here */
182 	return NULL;
183 }
184 
185 /** apply configuration to cachedb module 'global' state */
186 static int
187 cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
188 {
189 	const char* backend_str = cfg->cachedb_backend;
190 
191 	/* If unspecified we use the in-memory test DB. */
192 	if(!backend_str)
193 		backend_str = "testframe";
194 	cachedb_env->backend = cachedb_find_backend(backend_str);
195 	if(!cachedb_env->backend) {
196 		log_err("cachedb: cannot find backend name '%s'", backend_str);
197 		return 0;
198 	}
199 
200 	/* TODO see if more configuration needs to be applied or not */
201 	return 1;
202 }
203 
204 int
205 cachedb_init(struct module_env* env, int id)
206 {
207 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
208 		sizeof(struct cachedb_env));
209 	if(!cachedb_env) {
210 		log_err("malloc failure");
211 		return 0;
212 	}
213 	env->modinfo[id] = (void*)cachedb_env;
214 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
215 		log_err("cachedb: could not apply configuration settings.");
216 		return 0;
217 	}
218 	/* see if a backend is selected */
219 	if(!cachedb_env->backend || !cachedb_env->backend->name)
220 		return 1;
221 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
222 		log_err("cachedb: could not init %s backend",
223 			cachedb_env->backend->name);
224 		return 0;
225 	}
226 	cachedb_env->enabled = 1;
227 	return 1;
228 }
229 
230 void
231 cachedb_deinit(struct module_env* env, int id)
232 {
233 	struct cachedb_env* cachedb_env;
234 	if(!env || !env->modinfo[id])
235 		return;
236 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
237 	/* free contents */
238 	/* TODO */
239 	if(cachedb_env->enabled) {
240 		(*cachedb_env->backend->deinit)(env, cachedb_env);
241 	}
242 
243 	free(cachedb_env);
244 	env->modinfo[id] = NULL;
245 }
246 
247 /** new query for cachedb */
248 static int
249 cachedb_new(struct module_qstate* qstate, int id)
250 {
251 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
252 		qstate->region, sizeof(struct cachedb_qstate));
253 	qstate->minfo[id] = iq;
254 	if(!iq)
255 		return 0;
256 	memset(iq, 0, sizeof(*iq));
257 	/* initialise it */
258 	/* TODO */
259 
260 	return 1;
261 }
262 
263 /**
264  * Return an error
265  * @param qstate: our query state
266  * @param id: module id
267  * @param rcode: error code (DNS errcode).
268  * @return: 0 for use by caller, to make notation easy, like:
269  * 	return error_response(..).
270  */
271 static int
272 error_response(struct module_qstate* qstate, int id, int rcode)
273 {
274 	verbose(VERB_QUERY, "return error response %s",
275 		sldns_lookup_by_id(sldns_rcodes, rcode)?
276 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
277 	qstate->return_rcode = rcode;
278 	qstate->return_msg = NULL;
279 	qstate->ext_state[id] = module_finished;
280 	return 0;
281 }
282 
283 /**
284  * Hash the query name, type, class and dbacess-secret into lookup buffer.
285  * @param qstate: query state with query info
286  * 	and env->cfg with secret.
287  * @param buf: returned buffer with hash to lookup
288  * @param len: length of the buffer.
289  */
290 static void
291 calc_hash(struct module_qstate* qstate, char* buf, size_t len)
292 {
293 	uint8_t clear[1024];
294 	size_t clen = 0;
295 	uint8_t hash[CACHEDB_HASHSIZE/8];
296 	const char* hex = "0123456789ABCDEF";
297 	const char* secret = qstate->env->cfg->cachedb_secret ?
298 		qstate->env->cfg->cachedb_secret : "default";
299 	size_t i;
300 
301 	/* copy the hash info into the clear buffer */
302 	if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
303 		memmove(clear+clen, qstate->qinfo.qname,
304 			qstate->qinfo.qname_len);
305 		clen += qstate->qinfo.qname_len;
306 	}
307 	if(clen + 4 < sizeof(clear)) {
308 		uint16_t t = htons(qstate->qinfo.qtype);
309 		uint16_t c = htons(qstate->qinfo.qclass);
310 		memmove(clear+clen, &t, 2);
311 		memmove(clear+clen+2, &c, 2);
312 		clen += 4;
313 	}
314 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
315 		memmove(clear+clen, secret, strlen(secret));
316 		clen += strlen(secret);
317 	}
318 
319 	/* hash the buffer */
320 	secalgo_hash_sha256(clear, clen, hash);
321 	memset(clear, 0, clen);
322 
323 	/* hex encode output for portability (some online dbs need
324 	 * no nulls, no control characters, and so on) */
325 	log_assert(len >= sizeof(hash)*2 + 1);
326 	(void)len;
327 	for(i=0; i<sizeof(hash); i++) {
328 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
329 		buf[i*2+1] = hex[hash[i]&0x0f];
330 	}
331 	buf[sizeof(hash)*2] = 0;
332 }
333 
334 /** convert data from return_msg into the data buffer */
335 static int
336 prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
337 {
338 	uint64_t timestamp, expiry;
339 	size_t oldlim;
340 	struct edns_data edns;
341 	memset(&edns, 0, sizeof(edns));
342 	edns.edns_present = 1;
343 	edns.bits = EDNS_DO;
344 	edns.ext_rcode = 0;
345 	edns.edns_version = EDNS_ADVERTISED_VERSION;
346 	edns.udp_size = EDNS_ADVERTISED_SIZE;
347 
348 	if(!qstate->return_msg || !qstate->return_msg->rep)
349 		return 0;
350 	/* We don't store the reply if its TTL is 0 unless serve-expired is
351 	 * enabled.  Such a reply won't be reusable and simply be a waste for
352 	 * the backend.  It's also compatible with the default behavior of
353 	 * dns_cache_store_msg(). */
354 	if(qstate->return_msg->rep->ttl == 0 &&
355 		!qstate->env->cfg->serve_expired)
356 		return 0;
357 	if(verbosity >= VERB_ALGO)
358 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
359 	                qstate->return_msg->rep);
360 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
361 		qstate->return_msg->rep, 0, qstate->query_flags,
362 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
363 		return 0;
364 
365 	/* TTLs in the return_msg are relative to time(0) so we have to
366 	 * store that, we also store the smallest ttl in the packet+time(0)
367 	 * as the packet expiry time */
368 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
369 	timestamp = (uint64_t)*qstate->env->now;
370 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
371 	timestamp = htobe64(timestamp);
372 	expiry = htobe64(expiry);
373 	oldlim = sldns_buffer_limit(buf);
374 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
375 		sldns_buffer_capacity(buf))
376 		return 0; /* doesn't fit. */
377 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
378 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
379 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
380 		sizeof(expiry));
381 
382 	return 1;
383 }
384 
385 /** check expiry, return true if matches OK */
386 static int
387 good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
388 {
389 	uint64_t expiry;
390 	/* the expiry time is the last bytes of the buffer */
391 	if(sldns_buffer_limit(buf) < sizeof(expiry))
392 		return 0;
393 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
394 		&expiry, sizeof(expiry));
395 	expiry = be64toh(expiry);
396 
397 	if((time_t)expiry < *qstate->env->now &&
398 		!qstate->env->cfg->serve_expired)
399 		return 0;
400 
401 	return 1;
402 }
403 
404 /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
405  * negative, set the TTL to 0. */
406 static void
407 packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
408 {
409         size_t i;
410         size_t total = data->count + data->rrsig_count;
411 	if(subtract >= 0 && data->ttl > subtract)
412 		data->ttl -= subtract;
413 	else	data->ttl = 0;
414         for(i=0; i<total; i++) {
415 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
416                 	data->rr_ttl[i] -= subtract;
417                 else	data->rr_ttl[i] = 0;
418 	}
419 }
420 
421 /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
422  * negative, set the TTLs to 0. */
423 static void
424 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
425 {
426 	size_t i;
427 	if(adjust >= 0 && msg->rep->ttl > adjust)
428 		msg->rep->ttl -= adjust;
429 	else	msg->rep->ttl = 0;
430 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
431 
432 	for(i=0; i<msg->rep->rrset_count; i++) {
433 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
434 			rep->rrsets[i]->entry.data, adjust);
435 	}
436 }
437 
438 /** convert dns message in buffer to return_msg */
439 static int
440 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
441 {
442 	struct msg_parse* prs;
443 	struct edns_data edns;
444 	uint64_t timestamp, expiry;
445 	time_t adjust;
446 	size_t lim = sldns_buffer_limit(buf);
447 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
448 		return 0; /* too short */
449 
450 	/* remove timestamp and expiry from end */
451 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
452 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
453 		&timestamp, sizeof(timestamp));
454 	expiry = be64toh(expiry);
455 	timestamp = be64toh(timestamp);
456 
457 	/* parse DNS packet */
458 	regional_free_all(qstate->env->scratch);
459 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
460 		sizeof(struct msg_parse));
461 	if(!prs)
462 		return 0; /* out of memory */
463 	memset(prs, 0, sizeof(*prs));
464 	memset(&edns, 0, sizeof(edns));
465 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
466 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
467 		sldns_buffer_set_limit(buf, lim);
468 		return 0;
469 	}
470 	if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
471 		LDNS_RCODE_NOERROR) {
472 		sldns_buffer_set_limit(buf, lim);
473 		return 0;
474 	}
475 
476 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
477 	sldns_buffer_set_limit(buf, lim);
478 	if(!qstate->return_msg)
479 		return 0;
480 
481 	qstate->return_rcode = LDNS_RCODE_NOERROR;
482 
483 	/* see how much of the TTL expired, and remove it */
484 	if(*qstate->env->now <= (time_t)timestamp) {
485 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
486 		return 1; /* message from the future (clock skew?) */
487 	}
488 	adjust = *qstate->env->now - (time_t)timestamp;
489 	if(qstate->return_msg->rep->ttl < adjust) {
490 		verbose(VERB_ALGO, "cachedb msg expired");
491 		/* If serve-expired is enabled, we still use an expired message
492 		 * setting the TTL to 0. */
493 		if(qstate->env->cfg->serve_expired)
494 			adjust = -1;
495 		else
496 			return 0; /* message expired */
497 	}
498 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
499 	adjust_msg_ttl(qstate->return_msg, adjust);
500 
501 	/* Similar to the unbound worker, if serve-expired is enabled and
502 	 * the msg would be considered to be expired, mark the state so a
503 	 * refetch will be scheduled.  The comparison between 'expiry' and
504 	 * 'now' should be redundant given how these values were calculated,
505 	 * but we check it just in case as does good_expiry_and_qinfo(). */
506 	if(qstate->env->cfg->serve_expired &&
507 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
508 		qstate->need_refetch = 1;
509 	}
510 
511 	return 1;
512 }
513 
514 /**
515  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
516  * return true if lookup was successful.
517  */
518 static int
519 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
520 {
521 	char key[(CACHEDB_HASHSIZE/8)*2+1];
522 	calc_hash(qstate, key, sizeof(key));
523 
524 	/* call backend to fetch data for key into scratch buffer */
525 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
526 		qstate->env->scratch_buffer)) {
527 		return 0;
528 	}
529 
530 	/* check expiry date and check if query-data matches */
531 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
532 		return 0;
533 	}
534 
535 	/* parse dns message into return_msg */
536 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
537 		return 0;
538 	}
539 	return 1;
540 }
541 
542 /**
543  * Store the qstate.return_msg in extcache for key qstate.info
544  */
545 static void
546 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
547 {
548 	char key[(CACHEDB_HASHSIZE/8)*2+1];
549 	calc_hash(qstate, key, sizeof(key));
550 
551 	/* prepare data in scratch buffer */
552 	if(!prep_data(qstate, qstate->env->scratch_buffer))
553 		return;
554 
555 	/* call backend */
556 	(*ie->backend->store)(qstate->env, ie, key,
557 		sldns_buffer_begin(qstate->env->scratch_buffer),
558 		sldns_buffer_limit(qstate->env->scratch_buffer));
559 }
560 
561 /**
562  * See if unbound's internal cache can answer the query
563  */
564 static int
565 cachedb_intcache_lookup(struct module_qstate* qstate)
566 {
567 	struct dns_msg* msg;
568 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
569 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
570 		qstate->qinfo.qclass, qstate->query_flags,
571 		qstate->region, qstate->env->scratch,
572 		1 /* no partial messages with only a CNAME */
573 		);
574 	if(!msg && qstate->env->neg_cache) {
575 		/* lookup in negative cache; may result in
576 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
577 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
578 			qstate->region, qstate->env->rrset_cache,
579 			qstate->env->scratch_buffer,
580 			*qstate->env->now, 1/*add SOA*/, NULL,
581 			qstate->env->cfg);
582 	}
583 	if(!msg)
584 		return 0;
585 	/* this is the returned msg */
586 	qstate->return_rcode = LDNS_RCODE_NOERROR;
587 	qstate->return_msg = msg;
588 	return 1;
589 }
590 
591 /**
592  * Store query into the internal cache of unbound.
593  */
594 static void
595 cachedb_intcache_store(struct module_qstate* qstate)
596 {
597 	uint32_t store_flags = qstate->query_flags;
598 
599 	if(qstate->env->cfg->serve_expired)
600 		store_flags |= DNSCACHE_STORE_ZEROTTL;
601 	if(!qstate->return_msg)
602 		return;
603 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
604 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
605 		qstate->region, store_flags);
606 }
607 
608 /**
609  * Handle a cachedb module event with a query
610  * @param qstate: query state (from the mesh), passed between modules.
611  * 	contains qstate->env module environment with global caches and so on.
612  * @param iq: query state specific for this module.  per-query.
613  * @param ie: environment specific for this module.  global.
614  * @param id: module id.
615  */
616 static void
617 cachedb_handle_query(struct module_qstate* qstate,
618 	struct cachedb_qstate* ATTR_UNUSED(iq),
619 	struct cachedb_env* ie, int id)
620 {
621 	/* check if we are enabled, and skip if so */
622 	if(!ie->enabled) {
623 		/* pass request to next module */
624 		qstate->ext_state[id] = module_wait_module;
625 		return;
626 	}
627 
628 	if(qstate->blacklist || qstate->no_cache_lookup) {
629 		/* cache is blacklisted or we are instructed from edns to not look */
630 		/* pass request to next module */
631 		qstate->ext_state[id] = module_wait_module;
632 		return;
633 	}
634 
635 	/* lookup inside unbound's internal cache */
636 	if(cachedb_intcache_lookup(qstate)) {
637 		if(verbosity >= VERB_ALGO) {
638 			if(qstate->return_msg->rep)
639 				log_dns_msg("cachedb internal cache lookup",
640 					&qstate->return_msg->qinfo,
641 					qstate->return_msg->rep);
642 			else log_info("cachedb internal cache lookup: rcode %s",
643 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)?
644 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name:"??");
645 		}
646 		/* we are done with the query */
647 		qstate->ext_state[id] = module_finished;
648 		return;
649 	}
650 
651 	/* ask backend cache to see if we have data */
652 	if(cachedb_extcache_lookup(qstate, ie)) {
653 		if(verbosity >= VERB_ALGO)
654 			log_dns_msg(ie->backend->name,
655 				&qstate->return_msg->qinfo,
656 				qstate->return_msg->rep);
657 		/* store this result in internal cache */
658 		cachedb_intcache_store(qstate);
659 		/* we are done with the query */
660 		qstate->ext_state[id] = module_finished;
661 		return;
662 	}
663 
664 	/* no cache fetches */
665 	/* pass request to next module */
666 	qstate->ext_state[id] = module_wait_module;
667 }
668 
669 /**
670  * Handle a cachedb module event with a response from the iterator.
671  * @param qstate: query state (from the mesh), passed between modules.
672  * 	contains qstate->env module environment with global caches and so on.
673  * @param iq: query state specific for this module.  per-query.
674  * @param ie: environment specific for this module.  global.
675  * @param id: module id.
676  */
677 static void
678 cachedb_handle_response(struct module_qstate* qstate,
679 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
680 {
681 	/* check if we are not enabled or instructed to not cache, and skip */
682 	if(!ie->enabled || qstate->no_cache_store) {
683 		/* we are done with the query */
684 		qstate->ext_state[id] = module_finished;
685 		return;
686 	}
687 
688 	/* store the item into the backend cache */
689 	cachedb_extcache_store(qstate, ie);
690 
691 	/* we are done with the query */
692 	qstate->ext_state[id] = module_finished;
693 }
694 
695 void
696 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
697 	struct outbound_entry* outbound)
698 {
699 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
700 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
701 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
702 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
703 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
704 		&qstate->qinfo);
705 
706 	/* perform cachedb state machine */
707 	if((event == module_event_new || event == module_event_pass) &&
708 		iq == NULL) {
709 		if(!cachedb_new(qstate, id)) {
710 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
711 			return;
712 		}
713 		iq = (struct cachedb_qstate*)qstate->minfo[id];
714 	}
715 	if(iq && (event == module_event_pass || event == module_event_new)) {
716 		cachedb_handle_query(qstate, iq, ie, id);
717 		return;
718 	}
719 	if(iq && (event == module_event_moddone)) {
720 		cachedb_handle_response(qstate, iq, ie, id);
721 		return;
722 	}
723 	if(iq && outbound) {
724 		/* cachedb does not need to process responses at this time
725 		 * ignore it.
726 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
727 		*/
728 		return;
729 	}
730 	if(event == module_event_error) {
731 		verbose(VERB_ALGO, "got called with event error, giving up");
732 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
733 		return;
734 	}
735 	if(!iq && (event == module_event_moddone)) {
736 		/* during priming, module done but we never started */
737 		qstate->ext_state[id] = module_finished;
738 		return;
739 	}
740 
741 	log_err("bad event for cachedb");
742 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
743 }
744 
745 void
746 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
747 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
748 {
749 	/* cachedb does not use subordinate requests at this time */
750 	verbose(VERB_ALGO, "cachedb inform_super was called");
751 }
752 
753 void
754 cachedb_clear(struct module_qstate* qstate, int id)
755 {
756 	struct cachedb_qstate* iq;
757 	if(!qstate)
758 		return;
759 	iq = (struct cachedb_qstate*)qstate->minfo[id];
760 	if(iq) {
761 		/* free contents of iq */
762 		/* TODO */
763 	}
764 	qstate->minfo[id] = NULL;
765 }
766 
767 size_t
768 cachedb_get_mem(struct module_env* env, int id)
769 {
770 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
771 	if(!ie)
772 		return 0;
773 	return sizeof(*ie); /* TODO - more mem */
774 }
775 
776 /**
777  * The cachedb function block
778  */
779 static struct module_func_block cachedb_block = {
780 	"cachedb",
781 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
782 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
783 };
784 
785 struct module_func_block*
786 cachedb_get_funcblock(void)
787 {
788 	return &cachedb_block;
789 }
790 #endif /* USE_CACHEDB */
791