Skip to content
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

Deprecated DNSServer. Use DnsServer instead. #1915

Merged
merged 2 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
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
101 changes: 2 additions & 99 deletions Sming/Core/Network/DNSServer.h
Original file line number Diff line number Diff line change
@@ -1,100 +1,3 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* DnsServer.h
*
* File Author: https://github.com/patrickjahns
*
* The code is a port of the following projects
* https://github.com/israellot/esp-ginx/tree/master/app/dns
* https://github.com/esp8266/Arduino/tree/master/libraries/DNSServer
* Created on March 4, 2016
*
****/
#include "DnsServer.h"

/** @defgroup dnsserver DNS server
* @brief Provides DNS server
* @ingroup udp
* @{
*/
#pragma once

#include "UdpConnection.h"
#include "WString.h"

#define DNS_QR_QUERY 0
#define DNS_QR_RESPONSE 1
#define DNS_OPCODE_QUERY 0

enum class DNSReplyCode {
NoError = 0,
FormError = 1,
ServerFailure = 2,
NonExistentDomain = 3,
NotImplemented = 4,
Refused = 5,
YXDomain = 6,
YXRRSet = 7,
NXRRSet = 8
};

struct DNSHeader {
uint16_t ID; // identification number
char RD : 1; // recursion desired
char TC : 1; // truncated message
char AA : 1; // authoritive answer
char OPCode : 4; // message_type
char QR : 1; // query/response flag
char RCode : 4; // response code
char Z : 3; // its z! reserved
char RA : 1; // recursion available
uint16_t QDCount; // number of question entries
uint16_t ANCount; // number of answer entries
uint16_t NSCount; // number of authority entries
uint16_t ARCount; // number of resource entries
};

class DNSServer : public UdpConnection
{
public:
DNSServer()
{
}

void setErrorReplyCode(DNSReplyCode replyCode)
{
errorReplyCode = replyCode;
}

void setTTL(uint32_t ttl)
{
this->ttl = ttl;
}

// Returns true if successful, false if there are no sockets available
bool start(uint16_t port, const String& domainName, const IpAddress& resolvedIP);

// stops the DNS server
void stop();

protected:
void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;

private:
uint16_t port = 0;
String domainName;
ip_addr resolvedIP;
char* buffer = nullptr;
DNSHeader* dnsHeader = nullptr;
uint32_t ttl = 60;
DNSReplyCode errorReplyCode = DNSReplyCode::NonExistentDomain;

static void downcaseAndRemoveWwwPrefix(String& domainName);
String getDomainNameWithoutWwwPrefix();
bool requestIncludesOnlyOneQuestion();
};

/** @} */
#warning "Please include `DnsServer.h` instead of `DNSServer.h`"
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*
****/

#include "DNSServer.h"
#include "DnsServer.h"
#include "UdpConnection.h"
#include "WString.h"

