From: "plaisthos (Code Review)" Date: Wed, 11 Mar 2026 18:51:06 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <5d4cdedf801622149738d35c852e73a8bbc378c0-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="71eMa0cLcX8="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Change hash iv to a dynamically allocated ctx List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openvpn-devel --71eMa0cLcX8= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable plaisthos has uploaded a new patch set (#2). ( http://gerrit.openvpn.net/c/= openvpn/+/1571?usp=3Demail ) Change subject: Change hash iv to a dynamically allocated ctx ...................................................................... Change hash iv to a dynamically allocated ctx While for our own hash function, always using an uint32_t works well, it do= es not work very well if we move to another hash function. Move the context of a hardcoded uint32_t iv to a void pointer to a real ctx. Change-Id: If47c7d920b2fa4047b7db03fcde821899839324d Signed-off-by: Arne Schwabe --- M src/openvpn/list.c M src/openvpn/list.h M src/openvpn/mroute.c M src/openvpn/mroute.h M src/openvpn/multi.c M tests/unit_tests/openvpn/test_misc.c 6 files changed, 42 insertions(+), 23 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/71/1571/2 diff --git a/src/openvpn/list.c b/src/openvpn/list.c index 50defb85..36d8ede 100644 --- a/src/openvpn/list.c +++ b/src/openvpn/list.c @@ -29,13 +29,16 @@ #include "integer.h" #include "list.h" + +#include "crypto.h" #include "misc.h" #include "memdbg.h" struct hash * -hash_init(const uint32_t n_buckets, uint32_t iv, - uint64_t (*hash_function)(const void *key, uint32_t iv), +hash_init(const uint32_t n_buckets, void *ctx, + void (*free_hash_ctx)(void *ctx), + uint64_t (*hash_function)(const void *key, void *ctx), bool (*compare_function)(const void *key1, const void *key2)) { struct hash *h; @@ -46,7 +49,8 @@ h->mask =3D h->n_buckets - 1; h->hash_function =3D hash_function; h->compare_function =3D compare_function; - h->iv =3D iv; + h->free_ctx =3D free_hash_ctx; + h->ctx =3D ctx; ALLOC_ARRAY(h->buckets, struct hash_bucket, h->n_buckets); for (uint32_t i =3D 0; i < h->n_buckets; ++i) { @@ -71,6 +75,7 @@ he =3D next; } } + hash->free_ctx(hash->ctx); free(hash->buckets); free(hash); } @@ -412,6 +417,14 @@ c ^=3D (b >> 15); \ } +void * +hash_func_new_ctx(void) +{ + uint32_t *ctx =3D malloc(sizeof(uint32_t)); + *ctx =3D (uint32_t)get_random(); + return ctx; +} + uint64_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval) { diff --git a/src/openvpn/list.h b/src/openvpn/list.h index 06377c6..ea6f47a 100644 --- a/src/openvpn/list.h +++ b/src/openvpn/list.h @@ -54,14 +54,17 @@ uint32_t n_buckets; uint32_t n_elements; uint32_t mask; - uint32_t iv; - uint64_t (*hash_function)(const void *key, uint32_t iv); + void *ctx; + uint64_t (*hash_function)(const void *key, void *ctx); bool (*compare_function)(const void *key1, const void *key2); /* retur= n true if equal */ + void (*free_ctx)(void *ctx); + void *free_ctx_arg; struct hash_bucket *buckets; }; -struct hash *hash_init(const uint32_t n_buckets, const uint32_t iv, - uint64_t (*hash_function)(const void *key, uint32_t= iv), +struct hash *hash_init(const uint32_t n_buckets, void *ctx, + void (*free_hash_ctx)(void *ctx), + uint64_t (*hash_function)(const void *key, void *ct= x), bool (*compare_function)(const void *key1, const vo= id *key2)); void hash_free(struct hash *hash); @@ -100,10 +103,12 @@ uint64_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval); +void *hash_func_new_ctx(void); + static inline uint64_t hash_value(const struct hash *hash, const void *key) { - return (*hash->hash_function)(key, hash->iv); + return (*hash->hash_function)(key, hash->ctx); } static inline uint32_t diff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c index 78c689e..89b3ecc 100644 --- a/src/openvpn/mroute.c +++ b/src/openvpn/mroute.c @@ -355,10 +355,11 @@ * and the actual address. */ uint64_t -mroute_addr_hash_function(const void *key, uint32_t iv) +mroute_addr_hash_function(const void *key, void *ctx) { + uint32_t *iv =3D ctx; return hash_func(mroute_addr_hash_ptr((const struct mroute_addr *)key), - mroute_addr_hash_len((const struct mroute_addr *)key)= , iv); + mroute_addr_hash_len((const struct mroute_addr *)key)= , *iv); } bool diff --git a/src/openvpn/mroute.h b/src/openvpn/mroute.h index 2f5d019..51b62d2 100644 --- a/src/openvpn/mroute.h +++ b/src/openvpn/mroute.h @@ -144,7 +144,7 @@ bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_ar= ena *gc); -uint64_t mroute_addr_hash_function(const void *key, uint32_t iv); +uint64_t mroute_addr_hash_function(const void *key, void *ctx); bool mroute_addr_compare_function(const void *key1, const void *key2); diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 57ea3fb..200e1e7 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -229,7 +229,7 @@ #ifdef ENABLE_MANAGEMENT static uint64_t -cid_hash_function(const void *key, uint32_t iv) +cid_hash_function(const void *key, void *ctx) { const unsigned long *k =3D (const unsigned long *)key; return (uint64_t)*k; @@ -250,7 +250,7 @@ /* * inotify watcher descriptors are used as hash value */ -int_hash_function(const void *key, uint32_t iv) +int_hash_function(const void *key, void *ctx) { return (uint32_t)(uintptr_t)key; } @@ -290,18 +290,18 @@ * to determine which client sent an incoming packet * which is seen on the TCP/UDP socket. */ - m->hash =3D hash_init(t->options.real_hash_size, (uint32_t)get_random(= ), + m->hash =3D hash_init(t->options.real_hash_size, hash_func_new_ctx(), = free, mroute_addr_hash_function, mroute_addr_compare_fun= ction); /* * Virtual address hash table. Used to determine * which client to route a packet to. */ - m->vhash =3D hash_init(t->options.virtual_hash_size, (uint32_t)get_ran= dom(), + m->vhash =3D hash_init(t->options.virtual_hash_size, hash_func_new_ctx= (), free, mroute_addr_hash_function, mroute_addr_compare_fu= nction); #ifdef ENABLE_MANAGEMENT - m->cid_hash =3D hash_init(t->options.real_hash_size, 0, cid_hash_funct= ion, cid_compare_function); + m->cid_hash =3D hash_init(t->options.real_hash_size, NULL, free, cid_h= ash_function, cid_compare_function); #endif #ifdef ENABLE_ASYNC_PUSH @@ -309,8 +309,8 @@ * Mapping between inotify watch descriptors and * multi_instances. */ - m->inotify_watchers =3D hash_init(t->options.real_hash_size, (uint32_t= )get_random(), - int_hash_function, int_compare_functio= n); + m->inotify_watchers =3D + hash_init(t->options.real_hash_size, NULL, free, int_hash_function= , int_compare_function); #endif /* diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index a8383de..24bf22d 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -125,11 +125,12 @@ static uint64_t -word_hash_function(const void *key, uint32_t iv) +word_hash_function(const void *key, void *ctx) { + uint32_t *iv =3D (uint32_t *)ctx; const char *str =3D (const char *)key; const uint32_t len =3D (uint32_t)strlen(str); - return hash_func((const uint8_t *)str, len, iv); + return hash_func((const uint8_t *)str, len, *iv); } static bool @@ -171,10 +172,9 @@ * Test the hash code by implementing a simple * word frequency algorithm. */ - struct gc_arena gc =3D gc_new(); - struct hash *hash =3D hash_init(10000, (uint32_t)get_random(), word_ha= sh_function, word_compare_function); - struct hash *nhash =3D hash_init(256, (uint32_t)get_random(), word_has= h_function, word_compare_function); + struct hash *hash =3D hash_init(10000, hash_func_new_ctx(), free, word= _hash_function, word_compare_function); + struct hash *nhash =3D hash_init(256, hash_func_new_ctx(), free, word_= hash_function, word_compare_function); printf("hash_init n_buckets=3D%u mask=3D0x%08x\n", hash->n_buckets, ha= sh->mask); --=20 To view, visit http://gerrit.openvpn.net/c/openvpn/+/1571?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: If47c7d920b2fa4047b7db03fcde821899839324d Gerrit-Change-Number: 1571 Gerrit-PatchSet: 2 Gerrit-Owner: plaisthos Gerrit-CC: openvpn-devel --71eMa0cLcX8= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

plaisthos uploaded patch set #2 to this change.

View Change

Change hash iv to a dynamically allocated ctx

While for our own = hash function, always using an uint32_t works well, it does
not work ver= y well if we move to another hash function. Move the context of
a hardco= ded uint32_t iv to a void pointer to a real ctx.

Change-Id: If47c7d9= 20b2fa4047b7db03fcde821899839324d
Signed-off-by: Arne Schwabe <arne@...220...= ..1227...>
---
M src/openvpn/list.c
M src/openvpn/list.h
M s= rc/openvpn/mroute.c
M src/openvpn/mroute.h
M src/openvpn/multi.c
M= tests/unit_tests/openvpn/test_misc.c
6 files changed, 42 insertions(+),= 23 deletions(-)

git pull ssh://gerrit.openvpn.n=
et:29418/openvpn refs/changes/71/1571/2
diff --git a/src/openvpn/li=
st.c b/src/openvpn/list.c
index 50defb85..36d8ede 100644
--- a/src/openvpn/list.c
+++ b/src/openvpn/li= st.c
@@ -29,13 +29,16 @@

= #include "integer.h"
#include "list.h"= ;
+
+#include "crypto.h"<= br> #include "misc.h"

#i= nclude "memdbg.h"

struct hash = *
-hash_init(const uint= 32_t n_buckets, uint32_t iv,
- uint64_t (*hash_function)(const void *key, uint32_t iv),
+hash_init(const uint3= 2_t n_buckets, void *ctx,
+ void (*free_hash_ctx)(void *ctx),
+ uint64_t (*hash_function)(const voi= d *key, void *ctx),
bool (*compare_function)(con= st void *key1, const void *key2))
{
s= truct hash *h;
@@ -46,7 +49,8 @@
h->= ;mask =3D h->n_buckets - 1;
h->hash_function =3D= hash_function;
h->compare_function =3D compare_fun= ction;
- h->iv = =3D iv;
+ h->fr= ee_ctx =3D free_hash_ctx;
+ h->ctx =3D ctx;
ALLOC_ARRAY(h->buckets= , struct hash_bucket, h->n_buckets);
for (uint32_t = i =3D 0; i < h->n_buckets; ++i)
{
@@ -71,6 +75,7 @@

he =3D next;
}

}
+ hash->free_ctx(hash->ctx);
= free(hash->buckets);
free(hash);
}=
@@ -412,6 +417,14 @@
c ^=3D (b &g= t;> 15); \
}

