Home | History | Annotate | Line # | Download | only in validator
val_kcache.c revision 1.1.1.2.2.1
      1          1.1  christos /*
      2          1.1  christos  * validator/val_kcache.c - validator key shared cache with validated keys
      3          1.1  christos  *
      4          1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5          1.1  christos  *
      6          1.1  christos  * This software is open source.
      7          1.1  christos  *
      8          1.1  christos  * Redistribution and use in source and binary forms, with or without
      9          1.1  christos  * modification, are permitted provided that the following conditions
     10          1.1  christos  * are met:
     11          1.1  christos  *
     12          1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13          1.1  christos  * this list of conditions and the following disclaimer.
     14          1.1  christos  *
     15          1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16          1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17          1.1  christos  * and/or other materials provided with the distribution.
     18          1.1  christos  *
     19          1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20          1.1  christos  * be used to endorse or promote products derived from this software without
     21          1.1  christos  * specific prior written permission.
     22          1.1  christos  *
     23          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27          1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28          1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29          1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30          1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31          1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32          1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33          1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34          1.1  christos  */
     35          1.1  christos 
     36          1.1  christos /**
     37          1.1  christos  * \file
     38          1.1  christos  *
     39          1.1  christos  * This file contains functions for dealing with the validator key cache.
     40          1.1  christos  */
     41          1.1  christos #include "config.h"
     42          1.1  christos #include "validator/val_kcache.h"
     43          1.1  christos #include "validator/val_kentry.h"
     44          1.1  christos #include "util/log.h"
     45          1.1  christos #include "util/config_file.h"
     46          1.1  christos #include "util/data/dname.h"
     47          1.1  christos #include "util/module.h"
     48          1.1  christos 
     49          1.1  christos struct key_cache*
     50          1.1  christos key_cache_create(struct config_file* cfg)
     51          1.1  christos {
     52          1.1  christos 	struct key_cache* kcache = (struct key_cache*)calloc(1,
     53          1.1  christos 		sizeof(*kcache));
     54          1.1  christos 	size_t numtables, start_size, maxmem;
     55          1.1  christos 	if(!kcache) {
     56          1.1  christos 		log_err("malloc failure");
     57          1.1  christos 		return NULL;
     58          1.1  christos 	}
     59          1.1  christos 	numtables = cfg->key_cache_slabs;
     60          1.1  christos 	start_size = HASH_DEFAULT_STARTARRAY;
     61          1.1  christos 	maxmem = cfg->key_cache_size;
     62          1.1  christos 	kcache->slab = slabhash_create(numtables, start_size, maxmem,
     63          1.1  christos 		&key_entry_sizefunc, &key_entry_compfunc,
     64          1.1  christos 		&key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
     65          1.1  christos 	if(!kcache->slab) {
     66          1.1  christos 		log_err("malloc failure");
     67          1.1  christos 		free(kcache);
     68          1.1  christos 		return NULL;
     69          1.1  christos 	}
     70          1.1  christos 	return kcache;
     71          1.1  christos }
     72          1.1  christos 
     73          1.1  christos void
     74          1.1  christos key_cache_delete(struct key_cache* kcache)
     75          1.1  christos {
     76          1.1  christos 	if(!kcache)
     77          1.1  christos 		return;
     78          1.1  christos 	slabhash_delete(kcache->slab);
     79          1.1  christos 	free(kcache);
     80          1.1  christos }
     81          1.1  christos 
     82          1.1  christos void
     83          1.1  christos key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
     84  1.1.1.2.2.1    martin 	int copy_reason)
     85          1.1  christos {
     86  1.1.1.2.2.1    martin 	struct key_entry_key* k = key_entry_copy(kkey, copy_reason);
     87          1.1  christos 	if(!k)
     88          1.1  christos 		return;
     89          1.1  christos 	key_entry_hash(k);
     90          1.1  christos 	slabhash_insert(kcache->slab, k->entry.hash, &k->entry,
     91          1.1  christos 		k->entry.data, NULL);
     92          1.1  christos }
     93          1.1  christos 
     94          1.1  christos /**
     95          1.1  christos  * Lookup exactly in the key cache. Returns pointer to locked entry.
     96          1.1  christos  * Caller must unlock it after use.
     97          1.1  christos  * @param kcache: the key cache.
     98          1.1  christos  * @param name: for what name to look; uncompressed wireformat
     99          1.1  christos  * @param namelen: length of the name.
    100          1.1  christos  * @param key_class: class of the key.
    101          1.1  christos  * @param wr: set true to get a writelock.
    102          1.1  christos  * @return key entry, locked, or NULL if not found. No TTL checking is
    103          1.1  christos  * 	performed.
    104          1.1  christos  */
    105          1.1  christos static struct key_entry_key*
    106          1.1  christos key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen,
    107          1.1  christos 	uint16_t key_class, int wr)
    108          1.1  christos {
    109          1.1  christos 	struct lruhash_entry* e;
    110          1.1  christos 	struct key_entry_key lookfor;
    111          1.1  christos 	lookfor.entry.key = &lookfor;
    112          1.1  christos 	lookfor.name = name;
    113          1.1  christos 	lookfor.namelen = namelen;
    114          1.1  christos 	lookfor.key_class = key_class;
    115          1.1  christos 	key_entry_hash(&lookfor);
    116          1.1  christos 	e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
    117          1.1  christos 	if(!e)
    118          1.1  christos 		return NULL;
    119          1.1  christos 	return (struct key_entry_key*)e->key;
    120          1.1  christos }
    121          1.1  christos 
    122          1.1  christos struct key_entry_key*
    123          1.1  christos key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen,
    124          1.1  christos 	uint16_t key_class, struct regional* region, time_t now)
    125          1.1  christos {
    126          1.1  christos 	/* keep looking until we find a nonexpired entry */
    127          1.1  christos 	while(1) {
    128          1.1  christos 		struct key_entry_key* k = key_cache_search(kcache, name,
    129          1.1  christos 			namelen, key_class, 0);
    130          1.1  christos 		if(k) {
    131          1.1  christos 			/* see if TTL is OK */
    132          1.1  christos 			struct key_entry_data* d = (struct key_entry_data*)
    133          1.1  christos 				k->entry.data;
    134          1.1  christos 			if(now <= d->ttl) {
    135          1.1  christos 				/* copy and return it */
    136          1.1  christos 				struct key_entry_key* retkey =
    137          1.1  christos 					key_entry_copy_toregion(k, region);
    138          1.1  christos 				lock_rw_unlock(&k->entry.lock);
    139          1.1  christos 				return retkey;
    140          1.1  christos 			}
    141          1.1  christos 			lock_rw_unlock(&k->entry.lock);
    142          1.1  christos 		}
    143          1.1  christos 		/* snip off first label to continue */
    144          1.1  christos 		if(dname_is_root(name))
    145          1.1  christos 			break;
    146          1.1  christos 		dname_remove_label(&name, &namelen);
    147          1.1  christos 	}
    148          1.1  christos 	return NULL;
    149          1.1  christos }
    150          1.1  christos 
    151          1.1  christos size_t
    152          1.1  christos key_cache_get_mem(struct key_cache* kcache)
    153          1.1  christos {
    154          1.1  christos 	return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
    155          1.1  christos }
    156          1.1  christos 
    157          1.1  christos void key_cache_remove(struct key_cache* kcache,
    158          1.1  christos 	uint8_t* name, size_t namelen, uint16_t key_class)
    159          1.1  christos {
    160          1.1  christos 	struct key_entry_key lookfor;
    161          1.1  christos 	lookfor.entry.key = &lookfor;
    162          1.1  christos 	lookfor.name = name;
    163          1.1  christos 	lookfor.namelen = namelen;
    164          1.1  christos 	lookfor.key_class = key_class;
    165          1.1  christos 	key_entry_hash(&lookfor);
    166          1.1  christos 	slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
    167          1.1  christos }
    168