Const correctness.
[tinc] / src / vde_device.c
1 /*
2     device.c -- VDE plug
3     Copyright (C) 2013 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 "names.h"
27 #include "net.h"
28 #include "logger.h"
29 #include "utils.h"
30 #include "route.h"
31 #include "xalloc.h"
32
33 static struct vdepluglib plug;
34 static struct vdeconn *conn = NULL;
35 static int port = 0;
36 static char *group = NULL;
37 static const char *device_info = "VDE socket";
38
39 static bool setup_device(void) {
40         libvdeplug_dynopen(plug);
41
42         if(!plug.dl_handle) {
43                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open libvdeplug library!");
44                 return false;
45         }
46
47         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
48                 xasprintf(&device, RUNSTATEDIR "/vde.ctl");
49         }
50
51         get_config_string(lookup_config(config_tree, "Interface"), &iface);
52
53         get_config_int(lookup_config(config_tree, "VDEPort"), &port);
54
55         get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
56
57         struct vde_open_args args = {
58                 .port = port,
59                 .group = group,
60                 .mode = 0700,
61         };
62
63         conn = plug.vde_open(device, identname, &args);
64
65         if(!conn) {
66                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open VDE socket %s", device);
67                 return false;
68         }
69
70         device_fd = plug.vde_datafd(conn);
71
72 #ifdef FD_CLOEXEC
73         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
74 #endif
75
76         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
77
78         if(routing_mode == RMODE_ROUTER) {
79                 overwrite_mac = true;
80         }
81
82         return true;
83 }
84
85 static void close_device(void) {
86         if(conn) {
87                 plug.vde_close(conn);
88                 conn = NULL;
89         }
90
91         if(plug.dl_handle) {
92                 libvdeplug_dynclose(plug);
93         }
94
95         free(device);
96         device = NULL;
97
98         free(iface);
99         iface = NULL;
100
101         device_info = NULL;
102 }
103
104 static bool read_packet(vpn_packet_t *packet) {
105         int lenin = (ssize_t)plug.vde_recv(conn, DATA(packet), MTU, 0);
106
107         if(lenin <= 0) {
108                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
109                 event_exit();
110                 return false;
111         }
112
113         packet->len = lenin;
114
115         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
116
117         return true;
118 }
119
120 static bool write_packet(vpn_packet_t *packet) {
121         if((ssize_t)plug.vde_send(conn, DATA(packet), packet->len, 0) < 0) {
122                 if(errno != EINTR && errno != EAGAIN) {
123                         logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
124                         event_exit();
125                 }
126
127                 return false;
128         }
129
130         return true;
131 }
132
133 const devops_t vde_devops = {
134         .setup = setup_device,
135         .close = close_device,
136         .read = read_packet,
137         .write = write_packet,
138 };