From: Antonio Quartulli
Date: Tue, 12 Jul 2022 22:16:55 +0000 (+0200)
Subject: tun: create tun_name_is_fixed helper
X-Git-Tag: v2.6_beta1~166
X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a88d2fbe73a64f8f3861089a2b6e3b8e583e71b;p=thirdparty%2Fopenvpn.git
tun: create tun_name_is_fixed helper
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
Acked-by: Gert Doering
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
---
diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index 231f1b0d4..fece6336d 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 e12f0369d..4acccbffb 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -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)
diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h
index 5fcea5903..b7786f466 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 */