Skip to content

Commit 545a946

Browse files
committed
DNSServer: labels min length checks, simplified labels parser
1 parent e50dfef commit 545a946

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

libraries/DNSServer/src/DNSServer.cpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt)
6767
// Each label contains a byte to describe its length and the label itself. The list of
6868
// labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com"
6969
*/
70-
char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker
71-
++enoflbls; // include null terminator
72-
dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request
70+
const char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker
71+
++enoflbls; // advance after null terminator
72+
dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request
7373
dnsQuestion.QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE;
7474
/*
7575
check if we aint going out of pkt bounds
7676
proper dns req should have label terminator at least 4 bytes before end of packet
7777
*/
78-
if (dnsQuestion.QNameLength > currentPacketSize - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet
78+
if (dnsQuestion.QNameLength < 3 || dnsQuestion.QNameLength > currentPacketSize - DNS_HEADER_SIZE - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet
7979

8080
// Copy the QType and QClass
8181
memcpy( &dnsQuestion.QType, enoflbls, sizeof(dnsQuestion.QType) );
@@ -108,35 +108,18 @@ bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader)
108108

109109
String DNSServer::getDomainNameWithoutWwwPrefix(const char* start, size_t len)
110110
{
111-
String parsedDomainName("");
112-
113-
if (*start == 0)
114-
{
115-
return parsedDomainName;
116-
}
111+
String parsedDomainName(start, --len); // exclude trailing null byte from labels length, String constructor will add it anyway
117112

118-
parsedDomainName.reserve(len);
119113
int pos = 0;
120-
while(true)
114+
while(pos<len)
121115
{
122-
uint8_t labelLength = *(start + pos);
123-
124-
for(uint8_t i = 0; i < labelLength; i++)
125-
{
126-
pos++;
127-
parsedDomainName += (char)*(start + pos);
128-
}
129-
pos++;
130-
if (*(start + pos) == 0)
131-
{
132-
downcaseAndRemoveWwwPrefix(parsedDomainName);
133-
return parsedDomainName;
134-
}
135-
else
136-
{
137-
parsedDomainName += ".";
138-
}
116+
parsedDomainName.setCharAt(pos, 0x2e); // replace len byte with dot char "."
117+
pos += *(start + pos);
118+
++pos;
139119
}
120+
parsedDomainName.remove(0,1); // remove first "." char
121+
downcaseAndRemoveWwwPrefix(parsedDomainName);
122+
return parsedDomainName;
140123
}
141124

142125
void DNSServer::replyWithIP(AsyncUDPPacket& req, DNSHeader& dnsHeader, DNSQuestion& dnsQuestion)

libraries/DNSServer/src/DNSServer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef DNSServer_h
2-
#define DNSServer_h
1+
#pragma once
32
#include <AsyncUDP.h>
43

54
#define DNS_QR_QUERY 0
@@ -113,4 +112,3 @@ class DNSServer
113112
inline void replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader);
114113
void _handleUDP(AsyncUDPPacket& pkt);
115114
};
116-
#endif

0 commit comments

Comments
 (0)