From d3f3ee16d8adaef681afed67f236e3bb14e7cdc0 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 21 Oct 2020 13:05:26 +0000 Subject: [PATCH] change artifact name and added penalty for failed connections --- dns.c | 134 +++++++++++++++++++++++++++++--------------- 1 files changed, 87 insertions(+), 47 deletions(-) diff --git a/dns.c b/dns.c index af08c44..ad58873 100644 --- a/dns.c +++ b/dns.c @@ -5,10 +5,14 @@ #include <arpa/inet.h> //#include <netint/in.h> //#include <netdb.h> -#include <sys/time.h> +//#include <sys/time.h> +#include <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 +30,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 @@ -60,26 +64,30 @@ /* Pointers to record components */ struct RES_RECORD { - unsigned char* name; - struct R_DATA* resource; - unsigned char* rdata; + char* name; + struct R_DATA resource; + char* rdata; }; -void resolve(char* hostname, char* dns_ip, int query_type, int read) +// Test server dns_ip as IPv4 string for hostname +// Writes received packet to buf, which is supplied and returns time for request +struct timespec 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; + int s, i; + struct sockaddr_in dest; + unsigned char *qname; + struct DNS_HEADER* dns = (struct DNS_HEADER*)buf; + struct QUESTION* qinfo; + struct timespec start, end, total, timeout; + timeout.tv_nsec=0; timeout.tv_sec=1; s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timespec)); //use a 1 second timeout for receiving, should be more than enough and anything more is really bad dest.sin_family = AF_INET; dest.sin_port = htons(53); 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 @@ -105,118 +113,149 @@ //send request // return less than 0 is a fail + clock_gettime(CLOCK_MONOTONIC, &start); sendto(s,(char*)buf, sizeof(struct DNS_HEADER)+strlen((const char*)qname)+1+sizeof(struct QUESTION), 0, (struct sockaddr*)&dest, sizeof(dest)); //receive response //negative return is a fail i = sizeof(dest); - recvfrom(s, (char*)buf, 65536, 0, (struct sockaddr*)&dest, (socklen_t*)&i); + i = recvfrom(s, (char*)buf, 65536, 0, (struct sockaddr*)&dest, (socklen_t*)&i); + clock_gettime(CLOCK_MONOTONIC, &end); - //read response - if (read) { - print_packet(buf); - } - return; + // Make sure packet was returned + if (i == -1) + total.tv_nsec = -1; + else + total.tv_sec = end.tv_sec - start.tv_sec; + if ((end.tv_nsec - start.tv_nsec) < 0) + total.tv_nsec = start.tv_nsec - end.tv_nsec; + else + total.tv_nsec = end.tv_nsec - start.tv_nsec; + close(s); + return total; } +// Print dns packet content, not terribly reliable but works for testing resolve with A requests void print_packet(unsigned char* buf) { struct RES_RECORD answers[20], auth[20], addit[20]; struct DNS_HEADER *dns; struct sockaddr_in a; - char* qname = buf+sizeof(struct DNS_HEADER), *reader; + unsigned char* qname = buf+sizeof(struct DNS_HEADER), *reader; int stop, i, j; dns = (struct DNS_HEADER*)buf; 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 = (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); } } - +// convert from dot format to dns format +// eg google.com to 6google3com void change_to_DNS_name_format(unsigned char* dns, unsigned char* host) { int lock = 0; char h[300]; - strcpy(h, host); + strcpy(h, (char*)host); strcat((char*)h,"."); - for (int i = 0; i < strlen((char*)h); i++) { + for (int i = 0; i < strlen(h); i++) { if (h[i] == '.') { *dns++ = i-lock; for (;lock<i;lock++) { @@ -228,13 +267,14 @@ *dns++ = '\0'; } +// Convert from dns to dot format char* read_name(unsigned char* reader, unsigned char* buffer, int* count) { - unsigned char* name; + char* name; unsigned int p=0, jumped=0, offset; int i, j; *count = 1; - name = (unsigned char*)malloc(256); + name = (char*)malloc(256); name[0]='\0'; while (*reader != 0) { -- Gitblit v1.10.0