From ff3fa71574881d7e6e29937af52b62c24c867294 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 17 Jun 2007 02:55:19 +0000 Subject: tunDevice can now be open and closed tunDevice write seems to work now tunDevice read seems not to work yet --- anytun.cpp | 31 +++++++++++++++ datatypes.h | 5 +++ tunDevice.cpp | 120 +++++++++++++++++++++++++++++++++++++++++----------------- tunDevice.h | 16 ++++++-- 4 files changed, 135 insertions(+), 37 deletions(-) diff --git a/anytun.cpp b/anytun.cpp index 9aedadb..7ac1dd5 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -32,6 +32,7 @@ #include "datatypes.h" +#include "tunDevice.h" #include "buffer.h" int main(int argc, char* argv[]) @@ -52,5 +53,35 @@ int main(int argc, char* argv[]) for(unsigned int i=0;iopen(); + std::cout << "dev has actual name: " << dev->getActualName() << std::endl; + + sleep(10); + + dev->close(); + + sleep(10); + + delete dev; + +// 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); + +// delete dev; + return 0; } diff --git a/datatypes.h b/datatypes.h index 62fda83..74c5713 100644 --- a/datatypes.h +++ b/datatypes.h @@ -43,6 +43,11 @@ typedef unsigned int u_int32_t; typedef signed long long int64_t; typedef unsigned long long u_int64_t; +typedef u_int32_t seq_nr_t; +typedef u_int16_t sender_id_t; +typedef u_int8_t padding_t; +typedef u_int8_t pad_cnt_t; +typedef u_int16_t payload_type_t; typedef u_int32_t auth_tag_t; #endif diff --git a/tunDevice.cpp b/tunDevice.cpp index b42e6ce..f81a987 100644 --- a/tunDevice.cpp +++ b/tunDevice.cpp @@ -27,6 +27,9 @@ * distribution); if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include + extern "C" { #include "openvpn/config.h" #include "openvpn/syshead.h" @@ -36,66 +39,115 @@ extern "C" { #include "tunDevice.h" -TunDevice::TunDevice(const char* dev) +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 */ -// const char *ifconfig_local_parm, /* --ifconfig parm 1 */ -// const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */ -// in_addr_t local_public, -// in_addr_t remote_public, -// const bool strict_warn, -// struct env_set *es) +// const char *dev_type, /* --dev-type option */ +// const char *ifconfig_local_parm, /* --ifconfig parm 1 */ +// const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */ +// in_addr_t local_public, +// in_addr_t remote_public, +// const bool strict_warn, +// struct env_set *es) // init_tun_post (struct tuntap *tt, -// const struct frame *frame, -// const struct tuntap_options *options) - - -// open_tun (const char *dev, const char *dev_type, const char *dev_node, false, dev_) - -//------------------------ -// c->c1.tuntap = init_tun (c->options.dev, -// c->options.dev_type, -// c->options.ifconfig_local, -// c->options.ifconfig_remote_netmask, -// addr_host (&c->c1.link_socket_addr.local), -// addr_host (&c->c1.link_socket_addr.remote), -// !c->options.ifconfig_nowarn, -// c->c2.es); - -// init_tun_post (c->c1.tuntap, -// &c->c2.frame, -// &c->options.tuntap_options); - -// dev_ = init_tun(dev, +// const struct frame *frame, +// const struct tuntap_options *options) + +// ------------------------------------------- + +// c->c1.tuntap = init_tun (c->options.dev, +// c->options.dev_type, +// c->options.ifconfig_local, +// c->options.ifconfig_remote_netmask, +// addr_host (&c->c1.link_socket_addr.local), +// addr_host (&c->c1.link_socket_addr.remote), +// !c->options.ifconfig_nowarn, +// c->c2.es); +// init_tun_post (c->c1.tuntap, +// &c->c2.frame, +// &c->options.tuntap_options); + in_addr_t lp, rp; + +// lp = inet_addr("192.168.198.1"); +// rp = inet_addr("192.168.199.1"); + 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"); } 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_); + do_ifconfig(dev_, dev_->actual_name, 1500, NULL); + is_open_ = true; +} + +void TunDevice::close() { if(dev_) close_tun(dev_); + is_open_ = false; +} +bool TunDevice::isOpen() +{ + return is_open_; } -int TunDevice::read(uint8_t *buf, int len) +int TunDevice::read(Buffer& buf) { if(!dev_) return -1; - return read_tun(dev_, buf, len); + return read_tun(dev_, buf, buf.getLength()); } -int TunDevice::write(uint8_t *buf, int len) +int TunDevice::write(Buffer& buf) { if(!dev_) return -1; - return write_tun(dev_, buf, len); + return write_tun(dev_, buf, buf.getLength()); +} + +char* TunDevice::getActualName() +{ + if(!dev_) + return NULL; + + return dev_->actual_name; } diff --git a/tunDevice.h b/tunDevice.h index 487167a..b18d233 100644 --- a/tunDevice.h +++ b/tunDevice.h @@ -31,20 +31,30 @@ #ifndef _TUNDEVICE_H_ #define _TUNDEVICE_H_ +#include "buffer.h" + class TunDevice { public: - TunDevice(const char* dev); + TunDevice(const char* dev, const char* ifcfg_lp, const char* ifcfg_rnmp); ~TunDevice(); - int read(uint8_t *buf, int len); - int write(uint8_t *buf, int len); + void open(); + void close(); + bool isOpen(); + + int read(Buffer& buf); + int write(Buffer& buf); + + char* getActualName(); private: void operator=(const TunDevice &src); TunDevice(const TunDevice &src); struct tuntap *dev_; + char *dev_name_; + bool is_open_; }; #endif -- cgit v1.2.3