main.c | ●●●●● patch | view | raw | blame | history | |
slist.c | ●●●●● patch | view | raw | blame | history | |
slist.h | ●●●●● patch | view | raw | blame | history |
main.c
@@ -116,6 +116,7 @@ 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) { @@ -126,16 +127,18 @@ 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 if (run.tv_sec == -1) { // if test has failed 3 times, set time taken // to 3s as penalty run.tv_sec = 3; 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; } 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; } slist.c
@@ -4,7 +4,7 @@ void split(struct dns_list* head, struct dns_list** a, struct dns_list** b); struct dns_list* merge(struct dns_list* a, struct dns_list* b); int comp_times(struct timespec a, struct timespec b); int comp_times(struct dns_list* a, struct dns_list* b); int add_hosts_server(struct hosts_list** head, char* server) { @@ -103,7 +103,7 @@ if (!a) return b; if (!b) return a; if (comp_times(a->time, b->time) > 0) { if (comp_times(a, b) > 0) { out = b; out->next = merge(a, b->next); } else { @@ -113,14 +113,16 @@ return out; } int comp_times(struct timespec a, struct timespec b) int comp_times(struct dns_list* a, struct dns_list* b) { if (a.tv_sec == b.tv_sec) { if (a.tv_nsec >= b.tv_nsec) if (a->errors != b->errors) { return a->errors > b->errors; } else if (a->time.tv_sec == b->time.tv_sec) { if (a->time.tv_nsec >= b->time.tv_nsec) return 1; else return -1; } else if (a.tv_sec > b.tv_sec) { } else if (a->time.tv_sec > b->time.tv_sec) { return 1; } else return -1; @@ -128,10 +130,10 @@ int print_servers(struct dns_list* head) { printf("%-20s | %s\n", "Server", "Time"); printf("%-16s | %-11s | %s\n", "Server", "Time", "Errors"); while (head) { printf("%-20s | %ld.%09ld\n", head->server, head->time.tv_sec, head->time.tv_nsec); printf("%-16s | %ld.%09ld | %d\n", head->server, head->time.tv_sec, head->time.tv_nsec, head->errors); head = head->next; } return 0; slist.h
@@ -8,6 +8,7 @@ struct dns_list { char* server; struct timespec time; int errors; struct dns_list* next; };