aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2013-06-15 17:46:25 +0200
committerTobias Brunner <tobias@strongswan.org>2013-06-21 17:03:21 +0200
commitc8a56512a67076d5e5eb08166532e7081feec437 (patch)
tree4a4b7a5113532a144f9a1f3b10d436da6fde290c
parentdcaf8d570c5c17cf1ec82d9d19ca53e1bfdff52b (diff)
downloadstrongswan-c8a56512a67076d5e5eb08166532e7081feec437.tar.bz2
strongswan-c8a56512a67076d5e5eb08166532e7081feec437.tar.xz
tun-device: Avoid opening /dev/tunX multiple times (e.g. on FreeBSD)
-rw-r--r--src/libstrongswan/networking/tun_device.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/libstrongswan/networking/tun_device.c b/src/libstrongswan/networking/tun_device.c
index 2191198e2..00de2eddf 100644
--- a/src/libstrongswan/networking/tun_device.c
+++ b/src/libstrongswan/networking/tun_device.c
@@ -390,14 +390,18 @@ static bool init_tun(private_tun_device_t *this, const char *name_tmpl)
/* this works on FreeBSD and might also work on Linux with older TUN
* driver versions (no IFF_TUN) */
char devname[IFNAMSIZ];
- int i;
+ /* the same process is allowed to open a device again, but that's not what
+ * we want (unless we previously closed a device, which we don't know at
+ * this point). therefore, this counter is static so we don't accidentally
+ * open a device twice */
+ static int i = -1;
if (name_tmpl)
{
DBG1(DBG_LIB, "arbitrary naming of TUN devices is not supported");
}
- for (i = 0; i < 256; i++)
+ for (; ++i < 256; )
{
snprintf(devname, IFNAMSIZ, "/dev/tun%d", i);
this->tunfd = open(devname, O_RDWR);