]> git.meshlink.io Git - meshlink/blobdiff - src/uml_socket/device.c
Merge branch 'master' into 1.1
[meshlink] / src / uml_socket / device.c
index 08339d80b8ec2cf9d10e21e4d3671df03396a69f..ec9dcb1efa2b7702b1baea71ecf0cb751b0edd39 100644 (file)
@@ -1,7 +1,7 @@
 /*
     device.c -- UML network socket
-    Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
-                  2002-2004 Guus Sliepen <guus@tinc-vpn.org>
+    Copyright (C) 2002-2005 Ivo Timmermans,
+                  2002-2009 Guus Sliepen <guus@tinc-vpn.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include "utils.h"
 #include "route.h"
 
-#include "system.h"
-
 int device_fd = -1;
 static int listen_fd = -1;
 static int request_fd = -1;
 static int data_fd = -1;
 static int write_fd = -1;
 static int state = 0;
-char *device;
-char *iface;
-char *device_info;
+char *device = NULL;
+char *iface = NULL;
+static char *device_info;
 
 extern char *identname;
 extern bool running;
 
-int device_total_in = 0;
-int device_total_out = 0;
+static int device_total_in = 0;
+static int device_total_out = 0;
 
 enum request_type { REQ_NEW_CONTROL };
 
@@ -59,8 +57,7 @@ static struct request {
 
 static struct sockaddr_un data_sun;
 
-bool setup_device(void)
-{
+bool setup_device(void) {
        struct sockaddr_un listen_sun;
        static const int one = 1;
        struct {
@@ -73,10 +70,9 @@ bool setup_device(void)
        cp();
 
        if(!get_config_string(lookup_config(config_tree, "Device"), &device))
-               asprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
+               xasprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
 
-       if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
-               iface = device;
+       get_config_string(lookup_config(config_tree, "Interface"), &iface);
 
        device_info = _("UML network socket");
 
@@ -157,8 +153,7 @@ bool setup_device(void)
        return true;
 }
 
-void close_device(void)
-{
+void close_device(void) {
        cp();
 
        if(listen_fd >= 0)
@@ -174,11 +169,13 @@ void close_device(void)
                close(write_fd);
 
        unlink(device);
+
+       free(device);
+       if(iface) free(iface);
 }
 
-bool read_packet(vpn_packet_t *packet)
-{
-       int lenin;
+bool read_packet(vpn_packet_t *packet) {
+       int inlen;
 
        cp();
 
@@ -208,7 +205,7 @@ bool read_packet(vpn_packet_t *packet)
                }
 
                case 1: {
-                       if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
+                       if((inlen = read(request_fd, &request, sizeof request)) != sizeof request) {
                                logger(LOG_ERR, _("Error while reading request from %s %s: %s"), device_info,
                                           device, strerror(errno));
                                running = false;
@@ -238,14 +235,14 @@ bool read_packet(vpn_packet_t *packet)
                }
 
                case 2: {
-                       if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
+                       if((inlen = read(data_fd, packet->data, MTU)) <= 0) {
                                logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
                                           device, strerror(errno));
                                running = false;
                                return false;
                        }
 
-                       packet->len = lenin;
+                       packet->len = inlen;
 
                        device_total_in += packet->len;
 
@@ -257,23 +254,24 @@ bool read_packet(vpn_packet_t *packet)
        }
 }
 
-bool write_packet(vpn_packet_t *packet)
-{
+bool write_packet(vpn_packet_t *packet) {
        cp();
 
        if(state != 2) {
                ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Dropping packet of %d bytes to %s: not connected to UML yet"),
                                packet->len, device_info);
-               return true;
+               return false;
        }
 
        ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
                           packet->len, device_info);
 
-       if(sendto(write_fd, packet->data, packet->len, 0, &request.sock, sizeof request.sock) < 0) {
-               logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
-                          strerror(errno));
-               running = false;
+       if(write(write_fd, packet->data, packet->len) < 0) {
+               if(errno != EINTR && errno != EAGAIN) {
+                       logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno));
+                       running = false;
+               }
+
                return false;
        }
 
@@ -282,8 +280,7 @@ bool write_packet(vpn_packet_t *packet)
        return true;
 }
 
-void dump_device_stats(void)
-{
+void dump_device_stats(void) {
        cp();
 
        logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);