From d7bf0bd53dc3c5f77cfe5c8e2450d8f209d024b7 Mon Sep 17 00:00:00 2001 From: Soner Tari Date: Tue, 2 Jul 2024 21:12:46 +0300 Subject: [PATCH] Use clock_gettime() instead of gettimeofday(), thanks to @disaykin --- src/logpkt.c | 12 +++++++++--- src/sys.c | 8 ++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/logpkt.c b/src/logpkt.c index ba7cbb1..4ed7aa7 100644 --- a/src/logpkt.c +++ b/src/logpkt.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #ifndef WITHOUT_MIRROR @@ -298,11 +299,16 @@ static int logpkt_pcap_write(const uint8_t *pkt, size_t pktsz, int fd) { pcap_rec_hdr_t rec_hdr; - struct timeval tv; + struct timespec tv; - gettimeofday(&tv, NULL); + if (clock_gettime(CLOCK_MONOTONIC, &tv) == -1) + { + log_err_printf("Error getting current time: %s\n", + strerror(errno)); + return -1; + } rec_hdr.ts_sec = tv.tv_sec; - rec_hdr.ts_usec = tv.tv_usec; + rec_hdr.ts_usec = tv.tv_nsec / 1000; rec_hdr.orig_len = rec_hdr.incl_len = pktsz; if (logpkt_write_all(fd, &rec_hdr, sizeof(rec_hdr)) == -1) { diff --git a/src/sys.c b/src/sys.c index 6cecd83..d183c4a 100644 --- a/src/sys.c +++ b/src/sys.c @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -50,6 +49,7 @@ #include #include #include +#include #include #ifndef _SC_NPROCESSORS_ONLN @@ -1019,12 +1019,12 @@ static int sys_rand_seeded = 0; static void sys_rand_seed(void) { - struct timeval seed; + struct timespec seed; - if (gettimeofday(&seed, NULL) == -1) { + if (clock_gettime(CLOCK_MONOTONIC, &seed) == -1) { srandom((unsigned)time(NULL)); } else { - srandom((unsigned)(seed.tv_sec ^ seed.tv_usec)); + srandom((unsigned)(seed.tv_sec ^ seed.tv_nsec)); } sys_rand_seeded = 1; }