summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-05-09 18:19:05 +0000
committerChristian Pointner <equinox@anytun.org>2008-05-09 18:19:05 +0000
commite9104d332552bc617b30bc8b7db0ccfac53bff72 (patch)
tree4f95804bb4236782ac9fa080040dbe5fff3a660a /src
parentbugfix @ tundevice and ipv6 (diff)
linux tun/tap device works now with tun *and* tap
TODO: ifconfig
Diffstat (limited to 'src')
-rw-r--r--src/anytun.cpp32
-rw-r--r--src/deviceConfig.hpp66
-rw-r--r--src/linux/tunDevice.cpp83
-rw-r--r--src/linux/tunDevice.h10
-rw-r--r--src/options.cpp5
-rw-r--r--src/ovpn/tunDevice.cpp2
-rw-r--r--src/ovpn/tunDevice.h3
7 files changed, 139 insertions, 62 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp
index 281e09b..e10d6fb 100644
--- a/src/anytun.cpp
+++ b/src/anytun.cpp
@@ -140,9 +140,9 @@ void* sender(void* p)
u_int32_t len = param->dev.read(plain_packet.getPayload(), plain_packet.getPayloadLength());
plain_packet.setPayloadLength(len);
// set payload type
- if(param->dev.getType() == TunDevice::TYPE_TUN)
+ if(param->dev.getType() == TYPE_TUN)
plain_packet.setPayloadType(PAYLOAD_TYPE_TUN);
- else if(param->dev.getType() == TunDevice::TYPE_TAP)
+ else if(param->dev.getType() == TYPE_TAP)
plain_packet.setPayloadType(PAYLOAD_TYPE_TAP);
else
plain_packet.setPayloadType(0);
@@ -303,9 +303,9 @@ void* receiver(void* p)
c->decrypt(encrypted_packet, plain_packet);
// check payload_type
- if((param->dev.getType() == TunDevice::TYPE_TUN && plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN4 &&
- plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN6) ||
- (param->dev.getType() == TunDevice::TYPE_TAP && plain_packet.getPayloadType() != PAYLOAD_TYPE_TAP))
+ if((param->dev.getType() == TYPE_TUN && plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN4 &&
+ plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN6) ||
+ (param->dev.getType() == TYPE_TAP && plain_packet.getPayloadType() != PAYLOAD_TYPE_TAP))
continue;
// write it on the device
@@ -443,8 +443,8 @@ int main(int argc, char* argv[])
}
}
- std::string dev_type(gOpt.getDevType());
- TunDevice dev(gOpt.getDevName().c_str(), dev_type=="" ? NULL : dev_type.c_str(),
+ 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());
cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)";
@@ -456,16 +456,16 @@ int main(int argc, char* argv[])
}
-// Buffer buff(u_int32_t(1600));
-// int len;
-// while(1)
-// {
-// len = dev.read(buff.getBuf(), buff.getLength());
-// std::cout << "read " << len << " bytes form interface " << dev.getActualName() << std::endl;
-// dev.write(buff.getBuf(), len);
-// }
+ Buffer buff(u_int32_t(1600));
+ int len;
+ while(1)
+ {
+ len = dev.read(buff.getBuf(), buff.getLength());
+ std::cout << "read " << len << " bytes form interface " << dev.getActualName() << std::endl;
+ dev.write(buff.getBuf(), len);
+ }
-// exit(0);
+ exit(0);
diff --git a/src/deviceConfig.hpp b/src/deviceConfig.hpp
new file mode 100644
index 0000000..22fb5d8
--- /dev/null
+++ b/src/deviceConfig.hpp
@@ -0,0 +1,66 @@
+/*
+ * 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 anytun.org <satp@wirdorange.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 this program (see the file COPYING included with this
+ * distribution); if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _DEVICE_CONFIG_HPP_
+#define _DEVICE_CONFIG_HPP_
+
+class TunDevice;
+
+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)
+ {
+ type_ = TYPE_UNDEF;
+ if(dev_type) {
+ if(!strncmp(dev_type, "tun", 3))
+ type_ = TYPE_TUN;
+ else if(!strncmp(dev_type, "tap", 3))
+ type_ = TYPE_TAP;
+ }
+ else if(dev_name)
+ {
+ if(!strncmp(dev_name, "tun", 3))
+ type_ = TYPE_TUN;
+ else if(!strncmp(dev_name, "tap", 3))
+ type_ = TYPE_TAP;
+ }
+
+ }
+
+private:
+ device_type_t type_;
+
+ friend class TunDevice;
+};
+
+#endif
diff --git a/src/linux/tunDevice.cpp b/src/linux/tunDevice.cpp
index eb678ac..24bad02 100644
--- a/src/linux/tunDevice.cpp
+++ b/src/linux/tunDevice.cpp
@@ -42,13 +42,9 @@
#include "threadUtils.hpp"
-TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* 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)
{
- fd_ = -1;
- type_ = TYPE_UNDEF;
-
fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
-
if(fd_ < 0) {
std::string msg("can't open device file: ");
msg.append(strerror(errno));
@@ -58,13 +54,16 @@ TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifc
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
-// tun device
- ifr.ifr_flags = IFF_TUN;
- type_ = TYPE_TUN;
-
-// tap device
-// ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
-// type_ = TYPE_TAP;
+ if(conf_.type_ == TYPE_TUN) {
+ ifr.ifr_flags = IFF_TUN;
+ with_pi_ = true;
+ }
+ else if(conf_.type_ == TYPE_TAP) {
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+ with_pi_ = false;
+ }
+ else
+ throw std::runtime_error("unable to recognize type of device (tun or tap)");
if(dev_name)
strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
@@ -91,14 +90,19 @@ short TunDevice::read(u_int8_t* buf, u_int32_t len)
if(fd_ < 0)
return -1;
- struct iovec iov[2];
- struct tun_pi tpi;
-
- iov[0].iov_base = &tpi;
- iov[0].iov_len = sizeof(tpi);
- iov[1].iov_base = buf;
- iov[1].iov_len = len;
- return(::readv(fd_, iov, 2) - sizeof(tpi));
+ if(with_pi_)
+ {
+ struct iovec iov[2];
+ struct tun_pi tpi;
+
+ iov[0].iov_base = &tpi;
+ iov[0].iov_len = sizeof(tpi);
+ iov[1].iov_base = buf;
+ iov[1].iov_len = len;
+ return(::readv(fd_, iov, 2) - sizeof(tpi));
+ }
+ else
+ return(::read(fd_, buf, len));
}
int TunDevice::write(u_int8_t* buf, u_int32_t len)
@@ -106,21 +110,26 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len)
if(fd_ < 0)
return -1;
- struct iovec iov[2];
- struct tun_pi tpi;
- struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
-
- tpi.flags = 0;
- if(hdr->version == 6)
- tpi.proto = htons(ETH_P_IPV6);
+ if(with_pi_)
+ {
+ struct iovec iov[2];
+ struct tun_pi tpi;
+ struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
+
+ tpi.flags = 0;
+ if(hdr->version == 6)
+ tpi.proto = htons(ETH_P_IPV6);
+ else
+ tpi.proto = htons(ETH_P_IP);
+
+ iov[0].iov_base = &tpi;
+ iov[0].iov_len = sizeof(tpi);
+ iov[1].iov_base = buf;
+ iov[1].iov_len = len;
+ return(::writev(fd_, iov, 2) - sizeof(tpi));
+ }
else
- tpi.proto = htons(ETH_P_IP);
-
- iov[0].iov_base = &tpi;
- iov[0].iov_len = sizeof(tpi);
- iov[1].iov_base = buf;
- iov[1].iov_len = len;
- return(writev(fd_, iov, 2) - sizeof(tpi));
+ return(::write(fd_, buf, len));
}
const char* TunDevice::getActualName()
@@ -128,9 +137,9 @@ const char* TunDevice::getActualName()
return actual_name_.c_str();
}
-u_int32_t TunDevice::getType()
+device_type_t TunDevice::getType()
{
- return type_;
+ return conf_.type_;
}
const char* TunDevice::getTypeString()
@@ -138,7 +147,7 @@ const char* TunDevice::getTypeString()
if(fd_ < 0)
return NULL;
- switch(type_)
+ switch(conf_.type_)
{
case TYPE_UNDEF: return "undef"; break;
case TYPE_TUN: return "tun"; break;
diff --git a/src/linux/tunDevice.h b/src/linux/tunDevice.h
index 4588964..9f3557a 100644
--- a/src/linux/tunDevice.h
+++ b/src/linux/tunDevice.h
@@ -32,15 +32,12 @@
#define _TUNDEVICE_H_
#include "buffer.h"
+#include "deviceConfig.hpp"
#include "threadUtils.hpp"
class TunDevice
{
public:
- static const u_int32_t TYPE_UNDEF = 0;
- static const u_int32_t TYPE_TUN = 1;
- static const u_int32_t TYPE_TAP = 2;
-
TunDevice(const char* dev,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp);
~TunDevice();
@@ -48,7 +45,7 @@ public:
int write(u_int8_t* buf, u_int32_t len);
const char* getActualName();
- u_int32_t getType();
+ device_type_t getType();
const char* getTypeString();
private:
@@ -56,7 +53,8 @@ private:
TunDevice(const TunDevice &src);
int fd_;
- u_int32_t type_;
+ DeviceConfig conf_;
+ bool with_pi_;
std::string actual_name_;
};
diff --git a/src/options.cpp b/src/options.cpp
index 3b7e468..798504b 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -66,7 +66,7 @@ Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
remote_sync_addr_ = "";
remote_addr_ = "";
remote_port_ = 4444;
- dev_name_ = "tun";
+ dev_name_ = "";
dev_type_ = "";
ifconfig_param_local_ = "";
ifconfig_param_remote_netmask_ = "";
@@ -194,6 +194,9 @@ bool Options::parse(int argc, char* argv[])
if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null")
kd_prf_ = "aes-ctr";
+ if(dev_name_ == "" && dev_type_ == "")
+ dev_type_ = "tun";
+
while(!host_port_queue.empty())
{
std::stringstream tmp_stream(host_port_queue.front());
diff --git a/src/ovpn/tunDevice.cpp b/src/ovpn/tunDevice.cpp
index b3c07b0..032af29 100644
--- a/src/ovpn/tunDevice.cpp
+++ b/src/ovpn/tunDevice.cpp
@@ -144,7 +144,7 @@ char* TunDevice::getActualName()
return dev_->actual_name;
}
-u_int32_t TunDevice::getType()
+device_type_t TunDevice::getType()
{
if(!dev_)
return TYPE_UNDEF;
diff --git a/src/ovpn/tunDevice.h b/src/ovpn/tunDevice.h
index 56b06fd..ba41cce 100644
--- a/src/ovpn/tunDevice.h
+++ b/src/ovpn/tunDevice.h
@@ -32,6 +32,7 @@
#define _TUNDEVICE_H_
#include "buffer.h"
+#include "deviceConfig.hpp"
#include "threadUtils.hpp"
class TunDevice
@@ -48,7 +49,7 @@ public:
int write(u_int8_t* buf, u_int32_t len);
char* getActualName();
- u_int32_t getType();
+ device_type_t getType();
const char* getTypeString();
private: