Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/mdns__build-target-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
python -m pip install idf-build-apps
# Build default configs for all targets
python ./ci/build_apps.py components/mdns/${{ matrix.test.path }} -r default -d
# Build-only configs (compile check; discard artifacts so pytest won't run them)
if compgen -G "components/mdns/${{ matrix.test.path }}/sdkconfig.build_only.*" > /dev/null; then
python ./ci/build_apps.py components/mdns/${{ matrix.test.path }} -r 'sdkconfig.build_only.*=' -d
fi
# Build specific configs for test targets
python ./ci/build_apps.py components/mdns/${{ matrix.test.path }}
cd components/mdns/${{ matrix.test.path }}
Expand Down
8 changes: 7 additions & 1 deletion components/mdns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ 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"
if(CONFIG_MDNS_ENABLE_BROWSE)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Append after is cleaner

set(MDNS_BROWSER "mdns_browser.c")
else()
set(MDNS_BROWSER "")
endif()

set(MDNS_CORE "mdns_responder.c" "mdns_receive.c" "mdns_utils.c" "mdns_debug.c" ${MDNS_BROWSER} "mdns_send.c" "mdns_netif.c"
"mdns_querier.c" "mdns_pcb.c" "mdns_service.c")

idf_build_get_property(target IDF_TARGET)
Expand Down
8 changes: 8 additions & 0 deletions components/mdns/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ menu "mDNS"
help
Enable for the console cli to be available on the device.

config MDNS_ENABLE_BROWSE
bool "Enable mDNS browse (daemon service discovery)"
default y
help
Enable continuous browse APIs (mdns_browse_new / mdns_browse_delete)
and the related receive/send paths. Disable to save ~3–4 KB of flash
when the application only advertises services or uses one-shot queries.

config MDNS_RESPOND_REVERSE_QUERIES
bool "Enable responding to IPv4 reverse queries"
default n
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_MDNS_ENABLE_BROWSE=n
CONFIG_MDNS_RESOLVE_TEST_SERVICES=y
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y
CONFIG_MDNS_PUBLISH_DELEGATE_HOST=y
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
CONFIG_EXAMPLE_CONNECT_WIFI=n
CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y
CONFIG_EXAMPLE_ETH_PHY_IP101=y
CONFIG_EXAMPLE_ETH_MDC_GPIO=23
CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_MDNS_BUTTON_GPIO=32
6 changes: 6 additions & 0 deletions components/mdns/include/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif);
*/
esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action);

#ifdef CONFIG_MDNS_ENABLE_BROWSE
/**
* @brief Browse mDNS for a service `_service._proto`.
*
Expand All @@ -1063,6 +1064,8 @@ esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_a
* @note If one response packet contains answers for multiple active browses,
* only one browse is synchronized for that packet. This should not affect
* typical browse traffic, where packets answer one service type.
*
* @note Requires CONFIG_MDNS_ENABLE_BROWSE=y.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is enabled by default, I would phrase it as Can be disabled by ...

*/
mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier);

Expand All @@ -1074,8 +1077,11 @@ mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_brow
* - ESP_OK success.
* - ESP_ERR_FAIL mDNS is not running or the browsing of `_service._proto` is never started.
* - ESP_ERR_NO_MEM memory error.
*
* @note Requires CONFIG_MDNS_ENABLE_BROWSE=y.
*/
esp_err_t mdns_browse_delete(const char *service, const char *proto);
#endif /* CONFIG_MDNS_ENABLE_BROWSE */

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions components/mdns/mdns_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ static void register_mdns_service_subtype_set(void)
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd_service_sub));
}

#ifdef CONFIG_MDNS_ENABLE_BROWSE
static struct {
struct arg_str *service;
struct arg_str *proto;
Expand Down Expand Up @@ -1413,6 +1414,7 @@ static void register_mdns_browse_del(void)

ESP_ERROR_CHECK(esp_console_cmd_register(&cmd_browse_del));
}
#endif /* CONFIG_MDNS_ENABLE_BROWSE */

