mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-20 14:02:21 +02:00
mdns: fix mdns probe/reply behavior
* send correct hostnames when probing. * add test for mdns host delegation. * Original commit: espressif/esp-idf@d2a5d25984
This commit is contained in:
committed by
suren-gabrielyan-espressif
parent
4049b3b5ed
commit
418fb60dd9
@ -5,6 +5,7 @@ Shows how to use mDNS to advertise lookup services and hosts
|
||||
## Example workflow
|
||||
|
||||
- mDNS is initialized with host name and instance name defined through the project configuration and `_http._tcp` service is added to be advertised
|
||||
- A delegated host `esp32-delegated._local` is added and another `_http._tcp` service is added for this host.
|
||||
- WiFi STA is started and trying to connect to the access point defined through the project configuration
|
||||
- The system event handler is used to pass the network events to mDNS so the service is aware when the interface comes up or down
|
||||
- GPIO0 (BOOT Button) is initialized as pulled-up input that can be monitored for button press
|
||||
|
@ -12,6 +12,12 @@ menu "Example Configuration"
|
||||
help
|
||||
mDNS Instance Name for example to use
|
||||
|
||||
config MDNS_PUBLISH_DELEGATE_HOST
|
||||
bool "Publish a delegated host"
|
||||
help
|
||||
Enable publishing a delegated host other than ESP32.
|
||||
The example will also add a mock service for this host.
|
||||
|
||||
config MDNS_RESOLVE_TEST_SERVICES
|
||||
bool "Resolve test services"
|
||||
default n
|
||||
|
@ -34,6 +34,9 @@ static void query_mdns_host_with_getaddrinfo(char * host);
|
||||
static void initialise_mdns(void)
|
||||
{
|
||||
char *hostname = generate_hostname();
|
||||
char delegated_hostname[64];
|
||||
|
||||
snprintf(delegated_hostname, sizeof(delegated_hostname), "%s-delegated", hostname);
|
||||
//initialize mDNS
|
||||
ESP_ERROR_CHECK( mdns_init() );
|
||||
//set mDNS hostname (required if you want to advertise services)
|
||||
@ -51,19 +54,23 @@ static void initialise_mdns(void)
|
||||
|
||||
//initialize service
|
||||
ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
|
||||
|
||||
#if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
|
||||
mdns_ip_addr_t addr4, addr6;
|
||||
ip4_addr_t ip4_addr;
|
||||
ip6_addr_t ip6_addr;
|
||||
ip4addr_aton("10.0.0.1", &ip4_addr);
|
||||
ip4addr_aton("10.0.0.1", &ip4_addr); // mock address
|
||||
addr4.addr.u_addr.ip4.addr = ip4_addr.addr;
|
||||
addr4.addr.type = ESP_IPADDR_TYPE_V4;
|
||||
addr4.next = &addr6;
|
||||
ip6addr_aton("fd11:22::1", &ip6_addr);
|
||||
ip6addr_aton("fd11:22::1", &ip6_addr); // mock address
|
||||
memcpy(addr6.addr.u_addr.ip6.addr, ip6_addr.addr, sizeof(ip6_addr.addr));
|
||||
addr6.addr.type = ESP_IPADDR_TYPE_V6;
|
||||
addr6.next = NULL;
|
||||
ESP_ERROR_CHECK( mdns_delegate_hostname_add("test-device", &addr4) );
|
||||
ESP_ERROR_CHECK( mdns_service_add_for_host("test0", "_http", "_tcp", "test-device", 1234, serviceTxtData, 3) );
|
||||
ESP_ERROR_CHECK( mdns_delegate_hostname_add(delegated_hostname, &addr4) );
|
||||
ESP_ERROR_CHECK( mdns_service_add_for_host("test0", "_http", "_tcp", delegated_hostname, 1234, serviceTxtData, 3) );
|
||||
#endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
|
||||
|
||||
//add another TXT item
|
||||
ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") );
|
||||
//change TXT item value
|
||||
@ -186,16 +193,10 @@ static void mdns_example_task(void *pvParameters)
|
||||
query_mdns_host_with_gethostbyname("tinytester-lwip.local");
|
||||
query_mdns_host_with_getaddrinfo("tinytester-lwip.local");
|
||||
#endif
|
||||
bool removed = false;
|
||||
|
||||
while (1) {
|
||||
check_button();
|
||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||
if (pdTICKS_TO_MS(xTaskGetTickCount()) >= 15 * 1000 && ! removed) {
|
||||
ESP_LOGI(TAG, "Remove delegate device\n");
|
||||
ESP_ERROR_CHECK(mdns_delegate_hostname_remove("test-device"));
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ from tiny_test_fw import DUT
|
||||
|
||||
stop_mdns_server = Event()
|
||||
esp_answered = Event()
|
||||
esp_delegated_answered = Event()
|
||||
|
||||
|
||||
def get_dns_query_for_esp(esp_host):
|
||||
@ -68,6 +69,8 @@ def mdns_server(esp_host):
|
||||
if not esp_answered.is_set():
|
||||
sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
|
||||
time.sleep(0.2)
|
||||
sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
|
||||
time.sleep(0.2)
|
||||
data, addr = sock.recvfrom(1024)
|
||||
dns = dpkt.dns.DNS(data)
|
||||
if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
|
||||
@ -81,6 +84,9 @@ def mdns_server(esp_host):
|
||||
if dns.an[0].name == esp_host + u'.local':
|
||||
print('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
|
||||
esp_answered.set()
|
||||
if dns.an[0].name == esp_host + u'-delegated.local':
|
||||
print('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
|
||||
esp_delegated_answered.set()
|
||||
except socket.timeout:
|
||||
break
|
||||
except dpkt.UnpackError:
|
||||
@ -120,6 +126,8 @@ def test_examples_protocol_mdns(env, extra_data):
|
||||
# 3. check the mdns name is accessible
|
||||
if not esp_answered.wait(timeout=30):
|
||||
raise ValueError('Test has failed: did not receive mdns answer within timeout')
|
||||
if not esp_delegated_answered.wait(timeout=30):
|
||||
raise ValueError('Test has failed: did not receive mdns answer for delegated host within timeout')
|
||||
# 4. check DUT output if mdns advertized host is resolved
|
||||
dut1.expect(re.compile(r'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1'), timeout=30)
|
||||
dut1.expect(re.compile(r'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'), timeout=30)
|
||||
|
Reference in New Issue
Block a user