bugfix @ tundevice and ipv6
[anytun.git] / src / linux / tunDevice.cpp
blobeb678ac0506bf2f3bd0f853abf2e30600211806f
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <net/if.h>
36 #include <linux/ip.h>
37 #include <linux/if_ether.h>
38 #include <linux/if_tun.h>
39 #define DEFAULT_DEVICE "/dev/net/tun"
41 #include "tunDevice.h"
42 #include "threadUtils.hpp"
45 TunDevice::TunDevice(const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
47 fd_ = -1;
48 type_ = TYPE_UNDEF;
50 fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
52 if(fd_ < 0) {
53 std::string msg("can't open device file: ");
54 msg.append(strerror(errno));
55 throw std::runtime_error(msg);
58 struct ifreq ifr;
59 memset(&ifr, 0, sizeof(ifr));
61 // tun device
62 ifr.ifr_flags = IFF_TUN;
63 type_ = TYPE_TUN;
65 // tap device
66 // ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
67 // type_ = TYPE_TAP;
69 if(dev_name)
70 strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
72 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
73 actual_name_ = ifr.ifr_name;
74 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
75 actual_name_ = ifr.ifr_name;
76 } else {
77 std::string msg("tun/tap device ioctl failed: ");
78 msg.append(strerror(errno));
79 throw std::runtime_error(msg);
83 TunDevice::~TunDevice()
85 if(fd_ > 0)
86 ::close(fd_);
89 short TunDevice::read(u_int8_t* buf, u_int32_t len)
91 if(fd_ < 0)
92 return -1;
94 struct iovec iov[2];
95 struct tun_pi tpi;
97 iov[0].iov_base = &tpi;
98 iov[0].iov_len = sizeof(tpi);
99 iov[1].iov_base = buf;
100 iov[1].iov_len = len;
101 return(::readv(fd_, iov, 2) - sizeof(tpi));
104 int TunDevice::write(u_int8_t* buf, u_int32_t len)
106 if(fd_ < 0)
107 return -1;
109 struct iovec iov[2];
110 struct tun_pi tpi;
111 struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
113 tpi.flags = 0;
114 if(hdr->version == 6)
115 tpi.proto = htons(ETH_P_IPV6);
116 else
117 tpi.proto = htons(ETH_P_IP);
119 iov[0].iov_base = &tpi;
120 iov[0].iov_len = sizeof(tpi);
121 iov[1].iov_base = buf;
122 iov[1].iov_len = len;
123 return(writev(fd_, iov, 2) - sizeof(tpi));
126 const char* TunDevice::getActualName()
128 return actual_name_.c_str();
131 u_int32_t TunDevice::getType()
133 return type_;
136 const char* TunDevice::getTypeString()
138 if(fd_ < 0)
139 return NULL;
141 switch(type_)
143 case TYPE_UNDEF: return "undef"; break;
144 case TYPE_TUN: return "tun"; break;
145 case TYPE_TAP: return "tap"; break;
147 return NULL;