feat: DDP frame sync over UDP broadcast, DDP acks, DDP enable setting

- Driver acks each completed DDP frame; the test waits for the acks before broadcasting the sync.
- Sync listener on UDP 4049: a frame is held until the next sync, then shown.
- DDP pixels are shown at full brightness, ignoring the device brightness settings.
- "DDP and sync receive on" setting (NVS, network API, web UI); off ignores DDP and sync packets.
- ddp_test.py: finds devices on the subnet, drives them in lockstep at a fixed rate, runs animations.
- /api/ddp reports sync counters for diagnosis.
- Includes in-progress WebSocket, multi-driver web UI and Wi-Fi work from the same tree.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-11 14:10:55 +13:00
1 parent 47f4eba0d6
commit 4cd3bc7973
294 files changed
+48954 -301

No files matched your search

@@ -0,0 +1,101 @@
/*
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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 Check if a running browse `_service._proto` exists.
*
* @param service Service name.
* @param proto Protocol name.
* @return true if a running browse `_service._proto` exists, false otherwise.
*/
bool mdns_priv_browse_has_service(const char *service, const char *proto);
/**
* @brief Check if a running browse `_service._proto` with the given subtype exists.
*
* @param service Service name.
* @param proto Protocol name.
* @param subtype Subtype name, or NULL to match browses without subtype.
* @return true if a running browse with identical service, proto and subtype exists, false otherwise.
*/
bool mdns_priv_browse_has_subtype(const char *service, const char *proto, const char *subtype);
/**
* @brief Looks for a running browse item as consumer of the provided record data
*
* @note Called from the packet parser (mdns_receive.c)
*
* @return matching browse item, or NULL if not found
*/
mdns_browse_t *mdns_priv_browse_find_consumer(mdns_name_t *name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
/**
* @brief Send out browse queries by IP protocol
*
* @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_by_ip_protocol(mdns_if_t mdns_if, mdns_ip_protocol_t ip_protocol);
/**
* @brief Perform action from mdns service queue
*
* @note Called by `free_action()` and `execute_action()` in mdns_service.c
*/
void mdns_priv_browse_action(mdns_action_t *action, mdns_action_subtype_t type);
/**
* @brief Build temporary result and queue for sync from service cache
*
* @param entry Owner cache entry.
* @param service Updated service cache.
* @param records Bitmask of cache record types to sync.
*
* @note Called from mdns_priv_cache_process_sync() in mdns_cache.c
*
* @return true if all matching browses have been notified about the updated service, false otherwise
*/
bool mdns_priv_browse_update_from_service_cache(const mdns_cache_entry_t *entry, const mdns_service_cache_t *service,
mdns_cache_record_mask_t records);
/**
* @brief Notify one browse about one visible cache service.
*
* @return true if browse is successfully notified, false otherwise
*/
bool mdns_priv_browse_notify_from_service_cache(const mdns_cache_entry_t *entry, const mdns_service_cache_t *service,
mdns_browse_t *browse);
/**
* @brief Notify the affected normal/subtype browse about a PTR goodbye.
*
* @param subtype The PTR subtype, or NULL for the base PTR.
*
* @note Used for both goodbye and natural expiration.
*
* @note Must be called before the PTR/subtype is removed from the cache to avoid UAF.
*/
bool mdns_priv_browse_notify_ptr_goodbye_from_service_cache(const mdns_cache_entry_t *entry,
const mdns_service_cache_t *service,
const char *subtype);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,204 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#include "mdns_private.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Check if a host has a service by hostname, esp_netif, ip_protocol, service, and proto.
*
* @param hostname The hostname to check.
* @param esp_netif Pointer to the esp_netif to check.
* @param ip_protocol IP protocol to check.
* @param service The service name to check.
* @param proto The protocol to check.
*
* @return true if the host has the service, false otherwise.
*/
bool mdns_priv_cache_host_has_service(const char *hostname, const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *service, const char *proto);
/**
* @brief Find the subtype node in a service cache by subtype name.
*
* @param service Pointer to the service cache.
* @param subtype The subtype name to find.
*
* @return Pointer to the subtype node if found, NULL otherwise.
*/
const mdns_cache_subtype_t *mdns_priv_cache_service_find_subtype(const mdns_service_cache_t *service, const char *subtype);
/**
* @brief Update a PTR record for service cache `_service._proto` with possible subtype.
*
* @param esp_netif Pointer to the esp_netif.
* @param ip_protocol IP protocol.
* @param instance The instance name.
* @param service The service name.
* @param proto The protocol.
* @param subtype The subtype, or null/empty for base PTR record.
* @param ttl The PTR TTL.
*
* @return The result of the update, see @ref mdns_cache_update_result_t.
*
* @note The PTR record will be marked to-sync when:
* - A new service is added to the cache.
* - A new subtype is appended to the service cache.
* - The PTR TTL is updated.
*
* @note A TTL=0 goodbye notifies only the matching base/subtype browsers and
* removes only that PTR record. Other cached records are preserved.
*/
mdns_cache_update_result_t mdns_priv_cache_update_ptr(const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *instance, const char *service, const char *proto,
const char *subtype, uint32_t ttl);
/**
* @brief Update a SRV record for service cache `instance._service._proto`.
*
* @param esp_netif Pointer to the esp_netif.
* @param ip_protocol IP protocol.
* @param hostname The hostname to update.
* @param instance The instance name.
* @param service The service name.
* @param proto The protocol.
* @param priority The priority to update.
* @param weight The weight to update.
* @param port The port to update.
* @param ttl The SRV TTL.
*
* @return The result of the update, see @ref mdns_cache_update_result_t.
*
* @note The SRV record will be marked to-sync when:
* - A new SRV record is added to the service cache.
* - The service cache is moved to the host designated by @ref hostname.
* - The SRV record is updated.
* - Receives a TTL=0 goodbye: the SRV record will be marked as absent.
*/
mdns_cache_update_result_t mdns_priv_cache_update_srv(const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *hostname, const char *instance, const char *service,
const char *proto, uint16_t priority, uint16_t weight,
uint16_t port, uint32_t ttl);
/**
* @brief Update a TXT record for service cache `instance._service._proto`.
*
* @param esp_netif Pointer to the esp_netif.
* @param ip_protocol IP protocol.
* @param instance The instance name.
* @param service The service name.
* @param proto The protocol.
* @param txt The TXT record to update.
* @param ttl The TXT TTL.
*
* @return The result of the update, see @ref mdns_cache_update_result_t.
*
* @note The TXT record will be marked to-sync when:
* - A new TXT record is added to the service cache.
* - The TXT record is updated.
* - Receives a TTL=0 goodbye: the TXT record will be marked as absent.
*/
mdns_cache_update_result_t mdns_priv_cache_update_txt(const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *instance, const char *service, const char *proto,
mdns_txt_linked_item_t *txt, uint32_t ttl);
/**
* @brief Update an A/AAAA record for a cache entry `hostname`.
*
* @param esp_netif Pointer to the esp_netif.
* @param ip_protocol IP protocol.
* @param hostname The hostname to update.
* @param addr The address to update.
* @param ttl The TTL.
*
* @return The result of the update, see @ref mdns_cache_update_result_t.
*
* @note When an A/AAAA record is updated (added, removed, or updated),
* all services under this cache entry will be marked to-sync for browses.
*/
mdns_cache_update_result_t mdns_priv_cache_update_addr(const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *hostname, const esp_ip_addr_t *addr, uint32_t ttl);
/**
* @brief Update an A/AAAA record for an existing cache entry `hostname`.
*
* @param esp_netif Pointer to the esp_netif.
* @param ip_protocol IP protocol.
* @param hostname The hostname to update.
* @param addr The address to update.
* @param ttl The TTL.
*
* @return The result of the update, see @ref mdns_cache_update_result_t.
* If the cache entry does not exist, or no consumers subscribe to the hostname or service under the hostname,
* return MDNS_CACHE_NO_CHANGE.
*
* @note When an A/AAAA record is updated (added, removed, or updated),
* all services under this cache entry will be marked to-sync for browses.
*
* @note Used by browses in case ADDR records come before SRV record.
*/
mdns_cache_update_result_t mdns_priv_cache_update_existing_addr(const esp_netif_t *esp_netif, mdns_ip_protocol_t ip_protocol,
const char *hostname, const esp_ip_addr_t *addr, uint32_t ttl);
/**
* @brief Remove expired records from the entire cache.
*
* @param now_us The current time in microseconds from esp_timer_get_time().
*
* @note This function is called in mDNS service task while holding service lock.
* PTR expiration notifies browses immediately, other records are marked to-sync for browses.
*/
void mdns_priv_cache_remove_expired_records(int64_t now_us);
/**
* @brief Trim down the expiration time of unsubscribed records.
*
* @param now_us The current time in microseconds from esp_timer_get_time().
*
* @note This function scans through s_expiry and sets the expiration time of unsubscribed records to
* `min(expires_at_us, now_us + MDNS_CACHE_UNSUBSCRIBED_GRACE_SEC * MDNS_US_PER_SEC)`.
* Finally cleaned up in service task by `mdns_priv_cache_remove_expired_records()`.
*/
void mdns_priv_cache_trim_unsubscribed_records(int64_t now_us);
/**
* @brief Clear all cache entries.
*/
void mdns_priv_cache_clear(void);
/**
* @brief Convert a service cache to a mdns_result_t.
*
* @param entry Pointer to the cache entry.
* @param service Pointer to the service cache.
* @param out_result Pointer to the output mdns_result_t.
* @return ESP_OK on success, ESP_ERR_NO_MEM on allocation failure.
*/
esp_err_t mdns_priv_service_cache_to_result(const mdns_cache_entry_t *entry, const mdns_service_cache_t *service,
mdns_result_t **out_result);
/**
* @brief Process all pending synchronization cache entries.
*
* @note This function is called by the MDNS task to process all pending synchronization cache entries.
* It will generate temporary results, notify corresponding consumers, and clear the sync out flags.
*/
void mdns_priv_cache_process_sync(void);
/**
* @brief Replay all currently visible cache services to a newly registered browse.
*
* @return true if successfully notified, false otherwise
*/
bool mdns_priv_cache_notify_browse(mdns_browse_t *browse);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,72 @@
/*
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
*/
#ifdef CONFIG_MDNS_ENABLE_BROWSE
#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)
#else
#define DBG_BROWSE_RESULTS(result, browse)
#define DBG_BROWSE_RESULTS_WITH_MSG(result, ...)
#endif /* CONFIG_MDNS_ENABLE_BROWSE */
#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)
#ifdef CONFIG_MDNS_ENABLE_BROWSE
/**
* @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);
#endif /* CONFIG_MDNS_ENABLE_BROWSE */
/**
* @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
@@ -0,0 +1,65 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Allocate memory.
* @param size Number of bytes to allocate.
* @return Pointer to allocated memory, or NULL on failure.
*/
void *mdns_mem_malloc(size_t size);
/**
* @brief Allocate and zero memory.
* @param num Number of elements.
* @param size Size of each element.
* @return Pointer to allocated memory, or NULL on failure.
*/
void *mdns_mem_calloc(size_t num, size_t size);
/**
* @brief Free allocated memory.
* @param ptr Pointer to memory to free.
*/
void mdns_mem_free(void *ptr);
/**
* @brief Duplicate a string.
* @param s String to duplicate.
* @return Pointer to duplicated string, or NULL on failure.
*/
char *mdns_mem_strdup(const char *s);
/**
* @brief Duplicate a string with length limit.
* @param s String to duplicate.
* @param n Maximum number of characters to copy.
* @return Pointer to duplicated string, or NULL on failure.
*/
char *mdns_mem_strndup(const char *s, size_t n);
/**
* @brief Allocate memory for mDNS task.
* @param size Number of bytes to allocate.
* @return Pointer to allocated memory, or NULL on failure.
*/
void *mdns_mem_task_malloc(size_t size);
/**
* @brief Free allocated memory for mDNS task.
* @param ptr Pointer to memory to free.
*/
void mdns_mem_task_free(void *ptr);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,67 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,62 @@
/*
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
/*
* MDNS Server Networking -- private include
*
*/
#include "mdns.h"
#include "mdns_private.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Check if the netif on the selected interfacce and protocol is ready
*/
bool mdns_priv_if_ready(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
/**
* @brief Start PCB
*/
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_priv_if_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
/**
* @brief send packet over UDP
*
* @param server The server
* @param data byte array containing the packet data
* @param len length of the packet data
*
* @return length of sent packet or 0 on error
*/
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_priv_get_packet_data(mdns_rx_packet_t *packet);
/**
* @brief Gets data length of c
*/
size_t mdns_priv_get_packet_len(mdns_rx_packet_t *packet);
/**
* @brief Free the mDNS packet
*/
void mdns_priv_packet_free(mdns_rx_packet_t *packet);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,121 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,505 @@
/*
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "sdkconfig.h"
#include "mdns.h"
#include "esp_task.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_system.h"
/** Number of predefined interfaces */
#ifndef CONFIG_MDNS_PREDEF_NETIF_STA
#define CONFIG_MDNS_PREDEF_NETIF_STA 0
#endif
#ifndef CONFIG_MDNS_PREDEF_NETIF_AP
#define CONFIG_MDNS_PREDEF_NETIF_AP 0
#endif
#ifndef CONFIG_MDNS_PREDEF_NETIF_ETH
#define CONFIG_MDNS_PREDEF_NETIF_ETH 0
#endif
#define MDNS_MAX_PREDEF_INTERFACES (CONFIG_MDNS_PREDEF_NETIF_STA + CONFIG_MDNS_PREDEF_NETIF_AP + CONFIG_MDNS_PREDEF_NETIF_ETH)
#ifdef CONFIG_LWIP_IPV6_NUM_ADDRESSES
#define NETIF_IPV6_MAX_NUMS CONFIG_LWIP_IPV6_NUM_ADDRESSES
#else
#define NETIF_IPV6_MAX_NUMS 3
#endif
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
/* CONFIG_LWIP_IPV4 was introduced in IDF v5.1 */
/* For IDF v5.0, set CONFIG_LWIP_IPV4 to 1 by default */
#ifndef CONFIG_LWIP_IPV4
#define CONFIG_LWIP_IPV4 1
#endif // CONFIG_LWIP_IPV4
#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
/** Number of configured interfaces */
#if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES
#warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES.
#define MDNS_MAX_INTERFACES (MDNS_MAX_PREDEF_INTERFACES)
#else
#define MDNS_MAX_INTERFACES (CONFIG_MDNS_MAX_INTERFACES)
#endif
/** The maximum number of services */
#define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES
#define MDNS_ANSWER_PTR_TTL 4500
#define MDNS_ANSWER_TXT_TTL 4500
#define MDNS_ANSWER_SRV_TTL 120
#define MDNS_ANSWER_A_TTL 120
#define MDNS_ANSWER_AAAA_TTL 120
#define MDNS_FLAGS_QUERY_REPSONSE 0x8000
#define MDNS_FLAGS_AUTHORITATIVE 0x0400
#define MDNS_FLAGS_QR_AUTHORITATIVE (MDNS_FLAGS_QUERY_REPSONSE | MDNS_FLAGS_AUTHORITATIVE)
#define MDNS_FLAGS_DISTRIBUTED 0x0200
#define MDNS_NAME_REF 0xC000
//custom type! only used by this implementation
//to help manage service discovery handling
#define MDNS_TYPE_SDPTR 0x0032
#define MDNS_CLASS_IN 0x0001
#define MDNS_CLASS_ANY 0x00FF
#define MDNS_CLASS_IN_FLUSH_CACHE 0x8001
#define MDNS_ANSWER_ALL 0x3F
#define MDNS_ANSWER_PTR 0x08
#define MDNS_ANSWER_TXT 0x04
#define MDNS_ANSWER_SRV 0x02
#define MDNS_ANSWER_A 0x01
#define MDNS_ANSWER_AAAA 0x10
#define MDNS_ANSWER_NSEC 0x20
#define MDNS_ANSWER_SDPTR 0x80
#define MDNS_ANSWER_AAAA_SIZE 16
#define MDNS_SERVICE_PORT 5353 // UDP port that the server runs on
#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
#define MDNS_ACTION_QUEUE_LEN CONFIG_MDNS_ACTION_QUEUE_LEN // Maximum actions pending to the server
#define MDNS_TXT_MAX_LEN 1024 // Maximum string length of text data in TXT record
#define MDNS_MAX_PACKET_SIZE 1460 // Maximum size of mDNS outgoing packet
#define MDNS_HEAD_LEN 12
#define MDNS_HEAD_ID_OFFSET 0
#define MDNS_HEAD_FLAGS_OFFSET 2
#define MDNS_HEAD_QUESTIONS_OFFSET 4
#define MDNS_HEAD_ANSWERS_OFFSET 6
#define MDNS_HEAD_SERVERS_OFFSET 8
#define MDNS_HEAD_ADDITIONAL_OFFSET 10
#define MDNS_TYPE_OFFSET 0
#define MDNS_CLASS_OFFSET 2
#define MDNS_TTL_OFFSET 4
#define MDNS_LEN_OFFSET 8
#define MDNS_DATA_OFFSET 10
#define MDNS_SRV_PRIORITY_OFFSET 0
#define MDNS_SRV_WEIGHT_OFFSET 2
#define MDNS_SRV_PORT_OFFSET 4
#define MDNS_SRV_FQDN_OFFSET 6
#define MDNS_TIMER_PERIOD_US (CONFIG_MDNS_TIMER_PERIOD_MS*1000)
#define MDNS_US_PER_SEC 1000000LL
#define queueToEnd(type, queue, item) \
if (!queue) { \
queue = item; \
} else { \
type * _q = queue; \
while (_q->next) { _q = _q->next; } \
_q->next = item; \
}
#define queueDetach(type, queue, item) \
if (queue) { \
if (queue == item) { \
queue = queue->next; \
} else { \
type * _q = queue; \
while (_q->next && _q->next != item) { \
_q = _q->next; \
} \
if (_q->next == item) { \
_q->next = item->next; \
item->next = NULL; \
} \
} \
}
#define queueFree(type, queue) while (queue) { type * _q = queue; queue = queue->next; mdns_mem_free(_q); }
#ifndef HOOK_MALLOC_FAILED
#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 {
MDNS_ANSWER, MDNS_NS, MDNS_EXTRA
} mdns_parsed_record_type_t;
typedef enum {
ACTION_SYSTEM_EVENT,
ACTION_HOSTNAME_SET,
ACTION_INSTANCE_SET,
ACTION_SEARCH_ADD,
ACTION_SEARCH_SEND,
ACTION_SEARCH_END,
ACTION_BROWSE_START,
ACTION_BROWSE_END,
ACTION_TX_HANDLE,
ACTION_RX_HANDLE,
ACTION_TASK_STOP,
ACTION_DELEGATE_HOSTNAME_ADD,
ACTION_DELEGATE_HOSTNAME_REMOVE,
ACTION_DELEGATE_HOSTNAME_SET_ADDR,
ACTION_BROWSE_SEND_BY_IP_PROTOCOL,
ACTION_MAX
} mdns_action_type_t;
typedef enum {
ACTION_RUN,
ACTION_CLEANUP,
} mdns_action_subtype_t;
typedef struct {
uint16_t id;
uint16_t flags;
uint16_t questions; //QDCOUNT
uint16_t answers; //ANCOUNT
uint16_t servers; //NSCOUNT
uint16_t additional;//ARCOUNT
} mdns_header_t;
typedef struct {
char host[MDNS_NAME_BUF_LEN]; // hostname for A/AAAA records, instance name for SRV records
char service[MDNS_NAME_BUF_LEN];
char proto[MDNS_NAME_BUF_LEN];
char domain[MDNS_NAME_BUF_LEN];
uint8_t parts;
uint8_t sub; // 0: no subtype, 1: subtype
bool invalid;
} mdns_name_t;
typedef struct mdns_parsed_question_s {
struct mdns_parsed_question_s *next;
uint16_t type;
bool sub;
bool unicast;
char *host;
char *service;
char *proto;
char *domain;
} mdns_parsed_question_t;
typedef struct mdns_parsed_record_s {
struct mdns_parsed_record_s *next;
mdns_parsed_record_type_t record_type;
uint16_t type;
uint16_t clas;
uint8_t flush;
uint32_t ttl;
char *host;
char *service;
char *proto;
char *domain;
uint16_t data_len;
uint8_t *data;
} mdns_parsed_record_t;
typedef struct {
mdns_if_t tcpip_if;
mdns_ip_protocol_t ip_protocol;
esp_ip_addr_t src;
uint16_t src_port;
uint8_t multicast;
uint8_t authoritative;
uint8_t probe;
uint8_t discovery;
uint8_t distributed;
mdns_parsed_question_t *questions;
mdns_parsed_record_t *records;
uint16_t id;
} mdns_parsed_packet_t;
typedef struct {
mdns_if_t tcpip_if;
mdns_ip_protocol_t ip_protocol;
struct pbuf *pb;
esp_ip_addr_t src;
esp_ip_addr_t dest;
uint16_t src_port;
uint8_t multicast;
} mdns_rx_packet_t;
typedef struct mdns_txt_linked_item_s {
const char *key; /*!< item key name */
char *value; /*!< item value string */
uint8_t value_len; /*!< item value length */
struct mdns_txt_linked_item_s *next; /*!< next result, or NULL for the last result in the list */
} mdns_txt_linked_item_t;
typedef struct mdns_subtype_s {
const char *subtype; /*!< subtype */
struct mdns_subtype_s *next; /*!< next result, or NULL for the last result in the list */
} mdns_subtype_t;
typedef struct {
const char *instance;
const char *service;
const char *proto;
const char *hostname;
uint16_t priority;
uint16_t weight;
uint16_t port;
mdns_txt_linked_item_t *txt;
mdns_subtype_t *subtype;
} mdns_service_t;
typedef struct mdns_srv_item_s {
struct mdns_srv_item_s *next;
mdns_service_t *service;
} mdns_srv_item_t;
typedef struct mdns_out_question_s {
struct mdns_out_question_s *next;
uint16_t type;
bool unicast;
const char *host;
const char *service;
const char *proto;
const char *domain;
const char *subtype;
bool own_dynamic_memory;
} mdns_out_question_t;
typedef struct mdns_host_item_t {
const char *hostname;
mdns_ip_addr_t *address_list;
struct mdns_host_item_t *next;
} mdns_host_item_t;
typedef struct mdns_out_answer_s {
struct mdns_out_answer_s *next;
uint16_t type;
uint8_t bye;
uint8_t flush;
mdns_service_t *service;
mdns_host_item_t *host;
const char *custom_instance;
const char *custom_service;
const char *custom_proto;
} mdns_out_answer_t;
typedef struct mdns_tx_packet_s {
struct mdns_tx_packet_s *next;
uint32_t send_at;
mdns_if_t tcpip_if;
mdns_ip_protocol_t ip_protocol;
esp_ip_addr_t dst;
uint16_t port;
uint16_t flags;
uint8_t distributed;
mdns_out_question_t *questions;
mdns_out_answer_t *answers;
mdns_out_answer_t *servers;
mdns_out_answer_t *additional;
bool queued;
uint16_t id;
} mdns_tx_packet_t;
typedef enum {
SEARCH_OFF,
SEARCH_INIT,
SEARCH_RUNNING,
SEARCH_MAX
} mdns_search_once_state_t;
typedef enum {
BROWSE_OFF,
BROWSE_INIT,
BROWSE_RUNNING,
BROWSE_MAX
} mdns_browse_state_t;
typedef struct mdns_search_once_s {
struct mdns_search_once_s *next;
mdns_search_once_state_t state;
uint32_t started_at;
uint32_t sent_at;
uint32_t timeout;
mdns_query_notify_t notifier;
SemaphoreHandle_t done_semaphore;
uint16_t type;
bool unicast;
uint8_t max_results;
uint8_t num_results;
char *instance;
char *service;
char *proto;
char *subtype;
mdns_result_t *result;
} mdns_search_once_t;
typedef struct mdns_browse_s {
struct mdns_browse_s *next;
mdns_browse_state_t state;
mdns_browse_notify_t notifier;
char *service;
char *proto;
char *subtype;
} mdns_browse_t;
typedef struct {
mdns_action_type_t type;
union {
struct {
char *hostname;
} hostname_set;
char *instance;
struct {
mdns_if_t interface;
mdns_event_actions_t event_action;
} sys_event;
struct {
mdns_search_once_t *search;
} search_add;
struct {
mdns_tx_packet_t *packet;
} tx_handle;
struct {
mdns_rx_packet_t *packet;
} rx_handle;
struct {
const char *hostname;
mdns_ip_addr_t *address_list;
} delegate_hostname;
struct {
mdns_browse_t *browse;
} browse_add;
struct {
mdns_if_t interface;
mdns_ip_protocol_t ip_protocol;
} browse_send;
} data;
} mdns_action_t;
typedef enum {
MDNS_CACHE_CONSUMER_BROWSE = (1U << 0),
} mdns_cache_consumer_type_t;
typedef uint8_t mdns_cache_consumer_mask_t;
typedef enum {
// Browse update flags
MDNS_CACHE_RECORD_PTR = (1U << 0), /*!< Normal PTR record updated, only notify no-subtype browses. */
MDNS_CACHE_RECORD_SUBTYPE = (1U << 1), /*!< PTR record with subtype updated, only notify browse with identical subtype. */
// Resolver update flags
MDNS_CACHE_RECORD_SRV = (1U << 2),
MDNS_CACHE_RECORD_TXT = (1U << 3),
MDNS_CACHE_RECORD_ADDR = (1U << 4),
} mdns_cache_record_type_t;
typedef uint8_t mdns_cache_record_mask_t;
/**
* @brief mDNS cache expiry structure
*/
typedef struct mdns_cache_expiry_s {
bool queued;
mdns_cache_record_mask_t record_mask;
struct mdns_cache_expiry_s *next;
struct mdns_cache_entry_s *entry;
struct mdns_service_cache_s *service;
struct mdns_cache_addr_s *addr;
struct mdns_cache_subtype_s *subtype;
int64_t expires_at_us; /*!< record absolute expiration time in microseconds */
} mdns_cache_expiry_t;
/**
* @brief mDNS cache PTR subtype structure
*/
typedef struct mdns_cache_subtype_s {
char *subtype;
uint32_t ttl;
bool pending_sync; /*!< true if the subtype needs sync notification */
struct mdns_cache_subtype_s *next;
} mdns_cache_subtype_t;
/**
* @brief mDNS cache ADDR list structure
*/
typedef struct mdns_cache_addr_s {
esp_ip_addr_t addr;
uint32_t ttl;
struct mdns_cache_addr_s *next;
} mdns_cache_addr_t;
/**
* @brief mDNS cache service structure, contains PTR (with subtype list), SRV and TXT records
*/
typedef struct mdns_service_cache_s {
char *instance_name;
char *service;
char *proto;
// PTR
bool ptr_present; /*!< true if PTR record is present */
uint32_t ptr_ttl;
// Subtype list
mdns_cache_subtype_t *subtype_list;
// SRV
bool srv_present; /*!< true if SRV record is present */
uint16_t priority;
uint16_t weight;
uint16_t port;
uint32_t srv_ttl;
// TXT
bool txt_present; /*!< true if TXT record is present */
mdns_txt_linked_item_t *txt_list;
uint32_t txt_ttl;
// To-sync flags
mdns_cache_record_mask_t sync_records; /*!< bitmask of records to sync, see @ref mdns_cache_record_type_t */
mdns_cache_consumer_mask_t sync_consumers; /*!< bitmask of consumers to sync, see @ref mdns_cache_consumer_type_t */
struct mdns_service_cache_s *next;
} mdns_service_cache_t;
/**
* @brief mDNS cache entry structure, contains hostname, IP address list and service cache list
*/
typedef struct mdns_cache_entry_s {
char *hostname;
esp_netif_t *esp_netif;
mdns_ip_protocol_t ip_protocol;
mdns_cache_addr_t *addr_list;
mdns_service_cache_t *service_cache_list;
struct mdns_cache_entry_s *next;
} mdns_cache_entry_t;
/**
* @brief mDNS cache update result structure
*/
typedef enum {
MDNS_CACHE_NO_CHANGE, /*!< no change to the cache */
MDNS_CACHE_ADDED, /*!< new cache entry or service cache appended */
MDNS_CACHE_UPDATED, /*!< existing service cache entry updated */
MDNS_CACHE_REMOVED, /*!< existing service cache entry removed */
MDNS_CACHE_ERROR, /*!< error occurred while updating the cache */
} mdns_cache_update_result_t;
@@ -0,0 +1,116 @@
/*
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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 one search object (detached or still on the active list)
*/
void mdns_priv_search_once_free(mdns_search_once_t *search);
/**
* @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
@@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,98 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,131 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
#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
@@ -0,0 +1,172 @@
/*
* 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;
}
/**
* @brief Free a TXT linked list.
*
* @param txt Pointer to the TXT linked list.
*/
void mdns_utils_free_txt_linked_list(mdns_txt_linked_item_t *txt);
/**
* @brief Check if two strings are valid and case-insensitivly equal
*/
bool mdns_utils_str_case_equal(const char *a, const char *b);
/**
* @brief Check if two strings are either both null/empty or valid and case-insensitivly equal
*/
bool mdns_utils_str_case_equal_nullable(const char *a, const char *b);