[Openvpn-devel] Skip existing interfaces on opening the first available utun on macOS

Message ID 20200731100629.24611-1-arne@rfc2549.org
State Superseded
Headers show
Series [Openvpn-devel] Skip existing interfaces on opening the first available utun on macOS | expand

Commit Message

Arne Schwabe July 31, 2020, 12:06 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 | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Gert Doering Aug. 4, 2020, 9:39 a.m. UTC | #1
Hi,

On Fri, Jul 31, 2020 at 12:06:29PM +0200, Arne Schwabe wrote:
> This avoids the error messages trying to open already used utuns.
> 
> Signed-off-by: Arne Schwabe <arne@rfc2549.org>

I think the code looks good, but I have no time right now to test it
(I can build MacOS binaries but have no test environment set up
"just now").

Jonathan, do you have time?

gert
Lev Stipakov Aug. 4, 2020, 11:02 p.m. UTC | #2
Hi,

 +            ASSERT(snprintf(ifname, sizeof(ifname), "utun%d", utunnum));

Not sure about ASSERT here, because according to
https://linux.die.net/man/3/snprintf

> If an output error is encountered, a negative value is returned.

which won't trigger assert.

Otherwise looks good.

Compiled and tested on macOS.

Maybe Gert could remove ASSERT wrapping before commiting.

Acked-by: Lev Stipakov <lstipakov@gmail.com>
Arne Schwabe Aug. 18, 2020, 10:31 p.m. UTC | #3
Am 05.08.20 um 11:02 schrieb Lev Stipakov:
> Hi,
> 
>  +            ASSERT(snprintf(ifname, sizeof(ifname), "utun%d", utunnum));
> 
> Not sure about ASSERT here, because according to
> https://linux.die.net/man/3/snprintf
> 
>> If an output error is encountered, a negative value is returned.
> 
> which won't trigger assert.
> 
> Otherwise looks good.
> 
> Compiled and tested on macOS.
> 
> Maybe Gert could remove ASSERT wrapping before commiting.
> 
> Acked-by: Lev Stipakov <lstipakov@gmail.com>
> 

You are too late, there is already a v2 that fixed that problem and is
merged :)

Arne

Patch

diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index cc7b65cf..b9e444b6 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -3023,6 +3023,13 @@  open_darwin_utun(const char *dev, const char *dev_type, const char *dev_node, st
     {
         for (utunnum = 0; utunnum<255; utunnum++)
         {
+            char ifname[20];
+            /* if the interface exists silently skip it */
+            ASSERT(snprintf(ifname, sizeof(ifname), "utun%d", utunnum));
+            if (if_nametoindex(ifname))
+            {
+                continue;
+            }
             fd = utun_open_helper(ctlInfo, utunnum);
             /* Break if the fd is valid,
              * or if early initialization failed (-2) */