/* get next elem */ static PyObject *BGPRecord_get_next_elem(BGPRecordObject *self) { bgpstream_elem_t *elem; PyObject *pyelem; if ((elem = bgpstream_record_get_next_elem(self->rec)) == NULL) Py_RETURN_NONE; if ((pyelem = BGPElem_new(elem)) == NULL) { PyErr_SetString(PyExc_RuntimeError, "Could not create BGPElem object"); return NULL; } return pyelem; }
bgpstream_elem_t *bgpstream_record_get_next_elem(bgpstream_record_t *record) { if (bgpstream_elem_generator_is_populated(record->elem_generator) == 0 && bgpstream_elem_generator_populate(record->elem_generator, record) != 0) { return NULL; } bgpstream_elem_t *elem = bgpstream_elem_generator_get_next_elem(record->elem_generator); /* if the elem is compatible with the current filters * then return elem, otherwise run again * bgpstream_record_get_next_elem(record) */ if (elem == NULL || bgpstream_elem_check_filters(record->bs->filter_mgr, elem) == 1) { return elem; } return bgpstream_record_get_next_elem(record); }
/** Implements the process_record function of the plugin API */ int bgpcorsaro_pfxmonitor_process_record(bgpcorsaro_t *bgpcorsaro, bgpcorsaro_record_t *record) { struct bgpcorsaro_pfxmonitor_state_t *state = STATE(bgpcorsaro); bgpstream_record_t *bs_record = BS_REC(record); bgpstream_elem_t *elem; uint64_t overlap; uint8_t more_specific; /* no point carrying on if a previous plugin has already decided we should ignore this record */ if ((record->state.flags & BGPCORSARO_RECORD_STATE_FLAG_IGNORE) != 0) { return 0; } /* consider only valid records */ if (bs_record->status != BGPSTREAM_RECORD_STATUS_VALID_RECORD) { return 0; } /* process all elems in the record */ while ((elem = bgpstream_record_get_next_elem(bs_record)) != NULL) { if (elem->type != BGPSTREAM_ELEM_TYPE_ANNOUNCEMENT && elem->type != BGPSTREAM_ELEM_TYPE_WITHDRAWAL && elem->type != BGPSTREAM_ELEM_TYPE_RIB) { continue; } /* consider only prefixes announced by the peers (not the collector) */ if (elem->type != BGPSTREAM_ELEM_TYPE_WITHDRAWAL && bgpstream_as_path_get_len(elem->aspath) == 0) { continue; } /* consider only prefixes that overlap with the set provided */ /* first, check the non overlapping pfxs cache */ if (bgpstream_pfx_storage_set_exists(state->non_overlapping_pfx_cache, &elem->prefix) != 0) { /* confirmed as non-overlapping */ continue; } /* check the overlapping pfxs cache */ if (bgpstream_pfx_storage_set_exists(state->overlapping_pfx_cache, &elem->prefix) == 0) { /* not cached, but could still be overlapping, need to check */ more_specific = 0; overlap = bgpstream_ip_counter_is_overlapping( state->poi, (bgpstream_pfx_t *)&elem->prefix, &more_specific); if (overlap > 0 && (!state->more_specific || more_specific)) { bgpstream_pfx_storage_set_insert(state->overlapping_pfx_cache, &elem->prefix); /* overlapping */ } else { bgpstream_pfx_storage_set_insert(state->non_overlapping_pfx_cache, &elem->prefix); /* non-overlapping */ continue; } } /* by here, confirmed as overlapping, process the elem */ process_overlapping_pfx(state, bs_record, elem); } return 0; }
int main(int argc, char *argv[]) { int opt; int prevoptind; opterr = 0; // variables associated with options char *projects[PROJECT_CMD_CNT]; int projects_cnt = 0; char *types[TYPE_CMD_CNT]; int types_cnt = 0; char *collectors[COLLECTOR_CMD_CNT]; int collectors_cnt = 0; char *peerasns[PEERASN_CMD_CNT]; int peerasns_cnt = 0; char *prefixes[PREFIX_CMD_CNT]; int prefixes_cnt = 0; char *communities[COMMUNITY_CMD_CNT]; int communities_cnt = 0; struct window windows[WINDOW_CMD_CNT]; char *endp; int windows_cnt = 0; char *interface_options[OPTION_CMD_CNT]; int interface_options_cnt = 0; int rib_period = 0; int live = 0; int output_info = 0; int record_output_on = 0; int record_bgpdump_output_on = 0; int elem_output_on = 0; bgpstream_data_interface_option_t *option; int i; /* required to be created before usage is called */ bs = bgpstream_create(); if(!bs) { fprintf(stderr, "ERROR: Could not create BGPStream instance\n"); return -1; } datasource_id_default = datasource_id = bgpstream_get_data_interface_id(bs); datasource_info = bgpstream_get_data_interface_info(bs, datasource_id); assert(datasource_id != 0); /* allocate memory for bs_record */ bgpstream_record_t *bs_record = bgpstream_record_create(); if(bs_record == NULL) { fprintf(stderr, "ERROR: Could not create BGPStream record\n"); bgpstream_destroy(bs); return -1; } while (prevoptind = optind, (opt = getopt (argc, argv, "d:o:p:c:t:w:j:k:y:P:lrmeivh?")) >= 0) { if (optind == prevoptind + 2 && (optarg == NULL || *optarg == '-') ) { opt = ':'; -- optind; } switch (opt) { case 'p': if(projects_cnt == PROJECT_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d projects can be specified on " "the command line\n", PROJECT_CMD_CNT); usage(); exit(-1); } projects[projects_cnt++] = strdup(optarg); break; case 'c': if(collectors_cnt == COLLECTOR_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d collectors can be specified on " "the command line\n", COLLECTOR_CMD_CNT); usage(); exit(-1); } collectors[collectors_cnt++] = strdup(optarg); break; case 't': if(types_cnt == TYPE_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d types can be specified on " "the command line\n", TYPE_CMD_CNT); usage(); exit(-1); } types[types_cnt++] = strdup(optarg); break; case 'w': if(windows_cnt == WINDOW_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d windows can be specified on " "the command line\n", WINDOW_CMD_CNT); usage(); exit(-1); } /* split the window into a start and end */ if((endp = strchr(optarg, ',')) == NULL) { windows[windows_cnt].end = BGPSTREAM_FOREVER; } else { *endp = '\0'; endp++; windows[windows_cnt].end = atoi(endp); } windows[windows_cnt].start = atoi(optarg); windows_cnt++; break; case 'j': if(peerasns_cnt == PEERASN_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d peer asns can be specified on " "the command line\n", PEERASN_CMD_CNT); usage(); exit(-1); } peerasns[peerasns_cnt++] = strdup(optarg); break; case 'k': if(prefixes_cnt == PREFIX_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d peer asns can be specified on " "the command line\n", PREFIX_CMD_CNT); usage(); exit(-1); } prefixes[prefixes_cnt++] = strdup(optarg); break; case 'y': if(communities_cnt == COMMUNITY_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d communities can be specified on " "the command line\n", PREFIX_CMD_CNT); usage(); exit(-1); } communities[communities_cnt++] = strdup(optarg); break; case 'P': rib_period = atoi(optarg); break; case 'd': if((datasource_id = bgpstream_get_data_interface_id_by_name(bs, optarg)) == 0) { fprintf(stderr, "ERROR: Invalid data interface name '%s'\n", optarg); usage(); exit(-1); } datasource_info = bgpstream_get_data_interface_info(bs, datasource_id); break; case 'o': if(interface_options_cnt == OPTION_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d interface options can be specified\n", OPTION_CMD_CNT); usage(); exit(-1); } interface_options[interface_options_cnt++] = strdup(optarg); break; case 'l': live = 1; break; case 'r': record_output_on = 1; break; case 'm': record_bgpdump_output_on = 1; break; case 'e': elem_output_on = 1; break; case 'i': output_info = 1; break; case ':': fprintf(stderr, "ERROR: Missing option argument for -%c\n", optopt); usage(); exit(-1); break; case '?': case 'v': fprintf(stderr, "bgpreader version %d.%d.%d\n", BGPSTREAM_MAJOR_VERSION, BGPSTREAM_MID_VERSION, BGPSTREAM_MINOR_VERSION); usage(); exit(0); break; default: usage(); exit(-1); } } for(i=0; i<interface_options_cnt; i++) { if(*interface_options[i] == '?') { dump_if_options(); usage(); exit(0); } else { /* actually set this option */ if((endp = strchr(interface_options[i], ',')) == NULL) { fprintf(stderr, "ERROR: Malformed data interface option (%s)\n", interface_options[i]); fprintf(stderr, "ERROR: Expecting <option-name>,<option-value>\n"); usage(); exit(-1); } *endp = '\0'; endp++; if((option = bgpstream_get_data_interface_option_by_name(bs, datasource_id, interface_options[i])) == NULL) { fprintf(stderr, "ERROR: Invalid option '%s' for data interface '%s'\n", interface_options[i], datasource_info->name); usage(); exit(-1); } bgpstream_set_data_interface_option(bs, option, endp); } free(interface_options[i]); interface_options[i] = NULL; } interface_options_cnt = 0; if(windows_cnt == 0) { fprintf(stderr, "ERROR: At least one time window must be specified using -w\n"); usage(); exit(-1); } /* if the user did not specify any output format * then the default one is per elem */ if(record_output_on == 0 && elem_output_on == 0 && record_bgpdump_output_on == 0) { elem_output_on = 1; } /* the program can now start */ /* allocate memory for interface */ /* projects */ for(i=0; i<projects_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_PROJECT, projects[i]); free(projects[i]); } /* collectors */ for(i=0; i<collectors_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_COLLECTOR, collectors[i]); free(collectors[i]); } /* types */ for(i=0; i<types_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_RECORD_TYPE, types[i]); free(types[i]); } /* windows */ for(i=0; i<windows_cnt; i++) { bgpstream_add_interval_filter(bs, windows[i].start, windows[i].end); } /* peer asns */ for(i=0; i<peerasns_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PEER_ASN, peerasns[i]); free(peerasns[i]); } /* prefixes */ for(i=0; i<prefixes_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PREFIX, prefixes[i]); free(prefixes[i]); } /* communities */ for(i=0; i<communities_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_COMMUNITY, communities[i]); free(communities[i]); } /* frequencies */ if(rib_period > 0) { bgpstream_add_rib_period_filter(bs, rib_period); } /* datasource */ bgpstream_set_data_interface(bs, datasource_id); /* live */ if(live != 0) { bgpstream_set_live_mode(bs); } /* turn on interface */ if(bgpstream_start(bs) < 0) { fprintf(stderr, "ERROR: Could not init BGPStream\n"); return -1; } if(output_info) { if(record_output_on) { printf(BGPSTREAM_RECORD_OUTPUT_FORMAT); } if(elem_output_on) { printf(BGPSTREAM_ELEM_OUTPUT_FORMAT); } } /* use the interface */ int get_next_ret = 0; bgpstream_elem_t * bs_elem; do { get_next_ret = bgpstream_get_next_record(bs, bs_record); if(get_next_ret && record_output_on) { print_bs_record(bs_record); } if(get_next_ret && bs_record->status == BGPSTREAM_RECORD_STATUS_VALID_RECORD) { if(record_bgpdump_output_on) { bgpstream_record_print_mrt_data(bs_record); } if(elem_output_on) { /* check if the record is of type RIB, in case extract the ID */ if(bs_record->attributes.dump_type == BGPSTREAM_RIB) { /* print the RIB start line */ if(bs_record->dump_pos == BGPSTREAM_DUMP_START) { print_rib_control_message(bs_record); } } while((bs_elem = bgpstream_record_get_next_elem(bs_record)) != NULL) { if(print_elem(bs_record, bs_elem) != 0) { goto err; } } /* check if end of RIB has been reached */ if(bs_record->attributes.dump_type == BGPSTREAM_RIB && bs_record->dump_pos == BGPSTREAM_DUMP_END) { print_rib_control_message(bs_record); } } } } while(get_next_ret > 0); /* de-allocate memory for bs_record */ bgpstream_record_destroy(bs_record); /* turn off interface */ bgpstream_stop(bs); /* deallocate memory for interface */ bgpstream_destroy(bs); return 0; err: bgpstream_record_destroy(bs_record); bgpstream_stop(bs); bgpstream_destroy(bs); return -1; }