2 device.c -- UML network socket
3 Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2002-2004 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: device.c 1374 2004-03-21 14:21:22Z guus $
36 static int listen_fd = -1;
37 static int request_fd = -1;
38 static int data_fd = -1;
39 static int write_fd = -1;
45 extern char *identname;
48 int device_total_in = 0;
49 int device_total_out = 0;
51 enum request_type { REQ_NEW_CONTROL };
53 static struct request {
56 enum request_type type;
57 struct sockaddr_un sock;
60 static struct sockaddr_un data_sun;
62 bool setup_device(void)
64 struct sockaddr_un listen_sun;
65 static const int one = 1;
75 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
76 asprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
78 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
81 device_info = _("UML network socket");
83 if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
84 logger(LOG_ERR, _("Could not open write %s: %s"), device_info, strerror(errno));
89 setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
91 if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
92 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
97 if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
98 logger(LOG_ERR, _("Could not open data %s: %s"), device_info, strerror(errno));
103 setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
105 if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
106 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
113 gettimeofday(&tv, NULL);
114 name.usecs = tv.tv_usec;
115 data_sun.sun_family = AF_UNIX;
116 memcpy(&data_sun.sun_path, &name, sizeof name);
118 if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof data_sun) < 0) {
119 logger(LOG_ERR, _("Could not bind data %s: %s"), device_info, strerror(errno));
124 if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
125 logger(LOG_ERR, _("Could not open %s: %s"), device_info,
130 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
132 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
133 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
137 listen_sun.sun_family = AF_UNIX;
138 strncpy(listen_sun.sun_path, device, sizeof listen_sun.sun_path);
139 if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof listen_sun) < 0) {
140 logger(LOG_ERR, _("Could not bind %s to %s: %s"), device_info, device, strerror(errno));
144 if(listen(listen_fd, 1) < 0) {
145 logger(LOG_ERR, _("Could not listen on %s %s: %s"), device_info, device, strerror(errno));
149 device_fd = listen_fd;
152 logger(LOG_INFO, _("%s is a %s"), device, device_info);
154 if(routing_mode == RMODE_ROUTER)
155 overwrite_mac = true;
160 void close_device(void)
179 bool read_packet(vpn_packet_t *packet)
188 int salen = sizeof sa;
190 request_fd = accept(listen_fd, &sa, &salen);
192 logger(LOG_ERR, _("Could not accept connection to %s %s: %s"), device_info, device, strerror(errno));
196 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
197 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
204 device_fd = request_fd;
211 if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
212 logger(LOG_ERR, _("Error while reading request from %s %s: %s"), device_info,
213 device, strerror(errno));
218 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
219 logger(LOG_ERR, _("Unknown magic %x, version %d, request type %d from %s %s"),
220 request.magic, request.version, request.type, device_info, device);
225 if(connect(write_fd, &request.sock, sizeof request.sock) < 0) {
226 logger(LOG_ERR, _("Could not bind write %s: %s"), device_info, strerror(errno));
231 write(request_fd, &data_sun, sizeof data_sun);
234 logger(LOG_INFO, _("Connection with UML established"));
241 if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
242 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
243 device, strerror(errno));
250 device_total_in += packet->len;
252 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
260 bool write_packet(vpn_packet_t *packet)
265 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Dropping packet of %d bytes to %s: not connected to UML yet"),
266 packet->len, device_info);
270 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
271 packet->len, device_info);
273 if(sendto(write_fd, packet->data, packet->len, 0, &request.sock, sizeof request.sock) < 0) {
274 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
280 device_total_out += packet->len;
285 void dump_device_stats(void)
289 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
290 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
291 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);