From 07fa58ff28b700cf4bceb9f67db6af504f61a354 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 1 Oct 2014 13:42:12 +0200 Subject: netsniff-ng: Combine redundant pcap file rotation logic into function The code to create the next pcap dump file is duplicated for the HAVE_TPACKET3 and !HAVE_TPACKET3 case. Consolidate the functionality into a function to reduce code duplication. Signed-off-by: Tobias Klauser --- netsniff-ng.c | 58 ++++++++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/netsniff-ng.c b/netsniff-ng.c index 47edbcd..427b5e9 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -798,6 +798,28 @@ static void print_pcap_file_stats(int sock, struct ctx *ctx) } } +static void update_pcap_next_dump(struct ctx *ctx, unsigned long snaplen, int *fd, int sock) +{ + if (!dump_to_pcap(ctx)) + return; + + if (ctx->dump_mode == DUMP_INTERVAL_SIZE) { + interval += snaplen; + if (interval > ctx->dump_interval) { + next_dump = true; + interval = 0; + } + } + + if (next_dump) { + *fd = next_multi_pcap_file(ctx, *fd); + next_dump = false; + + if (ctx->verbose) + print_pcap_file_stats(sock, ctx); + } +} + #ifdef HAVE_TPACKET3 static void walk_t3_block(struct block_desc *pbd, struct ctx *ctx, int sock, int *fd, unsigned long *frame_count) @@ -846,23 +868,7 @@ next: } } - if (dump_to_pcap(ctx)) { - if (ctx->dump_mode == DUMP_INTERVAL_SIZE) { - interval += hdr->tp_snaplen; - if (interval > ctx->dump_interval) { - next_dump = true; - interval = 0; - } - } - - if (next_dump) { - *fd = next_multi_pcap_file(ctx, *fd); - next_dump = false; - - if (unlikely(ctx->verbose)) - print_pcap_file_stats(sock, ctx); - } - } + update_pcap_next_dump(ctx, hdr->tp_snaplen, fd, sock); } } #endif /* HAVE_TPACKET3 */ @@ -1007,23 +1013,7 @@ next: if (unlikely(sigint == 1)) break; - if (dump_to_pcap(ctx)) { - if (ctx->dump_mode == DUMP_INTERVAL_SIZE) { - interval += hdr->tp_h.tp_snaplen; - if (interval > ctx->dump_interval) { - next_dump = true; - interval = 0; - } - } - - if (next_dump) { - fd = next_multi_pcap_file(ctx, fd); - next_dump = false; - - if (unlikely(ctx->verbose)) - print_pcap_file_stats(sock, ctx); - } - } + update_pcap_next_dump(ctx, hdr->tp_h.tp_snaplen, &fd, sock); } #endif /* HAVE_TPACKET3 */ -- cgit v1.2.3-54-g00ecf