-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.c
607 lines (492 loc) · 14.7 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#define _XOPEN_SOURCE 700
#include <stdint.h>
#include <sys/socket.h>
#include <err.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <locale.h>
#define HDR_QR (1<<0)
#define HDR_SET_OPCODE(x) (((x)&0xf)<<1)
#define HDR_GET_OPCODE(x) (((x)>>1)&0xf)
#define HDR_AA (1<<6)
#define HDR_TC (1<<7)
#define HDR_RD (1<<8)
#define HDR_RA (1<<9)
#define HDR_SET_RCODE(x) (((x)&0xf)<<11)
#define HDR_GET_RCODE(x) (((x)>>11)&0xf)
struct dns_header {
union {
struct {
uint16_t ident;
uint16_t flags;
uint16_t num_questions;
uint16_t num_answers;
uint16_t num_rrs;
uint16_t num_add_rrs;
} __attribute__((packed));
uint16_t words[6];
} __attribute__((packed));
} __attribute__((packed));
struct dns_question {
char *name;
uint16_t type;
uint16_t class;
};
#define TYPE_A 1
#define TYPE_NS 2
#define TYPE_CNAME 5
#define TYPE_SOA 6
#define TYPE_PTR 12
#define TYPE_MX 15
#define TYPE_TXT 16
#define TYPE_AAAA 28
#define TYPE_SRV 33
#define QTYPE_IXFR 251
#define QTYPE_AXFR 252
#define QTYPE_ALL 255
#define CLASS_IN 1
#define CLASS_NONE 254
#define CLASS_ANY 255
static const char *qclasses[] = {
[CLASS_IN] = "IN",
[CLASS_NONE] = "NONE",
[CLASS_ANY] = "ANY",
[0xffff] = NULL,
};
#define OPCODE_QUERY 0
#define OPCODE_IQUERY 1
#define OPCODE_STATUS 2
#define OPCODE_NOTIFY 4
#define OPCODE_UPDATE 5
#define RCODE_NOERROR 0
#define RCODE_FORMERR 1
#define RCODE_SERVFAIL 2
#define RCODE_NXDOMAIN 3
#define RCODE_NOTIMP 4
#define RCODE_REFUSED 5
#define RCODE_YXDOMAIN 6
#define RCODE_YXRRSET 7
#define RCODE_NXRRSET 8
#define RCODE_NOTAUTH 9
#define RCODE_NOTZONE 10
static const char *qtypes[] = {
[TYPE_A] = "A",
[TYPE_NS] = "NS",
[TYPE_CNAME] = "CNAME",
[TYPE_SOA] = "SOA",
[TYPE_PTR] = "PTR",
[TYPE_MX] = "MX",
[TYPE_TXT] = "TXT",
[TYPE_AAAA] = "AAAA",
[TYPE_SRV] = "SRV",
[QTYPE_IXFR] = "IXFR",
[QTYPE_AXFR] = "AXFR",
[QTYPE_ALL] = "ALL",
[0xffff] = NULL
};
static const char *opcodes[] = {
[OPCODE_QUERY] = "QUERY",
[OPCODE_IQUERY] = "IQUERY",
[OPCODE_STATUS] = "STATUS",
[OPCODE_NOTIFY] = "NOTIFY",
[OPCODE_UPDATE] = "UPDATE",
[0xf] = NULL
};
static const char *rcodes[] = {
[RCODE_NOERROR] = "No error condition",
[RCODE_FORMERR] = "Formet error",
[RCODE_SERVFAIL] = "Server failure",
[RCODE_NXDOMAIN] = "Name error",
[RCODE_NOTIMP] = "Not implemented",
[RCODE_REFUSED] = "Refused",
[RCODE_YXDOMAIN] = "Domain exists",
[RCODE_YXRRSET] = "RR exists",
[RCODE_NXRRSET] = "RR missing",
[RCODE_NOTAUTH] = "Not authoritative",
[RCODE_NOTZONE] = "Not within zone",
[0xf] = NULL
};
struct dns_rr {
bool allocated;
unsigned char *name;
union {
struct {
uint16_t type;
uint16_t class;
uint32_t ttl;
uint16_t rdlength;
} __attribute__((packed));
} __attribute__((packed)) vals;
void *additional;
};
__attribute__((nonnull))
static void dump_qname(const unsigned char *qname)
{
printf("qname=");
const unsigned char *tmp = qname;
if (*tmp == 0xc0) {
printf("offset=0x%02x", tmp[1]);
return;
} else if (*tmp & 0xc0) {
printf("UNSUPPORTED");
return;
}
while (*tmp)
{
if (isprint(*tmp))
printf("%c ", *tmp);
else
printf("\\%03o ", *tmp);
tmp++;
}
}
/* take a hostname:
* www.google.com.
* and convert to (q)name format:
* \003 w w w \006 g o o g l e \003 c o m \000
*/
__attribute__((nonnull(1)))
static unsigned char *encode_qname(const char *name, int *len_out)
{
char *src_cpy = NULL;
unsigned char *ret = NULL;
if ((src_cpy = strdup(name)) == NULL)
return NULL;
char *ptr = src_cpy + strlen(src_cpy) - 1;
/* remove any trailing dot(s) */
while (*ptr && *ptr == '.')
*ptr-- = '\0';
if (!*ptr || !strlen(ptr))
goto fail;
ptr = src_cpy;
/* check the name is valid */
while (*ptr)
{
if (!isalnum(*ptr) && *ptr != '.')
goto fail;
ptr++;
}
/* +1 - terminating null length root label
* +1 - to cover the last label, that has no .
*/
int name_len = strlen(src_cpy) + 2;
if ((ret = calloc(1, name_len)) == NULL)
goto fail;
int offset = 0;
const char *tmp = src_cpy;
const char *old_tmp = src_cpy;
bool running = true;
/* pack into (q)name format:
* (LEN{1} [-A-Z0-9]*)* \000
*/
while(running)
{
int len;
tmp = strchr(old_tmp, '.');
if (tmp == NULL) {
tmp = old_tmp;
running = false;
len = src_cpy + name_len - 2 - tmp;
} else
len = tmp - old_tmp;
if (len <= 0 || len > 63) {
errno = ENAMETOOLONG;
goto fail;
}
/* top two bits are used for something other than len */
ret[offset++] = (uint8_t)(len & 0x3f);
memcpy(&ret[offset], old_tmp, len);
offset += len;
old_tmp = ++tmp;
}
free(src_cpy);
if (len_out)
*len_out = name_len;
return ret;
fail:
if (src_cpy)
free(src_cpy);
if (ret)
free(ret);
return NULL;
}
__attribute__((nonnull))
static void hexdump(const char *buf, int len)
{
for(int i = 0; i < len; i++)
printf("%02x ", (unsigned char)buf[i]);
}
__attribute__((nonnull))
static void free_dns_rr(struct dns_rr *blk, bool free_it)
{
if (blk->name)
free(blk->name);
if (blk->additional)
free(blk->additional);
if (free_it)
free(blk);
}
__attribute__((nonnull))
static void free_dns_rr_block(struct dns_rr *blk)
{
int i = 0;
while(blk[i].allocated)
free_dns_rr(&blk[i++], false);
free(blk);
return;
}
/* TODO make return struct dns_question like process_rr_block */
__attribute__((nonnull))
static void process_question_block(const char **const inbuf_ptr, int num_qs)
{
for (int count = 0; count < num_qs; count++)
{
const char *name_start = *inbuf_ptr;
while(**inbuf_ptr)
{
if (**inbuf_ptr & 0xc0) {
(*inbuf_ptr) += 2;
goto comp_skip;
}
(*inbuf_ptr) += **inbuf_ptr + 1;
}
(*inbuf_ptr)++;
comp_skip:
unsigned char *tmp_qname;
int len = (*inbuf_ptr) - name_start;
if ((tmp_qname = calloc(1, len)) == NULL)
err(EXIT_FAILURE, "calloc");
memcpy(tmp_qname, name_start, len);
uint16_t qtype = ntohs(*((uint16_t *)*inbuf_ptr)); (*inbuf_ptr) += 2;
uint16_t qclass = ntohs(*((uint16_t *)*inbuf_ptr)); (*inbuf_ptr) += 2;
printf("process_qblck: type=0x%02x[%4s] class=0x%02x[%3s] ",
qtype,
qtypes[qtype] ? qtypes[qtype] : "",
qclass,
qclasses[qclass] ? qclasses[qclass] : "");
dump_qname(tmp_qname);
/* FIXME */
free(tmp_qname);
printf("\n");
}
}
__attribute__((nonnull))
static struct dns_rr *process_rr_block(const char **const inbuf_ptr, int num_rrs)
{
struct dns_rr *ret = NULL;
if ((ret = calloc(num_rrs + 1, sizeof(struct dns_rr))) == NULL)
goto outer_fail;
for (int count = 0; count < num_rrs; count++)
{
/* TODO does this zero each time? */
struct dns_rr tmp_rr = {0};
tmp_rr.allocated = true;
const char *name_start = *inbuf_ptr;
/* consume each label */
while(**inbuf_ptr)
{
if (**inbuf_ptr & 0xc0) {
(*inbuf_ptr) += 2;
goto comp_skip;
}
/* skip over the label */
(*inbuf_ptr) += **inbuf_ptr + 1;
}
/* skip over root */
(*inbuf_ptr)++;
comp_skip:
int name_len = (*inbuf_ptr) - name_start;
if ((tmp_rr.name = calloc(1, name_len)) == NULL)
goto fail;
/* copy the full (q)name */
memcpy(tmp_rr.name, name_start, name_len);
/* parse the rest of the fields */
tmp_rr.vals.type = ntohs(*((uint16_t *)*inbuf_ptr)); (*inbuf_ptr) += 2;
tmp_rr.vals.class = ntohs(*((uint16_t *)*inbuf_ptr)); (*inbuf_ptr) += 2;
tmp_rr.vals.ttl = ntohs(*((uint32_t *)*inbuf_ptr)); (*inbuf_ptr) += 4;
tmp_rr.vals.rdlength = ntohs(*((uint16_t *)*inbuf_ptr)); (*inbuf_ptr) += 2;
/* allocate and read the additional block, if any */
if (tmp_rr.vals.rdlength) {
if ((tmp_rr.additional = calloc(1, tmp_rr.vals.rdlength)) == NULL)
goto fail;
memcpy(tmp_rr.additional, *inbuf_ptr, tmp_rr.vals.rdlength);
/* skip over it */
(*inbuf_ptr) += tmp_rr.vals.rdlength;
}
printf("process_block: type=0x%02x[%4s] class=0x%02x[%3s] ttl=0x%02d rdlength=0x%03x ",
tmp_rr.vals.type,
qtypes[tmp_rr.vals.type] ? qtypes[tmp_rr.vals.type] : "",
tmp_rr.vals.class,
qclasses[tmp_rr.vals.class] ? qclasses[tmp_rr.vals.class] : "",
tmp_rr.vals.ttl,
tmp_rr.vals.rdlength
);
dump_qname(tmp_rr.name);
if (tmp_rr.vals.rdlength && tmp_rr.additional) {
printf(" rd=");
hexdump(tmp_rr.additional, tmp_rr.vals.rdlength);
}
printf("\n");
/* add to the array */
memcpy(&ret[count], &tmp_rr, sizeof(tmp_rr));
continue;
fail:
if (tmp_rr.name) {
free(tmp_rr.name);
tmp_rr.name = NULL;
}
if (tmp_rr.additional) {
free(tmp_rr.additional);
tmp_rr.additional = NULL;
}
goto outer_fail;
}
return ret;
outer_fail:
if (ret)
free_dns_rr_block(ret);
return NULL;
}
__attribute__((nonnull))
static char *build_request(const char *name, int *pack_len)
{
char *buf = NULL;
struct dns_header hdr = {0};
unsigned char *qname = NULL;
int name_len;
hdr.ident = htons(random());
hdr.flags |= HDR_RD; /* Recursion Desired */
hdr.flags = htons(hdr.flags);
hdr.num_questions = htons(1);
if ((qname = encode_qname(name, &name_len)) == NULL)
goto fail;
const struct dns_rr rr = {
.name = qname,
.vals.type = htons(TYPE_A),
.vals.class = htons(CLASS_IN),
.vals.ttl = htonl(0),
.vals.rdlength = htons(0),
.allocated = false,
.additional = NULL,
};
*pack_len = sizeof(hdr) + sizeof(rr.vals) + name_len;
if ((buf = malloc(*pack_len)) == NULL)
goto fail;
memcpy(buf, &hdr, sizeof(hdr));
memcpy(buf + sizeof(hdr), rr.name, name_len);
memcpy(buf + sizeof(hdr) + name_len, &rr.vals, sizeof(rr.vals));
free(qname);
return buf;
fail:
if (buf)
free(buf);
if (qname)
free(qname);
return NULL;
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "POSIX");
if (argc != 2)
errx(EXIT_FAILURE, "Usage: %s NAME", argv[0]);
int sock_fd;
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
err(EXIT_FAILURE, "socket");
const struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_port = htons(53),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
};
if (connect(sock_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
err(EXIT_FAILURE, "connect");
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec);
srandom(random());
srandom(random());
ssize_t ret;
char *buf;
int pack_len;
if ((buf = build_request(argv[1], &pack_len)) == NULL)
err(EXIT_FAILURE, "build_request");
if ((ret = write(sock_fd, buf, pack_len)) == -1)
err(EXIT_FAILURE, "write");
else if (ret == 0)
errx(EXIT_FAILURE, "write: 0");
else if (ret != pack_len)
errx(EXIT_FAILURE, "write: short");
free(buf);
char inbuf[1500];
if ((ret = read(sock_fd, &inbuf, 1500)) == -1)
err(EXIT_FAILURE, "read");
else if (ret == 0)
errx(EXIT_FAILURE, "read: 0");
else if (ret < (int)sizeof(struct dns_header))
errx(EXIT_FAILURE, "read: short");
struct dns_header hdr;
memcpy(&hdr, inbuf, sizeof(hdr));
int count;
for (count = 0; count < 6; count++)
hdr.words[count] = ntohs(hdr.words[count]);
printf("read: %lu\n", ret);
printf("*** Header:\nident=%#x flags=%#x (%s%s%s%s%sOPCODE=%#x[%s] RCODE=%#x[%s]) ans=%d que=%d rrs=%d add_rrs=%d\n",
hdr.ident,
hdr.flags,
hdr.flags & HDR_QR ? "QR " : "",
hdr.flags & HDR_AA ? "AA " : "",
hdr.flags & HDR_TC ? "TC " : "",
hdr.flags & HDR_RD ? "RD " : "",
hdr.flags & HDR_RA ? "RA " : "",
HDR_GET_OPCODE(hdr.flags),
opcodes[HDR_GET_OPCODE(hdr.flags)],
HDR_GET_RCODE(hdr.flags),
rcodes[HDR_GET_RCODE(hdr.flags)],
hdr.num_answers,
hdr.num_questions,
hdr.num_rrs,
hdr.num_add_rrs);
const char *inbuf_end = inbuf + ret;
const char *inbuf_ptr = inbuf + sizeof(hdr);
void *tmp;
printf("*** Questions:\n");
if (hdr.num_questions) {
if (inbuf_ptr > inbuf_end)
errx(EXIT_FAILURE, "read: short buffer");
process_question_block(&inbuf_ptr, hdr.num_questions);
}
if (hdr.num_answers) {
printf("*** Answers:\n");
if (inbuf_ptr > inbuf_end)
errx(EXIT_FAILURE, "read: short buffer");
if ((tmp = process_rr_block(&inbuf_ptr, hdr.num_answers)) == NULL)
err(EXIT_FAILURE, "process_rr_block");
free_dns_rr_block(tmp);
}
if (hdr.num_rrs) {
printf("*** RRs:\n");
if (inbuf_ptr > inbuf_end)
errx(EXIT_FAILURE, "read: short buffer");
if ((tmp = process_rr_block(&inbuf_ptr, hdr.num_rrs)) == NULL)
err(EXIT_FAILURE, "process_rr_block");
free_dns_rr_block(tmp);
}
if (hdr.num_add_rrs) {
printf("*** Additional RRs:\n");
if (inbuf_ptr > inbuf_end)
errx(EXIT_FAILURE, "read: short buffer");
if ((tmp = process_rr_block(&inbuf_ptr, hdr.num_add_rrs)) == NULL)
err(EXIT_FAILURE, "process_rr_block");
free_dns_rr_block(tmp);
}
shutdown(sock_fd, SHUT_RDWR);
close(sock_fd);
exit(EXIT_SUCCESS);
}