|
| 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 | +} |
0 commit comments