Enable and fix many extra warnings supported by GCC and Clang.
[tinc] / src / uml_device.c
index be60911..f35ae0d 100644 (file)
@@ -1,7 +1,7 @@
 /*
     device.c -- UML network socket
     Copyright (C) 2002-2005 Ivo Timmermans,
-                  2002-2017 Guus Sliepen <guus@tinc-vpn.org>
+                  2002-2022 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
@@ -47,10 +47,14 @@ static struct request {
        struct sockaddr_un sock;
 } request;
 
-static struct sockaddr_un data_sun;
+static struct sockaddr_un data_sun = {
+       .sun_family = AF_UNIX,
+};
 
 static bool setup_device(void) {
-       struct sockaddr_un listen_sun;
+       struct sockaddr_un listen_sun = {
+               .sun_family = AF_UNIX,
+       };
        static const int one = 1;
        struct {
                char zero;
@@ -59,11 +63,11 @@ static bool setup_device(void) {
        } name;
        struct timeval tv;
 
-       if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
+       if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
                xasprintf(&device, RUNSTATEDIR "/%s.umlsocket", identname);
        }
 
-       get_config_string(lookup_config(config_tree, "Interface"), &iface);
+       get_config_string(lookup_config(&config_tree, "Interface"), &iface);
 
        if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
@@ -104,8 +108,7 @@ static bool setup_device(void) {
        name.zero = 0;
        name.pid = getpid();
        gettimeofday(&tv, NULL);
-       name.usecs = tv.tv_usec;
-       data_sun.sun_family = AF_UNIX;
+       name.usecs = (int) tv.tv_usec;
        memcpy(&data_sun.sun_path, &name, sizeof(name));
 
        if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof(data_sun)) < 0) {
@@ -131,9 +134,12 @@ static bool setup_device(void) {
                return false;
        }
 
-       listen_sun.sun_family = AF_UNIX;
+       if(strlen(device) >= sizeof(listen_sun.sun_path)) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "UML socket filename %s is too long!", device);
+               return false;
+       }
+
        strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path));
-       listen_sun.sun_path[sizeof(listen_sun.sun_path) - 1] = 0;
 
        if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
@@ -157,7 +163,7 @@ static bool setup_device(void) {
        return true;
 }
 
-void close_device(void) {
+static void close_device(void) {
        if(listen_fd >= 0) {
                close(listen_fd);
                listen_fd = -1;
@@ -183,16 +189,14 @@ void close_device(void) {
        free(device);
        device = NULL;
 
-       if(iface) {
-               free(iface);
-               iface = NULL;
-       }
+       free(iface);
+       iface = NULL;
 
        device_info = NULL;
 }
 
 static bool read_packet(vpn_packet_t *packet) {
-       int inlen;
+       ssize_t inlen;
 
        switch(state) {
        case 0: {
@@ -245,7 +249,12 @@ static bool read_packet(vpn_packet_t *packet) {
                        return false;
                }
 
-               write(request_fd, &data_sun, sizeof(data_sun));
+               if(write(request_fd, &data_sun, sizeof(data_sun)) != sizeof(data_sun)) {
+                       logger(DEBUG_ALWAYS, LOG_ERR, "Error while responding to request from %s %s: %s", device_info, device, strerror(errno));
+                       event_exit();
+                       return false;
+               }
+
                device_fd = data_fd;
 
                logger(DEBUG_ALWAYS, LOG_INFO, "Connection with UML established");