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