+void *
+hash_func_new_ctx(void)
+{
+ uint32_t *ctx =3D malloc(sizeof(uint32_t));
+ *ctx =3D (uint32_t)get_random();
+ return ctx;=
+}
+
uint64_t
= hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
{

diff --git a/src/openvpn/list.h b/src/openvpn/list.h=
index 06377c6..ea6f47a 100644
--- a/src/op= envpn/list.h
+++ b/src/openvpn/list.h
@@ -5= 4,14 +54,17 @@
uint32_t n_buckets;
= uint32_t n_elements;
uint32_t mask;
- uint32_t iv;
- uint64_t (*hash_function)(const void *k= ey, uint32_t iv);
+ = void *ctx;
+ uin= t64_t (*hash_function)(const void *key, void *ctx);
bo= ol (*compare_function)(const void *key1, const void *key2); /* return true = if equal */
+ void= (*free_ctx)(void *ctx);
+ void *free_ctx_arg;
struct hash_bucket *bucke= ts;
};

-struct hash *hash_init(const uint32_t n_buckets, const = uint32_t iv,
- = uint64_t (*hash_function)(const void *key, uint32_t iv),
+struct hash *hash_init(c= onst uint32_t n_buckets, void *ctx,
+ void (*free_hash_ctx)(void *ctx),
+ = uint64_t (*hash_function)(const void *key, void *ctx),
= bool (*compare_function)(const void *key1, const void = *key2));

void hash_free(struct hash *has= h);
@@ -100,10 +103,12 @@

uint64_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);<= /span>

