JNIEXPORT void JNICALL Java_org_netbsd_liblpm_LPM_destroy(JNIEnv *env, jobject obj, jlong lpm_ref) { lpm_t *lpm = (lpm_t *)lpm_ref; lpm_clear(lpm, lpm_jni_dtor, env); lpm_destroy(lpm); }
int main(int argc, char** argv) { if (argc != 6) { fprintf(stderr, "Usage: %s <prefix-file> <tsdb-file> " "<profile_dir> <capfile> <unix-timestamp>\n", argv[0]); exit(1); } char* prefix_file = argv[1]; char* tsdb_file = argv[2]; char* profile_dir = argv[3]; char* capfile = argv[4]; time_t epoch = atoi(argv[5]); uint16_t vals_per_entry = 1; // defines how many entries we can have // we currently store one value per entry. // this means we do not group by IP_interface // but have a value vor IP_interface_ifInOctets, ... // maybe we want to change that uint16_t slot_seconds = 300; // defines measurment interval struct lpm_tree* tree = read_prefix_file(prefix_file); if (!tree) { fprintf(stderr, "ERROR: Could not create subnet tree!\n"); exit(1); } if (0 != tsdb_open(tsdb_file, &db, &vals_per_entry, slot_seconds, 0)) { fprintf(stderr, "ERROR: Could not open tsdb database!\n"); exit(1); } if (0 != tsdb_goto_epoch(&db, epoch, 0, 1)) { fprintf(stderr, "ERROR: Could not set epoch to %u\n", epoch); exit(1); } fprintf(stderr, "reading nfdump files ...\n"); read_nfdump_files(profile_dir, capfile, tree); fprintf(stderr, "Dumping to tsdb ... \n"); twalk(network_tree, &store_records_in_db); fprintf(stderr, "Cleaning up!\n"); lpm_destroy(tree); tsdb_flush(&db); tsdb_close(&db); return 0; }
int main(int argc, char* argv[]) { char* input; int opt; struct lpm_tree* tree; FILE* in; char* ifile = "/dev/stdin"; /* Check inputs; print usage and exit if something is clearly wrong. */ if (argc < 3) { print_usage(argv[0]); exit(EXIT_FAILURE); } /* Parse options */ while ((opt = getopt(argc, argv, "df:")) != -1) { switch (opt) { case 'd': debug = 1; break; case 'f': input = optarg; break; default: /* '?' */ print_usage(argv[0]); exit(EXIT_FAILURE); } } /* Create a fresh tree. */ tree = lpm_init(); /* Read in all prefixes. */ in = fopen(input, "r"); while (1) { char *line = NULL; size_t linecap = 0; ssize_t linelen; char ip_string[INET6_ADDRSTRLEN]; int mask; uint8_t rt; linelen = getline(&line, &linecap, in); if (linelen < 0) { break; } rt = sscanf(line, "%39s %d%*[^\n]", ip_string, &mask); if (rt < 2) { continue; } lpm_insert(tree, ip_string, mask); } fclose(in); lpm_debug_print(tree); /* Begin reading from standard input the lines of text to convert. */ in = fopen(ifile, "r"); while (1) { char *line = NULL; size_t linecap = 0; ssize_t linelen; char address_string[16]; char output[16]; char* pointer; char* strstart; char* strend; int rt; /* Read line. */ linelen = getline(&line, &linecap, in); if (linelen < 0) { break; } line[strlen(line)-1] = '\0'; pointer = line; strstart = pointer; strend = strstr(strstart, " "); while (strend != NULL) { memset(address_string, '\0', 16); memcpy(address_string, strstart, strend - strstart); memset(output, '\0', 16); rt = lpm_lookup(tree, address_string, output); if (rt) { printf("%s ", output); } else { printf("%s ", address_string); } strstart = strend + 1; strend = strstr(strstart, " "); } memset(output, '\0', 16); rt = lpm_lookup(tree, strstart, output); if (rt) { printf("%s\n", output); } else { printf("%s\n", strstart); } free(line); } lpm_destroy(tree); fclose(in); return 1; }
static struct lpm_tree* read_prefix_file(const char* prefix_file) { struct lpm_tree* tree = lpm_init(); if (tree == NULL){ fprintf(stderr, "ERROR: Could not allocate lpm_tree()\n"); return NULL; } FILE* f = fopen(prefix_file, "r"); if (f == NULL) { fprintf(stderr, "ERROR: coult not open prefix file %s\n", prefix_file); goto out1; } char line[LINE_SIZE]; char tmp_line[LINE_SIZE]; char delimiter[] = " "; while (fgets(line, LINE_SIZE, f)) { // tokenize the subnet list. The expected format is // <NET_ID> <subnet1> <subnet2> ... // remove trailing \n if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = 0; } // duplicate string for parsing. we need the original // line for error messages strcpy(tmp_line, line); char* ptr = strtok(tmp_line, delimiter); uint32_t id; int is_first = 1; while (ptr) { if (is_first) { id = atoi(ptr); is_first = 0; } else { // extract the ip and the subnet string // expected format: ip/subnetmask char* slash_pos = strchr(ptr, '/'); char* netmask; if (slash_pos == NULL) { fprintf(stderr, "ERROR parsing line " "\"%s\". Could not find / in token " "%s\n", line, ptr); // there is an error in the file format // we do the only thing we can and drop // out goto out2; } if (strlen(slash_pos) == 1) { // there is no subnetmask behind the // slash. drop out fprintf(stderr, "ERROR parsing line " "\"%s\". Found / in token %s but no " "following subnet mask!\n", line, ptr); goto out2; } *slash_pos = 0; netmask = slash_pos + 1; if (0 == lpm_insert(tree, ptr, atoi(netmask), id)) { fprintf(stderr, "ERROR inserting %s " "into the subnet tree!\n", ptr); goto out2; } } ptr = strtok(NULL, delimiter); } } // We are done without errors. goto out1; out2: lpm_destroy(tree); tree = NULL; out1: fclose(f); out: return tree; }
RRDVisAnalyzer::~RRDVisAnalyzer() { lpm_destroy(tree); }