-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from esp8266/esp8266
Esp8266
- Loading branch information
Showing
34 changed files
with
474 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
hardware/esp8266com/esp8266/libraries/DNSServer/examples/DNSServer/DNSServer.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <DNSServer.h> | ||
|
||
const byte DNS_PORT = 53; | ||
IPAddress apIP(192, 168, 1, 1); | ||
DNSServer dnsServer; | ||
|
||
void setup() { | ||
WiFi.mode(WIFI_AP); | ||
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); | ||
WiFi.softAP("DNSServer example"); | ||
|
||
// modify TTL associated with the domain name (in seconds) | ||
// default is 60 seconds | ||
dnsServer.setTTL(300); | ||
// set which return code will be used for all other domains (e.g. sending | ||
// ServerFailure instead of NonExistentDomain will reduce number of queries | ||
// sent by clients) | ||
// default is DNSReplyCode::NonExistentDomain | ||
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); | ||
|
||
//start DNS server for a specific domain name | ||
dnsServer.start(DNS_PORT, "www.example.com", apIP); | ||
} | ||
|
||
void loop() { | ||
dnsServer.processNextRequest(); | ||
} |
9 changes: 9 additions & 0 deletions
9
hardware/esp8266com/esp8266/libraries/DNSServer/library.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=DNSServer | ||
version=1.0.0 | ||
author=Kristijan Novoselić | ||
maintainer=Kristijan Novoselić, <kristijan.novoselic@gmail.com> | ||
sentence=A simple DNS server for ESP8266. | ||
paragraph=This library implements a simple DNS server. | ||
category=Communication | ||
url= | ||
architectures=esp8266 |
133 changes: 133 additions & 0 deletions
133
hardware/esp8266com/esp8266/libraries/DNSServer/src/DNSServer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include "DNSServer.h" | ||
#include <lwip/def.h> | ||
#include <Arduino.h> | ||
|
||
DNSServer::DNSServer() | ||
{ | ||
_ttl = htonl(60); | ||
_errorReplyCode = DNSReplyCode::NonExistentDomain; | ||
} | ||
|
||
bool DNSServer::start(const uint16_t &port, const String &domainName, | ||
const IPAddress &resolvedIP) | ||
{ | ||
_port = port; | ||
_domainName = domainName; | ||
_resolvedIP[0] = resolvedIP[0]; | ||
_resolvedIP[1] = resolvedIP[1]; | ||
_resolvedIP[2] = resolvedIP[2]; | ||
_resolvedIP[3] = resolvedIP[3]; | ||
downcaseAndRemoveWwwPrefix(_domainName); | ||
return _udp.begin(_port) == 1; | ||
} | ||
|
||
void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode) | ||
{ | ||
_errorReplyCode = replyCode; | ||
} | ||
|
||
void DNSServer::setTTL(const uint32_t &ttl) | ||
{ | ||
_ttl = htonl(ttl); | ||
} | ||
|
||
void DNSServer::stop() | ||
{ | ||
_udp.stop(); | ||
} | ||
|
||
void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) | ||
{ | ||
domainName.toLowerCase(); | ||
domainName.replace("www.", ""); | ||
} | ||
|
||
void DNSServer::processNextRequest() | ||
{ | ||
_currentPacketSize = _udp.parsePacket(); | ||
if (_currentPacketSize) | ||
{ | ||
_buffer = (unsigned char*)malloc(_currentPacketSize * sizeof(char)); | ||
_udp.read(_buffer, _currentPacketSize); | ||
_dnsHeader = (DNSHeader*) _buffer; | ||
|
||
if (_dnsHeader->QR == DNS_QR_QUERY && | ||
_dnsHeader->OPCode == DNS_OPCODE_QUERY && | ||
requestIncludesOnlyOneQuestion() && | ||
getDomainNameWithoutWwwPrefix() == _domainName) | ||
{ | ||
replyWithIP(); | ||
} | ||
else if (_dnsHeader->QR == DNS_QR_QUERY) | ||
{ | ||
replyWithCustomCode(); | ||
} | ||
|
||
free(_buffer); | ||
} | ||
} | ||
|
||
bool DNSServer::requestIncludesOnlyOneQuestion() | ||
{ | ||
return ntohs(_dnsHeader->QDCount) == 1 && | ||
_dnsHeader->ANCount == 0 && | ||
_dnsHeader->NSCount == 0 && | ||
_dnsHeader->ARCount == 0; | ||
} | ||
|
||
String DNSServer::getDomainNameWithoutWwwPrefix() | ||
{ | ||
String parsedDomainName = ""; | ||
unsigned char *start = _buffer + 12; | ||
if (*start == 0) | ||
{ | ||
return parsedDomainName; | ||
} | ||
int pos = 0; | ||
while(true) | ||
{ | ||
unsigned char labelLength = *(start + pos); | ||
for(int i = 0; i < labelLength; i++) | ||
{ | ||
pos++; | ||
parsedDomainName += (char)*(start + pos); | ||
} | ||
pos++; | ||
if (*(start + pos) == 0) | ||
{ | ||
downcaseAndRemoveWwwPrefix(parsedDomainName); | ||
return parsedDomainName; | ||
} | ||
else | ||
{ | ||
parsedDomainName += "."; | ||
} | ||
} | ||
} | ||
|
||
void DNSServer::replyWithIP() | ||
{ | ||
_dnsHeader->QR = DNS_QR_RESPONSE; | ||
_dnsHeader->ANCount = _dnsHeader->QDCount; | ||
_dnsHeader->QDCount = 0; | ||
|
||
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort()); | ||
_udp.write(_buffer, _currentPacketSize); | ||
_udp.write((unsigned char*)&_ttl, 4); | ||
// Length of RData is 4 bytes (because, in this case, RData is IPv4) | ||
_udp.write((uint8_t)0); | ||
_udp.write((uint8_t)4); | ||
_udp.write(_resolvedIP, sizeof(_resolvedIP)); | ||
_udp.endPacket(); | ||
} | ||
|
||
void DNSServer::replyWithCustomCode() | ||
{ | ||
_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(); | ||
} |
Oops, something went wrong.