From f5afa7efbc1ff915e733e8d1474777de80534345 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 26 Nov 2008 10:47:14 +0000 Subject: cleaned tunDevice code --- src/Makefile | 2 +- src/anytun.cpp | 5 +--- src/bsd/tunDevice.cpp | 48 ++++++++---------------------- src/bsd/tunDevice.h | 66 ----------------------------------------- src/configure | 4 --- src/deviceConfig.hpp | 22 +++++++------- src/linux/tunDevice.cpp | 31 ++++--------------- src/linux/tunDevice.h | 65 ---------------------------------------- src/tunDevice.h | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 110 insertions(+), 212 deletions(-) delete mode 100644 src/bsd/tunDevice.h delete mode 100644 src/linux/tunDevice.h create mode 100644 src/tunDevice.h diff --git a/src/Makefile b/src/Makefile index de08ea2..da7b839 100644 --- a/src/Makefile +++ b/src/Makefile @@ -274,7 +274,7 @@ anyrtpproxy: anytun distclean: cleanall find . -name *.o -exec rm -f {} \; rm -f config.sub config.guess - rm -f tunDevice.cpp tunDevice.h + rm -f tunDevice.cpp cleanall: clean $(MAKE) --directory=$(CURDIR)/man clean diff --git a/src/anytun.cpp b/src/anytun.cpp index 258d98a..3e1c4fc 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -509,10 +509,7 @@ int main(int argc, char* argv[]) } } - TunDevice dev(gOpt.getDevName() =="" ? NULL : gOpt.getDevName().c_str(), - gOpt.getDevType() =="" ? NULL : gOpt.getDevType().c_str(), - gOpt.getIfconfigParamLocal() =="" ? NULL : gOpt.getIfconfigParamLocal().c_str(), - gOpt.getIfconfigParamRemoteNetmask() =="" ? NULL : gOpt.getIfconfigParamRemoteNetmask().c_str()); + TunDevice dev(gOpt.getDevName(), gOpt.getDevType(), gOpt.getIfconfigParamLocal(), gOpt.getIfconfigParamRemoteNetmask()); cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)"; cLog.msg(Log::PRIO_NOTICE) << "dev opened - actual name is '" << dev.getActualName() << "'"; cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'"; diff --git a/src/bsd/tunDevice.cpp b/src/bsd/tunDevice.cpp index 0ef1974..31e986c 100644 --- a/src/bsd/tunDevice.cpp +++ b/src/bsd/tunDevice.cpp @@ -50,11 +50,11 @@ #include -TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400) +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) { std::string device_file = "/dev/"; bool dynamic = true; - if(dev_name) { + if(dev_name != "") { device_file.append(dev_name); dynamic = false; } @@ -110,7 +110,7 @@ TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifc init_post(); - if(ifcfg_lp && ifcfg_rnmp) + if(ifcfg_lp != "" && ifcfg_rnmp != "") do_ifconfig(); } @@ -124,9 +124,9 @@ TunDevice::~TunDevice() void TunDevice::init_post() { - with_type_ = true; + with_pi_ = true; if(conf_.type_ == TYPE_TAP) - with_type_ = false; + with_pi_ = false; struct tuninfo ti; @@ -143,9 +143,9 @@ void TunDevice::init_post() void TunDevice::init_post() { - with_type_ = true; + with_pi_ = true; if(conf_.type_ == TYPE_TAP) - with_type_ = false; + with_pi_ = false; int arg = 0; ioctl(fd_, TUNSLMODE, &arg); @@ -157,7 +157,7 @@ void TunDevice::init_post() void TunDevice::init_post() { - with_type_ = false; + with_pi_ = false; int arg = IFF_POINTOPOINT|IFF_MULTICAST; ioctl(fd_, TUNSIFMODE, &arg); @@ -169,7 +169,7 @@ void TunDevice::init_post() #error This Device works just for OpenBSD, FreeBSD or NetBSD #endif -int TunDevice::fix_return(int ret, size_t type_length) +int TunDevice::fix_return(int ret, size_t pi_length) { if(ret < 0) return ret; @@ -177,12 +177,12 @@ int TunDevice::fix_return(int ret, size_t type_length) return (static_cast(ret) > type_length ? (ret - type_length) : 0); } -short TunDevice::read(u_int8_t* buf, u_int32_t len) +int TunDevice::read(u_int8_t* buf, u_int32_t len) { if(fd_ < 0) return -1; - if(with_type_) { + if(with_pi_) { struct iovec iov[2]; u_int32_t type; @@ -201,7 +201,7 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len) if(fd_ < 0) return -1; - if(with_type_) { + if(with_pi_) { struct iovec iov[2]; u_int32_t type; struct ip *hdr = reinterpret_cast(buf); @@ -222,30 +222,6 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len) return(::write(fd_, buf, len)); } -const char* TunDevice::getActualName() -{ - return actual_name_.c_str(); -} - -device_type_t TunDevice::getType() -{ - return conf_.type_; -} - -const char* TunDevice::getTypeString() -{ - if(fd_ < 0) - return NULL; - - switch(conf_.type_) - { - case TYPE_UNDEF: return "undef"; break; - case TYPE_TUN: return "tun"; break; - case TYPE_TAP: return "tap"; break; - } - return NULL; -} - void TunDevice::do_ifconfig() { std::ostringstream command; diff --git a/src/bsd/tunDevice.h b/src/bsd/tunDevice.h deleted file mode 100644 index 1d1c08e..0000000 --- a/src/bsd/tunDevice.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, - * Christian Pointner - * - * This file is part of Anytun. - * - * Anytun is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 3 as - * published by the Free Software Foundation. - * - * Anytun is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with anytun. If not, see . - */ - -#ifndef _TUNDEVICE_H_ -#define _TUNDEVICE_H_ - -#include "buffer.h" -#include "deviceConfig.hpp" -#include "threadUtils.hpp" - -class TunDevice -{ -public: - TunDevice(const char* dev,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp); - ~TunDevice(); - - short read(u_int8_t* buf, u_int32_t len); - int write(u_int8_t* buf, u_int32_t len); - - const char* getActualName(); - device_type_t getType(); - const char* getTypeString(); - -private: - void operator=(const TunDevice &src); - TunDevice(const TunDevice &src); - - void init_post(); - void do_ifconfig(); - int fix_return(int ret, size_t type_length); - - int fd_; - DeviceConfig conf_; - bool with_type_; - std::string actual_name_; -}; - -#endif diff --git a/src/configure b/src/configure index ccdad1b..4cf9ec6 100755 --- a/src/configure +++ b/src/configure @@ -35,15 +35,11 @@ TARGET=`uname -s` case $TARGET in Linux) rm -rf tunDevice.cpp - rm -rf tunDevice.h ln -sf linux/tunDevice.cpp - ln -sf linux/tunDevice.h ;; OpenBSD|FreeBSD|NetBSD) rm -rf tunDevice.cpp - rm -rf tunDevice.h ln -sf bsd/tunDevice.cpp - ln -sf bsd/tunDevice.h ;; *) echo "Plattform not supported" diff --git a/src/deviceConfig.hpp b/src/deviceConfig.hpp index f3b6f5e..44978d0 100644 --- a/src/deviceConfig.hpp +++ b/src/deviceConfig.hpp @@ -40,27 +40,27 @@ enum device_type_t { TYPE_UNDEF, TYPE_TUN, TYPE_TAP }; class DeviceConfig { public: - DeviceConfig(const char* dev_name ,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp, u_int16_t mtu) + DeviceConfig(std::string dev_name ,std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp, u_int16_t mtu) { mtu_ = mtu; type_ = TYPE_UNDEF; - if(dev_type) { - if(!strncmp(dev_type, "tun", 3)) + if(dev_type != "") { + if(!dev_type.compare(0,3,"tun")) type_ = TYPE_TUN; - else if(!strncmp(dev_type, "tap", 3)) + else if (!dev_type.compare(0,3,"tap")) type_ = TYPE_TAP; } - else if(dev_name) { - if(!strncmp(dev_name, "tun", 3)) + else if(dev_name != "") { + if(!dev_name.compare(0,3,"tun")) type_ = TYPE_TUN; - else if(!strncmp(dev_name, "tap", 3)) + else if(!dev_name.compare(0,3,"tap")) type_ = TYPE_TAP; } - if(ifcfg_lp) - local_.setNetworkAddress(ipv4, ifcfg_lp); - if(ifcfg_rnmp) - remote_netmask_.setNetworkAddress(ipv4, ifcfg_rnmp); + if(ifcfg_lp != "") + local_.setNetworkAddress(ipv4, ifcfg_lp.c_str()); + if(ifcfg_rnmp != "") + remote_netmask_.setNetworkAddress(ipv4, ifcfg_rnmp.c_str()); } private: diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp index 4a61432..c743229 100644 --- a/src/linux/tunDevice.cpp +++ b/src/linux/tunDevice.cpp @@ -46,7 +46,7 @@ #include "threadUtils.hpp" -TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400) +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) { @@ -74,8 +74,8 @@ TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifc else throw std::runtime_error("unable to recognize type of device (tun or tap)"); - if(dev_name) - strncpy(ifr.ifr_name, dev_name, IFNAMSIZ); + if(dev_name != "") + strncpy(ifr.ifr_name, dev_name.c_str(), IFNAMSIZ); if(!ioctl(fd_, TUNSETIFF, &ifr)) { actual_name_ = ifr.ifr_name; @@ -90,7 +90,7 @@ TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifc throw std::runtime_error(msg); } - if(ifcfg_lp && ifcfg_rnmp) + if(ifcfg_lp != "" && ifcfg_rnmp != "") do_ifconfig(); } @@ -155,28 +155,9 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len) return(::write(fd_, buf, len)); } -const char* TunDevice::getActualName() +void TunDevice::init_post() { - return actual_name_.c_str(); -} - -device_type_t TunDevice::getType() -{ - return conf_.type_; -} - -const char* TunDevice::getTypeString() -{ - if(fd_ < 0) - return NULL; - - switch(conf_.type_) - { - case TYPE_UNDEF: return "undef"; break; - case TYPE_TUN: return "tun"; break; - case TYPE_TAP: return "tap"; break; - } - return NULL; +// nothing to be done here } void TunDevice::do_ifconfig() diff --git a/src/linux/tunDevice.h b/src/linux/tunDevice.h deleted file mode 100644 index 05457b0..0000000 --- a/src/linux/tunDevice.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, - * Christian Pointner - * - * This file is part of Anytun. - * - * Anytun is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 3 as - * published by the Free Software Foundation. - * - * Anytun is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with anytun. If not, see . - */ - -#ifndef _TUNDEVICE_H_ -#define _TUNDEVICE_H_ - -#include "buffer.h" -#include "deviceConfig.hpp" -#include "threadUtils.hpp" - -class TunDevice -{ -public: - TunDevice(const char* dev,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp); - ~TunDevice(); - - int read(u_int8_t* buf, u_int32_t len); - int write(u_int8_t* buf, u_int32_t len); - - const char* getActualName(); - device_type_t getType(); - const char* getTypeString(); - -private: - void operator=(const TunDevice &src); - TunDevice(const TunDevice &src); - - void do_ifconfig(); - int fix_return(int ret, size_t pi_length); - - int fd_; - DeviceConfig conf_; - bool with_pi_; - std::string actual_name_; -}; - -#endif diff --git a/src/tunDevice.h b/src/tunDevice.h new file mode 100644 index 0000000..3768cf9 --- /dev/null +++ b/src/tunDevice.h @@ -0,0 +1,79 @@ +/* + * anytun + * + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methodes used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, + * Christian Pointner + * + * This file is part of Anytun. + * + * Anytun is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * Anytun is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with anytun. If not, see . + */ + +#ifndef _TUNDEVICE_H_ +#define _TUNDEVICE_H_ + +#include "buffer.h" +#include "deviceConfig.hpp" +#include "threadUtils.hpp" + +class TunDevice +{ +public: + TunDevice(std::string dev,std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp); + ~TunDevice(); + + int read(u_int8_t* buf, u_int32_t len); + int write(u_int8_t* buf, u_int32_t len); + + std::string getActualName() { return actual_name_.c_str(); } + device_type_t getType() { return conf_.type_; } + std::string getTypeString() + { + if(fd_ < 0) + return NULL; + + switch(conf_.type_) + { + case TYPE_UNDEF: return "undef"; break; + case TYPE_TUN: return "tun"; break; + case TYPE_TAP: return "tap"; break; + } + return NULL; + } + + +private: + void operator=(const TunDevice &src); + TunDevice(const TunDevice &src); + + void init_post(); + void do_ifconfig(); + int fix_return(int ret, size_t pi_length); + + int fd_; + DeviceConfig conf_; + bool with_pi_; + std::string actual_name_; +}; + +#endif -- cgit v1.2.3