-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_server.cpp
446 lines (349 loc) · 12.2 KB
/
dns_server.cpp
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
#include "dns_server.h"
void dns_server::init(double t,...) {
// PowerDEVS parameters
va_list parameters;
va_start(parameters,t);
// Set logger module name
std::string module_name = va_arg(parameters, char*);
logger.setModuleName("DNS " + module_name);
local_ip = va_arg(parameters,char*);
logger.info("DNS local ip: " + local_ip.as_string());
server_ip = va_arg(parameters,char*);
logger.info("DNS root server ip: " + server_ip.as_string());
// Building authoritative RR table
const char* file_path = va_arg(parameters,char*);
Parser<dns::ResourceRecord> parser_rr(file_path);
if (parser_rr.file_open()) {
while(true) {
try {
authoritative_RRs.push_back(parser_rr.next_input());
} catch(std::exception& e) {
break; // end of file throws an exception
}
}
} else {
logger.info("No authoritative RR table file.");
}
if (!authoritative_RRs.empty()) {
logger.info("Authoritative RR table:");
for (std::list<dns::ResourceRecord>::const_iterator it = authoritative_RRs.begin(); it != authoritative_RRs.end(); ++it) {
logger.info(it->as_string());
}
}
// Building authoritative RR table
file_path = va_arg(parameters,char*);
Parser<dns::Zone> parser_zone(file_path);
if (parser_zone.file_open()) {
while(true) {
try {
zone_servers.push_back(parser_zone.next_input());
logger.info(zone_servers.back().as_string());
} catch(std::exception& e) {
break; // end of file throws an exception
}
}
} else {
logger.info("No zone server table file.");
}
if (!zone_servers.empty()) {
logger.info("Zone servers table:");
for (std::list<dns::Zone>::const_iterator it = zone_servers.begin(); it != zone_servers.end(); ++it) {
logger.info(it->as_string());
}
}
// Getting recursive allowed parameter
recursive_allowed = va_arg(parameters,double);
if (recursive_allowed)
logger.info("Recursive allowed: True");
else
logger.info("Recursive allowed: False");
next_id = 0;
// Starting udp bind
bind = true;
start_reading = false;
next_internal = 0;
}
void dns_server::dinternal(double t) {
this->updateCache(t);
udp::Control c;
if (bind) {
bind = false;
start_reading = true;
c = udp::Control(0,udp::Ctrl::BIND);
c.local_port = 53;
c.local_ip = local_ip;
lower_layer_ctrl_out.push(c);
next_internal = 0;
return;
}
if (start_reading) {
start_reading = false;
c = udp::Control(0,udp::Ctrl::READ);
c.local_port = 53;
c.local_ip = local_ip;
lower_layer_ctrl_out.push(c);
next_internal = 0;
return;
}
if (!higher_layer_data_in.empty()) {
dns::DomainName s = higher_layer_data_in.front();
this->processDomainName(s);
higher_layer_data_in.pop();
next_internal = process_dns_query;
return;
}
if (!lower_layer_data_in.empty()) {
dns::Packet p = lower_layer_data_in.front();
this->processDNSPacket(p);
lower_layer_data_in.pop();
next_internal = process_dns_response;
start_reading = true;
return;
}
}
void dns_server::dexternal(double t) {
this->updateCache(t);
}
double dns_server::ta(double t) {
return next_internal;
}
Event dns_server::lambda(double) {
return output;
}
void dns_server::exit() {}
/**********************************************/
/************** Helper methods ****************/
/**********************************************/
void dns_server::processDomainName(dns::DomainName domain) {
logger.info("Process domain name " + domain.as_string());
dns::Packet p = this->QueryPacket(domain);
if (this->existRR(domain)) {
logger.info("Domain name is in cache");
this->directAnswer(p);
higher_layer_data_out.push(p);
} else {
logger.info("Domain not in cache");
logger.debug("\n" + p.as_string());
app_requests.push_back(p);
this->sendTo(p, server_ip, 53);
}
}
dns::Packet dns_server::QueryPacket(dns::DomainName d) {
// First aditional RR is the requested domain with the requester host IPv4
dns::ResourceRecord RRRequesterIP;
RRRequesterIP.name = d; // same name as the query
RRRequesterIP.QType = dns::Type::A;
RRRequesterIP.QClass = dns::Class::IN;
RRRequesterIP.AValue = local_ip; // client ip to let the server respond to this IPv4
// query RR
dns::ResourceRecord RRQuery;
RRQuery.name = d;
RRQuery.QType = dns::Type::A;
RRQuery.QClass = dns::Class::IN;
dns::Packet packet;
packet.header.id = next_id;
packet.header.setFlag(0x0000,0xFFFF); // set all in zero
packet.header.setFlag(dns::QR::QR_QUERY,dns::QR::QR_MASK);
packet.header.setFlag(dns::Opcode::Opcode_QUERY,dns::Opcode::Opcode_MASK);
packet.header.setFlag(dns::AA::AA_NOT_AUTHORITATIVE_ANSWER,dns::AA::AA_MASK);
packet.header.setFlag(dns::TC::TC_NOT_TRUNCATED,dns::TC::TC_MASK);
packet.header.setFlag(dns::RD::RD_RECURSIVE,dns::RD::RD_MASK);
packet.addQuestionResource(RRQuery);
packet.addAditionalResource(RRRequesterIP);
// update dns state
next_id++;
return packet;
}
void dns_server::sendTo(const dns::Packet& p, IPv4 server_ip, ushort server_port) {
udp::Control c(0, udp::Ctrl::SEND_TO);
c.local_ip = local_ip;
c.local_port = 53;
c.remote_ip = server_ip;
c.remote_port = server_port;
c.packet = p;
c.packet.aditionals.front().AValue = local_ip;
logger.info("Send DNS Query to: (" + c.remote_ip.as_string() + ", " + std::to_string(c.remote_port) + ")");
lower_layer_ctrl_out.push(c);
}
void dns_server::processDNSPacket(dns::Packet packet) {
dns::ResourceRecord r;
logger.info("Incoming packet:");
logger.info("\n" + packet.as_string());
if (packet.header.is(dns::QR::QR_ANSWER,dns::QR::QR_MASK)) { // ANSWER PACKET
logger.info("Answer packet");
if (!packet.header.is(dns::RCode::RCode_NO_ERROR,dns::RCode::RCode_MASK)) { // RETURN ERROR
logger.info("Error packet");
this->deliverAnswer(packet);
this->removePacket(packet.header.id);
return;
}
// If it's not a server error it must have an answer
r = packet.answers.front();
if (r.QType == dns::Type::A) { // RETURN FOUND IPv4 DNS PACKET
logger.info("Answer Type: A");
if (packet.header.is(dns::AA::AA_AUTHORITATIVE_ANSWER,dns::AA::AA_MASK)) {
logger.info("Caching authoritative answer");
cached_RRs.push_back(r);
}
this->deliverAnswer(packet);
this->removePacket(packet.header.id);
} else if (r.QType == dns::Type::NS) { // SEND REQUEST PACKET TO AUTHORITATIVE POINTED NAME SERVER
logger.info("Answer Type: NS");
dns::ResourceRecord ar = packet.authoritatives.front(); // authoritatives resources are allways A type
dns::Packet p = this->getPacket(packet.header.id);
this->sendTo(p, ar.AValue, 53);
}
} else { // QUERY PACKET
logger.info("Query packet");
r = packet.aditionals.front(); // first aditional is requester host ip
if (this->existRR(r.name)) { // RESOURCE EXIST
logger.info("Resource for domain " + r.name.as_string() + " exist");
this->directAnswer(packet);
this->sendTo(packet, r.AValue, 53);
} else if (packet.header.is(dns::RD::RD_RECURSIVE,dns::RD::RD_MASK) && recursive_allowed) { // RECURSIVE QUERY
logger.info("Recursive query for domain " + r.name.as_string());
host_requests.push_back(packet);
this->sendTo(packet, server_ip, 53);
} else { // RETURN BEST MATCH OR NAME ERROR
logger.info("No recursive query for domain " + r.name.as_string());
dns::Zone zone = this->getBestMatch(r.name);
if (!zone.empty()) {
logger.info("Zone found: " + zone.as_string());
packet.addAnswerResource(zone.name_server);
packet.addAuthoritativeResource(zone.authoritative);
packet.header.setFlag(dns::RCode::RCode_NO_ERROR,dns::RCode::RCode_MASK);
} else {
logger.info("Zone not found, send name error response");
packet.header.setFlag(dns::RCode::RCode_NAME_ERROR,dns::RCode::RCode_MASK);
}
this->setAsResponse(packet);
packet.header.setFlag(dns::AA::AA_NOT_AUTHORITATIVE_ANSWER,dns::AA::AA_MASK);
this->sendTo(packet, r.AValue, 53);
}
}
}
void dns_server::setAsResponse(dns::Packet& packet) {
packet.header.setFlag(dns::QR::QR_ANSWER,dns::QR::QR_MASK);
packet.header.setFlag(dns::TC::TC_NOT_TRUNCATED,dns::TC::TC_MASK);
}
void dns_server::directAnswer(dns::Packet& packet) {
this->setAsResponse(packet);
packet.header.setFlag(dns::RCode::RCode_NO_ERROR,dns::RCode::RCode_MASK);
dns::DomainName d = packet.aditionals.front().name;
this->setAuthoritativeFlag(packet, d);
packet.addAnswerResource(this->getRR(d));
}
void dns_server::deliverAnswer(const dns::Packet& packet) {
if (this->isAppRequest(packet.header.id)) {
logger.debug("Is app request");
higher_layer_data_out.push(packet);
} else if (this->isHostRequest(packet.header.id)) {
logger.debug("Is remote host request");
IPv4 host_ip = this->getPacket(packet.header.id).aditionals.front().AValue; // first aditional is requester host ip
this->sendTo(packet, host_ip, 53);
}
logger.debug("answer delivered");
}
void dns_server::setAuthoritativeFlag(dns::Packet& packet, dns::DomainName d) {
std::list<dns::ResourceRecord>::const_iterator it;
for(it = cached_RRs.begin(); it != cached_RRs.end(); ++it) {
if (it->name == d) {
packet.header.setFlag(dns::AA::AA_NOT_AUTHORITATIVE_ANSWER,dns::AA::AA_MASK);
return;
}
}
for(it = authoritative_RRs.begin(); it != authoritative_RRs.end(); ++it) {
if (it->name == d) {
packet.header.setFlag(dns::AA::AA_AUTHORITATIVE_ANSWER,dns::AA::AA_MASK);
return;
}
}
}
bool dns_server::isAppRequest(ushort id) const {
std::list<dns::Packet>::const_iterator it;
for(it = app_requests.begin(); it != app_requests.end(); ++it) {
if (it->header.id == id) return true;
}
return false;
}
bool dns_server::isHostRequest(ushort id) const {
std::list<dns::Packet>::const_iterator it;
for(it = host_requests.begin(); it != host_requests.end(); ++it) {
if (it->header.id == id) return true;
}
return false;
}
void dns_server::removePacket(ushort id) {
std::list<dns::Packet>::const_iterator it;
// Removing from app request
it = app_requests.begin();
while(it != app_requests.end()) {
if (it->header.id == id) {
it = app_requests.erase(it);
} else {
++it;
}
}
// Removing from host request
it = host_requests.begin();
while(it != host_requests.end()) {
if (it->header.id == id) {
it = host_requests.erase(it);
} else {
++it;
}
}
}
dns::Packet dns_server::getPacket(ushort id) {
std::list<dns::Packet>::const_iterator it;
for(it = app_requests.begin(); it != app_requests.end(); ++it) {
if (it->header.id == id) return *it;
}
for(it = host_requests.begin(); it != host_requests.end(); ++it) {
if (it->header.id == id) return *it;
}
return dns::Packet();
}
bool dns_server::existRR(const dns::DomainName& d) const {
std::list<dns::ResourceRecord>::const_iterator it;
for(it = cached_RRs.begin(); it != cached_RRs.end(); ++it) {
if (it->name == d) return true;
}
for(it = authoritative_RRs.begin(); it != authoritative_RRs.end(); ++it) {
if (it->name == d) return true;
}
return false;
}
dns::ResourceRecord dns_server::getRR(const dns::DomainName& d) {
std::list<dns::ResourceRecord>::const_iterator it;
for(it = cached_RRs.begin(); it != cached_RRs.end(); ++it) {
if (it->name == d) return *it;
}
for(it = authoritative_RRs.begin(); it != authoritative_RRs.end(); ++it) {
if (it->name == d) return *it;
}
return dns::ResourceRecord();
}
void dns_server::updateCache(double t) {
ushort elapsed_time = std::floor(t-last_transition);
std::list<dns::ResourceRecord>::iterator it = cached_RRs.begin();
while(it != cached_RRs.end()) {
it->TTL -= elapsed_time;
if (it->TTL <= 0) {
it = cached_RRs.erase(it);
} else {
++it;
}
}
}
dns::Zone dns_server::getBestMatch(dns::DomainName d) {
dns::Zone z;
std::list<dns::Zone>::const_iterator it;
logger.debug("Searching best zone for domain: " + d.as_string());
for(it = zone_servers.begin(); it != zone_servers.end(); ++it) {
if (it->isZoneOf(d) && (z.empty() || it->isZoneOf(z.authoritative.name))) {
z = *it;
}
}
return z;
}