do_ifconfig works now for new tun/tap device
[anytun.git] / src / linux / tunDevice.cpp
blob8548331c7922e5b2b5cd057b87a9cccf474ed775
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) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp)
47 fd_ = ::open(DEFAULT_DEVICE, O_RDWR);
48 if(fd_ < 0) {
49 std::string msg("can't open device file: ");
50 msg.append(strerror(errno));
51 throw std::runtime_error(msg);
54 struct ifreq ifr;
55 memset(&ifr, 0, sizeof(ifr));
57 if(conf_.type_ == TYPE_TUN) {
58 ifr.ifr_flags = IFF_TUN;
59 with_pi_ = true;
61 else if(conf_.type_ == TYPE_TAP) {
62 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
63 with_pi_ = false;
65 else
66 throw std::runtime_error("unable to recognize type of device (tun or tap)");
68 if(dev_name)
69 strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
71 if(!ioctl(fd_, TUNSETIFF, &ifr)) {
72 actual_name_ = ifr.ifr_name;
73 } else if(!ioctl(fd_, (('T' << 8) | 202), &ifr)) {
74 actual_name_ = ifr.ifr_name;
75 } else {
76 std::string msg("tun/tap device ioctl failed: ");
77 msg.append(strerror(errno));
78 throw std::runtime_error(msg);
81 if(ifcfg_lp && ifcfg_rnmp)
82 do_ifconfig();
85 TunDevice::~TunDevice()
87 if(fd_ > 0)
88 ::close(fd_);
91 short TunDevice::read(u_int8_t* buf, u_int32_t len)
93 if(fd_ < 0)
94 return -1;
96 if(with_pi_)
98 struct iovec iov[2];
99 struct tun_pi tpi;
101 iov[0].iov_base = &tpi;
102 iov[0].iov_len = sizeof(tpi);
103 iov[1].iov_base = buf;
104 iov[1].iov_len = len;
105 return(::readv(fd_, iov, 2) - sizeof(tpi));
107 else
108 return(::read(fd_, buf, len));
111 int TunDevice::write(u_int8_t* buf, u_int32_t len)
113 if(fd_ < 0)
114 return -1;
116 if(with_pi_)
118 struct iovec iov[2];
119 struct tun_pi tpi;
120 struct iphdr *hdr = reinterpret_cast<struct iphdr *>(buf);
122 tpi.flags = 0;
123 if(hdr->version == 4)
124 tpi.proto = htons(ETH_P_IP);
125 else
126 tpi.proto = htons(ETH_P_IPV6);
128 iov[0].iov_base = &tpi;
129 iov[0].iov_len = sizeof(tpi);
130 iov[1].iov_base = buf;
131 iov[1].iov_len = len;
132 return(::writev(fd_, iov, 2) - sizeof(tpi));
134 else
135 return(::write(fd_, buf, len));
138 const char* TunDevice::getActualName()
140 return actual_name_.c_str();
143 device_type_t TunDevice::getType()
145 return conf_.type_;
148 const char* TunDevice::getTypeString()
150 if(fd_ < 0)
151 return NULL;
153 switch(conf_.type_)
155 case TYPE_UNDEF: return "undef"; break;
156 case TYPE_TUN: return "tun"; break;
157 case TYPE_TAP: return "tap"; break;
159 return NULL;
162 void TunDevice::do_ifconfig()
164 std::string command("/sbin/ifconfig ");
165 command.append(actual_name_);
166 command.append(" ");
167 command.append(conf_.local_.toString());
168 command.append(" ");
170 if(conf_.type_ == TYPE_TUN)
171 command.append("pointopoint ");
172 else
173 command.append("netmask ");
175 command.append(conf_.remote_netmask_.toString());
176 command.append(" mtu 1400");
178 system(command.c_str());