[PATCH v4] Extract multi_get_instance_udp_real method

Gert Doering <[email protected]>
Newsgroups gmane.network.openvpn.devel
Message-ID <[email protected]>
From: Arne Schwabe <[email protected]>

This is just a small refactoring to make the code a little bit
better organised.

Change-Id: I37ade55d7674c68dd98cf4dfde35c4ad42e20d39
Signed-off-by: Arne Schwabe <[email protected]>
Acked-by: Frank Lichtenheld <[email protected]>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1725
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1725
This mail reflects revision 4 of this Change.

Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <[email protected]>

        
diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c
index 9acf297..d22c53b 100644
--- a/src/openvpn/mudp.c
+++ b/src/openvpn/mudp.c
@@ -205,7 +205,6 @@
 handle_connection_attempt(struct multi_context *m,
                           struct link_socket *sock,
                           struct mroute_addr *real,
-                          const uint64_t hv,
                           struct hash_bucket *bucket)
 {
     struct hash *hash = m->hash;
@@ -236,6 +235,7 @@
             mi = multi_create_instance(m, real, sock);
             if (mi)
             {
+                const uint64_t hv = hash_value(hash, real);
                 hash_add_fast(hash, bucket, &mi->real, hv, mi);
                 mi->did_real_hash = true;
                 multi_assign_peer_id(m, mi);
@@ -265,6 +265,28 @@
 }
 
 /**
+ * Looks up an multi instance by its real address (IP and port)
+ * @param m     multi context
+ * @param real  Address to look up
+ * @return      instance matching the address, NULL otherwise
+ */
+static struct multi_instance *
+multi_get_instance_udp_real(struct multi_context *m, struct mroute_addr *real)
+{
+    struct hash *hash = m->hash;
+    struct hash_element *he;
+    const uint64_t hv = hash_value(hash, real);
+    struct hash_bucket *bucket = hash_bucket(hash, hv);
+    he = hash_lookup_fast(hash, bucket, real, hv);
+    if (he)
+    {
+        return he->value;
+    }
+    return NULL;
+}
+
+
+/**
  * Get a client instance based on real address.  If
  * the instance doesn't exist, create it while
  * maintaining real address hash table atomicity.
@@ -280,7 +302,6 @@
 
     if (mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true) && m->top.c2.buf.len > 0)
     {
-        struct hash_element *he;
         const uint64_t hv = hash_value(hash, &real);
         struct hash_bucket *bucket = hash_bucket(hash, hv);
         uint8_t *ptr = BPTR(&m->top.c2.buf);
@@ -317,17 +338,13 @@
         }
         if (!v2 || peer_id_disabled)
         {
-            he = hash_lookup_fast(hash, bucket, &real, hv);
-            if (he)
-            {
-                mi = (struct multi_instance *)he->value;
-            }
+            mi = multi_get_instance_udp_real(m, &real);
         }
 
         /* we have no existing multi instance for this connection */
         if (!mi)
         {
-            mi = handle_connection_attempt(m, sock, &real, hv, bucket);
+            mi = handle_connection_attempt(m, sock, &real, bucket);
         }
 
 #ifdef ENABLE_DEBUG
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.