feat(mdns): Add reverse lookup to the example and test

This commit is contained in:
David Cermak
2023-02-08 20:20:06 +01:00
parent d4825f5c53
commit d464ed7d30
4 changed files with 36 additions and 15 deletions

View File

@ -276,16 +276,15 @@ void app_main(void)
* This is typically performed in "GOT_IP" event handler, but we call it here directly * 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. * 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_ENABLE_IP4 | MDNS_EVENT_ENABLE_IP6));
ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4)); ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4 | MDNS_EVENT_ANNOUNCE_IP6));
#endif
#if defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES) #if defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
while (mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_IP4_REVERSE_LOOKUP) != ESP_OK) { ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_IP4_REVERSE_LOOKUP | MDNS_EVENT_IP6_REVERSE_LOOKUP));
vTaskDelay(50 / portTICK_PERIOD_MS);
}
#endif #endif
#endif // CONFIG_MDNS_ADD_CUSTOM_NETIF
initialise_button(); initialise_button();
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
} }

View File

@ -120,10 +120,11 @@ def test_examples_protocol_mdns(dut):
2. get the dut host name (and IP address) 2. get the dut host name (and IP address)
3. check the mdns name is accessible 3. check the mdns name is accessible
4. check DUT output if mdns advertized host is resolved 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( specific_host = dut.expect(r'mdns hostname set to: \[(.*?)\]')[1].decode()
b'mdns hostname set to: \[(.*?)\]')).group(1).decode() # noqa: W605
mdns_server_events = { mdns_server_events = {
'stop': Event(), 'stop': Event(),
@ -132,9 +133,14 @@ def test_examples_protocol_mdns(dut):
} }
mdns_responder = Thread(target=mdns_server, mdns_responder = Thread(target=mdns_server,
args=(str(specific_host), mdns_server_events)) args=(str(specific_host), mdns_server_events))
ip_address = dut.expect( ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]',
re.compile(b'IPv4 address:([a-zA-Z0-9]*).*')).group(1).decode() timeout=30)[1].decode()
print('Connected to AP with IP: {}'.format(ip_address)) 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: try:
# 3. check the mdns name is accessible. # 3. check the mdns name is accessible.
mdns_responder.start() mdns_responder.start()
@ -165,11 +171,25 @@ def test_examples_protocol_mdns(dut):
]) ])
print('Resolving {} using "dig" succeeded with:\n{}'.format( print('Resolving {} using "dig" succeeded with:\n{}'.format(
specific_host, dig_output)) 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( raise ValueError(
'Test has failed: Incorrectly resolved DUT hostname using dig' 'Test has failed: Incorrectly resolved DUT hostname using dig'
"Output should've contained DUT's IP address:{}".format( "Output should've contained DUT's IP address:{}".format(ipv4))
ip_address)) # 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: finally:
mdns_server_events['stop'].set() mdns_server_events['stop'].set()
mdns_responder.join() mdns_responder.join()

View File

@ -6,6 +6,7 @@ CONFIG_MDNS_PREDEF_NETIF_STA=n
CONFIG_MDNS_PREDEF_NETIF_AP=n CONFIG_MDNS_PREDEF_NETIF_AP=n
CONFIG_MDNS_PREDEF_NETIF_ETH=n CONFIG_MDNS_PREDEF_NETIF_ETH=n
CONFIG_MDNS_ADD_CUSTOM_NETIF=y CONFIG_MDNS_ADD_CUSTOM_NETIF=y
CONFIG_MDNS_RESPOND_REVERSE_QUERIES=y
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_ETHERNET=y
CONFIG_EXAMPLE_CONNECT_WIFI=n CONFIG_EXAMPLE_CONNECT_WIFI=n

View File

@ -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) { if (action & MDNS_EVENT_IP6_REVERSE_LOOKUP) {
esp_ip6_addr_t addr6; esp_ip6_addr_t addr6;
if (!esp_netif_get_ip6_linklocal(_mdns_get_esp_netif(mdns_if), &addr6) && !_ipv6_address_is_zero(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 */ #endif /* CONFIG_MDNS_RESPOND_REVERSE_QUERIES */
} }