-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.cc
485 lines (411 loc) · 14 KB
/
dns.cc
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <algorithm>
#include <csignal>
#include <fstream>
#include <iostream>
#include <unordered_map>
#define BUFFER_SIZE 512
#define T_A 1
// struct for arguments
typedef struct args {
char *server;
int port;
char *filter;
} Args;
// struct dns header
struct dns_header {
unsigned short id;
unsigned char rd : 1;
unsigned char tc : 1;
unsigned char aa : 1;
unsigned char opcode : 4;
unsigned char qr : 1;
unsigned char rcode : 4;
unsigned char cd : 1;
unsigned char ad : 1;
unsigned char z : 1;
unsigned char ra : 1;
unsigned short q_count;
unsigned short ans_count;
unsigned short auth_count;
unsigned short add_count;
};
struct question {
unsigned short qtype;
unsigned short qclass;
};
// class for filtered domain names
class TreeNode {
public:
TreeNode(const std::string &name) { addNode(name); };
void setFilterable(bool f) { filterable = f; }
void addNode(const std::string &domain) {
if (domain.empty()) {
filterable = true;
return;
}
std::size_t found = domain.find_last_of(".");
std::string split = domain.substr(found + 1, domain.size());
if (map.find(split) == map.end()) {
if (domain == split)
map.insert(std::make_pair(split, new TreeNode("")));
else
map.insert(std::make_pair(
split, new TreeNode(domain.substr(0, found))));
} else {
if (domain == split)
map.find(split)->second->addNode("");
else
map.find(split)->second->addNode(domain.substr(0, found));
}
}
// find domain name
static bool filterDomain(const std::string &domain, TreeNode *td) {
if (td->filterable) return true;
std::size_t found = domain.find_last_of(".");
std::string split = domain.substr(found + 1, domain.size());
if (td->map.find(split) != td->map.end())
return filterDomain(domain.substr(0, found), td->map.at(split));
return false;
}
// clear memory before end
static void clearMemory(TreeNode *mp) {
for (std::unordered_map<std::string, TreeNode *>::iterator it =
mp->map.begin();
it != mp->map.end(); it++) {
if (!it->second->map.empty()) clearMemory(it->second);
delete it->second;
}
}
private:
bool filterable = false;
std::unordered_map<std::string, TreeNode *> map;
};
// Global variables
Args *arguments;
TreeNode filteredDomains("");
// Prototypes
void parsePacket(int, struct sockaddr_in, unsigned char *, unsigned long,
TreeNode &);
void loadFile(TreeNode &tree);
int parseArguments(int, char **);
bool checkResolverName();
unsigned char *translateName(unsigned char *, unsigned char *);
void sendToResolverIpv4(unsigned char *, int, struct sockaddr_in, int);
void sendToResolverIpv6(unsigned char *, int, struct sockaddr_in, int);
void changeToDnsNameFormat(unsigned char *, unsigned char *);
void sigintHandler(int);
bool isIpv6();
int main(int argc, char **argv) {
signal(SIGINT, sigintHandler);
if (parseArguments(argc, argv) == 1) {
if (arguments != NULL) {
if (arguments->filter != NULL) free(arguments->filter);
if (arguments->server != NULL) free(arguments->server);
free(arguments);
}
exit(1);
}
if (!isIpv6()) {
if (!checkResolverName()) sigintHandler(1);
}
loadFile(filteredDomains);
int server_fd;
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
unsigned long recvMessageSize;
unsigned char buffer[BUFFER_SIZE];
if ((server_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
sigintHandler(1);
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(arguments->port);
if (bind(server_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) <
0) {
sigintHandler(1);
}
for (;;) {
unsigned int clientLen = sizeof(clientAddr);
recvMessageSize = recvfrom(server_fd, buffer, BUFFER_SIZE, 0,
(struct sockaddr *)&clientAddr, &clientLen);
if (recvMessageSize > 0) {
parsePacket(server_fd, clientAddr, buffer, recvMessageSize,
filteredDomains);
}
}
return 0;
}
// main function to parse packet
void parsePacket(int socket, struct sockaddr_in clientAddr,
unsigned char *buffer, unsigned long bufferSize,
TreeNode &filteredDomains) {
struct dns_header *dns = NULL;
struct question *qinfo = NULL;
unsigned char *qname;
dns = (struct dns_header *)buffer;
qname = (unsigned char *)&buffer[sizeof(struct dns_header)];
qinfo = (struct question *)&buffer[sizeof(struct dns_header) +
(strlen((const char *)qname) + 1)];
// osetrit kdyz prijde odpoved a ne query (pridat else if)
if (htons(qinfo->qtype) == 1 && htons(dns->qr) == 0 && htons(dns->z) == 0) {
unsigned char *name_with_sub = translateName(qname, buffer);
std::string s(reinterpret_cast<char *>(name_with_sub));
if (!TreeNode::filterDomain(s, &filteredDomains)) {
// adress is not filtered
if (isIpv6())
sendToResolverIpv6(name_with_sub, socket, clientAddr,
htons(dns->id));
else
sendToResolverIpv4(name_with_sub, socket, clientAddr,
htons(dns->id));
} else {
// adress is filtered
dns->qr = dns->ra = dns->rd = 1;
dns->ad = 0;
dns->rcode = 5;
sendto(socket, buffer, bufferSize, 0,
(struct sockaddr *)&clientAddr, sizeof(clientAddr));
}
free(name_with_sub);
} else {
// send not implemented
dns->qr = dns->rd = dns->ra = 1;
dns->rcode = 4;
dns->ad = 0;
sendto(socket, buffer, bufferSize, 0, (struct sockaddr *)&clientAddr,
sizeof(clientAddr));
}
}
// send packet to ipv6 server
void sendToResolverIpv6(unsigned char *resolverName, int serverSocket,
struct sockaddr_in clientAddr, int id) {
struct dns_header *dns = NULL;
struct question *qinfo = NULL;
struct sockaddr_in6 dest;
int resolver_fd;
unsigned char buffer[BUFFER_SIZE], *qname;
resolver_fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
dest.sin6_family = AF_INET6;
dest.sin6_port = htons(53);
inet_pton(AF_INET6, arguments->server, &(dest.sin6_addr));
dns = (struct dns_header *)buffer;
dns->id = (unsigned short)htons(getpid());
dns->qr = 0;
dns->opcode = 0;
dns->aa = 0;
dns->tc = 0;
dns->rd = 1;
dns->ra = 0;
dns->z = 0;
dns->ad = 0;
dns->cd = 0;
dns->rcode = 0;
dns->q_count = htons(1);
dns->ans_count = 0;
dns->auth_count = 0;
dns->add_count = 0;
qname = (unsigned char *)&buffer[sizeof(struct dns_header)];
changeToDnsNameFormat(qname, resolverName);
qinfo = (struct question *)&buffer[sizeof(struct dns_header) +
(strlen((const char *)qname) + 1)];
qinfo->qtype = htons(T_A);
qinfo->qclass = htons(1);
if (sendto(resolver_fd, (char *)buffer,
sizeof(struct dns_header) + (strlen((const char *)qname) + 1) +
sizeof(struct question),
0, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
std::cerr << "IPv6 sendto failed" << std::endl;
}
unsigned int i = sizeof(dest);
unsigned long recvMsgResolverSize;
recvMsgResolverSize = recvfrom(resolver_fd, (char *)buffer, BUFFER_SIZE, 0,
(struct sockaddr *)&dest, &i);
dns = (struct dns_header *)buffer;
dns->id = htons(id);
if (recvMsgResolverSize > 0) {
sendto(serverSocket, (char *)buffer, recvMsgResolverSize, 0,
(struct sockaddr *)&clientAddr, sizeof(clientAddr));
}
}
// send packet to ipv4 server
void sendToResolverIpv4(unsigned char *resolverName, int serverSocket,
struct sockaddr_in clientAddr, int id) {
struct dns_header *dns = NULL;
struct question *qinfo = NULL;
struct sockaddr_in dest;
int resolver_fd;
unsigned char buffer[BUFFER_SIZE], *qname;
resolver_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
dest.sin_family = AF_INET;
dest.sin_port = htons(53);
dest.sin_addr.s_addr = inet_addr(arguments->server);
dns = (struct dns_header *)buffer;
dns->id = (unsigned short)htons(getpid());
dns->qr = 0;
dns->opcode = 0;
dns->aa = 0;
dns->tc = 0;
dns->rd = 1;
dns->ra = 0;
dns->z = 0;
dns->ad = 0;
dns->cd = 0;
dns->rcode = 0;
dns->q_count = htons(1);
dns->ans_count = 0;
dns->auth_count = 0;
dns->add_count = 0;
qname = (unsigned char *)&buffer[sizeof(struct dns_header)];
changeToDnsNameFormat(qname, resolverName);
qinfo = (struct question *)&buffer[sizeof(struct dns_header) +
(strlen((const char *)qname) + 1)];
qinfo->qtype = htons(T_A);
qinfo->qclass = htons(1);
if (sendto(resolver_fd, (char *)buffer,
sizeof(struct dns_header) + (strlen((const char *)qname) + 1) +
sizeof(struct question),
0, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
std::cerr << "sendto failed" << std::endl;
}
unsigned int i = sizeof(dest);
unsigned long recvMsgResolverSize;
recvMsgResolverSize = recvfrom(resolver_fd, (char *)buffer, BUFFER_SIZE, 0,
(struct sockaddr *)&dest, &i);
dns = (struct dns_header *)buffer;
dns->id = htons(id);
if (recvMsgResolverSize > 0) {
sendto(serverSocket, (char *)buffer, recvMsgResolverSize, 0,
(struct sockaddr *)&clientAddr, sizeof(clientAddr));
}
}
// DNS Query Program on Linux
// Autor : Silver Moon ([email protected])
// Datum : 29/4/2009
// https://gist.github.com/fffaraz/9d9170b57791c28ccda9255b48315168
// change from www.google.com to 3www6google3com
void changeToDnsNameFormat(unsigned char *dns, unsigned char *host) {
unsigned int lock = 0;
strcat((char *)host, ".");
for (unsigned int i = 0; i < strlen((char *)host); i++) {
if (host[i] == '.') {
*dns++ = i - lock;
for (; lock < i; lock++) {
*dns++ = host[lock];
}
lock++;
}
}
*dns++ = '\0';
}
// DNS Query Program on Linux
// Autor : Silver Moon ([email protected])
// Datum : 29/4/2009
// https://gist.github.com/fffaraz/9d9170b57791c28ccda9255b48315168
// get name from buffer
unsigned char *translateName(unsigned char *reader, unsigned char *buffer) {
unsigned char *name;
unsigned int p = 0, offset;
name = (unsigned char *)malloc(256);
name[0] = '\0';
while (*reader != 0) {
if (*reader >= 192) {
offset = (*reader) * 256 + *(reader + 1) - 49152;
reader = buffer + offset - 1;
} else {
name[p++] = *reader;
}
reader = reader + 1;
}
name[p] = '\0';
int i;
for (i = 0; i < (int)strlen((const char *)name); i++) {
p = name[i];
for (int j = 0; j < (int)p; j++) {
name[i] = name[i + 1];
i = i + 1;
}
name[i] = '.';
}
name[i - 1] = '\0';
return name;
}
// check if server name can be translated
bool checkResolverName() {
struct hostent *he = NULL;
struct in_addr a;
he = gethostbyname(arguments->server);
if (he) {
while (*he->h_addr_list) {
bcopy(*he->h_addr_list++, (char *)&a, sizeof(a));
arguments->server =
(char *)realloc(arguments->server, strlen(inet_ntoa(a)) + 1);
if (arguments->server == NULL) sigintHandler(1);
strcpy(arguments->server, inet_ntoa(a));
return true;
}
}
return false;
}
// check if resolver server is IPv6
bool isIpv6() {
struct sockaddr_in6 ipv6;
return inet_pton(AF_INET6, arguments->server, &(ipv6.sin6_addr)) != 0;
}
// load file to TreeNode
void loadFile(TreeNode &tree) {
std::ifstream f;
f.open(arguments->filter);
if (f.is_open()) {
std::string line;
while (std::getline(f, line)) {
if (line[0] != '#' && !line.empty()) tree.addNode(line);
}
f.close();
tree.setFilterable(false);
}
}
// function for parsing arguments
int parseArguments(int argc, char **argv) {
arguments = (Args *)malloc(sizeof(Args));
if (arguments == NULL) return 1;
arguments->port = 53;
arguments->filter = arguments->server = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-s") == 0) {
if (argv[i + 1] != NULL) {
arguments->server =
(char *)malloc(sizeof(char) * (strlen(argv[i + 1]) + 1));
strcpy(arguments->server, argv[i + 1]);
}
}
if (strcmp(argv[i], "-f") == 0) {
if (argv[i + 1] != NULL) {
arguments->filter =
(char *)malloc(sizeof(char) * (strlen(argv[i + 1]) + 1));
strcpy(arguments->filter, argv[i + 1]);
}
}
if (strcmp(argv[i], "-p") == 0)
if (argv[i + 1] != NULL)
arguments->port = (int)strtol(argv[i + 1], NULL, 10);
}
if (arguments->filter == NULL || arguments->server == NULL) return 1;
return 0;
}
// function to clean memory after CTRL + C
void sigintHandler(int number) {
free(arguments->filter);
free(arguments->server);
free(arguments);
TreeNode::clearMemory(&filteredDomains);
exit(number);
}