]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/validator/val_kcache.c
2 * validator/val_kcache.c - validator key shared cache with validated keys
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions for dealing with the validator key cache.
42 #include "validator/val_kcache.h"
43 #include "validator/val_kentry.h"
45 #include "util/config_file.h"
46 #include "util/data/dname.h"
47 #include "util/module.h"
50 key_cache_create(struct config_file
* cfg
)
52 struct key_cache
* kcache
= (struct key_cache
*)calloc(1,
54 size_t numtables
, start_size
, maxmem
;
56 log_err("malloc failure");
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
);
66 log_err("malloc failure");
74 key_cache_delete(struct key_cache
* kcache
)
78 slabhash_delete(kcache
->slab
);
83 key_cache_insert(struct key_cache
* kcache
, struct key_entry_key
* kkey
,
84 struct module_qstate
* qstate
)
86 struct key_entry_key
* k
= key_entry_copy(kkey
);
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(qstate
));
95 slabhash_insert(kcache
->slab
, k
->entry
.hash
, &k
->entry
,
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
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
)
114 struct lruhash_entry
* e
;
115 struct key_entry_key lookfor
;
116 lookfor
.entry
.key
= &lookfor
;
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
);
124 return (struct key_entry_key
*)e
->key
;
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
)
131 /* keep looking until we find a nonexpired entry */
133 struct key_entry_key
* k
= key_cache_search(kcache
, name
,
134 namelen
, key_class
, 0);
136 /* see if TTL is OK */
137 struct key_entry_data
* d
= (struct key_entry_data
*)
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
);
146 lock_rw_unlock(&k
->entry
.lock
);
148 /* snip off first label to continue */
149 if(dname_is_root(name
))
151 dname_remove_label(&name
, &namelen
);
157 key_cache_get_mem(struct key_cache
* kcache
)
159 return sizeof(*kcache
) + slabhash_get_mem(kcache
->slab
);
162 void key_cache_remove(struct key_cache
* kcache
,
163 uint8_t* name
, size_t namelen
, uint16_t key_class
)
165 struct key_entry_key lookfor
;
166 lookfor
.entry
.key
= &lookfor
;
168 lookfor
.namelen
= namelen
;
169 lookfor
.key_class
= key_class
;
170 key_entry_hash(&lookfor
);
171 slabhash_remove(kcache
->slab
, lookfor
.entry
.hash
, &lookfor
);