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