2 * cachedb/cachedb.c - cache from a database external to the program module
4 * Copyright (c) 2016, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains a module that uses an external database to cache
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"
60 /* header file for htobe64 */
64 #ifdef HAVE_SYS_ENDIAN_H
65 # include <sys/endian.h>
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)
75 /** the unit test testframe for cachedb, its module state contains
76 * a cache for a couple queries (in memory). */
77 struct testframe_moddata {
80 /** key for single stored data element, NULL if none */
82 /** data for single stored data element, NULL if none */
84 /** length of stored data */
85 size_t stored_datalen;
89 testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
91 struct testframe_moddata* d;
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");
101 lock_basic_init(&d->lock);
102 lock_protect(&d->lock, d, sizeof(*d));
107 testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
109 struct testframe_moddata* d = (struct testframe_moddata*)
110 cachedb_env->backend_data;
112 verbose(VERB_ALGO, "testframe_deinit");
115 lock_basic_destroy(&d->lock);
117 free(d->stored_data);
122 testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
123 char* key, struct sldns_buffer* result_buffer)
125 struct testframe_moddata* d = (struct testframe_moddata*)
126 cachedb_env->backend_data;
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 */
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,
140 sldns_buffer_flip(result_buffer);
141 lock_basic_unlock(&d->lock);
144 lock_basic_unlock(&d->lock);
149 testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
150 char* key, uint8_t* data, size_t data_len)
152 struct testframe_moddata* d = (struct testframe_moddata*)
153 cachedb_env->backend_data;
155 lock_basic_lock(&d->lock);
156 verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
158 /* free old data element (if any) */
160 d->stored_key = NULL;
161 free(d->stored_data);
162 d->stored_data = NULL;
163 d->stored_datalen = 0;
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");
171 d->stored_datalen = data_len;
172 d->stored_key = strdup(key);
174 free(d->stored_data);
175 d->stored_data = NULL;
176 d->stored_datalen = 0;
177 lock_basic_unlock(&d->lock);
180 lock_basic_unlock(&d->lock);
181 /* (key,data) successfully stored */
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
189 /** find a particular backend from possible backends */
190 static struct cachedb_backend*
191 cachedb_find_backend(const char* str)
194 if(strcmp(str, redis_backend.name) == 0)
195 return &redis_backend;
197 if(strcmp(str, testframe_backend.name) == 0)
198 return &testframe_backend;
199 /* TODO add more backends here */
203 /** apply configuration to cachedb module 'global' state */
205 cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
207 const char* backend_str = cfg->cachedb_backend;
209 /* If unspecified we use the in-memory test DB. */
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);
218 /* TODO see if more configuration needs to be applied or not */
223 cachedb_init(struct module_env* env, int id)
225 struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
226 sizeof(struct cachedb_env));
228 log_err("malloc failure");
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.");
236 /* see if a backend is selected */
237 if(!cachedb_env->backend || !cachedb_env->backend->name)
239 if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
240 log_err("cachedb: could not init %s backend",
241 cachedb_env->backend->name);
244 cachedb_env->enabled = 1;
249 cachedb_deinit(struct module_env* env, int id)
251 struct cachedb_env* cachedb_env;
252 if(!env || !env->modinfo[id])
254 cachedb_env = (struct cachedb_env*)env->modinfo[id];
257 if(cachedb_env->enabled) {
258 (*cachedb_env->backend->deinit)(env, cachedb_env);
262 env->modinfo[id] = NULL;
265 /** new query for cachedb */
267 cachedb_new(struct module_qstate* qstate, int id)
269 struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
270 qstate->region, sizeof(struct cachedb_qstate));
271 qstate->minfo[id] = iq;
274 memset(iq, 0, sizeof(*iq));
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(..).
290 error_response(struct module_qstate* qstate, int id, int rcode)
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;
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.
309 calc_hash(struct module_qstate* qstate, char* buf, size_t len)
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";
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;
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);
332 if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
333 memmove(clear+clen, secret, strlen(secret));
334 clen += strlen(secret);
337 /* hash the buffer */
338 secalgo_hash_sha256(clear, clen, hash);
339 memset(clear, 0, clen);
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);
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];
349 buf[sizeof(hash)*2] = 0;
352 /** convert data from return_msg into the data buffer */
354 prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
356 uint64_t timestamp, expiry;
358 struct edns_data edns;
359 memset(&edns, 0, sizeof(edns));
360 edns.edns_present = 1;
363 edns.edns_version = EDNS_ADVERTISED_VERSION;
364 edns.udp_size = EDNS_ADVERTISED_SIZE;
366 if(!qstate->return_msg || !qstate->return_msg->rep)
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)
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))
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, ×tamp, sizeof(timestamp));
397 sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
403 /** check expiry, return true if matches OK */
405 good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
408 /* the expiry time is the last bytes of the buffer */
409 if(sldns_buffer_limit(buf) < sizeof(expiry))
411 sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
412 &expiry, sizeof(expiry));
413 expiry = be64toh(expiry);
415 if((time_t)expiry < *qstate->env->now &&
416 !qstate->env->cfg->serve_expired)
422 /* Adjust the TTL of the given RRset by 'subtract'. If 'subtract' is
423 * negative, set the TTL to 0. */
425 packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
428 size_t total = data->count + data->rrsig_count;
429 if(subtract >= 0 && data->ttl > subtract)
430 data->ttl -= subtract;
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;
439 /* Adjust the TTL of a DNS message and its RRs by 'adjust'. If 'adjust' is
440 * negative, set the TTLs to 0. */
442 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
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);
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);
456 /** convert dns message in buffer to return_msg */
458 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
460 struct msg_parse* prs;
461 struct edns_data edns;
462 uint64_t timestamp, expiry;
464 size_t lim = sldns_buffer_limit(buf);
465 if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
466 return 0; /* too short */
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 ×tamp, sizeof(timestamp));
472 expiry = be64toh(expiry);
473 timestamp = be64toh(timestamp);
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));
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);
488 if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
489 LDNS_RCODE_NOERROR) {
490 sldns_buffer_set_limit(buf, lim);
494 qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
495 sldns_buffer_set_limit(buf, lim);
496 if(!qstate->return_msg)
499 qstate->return_rcode = LDNS_RCODE_NOERROR;
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?) */
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)
514 return 0; /* message expired */
516 verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
517 adjust_msg_ttl(qstate->return_msg, adjust);
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;
533 * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
534 * return true if lookup was successful.
537 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
539 char key[(CACHEDB_HASHSIZE/8)*2+1];
540 calc_hash(qstate, key, sizeof(key));
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)) {
548 /* check expiry date and check if query-data matches */
549 if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
553 /* parse dns message into return_msg */
554 if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
561 * Store the qstate.return_msg in extcache for key qstate.info
564 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
566 char key[(CACHEDB_HASHSIZE/8)*2+1];
567 calc_hash(qstate, key, sizeof(key));
569 /* prepare data in scratch buffer */
570 if(!prep_data(qstate, qstate->env->scratch_buffer))
574 (*ie->backend->store)(qstate->env, ie, key,
575 sldns_buffer_begin(qstate->env->scratch_buffer),
576 sldns_buffer_limit(qstate->env->scratch_buffer));
580 * See if unbound's internal cache can answer the query
583 cachedb_intcache_lookup(struct module_qstate* qstate)
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 */
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,
604 /* this is the returned msg */
605 qstate->return_rcode = LDNS_RCODE_NOERROR;
606 qstate->return_msg = msg;
611 * Store query into the internal cache of unbound.
614 cachedb_intcache_store(struct module_qstate* qstate)
616 uint32_t store_flags = qstate->query_flags;
618 if(qstate->env->cfg->serve_expired)
619 store_flags |= DNSCACHE_STORE_ZEROTTL;
620 if(!qstate->return_msg)
622 (void)dns_cache_store(qstate->env, &qstate->qinfo,
623 qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
624 qstate->region, store_flags);
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.
636 cachedb_handle_query(struct module_qstate* qstate,
637 struct cachedb_qstate* ATTR_UNUSED(iq),
638 struct cachedb_env* ie, int id)
640 /* check if we are enabled, and skip if so */
642 /* pass request to next module */
643 qstate->ext_state[id] = module_wait_module;
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;
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:"??");
665 /* we are done with the query */
666 qstate->ext_state[id] = module_finished;
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;
683 /* no cache fetches */
684 /* pass request to next module */
685 qstate->ext_state[id] = module_wait_module;
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.
697 cachedb_handle_response(struct module_qstate* qstate,
698 struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
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;
707 /* store the item into the backend cache */
708 cachedb_extcache_store(qstate, ie);
710 /* we are done with the query */
711 qstate->ext_state[id] = module_finished;
715 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
716 struct outbound_entry* outbound)
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",
725 /* perform cachedb state machine */
726 if((event == module_event_new || event == module_event_pass) &&
728 if(!cachedb_new(qstate, id)) {
729 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
732 iq = (struct cachedb_qstate*)qstate->minfo[id];
734 if(iq && (event == module_event_pass || event == module_event_new)) {
735 cachedb_handle_query(qstate, iq, ie, id);
738 if(iq && (event == module_event_moddone)) {
739 cachedb_handle_response(qstate, iq, ie, id);
743 /* cachedb does not need to process responses at this time
745 cachedb_process_response(qstate, iq, ie, id, outbound, event);
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);
754 if(!iq && (event == module_event_moddone)) {
755 /* during priming, module done but we never started */
756 qstate->ext_state[id] = module_finished;
760 log_err("bad event for cachedb");
761 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
765 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
766 int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
768 /* cachedb does not use subordinate requests at this time */
769 verbose(VERB_ALGO, "cachedb inform_super was called");
773 cachedb_clear(struct module_qstate* qstate, int id)
775 struct cachedb_qstate* iq;
778 iq = (struct cachedb_qstate*)qstate->minfo[id];
780 /* free contents of iq */
783 qstate->minfo[id] = NULL;
787 cachedb_get_mem(struct module_env* env, int id)
789 struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
792 return sizeof(*ie); /* TODO - more mem */
796 * The cachedb function block
798 static struct module_func_block cachedb_block = {
800 &cachedb_init, &cachedb_deinit, &cachedb_operate,
801 &cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
804 struct module_func_block*
805 cachedb_get_funcblock(void)
807 return &cachedb_block;
809 #endif /* USE_CACHEDB */