static int add_prefixes_from_file(bgpstream_ip_counter_t *poi, char *pfx_file_string) { char pfx_line[MAX_LOG_BUFFER_LEN]; io_t *file = NULL; bgpstream_pfx_storage_t pfx_st; if (pfx_file_string == NULL) { return -1; } if ((file = wandio_create(pfx_file_string)) == NULL) { fprintf(stderr, "ERROR: Could not open prefix file (%s)\n", pfx_file_string); return -1; } while (wandio_fgets(file, &pfx_line, MAX_LOG_BUFFER_LEN, 1) > 0) { /* treat # as comment line, and ignore empty lines */ if (pfx_line[0] == '#' || pfx_line[0] == '\0') { continue; } if (bgpstream_str2pfx(pfx_line, &pfx_st) == NULL || bgpstream_ip_counter_add(poi, (bgpstream_pfx_t *)&pfx_st) != 0) { /* failed to parse/insert the prefix */ return -1; } } wandio_destroy(file); return 0; }
static int same_header(char *mrt_filename, unsigned char *previous_header) { off_t read_bytes; io_t *io_h = wandio_create(mrt_filename); if(io_h == NULL) { bgpstream_log_err("\t\tBSDS_SINGLEFILE: can't open file!"); return -1; } read_bytes = wandio_read(io_h, (void *) &(buffer[0]), MAX_HEADER_READ_BYTES); if(read_bytes < 0) { bgpstream_log_err("\t\tBSDS_SINGLEFILE: can't read file!"); wandio_destroy(io_h); return -1; } int ret = memcmp(buffer, previous_header, sizeof(unsigned char) * read_bytes); wandio_destroy(io_h); /* if there is no difference, then they have the same header */ if(ret == 0) { /* fprintf(stderr, "same header\n"); */ return 1; } memcpy(previous_header, buffer, sizeof(unsigned char) * read_bytes); return 0; }
int bgpstream_csvfile_datasource_update_input_queue(bgpstream_csvfile_datasource_t* csvfile_ds, bgpstream_input_mgr_t *input_mgr) { bgpstream_debug("\t\tBSDS_CSVFILE: csvfile_ds update input queue start"); io_t *file_io = NULL; char buffer[BUFFER_LEN]; int read = 0; struct timeval tv; gettimeofday(&tv, NULL); /* we accept all timestamp earlier than now() - 1 second */ csvfile_ds->max_accepted_ts = tv.tv_sec - 1; csvfile_ds->num_results = 0; csvfile_ds->max_ts_infile = 0; csvfile_ds->input_mgr = input_mgr; if((file_io = wandio_create(csvfile_ds->csvfile_file)) == NULL) { bgpstream_log_err("\t\tBSDS_CSVFILE: create csvfile_ds can't open file %s", csvfile_ds->csvfile_file); return -1; } while((read = wandio_read(file_io, &buffer, BUFFER_LEN)) > 0) { if(csv_parse(&(csvfile_ds->parser), buffer, read, parse_csvfile_field, parse_csvfile_rowend, csvfile_ds) != read) { bgpstream_log_err("\t\tBSDS_CSVFILE: CSV error %s", csv_strerror(csv_error(&(csvfile_ds->parser)))); return -1; } } if(csv_fini(&(csvfile_ds->parser), parse_csvfile_field, parse_csvfile_rowend, csvfile_ds) != 0) { bgpstream_log_err("\t\tBSDS_CSVFILE: CSV error %s", csv_strerror(csv_error(&(csvfile_ds->parser)))); return -1; } wandio_destroy(file_io); csvfile_ds->input_mgr = NULL; csvfile_ds->last_processed_ts = csvfile_ds->max_ts_infile; bgpstream_debug("\t\tBSDS_CSVFILE: csvfile_ds update input queue end"); return csvfile_ds->num_results; }
/** Open a libftrace source on an IPFIX/PDU file */ libftrace_t *ftrace_create(const char *filename, int version, const char *lpfilename) { libftrace_t *ft = NULL; int pterrno = 0; /* create structure */ ft = malloc(sizeof(*ft)); memset(ft, 0, sizeof(*ft)); ft->filename = filename; /* create condition variables and mutex */ if ((pterrno = pthread_mutex_init(&ft->mux, NULL)) != 0) { fprintf(stderr, "couldn't init pthread mux: %s\n", strerror(pterrno)); goto err; } if ((pterrno = pthread_cond_init(&ft->wok, NULL)) != 0) { fprintf(stderr, "couldn't init pthread wok: %s\n", strerror(pterrno)); goto err; } if ((pterrno = pthread_cond_init(&ft->rok, NULL)) != 0) { fprintf(stderr, "couldn't init pthread rok: %s\n", strerror(pterrno)); goto err; } /* initialize logging if necessary */ if (lpfilename) { libfc_initialize_logging(lpfilename); } /* open underlying source */ if (!(ft->wio = wandio_create(filename))) { /* FIXME error handling */ fprintf(stderr, "couldn't open %s: %s", filename, strerror(errno)); goto err; } /* create a template set -- this will be populated once we decide what to collect */ ft->tg = libfc_template_group_new(version); /* all done */ return ft; err: ftrace_destroy(ft); return NULL; }
int test_broker() { SETUP; CHECK_SET_INTERFACE(broker); /* test http connectivity */ io_t *file = wandio_create(BGPSTREAM_DS_BROKER_URL); CHECK_MSG("HTTP connectivity to broker", "Failed to connect to BGPStream Broker via HTTP.\n" "Maybe wandio is built without HTTP support, " "or there is no Internet connectivity\n", file != NULL); bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_COLLECTOR, "route-views6"); bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_RECORD_TYPE, "updates"); bgpstream_add_interval_filter(bs,1427846550,1427846700); RUN(broker); TEARDOWN; return 0; }
corsaro_file_in_t *corsaro_file_ropen(const char *filename) { corsaro_file_in_t *f = NULL; char buffer[1024]; int len; /* 2013-01-22 AK has removed all of the logging output on failures this is because i dont want to need a corsaro_t object to open a file. but also because i think it should be up to the caller to log the errors. logs from this deep in corsaro just confuse people when somewhat common errors occur (file not found etc). */ if((f = malloc(sizeof(corsaro_file_in_t))) == NULL) { return NULL; } /* we need to try and guess the mode... */ /* if there is a : in the uri, we guess it is a libtrace file */ /* this should be refined to do something more intelligent */ if(strchr(filename, ':') != NULL) { f->mode = CORSARO_FILE_MODE_TRACE; /* open this as a trace file */ f->trace_io = trace_create(filename); if(trace_is_err(f->trace_io)) { free(f); return NULL; } if (trace_start(f->trace_io) == -1) { free(f); return NULL; } /* trace is set to go! */ return f; } else { /* lets open the file and take a peek to see what we find */ if((f->wand_io = wandio_create(filename)) == NULL) { free(f); return NULL; } len = wandio_peek(f->wand_io, buffer, sizeof(buffer)); /* an ASCII corsaro file will start with "# CORSARO_VERSION" */ if(len >= strlen(CORSARO_FILE_ASCII_CHECK) && memcmp(CORSARO_FILE_ASCII_CHECK, buffer, strlen(CORSARO_FILE_ASCII_CHECK)) == 0) { f->mode = CORSARO_FILE_MODE_ASCII; } /* a binary corsaro file will start with an corsaro header "EDGRHEAD" but, it is possible that an old binary corsaro file can just start with an interval header - "EDGRINTR", so we will only look for "EDGR" */ else if(len >= 4 && buffer[0] == 'E' && buffer[1] == 'D' && buffer[2] == 'G' && buffer[3] == 'R') { f->mode = CORSARO_FILE_MODE_BINARY; } else { /* who knows, but maybe someone wants to read a non-corsaro file */ f->mode = CORSARO_FILE_MODE_UNKNOWN; } } return f; }
int bgpstream_broker_datasource_update_input_queue( bgpstream_broker_datasource_t *broker_ds, bgpstream_input_mgr_t *input_mgr) { // we need to set two parameters: // - dataAddedSince ("time" from last response we got) // - minInitialTime (max("initialTime"+"duration") of any file we've ever seen) #define BUFLEN 20 char buf[BUFLEN]; io_t *jsonfile = NULL; ; int num_results; int attempts = 0; int wait_time = 1; int success = 0; if (broker_ds->last_response_time > 0) { // need to add dataAddedSince if (snprintf(buf, BUFLEN, "%" PRIu32, broker_ds->last_response_time) >= BUFLEN) { fprintf(stderr, "ERROR: Could not build dataAddedSince param string\n"); goto err; } AMPORQ; APPEND_STR("dataAddedSince="); APPEND_STR(buf); } if (broker_ds->current_window_end > 0) { // need to add minInitialTime if (snprintf(buf, BUFLEN, "%" PRIu32, broker_ds->current_window_end) >= BUFLEN) { fprintf(stderr, "ERROR: Could not build minInitialTime param string\n"); goto err; } AMPORQ; APPEND_STR("minInitialTime="); APPEND_STR(buf); } do { if (attempts > 0) { fprintf(stderr, "WARN: Broker request failed, waiting %ds before retry\n", wait_time); sleep(wait_time); if (wait_time < MAX_WAIT_TIME) { wait_time *= 2; } } attempts++; #ifdef WITH_BROKER_DEBUG fprintf(stderr, "\nQuery URL: \"%s\"\n", broker_ds->query_url_buf); #endif if ((jsonfile = wandio_create(broker_ds->query_url_buf)) == NULL) { fprintf(stderr, "ERROR: Could not open %s for reading\n", broker_ds->query_url_buf); goto retry; } if ((num_results = read_json(broker_ds, input_mgr, jsonfile)) == ERR_FATAL) { fprintf(stderr, "ERROR: Received fatal error code from read_json\n"); goto err; } else if (num_results == ERR_RETRY) { goto retry; } else { // success! success = 1; } retry: if (jsonfile != NULL) { wandio_destroy(jsonfile); jsonfile = NULL; } } while (success == 0); // reset the variable params *broker_ds->query_url_end = '\0'; broker_ds->query_url_remaining = URL_BUFLEN - strlen(broker_ds->query_url_end); return num_results; err: fprintf(stderr, "ERROR: Fatal error in broker data source\n"); if (jsonfile != NULL) { wandio_destroy(jsonfile); } return -1; }