Skip to contentSkip to conversation

wintun: refactor code to use enum driver type

PATCH In review Simon Rozman 3 participants 4 messages Jan 16, 2020 – Jan 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wintun: refactor code to use enum driver type

---
 src/openvpn/forward.c |   4 +-
 src/openvpn/init.c    |   2 +-
 src/openvpn/options.c |  16 +++----
 src/openvpn/options.h |   2 +-
 src/openvpn/tun.c     | 108 ++++++++++++++++++++----------------------
 src/openvpn/tun.h     |  16 +++++--
 6 files changed, 75 insertions(+), 73 deletions(-)
src/openvpn/forward.c +2 -2
index 6b823613..ea10f0bf 100644
@@ -1258,7 +1258,7 @@ read_incoming_tun(struct context *c)
     c->c2.buf = c->c2.buffers->read_tun_buf;
 
 #ifdef _WIN32
-    if (c->c1.tuntap->wintun)
+    if (c->c1.tuntap->windows_driver == WINDOWS_DRIVER_WINTUN)
     {
         read_wintun(c->c1.tuntap, &c->c2.buf);
         if (c->c2.buf.len == -1)
@@ -1274,7 +1274,7 @@ read_incoming_tun(struct context *c)
     {
         read_tun_buffered(c->c1.tuntap, &c->c2.buf);
     }
-#else
+#else  /* ifdef _WIN32 */
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
     c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
src/openvpn/init.c +1 -1
index 0bdb0a9c..ec444f47 100644
@@ -1734,7 +1734,7 @@ do_init_tun(struct context *c)
                             &c->net_ctx);
 
 #ifdef _WIN32
