void rpki_start() { unsigned int waiting_time = 0; unsigned int group_len = get_number_of_cache_groups(); struct rtr_mgr_group* groups = get_rtr_mgr_groups(); if (group_len == 0 || groups == NULL ) { RPKI_DEBUG("No caches were found in config. Prefix validation is off."); return; } RPKI_DEBUG("Init rtr_mgr."); rtr_config = rtr_mgr_init(groups, group_len, polling_period, expire_interval, &update_cb, NULL, NULL, NULL); RPKI_DEBUG("Starting rtr_mgr."); rtr_mgr_start(rtr_config); rtr_is_running = 1; RPKI_DEBUG("Waiting for rtr connection to synchronize."); while (waiting_time++ <= initial_synchronisation_timeout) { if (rtr_mgr_conf_in_sync(rtr_config)) { break; } sleep(1); } if (rtr_mgr_conf_in_sync(rtr_config)) { RPKI_DEBUG("Got synchronisation with at least one RPKI cache!"); } else { RPKI_DEBUG("Timeout expired! Proceeding without RPKI validation data."); } }
int main(void) { //create a TCP transport socket int retval = 0; struct tr_socket tr_tcp; char tcp_host[] = "rpki-validator.realmv6.org"; char tcp_port[] = "8282"; struct tr_tcp_config tcp_config = { tcp_host, //IP tcp_port, //Port NULL //Source address }; tr_tcp_init(&tcp_config, &tr_tcp); struct rtr_socket rtr_tcp; rtr_tcp.tr_socket = &tr_tcp; struct rtr_mgr_group groups[1]; groups[0].sockets = malloc(sizeof(struct rtr_socket *)); groups[0].sockets_len = 1; groups[0].sockets[0] = &rtr_tcp; groups[0].preference = 1; struct tr_socket tr_tcp2; struct rtr_socket rtr_tcp2; struct rtr_mgr_group group2; tr_tcp_init(&tcp_config, &tr_tcp2); rtr_tcp2.tr_socket = &tr_tcp2; group2.sockets = malloc(sizeof(struct rtr_socket *)); group2.sockets_len = 1; group2.sockets[0] = &rtr_tcp2; group2.preference = 2; struct rtr_mgr_config *conf; rtr_mgr_init(&conf, groups, 1, 30, 600, 600, NULL, NULL, &connection_status_callback, NULL); //start the connection manager rtr_mgr_start(conf); int sleep_counter = 0; // wait 20 sec till at least one group is fully synchronized with the server // otherwise EXIT_FAILURE. while (!rtr_mgr_conf_in_sync(conf)) { sleep_counter++; if (connection_status == RTR_MGR_ERROR || sleep_counter > connection_timeout) return EXIT_FAILURE; sleep(1); } assert(conf->len == 1); retval = rtr_mgr_add_group(conf, &group2); assert(retval == RTR_SUCCESS); //checking behavior in case the group preference already exists //by adding the same group twice. retval = rtr_mgr_add_group(conf, &group2); assert(retval == RTR_INVALID_PARAM); tommy_node *node = tommy_list_head(&conf->groups->list); struct rtr_mgr_group_node *group_node = node->data; struct rtr_mgr_group_node *group_node2 = node->next->data; assert(group_node->group->preference == 1); assert(group_node2->group->preference == 2); assert(conf->len == 2); rtr_mgr_remove_group(conf, 1); node = tommy_list_head(&conf->groups->list); group_node = node->data; assert(group_node->group->preference == 2); assert(conf->len == 1); struct tr_socket tr_tcp3; struct rtr_socket rtr_tcp3; struct rtr_mgr_group group3; tr_tcp_init(&tcp_config, &tr_tcp3); rtr_tcp3.tr_socket = &tr_tcp3; group3.sockets = malloc(sizeof(struct rtr_socket *)); group3.sockets_len = 1; group3.sockets[0] = &rtr_tcp3; group3.preference = 3; struct tr_socket tr_tcp4; struct rtr_socket rtr_tcp4; struct rtr_mgr_group group4; tr_tcp_init(&tcp_config, &tr_tcp4); rtr_tcp4.tr_socket = &tr_tcp4; group4.sockets = malloc(sizeof(struct rtr_socket *)); group4.sockets_len = 1; group4.sockets[0] = &rtr_tcp4; group4.preference = 4; rtr_mgr_add_group(conf, &group4); // remove group 2 so group 4 becomes the active group. rtr_mgr_remove_group(conf, 2); // add group 3 which has a higher preference than group 4 // and check whether it will be set as the active group. rtr_mgr_add_group(conf, &group3); node = tommy_list_head(&conf->groups->list); group_node = node->data; assert(group_node->group->preference == 3); //try to remove non-existent group retval = rtr_mgr_remove_group(conf, 10); assert(retval == RTR_ERROR); struct tr_socket tr_tcp5; struct rtr_socket rtr_tcp5; struct rtr_mgr_group group5; tr_tcp_init(&tcp_config, &tr_tcp5); rtr_tcp5.tr_socket = &tr_tcp5; group5.sockets = malloc(sizeof(struct rtr_socket *)); group5.sockets_len = 1; group5.sockets[0] = &rtr_tcp5; group5.preference = 5; //add 100 groups for (int i = 0; i < 100; i++) { retval = rtr_mgr_add_group(conf, &group5); group5.preference++; assert(retval == RTR_SUCCESS); } //remove 100 groups for (int i = 104; i >= 5; i--) { retval = rtr_mgr_remove_group(conf, i); assert(retval == RTR_SUCCESS); } rtr_mgr_remove_group(conf, 4); //try to remove last remainig group. retval = rtr_mgr_remove_group(conf, 3); assert(retval == RTR_ERROR); rtr_mgr_stop(conf); rtr_mgr_free(conf); free(groups[0].sockets); free(group2.sockets); free(group3.sockets); free(group4.sockets); free(group5.sockets); }
int main() { signal(SIGINT, &sig_handler); struct tr_tcp_config tcp_config = { "rpki.realmv6.org", //IP "42420" //Port }; struct tr_socket tr_tcp; tr_tcp_init(&tcp_config, &tr_tcp); struct rtr_socket rtr_tcp; rtr_tcp.tr_socket = &tr_tcp; struct tr_tcp_config tcp1_config = { "rpki.realmv6.org", //IP "8282" //Port }; struct tr_socket tr_tcp1; tr_tcp_init(&tcp1_config, &tr_tcp1); struct rtr_socket rtr_tcp1; rtr_tcp1.tr_socket = &tr_tcp1; struct rtr_mgr_group groups[2]; groups[0].sockets_len = 1; groups[0].sockets = malloc(1 * sizeof(struct rtr_socket*)); groups[0].sockets[0] = &rtr_tcp; groups[0].preference = 1; groups[1].sockets_len = 1; groups[1].sockets = malloc(1 * sizeof(struct rtr_socket*)); groups[1].sockets[0] = &rtr_tcp1; groups[1].preference = 2; struct rtr_mgr_config conf; conf.groups = groups; conf.len = 2; rtr_mgr_init(groups, 2, 60, 60, NULL, NULL, NULL, NULL); rtr_mgr_start(&conf); //fill_pfx_table(groups[1].sockets[0]->pfx_table); while(!rtr_mgr_conf_in_sync(&conf)){ sleep(1); } unsigned int asn; char prefix[256] = ""; unsigned int prefix_len; struct ip_addr ip_addr; enum pfxv_state state; char tmp[100]; time_t t = time(NULL); struct tm* tm = localtime(&t); strftime(tmp, sizeof(tmp), "log/prefixes-%F", tm); strcat(tmp, ".log"); int day = tm->tm_mday; pref_f = fopen(tmp, "w"); if(pref_f == NULL){ printf("can't open %s\n", tmp); exit(EXIT_FAILURE); } fprintf(pref_f, "#CLOCK DATE;ANNOUNCED_PREFIX PREF_LEN ASN;AS_PATH_ELEM{,AS_PATH_ELEM};ROA_ASN ROA_PREFIX/ROA_MIN_LEN-ROA_MAX_LEN {,ROA_ASN ROA_PREFIX/ROA_MIN_LEN-ROA_MAX_LEN };VAL_STATE"); strftime(tmp, sizeof(tmp), "log/benchmark-%F", tm); strcat(tmp, ".log"); bench_f = fopen(tmp, "w"); if(bench_f == NULL){ printf("can't open %s\n", tmp); exit(EXIT_FAILURE); } //purge stdin buffer __fpurge(stdin); pthread_t thrd_id; pthread_create(&thrd_id, NULL, &log_thr, NULL); printf("Benchmark started\n"); char as_path[1024]; struct pfx_record* reason = NULL; unsigned int reason_len = 0; while(true){ read_announcement(&asn, prefix, &prefix_len, as_path); strftime(tmp, sizeof(tmp), "%R:%S %F", tm); if(ip_str_to_addr(prefix, &ip_addr) == -1){ fprintf(stderr, "ERROR STR TO IPADDR\n"); exit(EXIT_FAILURE); } if(pfx_table_validate_r(conf.groups[0].sockets[0]->pfx_table, &reason, &reason_len, asn, &ip_addr, prefix_len, &state) == -1){ fprintf(stderr, "VALIDATE ERROR\n"); exit(EXIT_FAILURE); } if(fprintf(pref_f, "%s;%s %u %u;%s;", tmp, prefix, prefix_len, asn, as_path) < 0) perror("printf error"); if(reason != NULL && reason_len > 0){ for(unsigned int i = 0; i < reason_len; i++){ ip_addr_to_str(&(reason[i].prefix), tmp, sizeof(tmp)); fprintf(pref_f, "%u %s/%u-%u", reason[i].asn, tmp, reason[i].min_len, reason[i].max_len); if((i+1) < reason_len) fprintf(pref_f, ","); } fprintf(pref_f, ";"); } if(state == BGP_PFXV_STATE_VALID){ pthread_mutex_lock(&mutex); valid_state++; pthread_mutex_unlock(&mutex); if (fprintf(pref_f, "VALID\n") < 0 ) perror("printf error"); } else if (state == BGP_PFXV_STATE_INVALID){ pthread_mutex_lock(&mutex); invalid_state++; pthread_mutex_unlock(&mutex); if (fprintf(pref_f, "INVALID\n") < 0) perror("printf error"); } else if (state == BGP_PFXV_STATE_NOT_FOUND){ pthread_mutex_lock(&mutex); not_found_state++; pthread_mutex_unlock(&mutex); if (fprintf(pref_f, "NOT_FOUND\n") < 0) perror("printf error"); } //tagwechsel // t = time(NULL); if(t == ((time_t) -1)){ perror("time returned error value"); exit(EXIT_FAILURE); } struct tm* tm = localtime(&t); tm = localtime(&t); if(tm == NULL){ perror("localtime error"); exit(EXIT_FAILURE); } if(tm->tm_mday != day){ day = tm->tm_mday; fclose(bench_f); fclose(pref_f); strftime(tmp, sizeof(tmp), "log/prefixes-%F", tm); strcat(tmp, ".log"); pref_f = fopen(tmp, "w"); if(pref_f == NULL){ printf("can't open prefixes.log\n"); exit(EXIT_FAILURE); } pthread_mutex_lock(&mutex); strftime(tmp, sizeof(tmp), "log/benchmark-%F", tm); strcat(tmp, ".log"); bench_f = fopen(tmp, "w"); pthread_mutex_unlock(&mutex); } } fclose(bench_f); fclose(pref_f); }
int main(int argc, char *argv[]) { /* check arguments, need hostname/IP and port of cache-server */ if (argc < 3) { printf("Usage: %s [host] [port]\n", argv[0]); return EXIT_FAILURE; } struct tr_socket tr_tcp; struct tr_tcp_config tcp_config = { argv[1], argv[2], NULL }; struct rtr_socket rtr_tcp; struct rtr_mgr_config *conf; struct rtr_mgr_group groups[1]; /* init a TCP transport and create rtr socket */ tr_tcp_init(&tcp_config, &tr_tcp); rtr_tcp.tr_socket = &tr_tcp; /* create a rtr_mgr_group array with 1 element */ groups[0].sockets = malloc(1 * sizeof(struct rtr_socket *)); groups[0].sockets_len = 1; groups[0].sockets[0] = &rtr_tcp; groups[0].preference = 1; if (rtr_mgr_init(&conf, groups, 1, 30, 600, 600, NULL, NULL, &connection_status_callback, NULL) < 0) return EXIT_FAILURE; rtr_mgr_start(conf); char input[256]; int sleep_counter = 0; /* wait till at least one rtr_mgr_group is synchronized with server */ while (!rtr_mgr_conf_in_sync(conf)) { if (connection_error(connection_status)) return EXIT_FAILURE; sleep(1); sleep_counter++; if (sleep_counter >= connection_timeout) { /* * Wait for input before printing "timeout", * to avoid "broken pipee error while communicating * with the Python program */ if (fgets(input, 256, stdin)) ; printf("timeout\n"); fflush(stdout); return EXIT_FAILURE; } } char ip[128]; int mask; int asn; int counter; /* loop for input */ while (1) { int input_len; int spaces; /* recheck connection, exit on failure */ if (connection_error(connection_status)) return EXIT_FAILURE; /* try reading from stdin, exit on failure */ if (!fgets(input, 256, stdin)) { printf("input error\n"); return EXIT_FAILURE; } /* remove newline, if present */ input_len = strlen(input) - 1; if (input[input_len] == '\n') input[input_len] = '\0'; /* check if there are exactly 3 arguments */ spaces = 0; for (counter = 0; counter < input_len; counter++) { if (input[counter] == ' ' && input[counter + 1] != ' ' && input[counter + 1] != '\0' && counter != 0) spaces++; } /* check input matching pattern */ if (spaces != 2) { printf("Arguments required: IP Mask ASN\n"); fflush(stdout); continue; } char delims[] = " "; char *input_tok = NULL; input_tok = strtok(input, delims); strcpy(ip, input_tok); input_tok = strtok(NULL, delims); mask = atoi(input_tok); input_tok = strtok(NULL, delims); asn = atoi(input_tok); struct lrtr_ip_addr pref; enum pfxv_state result; struct pfx_record *reason = NULL; unsigned int reason_len = 0; lrtr_ip_str_to_addr(ip, &pref); /* do validation */ pfx_table_validate_r(groups[0].sockets[0]->pfx_table, &reason, &reason_len, asn, &pref, mask, &result); int validity_code = -1; /* translate validation result */ if (result == BGP_PFXV_STATE_VALID) validity_code = 0; else if (result == BGP_PFXV_STATE_NOT_FOUND) validity_code = 1; else if (result == BGP_PFXV_STATE_INVALID) validity_code = 2; /* IP Mask BGP-ASN| */ printf("%s %d %d|", ip, mask, asn); /* ROA-ASN IP MaskMin MaskMax, ... */ if (reason && (reason_len > 0)) { unsigned int i; for (i = 0; i < reason_len; i++) { char tmp[100]; lrtr_ip_addr_to_str(&reason[i].prefix, tmp, sizeof(tmp)); printf("%u %s %u %u", reason[i].asn, tmp, reason[i].min_len, reason[i].max_len); if ((i + 1) < reason_len) printf(","); } } /* |validity_code */ printf("|%d", validity_code); printf("\n"); fflush(stdout); } rtr_mgr_stop(conf); rtr_mgr_free(conf); free(groups[0].sockets); return EXIT_SUCCESS; }
//int validatePrefix(char* ip, int mask, int asn){ int main(int argc, char *argv[]) { if(argc<3) { printf("Usage: %s [host] [port]\n", argv[0]); return 0; } //create a TCP transport socket tr_socket tr_tcp1; tr_tcp_config tcp_config1 = { argv[1], //IP, old: "rpki.realmv6.org" argv[2] //Port, old: "42420" }; tr_tcp_init(&tcp_config1, &tr_tcp1); //create 3 rtr_sockets and associate them with the transprort sockets rtr_socket rtr_tcp1; rtr_tcp1.tr_socket = &tr_tcp1; //create a rtr_mgr_group array with 1 element rtr_mgr_group groups[1]; //The first group contains both TCP RTR sockets groups[0].sockets = malloc(1 * sizeof(rtr_socket*)); groups[0].sockets_len = 1; groups[0].sockets[0] = &rtr_tcp1; groups[0].preference = 1; //Preference value of this group //create a rtr_mgr_config struct that stores the group rtr_mgr_config conf; conf.groups = groups; conf.len = 1; //1 element in the groups array //initialize all rtr_sockets in the server pool with the same settings rtr_mgr_init(&conf, 240, 520, NULL); //start the connection manager rtr_mgr_start(&conf); //wait till at least one rtr_mgr_group is fully synchronized with the server while (!rtr_mgr_conf_in_sync(&conf)) sleep(1); char ip[128]; int mask; int asn; int counter; int spaces; while (1) { char input[256]; int inputLength; fgets(input, 256, stdin); // Remove newline, if present inputLength = strlen(input) - 1; if (input[inputLength] == '\n') input[inputLength] = '\0'; // Check if there are exactly 3 arguments spaces = 0; for (counter = 0; counter < inputLength; counter++) { if (input[counter] == ' ' && input[counter+1] != ' ' && input[counter+1] != '\0' && counter != 0) { spaces++; } } if (spaces != 2) { //break; //printf("Arguments required: IP, Mask, ASN, %d\n", spaces); //fflush(stdout); printf("Next\n"); continue; } char delims[] = " "; char *inputToken = NULL; inputToken = strtok(input, delims); strcpy(ip, inputToken); inputToken = strtok(NULL, delims); mask = atoi(inputToken); inputToken = strtok(NULL, delims); asn = atoi(inputToken); //validate the BGP-Route ip/mask, origin ASN: asn ip_addr pref; ip_str_to_addr(ip, &pref); pfxv_state result; pfx_record* reason = NULL; unsigned int reason_len = 0; //rtr_mgr_validate(&conf, asn, &pref, mask, &result); pfx_table_validate_r(conf.groups[0].sockets[0]->pfx_table, &reason, &reason_len, asn, &pref, mask, &result); int validityNr = 0; if (result == BGP_PFXV_STATE_NOT_FOUND) { validityNr = -1; } else if (result == BGP_PFXV_STATE_INVALID) { validityNr = 0; } else if (result == BGP_PFXV_STATE_VALID) { validityNr = 1; } // |ValidityNr printf("%d|", validityNr); // IP Mask BGP-ASN| printf("%s %d %d", ip, mask, asn); if(validityNr>=0) printf("|"); // ROA-ASN IP MaskMin MaskMax, .. if (reason != NULL && reason_len > 0) { unsigned int i; for (i = 0; i < reason_len; i++) { char tmp[100]; ip_addr_to_str(&(reason[i].prefix), tmp, sizeof(tmp)); printf("%u %s %u %u", reason[i].asn, tmp, reason[i].min_len, reason[i].max_len); if ((i + 1) < reason_len) printf(","); } } printf("\n"); fflush(stdout); } rtr_mgr_stop(&conf); rtr_mgr_free(&conf); free(groups[0].sockets); }