Skip to content

Commit fb8a2f0

Browse files
committed
fix(mdns): Host test with IDFv5.1
1 parent b87bef5 commit fb8a2f0

File tree

34 files changed

+187
-172
lines changed

34 files changed

+187
-172
lines changed

.github/workflows/host-test.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: Host test
33
on: [push, pull_request]
44

55
jobs:
6-
host_test:
7-
name: Build and Test on Host
6+
host_test_esp_modem:
7+
name: esp-modem Build and Test on Host
88
runs-on: ubuntu-20.04
99
container: espressif/idf:release-v4.3
1010
env:
@@ -38,3 +38,23 @@ jobs:
3838
if: always()
3939
with:
4040
files: esp-protocols/components/esp_modem/test/host_test/junit.xml
41+
42+
host_test_mdns:
43+
name: mdns Build and Test on Host
44+
runs-on: ubuntu-20.04
45+
container: espressif/idf:latest
46+
47+
steps:
48+
- name: Checkout esp-protocols
49+
uses: actions/checkout@master
50+
with:
51+
path: esp-protocols
52+
53+
- name: Build and Test
54+
shell: bash
55+
run: |
56+
apt-get update && apt-get install -y dnsutils gcc g++
57+
. ${IDF_PATH}/export.sh
58+
cd $GITHUB_WORKSPACE/esp-protocols/components/mdns/tests/host_test
59+
idf.py build
60+
./build/mdns_host.elf & dig +short -p 5353 @224.0.0.251 myesp.local

components/mdns/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ endif()
66

77
idf_build_get_property(target IDF_TARGET)
88
if(${target} STREQUAL "linux")
9-
set(dependencies esp_system_protocols_linux)
9+
set(dependencies esp_event esp_netif_linux esp_timer_linux esp_system)
1010
set(srcs "mdns.c" ${MDNS_NETWORKING})
1111
else()
1212
set(dependencies lwip console esp_netif)
@@ -22,6 +22,11 @@ idf_component_register(
2222
PRIV_REQUIRES ${private_dependencies})
2323
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
2424

25+
if(${target} STREQUAL "linux")
26+
target_link_libraries(${COMPONENT_LIB} PRIVATE "-lbsd")
27+
endif()
28+
29+
2530
if(CONFIG_ETH_ENABLED)
2631
idf_component_optional_requires(PRIVATE esp_eth)
2732
endif()

components/mdns/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ menu "mDNS"
8181
This option creates a new thread to serve receiving packets (TODO).
8282
This option uses additional N sockets, where N is number of interfaces.
8383

84+
config MDNS_SKIP_SUPPRESSING_OWN_QUERIES
85+
bool "Skip suppressing our own packets"
86+
default n
87+
help
88+
Enable only if the querier and the responder share the same IP address.
89+
This usually happens in test mode, where we may run multiple instances of
90+
responders/queriers on the same interface.
91+
92+
config MDNS_ENABLE_DEBUG_PRINTS
93+
bool "Enable debug prints of mDNS packets"
94+
default n
95+
help
96+
Enable for the library to log received and sent mDNS packets to stdout.
97+
8498
config MDNS_MULTIPLE_INSTANCE
8599
bool "Multiple instances under the same service type"
86100
default y

components/mdns/mdns.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
#include "mdns_networking.h"
1818
#include "esp_log.h"
1919
#include "esp_random.h"
20-
#if CONFIG_ETH_ENABLED
20+
#if __has_include("bsd/string.h")
21+
#include "bsd/string.h"
22+
#endif
23+
#if CONFIG_ETH_ENABLED && CONFIG_MDNS_PREDEF_NETIF_ETH
2124
#include "esp_eth.h"
2225
#endif
26+
#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP
2327
#include "esp_wifi.h"
28+
#endif
2429

2530

