mdns.rst 6.5 KB

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