diff --git a/.codespellrc b/.codespellrc new file mode 100644 index 0000000..00fe362 --- /dev/null +++ b/.codespellrc @@ -0,0 +1,7 @@ +# See: https://github.com/codespell-project/codespell#using-a-config-file +[codespell] +# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: +ignore-words-list = , +check-filenames = +check-hidden = +skip = ./.git,./src,./examples,./Packages_Patches,./LibraryPatches diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..81f7893 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,82 @@ +## Contributing to AsyncDNSServer_ESP32_W6100 + +### Reporting Bugs + +Please report bugs in AsyncDNSServer_ESP32_W6100 if you find them. + +However, before reporting a bug please check through the following: + +* [Existing Open Issues](https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100/issues) - someone might have already encountered this. + +If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100/issues/new). + +--- + +### How to submit a bug report + +Please ensure to specify the following: + +* Arduino IDE version (e.g. 1.8.19) or Platform.io version +* Board Core Version (e.g. ESP32 core v2.0.6) +* Contextual information (e.g. what you were trying to achieve) +* Simplest possible steps to reproduce +* Anything that might be relevant in your opinion, such as: + * Operating system (Windows, Ubuntu, etc.) and the output of `uname -a` + * Network configuration + + +Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted. + +--- + +### Example + +``` +Arduino IDE version: 1.8.19 +ESP32_DEV board +ESP32 core v2.0.6 +OS: Ubuntu 20.04 LTS +Linux xy-Inspiron-3593 5.15.0-57-generic #63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux + +Context: +I encountered a crash while using this library +Steps to reproduce: +1. ... +2. ... +3. ... +4. ... +``` + +### Additional context + +Add any other context about the problem here. + +--- + +### Sending Feature Requests + +Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. + +There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. + +--- + +### Sending Pull Requests + +Pull Requests with changes and fixes are also welcome! + +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/AsyncDNSServer_ESP32_W6100_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncDNSServer_ESP32_W6100_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncDNSServer_ESP32_W6100_GitHub$ bash utils/restyle.sh +``` + diff --git a/Images/W6100.png b/Images/W6100.png new file mode 100644 index 0000000..867119a Binary files /dev/null and b/Images/W6100.png differ diff --git a/LICENSE b/LICENSE index f288702..94a9ed0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. @@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found. GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program. If not, see . + along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see -. +. The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read -. +. diff --git a/LibraryPatches/esp32/cores/esp32/Server.h b/LibraryPatches/esp32/cores/esp32/Server.h new file mode 100644 index 0000000..2a677df --- /dev/null +++ b/LibraryPatches/esp32/cores/esp32/Server.h @@ -0,0 +1,35 @@ +/* + Server.h - Base class that provides Server + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef server_h +#define server_h + +#include "Print.h" + +class Server: public Print +{ + public: + // KH, change to fix compiler error for EthernetWebServer + // error: cannot declare field 'EthernetWebServer::_server' to be of abstract type 'EthernetServer' + // virtual void begin(uint16_t port=0) =0; + //virtual void begin() = 0; + void begin() {}; +}; + +#endif diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..e11b53d --- /dev/null +++ b/changelog.md @@ -0,0 +1,28 @@ +# AsyncDNSServer_ESP32_W6100 Library + +[![arduino-library-badge](https://www.ardu-badge.com/badge/AsyncDNSServer_ESP32_W6100.svg?)](https://www.ardu-badge.com/AsyncDNSServer_ESP32_W6100) +[![GitHub release](https://img.shields.io/github/release/khoih-prog/AsyncDNSServer_ESP32_W6100.svg)](https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100/releases) +[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) +[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/AsyncDNSServer_ESP32_W6100.svg)](http://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100/issues) + + +Donate to my libraries using BuyMeACoffee + + +--- +--- + +## Table of Contents + +* [Changelog](#changelog) + * [Initial Releases v1.0.0](#initial-releases-v100) + +--- +--- + +## Changelog + +### Initial Releases v1.0.0 + +1. Initial coding to support **ESP32 boards using LwIP W6100 Ethernet** + diff --git a/examples/AsyncCaptivePortalAdvanced_ESP32_W6100/AsyncCaptivePortalAdvanced_ESP32_W6100.ino b/examples/AsyncCaptivePortalAdvanced_ESP32_W6100/AsyncCaptivePortalAdvanced_ESP32_W6100.ino new file mode 100644 index 0000000..d94e1c7 --- /dev/null +++ b/examples/AsyncCaptivePortalAdvanced_ESP32_W6100/AsyncCaptivePortalAdvanced_ESP32_W6100.ino @@ -0,0 +1,298 @@ +/**************************************************************************************************************************** + AsyncCaptivePortalAdvanced_ESP32_W6100.ino + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + *****************************************************************************************************************************/ + +/* + This example serves a "hello world". + + Now the ESP32_W6100 is in your network. You can reach it through http://192.168.x.x/ + + This is a captive portal because it will redirect any http request to http://192.168.x.x/ +*/ + +#if !( defined(ESP32) ) + #error This code is designed for (ESP32 + W6100) to run on ESP32 platform! Please check your Tools->Board setting. +#endif + +#define ASYNC_DNS_ESP32_W6100_DEBUG_PORT Serial + +// Use from 0 to 4. Higher number, more debugging messages and memory usage. +#define _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ 1 + +///////////////////////////////////////////// + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; + +// Select the IP address according to your local network +IPAddress myIP(192, 168, 2, 232); +IPAddress myGW(192, 168, 2, 1); +IPAddress mySN(255, 255, 255, 0); + +// Google DNS Server IP +IPAddress myDNS(8, 8, 8, 8); + +////////////////////////////////////////////////////////// + +// Optional values to override default settings +// Don't change unless you know what you're doing +//#define ETH_SPI_HOST SPI3_HOST +//#define SPI_CLOCK_MHZ 25 + +// Must connect INT to GPIOxx or not working +//#define INT_GPIO 4 + +//#define MISO_GPIO 19 +//#define MOSI_GPIO 23 +//#define SCK_GPIO 18 +//#define CS_GPIO 5 + +////////////////////////////////////////////////////////// + +#include +#include + + +// DNS server +const byte DNS_PORT = 53; + +AsyncDNSServer dnsServer; + +// Web server +AsyncWebServer server(80); + +IPAddress apIP; + +/** Is this an IP? */ +bool isIp(String str) +{ + for (size_t i = 0; i < str.length(); i++) + { + int c = str.charAt(i); + + if (c != '.' && (c < '0' || c > '9')) + { + return false; + } + } + + return true; +} + +/** IP to String? */ +String toStringIp(IPAddress ip) +{ + String res = ""; + + for (int i = 0; i < 3; i++) + { + res += String((ip >> (8 * i)) & 0xFF) + "."; + } + + res += String(((ip >> 8 * 3)) & 0xFF); + + return res; +} + +/** Handle root or redirect to captive portal */ +void handleRoot(AsyncWebServerRequest * request) +{ + if (captivePortal(request)) + { + // If captive portal redirect instead of displaying the page. + return; + } + + String Page = F( + "" + "" + "ESP32_W6100-CaptivePortal" + "

