Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
corsaro_io.c
1 /*
2  * corsaro
3  *
4  * Alistair King, CAIDA, UC San Diego
5  * corsaro-info@caida.org
6  *
7  * Copyright (C) 2012 The Regents of the University of California.
8  *
9  * This file is part of corsaro.
10  *
11  * corsaro is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * corsaro is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with corsaro. If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #include "config.h"
27 #include "corsaro_int.h"
28 
29 #include <assert.h>
30 #include <inttypes.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
37 #ifdef HAVE_TIME_H
38 #include <time.h>
39 #endif
40 
41 #include "corsaro.h"
42 #include "corsaro_file.h"
43 #include "corsaro_log.h"
44 #include "corsaro_plugin.h"
45 #include "utils.h"
46 
47 /* plugin headers */
48 #ifdef WITH_PLUGIN_SIXT
49 #include "corsaro_flowtuple.h"
50 #endif
51 #ifdef WITH_PLUGIN_DOS
52 #include "corsaro_dos.h"
53 #endif
54 
55 #include "corsaro_io.h"
56 #include "corsaro_plugin.h"
57 
59 typedef struct output_funcs
60 {
61  int (*headers)(corsaro_t *corsaro, corsaro_file_t *file,
62  corsaro_header_t *header);
63  int (*interval_start)(corsaro_t *corsaro, corsaro_file_t *file,
64  corsaro_interval_t *int_start);
65  int (*interval_end)(corsaro_t *corsaro, corsaro_file_t *file,
66  corsaro_interval_t *int_end);
67  int (*plugin_start)(corsaro_t *corsaro, corsaro_file_t *file,
68  corsaro_plugin_t *plugin);
69  int (*plugin_end)(corsaro_t *corsaro, corsaro_file_t *file,
70  corsaro_plugin_t *plugin);
71  int (*trailers)(corsaro_t *corsaro, corsaro_file_t *file,
72  corsaro_trailer_t *trailer);
73 } output_funcs_t;
74 
75 #define GENERATE_OUTPUT_FUNCS(o) \
76  {output_headers_##o, output_interval_start_##o, output_interval_end_##o, \
77  output_plugin_start_##o, output_plugin_end_##o, output_trailers_##o}
78 
79 #define GENERATE_OUTPUT_FUNC_PROTOS(o) \
80  static int output_headers_##o(corsaro_t *corsaro, corsaro_file_t *file, \
81  corsaro_header_t *header); \
82  static int output_interval_start_##o(corsaro_t *corsaro, \
83  corsaro_file_t *file, \
84  corsaro_interval_t *int_start); \
85  static int output_interval_end_##o(corsaro_t *corsaro, \
86  corsaro_file_t *file, \
87  corsaro_interval_t *int_end); \
88  static int output_plugin_start_##o(corsaro_t *corsaro, corsaro_file_t *file, \
89  corsaro_plugin_t *plugin); \
90  static int output_plugin_end_##o(corsaro_t *corsaro, corsaro_file_t *file, \
91  corsaro_plugin_t *plugin); \
92  static int output_trailers_##o(corsaro_t *corsaro, corsaro_file_t *file, \
93  corsaro_trailer_t *trailer);
94 
95 GENERATE_OUTPUT_FUNC_PROTOS(ascii);
96 GENERATE_OUTPUT_FUNC_PROTOS(binary);
97 
98 static output_funcs_t output_funcs[] = {
99  GENERATE_OUTPUT_FUNCS(ascii), /* CORSARO_FILE_MODE_ASCII */
100  GENERATE_OUTPUT_FUNCS(binary), /* CORSARO_FILE_MODE_BINARY */
101  {NULL}, /* CORSARO_FILE_MODE_TRACE */
102 };
103 
104 
105 /*
106  * print_headers
107  *
108  * prints the global corsaro details which appear at the head of the output
109  * file
110  */
111 static int output_headers_ascii(corsaro_t *corsaro, corsaro_file_t *file,
112  corsaro_header_t *header)
113 {
114  corsaro_plugin_t *tmp = NULL;
115  uint32_t bytes_out = 0;
116 
117  corsaro_header_t *ph = NULL;
119 
120  int i;
121 
122  if(header == NULL)
123  {
124  h.version_major = CORSARO_MAJOR_VERSION;
125  h.version_minor = CORSARO_MINOR_VERSION;
126  h.local_init_time = corsaro->init_time.tv_sec;
127  h.interval_length = corsaro->interval;
128  h.traceuri = (uint8_t*)corsaro->uridata;
129 
130  ph = &h;
131  }
132  else
133  {
134  ph = header;
135  }
136 
137 
138  bytes_out += corsaro_file_printf(corsaro, file,
139  "# CORSARO_VERSION %"PRIu8".%"PRIu8"\n",
140  ph->version_major, ph->version_minor);
141  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_INITTIME %ld\n",
142  ph->local_init_time);
143  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_INTERVAL %d\n",
144  ph->interval_length);
145  if(ph->traceuri != NULL)
146  {
147  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_TRACEURI %s\n",
148  ph->traceuri);
149  }
150 
151  if(header == NULL)
152  {
153  while((tmp = corsaro_plugin_next(corsaro->plugin_manager, tmp)) != NULL)
154  {
155  bytes_out += corsaro_file_printf(corsaro, file,
156  "# CORSARO_PLUGIN %s\n",
157  tmp->name);
158  }
159  }
160  else
161  {
162  for(i=0;i<header->plugin_cnt;i++)
163  {
164  bytes_out += corsaro_file_printf(corsaro, file,
165  "# CORSARO_PLUGIN %s\n",
167  corsaro->plugin_manager,
168  header->plugin_magics[i]
169  ));
170  }
171  }
172 
173  return bytes_out;
174 }
175 
176 static int output_headers_binary(corsaro_t *corsaro, corsaro_file_t *file,
177  corsaro_header_t *header)
178 {
179  corsaro_plugin_t *p = NULL;
180  uint8_t fbuffer[CORSARO_IO_HEADER_FIXED_BYTE_LEN];
181  uint8_t *ptr = &fbuffer[0];
182  uint16_t tmp_len;
183  uint8_t tmp[4];
184 
185  uint32_t bytes_out = 0;
186 
187  /* magic numbers */
189  ptr+=4;
191  ptr+=4;
192 
193  /* version */
194  *ptr = CORSARO_MAJOR_VERSION;
195  ptr++;
196  *ptr = CORSARO_MID_VERSION;
197  ptr++;
198 
199  /* init time */
200  bytes_htonl(ptr, corsaro->init_time.tv_sec);
201  ptr+=4;
202 
203  /* interval length */
204  bytes_htons(ptr, corsaro->interval);
205 
206  if(corsaro_file_write(corsaro, file, &fbuffer[0],
209  {
210  corsaro_log(__func__, corsaro, "could not dump byte array to file");
211  return -1;
212  }
213 
215 
216  /* the traceuri */
217  if(corsaro->uridata != NULL)
218  {
219  tmp_len = strlen(corsaro->uridata);
220  bytes_htons(&tmp[0], tmp_len);
221  if(corsaro_file_write(corsaro, file, &tmp[0], 2) != 2)
222  {
223  corsaro_log(__func__, corsaro, "could not dump traceuri length to file");
224  return -1;
225  }
226  if(corsaro_file_write(corsaro, file, corsaro->uridata, tmp_len) != tmp_len)
227  {
228  corsaro_log(__func__, corsaro, "could not dump traceuri string to file");
229  return -1;
230  }
231  bytes_out += 2 + tmp_len;
232  }
233  else
234  {
235  memset(&tmp[0], 0, 2);
236  if(corsaro_file_write(corsaro, file, &tmp[0], 2) != 2)
237  {
238  corsaro_log(__func__, corsaro, "could not dump zero traceuri length to file");
239  return -1;
240  }
241  bytes_out += 2;
242  }
243 
244  /* the plugin list */
245  if(corsaro->plugin_manager->plugins_enabled != NULL)
246  {
247  bytes_htons(&tmp[0], corsaro->plugin_manager->plugins_enabled_cnt);
248  }
249  else
250  {
251  bytes_htons(&tmp[0], corsaro->plugin_manager->plugins_cnt);
252  }
253 
254  if(corsaro_file_write(corsaro, file, &tmp[0], 2) != 2)
255  {
256  corsaro_log(__func__, corsaro, "could not dump plugins cnt to file");
257  return -1;
258  }
259  bytes_out += 2;
260 
261  while((p = corsaro_plugin_next(corsaro->plugin_manager, p)) != NULL)
262  {
263  bytes_htonl(&tmp[0], p->magic);
264  if(corsaro_file_write(corsaro, file, &tmp[0], 4) != 4)
265  {
266  corsaro_log(__func__, corsaro, "could not dump plugin magic to file");
267  return -1;
268  }
269  bytes_out += 4;
270  }
271 
272  return bytes_out;
273 }
274 
275 /*
276  * output_interval
277  *
278  * prints data for the interval which is about to be completed.
279  */
280 static int output_interval_start_ascii(corsaro_t *corsaro, corsaro_file_t *file,
281  corsaro_interval_t *int_start)
282 {
283  return corsaro_file_printf(corsaro, file,
284  "# CORSARO_INTERVAL_START %d %ld\n",
285  int_start->number, int_start->time);
286 }
287 
288 static int output_interval_end_ascii(corsaro_t *corsaro, corsaro_file_t *file,
289  corsaro_interval_t *int_end)
290 {
291  return corsaro_file_printf(corsaro, file,
292  "# CORSARO_INTERVAL_END %d %ld\n",
293  int_end->number,
294  int_end->time);
295 }
296 
297 static int write_interval_header_binary(corsaro_t * corsaro, corsaro_file_t *file,
299 {
300  corsaro_interval_t nint;
301 
302 #if 0
304  uint8_t *iptr = &ibuff[0];
305 
306  /* interval header */
307  bytes_htonl(iptr, CORSARO_MAGIC);
308  iptr+=4;
310  iptr+=4;
311  bytes_htons(iptr, corsaro->interval_cnt);
312  iptr+=2;
313  bytes_htonl(iptr, tv.tv_sec);
314 
315  if(corsaro_file_write(corsaro, file, &ibuff[0],
318  {
319  corsaro_log(__func__, corsaro, "could not dump interval header to file");
320  return -1;
321  }
323 #endif
324 
325  /* byte flip all the fields */
326  nint.corsaro_magic = htonl(interval->corsaro_magic);
327  nint.magic = htonl(interval->magic);
328  nint.number = htons(interval->number);
329  nint.time = htonl(interval->time);
330 
331  if(corsaro_file_write(corsaro, file, &nint, sizeof(corsaro_interval_t)) !=
332  sizeof(corsaro_interval_t))
333  {
334  corsaro_log(__func__, corsaro, "could not dump interval header to file");
335  return -1;
336  }
337 
338  return sizeof(corsaro_interval_t);
339 }
340 
341 static int output_interval_start_binary(corsaro_t *corsaro, corsaro_file_t *file,
342  corsaro_interval_t *int_start)
343 {
344  return write_interval_header_binary(corsaro, file, int_start);
345 }
346 
347 static int output_interval_end_binary(corsaro_t *corsaro, corsaro_file_t *file,
348  corsaro_interval_t *int_end)
349 {
350  return write_interval_header_binary(corsaro, file, int_end);
351 }
352 
353 static int output_plugin_start_ascii(corsaro_t *corsaro, corsaro_file_t *file,
354  corsaro_plugin_t *plugin)
355 {
356  return corsaro_file_printf(corsaro, file,
357  "# CORSARO_PLUGIN_DATA_START %s\n",
358  plugin->name);
359 }
360 
361 static int output_plugin_end_ascii(corsaro_t *corsaro, corsaro_file_t *file,
362  corsaro_plugin_t *plugin)
363 {
364  return corsaro_file_printf(corsaro, file,
365  "# CORSARO_PLUGIN_DATA_END %s\n",
366  plugin->name);
367 }
368 
369 static int write_plugin_header_binary(corsaro_t *corsaro, corsaro_file_t *file,
370  corsaro_plugin_t *plugin)
371 {
372  corsaro_plugin_data_t data = {
373  htonl(CORSARO_MAGIC),
374  htonl(CORSARO_MAGIC_DATA),
375  htonl(plugin->magic),
376  };
377 
378  if(corsaro_file_write(corsaro, file, &data,
379  sizeof(corsaro_plugin_data_t)) !=
380  sizeof(corsaro_plugin_data_t))
381  {
382  corsaro_log(__func__, corsaro,
383  "could not dump interval data header to file");
384  return -1;
385  }
386  return sizeof(corsaro_plugin_data_t);
387 }
388 
389 static int output_plugin_start_binary(corsaro_t *corsaro, corsaro_file_t *file,
390  corsaro_plugin_t *plugin)
391 {
392  return write_plugin_header_binary(corsaro, file, plugin);
393 }
394 
395 static int output_plugin_end_binary(corsaro_t *corsaro, corsaro_file_t *file,
396  corsaro_plugin_t *plugin)
397 {
398  return write_plugin_header_binary(corsaro, file, plugin);
399 }
400 
401 /*
402  * print_trailers
403  *
404  * prints the global corsaro details which appear at the tail of the output
405  * file (when corsaro_finalize has been called)
406  */
407 static int output_trailers_ascii(corsaro_t *corsaro, corsaro_file_t *file,
408  corsaro_trailer_t *trailer)
409 {
410  struct timeval ts;
411  uint32_t bytes_out = 0;
412 
413  uint64_t acnt = corsaro_get_accepted_packets(corsaro);
414  uint64_t dcnt = corsaro_get_dropped_packets(corsaro);
415 
416  gettimeofday_wrap(&ts);
417 
418  bytes_out += corsaro_file_printf(corsaro, file,
419  "# CORSARO_PACKETCNT %"PRIu64"\n",
420  corsaro->packet_cnt);
421  if(acnt != UINT64_MAX)
422  {
423  bytes_out += corsaro_file_printf(corsaro, file,
424  "# CORSARO_ACCEPTEDCNT %"PRIu64"\n",
425  acnt);
426  }
427  if(dcnt != UINT64_MAX)
428  {
429  bytes_out += corsaro_file_printf(corsaro, file,
430  "# CORSARO_DROPPEDCNT %"PRIu64"\n",
431  dcnt);
432  }
433  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_FIRSTPKT %ld\n",
434  corsaro->first_ts.tv_sec);
435  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_LASTPKT %ld\n",
436  corsaro->last_ts.tv_sec);
437  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_FINALTIME %ld\n",
438  ts.tv_sec);
439  bytes_out += corsaro_file_printf(corsaro, file, "# CORSARO_RUNTIME %ld\n",
440  ts.tv_sec-corsaro->init_time.tv_sec);
441 
442  corsaro_log(__func__, corsaro, "pkt cnt: %"PRIu64, corsaro->packet_cnt);
443 
444  return bytes_out;
445 }
446 
447 static int output_trailers_binary(corsaro_t *corsaro, corsaro_file_t *file,
448  corsaro_trailer_t *trailer)
449 {
450  uint8_t buff[CORSARO_IO_TRAILER_BYTE_LEN];
451  uint8_t *ptr = &buff[0];
452 
453  struct timeval ts;
454  gettimeofday_wrap(&ts);
455 
457  ptr+=4;
458 
459  bytes_htonl(ptr, CORSARO_MAGIC_TRAILER);
460  ptr+=4;
461 
462  bytes_htonll(ptr, corsaro->packet_cnt);
463  ptr+=8;
464 
466  ptr+=8;
467 
469  ptr+=8;
470 
471  bytes_htonl(ptr, corsaro->first_ts.tv_sec);
472  ptr+=4;
473 
474  bytes_htonl(ptr, corsaro->last_ts.tv_sec);
475  ptr+=4;
476 
477  bytes_htonl(ptr, ts.tv_sec);
478  ptr+=4;
479 
480  bytes_htonl(ptr, ts.tv_sec-corsaro->init_time.tv_sec);
481 
482  if(corsaro_file_write(corsaro, file, &buff[0], CORSARO_IO_TRAILER_BYTE_LEN) !=
484  {
485  corsaro_log(__func__, corsaro, "could not dump trailers to file");
486  return -1;
487  }
488 
489  corsaro_log(__func__, corsaro, "pkt cnt: %"PRIu64, corsaro->packet_cnt);
491 }
492 
493 static char *stradd(const char *str, char *bufp, char *buflim)
494 {
495  while(bufp < buflim && (*bufp = *str++) != '\0')
496  {
497  ++bufp;
498  }
499  return bufp;
500 }
501 
502 static char *generate_file_name(corsaro_t *corsaro, const char *plugin,
503  corsaro_interval_t *interval,
504  corsaro_file_compress_t compress)
505 {
506  /* some of the structure of this code is borrowed from the
507  FreeBSD implementation of strftime */
508 
509  /* the output buffer */
510  /* @todo change the code to dynamically realloc this if we need more
511  space */
512  char buf[1024];
513  char tbuf[1024];
514  char *bufp = buf;
515  char *buflim = buf+sizeof(buf);
516 
517  char *tmpl = corsaro->template;
518  char secs[11]; /* length of UINT32_MAX +1 */
519  struct timeval tv;
520 
521  for(; *tmpl; ++tmpl)
522  {
523  if(*tmpl == '.' && compress == CORSARO_FILE_COMPRESS_NONE)
524  {
525  if(strncmp(tmpl, CORSARO_FILE_ZLIB_SUFFIX,
526  strlen(CORSARO_FILE_ZLIB_SUFFIX)) == 0 ||
527  strncmp(tmpl, CORSARO_FILE_BZ2_SUFFIX,
528  strlen(CORSARO_FILE_BZ2_SUFFIX)) == 0)
529  {
530  break;
531  }
532  }
533  else if(*tmpl == '%')
534  {
535  switch(*++tmpl)
536  {
537  case '\0':
538  --tmpl;
539  break;
540 
541  /* BEWARE: if you add a new pattern here, you must also add it to
542  * corsaro_io_template_has_timestamp */
543 
545  bufp = stradd(corsaro->monitorname, bufp, buflim);
546  continue;
547 
549  bufp = stradd(plugin, bufp, buflim);
550  continue;
551 
552  case 's':
553  if(interval != NULL)
554  {
555  snprintf(secs, sizeof(secs), "%"PRIu32, interval->time);
556  bufp = stradd(secs, bufp, buflim);
557  continue;
558  }
559  /* fall through */
560  default:
561  /* we want to be generous and leave non-recognized formats
562  intact - especially for strftime to use */
563  --tmpl;
564  }
565  }
566  if (bufp == buflim)
567  break;
568  *bufp++ = *tmpl;
569  }
570 
571  *bufp = '\0';
572 
573  /* now let strftime have a go */
574  if(interval != NULL)
575  {
576  tv.tv_sec = interval->time;
577  strftime(tbuf, sizeof(tbuf), buf, gmtime(&tv.tv_sec));
578  return strdup(tbuf);
579  }
580 
581  return strdup(buf);
582 }
583 
584 static int validate_header_static(corsaro_header_t *h)
585 {
586  /* WARNING: do not try and access past traceuri_len */
587 
588  /* byteswap the values */
589  h->corsaro_magic = ntohl(h->corsaro_magic);
590  h->magic = ntohl(h->magic);
591  h->local_init_time = ntohl(h->local_init_time);
592  h->interval_length = ntohs(h->interval_length);
593  h->traceuri_len = ntohs(h->traceuri_len);
594 
595  /* do some sanity checking on the interval */
596  if(h->corsaro_magic != CORSARO_MAGIC ||
598  {
599  return 0;
600  }
601  return 1;
602 }
603 
604 static int validate_interval(corsaro_interval_t *interval)
605 {
606  /* byteswap the values */
607  interval->corsaro_magic = ntohl(interval->corsaro_magic);
608  interval->magic = ntohl(interval->magic);
609  interval->number = ntohs(interval->number);
610  interval->time = ntohl(interval->time);
611 
612  /* do some sanity checking on the interval */
613  if(interval->corsaro_magic != CORSARO_MAGIC ||
614  interval->magic != CORSARO_MAGIC_INTERVAL)
615  {
616  return 0;
617  }
618  return 1;
619 }
620 
621 static int validate_plugin_data(corsaro_plugin_data_t *pd)
622 {
623  /* byteswap the values */
624  pd->corsaro_magic = ntohl(pd->corsaro_magic);
625  pd->magic = ntohl(pd->magic);
626  pd->plugin_magic = ntohl(pd->plugin_magic);
627 
628  /* do some sanity checking on the interval */
629  if(pd->corsaro_magic != CORSARO_MAGIC ||
630  pd->magic != CORSARO_MAGIC_DATA)
631  {
632  return 0;
633  }
634  return 1;
635 }
636 
637 static int validate_trailer(corsaro_trailer_t *t)
638 {
639  /* WARNING: do not try and access past traceuri_len */
640 
641  /* byteswap the values */
642  t->corsaro_magic = ntohl(t->corsaro_magic);
643  t->magic = ntohl(t->magic);
644  t->packet_cnt = ntohll(t->packet_cnt);
646  t->dropped_cnt = ntohll(t->dropped_cnt);
647  t->first_packet_time = ntohl(t->first_packet_time);
648  t->last_packet_time = ntohl(t->last_packet_time);
649  t->local_final_time = ntohl(t->local_final_time);
650  t->runtime = ntohl(t->runtime);
651 
652  /* do some sanity checking on the interval */
653  if(t->corsaro_magic != CORSARO_MAGIC ||
654  t->magic != CORSARO_MAGIC_TRAILER)
655  {
656  return 0;
657  }
658  return 1;
659 }
660 
661 off_t read_plugin_data(corsaro_in_t *corsaro,
662  corsaro_file_in_t *file,
663  corsaro_in_record_type_t *record_type,
665 {
666  off_t bread = -1;
667  if((bread = corsaro_io_read_bytes(corsaro, record,
668  sizeof(corsaro_plugin_data_t))) !=
669  sizeof(corsaro_plugin_data_t))
670  {
671  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
672  return -1;
673  }
674 
675  if(validate_plugin_data((corsaro_plugin_data_t *)record->buffer) != 1)
676  {
677  corsaro_log_in(__func__, corsaro, "could not validate plugin data");
678  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
679  return -1;
680  }
681 
682  assert(bread == sizeof(corsaro_plugin_data_t));
683  return bread;
684 }
685 
686 /* == EXPORTED FUNCTIONS BELOW THIS POINT == */
687 
689  const char *plugin_name,
690  corsaro_interval_t *interval)
691 {
692  return corsaro_io_prepare_file_full(corsaro, plugin_name, interval,
693  corsaro->output_mode,
694  corsaro->compress,
695  corsaro->compress_level,
696  O_CREAT);
697 }
698 
700  const char *plugin_name,
701  corsaro_interval_t *interval,
702  corsaro_file_mode_t mode,
703  corsaro_file_compress_t compress,
704  int compress_level,
705  int flags)
706 {
707  corsaro_file_t *f = NULL;
708  char *outfileuri;
709 
710  /* generate a file name based on the plugin name */
711  if((outfileuri = generate_file_name(corsaro, plugin_name,
712  interval, compress)) == NULL)
713  {
714  corsaro_log(__func__, corsaro, "could not generate file name for %s",
715  plugin_name);
716  return NULL;
717  }
718 
719  if((f = corsaro_file_open(corsaro,
720  outfileuri,
721  mode,
722  compress,
723  compress_level,
724  flags)) == NULL)
725  {
726  corsaro_log(__func__, corsaro, "could not open %s for writing",
727  outfileuri);
728  return NULL;
729  }
730 
731  free(outfileuri);
732  return f;
733 }
734 
735 int corsaro_io_validate_template(corsaro_t *corsaro, char *template)
736 {
737  /* be careful using corsaro here, it is likely not initialized fully */
738 
739  /* check for length first */
740  if(template == NULL)
741  {
742  corsaro_log(__func__, corsaro, "output template must be set");
743  return 0;
744  }
745 
746  /* check that the plugin pattern is in the template */
747  if(strstr(template, CORSARO_IO_PLUGIN_PATTERN_STR) == NULL)
748  {
749  corsaro_log(__func__, corsaro, "template string must contain %s",
751  return 0;
752  }
753 
754  /* we're good! */
755  return 1;
756 }
757 
759 {
760  char *p = corsaro->template;
761  assert(corsaro->template);
762  /* be careful using corsaro here, this is called pre-start */
763 
764  /* the easiest (but not easiest to maintain) way to do this is to step through
765  * each '%' character in the string and check what is after it. if it is
766  * anything other than P (for plugin) or N (for monitor name), then it is a
767  * timestamp. HOWEVER. If new corsaro-specific patterns are added, they must
768  * also be added here. gross */
769 
770  for(; *p; ++p)
771  {
772  if(*p == '%')
773  {
774  /* BEWARE: if you add a new pattern here, you must also add it to
775  * generate_file_name */
776  if(*(p+1) != CORSARO_IO_MONITOR_PATTERN &&
777  *(p+1) != CORSARO_IO_PLUGIN_PATTERN)
778  {
779  return 1;
780  }
781  }
782  }
783  return 0;
784 
785 }
786 
788  corsaro_header_t *header)
789 {
790  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
792  return output_funcs[CORSARO_FILE_MODE(file)].headers(corsaro, file, header);
793 }
794 
796  corsaro_header_t *header)
797 {
798  int i;
799 
800  fprintf(stdout, "# CORSARO_VERSION %"PRIu8".%"PRIu8"\n",
801  header->version_major, header->version_minor);
802  fprintf(stdout, "# CORSARO_INITTIME %"PRIu32"\n",
803  header->local_init_time);
804  fprintf(stdout, "# CORSARO_INTERVAL %"PRIu16"\n", header->interval_length);
805 
806  if(header->traceuri != NULL)
807  {
808  fprintf(stdout, "# CORSARO_TRACEURI %s\n", header->traceuri);
809  }
810  for(i=0;i<header->plugin_cnt;i++)
811  {
812  fprintf(stdout, "# CORSARO_PLUGIN %s\n",
814  plugin_manager,
815  header->plugin_magics[i]
816  )
817  );
818  }
819 
820 }
821 
823  corsaro_trailer_t *trailer)
824 {
825  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
827  return output_funcs[CORSARO_FILE_MODE(file)].trailers(corsaro, file,
828  trailer);
829 }
830 
832 {
833  fprintf(stdout, "# CORSARO_PACKETCNT %"PRIu64"\n",
834  trailer->packet_cnt);
835  if(trailer->accepted_cnt != UINT64_MAX)
836  {
837  fprintf(stdout, "# CORSARO_ACCEPTEDCNT %"PRIu64"\n",
838  trailer->accepted_cnt);
839  }
840  if(trailer->dropped_cnt != UINT64_MAX)
841  {
842  fprintf(stdout, "# CORSARO_DROPPEDCNT %"PRIu64"\n",
843  trailer->dropped_cnt);
844  }
845  fprintf(stdout, "# CORSARO_FIRSTPKT %"PRIu32"\n",
846  trailer->first_packet_time);
847  fprintf(stdout, "# CORSARO_LASTPKT %"PRIu32"\n",
848  trailer->last_packet_time);
849  fprintf(stdout, "# CORSARO_FINALTIME %"PRIu32"\n",
850  trailer->local_final_time);
851  fprintf(stdout, "# CORSARO_RUNTIME %"PRIu32"\n",
852  trailer->runtime);
853 }
854 
855 
857  corsaro_interval_t *int_start)
858 {
859  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
861  return output_funcs[CORSARO_FILE_MODE(file)].interval_start(corsaro, file,
862  int_start);
863 }
864 
866 {
867  fprintf(stdout,
868  "# CORSARO_INTERVAL_START %d %"PRIu32"\n",
869  int_start->number, int_start->time);
870 }
871 
873  corsaro_interval_t *int_end)
874 {
875  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
877  return (output_funcs[CORSARO_FILE_MODE(file)].interval_end)(corsaro, file,
878  int_end);
879 }
880 
882 {
883  fprintf(stdout,
884  "# CORSARO_INTERVAL_END %d %"PRIu32"\n",
885  int_end->number,
886  int_end->time);
887 }
888 
890  corsaro_plugin_t *plugin)
891 {
892  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
894  assert(plugin != NULL);
895 
896  return (output_funcs[CORSARO_FILE_MODE(file)].plugin_start)(corsaro, file,
897  plugin);
898 }
899 
900 void corsaro_io_print_plugin_start(corsaro_plugin_t *plugin)
901 {
902  fprintf(stdout, "# CORSARO_PLUGIN_DATA_START %s\n", plugin->name);
903 }
904 
906  corsaro_plugin_t *plugin)
907 {
908  assert(CORSARO_FILE_MODE(file) == CORSARO_FILE_MODE_ASCII ||
910  assert(plugin != NULL);
911 
912  return (output_funcs[CORSARO_FILE_MODE(file)].plugin_end)(corsaro, file,
913  plugin);
914 }
915 
916 void corsaro_io_print_plugin_end(corsaro_plugin_t *plugin)
917 {
918  fprintf(stdout, "# CORSARO_PLUGIN_DATA_END %s\n", plugin->name);
919 }
920 
926  corsaro_in_record_type_t record_type,
927  corsaro_in_record_t *record)
928 {
929  corsaro_plugin_t *plugin = NULL;
930  corsaro_plugin_data_t *data = NULL;
931 
932  switch(record_type)
933  {
935  /*
936  corsaro_log(__func__, corsaro, "refusing to write null record to file");
937  */
938  return 0;
939  break;
940 
942  return corsaro_io_write_header(corsaro, file,
943  (corsaro_header_t *)record->buffer);
944  break;
946  return corsaro_io_write_trailer(corsaro, file,
947  (corsaro_trailer_t *)record->buffer);
948  break;
949 
951  return corsaro_io_write_interval_start(corsaro, file,
952  (corsaro_interval_t *)record->buffer);
953  break;
954 
956  return corsaro_io_write_interval_end(corsaro, file,
957  (corsaro_interval_t *)record->buffer);
958  break;
959 
961  data = (corsaro_plugin_data_t*)record->buffer;
962  if((plugin = corsaro_plugin_get_by_magic(corsaro->plugin_manager,
963  data->plugin_magic)) == NULL)
964  {
965  corsaro_log(__func__, corsaro, "invalid plugin magic detected");
966  corsaro_log(__func__, corsaro, "is corsaro built with all"
967  "necessary plugins?");
968  return 0;
969  }
970  return corsaro_io_write_plugin_start(corsaro, file, plugin);
971  break;
972 
974  data = (corsaro_plugin_data_t*)record->buffer;
975  if((plugin = corsaro_plugin_get_by_magic(corsaro->plugin_manager,
976  data->plugin_magic)) == NULL)
977  {
978  corsaro_log(__func__, corsaro, "invalid plugin magic detected");
979  corsaro_log(__func__, corsaro, "is corsaro built with all"
980  "necessary plugins?");
981  return 0;
982  }
983  return corsaro_io_write_plugin_end(corsaro, file, plugin);
984  break;
985 
989 #ifdef WITH_PLUGIN_SIXT
990  return corsaro_flowtuple_record_fprint(corsaro, file, record_type, record);
991 #else
992  corsaro_log(__func__, corsaro, "corsaro is not built with flowtuple support");
993  return 0;
994 #endif
995  break;
996 
1000 #ifdef WITH_PLUGIN_DOS
1001  return corsaro_dos_record_fprint(corsaro, file, record_type, record);
1002 #else
1003  corsaro_log(__func__, corsaro, "corsaro is not built with dos support");
1004  return 0;
1005 #endif
1006  break;
1007 
1008  default:
1009  corsaro_log(__func__, corsaro, "invalid record type %d\n", (int)record_type);
1010  return 0;
1011  }
1012 
1013  return -1;
1014 }
1015 
1017  corsaro_in_record_type_t record_type,
1018  corsaro_in_record_t *record)
1019 {
1020  corsaro_plugin_t *plugin = NULL;
1021  corsaro_plugin_data_t *data = NULL;
1022 
1023  switch(record_type)
1024  {
1026  /*
1027  fprintf(stderr, "refusing to write null record to file");
1028  */
1029  break;
1030 
1032  corsaro_io_print_header(plugin_manager,
1033  (corsaro_header_t *)record->buffer);
1034  break;
1035 
1038  break;
1039 
1042  break;
1043 
1046  break;
1047 
1049  data = (corsaro_plugin_data_t*)record->buffer;
1050  if((plugin = corsaro_plugin_get_by_magic(plugin_manager,
1051  data->plugin_magic)) == NULL)
1052  {
1053  fprintf(stderr, "invalid plugin magic detected\n");
1054  fprintf(stderr, "is corsaro built with all"
1055  "necessary plugins?\n");
1056  return 0;
1057  }
1058  corsaro_io_print_plugin_start(plugin);
1059  break;
1060 
1062  data = (corsaro_plugin_data_t*)record->buffer;
1063  if((plugin = corsaro_plugin_get_by_magic(plugin_manager,
1064  data->plugin_magic)) == NULL)
1065  {
1066  fprintf(stderr, "invalid plugin magic detected\n");
1067  fprintf(stderr, "is corsaro built with all"
1068  "necessary plugins?\n");
1069  return 0;
1070  }
1071  corsaro_io_print_plugin_end(plugin);
1072  break;
1073 
1077 #ifdef WITH_PLUGIN_SIXT
1078  return corsaro_flowtuple_record_print(record_type, record);
1079 #else
1080  fprintf(stdout, "corsaro is not built with flowtuple support\n");
1081  return 0;
1082 #endif
1083  break;
1084 
1088 #ifdef WITH_PLUGIN_DOS
1089  return corsaro_dos_record_print(record_type, record);
1090 #else
1091  fprintf(stdout, "corsaro is not built with dos support\n");
1092  return 0;
1093 #endif
1094  break;
1095 
1096  default:
1097  fprintf(stderr, "invalid record type %d\n", (int)record_type);
1098  return -1;
1099  }
1100 
1101  return 0;
1102 }
1103 
1104 /* ==== INPUT FUNCTIONS ==== */
1105 
1107  corsaro_in_record_type_t *record_type,
1108  corsaro_in_record_t *record)
1109 {
1110  off_t bread;
1111  off_t bsbread = CORSARO_IO_HEADER_FIXED_BYTE_LEN+sizeof(uint16_t);
1112  corsaro_header_t *header;
1113  off_t offset = sizeof(corsaro_header_t);
1114  int i;
1115 
1116  /* read the static portion of the header */
1117  if((bread = corsaro_io_read_bytes(corsaro, record, bsbread)) != bsbread)
1118  {
1119  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1120  return bread;
1121  }
1122 
1123  header = (corsaro_header_t*)record->buffer;
1124 
1125  if(validate_header_static(header) != 1)
1126  {
1127  corsaro_log_in(__func__, corsaro, "could not validate header");
1128  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1129  return -1;
1130  }
1131 
1132  if(header->traceuri_len == 0)
1133  {
1134  header->traceuri = NULL;
1135  }
1136  else
1137  {
1138  /* read the traceuri into the buffer */
1139  if((bread += corsaro_io_read_bytes_offset(corsaro, record,
1140  offset,
1141  header->traceuri_len)) !=
1142  (bsbread+=header->traceuri_len))
1143  {
1144  corsaro_log_in(__func__, corsaro,
1145  "failed to read traceuri from file");
1146  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1147  return bread;
1148  }
1149  header->traceuri = record->buffer+sizeof(corsaro_header_t);
1150  offset += header->traceuri_len;
1151  *(record->buffer+offset) = '\0';
1152  offset++;
1153  }
1154 
1155  /* now, read the plugin count */
1156  if((bread += corsaro_io_read_bytes_offset(corsaro, record,
1158  +sizeof(uint16_t)
1159  +sizeof(uint8_t*),
1160  sizeof(uint16_t))) !=
1161  (bsbread+=sizeof(uint16_t)))
1162  {
1163  corsaro_log_in(__func__, corsaro,
1164  "failed to read plugin count from file");
1165  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1166  return bread;
1167  }
1168 
1169  header->plugin_cnt = ntohs(header->plugin_cnt);
1170 
1171  if(header->plugin_cnt == 0)
1172  {
1173  header->plugin_magics = NULL;
1174  }
1175  else
1176  {
1177  /* read the plugin array into the buffer */
1178  if((bread += corsaro_io_read_bytes_offset(corsaro, record,
1179  offset,
1180  sizeof(uint32_t)
1181  *header->plugin_cnt)) !=
1182  (bsbread+=sizeof(uint32_t)*header->plugin_cnt))
1183  {
1184  corsaro_log_in(__func__, corsaro,
1185  "failed to read plugin magics from file");
1186  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1187  return bread;
1188  }
1189  header->plugin_magics = (uint32_t*)(record->buffer+offset);
1190  }
1191 
1192  for(i =0; i<header->plugin_cnt;i++)
1193  {
1194  header->plugin_magics[i] = ntohl(header->plugin_magics[i]);
1195  }
1196 
1197  assert(bread == CORSARO_IO_HEADER_FIXED_BYTE_LEN+sizeof(uint16_t)
1198  +header->traceuri_len+sizeof(uint16_t)
1199  +(header->plugin_cnt*sizeof(uint32_t)));
1200 
1201  *record_type = CORSARO_IN_RECORD_TYPE_IO_HEADER;
1202 
1203  return bread;
1204 }
1205 
1207  corsaro_in_record_type_t *record_type,
1208  corsaro_in_record_t *record)
1209 {
1210  off_t bytes_read;
1211 
1212  if((bytes_read = corsaro_io_read_bytes(corsaro, record,
1213  sizeof(corsaro_trailer_t))) !=
1214  sizeof(corsaro_trailer_t))
1215  {
1216  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1217  return bytes_read;
1218  }
1219 
1220  if(validate_trailer((corsaro_trailer_t *)record->buffer) != 1)
1221  {
1222  corsaro_log_in(__func__, corsaro, "could not validate trailer");
1223  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1224  return -1;
1225  }
1226 
1227  *record_type = CORSARO_IN_RECORD_TYPE_IO_TRAILER;
1228 
1229  return bytes_read;
1230 }
1231 
1233  corsaro_in_record_type_t *record_type,
1234  corsaro_in_record_t *record)
1235 {
1236  off_t bread;
1237 
1238  if((bread = corsaro_io_read_bytes(corsaro, record,
1241  {
1242  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1243  return bread;
1244  }
1245 
1246  if(validate_interval((corsaro_interval_t *)record->buffer) != 1)
1247  {
1248  corsaro_log_in(__func__, corsaro, "could not validate interval");
1249  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1250  return -1;
1251  }
1252 
1253  assert(bread == CORSARO_IO_INTERVAL_HEADER_BYTE_LEN);
1254 
1256 
1257  return bread;
1258 }
1259 
1261  corsaro_in_record_type_t *record_type,
1262  corsaro_in_record_t *record)
1263 {
1264  off_t bread;
1265 
1266  if((bread = corsaro_io_read_bytes(corsaro, record,
1269  {
1270  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1271  return bread;
1272  }
1273 
1274  if(validate_interval((corsaro_interval_t *)record->buffer) != 1)
1275  {
1276  corsaro_log_in(__func__, corsaro, "could not validate interval");
1277  *record_type = CORSARO_IN_RECORD_TYPE_NULL;
1278  return -1;
1279  }
1280 
1281  assert(bread == CORSARO_IO_INTERVAL_HEADER_BYTE_LEN);
1282 
1284 
1285  return bread;
1286 }
1287 
1289  corsaro_file_in_t *file,
1290  corsaro_in_record_type_t *record_type,
1291  corsaro_in_record_t *record)
1292 {
1293  off_t bread = read_plugin_data(corsaro, file, record_type, record);
1294 
1295  if(bread > 0)
1296  {
1298  }
1299 
1300  return bread;
1301 }
1302 
1304  corsaro_in_record_type_t *record_type,
1305  corsaro_in_record_t *record)
1306 {
1307  off_t bread = read_plugin_data(corsaro, file, record_type, record);
1308 
1309  if(bread > 0)
1310  {
1311  *record_type = CORSARO_IN_RECORD_TYPE_IO_PLUGIN_END;
1312  }
1313 
1314  return bread;
1315 }
1316 
1318  off_t len)
1319 {
1320  /* fix this with a realloc later? */
1321  assert(record->buffer_len >= len);
1322 
1323  return corsaro_file_rread(corsaro->file, record->buffer, len);
1324 }
1325 
1327  corsaro_in_record_t *record,
1328  off_t offset, off_t len)
1329 {
1330  /* fix this with a realloc later? */
1331  assert(record->buffer_len >= offset+len);
1332 
1333  return corsaro_file_rread(corsaro->file,
1334  (record->buffer)+offset, len);
1335 }
Structure representing the start or end of an interval.
Definition: corsaro_int.h:156
The start of a plugin data section.
Definition: corsaro.h:118
An opaque structure defining an corsaro input file.
Definition: corsaro_file.h:86
uint32_t last_packet_time
The trace time of the last packet (seconds)
Definition: corsaro_int.h:131
off_t corsaro_io_write_record(corsaro_t *corsaro, corsaro_file_t *file, corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Write a generic corsaro record to the file.
Definition: corsaro_io.c:925
#define CORSARO_FILE_BZ2_SUFFIX
The suffix used to detect bzip output is desired.
Definition: corsaro_file.h:57
int interval
The number of seconds after which plugins will be asked to dump data.
Definition: corsaro_int.h:280
off_t corsaro_io_read_plugin_start(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the appropriate plugin header from the file.
Definition: corsaro_io.c:1288
const char * name
The name of this plugin used in the ascii output and eventually to allow plugins to be enabled and di...
Header file dealing with the corsaro plugin manager.
int compress_level
The compression level (ignored if not compressing)
Definition: corsaro_int.h:254
Header file which exports the public libcorsaro API.
void corsaro_io_print_interval_start(corsaro_interval_t *int_start)
Write the interval headers to stdout.
Definition: corsaro_io.c:865
void bytes_htonl(uint8_t *bytes, uint32_t u32)
Convert a host ordered long to a network ordered byte array.
Definition: utils.c:57
Header file dealing with the corsaro logging sub-system.
Structure representing the start or end of a plugin data block.
Definition: corsaro_int.h:172
uint64_t packet_cnt
The total number of packets that have been processed.
Definition: corsaro_int.h:307
An opaque structure defining an corsaro output file.
Definition: corsaro_file.h:60
off_t corsaro_file_printf(struct corsaro *corsaro, corsaro_file_t *file, const char *format,...)
Print a string to an corsaro file.
Definition: corsaro_file.c:214
struct timeval init_time
The local wall time that corsaro was started at.
Definition: corsaro_int.h:233
uint16_t traceuri_len
The length of the (optional) trace uri string.
Definition: corsaro_int.h:103
A reusable opaque structure for corsaro to read an input record into.
Definition: corsaro_int.h:350
corsaro_file_mode_t output_mode
The default output mode for new files.
Definition: corsaro_int.h:248
void corsaro_io_print_interval_end(corsaro_interval_t *int_end)
Write the interval trailers to stdout.
Definition: corsaro_io.c:881
#define CORSARO_IO_PLUGIN_PATTERN
The character to replace with the name of the plugin.
Definition: corsaro_io.h:56
ASCII IO mode.
Definition: corsaro.h:153
Binary IO mode.
Definition: corsaro.h:155
uint32_t corsaro_magic
The global corsaro magic number.
Definition: corsaro_int.h:175
size_t buffer_len
The length of the buffer.
Definition: corsaro_int.h:359
uint32_t * plugin_magics
A pointer to the list of plugin magic numbers used.
Definition: corsaro_int.h:109
corsaro_file_t * corsaro_io_prepare_file_full(corsaro_t *corsaro, const char *plugin_name, corsaro_interval_t *interval, corsaro_file_mode_t mode, corsaro_file_compress_t compress, int compress_level, int flags)
Uses the given settings to open an corsaro file for the given plugin.
Definition: corsaro_io.c:699
uint32_t magic
The interval magic number.
Definition: corsaro_int.h:161
off_t corsaro_flowtuple_record_fprint(corsaro_t *corsaro, corsaro_file_t *file, corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print a record to a file in ASCII format.
off_t corsaro_io_read_plugin_end(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the appropriate plugin trailer from the file.
Definition: corsaro_io.c:1303
off_t corsaro_io_read_trailer(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the corsaro trailers from the file.
Definition: corsaro_io.c:1206
uint32_t plugin_magic
The plugin magic.
Definition: corsaro_int.h:179
#define CORSARO_FILE_MODE(file)
Accessor macro for getting the mode of a file.
Definition: corsaro_file.h:113
int corsaro_dos_record_print(corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print any DoS record to a file in ASCII format.
Definition: corsaro_dos.c:1611
corsaro_plugin_t * corsaro_plugin_next(corsaro_plugin_manager_t *manager, corsaro_plugin_t *plugin)
Retrieve the next plugin in the list.
uint16_t plugins_cnt
The number of active plugins.
uint32_t corsaro_magic
The global corsaro magic number.
Definition: corsaro_int.h:159
struct timeval last_ts
The time of the most recent packet seen by corsaro.
Definition: corsaro_int.h:301
Header file which exports corsaro_flowtuple plugin API.
void corsaro_io_print_trailer(corsaro_trailer_t *trailer)
Write the corsaro trailers to stdout.
Definition: corsaro_io.c:831
corsaro_plugin_t * corsaro_plugin_get_by_magic(corsaro_plugin_manager_t *manager, uint32_t id)
Attempt to retrieve a plugin by magic number (not by using magic)
uint16_t number
The interval number (starts at 0)
Definition: corsaro_int.h:163
corsaro header magic number - "HEAD"
Definition: corsaro_int.h:75
off_t corsaro_io_read_bytes_offset(corsaro_in_t *corsaro, corsaro_in_record_t *record, off_t offset, off_t len)
Read the given number of bytes into the record buffer at the given offset.
Definition: corsaro_io.c:1326
uint32_t corsaro_magic
The global corsaro magic number.
Definition: corsaro_int.h:91
uint16_t * plugins_enabled
An array of plugin ids that have been enabled by the user.
struct corsaro_header corsaro_header_t
Opaque struct representing a corsaro file header.
Definition: corsaro.h:58
Header file for common utility functions.
struct corsaro_plugin_data corsaro_plugin_data_t
Opaque struct representing the start of end of a plugin data block.
Definition: corsaro.h:64
corsaro_plugin_manager_t * plugin_manager
A pointer to the corsaro plugin manager state.
Definition: corsaro_int.h:273
Structure representing a corsaro file header.
Definition: corsaro_int.h:88
Holds the metadata for the plugin manager.
Overall corsaro magic number - "EDGR".
Definition: corsaro_int.h:73
Structure representing a corsaro file trailer.
Definition: corsaro_int.h:116
corsaro_file_t * corsaro_io_prepare_file(corsaro_t *corsaro, const char *plugin_name, corsaro_interval_t *interval)
Uses the current settings to open an corsaro file for the given plugin.
Definition: corsaro_io.c:688
off_t corsaro_io_read_interval_end(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the appropriate interval trailers from the file.
Definition: corsaro_io.c:1260
uint16_t plugin_cnt
The number of plugins used.
Definition: corsaro_int.h:107
#define CORSARO_IO_HEADER_FIXED_BYTE_LEN
Length of the fixed part of the global corsaro header.
Definition: corsaro_io.h:45
uint32_t first_packet_time
The trace time of the first packet (seconds)
Definition: corsaro_int.h:129
The corsaro_flowtuple flowtuple record.
Definition: corsaro.h:134
uint64_t packet_cnt
The total number of packets that corsaro processed.
Definition: corsaro_int.h:123
off_t corsaro_io_write_interval_end(corsaro_t *corsaro, corsaro_file_t *file, corsaro_interval_t *int_end)
Write the appropriate interval trailers to the file.
Definition: corsaro_io.c:872
#define CORSARO_IO_MONITOR_PATTERN
The character to replace with the monitor name.
Definition: corsaro_io.h:61
const char * corsaro_plugin_get_name_by_magic(corsaro_plugin_manager_t *manager, uint32_t magic)
Get the name of a plugin given it's magic number.
The end of an interval.
Definition: corsaro.h:115
uint64_t corsaro_get_accepted_packets(corsaro_t *corsaro)
Accessor function to get the number of accepted packets in this interval.
Definition: corsaro.c:1188
uint32_t magic
The trailer magic number.
Definition: corsaro_int.h:121
Header file dealing with the low-level file IO.
off_t corsaro_io_write_header(corsaro_t *corsaro, corsaro_file_t *file, corsaro_header_t *header)
Write the corsaro headers to the file.
Definition: corsaro_io.c:787
static int interval
The amount of time to wait until we dump the hash.
uint64_t accepted_cnt
The number of packets libtrace reports as accepted.
Definition: corsaro_int.h:125
static corsaro_in_record_t * record
A pointer to a corsaro record.
Definition: corsaro_main.c:76
void corsaro_log_in(const char *func, corsaro_in_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro input object.
Definition: corsaro_log.c:121
void bytes_htons(uint8_t *bytes, uint16_t u16)
Convert a host ordered short to a network ordered byte array.
Definition: utils.c:50
off_t corsaro_io_read_interval_start(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the appropriate interval headers from the file.
Definition: corsaro_io.c:1232
int corsaro_io_template_has_timestamp(corsaro_t *corsaro)
Determines whether there are any time-related patterns in the file template.
Definition: corsaro_io.c:758
The overall corsaro trailer (currently only in global)
Definition: corsaro.h:109
struct corsaro_interval corsaro_interval_t
Opaque struct representing the start or end of an interval.
Definition: corsaro.h:62
off_t corsaro_io_read_bytes(corsaro_in_t *corsaro, corsaro_in_record_t *record, off_t len)
Read the given number of bytes into the record.
Definition: corsaro_io.c:1317
int corsaro_io_print_record(corsaro_plugin_manager_t *plugin_manager, corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print a generic corsaro record to stdout.
Definition: corsaro_io.c:1016
#define CORSARO_FILE_ZLIB_SUFFIX
The suffix used to detect gzip output is desired.
Definition: corsaro_file.h:54
enum corsaro_file_compress corsaro_file_compress_t
Supported compression types (must be kept in sync with wandio)
int corsaro_io_validate_template(corsaro_t *corsaro, char *template)
Validates a output file template for needed features.
Definition: corsaro_io.c:735
The corsaro_flowtuple flowtuple classification type end record.
Definition: corsaro.h:131
static corsaro_t * corsaro
A pointer to the instance of corsaro that we will drive.
Definition: corsaro_main.c:80
off_t corsaro_dos_record_fprint(corsaro_t *corsaro, corsaro_file_t *file, corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print any DoS record to stdout in ASCII format.
Definition: corsaro_dos.c:1578
uint8_t version_major
The corsaro major version number.
Definition: corsaro_int.h:95
off_t corsaro_file_rread(corsaro_file_in_t *file, void *buffer, off_t len)
Reads from an corsaro input file into the provided buffer.
Definition: corsaro_file.c:332
uint8_t * buffer
The buffer to read the record into.
Definition: corsaro_int.h:356
off_t corsaro_io_write_plugin_end(corsaro_t *corsaro, corsaro_file_t *file, corsaro_plugin_t *plugin)
Write the appropriate plugin trailer to the file.
Definition: corsaro_io.c:905
uint32_t local_init_time
The local time that corsaro was started at.
Definition: corsaro_int.h:99
corsaro interval magic number - "INTR"
Definition: corsaro_int.h:77
uint16_t interval_length
The interval length (seconds)
Definition: corsaro_int.h:101
uint32_t time
The time this interval started/ended.
Definition: corsaro_int.h:165
uint8_t * traceuri
A pointer to the traceuri string.
Definition: corsaro_int.h:105
int corsaro_flowtuple_record_print(corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print a record to stdout in ASCII format.
off_t corsaro_file_write(struct corsaro *corsaro, corsaro_file_t *file, const void *buffer, off_t len)
Writes the contents of a buffer using an corsaro output file.
Definition: corsaro_file.c:142
char * monitorname
The name of the monitor that corsaro is running on.
Definition: corsaro_int.h:242
The end of a plugin data section.
Definition: corsaro.h:121
Corsaro input state.
Definition: corsaro_int.h:323
void corsaro_io_print_header(corsaro_plugin_manager_t *plugin_manager, corsaro_header_t *header)
Write the corsaro headers to stdout.
Definition: corsaro_io.c:795
The corsaro_flowtuple flowtuple classification type start record.
Definition: corsaro.h:128
uint32_t corsaro_magic
The global corsaro magic number.
Definition: corsaro_int.h:119
The start of an interval.
Definition: corsaro.h:112
The overall corsaro header (currently only in global)
Definition: corsaro.h:106
char * uridata
The uri that was used to open the trace file.
Definition: corsaro_int.h:239
The corsaro_dos attack vector record.
Definition: corsaro.h:145
The corsaro_dos global header record.
Definition: corsaro.h:139
The corsaro_dos header record.
Definition: corsaro.h:142
uint32_t magic
The plugin data magic number.
Definition: corsaro_int.h:177
corsaro_file_t * corsaro_file_open(struct corsaro *corsaro, const char *filename, corsaro_file_mode_t mode, corsaro_file_compress_t compress_type, int compress_level, int flags)
Creates a new corsaro file write and opens the provided file for writing.
Definition: corsaro_file.c:59
corsaro_file_compress_t compress
The compression type (based on the file name)
Definition: corsaro_int.h:251
uint8_t version_minor
The corsaro minor version number.
Definition: corsaro_int.h:97
uint16_t plugins_enabled_cnt
The number of plugin ids in the plugins_enabled array.
#define CORSARO_IO_TRAILER_BYTE_LEN
Length of the corsaro trailer.
Definition: corsaro_io.h:52
Header file dealing with the corsaro file IO.
uint32_t local_final_time
The time that corsaro completed processing the trace.
Definition: corsaro_int.h:133
struct timeval first_ts
The time of the the first packet seen by corsaro.
Definition: corsaro_int.h:298
The null type used for wildcard matching.
Definition: corsaro.h:100
void bytes_htonll(uint8_t *bytes, uint64_t u64)
Convert a host ordered long-long (64 bit) to a network ordered byte array.
Definition: utils.c:64
void gettimeofday_wrap(struct timeval *tv)
Convenience function to get the current time of day.
Definition: utils.c:71
uint64_t corsaro_get_dropped_packets(corsaro_t *corsaro)
Accessor function to get the number of dropped packets in this interval.
Definition: corsaro.c:1195
Corsaro output state.
Definition: corsaro_int.h:230
No compression.
Definition: corsaro.h:169
#define CORSARO_IO_INTERVAL_HEADER_BYTE_LEN
Length of the interval header.
Definition: corsaro_io.h:48
uint32_t runtime
The number of (wall) seconds that corsaro took to process the trace.
Definition: corsaro_int.h:135
off_t corsaro_io_write_trailer(corsaro_t *corsaro, corsaro_file_t *file, corsaro_trailer_t *trailer)
Write the corsaro trailers to the file.
Definition: corsaro_io.c:822
off_t corsaro_io_write_interval_start(corsaro_t *corsaro, corsaro_file_t *file, corsaro_interval_t *int_start)
Write the appropriate interval headers to the file.
Definition: corsaro_io.c:856
uint64_t dropped_cnt
The number of packets libtrace reports as dropped.
Definition: corsaro_int.h:127
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
Definition: corsaro_log.c:113
enum corsaro_file_mode corsaro_file_mode_t
Enum of supported file modes.
const uint32_t magic
The magic number for this plugin's data.
char * template
The template used to create corsaro output files.
Definition: corsaro_int.h:245
off_t corsaro_io_write_plugin_start(corsaro_t *corsaro, corsaro_file_t *file, corsaro_plugin_t *plugin)
Write the appropriate plugin header to the file.
Definition: corsaro_io.c:889
off_t corsaro_io_read_header(corsaro_in_t *corsaro, corsaro_file_in_t *file, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read an corsaro header from the file.
Definition: corsaro_io.c:1106
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.
An corsaro packet processing plugin.
uint32_t magic
The header magic number.
Definition: corsaro_int.h:93
#define ntohll(x)
Byte-swap a 64-bit integer.
Definition: utils.h:66
#define CORSARO_IO_PLUGIN_PATTERN_STR
The pattern to replace in the output file name with the name of the plugin.
Definition: corsaro_io.h:58
corsaro_file_in_t * file
The corsaro input file to read data from.
Definition: corsaro_int.h:329
Header file dealing with the internal corsaro functions.