Coverage Report

Created: 2025-08-28 06:16

/src/unbound/validator/val_kcache.c
Line
Count
Source (jump to first uncovered line)
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
0
{
52
0
  struct key_cache* kcache = (struct key_cache*)calloc(1, 
53
0
    sizeof(*kcache));
54
0
  size_t numtables, start_size, maxmem;
55
0
  if(!kcache) {
56
0
    log_err("malloc failure");
57
0
    return NULL;
58
0
  }
59
0
  numtables = cfg->key_cache_slabs;
60
0
  start_size = HASH_DEFAULT_STARTARRAY;
61
0
  maxmem = cfg->key_cache_size;
62
0
  kcache->slab = slabhash_create(numtables, start_size, maxmem,
63
0
    &key_entry_sizefunc, &key_entry_compfunc,
64
0
    &key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
65
0
  if(!kcache->slab) {
66
0
    log_err("malloc failure");
67
0
    free(kcache);
68
0
    return NULL;
69
0
  }
70
0
  return kcache;
71
0
}
72
73
void 
74
key_cache_delete(struct key_cache* kcache)
75
0
{
76
0
  if(!kcache)
77
0
    return;
78
0
  slabhash_delete(kcache->slab);
79
0
  free(kcache);
80
0
}
81
82
void 
83
key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
84
  int copy_reason)
85
0
{
86
0
  struct key_entry_key* k = key_entry_copy(kkey, copy_reason);
87
0
  if(!k)
88
0
    return;
89
0
  key_entry_hash(k);
90
0
  slabhash_insert(kcache->slab, k->entry.hash, &k->entry, 
91
0
    k->entry.data, NULL);
92
0
}
93
94
/**
95
 * Lookup exactly in the key cache. Returns pointer to locked entry.
96
 * Caller must unlock it after use.
97
 * @param kcache: the key cache.
98
 * @param name: for what name to look; uncompressed wireformat
99
 * @param namelen: length of the name.
100
 * @param key_class: class of the key.
101
 * @param wr: set true to get a writelock.
102
 * @return key entry, locked, or NULL if not found. No TTL checking is
103
 *  performed.
104
 */
105
static struct key_entry_key*
106
key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen, 
107
  uint16_t key_class, int wr)
108
0
{
109
0
  struct lruhash_entry* e;
110
0
  struct key_entry_key lookfor;
111
0
  lookfor.entry.key = &lookfor;
112
0
  lookfor.name = name;
113
0
  lookfor.namelen = namelen;
114
0
  lookfor.key_class = key_class;
115
0
  key_entry_hash(&lookfor);
116
0
  e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
117
0
  if(!e) 
118
0
    return NULL;
119
0
  return (struct key_entry_key*)e->key;
120
0
}
121
122
struct key_entry_key* 
123
key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen, 
124
  uint16_t key_class, struct regional* region, time_t now)
125
0
{
126
  /* keep looking until we find a nonexpired entry */
127
0
  while(1) {
128
0
    struct key_entry_key* k = key_cache_search(kcache, name, 
129
0
      namelen, key_class, 0);
130
0
    if(k) {
131
      /* see if TTL is OK */
132
0
      struct key_entry_data* d = (struct key_entry_data*)
133
0
        k->entry.data;
134
0
      if(now <= d->ttl) {
135
        /* copy and return it */
136
0
        struct key_entry_key* retkey =
137
0
          key_entry_copy_toregion(k, region);
138
0
        lock_rw_unlock(&k->entry.lock);
139
0
        return retkey;
140
0
      }
141
0
      lock_rw_unlock(&k->entry.lock);
142
0
    }
143
    /* snip off first label to continue */
144
0
    if(dname_is_root(name))
145
0
      break;
146
0
    dname_remove_label(&name, &namelen);
147
0
  }
148
0
  return NULL;
149
0
}
150
151
size_t 
152
key_cache_get_mem(struct key_cache* kcache)
153
0
{
154
0
  return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
155
0
}
156
157
void key_cache_remove(struct key_cache* kcache,
158
  uint8_t* name, size_t namelen, uint16_t key_class)
159
0
{
160
0
  struct key_entry_key lookfor;
161
0
  lookfor.entry.key = &lookfor;
162
0
  lookfor.name = name;
163
0
  lookfor.namelen = namelen;
164
0
  lookfor.key_class = key_class;
165
0
  key_entry_hash(&lookfor);
166
0
  slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
167
0
}