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
|
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "log.h"
#include "map.h"
#define MAX_LEN 1024
#define BUF(name) char name[MAX_LEN]
static int line = 0;
static bool get_line(FILE* file, const BUF(buf)) {
line++;
return fgets((char*) buf, MAX_LEN, file) != NULL;
}
static bool is_whitespace(const char* buf) {
int i = 0;
char c;
while(c = buf[i], 1) {
if (c == '\n' || c == '\0') return true;
if (c != ' ' && c != '\n') return false;
i++;
}
}
static bool get_words(char* buf, char** words, int count) {
int last = 0;
int offset = 0;
int i = 0;
for(i = 0; i < count; i++) {
char c;
while(c = buf[offset], c != ' ' && c != '\0' && c != '\n') {
offset++;
}
if (offset - last < 1) {
return false;
}
words[i] = buf + last;
buf[offset] = '\0';
offset++;
last = offset;
if (c == '\0' || c == '\n') {
break;
}
}
return i + 1 == count;
}
static bool get_int(const char* word, uint32_t* i) {
char* end;
uint32_t res = (uint32_t) strtol(word, &end, 10);
if (*end == '\0') {
*i = res;
return true;
} else {
return false;
}
}
static bool config_read_qtype(const char* qstr, RecordType* qtype) {
if (strcmp(qstr, "A") == 0) {
*qtype = A;
return true;
} else if (strcmp(qstr, "NS") == 0) {
*qtype = NS;
return true;
} else if (strcmp(qstr, "CNAME") == 0) {
*qtype = CNAME;
return true;
} else if (strcmp(qstr, "SOA") == 0) {
*qtype = SOA;
return true;
} else if (strcmp(qstr, "PTR") == 0) {
*qtype = PTR;
return true;
} else if (strcmp(qstr, "MX") == 0) {
*qtype = MX;
return true;
} else if (strcmp(qstr, "TXT") == 0) {
*qtype = TXT;
return true;
} else if (strcmp(qstr, "AAAA") == 0) {
*qtype = AAAA;
return true;
} else if (strcmp(qstr, "SRV") == 0) {
*qtype = SRV;
return true;
} else if (strcmp(qstr, "CAA") == 0) {
*qtype = CAA;
return true;
} else {
return false;
}
}
static bool config_read_class(const char* cstr, uint16_t* class) {
if (strcmp(cstr, "IN") == 0) {
*class = 1;
return true;
} else if (strcmp(cstr, "CH") == 0) {
*class = 3;
return true;
} else if (strcmp(cstr, "HS") == 0) {
*class = 4;
return true;
} else {
return false;
}
}
// Format QTYPE CLASS DOMAIN: A IN google.com
static bool config_read_question(FILE* file, Question* question) {
BUF(buf);
if (!get_line(file, buf)) {
return false;
}
if (is_whitespace(buf)) {
return false;
}
char* words[3];
if (!get_words(&buf[0], &words[0], 3)) {
WARN("Invalid question at line %d", line);
return false;
};
uint16_t class;
if (!config_read_class(words[0], &class)) {
WARN("Invalid question class at line %d", line);
return false;
}
RecordType qtype;
if (!config_read_qtype(words[1], &qtype)) {
WARN("Invalid question qtype at line %d", line);
return false;
}
size_t domain_len = strlen(words[2]);
question->cls = class;
question->qtype = qtype;
question->domain = malloc(domain_len + 1);
question->domain[0] = domain_len;
memcpy(question->domain + 1, words[2], domain_len);
return true;
}
static void copy_str(char* from, uint8_t** to) {
size_t len = strlen(from);
if (len > 255) {
len = 255;
}
uint8_t* new = malloc(len + 1);
new[0] = len;
memcpy(new + 1, from, len);
*to = new;
}
static bool config_read_a_record(char* data, ARecord* record) {
sscanf(data, "%hhu.%hhu.%hhu.%hhu",
&record->addr[0],
&record->addr[1],
&record->addr[2],
&record->addr[3]
);
return true;
}
static bool config_read_ns_record(char* data, NSRecord* record) {
copy_str(data, &record->host);
return true;
}
static bool config_read_cname_record(char* data, CNAMERecord* record) {
copy_str(data, &record->host);
return true;
}
static bool config_read_soa_record(char* data, SOARecord* record) {
char* words[7];
if (!get_words(&data[0], &words[0], 7)) {
WARN("Invalid SOA record data at line %d", line);
record->mname = NULL;
record->nname = NULL;
return false;
}
copy_str(words[0], &record->mname);
copy_str(words[1], &record->nname);
if (!get_int(words[2], &record->serial)) {
WARN("Invalid SOA record data at line %d", line);
return false;
}
if (!get_int(words[3], &record->refresh)) {
WARN("Invalid SOA record data at line %d", line);
return false;
}
if (!get_int(words[4], &record->retry)) {
WARN("Invalid SOA record data at line %d", line);
return false;
}
if (!get_int(words[5], &record->expire)) {
WARN("Invalid SOA record data at line %d", line);
return false;
}
if (!get_int(words[6], &record->minimum)) {
WARN("Invalid SOA record data at line %d", line);
return false;
}
return true;
}
static bool config_read_ptr_record(char* data, PTRRecord* record) {
copy_str(data, &record->pointer);
return true;
}
static bool config_read_mx_record(char* data, MXRecord* record) {
char* words[2];
if (!get_words(&data[0], &words[0], 2)) {
WARN("Invalid MX record data at line %d", line);
record->host = NULL;
return false;
}
copy_str(words[1], &record->host);
uint32_t priority;
if (!get_int(words[0], &priority)) {
WARN("Invalid MX record data at line %d", line);
return false;
}
record->priority = (uint16_t) priority;
return true;
}
static bool config_read_txt_record(char* data, TXTRecord* record) {
int len = strlen(data);
uint8_t count = ((uint8_t)len + 254) / 255;
record->len = count;
record->text = malloc(sizeof(uint8_t*) * count);
for (uint8_t i = 0; i < count; i++) {
uint32_t offset = count * 255;
uint32_t part_len = len - offset;
if (part_len > 255) part_len = 255;
uint8_t* part = malloc(part_len + 1);
part[0] = part_len;
memcpy(part + 1, data + offset, part_len);
record->text[i] = part;
}
return true;
}
static bool config_read_aaaa_record(char* data, AAAARecord* record) {
for(int i = 0; i < 8; i++) {
if (sscanf(data, "%02hhx%02hhx:",
&record->addr[i*2 + 0],
&record->addr[i*2 + 1]
) == EOF) {
return false;
}
}
return true;
}
static bool config_read_srv_record(char* data, SRVRecord* record) {
char* words[4];
if (!get_words(&data[0], &words[0], 4)) {
WARN("Invalid SRV record data at line %d", line);
record->target = NULL;
return false;
}
copy_str(words[3], &record->target);
uint32_t priority;
if (!get_int(words[0], &priority)) {
WARN("Invalid SRV record data at line %d", line);
return false;
}
record->priority = (uint16_t) priority;
uint32_t weight;
if (!get_int(words[1], &weight)) {
WARN("Invalid SRV record data at line %d", line);
return false;
}
record->weight = (uint16_t) weight;
uint32_t port;
if (!get_int(words[2], &port)) {
WARN("Invalid SRV record data at line %d", line);
return false;
}
record->port = (uint16_t) port;
return true;
}
static bool config_read_caa_record(char* data, CAARecord* record) {
char* words[4];
if (!get_words(&data[0], &words[0], 4)) {
WARN("Invalid SRV record data at line %d", line);
record->tag = NULL;
record->value = NULL;
return false;
}
copy_str(words[2], &record->tag);
copy_str(words[3], &record->value);
uint32_t flags;
if (!get_int(words[0], &flags)) {
WARN("Invalid SRV record data at line %d", line);
return false;
}
record->flags = (uint8_t) flags;
uint32_t length;
if (!get_int(words[1], &length)) {
WARN("Invalid SRV record data at line %d", line);
return false;
}
record->length = (uint8_t) length;
return true;
}
static bool config_read_cmd_record(char* data, CMDRecord* record) {
record->command = data;
return true;
}
static bool config_read_record_data(char* data, Record* record) {
switch (record->type) {
case UNKOWN:
// This can never happend in here so uh do nothing i guess
return false;
case A:
return config_read_a_record(data, &record->data.a);
case NS:
return config_read_ns_record(data, &record->data.ns);
case CNAME:
return config_read_cname_record(data, &record->data.cname);
case SOA:
return config_read_soa_record(data, &record->data.soa);
case PTR:
return config_read_ptr_record(data, &record->data.ptr);
case MX:
return config_read_mx_record(data, &record->data.mx);
case TXT:
return config_read_txt_record(data, &record->data.txt);
case AAAA:
return config_read_aaaa_record(data, &record->data.aaaa);
case SRV:
return config_read_srv_record(data, &record->data.srv);
case CAA:
return config_read_caa_record(data, &record->data.caa);
case CMD:
return config_read_cmd_record(data, &record->data.cmd);
}
return false;
}
static bool config_read_record(FILE* file, Record* record, Question* question) {
BUF(buf);
if (!get_line(file, buf)) {
return false;
}
if (is_whitespace(buf)) {
return false;
}
char* words[2];
if (!get_words(&buf[0], &words[0], 2)) {
WARN("Invalid record at line %d", line);
return false;
}
uint32_t ttl;
if (!get_int(words[0], &ttl)) {
WARN("Invalid record ttl at line %d", line);
return false;
}
record->cls = question->cls;
record->type = question->qtype;
record->len = 0;
record->ttl = ttl;
record->domain = malloc(question->domain[0] + 1);
memcpy(record->domain, question->domain, question->domain[0] + 1);
if(!config_read_record_data(words[1], record)) {
free_record(record);
return false;
}
return true;
}
static void config_push_record(Record** buf, Record record, uint16_t* capacity, uint16_t* size) {
if (size == capacity) {
*capacity *= 2;
*buf = realloc(*buf, sizeof(Record) * *capacity);
}
(*buf)[*size] = record;
(*size)++;
}
bool load_config(const char* path, RecordMap* map) {
FILE* file = fopen(path, "r");
if (file == NULL) {
ERROR("Failed to open file %s: %s", path, strerror(errno));
return false;
}
line = 0;
record_map_init(map);
while (1) {
Question* question = malloc(sizeof(Question));
if (!config_read_question(file, question)) {
free(question);
break;
}
INIT_LOG_BUFFER(log);
LOGONLY(print_question(question, log));
TRACE("Found config question: %s", log);
Packet* packet = malloc(sizeof(Packet));
memset(packet, 0, sizeof(Packet));
packet->authorities = NULL;
packet->resources = NULL;
packet->questions = malloc(sizeof(Question));
packet->questions[0] = *question;
uint16_t capacity = 1;
packet->answers = malloc(sizeof(Record));
while(1) {
Record record;
if (!config_read_record(file, &record, question)) {
break;
}
LOGONLY(print_record(&record, log));
TRACE("Found config record: %s", log);
config_push_record(&packet->answers, record, &capacity, &packet->header.answers);
}
record_map_add(map, question, packet);
}
fclose(file);
return true;
}
|