1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 #include "iptraf-ng-compat.h"
7 #include "parse-options.h"
12 static const char *const capture_usage
[] = {
13 IPTRAF_NAME
" capture [-c] <device>",
17 static int cap_nr_pkt
= 1, help_opt
;
18 static char *ofilename
;
20 static struct options capture_options
[] = {
23 OPT_INTEGER('c', "capture", &cap_nr_pkt
, "capture <n> packets"),
24 OPT_STRING('o', "output", &ofilename
, "file", "save captured packet into <file>"),
28 int cmd_capture(int argc
, char **argv
)
30 parse_opts(argc
, argv
, capture_options
, capture_usage
);
32 if (help_opt
|| !*argv
|| argv
[1])
33 parse_usage_and_die(capture_usage
, capture_options
);
38 if (capt_init(&capt
, dev
) == -1)
39 die_errno("Unable to initialize packet capture interface");
43 fp
= fopen(ofilename
, "wb");
53 if (capt_get_packet(&capt
, &pkt
, NULL
, NULL
) == -1)
54 die_errno("fail to get packet");
63 fwrite(&pkt
, sizeof(pkt
), 1, fp
);
65 capt_put_packet(&capt
, &pkt
);
67 if (++captured
== cap_nr_pkt
)