mdns.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. mDNS Service
  2. ============
  3. Overview
  4. --------
  5. mDNS is a multicast UDP service that is used to provide local network service and host discovery.
  6. mDNS is installed by default on most operating systems or is available as separate package. On ``Mac OS`` it is installed by default and is called ``Bonjour``. Apple releases an installer for ``Windows`` that can be found `on Apple's support page <https://support.apple.com/downloads/bonjour%2520for%2520windows>`_. On ``Linux``, mDNS is provided by `avahi <https://github.com/lathiat/avahi>`_ and is usually installed by default.
  7. mDNS Properties
  8. ^^^^^^^^^^^^^^^
  9. * ``hostname``: the hostname that the device will respond to. If not set, the ``hostname`` will be read from the interface. Example: ``my-esp32`` will resolve to ``my-esp32.local``
  10. * ``default_instance``: friendly name for your device, like ``Jhon's ESP32 Thing``. If not set, ``hostname`` will be used.
  11. Example method to start mDNS for the STA interface and set ``hostname`` and ``default_instance``:
  12. .. highlight:: c
  13. ::
  14. void start_mdns_service()
  15. {
  16. //initialize mDNS service
  17. esp_err_t err = mdns_init();
  18. if (err) {
  19. printf("MDNS Init failed: %d\n", err);
  20. return;
  21. }
  22. //set hostname
  23. mdns_hostname_set("my-esp32");
  24. //set default instance
  25. mdns_instance_name_set("Jhon's ESP32 Thing");
  26. }
  27. mDNS Services
  28. ^^^^^^^^^^^^^
  29. mDNS can advertise information about network services that your device offers. Each service is defined by a few properties.
  30. * ``instance_name``: friendly name for your service, like ``Jhon's ESP32 Web Server``. If not defined, ``default_instance`` will be used.
  31. * ``service_type``: (required) service type, prepended with underscore. Some common types can be found `here <http://www.dns-sd.org/serviceTypes.html>`_.
  32. * ``proto``: (required) protocol that the service runs on, prepended with underscore. Example: ``_tcp`` or ``_udp``
  33. * ``port``: (required) network port that the service runs on
  34. * ``txt``: ``{var, val}`` array of strings, used to define properties for your service
  35. Example method to add a few services and different properties::
  36. void add_mdns_services()
  37. {
  38. //add our services
  39. mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
  40. mdns_service_add(NULL, "_arduino", "_tcp", 3232, NULL, 0);
  41. mdns_service_add(NULL, "_myservice", "_udp", 1234, NULL, 0);
  42. //NOTE: services must be added before their properties can be set
  43. //use custom instance for the web server
  44. mdns_service_instance_name_set("_http", "_tcp", "Jhon's ESP32 Web Server");
  45. mdns_txt_item_t serviceTxtData[3] = {
  46. {"board","esp32"},
  47. {"u","user"},
  48. {"p","password"}
  49. };
  50. //set txt data for service (will free and replace current data)
  51. mdns_service_txt_set("_http", "_tcp", serviceTxtData, 3);
  52. //change service port
  53. mdns_service_port_set("_myservice", "_udp", 4321);
  54. }
  55. mDNS Query
  56. ^^^^^^^^^^
  57. mDNS provides methods for browsing for services and resolving host's IP/IPv6 addresses.
  58. Results for services are returned as a linked list of ``mdns_result_t`` objects.
  59. Example method to resolve host IPs::
  60. void resolve_mdns_host(const char * host_name)
  61. {
  62. printf("Query A: %s.local", host_name);
  63. struct ip4_addr addr;
  64. addr.addr = 0;
  65. esp_err_t err = mdns_query_a(host_name, 2000, &addr);
  66. if(err){
  67. if(err == ESP_ERR_NOT_FOUND){
  68. printf("Host was not found!");
  69. return;
  70. }
  71. printf("Query Failed");
  72. return;
  73. }
  74. printf(IPSTR, IP2STR(&addr));
  75. }
  76. Example method to resolve local services::
  77. static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
  78. static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
  79. void mdns_print_results(mdns_result_t * results){
  80. mdns_result_t * r = results;
  81. mdns_ip_addr_t * a = NULL;
  82. int i = 1, t;
  83. while(r){
  84. printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
  85. if(r->instance_name){
  86. printf(" PTR : %s\n", r->instance_name);
  87. }
  88. if(r->hostname){
  89. printf(" SRV : %s.local:%u\n", r->hostname, r->port);
  90. }
  91. if(r->txt_count){
  92. printf(" TXT : [%u] ", r->txt_count);
  93. for(t=0; t<r->txt_count; t++){
  94. printf("%s=%s; ", r->txt[t].key, r->txt[t].value);
  95. }
  96. printf("\n");
  97. }
  98. a = r->addr;
  99. while(a){
  100. if(a->addr.type == MDNS_IP_PROTOCOL_V6){
  101. printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
  102. } else {
  103. printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
  104. }
  105. a = a->next;
  106. }
  107. r = r->next;
  108. }
  109. }
  110. void find_mdns_service(const char * service_name, const char * proto)
  111. {
  112. ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
  113. mdns_result_t * results = NULL;
  114. esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
  115. if(err){
  116. ESP_LOGE(TAG, "Query Failed");
  117. return;
  118. }
  119. if(!results){
  120. ESP_LOGW(TAG, "No results found!");
  121. return;
  122. }
  123. mdns_print_results(results);
  124. mdns_query_results_free(results);
  125. }
  126. Example of using the methods above::
  127. void my_app_some_method(){
  128. //search for esp32-mdns.local
  129. resolve_mdns_host("esp32-mdns");
  130. //search for HTTP servers
  131. find_mdns_service("_http", "_tcp");
  132. //or file servers
  133. find_mdns_service("_smb", "_tcp"); //windows sharing
  134. find_mdns_service("_afpovertcp", "_tcp"); //apple sharing
  135. find_mdns_service("_nfs", "_tcp"); //NFS server
  136. find_mdns_service("_ftp", "_tcp"); //FTP server
  137. //or networked printer
  138. find_mdns_service("_printer", "_tcp");
  139. find_mdns_service("_ipp", "_tcp");
  140. }
  141. Application Example
  142. -------------------
  143. mDNS server/scanner example: :example:`protocols/mdns`.
  144. API Reference
  145. -------------
  146. .. include:: /_build/inc/mdns.inc