-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.c
555 lines (473 loc) · 14.1 KB
/
frame.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* File: frame.c
* Author: Ethan Gordon
* Owns the header of all MARP packets.
**/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#include <time.h>
/* Local Files */
#include "frame.h"
#include "network/socket.h"
#define FRAME_MAX 512
#define LOCAL_VERSION 1
#define PEER_MAX 10
#define HEADER 9
extern char* programName;
/* Holds Frame header and payload. */
struct header {
uint32_t qid;
uint8_t version;
unsigned int qr:1;
unsigned int op:3;
unsigned int aa:1;
unsigned int rd:1;
unsigned int z:2;
uint8_t recurse;
uint16_t length;
};
struct frame {
struct header sHeader;
uint8_t* payload;
};
/**
* Frame_T Frame_init(void)
* Allocates a new Frame in the heap.
* @param: None
* @return: An empty Frame or NULL on error, with errno set.
**/
Frame_T Frame_init(void) {
Frame_T ret;
ret = calloc(1, sizeof(struct frame));
return ret;
} /* Frame_init() */
/**
* Frame_T Frame_buildQuery(int, int, const void*, size_t)
* @param authoritative: Only accept authoritative responses
* @param recurseDepth: How many nodes to recurse to, 0 to disable
* @param payload: buffer to send
* @param payLen: Length of @param payload
* @return a new Frame, or NULL on failure
**/
Frame_T Frame_buildQuery(int authoritative, int recurseDepth, const void* payload, size_t payLen) {
Frame_T ret = Frame_init();
if (ret == NULL) return NULL;
ret->payload = calloc(payLen, sizeof(uint8_t));
if (ret->payload == NULL) {
free(ret); return NULL;
}
ret->sHeader.length = payLen;
memcpy(ret->payload, payload, ret->sHeader.length);
ret->sHeader.version = 1;
if (authoritative) ret->sHeader.aa = 1;
ret->sHeader.recurse = recurseDepth;
if (recurseDepth) ret->sHeader.rd = 1;
ret->sHeader.qr = 1;
ret->sHeader.op = 0; /* Standard Query */
ret->sHeader.version = 1;
/* Randomly Generate QID */
srand(time(NULL));
ret->sHeader.qid = rand();
return ret;
}
/**
* const uint8_t Frame_getPayload(Frame_T, uint16_t*)
* @param frame: To get the payload, can't be NULL
* @param len: Value Parameter, overwritten with length of payload
* @return pointer to the payload buffer
**/
const uint8_t* Frame_getPayload(Frame_T frame, uint16_t* len) {
assert(frame != NULL);
*len = frame->sHeader.length;
return frame->payload;
}
/**
* int Frame_listen(Frame_T, Socket_T)
* Block until a new frame is received via the socket over the network (or timeout is reached).
* @param dest: Frame to fill, all contents will be overwritten
* @param socket: Where to listen for data. Must not be NULL.
* @param timeout: How long to wait, set to 0 to disable.
* @return: bytes written on Success, negative on Failure or Timeout
**/
int Frame_listen(Frame_T dest, Socket_T socket, int timeout) {
int error;
uint8_t *buf, *payload;
assert(dest != NULL);
assert(socket != NULL);
if (timeout < 0) return timeout;
if (dest->payload) free(dest->payload);
buf = calloc(FRAME_MAX, sizeof(uint8_t));
if (buf == NULL) {
return -1;
}
error = Socket_read(socket, (void*)buf, FRAME_MAX, timeout);
if (error < 0) {
free(buf);
return error;
} else if (error < HEADER) {
fprintf(stderr, "%s: Received too small frame from socket.", programName);
}
dest->payload = calloc(error - HEADER, sizeof(uint8_t));
if (dest->payload == NULL) {
free(buf);
return -1;
}
/* Fill Frame */
memcpy(&(dest->sHeader), buf, HEADER);
payload = buf + HEADER;
memcpy(dest->payload, payload, error - HEADER);
free(buf);
return error;
} /* End Frame_listen() */
static void* Frame_thread(void* arg);
struct threadarg {
Socket_T socket;
Frame_T frame;
};
/**
* int Frame_respond(Frame_T, Socket_T)
* Creates a new pthread in which to respond to the given Frame.
* Note: The provided frame is automatically de-allocated.
* @param frame: Must be an alread-filled frame.
* @param socket: Must be an already connected or bound socket.
* @return: A pointer to the pthread, or NULL on failure.
**/
pthread_t* Frame_respond(Frame_T frame, Socket_T socket) {
pthread_t *thread;
int error;
struct threadarg *arg;
/* Sanity Check */
if (frame == NULL) return NULL;
if (socket == NULL) return NULL;
if (frame->payload == NULL) return NULL;
/* Version Check */
if (frame->sHeader.version != LOCAL_VERSION) {
fprintf(stderr, "%s: Version %d not supported by this server!\n", programName, frame->sHeader.version);
return NULL;
}
/* Allocate and Start Thread */
arg = calloc(1, sizeof(struct threadarg));
if (arg == NULL) return NULL;
arg->socket = socket;
arg->frame = frame;
thread = calloc(1, sizeof(pthread_t));
if (thread == NULL) { free(arg); return NULL; }
error = pthread_create(thread, NULL, Frame_thread, arg);
if (error < 0) { free(thread); free(arg); return NULL; }
return thread;
} /* End Frame_respond() */
/**
* int Frame_send(Frame_T, Socket_T, const char* uint16_t)
* @param frame: To serialize and Send
* @param socket: use to send
* @param ip, port: Send to
* @return: @see Socket_write(), same return value
**/
int Frame_send(Frame_T frame, Socket_T socket, const char* ip, uint16_t port) {
uint8_t* buf;
assert(frame != NULL);
assert(socket != NULL);
buf = calloc(HEADER + frame->sHeader.length, sizeof(char));
if (buf == NULL) return -1;
memcpy(buf, &(frame->sHeader), HEADER);
memcpy(buf + HEADER, frame->payload, frame->sHeader.length);
return Socket_write(socket, ip, port, buf, HEADER + frame->sHeader.length);
}
/**
* void Frame_free(Frame_T)
* @param frame: Frame to completely clean up and de-allocate.
* @return: None
**/
void Frame_free(Frame_T frame) {
if (frame == NULL) return;
if (frame->payload) free(frame->payload);
free(frame);
} /* End Frame_free() */
/******************************************************
**************** The Meat of the Program *************
******************************************************/
/* Operation Codes */
enum {
kSTD,
kREV,
kPER,
kMAL,
kNTF,
kPNG
};
/**
* void Frame_responseSTD(Frame_T, Frame_T)
* @param frame: A standard query to parse
* @param response: filled with the standard response
* @return None
**/
static void Frame_responseSTD(Frame_T frame, Frame_T response) {
Query_T query;
Response_T resp;
const uint16_t* protocols;
uint16_t* protocolCopy;
const uint16_t* proto;
bool found = false;
uint8_t respHead[SHA256_SIZE + sizeof(uint16_t)];
int error, count;
count = error = 0;
/* Parse Query */
query = Query_init(frame->payload, frame->sHeader.length);
if (query == NULL) {
response->sHeader.op = kMAL;
return;
}
protocols = Query_protocols(query);
/* Create Response */
memset(respHead, 0x00, SHA256_SIZE + sizeof(uint16_t));
memcpy(respHead, Query_id(query), SHA256_SIZE);
resp = Response_init(respHead, SHA256_SIZE + sizeof(uint16_t));
if (resp == NULL) {
response->sHeader.op = kMAL;
Query_free(query);
return;
}
/* First, Check Local Database for Results */
for (proto = protocols; *proto != 0; proto++) {
size_t len;
const char* encrypted;
encrypted = Local_get((char*)respHead, *proto, &len);
if (encrypted != NULL) {
error = Response_buildRecord(resp, *proto, encrypted, (uint16_t)len, Local_getTTL((char*)respHead, *proto));
if (error < 0) {
Response_free(resp);
Query_free(query);
response->sHeader.op = kNTF;
return;
}
found = true;
}
count++;
}
if (found) {
/* Found in Local Database! Sign and Return */
response->sHeader.length = Response_size(resp);
/*error = Response_sign(resp, Local_getPrivkey()); */
response->payload = calloc(response->sHeader.length, sizeof(uint8_t));
if (response->payload == NULL || error < 0) {
if (response->payload) free(response->payload);
response->payload = NULL;
response->sHeader.length = 0;
response->sHeader.op = kNTF;
} else {
response->sHeader.aa = 1;
Response_serialize(resp, response->payload);
}
Response_free(resp);
Query_free(query);
return;
}
/* Only do cache check if client is okay with non-authoritative responses. */
if (!response->sHeader.aa) {
/* Make a Defensive Copy of Protocol List */
protocolCopy = calloc(count, sizeof(uint16_t));
memcpy(protocolCopy, protocols, count * sizeof(uint16_t));
/* Check Cache */
for (proto = protocolCopy; *proto != 0; proto++) {
size_t len;
const void* record = NULL;
record = Cache_get((char*)respHead, *proto, &len);
if (record != NULL) {
Response_addRecord(resp, *proto, record);
Query_rmProtocol(query, *proto);
}
}
/* If we covered all the protocols, return! */
protocols = Query_protocols(query);
if (*protocols == 0) {
response->sHeader.length = Response_size(resp);
response->payload = calloc(response->sHeader.length, sizeof(uint8_t));
if (response->payload == NULL || error < 0) {
if (response->payload) free(response->payload);
response->payload = NULL;
response->sHeader.length = 0;
response->sHeader.op = kNTF;
} else {
Response_serialize(resp, response->payload);
}
Response_free(resp);
Query_free(query);
return;
}
}
/* If not in the Local File or the Cache, recurse the request if requested */
if (frame->sHeader.rd && frame->sHeader.recurse) {
uint8_t* recBuf;
size_t newRespLen;
Recursor_T recursor;
frame->sHeader.recurse--;
frame->sHeader.length = Query_size(query);
/* Serialize and Recurse */
recBuf = calloc(sizeof(struct header) + frame->sHeader.length, sizeof(uint8_t));
if (recBuf == NULL) {
Response_free(resp);
Query_free(query);
response->sHeader.op = kNTF;
return;
}
memcpy(recBuf, &(frame->sHeader), sizeof(struct header));
Query_serialize(query, recBuf + sizeof(struct header));
recursor = Recursor_init(recBuf, sizeof(struct header) + frame->sHeader.length, PEER_MAX, frame->sHeader.recurse + 1);
free(recBuf);
if (recursor == NULL) {
Response_free(resp);
Query_free(query);
response->sHeader.op = kNTF;
return;
}
while((recBuf = (uint8_t*)Recursor_poll(recursor, &newRespLen)) != NULL) {
Frame_T f;
Response_T src;
f = calloc(1, sizeof(struct frame));
if (f == NULL) continue;
memcpy(recBuf, &(f->sHeader), sizeof(struct header));
f->payload = recBuf + sizeof(struct header);
if (f->sHeader.op != kSTD || f->sHeader.z || f->sHeader.qid != frame->sHeader.qid) {
free(f); continue;
}
src = Response_init(f->payload, f->sHeader.length);
if (src == NULL) {
free(f); continue;
}
Response_merge(resp, src);
}
}
/* If the Record Count is 0, Return NTF */
if (Response_recordCount(resp) == 0) {
Response_free(resp);
Query_free(query);
response->sHeader.op = kNTF;
return;
}
/* Serialize Response, Cleanup, and Return */
response->sHeader.length = Response_size(resp);
response->payload = calloc(response->sHeader.length, sizeof(uint8_t));
if (response->payload == NULL || error < 0) {
if (response->payload) free(response->payload);
response->payload = NULL;
response->sHeader.length = 0;
response->sHeader.op = kNTF;
} else {
Response_serialize(resp, response->payload);
}
Response_free(resp);
Query_free(query);
return;
}
/**
* void* Frame_thread(void*)
* @param arg: Socket and Frame to respond to
* @return a pointer to the integer return value. MUST BE FREED!
**/
static void* Frame_thread(void* arg) {
Socket_T socket;
Frame_T frame;
Frame_T response;
int *ret;
int bad = 0;
uint8_t* resBuf;
struct threadarg *args = arg;
/* Fill Socket and Frame */
assert(arg != NULL);
socket = args->socket;
frame = args->frame;
ret = (int*)args;
*ret = -1;
/* Make Response Frame */
response = malloc(sizeof(Frame_T));
if (response == NULL) return ret;
*response = *frame;
response->sHeader.qr = 0; /* Response */
response->sHeader.length = 0;
response->payload = NULL;
/* Validate Frame Header */
if (frame->sHeader.z) bad = 1;
if (!frame->sHeader.qr) bad = 1;
if (bad) {
response->sHeader.op = kMAL;
*ret = Socket_respond(socket, &response->sHeader, HEADER);
free(response);
Frame_free(frame);
Frame_free(response);
return ret;
}
/* Op Code Mux */
switch (frame->sHeader.op) {
case kSTD: /* Standard Query */
Frame_responseSTD(frame, response);
break;
case kREV: /* TODO: Currently Unsupported */
response->sHeader.op = kNTF;
break;
case kPER:
break;
case kPNG:
break;
default:
response->sHeader.op = kMAL;
}
/* Serialize and Send Response */
resBuf = calloc(HEADER + response->sHeader.length, sizeof(uint8_t));
if (resBuf == NULL) {
response->sHeader.op = kNTF;
*ret = Socket_respond(socket, &(response->sHeader), HEADER);
} else {
memcpy(resBuf, &(response->sHeader), HEADER);
memcpy(resBuf + HEADER, response->payload, response->sHeader.length);
*ret = Socket_respond(socket, resBuf, sizeof(struct header) + response->sHeader.length);
}
Frame_free(frame);
Frame_free(response);
free(resBuf);
return ret;
} /* End Frame_thread() */
/**
* void Frame_printInfo(Frame_T)
* @param frame: To print
* @return None
**/
void Frame_printInfo(Frame_T frame) {
putchar('\n');
puts("Frame Information:");
printf("Protocol Version %d\n", frame->sHeader.version);
printf("Frame ID: %d\n", frame->sHeader.qid);
if (frame->sHeader.qr) fputs("Query Type: ", stdout);
else fputs("Response Type: ", stdout);
switch(frame->sHeader.op) {
case kSTD:
puts("Standard");
break;
case kREV:
puts("Reverse");
break;
case kNTF:
puts("Not Found");
break;
case kMAL:
puts("Server Error");
break;
case kPER:
puts("Peer List");
break;
case kPNG:
puts("Ping");
break;
default:
puts("Unknown");
}
if (frame->sHeader.aa) puts("Authoritative: Yes");
else puts("Authoritative: No");
if (frame->sHeader.rd)
printf("Recursion requested to depth %d.", frame->sHeader.recurse);
putchar('\n');
}