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;
105 verbose(VERB_ALGO, "testframe_init");
106 d = (struct testframe_moddata*)calloc(1,
107 sizeof(struct testframe_moddata));
108 cachedb_env->backend_data = (void*)d;
109 if(!cachedb_env->backend_data) {
110 log_err("out of memory");
113 /* Register an EDNS option (65534) to bypass the worker cache lookup
115 if(!edns_register_option(LDNS_EDNS_UNBOUND_CACHEDB_TESTFRAME_TEST,
116 1 /* bypass cache */,
117 0 /* no aggregation */, env)) {
118 log_err("testframe_init, could not register test opcode");
122 lock_basic_init(&d->lock);
123 lock_protect(&d->lock, d, sizeof(*d));
128 testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
130 struct testframe_moddata* d = (struct testframe_moddata*)
131 cachedb_env->backend_data;
133 verbose(VERB_ALGO, "testframe_deinit");
136 lock_basic_destroy(&d->lock);
138 free(d->stored_data);
143 testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
144 char* key, struct sldns_buffer* result_buffer)
146 struct testframe_moddata* d = (struct testframe_moddata*)
147 cachedb_env->backend_data;
149 verbose(VERB_ALGO, "testframe_lookup of %s", key);
150 lock_basic_lock(&d->lock);
151 if(d->stored_key && strcmp(d->stored_key, key) == 0) {
152 if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
153 lock_basic_unlock(&d->lock);
154 return 0; /* too large */
156 verbose(VERB_ALGO, "testframe_lookup found %d bytes",
157 (int)d->stored_datalen);
158 sldns_buffer_clear(result_buffer);
159 sldns_buffer_write(result_buffer, d->stored_data,
161 sldns_buffer_flip(result_buffer);
162 lock_basic_unlock(&d->lock);
165 lock_basic_unlock(&d->lock);
170 testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
171 char* key, uint8_t* data, size_t data_len, time_t ATTR_UNUSED(ttl))
173 struct testframe_moddata* d = (struct testframe_moddata*)
174 cachedb_env->backend_data;
176 lock_basic_lock(&d->lock);
177 verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
179 /* free old data element (if any) */
181 d->stored_key = NULL;
182 free(d->stored_data);
183 d->stored_data = NULL;
184 d->stored_datalen = 0;
186 d->stored_data = memdup(data, data_len);
187 if(!d->stored_data) {
188 lock_basic_unlock(&d->lock);
189 log_err("out of memory");
192 d->stored_datalen = data_len;
193 d->stored_key = strdup(key);
195 free(d->stored_data);
196 d->stored_data = NULL;
197 d->stored_datalen = 0;
198 lock_basic_unlock(&d->lock);
201 lock_basic_unlock(&d->lock);
202 /* (key,data) successfully stored */
205 /** The testframe backend is for unit tests */
206 static struct cachedb_backend testframe_backend = { "testframe",
207 testframe_init, testframe_deinit, testframe_lookup, testframe_store
210 /** find a particular backend from possible backends */
211 static struct cachedb_backend*
212 cachedb_find_backend(const char* str)
215 if(strcmp(str, redis_backend.name) == 0)
216 return &redis_backend;
218 if(strcmp(str, testframe_backend.name) == 0)
219 return &testframe_backend;
220 /* TODO add more backends here */
224 /** apply configuration to cachedb module 'global' state */
226 cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
228 const char* backend_str = cfg->cachedb_backend;
229 if(!backend_str || *backend_str==0)
231 cachedb_env->backend = cachedb_find_backend(backend_str);
232 if(!cachedb_env->backend) {
233 log_err("cachedb: cannot find backend name '%s'", backend_str);
237 /* TODO see if more configuration needs to be applied or not */
242 cachedb_init(struct module_env* env, int id)
244 struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
245 sizeof(struct cachedb_env));
247 log_err("malloc failure");
250 env->modinfo[id] = (void*)cachedb_env;
251 if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
252 log_err("cachedb: could not apply configuration settings.");
254 env->modinfo[id] = NULL;
257 /* see if a backend is selected */
258 if(!cachedb_env->backend || !cachedb_env->backend->name)
260 if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
261 log_err("cachedb: could not init %s backend",
262 cachedb_env->backend->name);
264 env->modinfo[id] = NULL;
267 cachedb_env->enabled = 1;
268 if(env->cfg->serve_expired && env->cfg->serve_expired_reply_ttl)
270 "cachedb: serve-expired-reply-ttl is set but not working for data "
271 "originating from the external cache; 0 TTL is used for those.");
272 if(env->cfg->serve_expired && env->cfg->serve_expired_client_timeout)
274 "cachedb: serve-expired-client-timeout is set but not working for "
275 "data originating from the external cache; expired data are used "
276 "in the reply without first trying to refresh the data.");
281 cachedb_deinit(struct module_env* env, int id)
283 struct cachedb_env* cachedb_env;
284 if(!env || !env->modinfo[id])
286 cachedb_env = (struct cachedb_env*)env->modinfo[id];
287 if(cachedb_env->enabled) {
288 (*cachedb_env->backend->deinit)(env, cachedb_env);
291 env->modinfo[id] = NULL;
294 /** new query for cachedb */
296 cachedb_new(struct module_qstate* qstate, int id)
298 struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
299 qstate->region, sizeof(struct cachedb_qstate));
300 qstate->minfo[id] = iq;
303 memset(iq, 0, sizeof(*iq));
312 * @param qstate: our query state
313 * @param id: module id
314 * @param rcode: error code (DNS errcode).
315 * @return: 0 for use by caller, to make notation easy, like:
316 * return error_response(..).
319 error_response(struct module_qstate* qstate, int id, int rcode)
321 verbose(VERB_QUERY, "return error response %s",
322 sldns_lookup_by_id(sldns_rcodes, rcode)?
323 sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
324 qstate->return_rcode = rcode;
325 qstate->return_msg = NULL;
326 qstate->ext_state[id] = module_finished;
331 * Hash the query name, type, class and dbacess-secret into lookup buffer.
332 * @param qstate: query state with query info
333 * and env->cfg with secret.
334 * @param buf: returned buffer with hash to lookup
335 * @param len: length of the buffer.
338 calc_hash(struct module_qstate* qstate, char* buf, size_t len)
342 uint8_t hash[CACHEDB_HASHSIZE/8];
343 const char* hex = "0123456789ABCDEF";
344 const char* secret = qstate->env->cfg->cachedb_secret;
347 /* copy the hash info into the clear buffer */
348 if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
349 memmove(clear+clen, qstate->qinfo.qname,
350 qstate->qinfo.qname_len);
351 clen += qstate->qinfo.qname_len;
353 if(clen + 4 < sizeof(clear)) {
354 uint16_t t = htons(qstate->qinfo.qtype);
355 uint16_t c = htons(qstate->qinfo.qclass);
356 memmove(clear+clen, &t, 2);
357 memmove(clear+clen+2, &c, 2);
360 if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
361 memmove(clear+clen, secret, strlen(secret));
362 clen += strlen(secret);
365 /* hash the buffer */
366 secalgo_hash_sha256(clear, clen, hash);
367 #ifdef HAVE_EXPLICIT_BZERO
368 explicit_bzero(clear, clen);
370 memset(clear, 0, clen);
373 /* hex encode output for portability (some online dbs need
374 * no nulls, no control characters, and so on) */
375 log_assert(len >= sizeof(hash)*2 + 1);
377 for(i=0; i<sizeof(hash); i++) {
378 buf[i*2] = hex[(hash[i]&0xf0)>>4];
379 buf[i*2+1] = hex[hash[i]&0x0f];
381 buf[sizeof(hash)*2] = 0;
384 /** convert data from return_msg into the data buffer */
386 prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
388 uint64_t timestamp, expiry;
390 struct edns_data edns;
391 memset(&edns, 0, sizeof(edns));
392 edns.edns_present = 1;
395 edns.edns_version = EDNS_ADVERTISED_VERSION;
396 edns.udp_size = EDNS_ADVERTISED_SIZE;
398 if(!qstate->return_msg || !qstate->return_msg->rep)
400 /* do not store failures like SERVFAIL in the cachedb, this avoids
401 * overwriting expired, valid, content with broken content. */
402 if(FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
403 LDNS_RCODE_NOERROR &&
404 FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
405 LDNS_RCODE_NXDOMAIN &&
406 FLAGS_GET_RCODE(qstate->return_msg->rep->flags) !=
409 /* We don't store the reply if its TTL is 0 unless serve-expired is
410 * enabled. Such a reply won't be reusable and simply be a waste for
411 * the backend. It's also compatible with the default behavior of
412 * dns_cache_store_msg(). */
413 if(qstate->return_msg->rep->ttl == 0 &&
414 !qstate->env->cfg->serve_expired)
417 /* The EDE is added to the out-list so it is encoded in the cached message */
418 if (qstate->env->cfg->ede && qstate->return_msg->rep->reason_bogus != LDNS_EDE_NONE) {
419 edns_opt_list_append_ede(&edns.opt_list_out, qstate->env->scratch,
420 qstate->return_msg->rep->reason_bogus,
421 qstate->return_msg->rep->reason_bogus_str);
424 if(verbosity >= VERB_ALGO)
425 log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
426 qstate->return_msg->rep);
427 if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
428 qstate->return_msg->rep, 0, qstate->query_flags,
429 buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
432 /* TTLs in the return_msg are relative to time(0) so we have to
433 * store that, we also store the smallest ttl in the packet+time(0)
434 * as the packet expiry time */
435 /* qstate->return_msg->rep->ttl contains that relative shortest ttl */
436 timestamp = (uint64_t)*qstate->env->now;
437 expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
438 timestamp = htobe64(timestamp);
439 expiry = htobe64(expiry);
440 oldlim = sldns_buffer_limit(buf);
441 if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
442 sldns_buffer_capacity(buf))
443 return 0; /* doesn't fit. */
444 sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
445 sldns_buffer_write_at(buf, oldlim, ×tamp, sizeof(timestamp));
446 sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
452 /** check expiry, return true if matches OK */
454 good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
457 /* the expiry time is the last bytes of the buffer */
458 if(sldns_buffer_limit(buf) < sizeof(expiry))
460 sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
461 &expiry, sizeof(expiry));
462 expiry = be64toh(expiry);
464 /* Check if we are allowed to return expired entries:
465 * - serve_expired needs to be set
466 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
468 if((time_t)expiry < *qstate->env->now &&
469 (!qstate->env->cfg->serve_expired ||
470 (SERVE_EXPIRED_TTL &&
471 *qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
477 /* Adjust the TTL of the given RRset by 'subtract'. If 'subtract' is
478 * negative, set the TTL to 0. */
480 packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
483 size_t total = data->count + data->rrsig_count;
484 if(subtract >= 0 && data->ttl > subtract)
485 data->ttl -= subtract;
487 for(i=0; i<total; i++) {
488 if(subtract >= 0 && data->rr_ttl[i] > subtract)
489 data->rr_ttl[i] -= subtract;
490 else data->rr_ttl[i] = 0;
492 data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0;
495 /* Adjust the TTL of a DNS message and its RRs by 'adjust'. If 'adjust' is
496 * negative, set the TTLs to 0. */
498 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
501 if(adjust >= 0 && msg->rep->ttl > adjust)
502 msg->rep->ttl -= adjust;
505 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
506 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
508 for(i=0; i<msg->rep->rrset_count; i++) {
509 packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
510 rep->rrsets[i]->entry.data, adjust);
514 /** convert dns message in buffer to return_msg */
516 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
518 struct msg_parse* prs;
519 struct edns_data edns;
520 struct edns_option* ede;
521 uint64_t timestamp, expiry;
523 size_t lim = sldns_buffer_limit(buf);
524 if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
525 return 0; /* too short */
527 /* remove timestamp and expiry from end */
528 sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
529 sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
530 ×tamp, sizeof(timestamp));
531 expiry = be64toh(expiry);
532 timestamp = be64toh(timestamp);
534 /* parse DNS packet */
535 regional_free_all(qstate->env->scratch);
536 prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
537 sizeof(struct msg_parse));
539 return 0; /* out of memory */
540 memset(prs, 0, sizeof(*prs));
541 memset(&edns, 0, sizeof(edns));
542 sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
543 if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
544 sldns_buffer_set_limit(buf, lim);
547 if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) !=
548 LDNS_RCODE_NOERROR) {
549 sldns_buffer_set_limit(buf, lim);
553 qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
554 sldns_buffer_set_limit(buf, lim);
555 if(!qstate->return_msg)
558 /* We find the EDE in the in-list after parsing */
559 if(qstate->env->cfg->ede &&
560 (ede = edns_opt_list_find(edns.opt_list_in, LDNS_EDNS_EDE))) {
561 if(ede->opt_len >= 2) {
562 qstate->return_msg->rep->reason_bogus =
563 sldns_read_uint16(ede->opt_data);
565 /* allocate space and store the error string and it's size */
566 if(ede->opt_len > 2) {
567 size_t ede_len = ede->opt_len - 2;
568 qstate->return_msg->rep->reason_bogus_str = regional_alloc(
569 qstate->region, sizeof(char) * (ede_len+1));
570 memcpy(qstate->return_msg->rep->reason_bogus_str,
571 ede->opt_data+2, ede_len);
572 qstate->return_msg->rep->reason_bogus_str[ede_len] = 0;
576 qstate->return_rcode = LDNS_RCODE_NOERROR;
578 /* see how much of the TTL expired, and remove it */
579 if(*qstate->env->now <= (time_t)timestamp) {
580 verbose(VERB_ALGO, "cachedb msg adjust by zero");
581 return 1; /* message from the future (clock skew?) */
583 adjust = *qstate->env->now - (time_t)timestamp;
584 if(qstate->return_msg->rep->ttl < adjust) {
585 verbose(VERB_ALGO, "cachedb msg expired");
586 /* If serve-expired is enabled, we still use an expired message
587 * setting the TTL to 0. */
588 if(!qstate->env->cfg->serve_expired ||
589 (FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
590 != LDNS_RCODE_NOERROR &&
591 FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
592 != LDNS_RCODE_NXDOMAIN &&
593 FLAGS_GET_RCODE(qstate->return_msg->rep->flags)
594 != LDNS_RCODE_YXDOMAIN))
595 return 0; /* message expired */
599 verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
600 adjust_msg_ttl(qstate->return_msg, adjust);
602 /* Similar to the unbound worker, if serve-expired is enabled and
603 * the msg would be considered to be expired, mark the state so a
604 * refetch will be scheduled. The comparison between 'expiry' and
605 * 'now' should be redundant given how these values were calculated,
606 * but we check it just in case as does good_expiry_and_qinfo(). */
607 if(qstate->env->cfg->serve_expired &&
608 (adjust == -1 || (time_t)expiry < *qstate->env->now)) {
609 qstate->need_refetch = 1;
616 * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
617 * return true if lookup was successful.
620 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
622 char key[(CACHEDB_HASHSIZE/8)*2+1];
623 calc_hash(qstate, key, sizeof(key));
625 /* call backend to fetch data for key into scratch buffer */
626 if( !(*ie->backend->lookup)(qstate->env, ie, key,
627 qstate->env->scratch_buffer)) {
631 /* check expiry date and check if query-data matches */
632 if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
636 /* parse dns message into return_msg */
637 if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
644 * Store the qstate.return_msg in extcache for key qstate.info
647 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
649 char key[(CACHEDB_HASHSIZE/8)*2+1];
650 calc_hash(qstate, key, sizeof(key));
652 /* prepare data in scratch buffer */
653 if(!prep_data(qstate, qstate->env->scratch_buffer))
657 (*ie->backend->store)(qstate->env, ie, key,
658 sldns_buffer_begin(qstate->env->scratch_buffer),
659 sldns_buffer_limit(qstate->env->scratch_buffer),
660 qstate->return_msg->rep->ttl);
664 * See if unbound's internal cache can answer the query
667 cachedb_intcache_lookup(struct module_qstate* qstate, struct cachedb_env* cde)
669 uint8_t* dpname=NULL;
672 /* for testframe bypass this lookup */
673 if(cde->backend == &testframe_backend) {
676 if(iter_stub_fwd_no_cache(qstate, &qstate->qinfo,
677 &dpname, &dpnamelen))
678 return 0; /* no cache for these queries */
679 msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
680 qstate->qinfo.qname_len, qstate->qinfo.qtype,
681 qstate->qinfo.qclass, qstate->query_flags,
682 qstate->region, qstate->env->scratch,
683 1, /* no partial messages with only a CNAME */
686 if(!msg && qstate->env->neg_cache &&
687 iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
688 /* lookup in negative cache; may result in
689 * NOERROR/NODATA or NXDOMAIN answers that need validation */
690 msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
691 qstate->region, qstate->env->rrset_cache,
692 qstate->env->scratch_buffer,
693 *qstate->env->now, 1/*add SOA*/, NULL,
698 /* this is the returned msg */
699 qstate->return_rcode = LDNS_RCODE_NOERROR;
700 qstate->return_msg = msg;
705 * Store query into the internal cache of unbound.
708 cachedb_intcache_store(struct module_qstate* qstate)
710 uint32_t store_flags = qstate->query_flags;
712 if(qstate->env->cfg->serve_expired)
713 store_flags |= DNSCACHE_STORE_ZEROTTL;
714 if(!qstate->return_msg)
716 (void)dns_cache_store(qstate->env, &qstate->qinfo,
717 qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
718 qstate->region, store_flags, qstate->qstarttime);
722 * Handle a cachedb module event with a query
723 * @param qstate: query state (from the mesh), passed between modules.
724 * contains qstate->env module environment with global caches and so on.
725 * @param iq: query state specific for this module. per-query.
726 * @param ie: environment specific for this module. global.
727 * @param id: module id.
730 cachedb_handle_query(struct module_qstate* qstate,
731 struct cachedb_qstate* ATTR_UNUSED(iq),
732 struct cachedb_env* ie, int id)
734 qstate->is_cachedb_answer = 0;
735 /* check if we are enabled, and skip if so */
737 /* pass request to next module */
738 qstate->ext_state[id] = module_wait_module;
742 if(qstate->blacklist || qstate->no_cache_lookup) {
743 /* cache is blacklisted or we are instructed from edns to not look */
744 /* pass request to next module */
745 qstate->ext_state[id] = module_wait_module;
749 /* lookup inside unbound's internal cache.
750 * This does not look for expired entries. */
751 if(cachedb_intcache_lookup(qstate, ie)) {
752 if(verbosity >= VERB_ALGO) {
753 if(qstate->return_msg->rep)
754 log_dns_msg("cachedb internal cache lookup",
755 &qstate->return_msg->qinfo,
756 qstate->return_msg->rep);
757 else log_info("cachedb internal cache lookup: rcode %s",
758 sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
759 ?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
762 /* we are done with the query */
763 qstate->ext_state[id] = module_finished;
767 /* ask backend cache to see if we have data */
768 if(cachedb_extcache_lookup(qstate, ie)) {
769 if(verbosity >= VERB_ALGO)
770 log_dns_msg(ie->backend->name,
771 &qstate->return_msg->qinfo,
772 qstate->return_msg->rep);
773 /* store this result in internal cache */
774 cachedb_intcache_store(qstate);
775 /* In case we have expired data but there is a client timer for expired
776 * answers, pass execution to next module in order to try updating the
778 * TODO: this needs revisit. The expired data stored from cachedb has
779 * 0 TTL which is picked up by iterator later when looking in the cache.
780 * Document that ext cachedb does not work properly with
781 * serve_stale_reply_ttl yet. */
782 if(qstate->need_refetch && qstate->serve_expired_data &&
783 qstate->serve_expired_data->timer) {
784 qstate->return_msg = NULL;
785 qstate->ext_state[id] = module_wait_module;
788 qstate->is_cachedb_answer = 1;
789 /* we are done with the query */
790 qstate->ext_state[id] = module_finished;
794 /* no cache fetches */
795 /* pass request to next module */
796 qstate->ext_state[id] = module_wait_module;
800 * Handle a cachedb module event with a response from the iterator.
801 * @param qstate: query state (from the mesh), passed between modules.
802 * contains qstate->env module environment with global caches and so on.
803 * @param iq: query state specific for this module. per-query.
804 * @param ie: environment specific for this module. global.
805 * @param id: module id.
808 cachedb_handle_response(struct module_qstate* qstate,
809 struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
811 qstate->is_cachedb_answer = 0;
812 /* check if we are not enabled or instructed to not cache, and skip */
813 if(!ie->enabled || qstate->no_cache_store) {
814 /* we are done with the query */
815 qstate->ext_state[id] = module_finished;
818 if(qstate->env->cfg->cachedb_no_store) {
819 /* do not store the item in the external cache */
820 qstate->ext_state[id] = module_finished;
824 /* store the item into the backend cache */
825 cachedb_extcache_store(qstate, ie);
827 /* we are done with the query */
828 qstate->ext_state[id] = module_finished;
832 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
833 struct outbound_entry* outbound)
835 struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
836 struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
837 verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
838 id, strextstate(qstate->ext_state[id]), strmodulevent(event));
839 if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
842 /* perform cachedb state machine */
843 if((event == module_event_new || event == module_event_pass) &&
845 if(!cachedb_new(qstate, id)) {
846 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
849 iq = (struct cachedb_qstate*)qstate->minfo[id];
851 if(iq && (event == module_event_pass || event == module_event_new)) {
852 cachedb_handle_query(qstate, iq, ie, id);
855 if(iq && (event == module_event_moddone)) {
856 cachedb_handle_response(qstate, iq, ie, id);
860 /* cachedb does not need to process responses at this time
862 cachedb_process_response(qstate, iq, ie, id, outbound, event);
866 if(event == module_event_error) {
867 verbose(VERB_ALGO, "got called with event error, giving up");
868 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
871 if(!iq && (event == module_event_moddone)) {
872 /* during priming, module done but we never started */
873 qstate->ext_state[id] = module_finished;
877 log_err("bad event for cachedb");
878 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
882 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
883 int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
885 /* cachedb does not use subordinate requests at this time */
886 verbose(VERB_ALGO, "cachedb inform_super was called");
890 cachedb_clear(struct module_qstate* qstate, int id)
892 struct cachedb_qstate* iq;
895 iq = (struct cachedb_qstate*)qstate->minfo[id];
897 /* free contents of iq */
900 qstate->minfo[id] = NULL;
904 cachedb_get_mem(struct module_env* env, int id)
906 struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
909 return sizeof(*ie); /* TODO - more mem */
913 * The cachedb function block
915 static struct module_func_block cachedb_block = {
917 &cachedb_init, &cachedb_deinit, &cachedb_operate,
918 &cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
921 struct module_func_block*
922 cachedb_get_funcblock(void)
924 return &cachedb_block;
926 #endif /* USE_CACHEDB */