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
486
|
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/test/unit_test.hpp>
#include "dnsparser.hh"
BOOST_AUTO_TEST_SUITE(test_dnsparser_cc)
BOOST_AUTO_TEST_CASE(test_editDNSPacketTTL) {
auto generatePacket = [](uint32_t ttl) {
DNSName name("powerdns.com.");
ComboAddress v4("1.2.3.4");
ComboAddress v6("2001:db8::1");
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
/* record we want to see altered */
pwR.startRecord(name, QType::A, ttl, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
/* same record but different TTL (yeah, don't do that but it's just a test) */
pwR.startRecord(name, QType::A, 100, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
/* different type */
pwR.startRecord(name, QType::AAAA, 42, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP6(std::string(reinterpret_cast<const char*>(v6.sin6.sin6_addr.s6_addr), 16));
pwR.commit();
/* different class */
pwR.startRecord(name, QType::A, 42, QClass::CHAOS, DNSResourceRecord::ANSWER);
pwR.commit();
/* different section */
pwR.startRecord(name, QType::A, 42, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
return packet;
};
auto firstPacket = generatePacket(42);
auto expectedAlteredPacket = generatePacket(84);
size_t called = 0;
editDNSPacketTTL(reinterpret_cast<char*>(firstPacket.data()), firstPacket.size(), [&called](uint8_t section, uint16_t class_, uint16_t type, uint32_t ttl) {
called++;
/* only updates the TTL of IN/A, in answer, with an existing ttl of 42 */
if (section == 1 && class_ == QClass::IN && type == QType::A && ttl == 42) {
return 84;
}
return 0;
});
/* check that we have been for all records */
BOOST_CHECK_EQUAL(called, 5U);
BOOST_REQUIRE_EQUAL(firstPacket.size(), expectedAlteredPacket.size());
for (size_t idx = 0; idx < firstPacket.size(); idx++) {
BOOST_CHECK_EQUAL(firstPacket.at(idx), expectedAlteredPacket.at(idx));
}
BOOST_CHECK(firstPacket == expectedAlteredPacket);
/* now call it with a truncated packet, missing the last TTL and rdata,
we should only be called 4 times but everything else should be fine. */
called = 0;
editDNSPacketTTL(reinterpret_cast<char*>(firstPacket.data()), firstPacket.size() - sizeof(uint32_t) - /* rdata length */ sizeof (uint16_t) - /* IPv4 payload in rdata */ 4, [&called](uint8_t section, uint16_t class_, uint16_t type, uint32_t ttl) {
called++;
/* only updates the TTL of IN/A, in answer, with an existing ttl of 42 */
if (section == 1 && class_ == QClass::IN && type == QType::A && ttl == 42) {
return 84;
}
return 0;
});
/* check that we have been for all records */
BOOST_CHECK_EQUAL(called, 4U);
BOOST_CHECK(firstPacket == expectedAlteredPacket);
}
BOOST_AUTO_TEST_CASE(test_ageDNSPacket) {
auto generatePacket = [](uint32_t ttl) {
DNSName name("powerdns.com.");
ComboAddress v4("1.2.3.4");
ComboAddress v6("2001:db8::1");
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
/* record we want to see altered */
pwR.startRecord(name, QType::A, ttl, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
return packet;
};
auto firstPacket = generatePacket(3600);
auto expectedAlteredPacket = generatePacket(1800);
ageDNSPacket(reinterpret_cast<char*>(firstPacket.data()), firstPacket.size(), 1800);
BOOST_REQUIRE_EQUAL(firstPacket.size(), expectedAlteredPacket.size());
for (size_t idx = 0; idx < firstPacket.size(); idx++) {
BOOST_CHECK_EQUAL(firstPacket.at(idx), expectedAlteredPacket.at(idx));
}
BOOST_CHECK(firstPacket == expectedAlteredPacket);
/* now call it with a truncated packet, missing the last TTL and rdata,
the packet should not be altered. */
ageDNSPacket(reinterpret_cast<char*>(firstPacket.data()), firstPacket.size() - sizeof(uint32_t) - /* rdata length */ sizeof (uint16_t) - /* IPv4 payload in rdata */ 4 - /* size of OPT record */ 11, 900);
BOOST_CHECK(firstPacket == expectedAlteredPacket);
/* now remove more than the remaining TTL. We expect ageDNSPacket
to cap this at zero and not cause an unsigned underflow into
the 2^32-1 neighbourhood */
ageDNSPacket(reinterpret_cast<char*>(firstPacket.data()), firstPacket.size(), 1801);
uint32_t ttl = 0;
expectedAlteredPacket = generatePacket(ttl);
BOOST_REQUIRE_EQUAL(firstPacket.size(), expectedAlteredPacket.size());
for (size_t idx = 0; idx < firstPacket.size(); idx++) {
BOOST_CHECK_EQUAL(firstPacket.at(idx), expectedAlteredPacket.at(idx));
}
BOOST_CHECK(firstPacket == expectedAlteredPacket);
}
BOOST_AUTO_TEST_CASE(test_getDNSPacketMinTTL) {
const DNSName name("powerdns.com.");
const ComboAddress v4("1.2.3.4");
const ComboAddress v6("2001:db8::1");
{
/* no records */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), nullptr);
BOOST_CHECK_EQUAL(result, std::numeric_limits<uint32_t>::max());
}
{
/* only one record, not an OPT one */
uint32_t ttl = 42;
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, ttl, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), nullptr);
BOOST_CHECK_EQUAL(result, ttl);
}
{
/* only one record, an OPT one */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), nullptr);
BOOST_CHECK_EQUAL(result, std::numeric_limits<uint32_t>::max());
}
{
/* records with different TTLs, should return the lower */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 257, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), nullptr);
BOOST_CHECK_EQUAL(result, 255U);
}
{
/* SOA record in answer, seenAuthSOA should not be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::ANSWER);
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 255U);
BOOST_CHECK_EQUAL(seenAuthSOA, false);
}
{
/* one SOA record in auth, seenAuthSOA should be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 255U);
BOOST_CHECK_EQUAL(seenAuthSOA, true);
}
{
/* one SOA record of the wrong qclass in auth, seenAuthSOA should not be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 257, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 255, QClass::CHAOS, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 255U);
BOOST_CHECK_EQUAL(seenAuthSOA, false);
}
{
/* one A record in auth, seenAuthSOA should not be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 257U);
BOOST_CHECK_EQUAL(seenAuthSOA, false);
}
{
/* one SOA record in additional, seenAuthSOA should not be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::SOA, 255, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size(), &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 255U);
BOOST_CHECK_EQUAL(seenAuthSOA, false);
}
{
/* truncated packet, no exception should be raised */
/* one SOA record in auth, seenAuthSOA should be set */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 254, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
bool seenAuthSOA = false;
auto result = getDNSPacketMinTTL(reinterpret_cast<char*>(packet.data()), packet.size() - sizeof(uint32_t) - /* rdata length */ sizeof (uint16_t) - /* IPv4 payload in rdata */ 4, &seenAuthSOA);
BOOST_CHECK_EQUAL(result, 255U);
BOOST_CHECK_EQUAL(seenAuthSOA, true);
}
}
BOOST_AUTO_TEST_CASE(test_getDNSPacketLength) {
const DNSName name("powerdns.com.");
const ComboAddress v4("1.2.3.4");
const ComboAddress v6("2001:db8::1");
{
/* no records */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
auto result = getDNSPacketLength(reinterpret_cast<char*>(packet.data()), packet.size());
BOOST_CHECK_EQUAL(result, packet.size());
}
{
/* several records */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
auto result = getDNSPacketLength(reinterpret_cast<char*>(packet.data()), packet.size());
BOOST_CHECK_EQUAL(result, packet.size());
}
{
/* trailing data */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
auto realSize = packet.size();
packet.resize(realSize + 512);
auto result = getDNSPacketLength(reinterpret_cast<char*>(packet.data()), packet.size());
BOOST_CHECK_EQUAL(result, realSize);
}
{
/* truncated packet, should return the full size */
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
size_t fakeSize = packet.size()-1;
auto result = getDNSPacketLength(reinterpret_cast<char*>(packet.data()), fakeSize);
BOOST_CHECK_EQUAL(result, fakeSize);
}
}
BOOST_AUTO_TEST_CASE(test_getRecordsOfTypeCount) {
const DNSName name("powerdns.com.");
const ComboAddress v4("1.2.3.4");
const ComboAddress v6("2001:db8::1");
{
vector<uint8_t> packet;
DNSPacketWriter pwR(packet, name, QType::A, QClass::IN, 0);
pwR.getHeader()->qr = 1;
pwR.commit();
pwR.startRecord(name, QType::A, 255, QClass::IN, DNSResourceRecord::ANSWER);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.startRecord(name, QType::SOA, 257, QClass::IN, DNSResourceRecord::AUTHORITY);
pwR.commit();
pwR.startRecord(name, QType::A, 256, QClass::IN, DNSResourceRecord::ADDITIONAL);
pwR.xfrIP(v4.sin4.sin_addr.s_addr);
pwR.commit();
pwR.addOpt(4096, 0, 0);
pwR.commit();
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 0, QType::A), 1);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 0, QType::SOA), 0);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 1, QType::A), 1);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 1, QType::SOA), 0);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 2, QType::A), 0);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 2, QType::SOA), 1);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 3, QType::A), 1);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 3, QType::SOA), 0);
BOOST_CHECK_EQUAL(getRecordsOfTypeCount(reinterpret_cast<char*>(packet.data()), packet.size(), 4, QType::SOA), 0);
}
}
BOOST_AUTO_TEST_SUITE_END()
|