forked from guidoreina/DnsMessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DnsMessage.java
399 lines (329 loc) · 10.7 KB
/
DnsMessage.java
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
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Vector;
import java.net.InetAddress;
import java.net.UnknownHostException;
class DnsMessage {
// Maximum length of a DNS message.
private final static int MAX_LEN = 512;
// Length of the DNS header.
private final static int HEADER_LEN = 12;
// Maximum number of DNS pointers.
private final static int MAX_POINTERS = 64;
// Maximum length of a domain name.
private final static int DOMAIN_NAME_MAX_LEN = 255;
// Buffer containing the DNS message.
private byte[] buffer = null;
// Length of the DNS message.
private int length = 0;
// Offset in the buffer.
private int offset = 0;
// Domain.
public String domain = null;
// IP addresses.
public Vector<String> ipAddresses = new Vector<String>();
// Constructor.
DnsMessage() {
}
// Read file.
public byte[] read(String filename) {
File file = new File(filename);
// If the file exists...
if (file.isFile()) {
// Get file size.
long filesize = file.length();
// If the file is not too big...
if (filesize <= MAX_LEN) {
try {
// Open file for reading.
BufferedInputStream
reader = new BufferedInputStream(new FileInputStream(filename));
// Create buffer.
byte[] buf = new byte[(int) filesize];
// Read DNS message.
int read = reader.read(buf, 0, (int) filesize);
// If all the bytes could be read...
if (read == filesize) {
return buf;
} else {
System.err.printf("Error reading from '%s'.\n", filename);
}
} catch (IOException e) {
System.err.printf("Error reading from '%s'.\n", filename);
}
} else {
System.err.printf("File '%s' is too big (%d bytes).\n",
filename,
filesize);
}
} else {
System.err.printf("File '%s' doesn't exist.\n", filename);
}
return null;
}
private int ntohs(byte[] buf, int off) {
return ((buf[off] & 0xff) << 8) | (buf[off + 1] & 0xff);
}
private void arrayCopy(byte[] src,
int srcPos,
byte[] dest,
int destPos,
int length) {
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
public void clear() {
domain = null;
ipAddresses.clear();
}
public boolean parse(byte[] buf) {
clear();
// Save reference to the buffer.
buffer = buf;
// Save message length.
length = buffer.length;
// If the DNS message should be processed...
if ((length >= HEADER_LEN) && // The message is not too short.
(length <= MAX_LEN) && // The message is not too long.
(((buffer[2] >> 3) & 0x0f) <= 2) && // 0 <= OPCODE <= 2
((buffer[2] & 0x02) == 0) && // The message was not truncated.
((buffer[3] & 0x0f) == 0)) { // RCODE = 0
int qdcount = ntohs(buffer, 4);
// If there are questions...
if (qdcount > 0) {
offset = HEADER_LEN;
// If the QNAME is valid and the QTYPE and QCLASS fit...
if (((domain = parse_domain_name()) != null) &&
(offset + 4 <= length)) {
// If the QCLASS is 1 (IN [Internet])...
if (ntohs(buffer, offset + 2) == 1) {
// Get QTYPE.
int qtype = ntohs(buffer, offset);
if (qtype <= 255) {
// Query?
if ((buffer[2] & 0x80) == 0) {
return true;
} else {
int ancount = ntohs(buffer, 6);
// If there are answers...
if (ancount > 0) {
// Skip QTYPE and QCLASS.
offset += 4;
// Skip following questions (if any).
for (int i = 2; i <= qdcount; i++) {
if (!skip_question()) {
return false;
}
}
// Process answers.
for (int i = 1; i <= ancount; i++) {
if ((skip_domain_name()) && (offset + 10 <= length)) {
int rdlength = ntohs(buffer, offset + 8);
int next;
if ((next = offset + 10 + rdlength) <= length) {
// If the CLASS is 1 (IN [Internet])...
if (ntohs(buffer, offset + 2) == 1) {
// Check type.
switch (ntohs(buffer, offset)) {
case 1: // A (host address [IPv4]).
if (rdlength == 4) {
byte[] b = new byte[4];
arrayCopy(buffer, offset + 10, b, 0, 4);
try {
InetAddress
addr = InetAddress.getByAddress(b);
ipAddresses.add(addr.getHostAddress());
} catch (UnknownHostException e) {
}
} else {
return false;
}
break;
case 28: // AAAA (IPv6).
if (rdlength == 16) {
byte[] b = new byte[16];
arrayCopy(buffer, offset + 10, b, 0, 16);
try {
InetAddress
addr = InetAddress.getByAddress(b);
ipAddresses.add(addr.getHostAddress());
} catch (UnknownHostException e) {
}
} else {
return false;
}
break;
}
}
offset = next;
} else {
return false;
}
} else {
return false;
}
}
return !ipAddresses.isEmpty();
}
}
}
}
}
}
}
return false;
}
private String parse_domain_name()
{
byte[] domain = new byte[DOMAIN_NAME_MAX_LEN];
int len = 0;
int npointers = 0;
// Work with a copy of the offset.
int off = offset;
while (off < length) {
switch (buffer[off] & 0xc0) {
case 0: // Label.
// If not the null label...
if (buffer[off] > 0) {
int next;
if (((next = off + 1 + buffer[off]) < length) &&
(len + 1 + buffer[off] <= DOMAIN_NAME_MAX_LEN)) {
// If not the first label...
if (len > 0) {
domain[len++] = '.';
}
// Copy label.
arrayCopy(buffer, off + 1, domain, len, buffer[off]);
len += buffer[off];
off = next;
} else {
return null;
}
} else {
// Null label.
// If not the root domain name...
if (len > 0) {
if (npointers == 0) {
offset = off + 1;
}
return new String(domain, 0, len);
} else {
return null;
}
}
break;
case 0xc0: // Pointer.
if ((++npointers <= MAX_POINTERS) && (off + 1 < length)) {
// Compute pointer offset.
int ptroff = ntohs(buffer, off) & 0x3fff;
// Valid offset?
if ((ptroff >= HEADER_LEN) && (ptroff < length)) {
// First pointer?
if (npointers == 1) {
offset = off + 2;
}
off = ptroff;
} else {
return null;
}
} else {
return null;
}
break;
default:
return null;
}
}
return null;
}
private boolean skip_domain_name()
{
int len = 0;
int npointers = 0;
// Work with a copy of the offset.
int off = offset;
while (off < length) {
switch (buffer[off] & 0xc0) {
case 0: // Label.
// If not the null label...
if (buffer[off] > 0) {
int next;
if (((next = off + 1 + buffer[off]) < length) &&
(len + 1 + buffer[off] <= DOMAIN_NAME_MAX_LEN)) {
// If not the first label...
if (len > 0) {
len += (1 + buffer[off]);
} else {
len += buffer[off];
}
off = next;
} else {
return false;
}
} else {
// Null label.
if (npointers == 0) {
offset = off + 1;
}
return true;
}
break;
case 0xc0: // Pointer.
if ((++npointers <= MAX_POINTERS) && (off + 1 < length)) {
// Compute pointer offset.
int ptroff = ntohs(buffer, off) & 0x3fff;
// Valid offset?
if ((ptroff >= HEADER_LEN) && (ptroff < length)) {
// First pointer?
if (npointers == 1) {
offset = off + 2;
}
off = ptroff;
} else {
return false;
}
} else {
return false;
}
break;
default:
return false;
}
}
return false;
}
private boolean skip_question()
{
if ((skip_domain_name()) && (offset + 4 <= buffer.length)) {
offset += 4;
return true;
} else {
return false;
}
}
public static void main(String[] args) {
if (args.length == 1) {
DnsMessage dnsmsg = new DnsMessage();
// Read file.
byte[] buf = dnsmsg.read(args[0]);
// If the file could be read...
if (buf != null) {
// Parse DNS message.
if (dnsmsg.parse(buf)) {
System.out.printf("%s:\n", dnsmsg.domain);
// For each IP address...
for (String ipAddress : dnsmsg.ipAddresses) {
System.out.printf(" %s\n", ipAddress);
}
} else {
System.err.println("Error parsing DNS message.");
}
}
} else {
System.err.println("Usage: DnsMessage <filename>");
}
}
}