| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:13:11 +01:00
										 |  |  |  * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |  * SPDX-License-Identifier: Unlicense OR CC0-1.0 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |  * MDNS-SD Query and advertise Example | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include "freertos/FreeRTOS.h"
 | 
					
						
							|  |  |  | #include "freertos/task.h"
 | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  | #include "esp_netif_ip_addr.h"
 | 
					
						
							| 
									
										
										
										
											2022-01-12 12:23:47 +05:30
										 |  |  | #include "esp_mac.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  | #include "esp_event.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | #include "esp_log.h"
 | 
					
						
							|  |  |  | #include "nvs_flash.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-31 19:06:54 +02:00
										 |  |  | #include "esp_netif.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  | #include "protocol_examples_common.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | #include "mdns.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | #include "driver/gpio.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-31 19:06:54 +02:00
										 |  |  | #include "netdb.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
 | 
					
						
							|  |  |  | /* CONFIG_LWIP_IPV4 was introduced in IDF v5.1, set CONFIG_LWIP_IPV4 to 1 by default for IDF v5.0 */ | 
					
						
							|  |  |  | #ifndef CONFIG_LWIP_IPV4
 | 
					
						
							|  |  |  | #define CONFIG_LWIP_IPV4 1
 | 
					
						
							|  |  |  | #endif // CONFIG_LWIP_IPV4
 | 
					
						
							|  |  |  | #endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
 | 
					
						
							| 
									
										
										
										
											2022-02-04 18:40:21 +01:00
										 |  |  | #define EXAMPLE_BUTTON_GPIO   CONFIG_MDNS_BUTTON_GPIO
 | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static const char *TAG = "mdns-test"; | 
					
						
							|  |  |  | static char *generate_hostname(void); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static void  query_mdns_host_with_gethostbyname(char *host); | 
					
						
							|  |  |  | static void  query_mdns_host_with_getaddrinfo(char *host); | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | static void initialise_mdns(void) | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     char *hostname = generate_hostname(); | 
					
						
							| 
									
										
										
										
											2021-04-15 20:03:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     //initialize mDNS
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_init() ); | 
					
						
							|  |  |  |     //set mDNS hostname (required if you want to advertise services)
 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:45:52 +02:00
										 |  |  |     ESP_ERROR_CHECK( mdns_hostname_set(hostname) ); | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     //set default mDNS instance name
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_instance_name_set(EXAMPLE_MDNS_INSTANCE) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //structure with TXT records
 | 
					
						
							|  |  |  |     mdns_txt_item_t serviceTxtData[3] = { | 
					
						
							| 
									
										
										
										
											2021-03-30 13:19:01 +08:00
										 |  |  |         {"board", "esp32"}, | 
					
						
							|  |  |  |         {"u", "user"}, | 
					
						
							|  |  |  |         {"p", "password"} | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //initialize service
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) ); | 
					
						
							| 
									
										
										
										
											2021-09-30 15:18:16 +08:00
										 |  |  |     ESP_ERROR_CHECK( mdns_service_subtype_add_for_host("ESP32-WebServer", "_http", "_tcp", NULL, "_server") ); | 
					
						
							| 
									
										
										
										
											2021-09-13 19:33:02 +08:00
										 |  |  | #if CONFIG_MDNS_MULTIPLE_INSTANCE
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer1", "_http", "_tcp", 80, NULL, 0) ); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-15 20:03:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     char *delegated_hostname; | 
					
						
							|  |  |  |     if (-1 == asprintf(&delegated_hostname, "%s-delegated", hostname)) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     mdns_ip_addr_t addr4, addr6; | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     esp_netif_str_to_ip4("10.0.0.1", &addr4.addr.u_addr.ip4); | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     addr4.addr.type = ESP_IPADDR_TYPE_V4; | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     esp_netif_str_to_ip6("fd11:22::1", &addr6.addr.u_addr.ip6); | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     addr6.addr.type = ESP_IPADDR_TYPE_V6; | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     addr4.next = &addr6; | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     addr6.next = NULL; | 
					
						
							| 
									
										
										
										
											2021-04-15 20:03:53 +08:00
										 |  |  |     ESP_ERROR_CHECK( mdns_delegate_hostname_add(delegated_hostname, &addr4) ); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_service_add_for_host("test0", "_http", "_tcp", delegated_hostname, 1234, serviceTxtData, 3) ); | 
					
						
							| 
									
										
										
										
											2021-05-11 13:47:03 +08:00
										 |  |  |     free(delegated_hostname); | 
					
						
							| 
									
										
										
										
											2021-04-15 20:03:53 +08:00
										 |  |  | #endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     //add another TXT item
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") ); | 
					
						
							|  |  |  |     //change TXT item value
 | 
					
						
							| 
									
										
										
										
											2021-06-15 20:13:00 +08:00
										 |  |  |     ESP_ERROR_CHECK( mdns_service_txt_item_set_with_explicit_value_len("_http", "_tcp", "u", "admin", strlen("admin")) ); | 
					
						
							| 
									
										
										
										
											2019-01-25 20:24:30 +01:00
										 |  |  |     free(hostname); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  | /* these strings match mdns_ip_protocol_t enumeration */ | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static const char *ip_protocol_str[] = {"V4", "V6", "MAX"}; | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  | static void mdns_print_results(mdns_result_t *results) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     mdns_result_t *r = results; | 
					
						
							|  |  |  |     mdns_ip_addr_t *a = NULL; | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     int i = 1, t; | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |     while (r) { | 
					
						
							| 
									
										
										
										
											2023-04-10 13:16:29 +08:00
										 |  |  |         if (r->esp_netif) { | 
					
						
							| 
									
										
										
										
											2023-07-20 13:48:50 +04:00
										 |  |  |             printf("%d: Interface: %s, Type: %s, TTL: %" PRIu32 "\n", i++, esp_netif_get_ifkey(r->esp_netif), | 
					
						
							| 
									
										
										
										
											2023-04-10 13:16:29 +08:00
										 |  |  |                    ip_protocol_str[r->ip_protocol], r->ttl); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |         if (r->instance_name) { | 
					
						
							|  |  |  |             printf("  PTR : %s.%s.%s\n", r->instance_name, r->service_type, r->proto); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |         if (r->hostname) { | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |             printf("  SRV : %s.local:%u\n", r->hostname, r->port); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |         if (r->txt_count) { | 
					
						
							| 
									
										
										
										
											2021-06-15 20:13:00 +08:00
										 |  |  |             printf("  TXT : [%zu] ", r->txt_count); | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |             for (t = 0; t < r->txt_count; t++) { | 
					
						
							|  |  |  |                 printf("%s=%s(%d); ", r->txt[t].key, r->txt[t].value ? r->txt[t].value : "NULL", r->txt_value_len[t]); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |             printf("\n"); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         a = r->addr; | 
					
						
							| 
									
										
										
										
											2021-04-21 15:34:22 +08:00
										 |  |  |         while (a) { | 
					
						
							|  |  |  |             if (a->addr.type == ESP_IPADDR_TYPE_V6) { | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |                 printf("  AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6)); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 printf("  A   : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4))); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |             a = a->next; | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         r = r->next; | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static void query_mdns_service(const char *service_name, const char *proto) | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     mdns_result_t *results = NULL; | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20,  &results); | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     if (err) { | 
					
						
							| 
									
										
										
										
											2018-02-22 14:48:53 +01:00
										 |  |  |         ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err)); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     if (!results) { | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         ESP_LOGW(TAG, "No results found!"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     mdns_print_results(results); | 
					
						
							|  |  |  |     mdns_query_results_free(results); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 13:16:29 +08:00
										 |  |  | #if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							|  |  |  | static void lookup_mdns_delegated_service(const char *service_name, const char *proto) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Lookup delegated service: %s.%s.local", service_name, proto); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mdns_result_t *results = NULL; | 
					
						
							|  |  |  |     esp_err_t err = mdns_lookup_delegated_service(NULL, service_name, proto, 20, &results); | 
					
						
							|  |  |  |     if (err) { | 
					
						
							|  |  |  |         ESP_LOGE(TAG, "Lookup Failed: %s", esp_err_to_name(err)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!results) { | 
					
						
							|  |  |  |         ESP_LOGW(TAG, "No results found!"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mdns_print_results(results); | 
					
						
							|  |  |  |     mdns_query_results_free(results); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 12:22:21 +08:00
										 |  |  | static void lookup_mdns_selfhosted_service(const char *service_name, const char *proto) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Lookup selfhosted service: %s.%s.local", service_name, proto); | 
					
						
							|  |  |  |     mdns_result_t *results = NULL; | 
					
						
							|  |  |  |     esp_err_t err = mdns_lookup_selfhosted_service(NULL, service_name, proto, 20, &results); | 
					
						
							|  |  |  |     if (err) { | 
					
						
							|  |  |  |         ESP_LOGE(TAG, "Lookup Failed: %s", esp_err_to_name(err)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!results) { | 
					
						
							|  |  |  |         ESP_LOGW(TAG, "No results found!"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mdns_print_results(results); | 
					
						
							|  |  |  |     mdns_query_results_free(results); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 16:56:12 +02:00
										 |  |  | static bool check_and_print_result(mdns_search_once_t *search) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Check if any result is available
 | 
					
						
							|  |  |  |     mdns_result_t *result = NULL; | 
					
						
							| 
									
										
										
										
											2021-12-10 10:30:13 +01:00
										 |  |  |     if (!mdns_query_async_get_results(search, 0, &result, NULL)) { | 
					
						
							| 
									
										
										
										
											2021-06-03 16:56:12 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!result) {   // search timeout, but no result
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If yes, print the result
 | 
					
						
							|  |  |  |     mdns_ip_addr_t *a = result->addr; | 
					
						
							|  |  |  |     while (a) { | 
					
						
							|  |  |  |         if (a->addr.type == ESP_IPADDR_TYPE_V6) { | 
					
						
							|  |  |  |             printf("  AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6)); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             printf("  A   : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4))); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         a = a->next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // and free the result
 | 
					
						
							|  |  |  |     mdns_query_results_free(result); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void query_mdns_hosts_async(const char *host_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-05 16:59:04 +08:00
										 |  |  |     mdns_search_once_t *s_a = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL); | 
					
						
							|  |  |  |     mdns_search_once_t *s_aaaa = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_AAAA, 1000, 1, NULL); | 
					
						
							| 
									
										
										
										
											2021-06-03 16:56:12 +02:00
										 |  |  |     while (s_a || s_aaaa) { | 
					
						
							|  |  |  |         if (s_a && check_and_print_result(s_a)) { | 
					
						
							|  |  |  |             ESP_LOGI(TAG, "Query A %s.local finished", host_name); | 
					
						
							|  |  |  |             mdns_query_async_delete(s_a); | 
					
						
							|  |  |  |             s_a = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (s_aaaa && check_and_print_result(s_aaaa)) { | 
					
						
							|  |  |  |             ESP_LOGI(TAG, "Query AAAA %s.local finished", host_name); | 
					
						
							|  |  |  |             mdns_query_async_delete(s_aaaa); | 
					
						
							|  |  |  |             s_aaaa = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-04 18:40:21 +01:00
										 |  |  |         vTaskDelay(50 / portTICK_PERIOD_MS); | 
					
						
							| 
									
										
										
										
											2021-06-03 16:56:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #ifdef CONFIG_LWIP_IPV4
 | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static void query_mdns_host(const char *host_name) | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Query A: %s.local", host_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 19:06:54 +02:00
										 |  |  |     struct esp_ip4_addr addr; | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     addr.addr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esp_err_t err = mdns_query_a(host_name, 2000,  &addr); | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     if (err) { | 
					
						
							|  |  |  |         if (err == ESP_ERR_NOT_FOUND) { | 
					
						
							| 
									
										
										
										
											2018-02-22 14:48:53 +01:00
										 |  |  |             ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err)); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-22 14:48:53 +01:00
										 |  |  |         ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err)); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:45:52 +02:00
										 |  |  |     ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr)); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #endif // CONFIG_LWIP_IPV4
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void initialise_button(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  |     gpio_config_t io_conf = {0}; | 
					
						
							| 
									
										
										
										
											2020-06-19 12:00:58 +08:00
										 |  |  |     io_conf.intr_type = GPIO_INTR_DISABLE; | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  |     io_conf.pin_bit_mask = BIT64(EXAMPLE_BUTTON_GPIO); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     io_conf.mode = GPIO_MODE_INPUT; | 
					
						
							|  |  |  |     io_conf.pull_up_en = 1; | 
					
						
							|  |  |  |     io_conf.pull_down_en = 0; | 
					
						
							|  |  |  |     gpio_config(&io_conf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void check_button(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static bool old_level = true; | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  |     bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     if (!new_level && old_level) { | 
					
						
							| 
									
										
										
										
											2021-06-03 16:56:12 +02:00
										 |  |  |         query_mdns_hosts_async("esp32-mdns"); | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #ifdef CONFIG_LWIP_IPV4
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         query_mdns_host("esp32"); | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         query_mdns_service("_arduino", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_http", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_printer", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_ipp", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_afpovertcp", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_smb", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_ftp", "_tcp"); | 
					
						
							|  |  |  |         query_mdns_service("_nfs", "_tcp"); | 
					
						
							| 
									
										
										
										
											2023-04-10 13:16:29 +08:00
										 |  |  | #if CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							|  |  |  |         lookup_mdns_delegated_service("_http", "_tcp"); | 
					
						
							|  |  |  | #endif // CONFIG_MDNS_PUBLISH_DELEGATE_HOST
 | 
					
						
							| 
									
										
										
										
											2023-05-18 12:22:21 +08:00
										 |  |  |         lookup_mdns_selfhosted_service("_http", "_tcp"); | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     old_level = new_level; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void mdns_example_task(void *pvParameters) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-09 16:43:06 +02:00
										 |  |  | #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:45:52 +02:00
										 |  |  |     /* Send initial queries that are started by CI tester */ | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #ifdef CONFIG_LWIP_IPV4
 | 
					
						
							| 
									
										
										
										
											2018-10-12 16:45:52 +02:00
										 |  |  |     query_mdns_host("tinytester"); | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  |     query_mdns_host_with_gethostbyname("tinytester-lwip.local"); | 
					
						
							|  |  |  |     query_mdns_host_with_getaddrinfo("tinytester-lwip.local"); | 
					
						
							| 
									
										
										
										
											2018-10-12 16:45:52 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-30 13:19:01 +08:00
										 |  |  |     while (1) { | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |         check_button(); | 
					
						
							|  |  |  |         vTaskDelay(50 / portTICK_PERIOD_MS); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void app_main(void) | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  |     ESP_ERROR_CHECK(nvs_flash_init()); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:19:21 +02:00
										 |  |  |     ESP_ERROR_CHECK(esp_netif_init()); | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_loop_create_default()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-24 11:28:27 -08:00
										 |  |  |     ESP_LOGI(TAG, "mDNS Ver: %s", ESP_MDNS_VERSION_NUMBER); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 13:46:45 +08:00
										 |  |  |     initialise_mdns(); | 
					
						
							| 
									
										
										
										
											2018-11-21 13:52:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
 | 
					
						
							|  |  |  |      * Read "Establishing Wi-Fi or Ethernet Connection" section in | 
					
						
							|  |  |  |      * examples/protocols/README.md for more information about this function. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(example_connect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:07:23 +01:00
										 |  |  | #if defined(CONFIG_MDNS_ADD_CUSTOM_NETIF) && !defined(CONFIG_MDNS_PREDEF_NETIF_STA) && !defined(CONFIG_MDNS_PREDEF_NETIF_ETH)
 | 
					
						
							|  |  |  |     /* Demonstration of adding a custom netif to mdns service, but we're adding the default example one,
 | 
					
						
							|  |  |  |      * so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-01-03 16:36:08 +01:00
										 |  |  |     ESP_ERROR_CHECK(mdns_register_netif(EXAMPLE_INTERFACE)); | 
					
						
							|  |  |  |     /* It is not enough to just register the interface, we have to enable is manually.
 | 
					
						
							|  |  |  |      * This is typically performed in "GOT_IP" event handler, but we call it here directly | 
					
						
							|  |  |  |      * since the `EXAMPLE_INTERFACE` netif is connected already, to keep the example simple. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-02-08 20:20:06 +01:00
										 |  |  |     ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4 | MDNS_EVENT_ENABLE_IP6)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4 | MDNS_EVENT_ANNOUNCE_IP6)); | 
					
						
							| 
									
										
										
										
											2023-01-16 23:13:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
 | 
					
						
							| 
									
										
										
										
											2023-02-08 20:20:06 +01:00
										 |  |  |     ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_IP4_REVERSE_LOOKUP | MDNS_EVENT_IP6_REVERSE_LOOKUP)); | 
					
						
							| 
									
										
										
										
											2023-01-16 23:13:11 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 20:20:06 +01:00
										 |  |  | #endif // CONFIG_MDNS_ADD_CUSTOM_NETIF
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 14:21:40 +01:00
										 |  |  |     initialise_button(); | 
					
						
							| 
									
										
										
										
											2017-03-22 12:36:11 +08:00
										 |  |  |     xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); | 
					
						
							| 
									
										
										
										
											2017-01-16 23:08:35 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-25 20:24:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
 | 
					
						
							|  |  |  |  *  @return host name string allocated from the heap | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static char *generate_hostname(void) | 
					
						
							| 
									
										
										
										
											2019-01-25 20:24:30 +01:00
										 |  |  | { | 
					
						
							|  |  |  | #ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
 | 
					
						
							|  |  |  |     return strdup(CONFIG_MDNS_HOSTNAME); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     uint8_t mac[6]; | 
					
						
							|  |  |  |     char   *hostname; | 
					
						
							|  |  |  |     esp_read_mac(mac, ESP_MAC_WIFI_STA); | 
					
						
							|  |  |  |     if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) { | 
					
						
							|  |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return hostname; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  @brief Executes gethostbyname and displays list of resolved addresses. | 
					
						
							|  |  |  |  *  Note: This function is used only to test advertised mdns hostnames resolution | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static void  query_mdns_host_with_gethostbyname(char *host) | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     struct hostent *res = gethostbyname(host); | 
					
						
							|  |  |  |     if (res) { | 
					
						
							|  |  |  |         unsigned int i = 0; | 
					
						
							|  |  |  |         while (res->h_addr_list[i] != NULL) { | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  |             ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, | 
					
						
							|  |  |  | #if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4)
 | 
					
						
							|  |  |  |                      res->h_addrtype == AF_INET ? inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) : | 
					
						
							|  |  |  |                      inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) | 
					
						
							|  |  |  | #elif defined(CONFIG_LWIP_IPV6)
 | 
					
						
							|  |  |  |                      inet6_ntoa(*(struct in6_addr *) (res->h_addr_list[i])) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |                      inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])) | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  |             i++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  @brief Executes getaddrinfo and displays list of resolved addresses. | 
					
						
							|  |  |  |  *  Note: This function is used only to test advertised mdns hostnames resolution | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  | static void  query_mdns_host_with_getaddrinfo(char *host) | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     struct addrinfo hints; | 
					
						
							| 
									
										
										
										
											2021-04-07 16:21:16 +08:00
										 |  |  |     struct addrinfo *res; | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     memset(&hints, 0, sizeof(hints)); | 
					
						
							|  |  |  |     hints.ai_family = AF_UNSPEC; | 
					
						
							|  |  |  |     hints.ai_socktype = SOCK_STREAM; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!getaddrinfo(host, NULL, &hints, &res)) { | 
					
						
							|  |  |  |         while (res) { | 
					
						
							| 
									
										
										
										
											2022-09-07 09:41:32 +05:30
										 |  |  |             char *resolved_addr; | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  | #if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_LWIP_IPV4)
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:41:32 +05:30
										 |  |  |             resolved_addr = res->ai_family == AF_INET ? | 
					
						
							|  |  |  |                             inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr) : | 
					
						
							| 
									
										
										
										
											2023-11-03 17:45:57 +08:00
										 |  |  |                             inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); | 
					
						
							|  |  |  | #elif defined(CONFIG_LWIP_IPV6)
 | 
					
						
							|  |  |  |             resolved_addr = inet6_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); | 
					
						
							| 
									
										
										
										
											2022-09-07 09:41:32 +05:30
										 |  |  | #else
 | 
					
						
							|  |  |  |             resolved_addr = inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr); | 
					
						
							|  |  |  | #endif // CONFIG_LWIP_IPV6
 | 
					
						
							|  |  |  |             ESP_LOGI(TAG, "getaddrinfo: %s resolved to: %s", host, resolved_addr); | 
					
						
							| 
									
										
										
										
											2019-06-19 13:20:58 +02:00
										 |  |  |             res = res->ai_next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | #endif
 |