summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2009-01-31 01:47:38 +0000
committerChristian Pointner <equinox@anytun.org>2009-01-31 01:47:38 +0000
commit8fd23d4249be748356e591e0dd70ea82f70dcec5 (patch)
treeae37b1a560279a538737efe68e030630b134c76a
parentrefactoring (diff)
moved to new handling if ifconfig parameters (more windows friendly)
-rw-r--r--src/anytun.cpp10
-rw-r--r--src/deviceConfig.hpp21
-rw-r--r--src/linux/tunDevice.cpp15
-rw-r--r--src/networkAddress.cpp18
-rw-r--r--src/networkAddress.h3
-rw-r--r--src/options.cpp31
-rw-r--r--src/options.h9
-rw-r--r--src/tunDevice.h2
8 files changed, 57 insertions, 52 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp
index a5de1ff..fb9c429 100644
--- a/src/anytun.cpp
+++ b/src/anytun.cpp
@@ -93,10 +93,11 @@ void createConnection(const PacketSourceEndpoint & remote_end, window_size_t seq
gSyncQueue.push(sc);
#endif
#ifndef NO_ROUTING
- if (gOpt.getIfconfigParamRemoteNetmask() != "")
+ OptionNetwork net = gOpt.getIfconfigParam();
+ if (net.net_addr != "")
{
- NetworkAddress addr(gOpt.getIfconfigParamRemoteNetmask());
- NetworkPrefix prefix(addr,128);
+ NetworkAddress addr(net.net_addr);
+ NetworkPrefix prefix(addr,net.prefix_length);
gRoutingTable.addRoute(prefix,mux);
#ifndef ANYTUN_NOSYNC
SyncCommand sc2 (prefix);
@@ -365,7 +366,8 @@ int main(int argc, char* argv[])
#endif
#endif
- TunDevice dev(gOpt.getDevName(), gOpt.getDevType(), gOpt.getIfconfigParamLocal(), gOpt.getIfconfigParamRemoteNetmask());
+ OptionNetwork net = gOpt.getIfconfigParam();
+ TunDevice dev(gOpt.getDevName(), gOpt.getDevType(), net.net_addr, net.prefix_length);
cLog.msg(Log::PRIO_NOTICE) << "dev opened - name '" << dev.getActualName() << "', node '" << dev.getActualNode() << "'";
cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'";
#ifndef NO_EXEC
diff --git a/src/deviceConfig.hpp b/src/deviceConfig.hpp
index acca46e..3e6c912 100644
--- a/src/deviceConfig.hpp
+++ b/src/deviceConfig.hpp
@@ -33,6 +33,8 @@
#define _DEVICE_CONFIG_HPP_
#include "networkAddress.h"
+#include <boost/asio.hpp>
+
class TunDevice;
enum device_type_t { TYPE_UNDEF, TYPE_TUN, TYPE_TAP };
@@ -40,7 +42,7 @@ enum device_type_t { TYPE_UNDEF, TYPE_TUN, TYPE_TAP };
class DeviceConfig
{
public:
- DeviceConfig(std::string dev_name ,std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp, u_int16_t mtu)
+ DeviceConfig(std::string dev_name ,std::string dev_type, std::string ifcfg_addr, u_int16_t ifcfg_prefix, u_int16_t mtu)
{
mtu_ = mtu;
type_ = TYPE_UNDEF;
@@ -70,15 +72,22 @@ public:
throw std::runtime_error("Device type tun requires ifconfig parameters (--ifconfig)");
#endif
- if(ifcfg_lp != "")
- local_.setNetworkAddress(ipv4, ifcfg_lp.c_str());
- if(ifcfg_rnmp != "")
- remote_netmask_.setNetworkAddress(ipv4, ifcfg_rnmp.c_str());
+ if(ifcfg_addr != "")
+ addr_.setNetworkAddress(ipv4, ifcfg_addr.c_str());
+ prefix_ = ifcfg_prefix;
+ u_int32_t mask = 0;
+ for(u_int16_t i = 0; i < prefix_; ++i) {
+ mask = mask >> 1;
+ mask |= 0x80000000L;
+ }
+ netmask_.setNetworkAddress(boost::asio::ip::address_v4(mask));
}
private:
device_type_t type_;
- NetworkAddress local_, remote_netmask_;
+ NetworkAddress addr_;
+ NetworkAddress netmask_;
+ u_int16_t prefix_;
u_int16_t mtu_;
friend class TunDevice;
diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp
index 403cd99..8fde8ae 100644
--- a/src/linux/tunDevice.cpp
+++ b/src/linux/tunDevice.cpp
@@ -46,7 +46,7 @@
#include "threadUtils.hpp"
#include "log.h"
-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)
+TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, u_int16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
@@ -84,7 +84,7 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
}
actual_node_ = DEFAULT_DEVICE;
- if(ifcfg_lp != "" && ifcfg_rnmp != "")
+ if(ifcfg_addr != "")
do_ifconfig();
}
@@ -157,14 +157,8 @@ void TunDevice::init_post()
void TunDevice::do_ifconfig()
{
std::ostringstream command;
- command << "/sbin/ifconfig " << actual_name_ << " " << conf_.local_.toString();
-
- if(conf_.type_ == TYPE_TUN)
- command << " pointopoint ";
- else
- command << " netmask ";
-
- command << conf_.remote_netmask_.toString() << " mtu " << conf_.mtu_;
+ command << "/sbin/ifconfig " << actual_name_ << " " << conf_.addr_.toString()
+ << " netmask " << conf_.netmask_.toString() << " mtu " << conf_.mtu_;
int result = system(command.str().c_str());
if(result == -1)
@@ -177,5 +171,4 @@ void TunDevice::do_ifconfig()
else
cLog.msg(Log::PRIO_ERR) << "Execution of ifconfig: unkown error";
}
-
}
diff --git a/src/networkAddress.cpp b/src/networkAddress.cpp
index 059b670..6863fab 100644
--- a/src/networkAddress.cpp
+++ b/src/networkAddress.cpp
@@ -100,6 +100,24 @@ void NetworkAddress::setNetworkAddress(const network_address_type_t type, const
network_address_type_ = type;
}
+void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v4 addr)
+{
+ network_address_type_=ipv4;
+ ipv4_address_ = addr;
+}
+
+void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v6 addr)
+{
+ network_address_type_=ipv6;
+ ipv6_address_ = addr;
+}
+
+void NetworkAddress::setNetworkAddress(u_int64_t addr)
+{
+ network_address_type_=ethernet;
+ ethernet_address_=addr;
+}
+
network_address_type_t NetworkAddress::getNetworkAddressType() const
{
return network_address_type_;
diff --git a/src/networkAddress.h b/src/networkAddress.h
index 7510eb1..d683b4c 100644
--- a/src/networkAddress.h
+++ b/src/networkAddress.h
@@ -64,6 +64,9 @@ public:
NetworkAddress(const network_address_type_t type, const std::string & address );
~NetworkAddress();
void setNetworkAddress(const network_address_type_t type, const std::string & address );
+ void setNetworkAddress(boost::asio::ip::address_v4);
+ void setNetworkAddress(boost::asio::ip::address_v6);
+ void setNetworkAddress(u_int64_t);
network_address_type_t getNetworkAddressType() const;
std::string toString() const;
bool operator<(const NetworkAddress &s) const;
diff --git a/src/options.cpp b/src/options.cpp
index b39790b..8d4240b 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -167,8 +167,6 @@ Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
dev_name_ = "";
dev_type_ = "";
- ifconfig_param_local_ = "";
- ifconfig_param_remote_netmask_ = "";
post_up_script_ = "";
sender_id_ = 0;
@@ -357,7 +355,7 @@ bool Options::parse(int argc, char* argv[])
PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
PARSE_SCALAR_PARAM("-t","--type", dev_type_)
- PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
+ PARSE_SCALAR_PARAM("-n","--ifconfig", ifconfig_param_)
#ifndef NO_EXEC
PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_)
#endif
@@ -463,8 +461,7 @@ void Options::printUsage()
std::cout << " [-d|--dev] <name> device name" << std::endl;
std::cout << " [-t|--type] <tun|tap> device type" << std::endl;
- std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl;
- std::cout << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl;
+ std::cout << " [-n|--ifconfig] <local>/<prefix> the local address for the tun/tap device and the used prefix length" << std::endl;
#ifndef NO_EXEC
std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl;
#endif
@@ -529,8 +526,7 @@ void Options::printOptions()
std::cout << std::endl;
std::cout << "dev_name = '" << dev_name_ << "'" << std::endl;
std::cout << "dev_type = '" << dev_type_ << "'" << std::endl;
- std::cout << "ifconfig_param_local = '" << ifconfig_param_local_ << "'" << std::endl;
- std::cout << "ifconfig_param_remote_netmask = '" << ifconfig_param_remote_netmask_ << "'" << std::endl;
+ std::cout << "ifconfig_param_local = '" << ifconfig_param_.net_addr << "/" << ifconfig_param_.prefix_length << "'" << std::endl;
std::cout << "post_up_script = '" << post_up_script_ << "'" << std::endl;
std::cout << "routes:" << std::endl;
NetworkList::const_iterator rit;
@@ -785,29 +781,16 @@ Options& Options::setDevType(std::string d)
return *this;
}
-std::string Options::getIfconfigParamLocal()
+OptionNetwork Options::getIfconfigParam()
{
ReadersLock lock(mutex);
- return ifconfig_param_local_;
+ return ifconfig_param_;
}
-Options& Options::setIfconfigParamLocal(std::string i)
+Options& Options::setIfconfigParam(OptionNetwork i)
{
WritersLock lock(mutex);
- ifconfig_param_local_ = i;
- return *this;
-}
-
-std::string Options::getIfconfigParamRemoteNetmask()
-{
- ReadersLock lock(mutex);
- return ifconfig_param_remote_netmask_;
-}
-
-Options& Options::setIfconfigParamRemoteNetmask(std::string i)
-{
- WritersLock lock(mutex);
- ifconfig_param_remote_netmask_ = i;
+ ifconfig_param_ = i;
return *this;
}
diff --git a/src/options.h b/src/options.h
index dda7b10..9d07c84 100644
--- a/src/options.h
+++ b/src/options.h
@@ -123,10 +123,8 @@ public:
Options& setDevName(std::string d);
std::string getDevType();
Options& setDevType(std::string d);
- std::string getIfconfigParamLocal();
- Options& setIfconfigParamLocal(std::string i);
- std::string getIfconfigParamRemoteNetmask();
- Options& setIfconfigParamRemoteNetmask(std::string i);
+ OptionNetwork getIfconfigParam();
+ Options& setIfconfigParam(OptionNetwork i);
std::string getPostUpScript();
Options& setPostUpScript(std::string p);
NetworkList getRoutes();
@@ -190,8 +188,7 @@ private:
std::string dev_name_;
std::string dev_type_;
- std::string ifconfig_param_local_;
- std::string ifconfig_param_remote_netmask_;
+ OptionNetwork ifconfig_param_;
std::string post_up_script_;
NetworkList routes_;
diff --git a/src/tunDevice.h b/src/tunDevice.h
index 85f6a35..c470b9c 100644
--- a/src/tunDevice.h
+++ b/src/tunDevice.h
@@ -43,7 +43,7 @@
class TunDevice
{
public:
- TunDevice(std::string dev,std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp);
+ TunDevice(std::string dev,std::string dev_type, std::string ifcfg_addr, u_int16_t ifcfg_prefix);
~TunDevice();
int read(u_int8_t* buf, u_int32_t len);