Skip to content

Commit 2646dcd

Browse files
committed
fix(mdns): fix compiling issue when disabling IPv4
1 parent de8ec67 commit 2646dcd

File tree

9 files changed

+243
-132
lines changed

9 files changed

+243
-132
lines changed

components/mdns/examples/query_advertise/main/mdns_example_main.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#include "driver/gpio.h"
2222
#include "netdb.h"
2323

24+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
25+
/* CONFIG_LWIP_IPV4 was introduced in IDF v5.1, set CONFIG_LWIP_IPV4 to 1 by default for IDF v5.0 */
26+
#ifndef CONFIG_LWIP_IPV4
27+
#define CONFIG_LWIP_IPV4 1
28+
#endif // CONFIG_LWIP_IPV4
29+
#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
2430

2531
#define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
2632
#define EXAMPLE_BUTTON_GPIO CONFIG_MDNS_BUTTON_GPIO
@@ -227,7 +233,7 @@ static void query_mdns_hosts_async(const char *host_name)
227233
vTaskDelay(50 / portTICK_PERIOD_MS);
228234
}
229235
}
230-
236+
#ifdef CONFIG_LWIP_IPV4
231237
static void query_mdns_host(const char *host_name)
232238
{
233239
ESP_LOGI(TAG, "Query A: %s.local", host_name);
@@ -247,6 +253,7 @@ static void query_mdns_host(const char *host_name)
247253

248254
ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
249255
}
256+
#endif // CONFIG_LWIP_IPV4
250257

251258
static void initialise_button(void)
252259
{
@@ -265,7 +272,9 @@ static void check_button(void)
265272
bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO);
266273
if (!new_level && old_level) {
267274
query_mdns_hosts_async("esp32-mdns");
275+
#ifdef CONFIG_LWIP_IPV4
268276
query_mdns_host("esp32");
277+
#endif
269278
query_mdns_service("_arduino", "_tcp");
270279
query_mdns_service("_http", "_tcp");
271280
query_mdns_service("_printer", "_tcp");
@@ -286,7 +295,9 @@ static void mdns_example_task(void *pvParameters)
286295
{
287296
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
288297
/* Send initial queries that are started by CI tester */
298+
#ifdef CONFIG_LWIP_IPV4
289299
query_mdns_host("tinytester");
300+
#endif
290301
query_mdns_host_with_gethostbyname("tinytester-lwip.local");
291302
query_mdns_host_with_getaddrinfo("tinytester-lwip.local");
292303
#endif
@@ -362,7 +373,16 @@ static void query_mdns_host_with_gethostbyname(char *host)
362373
if (res) {
363374
unsigned int i = 0;
364375
while (res->h_addr_list[i] != NULL) {
365-
ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])));
376+
ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host,
377+
#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4)
378+
res->h_addrtype == AF_INET ? inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) :
379+
inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i]))
380+
#elif defined(CONFIG_LWIP_IPV6)
381+
inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i]))
382+
#else
383+
inet_ntoa(*(struct in_addr *) (res->h_addr_list[i]))
384+
#endif
385+
);
366386
i++;
367387
}
368388
}
@@ -384,10 +404,12 @@ static void query_mdns_host_with_getaddrinfo(char *host)
384404
if (!getaddrinfo(host, NULL, &hints, &res)) {
385405
while (res) {
386406
char *resolved_addr;
387-
#if CONFIG_LWIP_IPV6
407+
#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4)
388408
resolved_addr = res->ai_family == AF_INET ?
389409
inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) :
390-
inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
410+
inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
411+
#elif defined(CONFIG_LWIP_IPV6)
412+
resolved_addr = inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
391413
#else
392414
resolved_addr = inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr);
393415
#endif // CONFIG_LWIP_IPV6

