mdns.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. ::
  13. mdns_server_t * mdns = NULL;
  14. void start_mdns_service()
  15. {
  16. //initialize mDNS service on STA interface
  17. esp_err_t err = mdns_init(TCPIP_ADAPTER_IF_STA, &mdns);
  18. if (err) {
  19. printf("MDNS Init failed: %d\n", err);
  20. return;
  21. }
  22. //set hostname
  23. mdns_set_hostname(mdns, "my-esp32");
  24. //set default instance
  25. mdns_set_instance(mdns, "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. * ``service``: (required) service type, prepended with underscore. Some common types can be found `here <http://www.dns-sd.org/serviceTypes.html>`_.
  31. * ``proto``: (required) protocol that the service runs on, prepended with underscore. Example: ``_tcp`` or ``_udp``
  32. * ``port``: (required) network port that the service runs on
  33. * ``instance``: friendly name for your service, like ``Jhon's ESP32 Web Server``. If not defined, ``default_instance`` will be used.
  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. ::
  37. void add_mdns_services()
  38. {
  39. //add our services
  40. mdns_service_add(mdns, "_http", "_tcp", 80);
  41. mdns_service_add(mdns, "_arduino", "_tcp", 3232);
  42. mdns_service_add(mdns, "_myservice", "_udp", 1234);
  43. //NOTE: services must be added before their properties can be set
  44. //use custom instance for the web server
  45. mdns_service_instance_set(mdns, "_http", "_tcp", "Jhon's ESP32 Web Server");
  46. const char * arduTxtData[4] = {
  47. "board=esp32",
  48. "tcp_check=no",
  49. "ssh_upload=no",
  50. "auth_upload=no"
  51. };
  52. //set txt data for service (will free and replace current data)
  53. mdns_service_txt_set(mdns, "_arduino", "_tcp", 4, arduTxtData);
  54. //change service port
  55. mdns_service_port_set(mdns, "_myservice", "_udp", 4321);
  56. }
  57. mDNS Query
  58. ^^^^^^^^^^
  59. mDNS provides methods for browsing for services and resolving host's IP/IPv6 addresses.
  60. Results are returned as a linked list of ``mdns_result_t`` objects. If the result is from host query,
  61. it will contain only ``addr`` and ``addrv6`` if found. Service queries will populate all fields
  62. in a result that were found.
  63. Example method to resolve host IPs:
  64. ::
  65. void resolve_mdns_host(const char * hostname)
  66. {
  67. printf("mDNS Host Lookup: %s.local\n", hostname);
  68. //run search for 1000 ms
  69. if (mdns_query(mdns, hostname, NULL, 1000)) {
  70. //results were found
  71. const mdns_result_t * results = mdns_result_get(mdns, 0);
  72. //itterate through all results
  73. size_t i = 1;
  74. while(results) {
  75. //print result information
  76. printf(" %u: IP:" IPSTR ", IPv6:" IPV6STR "\n", i++
  77. IP2STR(&results->addr), IPV62STR(results->addrv6));
  78. //load next result. Will be NULL if this was the last one
  79. results = results->next;
  80. }
  81. //free the results from memory
  82. mdns_result_free(mdns);
  83. } else {
  84. //host was not found
  85. printf(" Host Not Found\n");
  86. }
  87. }
  88. Example method to resolve local services:
  89. ::
  90. void find_mdns_service(const char * service, const char * proto)
  91. {
  92. printf("mDNS Service Lookup: %s.%s\n", service, proto);
  93. //run search for 1000 ms
  94. if (mdns_query(mdns, service, proto, 1000)) {
  95. //results were found
  96. const mdns_result_t * results = mdns_result_get(mdns, 0);
  97. //itterate through all results
  98. size_t i = 1;
  99. while(results) {
  100. //print result information
  101. printf(" %u: hostname:%s, instance:\"%s\", IP:" IPSTR ", IPv6:" IPV6STR ", port:%u, txt:%s\n", i++,
  102. (results->host)?results->host:"NULL", (results->instance)?results->instance:"NULL",
  103. IP2STR(&results->addr), IPV62STR(results->addrv6),
  104. results->port, (results->txt)?results->txt:"\r");
  105. //load next result. Will be NULL if this was the last one
  106. results = results->next;
  107. }
  108. //free the results from memory
  109. mdns_result_free(mdns);
  110. } else {
  111. //service was not found
  112. printf(" Service Not Found\n");
  113. }
  114. }
  115. Example of using the methods above:
  116. ::
  117. void my_app_some_method(){
  118. //search for esp32-mdns.local
  119. resolve_mdns_host("esp32-mdns");
  120. //search for HTTP servers
  121. find_mdns_service("_http", "_tcp");
  122. //or file servers
  123. find_mdns_service("_smb", "_tcp"); //windows sharing
  124. find_mdns_service("_afpovertcp", "_tcp"); //apple sharing
  125. find_mdns_service("_nfs", "_tcp"); //NFS server
  126. find_mdns_service("_ftp", "_tcp"); //FTP server
  127. //or networked printer
  128. find_mdns_service("_printer", "_tcp");
  129. find_mdns_service("_ipp", "_tcp");
  130. }
  131. Application Example
  132. -------------------
  133. mDNS server/scanner example: :example:`protocols/mdns`.
  134. API Reference
  135. -------------
  136. Header Files
  137. ^^^^^^^^^^^^
  138. * :component_file:`mdns/include/mdns.h`
  139. Macros
  140. ^^^^^^
  141. Type Definitions
  142. ^^^^^^^^^^^^^^^^
  143. .. doxygentypedef:: mdns_server_t
  144. .. doxygentypedef:: mdns_result_t
  145. Enumerations
  146. ^^^^^^^^^^^^
  147. Structures
  148. ^^^^^^^^^^
  149. .. doxygenstruct:: mdns_result_s
  150. :members:
  151. Functions
  152. ^^^^^^^^^
  153. .. doxygenfunction:: mdns_init
  154. .. doxygenfunction:: mdns_free
  155. .. doxygenfunction:: mdns_set_hostname
  156. .. doxygenfunction:: mdns_set_instance
  157. .. doxygenfunction:: mdns_service_add
  158. .. doxygenfunction:: mdns_service_remove
  159. .. doxygenfunction:: mdns_service_instance_set
  160. .. doxygenfunction:: mdns_service_txt_set
  161. .. doxygenfunction:: mdns_service_port_set
  162. .. doxygenfunction:: mdns_service_remove_all
  163. .. doxygenfunction:: mdns_query
  164. .. doxygenfunction:: mdns_query_end
  165. .. doxygenfunction:: mdns_result_get_count
  166. .. doxygenfunction:: mdns_result_get
  167. .. doxygenfunction:: mdns_result_free