@@ -67,15 +67,15 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt)
67
67
// Each label contains a byte to describe its length and the label itself. The list of
68
68
// labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com"
69
69
*/
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
73
73
dnsQuestion.QNameLength = enoflbls - (char *)pkt.data () - DNS_HEADER_SIZE;
74
74
/*
75
75
check if we aint going out of pkt bounds
76
76
proper dns req should have label terminator at least 4 bytes before end of packet
77
77
*/
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
79
79
80
80
// Copy the QType and QClass
81
81
memcpy ( &dnsQuestion.QType , enoflbls, sizeof (dnsQuestion.QType ) );
@@ -108,35 +108,18 @@ bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader)
108
108
109
109
String DNSServer::getDomainNameWithoutWwwPrefix (const char * start, size_t len)
110
110
{
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
117
112
118
- parsedDomainName.reserve (len);
119
113
int pos = 0 ;
120
- while (true )
114
+ while (pos<len )
121
115
{
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;
139
119
}
120
+ parsedDomainName.remove (0 ,1 ); // remove first "." char
121
+ downcaseAndRemoveWwwPrefix (parsedDomainName);
122
+ return parsedDomainName;
140
123
}
141
124
142
125
void DNSServer::replyWithIP (AsyncUDPPacket& req, DNSHeader& dnsHeader, DNSQuestion& dnsQuestion)
0 commit comments