From 5c48bae63455253edb4607c2598e204c13ab5081 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 20 Jan 2009 14:40:00 +0000 Subject: fixed linux and bsd tunDevice (filedescriptor gets now closed in case of an exception during object construction) --- src/bsd/tunDevice.cpp | 8 ++++++-- src/linux/tunDevice.cpp | 15 ++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/bsd/tunDevice.cpp b/src/bsd/tunDevice.cpp index 421c1d5..3bbba35 100644 --- a/src/bsd/tunDevice.cpp +++ b/src/bsd/tunDevice.cpp @@ -124,13 +124,17 @@ void TunDevice::init_post() struct tuninfo ti; - if (ioctl(fd_, TUNGIFINFO, &ti) < 0) + if (ioctl(fd_, TUNGIFINFO, &ti) < 0) { + ::close(fd_); throw std::runtime_error("can't enable multicast for interface"); + } ti.flags |= IFF_MULTICAST; - if (ioctl(fd_, TUNSIFINFO, &ti) < 0) + if (ioctl(fd_, TUNSIFINFO, &ti) < 0) { + ::close(fd_); throw std::runtime_error("can't enable multicast for interface"); + } } #elif defined(__GNUC__) && defined(__FreeBSD__) diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp index 2c0618a..dba22e5 100644 --- a/src/linux/tunDevice.cpp +++ b/src/linux/tunDevice.cpp @@ -48,13 +48,6 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400) { - fd_ = ::open(DEFAULT_DEVICE, O_RDWR); - if(fd_ < 0) { - std::stringstream msg; - msg << "can't open device file (" << DEFAULT_DEVICE << "): " << LogErrno(errno); - throw std::runtime_error(msg.str()); - } - struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); @@ -72,11 +65,19 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc if(dev_name != "") strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ); + fd_ = ::open(DEFAULT_DEVICE, O_RDWR); + if(fd_ < 0) { + std::stringstream msg; + msg << "can't open device file (" << DEFAULT_DEVICE << "): " << LogErrno(errno); + throw std::runtime_error(msg.str()); + } + if(!ioctl(fd_, TUNSETIFF, &ifr)) { actual_name_ = ifr.ifr_name; } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) { actual_name_ = ifr.ifr_name; } else { + ::close(fd_); std::stringstream msg; msg << "tun/tap device ioctl failed: " << LogErrno(errno); throw std::runtime_error(msg.str()); -- cgit v1.2.3