HELLO WORLD!!

"); + + Page += "

From " + String(BOARD_NAME) + " using LAN8720A

"; + + AsyncWebServerResponse *response = request->beginResponse(200, "text/html", Page); + response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + response->addHeader("Pragma", "no-cache"); + response->addHeader("Expires", "-1"); + + request->send(response); +} + +// Redirect to captive portal if we got a request for another domain. +// Return true in that case so the page handler do not try to handle the request again. +bool captivePortal(AsyncWebServerRequest * request) +{ + if (!isIp(request->host())) + { + Serial.println("Request redirected to captive portal"); + + // Empty content inhibits Content-length header so we have to close the socket ourselves. + AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", ""); + response->addHeader("Location", String("http://") + toStringIp(request->client()->localIP())); + + request->send(response); + + request->client()->stop(); // Stop is needed because we sent no content length + + return true; + } + + return false; +} + +void handleNotFound(AsyncWebServerRequest * request) +{ + if (captivePortal(request)) + { + // If captive portal redirect instead of displaying the error page. + return; + } + + String message = F("File Not Found\n\n"); + + message += F("URI: "); + message += request->url(); + message += F("\nMethod: "); + message += (request->method() == HTTP_GET) ? "GET" : "POST"; + message += F("\nArguments: "); + message += request->args(); + message += F("\n"); + + for (uint8_t i = 0; i < request->args(); i++) + { + message += String(F(" ")) + request->argName(i) + F(": ") + request->arg(i) + F("\n"); + } + + AsyncWebServerResponse *response = request->beginResponse(404, "text/plain", message); + response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + response->addHeader("Pragma", "no-cache"); + response->addHeader("Expires", "-1"); + + request->send(response); +} + +void setup() +{ + Serial.begin(115200); + + while (!Serial && (millis() < 5000)); + + Serial.print(F("\nStart AsyncCaptivePortalAdvanced_ESP32_W6100 on ")); + Serial.print(ARDUINO_BOARD); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_ESP32_W6100_VERSION); + Serial.println(ASYNC_UDP_ESP32_W6100_VERSION); + Serial.println(ASYNC_DNS_SERVER_ESP32_W6100_VERSION); + + Serial.setDebugOutput(true); + + UDP_LOGWARN(F("Default SPI pinout:")); + UDP_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST); + UDP_LOGWARN1(F("MOSI:"), MOSI_GPIO); + UDP_LOGWARN1(F("MISO:"), MISO_GPIO); + UDP_LOGWARN1(F("SCK:"), SCK_GPIO); + UDP_LOGWARN1(F("CS:"), CS_GPIO); + UDP_LOGWARN1(F("INT:"), INT_GPIO); + UDP_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); + UDP_LOGWARN(F("=========================")); + + /////////////////////////////////// + + // To be called before ETH.begin() + ESP32_W6100_onEvent(); + + // start the ethernet connection and the server: + // Use DHCP dynamic IP and random mac + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, + // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac); + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST ); + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] ); + + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + //ETH.config(myIP, myGW, mySN, myDNS); + + ESP32_W6100_waitForConnect(); + + /////////////////////////////////// + + apIP = ETH.localIP(); + + /////////////////////////////////// + + + // 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 AsyncDNSReplyCode::NonExistentDomain + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); + + dnsServer.start(DNS_PORT, "*", apIP); + + /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */ + // simple HTTP server to see that DNS server is working + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) + { + handleRoot(request); + }); + + //Android captive portal. Maybe not needed. Might be handled by notFound handler. + server.on("/generate_204", HTTP_GET, [](AsyncWebServerRequest * request) + { + handleRoot(request); + }); + + //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler. + server.on("/fwlink", HTTP_GET, [](AsyncWebServerRequest * request) + { + handleRoot(request); + }); + + server.onNotFound(handleNotFound); + + server.begin(); // Web server start + + Serial.print(F("AsyncDNSServer is @ IP : ")); + Serial.println(apIP); +} + +void loop() +{ +} diff --git a/examples/AsyncCaptivePortal_ESP32_W6100/AsyncCaptivePortal_ESP32_W6100.ino b/examples/AsyncCaptivePortal_ESP32_W6100/AsyncCaptivePortal_ESP32_W6100.ino new file mode 100644 index 0000000..a23f412 --- /dev/null +++ b/examples/AsyncCaptivePortal_ESP32_W6100/AsyncCaptivePortal_ESP32_W6100.ino @@ -0,0 +1,171 @@ +/**************************************************************************************************************************** + AsyncCaptivePortal_ESP32_W6100.ino + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + *****************************************************************************************************************************/ + +#if !( defined(ESP32) ) + #error This code is designed for (ESP32 + W6100) to run on ESP32 platform! Please check your Tools->Board setting. +#endif + +#define ASYNC_DNS_ESP32_W6100_DEBUG_PORT Serial + +// Use from 0 to 4. Higher number, more debugging messages and memory usage. +#define _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ 1 + +///////////////////////////////////////////// + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; + +// Select the IP address according to your local network +IPAddress myIP(192, 168, 2, 232); +IPAddress myGW(192, 168, 2, 1); +IPAddress mySN(255, 255, 255, 0); + +// Google DNS Server IP +IPAddress myDNS(8, 8, 8, 8); + +////////////////////////////////////////////////////////// + +// Optional values to override default settings +// Don't change unless you know what you're doing +//#define ETH_SPI_HOST SPI3_HOST +//#define SPI_CLOCK_MHZ 25 + +// Must connect INT to GPIOxx or not working +//#define INT_GPIO 4 + +//#define MISO_GPIO 19 +//#define MOSI_GPIO 23 +//#define SCK_GPIO 18 +//#define CS_GPIO 5 + +////////////////////////////////////////////////////////// + +#include +#include + +const byte DNS_PORT = 53; + +IPAddress apIP; + +AsyncDNSServer dnsServer; +AsyncWebServer server(80); + +String responseHTML = "" + "" + "" + "ESP32_W6100-CaptivePortal" + "

