-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdump.c
More file actions
180 lines (161 loc) · 4.47 KB
/
dump.c
File metadata and controls
180 lines (161 loc) · 4.47 KB
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
/* (c) 2008, Quest Software Inc. All rights reserved. */
/*
* Exercise the DNS decoder by reading a single payload on stdin
* and attempting to dump the output.
*
* The input can either be raw binary (use the -r flag), or it can
* be the output of hexdump that running dnsupdate with -vvv flags will
* display.
*
* The -v flag passed to this program is only useful for debugging
* the hex input decoder.
*/
#include "common.h"
#include "dns.h"
#include "dnstcp.h"
#include "dnsdebug.h"
#include "err.h"
int verbose;
static int
read_raw(FILE *input, char *inbuf, size_t bufsz)
{
int inlen = 0;
int inread;
while (!feof(input) && !ferror(input)) {
inread = fread(inbuf + inlen, 1, bufsz - inlen, input);
inlen += inread;
}
return inlen;
}
static int
read_hex(FILE *input, char *inbuf, size_t bufsz)
{
int inlen = 0;
int inread;
enum { sOFFSET, sBYTE1, sBYTE2, sGAP1, sGAP2, sEXTRA } state = sOFFSET;
unsigned int value;
// 0200: 00 01 00 00 0e 10 00 0b 08 61 64 30 62 7a 32 63 ........ .ad0bz2c
# define ishex(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f'))
# define hexvalue(c) ((c) - ((c) >= 'a' ? 'a' - 10 : '0'))
value = 0;
state = sOFFSET;
while (!feof(input) && !ferror(input)) {
int ch = getc(input);
if (ch == -1)
break;
if (ishex(ch))
value = (value << 4) | hexvalue(ch);
if (verbose > 3)
printf("inlen=%d state=%s ch=0x%02x/%c %s/%02x value=0x%04x\n",
inlen,
(state == sOFFSET ? "sOFFSET" :
state == sBYTE1 ? "sBYTE1" :
state == sBYTE2 ? "sBYTE2" :
state == sGAP1 ? "sGAP1" :
state == sGAP2 ? "sGAP2" :
state == sEXTRA ? "sEXTRA" : "???"),
ch, ch,
ishex(ch) ? "hex" : "-",
ishex(ch) ? hexvalue(ch) : 0,
value);
switch (state) {
case sOFFSET:
if (ch == ' ' || ishex(ch))
break;
if (ch != ':')
errx(1, "expected colon, space or hex");
if (value != inlen)
errx(1, "offset mismatch %x != %x", value, inlen);
state = sGAP1;
break;
case sGAP1:
if (ch != ' ')
errx(1, "expected space");
if ((inlen % 16) == 8)
state = sGAP2;
else {
state = sBYTE1;
value = 0;
}
break;
case sGAP2:
if (ch != ' ')
errx(1, "expected space");
state = sBYTE1;
break;
case sBYTE1:
if (ch == ' ')
state = sEXTRA;
else if (!ishex(ch))
errx(1, "expected space or hex");
else
state = sBYTE2;
break;
case sBYTE2:
if (!ishex(ch))
errx(1, "expected 2nd digit for byte");
if (inlen >= bufsz)
errx(1, "input too long: %d", bufsz);
inbuf[inlen++] = value;
if (inlen % 16 == 0)
state = sEXTRA;
else
state = sGAP1;
break;
case sEXTRA:
if (ch == '\r' || ch == '\n') {
value = 0;
state = sOFFSET;
}
break;
}
}
return inlen;
}
int
main(int argc, char **argv)
{
char inbuf[65535];
int inlen;
int ch;
int error = 0;
FILE *input = stdin;
char *input_name = "<stdin>";
struct dns_msg *msg;
int is_raw = 0;
while ((ch = getopt(argc, argv, "rv")) != -1)
switch (ch) {
case 'v':
verbose++;
break;
case 'r':
is_raw=1;
break;
default:
error = 1;
}
if (optind < argc && !error) {
input_name = argv[optind++];
input = fopen(input_name, "rb");
if (!input)
err(1, "%s", input_name);
}
if (optind != argc)
error = 1;
if (error) {
fprintf(stderr, "usage: %s [-rv] [file]\n", argv[0]);
exit(1);
}
if (is_raw)
inlen = read_raw(input, inbuf, sizeof inbuf);
else
inlen = read_hex(input, inbuf, sizeof inbuf);
if (ferror(input))
err(1, "%s", input_name);
dumphex(inbuf, inlen);
msg = dns_msg_new();
dns_msg_setbuf(msg, inbuf, inlen);
dumpmsg(msg);
dns_msg_free(msg);
exit(0);
}