From dcbb2f131623dfd7ef080b86377ec2d4431e862a Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Sun, 18 Oct 2020 11:50:42 +0000
Subject: [PATCH] Removed pointer reference for memcpy and freeing memory
---
dns.c | 49 ++++++++++++++++++++++++++-----------------------
1 files changed, 26 insertions(+), 23 deletions(-)
diff --git a/dns.c b/dns.c
index af08c44..bfe157d 100644
--- a/dns.c
+++ b/dns.c
@@ -5,7 +5,7 @@
#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"
@@ -61,7 +61,7 @@
struct RES_RECORD
{
unsigned char* name;
- struct R_DATA* resource;
+ struct R_DATA resource;
unsigned char* rdata;
};
@@ -130,67 +130,69 @@
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].rdata);
putc('\n', stdout);
}
@@ -198,12 +200,13 @@
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) {
+ if (ntohs(addit[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));
}
+ free(auth[i].rdata);
putc('\n', stdout);
}
}
--
Gitblit v1.10.0