26 #include <arpa/inet.h>
55 KHASH_SET_INIT_INT64(64xx)
58 KHASH_INIT(sixt_map, corsaro_flowtuple_t*, kh_64xx_t*, 1,
62 KHASH_INIT(sixt_int, corsaro_flowtuple_t*, uint64_t, 1,
63 corsaro_flowtuple_hash_func, corsaro_flowtuple_hash_equal);
104 #define FIELD_ENABLED 1
171 fprintf(stderr,
"could not alloc corsaro_in\n");
178 fprintf(stderr,
"could not alloc record\n");
186 fprintf(stderr,
"could not start corsaro\n");
195 static int add_inc_map(
void *h, corsaro_flowtuple_t *t, uint32_t value)
197 kh_sixt_map_t *hash = (kh_sixt_map_t *)h;
200 corsaro_flowtuple_t *new_6t = NULL;
201 kh_64xx_t *val_map = NULL;
203 assert(hash != NULL);
209 if((khiter = kh_get(sixt_map, hash, t)) == kh_end(hash))
212 if((new_6t = malloc(
sizeof(corsaro_flowtuple_t))) == NULL)
219 memcpy(new_6t, t,
sizeof(corsaro_flowtuple_t));
222 khiter = kh_put(sixt_map, hash, new_6t, &khret);
225 val_map = kh_init(64xx);
228 kh_put(64xx, val_map, value, &khret);
231 kh_value(hash, khiter) = val_map;
236 kh_put(64xx, kh_value(hash, khiter), value, &khret);
242 int add_inc_hash(kh_sixt_int_t *hash, corsaro_flowtuple_t *t, uint32_t increment)
246 corsaro_flowtuple_t *new_6t = NULL;
248 assert(hash != NULL);
251 if((khiter = kh_get(sixt_int, hash, t)) == kh_end(hash))
254 if((new_6t = malloc(
sizeof(corsaro_flowtuple_t))) == NULL)
261 memcpy(new_6t, t,
sizeof(corsaro_flowtuple_t));
264 khiter = kh_put(sixt_int, hash, new_6t, &khret);
266 kh_value(hash, khiter) = increment;
272 assert((UINT64_MAX - kh_value(hash, khiter)) > increment);
274 kh_value(hash, khiter)+=increment;
288 assert(flowtuple != NULL);
290 tmp = flowtuple->src_ip;
291 inet_ntop(AF_INET,&tmp, &ip_a[0], 16);
293 inet_ntop(AF_INET, &tmp, &ip_b[0], 16);
295 fprintf(stdout,
"%s|%s"
297 "|%"PRIu8
"|%"PRIu8
"|0x%02"PRIx8
301 ntohs(flowtuple->src_port),
302 ntohs(flowtuple->dst_port),
305 flowtuple->tcp_flags,
306 ntohs(flowtuple->ip_len),
314 corsaro_flowtuple_t *key;
317 if(kh_size(hash) > 0)
319 for(k = kh_begin(hash); k != kh_end(hash); ++k)
321 if(kh_exist(hash, k))
323 key = kh_key(hash, k);
327 kh_destroy(64xx, kh_val(hash, k));
334 kh_clear(sixt_map, hash);
341 corsaro_flowtuple_t *key;
344 if(kh_size(hash) > 0)
346 for(k = kh_begin(hash); k != kh_end(hash); ++k)
348 if(kh_exist(hash, k))
350 key = kh_key(hash, k);
359 kh_clear(sixt_int, hash);
383 last_interval_end.
number++;
385 last_dump_end.
time = last_interval_end.
time+1;
399 value = ntohl(tuple->src_ip);
405 value = ntohs(tuple->src_port);
408 value = ntohs(tuple->dst_port);
411 value = tuple->protocol;
417 value = tuple->tcp_flags;
420 value = ntohs(tuple->ip_len);
423 value = ntohl(tuple->packet_cnt);
426 fprintf(stderr,
"ERROR: invalid value field number\n");
457 tuple->tcp_flags = 0;
463 tuple->packet_cnt = 0;
466 fprintf(stderr,
"ERROR: invalid field number\n");
479 fprintf(stderr,
"couldn't increment flowtuple packet_cnt value\n");
487 fprintf(stderr,
"could not add value to map");
501 corsaro_flowtuple_t *tuple;
503 fprintf(stderr,
"processing %s\n", file);
508 fprintf(stderr,
"failed to init corsaro\n");
514 if(last_interval_end.
time > 0)
527 if(interval_record->
time <= last_dump_end.
time)
529 fprintf(stderr,
"ERROR: decrease in timestamp.\n"
530 "Are the input files sorted properly?\n");
537 last_dump_end.
time = interval_record->
time;
542 if(last_interval_end.
time > 0)
567 last_interval_end.
time = interval_record->
time;
584 fprintf(stderr,
"corsaro_in_read_record failed to read record\n");
600 "usage: %s [-l] [-i interval] [-v value_field] [-f field]... [-F file_list] \n"
601 " flowtuple_file [flowtuple_file]\n"
602 " -l treat the input files as containing legacy format data\n"
603 " -i <interval> new distribution interval in seconds. (default: 0)\n"
604 " a value of -1 aggregates to a single interval\n"
605 " a value of 0 uses the original interval\n"
606 " -v <value> field to use as aggregation value (default: packet_cnt)\n"
607 " -f <field> a tuple field to re-aggregate with\n"
608 " -F <file_list> a file with the list flowtuple files\n"
609 " use '-' to read the list from standard input\n"
611 "Supported field names are:\n"
612 " src_ip, dst_ip, src_port, dst_port, protocol, ttl, tcp_flags, \n"
613 " ip_len, packet_cnt\n",
618 int main(
int argc,
char *argv[])
630 int wanted_n_fields = 0;
632 while((opt = getopt(argc, argv,
"li:f:F:v:?")) >= 0)
664 fprintf(stderr,
"a list of file has been already specified \n"
665 "this file is ignored: %s\n",optarg);
670 if(strcmp(optarg,
"-") == 0)
674 else if((flist = fopen(optarg,
"r")) == NULL)
676 fprintf(stderr,
"failed to open list of input files (%s)\n"
677 "NB: File List MUST be sorted\n", optarg);
685 fprintf(stderr,
"WARNING: Multiple value fields detected\n"
686 "Last specified will be used\n");
727 if(wanted_n_fields != field_cnt)
729 fprintf(stderr,
"Invalid field name\n");
736 fprintf(stderr,
"No value field specified. Defaulting to packet count\n");
743 sixt_v = kh_init(sixt_int);
747 sixt_f = kh_init(sixt_map);
754 while(fgets(file,
sizeof(file), flist) != NULL)
757 file[strlen(file)-1] =
'\0';
775 for (
int i = optind; i < argc; i++)
794 kh_destroy(sixt_map,
sixt_f);
801 kh_destroy(sixt_int,
sixt_v);
Structure representing the start or end of an interval.
void corsaro_in_free_record(corsaro_in_record_t *record)
Free an corsaro record object.
corsaro_in_t * corsaro_alloc_input(const char *corsarouri)
Allocate an corsaro object for reading an corsaro file.
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.
The Source Port field of the FlowTuple.
int add_inc_hash(kh_sixt_int_t *hash, corsaro_flowtuple_t *t, uint32_t increment)
Either add the given flowtuple to the hash, or increment the current count.
Header file dealing with the corsaro logging sub-system.
#define CORSARO_FLOWTUPLE_IP_TO_SIXT(n32, flowtuple)
Convert a 32bit network order IP address into the 3 byte flowtuple format.
#define FIELD_ENABLED
Value if field is enabled.
static corsaro_interval_t last_interval_end
The time that the last interval ended.
The Protocol field of the FlowTuple.
static int init_corsaro(char *corsarouri)
Initialize a corsaro_in instance for the given input file name.
A reusable opaque structure for corsaro to read an input record into.
static int value_field
The field to use as the value in aggregation.
void corsaro_io_print_interval_end(corsaro_interval_t *int_end)
Write the interval trailers to stdout.
The TCP Flags field of the FlowTuple.
static void dump_hash_map(kh_sixt_map_t *hash)
Dump a map of flowtuple records.
int process_flowtuple_file(char *file)
Process a flowtuple file.
#define corsaro_flowtuple_hash_equal(alpha, bravo)
Tests two flowtuples for equality.
static uint64_t flowtuple_cnt
The number of flowtuple records we have processed.
Header file which exports corsaro_flowtuple plugin API.
enum field_index field_index_t
Set of FlowTuple fields that can be used for aggregation.
uint16_t number
The interval number (starts at 0)
int corsaro_finalize_input(corsaro_in_t *corsaro)
Close the input file and free resources allocated by corsaro.
static void clean()
Cleanup and free state.
Overall corsaro magic number - "EDGR".
void corsaro_flowtuple_free(corsaro_flowtuple_t *t)
Free a FlowTuple record.
static char * field_names[]
Array of strings corresponding to FlowTuple fields.
The corsaro_flowtuple flowtuple record.
static kh_sixt_map_t * sixt_f
A map of aggregated flowtuple records.
int corsaro_start_input(corsaro_in_t *corsaro)
Initialize an corsaro input object that has already been allocated.
static int interval
The amount of time to wait until we dump the hash.
#define CORSARO_FLOWTUPLE_SIXT_TO_IP(flowtuple)
Convert the 3byte flowtuple dest ip to 32bits of network ordered uint32.
The Destination IP address field of the FlowTuple.
khint32_t corsaro_flowtuple_hash_func(struct corsaro_flowtuple *ft)
Hash the given flowtuple into a 32bit value.
static corsaro_interval_t last_dump_end
the END time of the interval that we last dumped data
KHASH_INIT(sixt_map, corsaro_flowtuple_t *, kh_64xx_t *, 1, corsaro_flowtuple_hash_func, corsaro_flowtuple_hash_equal)
Initialize the hash functions and datatypes.
int main(int argc, char *argv[])
Entry point for the cors-ft-aggregate tool.
static field_index_t fields[FIELD_CNT]
An array of enabled fields for aggregation.
corsaro_in_record_t * corsaro_in_alloc_record(corsaro_in_t *corsaro)
Allocate a reusable corsaro record object.
static int process_flowtuple(corsaro_flowtuple_t *tuple)
Process a FlowTuple record.
corsaro interval magic number - "INTR"
void corsaro_log_file(const char *func, corsaro_file_t *logfile, const char *format,...)
Write a formatted string to a generic log file.
static corsaro_in_record_t * record
The record object to read into.
uint32_t time
The time this interval started/ended.
static void usage(const char *name)
Print usage information to stderr.
The start of an interval.
static int next_interval
The time that we need to dump the next interval at.
static void dump_hash()
Dump the aggregated FlowTuple records.
The Destination Port field of the FlowTuple.
Header file dealing with the corsaro file IO.
static void dump_hash_int(kh_sixt_int_t *hash)
Dump a hash of flowtuple records.
The Source IP address field of the FlowTuple.
The IP Length field of the FlowTuple.
The TTL field of the FlowTuple.
The null type used for wildcard matching.
static int add_inc_map(void *h, corsaro_flowtuple_t *t, uint32_t value)
Either add the given flowtuple to the hash, or add the value to the map.
void * corsaro_in_get_record_data(corsaro_in_record_t *record)
Get a pointer data in a record.
The number of possible FlowTuple fields.
static int legacy
Set if reading from a legacy FlowTuple file.
off_t corsaro_in_read_record(corsaro_in_t *corsaro, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the next corsaro record from the given corsaro input file.
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.
The Packet Count field of the FlowTuple.
static kh_sixt_int_t * sixt_v
A hash of aggregated flowtuple records.
field_index
Set of FlowTuple fields that can be used for aggregation.
static void flowtuple_print_64(corsaro_flowtuple_t *flowtuple, uint64_t value)
Print a flowtuple with a 64 bit value field.