From 2ab65bf0baa49bbbc65fa0a56fb59da93dd6c3fd Mon Sep 17 00:00:00 2001 From: Erwin Rol Date: Sun, 9 Feb 2020 21:30:01 +0100 Subject: [PATCH] extcap: Fix packet time calculation The packet time calculation used the time(NULL) call to get the time in seconds and than divided by 1000 to get micro seconds. This results in correct seconds but some random micro seconds values. Now get the time in microseconds and divide by 1000000 to get the seconds and use the remainder for the micro seconds part. Change-Id: I31f90960e27b0089c20936f69c7dc30f1efd50d9 Signed-off-by: Erwin Rol Reviewed-on: https://code.wireshark.org/review/36067 Petri-Dish: Anders Broman Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- extcap/ciscodump.c | 7 ++++--- extcap/udpdump.c | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 681d1e1a5d..415a558fe5 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -228,7 +228,7 @@ static void ssh_loop_read(ssh_channel channel, FILE* fp, const guint32 count) unsigned offset = 0; unsigned packet_size = 0; guint8* packet; - time_t curtime = time(NULL); + gint64 curtime = g_get_real_time(); int err; guint64 bytes_written; long unsigned packets = 0; @@ -254,8 +254,9 @@ static void ssh_loop_read(ssh_channel channel, FILE* fp, const guint32 count) if (status == CISCODUMP_PARSER_END_PACKET) { /* dump the packet to the pcap file */ - if (!libpcap_write_packet(fp, curtime, (guint32)(curtime / 1000), packet_size, - packet_size, packet, &bytes_written, &err)) { + if (!libpcap_write_packet(fp, + (guint32)(curtime / G_USEC_PER_SEC), (guint32)(curtime % G_USEC_PER_SEC), + packet_size, packet_size, packet, &bytes_written, &err)) { g_debug("Error in libpcap_write_packet(): %s", g_strerror(err)); break; } diff --git a/extcap/udpdump.c b/extcap/udpdump.c index bfc725f1f2..28ba761708 100644 --- a/extcap/udpdump.c +++ b/extcap/udpdump.c @@ -258,7 +258,7 @@ static int dump_packet(const char* proto_name, const guint16 listenport, const c { guint8* mbuf; guint offset = 0; - time_t curtime = time(NULL); + gint64 curtime = g_get_real_time(); guint64 bytes_written = 0; int err; int ret = EXIT_SUCCESS; @@ -276,7 +276,9 @@ static int dump_packet(const char* proto_name, const guint16 listenport, const c memcpy(mbuf + offset, buf, buflen); offset += (guint)buflen; - if (!libpcap_write_packet(fp, curtime, (guint32)(curtime / 1000), offset, offset, mbuf, &bytes_written, &err)) { + if (!libpcap_write_packet(fp, + (guint32)(curtime / G_USEC_PER_SEC), (guint32)(curtime % G_USEC_PER_SEC), + offset, offset, mbuf, &bytes_written, &err)) { g_warning("Can't write packet: %s", g_strerror(err)); ret = EXIT_FAILURE; }