bugfix @buffer resize
[anytun.git] / tunDevice.cpp
blob0f17fb7c1f69d3ff84385627008b65f2878de4ea
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"
43 TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifcfg_rnmp)
45 dev_ = NULL;
47 // init_tun (const char *dev, /* --dev option */
48 // const char *dev_type, /* --dev-type option */
49 // const char *ifconfig_local_parm, /* --ifconfig parm 1 */
50 // const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
51 // in_addr_t local_public,
52 // in_addr_t remote_public,
53 // const bool strict_warn,
54 // struct env_set *es)
56 // init_tun_post (struct tuntap *tt,
57 // const struct frame *frame,
58 // const struct tuntap_options *options)
60 // open_tun (const char *dev,
61 // const char *dev_type,
62 // const char *dev_node,
63 // bool ipv6,
64 // struct tuntap *tt)
66 // -------------------------------------------
68 // c->c1.tuntap = init_tun (c->options.dev,
69 // c->options.dev_type,
70 // c->options.ifconfig_local,
71 // c->options.ifconfig_remote_netmask,
72 // addr_host (&c->c1.link_socket_addr.local),
73 // addr_host (&c->c1.link_socket_addr.remote),
74 // !c->options.ifconfig_nowarn,
75 // c->c2.es);
77 // init_tun_post (c->c1.tuntap,
78 // &c->c2.frame,
79 // &c->options.tuntap_options);
81 // open_tun (c->options.dev,
82 // c->options.dev_type,
83 // c->options.dev_node,
84 // c->options.tun_ipv6,
85 // c->c1.tuntap);
89 in_addr_t lp, rp;
91 // lp = inet_addr("192.168.198.1");
92 // rp = inet_addr("192.168.199.1");
94 dev_ = init_tun(dev_name, NULL, ifcfg_lp, ifcfg_rnmp, lp, rp, 0, NULL);
95 //init_tun_post(dev_, NULL, NULL);
96 if(!dev_)
97 throw std::runtime_error("can't init tun/tap device");
99 open_tun (dev_name, NULL, NULL, false, dev_);
100 do_ifconfig(dev_, dev_->actual_name, 1500, NULL);
103 TunDevice::~TunDevice()
105 if(dev_)
106 close_tun(dev_);
109 short TunDevice::read(Buffer& buf)
111 if(!dev_)
112 return -1;
114 struct pollfd pfd[1];
115 pfd[0].fd = tun_event_handle(dev_);
116 // pfd[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLOUT | POLLWRNORM | POLLWRBAND | POLLERR | POLLHUP | POLLNVAL;
117 pfd[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
118 poll(pfd, 1, -1);
119 return pfd[0].revents;//read_tun(dev_, buf, buf.getLength());
122 int TunDevice::write(Buffer& buf)
124 if(!dev_)
125 return -1;
127 return write_tun(dev_, buf, buf.getLength());
130 char* TunDevice::getActualName()
132 if(!dev_)
133 return NULL;
135 return dev_->actual_name;
138 char* TunDevice::getType()
140 if(!dev_)
141 return NULL;
143 switch(dev_->type)
145 case DEV_TYPE_UNDEF: return "undef"; break;
146 case DEV_TYPE_TUN: return "tun"; break;
147 case DEV_TYPE_TAP: return "tap"; break;
149 return NULL;