2631
#ifdef MDNS_ENABLE_DEBUG
@@ -1102,10 +1107,11 @@ static uint16_t _mdns_append_question(uint8_t *packet, uint16_t *index, mdns_out
11021107
const char *str[6];
11031108
uint8_t str_index = 0;
11041109
uint8_t part_length;
1110+
char *host_dup = NULL; // need to duplicate host-name for some cases
11051111
if (q->host && strstr(q->host, "in-addr")) {
1106-
char * host = strdup(q->host);
1112+
host_dup = strdup(q->host);
11071113
char *rest = NULL;
1108-
for (char *p = strtok_r(host,".", &rest); p != NULL; p = strtok_r(NULL, ".", &rest)) {
1114+
for (char *p = strtok_r(host_dup, ".", &rest); p != NULL; p = strtok_r(NULL, ".", &rest)) {
11091115
str[str_index++] = p;
11101116
}
11111117
if (q->domain) {
@@ -1131,11 +1137,13 @@ static uint16_t _mdns_append_question(uint8_t *packet, uint16_t *index, mdns_out
11311137

11321138
part_length = _mdns_append_fqdn(packet, index, str, str_index, MDNS_MAX_PACKET_SIZE);
11331139
if (!part_length) {
1140+
free(host_dup);
11341141
return 0;
11351142
}
11361143

11371144
part_length += _mdns_append_u16(packet, index, q->type);
11381145
part_length += _mdns_append_u16(packet, index, q->unicast ? 0x8001 : 0x0001);
1146+
free(host_dup);
11391147
return part_length;
11401148
}
11411149

@@ -1213,17 +1221,17 @@ static uint8_t _mdns_append_host_answer(uint8_t *packet, uint16_t *index, mdns_h
12131221
return num_records;
12141222
}
12151223

1216-
static uint16_t _mdns_append_rev_ptr_record(uint8_t * packet, uint16_t * index, const char* name, bool flush, bool bye)
1224+
static uint16_t _mdns_append_rev_ptr_record(uint8_t *packet, uint16_t *index, const char *name, bool flush, bool bye)
12171225
{
1218-
const char * str[6];
1226+
const char *str[6];
12191227
int i = 0;
12201228

12211229
if (strstr(name, "in-addr") == NULL) {
12221230
return 0;
12231231
}
1224-
char * host = strdup(name);
1232+
char *host = strdup(name);
12251233
char *rest = NULL;
1226-
for (char *p = strtok_r(host,".", &rest); p != NULL; p = strtok_r(NULL, ".", &rest)) {
1234+
for (char *p = strtok_r(host, ".", &rest); p != NULL; p = strtok_r(NULL, ".", &rest)) {
12271235
str[i++] = p;
12281236
}
12291237
str[i++] = "arpa";
@@ -1236,7 +1244,7 @@ static uint16_t _mdns_append_rev_ptr_record(uint8_t * packet, uint16_t * index,
12361244
}
12371245
record_length += part_length;
12381246

1239-
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye?0:MDNS_ANSWER_PTR_TTL);
1247+
part_length = _mdns_append_type(packet, index, MDNS_ANSWER_PTR, false, bye ? 0 : MDNS_ANSWER_PTR_TTL);
12401248
if (!part_length) {
12411249
return 0;
12421250
}
@@ -1258,7 +1266,7 @@ static uint16_t _mdns_append_rev_ptr_record(uint8_t * packet, uint16_t * index,
12581266
}
12591267

12601268

1261-
static uint8_t _mdns_append_reverse_ptr_record(uint8_t *packet, uint16_t *index, const char* name, bool flush, bool bye)
1269+
static uint8_t _mdns_append_reverse_ptr_record(uint8_t *packet, uint16_t *index, const char *name, bool flush, bool bye)
12621270
{
12631271
uint8_t appended_answers = 0;
12641272

@@ -1711,9 +1719,9 @@ static bool _mdns_create_answer_from_service(mdns_tx_packet_t *packet, mdns_serv
17111719
return true;
17121720
}
17131721

1714-
static bool _mdns_create_answer_from_reverse_query(mdns_tx_packet_t * packet, const char * hostname, bool send_flush)
1722+
static bool _mdns_create_answer_from_reverse_query(mdns_tx_packet_t *packet, const char *hostname, bool send_flush)
17151723
{
1716-
mdns_host_item_t * host = mdns_get_host_item(hostname);
1724+
mdns_host_item_t *host = mdns_get_host_item(hostname);
17171725
if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, NULL, host, send_flush, false)) {
17181726
return false;
17191727
}
@@ -3394,6 +3402,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
33943402
mdns_debug_packet(data, len);
33953403
#endif
33963404

3405+
#ifndef CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES
33973406
// Check if the packet wasn't sent by us
33983407
if (packet->ip_protocol == MDNS_IP_PROTOCOL_V4) {
33993408
esp_netif_ip_info_t if_ip_info;
@@ -3410,6 +3419,7 @@ void mdns_parse_packet(mdns_rx_packet_t *packet)
34103419
}
34113420
#endif
34123421
}
3422+
#endif
34133423

34143424
// Check for the minimum size of mdns packet
34153425
if (len <= MDNS_HEAD_ADDITIONAL_OFFSET) {
@@ -3977,6 +3987,7 @@ static inline void post_mdns_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_pr
39773987
mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6);
39783988
}
39793989

3990+
#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
39803991
void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base,
39813992
int32_t event_id, void *event_data)
39823993
{
@@ -4054,6 +4065,7 @@ void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base,
40544065
}
40554066
}
40564067
}
4068+
#endif /* CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH */
40574069

