From c87a77b5fd2a0378f2b992a5d579a80ee4033cec Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 12:50:30 +0200 Subject: [PATCH 1/9] Ensure tests compile on *BSD. --- test/pong.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/pong.c b/test/pong.c index 65a9075c..6e212bf2 100644 --- a/test/pong.c +++ b/test/pong.c @@ -1,6 +1,6 @@ /* pong.c -- ICMP echo reply generator - Copyright (C) 2013 Guus Sliepen + Copyright (C) 2013-2017 Guus Sliepen 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 @@ -19,6 +19,8 @@ #include "../src/system.h" +#include "../src/ethernet.h" + uint8_t mymac[6] = {6, 5, 5, 6, 5, 5}; static ssize_t do_arp(uint8_t *buf, ssize_t len, struct sockaddr_in *in) { From 2b4c0c63628ff9b432ec5d4b4c7b7ab2d4b02fb2 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 13:02:39 +0200 Subject: [PATCH 2/9] Make sure realname is always initialized. --- src/bsd/device.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bsd/device.c b/src/bsd/device.c index 54282626..fbc75937 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -198,18 +198,19 @@ static bool setup_device(void) { // Guess what the corresponding interface is called - char *realname; + char *realname = NULL; #if defined(HAVE_FDEVNAME) - realname = fdevname(device_fd) ? : device; + realname = fdevname(device_fd); #elif defined(HAVE_DEVNAME) struct stat buf; if(!fstat(device_fd, &buf)) - realname = devname(buf.st_rdev, S_IFCHR) ? : device; -#else - realname = device; + realname = devname(buf.st_rdev, S_IFCHR); #endif + if(!realname) + realname = device; + if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname); else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) From 1be0c284c7c8d34c2dd6c2160ce49aeae468e867 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 13:07:15 +0200 Subject: [PATCH 3/9] Fix compiler warnings on *BSD. --- src/net.c | 2 +- src/openssl/cipher.c | 4 ++-- test/pong.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/net.c b/src/net.c index 4369ff49..8cb7ed73 100644 --- a/src/net.c +++ b/src/net.c @@ -209,7 +209,7 @@ static void timeout_handler(void *data) { // timeout during ping if(c->status.pinged) { - logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now.tv_sec - c->last_ping_time); + logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)(now.tv_sec - c->last_ping_time)); terminate_connection(c, c->edge); continue; } diff --git a/src/openssl/cipher.c b/src/openssl/cipher.c index e362325a..a8032ea0 100644 --- a/src/openssl/cipher.c +++ b/src/openssl/cipher.c @@ -137,7 +137,7 @@ bool cipher_encrypt(cipher_t *cipher, const void *indata, size_t inlen, void *ou int len, pad; if(EVP_EncryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL) && EVP_EncryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen) - && EVP_EncryptFinal(cipher->ctx, (unsigned char *)outdata + len, &pad)) { + && EVP_EncryptFinal_ex(cipher->ctx, (unsigned char *)outdata + len, &pad)) { if(outlen) *outlen = len + pad; return true; } @@ -158,7 +158,7 @@ bool cipher_decrypt(cipher_t *cipher, const void *indata, size_t inlen, void *ou int len, pad; if(EVP_DecryptInit_ex(cipher->ctx, NULL, NULL, NULL, NULL) && EVP_DecryptUpdate(cipher->ctx, (unsigned char *)outdata, &len, indata, inlen) - && EVP_DecryptFinal(cipher->ctx, (unsigned char *)outdata + len, &pad)) { + && EVP_DecryptFinal_ex(cipher->ctx, (unsigned char *)outdata + len, &pad)) { if(outlen) *outlen = len + pad; return true; } diff --git a/test/pong.c b/test/pong.c index 6e212bf2..a4f2be73 100644 --- a/test/pong.c +++ b/test/pong.c @@ -154,7 +154,7 @@ int main(int argc, char *argv[]) { #endif default: - fprintf(stderr, "Multicast for address family %hx unsupported\n", ai->ai_family); + fprintf(stderr, "Multicast for address family %x unsupported\n", ai->ai_family); return 1; } From db80dbbac93ce3c714247e0af2147f5e1474a135 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 13:53:48 +0200 Subject: [PATCH 4/9] Fix segfault when adding environment variables. --- src/script.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/script.c b/src/script.c index 1ce59ea0..d65551a4 100644 --- a/src/script.c +++ b/src/script.c @@ -64,7 +64,7 @@ static void putenv(const char *p) {} static void unputenv(const char *p) {} #endif -static const int min_env_size; +static const int min_env_size = 10; int environment_add(environment_t *env, const char *format, ...) { if(env->n >= env->size) { @@ -95,7 +95,7 @@ void environment_update(environment_t *env, int pos, const char *format, ...) { void environment_init(environment_t *env) { env->n = 0; env->size = min_env_size; - env->entries = 0; //xzalloc(env->size * sizeof *env->entries); + env->entries = xzalloc(env->size * sizeof *env->entries); if(netname) environment_add(env, "NETNAME=%s", netname); From a14414731925cd59e64b3a90309b5a9ec60ac690 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 13:54:02 +0200 Subject: [PATCH 5/9] Fix tests on *BSD. --- src/ifconfig.c | 45 +++++++++++++++++++++---------------------- test/executables.test | 4 +++- test/sptps-basic.test | 4 ++++ test/testlib.sh | 16 ++++----------- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/ifconfig.c b/src/ifconfig.c index 7688a3c6..06f2c629 100644 --- a/src/ifconfig.c +++ b/src/ifconfig.c @@ -104,13 +104,6 @@ void ifconfig_address(FILE *out, const char *value) { case SUBNET_IPV6: fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str); break; default: return; } -#elif defined(HAVE_BSD) - switch(address.type) { - case SUBNET_MAC: fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", address_str); break; - case SUBNET_IPV4: fprintf(out, "ifconfig \"$INTERFACE\" %s\n", address_str); break; - case SUBNET_IPV6: fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", address_str); break; - default: return; - } #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN) switch(address.type) { case SUBNET_MAC: fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str); break; @@ -118,6 +111,13 @@ void ifconfig_address(FILE *out, const char *value) { case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 set address \"$INTERFACE\" static %s\n", address_str); break; default: return; } +#else // assume BSD + switch(address.type) { + case SUBNET_MAC: fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", address_str); break; + case SUBNET_IPV4: fprintf(out, "ifconfig \"$INTERFACE\" %s\n", address_str); break; + case SUBNET_IPV6: fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", address_str); break; + default: return; + } #endif } @@ -152,8 +152,21 @@ void ifconfig_route(FILE *out, const char *value) { default: return; } } -#elif defined(HAVE_BSD) - // BSD route command is silly and doesn't accept an interface name as a destination. +#elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN) + if(*gateway_str) { + switch(subnet.type) { + case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break; + case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break; + default: return; + } + } else { + switch(subnet.type) { + case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\"\n", subnet_str); break; + case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\"\n", subnet_str); break; + default: return; + } + } +#else // assume BSD if(!*gateway_str) { switch(subnet.type) { case SUBNET_IPV4: @@ -180,19 +193,5 @@ void ifconfig_route(FILE *out, const char *value) { case SUBNET_IPV6: fprintf(out, "route add -inet6 %s %s\n", subnet_str, gateway_str); break; default: return; } -#elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN) - if(*gateway_str) { - switch(subnet.type) { - case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break; - case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break; - default: return; - } - } else { - switch(subnet.type) { - case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\"\n", subnet_str); break; - case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\"\n", subnet_str); break; - default: return; - } - } #endif } diff --git a/test/executables.test b/test/executables.test index 35dd2bc8..801de58c 100755 --- a/test/executables.test +++ b/test/executables.test @@ -5,4 +5,6 @@ # Just test whether the executables work $tincd --help $tinc --help -$sptps_test --help +if [ -e $sptps_test ]; then + $sptps_test --help +fi diff --git a/test/sptps-basic.test b/test/sptps-basic.test index 644a31e0..28c0c404 100755 --- a/test/sptps-basic.test +++ b/test/sptps-basic.test @@ -2,6 +2,10 @@ . ./testlib.sh +# Skip this test if we did not compile sptps_test + +test -e $sptps_test || exit 77 + # Generate keys mkdir -p $d1 diff --git a/test/testlib.sh b/test/testlib.sh index 31033352..75b60a79 100644 --- a/test/testlib.sh +++ b/test/testlib.sh @@ -9,18 +9,10 @@ sptps_keypair=../src/sptps_keypair # Test directories -case "$_" in - /*) - d1=$_.1 - d2=$_.2 - d3=$_.3 - ;; - *) - d1=$PWD/$_.1 - d2=$PWD/$_.2 - d3=$PWD/$_.3 - ;; -esac +scriptname=`basename $0` +d1=$PWD/$scriptname.1 +d2=$PWD/$scriptname.2 +d3=$PWD/$scriptname.3 # Default arguments for both tinc and tincd From 70fed5f7ffdeb0416ee6b77881098faab9a7cd47 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 17 Apr 2017 16:05:30 +0200 Subject: [PATCH 6/9] Add missing tinc stop command to the scripts test. --- test/scripts.test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/scripts.test b/test/scripts.test index 6fe616f8..3b3f2749 100755 --- a/test/scripts.test +++ b/test/scripts.test @@ -71,6 +71,8 @@ echo bar-started >>$OUT # Stop server node $tinc $c1 stop +sleep 1 +$tinc $c2 stop # Check if the script output is what is expected From be8e5cbd1cfcd198f975542e52085abdd543ec80 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 18 Apr 2017 20:07:33 +0200 Subject: [PATCH 7/9] Remove dead stores. Found by the Clang static analyzer. --- src/ed25519/sc.c | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/ed25519/sc.c b/src/ed25519/sc.c index 42cfc2da..3364de42 100644 --- a/src/ed25519/sc.c +++ b/src/ed25519/sc.c @@ -81,42 +81,36 @@ void sc_reduce(unsigned char *s) { s14 -= s23 * 997805; s15 += s23 * 136657; s16 -= s23 * 683901; - s23 = 0; s10 += s22 * 666643; s11 += s22 * 470296; s12 += s22 * 654183; s13 -= s22 * 997805; s14 += s22 * 136657; s15 -= s22 * 683901; - s22 = 0; s9 += s21 * 666643; s10 += s21 * 470296; s11 += s21 * 654183; s12 -= s21 * 997805; s13 += s21 * 136657; s14 -= s21 * 683901; - s21 = 0; s8 += s20 * 666643; s9 += s20 * 470296; s10 += s20 * 654183; s11 -= s20 * 997805; s12 += s20 * 136657; s13 -= s20 * 683901; - s20 = 0; s7 += s19 * 666643; s8 += s19 * 470296; s9 += s19 * 654183; s10 -= s19 * 997805; s11 += s19 * 136657; s12 -= s19 * 683901; - s19 = 0; s6 += s18 * 666643; s7 += s18 * 470296; s8 += s18 * 654183; s9 -= s18 * 997805; s10 += s18 * 136657; s11 -= s18 * 683901; - s18 = 0; carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= shl64(carry6, 21); @@ -156,35 +150,30 @@ void sc_reduce(unsigned char *s) { s8 -= s17 * 997805; s9 += s17 * 136657; s10 -= s17 * 683901; - s17 = 0; s4 += s16 * 666643; s5 += s16 * 470296; s6 += s16 * 654183; s7 -= s16 * 997805; s8 += s16 * 136657; s9 -= s16 * 683901; - s16 = 0; s3 += s15 * 666643; s4 += s15 * 470296; s5 += s15 * 654183; s6 -= s15 * 997805; s7 += s15 * 136657; s8 -= s15 * 683901; - s15 = 0; s2 += s14 * 666643; s3 += s14 * 470296; s4 += s14 * 654183; s5 -= s14 * 997805; s6 += s14 * 136657; s7 -= s14 * 683901; - s14 = 0; s1 += s13 * 666643; s2 += s13 * 470296; s3 += s13 * 654183; s4 -= s13 * 997805; s5 += s13 * 136657; s6 -= s13 * 683901; - s13 = 0; s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -277,7 +266,6 @@ void sc_reduce(unsigned char *s) { s3 -= s12 * 997805; s4 += s12 * 136657; s5 -= s12 * 683901; - s12 = 0; carry0 = s0 >> 21; s1 += carry0; s0 -= shl64(carry0, 21); @@ -543,42 +531,36 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s14 -= s23 * 997805; s15 += s23 * 136657; s16 -= s23 * 683901; - s23 = 0; s10 += s22 * 666643; s11 += s22 * 470296; s12 += s22 * 654183; s13 -= s22 * 997805; s14 += s22 * 136657; s15 -= s22 * 683901; - s22 = 0; s9 += s21 * 666643; s10 += s21 * 470296; s11 += s21 * 654183; s12 -= s21 * 997805; s13 += s21 * 136657; s14 -= s21 * 683901; - s21 = 0; s8 += s20 * 666643; s9 += s20 * 470296; s10 += s20 * 654183; s11 -= s20 * 997805; s12 += s20 * 136657; s13 -= s20 * 683901; - s20 = 0; s7 += s19 * 666643; s8 += s19 * 470296; s9 += s19 * 654183; s10 -= s19 * 997805; s11 += s19 * 136657; s12 -= s19 * 683901; - s19 = 0; s6 += s18 * 666643; s7 += s18 * 470296; s8 += s18 * 654183; s9 -= s18 * 997805; s10 += s18 * 136657; s11 -= s18 * 683901; - s18 = 0; carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= shl64(carry6, 21); @@ -618,35 +600,30 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s8 -= s17 * 997805; s9 += s17 * 136657; s10 -= s17 * 683901; - s17 = 0; s4 += s16 * 666643; s5 += s16 * 470296; s6 += s16 * 654183; s7 -= s16 * 997805; s8 += s16 * 136657; s9 -= s16 * 683901; - s16 = 0; s3 += s15 * 666643; s4 += s15 * 470296; s5 += s15 * 654183; s6 -= s15 * 997805; s7 += s15 * 136657; s8 -= s15 * 683901; - s15 = 0; s2 += s14 * 666643; s3 += s14 * 470296; s4 += s14 * 654183; s5 -= s14 * 997805; s6 += s14 * 136657; s7 -= s14 * 683901; - s14 = 0; s1 += s13 * 666643; s2 += s13 * 470296; s3 += s13 * 654183; s4 -= s13 * 997805; s5 += s13 * 136657; s6 -= s13 * 683901; - s13 = 0; s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -739,7 +716,6 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s3 -= s12 * 997805; s4 += s12 * 136657; s5 -= s12 * 683901; - s12 = 0; carry0 = s0 >> 21; s1 += carry0; s0 -= shl64(carry0, 21); From 060ab1cd7cdf750a0477f2a8b6193d28849877e8 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 18 Apr 2017 20:09:08 +0200 Subject: [PATCH 8/9] Add field widths to sscanf() calls. Found by cppcheck. --- src/info.c | 10 +++++----- src/invitation.c | 4 ++-- src/tincctl.c | 16 ++++++++-------- src/top.c | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/info.c b/src/info.c index b9a6fcf9..09ca390e 100644 --- a/src/info.c +++ b/src/info.c @@ -69,7 +69,7 @@ static int info_node(int fd, const char *item) { long int last_state_change; while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s %s %s port %s %d %d %d %d %x %"PRIx32" %s %s %d %hd %hd %hd %ld", &code, &req, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_union.raw, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); + int n = sscanf(line, "%d %d %4095s %4095s %4095s port %4095s %d %d %d %d %x %"PRIx32" %4095s %4095s %d %hd %hd %hd %ld", &code, &req, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_union.raw, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); if(n == 2) break; @@ -91,7 +91,7 @@ static int info_node(int fd, const char *item) { } while(recvline(fd, line, sizeof line)) { - if(sscanf(line, "%d %d %s", &code, &req, node) == 2) + if(sscanf(line, "%d %d %4095s", &code, &req, node) == 2) break; } @@ -158,7 +158,7 @@ static int info_node(int fd, const char *item) { printf("Edges: "); sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s %s", &code, &req, from, to); + int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, from, to); if(n == 2) break; if(n != 4) { @@ -174,7 +174,7 @@ static int info_node(int fd, const char *item) { printf("Subnets: "); sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s %s", &code, &req, subnet, from); + int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, subnet, from); if(n == 2) break; if(n != 4) { @@ -209,7 +209,7 @@ static int info_subnet(int fd, const char *item) { sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s %s", &code, &req, netstr, owner); + int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, netstr, owner); if(n == 2) break; diff --git a/src/invitation.c b/src/invitation.c index ff93f9ee..9a082e3f 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -259,7 +259,7 @@ int cmd_invite(int argc, char *argv[]) { while(recvline(fd, line, sizeof line)) { char node[4096]; int code, req; - if(sscanf(line, "%d %d %s", &code, &req, node) != 3) + if(sscanf(line, "%d %d %4095s", &code, &req, node) != 3) break; if(!strcmp(node, argv[1])) found = true; @@ -1044,7 +1044,7 @@ next: char hisname[4096] = ""; int code, hismajor, hisminor = 0; - if(!recvline(sock, line, sizeof line) || sscanf(line, "%d %s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(sock, line, sizeof line) || !rstrip(line) || sscanf(line, "%d ", &code) != 1 || code != ACK || strlen(line) < 3) { + if(!recvline(sock, line, sizeof line) || sscanf(line, "%d %4095s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(sock, line, sizeof line) || !rstrip(line) || sscanf(line, "%d ", &code) != 1 || code != ACK || strlen(line) < 3) { fprintf(stderr, "Cannot read greeting from peer\n"); closesocket(sock); goto next; diff --git a/src/tincctl.c b/src/tincctl.c index 2db9f235..6f4fb930 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -803,7 +803,7 @@ bool connect_tincd(bool verbose) { char data[4096]; int version; - if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) { + if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %4095s %d", &code, data, &version) != 3 || code != 0) { if(verbose) fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno)); close(fd); @@ -1119,7 +1119,7 @@ static int cmd_dump(int argc, char *argv[]) { while(recvline(fd, line, sizeof line)) { char node1[4096], node2[4096]; - int n = sscanf(line, "%d %d %s %s", &code, &req, node1, node2); + int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, node1, node2); if(n == 2) { if(do_graph && req == REQ_DUMP_NODES) continue; @@ -1151,7 +1151,7 @@ static int cmd_dump(int argc, char *argv[]) { switch(req) { case REQ_DUMP_NODES: { - int n = sscanf(line, "%*d %*d %s %s %s port %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_int, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); + int n = sscanf(line, "%*d %*d %4095s %4095s %4095s port %4095s %d %d %d %d %x %x %4095s %4095s %d %hd %hd %hd %ld", node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_int, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); if(n != 17) { fprintf(stderr, "Unable to parse node dump from tincd: %s\n", line); return 1; @@ -1181,7 +1181,7 @@ static int cmd_dump(int argc, char *argv[]) { } break; case REQ_DUMP_EDGES: { - int n = sscanf(line, "%*d %*d %s %s %s port %s %s port %s %x %d", from, to, host, port, local_host, local_port, &options, &weight); + int n = sscanf(line, "%*d %*d %4095s %4095s %4095s port %4095s %4095s port %4095s %x %d", from, to, host, port, local_host, local_port, &options, &weight); if(n != 8) { fprintf(stderr, "Unable to parse edge dump from tincd.\n"); return 1; @@ -1199,7 +1199,7 @@ static int cmd_dump(int argc, char *argv[]) { } break; case REQ_DUMP_SUBNETS: { - int n = sscanf(line, "%*d %*d %s %s", subnet, node); + int n = sscanf(line, "%*d %*d %4095s %4095s", subnet, node); if(n != 2) { fprintf(stderr, "Unable to parse subnet dump from tincd.\n"); return 1; @@ -1208,7 +1208,7 @@ static int cmd_dump(int argc, char *argv[]) { } break; case REQ_DUMP_CONNECTIONS: { - int n = sscanf(line, "%*d %*d %s %s port %s %x %d %x", node, host, port, &options, &socket, &status_int); + int n = sscanf(line, "%*d %*d %4095s %4095s port %4095s %x %d %x", node, host, port, &options, &socket, &status_int); if(n != 6) { fprintf(stderr, "Unable to parse connection dump from tincd.\n"); return 1; @@ -2233,7 +2233,7 @@ static int cmd_import(int argc, char *argv[]) { bool firstline = true; while(fgets(buf, sizeof buf, in)) { - if(sscanf(buf, "Name = %s", name) == 1) { + if(sscanf(buf, "Name = %4095s", name) == 1) { firstline = false; if(!check_id(name)) { @@ -2725,7 +2725,7 @@ static char *complete_info(const char *text, int state) { while(recvline(fd, line, sizeof line)) { char item[4096]; - int n = sscanf(line, "%d %d %s", &code, &req, item); + int n = sscanf(line, "%d %d %4095s", &code, &req, item); if(n == 2) { i++; if(i >= 2) diff --git a/src/top.c b/src/top.c index 40b80479..e517b4f9 100644 --- a/src/top.c +++ b/src/top.c @@ -90,7 +90,7 @@ static bool update(int fd) { ns->known = false; while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes); + int n = sscanf(line, "%d %d %4095s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes); if(n == 2) return true; From 18646deca120f0ccc3bfad643dba83547ecc2f20 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 18 Apr 2017 20:09:38 +0200 Subject: [PATCH 9/9] Fix some minor issues found by cppcheck. --- src/invitation.c | 4 ++-- src/protocol_key.c | 4 ++-- src/route.c | 4 ++-- src/tincctl.c | 9 +++------ src/top.c | 33 ++++++++++++++++++++------------- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/invitation.c b/src/invitation.c index 9a082e3f..ac410b81 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -252,8 +252,8 @@ int cmd_invite(int argc, char *argv[]) { } // If a daemon is running, ensure no other nodes know about this name - bool found = false; if(connect_tincd(false)) { + bool found = false; sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES); while(recvline(fd, line, sizeof line)) { @@ -686,7 +686,7 @@ make_names: } // Copy the safe variable to the right config file - fprintf(variables[i].type & VAR_HOST ? fh : f, "%s = %s\n", l, value); + fprintf((variables[i].type & VAR_HOST) ? fh : f, "%s = %s\n", l, value); } fclose(f); diff --git a/src/protocol_key.c b/src/protocol_key.c index d24d4aca..e1bb3b9b 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -356,7 +356,7 @@ bool ans_key_h(connection_t *c, const char *request) { char key[MAX_STRING_SIZE]; char address[MAX_STRING_SIZE] = ""; char port[MAX_STRING_SIZE] = ""; - int cipher, digest, maclength, compression, keylen; + int cipher, digest, maclength, compression; node_t *from, *to; if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING, @@ -489,7 +489,7 @@ bool ans_key_h(connection_t *c, const char *request) { /* Process key */ - keylen = hex2bin(key, key, sizeof key); + int keylen = hex2bin(key, key, sizeof key); if(keylen != (from->outcipher ? cipher_keylength(from->outcipher) : 1)) { logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname); diff --git a/src/route.c b/src/route.c index c10fddb5..7ae49607 100644 --- a/src/route.c +++ b/src/route.c @@ -510,7 +510,7 @@ static void route_broadcast(node_t *source, vpn_packet_t *packet) { static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) { struct ip ip; vpn_packet_t fragment; - int len, maxlen, todo; + int maxlen, todo; uint8_t *offset; uint16_t ip_off, origf; @@ -537,7 +537,7 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et ip_off &= IP_OFFMASK; while(todo) { - len = todo > maxlen ? maxlen : todo; + int len = todo > maxlen ? maxlen : todo; memcpy(DATA(&fragment) + ether_size + ip_size, offset, len); todo -= len; offset += len; diff --git a/src/tincctl.c b/src/tincctl.c index 6f4fb930..6416ebe0 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -513,7 +513,7 @@ bool recvline(int fd, char *line, size_t len) { char *newline = NULL; if(!fd) - abort(); + return false; while(!(newline = memchr(buffer, '\n', blen))) { int result = recv(fd, buffer + blen, sizeof buffer - blen, 0); @@ -951,11 +951,11 @@ static int cmd_stop(int argc, char *argv[]) { if(!connect_tincd(true)) { if(pid) { if(kill(pid, SIGTERM)) { - fprintf(stderr, "Could not send TERM signal to process with PID %u: %s\n", pid, strerror(errno)); + fprintf(stderr, "Could not send TERM signal to process with PID %d: %s\n", pid, strerror(errno)); return 1; } - fprintf(stderr, "Sent TERM signal to process with PID %u.\n", pid); + fprintf(stderr, "Sent TERM signal to process with PID %d.\n", pid); waitpid(pid, NULL, 0); return 0; } @@ -1030,7 +1030,6 @@ static int dump_invitations(void) { FILE *f = fopen(fname, "r"); if(!f) { fprintf(stderr, "Cannot open %s: %s\n", fname, strerror(errno)); - fclose(f); continue; } @@ -2826,8 +2825,6 @@ static int cmd_shell(int argc, char *argv[]) { while(p && *p) { if(nargc >= maxargs) { - fprintf(stderr, "next %p '%s', p %p '%s'\n", next, next, p, p); - abort(); maxargs *= 2; nargv = xrealloc(nargv, maxargs * sizeof *nargv); } diff --git a/src/top.c b/src/top.c index e517b4f9..fe6f384e 100644 --- a/src/top.c +++ b/src/top.c @@ -158,40 +158,47 @@ static int cmpu64(uint64_t a, uint64_t b) { static int sortfunc(const void *a, const void *b) { const nodestats_t *na = *(const nodestats_t **)a; const nodestats_t *nb = *(const nodestats_t **)b; + int result; + switch(sortmode) { case 1: if(cumulative) - return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i; + result = -cmpu64(na->in_packets, nb->in_packets); else - return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i; + result = -cmpfloat(na->in_packets_rate, nb->in_packets_rate); case 2: if(cumulative) - return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i; + result = -cmpu64(na->in_bytes, nb->in_bytes); else - return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i; + result = -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate); case 3: if(cumulative) - return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i; + result = -cmpu64(na->out_packets, nb->out_packets); else - return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i; + result = -cmpfloat(na->out_packets_rate, nb->out_packets_rate); case 4: if(cumulative) - return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i; + result = -cmpu64(na->out_bytes, nb->out_bytes); else - return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i; + result = -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate); case 5: if(cumulative) - return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i; + result = -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets); else - return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i; + result = -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate); case 6: if(cumulative) - return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i; + result = -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes); else - return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i; + result = -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate); default: - return strcmp(na->name, nb->name) ?: na->i - nb->i; + result = strcmp(na->name, nb->name); } + + if(result) + return result; + else + return na->i - nb->i; } static void redraw(void) {