6148ccbe2f3807d0cdd4eeda5dc77966652c4116
[tinc] / src / vde / device.c
1 /*
2     device.c -- VDE plug
3     Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include <libvdeplug_dyn.h>
23
24 #include "conf.h"
25 #include "net.h"
26 #include "logger.h"
27 #include "utils.h"
28 #include "route.h"
29 #include "xalloc.h"
30
31 int device_fd = -1;
32 static struct vdepluglib plug;
33 static struct vdeconn *conn = NULL;
34 static int port = 0;
35 static char *group = NULL;
36 char *device = NULL;
37 char *iface = NULL;
38 static char *device_info;
39
40 extern char *identname;
41 extern bool running;
42
43 static uint64_t device_total_in = 0;
44 static uint64_t device_total_out = 0;
45
46 bool setup_device(void) {
47         libvdeplug_dynopen(plug);
48
49         if(!plug.dl_handle) {
50                 logger(LOG_ERR, "Could not open libvdeplug library!");
51                 return false;
52         }
53
54         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
55                 xasprintf(&device, LOCALSTATEDIR "/run/vde.ctl");
56
57         get_config_string(lookup_config(config_tree, "Interface"), &iface);
58
59         get_config_int(lookup_config(config_tree, "VDEPort"), &port);
60
61         get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
62
63         device_info = "VDE socket";
64
65         struct vde_open_args args = {
66                 .port = port,
67                 .group = group,
68                 .mode = 0700,
69         };
70
71         conn = plug.vde_open(device, identname, &args);
72         if(!conn) {
73                 logger(LOG_ERR, "Could not open VDE socket %s", device);
74                 return false;
75         }
76
77         device_fd = plug.vde_datafd(conn);
78
79         logger(LOG_INFO, "%s is a %s", device, device_info);
80
81         if(routing_mode == RMODE_ROUTER)
82                 overwrite_mac = true;
83
84         return true;
85 }
86
87 void close_device(void) {
88         if(conn)
89                 plug.vde_close(conn);
90
91         if(plug.dl_handle)
92                 libvdeplug_dynclose(plug);
93
94         free(device);
95
96         free(iface);
97 }
98
99 bool read_packet(vpn_packet_t *packet) {
100         int lenin = plug.vde_recv(conn, packet->data, MTU, 0);
101         if(lenin <= 0) {
102                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
103                 running = false;
104                 return false;
105         }
106
107         packet->len = lenin;
108         device_total_in += packet->len;
109         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
110
111         return true;
112 }
113
114 bool write_packet(vpn_packet_t *packet) {
115         if(plug.vde_send(conn, packet->data, packet->len, 0) < 0) {
116                 if(errno != EINTR && errno != EAGAIN) {
117                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
118                         running = false;
119                 }
120
121                 return false;
122         }
123
124         device_total_out += packet->len;
125
126         return true;
127 }
128
129 void dump_device_stats(void) {
130         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
131         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
132         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
133 }