void mdns_console_register(void)
{
Expand All @@ -1434,8 +1436,10 @@ void mdns_console_register(void)
register_mdns_undelegate_host();
register_mdns_service_subtype_set();

#ifdef CONFIG_MDNS_ENABLE_BROWSE
register_mdns_browse();
register_mdns_browse_del();
#endif

#ifdef CONFIG_LWIP_IPV4
register_mdns_query_a();
Expand Down
10 changes: 9 additions & 1 deletion components/mdns/mdns_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "mdns_mem_caps.h"
#include "mdns_utils.h"
#include "mdns_debug.h"
#ifdef CONFIG_MDNS_ENABLE_BROWSE
#include "mdns_browser.h"
#endif
#include "mdns_netif.h"
#include "mdns_pcb.h"
#include "mdns_responder.h"
Expand Down Expand Up @@ -177,7 +179,7 @@ static esp_err_t post_custom_action(mdns_if_t mdns_if, mdns_event_actions_t even
return ESP_OK;
}

#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
#if defined(CONFIG_MDNS_ENABLE_BROWSE) && (CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH)
static esp_err_t post_browse_send_by_ip_protocol_action(mdns_if_t mdns_if, mdns_ip_protocol_t ip_protocol)
{
if (!mdns_priv_is_server_init() || mdns_if >= MDNS_MAX_INTERFACES) {
Expand Down Expand Up @@ -281,12 +283,16 @@ static void handle_system_event_for_preset(void *arg, esp_event_base_t event_bas
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);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
post_browse_send_by_ip_protocol_action(mdns_if_from_preset(MDNS_IF_STA), MDNS_IP_PROTOCOL_V4);
#endif
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);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
post_browse_send_by_ip_protocol_action(mdns_if_from_preset(MDNS_IF_ETH), MDNS_IP_PROTOCOL_V4);
#endif
break;
#endif
case IP_EVENT_GOT_IP6: {
Expand All @@ -297,7 +303,9 @@ static void handle_system_event_for_preset(void *arg, esp_event_base_t event_bas
}
post_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
post_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
post_browse_send_by_ip_protocol_action(mdns_if, MDNS_IP_PROTOCOL_V6);
#endif

}
break;
Expand Down
30 changes: 30 additions & 0 deletions components/mdns/mdns_receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "mdns_debug.h"
#include "mdns_netif.h"
#include "mdns_send.h"
#ifdef CONFIG_MDNS_ENABLE_BROWSE
#include "mdns_browser.h"
#endif
#include "mdns_querier.h"
#include "mdns_pcb.h"
#include "mdns_responder.h"
Expand Down Expand Up @@ -597,6 +599,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
const uint8_t *content = data + MDNS_HEAD_LEN;
bool do_not_reply = false;
mdns_search_once_t *search_result = NULL;
#ifdef CONFIG_MDNS_ENABLE_BROWSE
mdns_browse_t *browse_result = NULL;
/*
* Browse packet limitations (see also mdns_browse_new() / mdns_browse_notify_t in mdns.h):
Expand All @@ -611,6 +614,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
char *browse_result_proto = NULL;
mdns_browse_sync_t *out_sync_browse = NULL;
mdns_browse_staged_ip_t *staged_browse_ips = NULL;
#endif

DBG_RX_PACKET(packet, data, len);

Expand Down Expand Up @@ -765,7 +769,9 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)

while (content < (data + len)) {
search_result = NULL;
#ifdef CONFIG_MDNS_ENABLE_BROWSE
browse_result = NULL;
#endif

content = mdns_utils_parse_fqdn(data, content, name, len);
if (!content) {
Expand Down Expand Up @@ -817,6 +823,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
continue;
}
search_result = mdns_priv_query_find(name, type, packet->tcpip_if, packet->ip_protocol);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
browse_result = mdns_priv_browse_find(name, type, packet->tcpip_if, packet->ip_protocol);
if (browse_result) {
packet_browse = browse_result;
Expand Down Expand Up @@ -853,14 +860,18 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
memcpy(browse_result_instance, name->host, MDNS_NAME_BUF_LEN);
}
}
#endif /* CONFIG_MDNS_ENABLE_BROWSE */
}

if (type == MDNS_TYPE_PTR) {
#ifdef CONFIG_MDNS_ENABLE_BROWSE
mdns_browse_t *browse_for_ptr = mdns_priv_browse_find_ptr(name);
#endif
size_t rdata_bound = (size_t)(data_ptr + data_len - data);
if (!mdns_utils_parse_fqdn(data, data_ptr, name, rdata_bound)) {
continue;//error
}
#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (browse_for_ptr) {
packet_browse = browse_for_ptr;
out_sync_browse = mdns_priv_browse_ensure_sync(browse_for_ptr, out_sync_browse);
Expand All @@ -871,6 +882,9 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
browse_for_ptr->proto, packet->tcpip_if, packet->ip_protocol,
ttl, out_sync_browse);
} else if (search_result) {
#else
if (search_result) {
#endif
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)) {
Expand Down Expand Up @@ -961,6 +975,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
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);

