Skip to content

Add _buffer initialization and some sanity check #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bool DNSServer::start(const uint16_t &port, const String &domainName,
const IPAddress &resolvedIP)
{
_port = port;
_buffer = NULL;
_domainName = domainName;
_resolvedIP[0] = resolvedIP[0];
_resolvedIP[1] = resolvedIP[1];
Expand All @@ -35,6 +36,8 @@ void DNSServer::setTTL(const uint32_t &ttl)
void DNSServer::stop()
{
_udp.stop();
free(_buffer);
_buffer = NULL;
}

void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName)
Expand All @@ -48,7 +51,9 @@ void DNSServer::processNextRequest()
_currentPacketSize = _udp.parsePacket();
if (_currentPacketSize)
{
if (_buffer != NULL) free(_buffer);
_buffer = (unsigned char*)malloc(_currentPacketSize * sizeof(char));
if (_buffer == NULL) return;
_udp.read(_buffer, _currentPacketSize);
_dnsHeader = (DNSHeader*) _buffer;

Expand All @@ -66,6 +71,7 @@ void DNSServer::processNextRequest()
}

free(_buffer);
_buffer = NULL;
}
}

Expand All @@ -80,6 +86,7 @@ bool DNSServer::requestIncludesOnlyOneQuestion()
String DNSServer::getDomainNameWithoutWwwPrefix()
{
String parsedDomainName = "";
if (_buffer == NULL) return parsedDomainName;
unsigned char *start = _buffer + 12;
if (*start == 0)
{
Expand Down Expand Up @@ -109,6 +116,7 @@ String DNSServer::getDomainNameWithoutWwwPrefix()

void DNSServer::replyWithIP()
{
if (_buffer == NULL) return;
_dnsHeader->QR = DNS_QR_RESPONSE;
_dnsHeader->ANCount = _dnsHeader->QDCount;
_dnsHeader->QDCount = _dnsHeader->QDCount;
Expand Down Expand Up @@ -152,11 +160,12 @@ void DNSServer::replyWithIP()

void DNSServer::replyWithCustomCode()
{
if (_buffer == NULL) return;
_dnsHeader->QR = DNS_QR_RESPONSE;
_dnsHeader->RCode = (unsigned char)_errorReplyCode;
_dnsHeader->QDCount = 0;

_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
_udp.write(_buffer, sizeof(DNSHeader));
_udp.endPacket();
}
}