]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
tun: create tun_name_is_fixed helper
authorAntonio Quartulli <a@unstable.cc>
Tue, 12 Jul 2022 22:16:55 +0000 (00:16 +0200)
committerGert Doering <gert@greenie.muc.de>
Thu, 14 Jul 2022 16:50:58 +0000 (18:50 +0200)
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
spots).

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>
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>
src/openvpn/buffer.h
src/openvpn/tun.c
src/openvpn/tun.h

index 231f1b0d4c12d71c208f29a7d0af2ec4b9a7420c..fece6336d433b4ee0a4356dc0083d1c33ddf09a1 100644 (file)
@@ -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))
index e12f0369dfb85bdc73d6bd820d93d016aa86e0a3..4acccbffbf26ce778bd952930ad1828d9570066c 100644 (file)
@@ -1717,6 +1717,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) */
 
+bool
+tun_name_is_fixed(const char *dev)
+{
+    return has_digit(dev);
+}
 
 #if !(defined(_WIN32) || defined(TARGET_LINUX))
 static void
@@ -1771,7 +1776,7 @@ open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
             else
 #endif
 
-            if (dynamic && !has_digit((unsigned char *)dev))
+            if (dynamic && !tun_name_is_fixed(dev))
             {
                 int i;
                 for (i = 0; i < 256; ++i)
index 5fcea5903f75ac4c6e8f69823a6d9ff1980da07e..b7786f46692a76efebe773fcf36bd53815764177 100644 (file)
@@ -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 */