29 #include <arpa/inet.h>
49 #include "corsaro_smee.h"
60 #define CORSARO_SMEE_MAGIC 0x534D4545
63 #define PLUGIN_NAME "smee"
66 #define CORSARO_SMEE_STATFILE PLUGIN_NAME"-stat"
68 #define CORSARO_SMEE_SUMFILE PLUGIN_NAME"-sum"
70 #define CORSARO_SMEE_SRCFILE PLUGIN_NAME"-sources"
73 #define CORSARO_SMEE_MX_LIFETIME 3600
75 #define CORSARO_SMEE_MX_SOURCES 4000000
77 #define CORSARO_SMEE_TIME_REC_INTERVAL 3600
89 struct corsaro_smee_state_t {
102 struct ip_address *local_addresses;
104 int local_addresses_cnt;
106 const char *meter_location;
112 int time_rec_interval;
114 int save_distributions;
118 #define STATE(corsaro) \
119 (CORSARO_PLUGIN_STATE(corsaro, smee, CORSARO_PLUGIN_ID_SMEE))
121 #define PLUGIN(corsaro) \
122 (CORSARO_PLUGIN_PLUGIN(corsaro, CORSARO_PLUGIN_ID_SMEE))
127 struct corsaro_smee_state_t *state =
STATE(corsaro);
128 struct ip_address *la = NULL;
133 if((state->local_addresses =
134 realloc(state->local_addresses,
135 sizeof(
struct ip_address) * (state->local_addresses_cnt+1))
138 fprintf(stderr,
"ERROR: could not re-alloc address list\n");
142 la = &(state->local_addresses[state->local_addresses_cnt++]);
145 if((tok = strsep(&address_str,
"/")) == NULL)
147 fprintf(stderr,
"ERROR: Invalid local address (%s)\n", address_str);
152 if((la->a.v4 = inet_addr(tok)) == -1)
156 fprintf(stderr,
"ERROR: Invalid local address (%s)\n", tok);
161 if((la->len = atoi(address_str)) > 32)
163 fprintf(stderr,
"ERROR: Invalid local address mask (%s)\n", address_str);
168 fprintf(stderr,
"WARNING: Local address mask of 0 (%s)\n", address_str);
178 "plugin usage: %s [-s] [-i interval] [-l meter_loc] [-L max_src_life] -a prefix\n"
180 "(-a can be specified multiple times)\n"
181 " -i interval between writing summary files (secs) "
183 " -l meter location (default: %s)\n"
185 " -L max lifetime for source to stay in hashtable "
186 "(secs) (default: %d)\n"
187 " -m memory size allocated for source hash table "
188 "(in KB) (default: %d)\n"
189 " -s write the source tables to a file "
190 "(disables summary tables)\n",
205 struct corsaro_smee_state_t *state =
STATE(corsaro);
208 assert(plugin->
argc > 0 && plugin->
argv != NULL);
213 while((opt = getopt(plugin->
argc, plugin->
argv,
":a:i:l:L:m:s?")) >= 0)
227 state->time_rec_interval = atoi(optarg);
231 state->meter_location = strdup(optarg);
235 state->max_lifetime = atoi(optarg);
239 state->max_sources = atoi(optarg);
243 state->save_distributions = 1;
254 if(state->local_addresses_cnt == 0)
257 "ERROR: At least one local prefix must be specified using -a\n");
267 const char *fmt, ...)
281 #define CORSARO_SMEE_FPRINTF(outfile) \
283 corsaro_t *corsaro = (corsaro_t*)user_data; \
285 corsaro_file_vprintf(corsaro, (outfile), fmt, ap); \
288 corsaro_file_printf(corsaro, (outfile), "\n"); \
342 struct corsaro_smee_state_t *state;
345 assert(plugin != NULL);
347 if((state =
malloc_zero(
sizeof(
struct corsaro_smee_state_t))) == NULL)
350 "could not malloc corsaro_smee_state_t");
360 state->save_distributions = 0;
408 state->meter_location,
411 state->time_rec_interval,
412 state->local_addresses,
413 state->local_addresses_cnt,
444 struct corsaro_smee_state_t *state =
STATE(corsaro);
447 if(state->smee_started != 0)
449 iat_process_packet(NULL, SM_DUMMY);
450 state->smee_started = 0;
455 if(state->statfile != NULL)
458 state->statfile = NULL;
460 if(state->sumfile != NULL)
463 state->sumfile = NULL;
465 if(state->srcfile != NULL)
468 state->srcfile = NULL;
471 if(state->local_addresses_cnt > 0 && state->local_addresses != NULL)
473 free(state->local_addresses);
474 state->local_addresses = NULL;
475 state->local_addresses_cnt = 0;
505 struct corsaro_smee_state_t *state =
STATE(corsaro);
507 if(state->rotate == 1)
509 if(state->statfile != NULL)
512 state->statfile = NULL;
514 if(state->sumfile != NULL)
517 state->sumfile = NULL;
519 if(state->srcfile != NULL)
522 state->srcfile = NULL;
529 if(state->statfile == NULL &&
534 corsaro_log(__func__, corsaro,
"could not open %s output file",
540 if(state->save_distributions == 0 &&
541 state->sumfile == NULL &&
546 corsaro_log(__func__, corsaro,
"could not open %s output file",
552 if(state->save_distributions != 0 &&
553 state->srcfile == NULL &&
558 corsaro_log(__func__, corsaro,
"could not open %s output file",
564 state->smee_started = 1;
578 iat_process_packet(NULL, SM_RECORD_REQ);
587 STATE(corsaro)->rotate = 1;
597 libtrace_packet_t *ltpacket =
LT_PKT(packet);
600 assert(
STATE(corsaro)->smee_started != 0);
603 rc = iat_process_packet(ltpacket, SM_PACKET);
605 if(rc != SM_OK && rc != SM_RECORD_INTERVAL)
607 corsaro_log(__func__, corsaro,
"iat_process_packet returned %d", rc);
Structure representing the start or end of an interval.
int corsaro_smee_end_interval(corsaro_t *corsaro, corsaro_interval_t *int_end)
Implements the end_interval function of the plugin API.
An opaque structure defining an corsaro input file.
Header file dealing with the corsaro plugin manager.
Header file dealing with the corsaro logging sub-system.
const char * corsaro_get_monitorname(corsaro_t *corsaro)
Accessor function to get the monitor name string.
An opaque structure defining an corsaro output file.
int corsaro_smee_init_input(corsaro_in_t *corsaro)
Implements the init_input function of the plugin API.
#define CORSARO_SMEE_MAGIC
The magic number for this plugin - "SMEE".
A reusable opaque structure for corsaro to read an input record into.
#define CORSARO_SMEE_TIME_REC_INTERVAL
Default interval in seconds to write summary files.
char ** argv
Array of plugin arguments This is populated by the plugin manager in corsaro_plugin_enable_plugin.
static uint64_t smee_pkt_drops(void *user_data)
Called by smee to determine the number of dropped packets.
#define LT_PKT(corsaro_packet)
Convenience macro to get to the libtrace packet inside an corsaro packet.
void corsaro_log_va(const char *func, corsaro_t *corsaro, const char *format, va_list args)
Write a formatted string to the logfile associated with an corsaro object.
static corsaro_plugin_t corsaro_smee_plugin
Common plugin information across all instances.
int corsaro_smee_close_output(corsaro_t *corsaro)
Implements the close_output function of the plugin API.
static int smee_sources_callback(void *user_data, const char *fmt,...)
Called by smee to populate the sources file.
static int parse_local_address(corsaro_t *corsaro, char *address_str)
Parse a local address prefix.
static int smee_stat_callback(void *user_data, const char *fmt,...)
Called by smee to write stats lines.
#define CORSARO_SMEE_SUMFILE
The name for the sum file.
void corsaro_file_close(struct corsaro *corsaro, corsaro_file_t *file)
Closes an corsaro output file and frees the writer structure.
static libtrace_packet_t * packet
A pointer to a libtrace packet.
void corsaro_plugin_free_state(corsaro_plugin_manager_t *manager, corsaro_plugin_t *plugin)
Free the state for a plugin.
int corsaro_smee_start_interval(corsaro_t *corsaro, corsaro_interval_t *int_start)
Implements the start_interval function of the plugin API.
#define STATE(corsaro)
Extends the generic plugin state convenience macro in corsaro_plugin.h.
Header file for common utility functions.
A lightweight wrapper around a libtrace packet.
off_t corsaro_smee_read_global_data_record(struct corsaro_in *corsaro, enum corsaro_in_record_type *record_type, struct corsaro_in_record *record)
Implements the read_global_data_record function of the plugin API.
corsaro_plugin_manager_t * plugin_manager
A pointer to the corsaro plugin manager state.
#define CORSARO_PLUGIN_GENERATE_PTRS(plugin)
Convenience macro that defines all the function pointers for the corsaro plugin API.
corsaro_in_record_type
Corsaro input record types.
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.
void corsaro_plugin_register_state(corsaro_plugin_manager_t *manager, corsaro_plugin_t *plugin, void *state)
Register the state for a plugin.
int corsaro_plugin_probe_filename(const char *fname, corsaro_plugin_t *plugin)
Check a filename to see if it contains a plugin's name.
#define CORSARO_SMEE_SRCFILE
The name for the src file.
#define CORSARO_PLUGIN_GENERATE_TAIL
Convenience macro that defines all the 'remaining' blank fields in a corsaro plugin object...
void * malloc_zero(const size_t size)
Allocate memory and set it to zero.
int corsaro_smee_close_input(corsaro_in_t *corsaro)
Implements the close_input function of the plugin API.
Header file dealing with the low-level file IO.
static corsaro_in_record_t * record
A pointer to a corsaro record.
int argc
Count of arguments in argv.
#define CORSARO_SMEE_STATFILE
The name for the stat file.
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.
int corsaro_is_rotate_interval(corsaro_t *corsaro)
Convenience function to determine if the output files should be rotated.
int corsaro_smee_process_packet(corsaro_t *corsaro, corsaro_packet_t *packet)
Implements the process_packet function of the plugin API.
static int smee_sum_callback(void *user_data, const char *fmt,...)
Called by smee to populate the summary file.
#define CORSARO_SMEE_MX_LIFETIME
Default max lifetime for source to stay in hashtable.
static void smee_log_callback(void *user_data, int priority, int die, const char *fmt,...)
Called by smee to log messages.
const char * corsaro_get_traceuri(corsaro_t *corsaro)
Accessor function to get the trace uri string.
int corsaro_smee_init_output(corsaro_t *corsaro)
Implements the init_output function of the plugin API.
static int parse_args(corsaro_t *corsaro)
Parse the arguments given to the plugin.
Header file dealing with the corsaro file IO.
corsaro_plugin_t * corsaro_smee_alloc(corsaro_t *corsaro)
Implements the alloc function of the plugin API.
off_t corsaro_smee_read_record(struct corsaro_in *corsaro, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Implements the read_record function of the plugin API.
#define PLUGIN_NAME
The name of this plugin.
uint64_t corsaro_get_dropped_packets(corsaro_t *corsaro)
Accessor function to get the number of dropped packets in this interval.
static void usage(corsaro_t *corsaro)
Print plugin usage to stderr.
int corsaro_smee_probe_filename(const char *fname)
Implements the probe_filename function of the plugin API.
#define PLUGIN(corsaro)
Extends the generic plugin plugin convenience macro in corsaro_plugin.h.
#define CORSARO_SMEE_MX_SOURCES
Default memory size allocated for source hash table (in KB)
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
int corsaro_smee_probe_magic(corsaro_in_t *corsaro, corsaro_file_in_t *file)
Implements the probe_magic function of the plugin API.
#define CORSARO_SMEE_FPRINTF(outfile)
Helper macro for IO callbacks to write to a Corsaro file.
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.
An corsaro packet processing plugin.
Header file dealing with the internal corsaro functions.