[Openvpn-devel,pre-07/25] tun: create tun_name_is_fixed helper

Message ID 20220712221655.19333-1-a@unstable.cc
State Accepted
Headers show
Series [Openvpn-devel,pre-07/25] tun: create tun_name_is_fixed helper | expand

Commit Message

Antonio Quartulli July 12, 2022, 12:16 p.m. UTC
This helper encloses the (simple) logic used by OpenVPN to determine if
the name passed to --dev has to be considered a fixed interface name or
just a pattern.

Having a helper is useful because when this logic is required elsewhere,
we can just re-use this logic without duplicating the code (which may
mean introducing bugs if a future logic change should not update all

The logic is actually fairly simple: check if the name contains a number
(i.e. tun0). If so, consider the name a fixed device name.

While at it make has_digit() accept a signed argument because strings
are normally signed (also isdigit() accepts a signed argument).

Signed-off-by: Antonio Quartulli <a@unstable.cc>
 src/openvpn/buffer.h | 4 ++--
 src/openvpn/tun.c    | 7 ++++++-
 src/openvpn/tun.h    | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)


Gert Doering July 14, 2022, 8:20 a.m. UTC | #1
Acked-by: Gert Doering <gert@greenie.muc.de>

Trivial enough, and confirmed that it does not break anything.

(Having the "has_digit()" in buffer.*h* when it is only ever called
from tun.c smells like "we might eventually include this in 
tun_name_is_fixed() and get rid of the inline helper" - but this is
a patch for another day.  Also, we might do it the other way round,
"everything that is not exactly 'tun', 'tap' or 'null' is considered 
fixed" - this whole open_tun() thing is a hairy mess of historic insanity).

Your patch has been applied to the master branch.

commit 4a88d2fbe73a64f8f3861089a2b6e3b8e583e71b
Author: Antonio Quartulli
Date:   Wed Jul 13 00:16:55 2022 +0200

     tun: create tun_name_is_fixed helper

     Signed-off-by: Antonio Quartulli <a@unstable.cc>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Message-Id: <20220712221655.19333-1-a@unstable.cc>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg24676.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>

kind regards,

Gert Doering


diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index 231f1b0d..fece6336 100644
--- a/src/openvpn/buffer.h
+++ b/src/openvpn/buffer.h
@@ -356,9 +356,9 @@  strncpynt(char *dest, const char *src, size_t maxlen)
 /* return true if string contains at least one numerical digit */
 static inline bool
-has_digit(const unsigned char *src)
+has_digit(const char *src)
-    unsigned char c;
+    char c;
     while ((c = *src++))
         if (isdigit(c))
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index f17db280..108090d0 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -1718,6 +1718,11 @@  read_tun_header(struct tuntap *tt, uint8_t *buf, int len)
 #endif /* if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H) */
+tun_name_is_fixed(const char *dev)
+    return has_digit(dev);
 #if !defined(_WIN32)
 static void
@@ -1772,7 +1777,7 @@  open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
-            if (dynamic && !has_digit((unsigned char *)dev))
+            if (dynamic && !tun_name_is_fixed(dev))
                 int i;
                 for (i = 0; i < 256; ++i)
diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h
index cf02bf43..8ec8f51f 100644
--- a/src/openvpn/tun.h
+++ b/src/openvpn/tun.h
@@ -694,5 +694,6 @@  tun_set(struct tuntap *tt,
 const char *tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);
+bool tun_name_is_fixed(const char *dev);
 #endif /* TUN_H */