29 #include <arpa/inet.h>
32 #include <netinet/in.h>
36 #include <sys/socket.h>
52 #ifdef WITH_PLUGIN_SIXT
56 #include "corsaro_pfx2as.h"
69 #ifdef CORSARO_PFX2AS_DEBUG
71 static int debug_cache_hits = 0;
73 static int debug_cache_misses = 0;
77 #define CORSARO_PFX2AS_MAGIC 0x41532323
80 #define PLUGIN_NAME "pfx2as"
92 #ifdef WITH_PLUGIN_SIXT
93 CORSARO_PLUGIN_GENERATE_PTRS_FT(corsaro_pfx2as),
101 struct corsaro_pfx2as_state_t {
107 khash_t(32
record) *as_cache;
114 #define BUFFER_LEN 1024
117 #define PFX2AS_COL_CNT 3
120 #define STATE(corsaro) \
121 (CORSARO_PLUGIN_STATE(corsaro, pfx2as, CORSARO_PLUGIN_ID_PFX2AS))
123 #define PLUGIN(corsaro) \
124 (CORSARO_PLUGIN_PLUGIN(corsaro, CORSARO_PLUGIN_ID_PFX2AS))
130 "plugin usage: %s [-c] -f pfx2as_file\n"
131 " -c cache the results for each IP\n"
132 " -f pfx2as file to use for lookups\n",
140 struct corsaro_pfx2as_state_t *state =
STATE(corsaro);
146 while((opt = getopt(plugin->
argc, plugin->
argv,
"f:c?")) >= 0)
151 state->cache_enabled = 1;
155 state->pfx2as_file = optarg;
166 if(state->pfx2as_file == NULL)
168 fprintf(stderr,
"ERROR: %s requires '-f' to be specified\n",
181 uint32_t *asn = NULL;
185 while((tok = strsep(&asn_str,
"_")) != NULL)
188 if((asn = realloc(asn,
sizeof(uint32_t) * (asn_cnt+1))) == NULL)
198 if((period = strchr(tok,
'.')) != NULL)
203 asn[asn_cnt] = (atoi(tok)<<16) | atoi(period+1);
208 asn[asn_cnt] = atoi(tok);
228 struct corsaro_pfx2as_state_t *state =
STATE(corsaro);
234 khash_t(strrec) *asn_table = kh_init(strrec);
244 uint32_t *asn = NULL;
245 char *asn_str = NULL;
258 while((tok = strsep(&rowp,
"\t")) != NULL)
264 addr = inet_addr(tok);
274 asn_str = strdup(tok);
279 corsaro_log(__func__, corsaro,
"invalid pfx2as file");
288 corsaro_log(__func__, corsaro,
"invalid pfx2as file");
292 if(asn_cnt <= 0 || asn_str == NULL)
294 corsaro_log(__func__, corsaro,
"could not parse asn string");
299 if((khiter = kh_get(strrec, asn_table, asn_str)) == kh_end(asn_table))
305 corsaro_log(__func__, corsaro,
"could not alloc geo record");
314 khiter = kh_put(strrec, asn_table, asn_str, &khret);
315 kh_value(asn_table, khiter) =
record;
323 record = kh_value(asn_table, khiter);
324 assert(record != NULL);
333 assert(record != NULL);
338 record->
asn_ip_cnt += (ip_broadcast_addr(addr, mask) -
339 ip_network_addr(addr, mask)) + 1;
348 corsaro_log(__func__, corsaro,
"failed to associate record");
354 kh_free(strrec, asn_table,
str_free);
355 kh_destroy(strrec, asn_table);
365 assert(hash != NULL);
367 if((khiter = kh_get(32
record, hash, ip)) == kh_end(hash))
371 return kh_value(hash, khiter);
382 khiter = kh_put(32record, hash, ip, &khret);
383 kh_value(hash, khiter) =
record;
390 struct corsaro_pfx2as_state_t *plugin_state =
STATE(corsaro);
398 if(plugin_state->cache_enabled != 0)
401 if((record =
cache_get(plugin_state->as_cache, src_ip)) == NULL)
406 plugin_state->provider,
409 cache_add(plugin_state->as_cache, src_ip, record);
411 #ifdef CORSARO_PFX2AS_DEBUG
412 debug_cache_misses++;
424 plugin_state->provider,
437 addr.s_addr = src_ip;
439 fprintf(stdout,
"src: %s\n",
444 assert(provider != NULL);
481 struct corsaro_pfx2as_state_t *state;
484 assert(plugin != NULL);
486 if((state =
malloc_zero(
sizeof(
struct corsaro_pfx2as_state_t))) == NULL)
489 "could not malloc corsaro_pfx2as_state_t");
504 if((state->provider =
510 corsaro_log(__func__, corsaro,
"could not register as a geolocation"
519 "failed to open pfx2as file '%s'", state->pfx2as_file);
527 corsaro_log(__func__, corsaro,
"could not read pfx2as file '%s'",
534 if(state->cache_enabled != 0)
537 state->as_cache = kh_init(32
record);
562 struct corsaro_pfx2as_state_t *state =
STATE(corsaro);
566 if(state->provider != NULL)
569 state->provider = NULL;
572 if(state->as_cache != NULL)
574 kh_destroy(32
record, state->as_cache);
575 state->as_cache = NULL;
581 #ifdef CORSARO_PFX2AS_DEBUG
582 corsaro_log(__func__, corsaro,
"cache hits: %d misses: %d",
583 debug_cache_hits, debug_cache_misses);
626 libtrace_ip_t *ip_hdr = NULL;
628 if((ip_hdr = trace_get_ip(
LT_PKT(packet))) == NULL)
637 #ifdef WITH_PLUGIN_SIXT
640 corsaro_flowtuple_t *flowtuple,
648 int corsaro_pfx2as_process_flowtuple_class_start(
corsaro_t *corsaro,
649 corsaro_flowtuple_class_start_t *
class)
656 int corsaro_pfx2as_process_flowtuple_class_end(
corsaro_t *corsaro,
657 corsaro_flowtuple_class_end_t *
class)
Structure representing the start or end of an interval.
An opaque structure defining an corsaro input file.
static void usage(corsaro_plugin_t *plugin)
Print plugin usage to stderr.
static void cache_add(kh_32record_t *hash, uint32_t ip, corsaro_geo_record_t *record)
Add an ASN record to the hash for the given IP address.
Header file dealing with the corsaro plugin manager.
Default Geolocation data-structure.
off_t corsaro_file_rgets(corsaro_file_in_t *file, void *buffer, off_t len)
Reads a string from an corsaro input file into the provided buffer.
static void str_free(const char *str)
Free a string (for use with the map)
Header file dealing with the corsaro logging sub-system.
int corsaro_pfx2as_end_interval(corsaro_t *corsaro, corsaro_interval_t *int_end)
Implements the end_interval function of the plugin API.
A reusable opaque structure for corsaro to read an input record into.
static int process_generic(corsaro_t *corsaro, corsaro_packet_state_t *state, uint32_t src_ip)
Common code between process_packet and process_flowtuple.
void corsaro_geo_free_provider(corsaro_t *corsaro, corsaro_geo_provider_t *provider)
Free the given geolocation provider object.
int corsaro_pfx2as_init_output(corsaro_t *corsaro)
Implements the init_output function of the plugin API.
char ** argv
Array of plugin arguments This is populated by the plugin manager in corsaro_plugin_enable_plugin.
corsaro_geo_provider_t * corsaro_geo_get_by_id(corsaro_t *corsaro, corsaro_geo_provider_id_t id)
Retrieve the provider object for the given provider ID.
#define LT_PKT(corsaro_packet)
Convenience macro to get to the libtrace packet inside an corsaro packet.
static corsaro_geo_record_t * cache_get(kh_32record_t *hash, uint32_t ip)
Get an ASN record from the cache given an IP address.
void corsaro_geo_provider_add_record(corsaro_geo_provider_t *provider, corsaro_geo_record_t *record)
Add the given geolocation record to the head of the given geolocation provider object.
Structure which represents a geolocation provider.
corsaro_geo_record_t * corsaro_geo_provider_lookup_record(corsaro_t *corsaro, corsaro_geo_provider_t *provider, uint32_t addr)
Look up the given address in the provider's datastructure.
Header file which exports corsaro_flowtuple plugin API.
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.
void corsaro_geo_dump_record(corsaro_geo_record_t *record)
Dump the given geolocation record to stdout (for debugging)
int corsaro_pfx2as_probe_filename(const char *fname)
Implements the probe_filename function of the plugin API.
Header file for common utility functions.
A lightweight wrapper around a libtrace packet.
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.
#define BUFFER_LEN
The length of the line buffer when reading pfx2as files.
int asn_cnt
Number of ASNs in the asn array.
void corsaro_plugin_register_state(corsaro_plugin_manager_t *manager, corsaro_plugin_t *plugin, void *state)
Register the state for a plugin.
uint32_t corsaro_flowtuple_get_source_ip(corsaro_flowtuple_t *flowtuple)
Convenience function to get the source IP address from a FlowTuple.
int corsaro_geo_provider_clear(corsaro_geo_provider_t *provider)
Remove all the existing records from the given geolocation provider.
void corsaro_file_rclose(corsaro_file_in_t *file)
Closes an corsaro input file and frees the reader structure.
int corsaro_plugin_probe_filename(const char *fname, corsaro_plugin_t *plugin)
Check a filename to see if it contains a plugin's name.
corsaro_geo_record_t * corsaro_geo_next_record(corsaro_geo_provider_t *provider, corsaro_geo_record_t *record)
Retrieve the next geolocation provider record in the list.
#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.
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_PFX2AS_MAGIC
The magic number for this plugin - "AS##".
uint32_t asn_ip_cnt
Number of IP addresses that this ASN (or ASN group) 'owns'.
void chomp(char *line)
Remove a newline from the given string.
static corsaro_plugin_t corsaro_pfx2as_plugin
Initialize the hash type (32bit keys, geo_record values)
corsaro_packet_state_t state
The corsaro state associated with this packet.
CAIDA Prefix2AS lookup plugin.
corsaro_geo_provider_t * corsaro_geo_init_provider(corsaro_t *corsaro, corsaro_geo_provider_id_t provider_id, corsaro_geo_datastructure_id_t ds_id, corsaro_geo_provider_default_t set_default)
Allocate a geolocation provider object in the packet state.
#define PFX2AS_COL_CNT
The number of columns in a pfx2as file.
int corsaro_geo_provider_associate_record(corsaro_t *corsaro, corsaro_geo_provider_t *provider, uint32_t addr, uint8_t mask, corsaro_geo_record_t *record)
Register a new prefix to record mapping for the given provider.
Structure which contains a geolocation record.
int corsaro_pfx2as_start_interval(corsaro_t *corsaro, corsaro_interval_t *int_start)
Implements the start_interval function of the plugin API.
int corsaro_pfx2as_process_packet(corsaro_t *corsaro, corsaro_packet_t *packet)
Implements the process_packet function of the plugin API.
uint32_t * asn
Array of Autonomous System Numbers.
Corsaro state for a packet.
int corsaro_pfx2as_probe_magic(corsaro_in_t *corsaro, corsaro_file_in_t *file)
Implements the probe_magic function of the plugin API.
Header file dealing with the corsaro file IO.
#define STATE(corsaro)
Extends the generic plugin state convenience macro in corsaro_plugin.h.
static int parse_args(corsaro_t *corsaro)
Parse the arguments given to the plugin.
int corsaro_pfx2as_init_input(corsaro_in_t *corsaro)
Implements the init_input function of the plugin API.
static int read_routeviews(corsaro_t *corsaro, corsaro_file_in_t *file)
Read the prefix2as file.
int corsaro_pfx2as_close_output(corsaro_t *corsaro)
Implements the close_output function of the plugin API.
static int parse_asn(char *asn_str, uint32_t **asn_arr)
Parse an underscore-separated list of ASNs.
off_t corsaro_pfx2as_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.
int corsaro_pfx2as_close_input(corsaro_in_t *corsaro)
Implements the close_input function of the plugin API.
corsaro_plugin_t * corsaro_pfx2as_alloc(corsaro_t *corsaro)
Implements the alloc function of the plugin API.
#define PLUGIN(corsaro)
Extends the generic plugin plugin convenience macro in corsaro_plugin.h.
corsaro_file_in_t * corsaro_file_ropen(const char *filename)
Creates a new corsaro file reader and opens the provided file for reading.
This provider should not be the default geolocation result.
off_t corsaro_pfx2as_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.
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.
Header file dealing with the corsaro geolocation subsystem.
An corsaro packet processing plugin.
corsaro_geo_record_t * corsaro_geo_init_record(corsaro_geo_provider_t *provider, uint32_t id)
Allocate an empty geolocation record for the given id.
'Geolocation' data from CAIDA pfx2as
Header file dealing with the internal corsaro functions.
#define PLUGIN_NAME
The name of this plugin - should match the file name.