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-2026 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 uint64_t hash_value;
45};
46
48{
50};
51
52
53#define HASH_KEY_LEN 16
54
55struct hash
56{
57 uint32_t n_buckets;
58 uint32_t n_elements;
59 uint32_t mask;
63 uint64_t (*hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN]);
64 bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
66};
67
68struct hash *hash_init(const uint32_t n_buckets,
69 uint64_t (*hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN]),
70 bool (*compare_function)(const void *key1, const void *key2));
71
72void hash_free(struct hash *hash);
73
74bool hash_add(struct hash *hash, const void *key, void *value, bool replace);
75
76struct hash_element *hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket,
77 const void *key, uint64_t hv);
78
79bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv);
80
81void hash_remove_by_value(struct hash *hash, void *value);
82
94
95void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket,
96 uint32_t end_bucket);
97
98void hash_iterator_init(struct hash *hash, struct hash_iterator *iter);
99
101
103
104void hash_iterator_free(struct hash_iterator *hi);
105
106static inline uint64_t
107hash_value(const struct hash *hash, const void *key)
108{
109 return (*hash->hash_function)(key, hash->hash_key);
110}
111
112static inline uint32_t
114{
115 return hash->n_elements;
116}
117
118static inline uint32_t
120{
121 return hash->n_buckets;
122}
123
124static inline struct hash_bucket *
125hash_bucket(struct hash *hash, uint64_t hv)
126{
127 return &hash->buckets[hv & hash->mask];
128}
129
130static inline void *
131hash_lookup(struct hash *hash, const void *key)
132{
133 void *ret = NULL;
134 struct hash_element *he;
135 uint64_t hv = hash_value(hash, key);
136 struct hash_bucket *bucket = &hash->buckets[hv & hash->mask];
137
138 he = hash_lookup_fast(hash, bucket, key, hv);
139 if (he)
140 {
141 ret = he->value;
142 }
143
144 return ret;
145}
146
147/* NOTE: assumes that key is not a duplicate */
148static inline void
149hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv,
150 void *value)
151{
152 struct hash_element *he;
153
154 ALLOC_OBJ(he, struct hash_element);
155 he->value = value;
156 he->key = key;
157 he->hash_value = hv;
158 he->next = bucket->list;
159 bucket->list = he;
160 ++hash->n_elements;
161}
162
163static inline bool
164hash_remove(struct hash *hash, const void *key)
165{
166 uint64_t hv;
167 struct hash_bucket *bucket;
168 bool ret;
169
170 hv = hash_value(hash, key);
171 bucket = &hash->buckets[hv & hash->mask];
172 ret = hash_remove_fast(hash, bucket, key, hv);
173 return ret;
174}
175
176#endif /* LIST */
Buffer management functions and garbage collection.
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:1963
static bool hash_remove(struct hash *hash, const void *key)
Definition list.h:164
void hash_iterator_init(struct hash *hash, struct hash_iterator *iter)
Definition list.c:236
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:272
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:278
static void * hash_lookup(struct hash *hash, const void *key)
Definition list.h:131
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:310
#define HASH_KEY_LEN
Definition list.h:53
static uint32_t hash_n_elements(const struct hash *hash)
Definition list.h:113
static uint32_t hash_n_buckets(const struct hash *hash)
Definition list.h:119
struct hash * hash_init(const uint32_t n_buckets, uint64_t(*hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN]), bool(*compare_function)(const void *key1, const void *key2))
Definition list.c:36
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv, void *value)
Definition list.h:149
bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
Definition list.c:109
void hash_free(struct hash *hash)
Definition list.c:62
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
Definition list.c:81
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:139
void hash_remove_by_value(struct hash *hash, void *value)
Definition list.c:167
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, uint32_t end_bucket)
Definition list.c:215
static uint64_t hash_value(const struct hash *hash, const void *key)
Definition list.h:107
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
uint64_t hash_value
Definition list.h:43
uint32_t bucket_index_end
Definition list.h:92
bool bucket_marked
Definition list.h:90
struct hash_bucket * bucket
Definition list.h:87
struct hash * hash
Definition list.h:85
uint32_t bucket_index
Definition list.h:86
struct hash_element * elem
Definition list.h:88
uint32_t bucket_index_start
Definition list.h:91
struct hash_element * last
Definition list.h:89
Definition list.h:56
uint32_t mask
Definition list.h:59
struct hash_bucket * buckets
Definition list.h:65
uint64_t(* hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
Definition list.h:63
uint32_t n_elements
Definition list.h:58
uint8_t hash_key[HASH_KEY_LEN]
key/iv used for the hash function.
Definition list.h:62
bool(* compare_function)(const void *key1, const void *key2)
Definition list.h:64
uint32_t n_buckets
Definition list.h:57
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:240
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152