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
34 #include <sys/socket.h>
36 #include <net/if_tun.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
46 #include "tunDevice.h"
47 #include "threadUtils.hpp"
48 #define DEVICE_FILE_MAX 255
52 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
)
54 std::string device_file
= "/dev/";
57 device_file
.append(dev_name
);
60 else if(conf_
.type_
== TYPE_TUN
) {
61 device_file
.append("tun");
64 else if(conf_
.type_
== TYPE_TAP
) {
65 device_file
.append("tap");
69 throw std::runtime_error("unable to recognize type of device (tun or tap)");
73 for(; dev_id
<= DEVICE_FILE_MAX
; ++dev_id
) {
74 std::ostringstream ds
;
77 fd_
= ::open(ds
.str().c_str(), O_RDWR
);
83 fd_
= ::open(device_file
.c_str(), O_RDWR
);
88 msg
= "can't open device file dynamically: no unused node left";
90 msg
= "can't open device file (";
91 msg
.append(device_file
);
93 msg
.append(strerror(errno
));
95 throw std::runtime_error(msg
);
102 actual_name_
= s
.str();
105 actual_name_
= dev_name
;
109 if(ifcfg_lp
&& ifcfg_rnmp
)
113 TunDevice::~TunDevice()
119 #if defined(__GNUC__) && defined(__OpenBSD__)
121 void TunDevice::init_post()
124 if(conf_
.type_
== TYPE_TAP
)
129 if (ioctl(fd_
, TUNGIFINFO
, &ti
) < 0)
130 throw std::runtime_error("can't enable multicast for interface");
132 ti
.flags
|= IFF_MULTICAST
;
134 if (ioctl(fd_
, TUNSIFINFO
, &ti
) < 0)
135 throw std::runtime_error("can't enable multicast for interface");
138 #elif defined(__GNUC__) && defined(__FreeBSD__)
140 void TunDevice::init_post()
143 if(conf_
.type_
== TYPE_TAP
)
147 ioctl(fd_
, TUNSLMODE
, &arg
);
149 ioctl(fd_
, TUNSIFHEAD
, &arg
);
152 #elif defined(__GNUC__) && defined(__NetBSD__)
154 void TunDevice::init_post()
158 int arg
= IFF_POINTOPOINT
|IFF_MULTICAST
;
159 ioctl(fd_
, TUNSIFMODE
, &arg
);
161 ioctl(fd_
, TUNSLMODE
, &arg
);
165 #error Target not supported
168 int TunDevice::fix_return(int ret
, size_t type_length
)
173 return (static_cast<size_t>(ret
) > type_length
? (ret
- type_length
) : 0);
176 short TunDevice::read(u_int8_t
* buf
, u_int32_t len
)
185 iov
[0].iov_base
= &type
;
186 iov
[0].iov_len
= sizeof(type
);
187 iov
[1].iov_base
= buf
;
188 iov
[1].iov_len
= len
;
189 return(fix_return(::readv(fd_
, iov
, 2), sizeof(type
)));
192 return(::read(fd_
, buf
, len
));
195 int TunDevice::write(u_int8_t
* buf
, u_int32_t len
)
203 struct ip
*hdr
= reinterpret_cast<struct ip
*>(buf
);
207 type
= htonl(AF_INET
);
209 type
= htonl(AF_INET6
);
211 iov
[0].iov_base
= &type
;
212 iov
[0].iov_len
= sizeof(type
);
213 iov
[1].iov_base
= buf
;
214 iov
[1].iov_len
= len
;
215 return(fix_return(::writev(fd_
, iov
, 2), sizeof(type
)));
218 return(::write(fd_
, buf
, len
));
221 const char* TunDevice::getActualName()
223 return actual_name_
.c_str();
226 device_type_t
TunDevice::getType()
231 const char* TunDevice::getTypeString()
238 case TYPE_UNDEF
: return "undef"; break;
239 case TYPE_TUN
: return "tun"; break;
240 case TYPE_TAP
: return "tap"; break;
245 void TunDevice::do_ifconfig()
247 // std::string command("/sbin/ifconfig ");
248 // command.append(actual_name_);
249 // command.append(" ");
250 // command.append(conf_.local_.toString());
251 // command.append(" ");
253 // if(conf_.type_ == TYPE_TUN)
254 // command.append("pointopoint ");
256 // command.append("netmask ");
258 // command.append(conf_.remote_netmask_.toString());
259 // command.append(" mtu 1400");
261 // system(command.c_str());