[Openvpn-devel,v2,1/7] tun.c: make Windows device lookup functions more general

Message ID 20191220161117.1434-1-simon@rozman.si
State Accepted
Headers show
Series [Openvpn-devel,v2,1/7] tun.c: make Windows device lookup functions more general | expand

Commit Message

Simon Rozman Dec. 20, 2019, 5:11 a.m. UTC
Since the introduction of Wintun, not all network devices in Windows are
TAP-Windows6. Rather than returning a simple true/false answer, a couple
of functions were reworked to return a corresponding struct tap_reg *
or NULL instead.

As it would make the code `tr = is_tap_win(...)` a bit awkward those
functions (both static) were renamed to better reflect their nature.

Signed-off-by: Simon Rozman <simon@rozman.si>
---
 src/openvpn/tun.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

Comments

Lev Stipakov Dec. 20, 2019, 7:20 a.m. UTC | #1
Compared with v1, only changes are (suggested) function names.

Compiled with MSVC.

Acked-by: Lev Stipakov <lstipakov@gmail.com>

pe 20. jouluk. 2019 klo 18.14 Simon Rozman (simon@rozman.si) kirjoitti:

> Since the introduction of Wintun, not all network devices in Windows are
> TAP-Windows6. Rather than returning a simple true/false answer, a couple
> of functions were reworked to return a corresponding struct tap_reg *
> or NULL instead.
>
> As it would make the code `tr = is_tap_win(...)` a bit awkward those
> functions (both static) were renamed to better reflect their nature.
>
> Signed-off-by: Simon Rozman <simon@rozman.si>
> ---
>  src/openvpn/tun.c | 39 ++++++++++++++++++++++-----------------
>  1 file changed, 22 insertions(+), 17 deletions(-)
>
> diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
> index ad497a71..0d6f40fe 100644
> --- a/src/openvpn/tun.c
> +++ b/src/openvpn/tun.c
> @@ -3976,10 +3976,10 @@ show_tap_win_adapters(int msglev, int warnlev)
>  }
>
>  /*
> - * Confirm that GUID is a TAP-Windows adapter.
> + * Lookup a TAP-Windows or Wintun adapter by GUID.
>   */
> -static bool
> -is_tap_win(const char *guid, const struct tap_reg *tap_reg)
> +static const struct tap_reg *
> +get_adapter_by_guid(const char *guid, const struct tap_reg *tap_reg)
>  {
>      const struct tap_reg *tr;
>
> @@ -3987,11 +3987,11 @@ is_tap_win(const char *guid, const struct tap_reg
> *tap_reg)
>      {
>          if (guid && !strcmp(tr->guid, guid))
>          {
> -            return true;
> +            return tr;
>          }
>      }
>
> -    return false;
> +    return NULL;
>  }
>
>  static const char *
> @@ -4010,16 +4010,16 @@ guid_to_name(const char *guid, const struct
> panel_reg *panel_reg)
>      return NULL;
>  }
>
> -static const char *
> -name_to_guid(const char *name, const struct tap_reg *tap_reg, const
> struct panel_reg *panel_reg)
> +static const struct tap_reg *
> +get_adapter_by_name(const char *name, const struct tap_reg *tap_reg,
> const struct panel_reg *panel_reg)
>  {
>      const struct panel_reg *pr;
>
>      for (pr = panel_reg; pr != NULL; pr = pr->next)
>      {
> -        if (name && !strcmp(pr->name, name) && is_tap_win(pr->guid,
> tap_reg))
> +        if (name && !strcmp(pr->name, name))
>          {
> -            return pr->guid;
> +            return get_adapter_by_guid(pr->guid, tap_reg);
>          }
>      }
>
> @@ -4116,6 +4116,7 @@ get_device_guid(const char *name,
>  {
>      struct buffer ret = alloc_buf_gc(256, gc);
>      struct buffer actual = clear_buf();
> +    const struct tap_reg *tr;
>
>      /* Make sure we have at least one TAP adapter */
>      if (!tap_reg)
> @@ -4131,7 +4132,8 @@ get_device_guid(const char *name,
>      }
>
>      /* Check if GUID was explicitly specified as --dev-node parameter */
> -    if (is_tap_win(name, tap_reg))
> +    tr = get_adapter_by_guid(name, tap_reg);
> +    if (tr)
>      {
>          const char *act = guid_to_name(name, panel_reg);
>          buf_printf(&ret, "%s", name);
> @@ -4148,11 +4150,11 @@ get_device_guid(const char *name,
>
>      /* Lookup TAP adapter in network connections list */
>      {
> -        const char *guid = name_to_guid(name, tap_reg, panel_reg);
> -        if (guid)
> +        tr = get_adapter_by_name(name, tap_reg, panel_reg);
> +        if (tr)
>          {
>              buf_printf(&actual, "%s", name);
> -            buf_printf(&ret, "%s", guid);
> +            buf_printf(&ret, "%s", tr->guid);
>              return BSTR(&ret);
>          }
>      }
> @@ -4696,11 +4698,14 @@ get_adapter_index_flexible(const char *name)  /*
> actual name or GUID */
>      {
>          const struct tap_reg *tap_reg = get_tap_reg(&gc);
>          const struct panel_reg *panel_reg = get_panel_reg(&gc);
> -        const char *guid = name_to_guid(name, tap_reg, panel_reg);
> -        index = get_adapter_index_method_1(guid);
> -        if (index == TUN_ADAPTER_INDEX_INVALID)
> +        const struct tap_reg *tr = get_adapter_by_name(name, tap_reg,
> panel_reg);
> +        if (tr)
>          {
> -            index = get_adapter_index_method_2(guid);
> +            index = get_adapter_index_method_1(tr->guid);
> +            if (index == TUN_ADAPTER_INDEX_INVALID)
> +            {
> +                index = get_adapter_index_method_2(tr->guid);
> +            }
>          }
>      }
>      if (index == TUN_ADAPTER_INDEX_INVALID)
> --
> 2.24.1.windows.2
>
>
>
> _______________________________________________
> Openvpn-devel mailing list
> Openvpn-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openvpn-devel
>
Gert Doering Dec. 22, 2019, 7:50 a.m. UTC | #2
Your patch has been applied to the master branch.

Lev: if there is a whole new v2 series, it would be good to have a bit
more verbose ACKs on the v2 patches than just "it's nearly the same as 
v1, so ACK" - if you haven't read the v1 thread (because "oh, a v2 is 
coming, so I can wait") this means you actually have to go back if you're 
interested...

Compile tested on Ubuntu 1604 and lightly stared at code.

commit be78706830f02b095392ff063c9490da27b6faa1
Author: Simon Rozman
Date:   Fri Dec 20 17:11:11 2019 +0100

     tun.c: make Windows device lookup functions more general

     Signed-off-by: Simon Rozman <simon@rozman.si>
     Acked-by: Lev Stipakov <lstipakov@gmail.com>
     Message-Id: <20191220161117.1434-1-simon@rozman.si>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg19280.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index ad497a71..0d6f40fe 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -3976,10 +3976,10 @@  show_tap_win_adapters(int msglev, int warnlev)
 }
 
 /*
- * Confirm that GUID is a TAP-Windows adapter.
+ * Lookup a TAP-Windows or Wintun adapter by GUID.
  */
-static bool
-is_tap_win(const char *guid, const struct tap_reg *tap_reg)
+static const struct tap_reg *
+get_adapter_by_guid(const char *guid, const struct tap_reg *tap_reg)
 {
     const struct tap_reg *tr;
 
@@ -3987,11 +3987,11 @@  is_tap_win(const char *guid, const struct tap_reg *tap_reg)
     {
         if (guid && !strcmp(tr->guid, guid))
         {
-            return true;
+            return tr;
         }
     }
 
-    return false;
+    return NULL;
 }
 
 static const char *
@@ -4010,16 +4010,16 @@  guid_to_name(const char *guid, const struct panel_reg *panel_reg)
     return NULL;
 }
 
-static const char *
-name_to_guid(const char *name, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg)
+static const struct tap_reg *
+get_adapter_by_name(const char *name, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg)
 {
     const struct panel_reg *pr;
 
     for (pr = panel_reg; pr != NULL; pr = pr->next)
     {
-        if (name && !strcmp(pr->name, name) && is_tap_win(pr->guid, tap_reg))
+        if (name && !strcmp(pr->name, name))
         {
-            return pr->guid;
+            return get_adapter_by_guid(pr->guid, tap_reg);
         }
     }
 
@@ -4116,6 +4116,7 @@  get_device_guid(const char *name,
 {
     struct buffer ret = alloc_buf_gc(256, gc);
     struct buffer actual = clear_buf();
+    const struct tap_reg *tr;
 
     /* Make sure we have at least one TAP adapter */
     if (!tap_reg)
@@ -4131,7 +4132,8 @@  get_device_guid(const char *name,
     }
 
     /* Check if GUID was explicitly specified as --dev-node parameter */
-    if (is_tap_win(name, tap_reg))
+    tr = get_adapter_by_guid(name, tap_reg);
+    if (tr)
     {
         const char *act = guid_to_name(name, panel_reg);
         buf_printf(&ret, "%s", name);
@@ -4148,11 +4150,11 @@  get_device_guid(const char *name,
 
     /* Lookup TAP adapter in network connections list */
     {
-        const char *guid = name_to_guid(name, tap_reg, panel_reg);
-        if (guid)
+        tr = get_adapter_by_name(name, tap_reg, panel_reg);
+        if (tr)
         {
             buf_printf(&actual, "%s", name);
-            buf_printf(&ret, "%s", guid);
+            buf_printf(&ret, "%s", tr->guid);
             return BSTR(&ret);
         }
     }
@@ -4696,11 +4698,14 @@  get_adapter_index_flexible(const char *name)  /* actual name or GUID */
     {
         const struct tap_reg *tap_reg = get_tap_reg(&gc);
         const struct panel_reg *panel_reg = get_panel_reg(&gc);
-        const char *guid = name_to_guid(name, tap_reg, panel_reg);
-        index = get_adapter_index_method_1(guid);
-        if (index == TUN_ADAPTER_INDEX_INVALID)
+        const struct tap_reg *tr = get_adapter_by_name(name, tap_reg, panel_reg);
+        if (tr)
         {
-            index = get_adapter_index_method_2(guid);
+            index = get_adapter_index_method_1(tr->guid);
+            if (index == TUN_ADAPTER_INDEX_INVALID)
+            {
+                index = get_adapter_index_method_2(tr->guid);
+            }
         }
     }
     if (index == TUN_ADAPTER_INDEX_INVALID)