bool DNSServer::start(uint16_t port, const String& domainName, const IpAddress& resolvedIP)
bool DnsServer::start(uint16_t port, const String& domainName, const IpAddress& resolvedIP)
{
this->port = port;
buffer = nullptr;
Expand All @@ -29,26 +29,26 @@ bool DNSServer::start(uint16_t port, const String& domainName, const IpAddress&
return listen(this->port) == 1;
}

void DNSServer::stop()
void DnsServer::stop()
{
close();
delete[] buffer;
buffer = nullptr;
}

void DNSServer::downcaseAndRemoveWwwPrefix(String& domainName)
void DnsServer::downcaseAndRemoveWwwPrefix(String& domainName)
{
domainName.toLowerCase();
domainName.replace(F("www."), String::empty);
}

bool DNSServer::requestIncludesOnlyOneQuestion()
bool DnsServer::requestIncludesOnlyOneQuestion()
{
return ntohs(dnsHeader->QDCount) == 1 && dnsHeader->ANCount == 0 && dnsHeader->NSCount == 0 &&
dnsHeader->ARCount == 0;
}

void DNSServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
void DnsServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
{
delete[] buffer;
buffer = new char[buf->tot_len];
Expand All @@ -59,7 +59,7 @@ void DNSServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
pbuf_copy_partial(buf, buffer, buf->tot_len, 0);
debug_d("DNS REQ for %s from %s:%d", getDomainNameWithoutWwwPrefix().c_str(), remoteIP.toString().c_str(),
remotePort);
dnsHeader = reinterpret_cast<DNSHeader*>(buffer);
dnsHeader = reinterpret_cast<DnsHeader*>(buffer);
if(dnsHeader->QR == DNS_QR_QUERY && dnsHeader->OPCode == DNS_OPCODE_QUERY && requestIncludesOnlyOneQuestion() &&
(domainName == "*" || getDomainNameWithoutWwwPrefix() == domainName)) {
char response[buf->tot_len + 16];
Expand Down Expand Up @@ -101,7 +101,7 @@ void DNSServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
dnsHeader->QR = DNS_QR_RESPONSE;
dnsHeader->RCode = char(errorReplyCode);
dnsHeader->QDCount = 0;
sendTo(remoteIP, remotePort, buffer, sizeof(DNSHeader));
sendTo(remoteIP, remotePort, buffer, sizeof(DnsHeader));
}

delete[] buffer;
Expand All @@ -110,7 +110,7 @@ void DNSServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
UdpConnection::onReceive(buf, remoteIP, remotePort);
}

String DNSServer::getDomainNameWithoutWwwPrefix()
String DnsServer::getDomainNameWithoutWwwPrefix()
{
String parsedDomainName;
if(buffer == nullptr) {
Expand Down
103 changes: 103 additions & 0 deletions Sming/Core/Network/DnsServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* DnsServer.h
*
* File Author: https://github.com/patrickjahns
*
* The code is a port of the following projects
* https://github.com/israellot/esp-ginx/tree/master/app/dns
* https://github.com/esp8266/Arduino/tree/master/libraries/DNSServer
* Created on March 4, 2016
*
****/

/** @defgroup dnsserver DNS server
* @brief Provides DNS server
* @ingroup udp
* @{
*/
#pragma once

#include "UdpConnection.h"
#include "WString.h"

#define DNS_QR_QUERY 0
#define DNS_QR_RESPONSE 1
#define DNS_OPCODE_QUERY 0

enum class DnsReplyCode {
NoError = 0,
FormError = 1,
ServerFailure = 2,
NonExistentDomain = 3,
NotImplemented = 4,
Refused = 5,
YXDomain = 6,
YXRRSet = 7,
NXRRSet = 8
};

struct DnsHeader {
uint16_t ID; // identification number
char RD : 1; // recursion desired
char TC : 1; // truncated message
char AA : 1; // authoritive answer
char OPCode : 4; // message_type
char QR : 1; // query/response flag
char RCode : 4; // response code
char Z : 3; // its z! reserved
char RA : 1; // recursion available
uint16_t QDCount; // number of question entries
uint16_t ANCount; // number of answer entries
uint16_t NSCount; // number of authority entries
uint16_t ARCount; // number of resource entries
};

class DnsServer : public UdpConnection
{
public:
DnsServer()
{
}

void setErrorReplyCode(DnsReplyCode replyCode)
{
errorReplyCode = replyCode;
}

void setTTL(uint32_t ttl)
{
this->ttl = ttl;
}

// Returns true if successful, false if there are no sockets available
bool start(uint16_t port, const String& domainName, const IpAddress& resolvedIP);

// stops the DNS server
void stop();

protected:
void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;

private:
uint16_t port = 0;
String domainName;
ip_addr resolvedIP;
char* buffer = nullptr;
DnsHeader* dnsHeader = nullptr;
uint32_t ttl = 60;
DnsReplyCode errorReplyCode = DnsReplyCode::NonExistentDomain;

static void downcaseAndRemoveWwwPrefix(String& domainName);
String getDomainNameWithoutWwwPrefix();
bool requestIncludesOnlyOneQuestion();
};

/** @deprecated Use `DnsServer` */
typedef DnsServer DNSServer SMING_DEPRECATED;

/** @} */
2 changes: 1 addition & 1 deletion Sming/Core/SmingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "Platform/AccessPoint.h"
#include "Platform/WDT.h"

#include "Network/DNSServer.h"
#include "Network/DnsServer.h"
#include "Network/HttpClient.h"
#include "Network/MqttClient.h"
#include "Network/NtpClient.h"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DNSCaptivePortal</name>
<name>DnsCaptivePortal</name>
<comment></comment>
<projects>
<project>SmingFramework</project>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SmingCore.h>

DNSServer dnsServer;
DnsServer dnsServer;
HttpServer server;

#define DNS_PORT 53
Expand Down