RRDVisAnalyzer::RRDVisAnalyzer(const ConfigObject& configObject, ReporterBase& reporter) : AnalyzerBase(configObject, reporter), configSection("rrdvisualizer"), firstFlow(true), lastFlowStart(0) { configFile = configObject.getConfString(configSection, "configfile"); rrdPath = configObject.getConfString(configSection, "rrdtool_path"); rrdDbPath = configObject.getConfString(configSection, "db_path"); // parse subnet config file std::ifstream subnetConfig(configFile.c_str()); std::string token; bool subnet = true; std::string subnet_string; tree = lpm_init(); std::vector<std::string> subnetList; while (subnetConfig) { subnetConfig >> token; if (subnet) { subnet_string = token; subnet = false; } else { size_t pos = subnet_string.find("/"); if (pos == std::string::npos) { throw std::runtime_error("Error: Cannot parse subnet \"" + subnet_string + "\""); } std::string ip = subnet_string.substr(0, pos); std::string mask = subnet_string.substr(pos + 1, subnet_string.size()); rrdDBMap[subnet_string] = token; lpm_insert(tree, ip.c_str(), atoi(mask.c_str())); subnet = true; } } // define the number of values that need to be aggregated // by the rrdtools intervals.push_back(1); intervals.push_back(5); intervals.push_back(30); intervals.push_back(120); intervals.push_back(24*60); // graph time spans graphTimeSpans.push_back("-1d"); graphTimeSpans.push_back("-1w"); graphTimeSpans.push_back("-1m"); graphTimeSpans.push_back("-1y"); }
JNIEXPORT jint JNICALL Java_org_netbsd_liblpm_LPM_insert__J_3BILjava_lang_Object_2 (JNIEnv *env, jobject obj, jlong lpm_ref, jbyteArray addr_ref, jint pref, jobject value) { lpm_t *lpm = (lpm_t *)lpm_ref; jobject val_ref, old_val_ref; jbyte *addr; size_t len; int ret; len = (*env)->GetArrayLength(env, addr_ref); assert(len == 16 || len == 4); addr = (*env)->GetByteArrayElements(env, addr_ref, NULL); if (addr == NULL) { return -1; } old_val_ref = lpm_lookup_prefix(lpm, addr, len, pref); val_ref = (*env)->NewWeakGlobalRef(env, value); if (val_ref == NULL) { (*env)->ReleaseByteArrayElements(env, addr_ref, addr, JNI_ABORT); return -1; } ret = lpm_insert(lpm, addr, len, pref, (void *)val_ref); if (ret != 0) { (*env)->DeleteWeakGlobalRef(env, val_ref); } else if (old_val_ref != NULL) { (*env)->DeleteWeakGlobalRef(env, old_val_ref); } (*env)->ReleaseByteArrayElements(env, addr_ref, addr, JNI_ABORT); return ret; }
JNIEXPORT jint JNICALL Java_org_netbsd_liblpm_LPM_insert__JLjava_lang_String_2Ljava_lang_Object_2 (JNIEnv *env, jobject obj, jlong lpm_ref, jstring cidr, jobject value) { lpm_t *lpm = (lpm_t *)lpm_ref; jobject val_ref, old_val_ref; const char *cidr_s; uint32_t addr[4]; size_t len; unsigned pref; int ret; cidr_s = (*env)->GetStringUTFChars(env, cidr, NULL); if (cidr_s == NULL) { return -1; } ret = lpm_strtobin(cidr_s, addr, &len, &pref); (*env)->ReleaseStringUTFChars(env, cidr, cidr_s); if (ret != 0) { return ret; } old_val_ref = lpm_lookup_prefix(lpm, addr, len, pref); val_ref = (*env)->NewWeakGlobalRef(env, value); if (val_ref == NULL) { return -1; } ret = lpm_insert(lpm, addr, len, pref, (void *)val_ref); if (ret != 0) { (*env)->DeleteWeakGlobalRef(env, val_ref); } else if (old_val_ref != NULL) { (*env)->DeleteWeakGlobalRef(env, old_val_ref); } return ret; }
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; }