]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
global: switch from timeval to timespec
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 22 Nov 2017 15:49:56 +0000 (16:49 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 22 Nov 2017 17:32:48 +0000 (18:32 +0100)
This gets us nanoseconds instead of microseconds, which is better, and
we can do this pretty much without freaking out existing userspace,
which doesn't actually make use of the nano/micro seconds field:

zx2c4@thinkpad ~ $ cat a.c
void main()
{
        puts(sizeof(struct timeval) == sizeof(struct timespec) ? "success" : "failure");
}
zx2c4@thinkpad ~ $ gcc a.c -m64 && ./a.out
success
zx2c4@thinkpad ~ $ gcc a.c -m32 && ./a.out
success

This doesn't solve y2038 problem, but timespec64 isn't yet a thing in
userspace.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/containers.h
src/ipc.c
src/show.c

index 58d06579c51decc4629114d209560a7f2babb01b..31eabea2ce4beb12576c2f98e7ae1bcb02325c13 100644 (file)
@@ -6,9 +6,9 @@
 
 #include <stdint.h>
 #include <stdlib.h>
+#include <time.h>
 #include <net/if.h>
 #include <netinet/in.h>
-#include <sys/time.h>
 #include <sys/socket.h>
 
 #include "../uapi/wireguard.h"
@@ -43,7 +43,7 @@ struct wgpeer {
                struct sockaddr_in6 addr6;
        } endpoint;
 
-       struct timeval last_handshake_time;
+       struct timespec last_handshake_time;
        uint64_t rx_bytes, tx_bytes;
        uint16_t persistent_keepalive_interval;
 
index 2d2428746c46cd32b0e824121db7cf4b97824066..a88672fd2eb8399676a6c730940d81defd4aba3c 100644 (file)
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -427,7 +427,7 @@ static int userspace_get_device(struct wgdevice **out, const char *interface)
                } else if (peer && !strcmp(key, "last_handshake_time_sec"))
                        peer->last_handshake_time.tv_sec = NUM(0xffffffffffffffffULL);
                else if (peer && !strcmp(key, "last_handshake_time_nsec"))
-                       peer->last_handshake_time.tv_usec = NUM(0xffffffffffffffffULL) / 1000;
+                       peer->last_handshake_time.tv_nsec = NUM(0xffffffffffffffffULL);
                else if (peer && !strcmp(key, "rx_bytes"))
                        peer->rx_bytes = NUM(0xffffffffffffffffULL);
                else if (peer && !strcmp(key, "tx_bytes"))
index 6ae583008b16782dfc6c8874bc0faddbe453030f..ebfdf5b7f406628e9f7e3d81e6044c74efe6a644 100644 (file)
@@ -24,13 +24,13 @@ static int peer_cmp(const void *first, const void *second)
        time_t diff;
        const struct wgpeer *a = *(const void **)first, *b = *(const void **)second;
 
-       if (!a->last_handshake_time.tv_sec && !a->last_handshake_time.tv_usec && (b->last_handshake_time.tv_sec || b->last_handshake_time.tv_usec))
+       if (!a->last_handshake_time.tv_sec && !a->last_handshake_time.tv_nsec && (b->last_handshake_time.tv_sec || b->last_handshake_time.tv_nsec))
                return 1;
-       if (!b->last_handshake_time.tv_sec && !b->last_handshake_time.tv_usec && (a->last_handshake_time.tv_sec || a->last_handshake_time.tv_usec))
+       if (!b->last_handshake_time.tv_sec && !b->last_handshake_time.tv_nsec && (a->last_handshake_time.tv_sec || a->last_handshake_time.tv_nsec))
                return -1;
        diff = a->last_handshake_time.tv_sec - b->last_handshake_time.tv_sec;
        if (!diff)
-               diff = a->last_handshake_time.tv_usec - b->last_handshake_time.tv_usec;
+               diff = a->last_handshake_time.tv_nsec - b->last_handshake_time.tv_nsec;
        if (diff < 0)
                return 1;
        if (diff > 0)
@@ -149,7 +149,7 @@ static size_t pretty_time(char *buf, const size_t len, unsigned long long left)
        return offset;
 }
 
-static char *ago(const struct timeval *t)
+static char *ago(const struct timespec *t)
 {
        static char buf[1024];
        size_t offset;