further cleanups
[anytun.git] / tunDevice.cpp
blob81f4cbb4b5b6dbcfe607c7c408d0a980fac530cc
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 <stdexcept>
32 #include <poll.h>
34 extern "C" {
35 #include "openvpn/config.h"
36 #include "openvpn/syshead.h"
37 #include "openvpn/tun.h"
40 #include "tunDevice.h"
41 #include "threadUtils.hpp"
44 TunDevice::TunDevice(const char* dev_name,const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
46 dev_ = NULL;
48 // init_tun (const char *dev, /* --dev option */
49 // const char *dev_type, /* --dev-type option */
50 // const char *ifconfig_local_parm, /* --ifconfig parm 1 */
51 // const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
52 // in_addr_t local_public,
53 // in_addr_t remote_public,
54 // const bool strict_warn,
55 // struct env_set *es)
57 // init_tun_post (struct tuntap *tt,
58 // const struct frame *frame,
59 // const struct tuntap_options *options)
61 // open_tun (const char *dev,
62 // const char *dev_type,
63 // const char *dev_node,
64 // bool ipv6,
65 // struct tuntap *tt)
67 // -------------------------------------------
69 // c->c1.tuntap = init_tun (c->options.dev,
70 // c->options.dev_type,
71 // c->options.ifconfig_local,
72 // c->options.ifconfig_remote_netmask,
73 // addr_host (&c->c1.link_socket_addr.local),
74 // addr_host (&c->c1.link_socket_addr.remote),
75 // !c->options.ifconfig_nowarn,
76 // c->c2.es);
78 // init_tun_post (c->c1.tuntap,
79 // &c->c2.frame,
80 // &c->options.tuntap_options);
82 // open_tun (c->options.dev,
83 // c->options.dev_type,
84 // c->options.dev_node,
85 // c->options.tun_ipv6,
86 // c->c1.tuntap);
90 in_addr_t lp, rp;
92 // lp = inet_addr("192.168.198.1");
93 // rp = inet_addr("192.168.199.1");
95 dev_ = init_tun(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, lp, rp, 0, NULL);
96 struct frame frame; // just for win32
97 struct tuntap_options options; // win32 & linux
98 options.txqueuelen = 100; // just for linux
99 init_tun_post(dev_, &frame, &options);
100 if(!dev_)
101 throw std::runtime_error("can't init tun/tap device");
103 open_tun (dev_name, NULL, NULL, false, dev_);
104 do_ifconfig(dev_, dev_->actual_name, 1000, NULL);
107 TunDevice::~TunDevice()
109 if(dev_)
110 close_tun(dev_);
113 short TunDevice::read(Buffer& buf)
115 if(!dev_)
116 return -1;
118 struct pollfd pfd[1];
119 pfd[0].fd = tun_event_handle(dev_);
120 pfd[0].events = POLLIN | POLLPRI;
121 pfd[0].revents = 0;
122 poll(pfd, 1, -1);
123 Lock lock(io_mutex_);
124 return read_tun(dev_, buf, buf.getLength());
127 int TunDevice::write(Buffer& buf)
129 if(!dev_)
130 return -1;
131 Lock lock(io_mutex_);
132 return write_tun(dev_, buf, buf.getLength());
135 char* TunDevice::getActualName()
137 if(!dev_)
138 return NULL;
140 return dev_->actual_name;
143 u_int32_t TunDevice::getType()
145 if(!dev_)
146 return TYPE_UNDEF;
148 switch(dev_->type)
150 case DEV_TYPE_TUN: return TYPE_TUN;
151 case DEV_TYPE_TAP: return TYPE_TAP;
153 return TYPE_UNDEF;
156 const char* TunDevice::getTypeString()
158 if(!dev_)
159 return NULL;
161 switch(dev_->type)
163 case DEV_TYPE_UNDEF: return "undef"; break;
164 case DEV_TYPE_TUN: return "tun"; break;
165 case DEV_TYPE_TAP: return "tap"; break;
167 return NULL;