Skip to content

Commit f07f344

Browse files
committed
fix(mdns): Add test programs
1 parent bb4f37f commit f07f344

File tree

8 files changed

+429
-0
lines changed

8 files changed

+429
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.22)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(simple)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "simple.c" "softap_example_main.c"
2+
INCLUDE_DIRS ".")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
menu "Example Configuration"
2+
3+
config ESP_WIFI_SSID
4+
string "WiFi SSID"
5+
default "myssid"
6+
help
7+
SSID (network name) for the example to connect to.
8+
9+
config ESP_WIFI_PASSWORD
10+
string "WiFi Password"
11+
default "mypassword"
12+
help
13+
WiFi password (WPA or WPA2) for the example to use.
14+
config ESP_WIFI_CHANNEL
15+
int "WiFi Channel"
16+
range 1 13
17+
default 1
18+
help
19+
WiFi channel (network channel) for the example to use.
20+
21+
config ESP_MAX_STA_CONN
22+
int "Maximal STA connections"
23+
default 4
24+
help
25+
Max number of the STA connects to AP.
26+
27+
config ESP_GTK_REKEYING_ENABLE
28+
bool "Enable GTK Rekeying"
29+
default y
30+
help
31+
Flag to enable GTK rekeying.
32+
33+
config ESP_GTK_REKEY_INTERVAL
34+
int "GTK rekey interval"
35+
depends on ESP_GTK_REKEYING_ENABLE
36+
range 60 65535
37+
default 600
38+
help
39+
GTK rekeying interval in seconds.
40+
endmenu
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencies:
2+
## Required IDF version
3+
idf: ">=5.0"
4+
espressif/mdns:
5+
version: "^1.0.0"
6+
override_path: "../../../"
7+
protocol_examples_common:
8+
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#include <string.h>
7+
#include "esp_event.h"
8+
#include "esp_log.h"
9+
#include "esp_mac.h"
10+
#include "esp_netif.h"
11+
#include "esp_netif_ip_addr.h"
12+
#include "mdns.h"
13+
#include "nvs_flash.h"
14+
#include "protocol_examples_common.h"
15+
16+
static const char *TAG = "mdns-simple";
17+
18+
static void initialise_mdns(void)
19+
{
20+
const char *mdns_hostname = "minifritz";
21+
const char *mdns_instance = "Hristo's Time Capsule";
22+
23+
mdns_txt_item_t arduTxtData[4] = {
24+
{"board", "esp32"},
25+
{"tcp_check", "no"},
26+
{"ssh_upload", "no"},
27+
{"auth_upload", "no"}
28+
};
29+
30+
uint8_t mac[6];
31+
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_STA));
32+
33+
char *winstance = NULL;
34+
if (asprintf(&winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]",
35+
mdns_hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) {
36+
abort();
37+
}
38+
39+
ESP_ERROR_CHECK(mdns_init());
40+
ESP_ERROR_CHECK(mdns_hostname_set(mdns_hostname));
41+
ESP_LOGI(TAG, "mdns hostname set to: [%s]", mdns_hostname);
42+
ESP_ERROR_CHECK(mdns_instance_name_set(mdns_instance));
43+
44+
// Delegate host: 17.17.17.17
45+
mdns_ip_addr_t addr4 = { 0 };
46+
addr4.addr.type = ESP_IPADDR_TYPE_V4;
47+
esp_netif_str_to_ip4("17.17.17.17", &addr4.addr.u_addr.ip4);
48+
addr4.next = NULL;
49+
ESP_ERROR_CHECK(mdns_delegate_hostname_add(mdns_hostname, &addr4));
50+
ESP_ERROR_CHECK(mdns_delegate_hostname_add("megafritz", &addr4));
51+
52+
// Services and subtypes mirroring the fuzz test setup
53+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_fritz", "_tcp", 22, NULL, 0));
54+
ESP_ERROR_CHECK(mdns_service_subtype_add_for_host(NULL, "_fritz", "_tcp", NULL, "_server"));
55+
56+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_telnet", "_tcp", 22, NULL, 0));
57+
58+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_workstation", "_tcp", 9, NULL, 0));
59+
ESP_ERROR_CHECK(mdns_service_instance_name_set("_workstation", "_tcp", winstance));
60+
61+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_arduino", "_tcp", 3232, NULL, 0));
62+
ESP_ERROR_CHECK(mdns_service_txt_set("_arduino", "_tcp", arduTxtData, 4));
63+
64+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0));
65+
ESP_ERROR_CHECK(mdns_service_instance_name_set("_http", "_tcp", "ESP WebServer"));
66+
67+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_afpovertcp", "_tcp", 548, NULL, 0));
68+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_rfb", "_tcp", 885, NULL, 0));
69+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_smb", "_tcp", 885, NULL, 0));
70+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_adisk", "_tcp", 885, NULL, 0));
71+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_airport", "_tcp", 885, NULL, 0));
72+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_printer", "_tcp", 885, NULL, 0));
73+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_airplay", "_tcp", 885, NULL, 0));
74+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_raop", "_tcp", 885, NULL, 0));
75+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_uscan", "_tcp", 885, NULL, 0));
76+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_uscans", "_tcp", 885, NULL, 0));
77+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_ippusb", "_tcp", 885, NULL, 0));
78+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_scanner", "_tcp", 885, NULL, 0));
79+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_ipp", "_tcp", 885, NULL, 0));
80+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_ipps", "_tcp", 885, NULL, 0));
81+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_pdl-datastream", "_tcp", 885, NULL, 0));
82+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_ptp", "_tcp", 885, NULL, 0));
83+
ESP_ERROR_CHECK(mdns_service_add(NULL, "_sleep-proxy", "_udp", 885, NULL, 0));
84+
85+
free(winstance);
86+
}
87+
88+
void init_softap(void);
89+
90+
/* these strings match mdns_ip_protocol_t enumeration */
91+
static const char *ip_protocol_str[] = {"V4", "V6", "MAX"};
92+
93+
static void mdns_print_results(mdns_result_t *results)
94+
{
95+
mdns_result_t *r = results;
96+
mdns_ip_addr_t *a = NULL;
97+
int i = 1, t;
98+
while (r) {
99+
if (r->esp_netif) {
100+
printf("%d: Interface: %s, Type: %s, TTL: %" PRIu32 "\n", i++, esp_netif_get_ifkey(r->esp_netif),
101+
ip_protocol_str[r->ip_protocol], r->ttl);
102+
}
103+
if (r->instance_name) {
104+
printf(" PTR : %s.%s.%s\n", r->instance_name, r->service_type, r->proto);
105+
}
106+
if (r->hostname) {
107+
printf(" SRV : %s.local:%u\n", r->hostname, r->port);
108+
}
109+
if (r->txt_count) {
110+
printf(" TXT : [%zu] ", r->txt_count);
111+
for (t = 0; t < r->txt_count; t++) {
112+
printf("%s=%s(%d); ", r->txt[t].key, r->txt[t].value ? r->txt[t].value : "NULL", r->txt_value_len[t]);
113+
}
114+
printf("\n");
115+
}
116+
a = r->addr;
117+
while (a) {
118+
if (a->addr.type == ESP_IPADDR_TYPE_V6) {
119+
printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
120+
} else {
121+
printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
122+
}
123+
a = a->next;
124+
}
125+
r = r->next;
126+
}
127+
}
128+
129+
130+
static void query_mdns_service(const char *service_name, const char *proto)
131+
{
132+
ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
133+
134+
mdns_result_t *results = NULL;
135+
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
136+
if (err) {
137+
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
138+
return;
139+
}
140+
if (!results) {
141+
ESP_LOGW(TAG, "No results found!");
142+
return;
143+
}
144+
145+
mdns_print_results(results);
146+
mdns_query_results_free(results);
147+
}
148+
149+
150+
void app_main(void)
151+
{
152+
// ESP_ERROR_CHECK(nvs_flash_init());
153+
// ESP_ERROR_CHECK(esp_netif_init());
154+
// ESP_ERROR_CHECK(esp_event_loop_create_default());
155+
156+
ESP_LOGI(TAG, "mDNS Ver: %s", ESP_MDNS_VERSION_NUMBER);
157+
158+
159+
// Use the common connection helper to bring up Wi‑Fi/Ethernet
160+
init_softap();
161+
initialise_mdns();
162+
while (1) {
163+
query_mdns_service("_fritz", "_tcp");
164+
165+
}
166+
// ESP_ERROR_CHECK(example_connect());
167+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* WiFi softAP Example
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
#include <string.h>
10+
#include "freertos/FreeRTOS.h"
11+
#include "freertos/task.h"
12+
#include "esp_mac.h"
13+
#include "esp_wifi.h"
14+
#include "esp_event.h"
15+
#include "esp_log.h"
16+
#include "nvs_flash.h"
17+
18+
#include "lwip/err.h"
19+
#include "lwip/sys.h"
20+
21+
/* The examples use WiFi configuration that you can set via project configuration menu.
22+
23+
If you'd rather not, just change the below entries to strings with
24+
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
25+
*/
26+
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
27+
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
28+
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
29+
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
30+
31+
#if CONFIG_ESP_GTK_REKEYING_ENABLE
32+
#define EXAMPLE_GTK_REKEY_INTERVAL CONFIG_ESP_GTK_REKEY_INTERVAL
33+
#else
34+
#define EXAMPLE_GTK_REKEY_INTERVAL 0
35+
#endif
36+
37+
static const char *TAG = "wifi softAP";
38+
39+
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
40+
int32_t event_id, void* event_data)
41+
{
42+
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
43+
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
44+
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
45+
MAC2STR(event->mac), event->aid);
46+
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
47+
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
48+
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d, reason=%d",
49+
MAC2STR(event->mac), event->aid, event->reason);
50+
}
51+
}
52+
53+
void wifi_init_softap(void)
54+
{
55+
ESP_ERROR_CHECK(esp_netif_init());
56+
ESP_ERROR_CHECK(esp_event_loop_create_default());
57+
esp_netif_create_default_wifi_ap();
58+
59+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
60+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
61+
62+
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
63+
ESP_EVENT_ANY_ID,
64+
&wifi_event_handler,
65+
NULL,
66+
NULL));
67+
68+
wifi_config_t wifi_config = {
69+
.ap = {
70+
.ssid = EXAMPLE_ESP_WIFI_SSID,
71+
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
72+
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
73+
.password = EXAMPLE_ESP_WIFI_PASS,
74+
.max_connection = EXAMPLE_MAX_STA_CONN,
75+
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
76+
.authmode = WIFI_AUTH_WPA3_PSK,
77+
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
78+
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
79+
.authmode = WIFI_AUTH_WPA2_PSK,
80+
#endif
81+
.pmf_cfg = {
82+
.required = true,
83+
},
84+
#ifdef CONFIG_ESP_WIFI_BSS_MAX_IDLE_SUPPORT
85+
.bss_max_idle_cfg = {
86+
.period = WIFI_AP_DEFAULT_MAX_IDLE_PERIOD,
87+
.protected_keep_alive = 1,
88+
},
89+
#endif
90+
.gtk_rekey_interval = EXAMPLE_GTK_REKEY_INTERVAL,
91+
},
92+
};
93+
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
94+
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
95+
}
96+
97+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
98+
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
99+
ESP_ERROR_CHECK(esp_wifi_start());
100+
101+
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
102+
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
103+
}
104+
105+
void init_softap(void)
106+
{
107+
//Initialize NVS
108+
esp_err_t ret = nvs_flash_init();
109+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
110+
ESP_ERROR_CHECK(nvs_flash_erase());
111+
ret = nvs_flash_init();
112+
}
113+
ESP_ERROR_CHECK(ret);
114+
115+
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
116+
wifi_init_softap();
117+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file was generated using idf.py save-defconfig. It can be edited manually.
2+
# Espressif IoT Development Framework (ESP-IDF) 6.0.0 Project Minimal Configuration
3+
#
4+
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
5+
CONFIG_MDNS_MAX_SERVICES=40
6+
CONFIG_MDNS_ENABLE_DEBUG_PRINTS=y

0 commit comments

Comments
 (0)