From 9ce26c637dc5f8af9134829c2f31627bf4bd2856 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 12 May 2008 20:43:19 +0000 Subject: added do_ifconfig for bsd device --- src/bsd/tunDevice.cpp | 38 ++++++++++++++++++++++++-------------- src/deviceConfig.hpp | 4 +++- src/linux/tunDevice.cpp | 20 +++++++++----------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/bsd/tunDevice.cpp b/src/bsd/tunDevice.cpp index e8a2849..f8b6795 100644 --- a/src/bsd/tunDevice.cpp +++ b/src/bsd/tunDevice.cpp @@ -49,7 +49,7 @@ #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) +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) { std::string device_file = "/dev/"; bool dynamic = true; @@ -164,7 +164,7 @@ void TunDevice::init_post() } #else - #error Target not supported + #error This Device works just for OpenBSD, FreeBSD or NetBSD #endif int TunDevice::fix_return(int ret, size_t type_length) @@ -246,19 +246,29 @@ const char* TunDevice::getTypeString() void TunDevice::do_ifconfig() { -// std::string command("/sbin/ifconfig "); -// command.append(actual_name_); -// command.append(" "); -// command.append(conf_.local_.toString()); -// command.append(" "); + std::ostringstream command; + command << "/sbin/ifconfig " << actual_name_ << " " << conf_.local_.toString(); -// if(conf_.type_ == TYPE_TUN) -// command.append("pointopoint "); -// else -// command.append("netmask "); + if(conf_.type_ == TYPE_TAP) + command << " netmask "; + else + command << " "; + + command << conf_.remote_netmask_.toString() << " mtu " << conf_.mtu_; -// command.append(conf_.remote_netmask_.toString()); -// command.append(" mtu 1400"); + if(conf_.type_ == TYPE_TUN) + command << " netmask 255.255.255.255 up"; + else { +#if defined(__GNUC__) && defined(__OpenBSD__) + command << " link0"; +#elif defined(__GNUC__) && defined(__FreeBSD__) + command << " up"; +#elif defined(__GNUC__) && defined(__NetBSD__) + command << ""; +#else + #error This Device works just for OpenBSD, FreeBSD or NetBSD +#endif + } -// system(command.c_str()); + system(command.str().c_str()); } diff --git a/src/deviceConfig.hpp b/src/deviceConfig.hpp index 8b534aa..ee5d50f 100644 --- a/src/deviceConfig.hpp +++ b/src/deviceConfig.hpp @@ -39,8 +39,9 @@ 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) + DeviceConfig(const char* dev_name ,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp, u_int16_t mtu) { + mtu_ = mtu; type_ = TYPE_UNDEF; if(dev_type) { if(!strncmp(dev_type, "tun", 3)) @@ -64,6 +65,7 @@ public: private: device_type_t type_; NetworkAddress local_, remote_netmask_; + u_int16_t mtu_; friend class TunDevice; }; diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp index 9f0ad52..1882a93 100644 --- a/src/linux/tunDevice.cpp +++ b/src/linux/tunDevice.cpp @@ -38,11 +38,13 @@ #include #define DEFAULT_DEVICE "/dev/net/tun" +#include + #include "tunDevice.h" #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) +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) { fd_ = ::open(DEFAULT_DEVICE, O_RDWR); if(fd_ < 0) { @@ -175,19 +177,15 @@ const char* TunDevice::getTypeString() void TunDevice::do_ifconfig() { - std::string command("/sbin/ifconfig "); - command.append(actual_name_); - command.append(" "); - command.append(conf_.local_.toString()); - command.append(" "); + std::ostringstream command; + command << "/sbin/ifconfig " << actual_name_ << " " << conf_.local_.toString(); if(conf_.type_ == TYPE_TUN) - command.append("pointopoint "); + command << " pointopoint "; else - command.append("netmask "); + command << " netmask "; - command.append(conf_.remote_netmask_.toString()); - command.append(" mtu 1400"); + command << conf_.remote_netmask_.toString() << " mtu " << conf_.mtu_; - system(command.c_str()); + system(command.str().c_str()); } -- cgit v1.2.3