-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDnsLayer.cpp
More file actions
770 lines (636 loc) · 24.2 KB
/
DnsLayer.cpp
File metadata and controls
770 lines (636 loc) · 24.2 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#define LOG_MODULE pcpp::PacketLogModuleDnsLayer
#include "DnsLayer.h"
#include "EndianPortable.h"
#include <pcapplusplus/IpAddress.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
#include <pcapplusplus/Logger.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string.h>
namespace visor::lib::dns {
DnsLayer::DnsLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, pcpp::Packet *packet)
: Layer(data, dataLen, prevLayer, packet)
{
m_Protocol = pcpp::DNS;
m_ResourceList = NULL;
m_FirstQuery = NULL;
m_FirstAnswer = NULL;
m_FirstAuthority = NULL;
m_FirstAdditional = NULL;
}
DnsLayer::DnsLayer()
{
const size_t headerLen = sizeof(dnshdr);
m_DataLen = headerLen;
m_Data = new uint8_t[headerLen];
memset(m_Data, 0, headerLen);
m_Protocol = pcpp::DNS;
m_ResourceList = NULL;
m_FirstQuery = NULL;
m_FirstAnswer = NULL;
m_FirstAuthority = NULL;
m_FirstAdditional = NULL;
}
DnsLayer::DnsLayer(const DnsLayer &other)
: Layer(other)
{
m_Protocol = pcpp::DNS;
m_ResourceList = NULL;
m_FirstQuery = NULL;
m_FirstAnswer = NULL;
m_FirstAuthority = NULL;
m_FirstAdditional = NULL;
}
DnsLayer &DnsLayer::operator=(const DnsLayer &other)
{
Layer::operator=(other);
IDnsResource *curResource = m_ResourceList;
while (curResource != NULL) {
IDnsResource *temp = curResource->getNextResource();
delete curResource;
curResource = temp;
}
m_ResourceList = NULL;
m_FirstQuery = NULL;
m_FirstAnswer = NULL;
m_FirstAuthority = NULL;
m_FirstAdditional = NULL;
return (*this);
}
DnsLayer::~DnsLayer()
{
IDnsResource *curResource = m_ResourceList;
while (curResource != NULL) {
IDnsResource *nextResource = curResource->getNextResource();
delete curResource;
curResource = nextResource;
}
}
bool DnsLayer::extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource *resource)
{
if (!Layer::extendLayer(offsetInLayer, numOfBytesToExtend))
return false;
IDnsResource *curResource = resource->getNextResource();
while (curResource != NULL) {
curResource->m_OffsetInLayer += numOfBytesToExtend;
curResource = curResource->getNextResource();
}
return true;
}
bool DnsLayer::shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource *resource)
{
if (!Layer::shortenLayer(offsetInLayer, numOfBytesToShorten))
return false;
IDnsResource *curResource = resource->getNextResource();
while (curResource != NULL) {
curResource->m_OffsetInLayer -= numOfBytesToShorten;
curResource = curResource->getNextResource();
}
return true;
}
bool DnsLayer::parseResources(bool queryOnly, bool additionalOnly, bool forceParse)
{
if (m_ResourcesParsed && (!forceParse || !m_ResourcesParseResult)) {
return m_ResourcesParseResult;
}
size_t offsetInPacket = sizeof(dnshdr);
IDnsResource *curResource = m_ResourceList;
uint16_t numOfQuestions = be16toh(getDnsHeader()->numberOfQuestions);
uint16_t numOfAnswers = be16toh(getDnsHeader()->numberOfAnswers);
uint16_t numOfAuthority = be16toh(getDnsHeader()->numberOfAuthority);
uint16_t numOfAdditional = be16toh(getDnsHeader()->numberOfAdditional);
uint32_t numOfOtherResources = numOfQuestions + numOfAnswers + numOfAuthority + numOfAdditional;
if (numOfOtherResources > 100) {
// probably bad packet
m_ResourcesParsed = true;
m_ResourcesParseResult = false;
return m_ResourcesParseResult;
}
for (uint32_t i = 0; i < numOfOtherResources; i++) {
DnsResourceType resType;
if (numOfQuestions > 0) {
resType = DnsQueryType;
numOfQuestions--;
} else if (numOfAnswers > 0) {
resType = DnsAnswerType;
numOfAnswers--;
} else if (numOfAuthority > 0) {
resType = DnsAuthorityType;
numOfAuthority--;
} else {
resType = DnsAdditionalType;
numOfAdditional--;
}
DnsResource *newResource = NULL;
DnsQuery *newQuery = NULL;
IDnsResource *newGenResource = NULL;
if (resType == DnsQueryType) {
newQuery = new DnsQuery(this, offsetInPacket);
newGenResource = newQuery;
offsetInPacket += newQuery->getSize();
} else {
newResource = new DnsResource(this, offsetInPacket, resType);
newGenResource = newResource;
offsetInPacket += newResource->getSize();
}
if (offsetInPacket > m_DataLen) {
// Parse packet failed, DNS resource is out of bounds. Probably a bad packet
delete newGenResource;
m_ResourcesParsed = true;
m_ResourcesParseResult = false;
return m_ResourcesParseResult;
}
// this resource is the first resource
if (m_ResourceList == NULL) {
m_ResourceList = newGenResource;
curResource = m_ResourceList;
} else {
curResource->setNextResource(newGenResource);
curResource = curResource->getNextResource();
}
if (resType == DnsQueryType && m_FirstQuery == NULL) {
m_FirstQuery = newQuery;
if (queryOnly) {
break;
}
} else if (resType == DnsAnswerType && m_FirstAnswer == NULL)
m_FirstAnswer = newResource;
else if (resType == DnsAuthorityType && m_FirstAuthority == NULL)
m_FirstAuthority = newResource;
else if (resType == DnsAdditionalType && m_FirstAdditional == NULL) {
m_FirstAdditional = newResource;
if (additionalOnly) {
break;
}
}
}
m_ResourcesParsed = true;
m_ResourcesParseResult = true;
return m_ResourcesParseResult;
}
IDnsResource *DnsLayer::getResourceByName(IDnsResource *startFrom, size_t resourceCount, const std::string &name, bool exactMatch) const
{
size_t index = 0;
while (index < resourceCount) {
if (startFrom == NULL)
return NULL;
std::string resourceName = startFrom->getName();
if (exactMatch && resourceName == name)
return startFrom;
else if (!exactMatch && resourceName.find(name) != std::string::npos)
return startFrom;
startFrom = startFrom->getNextResource();
index++;
}
return NULL;
}
DnsQuery *DnsLayer::getQuery(const std::string &name, bool exactMatch) const
{
uint16_t numOfQueries = be16toh(getDnsHeader()->numberOfQuestions);
IDnsResource *res = getResourceByName(m_FirstQuery, numOfQueries, name, exactMatch);
if (res != NULL)
return dynamic_cast<DnsQuery *>(res);
return NULL;
}
DnsQuery *DnsLayer::getFirstQuery() const
{
return m_FirstQuery;
}
DnsQuery *DnsLayer::getNextQuery(DnsQuery *query) const
{
if (query == NULL
|| query->getNextResource() == NULL
|| query->getType() != DnsQueryType
|| query->getNextResource()->getType() != DnsQueryType)
return NULL;
return (DnsQuery *)(query->getNextResource());
}
size_t DnsLayer::getQueryCount() const
{
return be16toh(getDnsHeader()->numberOfQuestions);
}
DnsResource *DnsLayer::getAnswer(const std::string &name, bool exactMatch) const
{
uint16_t numOfAnswers = be16toh(getDnsHeader()->numberOfAnswers);
IDnsResource *res = getResourceByName(m_FirstAnswer, numOfAnswers, name, exactMatch);
if (res != NULL)
return dynamic_cast<DnsResource *>(res);
return NULL;
}
DnsResource *DnsLayer::getFirstAnswer() const
{
return m_FirstAnswer;
}
DnsResource *DnsLayer::getNextAnswer(DnsResource *answer) const
{
if (answer == NULL
|| answer->getNextResource() == NULL
|| answer->getType() != DnsAnswerType
|| answer->getNextResource()->getType() != DnsAnswerType)
return NULL;
return (DnsResource *)(answer->getNextResource());
}
size_t DnsLayer::getAnswerCount() const
{
return be16toh(getDnsHeader()->numberOfAnswers);
}
DnsResource *DnsLayer::getAuthority(const std::string &name, bool exactMatch) const
{
uint16_t numOfAuthorities = be16toh(getDnsHeader()->numberOfAuthority);
IDnsResource *res = getResourceByName(m_FirstAuthority, numOfAuthorities, name, exactMatch);
if (res != NULL)
return dynamic_cast<DnsResource *>(res);
return NULL;
}
DnsResource *DnsLayer::getFirstAuthority() const
{
return m_FirstAuthority;
}
DnsResource *DnsLayer::getNextAuthority(DnsResource *authority) const
{
if (authority == NULL
|| authority->getNextResource() == NULL
|| authority->getType() != DnsAuthorityType
|| authority->getNextResource()->getType() != DnsAuthorityType)
return NULL;
return (DnsResource *)(authority->getNextResource());
}
size_t DnsLayer::getAuthorityCount() const
{
return be16toh(getDnsHeader()->numberOfAuthority);
}
DnsResource *DnsLayer::getAdditionalRecord(const std::string &name, bool exactMatch) const
{
uint16_t numOfAdditionalRecords = be16toh(getDnsHeader()->numberOfAdditional);
IDnsResource *res = getResourceByName(m_FirstAdditional, numOfAdditionalRecords, name, exactMatch);
if (res != NULL)
return dynamic_cast<DnsResource *>(res);
return NULL;
}
DnsResource *DnsLayer::getFirstAdditionalRecord() const
{
return m_FirstAdditional;
}
DnsResource *DnsLayer::getNextAdditionalRecord(DnsResource *additionalRecord) const
{
if (additionalRecord == NULL
|| additionalRecord->getNextResource() == NULL
|| additionalRecord->getType() != DnsAdditionalType
|| additionalRecord->getNextResource()->getType() != DnsAdditionalType)
return NULL;
return (DnsResource *)(additionalRecord->getNextResource());
}
size_t DnsLayer::getAdditionalRecordCount() const
{
return be16toh(getDnsHeader()->numberOfAdditional);
}
std::string DnsLayer::toString() const
{
std::ostringstream tidAsString;
tidAsString << be16toh(getDnsHeader()->transactionID);
std::ostringstream queryCount;
queryCount << getQueryCount();
std::ostringstream answerCount;
answerCount << getAnswerCount();
std::ostringstream authorityCount;
authorityCount << getAuthorityCount();
std::ostringstream additionalCount;
additionalCount << getAdditionalRecordCount();
if (getAnswerCount() > 0) {
return "DNS query response, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str();
} else if (getQueryCount() > 0) {
return "DNS query, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str();
} else // not likely - a DNS with no answers and no queries
{
return "DNS record without queries and answers, ID: " + tidAsString.str() + ";" + " queries: " + queryCount.str() + ", answers: " + answerCount.str() + ", authorities: " + authorityCount.str() + ", additional record: " + additionalCount.str();
}
}
IDnsResource *DnsLayer::getFirstResource(DnsResourceType resType) const
{
switch (resType) {
case DnsQueryType: {
return m_FirstQuery;
}
case DnsAnswerType: {
return m_FirstAnswer;
}
case DnsAuthorityType: {
return m_FirstAuthority;
}
case DnsAdditionalType: {
return m_FirstAdditional;
}
default:
return NULL;
}
}
void DnsLayer::setFirstResource(DnsResourceType resType, IDnsResource *resource)
{
switch (resType) {
case DnsQueryType: {
m_FirstQuery = dynamic_cast<DnsQuery *>(resource);
break;
}
case DnsAnswerType: {
m_FirstAnswer = dynamic_cast<DnsResource *>(resource);
break;
}
case DnsAuthorityType: {
m_FirstAuthority = dynamic_cast<DnsResource *>(resource);
break;
}
case DnsAdditionalType: {
m_FirstAdditional = dynamic_cast<DnsResource *>(resource);
break;
}
default:
return;
}
}
DnsResource *DnsLayer::addResource(DnsResourceType resType, const std::string &name, DnsType dnsType, DnsClass dnsClass,
uint32_t ttl, IDnsResourceData *data)
{
// create new query on temporary buffer
uint8_t newResourceRawData[256];
memset(newResourceRawData, 0, sizeof(newResourceRawData));
DnsResource *newResource = new DnsResource(newResourceRawData, resType);
newResource->setDnsClass(dnsClass);
newResource->setDnsType(dnsType);
// cannot return false since layer shouldn't be extended or shortened in this stage
newResource->setName(name);
newResource->setTTL(ttl);
if (!newResource->setData(data)) {
delete newResource;
PCPP_LOG_ERROR("Couldn't set new resource data");
return NULL;
}
size_t newResourceOffsetInLayer = sizeof(dnshdr);
IDnsResource *curResource = m_ResourceList;
while (curResource != NULL && curResource->getType() <= resType) {
newResourceOffsetInLayer += curResource->getSize();
IDnsResource *nextResource = curResource->getNextResource();
if (nextResource == NULL || nextResource->getType() > resType)
break;
curResource = nextResource;
}
// set next resource for new resource. This must happen here for extendLayer to succeed
if (curResource != NULL) {
if (curResource->getType() > newResource->getType())
newResource->setNextResource(m_ResourceList);
else
newResource->setNextResource(curResource->getNextResource());
} else // curResource != NULL
newResource->setNextResource(m_ResourceList);
// extend layer to make room for the new resource
if (!extendLayer(newResourceOffsetInLayer, newResource->getSize(), newResource)) {
PCPP_LOG_ERROR("Couldn't extend DNS layer, addResource failed");
delete newResource;
return NULL;
}
// connect the new resource to layer
newResource->setDnsLayer(this, newResourceOffsetInLayer);
// connect the new resource to the layer's resource list
if (curResource != NULL) {
curResource->setNextResource(newResource);
// this means the new resource is the first of it's type
if (curResource->getType() < newResource->getType()) {
setFirstResource(resType, newResource);
}
// this means the new resource should be the first resource in the packet
else if (curResource->getType() > newResource->getType()) {
m_ResourceList = newResource;
setFirstResource(resType, newResource);
}
} else // curResource != NULL, meaning this is the first resource in layer
{
m_ResourceList = newResource;
setFirstResource(resType, newResource);
}
return newResource;
}
DnsQuery *DnsLayer::addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
{
// create new query on temporary buffer
uint8_t newQueryRawData[256];
DnsQuery *newQuery = new DnsQuery(newQueryRawData);
newQuery->setDnsClass(dnsClass);
newQuery->setDnsType(dnsType);
// cannot return false since layer shouldn't be extended or shortened in this stage
newQuery->setName(name);
// find the offset in the layer to insert the new query
size_t newQueryOffsetInLayer = sizeof(dnshdr);
DnsQuery *curQuery = getFirstQuery();
while (curQuery != NULL) {
newQueryOffsetInLayer += curQuery->getSize();
DnsQuery *nextQuery = getNextQuery(curQuery);
if (nextQuery == NULL)
break;
curQuery = nextQuery;
}
// set next resource for new query. This must happen here for extendLayer to succeed
if (curQuery != NULL)
newQuery->setNextResource(curQuery->getNextResource());
else
newQuery->setNextResource(m_ResourceList);
// extend layer to make room for the new query
if (!extendLayer(newQueryOffsetInLayer, newQuery->getSize(), newQuery)) {
PCPP_LOG_ERROR("Couldn't extend DNS layer, addQuery failed");
delete newQuery;
return NULL;
}
// connect the new query to layer
newQuery->setDnsLayer(this, newQueryOffsetInLayer);
// connect the new query to the layer's resource list
if (curQuery != NULL)
curQuery->setNextResource(newQuery);
else // curQuery == NULL, meaning this is the first query
{
m_ResourceList = newQuery;
m_FirstQuery = newQuery;
}
// increase number of queries
getDnsHeader()->numberOfQuestions = htobe16(getQueryCount() + 1);
return newQuery;
}
DnsQuery *DnsLayer::addQuery(DnsQuery *const copyQuery)
{
if (copyQuery == NULL)
return NULL;
return addQuery(copyQuery->getName(), copyQuery->getDnsType(), copyQuery->getDnsClass());
}
bool DnsLayer::removeQuery(const std::string &queryNameToRemove, bool exactMatch)
{
DnsQuery *queryToRemove = getQuery(queryNameToRemove, exactMatch);
if (queryToRemove == NULL) {
PCPP_LOG_DEBUG("Query not found");
return false;
}
return removeQuery(queryToRemove);
}
bool DnsLayer::removeQuery(DnsQuery *queryToRemove)
{
bool res = removeResource(queryToRemove);
if (res) {
// decrease number of query records
getDnsHeader()->numberOfQuestions = htobe16(getQueryCount() - 1);
}
return res;
}
DnsResource *DnsLayer::addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
{
DnsResource *res = addResource(DnsAnswerType, name, dnsType, dnsClass, ttl, data);
if (res != NULL) {
// increase number of answer records
getDnsHeader()->numberOfAnswers = htobe16(getAnswerCount() + 1);
}
return res;
}
DnsResource *DnsLayer::addAnswer(DnsResource *const copyAnswer)
{
if (copyAnswer == NULL)
return NULL;
return addAnswer(copyAnswer->getName(), copyAnswer->getDnsType(), copyAnswer->getDnsClass(), copyAnswer->getTTL(), copyAnswer->getData().get());
}
bool DnsLayer::removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
{
DnsResource *answerToRemove = getAnswer(answerNameToRemove, exactMatch);
if (answerToRemove == NULL) {
PCPP_LOG_DEBUG("Answer record not found");
return false;
}
return removeAnswer(answerToRemove);
}
bool DnsLayer::removeAnswer(DnsResource *answerToRemove)
{
bool res = removeResource(answerToRemove);
if (res) {
// decrease number of answer records
getDnsHeader()->numberOfAnswers = htobe16(getAnswerCount() - 1);
}
return res;
}
DnsResource *DnsLayer::addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
{
DnsResource *res = addResource(DnsAuthorityType, name, dnsType, dnsClass, ttl, data);
if (res != NULL) {
// increase number of authority records
getDnsHeader()->numberOfAuthority = htobe16(getAuthorityCount() + 1);
}
return res;
}
DnsResource *DnsLayer::addAuthority(DnsResource *const copyAuthority)
{
if (copyAuthority == NULL)
return NULL;
return addAuthority(copyAuthority->getName(), copyAuthority->getDnsType(), copyAuthority->getDnsClass(), copyAuthority->getTTL(), copyAuthority->getData().get());
}
bool DnsLayer::removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
{
DnsResource *authorityToRemove = getAuthority(authorityNameToRemove, exactMatch);
if (authorityToRemove == NULL) {
PCPP_LOG_DEBUG("Authority not found");
return false;
}
return removeAuthority(authorityToRemove);
}
bool DnsLayer::removeAuthority(DnsResource *authorityToRemove)
{
bool res = removeResource(authorityToRemove);
if (res) {
// decrease number of authority records
getDnsHeader()->numberOfAuthority = htobe16(getAuthorityCount() - 1);
}
return res;
}
DnsResource *DnsLayer::addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
{
DnsResource *res = addResource(DnsAdditionalType, name, dnsType, dnsClass, ttl, data);
if (res != NULL) {
// increase number of authority records
getDnsHeader()->numberOfAdditional = htobe16(getAdditionalRecordCount() + 1);
}
return res;
}
DnsResource *DnsLayer::addAdditionalRecord(const std::string &name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData *data)
{
DnsResource *res = addAdditionalRecord(name, dnsType, DNS_CLASS_ANY, customData2, data);
if (res != NULL) {
res->setCustomDnsClass(customData1);
}
return res;
}
DnsResource *DnsLayer::addAdditionalRecord(DnsResource *const copyAdditionalRecord)
{
if (copyAdditionalRecord == NULL)
return NULL;
return addAdditionalRecord(copyAdditionalRecord->getName(), copyAdditionalRecord->getDnsType(), copyAdditionalRecord->getCustomDnsClass(), copyAdditionalRecord->getTTL(), copyAdditionalRecord->getData().get());
}
bool DnsLayer::removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
{
DnsResource *additionalRecordToRemove = getAdditionalRecord(additionalRecordNameToRemove, exactMatch);
if (additionalRecordToRemove == NULL) {
PCPP_LOG_DEBUG("Additional record not found");
return false;
}
return removeAdditionalRecord(additionalRecordToRemove);
}
bool DnsLayer::removeAdditionalRecord(DnsResource *additionalRecordToRemove)
{
bool res = removeResource(additionalRecordToRemove);
if (res) {
// decrease number of additional records
getDnsHeader()->numberOfAdditional = htobe16(getAdditionalRecordCount() - 1);
}
return res;
}
bool DnsLayer::removeResource(IDnsResource *resourceToRemove)
{
if (resourceToRemove == NULL) {
PCPP_LOG_DEBUG("resourceToRemove cannot be NULL");
return false;
}
// find the resource preceding resourceToRemove
IDnsResource *prevResource = m_ResourceList;
if (m_ResourceList != resourceToRemove) {
while (prevResource != NULL) {
IDnsResource *temp = prevResource->getNextResource();
if (temp == resourceToRemove)
break;
prevResource = temp;
}
}
if (prevResource == NULL) {
PCPP_LOG_DEBUG("Resource not found");
return false;
}
// shorten the layer and fix offset in layer for all next DNS resources in the packet
if (!shortenLayer(resourceToRemove->m_OffsetInLayer, resourceToRemove->getSize(), resourceToRemove)) {
PCPP_LOG_ERROR("Couldn't shorten the DNS layer, resource cannot be removed");
return false;
}
// remove resourceToRemove from the resources linked list
if (m_ResourceList != resourceToRemove) {
prevResource->setNextResource(resourceToRemove->getNextResource());
} else {
m_ResourceList = resourceToRemove->getNextResource();
}
// check whether resourceToRemove was the first of its type
if (getFirstResource(resourceToRemove->getType()) == resourceToRemove) {
IDnsResource *nextResource = resourceToRemove->getNextResource();
if (nextResource != NULL && nextResource->getType() == resourceToRemove->getType())
setFirstResource(resourceToRemove->getType(), nextResource);
else
setFirstResource(resourceToRemove->getType(), NULL);
}
// free resourceToRemove memory
delete resourceToRemove;
return true;
}
} // namespace visor