From 2edf2937403e6448f155b31403a4c43d5fe277d4 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Sun, 18 Oct 2020 13:03:16 +0000
Subject: [PATCH] added command line arguments

---
 dns.c |   90 +++++++++++++++++++++++++++------------------
 1 files changed, 54 insertions(+), 36 deletions(-)

diff --git a/dns.c b/dns.c
index af08c44..068753f 100644
--- a/dns.c
+++ b/dns.c
@@ -5,10 +5,13 @@
 #include <arpa/inet.h>
 //#include <netint/in.h>
 //#include <netdb.h>
-#include <sys/time.h>
+//#include <sys/time.h>
 #include <unistd.h>
 #include "dns.h"
 
+void change_to_DNS_name_format(unsigned char* dns, unsigned char* host);
+char* read_name(unsigned char* reader, unsigned char* buffer, int* count);
+
 // DNS code copied from
 // https://gist.github.com/fffaraz/9d9170b57791c28ccda9255b48315168
 
@@ -26,7 +29,7 @@
 	 unsigned char ad :1;		//authenticated data
 	 unsigned char z :1;		//reserved for future use
 	 unsigned char ra :1;		//recursion available
-	 unsigned short q_count;	//number of question entries
+	 unsigned short q_count;	//number of question entrise
 	 unsigned short ans_count;	//number of answer entries
 	 unsigned short auth_count; //number of authority entries
 	 unsigned short add_count;  //number of resource entries
@@ -61,17 +64,17 @@
 struct RES_RECORD
 {
 	 unsigned char* name;
-	 struct R_DATA* resource;
+	 struct R_DATA resource;
 	 unsigned char* rdata;
 };
 
-void resolve(char* hostname, char* dns_ip, int query_type, int read)
+void resolve(unsigned char* buf, char* hostname, char* dns_ip, int query_type)
 {
 	 int s, i, j;
 	 struct sockaddr_in dest, a;
-	 unsigned char buf[65536], *qname, *reader;
-	 struct DNS_HEADER* dns = NULL;
-	 struct QUESTION* qinfo = NULL;
+	 unsigned char *qname, *reader;
+	 struct DNS_HEADER* dns = (struct DNS_HEADER*)buf;
+	 struct QUESTION* qinfo;
 
 	 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	 dest.sin_family = AF_INET;
@@ -79,7 +82,6 @@
 	 dest.sin_addr.s_addr = inet_addr(dns_ip);
 
 	 //dns packet header
-	 dns = (struct DNS_HEADER*)&buf;
 	 dns->id = (unsigned short) htons(getpid());
 	 dns->qr = 0;				//make query
 	 dns->opcode = 0;			//standard query
@@ -111,11 +113,6 @@
 	 //negative return is a fail
 	 i = sizeof(dest);
 	 recvfrom(s, (char*)buf, 65536, 0, (struct sockaddr*)&dest, (socklen_t*)&i);
-
-	 //read response
-	 if (read) {
-		  print_packet(buf);
-	 }
 	 return;
 }
 
@@ -130,80 +127,101 @@
 	 reader = &buf[sizeof(struct DNS_HEADER)+strlen((const char*)qname)+1+sizeof(struct QUESTION)];
 	 printf("Response contains %d Qs, %d ans, %d auth serv, %d add reconds\n", ntohs(dns->q_count), ntohs(dns->ans_count), ntohs(dns->auth_count), ntohs(dns->add_count));
 	 stop = 0;
