From 6dc112cfc5cd4f6fa0615d89679e67d45c5551c4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 17 Jun 2007 14:53:17 +0000 Subject: bugfixes @ tunDevice --- anytun.cpp | 42 +++++++++++++++++------------------ tunDevice.cpp | 71 ++++++++++++++++++++++++++--------------------------------- tunDevice.h | 3 +-- 3 files changed, 53 insertions(+), 63 deletions(-) diff --git a/anytun.cpp b/anytun.cpp index 7ac1dd5..56c23d5 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -56,32 +56,32 @@ int main(int argc, char* argv[]) TunDevice* dev; dev = new TunDevice("tun", "192.168.200.1", "192.168.201.1"); - dev->open(); - std::cout << "dev has actual name: " << dev->getActualName() << std::endl; - + std::cout << "dev created (opened)" << std::endl; + std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl; + std::cout << "dev type is '" << dev->getType() << "'" << std::endl; sleep(10); - - dev->close(); - + delete dev; + std::cout << "dev destroyed" << std::endl; + sleep(10); + dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); + std::cout << "dev created (opened)" << std::endl; + std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl; + std::cout << "dev type is '" << dev->getType() << "'" << std::endl; + sleep(10); delete dev; + std::cout << "dev destroyed" << std::endl; -// dev = new TunDevice("tap", "192.168.202.1", "255.255.255.0"); -// dev->open(); -// std::cout << "dev has actual name: " << dev->getActualName() << std::endl; - -// sleep(10); - -// delete dev; - -// dev = new TunDevice("tun12", "192.168.200.1", "192.168.201.1"); -// dev->open(); -// std::cout << "dev has actual name: " << dev->getActualName() << std::endl; - -// sleep(10); + sleep(10); -// delete dev; - + dev = new TunDevice("tun17", "192.168.200.1", "192.168.201.1"); + std::cout << "dev created (opened)" << std::endl; + std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl; + std::cout << "dev type is '" << dev->getType() << "'" << std::endl; + sleep(10); + delete dev; + std::cout << "dev destroyed" << std::endl; + return 0; } diff --git a/tunDevice.cpp b/tunDevice.cpp index f81a987..04fcb9a 100644 --- a/tunDevice.cpp +++ b/tunDevice.cpp @@ -41,12 +41,7 @@ extern "C" { TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifcfg_rnmp) { - is_open_ = false; dev_ = NULL; - if(!dev_name) - dev_name_ = NULL; - else - strcpy(dev_name_,dev_name); // init_tun (const char *dev, /* --dev option */ // const char *dev_type, /* --dev-type option */ @@ -61,6 +56,12 @@ TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifc // const struct frame *frame, // const struct tuntap_options *options) +// open_tun (const char *dev, +// const char *dev_type, +// const char *dev_node, +// bool ipv6, +// struct tuntap *tt) + // ------------------------------------------- // c->c1.tuntap = init_tun (c->options.dev, @@ -75,6 +76,14 @@ TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifc // init_tun_post (c->c1.tuntap, // &c->c2.frame, // &c->options.tuntap_options); + +// open_tun (c->options.dev, +// c->options.dev_type, +// c->options.dev_node, +// c->options.tun_ipv6, +// c->c1.tuntap); + + in_addr_t lp, rp; @@ -84,48 +93,16 @@ TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifc dev_ = init_tun(dev_name, NULL, ifcfg_lp, ifcfg_rnmp, lp, rp, 0, NULL); //init_tun_post(dev_, NULL, NULL); if(!dev_) - throw std::runtime_error("can't init tun"); -} + throw std::runtime_error("can't init tun/tap device"); -TunDevice::~TunDevice() -{ - if(dev_ && is_open_) - close_tun(dev_); -} - -void TunDevice::open() -{ - if(!dev_) - return; -// open_tun (const char *dev, -// const char *dev_type, -// const char *dev_node, -// bool ipv6, -// struct tuntap *tt) - -// ------------------------ - -// open_tun (c->options.dev, -// c->options.dev_type, -// c->options.dev_node, -// c->options.tun_ipv6, -// c->c1.tuntap); - - open_tun (dev_name_, NULL, NULL, false, dev_); + open_tun (dev_name, NULL, NULL, false, dev_); do_ifconfig(dev_, dev_->actual_name, 1500, NULL); - is_open_ = true; } -void TunDevice::close() +TunDevice::~TunDevice() { if(dev_) close_tun(dev_); - is_open_ = false; -} - -bool TunDevice::isOpen() -{ - return is_open_; } int TunDevice::read(Buffer& buf) @@ -151,3 +128,17 @@ char* TunDevice::getActualName() return dev_->actual_name; } + +char* TunDevice::getType() +{ + if(!dev_) + return NULL; + + switch(dev_->type) + { + case DEV_TYPE_UNDEF: return "undef"; break; + case DEV_TYPE_TUN: return "tun"; break; + case DEV_TYPE_TAP: return "tap"; break; + } + return NULL; +} diff --git a/tunDevice.h b/tunDevice.h index b18d233..245f7e8 100644 --- a/tunDevice.h +++ b/tunDevice.h @@ -47,14 +47,13 @@ public: int write(Buffer& buf); char* getActualName(); + char* getType(); private: void operator=(const TunDevice &src); TunDevice(const TunDevice &src); struct tuntap *dev_; - char *dev_name_; - bool is_open_; }; #endif -- cgit v1.2.3