49 volatile sig_atomic_t corsaro_shutdown = 0;
51 static libtrace_t *trace = NULL;
52 static libtrace_packet_t *packet = NULL;
53 static libtrace_filter_t *filter = NULL;
56 static void catch_sigint(
int sig)
58 fprintf(stderr,
"caught SIGINT, shutting down at the next opportunity\n");
60 signal(sig, catch_sigint);
68 trace_destroy_packet(packet);
78 static int init_trace(
char *tracefile)
80 trace = trace_create(tracefile);
82 if (trace_is_err(trace)) {
83 trace_perror(trace,
"Opening trace file");
88 if (trace_start(trace) == -1) {
89 trace_perror(trace,
"Starting trace");
97 static void close_trace()
101 trace_destroy(trace);
106 static void usage(
const char *name)
109 "usage: %s -o outfile [-i interval] [-m mode] [-n name]\n"
110 " [-p plugin] [-f filter] trace_uri [trace_uri...]\n"
111 " -o <outfile> use <outfile> as a template for file names.\n"
112 " %%P will be replaced with the plugin name\n"
113 " -i <interval> distribution interval in seconds (default: %d)\n"
114 " -m <mode> output in 'ascii' or 'binary'. (default: ascii)\n"
115 " -n <name> monitor name (default: "
116 STR(CORSARO_MONITOR_NAME)
")\n"
117 " -p <plugin> enable the given plugin, -p can be used \n"
118 " multiple times (default: all)\n"
119 " -f <filter> BPF filter to apply to packets\n",
123 int main(
int argc,
char *argv[])
128 char *bpf_filter = NULL;
133 int tracefile_cnt = 0;
134 char *traceuri =
"Multiple Traces";
136 signal(SIGINT, catch_sigint);
138 while((opt = getopt(argc, argv,
"f:i:m:n:o:p:v?")) >= 0)
147 if(strcmp(optarg,
"ascii") == 0)
152 else if(strcmp(optarg,
"binary") == 0)
159 "ERROR: mode parameter must be 'ascii' or 'binary'\n");
166 name = strdup(optarg);
179 tmpl = strdup(optarg);
183 plugins[plugin_cnt++] = strdup(optarg);
187 bpf_filter = strdup(optarg);
192 fprintf(stderr,
"corsaro version %d.%d.%d\n", CORSARO_MAJOR_VERSION,
193 CORSARO_MID_VERSION, CORSARO_MINOR_VERSION);
204 if(optind > argc - 1)
210 tracefile_cnt = argc-optind;
213 if ((packet = trace_create_packet()) == NULL) {
214 perror(
"Creating libtrace packet");
221 if(tracefile_cnt == 1)
223 if(init_trace(argv[optind]) != 0)
225 fprintf(stderr,
"failed to init trace");
229 traceuri = argv[optind];
233 if(bpf_filter != NULL)
235 filter = trace_create_filter(bpf_filter);
248 corsaro_log(__func__, corsaro,
"failed to set trace uri");
255 corsaro_log(__func__, corsaro,
"failed to set monitor name");
265 for(i=0;i<plugin_cnt;i++)
269 corsaro_log(__func__, corsaro,
"failed to enable %s",
278 corsaro_log(__func__, corsaro,
"failed to start corsaro");
283 for(i = optind; i < argc; i++)
287 corsaro_log(__func__, corsaro,
"processing %s", argv[i]);
291 while (corsaro_shutdown == 0 && trace_read_packet(trace,packet)>0) {
292 if((filter == NULL || trace_apply_filter(filter, packet) > 0) &&
295 corsaro_log(__func__, corsaro,
"corsaro_per_packet failed");
301 if (trace_is_err(trace)) {
302 trace_perror(trace,
"Reading packets");
303 corsaro_log(__func__, corsaro,
"libtrace had an error reading packets");
311 for(i=0;i<plugin_cnt;i++)
313 if(plugins[i] != NULL)