[Openvpn-devel] Improve shuffling algorithm of connection list

Message ID 20241115194202.39508-1-shujifurukawa1213@gmail.com
State Superseded
Headers show
Series [Openvpn-devel] Improve shuffling algorithm of connection list | expand

Commit Message

Hurukawa2121 Nov. 15, 2024, 7:42 p.m. UTC
From: Hurukawa2121 <shujifurukawa1213@gamil.com>

---
 Improve shuffling algorithm of connection list

 This patch implements the Fisher-Yates shuffle algorithm to ensure that all permutations
 of the connection target list are generated with equal probability, eliminating
 biases present in the previous shuffling method.
 In the Fisher-Yates algorithm, there's only one way to obtain each permutation
 through these swaps, so all permutations occur with equal probability in theory.

 Signed-off-by: Hurukawa2121 <shujifurukawa1213@gmail.com>

 src/openvpn/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Patch

diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index 9371024e..3b00b49d 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -478,9 +478,9 @@  init_connection_list(struct context *c)
     if (c->options.remote_random)
     {
         int i;
-        for (i = 0; i < l->len; ++i)
+        for (i = l->len - 1; i > 0; --i)
         {
-            const int j = get_random() % l->len;
+            const int j = get_random() % (i + 1);
             if (i != j)
             {
                 struct connection_entry *tmp;