From 415e3b82ed70c48f61b51b7a93576335af2da7cb Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Mon, 19 Oct 2020 13:18:16 +0000
Subject: [PATCH] Added list for dns servers and hostnames

---
 Makefile        |    4 
 main.c          |  120 +++++++++++++++++++-----------
 servers.c       |   61 +++++++++++++++
 include/slist.h |   17 ++++
 4 files changed, 156 insertions(+), 46 deletions(-)

diff --git a/Makefile b/Makefile
index ce02a41..d0e275b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC=gcc
-_OBJ=main.o dns.o
-_DEPS=dns.h
+_OBJ=main.o dns.o servers.o
+_DEPS=dns.h servers.h
 IDIR=include
 CFLAGS=-I$(IDIR) -Wall -g
 ODIR=obj
diff --git a/include/slist.h b/include/slist.h
new file mode 100644
index 0000000..fc12640
--- /dev/null
+++ b/include/slist.h
@@ -0,0 +1,17 @@
+#include "time.h"
+
+struct hosts_list {
+	 char* server;
+	 struct hosts_list* next;
+};
+
+struct dns_list {
+	 char *server;
+	 struct timespec time;
+	 struct dns_list *next;
+};
+
+int add_hosts_server(struct hosts_list **head, char *server);
+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);
diff --git a/main.c b/main.c
index 166394e..009234a 100644
--- a/main.c
+++ b/main.c
@@ -4,51 +4,83 @@
 #include <string.h>
 #include <time.h>
 #include "dns.h"
+#include "servers.h"
+#include "slist.h"
+
+#define NUM_TESTS 10
+
+struct timespec test_server(char *dns_addr, struct hosts_list *hosts);
 
 int main(int argc, char** argv)
 {
-	 int option, type = T_A;
-	 char* server = "1.1.1.1", *hostname = "google.com";
-	 while((option = getopt(argc, argv, "s:h:t:")) != -1) {
-		  switch (option) {
-		  case 's': //server to use
-			   server = optarg;
-			   break;
-		  case 'h': //hostname to search
-			   hostname = optarg;
-			   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_MX;
-			   } else {
-					printf("Error: %s is not a valid DNS record type\n", optarg);
-					exit(1);
-			   }
-			   break;
-		  case '?':
-		  default:
-			   printf("Error: invalid option -%c\n", optopt);
-			   exit(1);
-		  }
-	 }
-	 unsigned char buf[65536];
-	 struct timespec time;
-	 time = resolve(buf, hostname, server, type);
-	 printf("Request took %ld.%09lds\n", time.tv_sec, time.tv_nsec);
-	 print_packet(buf);
-	 return 0;
+	int option, type = T_A;
+	char* server = "1.1.1.1", *hostname = "google.com";
+	struct hosts_list *servers_hosts = NULL;
+	struct dns_list *servers_dns = NULL;
+	while((option = getopt(argc, argv, "s:h:t:")) != -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);
+			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);
+			}
+			break;
+		case '?':
+		default:
+			printf("Error: invalid option -%c\n", optopt);
+			exit(1);
+		}
+	}
+	struct timespec total;
+	total = test_server(server, servers_hosts);
+	printf("Test took %ld.%09lds\n", total.tv_sec, total.tv_nsec);
+	free_dns_list(&servers_dns);
+	free_hosts_list(&servers_hosts);
+	return 0;
+}
+
+struct timespec test_server(char* dns_addr, struct hosts_list *hosts)
+{
+	struct timespec total;
+	total.tv_sec = 0; total.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;
+			}
+			curr = curr->next;
+		}
+	}
+	return total;
 }
diff --git a/servers.c b/servers.c
new file mode 100644
index 0000000..3e2d918
--- /dev/null
+++ b/servers.c
@@ -0,0 +1,61 @@
+#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;
+}

--
Gitblit v1.10.0