| 1 |
diff -Naur src/vde/device.c src/vde/device.c
|
| 2 |
--- src/vde/device.c 1969-12-31 19:00:00.000000000 -0500
|
| 3 |
+++ src/vde/device.c 2011-12-21 11:20:34.000000000 -0500
|
| 4 |
@@ -0,0 +1,134 @@
|
| 5 |
+/*
|
| 6 |
+ device.c -- VDE plug
|
| 7 |
+ Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
|
| 8 |
+
|
| 9 |
+ This program is free software; you can redistribute it and/or modify
|
| 10 |
+ it under the terms of the GNU General Public License as published by
|
| 11 |
+ the Free Software Foundation; either version 2 of the License, or
|
| 12 |
+ (at your option) any later version.
|
| 13 |
+
|
| 14 |
+ This program is distributed in the hope that it will be useful,
|
| 15 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 |
+ GNU General Public License for more details.
|
| 18 |
+
|
| 19 |
+ You should have received a copy of the GNU General Public License along
|
| 20 |
+ with this program; if not, write to the Free Software Foundation, Inc.,
|
| 21 |
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
| 22 |
+*/
|
| 23 |
+
|
| 24 |
+#include "system.h"
|
| 25 |
+
|
| 26 |
+#include <libvdeplug_dyn.h>
|
| 27 |
+
|
| 28 |
+#include "conf.h"
|
| 29 |
+#include "device.h"
|
| 30 |
+#include "net.h"
|
| 31 |
+#include "logger.h"
|
| 32 |
+#include "utils.h"
|
| 33 |
+#include "route.h"
|
| 34 |
+#include "xalloc.h"
|
| 35 |
+
|
| 36 |
+int device_fd = -1;
|
| 37 |
+static struct vdepluglib plug;
|
| 38 |
+static struct vdeconn *conn = NULL;
|
| 39 |
+static int port = 0;
|
| 40 |
+static char *group = NULL;
|
| 41 |
+char *device = NULL;
|
| 42 |
+char *iface = NULL;
|
| 43 |
+static char *device_info;
|
| 44 |
+
|
| 45 |
+extern char *identname;
|
| 46 |
+extern bool running;
|
| 47 |
+
|
| 48 |
+static uint64_t device_total_in = 0;
|
| 49 |
+static uint64_t device_total_out = 0;
|
| 50 |
+
|
| 51 |
+bool setup_device(void) {
|
| 52 |
+ libvdeplug_dynopen(plug);
|
| 53 |
+
|
| 54 |
+ if(!plug.dl_handle) {
|
| 55 |
+ logger(LOG_ERR, "Could not open libvdeplug library!");
|
| 56 |
+ return false;
|
| 57 |
+ }
|
| 58 |
+
|
| 59 |
+ if(!get_config_string(lookup_config(config_tree, "Device"), &device))
|
| 60 |
+ xasprintf(&device, LOCALSTATEDIR "/run/vde.ctl");
|
| 61 |
+
|
| 62 |
+ get_config_string(lookup_config(config_tree, "Interface"), &iface);
|
| 63 |
+
|
| 64 |
+ get_config_int(lookup_config(config_tree, "VDEPort"), &port);
|
| 65 |
+
|
| 66 |
+ get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
|
| 67 |
+
|
| 68 |
+ device_info = "VDE socket";
|
| 69 |
+
|
| 70 |
+ struct vde_open_args args = {
|
| 71 |
+ .port = port,
|
| 72 |
+ .group = group,
|
| 73 |
+ .mode = 0700,
|
| 74 |
+ };
|
| 75 |
+
|
| 76 |
+ conn = plug.vde_open(device, identname, &args);
|
| 77 |
+ if(!conn) {
|
| 78 |
+ logger(LOG_ERR, "Could not open VDE socket %s", device);
|
| 79 |
+ return false;
|
| 80 |
+ }
|
| 81 |
+
|
| 82 |
+ device_fd = plug.vde_datafd(conn);
|
| 83 |
+
|
| 84 |
+ logger(LOG_INFO, "%s is a %s", device, device_info);
|
| 85 |
+
|
| 86 |
+ if(routing_mode == RMODE_ROUTER)
|
| 87 |
+ overwrite_mac = true;
|
| 88 |
+
|
| 89 |
+ return true;
|
| 90 |
+}
|
| 91 |
+
|
| 92 |
+void close_device(void) {
|
| 93 |
+ if(conn)
|
| 94 |
+ plug.vde_close(conn);
|
| 95 |
+
|
| 96 |
+ if(plug.dl_handle)
|
| 97 |
+ libvdeplug_dynclose(plug);
|
| 98 |
+
|
| 99 |
+ free(device);
|
| 100 |
+
|
| 101 |
+ free(iface);
|
| 102 |
+}
|
| 103 |
+
|
| 104 |
+bool read_packet(vpn_packet_t *packet) {
|
| 105 |
+ int lenin = plug.vde_recv(conn, packet->data, MTU, 0);
|
| 106 |
+ if(lenin <= 0) {
|
| 107 |
+ logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
|
| 108 |
+ running = false;
|
| 109 |
+ return false;
|
| 110 |
+ }
|
| 111 |
+
|
| 112 |
+ packet->len = lenin;
|
| 113 |
+ device_total_in += packet->len;
|
| 114 |
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
|
| 115 |
+
|
| 116 |
+ return true;
|
| 117 |
+}
|
| 118 |
+
|
| 119 |
+bool write_packet(vpn_packet_t *packet) {
|
| 120 |
+ if(plug.vde_send(conn, packet->data, packet->len, 0) < 0) {
|
| 121 |
+ if(errno != EINTR && errno != EAGAIN) {
|
| 122 |
+ logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
|
| 123 |
+ running = false;
|
| 124 |
+ }
|
| 125 |
+
|
| 126 |
+ return false;
|
| 127 |
+ }
|
| 128 |
+
|
| 129 |
+ device_total_out += packet->len;
|
| 130 |
+
|
| 131 |
+ return true;
|
| 132 |
+}
|
| 133 |
+
|
| 134 |
+void dump_device_stats(void) {
|
| 135 |
+ logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
|
| 136 |
+ logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
|
| 137 |
+ logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
|
| 138 |
+}
|