mdns: fix test script delayed response

This commit is contained in:
Jiacheng Guo 2021-05-11 13:47:03 +08:00
parent e6135552d2
commit a4f263948c
3 changed files with 48 additions and 45 deletions

View File

@ -160,6 +160,7 @@ static bool _mdns_service_match(const mdns_service_t * srv, const char * service
* @param server the server
* @param service service type to match
* @param proto proto to match
* @param hostname hostname of the service (if non-null)
*
* @return the service item if found or NULL on error
*/
@ -177,7 +178,7 @@ static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char
static mdns_host_item_t * mdns_get_host_item(const char * hostname)
{
if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
if (strcasecmp(hostname, _mdns_server->hostname) == 0) {
return &_mdns_self_host;
}
mdns_host_item_t * host = _mdns_host_list;
@ -1122,22 +1123,14 @@ static void _mdns_free_tx_packet(mdns_tx_packet_t * packet)
if (!packet) {
return;
}
mdns_out_question_t *q = packet->questions;
mdns_out_question_t * q = packet->questions;
while (q) {
mdns_out_question_t *next = q->next;
mdns_out_question_t * next = q->next;
if (q->own_dynamic_memory) {
if (q->host) {
free((char *)q->host);
}
if (q->service) {
free((char *)q->service);
}
if (q->proto) {
free((char *)q->proto);
}
if (q->domain) {
free((char *)q->domain);
}
free((char *)q->host);
free((char *)q->service);
free((char *)q->proto);
free((char *)q->domain);
}
free(q);
q = next;
@ -4870,7 +4863,8 @@ esp_err_t mdns_instance_name_set(const char * instance)
esp_err_t mdns_service_add_for_host(const char * instance, const char * service, const char * proto,
const char * hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items)
{
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || _str_null_or_empty(hostname) ||
!port) {
return ESP_ERR_INVALID_ARG;
}
@ -4930,7 +4924,7 @@ esp_err_t mdns_service_add_for_host(const char * instance, const char * service,
esp_err_t mdns_service_add(const char * instance, const char * service, const char * proto, uint16_t port,
mdns_txt_item_t txt[], size_t num_items)
{
if (!_mdns_server) {
if (!_mdns_server || _str_null_or_empty(_mdns_server->hostname)) {
return ESP_ERR_INVALID_STATE;
}
return mdns_service_add_for_host(instance, service, proto, _mdns_server->hostname, port, txt, num_items);
@ -5405,7 +5399,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
header.answers = 0;
header.additional = 0;
header.servers = 0;
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: parse header questions\n");
break;
}
@ -5453,7 +5447,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
content = _mdns_parse_fqdn(data, content, name);
if (!content) {
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: parse mdns records\n");
break;
}
@ -5467,7 +5461,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
content = data_ptr + data_len;
if (content > (data + len)) {
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: content length overflow\n");
break;
}
@ -5520,13 +5514,13 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
_mdns_dbg_printf("[%u] ", data_len);
if (type == MDNS_TYPE_PTR) {
if (!_mdns_parse_fqdn(data, data_ptr, name)) {
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: parse PTR\n");
continue;
}
_mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain);
} else if (type == MDNS_TYPE_SRV) {
if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: parse SRV\n");
continue;
}
uint16_t priority = _mdns_read_u16(data_ptr, MDNS_SRV_PRIORITY_OFFSET);
@ -5538,7 +5532,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
while (i < data_len) {
uint8_t partLen = data_ptr[i++];
if ((i+partLen) > data_len) {
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
_mdns_dbg_printf("ERROR: parse TXT\n");
break;
}
char txt[partLen+1];

View File

@ -9,6 +9,7 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif_ip_addr.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
@ -33,10 +34,8 @@ static void query_mdns_host_with_getaddrinfo(char * host);
static void initialise_mdns(void)
{
char *hostname = generate_hostname();
char delegated_hostname[64];
char * hostname = generate_hostname();
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)
@ -56,19 +55,21 @@ static void initialise_mdns(void)
ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
#if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
char *delegated_hostname;
if (-1 == asprintf(&delegated_hostname, "%s-delegated", hostname)) {
abort();
}
mdns_ip_addr_t addr4, addr6;
ip4_addr_t ip4_addr;
ip6_addr_t ip6_addr;
ip4addr_aton("10.0.0.1", &ip4_addr); // mock address
addr4.addr.u_addr.ip4.addr = ip4_addr.addr;
esp_netif_str_to_ip4("10.0.0.1", &addr4.addr.u_addr.ip4);
addr4.addr.type = ESP_IPADDR_TYPE_V4;
addr4.next = &addr6;
ip6addr_aton("fd11:22::1", &ip6_addr); // mock address
memcpy(addr6.addr.u_addr.ip6.addr, ip6_addr.addr, sizeof(ip6_addr.addr));
esp_netif_str_to_ip6("fd11:22::1", &addr6.addr.u_addr.ip6);
addr6.addr.type = ESP_IPADDR_TYPE_V6;
addr4.next = &addr6;
addr6.next = NULL;
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) );
free(delegated_hostname);
#endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
//add another TXT item

View File

@ -1,5 +1,6 @@
import os
import re
import select
import socket
import struct
import subprocess
@ -10,6 +11,7 @@ import dpkt
import dpkt.dns
import ttfw_idf
from tiny_test_fw import DUT
from tiny_test_fw.Utility import console_log
stop_mdns_server = Event()
esp_answered = Event()
@ -19,7 +21,7 @@ esp_delegated_answered = Event()
def get_dns_query_for_esp(esp_host):
dns = dpkt.dns.DNS(b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01')
dns.qd[0].name = esp_host + u'.local'
print('Created query for esp host: {} '.format(dns.__repr__()))
console_log('Created query for esp host: {} '.format(dns.__repr__()))
return dns.pack()
@ -33,7 +35,7 @@ def get_dns_answer_to_mdns(tester_host):
arr.name = tester_host
arr.ip = socket.inet_aton('127.0.0.1')
dns. an.append(arr)
print('Created answer to mdns query: {} '.format(dns.__repr__()))
console_log('Created answer to mdns query: {} '.format(dns.__repr__()))
return dns.pack()
@ -57,35 +59,41 @@ def mdns_server(esp_host):
MCAST_GRP = '224.0.0.251'
TESTER_NAME = u'tinytester.local'
TESTER_NAME_LWIP = u'tinytester-lwip.local'
QUERY_TIMEOUT = 0.2
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.setblocking(False)
sock.bind((UDP_IP,UDP_PORT))
mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(30)
last_query_timepoint = time.time()
while not stop_mdns_server.is_set():
try:
if not esp_answered.is_set():
current_time = time.time()
if not esp_answered.is_set() and current_time - last_query_timepoint > QUERY_TIMEOUT:
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)
last_query_timepoint = current_time
timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
read_socks, _, _ = select.select([sock], [], [], timeout)
if not read_socks:
continue
data, addr = sock.recvfrom(1024)
dns = dpkt.dns.DNS(data)
if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
if dns.qd[0].name == TESTER_NAME:
print('Received query: {} '.format(dns.__repr__()))
console_log('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), (MCAST_GRP,UDP_PORT))
elif dns.qd[0].name == TESTER_NAME_LWIP:
print('Received query: {} '.format(dns.__repr__()))
console_log('Received query: {} '.format(dns.__repr__()))
sock.sendto(get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), addr)
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
if dns.an[0].name == esp_host + u'.local':
print('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
console_log('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__()))
console_log('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
esp_delegated_answered.set()
except socket.timeout:
break
@ -117,7 +125,7 @@ def test_examples_protocol_mdns(env, extra_data):
thread1.start()
try:
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)[0]
print('Connected to AP with IP: {}'.format(ip_address))
console_log('Connected to AP with IP: {}'.format(ip_address))
except DUT.ExpectTimeout:
stop_mdns_server.set()
thread1.join()
@ -135,7 +143,7 @@ def test_examples_protocol_mdns(env, extra_data):
# 5. check the DUT answers to `dig` command
dig_output = subprocess.check_output(['dig', '+short', '-p', '5353', '@224.0.0.251',
'{}.local'.format(specific_host)])
print('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
console_log('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
if not ip_address.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))