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.h>
32 static struct vdeconn *conn = NULL;
34 static char *group = NULL;
35 static const char *device_info = "VDE socket";
37 static bool setup_device(void) {
38 if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
39 xasprintf(&device, RUNSTATEDIR "/vde.ctl");
42 get_config_string(lookup_config(&config_tree, "Interface"), &iface);
44 get_config_int(lookup_config(&config_tree, "VDEPort"), &port);
46 get_config_string(lookup_config(&config_tree, "VDEGroup"), &group);
48 struct vde_open_args args = {
54 conn = vde_open(device, identname, &args);
57 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open VDE socket %s", device);
61 device_fd = vde_datafd(conn);
64 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
67 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
69 if(routing_mode == RMODE_ROUTER) {
76 static void close_device(void) {
91 static bool read_packet(vpn_packet_t *packet) {
92 ssize_t lenin = vde_recv(conn, DATA(packet), MTU, 0);
95 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
101 logger(DEBUG_TRAFFIC, LOG_DEBUG,
102 "Dropped a packet received from %s - the sender was not allowed to send that packet.", device_info);
107 logger(DEBUG_TRAFFIC, LOG_DEBUG,
108 "Received an invalid packet from %s - packet shorter than an ethernet header).", device_info);
114 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
119 static bool write_packet(vpn_packet_t *packet) {
120 if(vde_send(conn, DATA(packet), packet->len, 0) < 0) {
121 if(errno != EINTR && errno != EAGAIN) {
122 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
132 const devops_t vde_devops = {
133 .setup = setup_device,
134 .close = close_device,
136 .write = write_packet,