Skip to content

Commit 6a1508d

Browse files
committed
update visor_dns to match newer pcapplusplus version
1 parent 7940a37 commit 6a1508d

7 files changed

Lines changed: 112 additions & 184 deletions

File tree

libs/visor_dns/DnsLayer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ bool DnsLayer::parseResources(bool queryOnly, bool additionalOnly, bool forcePar
182182
m_ResourceList = newGenResource;
183183
curResource = m_ResourceList;
184184
} else {
185-
curResource->setNexResource(newGenResource);
185+
curResource->setNextResource(newGenResource);
186186
curResource = curResource->getNextResource();
187187
}
188188

@@ -458,11 +458,11 @@ DnsResource *DnsLayer::addResource(DnsResourceType resType, const std::string &n
458458
// set next resource for new resource. This must happen here for extendLayer to succeed
459459
if (curResource != NULL) {
460460
if (curResource->getType() > newResource->getType())
461-
newResource->setNexResource(m_ResourceList);
461+
newResource->setNextResource(m_ResourceList);
462462
else
463-
newResource->setNexResource(curResource->getNextResource());
463+
newResource->setNextResource(curResource->getNextResource());
464464
} else // curResource != NULL
465-
newResource->setNexResource(m_ResourceList);
465+
newResource->setNextResource(m_ResourceList);
466466

467467
// extend layer to make room for the new resource
468468
if (!extendLayer(newResourceOffsetInLayer, newResource->getSize(), newResource)) {
@@ -476,7 +476,7 @@ DnsResource *DnsLayer::addResource(DnsResourceType resType, const std::string &n
476476

477477
// connect the new resource to the layer's resource list
478478
if (curResource != NULL) {
479-
curResource->setNexResource(newResource);
479+
curResource->setNextResource(newResource);
480480
// this means the new resource is the first of it's type
481481
if (curResource->getType() < newResource->getType()) {
482482
setFirstResource(resType, newResource);
@@ -522,9 +522,9 @@ DnsQuery *DnsLayer::addQuery(const std::string &name, DnsType dnsType, DnsClass
522522

523523
// set next resource for new query. This must happen here for extendLayer to succeed
524524
if (curQuery != NULL)
525-
newQuery->setNexResource(curQuery->getNextResource());
525+
newQuery->setNextResource(curQuery->getNextResource());
526526
else
527-
newQuery->setNexResource(m_ResourceList);
527+
newQuery->setNextResource(m_ResourceList);
528528

529529
// extend layer to make room for the new query
530530
if (!extendLayer(newQueryOffsetInLayer, newQuery->getSize(), newQuery)) {
@@ -538,7 +538,7 @@ DnsQuery *DnsLayer::addQuery(const std::string &name, DnsType dnsType, DnsClass
538538

539539
// connect the new query to the layer's resource list
540540
if (curQuery != NULL)
541-
curQuery->setNexResource(newQuery);
541+
curQuery->setNextResource(newQuery);
542542
else // curQuery == NULL, meaning this is the first query
543543
{
544544
m_ResourceList = newQuery;
@@ -747,7 +747,7 @@ bool DnsLayer::removeResource(IDnsResource *resourceToRemove)
747747

748748
// remove resourceToRemove from the resources linked list
749749
if (m_ResourceList != resourceToRemove) {
750-
prevResource->setNexResource(resourceToRemove->getNextResource());
750+
prevResource->setNextResource(resourceToRemove->getNextResource());
751751
} else {
752752
m_ResourceList = resourceToRemove->getNextResource();
753753
}

libs/visor_dns/DnsLayer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef PV_PACKETPP_DNS_LAYER
2-
#define PV_PACKETPP_DNS_LAYER
1+
#pragma once
32

43
#ifdef __GNUC__
54
#pragma GCC diagnostic push
@@ -501,4 +500,3 @@ struct dnshdr {
501500

502501
} // namespace visor
503502

504-
#endif /* PV_PACKETPP_DNS_LAYER */

libs/visor_dns/DnsLayerEnums.h

Lines changed: 74 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,81 @@
1-
#ifndef PV_PACKETPP_DNS_LAYER_ENUMS
2-
#define PV_PACKETPP_DNS_LAYER_ENUMS
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
34

4-
/// @file
5-
6-
/**
7-
* \namespace visor
8-
*/
9-
namespace visor::lib::dns {
10-
/**
11-
* An enum for all possible DNS record types
12-
*/
13-
enum DnsType {
14-
/** IPv4 address record */
15-
DNS_TYPE_A = 1,
16-
/** Name Server record */
17-
DNS_TYPE_NS,
18-
/** Obsolete, replaced by MX */
19-
DNS_TYPE_MD,
20-
/** Obsolete, replaced by MX */
21-
DNS_TYPE_MF,
22-
/** Canonical name record */
23-
DNS_TYPE_CNAME,
24-
/** Start of Authority record */
25-
DNS_TYPE_SOA,
26-
/** mailbox domain name record */
27-
DNS_TYPE_MB,
28-
/** mail group member record */
29-
DNS_TYPE_MG,
30-
/** mail rename domain name record */
31-
DNS_TYPE_MR,
32-
/** NULL record */
33-
DNS_TYPE_NULL_R,
34-
/** well known service description record */
35-
DNS_TYPE_WKS,
36-
/** Pointer record */
37-
DNS_TYPE_PTR,
38-
/** Host information record */
39-
DNS_TYPE_HINFO,
40-
/** mailbox or mail list information record */
41-
DNS_TYPE_MINFO,
42-
/** Mail exchanger record */
43-
DNS_TYPE_MX,
44-
/** Text record */
45-
DNS_TYPE_TXT,
46-
/** Responsible person record */
47-
DNS_TYPE_RP,
48-
/** AFS database record */
49-
DNS_TYPE_AFSDB,
50-
/** DNS X25 resource record */
51-
DNS_TYPE_X25,
52-
/** Integrated Services Digital Network record */
53-
DNS_TYPE_ISDN,
54-
/** Route Through record */
55-
DNS_TYPE_RT,
56-
/** network service access point address record */
57-
DNS_TYPE_NSAP,
58-
/** network service access point address pointer record */
59-
DNS_TYPE_NSAP_PTR,
60-
/** Signature record */
61-
DNS_TYPE_SIG,
62-
/** Key record */
63-
DNS_TYPE_KEY,
64-
/** Mail Mapping Information record */
65-
DNS_TYPE_PX,
66-
/** DNS Geographical Position record */
67-
DNS_TYPE_GPOS,
68-
/** IPv6 address record */
69-
DNS_TYPE_AAAA,
70-
/** Location record */
71-
DNS_TYPE_LOC,
72-
/** Obsolete record */
73-
DNS_TYPE_NXT,
74-
/** DNS Endpoint Identifier record */
75-
DNS_TYPE_EID,
76-
/** DNS Nimrod Locator record */
77-
DNS_TYPE_NIMLOC,
78-
/** Service locator record */
79-
DNS_TYPE_SRV,
80-
/** Asynchronous Transfer Mode address record */
81-
DNS_TYPE_ATMA,
82-
/** Naming Authority Pointer record */
83-
DNS_TYPE_NAPTR,
84-
/** Key eXchanger record */
85-
DNS_TYPE_KX,
86-
/** Certificate record */
87-
DNS_TYPE_CERT,
88-
/** Obsolete, replaced by AAAA type */
89-
DNS_TYPE_A6,
90-
/** Delegation Name record */
91-
DNS_TYPE_DNAM,
92-
/** Kitchen sink record */
93-
DNS_TYPE_SINK,
94-
/** Option record */
95-
DNS_TYPE_OPT,
96-
/** Address Prefix List record */
97-
DNS_TYPE_APL,
98-
/** Delegation signer record */
99-
DNS_TYPE_DS,
100-
/** SSH Public Key Fingerprint record */
101-
DNS_TYPE_SSHFP,
102-
/** IPsec Key record */
103-
DNS_TYPE_IPSECKEY,
104-
/** DNSSEC signature record */
105-
DNS_TYPE_RRSIG,
106-
/** Next-Secure record */
107-
DNS_TYPE_NSEC,
108-
/** DNS Key record */
109-
DNS_TYPE_DNSKEY,
110-
/** DHCP identifier record */
111-
DNS_TYPE_DHCID,
112-
/** NSEC record version 3 */
113-
DNS_TYPE_NSEC3,
114-
/** NSEC3 parameters */
115-
DNS_TYPE_NSEC3PARAM,
116-
/** All cached records */
117-
DNS_TYPE_ALL = 255
118-
};
5+
#pragma once
1196

7+
#include <pcapplusplus/DnsLayerEnums.h>
1208

121-
/**
122-
* An enum for all possible DNS classes
123-
*/
124-
enum DnsClass
125-
{
126-
/** Internet class */
127-
DNS_CLASS_IN = 1,
128-
/** Internet class with QU flag set to True */
129-
DNS_CLASS_IN_QU = 32769,
130-
/** Chaos class */
131-
DNS_CLASS_CH = 3,
132-
/** Hesiod class */
133-
DNS_CLASS_HS = 4,
134-
/** ANY class */
135-
DNS_CLASS_ANY = 255
136-
};
9+
namespace visor::lib::dns {
13710

11+
// DNS record types
12+
using pcpp::DnsType;
13+
using pcpp::DNS_TYPE_A;
14+
using pcpp::DNS_TYPE_NS;
15+
using pcpp::DNS_TYPE_MD;
16+
using pcpp::DNS_TYPE_MF;
17+
using pcpp::DNS_TYPE_CNAME;
18+
using pcpp::DNS_TYPE_SOA;
19+
using pcpp::DNS_TYPE_MB;
20+
using pcpp::DNS_TYPE_MG;
21+
using pcpp::DNS_TYPE_MR;
22+
using pcpp::DNS_TYPE_NULL_R;
23+
using pcpp::DNS_TYPE_WKS;
24+
using pcpp::DNS_TYPE_PTR;
25+
using pcpp::DNS_TYPE_HINFO;
26+
using pcpp::DNS_TYPE_MINFO;
27+
using pcpp::DNS_TYPE_MX;
28+
using pcpp::DNS_TYPE_TXT;
29+
using pcpp::DNS_TYPE_RP;
30+
using pcpp::DNS_TYPE_AFSDB;
31+
using pcpp::DNS_TYPE_X25;
32+
using pcpp::DNS_TYPE_ISDN;
33+
using pcpp::DNS_TYPE_RT;
34+
using pcpp::DNS_TYPE_NSAP;
35+
using pcpp::DNS_TYPE_NSAP_PTR;
36+
using pcpp::DNS_TYPE_SIG;
37+
using pcpp::DNS_TYPE_KEY;
38+
using pcpp::DNS_TYPE_PX;
39+
using pcpp::DNS_TYPE_GPOS;
40+
using pcpp::DNS_TYPE_AAAA;
41+
using pcpp::DNS_TYPE_LOC;
42+
using pcpp::DNS_TYPE_NXT;
43+
using pcpp::DNS_TYPE_EID;
44+
using pcpp::DNS_TYPE_NIMLOC;
45+
using pcpp::DNS_TYPE_SRV;
46+
using pcpp::DNS_TYPE_ATMA;
47+
using pcpp::DNS_TYPE_NAPTR;
48+
using pcpp::DNS_TYPE_KX;
49+
using pcpp::DNS_TYPE_CERT;
50+
using pcpp::DNS_TYPE_A6;
51+
using pcpp::DNS_TYPE_DNAM;
52+
using pcpp::DNS_TYPE_SINK;
53+
using pcpp::DNS_TYPE_OPT;
54+
using pcpp::DNS_TYPE_APL;
55+
using pcpp::DNS_TYPE_DS;
56+
using pcpp::DNS_TYPE_SSHFP;
57+
using pcpp::DNS_TYPE_IPSECKEY;
58+
using pcpp::DNS_TYPE_RRSIG;
59+
using pcpp::DNS_TYPE_NSEC;
60+
using pcpp::DNS_TYPE_DNSKEY;
61+
using pcpp::DNS_TYPE_DHCID;
62+
using pcpp::DNS_TYPE_NSEC3;
63+
using pcpp::DNS_TYPE_NSEC3PARAM;
64+
using pcpp::DNS_TYPE_ALL;
13865

139-
/**
140-
* An enum for representing the 4 types of possible DNS records
141-
*/
142-
enum DnsResourceType
143-
{
144-
/** DNS query record */
145-
DnsQueryType = 0,
146-
/** DNS answer record */
147-
DnsAnswerType = 1,
148-
/** DNS authority record */
149-
DnsAuthorityType = 2,
150-
/** DNS additional record */
151-
DnsAdditionalType = 3
152-
};
66+
// DNS classes
67+
using pcpp::DnsClass;
68+
using pcpp::DNS_CLASS_IN;
69+
using pcpp::DNS_CLASS_IN_QU;
70+
using pcpp::DNS_CLASS_CH;
71+
using pcpp::DNS_CLASS_HS;
72+
using pcpp::DNS_CLASS_ANY;
15373

154-
}
74+
// DNS resource record types
75+
using pcpp::DnsResourceType;
76+
using pcpp::DnsQueryType;
77+
using pcpp::DnsAnswerType;
78+
using pcpp::DnsAuthorityType;
79+
using pcpp::DnsAdditionalType;
15580

156-
#endif // PV_PACKETPP_DNS_LAYER_ENUMS
81+
} // namespace visor::lib::dns

libs/visor_dns/DnsResource.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,15 @@ void DnsResource::setTTL(uint32_t newTTL)
307307

308308
size_t DnsResource::getDataLength() const
309309
{
310-
uint16_t dataLength{0};
311-
memcpy(&dataLength, (getRawData() + m_NameLength + 2 * sizeof(uint16_t) + sizeof(uint32_t)), sizeof(dataLength));
310+
size_t sizeToRead = m_NameLength + 2 * sizeof(uint16_t) + sizeof(uint32_t);
311+
312+
// Heap buffer overflow may occur here, check boundary of m_DnsLayer->m_Data first
313+
// Due to dataLength which is uint16_t, here m_DnsLayer->m_Data must have at least 2 bytes to read
314+
if (m_DnsLayer && m_OffsetInLayer + sizeToRead >= m_DnsLayer->m_DataLen - 1) {
315+
return 0;
316+
}
317+
318+
uint16_t dataLength = *(uint16_t *)(getRawData() + sizeToRead);
312319
return be16toh(dataLength);
313320
}
314321

libs/visor_dns/DnsResource.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef PV_PACKETPP_DNS_RESOURCE
2-
#define PV_PACKETPP_DNS_RESOURCE
1+
#pragma once
32

43
#include "DnsLayer.h"
54
#include "DnsLayerEnums.h"
@@ -47,7 +46,7 @@ class IDnsResource
4746
void encodeName(const std::string& decodedName, char* result, size_t& resultLen);
4847

4948
IDnsResource* getNextResource() const { return m_NextResource; }
50-
void setNexResource(IDnsResource* next) { m_NextResource = next; }
49+
void setNextResource(IDnsResource* next) { m_NextResource = next; }
5150

5251
uint8_t* getRawData() const;
5352

@@ -140,7 +139,7 @@ class IDnsResource
140139
private:
141140
DnsQuery(DnsLayer* dnsLayer, size_t offsetInLayer) : IDnsResource(dnsLayer, offsetInLayer) {}
142141

143-
DnsQuery(uint8_t* emptyRawData) : IDnsResource(emptyRawData) {}
142+
explicit DnsQuery(uint8_t* emptyRawData) : IDnsResource(emptyRawData) {}
144143

145144
public:
146145
virtual ~DnsQuery() {}
@@ -242,4 +241,3 @@ class IDnsResource
242241

243242
}
244243

245-
#endif // PV_PACKETPP_DNS_RESOURCE

libs/visor_dns/DnsResourceData.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ void IDnsResourceData::encodeName(const std::string &decodedName, char *result,
4040

4141
StringDnsResourceData::StringDnsResourceData(const uint8_t *dataPtr, size_t dataLen, IDnsResource *dnsResource)
4242
{
43-
char tempResult[256];
44-
decodeName((const char *)dataPtr, tempResult, dnsResource);
45-
m_Data = tempResult;
43+
if (dataPtr && dataLen > 0) {
44+
char tempResult[256];
45+
decodeName((const char *)dataPtr, tempResult, dnsResource);
46+
m_Data = tempResult;
47+
} else
48+
PCPP_LOG_ERROR("Cannot decode name, dataPtr is NULL or length is 0");
4649
}
4750

4851
bool StringDnsResourceData::toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
@@ -98,11 +101,14 @@ bool IPv6DnsResourceData::toByteArr(uint8_t *arr, size_t &arrLength, IDnsResourc
98101

99102
MxDnsResourceData::MxDnsResourceData(uint8_t *dataPtr, size_t dataLen, IDnsResource *dnsResource)
100103
{
101-
uint16_t preference = be16toh(*(uint16_t *)dataPtr);
102-
char tempMX[256];
103-
decodeName((const char *)(dataPtr + sizeof(preference)), tempMX, dnsResource);
104-
m_Data.preference = preference;
105-
m_Data.mailExchange = tempMX;
104+
if (dataPtr && dataLen > 0) {
105+
uint16_t preference = be16toh(*(uint16_t *)dataPtr);
106+
char tempMX[256];
107+
decodeName((const char *)(dataPtr + sizeof(preference)), tempMX, dnsResource);
108+
m_Data.preference = preference;
109+
m_Data.mailExchange = tempMX;
110+
} else
111+
PCPP_LOG_ERROR("Cannot decode name, dataPtr is NULL or length is 0");
106112
}
107113

108114
MxDnsResourceData::MxDnsResourceData(const uint16_t &preference, const std::string &mailExchange)
@@ -119,7 +125,7 @@ bool MxDnsResourceData::operator==(const MxDnsResourceData &other) const
119125
void MxDnsResourceData::setMxData(uint16_t preference, std::string mailExchange)
120126
{
121127
m_Data.preference = preference;
122-
m_Data.mailExchange = mailExchange;
128+
m_Data.mailExchange = std::move(mailExchange);
123129
}
124130

125131
std::string MxDnsResourceData::toString() const

0 commit comments

Comments
 (0)