diff --git a/.travis.yml b/.travis.yml index e6a2b4012..7c50c68de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,11 @@ language: bash os: linux -dist: trusty +dist: bionic git: depth: 1 submodules: false -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-7 - - gcc-7 - before_install: - git submodule update --init # no recursive update @@ -87,14 +79,14 @@ jobs: - name: "Host tests" stage: build script: $TRAVIS_BUILD_DIR/tests/ci/host_test.sh - install: sudo apt-get install valgrind lcov - env: CC=gcc-7 CXX=g++-7 + install: + - sudo apt-get install valgrind lcov - name: "Docs" stage: build script: $TRAVIS_BUILD_DIR/tests/ci/build_docs.sh install: - - sudo apt-get install python3-pip + - sudo apt-get install python3-pip python3-setuptools - pip3 install --user -r doc/requirements.txt; - name: "Style check" @@ -105,7 +97,6 @@ jobs: - name: "Mock trivial test" stage: build script: $TRAVIS_BUILD_DIR/tests/buildm.sh - env: CC=gcc-7 CXX=g++-7 - name: "Mac OSX can build sketches" os: osx diff --git a/README.md b/README.md index dbe51b4c6..22a12463a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ ESP8266 Arduino core comes with libraries to communicate over WiFi using TCP and # Contents - Installing options: - [Using Boards Manager](#installing-with-boards-manager) - - [Using git version](#using-git-version-basic-instructions) + - [Using git version](#using-git-version) - [Using PlatformIO](#using-platformio) - [Building with make](#building-with-make) - [Documentation](#documentation) @@ -38,35 +38,13 @@ Boards manager link: `https://arduino.esp8266.com/stable/package_esp8266com_inde Documentation: [https://arduino-esp8266.readthedocs.io/en/2.5.2/](https://arduino-esp8266.readthedocs.io/en/2.5.2/) -### Using git version (basic instructions) +### Using git version [![Linux build status](https://travis-ci.org/esp8266/Arduino.svg)](https://travis-ci.org/esp8266/Arduino) +Also known as latest git or master branch. + - Install the current upstream Arduino IDE at the 1.8 level or later. The current version is on the [Arduino website](https://www.arduino.cc/en/main/software). -- Go to Arduino directory - - For Mac OS X, it is `Arduino.app` showing as the Arduino icon. - This location may be your `~/Downloads`, `~/Desktop` or even `/Applications`. - ```bash - cd /Arduino.app/Contents/Java - ``` - - For Linux, it is ~/Arduino by default. - ```bash - cd ~/Arduino - ``` -- Clone this repository into hardware/esp8266com/esp8266 directory (or clone it elsewhere and create a symlink) -```bash -cd hardware -mkdir esp8266com -cd esp8266com -git clone https://github.com/esp8266/Arduino.git esp8266 -cd esp8266 -git submodule update --init -``` -- Download binary tools (you need Python 2.7) -```bash -cd esp8266/tools -python get.py -``` -- Restart Arduino +- Follow the [instructions in the documentation](https://arduino-esp8266.readthedocs.io/en/latest/installing.html#using-git-version). ### Using PlatformIO diff --git a/boards.txt b/boards.txt index ff7f8e663..e8e009f5f 100644 --- a/boards.txt +++ b/boards.txt @@ -76,6 +76,10 @@ generic.menu.FlashFreq.40=40MHz generic.menu.FlashFreq.40.build.flash_freq=40 generic.menu.FlashFreq.80=80MHz generic.menu.FlashFreq.80.build.flash_freq=80 +generic.menu.FlashFreq.20=20MHz +generic.menu.FlashFreq.20.build.flash_freq=20 +generic.menu.FlashFreq.26=26MHz +generic.menu.FlashFreq.26.build.flash_freq=26 generic.menu.FlashMode.dout=DOUT (compatible) generic.menu.FlashMode.dout.build.flash_mode=dout generic.menu.FlashMode.dout.build.flash_flags=-DFLASHMODE_DOUT @@ -370,11 +374,13 @@ generic.menu.led.15=15 generic.menu.led.15.build.led=-DLED_BUILTIN=15 generic.menu.led.16=16 generic.menu.led.16.build.led=-DLED_BUILTIN=16 -generic.menu.sdk.nonosdk222_100=nonos-sdk 2.2.1+100 (testing) -generic.menu.sdk.nonosdk222_100.build.sdk=NONOSDK22y +generic.menu.sdk.nonosdk222_100=nonos-sdk 2.2.1+100 (190703 approved) +generic.menu.sdk.nonosdk222_100.build.sdk=NONOSDK22x_190703 +generic.menu.sdk.nonosdk222_111=nonos-sdk 2.2.1+111 (191024 testing) +generic.menu.sdk.nonosdk222_111.build.sdk=NONOSDK22x_191024 generic.menu.sdk.nonosdk221=nonos-sdk 2.2.1 (legacy) generic.menu.sdk.nonosdk221.build.sdk=NONOSDK221 -generic.menu.sdk.nonosdk3v0=nonos-sdk pre-3 (known issues) +generic.menu.sdk.nonosdk3v0=nonos-sdk pre-3 (180626 known issues) generic.menu.sdk.nonosdk3v0.build.sdk=NONOSDK3V0 generic.menu.ip.lm2f=v2 Lower Memory generic.menu.ip.lm2f.build.lwip_include=lwip2/include @@ -4758,6 +4764,10 @@ wifinfo.menu.FlashFreq.40=40MHz wifinfo.menu.FlashFreq.40.build.flash_freq=40 wifinfo.menu.FlashFreq.80=80MHz wifinfo.menu.FlashFreq.80.build.flash_freq=80 +wifinfo.menu.FlashFreq.20=20MHz +wifinfo.menu.FlashFreq.20.build.flash_freq=20 +wifinfo.menu.FlashFreq.26=26MHz +wifinfo.menu.FlashFreq.26.build.flash_freq=26 wifinfo.menu.eesz.1M64=1MB (FS:64KB OTA:~470KB) wifinfo.menu.eesz.1M64.build.flash_size=1M wifinfo.menu.eesz.1M64.build.flash_size_bytes=0x100000 @@ -5778,6 +5788,10 @@ wifi_slot.menu.FlashFreq.40=40MHz wifi_slot.menu.FlashFreq.40.build.flash_freq=40 wifi_slot.menu.FlashFreq.80=80MHz wifi_slot.menu.FlashFreq.80.build.flash_freq=80 +wifi_slot.menu.FlashFreq.20=20MHz +wifi_slot.menu.FlashFreq.20.build.flash_freq=20 +wifi_slot.menu.FlashFreq.26=26MHz +wifi_slot.menu.FlashFreq.26.build.flash_freq=26 wifi_slot.menu.FlashMode.dout=DOUT (compatible) wifi_slot.menu.FlashMode.dout.build.flash_mode=dout wifi_slot.menu.FlashMode.dout.build.flash_flags=-DFLASHMODE_DOUT diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 82a183412..54919ba10 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -275,17 +275,16 @@ long secureRandom(long); long secureRandom(long, long); long map(long, long, long, long, long); -extern "C" void configTime(long timezone, int daylightOffset_sec, - const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); +void configTime(int timezone, int daylightOffset_sec, const char* server1, + const char* server2 = nullptr, const char* server3 = nullptr); -#endif +void configTime(const char* tz, const char* server1, + const char* server2 = nullptr, const char* server3 = nullptr); + +#endif // __cplusplus #include "pins_arduino.h" -#ifndef PUYA_SUPPORT -#define PUYA_SUPPORT 1 -#endif - #endif #ifdef DEBUG_ESP_OOM diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index a21dc4bc0..29222d6df 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "Arduino.h" +#include "Esp.h" #include "flash_utils.h" #include "eboot_command.h" #include @@ -36,6 +36,14 @@ extern struct rst_info resetInfo; //#define DEBUG_SERIAL Serial +#ifndef PUYA_SUPPORT + #define PUYA_SUPPORT 1 +#endif +#ifndef PUYA_BUFFER_SIZE + // Good alternative for buffer size is: SPI_FLASH_SEC_SIZE (= 4k) + // Always use a multiple of flash page size (256 bytes) + #define PUYA_BUFFER_SIZE 256 +#endif /** * User-defined Literals @@ -494,7 +502,7 @@ uint32_t EspClass::getSketchSize() { image_header_t image_header; uint32_t pos = APP_START_OFFSET; - if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header))) { + if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header)) != SPI_FLASH_RESULT_OK) { return 0; } pos += sizeof(image_header); @@ -506,7 +514,7 @@ uint32_t EspClass::getSketchSize() { ++section_index) { section_header_t section_header = {0, 0}; - if (spi_flash_read(pos, (uint32_t*) §ion_header, sizeof(section_header))) { + if (spi_flash_read(pos, (uint32_t*) §ion_header, sizeof(section_header)) != SPI_FLASH_RESULT_OK) { return 0; } pos += sizeof(section_header); @@ -577,26 +585,27 @@ bool EspClass::flashEraseSector(uint32_t sector) { } #if PUYA_SUPPORT -static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) { +static SpiFlashOpResult spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) { if (data == nullptr) { - return 1; // SPI_FLASH_RESULT_ERR + return SPI_FLASH_RESULT_ERR; } // PUYA flash chips need to read existing data, update in memory and write modified data again. static uint32_t *flash_write_puya_buf = nullptr; - int rc = 0; - uint32_t* ptr = data; if (flash_write_puya_buf == nullptr) { flash_write_puya_buf = (uint32_t*) malloc(PUYA_BUFFER_SIZE); // No need to ever free this, since the flash chip will never change at runtime. if (flash_write_puya_buf == nullptr) { // Memory could not be allocated. - return 1; // SPI_FLASH_RESULT_ERR + return SPI_FLASH_RESULT_ERR; } } + + SpiFlashOpResult rc = SPI_FLASH_RESULT_OK; + uint32_t* ptr = data; size_t bytesLeft = size; uint32_t pos = offset; - while (bytesLeft > 0 && rc == 0) { + while (bytesLeft > 0 && rc == SPI_FLASH_RESULT_OK) { size_t bytesNow = bytesLeft; if (bytesNow > PUYA_BUFFER_SIZE) { bytesNow = PUYA_BUFFER_SIZE; @@ -605,7 +614,7 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) { bytesLeft = 0; } rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow); - if (rc != 0) { + if (rc != SPI_FLASH_RESULT_OK) { return rc; } for (size_t i = 0; i < bytesNow / 4; ++i) { @@ -620,7 +629,7 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) { #endif bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) { - int rc = 0; + SpiFlashOpResult rc = SPI_FLASH_RESULT_OK; #if PUYA_SUPPORT if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA) { rc = spi_flash_write_puya(offset, data, size); @@ -630,12 +639,12 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) { { rc = spi_flash_write(offset, data, size); } - return rc == 0; + return rc == SPI_FLASH_RESULT_OK; } bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) { - int rc = spi_flash_read(offset, (uint32_t*) data, size); - return rc == 0; + auto rc = spi_flash_read(offset, (uint32_t*) data, size); + return rc == SPI_FLASH_RESULT_OK; } String EspClass::getSketchMD5() diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index 33c7f10ad..726633d61 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -23,15 +23,6 @@ #include -#ifndef PUYA_SUPPORT - #define PUYA_SUPPORT 0 -#endif -#ifndef PUYA_BUFFER_SIZE - // Good alternative for buffer size is: SPI_FLASH_SEC_SIZE (= 4k) - // Always use a multiple of flash page size (256 bytes) - #define PUYA_BUFFER_SIZE 256 -#endif - // Vendor IDs taken from Flashrom project // https://review.coreboot.org/cgit/flashrom.git/tree/flashchips.h?h=1.0.x typedef enum { diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 1be050e8c..129b55eda 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -1,166 +1,168 @@ -/* - HardwareSerial.cpp - esp8266 UART support - - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - 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 - - Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266) - Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266) - Modified 3 May 2015 by Hristo Gochkov (change register access methods) - */ - -#include -#include -#include -#include -#include -#include "Arduino.h" -#include "HardwareSerial.h" -#include "Esp.h" - -HardwareSerial::HardwareSerial(int uart_nr) - : _uart_nr(uart_nr), _rx_size(256) -{} - -void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) -{ - end(); - _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); -#if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) - if (static_cast(this) == static_cast(&DEBUG_ESP_PORT)) - { - setDebugOutput(true); - println(); - println(ESP.getFullVersion()); - } -#endif -} - -void HardwareSerial::end() -{ - if(uart_get_debug() == _uart_nr) { - uart_set_debug(UART_NO); - } - - uart_uninit(_uart); - _uart = NULL; -} - -void HardwareSerial::updateBaudRate(unsigned long baud) -{ - if(!_uart) { - return; - } - - uart_set_baudrate(_uart, baud); -} - -size_t HardwareSerial::setRxBufferSize(size_t size){ - if(_uart) { - _rx_size = uart_resize_rx_buffer(_uart, size); - } else { - _rx_size = size; - } - return _rx_size; -} - -void HardwareSerial::setDebugOutput(bool en) -{ - if(!_uart) { - return; - } - if(en) { - if(uart_tx_enabled(_uart)) { - uart_set_debug(_uart_nr); - } else { - uart_set_debug(UART_NO); - } - } else { - // disable debug for this interface - if(uart_get_debug() == _uart_nr) { - uart_set_debug(UART_NO); - } - } -} - -int HardwareSerial::available(void) -{ - int result = static_cast(uart_rx_available(_uart)); - if (!result) { - optimistic_yield(10000); - } - return result; -} - -void HardwareSerial::flush() -{ - if(!_uart || !uart_tx_enabled(_uart)) { - return; - } - - uart_wait_tx_empty(_uart); - //Workaround for a bug in serial not actually being finished yet - //Wait for 8 data bits, 1 parity and 2 stop bits, just in case - delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1); -} - -void HardwareSerial::startDetectBaudrate() -{ - uart_start_detect_baudrate(_uart_nr); -} - -unsigned long HardwareSerial::testBaudrate() -{ - return uart_detect_baudrate(_uart_nr); -} - -unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis) -{ - esp8266::polledTimeout::oneShotFastMs timeOut(timeoutMillis); - unsigned long detectedBaudrate; - while (!timeOut) { - if ((detectedBaudrate = testBaudrate())) { - break; - } - yield(); - delay(100); - } - return detectedBaudrate; -} - -size_t HardwareSerial::readBytes(char* buffer, size_t size) -{ - size_t got = 0; - - while (got < size) - { - esp8266::polledTimeout::oneShotFastMs timeOut(_timeout); - size_t avail; - while ((avail = available()) == 0 && !timeOut); - if (avail == 0) - break; - got += read(buffer + got, std::min(size - got, avail)); - } - return got; -} - -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) -HardwareSerial Serial(UART0); -#endif -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) -HardwareSerial Serial1(UART1); -#endif +/* + HardwareSerial.cpp - esp8266 UART support + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + 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 + + Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266) + Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266) + Modified 3 May 2015 by Hristo Gochkov (change register access methods) + */ + +#include +#include +#include +#include +#include +#include "Arduino.h" +#include "HardwareSerial.h" +#include "Esp.h" + +HardwareSerial::HardwareSerial(int uart_nr) + : _uart_nr(uart_nr), _rx_size(256) +{} + +void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) +{ + end(); + _uart = uart_init(_uart_nr, baud, (int) config, (int) mode, tx_pin, _rx_size); +#if defined(DEBUG_ESP_PORT) && !defined(NDEBUG) + if (static_cast(this) == static_cast(&DEBUG_ESP_PORT)) + { + setDebugOutput(true); + println(); + println(ESP.getFullVersion()); + } +#endif +} + +void HardwareSerial::end() +{ + if(uart_get_debug() == _uart_nr) { + uart_set_debug(UART_NO); + } + + uart_uninit(_uart); + _uart = NULL; +} + +void HardwareSerial::updateBaudRate(unsigned long baud) +{ + if(!_uart) { + return; + } + + uart_set_baudrate(_uart, baud); +} + +size_t HardwareSerial::setRxBufferSize(size_t size){ + if(_uart) { + _rx_size = uart_resize_rx_buffer(_uart, size); + } else { + _rx_size = size; + } + return _rx_size; +} + +void HardwareSerial::setDebugOutput(bool en) +{ + if(!_uart) { + return; + } + if(en) { + if(uart_tx_enabled(_uart)) { + uart_set_debug(_uart_nr); + } else { + uart_set_debug(UART_NO); + } + } else { + // disable debug for this interface + if(uart_get_debug() == _uart_nr) { + uart_set_debug(UART_NO); + } + } +} + +int HardwareSerial::available(void) +{ + int result = static_cast(uart_rx_available(_uart)); + if (!result) { + optimistic_yield(10000); + } + return result; +} + +void HardwareSerial::flush() +{ + uint8_t bit_length = 0; + if(!_uart || !uart_tx_enabled(_uart)) { + return; + } + + bit_length = uart_get_bit_length(_uart_nr); // data width, parity and stop + uart_wait_tx_empty(_uart); + //Workaround for a bug in serial not actually being finished yet + //Wait for 8 data bits, 1 parity and 2 stop bits, just in case + delayMicroseconds(bit_length * 1000000 / uart_get_baudrate(_uart) + 1); +} + +void HardwareSerial::startDetectBaudrate() +{ + uart_start_detect_baudrate(_uart_nr); +} + +unsigned long HardwareSerial::testBaudrate() +{ + return uart_detect_baudrate(_uart_nr); +} + +unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis) +{ + esp8266::polledTimeout::oneShotFastMs timeOut(timeoutMillis); + unsigned long detectedBaudrate; + while (!timeOut) { + if ((detectedBaudrate = testBaudrate())) { + break; + } + yield(); + delay(100); + } + return detectedBaudrate; +} + +size_t HardwareSerial::readBytes(char* buffer, size_t size) +{ + size_t got = 0; + + while (got < size) + { + esp8266::polledTimeout::oneShotFastMs timeOut(_timeout); + size_t avail; + while ((avail = available()) == 0 && !timeOut); + if (avail == 0) + break; + got += read(buffer + got, std::min(size - got, avail)); + } + return got; +} + +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) +HardwareSerial Serial(UART0); +#endif +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) +HardwareSerial Serial1(UART1); +#endif diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index ed39736e5..465521e51 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -24,16 +24,18 @@ struct recurrent_fn_t recurrent_fn_t* mNext = nullptr; mRecFuncT mFunc; esp8266::polledTimeout::periodicFastUs callNow; - recurrent_fn_t (esp8266::polledTimeout::periodicFastUs interval): callNow(interval) { } + std::function alarm = nullptr; + recurrent_fn_t(esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { } }; -static recurrent_fn_t* rFirst = nullptr; // fifo not needed +static recurrent_fn_t* rFirst = nullptr; +static recurrent_fn_t* rLast = nullptr; // Returns a pointer to an unused sched_fn_t, // or if none are available allocates a new one, // or nullptr if limit is reached IRAM_ATTR // called from ISR -static scheduled_fn_t* get_fn_unsafe () +static scheduled_fn_t* get_fn_unsafe() { scheduled_fn_t* result = nullptr; // try to get an item from unused items list @@ -52,7 +54,7 @@ static scheduled_fn_t* get_fn_unsafe () return result; } -static void recycle_fn_unsafe (scheduled_fn_t* fn) +static void recycle_fn_unsafe(scheduled_fn_t* fn) { fn->mFunc = nullptr; // special overload in c++ std lib fn->mNext = sUnused; @@ -60,8 +62,11 @@ static void recycle_fn_unsafe (scheduled_fn_t* fn) } IRAM_ATTR // (not only) called from ISR -bool schedule_function (const std::function& fn) +bool schedule_function(const std::function& fn) { + if (!fn) + return false; + esp8266::InterruptLock lockAllInterruptsInThisScope; scheduled_fn_t* item = get_fn_unsafe(); @@ -80,27 +85,37 @@ bool schedule_function (const std::function& fn) return true; } -bool schedule_recurrent_function_us (const std::function& fn, uint32_t repeat_us) +bool schedule_recurrent_function_us(const std::function& fn, + uint32_t repeat_us, const std::function& alarm) { assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s) - esp8266::InterruptLock lockAllInterruptsInThisScope; + if (!fn) + return false; recurrent_fn_t* item = new (std::nothrow) recurrent_fn_t(repeat_us); if (!item) return false; item->mFunc = fn; + item->alarm = alarm; - if (rFirst) - item->mNext = rFirst; + esp8266::InterruptLock lockAllInterruptsInThisScope; - rFirst = item; + if (rLast) + { + rLast->mNext = item; + } + else + { + rFirst = item; + } + rLast = item; return true; } -void run_scheduled_functions () +void run_scheduled_functions() { esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms @@ -128,15 +143,18 @@ void run_scheduled_functions () } } -void run_scheduled_recurrent_functions () +void run_scheduled_recurrent_functions() { + esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms + // Note to the reader: // There is no exposed API to remove a scheduled function: // Scheduled functions are removed only from this function, and // its purpose is that it is never called from an interrupt // (always on cont stack). - if (!rFirst) + auto current = rFirst; + if (!current) return; static bool fence = false; @@ -153,26 +171,35 @@ void run_scheduled_recurrent_functions () } recurrent_fn_t* prev = nullptr; - recurrent_fn_t* current = rFirst; + // prevent scheduling of new functions during this run + auto stop = rLast; - while (current) + bool done; + do { - if (current->callNow && !current->mFunc()) + done = current == stop; + const bool wakeup = current->alarm && current->alarm(); + bool callNow = current->callNow; + + if ((wakeup || callNow) && !current->mFunc()) { // remove function from stack esp8266::InterruptLock lockAllInterruptsInThisScope; auto to_ditch = current; + // removing rLast + if (rLast == current) + rLast = prev; + + current = current->mNext; if (prev) { - current = current->mNext; prev->mNext = current; } else { - rFirst = rFirst->mNext; - current = rFirst; + rFirst = current; } delete(to_ditch); @@ -182,7 +209,15 @@ void run_scheduled_recurrent_functions () prev = current; current = current->mNext; } - } + + if (yieldNow) + { + // because scheduled functions might last too long for watchdog etc, + // this is yield() in cont stack: + esp_schedule(); + cont_yield(g_pcont); + } + } while (current && !done); fence = false; } diff --git a/cores/esp8266/Schedule.h b/cores/esp8266/Schedule.h index 50fc88fc8..a02241fa6 100644 --- a/cores/esp8266/Schedule.h +++ b/cores/esp8266/Schedule.h @@ -10,7 +10,7 @@ // in user stack (called CONT stack) without the common restrictions from // system context. Details are below. -// The purpose of recurrent scheduled function is to independantly execute +// The purpose of recurrent scheduled function is to independently execute // user code in CONT stack on a regular basis. // It has been introduced with ethernet service in mind, it can also be used // for all libraries in the need of a regular `libdaemon_handlestuff()`. @@ -58,14 +58,14 @@ void run_scheduled_functions(); // functions. However a user function returning false will cancel itself. // * Long running operations or yield() or delay() are not allowed in the // recurrent function. -// * A recurrent function currently must not schedule another recurrent -// functions. - -bool schedule_recurrent_function_us (const std::function& fn, uint32_t repeat_us); +// * If alarm is used, anytime during scheduling when it returns true, +// any remaining delay from repeat_us is disregarded, and fn is executed. +bool schedule_recurrent_function_us(const std::function& fn, + uint32_t repeat_us, const std::function& alarm = nullptr); // Test recurrence and run recurrent scheduled functions. // (internally called at every `yield()` and `loop()`) -void run_scheduled_recurrent_functions (); +void run_scheduled_recurrent_functions(); #endif // ESP_SCHEDULE_H diff --git a/cores/esp8266/StreamString.h b/cores/esp8266/StreamString.h index 1fedc18de..2e81fa14a 100644 --- a/cores/esp8266/StreamString.h +++ b/cores/esp8266/StreamString.h @@ -1,39 +1,39 @@ -/** - StreamString.h - - Copyright (c) 2015 Markus Sattler. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - 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 STREAMSTRING_H_ -#define STREAMSTRING_H_ - - -class StreamString: public Stream, public String { -public: - size_t write(const uint8_t *buffer, size_t size) override; - size_t write(uint8_t data) override; - - int available() override; - int read() override; - int peek() override; - void flush() override; -}; - - -#endif /* STREAMSTRING_H_ */ +/** + StreamString.h + + Copyright (c) 2015 Markus Sattler. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + 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 STREAMSTRING_H_ +#define STREAMSTRING_H_ + + +class StreamString: public Stream, public String { +public: + size_t write(const uint8_t *buffer, size_t size) override; + size_t write(uint8_t data) override; + + int available() override; + int read() override; + int peek() override; + void flush() override; +}; + + +#endif /* STREAMSTRING_H_ */ diff --git a/cores/esp8266/TZ.h b/cores/esp8266/TZ.h new file mode 100644 index 000000000..95b609e61 --- /dev/null +++ b/cores/esp8266/TZ.h @@ -0,0 +1,475 @@ + +// autogenerated from https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv +// by script /tools/TZupdate.sh +// Mon Sep 9 20:58:30 UTC 2019 +// +// This database is autogenerated from IANA timezone database +// https://www.iana.org/time-zones +// and can be updated on demand in this repository +// or by yourself using the above script + +#ifndef TZDB_H +#define TZDB_H + +#define TZ_Africa_Abidjan PSTR("GMT0") +#define TZ_Africa_Accra PSTR("GMT0") +#define TZ_Africa_Addis_Ababa PSTR("EAT-3") +#define TZ_Africa_Algiers PSTR("CET-1") +#define TZ_Africa_Asmara PSTR("EAT-3") +#define TZ_Africa_Bamako PSTR("GMT0") +#define TZ_Africa_Bangui PSTR("WAT-1") +#define TZ_Africa_Banjul PSTR("GMT0") +#define TZ_Africa_Bissau PSTR("GMT0") +#define TZ_Africa_Blantyre PSTR("CAT-2") +#define TZ_Africa_Brazzaville PSTR("WAT-1") +#define TZ_Africa_Bujumbura PSTR("CAT-2") +#define TZ_Africa_Cairo PSTR("EET-2") +#define TZ_Africa_Casablanca PSTR("<+01>-1") +#define TZ_Africa_Ceuta PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Africa_Conakry PSTR("GMT0") +#define TZ_Africa_Dakar PSTR("GMT0") +#define TZ_Africa_Dar_es_Salaam PSTR("EAT-3") +#define TZ_Africa_Djibouti PSTR("EAT-3") +#define TZ_Africa_Douala PSTR("WAT-1") +#define TZ_Africa_El_Aaiun PSTR("<+01>-1") +#define TZ_Africa_Freetown PSTR("GMT0") +#define TZ_Africa_Gaborone PSTR("CAT-2") +#define TZ_Africa_Harare PSTR("CAT-2") +#define TZ_Africa_Johannesburg PSTR("SAST-2") +#define TZ_Africa_Juba PSTR("EAT-3") +#define TZ_Africa_Kampala PSTR("EAT-3") +#define TZ_Africa_Khartoum PSTR("CAT-2") +#define TZ_Africa_Kigali PSTR("CAT-2") +#define TZ_Africa_Kinshasa PSTR("WAT-1") +#define TZ_Africa_Lagos PSTR("WAT-1") +#define TZ_Africa_Libreville PSTR("WAT-1") +#define TZ_Africa_Lome PSTR("GMT0") +#define TZ_Africa_Luanda PSTR("WAT-1") +#define TZ_Africa_Lubumbashi PSTR("CAT-2") +#define TZ_Africa_Lusaka PSTR("CAT-2") +#define TZ_Africa_Malabo PSTR("WAT-1") +#define TZ_Africa_Maputo PSTR("CAT-2") +#define TZ_Africa_Maseru PSTR("SAST-2") +#define TZ_Africa_Mbabane PSTR("SAST-2") +#define TZ_Africa_Mogadishu PSTR("EAT-3") +#define TZ_Africa_Monrovia PSTR("GMT0") +#define TZ_Africa_Nairobi PSTR("EAT-3") +#define TZ_Africa_Ndjamena PSTR("WAT-1") +#define TZ_Africa_Niamey PSTR("WAT-1") +#define TZ_Africa_Nouakchott PSTR("GMT0") +#define TZ_Africa_Ouagadougou PSTR("GMT0") +#define TZ_Africa_PortomNovo PSTR("WAT-1") +#define TZ_Africa_Sao_Tome PSTR("GMT0") +#define TZ_Africa_Tripoli PSTR("EET-2") +#define TZ_Africa_Tunis PSTR("CET-1") +#define TZ_Africa_Windhoek PSTR("CAT-2") +#define TZ_America_Adak PSTR("HST10HDT,M3.2.0,M11.1.0") +#define TZ_America_Anchorage PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_Anguilla PSTR("AST4") +#define TZ_America_Antigua PSTR("AST4") +#define TZ_America_Araguaina PSTR("<-03>3") +#define TZ_America_Argentina_Buenos_Aires PSTR("<-03>3") +#define TZ_America_Argentina_Catamarca PSTR("<-03>3") +#define TZ_America_Argentina_Cordoba PSTR("<-03>3") +#define TZ_America_Argentina_Jujuy PSTR("<-03>3") +#define TZ_America_Argentina_La_Rioja PSTR("<-03>3") +#define TZ_America_Argentina_Mendoza PSTR("<-03>3") +#define TZ_America_Argentina_Rio_Gallegos PSTR("<-03>3") +#define TZ_America_Argentina_Salta PSTR("<-03>3") +#define TZ_America_Argentina_San_Juan PSTR("<-03>3") +#define TZ_America_Argentina_San_Luis PSTR("<-03>3") +#define TZ_America_Argentina_Tucuman PSTR("<-03>3") +#define TZ_America_Argentina_Ushuaia PSTR("<-03>3") +#define TZ_America_Aruba PSTR("AST4") +#define TZ_America_Asuncion PSTR("<-04>4<-03>,M10.1.0/0,M3.4.0/0") +#define TZ_America_Atikokan PSTR("EST5") +#define TZ_America_Bahia PSTR("<-03>3") +#define TZ_America_Bahia_Banderas PSTR("CST6CDT,M4.1.0,M10.5.0") +#define TZ_America_Barbados PSTR("AST4") +#define TZ_America_Belem PSTR("<-03>3") +#define TZ_America_Belize PSTR("CST6") +#define TZ_America_BlancmSablon PSTR("AST4") +#define TZ_America_Boa_Vista PSTR("<-04>4") +#define TZ_America_Bogota PSTR("<-05>5") +#define TZ_America_Boise PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Cambridge_Bay PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Campo_Grande PSTR("<-04>4") +#define TZ_America_Cancun PSTR("EST5") +#define TZ_America_Caracas PSTR("<-04>4") +#define TZ_America_Cayenne PSTR("<-03>3") +#define TZ_America_Cayman PSTR("EST5") +#define TZ_America_Chicago PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Chihuahua PSTR("MST7MDT,M4.1.0,M10.5.0") +#define TZ_America_Costa_Rica PSTR("CST6") +#define TZ_America_Creston PSTR("MST7") +#define TZ_America_Cuiaba PSTR("<-04>4") +#define TZ_America_Curacao PSTR("AST4") +#define TZ_America_Danmarkshavn PSTR("GMT0") +#define TZ_America_Dawson PSTR("PST8PDT,M3.2.0,M11.1.0") +#define TZ_America_Dawson_Creek PSTR("MST7") +#define TZ_America_Denver PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Detroit PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Dominica PSTR("AST4") +#define TZ_America_Edmonton PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Eirunepe PSTR("<-05>5") +#define TZ_America_El_Salvador PSTR("CST6") +#define TZ_America_Fortaleza PSTR("<-03>3") +#define TZ_America_Fort_Nelson PSTR("MST7") +#define TZ_America_Glace_Bay PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_America_Godthab PSTR("<-03>3<-02>,M3.5.0/-2,M10.5.0/-1") +#define TZ_America_Goose_Bay PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_America_Grand_Turk PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Grenada PSTR("AST4") +#define TZ_America_Guadeloupe PSTR("AST4") +#define TZ_America_Guatemala PSTR("CST6") +#define TZ_America_Guayaquil PSTR("<-05>5") +#define TZ_America_Guyana PSTR("<-04>4") +#define TZ_America_Halifax PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_America_Havana PSTR("CST5CDT,M3.2.0/0,M11.1.0/1") +#define TZ_America_Hermosillo PSTR("MST7") +#define TZ_America_Indiana_Indianapolis PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Knox PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Marengo PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Petersburg PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Tell_City PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Vevay PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Vincennes PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Indiana_Winamac PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Inuvik PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Iqaluit PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Jamaica PSTR("EST5") +#define TZ_America_Juneau PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_Kentucky_Louisville PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Kentucky_Monticello PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Kralendijk PSTR("AST4") +#define TZ_America_La_Paz PSTR("<-04>4") +#define TZ_America_Lima PSTR("<-05>5") +#define TZ_America_Los_Angeles PSTR("PST8PDT,M3.2.0,M11.1.0") +#define TZ_America_Lower_Princes PSTR("AST4") +#define TZ_America_Maceio PSTR("<-03>3") +#define TZ_America_Managua PSTR("CST6") +#define TZ_America_Manaus PSTR("<-04>4") +#define TZ_America_Marigot PSTR("AST4") +#define TZ_America_Martinique PSTR("AST4") +#define TZ_America_Matamoros PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Mazatlan PSTR("MST7MDT,M4.1.0,M10.5.0") +#define TZ_America_Menominee PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Merida PSTR("CST6CDT,M4.1.0,M10.5.0") +#define TZ_America_Metlakatla PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_Mexico_City PSTR("CST6CDT,M4.1.0,M10.5.0") +#define TZ_America_Miquelon PSTR("<-03>3<-02>,M3.2.0,M11.1.0") +#define TZ_America_Moncton PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_America_Monterrey PSTR("CST6CDT,M4.1.0,M10.5.0") +#define TZ_America_Montevideo PSTR("<-03>3") +#define TZ_America_Montreal PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Montserrat PSTR("AST4") +#define TZ_America_Nassau PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_New_York PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Nipigon PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Nome PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_Noronha PSTR("<-02>2") +#define TZ_America_North_Dakota_Beulah PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_North_Dakota_Center PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_North_Dakota_New_Salem PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Ojinaga PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_America_Panama PSTR("EST5") +#define TZ_America_Pangnirtung PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Paramaribo PSTR("<-03>3") +#define TZ_America_Phoenix PSTR("MST7") +#define TZ_America_PortmaumPrince PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Port_of_Spain PSTR("AST4") +#define TZ_America_Porto_Velho PSTR("<-04>4") +#define TZ_America_Puerto_Rico PSTR("AST4") +#define TZ_America_Punta_Arenas PSTR("<-03>3") +#define TZ_America_Rainy_River PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Rankin_Inlet PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Recife PSTR("<-03>3") +#define TZ_America_Regina PSTR("CST6") +#define TZ_America_Resolute PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Rio_Branco PSTR("<-05>5") +#define TZ_America_Santarem PSTR("<-03>3") +#define TZ_America_Santiago PSTR("<-04>4<-03>,M9.1.6/24,M4.1.6/24") +#define TZ_America_Santo_Domingo PSTR("AST4") +#define TZ_America_Sao_Paulo PSTR("<-03>3") +#define TZ_America_Scoresbysund PSTR("<-01>1<+00>,M3.5.0/0,M10.5.0/1") +#define TZ_America_Sitka PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_St_Barthelemy PSTR("AST4") +#define TZ_America_St_Johns PSTR("NST3:30NDT,M3.2.0,M11.1.0") +#define TZ_America_St_Kitts PSTR("AST4") +#define TZ_America_St_Lucia PSTR("AST4") +#define TZ_America_St_Thomas PSTR("AST4") +#define TZ_America_St_Vincent PSTR("AST4") +#define TZ_America_Swift_Current PSTR("CST6") +#define TZ_America_Tegucigalpa PSTR("CST6") +#define TZ_America_Thule PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_America_Thunder_Bay PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Tijuana PSTR("PST8PDT,M3.2.0,M11.1.0") +#define TZ_America_Toronto PSTR("EST5EDT,M3.2.0,M11.1.0") +#define TZ_America_Tortola PSTR("AST4") +#define TZ_America_Vancouver PSTR("PST8PDT,M3.2.0,M11.1.0") +#define TZ_America_Whitehorse PSTR("PST8PDT,M3.2.0,M11.1.0") +#define TZ_America_Winnipeg PSTR("CST6CDT,M3.2.0,M11.1.0") +#define TZ_America_Yakutat PSTR("AKST9AKDT,M3.2.0,M11.1.0") +#define TZ_America_Yellowknife PSTR("MST7MDT,M3.2.0,M11.1.0") +#define TZ_Antarctica_Casey PSTR("<+08>-8") +#define TZ_Antarctica_Davis PSTR("<+07>-7") +#define TZ_Antarctica_DumontDUrville PSTR("<+10>-10") +#define TZ_Antarctica_Macquarie PSTR("<+11>-11") +#define TZ_Antarctica_Mawson PSTR("<+05>-5") +#define TZ_Antarctica_McMurdo PSTR("NZST-12NZDT,M9.5.0,M4.1.0/3") +#define TZ_Antarctica_Palmer PSTR("<-03>3") +#define TZ_Antarctica_Rothera PSTR("<-03>3") +#define TZ_Antarctica_Syowa PSTR("<+03>-3") +#define TZ_Antarctica_Troll PSTR("<+00>0<+02>-2,M3.5.0/1,M10.5.0/3") +#define TZ_Antarctica_Vostok PSTR("<+06>-6") +#define TZ_Arctic_Longyearbyen PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Asia_Aden PSTR("<+03>-3") +#define TZ_Asia_Almaty PSTR("<+06>-6") +#define TZ_Asia_Amman PSTR("EET-2EEST,M3.5.4/24,M10.5.5/1") +#define TZ_Asia_Anadyr PSTR("<+12>-12") +#define TZ_Asia_Aqtau PSTR("<+05>-5") +#define TZ_Asia_Aqtobe PSTR("<+05>-5") +#define TZ_Asia_Ashgabat PSTR("<+05>-5") +#define TZ_Asia_Atyrau PSTR("<+05>-5") +#define TZ_Asia_Baghdad PSTR("<+03>-3") +#define TZ_Asia_Bahrain PSTR("<+03>-3") +#define TZ_Asia_Baku PSTR("<+04>-4") +#define TZ_Asia_Bangkok PSTR("<+07>-7") +#define TZ_Asia_Barnaul PSTR("<+07>-7") +#define TZ_Asia_Beirut PSTR("EET-2EEST,M3.5.0/0,M10.5.0/0") +#define TZ_Asia_Bishkek PSTR("<+06>-6") +#define TZ_Asia_Brunei PSTR("<+08>-8") +#define TZ_Asia_Chita PSTR("<+09>-9") +#define TZ_Asia_Choibalsan PSTR("<+08>-8") +#define TZ_Asia_Colombo PSTR("<+0530>-5:30") +#define TZ_Asia_Damascus PSTR("EET-2EEST,M3.5.5/0,M10.5.5/0") +#define TZ_Asia_Dhaka PSTR("<+06>-6") +#define TZ_Asia_Dili PSTR("<+09>-9") +#define TZ_Asia_Dubai PSTR("<+04>-4") +#define TZ_Asia_Dushanbe PSTR("<+05>-5") +#define TZ_Asia_Famagusta PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Asia_Gaza PSTR("EET-2EEST,M3.5.5/0,M10.5.6/1") +#define TZ_Asia_Hebron PSTR("EET-2EEST,M3.5.5/0,M10.5.6/1") +#define TZ_Asia_Ho_Chi_Minh PSTR("<+07>-7") +#define TZ_Asia_Hong_Kong PSTR("HKT-8") +#define TZ_Asia_Hovd PSTR("<+07>-7") +#define TZ_Asia_Irkutsk PSTR("<+08>-8") +#define TZ_Asia_Jakarta PSTR("WIB-7") +#define TZ_Asia_Jayapura PSTR("WIT-9") +#define TZ_Asia_Jerusalem PSTR("IST-2IDT,M3.4.4/26,M10.5.0") +#define TZ_Asia_Kabul PSTR("<+0430>-4:30") +#define TZ_Asia_Kamchatka PSTR("<+12>-12") +#define TZ_Asia_Karachi PSTR("PKT-5") +#define TZ_Asia_Kathmandu PSTR("<+0545>-5:45") +#define TZ_Asia_Khandyga PSTR("<+09>-9") +#define TZ_Asia_Kolkata PSTR("IST-5:30") +#define TZ_Asia_Krasnoyarsk PSTR("<+07>-7") +#define TZ_Asia_Kuala_Lumpur PSTR("<+08>-8") +#define TZ_Asia_Kuching PSTR("<+08>-8") +#define TZ_Asia_Kuwait PSTR("<+03>-3") +#define TZ_Asia_Macau PSTR("CST-8") +#define TZ_Asia_Magadan PSTR("<+11>-11") +#define TZ_Asia_Makassar PSTR("WITA-8") +#define TZ_Asia_Manila PSTR("PST-8") +#define TZ_Asia_Muscat PSTR("<+04>-4") +#define TZ_Asia_Nicosia PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Asia_Novokuznetsk PSTR("<+07>-7") +#define TZ_Asia_Novosibirsk PSTR("<+07>-7") +#define TZ_Asia_Omsk PSTR("<+06>-6") +#define TZ_Asia_Oral PSTR("<+05>-5") +#define TZ_Asia_Phnom_Penh PSTR("<+07>-7") +#define TZ_Asia_Pontianak PSTR("WIB-7") +#define TZ_Asia_Pyongyang PSTR("KST-9") +#define TZ_Asia_Qatar PSTR("<+03>-3") +#define TZ_Asia_Qyzylorda PSTR("<+05>-5") +#define TZ_Asia_Riyadh PSTR("<+03>-3") +#define TZ_Asia_Sakhalin PSTR("<+11>-11") +#define TZ_Asia_Samarkand PSTR("<+05>-5") +#define TZ_Asia_Seoul PSTR("KST-9") +#define TZ_Asia_Shanghai PSTR("CST-8") +#define TZ_Asia_Singapore PSTR("<+08>-8") +#define TZ_Asia_Srednekolymsk PSTR("<+11>-11") +#define TZ_Asia_Taipei PSTR("CST-8") +#define TZ_Asia_Tashkent PSTR("<+05>-5") +#define TZ_Asia_Tbilisi PSTR("<+04>-4") +#define TZ_Asia_Tehran PSTR("<+0330>-3:30<+0430>,J79/24,J263/24") +#define TZ_Asia_Thimphu PSTR("<+06>-6") +#define TZ_Asia_Tokyo PSTR("JST-9") +#define TZ_Asia_Tomsk PSTR("<+07>-7") +#define TZ_Asia_Ulaanbaatar PSTR("<+08>-8") +#define TZ_Asia_Urumqi PSTR("<+06>-6") +#define TZ_Asia_UstmNera PSTR("<+10>-10") +#define TZ_Asia_Vientiane PSTR("<+07>-7") +#define TZ_Asia_Vladivostok PSTR("<+10>-10") +#define TZ_Asia_Yakutsk PSTR("<+09>-9") +#define TZ_Asia_Yangon PSTR("<+0630>-6:30") +#define TZ_Asia_Yekaterinburg PSTR("<+05>-5") +#define TZ_Asia_Yerevan PSTR("<+04>-4") +#define TZ_Atlantic_Azores PSTR("<-01>1<+00>,M3.5.0/0,M10.5.0/1") +#define TZ_Atlantic_Bermuda PSTR("AST4ADT,M3.2.0,M11.1.0") +#define TZ_Atlantic_Canary PSTR("WET0WEST,M3.5.0/1,M10.5.0") +#define TZ_Atlantic_Cape_Verde PSTR("<-01>1") +#define TZ_Atlantic_Faroe PSTR("WET0WEST,M3.5.0/1,M10.5.0") +#define TZ_Atlantic_Madeira PSTR("WET0WEST,M3.5.0/1,M10.5.0") +#define TZ_Atlantic_Reykjavik PSTR("GMT0") +#define TZ_Atlantic_South_Georgia PSTR("<-02>2") +#define TZ_Atlantic_Stanley PSTR("<-03>3") +#define TZ_Atlantic_St_Helena PSTR("GMT0") +#define TZ_Australia_Adelaide PSTR("ACST-9:30ACDT,M10.1.0,M4.1.0/3") +#define TZ_Australia_Brisbane PSTR("AEST-10") +#define TZ_Australia_Broken_Hill PSTR("ACST-9:30ACDT,M10.1.0,M4.1.0/3") +#define TZ_Australia_Currie PSTR("AEST-10AEDT,M10.1.0,M4.1.0/3") +#define TZ_Australia_Darwin PSTR("ACST-9:30") +#define TZ_Australia_Eucla PSTR("<+0845>-8:45") +#define TZ_Australia_Hobart PSTR("AEST-10AEDT,M10.1.0,M4.1.0/3") +#define TZ_Australia_Lindeman PSTR("AEST-10") +#define TZ_Australia_Lord_Howe PSTR("<+1030>-10:30<+11>-11,M10.1.0,M4.1.0") +#define TZ_Australia_Melbourne PSTR("AEST-10AEDT,M10.1.0,M4.1.0/3") +#define TZ_Australia_Perth PSTR("AWST-8") +#define TZ_Australia_Sydney PSTR("AEST-10AEDT,M10.1.0,M4.1.0/3") +#define TZ_Europe_Amsterdam PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Andorra PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Astrakhan PSTR("<+04>-4") +#define TZ_Europe_Athens PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Belgrade PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Berlin PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Bratislava PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Brussels PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Bucharest PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Budapest PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Busingen PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Chisinau PSTR("EET-2EEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Copenhagen PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Dublin PSTR("IST-1GMT0,M10.5.0,M3.5.0/1") +#define TZ_Europe_Gibraltar PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Guernsey PSTR("GMT0BST,M3.5.0/1,M10.5.0") +#define TZ_Europe_Helsinki PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Isle_of_Man PSTR("GMT0BST,M3.5.0/1,M10.5.0") +#define TZ_Europe_Istanbul PSTR("<+03>-3") +#define TZ_Europe_Jersey PSTR("GMT0BST,M3.5.0/1,M10.5.0") +#define TZ_Europe_Kaliningrad PSTR("EET-2") +#define TZ_Europe_Kiev PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Kirov PSTR("<+03>-3") +#define TZ_Europe_Lisbon PSTR("WET0WEST,M3.5.0/1,M10.5.0") +#define TZ_Europe_Ljubljana PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_London PSTR("GMT0BST,M3.5.0/1,M10.5.0") +#define TZ_Europe_Luxembourg PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Madrid PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Malta PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Mariehamn PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Minsk PSTR("<+03>-3") +#define TZ_Europe_Monaco PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Moscow PSTR("MSK-3") +#define TZ_Europe_Oslo PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Paris PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Podgorica PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Prague PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Riga PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Rome PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Samara PSTR("<+04>-4") +#define TZ_Europe_San_Marino PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Sarajevo PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Saratov PSTR("<+04>-4") +#define TZ_Europe_Simferopol PSTR("MSK-3") +#define TZ_Europe_Skopje PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Sofia PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Stockholm PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Tallinn PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Tirane PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Ulyanovsk PSTR("<+04>-4") +#define TZ_Europe_Uzhgorod PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Vaduz PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Vatican PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Vienna PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Vilnius PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Volgograd PSTR("<+04>-4") +#define TZ_Europe_Warsaw PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Zagreb PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Europe_Zaporozhye PSTR("EET-2EEST,M3.5.0/3,M10.5.0/4") +#define TZ_Europe_Zurich PSTR("CET-1CEST,M3.5.0,M10.5.0/3") +#define TZ_Indian_Antananarivo PSTR("EAT-3") +#define TZ_Indian_Chagos PSTR("<+06>-6") +#define TZ_Indian_Christmas PSTR("<+07>-7") +#define TZ_Indian_Cocos PSTR("<+0630>-6:30") +#define TZ_Indian_Comoro PSTR("EAT-3") +#define TZ_Indian_Kerguelen PSTR("<+05>-5") +#define TZ_Indian_Mahe PSTR("<+04>-4") +#define TZ_Indian_Maldives PSTR("<+05>-5") +#define TZ_Indian_Mauritius PSTR("<+04>-4") +#define TZ_Indian_Mayotte PSTR("EAT-3") +#define TZ_Indian_Reunion PSTR("<+04>-4") +#define TZ_Pacific_Apia PSTR("<+13>-13<+14>,M9.5.0/3,M4.1.0/4") +#define TZ_Pacific_Auckland PSTR("NZST-12NZDT,M9.5.0,M4.1.0/3") +#define TZ_Pacific_Bougainville PSTR("<+11>-11") +#define TZ_Pacific_Chatham PSTR("<+1245>-12:45<+1345>,M9.5.0/2:45,M4.1.0/3:45") +#define TZ_Pacific_Chuuk PSTR("<+10>-10") +#define TZ_Pacific_Easter PSTR("<-06>6<-05>,M9.1.6/22,M4.1.6/22") +#define TZ_Pacific_Efate PSTR("<+11>-11") +#define TZ_Pacific_Enderbury PSTR("<+13>-13") +#define TZ_Pacific_Fakaofo PSTR("<+13>-13") +#define TZ_Pacific_Fiji PSTR("<+12>-12<+13>,M11.1.0,M1.2.2/123") +#define TZ_Pacific_Funafuti PSTR("<+12>-12") +#define TZ_Pacific_Galapagos PSTR("<-06>6") +#define TZ_Pacific_Gambier PSTR("<-09>9") +#define TZ_Pacific_Guadalcanal PSTR("<+11>-11") +#define TZ_Pacific_Guam PSTR("ChST-10") +#define TZ_Pacific_Honolulu PSTR("HST10") +#define TZ_Pacific_Kiritimati PSTR("<+14>-14") +#define TZ_Pacific_Kosrae PSTR("<+11>-11") +#define TZ_Pacific_Kwajalein PSTR("<+12>-12") +#define TZ_Pacific_Majuro PSTR("<+12>-12") +#define TZ_Pacific_Marquesas PSTR("<-0930>9:30") +#define TZ_Pacific_Midway PSTR("SST11") +#define TZ_Pacific_Nauru PSTR("<+12>-12") +#define TZ_Pacific_Niue PSTR("<-11>11") +#define TZ_Pacific_Norfolk PSTR("<+11>-11") +#define TZ_Pacific_Noumea PSTR("<+11>-11") +#define TZ_Pacific_Pago_Pago PSTR("SST11") +#define TZ_Pacific_Palau PSTR("<+09>-9") +#define TZ_Pacific_Pitcairn PSTR("<-08>8") +#define TZ_Pacific_Pohnpei PSTR("<+11>-11") +#define TZ_Pacific_Port_Moresby PSTR("<+10>-10") +#define TZ_Pacific_Rarotonga PSTR("<-10>10") +#define TZ_Pacific_Saipan PSTR("ChST-10") +#define TZ_Pacific_Tahiti PSTR("<-10>10") +#define TZ_Pacific_Tarawa PSTR("<+12>-12") +#define TZ_Pacific_Tongatapu PSTR("<+13>-13") +#define TZ_Pacific_Wake PSTR("<+12>-12") +#define TZ_Pacific_Wallis PSTR("<+12>-12") +#define TZ_Etc_GMT PSTR("GMT0") +#define TZ_Etc_GMTm0 PSTR("GMT0") +#define TZ_Etc_GMTm1 PSTR("<+01>-1") +#define TZ_Etc_GMTm2 PSTR("<+02>-2") +#define TZ_Etc_GMTm3 PSTR("<+03>-3") +#define TZ_Etc_GMTm4 PSTR("<+04>-4") +#define TZ_Etc_GMTm5 PSTR("<+05>-5") +#define TZ_Etc_GMTm6 PSTR("<+06>-6") +#define TZ_Etc_GMTm7 PSTR("<+07>-7") +#define TZ_Etc_GMTm8 PSTR("<+08>-8") +#define TZ_Etc_GMTm9 PSTR("<+09>-9") +#define TZ_Etc_GMTm10 PSTR("<+10>-10") +#define TZ_Etc_GMTm11 PSTR("<+11>-11") +#define TZ_Etc_GMTm12 PSTR("<+12>-12") +#define TZ_Etc_GMTm13 PSTR("<+13>-13") +#define TZ_Etc_GMTm14 PSTR("<+14>-14") +#define TZ_Etc_GMT0 PSTR("GMT0") +#define TZ_Etc_GMTp0 PSTR("GMT0") +#define TZ_Etc_GMTp1 PSTR("<-01>1") +#define TZ_Etc_GMTp2 PSTR("<-02>2") +#define TZ_Etc_GMTp3 PSTR("<-03>3") +#define TZ_Etc_GMTp4 PSTR("<-04>4") +#define TZ_Etc_GMTp5 PSTR("<-05>5") +#define TZ_Etc_GMTp6 PSTR("<-06>6") +#define TZ_Etc_GMTp7 PSTR("<-07>7") +#define TZ_Etc_GMTp8 PSTR("<-08>8") +#define TZ_Etc_GMTp9 PSTR("<-09>9") +#define TZ_Etc_GMTp10 PSTR("<-10>10") +#define TZ_Etc_GMTp11 PSTR("<-11>11") +#define TZ_Etc_GMTp12 PSTR("<-12>12") +#define TZ_Etc_UCT PSTR("UTC0") +#define TZ_Etc_UTC PSTR("UTC0") +#define TZ_Etc_Greenwich PSTR("GMT0") +#define TZ_Etc_Universal PSTR("UTC0") +#define TZ_Etc_Zulu PSTR("UTC0") + +#endif // TZDB_H diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index d6b6b621f..b028e19b9 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -23,6 +23,7 @@ extern "C" { } extern "C" uint32_t _FS_start; +extern "C" uint32_t _FS_end; UpdaterClass::UpdaterClass() : _async(false) @@ -105,15 +106,17 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) { wifi_set_sleep_type(NONE_SLEEP_T); + //address where we will start writing the update uintptr_t updateStartAddress = 0; + //size of current sketch rounded to a sector + size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); + //size of the update rounded to a sector + size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); + if (command == U_FLASH) { - //size of current sketch rounded to a sector - size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); //address of the end of the space available for sketch and update uintptr_t updateEndAddress = (uintptr_t)&_FS_start - 0x40200000; - //size of the update rounded to a sector - size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); - //address where we will start writing the update + updateStartAddress = (updateEndAddress > roundedSize)? (updateEndAddress - roundedSize) : 0; #ifdef DEBUG_UPDATER @@ -129,7 +132,24 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) { } } else if (command == U_FS) { - updateStartAddress = (uintptr_t)&_FS_start - 0x40200000; + if((uintptr_t)&_FS_start + roundedSize > (uintptr_t)&_FS_end) { + _setError(UPDATE_ERROR_SPACE); + return false; + } + +#ifdef ATOMIC_FS_UPDATE + //address of the end of the space available for update + uintptr_t updateEndAddress = (uintptr_t)&_FS_start - 0x40200000; + + updateStartAddress = (updateEndAddress > roundedSize)? (updateEndAddress - roundedSize) : 0; + + if(updateStartAddress < currentSketchSize) { + _setError(UPDATE_ERROR_SPACE); + return false; + } +#else + updateStartAddress = (uintptr_t)&_FS_start - 0x40200000; +#endif } else { // unknown command @@ -272,8 +292,19 @@ bool UpdaterClass::end(bool evenIfRemaining){ #ifdef DEBUG_UPDATER DEBUG_UPDATER.printf_P(PSTR("Staged: address:0x%08X, size:0x%08zX\n"), _startAddress, _size); +#endif } else if (_command == U_FS) { +#ifdef ATOMIC_FS_UPDATE + eboot_command ebcmd; + ebcmd.action = ACTION_COPY_RAW; + ebcmd.args[0] = _startAddress; + ebcmd.args[1] = (uintptr_t)&_FS_start - 0x40200000; + ebcmd.args[2] = _size; + eboot_command_write(&ebcmd); +#endif + +#ifdef DEBUG_UPDATER DEBUG_UPDATER.printf_P(PSTR("Filesystem: address:0x%08X, size:0x%08zX\n"), _startAddress, _size); #endif } @@ -387,7 +418,7 @@ bool UpdaterClass::_verifyHeader(uint8_t data) { } return true; } else if(_command == U_FS) { - // no check of SPIFFS possible with first byte. + // no check of FS possible with first byte. return true; } return false; @@ -421,7 +452,7 @@ bool UpdaterClass::_verifyEnd() { return true; } else if(_command == U_FS) { - // SPIFFS is already over written checks make no sense any more. + // FS is already over written checks make no sense any more. return true; } return false; diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 048704074..5e31b2770 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -129,10 +129,9 @@ String::~String() { // /*********************************************/ inline void String::init(void) { - setSSO(false); - setCapacity(0); + setSSO(true); setLen(0); - setBuffer(nullptr); + wbuffer()[0] = 0; } void String::invalidate(void) { diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 558d1da85..8c970abf1 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -53,7 +53,7 @@ class String { // if the initial value is null or invalid, or if memory allocation // fails, the string will be marked as invalid (i.e. "if (s)" will // be false). - String(const char *cstr = ""); + String(const char *cstr = nullptr); String(const String &str); String(const __FlashStringHelper *str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ diff --git a/cores/esp8266/base64.cpp b/cores/esp8266/base64.cpp index 3ae51f5a0..f0d079352 100644 --- a/cores/esp8266/base64.cpp +++ b/cores/esp8266/base64.cpp @@ -1,71 +1,71 @@ -/** - * base64.cpp - * - * Created on: 09.12.2015 - * - * Copyright (c) 2015 Markus Sattler. All rights reserved. - * This file is part of the ESP8266 core for Arduino. - * - * 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 - * - */ - -#include "Arduino.h" -extern "C" { -#include "libb64/cdecode.h" -#include "libb64/cencode.h" -} -#include "base64.h" - -/** - * convert input data to base64 - * @param data uint8_t * - * @param length size_t - * @return String - */ -String base64::encode(uint8_t * data, size_t length, bool doNewLines) { - // base64 needs more size then the source data, use cencode.h macros - size_t size = ((doNewLines ? base64_encode_expected_len(length) - : base64_encode_expected_len_nonewlines(length)) + 1); - char * buffer = (char *) malloc(size); - if(buffer) { - base64_encodestate _state; - if(doNewLines) - { - base64_init_encodestate(&_state); - } - else - { - base64_init_encodestate_nonewlines(&_state); - } - int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state); - len = base64_encode_blockend((buffer + len), &_state); - - String base64 = String(buffer); - free(buffer); - return base64; - } - return String("-FAIL-"); -} - -/** - * convert input data to base64 - * @param text String - * @return String - */ -String base64::encode(String text, bool doNewLines) { - return base64::encode((uint8_t *) text.c_str(), text.length(), doNewLines); -} - +/** + * base64.cpp + * + * Created on: 09.12.2015 + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is part of the ESP8266 core for Arduino. + * + * 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 + * + */ + +#include "Arduino.h" +extern "C" { +#include "libb64/cdecode.h" +#include "libb64/cencode.h" +} +#include "base64.h" + +/** + * convert input data to base64 + * @param data const uint8_t * + * @param length size_t + * @return String + */ +String base64::encode(const uint8_t * data, size_t length, bool doNewLines) { + // base64 needs more size then the source data, use cencode.h macros + size_t size = ((doNewLines ? base64_encode_expected_len(length) + : base64_encode_expected_len_nonewlines(length)) + 1); + char * buffer = (char *) malloc(size); + if(buffer) { + base64_encodestate _state; + if(doNewLines) + { + base64_init_encodestate(&_state); + } + else + { + base64_init_encodestate_nonewlines(&_state); + } + int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state); + len = base64_encode_blockend((buffer + len), &_state); + + String base64 = String(buffer); + free(buffer); + return base64; + } + return String("-FAIL-"); +} + +/** + * convert input data to base64 + * @param text const String& + * @return String + */ +String base64::encode(const String& text, bool doNewLines) { + return base64::encode((const uint8_t *) text.c_str(), text.length(), doNewLines); +} + diff --git a/cores/esp8266/base64.h b/cores/esp8266/base64.h index 67140123e..1d6e22fac 100644 --- a/cores/esp8266/base64.h +++ b/cores/esp8266/base64.h @@ -1,39 +1,39 @@ -/** - * base64.h - * - * Created on: 09.12.2015 - * - * Copyright (c) 2015 Markus Sattler. All rights reserved. - * This file is part of the ESP8266 core for Arduino. - * - * 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 CORE_BASE64_H_ -#define CORE_BASE64_H_ - -class base64 { - public: - // NOTE: The default behaviour of backend (lib64) - // is to add a newline every 72 (encoded) characters output. - // This may 'break' longer uris and json variables - static String encode(uint8_t * data, size_t length, bool doNewLines = true); - static String encode(String text, bool doNewLines = true); - private: -}; - - -#endif /* CORE_BASE64_H_ */ +/** + * base64.h + * + * Created on: 09.12.2015 + * + * Copyright (c) 2015 Markus Sattler. All rights reserved. + * This file is part of the ESP8266 core for Arduino. + * + * 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 CORE_BASE64_H_ +#define CORE_BASE64_H_ + +class base64 { + public: + // NOTE: The default behaviour of backend (lib64) + // is to add a newline every 72 (encoded) characters output. + // This may 'break' longer uris and json variables + static String encode(const uint8_t * data, size_t length, bool doNewLines = true); + static String encode(const String& text, bool doNewLines = true); + private: +}; + + +#endif /* CORE_BASE64_H_ */ diff --git a/cores/esp8266/cont.S b/cores/esp8266/cont.S index 4304db448..00ca01778 100644 --- a/cores/esp8266/cont.S +++ b/cores/esp8266/cont.S @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - .text + .section .irom0.text .align 4 .literal_position .global cont_yield @@ -84,7 +84,7 @@ cont_wrapper: //////////////////////////////////////////////////// - .text + .section .irom0.text .align 4 .literal_position .global cont_run diff --git a/cores/esp8266/core_esp8266_features.h b/cores/esp8266/core_esp8266_features.h index 6b8e22c9b..05bbc4c5c 100644 --- a/cores/esp8266/core_esp8266_features.h +++ b/cores/esp8266/core_esp8266_features.h @@ -1,50 +1,50 @@ -/* - core_esp8266_features.h - list of features integrated in to ESP8266 core - - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - 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 CORE_ESP8266_FEATURES_H -#define CORE_ESP8266_FEATURES_H - - -#define CORE_HAS_LIBB64 -#define CORE_HAS_BASE64_CLASS -#define CORE_HAS_CXA_GUARD -#define CORE_HAS_UMM - -#define WIFI_HAS_EVENT_CALLBACK - -#ifdef __cplusplus - -#include // malloc() -#include // size_t - +/* + core_esp8266_features.h - list of features integrated in to ESP8266 core + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + 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 CORE_ESP8266_FEATURES_H +#define CORE_ESP8266_FEATURES_H + + +#define CORE_HAS_LIBB64 +#define CORE_HAS_BASE64_CLASS +#define CORE_HAS_CXA_GUARD +#define CORE_HAS_UMM + +#define WIFI_HAS_EVENT_CALLBACK + +#ifdef __cplusplus + +#include // malloc() +#include // size_t + namespace arduino -{ +{ extern "C++" template T* new0 (size_t n, TConstructorArgs... TconstructorArgs) - { - // n==0: single allocation, otherwise it is an array - size_t offset = n? sizeof(size_t): 0; + { + // n==0: single allocation, otherwise it is an array + size_t offset = n? sizeof(size_t): 0; size_t arraysize = n? n: 1; T* ptr = (T*)malloc(offset + (arraysize * sizeof(T))); if (ptr) @@ -54,43 +54,43 @@ namespace arduino for (size_t i = 0; i < arraysize; i++) new (ptr + offset + i * sizeof(T)) T(TconstructorArgs...); return ptr + offset; - } + } return nullptr; } } - -#define arduino_new(Type, ...) arduino::new0(0, ##__VA_ARGS__) -#define arduino_newarray(Type, n, ...) arduino::new0(n, ##__VA_ARGS__) - -#endif // __cplusplus - -#ifndef __STRINGIFY -#define __STRINGIFY(a) #a -#endif - -// these low level routines provide a replacement for SREG interrupt save that AVR uses -// but are esp8266 specific. A normal use pattern is like -// -//{ -// uint32_t savedPS = xt_rsil(1); // this routine will allow level 2 and above -// // do work here -// xt_wsr_ps(savedPS); // restore the state -//} -// -// level (0-15), interrupts of the given level and above will be active -// level 15 will disable ALL interrupts, -// level 0 will enable ALL interrupts, -// -#ifndef CORE_MOCK -#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state) :: "memory"); state;})) -#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory") - + +#define arduino_new(Type, ...) arduino::new0(0, ##__VA_ARGS__) +#define arduino_newarray(Type, n, ...) arduino::new0(n, ##__VA_ARGS__) + +#endif // __cplusplus + +#ifndef __STRINGIFY +#define __STRINGIFY(a) #a +#endif + +// these low level routines provide a replacement for SREG interrupt save that AVR uses +// but are esp8266 specific. A normal use pattern is like +// +//{ +// uint32_t savedPS = xt_rsil(1); // this routine will allow level 2 and above +// // do work here +// xt_wsr_ps(savedPS); // restore the state +//} +// +// level (0-15), interrupts of the given level and above will be active +// level 15 will disable ALL interrupts, +// level 0 will enable ALL interrupts, +// +#ifndef CORE_MOCK +#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state) :: "memory"); state;})) +#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory") + inline uint32_t esp_get_cycle_count() __attribute__((always_inline)); inline uint32_t esp_get_cycle_count() { - uint32_t ccount; - __asm__ __volatile__("rsr %0,ccount":"=a"(ccount)); - return ccount; -} -#endif // not CORE_MOCK - -#endif // CORE_ESP8266_FEATURES_H + uint32_t ccount; + __asm__ __volatile__("rsr %0,ccount":"=a"(ccount)); + return ccount; +} +#endif // not CORE_MOCK + +#endif // CORE_ESP8266_FEATURES_H diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index bbe67f1d4..0486ee573 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -62,6 +62,10 @@ static int s_user_reset_reason = REASON_DEFAULT_RST; // From UMM, the last caller of a malloc/realloc/calloc which failed: extern void *umm_last_fail_alloc_addr; extern int umm_last_fail_alloc_size; +#if defined(DEBUG_ESP_OOM) +extern const char *umm_last_fail_alloc_file; +extern int umm_last_fail_alloc_line; +#endif static void raise_exception() __attribute__((noreturn)); @@ -108,8 +112,7 @@ void __wrap_system_restart_local() { else rst_info.reason = s_user_reset_reason; - // TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast - ets_install_putc1((void *)&uart_write_char_d); + ets_install_putc1(&uart_write_char_d); if (s_panic_line) { ets_printf_P(PSTR("\nPanic %S:%d %S"), s_panic_file, s_panic_line, s_panic_func); @@ -182,7 +185,13 @@ void __wrap_system_restart_local() { // Use cap-X formatting to ensure the standard EspExceptionDecoder doesn't match the address if (umm_last_fail_alloc_addr) { - ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size); +#if defined(DEBUG_ESP_OOM) + ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)@%S:%d\n"), + (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size, + umm_last_fail_alloc_file, umm_last_fail_alloc_line); +#else + ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size); +#endif } custom_crash_callback( &rst_info, sp_dump + offset, stack_end ); diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 6f6cd5897..281b19680 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -1,114 +1,154 @@ /* - si2c.c - Software I2C library for esp8266 + si2c.c - Software I2C library for esp8266 - Copyright (c) 2015 Hristo Gochkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. + Copyright (c) 2015 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. - 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 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. + 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 - Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support + 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 + Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support */ #include "twi.h" #include "pins_arduino.h" #include "wiring_private.h" + + extern "C" { - -unsigned int preferred_si2c_clock = 100000; #include "twi_util.h" - #include "ets_sys.h" +}; -unsigned char twi_dcount = 18; -static unsigned char twi_sda, twi_scl; -static uint32_t twi_clockStretchLimit; -static unsigned char twi_addr = 0; -// modes (private) -#define TWIPM_UNKNOWN 0 -#define TWIPM_IDLE 1 -#define TWIPM_ADDRESSED 2 -#define TWIPM_WAIT 3 +// Implement as a class to reduce code size by allowing access to many global variables with a single base pointer +class Twi +{ +private: + unsigned int preferred_si2c_clock = 100000; + unsigned char twi_dcount = 18; + unsigned char twi_sda = 0; + unsigned char twi_scl = 0; + unsigned char twi_addr = 0; + uint32_t twi_clockStretchLimit = 0; -// states (private) -#define TWIP_UNKNOWN 0 -#define TWIP_IDLE 1 -#define TWIP_START 2 -#define TWIP_SEND_ACK 3 -#define TWIP_WAIT_ACK 4 -#define TWIP_WAIT_STOP 5 -#define TWIP_SLA_W 6 -#define TWIP_SLA_R 7 -#define TWIP_REP_START 8 -#define TWIP_READ 9 -#define TWIP_STOP 10 -#define TWIP_REC_ACK 11 -#define TWIP_READ_ACK 12 -#define TWIP_RWAIT_ACK 13 -#define TWIP_WRITE 14 -#define TWIP_BUS_ERR 15 + // These are int-wide, even though they could all fit in a byte, to reduce code size and avoid any potential + // issues about RmW on packed bytes. The int-wide variations of asm instructions are smaller than the equivalent + // byte-wide ones, and since these emums are used everywhere, the difference adds up fast. There is only a single + // instance of the class, though, so the extra 12 bytes of RAM used here saves a lot more IRAM. + volatile enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twip_mode = TWIPM_IDLE; + volatile enum { TWIP_UNKNOWN = 0, TWIP_IDLE, TWIP_START, TWIP_SEND_ACK, TWIP_WAIT_ACK, TWIP_WAIT_STOP, TWIP_SLA_W, TWIP_SLA_R, TWIP_REP_START, TWIP_READ, TWIP_STOP, TWIP_REC_ACK, TWIP_READ_ACK, TWIP_RWAIT_ACK, TWIP_WRITE, TWIP_BUS_ERR } twip_state = TWIP_IDLE; + volatile int twip_status = TW_NO_INFO; + volatile int bitCount = 0; -static volatile uint8_t twip_mode = TWIPM_IDLE; -static volatile uint8_t twip_state = TWIP_IDLE; -static volatile uint8_t twip_status = TW_NO_INFO; -static volatile uint8_t bitCount = 0; + volatile uint8_t twi_data = 0x00; + volatile int twi_ack = 0; + volatile int twi_ack_rec = 0; + volatile int twi_timeout_ms = 10; -#define TWDR twi_data -static volatile uint8_t twi_data = 0x00; -static volatile uint8_t twi_ack = 0; -static volatile uint8_t twi_ack_rec = 0; -static volatile int twi_timeout_ms = 10; + volatile enum { TWI_READY = 0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twi_state = TWI_READY; + volatile uint8_t twi_error = 0xFF; -#define TWI_READY 0 -#define TWI_MRX 1 -#define TWI_MTX 2 -#define TWI_SRX 3 -#define TWI_STX 4 -static volatile uint8_t twi_state = TWI_READY; -static volatile uint8_t twi_error = 0xFF; + uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_txBufferIndex = 0; + volatile int twi_txBufferLength = 0; -static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; -static volatile uint8_t twi_txBufferIndex; -static volatile uint8_t twi_txBufferLength; + uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_rxBufferIndex = 0; -static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; -static volatile uint8_t twi_rxBufferIndex; + void (*twi_onSlaveTransmit)(void); + void (*twi_onSlaveReceive)(uint8_t*, size_t); -static void (*twi_onSlaveTransmit)(void); -static void (*twi_onSlaveReceive)(uint8_t*, size_t); + // ETS queue/timer interfaces + enum { EVENTTASK_QUEUE_SIZE = 1, EVENTTASK_QUEUE_PRIO = 2 }; + enum { TWI_SIG_RANGE = 0x00000100, TWI_SIG_RX = 0x00000101, TWI_SIG_TX = 0x00000102 }; + ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; + ETSTimer timer; -static void onSclChange(void); -static void onSdaChange(void); + // Event/IRQ callbacks, so they can't use "this" and need to be static + static void ICACHE_RAM_ATTR onSclChange(void); + static void ICACHE_RAM_ATTR onSdaChange(void); + static void eventTask(ETSEvent *e); + static void ICACHE_RAM_ATTR onTimer(void *unused); -#define EVENTTASK_QUEUE_SIZE 1 -#define EVENTTASK_QUEUE_PRIO 2 + // Allow not linking in the slave code if there is no call to setAddress + bool _slaveEnabled = false; -#define TWI_SIG_RANGE 0x00000100 -#define TWI_SIG_RX (TWI_SIG_RANGE + 0x01) -#define TWI_SIG_TX (TWI_SIG_RANGE + 0x02) + // Internal use functions + void ICACHE_RAM_ATTR busywait(unsigned char v); + bool write_start(void); + bool write_stop(void); + bool write_bit(bool bit); + bool read_bit(void); + bool write_byte(unsigned char byte); + unsigned char read_byte(bool nack); + void ICACHE_RAM_ATTR onTwipEvent(uint8_t status); -static ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; -static void eventTask(ETSEvent *e); -static ETSTimer timer; -static void onTimer(void *unused); + // Inline helpers + inline void SDA_LOW() + { + GPES = (1 << twi_sda); + } + inline void SDA_HIGH() + { + GPEC = (1 << twi_sda); + } + inline bool SDA_READ() + { + return (GPI & (1 << twi_sda)) != 0; + } + inline void SCL_LOW() + { + GPES = (1 << twi_scl); + } + inline void SCL_HIGH() + { + GPEC = (1 << twi_scl); + } + inline bool SCL_READ() + { + return (GPI & (1 << twi_scl)) != 0; + } + // Handle the case where a slave needs to stretch the clock with a time-limited busy wait + inline void WAIT_CLOCK_STRETCH() + { + for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) + { + /* noop */ + } + } -#define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low) -#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high) -#define SDA_READ() ((GPI & (1 << twi_sda)) != 0) -#define SCL_LOW() (GPES = (1 << twi_scl)) -#define SCL_HIGH() (GPEC = (1 << twi_scl)) -#define SCL_READ() ((GPI & (1 << twi_scl)) != 0) + // Generate a clock "valley" (at the end of a segment, just before a repeated start) + void twi_scl_valley(void); + +public: + void setClock(unsigned int freq); + void setClockStretchLimit(uint32_t limit); + void init(unsigned char sda, unsigned char scl); + void setAddress(uint8_t address); + unsigned char writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop); + unsigned char readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop); + uint8_t status(); + uint8_t transmit(const uint8_t* data, uint8_t length); + void attachSlaveRxEvent(void (*function)(uint8_t*, size_t)); + void attachSlaveTxEvent(void (*function)(void)); + inline void ICACHE_RAM_ATTR reply(uint8_t ack); + inline void ICACHE_RAM_ATTR stop(void); + inline void ICACHE_RAM_ATTR releaseBus(void); + void enableSlave(); +}; + +static Twi twi; #ifndef FCPU80 #define FCPU80 80000000L @@ -120,670 +160,924 @@ static void onTimer(void *unused); #define TWI_CLOCK_STRETCH_MULTIPLIER 6 #endif -void twi_setClock(unsigned int freq){ - preferred_si2c_clock = freq; + +void Twi::setClock(unsigned int freq) +{ + preferred_si2c_clock = freq; #if F_CPU == FCPU80 - if(freq <= 50000) twi_dcount = 38;//about 50KHz - else if(freq <= 100000) twi_dcount = 19;//about 100KHz - else if(freq <= 200000) twi_dcount = 8;//about 200KHz - else if(freq <= 300000) twi_dcount = 3;//about 300KHz - else if(freq <= 400000) twi_dcount = 1;//about 400KHz - else twi_dcount = 1;//about 400KHz + if (freq <= 50000) + { + twi_dcount = 38; //about 50KHz + } + else if (freq <= 100000) + { + twi_dcount = 19; //about 100KHz + } + else if (freq <= 200000) + { + twi_dcount = 8; //about 200KHz + } + else if (freq <= 300000) + { + twi_dcount = 3; //about 300KHz + } + else if (freq <= 400000) + { + twi_dcount = 1; //about 400KHz + } + else + { + twi_dcount = 1; //about 400KHz + } #else - if(freq <= 50000) twi_dcount = 64;//about 50KHz - else if(freq <= 100000) twi_dcount = 32;//about 100KHz - else if(freq <= 200000) twi_dcount = 14;//about 200KHz - else if(freq <= 300000) twi_dcount = 8;//about 300KHz - else if(freq <= 400000) twi_dcount = 5;//about 400KHz - else if(freq <= 500000) twi_dcount = 3;//about 500KHz - else if(freq <= 600000) twi_dcount = 2;//about 600KHz - else twi_dcount = 1;//about 700KHz + if (freq <= 50000) + { + twi_dcount = 64; //about 50KHz + } + else if (freq <= 100000) + { + twi_dcount = 32; //about 100KHz + } + else if (freq <= 200000) + { + twi_dcount = 14; //about 200KHz + } + else if (freq <= 300000) + { + twi_dcount = 8; //about 300KHz + } + else if (freq <= 400000) + { + twi_dcount = 5; //about 400KHz + } + else if (freq <= 500000) + { + twi_dcount = 3; //about 500KHz + } + else if (freq <= 600000) + { + twi_dcount = 2; //about 600KHz + } + else + { + twi_dcount = 1; //about 700KHz + } #endif } -void twi_setClockStretchLimit(uint32_t limit){ - twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; -} - -void twi_init(unsigned char sda, unsigned char scl) +void Twi::setClockStretchLimit(uint32_t limit) { - // set timer function - ets_timer_setfn(&timer, onTimer, NULL); - - // create event task - ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); - - twi_sda = sda; - twi_scl = scl; - pinMode(twi_sda, INPUT_PULLUP); - pinMode(twi_scl, INPUT_PULLUP); - twi_setClock(preferred_si2c_clock); - twi_setClockStretchLimit(230); // default value is 230 uS - - if (twi_addr != 0) - { - attachInterrupt(scl, onSclChange, CHANGE); - attachInterrupt(sda, onSdaChange, CHANGE); - } + twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; } -void twi_setAddress(uint8_t address) + + +void Twi::init(unsigned char sda, unsigned char scl) { - // set twi slave address (skip over R/W bit) - twi_addr = address << 1; + // set timer function + ets_timer_setfn(&timer, onTimer, NULL); + + // create event task + ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); + + twi_sda = sda; + twi_scl = scl; + pinMode(twi_sda, INPUT_PULLUP); + pinMode(twi_scl, INPUT_PULLUP); + twi_setClock(preferred_si2c_clock); + twi_setClockStretchLimit(230); // default value is 230 uS } -static void ICACHE_RAM_ATTR twi_delay(unsigned char v){ - unsigned int i; +void Twi::setAddress(uint8_t address) +{ + // set twi slave address (skip over R/W bit) + twi_addr = address << 1; +} + +void Twi::enableSlave() +{ + if (!_slaveEnabled) + { + attachInterrupt(twi_scl, onSclChange, CHANGE); + attachInterrupt(twi_sda, onSdaChange, CHANGE); + _slaveEnabled = true; + } +} + +void ICACHE_RAM_ATTR Twi::busywait(unsigned char v) +{ + unsigned int i; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-but-set-variable" - unsigned int reg; - for (i = 0; i < v; i++) { - reg = GPI; - } - (void)reg; + unsigned int reg; + for (i = 0; i < v; i++) + { + reg = GPI; + } + (void)reg; #pragma GCC diagnostic pop } -static bool twi_write_start(void) { - SCL_HIGH(); - SDA_HIGH(); - if (SDA_READ() == 0) { - return false; - } - twi_delay(twi_dcount); - SDA_LOW(); - twi_delay(twi_dcount); - return true; -} - -static bool twi_write_stop(void){ - uint32_t i = 0; - SCL_LOW(); - SDA_LOW(); - twi_delay(twi_dcount); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching - twi_delay(twi_dcount); - SDA_HIGH(); - twi_delay(twi_dcount); - return true; -} - -static bool twi_write_bit(bool bit) { - uint32_t i = 0; - SCL_LOW(); - if (bit) SDA_HIGH(); - else SDA_LOW(); - twi_delay(twi_dcount+1); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - twi_delay(twi_dcount); - return true; -} - -static bool twi_read_bit(void) { - uint32_t i = 0; - SCL_LOW(); - SDA_HIGH(); - twi_delay(twi_dcount+2); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - bool bit = SDA_READ(); - twi_delay(twi_dcount); - return bit; -} - -static bool twi_write_byte(unsigned char byte) { - unsigned char bit; - for (bit = 0; bit < 8; bit++) { - twi_write_bit(byte & 0x80); - byte <<= 1; - } - return !twi_read_bit();//NACK/ACK -} - -static unsigned char twi_read_byte(bool nack) { - unsigned char byte = 0; - unsigned char bit; - for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit(); - twi_write_bit(nack); - return byte; -} - -unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){ - unsigned int i; - if(!twi_write_start()) return 4;//line busy - if(!twi_write_byte(((address << 1) | 0) & 0xFF)) { - if (sendStop) twi_write_stop(); - return 2; //received NACK on transmit of address - } - for(i=0; i 0) { // if SDA low, read the bits slaves have to sent to a max - twi_read_bit(); - if (SCL_READ() == 0) { - return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time +bool Twi::read_bit(void) +{ + SCL_LOW(); + SDA_HIGH(); + busywait(twi_dcount + 2); + SCL_HIGH(); + WAIT_CLOCK_STRETCH(); + bool bit = SDA_READ(); + busywait(twi_dcount); + return bit; +} + +bool Twi::write_byte(unsigned char byte) +{ + unsigned char bit; + for (bit = 0; bit < 8; bit++) + { + write_bit(byte & 0x80); + byte <<= 1; + } + return !read_bit();//NACK/ACK +} + +unsigned char Twi::read_byte(bool nack) +{ + unsigned char byte = 0; + unsigned char bit; + for (bit = 0; bit < 8; bit++) + { + byte = (byte << 1) | read_bit(); + } + write_bit(nack); + return byte; +} + +unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) +{ + unsigned int i; + if (!write_start()) + { + return 4; //line busy + } + if (!write_byte(((address << 1) | 0) & 0xFF)) + { + if (sendStop) + { + write_stop(); + } + return 2; //received NACK on transmit of address + } + for (i = 0; i < len; i++) + { + if (!write_byte(buf[i])) + { + if (sendStop) + { + write_stop(); + } + return 3;//received NACK on transmit of data } } - if (SDA_READ() == 0) - return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. - - if (!twi_write_start()) - return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? + if (sendStop) + { + write_stop(); + } + else + { + twi_scl_valley(); + // TD-er: Also busywait(twi_dcount) here? + // busywait(twi_dcount); + } + i = 0; + while (!SDA_READ() && (i++) < 10) + { + twi_scl_valley(); + busywait(twi_dcount); + } + return 0; +} + +unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop) +{ + unsigned int i; + if (!write_start()) + { + return 4; //line busy + } + if (!write_byte(((address << 1) | 1) & 0xFF)) + { + if (sendStop) + { + write_stop(); + } + return 2;//received NACK on transmit of address + } + for (i = 0; i < (len - 1); i++) + { + buf[i] = read_byte(false); + } + buf[len - 1] = read_byte(true); + if (sendStop) + { + write_stop(); + } + else + { + twi_scl_valley(); + // TD-er: Also busywait(twi_dcount) here? + // busywait(twi_dcount); + } + i = 0; + while (!SDA_READ() && (i++) < 10) + { + twi_scl_valley(); + busywait(twi_dcount); + } + return 0; +} + +uint8_t Twi::status() +{ + if (!SCL_READ()) + { + return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover + } + + int clockCount = 20; + while (!SDA_READ() && clockCount-- > 0) // if SDA low, read the bits slaves have to sent to a max + { + read_bit(); + if (!SCL_READ()) + { + return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time + } + } + if (!SDA_READ()) + { + return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. + } + + if (!write_start()) + { + return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? + } return I2C_OK; } -uint8_t twi_transmit(const uint8_t* data, uint8_t length) +uint8_t Twi::transmit(const uint8_t* data, uint8_t length) { - uint8_t i; + uint8_t i; - // ensure data will fit into buffer - if (length > TWI_BUFFER_LENGTH) { - return 1; - } + // ensure data will fit into buffer + if (length > TWI_BUFFER_LENGTH) + { + return 1; + } - // ensure we are currently a slave transmitter - if (twi_state != TWI_STX) { - return 2; - } + // ensure we are currently a slave transmitter + if (twi_state != TWI_STX) + { + return 2; + } - // set length and copy data into tx buffer - twi_txBufferLength = length; - for (i = 0; i < length; ++i) { - twi_txBuffer[i] = data[i]; - } + // set length and copy data into tx buffer + twi_txBufferLength = length; + for (i = 0; i < length; ++i) + { + twi_txBuffer[i] = data[i]; + } - return 0; + return 0; } -void twi_attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ) +void Twi::attachSlaveRxEvent(void (*function)(uint8_t*, size_t)) { - twi_onSlaveReceive = function; + twi_onSlaveReceive = function; } -void twi_attachSlaveTxEvent( void (*function)(void) ) +void Twi::attachSlaveTxEvent(void (*function)(void)) { - twi_onSlaveTransmit = function; + twi_onSlaveTransmit = function; } -void ICACHE_RAM_ATTR twi_reply(uint8_t ack) +inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack) { - // transmit master read ready signal, with or without ack - if (ack) { - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - } else { - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 0; // ~_BV(TWEA) - } + // transmit master read ready signal, with or without ack + if (ack) + { + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + } + else + { + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 0; // ~_BV(TWEA) + } } -void ICACHE_RAM_ATTR twi_stop(void) +inline void ICACHE_RAM_ATTR Twi::stop(void) { - // send stop condition - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - twi_delay(5); // Maybe this should be here - SDA_HIGH(); // _BV(TWSTO) - // update twi state - twi_state = TWI_READY; + // send stop condition + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + busywait(5); // Maybe this should be here + SDA_HIGH(); // _BV(TWSTO) + // update twi state + twi_state = TWI_READY; } -void ICACHE_RAM_ATTR twi_releaseBus(void) +inline void ICACHE_RAM_ATTR Twi::releaseBus(void) { - // release bus - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - SDA_HIGH(); + // release bus + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + SDA_HIGH(); - // update twi state - twi_state = TWI_READY; + // update twi state + twi_state = TWI_READY; } -void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status) +void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) { - switch(status) { + twip_status = status; + switch (status) + { // Slave Receiver case TW_SR_SLA_ACK: // addressed, returned ack case TW_SR_GCALL_ACK: // addressed generally, returned ack case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack - // enter slave receiver mode - twi_state = TWI_SRX; - // indicate that rx buffer can be overwritten and ack - twi_rxBufferIndex = 0; - twi_reply(1); - break; + // enter slave receiver mode + twi_state = TWI_SRX; + // indicate that rx buffer can be overwritten and ack + twi_rxBufferIndex = 0; + reply(1); + break; case TW_SR_DATA_ACK: // data received, returned ack case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack - // if there is still room in the rx buffer - if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ - // put byte in buffer and ack - twi_rxBuffer[twi_rxBufferIndex++] = TWDR; - twi_reply(1); - }else{ - // otherwise nack - twi_reply(0); - } - break; + // if there is still room in the rx buffer + if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) + { + // put byte in buffer and ack + twi_rxBuffer[twi_rxBufferIndex++] = twi_data; + reply(1); + } + else + { + // otherwise nack + reply(0); + } + break; case TW_SR_STOP: // stop or repeated start condition received - // put a null char after data if there's room - if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ - twi_rxBuffer[twi_rxBufferIndex] = '\0'; - } - // callback to user-defined callback over event task to allow for non-RAM-residing code - //twi_rxBufferLock = true; // This may be necessary - ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi_rxBufferIndex); + // put a null char after data if there's room + if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) + { + twi_rxBuffer[twi_rxBufferIndex] = '\0'; + } + // callback to user-defined callback over event task to allow for non-RAM-residing code + //twi_rxBufferLock = true; // This may be necessary + ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi_rxBufferIndex); - // since we submit rx buffer to "wire" library, we can reset it - twi_rxBufferIndex = 0; - break; + // since we submit rx buffer to "wire" library, we can reset it + twi_rxBufferIndex = 0; + break; case TW_SR_DATA_NACK: // data received, returned nack case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack - // nack back at master - twi_reply(0); - break; + // nack back at master + reply(0); + break; // Slave Transmitter case TW_ST_SLA_ACK: // addressed, returned ack case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack - // enter slave transmitter mode - twi_state = TWI_STX; - // ready the tx buffer index for iteration - twi_txBufferIndex = 0; - // set tx buffer length to be zero, to verify if user changes it - twi_txBufferLength = 0; - // callback to user-defined callback over event task to allow for non-RAM-residing code - // request for txBuffer to be filled and length to be set - // note: user must call twi_transmit(bytes, length) to do this - ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_TX, 0); - break; + // enter slave transmitter mode + twi_state = TWI_STX; + // ready the tx buffer index for iteration + twi_txBufferIndex = 0; + // set tx buffer length to be zero, to verify if user changes it + twi_txBufferLength = 0; + // callback to user-defined callback over event task to allow for non-RAM-residing code + // request for txBuffer to be filled and length to be set + // note: user must call twi_transmit(bytes, length) to do this + ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_TX, 0); + break; - case TW_ST_DATA_ACK: // byte sent, ack returned - // copy data to output register - TWDR = twi_txBuffer[twi_txBufferIndex++]; + case TW_ST_DATA_ACK: // byte sent, ack returned + // copy data to output register + twi_data = twi_txBuffer[twi_txBufferIndex++]; - bitCount = 8; - bitCount--; - (twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); - twi_data <<= 1; + bitCount = 8; + bitCount--; + if (twi_data & 0x80) + { + SDA_HIGH(); + } + else + { + SDA_LOW(); + } + twi_data <<= 1; - // if there is more to send, ack, otherwise nack - if(twi_txBufferIndex < twi_txBufferLength){ - twi_reply(1); - }else{ - twi_reply(0); - } - break; + // if there is more to send, ack, otherwise nack + if (twi_txBufferIndex < twi_txBufferLength) + { + reply(1); + } + else + { + reply(0); + } + break; case TW_ST_DATA_NACK: // received nack, we are done case TW_ST_LAST_DATA: // received ack, but we are done already! - // leave slave receiver state - twi_releaseBus(); - break; + // leave slave receiver state + releaseBus(); + break; // All case TW_NO_INFO: // no state information - break; + break; case TW_BUS_ERROR: // bus error, illegal stop/start - twi_error = TW_BUS_ERROR; - twi_stop(); - break; - } + twi_error = TW_BUS_ERROR; + stop(); + break; + } } -void ICACHE_RAM_ATTR onTimer(void *unused) +void Twi::twi_scl_valley(void) { - (void)unused; - twi_releaseBus(); - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; + SCL_LOW(); + busywait(twi_dcount); + SCL_HIGH(); + WAIT_CLOCK_STRETCH(); } -static void eventTask(ETSEvent *e) +void ICACHE_RAM_ATTR Twi::onTimer(void *unused) +{ + (void)unused; + twi.releaseBus(); + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; +} + +void Twi::eventTask(ETSEvent *e) { - if (e == NULL) { - return; - } + if (e == NULL) + { + return; + } - switch (e->sig) - { - case TWI_SIG_TX: - twi_onSlaveTransmit(); + switch (e->sig) + { + case TWI_SIG_TX: + twi.twi_onSlaveTransmit(); - // if they didn't change buffer & length, initialize it - if (twi_txBufferLength == 0) { - twi_txBufferLength = 1; - twi_txBuffer[0] = 0x00; - } + // if they didn't change buffer & length, initialize it + if (twi.twi_txBufferLength == 0) + { + twi.twi_txBufferLength = 1; + twi.twi_txBuffer[0] = 0x00; + } - // Initiate transmission - twi_onTwipEvent(TW_ST_DATA_ACK); + // Initiate transmission + twi.onTwipEvent(TW_ST_DATA_ACK); - break; + break; - case TWI_SIG_RX: - // ack future responses and leave slave receiver state - twi_releaseBus(); - twi_onSlaveReceive(twi_rxBuffer, e->par); - break; - } + case TWI_SIG_RX: + // ack future responses and leave slave receiver state + twi.releaseBus(); + twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par); + break; + } } -void ICACHE_RAM_ATTR onSclChange(void) +// The state machine is converted from a 0...15 state to a 1-hot encoded state, and then +// compared to the logical-or of all states with the same branch. This removes the need +// for a large series of straight-line compares. The biggest win is when multiple states +// all have the same branch (onSdaChange), but for others there is some benefit, still. +#define S2M(x) (1<<(x)) +// Shorthand for if the state is any of the or'd bitmask x +#define IFSTATE(x) if (twip_state_mask & (x)) + +void ICACHE_RAM_ATTR Twi::onSclChange(void) { - static uint8_t sda; - static uint8_t scl; + unsigned int sda; + unsigned int scl; - sda = SDA_READ(); - scl = SCL_READ(); + // Store bool return in int to reduce final code size. + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); - twip_status = 0xF8; // reset TWI status + twi.twip_status = 0xF8; // reset TWI status - switch (twip_state) - { - case TWIP_IDLE: - case TWIP_WAIT_STOP: - case TWIP_BUS_ERR: - // ignore - break; + int twip_state_mask = S2M(twi.twip_state); + IFSTATE(S2M(TWIP_START) | S2M(TWIP_REP_START) | S2M(TWIP_SLA_W) | S2M(TWIP_READ)) + { + if (!scl) + { + // ignore + } + else + { + twi.bitCount--; + twi.twi_data <<= 1; + twi.twi_data |= sda; - case TWIP_START: - case TWIP_REP_START: - case TWIP_SLA_W: - case TWIP_READ: - if (!scl) { - // ignore - } else { - bitCount--; - twi_data <<= 1; - twi_data |= sda; + if (twi.bitCount != 0) + { + // continue + } + else + { + twi.twip_state = TWIP_SEND_ACK; + } + } + } + else IFSTATE(S2M(TWIP_SEND_ACK)) + { + if (scl) + { + // ignore + } + else + { + if (twi.twip_mode == TWIPM_IDLE) + { + if ((twi.twi_data & 0xFE) != twi.twi_addr) + { + // ignore + } + else + { + twi.SDA_LOW(); + } + } + else + { + if (!twi.twi_ack) + { + // ignore + } + else + { + twi.SDA_LOW(); + } + } + twi.twip_state = TWIP_WAIT_ACK; + } + } + else IFSTATE(S2M(TWIP_WAIT_ACK)) + { + if (scl) + { + // ignore + } + else + { + if (twi.twip_mode == TWIPM_IDLE) + { + if ((twi.twi_data & 0xFE) != twi.twi_addr) + { + twi.SDA_HIGH(); + twi.twip_state = TWIP_WAIT_STOP; + } + else + { + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); + twi.twip_mode = TWIPM_ADDRESSED; + if (!(twi.twi_data & 0x01)) + { + twi.onTwipEvent(TW_SR_SLA_ACK); + twi.bitCount = 8; + twi.twip_state = TWIP_SLA_W; + } + else + { + twi.onTwipEvent(TW_ST_SLA_ACK); + twi.twip_state = TWIP_SLA_R; + } + } + } + else + { + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); + if (!twi.twi_ack) + { + twi.onTwipEvent(TW_SR_DATA_NACK); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; + } + else + { + twi.onTwipEvent(TW_SR_DATA_ACK); + twi.bitCount = 8; + twi.twip_state = TWIP_READ; + } + } + } + } + else IFSTATE(S2M(TWIP_SLA_R) | S2M(TWIP_WRITE)) + { + if (scl) + { + // ignore + } + else + { + twi.bitCount--; + if (twi.twi_data & 0x80) + { + twi.SDA_HIGH(); + } + else + { + twi.SDA_LOW(); + } + twi.twi_data <<= 1; - if (bitCount != 0) { - // continue - } else { - twip_state = TWIP_SEND_ACK; - } - } - break; - - case TWIP_SEND_ACK: - if (scl) { - // ignore - } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { - // ignore - } else { - SDA_LOW(); - } - } else { - if (!twi_ack) { - // ignore - } else { - SDA_LOW(); - } - } - twip_state = TWIP_WAIT_ACK; - } - break; - - case TWIP_WAIT_ACK: - if (scl) { - // ignore - } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { - SDA_HIGH(); - twip_state = TWIP_WAIT_STOP; - } else { - SCL_LOW(); // clock stretching - SDA_HIGH(); - twip_mode = TWIPM_ADDRESSED; - if (!(twi_data & 0x01)) { - twip_status = TW_SR_SLA_ACK; - twi_onTwipEvent(twip_status); - bitCount = 8; - twip_state = TWIP_SLA_W; - } else { - twip_status = TW_ST_SLA_ACK; - twi_onTwipEvent(twip_status); - twip_state = TWIP_SLA_R; - } - } - } else { - SCL_LOW(); // clock stretching - SDA_HIGH(); - if (!twi_ack) { - twip_status = TW_SR_DATA_NACK; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; - } else { - twip_status = TW_SR_DATA_ACK; - twi_onTwipEvent(twip_status); - bitCount = 8; - twip_state = TWIP_READ; - } - } - } - break; - - case TWIP_SLA_R: - case TWIP_WRITE: - if (scl) { - // ignore - } else { - bitCount--; - (twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); - twi_data <<= 1; - - if (bitCount != 0) { - // continue - } else { - twip_state = TWIP_REC_ACK; - } - } - break; - - case TWIP_REC_ACK: - if (scl) { - // ignore - } else { - SDA_HIGH(); - twip_state = TWIP_READ_ACK; - } - break; - - case TWIP_READ_ACK: - if (!scl) { - // ignore - } else { - twi_ack_rec = !sda; - twip_state = TWIP_RWAIT_ACK; - } - break; - - case TWIP_RWAIT_ACK: - if (scl) { - // ignore - } else { - SCL_LOW(); // clock stretching - if (twi_ack && twi_ack_rec) { - twip_status = TW_ST_DATA_ACK; - twi_onTwipEvent(twip_status); - twip_state = TWIP_WRITE; - } else { - // we have no more data to send and/or the master doesn't want anymore - twip_status = twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; - } - } - break; - - default: - break; - } + if (twi.bitCount != 0) + { + // continue + } + else + { + twi.twip_state = TWIP_REC_ACK; + } + } + } + else IFSTATE(S2M(TWIP_REC_ACK)) + { + if (scl) + { + // ignore + } + else + { + twi.SDA_HIGH(); + twi.twip_state = TWIP_READ_ACK; + } + } + else IFSTATE(S2M(TWIP_READ_ACK)) + { + if (!scl) + { + // ignore + } + else + { + twi.twi_ack_rec = !sda; + twi.twip_state = TWIP_RWAIT_ACK; + } + } + else IFSTATE(S2M(TWIP_RWAIT_ACK)) + { + if (scl) + { + // ignore + } + else + { + twi.SCL_LOW(); // clock stretching + if (twi.twi_ack && twi.twi_ack_rec) + { + twi.onTwipEvent(TW_ST_DATA_ACK); + twi.twip_state = TWIP_WRITE; + } + else + { + // we have no more data to send and/or the master doesn't want anymore + twi.onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; + } + } + } } -void ICACHE_RAM_ATTR onSdaChange(void) +void ICACHE_RAM_ATTR Twi::onSdaChange(void) { - static uint8_t sda; - static uint8_t scl; - sda = SDA_READ(); - scl = SCL_READ(); + unsigned int sda; + unsigned int scl; - switch (twip_state) - { - case TWIP_IDLE: - if (!scl) { - // DATA - ignore - } else if (sda) { - // STOP - ignore - } else { - // START - bitCount = 8; - twip_state = TWIP_START; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - } - break; + // Store bool return in int to reduce final code size. + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); - case TWIP_START: - case TWIP_REP_START: - case TWIP_SEND_ACK: - case TWIP_WAIT_ACK: - case TWIP_SLA_R: - case TWIP_REC_ACK: - case TWIP_READ_ACK: - case TWIP_RWAIT_ACK: - case TWIP_WRITE: - if (!scl) { - // DATA - ignore - } else { - // START or STOP - SDA_HIGH(); // Should not be necessary - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; - } - break; - - case TWIP_WAIT_STOP: - case TWIP_BUS_ERR: - if (!scl) { - // DATA - ignore - } else { - if (sda) { - // STOP - SCL_LOW(); // clock stretching - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; - SCL_HIGH(); - } else { - // START - if (twip_state == TWIP_BUS_ERR) { - // ignore - } else { - bitCount = 8; - twip_state = TWIP_REP_START; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - } - } - } - break; - - case TWIP_SLA_W: - case TWIP_READ: - if (!scl) { - // DATA - ignore - } else { - // START or STOP - if (bitCount != 7) { - // inside byte transfer - error - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; - } else { - // during first bit in byte transfer - ok - SCL_LOW(); // clock stretching - twip_status = TW_SR_STOP; - twi_onTwipEvent(twip_status); - if (sda) { - // STOP - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; - } else { - // START - bitCount = 8; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - twip_state = TWIP_REP_START; - twip_mode = TWIPM_IDLE; - } - } - } - break; - - default: - break; - } + int twip_state_mask = S2M(twi.twip_state); + if (scl) /* !DATA */ + { + IFSTATE(S2M(TWIP_IDLE)) + { + if (sda) + { + // STOP - ignore + } + else + { + // START + twi.bitCount = 8; + twi.twip_state = TWIP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + } + } + else IFSTATE(S2M(TWIP_START) | S2M(TWIP_REP_START) | S2M(TWIP_SEND_ACK) | S2M(TWIP_WAIT_ACK) | S2M(TWIP_SLA_R) | S2M(TWIP_REC_ACK) | S2M(TWIP_READ_ACK) | S2M(TWIP_RWAIT_ACK) | S2M(TWIP_WRITE)) + { + // START or STOP + twi.SDA_HIGH(); // Should not be necessary + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; + } + else IFSTATE(S2M(TWIP_WAIT_STOP) | S2M(TWIP_BUS_ERR)) + { + if (sda) + { + // STOP + twi.SCL_LOW(); // clock stretching + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; + twi.SCL_HIGH(); + } + else + { + // START + if (twi.twip_state == TWIP_BUS_ERR) + { + // ignore + } + else + { + twi.bitCount = 8; + twi.twip_state = TWIP_REP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + } + } + } + else IFSTATE(S2M(TWIP_SLA_W) | S2M(TWIP_READ)) + { + // START or STOP + if (twi.bitCount != 7) + { + // inside byte transfer - error + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; + } + else + { + // during first bit in byte transfer - ok + twi.SCL_LOW(); // clock stretching + twi.onTwipEvent(TW_SR_STOP); + if (sda) + { + // STOP + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; + } + else + { + // START + twi.bitCount = 8; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + twi.twip_state = TWIP_REP_START; + twi.twip_mode = TWIPM_IDLE; + } + } + } + } } +// C wrappers for the object, since API is exposed only as C +extern "C" { + + void twi_init(unsigned char sda, unsigned char scl) + { + return twi.init(sda, scl); + } + + void twi_setAddress(uint8_t a) + { + return twi.setAddress(a); + } + + void twi_stop(void) + { + twi.stop(); + } + + void twi_setClock(unsigned int freq) + { + twi.setClock(freq); + } + + void twi_setClockStretchLimit(uint32_t limit) + { + twi.setClockStretchLimit(limit); + } + + uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) + { + return twi.writeTo(address, buf, len, sendStop); + } + + uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) + { + return twi.readFrom(address, buf, len, sendStop); + } + + uint8_t twi_status() + { + return twi.status(); + } + + uint8_t twi_transmit(const uint8_t * buf, uint8_t len) + { + return twi.transmit(buf, len); + } + + void twi_attachSlaveRxEvent(void (*cb)(uint8_t*, size_t)) + { + twi.attachSlaveRxEvent(cb); + } + + void twi_attachSlaveTxEvent(void (*cb)(void)) + { + twi.attachSlaveTxEvent(cb); + } + + void twi_reply(uint8_t r) + { + twi.reply(r); + } + + void twi_releaseBus(void) + { + twi.releaseBus(); + } + + void twi_enableSlaveMode(void) + { + twi.enableSlave(); + } + }; diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index 4a70609bf..2496995b0 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -17,7 +17,6 @@ extern bool timeshift64_is_set; void esp_yield(); void esp_schedule(); void tune_timeshift64 (uint64_t now_us); -void settimeofday_cb (void (*cb)(void)); void disable_extra4k_at_link_time (void) __attribute__((noinline)); uint32_t sqrt32 (uint32_t n); @@ -25,6 +24,14 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); #ifdef __cplusplus } + +#include + +using TrivialCB = std::function; + +void settimeofday_cb (TrivialCB&& cb); +void settimeofday_cb (const TrivialCB& cb); + #endif #endif // __COREDECLS_H diff --git a/cores/esp8266/debug.cpp b/cores/esp8266/debug.cpp index d8bf8bb11..0d0bde2df 100644 --- a/cores/esp8266/debug.cpp +++ b/cores/esp8266/debug.cpp @@ -1,36 +1,36 @@ -/* - debug.cpp - debug helper functions - Copyright (c) 2015 Markus Sattler. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - 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 - */ - -#include "Arduino.h" -#include "debug.h" - -void ICACHE_RAM_ATTR hexdump(const void *mem, uint32_t len, uint8_t cols) { - const uint8_t* src = (const uint8_t*) mem; - os_printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); - for(uint32_t i = 0; i < len; i++) { - if(i % cols == 0) { - os_printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); - yield(); - } - os_printf("%02X ", *src); - src++; - } - os_printf("\n"); -} +/* + debug.cpp - debug helper functions + Copyright (c) 2015 Markus Sattler. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + 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 + */ + +#include "Arduino.h" +#include "debug.h" + +void ICACHE_RAM_ATTR hexdump(const void *mem, uint32_t len, uint8_t cols) { + const uint8_t* src = (const uint8_t*) mem; + os_printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + for(uint32_t i = 0; i < len; i++) { + if(i % cols == 0) { + os_printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); + yield(); + } + os_printf("%02X ", *src); + src++; + } + os_printf("\n"); +} diff --git a/cores/esp8266/esp8266_undocumented.h b/cores/esp8266/esp8266_undocumented.h index dc84e2f57..22216d962 100644 --- a/cores/esp8266/esp8266_undocumented.h +++ b/cores/esp8266/esp8266_undocumented.h @@ -11,6 +11,27 @@ extern int rom_i2c_readReg_Mask(int, int, int, int, int); extern int uart_baudrate_detect(int, int); +/* +ROM function, uart_buff_switch(), is used to switch printing between UART0 and +UART1. It updates a structure that only controls a select group of print +functions. ets_putc() and ets_uart_printf() are examples and are not affected by +calls to ets_install_putc1(). + + Use: + 0 for UART0, also clears RX FIFO + 1 for UART1 + */ +extern void uart_buff_switch(uint8_t); + +/* + ROM function, ets_uart_printf(), prints on the UART selected by + uart_buff_switch(). Supported format options are the same as vprintf(). Also + has cooked newline behavior. No flash format/string support; however, ISR safe. + Also, uses a static function in ROM to print characters which is only + controlled by uart_buff_switch(). + */ +extern int ets_uart_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); + extern void ets_delay_us(uint32_t us); #ifdef __cplusplus diff --git a/cores/esp8266/heap.cpp b/cores/esp8266/heap.cpp index b0bd90861..cf4195c9f 100644 --- a/cores/esp8266/heap.cpp +++ b/cores/esp8266/heap.cpp @@ -7,38 +7,134 @@ #include "umm_malloc/umm_malloc.h" #include #include +#include extern "C" { +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) +#define UMM_MALLOC(s) umm_poison_malloc(s) +#define UMM_CALLOC(n,s) umm_poison_calloc(n,s) +#define UMM_REALLOC_FL(p,s,f,l) umm_poison_realloc_fl(p,s,f,l) +#define UMM_FREE_FL(p,f,l) umm_poison_free_fl(p,f,l) + +#undef realloc +#undef free + +#elif defined(DEBUG_ESP_OOM) +#define UMM_MALLOC(s) umm_malloc(s) +#define UMM_CALLOC(n,s) umm_calloc(n,s) +#define UMM_REALLOC_FL(p,s,f,l) umm_realloc(p,s) +#define UMM_FREE_FL(p,f,l) umm_free(p) + +#undef realloc +#undef free + +#else // ! UMM_POISON_CHECK && ! DEBUG_ESP_OOM +#define UMM_MALLOC(s) malloc(s) +#define UMM_CALLOC(n,s) calloc(n,s) +#define UMM_REALLOC_FL(p,s,f,l) realloc(p,s) +#define UMM_FREE_FL(p,f,l) free(p) +#endif + + +#if defined(UMM_POISON_CHECK) + #define POISON_CHECK__ABORT() \ + do { \ + if ( ! POISON_CHECK() ) \ + abort(); \ + } while(0) + + #define POISON_CHECK__PANIC_FL(file, line) \ + do { \ + if ( ! POISON_CHECK() ) \ + __panic_func(file, line, ""); \ + } while(0) + +#else // No full heap poison checking. + #define POISON_CHECK__ABORT() do {} while(0) + #define POISON_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0) +#endif + // Debugging helper, last allocation which returned NULL void *umm_last_fail_alloc_addr = NULL; int umm_last_fail_alloc_size = 0; +#if defined(DEBUG_ESP_OOM) +const char *umm_last_fail_alloc_file = NULL; +int umm_last_fail_alloc_line = 0; +#endif + +#ifdef UMM_INTEGRITY_CHECK +#define INTEGRITY_CHECK__ABORT() \ + do { \ + if ( ! INTEGRITY_CHECK() ) \ + abort(); \ + } while(0) + +#define INTEGRITY_CHECK__PANIC_FL(file, line) \ + do { \ + if ( ! INTEGRITY_CHECK() ) \ + __panic_func(file, line, ""); \ + } while(0) + +#else // ! UMM_INTEGRITY_CHECK +#define INTEGRITY_CHECK__ABORT() do {} while(0) +#define INTEGRITY_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0) + +#endif // UMM_INTEGRITY_CHECK + +#if defined(DEBUG_ESP_OOM) +#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l) \ + if(0 != (s) && 0 == p)\ + {\ + umm_last_fail_alloc_addr = __builtin_return_address(0);\ + umm_last_fail_alloc_size = s;\ + umm_last_fail_alloc_file = f;\ + umm_last_fail_alloc_line = l;\ + } +#define PTR_CHECK__LOG_LAST_FAIL(p, s) \ + if(0 != (s) && 0 == p)\ + {\ + umm_last_fail_alloc_addr = __builtin_return_address(0);\ + umm_last_fail_alloc_size = s;\ + umm_last_fail_alloc_file = NULL;\ + umm_last_fail_alloc_line = 0;\ + } +#else +#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l) \ + (void)f;\ + (void)l;\ + if(0 != (s) && 0 == p)\ + {\ + umm_last_fail_alloc_addr = __builtin_return_address(0);\ + umm_last_fail_alloc_size = s;\ + } +#define PTR_CHECK__LOG_LAST_FAIL(p, s) \ + if(0 != (s) && 0 == p)\ + {\ + umm_last_fail_alloc_addr = __builtin_return_address(0);\ + umm_last_fail_alloc_size = s;\ + } +#endif void* _malloc_r(struct _reent* unused, size_t size) { (void) unused; void *ret = malloc(size); - if (0 != size && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = size; - } + PTR_CHECK__LOG_LAST_FAIL(ret, size); return ret; } void _free_r(struct _reent* unused, void* ptr) { (void) unused; - return free(ptr); + free(ptr); } void* _realloc_r(struct _reent* unused, void* ptr, size_t size) { (void) unused; void *ret = realloc(ptr, size); - if (0 != size && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = size; - } + PTR_CHECK__LOG_LAST_FAIL(ret, size); return ret; } @@ -46,140 +142,169 @@ void* _calloc_r(struct _reent* unused, size_t count, size_t size) { (void) unused; void *ret = calloc(count, size); - if (0 != (count * size) && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = count * size; + PTR_CHECK__LOG_LAST_FAIL(ret, count * size); + return ret; +} + +#ifdef DEBUG_ESP_OOM +#undef malloc +#undef calloc +#undef realloc + +#define DEBUG_HEAP_PRINTF ets_uart_printf + +void ICACHE_RAM_ATTR print_loc(size_t size, const char* file, int line) +{ + (void)size; + (void)line; + if (system_get_os_print()) { + DEBUG_HEAP_PRINTF(":oom(%d)@", (int)size); + + bool inISR = ETS_INTR_WITHINISR(); + if (inISR && (uint32_t)file >= 0x40200000) { + DEBUG_HEAP_PRINTF("File: %p", file); + } else if (!inISR && (uint32_t)file >= 0x40200000) { + char buf[ets_strlen(file)] __attribute__ ((aligned(4))); + ets_strcpy(buf, file); + DEBUG_HEAP_PRINTF(buf); + } else { + DEBUG_HEAP_PRINTF(file); + } + + DEBUG_HEAP_PRINTF(":%d\n", line); } +} + +void ICACHE_RAM_ATTR print_oom_size(size_t size) +{ + (void)size; + if (system_get_os_print()) { + DEBUG_HEAP_PRINTF(":oom(%d)@?\n", (int)size); + } +} + +#define OOM_CHECK__PRINT_OOM(p, s) if (!p) print_oom_size(s) +#define OOM_CHECK__PRINT_LOC(p, s, f, l) if (!p) print_loc(s, f, l) + +#else // ! DEBUG_ESP_OOM + +#if 1 +//C - to be discussed - is this what you want? +//C Skip OOM logging of last fail for malloc/... and pvPort... . +//C It cost 64 more bytes of IRAM to turn on. And was not previously enabled. +#undef PTR_CHECK__LOG_LAST_FAIL_FL +#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l) +#undef PTR_CHECK__LOG_LAST_FAIL +#define PTR_CHECK__LOG_LAST_FAIL(p, s) +#endif + +#define OOM_CHECK__PRINT_OOM(p, s) +#define OOM_CHECK__PRINT_LOC(p, s, f, l) +#endif + +#if defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK) +/* + The thinking behind the ordering of Integrity Check, Full Poison Check, and + the specific *alloc function. + + 1. Integrity Check - verifies the heap management information is not corrupt. + This allows any other testing, that walks the heap, to run safely. + + 2. Place Full Poison Check before or after a specific *alloc function? + a. After, when the *alloc function operates on an existing allocation. + b. Before, when the *alloc function creates a new, not modified, allocation. + + In a free() or realloc() call, the focus is on their allocation. It is + checked 1st and reported on 1ST if an error exists. Full Posion Check is + done after. + + For malloc(), calloc(), and zalloc() Full Posion Check is done 1st since + these functions do not modify an existing allocation. +*/ +void* ICACHE_RAM_ATTR malloc(size_t size) +{ + INTEGRITY_CHECK__ABORT(); + POISON_CHECK__ABORT(); + void* ret = UMM_MALLOC(size); + PTR_CHECK__LOG_LAST_FAIL(ret, size); + OOM_CHECK__PRINT_OOM(ret, size); + return ret; +} + +void* ICACHE_RAM_ATTR calloc(size_t count, size_t size) +{ + INTEGRITY_CHECK__ABORT(); + POISON_CHECK__ABORT(); + void* ret = UMM_CALLOC(count, size); + PTR_CHECK__LOG_LAST_FAIL(ret, count * size); + OOM_CHECK__PRINT_OOM(ret, size); + return ret; +} + +void* ICACHE_RAM_ATTR realloc(void* ptr, size_t size) +{ + INTEGRITY_CHECK__ABORT(); + void* ret = UMM_REALLOC_FL(ptr, size, NULL, 0); + POISON_CHECK__ABORT(); + PTR_CHECK__LOG_LAST_FAIL(ret, size); + OOM_CHECK__PRINT_OOM(ret, size); + return ret; +} + +void ICACHE_RAM_ATTR free(void* p) +{ + INTEGRITY_CHECK__ABORT(); + UMM_FREE_FL(p, NULL, 0); + POISON_CHECK__ABORT(); +} +#endif + + +void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line) +{ + INTEGRITY_CHECK__PANIC_FL(file, line); + POISON_CHECK__PANIC_FL(file, line); + void* ret = UMM_MALLOC(size); + PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line); + OOM_CHECK__PRINT_LOC(ret, size, file, line); + return ret; +} + +void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line) +{ + INTEGRITY_CHECK__PANIC_FL(file, line); + POISON_CHECK__PANIC_FL(file, line); + void* ret = UMM_CALLOC(count, size); + PTR_CHECK__LOG_LAST_FAIL_FL(ret, count * size, file, line); + OOM_CHECK__PRINT_LOC(ret, size, file, line); + return ret; +} + +void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line) +{ + INTEGRITY_CHECK__PANIC_FL(file, line); + void* ret = UMM_REALLOC_FL(ptr, size, file, line); + POISON_CHECK__PANIC_FL(file, line); + PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line); + OOM_CHECK__PRINT_LOC(ret, size, file, line); + return ret; +} + +void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line) +{ + INTEGRITY_CHECK__PANIC_FL(file, line); + POISON_CHECK__PANIC_FL(file, line); + void* ret = UMM_CALLOC(1, size); + PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line); + OOM_CHECK__PRINT_LOC(ret, size, file, line); return ret; } void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line) { - (void) file; - (void) line; - free(ptr); -} - -#ifdef DEBUG_ESP_OOM - -void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line) -{ - return malloc_loc(size, file, line); -} - -void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line) -{ - return calloc_loc(count, size, file, line); -} - -void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line) -{ - return realloc_loc(ptr, size, file, line); -} - -void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line) -{ - return calloc_loc(1, size, file, line); -} - -#undef malloc -#undef calloc -#undef realloc - -static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n"; -static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@"; -static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n"; - -void* malloc (size_t s) -{ - void* ret = umm_malloc(s); - if (!ret) - os_printf(oom_fmt, (int)s); - return ret; -} - -void* calloc (size_t n, size_t s) -{ - void* ret = umm_calloc(n, s); - if (!ret) - os_printf(oom_fmt, (int)s); - return ret; -} - -void* realloc (void* p, size_t s) -{ - void* ret = umm_realloc(p, s); - if (!ret) - os_printf(oom_fmt, (int)s); - return ret; -} - -void print_loc (size_t s, const char* file, int line) -{ - os_printf(oom_fmt_1, (int)s); - os_printf(file); - os_printf(oom_fmt_2, line); -} - -void* malloc_loc (size_t s, const char* file, int line) -{ - void* ret = umm_malloc(s); - if (!ret) - print_loc(s, file, line); - return ret; -} - -void* calloc_loc (size_t n, size_t s, const char* file, int line) -{ - void* ret = umm_calloc(n, s); - if (!ret) - print_loc(s, file, line); - return ret; -} - -void* realloc_loc (void* p, size_t s, const char* file, int line) -{ - void* ret = umm_realloc(p, s); - if (!ret) - print_loc(s, file, line); - return ret; -} - -#else - -void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line) -{ - (void) file; - (void) line; - return malloc(size); -} - -void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line) -{ - (void) file; - (void) line; - return calloc(count, size); -} - -void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line) -{ - (void) file; - (void) line; - return realloc(ptr, size); -} - -void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line) -{ - (void) file; - (void) line; - return calloc(1, size); -} - -#endif // !defined(DEBUG_ESP_OOM) - -size_t xPortGetFreeHeapSize(void) -{ - return umm_free_heap_size(); + INTEGRITY_CHECK__PANIC_FL(file, line); + UMM_FREE_FL(ptr, file, line); + POISON_CHECK__PANIC_FL(file, line); } size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size) diff --git a/cores/esp8266/libc_replacements.cpp b/cores/esp8266/libc_replacements.cpp index 1d591abf0..b3beb07dd 100644 --- a/cores/esp8266/libc_replacements.cpp +++ b/cores/esp8266/libc_replacements.cpp @@ -100,7 +100,8 @@ int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__(( int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) { (void) r; if (file->_file == STDOUT_FILENO) { - return ets_putc(c); + ets_putc(c); + return c; } return EOF; } diff --git a/cores/esp8266/sntp-lwip2.cpp b/cores/esp8266/sntp-lwip2.cpp index 0dba9a7f8..063cc5be2 100644 --- a/cores/esp8266/sntp-lwip2.cpp +++ b/cores/esp8266/sntp-lwip2.cpp @@ -42,16 +42,22 @@ #include #include #include "coredecls.h" +#include "Schedule.h" -extern "C" { +static TrivialCB _settimeofday_cb; -static void (*_settimeofday_cb)(void) = NULL; +void settimeofday_cb (TrivialCB&& cb) +{ + _settimeofday_cb = std::move(cb); +} -void settimeofday_cb (void (*cb)(void)) +void settimeofday_cb (const TrivialCB& cb) { _settimeofday_cb = cb; } +extern "C" { + #if LWIP_VERSION_MAJOR == 1 #include @@ -478,7 +484,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz) sntp_set_system_time(tv->tv_sec); if (_settimeofday_cb) - _settimeofday_cb(); + schedule_function(_settimeofday_cb); } return 0; } diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 833dd3edb..04fc461d1 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -25,6 +25,21 @@ using namespace fs; + +// Deprecated functions, to be deleted in next release +int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) { + return flash_hal_write(addr, size, src); +} +int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) { + return flash_hal_erase(addr, size); +} +int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) { + return flash_hal_read(addr, size, dst); +} + + + + namespace spiffs_impl { FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) diff --git a/cores/esp8266/spiffs_api.h b/cores/esp8266/spiffs_api.h index c297d7d23..c19dbc2c4 100644 --- a/cores/esp8266/spiffs_api.h +++ b/cores/esp8266/spiffs_api.h @@ -39,6 +39,26 @@ extern "C" { using namespace fs; +// The following are deprecated symbols and functions, to be removed at the next major release. +// They are provided only for backwards compatibility and to give libs a chance to update. + +extern "C" uint32_t _SPIFFS_start __attribute__((deprecated)); +extern "C" uint32_t _SPIFFS_end __attribute__((deprecated)); +extern "C" uint32_t _SPIFFS_page __attribute__((deprecated)); +extern "C" uint32_t _SPIFFS_block __attribute__((deprecated)); + +#define SPIFFS_PHYS_ADDR ((uint32_t) (&_SPIFFS_start) - 0x40200000) +#define SPIFFS_PHYS_SIZE ((uint32_t) (&_SPIFFS_end) - (uint32_t) (&_SPIFFS_start)) +#define SPIFFS_PHYS_PAGE ((uint32_t) &_SPIFFS_page) +#define SPIFFS_PHYS_BLOCK ((uint32_t) &_SPIFFS_block) + +extern int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) __attribute__((deprecated)); +extern int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) __attribute__((deprecated)); +extern int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) __attribute__((deprecated)); + + + + namespace spiffs_impl { int getSpiffsMode(OpenMode openMode, AccessMode accessMode); @@ -151,11 +171,6 @@ public: bool begin() override { -#if defined(ARDUINO) && !defined(CORE_MOCK) - if (&_FS_end <= &_FS_start) - return false; -#endif - if (SPIFFS_mounted(&_fs) != 0) { return true; } diff --git a/cores/esp8266/time.cpp b/cores/esp8266/time.cpp index 001b0ec33..3a3e3f755 100644 --- a/cores/esp8266/time.cpp +++ b/cores/esp8266/time.cpp @@ -16,6 +16,7 @@ * */ +#include #include #include #include @@ -55,25 +56,12 @@ static void setServer(int id, const char* name_or_ip) { if (name_or_ip) { - //TODO: check whether server is given by name or IP + // per current configuration, + // lwIP can receive an IP address or a fqdn sntp_setservername(id, (char*) name_or_ip); } } - -void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) -{ - sntp_stop(); - - setServer(0, server1); - setServer(1, server2); - setServer(2, server3); - - sntp_set_timezone_in_seconds(timezone_sec); - sntp_set_daylight(daylightOffset_sec); - sntp_init(); -} - int clock_gettime(clockid_t unused, struct timespec *tp) { (void) unused; @@ -108,4 +96,33 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp) return 0; } -}; +}; // extern "C" + +void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) +{ + sntp_stop(); + + setServer(0, server1); + setServer(1, server2); + setServer(2, server3); + + sntp_set_timezone_in_seconds(timezone_sec); + sntp_set_daylight(daylightOffset_sec); + sntp_init(); +} + +void configTime(const char* tz, const char* server1, const char* server2, const char* server3) +{ + sntp_stop(); + + setServer(0, server1); + setServer(1, server2); + setServer(2, server3); + + char tzram[strlen_P(tz) + 1]; + memcpy_P(tzram, tz, sizeof(tzram)); + setenv("TZ", tzram, 1/*overwrite*/); + tzset(); + + sntp_init(); +} diff --git a/cores/esp8266/twi.h b/cores/esp8266/twi.h index 685221820..27aaaff64 100644 --- a/cores/esp8266/twi.h +++ b/cores/esp8266/twi.h @@ -1,23 +1,23 @@ /* - twi.h - Software I2C library for esp8266 + twi.h - Software I2C library for esp8266 - Copyright (c) 2015 Hristo Gochkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. + Copyright (c) 2015 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. - 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 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. + 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 - Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support + 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 + Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support */ #ifndef SI2C_h #define SI2C_h @@ -48,12 +48,13 @@ uint8_t twi_status(); uint8_t twi_transmit(const uint8_t*, uint8_t); -void twi_attachSlaveRxEvent( void (*)(uint8_t*, size_t) ); -void twi_attachSlaveTxEvent( void (*)(void) ); +void twi_attachSlaveRxEvent(void (*)(uint8_t*, size_t)); +void twi_attachSlaveTxEvent(void (*)(void)); void twi_reply(uint8_t); //void twi_stop(void); void twi_releaseBus(void); +void twi_enableSlaveMode(void); #ifdef __cplusplus } diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index 30b375cba..05a425c9c 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -48,6 +48,10 @@ #include "user_interface.h" #include "uart_register.h" +#define MODE2WIDTH(mode) (((mode%16)>>2)+5) +#define MODE2STOP(mode) (((mode)>>5)+1) +#define MODE2PARITY(mode) (mode%4) + /* Some general architecture for GDB integration with the UART to enable serial debugging. @@ -83,7 +87,7 @@ struct uart_rx_buffer_ uint8_t * buffer; }; -struct uart_ +struct uart_ { int uart_nr; int baud_rate; @@ -112,7 +116,7 @@ struct uart_ // called by ISR inline size_t ICACHE_RAM_ATTR -uart_rx_fifo_available(const int uart_nr) +uart_rx_fifo_available(const int uart_nr) { return (USS(uart_nr) >> USRXC) & 0xFF; } @@ -121,12 +125,12 @@ uart_rx_fifo_available(const int uart_nr) /**********************************************************/ /************ UNSAFE FUNCTIONS ****************************/ /**********************************************************/ -inline size_t -uart_rx_buffer_available_unsafe(const struct uart_rx_buffer_ * rx_buffer) +inline size_t +uart_rx_buffer_available_unsafe(const struct uart_rx_buffer_ * rx_buffer) { - if(rx_buffer->wpos < rx_buffer->rpos) + if(rx_buffer->wpos < rx_buffer->rpos) return (rx_buffer->wpos + rx_buffer->size) - rx_buffer->rpos; - + return rx_buffer->wpos - rx_buffer->rpos; } @@ -141,14 +145,14 @@ uart_rx_available_unsafe(uart_t* uart) // Copy all the rx fifo bytes that fit into the rx buffer // called by ISR inline void ICACHE_RAM_ATTR -uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart) +uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart) { struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer; while(uart_rx_fifo_available(uart->uart_nr)) { size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size; - if(nextPos == rx_buffer->rpos) + if(nextPos == rx_buffer->rpos) { if (!uart->rx_overrun) { @@ -175,17 +179,17 @@ uart_rx_copy_fifo_to_buffer_unsafe(uart_t* uart) } } -inline int +inline int uart_peek_char_unsafe(uart_t* uart) { if (!uart_rx_available_unsafe(uart)) return -1; - + //without the following if statement and body, there is a good chance of a fifo overrun if (uart_rx_buffer_available_unsafe(uart->rx_buffer) == 0) // hw fifo can't be peeked, data need to be copied to sw uart_rx_copy_fifo_to_buffer_unsafe(uart); - + return uart->rx_buffer->buffer[uart->rx_buffer->rpos]; } @@ -204,12 +208,19 @@ uart_read_char_unsafe(uart_t* uart) return -1; } +uint8_t +uart_get_bit_length(const int uart_nr) +{ + // return bit length from uart mode, +1 for the start bit which is always there. + return MODE2WIDTH(USC0(uart_nr)) + MODE2PARITY(USC0(uart_nr)) + MODE2STOP(USC0(uart_nr)) + 1; +} + size_t uart_rx_available(uart_t* uart) { if(uart == NULL || !uart->rx_enabled) return 0; - + ETS_UART_INTR_DISABLE(); int uartrxbufferavailable = uart_rx_buffer_available_unsafe(uart->rx_buffer); ETS_UART_INTR_ENABLE(); @@ -217,19 +228,19 @@ uart_rx_available(uart_t* uart) return uartrxbufferavailable + uart_rx_fifo_available(uart->uart_nr); } -int +int uart_peek_char(uart_t* uart) { if(uart == NULL || !uart->rx_enabled) return -1; - + ETS_UART_INTR_DISABLE(); //access to rx_buffer can be interrupted by the isr (similar to a critical section), so disable interrupts here int ret = uart_peek_char_unsafe(uart); ETS_UART_INTR_ENABLE(); return ret; } -int +int uart_read_char(uart_t* uart) { uint8_t ret; @@ -322,26 +333,26 @@ static void ICACHE_RAM_ATTR uart_isr_handle_data(void* arg, uint8_t data) USIC(uart->uart_nr) = usis; } -size_t +size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size) { - if(uart == NULL || !uart->rx_enabled) + if(uart == NULL || !uart->rx_enabled) return 0; - if(uart->rx_buffer->size == new_size) + if(uart->rx_buffer->size == new_size) return uart->rx_buffer->size; uint8_t * new_buf = (uint8_t*)malloc(new_size); if(!new_buf) return uart->rx_buffer->size; - + size_t new_wpos = 0; ETS_UART_INTR_DISABLE(); while(uart_rx_available_unsafe(uart) && new_wpos < new_size) new_buf[new_wpos++] = uart_read_char_unsafe(uart); //if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1 if (new_wpos == new_size) new_wpos = 0; - + uint8_t * old_buf = uart->rx_buffer->buffer; uart->rx_buffer->rpos = 0; uart->rx_buffer->wpos = new_wpos; @@ -359,13 +370,13 @@ uart_get_rx_buffer_size(uart_t* uart) } // The default ISR handler called when GDB is not enabled -void ICACHE_RAM_ATTR +void ICACHE_RAM_ATTR uart_isr(void * arg) { uart_t* uart = (uart_t*)arg; uint32_t usis = USIS(uart->uart_nr); - if(uart == NULL || !uart->rx_enabled) + if(uart == NULL || !uart->rx_enabled) { USIC(uart->uart_nr) = usis; ETS_UART_INTR_DISABLE(); @@ -380,14 +391,14 @@ uart_isr(void * arg) uart->rx_overrun = true; //os_printf_plus(overrun_str); } - + if (usis & ((1 << UIFR) | (1 << UIPE) | (1 << UITO))) uart->rx_error = true; USIC(uart->uart_nr) = usis; } -static void +static void uart_start_isr(uart_t* uart) { if(uart == NULL || !uart->rx_enabled) @@ -422,7 +433,7 @@ uart_start_isr(uart_t* uart) ETS_UART_INTR_ENABLE(); } -static void +static void uart_stop_isr(uart_t* uart) { if(uart == NULL || !uart->rx_enabled) @@ -459,7 +470,7 @@ uart_tx_fifo_full(const int uart_nr) } -static void +static void uart_do_write_char(const int uart_nr, char c) { while(uart_tx_fifo_full(uart_nr)); @@ -467,7 +478,7 @@ uart_do_write_char(const int uart_nr, char c) USF(uart_nr) = c; } -size_t +size_t uart_write_char(uart_t* uart, char c) { if(uart == NULL || !uart->tx_enabled) @@ -481,7 +492,7 @@ uart_write_char(uart_t* uart, char c) return 1; } -size_t +size_t uart_write(uart_t* uart, const char* buf, size_t size) { if(uart == NULL || !uart->tx_enabled) @@ -501,7 +512,7 @@ uart_write(uart_t* uart, const char* buf, size_t size) } -size_t +size_t uart_tx_free(uart_t* uart) { if(uart == NULL || !uart->tx_enabled) @@ -510,7 +521,7 @@ uart_tx_free(uart_t* uart) return UART_TX_FIFO_SIZE - uart_tx_fifo_available(uart->uart_nr); } -void +void uart_wait_tx_empty(uart_t* uart) { if(uart == NULL || !uart->tx_enabled) @@ -521,14 +532,14 @@ uart_wait_tx_empty(uart_t* uart) } -void +void uart_flush(uart_t* uart) { if(uart == NULL) return; uint32_t tmp = 0x00000000; - if(uart->rx_enabled) + if(uart->rx_enabled) { tmp |= (1 << UCRXRST); ETS_UART_INTR_DISABLE(); @@ -546,7 +557,7 @@ uart_flush(uart_t* uart) } } -void +void uart_set_baudrate(uart_t* uart, int baud_rate) { if(uart == NULL) @@ -556,7 +567,7 @@ uart_set_baudrate(uart_t* uart, int baud_rate) USD(uart->uart_nr) = (ESP8266_CLOCK / uart->baud_rate); } -int +int uart_get_baudrate(uart_t* uart) { if(uart == NULL) @@ -565,7 +576,7 @@ uart_get_baudrate(uart_t* uart) return uart->baud_rate; } -uart_t* +uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) { uart_t* uart = (uart_t*) malloc(sizeof(uart_t)); @@ -576,7 +587,7 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx uart->rx_overrun = false; uart->rx_error = false; - switch(uart->uart_nr) + switch(uart->uart_nr) { case UART0: ETS_UART_INTR_DISABLE(); @@ -586,10 +597,10 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx uart->rx_enabled = (mode != UART_TX_ONLY); uart->tx_enabled = (mode != UART_RX_ONLY); uart->rx_pin = (uart->rx_enabled)?3:255; - if(uart->rx_enabled) + if(uart->rx_enabled) { struct uart_rx_buffer_ * rx_buffer = (struct uart_rx_buffer_ *)malloc(sizeof(struct uart_rx_buffer_)); - if(rx_buffer == NULL) + if(rx_buffer == NULL) { free(uart); return NULL; @@ -598,7 +609,7 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx rx_buffer->rpos = 0; rx_buffer->wpos = 0; rx_buffer->buffer = (uint8_t *)malloc(rx_buffer->size); - if(rx_buffer->buffer == NULL) + if(rx_buffer->buffer == NULL) { free(rx_buffer); free(uart); @@ -607,20 +618,20 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx uart->rx_buffer = rx_buffer; pinMode(uart->rx_pin, SPECIAL); } - if(uart->tx_enabled) + if(uart->tx_enabled) { - if (tx_pin == 2) + if (tx_pin == 2) { uart->tx_pin = 2; pinMode(uart->tx_pin, FUNCTION_4); - } - else + } + else { uart->tx_pin = 1; pinMode(uart->tx_pin, FUNCTION_0); } - } - else + } + else { uart->tx_pin = 255; } @@ -666,7 +677,7 @@ uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx return uart; } -void +void uart_uninit(uart_t* uart) { if(uart == NULL) @@ -675,7 +686,7 @@ uart_uninit(uart_t* uart) uart_stop_isr(uart); if(uart->tx_enabled && (!gdbstub_has_uart_isr_control() || uart->uart_nr != UART0)) { - switch(uart->tx_pin) + switch(uart->tx_pin) { case 1: pinMode(1, INPUT); @@ -693,7 +704,7 @@ uart_uninit(uart_t* uart) free(uart->rx_buffer->buffer); free(uart->rx_buffer); if(!gdbstub_has_uart_isr_control()) { - switch(uart->rx_pin) + switch(uart->rx_pin) { case 3: pinMode(3, INPUT); @@ -707,16 +718,16 @@ uart_uninit(uart_t* uart) free(uart); } -void +void uart_swap(uart_t* uart, int tx_pin) { if(uart == NULL) return; - switch(uart->uart_nr) + switch(uart->uart_nr) { case UART0: - if(((uart->tx_pin == 1 || uart->tx_pin == 2) && uart->tx_enabled) || (uart->rx_pin == 3 && uart->rx_enabled)) + if(((uart->tx_pin == 1 || uart->tx_pin == 2) && uart->tx_enabled) || (uart->rx_pin == 3 && uart->rx_enabled)) { if(uart->tx_enabled) //TX { @@ -728,15 +739,15 @@ uart_swap(uart_t* uart, int tx_pin) pinMode(uart->rx_pin, INPUT); uart->rx_pin = 13; } - if(uart->tx_enabled) + if(uart->tx_enabled) pinMode(uart->tx_pin, FUNCTION_4); //TX if(uart->rx_enabled) pinMode(uart->rx_pin, FUNCTION_4); //RX - + IOSWAP |= (1 << IOSWAPU0); - } - else + } + else { if(uart->tx_enabled) //TX { @@ -751,7 +762,7 @@ uart_swap(uart_t* uart, int tx_pin) if(uart->tx_enabled) pinMode(uart->tx_pin, (tx_pin == 2)?FUNCTION_4:SPECIAL); //TX - if(uart->rx_enabled) + if(uart->rx_enabled) pinMode(3, SPECIAL); //RX IOSWAP &= ~(1 << IOSWAPU0); @@ -765,24 +776,24 @@ uart_swap(uart_t* uart, int tx_pin) } } -void +void uart_set_tx(uart_t* uart, int tx_pin) { if(uart == NULL) return; - switch(uart->uart_nr) + switch(uart->uart_nr) { case UART0: - if(uart->tx_enabled) + if(uart->tx_enabled) { - if (uart->tx_pin == 1 && tx_pin == 2) + if (uart->tx_pin == 1 && tx_pin == 2) { pinMode(uart->tx_pin, INPUT); uart->tx_pin = 2; pinMode(uart->tx_pin, FUNCTION_4); - } - else if (uart->tx_pin == 2 && tx_pin != 2) + } + else if (uart->tx_pin == 2 && tx_pin != 2) { pinMode(uart->tx_pin, INPUT); uart->tx_pin = 1; @@ -799,7 +810,7 @@ uart_set_tx(uart_t* uart, int tx_pin) } } -void +void uart_set_pins(uart_t* uart, int tx, int rx) { if(uart == NULL) @@ -807,13 +818,13 @@ uart_set_pins(uart_t* uart, int tx, int rx) if(uart->uart_nr == UART0) // Only UART0 allows pin changes { - if(uart->tx_enabled && uart->tx_pin != tx) + if(uart->tx_enabled && uart->tx_pin != tx) { - if( rx == 13 && tx == 15) + if( rx == 13 && tx == 15) { uart_swap(uart, 15); - } - else if (rx == 3 && (tx == 1 || tx == 2)) + } + else if (rx == 3 && (tx == 1 || tx == 2)) { if (uart->rx_pin != rx) uart_swap(uart, tx); @@ -827,7 +838,7 @@ uart_set_pins(uart_t* uart, int tx, int rx) } -bool +bool uart_tx_enabled(uart_t* uart) { if(uart == NULL) @@ -836,7 +847,7 @@ uart_tx_enabled(uart_t* uart) return uart->tx_enabled; } -bool +bool uart_rx_enabled(uart_t* uart) { if(uart == NULL) @@ -845,7 +856,7 @@ uart_rx_enabled(uart_t* uart) return uart->rx_enabled; } -bool +bool uart_has_overrun (uart_t* uart) { if (uart == NULL || !uart->rx_overrun) @@ -867,7 +878,7 @@ uart_has_rx_error (uart_t* uart) return true; } -static void +static void uart_ignore_char(char c) { (void) c; @@ -883,34 +894,42 @@ uart_write_char_delay(const int uart_nr, char c) } -static void +static void uart0_write_char(char c) { uart_write_char_delay(0, c); } -static void +static void uart1_write_char(char c) { uart_write_char_delay(1, c); } -void +void uart_set_debug(int uart_nr) { s_uart_debug_nr = uart_nr; - void (*func)(char) = NULL; + fp_putc_t func = NULL; switch(s_uart_debug_nr) { case UART0: func = &uart0_write_char; + // This selects the UART for ROM ets_putc which is used by + // ::printf, ets_printf_P in core_esp_postmortem.cpp and others. + // Has a side effect of clearing RX FIFO for UART0 + uart_buff_switch(0); break; case UART1: func = &uart1_write_char; + uart_buff_switch(1); break; case UART_NO: default: func = &uart_ignore_char; + // There is no disable option for ets_putc, + // we switch to UART0 for disable case. + uart_buff_switch(0); break; } if(gdbstub_has_putc1_control()) { @@ -921,11 +940,11 @@ uart_set_debug(int uart_nr) } else { system_set_os_print(0); } - ets_install_putc1((void *) func); + ets_install_putc1(func); } } -int +int uart_get_debug() { return s_uart_debug_nr; diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index 7f9dce0f0..fd6158568 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -147,6 +147,7 @@ int uart_get_debug(); void uart_start_detect_baudrate(int uart_nr); int uart_detect_baudrate(int uart_nr); +uint8_t uart_get_bit_length(const int uart_nr); #if defined (__cplusplus) } // extern "C" diff --git a/cores/esp8266/umm_malloc/Notes.h b/cores/esp8266/umm_malloc/Notes.h new file mode 100644 index 000000000..e9d96b1e8 --- /dev/null +++ b/cores/esp8266/umm_malloc/Notes.h @@ -0,0 +1,251 @@ +#if 0 +/* + * This .h is nothing but comments about thoughts and observations made while + * updating the Arduino ESP8266 Core, with the new upstream umm_malloc. It is + * added as a .h so that it does not get lost and to avoid cluttering up the + * code with a huge block comment. + + +PR text description: + +upstream version of `umm_malloc` customized for Arduino ESP8266 Core + +This updates the heap management library, umm_malloc, to the current upstream +version at https://github.com/rhempel/umm_malloc. Some reorganizing and new code +was needed to use the new version. + +This is a list of noteworthy changes: + +UMM_POISON - now has a lite option as well as the previous intensive check +option. The code for running the full poison test at the call of the various +alloc functions was removed in the upstream version. In this port, the missing +code was added to heap.cpp and umm_local.cpp. +* UMM_POISON - appears to have been partially changed to UMM_POISON_CHECK, + I treat it as deprecated and used UMM_POISON_CHECK when needed. + However, the Arduino Core's references to UMM_POISON were replaced with + UMM_POISON_CHECK_LITE. +* UMM_POISON_CHECK_LITE - Less intense, it just checks poison on active + neighboring allocations. +* UMM_POISON_CHECK - Full heap intensive check of poison + +A cautionary note, on the use of UMM_INTEGRITY_CHECK and UMM_POISON_CHECK, and +umm_info(). All of these run with IRQs disabled, for periods that can go into +100's of us. With umm_info(NULL, true) that may go into seconds, depending on +the serial interface speed and the number of memory allocations present. Use +UMM_INTEGRITY_CHECK, UMM_POISON_CHECK, and umm_info() sparingly. If you want to +see numbers for the disabled time, explore using UMM_CRITICAL_METRICS in +umm_malloc_cfg.h. + +=============================================================================== + + New upstream umm_malloc feature delta's from the old umm_malloc we were using: + + umm_posion check for a given *alloc - failure - no longer panics. + + option to run full poison check at each *alloc call, not present + + option to run full interity check at each *alloc call, not present + + upstream code does not call panic from poison_check_block. + + Defragmenting effect of realloc is gone. It now minimizes copy. This + may have been an accident during code cleanup. + + In one form or another these features have been restored in the + reintegration of the upstream umm_malloc into the Arduino ESP8266 Core. + +=============================================================================== + + A list of changes made for local adaptation of newer upstream umm_malloc. + + In umm_malloc.c + Renamed to umm_malloc.cpp + Added `extern "C" { ... };` around code. + Surround DBGLOG_LEVEL with #ifndef... Define value of DBGLOG_LEVEL from + umm_malloc_cfg.h + umm_realloc() - Added UMM_CRITICAL_SUSPEND()/UMM_CRITICAL_RESUME() for when + lightweight locks are available. eg. sti/cli. Single threaded single CPU + case. + umm_realloc() - appears to have been refactored to minimize memmove and + memcpy. The old version would always combine an adjacent block in the + direction of the start of the heap when available and do a memmove. This + had a defragging effect. This appears to have been replaced with an attempt + to minimize copy when possible. + Added heap stats tracking. + + In umm_info.c + umm_info() - Added UMM_CRITICAL_DECL(id_info), updated critical sections + with tag. + Carried forward: Added NULL ptr check at beginning (umm_malloc.c). + + In umm_poison.c: + Resolved C++ compiler error reported on get_poisoned(), and get_unpoisoned(). + They now take in void * arg instead of unsigned char *. + Added #if ... || defined(UMM_POISON_CHECK_LITE) to the conditional. + + In umm_integrity.c: + Replaced printf with DBGLOG_FUNCTION. This needs to be a malloc free + function and ISR safe. + Added critical sections. + + In umm_malloc_cfg.h: + Added macro UMM_CRITICAL_SUSPEND()/UMM_CRITICAL_RESUME() + + Globally change across all files %i to %d: umm_info.c, umm_malloc.c, + Added a #ifdef BUILD_UMM_MALLOC_C fence to prevent Arduino IDE from building + the various .c files that are #included into umm_malloc.cpp. They are + normally enabled by #define in umm_malloc_cfg.h. In this + case it builds fine; however, if the define is global, the IDE will try and + build the .c by itself. + + Notes, + umm_integrity_check() is called by macro INTEGRITY_CHECK which returns 1 + on success. No corruption. Does a time consuming scan of the whole heap. + It will call UMM_HEAP_CORRUPTION_CB if an error is found. + + umm_poison_check(), formerly known as check_poison_all_blocks(), + is called by macro POISON_CHECK which returns 1 on success for no + corruption. Does a time consuming scan of all active allocations for + modified poison. The new upstream version does *NOT* call + UMM_HEAP_CORRUPTION_CB if an error is found. The option description says + it does! + + umm_poison_realloc() and umm_poison_free() no longer call the macro + UMM_HEAP_CORRUPTION_CB on poison error. Just a printf message is + generated. I have added alternative functions umm_poison_free_fl, + umm_poison_realloc_fl, and get_unpoisoned_check_neighbors in + umm_local.cpp. These expand the poison check on the current allocation to + include its nearest allocated neighbors in the heap. + + umm_malloc() has been extended to call check_poison_neighbors for the + allocation it selects, conditionally for UMM_POISON_CHECK_LITE. + + For upstream umm_malloc "# define POISON_CHECK() 0" should have been 1 + add to list to report. + + +============================================================================== + +Notes from searching for the best print option + +Printing from the malloc routines is tricky. Since a print library +might call *alloc. Then recursion may follow as each error call may fail +into another error and so on. + +Objective: To be able to print "last gasp" diagnostic messages +when interrupts are disabled and w/o availability of heap resources. + +It turns out things are more complicated than that. These are three cases for +printing from the heap and the current solution.: + + 1. Printing detailed heap info through `umm_info(NULL, 1);`. This function + resides in flash and can only be called from non-ISR context. It can use + PROGMEM strings. Because SPI bus will not be busy when called from foreground. + + At this time it is believed that, while running from foreground, a cache-miss + at INTLEVEL 15 can be handled. The key factor being the SPI bus is not + busy at the time of the cache-miss. It is not clear what gets invoked to + process the cache-miss. A software vector call? A hardware assisted transfer? + In any case `umm_info_safe_printf_P()` is also in flash. + + The focus here is to print w/o allocating memory and use strings + in flash to preserve DRAM. + + 2. Printing diagnostic messages possibly from from ISR context. + + Use `ets_uart_printf()` in boot ROM. + + 3. Printing diagnostic messages from `heap.cpp` these printf's need to check + `system_get_os_print()` to confirm debug-output is enabled just as + `os_printf()` did. + + Do test calls to `system_get_os_print()` and call `ets_uart_printf()` + in boot ROM when debug print allowed. + +Considerations: +* can be called from ISR +* can be called from malloc code, cannot use malloc +* can be called from malloc code that was called from an ISR +* can be called from within a critical section, eg. xt_rsil(15); + * this may be effectively the same as being called from an ISR? + Update: Current thinking is that from foreground we have more leeway + than an ISR. + +Knowns: +* ets_printf - For RTOS SDK they replaced this function with one in the SDK. + Most of the problems I can see with ets_printf center around not being + able to maintain a port to thread context. That is you cannot have one + thread using one port while another thread uses the other. In the no OS + case we cannot have one area of code using one port and another area of + code using the other port. Most of the ROM printf functions are not built + to support this kind of usage. Things get especially dangerous when you + try to use the ets_external_printf stuff. +* ets_vprintf - by itself is safe. +* newlibc printf - not safe - lives in flash. +* newlibc snprintf - not safe - lives in flash. +* builtin putc1 print function - Is installed when you use + ets_install_uart_printf. Which calls ets_install_putc1. The selection of UART + is performed by calling uart_buff_switch with 0 for UART0 and 1 for UART1. + This should work for our purpose here, if handled as follows: + * call uart_buff_switch at each printf call to reselect UART + * Update: uart_buff_switch is now updated by uart_set_debug() in uart.cpp + * use a stack buffer to hold a copy the PROGMEM string to print from. + * use ets_vprintf for printing with putc1 function. +* os_printf_plus looks interesting. It is in IRAM. If no heap is available it + will use up to 64 bytes of stack space to copy a PROGMEM fmt for printing. + Issues: + * Printing is turned off by system_set_os_print + * putc1 needs to be in IRAM - this is a uart.cpp issue + * Need to force system_get_free_heap_size to return 0 during critical periods. + * won't work for umm_info if it prints over 64 characters. + * along with umm_info there are other debug messages that exceed 64 characters. +* ets_uart_printf - Appears safe. Just no PROGMEM support. Uses + uart_buff_switch to select UART. + +=============================================================================== + +heap.cpp is the entry point for most of the heap API calls. +It is a merge point for abstracted heap API calls, such as _malloc_r, +pvPortMalloc, and malloc. Thin wrappers are created here for these entry points +and others. The wrappers call through to equivalent umm_malloc entry-point. +These wrappers also provide the access points to do debug options, like OOM, +Integrity Check, and Poison Check. + +-DEBUG_ESP_OOM or select `Debug Level: "OOM"` from the IDE. +This option will add extra code to save information on the last OOM event. If +your code is built with the `Debug port: "Serial"` option, debug messages will +print on OOM events. You do not have to do `Debug port: "Serial"` to get OOM +debug messages. From within your code, you can make a call to +`Serial.debugOutput(true);` to enable OOM printing. Of course for this to work +your code must be built with `Debug Level: "OOM"` or equal. + +-DUUM_POISON is now the same as -DUMM_POISON_CHECK_LITE +This is new behavior with this updated. UMM_POISON_CHECK_LITE - checks the +allocation presented at realloc() and free(). Expands the poison check on the +current allocation to include its nearest allocated neighbors in the heap. +umm_malloc() will also check the neighbors of the selected allocation before +use. + +For more details and options search on UMM_POISON_CHECK in `umm_malloc_cfg.h` + +TODO: provide some interesting numbers on the time to perform: +* UMM_POISON_CHECK +* UMM_INTEGRITY_CHECK +* umm_info(NUll, 0) built with and without print capability +* umm_info(NUll, 1) printing a report to Serial device. + +=============================================================================== + +Enhancement ideas: + 1. Add tagging to heap allocations. Redefine UMM_POISONED_BLOCK_LEN_TYPE, + expand it to include an element for the calling address of allocating + requester. Expand umm_info(NULL, 1) to print the respective address with each + active allocation. The difficulty here will be the ever-growing complexity of + overlapping build options. I think it would be easiest to build this in with + and expand the UMM_POISON_CHECK_LITE option. + + 2. A build option to not have printing, from umm_info() compiled in. This can + save on the execution time spent with interrupts disabled. + +*/ +#endif diff --git a/cores/esp8266/umm_malloc/README.md b/cores/esp8266/umm_malloc/README.md index 1c7805a1c..f81aa09cc 100644 --- a/cores/esp8266/umm_malloc/README.md +++ b/cores/esp8266/umm_malloc/README.md @@ -10,18 +10,18 @@ might get expensive. ## Acknowledgements -Joerg Wunsch and the avr-libc provided the first malloc() implementation +Joerg Wunsch and the avr-libc provided the first `malloc()` implementation that I examined in detail. -http://www.nongnu.org/avr-libc +`http://www.nongnu.org/avr-libc` Doug Lea's paper on malloc() was another excellent reference and provides a lot of detail on advanced memory management techniques such as binning. -http://g.oswego.edu/dl/html/malloc.html +`http://g.oswego.edu/dl/html/malloc.html` Bill Dittman provided excellent suggestions, including macros to support -using these functions in critical sections, and for optimizing realloc() +using these functions in critical sections, and for optimizing `realloc()` further by checking to see if the previous block was free and could be used for the new block size. This can help to reduce heap fragmentation significantly. @@ -30,11 +30,73 @@ Yaniv Ankin suggested that a way to dump the current heap condition might be useful. I combined this with an idea from plarroy to also allow checking a free pointer to make sure it's valid. +Dimitry Frank contributed many helpful additions to make things more +robust including a user specified config file and a method of testing +the integrity of the data structures. + +## Usage + +Copy the `umm_malloc_cfg_example.h` file to `umm_malloc_cfg.h` and +make the changes required to support your application. + +The following `#define`s must be set to something useful for the +library to work at all + +- `UMM_MALLOC_CFG_HEAP_ADDR` must be set to the symbol representing + the starting address of the heap. The heap must be + aligned on the natural boundary size of the processor. +- `UMM_MALLOC_CFG_HEAP_SIZE` must be set to the size of the heap. + The heap size must be a multiple of the natural boundary size of + the processor. + +The fit algorithm is defined as either: + +- `UMM_BEST_FIT` which scans the entire free list and looks + for either an exact fit or the smallest block that will + satisfy the request. This is the default fit method. +- `UMM_FIRST_FIT` which scans the entire free list and looks + for the first block that satisfies the request. + +The following `#define`s are disabled by default and should +remain disabled for production use. They are helpful when +testing allocation errors (which are normally due to bugs in +the application code) or for running the test suite when +making changes to the code. + +You can define them in your compiler command line or uncomment +the corresponding entries is `umm_malloc_cfg.h`: + +- `UMM_INFO` is used to include code that allows dumping + the entire heap structure (helpful when there's a problem). + +- `UMM_INTEGRITY_CHECK` is used to include code that + performs an integrity check on the heap structure. It's + up to you to call the `umm_integrity_check()` function. + +- `UMM_POISON_CHECK` is used to include code that + adds some bytes around the memory being allocated that + are filled with known data. If the data is not intact + when the block is checked, then somone has written outside + of the memory block they have been allocated. It is up + to you to call the `umm_poison_check()` function. + +## API + +The following functions are available for your application: + +- `void *umm_malloc( size_t size );` +- `void *umm_calloc( size_t num, size_t size );` +- `void *umm_realloc( void *ptr, size_t size );` +- `void umm_free( void *ptr );` + +They have exactly the same semantics as the corresponding standard library +functions. + ## Background The memory manager assumes the following things: -1. The standard POSIX compliant malloc/realloc/free semantics are used +1. The standard POSIX compliant malloc/calloc/realloc/free semantics are used 1. All memory used by the manager is allocated at link time, it is aligned on a 32 bit boundary, it is contiguous, and its extent (start and end address) is filled in by the linker. @@ -45,17 +107,17 @@ The fastest linked list implementations use doubly linked lists so that its possible to insert and delete blocks in constant time. This memory manager keeps track of both free and used blocks in a doubly linked list. -Most memory managers use some kind of list structure made up of pointers +Most memory managers use a list structure made up of pointers to keep track of used - and sometimes free - blocks of memory. In an embedded system, this can get pretty expensive as each pointer can use up to 32 bits. -In most embedded systems there is no need for managing large blocks -of memory dynamically, so a full 32 bit pointer based data structure +In most embedded systems there is no need for managing a large quantity +of memory blocks dynamically, so a full 32 bit pointer based data structure for the free and used block lists is wasteful. A block of memory on the free list would use 16 bytes just for the pointers! -This memory management library sees the malloc heap as an array of blocks, +This memory management library sees the heap as an array of blocks, and uses block numbers to keep track of locations. The block numbers are 15 bits - which allows for up to 32767 blocks of memory. The high order bit marks a block as being either free or in use, which will be explained @@ -75,7 +137,7 @@ can always add more data bytes to the body of the memory block at the expense of free block size overhead. There are a lot of little features and optimizations in this memory -management system that makes it especially suited to small embedded, but +management system that makes it especially suited to small systems, and the best way to appreciate them is to review the data structures and algorithms used, so let's get started. @@ -124,10 +186,9 @@ Where: - n is the index of the next block in the heap - p is the index of the previous block in the heap -Note that the free list information is gone, because it's now being used to -store actual data for the application. It would have been nice to store -the next and previous free list indexes as well, but that would be a waste -of space. If we had even 500 items in use, that would be 2,000 bytes for +Note that the free list information is gone because it's now +being used to store actual data for the application. If we had +even 500 items in use, that would be 2,000 bytes for free list information. We simply can't afford to waste that much. The address of the `...` area is what is returned to the application @@ -143,7 +204,7 @@ described. `...` between memory blocks indicates zero or more additional blocks are allocated for use by the upper block. -And while we're talking about "upper" and "lower" blocks, we should make +While we're talking about "upper" and "lower" blocks, we should make a comment about adresses. In the diagrams, a block higher up in the picture is at a lower address. And the blocks grow downwards their block index increases as does their physical address. @@ -166,24 +227,27 @@ we're at the end of the list! At this point, the malloc has a special test that checks if the current block index is 0, which it is. This special case initializes the free -list to point at block index 1. +list to point at block index 1 and then points block 1 to the +last block (lf) on the heap. ``` BEFORE AFTER +----+----+----+----+ +----+----+----+----+ -0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | +0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | +----+----+----+----+ +----+----+----+----+ +----+----+----+----+ - 1 | 0 | 0 | 0 | 0 | + 1 |*lf | 0 | 0 | 0 | + +----+----+----+----+ + ... + +----+----+----+----+ + lf | 0 | 1 | 0 | 0 | +----+----+----+----+ ``` The heap is now ready to complete the first malloc operation. - -### Operation of malloc when we have reached the end of the free list and -there is no block large enough to accommodate the request. +### Operation of malloc when we have reached the end of the free list and there is no block large enough to accommodate the request. This happens at the very first malloc operation, or any time the free list is traversed and no free block large enough for the request is @@ -306,8 +370,7 @@ else ``` Step 1 of the free operation checks if the next block is free, and if it -is then insert this block into the free list and assimilate the next block -with this one. +is assimilate the next block with this one. Note that c is the block we are freeing up, cf is the free block that follows it. diff --git a/cores/esp8266/umm_malloc/dbglog/LICENSE b/cores/esp8266/umm_malloc/dbglog/LICENSE new file mode 100644 index 000000000..25e920054 --- /dev/null +++ b/cores/esp8266/umm_malloc/dbglog/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Ralph Hempel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cores/esp8266/umm_malloc/dbglog/README.txt b/cores/esp8266/umm_malloc/dbglog/README.txt new file mode 100644 index 000000000..54f86e148 --- /dev/null +++ b/cores/esp8266/umm_malloc/dbglog/README.txt @@ -0,0 +1 @@ +Downloaded from: https://github.com/rhempel/c-helper-macros/tree/develop diff --git a/cores/esp8266/umm_malloc/dbglog/dbglog.h b/cores/esp8266/umm_malloc/dbglog/dbglog.h new file mode 100644 index 000000000..16f57236c --- /dev/null +++ b/cores/esp8266/umm_malloc/dbglog/dbglog.h @@ -0,0 +1,98 @@ +/* ---------------------------------------------------------------------------- + * dbglog.h - A set of macros that cleans up code that needs to produce debug + * or log information. + * + * Many embedded systems still put a premium on code space and therefore need + * a way to conditionally compile in debug code. Yes, it can lead to code that + * runs differently depending on whether the debug code is cmpiled in or not + * but you need to be able to evaluate the tradeoff. + * + * See copyright notice in LICENSE.TXT + * ---------------------------------------------------------------------------- + * NOTE WELL that this file may be included multiple times - this allows you + * to set the trace level #define DBGLOG_LEVEL x + * + * To update which of the DBGLOG macros are compiled in, you must redefine the + * DBGLOG_LEVEL macro and the inlcude the dbglog.h file again, like this: + * + * #undef DBGLOG_LEVEL + * #define DBGLOG_LEVEL 6 + * #include "dbglog/dbglog.txt" + * + * To handle multiple inclusion, we need to first undefine any macros we define + * so that the compiler does not warn us that we are changing a macro. + * ---------------------------------------------------------------------------- + * The DBGLOG_LEVEL and DBGLOG_FUNCTION should be defined BEFORE this + * file is included or else the following defaults are used: + * + * #define DBGLOG_LEVEL 0 + * #define DBGLOG_FUNCTION printf + * ---------------------------------------------------------------------------- + * There are macros to handle the following decreasing levels of detail: + * + * 6 = TRACE + * 5 = DEBUG + * 4 = CRITICAL + * 3 = ERROR + * 2 = WARNING + * 1 = INFO + * 0 = FORCE - The DBGLOG_FUNCTION is always compiled in and is called only when + * the first parameter to the macro is non-0 + * ---------------------------------------------------------------------------- + */ + +#undef DBGLOG_TRACE +#undef DBGLOG_DEBUG +#undef DBGLOG_CRITICAL +#undef DBGLOG_ERROR +#undef DBGLOG_WARNING +#undef DBGLOG_INFO +#undef DBGLOG_FORCE + +#ifndef DBGLOG_LEVEL +# define DBGLOG_LEVEL 0 +#endif + +#ifndef DBGLOG_FUNCTION +# define DBGLOG_FUNCTION printf +#endif + +/* ------------------------------------------------------------------------- */ + +#if DBGLOG_LEVEL >= 6 +# define DBGLOG_TRACE(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_TRACE(format, ...) +#endif + +#if DBGLOG_LEVEL >= 5 +# define DBGLOG_DEBUG(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_DEBUG(format, ...) +#endif + +#if DBGLOG_LEVEL >= 4 +# define DBGLOG_CRITICAL(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_CRITICAL(format, ...) +#endif + +#if DBGLOG_LEVEL >= 3 +# define DBGLOG_ERROR(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_ERROR(format, ...) +#endif + +#if DBGLOG_LEVEL >= 2 +# define DBGLOG_WARNING(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_WARNING(format, ...) +#endif + +#if DBGLOG_LEVEL >= 1 +# define DBGLOG_INFO(format, ...) DBGLOG_FUNCTION(format, ## __VA_ARGS__) +#else +# define DBGLOG_INFO(format, ...) +#endif + +#define DBGLOG_FORCE(force, format, ...) {if(force) {DBGLOG_FUNCTION(format, ## __VA_ARGS__);}} diff --git a/cores/esp8266/umm_malloc/umm_info.c b/cores/esp8266/umm_malloc/umm_info.c new file mode 100644 index 000000000..96a0f89be --- /dev/null +++ b/cores/esp8266/umm_malloc/umm_info.c @@ -0,0 +1,185 @@ +#if defined(BUILD_UMM_MALLOC_C) + +#ifdef UMM_INFO + +/* ---------------------------------------------------------------------------- + * One of the coolest things about this little library is that it's VERY + * easy to get debug information about the memory heap by simply iterating + * through all of the memory blocks. + * + * As you go through all the blocks, you can check to see if it's a free + * block by looking at the high order bit of the next block index. You can + * also see how big the block is by subtracting the next block index from + * the current block number. + * + * The umm_info function does all of that and makes the results available + * in the ummHeapInfo structure. + * ---------------------------------------------------------------------------- + */ + +UMM_HEAP_INFO ummHeapInfo; + +void *umm_info( void *ptr, int force ) { + UMM_CRITICAL_DECL(id_info); + + unsigned short int blockNo = 0; + + if (umm_heap == NULL) { + umm_init(); + } + + /* Protect the critical section... */ + UMM_CRITICAL_ENTRY(id_info); + + /* + * Clear out all of the entries in the ummHeapInfo structure before doing + * any calculations.. + */ + memset( &ummHeapInfo, 0, sizeof( ummHeapInfo ) ); + + DBGLOG_FORCE( force, "\n" ); + DBGLOG_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" ); + DBGLOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5d|NF %5d|PF %5d|\n", + (unsigned long)(&UMM_BLOCK(blockNo)), + blockNo, + UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, + UMM_PBLOCK(blockNo), + (UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK )-blockNo, + UMM_NFREE(blockNo), + UMM_PFREE(blockNo) ); + + /* + * Now loop through the block lists, and keep track of the number and size + * of used and free blocks. The terminating condition is an nb pointer with + * a value of zero... + */ + + blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; + + while( UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK ) { + size_t curBlocks = (UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK )-blockNo; + + ++ummHeapInfo.totalEntries; + ummHeapInfo.totalBlocks += curBlocks; + + /* Is this a free block? */ + + if( UMM_NBLOCK(blockNo) & UMM_FREELIST_MASK ) { + ++ummHeapInfo.freeEntries; + ummHeapInfo.freeBlocks += curBlocks; + ummHeapInfo.freeSize2 += (unsigned int)curBlocks + * (unsigned int)sizeof(umm_block) + * (unsigned int)curBlocks + * (unsigned int)sizeof(umm_block); + + if (ummHeapInfo.maxFreeContiguousBlocks < curBlocks) { + ummHeapInfo.maxFreeContiguousBlocks = curBlocks; + } + + DBGLOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5u|NF %5d|PF %5d|\n", + (unsigned long)(&UMM_BLOCK(blockNo)), + blockNo, + UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, + UMM_PBLOCK(blockNo), + (unsigned int)curBlocks, + UMM_NFREE(blockNo), + UMM_PFREE(blockNo) ); + + /* Does this block address match the ptr we may be trying to free? */ + + if( ptr == &UMM_BLOCK(blockNo) ) { + + /* Release the critical section... */ + UMM_CRITICAL_EXIT(id_info); + + return( ptr ); + } + } else { + ++ummHeapInfo.usedEntries; + ummHeapInfo.usedBlocks += curBlocks; + + DBGLOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5u|\n", + (unsigned long)(&UMM_BLOCK(blockNo)), + blockNo, + UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, + UMM_PBLOCK(blockNo), + (unsigned int)curBlocks ); + } + + blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; + } + + /* + * Update the accounting totals with information from the last block, the + * rest must be free! + */ + + { + size_t curBlocks = UMM_NUMBLOCKS-blockNo; + ummHeapInfo.freeBlocks += curBlocks; + ummHeapInfo.totalBlocks += curBlocks; + + if (ummHeapInfo.maxFreeContiguousBlocks < curBlocks) { + ummHeapInfo.maxFreeContiguousBlocks = curBlocks; + } + } + + DBGLOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5d|NF %5d|PF %5d|\n", + (unsigned long)(&UMM_BLOCK(blockNo)), + blockNo, + UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, + UMM_PBLOCK(blockNo), + UMM_NUMBLOCKS-blockNo, + UMM_NFREE(blockNo), + UMM_PFREE(blockNo) ); + + DBGLOG_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" ); + + DBGLOG_FORCE( force, "Total Entries %5d Used Entries %5d Free Entries %5d\n", + ummHeapInfo.totalEntries, + ummHeapInfo.usedEntries, + ummHeapInfo.freeEntries ); + + DBGLOG_FORCE( force, "Total Blocks %5d Used Blocks %5d Free Blocks %5d\n", + ummHeapInfo.totalBlocks, + ummHeapInfo.usedBlocks, + ummHeapInfo.freeBlocks ); + + DBGLOG_FORCE( force, "+--------------------------------------------------------------+\n" ); + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) + if (ummHeapInfo.freeBlocks == ummStats.free_blocks) { + DBGLOG_FORCE( force, "heap info Free blocks and heap statistics Free blocks match.\n"); + } else { + DBGLOG_FORCE( force, "\nheap info Free blocks %5d != heap statistics Free Blocks %5d\n\n", + ummHeapInfo.freeBlocks, + ummStats.free_blocks ); + } + DBGLOG_FORCE( force, "+--------------------------------------------------------------+\n" ); + + print_stats(force); +#endif + + /* Release the critical section... */ + UMM_CRITICAL_EXIT(id_info); + + return( NULL ); +} + +/* ------------------------------------------------------------------------ */ + +size_t umm_free_heap_size( void ) { + umm_info(NULL, 0); + return (size_t)ummHeapInfo.freeBlocks * sizeof(umm_block); +} + +size_t umm_max_block_size( void ) { + umm_info(NULL, 0); + return ummHeapInfo.maxFreeContiguousBlocks * sizeof(umm_block); +} + +/* ------------------------------------------------------------------------ */ + +#endif + +#endif // defined(BUILD_UMM_MALLOC_C) diff --git a/cores/esp8266/umm_malloc/umm_integrity.c b/cores/esp8266/umm_malloc/umm_integrity.c new file mode 100644 index 000000000..ff3cb24a1 --- /dev/null +++ b/cores/esp8266/umm_malloc/umm_integrity.c @@ -0,0 +1,134 @@ +#if defined(BUILD_UMM_MALLOC_C) +/* integrity check (UMM_INTEGRITY_CHECK) {{{ */ +#if defined(UMM_INTEGRITY_CHECK) +/* + * Perform integrity check of the whole heap data. Returns 1 in case of + * success, 0 otherwise. + * + * First of all, iterate through all free blocks, and check that all backlinks + * match (i.e. if block X has next free block Y, then the block Y should have + * previous free block set to X). + * + * Additionally, we check that each free block is correctly marked with + * `UMM_FREELIST_MASK` on the `next` pointer: during iteration through free + * list, we mark each free block by the same flag `UMM_FREELIST_MASK`, but + * on `prev` pointer. We'll check and unmark it later. + * + * Then, we iterate through all blocks in the heap, and similarly check that + * all backlinks match (i.e. if block X has next block Y, then the block Y + * should have previous block set to X). + * + * But before checking each backlink, we check that the `next` and `prev` + * pointers are both marked with `UMM_FREELIST_MASK`, or both unmarked. + * This way, we ensure that the free flag is in sync with the free pointers + * chain. + */ +int umm_integrity_check(void) { + UMM_CRITICAL_DECL(id_integrity); + int ok = 1; + unsigned short int prev; + unsigned short int cur; + + if (umm_heap == NULL) { + umm_init(); + } + + /* Iterate through all free blocks */ + prev = 0; + UMM_CRITICAL_ENTRY(id_integrity); + while(1) { + cur = UMM_NFREE(prev); + + /* Check that next free block number is valid */ + if (cur >= UMM_NUMBLOCKS) { + DBGLOG_FUNCTION("heap integrity broken: too large next free num: %d " + "(in block %d, addr 0x%lx)\n", cur, prev, + (unsigned long)&UMM_NBLOCK(prev)); + ok = 0; + goto clean; + } + if (cur == 0) { + /* No more free blocks */ + break; + } + + /* Check if prev free block number matches */ + if (UMM_PFREE(cur) != prev) { + DBGLOG_FUNCTION("heap integrity broken: free links don't match: " + "%d -> %d, but %d -> %d\n", + prev, cur, cur, UMM_PFREE(cur)); + ok = 0; + goto clean; + } + + UMM_PBLOCK(cur) |= UMM_FREELIST_MASK; + + prev = cur; + } + + /* Iterate through all blocks */ + prev = 0; + while(1) { + cur = UMM_NBLOCK(prev) & UMM_BLOCKNO_MASK; + + /* Check that next block number is valid */ + if (cur >= UMM_NUMBLOCKS) { + DBGLOG_FUNCTION("heap integrity broken: too large next block num: %d " + "(in block %d, addr 0x%lx)\n", cur, prev, + (unsigned long)&UMM_NBLOCK(prev)); + ok = 0; + goto clean; + } + if (cur == 0) { + /* No more blocks */ + break; + } + + /* make sure the free mark is appropriate, and unmark it */ + if ((UMM_NBLOCK(cur) & UMM_FREELIST_MASK) + != (UMM_PBLOCK(cur) & UMM_FREELIST_MASK)) + { + DBGLOG_FUNCTION("heap integrity broken: mask wrong at addr 0x%lx: n=0x%x, p=0x%x\n", + (unsigned long)&UMM_NBLOCK(cur), + (UMM_NBLOCK(cur) & UMM_FREELIST_MASK), + (UMM_PBLOCK(cur) & UMM_FREELIST_MASK) + ); + ok = 0; + goto clean; + } + + /* make sure the block list is sequential */ + if (cur <= prev ) { + DBGLOG_FUNCTION("heap integrity broken: next block %d is before prev this one " + "(in block %d, addr 0x%lx)\n", cur, prev, + (unsigned long)&UMM_NBLOCK(prev)); + ok = 0; + goto clean; + } + +/* unmark */ + UMM_PBLOCK(cur) &= UMM_BLOCKNO_MASK; + + /* Check if prev block number matches */ + if (UMM_PBLOCK(cur) != prev) { + DBGLOG_FUNCTION("heap integrity broken: block links don't match: " + "%d -> %d, but %d -> %d\n", + prev, cur, cur, UMM_PBLOCK(cur)); + ok = 0; + goto clean; + } + + prev = cur; + } + +clean: + UMM_CRITICAL_EXIT(id_integrity); + if (!ok){ + UMM_HEAP_CORRUPTION_CB(); + } + return ok; +} + +#endif +/* }}} */ +#endif // defined(BUILD_UMM_MALLOC_C) diff --git a/cores/esp8266/umm_malloc/umm_local.c b/cores/esp8266/umm_malloc/umm_local.c new file mode 100644 index 000000000..b984e86b8 --- /dev/null +++ b/cores/esp8266/umm_malloc/umm_local.c @@ -0,0 +1,215 @@ +/* + * Local Additions/Enhancements + * + */ +#if defined(BUILD_UMM_MALLOC_C) + +#if defined(UMM_CRITICAL_METRICS) +/* + * umm_malloc performance measurments for critical sections + */ +UMM_TIME_STATS time_stats = { + {0xFFFFFFFF, 0U, 0U, 0U}, + {0xFFFFFFFF, 0U, 0U, 0U}, + {0xFFFFFFFF, 0U, 0U, 0U}, +#ifdef UMM_INFO + {0xFFFFFFFF, 0U, 0U, 0U}, +#endif +#ifdef UMM_POISON_CHECK + {0xFFFFFFFF, 0U, 0U, 0U}, +#endif +#ifdef UMM_INTEGRITY_CHECK + {0xFFFFFFFF, 0U, 0U, 0U}, +#endif + {0xFFFFFFFF, 0U, 0U, 0U} }; + +bool ICACHE_FLASH_ATTR get_umm_get_perf_data(UMM_TIME_STATS *p, size_t size) +{ + UMM_CRITICAL_DECL(id_no_tag); + if (p && sizeof(time_stats) == size) + { + UMM_CRITICAL_ENTRY(id_no_tag); + memcpy(p, &time_stats, size); + UMM_CRITICAL_EXIT(id_no_tag); + return true; + } + return false; +} +#endif + +// Alternate Poison functions + +#if defined(UMM_POISON_CHECK_LITE) +// We skip this when doing the full poison check. + +static int check_poison_neighbors( unsigned short cur ) { + unsigned short int c; + + if ( 0 == cur ) + return 1; + + c = UMM_PBLOCK(cur) & UMM_BLOCKNO_MASK; + while( c && (UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) ) { + /* + There can be up to 1 free block neighbor in either direction. + This loop should self limit to 2 passes, due to heap design. + i.e. Adjacent free space is always consolidated. + */ + if ( !(UMM_NBLOCK(c) & UMM_FREELIST_MASK) ) { + if ( !check_poison_block(&UMM_BLOCK(c)) ) + return 0; + + break; + } + + c = UMM_PBLOCK(c) & UMM_BLOCKNO_MASK; + } + + c = UMM_NBLOCK(cur) & UMM_BLOCKNO_MASK; + while( (UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) ) { + if ( !(UMM_NBLOCK(c) & UMM_FREELIST_MASK) ) { + if ( !check_poison_block(&UMM_BLOCK(c)) ) + return 0; + + break; + } + + c = UMM_NBLOCK(c) & UMM_BLOCKNO_MASK; + } + + return 1; +} +#endif + +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) + +/* ------------------------------------------------------------------------ */ + +static void *get_unpoisoned_check_neighbors( void *v_ptr, const char* file, int line ) { + unsigned char *ptr = (unsigned char *)v_ptr; + + if (ptr != NULL) { + + ptr -= (sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE); + +#if defined(UMM_POISON_CHECK_LITE) + UMM_CRITICAL_DECL(id_poison); + unsigned short int c; + bool poison = false; + + /* Figure out which block we're in. Note the use of truncated division... */ + c = (((char *)ptr)-(char *)(&(umm_heap[0])))/sizeof(umm_block); + + UMM_CRITICAL_ENTRY(id_poison); + poison = check_poison_block(&UMM_BLOCK(c)) && check_poison_neighbors(c); + UMM_CRITICAL_EXIT(id_poison); + + if (!poison) { + if (file) { + __panic_func(file, line, ""); + } else { + abort(); + } + } +#else + /* + * No need to check poison here. POISON_CHECK() has already done a + * full heap check. + */ + (void)file; + (void)line; +#endif + } + + return (void *)ptr; +} + +/* ------------------------------------------------------------------------ */ + +void *umm_poison_realloc_fl(void *ptr, size_t size, const char* file, int line) { + void *ret; + + ptr = get_unpoisoned_check_neighbors(ptr, file, line); + + size += poison_size(size); + ret = umm_realloc(ptr, size); + + ret = get_poisoned(ret, size); + + return ret; +} + +/* ------------------------------------------------------------------------ */ + +void umm_poison_free_fl(void *ptr, const char* file, int line) { + + ptr = get_unpoisoned_check_neighbors(ptr, file, line); + + umm_free(ptr); +} +#endif + +/* ------------------------------------------------------------------------ */ + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) || defined(UMM_INFO) +size_t umm_block_size( void ) { + return sizeof(umm_block); +} +#endif + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) +UMM_STATISTICS ummStats; + +// Keep complete call path in IRAM +size_t umm_free_heap_size_lw( void ) { + return (size_t)ummStats.free_blocks * sizeof(umm_block); +} +#endif + +/* + I assume xPortGetFreeHeapSize needs to be in IRAM. Since + system_get_free_heap_size is in IRAM. Which would mean, umm_free_heap_size() + in flash, was not a safe alternative for returning the same information. +*/ +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) +size_t xPortGetFreeHeapSize(void) __attribute__ ((alias("umm_free_heap_size_lw"))); +#elif defined(UMM_INFO) +#warning "No ISR safe function available to implement xPortGetFreeHeapSize()" +size_t xPortGetFreeHeapSize(void) __attribute__ ((alias("umm_free_heap_size"))); +#endif + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) +void print_stats(int force) { + DBGLOG_FORCE( force, "umm heap statistics:\n"); + DBGLOG_FORCE( force, " Free Space %5u\n", ummStats.free_blocks * sizeof(umm_block)); + DBGLOG_FORCE( force, " OOM Count %5u\n", ummStats.oom_count); +#if defined(UMM_STATS_FULL) + DBGLOG_FORCE( force, " Low Watermark %5u\n", ummStats.free_blocks_min * sizeof(umm_block)); + DBGLOG_FORCE( force, " Low Watermark ISR %5u\n", ummStats.free_blocks_isr_min * sizeof(umm_block)); + DBGLOG_FORCE( force, " MAX Alloc Request %5u\n", ummStats.alloc_max_size); +#endif + DBGLOG_FORCE( force, " Size of umm_block %5u\n", sizeof(umm_block)); + DBGLOG_FORCE( force, "+--------------------------------------------------------------+\n" ); +} +#endif + + + +int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) { + /* + To use ets_strlen() and ets_strcpy() safely with PROGMEM, flash storage, + the PROGMEM address must be word (4 bytes) aligned. The destination + address for ets_memcpy must also be word-aligned. + */ + char ram_buf[ets_strlen(fmt)] __attribute__ ((aligned(4))); + ets_strcpy(ram_buf, fmt); + va_list argPtr; + va_start(argPtr, fmt); + int result = ets_vprintf(ets_uart_putc1, ram_buf, argPtr); + va_end(argPtr); + return result; +} + +#endif // BUILD_UMM_MALLOC_C + + diff --git a/cores/esp8266/umm_malloc/umm_local.h b/cores/esp8266/umm_malloc/umm_local.h new file mode 100644 index 000000000..c2f05a57d --- /dev/null +++ b/cores/esp8266/umm_malloc/umm_local.h @@ -0,0 +1,54 @@ +#ifndef _UMM_LOCAL_H +#define _UMM_LOCAL_H +/* + * A home for local items exclusive to umm_malloc.c and not to be shared in + * umm_malloc_cfg.h. And, not for upstream version. + * Also used to redefine defines made in upstream files we donet want to edit. + * + */ + +#undef memcpy +#undef memmove +#undef memset +#define memcpy ets_memcpy +#define memmove ets_memmove +#define memset ets_memset + + +/* + * This redefines DBGLOG_FORCE defined in dbglog/dbglog.h + * Just for printing from umm_info() which is assumed to always be called from + * non-ISR. Thus SPI bus is available to handle cache-miss and reading a flash + * string while INTLEVEL is non-zero. + */ +#undef DBGLOG_FORCE +#define DBGLOG_FORCE(force, format, ...) {if(force) {UMM_INFO_PRINTF(format, ## __VA_ARGS__);}} +// #define DBGLOG_FORCE(force, format, ...) {if(force) {::printf(PSTR(format), ## __VA_ARGS__);}} + + +#if defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK) +#else + +#define umm_malloc(s) malloc(s) +#define umm_calloc(n,s) calloc(n,s) +#define umm_realloc(p,s) realloc(p,s) +#define umm_free(p) free(p) +#endif + + +#if defined(UMM_POISON_CHECK_LITE) +static int check_poison_neighbors( unsigned short cur ); +#endif + + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) +void ICACHE_FLASH_ATTR print_stats(int force); +#endif + + + +int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2))); +#define UMM_INFO_PRINTF(fmt, ...) umm_info_safe_printf_P(PSTR(fmt), ##__VA_ARGS__) + + +#endif diff --git a/cores/esp8266/umm_malloc/umm_malloc.cpp b/cores/esp8266/umm_malloc/umm_malloc.cpp index 13930a828..5feaa2ede 100644 --- a/cores/esp8266/umm_malloc/umm_malloc.cpp +++ b/cores/esp8266/umm_malloc/umm_malloc.cpp @@ -1,7 +1,8 @@ /* ---------------------------------------------------------------------------- * umm_malloc.c - a memory allocator for embedded systems (microcontrollers) * - * See copyright notice in LICENSE.TXT + * See LICENSE for copyright notice + * See README.md for acknowledgements and description of internals * ---------------------------------------------------------------------------- * * R.Hempel 2007-09-22 - Original @@ -18,637 +19,49 @@ * not worth the grief. * D.Frank 2014-04-02 - Fixed heap configuration when UMM_TEST_MAIN is NOT set, * added user-dependent configuration file umm_malloc_cfg.h - * ---------------------------------------------------------------------------- - * - * Note: when upgrading this file with upstream code, replace all %i with %d in - * printf format strings. ets_printf doesn't handle %i. - * - * ---------------------------------------------------------------------------- - * - * This is a memory management library specifically designed to work with the - * ARM7 embedded processor, but it should work on many other 32 bit processors, - * as well as 16 and 8 bit devices. - * - * ACKNOWLEDGEMENTS - * - * Joerg Wunsch and the avr-libc provided the first malloc() implementation - * that I examined in detail. - * - * http: *www.nongnu.org/avr-libc - * - * Doug Lea's paper on malloc() was another excellent reference and provides - * a lot of detail on advanced memory management techniques such as binning. - * - * http: *g.oswego.edu/dl/html/malloc.html - * - * Bill Dittman provided excellent suggestions, including macros to support - * using these functions in critical sections, and for optimizing realloc() - * further by checking to see if the previous block was free and could be - * used for the new block size. This can help to reduce heap fragmentation - * significantly. - * - * Yaniv Ankin suggested that a way to dump the current heap condition - * might be useful. I combined this with an idea from plarroy to also - * allow checking a free pointer to make sure it's valid. - * - * ---------------------------------------------------------------------------- - * - * The memory manager assumes the following things: - * - * 1. The standard POSIX compliant malloc/realloc/free semantics are used - * 2. All memory used by the manager is allocated at link time, it is aligned - * on a 32 bit boundary, it is contiguous, and its extent (start and end - * address) is filled in by the linker. - * 3. All memory used by the manager is initialized to 0 as part of the - * runtime startup routine. No other initialization is required. - * - * The fastest linked list implementations use doubly linked lists so that - * its possible to insert and delete blocks in constant time. This memory - * manager keeps track of both free and used blocks in a doubly linked list. - * - * Most memory managers use some kind of list structure made up of pointers - * to keep track of used - and sometimes free - blocks of memory. In an - * embedded system, this can get pretty expensive as each pointer can use - * up to 32 bits. - * - * In most embedded systems there is no need for managing large blocks - * of memory dynamically, so a full 32 bit pointer based data structure - * for the free and used block lists is wasteful. A block of memory on - * the free list would use 16 bytes just for the pointers! - * - * This memory management library sees the malloc heap as an array of blocks, - * and uses block numbers to keep track of locations. The block numbers are - * 15 bits - which allows for up to 32767 blocks of memory. The high order - * bit marks a block as being either free or in use, which will be explained - * later. - * - * The result is that a block of memory on the free list uses just 8 bytes - * instead of 16. - * - * In fact, we go even one step futher when we realize that the free block - * index values are available to store data when the block is allocated. - * - * The overhead of an allocated block is therefore just 4 bytes. - * - * Each memory block holds 8 bytes, and there are up to 32767 blocks - * available, for about 256K of heap space. If that's not enough, you - * can always add more data bytes to the body of the memory block - * at the expense of free block size overhead. - * - * There are a lot of little features and optimizations in this memory - * management system that makes it especially suited to small embedded, but - * the best way to appreciate them is to review the data structures and - * algorithms used, so let's get started. - * - * ---------------------------------------------------------------------------- - * - * We have a general notation for a block that we'll use to describe the - * different scenarios that our memory allocation algorithm must deal with: - * - * +----+----+----+----+ - * c |* n | p | nf | pf | - * +----+----+----+----+ - * - * Where - c is the index of this block - * * is the indicator for a free block - * n is the index of the next block in the heap - * p is the index of the previous block in the heap - * nf is the index of the next block in the free list - * pf is the index of the previous block in the free list - * - * The fact that we have forward and backward links in the block descriptors - * means that malloc() and free() operations can be very fast. It's easy - * to either allocate the whole free item to a new block or to allocate part - * of the free item and leave the rest on the free list without traversing - * the list from front to back first. - * - * The entire block of memory used by the heap is assumed to be initialized - * to 0. The very first block in the heap is special - it't the head of the - * free block list. It is never assimilated with a free block (more on this - * later). - * - * Once a block has been allocated to the application, it looks like this: - * - * +----+----+----+----+ - * c | n | p | ... | - * +----+----+----+----+ - * - * Where - c is the index of this block - * n is the index of the next block in the heap - * p is the index of the previous block in the heap - * - * Note that the free list information is gone, because it's now being used to - * store actual data for the application. It would have been nice to store - * the next and previous free list indexes as well, but that would be a waste - * of space. If we had even 500 items in use, that would be 2,000 bytes for - * free list information. We simply can't afford to waste that much. - * - * The address of the ... area is what is returned to the application - * for data storage. - * - * The following sections describe the scenarios encountered during the - * operation of the library. There are two additional notation conventions: - * - * ?? inside a pointer block means that the data is irrelevant. We don't care - * about it because we don't read or modify it in the scenario being - * described. - * - * ... between memory blocks indicates zero or more additional blocks are - * allocated for use by the upper block. - * - * And while we're talking about "upper" and "lower" blocks, we should make - * a comment about adresses. In the diagrams, a block higher up in the - * picture is at a lower address. And the blocks grow downwards their - * block index increases as does their physical address. - * - * Finally, there's one very important characteristic of the individual - * blocks that make up the heap - there can never be two consecutive free - * memory blocks, but there can be consecutive used memory blocks. - * - * The reason is that we always want to have a short free list of the - * largest possible block sizes. By always assimilating a newly freed block - * with adjacent free blocks, we maximize the size of each free memory area. - * - *--------------------------------------------------------------------------- - * - * Operation of malloc right after system startup - * - * As part of the system startup code, all of the heap has been cleared. - * - * During the very first malloc operation, we start traversing the free list - * starting at index 0. The index of the next free block is 0, which means - * we're at the end of the list! - * - * At this point, the malloc has a special test that checks if the current - * block index is 0, which it is. This special case initializes the free - * list to point at block index 1. - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ - * 1 | 0 | 0 | 0 | 0 | - * +----+----+----+----+ - * - * The heap is now ready to complete the first malloc operation. - * - * ---------------------------------------------------------------------------- - * - * Operation of malloc when we have reached the end of the free list and - * there is no block large enough to accommodate the request. - * - * This happens at the very first malloc operation, or any time the free - * list is traversed and no free block large enough for the request is - * found. - * - * The current block pointer will be at the end of the free list, and we - * know we're at the end of the list because the nf index is 0, like this: - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * pf |*?? | ?? | cf | ?? | pf |*?? | ?? | lf | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * p | cf | ?? | ... | p | cf | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * cf | 0 | p | 0 | pf | c | lf | p | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ - * lf | 0 | cf | 0 | pf | - * +----+----+----+----+ - * - * As we walk the free list looking for a block of size b or larger, we get - * to cf, which is the last item in the free list. We know this because the - * next index is 0. - * - * So we're going to turn cf into the new block of memory, and then create - * a new block that represents the last free entry (lf) and adjust the prev - * index of lf to point at the block we just created. We also need to adjust - * the next index of the new block (c) to point to the last free block. - * - * Note that the next free index of the pf block must point to the new lf - * because cf is no longer a free block! - * - * ---------------------------------------------------------------------------- - * - * Operation of malloc when we have found a block (cf) that will fit the - * current request of b units exactly. - * - * This one is pretty easy, just clear the free list bit in the current - * block and unhook it from the free list. - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * pf |*?? | ?? | cf | ?? | pf |*?? | ?? | nf | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * p | cf | ?? | ... | p | cf | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ Clear the free - * cf |* n | p | nf | pf | cf | n | p | .. | list bit here - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | cf | ... | n | ?? | cf | ... | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | cf | nf | ?? | ?? | ?? | pf | - * +----+----+----+----+ +----+----+----+----+ - * - * Unhooking from the free list is accomplished by adjusting the next and - * prev free list index values in the pf and nf blocks. - * - * ---------------------------------------------------------------------------- - * - * Operation of malloc when we have found a block that will fit the current - * request of b units with some left over. - * - * We'll allocate the new block at the END of the current free block so we - * don't have to change ANY free list pointers. - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * pf |*?? | ?? | cf | ?? | pf |*?? | ?? | cf | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * p | cf | ?? | ... | p | cf | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * cf |* n | p | nf | pf | cf |* c | p | nf | pf | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ This is the new - * c | n | cf | .. | block at cf+b - * +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | cf | ... | n | ?? | c | ... | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | cf | nf | ?? | ?? | ?? | pf | - * +----+----+----+----+ +----+----+----+----+ - * - * This one is prety easy too, except we don't need to mess with the - * free list indexes at all becasue we'll allocate the new block at the - * end of the current free block. We do, however have to adjust the - * indexes in cf, c, and n. - * - * ---------------------------------------------------------------------------- - * - * That covers the initialization and all possible malloc scenarios, so now - * we need to cover the free operation possibilities... - * - * The operation of free depends on the position of the current block being - * freed relative to free list items immediately above or below it. The code - * works like this: - * - * if next block is free - * assimilate with next block already on free list - * if prev block is free - * assimilate with prev block already on free list - * else - * put current block at head of free list - * - * ---------------------------------------------------------------------------- - * - * Step 1 of the free operation checks if the next block is free, and if it - * is then insert this block into the free list and assimilate the next block - * with this one. - * - * Note that c is the block we are freeing up, cf is the free block that - * follows it. - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * pf |*?? | ?? | cf | ?? | pf |*?? | ?? | nf | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * p | c | ?? | ... | p | c | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ This block is - * c | cf | p | ... | c | nn | p | ... | disconnected - * +----+----+----+----+ +----+----+----+----+ from free list, - * +----+----+----+----+ assimilated with - * cf |*nn | c | nf | pf | the next, and - * +----+----+----+----+ ready for step 2 - * +----+----+----+----+ +----+----+----+----+ - * nn | ?? | cf | ?? | ?? | nn | ?? | c | ... | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | cf | nf |*?? | ?? | ?? | pf | - * +----+----+----+----+ +----+----+----+----+ - * - * Take special note that the newly assimilated block (c) is completely - * disconnected from the free list, and it does not have its free list - * bit set. This is important as we move on to step 2 of the procedure... - * - * ---------------------------------------------------------------------------- - * - * Step 2 of the free operation checks if the prev block is free, and if it - * is then assimilate it with this block. - * - * Note that c is the block we are freeing up, pf is the free block that - * precedes it. - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ This block has - * pf |* c | ?? | nf | ?? | pf |* n | ?? | nf | ?? | assimilated the - * +----+----+----+----+ +----+----+----+----+ current block - * +----+----+----+----+ - * c | n | pf | ... | - * +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | c | ... | n | ?? | pf | ?? | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | pf | nf |*?? | ?? | ?? | pf | - * +----+----+----+----+ +----+----+----+----+ - * - * Nothing magic here, except that when we're done, the current block (c) - * is gone since it's been absorbed into the previous free block. Note that - * the previous step guarantees that the next block (n) is not free. - * - * ---------------------------------------------------------------------------- - * - * Step 3 of the free operation only runs if the previous block is not free. - * it just inserts the current block to the head of the free list. - * - * Remember, 0 is always the first block in the memory heap, and it's always - * head of the free list! - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * 0 | ?? | ?? | nf | 0 | 0 | ?? | ?? | c | 0 | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * p | c | ?? | ... | p | c | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * c | n | p | .. | c |* n | p | nf | 0 | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | c | ... | n | ?? | c | ... | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | 0 | nf |*?? | ?? | ?? | c | - * +----+----+----+----+ +----+----+----+----+ - * - * Again, nothing spectacular here, we're simply adjusting a few pointers - * to make the most recently freed block the first item in the free list. - * - * That's because finding the previous free block would mean a reverse - * traversal of blocks until we found a free one, and it's just easier to - * put it at the head of the list. No traversal is needed. - * - * ---------------------------------------------------------------------------- - * - * Finally, we can cover realloc, which has the following basic operation. - * - * The first thing we do is assimilate up with the next free block of - * memory if possible. This step might help if we're resizing to a bigger - * block of memory. It also helps if we're downsizing and creating a new - * free block with the leftover memory. - * - * First we check to see if the next block is free, and we assimilate it - * to this block if it is. If the previous block is also free, and if - * combining it with the current block would satisfy the request, then we - * assimilate with that block and move the current data down to the new - * location. - * - * Assimilating with the previous free block and moving the data works - * like this: - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * pf |*?? | ?? | cf | ?? | pf |*?? | ?? | nf | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * cf |* c | ?? | nf | pf | c | n | ?? | ... | The data gets - * +----+----+----+----+ +----+----+----+----+ moved from c to - * +----+----+----+----+ the new data area - * c | n | cf | ... | in cf, then c is - * +----+----+----+----+ adjusted to cf - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | c | ... | n | ?? | c | ?? | ?? | - * +----+----+----+----+ +----+----+----+----+ - * ... ... - * +----+----+----+----+ +----+----+----+----+ - * nf |*?? | ?? | ?? | cf | nf |*?? | ?? | ?? | pf | - * +----+----+----+----+ +----+----+----+----+ - * - * - * Once we're done that, there are three scenarios to consider: - * - * 1. The current block size is exactly the right size, so no more work is - * needed. - * - * 2. The current block is bigger than the new required size, so carve off - * the excess and add it to the free list. - * - * 3. The current block is still smaller than the required size, so malloc - * a new block of the correct size and copy the current data into the new - * block before freeing the current block. - * - * The only one of these scenarios that involves an operation that has not - * yet been described is the second one, and it's shown below: - * - * BEFORE AFTER - * - * +----+----+----+----+ +----+----+----+----+ - * p | c | ?? | ... | p | c | ?? | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ +----+----+----+----+ - * c | n | p | ... | c | s | p | ... | - * +----+----+----+----+ +----+----+----+----+ - * +----+----+----+----+ This is the - * s | n | c | .. | new block at - * +----+----+----+----+ c+blocks - * +----+----+----+----+ +----+----+----+----+ - * n | ?? | c | ... | n | ?? | s | ... | - * +----+----+----+----+ +----+----+----+----+ - * - * Then we call free() with the adress of the data portion of the new - * block (s) which adds it to the free list. - * + * R.Hempel 2016-12-04 - Add support for Unity test framework + * - Reorganize source files to avoid redundant content + * - Move integrity and poison checking to separate file + * R.Hempel 2017-12-29 - Fix bug in realloc when requesting a new block that + * results in OOM error - see Issue 11 + * R.Hempel 2019-09-07 - Separate the malloc() and free() functionality into + * wrappers that use critical section protection macros + * and static core functions that assume they are + * running in a protected con text. Thanks @devyte * ---------------------------------------------------------------------------- */ +/* + * This include is nothing but comments about thoughts and observations made + * while updating the Arduino ESP8266 Core, with the new upstream umm_malloc. + * It is added here as an include so that it does not get lost and to avoid + * cluttering up the code with a huge block comment. + */ + #include "Notes.h" +/* + * Added for using with Arduino ESP8266 and handling renameing to umm_malloc.cpp + */ + +#define BUILD_UMM_MALLOC_C + +extern "C" { + #include #include -#include #include "umm_malloc.h" #include "umm_malloc_cfg.h" /* user-dependent */ -extern "C" { +/* Use the default DBGLOG_LEVEL and DBGLOG_FUNCTION */ -#undef memcpy -#undef memmove -#undef memset -#define memcpy ets_memcpy -#define memmove ets_memmove -#define memset ets_memset - -// From UMM, the last caller of a malloc/realloc/calloc which failed: -extern void *umm_last_fail_alloc_addr; -extern int umm_last_fail_alloc_size; - -#ifndef UMM_FIRST_FIT -# ifndef UMM_BEST_FIT -# define UMM_BEST_FIT -# endif +#ifndef DBGLOG_LEVEL +#define DBGLOG_LEVEL 0 #endif -#ifndef DBG_LOG_LEVEL -# undef DBG_LOG_LEVEL -# define DBG_LOG_LEVEL 0 -#else -# undef DBG_LOG_LEVEL -# define DBG_LOG_LEVEL DBG_LOG_LEVEL -#endif +#include "dbglog/dbglog.h" -/* -Changes for July 2019: - - Correct critical section with interrupt level preserving and nest support - alternative. Replace ets_intr_lock()/ets_intr_unlock() with uint32_t - oldValue=xt_rsil(3)/xt_wrs(oldValue). Added UMM_CRITICAL_DECL macro to define - storage for current state. Expanded UMM_CRITICAL_... to use unique - identifiers. This helpt facilitate gather function specific timing - information. - - Replace printf with something that is ROM or IRAM based so that a printf - that occurs during an ISR malloc/new does not cause a crash. To avoid any - reentry issue it should also avoid doing malloc lib calls. - - Refactor realloc to avoid memcpy/memmove while in critical section. This is - only effective when realloc is called with interrupts enabled. The copy - process alone can take over 10us (when copying more than ~498 bytes with a - 80MHz CPU clock). It would be good practice for an ISR to avoid realloc. - Note, while doing this might initially sound scary, this appears to be very - stable. It ran on my troublesome sketch for over 3 weeks until I got back from - vacation and flashed an update. Troublesome sketch - runs ESPAsyncTCP, with - modified fauxmo emulation for 10 devices. It receives lost of Network traffic - related to uPnP scans, which includes lots of TCP connects disconnects RSTs - related to uPnP discovery. - - Locking is no longer nested in realloc, due to refactoring for reduced IRQ - off time. - - I have clocked umm_info critical lock time taking as much as 180us. A common - use for the umm_info call is to get the free heap result. It is common - to try and closely monitor free heap as a method to detect memory leaks. - This may result in frequent calls to umm_info. There has not been a clear - test case that shows an issue yet; however, I and others think they are or - have had crashes related to this. - - I have added code that updates a current free heap value from _umm_malloc, - _umm_realloc, and _umm_free. Removing the need to do a long interrupts - disabled calculation via umm_info. - - Build optional, min/max time measurements for locks held while in info, - malloc, realloc, and free. Also, maintains a count of how many times each is - called with INTLEVEL set. - - */ - -/* -- dbglog {{{ */ - -/* ---------------------------------------------------------------------------- - * A set of macros that cleans up code that needs to produce debug - * or log information. - * - * See copyright notice in LICENSE.TXT - * ---------------------------------------------------------------------------- - * - * There are macros to handle the following decreasing levels of detail: - * - * 6 = TRACE - * 5 = DEBUG - * 4 = CRITICAL - * 3 = ERROR - * 2 = WARNING - * 1 = INFO - * 0 = FORCE - The printf is always compiled in and is called only when - * the first parameter to the macro is non-0 - * - * ---------------------------------------------------------------------------- - * - * The following #define should be set up before this file is included so - * that we can be sure that the correct macros are defined. - * - * #define DBG_LOG_LEVEL x - * ---------------------------------------------------------------------------- - */ - -#undef DBG_LOG_TRACE -#undef DBG_LOG_DEBUG -#undef DBG_LOG_CRITICAL -#undef DBG_LOG_ERROR -#undef DBG_LOG_WARNING -#undef DBG_LOG_INFO -#undef DBG_LOG_FORCE - -/* ------------------------------------------------------------------------- */ - -#if DBG_LOG_LEVEL >= 6 -# define DBG_LOG_TRACE( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_TRACE( format, ... ) -#endif - -#if DBG_LOG_LEVEL >= 5 -# define DBG_LOG_DEBUG( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_DEBUG( format, ... ) -#endif - -#if DBG_LOG_LEVEL >= 4 -# define DBG_LOG_CRITICAL( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_CRITICAL( format, ... ) -#endif - -#if DBG_LOG_LEVEL >= 3 -# define DBG_LOG_ERROR( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_ERROR( format, ... ) -#endif - -#if DBG_LOG_LEVEL >= 2 -# define DBG_LOG_WARNING( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_WARNING( format, ... ) -#endif - -#if DBG_LOG_LEVEL >= 1 -# define DBG_LOG_INFO( format, ... ) DBGLOG_FUNCTION( format, ## __VA_ARGS__ ) -#else -# define DBG_LOG_INFO( format, ... ) -#endif - -#define DBG_LOG_FORCE( force, format, ... ) {if(force) {DBGLOG_FUNCTION( format, ## __VA_ARGS__ );}} - -/* }}} */ +#include "umm_local.h" // target-dependent supplemental /* ------------------------------------------------------------------------- */ @@ -673,13 +86,6 @@ UMM_H_ATTPACKPRE typedef struct umm_block_t { /* ------------------------------------------------------------------------- */ -#ifdef UMM_REDEFINE_MEM_FUNCTIONS -# define umm_free free -# define umm_malloc malloc -# define umm_calloc calloc -# define umm_realloc realloc -#endif - umm_block *umm_heap = NULL; unsigned short int umm_numblocks = 0; @@ -695,489 +101,18 @@ unsigned short int umm_numblocks = 0; #define UMM_PFREE(b) (UMM_BLOCK(b).body.free.prev) #define UMM_DATA(b) (UMM_BLOCK(b).body.data) -/* - * This does not look safe, no access locks. It currently is not being - * built, so not an immediate issue. - 06/10/19 - */ -/* integrity check (UMM_INTEGRITY_CHECK) {{{ */ -#if defined(UMM_INTEGRITY_CHECK) -/* - * Perform integrity check of the whole heap data. Returns 1 in case of - * success, 0 otherwise. - * - * First of all, iterate through all free blocks, and check that all backlinks - * match (i.e. if block X has next free block Y, then the block Y should have - * previous free block set to X). - * - * Additionally, we check that each free block is correctly marked with - * `UMM_FREELIST_MASK` on the `next` pointer: during iteration through free - * list, we mark each free block by the same flag `UMM_FREELIST_MASK`, but - * on `prev` pointer. We'll check and unmark it later. - * - * Then, we iterate through all blocks in the heap, and similarly check that - * all backlinks match (i.e. if block X has next block Y, then the block Y - * should have previous block set to X). - * - * But before checking each backlink, we check that the `next` and `prev` - * pointers are both marked with `UMM_FREELIST_MASK`, or both unmarked. - * This way, we ensure that the free flag is in sync with the free pointers - * chain. - */ -static int integrity_check(void) { - int ok = 1; - unsigned short int prev; - unsigned short int cur; - - if (umm_heap == NULL) { - umm_init(); - } - - /* Iterate through all free blocks */ - prev = 0; - while(1) { - cur = UMM_NFREE(prev); - - /* Check that next free block number is valid */ - if (cur >= UMM_NUMBLOCKS) { - DBGLOG_FUNCTION("heap integrity broken: too large next free num: %d " - "(in block %d, addr 0x%lx)\n", cur, prev, - (unsigned long)&UMM_NBLOCK(prev)); - ok = 0; - goto clean; - } - if (cur == 0) { - /* No more free blocks */ - break; - } - - /* Check if prev free block number matches */ - if (UMM_PFREE(cur) != prev) { - DBGLOG_FUNCTION("heap integrity broken: free links don't match: " - "%d -> %d, but %d -> %d\n", - prev, cur, cur, UMM_PFREE(cur)); - ok = 0; - goto clean; - } - - UMM_PBLOCK(cur) |= UMM_FREELIST_MASK; - - prev = cur; - } - - /* Iterate through all blocks */ - prev = 0; - while(1) { - cur = UMM_NBLOCK(prev) & UMM_BLOCKNO_MASK; - - /* Check that next block number is valid */ - if (cur >= UMM_NUMBLOCKS) { - DBGLOG_FUNCTION("heap integrity broken: too large next block num: %d " - "(in block %d, addr 0x%lx)\n", cur, prev, - (unsigned long)&UMM_NBLOCK(prev)); - ok = 0; - goto clean; - } - if (cur == 0) { - /* No more blocks */ - break; - } - - /* make sure the free mark is appropriate, and unmark it */ - if ((UMM_NBLOCK(cur) & UMM_FREELIST_MASK) - != (UMM_PBLOCK(cur) & UMM_FREELIST_MASK)) - { - DBGLOG_FUNCTION("heap integrity broken: mask wrong at addr 0x%lx: n=0x%x, p=0x%x\n", - (unsigned long)&UMM_NBLOCK(cur), - (UMM_NBLOCK(cur) & UMM_FREELIST_MASK), - (UMM_PBLOCK(cur) & UMM_FREELIST_MASK) - ); - ok = 0; - goto clean; - } - - /* unmark */ - UMM_PBLOCK(cur) &= UMM_BLOCKNO_MASK; - - /* Check if prev block number matches */ - if (UMM_PBLOCK(cur) != prev) { - DBGLOG_FUNCTION("heap integrity broken: block links don't match: " - "%d -> %d, but %d -> %d\n", - prev, cur, cur, UMM_PBLOCK(cur)); - ok = 0; - goto clean; - } - - prev = cur; - } - -clean: - if (!ok){ - UMM_HEAP_CORRUPTION_CB(); - } - return ok; -} - -#define INTEGRITY_CHECK() integrity_check() -#else -/* - * Integrity check is disabled, so just define stub macro - */ -#define INTEGRITY_CHECK() 1 -#endif -/* }}} */ - -/* poisoning (UMM_POISON) {{{ */ -#if defined(UMM_POISON) -#define POISON_BYTE (0xa5) - -/* - * Yields a size of the poison for the block of size `s`. - * If `s` is 0, returns 0. - */ -#define POISON_SIZE(s) ( \ - (s) ? \ - (UMM_POISON_SIZE_BEFORE + UMM_POISON_SIZE_AFTER + \ - sizeof(UMM_POISONED_BLOCK_LEN_TYPE) \ - ) : 0 \ - ) - -/* - * Print memory contents starting from given `ptr` - */ -static void dump_mem ( const unsigned char *ptr, size_t len ) { - while (len--) { - DBGLOG_FUNCTION(" 0x%.2x", (unsigned int)(*ptr++)); - } -} - -/* - * Put poison data at given `ptr` and `poison_size` - */ -static void put_poison( unsigned char *ptr, size_t poison_size ) { - memset(ptr, POISON_BYTE, poison_size); -} - -/* - * Check poison data at given `ptr` and `poison_size`. `where` is a pointer to - * a string, either "before" or "after", meaning, before or after the block. - * - * If poison is there, returns 1. - * Otherwise, prints the appropriate message, and returns 0. - */ -static int check_poison( const unsigned char *ptr, size_t poison_size, - const char *where) { - size_t i; - int ok = 1; - - for (i = 0; i < poison_size; i++) { - if (ptr[i] != POISON_BYTE) { - ok = 0; - break; - } - } - - if (!ok) { - DBGLOG_FUNCTION("there is no poison %s the block. " - "Expected poison address: 0x%lx, actual data:", - where, (unsigned long)ptr); - dump_mem(ptr, poison_size); - DBGLOG_FUNCTION("\n"); - } - - return ok; -} - -/* - * Check if a block is properly poisoned. Must be called only for non-free - * blocks. - */ -static int check_poison_block( umm_block *pblock ) { - int ok = 1; - - if (pblock->header.used.next & UMM_FREELIST_MASK) { - DBGLOG_FUNCTION("check_poison_block is called for free block 0x%lx\n", - (unsigned long)pblock); - } else { - /* the block is used; let's check poison */ - unsigned char *pc = (unsigned char *)pblock->body.data; - unsigned char *pc_cur; - - pc_cur = pc + sizeof(UMM_POISONED_BLOCK_LEN_TYPE); - if (!check_poison(pc_cur, UMM_POISON_SIZE_BEFORE, "before")) { - DBGLOG_FUNCTION("block start: %08x\n", pc + sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE); - UMM_HEAP_CORRUPTION_CB(); - ok = 0; - goto clean; - } - - pc_cur = pc + *((UMM_POISONED_BLOCK_LEN_TYPE *)pc) - UMM_POISON_SIZE_AFTER; - if (!check_poison(pc_cur, UMM_POISON_SIZE_AFTER, "after")) { - DBGLOG_FUNCTION("block start: %08x\n", pc + sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE); - UMM_HEAP_CORRUPTION_CB(); - ok = 0; - goto clean; - } - } - -clean: - return ok; -} - -/* - * Iterates through all blocks in the heap, and checks poison for all used - * blocks. - */ -static int check_poison_all_blocks(void) { - int ok = 1; - unsigned short int blockNo = 0; - - if (umm_heap == NULL) { - umm_init(); - } - - /* Now iterate through the blocks list */ - blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; - - while( UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK ) { - if ( !(UMM_NBLOCK(blockNo) & UMM_FREELIST_MASK) ) { - /* This is a used block (not free), so, check its poison */ - ok = check_poison_block(&UMM_BLOCK(blockNo)); - if (!ok){ - break; - } - } - - blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; - } - - return ok; -} - -/* - * Takes a pointer returned by actual allocator function (`_umm_malloc` or - * `_umm_realloc`), puts appropriate poison, and returns adjusted pointer that - * should be returned to the user. - * - * `size_w_poison` is a size of the whole block, including a poison. - */ -static void *get_poisoned( void *vptr, size_t size_w_poison ) { - unsigned char *ptr = (unsigned char *)vptr; - if (size_w_poison != 0 && ptr != NULL) { - - /* Put exact length of the user's chunk of memory */ - memcpy(ptr, &size_w_poison, sizeof(UMM_POISONED_BLOCK_LEN_TYPE)); - - /* Poison beginning and the end of the allocated chunk */ - put_poison(ptr + sizeof(UMM_POISONED_BLOCK_LEN_TYPE), - UMM_POISON_SIZE_BEFORE); - put_poison(ptr + size_w_poison - UMM_POISON_SIZE_AFTER, - UMM_POISON_SIZE_AFTER); - - /* Return pointer at the first non-poisoned byte */ - return ptr + sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE; - } else { - return ptr; - } -} - -/* - * Takes "poisoned" pointer (i.e. pointer returned from `get_poisoned()`), - * and checks that the poison of this particular block is still there. - * - * Returns unpoisoned pointer, i.e. actual pointer to the allocated memory. - */ -static void *get_unpoisoned( void *vptr ) { - unsigned char *ptr = (unsigned char *)vptr; - if (ptr != NULL) { - unsigned short int c; - - ptr -= (sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE); - - /* Figure out which block we're in. Note the use of truncated division... */ - c = (((char *)ptr)-(char *)(&(umm_heap[0])))/sizeof(umm_block); - - check_poison_block(&UMM_BLOCK(c)); - } - - return ptr; -} - -#define CHECK_POISON_ALL_BLOCKS() check_poison_all_blocks() -#define GET_POISONED(ptr, size) get_poisoned(ptr, size) -#define GET_UNPOISONED(ptr) get_unpoisoned(ptr) - -#else -/* - * Integrity check is disabled, so just define stub macros - */ -#define POISON_SIZE(s) 0 -#define CHECK_POISON_ALL_BLOCKS() 1 -#define GET_POISONED(ptr, size) (ptr) -#define GET_UNPOISONED(ptr) (ptr) -#endif -/* }}} */ - -/* ---------------------------------------------------------------------------- - * One of the coolest things about this little library is that it's VERY - * easy to get debug information about the memory heap by simply iterating - * through all of the memory blocks. - * - * As you go through all the blocks, you can check to see if it's a free - * block by looking at the high order bit of the next block index. You can - * also see how big the block is by subtracting the next block index from - * the current block number. - * - * The umm_info function does all of that and makes the results available - * in the ummHeapInfo structure. - * ---------------------------------------------------------------------------- +/* ------------------------------------------------------------------------- + * There are additional files that may be included here - normally it's + * not a good idea to include .c files but in this case it keeps the + * main umm_malloc file clear and prevents issues with exposing internal + * data structures to other programs. + * ------------------------------------------------------------------------- */ -UMM_HEAP_INFO ummHeapInfo; - -void ICACHE_FLASH_ATTR *umm_info( void *ptr, int force ) { - UMM_CRITICAL_DECL(id_info); - - unsigned short int blockNo = 0; - - if (umm_heap == NULL) { - umm_init(); - } - - /* Protect the critical section... */ - UMM_CRITICAL_ENTRY(id_info); - - /* - * Clear out all of the entries in the ummHeapInfo structure before doing - * any calculations.. - */ - memset( &ummHeapInfo, 0, sizeof( ummHeapInfo ) ); - - DBG_LOG_FORCE( force, "\n\nDumping the umm_heap...\n" ); - - DBG_LOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5d|NF %5d|PF %5d|\n", - (unsigned long)(&UMM_BLOCK(blockNo)), - blockNo, - UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, - UMM_PBLOCK(blockNo), - (UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK )-blockNo, - UMM_NFREE(blockNo), - UMM_PFREE(blockNo) ); - - /* - * Now loop through the block lists, and keep track of the number and size - * of used and free blocks. The terminating condition is an nb pointer with - * a value of zero... - */ - - blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; - - while( UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK ) { - size_t curBlocks = (UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK )-blockNo; - - ++ummHeapInfo.totalEntries; - ummHeapInfo.totalBlocks += curBlocks; - - /* Is this a free block? */ - - if( UMM_NBLOCK(blockNo) & UMM_FREELIST_MASK ) { - ++ummHeapInfo.freeEntries; - ummHeapInfo.freeBlocks += curBlocks; - ummHeapInfo.freeSize2 += (unsigned int)curBlocks - * (unsigned int)sizeof(umm_block) - * (unsigned int)curBlocks - * (unsigned int)sizeof(umm_block); - - if (ummHeapInfo.maxFreeContiguousBlocks < curBlocks) { - ummHeapInfo.maxFreeContiguousBlocks = curBlocks; - } - - DBG_LOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5u|NF %5d|PF %5d|\n", - (unsigned long)(&UMM_BLOCK(blockNo)), - blockNo, - UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, - UMM_PBLOCK(blockNo), - (unsigned int)curBlocks, - UMM_NFREE(blockNo), - UMM_PFREE(blockNo) ); - - /* Does this block address match the ptr we may be trying to free? */ - - if( ptr == &UMM_BLOCK(blockNo) ) { - - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_info); - - return( ptr ); - } - } else { - ++ummHeapInfo.usedEntries; - ummHeapInfo.usedBlocks += curBlocks; - - DBG_LOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5u|\n", - (unsigned long)(&UMM_BLOCK(blockNo)), - blockNo, - UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, - UMM_PBLOCK(blockNo), - (unsigned int)curBlocks ); - } - - blockNo = UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK; - } - - /* - * Update the accounting totals with information from the last block, the - * rest must be free! - */ - - { - size_t curBlocks = UMM_NUMBLOCKS-blockNo; - ummHeapInfo.freeBlocks += curBlocks; - ummHeapInfo.totalBlocks += curBlocks; - - if (ummHeapInfo.maxFreeContiguousBlocks < curBlocks) { - ummHeapInfo.maxFreeContiguousBlocks = curBlocks; - } - } - - DBG_LOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5d|NF %5d|PF %5d|\n", - (unsigned long)(&UMM_BLOCK(blockNo)), - blockNo, - UMM_NBLOCK(blockNo) & UMM_BLOCKNO_MASK, - UMM_PBLOCK(blockNo), - UMM_NUMBLOCKS-blockNo, - UMM_NFREE(blockNo), - UMM_PFREE(blockNo) ); - - DBG_LOG_FORCE( force, "Total Entries %5d Used Entries %5d Free Entries %5d\n", - ummHeapInfo.totalEntries, - ummHeapInfo.usedEntries, - ummHeapInfo.freeEntries ); - - DBG_LOG_FORCE( force, "Total Blocks %5d Used Blocks %5d Free Blocks %5d\n", - ummHeapInfo.totalBlocks, - ummHeapInfo.usedBlocks, - ummHeapInfo.freeBlocks ); - - if (ummHeapInfo.freeBlocks == ummStats.free_blocks) { - DBG_LOG_FORCE( force, "\nheap info Free blocks and heap statistics Free blocks match.\n"); - } else { - DBG_LOG_FORCE( force, "\nheap info Free blocks %5d != heap statistics Free Blocks %5d\n\n", - ummHeapInfo.freeBlocks, - ummStats.free_blocks ); - } - - DBG_LOG_FORCE( force, "\numm heap statistics:\n"); - DBG_LOG_FORCE( force, " Free Space %5u\n", ummStats.free_blocks * sizeof(umm_block)); - DBG_LOG_FORCE( force, " Low Watermark %5u\n", ummStats.free_blocks_min * sizeof(umm_block)); - DBG_LOG_FORCE( force, " MAX Alloc Request %5u\n", ummStats.alloc_max_size); - DBG_LOG_FORCE( force, " OOM Count %5u\n", ummStats.oom_count); - DBG_LOG_FORCE( force, " Size of umm_block %5u\n", sizeof(umm_block)); - - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_info); - - return( NULL ); -} +#include "umm_integrity.c" +#include "umm_poison.c" +#include "umm_info.c" +#include "umm_local.c" // target-dependent supplemental features /* ------------------------------------------------------------------------ */ @@ -1206,26 +141,23 @@ static unsigned short int umm_blocks( size_t size ) { } /* ------------------------------------------------------------------------ */ - /* * Split the block `c` into two blocks: `c` and `c + blocks`. * - * - `cur_freemask` should be `0` if `c` used, or `UMM_FREELIST_MASK` - * otherwise. * - `new_freemask` should be `0` if `c + blocks` used, or `UMM_FREELIST_MASK` * otherwise. * * Note that free pointers are NOT modified by this function. */ -static void umm_make_new_block( unsigned short int c, +static void umm_split_block( unsigned short int c, unsigned short int blocks, - unsigned short int cur_freemask, unsigned short int new_freemask ) { + unsigned short int new_freemask ) { UMM_NBLOCK(c+blocks) = (UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) | new_freemask; UMM_PBLOCK(c+blocks) = c; UMM_PBLOCK(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) = (c+blocks); - UMM_NBLOCK(c) = (c+blocks) | cur_freemask; + UMM_NBLOCK(c) = (c+blocks); } /* ------------------------------------------------------------------------ */ @@ -1241,7 +173,10 @@ static void umm_disconnect_from_free_list( unsigned short int c ) { UMM_NBLOCK(c) &= (~UMM_FREELIST_MASK); } -/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ + * The umm_assimilate_up() function assumes that UMM_NBLOCK(c) does NOT + * have the UMM_FREELIST_MASK bit set! + */ static void umm_assimilate_up( unsigned short int c ) { @@ -1251,7 +186,7 @@ static void umm_assimilate_up( unsigned short int c ) { * the free list */ - DBG_LOG_DEBUG( "Assimilate up to next block, which is FREE\n" ); + DBGLOG_DEBUG( "Assimilate up to next block, which is FREE\n" ); /* Disconnect the next block from the FREE list */ @@ -1264,7 +199,10 @@ static void umm_assimilate_up( unsigned short int c ) { } } -/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ + * The umm_assimilate_down() function assumes that UMM_NBLOCK(c) does NOT + * have the UMM_FREELIST_MASK bit set! + */ static unsigned short int umm_assimilate_down( unsigned short int c, unsigned short int freemask ) { @@ -1275,13 +213,12 @@ static unsigned short int umm_assimilate_down( unsigned short int c, unsigned sh } /* ------------------------------------------------------------------------- */ -/* This function called only one time during OS startup after flash is */ -/* enabled. No need to keep it in IRAM. */ -void ICACHE_FLASH_ATTR umm_init( void ) { + +void umm_init( void ) { /* init heap pointer and size, and memset it to 0 */ - umm_heap = (umm_block *)UMM_MALLOC_CFG__HEAP_ADDR; - umm_numblocks = (UMM_MALLOC_CFG__HEAP_SIZE / sizeof(umm_block)); - memset(umm_heap, 0x00, UMM_MALLOC_CFG__HEAP_SIZE); + umm_heap = (umm_block *)UMM_MALLOC_CFG_HEAP_ADDR; + umm_numblocks = (UMM_MALLOC_CFG_HEAP_SIZE / sizeof(umm_block)); + memset(umm_heap, 0x00, UMM_MALLOC_CFG_HEAP_SIZE); /* setup initial blank heap structure */ { @@ -1292,12 +229,19 @@ void ICACHE_FLASH_ATTR umm_init( void ) { /* index of the latest `umm_block` */ const unsigned short int block_last = UMM_NUMBLOCKS - 1; - /* init ummStats */ - ummStats.free_blocks = ummStats.free_blocks_min = block_last; + /* init ummStats.free_blocks */ +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) +#if defined(UMM_STATS_FULL) + ummStats.free_blocks_min = + ummStats.free_blocks_isr_min = +#endif + ummStats.free_blocks = block_last; +#endif /* setup the 0th `umm_block`, which just points to the 1st */ UMM_NBLOCK(block_0th) = block_1th; UMM_NFREE(block_0th) = block_1th; + UMM_PFREE(block_0th) = block_1th; /* * Now, we need to set the whole heap space as a huge free block. We should @@ -1335,21 +279,16 @@ void ICACHE_FLASH_ATTR umm_init( void ) { } } -/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ + * Must be called only from within critical sections guarded by + * UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT(). + */ -static void _umm_free( void *ptr ) { - UMM_CRITICAL_DECL(id_free); +static void umm_free_core( void *ptr ) { unsigned short int c; - /* If we're being asked to free a NULL pointer, well that's just silly! */ - - if( (void *)0 == ptr ) { - DBG_LOG_DEBUG( "free a null pointer -> do nothing\n" ); - - return; - } - + STATS__FREE_REQUEST(id_free); /* * FIXME: At some point it might be a good idea to add a check to make sure * that the pointer we're being asked to free up is actually within @@ -1363,13 +302,10 @@ static void _umm_free( void *ptr ) { c = (((char *)ptr)-(char *)(&(umm_heap[0])))/sizeof(umm_block); - DBG_LOG_DEBUG( "Freeing block %6d\n", c ); + DBGLOG_DEBUG( "Freeing block %6d\n", c ); - /* Protect the critical section... */ - UMM_CRITICAL_ENTRY(id_free); - - /* Update heap statistics */ - ummStats.free_blocks += (UMM_NBLOCK(c) - c); + /* Update stats Free Block count */ + STATS__FREE_BLOCKS_UPDATE(UMM_NBLOCK(c) - c); /* Now let's assimilate this block with the next one if possible. */ @@ -1379,7 +315,7 @@ static void _umm_free( void *ptr ) { if( UMM_NBLOCK(UMM_PBLOCK(c)) & UMM_FREELIST_MASK ) { - DBG_LOG_DEBUG( "Assimilate down to next block, which is FREE\n" ); + DBGLOG_DEBUG( "Assimilate down to next block, which is FREE\n" ); c = umm_assimilate_down(c, UMM_FREELIST_MASK); } else { @@ -1388,7 +324,7 @@ static void _umm_free( void *ptr ) { * of the free list */ - DBG_LOG_DEBUG( "Just add to head of free list\n" ); + DBGLOG_DEBUG( "Just add to head of free list\n" ); UMM_PFREE(UMM_NFREE(0)) = c; UMM_NFREE(c) = UMM_NFREE(0); @@ -1397,39 +333,41 @@ static void _umm_free( void *ptr ) { UMM_NBLOCK(c) |= UMM_FREELIST_MASK; } - -#if 0 - /* - * The following is experimental code that checks to see if the block we just - * freed can be assimilated with the very last block - it's pretty convoluted in - * terms of block index manipulation, and has absolutely no effect on heap - * fragmentation. I'm not sure that it's worth including but I've left it - * here for posterity. - */ - - if( 0 == UMM_NBLOCK(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK ) ) { - - if( UMM_PBLOCK(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) != UMM_PFREE(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK) ) { - UMM_NFREE(UMM_PFREE(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK)) = c; - UMM_NFREE(UMM_PFREE(c)) = UMM_NFREE(c); - UMM_PFREE(UMM_NFREE(c)) = UMM_PFREE(c); - UMM_PFREE(c) = UMM_PFREE(UMM_NBLOCK(c) & UMM_BLOCKNO_MASK); - } - - UMM_NFREE(c) = 0; - UMM_NBLOCK(c) = 0; - } -#endif - - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_free); } /* ------------------------------------------------------------------------ */ -static void *_umm_malloc( size_t size ) { - UMM_CRITICAL_DECL(id_malloc); +void umm_free( void *ptr ) { + UMM_CRITICAL_DECL(id_free); + if (umm_heap == NULL) { + umm_init(); + } + + /* If we're being asked to free a NULL pointer, well that's just silly! */ + + if( (void *)0 == ptr ) { + DBGLOG_DEBUG( "free a null pointer -> do nothing\n" ); + STATS__NULL_FREE_REQUEST(id_free); + + return; + } + + /* Free the memory withing a protected critical section */ + + UMM_CRITICAL_ENTRY(id_free); + + umm_free_core( ptr ); + + UMM_CRITICAL_EXIT(id_free); +} + +/* ------------------------------------------------------------------------ + * Must be called only from within critical sections guarded by + * UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT(). + */ + +static void *umm_malloc_core( size_t size ) { unsigned short int blocks; unsigned short int blockSize = 0; @@ -1438,25 +376,7 @@ static void *_umm_malloc( size_t size ) { unsigned short int cf; - if (umm_heap == NULL) { - umm_init(); - } - - /* - * the very first thing we do is figure out if we're being asked to allocate - * a size of 0 - and if we are we'll simply return a null pointer. if not - * then reduce the size by 1 byte so that the subsequent calculations on - * the number of blocks to allocate are easier... - */ - - if( 0 == size ) { - DBG_LOG_DEBUG( "malloc a block of 0 bytes -> do nothing\n" ); - - return( (void *)NULL ); - } - - if ( size > ummStats.alloc_max_size ) - ummStats.alloc_max_size = size; + STATS__ALLOC_REQUEST(id_malloc, size); blocks = umm_blocks( size ); @@ -1468,9 +388,6 @@ static void *_umm_malloc( size_t size ) { * algorithm */ - /* Protect the critical section... */ - UMM_CRITICAL_ENTRY(id_malloc); - cf = UMM_NFREE(0); bestBlock = UMM_NFREE(0); @@ -1479,17 +396,19 @@ static void *_umm_malloc( size_t size ) { while( cf ) { blockSize = (UMM_NBLOCK(cf) & UMM_BLOCKNO_MASK) - cf; - DBG_LOG_TRACE( "Looking at block %6d size %6d\n", cf, blockSize ); + DBGLOG_TRACE( "Looking at block %6d size %6d\n", cf, blockSize ); -#if defined UMM_FIRST_FIT - /* This is the first block that fits! */ - if( (blockSize >= blocks) ) - break; -#elif defined UMM_BEST_FIT +#if defined UMM_BEST_FIT if( (blockSize >= blocks) && (blockSize < bestSize) ) { bestBlock = cf; bestSize = blockSize; } +#elif defined UMM_FIRST_FIT + /* This is the first block that fits! */ + if( (blockSize >= blocks) ) + break; +#else +# error "No UMM_*_FIT is defined - check umm_malloc_cfg.h" #endif cf = UMM_NFREE(cf); @@ -1500,6 +419,8 @@ static void *_umm_malloc( size_t size ) { blockSize = bestSize; } + POISON_CHECK_NEIGHBORS(cf); + if( UMM_NBLOCK(cf) & UMM_BLOCKNO_MASK && blockSize >= blocks ) { /* * This is an existing block in the memory heap, we just need to split off @@ -1510,7 +431,7 @@ static void *_umm_malloc( size_t size ) { if( blockSize == blocks ) { /* It's an exact fit and we don't neet to split off a block. */ - DBG_LOG_DEBUG( "Allocating %6d blocks starting at %6d - exact\n", blocks, cf ); + DBGLOG_DEBUG( "Allocating %6d blocks starting at %6d - exact\n", blocks, cf ); /* Disconnect this block from the FREE list */ @@ -1518,18 +439,16 @@ static void *_umm_malloc( size_t size ) { } else { /* It's not an exact fit and we need to split off a block. */ - DBG_LOG_DEBUG( "Allocating %6d blocks starting at %6d - existing\n", blocks, cf ); + DBGLOG_DEBUG( "Allocating %6d blocks starting at %6d - existing\n", blocks, cf ); /* * split current free block `cf` into two blocks. The first one will be * returned to user, so it's not free, and the second one will be free. */ - umm_make_new_block( cf, blocks, - 0/*`cf` is not free*/, - UMM_FREELIST_MASK/*new block is free*/); + umm_split_block( cf, blocks, UMM_FREELIST_MASK /*new block is free*/ ); /* - * `umm_make_new_block()` does not update the free pointers (it affects + * `umm_split_block()` does not update the free pointers (it affects * only free flags), but effectively we've just moved beginning of the * free block from `cf` to `cf + blocks`. So we have to adjust pointers * to and from adjacent free blocks. @@ -1544,37 +463,66 @@ static void *_umm_malloc( size_t size ) { UMM_NFREE( cf + blocks ) = UMM_NFREE(cf); } - /* Update heap statistics */ - ummStats.free_blocks -= blocks; - if (ummStats.free_blocks < ummStats.free_blocks_min) - ummStats.free_blocks_min = ummStats.free_blocks; - + STATS__FREE_BLOCKS_UPDATE( -blocks ); + STATS__FREE_BLOCKS_MIN(); } else { - ummStats.oom_count += 1; - - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_malloc); - /* Out of memory */ + STATS__OOM_UPDATE(); - DBG_LOG_DEBUG( "Can't allocate %5d blocks\n", blocks ); + DBGLOG_DEBUG( "Can't allocate %5d blocks\n", blocks ); return( (void *)NULL ); } - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_malloc); - return( (void *)&UMM_DATA(cf) ); } /* ------------------------------------------------------------------------ */ -static void *_umm_realloc( void *ptr, size_t size ) { +void *umm_malloc( size_t size ) { + UMM_CRITICAL_DECL(id_malloc); + + void *ptr = NULL; + + if (umm_heap == NULL) { + umm_init(); + } + + /* + * the very first thing we do is figure out if we're being asked to allocate + * a size of 0 - and if we are we'll simply return a null pointer. if not + * then reduce the size by 1 byte so that the subsequent calculations on + * the number of blocks to allocate are easier... + */ + + if( 0 == size ) { + DBGLOG_DEBUG( "malloc a block of 0 bytes -> do nothing\n" ); + STATS__ZERO_ALLOC_REQUEST(id_malloc, size); + + return( ptr ); + } + + /* Allocate the memory withing a protected critical section */ + + UMM_CRITICAL_ENTRY(id_malloc); + + ptr = umm_malloc_core( size ); + + UMM_CRITICAL_EXIT(id_malloc); + + return( ptr ); +} + +/* ------------------------------------------------------------------------ */ + +void *umm_realloc( void *ptr, size_t size ) { UMM_CRITICAL_DECL(id_realloc); unsigned short int blocks; unsigned short int blockSize; + unsigned short int prevBlockSize = 0; + unsigned short int nextBlockSize = 0; + unsigned short int c; size_t curSize; @@ -1592,9 +540,9 @@ static void *_umm_realloc( void *ptr, size_t size ) { */ if( ((void *)NULL == ptr) ) { - DBG_LOG_DEBUG( "realloc the NULL pointer - call malloc()\n" ); + DBGLOG_DEBUG( "realloc the NULL pointer - call malloc()\n" ); - return( _umm_malloc(size) ); + return( umm_malloc(size) ); } /* @@ -1603,41 +551,17 @@ static void *_umm_realloc( void *ptr, size_t size ) { * we should operate the same as free. */ - if( 0 == size ) { - DBG_LOG_DEBUG( "realloc to 0 size, just free the block\n" ); - _umm_free( ptr ); + if( 0 == size ) { + DBGLOG_DEBUG( "realloc to 0 size, just free the block\n" ); + STATS__ZERO_ALLOC_REQUEST(id_realloc, size); + + umm_free( ptr ); return( (void *)NULL ); } - if ( size > ummStats.alloc_max_size ) - ummStats.alloc_max_size = size; - - /* - * Defer starting critical section. - * - * Initially we should be safe without a critical section as long as we are - * referencing values that are within our allocation as constants. - * And only reference values that will not change, while the redefintions of - * the allocations around us change. - * - * Example UMM_PBLOCK() could be change by a call to malloc from an ISR. - * On the other hand UMM_NBLOCK() is safe returns an address of the next - * block. The calculation is all based on information within our allocation - * that remains constant, until we change it. - * - * As long as we don't try to modify the next block or walk the chain of - * blocks we are okay. - * - * When called by an "interrupts enabled" type caller, it bears the - * responsibility to not call again, with the allocate we are currently - * working on. I think this is a normal expectation. I could be wrong. - * Such a situation would involve a function that is called from foreground - * and ISR context. Such code would already have to be re-entrant. This - * change may expand the corner cases for such a function. - * - */ + STATS__ALLOC_REQUEST(id_realloc, size); /* * Otherwise we need to actually do a reallocation. A naiive approach @@ -1654,7 +578,7 @@ static void *_umm_realloc( void *ptr, size_t size ) { c = (((char *)ptr)-(char *)(&(umm_heap[0])))/sizeof(umm_block); - /* Figure out how big this block is... */ + /* Figure out how big this block is ... the free bit is not set :-) */ blockSize = (UMM_NBLOCK(c) - c); @@ -1662,297 +586,245 @@ static void *_umm_realloc( void *ptr, size_t size ) { curSize = (blockSize*sizeof(umm_block))-(sizeof(((umm_block *)0)->header)); - /* - * Ok, now that we're here, we know the block number of the original chunk - * of memory, and we know how much new memory we want, and we know the original - * block size... - */ - - if( blockSize == blocks ) { - /* This space intentionally left blank - return the original pointer! */ - - DBG_LOG_DEBUG( "realloc the same size block - %d, do nothing\n", blocks ); - - return( ptr ); - } - - /* Now we need a critical section... */ + /* Protect the critical section... */ UMM_CRITICAL_ENTRY(id_realloc); - /* - * Now we have a block size that could be bigger or smaller. Either - * way, try to assimilate up to the next block before doing anything... + /* Now figure out if the previous and/or next blocks are free as well as + * their sizes - this will help us to minimize special code later when we + * decide if it's possible to use the adjacent blocks. * - * If it's still too small, we have to free it anyways and it will save the - * assimilation step later in free :-) + * We set prevBlockSize and nextBlockSize to non-zero values ONLY if they + * are free! */ - if( UMM_NBLOCK(UMM_NBLOCK(c)) & UMM_FREELIST_MASK ) { - // This will often be most of the free heap. The excess is - // restored when umm_free() is called before returning. - ummStats.free_blocks -= - (UMM_NBLOCK(UMM_NBLOCK(c)) & UMM_BLOCKNO_MASK) - UMM_NBLOCK(c); - umm_assimilate_up( c ); - } + if ((UMM_NBLOCK(UMM_NBLOCK(c)) & UMM_FREELIST_MASK)) { + nextBlockSize = (UMM_NBLOCK(UMM_NBLOCK(c)) & UMM_BLOCKNO_MASK) - UMM_NBLOCK(c); + } + if ((UMM_NBLOCK(UMM_PBLOCK(c)) & UMM_FREELIST_MASK)) { + prevBlockSize = (c - UMM_PBLOCK(c)); + } + + DBGLOG_DEBUG( "realloc blocks %d blockSize %d nextBlockSize %d prevBlockSize %d\n", blocks, blockSize, nextBlockSize, prevBlockSize ); + +#if defined(UMM_REALLOC_MINIMIZE_COPY) /* - * Now check if it might help to assimilate down, but don't actually - * do the downward assimilation unless the resulting block will hold the - * new request! If this block of code runs, then the new block will - * either fit the request exactly, or be larger than the request. + * Ok, now that we're here we know how many blocks we want and the current + * blockSize. The prevBlockSize and nextBlockSize are set and we can figure + * out the best strategy for the new allocation as follows: + * + * 1. If the new block is the same size or smaller than the current block do + * nothing. + * 2. If the next block is free and adding it to the current block gives us + * enough memory, assimilate the next block. + * 3. If the prev block is free and adding it to the current block gives us + * enough memory, remove the previous block from the free list, assimilate + * it, copy to the new block. + * 4. If the prev and next blocks are free and adding them to the current + * block gives us enough memory, assimilate the next block, remove the + * previous block from the free list, assimilate it, copy to the new block. + * 5. Otherwise try to allocate an entirely new block of memory. If the + * allocation works free the old block and return the new pointer. If + * the allocation fails, return NULL and leave the old block intact. + * + * All that's left to do is decide if the fit was exact or not. If the fit + * was not exact, then split the memory block so that we use only the requested + * number of blocks and add what's left to the free list. */ - - if( (UMM_NBLOCK(UMM_PBLOCK(c)) & UMM_FREELIST_MASK) && - (blocks <= (UMM_NBLOCK(c)-UMM_PBLOCK(c))) ) { - - /* Check if the resulting block would be big enough... */ - - DBG_LOG_DEBUG( "realloc() could assimilate down %d blocks - fits!\n\r", c-UMM_PBLOCK(c) ); - - /* - * Calculate the number of blocks to keep while the information is - * still available. - */ - - unsigned short int prevBlockSize = c - UMM_PBLOCK(c); - ummStats.free_blocks -= prevBlockSize; - unsigned short int prelimBlockSize = blockSize + prevBlockSize; - if(prelimBlockSize < blocks) - prelimBlockSize = blocks; - - /* Disconnect the previous block from the FREE list */ - - umm_disconnect_from_free_list( UMM_PBLOCK(c) ); - - /* - * Connect the previous block to the next block ... and then - * realign the current block pointer - */ - - c = umm_assimilate_down(c, 0); - - /* - * Currently all or most of the heap has been grabbed. Do an early split of - * allocation down to the amount needed to do a successful move operation. - * This will allow an alloc/new from a ISR to succeed while a memmove is - * running. - */ - if( (UMM_NBLOCK(c) - c) > prelimBlockSize ) { - umm_make_new_block( c, prelimBlockSize, 0, 0 ); - _umm_free( (void *)&UMM_DATA(c+prelimBlockSize) ); - } - - // This is the lowest low that may be seen by an ISR doing an alloc/new - if( ummStats.free_blocks < ummStats.free_blocks_min ) - ummStats.free_blocks_min = ummStats.free_blocks; - - /* - * For the ESP8266 interrupts should not be off for more than 10us. - * An unprotect/protect around memmove should be safe to do here. - * All variables used are on the stack. - */ - - UMM_CRITICAL_EXIT(id_realloc); - - /* - * Move the bytes down to the new block we just created, but be sure to move - * only the original bytes. - */ - - memmove( (void *)&UMM_DATA(c), ptr, curSize ); - - /* And don't forget to adjust the pointer to the new block location! */ - - ptr = (void *)&UMM_DATA(c); - - /* Now resume critical section... */ - UMM_CRITICAL_ENTRY(id_realloc); - } - - /* Now calculate the block size again...and we'll have three cases */ - - blockSize = (UMM_NBLOCK(c) - c); - - if( blockSize == blocks ) { - /* This space intentionally left blank - return the original pointer! */ - - DBG_LOG_DEBUG( "realloc the same size block - %d, do nothing\n", blocks ); - - } else if (blockSize > blocks ) { - /* - * New block is smaller than the old block, so just make a new block - * at the end of this one and put it up on the free list... - */ - - DBG_LOG_DEBUG( "realloc %d to a smaller block %d, shrink and free the leftover bits\n", blockSize, blocks ); - - umm_make_new_block( c, blocks, 0, 0 ); - _umm_free( (void *)&UMM_DATA(c+blocks) ); - } else { - /* New block is bigger than the old block... */ - - /* Finish up without critical section */ - UMM_CRITICAL_EXIT(id_realloc); - - void *oldptr = ptr; - - DBG_LOG_DEBUG( "realloc %d to a bigger block %d, make new, copy, and free the old\n", blockSize, blocks ); - - /* - * Now _umm_malloc() a new/ one, copy the old data to the new block, and - * free up the old block, but only if the malloc was sucessful! - */ - - if( (ptr = _umm_malloc( size )) ) { - memcpy( ptr, oldptr, curSize ); - _umm_free( oldptr ); + if (blockSize >= blocks) { + DBGLOG_DEBUG( "realloc the same or smaller size block - %i, do nothing\n", blocks ); + /* This space intentionally left blank */ + } else if ((blockSize + nextBlockSize) >= blocks) { + DBGLOG_DEBUG( "realloc using next block - %i\n", blocks ); + umm_assimilate_up( c ); + STATS__FREE_BLOCKS_UPDATE( - nextBlockSize ); + blockSize += nextBlockSize; + } else if ((prevBlockSize + blockSize) >= blocks) { + DBGLOG_DEBUG( "realloc using prev block - %i\n", blocks ); + umm_disconnect_from_free_list( UMM_PBLOCK(c) ); + c = umm_assimilate_down(c, 0); + STATS__FREE_BLOCKS_UPDATE( - prevBlockSize ); + STATS__FREE_BLOCKS_ISR_MIN(); + blockSize += prevBlockSize; + UMM_CRITICAL_SUSPEND(id_realloc); + memmove( (void *)&UMM_DATA(c), ptr, curSize ); + ptr = (void *)&UMM_DATA(c); + UMM_CRITICAL_RESUME(id_realloc); + } else if ((prevBlockSize + blockSize + nextBlockSize) >= blocks) { + DBGLOG_DEBUG( "realloc using prev and next block - %d\n", blocks ); + umm_assimilate_up( c ); + umm_disconnect_from_free_list( UMM_PBLOCK(c) ); + c = umm_assimilate_down(c, 0); + STATS__FREE_BLOCKS_UPDATE( - prevBlockSize - nextBlockSize ); +#ifdef UMM_LIGHTWEIGHT_CPU + if ((prevBlockSize + blockSize + nextBlockSize) > blocks) { + umm_split_block( c, blocks, 0 ); + umm_free_core( (void *)&UMM_DATA(c+blocks) ); + } + STATS__FREE_BLOCKS_ISR_MIN(); + blockSize = blocks; +#else + blockSize += (prevBlockSize + nextBlockSize); +#endif + UMM_CRITICAL_SUSPEND(id_realloc); + memmove( (void *)&UMM_DATA(c), ptr, curSize ); + ptr = (void *)&UMM_DATA(c); + UMM_CRITICAL_RESUME(id_realloc); } else { - ummStats.oom_count += 1; // Needs atomic + DBGLOG_DEBUG( "realloc a completely new block %i\n", blocks ); + void *oldptr = ptr; + if( (ptr = umm_malloc_core( size )) ) { + DBGLOG_DEBUG( "realloc %i to a bigger block %i, copy, and free the old\n", blockSize, blocks ); + UMM_CRITICAL_SUSPEND(id_realloc); + memcpy( ptr, oldptr, curSize ); + UMM_CRITICAL_RESUME(id_realloc); + umm_free_core( oldptr ); + } else { + DBGLOG_DEBUG( "realloc %i to a bigger block %i failed - return NULL and leave the old block!\n", blockSize, blocks ); + /* This space intentionally left blnk */ + STATS__OOM_UPDATE(); + } + /* This is not accurate for OOM case; however, it will work for + * stopping a call to free before return. + */ + blockSize = blocks; } +#elif defined(UMM_REALLOC_DEFRAG) + /* + * Ok, now that we're here we know how many blocks we want and the current + * blockSize. The prevBlockSize and nextBlockSize are set and we can figure + * out the best strategy for the new allocation. The following strategy is + * focused on defragging the heap: + * + * 1. If the prev is free and adding it to the current, or current and next + * block, gives us enough memory, proceed. Note, that next block may not + * be available. + * a. Remove the previous block from the free list, assimilate it. + * b. If this new block gives enough memory, copy to the new block. + * Note, this includes the case of same size or smaller block. + * c. Else assimilate the next block, copy to the new block. + * 2. If the new block is the same size or smaller than the current block do + * nothing. + * 3. If the next block is free and adding it to the current block gives us + * enough memory, assimilate the next block. + * 4. Otherwise try to allocate an entirely new block of memory. If the + * allocation works free the old block and return the new pointer. If + * the allocation fails, return NULL and leave the old block intact. + * + * All that's left to do is decide if the fit was exact or not. If the fit + * was not exact, then split the memory block so that we use only the + * requested number of blocks and add what's left to the free list. + */ + if (prevBlockSize && (prevBlockSize + blockSize + nextBlockSize) >= blocks) { // 1 + umm_disconnect_from_free_list( UMM_PBLOCK(c) ); + c = umm_assimilate_down(c, 0); + STATS__FREE_BLOCKS_UPDATE( - prevBlockSize ); + blockSize += prevBlockSize; + if (blockSize >= blocks) { + DBGLOG_DEBUG( "realloc using prev block - %d\n", blocks ); + STATS__FREE_BLOCKS_ISR_MIN(); + } else { + DBGLOG_DEBUG( "realloc using prev and next block - %d\n", blocks ); + umm_assimilate_up( c ); + STATS__FREE_BLOCKS_UPDATE( - nextBlockSize ); + blockSize += nextBlockSize; +#ifdef UMM_LIGHTWEIGHT_CPU + if (blockSize > blocks) { + umm_split_block( c, blocks, 0 ); + umm_free_core( (void *)&UMM_DATA(c+blocks) ); + } + STATS__FREE_BLOCKS_ISR_MIN(); + blockSize = blocks; +#endif + } + UMM_CRITICAL_SUSPEND(id_realloc); + memmove( (void *)&UMM_DATA(c), ptr, curSize ); + ptr = (void *)&UMM_DATA(c); + UMM_CRITICAL_RESUME(id_realloc); + } else if (blockSize >= blocks) { // 2 + DBGLOG_DEBUG( "realloc the same or smaller size block - %d, do nothing\n", blocks ); + /* This space intentionally left blank */ + } else if ((blockSize + nextBlockSize) >= blocks) { // 3 + DBGLOG_DEBUG( "realloc using next block - %d\n", blocks ); + umm_assimilate_up( c ); + STATS__FREE_BLOCKS_UPDATE( - nextBlockSize ); + blockSize += nextBlockSize; + } else { // 4 + DBGLOG_DEBUG( "realloc a completely new block %d\n", blocks ); + void *oldptr = ptr; + if( (ptr = umm_malloc_core( size )) ) { + DBGLOG_DEBUG( "realloc %d to a bigger block %d, copy, and free the old\n", blockSize, blocks ); + UMM_CRITICAL_SUSPEND(id_realloc); + memcpy( ptr, oldptr, curSize ); + UMM_CRITICAL_RESUME(id_realloc); + umm_free_core( oldptr); + } else { + DBGLOG_DEBUG( "realloc %d to a bigger block %d failed - return NULL and leave the old block!\n", blockSize, blocks ); + /* This space intentionally left blnk */ + STATS__OOM_UPDATE(); + } + /* This is not accurate for OOM case; however, it will work for + * stopping a call to free before return. + */ + blockSize = blocks; + } +#else +#warning "Neither UMM_REALLOC_DEFRAG nor UMM_REALLOC_MINIMIZE_COPY is defined - check umm_malloc_cfg.h" + /* An always copy option just for performance/fragmentation comparison */ + if (blockSize >= blocks) { + DBGLOG_DEBUG( "realloc the same or smaller size block - %d, do nothing\n", blocks ); + /* This space intentionally left blank */ + } else { + DBGLOG_DEBUG( "realloc a completely new block %d\n", blocks ); + void *oldptr = ptr; + if( (ptr = umm_malloc_core( size )) ) { + DBGLOG_DEBUG( "realloc %d to a bigger block %d, copy, and free the old\n", blockSize, blocks ); + UMM_CRITICAL_SUSPEND(id_realloc); + memcpy( ptr, oldptr, curSize ); + UMM_CRITICAL_RESUME(id_realloc); + umm_free_core( oldptr ); + } else { + DBGLOG_DEBUG( "realloc %d to a bigger block %d failed - return NULL and leave the old block!\n", blockSize, blocks ); + /* This space intentionally left blnk */ + STATS__OOM_UPDATE(); + } + /* This is not accurate for OOM case; however, it will work for + * stopping a call to free before return. + */ + blockSize = blocks; + } +#endif + /* Now all we need to do is figure out if the block fit exactly or if we + * need to split and free ... + */ + + if (blockSize > blocks ) { + DBGLOG_DEBUG( "split and free %d blocks from %d\n", blocks, blockSize ); + umm_split_block( c, blocks, 0 ); + umm_free_core( (void *)&UMM_DATA(c+blocks) ); + } + + STATS__FREE_BLOCKS_MIN(); + + /* Release the critical section... */ + UMM_CRITICAL_EXIT(id_realloc); + return( ptr ); - - } - - if (ummStats.free_blocks < ummStats.free_blocks_min) - ummStats.free_blocks_min = ummStats.free_blocks; - - /* Release the critical section... */ - UMM_CRITICAL_EXIT(id_realloc); - - return( ptr ); -} - -/* ------------------------------------------------------------------------ */ - -void *umm_malloc( size_t size ) { - void *ret; - - /* check poison of each blocks, if poisoning is enabled */ - if (!CHECK_POISON_ALL_BLOCKS()) { - return NULL; - } - - /* check full integrity of the heap, if this check is enabled */ - if (!INTEGRITY_CHECK()) { - return NULL; - } - - size += POISON_SIZE(size); - - ret = _umm_malloc( size ); - if (0 != size && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = size; - } - - ret = GET_POISONED(ret, size); - - return ret; } /* ------------------------------------------------------------------------ */ void *umm_calloc( size_t num, size_t item_size ) { void *ret; - size_t size = item_size * num; - /* check poison of each blocks, if poisoning is enabled */ - if (!CHECK_POISON_ALL_BLOCKS()) { - return NULL; - } + ret = umm_malloc((size_t)(item_size * num)); - /* check full integrity of the heap, if this check is enabled */ - if (!INTEGRITY_CHECK()) { - return NULL; - } - - size += POISON_SIZE(size); - ret = _umm_malloc(size); - if (ret) { - memset(ret, 0x00, size); - } - if (0 != size && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = size; - } - - ret = GET_POISONED(ret, size); + if (ret) + memset(ret, 0x00, (size_t)(item_size * num)); return ret; } /* ------------------------------------------------------------------------ */ -void *umm_realloc( void *ptr, size_t size ) { - void *ret; - - ptr = GET_UNPOISONED(ptr); - - /* check poison of each blocks, if poisoning is enabled */ - if (!CHECK_POISON_ALL_BLOCKS()) { - return NULL; - } - - /* check full integrity of the heap, if this check is enabled */ - if (!INTEGRITY_CHECK()) { - return NULL; - } - - size += POISON_SIZE(size); - ret = _umm_realloc( ptr, size ); - if (0 != size && 0 == ret) { - umm_last_fail_alloc_addr = __builtin_return_address(0); - umm_last_fail_alloc_size = size; - } - - ret = GET_POISONED(ret, size); - - return ret; -} - -/* ------------------------------------------------------------------------ */ - -void umm_free( void *ptr ) { - - ptr = GET_UNPOISONED(ptr); - - /* check poison of each blocks, if poisoning is enabled */ - if (!CHECK_POISON_ALL_BLOCKS()) { - return; - } - - /* check full integrity of the heap, if this check is enabled */ - if (!INTEGRITY_CHECK()) { - return; - } - - _umm_free( ptr ); -} - -/* ------------------------------------------------------------------------ */ - -size_t ICACHE_FLASH_ATTR umm_free_heap_size( void ) { - return (size_t)ummStats.free_blocks * sizeof(umm_block); -} - -size_t ICACHE_FLASH_ATTR umm_free_heap_size_min( void ) { - return (size_t)ummStats.free_blocks_min * sizeof(umm_block); -} - -size_t ICACHE_FLASH_ATTR umm_free_heap_size_min_reset( void ) { - ummStats.free_blocks_min = ummStats.free_blocks; - return (size_t)ummStats.free_blocks_min * sizeof(umm_block); -} - -size_t ICACHE_FLASH_ATTR umm_max_block_size( void ) { - umm_info(NULL, 0); - return ummHeapInfo.maxFreeContiguousBlocks * sizeof(umm_block); -} - -size_t ICACHE_FLASH_ATTR umm_block_size( void ) { - return sizeof(umm_block); -} - }; - -/* ------------------------------------------------------------------------ */ diff --git a/cores/esp8266/umm_malloc/umm_malloc.h b/cores/esp8266/umm_malloc/umm_malloc.h index fcb1ade82..4c68b7202 100644 --- a/cores/esp8266/umm_malloc/umm_malloc.h +++ b/cores/esp8266/umm_malloc/umm_malloc.h @@ -10,41 +10,18 @@ /* ------------------------------------------------------------------------ */ +//C This include is not in upstream neither are the #ifdef __cplusplus #include "umm_malloc_cfg.h" /* user-dependent */ #ifdef __cplusplus extern "C" { #endif -typedef struct UMM_HEAP_INFO_t { - unsigned short int totalEntries; - unsigned short int usedEntries; - unsigned short int freeEntries; - - unsigned short int totalBlocks; - unsigned short int usedBlocks; - unsigned short int freeBlocks; - - unsigned short int maxFreeContiguousBlocks; - - unsigned int freeSize2; -} -UMM_HEAP_INFO; - -extern UMM_HEAP_INFO ummHeapInfo; - -void umm_init( void ); - -void *umm_info( void *ptr, int force ); - +void umm_init( void ); void *umm_malloc( size_t size ); void *umm_calloc( size_t num, size_t size ); void *umm_realloc( void *ptr, size_t size ); -void umm_free( void *ptr ); - -size_t umm_free_heap_size( void ); -size_t umm_max_block_size( void ); -size_t umm_block_size( void ); +void umm_free( void *ptr ); #ifdef __cplusplus } diff --git a/cores/esp8266/umm_malloc/umm_malloc_cfg.h b/cores/esp8266/umm_malloc/umm_malloc_cfg.h index 39d5897cc..9d41d96b4 100644 --- a/cores/esp8266/umm_malloc/umm_malloc_cfg.h +++ b/cores/esp8266/umm_malloc/umm_malloc_cfg.h @@ -1,11 +1,17 @@ /* - * Configuration for umm_malloc + * Configuration for umm_malloc - target Arduino ESP8266 core + * + * Changes specific to a target platform go here. + * */ #ifndef _UMM_MALLOC_CFG_H #define _UMM_MALLOC_CFG_H #include +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -15,19 +21,6 @@ extern "C" { #include #include "c_types.h" -#include "umm_performance.h" -#include "umm_stats.h" - -#undef DBGLOG_FUNCTION -#if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_ISR) -int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2))); -// Note, _isr_safe_printf_P will not handle additional string arguments in -// PROGMEM. Only the 1st parameter, fmt, is supported in PROGMEM. -#define DBGLOG_FUNCTION(fmt, ...) _isr_safe_printf_P(PSTR(fmt), ##__VA_ARGS__) -#else -// Macro to place constant strings into PROGMEM and print them properly -#define DBGLOG_FUNCTION(fmt, ...) printf(PSTR(fmt), ## __VA_ARGS__ ) -#endif /* * There are a number of defines you can set at compile time that affect how @@ -37,19 +30,7 @@ int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2) * * -D UMM_TEST_MAIN * - * Set this if you want to compile in the test suite at the end of this file. - * - * If you leave this define unset, then you might want to set another one: - * - * -D UMM_REDEFINE_MEM_FUNCTIONS - * - * If you leave this define unset, then the function names are left alone as - * umm_malloc() umm_free() and umm_realloc() so that they cannot be confused - * with the C runtime functions malloc() free() and realloc() - * - * If you do set this define, then the function names become malloc() - * free() and realloc() so that they can be used as the C runtime functions - * in an embedded environment. + * Set this if you want to compile in the test suite * * -D UMM_BEST_FIT (defualt) * @@ -75,46 +56,300 @@ int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2) * ---------------------------------------------------------------------------- */ -///////////////////////////////////////////////// -#ifdef DEBUG_ESP_OOM - -#define MEMLEAK_DEBUG - -// umm_*alloc are not renamed to *alloc - -void *umm_malloc( size_t size ); -void *umm_calloc( size_t num, size_t size ); -void *umm_realloc( void *ptr, size_t size ); -#define umm_free free -#define umm_zalloc(s) umm_calloc(1,s) - -void* malloc_loc (size_t s, const char* file, int line); -void* calloc_loc (size_t n, size_t s, const char* file, int line); -void* realloc_loc (void* p, size_t s, const char* file, int line); - -// *alloc are macro calling *alloc_loc calling+checking umm_*alloc() -// they are defined at the bottom of this file - -///////////////////////////////////////////////// -#else // !defined(ESP_DEBUG_OOM) - - // umm_*alloc are renamed to *alloc - #define UMM_REDEFINE_MEM_FUNCTIONS - +#ifdef TEST_BUILD +extern char test_umm_heap[]; #endif - #define UMM_BEST_FIT - +#ifdef TEST_BUILD +/* Start addresses and the size of the heap */ +#define UMM_MALLOC_CFG_HEAP_ADDR (test_umm_heap) +#define UMM_MALLOC_CFG_HEAP_SIZE 0x10000 +#else /* Start addresses and the size of the heap */ extern char _heap_start[]; -#define UMM_MALLOC_CFG__HEAP_ADDR ((uint32_t)&_heap_start) -#define UMM_MALLOC_CFG__HEAP_SIZE ((size_t)(0x3fffc000 - UMM_MALLOC_CFG__HEAP_ADDR)) +#define UMM_MALLOC_CFG_HEAP_ADDR ((uint32_t)&_heap_start[0]) +#define UMM_MALLOC_CFG_HEAP_SIZE ((size_t)(0x3fffc000 - UMM_MALLOC_CFG_HEAP_ADDR)) +#endif /* A couple of macros to make packing structures less compiler dependent */ #define UMM_H_ATTPACKPRE #define UMM_H_ATTPACKSUF __attribute__((__packed__)) +#define UMM_BEST_FIT +#undef UMM_FIRST_FIT + +/* + * -D UMM_INFO : + * + * Enables a dup of the heap contents and a function to return the total + * heap size that is unallocated - note this is not the same as the largest + * unallocated block on the heap! + */ + +#define UMM_INFO + +#ifdef UMM_INFO + typedef struct UMM_HEAP_INFO_t { + unsigned short int totalEntries; + unsigned short int usedEntries; + unsigned short int freeEntries; + + unsigned short int totalBlocks; + unsigned short int usedBlocks; + unsigned short int freeBlocks; + + unsigned short int maxFreeContiguousBlocks; + + unsigned int freeSize2; + } + UMM_HEAP_INFO; + + extern UMM_HEAP_INFO ummHeapInfo; + + void ICACHE_FLASH_ATTR *umm_info( void *ptr, int force ); + size_t ICACHE_FLASH_ATTR umm_free_heap_size( void ); + size_t ICACHE_FLASH_ATTR umm_max_block_size( void ); +#else +#endif + +/* + * -D UMM_STATS : + * -D UMM_STATS_FULL + * + * This option provides a lightweight alternative to using `umm_info` just for + * getting `umm_free_heap_size`. With this option, a "free blocks" value is + * updated on each call to malloc/free/realloc. This option does not offer all + * the information that `umm_info` would have generated. + * + * This option is good for cases where the free heap is checked frequently. An + * example is when an app closely monitors free heap to detect memory leaks. In + * this case a single-core CPUs interrupt processing would have suffered the + * most. + * + * UMM_STATS_FULL provides additional heap statistics. It can be used to gain + * additional insight into heap usage. This option would add an additional 132 + * bytes of IRAM. + * + * Status: TODO: Needs to be proposed for upstream. + */ +/* +#define UMM_STATS +#define UMM_STATS_FULL + */ + +/* + * For the ESP8266 we want at lest UMM_STATS built, so we have an ISR safe + * function to call for implementing xPortGetFreeHeapSize(), because umm_info() + * is in flash. + */ +#if !defined(UMM_STATS) && !defined(UMM_STATS_FULL) +#define UMM_STATS +#endif + +#if defined(UMM_STATS) && defined(UMM_STATS_FULL) +#undef UMM_STATS +#endif + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) + +typedef struct UMM_STATISTICS_t { + unsigned short int free_blocks; + size_t oom_count; +#ifdef UMM_STATS_FULL + unsigned short int free_blocks_min; + unsigned short int free_blocks_isr_min; + size_t alloc_max_size; + size_t last_alloc_size; + size_t id_malloc_count; + size_t id_malloc_zero_count; + size_t id_realloc_count; + size_t id_realloc_zero_count; + size_t id_free_count; + size_t id_free_null_count; +#endif +} +UMM_STATISTICS; +extern UMM_STATISTICS ummStats; + +#define STATS__FREE_BLOCKS_UPDATE(s) ummStats.free_blocks += (s) +#define STATS__OOM_UPDATE() ummStats.oom_count += 1 + +size_t umm_free_heap_size_lw( void ); + +static inline size_t ICACHE_FLASH_ATTR umm_get_oom_count( void ) { + return ummStats.oom_count; +} + +#else // not UMM_STATS or UMM_STATS_FULL +#define STATS__FREE_BLOCKS_UPDATE(s) (void)(s) +#define STATS__OOM_UPDATE() (void)0 +#endif + +#if defined(UMM_STATS) || defined(UMM_STATS_FULL) || defined(UMM_INFO) +size_t ICACHE_FLASH_ATTR umm_block_size( void ); +#endif + +#ifdef UMM_STATS_FULL +#define STATS__FREE_BLOCKS_MIN() \ +do { \ + if (ummStats.free_blocks < ummStats.free_blocks_min) \ + ummStats.free_blocks_min = ummStats.free_blocks; \ +} while(false) + +#define STATS__FREE_BLOCKS_ISR_MIN() \ +do { \ + if (ummStats.free_blocks < ummStats.free_blocks_isr_min) \ + ummStats.free_blocks_isr_min = ummStats.free_blocks; \ +} while(false) + +#define STATS__ALLOC_REQUEST(tag, s) \ +do { \ + ummStats.tag##_count += 1; \ + ummStats.last_alloc_size = s; \ + if (ummStats.alloc_max_size < s) \ + ummStats.alloc_max_size = s; \ +} while(false) + +#define STATS__ZERO_ALLOC_REQUEST(tag, s) \ +do { \ + ummStats.tag##_zero_count += 1; \ +} while(false) + +#define STATS__NULL_FREE_REQUEST(tag) \ +do { \ + ummStats.tag##_null_count += 1; \ +} while(false) + +#define STATS__FREE_REQUEST(tag) \ +do { \ + ummStats.tag##_count += 1; \ +} while(false) + +static inline size_t ICACHE_FLASH_ATTR umm_free_heap_size_lw_min( void ) { + return (size_t)ummStats.free_blocks_min * umm_block_size(); +} + +static inline size_t ICACHE_FLASH_ATTR umm_free_heap_size_min_reset( void ) { + ummStats.free_blocks_min = ummStats.free_blocks; + return (size_t)ummStats.free_blocks_min * umm_block_size(); +} + +static inline size_t ICACHE_FLASH_ATTR umm_free_heap_size_min( void ) { + return ummStats.free_blocks_min * umm_block_size(); +} + +static inline size_t ICACHE_FLASH_ATTR umm_free_heap_size_isr_min( void ) { + return ummStats.free_blocks_isr_min * umm_block_size(); +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_max_alloc_size( void ) { + return ummStats.alloc_max_size; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_last_alloc_size( void ) { + return ummStats.last_alloc_size; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_malloc_count( void ) { + return ummStats.id_malloc_count; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_malloc_zero_count( void ) { + return ummStats.id_malloc_zero_count; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_realloc_count( void ) { + return ummStats.id_realloc_count; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_realloc_zero_count( void ) { + return ummStats.id_realloc_zero_count; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_free_count( void ) { + return ummStats.id_free_count; +} + +static inline size_t ICACHE_FLASH_ATTR umm_get_free_null_count( void ) { + return ummStats.id_free_null_count; +} + +#else // Not UMM_STATS_FULL +#define STATS__FREE_BLOCKS_MIN() (void)0 +#define STATS__FREE_BLOCKS_ISR_MIN() (void)0 +#define STATS__ALLOC_REQUEST(tag, s) (void)(s) +#define STATS__ZERO_ALLOC_REQUEST(tag, s) (void)(s) +#define STATS__NULL_FREE_REQUEST(tag) (void)0 +#define STATS__FREE_REQUEST(tag) (void)0 +#endif + +/* + Per Devyte, the core currently doesn't support masking a specific interrupt + level. That doesn't mean it can't be implemented, only that at this time + locking is implemented as all or nothing. + https://github.com/esp8266/Arduino/issues/6246#issuecomment-508612609 + + So for now we default to all, 15. + */ +#ifndef DEFAULT_CRITICAL_SECTION_INTLEVEL +#define DEFAULT_CRITICAL_SECTION_INTLEVEL 15 +#endif + +/* + * -D UMM_CRITICAL_METRICS + * + * Build option to collect timing usage data on critical section usage in + * functions: info, malloc, realloc. Collects MIN, MAX, and number of time IRQs + * were disabled at request time. Note, for realloc MAX disabled time will + * include the time spent in calling malloc and/or free. Examine code for + * specifics on what info is available and how to access. + * + * Status: TODO: Needs to be proposed for upstream. Also should include updates + * to UMM_POISON_CHECK and UMM_INTEGRITY_CHECK to include a critical section. + */ +/* +#define UMM_CRITICAL_METRICS + */ + +#if defined(UMM_CRITICAL_METRICS) +// This option adds support for gathering time locked data + +typedef struct UMM_TIME_STAT_t { + uint32_t min; + uint32_t max; + uint32_t start; + uint32_t intlevel; +} +UMM_TIME_STAT; + +typedef struct UMM_TIME_STATS_t UMM_TIME_STATS; + +extern UMM_TIME_STATS time_stats; + +bool get_umm_get_perf_data(UMM_TIME_STATS *p, size_t size); + +static inline void _critical_entry(UMM_TIME_STAT *p, uint32_t *saved_ps) { + *saved_ps = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL); + if (0U != (*saved_ps & 0x0FU)) { + p->intlevel += 1U; + } + + p->start = esp_get_cycle_count(); +} + +static inline void _critical_exit(UMM_TIME_STAT *p, uint32_t *saved_ps) { + uint32_t elapse = esp_get_cycle_count() - p->start; + if (elapse < p->min) + p->min = elapse; + + if (elapse > p->max) + p->max = elapse; + + xt_wsr_ps(*saved_ps); +} +#endif + /* * A couple of macros to make it easier to protect the memory allocator * in a multitasking system. You should set these macros up to use whatever @@ -125,23 +360,82 @@ extern char _heap_start[]; * called from within umm_malloc() */ - -#if defined(UMM_CRITICAL_PERIOD_ANALYZE) - -#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag -#define UMM_CRITICAL_ENTRY(tag) _critical_entry(&time_stats.tag, &_saved_ps_##tag) -#define UMM_CRITICAL_EXIT(tag) _critical_exit(&time_stats.tag, &_saved_ps_##tag) - +#ifdef TEST_BUILD + extern int umm_critical_depth; + extern int umm_max_critical_depth; + #define UMM_CRITICAL_ENTRY() {\ + ++umm_critical_depth; \ + if (umm_critical_depth > umm_max_critical_depth) { \ + umm_max_critical_depth = umm_critical_depth; \ + } \ + } + #define UMM_CRITICAL_EXIT() (umm_critical_depth--) #else + #if defined(UMM_CRITICAL_METRICS) + #define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag + #define UMM_CRITICAL_ENTRY(tag)_critical_entry(&time_stats.tag, &_saved_ps_##tag) + #define UMM_CRITICAL_EXIT(tag) _critical_exit(&time_stats.tag, &_saved_ps_##tag) -// This method preserves the intlevel on entry and restores the -// original intlevel at exit. -#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag -#define UMM_CRITICAL_ENTRY(tag) _saved_ps_##tag = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL) -#define UMM_CRITICAL_EXIT(tag) xt_wsr_ps(_saved_ps_##tag) - + #else // ! UMM_CRITICAL_METRICS + // This method preserves the intlevel on entry and restores the + // original intlevel at exit. + #define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag + #define UMM_CRITICAL_ENTRY(tag) _saved_ps_##tag = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL) + #define UMM_CRITICAL_EXIT(tag) xt_wsr_ps(_saved_ps_##tag) + #endif #endif + /* + * -D UMM_LIGHTWEIGHT_CPU + * + * The use of this macro is hardware/application specific. + * + * With some CPUs, the only available method for locking are the instructions + * for interrupts disable/enable. These macros are meant for lightweight single + * CPU systems that are sensitive to interrupts being turned off for too long. A + * typically UMM_CRITICAL_ENTRY would save current IRQ state then disable IRQs. + * Then UMM_CRITICAL_EXIT would restore previous IRQ state. This option adds + * additional critical entry/exit points by the method of defining the macros + * UMM_CRITICAL_SUSPEND and UMM_CRITICAL_RESUME to the values of + * UMM_CRITICAL_EXIT and UMM_CRITICAL_ENTRY. These additional exit/entries + * allow time to service interrupts during the reentrant sections of the code. + * + * Performance may be impacked if used with multicore CPUs. The higher frquency + * of locking and unlocking may be an issue with locking methods that have a + * high overhead. + * + * Status: TODO: Needs to be proposed for upstream. + */ +/* + */ +#define UMM_LIGHTWEIGHT_CPU + +#ifdef UMM_LIGHTWEIGHT_CPU +#define UMM_CRITICAL_SUSPEND(tag) UMM_CRITICAL_EXIT(tag) +#define UMM_CRITICAL_RESUME(tag) UMM_CRITICAL_ENTRY(tag) +#else +#define UMM_CRITICAL_SUSPEND(tag) do {} while(0) +#define UMM_CRITICAL_RESUME(tag) do {} while(0) +#endif + +/* + * -D UMM_REALLOC_MINIMIZE_COPY or + * -D UMM_REALLOC_DEFRAG + * + * Pick one of these two stratagies. UMM_REALLOC_MINIMIZE_COPY grows upward or + * shrinks an allocation, avoiding copy when possible. UMM_REALLOC_DEFRAG gives + * priority with growing the revised allocation toward an adjacent hole in the + * direction of the beginning of the heap when possible. + * + * Status: TODO: These are new options introduced to optionally restore the + * previous defrag propery of realloc. The issue has been raised in the upstream + * repo. No response at this time. Based on response, may propose for upstream. + */ +/* +#define UMM_REALLOC_MINIMIZE_COPY +*/ +#define UMM_REALLOC_DEFRAG + /* * -D UMM_INTEGRITY_CHECK : * @@ -155,12 +449,28 @@ extern char _heap_start[]; * 4 bytes, so there might be some trailing "extra" bytes which are not checked * for corruption. */ -/* -#define UMM_INTEGRITY_CHECK -*/ /* - * -D UMM_POISON : + * Not normally enabled. Full intergity check may exceed 10us. + */ +/* +#define UMM_INTEGRITY_CHECK + */ + +#ifdef UMM_INTEGRITY_CHECK + int umm_integrity_check( void ); +# define INTEGRITY_CHECK() umm_integrity_check() + extern void umm_corruption(void); +# define UMM_HEAP_CORRUPTION_CB() DBGLOG_FUNCTION( "Heap Corruption!" ) +#else +# define INTEGRITY_CHECK() 0 +#endif + +///////////////////////////////////////////////// + +/* + * -D UMM_POISON_CHECK : + * -D UMM_POISON_CHECK_LITE * * Enables heap poisoning: add predefined value (poison) before and after each * allocation, and check before each heap operation that no poison is @@ -185,17 +495,135 @@ extern char _heap_start[]; * * If poison corruption is detected, the message is printed and user-provided * callback is called: `UMM_HEAP_CORRUPTION_CB()` + * + * UMM_POISON_CHECK - does a global heap check on all active allocation at + * every alloc API call. May exceed 10us due to critical section with IRQs + * disabled. + * + * UMM_POISON_CHECK_LITE - checks the allocation presented at realloc() + * and free(). Expands the poison check on the current allocation to + * include its nearest allocated neighbors in the heap. + * umm_malloc() will also checks the neighbors of the selected allocation + * before use. + * + * Status: TODO?: UMM_POISON_CHECK_LITE is a new option. We could propose for + * upstream; however, the upstream version has much of the framework for calling + * poison check on each alloc call refactored out. Not sure how this will be + * received. */ +/* + * Compatibility for deprecated UMM_POISON + */ +#if defined(UMM_POISON) && !defined(UMM_POISON_CHECK) +#define UMM_POISON_CHECK_LITE +#endif + #if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_CORE) -#define UMM_POISON +#if !defined(UMM_POISON_CHECK) && !defined(UMM_POISON_CHECK_LITE) +/* +#define UMM_POISON_CHECK + */ + #define UMM_POISON_CHECK_LITE +#endif #endif #define UMM_POISON_SIZE_BEFORE 4 -#define UMM_POISON_SIZE_AFTER 4 +#define UMM_POISON_SIZE_AFTER 4 #define UMM_POISONED_BLOCK_LEN_TYPE uint32_t -#define UMM_HEAP_CORRUPTION_CB() panic() +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) + void *umm_poison_malloc( size_t size ); + void *umm_poison_calloc( size_t num, size_t size ); + void *umm_poison_realloc( void *ptr, size_t size ); + void umm_poison_free( void *ptr ); + int umm_poison_check( void ); + // Local Additions to better report location in code of the caller. + void *umm_poison_realloc_fl( void *ptr, size_t size, const char* file, int line ); + void umm_poison_free_fl( void *ptr, const char* file, int line ); + #if defined(UMM_POISON_CHECK_LITE) + /* + * We can safely do individual poison checks at free and realloc and stay + * under 10us or close. + */ + # define POISON_CHECK() 1 + # define POISON_CHECK_NEIGHBORS(c) \ + do {\ + if(!check_poison_neighbors(c)) \ + panic();\ + } while(false) + #else + /* Not normally enabled. A full heap poison check may exceed 10us. */ + # define POISON_CHECK() umm_poison_check() + # define POISON_CHECK_NEIGHBORS(c) do{}while(false) + #endif +#else +# define POISON_CHECK() 1 +# define POISON_CHECK_NEIGHBORS(c) do{}while(false) +#endif + +///////////////////////////////////////////////// +#undef DBGLOG_FUNCTION +#undef DBGLOG_FUNCTION_P + +#if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_OOM) || \ + defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || \ + defined(UMM_INTEGRITY_CHECK) +#define DBGLOG_FUNCTION(fmt, ...) ets_uart_printf(fmt, ##__VA_ARGS__) +#else +#define DBGLOG_FUNCTION(fmt, ...) do { (void)fmt; } while(false) +#endif + +///////////////////////////////////////////////// + +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK) +#if !defined(DBGLOG_LEVEL) || DBGLOG_LEVEL < 3 +// All debug prints in UMM_POISON_CHECK are level 3 +#undef DBGLOG_LEVEL +#define DBGLOG_LEVEL 3 +#endif +#endif + +#if defined(UMM_CRITICAL_METRICS) +struct UMM_TIME_STATS_t { + UMM_TIME_STAT id_malloc; + UMM_TIME_STAT id_realloc; + UMM_TIME_STAT id_free; +#ifdef UMM_INFO + UMM_TIME_STAT id_info; +#endif +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) + UMM_TIME_STAT id_poison; +#endif +#ifdef UMM_INTEGRITY_CHECK + UMM_TIME_STAT id_integrity; +#endif + UMM_TIME_STAT id_no_tag; +}; +#endif +///////////////////////////////////////////////// +#ifdef DEBUG_ESP_OOM + +#define MEMLEAK_DEBUG + +// umm_*alloc are not renamed to *alloc +// Assumes umm_malloc.h has already been included. + +#define umm_zalloc(s) umm_calloc(1,s) + +void* malloc_loc (size_t s, const char* file, int line); +void* calloc_loc (size_t n, size_t s, const char* file, int line); +void* realloc_loc (void* p, size_t s, const char* file, int line); +// *alloc are macro calling *alloc_loc calling+checking umm_*alloc() +// they are defined at the bottom of this file + +///////////////////////////////////////////////// + +#elif defined(UMM_POISON_CHECK) +void* realloc_loc (void* p, size_t s, const char* file, int line); +void free_loc (void* p, const char* file, int line); +#else // !defined(ESP_DEBUG_OOM) +#endif #ifdef __cplusplus } @@ -203,12 +631,57 @@ extern char _heap_start[]; #endif /* _UMM_MALLOC_CFG_H */ +#ifdef __cplusplus +extern "C" { +#endif #ifdef DEBUG_ESP_OOM // this must be outside from "#ifndef _UMM_MALLOC_CFG_H" // because Arduino.h's does #undef *alloc // Arduino.h recall us to redefine them #include -#define malloc(s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; malloc_loc(s, mem_debug_file, __LINE__); }) -#define calloc(n,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; calloc_loc(n, s, mem_debug_file, __LINE__); }) -#define realloc(p,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; realloc_loc(p, s, mem_debug_file, __LINE__); }) +// Reuse pvPort* calls, since they already support passing location information. +void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line); +void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line); +void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line); +void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line); +void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line); + +#define malloc(s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; pvPortMalloc(s, mem_debug_file, __LINE__); }) +#define calloc(n,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; pvPortCalloc(n, s, mem_debug_file, __LINE__); }) +#define realloc(p,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; pvPortRealloc(p, s, mem_debug_file, __LINE__); }) + +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) +#define dbg_heap_free(p) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; vPortFree(p, mem_debug_file, __LINE__); }) +#else +#define dbg_heap_free(p) free(p) +#endif + +#elif defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) +#include +void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line); +#define realloc(p,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; pvPortRealloc(p, s, mem_debug_file, __LINE__); }) + +void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line); +//C - to be discussed +/* + Problem, I would like to report the file and line number with the umm poison + event as close as possible to the event. The #define method works for malloc, + calloc, and realloc those names are not as generic as free. A #define free + captures too much. Classes with methods called free are included :( + Inline functions would report the address of the inline function in the .h + not where they are called. + + Anybody know a trick to make this work? + + Create dbg_heap_free() as an alternative for free() when you need a little + more help in debugging the more challenging problems. +*/ +#define dbg_heap_free(p) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; vPortFree(p, mem_debug_file, __LINE__); }) + +#else +#define dbg_heap_free(p) free(p) #endif /* DEBUG_ESP_OOM */ + +#ifdef __cplusplus +} +#endif diff --git a/cores/esp8266/umm_malloc/umm_performance.cpp b/cores/esp8266/umm_malloc/umm_performance.cpp deleted file mode 100644 index 4b9ec4e6b..000000000 --- a/cores/esp8266/umm_malloc/umm_performance.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * umm_malloc performance measurments and ESP specifics - */ - -#include -#include -#include -#include -#include "umm_performance.h" -#include "umm_stats.h" - -extern "C" { - -UMM_STATS ummStats = {0, 0, 0, 0}; - -#ifdef UMM_CRITICAL_PERIOD_ANALYZE -struct _UMM_TIME_STATS time_stats = { - {0xFFFFFFFF, 0U, 0U, 0U}, - {0xFFFFFFFF, 0U, 0U, 0U}, - {0xFFFFFFFF, 0U, 0U, 0U}, - {0xFFFFFFFF, 0U, 0U, 0U} }; - -bool ICACHE_FLASH_ATTR get_umm_get_perf_data(struct _UMM_TIME_STATS *p, size_t size) { - if (p && sizeof(time_stats) == size) { - uint32_t save_ps = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL); - memcpy(p, &time_stats, size); - xt_wsr_ps(save_ps); - return true; - } - return false; -} -#endif - -#if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_ISR) -/* - Printing from the malloc routines is tricky. Since a lot of library calls - will want to do malloc. - - Objective: To be able to print "last gasp" diagnostic messages - when interrupts are disabled and w/o availability of heap resources. -*/ - -// ROM _putc1, ignores CRs and sends CR/LF for LF, newline. -// Always returns character sent. -int constexpr (*_rom_putc1)(int) = (int (*)(int))0x40001dcc; -void uart_buff_switch(uint8_t); - -int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2))); -int ICACHE_RAM_ATTR _isr_safe_printf_P(const char *fmt, ...) { -#ifdef DEBUG_ESP_PORT -#define VALUE(x) __STRINGIFY(x) - // Preprocessor and compiler together will optimize away the if. - if (strcmp("Serial1", VALUE(DEBUG_ESP_PORT)) == 0) { - uart_buff_switch(1U); - } else { - uart_buff_switch(0U); - } -#else - uart_buff_switch(0U); // Side effect, clears RX FIFO -#endif - /* - To use ets_strlen() and ets_memcpy() safely with PROGMEM, flash storage, - the PROGMEM address must be word (4 bytes) aligned. The destination - address for ets_memcpy must also be word-aligned. We also round the - buf_len up to the nearest word boundary. So that all transfers will be - whole words. - */ - size_t str_len = ets_strlen(fmt); - size_t buf_len = (str_len + 1 + 3) & ~0x03U; - char ram_buf[buf_len] __attribute__ ((aligned(4))); - ets_memcpy(ram_buf, fmt, buf_len); - va_list argPtr; - va_start(argPtr, fmt); - int result = ets_vprintf(_rom_putc1, ram_buf, argPtr); - va_end(argPtr); - return result; -} - -#endif - -}; diff --git a/cores/esp8266/umm_malloc/umm_performance.h b/cores/esp8266/umm_malloc/umm_performance.h deleted file mode 100644 index 5d6ae24c6..000000000 --- a/cores/esp8266/umm_malloc/umm_performance.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * umm_malloc performance measurments and ESP specifics - */ - -#ifndef _UMM_PERFORMANCE_H -#define _UMM_PERFORMANCE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * -D UMM_CRITICAL_PERIOD_ANALYZE : - * - * Build option to collect timing usage data on critical section usage in - * functions: info, malloc, realloc. Collects MIN, MAX, and number of time - * IRQs were disabled at request time. Note, for realloc MAX disabled time - * will not include the time from calling malloc and/or free. - * Examine code for specifics on what info is available and how to access. -*/ -// #define UMM_CRITICAL_PERIOD_ANALYZE - - -/* - Per Devyte, the core currently doesn't support masking a specific interrupt - level. That doesn't mean it can't be implemented, only that at this time - locking is implemented as all or nothing. - https://github.com/esp8266/Arduino/issues/6246#issuecomment-508612609 - - So for now we default to all, 15. - */ -#ifndef DEFAULT_CRITICAL_SECTION_INTLEVEL -#define DEFAULT_CRITICAL_SECTION_INTLEVEL 15 -#endif - -#if defined(UMM_CRITICAL_PERIOD_ANALYZE) -// This option adds support for gathering time locked data -typedef struct _TIME_STAT { - uint32_t min; - uint32_t max; - uint32_t start; - uint32_t intlevel; -} time_stat_t; - -struct _UMM_TIME_STATS { - time_stat_t id_malloc; - time_stat_t id_realloc; - time_stat_t id_free; - time_stat_t id_info; -}; - -extern struct _UMM_TIME_STATS time_stats; - -bool get_umm_get_perf_data(struct _UMM_TIME_STATS *p, size_t size); - -static inline void _critical_entry(time_stat_t *p, uint32_t *saved_ps) { - *saved_ps = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL); - if (0U != (*saved_ps & 0x0FU)) { - p->intlevel += 1U; - } - - p->start = esp_get_cycle_count(); -} - -static inline void _critical_exit(time_stat_t *p, uint32_t *saved_ps) { - uint32_t elapse = esp_get_cycle_count() - p->start; - if (elapse < p->min) - p->min = elapse; - - if (elapse > p->max) - p->max = elapse; - - xt_wsr_ps(*saved_ps); -} -#endif - -#if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_ISR) -int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2))); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* _UMM_PERFORMANCE_H */ diff --git a/cores/esp8266/umm_malloc/umm_poison.c b/cores/esp8266/umm_malloc/umm_poison.c new file mode 100644 index 000000000..760ce1802 --- /dev/null +++ b/cores/esp8266/umm_malloc/umm_poison.c @@ -0,0 +1,241 @@ +#if defined(BUILD_UMM_MALLOC_C) + +/* poisoning (UMM_POISON_CHECK) {{{ */ +#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) +#define POISON_BYTE (0xa5) + +/* + * Yields a size of the poison for the block of size `s`. + * If `s` is 0, returns 0. + */ +static size_t poison_size(size_t s) { + return(s ? (UMM_POISON_SIZE_BEFORE + + sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + + UMM_POISON_SIZE_AFTER) + : 0); +} + +/* + * Print memory contents starting from given `ptr` + */ +static void dump_mem ( const unsigned char *ptr, size_t len ) { + while (len--) { + DBGLOG_ERROR(" 0x%.2x", (unsigned int)(*ptr++)); + } +} + +/* + * Put poison data at given `ptr` and `poison_size` + */ +static void put_poison( unsigned char *ptr, size_t poison_size ) { + memset(ptr, POISON_BYTE, poison_size); +} + +/* + * Check poison data at given `ptr` and `poison_size`. `where` is a pointer to + * a string, either "before" or "after", meaning, before or after the block. + * + * If poison is there, returns 1. + * Otherwise, prints the appropriate message, and returns 0. + */ +static int check_poison( const unsigned char *ptr, size_t poison_size, + const char *where) { + size_t i; + int ok = 1; + + for (i = 0; i < poison_size; i++) { + if (ptr[i] != POISON_BYTE) { + ok = 0; + break; + } + } + + if (!ok) { + DBGLOG_ERROR( "No poison %s block at: 0x%lx, actual data:", where, (unsigned long)ptr); + dump_mem(ptr, poison_size); + DBGLOG_ERROR( "\n" ); + } + + return ok; +} + +/* + * Check if a block is properly poisoned. Must be called only for non-free + * blocks. + */ +static int check_poison_block( umm_block *pblock ) { + int ok = 1; + + if (pblock->header.used.next & UMM_FREELIST_MASK) { + DBGLOG_ERROR( "check_poison_block is called for free block 0x%lx\n", (unsigned long)pblock); + } else { + /* the block is used; let's check poison */ + unsigned char *pc = (unsigned char *)pblock->body.data; + unsigned char *pc_cur; + + pc_cur = pc + sizeof(UMM_POISONED_BLOCK_LEN_TYPE); + if (!check_poison(pc_cur, UMM_POISON_SIZE_BEFORE, "before")) { + ok = 0; + goto clean; + } + + pc_cur = pc + *((UMM_POISONED_BLOCK_LEN_TYPE *)pc) - UMM_POISON_SIZE_AFTER; + if (!check_poison(pc_cur, UMM_POISON_SIZE_AFTER, "after")) { + ok = 0; + goto clean; + } + } + +clean: + return ok; +} + +/* + * Takes a pointer returned by actual allocator function (`umm_malloc` or + * `umm_realloc`), puts appropriate poison, and returns adjusted pointer that + * should be returned to the user. + * + * `size_w_poison` is a size of the whole block, including a poison. + */ +static void *get_poisoned( void *v_ptr, size_t size_w_poison ) { + unsigned char *ptr = (unsigned char *)v_ptr; + + if (size_w_poison != 0 && ptr != NULL) { + + /* Poison beginning and the end of the allocated chunk */ + put_poison(ptr + sizeof(UMM_POISONED_BLOCK_LEN_TYPE), + UMM_POISON_SIZE_BEFORE); + put_poison(ptr + size_w_poison - UMM_POISON_SIZE_AFTER, + UMM_POISON_SIZE_AFTER); + + /* Put exact length of the user's chunk of memory */ + *(UMM_POISONED_BLOCK_LEN_TYPE *)ptr = (UMM_POISONED_BLOCK_LEN_TYPE)size_w_poison; + + /* Return pointer at the first non-poisoned byte */ + ptr += sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE; + } + + return (void *)ptr; +} + +/* + * Takes "poisoned" pointer (i.e. pointer returned from `get_poisoned()`), + * and checks that the poison of this particular block is still there. + * + * Returns unpoisoned pointer, i.e. actual pointer to the allocated memory. + */ +static void *get_unpoisoned( void *v_ptr ) { + unsigned char *ptr = (unsigned char *)v_ptr; + + if (ptr != NULL) { + unsigned short int c; + + ptr -= (sizeof(UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE); + + /* Figure out which block we're in. Note the use of truncated division... */ + c = (((char *)ptr)-(char *)(&(umm_heap[0])))/sizeof(umm_block); + + check_poison_block(&UMM_BLOCK(c)); + } + + return (void *)ptr; +} + +/* }}} */ + +/* ------------------------------------------------------------------------ */ + +void *umm_poison_malloc( size_t size ) { + void *ret; + + size += poison_size(size); + + ret = umm_malloc( size ); + + ret = get_poisoned(ret, size); + + return ret; +} + +/* ------------------------------------------------------------------------ */ + +void *umm_poison_calloc( size_t num, size_t item_size ) { + void *ret; + size_t size = item_size * num; + + size += poison_size(size); + + ret = umm_malloc(size); + + if (NULL != ret) + memset(ret, 0x00, size); + + ret = get_poisoned(ret, size); + + return ret; +} + +/* ------------------------------------------------------------------------ */ + +void *umm_poison_realloc( void *ptr, size_t size ) { + void *ret; + + ptr = get_unpoisoned(ptr); + + size += poison_size(size); + ret = umm_realloc( ptr, size ); + + ret = get_poisoned(ret, size); + + return ret; +} + +/* ------------------------------------------------------------------------ */ + +void umm_poison_free( void *ptr ) { + + ptr = get_unpoisoned(ptr); + + umm_free( ptr ); +} + +/* + * Iterates through all blocks in the heap, and checks poison for all used + * blocks. + */ + +int umm_poison_check(void) { + UMM_CRITICAL_DECL(id_poison); + int ok = 1; + unsigned short int cur; + + if (umm_heap == NULL) { + umm_init(); + } + + UMM_CRITICAL_ENTRY(id_poison); + + /* Now iterate through the blocks list */ + cur = UMM_NBLOCK(0) & UMM_BLOCKNO_MASK; + + while( UMM_NBLOCK(cur) & UMM_BLOCKNO_MASK ) { + if ( !(UMM_NBLOCK(cur) & UMM_FREELIST_MASK) ) { + /* This is a used block (not free), so, check its poison */ + ok = check_poison_block(&UMM_BLOCK(cur)); + if (!ok){ + break; + } + } + + cur = UMM_NBLOCK(cur) & UMM_BLOCKNO_MASK; + } + UMM_CRITICAL_EXIT(id_poison); + + return ok; +} + +/* ------------------------------------------------------------------------ */ + +#endif + +#endif // defined(BUILD_UMM_MALLOC_C) diff --git a/cores/esp8266/umm_malloc/umm_stats.h b/cores/esp8266/umm_malloc/umm_stats.h deleted file mode 100644 index f2aae9875..000000000 --- a/cores/esp8266/umm_malloc/umm_stats.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * umm_malloc heap statistics - */ - -#ifndef _UMM_STATS_H -#define _UMM_STATS_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct UMM_STATS_t { - unsigned short int free_blocks; - unsigned short int free_blocks_min; - size_t alloc_max_size; - size_t oom_count; -} UMM_STATS; -extern UMM_STATS ummStats; - -size_t ICACHE_FLASH_ATTR umm_free_heap_size_min( void ); -size_t ICACHE_FLASH_ATTR umm_free_heap_size_min_reset( void ); - -inline size_t umm_get_max_alloc_size( void ) { - return ummStats.alloc_max_size; -} - -inline size_t umm_get_oom_count( void ) { - return ummStats.oom_count; -} - - -#ifdef __cplusplus -} -#endif - -#endif /* _UMM_STATS_H */ diff --git a/doc/filesystem.rst b/doc/filesystem.rst index a56ff8fb0..d70655f2f 100644 --- a/doc/filesystem.rst +++ b/doc/filesystem.rst @@ -175,16 +175,18 @@ use esptool.py. - Download the tool: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.4.0/ESP8266FS-0.4.0.zip - In your Arduino sketchbook directory, create ``tools`` directory if - it doesn't exist yet + it doesn't exist yet. - Unpack the tool into ``tools`` directory (the path will look like ``/Arduino/tools/ESP8266FS/tool/esp8266fs.jar``) If upgrading, overwrite the existing JAR file with the newer version. -- Restart Arduino IDE -- Open a sketch (or create a new one and save it) -- Go to sketch directory (choose Sketch > Show Sketch Folder) +- Restart Arduino IDE. +- Open a sketch (or create a new one and save it). +- Go to sketch directory (choose Sketch > Show Sketch Folder). - Create a directory named ``data`` and any files you want in the file - system there -- Make sure you have selected a board, port, and closed Serial Monitor + system there. +- Make sure you have selected a board, port, and closed Serial Monitor. +- If your board requires you to press a button (or other action) to enter + bootload mode for flashing a sketch, do that now. - Select Tools > ESP8266 Sketch Data Upload. This should start uploading the files into ESP8266 flash file system. When done, IDE status bar will display ``SPIFFS Image Uploaded`` message. diff --git a/doc/installing.rst b/doc/installing.rst index 1c6317d2b..a83d4161f 100644 --- a/doc/installing.rst +++ b/doc/installing.rst @@ -12,6 +12,7 @@ Prerequisites - Arduino 1.6.8, get it from `Arduino website `__. - Internet connection +- Python 3 interpreter (Mac/Linux only, Windows installation supplies its own) Instructions ~~~~~~~~~~~~ @@ -46,10 +47,13 @@ Prerequisites - Python 3.x (https://python.org) - terminal, console, or command prompt (depending on your OS) - Internet connection +- Uninstalling any core version installed via Board Manager Instructions - Windows 10 ~~~~~~~~~~~~~~~~~~~~~~~~~ -- First, make sure you don't already have the ESP8266 library installed using the Board Manager (see above) +- First, make sure you don't already have an ESP8266 core version installed + using the Board Manager (see above). If you do, uninstall it from the + Board Manager before proceeding. - Install git for Windows (if not already; see https://git-scm.com/download/win) @@ -130,9 +134,14 @@ Note that you could, in theory install in ``C:\Program Files (x86)\Arduino\hardw Instructions - Other OS ~~~~~~~~~~~~~~~~~~~~~~~ +- First, make sure you don't already have an ESP8266 core version installed + using the Board Manager (see above). If you do, uninstall it from the + Board Manager before proceeding. + - Open the console and go to Arduino directory. This can be either your *sketchbook* directory (usually ``/Arduino``), or the directory of Arduino application itself, the choice is up to you. + - Clone this repository into hardware/esp8266com/esp8266 directory. Alternatively, clone it elsewhere and create a symlink, if your OS supports them. @@ -186,9 +195,33 @@ Instructions - Other OS cd esp8266/tools python3 get.py + If you get an error message stating that python3 is not found, you will need to install it (most modern UNIX-like OSes provide Python 3 as + part of the default install). To install you will need to use ``sudo yum install python3``, ``sudo apt install python3``, or ``brew install python3`` + as appropriate. On the Mac you may get an error message like: + + .. code:: bash + + python3 get.py + Platform: x86_64-apple-darwin + Downloading python3-macosx-placeholder.tar.gz + Traceback (most recent call last): + File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open + encode_chunked=req.has_header('Transfer-encoding')) + ... + File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake + self._sslobj.do_handshake() + ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056) + + This is because Homebrew on the Mac does not always install the required SSL certificates by default. Install them manually (adjust the Python 3.7 as needed) with: + + .. code:: bash + + cd "/Applications/Python 3.7/" && sudo "./Install Certificates.command" + + - Restart Arduino -- When later updating your local library, goto the esp8266 directory and do a git pull +- When later updating your local library, goto the esp8266 directory and do a git pull .. code:: bash diff --git a/doc/libraries.rst b/doc/libraries.rst index a7c3ab0e6..c902dc196 100644 --- a/doc/libraries.rst +++ b/doc/libraries.rst @@ -29,6 +29,8 @@ EEPROM library uses one sector of flash located just after the SPIFFS. `Three examples `__ included. +Note that the sector needs to be re-flashed every time the changed EEPROM data needs to be saved, thus will wear out the flash memory very quickly even if small amounts of data are written. Consider using one of the EEPROM libraries mentioned down below. + I2C (Wire library) ------------------ @@ -141,14 +143,6 @@ Servo This library exposes the ability to control RC (hobby) servo motors. It will support up to 24 servos on any available output pin. By default the first 12 servos will use Timer0 and currently this will not interfere with any other support. Servo counts above 12 will use Timer1 and features that use it will be affected. While many RC servo motors will accept the 3.3V IO data pin from a ESP8266, most will not be able to run off 3.3v and will require another power source that matches their specifications. Make sure to connect the grounds between the ESP8266 and the servo motor power supply. -Improved EEPROM library for ESP (ESP_EEPROM) --------------------------------------------- - -An improved EEPROM library for ESPxxxx. Uses flash memory as per the standard ESP EEPROM library but reduces reflash - so reducing wear and improving commit() performance. - -As actions on the flash need to stop the interrupts, an EEPROM reflash could noticably affect anything using PWM, etc. - - Other libraries (not included with the IDE) ------------------------------------------- @@ -189,3 +183,5 @@ Libraries that don't rely on low-level access to AVR registers should work well. - `MFRC522 `__ - A library for using the Mifare RC522 RFID-tag reader/writer. - `Ping `__ - lets the ESP8266 ping a remote machine. - `AsyncPing `__ - fully asynchronous Ping library (have full ping statistic and hardware MAC address). +- `ESP_EEPROM `__ - This library writes a new copy of your data when you save (commit) it and keeps track of where in the sector the most recent copy is kept using a bitmap. The flash sector only needs to be erased when there is no more space for copies in the flash sector. +- `EEPROM Rotate `__ - Instead of using a single sector to persist the data from the emulated EEPROM, this library uses a number of sectors to do so: a sector pool. diff --git a/doc/ota_updates/readme.rst b/doc/ota_updates/readme.rst index 67f4f9110..f5c76f0d2 100644 --- a/doc/ota_updates/readme.rst +++ b/doc/ota_updates/readme.rst @@ -646,6 +646,8 @@ Update process - memory view - the new sketch is now copied "over" the old one. - the new sketch is started. +By default, filesystem updates are overriding the target flash directly. In order to use the same two step process set the `ATOMIC_FS_UPDATE` flag. + .. figure:: update_memory_copy.png :alt: Memory layout for OTA updates diff --git a/doc/reference.rst b/doc/reference.rst index 29ae74e37..db789128e 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -1,6 +1,42 @@ Reference ========= +Interrupts +---------- + +Interrupts can be used on the ESP8266, but they must be used with care +and have several limitations: + +* Interrupt callback functions must be in IRAM, because the flash may be + in the middle of other operations when they occur. Do this by adding + the ``ICACHE_RAM_ATTR`` attribute on the function definition. If this + attribute is not present, the sketch will crash when it attempts to + ``attachInterrupt`` with an error message. + +.. code:: cpp + + ICACHE_RAM_ATTR void gpio_change_handler(void *data) {... + +* Interrupts must not call ``delay()`` or ``yield()``, or call any routines + which internally use ``delay()`` or ``yield()`` either. + +* Long-running (>1ms) tasks in interrupts will cause instabilty or crashes. + WiFi and other portions of the core can become unstable if interrupts + are blocked by a long-running interrupt. If you have much to do, you can + set a volatile global flag that your main ``loop()`` can check each pass + or use a scheduled function (which will be called outside of the interrupt + context when it is safe) to do long-running work. + +* Memory operations can be dangerous and should be avoided in interrupts. + Calls to ``new`` or ``malloc`` should be minimized because they may require + a long running time if memory is fragmented. Calls to ``realloc`` and + ``free`` must NEVER be called. Using any routines or objects which call + ``free`` or ``realloc`` themselves is also forbidden for the same reason. + This means that ``String``, ``std::string``, ``std::vector`` and other + classes which use contiguous memory that may be resized must be used with + extreme care (ensuring strings aren't changed, vector elements aren't + added, etc.). + Digital IO ---------- diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index fa1aa3ee0..90bbf8ca5 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -21,6 +21,7 @@ #include "Arduino.h" #include "EEPROM.h" +#include "debug.h" extern "C" { #include "c_types.h" @@ -30,7 +31,7 @@ extern "C" { #include "spi_flash.h" } -extern "C" uint32_t _FS_end; +extern "C" uint32_t _EEPROM_start; EEPROMClass::EEPROMClass(uint32_t sector) : _sector(sector) @@ -41,7 +42,7 @@ EEPROMClass::EEPROMClass(uint32_t sector) } EEPROMClass::EEPROMClass(void) -: _sector((((uint32_t)&_FS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) +: _sector((((uint32_t)&_EEPROM_start - 0x40200000) / SPI_FLASH_SEC_SIZE)) , _data(0) , _size(0) , _dirty(false) @@ -49,10 +50,14 @@ EEPROMClass::EEPROMClass(void) } void EEPROMClass::begin(size_t size) { - if (size <= 0) + if (size <= 0) { + DEBUGV("EEPROMClass::begin error, size == 0\n"); return; - if (size > SPI_FLASH_SEC_SIZE) + } + if (size > SPI_FLASH_SEC_SIZE) { + DEBUGV("EEPROMClass::begin error, %d > %d\n", size, SPI_FLASH_SEC_SIZE); size = SPI_FLASH_SEC_SIZE; + } size = (size + 3) & (~3); @@ -66,9 +71,9 @@ void EEPROMClass::begin(size_t size) { _size = size; - noInterrupts(); - spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size); - interrupts(); + if (!ESP.flashRead(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size)) { + DEBUGV("EEPROMClass::begin flash read failed\n"); + } _dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time } @@ -88,19 +93,27 @@ void EEPROMClass::end() { uint8_t EEPROMClass::read(int const address) { - if (address < 0 || (size_t)address >= _size) + if (address < 0 || (size_t)address >= _size) { + DEBUGV("EEPROMClass::read error, address %d > %d or %d < 0\n", address, _size, address); return 0; - if(!_data) + } + if (!_data) { + DEBUGV("EEPROMClass::read without ::begin\n"); return 0; + } return _data[address]; } void EEPROMClass::write(int const address, uint8_t const value) { - if (address < 0 || (size_t)address >= _size) + if (address < 0 || (size_t)address >= _size) { + DEBUGV("EEPROMClass::write error, address %d > %d or %d < 0\n", address, _size, address); return; - if(!_data) + } + if(!_data) { + DEBUGV("EEPROMClass::read without ::begin\n"); return; + } // Optimise _dirty. Only flagged if data written is different. uint8_t* pData = &_data[address]; @@ -112,7 +125,6 @@ void EEPROMClass::write(int const address, uint8_t const value) { } bool EEPROMClass::commit() { - bool ret = false; if (!_size) return false; if(!_dirty) @@ -120,16 +132,15 @@ bool EEPROMClass::commit() { if(!_data) return false; - noInterrupts(); - if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) { - if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size) == SPI_FLASH_RESULT_OK) { + if (ESP.flashEraseSector(_sector)) { + if (ESP.flashWrite(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size)) { _dirty = false; - ret = true; + return true; } } - interrupts(); - return ret; + DEBUGV("EEPROMClass::commit failed\n"); + return false; } uint8_t * EEPROMClass::getDataPtr() { diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index c25a3463f..5ffbe8240 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -14,7 +14,7 @@ byte value; void setup() { // initialize serial and wait for port to open: - Serial.begin(9600); + Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino index 7bfccd25a..8b48a186a 100644 --- a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -13,6 +13,7 @@ int addr = 0; void setup() { + Serial.begin(115200); EEPROM.begin(512); } @@ -33,7 +34,11 @@ void loop() { addr = addr + 1; if (addr == 512) { addr = 0; - EEPROM.commit(); + if (EEPROM.commit()) { + Serial.println("EEPROM successfully committed"); + } else { + Serial.println("ERROR! EEPROM commit failed"); + } } delay(100); diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 6503ea839..a05b9dd6f 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -113,8 +113,8 @@ protected: * constructor */ HTTPClient::HTTPClient() + : _client(nullptr), _userAgent(F("ESP8266HTTPClient")) { - _client = nullptr; #if HTTPCLIENT_1_1_COMPATIBLE _tcpDeprecated.reset(nullptr); #endif @@ -536,8 +536,8 @@ void HTTPClient::setTimeout(uint16_t timeout) bool HTTPClient::setURL(String url) { // if the new location is only a path then only update the URI - if (_location.startsWith("/")) { - _uri = _location; + if (url && url[0] == '/') { + _uri = url; clear(); return true; } @@ -1294,21 +1294,21 @@ int HTTPClient::handleHeaderResponse() String headerValue = headerLine.substring(headerLine.indexOf(':') + 1); headerValue.trim(); - if(headerName.equalsIgnoreCase("Content-Length")) { + if(headerName.equalsIgnoreCase(F("Content-Length"))) { _size = headerValue.toInt(); } - if(_canReuse && headerName.equalsIgnoreCase("Connection")) { + if(_canReuse && headerName.equalsIgnoreCase(F("Connection"))) { if(headerValue.indexOf("close") >= 0 && headerValue.indexOf("keep-alive") < 0) { _canReuse = false; } } - if(headerName.equalsIgnoreCase("Transfer-Encoding")) { + if(headerName.equalsIgnoreCase(F("Transfer-Encoding"))) { transferEncoding = headerValue; } - if(headerName.equalsIgnoreCase("Location")) { + if(headerName.equalsIgnoreCase(F("Location"))) { _location = headerValue; } @@ -1334,7 +1334,7 @@ int HTTPClient::handleHeaderResponse() if(transferEncoding.length() > 0) { DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] Transfer-Encoding: %s\n", transferEncoding.c_str()); - if(transferEncoding.equalsIgnoreCase("chunked")) { + if(transferEncoding.equalsIgnoreCase(F("chunked"))) { _transferEncoding = HTTPC_TE_CHUNKED; } else { return HTTPC_ERROR_ENCODING; diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 189070a98..d6e4f88c0 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -242,7 +242,7 @@ protected: String _uri; String _protocol; String _headers; - String _userAgent = "ESP8266HTTPClient"; + String _userAgent; String _base64Authorization; /// Response handling diff --git a/libraries/ESP8266HTTPUpdateServer/library.properties b/libraries/ESP8266HTTPUpdateServer/library.properties index b52b2e26f..0505141f9 100644 --- a/libraries/ESP8266HTTPUpdateServer/library.properties +++ b/libraries/ESP8266HTTPUpdateServer/library.properties @@ -1,6 +1,6 @@ name=ESP8266HTTPUpdateServer version=1.0 -author=Ivan Grokhotkov, Miguel Ángel Ajo +author=Ivan Grokhotkov, Miguel Angel Ajo maintainer=Ivan Grokhtkov sentence=Simple HTTP Update server based on the ESP8266WebServer paragraph=The library accepts HTTP post requests to the /update url, and updates the ESP8266 firmware. diff --git a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h index b543073f4..6761177a0 100644 --- a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h +++ b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include "StreamString.h" #include "ESP8266HTTPUpdateServer.h" @@ -10,11 +13,25 @@ namespace esp8266httpupdateserver { using namespace esp8266webserver; static const char serverIndex[] PROGMEM = - R"(
- - -
- )"; + R"( + + + + + + +
+ Firmware:
+ + +
+
+ FileSystem:
+ + +
+ + )"; static const char successResponse[] PROGMEM = "Update Success! Rebooting..."; @@ -75,9 +92,18 @@ void ESP8266HTTPUpdateServerTemplate::setup(ESP8266WebServerTemplate WiFiUDP::stopAll(); if (_serial_output) Serial.printf("Update: %s\n", upload.filename.c_str()); - uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; - if(!Update.begin(maxSketchSpace)){//start with max available size - _setUpdaterError(); + if (upload.name == "filesystem") { + size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start); + SPIFFS.end(); + LittleFS.end(); + if (!Update.begin(fsSize, U_FS)){//start with max available size + if (_serial_output) Update.printError(Serial); + } + } else { + uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; + if (!Update.begin(maxSketchSpace, U_FLASH)){//start with max available size + _setUpdaterError(); + } } } else if(_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length()){ if (_serial_output) Serial.printf("."); diff --git a/libraries/ESP8266WebServer/src/Parsing-impl.h b/libraries/ESP8266WebServer/src/Parsing-impl.h index c6b8ea16e..decc9c8e0 100644 --- a/libraries/ESP8266WebServer/src/Parsing-impl.h +++ b/libraries/ESP8266WebServer/src/Parsing-impl.h @@ -238,7 +238,7 @@ bool ESP8266WebServerTemplate::_parseRequest(ClientType& client) { DEBUG_OUTPUT.println(headerValue); #endif - if (headerName.equalsIgnoreCase("Host")){ + if (headerName.equalsIgnoreCase(F("Host"))){ _hostHeader = headerValue; } } diff --git a/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino b/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino index 931afe938..5ba217b23 100644 --- a/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino +++ b/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino @@ -85,8 +85,9 @@ void setup() { Serial.swap(); // Hardware serial is now on RX:GPIO13 TX:GPIO15 // use SoftwareSerial on regular RX(3)/TX(1) for logging - logger = new SoftwareSerial(3, 1); - logger->begin(BAUD_LOGGER); + logger = new SoftwareSerial(); + logger->begin(BAUD_LOGGER, 3, 1); + logger->enableIntTx(false); logger->println("\n\nUsing SoftwareSerial for logging"); #else logger->begin(BAUD_LOGGER); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 146c968a5..8f63a97fa 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -49,24 +49,24 @@ extern "C" { * @param p Print interface */ void ESP8266WiFiClass::printDiag(Print& p) { - const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; - p.print("Mode: "); + const char* const modes[] = { "NULL", "STA", "AP", "STA+AP" }; + p.print(F("Mode: ")); p.println(modes[wifi_get_opmode()]); - const char* phymodes[] = { "", "B", "G", "N" }; - p.print("PHY mode: "); + const char* const phymodes[] = { "", "B", "G", "N" }; + p.print(F("PHY mode: ")); p.println(phymodes[(int) wifi_get_phy_mode()]); - p.print("Channel: "); + p.print(F("Channel: ")); p.println(wifi_get_channel()); - p.print("AP id: "); + p.print(F("AP id: ")); p.println(wifi_station_get_current_ap_id()); - p.print("Status: "); + p.print(F("Status: ")); p.println(wifi_station_get_connect_status()); - p.print("Auto connect: "); + p.print(F("Auto connect: ")); p.println(wifi_station_get_auto_connect()); struct station_config conf; @@ -75,22 +75,14 @@ void ESP8266WiFiClass::printDiag(Print& p) { char ssid[33]; //ssid can be up to 32chars, => plus null term memcpy(ssid, conf.ssid, sizeof(conf.ssid)); ssid[32] = 0; //nullterm in case of 32 char ssid - - p.print("SSID ("); - p.print(strlen(ssid)); - p.print("): "); - p.println(ssid); + p.printf_P(PSTR("SSID (%d): %s\n"), strlen(ssid), ssid); char passphrase[65]; memcpy(passphrase, conf.password, sizeof(conf.password)); passphrase[64] = 0; + p.printf_P(PSTR("Passphrase (%d): %s\n"), strlen(passphrase), passphrase); - p.print("Passphrase ("); - p.print(strlen(passphrase)); - p.print("): "); - p.println(passphrase); - - p.print("BSSID set: "); + p.print(F("BSSID set: ")); p.println(conf.bssid_set); } diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index d3029c453..8787562d6 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -43,7 +43,7 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { } -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) { @@ -94,7 +94,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(WiFiClient& client, const String& url return handleUpdate(http, currentVersion, false); } -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; @@ -133,7 +133,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(WiFiClient& client, const Strin return handleUpdate(http, currentVersion, true); } -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) { @@ -263,6 +263,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& http.setTimeout(_httpClientTimeout); http.setFollowRedirects(_followRedirects); http.setUserAgent(F("ESP8266-http-Update")); + http.addHeader(F("x-ESP8266-Chip-ID"), String(ESP.getChipId())); http.addHeader(F("x-ESP8266-STA-MAC"), WiFi.macAddress()); http.addHeader(F("x-ESP8266-AP-MAC"), WiFi.softAPmacAddress()); http.addHeader(F("x-ESP8266-free-space"), String(ESP.getFreeSketchSpace())); @@ -388,7 +389,11 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); http.end(); +#ifdef ATOMIC_FS_UPDATE + if(_rebootOnUpdate) { +#else if(_rebootOnUpdate && !spiffs) { +#endif ESP.restart(); } diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index a61e071f7..14c0b6552 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -26,14 +26,16 @@ #ifndef ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_ -#define HTTPUPDATE_1_2_COMPATIBLE - #include #include #include #include #include +#ifndef HTTPUPDATE_1_2_COMPATIBLE +#define HTTPUPDATE_1_2_COMPATIBLE HTTPCLIENT_1_1_COMPATIBLE +#endif + #ifdef DEBUG_ESP_HTTP_UPDATE #ifdef DEBUG_ESP_PORT #define DEBUG_HTTP_UPDATE(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ ) @@ -85,7 +87,7 @@ public: _ledOn = ledOn; } -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE // This function is deprecated, use rebootOnUpdate and the next one instead t_httpUpdate_return update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); @@ -97,7 +99,7 @@ public: #endif t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = ""); -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE // This function is deprecated, use one of the overloads below along with rebootOnUpdate t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); @@ -112,7 +114,7 @@ public: t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/", const String& currentVersion = ""); -#ifdef HTTPUPDATE_1_2_COMPATIBLE +#if HTTPUPDATE_1_2_COMPATIBLE // This function is deprecated, use rebootOnUpdate and the next one instead t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); diff --git a/libraries/ESP8266mDNS/src/ESP8266mDNS.cpp b/libraries/ESP8266mDNS/src/ESP8266mDNS.cpp index 62d54a7ed..c784a0d79 100644 --- a/libraries/ESP8266mDNS/src/ESP8266mDNS.cpp +++ b/libraries/ESP8266mDNS/src/ESP8266mDNS.cpp @@ -1,10 +1,10 @@ #include /* - * MDNS responder global instance - * - * Class type that is instantiated depends on the type mapping in ESP8266mDNS.h - */ + MDNS responder global instance + + Class type that is instantiated depends on the type mapping in ESP8266mDNS.h +*/ #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS) MDNSResponder MDNS; #endif diff --git a/libraries/ESP8266mDNS/src/ESP8266mDNS.h b/libraries/ESP8266mDNS/src/ESP8266mDNS.h index 2987f655c..66d40b1b2 100644 --- a/libraries/ESP8266mDNS/src/ESP8266mDNS.h +++ b/libraries/ESP8266mDNS/src/ESP8266mDNS.h @@ -1,44 +1,44 @@ /* - ESP8266mDNS.h - mDNSResponder for ESP8266 family - This file is part of the esp8266 core for Arduino environment. + ESP8266mDNS.h - mDNSResponder for ESP8266 family + This file is part of the esp8266 core for Arduino environment. - Legacy_ESP8266mDNS: - The well known, thouroughly tested (yet no flawless) default mDNS library for the ESP8266 family + Legacy_ESP8266mDNS: + The well known, thouroughly tested (yet no flawless) default mDNS library for the ESP8266 family - LEA_ESP8266mDNS: - An (currently) experimental mDNS implementation, that supports a lot more of mDNS features than Legacy_ESP8266mDNS, like: - - Presenting a DNS-SD service to interested observers, eg. a http server by presenting _http._tcp service - - Support for multi-level compressed names in input; in output only a very simple one-leven full-name compression is implemented - - Probing host and service domains for uniqueness in the local network - - Tiebreaking while probing is supportet in a very minimalistic way (the 'higher' IP address wins the tiebreak) - - Announcing available services after successful probing - - Using fixed service TXT items or - - Using dynamic service TXT items for presented services (via callback) - - Remove services (and un-announcing them to the observers by sending goodbye-messages) - - Static queries for DNS-SD services (creating a fixed answer set after a certain timeout period) - - Dynamic queries for DNS-SD services with cached and updated answers and user notifications - - Support for multi-homed client host domains + LEA_ESP8266mDNS: + An (currently) experimental mDNS implementation, that supports a lot more of mDNS features than Legacy_ESP8266mDNS, like: + - Presenting a DNS-SD service to interested observers, eg. a http server by presenting _http._tcp service + - Support for multi-level compressed names in input; in output only a very simple one-leven full-name compression is implemented + - Probing host and service domains for uniqueness in the local network + - Tiebreaking while probing is supportet in a very minimalistic way (the 'higher' IP address wins the tiebreak) + - Announcing available services after successful probing + - Using fixed service TXT items or + - Using dynamic service TXT items for presented services (via callback) + - Remove services (and un-announcing them to the observers by sending goodbye-messages) + - Static queries for DNS-SD services (creating a fixed answer set after a certain timeout period) + - Dynamic queries for DNS-SD services with cached and updated answers and user notifications + - Support for multi-homed client host domains - See 'LEA_ESP8266mDNS/EPS8266mDNS.h' for more implementation details and usage informations. - See 'examples/mDNS_Clock' and 'examples/mDNS_ServiceMonitor' for implementation examples of the advanced features. + See 'LEA_ESP8266mDNS/EPS8266mDNS.h' for more implementation details and usage informations. + See 'examples/mDNS_Clock' and 'examples/mDNS_ServiceMonitor' for implementation examples of the advanced features. - LEA_ESP8266mDNS is (mostly) client source code compatible to 'Legacy_ESP8266mDNS', so it could be - use as a 'drop-in' replacement in existing projects. + LEA_ESP8266mDNS is (mostly) client source code compatible to 'Legacy_ESP8266mDNS', so it could be + use as a 'drop-in' replacement in existing projects. - 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 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. + 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 + 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 */ @@ -47,10 +47,10 @@ #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS) - // Maps the implementation to use to the global namespace type - //using MDNSResponder = Legacy_MDNSResponder::MDNSResponder; //legacy - using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new - - extern MDNSResponder MDNS; +// Maps the implementation to use to the global namespace type +//using MDNSResponder = Legacy_MDNSResponder::MDNSResponder; //legacy +using MDNSResponder = esp8266::MDNSImplementation::MDNSResponder; //new + +extern MDNSResponder MDNS; #endif diff --git a/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.cpp b/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.cpp index 80f55ad81..af98b46b6 100644 --- a/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.cpp +++ b/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.cpp @@ -1,31 +1,31 @@ /* -ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) -Version 1.1 -Copyright (c) 2013 Tony DiCola (tony@tonydicola.com) -ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com) -MDNS-SD Suport 2015 Hristo Gochkov -Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com) + ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) + Version 1.1 + Copyright (c) 2013 Tony DiCola (tony@tonydicola.com) + ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com) + MDNS-SD Suport 2015 Hristo Gochkov + Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com) -License (MIT license): - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. */ @@ -44,9 +44,9 @@ License (MIT license): #include "debug.h" extern "C" { - #include "osapi.h" - #include "ets_sys.h" - #include "user_interface.h" +#include "osapi.h" +#include "ets_sys.h" +#include "user_interface.h" } #include "WiFiUdp.h" @@ -59,7 +59,8 @@ extern "C" { -namespace Legacy_MDNSResponder { +namespace Legacy_MDNSResponder +{ #ifdef DEBUG_ESP_MDNS @@ -94,1189 +95,1421 @@ static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251); static const int MDNS_MULTICAST_TTL = 1; static const int MDNS_PORT = 5353; -struct MDNSService { - MDNSService* _next; - char _name[32]; - char _proto[4]; - uint16_t _port; - uint16_t _txtLen; // length of all txts - struct MDNSTxt * _txts; +struct MDNSService +{ + MDNSService* _next; + char _name[32]; + char _proto[4]; + uint16_t _port; + uint16_t _txtLen; // length of all txts + struct MDNSTxt * _txts; }; -struct MDNSTxt{ - MDNSTxt * _next; - String _txt; +struct MDNSTxt +{ + MDNSTxt * _next; + String _txt; }; -struct MDNSAnswer { - MDNSAnswer* next; - uint8_t ip[4]; - uint16_t port; - char *hostname; +struct MDNSAnswer +{ + MDNSAnswer* next; + uint8_t ip[4]; + uint16_t port; + char *hostname; }; -struct MDNSQuery { - char _service[32]; - char _proto[4]; +struct MDNSQuery +{ + char _service[32]; + char _proto[4]; }; -MDNSResponder::MDNSResponder() : _conn(0) { - _services = 0; - _instanceName = ""; - _answers = 0; - _query = 0; - _newQuery = false; - _waitingForAnswers = false; -} -MDNSResponder::~MDNSResponder() { - if (_query != 0) { - os_free(_query); +MDNSResponder::MDNSResponder() : _conn(0) +{ + _services = 0; + _instanceName = ""; + _answers = 0; _query = 0; - } - - // Clear answer list - MDNSAnswer *answer; - int numAnswers = _getNumAnswers(); - for (int n = numAnswers - 1; n >= 0; n--) { - answer = _getAnswerFromIdx(n); - os_free(answer->hostname); - os_free(answer); - answer = 0; - } - _answers = 0; - - if (_conn) { - _conn->unref(); - } + _newQuery = false; + _waitingForAnswers = false; } - -bool MDNSResponder::begin(const char* hostname){ - size_t n = strlen(hostname); - if (n > 63) { // max size for a single label. - return false; - } - - // Copy in hostname characters as lowercase - _hostName = hostname; - _hostName.toLowerCase(); - - // If instance name is not already set copy hostname to instance name - if (_instanceName.equals("") ) _instanceName=hostname; - - _gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){ - (void) event; - _restart(); - }); - - _disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) { - (void) event; - _restart(); - }); - - return _listen(); -} - -void MDNSResponder::notifyAPChange() { - _restart(); -} - -void MDNSResponder::_restart() { - if (_conn) { - _conn->unref(); - _conn = nullptr; - } - _listen(); -} - -bool MDNSResponder::_listen() { - // Open the MDNS socket if it isn't already open. - if (!_conn) { - #ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.println("MDNS listening"); - #endif - - IPAddress mdns(MDNS_MULTICAST_ADDR); - - if (igmp_joingroup(IP4_ADDR_ANY4, mdns)!= ERR_OK) { - return false; +MDNSResponder::~MDNSResponder() +{ + if (_query != 0) + { + os_free(_query); + _query = 0; } - _conn = new UdpContext; - _conn->ref(); - - if (!_conn->listen(IP_ADDR_ANY, MDNS_PORT)) { - return false; - } - _conn->setMulticastTTL(MDNS_MULTICAST_TTL); - _conn->onRx(std::bind(&MDNSResponder::update, this)); - _conn->connect(mdns, MDNS_PORT); - } - return true; -} - -void MDNSResponder::update() { - if (!_conn || !_conn->next()) - return; - _parsePacket(); -} - - -void MDNSResponder::setInstanceName(String name){ - if (name.length() > 63) - return; - _instanceName = name; -} - - -bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){ - MDNSService* servicePtr; - - uint8_t txtLen = os_strlen(key) + os_strlen(value) + 1; // Add one for equals sign - txtLen += 1; //accounts for length byte added when building the txt responce - //Find the service - for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) { - //Checking Service names - if(strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0) { - //found a service name match - if (servicePtr->_txtLen + txtLen > 1300) - return false; //max txt record size - MDNSTxt *newtxt = new MDNSTxt; - newtxt->_txt = String(key) + "=" + String(value); - newtxt->_next = 0; - if(servicePtr->_txts == 0) { //no services have been added - //Adding First TXT to service - servicePtr->_txts = newtxt; - servicePtr->_txtLen += txtLen; - return true; - } else { - MDNSTxt * txtPtr = servicePtr->_txts; - while(txtPtr->_next != 0) { - txtPtr = txtPtr->_next; - } - //adding another TXT to service - txtPtr->_next = newtxt; - servicePtr->_txtLen += txtLen; - return true; - } - } - } - return false; -} - -void MDNSResponder::addService(char *name, char *proto, uint16_t port){ - if(_getServicePort(name, proto) != 0) - return; - if(os_strlen(name) > 32 || os_strlen(proto) != 3) - return; //bad arguments - struct MDNSService *srv = (struct MDNSService*)(os_malloc(sizeof(struct MDNSService))); - os_strcpy(srv->_name, name); - os_strcpy(srv->_proto, proto); - srv->_port = port; - srv->_next = 0; - srv->_txts = 0; - srv->_txtLen = 0; - - if(_services == 0) { - _services = srv; - } else { - MDNSService* servicePtr = _services; - while(servicePtr->_next != 0) - servicePtr = servicePtr->_next; - servicePtr->_next = srv; - } - -} - -int MDNSResponder::queryService(char *service, char *proto) { -#ifdef DEBUG_ESP_MDNS_TX - DEBUG_ESP_PORT.printf("queryService %s %s\n", service, proto); -#endif - while(_answers!=0){ - MDNSAnswer *currAnswer = _answers; - _answers = _answers->next; - os_free(currAnswer->hostname); - os_free(currAnswer); - currAnswer = 0; - } - if (_query != 0) { - os_free(_query); - _query = 0; - } - _query = (struct MDNSQuery*)(os_malloc(sizeof(struct MDNSQuery))); - os_strcpy(_query->_service, service); - os_strcpy(_query->_proto, proto); - _newQuery = true; - - char underscore[] = "_"; - - // build service name with _ - char serviceName[os_strlen(service) + 2]; - os_strcpy(serviceName, underscore); - os_strcat(serviceName, service); - size_t serviceNameLen = os_strlen(serviceName); - - //build proto name with _ - char protoName[5]; - os_strcpy(protoName, underscore); - os_strcat(protoName, proto); - size_t protoNameLen = 4; - - //local string - char localName[] = "local"; - size_t localNameLen = 5; - - //terminator - char terminator[] = "\0"; - - // Only supports sending one PTR query - uint8_t questionCount = 1; - - _waitingForAnswers = true; - for (int itfn = 0; itfn < 2; itfn++) { - struct ip_info ip_info; - - wifi_get_ip_info((!itfn) ? SOFTAP_IF : STATION_IF, &ip_info); - if (!ip_info.ip.addr) - continue; - _conn->setMulticastInterface(IPAddress(ip_info.ip.addr)); - - // Write the header - _conn->flush(); - uint8_t head[12] = { - 0x00, 0x00, //ID = 0 - 0x00, 0x00, //Flags = response + authoritative answer - 0x00, questionCount, //Question count - 0x00, 0x00, //Answer count - 0x00, 0x00, //Name server records - 0x00, 0x00 //Additional records - }; - _conn->append(reinterpret_cast(head), 12); - - // Only supports sending one PTR query - // Send the Name field (eg. "_http._tcp.local") - _conn->append(reinterpret_cast(&serviceNameLen), 1); // lenght of "_" + service - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_" + service - _conn->append(reinterpret_cast(&protoNameLen), 1); // lenght of "_" + proto - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_" + proto - _conn->append(reinterpret_cast(&localNameLen), 1); // lenght of "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator - - //Send the type and class - uint8_t ptrAttrs[4] = { - 0x00, 0x0c, //PTR record query - 0x00, 0x01 //Class IN - }; - _conn->append(reinterpret_cast(ptrAttrs), 4); - _conn->send(); - } - -#ifdef DEBUG_ESP_MDNS_TX - DEBUG_ESP_PORT.println("Waiting for answers.."); -#endif - delay(1000); - - _waitingForAnswers = false; - - return _getNumAnswers(); -} - -String MDNSResponder::hostname(int idx) { - MDNSAnswer *answer = _getAnswerFromIdx(idx); - if (answer == 0) { - return String(); - } - return answer->hostname; -} - -IPAddress MDNSResponder::IP(int idx) { - MDNSAnswer *answer = _getAnswerFromIdx(idx); - if (answer == 0) { - return IPAddress(); - } - return IPAddress(answer->ip); -} - -uint16_t MDNSResponder::port(int idx) { - MDNSAnswer *answer = _getAnswerFromIdx(idx); - if (answer == 0) { - return 0; - } - return answer->port; -} - -MDNSAnswer* MDNSResponder::_getAnswerFromIdx(int idx) { - MDNSAnswer *answer = _answers; - while (answer != 0 && idx-- > 0) { - answer = answer->next; - } - if (idx > 0) { - return 0; - } - return answer; -} - -int MDNSResponder::_getNumAnswers() { - int numAnswers = 0; - MDNSAnswer *answer = _answers; - while (answer != 0) { - numAnswers++; - answer = answer->next; - } - return numAnswers; -} - -MDNSTxt * MDNSResponder::_getServiceTxt(char *name, char *proto){ - MDNSService* servicePtr; - for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) { - if(servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){ - if (servicePtr->_txts == 0) - return nullptr; - return servicePtr->_txts; - } - } - return nullptr; -} - -uint16_t MDNSResponder::_getServiceTxtLen(char *name, char *proto){ - MDNSService* servicePtr; - for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) { - if(servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){ - if (servicePtr->_txts == 0) - return false; - return servicePtr->_txtLen; - } - } - return 0; -} - -uint16_t MDNSResponder::_getServicePort(char *name, char *proto){ - MDNSService* servicePtr; - for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) { - if(servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){ - return servicePtr->_port; - } - } - return 0; -} - -IPAddress MDNSResponder::_getRequestMulticastInterface(){ - struct ip_info ip_info; - bool match_ap = false; - if (wifi_get_opmode() & SOFTAP_MODE) { - const IPAddress& remote_ip = _conn->getRemoteAddress(); - wifi_get_ip_info(SOFTAP_IF, &ip_info); - IPAddress infoIp(ip_info.ip); - IPAddress infoMask(ip_info.netmask); - if (ip_info.ip.addr && ip_addr_netcmp((const ip_addr_t*)remote_ip, (const ip_addr_t*)infoIp, ip_2_ip4((const ip_addr_t*)infoMask))) - match_ap = true; - } - if (!match_ap) - wifi_get_ip_info(STATION_IF, &ip_info); - return IPAddress(ip_info.ip.addr); -} - -void MDNSResponder::_parsePacket(){ - int i; - char tmp; - bool serviceParsed = false; - bool protoParsed = false; - bool localParsed = false; - - char hostName[255]; - uint8_t hostNameLen; - - char serviceName[32]; - uint8_t serviceNameLen; - uint16_t servicePort = 0; - - char protoName[32]; - protoName[0] = 0; - uint8_t protoNameLen = 0; - - uint16_t packetHeader[6]; - - for(i=0; i<6; i++) - packetHeader[i] = _conn_read16(); - - if ((packetHeader[1] & 0x8000) != 0) { // Read answers -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("Reading answers RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n", packetHeader[0], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5]); -#endif - - if (!_waitingForAnswers) { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.println("Not expecting any answers right now, returning"); -#endif - _conn->flush(); - return; - } - - int numAnswers = packetHeader[3] + packetHeader[5]; - // Assume that the PTR answer always comes first and that it is always accompanied by a TXT, SRV, AAAA (optional) and A answer in the same packet. - if (numAnswers < 4) { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("Expected a packet with 4 or more answers, got %u\n", numAnswers); -#endif - _conn->flush(); - return; - } - - uint8_t tmp8; - uint16_t answerPort = 0; - uint8_t answerIp[4] = { 0,0,0,0 }; - char answerHostName[255]; - bool serviceMatch = false; - MDNSAnswer *answer; - uint8_t partsCollected = 0; - uint8_t stringsRead = 0; - - answerHostName[0] = '\0'; - // Clear answer list - if (_newQuery) { - int oldAnswers = _getNumAnswers(); - for (int n = oldAnswers - 1; n >= 0; n--) { + MDNSAnswer *answer; + int numAnswers = _getNumAnswers(); + for (int n = numAnswers - 1; n >= 0; n--) + { answer = _getAnswerFromIdx(n); os_free(answer->hostname); os_free(answer); answer = 0; - } - _answers = 0; - _newQuery = false; + } + _answers = 0; + + if (_conn) + { + _conn->unref(); + } +} + +bool MDNSResponder::begin(const char* hostname) +{ + size_t n = strlen(hostname); + if (n > 63) // max size for a single label. + { + return false; } - while (numAnswers--) { - // Read name - stringsRead = 0; - size_t last_bufferpos = 0; - do { - tmp8 = _conn_read8(); - if (tmp8 == 0x00) { // End of name - break; - } - if (tmp8 & 0xC0) { // Compressed pointer - uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8(); - if (_conn->isValidOffset(offset)) { - if (0 == last_bufferpos) - last_bufferpos = _conn->tell(); -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Compressed pointer, jumping from "); - DEBUG_ESP_PORT.print(last_bufferpos); - DEBUG_ESP_PORT.print(" to "); - DEBUG_ESP_PORT.println(offset); -#endif - _conn->seek(offset); - tmp8 = _conn_read8(); - } - else { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Skipping malformed compressed pointer"); -#endif - tmp8 = _conn_read8(); - break; - } - } - if(stringsRead > 3){ -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.println("failed to read the response name"); -#endif - _conn->flush(); - return; - } - _conn_readS(serviceName, tmp8); - serviceName[tmp8] = '\0'; -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf(" %d ", tmp8); - for (int n = 0; n < tmp8; n++) { - DEBUG_ESP_PORT.printf("%c", serviceName[n]); - } - DEBUG_ESP_PORT.println(); -#endif - if (serviceName[0] == '_') { - if (strcmp(&serviceName[1], _query->_service) == 0) { - serviceMatch = true; -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("found matching service: %s\n", _query->_service); -#endif - } - } - stringsRead++; - } while (true); - if (last_bufferpos > 0) - { - _conn->seek(last_bufferpos); -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Compressed pointer, jumping back to "); - DEBUG_ESP_PORT.println(last_bufferpos); -#endif - } + // Copy in hostname characters as lowercase + _hostName = hostname; + _hostName.toLowerCase(); - uint16_t answerType = _conn_read16(); // Read type - uint16_t answerClass = _conn_read16(); // Read class - uint32_t answerTtl = _conn_read32(); // Read ttl - uint16_t answerRdlength = _conn_read16(); // Read rdlength + // If instance name is not already set copy hostname to instance name + if (_instanceName.equals("")) + { + _instanceName = hostname; + } - (void) answerClass; - (void) answerTtl; + _gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP & event) + { + (void) event; + _restart(); + }); - if(answerRdlength > 255){ - if(answerType == MDNS_TYPE_TXT && answerRdlength < 1460){ - while(--answerRdlength) _conn->read(); - } else { + _disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected & event) + { + (void) event; + _restart(); + }); + + return _listen(); +} + +void MDNSResponder::notifyAPChange() +{ + _restart(); +} + +void MDNSResponder::_restart() +{ + if (_conn) + { + _conn->unref(); + _conn = nullptr; + } + _listen(); +} + +bool MDNSResponder::_listen() +{ + // Open the MDNS socket if it isn't already open. + if (!_conn) + { #ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("Data len too long! %u\n", answerRdlength); -#endif - _conn->flush(); - return; - } - } - -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("type: %04x rdlength: %d\n", answerType, answerRdlength); + DEBUG_ESP_PORT.println("MDNS listening"); #endif - if (answerType == MDNS_TYPE_PTR) { - partsCollected |= 0x01; - _conn_readS(hostName, answerRdlength); // Read rdata - if(hostName[answerRdlength-2] & 0xc0){ - memcpy(answerHostName, hostName+1, answerRdlength-3); - answerHostName[answerRdlength-3] = '\0'; - } -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("PTR %d ", answerRdlength); - for (int n = 0; n < answerRdlength; n++) { - DEBUG_ESP_PORT.printf("%c", hostName[n]); - } - DEBUG_ESP_PORT.println(); -#endif - } + IPAddress mdns(MDNS_MULTICAST_ADDR); - else if (answerType == MDNS_TYPE_TXT) { - partsCollected |= 0x02; - _conn_readS(hostName, answerRdlength); // Read rdata -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("TXT %d ", answerRdlength); - for (int n = 0; n < answerRdlength; n++) { - DEBUG_ESP_PORT.printf("%c", hostName[n]); - } - DEBUG_ESP_PORT.println(); -#endif - } - - else if (answerType == MDNS_TYPE_SRV) { - partsCollected |= 0x04; - uint16_t answerPrio = _conn_read16(); // Read priority - uint16_t answerWeight = _conn_read16(); // Read weight - answerPort = _conn_read16(); // Read port - last_bufferpos = 0; - - (void) answerPrio; - (void) answerWeight; - - // Read hostname - tmp8 = _conn_read8(); - if (tmp8 & 0xC0) { // Compressed pointer - uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8(); - if (_conn->isValidOffset(offset)) { - last_bufferpos = _conn->tell(); -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Compressed pointer, jumping from "); - DEBUG_ESP_PORT.print(last_bufferpos); - DEBUG_ESP_PORT.print(" to "); - DEBUG_ESP_PORT.println(offset); -#endif - _conn->seek(offset); - tmp8 = _conn_read8(); - } - else { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Skipping malformed compressed pointer"); -#endif - tmp8 = _conn_read8(); - break; - } - } - _conn_readS(answerHostName, tmp8); - answerHostName[tmp8] = '\0'; -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("SRV %d ", tmp8); - for (int n = 0; n < tmp8; n++) { - DEBUG_ESP_PORT.printf("%02x ", answerHostName[n]); - } - DEBUG_ESP_PORT.printf("\n%s\n", answerHostName); -#endif - if (last_bufferpos > 0) + if (igmp_joingroup(IP4_ADDR_ANY4, mdns) != ERR_OK) { - _conn->seek(last_bufferpos); - tmp8 = 2; // Size of compression octets -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.print("Compressed pointer, jumping back to "); - DEBUG_ESP_PORT.println(last_bufferpos); -#endif + return false; } - if (answerRdlength - (6 + 1 + tmp8) > 0) { // Skip any remaining rdata - _conn_readS(hostName, answerRdlength - (6 + 1 + tmp8)); - } - } - else if (answerType == MDNS_TYPE_A) { - partsCollected |= 0x08; - for (int i = 0; i < 4; i++) { - answerIp[i] = _conn_read8(); - } - } - else { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("Ignoring unsupported type %02x\n", tmp8); -#endif - for (int n = 0; n < answerRdlength; n++) - (void)_conn_read8(); - } + _conn = new UdpContext; + _conn->ref(); - if ((partsCollected == 0x0F) && serviceMatch) { -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.println("All answers parsed, adding to _answers list.."); -#endif - // Add new answer to answer list - if (_answers == 0) { - _answers = (struct MDNSAnswer*)(os_malloc(sizeof(struct MDNSAnswer))); - answer = _answers; + if (!_conn->listen(IP_ADDR_ANY, MDNS_PORT)) + { + return false; } - else { - answer = _answers; - while (answer->next != 0) { - answer = answer->next; - } - answer->next = (struct MDNSAnswer*)(os_malloc(sizeof(struct MDNSAnswer))); - answer = answer->next; - } - answer->next = 0; - answer->hostname = 0; + _conn->setMulticastTTL(MDNS_MULTICAST_TTL); + _conn->onRx(std::bind(&MDNSResponder::update, this)); + _conn->connect(mdns, MDNS_PORT); + } + return true; +} - // Populate new answer - answer->port = answerPort; - for (int i = 0; i < 4; i++) { - answer->ip[i] = answerIp[i]; +void MDNSResponder::update() +{ + if (!_conn || !_conn->next()) + { + return; + } + _parsePacket(); +} + + +void MDNSResponder::setInstanceName(String name) +{ + if (name.length() > 63) + { + return; + } + _instanceName = name; +} + + +bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value) +{ + MDNSService* servicePtr; + + uint8_t txtLen = os_strlen(key) + os_strlen(value) + 1; // Add one for equals sign + txtLen += 1; //accounts for length byte added when building the txt responce + //Find the service + for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) + { + //Checking Service names + if (strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0) + { + //found a service name match + if (servicePtr->_txtLen + txtLen > 1300) + { + return false; //max txt record size + } + MDNSTxt *newtxt = new MDNSTxt; + newtxt->_txt = String(key) + "=" + String(value); + newtxt->_next = 0; + if (servicePtr->_txts == 0) //no services have been added + { + //Adding First TXT to service + servicePtr->_txts = newtxt; + servicePtr->_txtLen += txtLen; + return true; + } + else + { + MDNSTxt * txtPtr = servicePtr->_txts; + while (txtPtr->_next != 0) + { + txtPtr = txtPtr->_next; + } + //adding another TXT to service + txtPtr->_next = newtxt; + servicePtr->_txtLen += txtLen; + return true; + } } - answer->hostname = (char *)os_malloc(strlen(answerHostName) + 1); - os_strcpy(answer->hostname, answerHostName); + } + return false; +} + +void MDNSResponder::addService(char *name, char *proto, uint16_t port) +{ + if (_getServicePort(name, proto) != 0) + { + return; + } + if (os_strlen(name) > 32 || os_strlen(proto) != 3) + { + return; //bad arguments + } + struct MDNSService *srv = (struct MDNSService*)(os_malloc(sizeof(struct MDNSService))); + os_strcpy(srv->_name, name); + os_strcpy(srv->_proto, proto); + srv->_port = port; + srv->_next = 0; + srv->_txts = 0; + srv->_txtLen = 0; + + if (_services == 0) + { + _services = srv; + } + else + { + MDNSService* servicePtr = _services; + while (servicePtr->_next != 0) + { + servicePtr = servicePtr->_next; + } + servicePtr->_next = srv; + } + +} + +int MDNSResponder::queryService(char *service, char *proto) +{ +#ifdef DEBUG_ESP_MDNS_TX + DEBUG_ESP_PORT.printf("queryService %s %s\n", service, proto); +#endif + while (_answers != 0) + { + MDNSAnswer *currAnswer = _answers; + _answers = _answers->next; + os_free(currAnswer->hostname); + os_free(currAnswer); + currAnswer = 0; + } + if (_query != 0) + { + os_free(_query); + _query = 0; + } + _query = (struct MDNSQuery*)(os_malloc(sizeof(struct MDNSQuery))); + os_strcpy(_query->_service, service); + os_strcpy(_query->_proto, proto); + _newQuery = true; + + char underscore[] = "_"; + + // build service name with _ + char serviceName[os_strlen(service) + 2]; + os_strcpy(serviceName, underscore); + os_strcat(serviceName, service); + size_t serviceNameLen = os_strlen(serviceName); + + //build proto name with _ + char protoName[5]; + os_strcpy(protoName, underscore); + os_strcat(protoName, proto); + size_t protoNameLen = 4; + + //local string + char localName[] = "local"; + size_t localNameLen = 5; + + //terminator + char terminator[] = "\0"; + + // Only supports sending one PTR query + uint8_t questionCount = 1; + + _waitingForAnswers = true; + for (int itfn = 0; itfn < 2; itfn++) + { + struct ip_info ip_info; + + wifi_get_ip_info((!itfn) ? SOFTAP_IF : STATION_IF, &ip_info); + if (!ip_info.ip.addr) + { + continue; + } + _conn->setMulticastInterface(IPAddress(ip_info.ip.addr)); + + // Write the header + _conn->flush(); + uint8_t head[12] = + { + 0x00, 0x00, //ID = 0 + 0x00, 0x00, //Flags = response + authoritative answer + 0x00, questionCount, //Question count + 0x00, 0x00, //Answer count + 0x00, 0x00, //Name server records + 0x00, 0x00 //Additional records + }; + _conn->append(reinterpret_cast(head), 12); + + // Only supports sending one PTR query + // Send the Name field (eg. "_http._tcp.local") + _conn->append(reinterpret_cast(&serviceNameLen), 1); // lenght of "_" + service + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_" + service + _conn->append(reinterpret_cast(&protoNameLen), 1); // lenght of "_" + proto + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_" + proto + _conn->append(reinterpret_cast(&localNameLen), 1); // lenght of "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + + //Send the type and class + uint8_t ptrAttrs[4] = + { + 0x00, 0x0c, //PTR record query + 0x00, 0x01 //Class IN + }; + _conn->append(reinterpret_cast(ptrAttrs), 4); + _conn->send(); + } + +#ifdef DEBUG_ESP_MDNS_TX + DEBUG_ESP_PORT.println("Waiting for answers.."); +#endif + delay(1000); + + _waitingForAnswers = false; + + return _getNumAnswers(); +} + +String MDNSResponder::hostname(int idx) +{ + MDNSAnswer *answer = _getAnswerFromIdx(idx); + if (answer == 0) + { + return String(); + } + return answer->hostname; +} + +IPAddress MDNSResponder::IP(int idx) +{ + MDNSAnswer *answer = _getAnswerFromIdx(idx); + if (answer == 0) + { + return IPAddress(); + } + return IPAddress(answer->ip); +} + +uint16_t MDNSResponder::port(int idx) +{ + MDNSAnswer *answer = _getAnswerFromIdx(idx); + if (answer == 0) + { + return 0; + } + return answer->port; +} + +MDNSAnswer* MDNSResponder::_getAnswerFromIdx(int idx) +{ + MDNSAnswer *answer = _answers; + while (answer != 0 && idx-- > 0) + { + answer = answer->next; + } + if (idx > 0) + { + return 0; + } + return answer; +} + +int MDNSResponder::_getNumAnswers() +{ + int numAnswers = 0; + MDNSAnswer *answer = _answers; + while (answer != 0) + { + numAnswers++; + answer = answer->next; + } + return numAnswers; +} + +MDNSTxt * MDNSResponder::_getServiceTxt(char *name, char *proto) +{ + MDNSService* servicePtr; + for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) + { + if (servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0) + { + if (servicePtr->_txts == 0) + { + return nullptr; + } + return servicePtr->_txts; + } + } + return nullptr; +} + +uint16_t MDNSResponder::_getServiceTxtLen(char *name, char *proto) +{ + MDNSService* servicePtr; + for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) + { + if (servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0) + { + if (servicePtr->_txts == 0) + { + return false; + } + return servicePtr->_txtLen; + } + } + return 0; +} + +uint16_t MDNSResponder::_getServicePort(char *name, char *proto) +{ + MDNSService* servicePtr; + for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) + { + if (servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0) + { + return servicePtr->_port; + } + } + return 0; +} + +IPAddress MDNSResponder::_getRequestMulticastInterface() +{ + struct ip_info ip_info; + bool match_ap = false; + if (wifi_get_opmode() & SOFTAP_MODE) + { + const IPAddress& remote_ip = _conn->getRemoteAddress(); + wifi_get_ip_info(SOFTAP_IF, &ip_info); + IPAddress infoIp(ip_info.ip); + IPAddress infoMask(ip_info.netmask); + if (ip_info.ip.addr && ip_addr_netcmp((const ip_addr_t*)remote_ip, (const ip_addr_t*)infoIp, ip_2_ip4((const ip_addr_t*)infoMask))) + { + match_ap = true; + } + } + if (!match_ap) + { + wifi_get_ip_info(STATION_IF, &ip_info); + } + return IPAddress(ip_info.ip.addr); +} + +void MDNSResponder::_parsePacket() +{ + int i; + char tmp; + bool serviceParsed = false; + bool protoParsed = false; + bool localParsed = false; + + char hostName[255]; + uint8_t hostNameLen; + + char serviceName[32]; + uint8_t serviceNameLen; + uint16_t servicePort = 0; + + char protoName[32]; + protoName[0] = 0; + uint8_t protoNameLen = 0; + + uint16_t packetHeader[6]; + + for (i = 0; i < 6; i++) + { + packetHeader[i] = _conn_read16(); + } + + if ((packetHeader[1] & 0x8000) != 0) // Read answers + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("Reading answers RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n", packetHeader[0], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5]); +#endif + + if (!_waitingForAnswers) + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.println("Not expecting any answers right now, returning"); +#endif + _conn->flush(); + return; + } + + int numAnswers = packetHeader[3] + packetHeader[5]; + // Assume that the PTR answer always comes first and that it is always accompanied by a TXT, SRV, AAAA (optional) and A answer in the same packet. + if (numAnswers < 4) + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("Expected a packet with 4 or more answers, got %u\n", numAnswers); +#endif + _conn->flush(); + return; + } + + uint8_t tmp8; + uint16_t answerPort = 0; + uint8_t answerIp[4] = { 0, 0, 0, 0 }; + char answerHostName[255]; + bool serviceMatch = false; + MDNSAnswer *answer; + uint8_t partsCollected = 0; + uint8_t stringsRead = 0; + + answerHostName[0] = '\0'; + + // Clear answer list + if (_newQuery) + { + int oldAnswers = _getNumAnswers(); + for (int n = oldAnswers - 1; n >= 0; n--) + { + answer = _getAnswerFromIdx(n); + os_free(answer->hostname); + os_free(answer); + answer = 0; + } + _answers = 0; + _newQuery = false; + } + + while (numAnswers--) + { + // Read name + stringsRead = 0; + size_t last_bufferpos = 0; + do + { + tmp8 = _conn_read8(); + if (tmp8 == 0x00) // End of name + { + break; + } + if (tmp8 & 0xC0) // Compressed pointer + { + uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8(); + if (_conn->isValidOffset(offset)) + { + if (0 == last_bufferpos) + { + last_bufferpos = _conn->tell(); + } +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Compressed pointer, jumping from "); + DEBUG_ESP_PORT.print(last_bufferpos); + DEBUG_ESP_PORT.print(" to "); + DEBUG_ESP_PORT.println(offset); +#endif + _conn->seek(offset); + tmp8 = _conn_read8(); + } + else + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Skipping malformed compressed pointer"); +#endif + tmp8 = _conn_read8(); + break; + } + } + if (stringsRead > 3) + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.println("failed to read the response name"); +#endif + _conn->flush(); + return; + } + _conn_readS(serviceName, tmp8); + serviceName[tmp8] = '\0'; +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf(" %d ", tmp8); + for (int n = 0; n < tmp8; n++) + { + DEBUG_ESP_PORT.printf("%c", serviceName[n]); + } + DEBUG_ESP_PORT.println(); +#endif + if (serviceName[0] == '_') + { + if (strcmp(&serviceName[1], _query->_service) == 0) + { + serviceMatch = true; +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("found matching service: %s\n", _query->_service); +#endif + } + } + stringsRead++; + } while (true); + if (last_bufferpos > 0) + { + _conn->seek(last_bufferpos); +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Compressed pointer, jumping back to "); + DEBUG_ESP_PORT.println(last_bufferpos); +#endif + } + + uint16_t answerType = _conn_read16(); // Read type + uint16_t answerClass = _conn_read16(); // Read class + uint32_t answerTtl = _conn_read32(); // Read ttl + uint16_t answerRdlength = _conn_read16(); // Read rdlength + + (void) answerClass; + (void) answerTtl; + + if (answerRdlength > 255) + { + if (answerType == MDNS_TYPE_TXT && answerRdlength < 1460) + { + while (--answerRdlength) + { + _conn->read(); + } + } + else + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("Data len too long! %u\n", answerRdlength); +#endif + _conn->flush(); + return; + } + } + +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("type: %04x rdlength: %d\n", answerType, answerRdlength); +#endif + + if (answerType == MDNS_TYPE_PTR) + { + partsCollected |= 0x01; + _conn_readS(hostName, answerRdlength); // Read rdata + if (hostName[answerRdlength - 2] & 0xc0) + { + memcpy(answerHostName, hostName + 1, answerRdlength - 3); + answerHostName[answerRdlength - 3] = '\0'; + } +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("PTR %d ", answerRdlength); + for (int n = 0; n < answerRdlength; n++) + { + DEBUG_ESP_PORT.printf("%c", hostName[n]); + } + DEBUG_ESP_PORT.println(); +#endif + } + + else if (answerType == MDNS_TYPE_TXT) + { + partsCollected |= 0x02; + _conn_readS(hostName, answerRdlength); // Read rdata +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("TXT %d ", answerRdlength); + for (int n = 0; n < answerRdlength; n++) + { + DEBUG_ESP_PORT.printf("%c", hostName[n]); + } + DEBUG_ESP_PORT.println(); +#endif + } + + else if (answerType == MDNS_TYPE_SRV) + { + partsCollected |= 0x04; + uint16_t answerPrio = _conn_read16(); // Read priority + uint16_t answerWeight = _conn_read16(); // Read weight + answerPort = _conn_read16(); // Read port + last_bufferpos = 0; + + (void) answerPrio; + (void) answerWeight; + + // Read hostname + tmp8 = _conn_read8(); + if (tmp8 & 0xC0) // Compressed pointer + { + uint16_t offset = ((((uint16_t)tmp8) & ~0xC0) << 8) | _conn_read8(); + if (_conn->isValidOffset(offset)) + { + last_bufferpos = _conn->tell(); +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Compressed pointer, jumping from "); + DEBUG_ESP_PORT.print(last_bufferpos); + DEBUG_ESP_PORT.print(" to "); + DEBUG_ESP_PORT.println(offset); +#endif + _conn->seek(offset); + tmp8 = _conn_read8(); + } + else + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Skipping malformed compressed pointer"); +#endif + tmp8 = _conn_read8(); + break; + } + } + _conn_readS(answerHostName, tmp8); + answerHostName[tmp8] = '\0'; +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("SRV %d ", tmp8); + for (int n = 0; n < tmp8; n++) + { + DEBUG_ESP_PORT.printf("%02x ", answerHostName[n]); + } + DEBUG_ESP_PORT.printf("\n%s\n", answerHostName); +#endif + if (last_bufferpos > 0) + { + _conn->seek(last_bufferpos); + tmp8 = 2; // Size of compression octets +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.print("Compressed pointer, jumping back to "); + DEBUG_ESP_PORT.println(last_bufferpos); +#endif + } + if (answerRdlength - (6 + 1 + tmp8) > 0) // Skip any remaining rdata + { + _conn_readS(hostName, answerRdlength - (6 + 1 + tmp8)); + } + } + + else if (answerType == MDNS_TYPE_A) + { + partsCollected |= 0x08; + for (int i = 0; i < 4; i++) + { + answerIp[i] = _conn_read8(); + } + } + else + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("Ignoring unsupported type %02x\n", tmp8); +#endif + for (int n = 0; n < answerRdlength; n++) + { + (void)_conn_read8(); + } + } + + if ((partsCollected == 0x0F) && serviceMatch) + { +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.println("All answers parsed, adding to _answers list.."); +#endif + // Add new answer to answer list + if (_answers == 0) + { + _answers = (struct MDNSAnswer*)(os_malloc(sizeof(struct MDNSAnswer))); + answer = _answers; + } + else + { + answer = _answers; + while (answer->next != 0) + { + answer = answer->next; + } + answer->next = (struct MDNSAnswer*)(os_malloc(sizeof(struct MDNSAnswer))); + answer = answer->next; + } + answer->next = 0; + answer->hostname = 0; + + // Populate new answer + answer->port = answerPort; + for (int i = 0; i < 4; i++) + { + answer->ip[i] = answerIp[i]; + } + answer->hostname = (char *)os_malloc(strlen(answerHostName) + 1); + os_strcpy(answer->hostname, answerHostName); + _conn->flush(); + return; + } + } + _conn->flush(); return; - } } - - _conn->flush(); - return; - } - // PARSE REQUEST NAME + // PARSE REQUEST NAME - hostNameLen = _conn_read8() % 255; - _conn_readS(hostName, hostNameLen); - hostName[hostNameLen] = '\0'; + hostNameLen = _conn_read8() % 255; + _conn_readS(hostName, hostNameLen); + hostName[hostNameLen] = '\0'; - if(hostName[0] == '_'){ - serviceParsed = true; - memcpy(serviceName, hostName+1, hostNameLen); - serviceNameLen = hostNameLen-1; - hostNameLen = 0; - } - - if(hostNameLen > 0 && !_hostName.equals(hostName) && !_instanceName.equals(hostName)){ -#ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_NO_HOST: %s\n", hostName); - DEBUG_ESP_PORT.printf("hostname: %s\n", _hostName.c_str() ); - DEBUG_ESP_PORT.printf("instance: %s\n", _instanceName.c_str() ); -#endif - _conn->flush(); - return; - } - - if(!serviceParsed){ - serviceNameLen = _conn_read8() % 255; - _conn_readS(serviceName, serviceNameLen); - serviceName[serviceNameLen] = '\0'; - - if(serviceName[0] == '_'){ - memmove(serviceName, serviceName+1, serviceNameLen); - serviceNameLen--; - serviceParsed = true; - } else if(serviceNameLen == 5 && strcmp("local", serviceName) == 0){ - tmp = _conn_read8(); - if(tmp == 0){ + if (hostName[0] == '_') + { serviceParsed = true; - serviceNameLen = 0; - protoParsed = true; - protoNameLen = 0; - localParsed = true; - } else { + memcpy(serviceName, hostName + 1, hostNameLen); + serviceNameLen = hostNameLen - 1; + hostNameLen = 0; + } + + if (hostNameLen > 0 && !_hostName.equals(hostName) && !_instanceName.equals(hostName)) + { #ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_FQDN: %s\n", serviceName); + DEBUG_ESP_PORT.printf("ERR_NO_HOST: %s\n", hostName); + DEBUG_ESP_PORT.printf("hostname: %s\n", _hostName.c_str()); + DEBUG_ESP_PORT.printf("instance: %s\n", _instanceName.c_str()); #endif _conn->flush(); return; - } - } else { -#ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_SERVICE: %s\n", serviceName); -#endif - _conn->flush(); - return; } - } - if(!protoParsed){ - protoNameLen = _conn_read8() % 255; - _conn_readS(protoName, protoNameLen); - protoName[protoNameLen] = '\0'; - if(protoNameLen == 4 && protoName[0] == '_'){ - memmove(protoName, protoName+1, protoNameLen); - protoNameLen--; - protoParsed = true; - } else if(strcmp("services", serviceName) == 0 && strcmp("_dns-sd", protoName) == 0){ - _conn->flush(); - IPAddress interface = _getRequestMulticastInterface(); - _replyToTypeEnumRequest(interface); - return; - } else { + if (!serviceParsed) + { + serviceNameLen = _conn_read8() % 255; + _conn_readS(serviceName, serviceNameLen); + serviceName[serviceNameLen] = '\0'; + + if (serviceName[0] == '_') + { + memmove(serviceName, serviceName + 1, serviceNameLen); + serviceNameLen--; + serviceParsed = true; + } + else if (serviceNameLen == 5 && strcmp("local", serviceName) == 0) + { + tmp = _conn_read8(); + if (tmp == 0) + { + serviceParsed = true; + serviceNameLen = 0; + protoParsed = true; + protoNameLen = 0; + localParsed = true; + } + else + { #ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_PROTO: %s\n", protoName); + DEBUG_ESP_PORT.printf("ERR_FQDN: %s\n", serviceName); #endif - _conn->flush(); - return; - } - } - - if(!localParsed){ - char localName[32]; - uint8_t localNameLen = _conn_read8() % 31; - _conn_readS(localName, localNameLen); - localName[localNameLen] = '\0'; - tmp = _conn_read8(); - if(localNameLen == 5 && strcmp("local", localName) == 0 && tmp == 0){ - localParsed = true; - } else { + _conn->flush(); + return; + } + } + else + { #ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_FQDN: %s\n", localName); + DEBUG_ESP_PORT.printf("ERR_SERVICE: %s\n", serviceName); #endif - _conn->flush(); - return; + _conn->flush(); + return; + } } - } - if(serviceNameLen > 0 && protoNameLen > 0){ - servicePort = _getServicePort(serviceName, protoName); - if(servicePort == 0){ + if (!protoParsed) + { + protoNameLen = _conn_read8() % 255; + _conn_readS(protoName, protoNameLen); + protoName[protoNameLen] = '\0'; + if (protoNameLen == 4 && protoName[0] == '_') + { + memmove(protoName, protoName + 1, protoNameLen); + protoNameLen--; + protoParsed = true; + } + else if (strcmp("services", serviceName) == 0 && strcmp("_dns-sd", protoName) == 0) + { + _conn->flush(); + IPAddress interface = _getRequestMulticastInterface(); + _replyToTypeEnumRequest(interface); + return; + } + else + { #ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_NO_SERVICE: %s\n", serviceName); + DEBUG_ESP_PORT.printf("ERR_PROTO: %s\n", protoName); #endif - _conn->flush(); - return; + _conn->flush(); + return; + } } - } else if(serviceNameLen > 0 || protoNameLen > 0){ + + if (!localParsed) + { + char localName[32]; + uint8_t localNameLen = _conn_read8() % 31; + _conn_readS(localName, localNameLen); + localName[localNameLen] = '\0'; + tmp = _conn_read8(); + if (localNameLen == 5 && strcmp("local", localName) == 0 && tmp == 0) + { + localParsed = true; + } + else + { #ifdef DEBUG_ESP_MDNS_ERR - DEBUG_ESP_PORT.printf("ERR_SERVICE_PROTO: %s\n", serviceName); + DEBUG_ESP_PORT.printf("ERR_FQDN: %s\n", localName); #endif - _conn->flush(); - return; - } - - // RESPOND - -#ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n", packetHeader[0], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5]); -#endif - - uint16_t currentType; - uint16_t currentClass; - - int numQuestions = packetHeader[2]; - if(numQuestions > 4) numQuestions = 4; - uint16_t questions[4]; - int question = 0; - - while(numQuestions--){ - currentType = _conn_read16(); - if(currentType & MDNS_NAME_REF){ //new header handle it better! - currentType = _conn_read16(); + _conn->flush(); + return; + } } - currentClass = _conn_read16(); - if(currentClass & MDNS_CLASS_IN) questions[question++] = currentType; - if(numQuestions > 0){ - if(_conn_read16() != 0xC00C){//new question but for another host/service + if (serviceNameLen > 0 && protoNameLen > 0) + { + servicePort = _getServicePort(serviceName, protoName); + if (servicePort == 0) + { +#ifdef DEBUG_ESP_MDNS_ERR + DEBUG_ESP_PORT.printf("ERR_NO_SERVICE: %s\n", serviceName); +#endif + _conn->flush(); + return; + } + } + else if (serviceNameLen > 0 || protoNameLen > 0) + { +#ifdef DEBUG_ESP_MDNS_ERR + DEBUG_ESP_PORT.printf("ERR_SERVICE_PROTO: %s\n", serviceName); +#endif _conn->flush(); - numQuestions = 0; - } + return; } + // RESPOND + #ifdef DEBUG_ESP_MDNS_RX - DEBUG_ESP_PORT.printf("REQ: "); - if(hostNameLen > 0) - DEBUG_ESP_PORT.printf("%s.", hostName); - if(serviceNameLen > 0) - DEBUG_ESP_PORT.printf("_%s.", serviceName); - if(protoNameLen > 0) - DEBUG_ESP_PORT.printf("_%s.", protoName); - DEBUG_ESP_PORT.printf("local. "); - - if(currentType == MDNS_TYPE_AAAA) - DEBUG_ESP_PORT.printf(" AAAA "); - else if(currentType == MDNS_TYPE_A) - DEBUG_ESP_PORT.printf(" A "); - else if(currentType == MDNS_TYPE_PTR) - DEBUG_ESP_PORT.printf(" PTR "); - else if(currentType == MDNS_TYPE_SRV) - DEBUG_ESP_PORT.printf(" SRV "); - else if(currentType == MDNS_TYPE_TXT) - DEBUG_ESP_PORT.printf(" TXT "); - else - DEBUG_ESP_PORT.printf(" 0x%04X ", currentType); - - if(currentClass == MDNS_CLASS_IN) - DEBUG_ESP_PORT.printf(" IN "); - else if(currentClass == MDNS_CLASS_IN_FLUSH_CACHE) - DEBUG_ESP_PORT.printf(" IN[F] "); - else - DEBUG_ESP_PORT.printf(" 0x%04X ", currentClass); - - DEBUG_ESP_PORT.printf("\n"); + DEBUG_ESP_PORT.printf("RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n", packetHeader[0], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5]); #endif - } - uint8_t questionMask = 0; - uint8_t responseMask = 0; - for(i=0;i 4) + { + numQuestions = 4; + } + uint16_t questions[4]; + int question = 0; + + while (numQuestions--) + { + currentType = _conn_read16(); + if (currentType & MDNS_NAME_REF) //new header handle it better! + { + currentType = _conn_read16(); + } + currentClass = _conn_read16(); + if (currentClass & MDNS_CLASS_IN) + { + questions[question++] = currentType; + } + + if (numQuestions > 0) + { + if (_conn_read16() != 0xC00C) //new question but for another host/service + { + _conn->flush(); + numQuestions = 0; + } + } + +#ifdef DEBUG_ESP_MDNS_RX + DEBUG_ESP_PORT.printf("REQ: "); + if (hostNameLen > 0) + { + DEBUG_ESP_PORT.printf("%s.", hostName); + } + if (serviceNameLen > 0) + { + DEBUG_ESP_PORT.printf("_%s.", serviceName); + } + if (protoNameLen > 0) + { + DEBUG_ESP_PORT.printf("_%s.", protoName); + } + DEBUG_ESP_PORT.printf("local. "); + + if (currentType == MDNS_TYPE_AAAA) + { + DEBUG_ESP_PORT.printf(" AAAA "); + } + else if (currentType == MDNS_TYPE_A) + { + DEBUG_ESP_PORT.printf(" A "); + } + else if (currentType == MDNS_TYPE_PTR) + { + DEBUG_ESP_PORT.printf(" PTR "); + } + else if (currentType == MDNS_TYPE_SRV) + { + DEBUG_ESP_PORT.printf(" SRV "); + } + else if (currentType == MDNS_TYPE_TXT) + { + DEBUG_ESP_PORT.printf(" TXT "); + } + else + { + DEBUG_ESP_PORT.printf(" 0x%04X ", currentType); + } + + if (currentClass == MDNS_CLASS_IN) + { + DEBUG_ESP_PORT.printf(" IN "); + } + else if (currentClass == MDNS_CLASS_IN_FLUSH_CACHE) + { + DEBUG_ESP_PORT.printf(" IN[F] "); + } + else + { + DEBUG_ESP_PORT.printf(" 0x%04X ", currentClass); + } + + DEBUG_ESP_PORT.printf("\n"); +#endif + } + uint8_t questionMask = 0; + uint8_t responseMask = 0; + for (i = 0; i < question; i++) + { + if (questions[i] == MDNS_TYPE_A) + { + questionMask |= 0x1; + responseMask |= 0x1; + } + else if (questions[i] == MDNS_TYPE_SRV) + { + questionMask |= 0x2; + responseMask |= 0x3; + } + else if (questions[i] == MDNS_TYPE_TXT) + { + questionMask |= 0x4; + responseMask |= 0x4; + } + else if (questions[i] == MDNS_TYPE_PTR) + { + questionMask |= 0x8; + responseMask |= 0xF; + } + } + + IPAddress interface = _getRequestMulticastInterface(); + return _replyToInstanceRequest(questionMask, responseMask, serviceName, protoName, servicePort, interface); } /** - * STRINGIZE - */ + STRINGIZE +*/ #ifndef STRINGIZE - #define STRINGIZE(x) #x +#define STRINGIZE(x) #x #endif #ifndef STRINGIZE_VALUE_OF - #define STRINGIZE_VALUE_OF(x) STRINGIZE(x) +#define STRINGIZE_VALUE_OF(x) STRINGIZE(x) #endif -void MDNSResponder::enableArduino(uint16_t port, bool auth){ +void MDNSResponder::enableArduino(uint16_t port, bool auth) +{ - addService("arduino", "tcp", port); - addServiceTxt("arduino", "tcp", "tcp_check", "no"); - addServiceTxt("arduino", "tcp", "ssh_upload", "no"); - addServiceTxt("arduino", "tcp", "board", STRINGIZE_VALUE_OF(ARDUINO_BOARD)); - addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes":"no"); + addService("arduino", "tcp", port); + addServiceTxt("arduino", "tcp", "tcp_check", "no"); + addServiceTxt("arduino", "tcp", "ssh_upload", "no"); + addServiceTxt("arduino", "tcp", "board", STRINGIZE_VALUE_OF(ARDUINO_BOARD)); + addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no"); } -void MDNSResponder::_replyToTypeEnumRequest(IPAddress multicastInterface) { - MDNSService* servicePtr; - for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) { - if(servicePtr->_port > 0){ - char *service = servicePtr->_name; - char *proto = servicePtr->_proto; - //uint16_t port = servicePtr->_port; +void MDNSResponder::_replyToTypeEnumRequest(IPAddress multicastInterface) +{ + MDNSService* servicePtr; + for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) + { + if (servicePtr->_port > 0) + { + char *service = servicePtr->_name; + char *proto = servicePtr->_proto; + //uint16_t port = servicePtr->_port; #ifdef DEBUG_ESP_MDNS_TX - DEBUG_ESP_PORT.printf("TX: service:%s, proto:%s\n", service, proto); + DEBUG_ESP_PORT.printf("TX: service:%s, proto:%s\n", service, proto); #endif - char sdHostName[] = "_services"; - size_t sdHostNameLen = 9; - char sdServiceName[] = "_dns-sd"; - size_t sdServiceNameLen = 7; - char sdProtoName[] = "_udp"; - size_t sdProtoNameLen = 4; + char sdHostName[] = "_services"; + size_t sdHostNameLen = 9; + char sdServiceName[] = "_dns-sd"; + size_t sdServiceNameLen = 7; + char sdProtoName[] = "_udp"; + size_t sdProtoNameLen = 4; - char underscore[] = "_"; + char underscore[] = "_"; - // build service name with _ - char serviceName[os_strlen(service) + 2]; - os_strcpy(serviceName, underscore); - os_strcat(serviceName, service); - size_t serviceNameLen = os_strlen(serviceName); + // build service name with _ + char serviceName[os_strlen(service) + 2]; + os_strcpy(serviceName, underscore); + os_strcat(serviceName, service); + size_t serviceNameLen = os_strlen(serviceName); - //build proto name with _ - char protoName[5]; - os_strcpy(protoName, underscore); - os_strcat(protoName, proto); - size_t protoNameLen = 4; + //build proto name with _ + char protoName[5]; + os_strcpy(protoName, underscore); + os_strcat(protoName, proto); + size_t protoNameLen = 4; - //local string - char localName[] = "local"; - size_t localNameLen = 5; + //local string + char localName[] = "local"; + size_t localNameLen = 5; - //terminator - char terminator[] = "\0"; + //terminator + char terminator[] = "\0"; - //Write the header - _conn->flush(); - uint8_t head[12] = { - 0x00, 0x00, //ID = 0 - 0x84, 0x00, //Flags = response + authoritative answer - 0x00, 0x00, //Question count - 0x00, 0x01, //Answer count - 0x00, 0x00, //Name server records - 0x00, 0x00, //Additional records - }; - _conn->append(reinterpret_cast(head), 12); + //Write the header + _conn->flush(); + uint8_t head[12] = + { + 0x00, 0x00, //ID = 0 + 0x84, 0x00, //Flags = response + authoritative answer + 0x00, 0x00, //Question count + 0x00, 0x01, //Answer count + 0x00, 0x00, //Name server records + 0x00, 0x00, //Additional records + }; + _conn->append(reinterpret_cast(head), 12); - // Send the Name field (ie. "_services._dns-sd._udp.local") - _conn->append(reinterpret_cast(&sdHostNameLen), 1); // length of "_services" - _conn->append(reinterpret_cast(sdHostName), sdHostNameLen); // "_services" - _conn->append(reinterpret_cast(&sdServiceNameLen), 1); // length of "_dns-sd" - _conn->append(reinterpret_cast(sdServiceName), sdServiceNameLen);// "_dns-sd" - _conn->append(reinterpret_cast(&sdProtoNameLen), 1); // length of "_udp" - _conn->append(reinterpret_cast(sdProtoName), sdProtoNameLen); // "_udp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator + // Send the Name field (ie. "_services._dns-sd._udp.local") + _conn->append(reinterpret_cast(&sdHostNameLen), 1); // length of "_services" + _conn->append(reinterpret_cast(sdHostName), sdHostNameLen); // "_services" + _conn->append(reinterpret_cast(&sdServiceNameLen), 1); // length of "_dns-sd" + _conn->append(reinterpret_cast(sdServiceName), sdServiceNameLen);// "_dns-sd" + _conn->append(reinterpret_cast(&sdProtoNameLen), 1); // length of "_udp" + _conn->append(reinterpret_cast(sdProtoName), sdProtoNameLen); // "_udp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator - //Send the type, class, ttl and rdata length - uint8_t ptrDataLen = serviceNameLen + protoNameLen + localNameLen + 4; // 4 is three label sizes and the terminator - uint8_t ptrAttrs[10] = { - 0x00, 0x0c, //PTR record query - 0x00, 0x01, //Class IN - 0x00, 0x00, 0x11, 0x94, //TTL 4500 - 0x00, ptrDataLen, //RData length - }; - _conn->append(reinterpret_cast(ptrAttrs), 10); + //Send the type, class, ttl and rdata length + uint8_t ptrDataLen = serviceNameLen + protoNameLen + localNameLen + 4; // 4 is three label sizes and the terminator + uint8_t ptrAttrs[10] = + { + 0x00, 0x0c, //PTR record query + 0x00, 0x01, //Class IN + 0x00, 0x00, 0x11, 0x94, //TTL 4500 + 0x00, ptrDataLen, //RData length + }; + _conn->append(reinterpret_cast(ptrAttrs), 10); - //Send the RData (ie. "_http._tcp.local") - _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" - _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator + //Send the RData (ie. "_http._tcp.local") + _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" + _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator - _conn->setMulticastInterface(multicastInterface); - _conn->send(); + _conn->setMulticastInterface(multicastInterface); + _conn->send(); + } } - } } -void MDNSResponder::_replyToInstanceRequest(uint8_t questionMask, uint8_t responseMask, char * service, char *proto, uint16_t port, IPAddress multicastInterface) { - int i; - if(questionMask == 0) return; - if(responseMask == 0) return; +void MDNSResponder::_replyToInstanceRequest(uint8_t questionMask, uint8_t responseMask, char * service, char *proto, uint16_t port, IPAddress multicastInterface) +{ + int i; + if (questionMask == 0) + { + return; + } + if (responseMask == 0) + { + return; + } #ifdef DEBUG_ESP_MDNS_TX DEBUG_ESP_PORT.printf("TX: qmask:%01X, rmask:%01X, service:%s, proto:%s, port:%u\n", questionMask, responseMask, service, proto, port); #endif - String instanceName = _instanceName; - size_t instanceNameLen = instanceName.length(); + String instanceName = _instanceName; + size_t instanceNameLen = instanceName.length(); - String hostName = _hostName; - size_t hostNameLen = hostName.length(); + String hostName = _hostName; + size_t hostNameLen = hostName.length(); - char underscore[] = "_"; - - // build service name with _ - char serviceName[os_strlen(service)+2]; - os_strcpy(serviceName,underscore); - os_strcat(serviceName, service); - size_t serviceNameLen = os_strlen(serviceName); + char underscore[] = "_"; - //build proto name with _ - char protoName[5]; - os_strcpy(protoName,underscore); - os_strcat(protoName, proto); - size_t protoNameLen = 4; + // build service name with _ + char serviceName[os_strlen(service) + 2]; + os_strcpy(serviceName, underscore); + os_strcat(serviceName, service); + size_t serviceNameLen = os_strlen(serviceName); - //local string - char localName[] = "local"; - size_t localNameLen = 5; + //build proto name with _ + char protoName[5]; + os_strcpy(protoName, underscore); + os_strcat(protoName, proto); + size_t protoNameLen = 4; - //terminator - char terminator[] = "\0"; + //local string + char localName[] = "local"; + size_t localNameLen = 5; - uint8_t answerMask = responseMask & questionMask; - uint8_t answerCount = 0; - uint8_t additionalMask = responseMask & ~questionMask; - uint8_t additionalCount = 0; - for(i=0;i<4;i++){ - if(answerMask & (1 << i)) - answerCount++; - if(additionalMask & (1 << i)) - additionalCount++; - } + //terminator + char terminator[] = "\0"; - - //Write the header - _conn->flush(); - uint8_t head[12] = { - 0x00, 0x00, //ID = 0 - 0x84, 0x00, //Flags = response + authoritative answer - 0x00, 0x00, //Question count - 0x00, answerCount, //Answer count - 0x00, 0x00, //Name server records - 0x00, additionalCount, //Additional records - }; - _conn->append(reinterpret_cast(head), 12); - - for(int responseSection = 0; responseSection < 2; ++responseSection) { - - // PTR Response - if((responseSection == 0 ? answerMask : additionalMask) & 0x8){ - // Send the Name field (ie. "_http._tcp.local") - _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" - _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator - - //Send the type, class, ttl and rdata length - uint8_t ptrDataLen = instanceNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator - uint8_t ptrAttrs[10] = { - 0x00, 0x0c, //PTR record query - 0x00, 0x01, //Class IN - 0x00, 0x00, 0x00, 0x78, //TTL 120 - 0x00, ptrDataLen, //RData length - }; - _conn->append(reinterpret_cast(ptrAttrs), 10); - - //Send the RData (ie. "My IOT device._http._tcp.local") - _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" - _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" - _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" - _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator - } - - //TXT Responce - if((responseSection == 0 ? answerMask : additionalMask) & 0x4){ - //Send the name field (ie. "My IOT device._http._tcp.local") - _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" - _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" - _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" - _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator - - //Send the type, class, ttl and rdata length - uint8_t txtDataLen = _getServiceTxtLen(service,proto); - uint8_t txtAttrs[10] = { - 0x00, 0x10, //TXT record query - 0x80, 0x01, //Class IN, with cache flush - 0x00, 0x00, 0x11, 0x94, //TTL 4500 - 0x00, txtDataLen, //RData length - }; - _conn->append(reinterpret_cast(txtAttrs), 10); - - //Send the RData - MDNSTxt * txtPtr = _getServiceTxt(service,proto); - while(txtPtr !=0){ - uint8_t txtLen = txtPtr->_txt.length(); - _conn->append(reinterpret_cast(&txtLen), 1); // length of txt - _conn->append(reinterpret_cast(txtPtr->_txt.c_str()), txtLen);// the txt - txtPtr = txtPtr->_next; - } + uint8_t answerMask = responseMask & questionMask; + uint8_t answerCount = 0; + uint8_t additionalMask = responseMask & ~questionMask; + uint8_t additionalCount = 0; + for (i = 0; i < 4; i++) + { + if (answerMask & (1 << i)) + { + answerCount++; + } + if (additionalMask & (1 << i)) + { + additionalCount++; + } } - //SRV Responce - if((responseSection == 0 ? answerMask : additionalMask) & 0x2){ - //Send the name field (ie. "My IOT device._http._tcp.local") - _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" - _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" - _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" - _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" - _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" - _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator + //Write the header + _conn->flush(); + uint8_t head[12] = + { + 0x00, 0x00, //ID = 0 + 0x84, 0x00, //Flags = response + authoritative answer + 0x00, 0x00, //Question count + 0x00, answerCount, //Answer count + 0x00, 0x00, //Name server records + 0x00, additionalCount, //Additional records + }; + _conn->append(reinterpret_cast(head), 12); - //Send the type, class, ttl, rdata length, priority and weight - uint8_t srvDataSize = hostNameLen + localNameLen + 3; // 3 is 2 lable size bytes and the terminator - srvDataSize += 6; // Size of Priority, weight and port - uint8_t srvAttrs[10] = { - 0x00, 0x21, //Type SRV - 0x80, 0x01, //Class IN, with cache flush - 0x00, 0x00, 0x00, 0x78, //TTL 120 - 0x00, srvDataSize, //RData length - }; - _conn->append(reinterpret_cast(srvAttrs), 10); + for (int responseSection = 0; responseSection < 2; ++responseSection) + { - //Send the RData Priority weight and port - uint8_t srvRData[6] = { - 0x00, 0x00, //Priority 0 - 0x00, 0x00, //Weight 0 - (uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF) - }; - _conn->append(reinterpret_cast(srvRData), 6); - //Send the RData (ie. "esp8266.local") - _conn->append(reinterpret_cast(&hostNameLen), 1); // length of "esp8266" - _conn->append(reinterpret_cast(hostName.c_str()), hostNameLen);// "esp8266" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator + // PTR Response + if ((responseSection == 0 ? answerMask : additionalMask) & 0x8) + { + // Send the Name field (ie. "_http._tcp.local") + _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" + _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + //Send the type, class, ttl and rdata length + uint8_t ptrDataLen = instanceNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator + uint8_t ptrAttrs[10] = + { + 0x00, 0x0c, //PTR record query + 0x00, 0x01, //Class IN + 0x00, 0x00, 0x00, 0x78, //TTL 120 + 0x00, ptrDataLen, //RData length + }; + _conn->append(reinterpret_cast(ptrAttrs), 10); + + //Send the RData (ie. "My IOT device._http._tcp.local") + _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" + _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" + _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" + _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + } + + //TXT Responce + if ((responseSection == 0 ? answerMask : additionalMask) & 0x4) + { + //Send the name field (ie. "My IOT device._http._tcp.local") + _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" + _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" + _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" + _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + + //Send the type, class, ttl and rdata length + uint8_t txtDataLen = _getServiceTxtLen(service, proto); + uint8_t txtAttrs[10] = + { + 0x00, 0x10, //TXT record query + 0x80, 0x01, //Class IN, with cache flush + 0x00, 0x00, 0x11, 0x94, //TTL 4500 + 0x00, txtDataLen, //RData length + }; + _conn->append(reinterpret_cast(txtAttrs), 10); + + //Send the RData + MDNSTxt * txtPtr = _getServiceTxt(service, proto); + while (txtPtr != 0) + { + uint8_t txtLen = txtPtr->_txt.length(); + _conn->append(reinterpret_cast(&txtLen), 1); // length of txt + _conn->append(reinterpret_cast(txtPtr->_txt.c_str()), txtLen);// the txt + txtPtr = txtPtr->_next; + } + } + + + //SRV Responce + if ((responseSection == 0 ? answerMask : additionalMask) & 0x2) + { + //Send the name field (ie. "My IOT device._http._tcp.local") + _conn->append(reinterpret_cast(&instanceNameLen), 1); // length of "My IOT device" + _conn->append(reinterpret_cast(instanceName.c_str()), instanceNameLen);// "My IOT device" + _conn->append(reinterpret_cast(&serviceNameLen), 1); // length of "_http" + _conn->append(reinterpret_cast(serviceName), serviceNameLen); // "_http" + _conn->append(reinterpret_cast(&protoNameLen), 1); // length of "_tcp" + _conn->append(reinterpret_cast(protoName), protoNameLen); // "_tcp" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + + //Send the type, class, ttl, rdata length, priority and weight + uint8_t srvDataSize = hostNameLen + localNameLen + 3; // 3 is 2 lable size bytes and the terminator + srvDataSize += 6; // Size of Priority, weight and port + uint8_t srvAttrs[10] = + { + 0x00, 0x21, //Type SRV + 0x80, 0x01, //Class IN, with cache flush + 0x00, 0x00, 0x00, 0x78, //TTL 120 + 0x00, srvDataSize, //RData length + }; + _conn->append(reinterpret_cast(srvAttrs), 10); + + //Send the RData Priority weight and port + uint8_t srvRData[6] = + { + 0x00, 0x00, //Priority 0 + 0x00, 0x00, //Weight 0 + (uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF) + }; + _conn->append(reinterpret_cast(srvRData), 6); + //Send the RData (ie. "esp8266.local") + _conn->append(reinterpret_cast(&hostNameLen), 1); // length of "esp8266" + _conn->append(reinterpret_cast(hostName.c_str()), hostNameLen);// "esp8266" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + + } + + // A Response + if ((responseSection == 0 ? answerMask : additionalMask) & 0x1) + { + //Send the RData (ie. "esp8266.local") + _conn->append(reinterpret_cast(&hostNameLen), 1); // length of "esp8266" + _conn->append(reinterpret_cast(hostName.c_str()), hostNameLen);// "esp8266" + _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" + _conn->append(reinterpret_cast(localName), localNameLen); // "local" + _conn->append(reinterpret_cast(&terminator), 1); // terminator + + uint8_t aaaAttrs[10] = + { + 0x00, 0x01, //TYPE A + 0x80, 0x01, //Class IN, with cache flush + 0x00, 0x00, 0x00, 0x78, //TTL 120 + 0x00, 0x04, //DATA LEN + }; + _conn->append(reinterpret_cast(aaaAttrs), 10); + + // Send RData + uint32_t ip = multicastInterface; + uint8_t aaaRData[4] = + { + (uint8_t)(ip & 0xFF), //IP first octet + (uint8_t)((ip >> 8) & 0xFF), //IP second octet + (uint8_t)((ip >> 16) & 0xFF), //IP third octet + (uint8_t)((ip >> 24) & 0xFF) //IP fourth octet + }; + _conn->append(reinterpret_cast(aaaRData), 4); + } } - // A Response - if((responseSection == 0 ? answerMask : additionalMask) & 0x1){ - //Send the RData (ie. "esp8266.local") - _conn->append(reinterpret_cast(&hostNameLen), 1); // length of "esp8266" - _conn->append(reinterpret_cast(hostName.c_str()), hostNameLen);// "esp8266" - _conn->append(reinterpret_cast(&localNameLen), 1); // length "local" - _conn->append(reinterpret_cast(localName), localNameLen); // "local" - _conn->append(reinterpret_cast(&terminator), 1); // terminator - - uint8_t aaaAttrs[10] = { - 0x00, 0x01, //TYPE A - 0x80, 0x01, //Class IN, with cache flush - 0x00, 0x00, 0x00, 0x78, //TTL 120 - 0x00, 0x04, //DATA LEN - }; - _conn->append(reinterpret_cast(aaaAttrs), 10); - - // Send RData - uint32_t ip = multicastInterface; - uint8_t aaaRData[4] = { - (uint8_t)(ip & 0xFF), //IP first octet - (uint8_t)((ip >> 8) & 0xFF), //IP second octet - (uint8_t)((ip >> 16) & 0xFF), //IP third octet - (uint8_t)((ip >> 24) & 0xFF) //IP fourth octet - }; - _conn->append(reinterpret_cast(aaaRData), 4); - } - } - - _conn->setMulticastInterface(multicastInterface); - _conn->send(); + _conn->setMulticastInterface(multicastInterface); + _conn->send(); } #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS) diff --git a/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.h b/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.h index 6d241ae06..9d3cfd2f6 100644 --- a/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.h +++ b/libraries/ESP8266mDNS/src/ESP8266mDNS_Legacy.h @@ -1,43 +1,43 @@ /* -ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) -Version 1.1 -Copyright (c) 2013 Tony DiCola (tony@tonydicola.com) -ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com) -Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com) + ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) + Version 1.1 + Copyright (c) 2013 Tony DiCola (tony@tonydicola.com) + ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com) + Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com) -This is a simple implementation of multicast DNS query support for an Arduino -running on ESP8266 chip. Only support for resolving address queries is currently -implemented. + This is a simple implementation of multicast DNS query support for an Arduino + running on ESP8266 chip. Only support for resolving address queries is currently + implemented. -Requirements: -- ESP8266WiFi library + Requirements: + - ESP8266WiFi library -Usage: -- Include the ESP8266 Multicast DNS library in the sketch. -- Call the begin method in the sketch's setup and provide a domain name (without - the '.local' suffix, i.e. just provide 'foo' to resolve 'foo.local'), and the - Adafruit CC3000 class instance. Optionally provide a time to live (in seconds) - for the DNS record--the default is 1 hour. -- Call the update method in each iteration of the sketch's loop function. + Usage: + - Include the ESP8266 Multicast DNS library in the sketch. + - Call the begin method in the sketch's setup and provide a domain name (without + the '.local' suffix, i.e. just provide 'foo' to resolve 'foo.local'), and the + Adafruit CC3000 class instance. Optionally provide a time to live (in seconds) + for the DNS record--the default is 1 hour. + - Call the update method in each iteration of the sketch's loop function. -License (MIT license): - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. */ #ifndef ESP8266MDNS_LEGACY_H @@ -54,95 +54,108 @@ License (MIT license): class UdpContext; -namespace Legacy_MDNSResponder { +namespace Legacy_MDNSResponder +{ struct MDNSService; struct MDNSTxt; struct MDNSAnswer; -class MDNSResponder { +class MDNSResponder +{ public: - MDNSResponder(); - ~MDNSResponder(); - bool begin(const char* hostName); - bool begin(const String& hostName) { - return begin(hostName.c_str()); - } - //for compatibility - bool begin(const char* hostName, IPAddress ip, uint32_t ttl=120){ - (void) ip; - (void) ttl; - return begin(hostName); - } - bool begin(const String& hostName, IPAddress ip, uint32_t ttl=120) { - return begin(hostName.c_str(), ip, ttl); - } - /* Application should call this whenever AP is configured/disabled */ - void notifyAPChange(); - void update(); + MDNSResponder(); + ~MDNSResponder(); + bool begin(const char* hostName); + bool begin(const String& hostName) + { + return begin(hostName.c_str()); + } + //for compatibility + bool begin(const char* hostName, IPAddress ip, uint32_t ttl = 120) + { + (void) ip; + (void) ttl; + return begin(hostName); + } + bool begin(const String& hostName, IPAddress ip, uint32_t ttl = 120) + { + return begin(hostName.c_str(), ip, ttl); + } + /* Application should call this whenever AP is configured/disabled */ + void notifyAPChange(); + void update(); - void addService(char *service, char *proto, uint16_t port); - void addService(const char *service, const char *proto, uint16_t port){ - addService((char *)service, (char *)proto, port); - } - void addService(const String& service, const String& proto, uint16_t port){ - addService(service.c_str(), proto.c_str(), port); - } - - bool addServiceTxt(char *name, char *proto, char * key, char * value); - bool addServiceTxt(const char *name, const char *proto, const char *key,const char * value){ - return addServiceTxt((char *)name, (char *)proto, (char *)key, (char *)value); - } - bool addServiceTxt(const String& name, const String& proto, const String& key, const String& value){ - return addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str()); - } - - int queryService(char *service, char *proto); - int queryService(const char *service, const char *proto){ - return queryService((char *)service, (char *)proto); - } - int queryService(const String& service, const String& proto){ - return queryService(service.c_str(), proto.c_str()); - } - String hostname(int idx); - IPAddress IP(int idx); - uint16_t port(int idx); - - void enableArduino(uint16_t port, bool auth=false); + void addService(char *service, char *proto, uint16_t port); + void addService(const char *service, const char *proto, uint16_t port) + { + addService((char *)service, (char *)proto, port); + } + void addService(const String& service, const String& proto, uint16_t port) + { + addService(service.c_str(), proto.c_str(), port); + } - void setInstanceName(String name); - void setInstanceName(const char * name){ - setInstanceName(String(name)); - } - void setInstanceName(char * name){ - setInstanceName(String(name)); - } + bool addServiceTxt(char *name, char *proto, char * key, char * value); + bool addServiceTxt(const char *name, const char *proto, const char *key, const char * value) + { + return addServiceTxt((char *)name, (char *)proto, (char *)key, (char *)value); + } + bool addServiceTxt(const String& name, const String& proto, const String& key, const String& value) + { + return addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str()); + } + + int queryService(char *service, char *proto); + int queryService(const char *service, const char *proto) + { + return queryService((char *)service, (char *)proto); + } + int queryService(const String& service, const String& proto) + { + return queryService(service.c_str(), proto.c_str()); + } + String hostname(int idx); + IPAddress IP(int idx); + uint16_t port(int idx); + + void enableArduino(uint16_t port, bool auth = false); + + void setInstanceName(String name); + void setInstanceName(const char * name) + { + setInstanceName(String(name)); + } + void setInstanceName(char * name) + { + setInstanceName(String(name)); + } private: - struct MDNSService * _services; - UdpContext* _conn; - String _hostName; - String _instanceName; - struct MDNSAnswer * _answers; - struct MDNSQuery * _query; - bool _newQuery; - bool _waitingForAnswers; - WiFiEventHandler _disconnectedHandler; - WiFiEventHandler _gotIPHandler; - + struct MDNSService * _services; + UdpContext* _conn; + String _hostName; + String _instanceName; + struct MDNSAnswer * _answers; + struct MDNSQuery * _query; + bool _newQuery; + bool _waitingForAnswers; + WiFiEventHandler _disconnectedHandler; + WiFiEventHandler _gotIPHandler; - uint16_t _getServicePort(char *service, char *proto); - MDNSTxt * _getServiceTxt(char *name, char *proto); - uint16_t _getServiceTxtLen(char *name, char *proto); - IPAddress _getRequestMulticastInterface(); - void _parsePacket(); - void _replyToTypeEnumRequest(IPAddress multicastInterface); - void _replyToInstanceRequest(uint8_t questionMask, uint8_t responseMask, char * service, char *proto, uint16_t port, IPAddress multicastInterface); - MDNSAnswer* _getAnswerFromIdx(int idx); - int _getNumAnswers(); - bool _listen(); - void _restart(); + + uint16_t _getServicePort(char *service, char *proto); + MDNSTxt * _getServiceTxt(char *name, char *proto); + uint16_t _getServiceTxtLen(char *name, char *proto); + IPAddress _getRequestMulticastInterface(); + void _parsePacket(); + void _replyToTypeEnumRequest(IPAddress multicastInterface); + void _replyToInstanceRequest(uint8_t questionMask, uint8_t responseMask, char * service, char *proto, uint16_t port, IPAddress multicastInterface); + MDNSAnswer* _getAnswerFromIdx(int idx); + int _getNumAnswers(); + bool _listen(); + void _restart(); }; } // namespace Legacy_MDNSResponder diff --git a/libraries/ESP8266mDNS/src/LEAmDNS.cpp b/libraries/ESP8266mDNS/src/LEAmDNS.cpp index 1b468fb26..95f07079d 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS.cpp @@ -1,1212 +1,1380 @@ -/* - * LEAmDNS.cpp - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#include -#include - -#include "LEAmDNS_Priv.h" - - -namespace esp8266 { - -/* - * LEAmDNS - */ -namespace MDNSImplementation { - -/** - * STRINGIZE - */ -#ifndef STRINGIZE - #define STRINGIZE(x) #x -#endif -#ifndef STRINGIZE_VALUE_OF - #define STRINGIZE_VALUE_OF(x) STRINGIZE(x) -#endif - - -/** - * INTERFACE - */ - -/** - * MDNSResponder::MDNSResponder - */ -MDNSResponder::MDNSResponder(void) -: m_pServices(0), - m_pUDPContext(0), - m_pcHostname(0), - m_pServiceQueries(0), - m_fnServiceTxtCallback(0), -#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE - m_bPassivModeEnabled(true), -#else - m_bPassivModeEnabled(false), -#endif - m_netif(nullptr) { -} - -/* - * MDNSResponder::~MDNSResponder - */ -MDNSResponder::~MDNSResponder(void) { - - _resetProbeStatus(false); - _releaseServiceQueries(); - _releaseHostname(); - _releaseUDPContext(); - _releaseServices(); -} - -/* - * MDNSResponder::begin - * - * Set the host domain (for probing) and install WiFi event handlers for - * IP assignment and disconnection management. In both cases, the MDNS responder - * is restarted (reset and restart probe status) - * Finally the responder is (re)started - * - */ -bool MDNSResponder::begin(const char* p_pcHostname, const IPAddress& p_IPAddress, uint32_t p_u32TTL) { - - (void)p_u32TTL; // ignored - bool bResult = false; - - if (0 == m_pUDPContext) { - if (_setHostname(p_pcHostname)) { - - //// select interface - - m_netif = nullptr; - IPAddress ipAddress = p_IPAddress; - - if (!ipAddress.isSet()) { - - IPAddress sta = WiFi.localIP(); - IPAddress ap = WiFi.softAPIP(); - - if (!sta.isSet() && !ap.isSet()) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] internal interfaces (STA, AP) are not set (none was specified)\n"))); - return false; - } - - if (ap.isSet()) { - - if (sta.isSet()) - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected over STA (none was specified)\n"))); - else - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected\n"))); - ipAddress = ap; - - } else { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface STA selected (none was specified)\n"))); - ipAddress = sta; - - } - - // continue to ensure interface is UP - } - - // check existence of this IP address in the interface list - bool found = false; - m_netif = nullptr; - for (auto a: addrList) - if (ipAddress == a.addr()) { - if (a.ifUp()) { - found = true; - m_netif = a.interface(); - break; - } - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] found interface for IP '%s' but it is not UP\n"), ipAddress.toString().c_str());); - } - if (!found) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] interface defined by IP '%s' not found\n"), ipAddress.toString().c_str());); - return false; - } - - //// done selecting the interface - - if (m_netif->num == STATION_IF) { - - m_GotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& pEvent) { - (void) pEvent; - // Ensure that _restart() runs in USER context - schedule_function([this]() { MDNSResponder::_restart(); }); - }); - - m_DisconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& pEvent) { - (void) pEvent; - // Ensure that _restart() runs in USER context - schedule_function([this]() { MDNSResponder::_restart(); }); - }); - } - - bResult = _restart(); - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), (p_pcHostname ?: "-")); } ); - } - else { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: Ignoring multiple calls to begin (Ignored host domain: '%s')!\n"), (p_pcHostname ?: "-"));); - } - return bResult; -} - -/* - * MDNSResponder::close - * - * Ends the MDNS responder. - * Announced services are unannounced (by multicasting a goodbye message) - * - */ -bool MDNSResponder::close(void) { - - m_GotIPHandler.reset(); // reset WiFi event callbacks. - m_DisconnectedHandler.reset(); - - _announce(false, true); - _resetProbeStatus(false); // Stop probing - - _releaseServiceQueries(); - _releaseUDPContext(); - _releaseHostname(); - - return true; -} - -/* - * MDNSResponder::end - * - * Ends the MDNS responder. - * for compatibility with esp32 - * - */ - -bool MDNSResponder::end(void) { - return close(); -} - -/* - * MDNSResponder::setHostname - * - * Replaces the current hostname and restarts probing. - * For services without own instance name (when the host name was used a instance - * name), the instance names are replaced also (and the probing is restarted). - * - */ -bool MDNSResponder::setHostname(const char* p_pcHostname) { - - bool bResult = false; - - if (_setHostname(p_pcHostname)) { - m_HostProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; - - // Replace 'auto-set' service names - bResult = true; - for (stcMDNSService* pService=m_pServices; ((bResult) && (pService)); pService=pService->m_pNext) { - if (pService->m_bAutoName) { - bResult = pService->setName(p_pcHostname); - pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; - } - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setHostname: FAILED for '%s'!\n"), (p_pcHostname ?: "-")); } ); - return bResult; -} - -/* - * MDNSResponder::setHostname (LEGACY) - */ -bool MDNSResponder::setHostname(String p_strHostname) { - - return setHostname(p_strHostname.c_str()); -} - - -/* - * SERVICES - */ - -/* - * MDNSResponder::addService - * - * Add service; using hostname if no name is explicitly provided for the service - * The usual '_' underline, which is prepended to service and protocol, eg. _http, - * may be given. If not, it is added automatically. - * - */ -MDNSResponder::hMDNSService MDNSResponder::addService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol, - uint16_t p_u16Port) { - - hMDNSService hResult = 0; - - if (((!p_pcName) || // NO name OR - (MDNS_DOMAIN_LABEL_MAXLENGTH >= os_strlen(p_pcName))) && // Fitting name - (p_pcService) && - (MDNS_SERVICE_NAME_LENGTH >= os_strlen(p_pcService)) && - (p_pcProtocol) && - ((MDNS_SERVICE_PROTOCOL_LENGTH - 1) != os_strlen(p_pcProtocol)) && - (p_u16Port)) { - - if (!_findService((p_pcName ?: m_pcHostname), p_pcService, p_pcProtocol)) { // Not already used - if (0 != (hResult = (hMDNSService)_allocService(p_pcName, p_pcService, p_pcProtocol, p_u16Port))) { - - // Start probing - ((stcMDNSService*)hResult)->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; - } - } - } // else: bad arguments - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: %s to add '%s.%s.%s'!\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcName ?: "-"), p_pcService, p_pcProtocol); ); - DEBUG_EX_ERR(if (!hResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: FAILED to add '%s.%s.%s'!\n"), (p_pcName ?: "-"), p_pcService, p_pcProtocol); } ); - return hResult; -} - -/* - * MDNSResponder::removeService - * - * Unanounce a service (by sending a goodbye message) and remove it - * from the MDNS responder - * - */ -bool MDNSResponder::removeService(const MDNSResponder::hMDNSService p_hService) { - - stcMDNSService* pService = 0; - bool bResult = (((pService = _findService(p_hService))) && - (_announceService(*pService, false)) && - (_releaseService(pService))); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeService: FAILED!\n")); } ); - return bResult; -} - -/* - * MDNSResponder::removeService - */ -bool MDNSResponder::removeService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol) { - - return removeService((hMDNSService)_findService((p_pcName ?: m_pcHostname), p_pcService, p_pcProtocol)); -} - -/* - * MDNSResponder::addService (LEGACY) - */ -bool MDNSResponder::addService(String p_strService, - String p_strProtocol, - uint16_t p_u16Port) { - - return (0 != addService(m_pcHostname, p_strService.c_str(), p_strProtocol.c_str(), p_u16Port)); -} - -/* - * MDNSResponder::setServiceName - */ -bool MDNSResponder::setServiceName(const MDNSResponder::hMDNSService p_hService, - const char* p_pcInstanceName) { - - stcMDNSService* pService = 0; - bool bResult = (((!p_pcInstanceName) || - (MDNS_DOMAIN_LABEL_MAXLENGTH >= os_strlen(p_pcInstanceName))) && - ((pService = _findService(p_hService))) && - (pService->setName(p_pcInstanceName)) && - ((pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart))); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setServiceName: FAILED for '%s'!\n"), (p_pcInstanceName ?: "-")); } ); - return bResult; -} - -/* - * SERVICE TXT - */ - -/* - * MDNSResponder::addServiceTxt - * - * Add a static service TXT item ('Key'='Value') to a service. - * - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - const char* p_pcValue) { - - hMDNSTxt hTxt = 0; - stcMDNSService* pService = _findService(p_hService); - if (pService) { - hTxt = (hMDNSTxt)_addServiceTxt(pService, p_pcKey, p_pcValue, false); - } - DEBUG_EX_ERR(if (!hTxt) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addServiceTxt: FAILED for '%s=%s'!\n"), (p_pcKey ?: "-"), (p_pcValue ?: "-")); } ); - return hTxt; -} - -/* - * MDNSResponder::addServiceTxt (uint32_t) - * - * Formats: http://www.cplusplus.com/reference/cstdio/printf/ - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint32_t p_u32Value) { - char acBuffer[32]; *acBuffer = 0; - sprintf(acBuffer, "%u", p_u32Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addServiceTxt (uint16_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint16_t p_u16Value) { - char acBuffer[16]; *acBuffer = 0; - sprintf(acBuffer, "%hu", p_u16Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addServiceTxt (uint8_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint8_t p_u8Value) { - char acBuffer[8]; *acBuffer = 0; - sprintf(acBuffer, "%hhu", p_u8Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addServiceTxt (int32_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int32_t p_i32Value) { - char acBuffer[32]; *acBuffer = 0; - sprintf(acBuffer, "%i", p_i32Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addServiceTxt (int16_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int16_t p_i16Value) { - char acBuffer[16]; *acBuffer = 0; - sprintf(acBuffer, "%hi", p_i16Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addServiceTxt (int8_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int8_t p_i8Value) { - char acBuffer[8]; *acBuffer = 0; - sprintf(acBuffer, "%hhi", p_i8Value); - - return addServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::removeServiceTxt - * - * Remove a static service TXT item from a service. - */ -bool MDNSResponder::removeServiceTxt(const MDNSResponder::hMDNSService p_hService, - const MDNSResponder::hMDNSTxt p_hTxt) { - - bool bResult = false; - - stcMDNSService* pService = _findService(p_hService); - if (pService) { - stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_hTxt); - if (pTxt) { - bResult = _releaseServiceTxt(pService, pTxt); - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceTxt: FAILED!\n")); } ); - return bResult; -} - -/* - * MDNSResponder::removeServiceTxt - */ -bool MDNSResponder::removeServiceTxt(const MDNSResponder::hMDNSService p_hService, - const char* p_pcKey) { - - bool bResult = false; - - stcMDNSService* pService = _findService(p_hService); - if (pService) { - stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_pcKey); - if (pTxt) { - bResult = _releaseServiceTxt(pService, pTxt); - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceTxt: FAILED for '%s'!\n"), (p_pcKey ?: "-")); } ); - return bResult; -} - -/* - * MDNSResponder::removeServiceTxt - */ -bool MDNSResponder::removeServiceTxt(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol, - const char* p_pcKey) { - - bool bResult = false; - - stcMDNSService* pService = _findService((p_pcName ?: m_pcHostname), p_pcService, p_pcProtocol); - if (pService) { - stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_pcKey); - if (pTxt) { - bResult = _releaseServiceTxt(pService, pTxt); - } - } - return bResult; -} - -/* - * MDNSResponder::addServiceTxt (LEGACY) - */ -bool MDNSResponder::addServiceTxt(const char* p_pcService, - const char* p_pcProtocol, - const char* p_pcKey, - const char* p_pcValue) { - - return (0 != _addServiceTxt(_findService(m_pcHostname, p_pcService, p_pcProtocol), p_pcKey, p_pcValue, false)); -} - -/* - * MDNSResponder::addServiceTxt (LEGACY) - */ -bool MDNSResponder::addServiceTxt(String p_strService, - String p_strProtocol, - String p_strKey, - String p_strValue) { - - return (0 != _addServiceTxt(_findService(m_pcHostname, p_strService.c_str(), p_strProtocol.c_str()), p_strKey.c_str(), p_strValue.c_str(), false)); -} - -/* - * MDNSResponder::setDynamicServiceTxtCallback (global) - * - * Set a global callback for dynamic service TXT items. The callback is called, whenever - * service TXT items are needed. - * - */ -bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) { - - m_fnServiceTxtCallback = p_fnCallback; - - return true; -} - -/* - * MDNSResponder::setDynamicServiceTxtCallback (service specific) - * - * Set a service specific callback for dynamic service TXT items. The callback is called, whenever - * service TXT items are needed for the given service. - * - */ -bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::hMDNSService p_hService, - MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) { - - bool bResult = false; - - stcMDNSService* pService = _findService(p_hService); - if (pService) { - pService->m_fnTxtCallback = p_fnCallback; - - bResult = true; - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setDynamicServiceTxtCallback: FAILED!\n")); } ); - return bResult; -} - -/* - * MDNSResponder::addDynamicServiceTxt - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - const char* p_pcValue) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addDynamicServiceTxt (%s=%s)\n"), p_pcKey, p_pcValue);); - - hMDNSTxt hTxt = 0; - - stcMDNSService* pService = _findService(p_hService); - if (pService) { - hTxt = _addServiceTxt(pService, p_pcKey, p_pcValue, true); - } - DEBUG_EX_ERR(if (!hTxt) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addDynamicServiceTxt: FAILED for '%s=%s'!\n"), (p_pcKey ?: "-"), (p_pcValue ?: "-")); } ); - return hTxt; -} - -/* - * MDNSResponder::addDynamicServiceTxt (uint32_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint32_t p_u32Value) { - - char acBuffer[32]; *acBuffer = 0; - sprintf(acBuffer, "%u", p_u32Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addDynamicServiceTxt (uint16_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint16_t p_u16Value) { - - char acBuffer[16]; *acBuffer = 0; - sprintf(acBuffer, "%hu", p_u16Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addDynamicServiceTxt (uint8_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - uint8_t p_u8Value) { - - char acBuffer[8]; *acBuffer = 0; - sprintf(acBuffer, "%hhu", p_u8Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addDynamicServiceTxt (int32_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int32_t p_i32Value) { - - char acBuffer[32]; *acBuffer = 0; - sprintf(acBuffer, "%i", p_i32Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addDynamicServiceTxt (int16_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int16_t p_i16Value) { - - char acBuffer[16]; *acBuffer = 0; - sprintf(acBuffer, "%hi", p_i16Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - -/* - * MDNSResponder::addDynamicServiceTxt (int8_t) - */ -MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, - const char* p_pcKey, - int8_t p_i8Value) { - - char acBuffer[8]; *acBuffer = 0; - sprintf(acBuffer, "%hhi", p_i8Value); - - return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); -} - - -/** - * STATIC SERVICE QUERY (LEGACY) - */ - -/* - * MDNSResponder::queryService - * - * Perform a (blocking) static service query. - * The arrived answers can be queried by calling: - * - answerHostname (or 'hostname') - * - answerIP (or 'IP') - * - answerPort (or 'port') - * - */ -uint32_t MDNSResponder::queryService(const char* p_pcService, - const char* p_pcProtocol, - const uint16_t p_u16Timeout /*= MDNS_QUERYSERVICES_WAIT_TIME*/) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService '%s.%s'\n"), p_pcService, p_pcProtocol);); - - uint32_t u32Result = 0; - - stcMDNSServiceQuery* pServiceQuery = 0; - if ((p_pcService) && - (os_strlen(p_pcService)) && - (p_pcProtocol) && - (os_strlen(p_pcProtocol)) && - (p_u16Timeout) && - (_removeLegacyServiceQuery()) && - ((pServiceQuery = _allocServiceQuery())) && - (_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) { - - pServiceQuery->m_bLegacyQuery = true; - - if (_sendMDNSServiceQuery(*pServiceQuery)) { - // Wait for answers to arrive - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: Waiting %u ms for answers...\n"), p_u16Timeout);); - delay(p_u16Timeout); - - // All answers should have arrived by now -> stop adding new answers - pServiceQuery->m_bAwaitingAnswers = false; - u32Result = pServiceQuery->answerCount(); - } - else { // FAILED to send query - _removeServiceQuery(pServiceQuery); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: INVALID input data!\n"));); - } - return u32Result; -} - -/* - * MDNSResponder::removeQuery - * - * Remove the last static service query (and all answers). - * - */ -bool MDNSResponder::removeQuery(void) { - - return _removeLegacyServiceQuery(); -} - -/* - * MDNSResponder::queryService (LEGACY) - */ -uint32_t MDNSResponder::queryService(String p_strService, - String p_strProtocol) { - - return queryService(p_strService.c_str(), p_strProtocol.c_str()); -} - -/* - * MDNSResponder::answerHostname - */ -const char* MDNSResponder::answerHostname(const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - - if ((pSQAnswer) && - (pSQAnswer->m_HostDomain.m_u16NameLength) && - (!pSQAnswer->m_pcHostDomain)) { - - char* pcHostDomain = pSQAnswer->allocHostDomain(pSQAnswer->m_HostDomain.c_strLength()); - if (pcHostDomain) { - pSQAnswer->m_HostDomain.c_str(pcHostDomain); - } - } - return (pSQAnswer ? pSQAnswer->m_pcHostDomain : 0); -} - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::answerIP - */ - IPAddress MDNSResponder::answerIP(const uint32_t p_u32AnswerIndex) { - - const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); - const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - const stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = (((pSQAnswer) && (pSQAnswer->m_pIP4Addresses)) ? pSQAnswer->IP4AddressAtIndex(0) : 0); - return (pIP4Address ? pIP4Address->m_IPAddress : IPAddress()); - } -#endif - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::answerIP6 - */ - IPAddress MDNSResponder::answerIP6(const uint32_t p_u32AnswerIndex) { - - const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); - const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - const stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = (((pSQAnswer) && (pSQAnswer->m_pIP6Addresses)) ? pSQAnswer->IP6AddressAtIndex(0) : 0); - return (pIP6Address ? pIP6Address->m_IPAddress : IP6Address()); - } -#endif - -/* - * MDNSResponder::answerPort - */ -uint16_t MDNSResponder::answerPort(const uint32_t p_u32AnswerIndex) { - - const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); - const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return (pSQAnswer ? pSQAnswer->m_u16Port : 0); -} - -/* - * MDNSResponder::hostname (LEGACY) - */ -String MDNSResponder::hostname(const uint32_t p_u32AnswerIndex) { - - return String(answerHostname(p_u32AnswerIndex)); -} - -/* - * MDNSResponder::IP (LEGACY) - */ -IPAddress MDNSResponder::IP(const uint32_t p_u32AnswerIndex) { - - return answerIP(p_u32AnswerIndex); -} - -/* - * MDNSResponder::port (LEGACY) - */ -uint16_t MDNSResponder::port(const uint32_t p_u32AnswerIndex) { - - return answerPort(p_u32AnswerIndex); -} - - -/** - * DYNAMIC SERVICE QUERY - */ - -/* - * MDNSResponder::installServiceQuery - * - * Add a dynamic service query and a corresponding callback to the MDNS responder. - * The callback will be called for every answer update. - * The answers can also be queried by calling: - * - answerServiceDomain - * - answerHostDomain - * - answerIP4Address/answerIP6Address - * - answerPort - * - answerTxts - * - */ -MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* p_pcService, - const char* p_pcProtocol, - MDNSResponder::MDNSServiceQueryCallbackFunc p_fnCallback) { - hMDNSServiceQuery hResult = 0; - - stcMDNSServiceQuery* pServiceQuery = 0; - if ((p_pcService) && - (os_strlen(p_pcService)) && - (p_pcProtocol) && - (os_strlen(p_pcProtocol)) && - (p_fnCallback) && - ((pServiceQuery = _allocServiceQuery())) && - (_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) { - - pServiceQuery->m_fnCallback = p_fnCallback; - pServiceQuery->m_bLegacyQuery = false; - - if (_sendMDNSServiceQuery(*pServiceQuery)) { - pServiceQuery->m_u8SentCount = 1; - pServiceQuery->m_ResendTimeout.reset(MDNS_DYNAMIC_QUERY_RESEND_DELAY); - - hResult = (hMDNSServiceQuery)pServiceQuery; - } - else { - _removeServiceQuery(pServiceQuery); - } - } - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: %s for '%s.%s'!\n\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcService ?: "-"), (p_pcProtocol ?: "-"));); - DEBUG_EX_ERR(if (!hResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: FAILED for '%s.%s'!\n\n"), (p_pcService ?: "-"), (p_pcProtocol ?: "-")); } ); - return hResult; -} - -/* - * MDNSResponder::removeServiceQuery - * - * Remove a dynamic service query (and all collected answers) from the MDNS responder - * - */ -bool MDNSResponder::removeServiceQuery(MDNSResponder::hMDNSServiceQuery p_hServiceQuery) { - - stcMDNSServiceQuery* pServiceQuery = 0; - bool bResult = (((pServiceQuery = _findServiceQuery(p_hServiceQuery))) && - (_removeServiceQuery(pServiceQuery))); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceQuery: FAILED!\n")); } ); - return bResult; -} - -/* - * MDNSResponder::answerCount - */ -uint32_t MDNSResponder::answerCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - return (pServiceQuery ? pServiceQuery->answerCount() : 0); -} - -std::vector MDNSResponder::answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) { - std::vector tempVector; - for (uint32_t i=0;ianswerAtIndex(p_u32AnswerIndex) : 0); - // Fill m_pcServiceDomain (if not already done) - if ((pSQAnswer) && - (pSQAnswer->m_ServiceDomain.m_u16NameLength) && - (!pSQAnswer->m_pcServiceDomain)) { - - pSQAnswer->m_pcServiceDomain = pSQAnswer->allocServiceDomain(pSQAnswer->m_ServiceDomain.c_strLength()); - if (pSQAnswer->m_pcServiceDomain) { - pSQAnswer->m_ServiceDomain.c_str(pSQAnswer->m_pcServiceDomain); - } - } - return (pSQAnswer ? pSQAnswer->m_pcServiceDomain : 0); -} - -/* - * MDNSResponder::hasAnswerHostDomain - */ -bool MDNSResponder::hasAnswerHostDomain(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return ((pSQAnswer) && - (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostDomainAndPort)); -} - -/* - * MDNSResponder::answerHostDomain - * - * Returns the host domain for the given service. - * If not already existing, the string is allocated, filled and attached to the answer. - * - */ -const char* MDNSResponder::answerHostDomain(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - // Fill m_pcHostDomain (if not already done) - if ((pSQAnswer) && - (pSQAnswer->m_HostDomain.m_u16NameLength) && - (!pSQAnswer->m_pcHostDomain)) { - - pSQAnswer->m_pcHostDomain = pSQAnswer->allocHostDomain(pSQAnswer->m_HostDomain.c_strLength()); - if (pSQAnswer->m_pcHostDomain) { - pSQAnswer->m_HostDomain.c_str(pSQAnswer->m_pcHostDomain); - } - } - return (pSQAnswer ? pSQAnswer->m_pcHostDomain : 0); -} - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::hasAnswerIP4Address - */ - bool MDNSResponder::hasAnswerIP4Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return ((pSQAnswer) && - (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_IP4Address)); - } - - /* - * MDNSResponder::answerIP4AddressCount - */ - uint32_t MDNSResponder::answerIP4AddressCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return (pSQAnswer ? pSQAnswer->IP4AddressCount() : 0); - } - - /* - * MDNSResponder::answerIP4Address - */ - IPAddress MDNSResponder::answerIP4Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex, - const uint32_t p_u32AddressIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = (pSQAnswer ? pSQAnswer->IP4AddressAtIndex(p_u32AddressIndex) : 0); - return (pIP4Address ? pIP4Address->m_IPAddress : IPAddress()); - } -#endif - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::hasAnswerIP6Address - */ - bool MDNSResponder::hasAnswerIP6Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return ((pSQAnswer) && - (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostIP6Address)); - } - - /* - * MDNSResponder::answerIP6AddressCount - */ - uint32_t MDNSResponder::answerIP6AddressCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return (pSQAnswer ? pSQAnswer->IP6AddressCount() : 0); - } - - /* - * MDNSResponder::answerIP6Address - */ - IPAddress MDNSResponder::answerIP6Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex, - const uint32_t p_u32AddressIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = (pSQAnswer ? pSQAnswer->IP6AddressAtIndex(p_u32AddressIndex) : 0); - return (pIP6Address ? pIP6Address->m_IPAddress : IPAddress()); - } -#endif - -/* - * MDNSResponder::hasAnswerPort - */ -bool MDNSResponder::hasAnswerPort(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return ((pSQAnswer) && - (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostDomainAndPort)); -} - -/* - * MDNSResponder::answerPort - */ -uint16_t MDNSResponder::answerPort(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return (pSQAnswer ? pSQAnswer->m_u16Port : 0); -} - -/* - * MDNSResponder::hasAnswerTxts - */ -bool MDNSResponder::hasAnswerTxts(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - return ((pSQAnswer) && - (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_Txts)); -} - -/* - * MDNSResponder::answerTxts - * - * Returns all TXT items for the given service as a ';'-separated string. - * If not already existing; the string is alloced, filled and attached to the answer. - * - */ -const char* MDNSResponder::answerTxts(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - // Fill m_pcTxts (if not already done) - if ((pSQAnswer) && - (pSQAnswer->m_Txts.m_pTxts) && - (!pSQAnswer->m_pcTxts)) { - - pSQAnswer->m_pcTxts = pSQAnswer->allocTxts(pSQAnswer->m_Txts.c_strLength()); - if (pSQAnswer->m_pcTxts) { - pSQAnswer->m_Txts.c_str(pSQAnswer->m_pcTxts); - } - } - return (pSQAnswer ? pSQAnswer->m_pcTxts : 0); -} - -/* - * PROBING - */ - -/* - * MDNSResponder::setProbeResultCallback - * - * Set a global callback for probe results. The callback is called, when probing - * for the host domain (or a service domain, without specific probe result callback) - * failes or succeedes. - * In the case of failure, the domain name should be changed via 'setHostname' or 'setServiceName'. - * When succeeded, the host or service domain will be announced by the MDNS responder. - * - */ -bool MDNSResponder::setHostProbeResultCallback(MDNSResponder::MDNSHostProbeFn p_fnCallback) { - - m_HostProbeInformation.m_fnHostProbeResultCallback = p_fnCallback; - - return true; -} - -bool MDNSResponder::setHostProbeResultCallback(MDNSHostProbeFn1 pfn) { - using namespace std::placeholders; - return setHostProbeResultCallback([this, pfn](const char* p_pcDomainName, bool p_bProbeResult) { pfn(*this, p_pcDomainName, p_bProbeResult); }); -} - -/* - * MDNSResponder::setServiceProbeResultCallback - * - * Set a service specific callback for probe results. The callback is called, when probing - * for the service domain failes or succeedes. - * In the case of failure, the service name should be changed via 'setServiceName'. - * When succeeded, the service domain will be announced by the MDNS responder. - * - */ -bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, - MDNSResponder::MDNSServiceProbeFn p_fnCallback) { - - bool bResult = false; - - stcMDNSService* pService = _findService(p_hService); - if (pService) { - pService->m_ProbeInformation.m_fnServiceProbeResultCallback = p_fnCallback; - - bResult = true; - } - return bResult; -} - -bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, - MDNSResponder::MDNSServiceProbeFn1 p_fnCallback) { - using namespace std::placeholders; - return setServiceProbeResultCallback(p_hService, [this, p_fnCallback](const char* p_pcServiceName, const hMDNSService p_hMDNSService, bool p_bProbeResult) { - p_fnCallback(*this, p_pcServiceName, p_hMDNSService, p_bProbeResult); - }); -} - - -/* - * MISC - */ - -/* - * MDNSResponder::notifyAPChange - * - * Should be called, whenever the AP for the MDNS responder changes. - * A bit of this is caught by the event callbacks installed in the constructor. - * - */ -bool MDNSResponder::notifyAPChange(void) { - - return _restart(); -} - -/* - * MDNSResponder::update - * - * Should be called in every 'loop'. - * - */ -bool MDNSResponder::update(void) { - - if (m_bPassivModeEnabled) { - m_bPassivModeEnabled = false; - } - return _process(true); -} - -/* - * MDNSResponder::announce - * - * Should be called, if the 'configuration' changes. Mainly this will be changes in the TXT items... - */ -bool MDNSResponder::announce(void) { - - return (_announce(true, true)); -} - -/* - * MDNSResponder::enableArduino - * - * Enable the OTA update service. - * - */ -MDNSResponder::hMDNSService MDNSResponder::enableArduino(uint16_t p_u16Port, - bool p_bAuthUpload /*= false*/) { - - hMDNSService hService = addService(0, "arduino", "tcp", p_u16Port); - if (hService) { - if ((!addServiceTxt(hService, "tcp_check", "no")) || - (!addServiceTxt(hService, "ssh_upload", "no")) || - (!addServiceTxt(hService, "board", STRINGIZE_VALUE_OF(ARDUINO_BOARD))) || - (!addServiceTxt(hService, "auth_upload", (p_bAuthUpload) ? "yes" : "no"))) { - - removeService(hService); - hService = 0; - } - } - return hService; -} - - -} //namespace MDNSImplementation - -} //namespace esp8266 - - +/* + LEAmDNS.cpp + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +#include +#include + +#include "LEAmDNS_Priv.h" + + +namespace esp8266 +{ + +/* + LEAmDNS +*/ +namespace MDNSImplementation +{ + +/** + STRINGIZE +*/ +#ifndef STRINGIZE +#define STRINGIZE(x) #x +#endif +#ifndef STRINGIZE_VALUE_OF +#define STRINGIZE_VALUE_OF(x) STRINGIZE(x) +#endif + + +/** + INTERFACE +*/ + +/** + MDNSResponder::MDNSResponder +*/ +MDNSResponder::MDNSResponder(void) + : m_pServices(0), + m_pUDPContext(0), + m_pcHostname(0), + m_pServiceQueries(0), + m_fnServiceTxtCallback(0), +#ifdef ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE + m_bPassivModeEnabled(true), +#else + m_bPassivModeEnabled(false), +#endif + m_netif(nullptr) +{ +} + +/* + MDNSResponder::~MDNSResponder +*/ +MDNSResponder::~MDNSResponder(void) +{ + + _resetProbeStatus(false); + _releaseServiceQueries(); + _releaseHostname(); + _releaseUDPContext(); + _releaseServices(); +} + +/* + MDNSResponder::begin + + Set the host domain (for probing) and install WiFi event handlers for + IP assignment and disconnection management. In both cases, the MDNS responder + is restarted (reset and restart probe status) + Finally the responder is (re)started + +*/ +bool MDNSResponder::begin(const char* p_pcHostname, const IPAddress& p_IPAddress, uint32_t p_u32TTL) +{ + + (void)p_u32TTL; // ignored + bool bResult = false; + + if (0 == m_pUDPContext) + { + if (_setHostname(p_pcHostname)) + { + + //// select interface + + m_netif = nullptr; + IPAddress ipAddress = p_IPAddress; + + if (!ipAddress.isSet()) + { + + IPAddress sta = WiFi.localIP(); + IPAddress ap = WiFi.softAPIP(); + + if (!sta.isSet() && !ap.isSet()) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] internal interfaces (STA, AP) are not set (none was specified)\n"))); + return false; + } + + if (ap.isSet()) + { + + if (sta.isSet()) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected over STA (none was specified)\n"))); + } + else + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface AP selected\n"))); + } + ipAddress = ap; + + } + else + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] default interface STA selected (none was specified)\n"))); + ipAddress = sta; + + } + + // continue to ensure interface is UP + } + + // check existence of this IP address in the interface list + bool found = false; + m_netif = nullptr; + for (auto a : addrList) + if (ipAddress == a.addr()) + { + if (a.ifUp()) + { + found = true; + m_netif = a.interface(); + break; + } + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] found interface for IP '%s' but it is not UP\n"), ipAddress.toString().c_str());); + } + if (!found) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] interface defined by IP '%s' not found\n"), ipAddress.toString().c_str());); + return false; + } + + //// done selecting the interface + + if (m_netif->num == STATION_IF) + { + + m_GotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP & pEvent) + { + (void) pEvent; + // Ensure that _restart() runs in USER context + schedule_function([this]() + { + MDNSResponder::_restart(); + }); + }); + + m_DisconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected & pEvent) + { + (void) pEvent; + // Ensure that _restart() runs in USER context + schedule_function([this]() + { + MDNSResponder::_restart(); + }); + }); + } + + bResult = _restart(); + } + DEBUG_EX_ERR(if (!bResult) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), (p_pcHostname ? : "-")); + }); + } + else + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: Ignoring multiple calls to begin (Ignored host domain: '%s')!\n"), (p_pcHostname ? : "-"));); + } + return bResult; +} + +/* + MDNSResponder::close + + Ends the MDNS responder. + Announced services are unannounced (by multicasting a goodbye message) + +*/ +bool MDNSResponder::close(void) +{ + + m_GotIPHandler.reset(); // reset WiFi event callbacks. + m_DisconnectedHandler.reset(); + + _announce(false, true); + _resetProbeStatus(false); // Stop probing + + _releaseServiceQueries(); + _releaseUDPContext(); + _releaseHostname(); + + return true; +} + +/* + MDNSResponder::end + + Ends the MDNS responder. + for compatibility with esp32 + +*/ + +bool MDNSResponder::end(void) +{ + return close(); +} + +/* + MDNSResponder::setHostname + + Replaces the current hostname and restarts probing. + For services without own instance name (when the host name was used a instance + name), the instance names are replaced also (and the probing is restarted). + +*/ +bool MDNSResponder::setHostname(const char* p_pcHostname) +{ + + bool bResult = false; + + if (_setHostname(p_pcHostname)) + { + m_HostProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; + + // Replace 'auto-set' service names + bResult = true; + for (stcMDNSService* pService = m_pServices; ((bResult) && (pService)); pService = pService->m_pNext) + { + if (pService->m_bAutoName) + { + bResult = pService->setName(p_pcHostname); + pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; + } + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setHostname: FAILED for '%s'!\n"), (p_pcHostname ? : "-")); + }); + return bResult; +} + +/* + MDNSResponder::setHostname (LEGACY) +*/ +bool MDNSResponder::setHostname(String p_strHostname) +{ + + return setHostname(p_strHostname.c_str()); +} + + +/* + SERVICES +*/ + +/* + MDNSResponder::addService + + Add service; using hostname if no name is explicitly provided for the service + The usual '_' underline, which is prepended to service and protocol, eg. _http, + may be given. If not, it is added automatically. + +*/ +MDNSResponder::hMDNSService MDNSResponder::addService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol, + uint16_t p_u16Port) +{ + + hMDNSService hResult = 0; + + if (((!p_pcName) || // NO name OR + (MDNS_DOMAIN_LABEL_MAXLENGTH >= os_strlen(p_pcName))) && // Fitting name + (p_pcService) && + (MDNS_SERVICE_NAME_LENGTH >= os_strlen(p_pcService)) && + (p_pcProtocol) && + ((MDNS_SERVICE_PROTOCOL_LENGTH - 1) != os_strlen(p_pcProtocol)) && + (p_u16Port)) + { + + if (!_findService((p_pcName ? : m_pcHostname), p_pcService, p_pcProtocol)) // Not already used + { + if (0 != (hResult = (hMDNSService)_allocService(p_pcName, p_pcService, p_pcProtocol, p_u16Port))) + { + + // Start probing + ((stcMDNSService*)hResult)->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart; + } + } + } // else: bad arguments + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: %s to add '%s.%s.%s'!\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcName ? : "-"), p_pcService, p_pcProtocol);); + DEBUG_EX_ERR(if (!hResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addService: FAILED to add '%s.%s.%s'!\n"), (p_pcName ? : "-"), p_pcService, p_pcProtocol); + }); + return hResult; +} + +/* + MDNSResponder::removeService + + Unanounce a service (by sending a goodbye message) and remove it + from the MDNS responder + +*/ +bool MDNSResponder::removeService(const MDNSResponder::hMDNSService p_hService) +{ + + stcMDNSService* pService = 0; + bool bResult = (((pService = _findService(p_hService))) && + (_announceService(*pService, false)) && + (_releaseService(pService))); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeService: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::removeService +*/ +bool MDNSResponder::removeService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol) +{ + + return removeService((hMDNSService)_findService((p_pcName ? : m_pcHostname), p_pcService, p_pcProtocol)); +} + +/* + MDNSResponder::addService (LEGACY) +*/ +bool MDNSResponder::addService(String p_strService, + String p_strProtocol, + uint16_t p_u16Port) +{ + + return (0 != addService(m_pcHostname, p_strService.c_str(), p_strProtocol.c_str(), p_u16Port)); +} + +/* + MDNSResponder::setServiceName +*/ +bool MDNSResponder::setServiceName(const MDNSResponder::hMDNSService p_hService, + const char* p_pcInstanceName) +{ + + stcMDNSService* pService = 0; + bool bResult = (((!p_pcInstanceName) || + (MDNS_DOMAIN_LABEL_MAXLENGTH >= os_strlen(p_pcInstanceName))) && + ((pService = _findService(p_hService))) && + (pService->setName(p_pcInstanceName)) && + ((pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_ReadyToStart))); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setServiceName: FAILED for '%s'!\n"), (p_pcInstanceName ? : "-")); + }); + return bResult; +} + +/* + SERVICE TXT +*/ + +/* + MDNSResponder::addServiceTxt + + Add a static service TXT item ('Key'='Value') to a service. + +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + const char* p_pcValue) +{ + + hMDNSTxt hTxt = 0; + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + hTxt = (hMDNSTxt)_addServiceTxt(pService, p_pcKey, p_pcValue, false); + } + DEBUG_EX_ERR(if (!hTxt) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addServiceTxt: FAILED for '%s=%s'!\n"), (p_pcKey ? : "-"), (p_pcValue ? : "-")); + }); + return hTxt; +} + +/* + MDNSResponder::addServiceTxt (uint32_t) + + Formats: http://www.cplusplus.com/reference/cstdio/printf/ +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint32_t p_u32Value) +{ + char acBuffer[32]; *acBuffer = 0; + sprintf(acBuffer, "%u", p_u32Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addServiceTxt (uint16_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint16_t p_u16Value) +{ + char acBuffer[16]; *acBuffer = 0; + sprintf(acBuffer, "%hu", p_u16Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addServiceTxt (uint8_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint8_t p_u8Value) +{ + char acBuffer[8]; *acBuffer = 0; + sprintf(acBuffer, "%hhu", p_u8Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addServiceTxt (int32_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int32_t p_i32Value) +{ + char acBuffer[32]; *acBuffer = 0; + sprintf(acBuffer, "%i", p_i32Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addServiceTxt (int16_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int16_t p_i16Value) +{ + char acBuffer[16]; *acBuffer = 0; + sprintf(acBuffer, "%hi", p_i16Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addServiceTxt (int8_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int8_t p_i8Value) +{ + char acBuffer[8]; *acBuffer = 0; + sprintf(acBuffer, "%hhi", p_i8Value); + + return addServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::removeServiceTxt + + Remove a static service TXT item from a service. +*/ +bool MDNSResponder::removeServiceTxt(const MDNSResponder::hMDNSService p_hService, + const MDNSResponder::hMDNSTxt p_hTxt) +{ + + bool bResult = false; + + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_hTxt); + if (pTxt) + { + bResult = _releaseServiceTxt(pService, pTxt); + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceTxt: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::removeServiceTxt +*/ +bool MDNSResponder::removeServiceTxt(const MDNSResponder::hMDNSService p_hService, + const char* p_pcKey) +{ + + bool bResult = false; + + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_pcKey); + if (pTxt) + { + bResult = _releaseServiceTxt(pService, pTxt); + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceTxt: FAILED for '%s'!\n"), (p_pcKey ? : "-")); + }); + return bResult; +} + +/* + MDNSResponder::removeServiceTxt +*/ +bool MDNSResponder::removeServiceTxt(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol, + const char* p_pcKey) +{ + + bool bResult = false; + + stcMDNSService* pService = _findService((p_pcName ? : m_pcHostname), p_pcService, p_pcProtocol); + if (pService) + { + stcMDNSServiceTxt* pTxt = _findServiceTxt(pService, p_pcKey); + if (pTxt) + { + bResult = _releaseServiceTxt(pService, pTxt); + } + } + return bResult; +} + +/* + MDNSResponder::addServiceTxt (LEGACY) +*/ +bool MDNSResponder::addServiceTxt(const char* p_pcService, + const char* p_pcProtocol, + const char* p_pcKey, + const char* p_pcValue) +{ + + return (0 != _addServiceTxt(_findService(m_pcHostname, p_pcService, p_pcProtocol), p_pcKey, p_pcValue, false)); +} + +/* + MDNSResponder::addServiceTxt (LEGACY) +*/ +bool MDNSResponder::addServiceTxt(String p_strService, + String p_strProtocol, + String p_strKey, + String p_strValue) +{ + + return (0 != _addServiceTxt(_findService(m_pcHostname, p_strService.c_str(), p_strProtocol.c_str()), p_strKey.c_str(), p_strValue.c_str(), false)); +} + +/* + MDNSResponder::setDynamicServiceTxtCallback (global) + + Set a global callback for dynamic service TXT items. The callback is called, whenever + service TXT items are needed. + +*/ +bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) +{ + + m_fnServiceTxtCallback = p_fnCallback; + + return true; +} + +/* + MDNSResponder::setDynamicServiceTxtCallback (service specific) + + Set a service specific callback for dynamic service TXT items. The callback is called, whenever + service TXT items are needed for the given service. + +*/ +bool MDNSResponder::setDynamicServiceTxtCallback(MDNSResponder::hMDNSService p_hService, + MDNSResponder::MDNSDynamicServiceTxtCallbackFunc p_fnCallback) +{ + + bool bResult = false; + + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + pService->m_fnTxtCallback = p_fnCallback; + + bResult = true; + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] setDynamicServiceTxtCallback: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::addDynamicServiceTxt +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + const char* p_pcValue) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addDynamicServiceTxt (%s=%s)\n"), p_pcKey, p_pcValue);); + + hMDNSTxt hTxt = 0; + + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + hTxt = _addServiceTxt(pService, p_pcKey, p_pcValue, true); + } + DEBUG_EX_ERR(if (!hTxt) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] addDynamicServiceTxt: FAILED for '%s=%s'!\n"), (p_pcKey ? : "-"), (p_pcValue ? : "-")); + }); + return hTxt; +} + +/* + MDNSResponder::addDynamicServiceTxt (uint32_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint32_t p_u32Value) +{ + + char acBuffer[32]; *acBuffer = 0; + sprintf(acBuffer, "%u", p_u32Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addDynamicServiceTxt (uint16_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint16_t p_u16Value) +{ + + char acBuffer[16]; *acBuffer = 0; + sprintf(acBuffer, "%hu", p_u16Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addDynamicServiceTxt (uint8_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + uint8_t p_u8Value) +{ + + char acBuffer[8]; *acBuffer = 0; + sprintf(acBuffer, "%hhu", p_u8Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addDynamicServiceTxt (int32_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int32_t p_i32Value) +{ + + char acBuffer[32]; *acBuffer = 0; + sprintf(acBuffer, "%i", p_i32Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addDynamicServiceTxt (int16_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int16_t p_i16Value) +{ + + char acBuffer[16]; *acBuffer = 0; + sprintf(acBuffer, "%hi", p_i16Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + +/* + MDNSResponder::addDynamicServiceTxt (int8_t) +*/ +MDNSResponder::hMDNSTxt MDNSResponder::addDynamicServiceTxt(MDNSResponder::hMDNSService p_hService, + const char* p_pcKey, + int8_t p_i8Value) +{ + + char acBuffer[8]; *acBuffer = 0; + sprintf(acBuffer, "%hhi", p_i8Value); + + return addDynamicServiceTxt(p_hService, p_pcKey, acBuffer); +} + + +/** + STATIC SERVICE QUERY (LEGACY) +*/ + +/* + MDNSResponder::queryService + + Perform a (blocking) static service query. + The arrived answers can be queried by calling: + - answerHostname (or 'hostname') + - answerIP (or 'IP') + - answerPort (or 'port') + +*/ +uint32_t MDNSResponder::queryService(const char* p_pcService, + const char* p_pcProtocol, + const uint16_t p_u16Timeout /*= MDNS_QUERYSERVICES_WAIT_TIME*/) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService '%s.%s'\n"), p_pcService, p_pcProtocol);); + + uint32_t u32Result = 0; + + stcMDNSServiceQuery* pServiceQuery = 0; + if ((p_pcService) && + (os_strlen(p_pcService)) && + (p_pcProtocol) && + (os_strlen(p_pcProtocol)) && + (p_u16Timeout) && + (_removeLegacyServiceQuery()) && + ((pServiceQuery = _allocServiceQuery())) && + (_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) + { + + pServiceQuery->m_bLegacyQuery = true; + + if (_sendMDNSServiceQuery(*pServiceQuery)) + { + // Wait for answers to arrive + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: Waiting %u ms for answers...\n"), p_u16Timeout);); + delay(p_u16Timeout); + + // All answers should have arrived by now -> stop adding new answers + pServiceQuery->m_bAwaitingAnswers = false; + u32Result = pServiceQuery->answerCount(); + } + else // FAILED to send query + { + _removeServiceQuery(pServiceQuery); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] queryService: INVALID input data!\n"));); + } + return u32Result; +} + +/* + MDNSResponder::removeQuery + + Remove the last static service query (and all answers). + +*/ +bool MDNSResponder::removeQuery(void) +{ + + return _removeLegacyServiceQuery(); +} + +/* + MDNSResponder::queryService (LEGACY) +*/ +uint32_t MDNSResponder::queryService(String p_strService, + String p_strProtocol) +{ + + return queryService(p_strService.c_str(), p_strProtocol.c_str()); +} + +/* + MDNSResponder::answerHostname +*/ +const char* MDNSResponder::answerHostname(const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + + if ((pSQAnswer) && + (pSQAnswer->m_HostDomain.m_u16NameLength) && + (!pSQAnswer->m_pcHostDomain)) + { + + char* pcHostDomain = pSQAnswer->allocHostDomain(pSQAnswer->m_HostDomain.c_strLength()); + if (pcHostDomain) + { + pSQAnswer->m_HostDomain.c_str(pcHostDomain); + } + } + return (pSQAnswer ? pSQAnswer->m_pcHostDomain : 0); +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::answerIP +*/ +IPAddress MDNSResponder::answerIP(const uint32_t p_u32AnswerIndex) +{ + + const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); + const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + const stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = (((pSQAnswer) && (pSQAnswer->m_pIP4Addresses)) ? pSQAnswer->IP4AddressAtIndex(0) : 0); + return (pIP4Address ? pIP4Address->m_IPAddress : IPAddress()); +} +#endif + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::answerIP6 +*/ +IPAddress MDNSResponder::answerIP6(const uint32_t p_u32AnswerIndex) +{ + + const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); + const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + const stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = (((pSQAnswer) && (pSQAnswer->m_pIP6Addresses)) ? pSQAnswer->IP6AddressAtIndex(0) : 0); + return (pIP6Address ? pIP6Address->m_IPAddress : IP6Address()); +} +#endif + +/* + MDNSResponder::answerPort +*/ +uint16_t MDNSResponder::answerPort(const uint32_t p_u32AnswerIndex) +{ + + const stcMDNSServiceQuery* pServiceQuery = _findLegacyServiceQuery(); + const stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return (pSQAnswer ? pSQAnswer->m_u16Port : 0); +} + +/* + MDNSResponder::hostname (LEGACY) +*/ +String MDNSResponder::hostname(const uint32_t p_u32AnswerIndex) +{ + + return String(answerHostname(p_u32AnswerIndex)); +} + +/* + MDNSResponder::IP (LEGACY) +*/ +IPAddress MDNSResponder::IP(const uint32_t p_u32AnswerIndex) +{ + + return answerIP(p_u32AnswerIndex); +} + +/* + MDNSResponder::port (LEGACY) +*/ +uint16_t MDNSResponder::port(const uint32_t p_u32AnswerIndex) +{ + + return answerPort(p_u32AnswerIndex); +} + + +/** + DYNAMIC SERVICE QUERY +*/ + +/* + MDNSResponder::installServiceQuery + + Add a dynamic service query and a corresponding callback to the MDNS responder. + The callback will be called for every answer update. + The answers can also be queried by calling: + - answerServiceDomain + - answerHostDomain + - answerIP4Address/answerIP6Address + - answerPort + - answerTxts + +*/ +MDNSResponder::hMDNSServiceQuery MDNSResponder::installServiceQuery(const char* p_pcService, + const char* p_pcProtocol, + MDNSResponder::MDNSServiceQueryCallbackFunc p_fnCallback) +{ + hMDNSServiceQuery hResult = 0; + + stcMDNSServiceQuery* pServiceQuery = 0; + if ((p_pcService) && + (os_strlen(p_pcService)) && + (p_pcProtocol) && + (os_strlen(p_pcProtocol)) && + (p_fnCallback) && + ((pServiceQuery = _allocServiceQuery())) && + (_buildDomainForService(p_pcService, p_pcProtocol, pServiceQuery->m_ServiceTypeDomain))) + { + + pServiceQuery->m_fnCallback = p_fnCallback; + pServiceQuery->m_bLegacyQuery = false; + + if (_sendMDNSServiceQuery(*pServiceQuery)) + { + pServiceQuery->m_u8SentCount = 1; + pServiceQuery->m_ResendTimeout.reset(MDNS_DYNAMIC_QUERY_RESEND_DELAY); + + hResult = (hMDNSServiceQuery)pServiceQuery; + } + else + { + _removeServiceQuery(pServiceQuery); + } + } + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: %s for '%s.%s'!\n\n"), (hResult ? "Succeeded" : "FAILED"), (p_pcService ? : "-"), (p_pcProtocol ? : "-"));); + DEBUG_EX_ERR(if (!hResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] installServiceQuery: FAILED for '%s.%s'!\n\n"), (p_pcService ? : "-"), (p_pcProtocol ? : "-")); + }); + return hResult; +} + +/* + MDNSResponder::removeServiceQuery + + Remove a dynamic service query (and all collected answers) from the MDNS responder + +*/ +bool MDNSResponder::removeServiceQuery(MDNSResponder::hMDNSServiceQuery p_hServiceQuery) +{ + + stcMDNSServiceQuery* pServiceQuery = 0; + bool bResult = (((pServiceQuery = _findServiceQuery(p_hServiceQuery))) && + (_removeServiceQuery(pServiceQuery))); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] removeServiceQuery: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::answerCount +*/ +uint32_t MDNSResponder::answerCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + return (pServiceQuery ? pServiceQuery->answerCount() : 0); +} + +std::vector MDNSResponder::answerInfo(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery) +{ + std::vector tempVector; + for (uint32_t i = 0; i < answerCount(p_hServiceQuery); i++) + { + tempVector.emplace_back(*this, p_hServiceQuery, i); + } + return tempVector; +} + +/* + MDNSResponder::answerServiceDomain + + Returns the domain for the given service. + If not already existing, the string is allocated, filled and attached to the answer. + +*/ +const char* MDNSResponder::answerServiceDomain(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + // Fill m_pcServiceDomain (if not already done) + if ((pSQAnswer) && + (pSQAnswer->m_ServiceDomain.m_u16NameLength) && + (!pSQAnswer->m_pcServiceDomain)) + { + + pSQAnswer->m_pcServiceDomain = pSQAnswer->allocServiceDomain(pSQAnswer->m_ServiceDomain.c_strLength()); + if (pSQAnswer->m_pcServiceDomain) + { + pSQAnswer->m_ServiceDomain.c_str(pSQAnswer->m_pcServiceDomain); + } + } + return (pSQAnswer ? pSQAnswer->m_pcServiceDomain : 0); +} + +/* + MDNSResponder::hasAnswerHostDomain +*/ +bool MDNSResponder::hasAnswerHostDomain(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return ((pSQAnswer) && + (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostDomainAndPort)); +} + +/* + MDNSResponder::answerHostDomain + + Returns the host domain for the given service. + If not already existing, the string is allocated, filled and attached to the answer. + +*/ +const char* MDNSResponder::answerHostDomain(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + // Fill m_pcHostDomain (if not already done) + if ((pSQAnswer) && + (pSQAnswer->m_HostDomain.m_u16NameLength) && + (!pSQAnswer->m_pcHostDomain)) + { + + pSQAnswer->m_pcHostDomain = pSQAnswer->allocHostDomain(pSQAnswer->m_HostDomain.c_strLength()); + if (pSQAnswer->m_pcHostDomain) + { + pSQAnswer->m_HostDomain.c_str(pSQAnswer->m_pcHostDomain); + } + } + return (pSQAnswer ? pSQAnswer->m_pcHostDomain : 0); +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::hasAnswerIP4Address +*/ +bool MDNSResponder::hasAnswerIP4Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return ((pSQAnswer) && + (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_IP4Address)); +} + +/* + MDNSResponder::answerIP4AddressCount +*/ +uint32_t MDNSResponder::answerIP4AddressCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return (pSQAnswer ? pSQAnswer->IP4AddressCount() : 0); +} + +/* + MDNSResponder::answerIP4Address +*/ +IPAddress MDNSResponder::answerIP4Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex, + const uint32_t p_u32AddressIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = (pSQAnswer ? pSQAnswer->IP4AddressAtIndex(p_u32AddressIndex) : 0); + return (pIP4Address ? pIP4Address->m_IPAddress : IPAddress()); +} +#endif + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::hasAnswerIP6Address +*/ +bool MDNSResponder::hasAnswerIP6Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return ((pSQAnswer) && + (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostIP6Address)); +} + +/* + MDNSResponder::answerIP6AddressCount +*/ +uint32_t MDNSResponder::answerIP6AddressCount(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return (pSQAnswer ? pSQAnswer->IP6AddressCount() : 0); +} + +/* + MDNSResponder::answerIP6Address +*/ +IPAddress MDNSResponder::answerIP6Address(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex, + const uint32_t p_u32AddressIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = (pSQAnswer ? pSQAnswer->IP6AddressAtIndex(p_u32AddressIndex) : 0); + return (pIP6Address ? pIP6Address->m_IPAddress : IPAddress()); +} +#endif + +/* + MDNSResponder::hasAnswerPort +*/ +bool MDNSResponder::hasAnswerPort(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return ((pSQAnswer) && + (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_HostDomainAndPort)); +} + +/* + MDNSResponder::answerPort +*/ +uint16_t MDNSResponder::answerPort(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return (pSQAnswer ? pSQAnswer->m_u16Port : 0); +} + +/* + MDNSResponder::hasAnswerTxts +*/ +bool MDNSResponder::hasAnswerTxts(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + return ((pSQAnswer) && + (pSQAnswer->m_u32ContentFlags & ServiceQueryAnswerType_Txts)); +} + +/* + MDNSResponder::answerTxts + + Returns all TXT items for the given service as a ';'-separated string. + If not already existing; the string is alloced, filled and attached to the answer. + +*/ +const char* MDNSResponder::answerTxts(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + // Fill m_pcTxts (if not already done) + if ((pSQAnswer) && + (pSQAnswer->m_Txts.m_pTxts) && + (!pSQAnswer->m_pcTxts)) + { + + pSQAnswer->m_pcTxts = pSQAnswer->allocTxts(pSQAnswer->m_Txts.c_strLength()); + if (pSQAnswer->m_pcTxts) + { + pSQAnswer->m_Txts.c_str(pSQAnswer->m_pcTxts); + } + } + return (pSQAnswer ? pSQAnswer->m_pcTxts : 0); +} + +/* + PROBING +*/ + +/* + MDNSResponder::setProbeResultCallback + + Set a global callback for probe results. The callback is called, when probing + for the host domain (or a service domain, without specific probe result callback) + failes or succeedes. + In the case of failure, the domain name should be changed via 'setHostname' or 'setServiceName'. + When succeeded, the host or service domain will be announced by the MDNS responder. + +*/ +bool MDNSResponder::setHostProbeResultCallback(MDNSResponder::MDNSHostProbeFn p_fnCallback) +{ + + m_HostProbeInformation.m_fnHostProbeResultCallback = p_fnCallback; + + return true; +} + +bool MDNSResponder::setHostProbeResultCallback(MDNSHostProbeFn1 pfn) +{ + using namespace std::placeholders; + return setHostProbeResultCallback([this, pfn](const char* p_pcDomainName, bool p_bProbeResult) + { + pfn(*this, p_pcDomainName, p_bProbeResult); + }); +} + +/* + MDNSResponder::setServiceProbeResultCallback + + Set a service specific callback for probe results. The callback is called, when probing + for the service domain failes or succeedes. + In the case of failure, the service name should be changed via 'setServiceName'. + When succeeded, the service domain will be announced by the MDNS responder. + +*/ +bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSResponder::MDNSServiceProbeFn p_fnCallback) +{ + + bool bResult = false; + + stcMDNSService* pService = _findService(p_hService); + if (pService) + { + pService->m_ProbeInformation.m_fnServiceProbeResultCallback = p_fnCallback; + + bResult = true; + } + return bResult; +} + +bool MDNSResponder::setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSResponder::MDNSServiceProbeFn1 p_fnCallback) +{ + using namespace std::placeholders; + return setServiceProbeResultCallback(p_hService, [this, p_fnCallback](const char* p_pcServiceName, const hMDNSService p_hMDNSService, bool p_bProbeResult) + { + p_fnCallback(*this, p_pcServiceName, p_hMDNSService, p_bProbeResult); + }); +} + + +/* + MISC +*/ + +/* + MDNSResponder::notifyAPChange + + Should be called, whenever the AP for the MDNS responder changes. + A bit of this is caught by the event callbacks installed in the constructor. + +*/ +bool MDNSResponder::notifyAPChange(void) +{ + + return _restart(); +} + +/* + MDNSResponder::update + + Should be called in every 'loop'. + +*/ +bool MDNSResponder::update(void) +{ + + if (m_bPassivModeEnabled) + { + m_bPassivModeEnabled = false; + } + return _process(true); +} + +/* + MDNSResponder::announce + + Should be called, if the 'configuration' changes. Mainly this will be changes in the TXT items... +*/ +bool MDNSResponder::announce(void) +{ + + return (_announce(true, true)); +} + +/* + MDNSResponder::enableArduino + + Enable the OTA update service. + +*/ +MDNSResponder::hMDNSService MDNSResponder::enableArduino(uint16_t p_u16Port, + bool p_bAuthUpload /*= false*/) +{ + + hMDNSService hService = addService(0, "arduino", "tcp", p_u16Port); + if (hService) + { + if ((!addServiceTxt(hService, "tcp_check", "no")) || + (!addServiceTxt(hService, "ssh_upload", "no")) || + (!addServiceTxt(hService, "board", STRINGIZE_VALUE_OF(ARDUINO_BOARD))) || + (!addServiceTxt(hService, "auth_upload", (p_bAuthUpload) ? "yes" : "no"))) + { + + removeService(hService); + hService = 0; + } + } + return hService; +} + + +} //namespace MDNSImplementation + +} //namespace esp8266 + + diff --git a/libraries/ESP8266mDNS/src/LEAmDNS.h b/libraries/ESP8266mDNS/src/LEAmDNS.h index ed05a66a2..da3aa01a1 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS.h +++ b/libraries/ESP8266mDNS/src/LEAmDNS.h @@ -1,1403 +1,1456 @@ -/* - * LEAmDNS.h - * (c) 2018, LaborEtArs - * - * Version 0.9 beta - * - * Some notes (from LaborEtArs, 2018): - * Essentially, this is an rewrite of the original EPS8266 Multicast DNS code (ESP8266mDNS). - * The target of this rewrite was to keep the existing interface as stable as possible while - * adding and extending the supported set of mDNS features. - * A lot of the additions were basicly taken from Erik Ekman's lwIP mdns app code. - * - * Supported mDNS features (in some cases somewhat limited): - * - Presenting a DNS-SD service to interested observers, eg. a http server by presenting _http._tcp service - * - Support for multi-level compressed names in input; in output only a very simple one-leven full-name compression is implemented - * - Probing host and service domains for uniqueness in the local network - * - Tiebreaking while probing is supportet in a very minimalistic way (the 'higher' IP address wins the tiebreak) - * - Announcing available services after successful probing - * - Using fixed service TXT items or - * - Using dynamic service TXT items for presented services (via callback) - * - Remove services (and un-announcing them to the observers by sending goodbye-messages) - * - Static queries for DNS-SD services (creating a fixed answer set after a certain timeout period) - * - Dynamic queries for DNS-SD services with cached and updated answers and user notifications - * - * - * Usage: - * In most cases, this implementation should work as a 'drop-in' replacement for the original - * ESP8266 Multicast DNS code. Adjustments to the existing code would only be needed, if some - * of the new features should be used. - * - * For presenting services: - * In 'setup()': - * Install a callback for the probing of host (and service) domains via 'MDNS.setProbeResultCallback(probeResultCallback, &userData);' - * Register DNS-SD services with 'MDNSResponder::hMDNSService hService = MDNS.addService("MyESP", "http", "tcp", 5000);' - * (Install additional callbacks for the probing of these service domains via 'MDNS.setServiceProbeResultCallback(hService, probeResultCallback, &userData);') - * Add service TXT items with 'MDNS.addServiceTxt(hService, "c#", "1");' or by installing a service TXT callback - * using 'MDNS.setDynamicServiceTxtCallback(dynamicServiceTxtCallback, &userData);' or service specific - * 'MDNS.setDynamicServiceTxtCallback(hService, dynamicServiceTxtCallback, &userData);' - * Call MDNS.begin("MyHostname"); - * - * In 'probeResultCallback(MDNSResponder* p_MDNSResponder, const char* p_pcDomain, MDNSResponder:hMDNSService p_hService, bool p_bProbeResult, void* p_pUserdata)': - * Check the probe result and update the host or service domain name if the probe failed - * - * In 'dynamicServiceTxtCallback(MDNSResponder* p_MDNSResponder, const hMDNSService p_hService, void* p_pUserdata)': - * Add dynamic TXT items by calling 'MDNS.addDynamicServiceTxt(p_hService, "c#", "1");' - * - * In loop(): - * Call 'MDNS.update();' - * - * - * For querying services: - * Static: - * Call 'uint32_t u32AnswerCount = MDNS.queryService("http", "tcp");' - * Iterate answers by: 'for (uint32_t u=0; u // for UdpContext.h -#include "WiFiUdp.h" -#include "lwip/udp.h" -#include "debug.h" -#include "include/UdpContext.h" -#include -#include -#include - - -#include "ESP8266WiFi.h" - - -namespace esp8266 { - -/** - * LEAmDNS - */ -namespace MDNSImplementation { - -//this should be defined at build time -#ifndef ARDUINO_BOARD -#define ARDUINO_BOARD "generic" -#endif - -#define MDNS_IP4_SUPPORT -//#define MDNS_IP6_SUPPORT - - -#ifdef MDNS_IP4_SUPPORT - #define MDNS_IP4_SIZE 4 -#endif -#ifdef MDNS_IP6_SUPPORT - #define MDNS_IP6_SIZE 16 -#endif -/* - * Maximum length for all service txts for one service - */ -#define MDNS_SERVICE_TXT_MAXLENGTH 1300 -/* - * Maximum length for a full domain name eg. MyESP._http._tcp.local - */ -#define MDNS_DOMAIN_MAXLENGTH 256 -/* - * Maximum length of on label in a domain name (length info fits into 6 bits) - */ -#define MDNS_DOMAIN_LABEL_MAXLENGTH 63 -/* - * Maximum length of a service name eg. http - */ -#define MDNS_SERVICE_NAME_LENGTH 15 -/* - * Maximum length of a service protocol name eg. tcp - */ -#define MDNS_SERVICE_PROTOCOL_LENGTH 3 -/* - * Default timeout for static service queries - */ -#define MDNS_QUERYSERVICES_WAIT_TIME 1000 - - -/** - * MDNSResponder - */ -class MDNSResponder { -public: - /* INTERFACE */ - MDNSResponder(void); - virtual ~MDNSResponder(void); - - // Start the MDNS responder by setting the default hostname - // Later call MDNS::update() in every 'loop' to run the process loop - // (probing, announcing, responding, ...) - // if interfaceAddress is not specified, default interface is STA, or AP when STA is not set - bool begin(const char* p_pcHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/); - bool begin(const String& p_strHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/) {return begin(p_strHostname.c_str(), p_IPAddress, p_u32TTL);} - - // Finish MDNS processing - bool close(void); - // for esp32 compatability - bool end(void); - // Change hostname (probing is restarted) - bool setHostname(const char* p_pcHostname); - // for compatibility... - bool setHostname(String p_strHostname); - - /** - * hMDNSService (opaque handle to access the service) - */ - typedef const void* hMDNSService; - - // Add a new service to the MDNS responder. If no name (instance name) is given (p_pcName = 0) - // the current hostname is used. If the hostname is changed later, the instance names for - // these 'auto-named' services are changed to the new name also (and probing is restarted). - // The usual '_' before p_pcService (eg. http) and protocol (eg. tcp) may be given. - hMDNSService addService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol, - uint16_t p_u16Port); - // Removes a service from the MDNS responder - bool removeService(const hMDNSService p_hService); - bool removeService(const char* p_pcInstanceName, - const char* p_pcServiceName, - const char* p_pcProtocol); - // for compatibility... - bool addService(String p_strServiceName, - String p_strProtocol, - uint16_t p_u16Port); - - - // Change the services instance name (and restart probing). - bool setServiceName(const hMDNSService p_hService, - const char* p_pcInstanceName); - //for compatibility - //Warning: this has the side effect of changing the hostname. - //TODO: implement instancename different from hostname - void setInstanceName(const char* p_pcHostname) {setHostname(p_pcHostname);} - // for esp32 compatibilty - void setInstanceName(const String& s_pcHostname) {setInstanceName(s_pcHostname.c_str());} - - /** - * hMDNSTxt (opaque handle to access the TXT items) - */ - typedef void* hMDNSTxt; - - // Add a (static) MDNS TXT item ('key' = 'value') to the service - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - const char* p_pcValue); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - uint32_t p_u32Value); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - uint16_t p_u16Value); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - uint8_t p_u8Value); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - int32_t p_i32Value); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - int16_t p_i16Value); - hMDNSTxt addServiceTxt(const hMDNSService p_hService, - const char* p_pcKey, - int8_t p_i8Value); - - // Remove an existing (static) MDNS TXT item from the service - bool removeServiceTxt(const hMDNSService p_hService, - const hMDNSTxt p_hTxt); - bool removeServiceTxt(const hMDNSService p_hService, - const char* p_pcKey); - bool removeServiceTxt(const char* p_pcinstanceName, - const char* p_pcServiceName, - const char* p_pcProtocol, - const char* p_pcKey); - // for compatibility... - bool addServiceTxt(const char* p_pcService, - const char* p_pcProtocol, - const char* p_pcKey, - const char* p_pcValue); - bool addServiceTxt(String p_strService, - String p_strProtocol, - String p_strKey, - String p_strValue); - - /** - * MDNSDynamicServiceTxtCallbackFn - * Callback function for dynamic MDNS TXT items - */ - - typedef std::function MDNSDynamicServiceTxtCallbackFunc; - - // Set a global callback for dynamic MDNS TXT items. The callback function is called - // every time, a TXT item is needed for one of the installed services. - bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFunc p_fnCallback); - // Set a service specific callback for dynamic MDNS TXT items. The callback function - // is called every time, a TXT item is needed for the given service. - bool setDynamicServiceTxtCallback(const hMDNSService p_hService, - MDNSDynamicServiceTxtCallbackFunc p_fnCallback); - - // Add a (dynamic) MDNS TXT item ('key' = 'value') to the service - // Dynamic TXT items are removed right after one-time use. So they need to be added - // every time the value s needed (via callback). - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - const char* p_pcValue); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - uint32_t p_u32Value); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - uint16_t p_u16Value); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - uint8_t p_u8Value); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - int32_t p_i32Value); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - int16_t p_i16Value); - hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, - const char* p_pcKey, - int8_t p_i8Value); - - // Perform a (static) service query. The function returns after p_u16Timeout milliseconds - // The answers (the number of received answers is returned) can be retrieved by calling - // - answerHostname (or hostname) - // - answerIP (or IP) - // - answerPort (or port) - uint32_t queryService(const char* p_pcService, - const char* p_pcProtocol, - const uint16_t p_u16Timeout = MDNS_QUERYSERVICES_WAIT_TIME); - bool removeQuery(void); - // for compatibility... - uint32_t queryService(String p_strService, - String p_strProtocol); - - const char* answerHostname(const uint32_t p_u32AnswerIndex); - IPAddress answerIP(const uint32_t p_u32AnswerIndex); - uint16_t answerPort(const uint32_t p_u32AnswerIndex); - // for compatibility... - String hostname(const uint32_t p_u32AnswerIndex); - IPAddress IP(const uint32_t p_u32AnswerIndex); - uint16_t port(const uint32_t p_u32AnswerIndex); - - /** - * hMDNSServiceQuery (opaque handle to access dynamic service queries) - */ - typedef const void* hMDNSServiceQuery; - - /** - * enuServiceQueryAnswerType - */ - typedef enum _enuServiceQueryAnswerType { - ServiceQueryAnswerType_ServiceDomain = (1 << 0), // Service instance name - ServiceQueryAnswerType_HostDomainAndPort = (1 << 1), // Host domain and service port - ServiceQueryAnswerType_Txts = (1 << 2), // TXT items -#ifdef MDNS_IP4_SUPPORT - ServiceQueryAnswerType_IP4Address = (1 << 3), // IP4 address -#endif -#ifdef MDNS_IP6_SUPPORT - ServiceQueryAnswerType_IP6Address = (1 << 4), // IP6 address -#endif - } enuServiceQueryAnswerType; - - enum class AnswerType : uint32_t { - Unknown = 0, - ServiceDomain = ServiceQueryAnswerType_ServiceDomain, - HostDomainAndPort = ServiceQueryAnswerType_HostDomainAndPort, - Txt = ServiceQueryAnswerType_Txts, -#ifdef MDNS_IP4_SUPPORT - IP4Address = ServiceQueryAnswerType_IP4Address, -#endif -#ifdef MDNS_IP6_SUPPORT - IP6Address = ServiceQueryAnswerType_IP6Address, -#endif - }; - - /** - * MDNSServiceQueryCallbackFn - * Callback function for received answers for dynamic service queries - */ - struct MDNSServiceInfo; // forward declaration - typedef std::function MDNSServiceQueryCallbackFunc; - - // Install a dynamic service query. For every received answer (part) the given callback - // function is called. The query will be updated every time, the TTL for an answer - // has timed-out. - // The answers can also be retrieved by calling - // - answerCount - // - answerServiceDomain - // - hasAnswerHostDomain/answerHostDomain - // - hasAnswerIP4Address/answerIP4Address - // - hasAnswerIP6Address/answerIP6Address - // - hasAnswerPort/answerPort - // - hasAnswerTxts/answerTxts - hMDNSServiceQuery installServiceQuery(const char* p_pcService, - const char* p_pcProtocol, - MDNSServiceQueryCallbackFunc p_fnCallback); - // Remove a dynamic service query - bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery); - - uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery); - std::vector answerInfo (const MDNSResponder::hMDNSServiceQuery p_hServiceQuery); - - const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - const char* answerHostDomain(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); -#ifdef MDNS_IP4_SUPPORT - bool hasAnswerIP4Address(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - uint32_t answerIP4AddressCount(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - IPAddress answerIP4Address(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex, - const uint32_t p_u32AddressIndex); -#endif -#ifdef MDNS_IP6_SUPPORT - bool hasAnswerIP6Address(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - uint32_t answerIP6AddressCount(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - IPAddress answerIP6Address(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex, - const uint32_t p_u32AddressIndex); -#endif - bool hasAnswerPort(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - uint16_t answerPort(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - bool hasAnswerTxts(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - // Get the TXT items as a ';'-separated string - const char* answerTxts(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - - /** - * MDNSProbeResultCallbackFn - * Callback function for (host and service domain) probe results - */ - typedef std::function MDNSHostProbeFn; - - typedef std::function MDNSHostProbeFn1; - - typedef std::function MDNSServiceProbeFn; - - typedef std::function MDNSServiceProbeFn1; - - // Set a global callback function for host and service probe results - // The callback function is called, when the probing for the host domain - // (or a service domain, which hasn't got a service specific callback) - // Succeeds or fails. - // In case of failure, the failed domain name should be changed. - bool setHostProbeResultCallback(MDNSHostProbeFn p_fnCallback); - bool setHostProbeResultCallback(MDNSHostProbeFn1 p_fnCallback); - - // Set a service specific probe result callback - bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, - MDNSServiceProbeFn p_fnCallback); - bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, - MDNSServiceProbeFn1 p_fnCallback); - - // Application should call this whenever AP is configured/disabled - bool notifyAPChange(void); - - // 'update' should be called in every 'loop' to run the MDNS processing - bool update(void); - - // 'announce' can be called every time, the configuration of some service - // changes. Mainly, this would be changed content of TXT items. - bool announce(void); - - // Enable OTA update - hMDNSService enableArduino(uint16_t p_u16Port, - bool p_bAuthUpload = false); - - // Domain name helper - static bool indexDomain(char*& p_rpcDomain, - const char* p_pcDivider = "-", - const char* p_pcDefaultDomain = 0); - - /** STRUCTS **/ - -public: - /** - * MDNSServiceInfo, used in application callbacks - */ - struct MDNSServiceInfo - { - MDNSServiceInfo(MDNSResponder& p_pM,MDNSResponder::hMDNSServiceQuery p_hS,uint32_t p_u32A) - : p_pMDNSResponder(p_pM), - p_hServiceQuery(p_hS), - p_u32AnswerIndex(p_u32A) - {}; - struct CompareKey - { - bool operator()(char const *a, char const *b) const - { - return strcmp(a, b) < 0; - } - }; - using KeyValueMap = std::map; - protected: - MDNSResponder& p_pMDNSResponder; - MDNSResponder::hMDNSServiceQuery p_hServiceQuery; - uint32_t p_u32AnswerIndex; - KeyValueMap keyValueMap; - public: - const char* serviceDomain(){ - return p_pMDNSResponder.answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex); - }; - bool hostDomainAvailable() - { - return (p_pMDNSResponder.hasAnswerHostDomain(p_hServiceQuery, p_u32AnswerIndex)); - } - const char* hostDomain(){ - return (hostDomainAvailable()) ? - p_pMDNSResponder.answerHostDomain(p_hServiceQuery, p_u32AnswerIndex) : nullptr; - }; - bool hostPortAvailable() - { - return (p_pMDNSResponder.hasAnswerPort(p_hServiceQuery, p_u32AnswerIndex)); - } - uint16_t hostPort(){ - return (hostPortAvailable()) ? - p_pMDNSResponder.answerPort(p_hServiceQuery, p_u32AnswerIndex) : 0; - }; - bool IP4AddressAvailable() - { - return (p_pMDNSResponder.hasAnswerIP4Address(p_hServiceQuery,p_u32AnswerIndex )); - } - std::vector IP4Adresses(){ - std::vector internalIP; - if (IP4AddressAvailable()) { - uint16_t cntIP4Adress = p_pMDNSResponder.answerIP4AddressCount(p_hServiceQuery, p_u32AnswerIndex); - for (uint32_t u2 = 0; u2 < cntIP4Adress; ++u2) { - internalIP.emplace_back(p_pMDNSResponder.answerIP4Address(p_hServiceQuery, p_u32AnswerIndex, u2)); - } - } - return internalIP; - }; - bool txtAvailable() - { - return (p_pMDNSResponder.hasAnswerTxts(p_hServiceQuery, p_u32AnswerIndex)); - } - const char* strKeyValue (){ - return (txtAvailable()) ? - p_pMDNSResponder.answerTxts(p_hServiceQuery, p_u32AnswerIndex) : nullptr; - }; - const KeyValueMap& keyValues() - { - if (txtAvailable() && keyValueMap.size() == 0) - { - for (auto kv = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex);kv != nullptr;kv = kv->m_pNext) { - keyValueMap.emplace(std::pair(kv->m_pcKey,kv->m_pcValue)); - } - } - return keyValueMap; - } - const char* value(const char* key) - { - char* result = nullptr; - - for (stcMDNSServiceTxt* pTxt=p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); pTxt; pTxt=pTxt->m_pNext) { - if ((key) && - (0 == strcmp(pTxt->m_pcKey, key))) { - result = pTxt->m_pcValue; - break; - } - } - return result; - } - }; -protected: - - /** - * stcMDNSServiceTxt - */ - struct stcMDNSServiceTxt { - stcMDNSServiceTxt* m_pNext; - char* m_pcKey; - char* m_pcValue; - bool m_bTemp; - - stcMDNSServiceTxt(const char* p_pcKey = 0, - const char* p_pcValue = 0, - bool p_bTemp = false); - stcMDNSServiceTxt(const stcMDNSServiceTxt& p_Other); - ~stcMDNSServiceTxt(void); - - stcMDNSServiceTxt& operator=(const stcMDNSServiceTxt& p_Other); - bool clear(void); - - char* allocKey(size_t p_stLength); - bool setKey(const char* p_pcKey, - size_t p_stLength); - bool setKey(const char* p_pcKey); - bool releaseKey(void); - - char* allocValue(size_t p_stLength); - bool setValue(const char* p_pcValue, - size_t p_stLength); - bool setValue(const char* p_pcValue); - bool releaseValue(void); - - bool set(const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp = false); - - bool update(const char* p_pcValue); - - size_t length(void) const; - }; - - /** - * stcMDNSTxts - */ - struct stcMDNSServiceTxts { - stcMDNSServiceTxt* m_pTxts; - - stcMDNSServiceTxts(void); - stcMDNSServiceTxts(const stcMDNSServiceTxts& p_Other); - ~stcMDNSServiceTxts(void); - - stcMDNSServiceTxts& operator=(const stcMDNSServiceTxts& p_Other); - - bool clear(void); - - bool add(stcMDNSServiceTxt* p_pTxt); - bool remove(stcMDNSServiceTxt* p_pTxt); - - bool removeTempTxts(void); - - stcMDNSServiceTxt* find(const char* p_pcKey); - const stcMDNSServiceTxt* find(const char* p_pcKey) const; - stcMDNSServiceTxt* find(const stcMDNSServiceTxt* p_pTxt); - - uint16_t length(void) const; - - size_t c_strLength(void) const; - bool c_str(char* p_pcBuffer); - - size_t bufferLength(void) const; - bool buffer(char* p_pcBuffer); - - bool compare(const stcMDNSServiceTxts& p_Other) const; - bool operator==(const stcMDNSServiceTxts& p_Other) const; - bool operator!=(const stcMDNSServiceTxts& p_Other) const; - }; - - /** - * enuContentFlags - */ - typedef enum _enuContentFlags { - // Host - ContentFlag_A = 0x01, - ContentFlag_PTR_IP4 = 0x02, - ContentFlag_PTR_IP6 = 0x04, - ContentFlag_AAAA = 0x08, - // Service - ContentFlag_PTR_TYPE = 0x10, - ContentFlag_PTR_NAME = 0x20, - ContentFlag_TXT = 0x40, - ContentFlag_SRV = 0x80, - } enuContentFlags; - - /** - * stcMDNS_MsgHeader - */ - struct stcMDNS_MsgHeader { - uint16_t m_u16ID; // Identifier - bool m_1bQR : 1; // Query/Response flag - unsigned char m_4bOpcode : 4; // Operation code - bool m_1bAA : 1; // Authoritative Answer flag - bool m_1bTC : 1; // Truncation flag - bool m_1bRD : 1; // Recursion desired - bool m_1bRA : 1; // Recursion available - unsigned char m_3bZ : 3; // Zero - unsigned char m_4bRCode : 4; // Response code - uint16_t m_u16QDCount; // Question count - uint16_t m_u16ANCount; // Answer count - uint16_t m_u16NSCount; // Authority Record count - uint16_t m_u16ARCount; // Additional Record count - - stcMDNS_MsgHeader(uint16_t p_u16ID = 0, - bool p_bQR = false, - unsigned char p_ucOpcode = 0, - bool p_bAA = false, - bool p_bTC = false, - bool p_bRD = false, - bool p_bRA = false, - unsigned char p_ucRCode = 0, - uint16_t p_u16QDCount = 0, - uint16_t p_u16ANCount = 0, - uint16_t p_u16NSCount = 0, - uint16_t p_u16ARCount = 0); - }; - - /** - * stcMDNS_RRDomain - */ - struct stcMDNS_RRDomain { - char m_acName[MDNS_DOMAIN_MAXLENGTH]; // Encoded domain name - uint16_t m_u16NameLength; // Length (incl. '\0') - - stcMDNS_RRDomain(void); - stcMDNS_RRDomain(const stcMDNS_RRDomain& p_Other); - - stcMDNS_RRDomain& operator=(const stcMDNS_RRDomain& p_Other); - - bool clear(void); - - bool addLabel(const char* p_pcLabel, - bool p_bPrependUnderline = false); - - bool compare(const stcMDNS_RRDomain& p_Other) const; - bool operator==(const stcMDNS_RRDomain& p_Other) const; - bool operator!=(const stcMDNS_RRDomain& p_Other) const; - bool operator>(const stcMDNS_RRDomain& p_Other) const; - - size_t c_strLength(void) const; - bool c_str(char* p_pcBuffer); - }; - - /** - * stcMDNS_RRAttributes - */ - struct stcMDNS_RRAttributes { - uint16_t m_u16Type; // Type - uint16_t m_u16Class; // Class, nearly always 'IN' - - stcMDNS_RRAttributes(uint16_t p_u16Type = 0, - uint16_t p_u16Class = 1 /*DNS_RRCLASS_IN Internet*/); - stcMDNS_RRAttributes(const stcMDNS_RRAttributes& p_Other); - - stcMDNS_RRAttributes& operator=(const stcMDNS_RRAttributes& p_Other); - }; - - /** - * stcMDNS_RRHeader - */ - struct stcMDNS_RRHeader { - stcMDNS_RRDomain m_Domain; - stcMDNS_RRAttributes m_Attributes; - - stcMDNS_RRHeader(void); - stcMDNS_RRHeader(const stcMDNS_RRHeader& p_Other); - - stcMDNS_RRHeader& operator=(const stcMDNS_RRHeader& p_Other); - - bool clear(void); - }; - - /** - * stcMDNS_RRQuestion - */ - struct stcMDNS_RRQuestion { - stcMDNS_RRQuestion* m_pNext; - stcMDNS_RRHeader m_Header; - bool m_bUnicast; // Unicast reply requested - - stcMDNS_RRQuestion(void); - }; - - /** - * enuAnswerType - */ - typedef enum _enuAnswerType { - AnswerType_A, - AnswerType_PTR, - AnswerType_TXT, - AnswerType_AAAA, - AnswerType_SRV, - AnswerType_Generic - } enuAnswerType; - - /** - * stcMDNS_RRAnswer - */ - struct stcMDNS_RRAnswer { - stcMDNS_RRAnswer* m_pNext; - const enuAnswerType m_AnswerType; - stcMDNS_RRHeader m_Header; - bool m_bCacheFlush; // Cache flush command bit - uint32_t m_u32TTL; // Validity time in seconds - - virtual ~stcMDNS_RRAnswer(void); - - enuAnswerType answerType(void) const; - - bool clear(void); - - protected: - stcMDNS_RRAnswer(enuAnswerType p_AnswerType, - const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - }; - -#ifdef MDNS_IP4_SUPPORT - /** - * stcMDNS_RRAnswerA - */ - struct stcMDNS_RRAnswerA : public stcMDNS_RRAnswer { - IPAddress m_IPAddress; - - stcMDNS_RRAnswerA(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerA(void); - - bool clear(void); - }; -#endif - - /** - * stcMDNS_RRAnswerPTR - */ - struct stcMDNS_RRAnswerPTR : public stcMDNS_RRAnswer { - stcMDNS_RRDomain m_PTRDomain; - - stcMDNS_RRAnswerPTR(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerPTR(void); - - bool clear(void); - }; - - /** - * stcMDNS_RRAnswerTXT - */ - struct stcMDNS_RRAnswerTXT : public stcMDNS_RRAnswer { - stcMDNSServiceTxts m_Txts; - - stcMDNS_RRAnswerTXT(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerTXT(void); - - bool clear(void); - }; - -#ifdef MDNS_IP6_SUPPORT - /** - * stcMDNS_RRAnswerAAAA - */ - struct stcMDNS_RRAnswerAAAA : public stcMDNS_RRAnswer { - //TODO: IP6Address m_IPAddress; - - stcMDNS_RRAnswerAAAA(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerAAAA(void); - - bool clear(void); - }; -#endif - - /** - * stcMDNS_RRAnswerSRV - */ - struct stcMDNS_RRAnswerSRV : public stcMDNS_RRAnswer { - uint16_t m_u16Priority; - uint16_t m_u16Weight; - uint16_t m_u16Port; - stcMDNS_RRDomain m_SRVDomain; - - stcMDNS_RRAnswerSRV(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerSRV(void); - - bool clear(void); - }; - - /** - * stcMDNS_RRAnswerGeneric - */ - struct stcMDNS_RRAnswerGeneric : public stcMDNS_RRAnswer { - uint16_t m_u16RDLength; // Length of variable answer - uint8_t* m_pu8RDData; // Offset of start of variable answer in packet - - stcMDNS_RRAnswerGeneric(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL); - ~stcMDNS_RRAnswerGeneric(void); - - bool clear(void); - }; - - - /** - * enuProbingStatus - */ - typedef enum _enuProbingStatus { - ProbingStatus_WaitingForData, - ProbingStatus_ReadyToStart, - ProbingStatus_InProgress, - ProbingStatus_Done - } enuProbingStatus; - - /** - * stcProbeInformation - */ - struct stcProbeInformation { - enuProbingStatus m_ProbingStatus; - uint8_t m_u8SentCount; // Used for probes and announcements - esp8266::polledTimeout::oneShotMs m_Timeout; // Used for probes and announcements - //clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements - bool m_bConflict; - bool m_bTiebreakNeeded; - MDNSHostProbeFn m_fnHostProbeResultCallback; - MDNSServiceProbeFn m_fnServiceProbeResultCallback; - - stcProbeInformation(void); - - bool clear(bool p_bClearUserdata = false); - }; - - - /** - * stcMDNSService - */ - struct stcMDNSService { - stcMDNSService* m_pNext; - char* m_pcName; - bool m_bAutoName; // Name was set automatically to hostname (if no name was supplied) - char* m_pcService; - char* m_pcProtocol; - uint16_t m_u16Port; - uint8_t m_u8ReplyMask; - stcMDNSServiceTxts m_Txts; - MDNSDynamicServiceTxtCallbackFunc m_fnTxtCallback; - stcProbeInformation m_ProbeInformation; - - stcMDNSService(const char* p_pcName = 0, - const char* p_pcService = 0, - const char* p_pcProtocol = 0); - ~stcMDNSService(void); - - bool setName(const char* p_pcName); - bool releaseName(void); - - bool setService(const char* p_pcService); - bool releaseService(void); - - bool setProtocol(const char* p_pcProtocol); - bool releaseProtocol(void); - }; - - /** - * stcMDNSServiceQuery - */ - struct stcMDNSServiceQuery { - /** - * stcAnswer - */ - struct stcAnswer { - /** - * stcTTL - */ - struct stcTTL { - /** - * timeoutLevel_t - */ - typedef uint8_t timeoutLevel_t; - /** - * TIMEOUTLEVELs - */ - const timeoutLevel_t TIMEOUTLEVEL_UNSET = 0; - const timeoutLevel_t TIMEOUTLEVEL_BASE = 80; - const timeoutLevel_t TIMEOUTLEVEL_INTERVAL = 5; - const timeoutLevel_t TIMEOUTLEVEL_FINAL = 100; - - uint32_t m_u32TTL; - esp8266::polledTimeout::oneShotMs m_TTLTimeout; - timeoutLevel_t m_timeoutLevel; - - stcTTL(void); - bool set(uint32_t p_u32TTL); - - bool flagged(void); - bool restart(void); - - bool prepareDeletion(void); - bool finalTimeoutLevel(void) const; - - unsigned long timeout(void) const; - }; -#ifdef MDNS_IP4_SUPPORT - /** - * stcIP4Address - */ - struct stcIP4Address { - stcIP4Address* m_pNext; - IPAddress m_IPAddress; - stcTTL m_TTL; - - stcIP4Address(IPAddress p_IPAddress, - uint32_t p_u32TTL = 0); - }; -#endif -#ifdef MDNS_IP6_SUPPORT - /** - * stcIP6Address - */ - struct stcIP6Address { - stcIP6Address* m_pNext; - IP6Address m_IPAddress; - stcTTL m_TTL; - - stcIP6Address(IPAddress p_IPAddress, - uint32_t p_u32TTL = 0); - }; -#endif - - stcAnswer* m_pNext; - // The service domain is the first 'answer' (from PTR answer, using service and protocol) to be set - // Defines the key for additional answer, like host domain, etc. - stcMDNS_RRDomain m_ServiceDomain; // 1. level answer (PTR), eg. MyESP._http._tcp.local - char* m_pcServiceDomain; - stcTTL m_TTLServiceDomain; - stcMDNS_RRDomain m_HostDomain; // 2. level answer (SRV, using service domain), eg. esp8266.local - char* m_pcHostDomain; - uint16_t m_u16Port; // 2. level answer (SRV, using service domain), eg. 5000 - stcTTL m_TTLHostDomainAndPort; - stcMDNSServiceTxts m_Txts; // 2. level answer (TXT, using service domain), eg. c#=1 - char* m_pcTxts; - stcTTL m_TTLTxts; -#ifdef MDNS_IP4_SUPPORT - stcIP4Address* m_pIP4Addresses; // 3. level answer (A, using host domain), eg. 123.456.789.012 -#endif -#ifdef MDNS_IP6_SUPPORT - stcIP6Address* m_pIP6Addresses; // 3. level answer (AAAA, using host domain), eg. 1234::09 -#endif - uint32_t m_u32ContentFlags; - - stcAnswer(void); - ~stcAnswer(void); - - bool clear(void); - - char* allocServiceDomain(size_t p_stLength); - bool releaseServiceDomain(void); - - char* allocHostDomain(size_t p_stLength); - bool releaseHostDomain(void); - - char* allocTxts(size_t p_stLength); - bool releaseTxts(void); - -#ifdef MDNS_IP4_SUPPORT - bool releaseIP4Addresses(void); - bool addIP4Address(stcIP4Address* p_pIP4Address); - bool removeIP4Address(stcIP4Address* p_pIP4Address); - const stcIP4Address* findIP4Address(const IPAddress& p_IPAddress) const; - stcIP4Address* findIP4Address(const IPAddress& p_IPAddress); - uint32_t IP4AddressCount(void) const; - const stcIP4Address* IP4AddressAtIndex(uint32_t p_u32Index) const; - stcIP4Address* IP4AddressAtIndex(uint32_t p_u32Index); -#endif -#ifdef MDNS_IP6_SUPPORT - bool releaseIP6Addresses(void); - bool addIP6Address(stcIP6Address* p_pIP6Address); - bool removeIP6Address(stcIP6Address* p_pIP6Address); - const stcIP6Address* findIP6Address(const IPAddress& p_IPAddress) const; - stcIP6Address* findIP6Address(const IPAddress& p_IPAddress); - uint32_t IP6AddressCount(void) const; - const stcIP6Address* IP6AddressAtIndex(uint32_t p_u32Index) const; - stcIP6Address* IP6AddressAtIndex(uint32_t p_u32Index); -#endif - }; - - stcMDNSServiceQuery* m_pNext; - stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local - MDNSServiceQueryCallbackFunc m_fnCallback; - bool m_bLegacyQuery; - uint8_t m_u8SentCount; - esp8266::polledTimeout::oneShotMs m_ResendTimeout; - bool m_bAwaitingAnswers; - stcAnswer* m_pAnswers; - - stcMDNSServiceQuery(void); - ~stcMDNSServiceQuery(void); - - bool clear(void); - - uint32_t answerCount(void) const; - const stcAnswer* answerAtIndex(uint32_t p_u32Index) const; - stcAnswer* answerAtIndex(uint32_t p_u32Index); - uint32_t indexOfAnswer(const stcAnswer* p_pAnswer) const; - - bool addAnswer(stcAnswer* p_pAnswer); - bool removeAnswer(stcAnswer* p_pAnswer); - - stcAnswer* findAnswerForServiceDomain(const stcMDNS_RRDomain& p_ServiceDomain); - stcAnswer* findAnswerForHostDomain(const stcMDNS_RRDomain& p_HostDomain); - }; - - /** - * stcMDNSSendParameter - */ - struct stcMDNSSendParameter { - protected: - /** - * stcDomainCacheItem - */ - struct stcDomainCacheItem { - stcDomainCacheItem* m_pNext; - const void* m_pHostnameOrService; // Opaque id for host or service domain (pointer) - bool m_bAdditionalData; // Opaque flag for special info (service domain included) - uint16_t m_u16Offset; // Offset in UDP output buffer - - stcDomainCacheItem(const void* p_pHostnameOrService, - bool p_bAdditionalData, - uint32_t p_u16Offset); - }; - - public: - uint16_t m_u16ID; // Query ID (used only in lagacy queries) - stcMDNS_RRQuestion* m_pQuestions; // A list of queries - uint8_t m_u8HostReplyMask; // Flags for reply components/answers - bool m_bLegacyQuery; // Flag: Legacy query - bool m_bResponse; // Flag: Response to a query - bool m_bAuthorative; // Flag: Authorative (owner) response - bool m_bCacheFlush; // Flag: Clients should flush their caches - bool m_bUnicast; // Flag: Unicast response - bool m_bUnannounce; // Flag: Unannounce service - uint16_t m_u16Offset; // Current offset in UDP write buffer (mainly for domain cache) - stcDomainCacheItem* m_pDomainCacheItems; // Cached host and service domains - - stcMDNSSendParameter(void); - ~stcMDNSSendParameter(void); - - bool clear(void); - - bool shiftOffset(uint16_t p_u16Shift); - - bool addDomainCacheItem(const void* p_pHostnameOrService, - bool p_bAdditionalData, - uint16_t p_u16Offset); - uint16_t findCachedDomainOffset(const void* p_pHostnameOrService, - bool p_bAdditionalData) const; - }; - - // Instance variables - stcMDNSService* m_pServices; - UdpContext* m_pUDPContext; - char* m_pcHostname; - stcMDNSServiceQuery* m_pServiceQueries; - WiFiEventHandler m_DisconnectedHandler; - WiFiEventHandler m_GotIPHandler; - MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback; - bool m_bPassivModeEnabled; - stcProbeInformation m_HostProbeInformation; - CONST netif* m_netif; // network interface to run on - - /** CONTROL **/ - /* MAINTENANCE */ - bool _process(bool p_bUserContext); - bool _restart(void); - - /* RECEIVING */ - bool _parseMessage(void); - bool _parseQuery(const stcMDNS_MsgHeader& p_Header); - - bool _parseResponse(const stcMDNS_MsgHeader& p_Header); - bool _processAnswers(const stcMDNS_RRAnswer* p_pPTRAnswers); - bool _processPTRAnswer(const stcMDNS_RRAnswerPTR* p_pPTRAnswer, - bool& p_rbFoundNewKeyAnswer); - bool _processSRVAnswer(const stcMDNS_RRAnswerSRV* p_pSRVAnswer, - bool& p_rbFoundNewKeyAnswer); - bool _processTXTAnswer(const stcMDNS_RRAnswerTXT* p_pTXTAnswer); -#ifdef MDNS_IP4_SUPPORT - bool _processAAnswer(const stcMDNS_RRAnswerA* p_pAAnswer); -#endif -#ifdef MDNS_IP6_SUPPORT - bool _processAAAAAnswer(const stcMDNS_RRAnswerAAAA* p_pAAAAAnswer); -#endif - - /* PROBING */ - bool _updateProbeStatus(void); - bool _resetProbeStatus(bool p_bRestart = true); - bool _hasProbesWaitingForAnswers(void) const; - bool _sendHostProbe(void); - bool _sendServiceProbe(stcMDNSService& p_rService); - bool _cancelProbingForHost(void); - bool _cancelProbingForService(stcMDNSService& p_rService); - - /* ANNOUNCE */ - bool _announce(bool p_bAnnounce, - bool p_bIncludeServices); - bool _announceService(stcMDNSService& p_rService, - bool p_bAnnounce = true); - - /* SERVICE QUERY CACHE */ - bool _hasServiceQueriesWaitingForAnswers(void) const; - bool _checkServiceQueryCache(void); - - /** TRANSFER **/ - /* SENDING */ - bool _sendMDNSMessage(stcMDNSSendParameter& p_SendParameter); - bool _sendMDNSMessage_Multicast(MDNSResponder::stcMDNSSendParameter& p_rSendParameter); - bool _prepareMDNSMessage(stcMDNSSendParameter& p_SendParameter, - IPAddress p_IPAddress); - bool _sendMDNSServiceQuery(const stcMDNSServiceQuery& p_ServiceQuery); - bool _sendMDNSQuery(const stcMDNS_RRDomain& p_QueryDomain, - uint16_t p_u16QueryType, - stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers = 0); - - const IPAddress _getResponseMulticastInterface() const { return IPAddress(m_netif->ip_addr); } - - uint8_t _replyMaskForHost(const stcMDNS_RRHeader& p_RRHeader, - bool* p_pbFullNameMatch = 0) const; - uint8_t _replyMaskForService(const stcMDNS_RRHeader& p_RRHeader, - const stcMDNSService& p_Service, - bool* p_pbFullNameMatch = 0) const; - - /* RESOURCE RECORD */ - bool _readRRQuestion(stcMDNS_RRQuestion& p_rQuestion); - bool _readRRAnswer(stcMDNS_RRAnswer*& p_rpAnswer); -#ifdef MDNS_IP4_SUPPORT - bool _readRRAnswerA(stcMDNS_RRAnswerA& p_rRRAnswerA, - uint16_t p_u16RDLength); -#endif - bool _readRRAnswerPTR(stcMDNS_RRAnswerPTR& p_rRRAnswerPTR, - uint16_t p_u16RDLength); - bool _readRRAnswerTXT(stcMDNS_RRAnswerTXT& p_rRRAnswerTXT, - uint16_t p_u16RDLength); -#ifdef MDNS_IP6_SUPPORT - bool _readRRAnswerAAAA(stcMDNS_RRAnswerAAAA& p_rRRAnswerAAAA, - uint16_t p_u16RDLength); -#endif - bool _readRRAnswerSRV(stcMDNS_RRAnswerSRV& p_rRRAnswerSRV, - uint16_t p_u16RDLength); - bool _readRRAnswerGeneric(stcMDNS_RRAnswerGeneric& p_rRRAnswerGeneric, - uint16_t p_u16RDLength); - - bool _readRRHeader(stcMDNS_RRHeader& p_rHeader); - bool _readRRDomain(stcMDNS_RRDomain& p_rRRDomain); - bool _readRRDomain_Loop(stcMDNS_RRDomain& p_rRRDomain, - uint8_t p_u8Depth); - bool _readRRAttributes(stcMDNS_RRAttributes& p_rAttributes); - - /* DOMAIN NAMES */ - bool _buildDomainForHost(const char* p_pcHostname, - stcMDNS_RRDomain& p_rHostDomain) const; - bool _buildDomainForDNSSD(stcMDNS_RRDomain& p_rDNSSDDomain) const; - bool _buildDomainForService(const stcMDNSService& p_Service, - bool p_bIncludeName, - stcMDNS_RRDomain& p_rServiceDomain) const; - bool _buildDomainForService(const char* p_pcService, - const char* p_pcProtocol, - stcMDNS_RRDomain& p_rServiceDomain) const; -#ifdef MDNS_IP4_SUPPORT - bool _buildDomainForReverseIP4(IPAddress p_IP4Address, - stcMDNS_RRDomain& p_rReverseIP4Domain) const; -#endif -#ifdef MDNS_IP6_SUPPORT - bool _buildDomainForReverseIP6(IPAddress p_IP4Address, - stcMDNS_RRDomain& p_rReverseIP6Domain) const; -#endif - - /* UDP */ - bool _udpReadBuffer(unsigned char* p_pBuffer, - size_t p_stLength); - bool _udpRead8(uint8_t& p_ru8Value); - bool _udpRead16(uint16_t& p_ru16Value); - bool _udpRead32(uint32_t& p_ru32Value); - - bool _udpAppendBuffer(const unsigned char* p_pcBuffer, - size_t p_stLength); - bool _udpAppend8(uint8_t p_u8Value); - bool _udpAppend16(uint16_t p_u16Value); - bool _udpAppend32(uint32_t p_u32Value); - -#if not defined ESP_8266_MDNS_INCLUDE || defined DEBUG_ESP_MDNS_RESPONDER - bool _udpDump(bool p_bMovePointer = false); - bool _udpDump(unsigned p_uOffset, - unsigned p_uLength); -#endif - - /* READ/WRITE MDNS STRUCTS */ - bool _readMDNSMsgHeader(stcMDNS_MsgHeader& p_rMsgHeader); - - bool _write8(uint8_t p_u8Value, - stcMDNSSendParameter& p_rSendParameter); - bool _write16(uint16_t p_u16Value, - stcMDNSSendParameter& p_rSendParameter); - bool _write32(uint32_t p_u32Value, - stcMDNSSendParameter& p_rSendParameter); - - bool _writeMDNSMsgHeader(const stcMDNS_MsgHeader& p_MsgHeader, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSRRAttributes(const stcMDNS_RRAttributes& p_Attributes, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSRRDomain(const stcMDNS_RRDomain& p_Domain, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSHostDomain(const char* m_pcHostname, - bool p_bPrependRDLength, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSServiceDomain(const stcMDNSService& p_Service, - bool p_bIncludeName, - bool p_bPrependRDLength, - stcMDNSSendParameter& p_rSendParameter); - - bool _writeMDNSQuestion(stcMDNS_RRQuestion& p_Question, - stcMDNSSendParameter& p_rSendParameter); - -#ifdef MDNS_IP4_SUPPORT - bool _writeMDNSAnswer_A(IPAddress p_IPAddress, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSAnswer_PTR_IP4(IPAddress p_IPAddress, - stcMDNSSendParameter& p_rSendParameter); -#endif - bool _writeMDNSAnswer_PTR_TYPE(stcMDNSService& p_rService, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSAnswer_PTR_NAME(stcMDNSService& p_rService, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSAnswer_TXT(stcMDNSService& p_rService, - stcMDNSSendParameter& p_rSendParameter); -#ifdef MDNS_IP6_SUPPORT - bool _writeMDNSAnswer_AAAA(IPAddress p_IPAddress, - stcMDNSSendParameter& p_rSendParameter); - bool _writeMDNSAnswer_PTR_IP6(IPAddress p_IPAddress, - stcMDNSSendParameter& p_rSendParameter); -#endif - bool _writeMDNSAnswer_SRV(stcMDNSService& p_rService, - stcMDNSSendParameter& p_rSendParameter); - - /** HELPERS **/ - /* UDP CONTEXT */ - bool _callProcess(void); - bool _allocUDPContext(void); - bool _releaseUDPContext(void); - - /* SERVICE QUERY */ - stcMDNSServiceQuery* _allocServiceQuery(void); - bool _removeServiceQuery(stcMDNSServiceQuery* p_pServiceQuery); - bool _removeLegacyServiceQuery(void); - stcMDNSServiceQuery* _findServiceQuery(hMDNSServiceQuery p_hServiceQuery); - stcMDNSServiceQuery* _findLegacyServiceQuery(void); - bool _releaseServiceQueries(void); - stcMDNSServiceQuery* _findNextServiceQueryByServiceType(const stcMDNS_RRDomain& p_ServiceDomain, - const stcMDNSServiceQuery* p_pPrevServiceQuery); - - /* HOSTNAME */ - bool _setHostname(const char* p_pcHostname); - bool _releaseHostname(void); - - /* SERVICE */ - stcMDNSService* _allocService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol, - uint16_t p_u16Port); - bool _releaseService(stcMDNSService* p_pService); - bool _releaseServices(void); - - stcMDNSService* _findService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol); - stcMDNSService* _findService(const hMDNSService p_hService); - - size_t _countServices(void) const; - - /* SERVICE TXT */ - stcMDNSServiceTxt* _allocServiceTxt(stcMDNSService* p_pService, - const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp); - bool _releaseServiceTxt(stcMDNSService* p_pService, - stcMDNSServiceTxt* p_pTxt); - stcMDNSServiceTxt* _updateServiceTxt(stcMDNSService* p_pService, - stcMDNSServiceTxt* p_pTxt, - const char* p_pcValue, - bool p_bTemp); - - stcMDNSServiceTxt* _findServiceTxt(stcMDNSService* p_pService, - const char* p_pcKey); - stcMDNSServiceTxt* _findServiceTxt(stcMDNSService* p_pService, - const hMDNSTxt p_hTxt); - - stcMDNSServiceTxt* _addServiceTxt(stcMDNSService* p_pService, - const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp); - - stcMDNSServiceTxt* _answerKeyValue(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex); - - bool _collectServiceTxts(stcMDNSService& p_rService); - bool _releaseTempServiceTxts(stcMDNSService& p_rService); - const stcMDNSServiceTxt* _serviceTxts(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol); - - /* MISC */ -#if not defined ESP_8266_MDNS_INCLUDE || defined DEBUG_ESP_MDNS_RESPONDER - bool _printRRDomain(const stcMDNS_RRDomain& p_rRRDomain) const; - bool _printRRAnswer(const MDNSResponder::stcMDNS_RRAnswer& p_RRAnswer) const; -#endif -}; - -}// namespace MDNSImplementation - -}// namespace esp8266 - -#endif // MDNS_H +/* + LEAmDNS.h + (c) 2018, LaborEtArs + + Version 0.9 beta + + Some notes (from LaborEtArs, 2018): + Essentially, this is an rewrite of the original EPS8266 Multicast DNS code (ESP8266mDNS). + The target of this rewrite was to keep the existing interface as stable as possible while + adding and extending the supported set of mDNS features. + A lot of the additions were basicly taken from Erik Ekman's lwIP mdns app code. + + Supported mDNS features (in some cases somewhat limited): + - Presenting a DNS-SD service to interested observers, eg. a http server by presenting _http._tcp service + - Support for multi-level compressed names in input; in output only a very simple one-leven full-name compression is implemented + - Probing host and service domains for uniqueness in the local network + - Tiebreaking while probing is supportet in a very minimalistic way (the 'higher' IP address wins the tiebreak) + - Announcing available services after successful probing + - Using fixed service TXT items or + - Using dynamic service TXT items for presented services (via callback) + - Remove services (and un-announcing them to the observers by sending goodbye-messages) + - Static queries for DNS-SD services (creating a fixed answer set after a certain timeout period) + - Dynamic queries for DNS-SD services with cached and updated answers and user notifications + + + Usage: + In most cases, this implementation should work as a 'drop-in' replacement for the original + ESP8266 Multicast DNS code. Adjustments to the existing code would only be needed, if some + of the new features should be used. + + For presenting services: + In 'setup()': + Install a callback for the probing of host (and service) domains via 'MDNS.setProbeResultCallback(probeResultCallback, &userData);' + Register DNS-SD services with 'MDNSResponder::hMDNSService hService = MDNS.addService("MyESP", "http", "tcp", 5000);' + (Install additional callbacks for the probing of these service domains via 'MDNS.setServiceProbeResultCallback(hService, probeResultCallback, &userData);') + Add service TXT items with 'MDNS.addServiceTxt(hService, "c#", "1");' or by installing a service TXT callback + using 'MDNS.setDynamicServiceTxtCallback(dynamicServiceTxtCallback, &userData);' or service specific + 'MDNS.setDynamicServiceTxtCallback(hService, dynamicServiceTxtCallback, &userData);' + Call MDNS.begin("MyHostname"); + + In 'probeResultCallback(MDNSResponder* p_MDNSResponder, const char* p_pcDomain, MDNSResponder:hMDNSService p_hService, bool p_bProbeResult, void* p_pUserdata)': + Check the probe result and update the host or service domain name if the probe failed + + In 'dynamicServiceTxtCallback(MDNSResponder* p_MDNSResponder, const hMDNSService p_hService, void* p_pUserdata)': + Add dynamic TXT items by calling 'MDNS.addDynamicServiceTxt(p_hService, "c#", "1");' + + In loop(): + Call 'MDNS.update();' + + + For querying services: + Static: + Call 'uint32_t u32AnswerCount = MDNS.queryService("http", "tcp");' + Iterate answers by: 'for (uint32_t u=0; u // for UdpContext.h +#include "WiFiUdp.h" +#include "lwip/udp.h" +#include "debug.h" +#include "include/UdpContext.h" +#include +#include +#include + + +#include "ESP8266WiFi.h" + + +namespace esp8266 +{ + +/** + LEAmDNS +*/ +namespace MDNSImplementation +{ + +//this should be defined at build time +#ifndef ARDUINO_BOARD +#define ARDUINO_BOARD "generic" +#endif + +#define MDNS_IP4_SUPPORT +//#define MDNS_IP6_SUPPORT + + +#ifdef MDNS_IP4_SUPPORT +#define MDNS_IP4_SIZE 4 +#endif +#ifdef MDNS_IP6_SUPPORT +#define MDNS_IP6_SIZE 16 +#endif +/* + Maximum length for all service txts for one service +*/ +#define MDNS_SERVICE_TXT_MAXLENGTH 1300 +/* + Maximum length for a full domain name eg. MyESP._http._tcp.local +*/ +#define MDNS_DOMAIN_MAXLENGTH 256 +/* + Maximum length of on label in a domain name (length info fits into 6 bits) +*/ +#define MDNS_DOMAIN_LABEL_MAXLENGTH 63 +/* + Maximum length of a service name eg. http +*/ +#define MDNS_SERVICE_NAME_LENGTH 15 +/* + Maximum length of a service protocol name eg. tcp +*/ +#define MDNS_SERVICE_PROTOCOL_LENGTH 3 +/* + Default timeout for static service queries +*/ +#define MDNS_QUERYSERVICES_WAIT_TIME 1000 + + +/** + MDNSResponder +*/ +class MDNSResponder +{ +public: + /* INTERFACE */ + MDNSResponder(void); + virtual ~MDNSResponder(void); + + // Start the MDNS responder by setting the default hostname + // Later call MDNS::update() in every 'loop' to run the process loop + // (probing, announcing, responding, ...) + // if interfaceAddress is not specified, default interface is STA, or AP when STA is not set + bool begin(const char* p_pcHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/); + bool begin(const String& p_strHostname, const IPAddress& p_IPAddress = INADDR_ANY, uint32_t p_u32TTL = 120 /*ignored*/) + { + return begin(p_strHostname.c_str(), p_IPAddress, p_u32TTL); + } + + // Finish MDNS processing + bool close(void); + // for esp32 compatability + bool end(void); + // Change hostname (probing is restarted) + bool setHostname(const char* p_pcHostname); + // for compatibility... + bool setHostname(String p_strHostname); + + /** + hMDNSService (opaque handle to access the service) + */ + typedef const void* hMDNSService; + + // Add a new service to the MDNS responder. If no name (instance name) is given (p_pcName = 0) + // the current hostname is used. If the hostname is changed later, the instance names for + // these 'auto-named' services are changed to the new name also (and probing is restarted). + // The usual '_' before p_pcService (eg. http) and protocol (eg. tcp) may be given. + hMDNSService addService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol, + uint16_t p_u16Port); + // Removes a service from the MDNS responder + bool removeService(const hMDNSService p_hService); + bool removeService(const char* p_pcInstanceName, + const char* p_pcServiceName, + const char* p_pcProtocol); + // for compatibility... + bool addService(String p_strServiceName, + String p_strProtocol, + uint16_t p_u16Port); + + + // Change the services instance name (and restart probing). + bool setServiceName(const hMDNSService p_hService, + const char* p_pcInstanceName); + //for compatibility + //Warning: this has the side effect of changing the hostname. + //TODO: implement instancename different from hostname + void setInstanceName(const char* p_pcHostname) + { + setHostname(p_pcHostname); + } + // for esp32 compatibilty + void setInstanceName(const String& s_pcHostname) + { + setInstanceName(s_pcHostname.c_str()); + } + + /** + hMDNSTxt (opaque handle to access the TXT items) + */ + typedef void* hMDNSTxt; + + // Add a (static) MDNS TXT item ('key' = 'value') to the service + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + const char* p_pcValue); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + uint32_t p_u32Value); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + uint16_t p_u16Value); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + uint8_t p_u8Value); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + int32_t p_i32Value); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + int16_t p_i16Value); + hMDNSTxt addServiceTxt(const hMDNSService p_hService, + const char* p_pcKey, + int8_t p_i8Value); + + // Remove an existing (static) MDNS TXT item from the service + bool removeServiceTxt(const hMDNSService p_hService, + const hMDNSTxt p_hTxt); + bool removeServiceTxt(const hMDNSService p_hService, + const char* p_pcKey); + bool removeServiceTxt(const char* p_pcinstanceName, + const char* p_pcServiceName, + const char* p_pcProtocol, + const char* p_pcKey); + // for compatibility... + bool addServiceTxt(const char* p_pcService, + const char* p_pcProtocol, + const char* p_pcKey, + const char* p_pcValue); + bool addServiceTxt(String p_strService, + String p_strProtocol, + String p_strKey, + String p_strValue); + + /** + MDNSDynamicServiceTxtCallbackFn + Callback function for dynamic MDNS TXT items + */ + + typedef std::function MDNSDynamicServiceTxtCallbackFunc; + + // Set a global callback for dynamic MDNS TXT items. The callback function is called + // every time, a TXT item is needed for one of the installed services. + bool setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallbackFunc p_fnCallback); + // Set a service specific callback for dynamic MDNS TXT items. The callback function + // is called every time, a TXT item is needed for the given service. + bool setDynamicServiceTxtCallback(const hMDNSService p_hService, + MDNSDynamicServiceTxtCallbackFunc p_fnCallback); + + // Add a (dynamic) MDNS TXT item ('key' = 'value') to the service + // Dynamic TXT items are removed right after one-time use. So they need to be added + // every time the value s needed (via callback). + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + const char* p_pcValue); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + uint32_t p_u32Value); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + uint16_t p_u16Value); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + uint8_t p_u8Value); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + int32_t p_i32Value); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + int16_t p_i16Value); + hMDNSTxt addDynamicServiceTxt(hMDNSService p_hService, + const char* p_pcKey, + int8_t p_i8Value); + + // Perform a (static) service query. The function returns after p_u16Timeout milliseconds + // The answers (the number of received answers is returned) can be retrieved by calling + // - answerHostname (or hostname) + // - answerIP (or IP) + // - answerPort (or port) + uint32_t queryService(const char* p_pcService, + const char* p_pcProtocol, + const uint16_t p_u16Timeout = MDNS_QUERYSERVICES_WAIT_TIME); + bool removeQuery(void); + // for compatibility... + uint32_t queryService(String p_strService, + String p_strProtocol); + + const char* answerHostname(const uint32_t p_u32AnswerIndex); + IPAddress answerIP(const uint32_t p_u32AnswerIndex); + uint16_t answerPort(const uint32_t p_u32AnswerIndex); + // for compatibility... + String hostname(const uint32_t p_u32AnswerIndex); + IPAddress IP(const uint32_t p_u32AnswerIndex); + uint16_t port(const uint32_t p_u32AnswerIndex); + + /** + hMDNSServiceQuery (opaque handle to access dynamic service queries) + */ + typedef const void* hMDNSServiceQuery; + + /** + enuServiceQueryAnswerType + */ + typedef enum _enuServiceQueryAnswerType + { + ServiceQueryAnswerType_ServiceDomain = (1 << 0), // Service instance name + ServiceQueryAnswerType_HostDomainAndPort = (1 << 1), // Host domain and service port + ServiceQueryAnswerType_Txts = (1 << 2), // TXT items +#ifdef MDNS_IP4_SUPPORT + ServiceQueryAnswerType_IP4Address = (1 << 3), // IP4 address +#endif +#ifdef MDNS_IP6_SUPPORT + ServiceQueryAnswerType_IP6Address = (1 << 4), // IP6 address +#endif + } enuServiceQueryAnswerType; + + enum class AnswerType : uint32_t + { + Unknown = 0, + ServiceDomain = ServiceQueryAnswerType_ServiceDomain, + HostDomainAndPort = ServiceQueryAnswerType_HostDomainAndPort, + Txt = ServiceQueryAnswerType_Txts, +#ifdef MDNS_IP4_SUPPORT + IP4Address = ServiceQueryAnswerType_IP4Address, +#endif +#ifdef MDNS_IP6_SUPPORT + IP6Address = ServiceQueryAnswerType_IP6Address, +#endif + }; + + /** + MDNSServiceQueryCallbackFn + Callback function for received answers for dynamic service queries + */ + struct MDNSServiceInfo; // forward declaration + typedef std::function MDNSServiceQueryCallbackFunc; + + // Install a dynamic service query. For every received answer (part) the given callback + // function is called. The query will be updated every time, the TTL for an answer + // has timed-out. + // The answers can also be retrieved by calling + // - answerCount + // - answerServiceDomain + // - hasAnswerHostDomain/answerHostDomain + // - hasAnswerIP4Address/answerIP4Address + // - hasAnswerIP6Address/answerIP6Address + // - hasAnswerPort/answerPort + // - hasAnswerTxts/answerTxts + hMDNSServiceQuery installServiceQuery(const char* p_pcService, + const char* p_pcProtocol, + MDNSServiceQueryCallbackFunc p_fnCallback); + // Remove a dynamic service query + bool removeServiceQuery(hMDNSServiceQuery p_hServiceQuery); + + uint32_t answerCount(const hMDNSServiceQuery p_hServiceQuery); + std::vector answerInfo(const MDNSResponder::hMDNSServiceQuery p_hServiceQuery); + + const char* answerServiceDomain(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + bool hasAnswerHostDomain(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + const char* answerHostDomain(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); +#ifdef MDNS_IP4_SUPPORT + bool hasAnswerIP4Address(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + uint32_t answerIP4AddressCount(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + IPAddress answerIP4Address(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex, + const uint32_t p_u32AddressIndex); +#endif +#ifdef MDNS_IP6_SUPPORT + bool hasAnswerIP6Address(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + uint32_t answerIP6AddressCount(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + IPAddress answerIP6Address(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex, + const uint32_t p_u32AddressIndex); +#endif + bool hasAnswerPort(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + uint16_t answerPort(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + bool hasAnswerTxts(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + // Get the TXT items as a ';'-separated string + const char* answerTxts(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + + /** + MDNSProbeResultCallbackFn + Callback function for (host and service domain) probe results + */ + typedef std::function MDNSHostProbeFn; + + typedef std::function MDNSHostProbeFn1; + + typedef std::function MDNSServiceProbeFn; + + typedef std::function MDNSServiceProbeFn1; + + // Set a global callback function for host and service probe results + // The callback function is called, when the probing for the host domain + // (or a service domain, which hasn't got a service specific callback) + // Succeeds or fails. + // In case of failure, the failed domain name should be changed. + bool setHostProbeResultCallback(MDNSHostProbeFn p_fnCallback); + bool setHostProbeResultCallback(MDNSHostProbeFn1 p_fnCallback); + + // Set a service specific probe result callback + bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSServiceProbeFn p_fnCallback); + bool setServiceProbeResultCallback(const MDNSResponder::hMDNSService p_hService, + MDNSServiceProbeFn1 p_fnCallback); + + // Application should call this whenever AP is configured/disabled + bool notifyAPChange(void); + + // 'update' should be called in every 'loop' to run the MDNS processing + bool update(void); + + // 'announce' can be called every time, the configuration of some service + // changes. Mainly, this would be changed content of TXT items. + bool announce(void); + + // Enable OTA update + hMDNSService enableArduino(uint16_t p_u16Port, + bool p_bAuthUpload = false); + + // Domain name helper + static bool indexDomain(char*& p_rpcDomain, + const char* p_pcDivider = "-", + const char* p_pcDefaultDomain = 0); + + /** STRUCTS **/ + +public: + /** + MDNSServiceInfo, used in application callbacks + */ + struct MDNSServiceInfo + { + MDNSServiceInfo(MDNSResponder& p_pM, MDNSResponder::hMDNSServiceQuery p_hS, uint32_t p_u32A) + : p_pMDNSResponder(p_pM), + p_hServiceQuery(p_hS), + p_u32AnswerIndex(p_u32A) + {}; + struct CompareKey + { + bool operator()(char const *a, char const *b) const + { + return strcmp(a, b) < 0; + } + }; + using KeyValueMap = std::map; + protected: + MDNSResponder& p_pMDNSResponder; + MDNSResponder::hMDNSServiceQuery p_hServiceQuery; + uint32_t p_u32AnswerIndex; + KeyValueMap keyValueMap; + public: + const char* serviceDomain() + { + return p_pMDNSResponder.answerServiceDomain(p_hServiceQuery, p_u32AnswerIndex); + }; + bool hostDomainAvailable() + { + return (p_pMDNSResponder.hasAnswerHostDomain(p_hServiceQuery, p_u32AnswerIndex)); + } + const char* hostDomain() + { + return (hostDomainAvailable()) ? + p_pMDNSResponder.answerHostDomain(p_hServiceQuery, p_u32AnswerIndex) : nullptr; + }; + bool hostPortAvailable() + { + return (p_pMDNSResponder.hasAnswerPort(p_hServiceQuery, p_u32AnswerIndex)); + } + uint16_t hostPort() + { + return (hostPortAvailable()) ? + p_pMDNSResponder.answerPort(p_hServiceQuery, p_u32AnswerIndex) : 0; + }; + bool IP4AddressAvailable() + { + return (p_pMDNSResponder.hasAnswerIP4Address(p_hServiceQuery, p_u32AnswerIndex)); + } + std::vector IP4Adresses() + { + std::vector internalIP; + if (IP4AddressAvailable()) + { + uint16_t cntIP4Adress = p_pMDNSResponder.answerIP4AddressCount(p_hServiceQuery, p_u32AnswerIndex); + for (uint32_t u2 = 0; u2 < cntIP4Adress; ++u2) + { + internalIP.emplace_back(p_pMDNSResponder.answerIP4Address(p_hServiceQuery, p_u32AnswerIndex, u2)); + } + } + return internalIP; + }; + bool txtAvailable() + { + return (p_pMDNSResponder.hasAnswerTxts(p_hServiceQuery, p_u32AnswerIndex)); + } + const char* strKeyValue() + { + return (txtAvailable()) ? + p_pMDNSResponder.answerTxts(p_hServiceQuery, p_u32AnswerIndex) : nullptr; + }; + const KeyValueMap& keyValues() + { + if (txtAvailable() && keyValueMap.size() == 0) + { + for (auto kv = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); kv != nullptr; kv = kv->m_pNext) + { + keyValueMap.emplace(std::pair(kv->m_pcKey, kv->m_pcValue)); + } + } + return keyValueMap; + } + const char* value(const char* key) + { + char* result = nullptr; + + for (stcMDNSServiceTxt* pTxt = p_pMDNSResponder._answerKeyValue(p_hServiceQuery, p_u32AnswerIndex); pTxt; pTxt = pTxt->m_pNext) + { + if ((key) && + (0 == strcmp(pTxt->m_pcKey, key))) + { + result = pTxt->m_pcValue; + break; + } + } + return result; + } + }; +protected: + + /** + stcMDNSServiceTxt + */ + struct stcMDNSServiceTxt + { + stcMDNSServiceTxt* m_pNext; + char* m_pcKey; + char* m_pcValue; + bool m_bTemp; + + stcMDNSServiceTxt(const char* p_pcKey = 0, + const char* p_pcValue = 0, + bool p_bTemp = false); + stcMDNSServiceTxt(const stcMDNSServiceTxt& p_Other); + ~stcMDNSServiceTxt(void); + + stcMDNSServiceTxt& operator=(const stcMDNSServiceTxt& p_Other); + bool clear(void); + + char* allocKey(size_t p_stLength); + bool setKey(const char* p_pcKey, + size_t p_stLength); + bool setKey(const char* p_pcKey); + bool releaseKey(void); + + char* allocValue(size_t p_stLength); + bool setValue(const char* p_pcValue, + size_t p_stLength); + bool setValue(const char* p_pcValue); + bool releaseValue(void); + + bool set(const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp = false); + + bool update(const char* p_pcValue); + + size_t length(void) const; + }; + + /** + stcMDNSTxts + */ + struct stcMDNSServiceTxts + { + stcMDNSServiceTxt* m_pTxts; + + stcMDNSServiceTxts(void); + stcMDNSServiceTxts(const stcMDNSServiceTxts& p_Other); + ~stcMDNSServiceTxts(void); + + stcMDNSServiceTxts& operator=(const stcMDNSServiceTxts& p_Other); + + bool clear(void); + + bool add(stcMDNSServiceTxt* p_pTxt); + bool remove(stcMDNSServiceTxt* p_pTxt); + + bool removeTempTxts(void); + + stcMDNSServiceTxt* find(const char* p_pcKey); + const stcMDNSServiceTxt* find(const char* p_pcKey) const; + stcMDNSServiceTxt* find(const stcMDNSServiceTxt* p_pTxt); + + uint16_t length(void) const; + + size_t c_strLength(void) const; + bool c_str(char* p_pcBuffer); + + size_t bufferLength(void) const; + bool buffer(char* p_pcBuffer); + + bool compare(const stcMDNSServiceTxts& p_Other) const; + bool operator==(const stcMDNSServiceTxts& p_Other) const; + bool operator!=(const stcMDNSServiceTxts& p_Other) const; + }; + + /** + enuContentFlags + */ + typedef enum _enuContentFlags + { + // Host + ContentFlag_A = 0x01, + ContentFlag_PTR_IP4 = 0x02, + ContentFlag_PTR_IP6 = 0x04, + ContentFlag_AAAA = 0x08, + // Service + ContentFlag_PTR_TYPE = 0x10, + ContentFlag_PTR_NAME = 0x20, + ContentFlag_TXT = 0x40, + ContentFlag_SRV = 0x80, + } enuContentFlags; + + /** + stcMDNS_MsgHeader + */ + struct stcMDNS_MsgHeader + { + uint16_t m_u16ID; // Identifier + bool m_1bQR : 1; // Query/Response flag + unsigned char m_4bOpcode : 4; // Operation code + bool m_1bAA : 1; // Authoritative Answer flag + bool m_1bTC : 1; // Truncation flag + bool m_1bRD : 1; // Recursion desired + bool m_1bRA : 1; // Recursion available + unsigned char m_3bZ : 3; // Zero + unsigned char m_4bRCode : 4; // Response code + uint16_t m_u16QDCount; // Question count + uint16_t m_u16ANCount; // Answer count + uint16_t m_u16NSCount; // Authority Record count + uint16_t m_u16ARCount; // Additional Record count + + stcMDNS_MsgHeader(uint16_t p_u16ID = 0, + bool p_bQR = false, + unsigned char p_ucOpcode = 0, + bool p_bAA = false, + bool p_bTC = false, + bool p_bRD = false, + bool p_bRA = false, + unsigned char p_ucRCode = 0, + uint16_t p_u16QDCount = 0, + uint16_t p_u16ANCount = 0, + uint16_t p_u16NSCount = 0, + uint16_t p_u16ARCount = 0); + }; + + /** + stcMDNS_RRDomain + */ + struct stcMDNS_RRDomain + { + char m_acName[MDNS_DOMAIN_MAXLENGTH]; // Encoded domain name + uint16_t m_u16NameLength; // Length (incl. '\0') + + stcMDNS_RRDomain(void); + stcMDNS_RRDomain(const stcMDNS_RRDomain& p_Other); + + stcMDNS_RRDomain& operator=(const stcMDNS_RRDomain& p_Other); + + bool clear(void); + + bool addLabel(const char* p_pcLabel, + bool p_bPrependUnderline = false); + + bool compare(const stcMDNS_RRDomain& p_Other) const; + bool operator==(const stcMDNS_RRDomain& p_Other) const; + bool operator!=(const stcMDNS_RRDomain& p_Other) const; + bool operator>(const stcMDNS_RRDomain& p_Other) const; + + size_t c_strLength(void) const; + bool c_str(char* p_pcBuffer); + }; + + /** + stcMDNS_RRAttributes + */ + struct stcMDNS_RRAttributes + { + uint16_t m_u16Type; // Type + uint16_t m_u16Class; // Class, nearly always 'IN' + + stcMDNS_RRAttributes(uint16_t p_u16Type = 0, + uint16_t p_u16Class = 1 /*DNS_RRCLASS_IN Internet*/); + stcMDNS_RRAttributes(const stcMDNS_RRAttributes& p_Other); + + stcMDNS_RRAttributes& operator=(const stcMDNS_RRAttributes& p_Other); + }; + + /** + stcMDNS_RRHeader + */ + struct stcMDNS_RRHeader + { + stcMDNS_RRDomain m_Domain; + stcMDNS_RRAttributes m_Attributes; + + stcMDNS_RRHeader(void); + stcMDNS_RRHeader(const stcMDNS_RRHeader& p_Other); + + stcMDNS_RRHeader& operator=(const stcMDNS_RRHeader& p_Other); + + bool clear(void); + }; + + /** + stcMDNS_RRQuestion + */ + struct stcMDNS_RRQuestion + { + stcMDNS_RRQuestion* m_pNext; + stcMDNS_RRHeader m_Header; + bool m_bUnicast; // Unicast reply requested + + stcMDNS_RRQuestion(void); + }; + + /** + enuAnswerType + */ + typedef enum _enuAnswerType + { + AnswerType_A, + AnswerType_PTR, + AnswerType_TXT, + AnswerType_AAAA, + AnswerType_SRV, + AnswerType_Generic + } enuAnswerType; + + /** + stcMDNS_RRAnswer + */ + struct stcMDNS_RRAnswer + { + stcMDNS_RRAnswer* m_pNext; + const enuAnswerType m_AnswerType; + stcMDNS_RRHeader m_Header; + bool m_bCacheFlush; // Cache flush command bit + uint32_t m_u32TTL; // Validity time in seconds + + virtual ~stcMDNS_RRAnswer(void); + + enuAnswerType answerType(void) const; + + bool clear(void); + + protected: + stcMDNS_RRAnswer(enuAnswerType p_AnswerType, + const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + }; + +#ifdef MDNS_IP4_SUPPORT + /** + stcMDNS_RRAnswerA + */ + struct stcMDNS_RRAnswerA : public stcMDNS_RRAnswer + { + IPAddress m_IPAddress; + + stcMDNS_RRAnswerA(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerA(void); + + bool clear(void); + }; +#endif + + /** + stcMDNS_RRAnswerPTR + */ + struct stcMDNS_RRAnswerPTR : public stcMDNS_RRAnswer + { + stcMDNS_RRDomain m_PTRDomain; + + stcMDNS_RRAnswerPTR(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerPTR(void); + + bool clear(void); + }; + + /** + stcMDNS_RRAnswerTXT + */ + struct stcMDNS_RRAnswerTXT : public stcMDNS_RRAnswer + { + stcMDNSServiceTxts m_Txts; + + stcMDNS_RRAnswerTXT(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerTXT(void); + + bool clear(void); + }; + +#ifdef MDNS_IP6_SUPPORT + /** + stcMDNS_RRAnswerAAAA + */ + struct stcMDNS_RRAnswerAAAA : public stcMDNS_RRAnswer + { + //TODO: IP6Address m_IPAddress; + + stcMDNS_RRAnswerAAAA(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerAAAA(void); + + bool clear(void); + }; +#endif + + /** + stcMDNS_RRAnswerSRV + */ + struct stcMDNS_RRAnswerSRV : public stcMDNS_RRAnswer + { + uint16_t m_u16Priority; + uint16_t m_u16Weight; + uint16_t m_u16Port; + stcMDNS_RRDomain m_SRVDomain; + + stcMDNS_RRAnswerSRV(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerSRV(void); + + bool clear(void); + }; + + /** + stcMDNS_RRAnswerGeneric + */ + struct stcMDNS_RRAnswerGeneric : public stcMDNS_RRAnswer + { + uint16_t m_u16RDLength; // Length of variable answer + uint8_t* m_pu8RDData; // Offset of start of variable answer in packet + + stcMDNS_RRAnswerGeneric(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL); + ~stcMDNS_RRAnswerGeneric(void); + + bool clear(void); + }; + + + /** + enuProbingStatus + */ + typedef enum _enuProbingStatus + { + ProbingStatus_WaitingForData, + ProbingStatus_ReadyToStart, + ProbingStatus_InProgress, + ProbingStatus_Done + } enuProbingStatus; + + /** + stcProbeInformation + */ + struct stcProbeInformation + { + enuProbingStatus m_ProbingStatus; + uint8_t m_u8SentCount; // Used for probes and announcements + esp8266::polledTimeout::oneShotMs m_Timeout; // Used for probes and announcements + //clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements + bool m_bConflict; + bool m_bTiebreakNeeded; + MDNSHostProbeFn m_fnHostProbeResultCallback; + MDNSServiceProbeFn m_fnServiceProbeResultCallback; + + stcProbeInformation(void); + + bool clear(bool p_bClearUserdata = false); + }; + + + /** + stcMDNSService + */ + struct stcMDNSService + { + stcMDNSService* m_pNext; + char* m_pcName; + bool m_bAutoName; // Name was set automatically to hostname (if no name was supplied) + char* m_pcService; + char* m_pcProtocol; + uint16_t m_u16Port; + uint8_t m_u8ReplyMask; + stcMDNSServiceTxts m_Txts; + MDNSDynamicServiceTxtCallbackFunc m_fnTxtCallback; + stcProbeInformation m_ProbeInformation; + + stcMDNSService(const char* p_pcName = 0, + const char* p_pcService = 0, + const char* p_pcProtocol = 0); + ~stcMDNSService(void); + + bool setName(const char* p_pcName); + bool releaseName(void); + + bool setService(const char* p_pcService); + bool releaseService(void); + + bool setProtocol(const char* p_pcProtocol); + bool releaseProtocol(void); + }; + + /** + stcMDNSServiceQuery + */ + struct stcMDNSServiceQuery + { + /** + stcAnswer + */ + struct stcAnswer + { + /** + stcTTL + */ + struct stcTTL + { + /** + timeoutLevel_t + */ + typedef uint8_t timeoutLevel_t; + /** + TIMEOUTLEVELs + */ + const timeoutLevel_t TIMEOUTLEVEL_UNSET = 0; + const timeoutLevel_t TIMEOUTLEVEL_BASE = 80; + const timeoutLevel_t TIMEOUTLEVEL_INTERVAL = 5; + const timeoutLevel_t TIMEOUTLEVEL_FINAL = 100; + + uint32_t m_u32TTL; + esp8266::polledTimeout::oneShotMs m_TTLTimeout; + timeoutLevel_t m_timeoutLevel; + + stcTTL(void); + bool set(uint32_t p_u32TTL); + + bool flagged(void); + bool restart(void); + + bool prepareDeletion(void); + bool finalTimeoutLevel(void) const; + + unsigned long timeout(void) const; + }; +#ifdef MDNS_IP4_SUPPORT + /** + stcIP4Address + */ + struct stcIP4Address + { + stcIP4Address* m_pNext; + IPAddress m_IPAddress; + stcTTL m_TTL; + + stcIP4Address(IPAddress p_IPAddress, + uint32_t p_u32TTL = 0); + }; +#endif +#ifdef MDNS_IP6_SUPPORT + /** + stcIP6Address + */ + struct stcIP6Address + { + stcIP6Address* m_pNext; + IP6Address m_IPAddress; + stcTTL m_TTL; + + stcIP6Address(IPAddress p_IPAddress, + uint32_t p_u32TTL = 0); + }; +#endif + + stcAnswer* m_pNext; + // The service domain is the first 'answer' (from PTR answer, using service and protocol) to be set + // Defines the key for additional answer, like host domain, etc. + stcMDNS_RRDomain m_ServiceDomain; // 1. level answer (PTR), eg. MyESP._http._tcp.local + char* m_pcServiceDomain; + stcTTL m_TTLServiceDomain; + stcMDNS_RRDomain m_HostDomain; // 2. level answer (SRV, using service domain), eg. esp8266.local + char* m_pcHostDomain; + uint16_t m_u16Port; // 2. level answer (SRV, using service domain), eg. 5000 + stcTTL m_TTLHostDomainAndPort; + stcMDNSServiceTxts m_Txts; // 2. level answer (TXT, using service domain), eg. c#=1 + char* m_pcTxts; + stcTTL m_TTLTxts; +#ifdef MDNS_IP4_SUPPORT + stcIP4Address* m_pIP4Addresses; // 3. level answer (A, using host domain), eg. 123.456.789.012 +#endif +#ifdef MDNS_IP6_SUPPORT + stcIP6Address* m_pIP6Addresses; // 3. level answer (AAAA, using host domain), eg. 1234::09 +#endif + uint32_t m_u32ContentFlags; + + stcAnswer(void); + ~stcAnswer(void); + + bool clear(void); + + char* allocServiceDomain(size_t p_stLength); + bool releaseServiceDomain(void); + + char* allocHostDomain(size_t p_stLength); + bool releaseHostDomain(void); + + char* allocTxts(size_t p_stLength); + bool releaseTxts(void); + +#ifdef MDNS_IP4_SUPPORT + bool releaseIP4Addresses(void); + bool addIP4Address(stcIP4Address* p_pIP4Address); + bool removeIP4Address(stcIP4Address* p_pIP4Address); + const stcIP4Address* findIP4Address(const IPAddress& p_IPAddress) const; + stcIP4Address* findIP4Address(const IPAddress& p_IPAddress); + uint32_t IP4AddressCount(void) const; + const stcIP4Address* IP4AddressAtIndex(uint32_t p_u32Index) const; + stcIP4Address* IP4AddressAtIndex(uint32_t p_u32Index); +#endif +#ifdef MDNS_IP6_SUPPORT + bool releaseIP6Addresses(void); + bool addIP6Address(stcIP6Address* p_pIP6Address); + bool removeIP6Address(stcIP6Address* p_pIP6Address); + const stcIP6Address* findIP6Address(const IPAddress& p_IPAddress) const; + stcIP6Address* findIP6Address(const IPAddress& p_IPAddress); + uint32_t IP6AddressCount(void) const; + const stcIP6Address* IP6AddressAtIndex(uint32_t p_u32Index) const; + stcIP6Address* IP6AddressAtIndex(uint32_t p_u32Index); +#endif + }; + + stcMDNSServiceQuery* m_pNext; + stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local + MDNSServiceQueryCallbackFunc m_fnCallback; + bool m_bLegacyQuery; + uint8_t m_u8SentCount; + esp8266::polledTimeout::oneShotMs m_ResendTimeout; + bool m_bAwaitingAnswers; + stcAnswer* m_pAnswers; + + stcMDNSServiceQuery(void); + ~stcMDNSServiceQuery(void); + + bool clear(void); + + uint32_t answerCount(void) const; + const stcAnswer* answerAtIndex(uint32_t p_u32Index) const; + stcAnswer* answerAtIndex(uint32_t p_u32Index); + uint32_t indexOfAnswer(const stcAnswer* p_pAnswer) const; + + bool addAnswer(stcAnswer* p_pAnswer); + bool removeAnswer(stcAnswer* p_pAnswer); + + stcAnswer* findAnswerForServiceDomain(const stcMDNS_RRDomain& p_ServiceDomain); + stcAnswer* findAnswerForHostDomain(const stcMDNS_RRDomain& p_HostDomain); + }; + + /** + stcMDNSSendParameter + */ + struct stcMDNSSendParameter + { + protected: + /** + stcDomainCacheItem + */ + struct stcDomainCacheItem + { + stcDomainCacheItem* m_pNext; + const void* m_pHostnameOrService; // Opaque id for host or service domain (pointer) + bool m_bAdditionalData; // Opaque flag for special info (service domain included) + uint16_t m_u16Offset; // Offset in UDP output buffer + + stcDomainCacheItem(const void* p_pHostnameOrService, + bool p_bAdditionalData, + uint32_t p_u16Offset); + }; + + public: + uint16_t m_u16ID; // Query ID (used only in lagacy queries) + stcMDNS_RRQuestion* m_pQuestions; // A list of queries + uint8_t m_u8HostReplyMask; // Flags for reply components/answers + bool m_bLegacyQuery; // Flag: Legacy query + bool m_bResponse; // Flag: Response to a query + bool m_bAuthorative; // Flag: Authorative (owner) response + bool m_bCacheFlush; // Flag: Clients should flush their caches + bool m_bUnicast; // Flag: Unicast response + bool m_bUnannounce; // Flag: Unannounce service + uint16_t m_u16Offset; // Current offset in UDP write buffer (mainly for domain cache) + stcDomainCacheItem* m_pDomainCacheItems; // Cached host and service domains + + stcMDNSSendParameter(void); + ~stcMDNSSendParameter(void); + + bool clear(void); + + bool shiftOffset(uint16_t p_u16Shift); + + bool addDomainCacheItem(const void* p_pHostnameOrService, + bool p_bAdditionalData, + uint16_t p_u16Offset); + uint16_t findCachedDomainOffset(const void* p_pHostnameOrService, + bool p_bAdditionalData) const; + }; + + // Instance variables + stcMDNSService* m_pServices; + UdpContext* m_pUDPContext; + char* m_pcHostname; + stcMDNSServiceQuery* m_pServiceQueries; + WiFiEventHandler m_DisconnectedHandler; + WiFiEventHandler m_GotIPHandler; + MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback; + bool m_bPassivModeEnabled; + stcProbeInformation m_HostProbeInformation; + CONST netif* m_netif; // network interface to run on + + /** CONTROL **/ + /* MAINTENANCE */ + bool _process(bool p_bUserContext); + bool _restart(void); + + /* RECEIVING */ + bool _parseMessage(void); + bool _parseQuery(const stcMDNS_MsgHeader& p_Header); + + bool _parseResponse(const stcMDNS_MsgHeader& p_Header); + bool _processAnswers(const stcMDNS_RRAnswer* p_pPTRAnswers); + bool _processPTRAnswer(const stcMDNS_RRAnswerPTR* p_pPTRAnswer, + bool& p_rbFoundNewKeyAnswer); + bool _processSRVAnswer(const stcMDNS_RRAnswerSRV* p_pSRVAnswer, + bool& p_rbFoundNewKeyAnswer); + bool _processTXTAnswer(const stcMDNS_RRAnswerTXT* p_pTXTAnswer); +#ifdef MDNS_IP4_SUPPORT + bool _processAAnswer(const stcMDNS_RRAnswerA* p_pAAnswer); +#endif +#ifdef MDNS_IP6_SUPPORT + bool _processAAAAAnswer(const stcMDNS_RRAnswerAAAA* p_pAAAAAnswer); +#endif + + /* PROBING */ + bool _updateProbeStatus(void); + bool _resetProbeStatus(bool p_bRestart = true); + bool _hasProbesWaitingForAnswers(void) const; + bool _sendHostProbe(void); + bool _sendServiceProbe(stcMDNSService& p_rService); + bool _cancelProbingForHost(void); + bool _cancelProbingForService(stcMDNSService& p_rService); + + /* ANNOUNCE */ + bool _announce(bool p_bAnnounce, + bool p_bIncludeServices); + bool _announceService(stcMDNSService& p_rService, + bool p_bAnnounce = true); + + /* SERVICE QUERY CACHE */ + bool _hasServiceQueriesWaitingForAnswers(void) const; + bool _checkServiceQueryCache(void); + + /** TRANSFER **/ + /* SENDING */ + bool _sendMDNSMessage(stcMDNSSendParameter& p_SendParameter); + bool _sendMDNSMessage_Multicast(MDNSResponder::stcMDNSSendParameter& p_rSendParameter); + bool _prepareMDNSMessage(stcMDNSSendParameter& p_SendParameter, + IPAddress p_IPAddress); + bool _sendMDNSServiceQuery(const stcMDNSServiceQuery& p_ServiceQuery); + bool _sendMDNSQuery(const stcMDNS_RRDomain& p_QueryDomain, + uint16_t p_u16QueryType, + stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers = 0); + + const IPAddress _getResponseMulticastInterface() const + { + return IPAddress(m_netif->ip_addr); + } + + uint8_t _replyMaskForHost(const stcMDNS_RRHeader& p_RRHeader, + bool* p_pbFullNameMatch = 0) const; + uint8_t _replyMaskForService(const stcMDNS_RRHeader& p_RRHeader, + const stcMDNSService& p_Service, + bool* p_pbFullNameMatch = 0) const; + + /* RESOURCE RECORD */ + bool _readRRQuestion(stcMDNS_RRQuestion& p_rQuestion); + bool _readRRAnswer(stcMDNS_RRAnswer*& p_rpAnswer); +#ifdef MDNS_IP4_SUPPORT + bool _readRRAnswerA(stcMDNS_RRAnswerA& p_rRRAnswerA, + uint16_t p_u16RDLength); +#endif + bool _readRRAnswerPTR(stcMDNS_RRAnswerPTR& p_rRRAnswerPTR, + uint16_t p_u16RDLength); + bool _readRRAnswerTXT(stcMDNS_RRAnswerTXT& p_rRRAnswerTXT, + uint16_t p_u16RDLength); +#ifdef MDNS_IP6_SUPPORT + bool _readRRAnswerAAAA(stcMDNS_RRAnswerAAAA& p_rRRAnswerAAAA, + uint16_t p_u16RDLength); +#endif + bool _readRRAnswerSRV(stcMDNS_RRAnswerSRV& p_rRRAnswerSRV, + uint16_t p_u16RDLength); + bool _readRRAnswerGeneric(stcMDNS_RRAnswerGeneric& p_rRRAnswerGeneric, + uint16_t p_u16RDLength); + + bool _readRRHeader(stcMDNS_RRHeader& p_rHeader); + bool _readRRDomain(stcMDNS_RRDomain& p_rRRDomain); + bool _readRRDomain_Loop(stcMDNS_RRDomain& p_rRRDomain, + uint8_t p_u8Depth); + bool _readRRAttributes(stcMDNS_RRAttributes& p_rAttributes); + + /* DOMAIN NAMES */ + bool _buildDomainForHost(const char* p_pcHostname, + stcMDNS_RRDomain& p_rHostDomain) const; + bool _buildDomainForDNSSD(stcMDNS_RRDomain& p_rDNSSDDomain) const; + bool _buildDomainForService(const stcMDNSService& p_Service, + bool p_bIncludeName, + stcMDNS_RRDomain& p_rServiceDomain) const; + bool _buildDomainForService(const char* p_pcService, + const char* p_pcProtocol, + stcMDNS_RRDomain& p_rServiceDomain) const; +#ifdef MDNS_IP4_SUPPORT + bool _buildDomainForReverseIP4(IPAddress p_IP4Address, + stcMDNS_RRDomain& p_rReverseIP4Domain) const; +#endif +#ifdef MDNS_IP6_SUPPORT + bool _buildDomainForReverseIP6(IPAddress p_IP4Address, + stcMDNS_RRDomain& p_rReverseIP6Domain) const; +#endif + + /* UDP */ + bool _udpReadBuffer(unsigned char* p_pBuffer, + size_t p_stLength); + bool _udpRead8(uint8_t& p_ru8Value); + bool _udpRead16(uint16_t& p_ru16Value); + bool _udpRead32(uint32_t& p_ru32Value); + + bool _udpAppendBuffer(const unsigned char* p_pcBuffer, + size_t p_stLength); + bool _udpAppend8(uint8_t p_u8Value); + bool _udpAppend16(uint16_t p_u16Value); + bool _udpAppend32(uint32_t p_u32Value); + +#if not defined ESP_8266_MDNS_INCLUDE || defined DEBUG_ESP_MDNS_RESPONDER + bool _udpDump(bool p_bMovePointer = false); + bool _udpDump(unsigned p_uOffset, + unsigned p_uLength); +#endif + + /* READ/WRITE MDNS STRUCTS */ + bool _readMDNSMsgHeader(stcMDNS_MsgHeader& p_rMsgHeader); + + bool _write8(uint8_t p_u8Value, + stcMDNSSendParameter& p_rSendParameter); + bool _write16(uint16_t p_u16Value, + stcMDNSSendParameter& p_rSendParameter); + bool _write32(uint32_t p_u32Value, + stcMDNSSendParameter& p_rSendParameter); + + bool _writeMDNSMsgHeader(const stcMDNS_MsgHeader& p_MsgHeader, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSRRAttributes(const stcMDNS_RRAttributes& p_Attributes, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSRRDomain(const stcMDNS_RRDomain& p_Domain, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSHostDomain(const char* m_pcHostname, + bool p_bPrependRDLength, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSServiceDomain(const stcMDNSService& p_Service, + bool p_bIncludeName, + bool p_bPrependRDLength, + stcMDNSSendParameter& p_rSendParameter); + + bool _writeMDNSQuestion(stcMDNS_RRQuestion& p_Question, + stcMDNSSendParameter& p_rSendParameter); + +#ifdef MDNS_IP4_SUPPORT + bool _writeMDNSAnswer_A(IPAddress p_IPAddress, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSAnswer_PTR_IP4(IPAddress p_IPAddress, + stcMDNSSendParameter& p_rSendParameter); +#endif + bool _writeMDNSAnswer_PTR_TYPE(stcMDNSService& p_rService, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSAnswer_PTR_NAME(stcMDNSService& p_rService, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSAnswer_TXT(stcMDNSService& p_rService, + stcMDNSSendParameter& p_rSendParameter); +#ifdef MDNS_IP6_SUPPORT + bool _writeMDNSAnswer_AAAA(IPAddress p_IPAddress, + stcMDNSSendParameter& p_rSendParameter); + bool _writeMDNSAnswer_PTR_IP6(IPAddress p_IPAddress, + stcMDNSSendParameter& p_rSendParameter); +#endif + bool _writeMDNSAnswer_SRV(stcMDNSService& p_rService, + stcMDNSSendParameter& p_rSendParameter); + + /** HELPERS **/ + /* UDP CONTEXT */ + bool _callProcess(void); + bool _allocUDPContext(void); + bool _releaseUDPContext(void); + + /* SERVICE QUERY */ + stcMDNSServiceQuery* _allocServiceQuery(void); + bool _removeServiceQuery(stcMDNSServiceQuery* p_pServiceQuery); + bool _removeLegacyServiceQuery(void); + stcMDNSServiceQuery* _findServiceQuery(hMDNSServiceQuery p_hServiceQuery); + stcMDNSServiceQuery* _findLegacyServiceQuery(void); + bool _releaseServiceQueries(void); + stcMDNSServiceQuery* _findNextServiceQueryByServiceType(const stcMDNS_RRDomain& p_ServiceDomain, + const stcMDNSServiceQuery* p_pPrevServiceQuery); + + /* HOSTNAME */ + bool _setHostname(const char* p_pcHostname); + bool _releaseHostname(void); + + /* SERVICE */ + stcMDNSService* _allocService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol, + uint16_t p_u16Port); + bool _releaseService(stcMDNSService* p_pService); + bool _releaseServices(void); + + stcMDNSService* _findService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol); + stcMDNSService* _findService(const hMDNSService p_hService); + + size_t _countServices(void) const; + + /* SERVICE TXT */ + stcMDNSServiceTxt* _allocServiceTxt(stcMDNSService* p_pService, + const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp); + bool _releaseServiceTxt(stcMDNSService* p_pService, + stcMDNSServiceTxt* p_pTxt); + stcMDNSServiceTxt* _updateServiceTxt(stcMDNSService* p_pService, + stcMDNSServiceTxt* p_pTxt, + const char* p_pcValue, + bool p_bTemp); + + stcMDNSServiceTxt* _findServiceTxt(stcMDNSService* p_pService, + const char* p_pcKey); + stcMDNSServiceTxt* _findServiceTxt(stcMDNSService* p_pService, + const hMDNSTxt p_hTxt); + + stcMDNSServiceTxt* _addServiceTxt(stcMDNSService* p_pService, + const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp); + + stcMDNSServiceTxt* _answerKeyValue(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex); + + bool _collectServiceTxts(stcMDNSService& p_rService); + bool _releaseTempServiceTxts(stcMDNSService& p_rService); + const stcMDNSServiceTxt* _serviceTxts(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol); + + /* MISC */ +#if not defined ESP_8266_MDNS_INCLUDE || defined DEBUG_ESP_MDNS_RESPONDER + bool _printRRDomain(const stcMDNS_RRDomain& p_rRRDomain) const; + bool _printRRAnswer(const MDNSResponder::stcMDNS_RRAnswer& p_RRAnswer) const; +#endif +}; + +}// namespace MDNSImplementation + +}// namespace esp8266 + +#endif // MDNS_H diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index 08d9760ec..41e9524ab 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -1,1832 +1,2134 @@ -/* - * LEAmDNS_Control.cpp - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#include -#include -#include -#include -#include -#include - -/* - * ESP8266mDNS Control.cpp - */ - -extern "C" { - #include "user_interface.h" -} - -#include "LEAmDNS_lwIPdefs.h" -#include "LEAmDNS_Priv.h" - -namespace esp8266 { -/* - * LEAmDNS - */ -namespace MDNSImplementation { - -/** - * CONTROL - */ - - -/** - * MAINTENANCE - */ - -/* - * MDNSResponder::_process - * - * Run the MDNS process. - * Is called, every time the UDPContext receives data AND - * should be called in every 'loop' by calling 'MDNS::update()'. - * - */ -bool MDNSResponder::_process(bool p_bUserContext) { - - bool bResult = true; - - if (!p_bUserContext) { - - if ((m_pUDPContext) && // UDPContext available AND - (m_pUDPContext->next())) { // has content - - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _update: Calling _parseMessage\n"));); - bResult = _parseMessage(); - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parsePacket %s\n"), (bResult ? "succeeded" : "FAILED"));); - } - } - else { - bResult = (m_netif != nullptr) && - (m_netif->flags & NETIF_FLAG_UP) && // network interface is up and running +/* + LEAmDNS_Control.cpp + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +#include +#include +#include +#include +#include +#include + +/* + ESP8266mDNS Control.cpp +*/ + +extern "C" { +#include "user_interface.h" +} + +#include "LEAmDNS_lwIPdefs.h" +#include "LEAmDNS_Priv.h" + +namespace esp8266 +{ +/* + LEAmDNS +*/ +namespace MDNSImplementation +{ + +/** + CONTROL +*/ + + +/** + MAINTENANCE +*/ + +/* + MDNSResponder::_process + + Run the MDNS process. + Is called, every time the UDPContext receives data AND + should be called in every 'loop' by calling 'MDNS::update()'. + +*/ +bool MDNSResponder::_process(bool p_bUserContext) +{ + + bool bResult = true; + + if (!p_bUserContext) + { + + if ((m_pUDPContext) && // UDPContext available AND + (m_pUDPContext->next())) // has content + { + + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _update: Calling _parseMessage\n"));); + bResult = _parseMessage(); + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parsePacket %s\n"), (bResult ? "succeeded" : "FAILED"));); + } + } + else + { + bResult = (m_netif != nullptr) && + (m_netif->flags & NETIF_FLAG_UP) && // network interface is up and running _updateProbeStatus() && // Probing _checkServiceQueryCache(); // Service query cache check - } - return bResult; -} - -/* - * MDNSResponder::_restart - */ -bool MDNSResponder::_restart(void) { - - return ((m_netif != nullptr) && - (m_netif->flags & NETIF_FLAG_UP) && // network interface is up and running - (_resetProbeStatus(true)) && // Stop and restart probing - (_allocUDPContext())); // Restart UDP -} - - -/** - * RECEIVING - */ - -/* - * MDNSResponder::_parseMessage - */ -bool MDNSResponder::_parseMessage(void) { - DEBUG_EX_INFO( - unsigned long ulStartTime = millis(); - unsigned uStartMemory = ESP.getFreeHeap(); - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage (Time: %lu ms, heap: %u bytes, from %s(%u), to %s(%u))\n"), ulStartTime, uStartMemory, - IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), m_pUDPContext->getRemotePort(), - IPAddress(m_pUDPContext->getDestAddress()).toString().c_str(), m_pUDPContext->getLocalPort()); - ); - //DEBUG_EX_INFO(_udpDump();); - - bool bResult = false; - - stcMDNS_MsgHeader header; - if (_readMDNSMsgHeader(header)) { - if (0 == header.m_4bOpcode) { // A standard query - if (header.m_1bQR) { // Received a response -> answers to a query - //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Reading answers: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount);); - bResult = _parseResponse(header); - } - else { // Received a query (Questions) - //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Reading query: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount);); - bResult = _parseQuery(header); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Received UNEXPECTED opcode:%u. Ignoring message!\n"), header.m_4bOpcode);); - m_pUDPContext->flush(); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: FAILED to read header\n"));); - m_pUDPContext->flush(); - } - DEBUG_EX_INFO( - unsigned uFreeHeap = ESP.getFreeHeap(); - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Done (%s after %lu ms, ate %i bytes, remaining %u)\n\n"), (bResult ? "Succeeded" : "FAILED"), (millis() - ulStartTime), (uStartMemory - uFreeHeap), uFreeHeap); - ); - return bResult; -} - -/* - * MDNSResponder::_parseQuery - * - * Queries are of interest in two cases: - * 1. allow for tiebreaking while probing in the case of a race condition between two instances probing for - * the same name at the same time - * 2. provide answers to questions for our host domain or any presented service - * - * When reading the questions, a set of (planned) responses is created, eg. a reverse PTR question for the host domain - * gets an A (IP address) response, a PTR question for the _services._dns-sd domain gets a PTR (type) response for any - * registered service, ... - * - * As any mDNS responder should be able to handle 'legacy' queries (from DNS clients), this case is handled here also. - * Legacy queries have got only one (unicast) question and are directed to the local DNS port (not the multicast port). - * - * 1. - */ -bool MDNSResponder::_parseQuery(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) { - - bool bResult = true; - - stcMDNSSendParameter sendParameter; - uint8_t u8HostOrServiceReplies = 0; - for (uint16_t qd=0; ((bResult) && (qdm_pNext) { - // Define service replies, BUT only answer queries after probing is done - uint8_t u8ReplyMaskForQuestion = (((m_bPassivModeEnabled) || - (ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus)) - ? _replyMaskForService(questionRR.m_Header, *pService, 0) - : 0); - u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion); - DEBUG_EX_INFO(if (u8ReplyMaskForQuestion) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service reply needed for (%s.%s.%s): 0x%X (%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, u8ReplyMaskForQuestion, IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str()); } ); - - // Check tiebreak need for service domain - if (ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) { - bool bFullNameMatch = false; - if ((_replyMaskForService(questionRR.m_Header, *pService, &bFullNameMatch)) && - (bFullNameMatch)) { - // We're in 'probing' state and someone is asking for this service domain: this might be - // a race-condition: Two services with the same domain names try simutanously to probe their domains - // See: RFC 6762, 8.2 (Tiebraking) - // However, we're using a max. reduced approach for tiebreaking here: The 'higher' SRV host wins! - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Possible race-condition for service domain %s.%s.%s detected while probing.\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); - - pService->m_ProbeInformation.m_bTiebreakNeeded = true; - } - } - } - - // Handle unicast and legacy specialities - // If only one question asks for unicast reply, the whole reply packet is send unicast - if (((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) || // Unicast (maybe legacy) query OR - (questionRR.m_bUnicast)) && // Expressivly unicast query - (!sendParameter.m_bUnicast)) { - - sendParameter.m_bUnicast = true; - sendParameter.m_bCacheFlush = false; - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Unicast response for %s!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());); - - if ((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) && // Unicast (maybe legacy) query AND - (1 == p_MsgHeader.m_u16QDCount) && // Only one question AND - ((sendParameter.m_u8HostReplyMask) || // Host replies OR - (u8HostOrServiceReplies))) { // Host or service replies available - // We're a match for this legacy query, BUT - // make sure, that the query comes from a local host - ip_info IPInfo_Local; - ip_info IPInfo_Remote; - if (((IPInfo_Remote.ip.addr = m_pUDPContext->getRemoteAddress())) && - (((wifi_get_ip_info(SOFTAP_IF, &IPInfo_Local)) && - (ip4_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))) || // Remote IP in SOFTAP's subnet OR - ((wifi_get_ip_info(STATION_IF, &IPInfo_Local)) && - (ip4_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))))) { // Remote IP in STATION's subnet - - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Legacy query from local host %s!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());); - - sendParameter.m_u16ID = p_MsgHeader.m_u16ID; - sendParameter.m_bLegacyQuery = true; - sendParameter.m_pQuestions = new stcMDNS_RRQuestion; - if ((bResult = (0 != sendParameter.m_pQuestions))) { - sendParameter.m_pQuestions->m_Header.m_Domain = questionRR.m_Header.m_Domain; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = questionRR.m_Header.m_Attributes.m_u16Type; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = questionRR.m_Header.m_Attributes.m_u16Class; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to add legacy question!\n"));); - } - } - else { - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Legacy query from NON-LOCAL host!\n"));); - bResult = false; - } - } - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to read question!\n"));); - } - } // for questions - - //DEBUG_EX_INFO(if (u8HostOrServiceReplies) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Reply needed: %u (%s: %s->%s)\n"), u8HostOrServiceReplies, clsTimeSyncer::timestr(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), IPAddress(m_pUDPContext->getDestAddress()).toString().c_str()); } ); - - // Handle known answers - uint32_t u32Answers = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount); - DEBUG_EX_INFO(if ((u8HostOrServiceReplies) && (u32Answers)) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Known answers(%u):\n"), u32Answers); } ); - - for (uint32_t an=0; ((bResult) && (anm_Header.m_Attributes.m_u16Type) && // No ANY type answer - (DNS_RRCLASS_ANY != pKnownRRAnswer->m_Header.m_Attributes.m_u16Class)) { // No ANY class answer - - // Find match between planned answer (sendParameter.m_u8HostReplyMask) and this 'known answer' - uint8_t u8HostMatchMask = (sendParameter.m_u8HostReplyMask & _replyMaskForHost(pKnownRRAnswer->m_Header)); - if ((u8HostMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND - ((MDNS_HOST_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) { // The TTL of the known answer is longer than half of the new host TTL (120s) - - // Compare contents - if (AnswerType_PTR == pKnownRRAnswer->answerType()) { - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain == hostDomain)) { - // Host domain match -#ifdef MDNS_IP4_SUPPORT - if (u8HostMatchMask & ContentFlag_PTR_IP4) { - // IP4 PTR was asked for, but is already known -> skipping - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP4 PTR already known... skipping!\n"));); - sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP4; - } -#endif -#ifdef MDNS_IP6_SUPPORT - if (u8HostMatchMask & ContentFlag_PTR_IP6) { - // IP6 PTR was asked for, but is already known -> skipping - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP6 PTR already known... skipping!\n"));); - sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP6; - } -#endif - } - } - else if (u8HostMatchMask & ContentFlag_A) { - // IP4 address was asked for -#ifdef MDNS_IP4_SUPPORT - if ((AnswerType_A == pKnownRRAnswer->answerType()) && - (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == _getResponseMulticastInterface())) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP4 address already known... skipping!\n"));); - sendParameter.m_u8HostReplyMask &= ~ContentFlag_A; - } // else: RData NOT IP4 length !! -#endif - } - else if (u8HostMatchMask & ContentFlag_AAAA) { - // IP6 address was asked for -#ifdef MDNS_IP6_SUPPORT - if ((AnswerType_AAAA == pAnswerRR->answerType()) && - (((stcMDNS_RRAnswerAAAA*)pAnswerRR)->m_IPAddress == _getResponseMulticastInterface())) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP6 address already known... skipping!\n"));); - sendParameter.m_u8HostReplyMask &= ~ContentFlag_AAAA; - } // else: RData NOT IP6 length !! -#endif - } - } // Host match /*and TTL*/ - - // - // Check host tiebreak possibility - if (m_HostProbeInformation.m_bTiebreakNeeded) { - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (pKnownRRAnswer->m_Header.m_Domain == hostDomain)) { - // Host domain match -#ifdef MDNS_IP4_SUPPORT - if (AnswerType_A == pKnownRRAnswer->answerType()) { - IPAddress localIPAddress(_getResponseMulticastInterface()); - if (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == localIPAddress) { - // SAME IP address -> We've received an old message from ourselfs (same IP) - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (was an old message)!\n"));); - m_HostProbeInformation.m_bTiebreakNeeded = false; - } - else { - if ((uint32_t)(((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress) > (uint32_t)localIPAddress) { // The OTHER IP is 'higher' -> LOST - // LOST tiebreak - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) LOST (lower)!\n"));); - _cancelProbingForHost(); - m_HostProbeInformation.m_bTiebreakNeeded = false; - } - else { // WON tiebreak - //TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (higher IP)!\n"));); - m_HostProbeInformation.m_bTiebreakNeeded = false; - } - } - } -#endif -#ifdef MDNS_IP6_SUPPORT - if (AnswerType_AAAA == pAnswerRR->answerType()) { - // TODO - } -#endif - } - } // Host tiebreak possibility - - // Check service answers - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - - uint8_t u8ServiceMatchMask = (pService->m_u8ReplyMask & _replyMaskForService(pKnownRRAnswer->m_Header, *pService)); - - if ((u8ServiceMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND - ((MDNS_SERVICE_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) { // The TTL of the known answer is longer than half of the new service TTL (4500s) - - if (AnswerType_PTR == pKnownRRAnswer->answerType()) { - stcMDNS_RRDomain serviceDomain; - if ((u8ServiceMatchMask & ContentFlag_PTR_TYPE) && - (_buildDomainForService(*pService, false, serviceDomain)) && - (serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service type PTR already known... skipping!\n"));); - pService->m_u8ReplyMask &= ~ContentFlag_PTR_TYPE; - } - if ((u8ServiceMatchMask & ContentFlag_PTR_NAME) && - (_buildDomainForService(*pService, true, serviceDomain)) && - (serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service name PTR already known... skipping!\n"));); - pService->m_u8ReplyMask &= ~ContentFlag_PTR_NAME; - } - } - else if (u8ServiceMatchMask & ContentFlag_SRV) { - DEBUG_EX_ERR(if (AnswerType_SRV != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (SRV)!\n"));); - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) { // Host domain match - - if ((MDNS_SRV_PRIORITY == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Priority) && - (MDNS_SRV_WEIGHT == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Weight) && - (pService->m_u16Port == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Port)) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service SRV answer already known... skipping!\n"));); - pService->m_u8ReplyMask &= ~ContentFlag_SRV; - } // else: Small differences -> send update message - } - } - else if (u8ServiceMatchMask & ContentFlag_TXT) { - DEBUG_EX_ERR(if (AnswerType_TXT != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (TXT)!\n"));); - _collectServiceTxts(*pService); - if (pService->m_Txts == ((stcMDNS_RRAnswerTXT*)pKnownRRAnswer)->m_Txts) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service TXT answer already known... skipping!\n"));); - pService->m_u8ReplyMask &= ~ContentFlag_TXT; - } - _releaseTempServiceTxts(*pService); - } - } // Service match and enough TTL - - // - // Check service tiebreak possibility - if (pService->m_ProbeInformation.m_bTiebreakNeeded) { - stcMDNS_RRDomain serviceDomain; - if ((_buildDomainForService(*pService, true, serviceDomain)) && - (pKnownRRAnswer->m_Header.m_Domain == serviceDomain)) { - // Service domain match - if (AnswerType_SRV == pKnownRRAnswer->answerType()) { - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) { // Host domain match - - // We've received an old message from ourselfs (same SRV) - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (was an old message)!\n"));); - pService->m_ProbeInformation.m_bTiebreakNeeded = false; - } - else { - if (((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain > hostDomain) { // The OTHER domain is 'higher' -> LOST - // LOST tiebreak - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) LOST (lower)!\n"));); - _cancelProbingForService(*pService); - pService->m_ProbeInformation.m_bTiebreakNeeded = false; - } - else { // WON tiebreak - //TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore - DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (higher)!\n"));); - pService->m_ProbeInformation.m_bTiebreakNeeded = false; - } - } - } - } - } // service tiebreak possibility - } // for services - } // ANY answers - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to read known answer!\n"));); - } - - if (pKnownRRAnswer) { - delete pKnownRRAnswer; - pKnownRRAnswer = 0; - } - } // for answers - - if (bResult) { - // Check, if a reply is needed - uint8_t u8ReplyNeeded = sendParameter.m_u8HostReplyMask; - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - u8ReplyNeeded |= pService->m_u8ReplyMask; - } - - if (u8ReplyNeeded) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Sending answer(0x%X)...\n"), u8ReplyNeeded);); - - sendParameter.m_bResponse = true; - sendParameter.m_bAuthorative = true; - - bResult = _sendMDNSMessage(sendParameter); - } - DEBUG_EX_INFO( - else { - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: No reply needed\n")); - } - ); - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Something FAILED!\n"));); - m_pUDPContext->flush(); - } - - // - // Check and reset tiebreak-states - if (m_HostProbeInformation.m_bTiebreakNeeded) { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for host domain!\n"));); - m_HostProbeInformation.m_bTiebreakNeeded = false; - } - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - if (pService->m_ProbeInformation.m_bTiebreakNeeded) { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for service domain (%s.%s.%s)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); - pService->m_ProbeInformation.m_bTiebreakNeeded = false; - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_parseResponse - * - * Responses are of interest in two cases: - * 1. find domain name conflicts while probing - * 2. get answers to service queries - * - * In both cases any included questions are ignored - * - * 1. If any answer has a domain name similar to one of the domain names we're planning to use (and are probing for), - * then we've got a 'probing conflict'. The conflict has to be solved on our side of the conflict (eg. by - * setting a new hostname and restart probing). The callback 'm_fnProbeResultCallback' is called with - * 'p_bProbeResult=false' in this case. - * - * 2. Service queries like '_http._tcp.local' will (if available) produce PTR, SRV, TXT and A/AAAA answers. - * All stored answers are pivoted by the service instance name (from the PTR record). Other answer parts, - * like host domain or IP address are than attached to this element. - * Any answer part carries a TTL, this is also stored (incl. the reception time); if the TTL is '0' the - * answer (part) is withdrawn by the sender and should be removed from any cache. RFC 6762, 10.1 proposes to - * set the caches TTL-value to 1 second in such a case and to delete the item only, if no update has - * has taken place in this second. - * Answer parts may arrive in 'unsorted' order, so they are grouped into three levels: - * Level 1: PRT - names the service instance (and is used as pivot), voids all other parts if is withdrawn or outdates - * Level 2: SRV - links the instance name to a host domain and port, voids A/AAAA parts if is withdrawn or outdates - * TXT - links the instance name to services TXTs - * Level 3: A/AAAA - links the host domain to an IP address - */ -bool MDNSResponder::_parseResponse(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse\n"));); - //DEBUG_EX_INFO(_udpDump();); - - bool bResult = false; - - // A response should be the result of a query or a probe - if ((_hasServiceQueriesWaitingForAnswers()) || // Waiting for query answers OR - (_hasProbesWaitingForAnswers())) { // Probe responses - - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: Received a response\n")); - //_udpDump(); - ); - - bResult = true; - // - // Ignore questions here - stcMDNS_RRQuestion dummyRRQ; - for (uint16_t qd=0; ((bResult) && (qdm_pNext = pCollectedRRAnswers; - pCollectedRRAnswers = pRRAnswer; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED to read answer!\n"));); - if (pRRAnswer) { - delete pRRAnswer; - pRRAnswer = 0; - } - bResult = false; - } - } // for answers - - // - // Process answers - if (bResult) { - bResult = ((!pCollectedRRAnswers) || - (_processAnswers(pCollectedRRAnswers))); - } - else { // Some failure while reading answers - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED to read answers!\n"));); - m_pUDPContext->flush(); - } - - // Delete collected answers - while (pCollectedRRAnswers) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: DELETING answer!\n"));); - stcMDNS_RRAnswer* pNextAnswer = pCollectedRRAnswers->m_pNext; - delete pCollectedRRAnswers; - pCollectedRRAnswers = pNextAnswer; - } - } - else { // Received an unexpected response -> ignore - /*DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: Received an unexpected response... ignoring!\nDUMP:\n")); - bool bDumpResult = true; - for (uint16_t qd=0; ((bDumpResult) && (qdflush(); - bResult = true; - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_processAnswers - * Host: - * A (0x01): eg. esp8266.local A OP TTL 123.456.789.012 - * AAAA (01Cx): eg. esp8266.local AAAA OP TTL 1234:5678::90 - * PTR (0x0C, IP4): eg. 012.789.456.123.in-addr.arpa PTR OP TTL esp8266.local - * PTR (0x0C, IP6): eg. 90.0.0.0.0.0.0.0.0.0.0.0.78.56.34.12.ip6.arpa PTR OP TTL esp8266.local - * Service: - * PTR (0x0C, srv name): eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local - * PTR (0x0C, srv type): eg. _services._dns-sd._udp.local PTR OP TTL _http._tcp.local - * SRV (0x21): eg. MyESP._http._tcp.local SRV OP TTL PRIORITY WEIGHT PORT esp8266.local - * TXT (0x10): eg. MyESP._http._tcp.local TXT OP TTL c#=1 - * - */ -bool MDNSResponder::_processAnswers(const MDNSResponder::stcMDNS_RRAnswer* p_pAnswers) { - - bool bResult = false; - - if (p_pAnswers) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Processing answers...\n"));); - bResult = true; - - // Answers may arrive in an unexpected order. So we loop our answers as long, as we - // can connect new information to service queries - bool bFoundNewKeyAnswer; - do { - bFoundNewKeyAnswer = false; - - const stcMDNS_RRAnswer* pRRAnswer = p_pAnswers; - while ((pRRAnswer) && - (bResult)) { - // 1. level answer (PTR) - if (AnswerType_PTR == pRRAnswer->answerType()) { - // eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local - bResult = _processPTRAnswer((stcMDNS_RRAnswerPTR*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new SRV or TXT answers to be linked to queries - } - // 2. level answers - // SRV -> host domain and port - else if (AnswerType_SRV == pRRAnswer->answerType()) { - // eg. MyESP_http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local - bResult = _processSRVAnswer((stcMDNS_RRAnswerSRV*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new A/AAAA answers to be linked to queries - } - // TXT -> Txts - else if (AnswerType_TXT == pRRAnswer->answerType()) { - // eg. MyESP_http._tcp.local TXT xxxx xx c#=1 - bResult = _processTXTAnswer((stcMDNS_RRAnswerTXT*)pRRAnswer); - } - // 3. level answers -#ifdef MDNS_IP4_SUPPORT - // A -> IP4Address - else if (AnswerType_A == pRRAnswer->answerType()) { - // eg. esp8266.local A xxxx xx 192.168.2.120 - bResult = _processAAnswer((stcMDNS_RRAnswerA*)pRRAnswer); - } -#endif -#ifdef MDNS_IP6_SUPPORT - // AAAA -> IP6Address - else if (AnswerType_AAAA == pRRAnswer->answerType()) { - // eg. esp8266.local AAAA xxxx xx 09cf::0c - bResult = _processAAAAAnswer((stcMDNS_RRAnswerAAAA*)pRRAnswer); - } -#endif - - // Finally check for probing conflicts - // Host domain - if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && - ((AnswerType_A == pRRAnswer->answerType()) || - (AnswerType_AAAA == pRRAnswer->answerType()))) { - - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (pRRAnswer->m_Header.m_Domain == hostDomain)) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.local\n"), m_pcHostname);); - _cancelProbingForHost(); - } - } - // Service domains - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && - ((AnswerType_TXT == pRRAnswer->answerType()) || - (AnswerType_SRV == pRRAnswer->answerType()))) { - - stcMDNS_RRDomain serviceDomain; - if ((_buildDomainForService(*pService, true, serviceDomain)) && - (pRRAnswer->m_Header.m_Domain == serviceDomain)) { - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.%s.%s\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); - _cancelProbingForService(*pService); - } - } - } - - pRRAnswer = pRRAnswer->m_pNext; // Next collected answer - } // while (answers) - } while ((bFoundNewKeyAnswer) && - (bResult)); - } // else: No answers provided - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_processPTRAnswer - */ -bool MDNSResponder::_processPTRAnswer(const MDNSResponder::stcMDNS_RRAnswerPTR* p_pPTRAnswer, - bool& p_rbFoundNewKeyAnswer) { - - bool bResult = false; - - if ((bResult = (0 != p_pPTRAnswer))) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: Processing PTR answers...\n"));); - // eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local - // Check pending service queries for eg. '_http._tcp' - - stcMDNSServiceQuery* pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, 0); - while (pServiceQuery) { - if (pServiceQuery->m_bAwaitingAnswers) { - // Find answer for service domain (eg. MyESP._http._tcp.local) - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pPTRAnswer->m_PTRDomain); - if (pSQAnswer) { // existing answer - if (p_pPTRAnswer->m_u32TTL) { // Received update message - pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL); // Update TTL tag - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: Updated TTL(%d) for "), (int)p_pPTRAnswer->m_u32TTL); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - ); - } - else { // received goodbye-message - pSQAnswer->m_TTLServiceDomain.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: 'Goodbye' received for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - ); - } - } - else if ((p_pPTRAnswer->m_u32TTL) && // Not just a goodbye-message - ((pSQAnswer = new stcMDNSServiceQuery::stcAnswer))) { // Not yet included -> add answer - pSQAnswer->m_ServiceDomain = p_pPTRAnswer->m_PTRDomain; - pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_ServiceDomain; - pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL); - pSQAnswer->releaseServiceDomain(); - - bResult = pServiceQuery->addAnswer(pSQAnswer); - p_rbFoundNewKeyAnswer = true; - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this,(hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_ServiceDomain), true); - } - } - } - pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, pServiceQuery); - } - } // else: No p_pPTRAnswer - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_processSRVAnswer - */ -bool MDNSResponder::_processSRVAnswer(const MDNSResponder::stcMDNS_RRAnswerSRV* p_pSRVAnswer, - bool& p_rbFoundNewKeyAnswer) { - - bool bResult = false; - - if ((bResult = (0 != p_pSRVAnswer))) { - // eg. MyESP._http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pSRVAnswer->m_Header.m_Domain); - if (pSQAnswer) { // Answer for this service domain (eg. MyESP._http._tcp.local) available - if (p_pSRVAnswer->m_u32TTL) { // First or update message (TTL != 0) - pSQAnswer->m_TTLHostDomainAndPort.set(p_pSRVAnswer->m_u32TTL); // Update TTL tag - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: Updated TTL(%d) for "), (int)p_pSRVAnswer->m_u32TTL); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); - ); - // Host domain & Port - if ((pSQAnswer->m_HostDomain != p_pSRVAnswer->m_SRVDomain) || - (pSQAnswer->m_u16Port != p_pSRVAnswer->m_u16Port)) { - - pSQAnswer->m_HostDomain = p_pSRVAnswer->m_SRVDomain; - pSQAnswer->releaseHostDomain(); - pSQAnswer->m_u16Port = p_pSRVAnswer->m_u16Port; - pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_HostDomainAndPort; - - p_rbFoundNewKeyAnswer = true; - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_HostDomainAndPort), true); - } - } - } - else { // Goodby message - pSQAnswer->m_TTLHostDomainAndPort.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: 'Goodbye' received for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); - ); - } - } - pServiceQuery = pServiceQuery->m_pNext; - } // while(service query) - } // else: No p_pSRVAnswer - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_processTXTAnswer - */ -bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT* p_pTXTAnswer) { - - bool bResult = false; - - if ((bResult = (0 != p_pTXTAnswer))) { - // eg. MyESP._http._tcp.local TXT xxxx xx c#=1 - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pTXTAnswer->m_Header.m_Domain); - if (pSQAnswer) { // Answer for this service domain (eg. MyESP._http._tcp.local) available - if (p_pTXTAnswer->m_u32TTL) { // First or update message - pSQAnswer->m_TTLTxts.set(p_pTXTAnswer->m_u32TTL); // Update TTL tag - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: Updated TTL(%d) for "), (int)p_pTXTAnswer->m_u32TTL); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); - ); - if (!pSQAnswer->m_Txts.compare(p_pTXTAnswer->m_Txts)) { - pSQAnswer->m_Txts = p_pTXTAnswer->m_Txts; - pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_Txts; - pSQAnswer->releaseTxts(); - - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo , static_cast(ServiceQueryAnswerType_Txts), true); - } - } - } - else { // Goodby message - pSQAnswer->m_TTLTxts.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: 'Goodbye' received for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); - ); - } - } - pServiceQuery = pServiceQuery->m_pNext; - } // while(service query) - } // else: No p_pTXTAnswer - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: FAILED!\n")); }); - return bResult; -} - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::_processAAnswer - */ - bool MDNSResponder::_processAAnswer(const MDNSResponder::stcMDNS_RRAnswerA* p_pAAnswer) { - - bool bResult = false; - - if ((bResult = (0 != p_pAAnswer))) { - // eg. esp8266.local A xxxx xx 192.168.2.120 - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAnswer->m_Header.m_Domain); - if (pSQAnswer) { // Answer for this host domain (eg. esp8266.local) available - stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = pSQAnswer->findIP4Address(p_pAAnswer->m_IPAddress); - if (pIP4Address) { - // Already known IP4 address - if (p_pAAnswer->m_u32TTL) { // Valid TTL -> Update answers TTL - pIP4Address->m_TTL.set(p_pAAnswer->m_u32TTL); - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: Updated TTL(%d) for "), (int)p_pAAnswer->m_u32TTL); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP4Address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str()); - ); - } - else { // 'Goodbye' message for known IP4 address - pIP4Address->m_TTL.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: 'Goodbye' received for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP4 address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str()); - ); - } - } - else { - // Until now unknown IP4 address -> Add (if the message isn't just a 'Goodbye' note) - if (p_pAAnswer->m_u32TTL) { // NOT just a 'Goodbye' message - pIP4Address = new stcMDNSServiceQuery::stcAnswer::stcIP4Address(p_pAAnswer->m_IPAddress, p_pAAnswer->m_u32TTL); - if ((pIP4Address) && - (pSQAnswer->addIP4Address(pIP4Address))) { - - pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address; - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo (*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_IP4Address), true); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED to add IP4 address (%s)!\n"), p_pAAnswer->m_IPAddress.toString().c_str());); - } - } - } - } - pServiceQuery = pServiceQuery->m_pNext; - } // while(service query) - } // else: No p_pAAnswer - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED!\n")); }); - return bResult; - } -#endif - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::_processAAAAAnswer - */ - bool MDNSResponder::_processAAAAAnswer(const MDNSResponder::stcMDNS_RRAnswerAAAA* p_pAAAAAnswer) { - - bool bResult = false; - - if ((bResult = (0 != p_pAAAAAnswer))) { - // eg. esp8266.local AAAA xxxx xx 0bf3::0c - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAAAAnswer->m_Header.m_Domain); - if (pSQAnswer) { // Answer for this host domain (eg. esp8266.local) available - stcIP6Address* pIP6Address = pSQAnswer->findIP6Address(p_pAAAAAnswer->m_IPAddress); - if (pIP6Address) { - // Already known IP6 address - if (p_pAAAAAnswer->m_u32TTL) { // Valid TTL -> Update answers TTL - pIP6Address->m_TTL.set(p_pAAAAAnswer->m_u32TTL); - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: Updated TTL(%lu) for "), p_pAAAAAnswer->m_u32TTL); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), pIP6Address->m_IPAddress.toString().c_str()); - ); - } - else { // 'Goodbye' message for known IP6 address - pIP6Address->m_TTL.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: 'Goodbye' received for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), pIP6Address->m_IPAddress.toString().c_str()); - ); - } - } - else { - // Until now unknown IP6 address -> Add (if the message isn't just a 'Goodbye' note) - if (p_pAAAAAnswer->m_u32TTL) { // NOT just a 'Goodbye' message - pIP6Address = new stcIP6Address(p_pAAAAAnswer->m_IPAddress, p_pAAAAAnswer->m_u32TTL); - if ((pIP6Address) && - (pSQAnswer->addIP6Address(pIP6Address))) { - - pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP6Address; - - if (pServiceQuery->m_fnCallback) { - pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP6Address, true, pServiceQuery->m_pUserdata); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED to add IP6 address (%s)!\n"), p_pAAAAAnswer->m_IPAddress.toString().c_str());); - } - } - } - } - pServiceQuery = pServiceQuery->m_pNext; - } // while(service query) - } // else: No p_pAAAAAnswer - - return bResult; - } -#endif - - -/* - * PROBING - */ - -/* - * MDNSResponder::_updateProbeStatus - * - * Manages the (outgoing) probing process. - * - If probing has not been started yet (ProbingStatus_NotStarted), the initial delay (see RFC 6762) is determined and - * the process is started - * - After timeout (of initial or subsequential delay) a probe message is send out for three times. If the message has - * already been sent out three times, the probing has been successful and is finished. - * - * Conflict management is handled in '_parseResponse ff.' - * Tiebraking is handled in 'parseQuery ff.' - */ -bool MDNSResponder::_updateProbeStatus(void) { - - bool bResult = true; - - // - // Probe host domain - if ((ProbingStatus_ReadyToStart == m_HostProbeInformation.m_ProbingStatus) && // Ready to get started AND - //TODO: Fix the following to allow Ethernet shield or other interfaces - (_getResponseMulticastInterface() != IPAddress())) { // Has IP address - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Starting host probing...\n"));); - - // First probe delay SHOULD be random 0-250 ms - m_HostProbeInformation.m_Timeout.reset(rand() % MDNS_PROBE_DELAY); - m_HostProbeInformation.m_ProbingStatus = ProbingStatus_InProgress; - } - else if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && // Probing AND - (m_HostProbeInformation.m_Timeout.expired())) { // Time for next probe - - if (MDNS_PROBE_COUNT > m_HostProbeInformation.m_u8SentCount) { // Send next probe - if ((bResult = _sendHostProbe())) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Did sent host probe\n\n"));); - m_HostProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); - ++m_HostProbeInformation.m_u8SentCount; - } - } - else { // Probing finished - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n"));); - m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done; - m_HostProbeInformation.m_Timeout.resetToNeverExpires(); - if (m_HostProbeInformation.m_fnHostProbeResultCallback) { - m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, true); - } - - // Prepare to announce host - m_HostProbeInformation.m_u8SentCount = 0; - m_HostProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Prepared host announcing.\n\n"));); - } - } // else: Probing already finished OR waiting for next time slot - else if ((ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus) && - (m_HostProbeInformation.m_Timeout.expired())) { - - if ((bResult = _announce(true, false))) { // Don't announce services here - ++m_HostProbeInformation.m_u8SentCount; - - if (MDNS_ANNOUNCE_COUNT > m_HostProbeInformation.m_u8SentCount) { - m_HostProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing host (%d).\n\n"), m_HostProbeInformation.m_u8SentCount);); - } - else { - m_HostProbeInformation.m_Timeout.resetToNeverExpires(); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host announcing.\n\n"));); - } - } - } - - // - // Probe services - for (stcMDNSService* pService=m_pServices; ((bResult) && (pService)); pService=pService->m_pNext) { - if (ProbingStatus_ReadyToStart == pService->m_ProbeInformation.m_ProbingStatus) { // Ready to get started - - pService->m_ProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); // More or equal than first probe for host domain - pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_InProgress; - } - else if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && // Probing AND - (pService->m_ProbeInformation.m_Timeout.expired())) { // Time for next probe - - if (MDNS_PROBE_COUNT > pService->m_ProbeInformation.m_u8SentCount) { // Send next probe - if ((bResult = _sendServiceProbe(*pService))) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Did sent service probe (%u)\n\n"), (pService->m_ProbeInformation.m_u8SentCount + 1));); - pService->m_ProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); - ++pService->m_ProbeInformation.m_u8SentCount; - } - } - else { // Probing finished - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service probing %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); - pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done; - pService->m_ProbeInformation.m_Timeout.resetToNeverExpires(); - if (pService->m_ProbeInformation.m_fnServiceProbeResultCallback) { - pService->m_ProbeInformation.m_fnServiceProbeResultCallback(pService->m_pcName, pService, true); - } - // Prepare to announce service - pService->m_ProbeInformation.m_u8SentCount = 0; - pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Prepared service announcing.\n\n"));); - } - } // else: Probing already finished OR waiting for next time slot - else if ((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus) && - (pService->m_ProbeInformation.m_Timeout.expired())) { - - if ((bResult = _announceService(*pService))) { // Announce service - ++pService->m_ProbeInformation.m_u8SentCount; - - if (MDNS_ANNOUNCE_COUNT > pService->m_ProbeInformation.m_u8SentCount) { - pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing service %s.%s.%s (%d)\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pService->m_ProbeInformation.m_u8SentCount);); - } - else { - pService->m_ProbeInformation.m_Timeout.resetToNeverExpires(); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service announcing for %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); - } - } - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: FAILED!\n\n")); }); - return bResult; -} - -/* - * MDNSResponder::_resetProbeStatus - * - * Resets the probe status. - * If 'p_bRestart' is set, the status is set to ProbingStatus_NotStarted. Consequently, - * when running 'updateProbeStatus' (which is done in every '_update' loop), the probing - * process is restarted. - */ -bool MDNSResponder::_resetProbeStatus(bool p_bRestart /*= true*/) { - - m_HostProbeInformation.clear(false); - m_HostProbeInformation.m_ProbingStatus = (p_bRestart ? ProbingStatus_ReadyToStart : ProbingStatus_Done); - - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - pService->m_ProbeInformation.clear(false); - pService->m_ProbeInformation.m_ProbingStatus = (p_bRestart ? ProbingStatus_ReadyToStart : ProbingStatus_Done); - } - return true; -} - -/* - * MDNSResponder::_hasProbesWaitingForAnswers - */ -bool MDNSResponder::_hasProbesWaitingForAnswers(void) const { - - bool bResult = ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && // Probing - (0 < m_HostProbeInformation.m_u8SentCount)); // And really probing - - for (stcMDNSService* pService=m_pServices; ((!bResult) && (pService)); pService=pService->m_pNext) { - bResult = ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && // Probing - (0 < pService->m_ProbeInformation.m_u8SentCount)); // And really probing - } - return bResult; -} - -/* - * MDNSResponder::_sendHostProbe - * - * Asks (probes) in the local network for the planned host domain - * - (eg. esp8266.local) - * - * To allow 'tiebreaking' (see '_parseQuery'), the answers for these questions are delivered in - * the 'knwon answers' section of the query. - * Host domain: - * - A/AAAA (eg. esp8266.esp -> 192.168.2.120) - */ -bool MDNSResponder::_sendHostProbe(void) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe (%s, %lu)\n"), m_pcHostname, millis());); - - bool bResult = true; - - // Requests for host domain - stcMDNSSendParameter sendParameter; - sendParameter.m_bCacheFlush = false; // RFC 6762 10.2 - - sendParameter.m_pQuestions = new stcMDNS_RRQuestion; - if (((bResult = (0 != sendParameter.m_pQuestions))) && - ((bResult = _buildDomainForHost(m_pcHostname, sendParameter.m_pQuestions->m_Header.m_Domain)))) { - - //sendParameter.m_pQuestions->m_bUnicast = true; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = DNS_RRTYPE_ANY; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (0x8000 | DNS_RRCLASS_IN); // Unicast & INternet - - // Add known answers -#ifdef MDNS_IP4_SUPPORT - sendParameter.m_u8HostReplyMask |= ContentFlag_A; // Add A answer -#endif -#ifdef MDNS_IP6_SUPPORT - sendParameter.m_u8HostReplyMask |= ContentFlag_AAAA; // Add AAAA answer -#endif - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe: FAILED to create host question!\n"));); - if (sendParameter.m_pQuestions) { - delete sendParameter.m_pQuestions; - sendParameter.m_pQuestions = 0; - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe: FAILED!\n")); }); - return ((bResult) && - (_sendMDNSMessage(sendParameter))); -} - -/* - * MDNSResponder::_sendServiceProbe - * - * Asks (probes) in the local network for the planned service instance domain - * - (eg. MyESP._http._tcp.local). - * - * To allow 'tiebreaking' (see '_parseQuery'), the answers for these questions are delivered in - * the 'knwon answers' section of the query. - * Service domain: - * - SRV (eg. MyESP._http._tcp.local -> 5000 esp8266.local) - * - PTR NAME (eg. _http._tcp.local -> MyESP._http._tcp.local) (TODO: Check if needed, maybe TXT is better) - */ -bool MDNSResponder::_sendServiceProbe(stcMDNSService& p_rService) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe (%s.%s.%s, %lu)\n"), (p_rService.m_pcName ?: m_pcHostname), p_rService.m_pcService, p_rService.m_pcProtocol, millis());); - - bool bResult = true; - - // Requests for service instance domain - stcMDNSSendParameter sendParameter; - sendParameter.m_bCacheFlush = false; // RFC 6762 10.2 - - sendParameter.m_pQuestions = new stcMDNS_RRQuestion; - if (((bResult = (0 != sendParameter.m_pQuestions))) && - ((bResult = _buildDomainForService(p_rService, true, sendParameter.m_pQuestions->m_Header.m_Domain)))) { - - sendParameter.m_pQuestions->m_bUnicast = true; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = DNS_RRTYPE_ANY; - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (0x8000 | DNS_RRCLASS_IN); // Unicast & INternet - - // Add known answers - p_rService.m_u8ReplyMask = (ContentFlag_SRV | ContentFlag_PTR_NAME); // Add SRV and PTR NAME answers - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe: FAILED to create service question!\n"));); - if (sendParameter.m_pQuestions) { - delete sendParameter.m_pQuestions; - sendParameter.m_pQuestions = 0; - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe: FAILED!\n")); }); - return ((bResult) && - (_sendMDNSMessage(sendParameter))); -} - -/* - * MDNSResponder::_cancelProbingForHost - */ -bool MDNSResponder::_cancelProbingForHost(void) { - - bool bResult = false; - - m_HostProbeInformation.clear(false); - // Send host notification - if (m_HostProbeInformation.m_fnHostProbeResultCallback) { - m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, false); - - bResult = true; - } - - for (stcMDNSService* pService=m_pServices; ((!bResult) && (pService)); pService=pService->m_pNext) { - bResult = _cancelProbingForService(*pService); - } - return bResult; -} - -/* - * MDNSResponder::_cancelProbingForService - */ -bool MDNSResponder::_cancelProbingForService(stcMDNSService& p_rService) { - - bool bResult = false; - - p_rService.m_ProbeInformation.clear(false); - // Send notification - if (p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback) { - p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback(p_rService.m_pcName,&p_rService,false); - bResult = true; - } - return bResult; -} - - - -/** - * ANNOUNCING - */ - -/* - * MDNSResponder::_announce - * - * Announces the host domain: - * - A/AAAA (eg. esp8266.local -> 192.168.2.120) - * - PTR (eg. 192.168.2.120.in-addr.arpa -> esp8266.local) - * - * and all presented services: - * - PTR_TYPE (_services._dns-sd._udp.local -> _http._tcp.local) - * - PTR_NAME (eg. _http._tcp.local -> MyESP8266._http._tcp.local) - * - SRV (eg. MyESP8266._http._tcp.local -> 5000 esp8266.local) - * - TXT (eg. MyESP8266._http._tcp.local -> c#=1) - * - * Goodbye (Un-Announcing) for the host domain and all services is also handled here. - * Goodbye messages are created by setting the TTL for the answer to 0, this happens - * inside the '_writeXXXAnswer' procs via 'sendParameter.m_bUnannounce = true' - */ -bool MDNSResponder::_announce(bool p_bAnnounce, - bool p_bIncludeServices) { - - bool bResult = false; - - stcMDNSSendParameter sendParameter; - if (ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus) { - - bResult = true; - - sendParameter.m_bResponse = true; // Announces are 'Unsolicited authorative responses' - sendParameter.m_bAuthorative = true; - sendParameter.m_bUnannounce = !p_bAnnounce; // When unannouncing, the TTL is set to '0' while creating the answers - - // Announce host - sendParameter.m_u8HostReplyMask = 0; - #ifdef MDNS_IP4_SUPPORT - sendParameter.m_u8HostReplyMask |= ContentFlag_A; // A answer - sendParameter.m_u8HostReplyMask |= ContentFlag_PTR_IP4; // PTR_IP4 answer - #endif - #ifdef MDNS_IP6_SUPPORT - sendParameter.m_u8HostReplyMask |= ContentFlag_AAAA; // AAAA answer - sendParameter.m_u8HostReplyMask |= ContentFlag_PTR_IP6; // PTR_IP6 answer - #endif - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: Announcing host %s (content 0x%X)\n"), m_pcHostname, sendParameter.m_u8HostReplyMask);); - - if (p_bIncludeServices) { - // Announce services (service type, name, SRV (location) and TXTs) - for (stcMDNSService* pService=m_pServices; ((bResult) && (pService)); pService=pService->m_pNext) { - if (ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus) { - pService->m_u8ReplyMask = (ContentFlag_PTR_TYPE | ContentFlag_PTR_NAME | ContentFlag_SRV | ContentFlag_TXT); - - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: Announcing service %s.%s.%s (content %u)\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pService->m_u8ReplyMask);); - } - } - } - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: FAILED!\n")); }); - return ((bResult) && - (_sendMDNSMessage(sendParameter))); -} - -/* - * MDNSResponder::_announceService - */ -bool MDNSResponder::_announceService(stcMDNSService& p_rService, - bool p_bAnnounce /*= true*/) { - - bool bResult = false; - - stcMDNSSendParameter sendParameter; - if (ProbingStatus_Done == p_rService.m_ProbeInformation.m_ProbingStatus) { - - sendParameter.m_bResponse = true; // Announces are 'Unsolicited authorative responses' - sendParameter.m_bAuthorative = true; - sendParameter.m_bUnannounce = !p_bAnnounce; // When unannouncing, the TTL is set to '0' while creating the answers - - // DON'T announce host - sendParameter.m_u8HostReplyMask = 0; - - // Announce services (service type, name, SRV (location) and TXTs) - p_rService.m_u8ReplyMask = (ContentFlag_PTR_TYPE | ContentFlag_PTR_NAME | ContentFlag_SRV | ContentFlag_TXT); - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announceService: Announcing service %s.%s.%s (content 0x%X)\n"), (p_rService.m_pcName ?: m_pcHostname), p_rService.m_pcService, p_rService.m_pcProtocol, p_rService.m_u8ReplyMask);); - - bResult = true; - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announceService: FAILED!\n")); }); - return ((bResult) && - (_sendMDNSMessage(sendParameter))); -} - - -/** - * SERVICE QUERY CACHE - */ - -/* - * MDNSResponder::_hasServiceQueriesWaitingForAnswers - */ -bool MDNSResponder::_hasServiceQueriesWaitingForAnswers(void) const { - - bool bOpenQueries = false; - - for (stcMDNSServiceQuery* pServiceQuery=m_pServiceQueries; pServiceQuery; pServiceQuery=pServiceQuery->m_pNext) { - if (pServiceQuery->m_bAwaitingAnswers) { - bOpenQueries = true; - break; - } - } - return bOpenQueries; -} - -/* - * MDNSResponder::_checkServiceQueryCache - * - * For any 'living' service query (m_bAwaitingAnswers == true) all available answers (their components) - * are checked for topicality based on the stored reception time and the answers TTL. - * When the components TTL is outlasted by more than 80%, a new question is generated, to get updated information. - * When no update arrived (in time), the component is removed from the answer (cache). - * - */ -bool MDNSResponder::_checkServiceQueryCache(void) { - - bool bResult = true; - - DEBUG_EX_INFO( - bool printedInfo = false; - ); - for (stcMDNSServiceQuery* pServiceQuery=m_pServiceQueries; ((bResult) && (pServiceQuery)); pServiceQuery=pServiceQuery->m_pNext) { - - // - // Resend dynamic service queries, if not already done often enough - if ((!pServiceQuery->m_bLegacyQuery) && - (MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount) && - (pServiceQuery->m_ResendTimeout.expired())) { - - if ((bResult = _sendMDNSServiceQuery(*pServiceQuery))) { - ++pServiceQuery->m_u8SentCount; - pServiceQuery->m_ResendTimeout.reset((MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount) - ? (MDNS_DYNAMIC_QUERY_RESEND_DELAY * (pServiceQuery->m_u8SentCount - 1)) - : esp8266::polledTimeout::oneShotMs::neverExpires); - } - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: %s to resend service query!"), (bResult ? "Succeeded" : "FAILED")); - printedInfo = true; - ); - } - - // - // Schedule updates for cached answers - if (pServiceQuery->m_bAwaitingAnswers) { - stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->m_pAnswers; - while ((bResult) && - (pSQAnswer)) { - stcMDNSServiceQuery::stcAnswer* pNextSQAnswer = pSQAnswer->m_pNext; - - // 1. level answer - if ((bResult) && - (pSQAnswer->m_TTLServiceDomain.flagged())) { - - if (!pSQAnswer->m_TTLServiceDomain.finalTimeoutLevel()) { - - bResult = ((_sendMDNSServiceQuery(*pServiceQuery)) && - (pSQAnswer->m_TTLServiceDomain.restart())); - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: PTR update scheduled for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" %s\n"), (bResult ? "OK" : "FAILURE")); - printedInfo = true; - ); - } - else { - // Timed out! -> Delete - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_ServiceDomain), false); - } - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove PTR answer for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - printedInfo = true; - ); - - bResult = pServiceQuery->removeAnswer(pSQAnswer); - pSQAnswer = 0; - continue; // Don't use this answer anymore - } - } // ServiceDomain flagged - - // 2. level answers - // HostDomain & Port (from SRV) - if ((bResult) && - (pSQAnswer->m_TTLHostDomainAndPort.flagged())) { - - if (!pSQAnswer->m_TTLHostDomainAndPort.finalTimeoutLevel()) { - - bResult = ((_sendMDNSQuery(pSQAnswer->m_ServiceDomain, DNS_RRTYPE_SRV)) && - (pSQAnswer->m_TTLHostDomainAndPort.restart())); - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: SRV update scheduled for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" host domain and port %s\n"), (bResult ? "OK" : "FAILURE")); - printedInfo = true; - ); - } - else { - // Timed out! -> Delete - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove SRV answer for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); - printedInfo = true; - ); - // Delete - pSQAnswer->m_HostDomain.clear(); - pSQAnswer->releaseHostDomain(); - pSQAnswer->m_u16Port = 0; - pSQAnswer->m_TTLHostDomainAndPort.set(0); - uint32_t u32ContentFlags = ServiceQueryAnswerType_HostDomainAndPort; - // As the host domain is the base for the IP4- and IP6Address, remove these too - #ifdef MDNS_IP4_SUPPORT - pSQAnswer->releaseIP4Addresses(); - u32ContentFlags |= ServiceQueryAnswerType_IP4Address; - #endif - #ifdef MDNS_IP6_SUPPORT - pSQAnswer->releaseIP6Addresses(); - u32ContentFlags |= ServiceQueryAnswerType_IP6Address; - #endif - - // Remove content flags for deleted answer parts - pSQAnswer->m_u32ContentFlags &= ~u32ContentFlags; - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo,static_cast(u32ContentFlags), false); - } - } - } // HostDomainAndPort flagged - - // Txts (from TXT) - if ((bResult) && - (pSQAnswer->m_TTLTxts.flagged())) { - - if (!pSQAnswer->m_TTLTxts.finalTimeoutLevel()) { - - bResult = ((_sendMDNSQuery(pSQAnswer->m_ServiceDomain, DNS_RRTYPE_TXT)) && - (pSQAnswer->m_TTLTxts.restart())); - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: TXT update scheduled for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" TXTs %s\n"), (bResult ? "OK" : "FAILURE")); - printedInfo = true; - ); - } - else { - // Timed out! -> Delete - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove TXT answer for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); - printedInfo = true; - ); - // Delete - pSQAnswer->m_Txts.clear(); - pSQAnswer->m_TTLTxts.set(0); - - // Remove content flags for deleted answer parts - pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_Txts; - - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_Txts), false); - } - } - } // TXTs flagged - - // 3. level answers -#ifdef MDNS_IP4_SUPPORT - // IP4Address (from A) - stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = pSQAnswer->m_pIP4Addresses; - bool bAUpdateQuerySent = false; - while ((pIP4Address) && - (bResult)) { - - stcMDNSServiceQuery::stcAnswer::stcIP4Address* pNextIP4Address = pIP4Address->m_pNext; // Get 'next' early, as 'current' may be deleted at the end... - - if (pIP4Address->m_TTL.flagged()) { - - if (!pIP4Address->m_TTL.finalTimeoutLevel()) { // Needs update - - if ((bAUpdateQuerySent) || - ((bResult = _sendMDNSQuery(pSQAnswer->m_HostDomain, DNS_RRTYPE_A)))) { - - pIP4Address->m_TTL.restart(); - bAUpdateQuerySent = true; - - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: IP4 update scheduled for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP4 address (%s)\n"), (pIP4Address->m_IPAddress.toString().c_str())); - printedInfo = true; - ); - } - } - else { - // Timed out! -> Delete - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove IP4 answer for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP4 address\n")); - printedInfo = true; - ); - pSQAnswer->removeIP4Address(pIP4Address); - if (!pSQAnswer->m_pIP4Addresses) { // NO IP4 address left -> remove content flag - pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_IP4Address; - } - // Notify client - if (pServiceQuery->m_fnCallback) { - MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); - pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_IP4Address), false); - } - } - } // IP4 flagged - - pIP4Address = pNextIP4Address; // Next - } // while -#endif -#ifdef MDNS_IP6_SUPPORT - // IP6Address (from AAAA) - stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = pSQAnswer->m_pIP6Addresses; - bool bAAAAUpdateQuerySent = false; - while ((pIP6Address) && - (bResult)) { - - stcMDNSServiceQuery::stcAnswer::stcIP6Address* pNextIP6Address = pIP6Address->m_pNext; // Get 'next' early, as 'current' may be deleted at the end... - - if (pIP6Address->m_TTL.flagged()) { - - if (!pIP6Address->m_TTL.finalTimeoutLevel()) { // Needs update - - if ((bAAAAUpdateQuerySent) || - ((bResult = _sendMDNSQuery(pSQAnswer->m_HostDomain, DNS_RRTYPE_AAAA)))) { - - pIP6Address->m_TTL.restart(); - bAAAAUpdateQuerySent = true; - - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: IP6 update scheduled for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), (pIP6Address->m_IPAddress.toString().c_str())); - printedInfo = true; - ); - } - } - else { - // Timed out! -> Delete - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove answer for ")); - _printRRDomain(pSQAnswer->m_ServiceDomain); - DEBUG_OUTPUT.printf_P(PSTR(" IP6Address\n")); - printedInfo = true; - ); - pSQAnswer->removeIP6Address(pIP6Address); - if (!pSQAnswer->m_pIP6Addresses) { // NO IP6 address left -> remove content flag - pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_IP6Address; - } - // Notify client - if (pServiceQuery->m_fnCallback) { - pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP6Address, false, pServiceQuery->m_pUserdata); - } - } - } // IP6 flagged - - pIP6Address = pNextIP6Address; // Next - } // while -#endif - pSQAnswer = pNextSQAnswer; - } - } - } - DEBUG_EX_INFO( - if (printedInfo) { - DEBUG_OUTPUT.printf_P(PSTR("\n")); - } - ); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: FAILED!\n")); }); - return bResult; -} - - -/* - * MDNSResponder::_replyMaskForHost - * - * Determines the relavant host answers for the given question. - * - A question for the hostname (eg. esp8266.local) will result in an A/AAAA (eg. 192.168.2.129) reply. - * - A question for the reverse IP address (eg. 192-168.2.120.inarpa.arpa) will result in an PTR_IP4 (eg. esp8266.local) reply. - * - * In addition, a full name match (question domain == host domain) is marked. - */ -uint8_t MDNSResponder::_replyMaskForHost(const MDNSResponder::stcMDNS_RRHeader& p_RRHeader, - bool* p_pbFullNameMatch /*= 0*/) const { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost\n"));); - - uint8_t u8ReplyMask = 0; - (p_pbFullNameMatch ? *p_pbFullNameMatch = false : 0); - - if ((DNS_RRCLASS_IN == p_RRHeader.m_Attributes.m_u16Class) || - (DNS_RRCLASS_ANY == p_RRHeader.m_Attributes.m_u16Class)) { - - if ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) { - // PTR request -#ifdef MDNS_IP4_SUPPORT - stcMDNS_RRDomain reverseIP4Domain; - if ((_buildDomainForReverseIP4(_getResponseMulticastInterface(), reverseIP4Domain)) && - (p_RRHeader.m_Domain == reverseIP4Domain)) { - // Reverse domain match - u8ReplyMask |= ContentFlag_PTR_IP4; - } -#endif -#ifdef MDNS_IP6_SUPPORT - // TODO -#endif - } // Address qeuest - - stcMDNS_RRDomain hostDomain; - if ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (p_RRHeader.m_Domain == hostDomain)) { // Host domain match - - (p_pbFullNameMatch ? (*p_pbFullNameMatch = true) : (0)); - -#ifdef MDNS_IP4_SUPPORT - if ((DNS_RRTYPE_A == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) { - // IP4 address request - u8ReplyMask |= ContentFlag_A; - } -#endif -#ifdef MDNS_IP6_SUPPORT - if ((DNS_RRTYPE_AAAA == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) { - // IP6 address request - u8ReplyMask |= ContentFlag_AAAA; - } -#endif - } - } - else { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost: INVALID RR-class (0x%04X)!\n"), p_RRHeader.m_Attributes.m_u16Class);); - } - DEBUG_EX_INFO(if (u8ReplyMask) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost: 0x%X\n"), u8ReplyMask); } ); - return u8ReplyMask; -} - -/* - * MDNSResponder::_replyMaskForService - * - * Determines the relevant service answers for the given question - * - A PTR dns-sd service enum question (_services.dns-sd._udp.local) will result into an PTR_TYPE (eg. _http._tcp.local) answer - * - A PTR service type question (eg. _http._tcp.local) will result into an PTR_NAME (eg. MyESP._http._tcp.local) answer - * - A PTR service name question (eg. MyESP._http._tcp.local) will result into an PTR_NAME (eg. MyESP._http._tcp.local) answer - * - A SRV service name question (eg. MyESP._http._tcp.local) will result into an SRV (eg. 5000 MyESP.local) answer - * - A TXT service name question (eg. MyESP._http._tcp.local) will result into an TXT (eg. c#=1) answer - * - * In addition, a full name match (question domain == service instance domain) is marked. - */ -uint8_t MDNSResponder::_replyMaskForService(const MDNSResponder::stcMDNS_RRHeader& p_RRHeader, - const MDNSResponder::stcMDNSService& p_Service, - bool* p_pbFullNameMatch /*= 0*/) const { - - uint8_t u8ReplyMask = 0; - (p_pbFullNameMatch ? *p_pbFullNameMatch = false : 0); - - if ((DNS_RRCLASS_IN == p_RRHeader.m_Attributes.m_u16Class) || - (DNS_RRCLASS_ANY == p_RRHeader.m_Attributes.m_u16Class)) { - - stcMDNS_RRDomain DNSSDDomain; - if ((_buildDomainForDNSSD(DNSSDDomain)) && // _services._dns-sd._udp.local - (p_RRHeader.m_Domain == DNSSDDomain) && - ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type))) { - // Common service info requested - u8ReplyMask |= ContentFlag_PTR_TYPE; - } - - stcMDNS_RRDomain serviceDomain; - if ((_buildDomainForService(p_Service, false, serviceDomain)) && // eg. _http._tcp.local - (p_RRHeader.m_Domain == serviceDomain) && - ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type))) { - // Special service info requested - u8ReplyMask |= ContentFlag_PTR_NAME; - } - - if ((_buildDomainForService(p_Service, true, serviceDomain)) && // eg. MyESP._http._tcp.local - (p_RRHeader.m_Domain == serviceDomain)) { - - (p_pbFullNameMatch ? (*p_pbFullNameMatch = true) : (0)); - - if ((DNS_RRTYPE_SRV == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) { - // Instance info SRV requested - u8ReplyMask |= ContentFlag_SRV; - } - if ((DNS_RRTYPE_TXT == p_RRHeader.m_Attributes.m_u16Type) || - (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) { - // Instance info TXT requested - u8ReplyMask |= ContentFlag_TXT; - } - } - } - else { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForService: INVALID RR-class (0x%04X)!\n"), p_RRHeader.m_Attributes.m_u16Class);); - } - DEBUG_EX_INFO(if (u8ReplyMask) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForService(%s.%s.%s): 0x%X\n"), p_Service.m_pcName, p_Service.m_pcService, p_Service.m_pcProtocol, u8ReplyMask); } ); - return u8ReplyMask; -} - -} // namespace MDNSImplementation - -} // namespace esp8266 + } + return bResult; +} + +/* + MDNSResponder::_restart +*/ +bool MDNSResponder::_restart(void) +{ + + return ((m_netif != nullptr) && + (m_netif->flags & NETIF_FLAG_UP) && // network interface is up and running + (_resetProbeStatus(true)) && // Stop and restart probing + (_allocUDPContext())); // Restart UDP +} + + +/** + RECEIVING +*/ + +/* + MDNSResponder::_parseMessage +*/ +bool MDNSResponder::_parseMessage(void) +{ + DEBUG_EX_INFO( + unsigned long ulStartTime = millis(); + unsigned uStartMemory = ESP.getFreeHeap(); + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage (Time: %lu ms, heap: %u bytes, from %s(%u), to %s(%u))\n"), ulStartTime, uStartMemory, + IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), m_pUDPContext->getRemotePort(), + IPAddress(m_pUDPContext->getDestAddress()).toString().c_str(), m_pUDPContext->getLocalPort()); + ); + //DEBUG_EX_INFO(_udpDump();); + + bool bResult = false; + + stcMDNS_MsgHeader header; + if (_readMDNSMsgHeader(header)) + { + if (0 == header.m_4bOpcode) // A standard query + { + if (header.m_1bQR) // Received a response -> answers to a query + { + //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Reading answers: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount);); + bResult = _parseResponse(header); + } + else // Received a query (Questions) + { + //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Reading query: ID:%u, Q:%u, A:%u, NS:%u, AR:%u\n"), header.m_u16ID, header.m_u16QDCount, header.m_u16ANCount, header.m_u16NSCount, header.m_u16ARCount);); + bResult = _parseQuery(header); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Received UNEXPECTED opcode:%u. Ignoring message!\n"), header.m_4bOpcode);); + m_pUDPContext->flush(); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: FAILED to read header\n"));); + m_pUDPContext->flush(); + } + DEBUG_EX_INFO( + unsigned uFreeHeap = ESP.getFreeHeap(); + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseMessage: Done (%s after %lu ms, ate %i bytes, remaining %u)\n\n"), (bResult ? "Succeeded" : "FAILED"), (millis() - ulStartTime), (uStartMemory - uFreeHeap), uFreeHeap); + ); + return bResult; +} + +/* + MDNSResponder::_parseQuery + + Queries are of interest in two cases: + 1. allow for tiebreaking while probing in the case of a race condition between two instances probing for + the same name at the same time + 2. provide answers to questions for our host domain or any presented service + + When reading the questions, a set of (planned) responses is created, eg. a reverse PTR question for the host domain + gets an A (IP address) response, a PTR question for the _services._dns-sd domain gets a PTR (type) response for any + registered service, ... + + As any mDNS responder should be able to handle 'legacy' queries (from DNS clients), this case is handled here also. + Legacy queries have got only one (unicast) question and are directed to the local DNS port (not the multicast port). + + 1. +*/ +bool MDNSResponder::_parseQuery(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) +{ + + bool bResult = true; + + stcMDNSSendParameter sendParameter; + uint8_t u8HostOrServiceReplies = 0; + for (uint16_t qd = 0; ((bResult) && (qd < p_MsgHeader.m_u16QDCount)); ++qd) + { + + stcMDNS_RRQuestion questionRR; + if ((bResult = _readRRQuestion(questionRR))) + { + // Define host replies, BUT only answer queries after probing is done + u8HostOrServiceReplies = + sendParameter.m_u8HostReplyMask |= (((m_bPassivModeEnabled) || + (ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus)) + ? _replyMaskForHost(questionRR.m_Header, 0) + : 0); + DEBUG_EX_INFO(if (u8HostOrServiceReplies) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Host reply needed 0x%X\n"), u8HostOrServiceReplies); + }); + + // Check tiebreak need for host domain + if (ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) + { + bool bFullNameMatch = false; + if ((_replyMaskForHost(questionRR.m_Header, &bFullNameMatch)) && + (bFullNameMatch)) + { + // We're in 'probing' state and someone is asking for our host domain: this might be + // a race-condition: Two host with the same domain names try simutanously to probe their domains + // See: RFC 6762, 8.2 (Tiebraking) + // However, we're using a max. reduced approach for tiebreaking here: The higher IP-address wins! + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Possible race-condition for host domain detected while probing.\n"));); + + m_HostProbeInformation.m_bTiebreakNeeded = true; + } + } + + // Define service replies + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + // Define service replies, BUT only answer queries after probing is done + uint8_t u8ReplyMaskForQuestion = (((m_bPassivModeEnabled) || + (ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus)) + ? _replyMaskForService(questionRR.m_Header, *pService, 0) + : 0); + u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion); + DEBUG_EX_INFO(if (u8ReplyMaskForQuestion) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service reply needed for (%s.%s.%s): 0x%X (%s)\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol, u8ReplyMaskForQuestion, IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str()); + }); + + // Check tiebreak need for service domain + if (ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) + { + bool bFullNameMatch = false; + if ((_replyMaskForService(questionRR.m_Header, *pService, &bFullNameMatch)) && + (bFullNameMatch)) + { + // We're in 'probing' state and someone is asking for this service domain: this might be + // a race-condition: Two services with the same domain names try simutanously to probe their domains + // See: RFC 6762, 8.2 (Tiebraking) + // However, we're using a max. reduced approach for tiebreaking here: The 'higher' SRV host wins! + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Possible race-condition for service domain %s.%s.%s detected while probing.\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); + + pService->m_ProbeInformation.m_bTiebreakNeeded = true; + } + } + } + + // Handle unicast and legacy specialities + // If only one question asks for unicast reply, the whole reply packet is send unicast + if (((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) || // Unicast (maybe legacy) query OR + (questionRR.m_bUnicast)) && // Expressivly unicast query + (!sendParameter.m_bUnicast)) + { + + sendParameter.m_bUnicast = true; + sendParameter.m_bCacheFlush = false; + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Unicast response for %s!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());); + + if ((DNS_MQUERY_PORT != m_pUDPContext->getRemotePort()) && // Unicast (maybe legacy) query AND + (1 == p_MsgHeader.m_u16QDCount) && // Only one question AND + ((sendParameter.m_u8HostReplyMask) || // Host replies OR + (u8HostOrServiceReplies))) // Host or service replies available + { + // We're a match for this legacy query, BUT + // make sure, that the query comes from a local host + ip_info IPInfo_Local; + ip_info IPInfo_Remote; + if (((IPInfo_Remote.ip.addr = m_pUDPContext->getRemoteAddress())) && + (((wifi_get_ip_info(SOFTAP_IF, &IPInfo_Local)) && + (ip4_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))) || // Remote IP in SOFTAP's subnet OR + ((wifi_get_ip_info(STATION_IF, &IPInfo_Local)) && + (ip4_addr_netcmp(&IPInfo_Remote.ip, &IPInfo_Local.ip, &IPInfo_Local.netmask))))) // Remote IP in STATION's subnet + { + + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Legacy query from local host %s, id %u!\n"), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), p_MsgHeader.m_u16ID);); + + sendParameter.m_u16ID = p_MsgHeader.m_u16ID; + sendParameter.m_bLegacyQuery = true; + sendParameter.m_pQuestions = new stcMDNS_RRQuestion; + if ((bResult = (0 != sendParameter.m_pQuestions))) + { + sendParameter.m_pQuestions->m_Header.m_Domain = questionRR.m_Header.m_Domain; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = questionRR.m_Header.m_Attributes.m_u16Type; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = questionRR.m_Header.m_Attributes.m_u16Class; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to add legacy question!\n"));); + } + } + else + { + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Legacy query from NON-LOCAL host!\n"));); + bResult = false; + } + } + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to read question!\n"));); + } + } // for questions + + //DEBUG_EX_INFO(if (u8HostOrServiceReplies) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Reply needed: %u (%s: %s->%s)\n"), u8HostOrServiceReplies, clsTimeSyncer::timestr(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str(), IPAddress(m_pUDPContext->getDestAddress()).toString().c_str()); } ); + + // Handle known answers + uint32_t u32Answers = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount); + DEBUG_EX_INFO(if ((u8HostOrServiceReplies) && (u32Answers)) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Known answers(%u):\n"), u32Answers); + }); + + for (uint32_t an = 0; ((bResult) && (an < u32Answers)); ++an) + { + stcMDNS_RRAnswer* pKnownRRAnswer = 0; + if (((bResult = _readRRAnswer(pKnownRRAnswer))) && + (pKnownRRAnswer)) + { + + if ((DNS_RRTYPE_ANY != pKnownRRAnswer->m_Header.m_Attributes.m_u16Type) && // No ANY type answer + (DNS_RRCLASS_ANY != pKnownRRAnswer->m_Header.m_Attributes.m_u16Class)) // No ANY class answer + { + + // Find match between planned answer (sendParameter.m_u8HostReplyMask) and this 'known answer' + uint8_t u8HostMatchMask = (sendParameter.m_u8HostReplyMask & _replyMaskForHost(pKnownRRAnswer->m_Header)); + if ((u8HostMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND + ((MDNS_HOST_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) // The TTL of the known answer is longer than half of the new host TTL (120s) + { + + // Compare contents + if (AnswerType_PTR == pKnownRRAnswer->answerType()) + { + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain == hostDomain)) + { + // Host domain match +#ifdef MDNS_IP4_SUPPORT + if (u8HostMatchMask & ContentFlag_PTR_IP4) + { + // IP4 PTR was asked for, but is already known -> skipping + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP4 PTR already known... skipping!\n"));); + sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP4; + } +#endif +#ifdef MDNS_IP6_SUPPORT + if (u8HostMatchMask & ContentFlag_PTR_IP6) + { + // IP6 PTR was asked for, but is already known -> skipping + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP6 PTR already known... skipping!\n"));); + sendParameter.m_u8HostReplyMask &= ~ContentFlag_PTR_IP6; + } +#endif + } + } + else if (u8HostMatchMask & ContentFlag_A) + { + // IP4 address was asked for +#ifdef MDNS_IP4_SUPPORT + if ((AnswerType_A == pKnownRRAnswer->answerType()) && + (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == _getResponseMulticastInterface())) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP4 address already known... skipping!\n"));); + sendParameter.m_u8HostReplyMask &= ~ContentFlag_A; + } // else: RData NOT IP4 length !! +#endif + } + else if (u8HostMatchMask & ContentFlag_AAAA) + { + // IP6 address was asked for +#ifdef MDNS_IP6_SUPPORT + if ((AnswerType_AAAA == pAnswerRR->answerType()) && + (((stcMDNS_RRAnswerAAAA*)pAnswerRR)->m_IPAddress == _getResponseMulticastInterface())) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: IP6 address already known... skipping!\n"));); + sendParameter.m_u8HostReplyMask &= ~ContentFlag_AAAA; + } // else: RData NOT IP6 length !! +#endif + } + } // Host match /*and TTL*/ + + // + // Check host tiebreak possibility + if (m_HostProbeInformation.m_bTiebreakNeeded) + { + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (pKnownRRAnswer->m_Header.m_Domain == hostDomain)) + { + // Host domain match +#ifdef MDNS_IP4_SUPPORT + if (AnswerType_A == pKnownRRAnswer->answerType()) + { + IPAddress localIPAddress(_getResponseMulticastInterface()); + if (((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress == localIPAddress) + { + // SAME IP address -> We've received an old message from ourselfs (same IP) + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (was an old message)!\n"));); + m_HostProbeInformation.m_bTiebreakNeeded = false; + } + else + { + if ((uint32_t)(((stcMDNS_RRAnswerA*)pKnownRRAnswer)->m_IPAddress) > (uint32_t)localIPAddress) // The OTHER IP is 'higher' -> LOST + { + // LOST tiebreak + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) LOST (lower)!\n"));); + _cancelProbingForHost(); + m_HostProbeInformation.m_bTiebreakNeeded = false; + } + else // WON tiebreak + { + //TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (IP4) WON (higher IP)!\n"));); + m_HostProbeInformation.m_bTiebreakNeeded = false; + } + } + } +#endif +#ifdef MDNS_IP6_SUPPORT + if (AnswerType_AAAA == pAnswerRR->answerType()) + { + // TODO + } +#endif + } + } // Host tiebreak possibility + + // Check service answers + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + + uint8_t u8ServiceMatchMask = (pService->m_u8ReplyMask & _replyMaskForService(pKnownRRAnswer->m_Header, *pService)); + + if ((u8ServiceMatchMask) && // The RR in the known answer matches an RR we are planning to send, AND + ((MDNS_SERVICE_TTL / 2) <= pKnownRRAnswer->m_u32TTL)) // The TTL of the known answer is longer than half of the new service TTL (4500s) + { + + if (AnswerType_PTR == pKnownRRAnswer->answerType()) + { + stcMDNS_RRDomain serviceDomain; + if ((u8ServiceMatchMask & ContentFlag_PTR_TYPE) && + (_buildDomainForService(*pService, false, serviceDomain)) && + (serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service type PTR already known... skipping!\n"));); + pService->m_u8ReplyMask &= ~ContentFlag_PTR_TYPE; + } + if ((u8ServiceMatchMask & ContentFlag_PTR_NAME) && + (_buildDomainForService(*pService, true, serviceDomain)) && + (serviceDomain == ((stcMDNS_RRAnswerPTR*)pKnownRRAnswer)->m_PTRDomain)) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service name PTR already known... skipping!\n"));); + pService->m_u8ReplyMask &= ~ContentFlag_PTR_NAME; + } + } + else if (u8ServiceMatchMask & ContentFlag_SRV) + { + DEBUG_EX_ERR(if (AnswerType_SRV != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (SRV)!\n"));); + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) // Host domain match + { + + if ((MDNS_SRV_PRIORITY == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Priority) && + (MDNS_SRV_WEIGHT == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Weight) && + (pService->m_u16Port == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_u16Port)) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service SRV answer already known... skipping!\n"));); + pService->m_u8ReplyMask &= ~ContentFlag_SRV; + } // else: Small differences -> send update message + } + } + else if (u8ServiceMatchMask & ContentFlag_TXT) + { + DEBUG_EX_ERR(if (AnswerType_TXT != pKnownRRAnswer->answerType()) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: ERROR! INVALID answer type (TXT)!\n"));); + _collectServiceTxts(*pService); + if (pService->m_Txts == ((stcMDNS_RRAnswerTXT*)pKnownRRAnswer)->m_Txts) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Service TXT answer already known... skipping!\n"));); + pService->m_u8ReplyMask &= ~ContentFlag_TXT; + } + _releaseTempServiceTxts(*pService); + } + } // Service match and enough TTL + + // + // Check service tiebreak possibility + if (pService->m_ProbeInformation.m_bTiebreakNeeded) + { + stcMDNS_RRDomain serviceDomain; + if ((_buildDomainForService(*pService, true, serviceDomain)) && + (pKnownRRAnswer->m_Header.m_Domain == serviceDomain)) + { + // Service domain match + if (AnswerType_SRV == pKnownRRAnswer->answerType()) + { + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (hostDomain == ((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain)) // Host domain match + { + + // We've received an old message from ourselfs (same SRV) + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (was an old message)!\n"));); + pService->m_ProbeInformation.m_bTiebreakNeeded = false; + } + else + { + if (((stcMDNS_RRAnswerSRV*)pKnownRRAnswer)->m_SRVDomain > hostDomain) // The OTHER domain is 'higher' -> LOST + { + // LOST tiebreak + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) LOST (lower)!\n"));); + _cancelProbingForService(*pService); + pService->m_ProbeInformation.m_bTiebreakNeeded = false; + } + else // WON tiebreak + { + //TiebreakState = TiebreakState_Won; // We received an 'old' message from ourselfs -> Just ignore + DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Tiebreak (SRV) won (higher)!\n"));); + pService->m_ProbeInformation.m_bTiebreakNeeded = false; + } + } + } + } + } // service tiebreak possibility + } // for services + } // ANY answers + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED to read known answer!\n"));); + } + + if (pKnownRRAnswer) + { + delete pKnownRRAnswer; + pKnownRRAnswer = 0; + } + } // for answers + + if (bResult) + { + // Check, if a reply is needed + uint8_t u8ReplyNeeded = sendParameter.m_u8HostReplyMask; + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + u8ReplyNeeded |= pService->m_u8ReplyMask; + } + + if (u8ReplyNeeded) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Sending answer(0x%X)...\n"), u8ReplyNeeded);); + + sendParameter.m_bResponse = true; + sendParameter.m_bAuthorative = true; + + bResult = _sendMDNSMessage(sendParameter); + } + DEBUG_EX_INFO( + else + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: No reply needed\n")); + } + ); + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: Something FAILED!\n"));); + m_pUDPContext->flush(); + } + + // + // Check and reset tiebreak-states + if (m_HostProbeInformation.m_bTiebreakNeeded) + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for host domain!\n"));); + m_HostProbeInformation.m_bTiebreakNeeded = false; + } + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + if (pService->m_ProbeInformation.m_bTiebreakNeeded) + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: UNSOLVED tiebreak-need for service domain (%s.%s.%s)\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); + pService->m_ProbeInformation.m_bTiebreakNeeded = false; + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseQuery: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_parseResponse + + Responses are of interest in two cases: + 1. find domain name conflicts while probing + 2. get answers to service queries + + In both cases any included questions are ignored + + 1. If any answer has a domain name similar to one of the domain names we're planning to use (and are probing for), + then we've got a 'probing conflict'. The conflict has to be solved on our side of the conflict (eg. by + setting a new hostname and restart probing). The callback 'm_fnProbeResultCallback' is called with + 'p_bProbeResult=false' in this case. + + 2. Service queries like '_http._tcp.local' will (if available) produce PTR, SRV, TXT and A/AAAA answers. + All stored answers are pivoted by the service instance name (from the PTR record). Other answer parts, + like host domain or IP address are than attached to this element. + Any answer part carries a TTL, this is also stored (incl. the reception time); if the TTL is '0' the + answer (part) is withdrawn by the sender and should be removed from any cache. RFC 6762, 10.1 proposes to + set the caches TTL-value to 1 second in such a case and to delete the item only, if no update has + has taken place in this second. + Answer parts may arrive in 'unsorted' order, so they are grouped into three levels: + Level 1: PRT - names the service instance (and is used as pivot), voids all other parts if is withdrawn or outdates + Level 2: SRV - links the instance name to a host domain and port, voids A/AAAA parts if is withdrawn or outdates + TXT - links the instance name to services TXTs + Level 3: A/AAAA - links the host domain to an IP address +*/ +bool MDNSResponder::_parseResponse(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse\n"));); + //DEBUG_EX_INFO(_udpDump();); + + bool bResult = false; + + // A response should be the result of a query or a probe + if ((_hasServiceQueriesWaitingForAnswers()) || // Waiting for query answers OR + (_hasProbesWaitingForAnswers())) // Probe responses + { + + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: Received a response\n")); + //_udpDump(); + ); + + bResult = true; + // + // Ignore questions here + stcMDNS_RRQuestion dummyRRQ; + for (uint16_t qd = 0; ((bResult) && (qd < p_MsgHeader.m_u16QDCount)); ++qd) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: Received a response containing a question... ignoring!\n"));); + bResult = _readRRQuestion(dummyRRQ); + } // for queries + + // + // Read and collect answers + stcMDNS_RRAnswer* pCollectedRRAnswers = 0; + uint32_t u32NumberOfAnswerRRs = (p_MsgHeader.m_u16ANCount + p_MsgHeader.m_u16NSCount + p_MsgHeader.m_u16ARCount); + for (uint32_t an = 0; ((bResult) && (an < u32NumberOfAnswerRRs)); ++an) + { + stcMDNS_RRAnswer* pRRAnswer = 0; + if (((bResult = _readRRAnswer(pRRAnswer))) && + (pRRAnswer)) + { + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: ADDING answer!\n"));); + pRRAnswer->m_pNext = pCollectedRRAnswers; + pCollectedRRAnswers = pRRAnswer; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED to read answer!\n"));); + if (pRRAnswer) + { + delete pRRAnswer; + pRRAnswer = 0; + } + bResult = false; + } + } // for answers + + // + // Process answers + if (bResult) + { + bResult = ((!pCollectedRRAnswers) || + (_processAnswers(pCollectedRRAnswers))); + } + else // Some failure while reading answers + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED to read answers!\n"));); + m_pUDPContext->flush(); + } + + // Delete collected answers + while (pCollectedRRAnswers) + { + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: DELETING answer!\n"));); + stcMDNS_RRAnswer* pNextAnswer = pCollectedRRAnswers->m_pNext; + delete pCollectedRRAnswers; + pCollectedRRAnswers = pNextAnswer; + } + } + else // Received an unexpected response -> ignore + { + /* DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: Received an unexpected response... ignoring!\nDUMP:\n")); + bool bDumpResult = true; + for (uint16_t qd=0; ((bDumpResult) && (qdflush(); + bResult = true; + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _parseResponse: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_processAnswers + Host: + A (0x01): eg. esp8266.local A OP TTL 123.456.789.012 + AAAA (01Cx): eg. esp8266.local AAAA OP TTL 1234:5678::90 + PTR (0x0C, IP4): eg. 012.789.456.123.in-addr.arpa PTR OP TTL esp8266.local + PTR (0x0C, IP6): eg. 90.0.0.0.0.0.0.0.0.0.0.0.78.56.34.12.ip6.arpa PTR OP TTL esp8266.local + Service: + PTR (0x0C, srv name): eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local + PTR (0x0C, srv type): eg. _services._dns-sd._udp.local PTR OP TTL _http._tcp.local + SRV (0x21): eg. MyESP._http._tcp.local SRV OP TTL PRIORITY WEIGHT PORT esp8266.local + TXT (0x10): eg. MyESP._http._tcp.local TXT OP TTL c#=1 + +*/ +bool MDNSResponder::_processAnswers(const MDNSResponder::stcMDNS_RRAnswer* p_pAnswers) +{ + + bool bResult = false; + + if (p_pAnswers) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Processing answers...\n"));); + bResult = true; + + // Answers may arrive in an unexpected order. So we loop our answers as long, as we + // can connect new information to service queries + bool bFoundNewKeyAnswer; + do + { + bFoundNewKeyAnswer = false; + + const stcMDNS_RRAnswer* pRRAnswer = p_pAnswers; + while ((pRRAnswer) && + (bResult)) + { + // 1. level answer (PTR) + if (AnswerType_PTR == pRRAnswer->answerType()) + { + // eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local + bResult = _processPTRAnswer((stcMDNS_RRAnswerPTR*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new SRV or TXT answers to be linked to queries + } + // 2. level answers + // SRV -> host domain and port + else if (AnswerType_SRV == pRRAnswer->answerType()) + { + // eg. MyESP_http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local + bResult = _processSRVAnswer((stcMDNS_RRAnswerSRV*)pRRAnswer, bFoundNewKeyAnswer); // May 'enable' new A/AAAA answers to be linked to queries + } + // TXT -> Txts + else if (AnswerType_TXT == pRRAnswer->answerType()) + { + // eg. MyESP_http._tcp.local TXT xxxx xx c#=1 + bResult = _processTXTAnswer((stcMDNS_RRAnswerTXT*)pRRAnswer); + } + // 3. level answers +#ifdef MDNS_IP4_SUPPORT + // A -> IP4Address + else if (AnswerType_A == pRRAnswer->answerType()) + { + // eg. esp8266.local A xxxx xx 192.168.2.120 + bResult = _processAAnswer((stcMDNS_RRAnswerA*)pRRAnswer); + } +#endif +#ifdef MDNS_IP6_SUPPORT + // AAAA -> IP6Address + else if (AnswerType_AAAA == pRRAnswer->answerType()) + { + // eg. esp8266.local AAAA xxxx xx 09cf::0c + bResult = _processAAAAAnswer((stcMDNS_RRAnswerAAAA*)pRRAnswer); + } +#endif + + // Finally check for probing conflicts + // Host domain + if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && + ((AnswerType_A == pRRAnswer->answerType()) || + (AnswerType_AAAA == pRRAnswer->answerType()))) + { + + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (pRRAnswer->m_Header.m_Domain == hostDomain)) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.local\n"), m_pcHostname);); + _cancelProbingForHost(); + } + } + // Service domains + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && + ((AnswerType_TXT == pRRAnswer->answerType()) || + (AnswerType_SRV == pRRAnswer->answerType()))) + { + + stcMDNS_RRDomain serviceDomain; + if ((_buildDomainForService(*pService, true, serviceDomain)) && + (pRRAnswer->m_Header.m_Domain == serviceDomain)) + { + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: Probing CONFLICT found with: %s.%s.%s\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); + _cancelProbingForService(*pService); + } + } + } + + pRRAnswer = pRRAnswer->m_pNext; // Next collected answer + } // while (answers) + } while ((bFoundNewKeyAnswer) && + (bResult)); + } // else: No answers provided + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAnswers: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_processPTRAnswer +*/ +bool MDNSResponder::_processPTRAnswer(const MDNSResponder::stcMDNS_RRAnswerPTR* p_pPTRAnswer, + bool& p_rbFoundNewKeyAnswer) +{ + + bool bResult = false; + + if ((bResult = (0 != p_pPTRAnswer))) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: Processing PTR answers...\n"));); + // eg. _http._tcp.local PTR xxxx xx MyESP._http._tcp.local + // Check pending service queries for eg. '_http._tcp' + + stcMDNSServiceQuery* pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, 0); + while (pServiceQuery) + { + if (pServiceQuery->m_bAwaitingAnswers) + { + // Find answer for service domain (eg. MyESP._http._tcp.local) + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pPTRAnswer->m_PTRDomain); + if (pSQAnswer) // existing answer + { + if (p_pPTRAnswer->m_u32TTL) // Received update message + { + pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL); // Update TTL tag + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: Updated TTL(%d) for "), (int)p_pPTRAnswer->m_u32TTL); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + ); + } + else // received goodbye-message + { + pSQAnswer->m_TTLServiceDomain.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: 'Goodbye' received for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + ); + } + } + else if ((p_pPTRAnswer->m_u32TTL) && // Not just a goodbye-message + ((pSQAnswer = new stcMDNSServiceQuery::stcAnswer))) // Not yet included -> add answer + { + pSQAnswer->m_ServiceDomain = p_pPTRAnswer->m_PTRDomain; + pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_ServiceDomain; + pSQAnswer->m_TTLServiceDomain.set(p_pPTRAnswer->m_u32TTL); + pSQAnswer->releaseServiceDomain(); + + bResult = pServiceQuery->addAnswer(pSQAnswer); + p_rbFoundNewKeyAnswer = true; + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_ServiceDomain), true); + } + } + } + pServiceQuery = _findNextServiceQueryByServiceType(p_pPTRAnswer->m_Header.m_Domain, pServiceQuery); + } + } // else: No p_pPTRAnswer + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processPTRAnswer: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_processSRVAnswer +*/ +bool MDNSResponder::_processSRVAnswer(const MDNSResponder::stcMDNS_RRAnswerSRV* p_pSRVAnswer, + bool& p_rbFoundNewKeyAnswer) +{ + + bool bResult = false; + + if ((bResult = (0 != p_pSRVAnswer))) + { + // eg. MyESP._http._tcp.local SRV xxxx xx yy zz 5000 esp8266.local + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pSRVAnswer->m_Header.m_Domain); + if (pSQAnswer) // Answer for this service domain (eg. MyESP._http._tcp.local) available + { + if (p_pSRVAnswer->m_u32TTL) // First or update message (TTL != 0) + { + pSQAnswer->m_TTLHostDomainAndPort.set(p_pSRVAnswer->m_u32TTL); // Update TTL tag + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: Updated TTL(%d) for "), (int)p_pSRVAnswer->m_u32TTL); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); + ); + // Host domain & Port + if ((pSQAnswer->m_HostDomain != p_pSRVAnswer->m_SRVDomain) || + (pSQAnswer->m_u16Port != p_pSRVAnswer->m_u16Port)) + { + + pSQAnswer->m_HostDomain = p_pSRVAnswer->m_SRVDomain; + pSQAnswer->releaseHostDomain(); + pSQAnswer->m_u16Port = p_pSRVAnswer->m_u16Port; + pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_HostDomainAndPort; + + p_rbFoundNewKeyAnswer = true; + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_HostDomainAndPort), true); + } + } + } + else // Goodby message + { + pSQAnswer->m_TTLHostDomainAndPort.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: 'Goodbye' received for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); + ); + } + } + pServiceQuery = pServiceQuery->m_pNext; + } // while(service query) + } // else: No p_pSRVAnswer + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processSRVAnswer: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_processTXTAnswer +*/ +bool MDNSResponder::_processTXTAnswer(const MDNSResponder::stcMDNS_RRAnswerTXT* p_pTXTAnswer) +{ + + bool bResult = false; + + if ((bResult = (0 != p_pTXTAnswer))) + { + // eg. MyESP._http._tcp.local TXT xxxx xx c#=1 + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForServiceDomain(p_pTXTAnswer->m_Header.m_Domain); + if (pSQAnswer) // Answer for this service domain (eg. MyESP._http._tcp.local) available + { + if (p_pTXTAnswer->m_u32TTL) // First or update message + { + pSQAnswer->m_TTLTxts.set(p_pTXTAnswer->m_u32TTL); // Update TTL tag + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: Updated TTL(%d) for "), (int)p_pTXTAnswer->m_u32TTL); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); + ); + if (!pSQAnswer->m_Txts.compare(p_pTXTAnswer->m_Txts)) + { + pSQAnswer->m_Txts = p_pTXTAnswer->m_Txts; + pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_Txts; + pSQAnswer->releaseTxts(); + + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_Txts), true); + } + } + } + else // Goodby message + { + pSQAnswer->m_TTLTxts.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: 'Goodbye' received for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); + ); + } + } + pServiceQuery = pServiceQuery->m_pNext; + } // while(service query) + } // else: No p_pTXTAnswer + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processTXTAnswer: FAILED!\n")); + }); + return bResult; +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::_processAAnswer +*/ +bool MDNSResponder::_processAAnswer(const MDNSResponder::stcMDNS_RRAnswerA* p_pAAnswer) +{ + + bool bResult = false; + + if ((bResult = (0 != p_pAAnswer))) + { + // eg. esp8266.local A xxxx xx 192.168.2.120 + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAnswer->m_Header.m_Domain); + if (pSQAnswer) // Answer for this host domain (eg. esp8266.local) available + { + stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = pSQAnswer->findIP4Address(p_pAAnswer->m_IPAddress); + if (pIP4Address) + { + // Already known IP4 address + if (p_pAAnswer->m_u32TTL) // Valid TTL -> Update answers TTL + { + pIP4Address->m_TTL.set(p_pAAnswer->m_u32TTL); + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: Updated TTL(%d) for "), (int)p_pAAnswer->m_u32TTL); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP4Address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str()); + ); + } + else // 'Goodbye' message for known IP4 address + { + pIP4Address->m_TTL.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: 'Goodbye' received for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP4 address (%s)\n"), pIP4Address->m_IPAddress.toString().c_str()); + ); + } + } + else + { + // Until now unknown IP4 address -> Add (if the message isn't just a 'Goodbye' note) + if (p_pAAnswer->m_u32TTL) // NOT just a 'Goodbye' message + { + pIP4Address = new stcMDNSServiceQuery::stcAnswer::stcIP4Address(p_pAAnswer->m_IPAddress, p_pAAnswer->m_u32TTL); + if ((pIP4Address) && + (pSQAnswer->addIP4Address(pIP4Address))) + { + + pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP4Address; + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_IP4Address), true); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED to add IP4 address (%s)!\n"), p_pAAnswer->m_IPAddress.toString().c_str());); + } + } + } + } + pServiceQuery = pServiceQuery->m_pNext; + } // while(service query) + } // else: No p_pAAnswer + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED!\n")); + }); + return bResult; +} +#endif + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::_processAAAAAnswer +*/ +bool MDNSResponder::_processAAAAAnswer(const MDNSResponder::stcMDNS_RRAnswerAAAA* p_pAAAAAnswer) +{ + + bool bResult = false; + + if ((bResult = (0 != p_pAAAAAnswer))) + { + // eg. esp8266.local AAAA xxxx xx 0bf3::0c + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->findAnswerForHostDomain(p_pAAAAAnswer->m_Header.m_Domain); + if (pSQAnswer) // Answer for this host domain (eg. esp8266.local) available + { + stcIP6Address* pIP6Address = pSQAnswer->findIP6Address(p_pAAAAAnswer->m_IPAddress); + if (pIP6Address) + { + // Already known IP6 address + if (p_pAAAAAnswer->m_u32TTL) // Valid TTL -> Update answers TTL + { + pIP6Address->m_TTL.set(p_pAAAAAnswer->m_u32TTL); + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: Updated TTL(%lu) for "), p_pAAAAAnswer->m_u32TTL); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), pIP6Address->m_IPAddress.toString().c_str()); + ); + } + else // 'Goodbye' message for known IP6 address + { + pIP6Address->m_TTL.prepareDeletion(); // Prepare answer deletion according to RFC 6762, 10.1 + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: 'Goodbye' received for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), pIP6Address->m_IPAddress.toString().c_str()); + ); + } + } + else + { + // Until now unknown IP6 address -> Add (if the message isn't just a 'Goodbye' note) + if (p_pAAAAAnswer->m_u32TTL) // NOT just a 'Goodbye' message + { + pIP6Address = new stcIP6Address(p_pAAAAAnswer->m_IPAddress, p_pAAAAAnswer->m_u32TTL); + if ((pIP6Address) && + (pSQAnswer->addIP6Address(pIP6Address))) + { + + pSQAnswer->m_u32ContentFlags |= ServiceQueryAnswerType_IP6Address; + + if (pServiceQuery->m_fnCallback) + { + pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP6Address, true, pServiceQuery->m_pUserdata); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _processAAnswer: FAILED to add IP6 address (%s)!\n"), p_pAAAAAnswer->m_IPAddress.toString().c_str());); + } + } + } + } + pServiceQuery = pServiceQuery->m_pNext; + } // while(service query) + } // else: No p_pAAAAAnswer + + return bResult; +} +#endif + + +/* + PROBING +*/ + +/* + MDNSResponder::_updateProbeStatus + + Manages the (outgoing) probing process. + - If probing has not been started yet (ProbingStatus_NotStarted), the initial delay (see RFC 6762) is determined and + the process is started + - After timeout (of initial or subsequential delay) a probe message is send out for three times. If the message has + already been sent out three times, the probing has been successful and is finished. + + Conflict management is handled in '_parseResponse ff.' + Tiebraking is handled in 'parseQuery ff.' +*/ +bool MDNSResponder::_updateProbeStatus(void) +{ + + bool bResult = true; + + // + // Probe host domain + if ((ProbingStatus_ReadyToStart == m_HostProbeInformation.m_ProbingStatus) && // Ready to get started AND + //TODO: Fix the following to allow Ethernet shield or other interfaces + (_getResponseMulticastInterface() != IPAddress())) // Has IP address + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Starting host probing...\n"));); + + // First probe delay SHOULD be random 0-250 ms + m_HostProbeInformation.m_Timeout.reset(rand() % MDNS_PROBE_DELAY); + m_HostProbeInformation.m_ProbingStatus = ProbingStatus_InProgress; + } + else if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && // Probing AND + (m_HostProbeInformation.m_Timeout.expired())) // Time for next probe + { + + if (MDNS_PROBE_COUNT > m_HostProbeInformation.m_u8SentCount) // Send next probe + { + if ((bResult = _sendHostProbe())) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Did sent host probe\n\n"));); + m_HostProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); + ++m_HostProbeInformation.m_u8SentCount; + } + } + else // Probing finished + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n"));); + m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done; + m_HostProbeInformation.m_Timeout.resetToNeverExpires(); + if (m_HostProbeInformation.m_fnHostProbeResultCallback) + { + m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, true); + } + + // Prepare to announce host + m_HostProbeInformation.m_u8SentCount = 0; + m_HostProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Prepared host announcing.\n\n"));); + } + } // else: Probing already finished OR waiting for next time slot + else if ((ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus) && + (m_HostProbeInformation.m_Timeout.expired())) + { + + if ((bResult = _announce(true, false))) // Don't announce services here + { + ++m_HostProbeInformation.m_u8SentCount; + + if (MDNS_ANNOUNCE_COUNT > m_HostProbeInformation.m_u8SentCount) + { + m_HostProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing host (%d).\n\n"), m_HostProbeInformation.m_u8SentCount);); + } + else + { + m_HostProbeInformation.m_Timeout.resetToNeverExpires(); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host announcing.\n\n"));); + } + } + } + + // + // Probe services + for (stcMDNSService* pService = m_pServices; ((bResult) && (pService)); pService = pService->m_pNext) + { + if (ProbingStatus_ReadyToStart == pService->m_ProbeInformation.m_ProbingStatus) // Ready to get started + { + + pService->m_ProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); // More or equal than first probe for host domain + pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_InProgress; + } + else if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && // Probing AND + (pService->m_ProbeInformation.m_Timeout.expired())) // Time for next probe + { + + if (MDNS_PROBE_COUNT > pService->m_ProbeInformation.m_u8SentCount) // Send next probe + { + if ((bResult = _sendServiceProbe(*pService))) + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Did sent service probe (%u)\n\n"), (pService->m_ProbeInformation.m_u8SentCount + 1));); + pService->m_ProbeInformation.m_Timeout.reset(MDNS_PROBE_DELAY); + ++pService->m_ProbeInformation.m_u8SentCount; + } + } + else // Probing finished + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service probing %s.%s.%s\n\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); + pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done; + pService->m_ProbeInformation.m_Timeout.resetToNeverExpires(); + if (pService->m_ProbeInformation.m_fnServiceProbeResultCallback) + { + pService->m_ProbeInformation.m_fnServiceProbeResultCallback(pService->m_pcName, pService, true); + } + // Prepare to announce service + pService->m_ProbeInformation.m_u8SentCount = 0; + pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Prepared service announcing.\n\n"));); + } + } // else: Probing already finished OR waiting for next time slot + else if ((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus) && + (pService->m_ProbeInformation.m_Timeout.expired())) + { + + if ((bResult = _announceService(*pService))) // Announce service + { + ++pService->m_ProbeInformation.m_u8SentCount; + + if (MDNS_ANNOUNCE_COUNT > pService->m_ProbeInformation.m_u8SentCount) + { + pService->m_ProbeInformation.m_Timeout.reset(MDNS_ANNOUNCE_DELAY); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing service %s.%s.%s (%d)\n\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pService->m_ProbeInformation.m_u8SentCount);); + } + else + { + pService->m_ProbeInformation.m_Timeout.resetToNeverExpires(); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service announcing for %s.%s.%s\n\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol);); + } + } + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: FAILED!\n\n")); + }); + return bResult; +} + +/* + MDNSResponder::_resetProbeStatus + + Resets the probe status. + If 'p_bRestart' is set, the status is set to ProbingStatus_NotStarted. Consequently, + when running 'updateProbeStatus' (which is done in every '_update' loop), the probing + process is restarted. +*/ +bool MDNSResponder::_resetProbeStatus(bool p_bRestart /*= true*/) +{ + + m_HostProbeInformation.clear(false); + m_HostProbeInformation.m_ProbingStatus = (p_bRestart ? ProbingStatus_ReadyToStart : ProbingStatus_Done); + + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + pService->m_ProbeInformation.clear(false); + pService->m_ProbeInformation.m_ProbingStatus = (p_bRestart ? ProbingStatus_ReadyToStart : ProbingStatus_Done); + } + return true; +} + +/* + MDNSResponder::_hasProbesWaitingForAnswers +*/ +bool MDNSResponder::_hasProbesWaitingForAnswers(void) const +{ + + bool bResult = ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && // Probing + (0 < m_HostProbeInformation.m_u8SentCount)); // And really probing + + for (stcMDNSService* pService = m_pServices; ((!bResult) && (pService)); pService = pService->m_pNext) + { + bResult = ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && // Probing + (0 < pService->m_ProbeInformation.m_u8SentCount)); // And really probing + } + return bResult; +} + +/* + MDNSResponder::_sendHostProbe + + Asks (probes) in the local network for the planned host domain + - (eg. esp8266.local) + + To allow 'tiebreaking' (see '_parseQuery'), the answers for these questions are delivered in + the 'knwon answers' section of the query. + Host domain: + - A/AAAA (eg. esp8266.esp -> 192.168.2.120) +*/ +bool MDNSResponder::_sendHostProbe(void) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe (%s, %lu)\n"), m_pcHostname, millis());); + + bool bResult = true; + + // Requests for host domain + stcMDNSSendParameter sendParameter; + sendParameter.m_bCacheFlush = false; // RFC 6762 10.2 + + sendParameter.m_pQuestions = new stcMDNS_RRQuestion; + if (((bResult = (0 != sendParameter.m_pQuestions))) && + ((bResult = _buildDomainForHost(m_pcHostname, sendParameter.m_pQuestions->m_Header.m_Domain)))) + { + + //sendParameter.m_pQuestions->m_bUnicast = true; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = DNS_RRTYPE_ANY; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (0x8000 | DNS_RRCLASS_IN); // Unicast & INternet + + // Add known answers +#ifdef MDNS_IP4_SUPPORT + sendParameter.m_u8HostReplyMask |= ContentFlag_A; // Add A answer +#endif +#ifdef MDNS_IP6_SUPPORT + sendParameter.m_u8HostReplyMask |= ContentFlag_AAAA; // Add AAAA answer +#endif + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe: FAILED to create host question!\n"));); + if (sendParameter.m_pQuestions) + { + delete sendParameter.m_pQuestions; + sendParameter.m_pQuestions = 0; + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendHostProbe: FAILED!\n")); + }); + return ((bResult) && + (_sendMDNSMessage(sendParameter))); +} + +/* + MDNSResponder::_sendServiceProbe + + Asks (probes) in the local network for the planned service instance domain + - (eg. MyESP._http._tcp.local). + + To allow 'tiebreaking' (see '_parseQuery'), the answers for these questions are delivered in + the 'knwon answers' section of the query. + Service domain: + - SRV (eg. MyESP._http._tcp.local -> 5000 esp8266.local) + - PTR NAME (eg. _http._tcp.local -> MyESP._http._tcp.local) (TODO: Check if needed, maybe TXT is better) +*/ +bool MDNSResponder::_sendServiceProbe(stcMDNSService& p_rService) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe (%s.%s.%s, %lu)\n"), (p_rService.m_pcName ? : m_pcHostname), p_rService.m_pcService, p_rService.m_pcProtocol, millis());); + + bool bResult = true; + + // Requests for service instance domain + stcMDNSSendParameter sendParameter; + sendParameter.m_bCacheFlush = false; // RFC 6762 10.2 + + sendParameter.m_pQuestions = new stcMDNS_RRQuestion; + if (((bResult = (0 != sendParameter.m_pQuestions))) && + ((bResult = _buildDomainForService(p_rService, true, sendParameter.m_pQuestions->m_Header.m_Domain)))) + { + + sendParameter.m_pQuestions->m_bUnicast = true; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = DNS_RRTYPE_ANY; + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (0x8000 | DNS_RRCLASS_IN); // Unicast & INternet + + // Add known answers + p_rService.m_u8ReplyMask = (ContentFlag_SRV | ContentFlag_PTR_NAME); // Add SRV and PTR NAME answers + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe: FAILED to create service question!\n"));); + if (sendParameter.m_pQuestions) + { + delete sendParameter.m_pQuestions; + sendParameter.m_pQuestions = 0; + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendServiceProbe: FAILED!\n")); + }); + return ((bResult) && + (_sendMDNSMessage(sendParameter))); +} + +/* + MDNSResponder::_cancelProbingForHost +*/ +bool MDNSResponder::_cancelProbingForHost(void) +{ + + bool bResult = false; + + m_HostProbeInformation.clear(false); + // Send host notification + if (m_HostProbeInformation.m_fnHostProbeResultCallback) + { + m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, false); + + bResult = true; + } + + for (stcMDNSService* pService = m_pServices; ((!bResult) && (pService)); pService = pService->m_pNext) + { + bResult = _cancelProbingForService(*pService); + } + return bResult; +} + +/* + MDNSResponder::_cancelProbingForService +*/ +bool MDNSResponder::_cancelProbingForService(stcMDNSService& p_rService) +{ + + bool bResult = false; + + p_rService.m_ProbeInformation.clear(false); + // Send notification + if (p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback) + { + p_rService.m_ProbeInformation.m_fnServiceProbeResultCallback(p_rService.m_pcName, &p_rService, false); + bResult = true; + } + return bResult; +} + + + +/** + ANNOUNCING +*/ + +/* + MDNSResponder::_announce + + Announces the host domain: + - A/AAAA (eg. esp8266.local -> 192.168.2.120) + - PTR (eg. 192.168.2.120.in-addr.arpa -> esp8266.local) + + and all presented services: + - PTR_TYPE (_services._dns-sd._udp.local -> _http._tcp.local) + - PTR_NAME (eg. _http._tcp.local -> MyESP8266._http._tcp.local) + - SRV (eg. MyESP8266._http._tcp.local -> 5000 esp8266.local) + - TXT (eg. MyESP8266._http._tcp.local -> c#=1) + + Goodbye (Un-Announcing) for the host domain and all services is also handled here. + Goodbye messages are created by setting the TTL for the answer to 0, this happens + inside the '_writeXXXAnswer' procs via 'sendParameter.m_bUnannounce = true' +*/ +bool MDNSResponder::_announce(bool p_bAnnounce, + bool p_bIncludeServices) +{ + + bool bResult = false; + + stcMDNSSendParameter sendParameter; + if (ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus) + { + + bResult = true; + + sendParameter.m_bResponse = true; // Announces are 'Unsolicited authorative responses' + sendParameter.m_bAuthorative = true; + sendParameter.m_bUnannounce = !p_bAnnounce; // When unannouncing, the TTL is set to '0' while creating the answers + + // Announce host + sendParameter.m_u8HostReplyMask = 0; +#ifdef MDNS_IP4_SUPPORT + sendParameter.m_u8HostReplyMask |= ContentFlag_A; // A answer + sendParameter.m_u8HostReplyMask |= ContentFlag_PTR_IP4; // PTR_IP4 answer +#endif +#ifdef MDNS_IP6_SUPPORT + sendParameter.m_u8HostReplyMask |= ContentFlag_AAAA; // AAAA answer + sendParameter.m_u8HostReplyMask |= ContentFlag_PTR_IP6; // PTR_IP6 answer +#endif + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: Announcing host %s (content 0x%X)\n"), m_pcHostname, sendParameter.m_u8HostReplyMask);); + + if (p_bIncludeServices) + { + // Announce services (service type, name, SRV (location) and TXTs) + for (stcMDNSService* pService = m_pServices; ((bResult) && (pService)); pService = pService->m_pNext) + { + if (ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus) + { + pService->m_u8ReplyMask = (ContentFlag_PTR_TYPE | ContentFlag_PTR_NAME | ContentFlag_SRV | ContentFlag_TXT); + + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: Announcing service %s.%s.%s (content %u)\n"), (pService->m_pcName ? : m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pService->m_u8ReplyMask);); + } + } + } + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announce: FAILED!\n")); + }); + return ((bResult) && + (_sendMDNSMessage(sendParameter))); +} + +/* + MDNSResponder::_announceService +*/ +bool MDNSResponder::_announceService(stcMDNSService& p_rService, + bool p_bAnnounce /*= true*/) +{ + + bool bResult = false; + + stcMDNSSendParameter sendParameter; + if (ProbingStatus_Done == p_rService.m_ProbeInformation.m_ProbingStatus) + { + + sendParameter.m_bResponse = true; // Announces are 'Unsolicited authorative responses' + sendParameter.m_bAuthorative = true; + sendParameter.m_bUnannounce = !p_bAnnounce; // When unannouncing, the TTL is set to '0' while creating the answers + + // DON'T announce host + sendParameter.m_u8HostReplyMask = 0; + + // Announce services (service type, name, SRV (location) and TXTs) + p_rService.m_u8ReplyMask = (ContentFlag_PTR_TYPE | ContentFlag_PTR_NAME | ContentFlag_SRV | ContentFlag_TXT); + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announceService: Announcing service %s.%s.%s (content 0x%X)\n"), (p_rService.m_pcName ? : m_pcHostname), p_rService.m_pcService, p_rService.m_pcProtocol, p_rService.m_u8ReplyMask);); + + bResult = true; + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _announceService: FAILED!\n")); + }); + return ((bResult) && + (_sendMDNSMessage(sendParameter))); +} + + +/** + SERVICE QUERY CACHE +*/ + +/* + MDNSResponder::_hasServiceQueriesWaitingForAnswers +*/ +bool MDNSResponder::_hasServiceQueriesWaitingForAnswers(void) const +{ + + bool bOpenQueries = false; + + for (stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; pServiceQuery; pServiceQuery = pServiceQuery->m_pNext) + { + if (pServiceQuery->m_bAwaitingAnswers) + { + bOpenQueries = true; + break; + } + } + return bOpenQueries; +} + +/* + MDNSResponder::_checkServiceQueryCache + + For any 'living' service query (m_bAwaitingAnswers == true) all available answers (their components) + are checked for topicality based on the stored reception time and the answers TTL. + When the components TTL is outlasted by more than 80%, a new question is generated, to get updated information. + When no update arrived (in time), the component is removed from the answer (cache). + +*/ +bool MDNSResponder::_checkServiceQueryCache(void) +{ + + bool bResult = true; + + DEBUG_EX_INFO( + bool printedInfo = false; + ); + for (stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; ((bResult) && (pServiceQuery)); pServiceQuery = pServiceQuery->m_pNext) + { + + // + // Resend dynamic service queries, if not already done often enough + if ((!pServiceQuery->m_bLegacyQuery) && + (MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount) && + (pServiceQuery->m_ResendTimeout.expired())) + { + + if ((bResult = _sendMDNSServiceQuery(*pServiceQuery))) + { + ++pServiceQuery->m_u8SentCount; + pServiceQuery->m_ResendTimeout.reset((MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount) + ? (MDNS_DYNAMIC_QUERY_RESEND_DELAY * (pServiceQuery->m_u8SentCount - 1)) + : esp8266::polledTimeout::oneShotMs::neverExpires); + } + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: %s to resend service query!"), (bResult ? "Succeeded" : "FAILED")); + printedInfo = true; + ); + } + + // + // Schedule updates for cached answers + if (pServiceQuery->m_bAwaitingAnswers) + { + stcMDNSServiceQuery::stcAnswer* pSQAnswer = pServiceQuery->m_pAnswers; + while ((bResult) && + (pSQAnswer)) + { + stcMDNSServiceQuery::stcAnswer* pNextSQAnswer = pSQAnswer->m_pNext; + + // 1. level answer + if ((bResult) && + (pSQAnswer->m_TTLServiceDomain.flagged())) + { + + if (!pSQAnswer->m_TTLServiceDomain.finalTimeoutLevel()) + { + + bResult = ((_sendMDNSServiceQuery(*pServiceQuery)) && + (pSQAnswer->m_TTLServiceDomain.restart())); + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: PTR update scheduled for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" %s\n"), (bResult ? "OK" : "FAILURE")); + printedInfo = true; + ); + } + else + { + // Timed out! -> Delete + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_ServiceDomain), false); + } + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove PTR answer for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + printedInfo = true; + ); + + bResult = pServiceQuery->removeAnswer(pSQAnswer); + pSQAnswer = 0; + continue; // Don't use this answer anymore + } + } // ServiceDomain flagged + + // 2. level answers + // HostDomain & Port (from SRV) + if ((bResult) && + (pSQAnswer->m_TTLHostDomainAndPort.flagged())) + { + + if (!pSQAnswer->m_TTLHostDomainAndPort.finalTimeoutLevel()) + { + + bResult = ((_sendMDNSQuery(pSQAnswer->m_ServiceDomain, DNS_RRTYPE_SRV)) && + (pSQAnswer->m_TTLHostDomainAndPort.restart())); + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: SRV update scheduled for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" host domain and port %s\n"), (bResult ? "OK" : "FAILURE")); + printedInfo = true; + ); + } + else + { + // Timed out! -> Delete + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove SRV answer for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" host domain and port\n")); + printedInfo = true; + ); + // Delete + pSQAnswer->m_HostDomain.clear(); + pSQAnswer->releaseHostDomain(); + pSQAnswer->m_u16Port = 0; + pSQAnswer->m_TTLHostDomainAndPort.set(0); + uint32_t u32ContentFlags = ServiceQueryAnswerType_HostDomainAndPort; + // As the host domain is the base for the IP4- and IP6Address, remove these too +#ifdef MDNS_IP4_SUPPORT + pSQAnswer->releaseIP4Addresses(); + u32ContentFlags |= ServiceQueryAnswerType_IP4Address; +#endif +#ifdef MDNS_IP6_SUPPORT + pSQAnswer->releaseIP6Addresses(); + u32ContentFlags |= ServiceQueryAnswerType_IP6Address; +#endif + + // Remove content flags for deleted answer parts + pSQAnswer->m_u32ContentFlags &= ~u32ContentFlags; + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(u32ContentFlags), false); + } + } + } // HostDomainAndPort flagged + + // Txts (from TXT) + if ((bResult) && + (pSQAnswer->m_TTLTxts.flagged())) + { + + if (!pSQAnswer->m_TTLTxts.finalTimeoutLevel()) + { + + bResult = ((_sendMDNSQuery(pSQAnswer->m_ServiceDomain, DNS_RRTYPE_TXT)) && + (pSQAnswer->m_TTLTxts.restart())); + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: TXT update scheduled for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" TXTs %s\n"), (bResult ? "OK" : "FAILURE")); + printedInfo = true; + ); + } + else + { + // Timed out! -> Delete + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove TXT answer for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" TXTs\n")); + printedInfo = true; + ); + // Delete + pSQAnswer->m_Txts.clear(); + pSQAnswer->m_TTLTxts.set(0); + + // Remove content flags for deleted answer parts + pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_Txts; + + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_Txts), false); + } + } + } // TXTs flagged + + // 3. level answers +#ifdef MDNS_IP4_SUPPORT + // IP4Address (from A) + stcMDNSServiceQuery::stcAnswer::stcIP4Address* pIP4Address = pSQAnswer->m_pIP4Addresses; + bool bAUpdateQuerySent = false; + while ((pIP4Address) && + (bResult)) + { + + stcMDNSServiceQuery::stcAnswer::stcIP4Address* pNextIP4Address = pIP4Address->m_pNext; // Get 'next' early, as 'current' may be deleted at the end... + + if (pIP4Address->m_TTL.flagged()) + { + + if (!pIP4Address->m_TTL.finalTimeoutLevel()) // Needs update + { + + if ((bAUpdateQuerySent) || + ((bResult = _sendMDNSQuery(pSQAnswer->m_HostDomain, DNS_RRTYPE_A)))) + { + + pIP4Address->m_TTL.restart(); + bAUpdateQuerySent = true; + + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: IP4 update scheduled for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP4 address (%s)\n"), (pIP4Address->m_IPAddress.toString().c_str())); + printedInfo = true; + ); + } + } + else + { + // Timed out! -> Delete + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove IP4 answer for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP4 address\n")); + printedInfo = true; + ); + pSQAnswer->removeIP4Address(pIP4Address); + if (!pSQAnswer->m_pIP4Addresses) // NO IP4 address left -> remove content flag + { + pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_IP4Address; + } + // Notify client + if (pServiceQuery->m_fnCallback) + { + MDNSServiceInfo serviceInfo(*this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer)); + pServiceQuery->m_fnCallback(serviceInfo, static_cast(ServiceQueryAnswerType_IP4Address), false); + } + } + } // IP4 flagged + + pIP4Address = pNextIP4Address; // Next + } // while +#endif +#ifdef MDNS_IP6_SUPPORT + // IP6Address (from AAAA) + stcMDNSServiceQuery::stcAnswer::stcIP6Address* pIP6Address = pSQAnswer->m_pIP6Addresses; + bool bAAAAUpdateQuerySent = false; + while ((pIP6Address) && + (bResult)) + { + + stcMDNSServiceQuery::stcAnswer::stcIP6Address* pNextIP6Address = pIP6Address->m_pNext; // Get 'next' early, as 'current' may be deleted at the end... + + if (pIP6Address->m_TTL.flagged()) + { + + if (!pIP6Address->m_TTL.finalTimeoutLevel()) // Needs update + { + + if ((bAAAAUpdateQuerySent) || + ((bResult = _sendMDNSQuery(pSQAnswer->m_HostDomain, DNS_RRTYPE_AAAA)))) + { + + pIP6Address->m_TTL.restart(); + bAAAAUpdateQuerySent = true; + + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: IP6 update scheduled for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP6 address (%s)\n"), (pIP6Address->m_IPAddress.toString().c_str())); + printedInfo = true; + ); + } + } + else + { + // Timed out! -> Delete + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: Will remove answer for ")); + _printRRDomain(pSQAnswer->m_ServiceDomain); + DEBUG_OUTPUT.printf_P(PSTR(" IP6Address\n")); + printedInfo = true; + ); + pSQAnswer->removeIP6Address(pIP6Address); + if (!pSQAnswer->m_pIP6Addresses) // NO IP6 address left -> remove content flag + { + pSQAnswer->m_u32ContentFlags &= ~ServiceQueryAnswerType_IP6Address; + } + // Notify client + if (pServiceQuery->m_fnCallback) + { + pServiceQuery->m_fnCallback(this, (hMDNSServiceQuery)pServiceQuery, pServiceQuery->indexOfAnswer(pSQAnswer), ServiceQueryAnswerType_IP6Address, false, pServiceQuery->m_pUserdata); + } + } + } // IP6 flagged + + pIP6Address = pNextIP6Address; // Next + } // while +#endif + pSQAnswer = pNextSQAnswer; + } + } + } + DEBUG_EX_INFO( + if (printedInfo) +{ + DEBUG_OUTPUT.printf_P(PSTR("\n")); + } + ); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: FAILED!\n")); + }); + return bResult; +} + + +/* + MDNSResponder::_replyMaskForHost + + Determines the relavant host answers for the given question. + - A question for the hostname (eg. esp8266.local) will result in an A/AAAA (eg. 192.168.2.129) reply. + - A question for the reverse IP address (eg. 192-168.2.120.inarpa.arpa) will result in an PTR_IP4 (eg. esp8266.local) reply. + + In addition, a full name match (question domain == host domain) is marked. +*/ +uint8_t MDNSResponder::_replyMaskForHost(const MDNSResponder::stcMDNS_RRHeader& p_RRHeader, + bool* p_pbFullNameMatch /*= 0*/) const +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost\n"));); + + uint8_t u8ReplyMask = 0; + (p_pbFullNameMatch ? *p_pbFullNameMatch = false : 0); + + if ((DNS_RRCLASS_IN == p_RRHeader.m_Attributes.m_u16Class) || + (DNS_RRCLASS_ANY == p_RRHeader.m_Attributes.m_u16Class)) + { + + if ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) + { + // PTR request +#ifdef MDNS_IP4_SUPPORT + stcMDNS_RRDomain reverseIP4Domain; + if ((_buildDomainForReverseIP4(_getResponseMulticastInterface(), reverseIP4Domain)) && + (p_RRHeader.m_Domain == reverseIP4Domain)) + { + // Reverse domain match + u8ReplyMask |= ContentFlag_PTR_IP4; + } +#endif +#ifdef MDNS_IP6_SUPPORT + // TODO +#endif + } // Address qeuest + + stcMDNS_RRDomain hostDomain; + if ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (p_RRHeader.m_Domain == hostDomain)) // Host domain match + { + + (p_pbFullNameMatch ? (*p_pbFullNameMatch = true) : (0)); + +#ifdef MDNS_IP4_SUPPORT + if ((DNS_RRTYPE_A == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) + { + // IP4 address request + u8ReplyMask |= ContentFlag_A; + } +#endif +#ifdef MDNS_IP6_SUPPORT + if ((DNS_RRTYPE_AAAA == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) + { + // IP6 address request + u8ReplyMask |= ContentFlag_AAAA; + } +#endif + } + } + else + { + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost: INVALID RR-class (0x%04X)!\n"), p_RRHeader.m_Attributes.m_u16Class);); + } + DEBUG_EX_INFO(if (u8ReplyMask) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForHost: 0x%X\n"), u8ReplyMask); + }); + return u8ReplyMask; +} + +/* + MDNSResponder::_replyMaskForService + + Determines the relevant service answers for the given question + - A PTR dns-sd service enum question (_services.dns-sd._udp.local) will result into an PTR_TYPE (eg. _http._tcp.local) answer + - A PTR service type question (eg. _http._tcp.local) will result into an PTR_NAME (eg. MyESP._http._tcp.local) answer + - A PTR service name question (eg. MyESP._http._tcp.local) will result into an PTR_NAME (eg. MyESP._http._tcp.local) answer + - A SRV service name question (eg. MyESP._http._tcp.local) will result into an SRV (eg. 5000 MyESP.local) answer + - A TXT service name question (eg. MyESP._http._tcp.local) will result into an TXT (eg. c#=1) answer + + In addition, a full name match (question domain == service instance domain) is marked. +*/ +uint8_t MDNSResponder::_replyMaskForService(const MDNSResponder::stcMDNS_RRHeader& p_RRHeader, + const MDNSResponder::stcMDNSService& p_Service, + bool* p_pbFullNameMatch /*= 0*/) const +{ + + uint8_t u8ReplyMask = 0; + (p_pbFullNameMatch ? *p_pbFullNameMatch = false : 0); + + if ((DNS_RRCLASS_IN == p_RRHeader.m_Attributes.m_u16Class) || + (DNS_RRCLASS_ANY == p_RRHeader.m_Attributes.m_u16Class)) + { + + stcMDNS_RRDomain DNSSDDomain; + if ((_buildDomainForDNSSD(DNSSDDomain)) && // _services._dns-sd._udp.local + (p_RRHeader.m_Domain == DNSSDDomain) && + ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type))) + { + // Common service info requested + u8ReplyMask |= ContentFlag_PTR_TYPE; + } + + stcMDNS_RRDomain serviceDomain; + if ((_buildDomainForService(p_Service, false, serviceDomain)) && // eg. _http._tcp.local + (p_RRHeader.m_Domain == serviceDomain) && + ((DNS_RRTYPE_PTR == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type))) + { + // Special service info requested + u8ReplyMask |= ContentFlag_PTR_NAME; + } + + if ((_buildDomainForService(p_Service, true, serviceDomain)) && // eg. MyESP._http._tcp.local + (p_RRHeader.m_Domain == serviceDomain)) + { + + (p_pbFullNameMatch ? (*p_pbFullNameMatch = true) : (0)); + + if ((DNS_RRTYPE_SRV == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) + { + // Instance info SRV requested + u8ReplyMask |= ContentFlag_SRV; + } + if ((DNS_RRTYPE_TXT == p_RRHeader.m_Attributes.m_u16Type) || + (DNS_RRTYPE_ANY == p_RRHeader.m_Attributes.m_u16Type)) + { + // Instance info TXT requested + u8ReplyMask |= ContentFlag_TXT; + } + } + } + else + { + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForService: INVALID RR-class (0x%04X)!\n"), p_RRHeader.m_Attributes.m_u16Class);); + } + DEBUG_EX_INFO(if (u8ReplyMask) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _replyMaskForService(%s.%s.%s): 0x%X\n"), p_Service.m_pcName, p_Service.m_pcService, p_Service.m_pcProtocol, u8ReplyMask); + }); + return u8ReplyMask; +} + +} // namespace MDNSImplementation + +} // namespace esp8266 diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp index cf5cab07b..d23941ce5 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Helpers.cpp @@ -1,750 +1,850 @@ -/* - * LEAmDNS_Helpers.cpp - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#include "lwip/igmp.h" - -#include "LEAmDNS_lwIPdefs.h" -#include "LEAmDNS_Priv.h" - - -namespace { - -/* - * strrstr (static) - * - * Backwards search for p_pcPattern in p_pcString - * Based on: https://stackoverflow.com/a/1634398/2778898 - * - */ -const char* strrstr(const char*__restrict p_pcString, const char*__restrict p_pcPattern) { - - const char* pcResult = 0; - - size_t stStringLength = (p_pcString ? strlen(p_pcString) : 0); - size_t stPatternLength = (p_pcPattern ? strlen(p_pcPattern) : 0); - - if ((stStringLength) && - (stPatternLength) && - (stPatternLength <= stStringLength)) { - // Pattern is shorter or has the same length tham the string - - for (const char* s=(p_pcString + stStringLength - stPatternLength); s>=p_pcString; --s) { - if (0 == strncmp(s, p_pcPattern, stPatternLength)) { - pcResult = s; - break; - } - } - } - return pcResult; -} - - -} // anonymous - - - - - -namespace esp8266 { - -/* - * LEAmDNS - */ -namespace MDNSImplementation { - -/** - * HELPERS - */ - -/* - * MDNSResponder::indexDomain (static) - * - * Updates the given domain 'p_rpcHostname' by appending a delimiter and an index number. - * - * If the given domain already hasa numeric index (after the given delimiter), this index - * incremented. If not, the delimiter and index '2' is added. - * - * If 'p_rpcHostname' is empty (==0), the given default name 'p_pcDefaultHostname' is used, - * if no default is given, 'esp8266' is used. - * - */ -/*static*/ bool MDNSResponder::indexDomain(char*& p_rpcDomain, - const char* p_pcDivider /*= "-"*/, - const char* p_pcDefaultDomain /*= 0*/) { - - bool bResult = false; - - // Ensure a divider exists; use '-' as default - const char* pcDivider = (p_pcDivider ?: "-"); - - if (p_rpcDomain) { - const char* pFoundDivider = strrstr(p_rpcDomain, pcDivider); - if (pFoundDivider) { // maybe already extended - char* pEnd = 0; - unsigned long ulIndex = strtoul((pFoundDivider + strlen(pcDivider)), &pEnd, 10); - if ((ulIndex) && - ((pEnd - p_rpcDomain) == (ptrdiff_t)strlen(p_rpcDomain)) && - (!*pEnd)) { // Valid (old) index found - - char acIndexBuffer[16]; - sprintf(acIndexBuffer, "%lu", (++ulIndex)); - size_t stLength = ((pFoundDivider - p_rpcDomain + strlen(pcDivider)) + strlen(acIndexBuffer) + 1); - char* pNewHostname = new char[stLength]; - if (pNewHostname) { - memcpy(pNewHostname, p_rpcDomain, (pFoundDivider - p_rpcDomain + strlen(pcDivider))); - pNewHostname[pFoundDivider - p_rpcDomain + strlen(pcDivider)] = 0; - strcat(pNewHostname, acIndexBuffer); - - delete[] p_rpcDomain; - p_rpcDomain = pNewHostname; - - bResult = true; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); - } - } - else { - pFoundDivider = 0; // Flag the need to (base) extend the hostname - } - } - - if (!pFoundDivider) { // not yet extended (or failed to increment extension) -> start indexing - size_t stLength = strlen(p_rpcDomain) + (strlen(pcDivider) + 1 + 1); // Name + Divider + '2' + '\0' - char* pNewHostname = new char[stLength]; - if (pNewHostname) { - sprintf(pNewHostname, "%s%s2", p_rpcDomain, pcDivider); - - delete[] p_rpcDomain; - p_rpcDomain = pNewHostname; - - bResult = true; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); - } - } - } - else { - // No given host domain, use base or default - const char* cpcDefaultName = (p_pcDefaultDomain ?: "esp8266"); - - size_t stLength = strlen(cpcDefaultName) + 1; // '\0' - p_rpcDomain = new char[stLength]; - if (p_rpcDomain) { - strncpy(p_rpcDomain, cpcDefaultName, stLength); - bResult = true; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); - } - } - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] indexDomain: %s\n"), p_rpcDomain);); - return bResult; -} - - -/* - * UDP CONTEXT - */ - -bool MDNSResponder::_callProcess(void) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf("[MDNSResponder] _callProcess (%lu, triggered by: %s)\n", millis(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());); - - return _process(false); -} - -/* - * MDNSResponder::_allocUDPContext - * - * (Re-)Creates the one-and-only UDP context for the MDNS responder. - * The context is added to the 'multicast'-group and listens to the MDNS port (5353). - * The travel-distance for multicast messages is set to 1 (local, via MDNS_MULTICAST_TTL). - * Messages are received via the MDNSResponder '_update' function. CAUTION: This function - * is called from the WiFi stack side of the ESP stack system. - * - */ -bool MDNSResponder::_allocUDPContext(void) { - DEBUG_EX_INFO(DEBUG_OUTPUT.println("[MDNSResponder] _allocUDPContext");); - - bool bResult = false; - - _releaseUDPContext(); - -#ifdef MDNS_IP4_SUPPORT - ip_addr_t multicast_addr = DNS_MQUERY_IPV4_GROUP_INIT; -#endif -#ifdef MDNS_IP6_SUPPORT - //TODO: set multicast address (lwip_joingroup() is IPv4 only at the time of writing) - multicast_addr.addr = DNS_MQUERY_IPV6_GROUP_INIT; -#endif - if (ERR_OK == igmp_joingroup(ip_2_ip4(&m_netif->ip_addr), ip_2_ip4(&multicast_addr))) { - m_pUDPContext = new UdpContext; - m_pUDPContext->ref(); - - if (m_pUDPContext->listen(IP4_ADDR_ANY, DNS_MQUERY_PORT)) { - m_pUDPContext->setMulticastTTL(MDNS_MULTICAST_TTL); - m_pUDPContext->onRx(std::bind(&MDNSResponder::_callProcess, this)); - - bResult = m_pUDPContext->connect(&multicast_addr, DNS_MQUERY_PORT); - } - } - return bResult; -} - -/* - * MDNSResponder::_releaseUDPContext - */ -bool MDNSResponder::_releaseUDPContext(void) { - - if (m_pUDPContext) { - m_pUDPContext->unref(); - m_pUDPContext = 0; - } - return true; -} - - -/* - * SERVICE QUERY - */ - -/* - * MDNSResponder::_allocServiceQuery - */ -MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_allocServiceQuery(void) { - - stcMDNSServiceQuery* pServiceQuery = new stcMDNSServiceQuery; - if (pServiceQuery) { - // Link to query list - pServiceQuery->m_pNext = m_pServiceQueries; - m_pServiceQueries = pServiceQuery; - } - return m_pServiceQueries; -} - -/* - * MDNSResponder::_removeServiceQuery - */ -bool MDNSResponder::_removeServiceQuery(MDNSResponder::stcMDNSServiceQuery* p_pServiceQuery) { - - bool bResult = false; - - if (p_pServiceQuery) { - stcMDNSServiceQuery* pPred = m_pServiceQueries; - while ((pPred) && - (pPred->m_pNext != p_pServiceQuery)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pServiceQuery->m_pNext; - delete p_pServiceQuery; - bResult = true; - } - else { // No predecesor - if (m_pServiceQueries == p_pServiceQuery) { - m_pServiceQueries = p_pServiceQuery->m_pNext; - delete p_pServiceQuery; - bResult = true; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.println("[MDNSResponder] _releaseServiceQuery: INVALID service query!");); - } - } - } - return bResult; -} - -/* - * MDNSResponder::_removeLegacyServiceQuery - */ -bool MDNSResponder::_removeLegacyServiceQuery(void) { - - stcMDNSServiceQuery* pLegacyServiceQuery = _findLegacyServiceQuery(); - return (pLegacyServiceQuery ? _removeServiceQuery(pLegacyServiceQuery) : true); -} - -/* - * MDNSResponder::_findServiceQuery - * - * 'Convert' hMDNSServiceQuery to stcMDNSServiceQuery* (ensure existance) - * - */ -MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findServiceQuery(MDNSResponder::hMDNSServiceQuery p_hServiceQuery) { - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - if ((hMDNSServiceQuery)pServiceQuery == p_hServiceQuery) { - break; - } - pServiceQuery = pServiceQuery->m_pNext; - } - return pServiceQuery; -} - -/* - * MDNSResponder::_findLegacyServiceQuery - */ -MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findLegacyServiceQuery(void) { - - stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; - while (pServiceQuery) { - if (pServiceQuery->m_bLegacyQuery) { - break; - } - pServiceQuery = pServiceQuery->m_pNext; - } - return pServiceQuery; -} - -/* - * MDNSResponder::_releaseServiceQueries - */ -bool MDNSResponder::_releaseServiceQueries(void) { - while (m_pServiceQueries) { - stcMDNSServiceQuery* pNext = m_pServiceQueries->m_pNext; - delete m_pServiceQueries; - m_pServiceQueries = pNext; - } - return true; -} - -/* - * MDNSResponder::_findNextServiceQueryByServiceType - */ -MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findNextServiceQueryByServiceType(const stcMDNS_RRDomain& p_ServiceTypeDomain, - const stcMDNSServiceQuery* p_pPrevServiceQuery) { - stcMDNSServiceQuery* pMatchingServiceQuery = 0; - - stcMDNSServiceQuery* pServiceQuery = (p_pPrevServiceQuery ? p_pPrevServiceQuery->m_pNext : m_pServiceQueries); - while (pServiceQuery) { - if (p_ServiceTypeDomain == pServiceQuery->m_ServiceTypeDomain) { - pMatchingServiceQuery = pServiceQuery; - break; - } - pServiceQuery = pServiceQuery->m_pNext; - } - return pMatchingServiceQuery; -} - - -/* - * HOSTNAME - */ - -/* - * MDNSResponder::_setHostname - */ -bool MDNSResponder::_setHostname(const char* p_pcHostname) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _allocHostname (%s)\n"), p_pcHostname);); - - bool bResult = false; - - _releaseHostname(); - - size_t stLength = 0; - if ((p_pcHostname) && - (MDNS_DOMAIN_LABEL_MAXLENGTH >= (stLength = strlen(p_pcHostname)))) { // char max size for a single label - // Copy in hostname characters as lowercase - if ((bResult = (0 != (m_pcHostname = new char[stLength + 1])))) { -#ifdef MDNS_FORCE_LOWERCASE_HOSTNAME - size_t i = 0; - for (; i= strlen(p_pcName))) && - (p_pcService) && - (MDNS_SERVICE_NAME_LENGTH >= strlen(p_pcService)) && - (p_pcProtocol) && - (MDNS_SERVICE_PROTOCOL_LENGTH >= strlen(p_pcProtocol)) && - (p_u16Port) && - (0 != (pService = new stcMDNSService)) && - (pService->setName(p_pcName ?: m_pcHostname)) && - (pService->setService(p_pcService)) && - (pService->setProtocol(p_pcProtocol))) { - - pService->m_bAutoName = (0 == p_pcName); - pService->m_u16Port = p_u16Port; - - // Add to list (or start list) - pService->m_pNext = m_pServices; - m_pServices = pService; - } - return pService; -} - -/* - * MDNSResponder::_releaseService - */ -bool MDNSResponder::_releaseService(MDNSResponder::stcMDNSService* p_pService) { - - bool bResult = false; - - if (p_pService) { - stcMDNSService* pPred = m_pServices; - while ((pPred) && - (pPred->m_pNext != p_pService)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pService->m_pNext; - delete p_pService; - bResult = true; - } - else { // No predecesor - if (m_pServices == p_pService) { - m_pServices = p_pService->m_pNext; - delete p_pService; - bResult = true; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.println("[MDNSResponder] _releaseService: INVALID service!");); - } - } - } - return bResult; -} - -/* - * MDNSResponder::_releaseServices - */ -bool MDNSResponder::_releaseServices(void) { - - stcMDNSService* pService = m_pServices; - while (pService) { - _releaseService(pService); - pService = m_pServices; - } - return true; -} - -/* - * MDNSResponder::_findService - */ -MDNSResponder::stcMDNSService* MDNSResponder::_findService(const char* p_pcName, - const char* p_pcService, - const char* p_pcProtocol) { - - stcMDNSService* pService = m_pServices; - while (pService) { - if ((0 == strcmp(pService->m_pcName, p_pcName)) && - (0 == strcmp(pService->m_pcService, p_pcService)) && - (0 == strcmp(pService->m_pcProtocol, p_pcProtocol))) { - - break; - } - pService = pService->m_pNext; - } - return pService; -} - -/* - * MDNSResponder::_findService - */ -MDNSResponder::stcMDNSService* MDNSResponder::_findService(const MDNSResponder::hMDNSService p_hService) { - - stcMDNSService* pService = m_pServices; - while (pService) { - if (p_hService == (hMDNSService)pService) { - break; - } - pService = pService->m_pNext; - } - return pService; -} - - -/* - * SERVICE TXT - */ - -/* - * MDNSResponder::_allocServiceTxt - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_allocServiceTxt(MDNSResponder::stcMDNSService* p_pService, - const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp) { - - stcMDNSServiceTxt* pTxt = 0; - - if ((p_pService) && - (p_pcKey) && - (MDNS_SERVICE_TXT_MAXLENGTH > (p_pService->m_Txts.length() + - 1 + // Length byte - (p_pcKey ? strlen(p_pcKey) : 0) + - 1 + // '=' - (p_pcValue ? strlen(p_pcValue) : 0)))) { - - pTxt = new stcMDNSServiceTxt; - if (pTxt) { - size_t stLength = (p_pcKey ? strlen(p_pcKey) : 0); - pTxt->m_pcKey = new char[stLength + 1]; - if (pTxt->m_pcKey) { - strncpy(pTxt->m_pcKey, p_pcKey, stLength); pTxt->m_pcKey[stLength] = 0; - } - - if (p_pcValue) { - stLength = (p_pcValue ? strlen(p_pcValue) : 0); - pTxt->m_pcValue = new char[stLength + 1]; - if (pTxt->m_pcValue) { - strncpy(pTxt->m_pcValue, p_pcValue, stLength); pTxt->m_pcValue[stLength] = 0; - } - } - pTxt->m_bTemp = p_bTemp; - - // Add to list (or start list) - p_pService->m_Txts.add(pTxt); - } - } - return pTxt; -} - -/* - * MDNSResponder::_releaseServiceTxt - */ -bool MDNSResponder::_releaseServiceTxt(MDNSResponder::stcMDNSService* p_pService, - MDNSResponder::stcMDNSServiceTxt* p_pTxt) { - - return ((p_pService) && - (p_pTxt) && - (p_pService->m_Txts.remove(p_pTxt))); -} - -/* - * MDNSResponder::_updateServiceTxt - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_updateServiceTxt(MDNSResponder::stcMDNSService* p_pService, - MDNSResponder::stcMDNSServiceTxt* p_pTxt, - const char* p_pcValue, - bool p_bTemp) { - - if ((p_pService) && - (p_pTxt) && - (MDNS_SERVICE_TXT_MAXLENGTH > (p_pService->m_Txts.length() - - (p_pTxt->m_pcValue ? strlen(p_pTxt->m_pcValue) : 0) + - (p_pcValue ? strlen(p_pcValue) : 0)))) { - p_pTxt->update(p_pcValue); - p_pTxt->m_bTemp = p_bTemp; - } - return p_pTxt; -} - -/* - * MDNSResponder::_findServiceTxt - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_findServiceTxt(MDNSResponder::stcMDNSService* p_pService, - const char* p_pcKey) { - - return (p_pService ? p_pService->m_Txts.find(p_pcKey) : 0); -} - -/* - * MDNSResponder::_findServiceTxt - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_findServiceTxt(MDNSResponder::stcMDNSService* p_pService, - const hMDNSTxt p_hTxt) { - - return (((p_pService) && (p_hTxt)) ? p_pService->m_Txts.find((stcMDNSServiceTxt*)p_hTxt) : 0); -} - -/* - * MDNSResponder::_addServiceTxt - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_addServiceTxt(MDNSResponder::stcMDNSService* p_pService, - const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp) { - stcMDNSServiceTxt* pResult = 0; - - if ((p_pService) && - (p_pcKey) && - (strlen(p_pcKey))) { - - stcMDNSServiceTxt* pTxt = p_pService->m_Txts.find(p_pcKey); - if (pTxt) { - pResult = _updateServiceTxt(p_pService, pTxt, p_pcValue, p_bTemp); - } - else { - pResult = _allocServiceTxt(p_pService, p_pcKey, p_pcValue, p_bTemp); - } - } - return pResult; -} - -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_answerKeyValue(const hMDNSServiceQuery p_hServiceQuery, - const uint32_t p_u32AnswerIndex) { - stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); - stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); - // Fill m_pcTxts (if not already done) - return (pSQAnswer) ? pSQAnswer->m_Txts.m_pTxts : 0; -} - -/* - * MDNSResponder::_collectServiceTxts - */ -bool MDNSResponder::_collectServiceTxts(MDNSResponder::stcMDNSService& p_rService) { - - // Call Dynamic service callbacks - if (m_fnServiceTxtCallback) { - m_fnServiceTxtCallback((hMDNSService)&p_rService); - } - if (p_rService.m_fnTxtCallback) { - p_rService.m_fnTxtCallback((hMDNSService)&p_rService); - } - return true; -} - -/* - * MDNSResponder::_releaseTempServiceTxts - */ -bool MDNSResponder::_releaseTempServiceTxts(MDNSResponder::stcMDNSService& p_rService) { - - return (p_rService.m_Txts.removeTempTxts()); -} - - -/* - * MISC - */ - -#ifdef DEBUG_ESP_MDNS_RESPONDER - /* - * MDNSResponder::_printRRDomain - */ - bool MDNSResponder::_printRRDomain(const MDNSResponder::stcMDNS_RRDomain& p_RRDomain) const { - - //DEBUG_OUTPUT.printf_P(PSTR("Domain: ")); - - const char* pCursor = p_RRDomain.m_acName; - uint8_t u8Length = *pCursor++; - if (u8Length) { - while (u8Length) { - for (uint8_t u=0; um_IPAddress.toString().c_str()); - break; -#endif - case DNS_RRTYPE_PTR: - DEBUG_OUTPUT.printf_P(PSTR("PTR ")); - _printRRDomain(((const stcMDNS_RRAnswerPTR*)&p_RRAnswer)->m_PTRDomain); - break; - case DNS_RRTYPE_TXT: { - size_t stTxtLength = ((const stcMDNS_RRAnswerTXT*)&p_RRAnswer)->m_Txts.c_strLength(); - char* pTxts = new char[stTxtLength]; - if (pTxts) { - ((/*const c_str()!!*/stcMDNS_RRAnswerTXT*)&p_RRAnswer)->m_Txts.c_str(pTxts); - DEBUG_OUTPUT.printf_P(PSTR("TXT(%u) %s"), stTxtLength, pTxts); - delete[] pTxts; - } - break; - } -#ifdef MDNS_IP6_SUPPORT - case DNS_RRTYPE_AAAA: - DEBUG_OUTPUT.printf_P(PSTR("AAAA IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); - break; -#endif - case DNS_RRTYPE_SRV: - DEBUG_OUTPUT.printf_P(PSTR("SRV Port:%u "), ((const stcMDNS_RRAnswerSRV*)&p_RRAnswer)->m_u16Port); - _printRRDomain(((const stcMDNS_RRAnswerSRV*)&p_RRAnswer)->m_SRVDomain); - break; - default: - DEBUG_OUTPUT.printf_P(PSTR("generic ")); - break; - } - DEBUG_OUTPUT.printf_P(PSTR("\n")); - - return true; - } -#endif - -} // namespace MDNSImplementation - -} // namespace esp8266 - - - - +/* + LEAmDNS_Helpers.cpp + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +#include "lwip/igmp.h" + +#include "LEAmDNS_lwIPdefs.h" +#include "LEAmDNS_Priv.h" + + +namespace +{ + +/* + strrstr (static) + + Backwards search for p_pcPattern in p_pcString + Based on: https://stackoverflow.com/a/1634398/2778898 + +*/ +const char* strrstr(const char*__restrict p_pcString, const char*__restrict p_pcPattern) +{ + + const char* pcResult = 0; + + size_t stStringLength = (p_pcString ? strlen(p_pcString) : 0); + size_t stPatternLength = (p_pcPattern ? strlen(p_pcPattern) : 0); + + if ((stStringLength) && + (stPatternLength) && + (stPatternLength <= stStringLength)) + { + // Pattern is shorter or has the same length tham the string + + for (const char* s = (p_pcString + stStringLength - stPatternLength); s >= p_pcString; --s) + { + if (0 == strncmp(s, p_pcPattern, stPatternLength)) + { + pcResult = s; + break; + } + } + } + return pcResult; +} + + +} // anonymous + + + + + +namespace esp8266 +{ + +/* + LEAmDNS +*/ +namespace MDNSImplementation +{ + +/** + HELPERS +*/ + +/* + MDNSResponder::indexDomain (static) + + Updates the given domain 'p_rpcHostname' by appending a delimiter and an index number. + + If the given domain already hasa numeric index (after the given delimiter), this index + incremented. If not, the delimiter and index '2' is added. + + If 'p_rpcHostname' is empty (==0), the given default name 'p_pcDefaultHostname' is used, + if no default is given, 'esp8266' is used. + +*/ +/*static*/ bool MDNSResponder::indexDomain(char*& p_rpcDomain, + const char* p_pcDivider /*= "-"*/, + const char* p_pcDefaultDomain /*= 0*/) +{ + + bool bResult = false; + + // Ensure a divider exists; use '-' as default + const char* pcDivider = (p_pcDivider ? : "-"); + + if (p_rpcDomain) + { + const char* pFoundDivider = strrstr(p_rpcDomain, pcDivider); + if (pFoundDivider) // maybe already extended + { + char* pEnd = 0; + unsigned long ulIndex = strtoul((pFoundDivider + strlen(pcDivider)), &pEnd, 10); + if ((ulIndex) && + ((pEnd - p_rpcDomain) == (ptrdiff_t)strlen(p_rpcDomain)) && + (!*pEnd)) // Valid (old) index found + { + + char acIndexBuffer[16]; + sprintf(acIndexBuffer, "%lu", (++ulIndex)); + size_t stLength = ((pFoundDivider - p_rpcDomain + strlen(pcDivider)) + strlen(acIndexBuffer) + 1); + char* pNewHostname = new char[stLength]; + if (pNewHostname) + { + memcpy(pNewHostname, p_rpcDomain, (pFoundDivider - p_rpcDomain + strlen(pcDivider))); + pNewHostname[pFoundDivider - p_rpcDomain + strlen(pcDivider)] = 0; + strcat(pNewHostname, acIndexBuffer); + + delete[] p_rpcDomain; + p_rpcDomain = pNewHostname; + + bResult = true; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); + } + } + else + { + pFoundDivider = 0; // Flag the need to (base) extend the hostname + } + } + + if (!pFoundDivider) // not yet extended (or failed to increment extension) -> start indexing + { + size_t stLength = strlen(p_rpcDomain) + (strlen(pcDivider) + 1 + 1); // Name + Divider + '2' + '\0' + char* pNewHostname = new char[stLength]; + if (pNewHostname) + { + sprintf(pNewHostname, "%s%s2", p_rpcDomain, pcDivider); + + delete[] p_rpcDomain; + p_rpcDomain = pNewHostname; + + bResult = true; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); + } + } + } + else + { + // No given host domain, use base or default + const char* cpcDefaultName = (p_pcDefaultDomain ? : "esp8266"); + + size_t stLength = strlen(cpcDefaultName) + 1; // '\0' + p_rpcDomain = new char[stLength]; + if (p_rpcDomain) + { + strncpy(p_rpcDomain, cpcDefaultName, stLength); + bResult = true; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.println(F("[MDNSResponder] indexDomain: FAILED to alloc new hostname!"));); + } + } + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] indexDomain: %s\n"), p_rpcDomain);); + return bResult; +} + + +/* + UDP CONTEXT +*/ + +bool MDNSResponder::_callProcess(void) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf("[MDNSResponder] _callProcess (%lu, triggered by: %s)\n", millis(), IPAddress(m_pUDPContext->getRemoteAddress()).toString().c_str());); + + return _process(false); +} + +/* + MDNSResponder::_allocUDPContext + + (Re-)Creates the one-and-only UDP context for the MDNS responder. + The context is added to the 'multicast'-group and listens to the MDNS port (5353). + The travel-distance for multicast messages is set to 1 (local, via MDNS_MULTICAST_TTL). + Messages are received via the MDNSResponder '_update' function. CAUTION: This function + is called from the WiFi stack side of the ESP stack system. + +*/ +bool MDNSResponder::_allocUDPContext(void) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.println("[MDNSResponder] _allocUDPContext");); + + bool bResult = false; + + _releaseUDPContext(); + +#ifdef MDNS_IP4_SUPPORT + ip_addr_t multicast_addr = DNS_MQUERY_IPV4_GROUP_INIT; +#endif +#ifdef MDNS_IP6_SUPPORT + //TODO: set multicast address (lwip_joingroup() is IPv4 only at the time of writing) + multicast_addr.addr = DNS_MQUERY_IPV6_GROUP_INIT; +#endif + if (ERR_OK == igmp_joingroup(ip_2_ip4(&m_netif->ip_addr), ip_2_ip4(&multicast_addr))) + { + m_pUDPContext = new UdpContext; + m_pUDPContext->ref(); + + if (m_pUDPContext->listen(IP4_ADDR_ANY, DNS_MQUERY_PORT)) + { + m_pUDPContext->setMulticastTTL(MDNS_MULTICAST_TTL); + m_pUDPContext->onRx(std::bind(&MDNSResponder::_callProcess, this)); + + bResult = m_pUDPContext->connect(&multicast_addr, DNS_MQUERY_PORT); + } + } + return bResult; +} + +/* + MDNSResponder::_releaseUDPContext +*/ +bool MDNSResponder::_releaseUDPContext(void) +{ + + if (m_pUDPContext) + { + m_pUDPContext->unref(); + m_pUDPContext = 0; + } + return true; +} + + +/* + SERVICE QUERY +*/ + +/* + MDNSResponder::_allocServiceQuery +*/ +MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_allocServiceQuery(void) +{ + + stcMDNSServiceQuery* pServiceQuery = new stcMDNSServiceQuery; + if (pServiceQuery) + { + // Link to query list + pServiceQuery->m_pNext = m_pServiceQueries; + m_pServiceQueries = pServiceQuery; + } + return m_pServiceQueries; +} + +/* + MDNSResponder::_removeServiceQuery +*/ +bool MDNSResponder::_removeServiceQuery(MDNSResponder::stcMDNSServiceQuery* p_pServiceQuery) +{ + + bool bResult = false; + + if (p_pServiceQuery) + { + stcMDNSServiceQuery* pPred = m_pServiceQueries; + while ((pPred) && + (pPred->m_pNext != p_pServiceQuery)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pServiceQuery->m_pNext; + delete p_pServiceQuery; + bResult = true; + } + else // No predecesor + { + if (m_pServiceQueries == p_pServiceQuery) + { + m_pServiceQueries = p_pServiceQuery->m_pNext; + delete p_pServiceQuery; + bResult = true; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.println("[MDNSResponder] _releaseServiceQuery: INVALID service query!");); + } + } + } + return bResult; +} + +/* + MDNSResponder::_removeLegacyServiceQuery +*/ +bool MDNSResponder::_removeLegacyServiceQuery(void) +{ + + stcMDNSServiceQuery* pLegacyServiceQuery = _findLegacyServiceQuery(); + return (pLegacyServiceQuery ? _removeServiceQuery(pLegacyServiceQuery) : true); +} + +/* + MDNSResponder::_findServiceQuery + + 'Convert' hMDNSServiceQuery to stcMDNSServiceQuery* (ensure existance) + +*/ +MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findServiceQuery(MDNSResponder::hMDNSServiceQuery p_hServiceQuery) +{ + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + if ((hMDNSServiceQuery)pServiceQuery == p_hServiceQuery) + { + break; + } + pServiceQuery = pServiceQuery->m_pNext; + } + return pServiceQuery; +} + +/* + MDNSResponder::_findLegacyServiceQuery +*/ +MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findLegacyServiceQuery(void) +{ + + stcMDNSServiceQuery* pServiceQuery = m_pServiceQueries; + while (pServiceQuery) + { + if (pServiceQuery->m_bLegacyQuery) + { + break; + } + pServiceQuery = pServiceQuery->m_pNext; + } + return pServiceQuery; +} + +/* + MDNSResponder::_releaseServiceQueries +*/ +bool MDNSResponder::_releaseServiceQueries(void) +{ + while (m_pServiceQueries) + { + stcMDNSServiceQuery* pNext = m_pServiceQueries->m_pNext; + delete m_pServiceQueries; + m_pServiceQueries = pNext; + } + return true; +} + +/* + MDNSResponder::_findNextServiceQueryByServiceType +*/ +MDNSResponder::stcMDNSServiceQuery* MDNSResponder::_findNextServiceQueryByServiceType(const stcMDNS_RRDomain& p_ServiceTypeDomain, + const stcMDNSServiceQuery* p_pPrevServiceQuery) +{ + stcMDNSServiceQuery* pMatchingServiceQuery = 0; + + stcMDNSServiceQuery* pServiceQuery = (p_pPrevServiceQuery ? p_pPrevServiceQuery->m_pNext : m_pServiceQueries); + while (pServiceQuery) + { + if (p_ServiceTypeDomain == pServiceQuery->m_ServiceTypeDomain) + { + pMatchingServiceQuery = pServiceQuery; + break; + } + pServiceQuery = pServiceQuery->m_pNext; + } + return pMatchingServiceQuery; +} + + +/* + HOSTNAME +*/ + +/* + MDNSResponder::_setHostname +*/ +bool MDNSResponder::_setHostname(const char* p_pcHostname) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _allocHostname (%s)\n"), p_pcHostname);); + + bool bResult = false; + + _releaseHostname(); + + size_t stLength = 0; + if ((p_pcHostname) && + (MDNS_DOMAIN_LABEL_MAXLENGTH >= (stLength = strlen(p_pcHostname)))) // char max size for a single label + { + // Copy in hostname characters as lowercase + if ((bResult = (0 != (m_pcHostname = new char[stLength + 1])))) + { +#ifdef MDNS_FORCE_LOWERCASE_HOSTNAME + size_t i = 0; + for (; i < stLength; ++i) + { + m_pcHostname[i] = (isupper(p_pcHostname[i]) ? tolower(p_pcHostname[i]) : p_pcHostname[i]); + } + m_pcHostname[i] = 0; +#else + strncpy(m_pcHostname, p_pcHostname, (stLength + 1)); +#endif + } + } + return bResult; +} + +/* + MDNSResponder::_releaseHostname +*/ +bool MDNSResponder::_releaseHostname(void) +{ + + if (m_pcHostname) + { + delete[] m_pcHostname; + m_pcHostname = 0; + } + return true; +} + + +/* + SERVICE +*/ + +/* + MDNSResponder::_allocService +*/ +MDNSResponder::stcMDNSService* MDNSResponder::_allocService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol, + uint16_t p_u16Port) +{ + + stcMDNSService* pService = 0; + if (((!p_pcName) || + (MDNS_DOMAIN_LABEL_MAXLENGTH >= strlen(p_pcName))) && + (p_pcService) && + (MDNS_SERVICE_NAME_LENGTH >= strlen(p_pcService)) && + (p_pcProtocol) && + (MDNS_SERVICE_PROTOCOL_LENGTH >= strlen(p_pcProtocol)) && + (p_u16Port) && + (0 != (pService = new stcMDNSService)) && + (pService->setName(p_pcName ? : m_pcHostname)) && + (pService->setService(p_pcService)) && + (pService->setProtocol(p_pcProtocol))) + { + + pService->m_bAutoName = (0 == p_pcName); + pService->m_u16Port = p_u16Port; + + // Add to list (or start list) + pService->m_pNext = m_pServices; + m_pServices = pService; + } + return pService; +} + +/* + MDNSResponder::_releaseService +*/ +bool MDNSResponder::_releaseService(MDNSResponder::stcMDNSService* p_pService) +{ + + bool bResult = false; + + if (p_pService) + { + stcMDNSService* pPred = m_pServices; + while ((pPred) && + (pPred->m_pNext != p_pService)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pService->m_pNext; + delete p_pService; + bResult = true; + } + else // No predecesor + { + if (m_pServices == p_pService) + { + m_pServices = p_pService->m_pNext; + delete p_pService; + bResult = true; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.println("[MDNSResponder] _releaseService: INVALID service!");); + } + } + } + return bResult; +} + +/* + MDNSResponder::_releaseServices +*/ +bool MDNSResponder::_releaseServices(void) +{ + + stcMDNSService* pService = m_pServices; + while (pService) + { + _releaseService(pService); + pService = m_pServices; + } + return true; +} + +/* + MDNSResponder::_findService +*/ +MDNSResponder::stcMDNSService* MDNSResponder::_findService(const char* p_pcName, + const char* p_pcService, + const char* p_pcProtocol) +{ + + stcMDNSService* pService = m_pServices; + while (pService) + { + if ((0 == strcmp(pService->m_pcName, p_pcName)) && + (0 == strcmp(pService->m_pcService, p_pcService)) && + (0 == strcmp(pService->m_pcProtocol, p_pcProtocol))) + { + + break; + } + pService = pService->m_pNext; + } + return pService; +} + +/* + MDNSResponder::_findService +*/ +MDNSResponder::stcMDNSService* MDNSResponder::_findService(const MDNSResponder::hMDNSService p_hService) +{ + + stcMDNSService* pService = m_pServices; + while (pService) + { + if (p_hService == (hMDNSService)pService) + { + break; + } + pService = pService->m_pNext; + } + return pService; +} + + +/* + SERVICE TXT +*/ + +/* + MDNSResponder::_allocServiceTxt +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_allocServiceTxt(MDNSResponder::stcMDNSService* p_pService, + const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp) +{ + + stcMDNSServiceTxt* pTxt = 0; + + if ((p_pService) && + (p_pcKey) && + (MDNS_SERVICE_TXT_MAXLENGTH > (p_pService->m_Txts.length() + + 1 + // Length byte + (p_pcKey ? strlen(p_pcKey) : 0) + + 1 + // '=' + (p_pcValue ? strlen(p_pcValue) : 0)))) + { + + pTxt = new stcMDNSServiceTxt; + if (pTxt) + { + size_t stLength = (p_pcKey ? strlen(p_pcKey) : 0); + pTxt->m_pcKey = new char[stLength + 1]; + if (pTxt->m_pcKey) + { + strncpy(pTxt->m_pcKey, p_pcKey, stLength); pTxt->m_pcKey[stLength] = 0; + } + + if (p_pcValue) + { + stLength = (p_pcValue ? strlen(p_pcValue) : 0); + pTxt->m_pcValue = new char[stLength + 1]; + if (pTxt->m_pcValue) + { + strncpy(pTxt->m_pcValue, p_pcValue, stLength); pTxt->m_pcValue[stLength] = 0; + } + } + pTxt->m_bTemp = p_bTemp; + + // Add to list (or start list) + p_pService->m_Txts.add(pTxt); + } + } + return pTxt; +} + +/* + MDNSResponder::_releaseServiceTxt +*/ +bool MDNSResponder::_releaseServiceTxt(MDNSResponder::stcMDNSService* p_pService, + MDNSResponder::stcMDNSServiceTxt* p_pTxt) +{ + + return ((p_pService) && + (p_pTxt) && + (p_pService->m_Txts.remove(p_pTxt))); +} + +/* + MDNSResponder::_updateServiceTxt +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_updateServiceTxt(MDNSResponder::stcMDNSService* p_pService, + MDNSResponder::stcMDNSServiceTxt* p_pTxt, + const char* p_pcValue, + bool p_bTemp) +{ + + if ((p_pService) && + (p_pTxt) && + (MDNS_SERVICE_TXT_MAXLENGTH > (p_pService->m_Txts.length() - + (p_pTxt->m_pcValue ? strlen(p_pTxt->m_pcValue) : 0) + + (p_pcValue ? strlen(p_pcValue) : 0)))) + { + p_pTxt->update(p_pcValue); + p_pTxt->m_bTemp = p_bTemp; + } + return p_pTxt; +} + +/* + MDNSResponder::_findServiceTxt +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_findServiceTxt(MDNSResponder::stcMDNSService* p_pService, + const char* p_pcKey) +{ + + return (p_pService ? p_pService->m_Txts.find(p_pcKey) : 0); +} + +/* + MDNSResponder::_findServiceTxt +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_findServiceTxt(MDNSResponder::stcMDNSService* p_pService, + const hMDNSTxt p_hTxt) +{ + + return (((p_pService) && (p_hTxt)) ? p_pService->m_Txts.find((stcMDNSServiceTxt*)p_hTxt) : 0); +} + +/* + MDNSResponder::_addServiceTxt +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_addServiceTxt(MDNSResponder::stcMDNSService* p_pService, + const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp) +{ + stcMDNSServiceTxt* pResult = 0; + + if ((p_pService) && + (p_pcKey) && + (strlen(p_pcKey))) + { + + stcMDNSServiceTxt* pTxt = p_pService->m_Txts.find(p_pcKey); + if (pTxt) + { + pResult = _updateServiceTxt(p_pService, pTxt, p_pcValue, p_bTemp); + } + else + { + pResult = _allocServiceTxt(p_pService, p_pcKey, p_pcValue, p_bTemp); + } + } + return pResult; +} + +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::_answerKeyValue(const hMDNSServiceQuery p_hServiceQuery, + const uint32_t p_u32AnswerIndex) +{ + stcMDNSServiceQuery* pServiceQuery = _findServiceQuery(p_hServiceQuery); + stcMDNSServiceQuery::stcAnswer* pSQAnswer = (pServiceQuery ? pServiceQuery->answerAtIndex(p_u32AnswerIndex) : 0); + // Fill m_pcTxts (if not already done) + return (pSQAnswer) ? pSQAnswer->m_Txts.m_pTxts : 0; +} + +/* + MDNSResponder::_collectServiceTxts +*/ +bool MDNSResponder::_collectServiceTxts(MDNSResponder::stcMDNSService& p_rService) +{ + + // Call Dynamic service callbacks + if (m_fnServiceTxtCallback) + { + m_fnServiceTxtCallback((hMDNSService)&p_rService); + } + if (p_rService.m_fnTxtCallback) + { + p_rService.m_fnTxtCallback((hMDNSService)&p_rService); + } + return true; +} + +/* + MDNSResponder::_releaseTempServiceTxts +*/ +bool MDNSResponder::_releaseTempServiceTxts(MDNSResponder::stcMDNSService& p_rService) +{ + + return (p_rService.m_Txts.removeTempTxts()); +} + + +/* + MISC +*/ + +#ifdef DEBUG_ESP_MDNS_RESPONDER +/* + MDNSResponder::_printRRDomain +*/ +bool MDNSResponder::_printRRDomain(const MDNSResponder::stcMDNS_RRDomain& p_RRDomain) const +{ + + //DEBUG_OUTPUT.printf_P(PSTR("Domain: ")); + + const char* pCursor = p_RRDomain.m_acName; + uint8_t u8Length = *pCursor++; + if (u8Length) + { + while (u8Length) + { + for (uint8_t u = 0; u < u8Length; ++u) + { + DEBUG_OUTPUT.printf_P(PSTR("%c"), *(pCursor++)); + } + u8Length = *pCursor++; + if (u8Length) + { + DEBUG_OUTPUT.printf_P(PSTR(".")); + } + } + } + else // empty domain + { + DEBUG_OUTPUT.printf_P(PSTR("-empty-")); + } + //DEBUG_OUTPUT.printf_P(PSTR("\n")); + + return true; +} + +/* + MDNSResponder::_printRRAnswer +*/ +bool MDNSResponder::_printRRAnswer(const MDNSResponder::stcMDNS_RRAnswer& p_RRAnswer) const +{ + + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] RRAnswer: ")); + _printRRDomain(p_RRAnswer.m_Header.m_Domain); + DEBUG_OUTPUT.printf_P(PSTR(" Type:0x%04X Class:0x%04X TTL:%u, "), p_RRAnswer.m_Header.m_Attributes.m_u16Type, p_RRAnswer.m_Header.m_Attributes.m_u16Class, p_RRAnswer.m_u32TTL); + switch (p_RRAnswer.m_Header.m_Attributes.m_u16Type & (~0x8000)) // Topmost bit might carry 'cache flush' flag + { +#ifdef MDNS_IP4_SUPPORT + case DNS_RRTYPE_A: + DEBUG_OUTPUT.printf_P(PSTR("A IP:%s"), ((const stcMDNS_RRAnswerA*)&p_RRAnswer)->m_IPAddress.toString().c_str()); + break; +#endif + case DNS_RRTYPE_PTR: + DEBUG_OUTPUT.printf_P(PSTR("PTR ")); + _printRRDomain(((const stcMDNS_RRAnswerPTR*)&p_RRAnswer)->m_PTRDomain); + break; + case DNS_RRTYPE_TXT: + { + size_t stTxtLength = ((const stcMDNS_RRAnswerTXT*)&p_RRAnswer)->m_Txts.c_strLength(); + char* pTxts = new char[stTxtLength]; + if (pTxts) + { + ((/*const c_str()!!*/stcMDNS_RRAnswerTXT*)&p_RRAnswer)->m_Txts.c_str(pTxts); + DEBUG_OUTPUT.printf_P(PSTR("TXT(%u) %s"), stTxtLength, pTxts); + delete[] pTxts; + } + break; + } +#ifdef MDNS_IP6_SUPPORT + case DNS_RRTYPE_AAAA: + DEBUG_OUTPUT.printf_P(PSTR("AAAA IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); + break; +#endif + case DNS_RRTYPE_SRV: + DEBUG_OUTPUT.printf_P(PSTR("SRV Port:%u "), ((const stcMDNS_RRAnswerSRV*)&p_RRAnswer)->m_u16Port); + _printRRDomain(((const stcMDNS_RRAnswerSRV*)&p_RRAnswer)->m_SRVDomain); + break; + default: + DEBUG_OUTPUT.printf_P(PSTR("generic ")); + break; + } + DEBUG_OUTPUT.printf_P(PSTR("\n")); + + return true; +} +#endif + +} // namespace MDNSImplementation + +} // namespace esp8266 + + + + diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Priv.h b/libraries/ESP8266mDNS/src/LEAmDNS_Priv.h index 6371478a9..cc56b133a 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Priv.h +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Priv.h @@ -1,180 +1,182 @@ -/* - * LEAmDNS_Priv.h - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#ifndef MDNS_PRIV_H -#define MDNS_PRIV_H - -namespace esp8266 { - -/* - * LEAmDNS - */ - -namespace MDNSImplementation { - -// Enable class debug functions -#define ESP_8266_MDNS_INCLUDE -//#define DEBUG_ESP_MDNS_RESPONDER - -#if !defined(DEBUG_ESP_MDNS_RESPONDER) && defined(DEBUG_ESP_MDNS) -#define DEBUG_ESP_MDNS_RESPONDER -#endif - -#ifndef LWIP_OPEN_SRC - #define LWIP_OPEN_SRC -#endif - -// -// If ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE is defined, the mDNS responder ignores a successful probing -// This allows to drive the responder in a environment, where 'update()' isn't called in the loop -//#define ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE - -// Enable/disable debug trace macros -#ifdef DEBUG_ESP_MDNS_RESPONDER -#define DEBUG_ESP_MDNS_INFO -#define DEBUG_ESP_MDNS_ERR -#define DEBUG_ESP_MDNS_TX -#define DEBUG_ESP_MDNS_RX -#endif - -#ifdef DEBUG_ESP_MDNS_RESPONDER - #ifdef DEBUG_ESP_MDNS_INFO - #define DEBUG_EX_INFO(A) A - #else - #define DEBUG_EX_INFO(A) do { (void)0; } while (0) - #endif - #ifdef DEBUG_ESP_MDNS_ERR - #define DEBUG_EX_ERR(A) A - #else - #define DEBUG_EX_ERR(A) do { (void)0; } while (0) - #endif - #ifdef DEBUG_ESP_MDNS_TX - #define DEBUG_EX_TX(A) A - #else - #define DEBUG_EX_TX(A) do { (void)0; } while (0) - #endif - #ifdef DEBUG_ESP_MDNS_RX - #define DEBUG_EX_RX(A) A - #else - #define DEBUG_EX_RX(A) do { (void)0; } while (0) - #endif - - #ifdef DEBUG_ESP_PORT - #define DEBUG_OUTPUT DEBUG_ESP_PORT - #else - #define DEBUG_OUTPUT Serial - #endif -#else - #define DEBUG_EX_INFO(A) do { (void)0; } while (0) - #define DEBUG_EX_ERR(A) do { (void)0; } while (0) - #define DEBUG_EX_TX(A) do { (void)0; } while (0) - #define DEBUG_EX_RX(A) do { (void)0; } while (0) -#endif - - -/* Replaced by 'lwip/prot/dns.h' definitions -#ifdef MDNS_IP4_SUPPORT - #define MDNS_MULTICAST_ADDR_IP4 (IPAddress(224, 0, 0, 251)) // ip_addr_t v4group = DNS_MQUERY_IPV4_GROUP_INIT -#endif -#ifdef MDNS_IP6_SUPPORT - #define MDNS_MULTICAST_ADDR_IP6 (IPAddress("FF02::FB")) // ip_addr_t v6group = DNS_MQUERY_IPV6_GROUP_INIT -#endif*/ -//#define MDNS_MULTICAST_PORT 5353 - -/* - * This is NOT the TTL (Time-To-Live) for MDNS records, but the - * subnet level distance MDNS records should travel. - * 1 sets the subnet distance to 'local', which is default for MDNS. - * (Btw.: 255 would set it to 'as far as possible' -> internet) - * - * However, RFC 3171 seems to force 255 instead - */ -#define MDNS_MULTICAST_TTL 255/*1*/ - -/* - * This is the MDNS record TTL - * Host level records are set to 2min (120s) - * service level records are set to 75min (4500s) - */ -#define MDNS_HOST_TTL 120 -#define MDNS_SERVICE_TTL 4500 - -/* - * Compressed labels are flaged by the two topmost bits of the length byte being set - */ -#define MDNS_DOMAIN_COMPRESS_MARK 0xC0 -/* - * Avoid endless recursion because of malformed compressed labels - */ -#define MDNS_DOMAIN_MAX_REDIRCTION 6 - -/* - * Default service priority and weight in SRV answers - */ -#define MDNS_SRV_PRIORITY 0 -#define MDNS_SRV_WEIGHT 0 - -/* - * Delay between and number of probes for host and service domains - * Delay between and number of announces for host and service domains - * Delay between and number of service queries; the delay is multiplied by the resent number in '_checkServiceQueryCache' - */ -#define MDNS_PROBE_DELAY 250 -#define MDNS_PROBE_COUNT 3 -#define MDNS_ANNOUNCE_DELAY 1000 -#define MDNS_ANNOUNCE_COUNT 8 -#define MDNS_DYNAMIC_QUERY_RESEND_COUNT 5 -#define MDNS_DYNAMIC_QUERY_RESEND_DELAY 5000 - - -/* - * Force host domain to use only lowercase letters - */ -//#define MDNS_FORCE_LOWERCASE_HOSTNAME - -/* - * Enable/disable the usage of the F() macro in debug trace printf calls. - * There needs to be an PGM comptible printf function to use this. - * - * USE_PGM_PRINTF and F - */ -#define USE_PGM_PRINTF - -#ifdef USE_PGM_PRINTF -#else - #ifdef F - #undef F - #endif - #define F(A) A -#endif - -} // namespace MDNSImplementation - -} // namespace esp8266 - -// Include the main header, so the submodlues only need to include this header -#include "LEAmDNS.h" - - -#endif // MDNS_PRIV_H +/* + LEAmDNS_Priv.h + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +#ifndef MDNS_PRIV_H +#define MDNS_PRIV_H + +namespace esp8266 +{ + +/* + LEAmDNS +*/ + +namespace MDNSImplementation +{ + +// Enable class debug functions +#define ESP_8266_MDNS_INCLUDE +//#define DEBUG_ESP_MDNS_RESPONDER + +#if !defined(DEBUG_ESP_MDNS_RESPONDER) && defined(DEBUG_ESP_MDNS) +#define DEBUG_ESP_MDNS_RESPONDER +#endif + +#ifndef LWIP_OPEN_SRC +#define LWIP_OPEN_SRC +#endif + +// +// If ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE is defined, the mDNS responder ignores a successful probing +// This allows to drive the responder in a environment, where 'update()' isn't called in the loop +//#define ENABLE_ESP_MDNS_RESPONDER_PASSIV_MODE + +// Enable/disable debug trace macros +#ifdef DEBUG_ESP_MDNS_RESPONDER +#define DEBUG_ESP_MDNS_INFO +#define DEBUG_ESP_MDNS_ERR +#define DEBUG_ESP_MDNS_TX +#define DEBUG_ESP_MDNS_RX +#endif + +#ifdef DEBUG_ESP_MDNS_RESPONDER +#ifdef DEBUG_ESP_MDNS_INFO +#define DEBUG_EX_INFO(A) A +#else +#define DEBUG_EX_INFO(A) do { (void)0; } while (0) +#endif +#ifdef DEBUG_ESP_MDNS_ERR +#define DEBUG_EX_ERR(A) A +#else +#define DEBUG_EX_ERR(A) do { (void)0; } while (0) +#endif +#ifdef DEBUG_ESP_MDNS_TX +#define DEBUG_EX_TX(A) A +#else +#define DEBUG_EX_TX(A) do { (void)0; } while (0) +#endif +#ifdef DEBUG_ESP_MDNS_RX +#define DEBUG_EX_RX(A) A +#else +#define DEBUG_EX_RX(A) do { (void)0; } while (0) +#endif + +#ifdef DEBUG_ESP_PORT +#define DEBUG_OUTPUT DEBUG_ESP_PORT +#else +#define DEBUG_OUTPUT Serial +#endif +#else +#define DEBUG_EX_INFO(A) do { (void)0; } while (0) +#define DEBUG_EX_ERR(A) do { (void)0; } while (0) +#define DEBUG_EX_TX(A) do { (void)0; } while (0) +#define DEBUG_EX_RX(A) do { (void)0; } while (0) +#endif + + +/* Replaced by 'lwip/prot/dns.h' definitions + #ifdef MDNS_IP4_SUPPORT + #define MDNS_MULTICAST_ADDR_IP4 (IPAddress(224, 0, 0, 251)) // ip_addr_t v4group = DNS_MQUERY_IPV4_GROUP_INIT + #endif + #ifdef MDNS_IP6_SUPPORT + #define MDNS_MULTICAST_ADDR_IP6 (IPAddress("FF02::FB")) // ip_addr_t v6group = DNS_MQUERY_IPV6_GROUP_INIT + #endif*/ +//#define MDNS_MULTICAST_PORT 5353 + +/* + This is NOT the TTL (Time-To-Live) for MDNS records, but the + subnet level distance MDNS records should travel. + 1 sets the subnet distance to 'local', which is default for MDNS. + (Btw.: 255 would set it to 'as far as possible' -> internet) + + However, RFC 3171 seems to force 255 instead +*/ +#define MDNS_MULTICAST_TTL 255/*1*/ + +/* + This is the MDNS record TTL + Host level records are set to 2min (120s) + service level records are set to 75min (4500s) +*/ +#define MDNS_HOST_TTL 120 +#define MDNS_SERVICE_TTL 4500 + +/* + Compressed labels are flaged by the two topmost bits of the length byte being set +*/ +#define MDNS_DOMAIN_COMPRESS_MARK 0xC0 +/* + Avoid endless recursion because of malformed compressed labels +*/ +#define MDNS_DOMAIN_MAX_REDIRCTION 6 + +/* + Default service priority and weight in SRV answers +*/ +#define MDNS_SRV_PRIORITY 0 +#define MDNS_SRV_WEIGHT 0 + +/* + Delay between and number of probes for host and service domains + Delay between and number of announces for host and service domains + Delay between and number of service queries; the delay is multiplied by the resent number in '_checkServiceQueryCache' +*/ +#define MDNS_PROBE_DELAY 250 +#define MDNS_PROBE_COUNT 3 +#define MDNS_ANNOUNCE_DELAY 1000 +#define MDNS_ANNOUNCE_COUNT 8 +#define MDNS_DYNAMIC_QUERY_RESEND_COUNT 5 +#define MDNS_DYNAMIC_QUERY_RESEND_DELAY 5000 + + +/* + Force host domain to use only lowercase letters +*/ +//#define MDNS_FORCE_LOWERCASE_HOSTNAME + +/* + Enable/disable the usage of the F() macro in debug trace printf calls. + There needs to be an PGM comptible printf function to use this. + + USE_PGM_PRINTF and F +*/ +#define USE_PGM_PRINTF + +#ifdef USE_PGM_PRINTF +#else +#ifdef F +#undef F +#endif +#define F(A) A +#endif + +} // namespace MDNSImplementation + +} // namespace esp8266 + +// Include the main header, so the submodlues only need to include this header +#include "LEAmDNS.h" + + +#endif // MDNS_PRIV_H diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp index e54beb50d..ce475de3b 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Structs.cpp @@ -1,2218 +1,2476 @@ -/* - * LEAmDNS_Structs.cpp - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#include "LEAmDNS_Priv.h" -#include "LEAmDNS_lwIPdefs.h" - -namespace esp8266 { - -/* - * LEAmDNS - */ -namespace MDNSImplementation { - -/** - * STRUCTS - */ - -/** - * MDNSResponder::stcMDNSServiceTxt - * - * One MDNS TXT item. - * m_pcValue may be '\0'. - * Objects can be chained together (list, m_pNext). - * A 'm_bTemp' flag differentiates between static and dynamic items. - * Output as byte array 'c#=1' is supported. - */ - -/* - * MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt constructor - */ -MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt(const char* p_pcKey /*= 0*/, - const char* p_pcValue /*= 0*/, - bool p_bTemp /*= false*/) -: m_pNext(0), - m_pcKey(0), - m_pcValue(0), - m_bTemp(p_bTemp) { - - setKey(p_pcKey); - setValue(p_pcValue); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt copy-constructor - */ -MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt(const MDNSResponder::stcMDNSServiceTxt& p_Other) -: m_pNext(0), - m_pcKey(0), - m_pcValue(0), - m_bTemp(false) { - - operator=(p_Other); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::~stcMDNSServiceTxt destructor - */ -MDNSResponder::stcMDNSServiceTxt::~stcMDNSServiceTxt(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::operator= - */ -MDNSResponder::stcMDNSServiceTxt& MDNSResponder::stcMDNSServiceTxt::operator=(const MDNSResponder::stcMDNSServiceTxt& p_Other) { - - if (&p_Other != this) { - clear(); - set(p_Other.m_pcKey, p_Other.m_pcValue, p_Other.m_bTemp); - } - return *this; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::clear - */ -bool MDNSResponder::stcMDNSServiceTxt::clear(void) { - - releaseKey(); - releaseValue(); - return true; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::allocKey - */ -char* MDNSResponder::stcMDNSServiceTxt::allocKey(size_t p_stLength) { - - releaseKey(); - if (p_stLength) { - m_pcKey = new char[p_stLength + 1]; - } - return m_pcKey; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::setKey - */ -bool MDNSResponder::stcMDNSServiceTxt::setKey(const char* p_pcKey, - size_t p_stLength) { - - bool bResult = false; - - releaseKey(); - if (p_stLength) { - if (allocKey(p_stLength)) { - strncpy(m_pcKey, p_pcKey, p_stLength); - m_pcKey[p_stLength] = 0; - bResult = true; - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::setKey - */ -bool MDNSResponder::stcMDNSServiceTxt::setKey(const char* p_pcKey) { - - return setKey(p_pcKey, (p_pcKey ? strlen(p_pcKey) : 0)); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::releaseKey - */ -bool MDNSResponder::stcMDNSServiceTxt::releaseKey(void) { - - if (m_pcKey) { - delete[] m_pcKey; - m_pcKey = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::allocValue - */ -char* MDNSResponder::stcMDNSServiceTxt::allocValue(size_t p_stLength) { - - releaseValue(); - if (p_stLength) { - m_pcValue = new char[p_stLength + 1]; - } - return m_pcValue; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::setValue - */ -bool MDNSResponder::stcMDNSServiceTxt::setValue(const char* p_pcValue, - size_t p_stLength) { - - bool bResult = false; - - releaseValue(); - if (p_stLength) { - if (allocValue(p_stLength)) { - strncpy(m_pcValue, p_pcValue, p_stLength); - m_pcValue[p_stLength] = 0; - bResult = true; - } - } - else { // No value -> also OK - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::setValue - */ -bool MDNSResponder::stcMDNSServiceTxt::setValue(const char* p_pcValue) { - - return setValue(p_pcValue, (p_pcValue ? strlen(p_pcValue) : 0)); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::releaseValue - */ -bool MDNSResponder::stcMDNSServiceTxt::releaseValue(void) { - - if (m_pcValue) { - delete[] m_pcValue; - m_pcValue = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceTxt::set - */ -bool MDNSResponder::stcMDNSServiceTxt::set(const char* p_pcKey, - const char* p_pcValue, - bool p_bTemp /*= false*/) { - - m_bTemp = p_bTemp; - return ((setKey(p_pcKey)) && - (setValue(p_pcValue))); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::update - */ -bool MDNSResponder::stcMDNSServiceTxt::update(const char* p_pcValue) { - - return setValue(p_pcValue); -} - -/* - * MDNSResponder::stcMDNSServiceTxt::length - * - * length of eg. 'c#=1' without any closing '\0' - */ -size_t MDNSResponder::stcMDNSServiceTxt::length(void) const { - - size_t stLength = 0; - if (m_pcKey) { - stLength += strlen(m_pcKey); // Key - stLength += 1; // '=' - stLength += (m_pcValue ? strlen(m_pcValue) : 0); // Value - } - return stLength; -} - - -/** - * MDNSResponder::stcMDNSServiceTxts - * - * A list of zero or more MDNS TXT items. - * Dynamic TXT items can be removed by 'removeTempTxts'. - * A TXT item can be looke up by its 'key' member. - * Export as ';'-separated byte array is supported. - * Export as 'length byte coded' byte array is supported. - * Comparision ((all A TXT items in B and equal) AND (all B TXT items in A and equal)) is supported. - * - */ - -/* - * MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts contructor - */ -MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts(void) -: m_pTxts(0) { - -} - -/* - * MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts copy-constructor - */ -MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts(const stcMDNSServiceTxts& p_Other) -: m_pTxts(0) { - - operator=(p_Other); -} - -/* - * MDNSResponder::stcMDNSServiceTxts::~stcMDNSServiceTxts destructor - */ -MDNSResponder::stcMDNSServiceTxts::~stcMDNSServiceTxts(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSServiceTxts::operator= - */ -MDNSResponder::stcMDNSServiceTxts& MDNSResponder::stcMDNSServiceTxts::operator=(const stcMDNSServiceTxts& p_Other) { - - if (this != &p_Other) { - clear(); - - for (stcMDNSServiceTxt* pOtherTxt=p_Other.m_pTxts; pOtherTxt; pOtherTxt=pOtherTxt->m_pNext) { - add(new stcMDNSServiceTxt(*pOtherTxt)); - } - } - return *this; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::clear - */ -bool MDNSResponder::stcMDNSServiceTxts::clear(void) { - - while (m_pTxts) { - stcMDNSServiceTxt* pNext = m_pTxts->m_pNext; - delete m_pTxts; - m_pTxts = pNext; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::add - */ -bool MDNSResponder::stcMDNSServiceTxts::add(MDNSResponder::stcMDNSServiceTxt* p_pTxt) { - - bool bResult = false; - - if (p_pTxt) { - p_pTxt->m_pNext = m_pTxts; - m_pTxts = p_pTxt; - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::remove - */ -bool MDNSResponder::stcMDNSServiceTxts::remove(stcMDNSServiceTxt* p_pTxt) { - - bool bResult = false; - - if (p_pTxt) { - stcMDNSServiceTxt* pPred = m_pTxts; - while ((pPred) && - (pPred->m_pNext != p_pTxt)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pTxt->m_pNext; - delete p_pTxt; - bResult = true; - } - else if (m_pTxts == p_pTxt) { // No predecesor, but first item - m_pTxts = p_pTxt->m_pNext; - delete p_pTxt; - bResult = true; - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::removeTempTxts - */ -bool MDNSResponder::stcMDNSServiceTxts::removeTempTxts(void) { - - bool bResult = true; - - stcMDNSServiceTxt* pTxt = m_pTxts; - while ((bResult) && - (pTxt)) { - stcMDNSServiceTxt* pNext = pTxt->m_pNext; - if (pTxt->m_bTemp) { - bResult = remove(pTxt); - } - pTxt = pNext; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::find - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const char* p_pcKey) { - - stcMDNSServiceTxt* pResult = 0; - - for (stcMDNSServiceTxt* pTxt=m_pTxts; pTxt; pTxt=pTxt->m_pNext) { - if ((p_pcKey) && - (0 == strcmp(pTxt->m_pcKey, p_pcKey))) { - pResult = pTxt; - break; - } - } - return pResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::find - */ -const MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const char* p_pcKey) const { - - const stcMDNSServiceTxt* pResult = 0; - - for (const stcMDNSServiceTxt* pTxt=m_pTxts; pTxt; pTxt=pTxt->m_pNext) { - if ((p_pcKey) && - (0 == strcmp(pTxt->m_pcKey, p_pcKey))) { - - pResult = pTxt; - break; - } - } - return pResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::find - */ -MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const stcMDNSServiceTxt* p_pTxt) { - - stcMDNSServiceTxt* pResult = 0; - - for (stcMDNSServiceTxt* pTxt=m_pTxts; pTxt; pTxt=pTxt->m_pNext) { - if (p_pTxt == pTxt) { - pResult = pTxt; - break; - } - } - return pResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::length - */ -uint16_t MDNSResponder::stcMDNSServiceTxts::length(void) const { - - uint16_t u16Length = 0; - - stcMDNSServiceTxt* pTxt = m_pTxts; - while (pTxt) { - u16Length += 1; // Length byte - u16Length += pTxt->length(); // Text - pTxt = pTxt->m_pNext; - } - return u16Length; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::c_strLength - * - * (incl. closing '\0'). Length bytes place is used for delimiting ';' and closing '\0' - */ -size_t MDNSResponder::stcMDNSServiceTxts::c_strLength(void) const { - - return length(); -} - -/* - * MDNSResponder::stcMDNSServiceTxts::c_str - */ -bool MDNSResponder::stcMDNSServiceTxts::c_str(char* p_pcBuffer) { - - bool bResult = false; - - if (p_pcBuffer) { - bResult = true; - - *p_pcBuffer = 0; - for (stcMDNSServiceTxt* pTxt=m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) { - size_t stLength; - if ((bResult = (0 != (stLength = (pTxt->m_pcKey ? strlen(pTxt->m_pcKey) : 0))))) { - if (pTxt != m_pTxts) { - *p_pcBuffer++ = ';'; - } - strncpy(p_pcBuffer, pTxt->m_pcKey, stLength); p_pcBuffer[stLength] = 0; - p_pcBuffer += stLength; - *p_pcBuffer++ = '='; - if ((stLength = (pTxt->m_pcValue ? strlen(pTxt->m_pcValue) : 0))) { - strncpy(p_pcBuffer, pTxt->m_pcValue, stLength); p_pcBuffer[stLength] = 0; - p_pcBuffer += stLength; - } - } - } - *p_pcBuffer++ = 0; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::bufferLength - * - * (incl. closing '\0'). - */ -size_t MDNSResponder::stcMDNSServiceTxts::bufferLength(void) const { - - return (length() + 1); -} - -/* - * MDNSResponder::stcMDNSServiceTxts::toBuffer - */ -bool MDNSResponder::stcMDNSServiceTxts::buffer(char* p_pcBuffer) { - - bool bResult = false; - - if (p_pcBuffer) { - bResult = true; - - *p_pcBuffer = 0; - for (stcMDNSServiceTxt* pTxt=m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) { - *(unsigned char*)p_pcBuffer++ = pTxt->length(); - size_t stLength; - if ((bResult = (0 != (stLength = (pTxt->m_pcKey ? strlen(pTxt->m_pcKey) : 0))))) { - memcpy(p_pcBuffer, pTxt->m_pcKey, stLength); - p_pcBuffer += stLength; - *p_pcBuffer++ = '='; - if ((stLength = (pTxt->m_pcValue ? strlen(pTxt->m_pcValue) : 0))) { - memcpy(p_pcBuffer, pTxt->m_pcValue, stLength); - p_pcBuffer += stLength; - } - } - } - *p_pcBuffer++ = 0; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::compare - */ -bool MDNSResponder::stcMDNSServiceTxts::compare(const MDNSResponder::stcMDNSServiceTxts& p_Other) const { - - bool bResult = false; - - if ((bResult = (length() == p_Other.length()))) { - // Compare A->B - for (const stcMDNSServiceTxt* pTxt=m_pTxts; ((bResult) && (pTxt)); pTxt=pTxt->m_pNext) { - const stcMDNSServiceTxt* pOtherTxt = p_Other.find(pTxt->m_pcKey); - bResult = ((pOtherTxt) && - (pTxt->m_pcValue) && - (pOtherTxt->m_pcValue) && - (strlen(pTxt->m_pcValue) == strlen(pOtherTxt->m_pcValue)) && - (0 == strcmp(pTxt->m_pcValue, pOtherTxt->m_pcValue))); - } - // Compare B->A - for (const stcMDNSServiceTxt* pOtherTxt=p_Other.m_pTxts; ((bResult) && (pOtherTxt)); pOtherTxt=pOtherTxt->m_pNext) { - const stcMDNSServiceTxt* pTxt = find(pOtherTxt->m_pcKey); - bResult = ((pTxt) && - (pOtherTxt->m_pcValue) && - (pTxt->m_pcValue) && - (strlen(pOtherTxt->m_pcValue) == strlen(pTxt->m_pcValue)) && - (0 == strcmp(pOtherTxt->m_pcValue, pTxt->m_pcValue))); - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceTxts::operator== - */ -bool MDNSResponder::stcMDNSServiceTxts::operator==(const stcMDNSServiceTxts& p_Other) const { - - return compare(p_Other); -} - -/* - * MDNSResponder::stcMDNSServiceTxts::operator!= - */ -bool MDNSResponder::stcMDNSServiceTxts::operator!=(const stcMDNSServiceTxts& p_Other) const { - - return !compare(p_Other); -} - - -/** - * MDNSResponder::stcMDNS_MsgHeader - * - * A MDNS message haeder. - * - */ - -/* - * MDNSResponder::stcMDNS_MsgHeader::stcMDNS_MsgHeader - */ -MDNSResponder::stcMDNS_MsgHeader::stcMDNS_MsgHeader(uint16_t p_u16ID /*= 0*/, - bool p_bQR /*= false*/, - unsigned char p_ucOpcode /*= 0*/, - bool p_bAA /*= false*/, - bool p_bTC /*= false*/, - bool p_bRD /*= false*/, - bool p_bRA /*= false*/, - unsigned char p_ucRCode /*= 0*/, - uint16_t p_u16QDCount /*= 0*/, - uint16_t p_u16ANCount /*= 0*/, - uint16_t p_u16NSCount /*= 0*/, - uint16_t p_u16ARCount /*= 0*/) -: m_u16ID(p_u16ID), - m_1bQR(p_bQR), m_4bOpcode(p_ucOpcode), m_1bAA(p_bAA), m_1bTC(p_bTC), m_1bRD(p_bRD), - m_1bRA(p_bRA), m_3bZ(0), m_4bRCode(p_ucRCode), - m_u16QDCount(p_u16QDCount), - m_u16ANCount(p_u16ANCount), - m_u16NSCount(p_u16NSCount), - m_u16ARCount(p_u16ARCount) { - -} - - -/** - * MDNSResponder::stcMDNS_RRDomain - * - * A MDNS domain object. - * The labels of the domain are stored (DNS-like encoded) in 'm_acName': - * [length byte]varlength label[length byte]varlength label[0] - * 'm_u16NameLength' stores the used length of 'm_acName'. - * Dynamic label addition is supported. - * Comparison is supported. - * Export as byte array 'esp8266.local' is supported. - * - */ - -/* - * MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain constructor - */ -MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain(void) -: m_u16NameLength(0) { - - clear(); -} - -/* - * MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain copy-constructor - */ -MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain(const stcMDNS_RRDomain& p_Other) -: m_u16NameLength(0) { - - operator=(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRDomain::operator = - */ -MDNSResponder::stcMDNS_RRDomain& MDNSResponder::stcMDNS_RRDomain::operator=(const stcMDNS_RRDomain& p_Other) { - - if (&p_Other != this) { - memcpy(m_acName, p_Other.m_acName, sizeof(m_acName)); - m_u16NameLength = p_Other.m_u16NameLength; - } - return *this; -} - -/* - * MDNSResponder::stcMDNS_RRDomain::clear - */ -bool MDNSResponder::stcMDNS_RRDomain::clear(void) { - - memset(m_acName, 0, sizeof(m_acName)); - m_u16NameLength = 0; - return true; -} - -/* - * MDNSResponder::stcMDNS_RRDomain::addLabel - */ -bool MDNSResponder::stcMDNS_RRDomain::addLabel(const char* p_pcLabel, - bool p_bPrependUnderline /*= false*/) { - - bool bResult = false; - - size_t stLength = (p_pcLabel - ? (strlen(p_pcLabel) + (p_bPrependUnderline ? 1 : 0)) - : 0); - if ((MDNS_DOMAIN_LABEL_MAXLENGTH >= stLength) && - (MDNS_DOMAIN_MAXLENGTH >= (m_u16NameLength + (1 + stLength)))) { - // Length byte - m_acName[m_u16NameLength] = (unsigned char)stLength; // Might be 0! - ++m_u16NameLength; - // Label - if (stLength) { - if (p_bPrependUnderline) { - m_acName[m_u16NameLength++] = '_'; - --stLength; - } - strncpy(&(m_acName[m_u16NameLength]), p_pcLabel, stLength); m_acName[m_u16NameLength + stLength] = 0; - m_u16NameLength += stLength; - } - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNS_RRDomain::compare - */ -bool MDNSResponder::stcMDNS_RRDomain::compare(const stcMDNS_RRDomain& p_Other) const { - - bool bResult = false; - - if (m_u16NameLength == p_Other.m_u16NameLength) { - const char* pT = m_acName; - const char* pO = p_Other.m_acName; - while ((pT) && - (pO) && - (*((unsigned char*)pT) == *((unsigned char*)pO)) && // Same length AND - (0 == strncasecmp((pT + 1), (pO + 1), *((unsigned char*)pT)))) { // Same content - if (*((unsigned char*)pT)) { // Not 0 - pT += (1 + *((unsigned char*)pT)); // Shift by length byte and lenght - pO += (1 + *((unsigned char*)pO)); - } - else { // Is 0 -> Successfully reached the end - bResult = true; - break; - } - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNS_RRDomain::operator == - */ -bool MDNSResponder::stcMDNS_RRDomain::operator==(const stcMDNS_RRDomain& p_Other) const { - - return compare(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRDomain::operator != - */ -bool MDNSResponder::stcMDNS_RRDomain::operator!=(const stcMDNS_RRDomain& p_Other) const { - - return !compare(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRDomain::operator > - */ -bool MDNSResponder::stcMDNS_RRDomain::operator>(const stcMDNS_RRDomain& p_Other) const { - - // TODO: Check, if this is a good idea... - return !compare(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRDomain::c_strLength - */ -size_t MDNSResponder::stcMDNS_RRDomain::c_strLength(void) const { - - size_t stLength = 0; - - unsigned char* pucLabelLength = (unsigned char*)m_acName; - while (*pucLabelLength) { - stLength += (*pucLabelLength + 1 /* +1 for '.' or '\0'*/); - pucLabelLength += (*pucLabelLength + 1); - } - return stLength; -} - -/* - * MDNSResponder::stcMDNS_RRDomain::c_str - */ -bool MDNSResponder::stcMDNS_RRDomain::c_str(char* p_pcBuffer) { - - bool bResult = false; - - if (p_pcBuffer) { - *p_pcBuffer = 0; - unsigned char* pucLabelLength = (unsigned char*)m_acName; - while (*pucLabelLength) { - memcpy(p_pcBuffer, (const char*)(pucLabelLength + 1), *pucLabelLength); - p_pcBuffer += *pucLabelLength; - pucLabelLength += (*pucLabelLength + 1); - *p_pcBuffer++ = (*pucLabelLength ? '.' : '\0'); - } - bResult = true; - } - return bResult; -} - - -/** - * MDNSResponder::stcMDNS_RRAttributes - * - * A MDNS attributes object. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes constructor - */ -MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes(uint16_t p_u16Type /*= 0*/, - uint16_t p_u16Class /*= 1 DNS_RRCLASS_IN Internet*/) -: m_u16Type(p_u16Type), - m_u16Class(p_u16Class) { - -} - -/* - * MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes copy-constructor - */ -MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes(const MDNSResponder::stcMDNS_RRAttributes& p_Other) { - - operator=(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRAttributes::operator = - */ -MDNSResponder::stcMDNS_RRAttributes& MDNSResponder::stcMDNS_RRAttributes::operator=(const MDNSResponder::stcMDNS_RRAttributes& p_Other) { - - if (&p_Other != this) { - m_u16Type = p_Other.m_u16Type; - m_u16Class = p_Other.m_u16Class; - } - return *this; -} - - -/** - * MDNSResponder::stcMDNS_RRHeader - * - * A MDNS record header (domain and attributes) object. - * - */ - -/* - * MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader constructor - */ -MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader(void) { - -} - -/* - * MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader copy-constructor - */ -MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader(const stcMDNS_RRHeader& p_Other) { - - operator=(p_Other); -} - -/* - * MDNSResponder::stcMDNS_RRHeader::operator = - */ -MDNSResponder::stcMDNS_RRHeader& MDNSResponder::stcMDNS_RRHeader::operator=(const MDNSResponder::stcMDNS_RRHeader& p_Other) { - - if (&p_Other != this) { - m_Domain = p_Other.m_Domain; - m_Attributes = p_Other.m_Attributes; - } - return *this; -} - -/* - * MDNSResponder::stcMDNS_RRHeader::clear - */ -bool MDNSResponder::stcMDNS_RRHeader::clear(void) { - - m_Domain.clear(); - return true; -} - - -/** - * MDNSResponder::stcMDNS_RRQuestion - * - * A MDNS question record object (header + question flags) - * - */ - -/* - * MDNSResponder::stcMDNS_RRQuestion::stcMDNS_RRQuestion constructor - */ -MDNSResponder::stcMDNS_RRQuestion::stcMDNS_RRQuestion(void) -: m_pNext(0), - m_bUnicast(false) { - -} - - -/** - * MDNSResponder::stcMDNS_RRAnswer - * - * A MDNS answer record object (header + answer content). - * This is a 'virtual' base class for all other MDNS answer classes. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAnswer::stcMDNS_RRAnswer constructor - */ -MDNSResponder::stcMDNS_RRAnswer::stcMDNS_RRAnswer(enuAnswerType p_AnswerType, - const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) -: m_pNext(0), - m_AnswerType(p_AnswerType), - m_Header(p_Header), - m_u32TTL(p_u32TTL) { - - // Extract 'cache flush'-bit - m_bCacheFlush = (m_Header.m_Attributes.m_u16Class & 0x8000); - m_Header.m_Attributes.m_u16Class &= (~0x8000); -} - -/* - * MDNSResponder::stcMDNS_RRAnswer::~stcMDNS_RRAnswer destructor - */ -MDNSResponder::stcMDNS_RRAnswer::~stcMDNS_RRAnswer(void) { - -} - -/* - * MDNSResponder::stcMDNS_RRAnswer::answerType - */ -MDNSResponder::enuAnswerType MDNSResponder::stcMDNS_RRAnswer::answerType(void) const { - - return m_AnswerType; -} - -/* - * MDNSResponder::stcMDNS_RRAnswer::clear - */ -bool MDNSResponder::stcMDNS_RRAnswer::clear(void) { - - m_pNext = 0; - m_Header.clear(); - return true; -} - - -/** - * MDNSResponder::stcMDNS_RRAnswerA - * - * A MDNS A answer object. - * Extends the base class by an IP4 address member. - * - */ - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA constructor - */ - MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA(const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) - : stcMDNS_RRAnswer(AnswerType_A, p_Header, p_u32TTL), - m_IPAddress(0, 0, 0, 0) { - - } - - /* - * MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA destructor - */ - MDNSResponder::stcMDNS_RRAnswerA::~stcMDNS_RRAnswerA(void) { - - clear(); - } - - /* - * MDNSResponder::stcMDNS_RRAnswerA::clear - */ - bool MDNSResponder::stcMDNS_RRAnswerA::clear(void) { - - m_IPAddress = IPAddress(0, 0, 0, 0); - return true; - } -#endif - - -/** - * MDNSResponder::stcMDNS_RRAnswerPTR - * - * A MDNS PTR answer object. - * Extends the base class by a MDNS domain member. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAnswerPTR::stcMDNS_RRAnswerPTR constructor - */ -MDNSResponder::stcMDNS_RRAnswerPTR::stcMDNS_RRAnswerPTR(const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) -: stcMDNS_RRAnswer(AnswerType_PTR, p_Header, p_u32TTL) { - -} - -/* - * MDNSResponder::stcMDNS_RRAnswerPTR::~stcMDNS_RRAnswerPTR destructor - */ -MDNSResponder::stcMDNS_RRAnswerPTR::~stcMDNS_RRAnswerPTR(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNS_RRAnswerPTR::clear - */ -bool MDNSResponder::stcMDNS_RRAnswerPTR::clear(void) { - - m_PTRDomain.clear(); - return true; -} - - -/** - * MDNSResponder::stcMDNS_RRAnswerTXT - * - * A MDNS TXT answer object. - * Extends the base class by a MDNS TXT items list member. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAnswerTXT::stcMDNS_RRAnswerTXT constructor - */ -MDNSResponder::stcMDNS_RRAnswerTXT::stcMDNS_RRAnswerTXT(const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) -: stcMDNS_RRAnswer(AnswerType_TXT, p_Header, p_u32TTL) { - -} - -/* - * MDNSResponder::stcMDNS_RRAnswerTXT::~stcMDNS_RRAnswerTXT destructor - */ -MDNSResponder::stcMDNS_RRAnswerTXT::~stcMDNS_RRAnswerTXT(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNS_RRAnswerTXT::clear - */ -bool MDNSResponder::stcMDNS_RRAnswerTXT::clear(void) { - - m_Txts.clear(); - return true; -} - - -/** - * MDNSResponder::stcMDNS_RRAnswerAAAA - * - * A MDNS AAAA answer object. - * (Should) extend the base class by an IP6 address member. - * - */ - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::stcMDNS_RRAnswerAAAA::stcMDNS_RRAnswerAAAA constructor - */ - MDNSResponder::stcMDNS_RRAnswerAAAA::stcMDNS_RRAnswerAAAA(const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) - : stcMDNS_RRAnswer(AnswerType_AAAA, p_Header, p_u32TTL) { - - } - - /* - * MDNSResponder::stcMDNS_RRAnswerAAAA::~stcMDNS_RRAnswerAAAA destructor - */ - MDNSResponder::stcMDNS_RRAnswerAAAA::~stcMDNS_RRAnswerAAAA(void) { - - clear(); - } - - /* - * MDNSResponder::stcMDNS_RRAnswerAAAA::clear - */ - bool MDNSResponder::stcMDNS_RRAnswerAAAA::clear(void) { - - return true; - } -#endif - - -/** - * MDNSResponder::stcMDNS_RRAnswerSRV - * - * A MDNS SRV answer object. - * Extends the base class by a port member. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAnswerSRV::stcMDNS_RRAnswerSRV constructor - */ -MDNSResponder::stcMDNS_RRAnswerSRV::stcMDNS_RRAnswerSRV(const MDNSResponder::stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) -: stcMDNS_RRAnswer(AnswerType_SRV, p_Header, p_u32TTL), - m_u16Priority(0), - m_u16Weight(0), - m_u16Port(0) { - -} - -/* - * MDNSResponder::stcMDNS_RRAnswerSRV::~stcMDNS_RRAnswerSRV destructor - */ -MDNSResponder::stcMDNS_RRAnswerSRV::~stcMDNS_RRAnswerSRV(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNS_RRAnswerSRV::clear - */ -bool MDNSResponder::stcMDNS_RRAnswerSRV::clear(void) { - - m_u16Priority = 0; - m_u16Weight = 0; - m_u16Port = 0; - m_SRVDomain.clear(); - return true; -} - - -/** - * MDNSResponder::stcMDNS_RRAnswerGeneric - * - * An unknown (generic) MDNS answer object. - * Extends the base class by a RDATA buffer member. - * - */ - -/* - * MDNSResponder::stcMDNS_RRAnswerGeneric::stcMDNS_RRAnswerGeneric constructor - */ -MDNSResponder::stcMDNS_RRAnswerGeneric::stcMDNS_RRAnswerGeneric(const stcMDNS_RRHeader& p_Header, - uint32_t p_u32TTL) -: stcMDNS_RRAnswer(AnswerType_Generic, p_Header, p_u32TTL), - m_u16RDLength(0), - m_pu8RDData(0) { - -} - -/* - * MDNSResponder::stcMDNS_RRAnswerGeneric::~stcMDNS_RRAnswerGeneric destructor - */ -MDNSResponder::stcMDNS_RRAnswerGeneric::~stcMDNS_RRAnswerGeneric(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNS_RRAnswerGeneric::clear - */ -bool MDNSResponder::stcMDNS_RRAnswerGeneric::clear(void) { - - if (m_pu8RDData) { - delete[] m_pu8RDData; - m_pu8RDData = 0; - } - m_u16RDLength = 0; - - return true; -} - - -/** - * MDNSResponder::stcProbeInformation - * - * Probing status information for a host or service domain - * - */ - -/* - * MDNSResponder::stcProbeInformation::stcProbeInformation constructor - */ -MDNSResponder::stcProbeInformation::stcProbeInformation(void) -: m_ProbingStatus(ProbingStatus_WaitingForData), - m_u8SentCount(0), - m_Timeout(esp8266::polledTimeout::oneShotMs::neverExpires), - m_bConflict(false), - m_bTiebreakNeeded(false), - m_fnHostProbeResultCallback(0), - m_fnServiceProbeResultCallback(0) { -} - -/* - * MDNSResponder::stcProbeInformation::clear - */ -bool MDNSResponder::stcProbeInformation::clear(bool p_bClearUserdata /*= false*/) { - - m_ProbingStatus = ProbingStatus_WaitingForData; - m_u8SentCount = 0; - m_Timeout.resetToNeverExpires(); - m_bConflict = false; - m_bTiebreakNeeded = false; - if (p_bClearUserdata) { - m_fnHostProbeResultCallback = 0; - m_fnServiceProbeResultCallback = 0; - } - return true; -} - -/** - * MDNSResponder::stcMDNSService - * - * A MDNS service object (to be announced by the MDNS responder) - * The service instance may be '\0'; in this case the hostname is used - * and the flag m_bAutoName is set. If the hostname changes, all 'auto- - * named' services are renamed also. - * m_u8Replymask is used while preparing a response to a MDNS query. It is - * resetted in '_sendMDNSMessage' afterwards. - */ - -/* - * MDNSResponder::stcMDNSService::stcMDNSService constructor - */ -MDNSResponder::stcMDNSService::stcMDNSService(const char* p_pcName /*= 0*/, - const char* p_pcService /*= 0*/, - const char* p_pcProtocol /*= 0*/) -: m_pNext(0), - m_pcName(0), - m_bAutoName(false), - m_pcService(0), - m_pcProtocol(0), - m_u16Port(0), - m_u8ReplyMask(0), - m_fnTxtCallback(0) { - - setName(p_pcName); - setService(p_pcService); - setProtocol(p_pcProtocol); -} - -/* - * MDNSResponder::stcMDNSService::~stcMDNSService destructor - */ -MDNSResponder::stcMDNSService::~stcMDNSService(void) { - - releaseName(); - releaseService(); - releaseProtocol(); -} - -/* - * MDNSResponder::stcMDNSService::setName - */ -bool MDNSResponder::stcMDNSService::setName(const char* p_pcName) { - - bool bResult = false; - - releaseName(); - size_t stLength = (p_pcName ? strlen(p_pcName) : 0); - if (stLength) { - if ((bResult = (0 != (m_pcName = new char[stLength + 1])))) { - strncpy(m_pcName, p_pcName, stLength); - m_pcName[stLength] = 0; - } - } - else { - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSService::releaseName - */ -bool MDNSResponder::stcMDNSService::releaseName(void) { - - if (m_pcName) { - delete[] m_pcName; - m_pcName = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSService::setService - */ -bool MDNSResponder::stcMDNSService::setService(const char* p_pcService) { - - bool bResult = false; - - releaseService(); - size_t stLength = (p_pcService ? strlen(p_pcService) : 0); - if (stLength) { - if ((bResult = (0 != (m_pcService = new char[stLength + 1])))) { - strncpy(m_pcService, p_pcService, stLength); - m_pcService[stLength] = 0; - } - } - else { - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSService::releaseService - */ -bool MDNSResponder::stcMDNSService::releaseService(void) { - - if (m_pcService) { - delete[] m_pcService; - m_pcService = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSService::setProtocol - */ -bool MDNSResponder::stcMDNSService::setProtocol(const char* p_pcProtocol) { - - bool bResult = false; - - releaseProtocol(); - size_t stLength = (p_pcProtocol ? strlen(p_pcProtocol) : 0); - if (stLength) { - if ((bResult = (0 != (m_pcProtocol = new char[stLength + 1])))) { - strncpy(m_pcProtocol, p_pcProtocol, stLength); - m_pcProtocol[stLength] = 0; - } - } - else { - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSService::releaseProtocol - */ -bool MDNSResponder::stcMDNSService::releaseProtocol(void) { - - if (m_pcProtocol) { - delete[] m_pcProtocol; - m_pcProtocol = 0; - } - return true; -} - - -/** - * MDNSResponder::stcMDNSServiceQuery - * - * A MDNS service query object. - * Service queries may be static or dynamic. - * As the static service query is processed in the blocking function 'queryService', - * only one static service service may exist. The processing of the answers is done - * on the WiFi-stack side of the ESP stack structure (via 'UDPContext.onRx(_update)'). - * - */ - -/** - * MDNSResponder::stcMDNSServiceQuery::stcAnswer - * - * One answer for a service query. - * Every answer must contain - * - a service instance entry (pivot), - * and may contain - * - a host domain, - * - a port - * - an IP4 address - * (- an IP6 address) - * - a MDNS TXTs - * The existance of a component is flaged in 'm_u32ContentFlags'. - * For every answer component a TTL value is maintained. - * Answer objects can be connected to a linked list. - * - * For the host domain, service domain and TXTs components, a char array - * representation can be retrieved (which is created on demand). - * - */ - -/** - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL - * - * The TTL (Time-To-Live) for an specific answer content. - * The 80% and outdated states are calculated based on the current time (millis) - * and the 'set' time (also millis). - * If the answer is scheduled for an update, the corresponding flag should be set. - * - * / - -/ * - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL constructor - * / -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL(uint32_t p_u32TTL / *= 0* /) -: m_bUpdateScheduled(false) { - - set(p_u32TTL * 1000); -} - -/ * - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set - * / -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TTL) { - - m_TTLTimeFlag.restart(p_u32TTL * 1000); - m_bUpdateScheduled = false; - - return true; -} - -/ * - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::has80Percent - * / -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::has80Percent(void) const { - - return ((m_TTLTimeFlag.getTimeout()) && - (!m_bUpdateScheduled) && - (m_TTLTimeFlag.hypotheticalTimeout((m_TTLTimeFlag.getTimeout() * 800) / 1000))); -} - -/ * - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::isOutdated - * / -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::isOutdated(void) const { - - return ((m_TTLTimeFlag.getTimeout()) && - (m_TTLTimeFlag.flagged())); -}*/ - - -/** - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL - * - * The TTL (Time-To-Live) for an specific answer content. - * The 80% and outdated states are calculated based on the current time (millis) - * and the 'set' time (also millis). - * If the answer is scheduled for an update, the corresponding flag should be set. - * - */ - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL constructor - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL(void) -: m_u32TTL(0), - m_TTLTimeout(esp8266::polledTimeout::oneShotMs::neverExpires), - m_timeoutLevel(TIMEOUTLEVEL_UNSET) { - -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TTL) { - - m_u32TTL = p_u32TTL; - if (m_u32TTL) { - m_timeoutLevel = TIMEOUTLEVEL_BASE; // Set to 80% - m_TTLTimeout.reset(timeout()); - } - else { - m_timeoutLevel = TIMEOUTLEVEL_UNSET; // undef - m_TTLTimeout.resetToNeverExpires(); - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged(void) { - - return ((m_u32TTL) && - (TIMEOUTLEVEL_UNSET != m_timeoutLevel) && - (m_TTLTimeout.expired())); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::restart - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::restart(void) { - - bool bResult = true; - - if ((TIMEOUTLEVEL_BASE <= m_timeoutLevel) && // >= 80% AND - (TIMEOUTLEVEL_FINAL > m_timeoutLevel)) { // < 100% - - m_timeoutLevel += TIMEOUTLEVEL_INTERVAL; // increment by 5% - m_TTLTimeout.reset(timeout()); - } - else { - bResult = false; - m_TTLTimeout.resetToNeverExpires(); - m_timeoutLevel = TIMEOUTLEVEL_UNSET; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::prepareDeletion - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::prepareDeletion(void) { - - m_timeoutLevel = TIMEOUTLEVEL_FINAL; - m_TTLTimeout.reset(1 * 1000); // See RFC 6762, 10.1 - - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::finalTimeoutLevel - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::finalTimeoutLevel(void) const { - - return (TIMEOUTLEVEL_FINAL == m_timeoutLevel); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::timeout - */ -unsigned long MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::timeout(void) const { - - uint32_t u32Timeout = esp8266::polledTimeout::oneShotMs::neverExpires; - - if (TIMEOUTLEVEL_BASE == m_timeoutLevel) { // 80% - u32Timeout = (m_u32TTL * 800); // to milliseconds - } - else if ((TIMEOUTLEVEL_BASE < m_timeoutLevel) && // >80% AND - (TIMEOUTLEVEL_FINAL >= m_timeoutLevel)) { // <= 100% - - u32Timeout = (m_u32TTL * 50); - } // else: invalid - return u32Timeout; -} - - -#ifdef MDNS_IP4_SUPPORT -/** - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address - * - */ - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address::stcIP4Address constructor - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address::stcIP4Address(IPAddress p_IPAddress, - uint32_t p_u32TTL /*= 0*/) -: m_pNext(0), - m_IPAddress(p_IPAddress) { - - m_TTL.set(p_u32TTL); -} -#endif - - -/** - * MDNSResponder::stcMDNSServiceQuery::stcAnswer - */ - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcAnswer constructor - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcAnswer(void) -: m_pNext(0), - m_pcServiceDomain(0), - m_pcHostDomain(0), - m_u16Port(0), - m_pcTxts(0), -#ifdef MDNS_IP4_SUPPORT - m_pIP4Addresses(0), -#endif -#ifdef MDNS_IP6_SUPPORT - m_pIP6Addresses(0), -#endif - m_u32ContentFlags(0) { -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::~stcAnswer destructor - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::~stcAnswer(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::clear - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::clear(void) { - - return ((releaseTxts()) && -#ifdef MDNS_IP4_SUPPORT - (releaseIP4Addresses()) && -#endif -#ifdef MDNS_IP6_SUPPORT - (releaseIP6Addresses()) -#endif - (releaseHostDomain()) && - (releaseServiceDomain())); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocServiceDomain - * - * Alloc memory for the char array representation of the service domain. - * - */ -char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocServiceDomain(size_t p_stLength) { - - releaseServiceDomain(); - if (p_stLength) { - m_pcServiceDomain = new char[p_stLength]; - } - return m_pcServiceDomain; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseServiceDomain - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseServiceDomain(void) { - - if (m_pcServiceDomain) { - delete[] m_pcServiceDomain; - m_pcServiceDomain = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocHostDomain - * - * Alloc memory for the char array representation of the host domain. - * - */ -char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocHostDomain(size_t p_stLength) { - - releaseHostDomain(); - if (p_stLength) { - m_pcHostDomain = new char[p_stLength]; - } - return m_pcHostDomain; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseHostDomain - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseHostDomain(void) { - - if (m_pcHostDomain) { - delete[] m_pcHostDomain; - m_pcHostDomain = 0; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocTxts - * - * Alloc memory for the char array representation of the TXT items. - * - */ -char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocTxts(size_t p_stLength) { - - releaseTxts(); - if (p_stLength) { - m_pcTxts = new char[p_stLength]; - } - return m_pcTxts; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseTxts - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseTxts(void) { - - if (m_pcTxts) { - delete[] m_pcTxts; - m_pcTxts = 0; - } - return true; -} - -#ifdef MDNS_IP4_SUPPORT -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP4Addresses - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP4Addresses(void) { - - while (m_pIP4Addresses) { - stcIP4Address* pNext = m_pIP4Addresses->m_pNext; - delete m_pIP4Addresses; - m_pIP4Addresses = pNext; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP4Address - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP4Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* p_pIP4Address) { - - bool bResult = false; - - if (p_pIP4Address) { - p_pIP4Address->m_pNext = m_pIP4Addresses; - m_pIP4Addresses = p_pIP4Address; - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP4Address - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP4Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* p_pIP4Address) { - - bool bResult = false; - - if (p_pIP4Address) { - stcIP4Address* pPred = m_pIP4Addresses; - while ((pPred) && - (pPred->m_pNext != p_pIP4Address)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pIP4Address->m_pNext; - delete p_pIP4Address; - bResult = true; - } - else if (m_pIP4Addresses == p_pIP4Address) { // No predecesor, but first item - m_pIP4Addresses = p_pIP4Address->m_pNext; - delete p_pIP4Address; - bResult = true; - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address (const) - */ -const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address(const IPAddress& p_IPAddress) const { - - return (stcIP4Address*)(((const stcAnswer*)this)->findIP4Address(p_IPAddress)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address(const IPAddress& p_IPAddress) { - - stcIP4Address* pIP4Address = m_pIP4Addresses; - while (pIP4Address) { - if (pIP4Address->m_IPAddress == p_IPAddress) { - break; - } - pIP4Address = pIP4Address->m_pNext; - } - return pIP4Address; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressCount - */ -uint32_t MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressCount(void) const { - - uint32_t u32Count = 0; - - stcIP4Address* pIP4Address = m_pIP4Addresses; - while (pIP4Address) { - ++u32Count; - pIP4Address = pIP4Address->m_pNext; - } - return u32Count; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex(uint32_t p_u32Index) { - - return (stcIP4Address*)(((const stcAnswer*)this)->IP4AddressAtIndex(p_u32Index)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex (const) - */ -const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex(uint32_t p_u32Index) const { - - const stcIP4Address* pIP4Address = 0; - - if (((uint32_t)(-1) != p_u32Index) && - (m_pIP4Addresses)) { - - uint32_t u32Index; - for (pIP4Address=m_pIP4Addresses, u32Index=0; ((pIP4Address) && (u32Indexm_pNext, ++u32Index); - } - return pIP4Address; -} -#endif - -#ifdef MDNS_IP6_SUPPORT -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP6Addresses - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP6Addresses(void) { - - while (m_pIP6Addresses) { - stcIP6Address* pNext = m_pIP6Addresses->m_pNext; - delete m_pIP6Addresses; - m_pIP6Addresses = pNext; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP6Address - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP6Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* p_pIP6Address) { - - bool bResult = false; - - if (p_pIP6Address) { - p_pIP6Address->m_pNext = m_pIP6Addresses; - m_pIP6Addresses = p_pIP6Address; - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP6Address - */ -bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP6Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* p_pIP6Address) { - - bool bResult = false; - - if (p_pIP6Address) { - stcIP6Address* pPred = m_pIP6Addresses; - while ((pPred) && - (pPred->m_pNext != p_pIP6Address)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pIP6Address->m_pNext; - delete p_pIP6Address; - bResult = true; - } - else if (m_pIP6Addresses == p_pIP6Address) { // No predecesor, but first item - m_pIP6Addresses = p_pIP6Address->m_pNext; - delete p_pIP6Address; - bResult = true; - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address(const IP6Address& p_IPAddress) { - - return (stcIP6Address*)(((const stcAnswer*)this)->findIP6Address(p_IPAddress)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address (const) - */ -const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address(const IPAddress& p_IPAddress) const { - - const stcIP6Address* pIP6Address = m_pIP6Addresses; - while (pIP6Address) { - if (p_IP6Address->m_IPAddress == p_IPAddress) { - break; - } - pIP6Address = pIP6Address->m_pNext; - } - return pIP6Address; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressCount - */ -uint32_t MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressCount(void) const { - - uint32_t u32Count = 0; - - stcIP6Address* pIP6Address = m_pIP6Addresses; - while (pIP6Address) { - ++u32Count; - pIP6Address = pIP6Address->m_pNext; - } - return u32Count; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex (const) - */ -const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex(uint32_t p_u32Index) const { - - return (stcIP6Address*)(((const stcAnswer*)this)->IP6AddressAtIndex(p_u32Index)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex(uint32_t p_u32Index) { - - stcIP6Address* pIP6Address = 0; - - if (((uint32_t)(-1) != p_u32Index) && - (m_pIP6Addresses)) { - - uint32_t u32Index; - for (pIP6Address=m_pIP6Addresses, u32Index=0; ((pIP6Address) && (u32Indexm_pNext, ++u32Index); - } - return pIP6Address; -} -#endif - - -/** - * MDNSResponder::stcMDNSServiceQuery - * - * A service query object. - * A static query is flaged via 'm_bLegacyQuery'; while the function 'queryService' - * is waiting for answers, the internal flag 'm_bAwaitingAnswers' is set. When the - * timeout is reached, the flag is removed. These two flags are only used for static - * service queries. - * All answers to the service query are stored in 'm_pAnswers' list. - * Individual answers may be addressed by index (in the list of answers). - * Every time a answer component is added (or changes) in a dynamic service query, - * the callback 'm_fnCallback' is called. - * The answer list may be searched by service and host domain. - * - * Service query object may be connected to a linked list. - */ - -/* - * MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery constructor - */ -MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void) -: m_pNext(0), - m_fnCallback(0), - m_bLegacyQuery(false), - m_u8SentCount(0), - m_ResendTimeout(esp8266::polledTimeout::oneShotMs::neverExpires), - m_bAwaitingAnswers(true), - m_pAnswers(0) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery destructor - */ -MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::clear - */ -bool MDNSResponder::stcMDNSServiceQuery::clear(void) { - - m_fnCallback = 0; - m_bLegacyQuery = false; - m_u8SentCount = 0; - m_ResendTimeout.resetToNeverExpires(); - m_bAwaitingAnswers = true; - while (m_pAnswers) { - stcAnswer* pNext = m_pAnswers->m_pNext; - delete m_pAnswers; - m_pAnswers = pNext; - } - return true; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::answerCount - */ -uint32_t MDNSResponder::stcMDNSServiceQuery::answerCount(void) const { - - uint32_t u32Count = 0; - - stcAnswer* pAnswer = m_pAnswers; - while (pAnswer) { - ++u32Count; - pAnswer = pAnswer->m_pNext; - } - return u32Count; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::answerAtIndex - */ -const MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::answerAtIndex(uint32_t p_u32Index) const { - - const stcAnswer* pAnswer = 0; - - if (((uint32_t)(-1) != p_u32Index) && - (m_pAnswers)) { - - uint32_t u32Index; - for (pAnswer=m_pAnswers, u32Index=0; ((pAnswer) && (u32Indexm_pNext, ++u32Index); - } - return pAnswer; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::answerAtIndex - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::answerAtIndex(uint32_t p_u32Index) { - - return (stcAnswer*)(((const stcMDNSServiceQuery*)this)->answerAtIndex(p_u32Index)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::indexOfAnswer - */ -uint32_t MDNSResponder::stcMDNSServiceQuery::indexOfAnswer(const MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) const { - - uint32_t u32Index = 0; - - for (const stcAnswer* pAnswer=m_pAnswers; pAnswer; pAnswer=pAnswer->m_pNext, ++u32Index) { - if (pAnswer == p_pAnswer) { - return u32Index; - } - } - return ((uint32_t)(-1)); -} - -/* - * MDNSResponder::stcMDNSServiceQuery::addAnswer - */ -bool MDNSResponder::stcMDNSServiceQuery::addAnswer(MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) { - - bool bResult = false; - - if (p_pAnswer) { - p_pAnswer->m_pNext = m_pAnswers; - m_pAnswers = p_pAnswer; - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::removeAnswer - */ -bool MDNSResponder::stcMDNSServiceQuery::removeAnswer(MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) { - - bool bResult = false; - - if (p_pAnswer) { - stcAnswer* pPred = m_pAnswers; - while ((pPred) && - (pPred->m_pNext != p_pAnswer)) { - pPred = pPred->m_pNext; - } - if (pPred) { - pPred->m_pNext = p_pAnswer->m_pNext; - delete p_pAnswer; - bResult = true; - } - else if (m_pAnswers == p_pAnswer) { // No predecesor, but first item - m_pAnswers = p_pAnswer->m_pNext; - delete p_pAnswer; - bResult = true; - } - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::findAnswerForServiceDomain - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::findAnswerForServiceDomain(const MDNSResponder::stcMDNS_RRDomain& p_ServiceDomain) { - - stcAnswer* pAnswer = m_pAnswers; - while (pAnswer) { - if (pAnswer->m_ServiceDomain == p_ServiceDomain) { - break; - } - pAnswer = pAnswer->m_pNext; - } - return pAnswer; -} - -/* - * MDNSResponder::stcMDNSServiceQuery::findAnswerForHostDomain - */ -MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::findAnswerForHostDomain(const MDNSResponder::stcMDNS_RRDomain& p_HostDomain) { - - stcAnswer* pAnswer = m_pAnswers; - while (pAnswer) { - if (pAnswer->m_HostDomain == p_HostDomain) { - break; - } - pAnswer = pAnswer->m_pNext; - } - return pAnswer; -} - - -/** - * MDNSResponder::stcMDNSSendParameter - * - * A 'collection' of properties and flags for one MDNS query or response. - * Mainly managed by the 'Control' functions. - * The current offset in the UPD output buffer is tracked to be able to do - * a simple host or service domain compression. - * - */ - -/** - * MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem - * - * A cached host or service domain, incl. the offset in the UDP output buffer. - * - */ - -/* - * MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem::stcDomainCacheItem constructor - */ -MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem::stcDomainCacheItem(const void* p_pHostnameOrService, - bool p_bAdditionalData, - uint32_t p_u16Offset) -: m_pNext(0), - m_pHostnameOrService(p_pHostnameOrService), - m_bAdditionalData(p_bAdditionalData), - m_u16Offset(p_u16Offset) { - -} - -/** - * MDNSResponder::stcMDNSSendParameter - */ - -/* - * MDNSResponder::stcMDNSSendParameter::stcMDNSSendParameter constructor - */ -MDNSResponder::stcMDNSSendParameter::stcMDNSSendParameter(void) -: m_pQuestions(0), - m_pDomainCacheItems(0) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSSendParameter::~stcMDNSSendParameter destructor - */ -MDNSResponder::stcMDNSSendParameter::~stcMDNSSendParameter(void) { - - clear(); -} - -/* - * MDNSResponder::stcMDNSSendParameter::clear - */ -bool MDNSResponder::stcMDNSSendParameter::clear(void) { - - m_u16ID = 0; - m_u8HostReplyMask = 0; - m_u16Offset = 0; - - m_bLegacyQuery = false; - m_bResponse = false; - m_bAuthorative = false; - m_bUnicast = false; - m_bUnannounce = false; - - m_bCacheFlush = true; - - while (m_pQuestions) { - stcMDNS_RRQuestion* pNext = m_pQuestions->m_pNext; - delete m_pQuestions; - m_pQuestions = pNext; - } - while (m_pDomainCacheItems) { - stcDomainCacheItem* pNext = m_pDomainCacheItems->m_pNext; - delete m_pDomainCacheItems; - m_pDomainCacheItems = pNext; - } - return true; -} - -/* - * MDNSResponder::stcMDNSSendParameter::shiftOffset - */ -bool MDNSResponder::stcMDNSSendParameter::shiftOffset(uint16_t p_u16Shift) { - - m_u16Offset += p_u16Shift; - return true; -} - -/* - * MDNSResponder::stcMDNSSendParameter::addDomainCacheItem - */ -bool MDNSResponder::stcMDNSSendParameter::addDomainCacheItem(const void* p_pHostnameOrService, - bool p_bAdditionalData, - uint16_t p_u16Offset) { - - bool bResult = false; - - stcDomainCacheItem* pNewItem = 0; - if ((p_pHostnameOrService) && - (p_u16Offset) && - ((pNewItem = new stcDomainCacheItem(p_pHostnameOrService, p_bAdditionalData, p_u16Offset)))) { - - pNewItem->m_pNext = m_pDomainCacheItems; - bResult = ((m_pDomainCacheItems = pNewItem)); - } - return bResult; -} - -/* - * MDNSResponder::stcMDNSSendParameter::findCachedDomainOffset - */ -uint16_t MDNSResponder::stcMDNSSendParameter::findCachedDomainOffset(const void* p_pHostnameOrService, - bool p_bAdditionalData) const { - - const stcDomainCacheItem* pCacheItem = m_pDomainCacheItems; - - for (; pCacheItem; pCacheItem=pCacheItem->m_pNext) { - if ((pCacheItem->m_pHostnameOrService == p_pHostnameOrService) && - (pCacheItem->m_bAdditionalData == p_bAdditionalData)) { // Found cache item - break; - } - } - return (pCacheItem ? pCacheItem->m_u16Offset : 0); -} - -} // namespace MDNSImplementation - -} // namespace esp8266 - - - +/* + LEAmDNS_Structs.cpp + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +#include "LEAmDNS_Priv.h" +#include "LEAmDNS_lwIPdefs.h" + +namespace esp8266 +{ + +/* + LEAmDNS +*/ +namespace MDNSImplementation +{ + +/** + STRUCTS +*/ + +/** + MDNSResponder::stcMDNSServiceTxt + + One MDNS TXT item. + m_pcValue may be '\0'. + Objects can be chained together (list, m_pNext). + A 'm_bTemp' flag differentiates between static and dynamic items. + Output as byte array 'c#=1' is supported. +*/ + +/* + MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt constructor +*/ +MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt(const char* p_pcKey /*= 0*/, + const char* p_pcValue /*= 0*/, + bool p_bTemp /*= false*/) + : m_pNext(0), + m_pcKey(0), + m_pcValue(0), + m_bTemp(p_bTemp) +{ + + setKey(p_pcKey); + setValue(p_pcValue); +} + +/* + MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt copy-constructor +*/ +MDNSResponder::stcMDNSServiceTxt::stcMDNSServiceTxt(const MDNSResponder::stcMDNSServiceTxt& p_Other) + : m_pNext(0), + m_pcKey(0), + m_pcValue(0), + m_bTemp(false) +{ + + operator=(p_Other); +} + +/* + MDNSResponder::stcMDNSServiceTxt::~stcMDNSServiceTxt destructor +*/ +MDNSResponder::stcMDNSServiceTxt::~stcMDNSServiceTxt(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSServiceTxt::operator= +*/ +MDNSResponder::stcMDNSServiceTxt& MDNSResponder::stcMDNSServiceTxt::operator=(const MDNSResponder::stcMDNSServiceTxt& p_Other) +{ + + if (&p_Other != this) + { + clear(); + set(p_Other.m_pcKey, p_Other.m_pcValue, p_Other.m_bTemp); + } + return *this; +} + +/* + MDNSResponder::stcMDNSServiceTxt::clear +*/ +bool MDNSResponder::stcMDNSServiceTxt::clear(void) +{ + + releaseKey(); + releaseValue(); + return true; +} + +/* + MDNSResponder::stcMDNSServiceTxt::allocKey +*/ +char* MDNSResponder::stcMDNSServiceTxt::allocKey(size_t p_stLength) +{ + + releaseKey(); + if (p_stLength) + { + m_pcKey = new char[p_stLength + 1]; + } + return m_pcKey; +} + +/* + MDNSResponder::stcMDNSServiceTxt::setKey +*/ +bool MDNSResponder::stcMDNSServiceTxt::setKey(const char* p_pcKey, + size_t p_stLength) +{ + + bool bResult = false; + + releaseKey(); + if (p_stLength) + { + if (allocKey(p_stLength)) + { + strncpy(m_pcKey, p_pcKey, p_stLength); + m_pcKey[p_stLength] = 0; + bResult = true; + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxt::setKey +*/ +bool MDNSResponder::stcMDNSServiceTxt::setKey(const char* p_pcKey) +{ + + return setKey(p_pcKey, (p_pcKey ? strlen(p_pcKey) : 0)); +} + +/* + MDNSResponder::stcMDNSServiceTxt::releaseKey +*/ +bool MDNSResponder::stcMDNSServiceTxt::releaseKey(void) +{ + + if (m_pcKey) + { + delete[] m_pcKey; + m_pcKey = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceTxt::allocValue +*/ +char* MDNSResponder::stcMDNSServiceTxt::allocValue(size_t p_stLength) +{ + + releaseValue(); + if (p_stLength) + { + m_pcValue = new char[p_stLength + 1]; + } + return m_pcValue; +} + +/* + MDNSResponder::stcMDNSServiceTxt::setValue +*/ +bool MDNSResponder::stcMDNSServiceTxt::setValue(const char* p_pcValue, + size_t p_stLength) +{ + + bool bResult = false; + + releaseValue(); + if (p_stLength) + { + if (allocValue(p_stLength)) + { + strncpy(m_pcValue, p_pcValue, p_stLength); + m_pcValue[p_stLength] = 0; + bResult = true; + } + } + else // No value -> also OK + { + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxt::setValue +*/ +bool MDNSResponder::stcMDNSServiceTxt::setValue(const char* p_pcValue) +{ + + return setValue(p_pcValue, (p_pcValue ? strlen(p_pcValue) : 0)); +} + +/* + MDNSResponder::stcMDNSServiceTxt::releaseValue +*/ +bool MDNSResponder::stcMDNSServiceTxt::releaseValue(void) +{ + + if (m_pcValue) + { + delete[] m_pcValue; + m_pcValue = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceTxt::set +*/ +bool MDNSResponder::stcMDNSServiceTxt::set(const char* p_pcKey, + const char* p_pcValue, + bool p_bTemp /*= false*/) +{ + + m_bTemp = p_bTemp; + return ((setKey(p_pcKey)) && + (setValue(p_pcValue))); +} + +/* + MDNSResponder::stcMDNSServiceTxt::update +*/ +bool MDNSResponder::stcMDNSServiceTxt::update(const char* p_pcValue) +{ + + return setValue(p_pcValue); +} + +/* + MDNSResponder::stcMDNSServiceTxt::length + + length of eg. 'c#=1' without any closing '\0' +*/ +size_t MDNSResponder::stcMDNSServiceTxt::length(void) const +{ + + size_t stLength = 0; + if (m_pcKey) + { + stLength += strlen(m_pcKey); // Key + stLength += 1; // '=' + stLength += (m_pcValue ? strlen(m_pcValue) : 0); // Value + } + return stLength; +} + + +/** + MDNSResponder::stcMDNSServiceTxts + + A list of zero or more MDNS TXT items. + Dynamic TXT items can be removed by 'removeTempTxts'. + A TXT item can be looke up by its 'key' member. + Export as ';'-separated byte array is supported. + Export as 'length byte coded' byte array is supported. + Comparision ((all A TXT items in B and equal) AND (all B TXT items in A and equal)) is supported. + +*/ + +/* + MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts contructor +*/ +MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts(void) + : m_pTxts(0) +{ + +} + +/* + MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts copy-constructor +*/ +MDNSResponder::stcMDNSServiceTxts::stcMDNSServiceTxts(const stcMDNSServiceTxts& p_Other) + : m_pTxts(0) +{ + + operator=(p_Other); +} + +/* + MDNSResponder::stcMDNSServiceTxts::~stcMDNSServiceTxts destructor +*/ +MDNSResponder::stcMDNSServiceTxts::~stcMDNSServiceTxts(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSServiceTxts::operator= +*/ +MDNSResponder::stcMDNSServiceTxts& MDNSResponder::stcMDNSServiceTxts::operator=(const stcMDNSServiceTxts& p_Other) +{ + + if (this != &p_Other) + { + clear(); + + for (stcMDNSServiceTxt* pOtherTxt = p_Other.m_pTxts; pOtherTxt; pOtherTxt = pOtherTxt->m_pNext) + { + add(new stcMDNSServiceTxt(*pOtherTxt)); + } + } + return *this; +} + +/* + MDNSResponder::stcMDNSServiceTxts::clear +*/ +bool MDNSResponder::stcMDNSServiceTxts::clear(void) +{ + + while (m_pTxts) + { + stcMDNSServiceTxt* pNext = m_pTxts->m_pNext; + delete m_pTxts; + m_pTxts = pNext; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceTxts::add +*/ +bool MDNSResponder::stcMDNSServiceTxts::add(MDNSResponder::stcMDNSServiceTxt* p_pTxt) +{ + + bool bResult = false; + + if (p_pTxt) + { + p_pTxt->m_pNext = m_pTxts; + m_pTxts = p_pTxt; + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::remove +*/ +bool MDNSResponder::stcMDNSServiceTxts::remove(stcMDNSServiceTxt* p_pTxt) +{ + + bool bResult = false; + + if (p_pTxt) + { + stcMDNSServiceTxt* pPred = m_pTxts; + while ((pPred) && + (pPred->m_pNext != p_pTxt)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pTxt->m_pNext; + delete p_pTxt; + bResult = true; + } + else if (m_pTxts == p_pTxt) // No predecesor, but first item + { + m_pTxts = p_pTxt->m_pNext; + delete p_pTxt; + bResult = true; + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::removeTempTxts +*/ +bool MDNSResponder::stcMDNSServiceTxts::removeTempTxts(void) +{ + + bool bResult = true; + + stcMDNSServiceTxt* pTxt = m_pTxts; + while ((bResult) && + (pTxt)) + { + stcMDNSServiceTxt* pNext = pTxt->m_pNext; + if (pTxt->m_bTemp) + { + bResult = remove(pTxt); + } + pTxt = pNext; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::find +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const char* p_pcKey) +{ + + stcMDNSServiceTxt* pResult = 0; + + for (stcMDNSServiceTxt* pTxt = m_pTxts; pTxt; pTxt = pTxt->m_pNext) + { + if ((p_pcKey) && + (0 == strcmp(pTxt->m_pcKey, p_pcKey))) + { + pResult = pTxt; + break; + } + } + return pResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::find +*/ +const MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const char* p_pcKey) const +{ + + const stcMDNSServiceTxt* pResult = 0; + + for (const stcMDNSServiceTxt* pTxt = m_pTxts; pTxt; pTxt = pTxt->m_pNext) + { + if ((p_pcKey) && + (0 == strcmp(pTxt->m_pcKey, p_pcKey))) + { + + pResult = pTxt; + break; + } + } + return pResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::find +*/ +MDNSResponder::stcMDNSServiceTxt* MDNSResponder::stcMDNSServiceTxts::find(const stcMDNSServiceTxt* p_pTxt) +{ + + stcMDNSServiceTxt* pResult = 0; + + for (stcMDNSServiceTxt* pTxt = m_pTxts; pTxt; pTxt = pTxt->m_pNext) + { + if (p_pTxt == pTxt) + { + pResult = pTxt; + break; + } + } + return pResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::length +*/ +uint16_t MDNSResponder::stcMDNSServiceTxts::length(void) const +{ + + uint16_t u16Length = 0; + + stcMDNSServiceTxt* pTxt = m_pTxts; + while (pTxt) + { + u16Length += 1; // Length byte + u16Length += pTxt->length(); // Text + pTxt = pTxt->m_pNext; + } + return u16Length; +} + +/* + MDNSResponder::stcMDNSServiceTxts::c_strLength + + (incl. closing '\0'). Length bytes place is used for delimiting ';' and closing '\0' +*/ +size_t MDNSResponder::stcMDNSServiceTxts::c_strLength(void) const +{ + + return length(); +} + +/* + MDNSResponder::stcMDNSServiceTxts::c_str +*/ +bool MDNSResponder::stcMDNSServiceTxts::c_str(char* p_pcBuffer) +{ + + bool bResult = false; + + if (p_pcBuffer) + { + bResult = true; + + *p_pcBuffer = 0; + for (stcMDNSServiceTxt* pTxt = m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) + { + size_t stLength; + if ((bResult = (0 != (stLength = (pTxt->m_pcKey ? strlen(pTxt->m_pcKey) : 0))))) + { + if (pTxt != m_pTxts) + { + *p_pcBuffer++ = ';'; + } + strncpy(p_pcBuffer, pTxt->m_pcKey, stLength); p_pcBuffer[stLength] = 0; + p_pcBuffer += stLength; + *p_pcBuffer++ = '='; + if ((stLength = (pTxt->m_pcValue ? strlen(pTxt->m_pcValue) : 0))) + { + strncpy(p_pcBuffer, pTxt->m_pcValue, stLength); p_pcBuffer[stLength] = 0; + p_pcBuffer += stLength; + } + } + } + *p_pcBuffer++ = 0; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::bufferLength + + (incl. closing '\0'). +*/ +size_t MDNSResponder::stcMDNSServiceTxts::bufferLength(void) const +{ + + return (length() + 1); +} + +/* + MDNSResponder::stcMDNSServiceTxts::toBuffer +*/ +bool MDNSResponder::stcMDNSServiceTxts::buffer(char* p_pcBuffer) +{ + + bool bResult = false; + + if (p_pcBuffer) + { + bResult = true; + + *p_pcBuffer = 0; + for (stcMDNSServiceTxt* pTxt = m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) + { + *(unsigned char*)p_pcBuffer++ = pTxt->length(); + size_t stLength; + if ((bResult = (0 != (stLength = (pTxt->m_pcKey ? strlen(pTxt->m_pcKey) : 0))))) + { + memcpy(p_pcBuffer, pTxt->m_pcKey, stLength); + p_pcBuffer += stLength; + *p_pcBuffer++ = '='; + if ((stLength = (pTxt->m_pcValue ? strlen(pTxt->m_pcValue) : 0))) + { + memcpy(p_pcBuffer, pTxt->m_pcValue, stLength); + p_pcBuffer += stLength; + } + } + } + *p_pcBuffer++ = 0; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::compare +*/ +bool MDNSResponder::stcMDNSServiceTxts::compare(const MDNSResponder::stcMDNSServiceTxts& p_Other) const +{ + + bool bResult = false; + + if ((bResult = (length() == p_Other.length()))) + { + // Compare A->B + for (const stcMDNSServiceTxt* pTxt = m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) + { + const stcMDNSServiceTxt* pOtherTxt = p_Other.find(pTxt->m_pcKey); + bResult = ((pOtherTxt) && + (pTxt->m_pcValue) && + (pOtherTxt->m_pcValue) && + (strlen(pTxt->m_pcValue) == strlen(pOtherTxt->m_pcValue)) && + (0 == strcmp(pTxt->m_pcValue, pOtherTxt->m_pcValue))); + } + // Compare B->A + for (const stcMDNSServiceTxt* pOtherTxt = p_Other.m_pTxts; ((bResult) && (pOtherTxt)); pOtherTxt = pOtherTxt->m_pNext) + { + const stcMDNSServiceTxt* pTxt = find(pOtherTxt->m_pcKey); + bResult = ((pTxt) && + (pOtherTxt->m_pcValue) && + (pTxt->m_pcValue) && + (strlen(pOtherTxt->m_pcValue) == strlen(pTxt->m_pcValue)) && + (0 == strcmp(pOtherTxt->m_pcValue, pTxt->m_pcValue))); + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceTxts::operator== +*/ +bool MDNSResponder::stcMDNSServiceTxts::operator==(const stcMDNSServiceTxts& p_Other) const +{ + + return compare(p_Other); +} + +/* + MDNSResponder::stcMDNSServiceTxts::operator!= +*/ +bool MDNSResponder::stcMDNSServiceTxts::operator!=(const stcMDNSServiceTxts& p_Other) const +{ + + return !compare(p_Other); +} + + +/** + MDNSResponder::stcMDNS_MsgHeader + + A MDNS message haeder. + +*/ + +/* + MDNSResponder::stcMDNS_MsgHeader::stcMDNS_MsgHeader +*/ +MDNSResponder::stcMDNS_MsgHeader::stcMDNS_MsgHeader(uint16_t p_u16ID /*= 0*/, + bool p_bQR /*= false*/, + unsigned char p_ucOpcode /*= 0*/, + bool p_bAA /*= false*/, + bool p_bTC /*= false*/, + bool p_bRD /*= false*/, + bool p_bRA /*= false*/, + unsigned char p_ucRCode /*= 0*/, + uint16_t p_u16QDCount /*= 0*/, + uint16_t p_u16ANCount /*= 0*/, + uint16_t p_u16NSCount /*= 0*/, + uint16_t p_u16ARCount /*= 0*/) + : m_u16ID(p_u16ID), + m_1bQR(p_bQR), m_4bOpcode(p_ucOpcode), m_1bAA(p_bAA), m_1bTC(p_bTC), m_1bRD(p_bRD), + m_1bRA(p_bRA), m_3bZ(0), m_4bRCode(p_ucRCode), + m_u16QDCount(p_u16QDCount), + m_u16ANCount(p_u16ANCount), + m_u16NSCount(p_u16NSCount), + m_u16ARCount(p_u16ARCount) +{ + +} + + +/** + MDNSResponder::stcMDNS_RRDomain + + A MDNS domain object. + The labels of the domain are stored (DNS-like encoded) in 'm_acName': + [length byte]varlength label[length byte]varlength label[0] + 'm_u16NameLength' stores the used length of 'm_acName'. + Dynamic label addition is supported. + Comparison is supported. + Export as byte array 'esp8266.local' is supported. + +*/ + +/* + MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain constructor +*/ +MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain(void) + : m_u16NameLength(0) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain copy-constructor +*/ +MDNSResponder::stcMDNS_RRDomain::stcMDNS_RRDomain(const stcMDNS_RRDomain& p_Other) + : m_u16NameLength(0) +{ + + operator=(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRDomain::operator = +*/ +MDNSResponder::stcMDNS_RRDomain& MDNSResponder::stcMDNS_RRDomain::operator=(const stcMDNS_RRDomain& p_Other) +{ + + if (&p_Other != this) + { + memcpy(m_acName, p_Other.m_acName, sizeof(m_acName)); + m_u16NameLength = p_Other.m_u16NameLength; + } + return *this; +} + +/* + MDNSResponder::stcMDNS_RRDomain::clear +*/ +bool MDNSResponder::stcMDNS_RRDomain::clear(void) +{ + + memset(m_acName, 0, sizeof(m_acName)); + m_u16NameLength = 0; + return true; +} + +/* + MDNSResponder::stcMDNS_RRDomain::addLabel +*/ +bool MDNSResponder::stcMDNS_RRDomain::addLabel(const char* p_pcLabel, + bool p_bPrependUnderline /*= false*/) +{ + + bool bResult = false; + + size_t stLength = (p_pcLabel + ? (strlen(p_pcLabel) + (p_bPrependUnderline ? 1 : 0)) + : 0); + if ((MDNS_DOMAIN_LABEL_MAXLENGTH >= stLength) && + (MDNS_DOMAIN_MAXLENGTH >= (m_u16NameLength + (1 + stLength)))) + { + // Length byte + m_acName[m_u16NameLength] = (unsigned char)stLength; // Might be 0! + ++m_u16NameLength; + // Label + if (stLength) + { + if (p_bPrependUnderline) + { + m_acName[m_u16NameLength++] = '_'; + --stLength; + } + strncpy(&(m_acName[m_u16NameLength]), p_pcLabel, stLength); m_acName[m_u16NameLength + stLength] = 0; + m_u16NameLength += stLength; + } + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNS_RRDomain::compare +*/ +bool MDNSResponder::stcMDNS_RRDomain::compare(const stcMDNS_RRDomain& p_Other) const +{ + + bool bResult = false; + + if (m_u16NameLength == p_Other.m_u16NameLength) + { + const char* pT = m_acName; + const char* pO = p_Other.m_acName; + while ((pT) && + (pO) && + (*((unsigned char*)pT) == *((unsigned char*)pO)) && // Same length AND + (0 == strncasecmp((pT + 1), (pO + 1), *((unsigned char*)pT)))) // Same content + { + if (*((unsigned char*)pT)) // Not 0 + { + pT += (1 + * ((unsigned char*)pT)); // Shift by length byte and lenght + pO += (1 + * ((unsigned char*)pO)); + } + else // Is 0 -> Successfully reached the end + { + bResult = true; + break; + } + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNS_RRDomain::operator == +*/ +bool MDNSResponder::stcMDNS_RRDomain::operator==(const stcMDNS_RRDomain& p_Other) const +{ + + return compare(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRDomain::operator != +*/ +bool MDNSResponder::stcMDNS_RRDomain::operator!=(const stcMDNS_RRDomain& p_Other) const +{ + + return !compare(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRDomain::operator > +*/ +bool MDNSResponder::stcMDNS_RRDomain::operator>(const stcMDNS_RRDomain& p_Other) const +{ + + // TODO: Check, if this is a good idea... + return !compare(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRDomain::c_strLength +*/ +size_t MDNSResponder::stcMDNS_RRDomain::c_strLength(void) const +{ + + size_t stLength = 0; + + unsigned char* pucLabelLength = (unsigned char*)m_acName; + while (*pucLabelLength) + { + stLength += (*pucLabelLength + 1 /* +1 for '.' or '\0'*/); + pucLabelLength += (*pucLabelLength + 1); + } + return stLength; +} + +/* + MDNSResponder::stcMDNS_RRDomain::c_str +*/ +bool MDNSResponder::stcMDNS_RRDomain::c_str(char* p_pcBuffer) +{ + + bool bResult = false; + + if (p_pcBuffer) + { + *p_pcBuffer = 0; + unsigned char* pucLabelLength = (unsigned char*)m_acName; + while (*pucLabelLength) + { + memcpy(p_pcBuffer, (const char*)(pucLabelLength + 1), *pucLabelLength); + p_pcBuffer += *pucLabelLength; + pucLabelLength += (*pucLabelLength + 1); + *p_pcBuffer++ = (*pucLabelLength ? '.' : '\0'); + } + bResult = true; + } + return bResult; +} + + +/** + MDNSResponder::stcMDNS_RRAttributes + + A MDNS attributes object. + +*/ + +/* + MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes constructor +*/ +MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes(uint16_t p_u16Type /*= 0*/, + uint16_t p_u16Class /*= 1 DNS_RRCLASS_IN Internet*/) + : m_u16Type(p_u16Type), + m_u16Class(p_u16Class) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes copy-constructor +*/ +MDNSResponder::stcMDNS_RRAttributes::stcMDNS_RRAttributes(const MDNSResponder::stcMDNS_RRAttributes& p_Other) +{ + + operator=(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRAttributes::operator = +*/ +MDNSResponder::stcMDNS_RRAttributes& MDNSResponder::stcMDNS_RRAttributes::operator=(const MDNSResponder::stcMDNS_RRAttributes& p_Other) +{ + + if (&p_Other != this) + { + m_u16Type = p_Other.m_u16Type; + m_u16Class = p_Other.m_u16Class; + } + return *this; +} + + +/** + MDNSResponder::stcMDNS_RRHeader + + A MDNS record header (domain and attributes) object. + +*/ + +/* + MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader constructor +*/ +MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader(void) +{ + +} + +/* + MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader copy-constructor +*/ +MDNSResponder::stcMDNS_RRHeader::stcMDNS_RRHeader(const stcMDNS_RRHeader& p_Other) +{ + + operator=(p_Other); +} + +/* + MDNSResponder::stcMDNS_RRHeader::operator = +*/ +MDNSResponder::stcMDNS_RRHeader& MDNSResponder::stcMDNS_RRHeader::operator=(const MDNSResponder::stcMDNS_RRHeader& p_Other) +{ + + if (&p_Other != this) + { + m_Domain = p_Other.m_Domain; + m_Attributes = p_Other.m_Attributes; + } + return *this; +} + +/* + MDNSResponder::stcMDNS_RRHeader::clear +*/ +bool MDNSResponder::stcMDNS_RRHeader::clear(void) +{ + + m_Domain.clear(); + return true; +} + + +/** + MDNSResponder::stcMDNS_RRQuestion + + A MDNS question record object (header + question flags) + +*/ + +/* + MDNSResponder::stcMDNS_RRQuestion::stcMDNS_RRQuestion constructor +*/ +MDNSResponder::stcMDNS_RRQuestion::stcMDNS_RRQuestion(void) + : m_pNext(0), + m_bUnicast(false) +{ + +} + + +/** + MDNSResponder::stcMDNS_RRAnswer + + A MDNS answer record object (header + answer content). + This is a 'virtual' base class for all other MDNS answer classes. + +*/ + +/* + MDNSResponder::stcMDNS_RRAnswer::stcMDNS_RRAnswer constructor +*/ +MDNSResponder::stcMDNS_RRAnswer::stcMDNS_RRAnswer(enuAnswerType p_AnswerType, + const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : m_pNext(0), + m_AnswerType(p_AnswerType), + m_Header(p_Header), + m_u32TTL(p_u32TTL) +{ + + // Extract 'cache flush'-bit + m_bCacheFlush = (m_Header.m_Attributes.m_u16Class & 0x8000); + m_Header.m_Attributes.m_u16Class &= (~0x8000); +} + +/* + MDNSResponder::stcMDNS_RRAnswer::~stcMDNS_RRAnswer destructor +*/ +MDNSResponder::stcMDNS_RRAnswer::~stcMDNS_RRAnswer(void) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswer::answerType +*/ +MDNSResponder::enuAnswerType MDNSResponder::stcMDNS_RRAnswer::answerType(void) const +{ + + return m_AnswerType; +} + +/* + MDNSResponder::stcMDNS_RRAnswer::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswer::clear(void) +{ + + m_pNext = 0; + m_Header.clear(); + return true; +} + + +/** + MDNSResponder::stcMDNS_RRAnswerA + + A MDNS A answer object. + Extends the base class by an IP4 address member. + +*/ + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA constructor +*/ +MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA(const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_A, p_Header, p_u32TTL), + m_IPAddress(0, 0, 0, 0) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerA::stcMDNS_RRAnswerA destructor +*/ +MDNSResponder::stcMDNS_RRAnswerA::~stcMDNS_RRAnswerA(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerA::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerA::clear(void) +{ + + m_IPAddress = IPAddress(0, 0, 0, 0); + return true; +} +#endif + + +/** + MDNSResponder::stcMDNS_RRAnswerPTR + + A MDNS PTR answer object. + Extends the base class by a MDNS domain member. + +*/ + +/* + MDNSResponder::stcMDNS_RRAnswerPTR::stcMDNS_RRAnswerPTR constructor +*/ +MDNSResponder::stcMDNS_RRAnswerPTR::stcMDNS_RRAnswerPTR(const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_PTR, p_Header, p_u32TTL) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerPTR::~stcMDNS_RRAnswerPTR destructor +*/ +MDNSResponder::stcMDNS_RRAnswerPTR::~stcMDNS_RRAnswerPTR(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerPTR::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerPTR::clear(void) +{ + + m_PTRDomain.clear(); + return true; +} + + +/** + MDNSResponder::stcMDNS_RRAnswerTXT + + A MDNS TXT answer object. + Extends the base class by a MDNS TXT items list member. + +*/ + +/* + MDNSResponder::stcMDNS_RRAnswerTXT::stcMDNS_RRAnswerTXT constructor +*/ +MDNSResponder::stcMDNS_RRAnswerTXT::stcMDNS_RRAnswerTXT(const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_TXT, p_Header, p_u32TTL) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerTXT::~stcMDNS_RRAnswerTXT destructor +*/ +MDNSResponder::stcMDNS_RRAnswerTXT::~stcMDNS_RRAnswerTXT(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerTXT::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerTXT::clear(void) +{ + + m_Txts.clear(); + return true; +} + + +/** + MDNSResponder::stcMDNS_RRAnswerAAAA + + A MDNS AAAA answer object. + (Should) extend the base class by an IP6 address member. + +*/ + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::stcMDNS_RRAnswerAAAA::stcMDNS_RRAnswerAAAA constructor +*/ +MDNSResponder::stcMDNS_RRAnswerAAAA::stcMDNS_RRAnswerAAAA(const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_AAAA, p_Header, p_u32TTL) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerAAAA::~stcMDNS_RRAnswerAAAA destructor +*/ +MDNSResponder::stcMDNS_RRAnswerAAAA::~stcMDNS_RRAnswerAAAA(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerAAAA::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerAAAA::clear(void) +{ + + return true; +} +#endif + + +/** + MDNSResponder::stcMDNS_RRAnswerSRV + + A MDNS SRV answer object. + Extends the base class by a port member. + +*/ + +/* + MDNSResponder::stcMDNS_RRAnswerSRV::stcMDNS_RRAnswerSRV constructor +*/ +MDNSResponder::stcMDNS_RRAnswerSRV::stcMDNS_RRAnswerSRV(const MDNSResponder::stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_SRV, p_Header, p_u32TTL), + m_u16Priority(0), + m_u16Weight(0), + m_u16Port(0) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerSRV::~stcMDNS_RRAnswerSRV destructor +*/ +MDNSResponder::stcMDNS_RRAnswerSRV::~stcMDNS_RRAnswerSRV(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerSRV::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerSRV::clear(void) +{ + + m_u16Priority = 0; + m_u16Weight = 0; + m_u16Port = 0; + m_SRVDomain.clear(); + return true; +} + + +/** + MDNSResponder::stcMDNS_RRAnswerGeneric + + An unknown (generic) MDNS answer object. + Extends the base class by a RDATA buffer member. + +*/ + +/* + MDNSResponder::stcMDNS_RRAnswerGeneric::stcMDNS_RRAnswerGeneric constructor +*/ +MDNSResponder::stcMDNS_RRAnswerGeneric::stcMDNS_RRAnswerGeneric(const stcMDNS_RRHeader& p_Header, + uint32_t p_u32TTL) + : stcMDNS_RRAnswer(AnswerType_Generic, p_Header, p_u32TTL), + m_u16RDLength(0), + m_pu8RDData(0) +{ + +} + +/* + MDNSResponder::stcMDNS_RRAnswerGeneric::~stcMDNS_RRAnswerGeneric destructor +*/ +MDNSResponder::stcMDNS_RRAnswerGeneric::~stcMDNS_RRAnswerGeneric(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNS_RRAnswerGeneric::clear +*/ +bool MDNSResponder::stcMDNS_RRAnswerGeneric::clear(void) +{ + + if (m_pu8RDData) + { + delete[] m_pu8RDData; + m_pu8RDData = 0; + } + m_u16RDLength = 0; + + return true; +} + + +/** + MDNSResponder::stcProbeInformation + + Probing status information for a host or service domain + +*/ + +/* + MDNSResponder::stcProbeInformation::stcProbeInformation constructor +*/ +MDNSResponder::stcProbeInformation::stcProbeInformation(void) + : m_ProbingStatus(ProbingStatus_WaitingForData), + m_u8SentCount(0), + m_Timeout(esp8266::polledTimeout::oneShotMs::neverExpires), + m_bConflict(false), + m_bTiebreakNeeded(false), + m_fnHostProbeResultCallback(0), + m_fnServiceProbeResultCallback(0) +{ +} + +/* + MDNSResponder::stcProbeInformation::clear +*/ +bool MDNSResponder::stcProbeInformation::clear(bool p_bClearUserdata /*= false*/) +{ + + m_ProbingStatus = ProbingStatus_WaitingForData; + m_u8SentCount = 0; + m_Timeout.resetToNeverExpires(); + m_bConflict = false; + m_bTiebreakNeeded = false; + if (p_bClearUserdata) + { + m_fnHostProbeResultCallback = 0; + m_fnServiceProbeResultCallback = 0; + } + return true; +} + +/** + MDNSResponder::stcMDNSService + + A MDNS service object (to be announced by the MDNS responder) + The service instance may be '\0'; in this case the hostname is used + and the flag m_bAutoName is set. If the hostname changes, all 'auto- + named' services are renamed also. + m_u8Replymask is used while preparing a response to a MDNS query. It is + resetted in '_sendMDNSMessage' afterwards. +*/ + +/* + MDNSResponder::stcMDNSService::stcMDNSService constructor +*/ +MDNSResponder::stcMDNSService::stcMDNSService(const char* p_pcName /*= 0*/, + const char* p_pcService /*= 0*/, + const char* p_pcProtocol /*= 0*/) + : m_pNext(0), + m_pcName(0), + m_bAutoName(false), + m_pcService(0), + m_pcProtocol(0), + m_u16Port(0), + m_u8ReplyMask(0), + m_fnTxtCallback(0) +{ + + setName(p_pcName); + setService(p_pcService); + setProtocol(p_pcProtocol); +} + +/* + MDNSResponder::stcMDNSService::~stcMDNSService destructor +*/ +MDNSResponder::stcMDNSService::~stcMDNSService(void) +{ + + releaseName(); + releaseService(); + releaseProtocol(); +} + +/* + MDNSResponder::stcMDNSService::setName +*/ +bool MDNSResponder::stcMDNSService::setName(const char* p_pcName) +{ + + bool bResult = false; + + releaseName(); + size_t stLength = (p_pcName ? strlen(p_pcName) : 0); + if (stLength) + { + if ((bResult = (0 != (m_pcName = new char[stLength + 1])))) + { + strncpy(m_pcName, p_pcName, stLength); + m_pcName[stLength] = 0; + } + } + else + { + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSService::releaseName +*/ +bool MDNSResponder::stcMDNSService::releaseName(void) +{ + + if (m_pcName) + { + delete[] m_pcName; + m_pcName = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSService::setService +*/ +bool MDNSResponder::stcMDNSService::setService(const char* p_pcService) +{ + + bool bResult = false; + + releaseService(); + size_t stLength = (p_pcService ? strlen(p_pcService) : 0); + if (stLength) + { + if ((bResult = (0 != (m_pcService = new char[stLength + 1])))) + { + strncpy(m_pcService, p_pcService, stLength); + m_pcService[stLength] = 0; + } + } + else + { + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSService::releaseService +*/ +bool MDNSResponder::stcMDNSService::releaseService(void) +{ + + if (m_pcService) + { + delete[] m_pcService; + m_pcService = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSService::setProtocol +*/ +bool MDNSResponder::stcMDNSService::setProtocol(const char* p_pcProtocol) +{ + + bool bResult = false; + + releaseProtocol(); + size_t stLength = (p_pcProtocol ? strlen(p_pcProtocol) : 0); + if (stLength) + { + if ((bResult = (0 != (m_pcProtocol = new char[stLength + 1])))) + { + strncpy(m_pcProtocol, p_pcProtocol, stLength); + m_pcProtocol[stLength] = 0; + } + } + else + { + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSService::releaseProtocol +*/ +bool MDNSResponder::stcMDNSService::releaseProtocol(void) +{ + + if (m_pcProtocol) + { + delete[] m_pcProtocol; + m_pcProtocol = 0; + } + return true; +} + + +/** + MDNSResponder::stcMDNSServiceQuery + + A MDNS service query object. + Service queries may be static or dynamic. + As the static service query is processed in the blocking function 'queryService', + only one static service service may exist. The processing of the answers is done + on the WiFi-stack side of the ESP stack structure (via 'UDPContext.onRx(_update)'). + +*/ + +/** + MDNSResponder::stcMDNSServiceQuery::stcAnswer + + One answer for a service query. + Every answer must contain + - a service instance entry (pivot), + and may contain + - a host domain, + - a port + - an IP4 address + (- an IP6 address) + - a MDNS TXTs + The existance of a component is flaged in 'm_u32ContentFlags'. + For every answer component a TTL value is maintained. + Answer objects can be connected to a linked list. + + For the host domain, service domain and TXTs components, a char array + representation can be retrieved (which is created on demand). + +*/ + +/** + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL + + The TTL (Time-To-Live) for an specific answer content. + The 80% and outdated states are calculated based on the current time (millis) + and the 'set' time (also millis). + If the answer is scheduled for an update, the corresponding flag should be set. + + / + + / * + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL constructor + / + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL(uint32_t p_u32TTL / *= 0* /) + : m_bUpdateScheduled(false) { + + set(p_u32TTL * 1000); + } + + / * + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set + / + bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TTL) { + + m_TTLTimeFlag.restart(p_u32TTL * 1000); + m_bUpdateScheduled = false; + + return true; + } + + / * + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::has80Percent + / + bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::has80Percent(void) const { + + return ((m_TTLTimeFlag.getTimeout()) && + (!m_bUpdateScheduled) && + (m_TTLTimeFlag.hypotheticalTimeout((m_TTLTimeFlag.getTimeout() * 800) / 1000))); + } + + / * + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::isOutdated + / + bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::isOutdated(void) const { + + return ((m_TTLTimeFlag.getTimeout()) && + (m_TTLTimeFlag.flagged())); + }*/ + + +/** + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL + + The TTL (Time-To-Live) for an specific answer content. + The 80% and outdated states are calculated based on the current time (millis) + and the 'set' time (also millis). + If the answer is scheduled for an update, the corresponding flag should be set. + +*/ + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL constructor +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL(void) + : m_u32TTL(0), + m_TTLTimeout(esp8266::polledTimeout::oneShotMs::neverExpires), + m_timeoutLevel(TIMEOUTLEVEL_UNSET) +{ + +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TTL) +{ + + m_u32TTL = p_u32TTL; + if (m_u32TTL) + { + m_timeoutLevel = TIMEOUTLEVEL_BASE; // Set to 80% + m_TTLTimeout.reset(timeout()); + } + else + { + m_timeoutLevel = TIMEOUTLEVEL_UNSET; // undef + m_TTLTimeout.resetToNeverExpires(); + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged(void) +{ + + return ((m_u32TTL) && + (TIMEOUTLEVEL_UNSET != m_timeoutLevel) && + (m_TTLTimeout.expired())); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::restart +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::restart(void) +{ + + bool bResult = true; + + if ((TIMEOUTLEVEL_BASE <= m_timeoutLevel) && // >= 80% AND + (TIMEOUTLEVEL_FINAL > m_timeoutLevel)) // < 100% + { + + m_timeoutLevel += TIMEOUTLEVEL_INTERVAL; // increment by 5% + m_TTLTimeout.reset(timeout()); + } + else + { + bResult = false; + m_TTLTimeout.resetToNeverExpires(); + m_timeoutLevel = TIMEOUTLEVEL_UNSET; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::prepareDeletion +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::prepareDeletion(void) +{ + + m_timeoutLevel = TIMEOUTLEVEL_FINAL; + m_TTLTimeout.reset(1 * 1000); // See RFC 6762, 10.1 + + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::finalTimeoutLevel +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::finalTimeoutLevel(void) const +{ + + return (TIMEOUTLEVEL_FINAL == m_timeoutLevel); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::timeout +*/ +unsigned long MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::timeout(void) const +{ + + uint32_t u32Timeout = esp8266::polledTimeout::oneShotMs::neverExpires; + + if (TIMEOUTLEVEL_BASE == m_timeoutLevel) // 80% + { + u32Timeout = (m_u32TTL * 800); // to milliseconds + } + else if ((TIMEOUTLEVEL_BASE < m_timeoutLevel) && // >80% AND + (TIMEOUTLEVEL_FINAL >= m_timeoutLevel)) // <= 100% + { + + u32Timeout = (m_u32TTL * 50); + } // else: invalid + return u32Timeout; +} + + +#ifdef MDNS_IP4_SUPPORT +/** + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address + +*/ + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address::stcIP4Address constructor +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address::stcIP4Address(IPAddress p_IPAddress, + uint32_t p_u32TTL /*= 0*/) + : m_pNext(0), + m_IPAddress(p_IPAddress) +{ + + m_TTL.set(p_u32TTL); +} +#endif + + +/** + MDNSResponder::stcMDNSServiceQuery::stcAnswer +*/ + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcAnswer constructor +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcAnswer(void) + : m_pNext(0), + m_pcServiceDomain(0), + m_pcHostDomain(0), + m_u16Port(0), + m_pcTxts(0), +#ifdef MDNS_IP4_SUPPORT + m_pIP4Addresses(0), +#endif +#ifdef MDNS_IP6_SUPPORT + m_pIP6Addresses(0), +#endif + m_u32ContentFlags(0) +{ +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::~stcAnswer destructor +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::~stcAnswer(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::clear +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::clear(void) +{ + + return ((releaseTxts()) && +#ifdef MDNS_IP4_SUPPORT + (releaseIP4Addresses()) && +#endif +#ifdef MDNS_IP6_SUPPORT + (releaseIP6Addresses()) +#endif + (releaseHostDomain()) && + (releaseServiceDomain())); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocServiceDomain + + Alloc memory for the char array representation of the service domain. + +*/ +char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocServiceDomain(size_t p_stLength) +{ + + releaseServiceDomain(); + if (p_stLength) + { + m_pcServiceDomain = new char[p_stLength]; + } + return m_pcServiceDomain; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseServiceDomain +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseServiceDomain(void) +{ + + if (m_pcServiceDomain) + { + delete[] m_pcServiceDomain; + m_pcServiceDomain = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocHostDomain + + Alloc memory for the char array representation of the host domain. + +*/ +char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocHostDomain(size_t p_stLength) +{ + + releaseHostDomain(); + if (p_stLength) + { + m_pcHostDomain = new char[p_stLength]; + } + return m_pcHostDomain; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseHostDomain +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseHostDomain(void) +{ + + if (m_pcHostDomain) + { + delete[] m_pcHostDomain; + m_pcHostDomain = 0; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocTxts + + Alloc memory for the char array representation of the TXT items. + +*/ +char* MDNSResponder::stcMDNSServiceQuery::stcAnswer::allocTxts(size_t p_stLength) +{ + + releaseTxts(); + if (p_stLength) + { + m_pcTxts = new char[p_stLength]; + } + return m_pcTxts; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseTxts +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseTxts(void) +{ + + if (m_pcTxts) + { + delete[] m_pcTxts; + m_pcTxts = 0; + } + return true; +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP4Addresses +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP4Addresses(void) +{ + + while (m_pIP4Addresses) + { + stcIP4Address* pNext = m_pIP4Addresses->m_pNext; + delete m_pIP4Addresses; + m_pIP4Addresses = pNext; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP4Address +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP4Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* p_pIP4Address) +{ + + bool bResult = false; + + if (p_pIP4Address) + { + p_pIP4Address->m_pNext = m_pIP4Addresses; + m_pIP4Addresses = p_pIP4Address; + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP4Address +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP4Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* p_pIP4Address) +{ + + bool bResult = false; + + if (p_pIP4Address) + { + stcIP4Address* pPred = m_pIP4Addresses; + while ((pPred) && + (pPred->m_pNext != p_pIP4Address)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pIP4Address->m_pNext; + delete p_pIP4Address; + bResult = true; + } + else if (m_pIP4Addresses == p_pIP4Address) // No predecesor, but first item + { + m_pIP4Addresses = p_pIP4Address->m_pNext; + delete p_pIP4Address; + bResult = true; + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address (const) +*/ +const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address(const IPAddress& p_IPAddress) const +{ + + return (stcIP4Address*)(((const stcAnswer*)this)->findIP4Address(p_IPAddress)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP4Address(const IPAddress& p_IPAddress) +{ + + stcIP4Address* pIP4Address = m_pIP4Addresses; + while (pIP4Address) + { + if (pIP4Address->m_IPAddress == p_IPAddress) + { + break; + } + pIP4Address = pIP4Address->m_pNext; + } + return pIP4Address; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressCount +*/ +uint32_t MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressCount(void) const +{ + + uint32_t u32Count = 0; + + stcIP4Address* pIP4Address = m_pIP4Addresses; + while (pIP4Address) + { + ++u32Count; + pIP4Address = pIP4Address->m_pNext; + } + return u32Count; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex(uint32_t p_u32Index) +{ + + return (stcIP4Address*)(((const stcAnswer*)this)->IP4AddressAtIndex(p_u32Index)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex (const) +*/ +const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP4Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP4AddressAtIndex(uint32_t p_u32Index) const +{ + + const stcIP4Address* pIP4Address = 0; + + if (((uint32_t)(-1) != p_u32Index) && + (m_pIP4Addresses)) + { + + uint32_t u32Index; + for (pIP4Address = m_pIP4Addresses, u32Index = 0; ((pIP4Address) && (u32Index < p_u32Index)); pIP4Address = pIP4Address->m_pNext, ++u32Index); + } + return pIP4Address; +} +#endif + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP6Addresses +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::releaseIP6Addresses(void) +{ + + while (m_pIP6Addresses) + { + stcIP6Address* pNext = m_pIP6Addresses->m_pNext; + delete m_pIP6Addresses; + m_pIP6Addresses = pNext; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP6Address +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::addIP6Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* p_pIP6Address) +{ + + bool bResult = false; + + if (p_pIP6Address) + { + p_pIP6Address->m_pNext = m_pIP6Addresses; + m_pIP6Addresses = p_pIP6Address; + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP6Address +*/ +bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::removeIP6Address(MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* p_pIP6Address) +{ + + bool bResult = false; + + if (p_pIP6Address) + { + stcIP6Address* pPred = m_pIP6Addresses; + while ((pPred) && + (pPred->m_pNext != p_pIP6Address)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pIP6Address->m_pNext; + delete p_pIP6Address; + bResult = true; + } + else if (m_pIP6Addresses == p_pIP6Address) // No predecesor, but first item + { + m_pIP6Addresses = p_pIP6Address->m_pNext; + delete p_pIP6Address; + bResult = true; + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address(const IP6Address& p_IPAddress) +{ + + return (stcIP6Address*)(((const stcAnswer*)this)->findIP6Address(p_IPAddress)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address (const) +*/ +const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::findIP6Address(const IPAddress& p_IPAddress) const +{ + + const stcIP6Address* pIP6Address = m_pIP6Addresses; + while (pIP6Address) + { + if (p_IP6Address->m_IPAddress == p_IPAddress) + { + break; + } + pIP6Address = pIP6Address->m_pNext; + } + return pIP6Address; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressCount +*/ +uint32_t MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressCount(void) const +{ + + uint32_t u32Count = 0; + + stcIP6Address* pIP6Address = m_pIP6Addresses; + while (pIP6Address) + { + ++u32Count; + pIP6Address = pIP6Address->m_pNext; + } + return u32Count; +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex (const) +*/ +const MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex(uint32_t p_u32Index) const +{ + + return (stcIP6Address*)(((const stcAnswer*)this)->IP6AddressAtIndex(p_u32Index)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcIP6Address* MDNSResponder::stcMDNSServiceQuery::stcAnswer::IP6AddressAtIndex(uint32_t p_u32Index) +{ + + stcIP6Address* pIP6Address = 0; + + if (((uint32_t)(-1) != p_u32Index) && + (m_pIP6Addresses)) + { + + uint32_t u32Index; + for (pIP6Address = m_pIP6Addresses, u32Index = 0; ((pIP6Address) && (u32Index < p_u32Index)); pIP6Address = pIP6Address->m_pNext, ++u32Index); + } + return pIP6Address; +} +#endif + + +/** + MDNSResponder::stcMDNSServiceQuery + + A service query object. + A static query is flaged via 'm_bLegacyQuery'; while the function 'queryService' + is waiting for answers, the internal flag 'm_bAwaitingAnswers' is set. When the + timeout is reached, the flag is removed. These two flags are only used for static + service queries. + All answers to the service query are stored in 'm_pAnswers' list. + Individual answers may be addressed by index (in the list of answers). + Every time a answer component is added (or changes) in a dynamic service query, + the callback 'm_fnCallback' is called. + The answer list may be searched by service and host domain. + + Service query object may be connected to a linked list. +*/ + +/* + MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery constructor +*/ +MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void) + : m_pNext(0), + m_fnCallback(0), + m_bLegacyQuery(false), + m_u8SentCount(0), + m_ResendTimeout(esp8266::polledTimeout::oneShotMs::neverExpires), + m_bAwaitingAnswers(true), + m_pAnswers(0) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery destructor +*/ +MDNSResponder::stcMDNSServiceQuery::~stcMDNSServiceQuery(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSServiceQuery::clear +*/ +bool MDNSResponder::stcMDNSServiceQuery::clear(void) +{ + + m_fnCallback = 0; + m_bLegacyQuery = false; + m_u8SentCount = 0; + m_ResendTimeout.resetToNeverExpires(); + m_bAwaitingAnswers = true; + while (m_pAnswers) + { + stcAnswer* pNext = m_pAnswers->m_pNext; + delete m_pAnswers; + m_pAnswers = pNext; + } + return true; +} + +/* + MDNSResponder::stcMDNSServiceQuery::answerCount +*/ +uint32_t MDNSResponder::stcMDNSServiceQuery::answerCount(void) const +{ + + uint32_t u32Count = 0; + + stcAnswer* pAnswer = m_pAnswers; + while (pAnswer) + { + ++u32Count; + pAnswer = pAnswer->m_pNext; + } + return u32Count; +} + +/* + MDNSResponder::stcMDNSServiceQuery::answerAtIndex +*/ +const MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::answerAtIndex(uint32_t p_u32Index) const +{ + + const stcAnswer* pAnswer = 0; + + if (((uint32_t)(-1) != p_u32Index) && + (m_pAnswers)) + { + + uint32_t u32Index; + for (pAnswer = m_pAnswers, u32Index = 0; ((pAnswer) && (u32Index < p_u32Index)); pAnswer = pAnswer->m_pNext, ++u32Index); + } + return pAnswer; +} + +/* + MDNSResponder::stcMDNSServiceQuery::answerAtIndex +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::answerAtIndex(uint32_t p_u32Index) +{ + + return (stcAnswer*)(((const stcMDNSServiceQuery*)this)->answerAtIndex(p_u32Index)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::indexOfAnswer +*/ +uint32_t MDNSResponder::stcMDNSServiceQuery::indexOfAnswer(const MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) const +{ + + uint32_t u32Index = 0; + + for (const stcAnswer* pAnswer = m_pAnswers; pAnswer; pAnswer = pAnswer->m_pNext, ++u32Index) + { + if (pAnswer == p_pAnswer) + { + return u32Index; + } + } + return ((uint32_t)(-1)); +} + +/* + MDNSResponder::stcMDNSServiceQuery::addAnswer +*/ +bool MDNSResponder::stcMDNSServiceQuery::addAnswer(MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) +{ + + bool bResult = false; + + if (p_pAnswer) + { + p_pAnswer->m_pNext = m_pAnswers; + m_pAnswers = p_pAnswer; + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::removeAnswer +*/ +bool MDNSResponder::stcMDNSServiceQuery::removeAnswer(MDNSResponder::stcMDNSServiceQuery::stcAnswer* p_pAnswer) +{ + + bool bResult = false; + + if (p_pAnswer) + { + stcAnswer* pPred = m_pAnswers; + while ((pPred) && + (pPred->m_pNext != p_pAnswer)) + { + pPred = pPred->m_pNext; + } + if (pPred) + { + pPred->m_pNext = p_pAnswer->m_pNext; + delete p_pAnswer; + bResult = true; + } + else if (m_pAnswers == p_pAnswer) // No predecesor, but first item + { + m_pAnswers = p_pAnswer->m_pNext; + delete p_pAnswer; + bResult = true; + } + } + return bResult; +} + +/* + MDNSResponder::stcMDNSServiceQuery::findAnswerForServiceDomain +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::findAnswerForServiceDomain(const MDNSResponder::stcMDNS_RRDomain& p_ServiceDomain) +{ + + stcAnswer* pAnswer = m_pAnswers; + while (pAnswer) + { + if (pAnswer->m_ServiceDomain == p_ServiceDomain) + { + break; + } + pAnswer = pAnswer->m_pNext; + } + return pAnswer; +} + +/* + MDNSResponder::stcMDNSServiceQuery::findAnswerForHostDomain +*/ +MDNSResponder::stcMDNSServiceQuery::stcAnswer* MDNSResponder::stcMDNSServiceQuery::findAnswerForHostDomain(const MDNSResponder::stcMDNS_RRDomain& p_HostDomain) +{ + + stcAnswer* pAnswer = m_pAnswers; + while (pAnswer) + { + if (pAnswer->m_HostDomain == p_HostDomain) + { + break; + } + pAnswer = pAnswer->m_pNext; + } + return pAnswer; +} + + +/** + MDNSResponder::stcMDNSSendParameter + + A 'collection' of properties and flags for one MDNS query or response. + Mainly managed by the 'Control' functions. + The current offset in the UPD output buffer is tracked to be able to do + a simple host or service domain compression. + +*/ + +/** + MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem + + A cached host or service domain, incl. the offset in the UDP output buffer. + +*/ + +/* + MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem::stcDomainCacheItem constructor +*/ +MDNSResponder::stcMDNSSendParameter::stcDomainCacheItem::stcDomainCacheItem(const void* p_pHostnameOrService, + bool p_bAdditionalData, + uint32_t p_u16Offset) + : m_pNext(0), + m_pHostnameOrService(p_pHostnameOrService), + m_bAdditionalData(p_bAdditionalData), + m_u16Offset(p_u16Offset) +{ + +} + +/** + MDNSResponder::stcMDNSSendParameter +*/ + +/* + MDNSResponder::stcMDNSSendParameter::stcMDNSSendParameter constructor +*/ +MDNSResponder::stcMDNSSendParameter::stcMDNSSendParameter(void) + : m_pQuestions(0), + m_pDomainCacheItems(0) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSSendParameter::~stcMDNSSendParameter destructor +*/ +MDNSResponder::stcMDNSSendParameter::~stcMDNSSendParameter(void) +{ + + clear(); +} + +/* + MDNSResponder::stcMDNSSendParameter::clear +*/ +bool MDNSResponder::stcMDNSSendParameter::clear(void) +{ + + m_u16ID = 0; + m_u8HostReplyMask = 0; + m_u16Offset = 0; + + m_bLegacyQuery = false; + m_bResponse = false; + m_bAuthorative = false; + m_bUnicast = false; + m_bUnannounce = false; + + m_bCacheFlush = true; + + while (m_pQuestions) + { + stcMDNS_RRQuestion* pNext = m_pQuestions->m_pNext; + delete m_pQuestions; + m_pQuestions = pNext; + } + while (m_pDomainCacheItems) + { + stcDomainCacheItem* pNext = m_pDomainCacheItems->m_pNext; + delete m_pDomainCacheItems; + m_pDomainCacheItems = pNext; + } + return true; +} + +/* + MDNSResponder::stcMDNSSendParameter::shiftOffset +*/ +bool MDNSResponder::stcMDNSSendParameter::shiftOffset(uint16_t p_u16Shift) +{ + + m_u16Offset += p_u16Shift; + return true; +} + +/* + MDNSResponder::stcMDNSSendParameter::addDomainCacheItem +*/ +bool MDNSResponder::stcMDNSSendParameter::addDomainCacheItem(const void* p_pHostnameOrService, + bool p_bAdditionalData, + uint16_t p_u16Offset) +{ + + bool bResult = false; + + stcDomainCacheItem* pNewItem = 0; + if ((p_pHostnameOrService) && + (p_u16Offset) && + ((pNewItem = new stcDomainCacheItem(p_pHostnameOrService, p_bAdditionalData, p_u16Offset)))) + { + + pNewItem->m_pNext = m_pDomainCacheItems; + bResult = ((m_pDomainCacheItems = pNewItem)); + } + return bResult; +} + +/* + MDNSResponder::stcMDNSSendParameter::findCachedDomainOffset +*/ +uint16_t MDNSResponder::stcMDNSSendParameter::findCachedDomainOffset(const void* p_pHostnameOrService, + bool p_bAdditionalData) const +{ + + const stcDomainCacheItem* pCacheItem = m_pDomainCacheItems; + + for (; pCacheItem; pCacheItem = pCacheItem->m_pNext) + { + if ((pCacheItem->m_pHostnameOrService == p_pHostnameOrService) && + (pCacheItem->m_bAdditionalData == p_bAdditionalData)) // Found cache item + { + break; + } + } + return (pCacheItem ? pCacheItem->m_u16Offset : 0); +} + +} // namespace MDNSImplementation + +} // namespace esp8266 + + + diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Transfer.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Transfer.cpp index b4eebbedd..7400abec4 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Transfer.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Transfer.cpp @@ -1,1587 +1,1779 @@ -/* - * LEAmDNS_Transfer.cpp - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -extern "C" { - #include "user_interface.h" -} - -#include "LEAmDNS_lwIPdefs.h" -#include "LEAmDNS_Priv.h" - - -namespace esp8266 { - -/* - * LEAmDNS - */ -namespace MDNSImplementation { - -/** - * CONST STRINGS - */ -static const char* scpcLocal = "local"; -static const char* scpcServices = "services"; -static const char* scpcDNSSD = "dns-sd"; -static const char* scpcUDP = "udp"; -//static const char* scpcTCP = "tcp"; - -#ifdef MDNS_IP4_SUPPORT - static const char* scpcReverseIP4Domain = "in-addr"; -#endif -#ifdef MDNS_IP6_SUPPORT - static const char* scpcReverseIP6Domain = "ip6"; -#endif -static const char* scpcReverseTopDomain = "arpa"; - -/** - * TRANSFER - */ - - -/** - * SENDING - */ - -/* - * MDNSResponder::_sendMDNSMessage - * - * Unicast responses are prepared and sent directly to the querier. - * Multicast responses or queries are transferred to _sendMDNSMessage_Multicast - * - * Any reply flags in installed services are removed at the end! - * - */ -bool MDNSResponder::_sendMDNSMessage(MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - bool bResult = true; - - if (p_rSendParameter.m_bResponse && - p_rSendParameter.m_bUnicast) { // Unicast response -> Send to querier - DEBUG_EX_ERR(if (!m_pUDPContext->getRemoteAddress()) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage: MISSING remote address for response!\n")); }); - IPAddress ipRemote; - ipRemote = m_pUDPContext->getRemoteAddress(); - bResult = ((_prepareMDNSMessage(p_rSendParameter, _getResponseMulticastInterface())) && - (m_pUDPContext->send(ipRemote, m_pUDPContext->getRemotePort()))); - } - else { // Multicast response - bResult = _sendMDNSMessage_Multicast(p_rSendParameter); - } - - // Finally clear service reply masks - for (stcMDNSService* pService=m_pServices; pService; pService=pService->m_pNext) { - pService->m_u8ReplyMask = 0; - } - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_sendMDNSMessage_Multicast - * - * Fills the UDP output buffer (via _prepareMDNSMessage) and sends the buffer - * via the selected WiFi interface (Station or AP) - */ -bool MDNSResponder::_sendMDNSMessage_Multicast(MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - bool bResult = false; - - IPAddress fromIPAddress; - fromIPAddress = _getResponseMulticastInterface(); - m_pUDPContext->setMulticastInterface(fromIPAddress); - -#ifdef MDNS_IP4_SUPPORT - IPAddress toMulticastAddress(DNS_MQUERY_IPV4_GROUP_INIT); -#endif -#ifdef MDNS_IP6_SUPPORT - //TODO: set multicast address - IPAddress toMulticastAddress(DNS_MQUERY_IPV6_GROUP_INIT); -#endif - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage_Multicast: Will send to '%s'.\n"), toMulticastAddress.toString().c_str());); - bResult = ((_prepareMDNSMessage(p_rSendParameter, fromIPAddress)) && - (m_pUDPContext->send(toMulticastAddress, DNS_MQUERY_PORT))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage_Multicast: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_prepareMDNSMessage - * - * The MDNS message is composed in a two-step process. - * In the first loop 'only' the header informations (mainly number of answers) are collected, - * while in the seconds loop, the header and all queries and answers are written to the UDP - * output buffer. - * - */ -bool MDNSResponder::_prepareMDNSMessage(MDNSResponder::stcMDNSSendParameter& p_rSendParameter, - IPAddress p_IPAddress) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage\n"));); - bool bResult = true; - - // Prepare header; count answers - stcMDNS_MsgHeader msgHeader(0, p_rSendParameter.m_bResponse, 0, p_rSendParameter.m_bAuthorative); - // If this is a response, the answers are anwers, - // else this is a query or probe and the answers go into auth section - uint16_t& ru16Answers = (p_rSendParameter.m_bResponse - ? msgHeader.m_u16ANCount - : msgHeader.m_u16NSCount); - - /** - * enuSequence - */ - enum enuSequence { - Sequence_Count = 0, - Sequence_Send = 1 - }; - - // Two step sequence: 'Count' and 'Send' - for (uint32_t sequence=Sequence_Count; ((bResult) && (sequence<=Sequence_Send)); ++sequence) { - DEBUG_EX_INFO( - if (Sequence_Send == sequence) { - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), - (unsigned)msgHeader.m_u16ID, - (unsigned)msgHeader.m_1bQR, (unsigned)msgHeader.m_4bOpcode, (unsigned)msgHeader.m_1bAA, (unsigned)msgHeader.m_1bTC, (unsigned)msgHeader.m_1bRD, - (unsigned)msgHeader.m_1bRA, (unsigned)msgHeader.m_4bRCode, - (unsigned)msgHeader.m_u16QDCount, - (unsigned)msgHeader.m_u16ANCount, - (unsigned)msgHeader.m_u16NSCount, - (unsigned)msgHeader.m_u16ARCount); - } - ); - // Count/send - // Header - bResult = ((Sequence_Count == sequence) - ? true - : _writeMDNSMsgHeader(msgHeader, p_rSendParameter)); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSMsgHeader FAILED!\n"));); - // Questions - for (stcMDNS_RRQuestion* pQuestion=p_rSendParameter.m_pQuestions; ((bResult) && (pQuestion)); pQuestion=pQuestion->m_pNext) { - ((Sequence_Count == sequence) - ? ++msgHeader.m_u16QDCount - : (bResult = _writeMDNSQuestion(*pQuestion, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSQuestion FAILED!\n"));); - } - - // Answers and authorative answers -#ifdef MDNS_IP4_SUPPORT - if ((bResult) && - (p_rSendParameter.m_u8HostReplyMask & ContentFlag_A)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_A(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_A(A) FAILED!\n"));); - } - if ((bResult) && - (p_rSendParameter.m_u8HostReplyMask & ContentFlag_PTR_IP4)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_PTR_IP4(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_IP4 FAILED!\n"));); - } -#endif -#ifdef MDNS_IP6_SUPPORT - if ((bResult) && - (p_rSendParameter.m_u8HostReplyMask & ContentFlag_AAAA)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_AAAA(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_AAAA(A) FAILED!\n"));); - } - if ((bResult) && - (p_rSendParameter.m_u8HostReplyMask & ContentFlag_PTR_IP6)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_PTR_IP6(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_IP6 FAILED!\n"));); - } -#endif - - for (stcMDNSService* pService=m_pServices; ((bResult) && (pService)); pService=pService->m_pNext) { - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_PTR_TYPE)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_PTR_TYPE(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_TYPE FAILED!\n"));); - } - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_PTR_NAME)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_PTR_NAME(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_NAME FAILED!\n"));); - } - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_SRV)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_SRV(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_SRV(A) FAILED!\n"));); - } - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_TXT)) { - ((Sequence_Count == sequence) - ? ++ru16Answers - : (bResult = _writeMDNSAnswer_TXT(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_TXT(A) FAILED!\n"));); - } - } // for services - - // Additional answers -#ifdef MDNS_IP4_SUPPORT - bool bNeedsAdditionalAnswerA = false; -#endif -#ifdef MDNS_IP6_SUPPORT - bool bNeedsAdditionalAnswerAAAA = false; -#endif - for (stcMDNSService* pService=m_pServices; ((bResult) && (pService)); pService=pService->m_pNext) { - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_PTR_NAME) && // If PTR_NAME is requested, AND - (!(pService->m_u8ReplyMask & ContentFlag_SRV))) { // NOT SRV -> add SRV as additional answer - ((Sequence_Count == sequence) - ? ++msgHeader.m_u16ARCount - : (bResult = _writeMDNSAnswer_SRV(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_SRV(B) FAILED!\n"));); - } - if ((bResult) && - (pService->m_u8ReplyMask & ContentFlag_PTR_NAME) && // If PTR_NAME is requested, AND - (!(pService->m_u8ReplyMask & ContentFlag_TXT))) { // NOT TXT -> add TXT as additional answer - ((Sequence_Count == sequence) - ? ++msgHeader.m_u16ARCount - : (bResult = _writeMDNSAnswer_TXT(*pService, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_TXT(B) FAILED!\n"));); - } - if ((pService->m_u8ReplyMask & (ContentFlag_PTR_NAME | ContentFlag_SRV)) || // If service instance name or SRV OR - (p_rSendParameter.m_u8HostReplyMask & (ContentFlag_A | ContentFlag_AAAA))) { // any host IP address is requested -#ifdef MDNS_IP4_SUPPORT - if ((bResult) && - (!(p_rSendParameter.m_u8HostReplyMask & ContentFlag_A))) { // Add IP4 address - bNeedsAdditionalAnswerA = true; - } -#endif -#ifdef MDNS_IP6_SUPPORT - if ((bResult) && - (!(p_rSendParameter.m_u8HostReplyMask & ContentFlag_AAAA))) { // Add IP6 address - bNeedsAdditionalAnswerAAAA = true; - } -#endif - } - } // for services - - // Answer A needed? -#ifdef MDNS_IP4_SUPPORT - if ((bResult) && - (bNeedsAdditionalAnswerA)) { - ((Sequence_Count == sequence) - ? ++msgHeader.m_u16ARCount - : (bResult = _writeMDNSAnswer_A(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_A(B) FAILED!\n"));); - } -#endif -#ifdef MDNS_IP6_SUPPORT - // Answer AAAA needed? - if ((bResult) && - (bNeedsAdditionalAnswerAAAA)) { - ((Sequence_Count == sequence) - ? ++msgHeader.m_u16ARCount - : (bResult = _writeMDNSAnswer_AAAA(p_IPAddress, p_rSendParameter))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_AAAA(B) FAILED!\n"));); - } -#endif - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: Loop %i FAILED!\n"), sequence);); - } // for sequence - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_sendMDNSServiceQuery - * - * Creates and sends a PTR query for the given service domain. - * - */ -bool MDNSResponder::_sendMDNSServiceQuery(const MDNSResponder::stcMDNSServiceQuery& p_ServiceQuery) { - - return _sendMDNSQuery(p_ServiceQuery.m_ServiceTypeDomain, DNS_RRTYPE_PTR); -} - -/* - * MDNSResponder::_sendMDNSQuery - * - * Creates and sends a query for the given domain and query type. - * - */ -bool MDNSResponder::_sendMDNSQuery(const MDNSResponder::stcMDNS_RRDomain& p_QueryDomain, - uint16_t p_u16QueryType, - stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers /*= 0*/) { - - bool bResult = false; - - stcMDNSSendParameter sendParameter; - if (0 != ((sendParameter.m_pQuestions = new stcMDNS_RRQuestion))) { - sendParameter.m_pQuestions->m_Header.m_Domain = p_QueryDomain; - - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = p_u16QueryType; - // It seems, that some mDNS implementations don't support 'unicast response' questions... - sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (/*0x8000 |*/ DNS_RRCLASS_IN); // /*Unicast &*/ INternet - - // TODO: Add knwon answer to the query - (void)p_pKnownAnswers; - - bResult = _sendMDNSMessage(sendParameter); - } // else: FAILED to alloc question - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSQuery: FAILED to alloc question!\n"));); - return bResult; -} - -/** - * HELPERS - */ - -/** - * RESOURCE RECORDS - */ - -/* - * MDNSResponder::_readRRQuestion - * - * Reads a question (eg. MyESP._http._tcp.local ANY IN) from the UPD input buffer. - * - */ -bool MDNSResponder::_readRRQuestion(MDNSResponder::stcMDNS_RRQuestion& p_rRRQuestion) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion\n"));); - - bool bResult = false; - - if ((bResult = _readRRHeader(p_rRRQuestion.m_Header))) { - // Extract unicast flag from class field - p_rRRQuestion.m_bUnicast = (p_rRRQuestion.m_Header.m_Attributes.m_u16Class & 0x8000); - p_rRRQuestion.m_Header.m_Attributes.m_u16Class &= (~0x8000); - - DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion ")); - _printRRDomain(p_rRRQuestion.m_Header.m_Domain); - DEBUG_OUTPUT.printf_P(PSTR(" Type:0x%04X Class:0x%04X %s\n"), (unsigned)p_rRRQuestion.m_Header.m_Attributes.m_u16Type, (unsigned)p_rRRQuestion.m_Header.m_Attributes.m_u16Class, (p_rRRQuestion.m_bUnicast ? "Unicast" : "Multicast")); - ); - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_readRRAnswer - * - * Reads an answer (eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local) - * from the UDP input buffer. - * After reading the domain and type info, the further processing of the answer - * is transferred the answer specific reading functions. - * Unknown answer types are processed by the generic answer reader (to remove them - * from the input buffer). - * - */ -bool MDNSResponder::_readRRAnswer(MDNSResponder::stcMDNS_RRAnswer*& p_rpRRAnswer) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer\n"));); - - bool bResult = false; - - stcMDNS_RRHeader header; - uint32_t u32TTL; - uint16_t u16RDLength; - if ((_readRRHeader(header)) && - (_udpRead32(u32TTL)) && - (_udpRead16(u16RDLength))) { - - /*DEBUG_EX_INFO( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: Reading 0x%04X answer (class:0x%04X, TTL:%u, RDLength:%u) for "), header.m_Attributes.m_u16Type, header.m_Attributes.m_u16Class, u32TTL, u16RDLength); - _printRRDomain(header.m_Domain); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - );*/ - - switch (header.m_Attributes.m_u16Type & (~0x8000)) { // Topmost bit might carry 'cache flush' flag -#ifdef MDNS_IP4_SUPPORT - case DNS_RRTYPE_A: - p_rpRRAnswer = new stcMDNS_RRAnswerA(header, u32TTL); - bResult = _readRRAnswerA(*(stcMDNS_RRAnswerA*&)p_rpRRAnswer, u16RDLength); - break; -#endif - case DNS_RRTYPE_PTR: - p_rpRRAnswer = new stcMDNS_RRAnswerPTR(header, u32TTL); - bResult = _readRRAnswerPTR(*(stcMDNS_RRAnswerPTR*&)p_rpRRAnswer, u16RDLength); - break; - case DNS_RRTYPE_TXT: - p_rpRRAnswer = new stcMDNS_RRAnswerTXT(header, u32TTL); - bResult = _readRRAnswerTXT(*(stcMDNS_RRAnswerTXT*&)p_rpRRAnswer, u16RDLength); - break; -#ifdef MDNS_IP6_SUPPORT - case DNS_RRTYPE_AAAA: - p_rpRRAnswer = new stcMDNS_RRAnswerAAAA(header, u32TTL); - bResult = _readRRAnswerAAAA(*(stcMDNS_RRAnswerAAAA*&)p_rpRRAnswer, u16RDLength); - break; -#endif - case DNS_RRTYPE_SRV: - p_rpRRAnswer = new stcMDNS_RRAnswerSRV(header, u32TTL); - bResult = _readRRAnswerSRV(*(stcMDNS_RRAnswerSRV*&)p_rpRRAnswer, u16RDLength); - break; - default: - p_rpRRAnswer = new stcMDNS_RRAnswerGeneric(header, u32TTL); - bResult = _readRRAnswerGeneric(*(stcMDNS_RRAnswerGeneric*&)p_rpRRAnswer, u16RDLength); - break; - } - DEBUG_EX_INFO( - if ((bResult) && - (p_rpRRAnswer)) { - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: ")); - _printRRDomain(p_rpRRAnswer->m_Header.m_Domain); - DEBUG_OUTPUT.printf_P(PSTR(" Type:0x%04X Class:0x%04X TTL:%u, RDLength:%u "), p_rpRRAnswer->m_Header.m_Attributes.m_u16Type, p_rpRRAnswer->m_Header.m_Attributes.m_u16Class, p_rpRRAnswer->m_u32TTL, u16RDLength); - switch (header.m_Attributes.m_u16Type & (~0x8000)) { // Topmost bit might carry 'cache flush' flag -#ifdef MDNS_IP4_SUPPORT - case DNS_RRTYPE_A: - DEBUG_OUTPUT.printf_P(PSTR("A IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); - break; -#endif - case DNS_RRTYPE_PTR: - DEBUG_OUTPUT.printf_P(PSTR("PTR ")); - _printRRDomain(((stcMDNS_RRAnswerPTR*&)p_rpRRAnswer)->m_PTRDomain); - break; - case DNS_RRTYPE_TXT: { - size_t stTxtLength = ((stcMDNS_RRAnswerTXT*&)p_rpRRAnswer)->m_Txts.c_strLength(); - char* pTxts = new char[stTxtLength]; - if (pTxts) { - ((stcMDNS_RRAnswerTXT*&)p_rpRRAnswer)->m_Txts.c_str(pTxts); - DEBUG_OUTPUT.printf_P(PSTR("TXT(%u) %s"), stTxtLength, pTxts); - delete[] pTxts; - } - break; - } -#ifdef MDNS_IP6_SUPPORT - case DNS_RRTYPE_AAAA: - DEBUG_OUTPUT.printf_P(PSTR("AAAA IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); - break; -#endif - case DNS_RRTYPE_SRV: - DEBUG_OUTPUT.printf_P(PSTR("SRV Port:%u "), ((stcMDNS_RRAnswerSRV*&)p_rpRRAnswer)->m_u16Port); - _printRRDomain(((stcMDNS_RRAnswerSRV*&)p_rpRRAnswer)->m_SRVDomain); - break; - default: - DEBUG_OUTPUT.printf_P(PSTR("generic ")); - break; - } - DEBUG_OUTPUT.printf_P(PSTR("\n")); - } - else { - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: FAILED to read specific answer of type 0x%04X!\n"), p_rpRRAnswer->m_Header.m_Attributes.m_u16Type); - } - ); // DEBUG_EX_INFO - } - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: FAILED!\n"));); - return bResult; -} - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::_readRRAnswerA - */ - bool MDNSResponder::_readRRAnswerA(MDNSResponder::stcMDNS_RRAnswerA& p_rRRAnswerA, - uint16_t p_u16RDLength) { - - uint32_t u32IP4Address; - bool bResult = ((MDNS_IP4_SIZE == p_u16RDLength) && - (_udpReadBuffer((unsigned char*)&u32IP4Address, MDNS_IP4_SIZE)) && - ((p_rRRAnswerA.m_IPAddress = IPAddress(u32IP4Address)))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerA: FAILED!\n"));); - return bResult; - } -#endif - -/* - * MDNSResponder::_readRRAnswerPTR - */ -bool MDNSResponder::_readRRAnswerPTR(MDNSResponder::stcMDNS_RRAnswerPTR& p_rRRAnswerPTR, - uint16_t p_u16RDLength) { - - bool bResult = ((p_u16RDLength) && - (_readRRDomain(p_rRRAnswerPTR.m_PTRDomain))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerPTR: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_readRRAnswerTXT - * - * Read TXT items from a buffer like 4c#=15ff=20 - */ -bool MDNSResponder::_readRRAnswerTXT(MDNSResponder::stcMDNS_RRAnswerTXT& p_rRRAnswerTXT, - uint16_t p_u16RDLength) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: RDLength:%u\n"), p_u16RDLength);); - bool bResult = true; - - p_rRRAnswerTXT.clear(); - if (p_u16RDLength) { - bResult = false; - - unsigned char* pucBuffer = new unsigned char[p_u16RDLength]; - if (pucBuffer) { - if (_udpReadBuffer(pucBuffer, p_u16RDLength)) { - bResult = true; - - const unsigned char* pucCursor = pucBuffer; - while ((pucCursor < (pucBuffer + p_u16RDLength)) && - (bResult)) { - bResult = false; - - stcMDNSServiceTxt* pTxt = 0; - unsigned char ucLength = *pucCursor++; // Length of the next txt item - if (ucLength) { - DEBUG_EX_INFO( - static char sacBuffer[64]; *sacBuffer = 0; - uint8_t u8MaxLength = ((ucLength > (sizeof(sacBuffer) - 1)) ? (sizeof(sacBuffer) - 1) : ucLength); - os_strncpy(sacBuffer, (const char*)pucCursor, u8MaxLength); sacBuffer[u8MaxLength] = 0; - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: Item(%u): %s\n"), ucLength, sacBuffer); - ); - - unsigned char* pucEqualSign = (unsigned char*)os_strchr((const char*)pucCursor, '='); // Position of the '=' sign - unsigned char ucKeyLength; - if ((pucEqualSign) && - ((ucKeyLength = (pucEqualSign - pucCursor)))) { - unsigned char ucValueLength = (ucLength - (pucEqualSign - pucCursor + 1)); - bResult = (((pTxt = new stcMDNSServiceTxt)) && - (pTxt->setKey((const char*)pucCursor, ucKeyLength)) && - (pTxt->setValue((const char*)(pucEqualSign + 1), ucValueLength))); - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: INVALID TXT format (No '=')!\n"));); - } - pucCursor += ucLength; - } - else { // no/zero length TXT - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: TXT answer contains no items.\n"));); - bResult = true; - } - - if ((bResult) && - (pTxt)) { // Everythings fine so far - // Link TXT item to answer TXTs - pTxt->m_pNext = p_rRRAnswerTXT.m_Txts.m_pTxts; - p_rRRAnswerTXT.m_Txts.m_pTxts = pTxt; - } - else { // At least no TXT (migth be OK, if length was 0) OR an error - if (!bResult) { - DEBUG_EX_ERR( - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to read TXT item!\n")); - DEBUG_OUTPUT.printf_P(PSTR("RData dump:\n")); - _udpDump((m_pUDPContext->tell() - p_u16RDLength), p_u16RDLength); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - ); - } - if (pTxt) { - delete pTxt; - pTxt = 0; - } - p_rRRAnswerTXT.clear(); - } - } // while - - DEBUG_EX_ERR( - if (!bResult) { // Some failure - DEBUG_OUTPUT.printf_P(PSTR("RData dump:\n")); - _udpDump((m_pUDPContext->tell() - p_u16RDLength), p_u16RDLength); - DEBUG_OUTPUT.printf_P(PSTR("\n")); - } - ); - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to read TXT content!\n"));); - } - // Clean up - delete[] pucBuffer; - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to alloc buffer for TXT content!\n"));); - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: WARNING! No content!\n"));); - } - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED!\n"));); - return bResult; -} - -#ifdef MDNS_IP6_SUPPORT - bool MDNSResponder::_readRRAnswerAAAA(MDNSResponder::stcMDNS_RRAnswerAAAA& p_rRRAnswerAAAA, - uint16_t p_u16RDLength) { - bool bResult = false; - // TODO: Implement - return bResult; - } -#endif - -/* - * MDNSResponder::_readRRAnswerSRV - */ -bool MDNSResponder::_readRRAnswerSRV(MDNSResponder::stcMDNS_RRAnswerSRV& p_rRRAnswerSRV, - uint16_t p_u16RDLength) { - - bool bResult = (((3 * sizeof(uint16_t)) < p_u16RDLength) && - (_udpRead16(p_rRRAnswerSRV.m_u16Priority)) && - (_udpRead16(p_rRRAnswerSRV.m_u16Weight)) && - (_udpRead16(p_rRRAnswerSRV.m_u16Port)) && - (_readRRDomain(p_rRRAnswerSRV.m_SRVDomain))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerSRV: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_readRRAnswerGeneric - */ -bool MDNSResponder::_readRRAnswerGeneric(MDNSResponder::stcMDNS_RRAnswerGeneric& p_rRRAnswerGeneric, - uint16_t p_u16RDLength) { - bool bResult = (0 == p_u16RDLength); - - p_rRRAnswerGeneric.clear(); - if (((p_rRRAnswerGeneric.m_u16RDLength = p_u16RDLength)) && - ((p_rRRAnswerGeneric.m_pu8RDData = new unsigned char[p_rRRAnswerGeneric.m_u16RDLength]))) { - - bResult = _udpReadBuffer(p_rRRAnswerGeneric.m_pu8RDData, p_rRRAnswerGeneric.m_u16RDLength); - } - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerGeneric: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_readRRHeader - */ -bool MDNSResponder::_readRRHeader(MDNSResponder::stcMDNS_RRHeader& p_rRRHeader) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRHeader\n"));); - - bool bResult = ((_readRRDomain(p_rRRHeader.m_Domain)) && - (_readRRAttributes(p_rRRHeader.m_Attributes))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRHeader: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_readRRDomain - * - * Reads a (maybe multilevel compressed) domain from the UDP input buffer. - * - */ -bool MDNSResponder::_readRRDomain(MDNSResponder::stcMDNS_RRDomain& p_rRRDomain) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain\n"));); - - bool bResult = ((p_rRRDomain.clear()) && - (_readRRDomain_Loop(p_rRRDomain, 0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_readRRDomain_Loop - * - * Reads a domain from the UDP input buffer. For every compression level, the functions - * calls itself recursively. To avoid endless recursion because of malformed MDNS records, - * the maximum recursion depth is set by MDNS_DOMAIN_MAX_REDIRCTION. - * - */ -bool MDNSResponder::_readRRDomain_Loop(MDNSResponder::stcMDNS_RRDomain& p_rRRDomain, - uint8_t p_u8Depth) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u)\n"), p_u8Depth);); - - bool bResult = false; - - if (MDNS_DOMAIN_MAX_REDIRCTION >= p_u8Depth) { - bResult = true; - - uint8_t u8Len = 0; - do { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Offset:%u p0:%02x\n"), p_u8Depth, m_pUDPContext->tell(), m_pUDPContext->peek());); - _udpRead8(u8Len); - - if (u8Len & MDNS_DOMAIN_COMPRESS_MARK) { - // Compressed label(s) - uint16_t u16Offset = ((u8Len & ~MDNS_DOMAIN_COMPRESS_MARK) << 8); // Implicit BE to LE conversion! - _udpRead8(u8Len); - u16Offset |= u8Len; - - if (m_pUDPContext->isValidOffset(u16Offset)) { - size_t stCurrentPosition = m_pUDPContext->tell(); // Prepare return from recursion - - //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Redirecting from %u to %u!\n"), p_u8Depth, stCurrentPosition, u16Offset);); - m_pUDPContext->seek(u16Offset); - if (_readRRDomain_Loop(p_rRRDomain, p_u8Depth + 1)) { // Do recursion - //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Succeeded to read redirected label! Returning to %u\n"), p_u8Depth, stCurrentPosition);); - m_pUDPContext->seek(stCurrentPosition); // Restore after recursion - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): FAILED to read redirected label!\n"), p_u8Depth);); - bResult = false; - } - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): INVALID offset in redirection!\n"), p_u8Depth);); - bResult = false; - } - break; - } - else { - // Normal (uncompressed) label (maybe '\0' only) - if (MDNS_DOMAIN_MAXLENGTH > (p_rRRDomain.m_u16NameLength + u8Len)) { - // Add length byte - p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength] = u8Len; - ++(p_rRRDomain.m_u16NameLength); - if (u8Len) { // Add name - if ((bResult = _udpReadBuffer((unsigned char*)&(p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength]), u8Len))) { - /*DEBUG_EX_INFO( - p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength + u8Len] = 0; // Closing '\0' for printing - DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Domain label (%u): %s\n"), p_u8Depth, (unsigned)(p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength - 1]), &(p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength])); - );*/ - - p_rRRDomain.m_u16NameLength += u8Len; - } - } - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(2) offset:%u p0:%x\n"), m_pUDPContext->tell(), m_pUDPContext->peek());); - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): ERROR! Domain name too long (%u + %u)!\n"), p_u8Depth, p_rRRDomain.m_u16NameLength, u8Len);); - bResult = false; - break; - } - } - } while ((bResult) && - (0 != u8Len)); - } - else { - DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): ERROR! Too many redirections!\n"), p_u8Depth);); - } - return bResult; -} - -/* - * MDNSResponder::_readRRAttributes - */ -bool MDNSResponder::_readRRAttributes(MDNSResponder::stcMDNS_RRAttributes& p_rRRAttributes) { - //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAttributes\n"));); - - bool bResult = ((_udpRead16(p_rRRAttributes.m_u16Type)) && - (_udpRead16(p_rRRAttributes.m_u16Class))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAttributes: FAILED!\n"));); - return bResult; -} - - -/* - * DOMAIN NAMES - */ - -/* - * MDNSResponder::_buildDomainForHost - * - * Builds a MDNS host domain (eg. esp8266.local) for the given hostname. - * - */ -bool MDNSResponder::_buildDomainForHost(const char* p_pcHostname, - MDNSResponder::stcMDNS_RRDomain& p_rHostDomain) const { - - p_rHostDomain.clear(); - bool bResult = ((p_pcHostname) && - (*p_pcHostname) && - (p_rHostDomain.addLabel(p_pcHostname)) && - (p_rHostDomain.addLabel(scpcLocal)) && - (p_rHostDomain.addLabel(0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForHost: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_buildDomainForDNSSD - * - * Builds the '_services._dns-sd._udp.local' domain. - * Used while detecting generic service enum question (DNS-SD) and answering these questions. - * - */ -bool MDNSResponder::_buildDomainForDNSSD(MDNSResponder::stcMDNS_RRDomain& p_rDNSSDDomain) const { - - p_rDNSSDDomain.clear(); - bool bResult = ((p_rDNSSDDomain.addLabel(scpcServices, true)) && - (p_rDNSSDDomain.addLabel(scpcDNSSD, true)) && - (p_rDNSSDDomain.addLabel(scpcUDP, true)) && - (p_rDNSSDDomain.addLabel(scpcLocal)) && - (p_rDNSSDDomain.addLabel(0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForDNSSD: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_buildDomainForService - * - * Builds the domain for the given service (eg. _http._tcp.local or - * MyESP._http._tcp.local (if p_bIncludeName is set)). - * - */ -bool MDNSResponder::_buildDomainForService(const MDNSResponder::stcMDNSService& p_Service, - bool p_bIncludeName, - MDNSResponder::stcMDNS_RRDomain& p_rServiceDomain) const { - - p_rServiceDomain.clear(); - bool bResult = (((!p_bIncludeName) || - (p_rServiceDomain.addLabel(p_Service.m_pcName))) && - (p_rServiceDomain.addLabel(p_Service.m_pcService, true)) && - (p_rServiceDomain.addLabel(p_Service.m_pcProtocol, true)) && - (p_rServiceDomain.addLabel(scpcLocal)) && - (p_rServiceDomain.addLabel(0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForService: FAILED!\n"));); - return bResult; -} - -/* - * MDNSResponder::_buildDomainForService - * - * Builds the domain for the given service properties (eg. _http._tcp.local). - * The usual prepended '_' are added, if missing in the input strings. - * - */ -bool MDNSResponder::_buildDomainForService(const char* p_pcService, - const char* p_pcProtocol, - MDNSResponder::stcMDNS_RRDomain& p_rServiceDomain) const { - - p_rServiceDomain.clear(); - bool bResult = ((p_pcService) && - (p_pcProtocol) && - (p_rServiceDomain.addLabel(p_pcService, ('_' != *p_pcService))) && - (p_rServiceDomain.addLabel(p_pcProtocol, ('_' != *p_pcProtocol))) && - (p_rServiceDomain.addLabel(scpcLocal)) && - (p_rServiceDomain.addLabel(0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForService: FAILED for (%s.%s)!\n"), (p_pcService ?: "-"), (p_pcProtocol ?: "-"));); - return bResult; -} - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::_buildDomainForReverseIP4 - * - * The IP4 address is stringized by printing the four address bytes into a char buffer in reverse order - * and adding 'in-addr.arpa' (eg. 012.789.456.123.in-addr.arpa). - * Used while detecting reverse IP4 questions and answering these - */ - bool MDNSResponder::_buildDomainForReverseIP4(IPAddress p_IP4Address, - MDNSResponder::stcMDNS_RRDomain& p_rReverseIP4Domain) const { - - bool bResult = true; - - p_rReverseIP4Domain.clear(); - - char acBuffer[32]; - for (int i=MDNS_IP4_SIZE; ((bResult) && (i>=1)); --i) { - itoa(p_IP4Address[i - 1], acBuffer, 10); - bResult = p_rReverseIP4Domain.addLabel(acBuffer); - } - bResult = ((bResult) && - (p_rReverseIP4Domain.addLabel(scpcReverseIP4Domain)) && - (p_rReverseIP4Domain.addLabel(scpcReverseTopDomain)) && - (p_rReverseIP4Domain.addLabel(0))); - DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForReverseIP4: FAILED!\n"));); - return bResult; - } -#endif - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::_buildDomainForReverseIP6 - * - * Used while detecting reverse IP6 questions and answering these - */ - bool MDNSResponder::_buildDomainForReverseIP6(IPAddress p_IP4Address, - MDNSResponder::stcMDNS_RRDomain& p_rReverseIP6Domain) const { - // TODO: Implement - return false; - } -#endif - - -/* - * UDP - */ - -/* - * MDNSResponder::_udpReadBuffer - */ -bool MDNSResponder::_udpReadBuffer(unsigned char* p_pBuffer, - size_t p_stLength) { - - bool bResult = ((m_pUDPContext) && - (true/*m_pUDPContext->getSize() > p_stLength*/) && - (p_pBuffer) && - (p_stLength) && - ((p_stLength == m_pUDPContext->read((char*)p_pBuffer, p_stLength)))); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _udpReadBuffer: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_udpRead8 - */ -bool MDNSResponder::_udpRead8(uint8_t& p_ru8Value) { - - return _udpReadBuffer((unsigned char*)&p_ru8Value, sizeof(p_ru8Value)); -} - -/* - * MDNSResponder::_udpRead16 - */ -bool MDNSResponder::_udpRead16(uint16_t& p_ru16Value) { - - bool bResult = false; - - if (_udpReadBuffer((unsigned char*)&p_ru16Value, sizeof(p_ru16Value))) { - p_ru16Value = lwip_ntohs(p_ru16Value); - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::_udpRead32 - */ -bool MDNSResponder::_udpRead32(uint32_t& p_ru32Value) { - - bool bResult = false; - - if (_udpReadBuffer((unsigned char*)&p_ru32Value, sizeof(p_ru32Value))) { - p_ru32Value = lwip_ntohl(p_ru32Value); - bResult = true; - } - return bResult; -} - -/* - * MDNSResponder::_udpAppendBuffer - */ -bool MDNSResponder::_udpAppendBuffer(const unsigned char* p_pcBuffer, - size_t p_stLength) { - - bool bResult = ((m_pUDPContext) && - (p_pcBuffer) && - (p_stLength) && - (p_stLength == m_pUDPContext->append((const char*)p_pcBuffer, p_stLength))); - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _udpAppendBuffer: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_udpAppend8 - */ -bool MDNSResponder::_udpAppend8(uint8_t p_u8Value) { - - return (_udpAppendBuffer((unsigned char*)&p_u8Value, sizeof(p_u8Value))); -} - -/* - * MDNSResponder::_udpAppend16 - */ -bool MDNSResponder::_udpAppend16(uint16_t p_u16Value) { - - p_u16Value = lwip_htons(p_u16Value); - return (_udpAppendBuffer((unsigned char*)&p_u16Value, sizeof(p_u16Value))); -} - -/* - * MDNSResponder::_udpAppend32 - */ -bool MDNSResponder::_udpAppend32(uint32_t p_u32Value) { - - p_u32Value = lwip_htonl(p_u32Value); - return (_udpAppendBuffer((unsigned char*)&p_u32Value, sizeof(p_u32Value))); -} - -#ifdef DEBUG_ESP_MDNS_RESPONDER - /* - * MDNSResponder::_udpDump - */ - bool MDNSResponder::_udpDump(bool p_bMovePointer /*= false*/) { - - const uint8_t cu8BytesPerLine = 16; - - uint32_t u32StartPosition = m_pUDPContext->tell(); - DEBUG_OUTPUT.println("UDP Context Dump:"); - uint32_t u32Counter = 0; - uint8_t u8Byte = 0; - - while (_udpRead8(u8Byte)) { - DEBUG_OUTPUT.printf_P(PSTR("%02x %s"), u8Byte, ((++u32Counter % cu8BytesPerLine) ? "" : "\n")); - } - DEBUG_OUTPUT.printf_P(PSTR("%sDone: %u bytes\n"), (((u32Counter) && (u32Counter % cu8BytesPerLine)) ? "\n" : ""), u32Counter); - - if (!p_bMovePointer) { // Restore - m_pUDPContext->seek(u32StartPosition); - } - return true; - } - - /* - * MDNSResponder::_udpDump - */ - bool MDNSResponder::_udpDump(unsigned p_uOffset, - unsigned p_uLength) { - - if ((m_pUDPContext) && - (m_pUDPContext->isValidOffset(p_uOffset))) { - unsigned uCurrentPosition = m_pUDPContext->tell(); // Remember start position - - m_pUDPContext->seek(p_uOffset); - uint8_t u8Byte; - for (unsigned u=0; ((useek(uCurrentPosition); - } - return true; - } -#endif - - -/** - * READ/WRITE MDNS STRUCTS - */ - -/* - * MDNSResponder::_readMDNSMsgHeader - * - * Read a MDNS header from the UDP input buffer. - * | 8 | 8 | 8 | 8 | - * 00| Identifier | Flags & Codes | - * 01| Question count | Answer count | - * 02| NS answer count | Ad answer count | - * - * All 16-bit and 32-bit elements need to be translated form network coding to host coding (done in _udpRead16 and _udpRead32) - * In addition, bitfield memory order is undefined in C standard (GCC doesn't order them in the coded direction...), so they - * need some mapping here - */ -bool MDNSResponder::_readMDNSMsgHeader(MDNSResponder::stcMDNS_MsgHeader& p_rMsgHeader) { - - bool bResult = false; - - uint8_t u8B1; - uint8_t u8B2; - if ((_udpRead16(p_rMsgHeader.m_u16ID)) && - (_udpRead8(u8B1))&& - (_udpRead8(u8B2)) && - (_udpRead16(p_rMsgHeader.m_u16QDCount)) && - (_udpRead16(p_rMsgHeader.m_u16ANCount)) && - (_udpRead16(p_rMsgHeader.m_u16NSCount)) && - (_udpRead16(p_rMsgHeader.m_u16ARCount))) { - - p_rMsgHeader.m_1bQR = (u8B1 & 0x80); // Query/Responde flag - p_rMsgHeader.m_4bOpcode = (u8B1 & 0x78); // Operation code (0: Standard query, others ignored) - p_rMsgHeader.m_1bAA = (u8B1 & 0x04); // Authorative answer - p_rMsgHeader.m_1bTC = (u8B1 & 0x02); // Truncation flag - p_rMsgHeader.m_1bRD = (u8B1 & 0x01); // Recursion desired - - p_rMsgHeader.m_1bRA = (u8B2 & 0x80); // Recursion available - p_rMsgHeader.m_3bZ = (u8B2 & 0x70); // Zero - p_rMsgHeader.m_4bRCode = (u8B2 & 0x0F); // Response code - - /*DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readMDNSMsgHeader: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), - (unsigned)p_rMsgHeader.m_u16ID, - (unsigned)p_rMsgHeader.m_1bQR, (unsigned)p_rMsgHeader.m_4bOpcode, (unsigned)p_rMsgHeader.m_1bAA, (unsigned)p_rMsgHeader.m_1bTC, (unsigned)p_rMsgHeader.m_1bRD, - (unsigned)p_rMsgHeader.m_1bRA, (unsigned)p_rMsgHeader.m_4bRCode, - (unsigned)p_rMsgHeader.m_u16QDCount, - (unsigned)p_rMsgHeader.m_u16ANCount, - (unsigned)p_rMsgHeader.m_u16NSCount, - (unsigned)p_rMsgHeader.m_u16ARCount););*/ - bResult = true; - } - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readMDNSMsgHeader: FAILED!\n")); } ); - return bResult; -} - -/* - * MDNSResponder::_write8 - */ -bool MDNSResponder::_write8(uint8_t p_u8Value, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - return ((_udpAppend8(p_u8Value)) && - (p_rSendParameter.shiftOffset(sizeof(p_u8Value)))); -} - -/* - * MDNSResponder::_write16 - */ -bool MDNSResponder::_write16(uint16_t p_u16Value, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - return ((_udpAppend16(p_u16Value)) && - (p_rSendParameter.shiftOffset(sizeof(p_u16Value)))); -} - -/* - * MDNSResponder::_write32 - */ -bool MDNSResponder::_write32(uint32_t p_u32Value, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - return ((_udpAppend32(p_u32Value)) && - (p_rSendParameter.shiftOffset(sizeof(p_u32Value)))); -} - -/* - * MDNSResponder::_writeMDNSMsgHeader - * - * Write MDNS header to the UDP output buffer. - * - * All 16-bit and 32-bit elements need to be translated form host coding to network coding (done in _udpAppend16 and _udpAppend32) - * In addition, bitfield memory order is undefined in C standard (GCC doesn't order them in the coded direction...), so they - * need some mapping here - */ -bool MDNSResponder::_writeMDNSMsgHeader(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - /*DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSMsgHeader: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), - (unsigned)p_MsgHeader.m_u16ID, - (unsigned)p_MsgHeader.m_1bQR, (unsigned)p_MsgHeader.m_4bOpcode, (unsigned)p_MsgHeader.m_1bAA, (unsigned)p_MsgHeader.m_1bTC, (unsigned)p_MsgHeader.m_1bRD, - (unsigned)p_MsgHeader.m_1bRA, (unsigned)p_MsgHeader.m_4bRCode, - (unsigned)p_MsgHeader.m_u16QDCount, - (unsigned)p_MsgHeader.m_u16ANCount, - (unsigned)p_MsgHeader.m_u16NSCount, - (unsigned)p_MsgHeader.m_u16ARCount););*/ - - uint8_t u8B1((p_MsgHeader.m_1bQR << 7) | (p_MsgHeader.m_4bOpcode << 3) | (p_MsgHeader.m_1bAA << 2) | (p_MsgHeader.m_1bTC << 1) | (p_MsgHeader.m_1bRD)); - uint8_t u8B2((p_MsgHeader.m_1bRA << 7) | (p_MsgHeader.m_3bZ << 4) | (p_MsgHeader.m_4bRCode)); - bool bResult = ((_write16(p_MsgHeader.m_u16ID, p_rSendParameter)) && - (_write8(u8B1, p_rSendParameter)) && - (_write8(u8B2, p_rSendParameter)) && - (_write16(p_MsgHeader.m_u16QDCount, p_rSendParameter)) && - (_write16(p_MsgHeader.m_u16ANCount, p_rSendParameter)) && - (_write16(p_MsgHeader.m_u16NSCount, p_rSendParameter)) && - (_write16(p_MsgHeader.m_u16ARCount, p_rSendParameter))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSMsgHeader: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_writeRRAttributes - */ -bool MDNSResponder::_writeMDNSRRAttributes(const MDNSResponder::stcMDNS_RRAttributes& p_Attributes, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - bool bResult = ((_write16(p_Attributes.m_u16Type, p_rSendParameter)) && - (_write16(p_Attributes.m_u16Class, p_rSendParameter))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSRRAttributes: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_writeMDNSRRDomain - */ -bool MDNSResponder::_writeMDNSRRDomain(const MDNSResponder::stcMDNS_RRDomain& p_Domain, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - bool bResult = ((_udpAppendBuffer((const unsigned char*)p_Domain.m_acName, p_Domain.m_u16NameLength)) && - (p_rSendParameter.shiftOffset(p_Domain.m_u16NameLength))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSRRDomain: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_writeMDNSHostDomain - * - * Write a host domain to the UDP output buffer. - * If the domain record is part of the answer, the records length is - * prepended (p_bPrependRDLength is set). - * - * A very simple form of name compression is applied here: - * If the domain is written to the UDP output buffer, the write offset is stored - * together with a domain id (the pointer) in a p_rSendParameter substructure (cache). - * If the same domain (pointer) should be written to the UDP output later again, - * the old offset is retrieved from the cache, marked as a compressed domain offset - * and written to the output buffer. - * - */ -bool MDNSResponder::_writeMDNSHostDomain(const char* p_pcHostname, - bool p_bPrependRDLength, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - // The 'skip-compression' version is handled in '_writeMDNSAnswer_SRV' - uint16_t u16CachedDomainOffset = p_rSendParameter.findCachedDomainOffset((const void*)p_pcHostname, false); - - stcMDNS_RRDomain hostDomain; - bool bResult = (u16CachedDomainOffset - // Found cached domain -> mark as compressed domain - ? ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset - ((!p_bPrependRDLength) || - (_write16(2, p_rSendParameter))) && // Length of 'Cxxx' - (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) - (_write8((uint8_t)(u16CachedDomainOffset & 0xFF), p_rSendParameter))) - // No cached domain -> add this domain to cache and write full domain name - : ((_buildDomainForHost(p_pcHostname, hostDomain)) && // eg. esp8266.local - ((!p_bPrependRDLength) || - (_write16(hostDomain.m_u16NameLength, p_rSendParameter))) && // RDLength (if needed) - (p_rSendParameter.addDomainCacheItem((const void*)p_pcHostname, false, p_rSendParameter.m_u16Offset)) && - (_writeMDNSRRDomain(hostDomain, p_rSendParameter)))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSHostDomain: FAILED!\n")); }); - return bResult; - -} - -/* - * MDNSResponder::_writeMDNSServiceDomain - * - * Write a service domain to the UDP output buffer. - * If the domain record is part of the answer, the records length is - * prepended (p_bPrependRDLength is set). - * - * A very simple form of name compression is applied here: see '_writeMDNSHostDomain' - * The cache differentiates of course between service domains which includes - * the instance name (p_bIncludeName is set) and thoose who don't. - * - */ -bool MDNSResponder::_writeMDNSServiceDomain(const MDNSResponder::stcMDNSService& p_Service, - bool p_bIncludeName, - bool p_bPrependRDLength, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - - // The 'skip-compression' version is handled in '_writeMDNSAnswer_SRV' - uint16_t u16CachedDomainOffset = p_rSendParameter.findCachedDomainOffset((const void*)&p_Service, p_bIncludeName); - - stcMDNS_RRDomain serviceDomain; - bool bResult = (u16CachedDomainOffset - // Found cached domain -> mark as compressed domain - ? ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset - ((!p_bPrependRDLength) || - (_write16(2, p_rSendParameter))) && // Lenght of 'Cxxx' - (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) - (_write8((uint8_t)(u16CachedDomainOffset & 0xFF), p_rSendParameter))) - // No cached domain -> add this domain to cache and write full domain name - : ((_buildDomainForService(p_Service, p_bIncludeName, serviceDomain)) && // eg. MyESP._http._tcp.local - ((!p_bPrependRDLength) || - (_write16(serviceDomain.m_u16NameLength, p_rSendParameter))) && // RDLength (if needed) - (p_rSendParameter.addDomainCacheItem((const void*)&p_Service, p_bIncludeName, p_rSendParameter.m_u16Offset)) && - (_writeMDNSRRDomain(serviceDomain, p_rSendParameter)))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSServiceDomain: FAILED!\n")); }); - return bResult; - -} - -/* - * MDNSResponder::_writeMDNSQuestion - * - * Write a MDNS question to the UDP output buffer - * - * QNAME (host/service domain, eg. esp8266.local) - * QTYPE (16bit, eg. ANY) - * QCLASS (16bit, eg. IN) - * - */ -bool MDNSResponder::_writeMDNSQuestion(MDNSResponder::stcMDNS_RRQuestion& p_Question, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSQuestion\n"));); - - bool bResult = ((_writeMDNSRRDomain(p_Question.m_Header.m_Domain, p_rSendParameter)) && - (_writeMDNSRRAttributes(p_Question.m_Header.m_Attributes, p_rSendParameter))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSQuestion: FAILED!\n")); }); - return bResult; - -} - - -#ifdef MDNS_IP4_SUPPORT - /* - * MDNSResponder::_writeMDNSAnswer_A - * - * Write a MDNS A answer to the UDP output buffer. - * - * NAME (var, host/service domain, eg. esp8266.local - * TYPE (16bit, eg. A) - * CLASS (16bit, eg. IN) - * TTL (32bit, eg. 120) - * RDLENGTH (16bit, eg 4) - * RDATA (var, eg. 123.456.789.012) - * - * eg. esp8266.local A 0x8001 120 4 123.456.789.012 - * Ref: http://www.zytrax.com/books/dns/ch8/a.html - */ - bool MDNSResponder::_writeMDNSAnswer_A(IPAddress p_IPAddress, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_A (%s)\n"), p_IPAddress.toString().c_str());); - - stcMDNS_RRAttributes attributes(DNS_RRTYPE_A, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - const unsigned char aucIPAddress[MDNS_IP4_SIZE] = { p_IPAddress[0], p_IPAddress[1], p_IPAddress[2], p_IPAddress[3] }; - bool bResult = ((_writeMDNSHostDomain(m_pcHostname, false, p_rSendParameter)) && - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL - (_write16(MDNS_IP4_SIZE, p_rSendParameter)) && // RDLength - (_udpAppendBuffer(aucIPAddress, MDNS_IP4_SIZE)) && // RData - (p_rSendParameter.shiftOffset(MDNS_IP4_SIZE))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_A: FAILED!\n")); }); - return bResult; - - } - - /* - * MDNSResponder::_writeMDNSAnswer_PTR_IP4 - * - * Write a MDNS reverse IP4 PTR answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * eg. 012.789.456.123.in-addr.arpa PTR 0x8001 120 15 esp8266.local - * Used while answering reverse IP4 questions - */ - bool MDNSResponder::_writeMDNSAnswer_PTR_IP4(IPAddress p_IPAddress, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP4 (%s)\n"), p_IPAddress.toString().c_str());); - - stcMDNS_RRDomain reverseIP4Domain; - stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - stcMDNS_RRDomain hostDomain; - bool bResult = ((_buildDomainForReverseIP4(p_IPAddress, reverseIP4Domain)) && // 012.789.456.123.in-addr.arpa - (_writeMDNSRRDomain(reverseIP4Domain, p_rSendParameter)) && - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL - (_writeMDNSHostDomain(m_pcHostname, true, p_rSendParameter))); // RDLength & RData (host domain, eg. esp8266.local) - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP4: FAILED!\n")); }); - return bResult; - } -#endif - -/* - * MDNSResponder::_writeMDNSAnswer_PTR_TYPE - * - * Write a MDNS PTR answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * PTR all-services -> service type - * eg. _services._dns-sd._udp.local PTR 0x8001 5400 xx _http._tcp.local - * http://www.zytrax.com/books/dns/ch8/ptr.html - */ -bool MDNSResponder::_writeMDNSAnswer_PTR_TYPE(MDNSResponder::stcMDNSService& p_rService, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_TYPE\n"));); - - stcMDNS_RRDomain dnssdDomain; - stcMDNS_RRDomain serviceDomain; - stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, DNS_RRCLASS_IN); // No cache flush! only INternet - bool bResult = ((_buildDomainForDNSSD(dnssdDomain)) && // _services._dns-sd._udp.local - (_writeMDNSRRDomain(dnssdDomain, p_rSendParameter)) && - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL - (_writeMDNSServiceDomain(p_rService, false, true, p_rSendParameter))); // RDLength & RData (service domain, eg. _http._tcp.local) - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_TYPE: FAILED!\n")); }); - return bResult; -} - -/* - * MDNSResponder::_writeMDNSAnswer_PTR_NAME - * - * Write a MDNS PTR answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * PTR service type -> service name - * eg. _http.tcp.local PTR 0x8001 120 xx myESP._http._tcp.local - * http://www.zytrax.com/books/dns/ch8/ptr.html - */ -bool MDNSResponder::_writeMDNSAnswer_PTR_NAME(MDNSResponder::stcMDNSService& p_rService, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_NAME\n"));); - - stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, DNS_RRCLASS_IN); // No cache flush! only INternet - bool bResult = ((_writeMDNSServiceDomain(p_rService, false, false, p_rSendParameter)) && // _http._tcp.local - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL - (_writeMDNSServiceDomain(p_rService, true, true, p_rSendParameter))); // RDLength & RData (service domain, eg. MyESP._http._tcp.local) - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_NAME: FAILED!\n")); }); - return bResult; -} - - -/* - * MDNSResponder::_writeMDNSAnswer_TXT - * - * Write a MDNS TXT answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * The TXT items in the RDATA block are 'length byte encoded': [len]vardata - * - * eg. myESP._http._tcp.local TXT 0x8001 120 4 c#=1 - * http://www.zytrax.com/books/dns/ch8/txt.html - */ -bool MDNSResponder::_writeMDNSAnswer_TXT(MDNSResponder::stcMDNSService& p_rService, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT\n"));); - - bool bResult = false; - - stcMDNS_RRAttributes attributes(DNS_RRTYPE_TXT, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - - if ((_collectServiceTxts(p_rService)) && - (_writeMDNSServiceDomain(p_rService, true, false, p_rSendParameter)) && // MyESP._http._tcp.local - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL - (_write16(p_rService.m_Txts.length(), p_rSendParameter))) { // RDLength - - bResult = true; - // RData Txts - for (stcMDNSServiceTxt* pTxt=p_rService.m_Txts.m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) { - unsigned char ucLengthByte = pTxt->length(); - bResult = ((_udpAppendBuffer((unsigned char*)&ucLengthByte, sizeof(ucLengthByte))) && // Length - (p_rSendParameter.shiftOffset(sizeof(ucLengthByte))) && - ((size_t)os_strlen(pTxt->m_pcKey) == m_pUDPContext->append(pTxt->m_pcKey, os_strlen(pTxt->m_pcKey))) && // Key - (p_rSendParameter.shiftOffset((size_t)os_strlen(pTxt->m_pcKey))) && - (1 == m_pUDPContext->append("=", 1)) && // = - (p_rSendParameter.shiftOffset(1)) && - ((!pTxt->m_pcValue) || - (((size_t)os_strlen(pTxt->m_pcValue) == m_pUDPContext->append(pTxt->m_pcValue, os_strlen(pTxt->m_pcValue))) && // Value - (p_rSendParameter.shiftOffset((size_t)os_strlen(pTxt->m_pcValue)))))); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT: FAILED to write %sTxt %s=%s!\n"), (pTxt->m_bTemp ? "temp. " : ""), (pTxt->m_pcKey ?: "?"), (pTxt->m_pcValue ?: "?")); }); - } - } - _releaseTempServiceTxts(p_rService); - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT: FAILED!\n")); }); - return bResult; -} - -#ifdef MDNS_IP6_SUPPORT - /* - * MDNSResponder::_writeMDNSAnswer_AAAA - * - * Write a MDNS AAAA answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * eg. esp8266.local AAAA 0x8001 120 16 xxxx::xx - * http://www.zytrax.com/books/dns/ch8/aaaa.html - */ - bool MDNSResponder::_writeMDNSAnswer_AAAA(IPAddress p_IPAddress, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_AAAA\n"));); - - stcMDNS_RRAttributes attributes(DNS_RRTYPE_AAAA, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - bool bResult = ((_writeMDNSHostDomain(m_pcHostname, false, p_rSendParameter)) && // esp8266.local - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL - (_write16(MDNS_IP6_SIZE, p_rSendParameter)) && // RDLength - (false /*TODO: IP6 version of: _udpAppendBuffer((uint32_t)p_IPAddress, MDNS_IP4_SIZE)*/)); // RData - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_AAAA: FAILED!\n")); }); - return bResult; - } - - /* - * MDNSResponder::_writeMDNSAnswer_PTR_IP6 - * - * Write a MDNS reverse IP6 PTR answer to the UDP output buffer. - * See: '_writeMDNSAnswer_A' - * - * eg. xxxx::xx.in6.arpa PTR 0x8001 120 15 esp8266.local - * Used while answering reverse IP6 questions - */ - bool MDNSResponder::_writeMDNSAnswer_PTR_IP6(IPAddress p_IPAddress, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP6\n"));); - - stcMDNS_RRDomain reverseIP6Domain; - stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - bool bResult = ((_buildDomainForReverseIP6(p_IPAddress, reverseIP6Domain)) && // xxxx::xx.ip6.arpa - (_writeMDNSRRDomain(reverseIP6Domain, p_rSendParameter)) && - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL - (_writeMDNSHostDomain(m_pcHostname, true, p_rSendParameter))); // RDLength & RData (host domain, eg. esp8266.local) - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP6: FAILED!\n")); }); - return bResult; - } -#endif - -/* - * MDNSResponder::_writeMDNSAnswer_SRV - * - * eg. MyESP._http.tcp.local SRV 0x8001 120 0 0 60068 esp8266.local - * http://www.zytrax.com/books/dns/ch8/srv.html ???? Include instance name ???? - */ -bool MDNSResponder::_writeMDNSAnswer_SRV(MDNSResponder::stcMDNSService& p_rService, - MDNSResponder::stcMDNSSendParameter& p_rSendParameter) { - DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_SRV\n"));); - - uint16_t u16CachedDomainOffset = (p_rSendParameter.m_bLegacyQuery - ? 0 - : p_rSendParameter.findCachedDomainOffset((const void*)m_pcHostname, false)); - - stcMDNS_RRAttributes attributes(DNS_RRTYPE_SRV, - ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet - stcMDNS_RRDomain hostDomain; - bool bResult = ((_writeMDNSServiceDomain(p_rService, true, false, p_rSendParameter)) && // MyESP._http._tcp.local - (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS - (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL - (!u16CachedDomainOffset - // No cache for domain name (or no compression allowed) - ? ((_buildDomainForHost(m_pcHostname, hostDomain)) && - (_write16((sizeof(uint16_t /*Prio*/) + // RDLength - sizeof(uint16_t /*Weight*/) + - sizeof(uint16_t /*Port*/) + - hostDomain.m_u16NameLength), p_rSendParameter)) && // Domain length - (_write16(MDNS_SRV_PRIORITY, p_rSendParameter)) && // Priority - (_write16(MDNS_SRV_WEIGHT, p_rSendParameter)) && // Weight - (_write16(p_rService.m_u16Port, p_rSendParameter)) && // Port - (p_rSendParameter.addDomainCacheItem((const void*)m_pcHostname, false, p_rSendParameter.m_u16Offset)) && - (_writeMDNSRRDomain(hostDomain, p_rSendParameter))) // Host, eg. esp8266.local - // Cache available for domain - : ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset - (_write16((sizeof(uint16_t /*Prio*/) + // RDLength - sizeof(uint16_t /*Weight*/) + - sizeof(uint16_t /*Port*/) + - 2), p_rSendParameter)) && // Length of 'C0xx' - (_write16(MDNS_SRV_PRIORITY, p_rSendParameter)) && // Priority - (_write16(MDNS_SRV_WEIGHT, p_rSendParameter)) && // Weight - (_write16(p_rService.m_u16Port, p_rSendParameter)) && // Port - (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) - (_write8((uint8_t)u16CachedDomainOffset, p_rSendParameter))))); // Offset - - DEBUG_EX_ERR(if (!bResult) { DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_SRV: FAILED!\n")); }); - return bResult; -} - -} // namespace MDNSImplementation - -} // namespace esp8266 - - - - - - +/* + LEAmDNS_Transfer.cpp + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +extern "C" { +#include "user_interface.h" +} + +#include "LEAmDNS_lwIPdefs.h" +#include "LEAmDNS_Priv.h" + + +namespace esp8266 +{ + +/* + LEAmDNS +*/ +namespace MDNSImplementation +{ + +/** + CONST STRINGS +*/ +static const char* scpcLocal = "local"; +static const char* scpcServices = "services"; +static const char* scpcDNSSD = "dns-sd"; +static const char* scpcUDP = "udp"; +//static const char* scpcTCP = "tcp"; + +#ifdef MDNS_IP4_SUPPORT +static const char* scpcReverseIP4Domain = "in-addr"; +#endif +#ifdef MDNS_IP6_SUPPORT +static const char* scpcReverseIP6Domain = "ip6"; +#endif +static const char* scpcReverseTopDomain = "arpa"; + +/** + TRANSFER +*/ + + +/** + SENDING +*/ + +/* + MDNSResponder::_sendMDNSMessage + + Unicast responses are prepared and sent directly to the querier. + Multicast responses or queries are transferred to _sendMDNSMessage_Multicast + + Any reply flags in installed services are removed at the end! + +*/ +bool MDNSResponder::_sendMDNSMessage(MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + bool bResult = true; + + if (p_rSendParameter.m_bResponse && + p_rSendParameter.m_bUnicast) // Unicast response -> Send to querier + { + DEBUG_EX_ERR(if (!m_pUDPContext->getRemoteAddress()) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage: MISSING remote address for response!\n")); + }); + IPAddress ipRemote; + ipRemote = m_pUDPContext->getRemoteAddress(); + bResult = ((_prepareMDNSMessage(p_rSendParameter, _getResponseMulticastInterface())) && + (m_pUDPContext->send(ipRemote, m_pUDPContext->getRemotePort()))); + } + else // Multicast response + { + bResult = _sendMDNSMessage_Multicast(p_rSendParameter); + } + + // Finally clear service reply masks + for (stcMDNSService* pService = m_pServices; pService; pService = pService->m_pNext) + { + pService->m_u8ReplyMask = 0; + } + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_sendMDNSMessage_Multicast + + Fills the UDP output buffer (via _prepareMDNSMessage) and sends the buffer + via the selected WiFi interface (Station or AP) +*/ +bool MDNSResponder::_sendMDNSMessage_Multicast(MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + bool bResult = false; + + IPAddress fromIPAddress; + fromIPAddress = _getResponseMulticastInterface(); + m_pUDPContext->setMulticastInterface(fromIPAddress); + +#ifdef MDNS_IP4_SUPPORT + IPAddress toMulticastAddress(DNS_MQUERY_IPV4_GROUP_INIT); +#endif +#ifdef MDNS_IP6_SUPPORT + //TODO: set multicast address + IPAddress toMulticastAddress(DNS_MQUERY_IPV6_GROUP_INIT); +#endif + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage_Multicast: Will send to '%s'.\n"), toMulticastAddress.toString().c_str());); + bResult = ((_prepareMDNSMessage(p_rSendParameter, fromIPAddress)) && + (m_pUDPContext->send(toMulticastAddress, DNS_MQUERY_PORT))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSMessage_Multicast: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_prepareMDNSMessage + + The MDNS message is composed in a two-step process. + In the first loop 'only' the header informations (mainly number of answers) are collected, + while in the seconds loop, the header and all queries and answers are written to the UDP + output buffer. + +*/ +bool MDNSResponder::_prepareMDNSMessage(MDNSResponder::stcMDNSSendParameter& p_rSendParameter, + IPAddress p_IPAddress) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage\n"));); + bool bResult = true; + + // Prepare header; count answers + stcMDNS_MsgHeader msgHeader(p_rSendParameter.m_u16ID, p_rSendParameter.m_bResponse, 0, p_rSendParameter.m_bAuthorative); + // If this is a response, the answers are anwers, + // else this is a query or probe and the answers go into auth section + uint16_t& ru16Answers = (p_rSendParameter.m_bResponse + ? msgHeader.m_u16ANCount + : msgHeader.m_u16NSCount); + + /** + enuSequence + */ + enum enuSequence + { + Sequence_Count = 0, + Sequence_Send = 1 + }; + + // Two step sequence: 'Count' and 'Send' + for (uint32_t sequence = Sequence_Count; ((bResult) && (sequence <= Sequence_Send)); ++sequence) + { + DEBUG_EX_INFO( + if (Sequence_Send == sequence) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), + (unsigned)msgHeader.m_u16ID, + (unsigned)msgHeader.m_1bQR, (unsigned)msgHeader.m_4bOpcode, (unsigned)msgHeader.m_1bAA, (unsigned)msgHeader.m_1bTC, (unsigned)msgHeader.m_1bRD, + (unsigned)msgHeader.m_1bRA, (unsigned)msgHeader.m_4bRCode, + (unsigned)msgHeader.m_u16QDCount, + (unsigned)msgHeader.m_u16ANCount, + (unsigned)msgHeader.m_u16NSCount, + (unsigned)msgHeader.m_u16ARCount); + } + ); + // Count/send + // Header + bResult = ((Sequence_Count == sequence) + ? true + : _writeMDNSMsgHeader(msgHeader, p_rSendParameter)); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSMsgHeader FAILED!\n"));); + // Questions + for (stcMDNS_RRQuestion* pQuestion = p_rSendParameter.m_pQuestions; ((bResult) && (pQuestion)); pQuestion = pQuestion->m_pNext) + { + ((Sequence_Count == sequence) + ? ++msgHeader.m_u16QDCount + : (bResult = _writeMDNSQuestion(*pQuestion, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSQuestion FAILED!\n"));); + } + + // Answers and authorative answers +#ifdef MDNS_IP4_SUPPORT + if ((bResult) && + (p_rSendParameter.m_u8HostReplyMask & ContentFlag_A)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_A(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_A(A) FAILED!\n"));); + } + if ((bResult) && + (p_rSendParameter.m_u8HostReplyMask & ContentFlag_PTR_IP4)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_PTR_IP4(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_IP4 FAILED!\n"));); + } +#endif +#ifdef MDNS_IP6_SUPPORT + if ((bResult) && + (p_rSendParameter.m_u8HostReplyMask & ContentFlag_AAAA)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_AAAA(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_AAAA(A) FAILED!\n"));); + } + if ((bResult) && + (p_rSendParameter.m_u8HostReplyMask & ContentFlag_PTR_IP6)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_PTR_IP6(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_IP6 FAILED!\n"));); + } +#endif + + for (stcMDNSService* pService = m_pServices; ((bResult) && (pService)); pService = pService->m_pNext) + { + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_PTR_TYPE)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_PTR_TYPE(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_TYPE FAILED!\n"));); + } + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_PTR_NAME)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_PTR_NAME(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_PTR_NAME FAILED!\n"));); + } + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_SRV)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_SRV(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_SRV(A) FAILED!\n"));); + } + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_TXT)) + { + ((Sequence_Count == sequence) + ? ++ru16Answers + : (bResult = _writeMDNSAnswer_TXT(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_TXT(A) FAILED!\n"));); + } + } // for services + + // Additional answers +#ifdef MDNS_IP4_SUPPORT + bool bNeedsAdditionalAnswerA = false; +#endif +#ifdef MDNS_IP6_SUPPORT + bool bNeedsAdditionalAnswerAAAA = false; +#endif + for (stcMDNSService* pService = m_pServices; ((bResult) && (pService)); pService = pService->m_pNext) + { + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_PTR_NAME) && // If PTR_NAME is requested, AND + (!(pService->m_u8ReplyMask & ContentFlag_SRV))) // NOT SRV -> add SRV as additional answer + { + ((Sequence_Count == sequence) + ? ++msgHeader.m_u16ARCount + : (bResult = _writeMDNSAnswer_SRV(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_SRV(B) FAILED!\n"));); + } + if ((bResult) && + (pService->m_u8ReplyMask & ContentFlag_PTR_NAME) && // If PTR_NAME is requested, AND + (!(pService->m_u8ReplyMask & ContentFlag_TXT))) // NOT TXT -> add TXT as additional answer + { + ((Sequence_Count == sequence) + ? ++msgHeader.m_u16ARCount + : (bResult = _writeMDNSAnswer_TXT(*pService, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_TXT(B) FAILED!\n"));); + } + if ((pService->m_u8ReplyMask & (ContentFlag_PTR_NAME | ContentFlag_SRV)) || // If service instance name or SRV OR + (p_rSendParameter.m_u8HostReplyMask & (ContentFlag_A | ContentFlag_AAAA))) // any host IP address is requested + { +#ifdef MDNS_IP4_SUPPORT + if ((bResult) && + (!(p_rSendParameter.m_u8HostReplyMask & ContentFlag_A))) // Add IP4 address + { + bNeedsAdditionalAnswerA = true; + } +#endif +#ifdef MDNS_IP6_SUPPORT + if ((bResult) && + (!(p_rSendParameter.m_u8HostReplyMask & ContentFlag_AAAA))) // Add IP6 address + { + bNeedsAdditionalAnswerAAAA = true; + } +#endif + } + } // for services + + // Answer A needed? +#ifdef MDNS_IP4_SUPPORT + if ((bResult) && + (bNeedsAdditionalAnswerA)) + { + ((Sequence_Count == sequence) + ? ++msgHeader.m_u16ARCount + : (bResult = _writeMDNSAnswer_A(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_A(B) FAILED!\n"));); + } +#endif +#ifdef MDNS_IP6_SUPPORT + // Answer AAAA needed? + if ((bResult) && + (bNeedsAdditionalAnswerAAAA)) + { + ((Sequence_Count == sequence) + ? ++msgHeader.m_u16ARCount + : (bResult = _writeMDNSAnswer_AAAA(p_IPAddress, p_rSendParameter))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: _writeMDNSAnswer_AAAA(B) FAILED!\n"));); + } +#endif + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: Loop %i FAILED!\n"), sequence);); + } // for sequence + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _prepareMDNSMessage: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_sendMDNSServiceQuery + + Creates and sends a PTR query for the given service domain. + +*/ +bool MDNSResponder::_sendMDNSServiceQuery(const MDNSResponder::stcMDNSServiceQuery& p_ServiceQuery) +{ + + return _sendMDNSQuery(p_ServiceQuery.m_ServiceTypeDomain, DNS_RRTYPE_PTR); +} + +/* + MDNSResponder::_sendMDNSQuery + + Creates and sends a query for the given domain and query type. + +*/ +bool MDNSResponder::_sendMDNSQuery(const MDNSResponder::stcMDNS_RRDomain& p_QueryDomain, + uint16_t p_u16QueryType, + stcMDNSServiceQuery::stcAnswer* p_pKnownAnswers /*= 0*/) +{ + + bool bResult = false; + + stcMDNSSendParameter sendParameter; + if (0 != ((sendParameter.m_pQuestions = new stcMDNS_RRQuestion))) + { + sendParameter.m_pQuestions->m_Header.m_Domain = p_QueryDomain; + + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Type = p_u16QueryType; + // It seems, that some mDNS implementations don't support 'unicast response' questions... + sendParameter.m_pQuestions->m_Header.m_Attributes.m_u16Class = (/*0x8000 |*/ DNS_RRCLASS_IN); // /*Unicast &*/ INternet + + // TODO: Add knwon answer to the query + (void)p_pKnownAnswers; + + bResult = _sendMDNSMessage(sendParameter); + } // else: FAILED to alloc question + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _sendMDNSQuery: FAILED to alloc question!\n"));); + return bResult; +} + +/** + HELPERS +*/ + +/** + RESOURCE RECORDS +*/ + +/* + MDNSResponder::_readRRQuestion + + Reads a question (eg. MyESP._http._tcp.local ANY IN) from the UPD input buffer. + +*/ +bool MDNSResponder::_readRRQuestion(MDNSResponder::stcMDNS_RRQuestion& p_rRRQuestion) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion\n"));); + + bool bResult = false; + + if ((bResult = _readRRHeader(p_rRRQuestion.m_Header))) + { + // Extract unicast flag from class field + p_rRRQuestion.m_bUnicast = (p_rRRQuestion.m_Header.m_Attributes.m_u16Class & 0x8000); + p_rRRQuestion.m_Header.m_Attributes.m_u16Class &= (~0x8000); + + DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion ")); + _printRRDomain(p_rRRQuestion.m_Header.m_Domain); + DEBUG_OUTPUT.printf_P(PSTR(" Type:0x%04X Class:0x%04X %s\n"), (unsigned)p_rRRQuestion.m_Header.m_Attributes.m_u16Type, (unsigned)p_rRRQuestion.m_Header.m_Attributes.m_u16Class, (p_rRRQuestion.m_bUnicast ? "Unicast" : "Multicast")); + ); + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRQuestion: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_readRRAnswer + + Reads an answer (eg. _http._tcp.local PTR OP TTL MyESP._http._tcp.local) + from the UDP input buffer. + After reading the domain and type info, the further processing of the answer + is transferred the answer specific reading functions. + Unknown answer types are processed by the generic answer reader (to remove them + from the input buffer). + +*/ +bool MDNSResponder::_readRRAnswer(MDNSResponder::stcMDNS_RRAnswer*& p_rpRRAnswer) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer\n"));); + + bool bResult = false; + + stcMDNS_RRHeader header; + uint32_t u32TTL; + uint16_t u16RDLength; + if ((_readRRHeader(header)) && + (_udpRead32(u32TTL)) && + (_udpRead16(u16RDLength))) + { + + /* DEBUG_EX_INFO( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: Reading 0x%04X answer (class:0x%04X, TTL:%u, RDLength:%u) for "), header.m_Attributes.m_u16Type, header.m_Attributes.m_u16Class, u32TTL, u16RDLength); + _printRRDomain(header.m_Domain); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + );*/ + + switch (header.m_Attributes.m_u16Type & (~0x8000)) // Topmost bit might carry 'cache flush' flag + { +#ifdef MDNS_IP4_SUPPORT + case DNS_RRTYPE_A: + p_rpRRAnswer = new stcMDNS_RRAnswerA(header, u32TTL); + bResult = _readRRAnswerA(*(stcMDNS_RRAnswerA*&)p_rpRRAnswer, u16RDLength); + break; +#endif + case DNS_RRTYPE_PTR: + p_rpRRAnswer = new stcMDNS_RRAnswerPTR(header, u32TTL); + bResult = _readRRAnswerPTR(*(stcMDNS_RRAnswerPTR*&)p_rpRRAnswer, u16RDLength); + break; + case DNS_RRTYPE_TXT: + p_rpRRAnswer = new stcMDNS_RRAnswerTXT(header, u32TTL); + bResult = _readRRAnswerTXT(*(stcMDNS_RRAnswerTXT*&)p_rpRRAnswer, u16RDLength); + break; +#ifdef MDNS_IP6_SUPPORT + case DNS_RRTYPE_AAAA: + p_rpRRAnswer = new stcMDNS_RRAnswerAAAA(header, u32TTL); + bResult = _readRRAnswerAAAA(*(stcMDNS_RRAnswerAAAA*&)p_rpRRAnswer, u16RDLength); + break; +#endif + case DNS_RRTYPE_SRV: + p_rpRRAnswer = new stcMDNS_RRAnswerSRV(header, u32TTL); + bResult = _readRRAnswerSRV(*(stcMDNS_RRAnswerSRV*&)p_rpRRAnswer, u16RDLength); + break; + default: + p_rpRRAnswer = new stcMDNS_RRAnswerGeneric(header, u32TTL); + bResult = _readRRAnswerGeneric(*(stcMDNS_RRAnswerGeneric*&)p_rpRRAnswer, u16RDLength); + break; + } + DEBUG_EX_INFO( + if ((bResult) && + (p_rpRRAnswer)) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: ")); + _printRRDomain(p_rpRRAnswer->m_Header.m_Domain); + DEBUG_OUTPUT.printf_P(PSTR(" Type:0x%04X Class:0x%04X TTL:%u, RDLength:%u "), p_rpRRAnswer->m_Header.m_Attributes.m_u16Type, p_rpRRAnswer->m_Header.m_Attributes.m_u16Class, p_rpRRAnswer->m_u32TTL, u16RDLength); + switch (header.m_Attributes.m_u16Type & (~0x8000)) // Topmost bit might carry 'cache flush' flag + { +#ifdef MDNS_IP4_SUPPORT + case DNS_RRTYPE_A: + DEBUG_OUTPUT.printf_P(PSTR("A IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); + break; +#endif + case DNS_RRTYPE_PTR: + DEBUG_OUTPUT.printf_P(PSTR("PTR ")); + _printRRDomain(((stcMDNS_RRAnswerPTR*&)p_rpRRAnswer)->m_PTRDomain); + break; + case DNS_RRTYPE_TXT: + { + size_t stTxtLength = ((stcMDNS_RRAnswerTXT*&)p_rpRRAnswer)->m_Txts.c_strLength(); + char* pTxts = new char[stTxtLength]; + if (pTxts) + { + ((stcMDNS_RRAnswerTXT*&)p_rpRRAnswer)->m_Txts.c_str(pTxts); + DEBUG_OUTPUT.printf_P(PSTR("TXT(%u) %s"), stTxtLength, pTxts); + delete[] pTxts; + } + break; + } +#ifdef MDNS_IP6_SUPPORT + case DNS_RRTYPE_AAAA: + DEBUG_OUTPUT.printf_P(PSTR("AAAA IP:%s"), ((stcMDNS_RRAnswerA*&)p_rpRRAnswer)->m_IPAddress.toString().c_str()); + break; +#endif + case DNS_RRTYPE_SRV: + DEBUG_OUTPUT.printf_P(PSTR("SRV Port:%u "), ((stcMDNS_RRAnswerSRV*&)p_rpRRAnswer)->m_u16Port); + _printRRDomain(((stcMDNS_RRAnswerSRV*&)p_rpRRAnswer)->m_SRVDomain); + break; + default: + DEBUG_OUTPUT.printf_P(PSTR("generic ")); + break; + } + DEBUG_OUTPUT.printf_P(PSTR("\n")); + } + else + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: FAILED to read specific answer of type 0x%04X!\n"), p_rpRRAnswer->m_Header.m_Attributes.m_u16Type); + } + ); // DEBUG_EX_INFO + } + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswer: FAILED!\n"));); + return bResult; +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::_readRRAnswerA +*/ +bool MDNSResponder::_readRRAnswerA(MDNSResponder::stcMDNS_RRAnswerA& p_rRRAnswerA, + uint16_t p_u16RDLength) +{ + + uint32_t u32IP4Address; + bool bResult = ((MDNS_IP4_SIZE == p_u16RDLength) && + (_udpReadBuffer((unsigned char*)&u32IP4Address, MDNS_IP4_SIZE)) && + ((p_rRRAnswerA.m_IPAddress = IPAddress(u32IP4Address)))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerA: FAILED!\n"));); + return bResult; +} +#endif + +/* + MDNSResponder::_readRRAnswerPTR +*/ +bool MDNSResponder::_readRRAnswerPTR(MDNSResponder::stcMDNS_RRAnswerPTR& p_rRRAnswerPTR, + uint16_t p_u16RDLength) +{ + + bool bResult = ((p_u16RDLength) && + (_readRRDomain(p_rRRAnswerPTR.m_PTRDomain))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerPTR: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_readRRAnswerTXT + + Read TXT items from a buffer like 4c#=15ff=20 +*/ +bool MDNSResponder::_readRRAnswerTXT(MDNSResponder::stcMDNS_RRAnswerTXT& p_rRRAnswerTXT, + uint16_t p_u16RDLength) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: RDLength:%u\n"), p_u16RDLength);); + bool bResult = true; + + p_rRRAnswerTXT.clear(); + if (p_u16RDLength) + { + bResult = false; + + unsigned char* pucBuffer = new unsigned char[p_u16RDLength]; + if (pucBuffer) + { + if (_udpReadBuffer(pucBuffer, p_u16RDLength)) + { + bResult = true; + + const unsigned char* pucCursor = pucBuffer; + while ((pucCursor < (pucBuffer + p_u16RDLength)) && + (bResult)) + { + bResult = false; + + stcMDNSServiceTxt* pTxt = 0; + unsigned char ucLength = *pucCursor++; // Length of the next txt item + if (ucLength) + { + DEBUG_EX_INFO( + static char sacBuffer[64]; *sacBuffer = 0; + uint8_t u8MaxLength = ((ucLength > (sizeof(sacBuffer) - 1)) ? (sizeof(sacBuffer) - 1) : ucLength); + os_strncpy(sacBuffer, (const char*)pucCursor, u8MaxLength); sacBuffer[u8MaxLength] = 0; + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: Item(%u): %s\n"), ucLength, sacBuffer); + ); + + unsigned char* pucEqualSign = (unsigned char*)os_strchr((const char*)pucCursor, '='); // Position of the '=' sign + unsigned char ucKeyLength; + if ((pucEqualSign) && + ((ucKeyLength = (pucEqualSign - pucCursor)))) + { + unsigned char ucValueLength = (ucLength - (pucEqualSign - pucCursor + 1)); + bResult = (((pTxt = new stcMDNSServiceTxt)) && + (pTxt->setKey((const char*)pucCursor, ucKeyLength)) && + (pTxt->setValue((const char*)(pucEqualSign + 1), ucValueLength))); + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: INVALID TXT format (No '=')!\n"));); + } + pucCursor += ucLength; + } + else // no/zero length TXT + { + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: TXT answer contains no items.\n"));); + bResult = true; + } + + if ((bResult) && + (pTxt)) // Everythings fine so far + { + // Link TXT item to answer TXTs + pTxt->m_pNext = p_rRRAnswerTXT.m_Txts.m_pTxts; + p_rRRAnswerTXT.m_Txts.m_pTxts = pTxt; + } + else // At least no TXT (migth be OK, if length was 0) OR an error + { + if (!bResult) + { + DEBUG_EX_ERR( + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to read TXT item!\n")); + DEBUG_OUTPUT.printf_P(PSTR("RData dump:\n")); + _udpDump((m_pUDPContext->tell() - p_u16RDLength), p_u16RDLength); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + ); + } + if (pTxt) + { + delete pTxt; + pTxt = 0; + } + p_rRRAnswerTXT.clear(); + } + } // while + + DEBUG_EX_ERR( + if (!bResult) // Some failure + { + DEBUG_OUTPUT.printf_P(PSTR("RData dump:\n")); + _udpDump((m_pUDPContext->tell() - p_u16RDLength), p_u16RDLength); + DEBUG_OUTPUT.printf_P(PSTR("\n")); + } + ); + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to read TXT content!\n"));); + } + // Clean up + delete[] pucBuffer; + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED to alloc buffer for TXT content!\n"));); + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: WARNING! No content!\n"));); + } + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerTXT: FAILED!\n"));); + return bResult; +} + +#ifdef MDNS_IP6_SUPPORT +bool MDNSResponder::_readRRAnswerAAAA(MDNSResponder::stcMDNS_RRAnswerAAAA& p_rRRAnswerAAAA, + uint16_t p_u16RDLength) +{ + bool bResult = false; + // TODO: Implement + return bResult; +} +#endif + +/* + MDNSResponder::_readRRAnswerSRV +*/ +bool MDNSResponder::_readRRAnswerSRV(MDNSResponder::stcMDNS_RRAnswerSRV& p_rRRAnswerSRV, + uint16_t p_u16RDLength) +{ + + bool bResult = (((3 * sizeof(uint16_t)) < p_u16RDLength) && + (_udpRead16(p_rRRAnswerSRV.m_u16Priority)) && + (_udpRead16(p_rRRAnswerSRV.m_u16Weight)) && + (_udpRead16(p_rRRAnswerSRV.m_u16Port)) && + (_readRRDomain(p_rRRAnswerSRV.m_SRVDomain))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerSRV: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_readRRAnswerGeneric +*/ +bool MDNSResponder::_readRRAnswerGeneric(MDNSResponder::stcMDNS_RRAnswerGeneric& p_rRRAnswerGeneric, + uint16_t p_u16RDLength) +{ + bool bResult = (0 == p_u16RDLength); + + p_rRRAnswerGeneric.clear(); + if (((p_rRRAnswerGeneric.m_u16RDLength = p_u16RDLength)) && + ((p_rRRAnswerGeneric.m_pu8RDData = new unsigned char[p_rRRAnswerGeneric.m_u16RDLength]))) + { + + bResult = _udpReadBuffer(p_rRRAnswerGeneric.m_pu8RDData, p_rRRAnswerGeneric.m_u16RDLength); + } + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAnswerGeneric: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_readRRHeader +*/ +bool MDNSResponder::_readRRHeader(MDNSResponder::stcMDNS_RRHeader& p_rRRHeader) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRHeader\n"));); + + bool bResult = ((_readRRDomain(p_rRRHeader.m_Domain)) && + (_readRRAttributes(p_rRRHeader.m_Attributes))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRHeader: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_readRRDomain + + Reads a (maybe multilevel compressed) domain from the UDP input buffer. + +*/ +bool MDNSResponder::_readRRDomain(MDNSResponder::stcMDNS_RRDomain& p_rRRDomain) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain\n"));); + + bool bResult = ((p_rRRDomain.clear()) && + (_readRRDomain_Loop(p_rRRDomain, 0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_readRRDomain_Loop + + Reads a domain from the UDP input buffer. For every compression level, the functions + calls itself recursively. To avoid endless recursion because of malformed MDNS records, + the maximum recursion depth is set by MDNS_DOMAIN_MAX_REDIRCTION. + +*/ +bool MDNSResponder::_readRRDomain_Loop(MDNSResponder::stcMDNS_RRDomain& p_rRRDomain, + uint8_t p_u8Depth) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u)\n"), p_u8Depth);); + + bool bResult = false; + + if (MDNS_DOMAIN_MAX_REDIRCTION >= p_u8Depth) + { + bResult = true; + + uint8_t u8Len = 0; + do + { + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Offset:%u p0:%02x\n"), p_u8Depth, m_pUDPContext->tell(), m_pUDPContext->peek());); + _udpRead8(u8Len); + + if (u8Len & MDNS_DOMAIN_COMPRESS_MARK) + { + // Compressed label(s) + uint16_t u16Offset = ((u8Len & ~MDNS_DOMAIN_COMPRESS_MARK) << 8); // Implicit BE to LE conversion! + _udpRead8(u8Len); + u16Offset |= u8Len; + + if (m_pUDPContext->isValidOffset(u16Offset)) + { + size_t stCurrentPosition = m_pUDPContext->tell(); // Prepare return from recursion + + //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Redirecting from %u to %u!\n"), p_u8Depth, stCurrentPosition, u16Offset);); + m_pUDPContext->seek(u16Offset); + if (_readRRDomain_Loop(p_rRRDomain, p_u8Depth + 1)) // Do recursion + { + //DEBUG_EX_RX(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Succeeded to read redirected label! Returning to %u\n"), p_u8Depth, stCurrentPosition);); + m_pUDPContext->seek(stCurrentPosition); // Restore after recursion + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): FAILED to read redirected label!\n"), p_u8Depth);); + bResult = false; + } + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): INVALID offset in redirection!\n"), p_u8Depth);); + bResult = false; + } + break; + } + else + { + // Normal (uncompressed) label (maybe '\0' only) + if (MDNS_DOMAIN_MAXLENGTH > (p_rRRDomain.m_u16NameLength + u8Len)) + { + // Add length byte + p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength] = u8Len; + ++(p_rRRDomain.m_u16NameLength); + if (u8Len) // Add name + { + if ((bResult = _udpReadBuffer((unsigned char*) & (p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength]), u8Len))) + { + /* DEBUG_EX_INFO( + p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength + u8Len] = 0; // Closing '\0' for printing + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): Domain label (%u): %s\n"), p_u8Depth, (unsigned)(p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength - 1]), &(p_rRRDomain.m_acName[p_rRRDomain.m_u16NameLength])); + );*/ + + p_rRRDomain.m_u16NameLength += u8Len; + } + } + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(2) offset:%u p0:%x\n"), m_pUDPContext->tell(), m_pUDPContext->peek());); + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): ERROR! Domain name too long (%u + %u)!\n"), p_u8Depth, p_rRRDomain.m_u16NameLength, u8Len);); + bResult = false; + break; + } + } + } while ((bResult) && + (0 != u8Len)); + } + else + { + DEBUG_EX_ERR(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRDomain_Loop(%u): ERROR! Too many redirections!\n"), p_u8Depth);); + } + return bResult; +} + +/* + MDNSResponder::_readRRAttributes +*/ +bool MDNSResponder::_readRRAttributes(MDNSResponder::stcMDNS_RRAttributes& p_rRRAttributes) +{ + //DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAttributes\n"));); + + bool bResult = ((_udpRead16(p_rRRAttributes.m_u16Type)) && + (_udpRead16(p_rRRAttributes.m_u16Class))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readRRAttributes: FAILED!\n"));); + return bResult; +} + + +/* + DOMAIN NAMES +*/ + +/* + MDNSResponder::_buildDomainForHost + + Builds a MDNS host domain (eg. esp8266.local) for the given hostname. + +*/ +bool MDNSResponder::_buildDomainForHost(const char* p_pcHostname, + MDNSResponder::stcMDNS_RRDomain& p_rHostDomain) const +{ + + p_rHostDomain.clear(); + bool bResult = ((p_pcHostname) && + (*p_pcHostname) && + (p_rHostDomain.addLabel(p_pcHostname)) && + (p_rHostDomain.addLabel(scpcLocal)) && + (p_rHostDomain.addLabel(0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForHost: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_buildDomainForDNSSD + + Builds the '_services._dns-sd._udp.local' domain. + Used while detecting generic service enum question (DNS-SD) and answering these questions. + +*/ +bool MDNSResponder::_buildDomainForDNSSD(MDNSResponder::stcMDNS_RRDomain& p_rDNSSDDomain) const +{ + + p_rDNSSDDomain.clear(); + bool bResult = ((p_rDNSSDDomain.addLabel(scpcServices, true)) && + (p_rDNSSDDomain.addLabel(scpcDNSSD, true)) && + (p_rDNSSDDomain.addLabel(scpcUDP, true)) && + (p_rDNSSDDomain.addLabel(scpcLocal)) && + (p_rDNSSDDomain.addLabel(0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForDNSSD: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_buildDomainForService + + Builds the domain for the given service (eg. _http._tcp.local or + MyESP._http._tcp.local (if p_bIncludeName is set)). + +*/ +bool MDNSResponder::_buildDomainForService(const MDNSResponder::stcMDNSService& p_Service, + bool p_bIncludeName, + MDNSResponder::stcMDNS_RRDomain& p_rServiceDomain) const +{ + + p_rServiceDomain.clear(); + bool bResult = (((!p_bIncludeName) || + (p_rServiceDomain.addLabel(p_Service.m_pcName))) && + (p_rServiceDomain.addLabel(p_Service.m_pcService, true)) && + (p_rServiceDomain.addLabel(p_Service.m_pcProtocol, true)) && + (p_rServiceDomain.addLabel(scpcLocal)) && + (p_rServiceDomain.addLabel(0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForService: FAILED!\n"));); + return bResult; +} + +/* + MDNSResponder::_buildDomainForService + + Builds the domain for the given service properties (eg. _http._tcp.local). + The usual prepended '_' are added, if missing in the input strings. + +*/ +bool MDNSResponder::_buildDomainForService(const char* p_pcService, + const char* p_pcProtocol, + MDNSResponder::stcMDNS_RRDomain& p_rServiceDomain) const +{ + + p_rServiceDomain.clear(); + bool bResult = ((p_pcService) && + (p_pcProtocol) && + (p_rServiceDomain.addLabel(p_pcService, ('_' != *p_pcService))) && + (p_rServiceDomain.addLabel(p_pcProtocol, ('_' != *p_pcProtocol))) && + (p_rServiceDomain.addLabel(scpcLocal)) && + (p_rServiceDomain.addLabel(0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForService: FAILED for (%s.%s)!\n"), (p_pcService ? : "-"), (p_pcProtocol ? : "-"));); + return bResult; +} + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::_buildDomainForReverseIP4 + + The IP4 address is stringized by printing the four address bytes into a char buffer in reverse order + and adding 'in-addr.arpa' (eg. 012.789.456.123.in-addr.arpa). + Used while detecting reverse IP4 questions and answering these +*/ +bool MDNSResponder::_buildDomainForReverseIP4(IPAddress p_IP4Address, + MDNSResponder::stcMDNS_RRDomain& p_rReverseIP4Domain) const +{ + + bool bResult = true; + + p_rReverseIP4Domain.clear(); + + char acBuffer[32]; + for (int i = MDNS_IP4_SIZE; ((bResult) && (i >= 1)); --i) + { + itoa(p_IP4Address[i - 1], acBuffer, 10); + bResult = p_rReverseIP4Domain.addLabel(acBuffer); + } + bResult = ((bResult) && + (p_rReverseIP4Domain.addLabel(scpcReverseIP4Domain)) && + (p_rReverseIP4Domain.addLabel(scpcReverseTopDomain)) && + (p_rReverseIP4Domain.addLabel(0))); + DEBUG_EX_ERR(if (!bResult) DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _buildDomainForReverseIP4: FAILED!\n"));); + return bResult; +} +#endif + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::_buildDomainForReverseIP6 + + Used while detecting reverse IP6 questions and answering these +*/ +bool MDNSResponder::_buildDomainForReverseIP6(IPAddress p_IP4Address, + MDNSResponder::stcMDNS_RRDomain& p_rReverseIP6Domain) const +{ + // TODO: Implement + return false; +} +#endif + + +/* + UDP +*/ + +/* + MDNSResponder::_udpReadBuffer +*/ +bool MDNSResponder::_udpReadBuffer(unsigned char* p_pBuffer, + size_t p_stLength) +{ + + bool bResult = ((m_pUDPContext) && + (true/*m_pUDPContext->getSize() > p_stLength*/) && + (p_pBuffer) && + (p_stLength) && + ((p_stLength == m_pUDPContext->read((char*)p_pBuffer, p_stLength)))); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _udpReadBuffer: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_udpRead8 +*/ +bool MDNSResponder::_udpRead8(uint8_t& p_ru8Value) +{ + + return _udpReadBuffer((unsigned char*)&p_ru8Value, sizeof(p_ru8Value)); +} + +/* + MDNSResponder::_udpRead16 +*/ +bool MDNSResponder::_udpRead16(uint16_t& p_ru16Value) +{ + + bool bResult = false; + + if (_udpReadBuffer((unsigned char*)&p_ru16Value, sizeof(p_ru16Value))) + { + p_ru16Value = lwip_ntohs(p_ru16Value); + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::_udpRead32 +*/ +bool MDNSResponder::_udpRead32(uint32_t& p_ru32Value) +{ + + bool bResult = false; + + if (_udpReadBuffer((unsigned char*)&p_ru32Value, sizeof(p_ru32Value))) + { + p_ru32Value = lwip_ntohl(p_ru32Value); + bResult = true; + } + return bResult; +} + +/* + MDNSResponder::_udpAppendBuffer +*/ +bool MDNSResponder::_udpAppendBuffer(const unsigned char* p_pcBuffer, + size_t p_stLength) +{ + + bool bResult = ((m_pUDPContext) && + (p_pcBuffer) && + (p_stLength) && + (p_stLength == m_pUDPContext->append((const char*)p_pcBuffer, p_stLength))); + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _udpAppendBuffer: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_udpAppend8 +*/ +bool MDNSResponder::_udpAppend8(uint8_t p_u8Value) +{ + + return (_udpAppendBuffer((unsigned char*)&p_u8Value, sizeof(p_u8Value))); +} + +/* + MDNSResponder::_udpAppend16 +*/ +bool MDNSResponder::_udpAppend16(uint16_t p_u16Value) +{ + + p_u16Value = lwip_htons(p_u16Value); + return (_udpAppendBuffer((unsigned char*)&p_u16Value, sizeof(p_u16Value))); +} + +/* + MDNSResponder::_udpAppend32 +*/ +bool MDNSResponder::_udpAppend32(uint32_t p_u32Value) +{ + + p_u32Value = lwip_htonl(p_u32Value); + return (_udpAppendBuffer((unsigned char*)&p_u32Value, sizeof(p_u32Value))); +} + +#ifdef DEBUG_ESP_MDNS_RESPONDER +/* + MDNSResponder::_udpDump +*/ +bool MDNSResponder::_udpDump(bool p_bMovePointer /*= false*/) +{ + + const uint8_t cu8BytesPerLine = 16; + + uint32_t u32StartPosition = m_pUDPContext->tell(); + DEBUG_OUTPUT.println("UDP Context Dump:"); + uint32_t u32Counter = 0; + uint8_t u8Byte = 0; + + while (_udpRead8(u8Byte)) + { + DEBUG_OUTPUT.printf_P(PSTR("%02x %s"), u8Byte, ((++u32Counter % cu8BytesPerLine) ? "" : "\n")); + } + DEBUG_OUTPUT.printf_P(PSTR("%sDone: %u bytes\n"), (((u32Counter) && (u32Counter % cu8BytesPerLine)) ? "\n" : ""), u32Counter); + + if (!p_bMovePointer) // Restore + { + m_pUDPContext->seek(u32StartPosition); + } + return true; +} + +/* + MDNSResponder::_udpDump +*/ +bool MDNSResponder::_udpDump(unsigned p_uOffset, + unsigned p_uLength) +{ + + if ((m_pUDPContext) && + (m_pUDPContext->isValidOffset(p_uOffset))) + { + unsigned uCurrentPosition = m_pUDPContext->tell(); // Remember start position + + m_pUDPContext->seek(p_uOffset); + uint8_t u8Byte; + for (unsigned u = 0; ((u < p_uLength) && (_udpRead8(u8Byte))); ++u) + { + DEBUG_OUTPUT.printf_P(PSTR("%02x "), u8Byte); + } + // Return to start position + m_pUDPContext->seek(uCurrentPosition); + } + return true; +} +#endif + + +/** + READ/WRITE MDNS STRUCTS +*/ + +/* + MDNSResponder::_readMDNSMsgHeader + + Read a MDNS header from the UDP input buffer. + | 8 | 8 | 8 | 8 | + 00| Identifier | Flags & Codes | + 01| Question count | Answer count | + 02| NS answer count | Ad answer count | + + All 16-bit and 32-bit elements need to be translated form network coding to host coding (done in _udpRead16 and _udpRead32) + In addition, bitfield memory order is undefined in C standard (GCC doesn't order them in the coded direction...), so they + need some mapping here +*/ +bool MDNSResponder::_readMDNSMsgHeader(MDNSResponder::stcMDNS_MsgHeader& p_rMsgHeader) +{ + + bool bResult = false; + + uint8_t u8B1; + uint8_t u8B2; + if ((_udpRead16(p_rMsgHeader.m_u16ID)) && + (_udpRead8(u8B1)) && + (_udpRead8(u8B2)) && + (_udpRead16(p_rMsgHeader.m_u16QDCount)) && + (_udpRead16(p_rMsgHeader.m_u16ANCount)) && + (_udpRead16(p_rMsgHeader.m_u16NSCount)) && + (_udpRead16(p_rMsgHeader.m_u16ARCount))) + { + + p_rMsgHeader.m_1bQR = (u8B1 & 0x80); // Query/Responde flag + p_rMsgHeader.m_4bOpcode = (u8B1 & 0x78); // Operation code (0: Standard query, others ignored) + p_rMsgHeader.m_1bAA = (u8B1 & 0x04); // Authorative answer + p_rMsgHeader.m_1bTC = (u8B1 & 0x02); // Truncation flag + p_rMsgHeader.m_1bRD = (u8B1 & 0x01); // Recursion desired + + p_rMsgHeader.m_1bRA = (u8B2 & 0x80); // Recursion available + p_rMsgHeader.m_3bZ = (u8B2 & 0x70); // Zero + p_rMsgHeader.m_4bRCode = (u8B2 & 0x0F); // Response code + + /* DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readMDNSMsgHeader: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), + (unsigned)p_rMsgHeader.m_u16ID, + (unsigned)p_rMsgHeader.m_1bQR, (unsigned)p_rMsgHeader.m_4bOpcode, (unsigned)p_rMsgHeader.m_1bAA, (unsigned)p_rMsgHeader.m_1bTC, (unsigned)p_rMsgHeader.m_1bRD, + (unsigned)p_rMsgHeader.m_1bRA, (unsigned)p_rMsgHeader.m_4bRCode, + (unsigned)p_rMsgHeader.m_u16QDCount, + (unsigned)p_rMsgHeader.m_u16ANCount, + (unsigned)p_rMsgHeader.m_u16NSCount, + (unsigned)p_rMsgHeader.m_u16ARCount););*/ + bResult = true; + } + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _readMDNSMsgHeader: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_write8 +*/ +bool MDNSResponder::_write8(uint8_t p_u8Value, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + return ((_udpAppend8(p_u8Value)) && + (p_rSendParameter.shiftOffset(sizeof(p_u8Value)))); +} + +/* + MDNSResponder::_write16 +*/ +bool MDNSResponder::_write16(uint16_t p_u16Value, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + return ((_udpAppend16(p_u16Value)) && + (p_rSendParameter.shiftOffset(sizeof(p_u16Value)))); +} + +/* + MDNSResponder::_write32 +*/ +bool MDNSResponder::_write32(uint32_t p_u32Value, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + return ((_udpAppend32(p_u32Value)) && + (p_rSendParameter.shiftOffset(sizeof(p_u32Value)))); +} + +/* + MDNSResponder::_writeMDNSMsgHeader + + Write MDNS header to the UDP output buffer. + + All 16-bit and 32-bit elements need to be translated form host coding to network coding (done in _udpAppend16 and _udpAppend32) + In addition, bitfield memory order is undefined in C standard (GCC doesn't order them in the coded direction...), so they + need some mapping here +*/ +bool MDNSResponder::_writeMDNSMsgHeader(const MDNSResponder::stcMDNS_MsgHeader& p_MsgHeader, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + /* DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSMsgHeader: ID:%u QR:%u OP:%u AA:%u TC:%u RD:%u RA:%u R:%u QD:%u AN:%u NS:%u AR:%u\n"), + (unsigned)p_MsgHeader.m_u16ID, + (unsigned)p_MsgHeader.m_1bQR, (unsigned)p_MsgHeader.m_4bOpcode, (unsigned)p_MsgHeader.m_1bAA, (unsigned)p_MsgHeader.m_1bTC, (unsigned)p_MsgHeader.m_1bRD, + (unsigned)p_MsgHeader.m_1bRA, (unsigned)p_MsgHeader.m_4bRCode, + (unsigned)p_MsgHeader.m_u16QDCount, + (unsigned)p_MsgHeader.m_u16ANCount, + (unsigned)p_MsgHeader.m_u16NSCount, + (unsigned)p_MsgHeader.m_u16ARCount););*/ + + uint8_t u8B1((p_MsgHeader.m_1bQR << 7) | (p_MsgHeader.m_4bOpcode << 3) | (p_MsgHeader.m_1bAA << 2) | (p_MsgHeader.m_1bTC << 1) | (p_MsgHeader.m_1bRD)); + uint8_t u8B2((p_MsgHeader.m_1bRA << 7) | (p_MsgHeader.m_3bZ << 4) | (p_MsgHeader.m_4bRCode)); + bool bResult = ((_write16(p_MsgHeader.m_u16ID, p_rSendParameter)) && + (_write8(u8B1, p_rSendParameter)) && + (_write8(u8B2, p_rSendParameter)) && + (_write16(p_MsgHeader.m_u16QDCount, p_rSendParameter)) && + (_write16(p_MsgHeader.m_u16ANCount, p_rSendParameter)) && + (_write16(p_MsgHeader.m_u16NSCount, p_rSendParameter)) && + (_write16(p_MsgHeader.m_u16ARCount, p_rSendParameter))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSMsgHeader: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_writeRRAttributes +*/ +bool MDNSResponder::_writeMDNSRRAttributes(const MDNSResponder::stcMDNS_RRAttributes& p_Attributes, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + bool bResult = ((_write16(p_Attributes.m_u16Type, p_rSendParameter)) && + (_write16(p_Attributes.m_u16Class, p_rSendParameter))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSRRAttributes: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_writeMDNSRRDomain +*/ +bool MDNSResponder::_writeMDNSRRDomain(const MDNSResponder::stcMDNS_RRDomain& p_Domain, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + bool bResult = ((_udpAppendBuffer((const unsigned char*)p_Domain.m_acName, p_Domain.m_u16NameLength)) && + (p_rSendParameter.shiftOffset(p_Domain.m_u16NameLength))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSRRDomain: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_writeMDNSHostDomain + + Write a host domain to the UDP output buffer. + If the domain record is part of the answer, the records length is + prepended (p_bPrependRDLength is set). + + A very simple form of name compression is applied here: + If the domain is written to the UDP output buffer, the write offset is stored + together with a domain id (the pointer) in a p_rSendParameter substructure (cache). + If the same domain (pointer) should be written to the UDP output later again, + the old offset is retrieved from the cache, marked as a compressed domain offset + and written to the output buffer. + +*/ +bool MDNSResponder::_writeMDNSHostDomain(const char* p_pcHostname, + bool p_bPrependRDLength, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + // The 'skip-compression' version is handled in '_writeMDNSAnswer_SRV' + uint16_t u16CachedDomainOffset = p_rSendParameter.findCachedDomainOffset((const void*)p_pcHostname, false); + + stcMDNS_RRDomain hostDomain; + bool bResult = (u16CachedDomainOffset + // Found cached domain -> mark as compressed domain + ? ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset + ((!p_bPrependRDLength) || + (_write16(2, p_rSendParameter))) && // Length of 'Cxxx' + (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) + (_write8((uint8_t)(u16CachedDomainOffset & 0xFF), p_rSendParameter))) + // No cached domain -> add this domain to cache and write full domain name + : ((_buildDomainForHost(p_pcHostname, hostDomain)) && // eg. esp8266.local + ((!p_bPrependRDLength) || + (_write16(hostDomain.m_u16NameLength, p_rSendParameter))) && // RDLength (if needed) + (p_rSendParameter.addDomainCacheItem((const void*)p_pcHostname, false, p_rSendParameter.m_u16Offset)) && + (_writeMDNSRRDomain(hostDomain, p_rSendParameter)))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSHostDomain: FAILED!\n")); + }); + return bResult; + +} + +/* + MDNSResponder::_writeMDNSServiceDomain + + Write a service domain to the UDP output buffer. + If the domain record is part of the answer, the records length is + prepended (p_bPrependRDLength is set). + + A very simple form of name compression is applied here: see '_writeMDNSHostDomain' + The cache differentiates of course between service domains which includes + the instance name (p_bIncludeName is set) and thoose who don't. + +*/ +bool MDNSResponder::_writeMDNSServiceDomain(const MDNSResponder::stcMDNSService& p_Service, + bool p_bIncludeName, + bool p_bPrependRDLength, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + + // The 'skip-compression' version is handled in '_writeMDNSAnswer_SRV' + uint16_t u16CachedDomainOffset = p_rSendParameter.findCachedDomainOffset((const void*)&p_Service, p_bIncludeName); + + stcMDNS_RRDomain serviceDomain; + bool bResult = (u16CachedDomainOffset + // Found cached domain -> mark as compressed domain + ? ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset + ((!p_bPrependRDLength) || + (_write16(2, p_rSendParameter))) && // Lenght of 'Cxxx' + (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) + (_write8((uint8_t)(u16CachedDomainOffset & 0xFF), p_rSendParameter))) + // No cached domain -> add this domain to cache and write full domain name + : ((_buildDomainForService(p_Service, p_bIncludeName, serviceDomain)) && // eg. MyESP._http._tcp.local + ((!p_bPrependRDLength) || + (_write16(serviceDomain.m_u16NameLength, p_rSendParameter))) && // RDLength (if needed) + (p_rSendParameter.addDomainCacheItem((const void*)&p_Service, p_bIncludeName, p_rSendParameter.m_u16Offset)) && + (_writeMDNSRRDomain(serviceDomain, p_rSendParameter)))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSServiceDomain: FAILED!\n")); + }); + return bResult; + +} + +/* + MDNSResponder::_writeMDNSQuestion + + Write a MDNS question to the UDP output buffer + + QNAME (host/service domain, eg. esp8266.local) + QTYPE (16bit, eg. ANY) + QCLASS (16bit, eg. IN) + +*/ +bool MDNSResponder::_writeMDNSQuestion(MDNSResponder::stcMDNS_RRQuestion& p_Question, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSQuestion\n"));); + + bool bResult = ((_writeMDNSRRDomain(p_Question.m_Header.m_Domain, p_rSendParameter)) && + (_writeMDNSRRAttributes(p_Question.m_Header.m_Attributes, p_rSendParameter))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSQuestion: FAILED!\n")); + }); + return bResult; + +} + + +#ifdef MDNS_IP4_SUPPORT +/* + MDNSResponder::_writeMDNSAnswer_A + + Write a MDNS A answer to the UDP output buffer. + + NAME (var, host/service domain, eg. esp8266.local + TYPE (16bit, eg. A) + CLASS (16bit, eg. IN) + TTL (32bit, eg. 120) + RDLENGTH (16bit, eg 4) + RDATA (var, eg. 123.456.789.012) + + eg. esp8266.local A 0x8001 120 4 123.456.789.012 + Ref: http://www.zytrax.com/books/dns/ch8/a.html +*/ +bool MDNSResponder::_writeMDNSAnswer_A(IPAddress p_IPAddress, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_A (%s)\n"), p_IPAddress.toString().c_str());); + + stcMDNS_RRAttributes attributes(DNS_RRTYPE_A, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + const unsigned char aucIPAddress[MDNS_IP4_SIZE] = { p_IPAddress[0], p_IPAddress[1], p_IPAddress[2], p_IPAddress[3] }; + bool bResult = ((_writeMDNSHostDomain(m_pcHostname, false, p_rSendParameter)) && + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL + (_write16(MDNS_IP4_SIZE, p_rSendParameter)) && // RDLength + (_udpAppendBuffer(aucIPAddress, MDNS_IP4_SIZE)) && // RData + (p_rSendParameter.shiftOffset(MDNS_IP4_SIZE))); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_A: FAILED!\n")); + }); + return bResult; + +} + +/* + MDNSResponder::_writeMDNSAnswer_PTR_IP4 + + Write a MDNS reverse IP4 PTR answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + eg. 012.789.456.123.in-addr.arpa PTR 0x8001 120 15 esp8266.local + Used while answering reverse IP4 questions +*/ +bool MDNSResponder::_writeMDNSAnswer_PTR_IP4(IPAddress p_IPAddress, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP4 (%s)\n"), p_IPAddress.toString().c_str());); + + stcMDNS_RRDomain reverseIP4Domain; + stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + stcMDNS_RRDomain hostDomain; + bool bResult = ((_buildDomainForReverseIP4(p_IPAddress, reverseIP4Domain)) && // 012.789.456.123.in-addr.arpa + (_writeMDNSRRDomain(reverseIP4Domain, p_rSendParameter)) && + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL + (_writeMDNSHostDomain(m_pcHostname, true, p_rSendParameter))); // RDLength & RData (host domain, eg. esp8266.local) + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP4: FAILED!\n")); + }); + return bResult; +} +#endif + +/* + MDNSResponder::_writeMDNSAnswer_PTR_TYPE + + Write a MDNS PTR answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + PTR all-services -> service type + eg. _services._dns-sd._udp.local PTR 0x8001 5400 xx _http._tcp.local + http://www.zytrax.com/books/dns/ch8/ptr.html +*/ +bool MDNSResponder::_writeMDNSAnswer_PTR_TYPE(MDNSResponder::stcMDNSService& p_rService, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_TYPE\n"));); + + stcMDNS_RRDomain dnssdDomain; + stcMDNS_RRDomain serviceDomain; + stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, DNS_RRCLASS_IN); // No cache flush! only INternet + bool bResult = ((_buildDomainForDNSSD(dnssdDomain)) && // _services._dns-sd._udp.local + (_writeMDNSRRDomain(dnssdDomain, p_rSendParameter)) && + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL + (_writeMDNSServiceDomain(p_rService, false, true, p_rSendParameter))); // RDLength & RData (service domain, eg. _http._tcp.local) + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_TYPE: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_writeMDNSAnswer_PTR_NAME + + Write a MDNS PTR answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + PTR service type -> service name + eg. _http.tcp.local PTR 0x8001 120 xx myESP._http._tcp.local + http://www.zytrax.com/books/dns/ch8/ptr.html +*/ +bool MDNSResponder::_writeMDNSAnswer_PTR_NAME(MDNSResponder::stcMDNSService& p_rService, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_NAME\n"));); + + stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, DNS_RRCLASS_IN); // No cache flush! only INternet + bool bResult = ((_writeMDNSServiceDomain(p_rService, false, false, p_rSendParameter)) && // _http._tcp.local + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL + (_writeMDNSServiceDomain(p_rService, true, true, p_rSendParameter))); // RDLength & RData (service domain, eg. MyESP._http._tcp.local) + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_NAME: FAILED!\n")); + }); + return bResult; +} + + +/* + MDNSResponder::_writeMDNSAnswer_TXT + + Write a MDNS TXT answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + The TXT items in the RDATA block are 'length byte encoded': [len]vardata + + eg. myESP._http._tcp.local TXT 0x8001 120 4 c#=1 + http://www.zytrax.com/books/dns/ch8/txt.html +*/ +bool MDNSResponder::_writeMDNSAnswer_TXT(MDNSResponder::stcMDNSService& p_rService, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT\n"));); + + bool bResult = false; + + stcMDNS_RRAttributes attributes(DNS_RRTYPE_TXT, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + + if ((_collectServiceTxts(p_rService)) && + (_writeMDNSServiceDomain(p_rService, true, false, p_rSendParameter)) && // MyESP._http._tcp.local + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL + (_write16(p_rService.m_Txts.length(), p_rSendParameter))) // RDLength + { + + bResult = true; + // RData Txts + for (stcMDNSServiceTxt* pTxt = p_rService.m_Txts.m_pTxts; ((bResult) && (pTxt)); pTxt = pTxt->m_pNext) + { + unsigned char ucLengthByte = pTxt->length(); + bResult = ((_udpAppendBuffer((unsigned char*)&ucLengthByte, sizeof(ucLengthByte))) && // Length + (p_rSendParameter.shiftOffset(sizeof(ucLengthByte))) && + ((size_t)os_strlen(pTxt->m_pcKey) == m_pUDPContext->append(pTxt->m_pcKey, os_strlen(pTxt->m_pcKey))) && // Key + (p_rSendParameter.shiftOffset((size_t)os_strlen(pTxt->m_pcKey))) && + (1 == m_pUDPContext->append("=", 1)) && // = + (p_rSendParameter.shiftOffset(1)) && + ((!pTxt->m_pcValue) || + (((size_t)os_strlen(pTxt->m_pcValue) == m_pUDPContext->append(pTxt->m_pcValue, os_strlen(pTxt->m_pcValue))) && // Value + (p_rSendParameter.shiftOffset((size_t)os_strlen(pTxt->m_pcValue)))))); + + DEBUG_EX_ERR(if (!bResult) + { + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT: FAILED to write %sTxt %s=%s!\n"), (pTxt->m_bTemp ? "temp. " : ""), (pTxt->m_pcKey ? : "?"), (pTxt->m_pcValue ? : "?")); + }); + } + } + _releaseTempServiceTxts(p_rService); + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_TXT: FAILED!\n")); + }); + return bResult; +} + +#ifdef MDNS_IP6_SUPPORT +/* + MDNSResponder::_writeMDNSAnswer_AAAA + + Write a MDNS AAAA answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + eg. esp8266.local AAAA 0x8001 120 16 xxxx::xx + http://www.zytrax.com/books/dns/ch8/aaaa.html +*/ +bool MDNSResponder::_writeMDNSAnswer_AAAA(IPAddress p_IPAddress, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_AAAA\n"));); + + stcMDNS_RRAttributes attributes(DNS_RRTYPE_AAAA, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + bool bResult = ((_writeMDNSHostDomain(m_pcHostname, false, p_rSendParameter)) && // esp8266.local + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL + (_write16(MDNS_IP6_SIZE, p_rSendParameter)) && // RDLength + (false /*TODO: IP6 version of: _udpAppendBuffer((uint32_t)p_IPAddress, MDNS_IP4_SIZE)*/)); // RData + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_AAAA: FAILED!\n")); + }); + return bResult; +} + +/* + MDNSResponder::_writeMDNSAnswer_PTR_IP6 + + Write a MDNS reverse IP6 PTR answer to the UDP output buffer. + See: '_writeMDNSAnswer_A' + + eg. xxxx::xx.in6.arpa PTR 0x8001 120 15 esp8266.local + Used while answering reverse IP6 questions +*/ +bool MDNSResponder::_writeMDNSAnswer_PTR_IP6(IPAddress p_IPAddress, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP6\n"));); + + stcMDNS_RRDomain reverseIP6Domain; + stcMDNS_RRAttributes attributes(DNS_RRTYPE_PTR, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + bool bResult = ((_buildDomainForReverseIP6(p_IPAddress, reverseIP6Domain)) && // xxxx::xx.ip6.arpa + (_writeMDNSRRDomain(reverseIP6Domain, p_rSendParameter)) && + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_HOST_TTL), p_rSendParameter)) && // TTL + (_writeMDNSHostDomain(m_pcHostname, true, p_rSendParameter))); // RDLength & RData (host domain, eg. esp8266.local) + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_PTR_IP6: FAILED!\n")); + }); + return bResult; +} +#endif + +/* + MDNSResponder::_writeMDNSAnswer_SRV + + eg. MyESP._http.tcp.local SRV 0x8001 120 0 0 60068 esp8266.local + http://www.zytrax.com/books/dns/ch8/srv.html ???? Include instance name ???? +*/ +bool MDNSResponder::_writeMDNSAnswer_SRV(MDNSResponder::stcMDNSService& p_rService, + MDNSResponder::stcMDNSSendParameter& p_rSendParameter) +{ + DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_SRV\n"));); + + uint16_t u16CachedDomainOffset = (p_rSendParameter.m_bLegacyQuery + ? 0 + : p_rSendParameter.findCachedDomainOffset((const void*)m_pcHostname, false)); + + stcMDNS_RRAttributes attributes(DNS_RRTYPE_SRV, + ((p_rSendParameter.m_bCacheFlush ? 0x8000 : 0) | DNS_RRCLASS_IN)); // Cache flush? & INternet + stcMDNS_RRDomain hostDomain; + bool bResult = ((_writeMDNSServiceDomain(p_rService, true, false, p_rSendParameter)) && // MyESP._http._tcp.local + (_writeMDNSRRAttributes(attributes, p_rSendParameter)) && // TYPE & CLASS + (_write32((p_rSendParameter.m_bUnannounce ? 0 : MDNS_SERVICE_TTL), p_rSendParameter)) && // TTL + (!u16CachedDomainOffset + // No cache for domain name (or no compression allowed) + ? ((_buildDomainForHost(m_pcHostname, hostDomain)) && + (_write16((sizeof(uint16_t /*Prio*/) + // RDLength + sizeof(uint16_t /*Weight*/) + + sizeof(uint16_t /*Port*/) + + hostDomain.m_u16NameLength), p_rSendParameter)) && // Domain length + (_write16(MDNS_SRV_PRIORITY, p_rSendParameter)) && // Priority + (_write16(MDNS_SRV_WEIGHT, p_rSendParameter)) && // Weight + (_write16(p_rService.m_u16Port, p_rSendParameter)) && // Port + (p_rSendParameter.addDomainCacheItem((const void*)m_pcHostname, false, p_rSendParameter.m_u16Offset)) && + (_writeMDNSRRDomain(hostDomain, p_rSendParameter))) // Host, eg. esp8266.local + // Cache available for domain + : ((MDNS_DOMAIN_COMPRESS_MARK > ((u16CachedDomainOffset >> 8) & ~MDNS_DOMAIN_COMPRESS_MARK)) && // Valid offset + (_write16((sizeof(uint16_t /*Prio*/) + // RDLength + sizeof(uint16_t /*Weight*/) + + sizeof(uint16_t /*Port*/) + + 2), p_rSendParameter)) && // Length of 'C0xx' + (_write16(MDNS_SRV_PRIORITY, p_rSendParameter)) && // Priority + (_write16(MDNS_SRV_WEIGHT, p_rSendParameter)) && // Weight + (_write16(p_rService.m_u16Port, p_rSendParameter)) && // Port + (_write8(((u16CachedDomainOffset >> 8) | MDNS_DOMAIN_COMPRESS_MARK), p_rSendParameter)) && // Compression mark (and offset) + (_write8((uint8_t)u16CachedDomainOffset, p_rSendParameter))))); // Offset + + DEBUG_EX_ERR(if (!bResult) +{ + DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _writeMDNSAnswer_SRV: FAILED!\n")); + }); + return bResult; +} + +} // namespace MDNSImplementation + +} // namespace esp8266 + + + + + + diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_lwIPdefs.h b/libraries/ESP8266mDNS/src/LEAmDNS_lwIPdefs.h index c58aebdc7..a3bcc4b37 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_lwIPdefs.h +++ b/libraries/ESP8266mDNS/src/LEAmDNS_lwIPdefs.h @@ -1,26 +1,26 @@ /* - * LEAmDNS_Priv.h - * - * License (MIT license): - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ + LEAmDNS_Priv.h + + License (MIT license): + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ #ifndef MDNS_LWIPDEFS_H #define MDNS_LWIPDEFS_H diff --git a/libraries/Hash/src/Hash.cpp b/libraries/Hash/src/Hash.cpp index 5a58c961d..2d8866361 100644 --- a/libraries/Hash/src/Hash.cpp +++ b/libraries/Hash/src/Hash.cpp @@ -23,22 +23,18 @@ */ #include +#include #include "Hash.h" -extern "C" { -#include "sha1/sha1.h" -} - /** * create a sha1 hash from data * @param data uint8_t * * @param size uint32_t * @param hash uint8_t[20] */ -void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) { - - SHA1_CTX ctx; +void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]) { + br_sha1_context ctx; #ifdef DEBUG_SHA1 os_printf("DATA:"); @@ -53,9 +49,9 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) { os_printf("\n"); #endif - SHA1Init(&ctx); - SHA1Update(&ctx, data, size); - SHA1Final(hash, &ctx); + br_sha1_init(&ctx); + br_sha1_update(&ctx, data, size); + br_sha1_out(&ctx, hash); #ifdef DEBUG_SHA1 os_printf("SHA1:"); @@ -66,52 +62,35 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) { #endif } -void sha1(char * data, uint32_t size, uint8_t hash[20]) { - sha1((uint8_t *) data, size, hash); +void sha1(const char* data, uint32_t size, uint8_t hash[20]) { + sha1((const uint8_t *) data, size, hash); } -void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]) { - sha1((uint8_t *) data, size, hash); -} - -void sha1(const char * data, uint32_t size, uint8_t hash[20]) { - sha1((uint8_t *) data, size, hash); -} - -void sha1(String data, uint8_t hash[20]) { +void sha1(const String& data, uint8_t hash[20]) { sha1(data.c_str(), data.length(), hash); } -String sha1(uint8_t* data, uint32_t size) { +String sha1(const uint8_t* data, uint32_t size) { uint8_t hash[20]; - String hashStr = ""; + String hashStr((const char*)nullptr); + hashStr.reserve(20 * 2 + 1); sha1(&data[0], size, &hash[0]); for(uint16_t i = 0; i < 20; i++) { - String hex = String(hash[i], HEX); - if(hex.length() < 2) { - hex = "0" + hex; - } + char hex[3]; + snprintf(hex, sizeof(hex), "%02x", hash[i]); hashStr += hex; } return hashStr; } -String sha1(char* data, uint32_t size) { - return sha1((uint8_t*) data, size); -} - -String sha1(const uint8_t* data, uint32_t size) { - return sha1((uint8_t*) data, size); -} - String sha1(const char* data, uint32_t size) { - return sha1((uint8_t*) data, size); + return sha1((const uint8_t*) data, size); } -String sha1(String data) { +String sha1(const String& data) { return sha1(data.c_str(), data.length()); } diff --git a/libraries/Hash/src/Hash.h b/libraries/Hash/src/Hash.h index 774b8aad1..67a315112 100644 --- a/libraries/Hash/src/Hash.h +++ b/libraries/Hash/src/Hash.h @@ -27,16 +27,12 @@ //#define DEBUG_SHA1 -void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]); -void sha1(char * data, uint32_t size, uint8_t hash[20]); -void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]); -void sha1(const char * data, uint32_t size, uint8_t hash[20]); -void sha1(String data, uint8_t hash[20]); +void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]); +void sha1(const char* data, uint32_t size, uint8_t hash[20]); +void sha1(const String& data, uint8_t hash[20]); -String sha1(uint8_t* data, uint32_t size); -String sha1(char* data, uint32_t size); String sha1(const uint8_t* data, uint32_t size); String sha1(const char* data, uint32_t size); -String sha1(String data); +String sha1(const String& data); #endif /* HASH_H_ */ diff --git a/libraries/Hash/src/sha1/sha1.c b/libraries/Hash/src/sha1/sha1.c deleted file mode 100644 index fae926462..000000000 --- a/libraries/Hash/src/sha1/sha1.c +++ /dev/null @@ -1,208 +0,0 @@ -/** - * @file sha1.c - * @date 20.05.2015 - * @author Steve Reid - * - * from: http://www.virtualbox.org/svn/vbox/trunk/src/recompiler/tests/sha1.c - */ - -/* from valgrind tests */ - -/* ================ sha1.c ================ */ -/* - SHA-1 in C - By Steve Reid - 100% Public Domain - - Test Vectors (from FIPS PUB 180-1) - "abc" - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 - A million repetitions of "a" - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F -*/ - -/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ -/* #define SHA1HANDSOFF * Copies data before messing with it. */ - -#define SHA1HANDSOFF - -#include -#include -#include -#include - -#include "sha1.h" - -//#include - -#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) - -/* blk0() and blk() perform the initial expand. */ -/* I got the idea of expanding during the round function from SSLeay */ -#if BYTE_ORDER == LITTLE_ENDIAN -#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ - |(rol(block->l[i],8)&0x00FF00FF)) -#elif BYTE_ORDER == BIG_ENDIAN -#define blk0(i) block->l[i] -#else -#error "Endianness not defined!" -#endif -#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ - ^block->l[(i+2)&15]^block->l[i&15],1)) - -/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ -#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); -#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); -#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); - - -/* Hash a single 512-bit block. This is the core of the algorithm. */ - -void ICACHE_FLASH_ATTR SHA1Transform(uint32_t state[5], uint8_t buffer[64]) -{ -uint32_t a, b, c, d, e; -typedef union { - unsigned char c[64]; - uint32_t l[16]; -} CHAR64LONG16; -#ifdef SHA1HANDSOFF -CHAR64LONG16 block[1]; /* use array to appear as a pointer */ - memcpy(block, buffer, 64); -#else - /* The following had better never be used because it causes the - * pointer-to-const buffer to be cast into a pointer to non-const. - * And the result is written through. I threw a "const" in, hoping - * this will cause a diagnostic. - */ -CHAR64LONG16* block = (const CHAR64LONG16*)buffer; -#endif - /* Copy context->state[] to working vars */ - a = state[0]; - b = state[1]; - c = state[2]; - d = state[3]; - e = state[4]; - /* 4 rounds of 20 operations each. Loop unrolled. */ - R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); - R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); - R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); - R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); - R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); - R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); - R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); - R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); - R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); - R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); - R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); - R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); - R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); - R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); - R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); - R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); - R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); - R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); - R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); - R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); - /* Add the working vars back into context.state[] */ - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - state[4] += e; - /* Wipe variables */ - a = b = c = d = e = 0; -#ifdef SHA1HANDSOFF - memset(block, '\0', sizeof(block)); -#endif -} - - -/* SHA1Init - Initialize new context */ - -void ICACHE_FLASH_ATTR SHA1Init(SHA1_CTX* context) -{ - /* SHA1 initialization constants */ - context->state[0] = 0x67452301; - context->state[1] = 0xEFCDAB89; - context->state[2] = 0x98BADCFE; - context->state[3] = 0x10325476; - context->state[4] = 0xC3D2E1F0; - context->count[0] = context->count[1] = 0; -} - - -/* Run your data through this. */ - -void ICACHE_FLASH_ATTR SHA1Update(SHA1_CTX* context, uint8_t* data, uint32_t len) -{ - uint32_t i; - uint32_t j; - - j = context->count[0]; - if ((context->count[0] += len << 3) < j) - context->count[1]++; - context->count[1] += (len>>29); - j = (j >> 3) & 63; - if ((j + len) > 63) { - memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1Transform(context->state, context->buffer); - for ( ; i + 63 < len; i += 64) { - SHA1Transform(context->state, &data[i]); - } - j = 0; - } - else i = 0; - memcpy(&context->buffer[j], &data[i], len - i); -} - - -/* Add padding and return the message digest. */ - -void ICACHE_FLASH_ATTR SHA1Final(unsigned char digest[20], SHA1_CTX* context) -{ -unsigned i; -unsigned char finalcount[8]; -unsigned char c; - -#if 0 /* untested "improvement" by DHR */ - /* Convert context->count to a sequence of bytes - * in finalcount. Second element first, but - * big-endian order within element. - * But we do it all backwards. - */ - unsigned char *fcp = &finalcount[8]; - - for (i = 0; i < 2; i++) - { - uint32_t t = context->count[i]; - int j; - - for (j = 0; j < 4; t >>= 8, j++) - *--fcp = (unsigned char) t; - } -#else - for (i = 0; i < 8; i++) { - finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] - >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ - } -#endif - c = 0200; - SHA1Update(context, &c, 1); - while ((context->count[0] & 504) != 448) { - c = 0000; - SHA1Update(context, &c, 1); - } - SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ - for (i = 0; i < 20; i++) { - digest[i] = (unsigned char) - ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); - } - /* Wipe variables */ - memset(context, '\0', sizeof(*context)); - memset(&finalcount, '\0', sizeof(finalcount)); -} -/* ================ end of sha1.c ================ */ diff --git a/libraries/Hash/src/sha1/sha1.h b/libraries/Hash/src/sha1/sha1.h deleted file mode 100644 index 158bd76b3..000000000 --- a/libraries/Hash/src/sha1/sha1.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @file sha1.h - * @date 20.05.2015 - * @author Steve Reid - * - * from: http://www.virtualbox.org/svn/vbox/trunk/src/recompiler/tests/sha1.c - */ - -/* ================ sha1.h ================ */ -/* - SHA-1 in C - By Steve Reid - 100% Public Domain -*/ - -#ifndef SHA1_H_ -#define SHA1_H_ - -typedef struct { - uint32_t state[5]; - uint32_t count[2]; - unsigned char buffer[64]; -} SHA1_CTX; - -void SHA1Transform(uint32_t state[5], uint8_t buffer[64]); -void SHA1Init(SHA1_CTX* context); -void SHA1Update(SHA1_CTX* context, uint8_t* data, uint32_t len); -void SHA1Final(unsigned char digest[20], SHA1_CTX* context); - -#endif /* SHA1_H_ */ - -/* ================ end of sha1.h ================ */ diff --git a/libraries/SPI/SPI.h b/libraries/SPI/SPI.h index e8fb8a390..40ed0d944 100644 --- a/libraries/SPI/SPI.h +++ b/libraries/SPI/SPI.h @@ -24,7 +24,7 @@ #include #include -#define SPI_HAS_TRANSACTION +#define SPI_HAS_TRANSACTION 1 // This defines are not representing the real Divider of the ESP8266 // the Defines match to an AVR Arduino on 16MHz for better compatibility diff --git a/libraries/SoftwareSerial b/libraries/SoftwareSerial index 776d49b2f..8f85d6490 160000 --- a/libraries/SoftwareSerial +++ b/libraries/SoftwareSerial @@ -1 +1 @@ -Subproject commit 776d49b2f570b93fe88ad7a082519b4fb56be817 +Subproject commit 8f85d649000b5bbdde1862d879dba4f225dd3a51 diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 88103650d..3aa3604ab 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -1,31 +1,31 @@ /* - TwoWire.cpp - TWI/I2C library for Arduino & Wiring - Copyright (c) 2006 Nicholas Zambetti. All right reserved. + TwoWire.cpp - TWI/I2C library for Arduino & Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 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. + 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 + 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 - Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts - Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support - Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support - Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support + Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts + Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support + Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support + Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support */ extern "C" { - #include - #include - #include +#include +#include +#include } #include "twi.h" @@ -58,226 +58,273 @@ static int default_scl_pin = SCL; // Constructors //////////////////////////////////////////////////////////////// -TwoWire::TwoWire(){} +TwoWire::TwoWire() {} // Public Methods ////////////////////////////////////////////////////////////// -void TwoWire::begin(int sda, int scl){ - default_sda_pin = sda; - default_scl_pin = scl; - twi_init(sda, scl); - flush(); +void TwoWire::begin(int sda, int scl) +{ + default_sda_pin = sda; + default_scl_pin = scl; + twi_init(sda, scl); + flush(); } -void TwoWire::begin(int sda, int scl, uint8_t address){ - default_sda_pin = sda; - default_scl_pin = scl; - twi_setAddress(address); - twi_init(sda, scl); - twi_attachSlaveTxEvent(onRequestService); - twi_attachSlaveRxEvent(onReceiveService); - flush(); +void TwoWire::begin(int sda, int scl, uint8_t address) +{ + default_sda_pin = sda; + default_scl_pin = scl; + twi_setAddress(address); + twi_init(sda, scl); + twi_attachSlaveTxEvent(onRequestService); + twi_attachSlaveRxEvent(onReceiveService); + flush(); } -void TwoWire::pins(int sda, int scl){ - default_sda_pin = sda; - default_scl_pin = scl; +void TwoWire::pins(int sda, int scl) +{ + default_sda_pin = sda; + default_scl_pin = scl; } -void TwoWire::begin(void){ - begin(default_sda_pin, default_scl_pin); +void TwoWire::begin(void) +{ + begin(default_sda_pin, default_scl_pin); } -void TwoWire::begin(uint8_t address){ - twi_setAddress(address); - twi_attachSlaveTxEvent(onRequestService); - twi_attachSlaveRxEvent(onReceiveService); - begin(); +void TwoWire::begin(uint8_t address) +{ + twi_setAddress(address); + twi_attachSlaveTxEvent(onRequestService); + twi_attachSlaveRxEvent(onReceiveService); + begin(); } -uint8_t TwoWire::status(){ - return twi_status(); +uint8_t TwoWire::status() +{ + return twi_status(); } -void TwoWire::begin(int address){ - begin((uint8_t)address); +void TwoWire::begin(int address) +{ + begin((uint8_t)address); } -void TwoWire::setClock(uint32_t frequency){ - twi_setClock(frequency); +void TwoWire::setClock(uint32_t frequency) +{ + twi_setClock(frequency); } -void TwoWire::setClockStretchLimit(uint32_t limit){ - twi_setClockStretchLimit(limit); +void TwoWire::setClockStretchLimit(uint32_t limit) +{ + twi_setClockStretchLimit(limit); } -size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop){ - if(size > BUFFER_LENGTH){ - size = BUFFER_LENGTH; - } - size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0)?size:0; - rxBufferIndex = 0; - rxBufferLength = read; - return read; -} - -uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop){ - return requestFrom(address, static_cast(quantity), static_cast(sendStop)); -} - -uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity){ - return requestFrom(address, static_cast(quantity), true); -} - -uint8_t TwoWire::requestFrom(int address, int quantity){ - return requestFrom(static_cast(address), static_cast(quantity), true); -} - -uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop){ - return requestFrom(static_cast(address), static_cast(quantity), static_cast(sendStop)); -} - -void TwoWire::beginTransmission(uint8_t address){ - transmitting = 1; - txAddress = address; - txBufferIndex = 0; - txBufferLength = 0; -} - -void TwoWire::beginTransmission(int address){ - beginTransmission((uint8_t)address); -} - -uint8_t TwoWire::endTransmission(uint8_t sendStop){ - int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, sendStop); - txBufferIndex = 0; - txBufferLength = 0; - transmitting = 0; - return ret; -} - -uint8_t TwoWire::endTransmission(void){ - return endTransmission(true); -} - -size_t TwoWire::write(uint8_t data){ - if(transmitting){ - if(txBufferLength >= BUFFER_LENGTH){ - setWriteError(); - return 0; +size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) +{ + if (size > BUFFER_LENGTH) + { + size = BUFFER_LENGTH; } - txBuffer[txBufferIndex] = data; - ++txBufferIndex; - txBufferLength = txBufferIndex; - } else { - twi_transmit(&data, 1); - } - return 1; + size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0) ? size : 0; + rxBufferIndex = 0; + rxBufferLength = read; + return read; } -size_t TwoWire::write(const uint8_t *data, size_t quantity){ - if(transmitting){ - for(size_t i = 0; i < quantity; ++i){ - if(!write(data[i])) return i; +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) +{ + return requestFrom(address, static_cast(quantity), static_cast(sendStop)); +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) +{ + return requestFrom(address, static_cast(quantity), true); +} + +uint8_t TwoWire::requestFrom(int address, int quantity) +{ + return requestFrom(static_cast(address), static_cast(quantity), true); +} + +uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) +{ + return requestFrom(static_cast(address), static_cast(quantity), static_cast(sendStop)); +} + +void TwoWire::beginTransmission(uint8_t address) +{ + transmitting = 1; + txAddress = address; + txBufferIndex = 0; + txBufferLength = 0; +} + +void TwoWire::beginTransmission(int address) +{ + beginTransmission((uint8_t)address); +} + +uint8_t TwoWire::endTransmission(uint8_t sendStop) +{ + int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, sendStop); + txBufferIndex = 0; + txBufferLength = 0; + transmitting = 0; + return ret; +} + +uint8_t TwoWire::endTransmission(void) +{ + return endTransmission(true); +} + +size_t TwoWire::write(uint8_t data) +{ + if (transmitting) + { + if (txBufferLength >= BUFFER_LENGTH) + { + setWriteError(); + return 0; + } + txBuffer[txBufferIndex] = data; + ++txBufferIndex; + txBufferLength = txBufferIndex; } - }else{ - twi_transmit(data, quantity); - } - return quantity; + else + { + twi_transmit(&data, 1); + } + return 1; } -int TwoWire::available(void){ - int result = rxBufferLength - rxBufferIndex; - - if (!result) { - // yielding here will not make more data "available", - // but it will prevent the system from going into WDT reset - optimistic_yield(1000); - } - - return result; +size_t TwoWire::write(const uint8_t *data, size_t quantity) +{ + if (transmitting) + { + for (size_t i = 0; i < quantity; ++i) + { + if (!write(data[i])) + { + return i; + } + } + } + else + { + twi_transmit(data, quantity); + } + return quantity; } -int TwoWire::read(void){ - int value = -1; - if(rxBufferIndex < rxBufferLength){ - value = rxBuffer[rxBufferIndex]; - ++rxBufferIndex; - } - return value; +int TwoWire::available(void) +{ + int result = rxBufferLength - rxBufferIndex; + + if (!result) + { + // yielding here will not make more data "available", + // but it will prevent the system from going into WDT reset + optimistic_yield(1000); + } + + return result; } -int TwoWire::peek(void){ - int value = -1; - if(rxBufferIndex < rxBufferLength){ - value = rxBuffer[rxBufferIndex]; - } - return value; +int TwoWire::read(void) +{ + int value = -1; + if (rxBufferIndex < rxBufferLength) + { + value = rxBuffer[rxBufferIndex]; + ++rxBufferIndex; + } + return value; } -void TwoWire::flush(void){ - rxBufferIndex = 0; - rxBufferLength = 0; - txBufferIndex = 0; - txBufferLength = 0; +int TwoWire::peek(void) +{ + int value = -1; + if (rxBufferIndex < rxBufferLength) + { + value = rxBuffer[rxBufferIndex]; + } + return value; +} + +void TwoWire::flush(void) +{ + rxBufferIndex = 0; + rxBufferLength = 0; + txBufferIndex = 0; + txBufferLength = 0; } void TwoWire::onReceiveService(uint8_t* inBytes, size_t numBytes) { - // don't bother if user hasn't registered a callback - if (!user_onReceive) { - return; - } - // // don't bother if rx buffer is in use by a master requestFrom() op - // // i know this drops data, but it allows for slight stupidity - // // meaning, they may not have read all the master requestFrom() data yet - // if(rxBufferIndex < rxBufferLength){ - // return; - // } - - // copy twi rx buffer into local read buffer - // this enables new reads to happen in parallel - for (uint8_t i = 0; i < numBytes; ++i) { - rxBuffer[i] = inBytes[i]; - } - - // set rx iterator vars - rxBufferIndex = 0; - rxBufferLength = numBytes; - - // alert user program - user_onReceive(numBytes); + // don't bother if user hasn't registered a callback + if (!user_onReceive) + { + return; + } + // // don't bother if rx buffer is in use by a master requestFrom() op + // // i know this drops data, but it allows for slight stupidity + // // meaning, they may not have read all the master requestFrom() data yet + // if(rxBufferIndex < rxBufferLength){ + // return; + // } + + // copy twi rx buffer into local read buffer + // this enables new reads to happen in parallel + for (uint8_t i = 0; i < numBytes; ++i) + { + rxBuffer[i] = inBytes[i]; + } + + // set rx iterator vars + rxBufferIndex = 0; + rxBufferLength = numBytes; + + // alert user program + user_onReceive(numBytes); } void TwoWire::onRequestService(void) { - // don't bother if user hasn't registered a callback - if (!user_onRequest) { - return; - } - - // reset tx buffer iterator vars - // !!! this will kill any pending pre-master sendTo() activity - txBufferIndex = 0; - txBufferLength = 0; - - // alert user program - user_onRequest(); + // don't bother if user hasn't registered a callback + if (!user_onRequest) + { + return; + } + + // reset tx buffer iterator vars + // !!! this will kill any pending pre-master sendTo() activity + txBufferIndex = 0; + txBufferLength = 0; + + // alert user program + user_onRequest(); } -void TwoWire::onReceive( void (*function)(int) ) { - // arduino api compatibility fixer: - // really hope size parameter will not exceed 2^31 :) - static_assert(sizeof(int) == sizeof(size_t), "something is wrong in Arduino kingdom"); - user_onReceive = reinterpret_cast(function); +void TwoWire::onReceive(void (*function)(int)) +{ + // arduino api compatibility fixer: + // really hope size parameter will not exceed 2^31 :) + static_assert(sizeof(int) == sizeof(size_t), "something is wrong in Arduino kingdom"); + user_onReceive = reinterpret_cast(function); } -void TwoWire::onReceive( void (*function)(size_t) ) { - user_onReceive = function; +void TwoWire::onReceive(void (*function)(size_t)) +{ + user_onReceive = function; + twi_enableSlaveMode(); } -void TwoWire::onRequest( void (*function)(void) ){ - user_onRequest = function; +void TwoWire::onRequest(void (*function)(void)) +{ + user_onRequest = function; + twi_enableSlaveMode(); } // Preinstantiate Objects ////////////////////////////////////////////////////// diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index d396f4a84..5d6b36457 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -1,24 +1,24 @@ /* - TwoWire.h - TWI/I2C library for Arduino & Wiring - Copyright (c) 2006 Nicholas Zambetti. All right reserved. + TwoWire.h - TWI/I2C library for Arduino & Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 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. + 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 + 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 - Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts - Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support - Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support + Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts + Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support + Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support */ #ifndef TwoWire_h @@ -33,7 +33,7 @@ class TwoWire : public Stream { - private: +private: static uint8_t rxBuffer[]; static uint8_t rxBufferIndex; static uint8_t rxBufferLength; @@ -48,10 +48,10 @@ class TwoWire : public Stream static void (*user_onReceive)(size_t); static void onRequestService(void); static void onReceiveService(uint8_t*, size_t); - public: +public: TwoWire(); void begin(int sda, int scl); - void begin(int sda, int scl, uint8_t address); + void begin(int sda, int scl, uint8_t address); void pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code void begin(); void begin(uint8_t); @@ -63,22 +63,22 @@ class TwoWire : public Stream uint8_t endTransmission(void); uint8_t endTransmission(uint8_t); size_t requestFrom(uint8_t address, size_t size, bool sendStop); - uint8_t status(); + uint8_t status(); uint8_t requestFrom(uint8_t, uint8_t); uint8_t requestFrom(uint8_t, uint8_t, uint8_t); uint8_t requestFrom(int, int); uint8_t requestFrom(int, int, int); - + virtual size_t write(uint8_t); virtual size_t write(const uint8_t *, size_t); virtual int available(void); virtual int read(void); virtual int peek(void); virtual void flush(void); - void onReceive( void (*)(int) ); // arduino api - void onReceive( void (*)(size_t) ); // legacy esp8266 backward compatibility - void onRequest( void (*)(void) ); + void onReceive(void (*)(int)); // arduino api + void onReceive(void (*)(size_t)); // legacy esp8266 backward compatibility + void onRequest(void (*)(void)); using Print::write; }; diff --git a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino index d7c6b52b4..fe713e02d 100644 --- a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino +++ b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino @@ -1,80 +1,160 @@ /* - NTP-TZ-DST + NTP-TZ-DST (v2) NetWork Time Protocol - Time Zone - Daylight Saving Time - This example shows how to read and set time, - and how to use NTP (set NTP0_OR_LOCAL1 to 0 below) - or an external RTC (set NTP0_OR_LOCAL1 to 1 below) - - TZ and DST below have to be manually set - according to your local settings. + This example shows: + - how to read and set time + - how to set timezone per country/city + - how is local time automatically handled per official timezone definitions + - how to change internal sntp start and update delay + - how to use callbacks when time is updated This example code is in the public domain. */ -#include -#include // time() ctime() -#include // struct timeval -#include // settimeofday_cb() - -//////////////////////////////////////////////////////// #ifndef STASSID #define STASSID "your-ssid" #define STAPSK "your-password" #endif -#define SSID STASSID -#define SSIDPWD STAPSK -#define TZ 1 // (utc+) TZ in hours -#define DST_MN 60 // use 60mn for summer time in some countries +// initial time (possibly given by an external RTC) +#define RTC_UTC_TEST 1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC -#define NTP0_OR_LOCAL1 1 // 0:use NTP 1:fake external RTC -#define RTC_TEST 1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC + +// This database is autogenerated from IANA timezone database +// https://www.iana.org/time-zones +// and can be updated on demand in this repository +#include + +// "TZ_" macros follow DST change across seasons without source code change +// check for your nearest city in TZ.h + +// espressif headquarter TZ +//#define MYTZ TZ_Asia_Shanghai + +// example for "Not Only Whole Hours" timezones: +// Kolkata/Calcutta is shifted by 30mn +//#define MYTZ TZ_Asia_Kolkata + +// example of a timezone with a variable Daylight-Saving-Time: +// demo: watch automatic time adjustment on Summer/Winter change (DST) +#define MYTZ TZ_Europe_London //////////////////////////////////////////////////////// -#define TZ_MN ((TZ)*60) -#define TZ_SEC ((TZ)*3600) -#define DST_SEC ((DST_MN)*60) +#include +#include // settimeofday_cb() +#include +#include -timeval cbtime; // time set in callback -bool cbtime_set = false; +#include // time() ctime() +#include // struct timeval -void time_is_set(void) { - gettimeofday(&cbtime, NULL); - cbtime_set = true; - Serial.println("------------------ settimeofday() was called ------------------"); -} - -void setup() { - Serial.begin(115200); - settimeofday_cb(time_is_set); - -#if NTP0_OR_LOCAL1 - // local - - ESP.eraseConfig(); - time_t rtc = RTC_TEST; - timeval tv = { rtc, 0 }; - timezone tz = { TZ_MN + DST_MN, 0 }; - settimeofday(&tv, &tz); - -#else // ntp - - configTime(TZ_SEC, DST_SEC, "pool.ntp.org"); - WiFi.mode(WIFI_STA); - WiFi.begin(SSID, SSIDPWD); - // don't wait, observe time changing when ntp timestamp is received - -#endif // ntp -} +#include // sntp_servermode_dhcp() // for testing purpose: extern "C" int clock_gettime(clockid_t unused, struct timespec *tp); +//////////////////////////////////////////////////////// + +static timeval tv; +static timespec tp; +static time_t now; +static uint32_t now_ms, now_us; + +static esp8266::polledTimeout::periodicMs showTimeNow(60000); +static int time_machine_days = 0; // 0 = now +static bool time_machine_running = false; + +// OPTIONAL: change SNTP startup delay +// a weak function is already defined and returns 0 (RFC violation) +// it can be redefined: +//uint32_t sntp_startup_delay_MS_rfc_not_less_than_60000 () +//{ +// //info_sntp_startup_delay_MS_rfc_not_less_than_60000_has_been_called = true; +// return 60000; // 60s (or lwIP's original default: (random() % 5000)) +//} + +// OPTIONAL: change SNTP update delay +// a weak function is already defined and returns 1 hour +// it can be redefined: +//uint32_t sntp_update_delay_MS_rfc_not_less_than_15000 () +//{ +// //info_sntp_update_delay_MS_rfc_not_less_than_15000_has_been_called = true; +// return 15000; // 15s +//} + + +void showTime() { + gettimeofday(&tv, nullptr); + clock_gettime(0, &tp); + now = time(nullptr); + now_ms = millis(); + now_us = micros(); + + Serial.println(); + printTm("localtime:", localtime(&now)); + Serial.println(); + printTm("gmtime: ", gmtime(&now)); + Serial.println(); + + // time from boot + Serial.print("clock: "); + Serial.print((uint32_t)tp.tv_sec); + Serial.print("s / "); + Serial.print((uint32_t)tp.tv_nsec); + Serial.println("ns"); + + // time from boot + Serial.print("millis: "); + Serial.println(now_ms); + Serial.print("micros: "); + Serial.println(now_us); + + // EPOCH+tz+dst + Serial.print("gtod: "); + Serial.print((uint32_t)tv.tv_sec); + Serial.print("s / "); + Serial.print((uint32_t)tv.tv_usec); + Serial.println("us"); + + // EPOCH+tz+dst + Serial.print("time: "); + Serial.println((uint32_t)now); + + // timezone and demo in the future + Serial.printf("timezone: %s\n", MYTZ); + + // human readable + Serial.print("ctime: "); + Serial.print(ctime(&now)); + +#if LWIP_VERSION_MAJOR > 1 + // LwIP v2 is able to list more details about the currently configured SNTP servers + for (int i = 0; i < SNTP_MAX_SERVERS; i++) { + IPAddress sntp = *sntp_getserver(i); + const char* name = sntp_getservername(i); + if (sntp.isSet()) { + Serial.printf("sntp%d: ", i); + if (name) { + Serial.printf("%s (%s) ", name, sntp.toString().c_str()); + } else { + Serial.printf("%s ", sntp.toString().c_str()); + } + Serial.printf("IPv6: %s Reachability: %o\n", + sntp.isV6() ? "Yes" : "No", + sntp_getreachability(i)); + } + } +#endif + + Serial.println(); +} + + #define PTM(w) \ - Serial.print(":" #w "="); \ + Serial.print(" " #w "="); \ Serial.print(tm->tm_##w); void printTm(const char* what, const tm* tm) { @@ -84,61 +164,74 @@ void printTm(const char* what, const tm* tm) { PTM(hour); PTM(min); PTM(sec); } -timeval tv; -timespec tp; -time_t now; -uint32_t now_ms, now_us; +void time_is_set_scheduled() { + // everything is allowed in this function -void loop() { - - gettimeofday(&tv, nullptr); - clock_gettime(0, &tp); - now = time(nullptr); - now_ms = millis(); - now_us = micros(); - - // localtime / gmtime every second change - static time_t lastv = 0; - if (lastv != tv.tv_sec) { - lastv = tv.tv_sec; - Serial.println(); - printTm("localtime", localtime(&now)); - Serial.println(); - printTm("gmtime ", gmtime(&now)); - Serial.println(); - Serial.println(); + if (time_machine_days == 0) { + time_machine_running = !time_machine_running; } - // time from boot - Serial.print("clock:"); - Serial.print((uint32_t)tp.tv_sec); - Serial.print("/"); - Serial.print((uint32_t)tp.tv_nsec); - Serial.print("ns"); - - // time from boot - Serial.print(" millis:"); - Serial.print(now_ms); - Serial.print(" micros:"); - Serial.print(now_us); - - // EPOCH+tz+dst - Serial.print(" gtod:"); - Serial.print((uint32_t)tv.tv_sec); - Serial.print("/"); - Serial.print((uint32_t)tv.tv_usec); - Serial.print("us"); - - // EPOCH+tz+dst - Serial.print(" time:"); - Serial.print((uint32_t)now); - - // human readable - Serial.print(" ctime:(UTC+"); - Serial.print((uint32_t)(TZ * 60 + DST_MN)); - Serial.print("mn)"); - Serial.print(ctime(&now)); - - // simple drifting loop - delay(100); + // time machine demo + if (time_machine_running) { + if (time_machine_days == 0) + Serial.printf("---- settimeofday() has been called - possibly from SNTP\n" + " (starting time machine demo to show libc's automatic DST handling)\n\n"); + now = time(nullptr); + const tm* tm = localtime(&now); + Serial.printf("future=%3ddays: DST=%s - ", + time_machine_days, + tm->tm_isdst ? "true " : "false"); + Serial.print(ctime(&now)); + gettimeofday(&tv, nullptr); + constexpr int days = 30; + time_machine_days += days; + if (time_machine_days > 360) { + tv.tv_sec -= (time_machine_days - days) * 60 * 60 * 24; + time_machine_days = 0; + } else { + tv.tv_sec += days * 60 * 60 * 24; + } + settimeofday(&tv, nullptr); + } else { + showTime(); + } +} + +void setup() { + Serial.begin(115200); + Serial.println("\nStarting...\n"); + + // setup RTC time + // it will be used until NTP server will send us real current time + time_t rtc = RTC_UTC_TEST; + timeval tv = { rtc, 0 }; + timezone tz = { 0, 0 }; + settimeofday(&tv, &tz); + + // install callback - called when settimeofday is called (by SNTP or us) + // once enabled (by DHCP), SNTP is updated every hour + settimeofday_cb(time_is_set_scheduled); + + // NTP servers may be overriden by your DHCP server for a more local one + // (see below) + configTime(MYTZ, "pool.ntp.org"); + + // OPTIONAL: disable obtaining SNTP servers from DHCP + //sntp_servermode_dhcp(0); // 0: disable obtaining SNTP servers from DHCP (enabled by default) + + // start network + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + + // don't wait for network, observe time changing + // when NTP timestamp is received + Serial.printf("Time is currently set by a constant:\n"); + showTime(); +} + +void loop() { + if (showTimeNow) { + showTime(); + } } diff --git a/libraries/esp8266/library.properties b/libraries/esp8266/library.properties index 6e1cc8ade..4c879f66a 100644 --- a/libraries/esp8266/library.properties +++ b/libraries/esp8266/library.properties @@ -7,4 +7,4 @@ paragraph= category=Other url= architectures=esp8266 -dot_a_linkage=true +dot_a_linkage=false diff --git a/package/README.md b/package/README.md index a67a77d3a..84b905913 100644 --- a/package/README.md +++ b/package/README.md @@ -107,18 +107,23 @@ Here is an overview of the release process. See the section below for detailed i The following points assume work in a direct clone of the repository, and not in a personal fork. -2. Update `version` to the release in platform.txt and commit. E.g. `2.5.0`. Make a PR, wait for Travis CI, and merge. +2. Make a PR with the following, wait for Travis CI, and merge. -3. Tag the latest commit on the master branch. In this project, tags have form `X.Y.Z`, e.g. `2.4.0`, or `X.Y.Z-betaN` for release candiate versions. Notice that there's no `v`at the beginning of the tag. Tags must be annotated, not lightweight tags. To create a tag, use git command (assuming that the master branch is checked out): + * platform.txt: update `version` to the release E.g. `3.0.0`, + + * `cores/esp8266/TZ.h`: import the latest database with the following shell command:\ + `$ cd tools; sh TZupdate.sh`. + +3. Tag the latest commit on the master branch. In this project, tags have form `X.Y.Z`, e.g. `3.0.0`, or `X.Y.Z-betaN` for release candiate versions. Notice that there's no `v`at the beginning of the tag. Tags must be annotated, not lightweight tags. To create a tag, use git command (assuming that the master branch is checked out): ``` - git tag -a -m "Release 2.5.0" 2.5.0 + git tag -a -m "Release 3.0.0" 3.0.0 ``` then push the tag created in step 3 to esp8266/Arduino Github repository: ``` - git push origin 2.5.0 + git push origin 3.0.0 ``` 4. In case something goes wrong, release can be canceled at any time: @@ -141,7 +146,7 @@ The following points assume work in a direct clone of the repository, and not in 11. Create a commit to the master branch, updating: - * The version in platform.txt file. This should correspond to the version of the *next* milestone, plus `-dev` suffix. E.g. `2.5.0-dev`. + * The version in platform.txt file. This should correspond to the version of the *next* milestone, plus `-dev` suffix. E.g. `3.1.0-dev`. * In main README.md: diff --git a/package/build_boards_manager_package.sh b/package/build_boards_manager_package.sh index 8220ad236..f5ffff447 100755 --- a/package/build_boards_manager_package.sh +++ b/package/build_boards_manager_package.sh @@ -3,25 +3,37 @@ #set -x -# Extract next version from platform.txt -next=`sed -n -E 's/version=([0-9.]+)/\1/p' ../platform.txt` +ver=`git describe --tag` +visiblever=$ver +if [ "$ver" = 0.0.1 ]; then -# Figure out how will the package be called -ver=`git describe --exact-match` -if [ $? -ne 0 ]; then - # not tagged version; generate nightly package - date_str=`date +"%Y%m%d"` - is_nightly=1 - plain_ver="${next}-nightly" - ver="${plain_ver}+${date_str}" -else + git tag -d 0.0.1 + ver=`git describe --tag HEAD` plain_ver=$ver + +else + + # Extract next version from platform.txt + next=`sed -n -E 's/version=([0-9.]+)/\1/p' ../platform.txt` + + # Figure out how will the package be called + ver=`git describe --exact-match` + if [ $? -ne 0 ]; then + # not tagged version; generate nightly package + date_str=`date +"%Y%m%d"` + is_nightly=1 + plain_ver="${next}-nightly" + ver="${plain_ver}+${date_str}" + else + plain_ver=$ver + fi + visiblever=$ver fi set -e -package_name=esp8266-$ver -echo "Version: $ver" +package_name=esp8266-$visiblever +echo "Version: $visiblever ($ver)" echo "Package name: $package_name" # Set REMOTE_URL environment variable to the address where the package will be @@ -34,7 +46,7 @@ echo "Remote: $REMOTE_URL" if [ -z "$PKG_URL" ]; then if [ -z "$PKG_URL_PREFIX" ]; then - PKG_URL_PREFIX="$REMOTE_URL/versions/$ver" + PKG_URL_PREFIX="$REMOTE_URL/versions/$visiblever" fi PKG_URL="$PKG_URL_PREFIX/$package_name.zip" fi @@ -43,9 +55,9 @@ echo "Docs: $DOC_URL" pushd .. # Create directory for the package -outdir=package/versions/$ver/$package_name +outdir=package/versions/$visiblever/$package_name srcdir=$PWD -rm -rf package/versions/$ver +rm -rf package/versions/$visiblever mkdir -p $outdir # Some files should be excluded from the package @@ -96,7 +108,7 @@ echo \#define ARDUINO_ESP8266_RELEASE_$ver_define >>$outdir/cores/esp8266/core_v echo \#define ARDUINO_ESP8266_RELEASE \"$ver_define\" >>$outdir/cores/esp8266/core_version.h # Zip the package -pushd package/versions/$ver +pushd package/versions/$visiblever echo "Making $package_name.zip" zip -qr $package_name.zip $package_name rm -rf $package_name @@ -109,7 +121,7 @@ echo SHA-256: $sha echo "Making package_esp8266com_index.json" -jq_arg=".packages[0].platforms[0].version = \"$ver\" | \ +jq_arg=".packages[0].platforms[0].version = \"$visiblever\" | \ .packages[0].platforms[0].url = \"$PKG_URL\" |\ .packages[0].platforms[0].archiveFileName = \"$package_name.zip\"" diff --git a/package/package_esp8266com_index.template.json b/package/package_esp8266com_index.template.json index cc4e3f866..709238253 100644 --- a/package/package_esp8266com_index.template.json +++ b/package/package_esp8266com_index.template.json @@ -359,4 +359,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/platform.txt b/platform.txt index 8bccbe2af..dfeee3ae0 100644 --- a/platform.txt +++ b/platform.txt @@ -39,7 +39,7 @@ build.stdcpp_level=-std=gnu++11 build.float=-u _printf_float -u _scanf_float build.led= -build.sdk=NONOSDK22y +build.sdk=NONOSDK22x_190703 compiler.path={runtime.tools.xtensa-lx106-elf-gcc.path}/bin/ compiler.sdk.path={runtime.platform.path}/tools/sdk @@ -137,7 +137,8 @@ tools.esptool.cmd={runtime.platform.path}/tools/python3/python3 tools.esptool.network_cmd={runtime.platform.path}/tools/python3/python3 tools.esptool.upload.protocol=esp -tools.esptool.upload.params.verbose=--trace +# esptool.py --trace option is a debug option, not a verbose option +tools.esptool.upload.params.verbose= tools.esptool.upload.params.quiet= # First, potentially perform an erase or nothing diff --git a/tests/ci/style_check.sh b/tests/ci/style_check.sh index a6b353242..5cba58f9f 100755 --- a/tests/ci/style_check.sh +++ b/tests/ci/style_check.sh @@ -9,6 +9,7 @@ ${org}/../restyle.sh # Revert changes which astyle might have done to the submodules, # as we don't want to fail the build because of the 3rd party libraries -git submodule foreach --recursive git reset --hard +git --version || true +git submodule foreach --recursive 'git reset --hard' git diff --exit-code -- $TRAVIS_BUILD_DIR/libraries diff --git a/tests/common.sh b/tests/common.sh index b93bd57ba..bf329b3ac 100755 --- a/tests/common.sh +++ b/tests/common.sh @@ -153,19 +153,31 @@ function install_libraries() function install_ide() { + #local idever='nightly' + #local ideurl='https://www.arduino.cc/download.php?f=/arduino-nightly' + + local idever='1.8.10' + local ideurl="https://downloads.arduino.cc/arduino-$idever" + + echo "using Arduino IDE distribution ${idever}" + local ide_path=$1 local core_path=$2 local debug=$3 if [ "$WINDOWS" = "1" ]; then # Acquire needed packages from Windows package manager - choco install --no-progress python3 - export PATH="/c/Python37:$PATH" # Ensure it's live from now on... - cp /c/Python37/python.exe /c/Python37/python3.exe + choco install --no-progress python3 >& pylog.txt + # Parse the python instrall dir from the output log. Sorry, can't set it via choco on the free version + PYDIR=$(cat pylog.txt | grep "^Installed to:" | cut -f2 -d"'" | sed 's/C:\\/\/c\//') + echo "Detected python3 install dir: $PYDIR" + export PATH="$PYDIR:$PATH" # Ensure it's live from now on... + cp "$PYDIR/python.exe" "$PYDIR/python3.exe" choco install --no-progress unzip choco install --no-progress sed #choco install --no-progress golang - test -r arduino-nightly-windows.zip || wget -nv -O arduino-nightly-windows.zip https://www.arduino.cc/download.php?f=/arduino-nightly-windows.zip - unzip -q arduino-nightly-windows.zip + test -r arduino-windows.zip || wget -nv -O arduino-windows.zip "${ideurl}-windows.zip" + unzip -q arduino-windows.zip + mv arduino-${idever} arduino-distrib elif [ "$MACOSX" = "1" ]; then # MACOS only has next-to-obsolete Python2 installed. Install Python 3 from python.org wget https://www.python.org/ftp/python/3.7.4/python-3.7.4-macosx10.9.pkg @@ -173,15 +185,17 @@ function install_ide() # Install the Python3 certificates, because SSL connections fail w/o them and of course they aren't installed by default. ( cd "/Applications/Python 3.7/" && sudo "./Install Certificates.command" ) # Hack to place arduino-builder in the same spot as sane OSes - test -r arduino.zip || wget -O arduino.zip https://downloads.arduino.cc/arduino-nightly-macosx.zip - unzip -q arduino.zip - mv Arduino.app arduino-nightly - mv arduino-nightly/Contents/Java/* arduino-nightly/. + test -r arduino-macos.zip || wget -O arduino-macos.zip "${ideurl}-macosx.zip" + unzip -q arduino-macos.zip + mv Arduino.app arduino-distrib + mv arduino-distrib/Contents/Java/* arduino-distrib/. else - test -r arduino.tar.xz || wget -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz - tar xf arduino.tar.xz + #test -r arduino.tar.xz || wget -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz + test -r arduino-linux.tar.xz || wget -O arduino-linux.tar.xz "${ideurl}-linux64.tar.xz" + tar xf arduino-linux.tar.xz + mv arduino-${idever} arduino-distrib fi - mv arduino-nightly $ide_path + mv arduino-distrib $ide_path cd $ide_path/hardware mkdir esp8266com cd esp8266com diff --git a/tests/device/Makefile b/tests/device/Makefile index b34cb0dbb..dab0ef379 100644 --- a/tests/device/Makefile +++ b/tests/device/Makefile @@ -11,9 +11,9 @@ UPLOAD_PORT ?= $(shell ls /dev/tty* | grep -m 1 -i USB) UPLOAD_BAUD ?= 460800 UPLOAD_BOARD ?= nodemcu BS_DIR ?= libraries/BSTest -DEBUG_LEVEL ?= DebugLevel=None____ +DEBUG_LEVEL ?= lvl=None____ #FQBN ?= esp8266com:esp8266:generic:CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=4M1M,LwIPVariant=v2mss536,ResetMethod=none,Debug=Serial,$(DEBUG_LEVEL) -FQBN ?= esp8266com:esp8266:generic:xtal=80,FlashFreq=40,FlashMode=dio,baud=115200,eesz=4M1M,ip=lm2f,ResetMethod=none,dbg=Serial,$(DEBUG_LEVEL) +FQBN ?= esp8266com:esp8266:generic:xtal=160,FlashFreq=40,FlashMode=dio,baud=115200,eesz=4M1M,ip=lm2f,ResetMethod=none,dbg=Serial,$(DEBUG_LEVEL) BUILD_TOOL := $(ARDUINO_IDE_PATH)/arduino-builder TEST_CONFIG := test_env.cfg TEST_REPORT_XML := test_report.xml @@ -104,7 +104,7 @@ ifneq ("$(NO_RUN)","1") endif $(TEST_REPORT_XML): $(HARDWARE_DIR) virtualenv - $(SILENT)$(BS_DIR)/virtualenv/bin/xunitmerge $(shell find $(BUILD_DIR) -name 'test_result.xml' | xargs echo) $(TEST_REPORT_XML) + $(SILENT)$(BS_DIR)/xunitmerge $(shell find $(BUILD_DIR) -name 'test_result.xml' | xargs echo) $(TEST_REPORT_XML) $(TEST_REPORT_HTML): $(TEST_REPORT_XML) | virtualenv $(SILENT)$(BS_DIR)/virtualenv/bin/junit2html $< $@ @@ -125,6 +125,7 @@ virtualenv: clean: rm -rf $(BUILD_DIR) rm -rf $(HARDWARE_DIR) + rm -rf $(BS_DIR)/virtualenv rm -f $(TEST_REPORT_HTML) $(TEST_REPORT_XML) distclean: clean diff --git a/tests/device/libraries/BSTest/requirements.txt b/tests/device/libraries/BSTest/requirements.txt index d31484d2a..a65d9b170 100644 --- a/tests/device/libraries/BSTest/requirements.txt +++ b/tests/device/libraries/BSTest/requirements.txt @@ -3,6 +3,5 @@ junit-xml MarkupSafe pexpect pyserial -xunitmerge junit2html -poster +poster3 diff --git a/tests/device/libraries/BSTest/runner.py b/tests/device/libraries/BSTest/runner.py index 425ac2a42..97849a5e3 100644 --- a/tests/device/libraries/BSTest/runner.py +++ b/tests/device/libraries/BSTest/runner.py @@ -236,10 +236,10 @@ ser = None def spawn_port(port_name, baudrate=115200): global ser ser = serial.serial_for_url(port_name, baudrate=baudrate) - return fdpexpect.fdspawn(ser, 'wb', timeout=0) + return fdpexpect.fdspawn(ser, 'wb', timeout=0, encoding='cp437') def spawn_exec(name): - return pexpect.spawn(name, timeout=0) + return pexpect.spawn(name, timeout=0, encoding='cp437') def run_tests(spawn, name, mocks, env_vars): tw = BSTestRunner(spawn, name, mocks, env_vars) diff --git a/tests/device/libraries/BSTest/xmerge.py b/tests/device/libraries/BSTest/xmerge.py new file mode 100644 index 000000000..c10ca7297 --- /dev/null +++ b/tests/device/libraries/BSTest/xmerge.py @@ -0,0 +1,154 @@ +# Cloned from https://github.com/miki725/xunitmerge +# to fix a Python3 error. +# +# xunitmerge is MIT licensed by Miroslav Shubernetskiy https://github.com/miki725 +# +# The MIT License (MIT) +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from contextlib import contextmanager +from xml.etree import ElementTree as etree +from xml.sax.saxutils import quoteattr + +import six + + +CNAME_TAGS = ('system-out', 'skipped', 'error', 'failure') +CNAME_PATTERN = '' +TAG_PATTERN = '<{tag}{attrs}>{text}' + + +@contextmanager +def patch_etree_cname(etree): + """ + Patch ElementTree's _serialize_xml function so that it will + write text as CDATA tag for tags tags defined in CNAME_TAGS. + + >>> import re + >>> from xml.etree import ElementTree + >>> xml_string = ''' + ... + ... + ... Some output here + ... + ... + ... Skipped + ... + ... + ... Error here + ... + ... + ... Failure here + ... + ... + ... ''' + >>> tree = ElementTree.fromstring(xml_string) + >>> with patch_etree_cname(ElementTree): + ... saved = str(ElementTree.tostring(tree)) + >>> systemout = re.findall(r'(.*?)', saved)[0] + >>> print(systemout) + + >>> skipped = re.findall(r'()', saved)[0] + >>> print(skipped) + + >>> error = re.findall(r'()', saved)[0] + >>> print(error) + + >>> failure = re.findall(r'()', saved)[0] + >>> print(failure) + + """ + original_serialize = etree._serialize_xml + + def _serialize_xml(write, elem, *args, **kwargs): + if elem.tag in CNAME_TAGS: + attrs = ' '.join( + ['{}={}'.format(k, quoteattr(v)) + for k, v in sorted(elem.attrib.items())] + ) + attrs = ' ' + attrs if attrs else '' + text = CNAME_PATTERN.format(elem.text) + write(TAG_PATTERN.format( + tag=elem.tag, + attrs=attrs, + text=text + )) + else: + original_serialize(write, elem, *args, **kwargs) + + etree._serialize_xml = etree._serialize['xml'] = _serialize_xml + + yield + + etree._serialize_xml = etree._serialize['xml'] = original_serialize + + +def merge_trees(*trees): + """ + Merge all given XUnit ElementTrees into a single ElementTree. + This combines all of the children test-cases and also merges + all of the metadata of how many tests were executed, etc. + """ + first_tree = trees[0] + first_root = first_tree.getroot() + + if len(trees) == 0: + return first_tree + + for tree in trees[1:]: + root = tree.getroot() + + # append children elements (testcases) + first_root.extend(root.getchildren()) + + # combine root attributes which stores the number + # of executed tests, skipped tests, etc + for key, value in first_root.attrib.items(): + if not value.isdigit(): + continue + combined = six.text_type(int(value) + int(root.attrib.get(key, '0'))) + first_root.set(key, combined) + + return first_tree + + +def merge_xunit(files, output, callback=None): + """ + Merge the given xunit xml files into a single output xml file. + + If callback is not None, it will be called with the merged ElementTree + before the output file is written (useful for applying other fixes to + the merged file). This can either modify the element tree in place (and + return None) or return a completely new ElementTree to be written. + """ + trees = [] + + for f in files: + trees.append(etree.parse(f)) + + merged = merge_trees(*trees) + + if callback is not None: + result = callback(merged) + if result is not None: + merged = result + + with patch_etree_cname(etree): + merged.write(output, encoding='utf-8', xml_declaration=True) diff --git a/tests/device/libraries/BSTest/xunitmerge b/tests/device/libraries/BSTest/xunitmerge new file mode 100755 index 000000000..61a69f6ec --- /dev/null +++ b/tests/device/libraries/BSTest/xunitmerge @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# Cloned from https://github.com/miki725/xunitmerge +# to fix a Python3 error. +# +# xunitmerge is MIT licensed by Miroslav Shubernetskiy https://github.com/miki725 +# +# The MIT License (MIT) +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import argparse +from xmerge import merge_xunit + + +parser = argparse.ArgumentParser( + description='Utility for merging multiple XUnit xml reports ' + 'into a single xml report.', +) +parser.add_argument( + 'report', + nargs='+', + type=argparse.FileType('r'), + help='Path of XUnit xml report. Multiple can be provided.', +) +parser.add_argument( + 'output', + help='Path where merged of XUnit will be saved.', +) + + +if __name__ == '__main__': + args = parser.parse_args() + merge_xunit(args.report, args.output) diff --git a/tests/device/test_ClientContext/test_ClientContext.py b/tests/device/test_ClientContext/test_ClientContext.py index ae29bcd2f..6650e1cfd 100644 --- a/tests/device/test_ClientContext/test_ClientContext.py +++ b/tests/device/test_ClientContext/test_ClientContext.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + from mock_decorators import setup, teardown from flask import Flask, request from threading import Thread @@ -21,7 +23,7 @@ def setup_tcpsrv(e): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) for port in range(8266, 8285 + 1): try: - print >>sys.stderr, 'trying port', port + print ('trying port %d' %port, file=sys.stderr) server_address = ("0.0.0.0", port) sock.bind(server_address) sock.listen(1) @@ -31,17 +33,17 @@ def setup_tcpsrv(e): print >>sys.stderr, 'busy' if not running: return - print >>sys.stderr, 'starting up on %s port %s' % server_address - print >>sys.stderr, 'waiting for connections' + print ('starting up on %s port %s' % server_address, file=sys.stderr) + print ( 'waiting for connections', file=sys.stderr) while running: - print >>sys.stderr, 'loop' + print ('loop', file=sys.stderr) readable, writable, errored = select.select([sock], [], [], 1.0) if readable: connection, client_address = sock.accept() try: - print >>sys.stderr, 'client connected:', client_address + print('client connected: %s' % str(client_address), file=sys.stderr) finally: - print >>sys.stderr, 'close' + print ('close', file=sys.stderr) connection.shutdown(socket.SHUT_RDWR) connection.close() @@ -54,7 +56,7 @@ def teardown_tcpsrv(e): global thread global running - print >>sys.stderr, 'closing' + print ('closing', file=sys.stderr) running = False thread.join() return 0 diff --git a/tests/device/test_http_client/test_http_client.py b/tests/device/test_http_client/test_http_client.py index d991ca985..83bc4e8c1 100644 --- a/tests/device/test_http_client/test_http_client.py +++ b/tests/device/test_http_client/test_http_client.py @@ -1,7 +1,7 @@ from mock_decorators import setup, teardown from flask import Flask, request, redirect from threading import Thread -import urllib2 +import urllib import os import ssl import time @@ -20,7 +20,7 @@ def setup_http_get(e): return 'Server shutting down...' @app.route("/", methods = ['GET', 'POST']) def root(): - print('Got data: ' + request.data); + print('Got data: ' + request.data.decode()); return 'hello!!!' @app.route("/data") def get_data(): @@ -48,7 +48,7 @@ def setup_http_get(e): @teardown('HTTP GET & POST requests') def teardown_http_get(e): - response = urllib2.urlopen('http://localhost:8088/shutdown') + response = urllib.request.urlopen('http://localhost:8088/shutdown') html = response.read() time.sleep(1) # avoid address in use error on macOS @@ -86,6 +86,6 @@ def teardown_http_get(e): ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE p = os.path.dirname(os.path.abspath(__file__)) - response = urllib2.urlopen('https://localhost:8088/shutdown', context=ctx) + response = urllib.request.urlopen('https://localhost:8088/shutdown', context=ctx) html = response.read() diff --git a/tests/device/test_http_server/test_http_server.py b/tests/device/test_http_server/test_http_server.py index 319a8a710..e184e367e 100644 --- a/tests/device/test_http_server/test_http_server.py +++ b/tests/device/test_http_server/test_http_server.py @@ -1,10 +1,9 @@ from collections import OrderedDict from mock_decorators import setup, teardown from threading import Thread -from poster.encode import MultipartParam -from poster.encode import multipart_encode -from poster.streaminghttp import register_openers -import urllib2 +from poster3.encode import MultipartParam +from poster3.encode import multipart_encode +from poster3.streaminghttp import register_openers import urllib def http_test(res, url, get=None, post=None): @@ -13,8 +12,8 @@ def http_test(res, url, get=None, post=None): if get: url += '?' + urllib.urlencode(get) if post: - post = urllib.urlencode(post) - request = urllib2.urlopen(url, post, 2) + post = urllib.parse.quote(post) + request = urllib.request.urlopen(url, post, 2) response = request.read() except: return 1 @@ -60,8 +59,8 @@ def setup_http_upload(e): register_openers() p = MultipartParam("file", "0123456789abcdef", "test.txt", "text/plain; charset=utf8") datagen, headers = multipart_encode( [("var4", "val with spaces"), p] ) - request = urllib2.Request('http://etd.local/upload', datagen, headers) - response = urllib2.urlopen(request, None, 2).read() + request = urllib.request('http://etd.local/upload', datagen, headers) + response = urllib.request.urlopen(request, None, 2).read() except: return 1 if response != 'test.txt:16\nvar4 = val with spaces': diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index 67af7ed2c..db0d8c889 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -313,6 +313,15 @@ uart_get_baudrate(uart_t* uart) return uart->baud_rate; } +uint8_t +uart_get_bit_length(const int uart_nr) +{ + uint8_t width = ((uart_nr % 16) >> 2) + 5; + uint8_t parity = (uart_nr >> 5) + 1; + uint8_t stop = uart_nr % 4; + return (width + parity + stop + 1); +} + uart_t* uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) { diff --git a/tests/restyle-all.sh b/tests/restyle-all.sh deleted file mode 100755 index e751f476d..000000000 --- a/tests/restyle-all.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -set -e - -org=$(cd ${0%/*}; pwd) -cd ${org}/.. -pwd -test -d cores/esp8266 -test -d libraries - -# this warning question will be removed after restyle-all.sh is renamed to restyle.sh -echo "This is dangerous if you have modified your local repository" -echo "type iknowwhatido to continue" -read ans -test "$ans" = iknowwhatido || exit 1 - -for d in cores/esp8266 libraries; do - for e in c cpp h; do - find $d -name "*.$e" -exec \ - astyle \ - --suffix=none \ - --options=${org}/astyle_core.conf {} \; - done -done - -for d in libraries; do - find $d -name "*.ino" -exec \ - astyle \ - --suffix=none \ - --options=${org}/astyle_examples.conf {} \; -done diff --git a/tests/restyle-examples-only.sh b/tests/restyle-examples-only.sh new file mode 100755 index 000000000..513a89190 --- /dev/null +++ b/tests/restyle-examples-only.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +org=$(cd ${0%/*}; pwd) +cd ${org}/.. +pwd +test -d cores/esp8266 +test -d libraries + +# in a near future, restyle-all.sh will be renamed to restyle.sh +# and will be checked against CI + +for d in libraries; do + find $d -name "*.ino" -exec \ + astyle \ + --suffix=none \ + --options=${org}/astyle_examples.conf {} \; +done diff --git a/tests/restyle.sh b/tests/restyle.sh index 513a89190..b085d33dc 100755 --- a/tests/restyle.sh +++ b/tests/restyle.sh @@ -8,12 +8,25 @@ pwd test -d cores/esp8266 test -d libraries -# in a near future, restyle-all.sh will be renamed to restyle.sh -# and will be checked against CI +#all="cores/esp8266 libraries" +all=" +libraries/ESP8266mDNS +libraries/Wire +cores/esp8266/core_esp8266_si2c.cpp +" + +for d in $all; do + for e in c cpp h; do + find $d -name "*.$e" -exec \ + astyle \ + --suffix=none \ + --options=${org}/astyle_core.conf {} \; + done +done for d in libraries; do - find $d -name "*.ino" -exec \ - astyle \ - --suffix=none \ - --options=${org}/astyle_examples.conf {} \; + find $d -name "*.ino" -exec \ + astyle \ + --suffix=none \ + --options=${org}/astyle_examples.conf {} \; done diff --git a/tests/run_CI_locally.sh b/tests/run_CI_locally.sh index 65b91277f..a370461f6 100755 --- a/tests/run_CI_locally.sh +++ b/tests/run_CI_locally.sh @@ -84,6 +84,8 @@ done cp tests/platformio.sh tests/platformio-custom.sh sed -i 's,pip ,pip2 ,g' tests/platformio-custom.sh +git submodule update --init + export HOME="${TMPCI}" export TRAVIS_BUILD_DIR="${TMPCI}" export BUILD_TYPE="$BUILD_TYPE" diff --git a/tools/TZupdate.sh b/tools/TZupdate.sh new file mode 100755 index 000000000..4c0d09e80 --- /dev/null +++ b/tools/TZupdate.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# this shell script refreshes world timezone definitions in +# cores/esp8266/TZ.h +# +# to run it, use: +# /path/to/TZupdate.sh +# tools/TZupdate.sh +# ./TZupdate.sh + +dir=$(cd ${0%/*} 2>/dev/null; pwd) +base=${0##*/} + +csv=https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv + +set -e + +tz_tmpdir=$(mktemp -d) +trap 'rm -r $tz_tmpdir' EXIT + +input=$tz_tmpdir/zones.csv +names=$tz_tmpdir/names.txt +values=$tz_tmpdir/values.txt + +wget -O $input $csv || curl $csv > $input + +sed -e 's/^[^,]*,//g' -e 's,^,PSTR(,g' -e 's,$,),g' < $input > $values +sed -e 's/^\([^,]*\),.*/#define TZ_\1/g' -e 's,["],,g' < $input | tr '/\-+' '_mp' > $names + +( + +cat << EOF + +// autogenerated from $csv +// by script /tools/${base} +// $(date -u) +// +// This database is autogenerated from IANA timezone database +// https://www.iana.org/time-zones +// and can be updated on demand in this repository +// or by yourself using the above script + +#ifndef TZDB_H +#define TZDB_H + +EOF + +paste $names $values + +cat << EOF + +#endif // TZDB_H +EOF + +) > $tz_tmpdir/TZ.h + +backup=$(date +%s) +mv ${dir}/../cores/esp8266/TZ.h ${dir}/../cores/esp8266/TZ.h.$backup +mv $tz_tmpdir/TZ.h ${dir}/../cores/esp8266/TZ.h + +cat << EOF + +Done: + '${dir}/../cores/esp8266/TZ.h' is updated + +Diff: +----8<-------8<------8<--- +$(diff -u ${dir}/../cores/esp8266/TZ.h.$backup ${dir}/../cores/esp8266/TZ.h) +--->8----->8------>8------ + +EOF diff --git a/tools/boards.txt.py b/tools/boards.txt.py index 3d8c947b2..ec29c5d27 100755 --- a/tools/boards.txt.py +++ b/tools/boards.txt.py @@ -905,6 +905,10 @@ macros = { ( '.menu.FlashFreq.40.build.flash_freq', '40' ), ( '.menu.FlashFreq.80', '80MHz' ), ( '.menu.FlashFreq.80.build.flash_freq', '80' ), + ( '.menu.FlashFreq.20', '20MHz' ), + ( '.menu.FlashFreq.20.build.flash_freq', '20' ), + ( '.menu.FlashFreq.26', '26MHz' ), + ( '.menu.FlashFreq.26.build.flash_freq', '26' ), ]), 'flashfreq_40': collections.OrderedDict([ @@ -1181,6 +1185,11 @@ def flash_map (flashsize_kb, fs_kb = 0): rfcal_size_kb = 4 sdkwifi_size_kb = 12 fs_end = (flashsize_kb - sdkwifi_size_kb - rfcal_size_kb - eeprom_size_kb) * 1024 + + # For legacy reasons (#6531), the EEPROM sector needs to be at the old + # FS_end calculated without regards to block size + eeprom_start = fs_end + rfcal_addr = (flashsize_kb - sdkwifi_size_kb - rfcal_size_kb) * 1024 if flashsize_kb <= 1024: max_upload_size = (flashsize_kb - (fs_kb + eeprom_size_kb + rfcal_size_kb + sdkwifi_size_kb)) * 1024 - reserved @@ -1264,6 +1273,13 @@ def flash_map (flashsize_kb, fs_kb = 0): print("PROVIDE ( _FS_end = 0x%08X );" % (0x40200000 + fs_end)) print("PROVIDE ( _FS_page = 0x%X );" % page) print("PROVIDE ( _FS_block = 0x%X );" % fs_blocksize) + print("PROVIDE ( _EEPROM_start = 0x%08x );" % (0x40200000 + eeprom_start)) + # Re-add deprecated symbols pointing to the same address as the new standard ones + print("/* The following symbols are DEPRECATED and will be REMOVED in a future release */") + print("PROVIDE ( _SPIFFS_start = 0x%08X );" % (0x40200000 + fs_start)) + print("PROVIDE ( _SPIFFS_end = 0x%08X );" % (0x40200000 + fs_end)) + print("PROVIDE ( _SPIFFS_page = 0x%X );" % page) + print("PROVIDE ( _SPIFFS_block = 0x%X );" % fs_blocksize) print("") print('INCLUDE "local.eagle.app.v6.common.ld"') @@ -1350,13 +1366,15 @@ def led (default,max): def sdk (): return { 'sdk': collections.OrderedDict([ - ('.menu.sdk.nonosdk222_100', 'nonos-sdk 2.2.1+100 (testing)'), - ('.menu.sdk.nonosdk222_100.build.sdk', 'NONOSDK22y'), + ('.menu.sdk.nonosdk222_100', 'nonos-sdk 2.2.1+100 (190703 approved)'), + ('.menu.sdk.nonosdk222_100.build.sdk', 'NONOSDK22x_190703'), + ('.menu.sdk.nonosdk222_111', 'nonos-sdk 2.2.1+111 (191024 testing)'), + ('.menu.sdk.nonosdk222_111.build.sdk', 'NONOSDK22x_191024'), + # ('.menu.sdk.nonosdk222_61', 'nonos-sdk 2.2.1+61 (190313 testing)'), + # ('.menu.sdk.nonosdk222_61.build.sdk', 'NONOSDK22x_190313'), ('.menu.sdk.nonosdk221', 'nonos-sdk 2.2.1 (legacy)'), ('.menu.sdk.nonosdk221.build.sdk', 'NONOSDK221'), - # ('.menu.sdk.nonosdk222_61', 'nonos-sdk 2.2.1+61 (testing)'), - # ('.menu.sdk.nonosdk222_61.build.sdk', 'NONOSDK22x'), - ('.menu.sdk.nonosdk3v0', 'nonos-sdk pre-3 (known issues)'), + ('.menu.sdk.nonosdk3v0', 'nonos-sdk pre-3 (180626 known issues)'), ('.menu.sdk.nonosdk3v0.build.sdk', 'NONOSDK3V0'), ]) } diff --git a/tools/esptool b/tools/esptool index 9ad444a6e..1319c49ad 160000 --- a/tools/esptool +++ b/tools/esptool @@ -1 +1 @@ -Subproject commit 9ad444a6e06e58833d5e6044c1d5f3eb3dd56023 +Subproject commit 1319c49adb2fe99d2981151ff781930d6ed62729 diff --git a/tools/platformio-build.py b/tools/platformio-build.py index de225fba1..6c52fd670 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -164,21 +164,26 @@ if "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK3" in flatten_cppdefines: CPPDEFINES=[("NONOSDK3V0", 1)], LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK3V0")] ) -elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x" in flatten_cppdefines: - env.Append( - CPPDEFINES=[("NONOSDK22x", 1)], - LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x")] - ) elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK221" in flatten_cppdefines: #(previous default) env.Append( CPPDEFINES=[("NONOSDK221", 1)], LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK221")] ) -else: #(default) elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22y" in flatten_cppdefines: +elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_190313" in flatten_cppdefines: env.Append( - CPPDEFINES=[("NONOSDK22y", 1)], - LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22y")] + CPPDEFINES=[("NONOSDK22x_190313", 1)], + LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_190313")] + ) +elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_191024" in flatten_cppdefines: + env.Append( + CPPDEFINES=[("NONOSDK22x_191024", 1)], + LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_191024")] + ) +else: #(default) if "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_190703" in flatten_cppdefines: + env.Append( + CPPDEFINES=[("NONOSDK22x_190703", 1)], + LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_190703")] ) # diff --git a/tools/sdk/include/ets_sys.h b/tools/sdk/include/ets_sys.h index da1f3897a..e30607eed 100644 --- a/tools/sdk/include/ets_sys.h +++ b/tools/sdk/include/ets_sys.h @@ -33,6 +33,17 @@ extern "C" { #endif +/* + * This "print character function" prototype is modeled after the argument for + * ets_install_putc1() found in "ESP8266_NONOS_SDK/include/osapi.h". This + * deviates away from the familiar C library definition of putchar; however, it + * agrees with the code we are working with. Note, in the ROM some "printf + * character functions" always return 0 (uart_tx_one_char and ets_putc), some + * return last character printed (buildin _putc1), and some return nothing + * (ets_write_char). Using a void return type safely represents them all. + */ +typedef void (*fp_putc_t)(char); + typedef uint32_t ETSSignal; typedef uint32_t ETSParam; @@ -205,15 +216,41 @@ char *ets_strstr(const char *haystack, const char *needle); int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4))); int ets_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); -void ets_install_putc1(void* routine); +void ets_install_putc1(fp_putc_t routine); void ets_isr_mask(int intr); void ets_isr_unmask(int intr); void ets_isr_attach(int intr, int_handler_t handler, void *arg); void ets_intr_lock(); void ets_intr_unlock(); int ets_vsnprintf(char * s, size_t n, const char * format, va_list arg) __attribute__ ((format (printf, 3, 0))); -int ets_vprintf(int (*print_function)(int), const char * format, va_list arg) __attribute__ ((format (printf, 2, 0))); -int ets_putc(int); +int ets_vprintf(fp_putc_t print_function, const char * format, va_list arg) __attribute__ ((format (printf, 2, 0))); + +/* + * ets_putc(), a "print character function" in ROM, prints a character to a + * UART. It always returns 0; however, the prototype here is defined with void + * return to make compatible with other usages of fp_putc_t. ets_putc() provides + * a "raw", print as is, interface. '\r' and '\n' are each printed exactly as is + * w/o addition. For a "cooked" interface use ets_uart_putc1(). + * The use of this function requires a prior setup call to uart_buff_switch() to + * select the UART. + */ +void ets_putc(char); + +/* + * ets_uart_putc1(), a "print character function" in ROM, prints a character to + * a UART. It returns the character printed; however, the prototype here is + * defined with void return to make compatible with other usages of fp_putc_t. + * This function provides additional processing to characters '\r' and '\n'. It + * filters out '\r'. When called with '\n', it prints characters '\r' and '\n'. + * This is sometimes refered to as a "cooked" interface. For a "raw", print as + * is, interface use ets_putc(). The use of this function requires a prior setup + * call to uart_buff_switch() to select the UART. + * ets_uart_putc1() is used internally by ets_uart_printf. It is also the + * function that gets installed by ets_uart_install_printf through a call to + * ets_install_putc1. + */ +void ets_uart_putc1(char); + bool ets_task(ETSTask task, uint8 prio, ETSEvent *queue, uint8 qlen); bool ets_post(uint8 prio, ETSSignal sig, ETSParam par); void ets_update_cpu_frequency(uint32_t ticks_per_us); diff --git a/tools/sdk/include/sntp.h b/tools/sdk/include/sntp.h index 4e842d743..294711057 100644 --- a/tools/sdk/include/sntp.h +++ b/tools/sdk/include/sntp.h @@ -5,11 +5,10 @@ #include "lwip/init.h" #include "lwip/ip_addr.h" - #if LWIP_VERSION_MAJOR == 1 -#define ipv4_addr_t ip_addr_t +#include "lwip/sntp.h" #else -typedef struct ip4_addr ipv4_addr_t; +#include "lwip/apps/sntp.h" #endif #ifdef __cplusplus @@ -32,48 +31,6 @@ sint8 sntp_get_timezone(void); * SNTP set time_zone (default GMT + 8) */ bool sntp_set_timezone(sint8 timezone); -/** - * Initialize this module. - * Send out request instantly or after SNTP_STARTUP_DELAY(_FUNC). - */ -void sntp_init(void); -/** - * Stop this module. - */ -void sntp_stop(void); -/** - * Initialize one of the NTP servers by IP address - * - * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS - * @param dnsserver IP address of the NTP server to set - */ -void sntp_setserver(unsigned char idx, ipv4_addr_t *addr); -/** - * Obtain one of the currently configured by IP address (or DHCP) NTP servers - * - * @param numdns the index of the NTP server - * @return IP address of the indexed NTP server or "ipv4_addr_any" if the NTP - * server has not been configured by address (or at all). - */ -ipv4_addr_t sntp_getserver(unsigned char idx); -/** - * Initialize one of the NTP servers by name - * - * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS,now sdk support SNTP_MAX_SERVERS = 3 - * @param dnsserver DNS name of the NTP server to set, to be resolved at contact time - */ -void sntp_setservername(unsigned char idx, char *server); -/** - * Obtain one of the currently configured by name NTP servers. - * - * @param numdns the index of the NTP server - * @return IP address of the indexed NTP server or NULL if the NTP - * server has not been configured by name (or at all) - */ -char *sntp_getservername(unsigned char idx); - -#define sntp_servermode_dhcp(x) - #ifdef __cplusplus } diff --git a/tools/sdk/ld/eagle.app.v6.common.ld.h b/tools/sdk/ld/eagle.app.v6.common.ld.h index 2f5735fff..383154e49 100644 --- a/tools/sdk/ld/eagle.app.v6.common.ld.h +++ b/tools/sdk/ld/eagle.app.v6.common.ld.h @@ -181,6 +181,9 @@ SECTIONS *libwps.a:(.literal.* .text.*) *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom0.text.* .irom.text .irom.text.*) + /* Constant strings in flash (PSTRs) */ + *(.irom0.pstr.*) + /* __FUNCTION__ locals */ *(.rodata._ZZ*__FUNCTION__) *(.rodata._ZZ*__PRETTY_FUNCTION__) diff --git a/tools/sdk/ld/eagle.flash.16m14m.ld b/tools/sdk/ld/eagle.flash.16m14m.ld index ccc5eb828..a67ef97d1 100644 --- a/tools/sdk/ld/eagle.flash.16m14m.ld +++ b/tools/sdk/ld/eagle.flash.16m14m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40400000 ); PROVIDE ( _FS_end = 0x411FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x411fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40400000 ); +PROVIDE ( _SPIFFS_end = 0x411FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.16m15m.ld b/tools/sdk/ld/eagle.flash.16m15m.ld index dcb650f66..bca117f76 100644 --- a/tools/sdk/ld/eagle.flash.16m15m.ld +++ b/tools/sdk/ld/eagle.flash.16m15m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40300000 ); PROVIDE ( _FS_end = 0x411FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x411fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40300000 ); +PROVIDE ( _SPIFFS_end = 0x411FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m.ld b/tools/sdk/ld/eagle.flash.1m.ld index 57c8ab2da..57c35adbc 100644 --- a/tools/sdk/ld/eagle.flash.1m.ld +++ b/tools/sdk/ld/eagle.flash.1m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402FB000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x0 ); PROVIDE ( _FS_block = 0x0 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402FB000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x0 ); +PROVIDE ( _SPIFFS_block = 0x0 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m128.ld b/tools/sdk/ld/eagle.flash.1m128.ld index 729f61398..f518f08fa 100644 --- a/tools/sdk/ld/eagle.flash.1m128.ld +++ b/tools/sdk/ld/eagle.flash.1m128.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402DB000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402DB000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m144.ld b/tools/sdk/ld/eagle.flash.1m144.ld index e644897c4..4a4f98651 100644 --- a/tools/sdk/ld/eagle.flash.1m144.ld +++ b/tools/sdk/ld/eagle.flash.1m144.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402D7000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402D7000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m160.ld b/tools/sdk/ld/eagle.flash.1m160.ld index 08b54f51f..289c0ad0f 100644 --- a/tools/sdk/ld/eagle.flash.1m160.ld +++ b/tools/sdk/ld/eagle.flash.1m160.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402D3000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402D3000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m192.ld b/tools/sdk/ld/eagle.flash.1m192.ld index 77ddac33d..2b71f41ac 100644 --- a/tools/sdk/ld/eagle.flash.1m192.ld +++ b/tools/sdk/ld/eagle.flash.1m192.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402CB000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402CB000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m256.ld b/tools/sdk/ld/eagle.flash.1m256.ld index b197a2b1c..6a90e9fcb 100644 --- a/tools/sdk/ld/eagle.flash.1m256.ld +++ b/tools/sdk/ld/eagle.flash.1m256.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402BB000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402BB000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m512.ld b/tools/sdk/ld/eagle.flash.1m512.ld index 811f7a150..e3c6db39e 100644 --- a/tools/sdk/ld/eagle.flash.1m512.ld +++ b/tools/sdk/ld/eagle.flash.1m512.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x4027B000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x4027B000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.1m64.ld b/tools/sdk/ld/eagle.flash.1m64.ld index 92447c0f9..87032bd02 100644 --- a/tools/sdk/ld/eagle.flash.1m64.ld +++ b/tools/sdk/ld/eagle.flash.1m64.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x402EB000 ); PROVIDE ( _FS_end = 0x402FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x402fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x402EB000 ); +PROVIDE ( _SPIFFS_end = 0x402FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.2m.ld b/tools/sdk/ld/eagle.flash.2m.ld index 00b11a073..e39377b28 100644 --- a/tools/sdk/ld/eagle.flash.2m.ld +++ b/tools/sdk/ld/eagle.flash.2m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x403FB000 ); PROVIDE ( _FS_end = 0x403FB000 ); PROVIDE ( _FS_page = 0x0 ); PROVIDE ( _FS_block = 0x0 ); +PROVIDE ( _EEPROM_start = 0x403fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x403FB000 ); +PROVIDE ( _SPIFFS_end = 0x403FB000 ); +PROVIDE ( _SPIFFS_page = 0x0 ); +PROVIDE ( _SPIFFS_block = 0x0 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.2m128.ld b/tools/sdk/ld/eagle.flash.2m128.ld index 1e96769b9..fd71ce458 100644 --- a/tools/sdk/ld/eagle.flash.2m128.ld +++ b/tools/sdk/ld/eagle.flash.2m128.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x403E0000 ); PROVIDE ( _FS_end = 0x403FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x403fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x403E0000 ); +PROVIDE ( _SPIFFS_end = 0x403FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.2m1m.ld b/tools/sdk/ld/eagle.flash.2m1m.ld index 21195281e..172636740 100644 --- a/tools/sdk/ld/eagle.flash.2m1m.ld +++ b/tools/sdk/ld/eagle.flash.2m1m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40300000 ); PROVIDE ( _FS_end = 0x403FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x403fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40300000 ); +PROVIDE ( _SPIFFS_end = 0x403FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.2m256.ld b/tools/sdk/ld/eagle.flash.2m256.ld index 4b59a1b15..6b72dc167 100644 --- a/tools/sdk/ld/eagle.flash.2m256.ld +++ b/tools/sdk/ld/eagle.flash.2m256.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x403C0000 ); PROVIDE ( _FS_end = 0x403FB000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x403fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x403C0000 ); +PROVIDE ( _SPIFFS_end = 0x403FB000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.2m512.ld b/tools/sdk/ld/eagle.flash.2m512.ld index a6015dd2d..eab32089f 100644 --- a/tools/sdk/ld/eagle.flash.2m512.ld +++ b/tools/sdk/ld/eagle.flash.2m512.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40380000 ); PROVIDE ( _FS_end = 0x403FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x403fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40380000 ); +PROVIDE ( _SPIFFS_end = 0x403FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.4m.ld b/tools/sdk/ld/eagle.flash.4m.ld index f77c95ae1..31c059807 100644 --- a/tools/sdk/ld/eagle.flash.4m.ld +++ b/tools/sdk/ld/eagle.flash.4m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x405FB000 ); PROVIDE ( _FS_end = 0x405FB000 ); PROVIDE ( _FS_page = 0x0 ); PROVIDE ( _FS_block = 0x0 ); +PROVIDE ( _EEPROM_start = 0x405fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x405FB000 ); +PROVIDE ( _SPIFFS_end = 0x405FB000 ); +PROVIDE ( _SPIFFS_page = 0x0 ); +PROVIDE ( _SPIFFS_block = 0x0 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.4m1m.ld b/tools/sdk/ld/eagle.flash.4m1m.ld index 4d295e306..cf6bd7aa1 100644 --- a/tools/sdk/ld/eagle.flash.4m1m.ld +++ b/tools/sdk/ld/eagle.flash.4m1m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40500000 ); PROVIDE ( _FS_end = 0x405FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x405fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40500000 ); +PROVIDE ( _SPIFFS_end = 0x405FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.4m2m.ld b/tools/sdk/ld/eagle.flash.4m2m.ld index d7034237c..9723b9490 100644 --- a/tools/sdk/ld/eagle.flash.4m2m.ld +++ b/tools/sdk/ld/eagle.flash.4m2m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40400000 ); PROVIDE ( _FS_end = 0x405FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x405fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40400000 ); +PROVIDE ( _SPIFFS_end = 0x405FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.4m3m.ld b/tools/sdk/ld/eagle.flash.4m3m.ld index 604ed2fa5..a85a1da02 100644 --- a/tools/sdk/ld/eagle.flash.4m3m.ld +++ b/tools/sdk/ld/eagle.flash.4m3m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40300000 ); PROVIDE ( _FS_end = 0x405FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x405fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40300000 ); +PROVIDE ( _SPIFFS_end = 0x405FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.512k.ld b/tools/sdk/ld/eagle.flash.512k.ld index 4f421ab8e..44ecf057d 100644 --- a/tools/sdk/ld/eagle.flash.512k.ld +++ b/tools/sdk/ld/eagle.flash.512k.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x4027B000 ); PROVIDE ( _FS_end = 0x4027B000 ); PROVIDE ( _FS_page = 0x0 ); PROVIDE ( _FS_block = 0x0 ); +PROVIDE ( _EEPROM_start = 0x4027b000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x4027B000 ); +PROVIDE ( _SPIFFS_end = 0x4027B000 ); +PROVIDE ( _SPIFFS_page = 0x0 ); +PROVIDE ( _SPIFFS_block = 0x0 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.512k128.ld b/tools/sdk/ld/eagle.flash.512k128.ld index 7a3c573af..63c5a4c6e 100644 --- a/tools/sdk/ld/eagle.flash.512k128.ld +++ b/tools/sdk/ld/eagle.flash.512k128.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x4025B000 ); PROVIDE ( _FS_end = 0x4027B000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x4027b000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x4025B000 ); +PROVIDE ( _SPIFFS_end = 0x4027B000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.512k32.ld b/tools/sdk/ld/eagle.flash.512k32.ld index 1334b8d8e..ef031f4cd 100644 --- a/tools/sdk/ld/eagle.flash.512k32.ld +++ b/tools/sdk/ld/eagle.flash.512k32.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40273000 ); PROVIDE ( _FS_end = 0x4027B000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x4027b000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40273000 ); +PROVIDE ( _SPIFFS_end = 0x4027B000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.512k64.ld b/tools/sdk/ld/eagle.flash.512k64.ld index fdea4da8e..3cd80b5da 100644 --- a/tools/sdk/ld/eagle.flash.512k64.ld +++ b/tools/sdk/ld/eagle.flash.512k64.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x4026B000 ); PROVIDE ( _FS_end = 0x4027B000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x1000 ); +PROVIDE ( _EEPROM_start = 0x4027b000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x4026B000 ); +PROVIDE ( _SPIFFS_end = 0x4027B000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x1000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.8m6m.ld b/tools/sdk/ld/eagle.flash.8m6m.ld index 2b2671602..107745422 100644 --- a/tools/sdk/ld/eagle.flash.8m6m.ld +++ b/tools/sdk/ld/eagle.flash.8m6m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40400000 ); PROVIDE ( _FS_end = 0x409FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x409fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40400000 ); +PROVIDE ( _SPIFFS_end = 0x409FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.8m7m.ld b/tools/sdk/ld/eagle.flash.8m7m.ld index 2dae7ac7f..099f801f1 100644 --- a/tools/sdk/ld/eagle.flash.8m7m.ld +++ b/tools/sdk/ld/eagle.flash.8m7m.ld @@ -18,5 +18,11 @@ PROVIDE ( _FS_start = 0x40300000 ); PROVIDE ( _FS_end = 0x409FA000 ); PROVIDE ( _FS_page = 0x100 ); PROVIDE ( _FS_block = 0x2000 ); +PROVIDE ( _EEPROM_start = 0x409fb000 ); +/* The following symbols are DEPRECATED and will be REMOVED in a future release */ +PROVIDE ( _SPIFFS_start = 0x40300000 ); +PROVIDE ( _SPIFFS_end = 0x409FA000 ); +PROVIDE ( _SPIFFS_page = 0x100 ); +PROVIDE ( _SPIFFS_block = 0x2000 ); INCLUDE "local.eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.rom.addr.v6.ld b/tools/sdk/ld/eagle.rom.addr.v6.ld index 144c72b55..84c5e3acf 100644 --- a/tools/sdk/ld/eagle.rom.addr.v6.ld +++ b/tools/sdk/ld/eagle.rom.addr.v6.ld @@ -119,6 +119,8 @@ PROVIDE ( ets_install_external_printf = 0x40002450 ); PROVIDE ( ets_install_putc1 = 0x4000242c ); PROVIDE ( ets_install_putc2 = 0x4000248c ); PROVIDE ( ets_install_uart_printf = 0x40002438 ); +/* Undocumented function to print character to UART */ +PROVIDE ( ets_uart_putc1 = 0x40001dcc ); /* permanently hide reimplemented ets_intr_*lock(), see #6484 PROVIDE ( ets_intr_lock = 0x40000f74 ); PROVIDE ( ets_intr_unlock = 0x40000f80 ); diff --git a/tools/sdk/lib/NONOSDK22x/commitlog-from-221.txt.gz b/tools/sdk/lib/NONOSDK22x_190313/commitlog-from-221.txt.gz similarity index 100% rename from tools/sdk/lib/NONOSDK22x/commitlog-from-221.txt.gz rename to tools/sdk/lib/NONOSDK22x_190313/commitlog-from-221.txt.gz diff --git a/tools/sdk/lib/NONOSDK22x/libairkiss.a b/tools/sdk/lib/NONOSDK22x_190313/libairkiss.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libairkiss.a rename to tools/sdk/lib/NONOSDK22x_190313/libairkiss.a diff --git a/tools/sdk/lib/NONOSDK22x/libcrypto.a b/tools/sdk/lib/NONOSDK22x_190313/libcrypto.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libcrypto.a rename to tools/sdk/lib/NONOSDK22x_190313/libcrypto.a diff --git a/tools/sdk/lib/NONOSDK22x/libespnow.a b/tools/sdk/lib/NONOSDK22x_190313/libespnow.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libespnow.a rename to tools/sdk/lib/NONOSDK22x_190313/libespnow.a diff --git a/tools/sdk/lib/NONOSDK22x/libmain.a b/tools/sdk/lib/NONOSDK22x_190313/libmain.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libmain.a rename to tools/sdk/lib/NONOSDK22x_190313/libmain.a diff --git a/tools/sdk/lib/NONOSDK22x/libnet80211.a b/tools/sdk/lib/NONOSDK22x_190313/libnet80211.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libnet80211.a rename to tools/sdk/lib/NONOSDK22x_190313/libnet80211.a diff --git a/tools/sdk/lib/NONOSDK22x/libphy.a b/tools/sdk/lib/NONOSDK22x_190313/libphy.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libphy.a rename to tools/sdk/lib/NONOSDK22x_190313/libphy.a diff --git a/tools/sdk/lib/NONOSDK22x/libpp.a b/tools/sdk/lib/NONOSDK22x_190313/libpp.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libpp.a rename to tools/sdk/lib/NONOSDK22x_190313/libpp.a diff --git a/tools/sdk/lib/NONOSDK22x/libsmartconfig.a b/tools/sdk/lib/NONOSDK22x_190313/libsmartconfig.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libsmartconfig.a rename to tools/sdk/lib/NONOSDK22x_190313/libsmartconfig.a diff --git a/tools/sdk/lib/NONOSDK22x/libwpa.a b/tools/sdk/lib/NONOSDK22x_190313/libwpa.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libwpa.a rename to tools/sdk/lib/NONOSDK22x_190313/libwpa.a diff --git a/tools/sdk/lib/NONOSDK22x/libwpa2.a b/tools/sdk/lib/NONOSDK22x_190313/libwpa2.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libwpa2.a rename to tools/sdk/lib/NONOSDK22x_190313/libwpa2.a diff --git a/tools/sdk/lib/NONOSDK22x/libwps.a b/tools/sdk/lib/NONOSDK22x_190313/libwps.a similarity index 100% rename from tools/sdk/lib/NONOSDK22x/libwps.a rename to tools/sdk/lib/NONOSDK22x_190313/libwps.a diff --git a/tools/sdk/lib/NONOSDK22x/version b/tools/sdk/lib/NONOSDK22x_190313/version similarity index 100% rename from tools/sdk/lib/NONOSDK22x/version rename to tools/sdk/lib/NONOSDK22x_190313/version diff --git a/tools/sdk/lib/NONOSDK22y/commitlog-from-22x.txt.gz b/tools/sdk/lib/NONOSDK22x_190703/commitlog-from-22x-190313.txt.gz similarity index 100% rename from tools/sdk/lib/NONOSDK22y/commitlog-from-22x.txt.gz rename to tools/sdk/lib/NONOSDK22x_190703/commitlog-from-22x-190313.txt.gz diff --git a/tools/sdk/lib/NONOSDK22y/libairkiss.a b/tools/sdk/lib/NONOSDK22x_190703/libairkiss.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libairkiss.a rename to tools/sdk/lib/NONOSDK22x_190703/libairkiss.a diff --git a/tools/sdk/lib/NONOSDK22y/libcrypto.a b/tools/sdk/lib/NONOSDK22x_190703/libcrypto.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libcrypto.a rename to tools/sdk/lib/NONOSDK22x_190703/libcrypto.a diff --git a/tools/sdk/lib/NONOSDK22y/libespnow.a b/tools/sdk/lib/NONOSDK22x_190703/libespnow.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libespnow.a rename to tools/sdk/lib/NONOSDK22x_190703/libespnow.a diff --git a/tools/sdk/lib/NONOSDK22y/libmain.a b/tools/sdk/lib/NONOSDK22x_190703/libmain.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libmain.a rename to tools/sdk/lib/NONOSDK22x_190703/libmain.a diff --git a/tools/sdk/lib/NONOSDK22y/libnet80211.a b/tools/sdk/lib/NONOSDK22x_190703/libnet80211.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libnet80211.a rename to tools/sdk/lib/NONOSDK22x_190703/libnet80211.a diff --git a/tools/sdk/lib/NONOSDK22y/libphy.a b/tools/sdk/lib/NONOSDK22x_190703/libphy.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libphy.a rename to tools/sdk/lib/NONOSDK22x_190703/libphy.a diff --git a/tools/sdk/lib/NONOSDK22y/libpp.a b/tools/sdk/lib/NONOSDK22x_190703/libpp.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libpp.a rename to tools/sdk/lib/NONOSDK22x_190703/libpp.a diff --git a/tools/sdk/lib/NONOSDK22y/libsmartconfig.a b/tools/sdk/lib/NONOSDK22x_190703/libsmartconfig.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libsmartconfig.a rename to tools/sdk/lib/NONOSDK22x_190703/libsmartconfig.a diff --git a/tools/sdk/lib/NONOSDK22y/libwpa.a b/tools/sdk/lib/NONOSDK22x_190703/libwpa.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libwpa.a rename to tools/sdk/lib/NONOSDK22x_190703/libwpa.a diff --git a/tools/sdk/lib/NONOSDK22y/libwpa2.a b/tools/sdk/lib/NONOSDK22x_190703/libwpa2.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libwpa2.a rename to tools/sdk/lib/NONOSDK22x_190703/libwpa2.a diff --git a/tools/sdk/lib/NONOSDK22y/libwps.a b/tools/sdk/lib/NONOSDK22x_190703/libwps.a similarity index 100% rename from tools/sdk/lib/NONOSDK22y/libwps.a rename to tools/sdk/lib/NONOSDK22x_190703/libwps.a diff --git a/tools/sdk/lib/NONOSDK22y/version b/tools/sdk/lib/NONOSDK22x_190703/version similarity index 100% rename from tools/sdk/lib/NONOSDK22y/version rename to tools/sdk/lib/NONOSDK22x_190703/version diff --git a/tools/sdk/lib/NONOSDK22x_191024/commitlog-from-22x-190703.txt.gz b/tools/sdk/lib/NONOSDK22x_191024/commitlog-from-22x-190703.txt.gz new file mode 100644 index 000000000..277cc8fa0 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/commitlog-from-22x-190703.txt.gz differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libairkiss.a b/tools/sdk/lib/NONOSDK22x_191024/libairkiss.a new file mode 100644 index 000000000..cfdcc8423 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libairkiss.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libcrypto.a b/tools/sdk/lib/NONOSDK22x_191024/libcrypto.a new file mode 100644 index 000000000..8a43cb727 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libcrypto.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libespnow.a b/tools/sdk/lib/NONOSDK22x_191024/libespnow.a new file mode 100644 index 000000000..92f6c9ab1 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libespnow.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libmain.a b/tools/sdk/lib/NONOSDK22x_191024/libmain.a new file mode 100644 index 000000000..58779578a Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libmain.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libnet80211.a b/tools/sdk/lib/NONOSDK22x_191024/libnet80211.a new file mode 100644 index 000000000..2d49d922e Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libnet80211.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libphy.a b/tools/sdk/lib/NONOSDK22x_191024/libphy.a new file mode 100644 index 000000000..cab912d28 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libphy.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libpp.a b/tools/sdk/lib/NONOSDK22x_191024/libpp.a new file mode 100644 index 000000000..2abbe7a3e Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libpp.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libsmartconfig.a b/tools/sdk/lib/NONOSDK22x_191024/libsmartconfig.a new file mode 100644 index 000000000..95aec76c6 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libsmartconfig.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libwpa.a b/tools/sdk/lib/NONOSDK22x_191024/libwpa.a new file mode 100644 index 000000000..41ad4876f Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libwpa.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libwpa2.a b/tools/sdk/lib/NONOSDK22x_191024/libwpa2.a new file mode 100644 index 000000000..07420c5bf Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libwpa2.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/libwps.a b/tools/sdk/lib/NONOSDK22x_191024/libwps.a new file mode 100644 index 000000000..ef0be1c35 Binary files /dev/null and b/tools/sdk/lib/NONOSDK22x_191024/libwps.a differ diff --git a/tools/sdk/lib/NONOSDK22x_191024/version b/tools/sdk/lib/NONOSDK22x_191024/version new file mode 100644 index 000000000..27a6cd4ec --- /dev/null +++ b/tools/sdk/lib/NONOSDK22x_191024/version @@ -0,0 +1 @@ +v2.2.1-111-gef6d0ed (shows as SDK:2.2.2-dev(5ab15d1) in debug mode) diff --git a/tools/sdk/lib/liblwip2-1460-feat.a b/tools/sdk/lib/liblwip2-1460-feat.a index 2194496de..4dc84bf8b 100644 Binary files a/tools/sdk/lib/liblwip2-1460-feat.a and b/tools/sdk/lib/liblwip2-1460-feat.a differ diff --git a/tools/sdk/lib/liblwip2-1460.a b/tools/sdk/lib/liblwip2-1460.a index 5acb4f95e..95c171203 100644 Binary files a/tools/sdk/lib/liblwip2-1460.a and b/tools/sdk/lib/liblwip2-1460.a differ diff --git a/tools/sdk/lib/liblwip2-536-feat.a b/tools/sdk/lib/liblwip2-536-feat.a index 10cc2a01e..6de957e3e 100644 Binary files a/tools/sdk/lib/liblwip2-536-feat.a and b/tools/sdk/lib/liblwip2-536-feat.a differ diff --git a/tools/sdk/lib/liblwip2-536.a b/tools/sdk/lib/liblwip2-536.a index 69e5e881d..04b92dc84 100644 Binary files a/tools/sdk/lib/liblwip2-536.a and b/tools/sdk/lib/liblwip2-536.a differ diff --git a/tools/sdk/lib/liblwip6-1460-feat.a b/tools/sdk/lib/liblwip6-1460-feat.a index 51db390d0..3256c5f9b 100644 Binary files a/tools/sdk/lib/liblwip6-1460-feat.a and b/tools/sdk/lib/liblwip6-1460-feat.a differ diff --git a/tools/sdk/lib/liblwip6-536-feat.a b/tools/sdk/lib/liblwip6-536-feat.a index d16ec2be8..4c8fea615 100644 Binary files a/tools/sdk/lib/liblwip6-536-feat.a and b/tools/sdk/lib/liblwip6-536-feat.a differ diff --git a/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h b/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h index d74b8b000..ce0dc78b3 100644 --- a/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h +++ b/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h @@ -34,7 +34,9 @@ extern "C" { // PSTR() macro modified to start on a 32-bit boundary. This adds on average // 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster #ifndef PSTR - #define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) PROGMEM = (s); &__c[0];})) + // Adapted from AVR-specific code at https://forum.arduino.cc/index.php?topic=194603.0 + // Uses C attribute section instead of ASM block to allow for C language string concatenation ("x" "y" === "xy") + #define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) __attribute__((section( "\".irom0.pstr." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\", \"aSM\", @progbits, 1 #"))) = (s); &__c[0];})) #endif // Flash memory must be read using 32 bit aligned addresses else a processor @@ -78,6 +80,14 @@ static inline uint16_t pgm_read_word_inlined(const void* addr) { return (uint16_t) res; /* This masks the lower half-word from the returned word */ } +/* Can't legally cast bits of uint32_t to a float w/o conversion or std::memcpy, which is inefficient. */ +/* The ASM block doesn't care the type, so just pass in what C thinks is a float and return in custom fcn. */ +static inline float pgm_read_float_unaligned(const void *addr) { + register float res; + pgm_read_with_offset(addr, res); + return res; +} + #define pgm_read_byte(addr) pgm_read_byte_inlined(addr) #define pgm_read_word_aligned(addr) pgm_read_word_inlined(addr) #ifdef __cplusplus @@ -96,7 +106,6 @@ static inline uint32_t pgm_read_dword_unaligned(const void *addr) { return res; } -#define pgm_read_float_unaligned(addr) ((float)pgm_read_dword_unaligned(addr)) #define pgm_read_ptr_unaligned(addr) ((void*)pgm_read_dword_unaligned(addr)) #define pgm_read_word_unaligned(addr) ((uint16_t)(pgm_read_dword_unaligned(addr) & 0xffff)) diff --git a/tools/sdk/lwip2/builder b/tools/sdk/lwip2/builder index ffa962483..354887a25 160000 --- a/tools/sdk/lwip2/builder +++ b/tools/sdk/lwip2/builder @@ -1 +1 @@ -Subproject commit ffa962483cc1c5d874b11bec13080359619c4cb2 +Subproject commit 354887a25f83064dc0c795e11704190845812713 diff --git a/tools/sdk/lwip2/include/dhcpserver.h b/tools/sdk/lwip2/include/dhcpserver.h index 0f8f9c1e6..4ec907126 100644 --- a/tools/sdk/lwip2/include/dhcpserver.h +++ b/tools/sdk/lwip2/include/dhcpserver.h @@ -1,130 +1,130 @@ - -// adapted from dhcpserver.c distributed in esp8266 sdk 2.0.0 -// same license may apply - -#ifndef __DHCPS_H__ -#define __DHCPS_H__ - -#include "glue.h" // for UDEBUG - -#define USE_DNS - -typedef struct dhcps_state{ - sint16_t state; -} dhcps_state; - -typedef struct dhcps_msg { - uint8_t op, htype, hlen, hops; - uint8_t xid[4]; - uint16_t secs, flags; - uint8_t ciaddr[4]; - uint8_t yiaddr[4]; - uint8_t siaddr[4]; - uint8_t giaddr[4]; - uint8_t chaddr[16]; - uint8_t sname[64]; - uint8_t file[128]; - uint8_t options[312]; -}dhcps_msg; - -#ifndef LWIP_OPEN_SRC -struct dhcps_lease { - bool enable; - struct ipv4_addr start_ip; - struct ipv4_addr end_ip; -}; - -enum dhcps_offer_option{ - OFFER_START = 0x00, - OFFER_ROUTER = 0x01, - OFFER_END -}; -#endif - -typedef enum { - DHCPS_TYPE_DYNAMIC, - DHCPS_TYPE_STATIC -} dhcps_type_t; - -typedef enum { - DHCPS_STATE_ONLINE, - DHCPS_STATE_OFFLINE -} dhcps_state_t; - -struct dhcps_pool{ - struct ipv4_addr ip; - uint8 mac[6]; - uint32 lease_timer; - dhcps_type_t type; - dhcps_state_t state; - -}; - -typedef struct _list_node{ - void *pnode; - struct _list_node *pnext; -}list_node; - -extern uint32 dhcps_lease_time; -#define DHCPS_LEASE_TIMER dhcps_lease_time //0x05A0 -#define DHCPS_MAX_LEASE 0x64 -#define BOOTP_BROADCAST 0x8000 - -#define DHCP_REQUEST 1 -#define DHCP_REPLY 2 -#define DHCP_HTYPE_ETHERNET 1 -#define DHCP_HLEN_ETHERNET 6 -#define DHCP_MSG_LEN 236 - -#define DHCPS_SERVER_PORT 67 -#define DHCPS_CLIENT_PORT 68 - -#define DHCPDISCOVER 1 -#define DHCPOFFER 2 -#define DHCPREQUEST 3 -#define DHCPDECLINE 4 -#define DHCPACK 5 -#define DHCPNAK 6 -#define DHCPRELEASE 7 - -#define DHCP_OPTION_SUBNET_MASK 1 -#define DHCP_OPTION_ROUTER 3 -#define DHCP_OPTION_DNS_SERVER 6 -#define DHCP_OPTION_REQ_IPADDR 50 -#define DHCP_OPTION_LEASE_TIME 51 -#define DHCP_OPTION_MSG_TYPE 53 -#define DHCP_OPTION_SERVER_ID 54 -#define DHCP_OPTION_INTERFACE_MTU 26 -#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31 -#define DHCP_OPTION_BROADCAST_ADDRESS 28 -#define DHCP_OPTION_REQ_LIST 55 -#define DHCP_OPTION_END 255 - -//#define USE_CLASS_B_NET 1 -#define DHCPS_DEBUG UDEBUG -#define MAX_STATION_NUM 8 - -#define DHCPS_STATE_OFFER 1 -#define DHCPS_STATE_DECLINE 2 -#define DHCPS_STATE_ACK 3 -#define DHCPS_STATE_NAK 4 -#define DHCPS_STATE_IDLE 5 -#define DHCPS_STATE_RELEASE 6 - -#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) - -#ifdef __cplusplus -extern "C" -{ -#endif - -void dhcps_set_dns (int num, const ipv4_addr_t* dns); - -void dhcps_start(struct ip_info *info); -void dhcps_stop(void); - -#ifdef __cplusplus -} -#endif - -#endif + +// adapted from dhcpserver.c distributed in esp8266 sdk 2.0.0 +// same license may apply + +#ifndef __DHCPS_H__ +#define __DHCPS_H__ + +#include "glue.h" // for UDEBUG + +#define USE_DNS + +typedef struct dhcps_state{ + sint16_t state; +} dhcps_state; + +typedef struct dhcps_msg { + uint8_t op, htype, hlen, hops; + uint8_t xid[4]; + uint16_t secs, flags; + uint8_t ciaddr[4]; + uint8_t yiaddr[4]; + uint8_t siaddr[4]; + uint8_t giaddr[4]; + uint8_t chaddr[16]; + uint8_t sname[64]; + uint8_t file[128]; + uint8_t options[312]; +}dhcps_msg; + +#ifndef LWIP_OPEN_SRC +struct dhcps_lease { + bool enable; + struct ipv4_addr start_ip; + struct ipv4_addr end_ip; +}; + +enum dhcps_offer_option{ + OFFER_START = 0x00, + OFFER_ROUTER = 0x01, + OFFER_END +}; +#endif + +typedef enum { + DHCPS_TYPE_DYNAMIC, + DHCPS_TYPE_STATIC +} dhcps_type_t; + +typedef enum { + DHCPS_STATE_ONLINE, + DHCPS_STATE_OFFLINE +} dhcps_state_t; + +struct dhcps_pool{ + struct ipv4_addr ip; + uint8 mac[6]; + uint32 lease_timer; + dhcps_type_t type; + dhcps_state_t state; + +}; + +typedef struct _list_node{ + void *pnode; + struct _list_node *pnext; +}list_node; + +extern uint32 dhcps_lease_time; +#define DHCPS_LEASE_TIMER dhcps_lease_time //0x05A0 +#define DHCPS_MAX_LEASE 0x64 +#define BOOTP_BROADCAST 0x8000 + +#define DHCP_REQUEST 1 +#define DHCP_REPLY 2 +#define DHCP_HTYPE_ETHERNET 1 +#define DHCP_HLEN_ETHERNET 6 +#define DHCP_MSG_LEN 236 + +#define DHCPS_SERVER_PORT 67 +#define DHCPS_CLIENT_PORT 68 + +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +#define DHCP_OPTION_SUBNET_MASK 1 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_DNS_SERVER 6 +#define DHCP_OPTION_REQ_IPADDR 50 +#define DHCP_OPTION_LEASE_TIME 51 +#define DHCP_OPTION_MSG_TYPE 53 +#define DHCP_OPTION_SERVER_ID 54 +#define DHCP_OPTION_INTERFACE_MTU 26 +#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31 +#define DHCP_OPTION_BROADCAST_ADDRESS 28 +#define DHCP_OPTION_REQ_LIST 55 +#define DHCP_OPTION_END 255 + +//#define USE_CLASS_B_NET 1 +#define DHCPS_DEBUG UDEBUG +#define MAX_STATION_NUM 8 + +#define DHCPS_STATE_OFFER 1 +#define DHCPS_STATE_DECLINE 2 +#define DHCPS_STATE_ACK 3 +#define DHCPS_STATE_NAK 4 +#define DHCPS_STATE_IDLE 5 +#define DHCPS_STATE_RELEASE 6 + +#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) + +#ifdef __cplusplus +extern "C" +{ +#endif + +void dhcps_set_dns (int num, const ipv4_addr_t* dns); + +void dhcps_start(struct ip_info *info); +void dhcps_stop(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tools/sdk/lwip2/include/lwip-err-t.h b/tools/sdk/lwip2/include/lwip-err-t.h index f9ba97f19..d4a832ab2 100644 --- a/tools/sdk/lwip2/include/lwip-err-t.h +++ b/tools/sdk/lwip2/include/lwip-err-t.h @@ -8,3 +8,4 @@ typedef unsigned long u32_t; typedef signed long s32_t; typedef unsigned long mem_ptr_t; #define LWIP_ERR_T s32_t +typedef uint32_t sys_prot_t; diff --git a/tools/sdk/lwip2/include/lwip-git-hash.h b/tools/sdk/lwip2/include/lwip-git-hash.h index ffcef8ec4..42aca1818 100644 --- a/tools/sdk/lwip2/include/lwip-git-hash.h +++ b/tools/sdk/lwip2/include/lwip-git-hash.h @@ -1,5 +1,5 @@ // generated by makefiles/make-lwip2-hash #ifndef LWIP_HASH_H #define LWIP_HASH_H -#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.2-8-g7958710" +#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.2-16-ge23a07e" #endif // LWIP_HASH_H diff --git a/tools/sdk/lwip2/include/lwip/napt.h b/tools/sdk/lwip2/include/lwip/napt.h index b25e03941..aecb43bed 100644 --- a/tools/sdk/lwip2/include/lwip/napt.h +++ b/tools/sdk/lwip2/include/lwip/napt.h @@ -1,3 +1,40 @@ +/** + * @file + * Network Address and Port Translation + */ + +/* + * Copyright (c) 2016-2019 NeoCat + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: NeoCat + * + */ + #ifndef __LWIP_NAPT_H__ #define __LWIP_NAPT_H__ diff --git a/tools/sdk/lwip2/include/lwipopts.h b/tools/sdk/lwip2/include/lwipopts.h index f5197795e..e2ae90030 100644 --- a/tools/sdk/lwip2/include/lwipopts.h +++ b/tools/sdk/lwip2/include/lwipopts.h @@ -3545,6 +3545,9 @@ #error LWIP_FEATURES must be defined #endif +#ifdef __cplusplus +extern "C" { +#endif /** * TCP_RANDOM_PORT: randomize port instead of simply increasing @@ -3561,27 +3564,34 @@ // so we do not define it. sntp server can come from dhcp server, or by // user. //#define SNTP_SERVER_ADDRESS "pool.ntp.org" // default -//#define SNTP_GET_SERVERS_FROM_DHCP // implicitely enabled by LWIP_DHCP_GET_NTP_SRV +//#define SNTP_GET_SERVERS_FROM_DHCP // implicitely enabled by LWIP_DHCP_GET_NTP_SRV -#define SNTP_SERVER_DNS 1 // enable SNTP support DNS names through sntp_setservername / sntp_getservername +#define SNTP_SERVER_DNS 1 // enable SNTP support DNS names through sntp_setservername / sntp_getservername #define SNTP_SET_SYSTEM_TIME_US(t,us) do { struct timeval tv = { t, us }; settimeofday(&tv, NULL); } while (0) +#define SNTP_SUPPRESS_DELAY_CHECK 1 +#define SNTP_UPDATE_DELAY_DEFAULT 3600000 // update delay defined by a default weak function +#define SNTP_UPDATE_DELAY sntp_update_delay_MS_rfc_not_less_than_15000() +extern uint32_t SNTP_UPDATE_DELAY; + #if LWIP_FEATURES -// lwip-1.4 had 3 possible SNTP servers (constant was harcoded) +// esp8266/arduino/lwip-1.4 had 3 possible SNTP servers (constant was harcoded) #define SNTP_MAX_SERVERS 3 #endif -// turn off random delay before sntp request -// when SNTP_STARTUP_DELAY is not defined, -// LWIP_RAND is used to set a delay +// no delay by default before sntp request +// https://github.com/esp8266/Arduino/pull/5564 // from sntp_opts.h: /** According to the RFC, this shall be a random delay * between 1 and 5 minutes (in milliseconds) to prevent load peaks. * This can be defined to a random generation function, * which must return the delay in milliseconds as u32_t. */ -#define SNTP_STARTUP_DELAY 0 +#define SNTP_STARTUP_DELAY 1 // enable startup delay +#define SNTP_STARTUP_DELAY_FUNC_DEFAULT 0 // to 0 by default via a default weak function +#define SNTP_STARTUP_DELAY_FUNC sntp_startup_delay_MS_rfc_not_less_than_60000() +extern uint32_t SNTP_STARTUP_DELAY_FUNC; /* -------------------------------------------------- @@ -3633,4 +3643,8 @@ void tcp_kill_timewait (void); #define MEMP_NUM_TCP_PCB_TIME_WAIT 5 #endif +#ifdef __cplusplus +} // extern "C" +#endif + #endif // MYLWIPOPTS_H diff --git a/tools/sizes.py b/tools/sizes.py index 669712c1d..f633f8a6a 100755 --- a/tools/sizes.py +++ b/tools/sizes.py @@ -23,6 +23,15 @@ import os import subprocess import sys +def get_segment_hints(): + hints = {} + hints['IROM'] = ' - code in flash (default or ICACHE_FLASH_ATTR)' + hints['IRAM'] = ' / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)' + hints['DATA'] = ') - initialized variables (global, static) in RAM/HEAP' + hints['RODATA'] = ') / 81920 - constants (global, static) in RAM/HEAP' + hints['BSS'] = ') - zeroed variables (global, static) in RAM/HEAP' + return hints + def get_segment_sizes(elf, path): sizes = {} sizes['IROM'] = 0 @@ -53,10 +62,11 @@ def main(): args = parser.parse_args() sizes = get_segment_sizes(args.elf, args.path) + hints = get_segment_hints() sys.stderr.write("Executable segment sizes:" + os.linesep) for k in sizes.keys(): - sys.stderr.write("%-7s: %d%s" % (k, sizes[k], os.linesep)) + sys.stderr.write("%-7s: %-5d %s %s" % (k, sizes[k], hints[k], os.linesep)) return 0