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