diff options
-rw-r--r-- | src/bsd/tunDevice.cpp | 8 | ||||
-rw-r--r-- | 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()); |