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 <erwin@erwinrol.com>
Reviewed-on: https://code.wireshark.org/review/36067
Petri-Dish: Anders Broman <a.broman58@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Erwin Rol 2020-02-09 21:30:01 +01:00 committed by Anders Broman
parent 20c8215fd5
commit 2ab65bf0ba
2 changed files with 8 additions and 5 deletions

View File

@ -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;
}

View File

@ -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;
}