OpenVPN
list.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef LIST_H
24#define LIST_H
25
26/*
27 * This code is a fairly straightforward hash
28 * table implementation using Bob Jenkins'
29 * hash function.
30 *
31 * Hash tables are used in OpenVPN to keep track of
32 * client instances over various key spaces.
33 */
34
35
36#include "basic.h"
37#include "buffer.h"
38
40{
41 void *value;
42 const void *key;
43 uint32_t hash_value;
45};
46
48{
50};
51
52struct hash
53{
54 uint32_t n_buckets;
55 uint32_t n_elements;
56 uint32_t mask;
57 uint32_t iv;
58 uint32_t (*hash_function)(const void *key, uint32_t iv);
59 bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
61};
62
63struct hash *hash_init(const uint32_t n_buckets, const uint32_t iv,
64 uint32_t (*hash_function)(const void *key, uint32_t iv),
65 bool (*compare_function)(const void *key1, const void *key2));
66
67void hash_free(struct hash *hash);
68
69bool hash_add(struct hash *hash, const void *key, void *value, bool replace);
70
71struct hash_element *hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket,
72 const void *key, uint32_t hv);
73
74bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv);
75
76void hash_remove_by_value(struct hash *hash, void *value);
77
89
90void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket,
91 uint32_t end_bucket);
92
93void hash_iterator_init(struct hash *hash, struct hash_iterator *iter);
94
96
98
99void hash_iterator_free(struct hash_iterator *hi);
100
101uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);
102
103static inline uint32_t
104hash_value(const struct hash *hash, const void *key)
105{
106 return (*hash->hash_function)(key, hash->iv);
107}
108
109static inline uint32_t
111{
112 return hash->n_elements;
113}
114
115static inline uint32_t
117{
118 return hash->n_buckets;
119}
120
121static inline struct hash_bucket *
122hash_bucket(struct hash *hash, uint32_t hv)
123{
124 return &hash->buckets[hv & hash->mask];
125}
126
127static inline void *
128hash_lookup(struct hash *hash, const void *key)
129{
130 void *ret = NULL;
131 struct hash_element *he;
132 uint32_t hv = hash_value(hash, key);
133 struct hash_bucket *bucket = &hash->buckets[hv & hash->mask];
134
135 he = hash_lookup_fast(hash, bucket, key, hv);
136 if (he)
137 {
138 ret = he->value;
139 }
140
141 return ret;
142}
143
144/* NOTE: assumes that key is not a duplicate */
145static inline void
146hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv,
147 void *value)
148{
149 struct hash_element *he;
150
151 ALLOC_OBJ(he, struct hash_element);
152 he->value = value;
153 he->key = key;
154 he->hash_value = hv;
155 he->next = bucket->list;
156 bucket->list = he;
157 ++hash->n_elements;
158}
159
160static inline bool
161hash_remove(struct hash *hash, const void *key)
162{
163 uint32_t hv;
164 struct hash_bucket *bucket;
165 bool ret;
166
167 hv = hash_value(hash, key);
168 bucket = &hash->buckets[hv & hash->mask];
169 ret = hash_remove_fast(hash, bucket, key, hv);
170 return ret;
171}
172
173#endif /* LIST */
#define ALLOC_OBJ(dptr, type)
Definition buffer.h:1037
#define key2
Definition cert_data.h:80
static const char *const key1
Definition cert_data.h:55
static bool hash_remove(struct hash *hash, const void *key)
Definition list.h:161
void hash_iterator_init(struct hash *hash, struct hash_iterator *iter)
Definition list.c:234
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:270
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:276
static void * hash_lookup(struct hash *hash, const void *key)
Definition list.h:128
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:308
static uint32_t hash_n_elements(const struct hash *hash)
Definition list.h:110
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:79
static uint32_t hash_n_buckets(const struct hash *hash)
Definition list.h:116
static uint32_t hash_value(const struct hash *hash, const void *key)
Definition list.h:104
bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:107
struct hash * hash_init(const uint32_t n_buckets, const uint32_t iv, uint32_t(*hash_function)(const void *key, uint32_t iv), bool(*compare_function)(const void *key1, const void *key2))
Definition list.c:37
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
Definition list.c:416
void hash_free(struct hash *hash)
Definition list.c:60
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:137
void hash_remove_by_value(struct hash *hash, void *value)
Definition list.c:165
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv, void *value)
Definition list.h:146
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, uint32_t end_bucket)
Definition list.c:213
struct hash_element * list
Definition list.h:49
void * value
Definition list.h:41
const void * key
Definition list.h:42
struct hash_element * next
Definition list.h:44
uint32_t hash_value
Definition list.h:43
uint32_t bucket_index_end
Definition list.h:87
bool bucket_marked
Definition list.h:85
struct hash_bucket * bucket
Definition list.h:82
struct hash * hash
Definition list.h:80
uint32_t bucket_index
Definition list.h:81
struct hash_element * elem
Definition list.h:83
uint32_t bucket_index_start
Definition list.h:86
struct hash_element * last
Definition list.h:84
Definition list.h:53
uint32_t(* hash_function)(const void *key, uint32_t iv)
Definition list.h:58
uint32_t mask
Definition list.h:56
struct hash_bucket * buckets
Definition list.h:60
uint32_t n_elements
Definition list.h:55
uint32_t iv
Definition list.h:57
bool(* compare_function)(const void *key1, const void *key2)
Definition list.h:59
uint32_t n_buckets
Definition list.h:54
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:240
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152