Permalink
Browse files

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>
  • Loading branch information...
droberson authored and tklauser committed Apr 19, 2018
1 parent 3b282c9 commit d7209265e9164b1699177552832e6e88e530dc9f
Showing with 18 additions and 3 deletions.
  1. +18 −3 netsniff-ng.c
View
@@ -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);
}

0 comments on commit d720926

Please sign in to comment.