1e2d15004SDag-Erling Smørgrav /* 2e2d15004SDag-Erling Smørgrav * cachedb/cachedb.c - cache from a database external to the program module 3e2d15004SDag-Erling Smørgrav * 4e2d15004SDag-Erling Smørgrav * Copyright (c) 2016, NLnet Labs. All rights reserved. 5e2d15004SDag-Erling Smørgrav * 6e2d15004SDag-Erling Smørgrav * This software is open source. 7e2d15004SDag-Erling Smørgrav * 8e2d15004SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9e2d15004SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10e2d15004SDag-Erling Smørgrav * are met: 11e2d15004SDag-Erling Smørgrav * 12e2d15004SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13e2d15004SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14e2d15004SDag-Erling Smørgrav * 15e2d15004SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16e2d15004SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17e2d15004SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18e2d15004SDag-Erling Smørgrav * 19e2d15004SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20e2d15004SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21e2d15004SDag-Erling Smørgrav * specific prior written permission. 22e2d15004SDag-Erling Smørgrav * 23e2d15004SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24e2d15004SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25e2d15004SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26e2d15004SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27e2d15004SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28e2d15004SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29e2d15004SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30e2d15004SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31e2d15004SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32e2d15004SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33e2d15004SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34e2d15004SDag-Erling Smørgrav */ 35e2d15004SDag-Erling Smørgrav 36e2d15004SDag-Erling Smørgrav /** 37e2d15004SDag-Erling Smørgrav * \file 38e2d15004SDag-Erling Smørgrav * 39e2d15004SDag-Erling Smørgrav * This file contains a module that uses an external database to cache 40e2d15004SDag-Erling Smørgrav * dns responses. 41e2d15004SDag-Erling Smørgrav */ 42e2d15004SDag-Erling Smørgrav 43e2d15004SDag-Erling Smørgrav #include "config.h" 44e2d15004SDag-Erling Smørgrav #ifdef USE_CACHEDB 45e2d15004SDag-Erling Smørgrav #include "cachedb/cachedb.h" 460fb34990SDag-Erling Smørgrav #include "cachedb/redis.h" 47e2d15004SDag-Erling Smørgrav #include "util/regional.h" 48e2d15004SDag-Erling Smørgrav #include "util/net_help.h" 49e2d15004SDag-Erling Smørgrav #include "util/config_file.h" 50e2d15004SDag-Erling Smørgrav #include "util/data/msgreply.h" 51e2d15004SDag-Erling Smørgrav #include "util/data/msgencode.h" 52e2d15004SDag-Erling Smørgrav #include "services/cache/dns.h" 53*335c7cdaSCy Schubert #include "services/mesh.h" 54*335c7cdaSCy Schubert #include "services/modstack.h" 55e2d15004SDag-Erling Smørgrav #include "validator/val_neg.h" 56e2d15004SDag-Erling Smørgrav #include "validator/val_secalgo.h" 57e2d15004SDag-Erling Smørgrav #include "iterator/iter_utils.h" 58e2d15004SDag-Erling Smørgrav #include "sldns/parseutil.h" 59e2d15004SDag-Erling Smørgrav #include "sldns/wire2str.h" 60e2d15004SDag-Erling Smørgrav #include "sldns/sbuffer.h" 61e2d15004SDag-Erling Smørgrav 620fb34990SDag-Erling Smørgrav /* header file for htobe64 */ 630fb34990SDag-Erling Smørgrav #ifdef HAVE_ENDIAN_H 640fb34990SDag-Erling Smørgrav # include <endian.h> 650fb34990SDag-Erling Smørgrav #endif 660fb34990SDag-Erling Smørgrav #ifdef HAVE_SYS_ENDIAN_H 670fb34990SDag-Erling Smørgrav # include <sys/endian.h> 680fb34990SDag-Erling Smørgrav #endif 69091e9e46SCy Schubert 70091e9e46SCy Schubert #ifndef HAVE_HTOBE64 710fb34990SDag-Erling Smørgrav # ifdef HAVE_LIBKERN_OSBYTEORDER_H 720fb34990SDag-Erling Smørgrav /* In practice this is specific to MacOS X. We assume it doesn't have 730fb34990SDag-Erling Smørgrav * htobe64/be64toh but has alternatives with a different name. */ 740fb34990SDag-Erling Smørgrav # include <libkern/OSByteOrder.h> 750fb34990SDag-Erling Smørgrav # define htobe64(x) OSSwapHostToBigInt64(x) 760fb34990SDag-Erling Smørgrav # define be64toh(x) OSSwapBigToHostInt64(x) 77091e9e46SCy Schubert # else 78091e9e46SCy Schubert /* not OSX */ 79e86b9096SDag-Erling Smørgrav /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */ 80e86b9096SDag-Erling Smørgrav # if __BIG_ENDIAN__ 81e86b9096SDag-Erling Smørgrav # define be64toh(n) (n) 82e86b9096SDag-Erling Smørgrav # define htobe64(n) (n) 83e86b9096SDag-Erling Smørgrav # else 84e86b9096SDag-Erling Smørgrav # define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32)) 85e86b9096SDag-Erling Smørgrav # define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32)) 86091e9e46SCy Schubert # endif /* _ENDIAN */ 87091e9e46SCy Schubert # endif /* HAVE_LIBKERN_OSBYTEORDER_H */ 88091e9e46SCy Schubert #endif /* HAVE_BE64TOH */ 89e86b9096SDag-Erling Smørgrav 90e2d15004SDag-Erling Smørgrav /** the unit test testframe for cachedb, its module state contains 91e2d15004SDag-Erling Smørgrav * a cache for a couple queries (in memory). */ 92e2d15004SDag-Erling Smørgrav struct testframe_moddata { 93971980c3SDag-Erling Smørgrav /** lock for mutex */ 94971980c3SDag-Erling Smørgrav lock_basic_type lock; 95e2d15004SDag-Erling Smørgrav /** key for single stored data element, NULL if none */ 96e2d15004SDag-Erling Smørgrav char* stored_key; 97e2d15004SDag-Erling Smørgrav /** data for single stored data element, NULL if none */ 98e2d15004SDag-Erling Smørgrav uint8_t* stored_data; 99e2d15004SDag-Erling Smørgrav /** length of stored data */ 100e2d15004SDag-Erling Smørgrav size_t stored_datalen; 101e2d15004SDag-Erling Smørgrav }; 102e2d15004SDag-Erling Smørgrav 103e2d15004SDag-Erling Smørgrav static int 104e2d15004SDag-Erling Smørgrav testframe_init(struct module_env* env, struct cachedb_env* cachedb_env) 105e2d15004SDag-Erling Smørgrav { 106971980c3SDag-Erling Smørgrav struct testframe_moddata* d; 107e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "testframe_init"); 108971980c3SDag-Erling Smørgrav d = (struct testframe_moddata*)calloc(1, 109e2d15004SDag-Erling Smørgrav sizeof(struct testframe_moddata)); 110971980c3SDag-Erling Smørgrav cachedb_env->backend_data = (void*)d; 111e2d15004SDag-Erling Smørgrav if(!cachedb_env->backend_data) { 112e2d15004SDag-Erling Smørgrav log_err("out of memory"); 113e2d15004SDag-Erling Smørgrav return 0; 114e2d15004SDag-Erling Smørgrav } 1158f76bb7dSCy Schubert /* Register an EDNS option (65534) to bypass the worker cache lookup 1168f76bb7dSCy Schubert * for testing */ 1178f76bb7dSCy Schubert if(!edns_register_option(LDNS_EDNS_UNBOUND_CACHEDB_TESTFRAME_TEST, 1188f76bb7dSCy Schubert 1 /* bypass cache */, 1198f76bb7dSCy Schubert 0 /* no aggregation */, env)) { 1208f76bb7dSCy Schubert log_err("testframe_init, could not register test opcode"); 1218f76bb7dSCy Schubert free(d); 1228f76bb7dSCy Schubert return 0; 1238f76bb7dSCy Schubert } 124971980c3SDag-Erling Smørgrav lock_basic_init(&d->lock); 125971980c3SDag-Erling Smørgrav lock_protect(&d->lock, d, sizeof(*d)); 126e2d15004SDag-Erling Smørgrav return 1; 127e2d15004SDag-Erling Smørgrav } 128e2d15004SDag-Erling Smørgrav 129e2d15004SDag-Erling Smørgrav static void 130e2d15004SDag-Erling Smørgrav testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env) 131e2d15004SDag-Erling Smørgrav { 132e2d15004SDag-Erling Smørgrav struct testframe_moddata* d = (struct testframe_moddata*) 133e2d15004SDag-Erling Smørgrav cachedb_env->backend_data; 134e2d15004SDag-Erling Smørgrav (void)env; 135e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "testframe_deinit"); 136e2d15004SDag-Erling Smørgrav if(!d) 137e2d15004SDag-Erling Smørgrav return; 138971980c3SDag-Erling Smørgrav lock_basic_destroy(&d->lock); 139e2d15004SDag-Erling Smørgrav free(d->stored_key); 140e2d15004SDag-Erling Smørgrav free(d->stored_data); 141e2d15004SDag-Erling Smørgrav free(d); 142e2d15004SDag-Erling Smørgrav } 143e2d15004SDag-Erling Smørgrav 144e2d15004SDag-Erling Smørgrav static int 145e2d15004SDag-Erling Smørgrav testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env, 146e2d15004SDag-Erling Smørgrav char* key, struct sldns_buffer* result_buffer) 147e2d15004SDag-Erling Smørgrav { 148e2d15004SDag-Erling Smørgrav struct testframe_moddata* d = (struct testframe_moddata*) 149e2d15004SDag-Erling Smørgrav cachedb_env->backend_data; 150e2d15004SDag-Erling Smørgrav (void)env; 151e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "testframe_lookup of %s", key); 152971980c3SDag-Erling Smørgrav lock_basic_lock(&d->lock); 153e2d15004SDag-Erling Smørgrav if(d->stored_key && strcmp(d->stored_key, key) == 0) { 154971980c3SDag-Erling Smørgrav if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) { 155971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 156e2d15004SDag-Erling Smørgrav return 0; /* too large */ 157971980c3SDag-Erling Smørgrav } 158e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "testframe_lookup found %d bytes", 159e2d15004SDag-Erling Smørgrav (int)d->stored_datalen); 160e2d15004SDag-Erling Smørgrav sldns_buffer_clear(result_buffer); 161e2d15004SDag-Erling Smørgrav sldns_buffer_write(result_buffer, d->stored_data, 162e2d15004SDag-Erling Smørgrav d->stored_datalen); 163e2d15004SDag-Erling Smørgrav sldns_buffer_flip(result_buffer); 164971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 165e2d15004SDag-Erling Smørgrav return 1; 166e2d15004SDag-Erling Smørgrav } 167971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 168e2d15004SDag-Erling Smørgrav return 0; 169e2d15004SDag-Erling Smørgrav } 170e2d15004SDag-Erling Smørgrav 171e2d15004SDag-Erling Smørgrav static void 172e2d15004SDag-Erling Smørgrav testframe_store(struct module_env* env, struct cachedb_env* cachedb_env, 17325039b37SCy Schubert char* key, uint8_t* data, size_t data_len, time_t ATTR_UNUSED(ttl)) 174e2d15004SDag-Erling Smørgrav { 175e2d15004SDag-Erling Smørgrav struct testframe_moddata* d = (struct testframe_moddata*) 176e2d15004SDag-Erling Smørgrav cachedb_env->backend_data; 177e2d15004SDag-Erling Smørgrav (void)env; 178971980c3SDag-Erling Smørgrav lock_basic_lock(&d->lock); 179e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len); 180e2d15004SDag-Erling Smørgrav 181e2d15004SDag-Erling Smørgrav /* free old data element (if any) */ 182e2d15004SDag-Erling Smørgrav free(d->stored_key); 183e2d15004SDag-Erling Smørgrav d->stored_key = NULL; 184e2d15004SDag-Erling Smørgrav free(d->stored_data); 185e2d15004SDag-Erling Smørgrav d->stored_data = NULL; 186e2d15004SDag-Erling Smørgrav d->stored_datalen = 0; 187e2d15004SDag-Erling Smørgrav 188e2d15004SDag-Erling Smørgrav d->stored_data = memdup(data, data_len); 189e2d15004SDag-Erling Smørgrav if(!d->stored_data) { 190971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 191e2d15004SDag-Erling Smørgrav log_err("out of memory"); 192e2d15004SDag-Erling Smørgrav return; 193e2d15004SDag-Erling Smørgrav } 194e2d15004SDag-Erling Smørgrav d->stored_datalen = data_len; 195e2d15004SDag-Erling Smørgrav d->stored_key = strdup(key); 196e2d15004SDag-Erling Smørgrav if(!d->stored_key) { 197e2d15004SDag-Erling Smørgrav free(d->stored_data); 198e2d15004SDag-Erling Smørgrav d->stored_data = NULL; 199e2d15004SDag-Erling Smørgrav d->stored_datalen = 0; 200971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 201e2d15004SDag-Erling Smørgrav return; 202e2d15004SDag-Erling Smørgrav } 203971980c3SDag-Erling Smørgrav lock_basic_unlock(&d->lock); 204e2d15004SDag-Erling Smørgrav /* (key,data) successfully stored */ 205e2d15004SDag-Erling Smørgrav } 206e2d15004SDag-Erling Smørgrav 207e2d15004SDag-Erling Smørgrav /** The testframe backend is for unit tests */ 208e2d15004SDag-Erling Smørgrav static struct cachedb_backend testframe_backend = { "testframe", 209e2d15004SDag-Erling Smørgrav testframe_init, testframe_deinit, testframe_lookup, testframe_store 210e2d15004SDag-Erling Smørgrav }; 211e2d15004SDag-Erling Smørgrav 212e2d15004SDag-Erling Smørgrav /** find a particular backend from possible backends */ 213e2d15004SDag-Erling Smørgrav static struct cachedb_backend* 214e2d15004SDag-Erling Smørgrav cachedb_find_backend(const char* str) 215e2d15004SDag-Erling Smørgrav { 2160fb34990SDag-Erling Smørgrav #ifdef USE_REDIS 2170fb34990SDag-Erling Smørgrav if(strcmp(str, redis_backend.name) == 0) 2180fb34990SDag-Erling Smørgrav return &redis_backend; 2190fb34990SDag-Erling Smørgrav #endif 220e2d15004SDag-Erling Smørgrav if(strcmp(str, testframe_backend.name) == 0) 221e2d15004SDag-Erling Smørgrav return &testframe_backend; 222e2d15004SDag-Erling Smørgrav /* TODO add more backends here */ 223e2d15004SDag-Erling Smørgrav return NULL; 224e2d15004SDag-Erling Smørgrav } 225e2d15004SDag-Erling Smørgrav 226e2d15004SDag-Erling Smørgrav /** apply configuration to cachedb module 'global' state */ 227e2d15004SDag-Erling Smørgrav static int 228e2d15004SDag-Erling Smørgrav cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg) 229e2d15004SDag-Erling Smørgrav { 230971980c3SDag-Erling Smørgrav const char* backend_str = cfg->cachedb_backend; 2318f76bb7dSCy Schubert if(!backend_str || *backend_str==0) 2328f76bb7dSCy Schubert return 1; 233e2d15004SDag-Erling Smørgrav cachedb_env->backend = cachedb_find_backend(backend_str); 234e2d15004SDag-Erling Smørgrav if(!cachedb_env->backend) { 235971980c3SDag-Erling Smørgrav log_err("cachedb: cannot find backend name '%s'", backend_str); 236c7f4d7adSDag-Erling Smørgrav return 0; 237e2d15004SDag-Erling Smørgrav } 238971980c3SDag-Erling Smørgrav 239e2d15004SDag-Erling Smørgrav /* TODO see if more configuration needs to be applied or not */ 240e2d15004SDag-Erling Smørgrav return 1; 241e2d15004SDag-Erling Smørgrav } 242e2d15004SDag-Erling Smørgrav 243e2d15004SDag-Erling Smørgrav int 244e2d15004SDag-Erling Smørgrav cachedb_init(struct module_env* env, int id) 245e2d15004SDag-Erling Smørgrav { 246e2d15004SDag-Erling Smørgrav struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1, 247e2d15004SDag-Erling Smørgrav sizeof(struct cachedb_env)); 248e2d15004SDag-Erling Smørgrav if(!cachedb_env) { 249e2d15004SDag-Erling Smørgrav log_err("malloc failure"); 250e2d15004SDag-Erling Smørgrav return 0; 251e2d15004SDag-Erling Smørgrav } 252e2d15004SDag-Erling Smørgrav env->modinfo[id] = (void*)cachedb_env; 253e2d15004SDag-Erling Smørgrav if(!cachedb_apply_cfg(cachedb_env, env->cfg)) { 254e2d15004SDag-Erling Smørgrav log_err("cachedb: could not apply configuration settings."); 2550eefd307SCy Schubert free(cachedb_env); 2560eefd307SCy Schubert env->modinfo[id] = NULL; 257e2d15004SDag-Erling Smørgrav return 0; 258e2d15004SDag-Erling Smørgrav } 259e2d15004SDag-Erling Smørgrav /* see if a backend is selected */ 260e2d15004SDag-Erling Smørgrav if(!cachedb_env->backend || !cachedb_env->backend->name) 261e2d15004SDag-Erling Smørgrav return 1; 262e2d15004SDag-Erling Smørgrav if(!(*cachedb_env->backend->init)(env, cachedb_env)) { 263e2d15004SDag-Erling Smørgrav log_err("cachedb: could not init %s backend", 264e2d15004SDag-Erling Smørgrav cachedb_env->backend->name); 2650eefd307SCy Schubert free(cachedb_env); 2660eefd307SCy Schubert env->modinfo[id] = NULL; 267e2d15004SDag-Erling Smørgrav return 0; 268e2d15004SDag-Erling Smørgrav } 269e2d15004SDag-Erling Smørgrav cachedb_env->enabled = 1; 270e2d15004SDag-Erling Smørgrav return 1; 271e2d15004SDag-Erling Smørgrav } 272e2d15004SDag-Erling Smørgrav 273e2d15004SDag-Erling Smørgrav void 274e2d15004SDag-Erling Smørgrav cachedb_deinit(struct module_env* env, int id) 275e2d15004SDag-Erling Smørgrav { 276e2d15004SDag-Erling Smørgrav struct cachedb_env* cachedb_env; 277e2d15004SDag-Erling Smørgrav if(!env || !env->modinfo[id]) 278e2d15004SDag-Erling Smørgrav return; 279e2d15004SDag-Erling Smørgrav cachedb_env = (struct cachedb_env*)env->modinfo[id]; 280e2d15004SDag-Erling Smørgrav if(cachedb_env->enabled) { 281e2d15004SDag-Erling Smørgrav (*cachedb_env->backend->deinit)(env, cachedb_env); 282e2d15004SDag-Erling Smørgrav } 283e2d15004SDag-Erling Smørgrav free(cachedb_env); 284e2d15004SDag-Erling Smørgrav env->modinfo[id] = NULL; 285e2d15004SDag-Erling Smørgrav } 286e2d15004SDag-Erling Smørgrav 287e2d15004SDag-Erling Smørgrav /** new query for cachedb */ 288e2d15004SDag-Erling Smørgrav static int 289e2d15004SDag-Erling Smørgrav cachedb_new(struct module_qstate* qstate, int id) 290e2d15004SDag-Erling Smørgrav { 291e2d15004SDag-Erling Smørgrav struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc( 292e2d15004SDag-Erling Smørgrav qstate->region, sizeof(struct cachedb_qstate)); 293e2d15004SDag-Erling Smørgrav qstate->minfo[id] = iq; 294e2d15004SDag-Erling Smørgrav if(!iq) 295e2d15004SDag-Erling Smørgrav return 0; 296e2d15004SDag-Erling Smørgrav memset(iq, 0, sizeof(*iq)); 297e2d15004SDag-Erling Smørgrav /* initialise it */ 298e2d15004SDag-Erling Smørgrav /* TODO */ 299e2d15004SDag-Erling Smørgrav 300e2d15004SDag-Erling Smørgrav return 1; 301e2d15004SDag-Erling Smørgrav } 302e2d15004SDag-Erling Smørgrav 303e2d15004SDag-Erling Smørgrav /** 304e2d15004SDag-Erling Smørgrav * Return an error 305e2d15004SDag-Erling Smørgrav * @param qstate: our query state 306e2d15004SDag-Erling Smørgrav * @param id: module id 307e2d15004SDag-Erling Smørgrav * @param rcode: error code (DNS errcode). 308e2d15004SDag-Erling Smørgrav * @return: 0 for use by caller, to make notation easy, like: 309e2d15004SDag-Erling Smørgrav * return error_response(..). 310e2d15004SDag-Erling Smørgrav */ 311e2d15004SDag-Erling Smørgrav static int 312e2d15004SDag-Erling Smørgrav error_response(struct module_qstate* qstate, int id, int rcode) 313e2d15004SDag-Erling Smørgrav { 314e2d15004SDag-Erling Smørgrav verbose(VERB_QUERY, "return error response %s", 315e2d15004SDag-Erling Smørgrav sldns_lookup_by_id(sldns_rcodes, rcode)? 316e2d15004SDag-Erling Smørgrav sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??"); 317e2d15004SDag-Erling Smørgrav qstate->return_rcode = rcode; 318e2d15004SDag-Erling Smørgrav qstate->return_msg = NULL; 319e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 320e2d15004SDag-Erling Smørgrav return 0; 321e2d15004SDag-Erling Smørgrav } 322e2d15004SDag-Erling Smørgrav 323e2d15004SDag-Erling Smørgrav /** 324e2d15004SDag-Erling Smørgrav * Hash the query name, type, class and dbacess-secret into lookup buffer. 325e2d15004SDag-Erling Smørgrav * @param qstate: query state with query info 326e2d15004SDag-Erling Smørgrav * and env->cfg with secret. 327e2d15004SDag-Erling Smørgrav * @param buf: returned buffer with hash to lookup 328e2d15004SDag-Erling Smørgrav * @param len: length of the buffer. 329e2d15004SDag-Erling Smørgrav */ 330e2d15004SDag-Erling Smørgrav static void 331e2d15004SDag-Erling Smørgrav calc_hash(struct module_qstate* qstate, char* buf, size_t len) 332e2d15004SDag-Erling Smørgrav { 333e2d15004SDag-Erling Smørgrav uint8_t clear[1024]; 334e2d15004SDag-Erling Smørgrav size_t clen = 0; 335e2d15004SDag-Erling Smørgrav uint8_t hash[CACHEDB_HASHSIZE/8]; 336e2d15004SDag-Erling Smørgrav const char* hex = "0123456789ABCDEF"; 337091e9e46SCy Schubert const char* secret = qstate->env->cfg->cachedb_secret; 338e2d15004SDag-Erling Smørgrav size_t i; 339e2d15004SDag-Erling Smørgrav 340e2d15004SDag-Erling Smørgrav /* copy the hash info into the clear buffer */ 341e2d15004SDag-Erling Smørgrav if(clen + qstate->qinfo.qname_len < sizeof(clear)) { 342e2d15004SDag-Erling Smørgrav memmove(clear+clen, qstate->qinfo.qname, 343e2d15004SDag-Erling Smørgrav qstate->qinfo.qname_len); 344e2d15004SDag-Erling Smørgrav clen += qstate->qinfo.qname_len; 345e2d15004SDag-Erling Smørgrav } 346e2d15004SDag-Erling Smørgrav if(clen + 4 < sizeof(clear)) { 347e2d15004SDag-Erling Smørgrav uint16_t t = htons(qstate->qinfo.qtype); 348e2d15004SDag-Erling Smørgrav uint16_t c = htons(qstate->qinfo.qclass); 349e2d15004SDag-Erling Smørgrav memmove(clear+clen, &t, 2); 350e2d15004SDag-Erling Smørgrav memmove(clear+clen+2, &c, 2); 351e2d15004SDag-Erling Smørgrav clen += 4; 352e2d15004SDag-Erling Smørgrav } 353e2d15004SDag-Erling Smørgrav if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) { 354e2d15004SDag-Erling Smørgrav memmove(clear+clen, secret, strlen(secret)); 355e2d15004SDag-Erling Smørgrav clen += strlen(secret); 356e2d15004SDag-Erling Smørgrav } 357e2d15004SDag-Erling Smørgrav 358e2d15004SDag-Erling Smørgrav /* hash the buffer */ 359e2d15004SDag-Erling Smørgrav secalgo_hash_sha256(clear, clen, hash); 3600eefd307SCy Schubert #ifdef HAVE_EXPLICIT_BZERO 3610eefd307SCy Schubert explicit_bzero(clear, clen); 3620eefd307SCy Schubert #else 363e2d15004SDag-Erling Smørgrav memset(clear, 0, clen); 3640eefd307SCy Schubert #endif 365e2d15004SDag-Erling Smørgrav 366e2d15004SDag-Erling Smørgrav /* hex encode output for portability (some online dbs need 367e2d15004SDag-Erling Smørgrav * no nulls, no control characters, and so on) */ 368e2d15004SDag-Erling Smørgrav log_assert(len >= sizeof(hash)*2 + 1); 369e2d15004SDag-Erling Smørgrav (void)len; 370e2d15004SDag-Erling Smørgrav for(i=0; i<sizeof(hash); i++) { 371e2d15004SDag-Erling Smørgrav buf[i*2] = hex[(hash[i]&0xf0)>>4]; 372e2d15004SDag-Erling Smørgrav buf[i*2+1] = hex[hash[i]&0x0f]; 373e2d15004SDag-Erling Smørgrav } 374e2d15004SDag-Erling Smørgrav buf[sizeof(hash)*2] = 0; 375e2d15004SDag-Erling Smørgrav } 376e2d15004SDag-Erling Smørgrav 377e2d15004SDag-Erling Smørgrav /** convert data from return_msg into the data buffer */ 378e2d15004SDag-Erling Smørgrav static int 379e2d15004SDag-Erling Smørgrav prep_data(struct module_qstate* qstate, struct sldns_buffer* buf) 380e2d15004SDag-Erling Smørgrav { 381e2d15004SDag-Erling Smørgrav uint64_t timestamp, expiry; 382e2d15004SDag-Erling Smørgrav size_t oldlim; 383e2d15004SDag-Erling Smørgrav struct edns_data edns; 384e2d15004SDag-Erling Smørgrav memset(&edns, 0, sizeof(edns)); 385e2d15004SDag-Erling Smørgrav edns.edns_present = 1; 386e2d15004SDag-Erling Smørgrav edns.bits = EDNS_DO; 387e2d15004SDag-Erling Smørgrav edns.ext_rcode = 0; 388e2d15004SDag-Erling Smørgrav edns.edns_version = EDNS_ADVERTISED_VERSION; 389e2d15004SDag-Erling Smørgrav edns.udp_size = EDNS_ADVERTISED_SIZE; 390e2d15004SDag-Erling Smørgrav 391e2d15004SDag-Erling Smørgrav if(!qstate->return_msg || !qstate->return_msg->rep) 392e2d15004SDag-Erling Smørgrav return 0; 3931838dec3SCy Schubert /* do not store failures like SERVFAIL in the cachedb, this avoids 3941838dec3SCy Schubert * overwriting expired, valid, content with broken content. */ 3951838dec3SCy Schubert if(FLAGS_GET_RCODE(qstate->return_msg->rep->flags) != 3961838dec3SCy Schubert LDNS_RCODE_NOERROR && 3971838dec3SCy Schubert FLAGS_GET_RCODE(qstate->return_msg->rep->flags) != 3981838dec3SCy Schubert LDNS_RCODE_NXDOMAIN && 3991838dec3SCy Schubert FLAGS_GET_RCODE(qstate->return_msg->rep->flags) != 4001838dec3SCy Schubert LDNS_RCODE_YXDOMAIN) 4011838dec3SCy Schubert return 0; 4028a384985SDag-Erling Smørgrav /* We don't store the reply if its TTL is 0 unless serve-expired is 4038a384985SDag-Erling Smørgrav * enabled. Such a reply won't be reusable and simply be a waste for 4048a384985SDag-Erling Smørgrav * the backend. It's also compatible with the default behavior of 4058a384985SDag-Erling Smørgrav * dns_cache_store_msg(). */ 4068a384985SDag-Erling Smørgrav if(qstate->return_msg->rep->ttl == 0 && 4078a384985SDag-Erling Smørgrav !qstate->env->cfg->serve_expired) 4088a384985SDag-Erling Smørgrav return 0; 4098f76bb7dSCy Schubert 4108f76bb7dSCy Schubert /* The EDE is added to the out-list so it is encoded in the cached message */ 4118f76bb7dSCy Schubert if (qstate->env->cfg->ede && qstate->return_msg->rep->reason_bogus != LDNS_EDE_NONE) { 4128f76bb7dSCy Schubert edns_opt_list_append_ede(&edns.opt_list_out, qstate->env->scratch, 4138f76bb7dSCy Schubert qstate->return_msg->rep->reason_bogus, 4148f76bb7dSCy Schubert qstate->return_msg->rep->reason_bogus_str); 4158f76bb7dSCy Schubert } 4168f76bb7dSCy Schubert 417e2d15004SDag-Erling Smørgrav if(verbosity >= VERB_ALGO) 418e2d15004SDag-Erling Smørgrav log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo, 419e2d15004SDag-Erling Smørgrav qstate->return_msg->rep); 420e2d15004SDag-Erling Smørgrav if(!reply_info_answer_encode(&qstate->return_msg->qinfo, 421e2d15004SDag-Erling Smørgrav qstate->return_msg->rep, 0, qstate->query_flags, 422e2d15004SDag-Erling Smørgrav buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0)) 423e2d15004SDag-Erling Smørgrav return 0; 424e2d15004SDag-Erling Smørgrav 425e2d15004SDag-Erling Smørgrav /* TTLs in the return_msg are relative to time(0) so we have to 426e2d15004SDag-Erling Smørgrav * store that, we also store the smallest ttl in the packet+time(0) 427e2d15004SDag-Erling Smørgrav * as the packet expiry time */ 428e2d15004SDag-Erling Smørgrav /* qstate->return_msg->rep->ttl contains that relative shortest ttl */ 429e2d15004SDag-Erling Smørgrav timestamp = (uint64_t)*qstate->env->now; 430e2d15004SDag-Erling Smørgrav expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl; 431e2d15004SDag-Erling Smørgrav timestamp = htobe64(timestamp); 432e2d15004SDag-Erling Smørgrav expiry = htobe64(expiry); 433e2d15004SDag-Erling Smørgrav oldlim = sldns_buffer_limit(buf); 434e2d15004SDag-Erling Smørgrav if(oldlim + sizeof(timestamp)+sizeof(expiry) >= 435e2d15004SDag-Erling Smørgrav sldns_buffer_capacity(buf)) 436e2d15004SDag-Erling Smørgrav return 0; /* doesn't fit. */ 437e2d15004SDag-Erling Smørgrav sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry)); 438e2d15004SDag-Erling Smørgrav sldns_buffer_write_at(buf, oldlim, ×tamp, sizeof(timestamp)); 439e2d15004SDag-Erling Smørgrav sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry, 440e2d15004SDag-Erling Smørgrav sizeof(expiry)); 441e2d15004SDag-Erling Smørgrav 442e2d15004SDag-Erling Smørgrav return 1; 443e2d15004SDag-Erling Smørgrav } 444e2d15004SDag-Erling Smørgrav 445e2d15004SDag-Erling Smørgrav /** check expiry, return true if matches OK */ 446e2d15004SDag-Erling Smørgrav static int 447e2d15004SDag-Erling Smørgrav good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf) 448e2d15004SDag-Erling Smørgrav { 449e2d15004SDag-Erling Smørgrav uint64_t expiry; 450e2d15004SDag-Erling Smørgrav /* the expiry time is the last bytes of the buffer */ 451e2d15004SDag-Erling Smørgrav if(sldns_buffer_limit(buf) < sizeof(expiry)) 452e2d15004SDag-Erling Smørgrav return 0; 453e2d15004SDag-Erling Smørgrav sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry), 454e2d15004SDag-Erling Smørgrav &expiry, sizeof(expiry)); 455e2d15004SDag-Erling Smørgrav expiry = be64toh(expiry); 456e2d15004SDag-Erling Smørgrav 457091e9e46SCy Schubert /* Check if we are allowed to return expired entries: 458091e9e46SCy Schubert * - serve_expired needs to be set 459091e9e46SCy Schubert * - if SERVE_EXPIRED_TTL is set make sure that the record is not older 460091e9e46SCy Schubert * than that. */ 4618a384985SDag-Erling Smørgrav if((time_t)expiry < *qstate->env->now && 462091e9e46SCy Schubert (!qstate->env->cfg->serve_expired || 463091e9e46SCy Schubert (SERVE_EXPIRED_TTL && 464091e9e46SCy Schubert *qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL))) 465e2d15004SDag-Erling Smørgrav return 0; 466e2d15004SDag-Erling Smørgrav 467e2d15004SDag-Erling Smørgrav return 1; 468e2d15004SDag-Erling Smørgrav } 469e2d15004SDag-Erling Smørgrav 4708a384985SDag-Erling Smørgrav /* Adjust the TTL of the given RRset by 'subtract'. If 'subtract' is 4718a384985SDag-Erling Smørgrav * negative, set the TTL to 0. */ 472c7f4d7adSDag-Erling Smørgrav static void 473c7f4d7adSDag-Erling Smørgrav packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract) 474c7f4d7adSDag-Erling Smørgrav { 475c7f4d7adSDag-Erling Smørgrav size_t i; 476c7f4d7adSDag-Erling Smørgrav size_t total = data->count + data->rrsig_count; 4778a384985SDag-Erling Smørgrav if(subtract >= 0 && data->ttl > subtract) 478c7f4d7adSDag-Erling Smørgrav data->ttl -= subtract; 479c7f4d7adSDag-Erling Smørgrav else data->ttl = 0; 480c7f4d7adSDag-Erling Smørgrav for(i=0; i<total; i++) { 4818a384985SDag-Erling Smørgrav if(subtract >= 0 && data->rr_ttl[i] > subtract) 482c7f4d7adSDag-Erling Smørgrav data->rr_ttl[i] -= subtract; 483c7f4d7adSDag-Erling Smørgrav else data->rr_ttl[i] = 0; 484c7f4d7adSDag-Erling Smørgrav } 485f44e67d1SCy Schubert data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0; 486c7f4d7adSDag-Erling Smørgrav } 487c7f4d7adSDag-Erling Smørgrav 4888a384985SDag-Erling Smørgrav /* Adjust the TTL of a DNS message and its RRs by 'adjust'. If 'adjust' is 4898a384985SDag-Erling Smørgrav * negative, set the TTLs to 0. */ 490c7f4d7adSDag-Erling Smørgrav static void 491c7f4d7adSDag-Erling Smørgrav adjust_msg_ttl(struct dns_msg* msg, time_t adjust) 492c7f4d7adSDag-Erling Smørgrav { 493c7f4d7adSDag-Erling Smørgrav size_t i; 4948a384985SDag-Erling Smørgrav if(adjust >= 0 && msg->rep->ttl > adjust) 495c7f4d7adSDag-Erling Smørgrav msg->rep->ttl -= adjust; 496091e9e46SCy Schubert else 497091e9e46SCy Schubert msg->rep->ttl = 0; 498c7f4d7adSDag-Erling Smørgrav msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl); 4994c75e3aaSDag-Erling Smørgrav msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL; 500c7f4d7adSDag-Erling Smørgrav 501c7f4d7adSDag-Erling Smørgrav for(i=0; i<msg->rep->rrset_count; i++) { 502c7f4d7adSDag-Erling Smørgrav packed_rrset_ttl_subtract((struct packed_rrset_data*)msg-> 503c7f4d7adSDag-Erling Smørgrav rep->rrsets[i]->entry.data, adjust); 504c7f4d7adSDag-Erling Smørgrav } 505c7f4d7adSDag-Erling Smørgrav } 506c7f4d7adSDag-Erling Smørgrav 507*335c7cdaSCy Schubert /* Set the TTL of the given RRset to fixed value. */ 508*335c7cdaSCy Schubert static void 509*335c7cdaSCy Schubert packed_rrset_ttl_set(struct packed_rrset_data* data, time_t ttl) 510*335c7cdaSCy Schubert { 511*335c7cdaSCy Schubert size_t i; 512*335c7cdaSCy Schubert size_t total = data->count + data->rrsig_count; 513*335c7cdaSCy Schubert data->ttl = ttl; 514*335c7cdaSCy Schubert for(i=0; i<total; i++) { 515*335c7cdaSCy Schubert data->rr_ttl[i] = ttl; 516*335c7cdaSCy Schubert } 517*335c7cdaSCy Schubert data->ttl_add = 0; 518*335c7cdaSCy Schubert } 519*335c7cdaSCy Schubert 520*335c7cdaSCy Schubert /* Set the TTL of a DNS message and its RRs by to a fixed value. */ 521*335c7cdaSCy Schubert static void 522*335c7cdaSCy Schubert set_msg_ttl(struct dns_msg* msg, time_t ttl) 523*335c7cdaSCy Schubert { 524*335c7cdaSCy Schubert size_t i; 525*335c7cdaSCy Schubert msg->rep->ttl = ttl; 526*335c7cdaSCy Schubert msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl); 527*335c7cdaSCy Schubert msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL; 528*335c7cdaSCy Schubert 529*335c7cdaSCy Schubert for(i=0; i<msg->rep->rrset_count; i++) { 530*335c7cdaSCy Schubert packed_rrset_ttl_set((struct packed_rrset_data*)msg-> 531*335c7cdaSCy Schubert rep->rrsets[i]->entry.data, ttl); 532*335c7cdaSCy Schubert } 533*335c7cdaSCy Schubert } 534*335c7cdaSCy Schubert 535e2d15004SDag-Erling Smørgrav /** convert dns message in buffer to return_msg */ 536e2d15004SDag-Erling Smørgrav static int 537*335c7cdaSCy Schubert parse_data(struct module_qstate* qstate, struct sldns_buffer* buf, 538*335c7cdaSCy Schubert int* msg_expired) 539e2d15004SDag-Erling Smørgrav { 540e2d15004SDag-Erling Smørgrav struct msg_parse* prs; 541e2d15004SDag-Erling Smørgrav struct edns_data edns; 5428f76bb7dSCy Schubert struct edns_option* ede; 543e2d15004SDag-Erling Smørgrav uint64_t timestamp, expiry; 544e2d15004SDag-Erling Smørgrav time_t adjust; 545e2d15004SDag-Erling Smørgrav size_t lim = sldns_buffer_limit(buf); 546e2d15004SDag-Erling Smørgrav if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry)) 547e2d15004SDag-Erling Smørgrav return 0; /* too short */ 548e2d15004SDag-Erling Smørgrav 549e2d15004SDag-Erling Smørgrav /* remove timestamp and expiry from end */ 550e2d15004SDag-Erling Smørgrav sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry)); 551e2d15004SDag-Erling Smørgrav sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp), 552e2d15004SDag-Erling Smørgrav ×tamp, sizeof(timestamp)); 553e2d15004SDag-Erling Smørgrav expiry = be64toh(expiry); 554e2d15004SDag-Erling Smørgrav timestamp = be64toh(timestamp); 555e2d15004SDag-Erling Smørgrav 556e2d15004SDag-Erling Smørgrav /* parse DNS packet */ 557e2d15004SDag-Erling Smørgrav regional_free_all(qstate->env->scratch); 558e2d15004SDag-Erling Smørgrav prs = (struct msg_parse*)regional_alloc(qstate->env->scratch, 559e2d15004SDag-Erling Smørgrav sizeof(struct msg_parse)); 560e2d15004SDag-Erling Smørgrav if(!prs) 561e2d15004SDag-Erling Smørgrav return 0; /* out of memory */ 562e2d15004SDag-Erling Smørgrav memset(prs, 0, sizeof(*prs)); 563e2d15004SDag-Erling Smørgrav memset(&edns, 0, sizeof(edns)); 564e2d15004SDag-Erling Smørgrav sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp)); 565e2d15004SDag-Erling Smørgrav if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) { 566e2d15004SDag-Erling Smørgrav sldns_buffer_set_limit(buf, lim); 567e2d15004SDag-Erling Smørgrav return 0; 568e2d15004SDag-Erling Smørgrav } 56924e36522SCy Schubert if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) != 570e2d15004SDag-Erling Smørgrav LDNS_RCODE_NOERROR) { 571e2d15004SDag-Erling Smørgrav sldns_buffer_set_limit(buf, lim); 572e2d15004SDag-Erling Smørgrav return 0; 573e2d15004SDag-Erling Smørgrav } 574e2d15004SDag-Erling Smørgrav 575e2d15004SDag-Erling Smørgrav qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region); 576e2d15004SDag-Erling Smørgrav sldns_buffer_set_limit(buf, lim); 577e2d15004SDag-Erling Smørgrav if(!qstate->return_msg) 578e2d15004SDag-Erling Smørgrav return 0; 579e2d15004SDag-Erling Smørgrav 5808f76bb7dSCy Schubert /* We find the EDE in the in-list after parsing */ 5818f76bb7dSCy Schubert if(qstate->env->cfg->ede && 5828f76bb7dSCy Schubert (ede = edns_opt_list_find(edns.opt_list_in, LDNS_EDNS_EDE))) { 5838f76bb7dSCy Schubert if(ede->opt_len >= 2) { 5848f76bb7dSCy Schubert qstate->return_msg->rep->reason_bogus = 5858f76bb7dSCy Schubert sldns_read_uint16(ede->opt_data); 5868f76bb7dSCy Schubert } 5878f76bb7dSCy Schubert /* allocate space and store the error string and it's size */ 5888f76bb7dSCy Schubert if(ede->opt_len > 2) { 5898f76bb7dSCy Schubert size_t ede_len = ede->opt_len - 2; 5908f76bb7dSCy Schubert qstate->return_msg->rep->reason_bogus_str = regional_alloc( 5918f76bb7dSCy Schubert qstate->region, sizeof(char) * (ede_len+1)); 5928f76bb7dSCy Schubert memcpy(qstate->return_msg->rep->reason_bogus_str, 5938f76bb7dSCy Schubert ede->opt_data+2, ede_len); 5948f76bb7dSCy Schubert qstate->return_msg->rep->reason_bogus_str[ede_len] = 0; 5958f76bb7dSCy Schubert } 5968f76bb7dSCy Schubert } 5978f76bb7dSCy Schubert 598e2d15004SDag-Erling Smørgrav qstate->return_rcode = LDNS_RCODE_NOERROR; 599e2d15004SDag-Erling Smørgrav 600e2d15004SDag-Erling Smørgrav /* see how much of the TTL expired, and remove it */ 601c7f4d7adSDag-Erling Smørgrav if(*qstate->env->now <= (time_t)timestamp) { 602c7f4d7adSDag-Erling Smørgrav verbose(VERB_ALGO, "cachedb msg adjust by zero"); 603c7f4d7adSDag-Erling Smørgrav return 1; /* message from the future (clock skew?) */ 604e2d15004SDag-Erling Smørgrav } 605c7f4d7adSDag-Erling Smørgrav adjust = *qstate->env->now - (time_t)timestamp; 606c7f4d7adSDag-Erling Smørgrav if(qstate->return_msg->rep->ttl < adjust) { 607c7f4d7adSDag-Erling Smørgrav verbose(VERB_ALGO, "cachedb msg expired"); 608*335c7cdaSCy Schubert *msg_expired = 1; 6098a384985SDag-Erling Smørgrav /* If serve-expired is enabled, we still use an expired message 6108a384985SDag-Erling Smørgrav * setting the TTL to 0. */ 6111838dec3SCy Schubert if(!qstate->env->cfg->serve_expired || 6121838dec3SCy Schubert (FLAGS_GET_RCODE(qstate->return_msg->rep->flags) 6131838dec3SCy Schubert != LDNS_RCODE_NOERROR && 6141838dec3SCy Schubert FLAGS_GET_RCODE(qstate->return_msg->rep->flags) 6151838dec3SCy Schubert != LDNS_RCODE_NXDOMAIN && 6161838dec3SCy Schubert FLAGS_GET_RCODE(qstate->return_msg->rep->flags) 6171838dec3SCy Schubert != LDNS_RCODE_YXDOMAIN)) 618c7f4d7adSDag-Erling Smørgrav return 0; /* message expired */ 6191838dec3SCy Schubert else 6201838dec3SCy Schubert adjust = -1; 621c7f4d7adSDag-Erling Smørgrav } 622c7f4d7adSDag-Erling Smørgrav verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust); 623c7f4d7adSDag-Erling Smørgrav adjust_msg_ttl(qstate->return_msg, adjust); 6248a384985SDag-Erling Smørgrav 6258a384985SDag-Erling Smørgrav /* Similar to the unbound worker, if serve-expired is enabled and 6268a384985SDag-Erling Smørgrav * the msg would be considered to be expired, mark the state so a 6278a384985SDag-Erling Smørgrav * refetch will be scheduled. The comparison between 'expiry' and 6288a384985SDag-Erling Smørgrav * 'now' should be redundant given how these values were calculated, 6298a384985SDag-Erling Smørgrav * but we check it just in case as does good_expiry_and_qinfo(). */ 6308a384985SDag-Erling Smørgrav if(qstate->env->cfg->serve_expired && 631*335c7cdaSCy Schubert !qstate->env->cfg->serve_expired_client_timeout && 6328a384985SDag-Erling Smørgrav (adjust == -1 || (time_t)expiry < *qstate->env->now)) { 6338a384985SDag-Erling Smørgrav qstate->need_refetch = 1; 6348a384985SDag-Erling Smørgrav } 6358a384985SDag-Erling Smørgrav 636c7f4d7adSDag-Erling Smørgrav return 1; 637e2d15004SDag-Erling Smørgrav } 638e2d15004SDag-Erling Smørgrav 639e2d15004SDag-Erling Smørgrav /** 640e2d15004SDag-Erling Smørgrav * Lookup the qstate.qinfo in extcache, store in qstate.return_msg. 641e2d15004SDag-Erling Smørgrav * return true if lookup was successful. 642e2d15004SDag-Erling Smørgrav */ 643e2d15004SDag-Erling Smørgrav static int 644*335c7cdaSCy Schubert cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie, 645*335c7cdaSCy Schubert int* msg_expired) 646e2d15004SDag-Erling Smørgrav { 647e2d15004SDag-Erling Smørgrav char key[(CACHEDB_HASHSIZE/8)*2+1]; 648e2d15004SDag-Erling Smørgrav calc_hash(qstate, key, sizeof(key)); 649e2d15004SDag-Erling Smørgrav 650e2d15004SDag-Erling Smørgrav /* call backend to fetch data for key into scratch buffer */ 651e2d15004SDag-Erling Smørgrav if( !(*ie->backend->lookup)(qstate->env, ie, key, 652e2d15004SDag-Erling Smørgrav qstate->env->scratch_buffer)) { 653e2d15004SDag-Erling Smørgrav return 0; 654e2d15004SDag-Erling Smørgrav } 655e2d15004SDag-Erling Smørgrav 656e2d15004SDag-Erling Smørgrav /* check expiry date and check if query-data matches */ 657e2d15004SDag-Erling Smørgrav if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) { 658e2d15004SDag-Erling Smørgrav return 0; 659e2d15004SDag-Erling Smørgrav } 660e2d15004SDag-Erling Smørgrav 661e2d15004SDag-Erling Smørgrav /* parse dns message into return_msg */ 662*335c7cdaSCy Schubert if( !parse_data(qstate, qstate->env->scratch_buffer, msg_expired) ) { 663e2d15004SDag-Erling Smørgrav return 0; 664e2d15004SDag-Erling Smørgrav } 665e2d15004SDag-Erling Smørgrav return 1; 666e2d15004SDag-Erling Smørgrav } 667e2d15004SDag-Erling Smørgrav 668e2d15004SDag-Erling Smørgrav /** 669e2d15004SDag-Erling Smørgrav * Store the qstate.return_msg in extcache for key qstate.info 670e2d15004SDag-Erling Smørgrav */ 671e2d15004SDag-Erling Smørgrav static void 672e2d15004SDag-Erling Smørgrav cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie) 673e2d15004SDag-Erling Smørgrav { 674e2d15004SDag-Erling Smørgrav char key[(CACHEDB_HASHSIZE/8)*2+1]; 675e2d15004SDag-Erling Smørgrav calc_hash(qstate, key, sizeof(key)); 676e2d15004SDag-Erling Smørgrav 677e2d15004SDag-Erling Smørgrav /* prepare data in scratch buffer */ 678e2d15004SDag-Erling Smørgrav if(!prep_data(qstate, qstate->env->scratch_buffer)) 679e2d15004SDag-Erling Smørgrav return; 680e2d15004SDag-Erling Smørgrav 681e2d15004SDag-Erling Smørgrav /* call backend */ 682e2d15004SDag-Erling Smørgrav (*ie->backend->store)(qstate->env, ie, key, 683e2d15004SDag-Erling Smørgrav sldns_buffer_begin(qstate->env->scratch_buffer), 68425039b37SCy Schubert sldns_buffer_limit(qstate->env->scratch_buffer), 68525039b37SCy Schubert qstate->return_msg->rep->ttl); 686e2d15004SDag-Erling Smørgrav } 687e2d15004SDag-Erling Smørgrav 688e2d15004SDag-Erling Smørgrav /** 689e2d15004SDag-Erling Smørgrav * See if unbound's internal cache can answer the query 690e2d15004SDag-Erling Smørgrav */ 691e2d15004SDag-Erling Smørgrav static int 6928f76bb7dSCy Schubert cachedb_intcache_lookup(struct module_qstate* qstate, struct cachedb_env* cde) 693e2d15004SDag-Erling Smørgrav { 694*335c7cdaSCy Schubert uint8_t dpname_storage[LDNS_MAX_DOMAINLEN+1]; 6955469a995SCy Schubert uint8_t* dpname=NULL; 6965469a995SCy Schubert size_t dpnamelen=0; 697e2d15004SDag-Erling Smørgrav struct dns_msg* msg; 6988f76bb7dSCy Schubert /* for testframe bypass this lookup */ 6998f76bb7dSCy Schubert if(cde->backend == &testframe_backend) { 7008f76bb7dSCy Schubert return 0; 7018f76bb7dSCy Schubert } 7025469a995SCy Schubert if(iter_stub_fwd_no_cache(qstate, &qstate->qinfo, 703*335c7cdaSCy Schubert &dpname, &dpnamelen, dpname_storage, sizeof(dpname_storage))) 7045469a995SCy Schubert return 0; /* no cache for these queries */ 705e2d15004SDag-Erling Smørgrav msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname, 706e2d15004SDag-Erling Smørgrav qstate->qinfo.qname_len, qstate->qinfo.qtype, 707e2d15004SDag-Erling Smørgrav qstate->qinfo.qclass, qstate->query_flags, 70857bddd21SDag-Erling Smørgrav qstate->region, qstate->env->scratch, 7095469a995SCy Schubert 1, /* no partial messages with only a CNAME */ 7105469a995SCy Schubert dpname, dpnamelen 71157bddd21SDag-Erling Smørgrav ); 7120fb34990SDag-Erling Smørgrav if(!msg && qstate->env->neg_cache && 7130fb34990SDag-Erling Smørgrav iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) { 714e2d15004SDag-Erling Smørgrav /* lookup in negative cache; may result in 715e2d15004SDag-Erling Smørgrav * NOERROR/NODATA or NXDOMAIN answers that need validation */ 716e2d15004SDag-Erling Smørgrav msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo, 717e2d15004SDag-Erling Smørgrav qstate->region, qstate->env->rrset_cache, 718e2d15004SDag-Erling Smørgrav qstate->env->scratch_buffer, 71957bddd21SDag-Erling Smørgrav *qstate->env->now, 1/*add SOA*/, NULL, 72057bddd21SDag-Erling Smørgrav qstate->env->cfg); 721e2d15004SDag-Erling Smørgrav } 722e2d15004SDag-Erling Smørgrav if(!msg) 723e2d15004SDag-Erling Smørgrav return 0; 724e2d15004SDag-Erling Smørgrav /* this is the returned msg */ 725e2d15004SDag-Erling Smørgrav qstate->return_rcode = LDNS_RCODE_NOERROR; 726e2d15004SDag-Erling Smørgrav qstate->return_msg = msg; 727e2d15004SDag-Erling Smørgrav return 1; 728e2d15004SDag-Erling Smørgrav } 729e2d15004SDag-Erling Smørgrav 730e2d15004SDag-Erling Smørgrav /** 731e2d15004SDag-Erling Smørgrav * Store query into the internal cache of unbound. 732e2d15004SDag-Erling Smørgrav */ 733e2d15004SDag-Erling Smørgrav static void 734*335c7cdaSCy Schubert cachedb_intcache_store(struct module_qstate* qstate, int msg_expired) 735e2d15004SDag-Erling Smørgrav { 7368a384985SDag-Erling Smørgrav uint32_t store_flags = qstate->query_flags; 737*335c7cdaSCy Schubert int serve_expired = qstate->env->cfg->serve_expired; 7388a384985SDag-Erling Smørgrav 7398a384985SDag-Erling Smørgrav if(qstate->env->cfg->serve_expired) 7408a384985SDag-Erling Smørgrav store_flags |= DNSCACHE_STORE_ZEROTTL; 741e2d15004SDag-Erling Smørgrav if(!qstate->return_msg) 742e2d15004SDag-Erling Smørgrav return; 743*335c7cdaSCy Schubert if(serve_expired && msg_expired) { 744*335c7cdaSCy Schubert /* Set TTLs to a value such that value + *env->now is 745*335c7cdaSCy Schubert * going to be now-3 seconds. Making it expired 746*335c7cdaSCy Schubert * in the cache. */ 747*335c7cdaSCy Schubert set_msg_ttl(qstate->return_msg, (time_t)-3); 748*335c7cdaSCy Schubert } 749e2d15004SDag-Erling Smørgrav (void)dns_cache_store(qstate->env, &qstate->qinfo, 750e2d15004SDag-Erling Smørgrav qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0, 751790c6b24SCy Schubert qstate->region, store_flags, qstate->qstarttime); 752*335c7cdaSCy Schubert if(serve_expired && msg_expired) { 753*335c7cdaSCy Schubert if(qstate->env->cfg->serve_expired_client_timeout) { 754*335c7cdaSCy Schubert /* No expired response from the query state, the 755*335c7cdaSCy Schubert * query resolution needs to continue and it can 756*335c7cdaSCy Schubert * pick up the expired result after the timer out 757*335c7cdaSCy Schubert * of cache. */ 758*335c7cdaSCy Schubert return; 759*335c7cdaSCy Schubert } 760*335c7cdaSCy Schubert /* set TTLs to zero again */ 761*335c7cdaSCy Schubert adjust_msg_ttl(qstate->return_msg, -1); 762*335c7cdaSCy Schubert /* Send serve expired responses based on the cachedb 763*335c7cdaSCy Schubert * returned message, that was just stored in the cache. 764*335c7cdaSCy Schubert * It can then continue to work on this query. */ 765*335c7cdaSCy Schubert mesh_respond_serve_expired(qstate->mesh_info); 766*335c7cdaSCy Schubert } 767e2d15004SDag-Erling Smørgrav } 768e2d15004SDag-Erling Smørgrav 769e2d15004SDag-Erling Smørgrav /** 770e2d15004SDag-Erling Smørgrav * Handle a cachedb module event with a query 771e2d15004SDag-Erling Smørgrav * @param qstate: query state (from the mesh), passed between modules. 772e2d15004SDag-Erling Smørgrav * contains qstate->env module environment with global caches and so on. 773e2d15004SDag-Erling Smørgrav * @param iq: query state specific for this module. per-query. 774e2d15004SDag-Erling Smørgrav * @param ie: environment specific for this module. global. 775e2d15004SDag-Erling Smørgrav * @param id: module id. 776e2d15004SDag-Erling Smørgrav */ 777e2d15004SDag-Erling Smørgrav static void 778e2d15004SDag-Erling Smørgrav cachedb_handle_query(struct module_qstate* qstate, 779e2d15004SDag-Erling Smørgrav struct cachedb_qstate* ATTR_UNUSED(iq), 780e2d15004SDag-Erling Smørgrav struct cachedb_env* ie, int id) 781e2d15004SDag-Erling Smørgrav { 782*335c7cdaSCy Schubert int msg_expired = 0; 7838f76bb7dSCy Schubert qstate->is_cachedb_answer = 0; 784e2d15004SDag-Erling Smørgrav /* check if we are enabled, and skip if so */ 785e2d15004SDag-Erling Smørgrav if(!ie->enabled) { 786e2d15004SDag-Erling Smørgrav /* pass request to next module */ 787e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_wait_module; 788e2d15004SDag-Erling Smørgrav return; 789e2d15004SDag-Erling Smørgrav } 790e2d15004SDag-Erling Smørgrav 791bc892140SDag-Erling Smørgrav if(qstate->blacklist || qstate->no_cache_lookup) { 792bc892140SDag-Erling Smørgrav /* cache is blacklisted or we are instructed from edns to not look */ 793e2d15004SDag-Erling Smørgrav /* pass request to next module */ 794e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_wait_module; 795e2d15004SDag-Erling Smørgrav return; 796e2d15004SDag-Erling Smørgrav } 797e2d15004SDag-Erling Smørgrav 798091e9e46SCy Schubert /* lookup inside unbound's internal cache. 799091e9e46SCy Schubert * This does not look for expired entries. */ 8008f76bb7dSCy Schubert if(cachedb_intcache_lookup(qstate, ie)) { 801bc892140SDag-Erling Smørgrav if(verbosity >= VERB_ALGO) { 802bc892140SDag-Erling Smørgrav if(qstate->return_msg->rep) 803e2d15004SDag-Erling Smørgrav log_dns_msg("cachedb internal cache lookup", 804e2d15004SDag-Erling Smørgrav &qstate->return_msg->qinfo, 805e2d15004SDag-Erling Smørgrav qstate->return_msg->rep); 806bc892140SDag-Erling Smørgrav else log_info("cachedb internal cache lookup: rcode %s", 807091e9e46SCy Schubert sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode) 808091e9e46SCy Schubert ?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name 809091e9e46SCy Schubert :"??"); 810bc892140SDag-Erling Smørgrav } 811e2d15004SDag-Erling Smørgrav /* we are done with the query */ 812e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 813e2d15004SDag-Erling Smørgrav return; 814e2d15004SDag-Erling Smørgrav } 815e2d15004SDag-Erling Smørgrav 816e2d15004SDag-Erling Smørgrav /* ask backend cache to see if we have data */ 817*335c7cdaSCy Schubert if(cachedb_extcache_lookup(qstate, ie, &msg_expired)) { 818e2d15004SDag-Erling Smørgrav if(verbosity >= VERB_ALGO) 819e2d15004SDag-Erling Smørgrav log_dns_msg(ie->backend->name, 820e2d15004SDag-Erling Smørgrav &qstate->return_msg->qinfo, 821e2d15004SDag-Erling Smørgrav qstate->return_msg->rep); 822e2d15004SDag-Erling Smørgrav /* store this result in internal cache */ 823*335c7cdaSCy Schubert cachedb_intcache_store(qstate, msg_expired); 824091e9e46SCy Schubert /* In case we have expired data but there is a client timer for expired 825091e9e46SCy Schubert * answers, pass execution to next module in order to try updating the 826091e9e46SCy Schubert * data first. 827091e9e46SCy Schubert * TODO: this needs revisit. The expired data stored from cachedb has 828091e9e46SCy Schubert * 0 TTL which is picked up by iterator later when looking in the cache. 829*335c7cdaSCy Schubert */ 830*335c7cdaSCy Schubert if(qstate->env->cfg->serve_expired && msg_expired) { 831*335c7cdaSCy Schubert qstate->return_msg = NULL; 832*335c7cdaSCy Schubert qstate->ext_state[id] = module_wait_module; 833*335c7cdaSCy Schubert /* The expired reply is sent with 834*335c7cdaSCy Schubert * mesh_respond_serve_expired, and so 835*335c7cdaSCy Schubert * the need_refetch is not used. */ 836*335c7cdaSCy Schubert qstate->need_refetch = 0; 837*335c7cdaSCy Schubert return; 838*335c7cdaSCy Schubert } 839091e9e46SCy Schubert if(qstate->need_refetch && qstate->serve_expired_data && 840091e9e46SCy Schubert qstate->serve_expired_data->timer) { 841091e9e46SCy Schubert qstate->return_msg = NULL; 842091e9e46SCy Schubert qstate->ext_state[id] = module_wait_module; 843091e9e46SCy Schubert return; 844091e9e46SCy Schubert } 8458f76bb7dSCy Schubert qstate->is_cachedb_answer = 1; 846e2d15004SDag-Erling Smørgrav /* we are done with the query */ 847e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 848e2d15004SDag-Erling Smørgrav return; 849e2d15004SDag-Erling Smørgrav } 850e2d15004SDag-Erling Smørgrav 851*335c7cdaSCy Schubert if(qstate->serve_expired_data && 852*335c7cdaSCy Schubert qstate->env->cfg->cachedb_check_when_serve_expired && 853*335c7cdaSCy Schubert !qstate->env->cfg->serve_expired_client_timeout) { 854*335c7cdaSCy Schubert /* Reply with expired data if any to client, because cachedb 855*335c7cdaSCy Schubert * also has no useful, current data */ 856*335c7cdaSCy Schubert mesh_respond_serve_expired(qstate->mesh_info); 857*335c7cdaSCy Schubert } 858*335c7cdaSCy Schubert 859e2d15004SDag-Erling Smørgrav /* no cache fetches */ 860e2d15004SDag-Erling Smørgrav /* pass request to next module */ 861e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_wait_module; 862e2d15004SDag-Erling Smørgrav } 863e2d15004SDag-Erling Smørgrav 864e2d15004SDag-Erling Smørgrav /** 865e2d15004SDag-Erling Smørgrav * Handle a cachedb module event with a response from the iterator. 866e2d15004SDag-Erling Smørgrav * @param qstate: query state (from the mesh), passed between modules. 867e2d15004SDag-Erling Smørgrav * contains qstate->env module environment with global caches and so on. 868e2d15004SDag-Erling Smørgrav * @param iq: query state specific for this module. per-query. 869e2d15004SDag-Erling Smørgrav * @param ie: environment specific for this module. global. 870e2d15004SDag-Erling Smørgrav * @param id: module id. 871e2d15004SDag-Erling Smørgrav */ 872e2d15004SDag-Erling Smørgrav static void 873e2d15004SDag-Erling Smørgrav cachedb_handle_response(struct module_qstate* qstate, 874e2d15004SDag-Erling Smørgrav struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id) 875e2d15004SDag-Erling Smørgrav { 8768f76bb7dSCy Schubert qstate->is_cachedb_answer = 0; 877bc892140SDag-Erling Smørgrav /* check if we are not enabled or instructed to not cache, and skip */ 878bc892140SDag-Erling Smørgrav if(!ie->enabled || qstate->no_cache_store) { 879e2d15004SDag-Erling Smørgrav /* we are done with the query */ 880e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 881e2d15004SDag-Erling Smørgrav return; 882e2d15004SDag-Erling Smørgrav } 883103ba509SCy Schubert if(qstate->env->cfg->cachedb_no_store) { 884103ba509SCy Schubert /* do not store the item in the external cache */ 885103ba509SCy Schubert qstate->ext_state[id] = module_finished; 886103ba509SCy Schubert return; 887103ba509SCy Schubert } 888e2d15004SDag-Erling Smørgrav 889e2d15004SDag-Erling Smørgrav /* store the item into the backend cache */ 890e2d15004SDag-Erling Smørgrav cachedb_extcache_store(qstate, ie); 891e2d15004SDag-Erling Smørgrav 892e2d15004SDag-Erling Smørgrav /* we are done with the query */ 893e2d15004SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 894e2d15004SDag-Erling Smørgrav } 895e2d15004SDag-Erling Smørgrav 896e2d15004SDag-Erling Smørgrav void 897e2d15004SDag-Erling Smørgrav cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id, 898e2d15004SDag-Erling Smørgrav struct outbound_entry* outbound) 899e2d15004SDag-Erling Smørgrav { 900e2d15004SDag-Erling Smørgrav struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id]; 901e2d15004SDag-Erling Smørgrav struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id]; 902e2d15004SDag-Erling Smørgrav verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s", 903e2d15004SDag-Erling Smørgrav id, strextstate(qstate->ext_state[id]), strmodulevent(event)); 904e2d15004SDag-Erling Smørgrav if(iq) log_query_info(VERB_QUERY, "cachedb operate: query", 905e2d15004SDag-Erling Smørgrav &qstate->qinfo); 906e2d15004SDag-Erling Smørgrav 907e2d15004SDag-Erling Smørgrav /* perform cachedb state machine */ 908e2d15004SDag-Erling Smørgrav if((event == module_event_new || event == module_event_pass) && 909e2d15004SDag-Erling Smørgrav iq == NULL) { 910e2d15004SDag-Erling Smørgrav if(!cachedb_new(qstate, id)) { 911e2d15004SDag-Erling Smørgrav (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL); 912e2d15004SDag-Erling Smørgrav return; 913e2d15004SDag-Erling Smørgrav } 914e2d15004SDag-Erling Smørgrav iq = (struct cachedb_qstate*)qstate->minfo[id]; 915e2d15004SDag-Erling Smørgrav } 916e2d15004SDag-Erling Smørgrav if(iq && (event == module_event_pass || event == module_event_new)) { 917e2d15004SDag-Erling Smørgrav cachedb_handle_query(qstate, iq, ie, id); 918e2d15004SDag-Erling Smørgrav return; 919e2d15004SDag-Erling Smørgrav } 920e2d15004SDag-Erling Smørgrav if(iq && (event == module_event_moddone)) { 921e2d15004SDag-Erling Smørgrav cachedb_handle_response(qstate, iq, ie, id); 922e2d15004SDag-Erling Smørgrav return; 923e2d15004SDag-Erling Smørgrav } 924e2d15004SDag-Erling Smørgrav if(iq && outbound) { 925e2d15004SDag-Erling Smørgrav /* cachedb does not need to process responses at this time 926e2d15004SDag-Erling Smørgrav * ignore it. 927e2d15004SDag-Erling Smørgrav cachedb_process_response(qstate, iq, ie, id, outbound, event); 928e2d15004SDag-Erling Smørgrav */ 929e2d15004SDag-Erling Smørgrav return; 930e2d15004SDag-Erling Smørgrav } 931e2d15004SDag-Erling Smørgrav if(event == module_event_error) { 932e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "got called with event error, giving up"); 933e2d15004SDag-Erling Smørgrav (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL); 934e2d15004SDag-Erling Smørgrav return; 935e2d15004SDag-Erling Smørgrav } 936bc892140SDag-Erling Smørgrav if(!iq && (event == module_event_moddone)) { 937bc892140SDag-Erling Smørgrav /* during priming, module done but we never started */ 938bc892140SDag-Erling Smørgrav qstate->ext_state[id] = module_finished; 939bc892140SDag-Erling Smørgrav return; 940bc892140SDag-Erling Smørgrav } 941e2d15004SDag-Erling Smørgrav 942e2d15004SDag-Erling Smørgrav log_err("bad event for cachedb"); 943e2d15004SDag-Erling Smørgrav (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL); 944e2d15004SDag-Erling Smørgrav } 945e2d15004SDag-Erling Smørgrav 946e2d15004SDag-Erling Smørgrav void 947e2d15004SDag-Erling Smørgrav cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate), 948e2d15004SDag-Erling Smørgrav int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super)) 949e2d15004SDag-Erling Smørgrav { 950e2d15004SDag-Erling Smørgrav /* cachedb does not use subordinate requests at this time */ 951e2d15004SDag-Erling Smørgrav verbose(VERB_ALGO, "cachedb inform_super was called"); 952e2d15004SDag-Erling Smørgrav } 953e2d15004SDag-Erling Smørgrav 954e2d15004SDag-Erling Smørgrav void 955e2d15004SDag-Erling Smørgrav cachedb_clear(struct module_qstate* qstate, int id) 956e2d15004SDag-Erling Smørgrav { 957e2d15004SDag-Erling Smørgrav struct cachedb_qstate* iq; 958e2d15004SDag-Erling Smørgrav if(!qstate) 959e2d15004SDag-Erling Smørgrav return; 960e2d15004SDag-Erling Smørgrav iq = (struct cachedb_qstate*)qstate->minfo[id]; 961e2d15004SDag-Erling Smørgrav if(iq) { 962e2d15004SDag-Erling Smørgrav /* free contents of iq */ 963e2d15004SDag-Erling Smørgrav /* TODO */ 964e2d15004SDag-Erling Smørgrav } 965e2d15004SDag-Erling Smørgrav qstate->minfo[id] = NULL; 966e2d15004SDag-Erling Smørgrav } 967e2d15004SDag-Erling Smørgrav 968e2d15004SDag-Erling Smørgrav size_t 969e2d15004SDag-Erling Smørgrav cachedb_get_mem(struct module_env* env, int id) 970e2d15004SDag-Erling Smørgrav { 971e2d15004SDag-Erling Smørgrav struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id]; 972e2d15004SDag-Erling Smørgrav if(!ie) 973e2d15004SDag-Erling Smørgrav return 0; 974e2d15004SDag-Erling Smørgrav return sizeof(*ie); /* TODO - more mem */ 975e2d15004SDag-Erling Smørgrav } 976e2d15004SDag-Erling Smørgrav 977e2d15004SDag-Erling Smørgrav /** 978e2d15004SDag-Erling Smørgrav * The cachedb function block 979e2d15004SDag-Erling Smørgrav */ 980e2d15004SDag-Erling Smørgrav static struct module_func_block cachedb_block = { 981e2d15004SDag-Erling Smørgrav "cachedb", 982e2d15004SDag-Erling Smørgrav &cachedb_init, &cachedb_deinit, &cachedb_operate, 983e2d15004SDag-Erling Smørgrav &cachedb_inform_super, &cachedb_clear, &cachedb_get_mem 984e2d15004SDag-Erling Smørgrav }; 985e2d15004SDag-Erling Smørgrav 986e2d15004SDag-Erling Smørgrav struct module_func_block* 987e2d15004SDag-Erling Smørgrav cachedb_get_funcblock(void) 988e2d15004SDag-Erling Smørgrav { 989e2d15004SDag-Erling Smørgrav return &cachedb_block; 990e2d15004SDag-Erling Smørgrav } 991*335c7cdaSCy Schubert 992*335c7cdaSCy Schubert int 993*335c7cdaSCy Schubert cachedb_is_enabled(struct module_stack* mods, struct module_env* env) 994*335c7cdaSCy Schubert { 995*335c7cdaSCy Schubert struct cachedb_env* ie; 996*335c7cdaSCy Schubert int id = modstack_find(mods, "cachedb"); 997*335c7cdaSCy Schubert if(id == -1) 998*335c7cdaSCy Schubert return 0; 999*335c7cdaSCy Schubert ie = (struct cachedb_env*)env->modinfo[id]; 1000*335c7cdaSCy Schubert if(ie && ie->enabled) 1001*335c7cdaSCy Schubert return 1; 1002*335c7cdaSCy Schubert return 0; 1003*335c7cdaSCy Schubert } 1004*335c7cdaSCy Schubert 1005*335c7cdaSCy Schubert void cachedb_msg_remove(struct module_qstate* qstate) 1006*335c7cdaSCy Schubert { 1007*335c7cdaSCy Schubert char key[(CACHEDB_HASHSIZE/8)*2+1]; 1008*335c7cdaSCy Schubert int id = modstack_find(qstate->env->modstack, "cachedb"); 1009*335c7cdaSCy Schubert struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id]; 1010*335c7cdaSCy Schubert 1011*335c7cdaSCy Schubert log_query_info(VERB_ALGO, "cachedb msg remove", &qstate->qinfo); 1012*335c7cdaSCy Schubert calc_hash(qstate, key, sizeof(key)); 1013*335c7cdaSCy Schubert sldns_buffer_clear(qstate->env->scratch_buffer); 1014*335c7cdaSCy Schubert sldns_buffer_write_u32(qstate->env->scratch_buffer, 0); 1015*335c7cdaSCy Schubert sldns_buffer_flip(qstate->env->scratch_buffer); 1016*335c7cdaSCy Schubert 1017*335c7cdaSCy Schubert /* call backend */ 1018*335c7cdaSCy Schubert (*ie->backend->store)(qstate->env, ie, key, 1019*335c7cdaSCy Schubert sldns_buffer_begin(qstate->env->scratch_buffer), 1020*335c7cdaSCy Schubert sldns_buffer_limit(qstate->env->scratch_buffer), 1021*335c7cdaSCy Schubert 0); 1022*335c7cdaSCy Schubert } 1023e2d15004SDag-Erling Smørgrav #endif /* USE_CACHEDB */ 1024