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
|
#include "dns.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
/* Entry point is dns_get. */
/* Read about DNS in RFC 1034 & 1035. */
struct header {
unsigned short id;
char a;
/*
int is_response: 1;
int opcode: 4;
int is_authanswer: 1;
int is_truncated: 1;
int is_recursion_desired: 1;
*/
char b;
/*
int is_recursion_available: 1;
int: 3;
int response_code: 4;
*/
unsigned short questions;
unsigned short answers;
unsigned short authorities;
unsigned short additionals;
};
struct question_header {
unsigned short type;
unsigned short class;
};
struct resource_header {
unsigned short type;
unsigned short class;
unsigned int ttl;
unsigned short datalen;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* get-related
*/
static void fill_header(char **writer)
{
struct header *head = (struct header *) *writer;
memset(head, 0, sizeof(*head));
/* PID is unique enough to use it as ID: */
head->id = (unsigned short) htons(getpid());
head->a = 0b00000001; /* recursion desired */
head->questions = htons(1); /* we have one question */
*writer += sizeof(*head);
}
static void fill_name(char **writer, const char *host)
{ /* In DNS names are always starting with dot,
* so 'example.com' need to become '.example.com'. */
char *last = *writer;
do {
if (*host == '.' || !*host) {
*last = *writer - last;
last = ++*writer;
} else {
*++*writer = *host;
}
} while (*host++);
*last = 0;
++*writer;
}
static void fill_question(char **writer, int query_type)
{
struct question_header *q = (struct question_header *) *writer;
q->type = htons(query_type);
q->class = htons(1);
*writer += sizeof(*q);
}
static int askudp(const char *s, const char *h, int qt, struct sockaddr_in *a)
{ /* Create socket, fill in the question and send it. */
int sent;
int plen = sizeof(struct header) + strlen(h) + 2 +
sizeof(struct question_header);
char *packet = malloc(plen);
char *writer = packet;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
return 0;
}
a->sin_family = AF_INET;
a->sin_port = htons(53);
a->sin_addr.s_addr = inet_addr(s);
/* Socket created, now fill in the question. */
fill_header(&writer);
fill_name(&writer, h);
fill_question(&writer, qt);
/* Question is filled in, so send it. */
sent = sendto(sock, packet, plen, 0, (struct sockaddr *) a, sizeof(*a));
free(packet);
if (sent != plen) {
return 0;
}
return sock;
}
static int asktcp(const char *s, const char *h, int qt)
{ /* Create socket, fill in the question and send it. */
struct sockaddr_in a;
int sent;
int plen = sizeof(struct header) + strlen(h) + 4 +
sizeof(struct question_header);
char *packet = malloc(plen);
char *writer = packet;
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
return 0;
}
a.sin_family = AF_INET;
a.sin_port = htons(53);
a.sin_addr.s_addr = inet_addr(s);
if (connect(sock, (struct sockaddr *) &a, sizeof(a))) {
return 0;
}
/* Socket created, now fill in the question.
Note the difference from UDP: TCP has size in the start. */
writer += 2;
fill_header(&writer);
fill_name(&writer, h);
fill_question(&writer, qt);
*((unsigned short *) packet) = htons(writer - packet - 2);
/* Question is filled in, so send it. */
sent = write(sock, packet, plen);
free(packet);
if (sent != plen) {
return 0;
}
return sock;
}
static char *gettcp(int sock)
{
short size;
char *packet;
if (!sock) {
return 0;
}
if (read(sock, &size, 2) < 0) {
close(sock);
return 0;
}
size = ntohs(size);
packet = malloc(size);
if (read(sock, packet, size) < 0) {
close(sock);
free(packet);
return 0;
}
close(sock);
return packet;
}
static char *get(const char *s, const char *h, int qt)
{ /* Get response in UDP, if it is truncated do it in TCP. */
struct sockaddr_in a;
char *packet;
socklen_t slen = sizeof(a);
int sock = askudp(s, h, qt, &a);
if (!sock) {
return 0;
}
packet = malloc(512); /* Max size of UDP is 512 bytes. */
if (recvfrom(sock, packet, 512, 0, (struct sockaddr *) &a, &slen) < 0) {
free(packet);
close(sock);
return 0;
} else if (packet[2] & 0b10) { /* is truncated */
free(packet);
close(sock);
return gettcp(asktcp(s, h, qt));
}
close(sock);
return packet;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* parse-related
*/
static void skip_questions(char **reader, int num)
{
for (; num; --num) {
*reader += strlen(*reader) + 1;
*reader += sizeof(struct question_header);
}
}
static char *read_name(char **extreader, char *packet)
{ /* Names are stored in binary format:
* length followed by name part (string between dots),
* length of zero means end of domain name,
* and length >= 192 means it will be followed by absolute link,
* name readed from link should be inserted instead of this part. */
char *name = malloc(256); /* cannot be more =) */
char *reader = *extreader;
char *writer = name;
int jumped = 0;
if (!*reader) {
*name = 0;
return name;
}
while (*reader) {
unsigned char len = *reader++;
if (len >= 192) {
int jump = ((unsigned char) *reader) + (len - 192) * 256;
if (!jumped) {
*extreader += 2;
jumped = 1;
}
reader = packet + jump;
continue;
}
memcpy(writer, reader, len);
writer += len;
reader += len;
if (!jumped) {
*extreader += len + 1;
}
*writer++ = '.';
}
if (!jumped) {
++*extreader;
}
*--writer = 0;
return name;
}
static struct dns_answers *get_answers(char **reader, char *packet, int num)
{ /* Read all (num) answers into res and return it. */
struct dns_answers *res;
struct resource_header *head;
if (!num) { /* no answers left to read */
return 0;
}
/* Read one answer, starting with header: */
res = malloc(sizeof(*res));
res->host = read_name(reader, packet);
head = (struct resource_header *) *reader;
res->type = ntohs(head->type);
res->size = ntohs(head->datalen);
*reader += 10;
/* Analyse answer data based on answer type: */
if (res->type == dns_type_cname) {
/* For CNAME data is just domain name: */
res->size--;
res->data = read_name(reader, packet);
} else if (res->type == dns_type_mx) {
/* For MX data is record preference, followed by its name: */
char *name;
unsigned short pref = ntohs(*((unsigned short *) *reader));
*reader += 2;
name = read_name(reader, packet);
res->data = malloc(2 + strlen(name) + 1);
*((unsigned short *) res->data) = pref;
memcpy(res->data + 2, name, strlen(name) + 1);
free(name);
} else if (res->type == dns_type_srv) {
/* For SRV data is record priority, weight and port
* followed by its name. */
char *name;
unsigned short priority;
unsigned short weight;
unsigned short port;
priority = ntohs(*((unsigned short *) *reader));
*reader += 2;
weight = ntohs(*((unsigned short *) *reader));
*reader += 2;
port = ntohs(*((unsigned short *) *reader));
*reader += 2;
name = read_name(reader, packet);
res->data = malloc(7 + strlen(name));
((unsigned short *) res->data)[0] = priority;
((unsigned short *) res->data)[1] = weight;
((unsigned short *) res->data)[2] = port;
memcpy(res->data + 6, name, strlen(name) + 1);
free(name);
} else {
/* For other types just copy the data: */
if (res->type == dns_type_txt) {
/* Skipping leading length byte for TXT: */
res->size--;
++*reader;
}
res->data = malloc(res->size);
memcpy(res->data, *reader, res->size);
*reader += res->size;
}
/* Start recursion for the rest of answers and add them as next: */
res->next = get_answers(reader, packet, num - 1);
return res;
}
static struct dns_answers *parse(char *packet)
{
struct dns_answers *res;
char *reader = packet;
struct header *head = (struct header *) reader;
if (!packet) {
return 0;
}
head->questions = ntohs(head->questions);
head->answers = ntohs(head->answers);
reader += sizeof(*head);
skip_questions(&reader, head->questions);
res = get_answers(&reader, packet, head->answers);
free(packet);
return res;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MX utility
*/
char *dns_mx_server(void *data)
{
return ((char *) data) + 2;
}
int dns_mx_preference(void *data)
{
return *((unsigned short *) data);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SRV utility
*/
char *dns_srv_server(void *data)
{
return ((char *) data) + 6;
}
int dns_srv_port(void *data)
{
return ((unsigned short *) data)[2];
}
int dns_srv_priority(void *data)
{
return *((unsigned short *) data);
}
int dns_srv_weight(void *data)
{
return ((unsigned short *) data)[1];
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor and destructor
*/
struct dns_answers *dns_get(const char *server, const char *host, int query)
{ /* Acquire response in get and parse it. */
return parse(get(server, host, query));
}
void dns_free(struct dns_answers *answers)
{
if (answers) {
dns_free(answers->next);
free(answers->host);
free(answers->data);
free(answers);
}
}
|