mdns_example_main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* MDNS-SD Query and advertise Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_system.h"
  11. #include "esp_event.h"
  12. #include "esp_log.h"
  13. #include "nvs_flash.h"
  14. #include "esp_netif.h"
  15. #include "protocol_examples_common.h"
  16. #include "mdns.h"
  17. #include "driver/gpio.h"
  18. #include "netdb.h"
  19. #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
  20. #define EXAMPLE_BUTTON_GPIO 0
  21. static const char *TAG = "mdns-test";
  22. static char* generate_hostname(void);
  23. #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
  24. static void query_mdns_host_with_gethostbyname(char * host);
  25. static void query_mdns_host_with_getaddrinfo(char * host);
  26. #endif
  27. static void initialise_mdns(void)
  28. {
  29. char* hostname = generate_hostname();
  30. //initialize mDNS
  31. ESP_ERROR_CHECK( mdns_init() );
  32. //set mDNS hostname (required if you want to advertise services)
  33. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  34. ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
  35. //set default mDNS instance name
  36. ESP_ERROR_CHECK( mdns_instance_name_set(EXAMPLE_MDNS_INSTANCE) );
  37. //structure with TXT records
  38. mdns_txt_item_t serviceTxtData[3] = {
  39. {"board","esp32"},
  40. {"u","user"},
  41. {"p","password"}
  42. };
  43. //initialize service
  44. ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
  45. //add another TXT item
  46. ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") );
  47. //change TXT item value
  48. ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "u", "admin") );
  49. free(hostname);
  50. }
  51. /* these strings match tcpip_adapter_if_t enumeration */
  52. static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
  53. /* these strings match mdns_ip_protocol_t enumeration */
  54. static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
  55. static void mdns_print_results(mdns_result_t * results){
  56. mdns_result_t * r = results;
  57. mdns_ip_addr_t * a = NULL;
  58. int i = 1, t;
  59. while(r){
  60. printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
  61. if(r->instance_name){
  62. printf(" PTR : %s\n", r->instance_name);
  63. }
  64. if(r->hostname){
  65. printf(" SRV : %s.local:%u\n", r->hostname, r->port);
  66. }
  67. if(r->txt_count){
  68. printf(" TXT : [%u] ", r->txt_count);
  69. for(t=0; t<r->txt_count; t++){
  70. printf("%s=%s; ", r->txt[t].key, r->txt[t].value?r->txt[t].value:"NULL");
  71. }
  72. printf("\n");
  73. }
  74. a = r->addr;
  75. while(a){
  76. if(a->addr.type == ESP_IPADDR_TYPE_V6){
  77. printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
  78. } else {
  79. printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
  80. }
  81. a = a->next;
  82. }
  83. r = r->next;
  84. }
  85. }
  86. static void query_mdns_service(const char * service_name, const char * proto)
  87. {
  88. ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
  89. mdns_result_t * results = NULL;
  90. esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
  91. if(err){
  92. ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
  93. return;
  94. }
  95. if(!results){
  96. ESP_LOGW(TAG, "No results found!");
  97. return;
  98. }
  99. mdns_print_results(results);
  100. mdns_query_results_free(results);
  101. }
  102. static void query_mdns_host(const char * host_name)
  103. {
  104. ESP_LOGI(TAG, "Query A: %s.local", host_name);
  105. struct esp_ip4_addr addr;
  106. addr.addr = 0;
  107. esp_err_t err = mdns_query_a(host_name, 2000, &addr);
  108. if(err){
  109. if(err == ESP_ERR_NOT_FOUND){
  110. ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
  111. return;
  112. }
  113. ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
  114. return;
  115. }
  116. ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
  117. }
  118. static void initialise_button(void)
  119. {
  120. gpio_config_t io_conf = {0};
  121. io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  122. io_conf.pin_bit_mask = BIT64(EXAMPLE_BUTTON_GPIO);
  123. io_conf.mode = GPIO_MODE_INPUT;
  124. io_conf.pull_up_en = 1;
  125. io_conf.pull_down_en = 0;
  126. gpio_config(&io_conf);
  127. }
  128. static void check_button(void)
  129. {
  130. static bool old_level = true;
  131. bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO);
  132. if (!new_level && old_level) {
  133. query_mdns_host("esp32");
  134. query_mdns_service("_arduino", "_tcp");
  135. query_mdns_service("_http", "_tcp");
  136. query_mdns_service("_printer", "_tcp");
  137. query_mdns_service("_ipp", "_tcp");
  138. query_mdns_service("_afpovertcp", "_tcp");
  139. query_mdns_service("_smb", "_tcp");
  140. query_mdns_service("_ftp", "_tcp");
  141. query_mdns_service("_nfs", "_tcp");
  142. }
  143. old_level = new_level;
  144. }
  145. static void mdns_example_task(void *pvParameters)
  146. {
  147. #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
  148. /* Send initial queries that are started by CI tester */
  149. query_mdns_host("tinytester");
  150. query_mdns_host_with_gethostbyname("tinytester-lwip.local");
  151. query_mdns_host_with_getaddrinfo("tinytester-lwip.local");
  152. #endif
  153. while(1) {
  154. check_button();
  155. vTaskDelay(50 / portTICK_PERIOD_MS);
  156. }
  157. }
  158. void app_main(void)
  159. {
  160. ESP_ERROR_CHECK(nvs_flash_init());
  161. ESP_ERROR_CHECK(esp_netif_init());
  162. ESP_ERROR_CHECK(esp_event_loop_create_default());
  163. initialise_mdns();
  164. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  165. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  166. * examples/protocols/README.md for more information about this function.
  167. */
  168. ESP_ERROR_CHECK(example_connect());
  169. initialise_button();
  170. xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
  171. }
  172. /** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
  173. * @return host name string allocated from the heap
  174. */
  175. static char* generate_hostname(void)
  176. {
  177. #ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
  178. return strdup(CONFIG_MDNS_HOSTNAME);
  179. #else
  180. uint8_t mac[6];
  181. char *hostname;
  182. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  183. if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) {
  184. abort();
  185. }
  186. return hostname;
  187. #endif
  188. }
  189. #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
  190. /**
  191. * @brief Executes gethostbyname and displays list of resolved addresses.
  192. * Note: This function is used only to test advertised mdns hostnames resolution
  193. */
  194. static void query_mdns_host_with_gethostbyname(char * host)
  195. {
  196. struct hostent *res = gethostbyname(host);
  197. if (res) {
  198. unsigned int i = 0;
  199. while (res->h_addr_list[i] != NULL) {
  200. ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])));
  201. i++;
  202. }
  203. }
  204. }
  205. /**
  206. * @brief Executes getaddrinfo and displays list of resolved addresses.
  207. * Note: This function is used only to test advertised mdns hostnames resolution
  208. */
  209. static void query_mdns_host_with_getaddrinfo(char * host)
  210. {
  211. struct addrinfo hints;
  212. struct addrinfo * res;
  213. memset(&hints, 0, sizeof(hints));
  214. hints.ai_family = AF_UNSPEC;
  215. hints.ai_socktype = SOCK_STREAM;
  216. if (!getaddrinfo(host, NULL, &hints, &res)) {
  217. while (res) {
  218. ESP_LOGI(TAG, "getaddrinfo: %s resolved to: %s", host,
  219. res->ai_family == AF_INET?
  220. inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr):
  221. inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr));
  222. res = res->ai_next;
  223. }
  224. }
  225. }
  226. #endif