3 Copyright (C) 2013 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>
33 static struct vdepluglib plug;
34 static struct vdeconn *conn = NULL;
36 static char *group = NULL;
37 static char *device_info;
39 static bool setup_device(void) {
40 libvdeplug_dynopen(plug);
43 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open libvdeplug library!");
47 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
48 xasprintf(&device, LOCALSTATEDIR "/run/vde.ctl");
50 get_config_string(lookup_config(config_tree, "Interface"), &iface);
52 get_config_int(lookup_config(config_tree, "VDEPort"), &port);
54 get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
56 device_info = "VDE socket";
58 struct vde_open_args args = {
64 conn = plug.vde_open(device, identname, &args);
66 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open VDE socket %s", device);
70 device_fd = plug.vde_datafd(conn);
73 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
76 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
78 if(routing_mode == RMODE_ROUTER)
84 static void close_device(void) {
86 plug.vde_close(conn); conn = NULL;
90 libvdeplug_dynclose(plug);
92 free(device); device = NULL;
94 free(iface); iface = NULL;
99 static bool read_packet(vpn_packet_t *packet) {
100 int lenin = (ssize_t)plug.vde_recv(conn, DATA(packet), MTU, 0);
102 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
109 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
114 static bool write_packet(vpn_packet_t *packet) {
115 if((ssize_t)plug.vde_send(conn, DATA(packet), packet->len, 0) < 0) {
116 if(errno != EINTR && errno != EAGAIN) {
117 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
127 const devops_t vde_devops = {
128 .setup = setup_device,
129 .close = close_device,
131 .write = write_packet,