summaryrefslogtreecommitdiff
path: root/src/linux
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2009-01-20 14:40:00 +0000
committerChristian Pointner <equinox@anytun.org>2009-01-20 14:40:00 +0000
commit5c48bae63455253edb4607c2598e204c13ab5081 (patch)
treefd0364db2fde879295641b8c5636dd6bdc263c84 /src/linux
parentimproved esit status handling of exec script for unix (diff)
fixed linux and bsd tunDevice (filedescriptor gets now closed in case of an exception during object construction)
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());