-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsquery.c
446 lines (384 loc) · 13.1 KB
/
dnsquery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include "headers.h"
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdint.h>
#define MAX_DATA_LENGTH 1000
uint8_t *processName(uint8_t *bstart, uint8_t *bcur, char *name);
void name_encode(char* name, char* name_encoded);
struct DNS_header {
uint16_t id; /* identification number */
uint8_t rd :1; /* recursion desired */
uint8_t tc:1; /* truncated message*/
uint8_t aa :1; /* authoritive answer*/
uint8_t opcode :4; /* purpose of message*/
uint8_t qr :1; /* query/response flag*/
uint8_t rcode :4; /* response code*/
uint8_t cd :1; /* checking disabled*/
uint8_t ad :1; /* authenticated data*/
uint8_t z :1; /* its z! reserved*/
uint8_t ra :1; /* recursion available*/
uint16_t q_count; /* number of question entries*/
uint16_t ans_count; /* number of answer entries*/
uint16_t auth_count; /* number of authority entries*/
uint16_t add_count; /* number of resource entries*/
};
struct DNS_query {
uint16_t type;
uint16_t class;
};
typedef struct {
char *name;
struct DNS_query *q;
} QUERY;
struct RESPONSE_fields {
uint16_t type;
uint16_t class;
uint32_t ttl;
uint16_t dl;
};
struct RESPONSE{
char *name;
struct RESPONSE_fields *rf;
char *res_data;
};
#define BUF_SIZE 65536
#define NAME_SIZE 100
char* dns_query(char *domain, int query_type, size_t *response_to_client_size){
char *response_to_client, *text, *ipaddress;
int flag = 1;
char name_dotted[NAME_SIZE];
char buff[BUF_SIZE], *name, buff_rec[BUF_SIZE];
uint8_t *pointer;
struct RESPONSE_fields *res_fields;
struct RESPONSE *answer;
struct DNS_query *question;
int i, j, bytes, bytes_rec;
int sockfd;
struct DNS_header *uheader;
/*struct sockaddr_in dest_address; */
struct timeval tv;
/*printf("entered hostname: %s\n", argv[1]);*/
/* bzero(buff, 65536);*/
tv.tv_sec = 10;
tv.tv_usec = 0;
uheader = (struct DNS_header *) buff;
uheader->id = getpid();
uheader->qr = 0;
uheader->opcode = 0;
uheader->aa = 0;
uheader->tc = 0;
uheader->rd = 1; /*query is recursive*/
uheader->ra = 0;
uheader->z = 0;
uheader->ad = 0;
uheader->cd = 0;
uheader->rcode = 0;
uheader->q_count = htons(1);
uheader->ans_count = 0;
uheader->auth_count = 0;
uheader->add_count = 0;
/**************IPv6 DNS server starts*******************/
/*Uncomment the lines insidethe IPv6 DNS server to use IPv6 address DNS
* address */
/*ipaddress = "2001:708:30:10::2";*/
/*ipaddress = "2001:4860:4860::8888";*/
/*{*/
/*struct sockaddr_in6 dest_address;*/
/*sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);*/
/*setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));*/
/*if(sockfd < 0){*/
/*perror("error creating socket\n");*/
/*}*/
/*bzero(&dest_address, sizeof(dest_address));*/
/*dest_address.sin6_family = AF_INET6;*/
/*dest_address.sin6_port = htons(53);*/
/*inet_pton(AF_INET6, ipaddress, &dest_address.sin6_addr);*/
/**************IPv6 DNS server ends *******************/
/**************IPv4 DNS server starts*******************/
/*Uncomment the lines inside the IPv4 DNS server to use IPv4 address DNS
* address */
/* create a UDP socket*/
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/*set socket flags for timeout*/
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
/*[>ipaddress = "208.67.222.222";<]*/
/*[>ipaddress = "195.148.124.10";<]*/
ipaddress = "8.8.8.8";
/*[>ipaddress = "4.85.23.113";<]*/
if(sockfd < 0){
perror("error creating socket\n");
}
{ struct sockaddr_in dest_address;
bzero(&dest_address, sizeof(dest_address));
dest_address.sin_family = AF_INET;
dest_address.sin_port = htons(53);
dest_address.sin_addr.s_addr = inet_addr(ipaddress);
/*******************IPv4 DNS address ends****************************/
name = &buff[sizeof(struct DNS_header)];
name_encode(domain, name);
question = (struct DNS_query *) &buff[sizeof(struct DNS_header) + strlen((const char *) name) + 1];
/*query type*/
question->type = htons(query_type);
question->class = htons(1);
/* printf("%s\n", name);*/
/* printf("%d\n", question->type);*/
/*printf("sockfd: %i\n", sockfd);*/
/* printf("name: %s\n", name);*/
bytes = sendto(sockfd, buff, sizeof(struct DNS_header) + sizeof(struct DNS_query) + strlen((const char *) name) + 1, 0, (struct sockaddr *) &dest_address, sizeof(dest_address));
}
printf("bytes sent to DNS server: %d\n", bytes);
if (bytes == -1){
perror("sendto error\n");
return http_response(0, strlen("error sending request to server. Network unreachable"), "error sending request to server. Network unreachable", response_to_client_size);
}
bzero(buff_rec, BUF_SIZE);
bytes_rec = recvfrom(sockfd, &buff_rec, BUF_SIZE, 0, NULL, 0);
printf("bytes received: %d\n", bytes_rec);
if (bytes_rec < 0){
/*char *response_to_client;*/
perror("recvfrom:\n");
if (errno == EWOULDBLOCK){
fprintf(stderr, "UDP socket timeout. Response UDP packet never reached DNS proxy\n");
flag = 0;
text = "UPD socket timeout. Response UDP packet was either lost, or such a DNS server doesn't exist";
response_to_client = http_response(flag, strlen(text), text, response_to_client_size);
/* printf("%s\n", response_to_client);*/
return response_to_client;
} else {
perror("receive error\n");
}
}
printf("bytes received from DNS server in response: %d\n", bytes_rec);
uint16_t que_num, ans_num, auth_num, add_num;
uint8_t response_code, qr;
uheader = (struct DNS_header *) buff_rec;
response_code = uheader->rcode;
if (response_code != 0){
flag = 0;
switch (response_code){
case 1:
text = "Incorrect DNS format of supplied name";
break;
case 2:
text = "There was a server failure. Try again later";
break;
case 3:
text = "The name supplied does not exist";
break;
case 4:
text = "Following query is not supported";
break;
case 5:
text = "Refused to resolve the name";
break;
}
response_to_client = http_response(flag, strlen(text), text, response_to_client_size);
return response_to_client;
}
qr = uheader->qr;
que_num = ntohs(uheader->q_count);
ans_num = ntohs(uheader->ans_count);
if (ans_num == 0){
text = "No records found";
flag = 0;
response_to_client = http_response(flag, strlen(text), text, response_to_client_size);
return response_to_client;
}
auth_num = ntohs(uheader->auth_count);
add_num = ntohs(uheader->add_count);
printf("response question number: %d\nresponse answer number: %d\n", que_num, ans_num);
printf("response code: %d, query/response: %d\n", response_code, qr);
printf("response authoritative records number: %d\nresponse additional records number: %d\n", auth_num, add_num);
answer = calloc(ans_num, sizeof(struct RESPONSE));
/*point to part after DNS header*/
pointer = (unsigned char *) &buff_rec[(sizeof(struct DNS_header) + strlen((const char *) name) + 1 + sizeof(struct DNS_query))];
struct RESPONSE_fields rf;
const char *ret_val;
i=0;
char response_parsed[NAME_SIZE][MAX_DATA_LENGTH];
bzero(response_parsed, NAME_SIZE*MAX_DATA_LENGTH);
/*processing answers section*/
for (j=0; j<ans_num; j++){
bzero(name_dotted, NAME_SIZE);
pointer = processName ((unsigned char*) buff_rec, pointer, name_dotted);
answer[j].name = name_dotted;
/* printf("response name: %s\n", name_dotted);*/
/*point to response fields structure: type, class, TTL, data length */
res_fields = (struct RESPONSE_fields *) pointer;
rf.type = ntohs(res_fields->type);
rf.class = ntohs(res_fields->class);
rf.ttl = ntohs(res_fields->ttl);
rf.dl = ntohs(res_fields->dl);
/* uint16_t size = rf.dl;*/
char address[MAX_DATA_LENGTH];
answer[j].res_data = address;
answer[j].rf = &rf;
/* printf("response type: %d\nresponse class: %d\nresponse TTL: %d\nresponse data length: %d\n", type, class, TTL, resource_data_length);*/
/*point to the beginning of resource data, 10 = sizeof RESPONSE_length*/
pointer = pointer + 10;
struct in_addr * ipv4_addr;
struct in6_addr * ipv6_addr;
short preference;
int pref_len;
switch (rf.type) {
case 1: // A
ipv4_addr = (struct in_addr *) pointer;
ret_val = inet_ntop(AF_INET, ipv4_addr, answer[j].res_data , INET_ADDRSTRLEN);
break;
case 28: // AAAA
ipv6_addr = (struct in6_addr *) pointer;
ret_val = inet_ntop(AF_INET6, ipv6_addr, answer[j].res_data , INET6_ADDRSTRLEN);
break;
case 15: // MX
// MX record consists of 2 bytes representing "preference" which are
// followed by DNS-encoded host name
preference = ntohs(*(short *) pointer);
pref_len = sprintf(answer[j].res_data, "%d ", preference);
processName((unsigned char*) buff_rec, pointer + 2, answer[j].res_data + pref_len);
// set to non-zero value to prevent error indication
ret_val = (const char *) 1;
break;
}
if (!ret_val){
perror("error converting an IP address\n");
flag = 0;
/*TODO follow http_response*/
break;
}
else {
/* printf("response ip address: %s\n", answer[j].res_data);*/
/*copy the IP address with the null byte*/
/*case when the response is */
strcpy(response_parsed[j], answer[j].res_data);
/* printf("ip address: %s", response_parsed[j]);*/
}
pointer = pointer + answer[j].rf->dl;
}
char unidim_response[MAX_DATA_LENGTH*ans_num];
bzero(unidim_response, MAX_DATA_LENGTH*ans_num);
int k=0;
/*convert 2-d answers section into 1-d answers section*/
for (j=0; j<ans_num; j++){
for (i=0; i<strlen(response_parsed[j]); i++){
unidim_response[k] = response_parsed[j][i];
/* printf("%c", unidim_response[k]);*/
k++;
}
unidim_response[k] = '\n';
k++;
/* printf("\n");*/
}
/* for (j=0; j<ans_num; j++){
printf("%s\n", unidim_response);
}*/
/* printf("size of content in dnsquery.c: %d\n", ans_num*INET_ADDRSTRLEN);*/
response_to_client = http_response(flag, k, unidim_response, response_to_client_size);
/*printf("response_to_client: %s\n", response_to_client);*/
/*free(response_to_client);*/
free(answer);
close(sockfd);
return response_to_client;
}
uint8_t *processName(uint8_t *bstart, uint8_t *bcur, char *name)
{
uint8_t *p = bcur;
char strbuf[80];
char *strp;
int compressed = 0;
name[0] = 0;
do {
strp = strbuf;
if ((*p & 0xc0) == 0xc0) { /* first two bits are set =>
compressed format */
uint16_t offset = (*p & 0x3f);
offset = (offset << 8) + *(p+1);
p = bstart + offset; /* move the read pointer to the
offset given in message */
if (!compressed) bcur += 2; /* adjustment of bcur must
only be done once, in
case there are multiple
nested pointers in msg */
compressed = 1;
} else if (*p > 0) {
/* strbuf contains one element of name, not full name*/
memcpy(strbuf, p+1, *p);
strp += *p;
p += *p + 1;
if (!compressed)
bcur = p; /* adjustment of bcur based on string
string length is only done if it
was not compressed, otherwise it
is assumed to be 16 bits always */
*strp = '.';
*(strp+1) = 0;
strcat(name, strbuf);
}
} while (*p > 0);
if (!compressed) bcur++; /* compensate for trailing 0 (unless name was
compressed) */
return bcur;
}
/* function that does conversion www.gmail.com -> */
void name_encode(char* name, char* name_encoded){
char *ptr;
int diff, i;
ptr = strchr(name, '.');
/*if (ptr == NULL) {
printf("no dot found\n");
name_encoded[0]=strlen(name);
for (i=1; i<strlen(name); i++){
name_encoded[i] = name[i-1];
}
name_encoded[i] = 0;
printf("name encoded: %s\n", name_encoded);
} else {*/
diff = ptr - name;
name_encoded[0] = diff;
for (i=0; i<strlen(name); i++){
name_encoded[i+1] = name[i];
if (name[i] == '.'){
ptr = strchr(&name[i+1], '.');
if (ptr == NULL){
ptr = strchr(&name[i], '\0');
}
diff = ptr - &name[i] - 1;
name_encoded[i+1] = diff;
}
}
name_encoded[i+1] = 0;
/*}*/
/*printf ("%s, strlen=%lu\n", name_encoded, strlen(name_encoded));*/
}
/* printf("The number of questions: %d\n", ntohs(uheader->q_count));
printf("The number of answers: %d\n", ntohs(uheader->ans_count)); */
/* printf("sizeof(unsigned int) = %lu sizeof(unsigned short) = %lu\n", sizeof(unsigned int), sizeof(unsigned short));
printf("sizeof(unsigned char) = %lu sizeof(char) = %lu\n", sizeof(unsigned char), sizeof(char));
*/
/* printf("sizeof(uint8_t)=%lu, sizeof(char)=%lu\n", sizeof(uint8_t), sizeof(char));*/
/* Processes one string in a DNS resource record.
*
* bstart (in): pointer to the start of the DNS message (UDP payload)
* bcur (in): pointer to the currently processed position in a message.
This should point to the start of compressed or uncompressed
name string.
* name (out): buffer for storing the name string in dot-separated format
*
* returns: updated position of bcur, pointing to the next position
* following the name
*/
/* TODO
* add malloc into the loop and assign to each var values that are in order to save them into another array
* case when the response cannot
*
*
*
* */