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
32 #include <sys/ioctl.h>
33 #include <arpa/inet.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
)
50 fd_
= ::open(DEFAULT_DEVICE
, O_RDWR
);
53 std::string
msg("can't open device file: ");
54 msg
.append(strerror(errno
));
55 throw std::runtime_error(msg
);
59 memset(&ifr
, 0, sizeof(ifr
));
62 ifr
.ifr_flags
= IFF_TUN
;
66 // ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
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
;
77 std::string
msg("tun/tap device ioctl failed: ");
78 msg
.append(strerror(errno
));
79 throw std::runtime_error(msg
);
83 TunDevice::~TunDevice()
89 short TunDevice::read(u_int8_t
* buf
, u_int32_t len
)
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
)
111 struct iphdr
*hdr
= reinterpret_cast<struct iphdr
*>(buf
);
114 if(hdr
->version
== 6)
115 tpi
.proto
= htons(ETH_P_IPV6
);
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()
136 const char* TunDevice::getTypeString()
143 case TYPE_UNDEF
: return "undef"; break;
144 case TYPE_TUN
: return "tun"; break;
145 case TYPE_TAP
: return "tap"; break;