From 6021a886575c0c943d21bdd284c8dce0a75c3b78 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 28 Jul 2020 09:06:12 +0200 Subject: [PATCH] mdns: Support queries in responses in mDNS non-strict mode By default adds original queries to responses in order to be resolved by some resolvers, such as lwIP mdns library. This functionality however is discouraged by the RFC6762, so it could be disabled in menuconfig if MDNS_STRICT_MODE configured Closes https://github.com/espressif/esp-idf/issues/5521 * Original commit: espressif/esp-idf@bcfa36db8ffff997f1f95eaf6b011ffc4d46a10f --- components/mdns/mdns.c | 11 +++++++++++ components/mdns/private_include/mdns_private.h | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 750456c7e..debc02651 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -1313,6 +1313,17 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t * parsed _mdns_free_tx_packet(packet); return; } +#ifdef MDNS_REPEAT_QUERY_IN_RESPONSE + mdns_out_question_t * out_question = malloc(sizeof(mdns_out_question_t)); + if (out_question == NULL) { + HOOK_MALLOC_FAILED; + _mdns_free_tx_packet(packet); + return; + } + memcpy(out_question, q, sizeof(mdns_out_question_t)); + out_question->next = NULL; + queueToEnd(mdns_out_question_t, packet->questions, out_question); +#endif // MDNS_REPEAT_QUERY_IN_RESPONSE } else if (!_mdns_alloc_answer(&packet->answers, q->type, NULL, send_flush, false)) { _mdns_free_tx_packet(packet); return; diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index f14619f06..3fa57e597 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -23,6 +23,22 @@ #define _mdns_dbg_printf(...) printf(__VA_ARGS__) #endif +/** mDNS strict mode: Set this to 1 for the mDNS library to strictly follow the RFC6762: + * Strict features: + * - to do not set original questions in response packets per RFC6762, sec 6 + * + * The actual 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. + */ +#define MDNS_STRICT_MODE 0 + +#if !MDNS_STRICT_MODE +/* mDNS responders sometimes repeat queries in responses + * but according to RFC6762, sec 6: Responses MUST NOT contain + * any item in question field */ +#define MDNS_REPEAT_QUERY_IN_RESPONSE 1 +#endif /** The maximum number of services */ #define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES