[Openvpn-devel,12/14] Extract multi_assign_peer_id into its own function

Message ID 20210401131337.3684-13-arne@rfc2549.org
State Accepted
Headers show
Series Various clean up patches | expand

Commit Message

Arne Schwabe April 1, 2021, 2:13 a.m. UTC
This makes multi_get_create_instance_udp a bit shorter and better
structured and also prepares this method to be called from the
mutlti TCP context with DCO which will also need to assign unique peer
ids to instances.

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
---
 src/openvpn/mudp.c  | 20 +-------------------
 src/openvpn/multi.c | 21 +++++++++++++++++++++
 src/openvpn/multi.h | 10 ++++++++++
 3 files changed, 32 insertions(+), 19 deletions(-)

Comments

Gert Doering April 2, 2021, 4:13 a.m. UTC | #1
Acked-by: Gert Doering <gert@greenie.muc.de>

Simple enough :-) - I have not tested this very very thoroughly, but
one of my server test instances has 3 parallel clients, so I could at
least see that they do get assigned different IDs (and that it compiles).

Your patch has been applied to the master branch.

commit 9fe0b2c287609cefe79aec30ff33f7a65cafd684
Author: Arne Schwabe
Date:   Thu Apr 1 15:13:35 2021 +0200

     Extract multi_assign_peer_id into its own function

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Message-Id: <20210401131337.3684-13-arne@rfc2549.org>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg21959.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c
index 90e32a8ec..9225d42de 100644
--- a/src/openvpn/mudp.c
+++ b/src/openvpn/mudp.c
@@ -103,27 +103,9 @@  multi_get_create_instance_udp(struct multi_context *m, bool *floated)
                     mi = multi_create_instance(m, &real);
                     if (mi)
                     {
-                        int i;
-
                         hash_add_fast(hash, bucket, &mi->real, hv, mi);
                         mi->did_real_hash = true;
-
-                        /* max_clients must be less then max peer-id value */
-                        ASSERT(m->max_clients < MAX_PEER_ID);
-
-                        for (i = 0; i < m->max_clients; ++i)
-                        {
-                            if (!m->instances[i])
-                            {
-                                mi->context.c2.tls_multi->peer_id = i;
-                                m->instances[i] = mi;
-                                break;
-                            }
-                        }
-
-                        /* should not really end up here, since multi_create_instance returns null
-                         * if amount of clients exceeds max_clients */
-                        ASSERT(i < m->max_clients);
+                        multi_assign_peer_id(m, mi);
                     }
                 }
                 else
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 071bd5b61..d4c5304cb 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -4016,6 +4016,27 @@  init_management_callback_multi(struct multi_context *m)
 #endif /* ifdef ENABLE_MANAGEMENT */
 }
 
+void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi)
+{
+    /* max_clients must be less then max peer-id value */
+    ASSERT(m->max_clients < MAX_PEER_ID);
+
+    for (int i = 0; i < m->max_clients; ++i)
+    {
+        if (!m->instances[i])
+        {
+            mi->context.c2.tls_multi->peer_id = i;
+            m->instances[i] = mi;
+            break;
+        }
+    }
+
+    /* should not really end up here, since multi_create_instance returns null
+     * if amount of clients exceeds max_clients */
+    ASSERT(mi->context.c2.tls_multi->peer_id < m->max_clients);
+}
+
+
 /*
  * Top level event loop.
  */
diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
index b7078b711..9d771f537 100644
--- a/src/openvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -676,5 +676,15 @@  multi_set_pending(struct multi_context *m, struct multi_instance *mi)
 {
     m->pending = mi;
 }
+/**
+ * Assigns a peer-id to a a client and adds the instance to the
+ * the instances array of the \c multi_context structure.
+ *
+ * @param m            - The single \c multi_context structure.
+ * @param mi           - The \c multi_instance of the VPN tunnel to be
+ *                       postprocessed.
+ */
+void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi);
+
 
 #endif /* MULTI_H */