mdns_example_main.c 7.9 KB

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