-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.c
353 lines (276 loc) · 8.11 KB
/
dns.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
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <arpa/inet.h>
#include "smartalloc.h"
#include "dns.h"
#include "cache.h"
//!!! When storing dns entries sanitize the input by making all chars lower case!!!//
ht_entry *cache = NULL;
void process_pkts(conn *Conn) {
static dns_state *Dstates[USHRT_MAX];
int listen = LISTEN_TRUE;
pkt *RecvPkt = pkt_alloc(DATAGRAM_MAX_SIZE);
int i;
for (i = 0; i < USHRT_MAX; i += 1)
Dstates[i] = NULL;
load_root_servers();
while (listen) {
recv_pkt(Conn, RecvPkt);
process_dns(Dstates, Conn, RecvPkt);
}
}
void process_dns(dns_state *Dstates[], conn *Conn, pkt *InitPkt) {
dns_state *Dstate;
dns_state **temp;
temp = get_dns_state(Dstates, InitPkt);
init_dns_state(temp, Conn, InitPkt);
Dstate = *temp;
while (Dstate->state != S_LISTEN) {
switch (Dstate->state) {
case S_RECV_QUERY:
recv_query(Dstate);
break;
case S_RECV_RESP:
printf("Received Responce\n");
break;
case S_CACHE_CHECK:
cache_check(Dstate);
break;
// case S_QUESTIONS_CHECK:
// break;
case S_RESOLVE:
break;
case S_STORE_IN_CACHE:
break;
case S_SEND_QUERY:
break;
case S_SEND_RESP:
break;
case S_CREATE_RESP:
break;
case S_EXIT:
Dstate->state = S_LISTEN;
break;
default:
fprintf(stderr, "!!Unknown STATE!!\n");
break;
}
}
}
void resolve(dns_state *Dstate) {
//need to check to how to handle cache hit & miss etc.
//is the current question resolved?
//if no
// ask question with current question label ptr
// generate a hash_key
// increment the label ptr
//if the current question is resolved
// does it answer the initial query?
// if yes
// state = generate_response
// if no
// state =
}
void store_in_cache(dns_state *Dstate) {
rr_l *reply_temp = Dstate->reply_rr;
while(reply_temp != NULL) {
add_record(key_generate((char *)reply_temp->rr.name), reply_temp->rr);
reply_temp = reply_temp->next;
}
}
void recv_query(dns_state *Dstate) {
parse_dns_hdr(Dstate);
parse_question(Dstate);
print_name(Dstate->external_q.name);
Dstate->state = S_CACHE_CHECK;//_QUESTIONS_CHECK;
// Dstate->state = S_LISTEN;
}
void cache_check(dns_state *Dstate) {
ht_entry *entry = find_entry(Dstate->question_key);
if (entry == NULL) {
if (Dstate->rd == RECURSE_YES)
Dstate->state = S_RESOLVE;//S_QUESTIONS_CHECK;
Dstate->state = S_CREATE_RESP;
}
Dstate->state = S_RESOLVE;
}
void print_name(name_t *name) {
if (name == NULL)
return;
do {
printf("%s.", name->label);
name = name->next_label;
}while(name != NULL);
printf("\n");
}
void name_to_str(name_t *name, char *namestr) {
int num_chars = 0;
if (name == NULL && namestr == NULL)
return;
if (name == NULL && namestr != NULL) {
namestr[0] = '.';
namestr[1] = '\0';
return;
}
do {
num_chars = snprintf(namestr, DNS_LABEL_MAX_LEN, "%s.", name->label);
namestr += num_chars; //!!CHECK TO MAK SURE NOT GOING BEYOND BOUNDARIES
name = name->next_label;
}while(name != NULL);
namestr[num_chars] = '\0';
}
void parse_question(dns_state *Dstate) {
uint8_t *buff;
uint32_t i;
printf("parsing question\n");
buff = Dstate->recvpkt.datagram + DNS_HDR_SIZE;
copy_name(&(Dstate->recvpkt), buff, &(Dstate->external_q.name));
Dstate->external_q.name_pos = end_of_name(Dstate->external_q.name);
name_to_str(Dstate->external_q.name, Dstate->external_q.namestr);
buff = Dstate->recvpkt.datagram + DNS_HDR_SIZE;
for (i = 0; i < DNS_NAME_MAX_LEN; i++)
if (buff[i] == '\0' || DNS_LABEL_PTR(buff[i]))
break;
buff += 1;
Dstate->external_q.qtype = buff[i];//get qtype
buff += 1;
Dstate->external_q.qclass = buff[i]; //get qclass
printf("qtype: %u :: qclass: %u\n", Dstate->external_q.qtype, Dstate->external_q.qclass);
}
name_t *end_of_name(name_t *name) {
name_t *temp = name;
while (temp->next_label != NULL)
temp = temp->next_label;
return temp;
}
void parse_dns_hdr(dns_state *Dstate) {
static dns_hdr hdr;
memcpy(&hdr, &(Dstate->recvpkt.datagram), sizeof(dns_hdr));
Dstate->id = htons(hdr.id);
Dstate->rd = DNS_HDR_RD(hdr.code_a);
Dstate->aa = DNS_HDR_AA(hdr.code_a);
Dstate->tc = DNS_HDR_TC(hdr.code_a);
Dstate->qr = DNS_HDR_QR(hdr.code_a);
Dstate->qdcount = htons(hdr.qdcount);
Dstate->ancount = htons(hdr.ancount);
Dstate->nscount = htons(hdr.nscount);
Dstate->arcount = htons(hdr.arcount);
}
dns_state *add_dns_state(void) {
dns_state *state = (dns_state *)malloc(sizeof(dns_state));
state->internal_q.name_answered = 0;
state->internal_q.type_answered = 0;
state->external_q.name_answered = 0;
state->external_q.type_answered = 0;
return state;
}
void init_dns_state(dns_state **Dstate, conn *Conn, pkt *Pkt) {
if (*Dstate == NULL) {
printf("Dstate is NULL\n");
*Dstate = add_dns_state();
}
(*Dstate)->conn = *Conn;
(*Dstate)->recvpkt = *Pkt;
(*Dstate)->state = query_or_resp(*Dstate);
(*Dstate)->external_q.name = NULL;
(*Dstate)->internal_q.name = NULL;
bzero((*Dstate)->external_q.namestr, DNS_NAME_MAX_LEN);
bzero((*Dstate)->internal_q.namestr, DNS_NAME_MAX_LEN);
}
m_state query_or_resp(dns_state *Dstate) {
uint8_t qr_field = Dstate->recvpkt.datagram[DNS_HDR_QR_OFFSET];
if(DNS_HDR_QR(qr_field) == QR_QUERY)
return S_RECV_QUERY;
return S_RECV_RESP;
}
dns_state **get_dns_state(dns_state *Dstates[], pkt *Pkt) {
uint16_t id;
memcpy(&id, Pkt->datagram + DNS_HDR_ID_OFFSET, sizeof(uint16_t));
id = ntohs(id);
return Dstates + id;
}
int copy_name(pkt *Pkt, uint8_t *buff, name_t **name) {
static name_meta_t name_meta;
// static name_t *name;
static name_t *name_temp;
printf("copying name\n");
clean_name_meta(&name_meta);
if (Pkt == NULL || buff == NULL)
return 0;
if (*name == NULL)
*name = name_alloc();
name_temp = *name;
while (1) {
if(handle_pointing(Pkt, buff) == PTR_ERR)
return GETNAME_ERR;
name_meta.label_len = (uint16_t)DNS_LABEL_LEN(*buff);
buff += 1;
if (copy_label(&name_meta, &name_temp, buff, Pkt->datagram + Pkt->datagram_len) == END_OF_NAME_TRUE)
return GETNAME_SUCCESS;
if ((*name)->next_label == NULL)
(*name)->next_label = name_alloc();
(*name)->next_label->prev_label = name_temp;
name_temp = (*name)->next_label;
buff += name_meta.chars_read_name;//LABEL_START_OF_STRING(buff);
}
}
int copy_label(name_meta_t *name_meta, name_t **name, uint8_t *src, uint8_t *src_end) {
uint16_t i;
for(i = 0; i < name_meta->label_len && src + i <= src_end; i += 1) {
(*name)->label[i] = src[i];
name_meta->chars_read_name +=1;
}
//propably have an error condition for going outside of dns packet
//propably have an error condition for going beyond max name size
if (src[i] == '\0')
return END_OF_NAME_TRUE;
return END_OF_NAME_FALSE;
}
void clean_name_meta(name_meta_t *name_meta) {
if (name_meta == NULL) {
fprintf(stderr, "clean_name_meta err\n");
return;
}
name_meta->name_len = 0;
name_meta->label_len = 0;
name_meta->chars_read_name = 0;
}
name_t *name_alloc(void) {
name_t *name = (name_t *) malloc(sizeof(name_t));
bzero(name->label, DNS_LABEL_MAX_LEN);
name->next_label = NULL;
name->prev_label = NULL;
return name;
}
int handle_pointing(pkt *Pkt, uint8_t *buff) {
static uint32_t ptr;
ptr = 0;
if (buff == NULL) {
fprintf(stderr, "handl_pointing -- buff NULL\n");
return PTR_ERR;
}
if (DNS_LABEL_PTR(*buff)) {
ptr = (uint32_t)DNS_LABEL_PTR_VALUE(*buff);
if (ptr <= Pkt->datagram_len) {
buff = Pkt->datagram + ptr;
return PTR_SUCCESS;
}
else {
fprintf(stderr, "PTR IS MESSED UP :: PACKET IS BROKE\n");
return PTR_ERR;
}
}
return PTR_SUCCESS;
}
bool rr_cmp(rr_t a, rr_t b) {
if (a.type != b.type)
return 0;
if (a.class != b.class)
return 0;
if (strncmp((char *)a.name, (char *)b.name, DNS_NAME_MAX_LEN))
return 0;
if (memcmp(a.rdata, b.rdata, a.rdlength <= b.rdlength ? a.rdlength: b.rdlength))
return 0;
return 1;
}