3 Copyright (C) 2012 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <libvdeplug_dyn.h>
32 static struct vdepluglib plug;
33 static struct vdeconn *conn = NULL;
35 static char *group = NULL;
36 static char *device_info;
38 extern char *identname;
39 extern volatile bool running;
41 static uint64_t device_total_in = 0;
42 static uint64_t device_total_out = 0;
44 static bool setup_device(void) {
45 libvdeplug_dynopen(plug);
48 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open libvdeplug library!");
52 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
53 xasprintf(&device, LOCALSTATEDIR "/run/vde.ctl");
55 get_config_string(lookup_config(config_tree, "Interface"), &iface);
57 get_config_int(lookup_config(config_tree, "VDEPort"), &port);
59 get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
61 device_info = "VDE socket";
63 struct vde_open_args args = {
69 conn = plug.vde_open(device, identname, &args);
71 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open VDE socket %s", device);
75 device_fd = plug.vde_datafd(conn);
78 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
81 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
83 if(routing_mode == RMODE_ROUTER)
89 static void close_device(void) {
94 libvdeplug_dynclose(plug);
101 static bool read_packet(vpn_packet_t *packet) {
102 int lenin = plug.vde_recv(conn, packet->data, MTU, 0);
104 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
110 device_total_in += packet->len;
111 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
116 static bool write_packet(vpn_packet_t *packet) {
117 if(plug.vde_send(conn, packet->data, packet->len, 0) < 0) {
118 if(errno != EINTR && errno != EAGAIN) {
119 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
126 device_total_out += packet->len;
131 static void dump_device_stats(void) {
132 logger(DEBUG_ALWAYS, LOG_DEBUG, "Statistics for %s %s:", device_info, device);
133 logger(DEBUG_ALWAYS, LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
134 logger(DEBUG_ALWAYS, LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
137 const devops_t vde_devops = {
138 .setup = setup_device,
139 .close = close_device,
141 .write = write_packet,
142 .dump_stats = dump_device_stats,