Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/features/lwipstack/lwip/doc/mdns.txt@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | Multicast DNS for lwIP |
kadonotakashi | 0:8fdf9a60065b | 2 | |
kadonotakashi | 0:8fdf9a60065b | 3 | Author: Erik Ekman |
kadonotakashi | 0:8fdf9a60065b | 4 | |
kadonotakashi | 0:8fdf9a60065b | 5 | |
kadonotakashi | 0:8fdf9a60065b | 6 | Note! The MDNS responder does not have all features required by the standards. |
kadonotakashi | 0:8fdf9a60065b | 7 | See notes in src/apps/mdns/mdns.c for what is left. It is however usable in normal |
kadonotakashi | 0:8fdf9a60065b | 8 | cases - but watch out if many devices on the same network try to use the same |
kadonotakashi | 0:8fdf9a60065b | 9 | host/service instance names. |
kadonotakashi | 0:8fdf9a60065b | 10 | |
kadonotakashi | 0:8fdf9a60065b | 11 | |
kadonotakashi | 0:8fdf9a60065b | 12 | How to enable: |
kadonotakashi | 0:8fdf9a60065b | 13 | ============== |
kadonotakashi | 0:8fdf9a60065b | 14 | |
kadonotakashi | 0:8fdf9a60065b | 15 | MDNS support does not depend on DNS. |
kadonotakashi | 0:8fdf9a60065b | 16 | MDNS supports using IPv4 only, v6 only, or v4+v6. |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | To enable MDNS responder, set |
kadonotakashi | 0:8fdf9a60065b | 19 | LWIP_MDNS_RESPONDER = 1 |
kadonotakashi | 0:8fdf9a60065b | 20 | in lwipopts.h and add src/apps/mdns/mdns.c to your list of files to build. |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | The max number of services supported per netif is defined by MDNS_MAX_SERVICES, |
kadonotakashi | 0:8fdf9a60065b | 23 | default is 1. |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | Increase MEMP_NUM_UDP_PCB by 1. MDNS needs one PCB. |
kadonotakashi | 0:8fdf9a60065b | 26 | Increase LWIP_NUM_NETIF_CLIENT_DATA by 1 (MDNS needs one entry on netif). |
kadonotakashi | 0:8fdf9a60065b | 27 | |
kadonotakashi | 0:8fdf9a60065b | 28 | MDNS with IPv4 requires LWIP_IGMP = 1, and preferably LWIP_AUTOIP = 1. |
kadonotakashi | 0:8fdf9a60065b | 29 | MDNS with IPv6 requires LWIP_IPV6_MLD = 1, and that a link-local address is |
kadonotakashi | 0:8fdf9a60065b | 30 | generated. |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | The MDNS code puts its structs on the stack where suitable to reduce dynamic |
kadonotakashi | 0:8fdf9a60065b | 33 | memory allocation. It may use up to 1kB of stack. |
kadonotakashi | 0:8fdf9a60065b | 34 | |
kadonotakashi | 0:8fdf9a60065b | 35 | MDNS needs a strncasecmp() implementation. If you have one, define |
kadonotakashi | 0:8fdf9a60065b | 36 | LWIP_MDNS_STRNCASECMP to it. Otherwise the code will provide an implementation |
kadonotakashi | 0:8fdf9a60065b | 37 | for you. |
kadonotakashi | 0:8fdf9a60065b | 38 | |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | How to use: |
kadonotakashi | 0:8fdf9a60065b | 41 | =========== |
kadonotakashi | 0:8fdf9a60065b | 42 | |
kadonotakashi | 0:8fdf9a60065b | 43 | Call mdns_resp_init() during system initialization. |
kadonotakashi | 0:8fdf9a60065b | 44 | This opens UDP sockets on port 5353 for IPv4 and IPv6. |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | To start responding on a netif, run |
kadonotakashi | 0:8fdf9a60065b | 48 | mdns_resp_add_netif(struct netif *netif, char *hostname, u32_t dns_ttl) |
kadonotakashi | 0:8fdf9a60065b | 49 | |
kadonotakashi | 0:8fdf9a60065b | 50 | The hostname will be copied. If this returns successfully, the netif will join |
kadonotakashi | 0:8fdf9a60065b | 51 | the multicast groups and any MDNS/legacy DNS requests sent unicast or multicast |
kadonotakashi | 0:8fdf9a60065b | 52 | to port 5353 will be handled: |
kadonotakashi | 0:8fdf9a60065b | 53 | - <hostname>.local type A, AAAA or ANY returns relevant IP addresses |
kadonotakashi | 0:8fdf9a60065b | 54 | - Reverse lookups (PTR in-addr.arpa, ip6.arpa) of netif addresses |
kadonotakashi | 0:8fdf9a60065b | 55 | returns <hostname>.local |
kadonotakashi | 0:8fdf9a60065b | 56 | Answers will use the supplied TTL (in seconds) |
kadonotakashi | 0:8fdf9a60065b | 57 | MDNS allows UTF-8 names, but it is recommended to stay within ASCII, |
kadonotakashi | 0:8fdf9a60065b | 58 | since the default case-insensitive comparison assumes this. |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | It is recommended to call this function after an IPv4 address has been set, |
kadonotakashi | 0:8fdf9a60065b | 61 | since there is currently no check if the v4 address is valid. |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | Call mdns_resp_netif_settings_changed() every time the IP address |
kadonotakashi | 0:8fdf9a60065b | 64 | on the netif has changed. |
kadonotakashi | 0:8fdf9a60065b | 65 | |
kadonotakashi | 0:8fdf9a60065b | 66 | To stop responding on a netif, run |
kadonotakashi | 0:8fdf9a60065b | 67 | mdns_resp_remove_netif(struct netif *netif) |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | Adding services: |
kadonotakashi | 0:8fdf9a60065b | 71 | ================ |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | The netif first needs to be registered. Then run |
kadonotakashi | 0:8fdf9a60065b | 74 | mdns_resp_add_service(struct netif *netif, char *name, char *service, |
kadonotakashi | 0:8fdf9a60065b | 75 | u16_t proto, u16_t port, u32_t dns_ttl, |
kadonotakashi | 0:8fdf9a60065b | 76 | service_get_txt_fn_t txt_fn, void *txt_userdata); |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | The name and service pointers will be copied. Name refers to the name of the |
kadonotakashi | 0:8fdf9a60065b | 79 | service instance, and service is the type of service, like _http |
kadonotakashi | 0:8fdf9a60065b | 80 | proto can be DNSSD_PROTO_UDP or DNSSD_PROTO_TCP which represent _udp and _tcp. |
kadonotakashi | 0:8fdf9a60065b | 81 | If this call returns successfully, the following queries will be answered: |
kadonotakashi | 0:8fdf9a60065b | 82 | - _services._dns-sd._udp.local type PTR returns <service>.<proto>.local |
kadonotakashi | 0:8fdf9a60065b | 83 | - <service>.<proto>.local type PTR returns <name>.<service>.<proto>.local |
kadonotakashi | 0:8fdf9a60065b | 84 | - <name>.<service>.<proto>.local type SRV returns hostname and port of service |
kadonotakashi | 0:8fdf9a60065b | 85 | - <name>.<service>.<proto>.local type TXT builds text strings by calling txt_fn |
kadonotakashi | 0:8fdf9a60065b | 86 | with the supplied userdata. The callback adds strings to the reply by calling |
kadonotakashi | 0:8fdf9a60065b | 87 | mdns_resp_add_service_txtitem(struct mdns_service *service, char *txt, |
kadonotakashi | 0:8fdf9a60065b | 88 | int txt_len). Example callback method: |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | static void srv_txt(struct mdns_service *service, void *txt_userdata) |
kadonotakashi | 0:8fdf9a60065b | 91 | { |
kadonotakashi | 0:8fdf9a60065b | 92 | res = mdns_resp_add_service_txtitem(service, "path=/", 6); |
kadonotakashi | 0:8fdf9a60065b | 93 | LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return); |
kadonotakashi | 0:8fdf9a60065b | 94 | } |
kadonotakashi | 0:8fdf9a60065b | 95 | |
kadonotakashi | 0:8fdf9a60065b | 96 | Since a hostname struct is used for TXT storage each single item can be max |
kadonotakashi | 0:8fdf9a60065b | 97 | 63 bytes long, and the total max length (including length bytes for each |
kadonotakashi | 0:8fdf9a60065b | 98 | item) is 255 bytes. |
kadonotakashi | 0:8fdf9a60065b | 99 | |
kadonotakashi | 0:8fdf9a60065b | 100 | If your device runs a webserver on port 80, an example call might be: |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | mdns_resp_add_service(netif, "myweb", "_http" |
kadonotakashi | 0:8fdf9a60065b | 103 | DNSSD_PROTO_TCP, 80, 3600, srv_txt, NULL); |
kadonotakashi | 0:8fdf9a60065b | 104 | |
kadonotakashi | 0:8fdf9a60065b | 105 | which will publish myweb._http._tcp.local for any hosts looking for web servers, |
kadonotakashi | 0:8fdf9a60065b | 106 | and point them to <hostname>.local:80 |
kadonotakashi | 0:8fdf9a60065b | 107 | |
kadonotakashi | 0:8fdf9a60065b | 108 | Relevant information will be sent as additional records to reduce number of |
kadonotakashi | 0:8fdf9a60065b | 109 | requests required from a client. |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | Removing services is currently not supported. Services are removed when the |
kadonotakashi | 0:8fdf9a60065b | 112 | netif is removed. |
kadonotakashi | 0:8fdf9a60065b | 113 |