components/mdns/examples/query_advertise/pytest_mdns.py

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,30 @@ def mdns_server(esp_host, events):
8787
continue
8888
data, addr = sock.recvfrom(1024)
8989
dns = dpkt.dns.DNS(data)
90-
if len(dns.qd) > 0 and dns.qd[0].type == dpkt.dns.DNS_A:
91-
if dns.qd[0].name == TESTER_NAME:
92-
print('Received query: {} '.format(dns.__repr__()))
93-
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME),
94-
(MCAST_GRP, UDP_PORT))
95-
elif dns.qd[0].name == TESTER_NAME_LWIP:
96-
print('Received query: {} '.format(dns.__repr__()))
97-
sock.sendto(
98-
get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id),
99-
addr)
100-
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
101-
print('Received answer from {}'.format(dns.an[0].name))
102-
if dns.an[0].name == esp_host + u'.local':
103-
print('Received answer to esp32-mdns query: {}'.format(
104-
dns.__repr__()))
105-
events['esp_answered'].set()
106-
if dns.an[0].name == esp_host + u'-delegated.local':
107-
print('Received answer to esp32-mdns-delegate query: {}'.
108-
format(dns.__repr__()))
109-
events['esp_delegated_answered'].set()
90+
if len(dns.qd) > 0:
91+
for dns_query in dns.qd:
92+
if dns_query.type == dpkt.dns.DNS_A:
93+
if dns_query.name == TESTER_NAME:
94+
print('Received query: {} '.format(dns.__repr__()))
95+
sock.sendto(get_dns_answer_to_mdns(TESTER_NAME),
96+
(MCAST_GRP, UDP_PORT))
97+
elif dns_query.name == TESTER_NAME_LWIP:
98+
print('Received query: {} '.format(dns.__repr__()))
99+
sock.sendto(
100+
get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id),
101+
addr)
102+
if len(dns.an) > 0:
103+
for dns_answer in dns.an:
104+
if dns_answer.type == dpkt.dns.DNS_A:
105+
print('Received answer from {}'.format(dns_answer.name))
106+
if dns_answer.name == esp_host + u'.local':
107+
print('Received answer to esp32-mdns query: {}'.format(
108+
dns.__repr__()))
109+
events['esp_answered'].set()
110+
if dns_answer.name == esp_host + u'-delegated.local':
111+
print('Received answer to esp32-mdns-delegate query: {}'.format(
112+
dns.__repr__()))
113+
events['esp_delegated_answered'].set()
110114
except socket.timeout:
111115
break
112116
except dpkt.UnpackError:
@@ -133,62 +137,66 @@ def test_examples_protocol_mdns(dut):
133137
}
134138
mdns_responder = Thread(target=mdns_server,
135139
args=(str(specific_host), mdns_server_events))
136-
ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]',
137-
timeout=30)[1].decode()
138-
ip_addresses = [ipv4]
140+
ip_addresses = []
141+
if dut.app.sdkconfig.get('LWIP_IPV4') is True:
142+
ipv4 = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]',
143+
timeout=30)[1].decode()
144+
ip_addresses.append(ipv4)
139145
if dut.app.sdkconfig.get('LWIP_IPV6') is True:
140146
ipv6_r = r':'.join((r'[0-9a-fA-F]{4}', ) * 8)
141147
ipv6 = dut.expect(ipv6_r, timeout=30)[0].decode()
142148
ip_addresses.append(ipv6)
143149
print('Connected with IP addresses: {}'.format(','.join(ip_addresses)))
144150
try:
145-
# 3. check the mdns name is accessible.
151+
# TODO: Add test for example disabling IPV4
146152
mdns_responder.start()
147-
if not mdns_server_events['esp_answered'].wait(timeout=30):
148-
raise ValueError(
149-
'Test has failed: did not receive mdns answer within timeout')
150-
if not mdns_server_events['esp_delegated_answered'].wait(timeout=30):
151-
raise ValueError(
152-
'Test has failed: did not receive mdns answer for delegated host within timeout'
153+
if dut.app.sdkconfig.get('LWIP_IPV4') is True:
154+
# 3. check the mdns name is accessible.
155+
if not mdns_server_events['esp_answered'].wait(timeout=30):
156+
raise ValueError(
157+
'Test has failed: did not receive mdns answer within timeout')
158+
if not mdns_server_events['esp_delegated_answered'].wait(timeout=30):
159+
raise ValueError(
160+
'Test has failed: did not receive mdns answer for delegated host within timeout'
161+
)
162+
# 4. check DUT output if mdns advertized host is resolved
163+
dut.expect(
164+
re.compile(
165+
b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1')
153166
)
154-
# 4. check DUT output if mdns advertized host is resolved
155-
dut.expect(
156-
re.compile(
157-
b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1')
158-
)
159-
dut.expect(
160-
re.compile(
161-
b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'
162-
))
163-
dut.expect(
164-
re.compile(
165-
b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'
166-
))
167-
# 5. check the DUT answers to `dig` command
168-
dig_output = subprocess.check_output([
169-
'dig', '+short', '-p', '5353', '@224.0.0.251',
170-
'{}.local'.format(specific_host)
171-
])
172-
print('Resolving {} using "dig" succeeded with:\n{}'.format(
173-
specific_host, dig_output))
174-
if not ipv4.encode('utf-8') in dig_output:
175-
raise ValueError(
176-
'Test has failed: Incorrectly resolved DUT hostname using dig'
177-
"Output should've contained DUT's IP address:{}".format(ipv4))
178-
# 6. check the DUT reverse lookup
179-
if dut.app.sdkconfig.get('MDNS_RESPOND_REVERSE_QUERIES') is True:
180-
for ip_address in ip_addresses:
181-
dig_output = subprocess.check_output([
182-
'dig', '+short', '-p', '5353', '@224.0.0.251', '-x',
183-
'{}'.format(ip_address)
184-
])
185-
print('Reverse lookup for {} using "dig" succeeded with:\n{}'.
186-
format(ip_address, dig_output))
187-
if specific_host not in dig_output.decode():
188-
raise ValueError(
189-
'Test has failed: Incorrectly resolved DUT IP address using dig'
190-
"Output should've contained DUT's name:{}".format(
191-
specific_host))
167+
dut.expect(
168+
re.compile(
169+
b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'
170+
))
171+
dut.expect(
172+
re.compile(
173+
b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'
174+
))
175+
# 5. check the DUT answers to `dig` command
176+
dig_output = subprocess.check_output([
177+
'dig', '+short', '-p', '5353', '@224.0.0.251',
178+
'{}.local'.format(specific_host)
179+
])
180+
print('Resolving {} using "dig" succeeded with:\n{}'.format(
181+
specific_host, dig_output))
182+
if not ipv4.encode('utf-8') in dig_output:
183+
raise ValueError(
184+
'Test has failed: Incorrectly resolved DUT hostname using dig'
185+
"Output should've contained DUT's IP address:{}".format(ipv4))
186+
# 6. check the DUT reverse lookup
187+
if dut.app.sdkconfig.get('MDNS_RESPOND_REVERSE_QUERIES') is True:
188+
for ip_address in ip_addresses:
189+
dig_output = subprocess.check_output([
190+
'dig', '+short', '-p', '5353', '@224.0.0.251', '-x',
191+
'{}'.format(ip_address)
192+
])
193+
print('Reverse lookup for {} using "dig" succeeded with:\n{}'.
194+
format(ip_address, dig_output))
195+
if specific_host not in dig_output.decode():
196+
raise ValueError(
197+
'Test has failed: Incorrectly resolved DUT IP address using dig'
198+
"Output should've contained DUT's name:{}".format(
199+
specific_host))
192200

193201
finally:
194202
mdns_server_events['stop'].set()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CONFIG_IDF_TARGET="esp32"
2+
CONFIG_MDNS_RESOLVE_TEST_SERVICES=y
3+
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y
4+
CONFIG_MDNS_PUBLISH_DELEGATE_HOST=y
5+
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
6+
CONFIG_LWIP_IPV4=n
7+
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
8+
CONFIG_EXAMPLE_CONNECT_WIFI=n
9+
CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y
10+
CONFIG_EXAMPLE_ETH_PHY_IP101=y
11+
CONFIG_EXAMPLE_ETH_MDC_GPIO=23
12+
CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
13+
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
14+
CONFIG_EXAMPLE_ETH_PHY_ADDR=1
15+
CONFIG_MDNS_BUTTON_GPIO=32

0 commit comments

Comments
 (0)