Skip to content

Commit 63541cf

Browse files
author
Laurent Louf
committed
A bit of aesthetics for the DNS server.
1 parent 3da6540 commit 63541cf

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

libraries/DNSServer/src/DNSServer.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ DNSServer::DNSServer()
77
{
88
_ttl = htonl(DNS_DEFAULT_TTL);
99
_errorReplyCode = DNSReplyCode::NonExistentDomain;
10-
_dnsHeader = NULL;
11-
_buffer = NULL;
10+
_dnsHeader = NULL;
11+
_buffer = NULL;
1212
_currentPacketSize = 0;
1313
_port = 0;
1414
}
@@ -55,11 +55,17 @@ void DNSServer::processNextRequest()
5555
_currentPacketSize = _udp.parsePacket();
5656
if (_currentPacketSize)
5757
{
58-
if (_buffer != NULL) free(_buffer);
58+
// Allocate buffer for the DNS query
59+
if (_buffer != NULL)
60+
free(_buffer);
5961
_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)
6167
_udp.read(_buffer, _currentPacketSize);
62-
_dnsHeader = (DNSHeader*) _buffer;
68+
_dnsHeader = (DNSHeader*) _buffer;
6369
_type = (uint16_t) ( (_buffer[_currentPacketSize - 4] << 8) + _buffer[_currentPacketSize - 3] ) ;
6470
_class = (uint16_t) ( (_buffer[_currentPacketSize - 2] << 8) + _buffer[_currentPacketSize - 1] ) ;
6571

@@ -89,15 +95,21 @@ bool DNSServer::requestIncludesOnlyOneQuestion()
8995
_dnsHeader->ARCount == 0;
9096
}
9197

98+
9299
String DNSServer::getDomainNameWithoutWwwPrefix()
93100
{
101+
// Error checking : if the buffer containing the DNS request is a null pointer, return an empty domain
94102
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;
97108
if (*start == 0)
98109
{
99110
return parsedDomainName;
100111
}
112+
101113
int pos = 0;
102114
while(true)
103115
{

libraries/DNSServer/src/DNSServer.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
enum class DNSReplyCode
1212
{
13-
NoError = 0,
13+
NoError = 0,
1414
FormError = 1,
15-
ServerFailure = 2,
15+
ServerFailure = 2,
1616
NonExistentDomain = 3,
17-
NotImplemented = 4,
18-
Refused = 5,
19-
YXDomain = 6,
20-
YXRRSet = 7,
21-
NXRRSet = 8
17+
NotImplemented = 4,
18+
Refused = 5,
19+
YXDomain = 6,
20+
YXRRSet = 7,
21+
NXRRSet = 8
2222
};
2323

2424
enum DNSType

0 commit comments

Comments
 (0)