1 /* 2 * validator/val_kcache.c - validator key shared cache with validated keys 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains functions for dealing with the validator key cache. 40 */ 41 #include "config.h" 42 #include "validator/val_kcache.h" 43 #include "validator/val_kentry.h" 44 #include "util/log.h" 45 #include "util/config_file.h" 46 #include "util/data/dname.h" 47 #include "util/module.h" 48 49 struct key_cache* 50 key_cache_create(struct config_file* cfg) 51 { 52 struct key_cache* kcache = (struct key_cache*)calloc(1, 53 sizeof(*kcache)); 54 size_t numtables, start_size, maxmem; 55 if(!kcache) { 56 log_err("malloc failure"); 57 return NULL; 58 } 59 numtables = cfg->key_cache_slabs; 60 start_size = HASH_DEFAULT_STARTARRAY; 61 maxmem = cfg->key_cache_size; 62 kcache->slab = slabhash_create(numtables, start_size, maxmem, 63 &key_entry_sizefunc, &key_entry_compfunc, 64 &key_entry_delkeyfunc, &key_entry_deldatafunc, NULL); 65 if(!kcache->slab) { 66 log_err("malloc failure"); 67 free(kcache); 68 return NULL; 69 } 70 return kcache; 71 } 72 73 void 74 key_cache_delete(struct key_cache* kcache) 75 { 76 if(!kcache) 77 return; 78 slabhash_delete(kcache->slab); 79 free(kcache); 80 } 81 82 void 83 key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey, 84 struct module_qstate* qstate) 85 { 86 struct key_entry_key* k = key_entry_copy(kkey); 87 if(!k) 88 return; 89 if(key_entry_isbad(k) && qstate->errinf && 90 qstate->env->cfg->val_log_level >= 2) { 91 /* on malloc failure there is simply no reason string */ 92 key_entry_set_reason(k, errinf_to_str_bogus(qstate)); 93 } 94 key_entry_hash(k); 95 slabhash_insert(kcache->slab, k->entry.hash, &k->entry, 96 k->entry.data, NULL); 97 } 98 99 /** 100 * Lookup exactly in the key cache. Returns pointer to locked entry. 101 * Caller must unlock it after use. 102 * @param kcache: the key cache. 103 * @param name: for what name to look; uncompressed wireformat 104 * @param namelen: length of the name. 105 * @param key_class: class of the key. 106 * @param wr: set true to get a writelock. 107 * @return key entry, locked, or NULL if not found. No TTL checking is 108 * performed. 109 */ 110 static struct key_entry_key* 111 key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen, 112 uint16_t key_class, int wr) 113 { 114 struct lruhash_entry* e; 115 struct key_entry_key lookfor; 116 lookfor.entry.key = &lookfor; 117 lookfor.name = name; 118 lookfor.namelen = namelen; 119 lookfor.key_class = key_class; 120 key_entry_hash(&lookfor); 121 e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr); 122 if(!e) 123 return NULL; 124 return (struct key_entry_key*)e->key; 125 } 126 127 struct key_entry_key* 128 key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen, 129 uint16_t key_class, struct regional* region, time_t now) 130 { 131 /* keep looking until we find a nonexpired entry */ 132 while(1) { 133 struct key_entry_key* k = key_cache_search(kcache, name, 134 namelen, key_class, 0); 135 if(k) { 136 /* see if TTL is OK */ 137 struct key_entry_data* d = (struct key_entry_data*) 138 k->entry.data; 139 if(now <= d->ttl) { 140 /* copy and return it */ 141 struct key_entry_key* retkey = 142 key_entry_copy_toregion(k, region); 143 lock_rw_unlock(&k->entry.lock); 144 return retkey; 145 } 146 lock_rw_unlock(&k->entry.lock); 147 } 148 /* snip off first label to continue */ 149 if(dname_is_root(name)) 150 break; 151 dname_remove_label(&name, &namelen); 152 } 153 return NULL; 154 } 155 156 size_t 157 key_cache_get_mem(struct key_cache* kcache) 158 { 159 return sizeof(*kcache) + slabhash_get_mem(kcache->slab); 160 } 161 162 void key_cache_remove(struct key_cache* kcache, 163 uint8_t* name, size_t namelen, uint16_t key_class) 164 { 165 struct key_entry_key lookfor; 166 lookfor.entry.key = &lookfor; 167 lookfor.name = name; 168 lookfor.namelen = namelen; 169 lookfor.key_class = key_class; 170 key_entry_hash(&lookfor); 171 slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor); 172 } 173