From bed116d98b402095b4f036ae1465c8f492537654 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 7 Feb 2025 11:39:11 +0100 Subject: [PATCH 1/5] feat(mdns): Refactor mdns library (stage #1) The MDNS component has been refactored from a single monolithic file mdns.c into a set of focused modules with clear responsibilities. This restructuring maintains the same functionality while improving code organization, maintainability, and testability. In the stage#2 we will focus on module based tests In the stage#3 we will focus on small scale refators and optimizations --- .github/workflows/mdns__host-tests.yml | 61 +- components/mdns/CMakeLists.txt | 7 +- components/mdns/Kconfig | 14 + components/mdns/mdns.c | 7869 ----------------- components/mdns/mdns_browser.c | 656 ++ components/mdns/mdns_debug.c | 416 + components/mdns/mdns_diagram.md | 51 + components/mdns/mdns_netif.c | 432 + components/mdns/mdns_networking_lwip.c | 132 +- components/mdns/mdns_networking_socket.c | 51 +- components/mdns/mdns_pcb.c | 462 + components/mdns/mdns_querier.c | 872 ++ components/mdns/mdns_receive.c | 1281 +++ components/mdns/mdns_responder.c | 1554 ++++ components/mdns/mdns_send.c | 1880 ++++ components/mdns/mdns_service.c | 424 + components/mdns/mdns_utils.c | 270 + .../mdns/private_include/mdns_browser.h | 78 + components/mdns/private_include/mdns_debug.h | 65 + components/mdns/private_include/mdns_netif.h | 67 + .../mdns/private_include/mdns_networking.h | 30 +- components/mdns/private_include/mdns_pcb.h | 121 + .../mdns/private_include/mdns_private.h | 74 +- .../mdns/private_include/mdns_querier.h | 111 + .../mdns/private_include/mdns_receive.h | 24 + .../mdns/private_include/mdns_responder.h | 98 + components/mdns/private_include/mdns_send.h | 131 + .../mdns/private_include/mdns_service.h | 32 + components/mdns/private_include/mdns_utils.h | 155 + components/mdns/refactor_2025.md | 56 + components/mdns/refactoring_details.md | 244 + .../mdns/tests/host_unit_test/CMakeLists.txt | 85 + .../host_unit_test/input/generate_cases.py | 166 + components/mdns/tests/host_unit_test/main.c | 110 + .../tests/host_unit_test/sdkconfig.defaults | 6 + .../tests/host_unit_test/stubs/esp_assert.h | 6 + .../mdns/tests/host_unit_test/stubs/esp_idf.c | 82 + .../tests/host_unit_test/stubs/esp_random.h | 0 .../tests/host_unit_test/stubs/mdns_engine.c | 62 + .../host_unit_test/stubs/mdns_mem_caps.c | 71 + .../host_unit_test/stubs/mdns_networking.c | 90 + .../tests/host_unit_test/stubs/sdkconfig.h | 885 ++ .../host_unit_test/unity/cmock_config.yml | 17 + .../host_unit_test/unity/create_test_packet.c | 160 + .../host_unit_test/unity/create_test_packet.h | 31 + .../host_unit_test/unity/enable_testing.cmake | 11 + .../unity/test_receiver/test_receiver.c | 93 + .../unity/test_receiver/unit_test.cmake | 7 + .../unity/test_sender/test_sender.c | 36 + .../unity/test_sender/unit_test.cmake | 6 + .../host_unit_test/unity/unit_test.cmake | 67 + .../tests/host_unit_test/unity/unity_main.c | 146 + .../tests/host_unit_test/unity/unity_main.h | 10 + .../tests/test_afl_fuzz_host/CMakeLists.txt | 7 - .../mdns/tests/test_afl_fuzz_host/Makefile | 96 - .../mdns/tests/test_afl_fuzz_host/README.md | 80 - .../tests/test_afl_fuzz_host/esp32_mock.c | 162 - .../tests/test_afl_fuzz_host/esp32_mock.h | 145 - .../mdns/tests/test_afl_fuzz_host/esp_attr.h | 14 - .../tests/test_afl_fuzz_host/esp_netif_mock.c | 55 - .../tests/test_afl_fuzz_host/in/file2.bin | Bin 33 -> 0 bytes .../test_afl_fuzz_host/in/minif_4a_txt.bin | Bin 39 -> 0 bytes .../test_afl_fuzz_host/in/minif_aaaa.bin | Bin 33 -> 0 bytes .../tests/test_afl_fuzz_host/in/minif_any.bin | Bin 109 -> 0 bytes .../test_afl_fuzz_host/in/minif_disc.bin | Bin 65 -> 0 bytes .../tests/test_afl_fuzz_host/in/minif_ptr.bin | Bin 36 -> 0 bytes .../test_afl_fuzz_host/in/minif_query.bin | Bin 77 -> 0 bytes .../test_afl_fuzz_host/in/minif_query2.bin | Bin 64 -> 0 bytes .../test_afl_fuzz_host/in/sub_fritz_m.bin | Bin 72 -> 0 bytes .../test_afl_fuzz_host/in/telnet_ptr.bin | Bin 72 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-14.bin | Bin 568 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-15.bin | Bin 524 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-16.bin | Bin 254 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-28.bin | Bin 62 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-29.bin | Bin 39 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-31.bin | Bin 91 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-53.bin | Bin 140 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-56.bin | Bin 262 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-63.bin | Bin 147 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-83.bin | Bin 105 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-88.bin | Bin 48 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-89.bin | Bin 459 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-95.bin | Bin 286 -> 0 bytes .../tests/test_afl_fuzz_host/in/test-96.bin | Bin 319 -> 0 bytes .../test_afl_fuzz_host/input_packets.txt | 166 - .../mdns/tests/test_afl_fuzz_host/mdns_di.h | 61 - .../mdns/tests/test_afl_fuzz_host/mdns_mock.h | 31 - .../mdns/tests/test_afl_fuzz_host/sdkconfig.h | 424 - .../mdns/tests/test_afl_fuzz_host/test.c | 263 - 89 files changed, 11837 insertions(+), 9532 deletions(-) delete mode 100644 components/mdns/mdns.c create mode 100644 components/mdns/mdns_browser.c create mode 100644 components/mdns/mdns_debug.c create mode 100644 components/mdns/mdns_diagram.md create mode 100644 components/mdns/mdns_netif.c create mode 100644 components/mdns/mdns_pcb.c create mode 100644 components/mdns/mdns_querier.c create mode 100644 components/mdns/mdns_receive.c create mode 100644 components/mdns/mdns_responder.c create mode 100644 components/mdns/mdns_send.c create mode 100644 components/mdns/mdns_service.c create mode 100644 components/mdns/mdns_utils.c create mode 100644 components/mdns/private_include/mdns_browser.h create mode 100644 components/mdns/private_include/mdns_debug.h create mode 100644 components/mdns/private_include/mdns_netif.h create mode 100644 components/mdns/private_include/mdns_pcb.h create mode 100644 components/mdns/private_include/mdns_querier.h create mode 100644 components/mdns/private_include/mdns_receive.h create mode 100644 components/mdns/private_include/mdns_responder.h create mode 100644 components/mdns/private_include/mdns_send.h create mode 100644 components/mdns/private_include/mdns_service.h create mode 100644 components/mdns/private_include/mdns_utils.h create mode 100644 components/mdns/refactor_2025.md create mode 100644 components/mdns/refactoring_details.md create mode 100644 components/mdns/tests/host_unit_test/CMakeLists.txt create mode 100644 components/mdns/tests/host_unit_test/input/generate_cases.py create mode 100644 components/mdns/tests/host_unit_test/main.c create mode 100644 components/mdns/tests/host_unit_test/sdkconfig.defaults create mode 100644 components/mdns/tests/host_unit_test/stubs/esp_assert.h create mode 100644 components/mdns/tests/host_unit_test/stubs/esp_idf.c create mode 100644 components/mdns/tests/host_unit_test/stubs/esp_random.h create mode 100644 components/mdns/tests/host_unit_test/stubs/mdns_engine.c create mode 100644 components/mdns/tests/host_unit_test/stubs/mdns_mem_caps.c create mode 100644 components/mdns/tests/host_unit_test/stubs/mdns_networking.c create mode 100644 components/mdns/tests/host_unit_test/stubs/sdkconfig.h create mode 100644 components/mdns/tests/host_unit_test/unity/cmock_config.yml create mode 100644 components/mdns/tests/host_unit_test/unity/create_test_packet.c create mode 100644 components/mdns/tests/host_unit_test/unity/create_test_packet.h create mode 100644 components/mdns/tests/host_unit_test/unity/enable_testing.cmake create mode 100644 components/mdns/tests/host_unit_test/unity/test_receiver/test_receiver.c create mode 100644 components/mdns/tests/host_unit_test/unity/test_receiver/unit_test.cmake create mode 100644 components/mdns/tests/host_unit_test/unity/test_sender/test_sender.c create mode 100644 components/mdns/tests/host_unit_test/unity/test_sender/unit_test.cmake create mode 100644 components/mdns/tests/host_unit_test/unity/unit_test.cmake create mode 100644 components/mdns/tests/host_unit_test/unity/unity_main.c create mode 100644 components/mdns/tests/host_unit_test/unity/unity_main.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/CMakeLists.txt delete mode 100644 components/mdns/tests/test_afl_fuzz_host/Makefile delete mode 100644 components/mdns/tests/test_afl_fuzz_host/README.md delete mode 100644 components/mdns/tests/test_afl_fuzz_host/esp32_mock.c delete mode 100644 components/mdns/tests/test_afl_fuzz_host/esp32_mock.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/esp_attr.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/esp_netif_mock.c delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/file2.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_4a_txt.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_aaaa.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_any.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_disc.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_ptr.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_query.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/minif_query2.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/sub_fritz_m.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/telnet_ptr.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-14.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-15.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-16.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-28.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-29.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-31.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-53.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-56.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-63.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-83.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-88.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-89.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-95.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/in/test-96.bin delete mode 100644 components/mdns/tests/test_afl_fuzz_host/input_packets.txt delete mode 100644 components/mdns/tests/test_afl_fuzz_host/mdns_di.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/mdns_mock.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/sdkconfig.h delete mode 100644 components/mdns/tests/test_afl_fuzz_host/test.c diff --git a/.github/workflows/mdns__host-tests.yml b/.github/workflows/mdns__host-tests.yml index f7512c4c81..06eb9f1544 100644 --- a/.github/workflows/mdns__host-tests.yml +++ b/.github/workflows/mdns__host-tests.yml @@ -29,6 +29,8 @@ jobs: # Build host tests app (with all configs and targets supported) python ./ci/build_apps.py components/mdns/tests/host_test/ cd components/mdns/tests/host_test + ls -la + ls -ls build* # First run the linux_app and send a quick A query and a reverse query ./build_linux_app/mdns_host.elf & python dnsfixture.py A myesp.local --ip_only | xargs python dnsfixture.py X @@ -55,8 +57,11 @@ jobs: shell: bash run: | . ${IDF_PATH}/export.sh - cd components/mdns/tests/test_afl_fuzz_host/ - make INSTR=off + cd components/mdns/tests/host_unit_test/ + idf.py reconfigure + mkdir build2 && cd build2 + cmake .. + cmake --build . - name: Test no malloc functions shell: bash run: | @@ -69,6 +74,39 @@ jobs: echo "OK" done + host_unit_test: + if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push' + name: Unit tests on host + strategy: + matrix: + idf_ver: ["latest"] + + runs-on: ubuntu-22.04 + container: espressif/idf:${{ matrix.idf_ver }} + steps: + - name: Checkout esp-protocols + uses: actions/checkout@v4 + - name: Install bsdlib and ruby + run: | + apt-get update -y + apt-get install -y libbsd-dev ruby + - name: Build and run unit tests + shell: bash + run: | + . ${IDF_PATH}/export.sh + cd components/mdns/tests/host_unit_test/ + idf.py reconfigure + mkdir build2 && cd build2 + cmake -DUNIT_TESTS=test_receiver .. + cmake --build . + ctest --extra-verbose + cd .. + mkdir build3 && cd build3 + cmake -DUNIT_TESTS=test_sender .. + cmake --build . + ctest --extra-verbose + + fuzz_test: if: contains(github.event.pull_request.labels.*.name, 'mdns-fuzz') || github.event_name == 'push' name: Fuzzer tests for mdns lib @@ -98,13 +136,26 @@ jobs: shell: bash run: | export IDF_PATH=$GITHUB_WORKSPACE/idf - cd components/mdns/tests/test_afl_fuzz_host/ - make fuzz + cd components/mdns/tests/host_unit_test/ + pip install dnslib + cd input && python generate_cases.py && cd .. + cmake -B build2 -S . -G "Ninja" -DCMAKE_C_COMPILER=afl-cc + cmake --build build2 + timeout 10m afl-fuzz -i input -o out -- build2/mdns_host_unit_test || \ + if [ $? -eq 124 ]; then # timeout exit code + if [ -n "$(find out/default/crashes -type f 2>/dev/null)" ]; then + echo "Crashes found!"; + tar -czf out/default/crashes.tar.gz -C out/default crashes; + exit 1; + fi + else + exit 1; + fi - name: Upload Crash Artifacts if: failure() uses: actions/upload-artifact@v4 with: name: fuzz-crashes - path: components/mdns/tests/test_afl_fuzz_host/out/default/crashes.tar.gz + path: components/mdns/tests/host_unit_test/out/default/crashes.tar.gz if-no-files-found: ignore diff --git a/components/mdns/CMakeLists.txt b/components/mdns/CMakeLists.txt index 5823849b05..12bbfb3489 100644 --- a/components/mdns/CMakeLists.txt +++ b/components/mdns/CMakeLists.txt @@ -12,15 +12,18 @@ endif() set(MDNS_MEMORY "mdns_mem_caps.c") +set(MDNS_CORE "mdns_responder.c" "mdns_receive.c" "mdns_utils.c" "mdns_debug.c" "mdns_browser.c" "mdns_send.c" "mdns_netif.c" + "mdns_querier.c" "mdns_pcb.c" "mdns_service.c") + idf_build_get_property(target IDF_TARGET) if(${target} STREQUAL "linux") set(dependencies esp_netif_linux esp_event) set(private_dependencies esp_timer console esp_system) - set(srcs "mdns.c" ${MDNS_MEMORY} ${MDNS_NETWORKING} ${MDNS_CONSOLE}) + set(srcs ${MDNS_CORE} ${MDNS_MEMORY} ${MDNS_NETWORKING} ${MDNS_CONSOLE}) else() set(dependencies lwip console esp_netif) set(private_dependencies esp_timer esp_wifi) - set(srcs "mdns.c" ${MDNS_MEMORY} ${MDNS_NETWORKING} ${MDNS_CONSOLE}) + set(srcs ${MDNS_CORE} ${MDNS_MEMORY} ${MDNS_NETWORKING} ${MDNS_CONSOLE}) endif() idf_component_register( diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index 04380a4630..bde726d1b7 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -142,6 +142,20 @@ menu "mDNS" help Enable for the library to log received and sent mDNS packets to stdout. + config MDNS_DEBUG_USE_ESP_LOG + bool "Route debug prints to ESP_LOG" + depends on MDNS_ENABLE_DEBUG_PRINTS + default y + help + Enable this option to route debug prints to ESP_LOG, which allows more flexibility. + + config MDNS_DEBUG_BUFFER_SIZE + int "Size of temporary buffer for debug prints" + depends on MDNS_DEBUG_USE_ESP_LOG + default 1024 + help + This buffer is used to output mDNS packets in debug prints. + config MDNS_ENABLE_CONSOLE_CLI bool "Enable Command Line Interface on device console" default y diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c deleted file mode 100644 index 22cfaf73aa..0000000000 --- a/components/mdns/mdns.c +++ /dev/null @@ -1,7869 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" -#include "freertos/semphr.h" -#include "esp_log.h" -#include "esp_event.h" -#include "esp_random.h" -#include "esp_check.h" -#include "mdns.h" -#include "mdns_private.h" -#include "mdns_networking.h" -#include "mdns_mem_caps.h" - -static void _mdns_browse_item_free(mdns_browse_t *browse); -static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse); -static esp_err_t _mdns_sync_browse_action(mdns_action_type_t type, mdns_browse_sync_t *browse_sync); -static void _mdns_browse_sync(mdns_browse_sync_t *browse_sync); -static void _mdns_browse_finish(mdns_browse_t *browse); -static void _mdns_browse_add(mdns_browse_t *browse); -static void _mdns_browse_send(mdns_browse_t *browse, mdns_if_t interface); - -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH -#include "esp_eth.h" -#endif - -#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0) -#define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED -#else -#define MDNS_ESP_WIFI_ENABLED (CONFIG_ESP_WIFI_ENABLED || CONFIG_ESP_WIFI_REMOTE_ENABLED) -#endif - -#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) -#include "esp_wifi.h" -#endif - -#ifdef MDNS_ENABLE_DEBUG -void mdns_debug_packet(const uint8_t *data, size_t len); -#endif - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) -#endif - -// Internal size of IPv6 address is defined here as size of AAAA record in mdns packet -// since the ip6_addr_t is defined in lwip and depends on using IPv6 zones -#define _MDNS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE) - -static const char *MDNS_DEFAULT_DOMAIN = "local"; -static const char *MDNS_SUB_STR = "_sub"; - -mdns_server_t *_mdns_server = NULL; -static mdns_host_item_t *_mdns_host_list = NULL; -static mdns_host_item_t _mdns_self_host; - -static const char *TAG = "mdns"; - -static volatile TaskHandle_t _mdns_service_task_handle = NULL; -static SemaphoreHandle_t _mdns_service_semaphore = NULL; -static StackType_t *_mdns_stack_buffer; - -static void _mdns_search_finish_done(void); -static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *search, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); -static void _mdns_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto, - uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse); -static void _mdns_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse); -static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto, - mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, - uint32_t ttl, mdns_browse_sync_t *out_sync_browse); -#ifdef MDNS_ENABLE_DEBUG -static void debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t); -static void debug_printf_browse_result_all(mdns_result_t *r_t); -#endif // MDNS_ENABLE_DEBUG -static void _mdns_search_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl); -static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl); -static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len, - size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, - uint32_t ttl); -static mdns_result_t *_mdns_search_result_add_ptr(mdns_search_once_t *search, const char *instance, - const char *service_type, const char *proto, mdns_if_t tcpip_if, - mdns_ip_protocol_t ip_protocol, uint32_t ttl); -static bool _mdns_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], size_t services_len, bool flush, bool bye); -static bool _mdns_append_host_list(mdns_out_answer_t **destination, bool flush, bool bye); -static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname); -static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action); - -static void _mdns_query_results_free(mdns_result_t *results); -typedef enum { - MDNS_IF_STA = 0, - MDNS_IF_AP = 1, - MDNS_IF_ETH = 2, -} mdns_predef_if_t; - -typedef struct mdns_interfaces mdns_interfaces_t; - -struct mdns_interfaces { - const bool predefined; - esp_netif_t *netif; - const mdns_predef_if_t predef_if; - mdns_if_t duplicate; -}; - -/* - * @brief Internal collection of mdns supported interfaces - * - */ -static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = { -#if CONFIG_MDNS_PREDEF_NETIF_STA - { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_MAX_INTERFACES }, -#endif -#if CONFIG_MDNS_PREDEF_NETIF_AP - { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_MAX_INTERFACES }, -#endif -#if CONFIG_MDNS_PREDEF_NETIF_ETH - { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_MAX_INTERFACES }, -#endif -}; - - -/** - * @brief Convert Predefined interface to the netif id from the internal netif list - * @param predef_if Predefined interface enum - * @return Ordinal number of internal list of mdns network interface. - * Returns MDNS_MAX_INTERFACES if the predefined interface wasn't found in the list - */ -static mdns_if_t mdns_if_from_preset_if(mdns_predef_if_t predef_if) -{ - for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) { - if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) { - return i; - } - } - return MDNS_MAX_INTERFACES; -} - -/** - * @brief Convert Predefined interface to esp-netif handle - * @param predef_if Predefined interface enum - * @return esp_netif pointer from system list of network interfaces - */ -static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t predef_if) -{ - switch (predef_if) { - case MDNS_IF_STA: - return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); - case MDNS_IF_AP: - return esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH - case MDNS_IF_ETH: - return esp_netif_get_handle_from_ifkey("ETH_DEF"); -#endif - default: - return NULL; - } -} - -/** - * @brief Gets the actual esp_netif pointer from the internal network interface list - * - * The supplied ordinal number could - * - point to a predef netif -> "STA", "AP", "ETH" - * - if no entry in the list (NULL) -> check if the system added this netif - * - point to a custom netif -> just return the entry in the list - * - users is responsible for the lifetime of this netif (to be valid between mdns-init -> deinit) - * - * @param tcpip_if Ordinal number of the interface - * @return Pointer ot the esp_netif object if the interface is available, NULL otherwise - */ -esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if) -{ - if (tcpip_if < MDNS_MAX_INTERFACES) { - if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) { - // If the local copy is NULL and this netif is predefined -> we can find it in the global netif list - s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if); - // failing to find it means that the netif is *not* available -> return NULL - } - return s_esp_netifs[tcpip_if].netif; - } - return NULL; -} - - -/* - * @brief Clean internal mdns interface's pointer - */ -static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) -{ - if (tcpip_if < MDNS_MAX_INTERFACES) { - s_esp_netifs[tcpip_if].netif = NULL; - } -} - - -/* - * @brief Convert esp-netif handle to mdns if - */ -static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif) -{ - for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) { - // The predefined netifs in the static array are NULL when firstly calling this function - // if IPv4 is disabled. Set these netifs here. - if (s_esp_netifs[i].netif == NULL && s_esp_netifs[i].predefined) { - s_esp_netifs[i].netif = esp_netif_from_preset_if(s_esp_netifs[i].predef_if); - } - if (esp_netif == s_esp_netifs[i].netif) { - return i; - } - } - return MDNS_MAX_INTERFACES; -} - - - -static inline bool _str_null_or_empty(const char *str) -{ - return (str == NULL || *str == 0); -} - -/* - * @brief Appends/increments a number to name/instance in case of collision - * */ -static char *_mdns_mangle_name(char *in) -{ - char *p = strrchr(in, '-'); - int suffix = 0; - if (p == NULL) { - //No - in ``in`` - suffix = 2; - } else { - char *endp = NULL; - suffix = strtol(p + 1, &endp, 10); - if (*endp != 0) { - //suffix is not numerical - suffix = 2; - p = NULL; //so we append -suffix to the entire string - } - } - char *ret; - if (p == NULL) { - //need to add -2 to string - ret = mdns_mem_malloc(strlen(in) + 3); - if (ret == NULL) { - HOOK_MALLOC_FAILED; - return NULL; - } - sprintf(ret, "%s-2", in); - } else { - size_t in_len = strlen(in); - ret = mdns_mem_malloc(in_len + 2); //one extra byte in case 9-10 or 99-100 etc - if (ret == NULL) { - HOOK_MALLOC_FAILED; - return NULL; - } - memcpy(ret, in, in_len); - int baseLen = p - in; //length of 'bla' in 'bla-123' - //overwrite suffix with new suffix - sprintf(ret + baseLen, "-%d", suffix + 1); - } - return ret; -} - -static bool _mdns_service_match(const mdns_service_t *srv, const char *service, const char *proto, - const char *hostname) -{ - if (!service || !proto || !srv->hostname) { - return false; - } - return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) && - (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname)); -} - -/** - * @brief finds service from given service type - * @param server the server - * @param service service type to match - * @param proto proto to match - * @param hostname hostname of the service (if non-null) - * - * @return the service item if found or NULL on error - */ -static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname) -{ - mdns_srv_item_t *s = _mdns_server->services; - while (s) { - if (_mdns_service_match(s->service, service, proto, hostname)) { - return s; - } - s = s->next; - } - return NULL; -} - -static mdns_srv_item_t *_mdns_get_service_item_subtype(const char *subtype, const char *service, const char *proto) -{ - mdns_srv_item_t *s = _mdns_server->services; - while (s) { - if (_mdns_service_match(s->service, service, proto, NULL)) { - mdns_subtype_t *subtype_item = s->service->subtype; - while (subtype_item) { - if (!strcasecmp(subtype_item->subtype, subtype)) { - return s; - } - subtype_item = subtype_item->next; - } - } - s = s->next; - } - return NULL; -} - -static mdns_host_item_t *mdns_get_host_item(const char *hostname) -{ - if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) { - return &_mdns_self_host; - } - mdns_host_item_t *host = _mdns_host_list; - while (host != NULL) { - if (strcasecmp(host->hostname, hostname) == 0) { - return host; - } - host = host->next; - } - return NULL; -} - -static bool _mdns_can_add_more_services(void) -{ -#if MDNS_MAX_SERVICES == 0 - return false; -#else - mdns_srv_item_t *s = _mdns_server->services; - uint16_t service_num = 0; - while (s) { - service_num ++; - s = s->next; - if (service_num >= MDNS_MAX_SERVICES) { - return false; - } - } - return true; -#endif -} - -esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet) -{ - mdns_action_t *action = NULL; - - action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - - action->type = ACTION_RX_HANDLE; - action->data.rx_handle.packet = packet; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -static const char *_mdns_get_default_instance_name(void) -{ - if (_mdns_server && !_str_null_or_empty(_mdns_server->instance)) { - return _mdns_server->instance; - } - - if (_mdns_server && !_str_null_or_empty(_mdns_server->hostname)) { - return _mdns_server->hostname; - } - - return NULL; -} - -/** - * @brief Get the service name of a service - */ -static const char *_mdns_get_service_instance_name(const mdns_service_t *service) -{ - if (service && !_str_null_or_empty(service->instance)) { - return service->instance; - } - - return _mdns_get_default_instance_name(); -} - -static bool _mdns_instance_name_match(const char *lhs, const char *rhs) -{ - if (lhs == NULL) { - lhs = _mdns_get_default_instance_name(); - } - if (rhs == NULL) { - rhs = _mdns_get_default_instance_name(); - } - return !strcasecmp(lhs, rhs); -} - -static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service, - const char *proto, const char *hostname) -{ - // service and proto must be supplied, if not this instance won't match - if (!service || !proto) { - return false; - } - // instance==NULL -> _mdns_instance_name_match() will check the default instance - // hostname==NULL -> matches if instance, service and proto matches - return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) && - !strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname)); -} - -static mdns_srv_item_t *_mdns_get_service_item_instance(const char *instance, const char *service, const char *proto, - const char *hostname) -{ - mdns_srv_item_t *s = _mdns_server->services; - while (s) { - if (instance) { - if (_mdns_service_match_instance(s->service, instance, service, proto, hostname)) { - return s; - } - } else { - if (_mdns_service_match(s->service, service, proto, hostname)) { - return s; - } - } - s = s->next; - } - return NULL; -} - -/** - * @brief reads MDNS FQDN into mdns_name_t structure - * FQDN is in format: [hostname.|[instance.]_service._proto.]local. - * - * @param packet MDNS packet - * @param start Starting point of FQDN - * @param name mdns_name_t structure to populate - * @param buf temporary char buffer - * - * @return the address after the parsed FQDN in the packet or NULL on error - */ -static const uint8_t *_mdns_read_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, char *buf, size_t packet_len) -{ - size_t index = 0; - const uint8_t *packet_end = packet + packet_len; - while (start + index < packet_end && start[index]) { - if (name->parts == 4) { - name->invalid = true; - } - uint8_t len = start[index++]; - if (len < 0xC0) { - if (len > 63) { - //length can not be more than 63 - return NULL; - } - uint8_t i; - for (i = 0; i < len; i++) { - if (start + index >= packet_end) { - return NULL; - } - buf[i] = start[index++]; - } - buf[len] = '\0'; - if (name->parts == 1 && buf[0] != '_' - && (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0) - && (strcasecmp(buf, "arpa") != 0) -#ifndef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - && (strcasecmp(buf, "ip6") != 0) - && (strcasecmp(buf, "in-addr") != 0) -#endif - ) { - strlcat(name->host, ".", sizeof(name->host)); - strlcat(name->host, buf, sizeof(name->host)); - } else if (strcasecmp(buf, MDNS_SUB_STR) == 0) { - name->sub = 1; - } else if (!name->invalid) { - char *mdns_name_ptrs[] = {name->host, name->service, name->proto, name->domain}; - memcpy(mdns_name_ptrs[name->parts++], buf, len + 1); - } - } else { - size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++]; - if ((packet + address) >= start) { - //reference address can not be after where we are - return NULL; - } - if (_mdns_read_fqdn(packet, packet + address, name, buf, packet_len)) { - return start + index; - } - return NULL; - } - } - return start + index + 1; -} - -/** - * @brief sets uint16_t value in a packet - * - * @param packet MDNS packet - * @param index offset of uint16_t value - * @param value the value to set - */ -static inline void _mdns_set_u16(uint8_t *packet, uint16_t index, uint16_t value) -{ - if ((index + 1) >= MDNS_MAX_PACKET_SIZE) { - return; - } - packet[index] = (value >> 8) & 0xFF; - packet[index + 1] = value & 0xFF; -} - -/** - * @brief appends byte in a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param value the value to set - * - * @return length of added data: 0 on error or 1 on success - */ -static inline uint8_t _mdns_append_u8(uint8_t *packet, uint16_t *index, uint8_t value) -{ - if (*index >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - packet[*index] = value; - *index += 1; - return 1; -} - -/** - * @brief appends uint16_t in a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param value the value to set - * - * @return length of added data: 0 on error or 2 on success - */ -static inline uint8_t _mdns_append_u16(uint8_t *packet, uint16_t *index, uint16_t value) -{ - if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, (value >> 8) & 0xFF); - _mdns_append_u8(packet, index, value & 0xFF); - return 2; -} - -/** - * @brief appends uint32_t in a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param value the value to set - * - * @return length of added data: 0 on error or 4 on success - */ -static inline uint8_t _mdns_append_u32(uint8_t *packet, uint16_t *index, uint32_t value) -{ - if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, (value >> 24) & 0xFF); - _mdns_append_u8(packet, index, (value >> 16) & 0xFF); - _mdns_append_u8(packet, index, (value >> 8) & 0xFF); - _mdns_append_u8(packet, index, value & 0xFF); - return 4; -} - -/** - * @brief appends answer type, class, ttl and data length to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param type answer type - * @param ttl answer ttl - * - * @return length of added data: 0 on error or 10 on success - */ -static inline uint8_t _mdns_append_type(uint8_t *packet, uint16_t *index, uint8_t type, bool flush, uint32_t ttl) -{ - if ((*index + 10) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - uint16_t mdns_class = MDNS_CLASS_IN; - if (flush) { - mdns_class = MDNS_CLASS_IN_FLUSH_CACHE; - } - if (type == MDNS_ANSWER_PTR) { - _mdns_append_u16(packet, index, MDNS_TYPE_PTR); - _mdns_append_u16(packet, index, mdns_class); - } else if (type == MDNS_ANSWER_TXT) { - _mdns_append_u16(packet, index, MDNS_TYPE_TXT); - _mdns_append_u16(packet, index, mdns_class); - } else if (type == MDNS_ANSWER_SRV) { - _mdns_append_u16(packet, index, MDNS_TYPE_SRV); - _mdns_append_u16(packet, index, mdns_class); - } else if (type == MDNS_ANSWER_A) { - _mdns_append_u16(packet, index, MDNS_TYPE_A); - _mdns_append_u16(packet, index, mdns_class); - } else if (type == MDNS_ANSWER_AAAA) { - _mdns_append_u16(packet, index, MDNS_TYPE_AAAA); - _mdns_append_u16(packet, index, mdns_class); - } else { - return 0; - } - _mdns_append_u32(packet, index, ttl); - _mdns_append_u16(packet, index, 0); - return 10; -} - -static inline uint8_t _mdns_append_string_with_len(uint8_t *packet, uint16_t *index, const char *string, uint8_t len) -{ - if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, len); - memcpy(packet + *index, string, len); - *index += len; - return len + 1; -} - -/** - * @brief appends single string to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param string the string to append - * - * @return length of added data: 0 on error or length of the string + 1 on success - */ -static inline uint8_t _mdns_append_string(uint8_t *packet, uint16_t *index, const char *string) -{ - uint8_t len = strlen(string); - if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, len); - memcpy(packet + *index, string, len); - *index += len; - return len + 1; -} - -/** - * @brief appends one TXT record ("key=value" or "key") - * - * @param packet MDNS packet - * @param index offset in the packet - * @param txt one txt record - * - * @return length of added data: length of the added txt value + 1 on success - * 0 if data won't fit the packet - * -1 if invalid TXT entry - */ -static inline int append_one_txt_record_entry(uint8_t *packet, uint16_t *index, mdns_txt_linked_item_t *txt) -{ - if (txt == NULL || txt->key == NULL) { - return -1; - } - size_t key_len = strlen(txt->key); - size_t len = key_len + txt->value_len + (txt->value ? 1 : 0); - if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, len); - memcpy(packet + *index, txt->key, key_len); - if (txt->value) { - packet[*index + key_len] = '='; - memcpy(packet + *index + key_len + 1, txt->value, txt->value_len); - } - *index += len; - return len + 1; -} - -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES -static inline int append_single_str(uint8_t *packet, uint16_t *index, const char *str, int len) -{ - if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - if (!_mdns_append_u8(packet, index, len)) { - return 0; - } - memcpy(packet + *index, str, len); - *index += len; - return *index; -} - -/** - * @brief appends FQDN to a packet from hostname separated by dots. This API works the same way as - * _mdns_append_fqdn(), but refrains from DNS compression (as it's mainly used for IP addresses (many short items), - * where we gain very little (or compression even gets counter-productive mainly for IPv6 addresses) - * - * @param packet MDNS packet - * @param index offset in the packet - * @param name name representing FQDN in '.' separated parts - * @param last true if appending the last part (domain, typically "arpa") - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t append_fqdn_dots(uint8_t *packet, uint16_t *index, const char *name, bool last) -{ - int len = strlen(name); - char *host = (char *)name; - char *end = host; - char *start = host; - do { - end = memchr(start, '.', host + len - start); - end = end ? end : host + len; - int part_len = end - start; - if (!append_single_str(packet, index, start, part_len)) { - return 0; - } - start = ++end; - } while (end < name + len); - - if (!append_single_str(packet, index, "arpa", sizeof("arpa") - 1)) { - return 0; - } - - //empty string so terminate - if (!_mdns_append_u8(packet, index, 0)) { - return 0; - } - return *index; -} -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - -/** - * @brief appends FQDN to a packet, incrementing the index and - * compressing the output if previous occurrence of the string (or part of it) has been found - * - * @param packet MDNS packet - * @param index offset in the packet - * @param strings string array containing the parts of the FQDN - * @param count number of strings in the array - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_fqdn(uint8_t *packet, uint16_t *index, const char *strings[], uint8_t count, size_t packet_len) -{ - if (!count) { - //empty string so terminate - return _mdns_append_u8(packet, index, 0); - } - static char buf[MDNS_NAME_BUF_LEN]; - uint8_t len = strlen(strings[0]); - //try to find first the string length in the packet (if it exists) - uint8_t *len_location = (uint8_t *)memchr(packet, (char)len, *index); - while (len_location) { - mdns_name_t name; - //check if the string after len_location is the string that we are looking for - if (memcmp(len_location + 1, strings[0], len)) { //not continuing with our string -search_next: - //try and find the length byte further in the packet - len_location = (uint8_t *)memchr(len_location + 1, (char)len, *index - (len_location + 1 - packet)); - continue; - } - //seems that we might have found the string that we are looking for - //read the destination into name and compare - name.parts = 0; - name.sub = 0; - name.invalid = false; - name.host[0] = 0; - name.service[0] = 0; - name.proto[0] = 0; - name.domain[0] = 0; - const uint8_t *content = _mdns_read_fqdn(packet, len_location, &name, buf, packet_len); - if (!content) { - //not a readable fqdn? - goto search_next; // could be our unfinished fqdn, continue searching - } - if (name.parts == count) { - uint8_t i; - for (i = 0; i < count; i++) { - if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) { - //not our string! let's search more - goto search_next; - } - } - //we actually have found the string - break; - } else { - goto search_next; - } - } - //string is not yet in the packet, so let's add it - if (!len_location) { - uint8_t written = _mdns_append_string(packet, index, strings[0]); - if (!written) { - return 0; - } - //run the same for the other strings in the name - return written + _mdns_append_fqdn(packet, index, &strings[1], count - 1, packet_len); - } - - //we have found the string so let's insert a pointer to it instead - uint16_t offset = len_location - packet; - offset |= MDNS_NAME_REF; - return _mdns_append_u16(packet, index, offset); -} - -/** - * @brief appends PTR record for service to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param server the server that is hosting the service - * @param service the service to add record for - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, const char *service, const char *proto, bool flush, bool bye) -{ - const char *str[4]; - uint16_t record_length = 0; - uint8_t part_length; - - if (service == NULL) { - return 0; - } - - str[0] = instance; - str[1] = service; - str[2] = proto; - str[3] = MDNS_DEFAULT_DOMAIN; - - part_length = _mdns_append_fqdn(packet, index, str + 1, 3, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - _mdns_set_u16(packet, data_len_location, part_length); - record_length += part_length; - return record_length; -} - -/** - * @brief appends PTR record for a subtype to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param instance the service instance name - * @param subtype the service subtype - * @param proto the service protocol - * @param flush whether to set the flush flag - * @param bye whether to set the bye flag - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_subtype_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, - const char *subtype, const char *service, const char *proto, bool flush, - bool bye) -{ - const char *subtype_str[5] = {subtype, MDNS_SUB_STR, service, proto, MDNS_DEFAULT_DOMAIN}; - const char *instance_str[4] = {instance, service, proto, MDNS_DEFAULT_DOMAIN}; - uint16_t record_length = 0; - uint8_t part_length; - - if (service == NULL) { - return 0; - } - - part_length = _mdns_append_fqdn(packet, index, subtype_str, ARRAY_SIZE(subtype_str), MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - part_length = _mdns_append_fqdn(packet, index, instance_str, ARRAY_SIZE(instance_str), MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - _mdns_set_u16(packet, data_len_location, part_length); - record_length += part_length; - return record_length; -} - -/** - * @brief appends DNS-SD PTR record for service to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param server the server that is hosting the service - * @param service the service to add record for - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_sdptr_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) -{ - const char *str[3]; - const char *sd_str[4]; - uint16_t record_length = 0; - uint8_t part_length; - - if (service == NULL) { - return 0; - } - - sd_str[0] = (char *)"_services"; - sd_str[1] = (char *)"_dns-sd"; - sd_str[2] = (char *)"_udp"; - sd_str[3] = MDNS_DEFAULT_DOMAIN; - - str[0] = service->service; - str[1] = service->proto; - str[2] = MDNS_DEFAULT_DOMAIN; - - part_length = _mdns_append_fqdn(packet, index, sd_str, 4, MDNS_MAX_PACKET_SIZE); - - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - part_length = _mdns_append_fqdn(packet, index, str, 3, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - _mdns_set_u16(packet, data_len_location, part_length); - record_length += part_length; - return record_length; -} - -/** - * @brief appends TXT record for service to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param server the server that is hosting the service - * @param service the service to add record for - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_txt_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) -{ - const char *str[4]; - uint16_t record_length = 0; - uint8_t part_length; - - if (service == NULL) { - return 0; - } - - str[0] = _mdns_get_service_instance_name(service); - str[1] = service->service; - str[2] = service->proto; - str[3] = MDNS_DEFAULT_DOMAIN; - - if (!str[0]) { - return 0; - } - - part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_TXT, flush, bye ? 0 : MDNS_ANSWER_TXT_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - uint16_t data_len = 0; - - mdns_txt_linked_item_t *txt = service->txt; - while (txt) { - int l = append_one_txt_record_entry(packet, index, txt); - if (l > 0) { - data_len += l; - } else if (l == 0) { // TXT entry won't fit into the mdns packet - return 0; - } - txt = txt->next; - } - if (!data_len) { - data_len = 1; - packet[*index] = 0; - *index = *index + 1; - } - _mdns_set_u16(packet, data_len_location, data_len); - record_length += data_len; - return record_length; -} - -/** - * @brief appends SRV record for service to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param server the server that is hosting the service - * @param service the service to add record for - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_srv_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) -{ - const char *str[4]; - uint16_t record_length = 0; - uint8_t part_length; - - if (service == NULL) { - return 0; - } - - str[0] = _mdns_get_service_instance_name(service); - str[1] = service->service; - str[2] = service->proto; - str[3] = MDNS_DEFAULT_DOMAIN; - - if (!str[0]) { - return 0; - } - - part_length = _mdns_append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_SRV, flush, bye ? 0 : MDNS_ANSWER_SRV_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - - part_length = 0; - part_length += _mdns_append_u16(packet, index, service->priority); - part_length += _mdns_append_u16(packet, index, service->weight); - part_length += _mdns_append_u16(packet, index, service->port); - if (part_length != 6) { - return 0; - } - - if (service->hostname) { - str[0] = service->hostname; - } else { - str[0] = _mdns_server->hostname; - } - str[1] = MDNS_DEFAULT_DOMAIN; - - if (_str_null_or_empty(str[0])) { - return 0; - } - - part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - _mdns_set_u16(packet, data_len_location, part_length + 6); - - record_length += part_length + 6; - return record_length; -} - -#ifdef CONFIG_LWIP_IPV4 -/** - * @brief appends A record to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param hostname the hostname address to add - * @param ip the IP address to add - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_a_record(uint8_t *packet, uint16_t *index, const char *hostname, uint32_t ip, bool flush, bool bye) -{ - const char *str[2]; - uint16_t record_length = 0; - uint8_t part_length; - - str[0] = hostname; - str[1] = MDNS_DEFAULT_DOMAIN; - - if (_str_null_or_empty(str[0])) { - return 0; - } - - part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_A, flush, bye ? 0 : MDNS_ANSWER_A_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - - if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) { - return 0; - } - _mdns_append_u8(packet, index, ip & 0xFF); - _mdns_append_u8(packet, index, (ip >> 8) & 0xFF); - _mdns_append_u8(packet, index, (ip >> 16) & 0xFF); - _mdns_append_u8(packet, index, (ip >> 24) & 0xFF); - _mdns_set_u16(packet, data_len_location, 4); - - record_length += 4; - return record_length; -} -#endif /* CONFIG_LWIP_IPV4 */ - -#ifdef CONFIG_LWIP_IPV6 -/** - * @brief appends AAAA record to a packet, incrementing the index - * - * @param packet MDNS packet - * @param index offset in the packet - * @param hostname the hostname address to add - * @param ipv6 the IPv6 address to add - * - * @return length of added data: 0 on error or length on success - */ -static uint16_t _mdns_append_aaaa_record(uint8_t *packet, uint16_t *index, const char *hostname, uint8_t *ipv6, bool flush, bool bye) -{ - const char *str[2]; - uint16_t record_length = 0; - uint8_t part_length; - - str[0] = hostname; - str[1] = MDNS_DEFAULT_DOMAIN; - - if (_str_null_or_empty(str[0])) { - return 0; - } - - - part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - record_length += part_length; - - part_length = _mdns_append_type(packet, index, MDNS_ANSWER_AAAA, flush, bye ? 0 : MDNS_ANSWER_AAAA_TTL); - if (!part_length) { - return 0; - } - record_length += part_length; - - uint16_t data_len_location = *index - 2; - - if ((*index + MDNS_ANSWER_AAAA_SIZE) > MDNS_MAX_PACKET_SIZE) { - return 0; - } - - part_length = MDNS_ANSWER_AAAA_SIZE; - memcpy(packet + *index, ipv6, part_length); - *index += part_length; - _mdns_set_u16(packet, data_len_location, part_length); - record_length += part_length; - return record_length; -} -#endif - -/** - * @brief Append question to packet - */ -static uint16_t _mdns_append_question(uint8_t *packet, uint16_t *index, mdns_out_question_t *q) -{ - uint8_t part_length; -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - if (q->host && (strstr(q->host, "in-addr") || strstr(q->host, "ip6"))) { - part_length = append_fqdn_dots(packet, index, q->host, false); - if (!part_length) { - return 0; - } - } else -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - { - const char *str[4]; - uint8_t str_index = 0; - if (q->host) { - str[str_index++] = q->host; - } - if (q->service) { - str[str_index++] = q->service; - } - if (q->proto) { - str[str_index++] = q->proto; - } - if (q->domain) { - str[str_index++] = q->domain; - } - part_length = _mdns_append_fqdn(packet, index, str, str_index, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - } - - part_length += _mdns_append_u16(packet, index, q->type); - part_length += _mdns_append_u16(packet, index, q->unicast ? 0x8001 : 0x0001); - return part_length; -} - -/** - * @brief Helper to get either ETH or STA if the other is provided - * Used when two interfaces are on the same subnet - */ -static mdns_if_t _mdns_get_other_if(mdns_if_t tcpip_if) -{ - if (tcpip_if < MDNS_MAX_INTERFACES) { - return s_esp_netifs[tcpip_if].duplicate; - } - return MDNS_MAX_INTERFACES; -} - -/** - * @brief Check if interface is duplicate (two interfaces on the same subnet) - */ -static bool _mdns_if_is_dup(mdns_if_t tcpip_if) -{ - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (other_if == MDNS_MAX_INTERFACES) { - return false; - } - if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP - || _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP - || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP - || _mdns_server->interfaces[other_if].pcbs[MDNS_IP_PROTOCOL_V6].state == PCB_DUP - ) { - return true; - } - return false; -} - -#ifdef CONFIG_LWIP_IPV6 -/** - * @brief Check if IPv6 address is NULL - */ -static bool _ipv6_address_is_zero(esp_ip6_addr_t ip6) -{ - uint8_t i; - uint8_t *data = (uint8_t *)ip6.addr; - for (i = 0; i < _MDNS_SIZEOF_IP6_ADDR; i++) { - if (data[i]) { - return false; - } - } - return true; -} -#endif /* CONFIG_LWIP_IPV6 */ - -static uint8_t _mdns_append_host_answer(uint8_t *packet, uint16_t *index, mdns_host_item_t *host, - uint8_t address_type, bool flush, bool bye) -{ - mdns_ip_addr_t *addr = host->address_list; - uint8_t num_records = 0; - - while (addr != NULL) { - if (addr->addr.type == address_type) { -#ifdef CONFIG_LWIP_IPV4 - if (address_type == ESP_IPADDR_TYPE_V4 && - _mdns_append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) { - break; - } -#endif /* CONFIG_LWIP_IPV4 */ -#ifdef CONFIG_LWIP_IPV6 - if (address_type == ESP_IPADDR_TYPE_V6 && - _mdns_append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush, - bye) <= 0) { - break; - } -#endif /* CONFIG_LWIP_IPV6 */ - num_records++; - } - addr = addr->next; - } - return num_records; -} - -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES -/** - * @brief Appends reverse lookup PTR record - */ -static uint8_t _mdns_append_reverse_ptr_record(uint8_t *packet, uint16_t *index, const char *name) -{ - if (strstr(name, "in-addr") == NULL && strstr(name, "ip6") == NULL) { - return 0; - } - - if (!append_fqdn_dots(packet, index, name, false)) { - return 0; - } - - if (!_mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, 10 /* TTL set to 10s*/)) { - return 0; - } - - uint16_t data_len_location = *index - 2; /* store the position of size (2=16bis) of this record */ - const char *str[2] = { _mdns_self_host.hostname, MDNS_DEFAULT_DOMAIN }; - - int part_length = _mdns_append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); - if (!part_length) { - return 0; - } - - _mdns_set_u16(packet, data_len_location, part_length); - return 1; /* appending only 1 record */ -} -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - -/** - * @brief Append PTR answers to packet - * - * @return number of answers added to the packet - */ -static uint8_t _mdns_append_service_ptr_answers(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, - bool bye) -{ - uint8_t appended_answers = 0; - - if (_mdns_append_ptr_record(packet, index, _mdns_get_service_instance_name(service), service->service, - service->proto, flush, bye) <= 0) { - return appended_answers; - } - appended_answers++; - - mdns_subtype_t *subtype = service->subtype; - while (subtype) { - appended_answers += - (_mdns_append_subtype_ptr_record(packet, index, _mdns_get_service_instance_name(service), subtype->subtype, - service->service, service->proto, flush, bye) > 0); - subtype = subtype->next; - } - - return appended_answers; -} - - -/** - * @brief Append answer to packet - * - * @return number of answers added to the packet - */ -static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_answer_t *answer, mdns_if_t tcpip_if) -{ - if (answer->host) { - bool is_host_valid = (&_mdns_self_host == answer->host); - mdns_host_item_t *target_host = _mdns_host_list; - while (target_host && !is_host_valid) { - if (target_host == answer->host) { - is_host_valid = true; - } - target_host = target_host->next; - } - if (!is_host_valid) { - return 0; - } - } - - if (answer->type == MDNS_TYPE_PTR) { - if (answer->service) { - return _mdns_append_service_ptr_answers(packet, index, answer->service, answer->flush, answer->bye); -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - } else if (answer->host && answer->host->hostname && - (strstr(answer->host->hostname, "in-addr") || strstr(answer->host->hostname, "ip6"))) { - return _mdns_append_reverse_ptr_record(packet, index, answer->host->hostname) > 0; -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - } else { - return _mdns_append_ptr_record(packet, index, - answer->custom_instance, answer->custom_service, answer->custom_proto, - answer->flush, answer->bye) > 0; - } - } else if (answer->type == MDNS_TYPE_SRV) { - return _mdns_append_srv_record(packet, index, answer->service, answer->flush, answer->bye) > 0; - } else if (answer->type == MDNS_TYPE_TXT) { - return _mdns_append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0; - } else if (answer->type == MDNS_TYPE_SDPTR) { - return _mdns_append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0; - } -#ifdef CONFIG_LWIP_IPV4 - else if (answer->type == MDNS_TYPE_A) { - if (answer->host == &_mdns_self_host) { - esp_netif_ip_info_t if_ip_info; - if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) { - return 0; - } - if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) { - return 0; - } - if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) { - return 0; - } - if (!_mdns_if_is_dup(tcpip_if)) { - return 1; - } - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &if_ip_info)) { - return 1; - } - if (_mdns_append_a_record(packet, index, _mdns_server->hostname, if_ip_info.ip.addr, answer->flush, answer->bye) > 0) { - return 2; - } - return 1; - } else if (answer->host != NULL) { - return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye); - } - } -#endif /* CONFIG_LWIP_IPV4 */ -#ifdef CONFIG_LWIP_IPV6 - else if (answer->type == MDNS_TYPE_AAAA) { - if (answer->host == &_mdns_self_host) { - struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS]; - uint8_t count = 0; - if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V6) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) { - return 0; - } - count = esp_netif_get_all_ip6(_mdns_get_esp_netif(tcpip_if), if_ip6s); - assert(count <= NETIF_IPV6_MAX_NUMS); - for (int i = 0; i < count; i++) { - if (_ipv6_address_is_zero(if_ip6s[i])) { - return 0; - } - if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)if_ip6s[i].addr, - answer->flush, answer->bye) <= 0) { - return 0; - } - } - if (!_mdns_if_is_dup(tcpip_if)) { - return count; - } - - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - struct esp_ip6_addr other_ip6; - if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) { - return count; - } - if (_mdns_append_aaaa_record(packet, index, _mdns_server->hostname, (uint8_t *)other_ip6.addr, - answer->flush, answer->bye) > 0) { - return 1 + count; - } - return count; - } else if (answer->host != NULL) { - return _mdns_append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush, - answer->bye); - } - } -#endif /* CONFIG_LWIP_IPV6 */ - return 0; -} - -/** - * @brief sends a packet - * - * @param p the packet - */ -static void _mdns_dispatch_tx_packet(mdns_tx_packet_t *p) -{ - static uint8_t packet[MDNS_MAX_PACKET_SIZE]; - uint16_t index = MDNS_HEAD_LEN; - memset(packet, 0, MDNS_HEAD_LEN); - mdns_out_question_t *q; - mdns_out_answer_t *a; - uint8_t count; - - _mdns_set_u16(packet, MDNS_HEAD_FLAGS_OFFSET, p->flags); - _mdns_set_u16(packet, MDNS_HEAD_ID_OFFSET, p->id); - - count = 0; - q = p->questions; - while (q) { - if (_mdns_append_question(packet, &index, q)) { - count++; - } - q = q->next; - } - _mdns_set_u16(packet, MDNS_HEAD_QUESTIONS_OFFSET, count); - - count = 0; - a = p->answers; - while (a) { - count += _mdns_append_answer(packet, &index, a, p->tcpip_if); - a = a->next; - } - _mdns_set_u16(packet, MDNS_HEAD_ANSWERS_OFFSET, count); - - count = 0; - a = p->servers; - while (a) { - count += _mdns_append_answer(packet, &index, a, p->tcpip_if); - a = a->next; - } - _mdns_set_u16(packet, MDNS_HEAD_SERVERS_OFFSET, count); - - count = 0; - a = p->additional; - while (a) { - count += _mdns_append_answer(packet, &index, a, p->tcpip_if); - a = a->next; - } - _mdns_set_u16(packet, MDNS_HEAD_ADDITIONAL_OFFSET, count); - -#ifdef MDNS_ENABLE_DEBUG - _mdns_dbg_printf("\nTX[%lu][%lu]: ", (unsigned long)p->tcpip_if, (unsigned long)p->ip_protocol); -#ifdef CONFIG_LWIP_IPV4 - if (p->dst.type == ESP_IPADDR_TYPE_V4) { - _mdns_dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port); - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (p->dst.type == ESP_IPADDR_TYPE_V6) { - _mdns_dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port); - } -#endif - mdns_debug_packet(packet, index); -#endif - - _mdns_udp_pcb_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index); -} - -/** - * @brief frees a packet - * - * @param packet the packet - */ -static void _mdns_free_tx_packet(mdns_tx_packet_t *packet) -{ - if (!packet) { - return; - } - mdns_out_question_t *q = packet->questions; - while (q) { - mdns_out_question_t *next = q->next; - if (q->own_dynamic_memory) { - mdns_mem_free((char *)q->host); - mdns_mem_free((char *)q->service); - mdns_mem_free((char *)q->proto); - mdns_mem_free((char *)q->domain); - } - mdns_mem_free(q); - q = next; - } - queueFree(mdns_out_answer_t, packet->answers); - queueFree(mdns_out_answer_t, packet->servers); - queueFree(mdns_out_answer_t, packet->additional); - mdns_mem_free(packet); -} - -/** - * @brief schedules a packet to be sent after given milliseconds - * - * @param packet the packet - * @param ms_after number of milliseconds after which the packet should be dispatched - */ -static void _mdns_schedule_tx_packet(mdns_tx_packet_t *packet, uint32_t ms_after) -{ - if (!packet) { - return; - } - packet->send_at = (xTaskGetTickCount() * portTICK_PERIOD_MS) + ms_after; - packet->next = NULL; - if (!_mdns_server->tx_queue_head || _mdns_server->tx_queue_head->send_at > packet->send_at) { - packet->next = _mdns_server->tx_queue_head; - _mdns_server->tx_queue_head = packet; - return; - } - mdns_tx_packet_t *q = _mdns_server->tx_queue_head; - while (q->next && q->next->send_at <= packet->send_at) { - q = q->next; - } - packet->next = q->next; - q->next = packet; -} - -/** - * @brief free all packets scheduled for sending - */ -static void _mdns_clear_tx_queue_head(void) -{ - mdns_tx_packet_t *q; - while (_mdns_server->tx_queue_head) { - q = _mdns_server->tx_queue_head; - _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next; - _mdns_free_tx_packet(q); - } -} - -/** - * @brief clear packets scheduled for sending on a specific interface - * - * @param tcpip_if the interface - * @param ip_protocol pcb type V4/V6 - */ -static void _mdns_clear_pcb_tx_queue_head(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_tx_packet_t *q, * p; - while (_mdns_server->tx_queue_head && _mdns_server->tx_queue_head->tcpip_if == tcpip_if && _mdns_server->tx_queue_head->ip_protocol == ip_protocol) { - q = _mdns_server->tx_queue_head; - _mdns_server->tx_queue_head = _mdns_server->tx_queue_head->next; - _mdns_free_tx_packet(q); - } - if (_mdns_server->tx_queue_head) { - q = _mdns_server->tx_queue_head; - while (q->next) { - if (q->next->tcpip_if == tcpip_if && q->next->ip_protocol == ip_protocol) { - p = q->next; - q->next = p->next; - _mdns_free_tx_packet(p); - } else { - q = q->next; - } - } - } -} - -/** - * @brief get the next packet scheduled for sending on a specific interface - * - * @param tcpip_if the interface - * @param ip_protocol pcb type V4/V6 - */ -static mdns_tx_packet_t *_mdns_get_next_pcb_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_tx_packet_t *q = _mdns_server->tx_queue_head; - while (q) { - if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol) { - return q; - } - q = q->next; - } - return NULL; -} - -/** - * @brief Find, remove and free answer from the scheduled packets - */ -static void _mdns_remove_scheduled_answer(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t *service) -{ - mdns_srv_item_t s = {NULL, NULL}; - if (!service) { - service = &s; - } - mdns_tx_packet_t *q = _mdns_server->tx_queue_head; - while (q) { - if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol && q->distributed) { - mdns_out_answer_t *a = q->answers; - if (a) { - if (a->type == type && a->service == service->service) { - q->answers = q->answers->next; - mdns_mem_free(a); - } else { - while (a->next) { - if (a->next->type == type && a->next->service == service->service) { - mdns_out_answer_t *b = a->next; - a->next = b->next; - mdns_mem_free(b); - break; - } - a = a->next; - } - } - } - } - q = q->next; - } -} - -/** - * @brief Remove and free answer from answer list (destination) - */ -static void _mdns_dealloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_srv_item_t *service) -{ - mdns_out_answer_t *d = *destination; - if (!d) { - return; - } - mdns_srv_item_t s = {NULL, NULL}; - if (!service) { - service = &s; - } - if (d->type == type && d->service == service->service) { - *destination = d->next; - mdns_mem_free(d); - return; - } - while (d->next) { - mdns_out_answer_t *a = d->next; - if (a->type == type && a->service == service->service) { - d->next = a->next; - mdns_mem_free(a); - return; - } - d = d->next; - } -} - -/** - * @brief Allocate new answer and add it to answer list (destination) - */ -static bool _mdns_alloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_service_t *service, - mdns_host_item_t *host, bool flush, bool bye) -{ - mdns_out_answer_t *d = *destination; - while (d) { - if (d->type == type && d->service == service && d->host == host) { - return true; - } - d = d->next; - } - - mdns_out_answer_t *a = (mdns_out_answer_t *)mdns_mem_malloc(sizeof(mdns_out_answer_t)); - if (!a) { - HOOK_MALLOC_FAILED; - return false; - } - a->type = type; - a->service = service; - a->host = host; - a->custom_service = NULL; - a->bye = bye; - a->flush = flush; - a->next = NULL; - queueToEnd(mdns_out_answer_t, *destination, a); - return true; -} - -/** - * @brief Allocate new packet for sending - */ -static mdns_tx_packet_t *_mdns_alloc_packet_default(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_tx_packet_t *packet = (mdns_tx_packet_t *)mdns_mem_malloc(sizeof(mdns_tx_packet_t)); - if (!packet) { - HOOK_MALLOC_FAILED; - return NULL; - } - memset((uint8_t *)packet, 0, sizeof(mdns_tx_packet_t)); - packet->tcpip_if = tcpip_if; - packet->ip_protocol = ip_protocol; - packet->port = MDNS_SERVICE_PORT; -#ifdef CONFIG_LWIP_IPV4 - if (ip_protocol == MDNS_IP_PROTOCOL_V4) { - esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251); - memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (ip_protocol == MDNS_IP_PROTOCOL_V6) { - esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000); - memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); - } -#endif - return packet; -} - -static bool _mdns_create_answer_from_service(mdns_tx_packet_t *packet, mdns_service_t *service, - mdns_parsed_question_t *question, bool shared, bool send_flush) -{ - mdns_host_item_t *host = mdns_get_host_item(service->hostname); - bool is_delegated = (host != &_mdns_self_host); - if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) { - // According to RFC6763-section12.1, for DNS-SD, SRV, TXT and all address records - // should be included in additional records. - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) || - !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_SRV, service, NULL, send_flush, false) || - !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_TXT, service, NULL, send_flush, false) || - !_mdns_alloc_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_A, service, host, send_flush, - false) || - !_mdns_alloc_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_AAAA, service, host, - send_flush, false)) { - return false; - } - } else if (question->type == MDNS_TYPE_SRV) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) || - !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) || - !_mdns_alloc_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) { - return false; - } - } else if (question->type == MDNS_TYPE_TXT) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false)) { - return false; - } - } else if (question->type == MDNS_TYPE_SDPTR) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, service, NULL, false, false)) { - return false; - } - } - return true; -} - -static bool _mdns_create_answer_from_hostname(mdns_tx_packet_t *packet, const char *hostname, bool send_flush) -{ - mdns_host_item_t *host = mdns_get_host_item(hostname); - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, send_flush, false) || - !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, send_flush, false)) { - return false; - } - return true; -} - -static bool _mdns_service_match_ptr_question(const mdns_service_t *service, const mdns_parsed_question_t *question) -{ - if (!_mdns_service_match(service, question->service, question->proto, NULL)) { - return false; - } - // The question parser stores anything before _type._proto in question->host - // So the question->host can be subtype or instance name based on its content - if (question->sub) { - mdns_subtype_t *subtype = service->subtype; - while (subtype) { - if (!strcasecmp(subtype->subtype, question->host)) { - return true; - } - subtype = subtype->next; - } - return false; - } - if (question->host) { - if (strcasecmp(_mdns_get_service_instance_name(service), question->host) != 0) { - return false; - } - } - return true; -} - -/** - * @brief Create answer packet to questions from parsed packet - */ -static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet) -{ - if (!parsed_packet->questions) { - return; - } - bool send_flush = parsed_packet->src_port == MDNS_SERVICE_PORT; - bool unicast = false; - bool shared = false; - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(parsed_packet->tcpip_if, parsed_packet->ip_protocol); - if (!packet) { - return; - } - packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; - packet->distributed = parsed_packet->distributed; - packet->id = parsed_packet->id; - - mdns_parsed_question_t *q = parsed_packet->questions; - uint32_t out_record_nums = 0; - while (q) { - shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe; - if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) { - mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL); - if (service == NULL) { // Service not found, but we continue to the next question - q = q->next; - continue; - } - if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } - } else if (q->service && q->proto) { - mdns_srv_item_t *service = _mdns_server->services; - while (service) { - if (_mdns_service_match_ptr_question(service->service, q)) { - mdns_parsed_record_t *r = parsed_packet->records; - bool is_record_exist = false; - while (r) { - if (service->service->instance && r->host) { - if (_mdns_service_match_instance(service->service, r->host, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) { - is_record_exist = true; - break; - } - } else if (!service->service->instance && !r->host) { - if (_mdns_service_match(service->service, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) { - is_record_exist = true; - break; - } - } - r = r->next; - } - if (!is_record_exist) { - if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } - } - } - service = service->next; - } - } else if (q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA) { - if (!_mdns_create_answer_from_hostname(packet, q->host, send_flush)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } - } else if (q->type == MDNS_TYPE_ANY) { - if (!_mdns_append_host_list(&packet->answers, send_flush, false)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - } else if (q->type == MDNS_TYPE_PTR) { - mdns_host_item_t *host = mdns_get_host_item(q->host); - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, NULL, host, send_flush, false)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - } else if (!_mdns_alloc_answer(&packet->answers, q->type, NULL, NULL, send_flush, false)) { - _mdns_free_tx_packet(packet); - return; - } else { - out_record_nums++; - } - - if (parsed_packet->src_port != MDNS_SERVICE_PORT && // Repeat the queries only for "One-Shot mDNS queries" - (q->type == MDNS_TYPE_ANY || q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - || q->type == MDNS_TYPE_PTR -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - )) { - mdns_out_question_t *out_question = mdns_mem_malloc(sizeof(mdns_out_question_t)); - if (out_question == NULL) { - HOOK_MALLOC_FAILED; - _mdns_free_tx_packet(packet); - return; - } - out_question->type = q->type; - out_question->unicast = q->unicast; - out_question->host = q->host; - q->host = NULL; - out_question->service = q->service; - q->service = NULL; - out_question->proto = q->proto; - q->proto = NULL; - out_question->domain = q->domain; - q->domain = NULL; - out_question->next = NULL; - out_question->own_dynamic_memory = true; - queueToEnd(mdns_out_question_t, packet->questions, out_question); - } - if (q->unicast) { - unicast = true; - } - q = q->next; - } - if (out_record_nums == 0) { - _mdns_free_tx_packet(packet); - return; - } - if (unicast || !send_flush) { - memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t)); - packet->port = parsed_packet->src_port; - } - - static uint8_t share_step = 0; - if (shared) { - _mdns_schedule_tx_packet(packet, 25 + (share_step * 25)); - share_step = (share_step + 1) & 0x03; - } else { - _mdns_dispatch_tx_packet(packet); - _mdns_free_tx_packet(packet); - } -} - -/** - * @brief Check if question is already in the list - */ -static bool _mdns_question_exists(mdns_out_question_t *needle, mdns_out_question_t *haystack) -{ - while (haystack) { - if (haystack->type == needle->type - && haystack->host == needle->host - && haystack->service == needle->service - && haystack->proto == needle->proto) { - return true; - } - haystack = haystack->next; - } - return false; -} - -static bool _mdns_append_host(mdns_out_answer_t **destination, mdns_host_item_t *host, bool flush, bool bye) -{ - if (!_mdns_alloc_answer(destination, MDNS_TYPE_A, NULL, host, flush, bye)) { - return false; - } - if (!_mdns_alloc_answer(destination, MDNS_TYPE_AAAA, NULL, host, flush, bye)) { - return false; - } - return true; -} - -static bool _mdns_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], - size_t services_len, bool flush, bool bye) -{ - if (services == NULL) { - mdns_host_item_t *host = mdns_get_host_item(_mdns_server->hostname); - if (host != NULL) { - return _mdns_append_host(destination, host, flush, bye); - } - return true; - } - for (size_t i = 0; i < services_len; i++) { - mdns_host_item_t *host = mdns_get_host_item(services[i]->service->hostname); - if (!_mdns_append_host(destination, host, flush, bye)) { - return false; - } - } - return true; -} - -static bool _mdns_append_host_list(mdns_out_answer_t **destination, bool flush, bool bye) -{ - if (!_str_null_or_empty(_mdns_server->hostname)) { - mdns_host_item_t *self_host = mdns_get_host_item(_mdns_server->hostname); - if (!_mdns_append_host(destination, self_host, flush, bye)) { - return false; - } - } - mdns_host_item_t *host = _mdns_host_list; - while (host != NULL) { - host = host->next; - if (!_mdns_append_host(destination, host, flush, bye)) { - return false; - } - } - return true; -} - -static bool _mdns_append_host_question(mdns_out_question_t **questions, const char *hostname, bool unicast) -{ - mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); - if (!q) { - HOOK_MALLOC_FAILED; - return false; - } - q->next = NULL; - q->unicast = unicast; - q->type = MDNS_TYPE_ANY; - q->host = hostname; - q->service = NULL; - q->proto = NULL; - q->domain = MDNS_DEFAULT_DOMAIN; - q->own_dynamic_memory = false; - if (_mdns_question_exists(q, *questions)) { - mdns_mem_free(q); - } else { - queueToEnd(mdns_out_question_t, *questions, q); - } - return true; -} - -static bool _mdns_append_host_questions_for_services(mdns_out_question_t **questions, mdns_srv_item_t *services[], - size_t len, bool unicast) -{ - if (!_str_null_or_empty(_mdns_server->hostname) && - !_mdns_append_host_question(questions, _mdns_server->hostname, unicast)) { - return false; - } - for (size_t i = 0; i < len; i++) { - if (!_mdns_append_host_question(questions, services[i]->service->hostname, unicast)) { - return false; - } - } - return true; -} - -/** - * @brief Create probe packet for particular services on particular PCB - */ -static mdns_tx_packet_t *_mdns_create_probe_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool first, bool include_ip) -{ - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); - if (!packet) { - return NULL; - } - - size_t i; - for (i = 0; i < len; i++) { - mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); - if (!q) { - HOOK_MALLOC_FAILED; - _mdns_free_tx_packet(packet); - return NULL; - } - q->next = NULL; - q->unicast = first; - q->type = MDNS_TYPE_ANY; - q->host = _mdns_get_service_instance_name(services[i]->service); - q->service = services[i]->service->service; - q->proto = services[i]->service->proto; - q->domain = MDNS_DEFAULT_DOMAIN; - q->own_dynamic_memory = false; - if (!q->host || _mdns_question_exists(q, packet->questions)) { - mdns_mem_free(q); - continue; - } else { - queueToEnd(mdns_out_question_t, packet->questions, q); - } - - if (!q->host || !_mdns_alloc_answer(&packet->servers, MDNS_TYPE_SRV, services[i]->service, NULL, false, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - } - - if (include_ip) { - if (!_mdns_append_host_questions_for_services(&packet->questions, services, len, first)) { - _mdns_free_tx_packet(packet); - return NULL; - } - - if (!_mdns_append_host_list_in_services(&packet->servers, services, len, false, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - } - - return packet; -} - -/** - * @brief Create announce packet for particular services on particular PCB - */ -static mdns_tx_packet_t *_mdns_create_announce_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool include_ip) -{ - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); - if (!packet) { - return NULL; - } - packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; - - uint8_t i; - for (i = 0; i < len; i++) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - } - if (include_ip) { - if (!_mdns_append_host_list_in_services(&packet->servers, services, len, true, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - } - return packet; -} - -/** - * @brief Convert probe packet to announce - */ -static mdns_tx_packet_t *_mdns_create_announce_from_probe(mdns_tx_packet_t *probe) -{ - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(probe->tcpip_if, probe->ip_protocol); - if (!packet) { - return NULL; - } - packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; - - mdns_out_answer_t *s = probe->servers; - while (s) { - if (s->type == MDNS_TYPE_SRV) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, s->service, NULL, false, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, s->service, NULL, false, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SRV, s->service, NULL, true, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_TXT, s->service, NULL, true, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - mdns_host_item_t *host = mdns_get_host_item(s->service->hostname); - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_A, NULL, host, true, false) - || !_mdns_alloc_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, true, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - - } else if (s->type == MDNS_TYPE_A || s->type == MDNS_TYPE_AAAA) { - if (!_mdns_alloc_answer(&packet->answers, s->type, NULL, s->host, true, false)) { - _mdns_free_tx_packet(packet); - return NULL; - } - } - - s = s->next; - } - return packet; -} - -/** - * @brief Send by for particular services on particular PCB - */ -static void _mdns_pcb_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip) -{ - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); - if (!packet) { - return; - } - packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; - size_t i; - for (i = 0; i < len; i++) { - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) { - _mdns_free_tx_packet(packet); - return; - } - } - if (include_ip) { - _mdns_append_host_list_in_services(&packet->answers, services, len, true, true); - } - _mdns_dispatch_tx_packet(packet); - _mdns_free_tx_packet(packet); -} - -/** - * @brief Send probe for additional services on particular PCB - */ -static void _mdns_init_pcb_probe_new_service(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip) -{ - mdns_pcb_t *pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; - size_t services_final_len = len; - - if (PCB_STATE_IS_PROBING(pcb)) { - services_final_len += pcb->probe_services_len; - } - mdns_srv_item_t **_services = NULL; - if (services_final_len) { - _services = (mdns_srv_item_t **)mdns_mem_malloc(sizeof(mdns_srv_item_t *) * services_final_len); - if (!_services) { - HOOK_MALLOC_FAILED; - return; - } - - size_t i; - for (i = 0; i < len; i++) { - _services[i] = services[i]; - } - if (pcb->probe_services) { - for (i = 0; i < pcb->probe_services_len; i++) { - _services[len + i] = pcb->probe_services[i]; - } - mdns_mem_free(pcb->probe_services); - } - } - - probe_ip = pcb->probe_ip || probe_ip; - - pcb->probe_ip = false; - pcb->probe_services = NULL; - pcb->probe_services_len = 0; - pcb->probe_running = false; - - mdns_tx_packet_t *packet = _mdns_create_probe_packet(tcpip_if, ip_protocol, _services, services_final_len, true, probe_ip); - if (!packet) { - mdns_mem_free(_services); - return; - } - - pcb->probe_ip = probe_ip; - pcb->probe_services = _services; - pcb->probe_services_len = services_final_len; - pcb->probe_running = true; - _mdns_schedule_tx_packet(packet, ((pcb->failed_probes > 5) ? 1000 : 120) + (esp_random() & 0x7F)); - pcb->state = PCB_PROBE_1; -} - -/** - * @brief Send probe for particular services on particular PCB - * - * Tests possible duplication on probing service structure and probes only for new entries. - * - If pcb probing then add only non-probing services and restarts probing - * - If pcb not probing, run probing for all specified services - */ -static void _mdns_init_pcb_probe(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip) -{ - mdns_pcb_t *pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; - - _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol); - - if (_str_null_or_empty(_mdns_server->hostname)) { - pcb->state = PCB_RUNNING; - return; - } - - if (PCB_STATE_IS_PROBING(pcb)) { - // Looking for already probing services to resolve duplications - mdns_srv_item_t *new_probe_services[len]; - int new_probe_service_len = 0; - bool found; - for (size_t j = 0; j < len; ++j) { - found = false; - for (int i = 0; i < pcb->probe_services_len; ++i) { - if (pcb->probe_services[i] == services[j]) { - found = true; - break; - } - } - if (!found) { - new_probe_services[new_probe_service_len++] = services[j]; - } - } - // init probing for newly added services - _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol, - new_probe_service_len ? new_probe_services : NULL, new_probe_service_len, probe_ip); - } else { - // not probing, so init for all services - _mdns_init_pcb_probe_new_service(tcpip_if, ip_protocol, services, len, probe_ip); - } -} - -/** - * @brief Restart the responder on particular PCB - */ -static void _mdns_restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - size_t srv_count = 0; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - srv_count++; - a = a->next; - } - if (srv_count == 0) { - // proble only IP - _mdns_init_pcb_probe(tcpip_if, ip_protocol, NULL, 0, true); - return; - } - mdns_srv_item_t *services[srv_count]; - size_t i = 0; - a = _mdns_server->services; - while (a) { - services[i++] = a; - a = a->next; - } - _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, srv_count, true); -} - -/** - * @brief Send by for particular services - */ -static void _mdns_send_bye(mdns_srv_item_t **services, size_t len, bool include_ip) -{ - uint8_t i, j; - if (_str_null_or_empty(_mdns_server->hostname)) { - return; - } - - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - if (mdns_is_netif_ready(i, j) && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) { - _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip); - } - } - } -} - -/** - * @brief Send bye for particular subtypes - */ -static void _mdns_send_bye_subtype(mdns_srv_item_t *service, const char *instance_name, mdns_subtype_t *remove_subtypes) -{ - uint8_t i, j; - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - if (mdns_is_netif_ready(i, j)) { - mdns_tx_packet_t *packet = _mdns_alloc_packet_default((mdns_if_t)i, (mdns_ip_protocol_t)j); - if (packet == NULL) { - return; - } - packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service->service, NULL, true, true)) { - _mdns_free_tx_packet(packet); - return; - } - - static uint8_t pkt[MDNS_MAX_PACKET_SIZE]; - uint16_t index = MDNS_HEAD_LEN; - memset(pkt, 0, MDNS_HEAD_LEN); - mdns_out_answer_t *a; - uint8_t count; - - _mdns_set_u16(pkt, MDNS_HEAD_FLAGS_OFFSET, packet->flags); - _mdns_set_u16(pkt, MDNS_HEAD_ID_OFFSET, packet->id); - - count = 0; - a = packet->answers; - while (a) { - if (a->type == MDNS_TYPE_PTR && a->service) { - const mdns_subtype_t *current_subtype = remove_subtypes; - while (current_subtype) { - count += (_mdns_append_subtype_ptr_record(pkt, &index, instance_name, current_subtype->subtype, a->service->service, a->service->proto, a->flush, a->bye) > 0); - current_subtype = current_subtype->next; - } - } - a = a->next; - } - _mdns_set_u16(pkt, MDNS_HEAD_ANSWERS_OFFSET, count); - - _mdns_udp_pcb_write(packet->tcpip_if, packet->ip_protocol, &packet->dst, packet->port, pkt, index); - - _mdns_free_tx_packet(packet); - } - } - } -} - -/** - * @brief Send announcement on particular PCB - */ -static void _mdns_announce_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip) -{ - mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol]; - size_t i; - if (mdns_is_netif_ready(tcpip_if, ip_protocol)) { - if (PCB_STATE_IS_PROBING(_pcb)) { - _mdns_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip); - } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) { - mdns_tx_packet_t *p = _mdns_get_next_pcb_packet(tcpip_if, ip_protocol); - if (p) { - for (i = 0; i < len; i++) { - if (!_mdns_alloc_answer(&p->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false) - || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false) - || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false) - || !_mdns_alloc_answer(&p->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) { - break; - } - } - if (include_ip) { - _mdns_dealloc_answer(&p->additional, MDNS_TYPE_A, NULL); - _mdns_dealloc_answer(&p->additional, MDNS_TYPE_AAAA, NULL); - _mdns_append_host_list_in_services(&p->answers, services, len, true, false); - } - _pcb->state = PCB_ANNOUNCE_1; - } - } else if (_pcb->state == PCB_RUNNING) { - - if (_str_null_or_empty(_mdns_server->hostname)) { - return; - } - - _pcb->state = PCB_ANNOUNCE_1; - mdns_tx_packet_t *p = _mdns_create_announce_packet(tcpip_if, ip_protocol, services, len, include_ip); - if (p) { - _mdns_schedule_tx_packet(p, 0); - } - } - } -} - -/** - * @brief Send probe on all active PCBs - */ -static void _mdns_probe_all_pcbs(mdns_srv_item_t **services, size_t len, bool probe_ip, bool clear_old_probe) -{ - uint8_t i, j; - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - if (mdns_is_netif_ready(i, j)) { - mdns_pcb_t *_pcb = &_mdns_server->interfaces[i].pcbs[j]; - if (clear_old_probe) { - mdns_mem_free(_pcb->probe_services); - _pcb->probe_services = NULL; - _pcb->probe_services_len = 0; - _pcb->probe_running = false; - } - _mdns_init_pcb_probe((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, probe_ip); - } - } - } -} - -/** - * @brief Send announcement on all active PCBs - */ -static void _mdns_announce_all_pcbs(mdns_srv_item_t **services, size_t len, bool include_ip) -{ - uint8_t i, j; - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - _mdns_announce_pcb((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip); - } - } -} - -/** - * @brief Restart the responder on all active PCBs - */ -static void _mdns_send_final_bye(bool include_ip) -{ - //collect all services and start probe - size_t srv_count = 0; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - srv_count++; - a = a->next; - } - if (!srv_count) { - return; - } - mdns_srv_item_t *services[srv_count]; - size_t i = 0; - a = _mdns_server->services; - while (a) { - services[i++] = a; - a = a->next; - } - _mdns_send_bye(services, srv_count, include_ip); -} - -/** - * @brief Stop the responder on all services without instance - */ -static void _mdns_send_bye_all_pcbs_no_instance(bool include_ip) -{ - size_t srv_count = 0; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - if (!a->service->instance) { - srv_count++; - } - a = a->next; - } - if (!srv_count) { - return; - } - mdns_srv_item_t *services[srv_count]; - size_t i = 0; - a = _mdns_server->services; - while (a) { - if (!a->service->instance) { - services[i++] = a; - } - a = a->next; - } - _mdns_send_bye(services, srv_count, include_ip); -} - -/** - * @brief Restart the responder on all services without instance - */ -static void _mdns_restart_all_pcbs_no_instance(void) -{ - size_t srv_count = 0; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - if (!a->service->instance) { - srv_count++; - } - a = a->next; - } - if (!srv_count) { - return; - } - mdns_srv_item_t *services[srv_count]; - size_t i = 0; - a = _mdns_server->services; - while (a) { - if (!a->service->instance) { - services[i++] = a; - } - a = a->next; - } - _mdns_probe_all_pcbs(services, srv_count, false, true); -} - -/** - * @brief Restart the responder on all active PCBs - */ -static void _mdns_restart_all_pcbs(void) -{ - _mdns_clear_tx_queue_head(); - size_t srv_count = 0; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - srv_count++; - a = a->next; - } - if (srv_count == 0) { - _mdns_probe_all_pcbs(NULL, 0, true, true); - return; - } - mdns_srv_item_t *services[srv_count]; - size_t l = 0; - a = _mdns_server->services; - while (a) { - services[l++] = a; - a = a->next; - } - - _mdns_probe_all_pcbs(services, srv_count, true, true); -} - - - -/** - * @brief creates/allocates new text item list - * @param num_items service number of txt items or 0 - * @param txt service txt items array or NULL - * - * @return pointer to the linked txt item list or NULL - */ -static mdns_txt_linked_item_t *_mdns_allocate_txt(size_t num_items, mdns_txt_item_t txt[]) -{ - mdns_txt_linked_item_t *new_txt = NULL; - size_t i = 0; - if (num_items) { - for (i = 0; i < num_items; i++) { - mdns_txt_linked_item_t *new_item = (mdns_txt_linked_item_t *)mdns_mem_malloc(sizeof(mdns_txt_linked_item_t)); - if (!new_item) { - HOOK_MALLOC_FAILED; - break; - } - new_item->key = mdns_mem_strdup(txt[i].key); - if (!new_item->key) { - mdns_mem_free(new_item); - break; - } - if (txt[i].value) { - new_item->value = mdns_mem_strdup(txt[i].value); - if (!new_item->value) { - mdns_mem_free((char *)new_item->key); - mdns_mem_free(new_item); - break; - } - new_item->value_len = strlen(new_item->value); - } else { - new_item->value = NULL; - new_item->value_len = 0; - } - new_item->next = new_txt; - new_txt = new_item; - } - } - return new_txt; -} - -/** - * @brief Deallocate the txt linked list - * @param txt pointer to the txt pointer to free, noop if txt==NULL - */ -static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt) -{ - mdns_txt_linked_item_t *t; - while (txt) { - t = txt; - txt = txt->next; - mdns_mem_free((char *)t->value); - mdns_mem_free((char *)t->key); - mdns_mem_free(t); - } -} - -/** - * @brief creates/allocates new service - * @param service service type - * @param proto service proto - * @param hostname service hostname - * @param port service port - * @param instance service instance - * @param num_items service number of txt items or 0 - * @param txt service txt items array or NULL - * - * @return pointer to the service or NULL on error - */ -static mdns_service_t *_mdns_create_service(const char *service, const char *proto, const char *hostname, - uint16_t port, const char *instance, size_t num_items, - mdns_txt_item_t txt[]) -{ - mdns_service_t *s = (mdns_service_t *)mdns_mem_calloc(1, sizeof(mdns_service_t)); - if (!s) { - HOOK_MALLOC_FAILED; - return NULL; - } - - mdns_txt_linked_item_t *new_txt = _mdns_allocate_txt(num_items, txt); - if (num_items && new_txt == NULL) { - goto fail; - } - - s->priority = 0; - s->weight = 0; - s->instance = instance ? mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1) : NULL; - s->txt = new_txt; - s->port = port; - s->subtype = NULL; - - if (hostname) { - s->hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); - if (!s->hostname) { - goto fail; - } - } else { - s->hostname = NULL; - } - - s->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); - if (!s->service) { - goto fail; - } - - s->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); - if (!s->proto) { - goto fail; - } - return s; - -fail: - _mdns_free_linked_txt(s->txt); - mdns_mem_free((char *)s->instance); - mdns_mem_free((char *)s->service); - mdns_mem_free((char *)s->proto); - mdns_mem_free((char *)s->hostname); - mdns_mem_free(s); - - return NULL; -} - -/** - * @brief Remove and free service answer from answer list (destination) - */ -static void _mdns_dealloc_scheduled_service_answers(mdns_out_answer_t **destination, mdns_service_t *service) -{ - mdns_out_answer_t *d = *destination; - if (!d) { - return; - } - while (d && d->service == service) { - *destination = d->next; - mdns_mem_free(d); - d = *destination; - } - while (d && d->next) { - mdns_out_answer_t *a = d->next; - if (a->service == service) { - d->next = a->next; - mdns_mem_free(a); - } else { - d = d->next; - } - } -} - -/** - * @brief Find, remove and free answers and scheduled packets for service - */ -static void _mdns_remove_scheduled_service_packets(mdns_service_t *service) -{ - if (!service) { - return; - } - mdns_tx_packet_t *p = NULL; - mdns_tx_packet_t *q = _mdns_server->tx_queue_head; - while (q) { - bool had_answers = (q->answers != NULL); - - _mdns_dealloc_scheduled_service_answers(&(q->answers), service); - _mdns_dealloc_scheduled_service_answers(&(q->additional), service); - _mdns_dealloc_scheduled_service_answers(&(q->servers), service); - - - mdns_pcb_t *_pcb = &_mdns_server->interfaces[q->tcpip_if].pcbs[q->ip_protocol]; - if (mdns_is_netif_ready(q->tcpip_if, q->ip_protocol)) { - if (PCB_STATE_IS_PROBING(_pcb)) { - uint8_t i; - //check if we are probing this service - for (i = 0; i < _pcb->probe_services_len; i++) { - mdns_srv_item_t *s = _pcb->probe_services[i]; - if (s->service == service) { - break; - } - } - if (i < _pcb->probe_services_len) { - if (_pcb->probe_services_len > 1) { - uint8_t n; - for (n = (i + 1); n < _pcb->probe_services_len; n++) { - _pcb->probe_services[n - 1] = _pcb->probe_services[n]; - } - _pcb->probe_services_len--; - } else { - _pcb->probe_services_len = 0; - mdns_mem_free(_pcb->probe_services); - _pcb->probe_services = NULL; - if (!_pcb->probe_ip) { - _pcb->probe_running = false; - _pcb->state = PCB_RUNNING; - } - } - - if (q->questions) { - mdns_out_question_t *qsn = NULL; - mdns_out_question_t *qs = q->questions; - if (qs->type == MDNS_TYPE_ANY - && qs->service && strcmp(qs->service, service->service) == 0 - && qs->proto && strcmp(qs->proto, service->proto) == 0) { - q->questions = q->questions->next; - mdns_mem_free(qs); - } else while (qs->next) { - qsn = qs->next; - if (qsn->type == MDNS_TYPE_ANY - && qsn->service && strcmp(qsn->service, service->service) == 0 - && qsn->proto && strcmp(qsn->proto, service->proto) == 0) { - qs->next = qsn->next; - mdns_mem_free(qsn); - break; - } - qs = qs->next; - } - } - } - } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) { - //if answers were cleared, set to running - if (had_answers && q->answers == NULL) { - _pcb->state = PCB_RUNNING; - } - } - } - - p = q; - q = q->next; - if (!p->questions && !p->answers && !p->additional && !p->servers) { - queueDetach(mdns_tx_packet_t, _mdns_server->tx_queue_head, p); - _mdns_free_tx_packet(p); - } - } -} - -static void _mdns_free_subtype(mdns_subtype_t *subtype) -{ - while (subtype) { - mdns_subtype_t *next = subtype->next; - mdns_mem_free((char *)subtype->subtype); - mdns_mem_free(subtype); - subtype = next; - } -} - -static void _mdns_free_service_subtype(mdns_service_t *service) -{ - _mdns_free_subtype(service->subtype); - service->subtype = NULL; -} - -/** - * @brief free service memory - * - * @param service the service - */ -static void _mdns_free_service(mdns_service_t *service) -{ - if (!service) { - return; - } - mdns_mem_free((char *)service->instance); - mdns_mem_free((char *)service->service); - mdns_mem_free((char *)service->proto); - mdns_mem_free((char *)service->hostname); - while (service->txt) { - mdns_txt_linked_item_t *s = service->txt; - service->txt = service->txt->next; - mdns_mem_free((char *)s->key); - mdns_mem_free((char *)s->value); - mdns_mem_free(s); - } - _mdns_free_service_subtype(service); - mdns_mem_free(service); -} - - -/* - * Received Packet Handling - * */ - -/** - * @brief Detect SRV collision - */ -static int _mdns_check_srv_collision(mdns_service_t *service, uint16_t priority, uint16_t weight, uint16_t port, const char *host, const char *domain) -{ - if (_str_null_or_empty(_mdns_server->hostname)) { - return 0; - } - - size_t our_host_len = strlen(_mdns_server->hostname); - size_t our_len = 14 + our_host_len; - - size_t their_host_len = strlen(host); - size_t their_domain_len = strlen(domain); - size_t their_len = 9 + their_host_len + their_domain_len; - - if (their_len > our_len) { - return 1;//they win - } else if (their_len < our_len) { - return -1;//we win - } - - uint16_t our_index = 0; - uint8_t our_data[our_len]; - _mdns_append_u16(our_data, &our_index, service->priority); - _mdns_append_u16(our_data, &our_index, service->weight); - _mdns_append_u16(our_data, &our_index, service->port); - our_data[our_index++] = our_host_len; - memcpy(our_data + our_index, _mdns_server->hostname, our_host_len); - our_index += our_host_len; - our_data[our_index++] = 5; - memcpy(our_data + our_index, MDNS_DEFAULT_DOMAIN, 5); - our_index += 5; - our_data[our_index++] = 0; - - uint16_t their_index = 0; - uint8_t their_data[their_len]; - _mdns_append_u16(their_data, &their_index, priority); - _mdns_append_u16(their_data, &their_index, weight); - _mdns_append_u16(their_data, &their_index, port); - their_data[their_index++] = their_host_len; - memcpy(their_data + their_index, host, their_host_len); - their_index += their_host_len; - their_data[their_index++] = their_domain_len; - memcpy(their_data + their_index, domain, their_domain_len); - their_index += their_domain_len; - their_data[their_index++] = 0; - - int ret = memcmp(our_data, their_data, our_len); - if (ret > 0) { - return -1;//we win - } else if (ret < 0) { - return 1;//they win - } - return 0;//same -} - -/** - * @brief Detect TXT collision - */ -static int _mdns_check_txt_collision(mdns_service_t *service, const uint8_t *data, size_t len) -{ - size_t data_len = 0; - if (len <= 1 && service->txt) { // len==0 means incorrect packet (and handled by the packet parser) - // but handled here again to fix clang-tidy warning on VLA "uint8_t our[0];" - return -1;//we win - } else if (len > 1 && !service->txt) { - return 1;//they win - } else if (len <= 1 && !service->txt) { - return 0;//same - } - - mdns_txt_linked_item_t *txt = service->txt; - while (txt) { - data_len += 1 /* record-len */ + strlen(txt->key) + txt->value_len + (txt->value ? 1 : 0 /* "=" */); - txt = txt->next; - } - - if (len > data_len) { - return 1;//they win - } else if (len < data_len) { - return -1;//we win - } - - uint8_t ours[len]; - uint16_t index = 0; - - txt = service->txt; - while (txt) { - append_one_txt_record_entry(ours, &index, txt); - txt = txt->next; - } - - int ret = memcmp(ours, data, len); - if (ret > 0) { - return -1;//we win - } else if (ret < 0) { - return 1;//they win - } - return 0;//same -} - -static esp_err_t mdns_pcb_deinit_local(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_proto) -{ - esp_err_t err = _mdns_pcb_deinit(tcpip_if, ip_proto); - mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_proto]; - if (_pcb == NULL || err != ESP_OK) { - return err; - } - mdns_mem_free(_pcb->probe_services); - _pcb->state = PCB_OFF; - _pcb->probe_ip = false; - _pcb->probe_services = NULL; - _pcb->probe_services_len = 0; - _pcb->probe_running = false; - _pcb->failed_probes = 0; - return ESP_OK; -} -/** - * @brief Set interface as duplicate if another is found on the same subnet - */ -static void _mdns_dup_interface(mdns_if_t tcpip_if) -{ - uint8_t i; - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (other_if == MDNS_MAX_INTERFACES) { - return; // no other interface found - } - for (i = 0; i < MDNS_IP_PROTOCOL_MAX; i++) { - if (mdns_is_netif_ready(other_if, i)) { - //stop this interface and mark as dup - if (mdns_is_netif_ready(tcpip_if, i)) { - _mdns_clear_pcb_tx_queue_head(tcpip_if, i); - mdns_pcb_deinit_local(tcpip_if, i); - } - _mdns_server->interfaces[tcpip_if].pcbs[i].state = PCB_DUP; - _mdns_announce_pcb(other_if, i, NULL, 0, true); - } - } -} - -#ifdef CONFIG_LWIP_IPV4 -/** - * @brief Detect IPv4 address collision - */ -static int _mdns_check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if) -{ - esp_netif_ip_info_t if_ip_info; - esp_netif_ip_info_t other_ip_info; - if (!ip->addr) { - return 1;//denial! they win - } - if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) { - return 1;//they win - } - int ret = memcmp((uint8_t *)&if_ip_info.ip.addr, (uint8_t *)&ip->addr, sizeof(esp_ip4_addr_t)); - if (ret > 0) { - return -1;//we win - } else if (ret < 0) { - //is it the other interface? - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (other_if == MDNS_MAX_INTERFACES) { - return 1;//AP interface! They win - } - if (esp_netif_get_ip_info(_mdns_get_esp_netif(other_if), &other_ip_info)) { - return 1;//IPv4 not active! They win - } - if (ip->addr != other_ip_info.ip.addr) { - return 1;//IPv4 not ours! They win - } - _mdns_dup_interface(tcpip_if); - return 2;//they win - } - return 0;//same -} -#endif /* CONFIG_LWIP_IPV4 */ - -#ifdef CONFIG_LWIP_IPV6 -/** - * @brief Detect IPv6 address collision - */ -static int _mdns_check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if) -{ - struct esp_ip6_addr if_ip6; - struct esp_ip6_addr other_ip6; - if (_ipv6_address_is_zero(*ip)) { - return 1;//denial! they win - } - if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(tcpip_if), &if_ip6)) { - return 1;//they win - } - int ret = memcmp((uint8_t *)&if_ip6.addr, (uint8_t *)ip->addr, _MDNS_SIZEOF_IP6_ADDR); - if (ret > 0) { - return -1;//we win - } else if (ret < 0) { - //is it the other interface? - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (other_if == MDNS_MAX_INTERFACES) { - return 1;//AP interface! They win - } - if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(other_if), &other_ip6)) { - return 1;//IPv6 not active! They win - } - if (memcmp((uint8_t *)&other_ip6.addr, (uint8_t *)ip->addr, _MDNS_SIZEOF_IP6_ADDR)) { - return 1;//IPv6 not ours! They win - } - _mdns_dup_interface(tcpip_if); - return 2;//they win - } - return 0;//same -} -#endif /* CONFIG_LWIP_IPV6 */ - -static bool _hostname_is_ours(const char *hostname) -{ - if (!_str_null_or_empty(_mdns_server->hostname) && - strcasecmp(hostname, _mdns_server->hostname) == 0) { - return true; - } - mdns_host_item_t *host = _mdns_host_list; - while (host != NULL) { - if (strcasecmp(hostname, host->hostname) == 0) { - return true; - } - host = host->next; - } - return false; -} - -/** - * @brief Adds a delegated hostname to the linked list - * @param hostname Host name pointer - * @param address_list Address list - * @return true on success - * false if the host wasn't attached (this is our hostname, or alloc failure) so we have to free the structs - */ -static bool _mdns_delegate_hostname_add(const char *hostname, mdns_ip_addr_t *address_list) -{ - if (_hostname_is_ours(hostname)) { - return false; - } - - mdns_host_item_t *host = (mdns_host_item_t *)mdns_mem_malloc(sizeof(mdns_host_item_t)); - - if (host == NULL) { - return false; - } - host->address_list = address_list; - host->hostname = hostname; - host->next = _mdns_host_list; - _mdns_host_list = host; - return true; -} - -static void free_address_list(mdns_ip_addr_t *address_list) -{ - while (address_list != NULL) { - mdns_ip_addr_t *next = address_list->next; - mdns_mem_free(address_list); - address_list = next; - } -} - - -static bool _mdns_delegate_hostname_set_address(const char *hostname, mdns_ip_addr_t *address_list) -{ - if (!_str_null_or_empty(_mdns_server->hostname) && - strcasecmp(hostname, _mdns_server->hostname) == 0) { - return false; - } - mdns_host_item_t *host = _mdns_host_list; - while (host != NULL) { - if (strcasecmp(hostname, host->hostname) == 0) { - // free previous address list - free_address_list(host->address_list); - // set current address list to the host - host->address_list = address_list; - return true; - } - host = host->next; - } - return false; -} - -static mdns_ip_addr_t *copy_address_list(const mdns_ip_addr_t *address_list) -{ - mdns_ip_addr_t *head = NULL; - mdns_ip_addr_t *tail = NULL; - while (address_list != NULL) { - mdns_ip_addr_t *addr = (mdns_ip_addr_t *)mdns_mem_malloc(sizeof(mdns_ip_addr_t)); - if (addr == NULL) { - free_address_list(head); - return NULL; - } - addr->addr = address_list->addr; - addr->next = NULL; - if (head == NULL) { - head = addr; - tail = addr; - } else { - tail->next = addr; - tail = tail->next; - } - address_list = address_list->next; - } - return head; -} - -static void free_delegated_hostnames(void) -{ - mdns_host_item_t *host = _mdns_host_list; - while (host != NULL) { - free_address_list(host->address_list); - mdns_mem_free((char *)host->hostname); - mdns_host_item_t *item = host; - host = host->next; - mdns_mem_free(item); - } - _mdns_host_list = NULL; -} - -static bool _mdns_delegate_hostname_remove(const char *hostname) -{ - mdns_srv_item_t *srv = _mdns_server->services; - mdns_srv_item_t *prev_srv = NULL; - while (srv) { - if (strcasecmp(srv->service->hostname, hostname) == 0) { - mdns_srv_item_t *to_free = srv; - _mdns_send_bye(&srv, 1, false); - _mdns_remove_scheduled_service_packets(srv->service); - if (prev_srv == NULL) { - _mdns_server->services = srv->next; - srv = srv->next; - } else { - prev_srv->next = srv->next; - srv = srv->next; - } - _mdns_free_service(to_free->service); - mdns_mem_free(to_free); - } else { - prev_srv = srv; - srv = srv->next; - } - } - mdns_host_item_t *host = _mdns_host_list; - mdns_host_item_t *prev_host = NULL; - while (host != NULL) { - if (strcasecmp(hostname, host->hostname) == 0) { - if (prev_host == NULL) { - _mdns_host_list = host->next; - } else { - prev_host->next = host->next; - } - free_address_list(host->address_list); - mdns_mem_free((char *)host->hostname); - mdns_mem_free(host); - break; - } else { - prev_host = host; - host = host->next; - } - } - return true; -} - -/** - * @brief Check if parsed name is discovery - */ -static bool _mdns_name_is_discovery(mdns_name_t *name, uint16_t type) -{ - return ( - (name->host[0] && !strcasecmp(name->host, "_services")) - && (name->service[0] && !strcasecmp(name->service, "_dns-sd")) - && (name->proto[0] && !strcasecmp(name->proto, "_udp")) - && (name->domain[0] && !strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN)) - && type == MDNS_TYPE_PTR - ); -} - -/** - * @brief Check if the parsed name is self-hosted, i.e. we should resolve conflicts - */ -static bool _mdns_name_is_selfhosted(mdns_name_t *name) -{ - if (_str_null_or_empty(_mdns_server->hostname)) { // self-hostname needs to be defined - return false; - } - - // hostname only -- check if selfhosted name - if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto) && - strcasecmp(name->host, _mdns_server->hostname) == 0) { - return true; - } - - // service -- check if selfhosted service - mdns_srv_item_t *srv = _mdns_get_service_item(name->service, name->proto, NULL); - if (srv && strcasecmp(_mdns_server->hostname, srv->service->hostname) == 0) { - return true; - } - return false; -} - -/** - * @brief Check if the parsed name is ours (matches service or host name) - */ -static bool _mdns_name_is_ours(mdns_name_t *name) -{ - //domain have to be "local" - if (_str_null_or_empty(name->domain) || (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES - && strcasecmp(name->domain, "arpa") -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ - )) { - return false; - } - - //if service and proto are empty, host must match out hostname - if (_str_null_or_empty(name->service) && _str_null_or_empty(name->proto)) { - if (!_str_null_or_empty(name->host) - && !_str_null_or_empty(_mdns_server->hostname) - && _hostname_is_ours(name->host)) { - return true; - } - return false; - } - - //if service or proto is empty, name is invalid - if (_str_null_or_empty(name->service) || _str_null_or_empty(name->proto)) { - return false; - } - - - //find the service - mdns_srv_item_t *service; - if (name->sub) { - service = _mdns_get_service_item_subtype(name->host, name->service, name->proto); - } else if (_str_null_or_empty(name->host)) { - service = _mdns_get_service_item(name->service, name->proto, NULL); - } else { - service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL); - } - if (!service) { - return false; - } - - //if query is PTR query and we have service, we have success - if (name->sub || _str_null_or_empty(name->host)) { - return true; - } - - //OK we have host in the name. find what is the instance of the service - const char *instance = _mdns_get_service_instance_name(service->service); - if (instance == NULL) { - return false; - } - - //compare the instance against the name - if (strcasecmp(name->host, instance) == 0) { - return true; - } - - return false; -} - -/** - * @brief read uint16_t from a packet - * @param packet the packet - * @param index index in the packet where the value starts - * - * @return the value - */ -static inline uint16_t _mdns_read_u16(const uint8_t *packet, uint16_t index) -{ - return (uint16_t)(packet[index]) << 8 | packet[index + 1]; -} - -/** - * @brief read uint32_t from a packet - * @param packet the packet - * @param index index in the packet where the value starts - * - * @return the value - */ -static inline uint32_t _mdns_read_u32(const uint8_t *packet, uint16_t index) -{ - return (uint32_t)(packet[index]) << 24 | (uint32_t)(packet[index + 1]) << 16 | (uint32_t)(packet[index + 2]) << 8 | packet[index + 3]; -} - -/** - * @brief reads and formats MDNS FQDN into mdns_name_t structure - * - * @param packet MDNS packet - * @param start Starting point of FQDN - * @param name mdns_name_t structure to populate - * - * @return the address after the parsed FQDN in the packet or NULL on error - */ -static const uint8_t *_mdns_parse_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, size_t packet_len) -{ - name->parts = 0; - name->sub = 0; - name->host[0] = 0; - name->service[0] = 0; - name->proto[0] = 0; - name->domain[0] = 0; - name->invalid = false; - - static char buf[MDNS_NAME_BUF_LEN]; - - const uint8_t *next_data = (uint8_t *)_mdns_read_fqdn(packet, start, name, buf, packet_len); - if (!next_data) { - return 0; - } - if (!name->parts || name->invalid) { - return next_data; - } - if (name->parts == 3) { - memmove((uint8_t *)name + (MDNS_NAME_BUF_LEN), (uint8_t *)name, 3 * (MDNS_NAME_BUF_LEN)); - name->host[0] = 0; - } else if (name->parts == 2) { - memmove((uint8_t *)(name->domain), (uint8_t *)(name->service), (MDNS_NAME_BUF_LEN)); - name->service[0] = 0; - name->proto[0] = 0; - } - if (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) == 0 || strcasecmp(name->domain, "arpa") == 0) { - return next_data; - } - name->invalid = true; // mark the current name invalid, but continue with other question - return next_data; -} - -/** - * @brief Called from parser to check if question matches particular service - */ -static bool _mdns_question_matches(mdns_parsed_question_t *question, uint16_t type, mdns_srv_item_t *service) -{ - if (question->type != type) { - return false; - } - if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { - return true; - } else if (type == MDNS_TYPE_PTR || type == MDNS_TYPE_SDPTR) { - if (question->service && question->proto && question->domain - && !strcasecmp(service->service->service, question->service) - && !strcasecmp(service->service->proto, question->proto) - && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) { - if (!service->service->instance) { - return true; - } else if (service->service->instance && question->host && !strcasecmp(service->service->instance, question->host)) { - return true; - } - } - } else if (service && (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT)) { - const char *name = _mdns_get_service_instance_name(service->service); - if (name && question->host && question->service && question->proto && question->domain - && !strcasecmp(name, question->host) - && !strcasecmp(service->service->service, question->service) - && !strcasecmp(service->service->proto, question->proto) - && !strcasecmp(MDNS_DEFAULT_DOMAIN, question->domain)) { - return true; - } - } - - return false; -} - -/** - * @brief Removes saved question from parsed data - */ -static void _mdns_remove_parsed_question(mdns_parsed_packet_t *parsed_packet, uint16_t type, mdns_srv_item_t *service) -{ - mdns_parsed_question_t *q = parsed_packet->questions; - - if (_mdns_question_matches(q, type, service)) { - parsed_packet->questions = q->next; - mdns_mem_free(q->host); - mdns_mem_free(q->service); - mdns_mem_free(q->proto); - mdns_mem_free(q->domain); - mdns_mem_free(q); - return; - } - - while (q->next) { - mdns_parsed_question_t *p = q->next; - if (_mdns_question_matches(p, type, service)) { - q->next = p->next; - mdns_mem_free(p->host); - mdns_mem_free(p->service); - mdns_mem_free(p->proto); - mdns_mem_free(p->domain); - mdns_mem_free(p); - return; - } - q = q->next; - } -} - -/** - * @brief Get number of items in TXT parsed data - */ -static int _mdns_txt_items_count_get(const uint8_t *data, size_t len) -{ - if (len == 1) { - return 0; - } - - int num_items = 0; - uint16_t i = 0; - size_t partLen = 0; - - while (i < len) { - partLen = data[i++]; - if (!partLen) { - break; - } - if ((i + partLen) > len) { - return -1;//error - } - i += partLen; - num_items++; - } - return num_items; -} - -/** - * @brief Get the length of TXT item's key name - */ -static int _mdns_txt_item_name_get_len(const uint8_t *data, size_t len) -{ - if (*data == '=') { - return -1; - } - for (size_t i = 0; i < len; i++) { - if (data[i] == '=') { - return i; - } - } - return len; -} - -/** - * @brief Create TXT result array from parsed TXT data - */ -static void _mdns_result_txt_create(const uint8_t *data, size_t len, mdns_txt_item_t **out_txt, uint8_t **out_value_len, - size_t *out_count) -{ - *out_txt = NULL; - *out_count = 0; - uint16_t i = 0, y; - size_t partLen = 0; - int num_items = _mdns_txt_items_count_get(data, len); - if (num_items < 0 || num_items > SIZE_MAX / sizeof(mdns_txt_item_t)) { - // Error: num_items is incorrect (or too large to allocate) - return; - } - - if (!num_items) { - return; - } - - mdns_txt_item_t *txt = (mdns_txt_item_t *)mdns_mem_malloc(sizeof(mdns_txt_item_t) * num_items); - if (!txt) { - HOOK_MALLOC_FAILED; - return; - } - uint8_t *txt_value_len = (uint8_t *)mdns_mem_malloc(num_items); - if (!txt_value_len) { - mdns_mem_free(txt); - HOOK_MALLOC_FAILED; - return; - } - memset(txt, 0, sizeof(mdns_txt_item_t) * num_items); - memset(txt_value_len, 0, num_items); - size_t txt_num = 0; - - while (i < len) { - partLen = data[i++]; - if (!partLen) { - break; - } - - if ((i + partLen) > len) { - goto handle_error;//error - } - - int name_len = _mdns_txt_item_name_get_len(data + i, partLen); - if (name_len < 0 || txt_num >= num_items) {//invalid item (no name or more items than expected) - i += partLen; - continue; - } - char *key = (char *)mdns_mem_malloc(name_len + 1); - if (!key) { - HOOK_MALLOC_FAILED; - goto handle_error;//error - } - mdns_txt_item_t *t = &txt[txt_num]; - uint8_t *value_len = &txt_value_len[txt_num]; - txt_num++; - - memcpy(key, data + i, name_len); - key[name_len] = 0; - i += name_len + 1; - t->key = key; - - int new_value_len = partLen - name_len - 1; - if (new_value_len > 0) { - char *value = (char *)mdns_mem_malloc(new_value_len + 1); - if (!value) { - HOOK_MALLOC_FAILED; - goto handle_error;//error - } - memcpy(value, data + i, new_value_len); - value[new_value_len] = 0; - *value_len = new_value_len; - i += new_value_len; - t->value = value; - } else { - t->value = NULL; - } - } - - *out_txt = txt; - *out_count = txt_num; - *out_value_len = txt_value_len; - return; - -handle_error : - for (y = 0; y < txt_num; y++) { - mdns_txt_item_t *t = &txt[y]; - mdns_mem_free((char *)t->key); - mdns_mem_free((char *)t->value); - } - mdns_mem_free(txt_value_len); - mdns_mem_free(txt); -} - -/** - * @brief Duplicate string or return error - */ -static esp_err_t _mdns_strdup_check(char **out, char *in) -{ - if (in && in[0]) { - *out = mdns_mem_strdup(in); - if (!*out) { - return ESP_FAIL; - } - return ESP_OK; - } - *out = NULL; - return ESP_OK; -} - -/** - * @brief main packet parser - * - * @param packet the packet - */ -void mdns_parse_packet(mdns_rx_packet_t *packet) -{ - static mdns_name_t n; - mdns_header_t header; - const uint8_t *data = _mdns_get_packet_data(packet); - size_t len = _mdns_get_packet_len(packet); - const uint8_t *content = data + MDNS_HEAD_LEN; - bool do_not_reply = false; - mdns_search_once_t *search_result = NULL; - mdns_browse_t *browse_result = NULL; - char *browse_result_instance = NULL; - char *browse_result_service = NULL; - char *browse_result_proto = NULL; - mdns_browse_sync_t *out_sync_browse = NULL; - -#ifdef MDNS_ENABLE_DEBUG - _mdns_dbg_printf("\nRX[%lu][%lu]: ", (unsigned long)packet->tcpip_if, (unsigned long)packet->ip_protocol); -#ifdef CONFIG_LWIP_IPV4 - if (packet->src.type == ESP_IPADDR_TYPE_V4) { - _mdns_dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4)); - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (packet->src.type == ESP_IPADDR_TYPE_V6) { - _mdns_dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6)); - } -#endif - mdns_debug_packet(data, len); -#endif - -#ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES - // Check if the packet wasn't sent by us -#ifdef CONFIG_LWIP_IPV4 - if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) { - esp_netif_ip_info_t if_ip_info; - if (esp_netif_get_ip_info(_mdns_get_esp_netif(packet->tcpip_if), &if_ip_info) == ESP_OK && - memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) { - return; - } - } -#endif /* CONFIG_LWIP_IPV4 */ -#ifdef CONFIG_LWIP_IPV6 - if (packet->ip_protocol == MDNS_IP_PROTOCOL_V6) { - struct esp_ip6_addr if_ip6; - if (esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK && - memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) { - return; - } - } -#endif /* CONFIG_LWIP_IPV6 */ -#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES - - // Check for the minimum size of mdns packet - if (len <= MDNS_HEAD_ADDITIONAL_OFFSET) { - return; - } - - mdns_parsed_packet_t *parsed_packet = (mdns_parsed_packet_t *)mdns_mem_malloc(sizeof(mdns_parsed_packet_t)); - if (!parsed_packet) { - HOOK_MALLOC_FAILED; - return; - } - memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t)); - - mdns_name_t *name = &n; - memset(name, 0, sizeof(mdns_name_t)); - - header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET); - header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET); - header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET); - header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET); - header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET); - header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET); - - if (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) { - mdns_mem_free(parsed_packet); - return; - } - - //if we have not set the hostname, we can not answer questions - if (header.questions && !header.answers && _str_null_or_empty(_mdns_server->hostname)) { - mdns_mem_free(parsed_packet); - return; - } - - parsed_packet->tcpip_if = packet->tcpip_if; - parsed_packet->ip_protocol = packet->ip_protocol; - parsed_packet->multicast = packet->multicast; - parsed_packet->authoritative = (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE); - parsed_packet->distributed = header.flags == MDNS_FLAGS_DISTRIBUTED; - parsed_packet->id = header.id; - esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src); - parsed_packet->src_port = packet->src_port; - parsed_packet->records = NULL; - - if (header.questions) { - uint8_t qs = header.questions; - - while (qs--) { - content = _mdns_parse_fqdn(data, content, name, len); - if (!content) { - header.answers = 0; - header.additional = 0; - header.servers = 0; - goto clear_rx_packet;//error - } - - if (content + MDNS_CLASS_OFFSET + 1 >= data + len) { - goto clear_rx_packet; // malformed packet, won't read behind it - } - uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET); - uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET); - bool unicast = !!(mdns_class & 0x8000); - mdns_class &= 0x7FFF; - content = content + 4; - - if (mdns_class != 0x0001 || name->invalid) {//bad class or invalid name for this question entry - continue; - } - - if (_mdns_name_is_discovery(name, type)) { - //service discovery - parsed_packet->discovery = true; - mdns_srv_item_t *a = _mdns_server->services; - while (a) { - mdns_parsed_question_t *question = (mdns_parsed_question_t *)mdns_mem_calloc(1, sizeof(mdns_parsed_question_t)); - if (!question) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - question->next = parsed_packet->questions; - parsed_packet->questions = question; - - question->unicast = unicast; - question->type = MDNS_TYPE_SDPTR; - question->host = NULL; - question->service = mdns_mem_strdup(a->service->service); - question->proto = mdns_mem_strdup(a->service->proto); - question->domain = mdns_mem_strdup(MDNS_DEFAULT_DOMAIN); - if (!question->service || !question->proto || !question->domain) { - goto clear_rx_packet; - } - a = a->next; - } - continue; - } - if (!_mdns_name_is_ours(name)) { - continue; - } - - if (type == MDNS_TYPE_ANY && !_str_null_or_empty(name->host)) { - parsed_packet->probe = true; - } - - mdns_parsed_question_t *question = (mdns_parsed_question_t *)mdns_mem_calloc(1, sizeof(mdns_parsed_question_t)); - if (!question) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - question->next = parsed_packet->questions; - parsed_packet->questions = question; - - question->unicast = unicast; - question->type = type; - question->sub = name->sub; - if (_mdns_strdup_check(&(question->host), name->host) - || _mdns_strdup_check(&(question->service), name->service) - || _mdns_strdup_check(&(question->proto), name->proto) - || _mdns_strdup_check(&(question->domain), name->domain)) { - goto clear_rx_packet; - } - } - } - - if (header.questions && !parsed_packet->questions && !parsed_packet->discovery && !header.answers) { - goto clear_rx_packet; - } else if (header.answers || header.servers || header.additional) { - uint16_t recordIndex = 0; - - while (content < (data + len)) { - - content = _mdns_parse_fqdn(data, content, name, len); - if (!content) { - goto clear_rx_packet;//error - } - - if (content + MDNS_LEN_OFFSET + 1 >= data + len) { - goto clear_rx_packet; // malformed packet, won't read behind it - } - uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET); - uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET); - uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET); - uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET); - const uint8_t *data_ptr = content + MDNS_DATA_OFFSET; - mdns_class &= 0x7FFF; - - content = data_ptr + data_len; - if (content > (data + len) || data_len == 0) { - goto clear_rx_packet; - } - - bool discovery = false; - bool ours = false; - mdns_srv_item_t *service = NULL; - mdns_parsed_record_type_t record_type = MDNS_ANSWER; - - if (recordIndex >= (header.answers + header.servers)) { - record_type = MDNS_EXTRA; - } else if (recordIndex >= (header.answers)) { - record_type = MDNS_NS; - } - recordIndex++; - - if (type == MDNS_TYPE_NSEC || type == MDNS_TYPE_OPT) { - //skip NSEC and OPT - continue; - } - - if (parsed_packet->discovery && _mdns_name_is_discovery(name, type)) { - discovery = true; - } else if (!name->sub && _mdns_name_is_ours(name)) { - ours = true; - if (name->service[0] && name->proto[0]) { - service = _mdns_get_service_item(name->service, name->proto, NULL); - } - } else { - if ((header.flags & MDNS_FLAGS_QUERY_REPSONSE) == 0 || record_type == MDNS_NS) { - //skip this record - continue; - } - search_result = _mdns_search_find_from(_mdns_server->search_once, name, type, packet->tcpip_if, packet->ip_protocol); - browse_result = _mdns_browse_find_from(_mdns_server->browse, name, type, packet->tcpip_if, packet->ip_protocol); - if (browse_result) { - if (!out_sync_browse) { - // will be freed in function `_mdns_browse_sync` - out_sync_browse = (mdns_browse_sync_t *)mdns_mem_malloc(sizeof(mdns_browse_sync_t)); - if (!out_sync_browse) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - out_sync_browse->browse = browse_result; - out_sync_browse->sync_result = NULL; - } - if (!browse_result_service) { - browse_result_service = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!browse_result_service) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - } - memcpy(browse_result_service, browse_result->service, MDNS_NAME_BUF_LEN); - if (!browse_result_proto) { - browse_result_proto = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!browse_result_proto) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - } - memcpy(browse_result_proto, browse_result->proto, MDNS_NAME_BUF_LEN); - if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { - if (!browse_result_instance) { - browse_result_instance = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!browse_result_instance) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - } - memcpy(browse_result_instance, name->host, MDNS_NAME_BUF_LEN); - } - } - } - - if (type == MDNS_TYPE_PTR) { - if (!_mdns_parse_fqdn(data, data_ptr, name, len)) { - continue;//error - } - if (search_result) { - _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto, - packet->tcpip_if, packet->ip_protocol, ttl); - } else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) { - if (name->host[0]) { - service = _mdns_get_service_item_instance(name->host, name->service, name->proto, NULL); - } else { - service = _mdns_get_service_item(name->service, name->proto, NULL); - } - if (discovery && service) { - _mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service); - } else if (service && parsed_packet->questions && !parsed_packet->probe) { - _mdns_remove_parsed_question(parsed_packet, type, service); - } else if (service) { - //check if TTL is more than half of the full TTL value (4500) - if (ttl > (MDNS_ANSWER_PTR_TTL / 2)) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); - } - } - if (service) { - mdns_parsed_record_t *record = mdns_mem_malloc(sizeof(mdns_parsed_record_t)); - if (!record) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - record->next = parsed_packet->records; - parsed_packet->records = record; - record->type = MDNS_TYPE_PTR; - record->record_type = MDNS_ANSWER; - record->ttl = ttl; - record->host = NULL; - record->service = NULL; - record->proto = NULL; - if (name->host[0]) { - record->host = mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!record->host) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - memcpy(record->host, name->host, MDNS_NAME_BUF_LEN); - } - if (name->service[0]) { - record->service = mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!record->service) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - memcpy(record->service, name->service, MDNS_NAME_BUF_LEN); - } - if (name->proto[0]) { - record->proto = mdns_mem_malloc(MDNS_NAME_BUF_LEN); - if (!record->proto) { - HOOK_MALLOC_FAILED; - goto clear_rx_packet; - } - memcpy(record->proto, name->proto, MDNS_NAME_BUF_LEN); - } - } - } - } else if (type == MDNS_TYPE_SRV) { - mdns_result_t *result = NULL; - if (search_result && search_result->type == MDNS_TYPE_PTR) { - result = search_result->result; - while (result) { - if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif - && packet->ip_protocol == result->ip_protocol - && result->instance_name && !strcmp(name->host, result->instance_name)) { - break; - } - result = result->next; - } - if (!result) { - result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto, - packet->tcpip_if, packet->ip_protocol, ttl); - if (!result) { - continue;//error - } - } - } - bool is_selfhosted = _mdns_name_is_selfhosted(name); - if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) { - continue;//error - } - if (data_ptr + MDNS_SRV_PORT_OFFSET + 1 >= data + len) { - goto clear_rx_packet; // malformed packet, won't read behind it - } - uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET); - uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET); - uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET); - - if (browse_result) { - _mdns_browse_result_add_srv(browse_result, name->host, browse_result_instance, browse_result_service, - browse_result_proto, port, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse); - } - if (search_result) { - if (search_result->type == MDNS_TYPE_PTR) { - if (!result->hostname) { // assign host/port for this entry only if not previously set - result->port = port; - result->hostname = mdns_mem_strdup(name->host); - } - } else { - _mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol, ttl); - } - } else if (ours) { - if (parsed_packet->questions && !parsed_packet->probe) { - _mdns_remove_parsed_question(parsed_packet, type, service); - continue; - } else if (parsed_packet->distributed) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); - continue; - } - if (!is_selfhosted) { - continue; - } - //detect collision (-1=won, 0=none, 1=lost) - int col = 0; - if (mdns_class > 1) { - col = 1; - } else if (!mdns_class) { - col = -1; - } else if (service) { // only detect srv collision if service existed - col = _mdns_check_srv_collision(service->service, priority, weight, port, name->host, name->domain); - } - if (service && col && (parsed_packet->probe || parsed_packet->authoritative)) { - if (col > 0 || !port) { - do_not_reply = true; - if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++; - if (!_str_null_or_empty(service->service->instance)) { - char *new_instance = _mdns_mangle_name((char *)service->service->instance); - if (new_instance) { - mdns_mem_free((char *)service->service->instance); - service->service->instance = new_instance; - } - _mdns_probe_all_pcbs(&service, 1, false, false); - } else if (!_str_null_or_empty(_mdns_server->instance)) { - char *new_instance = _mdns_mangle_name((char *)_mdns_server->instance); - if (new_instance) { - mdns_mem_free((char *)_mdns_server->instance); - _mdns_server->instance = new_instance; - } - _mdns_restart_all_pcbs_no_instance(); - } else { - char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname); - if (new_host) { - _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host); - mdns_mem_free((char *)_mdns_server->hostname); - _mdns_server->hostname = new_host; - _mdns_self_host.hostname = new_host; - } - _mdns_restart_all_pcbs(); - } - } else if (service) { - _mdns_pcb_send_bye(packet->tcpip_if, packet->ip_protocol, &service, 1, false); - _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, false); - } - } - } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); - } - } - } else if (type == MDNS_TYPE_TXT) { - mdns_txt_item_t *txt = NULL; - uint8_t *txt_value_len = NULL; - size_t txt_count = 0; - - mdns_result_t *result = NULL; - if (browse_result) { - _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); - _mdns_browse_result_add_txt(browse_result, browse_result_instance, browse_result_service, browse_result_proto, - txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse); - } - if (search_result) { - if (search_result->type == MDNS_TYPE_PTR) { - result = search_result->result; - while (result) { - if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif - && packet->ip_protocol == result->ip_protocol - && result->instance_name && !strcmp(name->host, result->instance_name)) { - break; - } - result = result->next; - } - if (!result) { - result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto, - packet->tcpip_if, packet->ip_protocol, ttl); - if (!result) { - continue;//error - } - } - if (!result->txt) { - _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); - if (txt_count) { - result->txt = txt; - result->txt_count = txt_count; - result->txt_value_len = txt_value_len; - } - } - } else { - _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); - if (txt_count) { - _mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl); - } - } - } else if (ours) { - if (parsed_packet->questions && !parsed_packet->probe && service) { - _mdns_remove_parsed_question(parsed_packet, type, service); - continue; - } - if (!_mdns_name_is_selfhosted(name)) { - continue; - } - //detect collision (-1=won, 0=none, 1=lost) - int col = 0; - if (mdns_class > 1) { - col = 1; - } else if (!mdns_class) { - col = -1; - } else if (service) { // only detect txt collision if service existed - col = _mdns_check_txt_collision(service->service, data_ptr, data_len); - } - if (col && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running && service) { - do_not_reply = true; - _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, true); - } else if (ttl > (MDNS_ANSWER_TXT_TTL / 2) && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); - } - } - - } -#ifdef CONFIG_LWIP_IPV6 - else if (type == MDNS_TYPE_AAAA) {//ipv6 - esp_ip_addr_t ip6; - ip6.type = ESP_IPADDR_TYPE_V6; - memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE); - if (browse_result) { - _mdns_browse_result_add_ip(browse_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse); - } - if (search_result) { - //check for more applicable searches (PTR & A/AAAA at the same time) - while (search_result) { - _mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl); - search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol); - } - } else if (ours) { - if (parsed_packet->questions && !parsed_packet->probe) { - _mdns_remove_parsed_question(parsed_packet, type, NULL); - continue; - } - if (!_mdns_name_is_selfhosted(name)) { - continue; - } - //detect collision (-1=won, 0=none, 1=lost) - int col = 0; - if (mdns_class > 1) { - col = 1; - } else if (!mdns_class) { - col = -1; - } else { - col = _mdns_check_aaaa_collision(&(ip6.u_addr.ip6), packet->tcpip_if); - } - if (col == 2) { - goto clear_rx_packet; - } else if (col == 1) { - do_not_reply = true; - if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - if (col && (parsed_packet->probe || parsed_packet->authoritative)) { - _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++; - char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname); - if (new_host) { - _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host); - mdns_mem_free((char *)_mdns_server->hostname); - _mdns_server->hostname = new_host; - _mdns_self_host.hostname = new_host; - } - _mdns_restart_all_pcbs(); - } - } else { - _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true); - } - } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL); - } - } - - } -#endif /* CONFIG_LWIP_IPV6 */ -#ifdef CONFIG_LWIP_IPV4 - else if (type == MDNS_TYPE_A) { - esp_ip_addr_t ip; - ip.type = ESP_IPADDR_TYPE_V4; - memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4); - if (browse_result) { - _mdns_browse_result_add_ip(browse_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl, out_sync_browse); - } - if (search_result) { - //check for more applicable searches (PTR & A/AAAA at the same time) - while (search_result) { - _mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl); - search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol); - } - } else if (ours) { - if (parsed_packet->questions && !parsed_packet->probe) { - _mdns_remove_parsed_question(parsed_packet, type, NULL); - continue; - } - if (!_mdns_name_is_selfhosted(name)) { - continue; - } - //detect collision (-1=won, 0=none, 1=lost) - int col = 0; - if (mdns_class > 1) { - col = 1; - } else if (!mdns_class) { - col = -1; - } else { - col = _mdns_check_a_collision(&(ip.u_addr.ip4), packet->tcpip_if); - } - if (col == 2) { - goto clear_rx_packet; - } else if (col == 1) { - do_not_reply = true; - if (_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - if (col && (parsed_packet->probe || parsed_packet->authoritative)) { - _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].failed_probes++; - char *new_host = _mdns_mangle_name((char *)_mdns_server->hostname); - if (new_host) { - _mdns_remap_self_service_hostname(_mdns_server->hostname, new_host); - mdns_mem_free((char *)_mdns_server->hostname); - _mdns_server->hostname = new_host; - _mdns_self_host.hostname = new_host; - } - _mdns_restart_all_pcbs(); - } - } else { - _mdns_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true); - } - } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running) { - _mdns_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL); - } - } - - } -#endif /* CONFIG_LWIP_IPV4 */ - } - //end while - if (parsed_packet->authoritative) { - _mdns_search_finish_done(); - } - } - - if (!do_not_reply && _mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].state > PCB_PROBE_3 && (parsed_packet->questions || parsed_packet->discovery)) { - _mdns_create_answer_from_parsed_packet(parsed_packet); - } - if (out_sync_browse) { -#ifdef MDNS_ENABLE_DEBUG - _mdns_dbg_printf("Browse %s%s total result:", out_sync_browse->browse->service, out_sync_browse->browse->proto); - debug_printf_browse_result_all(out_sync_browse->browse->result); -#endif // MDNS_ENABLE_DEBUG - if (out_sync_browse->sync_result) { -#ifdef MDNS_ENABLE_DEBUG - _mdns_dbg_printf("Changed result:"); - debug_printf_browse_result_all(out_sync_browse->sync_result->result); -#endif // MDNS_ENABLE_DEBUG - _mdns_sync_browse_action(ACTION_BROWSE_SYNC, out_sync_browse); - } else { - mdns_mem_free(out_sync_browse); - } - out_sync_browse = NULL; - } - -clear_rx_packet: - while (parsed_packet->questions) { - mdns_parsed_question_t *question = parsed_packet->questions; - parsed_packet->questions = parsed_packet->questions->next; - if (question->host) { - mdns_mem_free(question->host); - } - if (question->service) { - mdns_mem_free(question->service); - } - if (question->proto) { - mdns_mem_free(question->proto); - } - if (question->domain) { - mdns_mem_free(question->domain); - } - mdns_mem_free(question); - } - while (parsed_packet->records) { - mdns_parsed_record_t *record = parsed_packet->records; - parsed_packet->records = parsed_packet->records->next; - if (record->host) { - mdns_mem_free(record->host); - } - if (record->service) { - mdns_mem_free(record->service); - } - if (record->proto) { - mdns_mem_free(record->proto); - } - record->next = NULL; - mdns_mem_free(record); - } - mdns_mem_free(parsed_packet); - mdns_mem_free(browse_result_instance); - mdns_mem_free(browse_result_service); - mdns_mem_free(browse_result_proto); - mdns_mem_free(out_sync_browse); -} - -/** - * @brief Enable mDNS interface - */ -void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - if (!mdns_is_netif_ready(tcpip_if, ip_protocol)) { - if (_mdns_pcb_init(tcpip_if, ip_protocol)) { - _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].failed_probes = 0; - return; - } - } - _mdns_restart_pcb(tcpip_if, ip_protocol); -} - -/** - * @brief Disable mDNS interface - */ -void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - _mdns_clean_netif_ptr(tcpip_if); - - if (mdns_is_netif_ready(tcpip_if, ip_protocol)) { - _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol); - mdns_pcb_deinit_local(tcpip_if, ip_protocol); - mdns_if_t other_if = _mdns_get_other_if(tcpip_if); - if (other_if != MDNS_MAX_INTERFACES && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) { - _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF; - _mdns_enable_pcb(other_if, ip_protocol); - } - } - _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state = PCB_OFF; -} - -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES -static inline char nibble_to_hex(int var) -{ - return var > 9 ? var - 10 + 'a' : var + '0'; -} -#endif - -/** - * @brief Performs interface changes based on system events or custom commands - */ -static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) -{ - if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) { - return; - } - if (action & MDNS_EVENT_ENABLE_IP4) { - _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V4); - } - if (action & MDNS_EVENT_ENABLE_IP6) { - _mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6); - } - if (action & MDNS_EVENT_DISABLE_IP4) { - _mdns_disable_pcb(mdns_if, MDNS_IP_PROTOCOL_V4); - } - if (action & MDNS_EVENT_DISABLE_IP6) { - _mdns_disable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6); - } - if (action & MDNS_EVENT_ANNOUNCE_IP4) { - _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true); - } - if (action & MDNS_EVENT_ANNOUNCE_IP6) { - _mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V6, NULL, 0, true); - } - -#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES -#ifdef CONFIG_LWIP_IPV4 - if (action & MDNS_EVENT_IP4_REVERSE_LOOKUP) { - esp_netif_ip_info_t if_ip_info; - if (esp_netif_get_ip_info(_mdns_get_esp_netif(mdns_if), &if_ip_info) == ESP_OK) { - esp_ip4_addr_t *ip = &if_ip_info.ip; - char *reverse_query_name = NULL; - if (asprintf(&reverse_query_name, "%d.%d.%d.%d.in-addr", - esp_ip4_addr4_16(ip), esp_ip4_addr3_16(ip), - esp_ip4_addr2_16(ip), esp_ip4_addr1_16(ip)) > 0 && reverse_query_name) { - ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name); - _mdns_delegate_hostname_add(reverse_query_name, NULL); - } - } - } -#endif /* CONFIG_LWIP_IPV4 */ -#ifdef CONFIG_LWIP_IPV6 - if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) { - esp_ip6_addr_t addr6; - if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(addr6)) { - uint8_t *paddr = (uint8_t *)&addr6.addr; - const char sub[] = "ip6"; - const size_t query_name_size = 4 * sizeof(addr6.addr) /* (2 nibbles + 2 dots)/per byte of IP address */ + sizeof(sub); - char *reverse_query_name = mdns_mem_malloc(query_name_size); - if (reverse_query_name) { - char *ptr = &reverse_query_name[query_name_size]; // point to the end - memcpy(ptr - sizeof(sub), sub, sizeof(sub)); // copy the IP sub-domain - ptr -= sizeof(sub) + 1; // move before the sub-domain - while (reverse_query_name < ptr) { // continue populating reverse query from the end - *ptr-- = '.'; // nibble by nibble, until we reach the beginning - *ptr-- = nibble_to_hex(((*paddr) >> 4) & 0x0F); - *ptr-- = '.'; - *ptr-- = nibble_to_hex((*paddr) & 0x0F); - paddr++; - } - ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name); - _mdns_delegate_hostname_add(reverse_query_name, NULL); - } - } - } -#endif /* CONFIG_LWIP_IPV6 */ -#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ -} - -/** - * @brief Dispatch interface changes based on system events - */ -static inline void post_mdns_disable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) -{ - mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_DISABLE_IP4 : MDNS_EVENT_DISABLE_IP6); -} - -static inline void post_mdns_enable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) -{ - mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ENABLE_IP4 : MDNS_EVENT_ENABLE_IP6); -} - -static inline void post_mdns_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) -{ - mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6); -} - -#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH -void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - if (!_mdns_server) { - return; - } - -#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) - if (event_base == WIFI_EVENT) { - esp_netif_dhcp_status_t dcst; - switch (event_id) { - case WIFI_EVENT_STA_CONNECTED: - if (!esp_netif_dhcpc_get_status(esp_netif_from_preset_if(MDNS_IF_STA), &dcst)) { - if (dcst == ESP_NETIF_DHCP_STOPPED) { - post_mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); - } - } - break; - case WIFI_EVENT_STA_DISCONNECTED: - post_mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); - post_mdns_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6); - break; - case WIFI_EVENT_AP_START: - post_mdns_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); - break; - case WIFI_EVENT_AP_STOP: - post_mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); - post_mdns_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6); - break; - default: - break; - } - } else -#endif -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH - if (event_base == ETH_EVENT) { - esp_netif_dhcp_status_t dcst; - switch (event_id) { - case ETHERNET_EVENT_CONNECTED: - if (!esp_netif_dhcpc_get_status(esp_netif_from_preset_if(MDNS_IF_ETH), &dcst)) { - if (dcst == ESP_NETIF_DHCP_STOPPED) { - post_mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); - } - } - break; - case ETHERNET_EVENT_DISCONNECTED: - post_mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); - post_mdns_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6); - break; - default: - break; - } - } else -#endif - if (event_base == IP_EVENT) { - switch (event_id) { - case IP_EVENT_STA_GOT_IP: - post_mdns_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); - post_mdns_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6); - break; -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH - case IP_EVENT_ETH_GOT_IP: - post_mdns_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); - break; -#endif - case IP_EVENT_GOT_IP6: { - ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data; - mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(event->esp_netif); - if (mdns_if >= MDNS_MAX_INTERFACES) { - return; - } - post_mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6); - post_mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4); - mdns_browse_t *browse = _mdns_server->browse; - while (browse) { - _mdns_browse_send(browse, mdns_if); - browse = browse->next; - } - } - break; - default: - break; - } - } -} -#endif /* CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH */ - -/* - * MDNS Search - * */ - -/** - * @brief Free search structure (except the results) - */ -static void _mdns_search_free(mdns_search_once_t *search) -{ - mdns_mem_free(search->instance); - mdns_mem_free(search->service); - mdns_mem_free(search->proto); - vSemaphoreDelete(search->done_semaphore); - mdns_mem_free(search); -} - -/** - * @brief Allocate new search structure - */ -static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, - uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier) -{ - mdns_search_once_t *search = (mdns_search_once_t *)mdns_mem_malloc(sizeof(mdns_search_once_t)); - if (!search) { - HOOK_MALLOC_FAILED; - return NULL; - } - memset(search, 0, sizeof(mdns_search_once_t)); - - search->done_semaphore = xSemaphoreCreateBinary(); - if (!search->done_semaphore) { - mdns_mem_free(search); - return NULL; - } - - if (!_str_null_or_empty(name)) { - search->instance = mdns_mem_strndup(name, MDNS_NAME_BUF_LEN - 1); - if (!search->instance) { - _mdns_search_free(search); - return NULL; - } - } - - if (!_str_null_or_empty(service)) { - search->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); - if (!search->service) { - _mdns_search_free(search); - return NULL; - } - } - - if (!_str_null_or_empty(proto)) { - search->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); - if (!search->proto) { - _mdns_search_free(search); - return NULL; - } - } - - search->type = type; - search->unicast = unicast; - search->timeout = timeout; - search->num_results = 0; - search->max_results = max_results; - search->result = NULL; - search->state = SEARCH_INIT; - search->sent_at = 0; - search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS; - search->notifier = notifier; - search->next = NULL; - - return search; -} - -/** - * @brief Mark search as finished and remove it from search chain - */ -static void _mdns_search_finish(mdns_search_once_t *search) -{ - search->state = SEARCH_OFF; - queueDetach(mdns_search_once_t, _mdns_server->search_once, search); - if (search->notifier) { - search->notifier(search); - } - xSemaphoreGive(search->done_semaphore); -} - -/** - * @brief Add new search to the search chain - */ -static void _mdns_search_add(mdns_search_once_t *search) -{ - search->next = _mdns_server->search_once; - _mdns_server->search_once = search; -} - -/** - * @brief Called from parser to finish any searches that have reached maximum results - */ -static void _mdns_search_finish_done(void) -{ - mdns_search_once_t *search = _mdns_server->search_once; - mdns_search_once_t *s = NULL; - while (search) { - s = search; - search = search->next; - if (s->max_results && s->num_results >= s->max_results) { - _mdns_search_finish(s); - } - } -} - -/** - * @brief Create linked IP (copy) from parsed one - */ -static mdns_ip_addr_t *_mdns_result_addr_create_ip(esp_ip_addr_t *ip) -{ - mdns_ip_addr_t *a = (mdns_ip_addr_t *)mdns_mem_malloc(sizeof(mdns_ip_addr_t)); - if (!a) { - HOOK_MALLOC_FAILED; - return NULL; - } - memset(a, 0, sizeof(mdns_ip_addr_t)); - a->addr.type = ip->type; - if (ip->type == ESP_IPADDR_TYPE_V6) { - memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16); - } else { - a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr; - } - return a; -} - -static inline void _mdns_result_update_ttl(mdns_result_t *r, uint32_t ttl) -{ - r->ttl = r->ttl < ttl ? r->ttl : ttl; -} - -/** - * @brief Chain new IP to search result - */ -static void _mdns_result_add_ip(mdns_result_t *r, esp_ip_addr_t *ip) -{ - mdns_ip_addr_t *a = r->addr; - while (a) { - if (a->addr.type == ip->type) { -#ifdef CONFIG_LWIP_IPV4 - if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) { - return; - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) { - return; - } -#endif - } - a = a->next; - } - a = _mdns_result_addr_create_ip(ip); - if (!a) { - return; - } - a->next = r->addr; - r->addr = a; -} - -/** - * @brief Called from parser to add A/AAAA data to search result - */ -static void _mdns_search_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl) -{ - mdns_result_t *r = NULL; - mdns_ip_addr_t *a = NULL; - - if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4) - || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6) - || search->type == MDNS_TYPE_ANY) { - r = search->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { - _mdns_result_add_ip(r, ip); - _mdns_result_update_ttl(r, ttl); - return; - } - r = r->next; - } - if (!search->max_results || search->num_results < search->max_results) { - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - return; - } - - memset(r, 0, sizeof(mdns_result_t)); - - a = _mdns_result_addr_create_ip(ip); - if (!a) { - mdns_mem_free(r); - return; - } - a->next = r->addr; - r->hostname = mdns_mem_strdup(hostname); - r->addr = a; - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->next = search->result; - r->ttl = ttl; - search->result = r; - search->num_results++; - } - } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) { - r = search->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { - _mdns_result_add_ip(r, ip); - _mdns_result_update_ttl(r, ttl); - break; - } - r = r->next; - } - } -} - -/** - * @brief Called from parser to add PTR data to search result - */ -static mdns_result_t *_mdns_search_result_add_ptr(mdns_search_once_t *search, const char *instance, - const char *service_type, const char *proto, mdns_if_t tcpip_if, - mdns_ip_protocol_t ip_protocol, uint32_t ttl) -{ - mdns_result_t *r = search->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) { - _mdns_result_update_ttl(r, ttl); - return r; - } - r = r->next; - } - if (!search->max_results || search->num_results < search->max_results) { - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - return NULL; - } - - memset(r, 0, sizeof(mdns_result_t)); - r->instance_name = mdns_mem_strdup(instance); - r->service_type = mdns_mem_strdup(service_type); - r->proto = mdns_mem_strdup(proto); - if (!r->instance_name) { - mdns_mem_free(r); - return NULL; - } - - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->ttl = ttl; - r->next = search->result; - search->result = r; - search->num_results++; - return r; - } - return NULL; -} - -/** - * @brief Called from parser to add SRV data to search result - */ -static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl) -{ - mdns_result_t *r = search->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { - _mdns_result_update_ttl(r, ttl); - return; - } - r = r->next; - } - if (!search->max_results || search->num_results < search->max_results) { - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - return; - } - - memset(r, 0, sizeof(mdns_result_t)); - r->hostname = mdns_mem_strdup(hostname); - if (!r->hostname) { - mdns_mem_free(r); - return; - } - if (search->instance) { - r->instance_name = mdns_mem_strdup(search->instance); - } - r->service_type = mdns_mem_strdup(search->service); - r->proto = mdns_mem_strdup(search->proto); - r->port = port; - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->ttl = ttl; - r->next = search->result; - search->result = r; - search->num_results++; - } -} - -/** - * @brief Called from parser to add TXT data to search result - */ -static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len, - size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, - uint32_t ttl) -{ - mdns_result_t *r = search->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { - if (r->txt) { - goto free_txt; - } - r->txt = txt; - r->txt_value_len = txt_value_len; - r->txt_count = txt_count; - _mdns_result_update_ttl(r, ttl); - return; - } - r = r->next; - } - if (!search->max_results || search->num_results < search->max_results) { - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - goto free_txt; - } - - memset(r, 0, sizeof(mdns_result_t)); - r->txt = txt; - r->txt_value_len = txt_value_len; - r->txt_count = txt_count; - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->ttl = ttl; - r->next = search->result; - search->result = r; - search->num_results++; - } - return; - -free_txt: - for (size_t i = 0; i < txt_count; i++) { - mdns_mem_free((char *)(txt[i].key)); - mdns_mem_free((char *)(txt[i].value)); - } - mdns_mem_free(txt); - mdns_mem_free(txt_value_len); -} - -/** - * @brief Called from packet parser to find matching running search - */ -static mdns_search_once_t *_mdns_search_find_from(mdns_search_once_t *s, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_result_t *r = NULL; - while (s) { - if (s->state == SEARCH_OFF) { - s = s->next; - continue; - } - - if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { - if ((s->type == MDNS_TYPE_ANY && s->service != NULL) - || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV)) { - s = s->next; - continue; - } - if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) { - if (!strcasecmp(name->host, s->instance)) { - return s; - } - s = s->next; - continue; - } - r = s->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { - return s; - } - r = r->next; - } - s = s->next; - continue; - } - - if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { - if ((s->type == MDNS_TYPE_ANY && s->service == NULL) - || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR)) { - s = s->next; - continue; - } - if (strcasecmp(name->service, s->service) - || strcasecmp(name->proto, s->proto)) { - s = s->next; - continue; - } - if (s->type != MDNS_TYPE_PTR) { - if (s->instance && strcasecmp(name->host, s->instance) == 0) { - return s; - } - s = s->next; - continue; - } - return s; - } - - if (type == MDNS_TYPE_PTR && type == s->type && !strcasecmp(name->service, s->service) && !strcasecmp(name->proto, s->proto)) { - return s; - } - - s = s->next; - } - - return NULL; -} - -/** - * @brief Create search packet for particular interface - */ -static mdns_tx_packet_t *_mdns_create_search_packet(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_result_t *r = NULL; - mdns_tx_packet_t *packet = _mdns_alloc_packet_default(tcpip_if, ip_protocol); - if (!packet) { - return NULL; - } - - mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); - if (!q) { - HOOK_MALLOC_FAILED; - _mdns_free_tx_packet(packet); - return NULL; - } - q->next = NULL; - q->unicast = search->unicast; - q->type = search->type; - q->host = search->instance; - q->service = search->service; - q->proto = search->proto; - q->domain = MDNS_DEFAULT_DOMAIN; - q->own_dynamic_memory = false; - queueToEnd(mdns_out_question_t, packet->questions, q); - - if (search->type == MDNS_TYPE_PTR) { - r = search->result; - while (r) { - //full record on the same interface is available - if (r->esp_netif != _mdns_get_esp_netif(tcpip_if) || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) { - r = r->next; - continue; - } - mdns_out_answer_t *a = (mdns_out_answer_t *)mdns_mem_malloc(sizeof(mdns_out_answer_t)); - if (!a) { - HOOK_MALLOC_FAILED; - _mdns_free_tx_packet(packet); - return NULL; - } - a->type = MDNS_TYPE_PTR; - a->service = NULL; - a->custom_instance = r->instance_name; - a->custom_service = search->service; - a->custom_proto = search->proto; - a->bye = false; - a->flush = false; - a->next = NULL; - queueToEnd(mdns_out_answer_t, packet->answers, a); - r = r->next; - } - } - - return packet; -} - -/** - * @brief Send search packet to particular interface - */ -static void _mdns_search_send_pcb(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - mdns_tx_packet_t *packet = NULL; - if (mdns_is_netif_ready(tcpip_if, ip_protocol) && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) { - packet = _mdns_create_search_packet(search, tcpip_if, ip_protocol); - if (!packet) { - return; - } - _mdns_dispatch_tx_packet(packet); - _mdns_free_tx_packet(packet); - } -} - -/** - * @brief Send search packet to all available interfaces - */ -static void _mdns_search_send(mdns_search_once_t *search) -{ - mdns_search_once_t *queue = _mdns_server->search_once; - bool found = false; - // looking for this search in active searches - while (queue) { - if (queue == search) { - found = true; - break; - } - queue = queue->next; - } - - if (!found) { - // no longer active -> skip sending this search - return; - } - - uint8_t i, j; - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - _mdns_search_send_pcb(search, (mdns_if_t)i, (mdns_ip_protocol_t)j); - } - } -} - -static void _mdns_tx_handle_packet(mdns_tx_packet_t *p) -{ - mdns_tx_packet_t *a = NULL; - mdns_out_question_t *q = NULL; - mdns_pcb_t *pcb = &_mdns_server->interfaces[p->tcpip_if].pcbs[p->ip_protocol]; - uint32_t send_after = 1000; - - if (pcb->state == PCB_OFF) { - _mdns_free_tx_packet(p); - return; - } - _mdns_dispatch_tx_packet(p); - - switch (pcb->state) { - case PCB_PROBE_1: - q = p->questions; - while (q) { - q->unicast = false; - q = q->next; - } - //fallthrough - case PCB_PROBE_2: - _mdns_schedule_tx_packet(p, 250); - pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1); - break; - case PCB_PROBE_3: - a = _mdns_create_announce_from_probe(p); - if (!a) { - _mdns_schedule_tx_packet(p, 250); - break; - } - pcb->probe_running = false; - pcb->probe_ip = false; - pcb->probe_services_len = 0; - pcb->failed_probes = 0; - mdns_mem_free(pcb->probe_services); - pcb->probe_services = NULL; - _mdns_free_tx_packet(p); - p = a; - send_after = 250; - //fallthrough - case PCB_ANNOUNCE_1: - //fallthrough - case PCB_ANNOUNCE_2: - _mdns_schedule_tx_packet(p, send_after); - pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1); - break; - case PCB_ANNOUNCE_3: - pcb->state = PCB_RUNNING; - _mdns_free_tx_packet(p); - break; - default: - _mdns_free_tx_packet(p); - break; - } -} - -static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname) -{ - mdns_srv_item_t *service = _mdns_server->services; - - while (service) { - if (service->service->hostname && - strcmp(service->service->hostname, old_hostname) == 0) { - mdns_mem_free((char *)service->service->hostname); - service->service->hostname = mdns_mem_strdup(new_hostname); - } - service = service->next; - } -} - -static void _mdns_sync_browse_result_link_free(mdns_browse_sync_t *browse_sync) -{ - mdns_browse_result_sync_t *current = browse_sync->sync_result; - mdns_browse_result_sync_t *need_free; - while (current) { - need_free = current; - current = current->next; - mdns_mem_free(need_free); - } - mdns_mem_free(browse_sync); -} - -/** - * @brief Free action data - */ -static void _mdns_free_action(mdns_action_t *action) -{ - switch (action->type) { - case ACTION_HOSTNAME_SET: - mdns_mem_free(action->data.hostname_set.hostname); - break; - case ACTION_INSTANCE_SET: - mdns_mem_free(action->data.instance); - break; - case ACTION_SEARCH_ADD: - //fallthrough - case ACTION_SEARCH_SEND: - //fallthrough - case ACTION_SEARCH_END: - _mdns_search_free(action->data.search_add.search); - break; - case ACTION_BROWSE_ADD: - //fallthrough - case ACTION_BROWSE_END: - _mdns_browse_item_free(action->data.browse_add.browse); - break; - case ACTION_BROWSE_SYNC: - _mdns_sync_browse_result_link_free(action->data.browse_sync.browse_sync); - break; - case ACTION_TX_HANDLE: - _mdns_free_tx_packet(action->data.tx_handle.packet); - break; - case ACTION_RX_HANDLE: - _mdns_packet_free(action->data.rx_handle.packet); - break; - case ACTION_DELEGATE_HOSTNAME_SET_ADDR: - case ACTION_DELEGATE_HOSTNAME_ADD: - mdns_mem_free((char *)action->data.delegate_hostname.hostname); - free_address_list(action->data.delegate_hostname.address_list); - break; - case ACTION_DELEGATE_HOSTNAME_REMOVE: - mdns_mem_free((char *)action->data.delegate_hostname.hostname); - break; - default: - break; - } - mdns_mem_free(action); -} - -/** - * @brief Called from service thread to execute given action - */ -static void _mdns_execute_action(mdns_action_t *action) -{ - switch (action->type) { - case ACTION_SYSTEM_EVENT: - perform_event_action(action->data.sys_event.interface, action->data.sys_event.event_action); - break; - case ACTION_HOSTNAME_SET: - _mdns_send_bye_all_pcbs_no_instance(true); - _mdns_remap_self_service_hostname(_mdns_server->hostname, action->data.hostname_set.hostname); - mdns_mem_free((char *)_mdns_server->hostname); - _mdns_server->hostname = action->data.hostname_set.hostname; - _mdns_self_host.hostname = action->data.hostname_set.hostname; - _mdns_restart_all_pcbs(); - xSemaphoreGive(_mdns_server->action_sema); - break; - case ACTION_INSTANCE_SET: - _mdns_send_bye_all_pcbs_no_instance(false); - mdns_mem_free((char *)_mdns_server->instance); - _mdns_server->instance = action->data.instance; - _mdns_restart_all_pcbs_no_instance(); - - break; - case ACTION_SEARCH_ADD: - _mdns_search_add(action->data.search_add.search); - break; - case ACTION_SEARCH_SEND: - _mdns_search_send(action->data.search_add.search); - break; - case ACTION_SEARCH_END: - _mdns_search_finish(action->data.search_add.search); - break; - - case ACTION_BROWSE_ADD: - _mdns_browse_add(action->data.browse_add.browse); - break; - case ACTION_BROWSE_SYNC: - _mdns_browse_sync(action->data.browse_sync.browse_sync); - _mdns_sync_browse_result_link_free(action->data.browse_sync.browse_sync); - break; - case ACTION_BROWSE_END: - _mdns_browse_finish(action->data.browse_add.browse); - break; - - case ACTION_TX_HANDLE: { - mdns_tx_packet_t *p = _mdns_server->tx_queue_head; - // packet to be handled should be at tx head, but must be consistent with the one pushed to action queue - if (p && p == action->data.tx_handle.packet && p->queued) { - p->queued = false; // clearing, as the packet might be reused (pushed and transmitted again) - _mdns_server->tx_queue_head = p->next; - _mdns_tx_handle_packet(p); - } else { - ESP_LOGD(TAG, "Skipping transmit of an unexpected packet!"); - } - } - break; - case ACTION_RX_HANDLE: - mdns_parse_packet(action->data.rx_handle.packet); - _mdns_packet_free(action->data.rx_handle.packet); - break; - case ACTION_DELEGATE_HOSTNAME_ADD: - if (!_mdns_delegate_hostname_add(action->data.delegate_hostname.hostname, - action->data.delegate_hostname.address_list)) { - mdns_mem_free((char *)action->data.delegate_hostname.hostname); - free_address_list(action->data.delegate_hostname.address_list); - } - xSemaphoreGive(_mdns_server->action_sema); - break; - case ACTION_DELEGATE_HOSTNAME_SET_ADDR: - if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname, - action->data.delegate_hostname.address_list)) { - free_address_list(action->data.delegate_hostname.address_list); - } - mdns_mem_free((char *)action->data.delegate_hostname.hostname); - break; - case ACTION_DELEGATE_HOSTNAME_REMOVE: - _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname); - mdns_mem_free((char *)action->data.delegate_hostname.hostname); - break; - default: - break; - } - mdns_mem_free(action); -} - -/** - * @brief Queue search action - */ -static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t *search) -{ - mdns_action_t *action = NULL; - - action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - - action->type = type; - action->data.search_add.search = search; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -/** - * @brief Called from timer task to run mDNS responder - * - * periodically checks first unqueued packet (from tx head). - * if it is scheduled to be transmitted, then pushes the packet to action queue to be handled. - * - */ -static void _mdns_scheduler_run(void) -{ - MDNS_SERVICE_LOCK(); - mdns_tx_packet_t *p = _mdns_server->tx_queue_head; - mdns_action_t *action = NULL; - - // find first unqueued packet - while (p && p->queued) { - p = p->next; - } - if (!p) { - MDNS_SERVICE_UNLOCK(); - return; - } - while (p && (int32_t)(p->send_at - (xTaskGetTickCount() * portTICK_PERIOD_MS)) < 0) { - action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (action) { - action->type = ACTION_TX_HANDLE; - action->data.tx_handle.packet = p; - p->queued = true; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - p->queued = false; - } - } else { - HOOK_MALLOC_FAILED; - break; - } - //Find the next unqued packet - p = p->next; - } - MDNS_SERVICE_UNLOCK(); -} - -/** - * @brief Called from timer task to run active searches - */ -static void _mdns_search_run(void) -{ - MDNS_SERVICE_LOCK(); - mdns_search_once_t *s = _mdns_server->search_once; - uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS; - if (!s) { - MDNS_SERVICE_UNLOCK(); - return; - } - while (s) { - if (s->state != SEARCH_OFF) { - if (now > (s->started_at + s->timeout)) { - s->state = SEARCH_OFF; - if (_mdns_send_search_action(ACTION_SEARCH_END, s) != ESP_OK) { - s->state = SEARCH_RUNNING; - } - } else if (s->state == SEARCH_INIT || (now - s->sent_at) > 1000) { - s->state = SEARCH_RUNNING; - s->sent_at = now; - if (_mdns_send_search_action(ACTION_SEARCH_SEND, s) != ESP_OK) { - s->sent_at -= 1000; - } - } - } - s = s->next; - } - MDNS_SERVICE_UNLOCK(); -} - -/** - * @brief the main MDNS service task. Packets are received and parsed here - */ -static void _mdns_service_task(void *pvParameters) -{ - mdns_action_t *a = NULL; - for (;;) { - if (_mdns_server && _mdns_server->action_queue) { - if (xQueueReceive(_mdns_server->action_queue, &a, portMAX_DELAY) == pdTRUE) { - assert(a); - if (a->type == ACTION_TASK_STOP) { - break; - } - MDNS_SERVICE_LOCK(); - _mdns_execute_action(a); - MDNS_SERVICE_UNLOCK(); - } - } else { - vTaskDelay(500 * portTICK_PERIOD_MS); - } - } - _mdns_service_task_handle = NULL; - vTaskDelay(portMAX_DELAY); -} - -static void _mdns_timer_cb(void *arg) -{ - _mdns_scheduler_run(); - _mdns_search_run(); -} - -static esp_err_t _mdns_start_timer(void) -{ - esp_timer_create_args_t timer_conf = { - .callback = _mdns_timer_cb, - .arg = NULL, - .dispatch_method = ESP_TIMER_TASK, - .name = "mdns_timer" - }; - esp_err_t err = esp_timer_create(&timer_conf, &(_mdns_server->timer_handle)); - if (err) { - return err; - } - return esp_timer_start_periodic(_mdns_server->timer_handle, MDNS_TIMER_PERIOD_US); -} - -static esp_err_t _mdns_stop_timer(void) -{ - esp_err_t err = ESP_OK; - if (_mdns_server->timer_handle) { - err = esp_timer_stop(_mdns_server->timer_handle); - if (err) { - return err; - } - err = esp_timer_delete(_mdns_server->timer_handle); - } - return err; -} - -static esp_err_t _mdns_task_create_with_caps(void) -{ - esp_err_t ret = ESP_OK; - static StaticTask_t mdns_task_buffer; - - _mdns_stack_buffer = mdns_mem_task_malloc(MDNS_SERVICE_STACK_DEPTH); - ESP_GOTO_ON_FALSE(_mdns_stack_buffer != NULL, ESP_FAIL, err, TAG, "failed to allocate memory for the mDNS task's stack"); - - _mdns_service_task_handle = xTaskCreateStaticPinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY, _mdns_stack_buffer, &mdns_task_buffer, MDNS_TASK_AFFINITY); - ESP_GOTO_ON_FALSE(_mdns_service_task_handle != NULL, ESP_FAIL, err, TAG, "failed to create task for the mDNS"); - - return ret; - -err: - mdns_mem_task_free(_mdns_stack_buffer); - return ret; -} - -/** - * @brief Start the service thread if not running - * - * @return - * - ESP_OK on success - * - ESP_FAIL on error - */ -static esp_err_t _mdns_service_task_start(void) -{ - esp_err_t ret = ESP_OK; - if (!_mdns_service_semaphore) { - _mdns_service_semaphore = xSemaphoreCreateMutex(); - ESP_RETURN_ON_FALSE(_mdns_service_semaphore != NULL, ESP_FAIL, TAG, "Failed to create the mDNS service lock"); - } - MDNS_SERVICE_LOCK(); - ESP_GOTO_ON_ERROR(_mdns_start_timer(), err, TAG, "Failed to start the mDNS service timer"); - - if (!_mdns_service_task_handle) { - ESP_GOTO_ON_ERROR(_mdns_task_create_with_caps(), err_stop_timer, TAG, "Failed to start the mDNS service task"); -#ifdef MDNS_ENABLE_DEBUG -#if !CONFIG_IDF_TARGET_LINUX - StackType_t *mdns_debug_stack_buffer; - StaticTask_t *mdns_debug_task_buffer; - xTaskGetStaticBuffers(_mdns_service_task_handle, &mdns_debug_stack_buffer, &mdns_debug_task_buffer); - _mdns_dbg_printf("mdns_debug_stack_buffer:%p mdns_debug_task_buffer:%p\n", mdns_debug_stack_buffer, mdns_debug_task_buffer); -#endif // CONFIG_IDF_TARGET_LINUX -#endif // MDNS_ENABLE_DEBUG - } - MDNS_SERVICE_UNLOCK(); - return ret; - -err_stop_timer: - _mdns_stop_timer(); -err: - MDNS_SERVICE_UNLOCK(); - vSemaphoreDelete(_mdns_service_semaphore); - _mdns_service_semaphore = NULL; - return ret; -} - -/** - * @brief Stop the service thread - * - * @return - * - ESP_OK - */ -static esp_err_t _mdns_service_task_stop(void) -{ - _mdns_stop_timer(); - if (_mdns_service_task_handle) { - TaskHandle_t task_handle = _mdns_service_task_handle; - mdns_action_t action; - mdns_action_t *a = &action; - action.type = ACTION_TASK_STOP; - if (xQueueSend(_mdns_server->action_queue, &a, (TickType_t)0) != pdPASS) { - _mdns_service_task_handle = NULL; - } - while (_mdns_service_task_handle) { - vTaskDelay(10 / portTICK_PERIOD_MS); - } - vTaskDelete(task_handle); - } - vSemaphoreDelete(_mdns_service_semaphore); - _mdns_service_semaphore = NULL; - return ESP_OK; -} - -static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action) -{ - if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) { - return ESP_ERR_INVALID_STATE; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_calloc(1, sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - action->type = ACTION_SYSTEM_EVENT; - action->data.sys_event.event_action = event_action; - action->data.sys_event.interface = mdns_if; - - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - } - return ESP_OK; -} - -static inline void set_default_duplicated_interfaces(void) -{ - mdns_if_t wifi_sta_if = MDNS_MAX_INTERFACES; - mdns_if_t eth_if = MDNS_MAX_INTERFACES; - for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; i++) { - if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_STA) { - wifi_sta_if = i; - } - if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_ETH) { - eth_if = i; - } - } - if (wifi_sta_if != MDNS_MAX_INTERFACES && eth_if != MDNS_MAX_INTERFACES) { - s_esp_netifs[wifi_sta_if].duplicate = eth_if; - s_esp_netifs[eth_if].duplicate = wifi_sta_if; - } -} - -static inline void unregister_predefined_handlers(void) -{ -#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) - esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); -#endif -#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); -#endif -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH - esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); -#endif -} - -/* - * Public Methods - * */ - -esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action) -{ - return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action); -} - -esp_err_t mdns_register_netif(esp_netif_t *esp_netif) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - - esp_err_t err = ESP_ERR_NO_MEM; - MDNS_SERVICE_LOCK(); - for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { - if (s_esp_netifs[i].netif == esp_netif) { - MDNS_SERVICE_UNLOCK(); - return ESP_ERR_INVALID_STATE; - } - } - - for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { - if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == NULL) { - s_esp_netifs[i].netif = esp_netif; - err = ESP_OK; - break; - } - } - MDNS_SERVICE_UNLOCK(); - return err; -} - -esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - - esp_err_t err = ESP_ERR_NOT_FOUND; - MDNS_SERVICE_LOCK(); - for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { - if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == esp_netif) { - s_esp_netifs[i].netif = NULL; - err = ESP_OK; - break; - } - } - MDNS_SERVICE_UNLOCK(); - return err; -} - - -esp_err_t mdns_init(void) -{ - esp_err_t err = ESP_OK; - - if (_mdns_server) { - return err; - } - - _mdns_server = (mdns_server_t *)mdns_mem_malloc(sizeof(mdns_server_t)); - if (!_mdns_server) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - memset((uint8_t *)_mdns_server, 0, sizeof(mdns_server_t)); - // zero-out local copy of netifs to initiate a fresh search by interface key whenever a netif ptr is needed - for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { - s_esp_netifs[i].netif = NULL; - } - - _mdns_server->action_queue = xQueueCreate(MDNS_ACTION_QUEUE_LEN, sizeof(mdns_action_t *)); - if (!_mdns_server->action_queue) { - err = ESP_ERR_NO_MEM; - goto free_server; - } - - _mdns_server->action_sema = xSemaphoreCreateBinary(); - if (!_mdns_server->action_sema) { - err = ESP_ERR_NO_MEM; - goto free_queue; - } - -#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) - if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { - goto free_event_handlers; - } -#endif -#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH - if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { - goto free_event_handlers; - } -#endif -#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH - if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { - goto free_event_handlers; - } -#endif - -#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH - set_default_duplicated_interfaces(); -#endif - - uint8_t i; -#ifdef CONFIG_LWIP_IPV6 - esp_ip6_addr_t tmp_addr6; -#endif -#ifdef CONFIG_LWIP_IPV4 - esp_netif_ip_info_t if_ip_info; -#endif - - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { -#ifdef CONFIG_LWIP_IPV6 - if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(i), &tmp_addr6) && !_ipv6_address_is_zero(tmp_addr6)) { - _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V6); - } -#endif -#ifdef CONFIG_LWIP_IPV4 - if (!esp_netif_get_ip_info(_mdns_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) { - _mdns_enable_pcb(i, MDNS_IP_PROTOCOL_V4); - } -#endif - } - if (_mdns_service_task_start()) { - //service start failed! - err = ESP_FAIL; - goto free_all_and_disable_pcbs; - } - - return ESP_OK; - -free_all_and_disable_pcbs: - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V6); - _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4); - s_esp_netifs[i].duplicate = MDNS_MAX_INTERFACES; - } -#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH -free_event_handlers: - unregister_predefined_handlers(); -#endif - vSemaphoreDelete(_mdns_server->action_sema); -free_queue: - vQueueDelete(_mdns_server->action_queue); -free_server: - mdns_mem_free(_mdns_server); - _mdns_server = NULL; - return err; -} - -void mdns_free(void) -{ - uint8_t i, j; - if (!_mdns_server) { - return; - } - - // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit - unregister_predefined_handlers(); - - mdns_service_remove_all(); - free_delegated_hostnames(); - _mdns_service_task_stop(); - // at this point, the service task is deleted, so we can destroy the stack size - mdns_mem_task_free(_mdns_stack_buffer); - for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { - mdns_pcb_deinit_local(i, j); - } - } - mdns_mem_free((char *)_mdns_server->hostname); - mdns_mem_free((char *)_mdns_server->instance); - if (_mdns_server->action_queue) { - mdns_action_t *c; - while (xQueueReceive(_mdns_server->action_queue, &c, 0) == pdTRUE) { - _mdns_free_action(c); - } - vQueueDelete(_mdns_server->action_queue); - } - _mdns_clear_tx_queue_head(); - while (_mdns_server->search_once) { - mdns_search_once_t *h = _mdns_server->search_once; - _mdns_server->search_once = h->next; - mdns_mem_free(h->instance); - mdns_mem_free(h->service); - mdns_mem_free(h->proto); - vSemaphoreDelete(h->done_semaphore); - if (h->result) { - _mdns_query_results_free(h->result); - } - mdns_mem_free(h); - } - while (_mdns_server->browse) { - mdns_browse_t *b = _mdns_server->browse; - _mdns_server->browse = _mdns_server->browse->next; - _mdns_browse_item_free(b); - - } - vSemaphoreDelete(_mdns_server->action_sema); - mdns_mem_free(_mdns_server); - _mdns_server = NULL; -} - -esp_err_t mdns_hostname_set(const char *hostname) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_ARG; - } - if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { - return ESP_ERR_INVALID_ARG; - } - char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); - if (!new_hostname) { - return ESP_ERR_NO_MEM; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - mdns_mem_free(new_hostname); - return ESP_ERR_NO_MEM; - } - action->type = ACTION_HOSTNAME_SET; - action->data.hostname_set.hostname = new_hostname; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(new_hostname); - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY); - return ESP_OK; -} - -esp_err_t mdns_hostname_get(char *hostname) -{ - if (!hostname) { - return ESP_ERR_INVALID_ARG; - } - - if (!_mdns_server || !_mdns_server->hostname) { - return ESP_ERR_INVALID_STATE; - } - - MDNS_SERVICE_LOCK(); - size_t len = strnlen(_mdns_server->hostname, MDNS_NAME_BUF_LEN - 1); - strncpy(hostname, _mdns_server->hostname, len); - hostname[len] = 0; - MDNS_SERVICE_UNLOCK(); - return ESP_OK; -} - -esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { - return ESP_ERR_INVALID_ARG; - } - char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); - if (!new_hostname) { - return ESP_ERR_NO_MEM; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - mdns_mem_free(new_hostname); - return ESP_ERR_NO_MEM; - } - action->type = ACTION_DELEGATE_HOSTNAME_ADD; - action->data.delegate_hostname.hostname = new_hostname; - action->data.delegate_hostname.address_list = copy_address_list(address_list); - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(new_hostname); - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY); - return ESP_OK; -} - -esp_err_t mdns_delegate_hostname_remove(const char *hostname) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { - return ESP_ERR_INVALID_ARG; - } - char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); - if (!new_hostname) { - return ESP_ERR_NO_MEM; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - mdns_mem_free(new_hostname); - return ESP_ERR_NO_MEM; - } - action->type = ACTION_DELEGATE_HOSTNAME_REMOVE; - action->data.delegate_hostname.hostname = new_hostname; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(new_hostname); - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { - return ESP_ERR_INVALID_ARG; - } - char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); - if (!new_hostname) { - return ESP_ERR_NO_MEM; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - mdns_mem_free(new_hostname); - return ESP_ERR_NO_MEM; - } - action->type = ACTION_DELEGATE_HOSTNAME_SET_ADDR; - action->data.delegate_hostname.hostname = new_hostname; - action->data.delegate_hostname.address_list = copy_address_list(address_list); - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(new_hostname); - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -bool mdns_hostname_exists(const char *hostname) -{ - bool ret = false; - MDNS_SERVICE_LOCK(); - ret = _hostname_is_ours(hostname); - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_instance_name_set(const char *instance) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (_str_null_or_empty(instance) || _mdns_server->hostname == NULL || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) { - return ESP_ERR_INVALID_ARG; - } - char *new_instance = mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1); - if (!new_instance) { - return ESP_ERR_NO_MEM; - } - - mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - mdns_mem_free(new_instance); - return ESP_ERR_NO_MEM; - } - action->type = ACTION_INSTANCE_SET; - action->data.instance = new_instance; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(new_instance); - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -/* - * MDNS SERVICES - * */ - -esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *host, - uint16_t port, mdns_txt_item_t txt[], size_t num_items) -{ - if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !_mdns_server->hostname) { - return ESP_ERR_INVALID_ARG; - } - - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - mdns_service_t *s = NULL; - - ESP_GOTO_ON_FALSE(_mdns_can_add_more_services(), ESP_ERR_NO_MEM, err, TAG, - "Cannot add more services, please increase CONFIG_MDNS_MAX_SERVICES (%d)", CONFIG_MDNS_MAX_SERVICES); - - mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(!item, ESP_ERR_INVALID_ARG, err, TAG, "Service already exists"); - - s = _mdns_create_service(service, proto, hostname, port, instance, num_items, txt); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory"); - - item = (mdns_srv_item_t *)mdns_mem_malloc(sizeof(mdns_srv_item_t)); - ESP_GOTO_ON_FALSE(item, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory"); - - item->service = s; - item->next = NULL; - - item->next = _mdns_server->services; - _mdns_server->services = item; - _mdns_probe_all_pcbs(&item, 1, false, false); - MDNS_SERVICE_UNLOCK(); - return ESP_OK; - -err: - MDNS_SERVICE_UNLOCK(); - _mdns_free_service(s); - if (ret == ESP_ERR_NO_MEM) { - HOOK_MALLOC_FAILED; - } - return ret; -} - -esp_err_t mdns_service_add(const char *instance, const char *service, const char *proto, uint16_t port, - mdns_txt_item_t txt[], size_t num_items) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_add_for_host(instance, service, proto, NULL, port, txt, num_items); -} - -bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname) -{ - bool ret = false; - MDNS_SERVICE_LOCK(); - ret = _mdns_get_service_item(service_type, proto, hostname) != NULL; - MDNS_SERVICE_UNLOCK(); - return ret; -} - -bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto, - const char *hostname) -{ - bool ret = false; - MDNS_SERVICE_LOCK(); - ret = _mdns_get_service_item_instance(instance, service_type, proto, hostname) != NULL; - MDNS_SERVICE_UNLOCK(); - return ret; -} - -static mdns_txt_item_t *_copy_mdns_txt_items(mdns_txt_linked_item_t *items, uint8_t **txt_value_len, size_t *txt_count) -{ - mdns_txt_item_t *ret = NULL; - size_t ret_index = 0; - for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) { - ret_index++; - } - *txt_count = ret_index; - if (ret_index == 0) { // handle empty TXT - *txt_value_len = NULL; - return NULL; - } - ret = (mdns_txt_item_t *)mdns_mem_calloc(ret_index, sizeof(mdns_txt_item_t)); - *txt_value_len = (uint8_t *)mdns_mem_calloc(ret_index, sizeof(uint8_t)); - if (!ret || !(*txt_value_len)) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - ret_index = 0; - for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) { - size_t key_len = strlen(tmp->key); - char *key = (char *)mdns_mem_malloc(key_len + 1); - if (!key) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - memcpy(key, tmp->key, key_len); - key[key_len] = 0; - ret[ret_index].key = key; - char *value = (char *)mdns_mem_malloc(tmp->value_len + 1); - if (!value) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - memcpy(value, tmp->value, tmp->value_len); - value[tmp->value_len] = 0; - ret[ret_index].value = value; - (*txt_value_len)[ret_index] = tmp->value_len; - ret_index++; - } - return ret; - -handle_error: - for (size_t y = 0; y < ret_index + 1 && ret != NULL; y++) { - mdns_txt_item_t *t = &ret[y]; - mdns_mem_free((char *)t->key); - mdns_mem_free((char *)t->value); - } - mdns_mem_free(*txt_value_len); - mdns_mem_free(ret); - return NULL; -} - -static mdns_ip_addr_t *_copy_delegated_host_address_list(char *hostname) -{ - mdns_host_item_t *host = _mdns_host_list; - while (host) { - if (strcasecmp(host->hostname, hostname) == 0) { - return copy_address_list(host->address_list); - } - host = host->next; - } - return NULL; -} - -static mdns_result_t *_mdns_lookup_service(const char *instance, const char *service, const char *proto, size_t max_results, bool selfhost) -{ - if (_str_null_or_empty(service) || _str_null_or_empty(proto)) { - return NULL; - } - mdns_result_t *results = NULL; - size_t num_results = 0; - mdns_srv_item_t *s = _mdns_server->services; - while (s) { - mdns_service_t *srv = s->service; - if (!srv || !srv->hostname) { - s = s->next; - continue; - } - bool is_service_selfhosted = !_str_null_or_empty(_mdns_server->hostname) && !strcasecmp(_mdns_server->hostname, srv->hostname); - bool is_service_delegated = _str_null_or_empty(_mdns_server->hostname) || strcasecmp(_mdns_server->hostname, srv->hostname); - if ((selfhost && is_service_selfhosted) || (!selfhost && is_service_delegated)) { - if (!strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) && - (_str_null_or_empty(instance) || _mdns_instance_name_match(srv->instance, instance))) { - mdns_result_t *item = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!item) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - item->next = results; - results = item; - item->esp_netif = NULL; - item->ttl = _str_null_or_empty(instance) ? MDNS_ANSWER_PTR_TTL : MDNS_ANSWER_SRV_TTL; - item->ip_protocol = MDNS_IP_PROTOCOL_MAX; - if (srv->instance) { - item->instance_name = mdns_mem_strndup(srv->instance, MDNS_NAME_BUF_LEN - 1); - if (!item->instance_name) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - } else { - item->instance_name = NULL; - } - item->service_type = mdns_mem_strndup(srv->service, MDNS_NAME_BUF_LEN - 1); - if (!item->service_type) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - item->proto = mdns_mem_strndup(srv->proto, MDNS_NAME_BUF_LEN - 1); - if (!item->proto) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - item->hostname = mdns_mem_strndup(srv->hostname, MDNS_NAME_BUF_LEN - 1); - if (!item->hostname) { - HOOK_MALLOC_FAILED; - goto handle_error; - } - item->port = srv->port; - item->txt = _copy_mdns_txt_items(srv->txt, &(item->txt_value_len), &(item->txt_count)); - // We should not append addresses for selfhost lookup result as we don't know which interface's address to append. - if (selfhost) { - item->addr = NULL; - } else { - item->addr = _copy_delegated_host_address_list(item->hostname); - if (!item->addr) { - goto handle_error; - } - } - if (num_results < max_results) { - num_results++; - } - if (num_results >= max_results) { - break; - } - } - } - s = s->next; - } - return results; -handle_error: - _mdns_query_results_free(results); - return NULL; -} - -esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service, const char *proto, const char *host, uint16_t port) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && port, - ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - s->service->port = port; - _mdns_announce_all_pcbs(&s, 1, true); - -err: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_port_set(const char *service, const char *proto, uint16_t port) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_port_set_for_host(NULL, service, proto, NULL, port); -} - -esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *host, - mdns_txt_item_t txt_items[], uint8_t num_items) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !(num_items && txt_items == NULL), - ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - mdns_txt_linked_item_t *new_txt = NULL; - if (num_items) { - new_txt = _mdns_allocate_txt(num_items, txt_items); - if (!new_txt) { - return ESP_ERR_NO_MEM; - } - } - mdns_service_t *srv = s->service; - mdns_txt_linked_item_t *txt = srv->txt; - srv->txt = NULL; - _mdns_free_linked_txt(txt); - srv->txt = new_txt; - _mdns_announce_all_pcbs(&s, 1, false); - -err: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_txt_set(const char *service, const char *proto, mdns_txt_item_t txt[], uint8_t num_items) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_txt_set_for_host(NULL, service, proto, NULL, txt, num_items); -} - -esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service, const char *proto, - const char *host, const char *key, const char *value_arg, uint8_t value_len) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - char *value = NULL; - mdns_txt_linked_item_t *new_txt = NULL; - const char *hostname = host ? host : _mdns_server->hostname; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !_str_null_or_empty(key) && - !((!value_arg && value_len)), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - mdns_service_t *srv = s->service; - if (value_len > 0) { - value = (char *) mdns_mem_malloc(value_len); - ESP_GOTO_ON_FALSE(value, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - memcpy(value, value_arg, value_len); - } else { - value_len = 0; - } - mdns_txt_linked_item_t *txt = srv->txt; - while (txt) { - if (strcmp(txt->key, key) == 0) { - mdns_mem_free((char *)txt->value); - txt->value = value; - txt->value_len = value_len; - break; - } - txt = txt->next; - } - if (!txt) { - new_txt = (mdns_txt_linked_item_t *)mdns_mem_malloc(sizeof(mdns_txt_linked_item_t)); - ESP_GOTO_ON_FALSE(new_txt, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - new_txt->key = mdns_mem_strdup(key); - ESP_GOTO_ON_FALSE(new_txt->key, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - new_txt->value = value; - new_txt->value_len = value_len; - new_txt->next = srv->txt; - srv->txt = new_txt; - } - - _mdns_announce_all_pcbs(&s, 1, false); - -err: - MDNS_SERVICE_UNLOCK(); - return ret; -out_of_mem: - MDNS_SERVICE_UNLOCK(); - HOOK_MALLOC_FAILED; - mdns_mem_free(value); - mdns_mem_free(new_txt); - return ret; -} - -esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname, - const char *key, const char *value) -{ - return mdns_service_txt_item_set_for_host_with_explicit_value_len(instance, service, proto, hostname, key, value, - strlen(value)); -} - - -esp_err_t mdns_service_txt_item_set(const char *service, const char *proto, const char *key, const char *value) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key, - value, strlen(value)); -} - -esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service, const char *proto, const char *key, - const char *value, uint8_t value_len) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key, value, value_len); -} - -esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service, const char *proto, const char *host, - const char *key) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !_str_null_or_empty(key), - ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - mdns_service_t *srv = s->service; - mdns_txt_linked_item_t *txt = srv->txt; - if (!txt) { - goto err; - } - if (strcmp(txt->key, key) == 0) { - srv->txt = txt->next; - mdns_mem_free((char *)txt->key); - mdns_mem_free((char *)txt->value); - mdns_mem_free(txt); - } else { - while (txt->next) { - if (strcmp(txt->next->key, key) == 0) { - mdns_txt_linked_item_t *t = txt->next; - txt->next = t->next; - mdns_mem_free((char *)t->key); - mdns_mem_free((char *)t->value); - mdns_mem_free(t); - break; - } else { - txt = txt->next; - } - } - } - - _mdns_announce_all_pcbs(&s, 1, false); - -err: - MDNS_SERVICE_UNLOCK(); - if (ret == ESP_ERR_NO_MEM) { - HOOK_MALLOC_FAILED; - } - return ret; -} - -esp_err_t mdns_service_txt_item_remove(const char *service, const char *proto, const char *key) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_txt_item_remove_for_host(NULL, service, proto, NULL, key); -} - -static esp_err_t _mdns_service_subtype_remove_for_host(mdns_srv_item_t *service, const char *subtype) -{ - esp_err_t ret = ESP_ERR_NOT_FOUND; - mdns_subtype_t *srv_subtype = service->service->subtype; - mdns_subtype_t *pre = service->service->subtype; - while (srv_subtype) { - if (strcmp(srv_subtype->subtype, subtype) == 0) { - // Target subtype is found. - if (srv_subtype == service->service->subtype) { - // The first node needs to be removed - service->service->subtype = service->service->subtype->next; - } else { - pre->next = srv_subtype->next; - } - mdns_mem_free((char *)srv_subtype->subtype); - mdns_mem_free(srv_subtype); - ret = ESP_OK; - break; - } - pre = srv_subtype; - srv_subtype = srv_subtype->next; - } - if (ret == ESP_ERR_NOT_FOUND) { - ESP_LOGE(TAG, "Subtype : %s doesn't exist", subtype); - } - - return ret; -} - -esp_err_t mdns_service_subtype_remove_for_host(const char *instance_name, const char *service, const char *proto, - const char *hostname, const char *subtype) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && - !_str_null_or_empty(subtype), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - ret = _mdns_service_subtype_remove_for_host(s, subtype); - ESP_GOTO_ON_ERROR(ret, err, TAG, "Failed to remove the subtype: %s", subtype); - - // Transmit a sendbye message for the removed subtype. - mdns_subtype_t *remove_subtypes = (mdns_subtype_t *)mdns_mem_malloc(sizeof(mdns_subtype_t)); - ESP_GOTO_ON_FALSE(remove_subtypes, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - remove_subtypes->subtype = mdns_mem_strdup(subtype); - ESP_GOTO_ON_FALSE(remove_subtypes->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - remove_subtypes->next = NULL; - - _mdns_send_bye_subtype(s, instance_name, remove_subtypes); - _mdns_free_subtype(remove_subtypes); -err: - MDNS_SERVICE_UNLOCK(); - return ret; -out_of_mem: - HOOK_MALLOC_FAILED; - mdns_mem_free(remove_subtypes); - MDNS_SERVICE_UNLOCK(); - return ret; -} - -static esp_err_t _mdns_service_subtype_add_for_host(mdns_srv_item_t *service, const char *subtype) -{ - esp_err_t ret = ESP_OK; - mdns_subtype_t *srv_subtype = service->service->subtype; - while (srv_subtype) { - ESP_GOTO_ON_FALSE(strcmp(srv_subtype->subtype, subtype) != 0, ESP_ERR_INVALID_ARG, err, TAG, "Subtype: %s has already been added", subtype); - srv_subtype = srv_subtype->next; - } - - mdns_subtype_t *subtype_item = (mdns_subtype_t *)mdns_mem_malloc(sizeof(mdns_subtype_t)); - ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - subtype_item->subtype = mdns_mem_strdup(subtype); - ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - subtype_item->next = service->service->subtype; - service->service->subtype = subtype_item; - -err: - return ret; -out_of_mem: - HOOK_MALLOC_FAILED; - mdns_mem_free(subtype_item); - return ret; -} - -esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char *instance_name, const char *service, const char *proto, - const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - int cur_index = 0; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && - (num_items > 0), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - for (; cur_index < num_items; cur_index++) { - ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype); - if (ret == ESP_OK) { - continue; - } else if (ret == ESP_ERR_NO_MEM) { - ESP_LOGE(TAG, "Out of memory"); - goto err; - } else { - ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype); - goto exit; - } - } - - _mdns_announce_all_pcbs(&s, 1, false); -err: - if (ret == ESP_ERR_NO_MEM) { - for (int idx = 0; idx < cur_index; idx++) { - _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype); - } - } -exit: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto, - const char *hostname, const char *subtype) -{ - mdns_subtype_item_t _subtype[1]; - _subtype[0].subtype = subtype; - return mdns_service_subtype_add_multiple_items_for_host(instance_name, service_type, proto, hostname, _subtype, 1); -} - -static mdns_subtype_t *_mdns_service_find_subtype_needed_sendbye(mdns_service_t *service, mdns_subtype_item_t subtype[], - uint8_t num_items) -{ - if (!service) { - return NULL; - } - - mdns_subtype_t *current = service->subtype; - mdns_subtype_t *prev = NULL; - mdns_subtype_t *prev_goodbye = NULL; - mdns_subtype_t *out_goodbye_subtype = NULL; - - while (current) { - bool subtype_in_update = false; - - for (int i = 0; i < num_items; i++) { - if (strcmp(subtype[i].subtype, current->subtype) == 0) { - subtype_in_update = true; - break; - } - } - - if (!subtype_in_update) { - // Remove from original list - if (prev) { - prev->next = current->next; - } else { - service->subtype = current->next; - } - - mdns_subtype_t *to_move = current; - current = current->next; - - // Add to goodbye list - to_move->next = NULL; - if (prev_goodbye) { - prev_goodbye->next = to_move; - } else { - out_goodbye_subtype = to_move; - } - prev_goodbye = to_move; - } else { - prev = current; - current = current->next; - } - } - - return out_goodbye_subtype; -} - -esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char *instance_name, const char *service_type, const char *proto, - const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - int cur_index = 0; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service_type) && !_str_null_or_empty(proto), - ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service_type, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - mdns_subtype_t *goodbye_subtype = _mdns_service_find_subtype_needed_sendbye(s->service, subtype, num_items); - - if (goodbye_subtype) { - _mdns_send_bye_subtype(s, instance_name, goodbye_subtype); - } - - _mdns_free_subtype(goodbye_subtype); - _mdns_free_service_subtype(s->service); - - for (; cur_index < num_items; cur_index++) { - ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype); - if (ret == ESP_OK) { - continue; - } else if (ret == ESP_ERR_NO_MEM) { - ESP_LOGE(TAG, "Out of memory"); - goto err; - } else { - ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype); - goto exit; - } - } - if (num_items) { - _mdns_announce_all_pcbs(&s, 1, false); - } -err: - if (ret == ESP_ERR_NO_MEM) { - for (int idx = 0; idx < cur_index; idx++) { - _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype); - } - } -exit: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *host, - const char *instance) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && - !_str_null_or_empty(instance) && strlen(instance) <= (MDNS_NAME_BUF_LEN - 1), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_old, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - if (s->service->instance) { - _mdns_send_bye(&s, 1, false); - mdns_mem_free((char *)s->service->instance); - } - s->service->instance = mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1); - ESP_GOTO_ON_FALSE(s->service->instance, ESP_ERR_NO_MEM, err, TAG, "Out of memory"); - _mdns_probe_all_pcbs(&s, 1, false, false); - -err: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_instance_name_set(const char *service, const char *proto, const char *instance) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_instance_name_set_for_host(NULL, service, proto, NULL, instance); -} - -esp_err_t mdns_service_remove_for_host(const char *instance, const char *service, const char *proto, const char *host) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - const char *hostname = host ? host : _mdns_server->hostname; - ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto), - ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); - mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname); - ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - - mdns_srv_item_t *a = _mdns_server->services; - mdns_srv_item_t *b = a; - if (instance) { - while (a) { - if (_mdns_service_match_instance(a->service, instance, service, proto, hostname)) { - if (_mdns_server->services != a) { - b->next = a->next; - } else { - _mdns_server->services = a->next; - } - _mdns_send_bye(&a, 1, false); - _mdns_remove_scheduled_service_packets(a->service); - _mdns_free_service(a->service); - mdns_mem_free(a); - break; - } - b = a; - a = a->next; - } - } else { - while (a) { - if (_mdns_service_match(a->service, service, proto, hostname)) { - if (_mdns_server->services != a) { - b->next = a->next; - } else { - _mdns_server->services = a->next; - } - _mdns_send_bye(&a, 1, false); - _mdns_remove_scheduled_service_packets(a->service); - _mdns_free_service(a->service); - mdns_mem_free(a); - break; - } - b = a; - a = a->next; - } - } - -err: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -esp_err_t mdns_service_remove(const char *service_type, const char *proto) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - return mdns_service_remove_for_host(NULL, service_type, proto, NULL); -} - -esp_err_t mdns_service_remove_all(void) -{ - MDNS_SERVICE_LOCK(); - esp_err_t ret = ESP_OK; - ESP_GOTO_ON_FALSE(_mdns_server, ESP_ERR_INVALID_ARG, done, TAG, "Invalid state"); - if (!_mdns_server->services) { - goto done; - } - - _mdns_send_final_bye(false); - mdns_srv_item_t *services = _mdns_server->services; - _mdns_server->services = NULL; - while (services) { - mdns_srv_item_t *s = services; - services = services->next; - _mdns_remove_scheduled_service_packets(s->service); - _mdns_free_service(s->service); - mdns_mem_free(s); - } - -done: - MDNS_SERVICE_UNLOCK(); - return ret; -} - -/* - * MDNS QUERY - * */ -void mdns_query_results_free(mdns_result_t *results) -{ - MDNS_SERVICE_LOCK(); - _mdns_query_results_free(results); - MDNS_SERVICE_UNLOCK(); -} - -static void _mdns_query_results_free(mdns_result_t *results) -{ - mdns_result_t *r; - mdns_ip_addr_t *a; - - while (results) { - r = results; - - mdns_mem_free((char *)(r->hostname)); - mdns_mem_free((char *)(r->instance_name)); - mdns_mem_free((char *)(r->service_type)); - mdns_mem_free((char *)(r->proto)); - - for (size_t i = 0; i < r->txt_count; i++) { - mdns_mem_free((char *)(r->txt[i].key)); - mdns_mem_free((char *)(r->txt[i].value)); - } - mdns_mem_free(r->txt); - mdns_mem_free(r->txt_value_len); - - while (r->addr) { - a = r->addr; - r->addr = r->addr->next; - mdns_mem_free(a); - } - - results = results->next; - mdns_mem_free(r); - } -} - -esp_err_t mdns_query_async_delete(mdns_search_once_t *search) -{ - if (!search) { - return ESP_ERR_INVALID_ARG; - } - if (search->state != SEARCH_OFF) { - return ESP_ERR_INVALID_STATE; - } - - MDNS_SERVICE_LOCK(); - _mdns_search_free(search); - MDNS_SERVICE_UNLOCK(); - - return ESP_OK; -} - -bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results) -{ - if (xSemaphoreTake(search->done_semaphore, pdMS_TO_TICKS(timeout)) == pdTRUE) { - if (results) { - *results = search->result; - } - if (num_results) { - *num_results = search->num_results; - } - return true; - } - return false; -} - -mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type, - uint32_t timeout, size_t max_results, mdns_query_notify_t notifier) -{ - mdns_search_once_t *search = NULL; - - if (!_mdns_server || !timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) { - return NULL; - } - - search = _mdns_search_init(name, service, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, notifier); - if (!search) { - return NULL; - } - - if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) { - _mdns_search_free(search); - return NULL; - } - - return search; -} - -esp_err_t mdns_query_generic(const char *name, const char *service, const char *proto, uint16_t type, mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results) -{ - mdns_search_once_t *search = NULL; - - *results = NULL; - - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - - if (!timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - - search = _mdns_search_init(name, service, proto, type, transmission_type == MDNS_QUERY_UNICAST, timeout, max_results, NULL); - if (!search) { - return ESP_ERR_NO_MEM; - } - - if (_mdns_send_search_action(ACTION_SEARCH_ADD, search)) { - _mdns_search_free(search); - return ESP_ERR_NO_MEM; - } - xSemaphoreTake(search->done_semaphore, portMAX_DELAY); - - *results = search->result; - _mdns_search_free(search); - - return ESP_OK; -} - -esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results) -{ - return mdns_query_generic(name, service_type, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, results); -} - -esp_err_t mdns_query_ptr(const char *service, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results) -{ - if (_str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - - return mdns_query(NULL, service, proto, MDNS_TYPE_PTR, timeout, max_results, results); -} - -esp_err_t mdns_query_srv(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result) -{ - if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - - return mdns_query(instance, service, proto, MDNS_TYPE_SRV, timeout, 1, result); -} - -esp_err_t mdns_query_txt(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result) -{ - if (_str_null_or_empty(instance) || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - - return mdns_query(instance, service, proto, MDNS_TYPE_TXT, timeout, 1, result); -} - -esp_err_t mdns_lookup_delegated_service(const char *instance, const char *service, const char *proto, size_t max_results, - mdns_result_t **result) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (!result || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - MDNS_SERVICE_LOCK(); - *result = _mdns_lookup_service(instance, service, proto, max_results, false); - MDNS_SERVICE_UNLOCK(); - return ESP_OK; -} - -esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *service, const char *proto, size_t max_results, - mdns_result_t **result) -{ - if (!_mdns_server) { - return ESP_ERR_INVALID_STATE; - } - if (!result || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_ERR_INVALID_ARG; - } - MDNS_SERVICE_LOCK(); - *result = _mdns_lookup_service(instance, service, proto, max_results, true); - MDNS_SERVICE_UNLOCK(); - return ESP_OK; -} - -#ifdef CONFIG_LWIP_IPV4 -esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr) -{ - mdns_result_t *result = NULL; - esp_err_t err; - - if (_str_null_or_empty(name)) { - return ESP_ERR_INVALID_ARG; - } - - if (strstr(name, ".local")) { - ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain"); - } - - err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result); - - if (err) { - return err; - } - - if (!result) { - return ESP_ERR_NOT_FOUND; - } - - mdns_ip_addr_t *a = result->addr; - while (a) { - if (a->addr.type == ESP_IPADDR_TYPE_V4) { - addr->addr = a->addr.u_addr.ip4.addr; - mdns_query_results_free(result); - return ESP_OK; - } - a = a->next; - } - - mdns_query_results_free(result); - return ESP_ERR_NOT_FOUND; -} -#endif /* CONFIG_LWIP_IPV4 */ - -#ifdef CONFIG_LWIP_IPV6 -esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *addr) -{ - mdns_result_t *result = NULL; - esp_err_t err; - - if (_str_null_or_empty(name)) { - return ESP_ERR_INVALID_ARG; - } - - if (strstr(name, ".local")) { - ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain"); - } - - err = mdns_query(name, NULL, NULL, MDNS_TYPE_AAAA, timeout, 1, &result); - - if (err) { - return err; - } - - if (!result) { - return ESP_ERR_NOT_FOUND; - } - - mdns_ip_addr_t *a = result->addr; - while (a) { - if (a->addr.type == ESP_IPADDR_TYPE_V6) { - memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16); - mdns_query_results_free(result); - return ESP_OK; - } - a = a->next; - } - - mdns_query_results_free(result); - return ESP_ERR_NOT_FOUND; -} -#endif /* CONFIG_LWIP_IPV6 */ - -#ifdef MDNS_ENABLE_DEBUG - -void mdns_debug_packet(const uint8_t *data, size_t len) -{ - static mdns_name_t n; - mdns_header_t header; - const uint8_t *content = data + MDNS_HEAD_LEN; - uint32_t t = xTaskGetTickCount() * portTICK_PERIOD_MS; - mdns_name_t *name = &n; - memset(name, 0, sizeof(mdns_name_t)); - - _mdns_dbg_printf("Packet[%" PRIu32 "]: ", t); - - header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET); - header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET); - header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET); - header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET); - header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET); - header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET); - - _mdns_dbg_printf("%s", - (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE) ? "AUTHORITATIVE\n" : - (header.flags == MDNS_FLAGS_DISTRIBUTED) ? "DISTRIBUTED\n" : - (header.flags == 0) ? "\n" : " " - ); - if (header.flags && header.flags != MDNS_FLAGS_QR_AUTHORITATIVE) { - _mdns_dbg_printf("0x%04X\n", header.flags); - } - - if (header.questions) { - uint8_t qs = header.questions; - - while (qs--) { - content = _mdns_parse_fqdn(data, content, name, len); - if (!content || content + MDNS_CLASS_OFFSET + 1 >= data + len) { - header.answers = 0; - header.additional = 0; - header.servers = 0; - _mdns_dbg_printf("ERROR: parse header questions\n"); - break; - } - - uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET); - uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET); - bool unicast = !!(mdns_class & 0x8000); - mdns_class &= 0x7FFF; - content = content + 4; - - _mdns_dbg_printf(" Q: "); - if (unicast) { - _mdns_dbg_printf("*U* "); - } - if (type == MDNS_TYPE_PTR) { - _mdns_dbg_printf("%s.%s%s.%s.%s. PTR ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_SRV) { - _mdns_dbg_printf("%s.%s%s.%s.%s. SRV ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_TXT) { - _mdns_dbg_printf("%s.%s%s.%s.%s. TXT ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_A) { - _mdns_dbg_printf("%s.%s. A ", name->host, name->domain); - } else if (type == MDNS_TYPE_AAAA) { - _mdns_dbg_printf("%s.%s. AAAA ", name->host, name->domain); - } else if (type == MDNS_TYPE_NSEC) { - _mdns_dbg_printf("%s.%s%s.%s.%s. NSEC ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_ANY) { - _mdns_dbg_printf("%s.%s%s.%s.%s. ANY ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); - } else { - _mdns_dbg_printf("%s.%s%s.%s.%s. %04X ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain, type); - } - - if (mdns_class == 0x0001) { - _mdns_dbg_printf("IN"); - } else { - _mdns_dbg_printf("%04X", mdns_class); - } - _mdns_dbg_printf("\n"); - } - } - - if (header.answers || header.servers || header.additional) { - uint16_t recordIndex = 0; - - while (content < (data + len)) { - - content = _mdns_parse_fqdn(data, content, name, len); - if (!content) { - _mdns_dbg_printf("ERROR: parse mdns records\n"); - break; - } - - uint16_t type = _mdns_read_u16(content, MDNS_TYPE_OFFSET); - uint16_t mdns_class = _mdns_read_u16(content, MDNS_CLASS_OFFSET); - uint32_t ttl = _mdns_read_u32(content, MDNS_TTL_OFFSET); - uint16_t data_len = _mdns_read_u16(content, MDNS_LEN_OFFSET); - const uint8_t *data_ptr = content + MDNS_DATA_OFFSET; - bool flush = !!(mdns_class & 0x8000); - mdns_class &= 0x7FFF; - - content = data_ptr + data_len; - if (content > (data + len)) { - _mdns_dbg_printf("ERROR: content length overflow\n"); - break; - } - - mdns_parsed_record_type_t record_type = MDNS_ANSWER; - - if (recordIndex >= (header.answers + header.servers)) { - record_type = MDNS_EXTRA; - } else if (recordIndex >= (header.answers)) { - record_type = MDNS_NS; - } - recordIndex++; - - if (record_type == MDNS_EXTRA) { - _mdns_dbg_printf(" X"); - } else if (record_type == MDNS_NS) { - _mdns_dbg_printf(" S"); - } else { - _mdns_dbg_printf(" A"); - } - - if (type == MDNS_TYPE_PTR) { - _mdns_dbg_printf(": %s%s%s.%s.%s. PTR ", name->host, name->host[0] ? "." : "", name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_SRV) { - _mdns_dbg_printf(": %s.%s.%s.%s. SRV ", name->host, name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_TXT) { - _mdns_dbg_printf(": %s.%s.%s.%s. TXT ", name->host, name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_A) { - _mdns_dbg_printf(": %s.%s. A ", name->host, name->domain); - } else if (type == MDNS_TYPE_AAAA) { - _mdns_dbg_printf(": %s.%s. AAAA ", name->host, name->domain); - } else if (type == MDNS_TYPE_NSEC) { - _mdns_dbg_printf(": %s.%s.%s.%s. NSEC ", name->host, name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_ANY) { - _mdns_dbg_printf(": %s.%s.%s.%s. ANY ", name->host, name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_OPT) { - _mdns_dbg_printf(": . OPT "); - } else { - _mdns_dbg_printf(": %s.%s.%s.%s. %04X ", name->host, name->service, name->proto, name->domain, type); - } - - if (mdns_class == 0x0001) { - _mdns_dbg_printf("IN "); - } else { - _mdns_dbg_printf("%04X ", mdns_class); - } - if (flush) { - _mdns_dbg_printf("FLUSH "); - } - _mdns_dbg_printf("%" PRIu32, ttl); - _mdns_dbg_printf("[%u] ", data_len); - if (type == MDNS_TYPE_PTR) { - if (!_mdns_parse_fqdn(data, data_ptr, name, len)) { - _mdns_dbg_printf("ERROR: parse PTR\n"); - continue; - } - _mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain); - } else if (type == MDNS_TYPE_SRV) { - if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) { - _mdns_dbg_printf("ERROR: parse SRV\n"); - continue; - } - uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET); - uint16_t weight = _mdns_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET); - uint16_t port = _mdns_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET); - _mdns_dbg_printf("%u %u %u %s.%s.\n", priority, weight, port, name->host, name->domain); - } else if (type == MDNS_TYPE_TXT) { - uint16_t i = 0, y; - while (i < data_len) { - uint8_t partLen = data_ptr[i++]; - if ((i + partLen) > data_len) { - _mdns_dbg_printf("ERROR: parse TXT\n"); - break; - } - char txt[partLen + 1]; - for (y = 0; y < partLen; y++) { - char d = data_ptr[i++]; - txt[y] = d; - } - txt[partLen] = 0; - _mdns_dbg_printf("%s", txt); - if (i < data_len) { - _mdns_dbg_printf("; "); - } - } - _mdns_dbg_printf("\n"); - } else if (type == MDNS_TYPE_AAAA) { - esp_ip6_addr_t ip6; - memcpy(&ip6, data_ptr, sizeof(esp_ip6_addr_t)); - _mdns_dbg_printf(IPV6STR "\n", IPV62STR(ip6)); - } else if (type == MDNS_TYPE_A) { - esp_ip4_addr_t ip; - memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t)); - _mdns_dbg_printf(IPSTR "\n", IP2STR(&ip)); - } else if (type == MDNS_TYPE_NSEC) { - const uint8_t *old_ptr = data_ptr; - const uint8_t *new_ptr = _mdns_parse_fqdn(data, data_ptr, name, len); - if (new_ptr) { - _mdns_dbg_printf("%s.%s.%s.%s. ", name->host, name->service, name->proto, name->domain); - size_t diff = new_ptr - old_ptr; - data_len -= diff; - data_ptr = new_ptr; - } - size_t i; - for (i = 0; i < data_len; i++) { - _mdns_dbg_printf(" %02x", data_ptr[i]); - } - _mdns_dbg_printf("\n"); - } else if (type == MDNS_TYPE_OPT) { - uint16_t opCode = _mdns_read_u16(data_ptr, 0); - uint16_t opLen = _mdns_read_u16(data_ptr, 2); - _mdns_dbg_printf(" Code: %04x Data[%u]:", opCode, opLen); - size_t i; - for (i = 4; i < data_len; i++) { - _mdns_dbg_printf(" %02x", data_ptr[i]); - } - _mdns_dbg_printf("\n"); - } else { - size_t i; - for (i = 0; i < data_len; i++) { - _mdns_dbg_printf(" %02x", data_ptr[i]); - } - _mdns_dbg_printf("\n"); - } - } - } -} -#endif /* MDNS_ENABLE_DEBUG */ - -/** - * @brief Browse sync result action - */ -static esp_err_t _mdns_sync_browse_action(mdns_action_type_t type, mdns_browse_sync_t *browse_sync) -{ - mdns_action_t *action = NULL; - - action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - if (!action) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - - action->type = type; - action->data.browse_sync.browse_sync = browse_sync; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -/** - * @brief Browse action - */ -static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse) -{ - mdns_action_t *action = NULL; - - action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); - - if (!action) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - - action->type = type; - action->data.browse_add.browse = browse; - if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { - mdns_mem_free(action); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -/** - * @brief Free a browse item (Not free the list). - */ -static void _mdns_browse_item_free(mdns_browse_t *browse) -{ - mdns_mem_free(browse->service); - mdns_mem_free(browse->proto); - if (browse->result) { - _mdns_query_results_free(browse->result); - } - mdns_mem_free(browse); -} - -/** - * @brief Allocate new browse structure - */ -static mdns_browse_t *_mdns_browse_init(const char *service, const char *proto, mdns_browse_notify_t notifier) -{ - mdns_browse_t *browse = (mdns_browse_t *)mdns_mem_malloc(sizeof(mdns_browse_t)); - - if (!browse) { - HOOK_MALLOC_FAILED; - return NULL; - } - memset(browse, 0, sizeof(mdns_browse_t)); - - browse->state = BROWSE_INIT; - if (!_str_null_or_empty(service)) { - browse->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); - if (!browse->service) { - _mdns_browse_item_free(browse); - return NULL; - } - } - - if (!_str_null_or_empty(proto)) { - browse->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); - if (!browse->proto) { - _mdns_browse_item_free(browse); - return NULL; - } - } - - browse->notifier = notifier; - return browse; -} - -mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier) -{ - mdns_browse_t *browse = NULL; - - if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return NULL; - } - - browse = _mdns_browse_init(service, proto, notifier); - if (!browse) { - return NULL; - } - - if (_mdns_send_browse_action(ACTION_BROWSE_ADD, browse)) { - _mdns_browse_item_free(browse); - return NULL; - } - - return browse; -} - -esp_err_t mdns_browse_delete(const char *service, const char *proto) -{ - mdns_browse_t *browse = NULL; - - if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto)) { - return ESP_FAIL; - } - - browse = _mdns_browse_init(service, proto, NULL); - if (!browse) { - return ESP_ERR_NO_MEM; - } - - if (_mdns_send_browse_action(ACTION_BROWSE_END, browse)) { - _mdns_browse_item_free(browse); - return ESP_ERR_NO_MEM; - } - return ESP_OK; -} - -/** - * @brief Mark browse as finished, remove and free it from browse chain - */ -static void _mdns_browse_finish(mdns_browse_t *browse) -{ - browse->state = BROWSE_OFF; - mdns_browse_t *b = _mdns_server->browse; - mdns_browse_t *target_free = NULL; - while (b) { - if (strlen(b->service) == strlen(browse->service) && memcmp(b->service, browse->service, strlen(b->service)) == 0 && - strlen(b->proto) == strlen(browse->proto) && memcmp(b->proto, browse->proto, strlen(b->proto)) == 0) { - target_free = b; - b = b->next; - queueDetach(mdns_browse_t, _mdns_server->browse, target_free); - _mdns_browse_item_free(target_free); - } else { - b = b->next; - } - } - _mdns_browse_item_free(browse); -} - -/** - * @brief Add new browse to the browse chain - */ -static void _mdns_browse_add(mdns_browse_t *browse) -{ - browse->state = BROWSE_RUNNING; - mdns_browse_t *queue = _mdns_server->browse; - bool found = false; - // looking for this browse in active browses - while (queue) { - if (strlen(queue->service) == strlen(browse->service) && memcmp(queue->service, browse->service, strlen(queue->service)) == 0 && - strlen(queue->proto) == strlen(browse->proto) && memcmp(queue->proto, browse->proto, strlen(queue->proto)) == 0) { - found = true; - break; - } - queue = queue->next; - } - if (!found) { - browse->next = _mdns_server->browse; - _mdns_server->browse = browse; - } - for (uint8_t interface_idx = 0; interface_idx < MDNS_MAX_INTERFACES; interface_idx++) { - _mdns_browse_send(browse, (mdns_if_t)interface_idx); - } - if (found) { - _mdns_browse_item_free(browse); - } -} - -/** - * @brief Send PTR query packet to all available interfaces for browsing. - */ -static void _mdns_browse_send(mdns_browse_t *browse, mdns_if_t interface) -{ - // Using search once for sending the PTR query - mdns_search_once_t search = {0}; - - search.instance = NULL; - search.service = browse->service; - search.proto = browse->proto; - search.type = MDNS_TYPE_PTR; - search.unicast = false; - search.result = NULL; - search.next = NULL; - - for (uint8_t protocol_idx = 0; protocol_idx < MDNS_IP_PROTOCOL_MAX; protocol_idx++) { - _mdns_search_send_pcb(&search, interface, (mdns_ip_protocol_t)protocol_idx); - } -} - -/** - * @brief Add result to browse, only add when the result is a new one. - */ -static esp_err_t _mdns_add_browse_result(mdns_browse_sync_t *sync_browse, mdns_result_t *r) -{ - mdns_browse_result_sync_t *sync_r = sync_browse->sync_result; - while (sync_r) { - if (sync_r->result == r) { - break; - } - sync_r = sync_r->next; - } - if (!sync_r) { - // Do not find, need to add the result to the list - mdns_browse_result_sync_t *new = (mdns_browse_result_sync_t *)mdns_mem_malloc(sizeof(mdns_browse_result_sync_t)); - - if (!new) { - HOOK_MALLOC_FAILED; - return ESP_ERR_NO_MEM; - } - new->result = r; - new->next = sync_browse->sync_result; - sync_browse->sync_result = new; - } - return ESP_OK; -} - -/** - * @brief Called from parser to add A/AAAA data to search result - */ -static void _mdns_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip, - mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse) -{ - if (out_sync_browse->browse == NULL) { - return; - } else { - if (out_sync_browse->browse != browse) { - return; - } - } - mdns_result_t *r = NULL; - mdns_ip_addr_t *r_a = NULL; - if (browse) { - r = browse->result; - while (r) { - if (r->ip_protocol == ip_protocol) { - // Find the target result in browse result. - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { - r_a = r->addr; - // Check if the address has already added in result. - while (r_a) { -#ifdef CONFIG_LWIP_IPV4 - if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V4 && r_a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) { - break; - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(r_a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) { - break; - } -#endif - r_a = r_a->next; - } - if (!r_a) { - // The current IP is a new one, add it to the link list. - mdns_ip_addr_t *a = NULL; - a = _mdns_result_addr_create_ip(ip); - if (!a) { - return; - } - a->next = r->addr; - r->addr = a; - if (r->ttl != ttl) { - if (r->ttl == 0) { - r->ttl = ttl; - } else { - _mdns_result_update_ttl(r, ttl); - } - } - if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) { - return; - } - break; - } - } - } - r = r->next; - } - } - return; -} - -/** - * @brief Called from packet parser to find matching running search - */ -static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - // For browse, we only care about the SRV, TXT, A and AAAA - if (type != MDNS_TYPE_SRV && type != MDNS_TYPE_A && type != MDNS_TYPE_AAAA && type != MDNS_TYPE_TXT) { - return NULL; - } - mdns_result_t *r = NULL; - while (b) { - if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { - if (strcasecmp(name->service, b->service) - || strcasecmp(name->proto, b->proto)) { - b = b->next; - continue; - } - return b; - } else if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { - r = b->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { - return b; - } - r = r->next; - } - b = b->next; - continue; - } - } - return b; -} - -static bool is_txt_item_in_list(mdns_txt_item_t txt, uint8_t txt_value_len, mdns_txt_item_t *txt_list, uint8_t *txt_value_len_list, size_t txt_count) -{ - for (size_t i = 0; i < txt_count; i++) { - if (strcmp(txt.key, txt_list[i].key) == 0) { - if (txt_value_len == txt_value_len_list[i] && memcmp(txt.value, txt_list[i].value, txt_value_len) == 0) { - return true; - } else { - // The key value is unique, so there is no need to continue searching. - return false; - } - } - } - return false; -} - -/** - * @brief Called from parser to add TXT data to search result - */ -static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto, - mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, - uint32_t ttl, mdns_browse_sync_t *out_sync_browse) -{ - if (out_sync_browse->browse == NULL) { - return; - } else { - if (out_sync_browse->browse != browse) { - return; - } - } - mdns_result_t *r = browse->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && - !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) && - !_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) && - !_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) { - bool should_update = false; - if (r->txt) { - // Check if txt changed - if (txt_count != r->txt_count) { - should_update = true; - } else { - for (size_t txt_index = 0; txt_index < txt_count; txt_index++) { - if (!is_txt_item_in_list(txt[txt_index], txt_value_len[txt_index], r->txt, r->txt_value_len, r->txt_count)) { - should_update = true; - break; - } - } - } - // If the result has a previous txt entry, we delete it and re-add. - for (size_t i = 0; i < r->txt_count; i++) { - mdns_mem_free((char *)(r->txt[i].key)); - mdns_mem_free((char *)(r->txt[i].value)); - } - mdns_mem_free(r->txt); - mdns_mem_free(r->txt_value_len); - } - r->txt = txt; - r->txt_value_len = txt_value_len; - r->txt_count = txt_count; - if (r->ttl != ttl) { - uint32_t previous_ttl = r->ttl; - if (r->ttl == 0) { - r->ttl = ttl; - } else { - _mdns_result_update_ttl(r, ttl); - } - if (previous_ttl != r->ttl) { - should_update = true; - } - } - if (should_update) { - if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) { - return; - } - } - return; - } - r = r->next; - } - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - goto free_txt; - } - memset(r, 0, sizeof(mdns_result_t)); - r->instance_name = mdns_mem_strdup(instance); - r->service_type = mdns_mem_strdup(service); - r->proto = mdns_mem_strdup(proto); - if (!r->instance_name || !r->service_type || !r->proto) { - mdns_mem_free(r->instance_name); - mdns_mem_free(r->service_type); - mdns_mem_free(r->proto); - mdns_mem_free(r); - return; - } - r->txt = txt; - r->txt_value_len = txt_value_len; - r->txt_count = txt_count; - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->ttl = ttl; - r->next = browse->result; - browse->result = r; - _mdns_add_browse_result(out_sync_browse, r); - return; - -free_txt: - for (size_t i = 0; i < txt_count; i++) { - mdns_mem_free((char *)(txt[i].key)); - mdns_mem_free((char *)(txt[i].value)); - } - mdns_mem_free(txt); - mdns_mem_free(txt_value_len); - return; -} - -static esp_err_t _mdns_copy_address_in_previous_result(mdns_result_t *result_list, mdns_result_t *r) -{ - while (result_list) { - if (!_str_null_or_empty(result_list->hostname) && !_str_null_or_empty(r->hostname) && !strcasecmp(result_list->hostname, r->hostname) && - result_list->ip_protocol == r->ip_protocol && result_list->addr && !r->addr) { - // If there is a same hostname in previous result, we need to copy the address here. - r->addr = copy_address_list(result_list->addr); - if (!r->addr) { - return ESP_ERR_NO_MEM; - } - break; - } else { - result_list = result_list->next; - } - } - return ESP_OK; -} - -/** - * @brief Called from parser to add SRV data to search result - */ -static void _mdns_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto, - uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse) -{ - if (out_sync_browse->browse == NULL) { - return; - } else { - if (out_sync_browse->browse != browse) { - return; - } - } - mdns_result_t *r = browse->result; - while (r) { - if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && - !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) && - !_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) && - !_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) { - if (_str_null_or_empty(r->hostname) || strcasecmp(hostname, r->hostname)) { - r->hostname = mdns_mem_strdup(hostname); - r->port = port; - if (!r->hostname) { - HOOK_MALLOC_FAILED; - return; - } - if (!r->addr) { - esp_err_t err = _mdns_copy_address_in_previous_result(browse->result, r); - if (err == ESP_ERR_NO_MEM) { - return; - } - } - if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) { - return; - } - } - if (r->ttl != ttl) { - uint32_t previous_ttl = r->ttl; - if (r->ttl == 0) { - r->ttl = ttl; - } else { - _mdns_result_update_ttl(r, ttl); - } - if (previous_ttl != r->ttl) { - if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) { - return; - } - } - } - return; - } - r = r->next; - } - r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); - if (!r) { - HOOK_MALLOC_FAILED; - return; - } - - memset(r, 0, sizeof(mdns_result_t)); - r->hostname = mdns_mem_strdup(hostname); - r->instance_name = mdns_mem_strdup(instance); - r->service_type = mdns_mem_strdup(service); - r->proto = mdns_mem_strdup(proto); - if (!r->hostname || !r->instance_name || !r->service_type || !r->proto) { - HOOK_MALLOC_FAILED; - mdns_mem_free(r->hostname); - mdns_mem_free(r->instance_name); - mdns_mem_free(r->service_type); - mdns_mem_free(r->proto); - mdns_mem_free(r); - return; - } - r->port = port; - r->esp_netif = _mdns_get_esp_netif(tcpip_if); - r->ip_protocol = ip_protocol; - r->ttl = ttl; - r->next = browse->result; - browse->result = r; - _mdns_add_browse_result(out_sync_browse, r); - return; -} - -static void _mdns_browse_sync(mdns_browse_sync_t *browse_sync) -{ - mdns_browse_t *browse = browse_sync->browse; - mdns_browse_result_sync_t *sync_result = browse_sync->sync_result; - while (sync_result) { - mdns_result_t *result = sync_result->result; -#ifdef MDNS_ENABLE_DEBUG - debug_printf_browse_result(result, browse_sync->browse); -#endif - browse->notifier(result); - if (result->ttl == 0) { - queueDetach(mdns_result_t, browse->result, result); - // Just free current result - result->next = NULL; - mdns_query_results_free(result); - } - sync_result = sync_result->next; - } -} - -#ifdef MDNS_ENABLE_DEBUG -void _debug_printf_result(mdns_result_t *r_t) -{ - mdns_ip_addr_t *r_a = NULL; - int addr_count = 0; - _mdns_dbg_printf("result esp_netif: %p\n", r_t->esp_netif); - _mdns_dbg_printf("result ip_protocol: %d\n", r_t->ip_protocol); - _mdns_dbg_printf("result hostname: %s\n", _str_null_or_empty(r_t->hostname) ? "NULL" : r_t->hostname); - _mdns_dbg_printf("result instance_name: %s\n", _str_null_or_empty(r_t->instance_name) ? "NULL" : r_t->instance_name); - _mdns_dbg_printf("result service_type: %s\n", _str_null_or_empty(r_t->service_type) ? "NULL" : r_t->service_type); - _mdns_dbg_printf("result proto: %s\n", _str_null_or_empty(r_t->proto) ? "NULL" : r_t->proto); - _mdns_dbg_printf("result port: %d\n", r_t->port); - _mdns_dbg_printf("result ttl: %" PRIu32 "\n", r_t->ttl); - for (int i = 0; i < r_t->txt_count; i++) { - _mdns_dbg_printf("result txt item%d, key: %s, value: %s\n", i, r_t->txt[i].key, r_t->txt[i].value); - } - r_a = r_t->addr; - while (r_a) { -#ifdef CONFIG_LWIP_IPV4 - if (r_a->addr.type == ESP_IPADDR_TYPE_V4) { - _mdns_dbg_printf("Addr%d: " IPSTR "\n", addr_count++, IP2STR(&r_a->addr.u_addr.ip4)); - } -#endif -#ifdef CONFIG_LWIP_IPV6 - if (r_a->addr.type == ESP_IPADDR_TYPE_V6) { - _mdns_dbg_printf("Addr%d: " IPV6STR "\n", addr_count++, IPV62STR(r_a->addr.u_addr.ip6)); - } -#endif - r_a = r_a->next; - } -} - -static void debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t) -{ - _mdns_dbg_printf("----------------sync browse %s.%s result---------------\n", b_t->service, b_t->proto); - _mdns_dbg_printf("browse pointer: %p\n", b_t); - _debug_printf_result(r_t); -} - -static void debug_printf_browse_result_all(mdns_result_t *r_t) -{ - int count = 0; - while (r_t) { - _mdns_dbg_printf("----------------result %d---------------\n", count++); - _debug_printf_result(r_t); - r_t = r_t->next; - } -} -#endif // MDNS_ENABLE_DEBUG diff --git a/components/mdns/mdns_browser.c b/components/mdns/mdns_browser.c new file mode 100644 index 0000000000..ba9f64f2d6 --- /dev/null +++ b/components/mdns/mdns_browser.c @@ -0,0 +1,656 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "mdns_browser.h" +#include "mdns_mem_caps.h" +#include "mdns_debug.h" +#include "mdns_utils.h" +#include "mdns_querier.h" +#include "mdns_responder.h" +#include "mdns_netif.h" +#include "mdns_service.h" +#include "esp_log.h" + +static const char *TAG = "mdns_browser"; + +static mdns_browse_t *s_browse; + +/** + * @brief Browse action + */ +static esp_err_t send_browse_action(mdns_action_type_t type, mdns_browse_t *browse) +{ + mdns_action_t *action = NULL; + + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + + action->type = type; + action->data.browse_add.browse = browse; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +/** + * @brief Free a browse item (Not free the list). + */ +static void browse_item_free(mdns_browse_t *browse) +{ + mdns_mem_free(browse->service); + mdns_mem_free(browse->proto); + if (browse->result) { + mdns_priv_query_results_free(browse->result); + } + mdns_mem_free(browse); +} + +static void browse_sync(mdns_browse_sync_t *browse_sync) +{ + mdns_browse_t *browse = browse_sync->browse; + mdns_browse_result_sync_t *sync_result = browse_sync->sync_result; + while (sync_result) { + mdns_result_t *result = sync_result->result; + DBG_BROWSE_RESULTS(result, browse_sync->browse); + browse->notifier(result); + if (result->ttl == 0) { + queueDetach(mdns_result_t, browse->result, result); + // Just free current result + result->next = NULL; + mdns_query_results_free(result); + } + sync_result = sync_result->next; + } +} + +/** + * @brief Send PTR query packet to all available interfaces for browsing. + */ +static void browse_send(mdns_browse_t *browse, mdns_if_t interface) +{ + // Using search once for sending the PTR query + mdns_search_once_t search = {0}; + + search.instance = NULL; + search.service = browse->service; + search.proto = browse->proto; + search.type = MDNS_TYPE_PTR; + search.unicast = false; + search.result = NULL; + search.next = NULL; + + for (uint8_t protocol_idx = 0; protocol_idx < MDNS_IP_PROTOCOL_MAX; protocol_idx++) { + mdns_priv_query_send(&search, interface, (mdns_ip_protocol_t) protocol_idx); + } +} + +void mdns_priv_browse_send_all(mdns_if_t mdns_if) +{ + mdns_browse_t *browse = s_browse; + while (browse) { + browse_send(browse, mdns_if); + browse = browse->next; + } +} + +void mdns_priv_browse_free(void) +{ + while (s_browse) { + mdns_browse_t *b = s_browse; + s_browse = s_browse->next; + browse_item_free(b); + } +} + +/** + * @brief Mark browse as finished, remove and free it from browse chain + */ +static void browse_finish(mdns_browse_t *browse) +{ + browse->state = BROWSE_OFF; + mdns_browse_t *b = s_browse; + mdns_browse_t *target_free = NULL; + while (b) { + if (strlen(b->service) == strlen(browse->service) && memcmp(b->service, browse->service, strlen(b->service)) == 0 && + strlen(b->proto) == strlen(browse->proto) && memcmp(b->proto, browse->proto, strlen(b->proto)) == 0) { + target_free = b; + b = b->next; + queueDetach(mdns_browse_t, s_browse, target_free); + browse_item_free(target_free); + } else { + b = b->next; + } + } + browse_item_free(browse); +} + +/** + * @brief Allocate new browse structure + */ +static mdns_browse_t *browse_init(const char *service, const char *proto, mdns_browse_notify_t notifier) +{ + mdns_browse_t *browse = (mdns_browse_t *)mdns_mem_malloc(sizeof(mdns_browse_t)); + + if (!browse) { + HOOK_MALLOC_FAILED; + return NULL; + } + memset(browse, 0, sizeof(mdns_browse_t)); + + browse->state = BROWSE_INIT; + if (!mdns_utils_str_null_or_empty(service)) { + browse->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); + if (!browse->service) { + browse_item_free(browse); + HOOK_MALLOC_FAILED; + return NULL; + } + } + + if (!mdns_utils_str_null_or_empty(proto)) { + browse->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); + if (!browse->proto) { + browse_item_free(browse); + return NULL; + } + } + + browse->notifier = notifier; + return browse; +} + +/** + * @brief Add new browse to the browse chain + */ +static void browse_add(mdns_browse_t *browse) +{ + browse->state = BROWSE_RUNNING; + mdns_browse_t *queue = s_browse; + bool found = false; + // looking for this browse in active browses + while (queue) { + if (strlen(queue->service) == strlen(browse->service) && memcmp(queue->service, browse->service, strlen(queue->service)) == 0 && + strlen(queue->proto) == strlen(browse->proto) && memcmp(queue->proto, browse->proto, strlen(queue->proto)) == 0) { + found = true; + break; + } + queue = queue->next; + } + if (!found) { + browse->next = s_browse; + s_browse = browse; + } + for (uint8_t interface_idx = 0; interface_idx < MDNS_MAX_INTERFACES; interface_idx++) { + browse_send(browse, (mdns_if_t) interface_idx); + } + if (found) { + browse_item_free(browse); + } +} + +/** + * @brief Called from packet parser to find matching running search + */ +mdns_browse_t *mdns_priv_browse_find(mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_browse_t *b = s_browse; + // For browse, we only care about the SRV, TXT, A and AAAA + if (type != MDNS_TYPE_SRV && type != MDNS_TYPE_A && type != MDNS_TYPE_AAAA && type != MDNS_TYPE_TXT) { + return NULL; + } + mdns_result_t *r = NULL; + while (b) { + if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { + if (strcasecmp(name->service, b->service) + || strcasecmp(name->proto, b->proto)) { + b = b->next; + continue; + } + return b; + } else if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { + r = b->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { + return b; + } + r = r->next; + } + b = b->next; + continue; + } + } + return b; +} + +static void sync_browse_result_link_free(mdns_browse_sync_t *browse_sync) +{ + mdns_browse_result_sync_t *current = browse_sync->sync_result; + mdns_browse_result_sync_t *need_free; + while (current) { + need_free = current; + current = current->next; + mdns_mem_free(need_free); + } + mdns_mem_free(browse_sync); +} + +void mdns_priv_browse_action(mdns_action_t *action, mdns_action_subtype_t type) +{ + if (type == ACTION_RUN) { + switch (action->type) { + case ACTION_BROWSE_ADD: + browse_add(action->data.browse_add.browse); + break; + case ACTION_BROWSE_SYNC: + browse_sync(action->data.browse_sync.browse_sync); + sync_browse_result_link_free(action->data.browse_sync.browse_sync); + break; + case ACTION_BROWSE_END: + browse_finish(action->data.browse_add.browse); + break; + default: + abort(); + } + return; + } + if (type == ACTION_CLEANUP) { + switch (action->type) { + case ACTION_BROWSE_ADD: + //fallthrough + case ACTION_BROWSE_END: + browse_item_free(action->data.browse_add.browse); + break; + case ACTION_BROWSE_SYNC: + sync_browse_result_link_free(action->data.browse_sync.browse_sync); + break; + default: + abort(); + } + return; + } + +} + +/** + * @brief Add result to browse, only add when the result is a new one. + */ +static esp_err_t add_browse_result(mdns_browse_sync_t *sync_browse, mdns_result_t *r) +{ + mdns_browse_result_sync_t *sync_r = sync_browse->sync_result; + while (sync_r) { + if (sync_r->result == r) { + break; + } + sync_r = sync_r->next; + } + if (!sync_r) { + // Do not find, need to add the result to the list + mdns_browse_result_sync_t *new = (mdns_browse_result_sync_t *)mdns_mem_malloc(sizeof(mdns_browse_result_sync_t)); + + if (!new) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + new->result = r; + new->next = sync_browse->sync_result; + sync_browse->sync_result = new; + } + return ESP_OK; +} + +/** + * @brief Called from parser to add A/AAAA data to search result + */ +void mdns_priv_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse) +{ + if (out_sync_browse->browse == NULL) { + return; + } else { + if (out_sync_browse->browse != browse) { + return; + } + } + mdns_result_t *r = NULL; + mdns_ip_addr_t *r_a = NULL; + if (browse) { + r = browse->result; + while (r) { + if (r->ip_protocol == ip_protocol) { + // Find the target result in browse result. + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { + r_a = r->addr; + // Check if the address has already added in result. + while (r_a) { +#ifdef CONFIG_LWIP_IPV4 + if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V4 && r_a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) { + break; + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (r_a->addr.type == ip->type && r_a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(r_a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) { + break; + } +#endif + r_a = r_a->next; + } + if (!r_a) { + // The current IP is a new one, add it to the link list. + mdns_ip_addr_t *a = NULL; + a = mdns_priv_result_addr_create_ip(ip); + if (!a) { + return; + } + a->next = r->addr; + r->addr = a; + if (r->ttl != ttl) { + if (r->ttl == 0) { + r->ttl = ttl; + } else { + mdns_priv_query_update_result_ttl(r, ttl); + } + } + if (add_browse_result(out_sync_browse, r) != ESP_OK) { + return; + } + break; + } + } + } + r = r->next; + } + } +} + +static bool is_txt_item_in_list(mdns_txt_item_t txt, uint8_t txt_value_len, mdns_txt_item_t *txt_list, uint8_t *txt_value_len_list, size_t txt_count) +{ + for (size_t i = 0; i < txt_count; i++) { + if (strcmp(txt.key, txt_list[i].key) == 0) { + if (txt_value_len == txt_value_len_list[i] && memcmp(txt.value, txt_list[i].value, txt_value_len) == 0) { + return true; + } else { + // The key value is unique, so there is no need to continue searching. + return false; + } + } + } + return false; +} + +/** + * @brief Called from parser to add TXT data to search result + */ +void mdns_priv_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto, + mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, + uint32_t ttl, mdns_browse_sync_t *out_sync_browse) +{ + if (out_sync_browse->browse == NULL) { + return; + } else { + if (out_sync_browse->browse != browse) { + return; + } + } + mdns_result_t *r = browse->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && + !mdns_utils_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) && + !mdns_utils_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) && + !mdns_utils_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) { + bool should_update = false; + if (r->txt) { + // Check if txt changed + if (txt_count != r->txt_count) { + should_update = true; + } else { + for (size_t txt_index = 0; txt_index < txt_count; txt_index++) { + if (!is_txt_item_in_list(txt[txt_index], txt_value_len[txt_index], r->txt, r->txt_value_len, r->txt_count)) { + should_update = true; + break; + } + } + } + // If the result has a previous txt entry, we delete it and re-add. + for (size_t i = 0; i < r->txt_count; i++) { + mdns_mem_free((char *)(r->txt[i].key)); + mdns_mem_free((char *)(r->txt[i].value)); + } + mdns_mem_free(r->txt); + mdns_mem_free(r->txt_value_len); + } + r->txt = txt; + r->txt_value_len = txt_value_len; + r->txt_count = txt_count; + if (r->ttl != ttl) { + uint32_t previous_ttl = r->ttl; + if (r->ttl == 0) { + r->ttl = ttl; + } else { + mdns_priv_query_update_result_ttl(r, ttl); + } + if (previous_ttl != r->ttl) { + should_update = true; + } + } + if (should_update) { + if (add_browse_result(out_sync_browse, r) != ESP_OK) { + return; + } + } + return; + } + r = r->next; + } + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + goto free_txt; + } + memset(r, 0, sizeof(mdns_result_t)); + r->instance_name = mdns_mem_strdup(instance); + r->service_type = mdns_mem_strdup(service); + r->proto = mdns_mem_strdup(proto); + if (!r->instance_name || !r->service_type || !r->proto) { + mdns_mem_free(r->instance_name); + mdns_mem_free(r->service_type); + mdns_mem_free(r->proto); + mdns_mem_free(r); + return; + } + r->txt = txt; + r->txt_value_len = txt_value_len; + r->txt_count = txt_count; + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->ttl = ttl; + r->next = browse->result; + browse->result = r; + add_browse_result(out_sync_browse, r); + return; + +free_txt: + for (size_t i = 0; i < txt_count; i++) { + mdns_mem_free((char *)(txt[i].key)); + mdns_mem_free((char *)(txt[i].value)); + } + mdns_mem_free(txt); + mdns_mem_free(txt_value_len); + return; +} + +static esp_err_t copy_address_in_previous_result(mdns_result_t *result_list, mdns_result_t *r) +{ + while (result_list) { + if (!mdns_utils_str_null_or_empty(result_list->hostname) && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(result_list->hostname, r->hostname) && + result_list->ip_protocol == r->ip_protocol && result_list->addr && !r->addr) { + // If there is a same hostname in previous result, we need to copy the address here. + r->addr = mdns_utils_copy_address_list(result_list->addr); + if (!r->addr) { + return ESP_ERR_NO_MEM; + } + break; + } else { + result_list = result_list->next; + } + } + return ESP_OK; +} + +/** + * @brief Called from parser to add SRV data to search result + */ +void mdns_priv_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto, + uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse) +{ + if (out_sync_browse->browse == NULL) { + return; + } else { + if (out_sync_browse->browse != browse) { + return; + } + } + mdns_result_t *r = browse->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && + !mdns_utils_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) && + !mdns_utils_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) && + !mdns_utils_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) { + if (mdns_utils_str_null_or_empty(r->hostname) || strcasecmp(hostname, r->hostname)) { + r->hostname = mdns_mem_strdup(hostname); + r->port = port; + if (!r->hostname) { + HOOK_MALLOC_FAILED; + return; + } + if (!r->addr) { + esp_err_t err = copy_address_in_previous_result(browse->result, r); + if (err == ESP_ERR_NO_MEM) { + return; + } + } + if (add_browse_result(out_sync_browse, r) != ESP_OK) { + return; + } + } + if (r->ttl != ttl) { + uint32_t previous_ttl = r->ttl; + if (r->ttl == 0) { + r->ttl = ttl; + } else { + mdns_priv_query_update_result_ttl(r, ttl); + } + if (previous_ttl != r->ttl) { + if (add_browse_result(out_sync_browse, r) != ESP_OK) { + return; + } + } + } + return; + } + r = r->next; + } + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + return; + } + + memset(r, 0, sizeof(mdns_result_t)); + r->hostname = mdns_mem_strdup(hostname); + r->instance_name = mdns_mem_strdup(instance); + r->service_type = mdns_mem_strdup(service); + r->proto = mdns_mem_strdup(proto); + if (!r->hostname || !r->instance_name || !r->service_type || !r->proto) { + HOOK_MALLOC_FAILED; + mdns_mem_free(r->hostname); + mdns_mem_free(r->instance_name); + mdns_mem_free(r->service_type); + mdns_mem_free(r->proto); + mdns_mem_free(r); + return; + } + r->port = port; + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->ttl = ttl; + r->next = browse->result; + browse->result = r; + add_browse_result(out_sync_browse, r); +} + +/** + * @brief Browse sync result + */ +esp_err_t mdns_priv_browse_sync(mdns_browse_sync_t *browse_sync) +{ + mdns_action_t *action = NULL; + + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + + action->type = ACTION_BROWSE_SYNC; + action->data.browse_sync.browse_sync = browse_sync; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +/** + * @defgroup MDNS_PUBCLIC_API + */ +mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier) +{ + mdns_browse_t *browse = NULL; + + if (mdns_priv_is_server_init() || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return NULL; + } + + browse = browse_init(service, proto, notifier); + if (!browse) { + return NULL; + } + + if (send_browse_action(ACTION_BROWSE_ADD, browse)) { + browse_item_free(browse); + return NULL; + } + + return browse; +} + +esp_err_t mdns_browse_delete(const char *service, const char *proto) +{ + mdns_browse_t *browse = NULL; + + if (!mdns_priv_is_server_init() || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_FAIL; + } + + browse = browse_init(service, proto, NULL); + if (!browse) { + return ESP_ERR_NO_MEM; + } + + if (send_browse_action(ACTION_BROWSE_END, browse)) { + browse_item_free(browse); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} diff --git a/components/mdns/mdns_debug.c b/components/mdns/mdns_debug.c new file mode 100644 index 0000000000..597d4d9390 --- /dev/null +++ b/components/mdns/mdns_debug.c @@ -0,0 +1,416 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "mdns_utils.h" + +#ifdef CONFIG_MDNS_DEBUG_USE_ESP_LOG + +#include +#include "esp_log.h" + +#define MDNS_DBG_MAX_LINE CONFIG_MDNS_DEBUG_BUFFER_SIZE + +static char s_mdns_dbg_buf[MDNS_DBG_MAX_LINE]; +static size_t s_mdns_dbg_pos = 0; + +static void mdns_dbg_puts(const char *str) +{ + ESP_LOGI("mdns", "%s", str); +} + +static inline void mdns_dbg_flush(void) +{ + if (s_mdns_dbg_pos > 0) { + s_mdns_dbg_buf[s_mdns_dbg_pos] = '\0'; + mdns_dbg_puts(s_mdns_dbg_buf); + s_mdns_dbg_pos = 0; + } +} + +static void mdns_dbg_vprintf(const char *fmt, va_list args) +{ + // Try to format directly into the buffer + int len = vsnprintf(s_mdns_dbg_buf + s_mdns_dbg_pos, + MDNS_DBG_MAX_LINE - s_mdns_dbg_pos, + fmt, args); + + if (len < 0) { + return; // Error in formatting + } + + // Check if the entire formatted string fit in the buffer + if (len < (MDNS_DBG_MAX_LINE - s_mdns_dbg_pos)) { + // If it fit, just update the position + s_mdns_dbg_pos += len; + } else { + // The formatted string was truncated because it didn't fit + // First, flush what we have (the partial string) + mdns_dbg_flush(); + + // Create a new va_list copy and try again with the full buffer + va_list args_copy; + va_copy(args_copy, args); + + // Format again with the entire buffer available + len = vsnprintf(s_mdns_dbg_buf, MDNS_DBG_MAX_LINE - 1, fmt, args_copy); + va_end(args_copy); + + if (len < 0) { + return; // Error + } + + // Check if content will be lost (true truncation) + if (len >= MDNS_DBG_MAX_LINE - 1) { + // This is when actual content will be lost - log a warning + ESP_LOGW("mdns", "Message truncated: length (%d) exceeds buffer size (%d). Consider increasing CONFIG_MDNS_DEBUG_BUFFER_SIZE.", + len, MDNS_DBG_MAX_LINE - 1); + + // Display what we could fit, then flush and return + s_mdns_dbg_pos = MDNS_DBG_MAX_LINE - 1; + mdns_dbg_flush(); + return; + } + + // If we get here, the whole message fit this time + s_mdns_dbg_pos = len; + } + + // If buffer is nearly full after this operation, flush it + if (s_mdns_dbg_pos >= MDNS_DBG_MAX_LINE - 1) { + mdns_dbg_flush(); + } +} + +static void mdns_dbg_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + mdns_dbg_vprintf(fmt, ap); + va_end(ap); +} + +#define dbg_printf(...) mdns_dbg_printf(__VA_ARGS__) +#else +#define dbg_printf(...) printf(__VA_ARGS__) +#define mdns_dbg_flush() +#endif + +void static dbg_packet(const uint8_t *data, size_t len) +{ + static mdns_name_t n; + mdns_header_t header; + const uint8_t *content = data + MDNS_HEAD_LEN; + uint64_t t = xTaskGetTickCount() * portTICK_PERIOD_MS; + mdns_name_t *name = &n; + memset(name, 0, sizeof(mdns_name_t)); + + dbg_printf("Packet[%" PRIu64 "]: ", t); + + header.id = mdns_utils_read_u16(data, MDNS_HEAD_ID_OFFSET); + header.flags = mdns_utils_read_u16(data, MDNS_HEAD_FLAGS_OFFSET); + header.questions = mdns_utils_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET); + header.answers = mdns_utils_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET); + header.servers = mdns_utils_read_u16(data, MDNS_HEAD_SERVERS_OFFSET); + header.additional = mdns_utils_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET); + + dbg_printf("%s", + (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE) ? "AUTHORITATIVE\n" : + (header.flags == MDNS_FLAGS_DISTRIBUTED) ? "DISTRIBUTED\n" : + (header.flags == 0) ? "\n" : " " + ); + if (header.flags && header.flags != MDNS_FLAGS_QR_AUTHORITATIVE) { + dbg_printf("0x%04X\n", header.flags); + } + + if (header.questions) { + uint8_t qs = header.questions; + + while (qs--) { + content = mdns_utils_parse_fqdn(data, content, name, len); + if (!content || content + MDNS_CLASS_OFFSET + 1 >= data + len) { + header.answers = 0; + header.additional = 0; + header.servers = 0; + dbg_printf("ERROR: parse header questions\n"); + break; + } + + uint16_t type = mdns_utils_read_u16(content, MDNS_TYPE_OFFSET); + uint16_t mdns_class = mdns_utils_read_u16(content, MDNS_CLASS_OFFSET); + bool unicast = !!(mdns_class & 0x8000); + mdns_class &= 0x7FFF; + content = content + 4; + + dbg_printf(" Q: "); + if (unicast) { + dbg_printf("*U* "); + } + if (type == MDNS_TYPE_PTR) { + dbg_printf("%s.%s%s.%s.%s. PTR ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_SRV) { + dbg_printf("%s.%s%s.%s.%s. SRV ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_TXT) { + dbg_printf("%s.%s%s.%s.%s. TXT ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_A) { + dbg_printf("%s.%s. A ", name->host, name->domain); + } else if (type == MDNS_TYPE_AAAA) { + dbg_printf("%s.%s. AAAA ", name->host, name->domain); + } else if (type == MDNS_TYPE_NSEC) { + dbg_printf("%s.%s%s.%s.%s. NSEC ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_ANY) { + dbg_printf("%s.%s%s.%s.%s. ANY ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain); + } else { + dbg_printf("%s.%s%s.%s.%s. %04X ", name->host, name->sub ? "_sub." : "", name->service, name->proto, name->domain, type); + } + + if (mdns_class == 0x0001) { + dbg_printf("IN"); + } else { + dbg_printf("%04X", mdns_class); + } + dbg_printf("\n"); + } + } + + if (header.answers || header.servers || header.additional) { + uint16_t recordIndex = 0; + + while (content < (data + len)) { + + content = mdns_utils_parse_fqdn(data, content, name, len); + if (!content) { + dbg_printf("ERROR: parse mdns records\n"); + break; + } + + uint16_t type = mdns_utils_read_u16(content, MDNS_TYPE_OFFSET); + uint16_t mdns_class = mdns_utils_read_u16(content, MDNS_CLASS_OFFSET); + uint32_t ttl = mdns_utils_read_u32(content, MDNS_TTL_OFFSET); + uint16_t data_len = mdns_utils_read_u16(content, MDNS_LEN_OFFSET); + const uint8_t *data_ptr = content + MDNS_DATA_OFFSET; + bool flush = !!(mdns_class & 0x8000); + mdns_class &= 0x7FFF; + + content = data_ptr + data_len; + if (content > (data + len)) { + dbg_printf("ERROR: content length overflow\n"); + break; + } + + mdns_parsed_record_type_t record_type = MDNS_ANSWER; + + if (recordIndex >= (header.answers + header.servers)) { + record_type = MDNS_EXTRA; + } else if (recordIndex >= (header.answers)) { + record_type = MDNS_NS; + } + recordIndex++; + + if (record_type == MDNS_EXTRA) { + dbg_printf(" X"); + } else if (record_type == MDNS_NS) { + dbg_printf(" S"); + } else { + dbg_printf(" A"); + } + + if (type == MDNS_TYPE_PTR) { + dbg_printf(": %s%s%s.%s.%s. PTR ", name->host, name->host[0] ? "." : "", name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_SRV) { + dbg_printf(": %s.%s.%s.%s. SRV ", name->host, name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_TXT) { + dbg_printf(": %s.%s.%s.%s. TXT ", name->host, name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_A) { + dbg_printf(": %s.%s. A ", name->host, name->domain); + } else if (type == MDNS_TYPE_AAAA) { + dbg_printf(": %s.%s. AAAA ", name->host, name->domain); + } else if (type == MDNS_TYPE_NSEC) { + dbg_printf(": %s.%s.%s.%s. NSEC ", name->host, name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_ANY) { + dbg_printf(": %s.%s.%s.%s. ANY ", name->host, name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_OPT) { + dbg_printf(": . OPT "); + } else { + dbg_printf(": %s.%s.%s.%s. %04X ", name->host, name->service, name->proto, name->domain, type); + } + + if (mdns_class == 0x0001) { + dbg_printf("IN "); + } else { + dbg_printf("%04X ", mdns_class); + } + if (flush) { + dbg_printf("FLUSH "); + } + dbg_printf("%" PRIu32, ttl); + dbg_printf("[%u] ", data_len); + if (type == MDNS_TYPE_PTR) { + if (!mdns_utils_parse_fqdn(data, data_ptr, name, len)) { + dbg_printf("ERROR: parse PTR\n"); + continue; + } + dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain); + } else if (type == MDNS_TYPE_SRV) { + if (!mdns_utils_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) { + dbg_printf("ERROR: parse SRV\n"); + continue; + } + uint16_t priority = mdns_utils_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET); + uint16_t weight = mdns_utils_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET); + uint16_t port = mdns_utils_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET); + dbg_printf("%u %u %u %s.%s.\n", priority, weight, port, name->host, name->domain); + } else if (type == MDNS_TYPE_TXT) { + uint16_t i = 0, y; + while (i < data_len) { + uint8_t partLen = data_ptr[i++]; + if ((i + partLen) > data_len) { + dbg_printf("ERROR: parse TXT\n"); + break; + } + char txt[partLen + 1]; + for (y = 0; y < partLen; y++) { + char d = data_ptr[i++]; + txt[y] = d; + } + txt[partLen] = 0; + dbg_printf("%s", txt); + if (i < data_len) { + dbg_printf("; "); + } + } + dbg_printf("\n"); + } else if (type == MDNS_TYPE_AAAA) { + esp_ip6_addr_t ip6; + memcpy(&ip6, data_ptr, sizeof(esp_ip6_addr_t)); + dbg_printf(IPV6STR "\n", IPV62STR(ip6)); + } else if (type == MDNS_TYPE_A) { + esp_ip4_addr_t ip; + memcpy(&ip, data_ptr, sizeof(esp_ip4_addr_t)); + dbg_printf(IPSTR "\n", IP2STR(&ip)); + } else if (type == MDNS_TYPE_NSEC) { + const uint8_t *old_ptr = data_ptr; + const uint8_t *new_ptr = mdns_utils_parse_fqdn(data, data_ptr, name, len); + if (new_ptr) { + dbg_printf("%s.%s.%s.%s. ", name->host, name->service, name->proto, name->domain); + size_t diff = new_ptr - old_ptr; + data_len -= diff; + data_ptr = new_ptr; + } + size_t i; + for (i = 0; i < data_len; i++) { + dbg_printf(" %02x", data_ptr[i]); + } + dbg_printf("\n"); + } else if (type == MDNS_TYPE_OPT) { + uint16_t opCode = mdns_utils_read_u16(data_ptr, 0); + uint16_t opLen = mdns_utils_read_u16(data_ptr, 2); + dbg_printf(" Code: %04x Data[%u]:", opCode, opLen); + size_t i; + for (i = 4; i < data_len; i++) { + dbg_printf(" %02x", data_ptr[i]); + } + dbg_printf("\n"); + } else { + size_t i; + for (i = 0; i < data_len; i++) { + dbg_printf(" %02x", data_ptr[i]); + } + dbg_printf("\n"); + } + } + } + mdns_dbg_flush(); +} + +void mdns_debug_tx_packet(mdns_tx_packet_t *p, uint8_t packet[MDNS_MAX_PACKET_SIZE], uint16_t index) +{ + dbg_printf("\nTX[%lu][%lu]: ", (unsigned long)p->tcpip_if, (unsigned long)p->ip_protocol); +#ifdef CONFIG_LWIP_IPV4 + if (p->dst.type == ESP_IPADDR_TYPE_V4) { + dbg_printf("To: " IPSTR ":%u, ", IP2STR(&p->dst.u_addr.ip4), p->port); + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (p->dst.type == ESP_IPADDR_TYPE_V6) { + dbg_printf("To: " IPV6STR ":%u, ", IPV62STR(p->dst.u_addr.ip6), p->port); + } +#endif + dbg_packet(packet, index); + mdns_dbg_flush(); +} + +void mdns_debug_rx_packet(mdns_rx_packet_t *packet, const uint8_t* data, uint16_t len) +{ + dbg_printf("\nRX[%lu][%lu]: ", (unsigned long)packet->tcpip_if, (unsigned long)packet->ip_protocol); +#ifdef CONFIG_LWIP_IPV4 + if (packet->src.type == ESP_IPADDR_TYPE_V4) { + dbg_printf("From: " IPSTR ":%u, To: " IPSTR ", ", IP2STR(&packet->src.u_addr.ip4), packet->src_port, IP2STR(&packet->dest.u_addr.ip4)); + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (packet->src.type == ESP_IPADDR_TYPE_V6) { + dbg_printf("From: " IPV6STR ":%u, To: " IPV6STR ", ", IPV62STR(packet->src.u_addr.ip6), packet->src_port, IPV62STR(packet->dest.u_addr.ip6)); + } +#endif + dbg_packet(data, len); + mdns_dbg_flush(); +} + +static void dbg_printf_result(mdns_result_t *r_t) +{ + mdns_ip_addr_t *r_a = NULL; + int addr_count = 0; + dbg_printf("result esp_netif: %p\n", r_t->esp_netif); + dbg_printf("result ip_protocol: %d\n", r_t->ip_protocol); + dbg_printf("result hostname: %s\n", mdns_utils_str_null_or_empty(r_t->hostname) ? "NULL" : r_t->hostname); + dbg_printf("result instance_name: %s\n", mdns_utils_str_null_or_empty(r_t->instance_name) ? "NULL" : r_t->instance_name); + dbg_printf("result service_type: %s\n", mdns_utils_str_null_or_empty(r_t->service_type) ? "NULL" : r_t->service_type); + dbg_printf("result proto: %s\n", mdns_utils_str_null_or_empty(r_t->proto) ? "NULL" : r_t->proto); + dbg_printf("result port: %d\n", r_t->port); + dbg_printf("result ttl: %" PRIu32 "\n", r_t->ttl); + for (int i = 0; i < r_t->txt_count; i++) { + dbg_printf("result txt item%d, key: %s, value: %s\n", i, r_t->txt[i].key, r_t->txt[i].value); + } + r_a = r_t->addr; + while (r_a) { +#ifdef CONFIG_LWIP_IPV4 + if (r_a->addr.type == ESP_IPADDR_TYPE_V4) { + dbg_printf("Addr%d: " IPSTR "\n", addr_count++, IP2STR(&r_a->addr.u_addr.ip4)); + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (r_a->addr.type == ESP_IPADDR_TYPE_V6) { + dbg_printf("Addr%d: " IPV6STR "\n", addr_count++, IPV62STR(r_a->addr.u_addr.ip6)); + } +#endif + r_a = r_a->next; + } + mdns_dbg_flush(); +} + +void mdns_debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t) +{ + dbg_printf("----------------sync browse %s.%s result---------------\n", b_t->service, b_t->proto); + dbg_printf("browse pointer: %p\n", b_t); + dbg_printf_result(r_t); + mdns_dbg_flush(); +} + +void mdns_debug_printf_browse_result_all(mdns_result_t *r_t) +{ + int count = 0; + while (r_t) { + dbg_printf("----------------result %d---------------\n", count++); + dbg_printf_result(r_t); + r_t = r_t->next; + } + mdns_dbg_flush(); +} diff --git a/components/mdns/mdns_diagram.md b/components/mdns/mdns_diagram.md new file mode 100644 index 0000000000..b17797df97 --- /dev/null +++ b/components/mdns/mdns_diagram.md @@ -0,0 +1,51 @@ +```mermaid +graph TB + %% Housekeeping modules at the top + subgraph Housekeeping [Support Modules] + Service[mdns_service.c] + Utils[mdns_utils.c] + MemCaps[mdns_mem_caps.c] + Debug[mdns_debug.c] + end + + %% Switch to LR direction for the main flow + subgraph MainFlow [Main Data Flow] + direction LR + + %% Network on left side + Network((Network)) <--> Networking + + %% Networking layer + subgraph Networking [Networking Layer] + LWIP[mdns_networking_lwip.c] + Socket[mdns_networking_socket.c] + end + + %% Traffic flow + Networking --> |Incoming| Receive[mdns_receive.c] + Send[mdns_send.c] --> |Outgoing| Networking + + %% Core MDNS components + Receive --> Responder[mdns_responder.c] + Receive --> Browser[mdns_browser.c] + Receive --> Querier[mdns_querier.c] + + Responder --> Send + Browser --> Send + Querier --> Send + + PCB[mdns_pcb.c] --> Send + NetIF[mdns_netif.c] + + %% Users on the right side, aligned vertically + Responder --> Advertise((User: Advertising)) + Querier --> Search((User: Searching)) + Browser --> Browse((User: Browsing)) + end + + %% Style user nodes + style Advertise fill:#f9f,stroke:#333,stroke-width:2px + style Search fill:#f9f,stroke:#333,stroke-width:2px + style Browse fill:#f9f,stroke:#333,stroke-width:2px + style Housekeeping fill:#e6f7ff,stroke:#333,stroke-width:1px +``` diff --git a/components/mdns/mdns_netif.c b/components/mdns/mdns_netif.c new file mode 100644 index 0000000000..116fd43530 --- /dev/null +++ b/components/mdns/mdns_netif.c @@ -0,0 +1,432 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "esp_event.h" +#include "mdns.h" +#include "mdns_private.h" +#include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_debug.h" +#include "mdns_browser.h" +#include "mdns_netif.h" +#include "mdns_pcb.h" +#include "mdns_responder.h" +#include "mdns_service.h" + +static const char *TAG = "mdns_netif"; + +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH +#include "esp_eth.h" +#endif + +#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0) +#define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED +#else +#define MDNS_ESP_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED +#endif + +#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) +#include "esp_wifi.h" +#endif + +typedef enum { + MDNS_IF_STA = 0, + MDNS_IF_AP = 1, + MDNS_IF_ETH = 2, +} mdns_predef_if_t; + +typedef struct mdns_interfaces mdns_interfaces_t; + +struct mdns_interfaces { + const bool predefined; + esp_netif_t *netif; + const mdns_predef_if_t predef_if; + mdns_if_t duplicate; +}; + +/* + * @brief Internal collection of mdns supported interfaces + * + */ +static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = { +#if CONFIG_MDNS_PREDEF_NETIF_STA + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_MAX_INTERFACES }, +#endif +#if CONFIG_MDNS_PREDEF_NETIF_AP + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_MAX_INTERFACES }, +#endif +#if CONFIG_MDNS_PREDEF_NETIF_ETH + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_MAX_INTERFACES }, +#endif +}; + +/** + * @brief Helper to get either ETH or STA if the other is provided + * Used when two interfaces are on the same subnet + */ +mdns_if_t mdns_priv_netif_get_other_interface(mdns_if_t tcpip_if) +{ + if (tcpip_if < MDNS_MAX_INTERFACES) { + return s_esp_netifs[tcpip_if].duplicate; + } + return MDNS_MAX_INTERFACES; +} + +/** + * @brief Convert Predefined interface to the netif id from the internal netif list + * @param predef_if Predefined interface enum + * @return Ordinal number of internal list of mdns network interface. + * Returns MDNS_MAX_INTERFACES if the predefined interface wasn't found in the list + */ +static mdns_if_t mdns_if_from_preset(mdns_predef_if_t predef_if) +{ + for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) { + if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) { + return i; + } + } + return MDNS_MAX_INTERFACES; +} + +/** + * @brief Convert Predefined interface to esp-netif handle + * @param predef_if Predefined interface enum + * @return esp_netif pointer from system list of network interfaces + */ +static inline esp_netif_t *netif_from_preset(mdns_predef_if_t predef_if) +{ + switch (predef_if) { + case MDNS_IF_STA: + return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); + case MDNS_IF_AP: + return esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH + case MDNS_IF_ETH: + return esp_netif_get_handle_from_ifkey("ETH_DEF"); +#endif + default: + return NULL; + } +} + +esp_netif_t *mdns_priv_get_esp_netif(mdns_if_t tcpip_if) +{ + if (tcpip_if < MDNS_MAX_INTERFACES) { + if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) { + // If the local copy is NULL and this netif is predefined -> we can find it in the global netif list + s_esp_netifs[tcpip_if].netif = netif_from_preset(s_esp_netifs[tcpip_if].predef_if); + // failing to find it means that the netif is *not* available -> return NULL + } + return s_esp_netifs[tcpip_if].netif; + } + return NULL; +} + +/* + * @brief Clean internal mdns interface's pointer + */ +void mdns_priv_netif_disable(mdns_if_t tcpip_if) +{ + if (tcpip_if < MDNS_MAX_INTERFACES) { + s_esp_netifs[tcpip_if].netif = NULL; + } +} + +/* + * @brief Convert esp-netif handle to mdns if + */ +static mdns_if_t get_if_from_netif(esp_netif_t *esp_netif) +{ + for (int i = 0; i < MDNS_MAX_INTERFACES; ++i) { + // The predefined netifs in the static array are NULL when firstly calling this function + // if IPv4 is disabled. Set these netifs here. + if (s_esp_netifs[i].netif == NULL && s_esp_netifs[i].predefined) { + s_esp_netifs[i].netif = netif_from_preset(s_esp_netifs[i].predef_if); + } + if (esp_netif == s_esp_netifs[i].netif) { + return i; + } + } + return MDNS_MAX_INTERFACES; +} + +static esp_err_t post_custom_action(mdns_if_t mdns_if, mdns_event_actions_t event_action) +{ + if (!mdns_priv_is_server_init() || mdns_if >= MDNS_MAX_INTERFACES) { + return ESP_ERR_INVALID_STATE; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_calloc(1, sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + action->type = ACTION_SYSTEM_EVENT; + action->data.sys_event.event_action = event_action; + action->data.sys_event.interface = mdns_if; + + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + } + return ESP_OK; +} + +/** + * @brief Dispatch interface changes based on system events + */ +static inline void post_disable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) +{ + post_custom_action(mdns_if_from_preset(preset_if), + protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_DISABLE_IP4 : MDNS_EVENT_DISABLE_IP6); +} + +static inline void post_enable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) +{ + post_custom_action(mdns_if_from_preset(preset_if), + protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ENABLE_IP4 : MDNS_EVENT_ENABLE_IP6); +} + +static inline void post_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) +{ + post_custom_action(mdns_if_from_preset(preset_if), + protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6); +} + +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH +static void handle_system_event_for_preset(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + if (!mdns_priv_is_server_init()) { + return; + } + + esp_netif_dhcp_status_t dcst; +#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) + if (event_base == WIFI_EVENT) { + switch (event_id) { + case WIFI_EVENT_STA_CONNECTED: + if (!esp_netif_dhcpc_get_status(netif_from_preset(MDNS_IF_STA), &dcst)) { + if (dcst == ESP_NETIF_DHCP_STOPPED) { + post_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); + } + } + break; + case WIFI_EVENT_STA_DISCONNECTED: + post_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); + post_disable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6); + break; + case WIFI_EVENT_AP_START: + post_enable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); + break; + case WIFI_EVENT_AP_STOP: + post_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V4); + post_disable_pcb(MDNS_IF_AP, MDNS_IP_PROTOCOL_V6); + break; + default: + break; + } + } else +#endif +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH + if (event_base == ETH_EVENT) { + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + if (!esp_netif_dhcpc_get_status(netif_from_preset(MDNS_IF_ETH), &dcst)) { + if (dcst == ESP_NETIF_DHCP_STOPPED) { + post_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); + } + } + break; + case ETHERNET_EVENT_DISCONNECTED: + post_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); + post_disable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V6); + break; + default: + break; + } + } else +#endif + if (event_base == IP_EVENT) { + switch (event_id) { + case IP_EVENT_STA_GOT_IP: + post_enable_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V4); + post_announce_pcb(MDNS_IF_STA, MDNS_IP_PROTOCOL_V6); + break; +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH + case IP_EVENT_ETH_GOT_IP: + post_enable_pcb(MDNS_IF_ETH, MDNS_IP_PROTOCOL_V4); + break; +#endif + case IP_EVENT_GOT_IP6: { + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data; + mdns_if_t mdns_if = get_if_from_netif(event->esp_netif); + if (mdns_if >= MDNS_MAX_INTERFACES) { + return; + } + post_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6); + post_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4); + mdns_priv_browse_send_all(mdns_if); + + } + break; + default: + break; + } + } +} +#endif /* CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH */ + +static inline void set_default_duplicated_interfaces(void) +{ + mdns_if_t wifi_sta_if = MDNS_MAX_INTERFACES; + mdns_if_t eth_if = MDNS_MAX_INTERFACES; + for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; i++) { + if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_STA) { + wifi_sta_if = i; + } + if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == MDNS_IF_ETH) { + eth_if = i; + } + } + if (wifi_sta_if != MDNS_MAX_INTERFACES && eth_if != MDNS_MAX_INTERFACES) { + s_esp_netifs[wifi_sta_if].duplicate = eth_if; + s_esp_netifs[eth_if].duplicate = wifi_sta_if; + } +} + +void mdns_priv_netif_unregister_predefined_handlers(void) +{ +#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) + esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset); +#endif +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH + esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset); +#endif +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH + esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset); +#endif +} + +esp_err_t mdns_priv_netif_init(void) +{ + esp_err_t err = ESP_OK; + // zero-out local copy of netifs to initiate a fresh search by interface key whenever a netif ptr is needed + for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { + s_esp_netifs[i].netif = NULL; + } +#if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) + if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset, NULL)) != ESP_OK) { + goto free_event_handlers; + } +#endif +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH + if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset, NULL)) != ESP_OK) { + goto free_event_handlers; + } +#endif +#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH + if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, handle_system_event_for_preset, NULL)) != ESP_OK) { + goto free_event_handlers; + } +#endif + +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH + set_default_duplicated_interfaces(); +#endif + + uint8_t i; +#ifdef CONFIG_LWIP_IPV6 + esp_ip6_addr_t tmp_addr6; +#endif +#ifdef CONFIG_LWIP_IPV4 + esp_netif_ip_info_t if_ip_info; +#endif + + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { +#ifdef CONFIG_LWIP_IPV6 + if (!esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(i), &tmp_addr6) && !mdns_utils_ipv6_address_is_zero(tmp_addr6)) { + mdns_priv_pcb_enable(i, MDNS_IP_PROTOCOL_V6); + } +#endif +#ifdef CONFIG_LWIP_IPV4 + if (!esp_netif_get_ip_info(mdns_priv_get_esp_netif(i), &if_ip_info) && if_ip_info.ip.addr) { + mdns_priv_pcb_enable(i, MDNS_IP_PROTOCOL_V4); + } +#endif + } + return ESP_OK; +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH +free_event_handlers: + mdns_priv_netif_unregister_predefined_handlers(); +#endif + return err; +} + +esp_err_t mdns_priv_netif_deinit(void) +{ + for (int i = 0; i < MDNS_MAX_INTERFACES; i++) { + mdns_priv_pcb_disable(i, MDNS_IP_PROTOCOL_V6); + mdns_priv_pcb_disable(i, MDNS_IP_PROTOCOL_V4); + s_esp_netifs[i].duplicate = MDNS_MAX_INTERFACES; + } + return ESP_OK; +} + +/* + * Public Methods + * */ +esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action) +{ + return post_custom_action(get_if_from_netif(esp_netif), event_action); +} + +esp_err_t mdns_register_netif(esp_netif_t *esp_netif) +{ + if (!mdns_priv_is_server_init()) { + return ESP_ERR_INVALID_STATE; + } + + esp_err_t err = ESP_ERR_NO_MEM; + mdns_priv_service_lock(); + for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { + if (s_esp_netifs[i].netif == esp_netif) { + mdns_priv_service_unlock(); + return ESP_ERR_INVALID_STATE; + } + } + + for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { + if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == NULL) { + s_esp_netifs[i].netif = esp_netif; + err = ESP_OK; + break; + } + } + mdns_priv_service_unlock(); + return err; +} + +esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif) +{ + if (!mdns_priv_is_server_init()) { + return ESP_ERR_INVALID_STATE; + } + + esp_err_t err = ESP_ERR_NOT_FOUND; + mdns_priv_service_lock(); + for (mdns_if_t i = 0; i < MDNS_MAX_INTERFACES; ++i) { + if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == esp_netif) { + s_esp_netifs[i].netif = NULL; + err = ESP_OK; + break; + } + } + mdns_priv_service_lock(); + return err; +} diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index a686727645..373d957a9c 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -16,11 +16,12 @@ #include "lwip/udp.h" #include "lwip/mld6.h" #include "lwip/priv/tcpip_priv.h" -#include "esp_system.h" -#include "esp_event.h" #include "mdns_networking.h" #include "esp_netif_net_stack.h" #include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_netif.h" +#include "mdns_service.h" /* * MDNS Server Networking @@ -38,56 +39,75 @@ typedef struct interfaces { static interfaces_t s_interfaces[MDNS_MAX_INTERFACES]; -static struct udp_pcb *_pcb_main = NULL; +static struct udp_pcb *s_pcb_main = NULL; static const char *TAG = "mdns_networking"; -static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport); +static void receive(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport); + +static esp_err_t send_rx_action(mdns_rx_packet_t *packet) +{ + mdns_action_t *action = NULL; + + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + + action->type = ACTION_RX_HANDLE; + action->data.rx_handle.packet = packet; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} /** * @brief Low level UDP PCB Initialize */ -static esp_err_t _udp_pcb_main_init(void) +static esp_err_t pcb_init(void) { - if (_pcb_main) { + if (s_pcb_main) { return ESP_OK; } - _pcb_main = udp_new(); - if (!_pcb_main) { + s_pcb_main = udp_new(); + if (!s_pcb_main) { return ESP_ERR_NO_MEM; } - if (udp_bind(_pcb_main, IP_ANY_TYPE, MDNS_SERVICE_PORT) != 0) { - udp_remove(_pcb_main); - _pcb_main = NULL; + if (udp_bind(s_pcb_main, IP_ANY_TYPE, MDNS_SERVICE_PORT) != 0) { + udp_remove(s_pcb_main); + s_pcb_main = NULL; return ESP_ERR_INVALID_STATE; } - _pcb_main->mcast_ttl = 255; - _pcb_main->remote_port = MDNS_SERVICE_PORT; - ip_addr_copy(_pcb_main->remote_ip, *(IP_ANY_TYPE)); - udp_recv(_pcb_main, &_udp_recv, NULL); + s_pcb_main->mcast_ttl = 255; + s_pcb_main->remote_port = MDNS_SERVICE_PORT; + ip_addr_copy(s_pcb_main->remote_ip, *(IP_ANY_TYPE)); + udp_recv(s_pcb_main, receive, NULL); return ESP_OK; } /** * @brief Low level UDP PCB Free */ -static void _udp_pcb_main_deinit(void) +static void pcb_deinit(void) { - if (_pcb_main) { - udp_recv(_pcb_main, NULL, NULL); - udp_disconnect(_pcb_main); - udp_remove(_pcb_main); - _pcb_main = NULL; + if (s_pcb_main) { + udp_recv(s_pcb_main, NULL, NULL); + udp_disconnect(s_pcb_main); + udp_remove(s_pcb_main); + s_pcb_main = NULL; } } /** * @brief Low level UDP Multicast membership control */ -static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protocol, bool join) +static esp_err_t join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protocol, bool join) { struct netif *netif = NULL; - esp_netif_t *tcpip_if = _mdns_get_esp_netif(if_inx); + esp_netif_t *tcpip_if = mdns_priv_get_esp_netif(if_inx); if (!esp_netif_is_netif_up(tcpip_if)) { // Network interface went down before event propagated, skipping IGMP config @@ -135,7 +155,7 @@ static esp_err_t _udp_join_group(mdns_if_t if_inx, mdns_ip_protocol_t ip_protoco * @brief the receive callback of the raw udp api. Packets are received here * */ -static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport) +static void receive(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip_addr_t *raddr, uint16_t rport) { uint8_t i; @@ -188,7 +208,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip struct netif *netif = NULL; bool found = false; for (i = 0; i < MDNS_MAX_INTERFACES; i++) { - netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i)); + netif = esp_netif_get_netif_impl(mdns_priv_get_esp_netif(i)); if (s_interfaces[i].proto && netif && netif == ip_current_input_netif()) { #if LWIP_IPV4 if (packet->src.type == IPADDR_TYPE_V4) { @@ -204,7 +224,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip } } - if (!found || _mdns_send_rx_action(packet) != ESP_OK) { + if (!found || send_rx_action(packet) != ESP_OK) { pbuf_free(this_pb); mdns_mem_free(packet); } @@ -212,7 +232,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip } -bool mdns_is_netif_ready(mdns_if_t netif, mdns_ip_protocol_t ip_proto) +bool mdns_priv_if_ready(mdns_if_t netif, mdns_ip_protocol_t ip_proto) { return s_interfaces[netif].ready && s_interfaces[netif].proto & (ip_proto == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6); @@ -221,12 +241,12 @@ bool mdns_is_netif_ready(mdns_if_t netif, mdns_ip_protocol_t ip_proto) /** * @brief Check if any of the interfaces is up */ -static bool _udp_pcb_is_in_use(void) +static bool is_any_pcb_in_use(void) { int i, p; for (i = 0; i < MDNS_MAX_INTERFACES; i++) { for (p = 0; p < MDNS_IP_PROTOCOL_MAX; p++) { - if (mdns_is_netif_ready(i, p)) { + if (mdns_priv_if_ready(i, p)) { return true; } } @@ -237,14 +257,14 @@ static bool _udp_pcb_is_in_use(void) /** * @brief Stop PCB Main code */ -static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static void pcb_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { s_interfaces[tcpip_if].proto &= ~(ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6); if (s_interfaces[tcpip_if].proto == 0) { s_interfaces[tcpip_if].ready = false; - _udp_join_group(tcpip_if, ip_protocol, false); - if (!_udp_pcb_is_in_use()) { - _udp_pcb_main_deinit(); + join_group(tcpip_if, ip_protocol, false); + if (!is_any_pcb_in_use()) { + pcb_deinit(); } } } @@ -252,18 +272,18 @@ static void _udp_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) /** * @brief Start PCB Main code */ -static esp_err_t _udp_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +static esp_err_t pcb_if_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { - if (mdns_is_netif_ready(tcpip_if, ip_protocol)) { + if (mdns_priv_if_ready(tcpip_if, ip_protocol)) { return ESP_ERR_INVALID_STATE; } - esp_err_t err = _udp_join_group(tcpip_if, ip_protocol, true); + esp_err_t err = join_group(tcpip_if, ip_protocol, true); if (err) { return err; } - err = _udp_pcb_main_init(); + err = pcb_init(); if (err) { return err; } @@ -286,20 +306,20 @@ typedef struct { /** * @brief Start PCB from LwIP thread */ -static err_t _mdns_pcb_init_api(struct tcpip_api_call_data *api_call_msg) +static err_t pcb_if_init_lwip(struct tcpip_api_call_data *api_call_msg) { mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg; - msg->err = _udp_pcb_init(msg->tcpip_if, msg->ip_protocol) == ESP_OK ? ERR_OK : ERR_IF; - return msg->err; + msg->err = pcb_if_init(msg->tcpip_if, msg->ip_protocol); + return msg->err == ESP_OK ? ERR_OK : ERR_IF; } /** * @brief Stop PCB from LwIP thread */ -static err_t _mdns_pcb_deinit_api(struct tcpip_api_call_data *api_call_msg) +static err_t pcb_if_deinit_lwip(struct tcpip_api_call_data *api_call_msg) { mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg; - _udp_pcb_deinit(msg->tcpip_if, msg->ip_protocol); + pcb_if_deinit(msg->tcpip_if, msg->ip_protocol); msg->err = ESP_OK; return ESP_OK; } @@ -309,43 +329,43 @@ static err_t _mdns_pcb_deinit_api(struct tcpip_api_call_data *api_call_msg) * - _mdns prefixed * - commented in mdns_networking.h header */ -esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t mdns_priv_if_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_api_call_t msg = { .tcpip_if = tcpip_if, .ip_protocol = ip_protocol }; - tcpip_api_call(_mdns_pcb_init_api, &msg.call); + tcpip_api_call(pcb_if_init_lwip, &msg.call); return msg.err; } -esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t mdns_priv_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { mdns_api_call_t msg = { .tcpip_if = tcpip_if, .ip_protocol = ip_protocol }; - tcpip_api_call(_mdns_pcb_deinit_api, &msg.call); + tcpip_api_call(pcb_if_deinit_lwip, &msg.call); return msg.err; } -static err_t _mdns_udp_pcb_write_api(struct tcpip_api_call_data *api_call_msg) +static err_t write_if_lwip(struct tcpip_api_call_data *api_call_msg) { void *nif = NULL; mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg; - nif = esp_netif_get_netif_impl(_mdns_get_esp_netif(msg->tcpip_if)); - if (!nif || !mdns_is_netif_ready(msg->tcpip_if, msg->ip_protocol) || _pcb_main == NULL) { + nif = esp_netif_get_netif_impl(mdns_priv_get_esp_netif(msg->tcpip_if)); + if (!nif || !mdns_priv_if_ready(msg->tcpip_if, msg->ip_protocol) || s_pcb_main == NULL) { pbuf_free(msg->pbt); msg->err = ERR_IF; return ERR_IF; } - esp_err_t err = udp_sendto_if(_pcb_main, msg->pbt, msg->ip, msg->port, (struct netif *)nif); + err_t err = udp_sendto_if(s_pcb_main, msg->pbt, msg->ip, msg->port, (struct netif *)nif); pbuf_free(msg->pbt); - msg->err = err; + msg->err = err == ERR_OK ? ESP_OK : ESP_FAIL; return err; } -size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len) +size_t mdns_priv_if_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len) { struct pbuf *pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); if (pbt == NULL) { @@ -373,7 +393,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c .ip = &ip_add_copy, .port = port }; - tcpip_api_call(_mdns_udp_pcb_write_api, &msg.call); + tcpip_api_call(write_if_lwip, &msg.call); if (msg.err) { return 0; @@ -381,17 +401,17 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c return len; } -void *_mdns_get_packet_data(mdns_rx_packet_t *packet) +void *mdns_priv_get_packet_data(mdns_rx_packet_t *packet) { return packet->pb->payload; } -size_t _mdns_get_packet_len(mdns_rx_packet_t *packet) +size_t mdns_priv_get_packet_len(mdns_rx_packet_t *packet) { return packet->pb->len; } -void _mdns_packet_free(mdns_rx_packet_t *packet) +void mdns_priv_packet_free(mdns_rx_packet_t *packet) { pbuf_free(packet->pb); mdns_mem_free(packet); diff --git a/components/mdns/mdns_networking_socket.c b/components/mdns/mdns_networking_socket.c index 7bcafbb9c3..ee6e1b5ffa 100644 --- a/components/mdns/mdns_networking_socket.c +++ b/components/mdns/mdns_networking_socket.c @@ -22,6 +22,9 @@ #include #include "esp_log.h" #include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_netif.h" +#include "mdns_service.h" #if defined(CONFIG_IDF_TARGET_LINUX) #include @@ -58,6 +61,26 @@ struct pbuf { #define s6_addr32 un.u32_addr #endif // CONFIG_IDF_TARGET_LINUX +static esp_err_t send_rx_action(mdns_rx_packet_t *packet) +{ + mdns_action_t *action = NULL; + + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + + action->type = ACTION_RX_HANDLE; + action->data.rx_handle.packet = packet; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + + static void __attribute__((constructor)) ctor_networking_socket(void) { for (int i = 0; i < sizeof(s_interfaces) / sizeof(s_interfaces[0]); ++i) { @@ -71,29 +94,29 @@ static void delete_socket(int sock) close(sock); } -bool mdns_is_netif_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +bool mdns_priv_if_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { return s_interfaces[tcpip_if].proto & (ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6); } -void *_mdns_get_packet_data(mdns_rx_packet_t *packet) +void *mdns_priv_get_packet_data(mdns_rx_packet_t *packet) { return packet->pb->payload; } -size_t _mdns_get_packet_len(mdns_rx_packet_t *packet) +size_t mdns_priv_get_packet_len(mdns_rx_packet_t *packet) { return packet->pb->len; } -void _mdns_packet_free(mdns_rx_packet_t *packet) +void mdns_priv_packet_free(mdns_rx_packet_t *packet) { mdns_mem_free(packet->pb->payload); mdns_mem_free(packet->pb); mdns_mem_free(packet); } -esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t mdns_priv_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { s_interfaces[tcpip_if].proto &= ~(ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6); if (s_interfaces[tcpip_if].proto == 0) { @@ -192,7 +215,7 @@ static inline size_t espaddr_to_inet(const esp_ip_addr_t *addr, const uint16_t p return ss_addr_len; } -size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len) +size_t mdns_priv_if_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len) { if (!(s_interfaces[tcpip_if].proto & (ip_protocol == MDNS_IP_PROTOCOL_V4 ? PROTO_IPV4 : PROTO_IPV6))) { return 0; @@ -210,7 +233,7 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c ESP_LOGD(TAG, "[sock=%d]: Sending to IP %s port %d", sock, get_string_address(&in_addr), port); ssize_t actual_len = sendto(sock, data, len, 0, (struct sockaddr *)&in_addr, ss_size); if (actual_len < 0) { - ESP_LOGE(TAG, "[sock=%d]: _mdns_udp_pcb_write sendto() has failed\n errno=%d: %s", sock, errno, strerror(errno)); + ESP_LOGE(TAG, "[sock=%d]: mdns_priv_if_write sendto() has failed\n errno=%d: %s", sock, errno, strerror(errno)); } return actual_len; } @@ -325,8 +348,8 @@ void sock_recv_task(void *arg) packet->dest.type = packet->src.type; packet->ip_protocol = packet->src.type == ESP_IPADDR_TYPE_V4 ? MDNS_IP_PROTOCOL_V4 : MDNS_IP_PROTOCOL_V6; - if (_mdns_send_rx_action(packet) != ESP_OK) { - ESP_LOGE(TAG, "_mdns_send_rx_action failed!"); + if (send_rx_action(packet) != ESP_OK) { + ESP_LOGE(TAG, "send_rx_action failed!"); mdns_mem_free(packet->pb->payload); mdns_mem_free(packet->pb); mdns_mem_free(packet); @@ -338,7 +361,7 @@ void sock_recv_task(void *arg) vTaskDelete(NULL); } -static void mdns_networking_init(void) +static void networking_init(void) { if (s_run_sock_recv_task == false) { s_run_sock_recv_task = true; @@ -352,7 +375,7 @@ static bool create_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) return true; } int sock = s_interfaces[tcpip_if].sock; - esp_netif_t *netif = _mdns_get_esp_netif(tcpip_if); + esp_netif_t *netif = mdns_priv_get_esp_netif(tcpip_if); if (sock < 0) { sock = create_socket(netif); } @@ -369,14 +392,14 @@ static bool create_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) return true; } -esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +esp_err_t mdns_priv_if_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) { - ESP_LOGI(TAG, "_mdns_pcb_init(tcpip_if=%lu, ip_protocol=%lu)", (unsigned long)tcpip_if, (unsigned long)ip_protocol); + ESP_LOGI(TAG, "mdns_priv_if_init(tcpip_if=%lu, ip_protocol=%lu)", (unsigned long)tcpip_if, (unsigned long)ip_protocol); if (!create_pcb(tcpip_if, ip_protocol)) { return ESP_FAIL; } - mdns_networking_init(); + networking_init(); return ESP_OK; } diff --git a/components/mdns/mdns_pcb.c b/components/mdns/mdns_pcb.c new file mode 100644 index 0000000000..f631277081 --- /dev/null +++ b/components/mdns/mdns_pcb.c @@ -0,0 +1,462 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "mdns_private.h" +#include "mdns_networking.h" +#include "mdns_pcb.h" +#include "mdns_netif.h" +#include "mdns_utils.h" +#include "mdns_send.h" +#include "mdns_mem_caps.h" +#include "esp_log.h" +#include "esp_random.h" +#include "mdns_responder.h" + +#define PCB_STATE_IS_PROBING(s) (s->state > PCB_OFF && s->state < PCB_ANNOUNCE_1) +#define PCB_STATE_IS_ANNOUNCING(s) (s->state > PCB_PROBE_3 && s->state < PCB_RUNNING) + +typedef enum { + PCB_OFF, PCB_DUP, PCB_INIT, + PCB_PROBE_1, PCB_PROBE_2, PCB_PROBE_3, + PCB_ANNOUNCE_1, PCB_ANNOUNCE_2, PCB_ANNOUNCE_3, + PCB_RUNNING +} mdns_pcb_state_t; + +typedef struct { + mdns_pcb_state_t state; + mdns_srv_item_t **probe_services; + uint8_t probe_services_len; + uint8_t probe_ip; + uint8_t probe_running; + uint16_t failed_probes; +} mdns_pcb_t; + +static const char *TAG = "mdns_pcb"; +static mdns_pcb_t s_pcbs[MDNS_MAX_INTERFACES][MDNS_IP_PROTOCOL_MAX]; + +/** + * @brief Send announcement on particular PCB + */ +void mdns_priv_pcb_announce(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip) +{ + mdns_pcb_t *_pcb = &s_pcbs[tcpip_if][ip_protocol]; + size_t i; + if (mdns_priv_if_ready(tcpip_if, ip_protocol)) { + if (PCB_STATE_IS_PROBING(_pcb)) { + mdns_priv_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip); + } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) { + mdns_tx_packet_t *p = mdns_priv_get_next_packet(tcpip_if, ip_protocol); + if (p) { + for (i = 0; i < len; i++) { + if (!mdns_priv_create_answer(&p->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false) + || !mdns_priv_create_answer(&p->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, + false) + || !mdns_priv_create_answer(&p->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, + false) + || !mdns_priv_create_answer(&p->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, + false)) { + break; + } + } + if (include_ip) { + mdns_priv_dealloc_answer(&p->additional, MDNS_TYPE_A, NULL); + mdns_priv_dealloc_answer(&p->additional, MDNS_TYPE_AAAA, NULL); + mdns_priv_append_host_list_in_services(&p->answers, services, len, true, false); + } + _pcb->state = PCB_ANNOUNCE_1; + } + } else if (_pcb->state == PCB_RUNNING) { + + if (mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + return; + } + + _pcb->state = PCB_ANNOUNCE_1; + mdns_tx_packet_t *p = mdns_priv_create_announce_packet(tcpip_if, ip_protocol, services, len, include_ip); + if (p) { + mdns_priv_send_after(p, 0); + } + } + } +} + +/** + * @brief Check if interface is duplicate (two interfaces on the same subnet) + */ +bool mdns_priv_pcb_check_for_duplicates(mdns_if_t tcpip_if) +{ + mdns_if_t ifaces[MDNS_MAX_INTERFACES] = {tcpip_if, mdns_priv_netif_get_other_interface(tcpip_if) }; + if (ifaces[1] == MDNS_MAX_INTERFACES) { + return false; + } + // check both this netif and the potential duplicate on all protocols + // if any of them is in duplicate state, return true + for (int i = 0; i < 2; i++) { + for (int proto = 0; proto < MDNS_IP_PROTOCOL_MAX; proto++) { + if (s_pcbs[ifaces[i]][(mdns_ip_protocol_t) proto].state == PCB_DUP) { + return true; + } + } + } + return false; +} + +static esp_err_t deinit_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_proto) +{ + esp_err_t err = mdns_priv_if_deinit(tcpip_if, ip_proto); + mdns_pcb_t *pcb = &s_pcbs[tcpip_if][ip_proto]; + if (pcb == NULL || err != ESP_OK) { + return err; + } + mdns_mem_free(pcb->probe_services); + pcb->state = PCB_OFF; + pcb->probe_ip = false; + pcb->probe_services = NULL; + pcb->probe_services_len = 0; + pcb->probe_running = false; + pcb->failed_probes = 0; + return ESP_OK; +} + +/** + * @brief Restart the responder on particular PCB + */ +static void restart_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + size_t srv_count = 0; + mdns_srv_item_t *a = mdns_priv_get_services(); + while (a) { + srv_count++; + a = a->next; + } + if (srv_count == 0) { + // proble only IP + mdns_priv_init_pcb_probe(tcpip_if, ip_protocol, NULL, 0, true); + return; + } + mdns_srv_item_t *services[srv_count]; + size_t i = 0; + a = mdns_priv_get_services(); + while (a) { + services[i++] = a; + a = a->next; + } + mdns_priv_init_pcb_probe(tcpip_if, ip_protocol, services, srv_count, true); +} + +/** + * @brief Disable mDNS interface + */ +void mdns_priv_pcb_disable(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_priv_netif_disable(tcpip_if); + + if (mdns_priv_if_ready(tcpip_if, ip_protocol)) { + mdns_priv_clear_tx_queue_if(tcpip_if, ip_protocol); + deinit_pcb(tcpip_if, ip_protocol); + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + if (other_if != MDNS_MAX_INTERFACES && s_pcbs[other_if][ip_protocol].state == PCB_DUP) { + s_pcbs[other_if][ip_protocol].state = PCB_OFF; + mdns_priv_pcb_enable(other_if, ip_protocol); + } + } + s_pcbs[tcpip_if][ip_protocol].state = PCB_OFF; +} + +/** + * @brief Enable mDNS interface + */ +void mdns_priv_pcb_enable(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + if (!mdns_priv_if_ready(tcpip_if, ip_protocol)) { + if (mdns_priv_if_init(tcpip_if, ip_protocol)) { + s_pcbs[tcpip_if][ip_protocol].failed_probes = 0; + return; + } + } + restart_pcb(tcpip_if, ip_protocol); +} + +/** + * @brief Set interface as duplicate if another is found on the same subnet + */ +void mdns_priv_pcb_set_duplicate(mdns_if_t tcpip_if) +{ + uint8_t i; + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + if (other_if == MDNS_MAX_INTERFACES) { + return; // no other interface found + } + for (i = 0; i < MDNS_IP_PROTOCOL_MAX; i++) { + if (mdns_priv_if_ready(other_if, i)) { + //stop this interface and mark as dup + if (mdns_priv_if_ready(tcpip_if, i)) { + mdns_priv_clear_tx_queue_if(tcpip_if, i); + deinit_pcb(tcpip_if, i); + } + s_pcbs[tcpip_if][i].state = PCB_DUP; + mdns_priv_pcb_announce(other_if, i, NULL, 0, true); + } + } +} + +bool mdns_priv_pcb_is_off(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return s_pcbs[tcpip_if][ip_protocol].state == PCB_OFF; +} + +void mdns_priv_pcb_schedule_tx_packet(mdns_tx_packet_t *p) +{ + mdns_pcb_t *pcb = &s_pcbs[p->tcpip_if][p->ip_protocol]; + mdns_out_question_t *q = NULL; + mdns_tx_packet_t *a = NULL; + uint32_t send_after = 1000; + switch (pcb->state) { + case PCB_PROBE_1: + q = p->questions; + while (q) { + q->unicast = false; + q = q->next; + } + //fallthrough + case PCB_PROBE_2: + mdns_priv_send_after(p, 250); + pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1); + break; + case PCB_PROBE_3: + a = mdns_priv_create_announce_from_probe(p); + if (!a) { + mdns_priv_send_after(p, 250); + break; + } + pcb->probe_running = false; + pcb->probe_ip = false; + pcb->probe_services_len = 0; + pcb->failed_probes = 0; + mdns_mem_free(pcb->probe_services); + pcb->probe_services = NULL; + mdns_priv_free_tx_packet(p); + p = a; + send_after = 250; + //fallthrough + case PCB_ANNOUNCE_1: + //fallthrough + case PCB_ANNOUNCE_2: + mdns_priv_send_after(p, send_after); + pcb->state = (mdns_pcb_state_t)((uint8_t)(pcb->state) + 1); + break; + case PCB_ANNOUNCE_3: + pcb->state = PCB_RUNNING; + mdns_priv_free_tx_packet(p); + break; + default: + mdns_priv_free_tx_packet(p); + break; + } +} + +void mdns_priv_pcb_check_probing_services(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_service_t *service, bool removed_answers, bool *should_remove_questions) +{ + mdns_pcb_t *_pcb = &s_pcbs[tcpip_if][ip_protocol]; + + if (PCB_STATE_IS_PROBING(_pcb)) { + uint8_t i; + //check if we are probing this service + for (i = 0; i < _pcb->probe_services_len; i++) { + mdns_srv_item_t *s = _pcb->probe_services[i]; + if (s->service == service) { + break; + } + } + if (i < _pcb->probe_services_len) { + if (_pcb->probe_services_len > 1) { + uint8_t n; + for (n = (i + 1); n < _pcb->probe_services_len; n++) { + _pcb->probe_services[n - 1] = _pcb->probe_services[n]; + } + _pcb->probe_services_len--; + } else { + _pcb->probe_services_len = 0; + mdns_mem_free(_pcb->probe_services); + _pcb->probe_services = NULL; + if (!_pcb->probe_ip) { + _pcb->probe_running = false; + _pcb->state = PCB_RUNNING; + } + } + *should_remove_questions = true; + return; + } + } else if (PCB_STATE_IS_ANNOUNCING(_pcb)) { + //if answers were cleared, set to running + if (removed_answers) { + _pcb->state = PCB_RUNNING; + } + } + *should_remove_questions = false; +} + +void mdns_priv_pcb_deinit(void) +{ + for (int i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (int j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + deinit_pcb(i, j); + } + } +} + +bool mdsn_priv_pcb_is_inited(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return mdns_priv_if_ready(tcpip_if, ip_protocol) && s_pcbs[tcpip_if][ip_protocol].state > PCB_INIT; +} + +bool mdns_priv_pcb_is_duplicate(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return s_pcbs[tcpip_if][ip_protocol].state == PCB_DUP; +} + +bool mdns_priv_pcb_is_probing(mdns_rx_packet_t *packet) +{ + return s_pcbs[packet->tcpip_if][packet->ip_protocol].probe_running; +} + +bool mdns_priv_pcb_is_after_probing(mdns_rx_packet_t *packet) +{ + return s_pcbs[packet->tcpip_if][packet->ip_protocol].state > PCB_PROBE_3; +} + +void mdns_priv_pcb_set_probe_failed(mdns_rx_packet_t *packet) +{ + s_pcbs[packet->tcpip_if][packet->ip_protocol].failed_probes++; +} + +/** + * @brief Send probe for additional services on particular PCB + */ +static void init_probe_new_service(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip) +{ + mdns_pcb_t *pcb = &s_pcbs[tcpip_if][ip_protocol]; + size_t services_final_len = len; + + if (PCB_STATE_IS_PROBING(pcb)) { + services_final_len += pcb->probe_services_len; + } + mdns_srv_item_t **s = NULL; + if (services_final_len) { + s = (mdns_srv_item_t **)mdns_mem_malloc(sizeof(mdns_srv_item_t *) * services_final_len); + if (!s) { + HOOK_MALLOC_FAILED; + return; + } + + size_t i; + for (i = 0; i < len; i++) { + s[i] = services[i]; + } + if (pcb->probe_services) { + for (i = 0; i < pcb->probe_services_len; i++) { + s[len + i] = pcb->probe_services[i]; + } + mdns_mem_free(pcb->probe_services); + } + } + + probe_ip = pcb->probe_ip || probe_ip; + + pcb->probe_ip = false; + pcb->probe_services = NULL; + pcb->probe_services_len = 0; + pcb->probe_running = false; + + mdns_tx_packet_t *packet = mdns_priv_create_probe_packet(tcpip_if, ip_protocol, s, services_final_len, true, + probe_ip); + if (!packet) { + mdns_mem_free(s); + return; + } + + pcb->probe_ip = probe_ip; + pcb->probe_services = s; + pcb->probe_services_len = services_final_len; + pcb->probe_running = true; + mdns_priv_send_after(packet, ((pcb->failed_probes > 5) ? 1000 : 120) + (esp_random() & 0x7F)); + pcb->state = PCB_PROBE_1; +} + +void mdns_priv_init_pcb_probe(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip) +{ + mdns_pcb_t *pcb = &s_pcbs[tcpip_if][ip_protocol]; + + mdns_priv_clear_tx_queue_if(tcpip_if, ip_protocol); + + if (mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + pcb->state = PCB_RUNNING; + return; + } + + if (PCB_STATE_IS_PROBING(pcb)) { + // Looking for already probing services to resolve duplications + mdns_srv_item_t *new_probe_services[len]; + int new_probe_service_len = 0; + bool found; + for (size_t j = 0; j < len; ++j) { + found = false; + for (int i = 0; i < pcb->probe_services_len; ++i) { + if (pcb->probe_services[i] == services[j]) { + found = true; + break; + } + } + if (!found) { + new_probe_services[new_probe_service_len++] = services[j]; + } + } + // init probing for newly added services + init_probe_new_service(tcpip_if, ip_protocol, + new_probe_service_len ? new_probe_services : NULL, new_probe_service_len, probe_ip); + } else { + // not probing, so init for all services + init_probe_new_service(tcpip_if, ip_protocol, services, len, probe_ip); + } +} + +/** + * @brief Send by for particular services + */ +void mdns_priv_pcb_send_bye_service(mdns_srv_item_t **services, size_t len, bool include_ip) +{ + uint8_t i, j; + if (mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + return; + } + + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + if (mdns_priv_if_ready(i, j) && s_pcbs[i][j].state == PCB_RUNNING) { + mdns_priv_send_bye((mdns_if_t) i, (mdns_ip_protocol_t) j, services, len, include_ip); + } + } + } +} + +void mdns_priv_probe_all_pcbs(mdns_srv_item_t **services, size_t len, bool probe_ip, bool clear_old_probe) +{ + uint8_t i, j; + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + if (mdns_priv_if_ready(i, j)) { + mdns_pcb_t *_pcb = &s_pcbs[i][j]; + if (clear_old_probe) { + mdns_mem_free(_pcb->probe_services); + _pcb->probe_services = NULL; + _pcb->probe_services_len = 0; + _pcb->probe_running = false; + } + mdns_priv_init_pcb_probe((mdns_if_t) i, (mdns_ip_protocol_t) j, services, len, probe_ip); + } + } + } +} diff --git a/components/mdns/mdns_querier.c b/components/mdns/mdns_querier.c new file mode 100644 index 0000000000..09e81fa253 --- /dev/null +++ b/components/mdns/mdns_querier.c @@ -0,0 +1,872 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "mdns_querier.h" +#include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_send.h" +#include "esp_log.h" +#include "mdns_pcb.h" +#include "mdns_netif.h" +#include "mdns_responder.h" +#include "mdns_service.h" + +static const char *TAG = "mdns_querier"; +static mdns_search_once_t *s_search_once; + +static esp_err_t send_search_action(mdns_action_type_t type, mdns_search_once_t *search); +static void search_free(mdns_search_once_t *search); + +void mdns_priv_query_results_free(mdns_result_t *results) +{ + mdns_result_t *r; + mdns_ip_addr_t *a; + + while (results) { + r = results; + + mdns_mem_free((char *)(r->hostname)); + mdns_mem_free((char *)(r->instance_name)); + mdns_mem_free((char *)(r->service_type)); + mdns_mem_free((char *)(r->proto)); + + for (size_t i = 0; i < r->txt_count; i++) { + mdns_mem_free((char *)(r->txt[i].key)); + mdns_mem_free((char *)(r->txt[i].value)); + } + mdns_mem_free(r->txt); + mdns_mem_free(r->txt_value_len); + + while (r->addr) { + a = r->addr; + r->addr = r->addr->next; + mdns_mem_free(a); + } + + results = results->next; + mdns_mem_free(r); + } +} + +/** + * @brief Mark search as finished and remove it from search chain + */ +static void search_finish(mdns_search_once_t *search) +{ + search->state = SEARCH_OFF; + queueDetach(mdns_search_once_t, s_search_once, search); + if (search->notifier) { + search->notifier(search); + } + xSemaphoreGive(search->done_semaphore); +} + +/** + * @brief Add new search to the search chain + */ +void search_add(mdns_search_once_t *search) +{ + search->next = s_search_once; + s_search_once = search; +} + +/** + * @brief Send search packet to all available interfaces + */ +static void search_send(mdns_search_once_t *search) +{ + mdns_search_once_t *queue = s_search_once; + bool found = false; + // looking for this search in active searches + while (queue) { + if (queue == search) { + found = true; + break; + } + queue = queue->next; + } + + if (!found) { + // no longer active -> skip sending this search + return; + } + + uint8_t i, j; + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + mdns_priv_query_send(search, (mdns_if_t) i, (mdns_ip_protocol_t) j); + } + } +} + +void mdns_priv_query_action(mdns_action_t *action, mdns_action_subtype_t type) +{ + if (type == ACTION_RUN) { + switch (action->type) { + case ACTION_SEARCH_ADD: + search_add(action->data.search_add.search); + break; + case ACTION_SEARCH_SEND: + search_send(action->data.search_add.search); + break; + case ACTION_SEARCH_END: + search_finish(action->data.search_add.search); + break; + default: + abort(); + } + return; + } + if (type == ACTION_CLEANUP) { + search_free(action->data.search_add.search); + } +} + +/** + * @brief Called from timer task to run active searches + */ +void mdns_priv_query_start_stop(void) +{ + mdns_priv_service_lock(); + mdns_search_once_t *s = s_search_once; + uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS; + if (!s) { + mdns_priv_service_unlock(); + return; + } + while (s) { + if (s->state != SEARCH_OFF) { + if (now > (s->started_at + s->timeout)) { + s->state = SEARCH_OFF; + if (send_search_action(ACTION_SEARCH_END, s) != ESP_OK) { + s->state = SEARCH_RUNNING; + } + } else if (s->state == SEARCH_INIT || (now - s->sent_at) > 1000) { + s->state = SEARCH_RUNNING; + s->sent_at = now; + if (send_search_action(ACTION_SEARCH_SEND, s) != ESP_OK) { + s->sent_at -= 1000; + } + } + } + s = s->next; + } + mdns_priv_service_unlock(); +} + +void mdns_priv_query_free(void) +{ + while (s_search_once) { + mdns_search_once_t *h = s_search_once; + s_search_once = h->next; + mdns_mem_free(h->instance); + mdns_mem_free(h->service); + mdns_mem_free(h->proto); + vSemaphoreDelete(h->done_semaphore); + if (h->result) { + mdns_priv_query_results_free(h->result); + } + mdns_mem_free(h); + } +} + +/** + * @brief Called from parser to finish any searches that have reached maximum results + */ +void mdns_priv_query_done(void) +{ + mdns_search_once_t *search = s_search_once; + mdns_search_once_t *s = NULL; + while (search) { + s = search; + search = search->next; + if (s->max_results && s->num_results >= s->max_results) { + search_finish(s); + } + } +} + +/** + * @brief Called from packet parser to find matching running search + */ +mdns_search_once_t *mdns_priv_query_find_from(mdns_search_once_t *s, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_result_t *r = NULL; + while (s) { + if (s->state == SEARCH_OFF) { + s = s->next; + continue; + } + + if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { + if ((s->type == MDNS_TYPE_ANY && s->service != NULL) + || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV)) { + s = s->next; + continue; + } + if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) { + if (!strcasecmp(name->host, s->instance)) { + return s; + } + s = s->next; + continue; + } + r = s->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { + return s; + } + r = r->next; + } + s = s->next; + continue; + } + + if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { + if ((s->type == MDNS_TYPE_ANY && s->service == NULL) + || (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR)) { + s = s->next; + continue; + } + if (strcasecmp(name->service, s->service) + || strcasecmp(name->proto, s->proto)) { + s = s->next; + continue; + } + if (s->type != MDNS_TYPE_PTR) { + if (s->instance && strcasecmp(name->host, s->instance) == 0) { + return s; + } + s = s->next; + continue; + } + return s; + } + + if (type == MDNS_TYPE_PTR && type == s->type && !strcasecmp(name->service, s->service) && !strcasecmp(name->proto, s->proto)) { + return s; + } + + s = s->next; + } + + return NULL; +} + +mdns_search_once_t *mdns_priv_query_find(mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return mdns_priv_query_find_from(s_search_once, name, type, tcpip_if, ip_protocol); +} + +/** + * @brief Create search packet for particular interface + */ +static mdns_tx_packet_t *create_search_packet(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_result_t *r = NULL; + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(tcpip_if, ip_protocol); + if (!packet) { + return NULL; + } + + mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); + if (!q) { + HOOK_MALLOC_FAILED; + mdns_priv_free_tx_packet(packet); + return NULL; + } + q->next = NULL; + q->unicast = search->unicast; + q->type = search->type; + q->host = search->instance; + q->service = search->service; + q->proto = search->proto; + q->domain = MDNS_UTILS_DEFAULT_DOMAIN; + q->own_dynamic_memory = false; + queueToEnd(mdns_out_question_t, packet->questions, q); + + if (search->type == MDNS_TYPE_PTR) { + r = search->result; + while (r) { + //full record on the same interface is available + if (r->esp_netif != mdns_priv_get_esp_netif(tcpip_if) || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) { + r = r->next; + continue; + } + mdns_out_answer_t *a = (mdns_out_answer_t *)mdns_mem_malloc(sizeof(mdns_out_answer_t)); + if (!a) { + HOOK_MALLOC_FAILED; + mdns_priv_free_tx_packet(packet); + return NULL; + } + a->type = MDNS_TYPE_PTR; + a->service = NULL; + a->custom_instance = r->instance_name; + a->custom_service = search->service; + a->custom_proto = search->proto; + a->bye = false; + a->flush = false; + a->next = NULL; + queueToEnd(mdns_out_answer_t, packet->answers, a); + r = r->next; + } + } + + return packet; +} + + +/** + * @brief Send search packet to particular interface + */ +void mdns_priv_query_send(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_tx_packet_t *packet = NULL; + if (mdsn_priv_pcb_is_inited(tcpip_if, ip_protocol)) { + packet = create_search_packet(search, tcpip_if, ip_protocol); + if (!packet) { + return; + } + mdns_priv_dispatch_tx_packet(packet); + mdns_priv_free_tx_packet(packet); + } +} + +/** + * @brief Free search structure (except the results) + */ +static void search_free(mdns_search_once_t *search) +{ + mdns_mem_free(search->instance); + mdns_mem_free(search->service); + mdns_mem_free(search->proto); + vSemaphoreDelete(search->done_semaphore); + mdns_mem_free(search); +} + +/** + * @brief Allocate new search structure + */ +static mdns_search_once_t *search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, + uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier) +{ + mdns_search_once_t *search = (mdns_search_once_t *)mdns_mem_malloc(sizeof(mdns_search_once_t)); + if (!search) { + HOOK_MALLOC_FAILED; + return NULL; + } + memset(search, 0, sizeof(mdns_search_once_t)); + + search->done_semaphore = xSemaphoreCreateBinary(); + if (!search->done_semaphore) { + mdns_mem_free(search); + return NULL; + } + + if (!mdns_utils_str_null_or_empty(name)) { + search->instance = mdns_mem_strndup(name, MDNS_NAME_BUF_LEN - 1); + if (!search->instance) { + search_free(search); + return NULL; + } + } + + if (!mdns_utils_str_null_or_empty(service)) { + search->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); + if (!search->service) { + search_free(search); + return NULL; + } + } + + if (!mdns_utils_str_null_or_empty(proto)) { + search->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); + if (!search->proto) { + search_free(search); + return NULL; + } + } + + search->type = type; + search->unicast = unicast; + search->timeout = timeout; + search->num_results = 0; + search->max_results = max_results; + search->result = NULL; + search->state = SEARCH_INIT; + search->sent_at = 0; + search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS; + search->notifier = notifier; + search->next = NULL; + + return search; +} + +/** + * @brief Queue search action + */ +static esp_err_t send_search_action(mdns_action_type_t type, mdns_search_once_t *search) +{ + mdns_action_t *action = NULL; + + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + + action->type = type; + action->data.search_add.search = search; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +/** + * @brief Called from parser to add TXT data to search result + */ +void mdns_priv_query_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len, + size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, + uint32_t ttl) +{ + mdns_result_t *r = search->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { + if (r->txt) { + goto free_txt; + } + r->txt = txt; + r->txt_value_len = txt_value_len; + r->txt_count = txt_count; + mdns_priv_query_update_result_ttl(r, ttl); + return; + } + r = r->next; + } + if (!search->max_results || search->num_results < search->max_results) { + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + goto free_txt; + } + + memset(r, 0, sizeof(mdns_result_t)); + r->txt = txt; + r->txt_value_len = txt_value_len; + r->txt_count = txt_count; + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->ttl = ttl; + r->next = search->result; + search->result = r; + search->num_results++; + } + return; + +free_txt: + for (size_t i = 0; i < txt_count; i++) { + mdns_mem_free((char *)(txt[i].key)); + mdns_mem_free((char *)(txt[i].value)); + } + mdns_mem_free(txt); + mdns_mem_free(txt_value_len); +} + +/** + * @brief Chain new IP to search result + */ +static void result_add_ip(mdns_result_t *r, esp_ip_addr_t *ip) +{ + mdns_ip_addr_t *a = r->addr; + while (a) { + if (a->addr.type == ip->type) { +#ifdef CONFIG_LWIP_IPV4 + if (a->addr.type == ESP_IPADDR_TYPE_V4 && a->addr.u_addr.ip4.addr == ip->u_addr.ip4.addr) { + return; + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (a->addr.type == ESP_IPADDR_TYPE_V6 && !memcmp(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16)) { + return; + } +#endif + } + a = a->next; + } + a = mdns_priv_result_addr_create_ip(ip); + if (!a) { + return; + } + a->next = r->addr; + r->addr = a; +} + +/** + * @brief Called from parser to add A/AAAA data to search result + */ +void mdns_priv_query_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl) +{ + mdns_result_t *r = NULL; + mdns_ip_addr_t *a = NULL; + + if ((search->type == MDNS_TYPE_A && ip->type == ESP_IPADDR_TYPE_V4) + || (search->type == MDNS_TYPE_AAAA && ip->type == ESP_IPADDR_TYPE_V6) + || search->type == MDNS_TYPE_ANY) { + r = search->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { + result_add_ip(r, ip); + mdns_priv_query_update_result_ttl(r, ttl); + return; + } + r = r->next; + } + if (!search->max_results || search->num_results < search->max_results) { + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + return; + } + + memset(r, 0, sizeof(mdns_result_t)); + + a = mdns_priv_result_addr_create_ip(ip); + if (!a) { + mdns_mem_free(r); + return; + } + a->next = r->addr; + r->hostname = mdns_mem_strdup(hostname); + r->addr = a; + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->next = search->result; + r->ttl = ttl; + search->result = r; + search->num_results++; + } + } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) { + r = search->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { + result_add_ip(r, ip); + mdns_priv_query_update_result_ttl(r, ttl); + break; + } + r = r->next; + } + } +} + +/** + * @brief Called from parser to add SRV data to search result + */ +void mdns_priv_query_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl) +{ + mdns_result_t *r = search->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !mdns_utils_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { + mdns_priv_query_update_result_ttl(r, ttl); + return; + } + r = r->next; + } + if (!search->max_results || search->num_results < search->max_results) { + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + return; + } + + memset(r, 0, sizeof(mdns_result_t)); + r->hostname = mdns_mem_strdup(hostname); + if (!r->hostname) { + mdns_mem_free(r); + return; + } + if (search->instance) { + r->instance_name = mdns_mem_strdup(search->instance); + } + r->service_type = mdns_mem_strdup(search->service); + r->proto = mdns_mem_strdup(search->proto); + r->port = port; + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->ttl = ttl; + r->next = search->result; + search->result = r; + search->num_results++; + } +} + +/** + * @brief Called from parser to add PTR data to search result + */ +mdns_result_t *mdns_priv_query_result_add_ptr(mdns_search_once_t *search, const char *instance, + const char *service_type, const char *proto, mdns_if_t tcpip_if, + mdns_ip_protocol_t ip_protocol, uint32_t ttl) +{ + mdns_result_t *r = search->result; + while (r) { + if (r->esp_netif == mdns_priv_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !mdns_utils_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) { + mdns_priv_query_update_result_ttl(r, ttl); + return r; + } + r = r->next; + } + if (!search->max_results || search->num_results < search->max_results) { + r = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!r) { + HOOK_MALLOC_FAILED; + return NULL; + } + + memset(r, 0, sizeof(mdns_result_t)); + r->instance_name = mdns_mem_strdup(instance); + r->service_type = mdns_mem_strdup(service_type); + r->proto = mdns_mem_strdup(proto); + if (!r->instance_name) { + mdns_mem_free(r); + return NULL; + } + + r->esp_netif = mdns_priv_get_esp_netif(tcpip_if); + r->ip_protocol = ip_protocol; + r->ttl = ttl; + r->next = search->result; + search->result = r; + search->num_results++; + return r; + } + return NULL; +} + +mdns_ip_addr_t *mdns_priv_result_addr_create_ip(esp_ip_addr_t *ip) +{ + mdns_ip_addr_t *a = (mdns_ip_addr_t *)mdns_mem_malloc(sizeof(mdns_ip_addr_t)); + if (!a) { + HOOK_MALLOC_FAILED; + return NULL; + } + memset(a, 0, sizeof(mdns_ip_addr_t)); + a->addr.type = ip->type; + if (ip->type == ESP_IPADDR_TYPE_V6) { + memcpy(a->addr.u_addr.ip6.addr, ip->u_addr.ip6.addr, 16); + } else { + a->addr.u_addr.ip4.addr = ip->u_addr.ip4.addr; + } + return a; +} + +/** + * @brief MDNS_PUBLIC_API + * */ +void mdns_query_results_free(mdns_result_t *results) +{ + mdns_priv_service_lock(); + mdns_priv_query_results_free(results); + mdns_priv_service_unlock(); +} + +esp_err_t mdns_query_async_delete(mdns_search_once_t *search) +{ + if (!search) { + return ESP_ERR_INVALID_ARG; + } + if (search->state != SEARCH_OFF) { + return ESP_ERR_INVALID_STATE; + } + + mdns_priv_service_lock(); + search_free(search); + mdns_priv_service_unlock(); + + return ESP_OK; +} + +bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results) +{ + if (xSemaphoreTake(search->done_semaphore, pdMS_TO_TICKS(timeout)) == pdTRUE) { + if (results) { + *results = search->result; + } + if (num_results) { + *num_results = search->num_results; + } + return true; + } + return false; +} + +mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type, + uint32_t timeout, size_t max_results, mdns_query_notify_t notifier) +{ + mdns_search_once_t *search = NULL; + + if (!mdns_priv_is_server_init() || !timeout || mdns_utils_str_null_or_empty(service) != mdns_utils_str_null_or_empty(proto)) { + return NULL; + } + + search = search_init(name, service, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, notifier); + if (!search) { + return NULL; + } + + if (send_search_action(ACTION_SEARCH_ADD, search)) { + search_free(search); + return NULL; + } + + return search; +} + +esp_err_t mdns_query_generic(const char *name, const char *service, const char *proto, uint16_t type, mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results) +{ + mdns_search_once_t *search = NULL; + + *results = NULL; + + if (!mdns_priv_is_server_init()) { + return ESP_ERR_INVALID_STATE; + } + + if (!timeout || mdns_utils_str_null_or_empty(service) != mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + + search = search_init(name, service, proto, type, transmission_type == MDNS_QUERY_UNICAST, timeout, max_results, + NULL); + if (!search) { + return ESP_ERR_NO_MEM; + } + + if (send_search_action(ACTION_SEARCH_ADD, search)) { + search_free(search); + return ESP_ERR_NO_MEM; + } + xSemaphoreTake(search->done_semaphore, portMAX_DELAY); + + *results = search->result; + search_free(search); + + return ESP_OK; +} + +esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results) +{ + return mdns_query_generic(name, service_type, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, results); +} + +esp_err_t mdns_query_ptr(const char *service, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results) +{ + if (mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + + return mdns_query(NULL, service, proto, MDNS_TYPE_PTR, timeout, max_results, results); +} + +esp_err_t mdns_query_srv(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result) +{ + if (mdns_utils_str_null_or_empty(instance) || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + + return mdns_query(instance, service, proto, MDNS_TYPE_SRV, timeout, 1, result); +} + +esp_err_t mdns_query_txt(const char *instance, const char *service, const char *proto, uint32_t timeout, mdns_result_t **result) +{ + if (mdns_utils_str_null_or_empty(instance) || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + + return mdns_query(instance, service, proto, MDNS_TYPE_TXT, timeout, 1, result); +} + +#ifdef CONFIG_LWIP_IPV4 +esp_err_t mdns_query_a(const char *name, uint32_t timeout, esp_ip4_addr_t *addr) +{ + mdns_result_t *result = NULL; + esp_err_t err; + + if (mdns_utils_str_null_or_empty(name)) { + return ESP_ERR_INVALID_ARG; + } + + if (strstr(name, ".local")) { + ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain"); + } + + err = mdns_query(name, NULL, NULL, MDNS_TYPE_A, timeout, 1, &result); + + if (err) { + return err; + } + + if (!result) { + return ESP_ERR_NOT_FOUND; + } + + mdns_ip_addr_t *a = result->addr; + while (a) { + if (a->addr.type == ESP_IPADDR_TYPE_V4) { + addr->addr = a->addr.u_addr.ip4.addr; + mdns_query_results_free(result); + return ESP_OK; + } + a = a->next; + } + + mdns_query_results_free(result); + return ESP_ERR_NOT_FOUND; +} +#endif /* CONFIG_LWIP_IPV4 */ + +#ifdef CONFIG_LWIP_IPV6 +esp_err_t mdns_query_aaaa(const char *name, uint32_t timeout, esp_ip6_addr_t *addr) +{ + mdns_result_t *result = NULL; + esp_err_t err; + + if (mdns_utils_str_null_or_empty(name)) { + return ESP_ERR_INVALID_ARG; + } + + if (strstr(name, ".local")) { + ESP_LOGW(TAG, "Please note that hostname must not contain domain name, as mDNS uses '.local' domain"); + } + + err = mdns_query(name, NULL, NULL, MDNS_TYPE_AAAA, timeout, 1, &result); + + if (err) { + return err; + } + + if (!result) { + return ESP_ERR_NOT_FOUND; + } + + mdns_ip_addr_t *a = result->addr; + while (a) { + if (a->addr.type == ESP_IPADDR_TYPE_V6) { + memcpy(addr->addr, a->addr.u_addr.ip6.addr, 16); + mdns_query_results_free(result); + return ESP_OK; + } + a = a->next; + } + + mdns_query_results_free(result); + return ESP_ERR_NOT_FOUND; +} +#endif /* CONFIG_LWIP_IPV6 */ diff --git a/components/mdns/mdns_receive.c b/components/mdns/mdns_receive.c new file mode 100644 index 0000000000..bd79a1346e --- /dev/null +++ b/components/mdns/mdns_receive.c @@ -0,0 +1,1281 @@ + +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "mdns_private.h" +#include "mdns_networking.h" +#include "mdns.h" +#include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_debug.h" +#include "mdns_netif.h" +#include "mdns_send.h" +#include "mdns_browser.h" +#include "mdns_querier.h" +#include "mdns_pcb.h" +#include "mdns_responder.h" + +static const char *TAG = "mdns_receive"; + +/** + * @brief Check if parsed name is discovery + */ +static bool is_discovery(mdns_name_t *name, uint16_t type) +{ + return ( + (name->host[0] && !strcasecmp(name->host, "_services")) + && (name->service[0] && !strcasecmp(name->service, "_dns-sd")) + && (name->proto[0] && !strcasecmp(name->proto, "_udp")) + && (name->domain[0] && !strcasecmp(name->domain, MDNS_UTILS_DEFAULT_DOMAIN)) + && type == MDNS_TYPE_PTR + ); +} + +static mdns_srv_item_t *get_service_item_subtype(const char *subtype, const char *service, const char *proto) +{ + mdns_srv_item_t *s = mdns_priv_get_services(); + while (s) { + if (mdns_utils_service_match(s->service, service, proto, NULL)) { + mdns_subtype_t *subtype_item = s->service->subtype; + while (subtype_item) { + if (!strcasecmp(subtype_item->subtype, subtype)) { + return s; + } + subtype_item = subtype_item->next; + } + } + s = s->next; + } + return NULL; +} + +/** + * @brief Check if the parsed name is ours (matches service or host name) + */ +static bool is_ours(mdns_name_t *name) +{ + //domain have to be "local" + if (mdns_utils_str_null_or_empty(name->domain) || (strcasecmp(name->domain, MDNS_UTILS_DEFAULT_DOMAIN) +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + && strcasecmp(name->domain, "arpa") +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + )) { + return false; + } + + //if service and proto are empty, host must match out hostname + if (mdns_utils_str_null_or_empty(name->service) && mdns_utils_str_null_or_empty(name->proto)) { + if (!mdns_utils_str_null_or_empty(name->host) + && !mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname()) + && mdns_utils_hostname_is_ours(name->host)) { + return true; + } + return false; + } + + //if service or proto is empty, name is invalid + if (mdns_utils_str_null_or_empty(name->service) || mdns_utils_str_null_or_empty(name->proto)) { + return false; + } + + + //find the service + mdns_srv_item_t *service; + if (name->sub) { + service = get_service_item_subtype(name->host, name->service, name->proto); + } else if (mdns_utils_str_null_or_empty(name->host)) { + service = mdns_utils_get_service_item(name->service, name->proto, NULL); + } else { + service = mdns_utils_get_service_item_instance(name->host, name->service, name->proto, NULL); + } + if (!service) { + return false; + } + + //if query is PTR query and we have service, we have success + if (name->sub || mdns_utils_str_null_or_empty(name->host)) { + return true; + } + + //OK we have host in the name. find what is the instance of the service + const char *instance = mdns_utils_get_service_instance_name(service->service); + if (instance == NULL) { + return false; + } + + //compare the instance against the name + if (strcasecmp(name->host, instance) == 0) { + return true; + } + + return false; +} + +/** + * @brief Duplicate string or return error + */ +static esp_err_t strdup_check(char **out, char *in) +{ + if (in && in[0]) { + *out = mdns_mem_strdup(in); + if (!*out) { + return ESP_FAIL; + } + return ESP_OK; + } + *out = NULL; + return ESP_OK; +} + +/* + * @brief Appends/increments a number to name/instance in case of collision + * */ +static char *mangle_name(char *in) +{ + char *p = strrchr(in, '-'); + int suffix = 0; + if (p == NULL) { + //No - in ``in`` + suffix = 2; + } else { + char *endp = NULL; + suffix = strtol(p + 1, &endp, 10); + if (*endp != 0) { + //suffix is not numerical + suffix = 2; + p = NULL; //so we append -suffix to the entire string + } + } + char *ret; + if (p == NULL) { + //need to add -2 to string + ret = mdns_mem_malloc(strlen(in) + 3); + if (ret == NULL) { + HOOK_MALLOC_FAILED; + return NULL; + } + sprintf(ret, "%s-2", in); + } else { + size_t in_len = strlen(in); + ret = mdns_mem_malloc(in_len + 2); //one extra byte in case 9-10 or 99-100 etc + if (ret == NULL) { + HOOK_MALLOC_FAILED; + return NULL; + } + memcpy(ret, in, in_len); + int baseLen = p - in; //length of 'bla' in 'bla-123' + //overwrite suffix with new suffix + sprintf(ret + baseLen, "-%d", suffix + 1); + } + return ret; +} + +/** + * @brief Get number of items in TXT parsed data + */ +static int get_txt_items_count(const uint8_t *data, size_t len) +{ + if (len == 1) { + return 0; + } + + int num_items = 0; + uint16_t i = 0; + size_t partLen = 0; + + while (i < len) { + partLen = data[i++]; + if (!partLen) { + break; + } + if ((i + partLen) > len) { + return -1;//error + } + i += partLen; + num_items++; + } + return num_items; +} + +/** + * @brief Get the length of TXT item's key name + */ +static int get_txt_item_len(const uint8_t *data, size_t len) +{ + if (*data == '=') { + return -1; + } + for (size_t i = 0; i < len; i++) { + if (data[i] == '=') { + return i; + } + } + return len; +} + +/** + * @brief Create TXT result array from parsed TXT data + */ +static void result_txt_create(const uint8_t *data, size_t len, mdns_txt_item_t **out_txt, uint8_t **out_value_len, + size_t *out_count) +{ + *out_txt = NULL; + *out_count = 0; + uint16_t i = 0, y; + size_t part_len = 0; + int num_items = get_txt_items_count(data, len); + if (num_items < 0 || num_items > SIZE_MAX / sizeof(mdns_txt_item_t)) { + // Error: num_items is incorrect (or too large to allocate) + return; + } + + if (!num_items) { + return; + } + + mdns_txt_item_t *txt = (mdns_txt_item_t *)mdns_mem_malloc(sizeof(mdns_txt_item_t) * num_items); + if (!txt) { + HOOK_MALLOC_FAILED; + return; + } + uint8_t *txt_value_len = (uint8_t *)mdns_mem_malloc(num_items); + if (!txt_value_len) { + mdns_mem_free(txt); + HOOK_MALLOC_FAILED; + return; + } + memset(txt, 0, sizeof(mdns_txt_item_t) * num_items); + memset(txt_value_len, 0, num_items); + size_t txt_num = 0; + + while (i < len && txt_num < num_items) { + part_len = data[i++]; + if (!part_len) { + break; + } + + if ((i + part_len) > len) { + goto handle_error;//error + } + + int name_len = get_txt_item_len(data + i, part_len); + if (name_len < 0) { //invalid item (no name) + i += part_len; + continue; + } + char *key = (char *) mdns_mem_malloc(name_len + 1); + if (!key) { + HOOK_MALLOC_FAILED; + goto handle_error;//error + } + + mdns_txt_item_t *t = &txt[txt_num]; + uint8_t *value_len = &txt_value_len[txt_num]; + txt_num++; + + memcpy(key, data + i, name_len); + key[name_len] = 0; + i += name_len + 1; + t->key = key; + + int new_value_len = part_len - name_len - 1; + if (new_value_len > 0) { + char *value = (char *) mdns_mem_malloc(new_value_len + 1); + if (!value) { + HOOK_MALLOC_FAILED; + goto handle_error;//error + } + memcpy(value, data + i, new_value_len); + value[new_value_len] = 0; + *value_len = new_value_len; + i += new_value_len; + t->value = value; + } else { + t->value = NULL; + } + } + + *out_txt = txt; + *out_count = txt_num; + *out_value_len = txt_value_len; + return; + +handle_error : + for (y = 0; y < txt_num; y++) { + mdns_txt_item_t *t = &txt[y]; + mdns_mem_free((char *)t->key); + mdns_mem_free((char *)t->value); + } + mdns_mem_free(txt_value_len); + mdns_mem_free(txt); +} + +#ifdef CONFIG_LWIP_IPV4 +/** + * @brief Detect IPv4 address collision + */ +static int check_a_collision(esp_ip4_addr_t *ip, mdns_if_t tcpip_if) +{ + esp_netif_ip_info_t if_ip_info; + esp_netif_ip_info_t other_ip_info; + if (!ip->addr) { + return 1;//denial! they win + } + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(tcpip_if), &if_ip_info)) { + return 1;//they win + } + int ret = memcmp((uint8_t *)&if_ip_info.ip.addr, (uint8_t *)&ip->addr, sizeof(esp_ip4_addr_t)); + if (ret > 0) { + return -1;//we win + } else if (ret < 0) { + //is it the other interface? + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + if (other_if == MDNS_MAX_INTERFACES) { + return 1;//AP interface! They win + } + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(other_if), &other_ip_info)) { + return 1;//IPv4 not active! They win + } + if (ip->addr != other_ip_info.ip.addr) { + return 1;//IPv4 not ours! They win + } + mdns_priv_pcb_set_duplicate(tcpip_if); + return 2;//they win + } + return 0;//same +} +#endif /* CONFIG_LWIP_IPV4 */ + +#ifdef CONFIG_LWIP_IPV6 +/** + * @brief Detect IPv6 address collision + */ +static int check_aaaa_collision(esp_ip6_addr_t *ip, mdns_if_t tcpip_if) +{ + struct esp_ip6_addr if_ip6; + struct esp_ip6_addr other_ip6; + if (mdns_utils_ipv6_address_is_zero(*ip)) { + return 1;//denial! they win + } + if (esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(tcpip_if), &if_ip6)) { + return 1;//they win + } + int ret = memcmp((uint8_t *)&if_ip6.addr, (uint8_t *)ip->addr, MDNS_UTILS_SIZEOF_IP6_ADDR); + if (ret > 0) { + return -1;//we win + } else if (ret < 0) { + //is it the other interface? + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + if (other_if == MDNS_MAX_INTERFACES) { + return 1;//AP interface! They win + } + if (esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(other_if), &other_ip6)) { + return 1;//IPv6 not active! They win + } + if (memcmp((uint8_t *)&other_ip6.addr, (uint8_t *)ip->addr, MDNS_UTILS_SIZEOF_IP6_ADDR)) { + return 1;//IPv6 not ours! They win + } + mdns_priv_pcb_set_duplicate(tcpip_if); + return 2;//they win + } + return 0;//same +} +#endif /* CONFIG_LWIP_IPV6 */ + +/** + * @brief Detect TXT collision + */ +static int check_txt_collision(mdns_service_t *service, const uint8_t *data, size_t len) +{ + size_t data_len = 0; + if (len <= 1 && service->txt) { // len==0 means incorrect packet (and handled by the packet parser) + // but handled here again to fix clang-tidy warning on VLA "uint8_t our[0];" + return -1;//we win + } else if (len > 1 && !service->txt) { + return 1;//they win + } else if (len <= 1 && !service->txt) { + return 0;//same + } + + mdns_txt_linked_item_t *txt = service->txt; + while (txt) { + data_len += 1 /* record-len */ + strlen(txt->key) + txt->value_len + (txt->value ? 1 : 0 /* "=" */); + txt = txt->next; + } + + if (len > data_len) { + return 1;//they win + } else if (len < data_len) { + return -1;//we win + } + + uint8_t ours[len]; + uint16_t index = 0; + + txt = service->txt; + while (txt) { + mdns_priv_append_one_txt_record_entry(ours, &index, txt); + txt = txt->next; + } + + int ret = memcmp(ours, data, len); + if (ret > 0) { + return -1;//we win + } else if (ret < 0) { + return 1;//they win + } + return 0;//same +} + +/** + * @brief Detect SRV collision + */ +static int check_srv_collision(mdns_service_t *service, uint16_t priority, uint16_t weight, uint16_t port, const char *host, const char *domain) +{ + if (mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + return 0; + } + + size_t our_host_len = strlen(mdns_priv_get_global_hostname()); + size_t our_len = 14 + our_host_len; + + size_t their_host_len = strlen(host); + size_t their_domain_len = strlen(domain); + size_t their_len = 9 + their_host_len + their_domain_len; + + if (their_len > our_len) { + return 1;//they win + } else if (their_len < our_len) { + return -1;//we win + } + + uint16_t our_index = 0; + uint8_t our_data[our_len]; + mdns_utils_append_u16(our_data, &our_index, service->priority); + mdns_utils_append_u16(our_data, &our_index, service->weight); + mdns_utils_append_u16(our_data, &our_index, service->port); + our_data[our_index++] = our_host_len; + memcpy(our_data + our_index, mdns_priv_get_global_hostname(), our_host_len); + our_index += our_host_len; + our_data[our_index++] = 5; + memcpy(our_data + our_index, MDNS_UTILS_DEFAULT_DOMAIN, 5); + our_index += 5; + our_data[our_index++] = 0; + + uint16_t their_index = 0; + uint8_t their_data[their_len]; + mdns_utils_append_u16(their_data, &their_index, priority); + mdns_utils_append_u16(their_data, &their_index, weight); + mdns_utils_append_u16(their_data, &their_index, port); + their_data[their_index++] = their_host_len; + memcpy(their_data + their_index, host, their_host_len); + their_index += their_host_len; + their_data[their_index++] = their_domain_len; + memcpy(their_data + their_index, domain, their_domain_len); + their_index += their_domain_len; + their_data[their_index++] = 0; + + int ret = memcmp(our_data, their_data, our_len); + if (ret > 0) { + return -1;//we win + } else if (ret < 0) { + return 1;//they win + } + return 0;//same +} + +/** + * @brief Check if the parsed name is self-hosted, i.e. we should resolve conflicts + */ +static bool is_name_selfhosted(mdns_name_t *name) +{ + if (mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { // self-hostname needs to be defined + return false; + } + + // hostname only -- check if selfhosted name + if (mdns_utils_str_null_or_empty(name->service) && mdns_utils_str_null_or_empty(name->proto) && + strcasecmp(name->host, mdns_priv_get_global_hostname()) == 0) { + return true; + } + + // service -- check if selfhosted service + mdns_srv_item_t *srv = mdns_utils_get_service_item(name->service, name->proto, NULL); + if (srv && strcasecmp(mdns_priv_get_global_hostname(), srv->service->hostname) == 0) { + return true; + } + return false; +} + +/** + * @brief Called from parser to check if question matches particular service + */ +static bool question_matches(mdns_parsed_question_t *question, uint16_t type, mdns_srv_item_t *service) +{ + if (question->type != type) { + return false; + } + if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) { + return true; + } else if (type == MDNS_TYPE_PTR || type == MDNS_TYPE_SDPTR) { + if (question->service && question->proto && question->domain + && !strcasecmp(service->service->service, question->service) + && !strcasecmp(service->service->proto, question->proto) + && !strcasecmp(MDNS_UTILS_DEFAULT_DOMAIN, question->domain)) { + if (!service->service->instance) { + return true; + } else if (service->service->instance && question->host && !strcasecmp(service->service->instance, question->host)) { + return true; + } + } + } else if (service && (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT)) { + const char *name = mdns_utils_get_service_instance_name(service->service); + if (name && question->host && question->service && question->proto && question->domain + && !strcasecmp(name, question->host) + && !strcasecmp(service->service->service, question->service) + && !strcasecmp(service->service->proto, question->proto) + && !strcasecmp(MDNS_UTILS_DEFAULT_DOMAIN, question->domain)) { + return true; + } + } + + return false; +} + +/** + * @brief Removes saved question from parsed data + */ +static void remove_parsed_question(mdns_parsed_packet_t *parsed_packet, uint16_t type, mdns_srv_item_t *service) +{ + mdns_parsed_question_t *q = parsed_packet->questions; + + if (question_matches(q, type, service)) { + parsed_packet->questions = q->next; + mdns_mem_free(q->host); + mdns_mem_free(q->service); + mdns_mem_free(q->proto); + mdns_mem_free(q->domain); + mdns_mem_free(q); + return; + } + + while (q->next) { + mdns_parsed_question_t *p = q->next; + if (question_matches(p, type, service)) { + q->next = p->next; + mdns_mem_free(p->host); + mdns_mem_free(p->service); + mdns_mem_free(p->proto); + mdns_mem_free(p->domain); + mdns_mem_free(p); + return; + } + q = q->next; + } +} + +/** + * @brief main packet parser + * + * @param packet the packet + */ +static void mdns_parse_packet(mdns_rx_packet_t *packet) +{ + static mdns_name_t n; + mdns_header_t header; + const uint8_t *data = mdns_priv_get_packet_data(packet); + size_t len = mdns_priv_get_packet_len(packet); + const uint8_t *content = data + MDNS_HEAD_LEN; + bool do_not_reply = false; + mdns_search_once_t *search_result = NULL; + mdns_browse_t *browse_result = NULL; + char *browse_result_instance = NULL; + char *browse_result_service = NULL; + char *browse_result_proto = NULL; + mdns_browse_sync_t *out_sync_browse = NULL; + + DBG_RX_PACKET(packet, data, len); + +#ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES + // Check if the packet wasn't sent by us +#ifdef CONFIG_LWIP_IPV4 + if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) { + esp_netif_ip_info_t if_ip_info; + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(packet->tcpip_if), &if_ip_info) == ESP_OK && + memcmp(&if_ip_info.ip.addr, &packet->src.u_addr.ip4.addr, sizeof(esp_ip4_addr_t)) == 0) { + return; + } + } +#endif /* CONFIG_LWIP_IPV4 */ +#ifdef CONFIG_LWIP_IPV6 + if (packet->ip_protocol == MDNS_IP_PROTOCOL_V6) { + struct esp_ip6_addr if_ip6; + if (esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(packet->tcpip_if), &if_ip6) == ESP_OK && + memcmp(&if_ip6, &packet->src.u_addr.ip6, sizeof(esp_ip6_addr_t)) == 0) { + return; + } + } +#endif /* CONFIG_LWIP_IPV6 */ +#endif // CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES + + // Check for the minimum size of mdns packet + if (len <= MDNS_HEAD_ADDITIONAL_OFFSET) { + return; + } + + mdns_parsed_packet_t *parsed_packet = (mdns_parsed_packet_t *)mdns_mem_malloc(sizeof(mdns_parsed_packet_t)); + if (!parsed_packet) { + HOOK_MALLOC_FAILED; + return; + } + memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t)); + + mdns_name_t *name = &n; + memset(name, 0, sizeof(mdns_name_t)); + + header.id = mdns_utils_read_u16(data, MDNS_HEAD_ID_OFFSET); + header.flags = mdns_utils_read_u16(data, MDNS_HEAD_FLAGS_OFFSET); + header.questions = mdns_utils_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET); + header.answers = mdns_utils_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET); + header.servers = mdns_utils_read_u16(data, MDNS_HEAD_SERVERS_OFFSET); + header.additional = mdns_utils_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET); + + if (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) { + mdns_mem_free(parsed_packet); + return; + } + + //if we have not set the hostname, we can not answer questions + if (header.questions && !header.answers && mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + mdns_mem_free(parsed_packet); + return; + } + + parsed_packet->tcpip_if = packet->tcpip_if; + parsed_packet->ip_protocol = packet->ip_protocol; + parsed_packet->multicast = packet->multicast; + parsed_packet->authoritative = (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE); + parsed_packet->distributed = header.flags == MDNS_FLAGS_DISTRIBUTED; + parsed_packet->id = header.id; + esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src); + parsed_packet->src_port = packet->src_port; + parsed_packet->records = NULL; + + if (header.questions) { + uint8_t qs = header.questions; + + while (qs--) { + content = mdns_utils_parse_fqdn(data, content, name, len); + if (!content) { + header.answers = 0; + header.additional = 0; + header.servers = 0; + goto clear_rx_packet; // error + } + + if (content + MDNS_CLASS_OFFSET + 1 >= data + len) { + goto clear_rx_packet; // malformed packet, won't read behind it + } + uint16_t type = mdns_utils_read_u16(content, MDNS_TYPE_OFFSET); + uint16_t mdns_class = mdns_utils_read_u16(content, MDNS_CLASS_OFFSET); + bool unicast = !!(mdns_class & 0x8000); + mdns_class &= 0x7FFF; + content = content + 4; + + if (mdns_class != 0x0001 || name->invalid) { // bad class or invalid name for this question entry + continue; + } + + if (is_discovery(name, type)) { + //service discovery + parsed_packet->discovery = true; + mdns_srv_item_t *a = mdns_priv_get_services(); + while (a) { + mdns_parsed_question_t *question = (mdns_parsed_question_t *)mdns_mem_calloc(1, sizeof(mdns_parsed_question_t)); + if (!question) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + question->next = parsed_packet->questions; + parsed_packet->questions = question; + + question->unicast = unicast; + question->type = MDNS_TYPE_SDPTR; + question->host = NULL; + question->service = mdns_mem_strdup(a->service->service); + question->proto = mdns_mem_strdup(a->service->proto); + question->domain = mdns_mem_strdup(MDNS_UTILS_DEFAULT_DOMAIN); + if (!question->service || !question->proto || !question->domain) { + goto clear_rx_packet; + } + a = a->next; + } + continue; + } + if (!is_ours(name)) { + continue; + } + + if (type == MDNS_TYPE_ANY && !mdns_utils_str_null_or_empty(name->host)) { + parsed_packet->probe = true; + } + + mdns_parsed_question_t *question = (mdns_parsed_question_t *)mdns_mem_calloc(1, sizeof(mdns_parsed_question_t)); + if (!question) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + question->next = parsed_packet->questions; + parsed_packet->questions = question; + + question->unicast = unicast; + question->type = type; + question->sub = name->sub; + if (strdup_check(&(question->host), name->host) + || strdup_check(&(question->service), name->service) + || strdup_check(&(question->proto), name->proto) + || strdup_check(&(question->domain), name->domain)) { + goto clear_rx_packet; + } + } + } + + if (header.questions && !parsed_packet->questions && !parsed_packet->discovery && !header.answers) { + goto clear_rx_packet; + } else if (header.answers || header.servers || header.additional) { + uint16_t recordIndex = 0; + + while (content < (data + len)) { + + content = mdns_utils_parse_fqdn(data, content, name, len); + if (!content) { + goto clear_rx_packet; + } + + if (content + MDNS_LEN_OFFSET + 1 >= data + len) { + goto clear_rx_packet; // malformed packet, won't read behind it + } + uint16_t type = mdns_utils_read_u16(content, MDNS_TYPE_OFFSET); + uint16_t mdns_class = mdns_utils_read_u16(content, MDNS_CLASS_OFFSET); + uint32_t ttl = mdns_utils_read_u32(content, MDNS_TTL_OFFSET); + uint16_t data_len = mdns_utils_read_u16(content, MDNS_LEN_OFFSET); + const uint8_t *data_ptr = content + MDNS_DATA_OFFSET; + mdns_class &= 0x7FFF; + + content = data_ptr + data_len; + if (content > (data + len) || data_len == 0) { + goto clear_rx_packet; + } + + bool discovery = false; + bool ours = false; + mdns_srv_item_t *service = NULL; + mdns_parsed_record_type_t record_type = MDNS_ANSWER; + + if (recordIndex >= (header.answers + header.servers)) { + record_type = MDNS_EXTRA; + } else if (recordIndex >= (header.answers)) { + record_type = MDNS_NS; + } + recordIndex++; + + if (type == MDNS_TYPE_NSEC || type == MDNS_TYPE_OPT) { + //skip NSEC and OPT + continue; + } + + if (parsed_packet->discovery && is_discovery(name, type)) { + discovery = true; + } else if (!name->sub && is_ours(name)) { + ours = true; + if (name->service[0] && name->proto[0]) { + service = mdns_utils_get_service_item(name->service, name->proto, NULL); + } + } else { + if ((header.flags & MDNS_FLAGS_QUERY_REPSONSE) == 0 || record_type == MDNS_NS) { + //skip this record + continue; + } + search_result = mdns_priv_query_find(name, type, packet->tcpip_if, packet->ip_protocol); + browse_result = mdns_priv_browse_find(name, type, packet->tcpip_if, packet->ip_protocol); + if (browse_result) { + if (!out_sync_browse) { + // will be freed in function `browse_sync` + out_sync_browse = (mdns_browse_sync_t *)mdns_mem_malloc(sizeof(mdns_browse_sync_t)); + if (!out_sync_browse) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + out_sync_browse->browse = browse_result; + out_sync_browse->sync_result = NULL; + } + if (!browse_result_service) { + browse_result_service = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!browse_result_service) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + } + memcpy(browse_result_service, browse_result->service, MDNS_NAME_BUF_LEN); + if (!browse_result_proto) { + browse_result_proto = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!browse_result_proto) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + } + memcpy(browse_result_proto, browse_result->proto, MDNS_NAME_BUF_LEN); + if (type == MDNS_TYPE_SRV || type == MDNS_TYPE_TXT) { + if (!browse_result_instance) { + browse_result_instance = (char *)mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!browse_result_instance) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + } + memcpy(browse_result_instance, name->host, MDNS_NAME_BUF_LEN); + } + } + } + + if (type == MDNS_TYPE_PTR) { + if (!mdns_utils_parse_fqdn(data, data_ptr, name, len)) { + continue;//error + } + if (search_result) { + mdns_priv_query_result_add_ptr(search_result, name->host, name->service, name->proto, + packet->tcpip_if, packet->ip_protocol, ttl); + } else if ((discovery || ours) && !name->sub && is_ours(name)) { + if (name->host[0]) { + service = mdns_utils_get_service_item_instance(name->host, name->service, name->proto, NULL); + } else { + service = mdns_utils_get_service_item(name->service, name->proto, NULL); + } + if (discovery && service) { + remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service); + } else if (service && parsed_packet->questions && !parsed_packet->probe) { + remove_parsed_question(parsed_packet, type, service); + } else if (service) { + //check if TTL is more than half of the full TTL value (4500) + if (ttl > (MDNS_ANSWER_PTR_TTL / 2)) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); + } + } + if (service) { + mdns_parsed_record_t *record = mdns_mem_malloc(sizeof(mdns_parsed_record_t)); + if (!record) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + record->next = parsed_packet->records; + parsed_packet->records = record; + record->type = MDNS_TYPE_PTR; + record->record_type = MDNS_ANSWER; + record->ttl = ttl; + record->host = NULL; + record->service = NULL; + record->proto = NULL; + if (name->host[0]) { + record->host = mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!record->host) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + memcpy(record->host, name->host, MDNS_NAME_BUF_LEN); + } + if (name->service[0]) { + record->service = mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!record->service) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + memcpy(record->service, name->service, MDNS_NAME_BUF_LEN); + } + if (name->proto[0]) { + record->proto = mdns_mem_malloc(MDNS_NAME_BUF_LEN); + if (!record->proto) { + HOOK_MALLOC_FAILED; + goto clear_rx_packet; + } + memcpy(record->proto, name->proto, MDNS_NAME_BUF_LEN); + } + } + } + } else if (type == MDNS_TYPE_SRV) { + mdns_result_t *result = NULL; + if (search_result && search_result->type == MDNS_TYPE_PTR) { + result = search_result->result; + while (result) { + if (mdns_priv_get_esp_netif(packet->tcpip_if) == result->esp_netif + && packet->ip_protocol == result->ip_protocol + && result->instance_name && !strcmp(name->host, result->instance_name)) { + break; + } + result = result->next; + } + if (!result) { + result = mdns_priv_query_result_add_ptr(search_result, name->host, name->service, name->proto, + packet->tcpip_if, packet->ip_protocol, ttl); + if (!result) { + continue; + } + } + } + bool is_selfhosted = is_name_selfhosted(name); + if (!mdns_utils_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name, len)) { + continue; + } + if (data_ptr + MDNS_SRV_PORT_OFFSET + 1 >= data + len) { + goto clear_rx_packet; // malformed packet, won't read behind it + } + uint16_t priority = mdns_utils_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET); + uint16_t weight = mdns_utils_read_u16(data_ptr, MDNS_SRV_WEIGHT_OFFSET); + uint16_t port = mdns_utils_read_u16(data_ptr, MDNS_SRV_PORT_OFFSET); + + if (browse_result) { + mdns_priv_browse_result_add_srv(browse_result, name->host, browse_result_instance, + browse_result_service, + browse_result_proto, port, packet->tcpip_if, packet->ip_protocol, + ttl, + out_sync_browse); + } + if (search_result) { + if (search_result->type == MDNS_TYPE_PTR) { + if (!result->hostname) { // assign host/port for this entry only if not previously set + result->port = port; + result->hostname = mdns_mem_strdup(name->host); + } + } else { + mdns_priv_query_result_add_srv(search_result, name->host, port, packet->tcpip_if, + packet->ip_protocol, ttl); + } + } else if (ours) { + if (parsed_packet->questions && !parsed_packet->probe) { + remove_parsed_question(parsed_packet, type, service); + continue; + } else if (parsed_packet->distributed) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); + continue; + } + if (!is_selfhosted) { + continue; + } + //detect collision (-1=won, 0=none, 1=lost) + int col = 0; + if (mdns_class > 1) { + col = 1; + } else if (!mdns_class) { + col = -1; + } else if (service) { // only detect srv collision if service existed + col = check_srv_collision(service->service, priority, weight, port, name->host, name->domain); + } + if (service && col && (parsed_packet->probe || parsed_packet->authoritative)) { + if (col > 0 || !port) { + do_not_reply = true; + if (mdns_priv_pcb_is_probing(packet)) { + mdns_priv_pcb_set_probe_failed(packet); + if (!mdns_utils_str_null_or_empty(service->service->instance)) { + char *new_instance = mangle_name((char *) service->service->instance); + if (new_instance) { + mdns_mem_free((char *)service->service->instance); + service->service->instance = new_instance; + } + mdns_priv_probe_all_pcbs(&service, 1, false, false); + } else if (!mdns_utils_str_null_or_empty(mdns_priv_get_instance())) { + char *new_instance = mangle_name((char *) mdns_priv_get_instance()); + if (new_instance) { + mdns_priv_set_instance(new_instance); + } + mdns_priv_restart_all_pcbs_no_instance(); + } else { + char *new_host = mangle_name((char *) mdns_priv_get_global_hostname()); + if (new_host) { + mdns_priv_remap_self_service_hostname(mdns_priv_get_global_hostname(), new_host); + mdns_priv_set_global_hostname(new_host); + } + mdns_priv_restart_all_pcbs(); + } + } else if (service) { + mdns_priv_send_bye(packet->tcpip_if, packet->ip_protocol, &service, 1, false); + mdns_priv_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, false); + } + } + } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); + } + } + } else if (type == MDNS_TYPE_TXT) { + mdns_txt_item_t *txt = NULL; + uint8_t *txt_value_len = NULL; + size_t txt_count = 0; + + mdns_result_t *result = NULL; + if (browse_result) { + result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); + mdns_priv_browse_result_add_txt(browse_result, browse_result_instance, browse_result_service, + browse_result_proto, + txt, txt_value_len, txt_count, packet->tcpip_if, + packet->ip_protocol, + ttl, out_sync_browse); + } + if (search_result) { + if (search_result->type == MDNS_TYPE_PTR) { + result = search_result->result; + while (result) { + if (mdns_priv_get_esp_netif(packet->tcpip_if) == result->esp_netif + && packet->ip_protocol == result->ip_protocol + && result->instance_name && !strcmp(name->host, result->instance_name)) { + break; + } + result = result->next; + } + if (!result) { + result = mdns_priv_query_result_add_ptr(search_result, name->host, name->service, + name->proto, + packet->tcpip_if, packet->ip_protocol, ttl); + if (!result) { + continue; + } + } + if (!result->txt) { + result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); + if (txt_count) { + result->txt = txt; + result->txt_count = txt_count; + result->txt_value_len = txt_value_len; + } + } + } else { + result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count); + if (txt_count) { + mdns_priv_query_result_add_txt(search_result, txt, txt_value_len, txt_count, + packet->tcpip_if, packet->ip_protocol, ttl); + } + } + } else if (ours) { + if (parsed_packet->questions && !parsed_packet->probe && service) { + remove_parsed_question(parsed_packet, type, service); + continue; + } + if (!is_name_selfhosted(name)) { + continue; + } + //detect collision (-1=won, 0=none, 1=lost) + int col = 0; + if (mdns_class > 1) { + col = 1; + } else if (!mdns_class) { + col = -1; + } else if (service) { // only detect txt collision if service existed + col = check_txt_collision(service->service, data_ptr, data_len); + } + if (col && !mdns_priv_pcb_is_probing(packet) && service) { + do_not_reply = true; + mdns_priv_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, &service, 1, true); + } else if (ttl > (MDNS_ANSWER_TXT_TTL / 2) && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !mdns_priv_pcb_is_probing( + packet)) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, service); + } + } + + } +#ifdef CONFIG_LWIP_IPV6 + else if (type == MDNS_TYPE_AAAA) { // ipv6 + esp_ip_addr_t ip6; + ip6.type = ESP_IPADDR_TYPE_V6; + memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE); + if (browse_result) { + mdns_priv_browse_result_add_ip(browse_result, name->host, &ip6, packet->tcpip_if, + packet->ip_protocol, + ttl, out_sync_browse); + } + if (search_result) { + //check for more applicable searches (PTR & A/AAAA at the same time) + while (search_result) { + mdns_priv_query_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, + packet->ip_protocol, ttl); + search_result = mdns_priv_query_find_from(search_result->next, name, type, packet->tcpip_if, + packet->ip_protocol); + } + } else if (ours) { + if (parsed_packet->questions && !parsed_packet->probe) { + remove_parsed_question(parsed_packet, type, NULL); + continue; + } + if (!is_name_selfhosted(name)) { + continue; + } + //detect collision (-1=won, 0=none, 1=lost) + int col = 0; + if (mdns_class > 1) { + col = 1; + } else if (!mdns_class) { + col = -1; + } else { + col = check_aaaa_collision(&(ip6.u_addr.ip6), packet->tcpip_if); + } + if (col == 2) { + goto clear_rx_packet; + } else if (col == 1) { + do_not_reply = true; + if (mdns_priv_pcb_is_probing(packet)) { + if (col && (parsed_packet->probe || parsed_packet->authoritative)) { + mdns_priv_pcb_set_probe_failed(packet); + char *new_host = mangle_name((char *) mdns_priv_get_global_hostname()); + if (new_host) { + mdns_priv_remap_self_service_hostname(mdns_priv_get_global_hostname(), new_host); + mdns_priv_set_global_hostname(new_host); + } + mdns_priv_restart_all_pcbs(); + } + } else { + mdns_priv_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true); + } + } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !mdns_priv_pcb_is_probing( + packet)) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL); + } + } + + } +#endif /* CONFIG_LWIP_IPV6 */ +#ifdef CONFIG_LWIP_IPV4 + else if (type == MDNS_TYPE_A) { + esp_ip_addr_t ip; + ip.type = ESP_IPADDR_TYPE_V4; + memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4); + if (browse_result) { + mdns_priv_browse_result_add_ip(browse_result, name->host, &ip, packet->tcpip_if, + packet->ip_protocol, + ttl, out_sync_browse); + } + if (search_result) { + //check for more applicable searches (PTR & A/AAAA at the same time) + while (search_result) { + mdns_priv_query_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, + packet->ip_protocol, ttl); + search_result = mdns_priv_query_find_from(search_result->next, name, type, packet->tcpip_if, + packet->ip_protocol); + } + } else if (ours) { + if (parsed_packet->questions && !parsed_packet->probe) { + remove_parsed_question(parsed_packet, type, NULL); + continue; + } + if (!is_name_selfhosted(name)) { + continue; + } + //detect collision (-1=won, 0=none, 1=lost) + int col = 0; + if (mdns_class > 1) { + col = 1; + } else if (!mdns_class) { + col = -1; + } else { + col = check_a_collision(&(ip.u_addr.ip4), packet->tcpip_if); + } + if (col == 2) { + goto clear_rx_packet; + } else if (col == 1) { + do_not_reply = true; + if (mdns_priv_pcb_is_probing(packet)) { + if (col && (parsed_packet->probe || parsed_packet->authoritative)) { + mdns_priv_pcb_set_probe_failed(packet); + char *new_host = mangle_name((char *) mdns_priv_get_global_hostname()); + if (new_host) { + mdns_priv_remap_self_service_hostname(mdns_priv_get_global_hostname(), new_host); + mdns_priv_set_global_hostname(new_host); + } + mdns_priv_restart_all_pcbs(); + } + } else { + mdns_priv_init_pcb_probe(packet->tcpip_if, packet->ip_protocol, NULL, 0, true); + } + } else if (ttl > 60 && !col && !parsed_packet->authoritative && !parsed_packet->probe && !parsed_packet->questions && !mdns_priv_pcb_is_probing( + packet)) { + mdns_priv_remove_scheduled_answer(packet->tcpip_if, packet->ip_protocol, type, NULL); + } + } + + } +#endif /* CONFIG_LWIP_IPV4 */ + } + //end while + if (parsed_packet->authoritative) { + mdns_priv_query_done(); + } + } + + if (!do_not_reply && mdns_priv_pcb_is_after_probing(packet) && (parsed_packet->questions || parsed_packet->discovery)) { + mdns_priv_create_answer_from_parsed_packet(parsed_packet); + } + if (out_sync_browse) { + DBG_BROWSE_RESULTS_WITH_MSG(out_sync_browse->browse->result, + "Browse %s%s total result:", out_sync_browse->browse->service, out_sync_browse->browse->proto); + if (out_sync_browse->sync_result) { + DBG_BROWSE_RESULTS_WITH_MSG(out_sync_browse->sync_result->result, + "Changed result:"); + mdns_priv_browse_sync(out_sync_browse); + } else { + mdns_mem_free(out_sync_browse); + } + out_sync_browse = NULL; + } + +clear_rx_packet: + while (parsed_packet->questions) { + mdns_parsed_question_t *question = parsed_packet->questions; + parsed_packet->questions = parsed_packet->questions->next; + if (question->host) { + mdns_mem_free(question->host); + } + if (question->service) { + mdns_mem_free(question->service); + } + if (question->proto) { + mdns_mem_free(question->proto); + } + if (question->domain) { + mdns_mem_free(question->domain); + } + mdns_mem_free(question); + } + while (parsed_packet->records) { + mdns_parsed_record_t *record = parsed_packet->records; + parsed_packet->records = parsed_packet->records->next; + if (record->host) { + mdns_mem_free(record->host); + } + if (record->service) { + mdns_mem_free(record->service); + } + if (record->proto) { + mdns_mem_free(record->proto); + } + record->next = NULL; + mdns_mem_free(record); + } + mdns_mem_free(parsed_packet); + mdns_mem_free(browse_result_instance); + mdns_mem_free(browse_result_service); + mdns_mem_free(browse_result_proto); + mdns_mem_free(out_sync_browse); +} + +void mdns_priv_receive_action(mdns_action_t *action, mdns_action_subtype_t type) +{ + if (action->type != ACTION_RX_HANDLE) { + abort(); + } + if (type == ACTION_RUN) { + mdns_parse_packet(action->data.rx_handle.packet); + mdns_priv_packet_free(action->data.rx_handle.packet); + } else if (type == ACTION_CLEANUP) { + mdns_priv_packet_free(action->data.rx_handle.packet); + } +} diff --git a/components/mdns/mdns_responder.c b/components/mdns/mdns_responder.c new file mode 100644 index 0000000000..05469212cc --- /dev/null +++ b/components/mdns/mdns_responder.c @@ -0,0 +1,1554 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "esp_log.h" +#include "esp_check.h" +#include "mdns.h" +#include "mdns_private.h" +#include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_send.h" +#include "mdns_querier.h" +#include "mdns_pcb.h" +#include "mdns_service.h" + +typedef struct mdns_server_s { + const char *hostname; + const char *instance; + mdns_srv_item_t *services; + mdns_host_item_t *host_list; + mdns_host_item_t self_host; + SemaphoreHandle_t action_sema; +} mdns_server_t; + +static const char *TAG = "mdns_responder"; +static mdns_server_t *s_server = NULL; + +esp_err_t mdns_priv_responder_init(void) +{ + s_server = (mdns_server_t *)mdns_mem_malloc(sizeof(mdns_server_t)); + if (!s_server) { + HOOK_MALLOC_FAILED; + return ESP_ERR_NO_MEM; + } + memset((uint8_t *)s_server, 0, sizeof(mdns_server_t)); + + s_server->action_sema = xSemaphoreCreateBinary(); + if (!s_server->action_sema) { + mdns_mem_free(s_server); + return ESP_ERR_NO_MEM; + } + + return ESP_OK; +} + +static void free_delegated_hostnames(void) +{ + mdns_host_item_t *host = s_server->host_list; + while (host != NULL) { + mdns_utils_free_address_list(host->address_list); + mdns_mem_free((char *)host->hostname); + mdns_host_item_t *item = host; + host = host->next; + mdns_mem_free(item); + } + s_server->host_list = NULL; +} + +void mdns_priv_responder_free(void) +{ + if (s_server->action_sema != NULL) { + vSemaphoreDelete(s_server->action_sema); + s_server->action_sema = NULL; + } + if (s_server == NULL) { + return; + } + mdns_mem_free((char *)s_server->hostname); + mdns_mem_free((char *)s_server->instance); + free_delegated_hostnames(); + mdns_mem_free(s_server); + s_server = NULL; +} + +const char *mdns_priv_get_global_hostname(void) +{ + return s_server ? s_server->hostname : NULL; +} + +mdns_srv_item_t *mdns_priv_get_services(void) +{ + return s_server ? s_server->services : NULL; +} + +mdns_host_item_t *mdns_priv_get_hosts(void) +{ + return s_server ? s_server->host_list : NULL; +} + +mdns_host_item_t *mdns_priv_get_self_host(void) +{ + return s_server ? &s_server->self_host : NULL; +} + +void mdns_priv_set_global_hostname(const char *hostname) +{ + if (s_server) { + if (s_server->hostname) { + mdns_mem_free((void *)s_server->hostname); + } + s_server->hostname = hostname; + s_server->self_host.hostname = hostname; + } +} + +const char *mdns_priv_get_instance(void) +{ + return s_server ? s_server->instance : NULL; +} + +void mdns_priv_set_instance(const char *instance) +{ + if (s_server) { + if (s_server->instance) { + mdns_mem_free((void *)s_server->instance); + } + s_server->instance = instance; + } +} + +bool mdns_priv_is_server_init(void) +{ + return s_server != NULL; +} + +static bool can_add_more_services(void) +{ +#if MDNS_MAX_SERVICES == 0 + return false; +#else + mdns_srv_item_t *s = s_server->services; + uint16_t service_num = 0; + while (s) { + service_num ++; + s = s->next; + if (service_num >= MDNS_MAX_SERVICES) { + return false; + } + } + return true; +#endif +} + +/** + * @brief Send announcement on all active PCBs + */ +static void announce_all_pcbs(mdns_srv_item_t **services, size_t len, bool include_ip) +{ + uint8_t i, j; + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + mdns_priv_pcb_announce((mdns_if_t) i, (mdns_ip_protocol_t) j, services, len, include_ip); + } + } +} + +/** + * @brief Send bye to all services + */ +static void send_final_bye(bool include_ip) +{ + //collect all services to send bye packet + size_t srv_count = 0; + mdns_srv_item_t *a = s_server->services; + while (a) { + srv_count++; + a = a->next; + } + if (!srv_count) { + return; + } + mdns_srv_item_t *services[srv_count]; + size_t i = 0; + a = s_server->services; + while (a) { + services[i++] = a; + a = a->next; + } + mdns_priv_pcb_send_bye_service(services, srv_count, include_ip); +} + +/** + * @brief Stop the responder on all services without instance + */ +static void send_bye_all_pcbs_no_instance(bool include_ip) +{ + size_t srv_count = 0; + mdns_srv_item_t *a = s_server->services; + while (a) { + if (!a->service->instance) { + srv_count++; + } + a = a->next; + } + if (!srv_count) { + return; + } + mdns_srv_item_t *services[srv_count]; + size_t i = 0; + a = s_server->services; + while (a) { + if (!a->service->instance) { + services[i++] = a; + } + a = a->next; + } + mdns_priv_pcb_send_bye_service(services, srv_count, include_ip); +} + +void mdns_priv_restart_all_pcbs_no_instance(void) +{ + size_t srv_count = 0; + mdns_srv_item_t *a = s_server->services; + while (a) { + if (!a->service->instance) { + srv_count++; + } + a = a->next; + } + if (!srv_count) { + return; + } + mdns_srv_item_t *services[srv_count]; + size_t i = 0; + a = s_server->services; + while (a) { + if (!a->service->instance) { + services[i++] = a; + } + a = a->next; + } + mdns_priv_probe_all_pcbs(services, srv_count, false, true); +} + +void mdns_priv_restart_all_pcbs(void) +{ + mdns_priv_clear_tx_queue(); + size_t srv_count = 0; + mdns_srv_item_t *a = s_server->services; + while (a) { + srv_count++; + a = a->next; + } + if (srv_count == 0) { + mdns_priv_probe_all_pcbs(NULL, 0, true, true); + return; + } + mdns_srv_item_t *services[srv_count]; + size_t l = 0; + a = s_server->services; + while (a) { + services[l++] = a; + a = a->next; + } + + mdns_priv_probe_all_pcbs(services, srv_count, true, true); +} + +/** + * @brief creates/allocates new text item list + * @param num_items service number of txt items or 0 + * @param txt service txt items array or NULL + * + * @return pointer to the linked txt item list or NULL + */ +static mdns_txt_linked_item_t *allocate_txt(size_t num_items, mdns_txt_item_t txt[]) +{ + mdns_txt_linked_item_t *new_txt = NULL; + size_t i = 0; + if (num_items) { + for (i = 0; i < num_items; i++) { + mdns_txt_linked_item_t *new_item = (mdns_txt_linked_item_t *)mdns_mem_malloc(sizeof(mdns_txt_linked_item_t)); + if (!new_item) { + HOOK_MALLOC_FAILED; + break; + } + new_item->key = mdns_mem_strdup(txt[i].key); + if (!new_item->key) { + mdns_mem_free(new_item); + break; + } + new_item->value = mdns_mem_strdup(txt[i].value); + if (!new_item->value) { + mdns_mem_free((char *)new_item->key); + mdns_mem_free(new_item); + break; + } + new_item->value_len = strlen(new_item->value); + new_item->next = new_txt; + new_txt = new_item; + } + } + return new_txt; +} + +/** + * @brief Deallocate the txt linked list + * @param txt pointer to the txt pointer to free, noop if txt==NULL + */ +static void free_linked_txt(mdns_txt_linked_item_t *txt) +{ + mdns_txt_linked_item_t *t; + while (txt) { + t = txt; + txt = txt->next; + mdns_mem_free((char *)t->value); + mdns_mem_free((char *)t->key); + mdns_mem_free(t); + } +} + +/** + * @brief creates/allocates new service + * @param service service type + * @param proto service proto + * @param hostname service hostname + * @param port service port + * @param instance service instance + * @param num_items service number of txt items or 0 + * @param txt service txt items array or NULL + * + * @return pointer to the service or NULL on error + */ +static mdns_service_t *create_service(const char *service, const char *proto, const char *hostname, + uint16_t port, const char *instance, size_t num_items, + mdns_txt_item_t txt[]) +{ + mdns_service_t *s = (mdns_service_t *)mdns_mem_calloc(1, sizeof(mdns_service_t)); + if (!s) { + HOOK_MALLOC_FAILED; + return NULL; + } + + mdns_txt_linked_item_t *new_txt = allocate_txt(num_items, txt); + if (num_items && new_txt == NULL) { + goto fail; + } + + s->priority = 0; + s->weight = 0; + s->instance = instance ? mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1) : NULL; + s->txt = new_txt; + s->port = port; + s->subtype = NULL; + + if (hostname) { + s->hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!s->hostname) { + goto fail; + } + } else { + s->hostname = NULL; + } + + s->service = mdns_mem_strndup(service, MDNS_NAME_BUF_LEN - 1); + if (!s->service) { + goto fail; + } + + s->proto = mdns_mem_strndup(proto, MDNS_NAME_BUF_LEN - 1); + if (!s->proto) { + goto fail; + } + return s; + +fail: + free_linked_txt(s->txt); + mdns_mem_free((char *)s->instance); + mdns_mem_free((char *)s->service); + mdns_mem_free((char *)s->proto); + mdns_mem_free((char *)s->hostname); + mdns_mem_free(s); + + return NULL; +} + +static void free_subtype(mdns_subtype_t *subtype) +{ + while (subtype) { + mdns_subtype_t *next = subtype->next; + mdns_mem_free((char *)subtype->subtype); + mdns_mem_free(subtype); + subtype = next; + } +} + +static void free_service_subtype(mdns_service_t *service) +{ + free_subtype(service->subtype); + service->subtype = NULL; +} + +/** + * @brief free service memory + * + * @param service the service + */ +static void free_service(mdns_service_t *service) +{ + if (!service) { + return; + } + mdns_mem_free((char *)service->instance); + mdns_mem_free((char *)service->service); + mdns_mem_free((char *)service->proto); + mdns_mem_free((char *)service->hostname); + while (service->txt) { + mdns_txt_linked_item_t *s = service->txt; + service->txt = service->txt->next; + mdns_mem_free((char *)s->key); + mdns_mem_free((char *)s->value); + mdns_mem_free(s); + } + free_service_subtype(service); + mdns_mem_free(service); +} + +bool mdns_priv_delegate_hostname_add(const char *hostname, mdns_ip_addr_t *address_list) +{ + if (mdns_utils_hostname_is_ours(hostname)) { + return false; + } + + mdns_host_item_t *host = (mdns_host_item_t *)mdns_mem_malloc(sizeof(mdns_host_item_t)); + + if (host == NULL) { + return false; + } + host->address_list = address_list; + host->hostname = hostname; + host->next = s_server->host_list; + s_server->host_list = host; + return true; +} + +static bool delegate_hostname_set_address(const char *hostname, mdns_ip_addr_t *address_list) +{ + if (!mdns_utils_str_null_or_empty(s_server->hostname) && + strcasecmp(hostname, s_server->hostname) == 0) { + return false; + } + mdns_host_item_t *host = s_server->host_list; + while (host != NULL) { + if (strcasecmp(hostname, host->hostname) == 0) { + // free previous address list + mdns_utils_free_address_list(host->address_list); + // set current address list to the host + host->address_list = address_list; + return true; + } + host = host->next; + } + return false; +} + +static bool delegate_hostname_remove(const char *hostname) +{ + mdns_srv_item_t *srv = s_server->services; + mdns_srv_item_t *prev_srv = NULL; + while (srv) { + if (strcasecmp(srv->service->hostname, hostname) == 0) { + mdns_srv_item_t *to_free = srv; + mdns_priv_pcb_send_bye_service(&srv, 1, false); + mdns_priv_remove_scheduled_service_packets(srv->service); + if (prev_srv == NULL) { + s_server->services = srv->next; + srv = srv->next; + } else { + prev_srv->next = srv->next; + srv = srv->next; + } + free_service(to_free->service); + mdns_mem_free(to_free); + } else { + prev_srv = srv; + srv = srv->next; + } + } + mdns_host_item_t *host = s_server->host_list; + mdns_host_item_t *prev_host = NULL; + while (host != NULL) { + if (strcasecmp(hostname, host->hostname) == 0) { + if (prev_host == NULL) { + s_server->host_list = host->next; + } else { + prev_host->next = host->next; + } + mdns_utils_free_address_list(host->address_list); + mdns_mem_free((char *)host->hostname); + mdns_mem_free(host); + break; + } else { + prev_host = host; + host = host->next; + } + } + return true; +} + +void mdns_priv_remap_self_service_hostname(const char *old_hostname, const char *new_hostname) +{ + mdns_srv_item_t *service = s_server->services; + + while (service) { + if (service->service->hostname && + strcmp(service->service->hostname, old_hostname) == 0) { + mdns_mem_free((char *)service->service->hostname); + service->service->hostname = mdns_mem_strdup(new_hostname); + } + service = service->next; + } +} + +void mdns_priv_responder_action(mdns_action_t *action, mdns_action_subtype_t type) +{ + if (type == ACTION_RUN) { + switch (action->type) { + case ACTION_HOSTNAME_SET: + send_bye_all_pcbs_no_instance(true); + mdns_priv_remap_self_service_hostname(s_server->hostname, action->data.hostname_set.hostname); + mdns_mem_free((char *)s_server->hostname); + s_server->hostname = action->data.hostname_set.hostname; + s_server->self_host.hostname = action->data.hostname_set.hostname; + mdns_priv_restart_all_pcbs(); + xSemaphoreGive(s_server->action_sema); + break; + case ACTION_INSTANCE_SET: + send_bye_all_pcbs_no_instance(false); + mdns_mem_free((char *)s_server->instance); + s_server->instance = action->data.instance; + mdns_priv_restart_all_pcbs_no_instance(); + break; + case ACTION_DELEGATE_HOSTNAME_ADD: + if (!mdns_priv_delegate_hostname_add(action->data.delegate_hostname.hostname, + action->data.delegate_hostname.address_list)) { + mdns_mem_free((char *)action->data.delegate_hostname.hostname); + mdns_utils_free_address_list(action->data.delegate_hostname.address_list); + } + xSemaphoreGive(s_server->action_sema); + break; + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + if (!delegate_hostname_set_address(action->data.delegate_hostname.hostname, + action->data.delegate_hostname.address_list)) { + mdns_utils_free_address_list(action->data.delegate_hostname.address_list); + } + mdns_mem_free((char *)action->data.delegate_hostname.hostname); + break; + case ACTION_DELEGATE_HOSTNAME_REMOVE: + delegate_hostname_remove(action->data.delegate_hostname.hostname); + mdns_mem_free((char *)action->data.delegate_hostname.hostname); + break; + default: + abort(); + } + return; + } + if (type == ACTION_CLEANUP) { + switch (action->type) { + case ACTION_HOSTNAME_SET: + mdns_mem_free(action->data.hostname_set.hostname); + break; + case ACTION_INSTANCE_SET: + mdns_mem_free(action->data.instance); + break; + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + case ACTION_DELEGATE_HOSTNAME_ADD: + mdns_mem_free((char *)action->data.delegate_hostname.hostname); + mdns_utils_free_address_list(action->data.delegate_hostname.address_list); + break; + case ACTION_DELEGATE_HOSTNAME_REMOVE: + mdns_mem_free((char *)action->data.delegate_hostname.hostname); + break; + default: + abort(); + } + return; + } +} + +/** + * @ingroup PUBLIC_API + */ +esp_err_t mdns_hostname_set(const char *hostname) +{ + if (!s_server) { + return ESP_ERR_INVALID_ARG; + } + if (mdns_utils_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!new_hostname) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + mdns_mem_free(new_hostname); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_HOSTNAME_SET; + action->data.hostname_set.hostname = new_hostname; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(new_hostname); + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + xSemaphoreTake(s_server->action_sema, portMAX_DELAY); + return ESP_OK; +} + +esp_err_t mdns_hostname_get(char *hostname) +{ + if (!hostname) { + return ESP_ERR_INVALID_ARG; + } + + if (!s_server || !s_server->hostname) { + return ESP_ERR_INVALID_STATE; + } + + mdns_priv_service_lock(); + size_t len = strnlen(s_server->hostname, MDNS_NAME_BUF_LEN - 1); + strncpy(hostname, s_server->hostname, len); + hostname[len] = 0; + mdns_priv_service_unlock(); + return ESP_OK; +} + +esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (mdns_utils_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!new_hostname) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + mdns_mem_free(new_hostname); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_DELEGATE_HOSTNAME_ADD; + action->data.delegate_hostname.hostname = new_hostname; + action->data.delegate_hostname.address_list = mdns_utils_copy_address_list(address_list); + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(new_hostname); + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + xSemaphoreTake(s_server->action_sema, portMAX_DELAY); + return ESP_OK; +} + +esp_err_t mdns_delegate_hostname_remove(const char *hostname) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (mdns_utils_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!new_hostname) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + mdns_mem_free(new_hostname); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_DELEGATE_HOSTNAME_REMOVE; + action->data.delegate_hostname.hostname = new_hostname; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(new_hostname); + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (mdns_utils_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_hostname = mdns_mem_strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!new_hostname) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + mdns_mem_free(new_hostname); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_DELEGATE_HOSTNAME_SET_ADDR; + action->data.delegate_hostname.hostname = new_hostname; + action->data.delegate_hostname.address_list = mdns_utils_copy_address_list(address_list); + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(new_hostname); + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +bool mdns_hostname_exists(const char *hostname) +{ + bool ret = false; + mdns_priv_service_lock(); + ret = mdns_utils_hostname_is_ours(hostname); + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_instance_name_set(const char *instance) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (mdns_utils_str_null_or_empty(instance) || s_server->hostname == NULL || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_instance = mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1); + if (!new_instance) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + mdns_mem_free(new_instance); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_INSTANCE_SET; + action->data.instance = new_instance; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(new_instance); + mdns_mem_free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *host, + uint16_t port, mdns_txt_item_t txt[], size_t num_items) +{ + if (!s_server || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto) || !s_server->hostname) { + return ESP_ERR_INVALID_ARG; + } + + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + mdns_service_t *s = NULL; + + ESP_GOTO_ON_FALSE(can_add_more_services(), ESP_ERR_NO_MEM, err, TAG, + "Cannot add more services, please increase CONFIG_MDNS_MAX_SERVICES (%d)", CONFIG_MDNS_MAX_SERVICES); + + mdns_srv_item_t *item = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(!item, ESP_ERR_INVALID_ARG, err, TAG, "Service already exists"); + + s = create_service(service, proto, hostname, port, instance, num_items, txt); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory"); + + item = (mdns_srv_item_t *)mdns_mem_malloc(sizeof(mdns_srv_item_t)); + ESP_GOTO_ON_FALSE(item, ESP_ERR_NO_MEM, err, TAG, "Cannot create service: Out of memory"); + + item->service = s; + item->next = NULL; + + item->next = s_server->services; + s_server->services = item; + mdns_priv_probe_all_pcbs(&item, 1, false, false); + mdns_priv_service_unlock(); + return ESP_OK; + +err: + mdns_priv_service_unlock(); + free_service(s); + if (ret == ESP_ERR_NO_MEM) { + HOOK_MALLOC_FAILED; + } + return ret; +} + +esp_err_t mdns_service_add(const char *instance, const char *service, const char *proto, uint16_t port, + mdns_txt_item_t txt[], size_t num_items) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_add_for_host(instance, service, proto, NULL, port, txt, num_items); +} + +bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname) +{ + bool ret = false; + mdns_priv_service_lock(); + ret = mdns_utils_get_service_item(service_type, proto, hostname) != NULL; + mdns_priv_service_unlock(); + return ret; +} + +bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto, + const char *hostname) +{ + bool ret = false; + mdns_priv_service_lock(); + ret = mdns_utils_get_service_item_instance(instance, service_type, proto, hostname) != NULL; + mdns_priv_service_unlock(); + return ret; +} + +static mdns_txt_item_t *copy_txt_items(mdns_txt_linked_item_t *items, uint8_t **txt_value_len, size_t *txt_count) +{ + mdns_txt_item_t *ret = NULL; + size_t ret_index = 0; + for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) { + ret_index++; + } + *txt_count = ret_index; + if (ret_index == 0) { // handle empty TXT + *txt_value_len = NULL; + return NULL; + } + ret = (mdns_txt_item_t *)mdns_mem_calloc(ret_index, sizeof(mdns_txt_item_t)); + *txt_value_len = (uint8_t *)mdns_mem_calloc(ret_index, sizeof(uint8_t)); + if (!ret || !(*txt_value_len)) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + ret_index = 0; + for (mdns_txt_linked_item_t *tmp = items; tmp != NULL; tmp = tmp->next) { + size_t key_len = strlen(tmp->key); + char *key = (char *)mdns_mem_malloc(key_len + 1); + if (!key) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + memcpy(key, tmp->key, key_len); + key[key_len] = 0; + ret[ret_index].key = key; + char *value = (char *)mdns_mem_malloc(tmp->value_len + 1); + if (!value) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + memcpy(value, tmp->value, tmp->value_len); + value[tmp->value_len] = 0; + ret[ret_index].value = value; + (*txt_value_len)[ret_index] = tmp->value_len; + ret_index++; + } + return ret; + +handle_error: + for (size_t y = 0; y < ret_index + 1 && ret != NULL; y++) { + mdns_txt_item_t *t = &ret[y]; + mdns_mem_free((char *)t->key); + mdns_mem_free((char *)t->value); + } + mdns_mem_free(*txt_value_len); + mdns_mem_free(ret); + return NULL; +} + +static mdns_ip_addr_t *copy_delegated_host_address_list(char *hostname) +{ + mdns_host_item_t *host = s_server->host_list; + while (host) { + if (strcasecmp(host->hostname, hostname) == 0) { + return mdns_utils_copy_address_list(host->address_list); + } + host = host->next; + } + return NULL; +} + +static mdns_result_t *lookup_service(const char *instance, const char *service, const char *proto, size_t max_results, bool selfhost) +{ + if (mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return NULL; + } + mdns_result_t *results = NULL; + size_t num_results = 0; + mdns_srv_item_t *s = s_server->services; + while (s) { + mdns_service_t *srv = s->service; + if (!srv || !srv->hostname) { + s = s->next; + continue; + } + bool is_service_selfhosted = !mdns_utils_str_null_or_empty(s_server->hostname) && !strcasecmp(s_server->hostname, srv->hostname); + bool is_service_delegated = mdns_utils_str_null_or_empty(s_server->hostname) || strcasecmp(s_server->hostname, srv->hostname); + if ((selfhost && is_service_selfhosted) || (!selfhost && is_service_delegated)) { + if (!strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) && + (mdns_utils_str_null_or_empty(instance) || mdns_utils_instance_name_match(srv->instance, instance))) { + mdns_result_t *item = (mdns_result_t *)mdns_mem_malloc(sizeof(mdns_result_t)); + if (!item) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + item->next = results; + results = item; + item->esp_netif = NULL; + item->ttl = mdns_utils_str_null_or_empty(instance) ? MDNS_ANSWER_PTR_TTL : MDNS_ANSWER_SRV_TTL; + item->ip_protocol = MDNS_IP_PROTOCOL_MAX; + if (srv->instance) { + item->instance_name = mdns_mem_strndup(srv->instance, MDNS_NAME_BUF_LEN - 1); + if (!item->instance_name) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + } else { + item->instance_name = NULL; + } + item->service_type = mdns_mem_strndup(srv->service, MDNS_NAME_BUF_LEN - 1); + if (!item->service_type) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + item->proto = mdns_mem_strndup(srv->proto, MDNS_NAME_BUF_LEN - 1); + if (!item->proto) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + item->hostname = mdns_mem_strndup(srv->hostname, MDNS_NAME_BUF_LEN - 1); + if (!item->hostname) { + HOOK_MALLOC_FAILED; + goto handle_error; + } + item->port = srv->port; + item->txt = copy_txt_items(srv->txt, &(item->txt_value_len), &(item->txt_count)); + // We should not append addresses for selfhost lookup result as we don't know which interface's address to append. + if (selfhost) { + item->addr = NULL; + } else { + item->addr = copy_delegated_host_address_list(item->hostname); + if (!item->addr) { + goto handle_error; + } + } + if (num_results < max_results) { + num_results++; + } + if (num_results >= max_results) { + break; + } + } + } + s = s->next; + } + return results; +handle_error: + mdns_priv_query_results_free(results); + return NULL; +} + +esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service, const char *proto, const char *host, uint16_t port) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && port, + ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + s->service->port = port; + announce_all_pcbs(&s, 1, true); + +err: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_port_set(const char *service, const char *proto, uint16_t port) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_port_set_for_host(NULL, service, proto, NULL, port); +} + +esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *host, + mdns_txt_item_t txt_items[], uint8_t num_items) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && !(num_items && txt_items == NULL), + ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + mdns_txt_linked_item_t *new_txt = NULL; + if (num_items) { + new_txt = allocate_txt(num_items, txt_items); + if (!new_txt) { + return ESP_ERR_NO_MEM; + } + } + mdns_service_t *srv = s->service; + mdns_txt_linked_item_t *txt = srv->txt; + srv->txt = NULL; + free_linked_txt(txt); + srv->txt = new_txt; + announce_all_pcbs(&s, 1, false); + +err: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_txt_set(const char *service, const char *proto, mdns_txt_item_t txt[], uint8_t num_items) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_txt_set_for_host(NULL, service, proto, NULL, txt, num_items); +} + +esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service, const char *proto, + const char *host, const char *key, const char *value_arg, uint8_t value_len) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + char *value = NULL; + mdns_txt_linked_item_t *new_txt = NULL; + const char *hostname = host ? host : s_server->hostname; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && !mdns_utils_str_null_or_empty(key) && + !((!value_arg && value_len)), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + mdns_service_t *srv = s->service; + if (value_len > 0) { + value = (char *) mdns_mem_malloc(value_len); + ESP_GOTO_ON_FALSE(value, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + memcpy(value, value_arg, value_len); + } else { + value_len = 0; + } + mdns_txt_linked_item_t *txt = srv->txt; + while (txt) { + if (strcmp(txt->key, key) == 0) { + mdns_mem_free((char *)txt->value); + txt->value = value; + txt->value_len = value_len; + break; + } + txt = txt->next; + } + if (!txt) { + new_txt = (mdns_txt_linked_item_t *)mdns_mem_malloc(sizeof(mdns_txt_linked_item_t)); + ESP_GOTO_ON_FALSE(new_txt, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + new_txt->key = mdns_mem_strdup(key); + ESP_GOTO_ON_FALSE(new_txt->key, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + new_txt->value = value; + new_txt->value_len = value_len; + new_txt->next = srv->txt; + srv->txt = new_txt; + } + + announce_all_pcbs(&s, 1, false); + +err: + mdns_priv_service_unlock(); + return ret; +out_of_mem: + mdns_priv_service_unlock(); + HOOK_MALLOC_FAILED; + mdns_mem_free(value); + mdns_mem_free(new_txt); + return ret; +} + +esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname, + const char *key, const char *value) +{ + return mdns_service_txt_item_set_for_host_with_explicit_value_len(instance, service, proto, hostname, key, value, + strlen(value)); +} + + +esp_err_t mdns_service_txt_item_set(const char *service, const char *proto, const char *key, const char *value) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key, + value, strlen(value)); +} + +esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service, const char *proto, const char *key, + const char *value, uint8_t value_len) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_txt_item_set_for_host_with_explicit_value_len(NULL, service, proto, NULL, key, value, value_len); +} + +esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service, const char *proto, const char *host, + const char *key) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && !mdns_utils_str_null_or_empty(key), + ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + mdns_service_t *srv = s->service; + mdns_txt_linked_item_t *txt = srv->txt; + if (!txt) { + goto err; + } + if (strcmp(txt->key, key) == 0) { + srv->txt = txt->next; + mdns_mem_free((char *)txt->key); + mdns_mem_free((char *)txt->value); + mdns_mem_free(txt); + } else { + while (txt->next) { + if (strcmp(txt->next->key, key) == 0) { + mdns_txt_linked_item_t *t = txt->next; + txt->next = t->next; + mdns_mem_free((char *)t->key); + mdns_mem_free((char *)t->value); + mdns_mem_free(t); + break; + } else { + txt = txt->next; + } + } + } + + announce_all_pcbs(&s, 1, false); + +err: + mdns_priv_service_unlock(); + if (ret == ESP_ERR_NO_MEM) { + HOOK_MALLOC_FAILED; + } + return ret; +} + +esp_err_t mdns_service_txt_item_remove(const char *service, const char *proto, const char *key) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_txt_item_remove_for_host(NULL, service, proto, NULL, key); +} + +static esp_err_t service_subtype_remove_for_host(mdns_srv_item_t *service, const char *subtype) +{ + esp_err_t ret = ESP_ERR_NOT_FOUND; + mdns_subtype_t *srv_subtype = service->service->subtype; + mdns_subtype_t *pre = service->service->subtype; + while (srv_subtype) { + if (strcmp(srv_subtype->subtype, subtype) == 0) { + // Target subtype is found. + if (srv_subtype == service->service->subtype) { + // The first node needs to be removed + service->service->subtype = service->service->subtype->next; + } else { + pre->next = srv_subtype->next; + } + mdns_mem_free((char *)srv_subtype->subtype); + mdns_mem_free(srv_subtype); + ret = ESP_OK; + break; + } + pre = srv_subtype; + srv_subtype = srv_subtype->next; + } + if (ret == ESP_ERR_NOT_FOUND) { + ESP_LOGE(TAG, "Subtype : %s doesn't exist", subtype); + } + + return ret; +} + +esp_err_t mdns_service_subtype_remove_for_host(const char *instance_name, const char *service, const char *proto, + const char *hostname, const char *subtype) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && + !mdns_utils_str_null_or_empty(subtype), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance_name, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + ret = service_subtype_remove_for_host(s, subtype); + ESP_GOTO_ON_ERROR(ret, err, TAG, "Failed to remove the subtype: %s", subtype); + + // Transmit a sendbye message for the removed subtype. + mdns_subtype_t *remove_subtypes = (mdns_subtype_t *)mdns_mem_malloc(sizeof(mdns_subtype_t)); + ESP_GOTO_ON_FALSE(remove_subtypes, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + remove_subtypes->subtype = mdns_mem_strdup(subtype); + ESP_GOTO_ON_FALSE(remove_subtypes->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + remove_subtypes->next = NULL; + + mdns_priv_send_bye_subtype(s, instance_name, remove_subtypes); + free_subtype(remove_subtypes); +err: + mdns_priv_service_unlock(); + return ret; +out_of_mem: + HOOK_MALLOC_FAILED; + mdns_mem_free(remove_subtypes); + mdns_priv_service_unlock(); + return ret; +} + +static esp_err_t service_subtype_add_for_host(mdns_srv_item_t *service, const char *subtype) +{ + esp_err_t ret = ESP_OK; + mdns_subtype_t *srv_subtype = service->service->subtype; + while (srv_subtype) { + ESP_GOTO_ON_FALSE(strcmp(srv_subtype->subtype, subtype) != 0, ESP_ERR_INVALID_ARG, err, TAG, "Subtype: %s has already been added", subtype); + srv_subtype = srv_subtype->next; + } + + mdns_subtype_t *subtype_item = (mdns_subtype_t *)mdns_mem_malloc(sizeof(mdns_subtype_t)); + ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + subtype_item->subtype = mdns_mem_strdup(subtype); + ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); + subtype_item->next = service->service->subtype; + service->service->subtype = subtype_item; + +err: + return ret; +out_of_mem: + HOOK_MALLOC_FAILED; + mdns_mem_free(subtype_item); + return ret; +} + +esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char *instance_name, const char *service, const char *proto, + const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + int cur_index = 0; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && + (num_items > 0), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance_name, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + for (; cur_index < num_items; cur_index++) { + ret = service_subtype_add_for_host(s, subtype[cur_index].subtype); + if (ret == ESP_OK) { + continue; + } else if (ret == ESP_ERR_NO_MEM) { + ESP_LOGE(TAG, "Out of memory"); + goto err; + } else { + ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype); + goto exit; + } + } + + announce_all_pcbs(&s, 1, false); +err: + if (ret == ESP_ERR_NO_MEM) { + for (int idx = 0; idx < cur_index; idx++) { + service_subtype_remove_for_host(s, subtype[idx].subtype); + } + } +exit: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto, + const char *hostname, const char *subtype) +{ + mdns_subtype_item_t _subtype[1]; + _subtype[0].subtype = subtype; + return mdns_service_subtype_add_multiple_items_for_host(instance_name, service_type, proto, hostname, _subtype, 1); +} + +static mdns_subtype_t *service_find_subtype_needed_sendbye(mdns_service_t *service, mdns_subtype_item_t subtype[], + uint8_t num_items) +{ + if (!service) { + return NULL; + } + + mdns_subtype_t *current = service->subtype; + mdns_subtype_t *prev = NULL; + mdns_subtype_t *prev_goodbye = NULL; + mdns_subtype_t *out_goodbye_subtype = NULL; + + while (current) { + bool subtype_in_update = false; + + for (int i = 0; i < num_items; i++) { + if (strcmp(subtype[i].subtype, current->subtype) == 0) { + subtype_in_update = true; + break; + } + } + + if (!subtype_in_update) { + // Remove from original list + if (prev) { + prev->next = current->next; + } else { + service->subtype = current->next; + } + + mdns_subtype_t *to_move = current; + current = current->next; + + // Add to goodbye list + to_move->next = NULL; + if (prev_goodbye) { + prev_goodbye->next = to_move; + } else { + out_goodbye_subtype = to_move; + } + prev_goodbye = to_move; + } else { + prev = current; + current = current->next; + } + } + + return out_goodbye_subtype; +} + +esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char *instance_name, const char *service_type, const char *proto, + const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + int cur_index = 0; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service_type) && !mdns_utils_str_null_or_empty(proto), + ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance_name, service_type, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + mdns_subtype_t *goodbye_subtype = service_find_subtype_needed_sendbye(s->service, subtype, num_items); + + if (goodbye_subtype) { + mdns_priv_send_bye_subtype(s, instance_name, goodbye_subtype); + } + + free_subtype(goodbye_subtype); + free_service_subtype(s->service); + + for (; cur_index < num_items; cur_index++) { + ret = service_subtype_add_for_host(s, subtype[cur_index].subtype); + if (ret == ESP_OK) { + continue; + } else if (ret == ESP_ERR_NO_MEM) { + ESP_LOGE(TAG, "Out of memory"); + goto err; + } else { + ESP_LOGE(TAG, "Failed to add subtype: %s", subtype[cur_index].subtype); + goto exit; + } + } + if (num_items) { + announce_all_pcbs(&s, 1, false); + } +err: + if (ret == ESP_ERR_NO_MEM) { + for (int idx = 0; idx < cur_index; idx++) { + service_subtype_remove_for_host(s, subtype[idx].subtype); + } + } +exit: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *host, + const char *instance) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto) && + !mdns_utils_str_null_or_empty(instance) && strlen(instance) <= (MDNS_NAME_BUF_LEN - 1), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance_old, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + if (s->service->instance) { + mdns_priv_pcb_send_bye_service(&s, 1, false); + mdns_mem_free((char *)s->service->instance); + } + s->service->instance = mdns_mem_strndup(instance, MDNS_NAME_BUF_LEN - 1); + ESP_GOTO_ON_FALSE(s->service->instance, ESP_ERR_NO_MEM, err, TAG, "Out of memory"); + mdns_priv_probe_all_pcbs(&s, 1, false, false); + +err: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_instance_name_set(const char *service, const char *proto, const char *instance) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_instance_name_set_for_host(NULL, service, proto, NULL, instance); +} + +esp_err_t mdns_service_remove_for_host(const char *instance, const char *service, const char *proto, const char *host) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + const char *hostname = host ? host : s_server->hostname; + ESP_GOTO_ON_FALSE(s_server && s_server->services && !mdns_utils_str_null_or_empty(service) && !mdns_utils_str_null_or_empty(proto), + ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + mdns_srv_item_t *s = mdns_utils_get_service_item_instance(instance, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + mdns_srv_item_t *a = s_server->services; + mdns_srv_item_t *b = a; + if (instance) { + while (a) { + if (mdns_utils_service_match_instance(a->service, instance, service, proto, hostname)) { + if (s_server->services != a) { + b->next = a->next; + } else { + s_server->services = a->next; + } + mdns_priv_pcb_send_bye_service(&a, 1, false); + mdns_priv_remove_scheduled_service_packets(a->service); + free_service(a->service); + mdns_mem_free(a); + break; + } + b = a; + a = a->next; + } + } else { + while (a) { + if (mdns_utils_service_match(a->service, service, proto, hostname)) { + if (s_server->services != a) { + b->next = a->next; + } else { + s_server->services = a->next; + } + mdns_priv_pcb_send_bye_service(&a, 1, false); + mdns_priv_remove_scheduled_service_packets(a->service); + free_service(a->service); + mdns_mem_free(a); + break; + } + b = a; + a = a->next; + } + } + +err: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_service_remove(const char *service_type, const char *proto) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + return mdns_service_remove_for_host(NULL, service_type, proto, NULL); +} + +esp_err_t mdns_service_remove_all(void) +{ + mdns_priv_service_lock(); + esp_err_t ret = ESP_OK; + ESP_GOTO_ON_FALSE(s_server, ESP_ERR_INVALID_ARG, done, TAG, "Invalid state"); + if (!s_server->services) { + goto done; + } + + send_final_bye(false); + mdns_srv_item_t *services = s_server->services; + s_server->services = NULL; + while (services) { + mdns_srv_item_t *s = services; + services = services->next; + mdns_priv_remove_scheduled_service_packets(s->service); + free_service(s->service); + mdns_mem_free(s); + } + +done: + mdns_priv_service_unlock(); + return ret; +} + +esp_err_t mdns_lookup_delegated_service(const char *instance, const char *service, const char *proto, size_t max_results, + mdns_result_t **result) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (!result || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + mdns_priv_service_lock(); + *result = lookup_service(instance, service, proto, max_results, false); + mdns_priv_service_unlock(); + return ESP_OK; +} + +esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *service, const char *proto, size_t max_results, + mdns_result_t **result) +{ + if (!s_server) { + return ESP_ERR_INVALID_STATE; + } + if (!result || mdns_utils_str_null_or_empty(service) || mdns_utils_str_null_or_empty(proto)) { + return ESP_ERR_INVALID_ARG; + } + mdns_priv_service_lock(); + *result = lookup_service(instance, service, proto, max_results, true); + mdns_priv_service_unlock(); + return ESP_OK; +} diff --git a/components/mdns/mdns_send.c b/components/mdns/mdns_send.c new file mode 100644 index 0000000000..0f09759a66 --- /dev/null +++ b/components/mdns/mdns_send.c @@ -0,0 +1,1880 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "mdns_send.h" +#include "mdns_utils.h" +#include "mdns_networking.h" +#include "mdns_debug.h" +#include "mdns_mem_caps.h" +#include "esp_log.h" +#include "mdns_debug.h" +#include "mdns_netif.h" +#include "mdns_pcb.h" +#include "mdns_responder.h" +#include "mdns_service.h" + +static const char *TAG = "mdns_send"; +static const char *MDNS_SUB_STR = "_sub"; + +static mdns_tx_packet_t *s_tx_queue_head; + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) +#endif + +/** + * @brief appends uint32_t in a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param value the value to set + * + * @return length of added data: 0 on error or 4 on success + */ +static inline uint8_t append_u32(uint8_t *packet, uint16_t *index, uint32_t value) +{ + if ((*index + sizeof(uint32_t)) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + mdns_utils_append_u8(packet, index, (value >> 24) & 0xFF); + mdns_utils_append_u8(packet, index, (value >> 16) & 0xFF); + mdns_utils_append_u8(packet, index, (value >> 8) & 0xFF); + mdns_utils_append_u8(packet, index, value & 0xFF); + return sizeof(uint32_t); +} + +/** + * @brief appends answer type, class, ttl and data length to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param type answer type + * @param ttl answer ttl + * + * @return length of added data: 0 on error or 10 on success + */ +static inline uint8_t append_type(uint8_t *packet, uint16_t *index, uint8_t type, bool flush, uint32_t ttl) +{ + const size_t len = sizeof(uint16_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t); + if ((*index + len) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + uint16_t mdns_class = MDNS_CLASS_IN; + if (flush) { + mdns_class = MDNS_CLASS_IN_FLUSH_CACHE; + } + if (type == MDNS_ANSWER_PTR) { + mdns_utils_append_u16(packet, index, MDNS_TYPE_PTR); + mdns_utils_append_u16(packet, index, mdns_class); + } else if (type == MDNS_ANSWER_TXT) { + mdns_utils_append_u16(packet, index, MDNS_TYPE_TXT); + mdns_utils_append_u16(packet, index, mdns_class); + } else if (type == MDNS_ANSWER_SRV) { + mdns_utils_append_u16(packet, index, MDNS_TYPE_SRV); + mdns_utils_append_u16(packet, index, mdns_class); + } else if (type == MDNS_ANSWER_A) { + mdns_utils_append_u16(packet, index, MDNS_TYPE_A); + mdns_utils_append_u16(packet, index, mdns_class); + } else if (type == MDNS_ANSWER_AAAA) { + mdns_utils_append_u16(packet, index, MDNS_TYPE_AAAA); + mdns_utils_append_u16(packet, index, mdns_class); + } else { + return 0; + } + append_u32(packet, index, ttl); + mdns_utils_append_u16(packet, index, 0); + return len; +} + +/** + * @brief appends single string to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param string the string to append + * + * @return length of added data: 0 on error or length of the string + 1 on success + */ +static inline uint8_t append_string(uint8_t *packet, uint16_t *index, const char *string) +{ + uint8_t len = strlen(string); + if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + mdns_utils_append_u8(packet, index, len); + memcpy(packet + *index, string, len); + *index += len; + return len + 1; +} + +int mdns_priv_append_one_txt_record_entry(uint8_t *packet, uint16_t *index, mdns_txt_linked_item_t *txt) +{ + if (txt == NULL || txt->key == NULL) { + return -1; + } + size_t key_len = strlen(txt->key); + size_t len = key_len + txt->value_len + (txt->value ? 1 : 0); + if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + mdns_utils_append_u8(packet, index, len); + memcpy(packet + *index, txt->key, key_len); + if (txt->value) { + packet[*index + key_len] = '='; + memcpy(packet + *index + key_len + 1, txt->value, txt->value_len); + } + *index += len; + return len + 1; +} + +static uint16_t append_fqdn(uint8_t *packet, uint16_t *index, const char *strings[], uint8_t count, size_t packet_len); + +/** + * @brief sets uint16_t value in a packet + * + * @param packet MDNS packet + * @param index offset of uint16_t value + * @param value the value to set + */ +static inline void set_u16(uint8_t *packet, uint16_t index, uint16_t value) +{ + if ((index + 1) >= MDNS_MAX_PACKET_SIZE) { + return; + } + packet[index] = (value >> 8) & 0xFF; + packet[index + 1] = value & 0xFF; +} + +/** + * @brief Allocate new packet for sending + */ +mdns_tx_packet_t *mdns_priv_alloc_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_tx_packet_t *packet = (mdns_tx_packet_t *)mdns_mem_malloc(sizeof(mdns_tx_packet_t)); + if (!packet) { + HOOK_MALLOC_FAILED; + return NULL; + } + memset((uint8_t *)packet, 0, sizeof(mdns_tx_packet_t)); + packet->tcpip_if = tcpip_if; + packet->ip_protocol = ip_protocol; + packet->port = MDNS_SERVICE_PORT; +#ifdef CONFIG_LWIP_IPV4 + if (ip_protocol == MDNS_IP_PROTOCOL_V4) { + esp_ip_addr_t addr = ESP_IP4ADDR_INIT(224, 0, 0, 251); + memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); + } +#endif +#ifdef CONFIG_LWIP_IPV6 + if (ip_protocol == MDNS_IP_PROTOCOL_V6) { + esp_ip_addr_t addr = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfb000000); + memcpy(&packet->dst, &addr, sizeof(esp_ip_addr_t)); + } +#endif + return packet; +} + +/** + * @brief frees a packet + * + * @param packet the packet + */ +void mdns_priv_free_tx_packet(mdns_tx_packet_t *packet) +{ + if (!packet) { + return; + } + mdns_out_question_t *q = packet->questions; + while (q) { + mdns_out_question_t *next = q->next; + if (q->own_dynamic_memory) { + mdns_mem_free((char *)q->host); + mdns_mem_free((char *)q->service); + mdns_mem_free((char *)q->proto); + mdns_mem_free((char *)q->domain); + } + mdns_mem_free(q); + q = next; + } + queueFree(mdns_out_answer_t, packet->answers); + queueFree(mdns_out_answer_t, packet->servers); + queueFree(mdns_out_answer_t, packet->additional); + mdns_mem_free(packet); +} + +/** + * @brief Allocate new answer and add it to answer list (destination) + */ +bool mdns_priv_create_answer(mdns_out_answer_t **destination, uint16_t type, mdns_service_t *service, + mdns_host_item_t *host, bool flush, bool bye) +{ + mdns_out_answer_t *d = *destination; + while (d) { + if (d->type == type && d->service == service && d->host == host) { + return true; + } + d = d->next; + } + + mdns_out_answer_t *a = (mdns_out_answer_t *)mdns_mem_malloc(sizeof(mdns_out_answer_t)); + if (!a) { + HOOK_MALLOC_FAILED; + return false; + } + a->type = type; + a->service = service; + a->host = host; + a->custom_service = NULL; + a->bye = bye; + a->flush = flush; + a->next = NULL; + queueToEnd(mdns_out_answer_t, *destination, a); + return true; +} + +static mdns_host_item_t *get_host_item(const char *hostname) +{ + if (hostname == NULL || strcasecmp(hostname, mdns_priv_get_global_hostname()) == 0) { + return mdns_priv_get_self_host(); + } + mdns_host_item_t *host = mdns_priv_get_hosts(); + while (host != NULL) { + if (strcasecmp(host->hostname, hostname) == 0) { + return host; + } + host = host->next; + } + return NULL; +} + +static bool create_answer_from_service(mdns_tx_packet_t *packet, mdns_service_t *service, + mdns_parsed_question_t *question, bool shared, bool send_flush) +{ + mdns_host_item_t *host = get_host_item(service->hostname); + bool is_delegated = (host != mdns_priv_get_self_host()); + if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) { + // According to RFC6763-section12.1, for DNS-SD, SRV, TXT and all address records + // should be included in additional records. + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) || + !mdns_priv_create_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_SRV, service, + NULL, send_flush, false) || + !mdns_priv_create_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_TXT, service, + NULL, send_flush, false) || + !mdns_priv_create_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_A, + service, host, send_flush, + false) || + !mdns_priv_create_answer((shared || is_delegated) ? &packet->additional : &packet->answers, + MDNS_TYPE_AAAA, service, host, + send_flush, false)) { + return false; + } + } else if (question->type == MDNS_TYPE_SRV) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) || + !mdns_priv_create_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) || + !mdns_priv_create_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) { + return false; + } + } else if (question->type == MDNS_TYPE_TXT) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false)) { + return false; + } + } else if (question->type == MDNS_TYPE_SDPTR) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SDPTR, service, NULL, false, false)) { + return false; + } + } + return true; +} + +static bool create_answer_from_hostname(mdns_tx_packet_t *packet, const char *hostname, bool send_flush) +{ + mdns_host_item_t *host = get_host_item(hostname); + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_A, NULL, host, send_flush, false) || + !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, send_flush, false)) { + return false; + } + return true; +} + +static bool service_match_ptr_question(const mdns_service_t *service, const mdns_parsed_question_t *question) +{ + if (!mdns_utils_service_match(service, question->service, question->proto, NULL)) { + return false; + } + // The question parser stores anything before _type._proto in question->host + // So the question->host can be subtype or instance name based on its content + if (question->sub) { + mdns_subtype_t *subtype = service->subtype; + while (subtype) { + if (!strcasecmp(subtype->subtype, question->host)) { + return true; + } + subtype = subtype->next; + } + return false; + } + if (question->host) { + if (strcasecmp(mdns_utils_get_service_instance_name(service), question->host) != 0) { + return false; + } + } + return true; +} +static bool append_host(mdns_out_answer_t **destination, mdns_host_item_t *host, bool flush, bool bye) +{ + if (!mdns_priv_create_answer(destination, MDNS_TYPE_A, NULL, host, flush, bye)) { + return false; + } + if (!mdns_priv_create_answer(destination, MDNS_TYPE_AAAA, NULL, host, flush, bye)) { + return false; + } + return true; +} + +bool mdns_priv_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], + size_t services_len, bool flush, bool bye) +{ + if (services == NULL) { + mdns_host_item_t *host = get_host_item(mdns_priv_get_global_hostname()); + if (host != NULL) { + return append_host(destination, host, flush, bye); + } + return true; + } + for (size_t i = 0; i < services_len; i++) { + mdns_host_item_t *host = get_host_item(services[i]->service->hostname); + if (!append_host(destination, host, flush, bye)) { + return false; + } + } + return true; +} + +static bool append_host_list(mdns_out_answer_t **destination, bool flush, bool bye) +{ + if (!mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname())) { + mdns_host_item_t *self_host = get_host_item(mdns_priv_get_global_hostname()); + if (!append_host(destination, self_host, flush, bye)) { + return false; + } + } + mdns_host_item_t *host = mdns_priv_get_hosts(); + while (host != NULL) { + host = host->next; + if (!append_host(destination, host, flush, bye)) { + return false; + } + } + return true; +} + +/** + * @brief Check if question is already in the list + */ +static bool question_exists(mdns_out_question_t *needle, mdns_out_question_t *haystack) +{ + while (haystack) { + if (haystack->type == needle->type + && haystack->host == needle->host + && haystack->service == needle->service + && haystack->proto == needle->proto) { + return true; + } + haystack = haystack->next; + } + return false; +} + +static bool append_host_question(mdns_out_question_t **questions, const char *hostname, bool unicast) +{ + mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); + if (!q) { + HOOK_MALLOC_FAILED; + return false; + } + q->next = NULL; + q->unicast = unicast; + q->type = MDNS_TYPE_ANY; + q->host = hostname; + q->service = NULL; + q->proto = NULL; + q->domain = MDNS_UTILS_DEFAULT_DOMAIN; + q->own_dynamic_memory = false; + if (question_exists(q, *questions)) { + mdns_mem_free(q); + } else { + queueToEnd(mdns_out_question_t, *questions, q); + } + return true; +} + +static bool append_host_questions_for_services(mdns_out_question_t **questions, mdns_srv_item_t *services[], + size_t len, bool unicast) +{ + if (!mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname()) && + !append_host_question(questions, mdns_priv_get_global_hostname(), unicast)) { + return false; + } + for (size_t i = 0; i < len; i++) { + if (!append_host_question(questions, services[i]->service->hostname, unicast)) { + return false; + } + } + return true; +} + +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES +static inline int append_single_str(uint8_t *packet, uint16_t *index, const char *str, int len) +{ + if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + if (!mdns_utils_append_u8(packet, index, len)) { + return 0; + } + memcpy(packet + *index, str, len); + *index += len; + return *index; +} + +/** + * @brief appends FQDN to a packet from hostname separated by dots. This API works the same way as + * append_fqdn(), but refrains from DNS compression (as it's mainly used for IP addresses (many short items), + * where we gain very little (or compression even gets counter-productive mainly for IPv6 addresses) + * + * @param packet MDNS packet + * @param index offset in the packet + * @param name name representing FQDN in '.' separated parts + * @param last true if appending the last part (domain, typically "arpa") + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_fqdn_dots(uint8_t *packet, uint16_t *index, const char *name, bool last) +{ + int len = strlen(name); + char *host = (char *)name; + char *end = host; + char *start = host; + do { + end = memchr(start, '.', host + len - start); + end = end ? end : host + len; + int part_len = end - start; + if (!append_single_str(packet, index, start, part_len)) { + return 0; + } + start = ++end; + } while (end < name + len); + + if (!append_single_str(packet, index, "arpa", sizeof("arpa") - 1)) { + return 0; + } + + //empty string so terminate + if (!mdns_utils_append_u8(packet, index, 0)) { + return 0; + } + return *index; +} + +/** + * @brief Appends reverse lookup PTR record + */ +static uint8_t append_reverse_ptr_record(uint8_t *packet, uint16_t *index, const char *name) +{ + if (strstr(name, "in-addr") == NULL && strstr(name, "ip6") == NULL) { + return 0; + } + + if (!append_fqdn_dots(packet, index, name, false)) { + return 0; + } + + if (!append_type(packet, index, MDNS_ANSWER_PTR, false, 10 /* TTL set to 10s*/)) { + return 0; + } + + uint16_t data_len_location = *index - 2; /* store the position of size (2=16bis) of this record */ + const char *str[2] = {mdns_priv_get_self_host()->hostname, MDNS_UTILS_DEFAULT_DOMAIN }; + + int part_length = append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + + set_u16(packet, data_len_location, part_length); + return 1; /* appending only 1 record */ +} +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + +void mdns_priv_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet) +{ + if (!parsed_packet->questions) { + return; + } + bool send_flush = parsed_packet->src_port == MDNS_SERVICE_PORT; + bool unicast = false; + bool shared = false; + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(parsed_packet->tcpip_if, parsed_packet->ip_protocol); + if (!packet) { + return; + } + packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; + packet->distributed = parsed_packet->distributed; + packet->id = parsed_packet->id; + + mdns_parsed_question_t *q = parsed_packet->questions; + uint32_t out_record_nums = 0; + while (q) { + shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe; + if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) { + mdns_srv_item_t *service = mdns_utils_get_service_item_instance(q->host, q->service, q->proto, NULL); + if (service == NULL) { // Service not found, but we continue to the next question + q = q->next; + continue; + } + if (!create_answer_from_service(packet, service->service, q, shared, send_flush)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } + } else if (q->service && q->proto) { + mdns_srv_item_t *service = mdns_priv_get_services(); + while (service) { + if (service_match_ptr_question(service->service, q)) { + mdns_parsed_record_t *r = parsed_packet->records; + bool is_record_exist = false; + while (r) { + if (service->service->instance && r->host) { + if (mdns_utils_service_match_instance(service->service, r->host, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) { + is_record_exist = true; + break; + } + } else if (!service->service->instance && !r->host) { + if (mdns_utils_service_match(service->service, r->service, r->proto, NULL) && r->ttl > (MDNS_ANSWER_PTR_TTL / 2)) { + is_record_exist = true; + break; + } + } + r = r->next; + } + if (!is_record_exist) { + if (!create_answer_from_service(packet, service->service, q, shared, send_flush)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } + } + } + service = service->next; + } + } else if (q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA) { + if (!create_answer_from_hostname(packet, q->host, send_flush)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } + } else if (q->type == MDNS_TYPE_ANY) { + if (!append_host_list(&packet->answers, send_flush, false)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + } else if (q->type == MDNS_TYPE_PTR) { + mdns_host_item_t *host = get_host_item(q->host); + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, NULL, host, send_flush, false)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + } else if (!mdns_priv_create_answer(&packet->answers, q->type, NULL, NULL, send_flush, false)) { + mdns_priv_free_tx_packet(packet); + return; + } else { + out_record_nums++; + } + + if (parsed_packet->src_port != MDNS_SERVICE_PORT && // Repeat the queries only for "One-Shot mDNS queries" + (q->type == MDNS_TYPE_ANY || q->type == MDNS_TYPE_A || q->type == MDNS_TYPE_AAAA +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + || q->type == MDNS_TYPE_PTR +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + )) { + mdns_out_question_t *out_question = mdns_mem_malloc(sizeof(mdns_out_question_t)); + if (out_question == NULL) { + HOOK_MALLOC_FAILED; + mdns_priv_free_tx_packet(packet); + return; + } + out_question->type = q->type; + out_question->unicast = q->unicast; + out_question->host = q->host; + q->host = NULL; + out_question->service = q->service; + q->service = NULL; + out_question->proto = q->proto; + q->proto = NULL; + out_question->domain = q->domain; + q->domain = NULL; + out_question->next = NULL; + out_question->own_dynamic_memory = true; + queueToEnd(mdns_out_question_t, packet->questions, out_question); + } + if (q->unicast) { + unicast = true; + } + q = q->next; + } + if (out_record_nums == 0) { + mdns_priv_free_tx_packet(packet); + return; + } + if (unicast || !send_flush) { + memcpy(&packet->dst, &parsed_packet->src, sizeof(esp_ip_addr_t)); + packet->port = parsed_packet->src_port; + } + + static uint8_t share_step = 0; + if (shared) { + mdns_priv_send_after(packet, 25 + (share_step * 25)); + share_step = (share_step + 1) & 0x03; + } else { + mdns_priv_dispatch_tx_packet(packet); + mdns_priv_free_tx_packet(packet); + } +} + +/** + * @brief appends FQDN to a packet, incrementing the index and + * compressing the output if previous occurrence of the string (or part of it) has been found + * + * @param packet MDNS packet + * @param index offset in the packet + * @param strings string array containing the parts of the FQDN + * @param count number of strings in the array + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_fqdn(uint8_t *packet, uint16_t *index, const char *strings[], uint8_t count, size_t packet_len) +{ + if (!count) { + // empty string so terminate + return mdns_utils_append_u8(packet, index, 0); + } + static char buf[MDNS_NAME_BUF_LEN]; + uint8_t len = strlen(strings[0]); + // try to find first the string length in the packet (if it exists) + uint8_t *len_location = (uint8_t *)memchr(packet, (char)len, *index); + while (len_location) { + mdns_name_t name; + // check if the string after len_location is the string that we are looking for + if (memcmp(len_location + 1, strings[0], len)) { //not continuing with our string +search_next: + // try and find the length byte further in the packet + len_location = (uint8_t *)memchr(len_location + 1, (char)len, *index - (len_location + 1 - packet)); + continue; + } + // seems that we might have found the string that we are looking for + // read the destination into name and compare + name.parts = 0; + name.sub = 0; + name.invalid = false; + name.host[0] = 0; + name.service[0] = 0; + name.proto[0] = 0; + name.domain[0] = 0; + const uint8_t *content = mdns_utils_read_fqdn(packet, len_location, &name, buf, packet_len); + if (!content) { + // not a readable fqdn? + goto search_next; // could be our unfinished fqdn, continue searching + } + if (name.parts == count) { + uint8_t i; + for (i = 0; i < count; i++) { + if (strcasecmp(strings[i], (const char *)&name + (i * (MDNS_NAME_BUF_LEN)))) { + // not our string! let's search more + goto search_next; + } + } + // we actually have found the string + break; + } else { + goto search_next; + } + } + // string is not yet in the packet, so let's add it + if (!len_location) { + uint8_t written = append_string(packet, index, strings[0]); + if (!written) { + return 0; + } + // run the same for the other strings in the name + return written + append_fqdn(packet, index, &strings[1], count - 1, packet_len); + } + + // we have found the string so let's insert a pointer to it instead + uint16_t offset = len_location - packet; + offset |= MDNS_NAME_REF; + return mdns_utils_append_u16(packet, index, offset); +} + + +/** + * @brief Append question to packet + */ +static uint16_t append_question(uint8_t *packet, uint16_t *index, mdns_out_question_t *q) +{ + uint8_t part_length; +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + if (q->host && (strstr(q->host, "in-addr") || strstr(q->host, "ip6"))) { + part_length = append_fqdn_dots(packet, index, q->host, false); + if (!part_length) { + return 0; + } + } else +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + { + const char *str[4]; + uint8_t str_index = 0; + if (q->host) { + str[str_index++] = q->host; + } + if (q->service) { + str[str_index++] = q->service; + } + if (q->proto) { + str[str_index++] = q->proto; + } + if (q->domain) { + str[str_index++] = q->domain; + } + part_length = append_fqdn(packet, index, str, str_index, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + } + + part_length += mdns_utils_append_u16(packet, index, q->type); + part_length += mdns_utils_append_u16(packet, index, q->unicast ? 0x8001 : 0x0001); + return part_length; +} + +/** + * @brief appends PTR record for service to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param server the server that is hosting the service + * @param service the service to add record for + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, const char *service, const char *proto, bool flush, bool bye) +{ + const char *str[4]; + uint16_t record_length = 0; + uint8_t part_length; + + if (service == NULL) { + return 0; + } + + str[0] = instance; + str[1] = service; + str[2] = proto; + str[3] = MDNS_UTILS_DEFAULT_DOMAIN; + + part_length = append_fqdn(packet, index, str + 1, 3, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + part_length = append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + set_u16(packet, data_len_location, part_length); + record_length += part_length; + return record_length; +} + +/** + * @brief appends PTR record for a subtype to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param instance the service instance name + * @param subtype the service subtype + * @param proto the service protocol + * @param flush whether to set the flush flag + * @param bye whether to set the bye flag + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_subtype_ptr_record(uint8_t *packet, uint16_t *index, const char *instance, + const char *subtype, const char *service, const char *proto, bool flush, + bool bye) +{ + const char *subtype_str[5] = {subtype, MDNS_SUB_STR, service, proto, MDNS_UTILS_DEFAULT_DOMAIN}; + const char *instance_str[4] = {instance, service, proto, MDNS_UTILS_DEFAULT_DOMAIN}; + uint16_t record_length = 0; + uint8_t part_length; + + if (service == NULL) { + return 0; + } + + part_length = append_fqdn(packet, index, subtype_str, ARRAY_SIZE(subtype_str), MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + part_length = append_fqdn(packet, index, instance_str, ARRAY_SIZE(instance_str), MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + set_u16(packet, data_len_location, part_length); + record_length += part_length; + return record_length; +} +/** + * @brief Append PTR answers to packet + * + * @return number of answers added to the packet + */ +static uint8_t append_service_ptr_answers(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, + bool bye) +{ + uint8_t appended_answers = 0; + + if (append_ptr_record(packet, index, mdns_utils_get_service_instance_name(service), service->service, + service->proto, flush, bye) <= 0) { + return appended_answers; + } + appended_answers++; + + mdns_subtype_t *subtype = service->subtype; + while (subtype) { + appended_answers += + (append_subtype_ptr_record(packet, index, mdns_utils_get_service_instance_name(service), subtype->subtype, + service->service, service->proto, flush, bye) > 0); + subtype = subtype->next; + } + + return appended_answers; +} + +/** + * @brief appends SRV record for service to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param server the server that is hosting the service + * @param service the service to add record for + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_srv_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) +{ + const char *str[4]; + uint16_t record_length = 0; + uint8_t part_length; + + if (service == NULL) { + return 0; + } + + str[0] = mdns_utils_get_service_instance_name(service); + str[1] = service->service; + str[2] = service->proto; + str[3] = MDNS_UTILS_DEFAULT_DOMAIN; + + if (!str[0]) { + return 0; + } + + part_length = append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_SRV, flush, bye ? 0 : MDNS_ANSWER_SRV_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + + part_length = 0; + part_length += mdns_utils_append_u16(packet, index, service->priority); + part_length += mdns_utils_append_u16(packet, index, service->weight); + part_length += mdns_utils_append_u16(packet, index, service->port); + if (part_length != 6) { + return 0; + } + + if (service->hostname) { + str[0] = service->hostname; + } else { + str[0] = mdns_priv_get_global_hostname(); + } + str[1] = MDNS_UTILS_DEFAULT_DOMAIN; + + if (mdns_utils_str_null_or_empty(str[0])) { + return 0; + } + + part_length = append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + set_u16(packet, data_len_location, part_length + 6); + + record_length += part_length + 6; + return record_length; +} + +/** + * @brief appends TXT record for service to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param server the server that is hosting the service + * @param service the service to add record for + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_txt_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) +{ + const char *str[4]; + uint16_t record_length = 0; + uint8_t part_length; + + if (service == NULL) { + return 0; + } + + str[0] = mdns_utils_get_service_instance_name(service); + str[1] = service->service; + str[2] = service->proto; + str[3] = MDNS_UTILS_DEFAULT_DOMAIN; + + if (!str[0]) { + return 0; + } + + part_length = append_fqdn(packet, index, str, 4, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_TXT, flush, bye ? 0 : MDNS_ANSWER_TXT_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + uint16_t data_len = 0; + + mdns_txt_linked_item_t *txt = service->txt; + while (txt) { + int l = mdns_priv_append_one_txt_record_entry(packet, index, txt); + if (l > 0) { + data_len += l; + } else if (l == 0) { // TXT entry won't fit into the mdns packet + return 0; + } + txt = txt->next; + } + if (!data_len) { + data_len = 1; + packet[*index] = 0; + *index = *index + 1; + } + set_u16(packet, data_len_location, data_len); + record_length += data_len; + return record_length; +} + +/** + * @brief appends DNS-SD PTR record for service to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param server the server that is hosting the service + * @param service the service to add record for + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_sdptr_record(uint8_t *packet, uint16_t *index, mdns_service_t *service, bool flush, bool bye) +{ + const char *str[3]; + const char *sd_str[4]; + uint16_t record_length = 0; + uint8_t part_length; + + if (service == NULL) { + return 0; + } + + sd_str[0] = (char *)"_services"; + sd_str[1] = (char *)"_dns-sd"; + sd_str[2] = (char *)"_udp"; + sd_str[3] = MDNS_UTILS_DEFAULT_DOMAIN; + + str[0] = service->service; + str[1] = service->proto; + str[2] = MDNS_UTILS_DEFAULT_DOMAIN; + + part_length = append_fqdn(packet, index, sd_str, 4, MDNS_MAX_PACKET_SIZE); + + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_PTR, flush, MDNS_ANSWER_PTR_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + part_length = append_fqdn(packet, index, str, 3, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + set_u16(packet, data_len_location, part_length); + record_length += part_length; + return record_length; +} + +#ifdef CONFIG_LWIP_IPV4 +/** + * @brief appends A record to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param hostname the hostname address to add + * @param ip the IP address to add + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_a_record(uint8_t *packet, uint16_t *index, const char *hostname, uint32_t ip, bool flush, bool bye) +{ + const char *str[2]; + uint16_t record_length = 0; + uint8_t part_length; + + str[0] = hostname; + str[1] = MDNS_UTILS_DEFAULT_DOMAIN; + + if (mdns_utils_str_null_or_empty(str[0])) { + return 0; + } + + part_length = append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_A, flush, bye ? 0 : MDNS_ANSWER_A_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + + if ((*index + 3) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + mdns_utils_append_u8(packet, index, ip & 0xFF); + mdns_utils_append_u8(packet, index, (ip >> 8) & 0xFF); + mdns_utils_append_u8(packet, index, (ip >> 16) & 0xFF); + mdns_utils_append_u8(packet, index, (ip >> 24) & 0xFF); + set_u16(packet, data_len_location, 4); + + record_length += 4; + return record_length; +} +#endif /* CONFIG_LWIP_IPV4 */ + +#ifdef CONFIG_LWIP_IPV6 +/** + * @brief appends AAAA record to a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param hostname the hostname address to add + * @param ipv6 the IPv6 address to add + * + * @return length of added data: 0 on error or length on success + */ +static uint16_t append_aaaa_record(uint8_t *packet, uint16_t *index, const char *hostname, uint8_t *ipv6, bool flush, bool bye) +{ + const char *str[2]; + uint16_t record_length = 0; + uint8_t part_length; + + str[0] = hostname; + str[1] = MDNS_UTILS_DEFAULT_DOMAIN; + + if (mdns_utils_str_null_or_empty(str[0])) { + return 0; + } + + + part_length = append_fqdn(packet, index, str, 2, MDNS_MAX_PACKET_SIZE); + if (!part_length) { + return 0; + } + record_length += part_length; + + part_length = append_type(packet, index, MDNS_ANSWER_AAAA, flush, bye ? 0 : MDNS_ANSWER_AAAA_TTL); + if (!part_length) { + return 0; + } + record_length += part_length; + + uint16_t data_len_location = *index - 2; + + if ((*index + MDNS_ANSWER_AAAA_SIZE) > MDNS_MAX_PACKET_SIZE) { + return 0; + } + + part_length = MDNS_ANSWER_AAAA_SIZE; + memcpy(packet + *index, ipv6, part_length); + *index += part_length; + set_u16(packet, data_len_location, part_length); + record_length += part_length; + return record_length; +} +#endif + +static uint8_t append_host_answer(uint8_t *packet, uint16_t *index, mdns_host_item_t *host, + uint8_t address_type, bool flush, bool bye) +{ + mdns_ip_addr_t *addr = host->address_list; + uint8_t num_records = 0; + + while (addr != NULL) { + if (addr->addr.type == address_type) { +#ifdef CONFIG_LWIP_IPV4 + if (address_type == ESP_IPADDR_TYPE_V4 && + append_a_record(packet, index, host->hostname, addr->addr.u_addr.ip4.addr, flush, bye) <= 0) { + break; + } +#endif /* CONFIG_LWIP_IPV4 */ +#ifdef CONFIG_LWIP_IPV6 + if (address_type == ESP_IPADDR_TYPE_V6 && + append_aaaa_record(packet, index, host->hostname, (uint8_t *)addr->addr.u_addr.ip6.addr, flush, + bye) <= 0) { + break; + } +#endif /* CONFIG_LWIP_IPV6 */ + num_records++; + } + addr = addr->next; + } + return num_records; +} + +/** + * @brief Append answer to packet + * + * @return number of answers added to the packet + */ +static uint8_t append_answer(uint8_t *packet, uint16_t *index, mdns_out_answer_t *answer, mdns_if_t tcpip_if) +{ + if (answer->host) { + bool is_host_valid = (mdns_priv_get_self_host() == answer->host); + mdns_host_item_t *target_host = mdns_priv_get_hosts(); + while (target_host && !is_host_valid) { + if (target_host == answer->host) { + is_host_valid = true; + } + target_host = target_host->next; + } + if (!is_host_valid) { + return 0; + } + } + + if (answer->type == MDNS_TYPE_PTR) { + if (answer->service) { + return append_service_ptr_answers(packet, index, answer->service, answer->flush, answer->bye); +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + } else if (answer->host && answer->host->hostname && + (strstr(answer->host->hostname, "in-addr") || strstr(answer->host->hostname, "ip6"))) { + return append_reverse_ptr_record(packet, index, answer->host->hostname) > 0; +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ + } else { + return append_ptr_record(packet, index, + answer->custom_instance, answer->custom_service, answer->custom_proto, + answer->flush, answer->bye) > 0; + } + } else if (answer->type == MDNS_TYPE_SRV) { + return append_srv_record(packet, index, answer->service, answer->flush, answer->bye) > 0; + } else if (answer->type == MDNS_TYPE_TXT) { + return append_txt_record(packet, index, answer->service, answer->flush, answer->bye) > 0; + } else if (answer->type == MDNS_TYPE_SDPTR) { + return append_sdptr_record(packet, index, answer->service, answer->flush, answer->bye) > 0; + } +#ifdef CONFIG_LWIP_IPV4 + else if (answer->type == MDNS_TYPE_A) { + if (answer->host == mdns_priv_get_self_host()) { + esp_netif_ip_info_t if_ip_info; + if (!mdns_priv_if_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && !mdns_priv_pcb_is_duplicate(tcpip_if, + MDNS_IP_PROTOCOL_V4)) { + return 0; + } + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(tcpip_if), &if_ip_info)) { + return 0; + } + if (append_a_record(packet, index, mdns_priv_get_global_hostname(), if_ip_info.ip.addr, answer->flush, answer->bye) <= 0) { + return 0; + } + if (!mdns_priv_pcb_check_for_duplicates(tcpip_if)) { + return 1; + } + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(other_if), &if_ip_info)) { + return 1; + } + if (append_a_record(packet, index, mdns_priv_get_global_hostname(), if_ip_info.ip.addr, answer->flush, answer->bye) > 0) { + return 2; + } + return 1; + } else if (answer->host != NULL) { + return append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V4, answer->flush, answer->bye); + } + } +#endif /* CONFIG_LWIP_IPV4 */ +#ifdef CONFIG_LWIP_IPV6 + else if (answer->type == MDNS_TYPE_AAAA) { + if (answer->host == mdns_priv_get_self_host()) { + struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS]; + uint8_t count = 0; + if (!mdns_priv_if_ready(tcpip_if, MDNS_IP_PROTOCOL_V6) && !mdns_priv_pcb_is_duplicate(tcpip_if, + MDNS_IP_PROTOCOL_V6)) { + return 0; + } + count = esp_netif_get_all_ip6(mdns_priv_get_esp_netif(tcpip_if), if_ip6s); + assert(count <= NETIF_IPV6_MAX_NUMS); + for (int i = 0; i < count; i++) { + if (mdns_utils_ipv6_address_is_zero(if_ip6s[i])) { + return 0; + } + if (append_aaaa_record(packet, index, mdns_priv_get_global_hostname(), (uint8_t *)if_ip6s[i].addr, + answer->flush, answer->bye) <= 0) { + return 0; + } + } + if (!mdns_priv_pcb_check_for_duplicates(tcpip_if)) { + return count; + } + + mdns_if_t other_if = mdns_priv_netif_get_other_interface(tcpip_if); + struct esp_ip6_addr other_ip6; + if (esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(other_if), &other_ip6)) { + return count; + } + if (append_aaaa_record(packet, index, mdns_priv_get_global_hostname(), (uint8_t *)other_ip6.addr, + answer->flush, answer->bye) > 0) { + return 1 + count; + } + return count; + } else if (answer->host != NULL) { + return append_host_answer(packet, index, answer->host, ESP_IPADDR_TYPE_V6, answer->flush, + answer->bye); + } + } +#endif /* CONFIG_LWIP_IPV6 */ + return 0; +} + +/** + * @brief sends a packet + * + * @param p the packet + */ +void mdns_priv_dispatch_tx_packet(mdns_tx_packet_t *p) +{ + static uint8_t packet[MDNS_MAX_PACKET_SIZE]; + uint16_t index = MDNS_HEAD_LEN; + memset(packet, 0, MDNS_HEAD_LEN); + mdns_out_question_t *q; + mdns_out_answer_t *a; + uint8_t count; + + set_u16(packet, MDNS_HEAD_FLAGS_OFFSET, p->flags); + set_u16(packet, MDNS_HEAD_ID_OFFSET, p->id); + + count = 0; + q = p->questions; + while (q) { + if (append_question(packet, &index, q)) { + count++; + } + q = q->next; + } + set_u16(packet, MDNS_HEAD_QUESTIONS_OFFSET, count); + + count = 0; + a = p->answers; + while (a) { + count += append_answer(packet, &index, a, p->tcpip_if); + a = a->next; + } + set_u16(packet, MDNS_HEAD_ANSWERS_OFFSET, count); + + count = 0; + a = p->servers; + while (a) { + count += append_answer(packet, &index, a, p->tcpip_if); + a = a->next; + } + set_u16(packet, MDNS_HEAD_SERVERS_OFFSET, count); + + count = 0; + a = p->additional; + while (a) { + count += append_answer(packet, &index, a, p->tcpip_if); + a = a->next; + } + set_u16(packet, MDNS_HEAD_ADDITIONAL_OFFSET, count); + + DBG_TX_PACKET(p, packet, index); + + mdns_priv_if_write(p->tcpip_if, p->ip_protocol, &p->dst, p->port, packet, index); +} + +/** + * @brief Create probe packet for particular services on particular PCB + */ +mdns_tx_packet_t *mdns_priv_create_probe_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool first, bool include_ip) +{ + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(tcpip_if, ip_protocol); + if (!packet) { + return NULL; + } + + size_t i; + for (i = 0; i < len; i++) { + mdns_out_question_t *q = (mdns_out_question_t *)mdns_mem_malloc(sizeof(mdns_out_question_t)); + if (!q) { + HOOK_MALLOC_FAILED; + mdns_priv_free_tx_packet(packet); + return NULL; + } + q->next = NULL; + q->unicast = first; + q->type = MDNS_TYPE_ANY; + q->host = mdns_utils_get_service_instance_name(services[i]->service); + q->service = services[i]->service->service; + q->proto = services[i]->service->proto; + q->domain = MDNS_UTILS_DEFAULT_DOMAIN; + q->own_dynamic_memory = false; + if (!q->host || question_exists(q, packet->questions)) { + mdns_mem_free(q); + continue; + } else { + queueToEnd(mdns_out_question_t, packet->questions, q); + } + + if (!q->host || !mdns_priv_create_answer(&packet->servers, MDNS_TYPE_SRV, services[i]->service, NULL, false, + false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + } + + if (include_ip) { + if (!append_host_questions_for_services(&packet->questions, services, len, first)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + + if (!mdns_priv_append_host_list_in_services(&packet->servers, services, len, false, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + } + + return packet; +} + +/** + * @brief Create announce packet for particular services on particular PCB + */ +mdns_tx_packet_t *mdns_priv_create_announce_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool include_ip) +{ + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(tcpip_if, ip_protocol); + if (!packet) { + return NULL; + } + packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; + + uint8_t i; + for (i = 0; i < len; i++) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SDPTR, services[i]->service, NULL, false, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, false, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SRV, services[i]->service, NULL, true, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_TXT, services[i]->service, NULL, true, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + } + if (include_ip) { + if (!mdns_priv_append_host_list_in_services(&packet->servers, services, len, true, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + } + return packet; +} + +/** + * @brief Convert probe packet to announce + */ +mdns_tx_packet_t *mdns_priv_create_announce_from_probe(mdns_tx_packet_t *probe) +{ + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(probe->tcpip_if, probe->ip_protocol); + if (!packet) { + return NULL; + } + packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; + + mdns_out_answer_t *s = probe->servers; + while (s) { + if (s->type == MDNS_TYPE_SRV) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SDPTR, s->service, NULL, false, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, s->service, NULL, false, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SRV, s->service, NULL, true, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_TXT, s->service, NULL, true, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + mdns_host_item_t *host = get_host_item(s->service->hostname); + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_A, NULL, host, true, false) + || !mdns_priv_create_answer(&packet->answers, MDNS_TYPE_AAAA, NULL, host, true, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + + } else if (s->type == MDNS_TYPE_A || s->type == MDNS_TYPE_AAAA) { + if (!mdns_priv_create_answer(&packet->answers, s->type, NULL, s->host, true, false)) { + mdns_priv_free_tx_packet(packet); + return NULL; + } + } + + s = s->next; + } + return packet; +} + +void mdns_priv_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip) +{ + mdns_tx_packet_t *packet = mdns_priv_alloc_packet(tcpip_if, ip_protocol); + if (!packet) { + return; + } + packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; + size_t i; + for (i = 0; i < len; i++) { + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) { + mdns_priv_free_tx_packet(packet); + return; + } + } + if (include_ip) { + mdns_priv_append_host_list_in_services(&packet->answers, services, len, true, true); + } + mdns_priv_dispatch_tx_packet(packet); + mdns_priv_free_tx_packet(packet); +} + +/** + * @brief Send bye for particular subtypes + */ +void mdns_priv_send_bye_subtype(mdns_srv_item_t *service, const char *instance_name, mdns_subtype_t *remove_subtypes) +{ + uint8_t i, j; + for (i = 0; i < MDNS_MAX_INTERFACES; i++) { + for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) { + if (mdns_priv_if_ready(i, j)) { + mdns_tx_packet_t *packet = mdns_priv_alloc_packet((mdns_if_t) i, (mdns_ip_protocol_t) j); + if (packet == NULL) { + return; + } + packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE; + if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, service->service, NULL, true, true)) { + mdns_priv_free_tx_packet(packet); + return; + } + + static uint8_t pkt[MDNS_MAX_PACKET_SIZE]; + uint16_t index = MDNS_HEAD_LEN; + memset(pkt, 0, MDNS_HEAD_LEN); + mdns_out_answer_t *a; + uint8_t count; + + set_u16(pkt, MDNS_HEAD_FLAGS_OFFSET, packet->flags); + set_u16(pkt, MDNS_HEAD_ID_OFFSET, packet->id); + + count = 0; + a = packet->answers; + while (a) { + if (a->type == MDNS_TYPE_PTR && a->service) { + const mdns_subtype_t *current_subtype = remove_subtypes; + while (current_subtype) { + count += (append_subtype_ptr_record(pkt, &index, instance_name, current_subtype->subtype, a->service->service, a->service->proto, a->flush, a->bye) > 0); + current_subtype = current_subtype->next; + } + } + a = a->next; + } + set_u16(pkt, MDNS_HEAD_ANSWERS_OFFSET, count); + + mdns_priv_if_write(packet->tcpip_if, packet->ip_protocol, &packet->dst, packet->port, pkt, index); + + mdns_priv_free_tx_packet(packet); + } + } + } +} + +/** + * @brief Find, remove and free answer from the scheduled packets + */ +void mdns_priv_remove_scheduled_answer(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t *service) +{ + mdns_srv_item_t s = {NULL, NULL}; + if (!service) { + service = &s; + } + mdns_tx_packet_t *q = s_tx_queue_head; + while (q) { + if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol && q->distributed) { + mdns_out_answer_t *a = q->answers; + if (a) { + if (a->type == type && a->service == service->service) { + q->answers = q->answers->next; + mdns_mem_free(a); + } else { + while (a->next) { + if (a->next->type == type && a->next->service == service->service) { + mdns_out_answer_t *b = a->next; + a->next = b->next; + mdns_mem_free(b); + break; + } + a = a->next; + } + } + } + } + q = q->next; + } +} + + +/** + * @brief schedules a packet to be sent after given milliseconds + * + * @param packet the packet + * @param ms_after number of milliseconds after which the packet should be dispatched + */ +void mdns_priv_send_after(mdns_tx_packet_t *packet, uint32_t ms_after) +{ + if (!packet) { + return; + } + packet->send_at = (xTaskGetTickCount() * portTICK_PERIOD_MS) + ms_after; + packet->next = NULL; + if (!s_tx_queue_head || s_tx_queue_head->send_at > packet->send_at) { + packet->next = s_tx_queue_head; + s_tx_queue_head = packet; + return; + } + mdns_tx_packet_t *q = s_tx_queue_head; + while (q->next && q->next->send_at <= packet->send_at) { + q = q->next; + } + packet->next = q->next; + q->next = packet; +} + +/** + * @brief free all packets scheduled for sending + */ +void mdns_priv_clear_tx_queue(void) +{ + mdns_tx_packet_t *q; + while (s_tx_queue_head) { + q = s_tx_queue_head; + s_tx_queue_head = s_tx_queue_head->next; + mdns_priv_free_tx_packet(q); + } +} + +/** + * @brief clear packets scheduled for sending on a specific interface + * + * @param tcpip_if the interface + * @param ip_protocol pcb type V4/V6 + */ +void mdns_priv_clear_tx_queue_if(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_tx_packet_t *q, * p; + while (s_tx_queue_head && s_tx_queue_head->tcpip_if == tcpip_if && s_tx_queue_head->ip_protocol == ip_protocol) { + q = s_tx_queue_head; + s_tx_queue_head = s_tx_queue_head->next; + mdns_priv_free_tx_packet(q); + } + if (s_tx_queue_head) { + q = s_tx_queue_head; + while (q->next) { + if (q->next->tcpip_if == tcpip_if && q->next->ip_protocol == ip_protocol) { + p = q->next; + q->next = p->next; + mdns_priv_free_tx_packet(p); + } else { + q = q->next; + } + } + } +} + +/** + * @brief get the next packet scheduled for sending on a specific interface + * + * @param tcpip_if the interface + * @param ip_protocol pcb type V4/V6 + */ +mdns_tx_packet_t *mdns_priv_get_next_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + mdns_tx_packet_t *q = s_tx_queue_head; + while (q) { + if (q->tcpip_if == tcpip_if && q->ip_protocol == ip_protocol) { + return q; + } + q = q->next; + } + return NULL; +} + +/** + * @brief Called from timer task to run mDNS responder + * + * periodically checks first unqueued packet (from tx head). + * if it is scheduled to be transmitted, then pushes the packet to action queue to be handled. + * + */ +void mdns_priv_send_packets(void) +{ + mdns_priv_service_lock(); + mdns_tx_packet_t *p = s_tx_queue_head; + mdns_action_t *action = NULL; + + // find first unqueued packet + while (p && p->queued) { + p = p->next; + } + if (!p) { + mdns_priv_service_unlock(); + return; + } + while (p && (int32_t)(p->send_at - (xTaskGetTickCount() * portTICK_PERIOD_MS)) < 0) { + action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (action) { + action->type = ACTION_TX_HANDLE; + action->data.tx_handle.packet = p; + p->queued = true; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + p->queued = false; + } + } else { + HOOK_MALLOC_FAILED; + break; + } + //Find the next unqued packet + p = p->next; + } + mdns_priv_service_unlock(); +} + + + +/** + * @brief Remove and free service answer from answer list (destination) + */ +static void dealloc_scheduled_service_answers(mdns_out_answer_t **destination, mdns_service_t *service) +{ + mdns_out_answer_t *d = *destination; + if (!d) { + return; + } + while (d && d->service == service) { + *destination = d->next; + mdns_mem_free(d); + d = *destination; + } + while (d && d->next) { + mdns_out_answer_t *a = d->next; + if (a->service == service) { + d->next = a->next; + mdns_mem_free(a); + } else { + d = d->next; + } + } +} + +/** + * @brief Find, remove and free answers and scheduled packets for service + */ +void mdns_priv_remove_scheduled_service_packets(mdns_service_t *service) +{ + if (!service) { + return; + } + mdns_tx_packet_t *p = NULL; + mdns_tx_packet_t *q = s_tx_queue_head; + while (q) { + bool had_answers = (q->answers != NULL); + + dealloc_scheduled_service_answers(&(q->answers), service); + dealloc_scheduled_service_answers(&(q->additional), service); + dealloc_scheduled_service_answers(&(q->servers), service); + + if (mdns_priv_if_ready(q->tcpip_if, q->ip_protocol)) { + bool should_remove_questions = false; + mdns_priv_pcb_check_probing_services(q->tcpip_if, q->ip_protocol, service, + had_answers && q->answers == NULL, &should_remove_questions); + if (should_remove_questions && q->questions) { + mdns_out_question_t *qsn = NULL; + mdns_out_question_t *qs = q->questions; + if (qs->type == MDNS_TYPE_ANY + && qs->service && strcmp(qs->service, service->service) == 0 + && qs->proto && strcmp(qs->proto, service->proto) == 0) { + q->questions = q->questions->next; + mdns_mem_free(qs); + } else while (qs->next) { + qsn = qs->next; + if (qsn->type == MDNS_TYPE_ANY + && qsn->service && strcmp(qsn->service, service->service) == 0 + && qsn->proto && strcmp(qsn->proto, service->proto) == 0) { + qs->next = qsn->next; + mdns_mem_free(qsn); + break; + } + qs = qs->next; + } + } + } + + p = q; + q = q->next; + if (!p->questions && !p->answers && !p->additional && !p->servers) { + queueDetach(mdns_tx_packet_t, s_tx_queue_head, p); + mdns_priv_free_tx_packet(p); + } + } +} + +static void handle_packet(mdns_tx_packet_t *p) +{ + if (mdns_priv_pcb_is_off(p->tcpip_if, p->ip_protocol)) { + mdns_priv_free_tx_packet(p); + return; + } + mdns_priv_dispatch_tx_packet(p); + mdns_priv_pcb_schedule_tx_packet(p); +} + +static void send_packet(mdns_tx_packet_t *packet) +{ + mdns_tx_packet_t *p = s_tx_queue_head; + // packet to be handled should be at tx head, but must be consistent with the one pushed to action queue + if (p && p == packet && p->queued) { + p->queued = false; // clearing, as the packet might be reused (pushed and transmitted again) + s_tx_queue_head = p->next; + handle_packet(p); + } else { + ESP_LOGD(TAG, "Skipping transmit of an unexpected packet!"); + } +} + +/** + * @brief Remove and free answer from answer list (destination) + */ +void mdns_priv_dealloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_srv_item_t *service) +{ + mdns_out_answer_t *d = *destination; + if (!d) { + return; + } + mdns_srv_item_t s = {NULL, NULL}; + if (!service) { + service = &s; + } + if (d->type == type && d->service == service->service) { + *destination = d->next; + mdns_mem_free(d); + return; + } + while (d->next) { + mdns_out_answer_t *a = d->next; + if (a->type == type && a->service == service->service) { + d->next = a->next; + mdns_mem_free(a); + return; + } + d = d->next; + } +} + +void mdns_priv_send_action(mdns_action_t *action, mdns_action_subtype_t type) +{ + if (action->type != ACTION_TX_HANDLE) { + abort(); + } + if (type == ACTION_RUN) { + send_packet(action->data.tx_handle.packet); + } else if (type == ACTION_CLEANUP) { + mdns_priv_free_tx_packet(action->data.tx_handle.packet); + } +} diff --git a/components/mdns/mdns_service.c b/components/mdns/mdns_service.c new file mode 100644 index 0000000000..4f54217cff --- /dev/null +++ b/components/mdns/mdns_service.c @@ -0,0 +1,424 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/semphr.h" +#include "esp_check.h" +#include "mdns.h" +#include "mdns_mem_caps.h" +#include "mdns_utils.h" +#include "mdns_browser.h" +#include "mdns_netif.h" +#include "mdns_send.h" +#include "mdns_receive.h" +#include "mdns_querier.h" +#include "mdns_pcb.h" +#include "mdns_responder.h" + +#define MDNS_SERVICE_STACK_DEPTH CONFIG_MDNS_TASK_STACK_SIZE +#define MDNS_TASK_PRIORITY CONFIG_MDNS_TASK_PRIORITY +#if (MDNS_TASK_PRIORITY > ESP_TASK_PRIO_MAX) +#error "mDNS task priority is higher than ESP_TASK_PRIO_MAX" +#elif (MDNS_TASK_PRIORITY > ESP_TASKD_EVENT_PRIO) +#warning "mDNS task priority is higher than ESP_TASKD_EVENT_PRIO, mDNS library might not work correctly" +#endif +#define MDNS_TASK_AFFINITY CONFIG_MDNS_TASK_AFFINITY + +#define MDNS_SERVICE_LOCK() xSemaphoreTake(s_service_semaphore, portMAX_DELAY) +#define MDNS_SERVICE_UNLOCK() xSemaphoreGive(s_service_semaphore) + +static volatile TaskHandle_t s_service_task_handle = NULL; +static SemaphoreHandle_t s_service_semaphore = NULL; +static StackType_t *s_stack_buffer; +static QueueHandle_t s_action_queue; +static esp_timer_handle_t s_timer_handle; + +static const char *TAG = "mdns_service"; + +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES +static inline char nibble_to_hex(int var) +{ + return var > 9 ? var - 10 + 'a' : var + '0'; +} +#endif + +/** + * @brief Performs interface changes based on system events or custom commands + */ +static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) +{ + if (!mdns_priv_is_server_init() || mdns_if >= MDNS_MAX_INTERFACES) { + return; + } + if (action & MDNS_EVENT_ENABLE_IP4) { + mdns_priv_pcb_enable(mdns_if, MDNS_IP_PROTOCOL_V4); + } + if (action & MDNS_EVENT_ENABLE_IP6) { + mdns_priv_pcb_enable(mdns_if, MDNS_IP_PROTOCOL_V6); + } + if (action & MDNS_EVENT_DISABLE_IP4) { + mdns_priv_pcb_disable(mdns_if, MDNS_IP_PROTOCOL_V4); + } + if (action & MDNS_EVENT_DISABLE_IP6) { + mdns_priv_pcb_disable(mdns_if, MDNS_IP_PROTOCOL_V6); + } + if (action & MDNS_EVENT_ANNOUNCE_IP4) { + mdns_priv_pcb_announce(mdns_if, MDNS_IP_PROTOCOL_V4, NULL, 0, true); + } + if (action & MDNS_EVENT_ANNOUNCE_IP6) { + mdns_priv_pcb_announce(mdns_if, MDNS_IP_PROTOCOL_V6, NULL, 0, true); + } + +#ifdef CONFIG_MDNS_RESPOND_REVERSE_QUERIES +#ifdef CONFIG_LWIP_IPV4 + if (action & MDNS_EVENT_IP4_REVERSE_LOOKUP) { + esp_netif_ip_info_t if_ip_info; + if (esp_netif_get_ip_info(mdns_priv_get_esp_netif(mdns_if), &if_ip_info) == ESP_OK) { + esp_ip4_addr_t *ip = &if_ip_info.ip; + char *reverse_query_name = NULL; + if (asprintf(&reverse_query_name, "%d.%d.%d.%d.in-addr", + esp_ip4_addr4_16(ip), esp_ip4_addr3_16(ip), + esp_ip4_addr2_16(ip), esp_ip4_addr1_16(ip)) > 0 && reverse_query_name) { + ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name); + mdns_priv_delegate_hostname_add(reverse_query_name, NULL); + } + } + } +#endif /* CONFIG_LWIP_IPV4 */ +#ifdef CONFIG_LWIP_IPV6 + if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) { + esp_ip6_addr_t addr6; + if (!esp_netif_get_ip6_linklocal(mdns_priv_get_esp_netif(mdns_if), &addr6) && !mdns_utils_ipv6_address_is_zero(addr6)) { + uint8_t *paddr = (uint8_t *)&addr6.addr; + const char sub[] = "ip6"; + const size_t query_name_size = 4 * sizeof(addr6.addr) /* (2 nibbles + 2 dots)/per byte of IP address */ + sizeof(sub); + char *reverse_query_name = mdns_mem_malloc(query_name_size); + if (reverse_query_name) { + char *ptr = &reverse_query_name[query_name_size]; // point to the end + memcpy(ptr - sizeof(sub), sub, sizeof(sub)); // copy the IP sub-domain + ptr -= sizeof(sub) + 1; // move before the sub-domain + while (reverse_query_name < ptr) { // continue populating reverse query from the end + *ptr-- = '.'; // nibble by nibble, until we reach the beginning + *ptr-- = nibble_to_hex(((*paddr) >> 4) & 0x0F); + *ptr-- = '.'; + *ptr-- = nibble_to_hex((*paddr) & 0x0F); + paddr++; + } + ESP_LOGD(TAG, "Registered reverse query: %s.arpa", reverse_query_name); + mdns_priv_delegate_hostname_add(reverse_query_name, NULL); + } + } + } +#endif /* CONFIG_LWIP_IPV6 */ +#endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ +} + +/** + * @brief Free action data + */ +static void free_action(mdns_action_t *action) +{ + switch (action->type) { + case ACTION_SEARCH_ADD: + case ACTION_SEARCH_SEND: + case ACTION_SEARCH_END: + mdns_priv_query_action(action, ACTION_CLEANUP); + break; + case ACTION_BROWSE_ADD: + case ACTION_BROWSE_END: + case ACTION_BROWSE_SYNC: + mdns_priv_browse_action(action, ACTION_CLEANUP); + break; + case ACTION_TX_HANDLE: + mdns_priv_send_action(action, ACTION_CLEANUP); + break; + case ACTION_RX_HANDLE: + mdns_priv_receive_action(action, ACTION_CLEANUP); + break; + case ACTION_HOSTNAME_SET: + case ACTION_INSTANCE_SET: + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + case ACTION_DELEGATE_HOSTNAME_REMOVE: + case ACTION_DELEGATE_HOSTNAME_ADD: + mdns_priv_responder_action(action, ACTION_CLEANUP); + break; + default: + break; + } + mdns_mem_free(action); +} + +/** + * @brief Called from service thread to execute given action + */ +static void execute_action(mdns_action_t *action) +{ + switch (action->type) { + case ACTION_SYSTEM_EVENT: + perform_event_action(action->data.sys_event.interface, action->data.sys_event.event_action); + break; + case ACTION_SEARCH_ADD: + case ACTION_SEARCH_SEND: + case ACTION_SEARCH_END: + mdns_priv_query_action(action, ACTION_RUN); + break; + case ACTION_BROWSE_ADD: + case ACTION_BROWSE_SYNC: + case ACTION_BROWSE_END: + mdns_priv_browse_action(action, ACTION_RUN); + break; + + case ACTION_TX_HANDLE: + mdns_priv_send_action(action, ACTION_RUN); + break; + case ACTION_RX_HANDLE: + mdns_priv_receive_action(action, ACTION_RUN); + break; + case ACTION_HOSTNAME_SET: + case ACTION_INSTANCE_SET: + case ACTION_DELEGATE_HOSTNAME_ADD: + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + case ACTION_DELEGATE_HOSTNAME_REMOVE: + mdns_priv_responder_action(action, ACTION_RUN); + break; + default: + break; + } + mdns_mem_free(action); +} + +/** + * @brief the main MDNS service task. Packets are received and parsed here + */ +static void service_task(void *pvParameters) +{ + mdns_action_t *a = NULL; + for (;;) { + if (mdns_priv_is_server_init() && s_action_queue) { + if (xQueueReceive(s_action_queue, &a, portMAX_DELAY) == pdTRUE) { + assert(a); + if (a->type == ACTION_TASK_STOP) { + break; + } + MDNS_SERVICE_LOCK(); + execute_action(a); + MDNS_SERVICE_UNLOCK(); + } + } else { + vTaskDelay(500 * portTICK_PERIOD_MS); + } + } + s_service_task_handle = NULL; + vTaskDelay(portMAX_DELAY); +} + +static void timer_cb(void *arg) +{ + mdns_priv_send_packets(); + mdns_priv_query_start_stop(); +} + +static esp_err_t start_timer(void) +{ + esp_timer_create_args_t timer_conf = { + .callback = timer_cb, + .arg = NULL, + .dispatch_method = ESP_TIMER_TASK, + .name = "mdns_timer" + }; + esp_err_t err = esp_timer_create(&timer_conf, &(s_timer_handle)); + if (err) { + return err; + } + return esp_timer_start_periodic(s_timer_handle, MDNS_TIMER_PERIOD_US); +} + +static esp_err_t stop_timer(void) +{ + esp_err_t err = ESP_OK; + if (s_timer_handle) { + err = esp_timer_stop(s_timer_handle); + if (err) { + return err; + } + err = esp_timer_delete(s_timer_handle); + } + return err; +} + +static esp_err_t create_task_with_caps(void) +{ + esp_err_t ret = ESP_OK; + static StaticTask_t mdns_task_buffer; + + s_stack_buffer = mdns_mem_task_malloc(MDNS_SERVICE_STACK_DEPTH); + ESP_GOTO_ON_FALSE(s_stack_buffer != NULL, ESP_FAIL, alloc_failed, TAG, "failed to allocate memory for the mDNS task's stack"); + + s_service_task_handle = xTaskCreateStaticPinnedToCore(service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY, s_stack_buffer, &mdns_task_buffer, MDNS_TASK_AFFINITY); + ESP_GOTO_ON_FALSE(s_service_task_handle != NULL, ESP_FAIL, err, TAG, "failed to create task for the mDNS"); + + return ret; + +alloc_failed: + HOOK_MALLOC_FAILED; +err: + mdns_mem_task_free(s_stack_buffer); + return ret; +} + +/** + * @brief Start the service thread if not running + * + * @return + * - ESP_OK on success + * - ESP_FAIL on error + */ +static esp_err_t service_task_start(void) +{ + esp_err_t ret = ESP_OK; + if (!s_service_semaphore) { + s_service_semaphore = xSemaphoreCreateMutex(); + ESP_RETURN_ON_FALSE(s_service_semaphore != NULL, ESP_FAIL, TAG, "Failed to create the mDNS service lock"); + } + MDNS_SERVICE_LOCK(); + ESP_GOTO_ON_ERROR(start_timer(), err, TAG, "Failed to start the mDNS service timer"); + + if (!s_service_task_handle) { + ESP_GOTO_ON_ERROR(create_task_with_caps(), err_stop_timer, TAG, "Failed to start the mDNS service task"); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) && !CONFIG_IDF_TARGET_LINUX + StackType_t *mdns_debug_stack_buffer; + StaticTask_t *mdns_debug_task_buffer; + xTaskGetStaticBuffers(s_service_task_handle, &mdns_debug_stack_buffer, &mdns_debug_task_buffer); + ESP_LOGD(TAG, "mdns_debug_stack_buffer:%p mdns_debug_task_buffer:%p\n", mdns_debug_stack_buffer, mdns_debug_task_buffer); +#endif // CONFIG_IDF_TARGET_LINUX + } + MDNS_SERVICE_UNLOCK(); + return ret; + +err_stop_timer: + stop_timer(); +err: + MDNS_SERVICE_UNLOCK(); + vSemaphoreDelete(s_service_semaphore); + s_service_semaphore = NULL; + return ret; +} + +/** + * @brief Stop the service thread + * + * @return + * - ESP_OK + */ +static esp_err_t service_task_stop(void) +{ + stop_timer(); + if (s_service_task_handle) { + TaskHandle_t task_handle = s_service_task_handle; + mdns_action_t action; + mdns_action_t *a = &action; + action.type = ACTION_TASK_STOP; + if (xQueueSend(s_action_queue, &a, (TickType_t)0) != pdPASS) { + s_service_task_handle = NULL; + } + while (s_service_task_handle) { + vTaskDelay(10 / portTICK_PERIOD_MS); + } + vTaskDelete(task_handle); + } + vSemaphoreDelete(s_service_semaphore); + s_service_semaphore = NULL; + return ESP_OK; +} + +void mdns_priv_service_lock(void) +{ + MDNS_SERVICE_LOCK(); +} + +void mdns_priv_service_unlock(void) +{ + MDNS_SERVICE_UNLOCK(); +} + +esp_err_t mdns_init(void) +{ + esp_err_t err = ESP_OK; + + if (mdns_priv_is_server_init()) { + return err; + } + + if (mdns_priv_responder_init() != ESP_OK) { + return ESP_ERR_NO_MEM; + } + + s_action_queue = xQueueCreate(MDNS_ACTION_QUEUE_LEN, sizeof(mdns_action_t *)); + if (!s_action_queue) { + err = ESP_ERR_NO_MEM; + goto free_responder; + } + + if (mdns_priv_netif_init() != ESP_OK) { + err = ESP_FAIL; + goto free_queue; + } + + if (service_task_start()) { + //service start failed! + err = ESP_FAIL; + goto free_all_and_disable_pcbs; + } + + return ESP_OK; + +free_all_and_disable_pcbs: + mdns_priv_netif_deinit(); +free_queue: + vQueueDelete(s_action_queue); +free_responder: + mdns_priv_responder_free(); + return err; +} + +void mdns_free(void) +{ + if (!mdns_priv_is_server_init()) { + return; + } + + // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit + mdns_priv_netif_unregister_predefined_handlers(); + + mdns_service_remove_all(); + service_task_stop(); + // at this point, the service task is deleted, so we can destroy the stack size + mdns_mem_task_free(s_stack_buffer); + mdns_priv_pcb_deinit(); + if (s_action_queue) { + mdns_action_t *c; + while (xQueueReceive(s_action_queue, &c, 0) == pdTRUE) { + free_action(c); + } + vQueueDelete(s_action_queue); + } + mdns_priv_clear_tx_queue(); + mdns_priv_query_free(); + mdns_priv_browse_free(); + mdns_priv_responder_free(); +} + +bool mdns_priv_queue_action(mdns_action_t *action) +{ + if (xQueueSend(s_action_queue, &action, (TickType_t)0) != pdPASS) { + return false; + } + return true; +} diff --git a/components/mdns/mdns_utils.c b/components/mdns/mdns_utils.c new file mode 100644 index 0000000000..7e02bad2c3 --- /dev/null +++ b/components/mdns/mdns_utils.c @@ -0,0 +1,270 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "mdns_private.h" +#include "mdns_mem_caps.h" +#include "esp_log.h" +#include "mdns_utils.h" +#include "mdns_responder.h" + +static const char *TAG = "mdns_utils"; +static const char *MDNS_DEFAULT_DOMAIN = "local"; +static const char *MDNS_SUB_STR = "_sub"; + +const uint8_t *mdns_utils_read_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, char *buf, size_t packet_len) +{ + size_t index = 0; + const uint8_t *packet_end = packet + packet_len; + while (start + index < packet_end && start[index]) { + if (name->parts == 4) { + name->invalid = true; + } + uint8_t len = start[index++]; + if (len < 0xC0) { + if (len > 63) { + //length can not be more than 63 + return NULL; + } + uint8_t i; + for (i = 0; i < len; i++) { + if (start + index >= packet_end) { + return NULL; + } + buf[i] = start[index++]; + } + buf[len] = '\0'; + if (name->parts == 1 && buf[0] != '_' + && (strcasecmp(buf, MDNS_DEFAULT_DOMAIN) != 0) + && (strcasecmp(buf, "arpa") != 0) +#ifndef CONFIG_MDNS_RESPOND_REVERSE_QUERIES + && (strcasecmp(buf, "ip6") != 0) + && (strcasecmp(buf, "in-addr") != 0) +#endif + ) { + strlcat(name->host, ".", sizeof(name->host)); + strlcat(name->host, buf, sizeof(name->host)); + } else if (strcasecmp(buf, MDNS_SUB_STR) == 0) { + name->sub = 1; + } else if (!name->invalid) { + char *mdns_name_ptrs[] = {name->host, name->service, name->proto, name->domain}; + memcpy(mdns_name_ptrs[name->parts++], buf, len + 1); + } + } else { + size_t address = (((uint16_t)len & 0x3F) << 8) | start[index++]; + if ((packet + address) >= start) { + //reference address can not be after where we are + return NULL; + } + if (mdns_utils_read_fqdn(packet, packet + address, name, buf, packet_len)) { + return start + index; + } + return NULL; + } + } + return start + index + 1; +} + +const uint8_t *mdns_utils_parse_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, size_t packet_len) +{ + name->parts = 0; + name->sub = 0; + name->host[0] = 0; + name->service[0] = 0; + name->proto[0] = 0; + name->domain[0] = 0; + name->invalid = false; + + static char buf[MDNS_NAME_BUF_LEN]; + + const uint8_t *next_data = (uint8_t *) mdns_utils_read_fqdn(packet, start, name, buf, packet_len); + if (!next_data) { + return 0; + } + if (!name->parts || name->invalid) { + return next_data; + } + if (name->parts == 3) { + memmove((uint8_t *)name + (MDNS_NAME_BUF_LEN), (uint8_t *)name, 3 * (MDNS_NAME_BUF_LEN)); + name->host[0] = 0; + } else if (name->parts == 2) { + memmove((uint8_t *)(name->domain), (uint8_t *)(name->service), (MDNS_NAME_BUF_LEN)); + name->service[0] = 0; + name->proto[0] = 0; + } + if (strcasecmp(name->domain, MDNS_DEFAULT_DOMAIN) == 0 || strcasecmp(name->domain, "arpa") == 0) { + return next_data; + } + name->invalid = true; // mark the current name invalid, but continue with other question + return next_data; +} + +bool mdns_utils_hostname_is_ours(const char *hostname) +{ + if (!mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname()) && + strcasecmp(hostname, mdns_priv_get_global_hostname()) == 0) { + return true; + } + mdns_host_item_t *host = mdns_priv_get_hosts(); + while (host != NULL) { + if (strcasecmp(hostname, host->hostname) == 0) { + return true; + } + host = host->next; + } + return false; +} + +bool mdns_utils_service_match(const mdns_service_t *srv, const char *service, const char *proto, + const char *hostname) +{ + if (!service || !proto || !srv || !srv->hostname) { + return false; + } + return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) && + (mdns_utils_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname)); +} + +mdns_srv_item_t *mdns_utils_get_service_item(const char *service, const char *proto, const char *hostname) +{ + mdns_srv_item_t *s = mdns_priv_get_services(); + while (s) { + if (mdns_utils_service_match(s->service, service, proto, hostname)) { + return s; + } + s = s->next; + } + return NULL; +} + +mdns_srv_item_t *mdns_utils_get_service_item_instance(const char *instance, const char *service, const char *proto, + const char *hostname) +{ + mdns_srv_item_t *s = mdns_priv_get_services(); + while (s) { + if (instance) { + if (mdns_utils_service_match_instance(s->service, instance, service, proto, hostname)) { + return s; + } + } else { + if (mdns_utils_service_match(s->service, service, proto, hostname)) { + return s; + } + } + s = s->next; + } + return NULL; +} + +bool mdns_utils_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service, + const char *proto, const char *hostname) +{ + // service and proto must be supplied, if not this instance won't match + if (!service || !proto) { + return false; + } + // instance==NULL -> mdns_utils_instance_name_match() will check the default instance + // hostname==NULL -> matches if instance, service and proto matches + return !strcasecmp(srv->service, service) && mdns_utils_instance_name_match(srv->instance, instance) && + !strcasecmp(srv->proto, proto) && (mdns_utils_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname)); +} + +static const char *get_default_instance_name(void) +{ + const char* instance = mdns_priv_get_instance(); + if (!mdns_utils_str_null_or_empty(instance)) { + return instance; + } + + const char* host = mdns_priv_get_global_hostname(); + if (!mdns_utils_str_null_or_empty(host)) { + return host; + } + return NULL; +} + +/** + * @brief Get the service name of a service + */ +const char *mdns_utils_get_service_instance_name(const mdns_service_t *service) +{ + if (service && !mdns_utils_str_null_or_empty(service->instance)) { + return service->instance; + } + + return get_default_instance_name(); +} + + +bool mdns_utils_instance_name_match(const char *lhs, const char *rhs) +{ + if (lhs == NULL) { + lhs = get_default_instance_name(); + } + if (rhs == NULL) { + rhs = get_default_instance_name(); + } + return !strcasecmp(lhs, rhs); +} + + +mdns_ip_addr_t *mdns_utils_copy_address_list(const mdns_ip_addr_t *address_list) +{ + mdns_ip_addr_t *head = NULL; + mdns_ip_addr_t *tail = NULL; + while (address_list != NULL) { + mdns_ip_addr_t *addr = (mdns_ip_addr_t *)mdns_mem_malloc(sizeof(mdns_ip_addr_t)); + if (addr == NULL) { + HOOK_MALLOC_FAILED; + mdns_utils_free_address_list(head); + return NULL; + } + addr->addr = address_list->addr; + addr->next = NULL; + if (head == NULL) { + head = addr; + tail = addr; + } else { + tail->next = addr; + tail = tail->next; + } + address_list = address_list->next; + } + return head; +} + +void mdns_utils_free_address_list(mdns_ip_addr_t *address_list) +{ + while (address_list != NULL) { + mdns_ip_addr_t *next = address_list->next; + mdns_mem_free(address_list); + address_list = next; + } +} + +#ifdef CONFIG_LWIP_IPV6 +bool mdns_utils_ipv6_address_is_zero(esp_ip6_addr_t ip6) +{ + uint8_t i; + uint8_t *data = (uint8_t *)ip6.addr; + for (i = 0; i < MDNS_UTILS_SIZEOF_IP6_ADDR; i++) { + if (data[i]) { + return false; + } + } + return true; +} +#endif /* CONFIG_LWIP_IPV6 */ + +uint8_t mdns_utils_append_u16(uint8_t *packet, uint16_t *index, uint16_t value) +{ + if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + mdns_utils_append_u8(packet, index, (value >> 8) & 0xFF); + mdns_utils_append_u8(packet, index, value & 0xFF); + return 2; +} diff --git a/components/mdns/private_include/mdns_browser.h b/components/mdns/private_include/mdns_browser.h new file mode 100644 index 0000000000..30b7f7ac28 --- /dev/null +++ b/components/mdns/private_include/mdns_browser.h @@ -0,0 +1,78 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Free browse item queue + * + * @note Called from mdns_free() + */ +void mdns_priv_browse_free(void); + +/** + * @brief Looks for the name/type in active browse items + * + * @note Called from the packet parser (mdns_receive.c) + * + * @return browse results + */ +mdns_browse_t *mdns_priv_browse_find(mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Send out all browse queries + * + * @note Called from the network events (mdns_netif.c) + * @note Calls (indirectly) search-send from mdns_querier.c, which sends out the query + */ +void mdns_priv_browse_send_all(mdns_if_t mdns_if); + +/** + * @brief Sync browse results + * + * @note Called from the packet parser + * @note Calls mdns_priv_queue_action() from mdns_engine + */ +esp_err_t mdns_priv_browse_sync(mdns_browse_sync_t *browse_sync); + +/** + * @brief Perform action from mdns service queue + * + * @note Called from the _mdns_service_task() in mdns.c + */ +void mdns_priv_browse_action(mdns_action_t *action, mdns_action_subtype_t type); + +/** + * @brief Add a TXT record to the browse result + * + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_browse_result_add_txt(mdns_browse_t *browse, const char *instance, const char *service, const char *proto, + mdns_txt_item_t *txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, + uint32_t ttl, mdns_browse_sync_t *out_sync_browse); +/** + * @brief Add an IP record to the browse result + * + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_browse_result_add_ip(mdns_browse_t *browse, const char *hostname, esp_ip_addr_t *ip, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse); +/** + * @brief Add a SRV record to the browse result + * + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_browse_result_add_srv(mdns_browse_t *browse, const char *hostname, const char *instance, const char *service, const char *proto, + uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl, mdns_browse_sync_t *out_sync_browse); +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_debug.h b/components/mdns/private_include/mdns_debug.h new file mode 100644 index 0000000000..e980389ce3 --- /dev/null +++ b/components/mdns/private_include/mdns_debug.h @@ -0,0 +1,65 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "sdkconfig.h" +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef CONFIG_MDNS_ENABLE_DEBUG_PRINTS +#include "esp_log.h" + +/* Define the debug macros for the mDNS module + */ +#define DBG_BROWSE_RESULTS(result, browse) mdns_debug_printf_browse_result(result, browse) + +#define DBG_BROWSE_RESULTS_WITH_MSG(result, ...) do { \ + ESP_LOGD("mdns", __VA_ARGS__); \ + mdns_debug_printf_browse_result_all(result); \ + } while(0) + +#define DBG_TX_PACKET(packet, data, len) mdns_debug_tx_packet(packet, data, len) + +#define DBG_RX_PACKET(packet, data, len) mdns_debug_rx_packet(packet, data, len) + +/** + * @brief Print the browse results + */ +void mdns_debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t); + +/** + * @brief Print all the browse results + */ +void mdns_debug_printf_browse_result_all(mdns_result_t *r_t); + +/** + * @brief Print the tx packet + */ +void mdns_debug_tx_packet(mdns_tx_packet_t *p, uint8_t packet[MDNS_MAX_PACKET_SIZE], uint16_t index); + +/** + * @brief Print the rx packet + */ +void mdns_debug_rx_packet(mdns_rx_packet_t *packet, const uint8_t* data, uint16_t len); + +#else + +/* Define the dummy debug macros if debugging is OFF + */ +#define DBG_BROWSE_RESULTS(result, browse) +#define DBG_BROWSE_RESULTS_WITH_MSG(result, ...) +#define DBG_TX_PACKET(packet, data, len) +#define DBG_RX_PACKET(packet, data, len) + +#endif // CONFIG_MDNS_ENABLE_DEBUG_PRINTS + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_netif.h b/components/mdns/private_include/mdns_netif.h new file mode 100644 index 0000000000..42aa5f1ff0 --- /dev/null +++ b/components/mdns/private_include/mdns_netif.h @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Initialize the mDNS network interfaces + * + * @note Called from mdns_init() in mdns.c + */ +esp_err_t mdns_priv_netif_init(void); + +/** + * @brief Deinitialize the mDNS network interfaces + * + * @note Called from mdns_init() in mdns.c + */ +esp_err_t mdns_priv_netif_deinit(void); + +/** + * @brief Unregister predefined (default) network interfaces + * + * @note Called from mdns_free() in mdns.c + * + */ +void mdns_priv_netif_unregister_predefined_handlers(void); + +/** + * @brief Clean the internal netif for the particular interface + * + * @note Called from mdns_responder on disabling pcbs + */ +void mdns_priv_netif_disable(mdns_if_t tcpip_if); + +/** + * @brief Returns potentially duplicated interface + * + * @note Called from multiple places where Rx and Tx packets are processed + */ +mdns_if_t mdns_priv_netif_get_other_interface(mdns_if_t tcpip_if); + +/** + * @brief Gets the actual esp_netif pointer from the internal network interface list + * + * The supplied ordinal number could + * - point to a predef netif -> "STA", "AP", "ETH" + * - if no entry in the list (NULL) -> check if the system added this netif + * - point to a custom netif -> just return the entry in the list + * - users is responsible for the lifetime of this netif (to be valid between mdns-init -> deinit) + * + * @param tcpip_if Ordinal number of the interface + * @return Pointer ot the esp_netif object if the interface is available, NULL otherwise + */ +esp_netif_t *mdns_priv_get_esp_netif(mdns_if_t tcpip_if); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_networking.h b/components/mdns/private_include/mdns_networking.h index 83cbe90cc9..9b5e396bc0 100644 --- a/components/mdns/private_include/mdns_networking.h +++ b/components/mdns/private_include/mdns_networking.h @@ -1,10 +1,9 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ -#ifndef ESP_MDNS_NETWORKING_H_ -#define ESP_MDNS_NETWORKING_H_ +#pragma once /* * MDNS Server Networking -- private include @@ -13,23 +12,24 @@ #include "mdns.h" #include "mdns_private.h" +#ifdef __cplusplus +extern "C" { +#endif /** - * @brief Queue RX packet action + * @brief Check if the netif on the selected interfacce and protocol is ready */ -esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet); - -bool mdns_is_netif_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +bool mdns_priv_if_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); /** * @brief Start PCB */ -esp_err_t _mdns_pcb_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +esp_err_t mdns_priv_if_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); /** * @brief Stop PCB */ -esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); +esp_err_t mdns_priv_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); /** * @brief send packet over UDP @@ -40,21 +40,23 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); * * @return length of sent packet or 0 on error */ -size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len); +size_t mdns_priv_if_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len); /** * @brief Gets data pointer to the mDNS packet */ -void *_mdns_get_packet_data(mdns_rx_packet_t *packet); +void *mdns_priv_get_packet_data(mdns_rx_packet_t *packet); /** * @brief Gets data length of c */ -size_t _mdns_get_packet_len(mdns_rx_packet_t *packet); +size_t mdns_priv_get_packet_len(mdns_rx_packet_t *packet); /** * @brief Free the mDNS packet */ -void _mdns_packet_free(mdns_rx_packet_t *packet); +void mdns_priv_packet_free(mdns_rx_packet_t *packet); -#endif /* ESP_MDNS_NETWORKING_H_ */ +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_pcb.h b/components/mdns/private_include/mdns_pcb.h new file mode 100644 index 0000000000..723bdca209 --- /dev/null +++ b/components/mdns/private_include/mdns_pcb.h @@ -0,0 +1,121 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Disable the PCB for the selected interface and protocol + * @note Called from the main module (deinit and event handler) + */ +void mdns_priv_pcb_disable(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Enable the PCB for the selected interface and protocol + * @note Called from the main module (deinit and event handler) + */ +void mdns_priv_pcb_enable(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Set the interface as duplicate + * @note Called from the packet parser when checking for collisions + */ +void mdns_priv_pcb_set_duplicate(mdns_if_t tcpip_if); + +/** + * @brief Send announcement on particular PCB + * @note Called mainly from mdns.c to send announcements on all interfaces + */ +void mdns_priv_pcb_announce(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip); + +/** + * @brief Checks if the PCB is OFF + * @note Called from mdns_send.c + */ +bool mdns_priv_pcb_is_off(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Schedules TX packets for various PCB states of probing and announcing + * @note Called from mdns_send.c + */ +void mdns_priv_pcb_schedule_tx_packet(mdns_tx_packet_t *p); + +/** + * @brief Update probing services on certain service removal + * @note Called from mdns_send.c upon removing scheduled packets for a service + */ +void mdns_priv_pcb_check_probing_services(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_service_t *service, bool removed_answers, bool *should_remove_questions); + +/** + * @brief Deinit pcbs + * @note Called from mdns_free() + */ +void mdns_priv_pcb_deinit(void); + +/** + * @brief Checks if the netif and PCB is initialized + * @note Called from mdns_send.c + */ +bool mdsn_priv_pcb_is_inited(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Checks if PCB is duplicated + * @note Called from mdns_send.c + */ +bool mdns_priv_pcb_is_duplicate(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Checks if PCB is probing + * @note Called from mdns_receive.c + */ +bool mdns_priv_pcb_is_probing(mdns_rx_packet_t *packet); + +/** + * @brief Set probe failed to PCB + * @note Called from mdns_receive.c + */ +void mdns_priv_pcb_set_probe_failed(mdns_rx_packet_t *packet); + +/** + * @brief Checks if PCB completed probing + * @note Called from mdns_receive.c + */ +bool mdns_priv_pcb_is_after_probing(mdns_rx_packet_t *packet); + +/** + * @brief Checks if the selected interface has a duplicate + * @note Called from mdns_send.c + */ +bool mdns_priv_pcb_check_for_duplicates(mdns_if_t tcpip_if); + +/** + * @brief Sends bye for particular services on particular PCB + * @note Called from mdns.c + */ +void mdns_priv_pcb_send_bye_service(mdns_srv_item_t **services, size_t len, bool include_ip); + +/** + * @brief Send probe on all active PCBs + */ +void mdns_priv_probe_all_pcbs(mdns_srv_item_t **services, size_t len, bool probe_ip, bool clear_old_probe); + +/** + * @brief Send probe for particular services on particular PCB + * + * Tests possible duplication on probing service structure and probes only for new entries. + * - If pcb probing then add only non-probing services and restarts probing + * - If pcb not probing, run probing for all specified services + */ +void mdns_priv_init_pcb_probe(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool probe_ip); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 6bc0891f42..e48c85f80b 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -3,8 +3,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -#ifndef MDNS_PRIVATE_H_ -#define MDNS_PRIVATE_H_ +#pragma once #include "sdkconfig.h" #include "mdns.h" @@ -16,11 +15,6 @@ #include "esp_timer.h" #include "esp_system.h" -#ifdef CONFIG_MDNS_ENABLE_DEBUG_PRINTS -#define MDNS_ENABLE_DEBUG -#define _mdns_dbg_printf(...) printf(__VA_ARGS__) -#endif - /** Number of predefined interfaces */ #ifndef CONFIG_MDNS_PREDEF_NETIF_STA #define CONFIG_MDNS_PREDEF_NETIF_STA 0 @@ -90,14 +84,6 @@ #define MDNS_ANSWER_AAAA_SIZE 16 #define MDNS_SERVICE_PORT 5353 // UDP port that the server runs on -#define MDNS_SERVICE_STACK_DEPTH CONFIG_MDNS_TASK_STACK_SIZE -#define MDNS_TASK_PRIORITY CONFIG_MDNS_TASK_PRIORITY -#if (MDNS_TASK_PRIORITY > ESP_TASK_PRIO_MAX) -#error "mDNS task priority is higher than ESP_TASK_PRIO_MAX" -#elif (MDNS_TASK_PRIORITY > ESP_TASKD_EVENT_PRIO) -#warning "mDNS task priority is higher than ESP_TASKD_EVENT_PRIO, mDNS library might not work correctly" -#endif -#define MDNS_TASK_AFFINITY CONFIG_MDNS_TASK_AFFINITY #define MDNS_SERVICE_ADD_TIMEOUT_MS CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS #define MDNS_PACKET_QUEUE_LEN 16 // Maximum packets that can be queued for parsing @@ -126,9 +112,6 @@ #define MDNS_TIMER_PERIOD_US (CONFIG_MDNS_TIMER_PERIOD_MS*1000) -#define MDNS_SERVICE_LOCK() xSemaphoreTake(_mdns_service_semaphore, portMAX_DELAY) -#define MDNS_SERVICE_UNLOCK() xSemaphoreGive(_mdns_service_semaphore) - #define queueToEnd(type, queue, item) \ if (!queue) { \ queue = item; \ @@ -156,23 +139,12 @@ #define queueFree(type, queue) while (queue) { type * _q = queue; queue = queue->next; mdns_mem_free(_q); } -#define PCB_STATE_IS_PROBING(s) (s->state > PCB_OFF && s->state < PCB_ANNOUNCE_1) -#define PCB_STATE_IS_ANNOUNCING(s) (s->state > PCB_PROBE_3 && s->state < PCB_RUNNING) -#define PCB_STATE_IS_RUNNING(s) (s->state == PCB_RUNNING) - #ifndef HOOK_MALLOC_FAILED -#define HOOK_MALLOC_FAILED ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %" PRIu32 " bytes)", __LINE__, esp_get_free_heap_size()); +#define HOOK_MALLOC_FAILED do { ESP_LOGE(TAG, "Cannot allocate memory (%s(%d), free heap: %" PRIu32 " bytes)", __func__, __LINE__, esp_get_free_heap_size()); } while(0) #endif typedef size_t mdns_if_t; -typedef enum { - PCB_OFF, PCB_DUP, PCB_INIT, - PCB_PROBE_1, PCB_PROBE_2, PCB_PROBE_3, - PCB_ANNOUNCE_1, PCB_ANNOUNCE_2, PCB_ANNOUNCE_3, - PCB_RUNNING -} mdns_pcb_state_t; - typedef enum { MDNS_ANSWER, MDNS_NS, MDNS_EXTRA } mdns_parsed_record_type_t; @@ -196,6 +168,10 @@ typedef enum { ACTION_MAX } mdns_action_type_t; +typedef enum { + ACTION_RUN, + ACTION_CLEANUP, +} mdns_action_subtype_t; typedef struct { uint16_t id; @@ -342,15 +318,6 @@ typedef struct mdns_tx_packet_s { uint16_t id; } mdns_tx_packet_t; -typedef struct { - mdns_pcb_state_t state; - mdns_srv_item_t **probe_services; - uint8_t probe_services_len; - uint8_t probe_ip; - uint8_t probe_running; - uint16_t failed_probes; -} mdns_pcb_t; - typedef enum { SEARCH_OFF, SEARCH_INIT, @@ -405,21 +372,6 @@ typedef struct mdns_browse_sync { mdns_browse_result_sync_t *sync_result; } mdns_browse_sync_t; -typedef struct mdns_server_s { - struct { - mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX]; - } interfaces[MDNS_MAX_INTERFACES]; - const char *hostname; - const char *instance; - mdns_srv_item_t *services; - QueueHandle_t action_queue; - SemaphoreHandle_t action_sema; - mdns_tx_packet_t *tx_queue_head; - mdns_search_once_t *search_once; - esp_timer_handle_t timer_handle; - mdns_browse_t *browse; -} mdns_server_t; - typedef struct { mdns_action_type_t type; union { @@ -452,17 +404,3 @@ typedef struct { } browse_sync; } data; } mdns_action_t; - -/* - * @brief Convert mnds if to esp-netif handle - * - * @param tcpip_if mdns supported interface as internal enum - * - * @return - * - ptr to esp-netif on success - * - NULL if no available netif for current interface index - */ -esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if); - - -#endif /* MDNS_PRIVATE_H_ */ diff --git a/components/mdns/private_include/mdns_querier.h b/components/mdns/private_include/mdns_querier.h new file mode 100644 index 0000000000..50467121fe --- /dev/null +++ b/components/mdns/private_include/mdns_querier.h @@ -0,0 +1,111 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Start or stop requested queries + * @note Called periodically from timer task in mdns.c + */ +void mdns_priv_query_start_stop(void); + +/** + * @brief Free search item queue + * @note Called from mdns_free() + */ +void mdns_priv_query_free(void); + +/** + * @brief Free search results + * @note Called from multiple modules (browser, querier, core) + */ +void mdns_priv_query_results_free(mdns_result_t *results); + +/** + * @brief Complete the query if max results reached + * @note Called from the packet parser + */ +void mdns_priv_query_done(void); + +/** +* @brief Looks for the name/type in active search items +* +* @note Called from the packet parser (mdns_receive.c) +* +* @return search results +*/ +mdns_search_once_t *mdns_priv_query_find(mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Looks for the name/type in the search items + * + * @note Called from the packet parser (mdns_receive.c) + */ +mdns_search_once_t *mdns_priv_query_find_from(mdns_search_once_t *s, mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Add TXT item to the search results + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_query_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len, + size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, + uint32_t ttl); +/** + * @brief Add IP (A/AAAA records) to the search results + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_query_result_add_ip(mdns_search_once_t *search, const char *hostname, esp_ip_addr_t *ip, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl); + +/** + * @brief Add SRV record to the search results + * @note Called from the packet parser (mdns_receive.c) + */ +void mdns_priv_query_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port, + mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl); + +/** + * @brief Add PTR record to the search results + * @note Called from the packet parser (mdns_receive.c) + */ +mdns_result_t *mdns_priv_query_result_add_ptr(mdns_search_once_t *search, const char *instance, + const char *service_type, const char *proto, mdns_if_t tcpip_if, + mdns_ip_protocol_t ip_protocol, uint32_t ttl); + +/** + * @brief Perform action from mdns service queue + * + * @note Called from the _mdns_service_task() in mdns.c + */ +void mdns_priv_query_action(mdns_action_t *action, mdns_action_subtype_t type); + +/** + * @brief Create linked IP (copy) from parsed one + */ +mdns_ip_addr_t *mdns_priv_result_addr_create_ip(esp_ip_addr_t *ip); + +/** + * @brief Update TTL results to whichever is smaller + */ +static inline void mdns_priv_query_update_result_ttl(mdns_result_t *r, uint32_t ttl) +{ + r->ttl = r->ttl < ttl ? r->ttl : ttl; +} + +/** + * @brief Send a search query packet on the specified interface and protocol + */ +void mdns_priv_query_send(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_receive.h b/components/mdns/private_include/mdns_receive.h new file mode 100644 index 0000000000..03cad700e3 --- /dev/null +++ b/components/mdns/private_include/mdns_receive.h @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* @brief Perform action from mdns service queue +* +* @note Called from the _mdns_service_task() in mdns.c +*/ +void mdns_priv_receive_action(mdns_action_t *action, mdns_action_subtype_t type); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_responder.h b/components/mdns/private_include/mdns_responder.h new file mode 100644 index 0000000000..fc0fbedb92 --- /dev/null +++ b/components/mdns/private_include/mdns_responder.h @@ -0,0 +1,98 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* @brief Perform action from mdns responder +* +* @note Called from the _mdns_service_task() in mdns.c +*/ +void mdns_priv_responder_action(mdns_action_t *action, mdns_action_subtype_t type); + +/** + * @brief Initializes responder storage + */ +esp_err_t mdns_priv_responder_init(void); + +/** + * @brief Frees responder storage + */ +void mdns_priv_responder_free(void); + +/** + * @brief get global (mdns_server->hostname) hostname + */ +const char *mdns_priv_get_global_hostname(void); + +/** + * @brief get service list + */ +mdns_srv_item_t *mdns_priv_get_services(void); + +/** + * @brief get host list + */ +mdns_host_item_t *mdns_priv_get_hosts(void); + +/** + * @brief get self host + */ +mdns_host_item_t *mdns_priv_get_self_host(void); + +/** + * @brief set global hostname + */ +void mdns_priv_set_global_hostname(const char *hostname); + +/** + * @brief get mdns_server->instance + */ +const char *mdns_priv_get_instance(void); + +/** + * @brief set mdns_server->instance + */ +void mdns_priv_set_instance(const char *instance); + +/** + * @brief Returns true if the mdns server is initialized + */ +bool mdns_priv_is_server_init(void); + +/** + * @brief Restart the responder on all services without instance + */ +void mdns_priv_restart_all_pcbs_no_instance(void); + +/** + * @brief Restart the responder on all active PCBs + */ +void mdns_priv_restart_all_pcbs(void); + +/** + * @brief Adds a delegated hostname to the linked list + * @param hostname Host name pointer + * @param address_list Address list + * @return true on success + * false if the host wasn't attached (this is our hostname, or alloc failure) so we have to free the structs + */ +bool mdns_priv_delegate_hostname_add(const char *hostname, mdns_ip_addr_t *address_list); + +/** + * @brief Remaps hostname of self service + */ +void mdns_priv_remap_self_service_hostname(const char *old_hostname, const char *new_hostname); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_send.h b/components/mdns/private_include/mdns_send.h new file mode 100644 index 0000000000..43fa91687f --- /dev/null +++ b/components/mdns/private_include/mdns_send.h @@ -0,0 +1,131 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Free a transmit packet and its associated resources + */ +void mdns_priv_free_tx_packet(mdns_tx_packet_t *packet); + +/** + * @brief Create a probe packet for service discovery + */ +mdns_tx_packet_t *mdns_priv_create_probe_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool first, bool include_ip); + +/** + * @brief Allocate and initialize an answer record for mDNS response + */ +bool mdns_priv_create_answer(mdns_out_answer_t **destination, uint16_t type, mdns_service_t *service, + mdns_host_item_t *host, bool flush, bool bye); + +/** + * @brief Create an announcement packet for service advertisement + */ +mdns_tx_packet_t *mdns_priv_create_announce_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t *services[], size_t len, bool include_ip); + +/** + * @brief Create an announcement packet from an existing probe packet + */ +mdns_tx_packet_t *mdns_priv_create_announce_from_probe(mdns_tx_packet_t *probe); + +/** + * @brief Dispatch a transmit packet for sending + */ +void mdns_priv_dispatch_tx_packet(mdns_tx_packet_t *p); + +/** + * @brief Send a goodbye message for a service subtype + */ +void mdns_priv_send_bye_subtype(mdns_srv_item_t *service, const char *instance_name, mdns_subtype_t *remove_subtypes); + +/** + * @brief Append host list information to service records + */ +bool mdns_priv_append_host_list_in_services(mdns_out_answer_t **destination, mdns_srv_item_t *services[], size_t services_len, bool flush, bool bye); + +/** + * @brief Clear the transmit queue head for a specific interface and protocol + */ +void mdns_priv_clear_tx_queue_if(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Clear the global transmit queue head + */ +void mdns_priv_clear_tx_queue(void); + +/** + * @brief Remove scheduled service packets for a given service + */ +void mdns_priv_remove_scheduled_service_packets(mdns_service_t *service); + +/** + * @brief Get the next packet from the PCB queue for a specific interface and protocol + */ +mdns_tx_packet_t *mdns_priv_get_next_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Deallocate an answer record + */ +void mdns_priv_dealloc_answer(mdns_out_answer_t **destination, uint16_t type, mdns_srv_item_t *service); + +/** + * @brief Remove a scheduled answer for a specific interface, protocol, and service + */ +void mdns_priv_remove_scheduled_answer(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint16_t type, mdns_srv_item_t *service); + +/** + * @brief Allocate a new packet with default settings + */ +mdns_tx_packet_t *mdns_priv_alloc_packet(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol); + +/** + * @brief Send all pending mDNS packets + */ +void mdns_priv_send_packets(void); + +/** + * @brief Execute an mDNS action with specified type + */ +void mdns_priv_send_action(mdns_action_t *action, mdns_action_subtype_t type); + +/** + * @brief Schedule a transmit packet for sending after a specified delay + */ +void mdns_priv_send_after(mdns_tx_packet_t *packet, uint32_t ms_after); + +/** + * @brief Send by for particular services on particular PCB + */ +void mdns_priv_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, mdns_srv_item_t **services, size_t len, bool include_ip); + +/** + * @brief Create answer packet to questions from parsed packet + */ +void mdns_priv_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_packet); + +/** + * @brief appends one TXT record ("key=value" or "key") + * + * @param packet MDNS packet + * @param index offset in the packet + * @param txt one txt record + * + * @return length of added data: length of the added txt value + 1 on success + * 0 if data won't fit the packet + * -1 if invalid TXT entry + */ +int mdns_priv_append_one_txt_record_entry(uint8_t *packet, uint16_t *index, mdns_txt_linked_item_t *txt); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_service.h b/components/mdns/private_include/mdns_service.h new file mode 100644 index 0000000000..597da0f590 --- /dev/null +++ b/components/mdns/private_include/mdns_service.h @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "mdns_private.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Lock mdns service + */ +void mdns_priv_service_lock(void); + +/** + * @brief Unlock mdns service + */ +void mdns_priv_service_unlock(void); + +/** + * @brief Send the given action to the service queue + */ +bool mdns_priv_queue_action(mdns_action_t *action); + +#ifdef __cplusplus +} +#endif diff --git a/components/mdns/private_include/mdns_utils.h b/components/mdns/private_include/mdns_utils.h new file mode 100644 index 0000000000..470bb153b0 --- /dev/null +++ b/components/mdns/private_include/mdns_utils.h @@ -0,0 +1,155 @@ +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "mdns_private.h" + +#define MDNS_UTILS_DEFAULT_DOMAIN "local" +#define MDNS_UTILS_SIZEOF_IP6_ADDR (MDNS_ANSWER_AAAA_SIZE) + +/** + * @brief read uint16_t from a packet + * @param packet the packet + * @param index index in the packet where the value starts + * + * @return the value + */ +static inline uint16_t mdns_utils_read_u16(const uint8_t *packet, uint16_t index) +{ + return (uint16_t)(packet[index]) << 8 | packet[index + 1]; +} + +/** + * @brief read uint32_t from a packet + * @param packet the packet + * @param index index in the packet where the value starts + * + * @return the value + */ +static inline uint32_t mdns_utils_read_u32(const uint8_t *packet, uint16_t index) +{ + return (uint32_t)(packet[index]) << 24 | (uint32_t)(packet[index + 1]) << 16 | (uint32_t)(packet[index + 2]) << 8 | packet[index + 3]; +} + +/** + * @brief Check for null or empty string + */ +static inline bool mdns_utils_str_null_or_empty(const char *str) +{ + return (str == NULL || *str == 0); +} + +/** + * @brief reads and formats MDNS FQDN into mdns_name_t structure + * + * @param packet MDNS packet + * @param start Starting point of FQDN + * @param name mdns_name_t structure to populate + * + * @return the address after the parsed FQDN in the packet or NULL on error + */ +const uint8_t *mdns_utils_parse_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, size_t packet_len); + +/** + * @brief reads MDNS FQDN into mdns_name_t structure + * FQDN is in format: [hostname.|[instance.]_service._proto.]local. + * + * @param packet MDNS packet + * @param start Starting point of FQDN + * @param name mdns_name_t structure to populate + * @param buf temporary char buffer + * + * @return the address after the parsed FQDN in the packet or NULL on error + */ +const uint8_t *mdns_utils_read_fqdn(const uint8_t *packet, const uint8_t *start, mdns_name_t *name, char *buf, size_t packet_len); + +/** + * @brief Check if IPv6 address is NULL + */ +bool mdns_utils_ipv6_address_is_zero(esp_ip6_addr_t ip6); + + +/** + * @brief Checks if the hostname is ours + */ +bool mdns_utils_hostname_is_ours(const char *hostname); + +/** + * @brief Checks if given service matches with arguments + */ +bool mdns_utils_service_match(const mdns_service_t *srv, const char *service, const char *proto, + const char *hostname); + +/** + * @brief finds service from given service type + * @param server the server + * @param service service type to match + * @param proto proto to match + * @param hostname hostname of the service (if non-null) + * + * @return the service item if found or NULL on error + */ +mdns_srv_item_t *mdns_utils_get_service_item(const char *service, const char *proto, const char *hostname); + +/** + * @brief finds service from given instance, etc + */ +mdns_srv_item_t *mdns_utils_get_service_item_instance(const char *instance, const char *service, const char *proto, + const char *hostname); + +/** + * @brief returns true if the service matches with args + */ +bool mdns_utils_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service, + const char *proto, const char *hostname); + +/** + * @brief returns true if the two instances match + */ +bool mdns_utils_instance_name_match(const char *lhs, const char *rhs); + +/** + * @brief Get service instance name + */ +const char *mdns_utils_get_service_instance_name(const mdns_service_t *service); + +/** + * @brief Copy address list + */ +mdns_ip_addr_t *mdns_utils_copy_address_list(const mdns_ip_addr_t *address_list); + +/** + * @brief Free address list + */ +void mdns_utils_free_address_list(mdns_ip_addr_t *address_list); + +/** + * @brief appends uint16_t in a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param value the value to set + * + * @return length of added data: 0 on error or 2 on success + */ +uint8_t mdns_utils_append_u16(uint8_t *packet, uint16_t *index, uint16_t value); + +/** + * @brief appends byte in a packet, incrementing the index + * + * @param packet MDNS packet + * @param index offset in the packet + * @param value the value to set + * + * @return length of added data: 0 on error or 1 on success + */ +static inline uint8_t mdns_utils_append_u8(uint8_t *packet, uint16_t *index, uint8_t value) +{ + if (*index >= MDNS_MAX_PACKET_SIZE) { + return 0; + } + packet[*index] = value; + *index += 1; + return 1; +} diff --git a/components/mdns/refactor_2025.md b/components/mdns/refactor_2025.md new file mode 100644 index 0000000000..53b6fd7f1d --- /dev/null +++ b/components/mdns/refactor_2025.md @@ -0,0 +1,56 @@ +# MDNS Component Refactor + +## Introduction + +The MDNS component has been refactored from a single monolithic file `mdns.c` into a set of focused modules with clear responsibilities. This restructuring maintains the same functionality while improving code organization, maintainability, and testability. + +The refactor has been performed in thee major stages, to ensure smooth transition to production while avoiding major changes in untested paths. These stages are: +[x] Restructuring into several modules while keeping the actual code changes to minimum +[ ] Cover functionality of each module by specifically focused unit tests. +[ ] Optimizations on module levels (string and queue operations, esp_timer dependency, socket task improvements, error checking) + +## Module Structure + +### mdns_service.c +The central service module that orchestrates the MDNS component operation. It manages the service task, timer, action queue, and system event handling. This module provides the entry points for the public API and coordinates actions between different modules. It's responsible for the general lifecycle of the MDNS service. + +### mdns_responder.c +Implements the responder functionality that answers incoming MDNS queries. It manages hostname and service registration, and generates appropriate response packets based on the queried information. The responder tracks all registered services and their associated metadata, handling hostname conflicts and service announcement. + +### mdns_browser.c +Handles browse/query functionality, managing browse requests for MDNS services on the network. It provides the infrastructure for adding, tracking, and removing browse requests, and processes incoming service discovery responses. The module manages browsing state and notifies applications when services appear or disappear. + +### mdns_querier.c +Handles outgoing query functionality, maintaining state for ongoing queries and processing query responses. It supports one-shot and continuous queries, manages query timeouts, and organizes results. The module provides the infrastructure for applications to discover services and resolve hostnames. + +### mdns_send.c +Manages packet generation and transmission. It constructs properly formatted MDNS packets, with correct headers and resource records, and handles the transmission scheduling and queueing of outgoing messages. This module encapsulates knowledge about the MDNS protocol packet structure. + +### mdns_receive.c +Processes incoming MDNS packets, parsing and validating their structure. It extracts queries and responses from received packets and dispatches them to the appropriate handling modules. This module focuses on packet parsing and initial classification. + +### mdns_pcb.c +Manages protocol control blocks (PCBs) that handle the state machine for MDNS interface operation. It coordinates the probing, announcing, and running states for each network interface and protocol combination. This module is responsible for service conflict detection, duplicate interface handling, and ensuring proper service announcements. + +### mdns_networking_lwip.c and mdns_networking_socket.c +These modules provide networking abstraction for different underlying network stacks. They handle multicast group management, packet reception, and transmission over the network interfaces. This abstraction allows the MDNS implementation to work with different networking stacks. + +### mdns_netif.c +Manages network interface registration and monitoring. It tracks network interface state changes and notifies the MDNS service when interfaces become available or unavailable, enabling dynamic adaptation to network configuration changes. + +### mdns_utils.c +Contains common utility functions used across multiple modules, such as string handling, data structure operations, and debug helpers. This module centralizes shared functionality to avoid code duplication. + +### mdns_mem_caps.c +Provides memory allocation functions with capability-based allocation support. It encapsulates memory management operations, making it easier to track and debug memory usage across the MDNS component. + +### mdns_debug.c +Contains debugging and logging utilities specific to the MDNS component. It provides structured logging and debug information for troubleshooting complex MDNS operations. + +## Testability Improvements + +The refactored module structure significantly enhances testability by clearly separating different functional areas. Each module can now be tested in isolation with proper mocking of its dependencies. The separation of networking, packet handling, and business logic makes it possible to create focused unit tests without the complexity of managing the entire MDNS stack. Module-specific state is now contained within individual modules rather than distributed throughout a monolithic codebase, making test case setup and verification more straightforward. The refactoring also improves the ability to simulate various network conditions and edge cases by intercepting inter-module communication at well-defined boundaries. + +## Conclusion + +The MDNS component refactoring provides a more maintainable and testable architecture while preserving the existing public API. By breaking down the monolithic implementation into focused modules with clear responsibilities, the codebase becomes easier to understand, extend, and maintain. The refactored structure makes it simpler to identify and fix bugs, implement new features, and adapt to future requirements. This architectural improvement supports long-term evolution of the component while maintaining backward compatibility for existing applications. diff --git a/components/mdns/refactoring_details.md b/components/mdns/refactoring_details.md new file mode 100644 index 0000000000..8ee403d905 --- /dev/null +++ b/components/mdns/refactoring_details.md @@ -0,0 +1,244 @@ +| Original Function | Refactored Function | Concerns | +|------------------|--------------------|---------| +| [mdns_if_from_preset_if](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L136) | [mdns_if_from_preset](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L87) | The function name was changed from `mdns_if_from_preset_if` to `mdns_if_from_preset`, but the logic remains identical. While this appears to be a systematic naming convention change (supported by the evidence that all call sites in the refactored codebase now use the new name), there's a risk that some call sites in the original codebase might not have been updated, or that external code depending on the original function name could break. The functionality itself is preserved, but the interface change could cause linkage or compilation issues if not all dependencies are updated. | +| [esp_netif_from_preset_if](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L151) | [netif_from_preset](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L102) | | +| [_mdns_get_esp_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L179) | [mdns_priv_get_esp_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L118) | The function `esp_netif_from_preset_if()` has been replaced with `netif_from_preset()` in the refactored code. While both functions appear to serve the same purpose of looking up predefined network interfaces, this change could introduce subtle bugs if:
1. The new function has different error handling behavior
2. The new function returns different values for edge cases (e.g., when interfaces are not available)
3. The new function has different performance characteristics that could affect timing-sensitive MDNS operations

The original `esp_netif_from_preset_if()` was an inline function with a switch statement mapping predefined interface types to actual esp_netif instances. If `netif_from_preset()` has a different implementation or doesn't handle all the same `mdns_predef_if_t` enum values, this could break MDNS functionality for predefined interfaces like STA, AP, or ETH when they become available after initialization. | +| [_mdns_clean_netif_ptr](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L196) | [mdns_priv_netif_disable](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L134) | | +| [_mdns_get_if_from_esp_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L207) | [get_if_from_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L144) | The function `esp_netif_from_preset_if` was renamed to `netif_from_preset` in the refactored code. While both functions appear to serve the same purpose (converting a predefined interface enum to an esp_netif pointer), this change could introduce subtle bugs if the implementation of `netif_from_preset` differs from the original `esp_netif_from_preset_if` function, particularly in error handling, return values for unsupported interface types, or behavioral differences in edge cases. | +| [_str_null_or_empty](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L224) | [mdns_utils_str_null_or_empty](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.h#L38) | The refactoring introduced a potential compilation issue. The original function `_str_null_or_empty` was defined as `static inline` within `mdns.c`, making it only visible within that translation unit. The refactored version `mdns_utils_str_null_or_empty` is also `static inline` but in `mdns_utils.h`, which means it needs to be properly included in all files that use it. From the search results, I can see that `mdns_browser.c`, `mdns_debug.c`, and `mdns_responder.c` are using the new function, but I cannot verify if the original `mdns.c` file has been updated to include `mdns_utils.h` and replace all calls to `_str_null_or_empty` with `mdns_utils_str_null_or_empty`. If the original `mdns.c` still contains calls to the old function name without including the new header, this will cause compilation failures. | +| [_mdns_mangle_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L232) | [mangle_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L138) | | +| [_mdns_service_match](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L272) | [mdns_utils_service_match](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L121) | The refactored function adds a null check for the 'srv' parameter (!srv), which changes the behavior from the original function. In the original implementation, callers could potentially pass NULL for 'srv', which would cause a segmentation fault when accessing srv->hostname. The refactored version now explicitly checks for NULL and returns false, which may mask underlying bugs in calling code that should be fixed rather than silently handled. Additionally, the function visibility changed from static to public, which could break encapsulation if this was intended to be an internal helper function. | +| [_mdns_get_service_item](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L291) | [mdns_utils_get_service_item](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L131) | The refactored function changed from static to non-static visibility and now uses mdns_priv_get_services() instead of directly accessing _mdns_server->services. This could introduce subtle bugs related to:
1. Thread safety - the original function was likely called within MDNS_SERVICE_LOCK/UNLOCK contexts, but the refactored version's access pattern through mdns_priv_get_services() may not provide the same guarantees
2. Encapsulation - making the function non-static exposes internal service lookup logic that was previously hidden
3. Dependency on mdns_utils_service_match having identical behavior to _mdns_service_match, which needs verification
4. Potential null pointer issues if mdns_priv_get_services() returns NULL when _mdns_server->services would not be NULL | +| [_mdns_get_service_item_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L303) | [get_service_item_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L39) | | +| [mdns_get_host_item](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L321) | [get_host_item](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L240) | The refactored get_host_item function introduces a potential NULL return when the hostname matches the server's hostname or is NULL. In the original implementation, &_mdns_self_host was always returned (a guaranteed non-NULL address), but the refactored version calls mdns_priv_get_self_host() which can return NULL if s_server is NULL. This could break callers that assume non-NULL returns for self-host cases, potentially causing segmentation faults or incorrect behavior in service discovery and host resolution. | +| [_mdns_can_add_more_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L336) | [can_add_more_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L129) | | +| [_mdns_send_rx_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L354) | [send_rx_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_networking_lwip.c#L48) | The refactored function 'send_rx_action' replaces the direct xQueueSend call with mdns_priv_queue_action, but the implementation of mdns_priv_queue_action is not visible in the provided context. This introduces a potential subtle bug if mdns_priv_queue_action does not properly handle the queue operation with the same blocking/non-blocking behavior (original used (TickType_t)0 timeout) or if it returns incorrect boolean values that don't match the expected ESP_OK/ESP_ERR_NO_MEM semantics. Additionally, the function was changed from global to static scope, which could break external callers if not properly addressed in the refactoring. | +| [_mdns_get_default_instance_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L373) | [get_default_instance_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L175) | The refactored function introduces potential subtle bugs due to the separation of concerns. Specifically:
1. The helper functions mdns_priv_get_instance() and mdns_priv_get_global_hostname() may have different NULL-checking behavior than the original direct _mdns_server access
2. There could be thread safety issues if s_server (the refactored equivalent of _mdns_server) is accessed without proper synchronization
3. The behavior when s_server is NULL may not exactly match the original behavior, particularly in edge cases
4. The replacement of _str_null_or_empty with mdns_utils_str_null_or_empty could have different implementations that handle edge cases differently | +| [_mdns_get_service_instance_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L389) | [mdns_utils_get_service_instance_name](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L192) | The refactored function get_default_instance_name() can return NULL when both mdns_priv_get_instance() and mdns_priv_get_global_hostname() return NULL or empty strings, whereas the original _mdns_get_default_instance_name() appears to always return a non-NULL string. This behavioral change could lead to null pointer dereferences in callers that expect a valid string, as evidenced by the original usage patterns where the return value is used directly in string operations without null checks. | +| [_mdns_instance_name_match](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L398) | [mdns_utils_instance_name_match](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L202) | The refactored function `mdns_utils_instance_name_match` calls `get_default_instance_name()` which uses `mdns_priv_get_instance()` internally, while the original function `_mdns_instance_name_match` used `_mdns_get_default_instance_name()` that directly accessed `_mdns_server->instance`. The behavioral equivalence depends on whether `mdns_priv_get_instance()` returns the same value as the original direct server access, particularly in edge cases where the server state might be inconsistent or during initialization/teardown phases. | +| [_mdns_service_match_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L409) | [mdns_utils_service_match_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L162) | The refactored function changed from static to public scope (removed static keyword), which could break encapsulation and allow unintended external access to internal service matching logic. Additionally, the helper function `mdns_utils_str_null_or_empty` might have different behavior than the original `_str_null_or_empty` if it was reimplemented during the utility extraction. | +| [_mdns_get_service_item_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L422) | [mdns_utils_get_service_item_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L143) | | +| [_mdns_read_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L452) | [mdns_utils_read_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L18) | The refactored function mdns_utils_parse_fqdn uses a static buffer (static char buf[MDNS_NAME_BUF_LEN]) which could cause thread safety issues in a multi-threaded environment or reentrancy problems if the function is called recursively. Additionally, the memory movement operations using memmove in mdns_utils_parse_fqdn appear to be incorrectly calculating the offsets - the destination and source calculations seem to assume the mdns_name_t structure fields are exactly MDNS_NAME_BUF_LEN bytes apart, which may not be correct if the structure has padding or different field sizes. | +| [_mdns_set_u16](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L512) | ??? | | +| [_mdns_append_u8](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L530) | [mdns_utils_append_u8](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.h#L147) | | +| [_mdns_append_u16](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L549) | [mdns_utils_append_u16](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L262) | | +| [_mdns_append_u32](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L568) | [append_u32](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L39) | The bounds check in the refactored function uses `(*index + sizeof(uint32_t)) >= MDNS_MAX_PACKET_SIZE` while the original used `(*index + 3) >= MDNS_MAX_PACKET_SIZE`. Although `sizeof(uint32_t)` should typically be 4, this creates a potential portability issue if compiled on a system where `uint32_t` has a different size. Additionally, the refactored function now depends on `mdns_utils_append_u8` which has different error handling - it returns 0 on bounds violation but doesn't prevent the index from being incremented in subsequent calls, potentially causing inconsistent state. | +| [_mdns_append_type](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L590) | [append_type](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L61) | The refactored function uses inconsistent function naming: `mdns_utils_append_u16` and `append_u32` (without the `mdns_utils_` prefix). This suggests either a missing utility function implementation or inconsistent refactoring that could lead to compilation errors if `append_u32` is not properly defined in the same context. Additionally, the buffer size calculation changed from hardcoded 10 to using sizeof operations, which while functionally equivalent, introduces potential maintenance complexity. | +| [_mdns_append_string_with_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L622) | [append_string](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L103) | The refactored function `append_string` introduces a subtle bug by replacing the explicit length parameter with `strlen(string)`. This breaks compatibility with:
1. Non-null-terminated strings that were previously supported
2. Binary data that might contain embedded null bytes
3. Callers who want to control the exact length written (important for DNS label encoding)
4. Embedded systems use cases where string length might be known without scanning

Additionally, the return value behavior has changed from returning the number of bytes written (len + 1) to potentially returning 0 on failure, which may break error handling logic in callers expecting the original return value pattern. | +| [_mdns_append_string](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L642) | [append_string](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L103) | The refactored function `append_single_str` has a critical semantic change in its return value. While the original `_mdns_append_string` returns the length of added data (string length + 1) on success, `append_single_str` returns the updated packet index. This could break calling code that relies on the original return value semantics for length calculations or error checking. Additionally, `append_single_str` introduces an extra error check for `mdns_utils_append_u8` that wasn't present in the original, which changes the error handling behavior. | +| [append_one_txt_record_entry](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L665) | [mdns_priv_append_one_txt_record_entry](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L3399) | The boundary check condition `(*index + len + 1) >= MDNS_MAX_PACKET_SIZE` in both original and refactored code may be incorrect. It should likely use `>` instead of `>=` to prevent buffer overflow when `*index + len + 1` exactly equals `MDNS_MAX_PACKET_SIZE`. This would write one byte beyond the allocated buffer size. The refactoring preserved this potential off-by-one error rather than fixing it. | +| [append_single_str](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L686) | [append_single_str](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L430) | The refactored append_single_str function now calls mdns_utils_append_u8 instead of _mdns_append_u8, but the return value check is incorrect. The original _mdns_append_u8 likely returned 0 on failure, but the new mdns_utils_append_u8 returns 1 on success and 0 on failure. However, the refactored code checks "if (!mdns_utils_append_u8(...))" which is correct for the new return semantics, but there's a potential issue: the original function returned the final packet index on success, while the new helper returns a boolean. This could cause issues if any calling code depends on the specific return value semantics beyond just success/failure checking. | +| [append_fqdn_dots](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L711) | [append_fqdn_dots](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L455) | The refactored append_fqdn_dots function in Result 1 has a subtle bug: it always appends "arpa" suffix regardless of the input domain, whereas the original function's logic suggests this should only be used for reverse DNS queries. The function also ignores the 'last' parameter completely, which may indicate missing functionality for handling non-reverse DNS queries. Additionally, the hardcoded "arpa" suffix could cause incorrect FQDN formatting for regular forward DNS queries. | +| [_mdns_append_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L750) | [append_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L667) | The refactored function calls `append_string` instead of `_mdns_append_string`, but the search results show no implementation of `append_string` in the refactored codebase. This suggests the function may be missing, which would cause linking errors or runtime failures. Additionally, the recursive call to `append_fqdn` relies on this potentially missing function, creating a dependency chain that may be broken. | +| [_mdns_append_ptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L823) | [append_ptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L781) | | +| [_mdns_append_subtype_ptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L873) | ??? | | +| [_mdns_append_sdptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L918) | [append_sdptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1038) | | +| [_mdns_append_txt_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L968) | [append_txt_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L974) | The refactored function uses renamed helper functions (mdns_utils_get_service_instance_name, append_fqdn, append_type, mdns_priv_append_one_txt_record_entry, set_u16) and constants (MDNS_UTILS_DEFAULT_DOMAIN) without verification that these have identical behavior and error handling as their original counterparts. Specifically, if any of these helper functions now have different buffer bounds checking, error return values, or memory management behavior, it could introduce subtle bugs in packet construction, buffer overflow protection, or error propagation. | +| [_mdns_append_srv_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1032) | [append_srv_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L902) | The refactored function replaces direct struct member access `_mdns_server->hostname` with a function call `mdns_priv_get_global_hostname()`. This could introduce subtle bugs if:
1. The function returns NULL when the original struct member was guaranteed to be non-NULL
2. The function has different error handling semantics
3. The function introduces side effects or performance overhead not present in the original
4. The function's behavior differs in edge cases (e.g., during initialization/shutdown)

Additionally, the function name changes from `_mdns_append_u16` to `mdns_utils_append_u16` and similar utility function renames could introduce behavioral differences if the implementations are not functionally equivalent. | +| [_mdns_append_a_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1105) | [append_a_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1089) | The boundary check condition `(*index + 3) >= MDNS_MAX_PACKET_SIZE` is preserved from the original but appears to be incorrect. When *index equals MDNS_MAX_PACKET_SIZE - 3, the condition evaluates to true (preventing the write), but the code needs to write 4 bytes starting from *index. This means the valid range should be *index <= MDNS_MAX_PACKET_SIZE - 4. The condition should likely be `(*index + 4) > MDNS_MAX_PACKET_SIZE` or `(*index > MDNS_MAX_PACKET_SIZE - 4)` to properly prevent buffer overflow. | +| [_mdns_append_aaaa_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1157) | [append_aaaa_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1141) | The refactored function uses renamed utility functions (append_fqdn, append_type, set_u16, mdns_utils_str_null_or_empty) and constants (MDNS_UTILS_DEFAULT_DOMAIN) without verification that they maintain identical behavior to the original implementations. Specifically, if any of these helper functions now have different error handling, boundary checking, or return value semantics, it could cause subtle bugs in AAAA record construction. The domain name change from MDNS_DEFAULT_DOMAIN to MDNS_UTILS_DEFAULT_DOMAIN could also affect DNS resolution if the values differ. | +| [_mdns_append_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1201) | [append_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L734) | The refactored function uses `append_fqdn` and `mdns_utils_append_u16` which may have different error handling behavior than the original `_mdns_append_fqdn` and `_mdns_append_u16` functions. Specifically, the bounds checking in `mdns_utils_append_u16` returns 0 on error, but the original function's behavior is unknown. Additionally, the cumulative length calculation in the refactored function could be incorrect if the utility functions return 0 on error, as it would add 0 to `part_length` instead of properly handling the error case. | +| [_mdns_get_other_if](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1242) | [mdns_priv_netif_get_other_interface](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L73) | | +| [_mdns_if_is_dup](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1253) | [mdns_priv_pcb_check_for_duplicates](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L89) | The refactored function uses a different data structure (s_pcbs) and interface lookup mechanism (s_esp_netifs[tcpip_if].duplicate) compared to the original (_mdns_server->interfaces[...].pcbs and _mdns_get_other_if). Without verification that these data structures are properly initialized and contain equivalent state information, there's a risk of accessing uninitialized memory or incorrect duplicate detection. Additionally, the refactored code assumes MDNS_IP_PROTOCOL_MAX equals 2 (covering both IPv4 and IPv6), which may not match the original implementation's explicit protocol checks. | +| [_ipv6_address_is_zero](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1273) | [mdns_utils_ipv6_address_is_zero](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L249) | | +| [_mdns_append_host_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1286) | [append_host_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1182) | The refactored append_answer function introduces new host validation logic that checks if the host is valid (either self-host or in the hosts list) before processing. This validation was not present in the original _mdns_append_host_answer function and could potentially block legitimate host answers if the validation logic is incorrect or if there are timing issues with the host list management. Additionally, the return value semantics have changed - the original function returned the count of successfully appended records, while append_answer returns different values (0, 1, 2, count) depending on the context, which could break the calling code's expectations. | +| [_mdns_append_reverse_ptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1318) | [append_reverse_ptr_record](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L485) | The refactored function uses `mdns_priv_get_self_host()->hostname` without checking if `mdns_priv_get_self_host()` returns NULL. In the original code, `_mdns_self_host.hostname` was accessed directly, suggesting `_mdns_self_host` was a global struct. The new access pattern through a function call introduces a potential null pointer dereference if the function returns NULL when the self host is not properly initialized. | +| [_mdns_append_service_ptr_answers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1350) | [append_service_ptr_answers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L870) | | +| [_mdns_append_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1378) | [append_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1215) | The refactoring introduces potential behavioral changes in the IPv4 and IPv6 address handling logic. Specifically:

1. The original code checked `_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP` and `_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP`, but the refactored code uses `!mdns_priv_pcb_is_duplicate(tcpip_if, MDNS_IP_PROTOCOL_V4)` and `!mdns_priv_pcb_is_duplicate(tcpip_if, MDNS_IP_PROTOCOL_V6)`. The logic appears inverted - the original checks if state is NOT PCB_DUP, while the new function name suggests it checks if IS duplicate.

2. The original used `_mdns_if_is_dup(tcpip_if)` while the refactored uses `mdns_priv_pcb_check_for_duplicates(tcpip_if)`. These appear to be different checks with potentially different semantics.

3. The IPv6 zero address check changed from `_ipv6_address_is_zero(if_ip6s[i])` to `mdns_utils_ipv6_address_is_zero(if_ip6s[i])`. Without seeing the implementation of both functions, we cannot guarantee identical behavior.

These changes could affect when additional interface addresses are appended and when IPv6 addresses are considered valid, potentially breaking the duplicate interface handling logic and IPv6 address validation. | +| [_mdns_dispatch_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1490) | [mdns_priv_dispatch_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1329) | The refactoring changed the function from static to global visibility (mdns_priv_dispatch_tx_packet), which breaks encapsulation and could allow unintended external calls to this internal packet dispatch function. Additionally, the static buffer 'packet[MDNS_MAX_PACKET_SIZE]' remains, creating potential thread safety issues if multiple threads call this function concurrently, as they would share the same buffer. The debug functionality extraction to DBG_TX_PACKET macro and mdns_debug_tx_packet function appears correct, but without seeing the macro definition, we cannot verify if debug code is properly conditionally compiled. | +| [_mdns_free_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1559) | [mdns_priv_free_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L187) | | +| [_mdns_schedule_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1588) | [mdns_priv_send_after](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1617) | The refactored function mdns_priv_send_after has changed from static to non-static visibility, which could lead to unintended external access to the internal packet scheduling mechanism. Additionally, the global queue variable has been renamed from _mdns_server->tx_queue_head to s_tx_queue_head, but without seeing the broader context of how s_tx_queue_head is declared and managed, there may be synchronization issues if multiple threads can access this queue concurrently. The original function was tightly coupled with the _mdns_server structure, and this refactoring appears to separate the queue management from the server context without clear evidence of proper synchronization mechanisms being added. | +| [_mdns_clear_tx_queue_head](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1611) | [mdns_priv_clear_tx_queue](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2107) | The refactored function uses a global variable `s_tx_queue_head` instead of accessing it through `_mdns_server->tx_queue_head`. This changes the data structure from a server-specific queue to a global queue, which could introduce race conditions in multi-server scenarios or break the encapsulation of server state. Additionally, the function name change suggests it's now part of a different module (mdns_send.c), which may indicate architectural changes that need verification across the entire codebase. | +| [_mdns_clear_pcb_tx_queue_head](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1627) | [mdns_priv_clear_tx_queue_if](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1656) | | +| [_mdns_get_next_pcb_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1655) | [mdns_priv_get_next_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2294) | The refactored function changed from accessing `_mdns_server->tx_queue_head` to `s_tx_queue_head`, which represents a change in global state management. This could introduce subtle bugs if:
1. `s_tx_queue_head` is not properly initialized or synchronized with the rest of the system
2. Other parts of the codebase still reference `_mdns_server->tx_queue_head` inconsistently
3. The queue management operations (add/remove packets) use different queue head variables in different modules

Additionally, the function visibility changed from static to non-static, which could break encapsulation and allow unintended external access to internal queue operations. | +| [_mdns_remove_scheduled_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1670) | [mdns_priv_remove_scheduled_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2558) | The refactored function changed from static to non-static scope while maintaining the pattern of creating a local mdns_srv_item_t variable when service is NULL. This could lead to undefined behavior if the function is called from multiple threads or contexts, as the local variable's address is used for comparison in the linked list traversal. Additionally, the global variable access changed from _mdns_server->tx_queue_head to s_tx_queue_head, which may have different synchronization guarantees. | +| [_mdns_dealloc_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1704) | [mdns_priv_dealloc_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2408) | The refactored function `mdns_priv_dealloc_answer` has a potential bug when `service` parameter is NULL. In the original function, when `service` is NULL, it creates a local `mdns_srv_item_t s` with NULL pointers and uses `service = &s`, then compares `d->service == service->service`. However, `service->service` will be NULL in this case, so it's comparing `d->service == NULL`. This logic is preserved in the refactored version, but the comparison `d->service == service->service` when `service` points to a stack-allocated structure with NULL members could be confusing and error-prone. The function appears to be trying to match answers where the service pointer is NULL, but this pattern is fragile and depends on the caller's understanding of when to pass NULL vs an actual service. | +| [_mdns_alloc_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1733) | [mdns_priv_create_answer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L1053) | The refactored function `mdns_priv_create_answer` has lost the `static` qualifier that was present in the original `_mdns_alloc_answer` function. This changes the function's visibility from file-scope to global scope, which could lead to unintended external access and potential linkage issues. Additionally, the function name change from `_mdns_alloc_answer` to `mdns_priv_create_answer` suggests it should remain private, but removing the `static` qualifier contradicts this intention. | +| [_mdns_alloc_packet_default](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1763) | [mdns_priv_alloc_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2698) | The function visibility has changed from static to non-static (exported), which could break encapsulation and allow unintended external access to this internal packet allocation function. This may lead to architectural issues where external code could bypass proper MDNS APIs and directly manipulate packet allocation, potentially causing memory management issues or protocol violations. | +| [_mdns_create_answer_from_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1789) | [create_answer_from_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L255) | The refactored function uses `mdns_priv_get_self_host()` instead of comparing directly with `&_mdns_self_host`, which could introduce subtle behavioral differences if the function doesn't return the expected reference. Additionally, the MDNS_TYPE_SDPTR case consistently uses `false` for the `send_flush` parameter in both versions, but this might be an existing issue that wasn't addressed during refactoring. | +| [_mdns_create_answer_from_hostname](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1824) | [create_answer_from_hostname](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L292) | | +| [_mdns_service_match_ptr_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1834) | [service_match_ptr_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L302) | The refactored function uses renamed helper functions (mdns_utils_service_match and mdns_utils_get_service_instance_name) without verification that they maintain identical behavior to the original _mdns_service_match and _mdns_get_service_instance_name functions. If these helper functions have different error handling, null checking, or return value semantics, it could introduce subtle bugs in service discovery logic. | +| [_mdns_create_answer_from_parsed_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L1862) | [mdns_priv_create_answer_from_parsed_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L3276) | The refactored function has changed from static to non-static visibility, which could break encapsulation and allow unintended external access. Additionally, the function `mdns_priv_get_services()` replaces direct access to `mdns_server->services`, but without seeing its implementation, we cannot verify it returns the same service list with proper synchronization. The `get_host_item()` function also replaces `mdns_get_host_item()` - if these have different implementations, it could affect host matching logic and service discovery behavior. | +| [_mdns_question_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2009) | [question_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L377) | | +| [_mdns_append_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2023) | [append_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L326) | The refactored `append_host_list` function contains a potential linked list traversal bug. The original code in `_mdns_append_host_list` correctly traversed the host list by advancing the pointer after processing each host. However, the refactored version advances `host = host->next` before calling `append_host`, which means:
1. The first host in the list is skipped entirely
2. The last iteration will attempt to access `host->next` which could be NULL, leading to potential NULL pointer dereference in `append_host`
3. This could result in missing host entries in MDNS responses

The correct pattern should be:
```c
while (host != NULL) {
if (!append_host(destination, host, flush, bye)) {
return false;
}
host = host->next;
}
``` | +| [_mdns_append_host_list_in_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2034) | [mdns_priv_append_host_list_in_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L1834) | The refactored function changes the hostname lookup mechanism from `_mdns_server->hostname` to `mdns_priv_get_global_hostname()`. This could introduce subtle bugs if:
1. The global hostname differs from the server hostname in certain contexts
2. The `mdns_priv_get_global_hostname()` function has different initialization timing or state dependencies
3. There are race conditions where the global hostname changes during packet construction

Additionally, the function dependencies have changed from `mdns_get_host_item` and `_mdns_append_host` to `get_host_item` and `append_host`. Without verifying these functions have identical behavior, there's risk of:
- Different error handling semantics
- Changed memory management patterns
- Modified host lookup logic | +| [_mdns_append_host_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2053) | [append_host_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L356) | The refactored append_host_list function has a critical bug in the while loop logic. The original code correctly processes the current host before advancing to the next one (host = host->next at the end of the loop). The refactored version advances the pointer before processing (host = host->next at the beginning of the loop), which causes it to skip the first host in the list and potentially access invalid memory when the list is empty or has only one element. | +| [_mdns_append_host_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2071) | [append_host_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L391) | The refactored function uses MDNS_UTILS_DEFAULT_DOMAIN instead of MDNS_DEFAULT_DOMAIN, which could be a different constant value. Additionally, it calls question_exists instead of _mdns_question_exists, which might have different implementation behavior. The related function append_host_questions_for_services now uses mdns_priv_get_global_hostname() instead of directly accessing _mdns_server->hostname, which could introduce different behavior if the global hostname retrieval differs from the direct struct access. | +| [_mdns_append_host_questions_for_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2094) | [append_host_questions_for_services](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L414) | The refactored function introduces a subtle bug in the append_host_question helper function. The original implementation did not check for duplicate questions before adding them to the queue, but the refactored version now includes a question_exists check that frees the allocated memory if a duplicate is found, yet still returns true. This means:
1. Memory is allocated but potentially leaked if duplicates exist (malloc followed by free without being added to queue)
2. The function returns true even when the question wasn't actually added to the questions list
3. This could lead to inconsistent state where callers think questions were added but they weren't
Additionally, the change from direct _mdns_server->hostname access to mdns_priv_get_global_hostname() and from _str_null_or_empty to mdns_utils_str_null_or_empty could introduce behavioral differences if these functions don't have identical implementations. | +| [_mdns_create_probe_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2112) | [mdns_priv_create_probe_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L887) | The function `question_exists` (without the `mdns_priv_` prefix) appears to be a different implementation than the original `_mdns_question_exists`. This could introduce subtle bugs if the new function has different behavior for duplicate question detection, potentially causing incorrect packet construction or memory leaks. Additionally, the constant `MDNS_UTILS_DEFAULT_DOMAIN` may have a different value than the original `MDNS_DEFAULT_DOMAIN`, which could affect DNS query formatting and service discovery. | +| [_mdns_create_announce_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2166) | [mdns_priv_create_announce_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L1256) | The refactored function uses different underlying helper functions (mdns_priv_alloc_packet, mdns_priv_create_answer, mdns_priv_free_tx_packet, mdns_priv_append_host_list_in_services) compared to the original (_mdns_alloc_packet_default, _mdns_alloc_answer, _mdns_free_tx_packet, _mdns_append_host_list_in_services). While the function signatures appear identical, the inability to find these renamed functions in the search results suggests they may have different implementations or behaviors that could introduce subtle bugs, particularly around memory management, error handling, or protocol compliance. The refactoring assumes functional equivalence of these underlying components without verification. | +| [_mdns_create_announce_from_probe](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2196) | [mdns_priv_create_announce_from_probe](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L1417) | The function `mdns_get_host_item` from the original code has been replaced with `get_host_item` without the `mdns_` prefix. This could indicate either an incomplete refactoring where the prefix was accidentally omitted, or a different function with potentially different behavior. If `get_host_item` is not the correct replacement or has different memory management semantics, it could lead to null pointer dereferences or incorrect host item lookups. | +| [_mdns_pcb_send_bye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2236) | [mdns_priv_send_bye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1505) | The refactored function mdns_priv_pcb_send_bye_service introduces a new global hostname check using mdns_utils_str_null_or_empty(mdns_priv_get_global_hostname()) that wasn't present in the original code. This could prevent goodbye packets from being sent when the global hostname is empty, potentially leaving stale service records in the network. The original implementation would send goodbye packets regardless of hostname state, which may be the intended behavior for proper service cleanup. | +| [_mdns_init_pcb_probe_new_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2260) | [init_probe_new_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L339) | The refactoring introduces a potential stack overflow vulnerability in mdns_priv_init_pcb_probe where a Variable Length Array (VLA) `new_probe_services[len]` is allocated on the stack without bounds checking. If `len` is large, this could cause stack exhaustion. Additionally, the global hostname check in mdns_priv_init_pcb_probe may prematurely set PCB state to RUNNING without probing services, potentially skipping necessary service discovery steps. | +| [_mdns_init_pcb_probe_new_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2260) | [mdns_priv_init_pcb_probe](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_pcb.c#L2478) | The refactoring introduces a potential stack overflow vulnerability in mdns_priv_init_pcb_probe where a Variable Length Array (VLA) `new_probe_services[len]` is allocated on the stack without bounds checking. If `len` is large, this could cause stack exhaustion. Additionally, the global hostname check in mdns_priv_init_pcb_probe may prematurely set PCB state to RUNNING without probing services, potentially skipping necessary service discovery steps. | +| [_mdns_init_pcb_probe](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2316) | [mdns_priv_init_pcb_probe](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_pcb.c#L2478) | The refactored code introduces a potential stack overflow vulnerability by using a Variable Length Array (VLA) `new_probe_services[len]` without bounds checking. In the original code, the VLA usage was the same, but the refactoring context doesn't show if `len` is properly validated elsewhere. Large `len` values could exhaust the stack, especially in embedded environments. Additionally, the refactored `init_probe_new_service` function has complex memory management logic that could leak memory if `mdns_priv_create_probe_packet` returns NULL, though this appears consistent with the original design. | +| [_mdns_restart_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2356) | [restart_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L127) | The refactored function uses `mdns_priv_get_services()` instead of direct access to `_mdns_server->services`, which changes the data access pattern. While this might be intentional encapsulation, it could introduce subtle bugs if:
1. `mdns_priv_get_services()` returns a different service list than the original global variable
2. The function is called multiple times (line 132 and 140) and the service list changes between calls
3. The service counting logic becomes inconsistent if services are added/removed during execution

Additionally, the function name changed from `_mdns_restart_pcb` to `restart_pcb`, which may affect visibility and linkage if this was previously a static function. | +| [_mdns_send_bye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2382) | [mdns_priv_pcb_send_bye_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L429) | The refactored function uses a different PCB state access pattern: original code accessed `_mdns_server->interfaces[i].pcbs[j].state` while refactored code uses `s_pcbs[i][j].state`. This structural change could introduce subtle bugs if the PCB state management or initialization logic differs between the two implementations, potentially leading to incorrect state checks or accessing uninitialized memory. | +| [_mdns_send_bye_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2401) | [mdns_priv_send_bye_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L1681) | The refactored function changed from static to non-static visibility, which could break encapsulation and allow unintended external access. Additionally, the static buffer 'pkt' remains in the refactored code, creating potential race conditions in multi-threaded environments since multiple instances could overwrite the same buffer. | +| [_mdns_announce_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2451) | [mdns_priv_pcb_announce](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_pcb.c#L861) | The refactored function accesses PCB state through a different global structure (s_pcbs[][] instead of _mdns_server->interfaces[].pcbs[]), which could lead to state inconsistencies if the initialization or synchronization of these structures differs. Additionally, the hostname check now uses mdns_priv_get_global_hostname() instead of direct access to _mdns_server->hostname, which may return different values if the global hostname management was modified during refactoring. | +| [_mdns_probe_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2494) | [mdns_priv_probe_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_pcb.c#L2342) | The refactored function accesses PCB data through `s_pcbs[i][j]` instead of `_mdns_server->interfaces[i].pcbs[j]`, which represents a significant architectural change in data structure organization. This could introduce subtle bugs if:

1. The `s_pcbs` array is not properly initialized or synchronized with the original server state
2. The memory layout or indexing differs between the two data structures
3. There are race conditions in accessing the global `s_pcbs` vs the server-managed PCB structures

Additionally, the helper function `mdns_priv_init_pcb_probe` (shown in Result 3) has been substantially refactored with new logic for duplicate service detection and different probing state handling, which may change the probing behavior in edge cases where services are already being probed. | +| [_mdns_announce_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2516) | [announce_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L150) | The refactored function `announce_all_pcbs` calls `mdns_priv_pcb_announce` which introduces state-dependent behavior that wasn't present in the original implementation. In the PCB_RUNNING state, there's a global hostname check that can cause the function to return early without any announcement if the hostname is null or empty. This could lead to silent failures where services aren't announced when expected, particularly during system initialization or hostname changes. The original implementation would always attempt to announce regardless of PCB state or hostname status. | +| [_mdns_send_final_bye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2529) | [send_final_bye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L163) | The refactored function send_final_bye calls mdns_priv_pcb_send_bye_service which adds a hostname validation check that wasn't present in the original function. This could prevent bye packets from being sent when the global hostname is empty/null, potentially leaving stale service records in the network. Additionally, the new implementation iterates through multiple interfaces/protocols and checks PCB state, which may change the multicast behavior compared to the original single-call approach. | +| [_mdns_send_bye_all_pcbs_no_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2554) | [send_bye_all_pcbs_no_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L188) | The refactored function `send_bye_all_pcbs_no_instance` now calls `mdns_priv_pcb_send_bye_service` which includes an additional global hostname check that wasn't present in the original implementation. This could potentially prevent bye packets from being sent when the global hostname is empty, even though services without instances should still be able to send bye packets independently of the hostname state. This represents a behavioral change that might affect service shutdown behavior. | +| [_mdns_restart_all_pcbs_no_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2582) | [mdns_priv_restart_all_pcbs_no_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L213) | | +| [_mdns_restart_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2610) | [mdns_priv_restart_all_pcbs](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L238) | The refactored function changed from static to non-static visibility, which could lead to unintended external access and linkage issues. Additionally, the global variable changed from _mdns_server to s_server, and function calls changed from _mdns_clear_tx_queue_head() to mdns_priv_clear_tx_queue() and _mdns_probe_all_pcbs() to mdns_priv_probe_all_pcbs(). Without verification that these are functionally equivalent, there's risk of behavioral changes or memory management issues. | +| [_mdns_allocate_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2643) | [allocate_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L269) | The refactored function `allocate_txt` has a subtle memory leak bug. In the original function, when `mdns_mem_strdup(txt[i].value)` fails, it properly cleans up by freeing both `new_item->key` and `new_item` before breaking. However, in the refactored version, when `mdns_mem_strdup(txt[i].value)` fails, it only frees `(char *)new_item->key` and `new_item`, but any previously allocated items in the linked list (`new_txt`) remain allocated and are not cleaned up. This creates a memory leak where partial allocations are not properly rolled back when allocation failures occur mid-loop. | +| [_mdns_free_linked_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2682) | [free_linked_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L308) | The refactored function free_linked_txt casts t->key and t->value to (char *) before freeing, which assumes these fields were allocated as char arrays. While this matches the original behavior, this pattern relies on implementation details of the memory allocator and could be problematic if the allocation types change. The casting suggests potential type safety issues in the memory management design. | +| [_mdns_create_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2706) | [create_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L332) | The refactored code introduces a potential resource leak in the error handling path of mdns_service_add_for_host. When create_service succeeds but the subsequent mdns_mem_malloc for the mdns_srv_item_t fails, the function calls free_service(s) but does not remove the service from the global s_server->services list. Additionally, the locking mechanism (mdns_priv_service_lock/unlock) wraps the entire function but the original _mdns_create_service was likely called within an already locked context, potentially creating deadlock risks or inconsistent locking behavior. | +| [_mdns_dealloc_scheduled_service_answers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2762) | [dealloc_scheduled_service_answers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1742) | The refactored function 'dealloc_scheduled_service_answers' has identical logic to the original but there's a potential issue with the second while loop. In the original function, when d->next is NULL but d itself matches the service, it won't be removed in the second loop. The refactored version preserves this same behavior, which might be intentional but could lead to leftover answers if the first element after the initial removal matches the service but has no next pointer. This appears to be a pre-existing issue carried over from the original implementation. | +| [_mdns_remove_scheduled_service_packets](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2787) | [mdns_priv_remove_scheduled_service_packets](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mock_mdns_send.c#L2171) | The refactored function passes the condition `had_answers && q->answers == NULL` to `mdns_priv_pcb_check_probing_services` for the announcing state check, but this condition is evaluated before the deallocation calls. In the original code, this condition was checked after deallocation, which could lead to different behavior if the deallocation functions modify the answers list during execution. This timing difference might cause incorrect PCB state transitions during service removal. | +| [_mdns_free_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2868) | [free_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L385) | The refactored function `free_subtype` appears to be a direct replacement for `_mdns_free_subtype` with identical logic, but there's a potential memory safety issue: the function casts `subtype->subtype` to `(char *)` before freeing, which suggests the original data might have been declared as `const char *`. This cast could mask const-correctness violations in the codebase. If the subtype string was originally allocated as read-only memory or through string literals, this could lead to undefined behavior when attempting to free it. | +| [_mdns_free_service_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2878) | [free_service_subtype](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L395) | | +| [_mdns_free_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2889) | [free_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L406) | The refactored function `free_service` calls `free_service_subtype(service)` which sets `service->subtype = NULL`, but this happens after the service structure has already been freed with `mdns_mem_free(service)`. This creates a use-after-free bug where we're writing to memory that has already been deallocated. In the original code, `_mdns_free_service_subtype` was called before freeing the service structure, which was correct. | +| [_mdns_check_srv_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2917) | [check_srv_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L438) | The refactored function hardcodes domain length as 5 bytes when copying MDNS_UTILS_DEFAULT_DOMAIN, but there's no verification that MDNS_UTILS_DEFAULT_DOMAIN is actually exactly 5 bytes. If the domain constant differs in length, this could cause buffer overruns or incorrect collision detection. Additionally, the function uses Variable Length Arrays (VLAs) which could cause stack overflow with very long hostnames, and the uint16_t index variables may overflow if the total data length exceeds 65535 bytes. | +| [_mdns_check_txt_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L2974) | [check_txt_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L393) | The refactored function uses mdns_priv_append_one_txt_record_entry instead of append_one_txt_record_entry. While both functions appear to have identical signatures and similar logic, the refactored version is no longer inline and has been moved to a separate compilation unit (mdns_send.c). This could potentially impact performance in the collision detection hot path, and there's a risk that the implementation might differ slightly in edge cases (like handling of empty values or special characters) which could affect the byte-for-byte comparison critical for proper mDNS collision resolution. | +| [mdns_pcb_deinit_local](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3016) | [deinit_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L107) | The NULL check on `pcb` in the refactored function is problematic. In the original code, `_pcb` was obtained via `&_mdns_server->interfaces[tcpip_if].pcbs[ip_proto]`, which would always return a valid address (not NULL) if the array structure exists. In the refactored code, `pcb = &s_pcbs[tcpip_if][ip_proto]` should also always return a valid address. This NULL check could mask actual initialization issues with the `s_pcbs` array and might prevent proper error propagation from `mdns_priv_if_deinit`. The check should either be removed or replaced with a proper array bounds validation. | +| [_mdns_dup_interface](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3035) | [mdns_priv_pcb_set_duplicate](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L186) | The refactored function uses a different data structure (s_pcbs[tcpip_if][i] vs _mdns_server->interfaces[tcpip_if].pcbs[i]) and the state transition to PCB_DUP is performed unconditionally without verifying if the PCB was previously in a valid state. This could lead to inconsistent state management if the PCB was already in PCB_OFF or another terminal state. Additionally, the function is no longer static, which changes its visibility and could affect encapsulation. | +| [_mdns_check_a_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3059) | [check_a_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L322) | The refactored function uses different helper functions (mdns_priv_get_esp_netif, mdns_priv_netif_get_other_interface, mdns_priv_pcb_set_duplicate) compared to the original (_mdns_get_esp_netif, _mdns_get_other_if, _mdns_dup_interface). While the function logic appears identical, there's a risk that these renamed helper functions might have different implementations or error handling behaviors that could affect the collision detection logic. Specifically, if mdns_priv_netif_get_other_interface returns different values than _mdns_get_other_if for edge cases, or if mdns_priv_pcb_set_duplicate has different side effects than _mdns_dup_interface, this could introduce subtle behavioral changes in the collision resolution protocol. | +| [_mdns_check_aaaa_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3095) | [check_aaaa_collision](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L358) | The refactored function uses renamed helper functions and constants whose implementations cannot be verified from the provided context. Specifically:
1. `mdns_priv_netif_get_other_interface` replaces `_mdns_get_other_if` - if this function has different behavior in edge cases (like when interfaces are disabled), it could affect collision detection logic
2. `mdns_priv_pcb_set_duplicate` replaces `_mdns_dup_interface` - if this function performs additional operations or misses some operations from the original, it could impact interface state management
3. `MDNS_UTILS_SIZEOF_IP6_ADDR` must exactly match the original `_MDNS_SIZEOF_IP6_ADDR` (typically 16 bytes for IPv6 addresses), otherwise memcmp comparisons will be incorrect

The functional logic appears identical, but without verifying the implementations of these renamed components, there's risk of subtle behavioral changes. | +| [_hostname_is_ours](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3127) | [mdns_utils_hostname_is_ours](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L105) | The refactored function mdns_utils_hostname_is_ours accesses global data through mdns_priv_get_global_hostname() and mdns_priv_get_hosts() without any locking mechanism, while the original function was called within MDNS_SERVICE_LOCK/UNLOCK sections. This could lead to race conditions if the global hostname or host list is modified concurrently. The wrapper function mdns_hostname_exists does provide locking, but direct calls to mdns_utils_hostname_is_ours without locking could access inconsistent state. | +| [_mdns_delegate_hostname_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3150) | [mdns_priv_delegate_hostname_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L426) | The refactored function uses s_server->host_list instead of the global _mdns_host_list, but there's no null check for s_server. If s_server is NULL, this will cause a segmentation fault when accessing s_server->host_list. The original function directly used the global variable _mdns_host_list which would always be accessible (though potentially NULL for an empty list). This introduces a new failure mode that wasn't present in the original implementation. | +| [free_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3168) | [mdns_utils_free_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L239) | | +| [_mdns_delegate_hostname_set_address](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3178) | [delegate_hostname_set_address](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L444) | The refactored function delegate_hostname_set_address uses mdns_utils_free_address_list instead of the original free_address_list, but the memory ownership semantics may have changed. In the original code, when ACTION_DELEGATE_HOSTNAME_SET_ADDR action fails, the caller explicitly frees the address_list, suggesting the function takes ownership. The refactored version appears to directly assign the address_list to the host item without clear documentation of ownership transfer, which could lead to double-free or memory leaks if the memory management contract has changed. | +| [copy_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3198) | [mdns_utils_copy_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L214) | The refactored function introduces a HOOK_MALLOC_FAILED macro call before freeing the partially allocated list. If this hook has side effects (like logging, delays, or state changes), it could potentially affect error handling behavior or timing in memory-constrained scenarios. Additionally, the function now depends on mdns_utils_free_address_list being available and functionally equivalent to the original free_address_list. | +| [free_delegated_hostnames](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3222) | [free_delegated_hostnames](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L49) | The refactored function now accesses s_server->host_list instead of the global _mdns_host_list, but there is no NULL check for s_server. If free_delegated_hostnames is called when s_server is NULL (e.g., during shutdown or error conditions), this will cause a segmentation fault. Additionally, the function mdns_priv_responder_free calls free_delegated_hostnames after checking if s_server is NULL, suggesting that s_server could be NULL in some scenarios. | +| [_mdns_delegate_hostname_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3235) | [delegate_hostname_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L464) | The refactored function uses different global variables and function names without clear verification that they maintain identical behavior. Specifically: s_server vs _mdns_server, mdns_priv_pcb_send_bye_service vs _mdns_send_bye, mdns_priv_remove_scheduled_service_packets vs _mdns_remove_scheduled_service_packets, and free_service vs _mdns_free_service. The host list management also changed from _mdns_host_list to s_server->host_list. These changes could introduce subtle bugs if the underlying implementations differ, especially around memory management, protocol behavior, or state consistency. | +| [_mdns_name_is_discovery](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3282) | [is_discovery](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L28) | | +| [_mdns_name_is_selfhosted](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3296) | [is_name_selfhosted](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L495) | The refactored function `is_name_selfhosted` introduces a potential null pointer dereference. The original function accessed `_mdns_server->hostname` directly, while the refactored version calls `mdns_priv_get_global_hostname()` without checking if the returned pointer is valid before passing it to `mdns_utils_str_null_or_empty()`. If `mdns_priv_get_global_hostname()` can return NULL, this could lead to undefined behavior. | +| [_mdns_name_is_ours](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3319) | [is_ours](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L60) | The refactored function uses mdns_utils_hostname_is_ours which now checks against multiple hosts from mdns_priv_get_hosts() in addition to the global hostname. This expands the definition of "ours" to include additional hosts, which may break assumptions in the original code that only the global hostname was considered. Additionally, the change from _mdns_server->hostname to mdns_priv_get_global_hostname() could have different semantics if the underlying data structure or initialization timing has changed. | +| [_mdns_read_u16](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3385) | [mdns_utils_read_u16](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.h#L18) | | +| [_mdns_read_u32](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3397) | [mdns_utils_read_u32](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.h#L30) | | +| [_mdns_parse_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3411) | [mdns_utils_parse_fqdn](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_utils.c#L71) | The refactoring changed the function scope from static to external (non-static), which could lead to unintended access from other compilation units and potential namespace pollution. Additionally, the static buffer usage in both mdns_utils_parse_fqdn and mdns_utils_read_fqdn creates reentrancy issues in multi-threaded environments or during recursive calls. | +| [_mdns_question_matches](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3448) | [question_matches](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L518) | The refactored function uses MDNS_UTILS_DEFAULT_DOMAIN instead of MDNS_DEFAULT_DOMAIN, which could introduce subtle bugs if these constants have different values or if MDNS_UTILS_DEFAULT_DOMAIN is not properly defined in the refactored codebase. Additionally, the function _mdns_get_service_instance_name was renamed to mdns_utils_get_service_instance_name, and any behavioral differences between these functions could affect the matching logic for SRV and TXT record types. | +| [_mdns_remove_parsed_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3483) | [remove_parsed_question](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L553) | | +| [_mdns_txt_items_count_get](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3515) | [get_txt_items_count](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L181) | | +| [_mdns_txt_item_name_get_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3542) | [get_txt_item_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L208) | The refactored code in result_txt_create removed the bounds check "txt_num >= num_items" that was present in the original code. In the original implementation, the condition was "if (name_len < 0 || txt_num >= num_items)" which provided protection against buffer overruns when the actual number of parsed TXT items exceeds the pre-allocated array size. The refactored version only checks "if (name_len < 0)" which could lead to writing beyond the allocated txt array bounds if get_txt_items_count returns an incorrect value or if the TXT data contains more valid items than expected. | +| [_mdns_result_txt_create](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3558) | [result_txt_create](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L224) | The refactored function removed the `txt_num >= num_items` check from the invalid item condition, relying solely on the while loop condition `txt_num < num_items`. This creates a potential buffer overrun vulnerability if there are many invalid TXT items (where name_len < 0) followed by valid ones. The original code had double protection by checking both conditions, but the refactored version only checks the array bounds in the loop condition, which could be bypassed if invalid items cause the loop to continue without incrementing txt_num properly. | +| [_mdns_strdup_check](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3654) | [strdup_check](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L122) | | +| [mdns_parse_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L3672) | [mdns_parse_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_receive.c#L587) | The refactored function has been changed from public to static scope, which could break external callers. Additionally, there are potential subtle bugs in the collision detection logic where the refactored version uses simplified PCB state checking via `mdns_priv_pcb_is_probing(packet)` instead of the original direct structure access `_mdns_server->interfaces[packet->tcpip_if].pcbs[packet->ip_protocol].probe_running`. This abstraction might not handle all edge cases identically, particularly during concurrent probe operations or interface state transitions. | +| [_mdns_enable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4362) | [mdns_priv_pcb_enable](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L172) | | +| [_mdns_disable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4376) | [mdns_priv_pcb_disable](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L153) | The refactored function changes the data structure access pattern from `_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state` to `s_pcbs[tcpip_if][ip_protocol].state`, which represents a significant architectural change. This could introduce subtle bugs related to:
1. Memory layout differences affecting state synchronization
2. Race conditions if the new s_pcbs structure has different locking mechanisms
3. Potential null pointer dereferences if s_pcbs array initialization differs from the original _mdns_server structure
4. Consistency issues with the PCB state transitions across the codebase due to the structural change

Additionally, the function renaming (`mdns_pcb_deinit_local` → `deinit_pcb`, `_mdns_get_other_if` → `mdns_priv_netif_get_other_interface`) suggests broader architectural changes that could affect error handling paths or edge case behavior. | +| [nibble_to_hex](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4393) | [nibble_to_hex](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L46) | | +| [perform_event_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4402) | [perform_event_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L55) | The refactored function maintains the same logic but introduces potential memory leaks in the reverse DNS lookup code. In the IPv4 reverse lookup, asprintf allocates memory for reverse_query_name but there's no free() call if mdns_priv_delegate_hostname_add succeeds. Similarly, in IPv6 reverse lookup, mdns_mem_malloc allocates memory but there's no corresponding free. The original code had the same issue, but this refactoring missed the opportunity to fix it. | +| [post_mdns_disable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4473) | [post_disable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L183) | | +| [post_mdns_enable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4478) | [post_enable_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L189) | | +| [post_mdns_announce_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4483) | [post_announce_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L195) | | +| [mdns_preset_if_handle_system_event](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4489) | [handle_system_event_for_preset](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L202) | The refactored function replaces manual browse iteration with mdns_priv_browse_send_all(), but without seeing the implementation of this function, we cannot verify if it maintains the same iteration behavior as the original _mdns_browse_send() calls. Additionally, the function name changes (esp_netif_from_preset_if to netif_from_preset, etc.) suggest potential semantic differences that need verification. | +| [_mdns_search_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4582) | [search_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L343) | The refactored search_free function does not check if the search parameter is NULL before dereferencing its fields. While the original function also lacked this check, the refactoring moved the function to a different context (mdns_querier.c) where the calling patterns might have changed. In C programming, defensive programming practices suggest checking for NULL pointers before dereferencing, especially for cleanup functions that might be called from multiple contexts with potentially invalid parameters. | +| [_mdns_search_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4594) | [search_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L355) | | +| [_mdns_search_finish](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4652) | [search_finish](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L59) | The refactored function changes the global variable from `_mdns_server->search_once` to `s_search_once`, but there's no evidence that the ACTION_SEARCH_END case in the action handler was updated to call the new function. This could leave dangling searches that are never properly finished when triggered through the action system, potentially causing resource leaks or synchronization issues with the semaphore. | +| [_mdns_search_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4665) | [search_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L72) | | +| [_mdns_search_finish_done](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4674) | [mdns_priv_query_done](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L181) | The refactored function uses a global variable `s_search_once` instead of accessing it through `_mdns_server->search_once`, which changes the data structure access pattern. Additionally, the new `search_finish` function performs different cleanup operations (queue detachment and semaphore signaling) compared to the original `_mdns_search_finish` function, whose implementation is not shown for verification. This could lead to inconsistent search state management or resource cleanup issues. | +| [_mdns_result_addr_create_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4690) | [mdns_priv_result_addr_create_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L653) | The function visibility changed from static to non-static, which could lead to unintended usage across the codebase. Additionally, the refactored function is now called from new contexts (result_add_ip and mdns_priv_query_result_add_ip) that weren't in the original codebase, potentially introducing different error handling requirements. While the core implementation remains identical, the architectural changes in how IP addresses are managed could affect resource cleanup patterns in case of failures. | +| [_mdns_result_update_ttl](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4707) | [mdns_priv_query_update_result_ttl](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.h#L99) | | +| [_mdns_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4715) | [result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L485) | The refactoring introduces a subtle bug in the memory management logic. In the original code, when _mdns_result_addr_create_ip fails, the function simply returns without affecting the result structure. However, in mdns_priv_query_result_add_ip, when mdns_priv_result_addr_create_ip fails during new result creation, the partially allocated mdns_result_t structure is freed, but the search->result pointer is never updated, leaving the search result list in an inconsistent state. Additionally, the hostname string duplication occurs after IP address creation, which could lead to memory leaks if the IP creation fails but the hostname was already allocated. | +| [_mdns_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4715) | [mdns_priv_query_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L514) | The refactoring introduces a subtle bug in the memory management logic. In the original code, when _mdns_result_addr_create_ip fails, the function simply returns without affecting the result structure. However, in mdns_priv_query_result_add_ip, when mdns_priv_result_addr_create_ip fails during new result creation, the partially allocated mdns_result_t structure is freed, but the search->result pointer is never updated, leaving the search result list in an inconsistent state. Additionally, the hostname string duplication occurs after IP address creation, which could lead to memory leaks if the IP creation fails but the hostname was already allocated. | +| [_mdns_search_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4744) | [mdns_priv_query_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L514) | The function 'result_add_ip' is called without the 'mdns_priv_' prefix, which may indicate either a missing namespace prefix or an undefined function. This could lead to linking errors or incorrect behavior if the intended function is not available. All other helper functions were properly prefixed with 'mdns_priv_' or 'mdns_utils_', making this inconsistency potentially problematic. | +| [_mdns_search_result_add_ptr](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4802) | [mdns_priv_query_result_add_ptr](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L614) | The refactored function mdns_priv_query_result_add_ptr appears to have a critical bug: callers in the refactored code (mdns_receive.c lines 851-852, 922-923, 1038-1040) do not check the return value for NULL, unlike the original code which explicitly checked for NULL returns (lines 4020-4023, 4132-4135). This could lead to NULL pointer dereferences if the function fails to allocate memory or encounters other errors. The function signature still returns mdns_result_t*, suggesting it should be checked, but the calling code ignores the return value. | +| [_mdns_search_result_add_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4844) | [mdns_priv_query_result_add_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L572) | | +| [_mdns_search_result_add_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4886) | [mdns_priv_query_result_add_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L435) | The refactoring splits the original unified TXT handling function into two specialized functions (query vs browse) with different signatures. The original parser call site at line 4149 now needs to determine which function to call based on context, but the additional parameters required by mdns_priv_browse_result_add_txt (instance, service, proto) may not be available in all calling contexts where the original function was used. This could lead to either incorrect function selection or missing data when calling the browse version, potentially causing incomplete TXT record processing or memory access issues. | +| [_mdns_search_find_from](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L4936) | [mdns_priv_query_find_from](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L197) | The function has been changed from static to non-static linkage, which could expose internal implementation details and create unintended dependencies. Additionally, the utility function replacements (_mdns_get_esp_netif → mdns_priv_get_esp_netif and _str_null_or_empty → mdns_utils_str_null_or_empty) need verification that they maintain identical behavior in all edge cases, particularly for NULL inputs and network interface mapping. | +| [_mdns_create_search_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5003) | [create_search_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L269) | The refactored function uses `mdns_priv_alloc_packet` instead of `_mdns_alloc_packet_default`. If `mdns_priv_alloc_packet` doesn't provide the same default packet initialization (like setting default flags, TTL values, or other packet metadata), it could result in malformed mDNS search packets that don't conform to protocol specifications. Additionally, the domain constant change from `MDNS_DEFAULT_DOMAIN` to `MDNS_UTILS_DEFAULT_DOMAIN` could affect DNS query formation if the values differ, potentially causing search queries to use the wrong domain. | +| [_mdns_search_send_pcb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5060) | [mdns_priv_query_send](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L327) | The refactored function uses `mdsn_priv_pcb_is_inited(tcpip_if, ip_protocol)` instead of the original complex condition `mdns_is_netif_ready(tcpip_if, ip_protocol) && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT`. This could introduce subtle bugs if the new function doesn't properly replicate both the network interface readiness check AND the PCB state validation. Additionally, there appears to be a potential typo in the function name "mdsn_priv_pcb_is_inited" (missing 'n' in 'mdns'). | +| [_mdns_search_send](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5076) | [search_send](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L81) | The refactored function `mdns_priv_query_send` (which replaces `_mdns_search_send_pcb`) immediately frees the packet after dispatch with `mdns_priv_free_tx_packet(packet)`, while the original implementation's packet lifecycle management is unclear. This could cause issues if packet transmission is asynchronous and the packet needs to remain valid until transmission completes. Additionally, the refactored version introduces early return on packet creation failure, which changes the error handling behavior compared to the original loop structure. | +| [_mdns_tx_handle_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5102) | [handle_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1816) | The refactoring splits the original function's logic across multiple functions, but introduces a potential bug in the PCB_PROBE_3 case. In the original code, when creating an announce packet fails, it breaks from the switch statement after scheduling a retry. However, in the refactored version, the variable 'p' is reassigned to 'a' (the new announce packet) in the success case, but this reassignment only affects the local copy in mdns_priv_pcb_schedule_tx_packet. The calling function handle_packet still holds the original 'p' pointer, which could lead to double-free or use-after-free issues if the packet lifecycle isn't properly managed across the function boundaries. | +| [_mdns_tx_handle_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5102) | [mdns_priv_pcb_schedule_tx_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_pcb.c#L211) | The refactoring splits the original function's logic across multiple functions, but introduces a potential bug in the PCB_PROBE_3 case. In the original code, when creating an announce packet fails, it breaks from the switch statement after scheduling a retry. However, in the refactored version, the variable 'p' is reassigned to 'a' (the new announce packet) in the success case, but this reassignment only affects the local copy in mdns_priv_pcb_schedule_tx_packet. The calling function handle_packet still holds the original 'p' pointer, which could lead to double-free or use-after-free issues if the packet lifecycle isn't properly managed across the function boundaries. | +| [_mdns_remap_self_service_hostname](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5159) | [mdns_priv_remap_self_service_hostname](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L508) | The function scope change from static to non-static could potentially cause linking conflicts if this function is now accessible from other compilation units. Additionally, the global variable change from _mdns_server to s_server requires verification that s_server is properly initialized and equivalent to the original _mdns_server in all execution contexts. | +| [_mdns_sync_browse_result_link_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5173) | [sync_browse_result_link_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L236) | The function name changed from `_mdns_sync_browse_result_link_free` to `sync_browse_result_link_free` (removing the leading underscore and "mdns_" prefix). While the core logic remains identical, this naming change could introduce subtle bugs if:
1. Call sites in the refactored codebase haven't been updated to use the new function name
2. The function visibility/scope changed due to the naming convention alteration
3. Other parts of the codebase rely on the specific naming pattern for internal functions

The original function was called in ACTION_BROWSE_SYNC cases in mdns.c, and if those call sites weren't properly updated to reference the renamed function, it would result in compilation errors or runtime failures due to undefined function references. | +| [_mdns_free_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5188) | [free_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L126) | The refactored free_action function delegates cleanup to external functions (mdns_priv_*_action with ACTION_CLEANUP mode) without visible implementation. This introduces a risk that the specific memory cleanup operations performed in the original function (mdns_mem_free for hostnames, _mdns_search_free for searches, free_address_list for address lists) may not be properly handled, potentially leading to memory leaks. The architectural change from direct cleanup to delegation requires verification that all cleanup functions properly handle their respective resource types. | +| [_mdns_execute_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5235) | [execute_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_engine.c#L15) | The refactoring introduces several subtle bugs:

1. **Missing ACTION_SYSTEM_EVENT handling**: In Result 2 (mdns_engine.c), the ACTION_SYSTEM_EVENT case is completely empty, while the original function called perform_event_action(). This will break system event processing.

2. **Missing semaphore operations**: The original ACTION_HOSTNAME_SET case called xSemaphoreGive(_mdns_server->action_sema) after completion, but the refactored version delegates to mdns_priv_responder_action without preserving this critical synchronization.

3. **Missing memory cleanup in ACTION_BROWSE_SYNC**: The original function explicitly called _mdns_sync_browse_result_link_free() for ACTION_BROWSE_SYNC, but the refactored version delegates this to mdns_priv_browse_action without clear evidence this cleanup is preserved.

4. **Split responsibility confusion**: The introduction of both execute_action and free_action functions suggests the original single responsibility has been split, but it's unclear when each should be called. This could lead to double-free or memory leaks if not properly coordinated.

5. **Inconsistent implementations**: Having two different execute_action functions (Result 1 vs Result 2) with different behavior for ACTION_SYSTEM_EVENT suggests either incomplete refactoring or platform-specific variations that need verification. | +| [_mdns_execute_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5235) | [free_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L126) | The refactoring introduces several subtle bugs:

1. **Missing ACTION_SYSTEM_EVENT handling**: In Result 2 (mdns_engine.c), the ACTION_SYSTEM_EVENT case is completely empty, while the original function called perform_event_action(). This will break system event processing.

2. **Missing semaphore operations**: The original ACTION_HOSTNAME_SET case called xSemaphoreGive(_mdns_server->action_sema) after completion, but the refactored version delegates to mdns_priv_responder_action without preserving this critical synchronization.

3. **Missing memory cleanup in ACTION_BROWSE_SYNC**: The original function explicitly called _mdns_sync_browse_result_link_free() for ACTION_BROWSE_SYNC, but the refactored version delegates this to mdns_priv_browse_action without clear evidence this cleanup is preserved.

4. **Split responsibility confusion**: The introduction of both execute_action and free_action functions suggests the original single responsibility has been split, but it's unclear when each should be called. This could lead to double-free or memory leaks if not properly coordinated.

5. **Inconsistent implementations**: Having two different execute_action functions (Result 1 vs Result 2) with different behavior for ACTION_SYSTEM_EVENT suggests either incomplete refactoring or platform-specific variations that need verification. | +| [_mdns_send_search_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5322) | [send_search_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L413) | The refactored function replaces direct queue access `xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0)` with a wrapper function `mdns_priv_queue_action(action)`. Without seeing the implementation of `mdns_priv_queue_action`, we cannot verify if it maintains the same non-blocking behavior (0 timeout) and return value semantics. If `mdns_priv_queue_action` uses blocking behavior or has different error handling, it could introduce timing issues or resource management problems in the MDNS search functionality. | +| [_mdns_scheduler_run](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5348) | [mdns_priv_send_packets](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_send.c#L1703) | The refactored function uses `mdns_priv_queue_action(action)` instead of directly calling `xQueueSend`. While this appears to be a wrapper function, there's a potential subtle bug: if `mdns_priv_queue_action` has different timeout behavior than the original `(TickType_t)0` (non-blocking), it could introduce blocking behavior that wasn't present in the original implementation. Additionally, the error handling logic assumes `mdns_priv_queue_action` returns false on failure (matching the original `xQueueSend != pdPASS`), but this needs verification to ensure the same cleanup behavior is maintained. | +| [_mdns_search_run](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5385) | [mdns_priv_query_start_stop](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L133) | The refactored function mdns_priv_query_start_stop introduces a subtle bug in error handling. In the original code, when _mdns_send_search_action(ACTION_SEARCH_END, s) fails, the search state is restored to SEARCH_RUNNING, allowing retry in the next iteration. However, in the refactored version, when send_search_action(ACTION_SEARCH_END, s) fails, the state is also restored to SEARCH_RUNNING, but the timeout condition (now > (s->started_at + s->timeout)) will remain true in subsequent iterations, causing the search to immediately try to end again in an infinite loop until the send succeeds. This could lead to excessive retries and potential resource exhaustion. | +| [_mdns_service_task](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5417) | [service_task](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L200) | The refactored function replaces the direct pointer check `_mdns_server && _mdns_server->action_queue` with a function call `mdns_priv_is_server_init()` and a global variable `s_action_queue`. This introduces potential issues:
1. The function call `mdns_priv_is_server_init()` may have different timing/side effects compared to the simple pointer check
2. The global `s_action_queue` is not protected by the same locking mechanism as the original server structure
3. There's a potential race condition if `s_action_queue` is modified while the service task is running
4. The separation of server initialization check from queue access could lead to inconsistent state detection | +| [_mdns_timer_cb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5439) | [timer_cb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L222) | The refactoring changes the core functionality of the timer callback from scheduler/search operations to packet sending/query operations. While the structural mapping is clear (timer_cb replaces _mdns_timer_cb and start_timer replaces _mdns_start_timer), the functional equivalence cannot be verified without understanding what mdns_priv_send_packets and mdns_priv_query_start_stop do compared to the original _mdns_scheduler_run and _mdns_search_run. This could introduce subtle bugs in MDNS protocol timing, packet scheduling, or service discovery behavior. | +| [_mdns_timer_cb](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5439) | [start_timer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L228) | The refactoring changes the core functionality of the timer callback from scheduler/search operations to packet sending/query operations. While the structural mapping is clear (timer_cb replaces _mdns_timer_cb and start_timer replaces _mdns_start_timer), the functional equivalence cannot be verified without understanding what mdns_priv_send_packets and mdns_priv_query_start_stop do compared to the original _mdns_scheduler_run and _mdns_search_run. This could introduce subtle bugs in MDNS protocol timing, packet scheduling, or service discovery behavior. | +| [_mdns_start_timer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5445) | [start_timer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L228) | The refactored start_timer function uses a global variable s_timer_handle instead of the original _mdns_server->timer_handle, but there's no evidence that s_timer_handle is properly initialized to NULL. This could lead to double-creation of timers if the function is called multiple times, as the original code would have stored the handle in a server structure that gets reinitialized. Additionally, the callback function was renamed from _mdns_timer_cb to timer_cb without verification that the implementation remains equivalent. | +| [_mdns_stop_timer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5460) | [stop_timer](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L243) | | +| [_mdns_task_create_with_caps](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5473) | [create_task_with_caps](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L256) | The refactored function introduces a new error handling path with 'alloc_failed' label that calls HOOK_MALLOC_FAILED before proceeding to the 'err' label. This changes the control flow and could potentially cause issues if HOOK_MALLOC_FAILED has side effects or if the order of cleanup operations matters. In the original function, memory allocation failure went directly to the cleanup code, but now there's an intermediate step that might interfere with proper resource management. | +| [_mdns_service_task_start](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5498) | [service_task_start](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L283) | | +| [_mdns_service_task_stop](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5537) | [service_task_stop](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L320) | The refactored function maintains the same potential resource management issue as the original: if xQueueSend fails, the semaphore gets deleted immediately while the task might still be running and accessing it. However, there's an additional subtle change - the refactored version uses different timer functions (esp_timer_stop/delete vs original _mdns_stop_timer) which could have different error handling behavior. The original _mdns_stop_timer might have handled timer state differently than the new stop_timer function. | +| [mdns_post_custom_action_tcpip_if](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5558) | [post_custom_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L159) | The refactored function inverts the queue operation logic: original uses `xQueueSend(...) != pdPASS` for failure case, while refactored uses `!mdns_priv_queue_action(action)` for failure case. This suggests `mdns_priv_queue_action` returns true on success, but without seeing its implementation, we cannot verify if the queue behavior is equivalent. Additionally, the server initialization check changed from direct `!_mdns_server` to `!mdns_priv_is_server_init()` which may have different semantics. | +| [set_default_duplicated_interfaces](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5579) | [set_default_duplicated_interfaces](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L285) | The function appears to be identical in both implementations (100% similarity), but there's a potential architectural concern. The refactored codebase contains mdns_priv_pcb_set_duplicate which handles PCB-level duplication with additional operations (clearing TX queues, deinitializing PCBs, setting PCB_DUP state), while the original netif-level duplication logic remains unchanged. This creates a potential inconsistency where netif-level duplication is established but PCB-level duplication handling may not be properly synchronized. The bidirectional relationship between interfaces is managed differently in these two approaches, which could lead to race conditions or inconsistent state between netif and PCB layers. | +| [unregister_predefined_handlers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5597) | [mdns_priv_netif_unregister_predefined_handlers](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L303) | The refactored function uses a different event handler function name: 'handle_system_event_for_preset' instead of 'mdns_preset_if_handle_system_event'. While the conditional compilation logic is identical, this change could introduce subtle bugs if:
1. The new handler function has different behavior or implementation
2. The new handler function has a different signature that's not compatible with the event system
3. The original handler function 'mdns_preset_if_handle_system_event' is still registered elsewhere and not properly cleaned up
4. The new handler function doesn't exist or has different error handling

The search shows 'mdns_preset_if_handle_system_event' exists in the original codebase with a specific implementation, but no matches were found for 'handle_system_event_for_preset' in the refactored codebase, suggesting it might be missing or renamed differently. | +| [mdns_netif_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5614) | [mdns_netif_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L384) | The refactored post_custom_action function returns ESP_OK even when mdns_priv_queue_action fails (returns false), which occurs when memory allocation for the action fails. This masks memory allocation failures and changes the error behavior from the original implementation. The original mdns_post_custom_action_tcpip_if likely returned ESP_ERR_NO_MEM in this scenario, but the refactored version returns success, potentially hiding critical resource constraints. | +| [mdns_register_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5619) | [mdns_register_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L389) | The refactored mdns_unregister_netif function in Result 2 has a critical bug where it calls mdns_priv_service_lock() twice at the end instead of unlocking. This will cause a deadlock situation where the service lock is never released, blocking all subsequent mDNS operations that require the lock. The correct pattern should be mdns_priv_service_unlock() before returning. | +| [mdns_unregister_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5645) | [mdns_unregister_netif](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_netif.c#L415) | The refactored mdns_unregister_netif function has a critical bug where it calls mdns_priv_service_lock() twice instead of calling mdns_priv_service_unlock() to release the lock. This will cause a deadlock situation as the service lock is never released, preventing other threads from accessing the shared resource and potentially hanging the system. | +| [mdns_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5665) | [mdns_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L351) | The refactored mdns_init function has a subtle bug in error handling: when mdns_priv_netif_init() fails, the code jumps to free_queue label which only deletes the action queue, but does not delete the semaphore created in mdns_priv_responder_init(). In the original code, both the queue and semaphore were properly cleaned up in the error path. This could lead to a resource leak of the semaphore. | +| [mdns_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5763) | [mdns_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_service.c#L391) | The refactored mdns_free function introduces a subtle bug in the cleanup sequence. In the original code, free_delegated_hostnames() was called before mdns_mem_free((char *)_mdns_server->hostname) and mdns_mem_free((char *)_mdns_server->instance). However, in the refactored version, free_delegated_hostnames() is called inside mdns_priv_responder_free() AFTER the hostname and instance have already been freed. This could lead to accessing freed memory if free_delegated_hostnames() references the server's hostname or instance fields. | +| [mdns_hostname_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5816) | [mdns_hostname_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L591) | The refactored function replaces direct xQueueSend with mdns_priv_queue_action, but there's a potential synchronization bug. The original code used xQueueSend with 0 timeout (non-blocking) and returned ESP_ERR_NO_MEM on queue full. The new mdns_priv_queue_action function's implementation is unknown - if it uses a different timeout or blocking behavior, it could cause deadlocks or different memory error handling. Additionally, the semaphore take operation (xSemaphoreTake) remains but the queue mechanism changed, which could lead to inconsistent state if the new queue function doesn't properly coordinate with the semaphore signaling. | +| [mdns_hostname_get](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5846) | [mdns_hostname_get](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L621) | | +| [mdns_delegate_hostname_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5864) | [mdns_delegate_hostname_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L639) | The refactored function uses mdns_priv_queue_action() instead of xQueueSend() with (TickType_t)0 timeout. If mdns_priv_queue_action() has different queue semantics (e.g., different timeout behavior or error handling), it could introduce subtle bugs in memory management or action queuing reliability. Additionally, the replacement of copy_address_list() with mdns_utils_copy_address_list() requires verification that the memory allocation and cleanup behavior remains identical, especially in error scenarios. | +| [mdns_delegate_hostname_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5895) | [mdns_delegate_hostname_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L670) | The refactored function shows inconsistent synchronization behavior compared to similar functions in the same codebase. While mdns_hostname_set and mdns_delegate_hostname_add both call xSemaphoreTake(s_server->action_sema, portMAX_DELAY) after queueing their actions, mdns_delegate_hostname_remove does not. This inconsistency could lead to race conditions where callers of mdns_delegate_hostname_remove assume the operation is complete when it may still be pending in the action queue. The original codebase also lacked this synchronization, but the refactoring introduced this pattern inconsistently across similar functions. | +| [mdns_delegate_hostname_set_address](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5924) | [mdns_delegate_hostname_set_address](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L699) | The refactored function removed the semaphore synchronization (xSemaphoreTake) that was present in similar functions (mdns_delegate_hostname_add and mdns_hostname_set), while the queue mechanism changed from direct xQueueSend to mdns_priv_queue_action. This creates inconsistent synchronization behavior across similar API functions and could lead to race conditions if the action handler expects synchronous completion. The original implementation used immediate queue sending (timeout 0) without waiting, but the refactored similar functions now wait for semaphore, suggesting this function may have missed a required synchronization update. | +| [mdns_hostname_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5954) | [mdns_hostname_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L729) | | +| [mdns_instance_name_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5963) | [mdns_instance_name_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L738) | The refactored function replaces direct xQueueSend with mdns_priv_queue_action, but the timeout behavior may have changed. The original used (TickType_t)0 timeout (non-blocking), while the helper function's timeout semantics are unclear. Additionally, the refactored version lacks the explicit semaphore synchronization present in mdns_hostname_set (xSemaphoreTake), potentially creating inconsistent behavior between similar operations. | +| [mdns_service_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L5996) | [mdns_service_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L767) | The refactored function has a critical bug in the linked list insertion logic. The original code correctly sets up the linked list by assigning the new item to the head of the services list. However, the refactored code has a logical error: it sets `item->next = NULL` and then immediately overwrites it with `item->next = s_server->services`, which is correct, but then assigns `s_server->services = item`. This appears correct at first glance, but there's a subtle issue - the original code properly maintains the linked list structure while the refactored version might have different behavior if the helper functions (like `mdns_utils_get_service_item_instance` and `create_service`) have different implementations than their original counterparts. Additionally, the memory cleanup function changed from `_mdns_free_service` to `free_service` - if these have different implementations, it could lead to memory leaks or corruption in error paths. | +| [mdns_service_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6038) | [mdns_service_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L809) | The refactored mdns_service_add function appears functionally equivalent to the original, but there are potential issues in its dependency chain. The function itself only changed the global variable name from _mdns_server to s_server, but the mdns_service_add_for_host function it calls has undergone significant refactoring with new locking mechanisms, service limit checks, and different error handling patterns. This could introduce subtle bugs like:
1. Deadlocks if the new mdns_priv_service_lock/unlock functions are not properly balanced
2. Different service validation behavior through mdns_utils_get_service_item_instance
3. Changed memory allocation patterns with create_service and HOOK_MALLOC_FAILED
4. Potential race conditions if the locking scope differs from the original implementation | +| [mdns_service_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6047) | [mdns_service_exists](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L818) | | +| [mdns_service_exists_with_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6056) | [mdns_service_exists_with_instance](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L827) | | +| [_copy_mdns_txt_items](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6066) | [copy_txt_items](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L837) | The error handling cleanup loop condition `y < ret_index + 1` in both original and refactored code appears to have a potential off-by-one error. When `ret_index` represents the number of successfully allocated items, accessing `ret[ret_index]` (which would be the `ret_index + 1`-th element) could read/write beyond the allocated memory bounds if an allocation failure occurs early in the second loop. The condition should likely be `y < ret_index` to only clean up the items that were actually allocated. | +| [_copy_delegated_host_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6119) | [copy_delegated_host_address_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L890) | | +| [_mdns_lookup_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6131) | [lookup_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_console.c#L1139) | | +| [mdns_service_port_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6211) | [mdns_service_port_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L982) | | +| [mdns_service_port_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6229) | [mdns_service_port_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1000) | | +| [mdns_service_txt_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6237) | [mdns_service_txt_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1008) | The refactored function has the same subtle bug as the original: when memory allocation fails (allocate_txt returns NULL), the function returns ESP_ERR_NO_MEM directly without releasing the service lock (mdns_priv_service_unlock). This creates a potential deadlock scenario where the service lock remains held indefinitely after a memory allocation failure. | +| [mdns_service_txt_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6267) | [mdns_service_txt_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1038) | The refactored function uses s_server instead of _mdns_server for the server state check. While both appear to serve the same purpose (global MDNS server state), the refactored code introduces a potential NULL pointer dereference in mdns_priv_responder_free() at line 64 where it accesses s_server->action_sema without first checking if s_server is NULL, unlike the original code which consistently checked _mdns_server before accessing its members. | +| [mdns_service_txt_item_set_for_host_with_explicit_value_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6275) | [mdns_service_txt_item_set_for_host_with_explicit_value_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1046) | The refactored function uses renamed helper functions (mdns_utils_str_null_or_empty, mdns_utils_get_service_item_instance, announce_all_pcbs, mdns_priv_service_lock/unlock) and a renamed global variable (s_server). While the core logic appears identical, there's a risk that these renamed functions may have different implementations or behaviors compared to their original counterparts. Specifically, the memory allocation error handling in the out_of_mem label still uses HOOK_MALLOC_FAILED and mdns_mem_free, but if the renamed locking functions don't properly handle error states or if the global s_server initialization differs from _mdns_server, it could lead to subtle bugs like memory leaks, race conditions, or incorrect service discovery behavior. | +| [mdns_service_txt_item_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6331) | [mdns_service_txt_item_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1102) | The refactored function appears identical to the original, but there are two related functions (mdns_service_txt_item_set and mdns_service_txt_item_set_with_explicit_value_len) that now include state checking for s_server, while the original mdns_service_txt_item_set_for_host does not. This creates an inconsistency where some TXT item setting functions check server state while others don't. Additionally, the global variable has been renamed from _mdns_server to s_server, which could indicate broader changes in the codebase that might affect error handling consistency. | +| [mdns_service_txt_item_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6339) | [mdns_service_txt_item_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1110) | | +| [mdns_service_txt_item_set_with_explicit_value_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6348) | [mdns_service_txt_item_set_with_explicit_value_len](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1119) | | +| [mdns_service_txt_item_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6357) | [mdns_service_txt_item_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1128) | The refactored function calls `announce_all_pcbs(&s, 1, false)` but there's no evidence that `announce_all_pcbs` exists in the refactored codebase. The search for "announce_all_pcbs" returned no matches, suggesting this function may be missing or renamed differently than expected. This could lead to a compilation error or runtime failure when trying to announce the TXT item removal. | +| [mdns_service_txt_item_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6404) | [mdns_service_txt_item_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1175) | The refactored mdns_service_txt_item_remove_for_host function has several potential issues:
1. The complex ESP_GOTO_ON_FALSE conditional combines multiple error checks (server state, service/proto/key validation) into a single error path, which may mask specific error conditions that were handled separately in the original implementation
2. The memory management uses mdns_mem_free() instead of the original free() calls, which could indicate inconsistent memory management strategy across the codebase
3. The announce_all_pcbs(&s, 1, false) call appears to be new functionality that wasn't present in the original TXT removal logic, potentially causing unnecessary network announcements
4. The error handling with HOOK_MALLOC_FAILED at the end seems misplaced since no memory allocation occurs in the main logic path of this function
5. The function returns ESP_OK even when no TXT item was found to remove (the function completes without setting an error when the key isn't found) | +| [_mdns_service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6412) | [service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1183) | The refactored mdns_service_subtype_remove_for_host function has a potential memory leak in the error path. When memory allocation fails at remove_subtypes->subtype = mdns_mem_strdup(subtype), the code jumps to out_of_mem label which frees remove_subtypes but does not free the already allocated remove_subtypes structure itself. This creates a scenario where remove_subtypes is allocated but its subtype field allocation fails, leaving the remove_subtypes structure leaked. The original function had no such memory allocation, so this is a new bug introduced during refactoring. | +| [_mdns_service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6412) | [mdns_service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1212) | The refactored mdns_service_subtype_remove_for_host function has a potential memory leak in the error path. When memory allocation fails at remove_subtypes->subtype = mdns_mem_strdup(subtype), the code jumps to out_of_mem label which frees remove_subtypes but does not free the already allocated remove_subtypes structure itself. This creates a scenario where remove_subtypes is allocated but its subtype field allocation fails, leaving the remove_subtypes structure leaked. The original function had no such memory allocation, so this is a new bug introduced during refactoring. | +| [mdns_service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6441) | [mdns_service_subtype_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1212) | The refactored function uses `free_subtype(remove_subtypes)` instead of `_mdns_free_subtype(remove_subtypes)`. If `free_subtype` doesn't properly handle the case where `remove_subtypes->subtype` is NULL (due to failed mdns_mem_strdup), this could lead to memory leaks or undefined behavior. Additionally, the refactored version calls `free_subtype` even when `mdns_priv_send_bye_subtype` might have taken ownership of the subtypes, potentially causing double-free issues if the ownership semantics changed. | +| [_mdns_service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6474) | [service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1245) | The refactored function 'mdns_service_subtype_add_multiple_items_for_host' has inconsistent error handling compared to the original. In the original code, when _mdns_service_subtype_add_for_host returns ESP_ERR_INVALID_ARG (subtype already exists), the calling code continues processing other subtypes. However, in the refactored version, any error other than ESP_OK or ESP_ERR_NO_MEM causes immediate termination with goto exit, preventing processing of remaining subtypes and potentially leaving the service in an inconsistent state. Additionally, the memory cleanup rollback only occurs for ESP_ERR_NO_MEM cases, not for other error types. | +| [mdns_service_subtype_add_multiple_items_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6498) | [mdns_service_subtype_add_multiple_items_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1269) | The refactored function in Result 1 appears to be the direct replacement, but there are several subtle concerns:

1. **Missing validation for num_items > 0**: In the original code, the ESP_GOTO_ON_FALSE check includes `(num_items > 0)`, but in Result 1, this validation appears to be missing from the condition, which could allow calling the function with num_items=0 and bypass proper error handling.

2. **Function signature inconsistency**: The refactored function uses parameter name 'service' while the original uses 'service_type' in the header declaration. This could cause compilation issues if the header wasn't updated consistently.

3. **Resource management in error path**: The cleanup loop in the error path uses `service_subtype_remove_for_host` which may have different memory management behavior than the original `_mdns_service_subtype_remove_for_host`. If the memory allocation strategy changed (as seen in Result 3), the removal function might not properly handle the new allocation pattern.

4. **Potential double-free in update function**: Result 2 shows `free_subtype(goodbye_subtype)` followed by `free_service_subtype(s->service)` - if these functions have overlapping cleanup responsibilities, this could lead to double-free issues. | +| [mdns_service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6535) | [mdns_service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1306) | The refactoring introduced a subtle bug in the error handling logic. In the original implementation, when _mdns_service_subtype_add_for_host returns ESP_ERR_NO_MEM, the calling function mdns_service_subtype_add_multiple_items_for_host would continue processing other subtypes but eventually roll back all successful additions. However, in the refactored version, service_subtype_add_for_host immediately returns ESP_ERR_NO_MEM without proper cleanup of the allocated subtype_item, causing a memory leak. Additionally, the refactored version uses different error labels (err vs out_of_mem) and the memory cleanup path in service_subtype_add_for_host returns the same error code regardless of which allocation failed, losing error specificity. | +| [mdns_service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6535) | [service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1245) | The refactoring introduced a subtle bug in the error handling logic. In the original implementation, when _mdns_service_subtype_add_for_host returns ESP_ERR_NO_MEM, the calling function mdns_service_subtype_add_multiple_items_for_host would continue processing other subtypes but eventually roll back all successful additions. However, in the refactored version, service_subtype_add_for_host immediately returns ESP_ERR_NO_MEM without proper cleanup of the allocated subtype_item, causing a memory leak. Additionally, the refactored version uses different error labels (err vs out_of_mem) and the memory cleanup path in service_subtype_add_for_host returns the same error code regardless of which allocation failed, losing error specificity. | +| [mdns_service_subtype_add_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6535) | [mdns_service_subtype_add_multiple_items_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1269) | The refactoring introduced a subtle bug in the error handling logic. In the original implementation, when _mdns_service_subtype_add_for_host returns ESP_ERR_NO_MEM, the calling function mdns_service_subtype_add_multiple_items_for_host would continue processing other subtypes but eventually roll back all successful additions. However, in the refactored version, service_subtype_add_for_host immediately returns ESP_ERR_NO_MEM without proper cleanup of the allocated subtype_item, causing a memory leak. Additionally, the refactored version uses different error labels (err vs out_of_mem) and the memory cleanup path in service_subtype_add_for_host returns the same error code regardless of which allocation failed, losing error specificity. | +| [_mdns_service_find_subtype_needed_sendbye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6543) | [service_find_subtype_needed_sendbye](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1314) | | +| [mdns_service_subtype_update_multiple_items_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6593) | [mdns_service_subtype_update_multiple_items_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1364) | The refactored function calls free_service_subtype(s->service) which completely clears all subtypes from the service before adding new ones in the loop. This differs from the original behavior where _mdns_free_service_subtype might have been more selective. This could cause temporary service unavailability and potential race conditions where subtypes are briefly unavailable between the free and add operations. Additionally, the refactored version uses different locking functions (mdns_priv_service_lock/unlock vs MDNS_SERVICE_LOCK/UNLOCK) which may have different thread safety guarantees. | +| [mdns_service_instance_name_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6640) | [mdns_service_instance_name_set_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1411) | | +| [mdns_service_instance_name_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6666) | [mdns_service_instance_name_set](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1437) | The refactored function in Result 1 appears to be functionally equivalent to the original, but there's a subtle variable name change from `_mdns_server` to `s_server`. While this is likely just a naming convention change, it could indicate broader architectural changes that might affect other parts of the codebase if the global server variable semantics have changed. The function maintains the same parameter signature and delegation pattern to `mdns_service_instance_name_set_for_host`.

However, the presence of `mdns_instance_name_set` (Result 2) is concerning as it suggests a parallel API with different semantics - it only takes an instance parameter and implements the functionality directly rather than delegating. This could lead to confusion about which function to use and potential inconsistencies in service instance management. | +| [mdns_service_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6674) | [mdns_service_remove_for_host](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1445) | The refactored function calls `free_service(a->service)` and `mdns_mem_free(a)` after removing the service from the linked list, but the original function used `_mdns_free_service(a->service)`. Without seeing the implementation of `free_service` vs `_mdns_free_service`, there's a risk that the refactored version may have different memory management behavior, potentially leading to memory leaks or double-free issues if the implementations differ. | +| [mdns_service_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6727) | [mdns_service_remove](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1498) | | +| [mdns_service_remove_all](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6735) | [mdns_service_remove_all](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1506) | | +| [mdns_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6763) | [mdns_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L673) | | +| [_mdns_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6770) | [mdns_priv_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L25) | The refactoring changes the locking mechanism from MDNS_SERVICE_LOCK/UNLOCK to mdns_priv_service_lock/unlock without clear evidence of functional equivalence. This could introduce subtle synchronization issues, deadlock risks, or race conditions if the new locking functions have different semantics, scope, or implementation than the original ones. The change from static to public function scope (mdns_priv_query_results_free) also increases the risk of improper direct usage bypassing necessary locking. | +| [_mdns_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6770) | [mdns_query_results_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L673) | The refactoring changes the locking mechanism from MDNS_SERVICE_LOCK/UNLOCK to mdns_priv_service_lock/unlock without clear evidence of functional equivalence. This could introduce subtle synchronization issues, deadlock risks, or race conditions if the new locking functions have different semantics, scope, or implementation than the original ones. The change from static to public function scope (mdns_priv_query_results_free) also increases the risk of improper direct usage bypassing necessary locking. | +| [mdns_query_async_delete](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6801) | [mdns_query_async_delete](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L680) | | +| [mdns_query_async_get_results](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6817) | [mdns_query_async_get_results](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L696) | The function implementation remains identical, but the relocation from mdns.c to mdns_querier.c introduces potential architectural risks. The main concerns are: 1) Header file dependencies - mdns.h must properly declare the function and include necessary types from the new module, 2) Structure definition consistency - mdns_search_once_t must have identical layout in both compilation units, particularly the done_semaphore and result fields, 3) Build system integration - mdns_querier.c must be properly compiled and linked, and 4) Cross-module access - other functions in mdns.c that previously accessed this function directly may now require proper inter-module function calls. | +| [mdns_query_async_new](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6831) | [mdns_query_async_new](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L710) | The refactored search_init function now uses uint8_t for max_results parameter instead of size_t, which could cause truncation issues if max_results values exceed 255. This is a potential data loss bug since the original function accepted size_t and could handle larger values. Additionally, the search_init function creates a semaphore that needs verification of proper cleanup in all error paths through search_free. | +| [mdns_query_generic](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6853) | [mdns_query_generic](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L732) | The refactored function uses renamed helper functions (mdns_priv_is_server_init, mdns_utils_str_null_or_empty, search_init, send_search_action, search_free) without clear verification that these maintain the exact same behavior and error handling semantics as their original counterparts. Specifically:

1. The condition `mdns_utils_str_null_or_empty(service) != mdns_utils_str_null_or_empty(proto)` relies on the renamed string function returning consistent boolean values
2. The search initialization and action functions may have different memory management or error propagation behaviors
3. The semaphore wait on `search->done_semaphore` assumes the internal search structure and synchronization mechanism remains unchanged
4. Without seeing the implementations of these renamed functions, we cannot verify that resource cleanup (search_free) properly handles the results pointer assignment that occurs before freeing

These renamed functions could introduce subtle behavioral changes in error cases, memory management, or protocol compliance. | +| [mdns_query](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6884) | [mdns_query](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L764) | The refactored mdns_query function appears identical to the original, but its dependency on mdns_query_generic has been significantly changed, introducing potential subtle bugs:

1. **Timeout handling regression**: The refactored mdns_query_generic uses `portMAX_DELAY` instead of the original timeout parameter, making queries potentially block indefinitely rather than respecting the user-specified timeout.

2. **Error code inconsistency**: The refactored version returns `ESP_ERR_NO_MEM` when `send_search_action` fails, but this function could fail for other reasons (network errors, internal state issues) that should return different error codes.

3. **Parameter validation change**: The validation logic for service/proto parameters has been altered from the original implementation, potentially allowing invalid parameter combinations that were previously rejected.

4. **Function dependency changes**: The refactored code uses `search_init`, `send_search_action`, and `search_free` instead of the original `_mdns_search_init` and internal mechanisms, raising concerns about functional equivalence and proper resource management.

While the mdns_query wrapper function itself is unchanged, these underlying changes in mdns_query_generic could lead to unexpected blocking behavior, incorrect error reporting, and different parameter validation semantics. | +| [mdns_query_ptr](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6889) | [mdns_query_ptr](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L769) | | +| [mdns_query_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6898) | [mdns_query_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L778) | | +| [mdns_query_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6907) | [mdns_query_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L787) | | +| [mdns_lookup_delegated_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6916) | [mdns_lookup_delegated_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1531) | The refactored lookup_service function in mdns_console.c (Result 2) creates a circular dependency: it calls mdns_lookup_delegated_service/mdns_lookup_selfhosted_service, which in turn call the lookup_service function in mdns_responder.c. This appears to be a naming collision where two different functions with the same name exist in different compilation units, which could lead to linker errors or incorrect function resolution depending on the build configuration. | +| [mdns_lookup_selfhosted_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6931) | [mdns_lookup_selfhosted_service](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_responder.c#L1546) | The refactored code introduces a potential circular dependency. The function `lookup_service` in mdns_console.c calls `mdns_lookup_selfhosted_service`, which in turn calls `lookup_service` (the internal implementation). This creates infinite recursion. The original code called `_mdns_lookup_service` directly without this circular call pattern. Additionally, the parameter order in the `lookup_service` wrapper function appears inconsistent - it takes a `delegated` parameter but the actual implementation functions don't have this parameter in their signatures. | +| [mdns_query_a](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6947) | [mdns_query_a](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L797) | The refactored function changes the validation function from `_str_null_or_empty` to `mdns_utils_str_null_or_empty`. While this appears to be a simple renaming for better organization, there's a risk that the new function might have different behavior (e.g., different empty string definition, different return values, or different error handling) that could affect the input validation logic. The original codebase search didn't find references to `mdns_utils_str_null_or_empty`, suggesting this might be a newly introduced utility function whose implementation should be verified for functional equivalence. | +| [mdns_query_aaaa](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L6986) | [mdns_query_aaaa](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_querier.c#L836) | | +| [mdns_debug_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7026) | [dbg_packet](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_debug.c#L106) | The refactored function changes the timestamp type from uint32_t to uint64_t but uses the same format string logic. In embedded systems with 32-bit architectures, this could cause issues with printf formatting and stack alignment. Additionally, the static mdns_name_t variable could cause thread-safety issues in multi-threaded environments, and the new mdns_dbg_flush() call at the end might introduce blocking behavior that wasn't present in the original function. | +| [_mdns_sync_browse_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7256) | [mdns_priv_browse_sync](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L595) | The refactored function `mdns_priv_browse_sync` hardcodes the action type to `ACTION_BROWSE_SYNC`, removing the `type` parameter from the original function. This could be problematic if the original function was ever called with action types other than `ACTION_BROWSE_SYNC`. While the search shows only `ACTION_BROWSE_SYNC` usage in the original codebase, this change reduces flexibility and could break future extensions that might use different browse sync action types. | +| [_mdns_send_browse_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7278) | [send_browse_action](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L26) | The refactored function uses `!mdns_priv_queue_action(action)` instead of the original `xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS`. This boolean logic inversion suggests that `mdns_priv_queue_action` returns true for success and false for failure, while `xQueueSend` returns `pdPASS` (typically 1) for success and other values for failure. If `mdns_priv_queue_action` doesn't properly replicate the exact queueing semantics (including the zero timeout behavior), this could introduce subtle behavioral changes in the action queueing mechanism, potentially affecting the MDNS browsing functionality under high load conditions. | +| [_mdns_browse_item_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7301) | [browse_item_free](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L49) | | +| [_mdns_browse_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7314) | [browse_init](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L141) | The refactored browse_init function has inconsistent error handling for memory allocation failures. When browse->service allocation fails, HOOK_MALLOC_FAILED is called twice (once for browse malloc and once for service strndup), but when browse->proto allocation fails, HOOK_MALLOC_FAILED is only called once (for browse malloc). This inconsistency could lead to incorrect memory failure tracking or logging. The original function had consistent behavior where HOOK_MALLOC_FAILED was only called for the initial browse allocation failure. | +| [mdns_browse_new](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7345) | [mdns_browse_new](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L617) | The refactored mdns_browse_new function has a critical logic error in the server initialization check. The original code returned NULL when `!_mdns_server` (server not initialized), but the refactored code returns NULL when `mdns_priv_is_server_init()` (server IS initialized). This logical inversion would prevent browsing from working when the MDNS server is properly initialized, making the function always return NULL in normal operation. | +| [mdns_browse_delete](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7366) | [mdns_browse_delete](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L638) | | +| [_mdns_browse_finish](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7389) | [browse_finish](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L119) | The refactored function uses a global variable 's_browse' instead of accessing '_mdns_server->browse'. This structural change could introduce subtle bugs if:
1. 's_browse' is not properly synchronized with the server state in other parts of the system
2. There are concurrent access issues since global variables are more prone to race conditions
3. Other functions that previously operated on '_mdns_server->browse' might not be updated to use 's_browse' consistently
4. The initialization and lifetime management of 's_browse' might differ from the server-based approach | +| [_mdns_browse_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7411) | [browse_add](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L176) | The refactored function uses a global variable 's_browse' instead of accessing it through '_mdns_server->browse'. This structural change could introduce subtle bugs if:
1. 's_browse' is not properly initialized or synchronized with the original server state
2. Other parts of the system still expect to access browse data through '_mdns_server->browse'
3. There are thread safety issues since global 's_browse' might not have the same protection as the original server structure
4. The browse queue management is now split between different data structures, potentially causing inconsistencies | +| [_mdns_browse_send](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7440) | [browse_send](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L80) | The refactored function replaces _mdns_search_send_pcb with mdns_priv_query_send, which changes the PCB readiness check from mdns_is_netif_ready(tcpip_if, ip_protocol) && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT to mdsn_priv_pcb_is_inited(tcpip_if, ip_protocol). This could change when queries are sent and there's a potential typo in "mdsn_priv_pcb_is_inited" (should likely be "mdns_priv_pcb_is_inited"). Additionally, the packet management in mdns_priv_query_send (create, dispatch, immediate free) may have different error handling characteristics than the original implementation. | +| [_mdns_add_browse_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7461) | [add_browse_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L288) | The function visibility has changed from static to potentially non-static (the refactored code snippet doesn't show the static keyword). This could lead to linkage issues or unintended external access. Additionally, all call sites in the original codebase need to be verified to ensure they now call the renamed function in the new file location (mdns_browser.c vs mdns.c). | +| [_mdns_browse_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7488) | [mdns_priv_browse_result_add_ip](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L315) | The refactored function replaces `_mdns_add_browse_result` with `add_browse_result` without the `_mdns_` prefix, which may indicate a missing namespace or different function implementation. Additionally, the TTL update logic changed from `_mdns_result_update_ttl` to `mdns_priv_query_update_result_ttl`, which could have different behavior for TTL calculations. The search results show no matches for these renamed functions in the refactored codebase, suggesting they may be missing or have different signatures/implementations. | +| [_mdns_browse_find_from](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7553) | [mdns_priv_browse_find](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L205) | The refactored function changed from taking a browse list parameter to using a global variable 's_browse', which could introduce thread safety issues and makes the function dependent on global state. Additionally, the original function was called with '_mdns_server->browse' as parameter, but the refactored version directly uses 's_browse' without clear evidence that these are equivalent. This change in data access pattern could lead to incorrect browse list traversal if the global variable is not properly synchronized with the original server state. | +| [is_txt_item_in_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7583) | [is_txt_item_in_list](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L376) | The refactored function appears to be identical to the original (100% similarity) but has been moved from mdns.c to mdns_browser.c. This change in location could introduce subtle issues if:
1. The function now has different visibility/scope in the new file
2. Callers in the original location (mdns.c) cannot access it if it remains static
3. The function assumes different context or dependencies in the browser component vs responder component
4. The TXT item comparison logic might need different behavior between browser and responder contexts | +| [_mdns_browse_result_add_txt](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7601) | ??? | | +| [_mdns_copy_address_in_previous_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7699) | [copy_address_in_previous_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L492) | | +| [_mdns_browse_result_add_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7720) | [mdns_priv_browse_result_add_srv](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L513) | The refactored function calls two helper functions without the expected 'mdns_priv_' prefix: 'copy_address_in_previous_result' and 'add_browse_result'. This naming inconsistency suggests these functions may not be properly declared or implemented in the refactored codebase, potentially leading to linker errors or incorrect function resolution. Additionally, the final explicit 'return' statement was removed, which could affect code clarity but likely doesn't impact functionality. | +| [_mdns_browse_sync](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7800) | [browse_sync](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_browser.c#L59) | | +| [_debug_printf_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7821) | [dbg_printf_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_debug.c#L367) | The refactored function dbg_printf_result has been changed from non-static to static, which may break external calls if the function was used outside its compilation unit. Additionally, the addition of mdns_dbg_flush() at the end changes the debug output behavior - it now forces immediate flushing which could impact performance in debug-heavy scenarios or embedded systems with constrained I/O resources. | +| [debug_printf_browse_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7852) | [mdns_debug_printf_browse_result](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_debug.c#L399) | The refactored function adds an unconditional call to mdns_dbg_flush() which was not present in the original function. This could potentially impact performance in debug mode, especially if called frequently, as it may force immediate output flushing that wasn't happening before. Additionally, the debug macro names have changed from _mdns_dbg_printf/_debug_printf_result to dbg_printf/dbg_printf_result, which could have different formatting or behavior if these macros are not functionally equivalent. | +| [debug_printf_browse_result_all](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns.c#L7859) | [mdns_debug_printf_browse_result_all](https://github.com/espressif/esp-protocols/blob/main/components/mdns/mdns_debug.c#L407) | The refactored function adds a call to mdns_dbg_flush() at the end. While this might be intentional to ensure debug output is flushed, it could introduce timing differences in debugging scenarios or potentially block if the flush operation has significant overhead. In embedded systems or real-time environments, this could affect system behavior during debugging sessions. The original function did not have any flush operation, so output buffering behavior may have changed. | diff --git a/components/mdns/tests/host_unit_test/CMakeLists.txt b/components/mdns/tests/host_unit_test/CMakeLists.txt new file mode 100644 index 0000000000..40d8c60e88 --- /dev/null +++ b/components/mdns/tests/host_unit_test/CMakeLists.txt @@ -0,0 +1,85 @@ +cmake_minimum_required(VERSION 3.5) +if(ESP_PLATFORM) + # Used for reconfiguration of the project + # Executed by idf.py reconfigure + set(EXTRA_COMPONENT_DIRS ../../) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) + project(mdns_host_unit_test_config) + return() +endif() + +project(mdns_host_unit_test C) + +# Set ENABLE_UNIT_TESTS with a default of OFF +if(NOT DEFINED UNIT_TESTS) + set(UNIT_TESTS "OFF" CACHE STRING "Unit tests: OFF, test_receiver, test_sender") +else() + set(ENABLE_UNIT_TESTS 1) + message(STATUS "Unit testing enabled with UNIT_TESTS=${UNIT_TESTS}") +endif() + +# Set variables for directories +set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(STUB_DIR ${TEST_DIR}/stubs) +set(MDNS_DIR ${TEST_DIR}/../../) +set(COMPONENT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../) +set(ESP_NETIF_LINUX_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../host_test/components/esp_netif_linux/include) +set(COMMON_COMPONENTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../common_components/linux_compat/) +set(IDF_COMPONENTS_DIR "$ENV{IDF_PATH}/components") + +include_directories( + ${TEST_DIR} + ${STUB_DIR} + ${TEST_DIR}/build/config + ${COMMON_COMPONENTS_DIR}/freertos/include + ${COMMON_COMPONENTS_DIR}/esp_timer/include + ${IDF_COMPONENTS_DIR}/esp_event/include + ${IDF_COMPONENTS_DIR}/esp_netif/include + ${IDF_COMPONENTS_DIR}/esp_common/include + ${IDF_COMPONENTS_DIR}/esp_system/include + ${IDF_COMPONENTS_DIR}/log/include + ${IDF_COMPONENTS_DIR}/esp_rom/include + ${IDF_COMPONENTS_DIR}/heap/include + ${IDF_COMPONENTS_DIR}/esp_rom/linux/include/linux/ + ${ESP_NETIF_LINUX_DIR} + ${COMPONENT_DIR} + ${COMPONENT_DIR}/include + ${COMPONENT_DIR}/private_include +) + +# Source files +set(SOURCES + ${STUB_DIR}/mdns_mem_caps.c + ${STUB_DIR}/esp_idf.c + ${STUB_DIR}/mdns_networking.c + ${STUB_DIR}/mdns_engine.c + ${MDNS_DIR}/mdns_receive.c + ${MDNS_DIR}/mdns_utils.c + ${MDNS_DIR}/mdns_browser.c + ${MDNS_DIR}/mdns_querier.c + ${MDNS_DIR}/mdns_responder.c + ${MDNS_DIR}/mdns_send.c + ${MDNS_DIR}/mdns_pcb.c + ${MDNS_DIR}/mdns_netif.c +) + +if(ENABLE_UNIT_TESTS) + include(unity/unit_test.cmake) +else() + list(APPEND SOURCES main.c) +endif() + +add_executable(${PROJECT_NAME} ${SOURCES}) + +# Add libbsd if available +find_library(LIB_BSD bsd) +if(LIB_BSD) + target_link_libraries(${PROJECT_NAME} PRIVATE ${LIB_BSD}) +elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.") +endif() + +# Enable testing if unit tests are enabled +if(ENABLE_UNIT_TESTS) + include(unity/enable_testing.cmake) +endif() diff --git a/components/mdns/tests/host_unit_test/input/generate_cases.py b/components/mdns/tests/host_unit_test/input/generate_cases.py new file mode 100644 index 0000000000..5bf0193eee --- /dev/null +++ b/components/mdns/tests/host_unit_test/input/generate_cases.py @@ -0,0 +1,166 @@ +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +from dnslib import (AAAA, PTR, RR, SRV, TXT, A, DNSHeader, DNSQuestion, + DNSRecord) + + +def create_mdns_packet(queries, answers, additional, output_file='mdns_packet.bin'): + dns_header = DNSHeader(id=0, qr=1, aa=1, ra=0) + dns_record = DNSRecord(dns_header) + for qname, qtype in queries: + dns_record.add_question(DNSQuestion(qname, qtype)) + for name, qtype, value, ttl in answers: + if qtype == 'A': + rr = RR(name, rdata=A(value), ttl=ttl) + elif qtype == 'PTR': + rr = RR(name, rdata=PTR(value), ttl=ttl) + elif qtype == 'TXT': + rr = RR(name, rdata=TXT(value), ttl=ttl) + elif qtype == 'AAAA': + rr = RR(name, rdata=AAAA(value), ttl=ttl) + elif qtype == 'SRV': + # SRV value should be in format: "priority weight port target" + parts = value.split() + priority, weight, port = map(int, parts[:3]) + target = parts[3] + rr = RR(name, rdata=SRV(priority, weight, port, target), ttl=ttl) + else: + print(f'Unsupported type: {qtype}') + continue + dns_record.add_answer(rr) + for name, qtype, value, ttl in additional: + if qtype == 'A': + rr = RR(name, rdata=A(value), ttl=ttl) + elif qtype == 'TXT': + rr = RR(name, rdata=TXT(value), ttl=ttl) + elif qtype == 'AAAA': + rr = RR(name, rdata=AAAA(value), ttl=ttl) + elif qtype == 'SRV': + parts = value.split() + priority, weight, port = map(int, parts[:3]) + target = parts[3] + rr = RR(name, rdata=SRV(priority, weight, port, target), ttl=ttl) + else: + print(f'Unsupported type: {qtype}') + continue + dns_record.add_ar(rr) + mdns_payload = dns_record.pack() + + with open(output_file, 'wb') as f: + f.write(mdns_payload) + + print(f'mDNS packet saved as {output_file}') + + +# Test case 1: Basic hostname queries +queries = [ + ('test4.local.', 1), # A record for main hostname + ('test.local.', 1), # A record for main hostname +] +answers = [ +] +additional = [ +] +create_mdns_packet(queries, answers, additional, 'test_hostname_queries.bin') +print('Test case 1: Hostname queries') + +# Test case 2: HTTP service discovery with subtypes +queries = [ + ('_http._tcp.local.', 12), # PTR query + ('subtype._sub._http._tcp.local.', 12), # Subtype query +] +answers = [ + ('_http._tcp.local.', 'PTR', 'inst1._http._tcp.local.', 120), + ('_http._tcp.local.', 'PTR', 'inst2._http._tcp.local.', 120), + ('subtype._sub._http._tcp.local.', 'PTR', 'inst1._http._tcp.local.', 120), +] +additional = [ + ('inst1._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('inst1._http._tcp.local.', 'TXT', 'board=esp32\0tcp_check=no\0ssh_upload=no\0auth_upload=no', 120), + ('inst2._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('inst2._http._tcp.local.', 'TXT', 'board=esp32\0tcp_check=no\0ssh_upload=no\0auth_upload=no', 120), +] +create_mdns_packet(queries, answers, additional, 'test_http_services.bin') +print('Test case 2: HTTP service discovery with subtypes') + +# Test case 3: Scanner service discovery +queries = [ + ('_scanner._tcp.local.', 12), # PTR query +] +answers = [ + ('_scanner._tcp.local.', 'PTR', 'inst4._scanner._tcp.local.', 120), + ('_scanner._tcp.local.', 'PTR', 'inst5._scanner._tcp.local.', 120), +] +additional = [ + ('inst4._scanner._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('inst5._scanner._tcp.local.', 'SRV', '0 0 80 test.local.', 120), +] +create_mdns_packet(queries, answers, additional, 'test_scanner_services.bin') +print('Test case 3: Scanner service discovery') + +# Test case 4: UDP service (sleep protocol) +queries = [ + ('_sleep._udp.local.', 12), # PTR query +] +answers = [ + ('_sleep._udp.local.', 'PTR', 'inst7._sleep._udp.local.', 120), +] +additional = [ + ('inst7._sleep._udp.local.', 'SRV', '0 0 80 test.local.', 120), +] +create_mdns_packet(queries, answers, additional, 'test_udp_service.bin') +print('Test case 4: UDP service discovery') + +# Test case 5: Async queries from main.c +queries = [ + ('host_name.local.', 1), # A query + ('host_name2.local.', 28), # AAAA query + ('minifritz._http._tcp.local.', 12), # PTR query +] +answers = [ + ('host_name.local.', 'A', '192.168.1.10', 120), + ('host_name2.local.', 'AAAA', 'fe80::1234', 120), + ('minifritz._http._tcp.local.', 'PTR', 'minifritz._http._tcp.local.', 120), +] +additional = [ + ('minifritz._http._tcp.local.', 'SRV', '0 0 80 minifritz.local.', 120), + ('minifritz.local.', 'A', '192.168.1.20', 120), +] +create_mdns_packet(queries, answers, additional, 'test_async_queries.bin') +print('Test case 5: Async queries') + +# Test case 6: Multiple services on same instance +queries = [ + ('_http._tcp.local.', 12), + ('_scanner._tcp.local.', 12), +] +answers = [ + ('_http._tcp.local.', 'PTR', 'inst1._http._tcp.local.', 120), + ('_http._tcp.local.', 'PTR', 'inst2._http._tcp.local.', 120), + ('_http._tcp.local.', 'PTR', 'inst3._http._tcp.local.', 120), + ('_scanner._tcp.local.', 'PTR', 'inst4._scanner._tcp.local.', 120), +] +additional = [ + ('inst1._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('inst2._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('test.local.', 'A', '192.168.1.100', 120), +] +create_mdns_packet(queries, answers, additional, 'test_multiple_services.bin') +print('Test case 6: Multiple services') + +# Test case 7: Subtype queries for different hosts +queries = [ + ('subtype._sub._http._tcp.local.', 12), + ('subtype3._sub._http._tcp.local.', 12), +] +answers = [ + ('subtype._sub._http._tcp.local.', 'PTR', 'inst1._http._tcp.local.', 120), + ('subtype3._sub._http._tcp.local.', 'PTR', 'inst2._http._tcp.local.', 120), +] +additional = [ + ('inst1._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('inst2._http._tcp.local.', 'SRV', '0 0 80 test.local.', 120), + ('test.local.', 'A', '192.168.1.100', 120), +] +create_mdns_packet(queries, answers, additional, 'test_subtype_queries.bin') +print('Test case 7: Subtype queries') diff --git a/components/mdns/tests/host_unit_test/main.c b/components/mdns/tests/host_unit_test/main.c new file mode 100644 index 0000000000..a507fba9ad --- /dev/null +++ b/components/mdns/tests/host_unit_test/main.c @@ -0,0 +1,110 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include "esp_err.h" +#include "mdns_receive.h" +#include "mdns_responder.h" +#include "mdns_mem_caps.h" + +esp_err_t mdns_packet_push(esp_ip_addr_t *addr, int port, mdns_if_t tcpip_if, uint8_t*data, size_t len); + +mdns_search_once_t *s_a, *s_aaaa, *s_ptr, *s_srv, *s_txt; + +void init_responder(void) +{ + mdns_ip_addr_t addr = { .addr = { .u_addr = ESP_IPADDR_TYPE_V4 } }; + addr.addr.u_addr.ip4.addr = 0x11111111; + mdns_txt_item_t txt[4] = { + {"board", "esp32"}, + {"tcp_check", "no"}, + {"ssh_upload", "no"}, + {"auth_upload", "no"} + }; + mdns_priv_responder_init(); + mdns_hostname_set("test"); + mdns_instance_name_set("test2"); + mdns_delegate_hostname_add("test3", NULL); + mdns_delegate_hostname_add("test4", &addr); + mdns_service_add("inst1", "_http", "_tcp", 80, txt, 4); + mdns_service_subtype_add_for_host("inst1", "_http", "_tcp", "test", "subtype"); + mdns_service_add("inst2", "_http", "_tcp", 80, txt, 1); + mdns_service_subtype_add_for_host("inst2", "_http", "_tcp", "test", "subtype3"); + mdns_service_add("inst3", "_http", "_tcp", 80, NULL, 0); + mdns_service_add_for_host("deleg1", "_http", "_tcp", "test3", 80, txt, 2); + mdns_service_add_for_host(NULL, "_http", "_tcp", "test4", 80, txt, 2); + mdns_service_add(NULL, "_scanner", "_tcp", 80, NULL, 0); + mdns_service_add("inst5", "_scanner", "_tcp", 80, NULL, 0); + mdns_service_add("inst6", "_http", "_tcp", 80, NULL, 0); + mdns_service_add("inst7", "_sleep", "_udp", 80, NULL, 0); + + s_a = mdns_query_async_new("host_name", NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL); + s_aaaa = mdns_query_async_new("host_name2", NULL, NULL, MDNS_TYPE_AAAA, 1000, 1, NULL); + s_ptr = mdns_query_async_new("minifritz", "_http", "_tcp", MDNS_TYPE_PTR, 1000, 1, NULL); + s_srv = mdns_query_async_new("fritz", "_http", "_tcp", MDNS_TYPE_SRV, 1000, 1, NULL); + s_txt = mdns_query_async_new("fritz", "_http", "_tcp", MDNS_TYPE_TXT, 1000, 1, NULL); +} + +void deinit_responder(void) +{ + mdns_query_async_delete(s_a); + mdns_query_async_delete(s_aaaa); + mdns_query_async_delete(s_ptr); + mdns_query_async_delete(s_srv); + mdns_query_async_delete(s_txt); + mdns_service_remove_all(); + mdns_priv_responder_free(); +} + +static void send_packet(bool ip4, bool mdns_port, uint8_t*data, size_t len) +{ + esp_ip_addr_t addr4 = ESP_IP4ADDR_INIT(192, 168, 1, 1); + esp_ip_addr_t addr6 = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfe800000); + esp_ip_addr_t *addr = ip4 ? &addr4 : &addr6; + int port = mdns_port ? 53 : 5353; + + if (mdns_packet_push(addr, port, 0, data, len) != ESP_OK) { + printf("Failed to push packet\n"); + } +} + +int main(int argc, char **argv) +{ + init_responder(); + + // Original fuzzing code + uint8_t buf[1460]; + FILE *file; + size_t len = 1460; + memset(buf, 0, len); +#ifndef __AFL_LOOP + // + // Note: parameter1 is a file (mangled packet) which caused the crash + if (argc != 2) { + printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n"); + return 1; + } + file = fopen(argv[1], "r"); + assert(file >= 0); + len = fread(buf, 1, 1460, file); + fclose(file); + { +#else + while (__AFL_LOOP(1000)) { + memset(buf, 0, 1460); + size_t len = read(0, buf, 1460); +#endif + send_packet(true, true, buf, len); + send_packet(true, false, buf, len); + send_packet(false, true, buf, len); + send_packet(false, false, buf, len); + } + deinit_responder(); + + return 0; +} diff --git a/components/mdns/tests/host_unit_test/sdkconfig.defaults b/components/mdns/tests/host_unit_test/sdkconfig.defaults new file mode 100644 index 0000000000..cd903adc40 --- /dev/null +++ b/components/mdns/tests/host_unit_test/sdkconfig.defaults @@ -0,0 +1,6 @@ +# This file was generated using idf.py save-defconfig. It can be edited manually. +# Espressif IoT Development Framework (ESP-IDF) 5.5.0 Project Minimal Configuration +# +CONFIG_MDNS_PREDEF_NETIF_STA=n +CONFIG_MDNS_PREDEF_NETIF_AP=n +CONFIG_MDNS_PREDEF_NETIF_ETH=n diff --git a/components/mdns/tests/host_unit_test/stubs/esp_assert.h b/components/mdns/tests/host_unit_test/stubs/esp_assert.h new file mode 100644 index 0000000000..989f6cf277 --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/esp_assert.h @@ -0,0 +1,6 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#define ESP_STATIC_ASSERT _Static_assert diff --git a/components/mdns/tests/host_unit_test/stubs/esp_idf.c b/components/mdns/tests/host_unit_test/stubs/esp_idf.c new file mode 100644 index 0000000000..0237a4091c --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/esp_idf.c @@ -0,0 +1,82 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "esp_log.h" +#include "esp_netif.h" +#include "freertos/FreeRTOS.h" + +void esp_log(esp_log_config_t config, const char* tag, const char* format, ...) +{} + +uint32_t esp_log_timestamp(void) +{ + return 0; +} + +uint32_t esp_get_free_heap_size(void) +{ + return 0; +} + +esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) +{ + return (esp_netif_t*)1; +} + +esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) +{ + ip_info->ip.addr = 0xC0A80164; // 192.168.1.100 + ip_info->netmask.addr = 0xFFFFFF00; // 255.255.255.0 + ip_info->gw.addr = 0xC0A80101; // 192.168.1.1 + return ESP_OK; +} + +esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) +{ + if_ip6->addr[0] = 0xFE80; + if_ip6->addr[1] = 0x0000; + if_ip6->addr[2] = 0x0000; + if_ip6->addr[3] = 0x0001; + return ESP_OK; +} + +uint32_t esp_random(void) +{ + return 0; +} + +TickType_t xTaskGetTickCount(void) +{ + return 0; +} + +int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]) +{ + if_ip6[0].addr[0] = 0xFE80; + if_ip6[0].addr[1] = 0x0000; + if_ip6[0].addr[2] = 0x0000; + if_ip6[0].addr[3] = 0x0001; + return 1; +} + + +BaseType_t xSemaphoreGive(QueueHandle_t xQueue) +{ + return pdPASS; +} + +BaseType_t xSemaphoreTake(QueueHandle_t xQueue, TickType_t xTicksToWait) +{ + return pdPASS; +} + +QueueHandle_t xSemaphoreCreateBinary(void) +{ + return (QueueHandle_t)1; +} + +void vQueueDelete(QueueHandle_t xQueue) +{ +} diff --git a/components/mdns/tests/host_unit_test/stubs/esp_random.h b/components/mdns/tests/host_unit_test/stubs/esp_random.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/mdns/tests/host_unit_test/stubs/mdns_engine.c b/components/mdns/tests/host_unit_test/stubs/mdns_engine.c new file mode 100644 index 0000000000..400ec929f9 --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/mdns_engine.c @@ -0,0 +1,62 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "mdns.h" +#include "mdns_private.h" +#include "mdns_querier.h" +#include "mdns_browser.h" +#include "mdns_send.h" +#include "mdns_responder.h" +#include "mdns_receive.h" +#include "mdns_mem_caps.h" + +static void execute_action(mdns_action_t *action) +{ + switch (action->type) { + case ACTION_SYSTEM_EVENT: + break; + case ACTION_SEARCH_ADD: + case ACTION_SEARCH_SEND: + case ACTION_SEARCH_END: + mdns_priv_query_action(action, ACTION_RUN); + break; + case ACTION_BROWSE_ADD: + case ACTION_BROWSE_SYNC: + case ACTION_BROWSE_END: + mdns_priv_browse_action(action, ACTION_RUN); + break; + + case ACTION_TX_HANDLE: + mdns_priv_send_action(action, ACTION_RUN); + break; + case ACTION_RX_HANDLE: + mdns_priv_receive_action(action, ACTION_RUN); + break; + case ACTION_HOSTNAME_SET: + case ACTION_INSTANCE_SET: + case ACTION_DELEGATE_HOSTNAME_ADD: + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + case ACTION_DELEGATE_HOSTNAME_REMOVE: + mdns_priv_responder_action(action, ACTION_RUN); + break; + default: + break; + } + mdns_mem_free(action); +} + +bool mdns_priv_queue_action(mdns_action_t *action) +{ + execute_action(action); + return true; +} + +void mdns_priv_service_lock(void) +{ +} + +void mdns_priv_service_unlock(void) +{ +} diff --git a/components/mdns/tests/host_unit_test/stubs/mdns_mem_caps.c b/components/mdns/tests/host_unit_test/stubs/mdns_mem_caps.c new file mode 100644 index 0000000000..7da2681a56 --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/mdns_mem_caps.c @@ -0,0 +1,71 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "mdns_mem_caps.h" + +void *mdns_mem_malloc(size_t size) +{ + return malloc(size); +} + +void *mdns_mem_calloc(size_t num, size_t size) +{ + return calloc(num, size); +} + +void mdns_mem_free(void *ptr) +{ + free(ptr); +} + +char *mdns_mem_strdup(const char *s) +{ + if (s == NULL) { + return NULL; + } + + size_t len = strlen(s) + 1; + char *new_str = mdns_mem_malloc(len); + if (new_str == NULL) { + return NULL; + } + + return memcpy(new_str, s, len); +} + +char *mdns_mem_strndup(const char *s, size_t n) +{ + if (s == NULL) { + return NULL; + } + + size_t len = strlen(s); + if (len > n) { + len = n; + } + + char *new_str = mdns_mem_malloc(len + 1); + if (new_str == NULL) { + return NULL; + } + + memcpy(new_str, s, len); + new_str[len] = '\0'; + + return new_str; +} + +void *mdns_mem_task_malloc(size_t size) +{ + return malloc(size); +} + +void mdns_mem_task_free(void *ptr) +{ + free(ptr); +} diff --git a/components/mdns/tests/host_unit_test/stubs/mdns_networking.c b/components/mdns/tests/host_unit_test/stubs/mdns_networking.c new file mode 100644 index 0000000000..2bca2e5216 --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/mdns_networking.c @@ -0,0 +1,90 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "mdns_networking.h" +#include "mdns_mem_caps.h" +#include "mdns_service.h" + +struct pbuf { + void *payload; + size_t len; +}; + +bool mdns_priv_if_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return true; // Default to ready for testing +} + +esp_err_t mdns_priv_if_init(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return ESP_OK; +} + +esp_err_t mdns_priv_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) +{ + return ESP_OK; +} + +size_t mdns_priv_if_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, const esp_ip_addr_t *ip, uint16_t port, uint8_t *data, size_t len) +{ + return len; // Return the input length as if all data was sent successfully +} + +void *mdns_priv_get_packet_data(mdns_rx_packet_t *packet) +{ + return packet->pb->payload; +} + +size_t mdns_priv_get_packet_len(mdns_rx_packet_t *packet) +{ + return packet->pb->len; +} + +void mdns_priv_packet_free(mdns_rx_packet_t *packet) +{ + mdns_mem_free(packet->pb->payload); + mdns_mem_free(packet->pb); + mdns_mem_free(packet); +} + +esp_err_t mdns_packet_push(esp_ip_addr_t *addr, int port, mdns_if_t tcpip_if, uint8_t*data, size_t len) +{ + // Allocate the packet structure and pass it to the mdns main engine + mdns_rx_packet_t *packet = (mdns_rx_packet_t *) mdns_mem_calloc(1, sizeof(mdns_rx_packet_t)); + struct pbuf *packet_pbuf = mdns_mem_calloc(1, sizeof(struct pbuf)); + uint8_t *buf = mdns_mem_malloc(len); + if (packet == NULL || packet_pbuf == NULL || buf == NULL) { + mdns_mem_free(buf); + mdns_mem_free(packet_pbuf); + mdns_mem_free(packet); + return ESP_ERR_NO_MEM; + } + memcpy(buf, data, len); + packet_pbuf->payload = buf; + packet_pbuf->len = len; + packet->tcpip_if = tcpip_if; + packet->pb = packet_pbuf; + packet->src_port = port; + memcpy(&packet->src, addr, sizeof(esp_ip_addr_t)); + memset(&packet->dest, 0, sizeof(esp_ip_addr_t)); + packet->multicast = 1; + packet->dest.type = packet->src.type; + packet->ip_protocol = packet->src.type == ESP_IPADDR_TYPE_V4 ? MDNS_IP_PROTOCOL_V4 : MDNS_IP_PROTOCOL_V6; + mdns_action_t *action = (mdns_action_t *)mdns_mem_malloc(sizeof(mdns_action_t)); + if (!action) { + return ESP_ERR_NO_MEM; + } + + action->type = ACTION_RX_HANDLE; + action->data.rx_handle.packet = packet; + if (!mdns_priv_queue_action(action)) { + mdns_mem_free(action); + mdns_mem_free(packet->pb->payload); + mdns_mem_free(packet->pb); + mdns_mem_free(packet); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} diff --git a/components/mdns/tests/host_unit_test/stubs/sdkconfig.h b/components/mdns/tests/host_unit_test/stubs/sdkconfig.h new file mode 100644 index 0000000000..2e7649f23d --- /dev/null +++ b/components/mdns/tests/host_unit_test/stubs/sdkconfig.h @@ -0,0 +1,885 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* + * Automatically generated file. DO NOT EDIT. + * Espressif IoT Development Framework (ESP-IDF) 5.5.0 Configuration Header + */ +#pragma once +#define CONFIG_SOC_BROWNOUT_RESET_SUPPORTED "Not determined" +#define CONFIG_SOC_TWAI_BRP_DIV_SUPPORTED "Not determined" +#define CONFIG_SOC_DPORT_WORKAROUND "Not determined" +#define CONFIG_SOC_CAPS_ECO_VER_MAX 301 +#define CONFIG_SOC_ADC_SUPPORTED 1 +#define CONFIG_SOC_DAC_SUPPORTED 1 +#define CONFIG_SOC_UART_SUPPORTED 1 +#define CONFIG_SOC_MCPWM_SUPPORTED 1 +#define CONFIG_SOC_GPTIMER_SUPPORTED 1 +#define CONFIG_SOC_SDMMC_HOST_SUPPORTED 1 +#define CONFIG_SOC_BT_SUPPORTED 1 +#define CONFIG_SOC_PCNT_SUPPORTED 1 +#define CONFIG_SOC_PHY_SUPPORTED 1 +#define CONFIG_SOC_WIFI_SUPPORTED 1 +#define CONFIG_SOC_SDIO_SLAVE_SUPPORTED 1 +#define CONFIG_SOC_TWAI_SUPPORTED 1 +#define CONFIG_SOC_EFUSE_SUPPORTED 1 +#define CONFIG_SOC_EMAC_SUPPORTED 1 +#define CONFIG_SOC_ULP_SUPPORTED 1 +#define CONFIG_SOC_CCOMP_TIMER_SUPPORTED 1 +#define CONFIG_SOC_RTC_FAST_MEM_SUPPORTED 1 +#define CONFIG_SOC_RTC_SLOW_MEM_SUPPORTED 1 +#define CONFIG_SOC_RTC_MEM_SUPPORTED 1 +#define CONFIG_SOC_I2S_SUPPORTED 1 +#define CONFIG_SOC_RMT_SUPPORTED 1 +#define CONFIG_SOC_SDM_SUPPORTED 1 +#define CONFIG_SOC_GPSPI_SUPPORTED 1 +#define CONFIG_SOC_LEDC_SUPPORTED 1 +#define CONFIG_SOC_I2C_SUPPORTED 1 +#define CONFIG_SOC_SUPPORT_COEXISTENCE 1 +#define CONFIG_SOC_AES_SUPPORTED 1 +#define CONFIG_SOC_MPI_SUPPORTED 1 +#define CONFIG_SOC_SHA_SUPPORTED 1 +#define CONFIG_SOC_FLASH_ENC_SUPPORTED 1 +#define CONFIG_SOC_SECURE_BOOT_SUPPORTED 1 +#define CONFIG_SOC_TOUCH_SENSOR_SUPPORTED 1 +#define CONFIG_SOC_BOD_SUPPORTED 1 +#define CONFIG_SOC_ULP_FSM_SUPPORTED 1 +#define CONFIG_SOC_CLK_TREE_SUPPORTED 1 +#define CONFIG_SOC_MPU_SUPPORTED 1 +#define CONFIG_SOC_WDT_SUPPORTED 1 +#define CONFIG_SOC_SPI_FLASH_SUPPORTED 1 +#define CONFIG_SOC_RNG_SUPPORTED 1 +#define CONFIG_SOC_LIGHT_SLEEP_SUPPORTED 1 +#define CONFIG_SOC_DEEP_SLEEP_SUPPORTED 1 +#define CONFIG_SOC_LP_PERIPH_SHARE_INTERRUPT 1 +#define CONFIG_SOC_PM_SUPPORTED 1 +#define CONFIG_SOC_DPORT_WORKAROUND_DIS_INTERRUPT_LVL 5 +#define CONFIG_SOC_XTAL_SUPPORT_26M 1 +#define CONFIG_SOC_XTAL_SUPPORT_40M 1 +#define CONFIG_SOC_XTAL_SUPPORT_AUTO_DETECT 1 +#define CONFIG_SOC_ADC_RTC_CTRL_SUPPORTED 1 +#define CONFIG_SOC_ADC_DIG_CTRL_SUPPORTED 1 +#define CONFIG_SOC_ADC_DMA_SUPPORTED 1 +#define CONFIG_SOC_ADC_PERIPH_NUM 2 +#define CONFIG_SOC_ADC_MAX_CHANNEL_NUM 10 +#define CONFIG_SOC_ADC_ATTEN_NUM 4 +#define CONFIG_SOC_ADC_DIGI_CONTROLLER_NUM 2 +#define CONFIG_SOC_ADC_PATT_LEN_MAX 16 +#define CONFIG_SOC_ADC_DIGI_MIN_BITWIDTH 9 +#define CONFIG_SOC_ADC_DIGI_MAX_BITWIDTH 12 +#define CONFIG_SOC_ADC_DIGI_RESULT_BYTES 2 +#define CONFIG_SOC_ADC_DIGI_DATA_BYTES_PER_CONV 4 +#define CONFIG_SOC_ADC_DIGI_MONITOR_NUM 0 +#define CONFIG_SOC_ADC_SAMPLE_FREQ_THRES_HIGH 2 +#define CONFIG_SOC_ADC_SAMPLE_FREQ_THRES_LOW 20 +#define CONFIG_SOC_ADC_RTC_MIN_BITWIDTH 9 +#define CONFIG_SOC_ADC_RTC_MAX_BITWIDTH 12 +#define CONFIG_SOC_ADC_SHARED_POWER 1 +#define CONFIG_SOC_SHARED_IDCACHE_SUPPORTED 1 +#define CONFIG_SOC_IDCACHE_PER_CORE 1 +#define CONFIG_SOC_CPU_CORES_NUM 2 +#define CONFIG_SOC_CPU_INTR_NUM 32 +#define CONFIG_SOC_CPU_HAS_FPU 1 +#define CONFIG_SOC_HP_CPU_HAS_MULTIPLE_CORES 1 +#define CONFIG_SOC_CPU_BREAKPOINTS_NUM 2 +#define CONFIG_SOC_CPU_WATCHPOINTS_NUM 2 +#define CONFIG_SOC_CPU_WATCHPOINT_MAX_REGION_SIZE 64 +#define CONFIG_SOC_DAC_CHAN_NUM 2 +#define CONFIG_SOC_DAC_RESOLUTION 8 +#define CONFIG_SOC_DAC_DMA_16BIT_ALIGN 1 +#define CONFIG_SOC_GPIO_PORT 1 +#define CONFIG_SOC_GPIO_PIN_COUNT 40 +#define CONFIG_SOC_GPIO_VALID_GPIO_MASK 0xFFFFFFFFFF +#define CONFIG_SOC_GPIO_IN_RANGE_MAX 39 +#define CONFIG_SOC_GPIO_OUT_RANGE_MAX 33 +#define CONFIG_SOC_GPIO_VALID_DIGITAL_IO_PAD_MASK 0xEF0FEA +#define CONFIG_SOC_GPIO_CLOCKOUT_BY_IO_MUX 1 +#define CONFIG_SOC_GPIO_CLOCKOUT_CHANNEL_NUM 3 +#define CONFIG_SOC_GPIO_SUPPORT_HOLD_IO_IN_DSLP 1 +#define CONFIG_SOC_I2C_NUM 2 +#define CONFIG_SOC_HP_I2C_NUM 2 +#define CONFIG_SOC_I2C_FIFO_LEN 32 +#define CONFIG_SOC_I2C_CMD_REG_NUM 16 +#define CONFIG_SOC_I2C_SUPPORT_SLAVE 1 +#define CONFIG_SOC_I2C_SUPPORT_APB 1 +#define CONFIG_SOC_I2C_SUPPORT_10BIT_ADDR 1 +#define CONFIG_SOC_I2C_STOP_INDEPENDENT 1 +#define CONFIG_SOC_I2S_NUM 2 +#define CONFIG_SOC_I2S_HW_VERSION_1 1 +#define CONFIG_SOC_I2S_SUPPORTS_APLL 1 +#define CONFIG_SOC_I2S_SUPPORTS_PLL_F160M 1 +#define CONFIG_SOC_I2S_SUPPORTS_PDM 1 +#define CONFIG_SOC_I2S_SUPPORTS_PDM_TX 1 +#define CONFIG_SOC_I2S_SUPPORTS_PCM2PDM 1 +#define CONFIG_SOC_I2S_SUPPORTS_PDM_RX 1 +#define CONFIG_SOC_I2S_SUPPORTS_PDM2PCM 1 +#define CONFIG_SOC_I2S_PDM_MAX_TX_LINES 1 +#define CONFIG_SOC_I2S_PDM_MAX_RX_LINES 1 +#define CONFIG_SOC_I2S_SUPPORTS_ADC_DAC 1 +#define CONFIG_SOC_I2S_SUPPORTS_ADC 1 +#define CONFIG_SOC_I2S_SUPPORTS_DAC 1 +#define CONFIG_SOC_I2S_SUPPORTS_LCD_CAMERA 1 +#define CONFIG_SOC_I2S_MAX_DATA_WIDTH 24 +#define CONFIG_SOC_I2S_TRANS_SIZE_ALIGN_WORD 1 +#define CONFIG_SOC_I2S_LCD_I80_VARIANT 1 +#define CONFIG_SOC_LCD_I80_SUPPORTED 1 +#define CONFIG_SOC_LCD_I80_BUSES 2 +#define CONFIG_SOC_LCD_I80_BUS_WIDTH 24 +#define CONFIG_SOC_LEDC_HAS_TIMER_SPECIFIC_MUX 1 +#define CONFIG_SOC_LEDC_SUPPORT_APB_CLOCK 1 +#define CONFIG_SOC_LEDC_SUPPORT_REF_TICK 1 +#define CONFIG_SOC_LEDC_SUPPORT_HS_MODE 1 +#define CONFIG_SOC_LEDC_TIMER_NUM 4 +#define CONFIG_SOC_LEDC_CHANNEL_NUM 8 +#define CONFIG_SOC_LEDC_TIMER_BIT_WIDTH 20 +#define CONFIG_SOC_MCPWM_GROUPS 2 +#define CONFIG_SOC_MCPWM_TIMERS_PER_GROUP 3 +#define CONFIG_SOC_MCPWM_OPERATORS_PER_GROUP 3 +#define CONFIG_SOC_MCPWM_COMPARATORS_PER_OPERATOR 2 +#define CONFIG_SOC_MCPWM_GENERATORS_PER_OPERATOR 2 +#define CONFIG_SOC_MCPWM_TRIGGERS_PER_OPERATOR 2 +#define CONFIG_SOC_MCPWM_GPIO_FAULTS_PER_GROUP 3 +#define CONFIG_SOC_MCPWM_CAPTURE_TIMERS_PER_GROUP 1 +#define CONFIG_SOC_MCPWM_CAPTURE_CHANNELS_PER_TIMER 3 +#define CONFIG_SOC_MCPWM_GPIO_SYNCHROS_PER_GROUP 3 +#define CONFIG_SOC_MMU_PERIPH_NUM 2 +#define CONFIG_SOC_MMU_LINEAR_ADDRESS_REGION_NUM 3 +#define CONFIG_SOC_MPU_MIN_REGION_SIZE 0x20000000 +#define CONFIG_SOC_MPU_REGIONS_MAX_NUM 8 +#define CONFIG_SOC_PCNT_GROUPS 1 +#define CONFIG_SOC_PCNT_UNITS_PER_GROUP 8 +#define CONFIG_SOC_PCNT_CHANNELS_PER_UNIT 2 +#define CONFIG_SOC_PCNT_THRES_POINT_PER_UNIT 2 +#define CONFIG_SOC_RMT_GROUPS 1 +#define CONFIG_SOC_RMT_TX_CANDIDATES_PER_GROUP 8 +#define CONFIG_SOC_RMT_RX_CANDIDATES_PER_GROUP 8 +#define CONFIG_SOC_RMT_CHANNELS_PER_GROUP 8 +#define CONFIG_SOC_RMT_MEM_WORDS_PER_CHANNEL 64 +#define CONFIG_SOC_RMT_SUPPORT_REF_TICK 1 +#define CONFIG_SOC_RMT_SUPPORT_APB 1 +#define CONFIG_SOC_RMT_CHANNEL_CLK_INDEPENDENT 1 +#define CONFIG_SOC_RTCIO_PIN_COUNT 18 +#define CONFIG_SOC_RTCIO_INPUT_OUTPUT_SUPPORTED 1 +#define CONFIG_SOC_RTCIO_HOLD_SUPPORTED 1 +#define CONFIG_SOC_RTCIO_WAKE_SUPPORTED 1 +#define CONFIG_SOC_SDM_GROUPS 1 +#define CONFIG_SOC_SDM_CHANNELS_PER_GROUP 8 +#define CONFIG_SOC_SDM_CLK_SUPPORT_APB 1 +#define CONFIG_SOC_SPI_HD_BOTH_INOUT_SUPPORTED 1 +#define CONFIG_SOC_SPI_AS_CS_SUPPORTED 1 +#define CONFIG_SOC_SPI_PERIPH_NUM 3 +#define CONFIG_SOC_SPI_DMA_CHAN_NUM 2 +#define CONFIG_SOC_SPI_MAX_CS_NUM 3 +#define CONFIG_SOC_SPI_SUPPORT_CLK_APB 1 +#define CONFIG_SOC_SPI_MAXIMUM_BUFFER_SIZE 64 +#define CONFIG_SOC_SPI_MAX_PRE_DIVIDER 8192 +#define CONFIG_SOC_MEMSPI_SRC_FREQ_80M_SUPPORTED 1 +#define CONFIG_SOC_MEMSPI_SRC_FREQ_40M_SUPPORTED 1 +#define CONFIG_SOC_MEMSPI_SRC_FREQ_26M_SUPPORTED 1 +#define CONFIG_SOC_MEMSPI_SRC_FREQ_20M_SUPPORTED 1 +#define CONFIG_SOC_TIMER_GROUPS 2 +#define CONFIG_SOC_TIMER_GROUP_TIMERS_PER_GROUP 2 +#define CONFIG_SOC_TIMER_GROUP_COUNTER_BIT_WIDTH 64 +#define CONFIG_SOC_TIMER_GROUP_TOTAL_TIMERS 4 +#define CONFIG_SOC_TIMER_GROUP_SUPPORT_APB 1 +#define CONFIG_SOC_LP_TIMER_BIT_WIDTH_LO 32 +#define CONFIG_SOC_LP_TIMER_BIT_WIDTH_HI 16 +#define CONFIG_SOC_TOUCH_SENSOR_VERSION 1 +#define CONFIG_SOC_TOUCH_SENSOR_NUM 10 +#define CONFIG_SOC_TOUCH_SUPPORT_SLEEP_WAKEUP 1 +#define CONFIG_SOC_TOUCH_SAMPLE_CFG_NUM 1 +#define CONFIG_SOC_TWAI_CONTROLLER_NUM 1 +#define CONFIG_SOC_TWAI_BRP_MIN 2 +#define CONFIG_SOC_TWAI_CLK_SUPPORT_APB 1 +#define CONFIG_SOC_TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT 1 +#define CONFIG_SOC_UART_NUM 3 +#define CONFIG_SOC_UART_HP_NUM 3 +#define CONFIG_SOC_UART_SUPPORT_APB_CLK 1 +#define CONFIG_SOC_UART_SUPPORT_REF_TICK 1 +#define CONFIG_SOC_UART_FIFO_LEN 128 +#define CONFIG_SOC_UART_BITRATE_MAX 5000000 +#define CONFIG_SOC_UART_WAKEUP_SUPPORT_ACTIVE_THRESH_MODE 1 +#define CONFIG_SOC_SPIRAM_SUPPORTED 1 +#define CONFIG_SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE 1 +#define CONFIG_SOC_SHA_SUPPORT_PARALLEL_ENG 1 +#define CONFIG_SOC_SHA_ENDIANNESS_BE 1 +#define CONFIG_SOC_SHA_SUPPORT_SHA1 1 +#define CONFIG_SOC_SHA_SUPPORT_SHA256 1 +#define CONFIG_SOC_SHA_SUPPORT_SHA384 1 +#define CONFIG_SOC_SHA_SUPPORT_SHA512 1 +#define CONFIG_SOC_MPI_MEM_BLOCKS_NUM 4 +#define CONFIG_SOC_MPI_OPERATIONS_NUM 1 +#define CONFIG_SOC_RSA_MAX_BIT_LEN 4096 +#define CONFIG_SOC_AES_SUPPORT_AES_128 1 +#define CONFIG_SOC_AES_SUPPORT_AES_192 1 +#define CONFIG_SOC_AES_SUPPORT_AES_256 1 +#define CONFIG_SOC_SECURE_BOOT_V1 1 +#define CONFIG_SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS 1 +#define CONFIG_SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX 32 +#define CONFIG_SOC_PHY_DIG_REGS_MEM_SIZE 21 +#define CONFIG_SOC_PM_SUPPORT_EXT0_WAKEUP 1 +#define CONFIG_SOC_PM_SUPPORT_EXT1_WAKEUP 1 +#define CONFIG_SOC_PM_SUPPORT_EXT_WAKEUP 1 +#define CONFIG_SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP 1 +#define CONFIG_SOC_PM_SUPPORT_RTC_PERIPH_PD 1 +#define CONFIG_SOC_PM_SUPPORT_RTC_FAST_MEM_PD 1 +#define CONFIG_SOC_PM_SUPPORT_RTC_SLOW_MEM_PD 1 +#define CONFIG_SOC_PM_SUPPORT_RC_FAST_PD 1 +#define CONFIG_SOC_PM_SUPPORT_VDDSDIO_PD 1 +#define CONFIG_SOC_PM_SUPPORT_MODEM_PD 1 +#define CONFIG_SOC_CONFIGURABLE_VDDSDIO_SUPPORTED 1 +#define CONFIG_SOC_PM_MODEM_PD_BY_SW 1 +#define CONFIG_SOC_CLK_APLL_SUPPORTED 1 +#define CONFIG_SOC_CLK_RC_FAST_D256_SUPPORTED 1 +#define CONFIG_SOC_RTC_SLOW_CLK_SUPPORT_RC_FAST_D256 1 +#define CONFIG_SOC_CLK_RC_FAST_SUPPORT_CALIBRATION 1 +#define CONFIG_SOC_CLK_XTAL32K_SUPPORTED 1 +#define CONFIG_SOC_CLK_LP_FAST_SUPPORT_XTAL_D4 1 +#define CONFIG_SOC_SDMMC_USE_IOMUX 1 +#define CONFIG_SOC_SDMMC_NUM_SLOTS 2 +#define CONFIG_SOC_WIFI_WAPI_SUPPORT 1 +#define CONFIG_SOC_WIFI_CSI_SUPPORT 1 +#define CONFIG_SOC_WIFI_MESH_SUPPORT 1 +#define CONFIG_SOC_WIFI_SUPPORT_VARIABLE_BEACON_WINDOW 1 +#define CONFIG_SOC_WIFI_NAN_SUPPORT 1 +#define CONFIG_SOC_BLE_SUPPORTED 1 +#define CONFIG_SOC_BLE_MESH_SUPPORTED 1 +#define CONFIG_SOC_BT_CLASSIC_SUPPORTED 1 +#define CONFIG_SOC_BLUFI_SUPPORTED 1 +#define CONFIG_SOC_BT_H2C_ENC_KEY_CTRL_ENH_VSC_SUPPORTED 1 +#define CONFIG_SOC_ULP_HAS_ADC 1 +#define CONFIG_SOC_PHY_COMBO_MODULE 1 +#define CONFIG_SOC_EMAC_RMII_CLK_OUT_INTERNAL_LOOPBACK 1 +#define CONFIG_IDF_CMAKE 1 +#define CONFIG_IDF_TOOLCHAIN "gcc" +#define CONFIG_IDF_TOOLCHAIN_GCC 1 +#define CONFIG_IDF_TARGET_ARCH_XTENSA 1 +#define CONFIG_IDF_TARGET_ARCH "xtensa" +#define CONFIG_IDF_TARGET "esp32" +#define CONFIG_IDF_INIT_VERSION "5.5.0" +#define CONFIG_IDF_TARGET_ESP32 1 +#define CONFIG_IDF_FIRMWARE_CHIP_ID 0x0000 +#define CONFIG_APP_BUILD_TYPE_APP_2NDBOOT 1 +#define CONFIG_APP_BUILD_GENERATE_BINARIES 1 +#define CONFIG_APP_BUILD_BOOTLOADER 1 +#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1 +#define CONFIG_BOOTLOADER_COMPILE_TIME_DATE 1 +#define CONFIG_BOOTLOADER_PROJECT_VER 1 +#define CONFIG_BOOTLOADER_OFFSET_IN_FLASH 0x1000 +#define CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE 1 +#define CONFIG_BOOTLOADER_LOG_VERSION_1 1 +#define CONFIG_BOOTLOADER_LOG_VERSION 1 +#define CONFIG_BOOTLOADER_LOG_LEVEL_INFO 1 +#define CONFIG_BOOTLOADER_LOG_LEVEL 3 +#define CONFIG_BOOTLOADER_LOG_TIMESTAMP_SOURCE_CPU_TICKS 1 +#define CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT 1 +#define CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V 1 +#define CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE 1 +#define CONFIG_BOOTLOADER_WDT_ENABLE 1 +#define CONFIG_BOOTLOADER_WDT_TIME_MS 9000 +#define CONFIG_BOOTLOADER_RESERVE_RTC_SIZE 0x0 +#define CONFIG_SECURE_BOOT_V1_SUPPORTED 1 +#define CONFIG_APP_COMPILE_TIME_DATE 1 +#define CONFIG_APP_RETRIEVE_LEN_ELF_SHA 9 +#define CONFIG_ESP_ROM_HAS_CRC_LE 1 +#define CONFIG_ESP_ROM_HAS_CRC_BE 1 +#define CONFIG_ESP_ROM_HAS_MZ_CRC32 1 +#define CONFIG_ESP_ROM_HAS_JPEG_DECODE 1 +#define CONFIG_ESP_ROM_HAS_UART_BUF_SWITCH 1 +#define CONFIG_ESP_ROM_NEEDS_SWSETUP_WORKAROUND 1 +#define CONFIG_ESP_ROM_HAS_NEWLIB 1 +#define CONFIG_ESP_ROM_HAS_NEWLIB_NANO_FORMAT 1 +#define CONFIG_ESP_ROM_HAS_NEWLIB_32BIT_TIME 1 +#define CONFIG_ESP_ROM_HAS_SW_FLOAT 1 +#define CONFIG_ESP_ROM_USB_OTG_NUM -1 +#define CONFIG_ESP_ROM_USB_SERIAL_DEVICE_NUM -1 +#define CONFIG_ESP_ROM_SUPPORT_DEEP_SLEEP_WAKEUP_STUB 1 +#define CONFIG_ESP_ROM_HAS_OUTPUT_PUTC_FUNC 1 +#define CONFIG_ESPTOOLPY_FLASHMODE_DIO 1 +#define CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR 1 +#define CONFIG_ESPTOOLPY_FLASHMODE "dio" +#define CONFIG_ESPTOOLPY_FLASHFREQ_40M 1 +#define CONFIG_ESPTOOLPY_FLASHFREQ "40m" +#define CONFIG_ESPTOOLPY_FLASHSIZE_2MB 1 +#define CONFIG_ESPTOOLPY_FLASHSIZE "2MB" +#define CONFIG_ESPTOOLPY_BEFORE_RESET 1 +#define CONFIG_ESPTOOLPY_BEFORE "default_reset" +#define CONFIG_ESPTOOLPY_AFTER_RESET 1 +#define CONFIG_ESPTOOLPY_AFTER "hard_reset" +#define CONFIG_ESPTOOLPY_MONITOR_BAUD 115200 +#define CONFIG_PARTITION_TABLE_SINGLE_APP 1 +#define CONFIG_PARTITION_TABLE_CUSTOM_FILENAME "partitions.csv" +#define CONFIG_PARTITION_TABLE_FILENAME "partitions_singleapp.csv" +#define CONFIG_PARTITION_TABLE_OFFSET 0x8000 +#define CONFIG_PARTITION_TABLE_MD5 1 +#define CONFIG_COMPILER_OPTIMIZATION_DEBUG 1 +#define CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE 1 +#define CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE 1 +#define CONFIG_COMPILER_FLOAT_LIB_FROM_GCCLIB 1 +#define CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL 2 +#define CONFIG_COMPILER_HIDE_PATHS_MACROS 1 +#define CONFIG_COMPILER_STACK_CHECK_MODE_NONE 1 +#define CONFIG_COMPILER_DISABLE_DEFAULT_ERRORS 1 +#define CONFIG_COMPILER_RT_LIB_GCCLIB 1 +#define CONFIG_COMPILER_RT_LIB_NAME "gcc" +#define CONFIG_COMPILER_ORPHAN_SECTIONS_WARNING 1 +#define CONFIG_APPTRACE_DEST_NONE 1 +#define CONFIG_APPTRACE_DEST_UART_NONE 1 +#define CONFIG_APPTRACE_UART_TASK_PRIO 1 +#define CONFIG_APPTRACE_LOCK_ENABLE 1 +#define CONFIG_TWAI_ERRATA_FIX_BUS_OFF_REC 1 +#define CONFIG_TWAI_ERRATA_FIX_TX_INTR_LOST 1 +#define CONFIG_TWAI_ERRATA_FIX_RX_FRAME_INVALID 1 +#define CONFIG_TWAI_ERRATA_FIX_RX_FIFO_CORRUPT 1 +#define CONFIG_TWAI_ERRATA_FIX_LISTEN_ONLY_DOM 1 +#define CONFIG_ADC_DISABLE_DAC 1 +#define CONFIG_ADC_CAL_EFUSE_TP_ENABLE 1 +#define CONFIG_ADC_CAL_EFUSE_VREF_ENABLE 1 +#define CONFIG_ADC_CAL_LUT_ENABLE 1 +#define CONFIG_EFUSE_CODE_SCHEME_COMPAT_3_4 1 +#define CONFIG_EFUSE_MAX_BLK_LEN 192 +#define CONFIG_ESP_TLS_USING_MBEDTLS 1 +#define CONFIG_ADC_CALI_EFUSE_TP_ENABLE 1 +#define CONFIG_ADC_CALI_EFUSE_VREF_ENABLE 1 +#define CONFIG_ADC_CALI_LUT_ENABLE 1 +#define CONFIG_ADC_DISABLE_DAC_OUTPUT 1 +#define CONFIG_ESP_COEX_ENABLED 1 +#define CONFIG_ESP_ERR_TO_NAME_LOOKUP 1 +#define CONFIG_DAC_DMA_AUTO_16BIT_ALIGN 1 +#define CONFIG_GPTIMER_ISR_HANDLER_IN_IRAM 1 +#define CONFIG_GPTIMER_OBJ_CACHE_SAFE 1 +#define CONFIG_I2C_MASTER_ISR_HANDLER_IN_IRAM 1 +#define CONFIG_RMT_ISR_HANDLER_IN_IRAM 1 +#define CONFIG_RMT_OBJ_CACHE_SAFE 1 +#define CONFIG_SPI_MASTER_ISR_IN_IRAM 1 +#define CONFIG_SPI_SLAVE_ISR_IN_IRAM 1 +#define CONFIG_ETH_ENABLED 1 +#define CONFIG_ETH_USE_ESP32_EMAC 1 +#define CONFIG_ETH_PHY_INTERFACE_RMII 1 +#define CONFIG_ETH_RMII_CLK_INPUT 1 +#define CONFIG_ETH_RMII_CLK_IN_GPIO 0 +#define CONFIG_ETH_DMA_BUFFER_SIZE 512 +#define CONFIG_ETH_DMA_RX_BUFFER_NUM 10 +#define CONFIG_ETH_DMA_TX_BUFFER_NUM 10 +#define CONFIG_ETH_USE_SPI_ETHERNET 1 +#define CONFIG_ESP_EVENT_POST_FROM_ISR 1 +#define CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR 1 +#define CONFIG_ESP_GDBSTUB_ENABLED 1 +#define CONFIG_ESP_GDBSTUB_SUPPORT_TASKS 1 +#define CONFIG_ESP_GDBSTUB_MAX_TASKS 32 +#define CONFIG_ESPHID_TASK_SIZE_BT 2048 +#define CONFIG_ESPHID_TASK_SIZE_BLE 4096 +#define CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS 1 +#define CONFIG_ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT 2000 +#define CONFIG_HTTPD_MAX_REQ_HDR_LEN 1024 +#define CONFIG_HTTPD_MAX_URI_LEN 512 +#define CONFIG_HTTPD_ERR_RESP_NO_DELAY 1 +#define CONFIG_HTTPD_PURGE_BUF_LEN 32 +#define CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT 2000 +#define CONFIG_ESP_HTTPS_OTA_EVENT_POST_TIMEOUT 2000 +#define CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT 2000 +#define CONFIG_ESP32_REV_MIN_0 1 +#define CONFIG_ESP32_REV_MIN 0 +#define CONFIG_ESP32_REV_MIN_FULL 0 +#define CONFIG_ESP_REV_MIN_FULL 0 +#define CONFIG_ESP32_REV_MAX_FULL 399 +#define CONFIG_ESP_REV_MAX_FULL 399 +#define CONFIG_ESP_EFUSE_BLOCK_REV_MIN_FULL 0 +#define CONFIG_ESP_EFUSE_BLOCK_REV_MAX_FULL 99 +#define CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA 1 +#define CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP 1 +#define CONFIG_ESP_MAC_ADDR_UNIVERSE_BT 1 +#define CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH 1 +#define CONFIG_ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR 1 +#define CONFIG_ESP_MAC_UNIVERSAL_MAC_ADDRESSES 4 +#define CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR 1 +#define CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES 4 +#define CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND 1 +#define CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND 1 +#define CONFIG_ESP_SLEEP_WAIT_FLASH_READY_EXTRA_DELAY 2000 +#define CONFIG_ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS 1 +#define CONFIG_RTC_CLK_SRC_INT_RC 1 +#define CONFIG_RTC_CLK_CAL_CYCLES 1024 +#define CONFIG_PERIPH_CTRL_FUNC_IN_IRAM 1 +#define CONFIG_XTAL_FREQ_40 1 +#define CONFIG_XTAL_FREQ 40 +#define CONFIG_ESP_BROWNOUT_DET 1 +#define CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0 1 +#define CONFIG_ESP_BROWNOUT_DET_LVL 0 +#define CONFIG_ESP_BROWNOUT_USE_INTR 1 +#define CONFIG_ESP_SPI_BUS_LOCK_ISR_FUNCS_IN_IRAM 1 +#define CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL 120 +#define CONFIG_ESP_NETIF_TCPIP_LWIP 1 +#define CONFIG_ESP_NETIF_USES_TCPIP_WITH_BSD_API 1 +#define CONFIG_ESP_NETIF_REPORT_DATA_TRAFFIC 1 +#define CONFIG_ESP_PHY_ENABLED 1 +#define CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE 1 +#define CONFIG_ESP_PHY_MAX_WIFI_TX_POWER 20 +#define CONFIG_ESP_PHY_MAX_TX_POWER 20 +#define CONFIG_ESP_PHY_RF_CAL_PARTIAL 1 +#define CONFIG_ESP_PHY_CALIBRATION_MODE 0 +#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160 1 +#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ 160 +#define CONFIG_ESP32_TRACEMEM_RESERVE_DRAM 0x0 +#define CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT 1 +#define CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS 0 +#define CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE 32 +#define CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE 2304 +#define CONFIG_ESP_MAIN_TASK_STACK_SIZE 3584 +#define CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0 1 +#define CONFIG_ESP_MAIN_TASK_AFFINITY 0x0 +#define CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE 2048 +#define CONFIG_ESP_CONSOLE_UART_DEFAULT 1 +#define CONFIG_ESP_CONSOLE_UART 1 +#define CONFIG_ESP_CONSOLE_UART_NUM 0 +#define CONFIG_ESP_CONSOLE_ROM_SERIAL_PORT_NUM 0 +#define CONFIG_ESP_CONSOLE_UART_BAUDRATE 115200 +#define CONFIG_ESP_INT_WDT 1 +#define CONFIG_ESP_INT_WDT_TIMEOUT_MS 300 +#define CONFIG_ESP_INT_WDT_CHECK_CPU1 1 +#define CONFIG_ESP_TASK_WDT_EN 1 +#define CONFIG_ESP_TASK_WDT_INIT 1 +#define CONFIG_ESP_TASK_WDT_TIMEOUT_S 5 +#define CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 1 +#define CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 1 +#define CONFIG_ESP_DEBUG_OCDAWARE 1 +#define CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_4 1 +#define CONFIG_ESP_IPC_TASK_STACK_SIZE 1024 +#define CONFIG_ESP_IPC_USES_CALLERS_PRIORITY 1 +#define CONFIG_ESP_IPC_ISR_ENABLE 1 +#define CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER 1 +#define CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER 1 +#define CONFIG_ESP_TIMER_TASK_STACK_SIZE 3584 +#define CONFIG_ESP_TIMER_INTERRUPT_LEVEL 1 +#define CONFIG_ESP_TIMER_TASK_AFFINITY 0x0 +#define CONFIG_ESP_TIMER_TASK_AFFINITY_CPU0 1 +#define CONFIG_ESP_TIMER_ISR_AFFINITY_CPU0 1 +#define CONFIG_ESP_TIMER_IMPL_TG0_LAC 1 +#define CONFIG_ESP_WIFI_ENABLED 1 +#define CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM 10 +#define CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM 32 +#define CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER 1 +#define CONFIG_ESP_WIFI_TX_BUFFER_TYPE 1 +#define CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM 32 +#define CONFIG_ESP_WIFI_STATIC_RX_MGMT_BUFFER 1 +#define CONFIG_ESP_WIFI_DYNAMIC_RX_MGMT_BUF 0 +#define CONFIG_ESP_WIFI_RX_MGMT_BUF_NUM_DEF 5 +#define CONFIG_ESP_WIFI_AMPDU_TX_ENABLED 1 +#define CONFIG_ESP_WIFI_TX_BA_WIN 6 +#define CONFIG_ESP_WIFI_AMPDU_RX_ENABLED 1 +#define CONFIG_ESP_WIFI_RX_BA_WIN 6 +#define CONFIG_ESP_WIFI_NVS_ENABLED 1 +#define CONFIG_ESP_WIFI_TASK_PINNED_TO_CORE_0 1 +#define CONFIG_ESP_WIFI_SOFTAP_BEACON_MAX_LEN 752 +#define CONFIG_ESP_WIFI_MGMT_SBUF_NUM 32 +#define CONFIG_ESP_WIFI_IRAM_OPT 1 +#define CONFIG_ESP_WIFI_RX_IRAM_OPT 1 +#define CONFIG_ESP_WIFI_ENABLE_WPA3_SAE 1 +#define CONFIG_ESP_WIFI_ENABLE_SAE_PK 1 +#define CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT 1 +#define CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA 1 +#define CONFIG_ESP_WIFI_SLP_DEFAULT_MIN_ACTIVE_TIME 50 +#define CONFIG_ESP_WIFI_SLP_DEFAULT_MAX_ACTIVE_TIME 10 +#define CONFIG_ESP_WIFI_SLP_DEFAULT_WAIT_BROADCAST_DATA_TIME 15 +#define CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE 1 +#define CONFIG_ESP_WIFI_GMAC_SUPPORT 1 +#define CONFIG_ESP_WIFI_SOFTAP_SUPPORT 1 +#define CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM 7 +#define CONFIG_ESP_WIFI_MBEDTLS_CRYPTO 1 +#define CONFIG_ESP_WIFI_MBEDTLS_TLS_CLIENT 1 +#define CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT 1 +#define CONFIG_ESP_COREDUMP_ENABLE_TO_NONE 1 +#define CONFIG_FATFS_VOLUME_COUNT 2 +#define CONFIG_FATFS_LFN_NONE 1 +#define CONFIG_FATFS_SECTOR_4096 1 +#define CONFIG_FATFS_CODEPAGE_437 1 +#define CONFIG_FATFS_CODEPAGE 437 +#define CONFIG_FATFS_FS_LOCK 0 +#define CONFIG_FATFS_TIMEOUT_MS 10000 +#define CONFIG_FATFS_PER_FILE_CACHE 1 +#define CONFIG_FATFS_USE_STRFUNC_NONE 1 +#define CONFIG_FATFS_VFS_FSTAT_BLKSIZE 0 +#define CONFIG_FATFS_LINK_LOCK 1 +#define CONFIG_FREERTOS_HZ 100 +#define CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY 1 +#define CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS 1 +#define CONFIG_FREERTOS_IDLE_TASK_STACKSIZE 1536 +#define CONFIG_FREERTOS_MAX_TASK_NAME_LEN 16 +#define CONFIG_FREERTOS_USE_TIMERS 1 +#define CONFIG_FREERTOS_TIMER_SERVICE_TASK_NAME "Tmr Svc" +#define CONFIG_FREERTOS_TIMER_TASK_NO_AFFINITY 1 +#define CONFIG_FREERTOS_TIMER_SERVICE_TASK_CORE_AFFINITY 0x7FFFFFFF +#define CONFIG_FREERTOS_TIMER_TASK_PRIORITY 1 +#define CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH 2048 +#define CONFIG_FREERTOS_TIMER_QUEUE_LENGTH 10 +#define CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE 0 +#define CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES 1 +#define CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER 1 +#define CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS 1 +#define CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER 1 +#define CONFIG_FREERTOS_ISR_STACKSIZE 1536 +#define CONFIG_FREERTOS_INTERRUPT_BACKTRACE 1 +#define CONFIG_FREERTOS_TICK_SUPPORT_CORETIMER 1 +#define CONFIG_FREERTOS_CORETIMER_0 1 +#define CONFIG_FREERTOS_SYSTICK_USES_CCOUNT 1 +#define CONFIG_FREERTOS_PORT 1 +#define CONFIG_FREERTOS_NO_AFFINITY 0x7FFFFFFF +#define CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION 1 +#define CONFIG_FREERTOS_DEBUG_OCDAWARE 1 +#define CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT 1 +#define CONFIG_FREERTOS_PLACE_SNAPSHOT_FUNS_INTO_FLASH 1 +#define CONFIG_FREERTOS_NUMBER_OF_CORES 2 +#define CONFIG_HAL_ASSERTION_EQUALS_SYSTEM 1 +#define CONFIG_HAL_DEFAULT_ASSERTION_LEVEL 2 +#define CONFIG_HEAP_POISONING_DISABLED 1 +#define CONFIG_HEAP_TRACING_OFF 1 +#define CONFIG_LOG_VERSION_1 1 +#define CONFIG_LOG_VERSION 1 +#define CONFIG_LOG_DEFAULT_LEVEL_INFO 1 +#define CONFIG_LOG_DEFAULT_LEVEL 3 +#define CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT 1 +#define CONFIG_LOG_MAXIMUM_LEVEL 3 +#define CONFIG_LOG_DYNAMIC_LEVEL_CONTROL 1 +#define CONFIG_LOG_TAG_LEVEL_IMPL_CACHE_AND_LINKED_LIST 1 +#define CONFIG_LOG_TAG_LEVEL_CACHE_BINARY_MIN_HEAP 1 +#define CONFIG_LOG_TAG_LEVEL_IMPL_CACHE_SIZE 31 +#define CONFIG_LOG_TIMESTAMP_SOURCE_RTOS 1 +#define CONFIG_LWIP_ENABLE 1 +#define CONFIG_LWIP_LOCAL_HOSTNAME "espressif" +#define CONFIG_LWIP_TCPIP_TASK_PRIO 18 +#define CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES 1 +#define CONFIG_LWIP_TIMERS_ONDEMAND 1 +#define CONFIG_LWIP_ND6 1 +#define CONFIG_LWIP_MAX_SOCKETS 10 +#define CONFIG_LWIP_SO_REUSE 1 +#define CONFIG_LWIP_SO_REUSE_RXTOALL 1 +#define CONFIG_LWIP_IP_DEFAULT_TTL 64 +#define CONFIG_LWIP_IP4_FRAG 1 +#define CONFIG_LWIP_IP6_FRAG 1 +#define CONFIG_LWIP_IP_REASS_MAX_PBUFS 10 +#define CONFIG_LWIP_ESP_GRATUITOUS_ARP 1 +#define CONFIG_LWIP_GARP_TMR_INTERVAL 60 +#define CONFIG_LWIP_ESP_MLDV6_REPORT 1 +#define CONFIG_LWIP_MLDV6_TMR_INTERVAL 40 +#define CONFIG_LWIP_TCPIP_RECVMBOX_SIZE 32 +#define CONFIG_LWIP_DHCP_DOES_ARP_CHECK 1 +#define CONFIG_LWIP_DHCP_DISABLE_VENDOR_CLASS_ID 1 +#define CONFIG_LWIP_DHCP_OPTIONS_LEN 68 +#define CONFIG_LWIP_NUM_NETIF_CLIENT_DATA 0 +#define CONFIG_LWIP_DHCP_COARSE_TIMER_SECS 1 +#define CONFIG_LWIP_DHCPS 1 +#define CONFIG_LWIP_DHCPS_LEASE_UNIT 60 +#define CONFIG_LWIP_DHCPS_MAX_STATION_NUM 8 +#define CONFIG_LWIP_DHCPS_STATIC_ENTRIES 1 +#define CONFIG_LWIP_DHCPS_ADD_DNS 1 +#define CONFIG_LWIP_IPV4 1 +#define CONFIG_LWIP_IPV6 1 +#define CONFIG_LWIP_IPV6_NUM_ADDRESSES 3 +#define CONFIG_LWIP_NETIF_LOOPBACK 1 +#define CONFIG_LWIP_LOOPBACK_MAX_PBUFS 8 +#define CONFIG_LWIP_MAX_ACTIVE_TCP 16 +#define CONFIG_LWIP_MAX_LISTENING_TCP 16 +#define CONFIG_LWIP_TCP_HIGH_SPEED_RETRANSMISSION 1 +#define CONFIG_LWIP_TCP_MAXRTX 12 +#define CONFIG_LWIP_TCP_SYNMAXRTX 12 +#define CONFIG_LWIP_TCP_MSS 1440 +#define CONFIG_LWIP_TCP_TMR_INTERVAL 250 +#define CONFIG_LWIP_TCP_MSL 60000 +#define CONFIG_LWIP_TCP_FIN_WAIT_TIMEOUT 20000 +#define CONFIG_LWIP_TCP_SND_BUF_DEFAULT 5760 +#define CONFIG_LWIP_TCP_WND_DEFAULT 5760 +#define CONFIG_LWIP_TCP_RECVMBOX_SIZE 6 +#define CONFIG_LWIP_TCP_ACCEPTMBOX_SIZE 6 +#define CONFIG_LWIP_TCP_QUEUE_OOSEQ 1 +#define CONFIG_LWIP_TCP_OOSEQ_TIMEOUT 6 +#define CONFIG_LWIP_TCP_OOSEQ_MAX_PBUFS 4 +#define CONFIG_LWIP_TCP_OVERSIZE_MSS 1 +#define CONFIG_LWIP_TCP_RTO_TIME 1500 +#define CONFIG_LWIP_MAX_UDP_PCBS 16 +#define CONFIG_LWIP_UDP_RECVMBOX_SIZE 6 +#define CONFIG_LWIP_CHECKSUM_CHECK_ICMP 1 +#define CONFIG_LWIP_TCPIP_TASK_STACK_SIZE 3072 +#define CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY 1 +#define CONFIG_LWIP_TCPIP_TASK_AFFINITY 0x7FFFFFFF +#define CONFIG_LWIP_IPV6_MEMP_NUM_ND6_QUEUE 3 +#define CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS 5 +#define CONFIG_LWIP_IPV6_ND6_NUM_PREFIXES 5 +#define CONFIG_LWIP_IPV6_ND6_NUM_ROUTERS 3 +#define CONFIG_LWIP_IPV6_ND6_NUM_DESTINATIONS 10 +#define CONFIG_LWIP_ICMP 1 +#define CONFIG_LWIP_MAX_RAW_PCBS 16 +#define CONFIG_LWIP_SNTP_MAX_SERVERS 1 +#define CONFIG_LWIP_SNTP_UPDATE_DELAY 3600000 +#define CONFIG_LWIP_SNTP_STARTUP_DELAY 1 +#define CONFIG_LWIP_SNTP_MAXIMUM_STARTUP_DELAY 5000 +#define CONFIG_LWIP_DNS_MAX_HOST_IP 1 +#define CONFIG_LWIP_DNS_MAX_SERVERS 3 +#define CONFIG_LWIP_BRIDGEIF_MAX_PORTS 7 +#define CONFIG_LWIP_ESP_LWIP_ASSERT 1 +#define CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT 1 +#define CONFIG_LWIP_HOOK_IP6_ROUTE_NONE 1 +#define CONFIG_LWIP_HOOK_ND6_GET_GW_NONE 1 +#define CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_NONE 1 +#define CONFIG_LWIP_HOOK_DHCP_EXTRA_OPTION_NONE 1 +#define CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE 1 +#define CONFIG_LWIP_HOOK_DNS_EXT_RESOLVE_NONE 1 +#define CONFIG_LWIP_HOOK_IP6_INPUT_DEFAULT 1 +#define CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC 1 +#define CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN 1 +#define CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN 16384 +#define CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN 4096 +#define CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE 1 +#define CONFIG_MBEDTLS_PKCS7_C 1 +#define CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 1 +#define CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL 1 +#define CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS 200 +#define CONFIG_MBEDTLS_CMAC_C 1 +#define CONFIG_MBEDTLS_HARDWARE_AES 1 +#define CONFIG_MBEDTLS_GCM_SUPPORT_NON_AES_CIPHER 1 +#define CONFIG_MBEDTLS_HARDWARE_MPI 1 +#define CONFIG_MBEDTLS_HARDWARE_SHA 1 +#define CONFIG_MBEDTLS_ROM_MD5 1 +#define CONFIG_MBEDTLS_HAVE_TIME 1 +#define CONFIG_MBEDTLS_ECDSA_DETERMINISTIC 1 +#define CONFIG_MBEDTLS_SHA512_C 1 +#define CONFIG_MBEDTLS_TLS_SERVER_AND_CLIENT 1 +#define CONFIG_MBEDTLS_TLS_SERVER 1 +#define CONFIG_MBEDTLS_TLS_CLIENT 1 +#define CONFIG_MBEDTLS_TLS_ENABLED 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_RSA 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_ELLIPTIC_CURVE 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA 1 +#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_RSA 1 +#define CONFIG_MBEDTLS_SSL_RENEGOTIATION 1 +#define CONFIG_MBEDTLS_SSL_PROTO_TLS1_2 1 +#define CONFIG_MBEDTLS_SSL_ALPN 1 +#define CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS 1 +#define CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS 1 +#define CONFIG_MBEDTLS_AES_C 1 +#define CONFIG_MBEDTLS_CCM_C 1 +#define CONFIG_MBEDTLS_GCM_C 1 +#define CONFIG_MBEDTLS_PEM_PARSE_C 1 +#define CONFIG_MBEDTLS_PEM_WRITE_C 1 +#define CONFIG_MBEDTLS_X509_CRL_PARSE_C 1 +#define CONFIG_MBEDTLS_X509_CSR_PARSE_C 1 +#define CONFIG_MBEDTLS_ECP_C 1 +#define CONFIG_MBEDTLS_PK_PARSE_EC_EXTENDED 1 +#define CONFIG_MBEDTLS_PK_PARSE_EC_COMPRESSED 1 +#define CONFIG_MBEDTLS_ECDH_C 1 +#define CONFIG_MBEDTLS_ECDSA_C 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP192R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP224R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP384R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP521R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP192K1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP224K1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_SECP256K1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_BP256R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_BP384R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_BP512R1_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED 1 +#define CONFIG_MBEDTLS_ECP_NIST_OPTIM 1 +#define CONFIG_MBEDTLS_ERROR_STRINGS 1 +#define CONFIG_MBEDTLS_FS_IO 1 +#define CONFIG_MQTT_PROTOCOL_311 1 +#define CONFIG_MQTT_TRANSPORT_SSL 1 +#define CONFIG_MQTT_TRANSPORT_WEBSOCKET 1 +#define CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE 1 +#define CONFIG_LIBC_NEWLIB 1 +#define CONFIG_LIBC_STDOUT_LINE_ENDING_CRLF 1 +#define CONFIG_LIBC_STDIN_LINE_ENDING_CR 1 +#define CONFIG_LIBC_TIME_SYSCALL_USE_RTC_HRT 1 +#define CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0 1 +#define CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1 1 +#define CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2 1 +#define CONFIG_PTHREAD_TASK_PRIO_DEFAULT 5 +#define CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT 3072 +#define CONFIG_PTHREAD_STACK_MIN 768 +#define CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY 1 +#define CONFIG_PTHREAD_TASK_CORE_DEFAULT -1 +#define CONFIG_PTHREAD_TASK_NAME_DEFAULT "pthread" +#define CONFIG_MMU_PAGE_SIZE_64KB 1 +#define CONFIG_MMU_PAGE_MODE "64KB" +#define CONFIG_MMU_PAGE_SIZE 0x10000 +#define CONFIG_SPI_FLASH_BROWNOUT_RESET_XMC 1 +#define CONFIG_SPI_FLASH_BROWNOUT_RESET 1 +#define CONFIG_SPI_FLASH_SUSPEND_TSUS_VAL_US 50 +#define CONFIG_SPI_FLASH_PLACE_FUNCTIONS_IN_IRAM 1 +#define CONFIG_SPI_FLASH_ROM_DRIVER_PATCH 1 +#define CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS 1 +#define CONFIG_SPI_FLASH_YIELD_DURING_ERASE 1 +#define CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS 20 +#define CONFIG_SPI_FLASH_ERASE_YIELD_TICKS 1 +#define CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE 8192 +#define CONFIG_SPI_FLASH_VENDOR_XMC_SUPPORTED 1 +#define CONFIG_SPI_FLASH_VENDOR_GD_SUPPORTED 1 +#define CONFIG_SPI_FLASH_VENDOR_ISSI_SUPPORTED 1 +#define CONFIG_SPI_FLASH_VENDOR_MXIC_SUPPORTED 1 +#define CONFIG_SPI_FLASH_VENDOR_WINBOND_SUPPORTED 1 +#define CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP 1 +#define CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP 1 +#define CONFIG_SPI_FLASH_SUPPORT_GD_CHIP 1 +#define CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP 1 +#define CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE 1 +#define CONFIG_SPIFFS_MAX_PARTITIONS 3 +#define CONFIG_SPIFFS_CACHE 1 +#define CONFIG_SPIFFS_CACHE_WR 1 +#define CONFIG_SPIFFS_PAGE_CHECK 1 +#define CONFIG_SPIFFS_GC_MAX_RUNS 10 +#define CONFIG_SPIFFS_PAGE_SIZE 256 +#define CONFIG_SPIFFS_OBJ_NAME_LEN 32 +#define CONFIG_SPIFFS_USE_MAGIC 1 +#define CONFIG_SPIFFS_USE_MAGIC_LENGTH 1 +#define CONFIG_SPIFFS_META_LENGTH 4 +#define CONFIG_SPIFFS_USE_MTIME 1 +#define CONFIG_WS_TRANSPORT 1 +#define CONFIG_WS_BUFFER_SIZE 1024 +#define CONFIG_UNITY_ENABLE_FLOAT 1 +#define CONFIG_UNITY_ENABLE_DOUBLE 1 +#define CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER 1 +#define CONFIG_VFS_SUPPORT_IO 1 +#define CONFIG_VFS_SUPPORT_DIR 1 +#define CONFIG_VFS_SUPPORT_SELECT 1 +#define CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT 1 +#define CONFIG_VFS_SUPPORT_TERMIOS 1 +#define CONFIG_VFS_MAX_COUNT 8 +#define CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS 1 +#define CONFIG_VFS_INITIALIZE_DEV_NULL 1 +#define CONFIG_WL_SECTOR_SIZE_4096 1 +#define CONFIG_WL_SECTOR_SIZE 4096 +#define CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES 16 +#define CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT 30 +#define CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN 1 +#define CONFIG_MDNS_MAX_INTERFACES 3 +#define CONFIG_MDNS_MAX_SERVICES 10 +#define CONFIG_MDNS_TASK_PRIORITY 1 +#define CONFIG_MDNS_ACTION_QUEUE_LEN 16 +#define CONFIG_MDNS_TASK_STACK_SIZE 4096 +#define CONFIG_MDNS_TASK_AFFINITY_CPU0 1 +#define CONFIG_MDNS_TASK_AFFINITY 0x0 +#define CONFIG_MDNS_TASK_CREATE_FROM_INTERNAL 1 +#define CONFIG_MDNS_MEMORY_ALLOC_INTERNAL 1 +#define CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS 2000 +#define CONFIG_MDNS_TIMER_PERIOD_MS 100 +#define CONFIG_MDNS_ENABLE_CONSOLE_CLI 1 +#define CONFIG_MDNS_MULTIPLE_INSTANCE 1 + +/* List of deprecated options */ +#define CONFIG_ADC2_DISABLE_DAC CONFIG_ADC_DISABLE_DAC +#define CONFIG_BROWNOUT_DET CONFIG_ESP_BROWNOUT_DET +#define CONFIG_BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL +#define CONFIG_BROWNOUT_DET_LVL_SEL_0 CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0 +#define CONFIG_COMPILER_OPTIMIZATION_DEFAULT CONFIG_COMPILER_OPTIMIZATION_DEBUG +#define CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG CONFIG_COMPILER_OPTIMIZATION_DEBUG +#define CONFIG_CONSOLE_UART CONFIG_ESP_CONSOLE_UART +#define CONFIG_CONSOLE_UART_BAUDRATE CONFIG_ESP_CONSOLE_UART_BAUDRATE +#define CONFIG_CONSOLE_UART_DEFAULT CONFIG_ESP_CONSOLE_UART_DEFAULT +#define CONFIG_CONSOLE_UART_NUM CONFIG_ESP_CONSOLE_UART_NUM +#define CONFIG_ESP32_APPTRACE_DEST_NONE CONFIG_APPTRACE_DEST_NONE +#define CONFIG_ESP32_APPTRACE_LOCK_ENABLE CONFIG_APPTRACE_LOCK_ENABLE +#define CONFIG_ESP32_BROWNOUT_DET CONFIG_ESP_BROWNOUT_DET +#define CONFIG_ESP32_BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL +#define CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0 CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0 +#define CONFIG_ESP32_DEBUG_OCDAWARE CONFIG_ESP_DEBUG_OCDAWARE +#define CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP_SLEEP_WAIT_FLASH_READY_EXTRA_DELAY +#define CONFIG_ESP32_DEFAULT_CPU_FREQ_160 CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160 +#define CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ +#define CONFIG_ESP32_DEFAULT_PTHREAD_CORE_NO_AFFINITY CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY +#define CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE CONFIG_ESP_COREDUMP_ENABLE_TO_NONE +#define CONFIG_ESP32_PANIC_PRINT_REBOOT CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT +#define CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE +#define CONFIG_ESP32_PHY_MAX_TX_POWER CONFIG_ESP_PHY_MAX_TX_POWER +#define CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER CONFIG_ESP_PHY_MAX_WIFI_TX_POWER +#define CONFIG_ESP32_PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN +#define CONFIG_ESP32_PTHREAD_TASK_CORE_DEFAULT CONFIG_PTHREAD_TASK_CORE_DEFAULT +#define CONFIG_ESP32_PTHREAD_TASK_NAME_DEFAULT CONFIG_PTHREAD_TASK_NAME_DEFAULT +#define CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT CONFIG_PTHREAD_TASK_PRIO_DEFAULT +#define CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT +#define CONFIG_ESP32_RTC_CLK_CAL_CYCLES CONFIG_RTC_CLK_CAL_CYCLES +#define CONFIG_ESP32_RTC_CLK_SRC_INT_RC CONFIG_RTC_CLK_SRC_INT_RC +#define CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC CONFIG_RTC_CLK_SRC_INT_RC +#define CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 CONFIG_LIBC_TIME_SYSCALL_USE_RTC_HRT +#define CONFIG_ESP32_TIME_SYSCALL_USE_RTC_HRT CONFIG_LIBC_TIME_SYSCALL_USE_RTC_HRT +#define CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED CONFIG_ESP_WIFI_AMPDU_RX_ENABLED +#define CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED CONFIG_ESP_WIFI_AMPDU_TX_ENABLED +#define CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM +#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER +#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM +#define CONFIG_ESP32_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED +#define CONFIG_ESP32_WIFI_ENABLE_WPA3_OWE_STA CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA +#define CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE CONFIG_ESP_WIFI_ENABLE_WPA3_SAE +#define CONFIG_ESP32_WIFI_IRAM_OPT CONFIG_ESP_WIFI_IRAM_OPT +#define CONFIG_ESP32_WIFI_MGMT_SBUF_NUM CONFIG_ESP_WIFI_MGMT_SBUF_NUM +#define CONFIG_ESP32_WIFI_NVS_ENABLED CONFIG_ESP_WIFI_NVS_ENABLED +#define CONFIG_ESP32_WIFI_RX_BA_WIN CONFIG_ESP_WIFI_RX_BA_WIN +#define CONFIG_ESP32_WIFI_RX_IRAM_OPT CONFIG_ESP_WIFI_RX_IRAM_OPT +#define CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN CONFIG_ESP_WIFI_SOFTAP_BEACON_MAX_LEN +#define CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM +#define CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0 CONFIG_ESP_WIFI_TASK_PINNED_TO_CORE_0 +#define CONFIG_ESP32_WIFI_TX_BA_WIN CONFIG_ESP_WIFI_TX_BA_WIN +#define CONFIG_ESP32_WIFI_TX_BUFFER_TYPE CONFIG_ESP_WIFI_TX_BUFFER_TYPE +#define CONFIG_ESP32_XTAL_FREQ CONFIG_XTAL_FREQ +#define CONFIG_ESP32_XTAL_FREQ_40 CONFIG_XTAL_FREQ_40 +#define CONFIG_ESP_GRATUITOUS_ARP CONFIG_LWIP_ESP_GRATUITOUS_ARP +#define CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY CONFIG_ESP_SLEEP_WAIT_FLASH_READY_EXTRA_DELAY +#define CONFIG_ESP_SYSTEM_BROWNOUT_INTR CONFIG_ESP_BROWNOUT_USE_INTR +#define CONFIG_ESP_TASK_WDT CONFIG_ESP_TASK_WDT_INIT +#define CONFIG_FLASHMODE_DIO CONFIG_ESPTOOLPY_FLASHMODE_DIO +#define CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR +#define CONFIG_GARP_TMR_INTERVAL CONFIG_LWIP_GARP_TMR_INTERVAL +#define CONFIG_GDBSTUB_MAX_TASKS CONFIG_ESP_GDBSTUB_MAX_TASKS +#define CONFIG_GDBSTUB_SUPPORT_TASKS CONFIG_ESP_GDBSTUB_SUPPORT_TASKS +#define CONFIG_INT_WDT CONFIG_ESP_INT_WDT +#define CONFIG_INT_WDT_CHECK_CPU1 CONFIG_ESP_INT_WDT_CHECK_CPU1 +#define CONFIG_INT_WDT_TIMEOUT_MS CONFIG_ESP_INT_WDT_TIMEOUT_MS +#define CONFIG_IPC_TASK_STACK_SIZE CONFIG_ESP_IPC_TASK_STACK_SIZE +#define CONFIG_LOG_BOOTLOADER_LEVEL CONFIG_BOOTLOADER_LOG_LEVEL +#define CONFIG_LOG_BOOTLOADER_LEVEL_INFO CONFIG_BOOTLOADER_LOG_LEVEL_INFO +#define CONFIG_MAIN_TASK_STACK_SIZE CONFIG_ESP_MAIN_TASK_STACK_SIZE +#define CONFIG_MONITOR_BAUD CONFIG_ESPTOOLPY_MONITOR_BAUD +#define CONFIG_NEWLIB_STDIN_LINE_ENDING_CR CONFIG_LIBC_STDIN_LINE_ENDING_CR +#define CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF CONFIG_LIBC_STDOUT_LINE_ENDING_CRLF +#define CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC_HRT CONFIG_LIBC_TIME_SYSCALL_USE_RTC_HRT +#define CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES +#define CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE +#define CONFIG_OPTIMIZATION_ASSERTION_LEVEL CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL +#define CONFIG_OPTIMIZATION_LEVEL_DEBUG CONFIG_COMPILER_OPTIMIZATION_DEBUG +#define CONFIG_POST_EVENTS_FROM_IRAM_ISR CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR +#define CONFIG_POST_EVENTS_FROM_ISR CONFIG_ESP_EVENT_POST_FROM_ISR +#define CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS +#define CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS +#define CONFIG_STACK_CHECK_NONE CONFIG_COMPILER_STACK_CHECK_MODE_NONE +#define CONFIG_SUPPORT_TERMIOS CONFIG_VFS_SUPPORT_TERMIOS +#define CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT +#define CONFIG_SYSTEM_EVENT_QUEUE_SIZE CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE +#define CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE +#define CONFIG_TASK_WDT CONFIG_ESP_TASK_WDT_INIT +#define CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0 CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 +#define CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1 CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 +#define CONFIG_TASK_WDT_TIMEOUT_S CONFIG_ESP_TASK_WDT_TIMEOUT_S +#define CONFIG_TCPIP_RECVMBOX_SIZE CONFIG_LWIP_TCPIP_RECVMBOX_SIZE +#define CONFIG_TCPIP_TASK_AFFINITY CONFIG_LWIP_TCPIP_TASK_AFFINITY +#define CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY +#define CONFIG_TCPIP_TASK_STACK_SIZE CONFIG_LWIP_TCPIP_TASK_STACK_SIZE +#define CONFIG_TCP_MAXRTX CONFIG_LWIP_TCP_MAXRTX +#define CONFIG_TCP_MSL CONFIG_LWIP_TCP_MSL +#define CONFIG_TCP_MSS CONFIG_LWIP_TCP_MSS +#define CONFIG_TCP_OVERSIZE_MSS CONFIG_LWIP_TCP_OVERSIZE_MSS +#define CONFIG_TCP_QUEUE_OOSEQ CONFIG_LWIP_TCP_QUEUE_OOSEQ +#define CONFIG_TCP_RECVMBOX_SIZE CONFIG_LWIP_TCP_RECVMBOX_SIZE +#define CONFIG_TCP_SND_BUF_DEFAULT CONFIG_LWIP_TCP_SND_BUF_DEFAULT +#define CONFIG_TCP_SYNMAXRTX CONFIG_LWIP_TCP_SYNMAXRTX +#define CONFIG_TCP_WND_DEFAULT CONFIG_LWIP_TCP_WND_DEFAULT +#define CONFIG_TIMER_QUEUE_LENGTH CONFIG_FREERTOS_TIMER_QUEUE_LENGTH +#define CONFIG_TIMER_TASK_PRIORITY CONFIG_FREERTOS_TIMER_TASK_PRIORITY +#define CONFIG_TIMER_TASK_STACK_DEPTH CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH +#define CONFIG_TIMER_TASK_STACK_SIZE CONFIG_ESP_TIMER_TASK_STACK_SIZE +#define CONFIG_TRACEMEM_RESERVE_DRAM CONFIG_ESP32_TRACEMEM_RESERVE_DRAM +#define CONFIG_UDP_RECVMBOX_SIZE CONFIG_LWIP_UDP_RECVMBOX_SIZE +#define CONFIG_WPA_MBEDTLS_CRYPTO CONFIG_ESP_WIFI_MBEDTLS_CRYPTO +#define CONFIG_WPA_MBEDTLS_TLS_CLIENT CONFIG_ESP_WIFI_MBEDTLS_TLS_CLIENT diff --git a/components/mdns/tests/host_unit_test/unity/cmock_config.yml b/components/mdns/tests/host_unit_test/unity/cmock_config.yml new file mode 100644 index 0000000000..2487467f2f --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/cmock_config.yml @@ -0,0 +1,17 @@ +--- +:cmock: + :mock_prefix: mock_ + :plugins: + - :callback + - :ignore + - :expect_any_args + - :array + - :return_thru_ptr + :strippables: + - '(?:extern|static)\s+' + :treat_as: + uint8_t: HEX8 + uint16_t: HEX16 + uint32_t: UINT32 + int8_t: INT8 + bool: UINT8 diff --git a/components/mdns/tests/host_unit_test/unity/create_test_packet.c b/components/mdns/tests/host_unit_test/unity/create_test_packet.c new file mode 100644 index 0000000000..eb25a228ab --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/create_test_packet.c @@ -0,0 +1,160 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include "esp_err.h" +#include "unity.h" +#include "create_test_packet.h" + +// Encode a domain name in DNS format (length byte + characters) +size_t encode_dns_name(uint8_t* buffer, const char* name) +{ + size_t pos = 0; + const char* segment_start = name; + + // Process each segment (separated by dots) + for (const char* p = name; ; p++) { + if (*p == '.' || *p == 0) { + // Calculate segment length + size_t segment_len = p - segment_start; + + // Write length byte + buffer[pos++] = segment_len; + + // Write segment characters + for (size_t i = 0; i < segment_len; i++) { + buffer[pos++] = segment_start[i]; + } + + // If at end of string, exit loop + if (*p == 0) { + break; + } + + // Move to next segment + segment_start = p + 1; + } + } + + // Add null terminator + buffer[pos++] = 0; + + return pos; +} + +// Create an mDNS test packet from arrays of queries and answers +uint8_t* create_mdns_test_packet( + mdns_test_query_t queries[], size_t query_count, + mdns_test_answer_t answers[], size_t answer_count, + mdns_test_answer_t additional[], size_t additional_count, + size_t* packet_len) +{ + // Allocate buffer for packet (max reasonable size for test) + uint8_t* packet = malloc(1460); + size_t pos = 0; + + // DNS Header (12 bytes) + // Transaction ID + packet[pos++] = 0x00; + packet[pos++] = 0x00; + + // Flags (QR=1 for response if there are answers, AA=1) + uint16_t flags = 0x0000; + if (answer_count > 0) { + flags |= 0x8400; // QR=1, AA=1 + } + packet[pos++] = (flags >> 8) & 0xFF; + packet[pos++] = flags & 0xFF; + + // Counts + packet[pos++] = 0x00; + packet[pos++] = query_count & 0xFF; // QDCOUNT + packet[pos++] = 0x00; + packet[pos++] = answer_count & 0xFF; // ANCOUNT + packet[pos++] = 0x00; + packet[pos++] = 0x00; // NSCOUNT + packet[pos++] = 0x00; + packet[pos++] = additional_count & 0xFF; // ARCOUNT + + // Add queries + for (size_t i = 0; i < query_count; i++) { + // Encode name + pos += encode_dns_name(packet + pos, queries[i].name); + + // Type + packet[pos++] = (queries[i].type >> 8) & 0xFF; + packet[pos++] = queries[i].type & 0xFF; + + // Class + packet[pos++] = (queries[i].class >> 8) & 0xFF; + packet[pos++] = queries[i].class & 0xFF; + } + + // Add answers + for (size_t i = 0; i < answer_count; i++) { + // Encode name + pos += encode_dns_name(packet + pos, answers[i].name); + + // Type + packet[pos++] = (answers[i].type >> 8) & 0xFF; + packet[pos++] = answers[i].type & 0xFF; + + // Class + packet[pos++] = (answers[i].class >> 8) & 0xFF; + packet[pos++] = answers[i].class & 0xFF; + + // TTL (4 bytes) + packet[pos++] = (answers[i].ttl >> 24) & 0xFF; + packet[pos++] = (answers[i].ttl >> 16) & 0xFF; + packet[pos++] = (answers[i].ttl >> 8) & 0xFF; + packet[pos++] = answers[i].ttl & 0xFF; + + // Data length + packet[pos++] = (answers[i].data_len >> 8) & 0xFF; + packet[pos++] = answers[i].data_len & 0xFF; + + // Data + if (answers[i].data && answers[i].data_len > 0) { + memcpy(packet + pos, answers[i].data, answers[i].data_len); + pos += answers[i].data_len; + } + } + + // Add additional records + for (size_t i = 0; i < additional_count; i++) { + // Encode name + pos += encode_dns_name(packet + pos, additional[i].name); + + // Type + packet[pos++] = (additional[i].type >> 8) & 0xFF; + packet[pos++] = additional[i].type & 0xFF; + + // Class + packet[pos++] = (additional[i].class >> 8) & 0xFF; + packet[pos++] = additional[i].class & 0xFF; + + // TTL (4 bytes) + packet[pos++] = (additional[i].ttl >> 24) & 0xFF; + packet[pos++] = (additional[i].ttl >> 16) & 0xFF; + packet[pos++] = (additional[i].ttl >> 8) & 0xFF; + packet[pos++] = additional[i].ttl & 0xFF; + + // Data length + packet[pos++] = (additional[i].data_len >> 8) & 0xFF; + packet[pos++] = additional[i].data_len & 0xFF; + + // Data + if (additional[i].data && additional[i].data_len > 0) { + memcpy(packet + pos, additional[i].data, additional[i].data_len); + pos += additional[i].data_len; + } + } + + *packet_len = pos; + return packet; +} diff --git a/components/mdns/tests/host_unit_test/unity/create_test_packet.h b/components/mdns/tests/host_unit_test/unity/create_test_packet.h new file mode 100644 index 0000000000..25201c905c --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/create_test_packet.h @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once +// Structure for a DNS query +typedef struct { + const char* name; // Name to query (e.g., "test.local") + uint16_t type; // Query type (e.g., MDNS_TYPE_A) + uint16_t class; // Query class (typically 1 for IN) +} mdns_test_query_t; + +// Structure for a DNS answer record +typedef struct { + const char* name; // Name this record refers to + uint16_t type; // Record type + uint16_t class; // Record class + uint32_t ttl; // Time to live + uint16_t data_len; // Length of data + uint8_t* data; // Record data +} mdns_test_answer_t; + + +uint8_t* create_mdns_test_packet( + mdns_test_query_t queries[], size_t query_count, + mdns_test_answer_t answers[], size_t answer_count, + mdns_test_answer_t additional[], size_t additional_count, + size_t* packet_len); + +size_t encode_dns_name(uint8_t* buffer, const char* name); diff --git a/components/mdns/tests/host_unit_test/unity/enable_testing.cmake b/components/mdns/tests/host_unit_test/unity/enable_testing.cmake new file mode 100644 index 0000000000..5812cf463c --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/enable_testing.cmake @@ -0,0 +1,11 @@ +enable_testing() +add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} --test) + +add_custom_target(generate_mocks + DEPENDS ${MOCK_OUTPUTS} +) +add_dependencies(${PROJECT_NAME} generate_mocks) + +# Add sanitizers: Please comment out when debugging +target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address -fsanitize=undefined) +target_compile_options(${PROJECT_NAME} PRIVATE -fsanitize=address -fsanitize=undefined) diff --git a/components/mdns/tests/host_unit_test/unity/test_receiver/test_receiver.c b/components/mdns/tests/host_unit_test/unity/test_receiver/test_receiver.c new file mode 100644 index 0000000000..698c850362 --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/test_receiver/test_receiver.c @@ -0,0 +1,93 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include "unity.h" +#include "create_test_packet.h" +#include "unity_main.h" +#include "mock_mdns_pcb.h" +#include "mock_mdns_send.h" +#include "create_test_packet.h" + +static void test_mdns_hostname_queries(void) +{ + // Define the queries for test4.local and test.local + mdns_test_query_t queries[] = { + { "test4.local", 1, 1 }, // A record for test4.local + { "test.local", 1, 1 } // A record for test.local + }; + + // Create and send the packet + size_t packet_len; + uint8_t* packet = create_mdns_test_packet( + queries, 2, // Queries + NULL, 0, // No answers + NULL, 0, // No additional records + &packet_len + ); + + send_test_packet_multiple(packet, packet_len); +} + +// Example of a more complex test with answers and additional records +static void test_mdns_with_answers(void) +{ + // Define a query for _http._tcp.local PTR record + mdns_test_query_t queries[] = { + { "_http._tcp.local", 12, 1 } // PTR record + }; + + // Example data for a PTR record (simplified) + uint8_t ptr_data[200]; + size_t ptr_data_len = encode_dns_name(ptr_data, "test._http._tcp.local"); + + // Define an answer for the PTR record + mdns_test_answer_t answers[] = { + { "_http._tcp.local", 12, 1, 120, ptr_data_len, ptr_data } + }; + + // Create and send the packet + size_t packet_len; + uint8_t* packet = create_mdns_test_packet( + queries, 1, // Single query + answers, 1, // Single answer + NULL, 0, // No additional records + &packet_len + ); + + send_test_packet_multiple(packet, packet_len); + +} + +static void mdns_priv_create_answer_from_parsed_packet_Callback(mdns_parsed_packet_t* parsed_packet, int cmock_num_calls) +{ + printf("callback\n"); +} + +void setup_cmock(void) +{ + mdns_priv_probe_all_pcbs_CMockIgnore(); + mdns_priv_pcb_announce_CMockIgnore(); + mdns_priv_pcb_send_bye_service_CMockIgnore(); + mdns_priv_pcb_check_probing_services_CMockIgnore(); + mdns_priv_pcb_is_after_probing_IgnoreAndReturn(true); + + mdns_priv_clear_tx_queue_CMockIgnore(); + mdns_priv_remove_scheduled_service_packets_CMockIgnore(); + mdns_priv_create_answer_from_parsed_packet_Stub(mdns_priv_create_answer_from_parsed_packet_Callback); +} + +void run_unity_tests(void) +{ + UNITY_BEGIN(); + + // Run hostname queries test + RUN_TEST(test_mdns_hostname_queries); + + // Run test with answers + RUN_TEST(test_mdns_with_answers); + + UNITY_END(); +} diff --git a/components/mdns/tests/host_unit_test/unity/test_receiver/unit_test.cmake b/components/mdns/tests/host_unit_test/unity/test_receiver/unit_test.cmake new file mode 100644 index 0000000000..b08bbe6c4d --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/test_receiver/unit_test.cmake @@ -0,0 +1,7 @@ +# Testing mDNS receiver, so mocking pbb and send +set(MOCK_FILES + "mdns_pcb" + "mdns_send" +) + +list(APPEND SOURCES unity/${UNIT_TESTS}/test_receiver.c) diff --git a/components/mdns/tests/host_unit_test/unity/test_sender/test_sender.c b/components/mdns/tests/host_unit_test/unity/test_sender/test_sender.c new file mode 100644 index 0000000000..0d444c28ce --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/test_sender/test_sender.c @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include "unity.h" +#include "unity_main.h" +#include "mock_mdns_pcb.h" +#include "mdns_send.h" + +void setup_cmock(void) +{ + mdns_priv_probe_all_pcbs_CMockIgnore(); + mdns_priv_pcb_announce_CMockIgnore(); + mdns_priv_pcb_send_bye_service_CMockIgnore(); + mdns_priv_pcb_check_probing_services_CMockIgnore(); + mdns_priv_pcb_is_after_probing_IgnoreAndReturn(true); +} + +static void test_dispatch_tx_packet(void) +{ + mdns_tx_packet_t p = {}; + mdns_priv_dispatch_tx_packet(&p); +} + +void run_unity_tests(void) +{ + UNITY_BEGIN(); + + // Run hostname queries test + RUN_TEST(test_dispatch_tx_packet); + + + UNITY_END(); +} diff --git a/components/mdns/tests/host_unit_test/unity/test_sender/unit_test.cmake b/components/mdns/tests/host_unit_test/unity/test_sender/unit_test.cmake new file mode 100644 index 0000000000..2fa0b64337 --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/test_sender/unit_test.cmake @@ -0,0 +1,6 @@ +# Testing mDNS sender, need to mock pbb +set(MOCK_FILES + "mdns_pcb" +) + +list(APPEND SOURCES unity/${UNIT_TESTS}/test_sender.c) diff --git a/components/mdns/tests/host_unit_test/unity/unit_test.cmake b/components/mdns/tests/host_unit_test/unity/unit_test.cmake new file mode 100644 index 0000000000..8379b4ba8a --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/unit_test.cmake @@ -0,0 +1,67 @@ +set(UNITY_DIR "$ENV{IDF_PATH}/components/unity") +set(CMOCK_DIR "$ENV{IDF_PATH}/components/cmock") + +include_directories(${UNITY_DIR}/unity/src) +add_definitions(-DENABLE_UNIT_TESTS) + +# Use ruby command directly instead of looking for ruby.exe +find_program(RUBY_EXECUTABLE ruby) +if(NOT RUBY_EXECUTABLE) + message(FATAL_ERROR "Ruby is required for CMock but was not found!") +endif() + +include(unity/${UNIT_TESTS}/unit_test.cmake) + +# Verify headers exist and create mock commands for each +foreach(mock_file ${MOCK_FILES}) + set(header_path "${COMPONENT_DIR}/private_include/${mock_file}.h") + if(NOT EXISTS ${header_path}) + message(FATAL_ERROR "Cannot find ${mock_file}.h at ${header_path}") + endif() + + list(APPEND MOCK_OUTPUTS + ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c + ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.h + ) + + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c + ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.h + COMMAND ${RUBY_EXECUTABLE} + ${CMOCK_DIR}/CMock/lib/cmock.rb + -o${CMAKE_CURRENT_SOURCE_DIR}/unity/cmock_config.yml + ${header_path} + DEPENDS ${header_path} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating mock for ${mock_file}.h" + ) +endforeach() + +# Include CMock headers +include_directories( + ${CMOCK_DIR}/CMock/src + ${CMAKE_CURRENT_BINARY_DIR}/mocks + ${UNITY_DIR}/unity/src + ${UNITY_DIR}/include + unity +) + +# Create directory for generated mocks +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mocks) + +foreach(mock_file ${MOCK_FILES}) + list(REMOVE_ITEM SOURCES ${MDNS_DIR}/${mock_file}.c) +endforeach() + +# Add test-related sources +list(APPEND SOURCES + ${UNITY_DIR}/unity/src/unity.c + ${CMOCK_DIR}/CMock/src/cmock.c + unity/unity_main.c + unity/create_test_packet.c +) + +# Add all generated mock files +foreach(mock_file ${MOCK_FILES}) + list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c) +endforeach() diff --git a/components/mdns/tests/host_unit_test/unity/unity_main.c b/components/mdns/tests/host_unit_test/unity/unity_main.c new file mode 100644 index 0000000000..cee65d98bb --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/unity_main.c @@ -0,0 +1,146 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include "esp_err.h" +#include "unity.h" +#include "mdns_receive.h" +#include "mdns_responder.h" +#include "mdns_mem_caps.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +// Sample test case - update based on the actual functionality in mdns_receive.c +void test_init(void) +{ + + +} + +esp_err_t mdns_packet_push(esp_ip_addr_t *addr, int port, mdns_if_t tcpip_if, uint8_t*data, size_t len); + +mdns_search_once_t *s_a, *s_aaaa, *s_ptr, *s_srv, *s_txt; + +void init_responder(void) +{ + mdns_ip_addr_t addr = { .addr = { .u_addr = ESP_IPADDR_TYPE_V4 } }; + addr.addr.u_addr.ip4.addr = 0x11111111; + mdns_txt_item_t txt[4] = { + {"board", "esp32"}, + {"tcp_check", "no"}, + {"ssh_upload", "no"}, + {"auth_upload", "no"} + }; + mdns_priv_responder_init(); + mdns_hostname_set("test"); + mdns_instance_name_set("test2"); + mdns_delegate_hostname_add("test3", NULL); + mdns_delegate_hostname_add("test4", &addr); + mdns_service_add("inst1", "_http", "_tcp", 80, txt, 4); + mdns_service_subtype_add_for_host("inst1", "_http", "_tcp", "test", "subtype"); + mdns_service_add("inst2", "_http", "_tcp", 80, txt, 1); + mdns_service_subtype_add_for_host("inst2", "_http", "_tcp", "test", "subtype3"); + mdns_service_add("inst3", "_http", "_tcp", 80, NULL, 0); + mdns_service_add_for_host("deleg1", "_http", "_tcp", "test3", 80, txt, 2); + mdns_service_add_for_host(NULL, "_http", "_tcp", "test4", 80, txt, 2); + mdns_service_add(NULL, "_scanner", "_tcp", 80, NULL, 0); + mdns_service_add("inst5", "_scanner", "_tcp", 80, NULL, 0); + mdns_service_add("inst6", "_http", "_tcp", 80, NULL, 0); + mdns_service_add("inst7", "_sleep", "_udp", 80, NULL, 0); + + s_a = mdns_query_async_new("host_name", NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL); + s_aaaa = mdns_query_async_new("host_name2", NULL, NULL, MDNS_TYPE_AAAA, 1000, 1, NULL); + s_ptr = mdns_query_async_new("minifritz", "_http", "_tcp", MDNS_TYPE_PTR, 1000, 1, NULL); + s_srv = mdns_query_async_new("fritz", "_http", "_tcp", MDNS_TYPE_SRV, 1000, 1, NULL); + s_txt = mdns_query_async_new("fritz", "_http", "_tcp", MDNS_TYPE_TXT, 1000, 1, NULL); + +} + +void deinit_responder(void) +{ + mdns_query_async_delete(s_a); + mdns_query_async_delete(s_aaaa); + mdns_query_async_delete(s_ptr); + mdns_query_async_delete(s_srv); + mdns_query_async_delete(s_txt); + mdns_service_remove_all(); + mdns_priv_responder_free(); +} + +void send_packet(bool ip4, bool mdns_port, uint8_t*data, size_t len) +{ + esp_ip_addr_t addr4 = ESP_IP4ADDR_INIT(192, 168, 1, 1); + esp_ip_addr_t addr6 = ESP_IP6ADDR_INIT(0x000002ff, 0, 0, 0xfe800000); + esp_ip_addr_t *addr = ip4 ? &addr4 : &addr6; + int port = mdns_port ? 53 : 5353; + + if (mdns_packet_push(addr, port, 0, data, len) != ESP_OK) { + printf("Failed to push packet\n"); + } +} + +// Helper function to send a test packet with different configurations and clean up +void send_test_packet_multiple(uint8_t* packet, size_t packet_len) +{ + if (packet) { + // Test with different packet configurations + send_packet(true, true, packet, packet_len); + send_packet(true, false, packet, packet_len); + send_packet(false, true, packet, packet_len); + send_packet(false, false, packet, packet_len); + + // Free the packet + free(packet); + } else { + printf("Failed to create test packet\n"); + } +} + +void test_mdns_receive_from_file(const char* filename) +{ + uint8_t buf[1460]; + FILE *file = fopen(filename, "r"); + TEST_ASSERT_NOT_NULL_MESSAGE(file, "Failed to open test packet file"); + + size_t len = fread(buf, 1, 1460, file); + fclose(file); + + // Test with different packet configurations + send_packet(true, true, buf, len); + send_packet(true, false, buf, len); + send_packet(false, true, buf, len); + send_packet(false, false, buf, len); + + // Add assertions here based on expected behavior + // For example: + // TEST_ASSERT_EQUAL(expected_result, actual_result); +} + + +void run_unity_tests(void); +void setup_cmock(void); + +int main(int argc, char **argv) +{ + if (argc >= 2 && strcmp(argv[1], "--test") == 0) { + setup_cmock(); + + init_responder(); + run_unity_tests(); + deinit_responder(); + return 0; + } + printf("Unit test configuration: run with --test argument\n"); + return 1; +} diff --git a/components/mdns/tests/host_unit_test/unity/unity_main.h b/components/mdns/tests/host_unit_test/unity/unity_main.h new file mode 100644 index 0000000000..b69098df00 --- /dev/null +++ b/components/mdns/tests/host_unit_test/unity/unity_main.h @@ -0,0 +1,10 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once +#include + +void send_packet(bool ip4, bool mdns_port, uint8_t*data, size_t len); +void send_test_packet_multiple(uint8_t* packet, size_t packet_len); diff --git a/components/mdns/tests/test_afl_fuzz_host/CMakeLists.txt b/components/mdns/tests/test_afl_fuzz_host/CMakeLists.txt deleted file mode 100644 index d65845194f..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# The following four lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - -project(fuzz_test_update) diff --git a/components/mdns/tests/test_afl_fuzz_host/Makefile b/components/mdns/tests/test_afl_fuzz_host/Makefile deleted file mode 100644 index c9f08b1066..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/Makefile +++ /dev/null @@ -1,96 +0,0 @@ -#INSTR=off -TEST_NAME=test -FUZZ=afl-fuzz -COMPONENTS_DIR=$(IDF_PATH)/components -# Use ESP32 toolchain include path if available, otherwise fall back to system includes for host-based compilation -COMPILER_INCLUDE_DIR=$(shell if command -v xtensa-esp32-elf-gcc >/dev/null 2>&1; then echo `which xtensa-esp32-elf-gcc | xargs dirname | xargs dirname`/xtensa-esp32-elf; else echo /usr; fi) - -CFLAGS=-g -Wno-unused-value -Wno-missing-declarations -Wno-pointer-bool-conversion -Wno-macro-redefined -Wno-int-to-void-pointer-cast -DHOOK_MALLOC_FAILED -DESP_EVENT_H_ -D__ESP_LOG_H__ \ - -I. -I../.. -I../../include -I../../private_include -I ./build/config \ - -I$(COMPONENTS_DIR) \ - -I$(COMPONENTS_DIR)/driver/include \ - -I$(COMPONENTS_DIR)/esp_common/include \ - -I$(COMPONENTS_DIR)/esp_event/include \ - -I$(COMPONENTS_DIR)/esp_eth/include \ - -I$(COMPONENTS_DIR)/esp_hw_support/include \ - -I$(COMPONENTS_DIR)/esp_netif/include \ - -I$(COMPONENTS_DIR)/esp_netif/private_include \ - -I$(COMPONENTS_DIR)/esp_netif/lwip \ - -I$(COMPONENTS_DIR)/esp_rom/include \ - -I$(COMPONENTS_DIR)/esp_system/include \ - -I$(COMPONENTS_DIR)/esp_timer/include \ - -I$(COMPONENTS_DIR)/esp_wifi/include \ - -I$(COMPONENTS_DIR)/freertos/FreeRTOS-Kernel \ - -I$(COMPONENTS_DIR)/freertos/FreeRTOS-Kernel/include \ - -I$(COMPONENTS_DIR)/freertos/esp_additions/include/freertos \ - -I$(COMPONENTS_DIR)/hal/include \ - -I$(COMPONENTS_DIR)/hal/esp32/include \ - -I$(COMPONENTS_DIR)/heap/include \ - -I$(COMPONENTS_DIR)/log/include \ - -I$(COMPONENTS_DIR)/lwip/lwip/src/include \ - -I$(COMPONENTS_DIR)/linux/include \ - -I$(COMPONENTS_DIR)/lwip/port/esp32/include \ - -I$(COMPONENTS_DIR)/lwip/lwip/src/include/lwip/apps \ - -I$(COMPONENTS_DIR)/soc/include \ - -I$(COMPONENTS_DIR)/soc/esp32/include \ - -I$(COMPONENTS_DIR)/soc/src/esp32/include \ - -I$(COMPONENTS_DIR)/xtensa/include \ - -I$(COMPONENTS_DIR)/xtensa/esp32/include \ - -I$(COMPONENTS_DIR)/esp_hw_support/etm/include \ - -I$(COMPILER_INCLUDE_DIR)/include - - -MDNS_C_DEPENDENCY_INJECTION=-include mdns_di.h -ifeq ($(MDNS_NO_SERVICES),on) - CFLAGS+=-DMDNS_NO_SERVICES -endif - -ifeq ($(INSTR),off) - CC=gcc - CFLAGS+=-DINSTR_IS_OFF - TEST_NAME=test_sim -else - CC=afl-clang-fast -endif -CPP=$(CC) -LD=$(CC) -OBJECTS=esp32_mock.o mdns.o test.o esp_netif_mock.o - -OS := $(shell uname) -ifeq ($(OS),Darwin) - LDLIBS= -else - LDLIBS=-lbsd - CFLAGS+=-DUSE_BSD_STRING -endif - -all: $(TEST_NAME) - -%.o: %.c - @echo "[CC] $<" - @$(CC) $(CFLAGS) -c $< -o $@ - -mdns.o: ../../mdns.c - @echo "[CC] $<" - @$(CC) $(CFLAGS) -include mdns_mock.h $(MDNS_C_DEPENDENCY_INJECTION) -c $< -o $@ - -$(TEST_NAME): $(OBJECTS) - @echo "[LD] $@" - @$(LD) $(OBJECTS) -o $@ $(LDLIBS) - -fuzz: $(TEST_NAME) - # timeout returns 124 if time limit is reached, original return code otherwise - # pass only if: fuzzing was running smoothly until timeout AND no crash found - @timeout 10m $(FUZZ) -i "in" -o "out" -- ./$(TEST_NAME) || \ - if [ $$? -eq 124 ]; then \ - if [ -n "$$(find out/default/crashes -type f 2>/dev/null)" ]; then \ - echo "Crashes found!"; \ - tar -czf out/default/crashes.tar.gz -C out/default crashes; \ - exit 1; \ - fi \ - else \ - exit 1; \ - fi - -clean: - @rm -rf *.o *.SYM $(TEST_NAME) out diff --git a/components/mdns/tests/test_afl_fuzz_host/README.md b/components/mdns/tests/test_afl_fuzz_host/README.md deleted file mode 100644 index e22a89606f..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/README.md +++ /dev/null @@ -1,80 +0,0 @@ -## Introduction -This test uses [american fuzzy lop](http://lcamtuf.coredump.cx/afl/) to mangle real mdns packets and look for exceptions caused by the parser. - -A few actual packets are collected and exported as bins in the `in` folder, which is then passed as input to AFL when testing. The setup procedure for the test includes all possible services and scenarios that could be used with the given input packets.The output of the parser before fuzzing can be found in [input_packets.txt](input_packets.txt) - -## Building and running the tests using AFL -To build and run the tests using AFL(afl-clang-fast) instrumentation - -```bash -cd $IDF_PATH/components/mdns/test_afl_host -make fuzz -``` - -(Please note you have to install AFL instrumentation first, check `Installing AFL` section) - -## Building the tests using GCC INSTR(off) - -To build the tests without AFL instrumentations and instead of that use GCC compiler(In this case it will only check for compilation issues and will not run AFL tests). - -```bash -cd $IDF_PATH/components/mdns/test_afl_host -make INSTR=off -``` - -Note, that this setup is useful if we want to reproduce issues reported by fuzzer tests executed in the CI, or to simulate how the packet parser treats the input packets on the host machine. - -## Installing AFL -To run the test yourself, you need to download the [latest afl archive](http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz) and extract it to a folder on your computer. - -The rest of the document will refer to that folder as ```PATH_TO_AFL```. - -### Preparation -- On Mac, you will need to install the latest Xcode and llvm support from [Homebrew](https://brew.sh) - - ```bash - /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - brew install --with-clang --with-lld --HEAD llvm - export PATH="/usr/local/opt/llvm/bin:$PATH" - ``` - -- On Ubuntu you need the following packages: - - ```bash - sudo apt-get install make clang-4.0(or <=4.0) llvm-4.0(or <=4.0) libbsd-dev - ``` - -Please note that if specified package version can't be installed(due to system is the latest), you can download, build and install it manually. - -### Compile AFL -Compiling AFL is as easy as running make: - -```bash -cd [PATH_TO_AFL] -make -cd llvm_mode/ -make -``` - -After successful compilation, you can export the following variables to your shell (you can also add them to your profile if you want to use AFL in other projects). - -```bash -export AFL_PATH=[PATH_TO_AFL] -export PATH="$AFL_PATH:$PATH" -``` - -Please note LLVM must be <=4.0.0, otherwise afl does not compile, as there are some limitations with building AFL on MacOS/Linux with the latest LLVM. Also, Windows build on cygwin is not fully supported. - -## Additional info -Apple has a crash reporting service that could interfere with AFLs normal operation. To turn that off, run the following command: - -```bash -launchctl unload -w /System/Library/LaunchAgents/com.apple.ReportCrash.plist -sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.ReportCrash.Root.plist -``` - -Ubuntu has a similar service. To turn that off, run as root: - -```bash -echo core >/proc/sys/kernel/core_pattern -``` diff --git a/components/mdns/tests/test_afl_fuzz_host/esp32_mock.c b/components/mdns/tests/test_afl_fuzz_host/esp32_mock.c deleted file mode 100644 index 7f14dc6baa..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/esp32_mock.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -#include -#include -#include -#include -#include -#include "esp32_mock.h" -#include "esp_log.h" - -void *g_queue; -int g_queue_send_shall_fail = 0; -int g_size = 0; - -const char *WIFI_EVENT = "wifi_event"; -const char *ETH_EVENT = "eth_event"; - -esp_err_t esp_event_handler_register(const char *event_base, - int32_t event_id, - void *event_handler, - void *event_handler_arg) -{ - return ESP_OK; -} - -esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler) -{ - return ESP_OK; -} - -esp_err_t esp_timer_delete(esp_timer_handle_t timer) -{ - return ESP_OK; -} - -esp_err_t esp_timer_stop(esp_timer_handle_t timer) -{ - return ESP_OK; -} - -esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period) -{ - return ESP_OK; -} - -esp_err_t esp_timer_create(const esp_timer_create_args_t *create_args, - esp_timer_handle_t *out_handle) -{ - return ESP_OK; -} - -uint32_t xTaskGetTickCount(void) -{ - static uint32_t tick = 0; - return tick++; -} - -/// Queue mock -QueueHandle_t xQueueCreate(uint32_t uxQueueLength, uint32_t uxItemSize) -{ - g_size = uxItemSize; - g_queue = malloc((uxQueueLength) * (uxItemSize)); - return g_queue; -} - - -void vQueueDelete(QueueHandle_t xQueue) -{ - free(xQueue); -} - -uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait) -{ - if (g_queue_send_shall_fail) { - return pdFALSE; - } else { - memcpy(xQueue, pvItemToQueue, g_size); - return pdPASS; - } -} - - -uint32_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait) -{ - return pdFALSE; -} - -void GetLastItem(void *pvBuffer) -{ - memcpy(pvBuffer, g_queue, g_size); -} - -void ForceTaskDelete(void) -{ - g_queue_send_shall_fail = 1; -} - -TaskHandle_t xTaskGetCurrentTaskHandle(void) -{ - return NULL; -} - -void xTaskNotifyGive(TaskHandle_t task) -{ - return; -} - -BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time) -{ - return pdTRUE; -} - -void esp_log_write(esp_log_level_t level, const char *tag, const char *format, ...) -{ -} - -void esp_log(esp_log_config_t config, const char *tag, const char *format, ...) -{ -} - -uint32_t esp_log_timestamp(void) -{ - return 0; -} - -void *mdns_mem_malloc(size_t size) -{ - return malloc(size); -} - -void *mdns_mem_calloc(size_t num, size_t size) -{ - return calloc(num, size); -} - -void mdns_mem_free(void *ptr) -{ - free(ptr); -} - -char *mdns_mem_strdup(const char *s) -{ - return strdup(s); -} - -char *mdns_mem_strndup(const char *s, size_t n) -{ - return strndup(s, n); -} - -void *mdns_mem_task_malloc(size_t size) -{ - return malloc(size); -} - -void mdns_mem_task_free(void *ptr) -{ - free(ptr); -} diff --git a/components/mdns/tests/test_afl_fuzz_host/esp32_mock.h b/components/mdns/tests/test_afl_fuzz_host/esp32_mock.h deleted file mode 100644 index 70a2037ffa..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/esp32_mock.h +++ /dev/null @@ -1,145 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#ifndef _ESP32_COMPAT_H_ -#define _ESP32_COMPAT_H_ - -// Skip these include files -#define ESP_MDNS_NETWORKING_H_ -#define INC_FREERTOS_H -#define QUEUE_H -#define SEMAPHORE_H -#define _ESP_TASK_H_ - -#ifdef USE_BSD_STRING -#include -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include "esp_timer.h" - -#define ESP_FAIL -1 - -#define ESP_ERR_NO_MEM 0x101 -#define ESP_ERR_INVALID_ARG 0x102 -#define ESP_ERR_INVALID_STATE 0x103 -#define ESP_ERR_INVALID_SIZE 0x104 -#define ESP_ERR_NOT_FOUND 0x105 -#define ESP_ERR_NOT_SUPPORTED 0x106 -#define ESP_ERR_TIMEOUT 0x107 -#define ESP_ERR_INVALID_RESPONSE 0x108 -#define ESP_ERR_INVALID_CRC 0x109 - -#define MDNS_TASK_MEMORY_LOG "internal RAM" -#define MALLOC_CAP_8BIT (1<<2) -#define MALLOC_CAP_INTERNAL (1<<11) - -#define pdTRUE true -#define pdFALSE false -#define pdPASS ( pdTRUE ) -#define pdFAIL ( pdFALSE ) - -#define portMAX_DELAY 0xFFFFFFFF -#define portTICK_PERIOD_MS 1 -#define LWIP_HDR_PBUF_H -#define __ESP_RANDOM_H__ -#define INC_TASK_H - -#define pdMS_TO_TICKS(a) a -#define xSemaphoreTake(s,d) true -#define vTaskDelete(a) free(NULL) -#define xSemaphoreGive(s) -#define xQueueCreateMutex(s) -#define _mdns_pcb_init(a,b) true -#define _mdns_pcb_deinit(a,b) true -#define xSemaphoreCreateMutex() malloc(1) -#define xSemaphoreCreateBinary() malloc(1) -#define vSemaphoreDelete(s) free(s) -#define queueQUEUE_TYPE_MUTEX ( ( uint8_t ) 1U -#define xTaskCreatePinnedToCore(a,b,c,d,e,f,g) *(f) = malloc(1) -#define xTaskCreateStaticPinnedToCore(a,b,c,d,e,f,g,h) ((void*)1) -#define vTaskDelay(m) usleep((m)*0) -#define esp_random() (rand()%UINT32_MAX) - - -#define ESP_TASK_PRIO_MAX 25 -#define ESP_TASKD_EVENT_PRIO 5 -#define _mdns_udp_pcb_write(tcpip_if, ip_protocol, ip, port, data, len) len -#define TaskHandle_t TaskHandle_t - - -typedef int32_t esp_err_t; - -typedef void *SemaphoreHandle_t; -typedef void *QueueHandle_t; -typedef void *TaskHandle_t; -typedef int BaseType_t; -typedef uint32_t TickType_t; -typedef void *StackType_t; -typedef void *StaticTask_t; - -struct udp_pcb { - uint8_t dummy; -}; - -struct ip4_addr { - uint32_t addr; -}; -typedef struct ip4_addr ip4_addr_t; - -struct ip6_addr { - uint32_t addr[4]; -}; -typedef struct ip6_addr ip6_addr_t; - -typedef void *system_event_t; - -struct pbuf { - struct pbuf *next; - void *payload; - uint16_t tot_len; - uint16_t len; - uint8_t /*pbuf_type*/ type; - uint8_t flags; - uint16_t ref; -}; - -uint32_t xTaskGetTickCount(void); -typedef void (*esp_timer_cb_t)(void *arg); - -// Queue mock -QueueHandle_t xQueueCreate(uint32_t uxQueueLength, - uint32_t uxItemSize); - -void vQueueDelete(QueueHandle_t xQueue); - -uint32_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait); - -uint32_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait); - -void GetLastItem(void *pvBuffer); - -void ForceTaskDelete(void); - -esp_err_t esp_event_handler_register(const char *event_base, int32_t event_id, void *event_handler, void *event_handler_arg); - -esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id, void *event_handler); - - -TaskHandle_t xTaskGetCurrentTaskHandle(void); -void xTaskNotifyGive(TaskHandle_t task); -BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time); - -static inline void xTaskGetStaticBuffers(void *pvTaskBuffer, void *pvStackBuffer, void *pvTaskTCB) -{ -} - -#endif //_ESP32_COMPAT_H_ diff --git a/components/mdns/tests/test_afl_fuzz_host/esp_attr.h b/components/mdns/tests/test_afl_fuzz_host/esp_attr.h deleted file mode 100644 index 75b66786aa..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/esp_attr.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -#pragma once -#define IRAM_ATTR -#define FLAG_ATTR(TYPE) -#define QUEUE_H -#define __ARCH_CC_H__ -#define __XTENSA_API_H__ -#define SSIZE_MAX INT_MAX -#define LWIP_HDR_IP6_ADDR_H -#define LWIP_HDR_IP4_ADDR_H diff --git a/components/mdns/tests/test_afl_fuzz_host/esp_netif_mock.c b/components/mdns/tests/test_afl_fuzz_host/esp_netif_mock.c deleted file mode 100644 index 80629ad63d..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/esp_netif_mock.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include "esp32_mock.h" - -typedef struct esp_netif_s esp_netif_t; -typedef struct esp_netif_ip_info esp_netif_ip_info_t; -typedef struct esp_netif_dhcp_status esp_netif_dhcp_status_t; - - -const char *IP_EVENT = "IP_EVENT"; - - -esp_err_t esp_netif_add_to_list(esp_netif_t *netif) -{ - return ESP_OK; -} - -esp_err_t esp_netif_remove_from_list(esp_netif_t *netif) -{ - return ESP_ERR_NOT_FOUND; -} - -esp_netif_t *esp_netif_next(esp_netif_t *netif) -{ - return NULL; -} - -esp_netif_t *esp_netif_next_unsafe(esp_netif_t *netif) -{ - return NULL; -} - -esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key) -{ - return NULL; -} - -esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) -{ - return ESP_ERR_NOT_SUPPORTED; -} - -esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) -{ - return ESP_ERR_NOT_SUPPORTED; -} diff --git a/components/mdns/tests/test_afl_fuzz_host/in/file2.bin b/components/mdns/tests/test_afl_fuzz_host/in/file2.bin deleted file mode 100644 index a7ce859cc0fdf7cef7985d51d480e03cef0bde7e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 33 kcmZQz00Kr3!N9*z=%+RBzQm% hBz1s?L6LzGXixSyIIkUy@wFnv;?$zD)FPJn;?g9x__U(Tk}8(?lH>x`oc!d(90mp+2F3$CAd-PW Ra1sL#XKrR*CRo)0NdPDN5l#RA diff --git a/components/mdns/tests/test_afl_fuzz_host/in/telnet_ptr.bin b/components/mdns/tests/test_afl_fuzz_host/in/telnet_ptr.bin deleted file mode 100644 index 1cd8e85d8021f55039566cdd619235aeb952ffa6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 72 zcmZQzXkh>XMi9XnUz}Q0mYJMd%pRYTSFBr{!V+JaQox#%pPZP(z`z4iB{+paggw3_ PH775%geATt872z=Qoj-} diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-14.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-14.bin deleted file mode 100644 index b9d059d8202682602e7232a8505c495a3047e3a4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 568 zcmaiwPfNov7{)Vc-GqW7j)|wjsGcf~S~hRiF&W~a2k#+k0~WgWB}sAnMEe#aev*0f zYnYmKpg5L8@;*Gj_g@gw=M=T;r$cDQq2$scG66d;6PhFBpq5SEqy*Ki;|>9y6_W8k z(juaoD$gKwot!QVViQiwP4*$FL_!N=w2csr$^a@cZR*xs(X_FMHk)!f+~gb+NOM1> zk_yQgof*O+&`j$PkuwH9@N&MeNR@)Rs7LhfhXIZT<8b&C;fD#&geg za3+LG&Fd@xs>KLTNcn;-*5pXh{6Hl3Gf@0Bv?l!J7wTd;niT zpQFAGPvJB)ejMCnH#5JT{b&BO0M9^cBj1M?YNV6sL|5fVbt9Ej+lyF*S{proq+$Y_ zy(EZB<0$aQ0F|dTfW0^Pc%S>15?e0gf=P$9ZRU2E6gBl1yUeqg)y}c?t9peygs&#} z7|j&Zdeo=X8zO0VLkn$-((43yUL=X?$Ick}!j;^kSawken}R!yj@dNbnpQ!IPSfSw z6t(+@>poibXblSGtk>cz>tEHauqc}6a-MwrlizE$|MzI+T(JBX1>O+*nyq$)j zQylnX9d7k~Ow=qiw8BP1a9+~%qG`nQ)xD8Uv1!|NXD5mA#_QH@&$~uSCEPYe#h+j? zazWa{-WhziU)BS1Dn2W)3#)mrXUl8}NBQ(&0aZF-|Mg&HNV7tR6XMi3VDD9S7@$yYB{2+7P%Rd7x$C@#%O<%mzrEGoz^Dq)E)NiJZ`$xlwq zVPFtw0BRAO!mwg_d7`hQt+|1&xv{RZldhAAuCt}Cld(<_s<4x;fr+lGIY`(DNi{^+ z0x0a4YnzvpS*%-zV0U=oA+vm)V+|Sr{1T8R{fu1*g~=8yw(a(1*I26Ub&|U}#`) F005APLjnK* diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-28.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-28.bin deleted file mode 100644 index 537352ae67db92f820ff0d1299dae3534eef1f48..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 62 zcmZQz00Kr3!N4HuQIuI+lCNH@5R#djs^FYhP+Xdm${nAWR*+woT2zu;z!DE6Sab4| L6LT0C6dM=;y^9dW diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-29.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-29.bin deleted file mode 100644 index 837737b32e12e3f33ff3f1c3b59f6ff351b05fba..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 39 qcmZQz00Jfu!N9XCLm_y%+1WpOe@MPsbbB^PfpBXV328G1WHse2>fdR(m)`h7xI6{k-wY= bco-O=ax4c{{1X83^`U$YAe+U3fq?@6q#+cG diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-53.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-53.bin deleted file mode 100644 index 73181ea8c378d33efed725aa209387113edffdd3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 140 zcmZQz00Jf;1Ok@$;@l*b_>$xT)|~w0#2f|&9tKA7;QX}A%0ykyvc$anvczIt-^666 z{QT?#VhoH7j0bo?Y8V)}zcR>5K~yS0RX%E%S*!q4%mY{WjX^@xqbReuBwxK)AtW<5 PRlzy2ptv+A^#Bh5gdisG diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-56.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-56.bin deleted file mode 100644 index 980b6516a20908c4b681b1cf3717c7ef35f062b6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 262 zcmZQzXkh>XCLm@L_9)6MF3Eq?Ftb>}H!)cuH#0AjCq5;$EHgP(H#09SpC!H|xqvk% zKRGdnfkA+Qk%2*Q3WJP5ZhlH?jxA6JNQ0S<5m$b3MOkW5v8|yQOMG!|(g9rt9;jk5 zY<3)oKTyCR&;T)xkpak21aTND7=%D>W8BLLGB~X$v!v>P(SfB5GEiB8e+?jc1_lwm xkpDZ5{N)77F+$~74y^bm05nM-YAh#^&C0;gz~BH>1QrAu&jDn!I5049006}!N5=pF diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-63.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-63.bin deleted file mode 100644 index 883d444ba269c8938aa87f3ba8da53a1305f9763..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 147 zcmZQz00Jf;1Oo2(#I%C^vecrIAisgkVmtU4x ztm~VY?3ACMeL$9hk%9354@eEr1V;v0DTqo1sLDqTGm8~qig}O}N{D(CWfqs@s~0PT SWag$SI42eqm*%7%-~j;3q9}|2 diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-83.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-83.bin deleted file mode 100644 index c6cc159a2597bc35b4316a796af18534bfee7dc3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 105 zcmZQzXkh>XMj&Pq56(}^tW4DPEKAJGFH0=e^-WB6%FoYc&B;$r%mFHD04b|rU^%ej opU43o1{o+{;9mn!90-_XHvj*(sc11!P9G}A0c5i{Ffecc09-p6=Kufz diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-88.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-88.bin deleted file mode 100644 index fcbb384438a28961526215b8b66e32193b6fd0e8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 48 xcmZQz00Jfu!N9;0UzC=_5?_*Bz?zevoS4JFz{9}E5ucb@RFGd(azGHo1pvO@39SGC diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-89.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-89.bin deleted file mode 100644 index d60a987cad92fe5e9e86e83ea8d2ee16f7fe93e2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 459 zcmZut!AiqG6nsjH6?&*>t%!$uQZQS0laj>4LYts?QG|-ulBSVH(x&XjT5mmg=~3_> ztY`gzT*ZHizSvY0e6Z}id9$-S3-AC~%eYnwZO0eGP(-mV!P{&eAgrX;5v-UP|9p7oBgos1w8f0EOdo2ePiw} zNje}4^KDp~;Po(uTs%uyd;q1i$LGwR@0_zHHz+YQVq3(j6Z?o*nj-$v7NK==Y^1dM zOFgGeq|f_&(D5aSJQ-nY_c&vUuzMxFc@pCZ#o|bZ%N*uF-f3J6VQOGQliZLVkQEZ||>|XE&P_Qhxn0o}&II^Gj0M Z$Nq4kL7Zy<6!6?PhySpOI1ey^#uu&jeZK$z diff --git a/components/mdns/tests/test_afl_fuzz_host/in/test-95.bin b/components/mdns/tests/test_afl_fuzz_host/in/test-95.bin deleted file mode 100644 index 26553090fa6d7cddb5fb7c748fc72d8d20c77364..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 286 zcmZQz00L$PHW0-fpO{vVUzS=_l3c(N4XCLm@O^(e|LF3DFfRtU+=O;vDCEGRC`N#%)8NiEAvPSwrKOUq}8FG((7 z&B;$r%wb>1yv^6)dGB>tzcCvCZv2wPwax&IQObK(dHK;H&FfedS$u_cu z^DV$~ezs8iK?*}{O-<7hlaefwbQ8^tlXOi@Obm4`EzC`H%~Dg6Obtv@lZ*`#fi^3G pY-XrnkOhSV1E&Zi7>ad4{?UaRc)$dxP#@|pb->payload; -} - -static inline size_t _mdns_get_packet_len(mdns_rx_packet_t *packet) -{ - return packet->pb->len; -} - -static inline void _mdns_packet_free(mdns_rx_packet_t *packet) -{ - free(packet->pb); - free(packet); -} - -static inline bool mdns_is_netif_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) -{ - return true; -} diff --git a/components/mdns/tests/test_afl_fuzz_host/sdkconfig.h b/components/mdns/tests/test_afl_fuzz_host/sdkconfig.h deleted file mode 100644 index da75d14752..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/sdkconfig.h +++ /dev/null @@ -1,424 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -/* - * This config file commited in order to not run `idf.py reconfigure` each time when running fuzzer test. You can modify it manually or run `idf.py reconfigure` to generate new one if needed. - * Espressif IoT Development Framework (ESP-IDF) Configuration Header - */ -#pragma once -#define CONFIG_IDF_TARGET "esp32" -#define CONFIG_IDF_TARGET_ESP32 1 -#define CONFIG_IDF_FIRMWARE_CHIP_ID 0x0000 -#define CONFIG_SDK_TOOLPREFIX "xtensa-esp32-elf-" -#define CONFIG_APP_BUILD_TYPE_APP_2NDBOOT 1 -#define CONFIG_APP_BUILD_GENERATE_BINARIES 1 -#define CONFIG_APP_BUILD_BOOTLOADER 1 -#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1 -#define CONFIG_APP_COMPILE_TIME_DATE 1 -#define CONFIG_APP_RETRIEVE_LEN_ELF_SHA 16 -#define CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE 1 -#define CONFIG_BOOTLOADER_LOG_LEVEL_INFO 1 -#define CONFIG_BOOTLOADER_LOG_LEVEL 3 -#define CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V 1 -#define CONFIG_BOOTLOADER_WDT_ENABLE 1 -#define CONFIG_BOOTLOADER_WDT_TIME_MS 9000 -#define CONFIG_BOOTLOADER_RESERVE_RTC_SIZE 0x0 -#define CONFIG_ESPTOOLPY_WITH_STUB 1 -#define CONFIG_ESPTOOLPY_FLASHMODE_DIO 1 -#define CONFIG_ESPTOOLPY_FLASHMODE "dio" -#define CONFIG_ESPTOOLPY_FLASHFREQ_40M 1 -#define CONFIG_ESPTOOLPY_FLASHFREQ "40m" -#define CONFIG_ESPTOOLPY_FLASHSIZE_2MB 1 -#define CONFIG_ESPTOOLPY_FLASHSIZE "2MB" -#define CONFIG_ESPTOOLPY_FLASHSIZE_DETECT 1 -#define CONFIG_ESPTOOLPY_BEFORE_RESET 1 -#define CONFIG_ESPTOOLPY_BEFORE "default_reset" -#define CONFIG_ESPTOOLPY_AFTER_RESET 1 -#define CONFIG_ESPTOOLPY_AFTER "hard_reset" -#define CONFIG_PARTITION_TABLE_SINGLE_APP 1 -#define CONFIG_PARTITION_TABLE_CUSTOM_FILENAME "partitions.csv" -#define CONFIG_PARTITION_TABLE_FILENAME "partitions_singleapp.csv" -#define CONFIG_PARTITION_TABLE_OFFSET 0x8000 -#define CONFIG_PARTITION_TABLE_MD5 1 -#define CONFIG_COMPILER_OPTIMIZATION_DEFAULT 1 -#define CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE 1 -#define CONFIG_COMPILER_STACK_CHECK_MODE_NONE 1 -#define CONFIG_APPTRACE_DEST_NONE 1 -#define CONFIG_APPTRACE_LOCK_ENABLE 1 -#define CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF 0 -#define CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF 0 -#define CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF 0 -#define CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF 0 -#define CONFIG_BTDM_CTRL_PINNED_TO_CORE 0 -#define CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF 1 -#define CONFIG_BTDM_RESERVE_DRAM 0x0 -#define CONFIG_COAP_MBEDTLS_PSK 1 -#define CONFIG_COAP_LOG_DEFAULT_LEVEL 0 -#define CONFIG_ADC_DISABLE_DAC 1 -#define CONFIG_SPI_MASTER_ISR_IN_IRAM 1 -#define CONFIG_SPI_SLAVE_ISR_IN_IRAM 1 -#define CONFIG_EFUSE_CODE_SCHEME_COMPAT_3_4 1 -#define CONFIG_EFUSE_MAX_BLK_LEN 192 -#define CONFIG_ESP_TLS_USING_MBEDTLS 1 -#define CONFIG_ESP32_REV_MIN_0 1 -#define CONFIG_ESP32_REV_MIN 0 -#define CONFIG_ESP32_DPORT_WORKAROUND 1 -#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160 1 -#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ 160 -#define CONFIG_ESP32_TRACEMEM_RESERVE_DRAM 0x0 -#define CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR 1 -#define CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES 4 -#define CONFIG_ULP_COPROC_RESERVE_MEM 0 -#define CONFIG_ESP_DEBUG_OCDAWARE 1 -#define CONFIG_ESP_BROWNOUT_DET 1 -#define CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0 1 -#define CONFIG_ESP_BROWNOUT_DET_LVL 0 -#define CONFIG_ESP32_REDUCE_PHY_TX_POWER 1 -#define CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC_HRT 1 -#define CONFIG_RTC_CLK_SRC_INT_RC 1 -#define CONFIG_RTC_CLK_CAL_CYCLES 1024 -#define CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY 2000 -#define CONFIG_ESP32_XTAL_FREQ_40 1 -#define CONFIG_ESP32_XTAL_FREQ 40 -#define CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL 5 -#define CONFIG_ADC_CAL_EFUSE_TP_ENABLE 1 -#define CONFIG_ADC_CAL_EFUSE_VREF_ENABLE 1 -#define CONFIG_ADC_CAL_LUT_ENABLE 1 -#define CONFIG_ESP_ERR_TO_NAME_LOOKUP 1 -#define CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE 32 -#define CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE 2304 -#define CONFIG_ESP_MAIN_TASK_STACK_SIZE 3584 -#define CONFIG_ESP_IPC_TASK_STACK_SIZE 1024 -#define CONFIG_ESP_IPC_USES_CALLERS_PRIORITY 1 -#define CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE 2048 -#define CONFIG_ESP_CONSOLE_UART_DEFAULT 1 -#define CONFIG_ESP_CONSOLE_UART_NUM 0 -#define CONFIG_ESP_CONSOLE_UART_TX_GPIO 1 -#define CONFIG_ESP_CONSOLE_UART_RX_GPIO 3 -#define CONFIG_ESP_CONSOLE_UART_BAUDRATE 115200 -#define CONFIG_ESP_INT_WDT 1 -#define CONFIG_ESP_INT_WDT_TIMEOUT_MS 300 -#define CONFIG_ESP_INT_WDT_CHECK_CPU1 1 -#define CONFIG_ESP_TASK_WDT 1 -#define CONFIG_ESP_TASK_WDT_TIMEOUT_S 5 -#define CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 1 -#define CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 1 -#define CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA 1 -#define CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP 1 -#define CONFIG_ESP_MAC_ADDR_UNIVERSE_BT 1 -#define CONFIG_ESP_MAC_ADDR_UNIVERSE_BT_OFFSET 2 -#define CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH 1 -#define CONFIG_ETH_ENABLED 1 -#define CONFIG_ETH_USE_ESP32_EMAC 1 -#define CONFIG_ETH_PHY_INTERFACE_RMII 1 -#define CONFIG_ETH_RMII_CLK_INPUT 1 -#define CONFIG_ETH_RMII_CLK_IN_GPIO 0 -#define CONFIG_ETH_DMA_BUFFER_SIZE 512 -#define CONFIG_ETH_DMA_RX_BUFFER_NUM 10 -#define CONFIG_ETH_DMA_TX_BUFFER_NUM 10 -#define CONFIG_ETH_USE_SPI_ETHERNET 1 -#define CONFIG_ESP_EVENT_POST_FROM_ISR 1 -#define CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR 1 -#define CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS 1 -#define CONFIG_HTTPD_MAX_REQ_HDR_LEN 512 -#define CONFIG_HTTPD_MAX_URI_LEN 512 -#define CONFIG_HTTPD_ERR_RESP_NO_DELAY 1 -#define CONFIG_HTTPD_PURGE_BUF_LEN 32 -#define CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL 120 -#define CONFIG_ESP_NETIF_TCPIP_LWIP 1 -#define CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT 1 -#define CONFIG_ESP_TIMER_TASK_STACK_SIZE 3584 -#define CONFIG_ESP_TIMER_IMPL_TG0_LAC 1 -#define CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM 10 -#define CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM 32 -#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER 1 -#define CONFIG_ESP32_WIFI_TX_BUFFER_TYPE 1 -#define CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM 32 -#define CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED 1 -#define CONFIG_ESP32_WIFI_TX_BA_WIN 6 -#define CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED 1 -#define CONFIG_ESP32_WIFI_RX_BA_WIN 6 -#define CONFIG_ESP32_WIFI_NVS_ENABLED 1 -#define CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0 1 -#define CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN 752 -#define CONFIG_ESP32_WIFI_MGMT_SBUF_NUM 32 -#define CONFIG_ESP32_WIFI_IRAM_OPT 1 -#define CONFIG_ESP32_WIFI_RX_IRAM_OPT 1 -#define CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE 1 -#define CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE 1 -#define CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER 20 -#define CONFIG_ESP32_PHY_MAX_TX_POWER 20 -#define CONFIG_ESP_COREDUMP_ENABLE_TO_NONE 1 -#define CONFIG_FATFS_CODEPAGE_437 1 -#define CONFIG_FATFS_CODEPAGE 437 -#define CONFIG_FATFS_LFN_NONE 1 -#define CONFIG_FATFS_FS_LOCK 0 -#define CONFIG_FATFS_TIMEOUT_MS 10000 -#define CONFIG_FATFS_PER_FILE_CACHE 1 -#define CONFIG_FMB_COMM_MODE_RTU_EN 1 -#define CONFIG_FMB_COMM_MODE_ASCII_EN 1 -#define CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND 150 -#define CONFIG_FMB_MASTER_DELAY_MS_CONVERT 200 -#define CONFIG_FMB_QUEUE_LENGTH 20 -#define CONFIG_FMB_SERIAL_TASK_STACK_SIZE 2048 -#define CONFIG_FMB_SERIAL_BUF_SIZE 256 -#define CONFIG_FMB_SERIAL_ASCII_BITS_PER_SYMB 8 -#define CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS 1000 -#define CONFIG_FMB_SERIAL_TASK_PRIO 10 -#define CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT 20 -#define CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE 20 -#define CONFIG_FMB_CONTROLLER_STACK_SIZE 4096 -#define CONFIG_FMB_EVENT_QUEUE_TIMEOUT 20 -#define CONFIG_FMB_TIMER_PORT_ENABLED 1 -#define CONFIG_FMB_TIMER_GROUP 0 -#define CONFIG_FMB_TIMER_INDEX 0 -#define CONFIG_FREERTOS_NO_AFFINITY 0x7FFFFFFF -#define CONFIG_FREERTOS_CORETIMER_0 1 -#define CONFIG_FREERTOS_HZ 100 -#define CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION 1 -#define CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY 1 -#define CONFIG_FREERTOS_INTERRUPT_BACKTRACE 1 -#define CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS 1 -#define CONFIG_FREERTOS_IDLE_TASK_STACKSIZE 1536 -#define CONFIG_FREERTOS_ISR_STACKSIZE 1536 -#define CONFIG_FREERTOS_MAX_TASK_NAME_LEN 16 -#define CONFIG_FREERTOS_TIMER_TASK_PRIORITY 1 -#define CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH 2048 -#define CONFIG_FREERTOS_TIMER_QUEUE_LENGTH 10 -#define CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE 0 -#define CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER 1 -#define CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER 1 -#define CONFIG_FREERTOS_DEBUG_OCDAWARE 1 -#define CONFIG_HEAP_POISONING_DISABLED 1 -#define CONFIG_HEAP_TRACING_OFF 1 -#define CONFIG_LOG_DEFAULT_LEVEL_INFO 1 -#define CONFIG_LOG_DEFAULT_LEVEL 3 -#define CONFIG_LOG_MAXIMUM_LEVEL 3 -#define CONFIG_LOG_COLORS 1 -#define CONFIG_LOG_TIMESTAMP_SOURCE_RTOS 1 -#define CONFIG_LWIP_LOCAL_HOSTNAME "espressif" -#define CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES 1 -#define CONFIG_LWIP_TIMERS_ONDEMAND 1 -#define CONFIG_LWIP_MAX_SOCKETS 10 -#define CONFIG_LWIP_SO_REUSE 1 -#define CONFIG_LWIP_SO_REUSE_RXTOALL 1 -#define CONFIG_LWIP_IP_FRAG 1 -#define CONFIG_LWIP_ESP_GRATUITOUS_ARP 1 -#define CONFIG_LWIP_GARP_TMR_INTERVAL 60 -#define CONFIG_LWIP_TCPIP_RECVMBOX_SIZE 32 -#define CONFIG_LWIP_DHCP_DOES_ARP_CHECK 1 -#define CONFIG_LWIP_DHCPS_LEASE_UNIT 60 -#define CONFIG_LWIP_DHCPS_MAX_STATION_NUM 8 -#define CONFIG_LWIP_NETIF_LOOPBACK 1 -#define CONFIG_LWIP_LOOPBACK_MAX_PBUFS 8 -#define CONFIG_LWIP_MAX_ACTIVE_TCP 16 -#define CONFIG_LWIP_MAX_LISTENING_TCP 16 -#define CONFIG_LWIP_TCP_MAXRTX 12 -#define CONFIG_LWIP_TCP_SYNMAXRTX 6 -#define CONFIG_LWIP_TCP_MSS 1440 -#define CONFIG_LWIP_TCP_TMR_INTERVAL 250 -#define CONFIG_LWIP_TCP_MSL 60000 -#define CONFIG_LWIP_TCP_SND_BUF_DEFAULT 5744 -#define CONFIG_LWIP_TCP_WND_DEFAULT 5744 -#define CONFIG_LWIP_TCP_RECVMBOX_SIZE 6 -#define CONFIG_LWIP_TCP_QUEUE_OOSEQ 1 -#define CONFIG_LWIP_TCP_OVERSIZE_MSS 1 -#define CONFIG_LWIP_MAX_UDP_PCBS 16 -#define CONFIG_LWIP_UDP_RECVMBOX_SIZE 6 -#define CONFIG_LWIP_TCPIP_TASK_STACK_SIZE 3072 -#define CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY 1 -#define CONFIG_LWIP_TCPIP_TASK_AFFINITY 0x7FFFFFFF -#define CONFIG_LWIP_MAX_RAW_PCBS 16 -#define CONFIG_LWIP_DHCP_MAX_NTP_SERVERS 1 -#define CONFIG_LWIP_SNTP_UPDATE_DELAY 3600000 -#define CONFIG_LWIP_ESP_LWIP_ASSERT 1 -#define CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC 1 -#define CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN 1 -#define CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN 16384 -#define CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN 4096 -#define CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 1 -#define CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL 1 -#define CONFIG_MBEDTLS_HARDWARE_AES 1 -#define CONFIG_MBEDTLS_HARDWARE_MPI 1 -#define CONFIG_MBEDTLS_HARDWARE_SHA 1 -#define CONFIG_MBEDTLS_HAVE_TIME 1 -#define CONFIG_MBEDTLS_ECDSA_DETERMINISTIC 1 -#define CONFIG_MBEDTLS_SHA512_C 1 -#define CONFIG_MBEDTLS_TLS_SERVER_AND_CLIENT 1 -#define CONFIG_MBEDTLS_TLS_SERVER 1 -#define CONFIG_MBEDTLS_TLS_CLIENT 1 -#define CONFIG_MBEDTLS_TLS_ENABLED 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_RSA 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_DHE_RSA 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_ELLIPTIC_CURVE 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA 1 -#define CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_RSA 1 -#define CONFIG_MBEDTLS_SSL_RENEGOTIATION 1 -#define CONFIG_MBEDTLS_SSL_PROTO_TLS1 1 -#define CONFIG_MBEDTLS_SSL_PROTO_TLS1_1 1 -#define CONFIG_MBEDTLS_SSL_PROTO_TLS1_2 1 -#define CONFIG_MBEDTLS_SSL_ALPN 1 -#define CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS 1 -#define CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS 1 -#define CONFIG_MBEDTLS_AES_C 1 -#define CONFIG_MBEDTLS_RC4_DISABLED 1 -#define CONFIG_MBEDTLS_CCM_C 1 -#define CONFIG_MBEDTLS_GCM_C 1 -#define CONFIG_MBEDTLS_PEM_PARSE_C 1 -#define CONFIG_MBEDTLS_PEM_WRITE_C 1 -#define CONFIG_MBEDTLS_X509_CRL_PARSE_C 1 -#define CONFIG_MBEDTLS_X509_CSR_PARSE_C 1 -#define CONFIG_MBEDTLS_ECP_C 1 -#define CONFIG_MBEDTLS_ECDH_C 1 -#define CONFIG_MBEDTLS_ECDSA_C 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP192R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP224R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP384R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP521R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP192K1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP224K1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_SECP256K1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_BP256R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_BP384R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_BP512R1_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED 1 -#define CONFIG_MBEDTLS_ECP_NIST_OPTIM 1 -#define CONFIG_MDNS_MAX_SERVICES 25 -#define CONFIG_MDNS_MAX_INTERFACES 3 -#define CONFIG_MDNS_TASK_PRIORITY 1 -#define CONFIG_MDNS_ACTION_QUEUE_LEN 16 -#define CONFIG_MDNS_TASK_STACK_SIZE 4096 -#define CONFIG_MDNS_TASK_AFFINITY_CPU0 1 -#define CONFIG_MDNS_TASK_AFFINITY 0x0 -#define CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS 1 -#define CONFIG_MDNS_TIMER_PERIOD_MS 100 -#define CONFIG_MQTT_PROTOCOL_311 1 -#define CONFIG_MQTT_TRANSPORT_SSL 1 -#define CONFIG_MQTT_TRANSPORT_WEBSOCKET 1 -#define CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE 1 -#define CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF 1 -#define CONFIG_NEWLIB_STDIN_LINE_ENDING_CR 1 -#define CONFIG_OPENSSL_ASSERT_EXIT 1 -#define CONFIG_PTHREAD_TASK_PRIO_DEFAULT 5 -#define CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT 3072 -#define CONFIG_PTHREAD_STACK_MIN 768 -#define CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY 1 -#define CONFIG_PTHREAD_TASK_CORE_DEFAULT -1 -#define CONFIG_PTHREAD_TASK_NAME_DEFAULT "pthread" -#define CONFIG_SPI_FLASH_ROM_DRIVER_PATCH 1 -#define CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS 1 -#define CONFIG_SPI_FLASH_YIELD_DURING_ERASE 1 -#define CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS 20 -#define CONFIG_SPI_FLASH_ERASE_YIELD_TICKS 1 -#define CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP 1 -#define CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP 1 -#define CONFIG_SPI_FLASH_SUPPORT_GD_CHIP 1 -#define CONFIG_SPIFFS_MAX_PARTITIONS 3 -#define CONFIG_SPIFFS_CACHE 1 -#define CONFIG_SPIFFS_CACHE_WR 1 -#define CONFIG_SPIFFS_PAGE_CHECK 1 -#define CONFIG_SPIFFS_GC_MAX_RUNS 10 -#define CONFIG_SPIFFS_PAGE_SIZE 256 -#define CONFIG_SPIFFS_OBJ_NAME_LEN 32 -#define CONFIG_SPIFFS_USE_MAGIC 1 -#define CONFIG_SPIFFS_USE_MAGIC_LENGTH 1 -#define CONFIG_SPIFFS_META_LENGTH 4 -#define CONFIG_SPIFFS_USE_MTIME 1 -#define CONFIG_USB_DESC_CUSTOM_VID 0x1234 -#define CONFIG_USB_DESC_CUSTOM_PID 0x5678 -#define CONFIG_UNITY_ENABLE_FLOAT 1 -#define CONFIG_UNITY_ENABLE_DOUBLE 1 -#define CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER 1 -#define CONFIG_VFS_SUPPORT_IO 1 -#define CONFIG_VFS_SUPPORT_DIR 1 -#define CONFIG_VFS_SUPPORT_SELECT 1 -#define CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT 1 -#define CONFIG_VFS_SUPPORT_TERMIOS 1 -#define CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS 1 -#define CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN 128 -#define CONFIG_WL_SECTOR_SIZE_4096 1 -#define CONFIG_WL_SECTOR_SIZE 4096 -#define CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES 16 -#define CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT 30 -#define CONFIG_WPA_MBEDTLS_CRYPTO 1 - -/* List of deprecated options */ -#define CONFIG_ADC2_DISABLE_DAC CONFIG_ADC_DISABLE_DAC -#define CONFIG_BROWNOUT_DET CONFIG_ESP_BROWNOUT_DET -#define CONFIG_BROWNOUT_DET_LVL_SEL_0 CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0 -#define CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG CONFIG_COMPILER_OPTIMIZATION_DEFAULT -#define CONFIG_CONSOLE_UART_BAUDRATE CONFIG_ESP_CONSOLE_UART_BAUDRATE -#define CONFIG_CONSOLE_UART_DEFAULT CONFIG_ESP_CONSOLE_UART_DEFAULT -#define CONFIG_CONSOLE_UART_RX_GPIO CONFIG_ESP_CONSOLE_UART_RX_GPIO -#define CONFIG_CONSOLE_UART_TX_GPIO CONFIG_ESP_CONSOLE_UART_TX_GPIO -#define CONFIG_ESP32S2_PANIC_PRINT_REBOOT CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT -#define CONFIG_ESP32_APPTRACE_DEST_NONE CONFIG_APPTRACE_DEST_NONE -#define CONFIG_ESP32_DEFAULT_PTHREAD_CORE_NO_AFFINITY CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY -#define CONFIG_ESP32_PANIC_PRINT_REBOOT CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT -#define CONFIG_ESP32_PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN -#define CONFIG_ESP32_PTHREAD_TASK_NAME_DEFAULT CONFIG_PTHREAD_TASK_NAME_DEFAULT -#define CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT CONFIG_PTHREAD_TASK_PRIO_DEFAULT -#define CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT -#define CONFIG_ESP_GRATUITOUS_ARP CONFIG_LWIP_ESP_GRATUITOUS_ARP -#define CONFIG_FLASHMODE_DIO CONFIG_ESPTOOLPY_FLASHMODE_DIO -#define CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR -#define CONFIG_GARP_TMR_INTERVAL CONFIG_LWIP_GARP_TMR_INTERVAL -#define CONFIG_INT_WDT CONFIG_ESP_INT_WDT -#define CONFIG_INT_WDT_CHECK_CPU1 CONFIG_ESP_INT_WDT_CHECK_CPU1 -#define CONFIG_INT_WDT_TIMEOUT_MS CONFIG_ESP_INT_WDT_TIMEOUT_MS -#define CONFIG_IPC_TASK_STACK_SIZE CONFIG_ESP_IPC_TASK_STACK_SIZE -#define CONFIG_LOG_BOOTLOADER_LEVEL_INFO CONFIG_BOOTLOADER_LOG_LEVEL_INFO -#define CONFIG_MAIN_TASK_STACK_SIZE CONFIG_ESP_MAIN_TASK_STACK_SIZE -#define CONFIG_MB_CONTROLLER_NOTIFY_QUEUE_SIZE CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE -#define CONFIG_MB_CONTROLLER_NOTIFY_TIMEOUT CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT -#define CONFIG_MB_CONTROLLER_STACK_SIZE CONFIG_FMB_CONTROLLER_STACK_SIZE -#define CONFIG_MB_EVENT_QUEUE_TIMEOUT CONFIG_FMB_EVENT_QUEUE_TIMEOUT -#define CONFIG_MB_MASTER_DELAY_MS_CONVERT CONFIG_FMB_MASTER_DELAY_MS_CONVERT -#define CONFIG_MB_MASTER_TIMEOUT_MS_RESPOND CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND -#define CONFIG_MB_QUEUE_LENGTH CONFIG_FMB_QUEUE_LENGTH -#define CONFIG_MB_SERIAL_BUF_SIZE CONFIG_FMB_SERIAL_BUF_SIZE -#define CONFIG_MB_SERIAL_TASK_PRIO CONFIG_FMB_SERIAL_TASK_PRIO -#define CONFIG_MB_SERIAL_TASK_STACK_SIZE CONFIG_FMB_SERIAL_TASK_STACK_SIZE -#define CONFIG_MB_TIMER_GROUP CONFIG_FMB_TIMER_GROUP -#define CONFIG_MB_TIMER_INDEX CONFIG_FMB_TIMER_INDEX -#define CONFIG_MB_TIMER_PORT_ENABLED CONFIG_FMB_TIMER_PORT_ENABLED -#define CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE -#define CONFIG_OPTIMIZATION_LEVEL_DEBUG CONFIG_COMPILER_OPTIMIZATION_DEFAULT -#define CONFIG_POST_EVENTS_FROM_IRAM_ISR CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR -#define CONFIG_POST_EVENTS_FROM_ISR CONFIG_ESP_EVENT_POST_FROM_ISR -#define CONFIG_REDUCE_PHY_TX_POWER CONFIG_ESP32_REDUCE_PHY_TX_POWER -#define CONFIG_SEMIHOSTFS_HOST_PATH_MAX_LEN CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN -#define CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS -#define CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS -#define CONFIG_STACK_CHECK_NONE CONFIG_COMPILER_STACK_CHECK_MODE_NONE -#define CONFIG_SUPPORT_TERMIOS CONFIG_VFS_SUPPORT_TERMIOS -#define CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT -#define CONFIG_SYSTEM_EVENT_QUEUE_SIZE CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE -#define CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE -#define CONFIG_TASK_WDT CONFIG_ESP_TASK_WDT -#define CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0 CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 -#define CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1 CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 -#define CONFIG_TASK_WDT_TIMEOUT_S CONFIG_ESP_TASK_WDT_TIMEOUT_S -#define CONFIG_TCPIP_RECVMBOX_SIZE CONFIG_LWIP_TCPIP_RECVMBOX_SIZE -#define CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY -#define CONFIG_TCPIP_TASK_STACK_SIZE CONFIG_LWIP_TCPIP_TASK_STACK_SIZE -#define CONFIG_TCP_MAXRTX CONFIG_LWIP_TCP_MAXRTX -#define CONFIG_TCP_MSL CONFIG_LWIP_TCP_MSL -#define CONFIG_TCP_MSS CONFIG_LWIP_TCP_MSS -#define CONFIG_TCP_OVERSIZE_MSS CONFIG_LWIP_TCP_OVERSIZE_MSS -#define CONFIG_TCP_QUEUE_OOSEQ CONFIG_LWIP_TCP_QUEUE_OOSEQ -#define CONFIG_TCP_RECVMBOX_SIZE CONFIG_LWIP_TCP_RECVMBOX_SIZE -#define CONFIG_TCP_SND_BUF_DEFAULT CONFIG_LWIP_TCP_SND_BUF_DEFAULT -#define CONFIG_TCP_SYNMAXRTX CONFIG_LWIP_TCP_SYNMAXRTX -#define CONFIG_TCP_WND_DEFAULT CONFIG_LWIP_TCP_WND_DEFAULT -#define CONFIG_TIMER_QUEUE_LENGTH CONFIG_FREERTOS_TIMER_QUEUE_LENGTH -#define CONFIG_TIMER_TASK_PRIORITY CONFIG_FREERTOS_TIMER_TASK_PRIORITY -#define CONFIG_TIMER_TASK_STACK_DEPTH CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH -#define CONFIG_TIMER_TASK_STACK_SIZE CONFIG_ESP_TIMER_TASK_STACK_SIZE -#define CONFIG_TOOLPREFIX CONFIG_SDK_TOOLPREFIX -#define CONFIG_UDP_RECVMBOX_SIZE CONFIG_LWIP_UDP_RECVMBOX_SIZE diff --git a/components/mdns/tests/test_afl_fuzz_host/test.c b/components/mdns/tests/test_afl_fuzz_host/test.c deleted file mode 100644 index d753dc9609..0000000000 --- a/components/mdns/tests/test_afl_fuzz_host/test.c +++ /dev/null @@ -1,263 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include - -#include "esp32_mock.h" -#include "mdns.h" -#include "mdns_private.h" - -// -// Global stuctures containing packet payload, search -mdns_rx_packet_t g_packet; -struct pbuf mypbuf; -mdns_search_once_t *search = NULL; - -// -// Dependency injected test functions -void mdns_test_execute_action(void *action); -mdns_srv_item_t *mdns_test_mdns_get_service_item(const char *service, const char *proto); -mdns_search_once_t *mdns_test_search_init(const char *name, const char *service, const char *proto, uint16_t type, uint32_t timeout, uint8_t max_results); -esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t *search); -void mdns_test_search_free(mdns_search_once_t *search); -void mdns_test_init_di(void); -extern mdns_server_t *_mdns_server; - -// -// mdns function wrappers for mdns setup in test mode -static int mdns_test_hostname_set(const char *mdns_hostname) -{ - for (int i = 0; i < MDNS_MAX_INTERFACES; i++) { - _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V4].state = PCB_RUNNING; // mark the PCB running to exercise mdns in fully operational mode - _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V6].state = PCB_RUNNING; - } - int ret = mdns_hostname_set(mdns_hostname); - mdns_action_t *a = NULL; - GetLastItem(&a); - mdns_test_execute_action(a); - return ret; -} - -static int mdns_test_add_delegated_host(const char *mdns_hostname) -{ - mdns_ip_addr_t addr = { .addr = { .u_addr = ESP_IPADDR_TYPE_V4 } }; - addr.addr.u_addr.ip4.addr = 0x11111111; - int ret = mdns_delegate_hostname_add(mdns_hostname, &addr); - mdns_action_t *a = NULL; - GetLastItem(&a); - mdns_test_execute_action(a); - return ret; -} - - -static int mdns_test_service_instance_name_set(const char *service, const char *proto, const char *instance) -{ - int ret = mdns_service_instance_name_set(service, proto, instance); - mdns_action_t *a = NULL; - GetLastItem(&a); - mdns_test_execute_action(a); - return ret; -} - -static int mdns_test_service_txt_set(const char *service, const char *proto, uint8_t num_items, mdns_txt_item_t txt[]) -{ - int ret = mdns_service_txt_set(service, proto, txt, num_items); - mdns_action_t *a = NULL; - GetLastItem(&a); - mdns_test_execute_action(a); - return ret; -} - -static int mdns_test_sub_service_add(const char *sub_name, const char *service_name, const char *proto, uint32_t port) -{ - if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) { - return ESP_FAIL; - } - - if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) { - return ESP_FAIL; - } - return mdns_service_subtype_add_for_host(NULL, service_name, proto, NULL, sub_name); -} - -static int mdns_test_service_add(const char *service_name, const char *proto, uint32_t port) -{ - if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) { - return ESP_FAIL; - } - - if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) { - return ESP_FAIL; - } - return ESP_OK; -} - -static mdns_result_t *mdns_test_query(const char *name, const char *service, const char *proto, uint16_t type) -{ - search = mdns_test_search_init(name, service, proto, type, 3000, 20); - if (!search) { - abort(); - } - - if (mdns_test_send_search_action(ACTION_SEARCH_ADD, search)) { - mdns_test_search_free(search); - abort(); - } - - mdns_action_t *a = NULL; - GetLastItem(&a); - mdns_test_execute_action(a); - return NULL; -} - -static void mdns_test_query_free(void) -{ - mdns_test_search_free(search); -} - -// -// function "under test" where afl-mangled packets passed -// -void mdns_parse_packet(mdns_rx_packet_t *packet); - -// -// Test starts here -// -int main(int argc, char **argv) -{ - int i; - const char *mdns_hostname = "minifritz"; - const char *mdns_instance = "Hristo's Time Capsule"; - mdns_txt_item_t arduTxtData[4] = { - {"board", "esp32"}, - {"tcp_check", "no"}, - {"ssh_upload", "no"}, - {"auth_upload", "no"} - }; - - const uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x32}; - - uint8_t buf[1460]; - char winstance[21 + strlen(mdns_hostname)]; - - sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", mdns_hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - - // Init depencency injected methods - mdns_test_init_di(); - - if (mdns_init()) { - abort(); - } - - if (mdns_test_hostname_set(mdns_hostname)) { - abort(); - } - - if (mdns_test_add_delegated_host(mdns_hostname) || mdns_test_add_delegated_host("megafritz")) { - abort(); - } - -#ifndef MDNS_NO_SERVICES - - if (mdns_test_sub_service_add("_server", "_fritz", "_tcp", 22)) { - abort(); - } - - if (mdns_test_service_add("_telnet", "_tcp", 22)) { - abort(); - } - - if (mdns_test_service_add("_workstation", "_tcp", 9)) { - abort(); - } - if (mdns_test_service_instance_name_set("_workstation", "_tcp", winstance)) { - abort(); - } - - if (mdns_test_service_add("_arduino", "_tcp", 3232)) { - abort(); - } - - if (mdns_test_service_txt_set("_arduino", "_tcp", 4, arduTxtData)) { - abort(); - } - - if (mdns_test_service_add("_http", "_tcp", 80)) { - abort(); - } - - if (mdns_test_service_instance_name_set("_http", "_tcp", "ESP WebServer")) { - abort(); - } - - if ( - mdns_test_service_add("_afpovertcp", "_tcp", 548) - || mdns_test_service_add("_rfb", "_tcp", 885) - || mdns_test_service_add("_smb", "_tcp", 885) - || mdns_test_service_add("_adisk", "_tcp", 885) - || mdns_test_service_add("_airport", "_tcp", 885) - || mdns_test_service_add("_printer", "_tcp", 885) - || mdns_test_service_add("_airplay", "_tcp", 885) - || mdns_test_service_add("_raop", "_tcp", 885) - || mdns_test_service_add("_uscan", "_tcp", 885) - || mdns_test_service_add("_uscans", "_tcp", 885) - || mdns_test_service_add("_ippusb", "_tcp", 885) - || mdns_test_service_add("_scanner", "_tcp", 885) - || mdns_test_service_add("_ipp", "_tcp", 885) - || mdns_test_service_add("_ipps", "_tcp", 885) - || mdns_test_service_add("_pdl-datastream", "_tcp", 885) - || mdns_test_service_add("_ptp", "_tcp", 885) - || mdns_test_service_add("_sleep-proxy", "_udp", 885)) { - abort(); - } -#endif - mdns_result_t *results = NULL; - FILE *file; - size_t nread; - -#ifdef INSTR_IS_OFF - size_t len = 1460; - memset(buf, 0, 1460); - - if (argc != 2) { - printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n"); - return 1; - } else { - // - // Note: parameter1 is a file (mangled packet) which caused the crash - file = fopen(argv[1], "r"); - assert(file >= 0); - len = fread(buf, 1, 1460, file); - fclose(file); - } - - for (i = 0; i < 1; i++) { -#else - while (__AFL_LOOP(1000)) { - memset(buf, 0, 1460); - size_t len = read(0, buf, 1460); -#endif - mypbuf.payload = malloc(len); - memcpy(mypbuf.payload, buf, len); - mypbuf.len = len; - g_packet.pb = &mypbuf; - mdns_test_query("minifritz", "_fritz", "_tcp", MDNS_TYPE_ANY); - mdns_test_query(NULL, "_fritz", "_tcp", MDNS_TYPE_PTR); - mdns_test_query(NULL, "_afpovertcp", "_tcp", MDNS_TYPE_PTR); - mdns_parse_packet(&g_packet); - free(mypbuf.payload); - } -#ifndef MDNS_NO_SERVICES - mdns_service_remove_all(); -#endif - ForceTaskDelete(); - mdns_free(); - return 0; -} From 4d8d25a3455e505ec217e34ac8d34cf7cd008c0c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 3 Nov 2025 12:07:07 +0100 Subject: [PATCH 2/5] fix(mdns): Host test to use hw_support include dir Forward port of 8bba3a97 --- components/mdns/tests/host_unit_test/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/mdns/tests/host_unit_test/CMakeLists.txt b/components/mdns/tests/host_unit_test/CMakeLists.txt index 40d8c60e88..493509559a 100644 --- a/components/mdns/tests/host_unit_test/CMakeLists.txt +++ b/components/mdns/tests/host_unit_test/CMakeLists.txt @@ -38,6 +38,8 @@ include_directories( ${IDF_COMPONENTS_DIR}/esp_common/include ${IDF_COMPONENTS_DIR}/esp_system/include ${IDF_COMPONENTS_DIR}/log/include + ${IDF_COMPONENTS_DIR}/esp_hw_support/etm/include + ${IDF_COMPONENTS_DIR}/soc/linux/include ${IDF_COMPONENTS_DIR}/esp_rom/include ${IDF_COMPONENTS_DIR}/heap/include ${IDF_COMPONENTS_DIR}/esp_rom/linux/include/linux/ From 27d43277d2b89bcbd76fd4d448e93b64c4565bf7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 3 Nov 2025 12:07:42 +0100 Subject: [PATCH 3/5] fix(mdns): put srv/txt records in additional section for ptr queries Forward port of b7b8c5db --- components/mdns/mdns_send.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/components/mdns/mdns_send.c b/components/mdns/mdns_send.c index 0f09759a66..f007ea0790 100644 --- a/components/mdns/mdns_send.c +++ b/components/mdns/mdns_send.c @@ -261,10 +261,8 @@ static bool create_answer_from_service(mdns_tx_packet_t *packet, mdns_service_t // According to RFC6763-section12.1, for DNS-SD, SRV, TXT and all address records // should be included in additional records. if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) || - !mdns_priv_create_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_SRV, service, - NULL, send_flush, false) || - !mdns_priv_create_answer(is_delegated ? &packet->additional : &packet->answers, MDNS_TYPE_TXT, service, - NULL, send_flush, false) || + !mdns_priv_create_answer(&packet->additional, MDNS_TYPE_SRV, service, NULL, send_flush, false) || + !mdns_priv_create_answer(&packet->additional, MDNS_TYPE_TXT, service, NULL, send_flush, false) || !mdns_priv_create_answer((shared || is_delegated) ? &packet->additional : &packet->answers, MDNS_TYPE_A, service, host, send_flush, false) || From 63082b996d3a2e62569418897fb706d5782ec884 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 3 Nov 2025 16:32:49 +0100 Subject: [PATCH 4/5] feat(mdns): support null value for boolean txt records Forward port of fa96de3bd --- components/mdns/mdns_responder.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/mdns/mdns_responder.c b/components/mdns/mdns_responder.c index 05469212cc..5f47c2f632 100644 --- a/components/mdns/mdns_responder.c +++ b/components/mdns/mdns_responder.c @@ -282,13 +282,18 @@ static mdns_txt_linked_item_t *allocate_txt(size_t num_items, mdns_txt_item_t tx mdns_mem_free(new_item); break; } - new_item->value = mdns_mem_strdup(txt[i].value); - if (!new_item->value) { - mdns_mem_free((char *)new_item->key); - mdns_mem_free(new_item); - break; + if (txt[i].value) { + new_item->value = mdns_mem_strdup(txt[i].value); + if (!new_item->value) { + mdns_mem_free((char *)new_item->key); + mdns_mem_free(new_item); + break; + } + new_item->value_len = strlen(new_item->value); + } else { + new_item->value = NULL; + new_item->value_len = 0; } - new_item->value_len = strlen(new_item->value); new_item->next = new_txt; new_txt = new_item; } From f20a234f65b3bc99fd8264ebc5031b8366fcd187 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 14 Nov 2025 17:13:08 +0100 Subject: [PATCH 5/5] fix(mdns): Fix unused variable `dcst` warning for wifi-remote chips Forward-port of 081eef88 --- components/mdns/mdns_netif.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/mdns/mdns_netif.c b/components/mdns/mdns_netif.c index 116fd43530..ddb8057de9 100644 --- a/components/mdns/mdns_netif.c +++ b/components/mdns/mdns_netif.c @@ -28,7 +28,7 @@ static const char *TAG = "mdns_netif"; #if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0) #define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED #else -#define MDNS_ESP_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED +#define MDNS_ESP_WIFI_ENABLED (CONFIG_ESP_WIFI_ENABLED || CONFIG_ESP_WIFI_REMOTE_ENABLED) #endif #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) @@ -206,9 +206,9 @@ static void handle_system_event_for_preset(void *arg, esp_event_base_t event_bas return; } - esp_netif_dhcp_status_t dcst; #if MDNS_ESP_WIFI_ENABLED && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP) if (event_base == WIFI_EVENT) { + esp_netif_dhcp_status_t dcst; switch (event_id) { case WIFI_EVENT_STA_CONNECTED: if (!esp_netif_dhcpc_get_status(netif_from_preset(MDNS_IF_STA), &dcst)) { @@ -235,6 +235,7 @@ static void handle_system_event_for_preset(void *arg, esp_event_base_t event_bas #endif #if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH if (event_base == ETH_EVENT) { + esp_netif_dhcp_status_t dcst; switch (event_id) { case ETHERNET_EVENT_CONNECTED: if (!esp_netif_dhcpc_get_status(netif_from_preset(MDNS_IF_ETH), &dcst)) {