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/linux/tunDevice.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/linux/tunDevice.cpp') 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