-    c->c1.tuntap->wintun = c->options.wintun;
+    c->c1.tuntap->windows_driver = c->options.windows_driver;
 #endif
 
     init_tun_post(c->c1.tuntap,
src/openvpn/options.c +8 -8
index a6f40e10..709ba4bb 100644
@@ -854,7 +854,7 @@ init_options(struct options *o, const bool init_gc)
     o->tuntap_options.dhcp_masq_offset = 0;     /* use network address as internal DHCP server address */
     o->route_method = ROUTE_METHOD_ADAPTIVE;
     o->block_outside_dns = false;
-    o->wintun = false;
+    o->windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6;
 #endif
     o->vlan_accept = VLAN_ALL;
     o->vlan_pvid = 1;
@@ -3002,7 +3002,7 @@ options_postprocess_mutate_invariant(struct options *options)
 
 #ifdef _WIN32
     /* when using wintun, kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
-    if (options->wintun)
+    if (options->windows_driver == WINDOWS_DRIVER_WINTUN)
     {
         options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
@@ -4076,23 +4076,23 @@ foreign_option(struct options *o, char *argv[], int len, struct env_set *es)
  *
  * @param str       value of --windows-driver option
  * @param msglevel  msglevel to report parsing error
- * @return bool     true if --windows-driver is wintun, false otherwise
+ * @return enum windows_driver_type  driver type, WINDOWS_DRIVER_UNSPECIFIED on unknown --windows-driver value
  */
-static bool
+static enum windows_driver_type
 parse_windows_driver(const char *str, const int msglevel)
 {
     if (streq(str, "tap-windows6"))
     {
-        return false;
+        return WINDOWS_DRIVER_TAP_WINDOWS6;
     }
     else if (streq(str, "wintun"))
     {
-        return true;
+        return WINDOWS_DRIVER_WINTUN;
     }
     else
     {
         msg(msglevel, "--windows-driver must be tap-windows6 or wintun");
-        return false;
+        return WINDOWS_DRIVER_UNSPECIFIED;
     }
 }
 #endif
@@ -5367,7 +5367,7 @@ add_option(struct options *options,
     else if (streq(p[0], "windows-driver") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
-        options->wintun = parse_windows_driver(p[1], M_FATAL);
+        options->windows_driver = parse_windows_driver(p[1], M_FATAL);
     }
 #endif
     else if (streq(p[0], "dev-node") && p[1] && !p[2])
src/openvpn/options.h +1 -1
index 7fd2c00f..84d05f26 100644
@@ -634,7 +634,7 @@ struct options
     bool show_net_up;
     int route_method;
     bool block_outside_dns;
-    bool wintun;
+    enum windows_driver_type windows_driver;
 #endif
 
     bool use_peer_id;
src/openvpn/tun.c +52 -56

263 lines not on this page · raw message

src/openvpn/tun.h +11 -5
index b0b80d1a..97d28f45 100644
@@ -43,6 +43,12 @@
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
+
+enum windows_driver_type {
+    WINDOWS_DRIVER_UNSPECIFIED,
+    WINDOWS_DRIVER_TAP_WINDOWS6,
+    WINDOWS_DRIVER_WINTUN
+};
 #endif
 
 #if defined(_WIN32) || defined(TARGET_ANDROID)
@@ -181,7 +187,7 @@ struct tuntap
      * ~0 if undefined */
     DWORD adapter_index;
 
-    bool wintun; /* true if wintun is used instead of tap-windows6 */
+    enum windows_driver_type windows_driver;
     int standby_iter;
 
     HANDLE wintun_send_ring_handle;
@@ -221,7 +227,7 @@ tuntap_defined(const struct tuntap *tt)
 inline bool
 tuntap_is_wintun(struct tuntap *tt)
 {
-    return tt && tt->wintun;
+    return tt && tt->windows_driver == WINDOWS_DRIVER_WINTUN;
 }
 
 inline bool
@@ -365,7 +371,7 @@ route_order(void)
 struct tap_reg
 {
     const char *guid;
-    bool wintun;
+    enum windows_driver_type windows_driver;
     struct tap_reg *next;
 };
 
@@ -642,7 +648,7 @@ write_wintun(struct tuntap *tt, struct buffer *buf)
 static inline int
 write_tun_buffered(struct tuntap *tt, struct buffer *buf)
 {
-    if (tt->wintun)
+    if (tt->windows_driver == WINDOWS_DRIVER_WINTUN)
     {
         return write_wintun(tt, buf);
     }
@@ -712,7 +718,7 @@ tun_set(struct tuntap *tt,
             }
         }
 #ifdef _WIN32
-        if (!tt->wintun && (rwflags & EVENT_READ))
+        if (tt->windows_driver == WINDOWS_DRIVER_TAP_WINDOWS6 && (rwflags & EVENT_READ))
         {
             tun_read_queue(tt, 0);
         }
Signature · 1 line
2.24.1.windows.2
Acked-by on v1 1/1
#1 lore raw

Hi,

While we've discussed this during hackathon, it would nevertheless be nice to explain in commit message
why this is needed (something like make code more device type agnostic).

Also, this line

+ msg(M_FATAL, "Adapter '%s' is using %s driver, %s expected.
If you want to use this device, adjust --windows-driver.", dev_node, print_windows_driver(windows_driver), print_windows_driver(tt->windows_driver));

is a bit too long, it would be nice to split it.

Other than that, looks good.

Built and tested with MSVC.

Signature · 1 line
-Lev
#2 lore raw
In reply to Simon Rozman

Hi,

Quoted 4 lines · Simon Rozman

{

src/openvpn/tun.c +0 -0
@@ -3676,14 +3692,16 @@ get_tap_reg(struct gc_arena *gc)
 
                 if (status == ERROR_SUCCESS && data_type == REG_SZ)

src/openvpn/tun.c get_tap_reg(struct gc_arena *gc) · line 3700

Quoted 10 lines
src/openvpn/tun.c +5 -3
                 {
-                    if (!strcmp(component_id, TAP_WIN_COMPONENT_ID) ||
-                        !strcmp(component_id, "root\\" TAP_WIN_COMPONENT_ID) ||
-                        !strcmp(component_id, WINTUN_COMPONENT_ID))
+                    enum windows_driver_type windows_driver = WINDOWS_DRIVER_UNSPECIFIED;
+
+                    if ((windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6, !strcmp(component_id, TAP_WIN_COMPONENT_ID))
+                        || (windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6, !strcmp(component_id, "root\\" TAP_WIN_COMPONENT_ID))
+                        || (windows_driver = WINDOWS_DRIVER_WINTUN, !strcmp(component_id, WINTUN_COMPONENT_ID)))
                     {

I could propably figure out what this if() statement with three intentional assignments in it does, but I'm fairly sure I do not *want* to spend the time, and I think nobody else should.

Lev has ACKed the patch, and it's WIN32 only - but I would very much prefer are more readable variant of this. Like

	if ( strcmp(component_id, TAP_WIN_COMPONENT_ID) == 0 ||
	     strcmp(component_id, "root\\" TAP_WIN_COMPONENT_ID) )
	{
	    windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6;
	}
	else if ( strcmp(component_id, WINTUN_COMPONENT_ID) == 0 )
	{
	    windows_driver = WINDOWS_DRIVER_WINTUN:
	}

	if ( windows_driver != WINDOWS_DRIVER_UNSPECIFIED )
	{
	    ... pre-existing code in the if() clause

not many more lines, but way easier to read than on-the-fly assignments in if(), and also easier to read than the old code.

Pretty please :)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#3 lore raw
In reply to Simon Rozman

Your patch has been applied to the master branch.

Test compiled on MingW/Ubuntu 16 (worked). Stared a bit at the code, most of it looks good and actually simplifies. The convoluted if() is something I'd like to see unwrapped - see other mail.

commit 36215dc5c3e08a935392460ab236030d6d9cde8b

Author: Simon Rozman
Date:   Thu Jan 16 15:19:00 2020 +0100
     wintun: refactor code to use enum driver type

     Signed-off-by: Simon Rozman <[email protected]...>
     Acked-by: Lev Stipakov <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg19312.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering