1e2d15004SDag-Erling Smørgrav /* 2e2d15004SDag-Erling Smørgrav * cachedb/cachedb.h - 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 #include "util/module.h" 43e2d15004SDag-Erling Smørgrav struct cachedb_backend; 44335c7cdaSCy Schubert struct module_stack; 45e2d15004SDag-Erling Smørgrav 46e2d15004SDag-Erling Smørgrav /** 47e2d15004SDag-Erling Smørgrav * The global variable environment contents for the cachedb 48e2d15004SDag-Erling Smørgrav * Shared between threads, this represents long term information. 49e2d15004SDag-Erling Smørgrav * Like database connections. 50e2d15004SDag-Erling Smørgrav */ 51e2d15004SDag-Erling Smørgrav struct cachedb_env { 52e2d15004SDag-Erling Smørgrav /** true is cachedb is enabled, the backend is turned on */ 53e2d15004SDag-Erling Smørgrav int enabled; 54e2d15004SDag-Erling Smørgrav 55e2d15004SDag-Erling Smørgrav /** the backend routines */ 56e2d15004SDag-Erling Smørgrav struct cachedb_backend* backend; 57e2d15004SDag-Erling Smørgrav 58e2d15004SDag-Erling Smørgrav /** backend specific data here */ 59e2d15004SDag-Erling Smørgrav void* backend_data; 60e2d15004SDag-Erling Smørgrav }; 61e2d15004SDag-Erling Smørgrav 62e2d15004SDag-Erling Smørgrav /** 63e2d15004SDag-Erling Smørgrav * Per query state for the cachedb module. 64e2d15004SDag-Erling Smørgrav */ 65e2d15004SDag-Erling Smørgrav struct cachedb_qstate { 66e2d15004SDag-Erling Smørgrav int todo; 67e2d15004SDag-Erling Smørgrav }; 68e2d15004SDag-Erling Smørgrav 69e2d15004SDag-Erling Smørgrav /** 70e2d15004SDag-Erling Smørgrav * Backend call routines 71e2d15004SDag-Erling Smørgrav */ 72e2d15004SDag-Erling Smørgrav struct cachedb_backend { 73e2d15004SDag-Erling Smørgrav /** backend name */ 74e2d15004SDag-Erling Smørgrav const char* name; 75e2d15004SDag-Erling Smørgrav 76e2d15004SDag-Erling Smørgrav /** Init(env, cachedb_env): false on setup failure */ 77e2d15004SDag-Erling Smørgrav int (*init)(struct module_env*, struct cachedb_env*); 78e2d15004SDag-Erling Smørgrav 79e2d15004SDag-Erling Smørgrav /** Deinit - close db for program exit */ 80e2d15004SDag-Erling Smørgrav void (*deinit)(struct module_env*, struct cachedb_env*); 81e2d15004SDag-Erling Smørgrav 82e2d15004SDag-Erling Smørgrav /** Lookup (env, cachedb_env, key, result_buffer): true if found */ 83e2d15004SDag-Erling Smørgrav int (*lookup)(struct module_env*, struct cachedb_env*, char*, 84e2d15004SDag-Erling Smørgrav struct sldns_buffer*); 85e2d15004SDag-Erling Smørgrav 86e2d15004SDag-Erling Smørgrav /** Store (env, cachedb_env, key, data, data_len) */ 87e2d15004SDag-Erling Smørgrav void (*store)(struct module_env*, struct cachedb_env*, char*, 8825039b37SCy Schubert uint8_t*, size_t, time_t); 89e2d15004SDag-Erling Smørgrav }; 90e2d15004SDag-Erling Smørgrav 910fb34990SDag-Erling Smørgrav #define CACHEDB_HASHSIZE 256 /* bit hash */ 920fb34990SDag-Erling Smørgrav 93e2d15004SDag-Erling Smørgrav /** Init the cachedb module */ 94e2d15004SDag-Erling Smørgrav int cachedb_init(struct module_env* env, int id); 95e2d15004SDag-Erling Smørgrav /** Deinit the cachedb module */ 96e2d15004SDag-Erling Smørgrav void cachedb_deinit(struct module_env* env, int id); 97e2d15004SDag-Erling Smørgrav /** Operate on an event on a query (in qstate). */ 98e2d15004SDag-Erling Smørgrav void cachedb_operate(struct module_qstate* qstate, enum module_ev event, 99e2d15004SDag-Erling Smørgrav int id, struct outbound_entry* outbound); 100e2d15004SDag-Erling Smørgrav /** Subordinate query done, inform this super request of its conclusion */ 101e2d15004SDag-Erling Smørgrav void cachedb_inform_super(struct module_qstate* qstate, int id, 102e2d15004SDag-Erling Smørgrav struct module_qstate* super); 103e2d15004SDag-Erling Smørgrav /** clear the cachedb query-specific contents out of qstate */ 104e2d15004SDag-Erling Smørgrav void cachedb_clear(struct module_qstate* qstate, int id); 105e2d15004SDag-Erling Smørgrav /** return memory estimate for cachedb module */ 106e2d15004SDag-Erling Smørgrav size_t cachedb_get_mem(struct module_env* env, int id); 107e2d15004SDag-Erling Smørgrav 108e2d15004SDag-Erling Smørgrav /** 109e2d15004SDag-Erling Smørgrav * Get the function block with pointers to the cachedb functions 110e2d15004SDag-Erling Smørgrav * @return the function block for "cachedb". 111e2d15004SDag-Erling Smørgrav */ 112e2d15004SDag-Erling Smørgrav struct module_func_block* cachedb_get_funcblock(void); 113e2d15004SDag-Erling Smørgrav 114335c7cdaSCy Schubert /** 115335c7cdaSCy Schubert * See if the cachedb is enabled. 116335c7cdaSCy Schubert * @param mods: module stack. It finds the cachedb module environment. 117335c7cdaSCy Schubert * @param env: module environment. 118335c7cdaSCy Schubert * @return true if exists and enabled. 119335c7cdaSCy Schubert */ 120335c7cdaSCy Schubert int cachedb_is_enabled(struct module_stack* mods, struct module_env* env); 121335c7cdaSCy Schubert 122335c7cdaSCy Schubert /** 123335c7cdaSCy Schubert * Remove a message from the global cache. Because edns subnet has a more 124335c7cdaSCy Schubert * specific entry, and if not removed when everything expires, the global 125335c7cdaSCy Schubert * entry is used, instead of a fresh lookup of the edns subnet entry. 126335c7cdaSCy Schubert * @param qstate: query state. 127335c7cdaSCy Schubert */ 128335c7cdaSCy Schubert void cachedb_msg_remove(struct module_qstate* qstate); 129*56850988SCy Schubert 130*56850988SCy Schubert /** 131*56850988SCy Schubert * Remove message from the cachedb cache, by query info. 132*56850988SCy Schubert * @param env: module environment to look up cachedb state. 133*56850988SCy Schubert * @param qinfo: the message to remove. 134*56850988SCy Schubert */ 135*56850988SCy Schubert void cachedb_msg_remove_qinfo(struct module_env* env, 136*56850988SCy Schubert struct query_info* qinfo); 137