[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
Related show

Commit Message

Arne Schwabe July 31, 2020, 10:06 a.m.
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, 7:39 p.m. | #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. 5, 2020, 9:02 a.m. | #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. 19, 2020, 8:31 a.m. | #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) */