-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnsdebug.c
391 lines (345 loc) · 9.89 KB
/
dnsdebug.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
/* (c) 2006, Quest Software Inc. All rights reserved */
/* David Leonard, 2006 */
#include "common.h"
#include "dns.h"
#include "dnsdebug.h"
extern int verbose;
/*
* Debug/dump routines.
* These functions double as testing and inspection; they
* implement their own packet parsers, so to speak.
*/
/* A desc table is an array of 'struct desc' terminated with a NULL desc */
struct desc {
uint16_t value;
const char *desc;
};
static const char *desc_lookup(const struct desc *desc, uint16_t value);
static void dumprr_part(const struct dns_rr *rr);
static void dumpheader(const struct dns_header *hdr);
static void dumpquestion(const struct dns_rr *question, const char *section);
static struct desc opcode_desc[] = {
{ DNS_OP_QUERY, "query" },
{ DNS_OP_IQUERY, "iquery" },
{ DNS_OP_STATUS, "status" },
{ DNS_OP_UPDATE, "update" },
{ 0, 0 }
};
static struct desc rrtype_desc[] = {
{ DNS_TYPE_A, "A" },
{ DNS_TYPE_NS, "NS" },
{ 3, "MD" },
{ 4, "MF" },
{ 5, "CNAME" },
{ DNS_TYPE_SOA, "SOA" },
{ 7, "MB" },
{ 8, "MG" },
{ 9, "MR" },
{ DNS_TYPE_NULL, "NULL" },
{ 11, "WKS" },
{ DNS_TYPE_PTR, "PTR" },
{ 13, "HINFO" },
{ 14, "MINFO" },
{ 15, "MX" },
{ DNS_TYPE_TXT, "TXT" },
{ DNS_TYPE_AAAA, "AAAA" },
{ DNS_TYPE_TKEY, "TKEY" },
{ DNS_TYPE_TSIG, "TSIG" },
{ 252, "AXFR" },
{ 253, "MAILB" },
{ 254, "MAILA" },
{ DNS_TYPE_ANY, "*" },
{ 0, 0 }
};
static struct desc rrclass_desc[] = {
{ DNS_CLASS_IN, "IN" },
{ 2, "CSNET" },
{ 3, "CHAOS" },
{ 4, "HESIOD" },
{ DNS_CLASS_NONE, "NONE" },
{ DNS_CLASS_ANY, "ANY" },
{ 0, 0 }
};
/* Returns a description. Note returns shared static storage sometimes */
static const char *
desc_lookup(const struct desc *desc, uint16_t value)
{
const struct desc *d;
static char descbuf[30];
if (!desc) return 0;
for (d = desc; d->desc; d++)
if (d->value == value)
return d->desc;
snprintf(descbuf, sizeof descbuf, "<0x%x>", value);
return descbuf;
}
const char *
dns_type_str(uint16_t type)
{
return desc_lookup(rrtype_desc, type);
}
void
dumphex(const void *buf, size_t len)
{
const unsigned char *data = (const unsigned char *)buf;
size_t i, j;
for (i = 0; i < len; i += 16) {
fprintf(stderr, " %04x:", (int)i);
for (j = i; j < i + 16; j++) {
if (j < len)
fprintf(stderr, " %02x", data[j]);
else
fprintf(stderr, " ");
if (j == i + 7)
putc(' ', stderr);
}
fprintf(stderr, " ");
for (j = i; j < i + 16 && j < len; j++) {
if (data[j] < ' ' || data[j] >= 0x7f)
putc('.', stderr);
else
putc(data[j], stderr);
if (j == i + 7)
putc(' ', stderr);
}
putc('\n', stderr);
}
}
static void
dumpdata(struct dns_msg *msg, const char *name, uint16_t len)
{
unsigned char *p;
p = (unsigned char *)msg->data + dns_msg_getpos(msg);
dns_rd_skip(msg, len);
fprintf(stderr, "\t%-20s: (len=%d)\n", name, len);
dumphex(p, len);
}
static void
dumpname(struct dns_msg *msg, const char *name)
{
char buf[DNS_MAXNAME];
dns_rd_name(msg, buf, sizeof buf);
fprintf(stderr, "\t%-20s: %s\n", name, buf);
}
static void
dumpuint16(struct dns_msg *msg, const char *name)
{
uint16_t v;
v = dns_rd_uint16(msg);
fprintf(stderr, "\t%-20s: %u (0x%x)\n", name, v, v);
}
static void
dumpuint16desc(struct dns_msg *msg, const char *name, struct desc *desc)
{
uint16_t v;
v = dns_rd_uint16(msg);
fprintf(stderr, "\t%-20s: %s (%u)\n", name, desc_lookup(desc, v), v);
}
static void
dumpuint16rcode(struct dns_msg *msg, const char *name)
{
uint16_t v;
v = dns_rd_uint16(msg);
fprintf(stderr, "\t%-20s: %s\n", name, dns_rcode_name(v));
}
static void
dumpuint32(struct dns_msg *msg, const char *name)
{
uint32_t v;
v = dns_rd_uint32(msg);
fprintf(stderr, "\t%-20s: %u (0x%x)\n", name, v, v);
}
static void
dumpuint32time(struct dns_msg *msg, const char *name)
{
uint32_t v;
time_t t;
v = dns_rd_uint32(msg);
t = (time_t)v;
fprintf(stderr, "\t%-20s: %.24s (0x%x)\n", name, ctime(&t), v);
}
static void
dumpuint32rtime(struct dns_msg *msg, const char *name)
{
static struct {
const char *abbrev;
uint32_t interval;
} intervals[] = {
{ "d", 24 * 60 * 60 },
{ "h", 60 * 60 },
{ "m", 60 },
{ "s", 1 },
};
uint32_t v,t;
int i, printed_something = 0;
v = dns_rd_uint32(msg);
t = v;
fprintf(stderr, "\t%-20s:", name);
for (i = 0; i < sizeof intervals / sizeof intervals[0]; i++)
if (t >= intervals[i].interval || printed_something) {
int x = t / intervals[i].interval;
if (t) {
fprintf(stderr, " %d%s", x, intervals[i].abbrev);
printed_something = 1;
}
t = t - x * intervals[i].interval;
}
if (!printed_something)
fprintf(stderr, " 0");
fprintf(stderr, " (0x%x)\n", v);
}
static void
dumpheader(const struct dns_header *hdr)
{
fprintf(stderr, " header:\n");
fprintf(stderr, "\t%-20s: 0x%04x\n", "id", hdr->id);
fprintf(stderr, "\t%-20s: %s\n", "response", hdr->response ? "RESPONSE" : "QUERY");
fprintf(stderr, "\t%-20s: %s\n", "opcode", desc_lookup(opcode_desc, hdr->opcode));
fprintf(stderr, "\t%-20s: %s\n", "authoritative", hdr->authoritative ? "yes" : "no");
fprintf(stderr, "\t%-20s: %s\n", "truncated", hdr->truncated ? "yes" : "no");
fprintf(stderr, "\t%-20s: %s\n", "recurse-desired",
hdr->recurse_desired ? "yes" : "no");
fprintf(stderr, "\t%-20s: %s\n", "recurse-avail", hdr->recurse_avail ? "yes" : "no");
fprintf(stderr, "\t%-20s: %s (%u)\n", "rcode",
dns_rcode_name(hdr->rcode), hdr->rcode);
fprintf(stderr, "\t%-20s: %u\n", "qdcount", hdr->qdcount);
fprintf(stderr, "\t%-20s: %u\n", "ancount", hdr->ancount);
fprintf(stderr, "\t%-20s: %u\n", "nscount", hdr->nscount);
fprintf(stderr, "\t%-20s: %u\n", "arcount", hdr->arcount);
}
static void
dumprr_part(const struct dns_rr *rr)
{
fprintf(stderr, "\t%-20s: \"%s\"\n", "name", rr->name);
fprintf(stderr, "\t%-20s: %s (%u)\n", "type",
desc_lookup(rrtype_desc, rr->type), rr->type);
fprintf(stderr, "\t%-20s: %s (%u)\n", "class",
desc_lookup(rrclass_desc, rr->class_), rr->class_);
}
void
dumprr(const struct dns_rr *rr, const char *name)
{
fprintf(stderr, " %s:\n", name);
dumprr_part(rr);
fprintf(stderr, "\t%-20s: %ld sec\n", "ttl", (long)rr->ttl);
}
static void
dumpquestion(const struct dns_rr *question, const char *section)
{
fprintf(stderr, " %s:\n", section);
dumprr_part(question);
}
void
dumpmsg(struct dns_msg *msg)
{
char data[32768];
struct dns_rr rr;
struct dns_header header;
int i;
uint16_t len;
const char *section_names_query[] = {
"question", "answer", "authority", "additional" };
const char *section_names_update[] = {
"zone", "prerequisite", "update", "additional" };
const char **section_names;
const char *section_name;
size_t savepos;
void *bufsave;
size_t bufszsave;
fprintf(stderr, "-------\n");
assert(dns_msg_getpos(msg) == 0);
dns_msg_getbuf(msg, &bufsave, &bufszsave);
if (verbose > 2)
dumphex(bufsave, bufszsave);
dns_rd_header(msg, &header);
dumpheader(&header);
section_names = header.opcode == DNS_OP_UPDATE
? section_names_update
: section_names_query;
section_name = section_names[0];
for (i = 0; i < header.qdcount; i++) {
dns_rd_question(msg, &rr);
dumpquestion(&rr, section_name);
}
for (;;) {
if (header.ancount) {
section_name = section_names[1];
header.ancount--;
} else if (header.nscount) {
section_name = section_names[2];
header.nscount--;
} else if (header.arcount) {
section_name = section_names[3];
header.arcount--;
} else
break;
dns_rd_rr_head(msg, &rr);
dumprr(&rr, section_name);
/* Extract the rdata length without moving the read pointer */
savepos = dns_msg_getpos(msg);
dumpuint16(msg, "rdata.length");
dns_rd_setpos(msg, savepos);
if (rr.type == 250) {
uint16_t timehi;
uint32_t timelo;
time_t t;
dns_rd_begin(msg);
dumpname(msg, "tsig.algorithm");
timehi = dns_rd_uint16(msg);
timelo = dns_rd_uint32(msg);
/* Note: on 32-bit time_t systems, higher order bits are lost */
t = timehi << 32 | timelo;
fprintf(stderr, "\t%-20s: 0x%x:%08x (%.24s)\n", "tsig.time",
timehi, timelo, ctime(&t));
dumpuint16(msg, "tsig.fudge");
dumpdata(msg, "tsig.mac", dns_rd_uint16(msg));
dumpuint16(msg, "tsig.orig_id");
dumpuint16rcode(msg, "tsig.error");
dumpdata(msg, "tsig.other", dns_rd_uint16(msg));
dns_rd_end(msg);
} else if (rr.type == 249) {
static struct desc modetab[] = {
{1, "server assignment"},
{2, "Diffie-Hellman exchange"},
{3, "GSS-API negotation"},
{4, "resolver assignment"},
{5, "key deletion"},
{0, 0}
};
dns_rd_begin(msg);
dumpname(msg, "tkey.algorithm");
dumpuint32time(msg, "tkey.inception");
dumpuint32time(msg, "tkey.expiration");
dumpuint16desc(msg, "tkey.mode", modetab);
dumpuint16rcode(msg, "tkey.error");
dumpdata(msg, "tkey.key", dns_rd_uint16(msg));
dumpdata(msg, "tkey.other", dns_rd_uint16(msg));
dns_rd_end(msg);
} else if (rr.type == 6) {
dns_rd_begin(msg);
dumpname(msg, "soa.mname");
dumpname(msg, "soa.rname");
dumpuint32(msg, "soa.serial");
dumpuint32rtime(msg, "soa.refresh");
dumpuint32rtime(msg, "soa.retry");
dumpuint32rtime(msg, "soa.expire");
dumpuint32(msg, "soa.minttl");
dns_rd_end(msg);
} else if (rr.type == 2) {
dns_rd_begin(msg);
dumpname(msg, "ns.nsdname");
dns_rd_end(msg);
} else {
len = dns_rd_data(msg, data, sizeof data);
if (len)
dumphex(data, len);
}
}
len = msg->remain[msg->depth];
if (len != 0) {
fprintf(stderr, " %s:\n", "(LEFTOVER)");
dumphex((char *)msg->data + msg->pos, len);
}
dns_msg_setbuf(msg, bufsave, bufszsave);
fflush(stdout);
}