Hello World from ESP32_W6100!

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

"; + +void handleNotFound(AsyncWebServerRequest *request) +{ + request->send(200, "text/html", responseHTML); +} + +void setup() +{ + Serial.begin(115200); + + while (!Serial && (millis() < 5000)); + + Serial.print(F("\nStart AsyncCaptivePortal_ESP32_W6100 on ")); + Serial.print(ARDUINO_BOARD); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_ESP32_W6100_VERSION); + Serial.println(ASYNC_UDP_ESP32_W6100_VERSION); + Serial.println(ASYNC_DNS_SERVER_ESP32_W6100_VERSION); + + Serial.setDebugOutput(true); + + UDP_LOGWARN(F("Default SPI pinout:")); + UDP_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST); + UDP_LOGWARN1(F("MOSI:"), MOSI_GPIO); + UDP_LOGWARN1(F("MISO:"), MISO_GPIO); + UDP_LOGWARN1(F("SCK:"), SCK_GPIO); + UDP_LOGWARN1(F("CS:"), CS_GPIO); + UDP_LOGWARN1(F("INT:"), INT_GPIO); + UDP_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); + UDP_LOGWARN(F("=========================")); + + /////////////////////////////////// + + // To be called before ETH.begin() + ESP32_W6100_onEvent(); + + // start the ethernet connection and the server: + // Use DHCP dynamic IP and random mac + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, + // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac); + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST ); + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] ); + + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + //ETH.config(myIP, myGW, mySN, myDNS); + + ESP32_W6100_waitForConnect(); + + /////////////////////////////////// + + apIP = ETH.localIP(); + + /////////////////////////////////// + + // 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 AsyncDNSReplyCode::NonExistentDomain + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); + + // if DNSServer is started with "*" for domain name, it will reply with + // provided IP to all DNS request + dnsServer.start(DNS_PORT, "*", apIP); + + server.onNotFound(handleNotFound); + + server.begin(); + + Serial.print(F("AsyncDNSServer is @ IP : ")); + Serial.println(apIP); +} + +void loop() +{ +} diff --git a/examples/AsyncDNSServer_ESP32_W6100/AsyncDNSServer_ESP32_W6100.ino b/examples/AsyncDNSServer_ESP32_W6100/AsyncDNSServer_ESP32_W6100.ino new file mode 100644 index 0000000..4caa9ad --- /dev/null +++ b/examples/AsyncDNSServer_ESP32_W6100/AsyncDNSServer_ESP32_W6100.ino @@ -0,0 +1,168 @@ +/**************************************************************************************************************************** + AsyncDNSServer_ESP32_W6100.ino + + AFor ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + *****************************************************************************************************************************/ + +#if !( defined(ESP32) ) + #error This code is designed for (ESP32 + W6100) to run on ESP32 platform! Please check your Tools->Board setting. +#endif + +#define ASYNC_DNS_ESP32_W6100_DEBUG_PORT Serial + +// Use from 0 to 4. Higher number, more debugging messages and memory usage. +#define _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ 1 + +///////////////////////////////////////////// + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; + +// Select the IP address according to your local network +IPAddress myIP(192, 168, 2, 232); +IPAddress myGW(192, 168, 2, 1); +IPAddress mySN(255, 255, 255, 0); + +// Google DNS Server IP +IPAddress myDNS(8, 8, 8, 8); + +////////////////////////////////////////////////////////// + +// Optional values to override default settings +// Don't change unless you know what you're doing +//#define ETH_SPI_HOST SPI3_HOST +//#define SPI_CLOCK_MHZ 25 + +// Must connect INT to GPIOxx or not working +//#define INT_GPIO 4 + +//#define MISO_GPIO 19 +//#define MOSI_GPIO 23 +//#define SCK_GPIO 18 +//#define CS_GPIO 5 + +////////////////////////////////////////////////////////// + +#include +#include + +const byte DNS_PORT = 53; + +IPAddress apIP; + +AsyncDNSServer dnsServer; + +AsyncWebServer server(80); + +void handleNotFound(AsyncWebServerRequest *request) +{ + String message = "Hello World from " + String(ARDUINO_BOARD); + message += "\nURI: "; + message += request->url(); + + request->send(200, "text/plain", message); +} + + +void setup() +{ + Serial.begin(115200); + + while (!Serial && (millis() < 5000)); + + Serial.print(F("\nStart AsyncDNSServer_ESP32_W6100 on ")); + Serial.print(ARDUINO_BOARD); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_ESP32_W6100_VERSION); + Serial.println(ASYNC_UDP_ESP32_W6100_VERSION); + Serial.println(ASYNC_DNS_SERVER_ESP32_W6100_VERSION); + + Serial.setDebugOutput(true); + + UDP_LOGWARN(F("Default SPI pinout:")); + UDP_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST); + UDP_LOGWARN1(F("MOSI:"), MOSI_GPIO); + UDP_LOGWARN1(F("MISO:"), MISO_GPIO); + UDP_LOGWARN1(F("SCK:"), SCK_GPIO); + UDP_LOGWARN1(F("CS:"), CS_GPIO); + UDP_LOGWARN1(F("INT:"), INT_GPIO); + UDP_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); + UDP_LOGWARN(F("=========================")); + + /////////////////////////////////// + + // To be called before ETH.begin() + ESP32_W6100_onEvent(); + + // start the ethernet connection and the server: + // Use DHCP dynamic IP and random mac + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, + // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac); + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST ); + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] ); + + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + //ETH.config(myIP, myGW, mySN, myDNS); + + ESP32_W6100_waitForConnect(); + + /////////////////////////////////// + + apIP = ETH.localIP(); + + /////////////////////////////////// + + // 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 AsyncDNSReplyCode::NonExistentDomain + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); + + // start DNS server for a specific domain name + dnsServer.start(DNS_PORT, "*", apIP); + + server.onNotFound(handleNotFound); + + server.begin(); + + Serial.print(F("AsyncDNSServer is @ IP : ")); + Serial.println(apIP); +} + +void loop() +{ +} diff --git a/examples/AsyncDNServerFull_ESP32_W6100/AsyncDNServerFull_ESP32_W6100.ino b/examples/AsyncDNServerFull_ESP32_W6100/AsyncDNServerFull_ESP32_W6100.ino new file mode 100644 index 0000000..49a9ab2 --- /dev/null +++ b/examples/AsyncDNServerFull_ESP32_W6100/AsyncDNServerFull_ESP32_W6100.ino @@ -0,0 +1,174 @@ +/**************************************************************************************************************************** + AsyncDNSServerFull_ESP32_W6100.ino + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + *****************************************************************************************************************************/ + +#if !( defined(ESP32) ) + #error This code is designed for (ESP32 + W6100) to run on ESP32 platform! Please check your Tools->Board setting. +#endif + +#define ASYNC_DNS_ESP32_W6100_DEBUG_PORT Serial + +// Use from 0 to 4. Higher number, more debugging messages and memory usage. +#define _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ 1 + +///////////////////////////////////////////// + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, +}; + +// Select the IP address according to your local network +IPAddress myIP(192, 168, 2, 232); +IPAddress myGW(192, 168, 2, 1); +IPAddress mySN(255, 255, 255, 0); + +// Google DNS Server IP +IPAddress myDNS(8, 8, 8, 8); + +////////////////////////////////////////////////////////// + +// Optional values to override default settings +// Don't change unless you know what you're doing +//#define ETH_SPI_HOST SPI3_HOST +//#define SPI_CLOCK_MHZ 25 + +// Must connect INT to GPIOxx or not working +//#define INT_GPIO 4 + +//#define MISO_GPIO 19 +//#define MOSI_GPIO 23 +//#define SCK_GPIO 18 +//#define CS_GPIO 5 + +////////////////////////////////////////////////////////// + +#include +#include + +const byte DNS_PORT = 53; + +IPAddress apIP; + +AsyncDNSServer dnsServer; + +AsyncWebServer server(80); + +void handleNotFound(AsyncWebServerRequest *request) +{ + String message = "Hello World from " + String(BOARD_NAME); + message += "\nURI: "; + message += request->url(); + + request->send(200, "text/plain", message); +} + +void setup() +{ + Serial.begin(115200); + + while (!Serial && (millis() < 5000)); + + Serial.print(F("\nStart AsyncDNSServerFull_ESP32_W6100 on ")); + Serial.print(ARDUINO_BOARD); + Serial.print(F(" with ")); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_ESP32_W6100_VERSION); + Serial.println(ASYNC_UDP_ESP32_W6100_VERSION); + Serial.println(ASYNC_DNS_SERVER_ESP32_W6100_VERSION); + + Serial.setDebugOutput(true); + + UDP_LOGWARN(F("Default SPI pinout:")); + UDP_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST); + UDP_LOGWARN1(F("MOSI:"), MOSI_GPIO); + UDP_LOGWARN1(F("MISO:"), MISO_GPIO); + UDP_LOGWARN1(F("SCK:"), SCK_GPIO); + UDP_LOGWARN1(F("CS:"), CS_GPIO); + UDP_LOGWARN1(F("INT:"), INT_GPIO); + UDP_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); + UDP_LOGWARN(F("=========================")); + + /////////////////////////////////// + + // To be called before ETH.begin() + ESP32_W6100_onEvent(); + + // start the ethernet connection and the server: + // Use DHCP dynamic IP and random mac + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, + // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac); + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST ); + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] ); + + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + //ETH.config(myIP, myGW, mySN, myDNS); + + ESP32_W6100_waitForConnect(); + + /////////////////////////////////// + + apIP = ETH.localIP(); + + /////////////////////////////////// + + // 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 AsyncDNSReplyCode::NonExistentDomain + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); + + // start DNS server for a specific domain name + dnsServer.start(DNS_PORT, "*", apIP); + + // simple HTTP server to see that DNS server is working + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) + { + request->send(200, "text/plain", "Hello from AsyncDNSServer running on " + String(BOARD_NAME)); + }); + + server.onNotFound(handleNotFound); + + server.begin(); + + Serial.print(F("AsyncDNSServer is @ IP : ")); + Serial.println(apIP); +} + +void loop() +{ +} diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..8277e68 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,36 @@ +####################################### +# Syntax Coloring Map For Ultrasound +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +DNSHeader KEYWORD1 +AsyncDNSServer KEYWORD1 +AsyncDNSReplyCode KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +################### +# General +################### + +requestIncludesOnlyOneQuestion KEYWORD2 +downcaseAndRemoveWwwPrefix KEYWORD2 +getDomainNameWithoutWwwPrefix KEYWORD2 + +################### +# AsyncDNSServer +################### + +setErrorReplyCode KEYWORD2 +setTTL KEYWORD2 +start KEYWORD2 +stop KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/library.json b/library.json new file mode 100644 index 0000000..729b866 --- /dev/null +++ b/library.json @@ -0,0 +1,52 @@ +{ + "name":"AsyncDNSServer_ESP32_W6100", + "version": "1.0.0", + "description":"Fully Asynchronous DNS Server Library for ESP32 boards using LwIP W6100 Ethernet. This library is one of the current or future and more advanced Async libraries, such as AsyncWebServer_ESP32_W6100, AsyncHTTPRequest_ESP32_W6100, AsyncHTTPSRequest_ESP32_W6100", + "keywords":"communication, data, async, udp, ntp, time, time-server, server, client, multicast, broadcast, webserver, esp32, w6100, lwip, lwip-w6100, udp-server, udp-multicast-server, dns-server, captive-portal", + "authors": + [ + { + "name": "Develo", + "url": "https://github.com/devyte" + }, + { + "name": "Khoi Hoang", + "url": "https://github.com/khoih-prog", + "email": "khoih.prog@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100" + }, + "homepage": "https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100", + "export": { + "exclude": [ + "linux", + "extras", + "tests" + ] + }, + "dependencies": + [ + { + "owner": "khoih-prog", + "name": "WebServer_ESP32_W6100", + "version": "^1.5.2", + "platforms": "espressif32" + }, + { + "owner": "khoih-prog", + "name": "AsyncUDP_ESP32_W6100", + "version": "^2.0.0", + "platforms": "espressif32" + } + ], + "license": "LGPL-3.0", + "frameworks": "*", + "platforms": ["espressif32"], + "examples": "examples/*/*/*.ino", + "headers": [ "AsyncDNSServer_ESP32_W6100.h", "AsyncDNSServer_ESP32_W6100.hpp" ] +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..a4e9e56 --- /dev/null +++ b/library.properties @@ -0,0 +1,13 @@ +name=AsyncDNSServer_ESP32_W6100 +version=1.0.0 +author=Develo,Khoi Hoang +maintainer=Khoi Hoang +sentence=Fully Asynchronous DNS Server Library for ESP32 boards using LwIP W6100 Ethernet. +paragraph=This library is one of the current or future and more advanced Async libraries, such as AsyncWebServer_ESP32_W6100, AsyncHTTPRequest_ESP32_W6100, AsyncHTTPSRequest_ESP32_W6100 +category=Communication +url=https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 +repository=https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 +architectures=esp32 +depends=WebServer_ESP32_W6100, AsyncUDP_ESP32_W6100 +license=GPLv3 +includes=AsyncDNSServer_ESP32_W6100.h, AsyncDNSServer_ESP32_W6100.hpp diff --git a/platformio/platformio.ini b/platformio/platformio.ini new file mode 100644 index 0000000..32e90e1 --- /dev/null +++ b/platformio/platformio.ini @@ -0,0 +1,164 @@ +;PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +; ============================================================ +; chose environment: +; ESP32 +; esp32s2 +; esp32s3 +; esp32c3 + +; ============================================================ +default_envs = ESP32 + +[env] +; ============================================================ +; Serial configuration +; choose upload speed, serial-monitor speed +; ============================================================ +upload_speed = 921600 +;upload_port = COM11 +;monitor_speed = 9600 +;monitor_port = COM11 + +; ============================================================ +; Checks for the compatibility with frameworks and dev/platforms +lib_compat_mode = strict +lib_ldf_mode = chain+ +;lib_ldf_mode = deep+ + +; ============================================================ +lib_deps = +; PlatformIO 4.x +; WebServer_ESP32_W6100@~1.5.2 +; AsyncUdp_ESP32_W6100@~2.0.0 +; PlatformIO 5.x + khoih-prog/WebServer_ESP32_W6100@~1.5.2 + khoih-prog/AsyncUdp_ESP32_W6100@~2.0.0 + +; ============================================================ +build_flags = +; set your debug output (default=Serial) + -D DEBUG_ESP_PORT=Serial +; comment the following line to enable WiFi debugging + -D NDEBUG + +; ============================================================ +[env:ESP32] +platform = espressif32 +framework = arduino + +; ============================================================ +; Board configuration +; choose your board by uncommenting one of the following lines +; ============================================================ +;board = esp32cam +;board = alksesp32 +;board = featheresp32 +;board = espea32 +;board = bpi-bit +;board = d-duino-32 +board = esp32doit-devkit-v1 +;board = pocket_32 +;board = fm-devkit +;board = pico32 +;board = esp32-evb +;board = esp32-gateway +;board = esp32-pro +;board = esp32-poe +;board = oroca_edubot +;board = onehorse32dev +;board = lopy +;board = lopy4 +;board = wesp32 +;board = esp32thing +;board = sparkfun_lora_gateway_1-channel +;board = ttgo-lora32-v1 +;board = ttgo-t-beam +;board = turta_iot_node +;board = lolin_d32 +;board = lolin_d32_pro +;board = lolin32 +;board = wemosbat +;board = widora-air +;board = xinabox_cw02 +;board = iotbusio +;board = iotbusproteus +;board = nina_w10 + +; ============================================================ +[env:esp32s2] +platform = espressif32 +framework = arduino + +; toolchain download links see +; refer "name": "xtensa-esp32s2-elf-gcc","version": "gcc8_4_0-esp-2021r1" section of +; https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json +; e.g. Windows: https://github.com/espressif/crosstool-NG/releases/download/esp-2021r1/xtensa-esp32s2-elf-gcc8_4_0-esp-2021r1-win32.zip +platform_packages = + toolchain-xtensa32s2@file://C:\Users\Max\Downloads\xtensa-esp32s2-elf + framework-arduinoespressif32@https://github.com/espressif/arduino-esp32.git#a4118ea88987c28aac3a49bcb9cc5d6c0acc6f3f + platformio/tool-esptoolpy @ ~1.30100 +framework = arduino +board = esp32dev +board_build.mcu = esp32s2 +board_build.partitions = huge_app.csv +board_build.variant = esp32s2 +board_build.f_cpu = 240000000L +board_build.f_flash = 80000000L +board_build.flash_mode = qio +board_build.arduino.ldscript = esp32s2_out.ld +build_unflags = + -DARDUINO_ESP32_DEV + -DARDUINO_VARIANT="esp32" +build_flags = + -DARDUINO_ESP32S2_DEV + -DARDUINO_VARIANT="esp32s2" + +; ============================================================ +[env:esp32s3] +platform = espressif32 +framework = arduino + +board_build.mcu = esp32s3 +board_build.partitions = huge_app.csv +board_build.variant = esp32s3 +board_build.f_cpu = 240000000L +board_build.f_flash = 80000000L +board_build.flash_mode = qio +board_build.arduino.ldscript = esp32s3_out.ld +build_unflags = + -DARDUINO_ESP32_DEV + -DARDUINO_VARIANT="esp32" +build_flags = + -DARDUINO_ESP32S3_DEV + -DARDUINO_VARIANT="esp32s3" + +; ============================================================ +[env:esp32sc3] +platform = espressif32 +framework = arduino + +board_build.mcu = esp32c3 +board_build.partitions = huge_app.csv +board_build.variant = esp32c3 +board_build.f_cpu = 160000000L +board_build.f_flash = 80000000L +board_build.flash_mode = qio +board_build.arduino.ldscript = esp32c3_out.ld +build_unflags = + -DARDUINO_ESP32_DEV + -DARDUINO_VARIANT="esp32" +build_flags = + -DARDUINO_ESP32S3_DEV + -DARDUINO_VARIANT="esp32c3" + +; ============================================================ diff --git a/src/AsyncDNSServer_ESP32_W6100.h b/src/AsyncDNSServer_ESP32_W6100.h new file mode 100644 index 0000000..3a0d3de --- /dev/null +++ b/src/AsyncDNSServer_ESP32_W6100.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + AsyncDNSServer_ESP32_W6100.h + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 10/01/2023 Initial coding for ESP32 + LwIP W6100. Sync with AsyncDNSServer_STM32 + *****************************************************************************************************************************/ + +#pragma once + +#ifndef AsyncDNSServer_ESP32_W6100_h +#define AsyncDNSServer_ESP32_W6100_h + +//////////////////////////////////////////////// + +#define ASYNC_DNS_SERVER_ESP32_W6100_VERSION "AsyncDNSServer_ESP32_W6100 v1.0.0" + +//////////////////////////////////////////////// + +#include +#include "AsyncDNSServer_ESP32_W6100_Debug.h" + +#define DNS_QR_QUERY 0 +#define DNS_QR_RESPONSE 1 +#define DNS_OPCODE_QUERY 0 + +//////////////////////////////////////////////// + +enum class AsyncDNSReplyCode : unsigned char +{ + NoError = 0, + FormError = 1, + ServerFailure = 2, + NonExistentDomain = 3, + NotImplemented = 4, + Refused = 5, + YXDomain = 6, + YXRRSet = 7, + NXRRSet = 8 +}; + +//////////////////////////////////////////////// + +class AsyncDNSServer +{ + public: + AsyncDNSServer(); + void setErrorReplyCode(const AsyncDNSReplyCode &replyCode); + void setTTL(const uint32_t ttl); + + // Returns true if successful, false if there are no sockets available + bool start(const uint16_t port, const String &domainName, const IPAddress &resolvedIP); + + // stops the DNS server + void stop(); + + private: + AsyncUDP _udp; + uint16_t _port; + String _domainName; + unsigned char _resolvedIP[4]; + uint32_t _ttl; + AsyncDNSReplyCode _errorReplyCode; + + void processRequest(AsyncUDPPacket &packet); + void replyWithIP(AsyncUDPPacket &packet); + void replyWithCustomCode(AsyncUDPPacket &packet); +}; + +//////////////////////////////////////////////// + +#include "AsyncDNSServer_ESP32_W6100_Impl.h" + +#endif // AsyncDNSServer_ESP32_W6100_h diff --git a/src/AsyncDNSServer_ESP32_W6100_Debug.h b/src/AsyncDNSServer_ESP32_W6100_Debug.h new file mode 100644 index 0000000..97d6f0a --- /dev/null +++ b/src/AsyncDNSServer_ESP32_W6100_Debug.h @@ -0,0 +1,95 @@ +/**************************************************************************************************************************** + AsyncDNSServer_ESP32_W6100_Debug.h + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 10/01/2023 Initial coding for ESP32 + LwIP W6100. Sync with AsyncDNSServer_STM32 + *****************************************************************************************************************************/ + +#pragma once + +#ifndef AsyncDNSServer_ESP32_W6100_Debug_H +#define AsyncDNSServer_ESP32_W6100_Debug_H + +#ifdef ASYNC_DNS_ESP32_W6100_DEBUG_PORT + #define DBG_PORT_ASYNC_DNS ASYNC_DNS_ESP32_W6100_DEBUG_PORT +#else + #define DBG_PORT_ASYNC_DNS Serial +#endif + +///////////////////////////////////////////////////////// + +// Change _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ to set tracing and logging verbosity +// 0: DISABLED: no logging +// 1: ERROR: errors +// 2: WARN: errors and warnings +// 3: INFO: errors, warnings and informational (default) +// 4: DEBUG: errors, warnings, informational and debug + +#ifndef _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ + #define _ASYNC_DNS_ESP32_W6100_LOGLEVEL_ 0 +#endif + +///////////////////////////////////////////////////////// + +const char ASYNC_DNS_MARK[] = "[DNS] "; + +#define ASYNC_DNS_PRINT DBG_PORT_ASYNC_DNS.print +#define ASYNC_DNS_PRINTLN DBG_PORT_ASYNC_DNS.println + +#define ASYNC_DNS_PRINT_MARK ASYNC_DNS_PRINT(ASYNC_DNS_MARK) +#define ASYNC_DNS_PRINT_SP DBG_PORT_ASYNC_DNS.print(" ") + +/////////////////////////////////////// + +#define DNS_LOG(x) { ASYNC_DNS_PRINTLN(x); } +#define DNS_LOG0(x) { ASYNC_DNS_PRINT(x); } +#define DNS_LOG1(x,y) { ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINTLN(y); } +#define DNS_LOG2(x,y,z) { ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINTLN(z); } +#define DNS_LOG3(x,y,z,w) { ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT(z); ASYNC_DNS_PRINTLN(w); } + +///////////////////////////////////////////////////////// + +#define DNS_LOGERROR(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>0) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINTLN(x); } +#define DNS_LOGERROR0(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>0) { ASYNC_DNS_PRINT(x); } +#define DNS_LOGERROR1(x,y) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>0) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(y); } +#define DNS_LOGERROR2(x,y,z) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>0) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(z); } +#define DNS_LOGERROR3(x,y,z,w) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>0) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(z); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(w); } + +///////////////////////////////////////////////////////// + +#define DNS_LOGWARN(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>1) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINTLN(x); } +#define DNS_LOGWARN0(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>1) { ASYNC_DNS_PRINT(x); } +#define DNS_LOGWARN1(x,y) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>1) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(y); } +#define DNS_LOGWARN2(x,y,z) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>1) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(z); } +#define DNS_LOGWARN3(x,y,z,w) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>1) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(z); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(w); } + +///////////////////////////////////////////////////////// + +#define DNS_LOGINFO(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>2) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINTLN(x); } +#define DNS_LOGINFO0(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>2) { ASYNC_DNS_PRINT(x); } +#define DNS_LOGINFO1(x,y) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>2) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(y); } +#define DNS_LOGINFO2(x,y,z) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>2) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(z); } +#define DNS_LOGINFO3(x,y,z,w) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>2) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(z); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(w); } + +///////////////////////////////////////////////////////// + +#define DNS_LOGDEBUG(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>3) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINTLN(x); } +#define DNS_LOGDEBUG0(x) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>3) { ASYNC_DNS_PRINT(x); } +#define DNS_LOGDEBUG1(x,y) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>3) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(y); } +#define DNS_LOGDEBUG2(x,y,z) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>3) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(z); } +#define DNS_LOGDEBUG3(x,y,z,w) if(_ASYNC_DNS_ESP32_W6100_LOGLEVEL_>3) { ASYNC_DNS_PRINT_MARK; ASYNC_DNS_PRINT(x); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(y); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINT(z); ASYNC_DNS_PRINT_SP; ASYNC_DNS_PRINTLN(w); } + +///////////////////////////////////////////////////////// + +#endif //AsyncDNSServer_ESP32_W6100_Debug_H diff --git a/src/AsyncDNSServer_ESP32_W6100_Impl.h b/src/AsyncDNSServer_ESP32_W6100_Impl.h new file mode 100644 index 0000000..b88248a --- /dev/null +++ b/src/AsyncDNSServer_ESP32_W6100_Impl.h @@ -0,0 +1,248 @@ +/**************************************************************************************************************************** + AsyncDNSServer_ESP32_W6100_Impl.h + + For ESP32_W6100 (ESP32 + W6100) + + AsyncDNSServer_ESP32_W6100 is a Async DNS Server library for the ESP32_W6100 + + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_ESP32_W6100 + Licensed under GPLv3 license + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 10/01/2023 Initial coding for ESP32 + LwIP W6100. Sync with AsyncDNSServer_STM32 + *****************************************************************************************************************************/ + +#pragma once + +#ifndef AsyncDNSServer_ESP32_W6100_Impl_h +#define AsyncDNSServer_ESP32_W6100_Impl_h + +#include + +extern "C" +{ + #include +}; + +//////////////////////////////////////////////// + +namespace +{ +struct DNSHeader +{ + uint16_t ID; // identification number + unsigned char RD : 1; // recursion desired + unsigned char TC : 1; // truncated message + unsigned char AA : 1; // authoritive answer + unsigned char OPCode : 4; // message_type + unsigned char QR : 1; // query/response flag + unsigned char RCode : 4; // response code + unsigned char Z : 3; // its z! reserved + unsigned 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 +}; + +//////////////////////////////////////////////// + +bool requestIncludesOnlyOneQuestion(DNSHeader * _dnsHeader) +{ + return ntohs(_dnsHeader->QDCount) == 1 && + _dnsHeader->ANCount == 0 && + _dnsHeader->NSCount == 0 && + _dnsHeader->ARCount == 0; +} + +//////////////////////////////////////////////// + +void downcaseAndRemoveWwwPrefix(String &domainName) +{ + domainName.toLowerCase(); + domainName.replace("www.", ""); +} + +//////////////////////////////////////////////// + +String getDomainNameWithoutWwwPrefix(unsigned char *start) +{ + String parsedDomainName = ""; + + if (start == nullptr || *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 += "."; + } + } +} +} + +///////////////////////////////////////////////////// + +AsyncDNSServer::AsyncDNSServer() +{ + _ttl = htonl(60); + _errorReplyCode = AsyncDNSReplyCode::NonExistentDomain; +} + +//////////////////////////////////////////////// + +bool AsyncDNSServer::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 (_udp.listen(_port)) + { + _udp.onPacket( + [&](AsyncUDPPacket & packet) + { + this->processRequest(packet); + } + ); + + return true; + } + + return false; +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::setErrorReplyCode(const AsyncDNSReplyCode &replyCode) +{ + _errorReplyCode = replyCode; +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::setTTL(const uint32_t ttl) +{ + _ttl = htonl(ttl); +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::stop() +{ + DNS_LOGDEBUG("Stop"); + + _udp.close(); +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::processRequest(AsyncUDPPacket &packet) +{ + if (packet.length() >= sizeof(DNSHeader)) + { + unsigned char * _buffer = packet.data(); + DNSHeader * _dnsHeader = (DNSHeader*) _buffer; + + String domainNameWithoutWwwPrefix = (_buffer == nullptr ? "" : getDomainNameWithoutWwwPrefix(_buffer + sizeof( + DNSHeader))); + + DNS_LOGDEBUG1("processRequest: domainNameWithoutWwwPrefix =", domainNameWithoutWwwPrefix); + + if (_dnsHeader->QR == DNS_QR_QUERY && _dnsHeader->OPCode == DNS_OPCODE_QUERY && + requestIncludesOnlyOneQuestion(_dnsHeader) && + (_domainName == "*" || domainNameWithoutWwwPrefix == _domainName) + ) + { + replyWithIP(packet); + } + else if (_dnsHeader->QR == DNS_QR_QUERY) + { + replyWithCustomCode(packet); + } + } +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::replyWithIP(AsyncUDPPacket &packet) +{ + //6 bytes below + szeof(ttl) + 2 bytes. Precalculate to avoid using default of 1460, which is way too much + AsyncUDPMessage msg(packet.length() + 12 + sizeof(_resolvedIP)); + + msg.write(packet.data(), packet.length()); + DNSHeader * _dnsHeader = (DNSHeader *)msg.data(); + + _dnsHeader->QR = DNS_QR_RESPONSE; + _dnsHeader->ANCount = _dnsHeader->QDCount; + _dnsHeader->QDCount = _dnsHeader->QDCount; + //_dnsHeader->RA = 1; + + msg.write((uint8_t)192); // answer name is a pointer + msg.write((uint8_t)12); // pointer to offset at 0x00c + + msg.write((uint8_t)0); // 0x0001 answer is type A query (host address) + msg.write((uint8_t)1); + + msg.write((uint8_t)0); //0x0001 answer is class IN (internet address) + msg.write((uint8_t)1); + + msg.write((uint8_t *)&_ttl, sizeof(_ttl)); + + // Length of RData is 4 bytes (because, in this case, RData is IPv4) + msg.write((uint8_t)0); + msg.write((uint8_t)4); + msg.write(_resolvedIP, sizeof(_resolvedIP)); + + packet.send(msg); + + DNS_LOGDEBUG1(F("replyWithIP: DNS responds: "), (IPAddress) _resolvedIP); +} + +//////////////////////////////////////////////// + +void AsyncDNSServer::replyWithCustomCode(AsyncUDPPacket &packet) +{ + AsyncUDPMessage msg(packet.length()); + + msg.write(packet.data(), packet.length()); + DNSHeader * _dnsHeader = (DNSHeader *)msg.data(); + + _dnsHeader->QR = DNS_QR_RESPONSE; + _dnsHeader->RCode = (unsigned char)_errorReplyCode; //default is AsyncDNSReplyCode::NonExistentDomain + _dnsHeader->QDCount = 0; + + packet.send(msg); +} + +//////////////////////////////////////////////// + + +#endif // AsyncDNSServer_ESP32_W6100_Impl_h diff --git a/utils/astyle_library.conf b/utils/astyle_library.conf new file mode 100644 index 0000000..8a73bc2 --- /dev/null +++ b/utils/astyle_library.conf @@ -0,0 +1,70 @@ +# Code formatting rules for Arduino libraries, modified from for KH libraries: +# +# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf +# + +# astyle --style=allman -s2 -t2 -C -S -xW -Y -M120 -f -p -xg -H -xb -c --xC120 -xL *.h *.cpp *.ino + +--mode=c +--lineend=linux +--style=allman + +# -r or -R +#--recursive + +# -c => Converts tabs into spaces +convert-tabs + +# -s2 => 2 spaces indentation +--indent=spaces=2 + +# -t2 => tab =2 spaces +#--indent=tab=2 + +# -C +--indent-classes + +# -S +--indent-switches + +# -xW +--indent-preproc-block + +# -Y => indent classes, switches (and cases), comments starting at column 1 +--indent-col1-comments + +# -M120 => maximum of 120 spaces to indent a continuation line +--max-continuation-indent=120 + +# -xC120 => max‑code‑length will break a line if the code exceeds # characters +--max-code-length=120 + +# -f => +--break-blocks + +# -p => put a space around operators +--pad-oper + +# -xg => Insert space padding after commas +--pad-comma + +# -H => put a space after if/for/while +pad-header + +# -xb => Break one line headers (e.g. if/for/while) +--break-one-line-headers + +# -c => Converts tabs into spaces +#--convert-tabs + +# if you like one-liners, keep them +#keep-one-line-statements + +# -xV +--attach-closing-while + +#unpad-paren + +# -xp +remove-comment-prefix + diff --git a/utils/restyle.sh b/utils/restyle.sh new file mode 100644 index 0000000..bcd846f --- /dev/null +++ b/utils/restyle.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for dir in . ; do + find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.ino" \) -exec astyle --suffix=none --options=./utils/astyle_library.conf \{\} \; +done +