Use clock_gettime() instead of gettimeofday(), thanks to @disaykin

This commit is contained in:
Soner Tari 2024-07-02 21:12:46 +03:00
parent b3705efe9d
commit d7bf0bd53d
2 changed files with 13 additions and 7 deletions

View File

@ -40,6 +40,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <time.h>
#include <errno.h>
#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) {

View File

@ -38,7 +38,6 @@
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/un.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
@ -50,6 +49,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#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;
}