-
Notifications
You must be signed in to change notification settings - Fork 158
/
dns_query.c
341 lines (269 loc) · 9.55 KB
/
dns_query.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
/*
* MIT License
*
* Copyright (c) 2018 Lewis Van Winkle
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "chap05.h"
const unsigned char *print_name(const unsigned char *msg,
const unsigned char *p, const unsigned char *end) {
if (p + 2 > end) {
fprintf(stderr, "End of message.\n"); exit(1);}
if ((*p & 0xC0) == 0xC0) {
const int k = ((*p & 0x3F) << 8) + p[1];
p += 2;
printf(" (pointer %d) ", k);
print_name(msg, msg+k, end);
return p;
} else {
const int len = *p++;
if (p + len + 1 > end) {
fprintf(stderr, "End of message.\n"); exit(1);}
printf("%.*s", len, p);
p += len;
if (*p) {
printf(".");
return print_name(msg, p, end);
} else {
return p+1;
}
}
}
void print_dns_message(const char *message, int msg_length) {
if (msg_length < 12) {
fprintf(stderr, "Message is too short to be valid.\n");
exit(1);
}
const unsigned char *msg = (const unsigned char *)message;
/*
int i;
for (i = 0; i < msg_length; ++i) {
unsigned char r = msg[i];
printf("%02d: %02X %03d '%c'\n", i, r, r, r);
}
printf("\n");
*/
printf("ID = %0X %0X\n", msg[0], msg[1]);
const int qr = (msg[2] & 0x80) >> 7;
printf("QR = %d %s\n", qr, qr ? "response" : "query");
const int opcode = (msg[2] & 0x78) >> 3;
printf("OPCODE = %d ", opcode);
switch(opcode) {
case 0: printf("standard\n"); break;
case 1: printf("reverse\n"); break;
case 2: printf("status\n"); break;
default: printf("?\n"); break;
}
const int aa = (msg[2] & 0x04) >> 2;
printf("AA = %d %s\n", aa, aa ? "authoritative" : "");
const int tc = (msg[2] & 0x02) >> 1;
printf("TC = %d %s\n", tc, tc ? "message truncated" : "");
const int rd = (msg[2] & 0x01);
printf("RD = %d %s\n", rd, rd ? "recursion desired" : "");
if (qr) {
const int rcode = msg[3] & 0x0F;
printf("RCODE = %d ", rcode);
switch(rcode) {
case 0: printf("success\n"); break;
case 1: printf("format error\n"); break;
case 2: printf("server failure\n"); break;
case 3: printf("name error\n"); break;
case 4: printf("not implemented\n"); break;
case 5: printf("refused\n"); break;
default: printf("?\n"); break;
}
if (rcode != 0) return;
}
const int qdcount = (msg[4] << 8) + msg[5];
const int ancount = (msg[6] << 8) + msg[7];
const int nscount = (msg[8] << 8) + msg[9];
const int arcount = (msg[10] << 8) + msg[11];
printf("QDCOUNT = %d\n", qdcount);
printf("ANCOUNT = %d\n", ancount);
printf("NSCOUNT = %d\n", nscount);
printf("ARCOUNT = %d\n", arcount);
const unsigned char *p = msg + 12;
const unsigned char *end = msg + msg_length;
if (qdcount) {
int i;
for (i = 0; i < qdcount; ++i) {
if (p >= end) {
fprintf(stderr, "End of message.\n"); exit(1);}
printf("Query %2d\n", i + 1);
printf(" name: ");
p = print_name(msg, p, end); printf("\n");
if (p + 4 > end) {
fprintf(stderr, "End of message.\n"); exit(1);}
const int type = (p[0] << 8) + p[1];
printf(" type: %d\n", type);
p += 2;
const int qclass = (p[0] << 8) + p[1];
printf(" class: %d\n", qclass);
p += 2;
}
}
if (ancount || nscount || arcount) {
int i;
for (i = 0; i < ancount + nscount + arcount; ++i) {
if (p >= end) {
fprintf(stderr, "End of message.\n"); exit(1);}
printf("Answer %2d\n", i + 1);
printf(" name: ");
p = print_name(msg, p, end); printf("\n");
if (p + 10 > end) {
fprintf(stderr, "End of message.\n"); exit(1);}
const int type = (p[0] << 8) + p[1];
printf(" type: %d\n", type);
p += 2;
const int qclass = (p[0] << 8) + p[1];
printf(" class: %d\n", qclass);
p += 2;
const unsigned int ttl = (p[0] << 24) + (p[1] << 16) +
(p[2] << 8) + p[3];
printf(" ttl: %u\n", ttl);
p += 4;
const int rdlen = (p[0] << 8) + p[1];
printf(" rdlen: %d\n", rdlen);
p += 2;
if (p + rdlen > end) {
fprintf(stderr, "End of message.\n"); exit(1);}
if (rdlen == 4 && type == 1) {
/* A Record */
printf("Address ");
printf("%d.%d.%d.%d\n", p[0], p[1], p[2], p[3]);
} else if (rdlen == 16 && type == 28) {
/* AAAA Record */
printf("Address ");
int j;
for (j = 0; j < rdlen; j+=2) {
printf("%02x%02x", p[j], p[j+1]);
if (j + 2 < rdlen) printf(":");
}
printf("\n");
} else if (type == 15 && rdlen > 3) {
/* MX Record */
const int preference = (p[0] << 8) + p[1];
printf(" pref: %d\n", preference);
printf("MX: ");
print_name(msg, p+2, end); printf("\n");
} else if (type == 16) {
/* TXT Record */
printf("TXT: '%.*s'\n", rdlen-1, p+1);
} else if (type == 5) {
/* CNAME Record */
printf("CNAME: ");
print_name(msg, p, end); printf("\n");
}
p += rdlen;
}
}
if (p != end) {
printf("There is some unread data left over.\n");
}
printf("\n");
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage:\n\tdns_query hostname type\n");
printf("Example:\n\tdns_query example.com aaaa\n");
exit(0);
}
if (strlen(argv[1]) > 253) {
fprintf(stderr, "Hostname too long.");
exit(1);
}
unsigned char type;
if (strcmp(argv[2], "a") == 0) {
type = 1;
} else if (strcmp(argv[2], "mx") == 0) {
type = 15;
} else if (strcmp(argv[2], "txt") == 0) {
type = 16;
} else if (strcmp(argv[2], "aaaa") == 0) {
type = 28;
} else if (strcmp(argv[2], "any") == 0) {
type = 255;
} else {
fprintf(stderr, "Unknown type '%s'. Use a, aaaa, txt, mx, or any.",
argv[2]);
exit(1);
}
#if defined(_WIN32)
WSADATA d;
if (WSAStartup(MAKEWORD(2, 2), &d)) {
fprintf(stderr, "Failed to initialize.\n");
return 1;
}
#endif
printf("Configuring remote address...\n");
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
struct addrinfo *peer_address;
if (getaddrinfo("8.8.8.8", "53", &hints, &peer_address)) {
fprintf(stderr, "getaddrinfo() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
printf("Creating socket...\n");
SOCKET socket_peer;
socket_peer = socket(peer_address->ai_family,
peer_address->ai_socktype, peer_address->ai_protocol);
if (!ISVALIDSOCKET(socket_peer)) {
fprintf(stderr, "socket() failed. (%d)\n", GETSOCKETERRNO());
return 1;
}
char query[1024] = {0xAB, 0xCD, /* ID */
0x01, 0x00, /* Set recursion */
0x00, 0x01, /* QDCOUNT */
0x00, 0x00, /* ANCOUNT */
0x00, 0x00, /* NSCOUNT */
0x00, 0x00 /* ARCOUNT */};
char *p = query + 12;
char *h = argv[1];
while(*h) {
char *len = p;
p++;
if (h != argv[1]) ++h;
while(*h && *h != '.') *p++ = *h++;
*len = p - len - 1;
}
*p++ = 0;
*p++ = 0x00; *p++ = type; /* QTYPE */
*p++ = 0x00; *p++ = 0x01; /* QCLASS */
const int query_size = p - query;
int bytes_sent = sendto(socket_peer,
query, query_size,
0,
peer_address->ai_addr, peer_address->ai_addrlen);
printf("Sent %d bytes.\n", bytes_sent);
print_dns_message(query, query_size);
char read[1024];
int bytes_received = recvfrom(socket_peer,
read, 1024, 0, 0, 0);
printf("Received %d bytes.\n", bytes_received);
print_dns_message(read, bytes_received);
printf("\n");
freeaddrinfo(peer_address);
CLOSESOCKET(socket_peer);
#if defined(_WIN32)
WSACleanup();
#endif
return 0;
}