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
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19
* You should have received a copy of the GNU General Public License
20
* along with this program (see the file COPYING included with this
21
* distribution); if not, write to the Free Software Foundation, Inc.,
22
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
* This code is a fairly straightforward hash
30
* table implementation using Bob Jenkins'
33
* Hash tables are used in OpenVPN to keep track of
34
* client instances over various key spaces.
39
/* define this to enable special list test mode */
45
#define hashsize(n) ((uint32_t)1<<(n))
46
#define hashmask(n) (hashsize(n)-1)
52
unsigned int hash_value;
53
struct hash_element *next;
58
struct hash_element *list;
67
uint32_t (*hash_function)(const void *key, uint32_t iv);
68
bool (*compare_function)(const void *key1, const void *key2); /* return true if equal */
69
struct hash_bucket *buckets;
72
struct hash *hash_init (const int n_buckets,
74
uint32_t (*hash_function)(const void *key, uint32_t iv),
75
bool (*compare_function)(const void *key1, const void *key2));
77
void hash_free (struct hash *hash);
79
bool hash_add (struct hash *hash, const void *key, void *value, bool replace);
81
struct hash_element *hash_lookup_fast (struct hash *hash,
82
struct hash_bucket *bucket,
86
bool hash_remove_fast (struct hash *hash,
87
struct hash_bucket *bucket,
91
void hash_remove_by_value (struct hash *hash, void *value);
97
struct hash_bucket *bucket;
98
struct hash_element *elem;
99
struct hash_element *last;
101
int bucket_index_start;
102
int bucket_index_end;
105
void hash_iterator_init_range (struct hash *hash,
106
struct hash_iterator *hi,
110
void hash_iterator_init (struct hash *hash, struct hash_iterator *iter);
111
struct hash_element *hash_iterator_next (struct hash_iterator *hi);
112
void hash_iterator_delete_element (struct hash_iterator *hi);
113
void hash_iterator_free (struct hash_iterator *hi);
115
uint32_t hash_func (const uint8_t *k, uint32_t length, uint32_t initval);
117
uint32_t void_ptr_hash_function (const void *key, uint32_t iv);
118
bool void_ptr_compare_function (const void *key1, const void *key2);
121
void list_test (void);
124
static inline uint32_t
125
hash_value (const struct hash *hash, const void *key)
127
return (*hash->hash_function)(key, hash->iv);
131
hash_n_elements (const struct hash *hash)
133
return hash->n_elements;
137
hash_n_buckets (const struct hash *hash)
139
return hash->n_buckets;
142
static inline struct hash_bucket *
143
hash_bucket (struct hash *hash, uint32_t hv)
145
return &hash->buckets[hv & hash->mask];
149
hash_lookup (struct hash *hash, const void *key)
152
struct hash_element *he;
153
uint32_t hv = hash_value (hash, key);
154
struct hash_bucket *bucket = &hash->buckets[hv & hash->mask];
156
he = hash_lookup_fast (hash, bucket, key, hv);
163
/* NOTE: assumes that key is not a duplicate */
165
hash_add_fast (struct hash *hash,
166
struct hash_bucket *bucket,
171
struct hash_element *he;
173
ALLOC_OBJ (he, struct hash_element);
177
he->next = bucket->list;
183
hash_remove (struct hash *hash, const void *key)
186
struct hash_bucket *bucket;
189
hv = hash_value (hash, key);
190
bucket = &hash->buckets[hv & hash->mask];
191
ret = hash_remove_fast (hash, bucket, key, hv);
195
#endif /* P2MP_SERVER */