From d464ed7d3067dbb6426c8a7249bfb45a17605f04 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 8 Feb 2023 20:20:06 +0100 Subject: [PATCH] feat(mdns): Add reverse lookup to the example and test --- .../mdns/examples/main/mdns_example_main.c | 11 +++--- components/mdns/examples/pytest_mdns.py | 36 ++++++++++++++----- .../examples/sdkconfig.ci.eth_custom_netif | 1 + components/mdns/mdns.c | 3 +- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/components/mdns/examples/main/mdns_example_main.c b/components/mdns/examples/main/mdns_example_main.c index bd2cfe147..181dfed88 100644 --- a/components/mdns/examples/main/mdns_example_main.c +++ b/components/mdns/examples/main/mdns_example_main.c @@ -276,16 +276,15 @@ void app_main(void) * This is typically performed in "GOT_IP" event handler, but we call it here directly * since the `EXAMPLE_INTERFACE` netif is connected already, to keep the example simple. */ - ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4)); - ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4)); -#endif + ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4 | MDNS_EVENT_ENABLE_IP6)); + ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4 | MDNS_EVENT_ANNOUNCE_IP6)); #if defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES) - while (mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_IP4_REVERSE_LOOKUP) != ESP_OK) { - vTaskDelay(50 / portTICK_PERIOD_MS); - } + ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_IP4_REVERSE_LOOKUP | MDNS_EVENT_IP6_REVERSE_LOOKUP)); #endif +#endif // CONFIG_MDNS_ADD_CUSTOM_NETIF + initialise_button(); xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); } diff --git a/components/mdns/examples/pytest_mdns.py b/components/mdns/examples/pytest_mdns.py index f13abc149..42364dfe3 100644 --- a/components/mdns/examples/pytest_mdns.py +++ b/components/mdns/examples/pytest_mdns.py @@ -120,10 +120,11 @@ def test_examples_protocol_mdns(dut): 2. get the dut host name (and IP address) 3. check the mdns name is accessible 4. check DUT output if mdns advertized host is resolved + 5. check if DUT responds to dig + 6. check the DUT is searchable via reverse IP lookup """ - specific_host = dut.expect(re.compile( - b'mdns hostname set to: \[(.*?)\]')).group(1).decode() # noqa: W605 + specific_host = dut.expect(r'mdns hostname set to: \[(.*?)\]')[1].decode() mdns_server_events = { 'stop': Event(), @@ -132,9 +133,14 @@ def test_examples_protocol_mdns(dut): } mdns_responder = Thread(target=mdns_server, args=(str(specific_host), mdns_server_events)) - ip_address = dut.expect( - re.compile(b'IPv4 address:([a-zA-Z0-9]*).*')).group(1).decode() - print('Connected to AP with IP: {}'.format(ip_address)) + ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', + timeout=30)[1].decode() + ip_addresses = [ipv4] + if dut.app.sdkconfig.get('LWIP_IPV6') is True: + ipv6_r = r':'.join((r'[0-9a-fA-F]{4}', ) * 8) + ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode() + ip_addresses.append(ipv6) + print('Connected with IP addresses: {}'.format(','.join(ip_addresses))) try: # 3. check the mdns name is accessible. mdns_responder.start() @@ -165,11 +171,25 @@ def test_examples_protocol_mdns(dut): ]) print('Resolving {} using "dig" succeeded with:\n{}'.format( specific_host, dig_output)) - if not ip_address.encode('utf-8') in dig_output: + if not ipv4.encode('utf-8') in dig_output: raise ValueError( 'Test has failed: Incorrectly resolved DUT hostname using dig' - "Output should've contained DUT's IP address:{}".format( - ip_address)) + "Output should've contained DUT's IP address:{}".format(ipv4)) + # 6. check the DUT reverse lookup + if dut.app.sdkconfig.get('MDNS_RESPOND_REVERSE_QUERIES') is True: + for ip_address in ip_addresses: + dig_output = subprocess.check_output([ + 'dig', '+short', '-p', '5353', '@224.0.0.251', '-x', + '{}'.format(ip_address) + ]) + print('Reverse lookup for {} using "dig" succeeded with:\n{}'. + format(ip_address, dig_output)) + if specific_host not in dig_output.decode(): + raise ValueError( + 'Test has failed: Incorrectly resolved DUT IP address using dig' + "Output should've contained DUT's name:{}".format( + specific_host)) + finally: mdns_server_events['stop'].set() mdns_responder.join() diff --git a/components/mdns/examples/sdkconfig.ci.eth_custom_netif b/components/mdns/examples/sdkconfig.ci.eth_custom_netif index 56499bd43..b9d712041 100644 --- a/components/mdns/examples/sdkconfig.ci.eth_custom_netif +++ b/components/mdns/examples/sdkconfig.ci.eth_custom_netif @@ -6,6 +6,7 @@ CONFIG_MDNS_PREDEF_NETIF_STA=n CONFIG_MDNS_PREDEF_NETIF_AP=n CONFIG_MDNS_PREDEF_NETIF_ETH=n CONFIG_MDNS_ADD_CUSTOM_NETIF=y +CONFIG_MDNS_RESPOND_REVERSE_QUERIES=y CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 7e7a49f92..efdafa4ab 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -4016,6 +4016,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) } } +#ifdef CONFIG_LWIP_IPV6 if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) { esp_ip6_addr_t addr6; if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(addr6)) { @@ -4039,7 +4040,7 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) } } } - +#endif /* CONFIG_LWIP_IPV6 */ #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */ }