From b6574898519ef89d5481ad677347f0630c877561 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Tue, 20 Oct 2020 13:29:13 +0000
Subject: [PATCH] Specify number of serves and hosts, test and sort servers
---
/dev/null | 61 ------------
slist.c | 137 +++++++++++++++++++++++++++
Makefile | 4
main.c | 91 ++++++++++--------
include/slist.h | 2
5 files changed, 191 insertions(+), 104 deletions(-)
diff --git a/Makefile b/Makefile
index d0e275b..c67c988 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CC=gcc
-_OBJ=main.o dns.o servers.o
-_DEPS=dns.h servers.h
+_OBJ=main.o dns.o slist.o
+_DEPS=dns.h servers.h slist.h
IDIR=include
CFLAGS=-I$(IDIR) -Wall -g
ODIR=obj
diff --git a/include/slist.h b/include/slist.h
index fc12640..2533c33 100644
--- a/include/slist.h
+++ b/include/slist.h
@@ -15,3 +15,5 @@
int free_hosts_list(struct hosts_list **head);
int add_dns_server(struct dns_list **head, char *server);
int free_dns_list(struct dns_list **head);
+int sort_servers(struct dns_list **headRef);
+int print_servers(struct dns_list *head);
diff --git a/main.c b/main.c
index 009234a..3db7eee 100644
--- a/main.c
+++ b/main.c
@@ -9,78 +9,87 @@
#define NUM_TESTS 10
-struct timespec test_server(char *dns_addr, struct hosts_list *hosts);
+int test_dns(struct dns_list *dnss, struct hosts_list *hosts, int num_hosts, int num_tests);
+struct timespec test_server(struct dns_list *dns, struct hosts_list *hosts, int num_hosts, int num_tests);
int main(int argc, char** argv)
{
- int option, type = T_A;
- char* server = "1.1.1.1", *hostname = "google.com";
+ int option, num_hosts = 0, added_hosts = 0, num_tests = NUM_TESTS;
struct hosts_list *servers_hosts = NULL;
struct dns_list *servers_dns = NULL;
- while((option = getopt(argc, argv, "s:h:t:")) != -1) {
+ while((option = getopt(argc, argv, "s:h:t:n:")) != -1) {
switch (option) {
case 's': //server to use
add_dns_server(&servers_dns, optarg);
break;
case 'h': //hostname to search
add_hosts_server(&servers_hosts, optarg);
+ added_hosts++;
break;
- case 't':
- if (!strcmp(optarg, "A")) {
- type = T_A;
- } else if (!strcmp(optarg, "AAAA")) {
- type = T_AAAA;
- } else if (!strcmp(optarg, "NS")) {
- type = T_NS;
- } else if (!strcmp(optarg, "CNAME")) {
- type = T_CNAME;
- } else if (!strcmp(optarg, "SOA")) {
- type = T_SOA;
- } else if (!strcmp(optarg, "PTR")) {
- type = T_PTR;
- } else if (!strcmp(optarg, "MX")) {
- type = T_MX;
- } else if (!strcmp(optarg, "TXT")) {
- type = T_TXT;
- } else {
- printf("Error: %s is not a valid DNS record type\n", optarg);
- free_hosts_list(&servers_hosts);
- free_dns_list(&servers_dns);
- exit(1);
- }
+ case 't': //set number of hosts to test
+ num_hosts = atoi(optarg);
+ break;
+ case 'n': //number of tests to perform
+ num_tests = atoi(optarg);
break;
case '?':
default:
printf("Error: invalid option -%c\n", optopt);
+ free_dns_list(&servers_dns);
+ free_hosts_list(&servers_hosts);
exit(1);
}
}
- struct timespec total;
- total = test_server(server, servers_hosts);
- printf("Test took %ld.%09lds\n", total.tv_sec, total.tv_nsec);
+ if (num_hosts == 0 || num_hosts > NUM_HOSTNAMES+added_hosts) {
+ num_hosts = NUM_HOSTNAMES + added_hosts;
+ }
+ for (int i = added_hosts; i < num_hosts; i++) {
+ add_hosts_server(&servers_hosts, HOSTNAMES[i-added_hosts]);
+ }
+ for (int i = 0; i < NUM_DNS; i++) {
+ add_dns_server(&servers_dns, DNS_SERVERS[i]);
+ }
+ test_dns(servers_dns, servers_hosts, num_hosts, num_tests);
+ sort_servers(&servers_dns);
+ print_servers(servers_dns);
free_dns_list(&servers_dns);
free_hosts_list(&servers_hosts);
return 0;
}
-struct timespec test_server(char* dns_addr, struct hosts_list *hosts)
+int test_dns(struct dns_list *dnss, struct hosts_list *hosts, int num_hosts, int num_tests)
{
- struct timespec total;
- total.tv_sec = 0; total.tv_nsec = 0;
- for (int i = 0; i < NUM_TESTS; i++) {
+ struct dns_list *curr = dnss;
+ while (curr) {
+ test_server(curr, hosts, num_hosts, num_tests);
+ curr = curr->next;
+ }
+ return 0;
+}
+
+struct timespec test_server(struct dns_list *dns, struct hosts_list *hosts, int num_hosts, int num_tests)
+{
+ unsigned long long avg_nsec = 0;
+ dns->time.tv_sec = 0; dns->time.tv_nsec = 0;
+ for (int i = 0; i < num_tests; i++) {
struct hosts_list *curr = hosts;
while (curr) {
struct timespec run;
unsigned char buf[65536];
- run = resolve(buf, curr->server, dns_addr, T_A);
- total.tv_sec += run.tv_sec;
- total.tv_nsec += run.tv_nsec;
- if (total.tv_nsec >= 1000000000) {
- total.tv_sec += 1;
- total.tv_nsec -= 1000000000;
+ run = resolve(buf, curr->server, dns->server, T_A);
+ if (run.tv_sec == -1)
+ continue;
+ dns->time.tv_sec += run.tv_sec;
+ dns->time.tv_nsec += run.tv_nsec;
+ if (dns->time.tv_nsec >= 1000000000) {
+ dns->time.tv_sec += 1;
+ dns->time.tv_nsec -= 1000000000;
}
curr = curr->next;
}
}
- return total;
+ avg_nsec = 1000000000*(dns->time.tv_sec%(num_hosts*num_tests))+dns->time.tv_nsec;
+ dns->time.tv_sec = dns->time.tv_sec/(num_hosts*num_tests);
+ dns->time.tv_nsec = avg_nsec/(num_hosts*num_tests);
+ return dns->time;
}
diff --git a/servers.c b/servers.c
deleted file mode 100644
index 3e2d918..0000000
--- a/servers.c
+++ /dev/null
@@ -1,61 +0,0 @@
-#include <stdlib.h>
-#include "slist.h"
-
-int add_hosts_server(struct hosts_list **head, char *server)
-{
- struct hosts_list *end;
- if (!(*head)) {
- *head = malloc(sizeof(struct hosts_list));
- end = *head;
- } else {
- end = *head;
- while (end->next)
- end = end->next;
- end->next = malloc(sizeof(struct hosts_list));
- end = end->next;
- }
- end->next = NULL;
- end->server = server;
- return 0;
-}
-
-int free_hosts_list(struct hosts_list **head)
-{
- struct hosts_list *temp;
- while (*head) {
- temp = (*head)->next;
- free(*head);
- *head = temp;
- }
- return 0;
-}
-
-int add_dns_server(struct dns_list **head, char* server)
-{
- struct dns_list *end;
- if (!(*head)) {
- *head = malloc(sizeof(struct dns_list));
- end = *head;
- } else {
- end = *head;
- while (end->next)
- end = end->next;
- end->next = malloc(sizeof(struct dns_list));
- end = end->next;
- }
- end->next = NULL;
- end->server = server;
- end->time.tv_nsec = 0; end->time.tv_sec = 0;
- return 0;
-}
-
-int free_dns_list(struct dns_list **head)
-{
- struct dns_list *temp;
- while (*head) {
- temp = (*head)->next;
- free(*head);
- *head = temp;
- }
- return 0;
-}
diff --git a/slist.c b/slist.c
new file mode 100644
index 0000000..7aa0413
--- /dev/null
+++ b/slist.c
@@ -0,0 +1,137 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "slist.h"
+
+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 add_hosts_server(struct hosts_list **head, char *server)
+{
+ struct hosts_list *end;
+ if (!(*head)) {
+ *head = malloc(sizeof(struct hosts_list));
+ end = *head;
+ } else {
+ end = *head;
+ while (end->next)
+ end = end->next;
+ end->next = malloc(sizeof(struct hosts_list));
+ end = end->next;
+ }
+ end->next = NULL;
+ end->server = server;
+ return 0;
+}
+
+int free_hosts_list(struct hosts_list **head)
+{
+ struct hosts_list *temp;
+ while (*head) {
+ temp = (*head)->next;
+ free(*head);
+ *head = temp;
+ }
+ return 0;
+}
+
+int add_dns_server(struct dns_list **head, char* server)
+{
+ struct dns_list *end;
+ if (!(*head)) {
+ *head = malloc(sizeof(struct dns_list));
+ end = *head;
+ } else {
+ end = *head;
+ while (end->next)
+ end = end->next;
+ end->next = malloc(sizeof(struct dns_list));
+ end = end->next;
+ }
+ end->next = NULL;
+ end->server = server;
+ end->time.tv_nsec = 0; end->time.tv_sec = 0;
+ return 0;
+}
+
+int free_dns_list(struct dns_list **head)
+{
+ struct dns_list *temp;
+ while (*head) {
+ temp = (*head)->next;
+ free(*head);
+ *head = temp;
+ }
+ return 0;
+}
+
+int sort_servers(struct dns_list **headRef)
+{
+ struct dns_list *head = *headRef;
+ struct dns_list *a, *b;
+ if (!head || !(head->next)) { //Empty list or containing one element
+ return 0;
+ }
+ split(head, &a, &b);
+ sort_servers(&a);
+ sort_servers(&b);
+ *headRef = merge(a, b);
+ return 0;
+}
+
+void split(struct dns_list *head, struct dns_list **a, struct dns_list **b)
+{
+ struct dns_list *fast = head->next, *slow = head;
+ while(fast) {
+ fast = fast->next;
+ if (fast) {
+ slow = slow->next;
+ fast = fast->next;
+ }
+ }
+ *a = head;
+ *b = slow->next;
+ slow->next = NULL;
+}
+
+struct dns_list *merge(struct dns_list *a, struct dns_list *b)
+{
+ struct dns_list *out = NULL;
+ int comp;
+ if (!a)
+ return b;
+ if (!b)
+ return a;
+
+ if (comp_times(a->time, b->time) > 0) {
+ out = b;
+ out->next = merge(a, b->next);
+ } else {
+ out = a;
+ out->next = merge(a->next, b);
+ }
+ return out;
+}
+
+int comp_times(struct timespec a, struct timespec b)
+{
+ if (a.tv_sec == b.tv_sec) {
+ if (a.tv_nsec >= b.tv_nsec)
+ return 1;
+ else
+ return -1;
+ } else if (a.tv_sec > b.tv_sec) {
+ return 1;
+ } else
+ return -1;
+}
+
+int print_servers(struct dns_list *head)
+{
+ printf("%-20s | %s\n", "Server", "Time");
+ while (head) {
+ printf("%-20s | %ld.%09ld\n", head->server, head->time.tv_sec, head->time.tv_nsec);
+ head = head->next;
+ }
+ return 0;
+}
--
Gitblit v1.10.0