diff options
author | Daniel Roberson <daniel@planethacker.net> | 2018-04-19 11:12:00 -0700 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2018-05-14 14:10:07 +0200 |
commit | d7209265e9164b1699177552832e6e88e530dc9f (patch) | |
tree | ab33a1621dec3b236557f1083e2897db5534c0a9 | |
parent | 3b282c9a69f04bf338af5243c56e45cb99b201b1 (diff) |
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 <daniel@planethacker.net>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | netsniff-ng.c | 21 |
1 files 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 <pthread.h> #include <fcntl.h> #include <inttypes.h> +#include <limits.h> #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); } |