summaryrefslogtreecommitdiff
path: root/src/linux
diff options
context:
space:
mode:
Diffstat (limited to 'src/linux')
-rw-r--r--src/linux/tunDevice.cpp15
1 files changed, 8 insertions, 7 deletions
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());