From 7064eb97297ca4fa7185486ebeb2763d08ca5450 Mon Sep 17 00:00:00 2001 From: "suren.gabrielyan" Date: Fri, 5 Mar 2021 21:12:51 +0400 Subject: [PATCH 1/4] mdns: Add MDNS_STRICT_MODE config option Strict mode was hardcoded in private header file, but it's useful for users to enable/disable it depending on the mdns library they are using. e.g. Avahi might not resolve the non-strict answers. --- components/mdns/Kconfig | 11 +++++++++++ components/mdns/private_include/mdns_private.h | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index 72800aaa2a..38dec76287 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -56,6 +56,17 @@ menu "mDNS" Configures timeout for adding a new mDNS service. Adding a service fails if could not be completed within this time. + config MDNS_STRICT_MODE + bool "mDNS strict mode" + default "n" + help + Configures strict mode. Set this to 1 for the mDNS library to strictly follow the RFC6762: + Currently the only strict feature: Do not repeat original questions in response packets + (defined in RFC6762 sec. 6). + Default configuration is 0, i.e. non-strict mode, since some implementations, + such as lwIP mdns resolver (used by standard POSIX API like getaddrinfo, gethostbyname) + could not correctly resolve advertised names. + config MDNS_TIMER_PERIOD_MS int "mDNS timer period (ms)" range 10 10000 diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 3fa57e597e..211984e275 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -31,7 +31,11 @@ * such as lwIP mdns resolver (used by standard POSIX API like getaddrinfo, gethostbyname) * could not correctly resolve advertised names. */ +#ifndef CONFIG_MDNS_STRICT_MODE #define MDNS_STRICT_MODE 0 +#else +#define MDNS_STRICT_MODE 1 +#endif #if !MDNS_STRICT_MODE /* mDNS responders sometimes repeat queries in responses From 92e511a5b60370c52081b8503f1154555dddc836 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 8 Mar 2021 19:40:53 +0100 Subject: [PATCH 2/4] mdns: Fix the resolver to correctly parse it's own non-strict answers The resolver was able to respond correctly, but would also resolve its own queries and cause issues with BCT 1.5.2, specifically * MULTIPLE QUESTIONS - DUPLICATE SUPPRESSION * MULTIPLE QUESTIONS - DISTRIBUTED DUPLICATE SUPPRESSION tests failed. --- components/mdns/mdns.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 3d3222f5cc..782dc6d2a8 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2796,13 +2796,12 @@ void mdns_parse_packet(mdns_rx_packet_t * packet) 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 && name->service[0] && name->proto && name->proto[0]) { - service = _mdns_get_service_item(name->service, name->proto); - } + } else if (!name->sub && _mdns_name_is_ours(name)) { + ours = true; + if (name->service && name->service[0] && name->proto && name->proto[0]) { + service = _mdns_get_service_item(name->service, name->proto); } + } else { if (!parsed_packet->authoritative || record_type == MDNS_NS) { //skip this record continue; From 4f4640b1df62f353d6eb59f309bbadcc5adbf3bd Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 19 Feb 2021 16:44:08 +0100 Subject: [PATCH 3/4] mdns: Fix parsing answers with questions when instance name not set mdns resolver didn't correctly resolved queries when host name wasn't assigned. Fixed by allowing processing also if some answer present (non-strict mode) Closes https://github.com/espressif/esp-idf/issues/6598 --- components/mdns/mdns.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 782dc6d2a8..1436072abe 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2665,7 +2665,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet) } //if we have not set the hostname, we can not answer questions - if (header.questions && _str_null_or_empty(_mdns_server->hostname)) { + if (header.questions && !header.answers && _str_null_or_empty(_mdns_server->hostname)) { free(parsed_packet); return; } From 19634e34e9c0ceb7338ed948b6737bae09fd672f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 28 Dec 2020 17:37:03 +0100 Subject: [PATCH 4/4] mdns: Fixed the ip header TTL to be correctly set to 255 Defined in https://tools.ietf.org/html/rfc6762#section-11: All Multicast DNS responses (including responses sent via unicast) SHOULD be sent with IP TTL set to 255 --- components/mdns/mdns_networking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mdns/mdns_networking.c b/components/mdns/mdns_networking.c index b7ca2816ee..dd521d81d2 100644 --- a/components/mdns/mdns_networking.c +++ b/components/mdns/mdns_networking.c @@ -38,7 +38,7 @@ static esp_err_t _udp_pcb_main_init(void) _pcb_main = NULL; return ESP_ERR_INVALID_STATE; } - _pcb_main->mcast_ttl = 1; + _pcb_main->mcast_ttl = 255; _pcb_main->remote_port = MDNS_SERVICE_PORT; ip_addr_copy(_pcb_main->remote_ip, ip_addr_any_type); udp_recv(_pcb_main, &_udp_recv, _mdns_server);