#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (browse_result && !mdns_utils_str_null_or_empty(browse_result_instance)
&& !mdns_utils_str_null_or_empty(browse_result_service)
&& !mdns_utils_str_null_or_empty(browse_result_proto)) {
Expand All @@ -970,6 +985,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
ttl,
out_sync_browse);
}
#endif
if (search_result) {
if (search_result->type == MDNS_TYPE_PTR) {
if (!result->hostname) { // assign host/port for this entry only if not previously set
Expand Down Expand Up @@ -1041,6 +1057,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
size_t txt_count = 0;

mdns_result_t *result = NULL;
#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (browse_result && !mdns_utils_str_null_or_empty(browse_result_instance)
&& !mdns_utils_str_null_or_empty(browse_result_service)
&& !mdns_utils_str_null_or_empty(browse_result_proto)) {
Expand All @@ -1051,6 +1068,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
packet->ip_protocol,
ttl, out_sync_browse);
}
#endif
if (search_result) {
if (search_result->type == MDNS_TYPE_PTR) {
result = search_result->result;
Expand Down Expand Up @@ -1120,6 +1138,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
esp_ip_addr_t ip6;
ip6.type = ESP_IPADDR_TYPE_V6;
memcpy(ip6.u_addr.ip6.addr, data_ptr, MDNS_ANSWER_AAAA_SIZE);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (packet_browse || browse_result) {
mdns_browse_t *browse_ip = browse_result ? browse_result : packet_browse;
out_sync_browse = mdns_priv_browse_ensure_sync(browse_ip, out_sync_browse);
Expand All @@ -1128,6 +1147,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
goto clear_rx_packet;
}
}
#endif
if (search_result) {
//check for more applicable searches (PTR & A/AAAA at the same time)
while (search_result) {
Expand Down Expand Up @@ -1186,6 +1206,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
esp_ip_addr_t ip;
ip.type = ESP_IPADDR_TYPE_V4;
memcpy(&(ip.u_addr.ip4.addr), data_ptr, 4);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (packet_browse || browse_result) {
mdns_browse_t *browse_ip = browse_result ? browse_result : packet_browse;
out_sync_browse = mdns_priv_browse_ensure_sync(browse_ip, out_sync_browse);
Expand All @@ -1194,6 +1215,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
goto clear_rx_packet;
}
}
#endif
if (search_result) {
//check for more applicable searches (PTR & A/AAAA at the same time)
while (search_result) {
Expand Down Expand Up @@ -1251,6 +1273,7 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
}
}

#ifdef CONFIG_MDNS_ENABLE_BROWSE
if (staged_browse_ips) {
mdns_browse_t *browse_apply = packet_browse;
if (!browse_apply && out_sync_browse) {
Expand All @@ -1263,10 +1286,12 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
}
mdns_priv_browse_staged_ip_free(staged_browse_ips);
staged_browse_ips = NULL;
#endif

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);
}
#ifdef CONFIG_MDNS_ENABLE_BROWSE
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);
Expand All @@ -1279,10 +1304,13 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
}
out_sync_browse = NULL;
}
#endif

clear_rx_packet:
#ifdef CONFIG_MDNS_ENABLE_BROWSE
mdns_priv_browse_staged_ip_free(staged_browse_ips);
staged_browse_ips = NULL;
#endif
while (parsed_packet->questions) {
mdns_parsed_question_t *question = parsed_packet->questions;
parsed_packet->questions = parsed_packet->questions->next;
Expand Down Expand Up @@ -1316,10 +1344,12 @@ static void mdns_parse_packet(mdns_rx_packet_t *packet)
mdns_mem_free(record);
}
mdns_mem_free(parsed_packet);
#ifdef CONFIG_MDNS_ENABLE_BROWSE
mdns_mem_free(browse_result_instance);
mdns_mem_free(browse_result_service);
mdns_mem_free(browse_result_proto);
mdns_priv_browse_sync_free(out_sync_browse);
#endif
}

void mdns_priv_receive_action(mdns_action_t *action, mdns_action_subtype_t type)
Expand Down
Loading
Loading