From 0eee31546dd4e6df0d1c1cc2740da0675dffb4bf 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 60dfeb4ee5..9ed0dcfbd1 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -32,7 +32,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 b649603a0d70ec804567f57752c3eddaed56198f 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 401d931225..ebf2c63c55 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2815,13 +2815,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 34049454dfaf5132d9b258ef4d04921befc8997b 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 ebf2c63c55..6d47084314 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2679,7 +2679,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 5cce919cbef87f543bb9f5275b77b97b3b1ea67e 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 152ab2798e..48d11918ba 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_ANY_TYPE)); udp_recv(_pcb_main, &_udp_recv, _mdns_server);