+vo= id *hash_func_new_ctx(void);
+
static inline uint64_t
hash_val= ue(const struct hash *hash, const void *key)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- return (*hash->hash_functi= on)(key, hash->iv);

-m= route_addr_hash_function(const void *key, uint32_t iv)
+mroute_addr_hash_function(const void *ke= y, void *ctx)
{
+ uint32_t *iv =3D ctx;
return hash_fun= c(mroute_addr_hash_ptr((const struct mroute_addr *)key),
- mroute_addr_hash_le= n((const struct mroute_addr *)key), iv);
+ mroute_addr_hash_len((const struc= t mroute_addr *)key), *iv);
}

= bool
diff --git a/src/openvpn/mroute.h b/src/openvpn= /mroute.h
index 2f5d019..51b62d2 100644
---= a/src/openvpn/mroute.h
+++ b/src/openvpn/mroute.h@@ -144,7 +144,7 @@

bool mroute_= learnable_address(const struct mroute_addr *addr, struct gc_arena *gc);

-uint64_= t mroute_addr_hash_function(const void *key, uint32_t iv);
+uint64_t mroute_addr_hash_function(c= onst void *key, void *ctx);

bool mroute_= addr_compare_function(const void *key1, const void *key2);
=
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 57ea3fb..200e1e7 100644
--- a/src/openv= pn/multi.c
+++ b/src/openvpn/multi.c
@@ -22= 9,7 +229,7 @@
#ifdef ENABLE_MANAGEMENT

