@@ -7,8 +7,8 @@ DNSServer::DNSServer()
7
7
{
8
8
_ttl = htonl (DNS_DEFAULT_TTL);
9
9
_errorReplyCode = DNSReplyCode::NonExistentDomain;
10
- _dnsHeader = NULL ;
11
- _buffer = NULL ;
10
+ _dnsHeader = NULL ;
11
+ _buffer = NULL ;
12
12
_currentPacketSize = 0 ;
13
13
_port = 0 ;
14
14
}
@@ -55,11 +55,17 @@ void DNSServer::processNextRequest()
55
55
_currentPacketSize = _udp.parsePacket ();
56
56
if (_currentPacketSize)
57
57
{
58
- if (_buffer != NULL ) free (_buffer);
58
+ // Allocate buffer for the DNS query
59
+ if (_buffer != NULL )
60
+ free (_buffer);
59
61
_buffer = (unsigned char *)malloc (_currentPacketSize * sizeof (char ));
60
- if (_buffer == NULL ) return ;
62
+ if (_buffer == NULL )
63
+ return ;
64
+
65
+ // Put the packet received in the buffer and get DNS header (beginning of message),
66
+ // type and class (last 4 bytes of the message)
61
67
_udp.read (_buffer, _currentPacketSize);
62
- _dnsHeader = (DNSHeader*) _buffer;
68
+ _dnsHeader = (DNSHeader*) _buffer;
63
69
_type = (uint16_t ) ( (_buffer[_currentPacketSize - 4 ] << 8 ) + _buffer[_currentPacketSize - 3 ] ) ;
64
70
_class = (uint16_t ) ( (_buffer[_currentPacketSize - 2 ] << 8 ) + _buffer[_currentPacketSize - 1 ] ) ;
65
71
@@ -89,15 +95,21 @@ bool DNSServer::requestIncludesOnlyOneQuestion()
89
95
_dnsHeader->ARCount == 0 ;
90
96
}
91
97
98
+
92
99
String DNSServer::getDomainNameWithoutWwwPrefix ()
93
100
{
101
+ // Error checking : if the buffer containing the DNS request is a null pointer, return an empty domain
94
102
String parsedDomainName = " " ;
95
- if (_buffer == NULL ) return parsedDomainName;
96
- unsigned char *start = _buffer + 12 ;
103
+ if (_buffer == NULL )
104
+ return parsedDomainName;
105
+
106
+ // Set the start of the domain just after the header (12 bytes). If equal to null character, return an empty domain
107
+ unsigned char *start = _buffer + DNS_OFFSET_DOMAIN_NAME;
97
108
if (*start == 0 )
98
109
{
99
110
return parsedDomainName;
100
111
}
112
+
101
113
int pos = 0 ;
102
114
while (true )
103
115
{
0 commit comments