40584070
/*
40594071
* MDNS Search
@@ -6151,19 +6163,19 @@ void mdns_debug_packet(const uint8_t *data, size_t len)
61516163
_mdns_dbg_printf("Packet[%u]: ", t);
61526164

61536165
header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
6154-
header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
6166+
header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
61556167
header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
61566168
header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
61576169
header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
61586170
header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
61596171

61606172
_mdns_dbg_printf("%s",
6161-
(header.flags.value == MDNS_FLAGS_QR_AUTHORITATIVE) ? "AUTHORITATIVE\n" :
6162-
(header.flags.value == MDNS_FLAGS_DISTRIBUTED) ? "DISTRIBUTED\n" :
6163-
(header.flags.value == 0) ? "\n" : " "
6173+
(header.flags == MDNS_FLAGS_QR_AUTHORITATIVE) ? "AUTHORITATIVE\n" :
6174+
(header.flags == MDNS_FLAGS_DISTRIBUTED) ? "DISTRIBUTED\n" :
6175+
(header.flags == 0) ? "\n" : " "
61646176
);
6165-
if (header.flags.value && header.flags.value != MDNS_FLAGS_QR_AUTHORITATIVE) {
6166-
_mdns_dbg_printf("0x%04X\n", header.flags.value);
6177+
if (header.flags && header.flags != MDNS_FLAGS_QR_AUTHORITATIVE) {
6178+
_mdns_dbg_printf("0x%04X\n", header.flags);
61676179
}
61686180

61696181
if (header.questions) {

components/mdns/private_include/mdns_private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include "freertos/queue.h"
1515
#include "freertos/semphr.h"
1616
#include "esp_timer.h"
17+
#include "esp_system.h"
1718

18-
//#define MDNS_ENABLE_DEBUG
19-
20-
#ifdef MDNS_ENABLE_DEBUG
19+
#ifdef CONFIG_MDNS_ENABLE_DEBUG_PRINTS
20+
#define MDNS_ENABLE_DEBUG
2121
#define _mdns_dbg_printf(...) printf(__VA_ARGS__)
2222
#endif
2323

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cmake_minimum_required(VERSION 3.5)
22

3+
set(EXTRA_COMPONENT_DIRS "../..")
4+
35
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4-
set(COMPONENTS main)
6+
set(COMPONENTS main esp_netif_linux)
57
project(mdns_host)

components/mdns/tests/host_test/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Setup dummy network interfaces
2+
3+
Note: Set two addresses so we could use one as source and another as destination
24
```
35
sudo ip link add eth2 type dummy
46
sudo ip addr add 192.168.1.200/24 dev eth2
7+
sudo ip addr add 192.168.1.201/24 dev eth2
58
sudo ip link set eth2 up
69
sudo ifconfig eth2 multicast
710
```
@@ -12,6 +15,11 @@ sudo ifconfig eth2 multicast
1215
dig +short -b 192.168.1.200 -p 5353 @224.0.0.251 myesp.local
1316
```
1417

18+
or a reverse query:
19+
```
20+
dig +short -b 192.168.2.200 -p 5353 @224.0.0.251 -x 192.168.1.200
21+
```
22+
1523
# Run avahi to browse services
1624

1725
Avahi needs the netif to have the "multicast" flag set
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
idf_component_register(SRCS esp_event_mock.c
22
INCLUDE_DIRS include
3-
REQUIRES esp_system_protocols_linux)
3+
REQUIRES)
File renamed without changes.

components/mdns/tests/host_test/components/esp_event_mock/include/esp_event.h renamed to components/mdns/tests/host_test/components/esp_event/include/esp_event.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
#include "stdbool.h"
99
#include "esp_err.h"
1010
#include "esp_event_base.h"
11-
#include "bsd_strings.h"
11+
//#include "bsd_strings.h"
1212

13-
#define ESP_EVENT_DECLARE_BASE(x)
14-
#define ESP_EVENT_ANY_ID (-1)
1513

16-
typedef void *esp_event_base_t;
17-
typedef void *system_event_t;
1814

19-
const char *WIFI_EVENT;
20-
const char *IP_EVENT;
15+
//const char *WIFI_EVENT;
16+
//const char *IP_EVENT;
2117

2218
esp_err_t esp_event_handler_register(const char *event_base, int32_t event_id, void *event_handler, void *event_handler_arg);
2319

0 commit comments

Comments
 (0)