Skip to content

Commit 8a8d58d

Browse files
gjc13suren-gabrielyan-espressif
authored andcommitted
mdns: fix test script delayed response
* Original commit: espressif/esp-idf@a4f2639
1 parent 402baeb commit 8a8d58d

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

components/mdns/mdns.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ static bool _mdns_service_match(const mdns_service_t * srv, const char * service
160160
* @param server the server
161161
* @param service service type to match
162162
* @param proto proto to match
163+
* @param hostname hostname of the service (if non-null)
163164
*
164165
* @return the service item if found or NULL on error
165166
*/
@@ -177,7 +178,7 @@ static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char
177178

178179
static mdns_host_item_t * mdns_get_host_item(const char * hostname)
179180
{
180-
if (hostname == NULL || strcasecmp(hostname, _mdns_server->hostname) == 0) {
181+
if (strcasecmp(hostname, _mdns_server->hostname) == 0) {
181182
return &_mdns_self_host;
182183
}
183184
mdns_host_item_t * host = _mdns_host_list;
@@ -1122,22 +1123,14 @@ static void _mdns_free_tx_packet(mdns_tx_packet_t * packet)
11221123
if (!packet) {
11231124
return;
11241125
}
1125-
mdns_out_question_t *q = packet->questions;
1126+
mdns_out_question_t * q = packet->questions;
11261127
while (q) {
1127-
mdns_out_question_t *next = q->next;
1128+
mdns_out_question_t * next = q->next;
11281129
if (q->own_dynamic_memory) {
1129-
if (q->host) {
1130-
free((char *)q->host);
1131-
}
1132-
if (q->service) {
1133-
free((char *)q->service);
1134-
}
1135-
if (q->proto) {
1136-
free((char *)q->proto);
1137-
}
1138-
if (q->domain) {
1139-
free((char *)q->domain);
1140-
}
1130+
free((char *)q->host);
1131+
free((char *)q->service);
1132+
free((char *)q->proto);
1133+
free((char *)q->domain);
11411134
}
11421135
free(q);
11431136
q = next;
@@ -4870,7 +4863,8 @@ esp_err_t mdns_instance_name_set(const char * instance)
48704863
esp_err_t mdns_service_add_for_host(const char * instance, const char * service, const char * proto,
48714864
const char * hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items)
48724865
{
4873-
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
4866+
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || _str_null_or_empty(hostname) ||
4867+
!port) {
48744868
return ESP_ERR_INVALID_ARG;
48754869
}
48764870

@@ -4930,7 +4924,7 @@ esp_err_t mdns_service_add_for_host(const char * instance, const char * service,
49304924
esp_err_t mdns_service_add(const char * instance, const char * service, const char * proto, uint16_t port,
49314925
mdns_txt_item_t txt[], size_t num_items)
49324926
{
4933-
if (!_mdns_server) {
4927+
if (!_mdns_server || _str_null_or_empty(_mdns_server->hostname)) {
49344928
return ESP_ERR_INVALID_STATE;
49354929
}
49364930
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)
54055399
header.answers = 0;
54065400
header.additional = 0;
54075401
header.servers = 0;
5408-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5402+
_mdns_dbg_printf("ERROR: parse header questions\n");
54095403
break;
54105404
}
54115405

@@ -5453,7 +5447,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
54535447

54545448
content = _mdns_parse_fqdn(data, content, name);
54555449
if (!content) {
5456-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5450+
_mdns_dbg_printf("ERROR: parse mdns records\n");
54575451
break;
54585452
}
54595453

@@ -5467,7 +5461,7 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
54675461

54685462
content = data_ptr + data_len;
54695463
if (content > (data + len)) {
5470-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5464+
_mdns_dbg_printf("ERROR: content length overflow\n");
54715465
break;
54725466
}
54735467

@@ -5520,13 +5514,13 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
55205514
_mdns_dbg_printf("[%u] ", data_len);
55215515
if (type == MDNS_TYPE_PTR) {
55225516
if (!_mdns_parse_fqdn(data, data_ptr, name)) {
5523-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5517+
_mdns_dbg_printf("ERROR: parse PTR\n");
55245518
continue;
55255519
}
55265520
_mdns_dbg_printf("%s.%s.%s.%s.\n", name->host, name->service, name->proto, name->domain);
55275521
} else if (type == MDNS_TYPE_SRV) {
55285522
if (!_mdns_parse_fqdn(data, data_ptr + MDNS_SRV_FQDN_OFFSET, name)) {
5529-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5523+
_mdns_dbg_printf("ERROR: parse SRV\n");
55305524
continue;
55315525
}
55325526
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)
55385532
while (i < data_len) {
55395533
uint8_t partLen = data_ptr[i++];
55405534
if ((i+partLen) > data_len) {
5541-
_mdns_dbg_printf("ERROR: %s:%u\n", __FILE__, __LINE__);
5535+
_mdns_dbg_printf("ERROR: parse TXT\n");
55425536
break;
55435537
}
55445538
char txt[partLen+1];

examples/protocols/mdns/main/mdns_example_main.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string.h>
1010
#include "freertos/FreeRTOS.h"
1111
#include "freertos/task.h"
12+
#include "esp_netif_ip_addr.h"
1213
#include "esp_system.h"
1314
#include "esp_event.h"
1415
#include "esp_log.h"
@@ -33,10 +34,8 @@ static void query_mdns_host_with_getaddrinfo(char * host);
3334

3435
static void initialise_mdns(void)
3536
{
36-
char *hostname = generate_hostname();
37-
char delegated_hostname[64];
37+
char * hostname = generate_hostname();
3838

39-
snprintf(delegated_hostname, sizeof(delegated_hostname), "%s-delegated", hostname);
4039
//initialize mDNS
4140
ESP_ERROR_CHECK( mdns_init() );
4241
//set mDNS hostname (required if you want to advertise services)
@@ -56,19 +55,21 @@ static void initialise_mdns(void)
5655
ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
5756

5857
#if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
58+
char *delegated_hostname;
59+
if (-1 == asprintf(&delegated_hostname, "%s-delegated", hostname)) {
60+
abort();
61+
}
62+
5963
mdns_ip_addr_t addr4, addr6;
60-
ip4_addr_t ip4_addr;
61-
ip6_addr_t ip6_addr;
62-
ip4addr_aton("10.0.0.1", &ip4_addr); // mock address
63-
addr4.addr.u_addr.ip4.addr = ip4_addr.addr;
64+
esp_netif_str_to_ip4("10.0.0.1", &addr4.addr.u_addr.ip4);
6465
addr4.addr.type = ESP_IPADDR_TYPE_V4;
65-
addr4.next = &addr6;
66-
ip6addr_aton("fd11:22::1", &ip6_addr); // mock address
67-
memcpy(addr6.addr.u_addr.ip6.addr, ip6_addr.addr, sizeof(ip6_addr.addr));
66+
esp_netif_str_to_ip6("fd11:22::1", &addr6.addr.u_addr.ip6);
6867
addr6.addr.type = ESP_IPADDR_TYPE_V6;
68+
addr4.next = &addr6;
6969
addr6.next = NULL;
7070
ESP_ERROR_CHECK( mdns_delegate_hostname_add(delegated_hostname, &addr4) );
7171
ESP_ERROR_CHECK( mdns_service_add_for_host("test0", "_http", "_tcp", delegated_hostname, 1234, serviceTxtData, 3) );
72+
free(delegated_hostname);
7273
#endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
7374

7475
//add another TXT item

examples/protocols/mdns/mdns_example_test.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import select
34
import socket
45
import struct
56
import subprocess
@@ -10,6 +11,7 @@
1011
import dpkt.dns
1112
import ttfw_idf
1213
from tiny_test_fw import DUT
14+
from tiny_test_fw.Utility import console_log
1315

1416
stop_mdns_server = Event()
1517
esp_answered = Event()
@@ -19,7 +21,7 @@
1921
def get_dns_query_for_esp(esp_host):
2022
dns = dpkt.dns.DNS(b'\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01')
2123
dns.qd[0].name = esp_host + u'.local'
22-
print('Created query for esp host: {} '.format(dns.__repr__()))
24+
console_log('Created query for esp host: {} '.format(dns.__repr__()))
2325
return dns.pack()
2426

2527

@@ -33,7 +35,7 @@ def get_dns_answer_to_mdns(tester_host):
3335
arr.name = tester_host
3436
arr.ip = socket.inet_aton('127.0.0.1')
3537
dns. an.append(arr)
36-
print('Created answer to mdns query: {} '.format(dns.__repr__()))
38+
console_log('Created answer to mdns query: {} '.format(dns.__repr__()))
3739
return dns.pack()
3840

3941

@@ -57,35 +59,41 @@ def mdns_server(esp_host):
5759
MCAST_GRP = '224.0.0.251'
5860
TESTER_NAME = u'tinytester.local'
5961
TESTER_NAME_LWIP = u'tinytester-lwip.local'
62+
QUERY_TIMEOUT = 0.2
6063
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
6164
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
6265
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
66+
sock.setblocking(False)
6367
sock.bind((UDP_IP,UDP_PORT))
6468
mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
6569
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
66-
sock.settimeout(30)
70+
last_query_timepoint = time.time()
6771
while not stop_mdns_server.is_set():
6872
try:
69-
if not esp_answered.is_set():
73+
current_time = time.time()
74+
if not esp_answered.is_set() and current_time - last_query_timepoint > QUERY_TIMEOUT:
7075
sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
71-
time.sleep(0.2)
7276
sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
73-
time.sleep(0.2)
77+
last_query_timepoint = current_time
78+
timeout = max(0, QUERY_TIMEOUT - (current_time - last_query_timepoint))
79+
read_socks, _, _ = select.select([sock], [], [], timeout)
80+
if not read_socks:
81+
continue
7482
data, addr = sock.recvfrom(1024)
7583
dns = dpkt.dns.DNS(data)
7684
if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
7785
if dns.qd[0].name == TESTER_NAME:
78-
print('Received query: {} '.format(dns.__repr__()))
86+
console_log('Received query: {} '.format(dns.__repr__()))
7987
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME), (MCAST_GRP,UDP_PORT))
8088
elif dns.qd[0].name == TESTER_NAME_LWIP:
81-
print('Received query: {} '.format(dns.__repr__()))
89+
console_log('Received query: {} '.format(dns.__repr__()))
8290
sock.sendto(get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), addr)
8391
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
8492
if dns.an[0].name == esp_host + u'.local':
85-
print('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
93+
console_log('Received answer to esp32-mdns query: {}'.format(dns.__repr__()))
8694
esp_answered.set()
8795
if dns.an[0].name == esp_host + u'-delegated.local':
88-
print('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
96+
console_log('Received answer to esp32-mdns-delegate query: {}'.format(dns.__repr__()))
8997
esp_delegated_answered.set()
9098
except socket.timeout:
9199
break
@@ -117,7 +125,7 @@ def test_examples_protocol_mdns(env, extra_data):
117125
thread1.start()
118126
try:
119127
ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30)[0]
120-
print('Connected to AP with IP: {}'.format(ip_address))
128+
console_log('Connected to AP with IP: {}'.format(ip_address))
121129
except DUT.ExpectTimeout:
122130
stop_mdns_server.set()
123131
thread1.join()
@@ -135,7 +143,7 @@ def test_examples_protocol_mdns(env, extra_data):
135143
# 5. check the DUT answers to `dig` command
136144
dig_output = subprocess.check_output(['dig', '+short', '-p', '5353', '@224.0.0.251',
137145
'{}.local'.format(specific_host)])
138-
print('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
146+
console_log('Resolving {} using "dig" succeeded with:\n{}'.format(specific_host, dig_output))
139147
if not ip_address.encode('utf-8') in dig_output:
140148
raise ValueError('Test has failed: Incorrectly resolved DUT hostname using dig'
141149
"Output should've contained DUT's IP address:{}".format(ip_address))

0 commit comments

Comments
 (0)