Fix more memory leaks found by ASAN.
[tinc] / src / net_socket.c
index dcf8372..91f0bbb 100644 (file)
@@ -1,7 +1,7 @@
 /*
     net_socket.c -- Handle various kinds of sockets.
     Copyright (C) 1998-2005 Ivo Timmermans,
-                  2000-2017 Guus Sliepen <guus@tinc-vpn.org>
+                  2000-2018 Guus Sliepen <guus@tinc-vpn.org>
                   2006      Scott Lamb <slamb@slamb.org>
                   2009      Florian Forster <octo@verplant.org>
 
@@ -41,6 +41,8 @@ int maxtimeout = 900;
 int seconds_till_retry = 5;
 int udp_rcvbuf = 1024 * 1024;
 int udp_sndbuf = 1024 * 1024;
+bool udp_rcvbuf_warnings;
+bool udp_sndbuf_warnings;
 int max_connection_burst = 10;
 int fwmark;
 
@@ -122,6 +124,7 @@ static bool bind_to_interface(int sd) {
        }
 
 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
+       (void)sd;
        logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "BindToInterface");
 #endif
 
@@ -241,6 +244,35 @@ int setup_listen_socket(const sockaddr_t *sa) {
        return nfd;
 }
 
+static void set_udp_buffer(int nfd, int type, const char *name, int size, bool warnings) {
+       if(!size) {
+               return;
+       }
+
+       if(setsockopt(nfd, SOL_SOCKET, type, (void *)&size, sizeof(size))) {
+               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i: %s", name, size, sockstrerror(sockerrno));
+               return;
+       }
+
+       if(!warnings) {
+               return;
+       }
+
+       // The system may cap the requested buffer size.
+       // Read back the value and check if it is now as requested.
+       int actual = -1;
+       socklen_t optlen = sizeof(actual);
+
+       if(getsockopt(nfd, SOL_SOCKET, type, (void *)&actual, &optlen)) {
+               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: %s", name, sockstrerror(sockerrno));
+       } else if(optlen != sizeof(actual)) {
+               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: unexpected returned optlen %d", name, (int)optlen);
+       } else if(actual < size) {
+               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i, the system set it to %i instead", name, size, actual);
+       }
+}
+
+
 int setup_vpn_in_socket(const sockaddr_t *sa) {
        int nfd;
        char *addrstr;
@@ -284,13 +316,8 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
        setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
        setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
 
-       if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) {
-               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, sockstrerror(sockerrno));
-       }
-
-       if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) {
-               logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, sockstrerror(sockerrno));
-       }
+       set_udp_buffer(nfd, SO_RCVBUF, "SO_RCVBUF", udp_rcvbuf, udp_rcvbuf_warnings);
+       set_udp_buffer(nfd, SO_SNDBUF, "SO_SNDBUF", udp_sndbuf, udp_sndbuf_warnings);
 
 #if defined(IPV6_V6ONLY)
 
@@ -387,7 +414,7 @@ void finish_connecting(connection_t *c) {
        send_id(c);
 }
 
-static void do_outgoing_pipe(connection_t *c, char *command) {
+static void do_outgoing_pipe(connection_t *c, const char *command) {
 #ifndef HAVE_MINGW
        int fd[2];
 
@@ -435,6 +462,8 @@ static void do_outgoing_pipe(connection_t *c, char *command) {
 
        exit(result);
 #else
+       (void)c;
+       (void)command;
        logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type exec not supported on this platform!");
        return;
 #endif
@@ -524,7 +553,7 @@ bool do_outgoing_connection(outgoing_t *outgoing) {
        int result;
 
 begin:
-       sa = get_recent_address(outgoing->address_cache);
+       sa = get_recent_address(outgoing->node->address_cache);
 
        if(!sa) {
                logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->node->name);
@@ -629,6 +658,10 @@ void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) {
 
        node_t *n = outgoing->node;
 
+       if(!n->address_cache) {
+               n->address_cache = open_address_cache(n);
+       }
+
        if(n->connection) {
                logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", n->name);
 
@@ -640,10 +673,6 @@ void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) {
                }
        }
 
-       if(!outgoing->address_cache) {
-               outgoing->address_cache = open_address_cache(n);
-       }
-
        do_outgoing_connection(outgoing);
        return;
 
@@ -784,11 +813,6 @@ void handle_new_unix_connection(void *data, int flags) {
 
 static void free_outgoing(outgoing_t *outgoing) {
        timeout_del(&outgoing->ev);
-
-       if(outgoing->address_cache) {
-               close_address_cache(outgoing->address_cache);
-       }
-
        free(outgoing);
 }
 
@@ -842,6 +866,8 @@ void try_outgoing_connections(void) {
                                node_add(n);
                        }
 
+                       free(name);
+
                        outgoing->node = n;
                        list_insert_tail(outgoing_list, outgoing);
                        setup_outgoing_connection(outgoing, true);