static uint64_t
-cid_hash_function(const void *key, uint32_t iv)
+cid_hash_function(const void *key, vo= id *ctx)
{
const unsigned long *k =3D= (const unsigned long *)key;
return (uint64_t)*k;
@@ -250,7 +250,7 @@
/*
* ino= tify watcher descriptors are used as hash value
*/=
-int_hash_function(const void= *key, uint32_t iv)
+= int_hash_function(const void *key, void *ctx)
{
= return (uint32_t)(uintptr_t)key;
}
@@ -290,18 +290,18 @@

* to determine which client= sent an incoming packet
* which is seen on the TCP/U= DP socket.
*/
- m->hash =3D hash_init(t->options.real_hash_size, (u= int32_t)get_random(),
+ m->hash =3D hash_init(t->options.real_hash_size, hash_func_new_= ctx(), free,
mroute_addr_hash_func= tion, mroute_addr_compare_function);

= /*
* Virtual address hash table. Used to determine<= /span>
* which client to route a packet to.
= */
- m->vha= sh =3D hash_init(t->options.virtual_hash_size, (uint32_t)get_random(),
+ m->vhash =3D h= ash_init(t->options.virtual_hash_size, hash_func_new_ctx(), free,=
mroute_addr_hash_function, mroute_addr_= compare_function);

#ifdef ENABLE_MANAGEM= ENT
- m->cid_hash= =3D hash_init(t->options.real_hash_size, 0, cid_hash_function, cid_comp= are_function);
+ m= ->cid_hash =3D hash_init(t->options.real_hash_size, NULL, free, cid_h= ash_function, cid_compare_function);
#endif

#ifdef ENABLE_ASYNC_PUSH
@@ -309,8 +30= 9,8 @@
* Mapping between inotify watch descriptors an= d
* multi_instances.
*/<= br>- m->inotify_watchers = =3D hash_init(t->options.real_hash_size, (uint32_t)get_random(),<= br>- = int_hash_function, int_compare_function);
+ m->inotify_watchers =3D
+ hash_init(t->options.rea= l_hash_size, NULL, free, int_hash_function, int_compare_function);
#endif

/*
d= iff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn= /test_misc.c
index a8383de..24bf22d 100644
= --- a/tests/unit_tests/openvpn/test_misc.c
+++ b/tests/unit= _tests/openvpn/test_misc.c
@@ -125,11 +125,12 @@
=

static uint64_t
-word_hash_function(const void *key, ui= nt32_t iv)
+word_hash= _function(const void *key, void *ctx)
{
+ uint32_t *iv =3D (uint32_t *)ctx;<= /span>
const char *str =3D (const char *)key;
const uint32_t len =3D (uint32_t)strlen(str);

- return hash_func((const uint8_t *)str, = len, iv);
+ return= hash_func((const uint8_t *)str, len, *iv);
}

static bool
@@ -171,10 +172,9 @@
* Test the hash code by implementing a simple
= * word frequency algorithm.
*/
= -
struct gc_= arena gc =3D gc_new();
= - struct hash *hash =3D hash_init(10000, (uint32_t)get_random(), word_ha= sh_function, word_compare_function);
- struct hash *nhash =3D hash_init(256, (uint32_t)get_rand= om(), word_hash_function, word_compare_function);
+ struct hash *hash =3D hash_init(10000, ha= sh_func_new_ctx(), free, word_hash_function, word_compare_function);=
+ struct hash *nhash =3D= hash_init(256, hash_func_new_ctx(), free, word_hash_function, word_compare= _function);

printf("hash_init n= _buckets=3D%u mask=3D0x%08x\n", hash->n_buckets, hash->mask);


To view, visit change 1571= . To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If47c7d920b2fa4047b7db03fcde= 821899839324d
Gerrit-Change-Number: 1571
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --71eMa0cLcX8=--