tunDevice can now be open and closed
[anytun.git] / tunDevice.cpp
blobf81a987f54c7af6e142c8bd15fc1f3c504ddfc30
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>
33 extern "C" {
34 #include "openvpn/config.h"
35 #include "openvpn/syshead.h"
36 #include "openvpn/tun.h"
39 #include "tunDevice.h"
42 TunDevice::TunDevice(const char* dev_name, const char* ifcfg_lp, const char* ifcfg_rnmp)
44 is_open_ = false;
45 dev_ = NULL;
46 if(!dev_name)
47 dev_name_ = NULL;
48 else
49 strcpy(dev_name_,dev_name);
51 // init_tun (const char *dev, /* --dev option */
52 // const char *dev_type, /* --dev-type option */
53 // const char *ifconfig_local_parm, /* --ifconfig parm 1 */
54 // const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
55 // in_addr_t local_public,
56 // in_addr_t remote_public,
57 // const bool strict_warn,
58 // struct env_set *es)
60 // init_tun_post (struct tuntap *tt,
61 // const struct frame *frame,
62 // const struct tuntap_options *options)
64 // -------------------------------------------
66 // c->c1.tuntap = init_tun (c->options.dev,
67 // c->options.dev_type,
68 // c->options.ifconfig_local,
69 // c->options.ifconfig_remote_netmask,
70 // addr_host (&c->c1.link_socket_addr.local),
71 // addr_host (&c->c1.link_socket_addr.remote),
72 // !c->options.ifconfig_nowarn,
73 // c->c2.es);
75 // init_tun_post (c->c1.tuntap,
76 // &c->c2.frame,
77 // &c->options.tuntap_options);
79 in_addr_t lp, rp;
81 // lp = inet_addr("192.168.198.1");
82 // rp = inet_addr("192.168.199.1");
84 dev_ = init_tun(dev_name, NULL, ifcfg_lp, ifcfg_rnmp, lp, rp, 0, NULL);
85 //init_tun_post(dev_, NULL, NULL);
86 if(!dev_)
87 throw std::runtime_error("can't init tun");
90 TunDevice::~TunDevice()
92 if(dev_ && is_open_)
93 close_tun(dev_);
96 void TunDevice::open()
98 if(!dev_)
99 return;
100 // open_tun (const char *dev,
101 // const char *dev_type,
102 // const char *dev_node,
103 // bool ipv6,
104 // struct tuntap *tt)
106 // ------------------------
108 // open_tun (c->options.dev,
109 // c->options.dev_type,
110 // c->options.dev_node,
111 // c->options.tun_ipv6,
112 // c->c1.tuntap);
114 open_tun (dev_name_, NULL, NULL, false, dev_);
115 do_ifconfig(dev_, dev_->actual_name, 1500, NULL);
116 is_open_ = true;
119 void TunDevice::close()
121 if(dev_)
122 close_tun(dev_);
123 is_open_ = false;
126 bool TunDevice::isOpen()
128 return is_open_;
131 int TunDevice::read(Buffer& buf)
133 if(!dev_)
134 return -1;
136 return read_tun(dev_, buf, buf.getLength());
139 int TunDevice::write(Buffer& buf)
141 if(!dev_)
142 return -1;
144 return write_tun(dev_, buf, buf.getLength());
147 char* TunDevice::getActualName()
149 if(!dev_)
150 return NULL;
152 return dev_->actual_name;