From bc1f6be5dbcfe60cec053825b5efe73cff729844 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 22 Nov 2017 16:49:56 +0100 Subject: [PATCH] global: switch from timeval to timespec 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 --- src/containers.h | 4 ++-- src/ipc.c | 2 +- src/show.c | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/containers.h b/src/containers.h index 58d0657..31eabea 100644 --- a/src/containers.h +++ b/src/containers.h @@ -6,9 +6,9 @@ #include #include +#include #include #include -#include #include #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; diff --git a/src/ipc.c b/src/ipc.c index 2d24287..a88672f 100644 --- 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")) diff --git a/src/show.c b/src/show.c index 6ae5830..ebfdf5b 100644 --- a/src/show.c +++ b/src/show.c @@ -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;