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 <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2017-11-22 16:49:56 +01:00
parent 08ce3b2426
commit bc1f6be5db
3 changed files with 7 additions and 7 deletions

View file

@ -6,9 +6,9 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <net/if.h> #include <net/if.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "../uapi/wireguard.h" #include "../uapi/wireguard.h"
@ -43,7 +43,7 @@ struct wgpeer {
struct sockaddr_in6 addr6; struct sockaddr_in6 addr6;
} endpoint; } endpoint;
struct timeval last_handshake_time; struct timespec last_handshake_time;
uint64_t rx_bytes, tx_bytes; uint64_t rx_bytes, tx_bytes;
uint16_t persistent_keepalive_interval; uint16_t persistent_keepalive_interval;

View file

@ -427,7 +427,7 @@ static int userspace_get_device(struct wgdevice **out, const char *interface)
} else if (peer && !strcmp(key, "last_handshake_time_sec")) } else if (peer && !strcmp(key, "last_handshake_time_sec"))
peer->last_handshake_time.tv_sec = NUM(0xffffffffffffffffULL); peer->last_handshake_time.tv_sec = NUM(0xffffffffffffffffULL);
else if (peer && !strcmp(key, "last_handshake_time_nsec")) 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")) else if (peer && !strcmp(key, "rx_bytes"))
peer->rx_bytes = NUM(0xffffffffffffffffULL); peer->rx_bytes = NUM(0xffffffffffffffffULL);
else if (peer && !strcmp(key, "tx_bytes")) else if (peer && !strcmp(key, "tx_bytes"))

View file

@ -24,13 +24,13 @@ static int peer_cmp(const void *first, const void *second)
time_t diff; time_t diff;
const struct wgpeer *a = *(const void **)first, *b = *(const void **)second; 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; 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; return -1;
diff = a->last_handshake_time.tv_sec - b->last_handshake_time.tv_sec; diff = a->last_handshake_time.tv_sec - b->last_handshake_time.tv_sec;
if (!diff) 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) if (diff < 0)
return 1; return 1;
if (diff > 0) if (diff > 0)
@ -149,7 +149,7 @@ static size_t pretty_time(char *buf, const size_t len, unsigned long long left)
return offset; return offset;
} }
static char *ago(const struct timeval *t) static char *ago(const struct timespec *t)
{ {
static char buf[1024]; static char buf[1024];
size_t offset; size_t offset;