From be527d670c50d6bbac90aec41cd87366e16515e4 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 01 Jun 2022 10:00:01 +0000 Subject: [PATCH] Added input argument for reliability testing --- main.c | 81 +++++++++++++++++++++++++++++----------- 1 files changed, 59 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index 340187f..9ac6e1f 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ #define NUM_TESTS 10 -int test_dns(void); +int test_dns(int num_reach_tests); void* test_server(void* in); void* print_progress(void* in); @@ -24,14 +24,15 @@ // Variables are only modified before threads are created and as such are // thread-safe tests_done is modified to provide a rough count of number of // tests being completed for progress measurement -int tests_done = 0, num_tests = NUM_TESTS, num_servers = NUM_DNS, num_hosts = 0; +int tests_done = 0, num_tests = NUM_TESTS, num_servers = 0, num_hosts = 0; struct hosts_list* hosts = NULL; struct dns_list* servers = NULL; int main(int argc, char** argv) { int option, added_hosts = 0; - while ((option = getopt(argc, argv, "s:a:t:n:h")) != -1) { + int num_reach_tests = 10; + while ((option = getopt(argc, argv, "s:a:t:n:r:h")) != -1) { switch (option) { case 's': // server to use add_dns_server(&servers, optarg); @@ -47,6 +48,9 @@ case 'n': // number of tests to perform num_tests = atoi(optarg); break; + case 'r': // number of tests for reachability + num_reach_tests = atoi(optarg); + break; case '?': case 'h': default: @@ -57,11 +61,12 @@ printf( "\t-t <number>\tspecify the number of hostnames to be tested, " "capped at 50 + number manually added\n\t\t\tdefaults to test " - "all " + "all " "available\n"); printf( "\t-n <number>\tspecify the number of tests to perform on each " "hostname\n\t\t\tdefaults to 10\n"); + printf("\t-r <number>\tspecify the number of tests to run for reachability, defaulting to 10. Reachability testing only works when run as root\n"); printf("\t-h\t\tShow this help\n"); free_dns_list(&servers); free_hosts_list(&hosts); @@ -76,8 +81,9 @@ } for (int i = 0; i < NUM_DNS; i++) { add_dns_server(&servers, (char*)DNS_SERVERS[i]); + num_servers++; } - test_dns(); + test_dns(num_reach_tests); sort_servers(&servers); print_servers(servers); free_dns_list(&servers); @@ -88,17 +94,48 @@ // Test each dns server individually // Each test runs in its own thread and results are written to the respective // dns_list -int test_dns(void) +int test_dns(int num_reach_tests) { struct dns_list* curr = servers; int i = 0; - pthread_t* threads = malloc(num_servers * sizeof(pthread_t)); + int init_num_servers = num_servers; + pthread_t* threads; pthread_t progress; + + // Check each server for reachability, can't be done in parallel as incorrect packets are received + // reachable() requires raw packets, which needs root + if (getuid() == 0) { + while (curr) { + printf("\rTesting reachability: %d/%d", i, init_num_servers); fflush(stdout); + unsigned char* buf[65535]; + int error_count = 0; + curr->errors = 0; + // retry 10 times for UDP unreliability + for (int i = 0; i < num_reach_tests; i++) { + error_count += (reachable((unsigned char*)buf, curr->server) != 0); + } + /* fprintf(stderr, "ip: %s, ec: %d\n", curr->server, error_count); */ + // 30% error rate means unreachable + if (error_count >= (num_reach_tests * 3) / 10) { + curr->errors = -1 * error_count; + num_servers--; + } + curr = curr->next; + i++; + } + printf("\rTesting reachability: %d/%d\n", i, init_num_servers); + } + + threads = malloc(num_servers * sizeof(pthread_t)); + curr = servers; + i = 0; pthread_create(&progress, NULL, print_progress, NULL); while (curr) { - pthread_create(&threads[i], NULL, test_server, (void*)curr); + if (curr->errors == 0) { + pthread_create(&threads[i], NULL, test_server, (void*)curr); + i++; + } curr = curr->next; - i++; } for (int i = 0; i < num_servers; i++) { pthread_join(threads[i], NULL); @@ -116,7 +153,6 @@ struct dns_list* dns = (struct dns_list*)in; dns->time.tv_sec = 0; dns->time.tv_nsec = 0; - dns->errors = 0; for (int i = 0; i < num_tests; i++) { struct hosts_list* curr = hosts; while (curr) { @@ -127,18 +163,19 @@ for (int j = 0; j < 3 && run.tv_sec == -1; j++) { run = resolve(buf, curr->server, dns->server, T_A); } - if (run.tv_sec == -1) { // if test has failed 3 times, set time taken - // to 3s as penalty - dns->errors++; - } else { - dns->time.tv_sec += run.tv_sec; - dns->time.tv_nsec += run.tv_nsec; - if (dns->time.tv_nsec >= - 1000000000) { // nanoseconds have overflowed into seconds - dns->time.tv_sec += 1; - dns->time.tv_nsec -= 1000000000; - } - } + if (run.tv_sec == + -1) { // if test has failed 3 times, set time taken + // to 3s as penalty + dns->errors++; + } else { + dns->time.tv_sec += run.tv_sec; + dns->time.tv_nsec += run.tv_nsec; + if (dns->time.tv_nsec >= + 1000000000) { // nanoseconds have overflowed into seconds + dns->time.tv_sec += 1; + dns->time.tv_nsec -= 1000000000; + } + } tests_done++; curr = curr->next; } -- Gitblit v1.10.0