From d7209265e9164b1699177552832e6e88e530dc9f Mon Sep 17 00:00:00 2001 From: Daniel Roberson Date: Thu, 19 Apr 2018 11:12:00 -0700 Subject: netsniff-ng: add date format strings to --out This adds the ability to use date(1)/strftime(3) style format strings when specifying an output file. Example: netsniff-ng --out %Y-%m-%d.pcap ### outputs to 2018-04-20.pcap Fixes #158 Signed-off-by: Daniel Roberson Signed-off-by: Tobias Klauser --- netsniff-ng.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/netsniff-ng.c b/netsniff-ng.c index cbd837b..48e1221 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "ring_rx.h" #include "ring_tx.h" @@ -793,7 +794,7 @@ static void finish_multi_pcap_file(struct ctx *ctx, int fd) static int next_multi_pcap_file(struct ctx *ctx, int fd) { int ret; - char fname[512]; + char fname[PATH_MAX]; time_t ftime; __pcap_io->fsync_pcap(fd); @@ -843,7 +844,7 @@ static void reset_interval(struct ctx *ctx) static int begin_multi_pcap_file(struct ctx *ctx) { int fd, ret; - char fname[256]; + char fname[PATH_MAX]; bug_on(!__pcap_io); @@ -887,6 +888,7 @@ static void finish_single_pcap_file(struct ctx *ctx, int fd) static int begin_single_pcap_file(struct ctx *ctx) { int fd, ret; + char fname[PATH_MAX]; bug_on(!__pcap_io); @@ -896,7 +898,20 @@ static int begin_single_pcap_file(struct ctx *ctx) if (ctx->pcap == PCAP_OPS_MM) ctx->pcap = PCAP_OPS_SG; } else { - fd = open_or_die_m(ctx->device_out, + time_t t; + struct tm *ltm; + + t = time(NULL); + if (t == -1) + panic("time() failed\n"); + + ltm = localtime(&t); + if (ltm == NULL) + panic("localtime() failed\n"); + + strftime(fname, sizeof(fname), ctx->device_out, ltm); + + fd = open_or_die_m(fname, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, DEFFILEMODE); } -- cgit v1.2.3-54-g00ecf