esp-idf/docs/en/api-reference/protocols/mdns.rst

190 lines
6.3 KiB
ReStructuredText
Raw Normal View History

2017-01-04 11:54:07 -05:00
mDNS Service
============
2019-03-21 03:38:48 -04:00
:link_to_translation:`zh_CN:[中文]`
2017-01-04 11:54:07 -05:00
Overview
--------
mDNS is a multicast UDP service that is used to provide local network service and host discovery.
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.
mDNS Properties
^^^^^^^^^^^^^^^
* ``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``
* ``default_instance``: friendly name for your device, like ``Jhon's ESP32 Thing``. If not set, ``hostname`` will be used.
Example method to start mDNS for the STA interface and set ``hostname`` and ``default_instance``:
.. highlight:: c
::
2017-01-04 11:54:07 -05:00
void start_mdns_service()
{
2017-12-07 08:21:40 -05:00
//initialize mDNS service
esp_err_t err = mdns_init();
2017-01-04 11:54:07 -05:00
if (err) {
printf("MDNS Init failed: %d\n", err);
return;
}
//set hostname
2017-12-07 08:21:40 -05:00
mdns_hostname_set("my-esp32");
2017-01-04 11:54:07 -05:00
//set default instance
2017-12-07 08:21:40 -05:00
mdns_instance_name_set("Jhon's ESP32 Thing");
2017-01-04 11:54:07 -05:00
}
mDNS Services
^^^^^^^^^^^^^
mDNS can advertise information about network services that your device offers. Each service is defined by a few properties.
2017-12-07 08:21:40 -05:00
* ``instance_name``: friendly name for your service, like ``Jhon's ESP32 Web Server``. If not defined, ``default_instance`` will be used.
* ``service_type``: (required) service type, prepended with underscore. Some common types can be found `here <http://www.dns-sd.org/serviceTypes.html>`_.
2017-01-04 11:54:07 -05:00
* ``proto``: (required) protocol that the service runs on, prepended with underscore. Example: ``_tcp`` or ``_udp``
* ``port``: (required) network port that the service runs on
2017-12-07 08:21:40 -05:00
* ``txt``: ``{var, val}`` array of strings, used to define properties for your service
2017-01-04 11:54:07 -05:00
Example method to add a few services and different properties::
2017-01-04 11:54:07 -05:00
void add_mdns_services()
{
//add our services
2017-12-07 08:21:40 -05:00
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
mdns_service_add(NULL, "_arduino", "_tcp", 3232, NULL, 0);
mdns_service_add(NULL, "_myservice", "_udp", 1234, NULL, 0);
2017-01-04 11:54:07 -05:00
//NOTE: services must be added before their properties can be set
//use custom instance for the web server
2017-12-07 08:21:40 -05:00
mdns_service_instance_name_set("_http", "_tcp", "Jhon's ESP32 Web Server");
2017-01-04 11:54:07 -05:00
2017-12-07 08:21:40 -05:00
mdns_txt_item_t serviceTxtData[3] = {
{"board","esp32"},
{"u","user"},
{"p","password"}
2017-01-04 11:54:07 -05:00
};
//set txt data for service (will free and replace current data)
2017-12-07 08:21:40 -05:00
mdns_service_txt_set("_http", "_tcp", serviceTxtData, 3);
2017-01-04 11:54:07 -05:00
//change service port
2017-12-07 08:21:40 -05:00
mdns_service_port_set("_myservice", "_udp", 4321);
2017-01-04 11:54:07 -05:00
}
mDNS Query
^^^^^^^^^^
mDNS provides methods for browsing for services and resolving host's IP/IPv6 addresses.
2017-12-07 08:21:40 -05:00
Results for services are returned as a linked list of ``mdns_result_t`` objects.
2017-01-04 11:54:07 -05:00
Example method to resolve host IPs::
2017-01-04 11:54:07 -05:00
2017-12-07 08:21:40 -05:00
void resolve_mdns_host(const char * host_name)
2017-01-04 11:54:07 -05:00
{
2017-12-07 08:21:40 -05:00
printf("Query A: %s.local", host_name);
struct ip4_addr addr;
addr.addr = 0;
esp_err_t err = mdns_query_a(host_name, 2000, &addr);
if(err){
if(err == ESP_ERR_NOT_FOUND){
printf("Host was not found!");
return;
2017-01-04 11:54:07 -05:00
}
2017-12-07 08:21:40 -05:00
printf("Query Failed");
return;
2017-01-04 11:54:07 -05:00
}
2017-12-07 08:21:40 -05:00
printf(IPSTR, IP2STR(&addr));
2017-01-04 11:54:07 -05:00
}
Example method to resolve local services::
2017-01-04 11:54:07 -05:00
2017-12-07 08:21:40 -05:00
static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
void mdns_print_results(mdns_result_t * results){
mdns_result_t * r = results;
mdns_ip_addr_t * a = NULL;
int i = 1, t;
while(r){
printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
if(r->instance_name){
printf(" PTR : %s\n", r->instance_name);
}
if(r->hostname){
printf(" SRV : %s.local:%u\n", r->hostname, r->port);
}
if(r->txt_count){
printf(" TXT : [%u] ", r->txt_count);
for(t=0; t<r->txt_count; t++){
printf("%s=%s; ", r->txt[t].key, r->txt[t].value);
}
printf("\n");
2017-01-04 11:54:07 -05:00
}
2017-12-07 08:21:40 -05:00
a = r->addr;
while(a){
if(a->addr.type == IPADDR_TYPE_V6){
2017-12-07 08:21:40 -05:00
printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
} else {
printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
}
a = a->next;
}
r = r->next;
2017-01-04 11:54:07 -05:00
}
2017-12-07 08:21:40 -05:00
}
void find_mdns_service(const char * service_name, const char * proto)
{
ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
mdns_result_t * results = NULL;
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
if(err){
ESP_LOGE(TAG, "Query Failed");
return;
}
if(!results){
ESP_LOGW(TAG, "No results found!");
return;
}
mdns_print_results(results);
mdns_query_results_free(results);
2017-01-04 11:54:07 -05:00
}
Example of using the methods above::
2017-01-04 11:54:07 -05:00
void my_app_some_method(){
//search for esp32-mdns.local
resolve_mdns_host("esp32-mdns");
//search for HTTP servers
find_mdns_service("_http", "_tcp");
//or file servers
find_mdns_service("_smb", "_tcp"); //windows sharing
find_mdns_service("_afpovertcp", "_tcp"); //apple sharing
find_mdns_service("_nfs", "_tcp"); //NFS server
find_mdns_service("_ftp", "_tcp"); //FTP server
//or networked printer
find_mdns_service("_printer", "_tcp");
find_mdns_service("_ipp", "_tcp");
}
Application Example
-------------------
docs: use custom roles to generate GitHub links This change replaces direct links to GitHub master branch with auto-generated links using docutils custom roles. These auto-generated links point to the tree or blob for the git commit ID (or tag) of the repository. This is needed to ensure that links don’t become broken when files in master branch are moved around or deleted. The following roles are introduced: - :idf:`path` - points to directory inside ESP-IDF - :idf_blob:`path` - points to file inside ESP-IDF - :idf_raw:`path` - points to raw view of the file inside ESP-IDF - :component:`path` - points to directory inside ESP-IDF components dir - :component_blob:`path` - points to file inside ESP-IDF components dir - :component_raw:`path` - points to raw view of the file inside ESP-IDF components dir - :example:`path` - points to directory inside ESP-IDF examples dir - :example_blob:`path` - points to file inside ESP-IDF examples dir - :example_raw:`path` - points to raw view of the file inside ESP-IDF examples dir A check is added to the CI build script, which searches RST files for presence of hard-coded links (identified by tree/master, blob/master, or raw/master part of the URL). This check can be run manually: cd docs && make gh-linkcheck Additionally, Sphinx linkcheck build type is used to create new CI test, which check for broken links. This test has to be triggered explicitly, because including it in normal build process (when the commit is not yet deployed to Github) will not work. It can be triggered in a regular fashion using a combination of cron and Curl, similar to stress tests.
2017-01-19 03:16:06 -05:00
mDNS server/scanner example: :example:`protocols/mdns`.
2017-01-04 11:54:07 -05:00
API Reference
-------------
.. include:: /_build/inc/mdns.inc
2017-01-04 11:54:07 -05:00