[Openvpn-devel,v2,14/17] Skip existing interfaces on opening the first available utun on macOS

Message ID 20200810143707.5834-15-arne@rfc2549.org
State Accepted
Headers show
Series OpenVPN refactoring | expand

Commit Message

Arne Schwabe Aug. 10, 2020, 4:37 a.m. UTC
This avoids the error messages trying to open already used utuns.

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
---
 src/openvpn/tun.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Gert Doering Aug. 10, 2020, 8:56 a.m. UTC | #1
Acked-by: Lev Stipakov <lstipakov@gmail.com>

Lev ACKed the v1 of this patch, with the request to remove the
ASSERT() wrapping - v2 fixes this instead so it actually works, so
that should take the comment into account.  I think the ASSERT()
is slightly overdoing things - this cannot fail, and having too
much "did it really not fail?" wrapping makes the code harder to
read again.  But then, I did not write the code, and it works...

Not really tested except for "test compile on current OSX and run
make check"

Your patch has been applied to the master branch.

commit 4edcf5710d043d366d8fd52a53b259399678309a
Author: Arne Schwabe
Date:   Mon Aug 10 16:37:04 2020 +0200

     Skip existing interfaces on opening the first available utun on macOS

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Lev Stipakov <lstipakov@gmail.com>
     Message-Id: <20200810143707.5834-15-arne@rfc2549.org>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg20665.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 cc7b65cf..30454454 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -3021,8 +3021,15 @@  open_darwin_utun(const char *dev, const char *dev_type, const char *dev_node, st
     /* try to open first available utun device if no specific utun is requested */
     if (utunnum == -1)
     {
-        for (utunnum = 0; utunnum<255; utunnum++)
+        for (utunnum = 0; utunnum < 255; utunnum++)
         {
+            char ifname[20];
+            /* if the interface exists silently skip it */
+            ASSERT(snprintf(ifname, sizeof(ifname), "utun%d", utunnum) > 0);
+            if (if_nametoindex(ifname))
+            {
+                continue;
+            }
             fd = utun_open_helper(ctlInfo, utunnum);
             /* Break if the fd is valid,
              * or if early initialization failed (-2) */