1933707f3Ssthen /*
2933707f3Ssthen * validator/val_kcache.c - validator key shared cache with validated keys
3933707f3Ssthen *
4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen *
6933707f3Ssthen * This software is open source.
7933707f3Ssthen *
8933707f3Ssthen * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen * modification, are permitted provided that the following conditions
10933707f3Ssthen * are met:
11933707f3Ssthen *
12933707f3Ssthen * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen * this list of conditions and the following disclaimer.
14933707f3Ssthen *
15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen * and/or other materials provided with the distribution.
18933707f3Ssthen *
19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen * be used to endorse or promote products derived from this software without
21933707f3Ssthen * specific prior written permission.
22933707f3Ssthen *
23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen */
35933707f3Ssthen
36933707f3Ssthen /**
37933707f3Ssthen * \file
38933707f3Ssthen *
39933707f3Ssthen * This file contains functions for dealing with the validator key cache.
40933707f3Ssthen */
41933707f3Ssthen #include "config.h"
42933707f3Ssthen #include "validator/val_kcache.h"
43933707f3Ssthen #include "validator/val_kentry.h"
44933707f3Ssthen #include "util/log.h"
45933707f3Ssthen #include "util/config_file.h"
46933707f3Ssthen #include "util/data/dname.h"
47933707f3Ssthen #include "util/module.h"
48933707f3Ssthen
49933707f3Ssthen struct key_cache*
key_cache_create(struct config_file * cfg)50933707f3Ssthen key_cache_create(struct config_file* cfg)
51933707f3Ssthen {
52933707f3Ssthen struct key_cache* kcache = (struct key_cache*)calloc(1,
53933707f3Ssthen sizeof(*kcache));
54933707f3Ssthen size_t numtables, start_size, maxmem;
55933707f3Ssthen if(!kcache) {
56933707f3Ssthen log_err("malloc failure");
57933707f3Ssthen return NULL;
58933707f3Ssthen }
59933707f3Ssthen numtables = cfg->key_cache_slabs;
60933707f3Ssthen start_size = HASH_DEFAULT_STARTARRAY;
61933707f3Ssthen maxmem = cfg->key_cache_size;
62933707f3Ssthen kcache->slab = slabhash_create(numtables, start_size, maxmem,
63933707f3Ssthen &key_entry_sizefunc, &key_entry_compfunc,
64933707f3Ssthen &key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
65933707f3Ssthen if(!kcache->slab) {
66933707f3Ssthen log_err("malloc failure");
67933707f3Ssthen free(kcache);
68933707f3Ssthen return NULL;
69933707f3Ssthen }
70933707f3Ssthen return kcache;
71933707f3Ssthen }
72933707f3Ssthen
73933707f3Ssthen void
key_cache_delete(struct key_cache * kcache)74933707f3Ssthen key_cache_delete(struct key_cache* kcache)
75933707f3Ssthen {
76933707f3Ssthen if(!kcache)
77933707f3Ssthen return;
78933707f3Ssthen slabhash_delete(kcache->slab);
79933707f3Ssthen free(kcache);
80933707f3Ssthen }
81933707f3Ssthen
82933707f3Ssthen void
key_cache_insert(struct key_cache * kcache,struct key_entry_key * kkey,int copy_reason)83933707f3Ssthen key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
84*437d2860Ssthen int copy_reason)
85933707f3Ssthen {
86*437d2860Ssthen struct key_entry_key* k = key_entry_copy(kkey, copy_reason);
87933707f3Ssthen if(!k)
88933707f3Ssthen return;
89933707f3Ssthen key_entry_hash(k);
90933707f3Ssthen slabhash_insert(kcache->slab, k->entry.hash, &k->entry,
91933707f3Ssthen k->entry.data, NULL);
92933707f3Ssthen }
93933707f3Ssthen
94933707f3Ssthen /**
95933707f3Ssthen * Lookup exactly in the key cache. Returns pointer to locked entry.
96933707f3Ssthen * Caller must unlock it after use.
97933707f3Ssthen * @param kcache: the key cache.
98933707f3Ssthen * @param name: for what name to look; uncompressed wireformat
99933707f3Ssthen * @param namelen: length of the name.
100933707f3Ssthen * @param key_class: class of the key.
101933707f3Ssthen * @param wr: set true to get a writelock.
102933707f3Ssthen * @return key entry, locked, or NULL if not found. No TTL checking is
103933707f3Ssthen * performed.
104933707f3Ssthen */
105933707f3Ssthen static struct key_entry_key*
key_cache_search(struct key_cache * kcache,uint8_t * name,size_t namelen,uint16_t key_class,int wr)106933707f3Ssthen key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen,
107933707f3Ssthen uint16_t key_class, int wr)
108933707f3Ssthen {
109933707f3Ssthen struct lruhash_entry* e;
110933707f3Ssthen struct key_entry_key lookfor;
111933707f3Ssthen lookfor.entry.key = &lookfor;
112933707f3Ssthen lookfor.name = name;
113933707f3Ssthen lookfor.namelen = namelen;
114933707f3Ssthen lookfor.key_class = key_class;
115933707f3Ssthen key_entry_hash(&lookfor);
116933707f3Ssthen e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
117933707f3Ssthen if(!e)
118933707f3Ssthen return NULL;
119933707f3Ssthen return (struct key_entry_key*)e->key;
120933707f3Ssthen }
121933707f3Ssthen
122933707f3Ssthen struct key_entry_key*
key_cache_obtain(struct key_cache * kcache,uint8_t * name,size_t namelen,uint16_t key_class,struct regional * region,time_t now)123933707f3Ssthen key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen,
124229e174cSsthen uint16_t key_class, struct regional* region, time_t now)
125933707f3Ssthen {
126933707f3Ssthen /* keep looking until we find a nonexpired entry */
127933707f3Ssthen while(1) {
128933707f3Ssthen struct key_entry_key* k = key_cache_search(kcache, name,
129933707f3Ssthen namelen, key_class, 0);
130933707f3Ssthen if(k) {
131933707f3Ssthen /* see if TTL is OK */
132933707f3Ssthen struct key_entry_data* d = (struct key_entry_data*)
133933707f3Ssthen k->entry.data;
134933707f3Ssthen if(now <= d->ttl) {
135933707f3Ssthen /* copy and return it */
136933707f3Ssthen struct key_entry_key* retkey =
137933707f3Ssthen key_entry_copy_toregion(k, region);
138933707f3Ssthen lock_rw_unlock(&k->entry.lock);
139933707f3Ssthen return retkey;
140933707f3Ssthen }
141933707f3Ssthen lock_rw_unlock(&k->entry.lock);
142933707f3Ssthen }
143933707f3Ssthen /* snip off first label to continue */
144933707f3Ssthen if(dname_is_root(name))
145933707f3Ssthen break;
146933707f3Ssthen dname_remove_label(&name, &namelen);
147933707f3Ssthen }
148933707f3Ssthen return NULL;
149933707f3Ssthen }
150933707f3Ssthen
151933707f3Ssthen size_t
key_cache_get_mem(struct key_cache * kcache)152933707f3Ssthen key_cache_get_mem(struct key_cache* kcache)
153933707f3Ssthen {
154933707f3Ssthen return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
155933707f3Ssthen }
156933707f3Ssthen
key_cache_remove(struct key_cache * kcache,uint8_t * name,size_t namelen,uint16_t key_class)157933707f3Ssthen void key_cache_remove(struct key_cache* kcache,
158933707f3Ssthen uint8_t* name, size_t namelen, uint16_t key_class)
159933707f3Ssthen {
160933707f3Ssthen struct key_entry_key lookfor;
161933707f3Ssthen lookfor.entry.key = &lookfor;
162933707f3Ssthen lookfor.name = name;
163933707f3Ssthen lookfor.namelen = namelen;
164933707f3Ssthen lookfor.key_class = key_class;
165933707f3Ssthen key_entry_hash(&lookfor);
166933707f3Ssthen slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
167933707f3Ssthen }
168