+
+	 //read answers
 	 for (i = 0; i < ntohs(dns->ans_count); i++) {
 		  answers[i].name = read_name(reader, buf, &stop);
 		  reader = reader + stop;
-		  answers[i].resource = (struct R_DATA*)reader;
+		  memcpy(&(answers[i].resource), reader, sizeof(struct R_DATA));
 		  reader = reader+sizeof(struct R_DATA);
-		  
-		  if (ntohs(answers[i].resource->type) == T_A) { //IPv4 address
-			   answers[i].rdata = (unsigned char*)malloc(ntohs(answers[i].resource->data_len));
-			   for (j = 0; j < ntohs(answers[i].resource->data_len); j++) {
+
+		  if (ntohs(answers[i].resource.type) == T_A) { //IPv4 address
+			   answers[i].rdata = (unsigned char*)malloc(ntohs(answers[i].resource.data_len));
+			   for (j = 0; j < ntohs(answers[i].resource.data_len); j++) {
 					answers[i].rdata[j] = reader[j];
 			   }
-			   answers[i].rdata[ntohs(answers[i].resource->data_len)] = '\0';
-			   reader = reader + ntohs(answers[i].resource->data_len);
+			   answers[i].rdata[ntohs(answers[i].resource.data_len)] = '\0';
+			   reader = reader + ntohs(answers[i].resource.data_len);
 		  } else {
 			   answers[i].rdata = read_name(reader, buf, &stop);
 			   reader = reader + stop;
 		  }
 	 }
-	 
+
 	 //read authorities
 	 for (i = 0; i < ntohs(dns->auth_count); i++) {
 		  auth[i].name = read_name(reader, buf, &stop);
 		  reader += stop;
-		  auth[i].resource = (struct R_DATA*)reader;
+		  memcpy(&(auth[i].resource), reader, sizeof(struct R_DATA));
 		  reader += sizeof(struct R_DATA);
 		  auth[i].rdata = read_name(reader, buf, &stop);
 		  reader += stop;
 	 }
-	 
+
 	 //read additional
 	 for (i = 0; i < ntohs(dns->add_count); i++) {
 		  addit[i].name = read_name(reader, buf, &stop);
 		  reader += stop;
-		  addit[i].resource = (struct R_DATA*)reader;
+		  memcpy(&(addit[i].resource), reader, sizeof(struct R_DATA));
 		  reader += sizeof(struct R_DATA);
 
-		  if (ntohs(addit[i].resource->type) == 1) {
-			   addit[i].rdata = malloc(ntohs(addit[i].resource->data_len));
-			   for (j = 0; j < ntohs(addit[i].resource->data_len); j++)
+		  if (ntohs(addit[i].resource.type) == 1) {
+			   addit[i].rdata = malloc(ntohs(addit[i].resource.data_len));
+			   for (j = 0; j < ntohs(addit[i].resource.data_len); j++)
 					addit[i].rdata[j] = reader[j];
-			   
-			   addit[i].rdata[ntohs(addit[i].resource->data_len)] = '\0';
-			   reader += ntohs(addit[i].resource->data_len);
+			   addit[i].rdata[ntohs(addit[i].resource.data_len)] = '\0';
+			   reader += ntohs(addit[i].resource.data_len);
 		  } else {
 			   addit[i].rdata = read_name(reader, buf, &stop);
 			   reader += stop;
 		  }
 	 }
-	 
+
 	 //print answers
 	 printf("ans recs: %d\n", ntohs(dns->ans_count));
 	 for (i = 0; i < ntohs(dns->ans_count); i++) {
 		  printf("name: %s ", answers[i].name);
-		  if (ntohs(answers[i].resource->type) == T_A) { //IPv4
+		  if (ntohs(answers[i].resource.type) == T_A) { //IPv4
 			   long* p;
 			   p = (long*)answers[i].rdata;
 			   a.sin_addr.s_addr=(*p);
 			   printf("has IPv4 addresss: %s", inet_ntoa(a.sin_addr));
-		  } else if (ntohs(answers[i].resource->type) == T_CNAME) { //CNAME
+		  } else if (ntohs(answers[i].resource.type) == T_CNAME) { //CNAME
 			   printf("has alias: %s", answers[i].rdata);
 		  }
+		  free(answers[i].name);
+		  free(answers[i].rdata);
 		  putc('\n', stdout);
 	 }
 
 	 //print authorities
 	 printf("Auth recs: %d\n", ntohs(dns->auth_count));
 	 for (i = 0; i < ntohs(dns->auth_count); i++) {
-		  printf("name: %s ", addit[i].name);
-		  if (ntohs(addit[i].resource->type) == 1) {
+		  printf("name: %s ", auth[i].name);
+		  if (ntohs(auth[i].resource.type) == 1) {
 			   long* p;
-			   p = (long*)addit[i].rdata;
+			   p = (long*)auth[i].rdata;
 			   a.sin_addr.s_addr = *p;
 			   printf("has IPv4 address: %s", inet_ntoa(a.sin_addr));
 		  }
+		  free(answers[i].name);
+		  free(auth[i].rdata);
+		  putc('\n', stdout);
+	 }
+
+	 printf("Addit recs: %d\n", ntohs(dns->add_count));
+	 for (i = 0; i < ntohs(dns->add_count); i++) {
+		  printf("name; %s", addit[i].name);
+		  if (ntohs(auth[i].resource.type) == 1) {
+			   long *p;
+			   p = (long*)addit[i].rdata;
+			   a.sin_addr.s_addr = *p;
+			   printf("has IPv4 address: %s", inet_ntoa(a.sin_addr));
+		  } else {
+			   printf("has %s", addit[i].rdata);
+		  }
+		  free(answers[i].name);
+		  free(addit[i].rdata);
 		  putc('\n', stdout);
 	 }
 }

--
Gitblit v1.10.0