From 9f089648b390933bd9bb5ef460c0328b205c4c07 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Tue, 15 Nov 2022 19:12:45 +0300 Subject: [PATCH 1/7] DNSServer: switch to AsyncUDP instead of WiFiUDP AsyncUDP offers event driven approch for handling udp dns req's WiFiUDP hooks to loop() for packet processing and making useless malloc's each run --- libraries/DNSServer/src/DNSServer.cpp | 63 +++++++++++++-------------- libraries/DNSServer/src/DNSServer.h | 11 ++--- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 3aa7af1631a..67ae6d96d07 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -47,7 +47,8 @@ bool DNSServer::start(const uint16_t &port, const String &domainName, _resolvedIP[2] = resolvedIP[2]; _resolvedIP[3] = resolvedIP[3]; downcaseAndRemoveWwwPrefix(_domainName); - return _udp.begin(_port) == 1; + _udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); }); + return _udp.listen(_port); } void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode) @@ -62,7 +63,7 @@ void DNSServer::setTTL(const uint32_t &ttl) void DNSServer::stop() { - _udp.stop(); + _udp.close(); free(_buffer); _buffer = NULL; } @@ -73,11 +74,11 @@ void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) domainName.replace("www.", ""); } -void DNSServer::processNextRequest() +void DNSServer::_handleUDP(AsyncUDPPacket& pkt) { - _currentPacketSize = _udp.parsePacket(); - if (_currentPacketSize) - { + _currentPacketSize = pkt.length(); + if (!_currentPacketSize) return; + // Allocate buffer for the DNS query if (_buffer != NULL) free(_buffer); @@ -87,7 +88,7 @@ void DNSServer::processNextRequest() // Put the packet received in the buffer and get DNS header (beginning of message) // and the question - _udp.read(_buffer, _currentPacketSize); + pkt.read(_buffer, _currentPacketSize); memcpy( _dnsHeader, _buffer, DNS_HEADER_SIZE ) ; if ( requestIncludesOnlyOneQuestion() ) { @@ -116,16 +117,16 @@ void DNSServer::processNextRequest() (_domainName == "*" || getDomainNameWithoutWwwPrefix() == _domainName) ) { - replyWithIP(); + replyWithIP(pkt); } else if (_dnsHeader->QR == DNS_QR_QUERY) { - replyWithCustomCode(); + replyWithCustomCode(pkt); } free(_buffer); _buffer = NULL; - } + } bool DNSServer::requestIncludesOnlyOneQuestion() @@ -140,7 +141,7 @@ bool DNSServer::requestIncludesOnlyOneQuestion() String DNSServer::getDomainNameWithoutWwwPrefix() { // Error checking : if the buffer containing the DNS request is a null pointer, return an empty domain - String parsedDomainName = ""; + String parsedDomainName(""); if (_buffer == NULL) return parsedDomainName; @@ -173,39 +174,38 @@ String DNSServer::getDomainNameWithoutWwwPrefix() } } -void DNSServer::replyWithIP() +void DNSServer::replyWithIP(AsyncUDPPacket& req) { - if (_buffer == NULL) return; - - _udp.beginPacket(_udp.remoteIP(), _udp.remotePort()); + AsyncUDPMessage rpl; // Change the type of message to a response and set the number of answers equal to // the number of questions in the header _dnsHeader->QR = DNS_QR_RESPONSE; _dnsHeader->ANCount = _dnsHeader->QDCount; - _udp.write( (unsigned char*) _dnsHeader, DNS_HEADER_SIZE ) ; + rpl.write( (unsigned char*) _dnsHeader, DNS_HEADER_SIZE ) ; // Write the question - _udp.write(_dnsQuestion->QName, _dnsQuestion->QNameLength) ; - _udp.write( (unsigned char*) &_dnsQuestion->QType, 2 ) ; - _udp.write( (unsigned char*) &_dnsQuestion->QClass, 2 ) ; + rpl.write(_dnsQuestion->QName, _dnsQuestion->QNameLength) ; + rpl.write( (unsigned char*) &_dnsQuestion->QType, 2 ) ; + rpl.write( (unsigned char*) &_dnsQuestion->QClass, 2 ) ; // Write the answer // Use DNS name compression : instead of repeating the name in this RNAME occurence, // set the two MSB of the byte corresponding normally to the length to 1. The following // 14 bits must be used to specify the offset of the domain name in the message // (<255 here so the first byte has the 6 LSB at 0) - _udp.write((uint8_t) 0xC0); - _udp.write((uint8_t) DNS_OFFSET_DOMAIN_NAME); + rpl.write((uint8_t) 0xC0); + rpl.write((uint8_t) DNS_OFFSET_DOMAIN_NAME); // DNS type A : host address, DNS class IN for INternet, returning an IPv4 address uint16_t answerType = htons(DNS_TYPE_A), answerClass = htons(DNS_CLASS_IN), answerIPv4 = htons(DNS_RDLENGTH_IPV4) ; - _udp.write((unsigned char*) &answerType, 2 ); - _udp.write((unsigned char*) &answerClass, 2 ); - _udp.write((unsigned char*) &_ttl, 4); // DNS Time To Live - _udp.write((unsigned char*) &answerIPv4, 2 ); - _udp.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return - _udp.endPacket(); + rpl.write((unsigned char*) &answerType, 2 ); + rpl.write((unsigned char*) &answerClass, 2 ); + rpl.write((unsigned char*) &_ttl, 4); // DNS Time To Live + rpl.write((unsigned char*) &answerIPv4, 2 ); + rpl.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return + + _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); #ifdef DEBUG_ESP_DNS DEBUG_OUTPUT.printf("DNS responds: %s for %s\n", @@ -213,14 +213,13 @@ void DNSServer::replyWithIP() #endif } -void DNSServer::replyWithCustomCode() +void DNSServer::replyWithCustomCode(AsyncUDPPacket& req) { - 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(); + AsyncUDPMessage rpl(sizeof(DNSHeader)); + rpl.write((unsigned char*)_dnsHeader, sizeof(DNSHeader)); + _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); } diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index 1250f5ce960..f1e01b803c1 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -1,6 +1,6 @@ #ifndef DNSServer_h #define DNSServer_h -#include +#include #define DNS_QR_QUERY 0 #define DNS_QR_RESPONSE 1 @@ -77,7 +77,7 @@ class DNSServer public: DNSServer(); ~DNSServer(); - void processNextRequest(); + void processNextRequest(){}; // stub, left for compatibility with an old version void setErrorReplyCode(const DNSReplyCode &replyCode); void setTTL(const uint32_t &ttl); @@ -89,7 +89,7 @@ class DNSServer void stop(); private: - WiFiUDP _udp; + AsyncUDP _udp; uint16_t _port; String _domainName; unsigned char _resolvedIP[4]; @@ -104,7 +104,8 @@ class DNSServer void downcaseAndRemoveWwwPrefix(String &domainName); String getDomainNameWithoutWwwPrefix(); bool requestIncludesOnlyOneQuestion(); - void replyWithIP(); - void replyWithCustomCode(); + void replyWithIP(AsyncUDPPacket& req); + void replyWithCustomCode(AsyncUDPPacket& req); + void _handleUDP(AsyncUDPPacket& pkt); }; #endif From d8b8993b388744379a93331d3eb5e4a999710757 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Wed, 16 Nov 2022 22:57:36 +0300 Subject: [PATCH 2/7] DNSServer code refactoring get rid of intermediate mem buffers and extra data copies, most of the data could be referenced or copied from the source packet - removed _buffer member - replaced DNSQuestion.QName from uint8_t[] to char* added sanity checks for mem bounds optimize label/packet length calculations other code cleanup --- libraries/DNSServer/src/DNSServer.cpp | 100 ++++++++++++-------------- libraries/DNSServer/src/DNSServer.h | 21 ++++-- 2 files changed, 59 insertions(+), 62 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 67ae6d96d07..b39870f8dff 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -9,30 +9,21 @@ #define DEBUG_OUTPUT Serial #endif -DNSServer::DNSServer() +DNSServer::DNSServer() : _port(0), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain) { - _ttl = htonl(DNS_DEFAULT_TTL); - _errorReplyCode = DNSReplyCode::NonExistentDomain; - _dnsHeader = (DNSHeader*) malloc( sizeof(DNSHeader) ) ; - _dnsQuestion = (DNSQuestion*) malloc( sizeof(DNSQuestion) ) ; - _buffer = NULL; - _currentPacketSize = 0; - _port = 0; + _dnsHeader = new DNSHeader(); + _dnsQuestion = new DNSQuestion(); } DNSServer::~DNSServer() { if (_dnsHeader) { - free(_dnsHeader); - _dnsHeader = NULL; + delete _dnsHeader; + _dnsHeader = nullptr; } if (_dnsQuestion) { - free(_dnsQuestion); - _dnsQuestion = NULL; - } - if (_buffer) { - free(_buffer); - _buffer = NULL; + delete _dnsQuestion; + _dnsQuestion = nullptr; } } @@ -40,7 +31,6 @@ 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]; @@ -64,8 +54,6 @@ void DNSServer::setTTL(const uint32_t &ttl) void DNSServer::stop() { _udp.close(); - free(_buffer); - _buffer = NULL; } void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) @@ -76,22 +64,30 @@ void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) void DNSServer::_handleUDP(AsyncUDPPacket& pkt) { - _currentPacketSize = pkt.length(); - if (!_currentPacketSize) return; - - // Allocate buffer for the DNS query - if (_buffer != NULL) - free(_buffer); - _buffer = (unsigned char*)malloc(_currentPacketSize * sizeof(char)); - if (_buffer == NULL) - return; + size_t _currentPacketSize = pkt.length(); + if (_currentPacketSize < DNS_HEADER_SIZE) return; + + // get DNS header (beginning of message) + memcpy( _dnsHeader, pkt.data(), DNS_HEADER_SIZE ); + if (_dnsHeader->QR != DNS_QR_QUERY) return; // ignore non-query mesages - // Put the packet received in the buffer and get DNS header (beginning of message) - // and the question - pkt.read(_buffer, _currentPacketSize); - memcpy( _dnsHeader, _buffer, DNS_HEADER_SIZE ) ; if ( requestIncludesOnlyOneQuestion() ) { + char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker + ++enoflbls; // include null terminator + _dnsQuestion->QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request + _dnsQuestion->QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE; + /* + check if we aint going out of pkt bounds + proper dns req should have label terminator at least 4 bytes before end of packet + */ + if (_dnsQuestion->QNameLength > _currentPacketSize - sizeof(_dnsQuestion->QType) - sizeof(_dnsQuestion->QClass)) return; // malformed packet + + // Copy the QType and QClass + memcpy( &_dnsQuestion->QType, enoflbls, sizeof(_dnsQuestion->QType) ); + memcpy( &_dnsQuestion->QClass, enoflbls + sizeof(_dnsQuestion->QType), sizeof(_dnsQuestion->QClass) ); + +/* // The QName has a variable length, maximum 255 bytes and is comprised of multiple labels. // Each label contains a byte to describe its length and the label itself. The list of // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" @@ -108,25 +104,22 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt) // Copy the QType and QClass memcpy( &_dnsQuestion->QType, (void*) &_buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength], sizeof(_dnsQuestion->QType) ) ; memcpy( &_dnsQuestion->QClass, (void*) &_buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength + sizeof(_dnsQuestion->QType)], sizeof(_dnsQuestion->QClass) ) ; +*/ } - - if (_dnsHeader->QR == DNS_QR_QUERY && - _dnsHeader->OPCode == DNS_OPCODE_QUERY && + // will reply with IP only to "*" or if doman matches without www. subdomain + if (_dnsHeader->OPCode == DNS_OPCODE_QUERY && requestIncludesOnlyOneQuestion() && - (_domainName == "*" || getDomainNameWithoutWwwPrefix() == _domainName) + (_domainName == "*" || + getDomainNameWithoutWwwPrefix((const char*)pkt.data() + DNS_HEADER_SIZE, _dnsQuestion->QNameLength) == _domainName) ) { replyWithIP(pkt); + return; } - else if (_dnsHeader->QR == DNS_QR_QUERY) - { - replyWithCustomCode(pkt); - } - - free(_buffer); - _buffer = NULL; + // otherwise reply with custom code + replyWithCustomCode(pkt); } bool DNSServer::requestIncludesOnlyOneQuestion() @@ -138,25 +131,22 @@ bool DNSServer::requestIncludesOnlyOneQuestion() } -String DNSServer::getDomainNameWithoutWwwPrefix() +String DNSServer::getDomainNameWithoutWwwPrefix(const char* start, size_t len) { - // Error checking : if the buffer containing the DNS request is a null pointer, return an empty domain String parsedDomainName(""); - if (_buffer == NULL) - return parsedDomainName; - // Set the start of the domain just after the header (12 bytes). If equal to null character, return an empty domain - unsigned char *start = _buffer + DNS_OFFSET_DOMAIN_NAME; if (*start == 0) { return parsedDomainName; } + parsedDomainName.reserve(len); int pos = 0; while(true) { - unsigned char labelLength = *(start + pos); - for(int i = 0; i < labelLength; i++) + uint8_t labelLength = *(start + pos); + + for(uint8_t i = 0; i < labelLength; i++) { pos++; parsedDomainName += (char)*(start + pos); @@ -186,8 +176,8 @@ void DNSServer::replyWithIP(AsyncUDPPacket& req) // Write the question rpl.write(_dnsQuestion->QName, _dnsQuestion->QNameLength) ; - rpl.write( (unsigned char*) &_dnsQuestion->QType, 2 ) ; - rpl.write( (unsigned char*) &_dnsQuestion->QClass, 2 ) ; + rpl.write( (uint8_t*) &_dnsQuestion->QType, 2 ) ; + rpl.write( (uint8_t*) &_dnsQuestion->QClass, 2 ) ; // Write the answer // Use DNS name compression : instead of repeating the name in this RNAME occurence, @@ -209,14 +199,14 @@ void DNSServer::replyWithIP(AsyncUDPPacket& req) #ifdef DEBUG_ESP_DNS DEBUG_OUTPUT.printf("DNS responds: %s for %s\n", - IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix().c_str() ); + IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix((const char*)rpl.data() + DNS_HEADER_SIZE, _dnsQuestion->QNameLength).c_str() ); #endif } void DNSServer::replyWithCustomCode(AsyncUDPPacket& req) { _dnsHeader->QR = DNS_QR_RESPONSE; - _dnsHeader->RCode = (unsigned char)_errorReplyCode; + _dnsHeader->RCode = (uint16_t)_errorReplyCode; _dnsHeader->QDCount = 0; AsyncUDPMessage rpl(sizeof(DNSHeader)); diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index f1e01b803c1..0df66aecdac 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -9,7 +9,7 @@ #define DNS_OFFSET_DOMAIN_NAME 12 // Offset in bytes to reach the domain name in the DNS message #define DNS_HEADER_SIZE 12 -enum class DNSReplyCode +enum class DNSReplyCode:uint16_t { NoError = 0, FormError = 1, @@ -66,7 +66,7 @@ struct DNSHeader struct DNSQuestion { - uint8_t QName[256] ; //need 1 Byte for zero termination! + const uint8_t* QName; uint16_t QNameLength ; uint16_t QType ; uint16_t QClass ; @@ -91,18 +91,25 @@ class DNSServer private: AsyncUDP _udp; uint16_t _port; + uint32_t _ttl; + DNSReplyCode _errorReplyCode; String _domainName; unsigned char _resolvedIP[4]; - int _currentPacketSize; - unsigned char* _buffer; DNSHeader* _dnsHeader; - uint32_t _ttl; - DNSReplyCode _errorReplyCode; DNSQuestion* _dnsQuestion ; void downcaseAndRemoveWwwPrefix(String &domainName); - String getDomainNameWithoutWwwPrefix(); + + /** + * @brief Get the Domain Name Without Www Prefix object + * scan labels in DNS packet and build a string of a domain name + * truncate any www. label if found + * @param start a pointer to the start of labels records in DNS packet + * @param len labels length + * @return String + */ + String getDomainNameWithoutWwwPrefix(const char* start, size_t len); bool requestIncludesOnlyOneQuestion(); void replyWithIP(AsyncUDPPacket& req); void replyWithCustomCode(AsyncUDPPacket& req); From e50dfef07780d470a2cd21cd3d5a3a79f4f3e3c4 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Thu, 17 Nov 2022 00:17:20 +0300 Subject: [PATCH 3/7] DNSServer drop dynamically allocated member structs DNSHeader and DNSQuestion structs could be created on stack no need to keep it as obj members --- libraries/DNSServer/src/DNSServer.cpp | 113 ++++++++++---------------- libraries/DNSServer/src/DNSServer.h | 12 ++- 2 files changed, 49 insertions(+), 76 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index b39870f8dff..a8d185898bd 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -9,23 +9,9 @@ #define DEBUG_OUTPUT Serial #endif -DNSServer::DNSServer() : _port(0), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain) -{ - _dnsHeader = new DNSHeader(); - _dnsQuestion = new DNSQuestion(); -} +DNSServer::DNSServer() : _port(0), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain){} -DNSServer::~DNSServer() -{ - if (_dnsHeader) { - delete _dnsHeader; - _dnsHeader = nullptr; - } - if (_dnsQuestion) { - delete _dnsQuestion; - _dnsQuestion = nullptr; - } -} +DNSServer::~DNSServer(){} bool DNSServer::start(const uint16_t &port, const String &domainName, const IPAddress &resolvedIP) @@ -37,6 +23,7 @@ bool DNSServer::start(const uint16_t &port, const String &domainName, _resolvedIP[2] = resolvedIP[2]; _resolvedIP[3] = resolvedIP[3]; downcaseAndRemoveWwwPrefix(_domainName); + _udp.close(); _udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); }); return _udp.listen(_port); } @@ -64,70 +51,58 @@ void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) void DNSServer::_handleUDP(AsyncUDPPacket& pkt) { - size_t _currentPacketSize = pkt.length(); - if (_currentPacketSize < DNS_HEADER_SIZE) return; + size_t currentPacketSize = pkt.length(); + if (currentPacketSize < DNS_HEADER_SIZE) return; // get DNS header (beginning of message) - memcpy( _dnsHeader, pkt.data(), DNS_HEADER_SIZE ); - if (_dnsHeader->QR != DNS_QR_QUERY) return; // ignore non-query mesages + DNSHeader dnsHeader; + DNSQuestion dnsQuestion; + memcpy( &dnsHeader, pkt.data(), DNS_HEADER_SIZE ); + if (dnsHeader.QR != DNS_QR_QUERY) return; // ignore non-query mesages - if ( requestIncludesOnlyOneQuestion() ) + if ( requestIncludesOnlyOneQuestion(dnsHeader) ) { +/* + // The QName has a variable length, maximum 255 bytes and is comprised of multiple labels. + // Each label contains a byte to describe its length and the label itself. The list of + // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" +*/ char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker ++enoflbls; // include null terminator - _dnsQuestion->QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request - _dnsQuestion->QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE; + dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request + dnsQuestion.QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE; /* check if we aint going out of pkt bounds proper dns req should have label terminator at least 4 bytes before end of packet */ - if (_dnsQuestion->QNameLength > _currentPacketSize - sizeof(_dnsQuestion->QType) - sizeof(_dnsQuestion->QClass)) return; // malformed packet + if (dnsQuestion.QNameLength > currentPacketSize - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet // Copy the QType and QClass - memcpy( &_dnsQuestion->QType, enoflbls, sizeof(_dnsQuestion->QType) ); - memcpy( &_dnsQuestion->QClass, enoflbls + sizeof(_dnsQuestion->QType), sizeof(_dnsQuestion->QClass) ); - -/* - // The QName has a variable length, maximum 255 bytes and is comprised of multiple labels. - // Each label contains a byte to describe its length and the label itself. The list of - // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" - // Iterate through the labels and copy them as they come into a single buffer (for simplicity's sake) - _dnsQuestion->QNameLength = 0 ; - while ( _buffer[ DNS_HEADER_SIZE + _dnsQuestion->QNameLength ] != 0 ) - { - memcpy( (void*) &_dnsQuestion->QName[_dnsQuestion->QNameLength], (void*) &_buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength], _buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength] + 1 ) ; - _dnsQuestion->QNameLength += _buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength] + 1 ; - } - _dnsQuestion->QName[_dnsQuestion->QNameLength] = 0 ; - _dnsQuestion->QNameLength++ ; - - // Copy the QType and QClass - memcpy( &_dnsQuestion->QType, (void*) &_buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength], sizeof(_dnsQuestion->QType) ) ; - memcpy( &_dnsQuestion->QClass, (void*) &_buffer[DNS_HEADER_SIZE + _dnsQuestion->QNameLength + sizeof(_dnsQuestion->QType)], sizeof(_dnsQuestion->QClass) ) ; -*/ + memcpy( &dnsQuestion.QType, enoflbls, sizeof(dnsQuestion.QType) ); + memcpy( &dnsQuestion.QClass, enoflbls + sizeof(dnsQuestion.QType), sizeof(dnsQuestion.QClass) ); } - + // will reply with IP only to "*" or if doman matches without www. subdomain - if (_dnsHeader->OPCode == DNS_OPCODE_QUERY && - requestIncludesOnlyOneQuestion() && + if (dnsHeader.OPCode == DNS_OPCODE_QUERY && + requestIncludesOnlyOneQuestion(dnsHeader) && (_domainName == "*" || - getDomainNameWithoutWwwPrefix((const char*)pkt.data() + DNS_HEADER_SIZE, _dnsQuestion->QNameLength) == _domainName) + getDomainNameWithoutWwwPrefix((const char*)pkt.data() + DNS_HEADER_SIZE, dnsQuestion.QNameLength) == _domainName) ) { - replyWithIP(pkt); + replyWithIP(pkt, dnsHeader, dnsQuestion); return; } // otherwise reply with custom code - replyWithCustomCode(pkt); + replyWithCustomCode(pkt, dnsHeader); } -bool DNSServer::requestIncludesOnlyOneQuestion() +bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader) { - return ntohs(_dnsHeader->QDCount) == 1 && - _dnsHeader->ANCount == 0 && - _dnsHeader->NSCount == 0 && - _dnsHeader->ARCount == 0; + return ntohs(dnsHeader.QDCount) == 1 && + dnsHeader.ANCount == 0 && + dnsHeader.NSCount == 0 && + dnsHeader.ARCount == 0; } @@ -164,20 +139,20 @@ String DNSServer::getDomainNameWithoutWwwPrefix(const char* start, size_t len) } } -void DNSServer::replyWithIP(AsyncUDPPacket& req) +void DNSServer::replyWithIP(AsyncUDPPacket& req, DNSHeader& dnsHeader, DNSQuestion& dnsQuestion) { AsyncUDPMessage rpl; // Change the type of message to a response and set the number of answers equal to // the number of questions in the header - _dnsHeader->QR = DNS_QR_RESPONSE; - _dnsHeader->ANCount = _dnsHeader->QDCount; - rpl.write( (unsigned char*) _dnsHeader, DNS_HEADER_SIZE ) ; + dnsHeader.QR = DNS_QR_RESPONSE; + dnsHeader.ANCount = dnsHeader.QDCount; + rpl.write( (unsigned char*) &dnsHeader, DNS_HEADER_SIZE ) ; // Write the question - rpl.write(_dnsQuestion->QName, _dnsQuestion->QNameLength) ; - rpl.write( (uint8_t*) &_dnsQuestion->QType, 2 ) ; - rpl.write( (uint8_t*) &_dnsQuestion->QClass, 2 ) ; + rpl.write(dnsQuestion.QName, dnsQuestion.QNameLength) ; + rpl.write( (uint8_t*) &dnsQuestion.QType, 2 ) ; + rpl.write( (uint8_t*) &dnsQuestion.QClass, 2 ) ; // Write the answer // Use DNS name compression : instead of repeating the name in this RNAME occurence, @@ -199,17 +174,17 @@ void DNSServer::replyWithIP(AsyncUDPPacket& req) #ifdef DEBUG_ESP_DNS DEBUG_OUTPUT.printf("DNS responds: %s for %s\n", - IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix((const char*)rpl.data() + DNS_HEADER_SIZE, _dnsQuestion->QNameLength).c_str() ); + IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix((const char*)rpl.data() + DNS_HEADER_SIZE, dnsQuestion.QNameLength).c_str() ); #endif } -void DNSServer::replyWithCustomCode(AsyncUDPPacket& req) +void DNSServer::replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader) { - _dnsHeader->QR = DNS_QR_RESPONSE; - _dnsHeader->RCode = (uint16_t)_errorReplyCode; - _dnsHeader->QDCount = 0; + dnsHeader.QR = DNS_QR_RESPONSE; + dnsHeader.RCode = (uint16_t)_errorReplyCode; + dnsHeader.QDCount = 0; AsyncUDPMessage rpl(sizeof(DNSHeader)); - rpl.write((unsigned char*)_dnsHeader, sizeof(DNSHeader)); + rpl.write((unsigned char*)&dnsHeader, sizeof(DNSHeader)); _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); } diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index 0df66aecdac..cb497701cc8 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -59,9 +59,9 @@ struct DNSHeader uint16_t Flags; }; uint16_t QDCount; // number of question entries - uint16_t ANCount; // number of answer entries + uint16_t ANCount; // number of ANswer entries uint16_t NSCount; // number of authority entries - uint16_t ARCount; // number of resource entries + uint16_t ARCount; // number of Additional Resource entries }; struct DNSQuestion @@ -95,8 +95,6 @@ class DNSServer DNSReplyCode _errorReplyCode; String _domainName; unsigned char _resolvedIP[4]; - DNSHeader* _dnsHeader; - DNSQuestion* _dnsQuestion ; void downcaseAndRemoveWwwPrefix(String &domainName); @@ -110,9 +108,9 @@ class DNSServer * @return String */ String getDomainNameWithoutWwwPrefix(const char* start, size_t len); - bool requestIncludesOnlyOneQuestion(); - void replyWithIP(AsyncUDPPacket& req); - void replyWithCustomCode(AsyncUDPPacket& req); + inline bool requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader); + void replyWithIP(AsyncUDPPacket& req, DNSHeader& dnsHeader, DNSQuestion& dnsQuestion); + inline void replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader); void _handleUDP(AsyncUDPPacket& pkt); }; #endif From 545a9460da8fa42db747e708b0ba597d20175062 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Sun, 20 Nov 2022 22:47:23 +0300 Subject: [PATCH 4/7] DNSServer: labels min length checks, simplified labels parser --- libraries/DNSServer/src/DNSServer.cpp | 41 ++++++++------------------- libraries/DNSServer/src/DNSServer.h | 4 +-- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index a8d185898bd..6fbb4871d49 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -67,15 +67,15 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt) // Each label contains a byte to describe its length and the label itself. The list of // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" */ - char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker - ++enoflbls; // include null terminator - dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request + const char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker + ++enoflbls; // advance after null terminator + dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request dnsQuestion.QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE; /* check if we aint going out of pkt bounds proper dns req should have label terminator at least 4 bytes before end of packet */ - if (dnsQuestion.QNameLength > currentPacketSize - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet + if (dnsQuestion.QNameLength < 3 || dnsQuestion.QNameLength > currentPacketSize - DNS_HEADER_SIZE - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet // Copy the QType and QClass memcpy( &dnsQuestion.QType, enoflbls, sizeof(dnsQuestion.QType) ); @@ -108,35 +108,18 @@ bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader) String DNSServer::getDomainNameWithoutWwwPrefix(const char* start, size_t len) { - String parsedDomainName(""); - - if (*start == 0) - { - return parsedDomainName; - } + String parsedDomainName(start, --len); // exclude trailing null byte from labels length, String constructor will add it anyway - parsedDomainName.reserve(len); int pos = 0; - while(true) + while(pos #define DNS_QR_QUERY 0 @@ -113,4 +112,3 @@ class DNSServer inline void replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader); void _handleUDP(AsyncUDPPacket& pkt); }; -#endif From 5daade10ddd99a7bfae96463aaf637991227d652 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Sun, 11 Dec 2022 22:04:29 +0900 Subject: [PATCH 5/7] DNSServer use default settings for catch-all setup - default constructor and start() method simply runs a catch-all DNS setup - avoid string comparison for domain reqs in catch-all mode - use IPaddress class for _resolvedIP (looking for IPv6 support in future) --- libraries/DNSServer/src/DNSServer.cpp | 56 +++++++++++++++++---------- libraries/DNSServer/src/DNSServer.h | 28 ++++++++++++-- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 6fbb4871d49..9cea3290099 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -1,6 +1,8 @@ #include "DNSServer.h" #include #include +#include + // #define DEBUG_ESP_DNS #ifdef DEBUG_ESP_PORT @@ -9,20 +11,34 @@ #define DEBUG_OUTPUT Serial #endif -DNSServer::DNSServer() : _port(0), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain){} +#define DNS_MIN_REQ_LEN 17 // minimal size for DNS request asking ROOT = DNS_HEADER_SIZE + 1 null byte for Name + 4 bytes type/class + +DNSServer::DNSServer() : _port(DNS_DEFAULT_PORT), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain){} + +DNSServer::DNSServer(const String &domainName) : _port(DNS_DEFAULT_PORT), _ttl(htonl(DNS_DEFAULT_TTL)), _errorReplyCode(DNSReplyCode::NonExistentDomain), _domainName(domainName){}; + + +bool DNSServer::start(){ + if (WiFi.getMode() & WIFI_AP){ + _resolvedIP = WiFi.softAPIP(); + } else return false; // won't run if WiFi is not in AP mode -DNSServer::~DNSServer(){} + _udp.close(); + _udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); }); + return _udp.listen(_port); +} 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); + if (domainName != "*"){ + _domainName = domainName; + downcaseAndRemoveWwwPrefix(_domainName); + } else + _domainName.clear(); + + _resolvedIP = resolvedIP; _udp.close(); _udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); }); return _udp.listen(_port); @@ -51,8 +67,7 @@ void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) void DNSServer::_handleUDP(AsyncUDPPacket& pkt) { - size_t currentPacketSize = pkt.length(); - if (currentPacketSize < DNS_HEADER_SIZE) return; + if (pkt.length() < DNS_MIN_REQ_LEN) return; // truncated packet or not a DNS req // get DNS header (beginning of message) DNSHeader dnsHeader; @@ -67,7 +82,7 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt) // Each label contains a byte to describe its length and the label itself. The list of // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" */ - const char * enoflbls = strchr((const char*)pkt.data() + DNS_HEADER_SIZE, 0); // find end_of_label marker + const char * enoflbls = strchr(reinterpret_cast(pkt.data()) + DNS_HEADER_SIZE, 0); // find end_of_label marker ++enoflbls; // advance after null terminator dnsQuestion.QName = pkt.data() + DNS_HEADER_SIZE; // we can reference labels from the request dnsQuestion.QNameLength = enoflbls - (char*)pkt.data() - DNS_HEADER_SIZE; @@ -75,7 +90,7 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt) check if we aint going out of pkt bounds proper dns req should have label terminator at least 4 bytes before end of packet */ - if (dnsQuestion.QNameLength < 3 || dnsQuestion.QNameLength > currentPacketSize - DNS_HEADER_SIZE - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet + if (dnsQuestion.QNameLength > pkt.length() - DNS_HEADER_SIZE - sizeof(dnsQuestion.QType) - sizeof(dnsQuestion.QClass)) return; // malformed packet // Copy the QType and QClass memcpy( &dnsQuestion.QType, enoflbls, sizeof(dnsQuestion.QType) ); @@ -85,8 +100,8 @@ void DNSServer::_handleUDP(AsyncUDPPacket& pkt) // will reply with IP only to "*" or if doman matches without www. subdomain if (dnsHeader.OPCode == DNS_OPCODE_QUERY && requestIncludesOnlyOneQuestion(dnsHeader) && - (_domainName == "*" || - getDomainNameWithoutWwwPrefix((const char*)pkt.data() + DNS_HEADER_SIZE, dnsQuestion.QNameLength) == _domainName) + (_domainName.isEmpty() || + getDomainNameWithoutWwwPrefix(static_cast(dnsQuestion.QName), dnsQuestion.QNameLength) == _domainName) ) { replyWithIP(pkt, dnsHeader, dnsQuestion); @@ -106,14 +121,14 @@ bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader) } -String DNSServer::getDomainNameWithoutWwwPrefix(const char* start, size_t len) +String DNSServer::getDomainNameWithoutWwwPrefix(const unsigned char* start, size_t len) { String parsedDomainName(start, --len); // exclude trailing null byte from labels length, String constructor will add it anyway int pos = 0; while(pos(&ip), sizeof(uint32_t)); // The IPv4 address to return _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); #ifdef DEBUG_ESP_DNS DEBUG_OUTPUT.printf("DNS responds: %s for %s\n", - IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix((const char*)rpl.data() + DNS_HEADER_SIZE, dnsQuestion.QNameLength).c_str() ); + _resolvedIP.toString().c_str(), getDomainNameWithoutWwwPrefix(static_cast(dnsQuestion.QName), dnsQuestion.QNameLength).c_str() ); #endif } void DNSServer::replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader) { dnsHeader.QR = DNS_QR_RESPONSE; - dnsHeader.RCode = (uint16_t)_errorReplyCode; + dnsHeader.RCode = static_cast(_errorReplyCode); dnsHeader.QDCount = 0; AsyncUDPMessage rpl(sizeof(DNSHeader)); - rpl.write((unsigned char*)&dnsHeader, sizeof(DNSHeader)); + rpl.write(reinterpret_cast(&dnsHeader), sizeof(DNSHeader)); _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); } diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index ecfaab1be9c..9a90e12450f 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -5,8 +5,9 @@ #define DNS_QR_RESPONSE 1 #define DNS_OPCODE_QUERY 0 #define DNS_DEFAULT_TTL 60 // Default Time To Live : time interval in seconds that the resource record should be cached before being discarded -#define DNS_OFFSET_DOMAIN_NAME 12 // Offset in bytes to reach the domain name in the DNS message #define DNS_HEADER_SIZE 12 +#define DNS_OFFSET_DOMAIN_NAME DNS_HEADER_SIZE // Offset in bytes to reach the domain name labels in the DNS message +#define DNS_DEFAULT_PORT 53 enum class DNSReplyCode:uint16_t { @@ -75,11 +76,30 @@ class DNSServer { public: DNSServer(); - ~DNSServer(); + /** + * @brief Construct a new DNSServer object + * builds DNS server with default parameters + * @param domainName - domain name to serve + */ + DNSServer(const String &domainName); + ~DNSServer(){}; // default d-tor void processNextRequest(){}; // stub, left for compatibility with an old version void setErrorReplyCode(const DNSReplyCode &replyCode); void setTTL(const uint32_t &ttl); + /** + * @brief Starts a server with current configuration or with default parameters + * if it's the first call. + * Defaults are: + * port: 53 + * domainName: any + * ip: WiFi AP's IP address + * + * @return true on success + * @return false if IP or socket error + */ + bool start(); + // Returns true if successful, false if there are no sockets available bool start(const uint16_t &port, const String &domainName, @@ -93,7 +113,7 @@ class DNSServer uint32_t _ttl; DNSReplyCode _errorReplyCode; String _domainName; - unsigned char _resolvedIP[4]; + IPAddress _resolvedIP; void downcaseAndRemoveWwwPrefix(String &domainName); @@ -106,7 +126,7 @@ class DNSServer * @param len labels length * @return String */ - String getDomainNameWithoutWwwPrefix(const char* start, size_t len); + String getDomainNameWithoutWwwPrefix(const unsigned char* start, size_t len); inline bool requestIncludesOnlyOneQuestion(DNSHeader& dnsHeader); void replyWithIP(AsyncUDPPacket& req, DNSHeader& dnsHeader, DNSQuestion& dnsQuestion); inline void replyWithCustomCode(AsyncUDPPacket& req, DNSHeader& dnsHeader); From 77fbc2a74d233edd8ee97e05e9644cf2dd8407d0 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Sun, 11 Dec 2022 22:08:31 +0900 Subject: [PATCH 6/7] CaptivePortal example refactored - use webserver instead of simple tcp setver - use redirects to allows CaptivePortal detection pop-ups in modern systems --- .../examples/CaptivePortal/CaptivePortal.ino | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino b/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino index 9221af1eaa2..7e292e1adfb 100644 --- a/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino +++ b/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino @@ -1,52 +1,59 @@ +/* +This example enables catch-all Captive portal for ESP32 Access-Point +It will allow modern devices/OSes to detect that WiFi connection is +limited and offer a user to access a banner web-page. +There is no need to find and open device's IP address/URL, i.e. http://192.168.4.1/ +This works for Android, Ubuntu, FireFox, Windows, maybe others... +*/ + +#include #include #include +#include + -const byte DNS_PORT = 53; -IPAddress apIP(8,8,4,4); // The default android DNS DNSServer dnsServer; -WiFiServer server(80); +WebServer server(80); + +static const char responsePortal[] = R"===( +ESP32 CaptivePortal +

Hello World!

This is a captive portal example page. All unknown http requests will +be redirected here.

+)==="; + +// index page handler +void handleRoot() { + server.send(200, "text/plain", "Hello from esp32!"); +} -String responseHTML = "" - "CaptivePortal" - "

Hello World!

This is a captive portal example. All requests will " - "be redirected here.

"; +// this will redirect unknown http req's to our captive portal page +// based on this redirect various systems could detect that WiFi AP has a captive portal page +void handleNotFound() { + server.sendHeader("Location", "/portal"); + server.send(302, "text/plain", "redirect to captive portal"); +} -void setup() { +void setup() { + Serial.begin(115200); WiFi.mode(WIFI_AP); WiFi.softAP("ESP32-DNSServer"); - WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); - // if DNSServer is started with "*" for domain name, it will reply with - // provided IP to all DNS request - dnsServer.start(DNS_PORT, "*", apIP); + // by default DNSServer is started serving any "*" domain name. It will reply + // AccessPoint's IP to all DNS request (this is requred for Captive Portal detection) + dnsServer.start(); + + // serve a simple root page + server.on("/", handleRoot); + + // serve portal page + server.on("/portal",[](){server.send(200, "text/html", responsePortal);}); + // all unknown pages are redirected to captive portal + server.onNotFound(handleNotFound); server.begin(); } void loop() { - dnsServer.processNextRequest(); - WiFiClient client = server.available(); // listen for incoming clients - - if (client) { - String currentLine = ""; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - if (c == '\n') { - if (currentLine.length() == 0) { - client.println("HTTP/1.1 200 OK"); - client.println("Content-type:text/html"); - client.println(); - client.print(responseHTML); - break; - } else { - currentLine = ""; - } - } else if (c != '\r') { - currentLine += c; - } - } - } - client.stop(); - } + server.handleClient(); + delay(5); // give CPU some idle time } From 4998243004c258444f7a29b0cb303e4b555b8918 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Thu, 22 Dec 2022 23:23:33 +0900 Subject: [PATCH 7/7] DNSServer status getters added add isUp() method - returns 'true' if server is up and UDP socket is listening for UDP req's add isCaptive() method - returns 'true' if server runs in catch-all (captive portal mode) some doxygen comments added start() method now keeps existing IP address if any --- libraries/DNSServer/src/DNSServer.cpp | 12 ++--- libraries/DNSServer/src/DNSServer.h | 66 +++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 9cea3290099..a8114733460 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -19,18 +19,18 @@ DNSServer::DNSServer(const String &domainName) : _port(DNS_DEFAULT_PORT), _ttl(h bool DNSServer::start(){ - if (WiFi.getMode() & WIFI_AP){ - _resolvedIP = WiFi.softAPIP(); - } else return false; // won't run if WiFi is not in AP mode + if (_resolvedIP.operator uint32_t() == 0){ // no address is set, try to obtain AP interface's IP + if (WiFi.getMode() & WIFI_AP){ + _resolvedIP = WiFi.softAPIP(); + } else return false; // won't run if WiFi is not in AP mode + } _udp.close(); _udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); }); return _udp.listen(_port); } -bool DNSServer::start(const uint16_t &port, const String &domainName, - const IPAddress &resolvedIP) -{ +bool DNSServer::start(uint16_t port, const String &domainName, const IPAddress &resolvedIP){ _port = port; if (domainName != "*"){ _domainName = domainName; diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index 9a90e12450f..860507ac9ec 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -75,7 +75,14 @@ struct DNSQuestion class DNSServer { public: + /** + * @brief Construct a new DNSServer object + * by default server is configured to run in "Captive-portal" mode + * it must be started with start() call to establish a listening socket + * + */ DNSServer(); + /** * @brief Construct a new DNSServer object * builds DNS server with default parameters @@ -83,12 +90,35 @@ class DNSServer */ DNSServer(const String &domainName); ~DNSServer(){}; // default d-tor - void processNextRequest(){}; // stub, left for compatibility with an old version + + // Copy semantics not implemented (won't run on same UDP port anyway) + DNSServer(const DNSServer&) = delete; + DNSServer& operator=(const DNSServer&) = delete; + + + /** + * @brief stub, left for compatibility with an old version + * does nothing actually + * + */ + void processNextRequest(){}; + + /** + * @brief Set the Error Reply Code for all req's not matching predifined domain + * + * @param replyCode + */ void setErrorReplyCode(const DNSReplyCode &replyCode); + + /** + * @brief set TTL for successfull replies + * + * @param ttl in seconds + */ void setTTL(const uint32_t &ttl); /** - * @brief Starts a server with current configuration or with default parameters + * @brief (re)Starts a server with current configuration or with default parameters * if it's the first call. * Defaults are: * port: 53 @@ -100,13 +130,39 @@ class DNSServer */ bool start(); - // Returns true if successful, false if there are no sockets available - bool start(const uint16_t &port, + /** + * @brief (re)Starts a server with provided configuration + * + * @return true on success + * @return false if IP or socket error + */ + bool start(uint16_t port, const String &domainName, const IPAddress &resolvedIP); - // stops the DNS server + + /** + * @brief stops the server and close UDP socket + * + */ void stop(); + /** + * @brief returns true if DNS server runs in captive-portal mode + * i.e. all requests are served with AP's ip address + * + * @return true if catch-all mode active + * @return false otherwise + */ + inline bool isCaptive() const { return _domainName.isEmpty(); }; + + /** + * @brief returns 'true' if server is up and UDP socket is listening for UDP req's + * + * @return true if server is up + * @return false otherwise + */ + inline bool isUp() { return _udp.connected(); }; + private: AsyncUDP _udp; uint16_t _port;