From 31afa3d8d6499d119bdf54bd37820b630c9e37e9 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Mon, 19 Oct 2020 03:00:33 +0000
Subject: [PATCH] calculating request time as struct timespec
---
include/dns.h | 2 +-
main.c | 5 ++++-
dns.c | 22 +++++++++++++++++++---
3 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/dns.c b/dns.c
index 7499788..3784d2f 100644
--- a/dns.c
+++ b/dns.c
@@ -6,6 +6,7 @@
//#include <netint/in.h>
//#include <netdb.h>
//#include <sys/time.h>
+#include <time.h>
#include <unistd.h>
#include "dns.h"
@@ -68,15 +69,18 @@
char* rdata;
};
-void resolve(unsigned char* buf, char* hostname, char* dns_ip, int query_type)
+struct timespec resolve(unsigned char* buf, char* hostname, char* dns_ip, int query_type)
{
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);
@@ -107,13 +111,25 @@
//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);
- return;
+ i = recvfrom(s, (char*)buf, 65536, 0, (struct sockaddr*)&dest, (socklen_t*)&i);
+ clock_gettime(CLOCK_MONOTONIC, &end);
+
+ 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;
}
void print_packet(unsigned char* buf)
diff --git a/include/dns.h b/include/dns.h
index b6e6a90..0776827 100644
--- a/include/dns.h
+++ b/include/dns.h
@@ -8,5 +8,5 @@
#define T_TXT 16 // Text record
#define T_AAAA 28 // IPv6 address
-void resolve(unsigned char* buf, char* hostname, char* dns_ip, int query_type);
+struct timespec resolve(unsigned char* buf, char* hostname, char* dns_ip, int query_type);
void print_packet(unsigned char* buf);
diff --git a/main.c b/main.c
index 721f769..166394e 100644
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <time.h>
#include "dns.h"
int main(int argc, char** argv)
@@ -45,7 +46,9 @@
}
}
unsigned char buf[65536];
- resolve(buf, hostname, server, type);
+ 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;
}
--
Gitblit v1.10.0