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>
69 # ifdef HAVE_LIBKERN_OSBYTEORDER_H
70 /* In practice this is specific to MacOS X. We assume it doesn't have
71 * htobe64/be64toh but has alternatives with a different name. */
72 # include <libkern/OSByteOrder.h>
73 # define htobe64(x) OSSwapHostToBigInt64(x)
74 # define be64toh(x) OSSwapBigToHostInt64(x)
77 /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */
79 # define be64toh(n) (n)
80 # define htobe64(n) (n)
82 # define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
83 # define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
85 # endif /* HAVE_LIBKERN_OSBYTEORDER_H */
86 #endif /* HAVE_BE64TOH */
88 /** the unit test testframe for cachedb, its module state contains
89 * a cache for a couple queries (in memory). */
90 struct testframe_moddata {
93 /** key for single stored data element, NULL if none */
95 /** data for single stored data element, NULL if none */
97 /** length of stored data */
98 size_t stored_datalen;
102 testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
104 struct testframe_moddata* d;
106 verbose(VERB_ALGO, "testframe_init");
107 d = (struct testframe_moddata*)calloc(1,
108 sizeof(struct testframe_moddata));
109 cachedb_env->backend_data = (void*)d;
110 if(!cachedb_env->backend_data) {
111 log_err("out of memory");
114 lock_basic_init(&d->lock);
115 lock_protect(&d->lock, d, sizeof(*d));
120 testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
122 struct testframe_moddata* d = (struct testframe_moddata*)
123 cachedb_env->backend_data;
125 verbose(VERB_ALGO, "testframe_deinit");
128 lock_basic_destroy(&d->lock);
130 free(d->stored_data);
135 testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
136 char* key, struct sldns_buffer* result_buffer)
138 struct testframe_moddata* d = (struct testframe_moddata*)
139 cachedb_env->backend_data;
141 verbose(VERB_ALGO, "testframe_lookup of %s", key);
142 lock_basic_lock(&d->lock);
143 if(d->stored_key && strcmp(d->stored_key, key) == 0) {
144 if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
145 lock_basic_unlock(&d->lock);
146 return 0; /* too large */
148 verbose(VERB_ALGO, "testframe_lookup found %d bytes",
149 (int)d->stored_datalen);
150 sldns_buffer_clear(result_buffer);
151 sldns_buffer_write(result_buffer, d->stored_data,
153 sldns_buffer_flip(result_buffer);
154 lock_basic_unlock(&d->lock);
157 lock_basic_unlock(&d->lock);
162 testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
163 char* key, uint8_t* data, size_t data_len)
165 struct testframe_moddata* d = (struct testframe_moddata*)
166 cachedb_env->backend_data;
168 lock_basic_lock(&d->lock);
169 verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
171 /* free old data element (if any) */
173 d->stored_key = NULL;
174 free(d->stored_data);
175 d->stored_data = NULL;
176 d->stored_datalen = 0;
178 d->stored_data = memdup(data, data_len);
179 if(!d->stored_data) {
180 lock_basic_unlock(&d->lock);
181 log_err("out of memory");
184 d->stored_datalen = data_len;
185 d->stored_key = strdup(key);
187 free(d->stored_data);
188 d->stored_data = NULL;
189 d->stored_datalen = 0;
190 lock_basic_unlock(&d->lock);
193 lock_basic_unlock(&d->lock);
194 /* (key,data) successfully stored */
197 /** The testframe backend is for unit tests */
198 static struct cachedb_backend testframe_backend = { "testframe",
199 testframe_init, testframe_deinit, testframe_lookup, testframe_store
202 /** find a particular backend from possible backends */
203 static struct cachedb_backend*
204 cachedb_find_backend(const char* str)
207 if(strcmp(str, redis_backend.name) == 0)
208 return &redis_backend;
210 if(strcmp(str, testframe_backend.name) == 0)
211 return &testframe_backend;
212 /* TODO add more backends here */
216 /** apply configuration to cachedb module 'global' state */
218 cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
220 const char* backend_str = cfg->cachedb_backend;
221 cachedb_env->backend = cachedb_find_backend(backend_str);
222 if(!cachedb_env->backend) {
223 log_err("cachedb: cannot find backend name '%s'", backend_str);
227 /* TODO see if more configuration needs to be applied or not */
232 cachedb_init(struct module_env* env, int id)
234 struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
235 sizeof(struct cachedb_env));
237 log_err("malloc failure");
240 env->modinfo[id] = (void*)cachedb_env;
241 if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
242 log_err("cachedb: could not apply configuration settings.");
244 env->modinfo[id] = NULL;
247 /* see if a backend is selected */
248 if(!cachedb_env->backend || !cachedb_env->backend->name)
250 if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
251 log_err("cachedb: could not init %s backend",
252 cachedb_env->backend->name);
254 env->modinfo[id] = NULL;
257 cachedb_env->enabled = 1;
258 if(env->cfg->serve_expired_reply_ttl)
260 "cachedb: serve-expired-reply-ttl is set but not working for data "
261 "originating from the external cache; 0 TLL is used for those.");
262 if(env->cfg->serve_expired_client_timeout)
264 "cachedb: serve-expired-client-timeout is set but not working for "
265 "data originating from the external cache; expired data are used "
266 "in the reply without first trying to refresh the data.");
271 cachedb_deinit(struct module_env* env, int id)
273 struct cachedb_env* cachedb_env;
274 if(!env || !env->modinfo[id])
276 cachedb_env = (struct cachedb_env*)env->modinfo[id];
279 if(cachedb_env->enabled) {
280 (*cachedb_env->backend->deinit)(env, cachedb_env);
284 env->modinfo[id] = NULL;
287 /** new query for cachedb */
289 cachedb_new(struct module_qstate* qstate, int id)
291 struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
292 qstate->region, sizeof(struct cachedb_qstate));
293 qstate->minfo[id] = iq;
296 memset(iq, 0, sizeof(*iq));
305 * @param qstate: our query state
306 * @param id: module id
307 * @param rcode: error code (DNS errcode).
308 * @return: 0 for use by caller, to make notation easy, like:
309 * return error_response(..).
312 error_response(struct module_qstate* qstate, int id, int rcode)
314 verbose(VERB_QUERY, "return error response %s",
315 sldns_lookup_by_id(sldns_rcodes, rcode)?
316 sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
317 qstate->return_rcode = rcode;
318 qstate->return_msg = NULL;
319 qstate->ext_state[id] = module_finished;
324 * Hash the query name, type, class and dbacess-secret into lookup buffer.
325 * @param qstate: query state with query info
326 * and env->cfg with secret.
327 * @param buf: returned buffer with hash to lookup
328 * @param len: length of the buffer.
331 calc_hash(struct module_qstate* qstate, char* buf, size_t len)
335 uint8_t hash[CACHEDB_HASHSIZE/8];
336 const char* hex = "0123456789ABCDEF";
337 const char* secret = qstate->env->cfg->cachedb_secret;
340 /* copy the hash info into the clear buffer */
341 if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
342 memmove(clear+clen, qstate->qinfo.qname,
343 qstate->qinfo.qname_len);
344 clen += qstate->qinfo.qname_len;
346 if(clen + 4 < sizeof(clear)) {
347 uint16_t t = htons(qstate->qinfo.qtype);
348 uint16_t c = htons(qstate->qinfo.qclass);
349 memmove(clear+clen, &t, 2);
350 memmove(clear+clen+2, &c, 2);
353 if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
354 memmove(clear+clen, secret, strlen(secret));
355 clen += strlen(secret);
358 /* hash the buffer */
359 secalgo_hash_sha256(clear, clen, hash);
360 #ifdef HAVE_EXPLICIT_BZERO
361 explicit_bzero(clear, clen);
363 memset(clear, 0, clen);
366 /* hex encode output for portability (some online dbs need
367 * no nulls, no control characters, and so on) */
368 log_assert(len >= sizeof(hash)*2 + 1);
370 for(i=0; i<sizeof(hash); i++) {
371 buf[i*2] = hex[(hash[i]&0xf0)>>4];
372 buf[i*2+1] = hex[hash[i]&0x0f];
374 buf[sizeof(hash)*2] = 0;
377 /** convert data from return_msg into the data buffer */
379 prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
381 uint64_t timestamp, expiry;
383 struct edns_data edns;
384 memset(&edns, 0, sizeof(edns));
385 edns.edns_present = 1;
388 edns.edns_version = EDNS_ADVERTISED_VERSION;
389 edns.udp_size = EDNS_ADVERTISED_SIZE;
391 if(!qstate->return_msg || !qstate->return_msg->rep)
393 /* We don't store the reply if its TTL is 0 unless serve-expired is
394 * enabled. Such a reply won't be reusable and simply be a waste for
395 * the backend. It's also compatible with the default behavior of
396 * dns_cache_store_msg(). */
397 if(qstate->return_msg->rep->ttl == 0 &&
398 !qstate->env->cfg->serve_expired)
400 if(verbosity >= VERB_ALGO)
401 log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
402 qstate->return_msg->rep);
403 if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
404 qstate->return_msg->rep, 0, qstate->query_flags,
405 buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
408 /* TTLs in the return_msg are relative to time(0) so we have to
409 * store that, we also store the smallest ttl in the packet+time(0)
410 * as the packet expiry time */
411 /* qstate->return_msg->rep->ttl contains that relative shortest ttl */
412 timestamp = (uint64_t)*qstate->env->now;
413 expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
414 timestamp = htobe64(timestamp);
415 expiry = htobe64(expiry);
416 oldlim = sldns_buffer_limit(buf);
417 if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
418 sldns_buffer_capacity(buf))
419 return 0; /* doesn't fit. */
420 sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
421 sldns_buffer_write_at(buf, oldlim, ×tamp, sizeof(timestamp));
422 sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
428 /** check expiry, return true if matches OK */
430 good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
433 /* the expiry time is the last bytes of the buffer */
434 if(sldns_buffer_limit(buf) < sizeof(expiry))
436 sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
437 &expiry, sizeof(expiry));
438 expiry = be64toh(expiry);
440 /* Check if we are allowed to return expired entries:
441 * - serve_expired needs to be set
442 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
444 if((time_t)expiry < *qstate->env->now &&
445 (!qstate->env->cfg->serve_expired ||
446 (SERVE_EXPIRED_TTL &&
447 *qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
453 /* Adjust the TTL of the given RRset by 'subtract'. If 'subtract' is
454 * negative, set the TTL to 0. */
456 packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
459 size_t total = data->count + data->rrsig_count;
460 if(subtract >= 0 && data->ttl > subtract)
461 data->ttl -= subtract;
463 for(i=0; i<total; i++) {
464 if(subtract >= 0 && data->rr_ttl[i] > subtract)
465 data->rr_ttl[i] -= subtract;
466 else data->rr_ttl[i] = 0;
470 /* Adjust the TTL of a DNS message and its RRs by 'adjust'. If 'adjust' is
471 * negative, set the TTLs to 0. */
473 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
476 if(adjust >= 0 && msg->rep->ttl > adjust)
477 msg->rep->ttl -= adjust;
480 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
481 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
483 for(i=0; i<msg->rep->rrset_count; i++) {
484 packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
485 rep->rrsets[i]->entry.data, adjust);
489 /** convert dns message in buffer to return_msg */
491 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
493 struct msg_parse* prs;
494 struct edns_data edns;
495 uint64_t timestamp, expiry;
497 size_t lim = sldns_buffer_limit(buf);
498 if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
499 return 0; /* too short */
501 /* remove timestamp and expiry from end */
502 sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
503 sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
504 ×tamp, sizeof(timestamp));
505 expiry = be64toh(expiry);
506 timestamp = be64toh(timestamp);
508 /* parse DNS packet */
509 regional_free_all(qstate->env->scratch);
510 prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
511 sizeof(struct msg_parse));
513 return 0; /* out of memory */
514 memset(prs, 0, sizeof(*prs));
515 memset(&edns, 0, sizeof(edns));
516 sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
517 if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
518 sldns_buffer_set_limit(buf, lim);
521 if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
522 LDNS_RCODE_NOERROR) {
523 sldns_buffer_set_limit(buf, lim);
527 qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
528 sldns_buffer_set_limit(buf, lim);
529 if(!qstate->return_msg)
532 qstate->return_rcode = LDNS_RCODE_NOERROR;
534 /* see how much of the TTL expired, and remove it */
535 if(*qstate->env->now <= (time_t)timestamp) {
536 verbose(VERB_ALGO, "cachedb msg adjust by zero");
537 return 1; /* message from the future (clock skew?) */
539 adjust = *qstate->env->now - (time_t)timestamp;
540 if(qstate->return_msg->rep->ttl < adjust) {
541 verbose(VERB_ALGO, "cachedb msg expired");
542 /* If serve-expired is enabled, we still use an expired message
543 * setting the TTL to 0. */
544 if(qstate->env->cfg->serve_expired)
547 return 0; /* message expired */
549 verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
550 adjust_msg_ttl(qstate->return_msg, adjust);
552 /* Similar to the unbound worker, if serve-expired is enabled and
553 * the msg would be considered to be expired, mark the state so a
554 * refetch will be scheduled. The comparison between 'expiry' and
555 * 'now' should be redundant given how these values were calculated,
556 * but we check it just in case as does good_expiry_and_qinfo(). */
557 if(qstate->env->cfg->serve_expired &&
558 (adjust == -1 || (time_t)expiry < *qstate->env->now)) {
559 qstate->need_refetch = 1;
566 * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
567 * return true if lookup was successful.
570 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
572 char key[(CACHEDB_HASHSIZE/8)*2+1];
573 calc_hash(qstate, key, sizeof(key));
575 /* call backend to fetch data for key into scratch buffer */
576 if( !(*ie->backend->lookup)(qstate->env, ie, key,
577 qstate->env->scratch_buffer)) {
581 /* check expiry date and check if query-data matches */
582 if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
586 /* parse dns message into return_msg */
587 if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
594 * Store the qstate.return_msg in extcache for key qstate.info
597 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
599 char key[(CACHEDB_HASHSIZE/8)*2+1];
600 calc_hash(qstate, key, sizeof(key));
602 /* prepare data in scratch buffer */
603 if(!prep_data(qstate, qstate->env->scratch_buffer))
607 (*ie->backend->store)(qstate->env, ie, key,
608 sldns_buffer_begin(qstate->env->scratch_buffer),
609 sldns_buffer_limit(qstate->env->scratch_buffer));
613 * See if unbound's internal cache can answer the query
616 cachedb_intcache_lookup(struct module_qstate* qstate)
619 msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
620 qstate->qinfo.qname_len, qstate->qinfo.qtype,
621 qstate->qinfo.qclass, qstate->query_flags,
622 qstate->region, qstate->env->scratch,
623 1 /* no partial messages with only a CNAME */
625 if(!msg && qstate->env->neg_cache &&
626 iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
627 /* lookup in negative cache; may result in
628 * NOERROR/NODATA or NXDOMAIN answers that need validation */
629 msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
630 qstate->region, qstate->env->rrset_cache,
631 qstate->env->scratch_buffer,
632 *qstate->env->now, 1/*add SOA*/, NULL,
637 /* this is the returned msg */
638 qstate->return_rcode = LDNS_RCODE_NOERROR;
639 qstate->return_msg = msg;
644 * Store query into the internal cache of unbound.
647 cachedb_intcache_store(struct module_qstate* qstate)
649 uint32_t store_flags = qstate->query_flags;
651 if(qstate->env->cfg->serve_expired)
652 store_flags |= DNSCACHE_STORE_ZEROTTL;
653 if(!qstate->return_msg)
655 (void)dns_cache_store(qstate->env, &qstate->qinfo,
656 qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
657 qstate->region, store_flags);
661 * Handle a cachedb module event with a query
662 * @param qstate: query state (from the mesh), passed between modules.
663 * contains qstate->env module environment with global caches and so on.
664 * @param iq: query state specific for this module. per-query.
665 * @param ie: environment specific for this module. global.
666 * @param id: module id.
669 cachedb_handle_query(struct module_qstate* qstate,
670 struct cachedb_qstate* ATTR_UNUSED(iq),
671 struct cachedb_env* ie, int id)
673 /* check if we are enabled, and skip if so */
675 /* pass request to next module */
676 qstate->ext_state[id] = module_wait_module;
680 if(qstate->blacklist || qstate->no_cache_lookup) {
681 /* cache is blacklisted or we are instructed from edns to not look */
682 /* pass request to next module */
683 qstate->ext_state[id] = module_wait_module;
687 /* lookup inside unbound's internal cache.
688 * This does not look for expired entries. */
689 if(cachedb_intcache_lookup(qstate)) {
690 if(verbosity >= VERB_ALGO) {
691 if(qstate->return_msg->rep)
692 log_dns_msg("cachedb internal cache lookup",
693 &qstate->return_msg->qinfo,
694 qstate->return_msg->rep);
695 else log_info("cachedb internal cache lookup: rcode %s",
696 sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
697 ?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
700 /* we are done with the query */
701 qstate->ext_state[id] = module_finished;
705 /* ask backend cache to see if we have data */
706 if(cachedb_extcache_lookup(qstate, ie)) {
707 if(verbosity >= VERB_ALGO)
708 log_dns_msg(ie->backend->name,
709 &qstate->return_msg->qinfo,
710 qstate->return_msg->rep);
711 /* store this result in internal cache */
712 cachedb_intcache_store(qstate);
713 /* In case we have expired data but there is a client timer for expired
714 * answers, pass execution to next module in order to try updating the
716 * TODO: this needs revisit. The expired data stored from cachedb has
717 * 0 TTL which is picked up by iterator later when looking in the cache.
718 * Document that ext cachedb does not work properly with
719 * serve_stale_reply_ttl yet. */
720 if(qstate->need_refetch && qstate->serve_expired_data &&
721 qstate->serve_expired_data->timer) {
722 qstate->return_msg = NULL;
723 qstate->ext_state[id] = module_wait_module;
726 /* we are done with the query */
727 qstate->ext_state[id] = module_finished;
731 /* no cache fetches */
732 /* pass request to next module */
733 qstate->ext_state[id] = module_wait_module;
737 * Handle a cachedb module event with a response from the iterator.
738 * @param qstate: query state (from the mesh), passed between modules.
739 * contains qstate->env module environment with global caches and so on.
740 * @param iq: query state specific for this module. per-query.
741 * @param ie: environment specific for this module. global.
742 * @param id: module id.
745 cachedb_handle_response(struct module_qstate* qstate,
746 struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
748 /* check if we are not enabled or instructed to not cache, and skip */
749 if(!ie->enabled || qstate->no_cache_store) {
750 /* we are done with the query */
751 qstate->ext_state[id] = module_finished;
755 /* store the item into the backend cache */
756 cachedb_extcache_store(qstate, ie);
758 /* we are done with the query */
759 qstate->ext_state[id] = module_finished;
763 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
764 struct outbound_entry* outbound)
766 struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
767 struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
768 verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
769 id, strextstate(qstate->ext_state[id]), strmodulevent(event));
770 if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
773 /* perform cachedb state machine */
774 if((event == module_event_new || event == module_event_pass) &&
776 if(!cachedb_new(qstate, id)) {
777 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
780 iq = (struct cachedb_qstate*)qstate->minfo[id];
782 if(iq && (event == module_event_pass || event == module_event_new)) {
783 cachedb_handle_query(qstate, iq, ie, id);
786 if(iq && (event == module_event_moddone)) {
787 cachedb_handle_response(qstate, iq, ie, id);
791 /* cachedb does not need to process responses at this time
793 cachedb_process_response(qstate, iq, ie, id, outbound, event);
797 if(event == module_event_error) {
798 verbose(VERB_ALGO, "got called with event error, giving up");
799 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
802 if(!iq && (event == module_event_moddone)) {
803 /* during priming, module done but we never started */
804 qstate->ext_state[id] = module_finished;
808 log_err("bad event for cachedb");
809 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
813 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
814 int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
816 /* cachedb does not use subordinate requests at this time */
817 verbose(VERB_ALGO, "cachedb inform_super was called");
821 cachedb_clear(struct module_qstate* qstate, int id)
823 struct cachedb_qstate* iq;
826 iq = (struct cachedb_qstate*)qstate->minfo[id];
828 /* free contents of iq */
831 qstate->minfo[id] = NULL;
835 cachedb_get_mem(struct module_env* env, int id)
837 struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
840 return sizeof(*ie); /* TODO - more mem */
844 * The cachedb function block
846 static struct module_func_block cachedb_block = {
848 &cachedb_init, &cachedb_deinit, &cachedb_operate,
849 &cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
852 struct module_func_block*
853 cachedb_get_funcblock(void)
855 return &cachedb_block;
857 #endif /* USE_CACHEDB */