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-2024 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef LIST_H
25#define LIST_H
26
27/*
28 * This code is a fairly straightforward hash
29 * table implementation using Bob Jenkins'
30 * hash function.
31 *
32 * Hash tables are used in OpenVPN to keep track of
33 * client instances over various key spaces.
34 */
35
36
37#include "basic.h"
38#include "buffer.h"
39
40#define hashsize(n) ((uint32_t)1<<(n))
41#define hashmask(n) (hashsize(n)-1)
42
44{
45 void *value;
46 const void *key;
47 unsigned int hash_value;
49};
50
52{
54};
55
56struct hash
57{
60 int mask;
61 uint32_t iv;
62 uint32_t (*hash_function)(const void *key, uint32_t iv);
63 bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
65};
66
67struct hash *hash_init(const int n_buckets,
68 const uint32_t iv,
69 uint32_t (*hash_function)(const void *key, uint32_t iv),
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
77 struct hash_bucket *bucket,
78 const void *key,
79 uint32_t hv);
80
81bool hash_remove_fast(struct hash *hash,
82 struct hash_bucket *bucket,
83 const void *key,
84 uint32_t hv);
85
86void hash_remove_by_value(struct hash *hash, void *value);
87
99
101 struct hash_iterator *hi,
102 int start_bucket,
103 int end_bucket);
104
105void hash_iterator_init(struct hash *hash, struct hash_iterator *iter);
106
108
110
111void hash_iterator_free(struct hash_iterator *hi);
112
113uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);
114
115static inline uint32_t
116hash_value(const struct hash *hash, const void *key)
117{
118 return (*hash->hash_function)(key, hash->iv);
119}
120
121static inline int
123{
124 return hash->n_elements;
125}
126
127static inline int
129{
130 return hash->n_buckets;
131}
132
133static inline struct hash_bucket *
134hash_bucket(struct hash *hash, uint32_t hv)
135{
136 return &hash->buckets[hv & hash->mask];
137}
138
139static inline void *
140hash_lookup(struct hash *hash, const void *key)
141{
142 void *ret = NULL;
143 struct hash_element *he;
144 uint32_t hv = hash_value(hash, key);
145 struct hash_bucket *bucket = &hash->buckets[hv & hash->mask];
146
147 he = hash_lookup_fast(hash, bucket, key, hv);
148 if (he)
149 {
150 ret = he->value;
151 }
152
153 return ret;
154}
155
156/* NOTE: assumes that key is not a duplicate */
157static inline void
159 struct hash_bucket *bucket,
160 const void *key,
161 uint32_t hv,
162 void *value)
163{
164 struct hash_element *he;
165
166 ALLOC_OBJ(he, struct hash_element);
167 he->value = value;
168 he->key = key;
169 he->hash_value = hv;
170 he->next = bucket->list;
171 bucket->list = he;
172 ++hash->n_elements;
173}
174
175static inline bool
176hash_remove(struct hash *hash, const void *key)
177{
178 uint32_t hv;
179 struct hash_bucket *bucket;
180 bool ret;
181
182 hv = hash_value(hash, key);
183 bucket = &hash->buckets[hv & hash->mask];
184 ret = hash_remove_fast(hash, bucket, key, hv);
185 return ret;
186}
187
188#endif /* LIST */
#define ALLOC_OBJ(dptr, type)
Definition buffer.h:1055
#define key2
Definition cert_data.h:82
static const char *const key1
Definition cert_data.h:56
static bool hash_remove(struct hash *hash, const void *key)
Definition list.h:176
void hash_iterator_init(struct hash *hash, struct hash_iterator *iter)
Definition list.c:246
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:283
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:289
static void * hash_lookup(struct hash *hash, const void *key)
Definition list.h:140
static int hash_n_elements(const struct hash *hash)
Definition list.h:122
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:321
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:83
struct hash * hash_init(const int 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:38
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket, int end_bucket)
Definition list.c:223
static uint32_t hash_value(const struct hash *hash, const void *key)
Definition list.h:116
static int hash_n_buckets(const struct hash *hash)
Definition list.h:128
bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:114
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
Definition list.c:408
void hash_free(struct hash *hash)
Definition list.c:63
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:147
void hash_remove_by_value(struct hash *hash, void *value)
Definition list.c:175
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv, void *value)
Definition list.h:158
struct hash_element * list
Definition list.h:53
void * value
Definition list.h:45
const void * key
Definition list.h:46
struct hash_element * next
Definition list.h:48
unsigned int hash_value
Definition list.h:47
bool bucket_marked
Definition list.h:95
int bucket_index_end
Definition list.h:97
struct hash_bucket * bucket
Definition list.h:92
struct hash * hash
Definition list.h:90
int bucket_index_start
Definition list.h:96
struct hash_element * elem
Definition list.h:93
int bucket_index
Definition list.h:91
struct hash_element * last
Definition list.h:94
Definition list.h:57
uint32_t(* hash_function)(const void *key, uint32_t iv)
Definition list.h:62
struct hash_bucket * buckets
Definition list.h:64
int n_buckets
Definition list.h:58
int mask
Definition list.h:60
uint32_t iv
Definition list.h:61
bool(* compare_function)(const void *key1, const void *key2)
Definition list.h:63
int n_elements
Definition list.h:59
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:239
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152