From 06259c43d08c699ca5245ac52382d98fd3374394 Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Sat, 17 Feb 2024 18:45:14 +0100 Subject: [PATCH] pcap: Add a packet couter for diagnostics Signed-off-by: Siddharth Chandrasekaran --- include/utils/pcap_gen.h | 1 + src/pcap_gen.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/utils/pcap_gen.h b/include/utils/pcap_gen.h index 7a6778d..f59b1f3 100644 --- a/include/utils/pcap_gen.h +++ b/include/utils/pcap_gen.h @@ -18,6 +18,7 @@ typedef struct { FILE *file; size_t offset; void *cache; + size_t num_packets; } pcap_t; pcap_t *pcap_start(char *path, uint32_t max_packet_size, uint32_t link_type); diff --git a/src/pcap_gen.c b/src/pcap_gen.c index 2637c69..8b53d8c 100644 --- a/src/pcap_gen.c +++ b/src/pcap_gen.c @@ -55,6 +55,7 @@ pcap_t *pcap_start(char *path, uint32_t max_packet_size, uint32_t link_type) header.link_type = link_type; cap->offset = 0; + cap->num_packets = 0; cap->file = fopen(path, "wb"); if(cap->file == NULL) { free(cap->cache); @@ -86,8 +87,9 @@ int pcap_add(pcap_t *cap, uint8_t *capture_data, uint32_t length) uint32_t sec, usec; if (cap->offset + sizeof(header) + length > PCAP_CACHE_SIZE) { - if (pcap_flush(cap)) + if (pcap_flush(cap)) { return -1; + } } get_time(&sec, &usec); @@ -100,7 +102,7 @@ int pcap_add(pcap_t *cap, uint8_t *capture_data, uint32_t length) memcpy((char *)cap->cache + cap->offset, capture_data, length); cap->offset += length; - + cap->num_packets++; return 0; }