void ReadBGPFile(BGPDATA BGP,string file,std::ofstream &outFile ) { BGPDUMP *mydump; BGPDUMP_ENTRY *my_entry=NULL; struct stat buf; int i = 0; if (BGP.verbose) cerr << file << "\n"; mydump = bgpdump_open_dump(file.c_str()); if (mydump == NULL) { int val = stat(file.c_str(),&buf); if (val < 0) { printf ("Error statting file: %s\n",strerror(errno)); } cerr << "Can not open "<< file << "\n"; return; } do { ++i; my_entry=bgpdump_read_next(mydump); if(my_entry!=NULL) { process(my_entry,BGP,outFile); bgpdump_free_mem(my_entry); } } while(mydump->eof==0); bgpdump_close_dump(mydump); }
void bgpstream_record_clear(bgpstream_record_t *record) { record->status = BGPSTREAM_RECORD_STATUS_EMPTY_SOURCE; record->dump_pos = BGPSTREAM_DUMP_START; record->attributes.dump_project[0] = '\0'; record->attributes.dump_collector[0] = '\0'; record->attributes.dump_type = BGPSTREAM_UPDATE; record->attributes.dump_time = 0; record->attributes.record_time = 0; if (record->bd_entry != NULL) { bgpdump_free_mem(record->bd_entry); record->bd_entry = NULL; } // record->bs = NULL; bgpstream_elem_generator_clear(record->elem_generator); }
/* free memory associated to a bs_record */ void bgpstream_record_destroy(bgpstream_record_t *const bs_record) { bgpstream_debug("BS: destroy record start"); if (bs_record == NULL) { bgpstream_debug("BS: record destroy end"); return; // nothing to do } if (bs_record->bd_entry != NULL) { bgpstream_debug("BS - free bs_record->bgpdump_entry"); bgpdump_free_mem(bs_record->bd_entry); bs_record->bd_entry = NULL; } if (bs_record->elem_generator != NULL) { bgpstream_elem_generator_destroy(bs_record->elem_generator); bs_record->elem_generator = NULL; } bgpstream_debug("BS - free bs_record"); free(bs_record); bgpstream_debug("BS: destroy record end"); }
BGPDUMP_ENTRY* bgpdump_read_next(BGPDUMP *dump) { BGPDUMP_ENTRY *this_entry=NULL; struct mstream s; u_char *buffer; int ok=0; u_int32_t bytes_read; this_entry = malloc(sizeof(BGPDUMP_ENTRY)); bytes_read = cfr_read_n(dump->f, &(this_entry->time), 4); bytes_read += cfr_read_n(dump->f, &(this_entry->type), 2); bytes_read += cfr_read_n(dump->f, &(this_entry->subtype), 2); bytes_read += cfr_read_n(dump->f, &(this_entry->length), 4); if(bytes_read != 12) { if(bytes_read > 0) { /* Malformed record */ dump->parsed++; syslog(LOG_ERR, "bgpdump_read_next: incomplete MRT header (%d bytes read, expecting 12)", bytes_read); } /* Nothing more to read, quit */ free(this_entry); dump->eof=1; return(NULL); } dump->parsed++; /* Intel byte ordering stuff ... */ this_entry->type=ntohs(this_entry->type); this_entry->subtype=ntohs(this_entry->subtype); this_entry->time=ntohl(this_entry->time); this_entry->length=ntohl(this_entry->length); this_entry->attr=NULL; buffer = malloc(this_entry->length); bytes_read = cfr_read_n(dump->f, buffer, this_entry->length); if(bytes_read != this_entry->length) { syslog(LOG_ERR, "bgpdump_read_next: incomplete dump record (%d bytes read, expecting %d)", bytes_read, this_entry->length); free(this_entry); free(buffer); dump->eof=1; return(NULL); } ok=0; mstream_init(&s,buffer,this_entry->length); switch(this_entry->type) { case BGPDUMP_TYPE_MRTD_BGP: break; case BGPDUMP_TYPE_MRTD_TABLE_DUMP: ok = process_mrtd_table_dump(&s,this_entry); break; case BGPDUMP_TYPE_ZEBRA_BGP: ok = process_zebra_bgp(&s,this_entry); break; case BGPDUMP_TYPE_TABLE_DUMP_V2: ok = process_mrtd_table_dump_v2(&s,this_entry); break; } free(buffer); if(ok) { dump->parsed_ok++; } else { bgpdump_free_mem(this_entry); return NULL; } return this_entry; }