mdns.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. mdns_server_t * mdns = NULL;
  15. void start_mdns_service()
  16. {
  17. //initialize mDNS service on STA interface
  18. esp_err_t err = mdns_init(TCPIP_ADAPTER_IF_STA, &mdns);
  19. if (err) {
  20. printf("MDNS Init failed: %d\n", err);
  21. return;
  22. }
  23. //set hostname
  24. mdns_set_hostname(mdns, "my-esp32");
  25. //set default instance
  26. mdns_set_instance(mdns, "Jhon's ESP32 Thing");
  27. }
  28. mDNS Services
  29. ^^^^^^^^^^^^^
  30. mDNS can advertise information about network services that your device offers. Each service is defined by a few properties.
  31. * ``service``: (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. * ``instance``: friendly name for your service, like ``Jhon's ESP32 Web Server``. If not defined, ``default_instance`` will be used.
  35. * ``txt``: ``var=val`` array of strings, used to define properties for your service
  36. Example method to add a few services and different properties::
  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, it will contain only ``addr`` and ``addrv6`` if found. Service queries will populate all fields in a result that were found.
  61. Example method to resolve host IPs::
  62. void resolve_mdns_host(const char * hostname)
  63. {
  64. printf("mDNS Host Lookup: %s.local\n", hostname);
  65. //run search for 1000 ms
  66. if (mdns_query(mdns, hostname, NULL, 1000)) {
  67. //results were found
  68. const mdns_result_t * results = mdns_result_get(mdns, 0);
  69. //itterate through all results
  70. size_t i = 1;
  71. while(results) {
  72. //print result information
  73. printf(" %u: IP:" IPSTR ", IPv6:" IPV6STR "\n", i++
  74. IP2STR(&results->addr), IPV62STR(results->addrv6));
  75. //load next result. Will be NULL if this was the last one
  76. results = results->next;
  77. }
  78. //free the results from memory
  79. mdns_result_free(mdns);
  80. } else {
  81. //host was not found
  82. printf(" Host Not Found\n");
  83. }
  84. }
  85. Example method to resolve local services::
  86. void find_mdns_service(const char * service, const char * proto)
  87. {
  88. printf("mDNS Service Lookup: %s.%s\n", service, proto);
  89. //run search for 1000 ms
  90. if (mdns_query(mdns, service, proto, 1000)) {
  91. //results were found
  92. const mdns_result_t * results = mdns_result_get(mdns, 0);
  93. //itterate through all results
  94. size_t i = 1;
  95. while(results) {
  96. //print result information
  97. printf(" %u: hostname:%s, instance:\"%s\", IP:" IPSTR ", IPv6:" IPV6STR ", port:%u, txt:%s\n", i++,
  98. (results->host)?results->host:"NULL", (results->instance)?results->instance:"NULL",
  99. IP2STR(&results->addr), IPV62STR(results->addrv6),
  100. results->port, (results->txt)?results->txt:"\r");
  101. //load next result. Will be NULL if this was the last one
  102. results = results->next;
  103. }
  104. //free the results from memory
  105. mdns_result_free(mdns);
  106. } else {
  107. //service was not found
  108. printf(" Service Not Found\n");
  109. }
  110. }
  111. Example of using the methods above::
  112. void my_app_some_method(){
  113. //search for esp32-mdns.local
  114. resolve_mdns_host("esp32-mdns");
  115. //search for HTTP servers
  116. find_mdns_service("_http", "_tcp");
  117. //or file servers
  118. find_mdns_service("_smb", "_tcp"); //windows sharing
  119. find_mdns_service("_afpovertcp", "_tcp"); //apple sharing
  120. find_mdns_service("_nfs", "_tcp"); //NFS server
  121. find_mdns_service("_ftp", "_tcp"); //FTP server
  122. //or networked printer
  123. find_mdns_service("_printer", "_tcp");
  124. find_mdns_service("_ipp", "_tcp");
  125. }
  126. Application Example
  127. -------------------
  128. mDNS server/scanner example: :example:`protocols/mdns`.
  129. API Reference
  130. -------------
  131. .. include:: /_build/inc/mdns.inc