mdns.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Multicast DNS for lwIP
  2. Author: Erik Ekman
  3. Note! The MDNS responder does not have all features required by the standards.
  4. See notes in src/apps/mdns/mdns.c for what is left. It is however usable in normal
  5. cases - but watch out if many devices on the same network try to use the same
  6. host/service instance names.
  7. How to enable:
  8. ==============
  9. MDNS support does not depend on DNS.
  10. MDNS supports using IPv4 only, v6 only, or v4+v6.
  11. To enable MDNS responder, set
  12. LWIP_MDNS_RESPONDER = 1
  13. in lwipopts.h and add src/apps/mdns/mdns.c to your list of files to build.
  14. The max number of services supported per netif is defined by MDNS_MAX_SERVICES,
  15. default is 1.
  16. Increase MEMP_NUM_UDP_PCB by 1. MDNS needs one PCB.
  17. Increase LWIP_NUM_NETIF_CLIENT_DATA by 1 (MDNS needs one entry on netif).
  18. MDNS with IPv4 requires LWIP_IGMP = 1, and preferably LWIP_AUTOIP = 1.
  19. MDNS with IPv6 requires LWIP_IPV6_MLD = 1, and that a link-local address is
  20. generated.
  21. The MDNS code puts its structs on the stack where suitable to reduce dynamic
  22. memory allocation. It may use up to 1kB of stack.
  23. MDNS (like other apps) needs a strncasecmp() implementation. If you have one, define
  24. 'lwip_strnicmp' to it. Otherwise the code will provide an implementation
  25. for you.
  26. How to use:
  27. ===========
  28. Call mdns_resp_init() during system initialization.
  29. This opens UDP sockets on port 5353 for IPv4 and IPv6.
  30. To start responding on a netif, run
  31. mdns_resp_add_netif(struct netif *netif, const char *hostname)
  32. The hostname will be copied. If this returns successfully, the netif will join
  33. the multicast groups and any MDNS/legacy DNS requests sent unicast or multicast
  34. to port 5353 will be handled:
  35. - <hostname>.local type A, AAAA or ANY returns relevant IP addresses
  36. - Reverse lookups (PTR in-addr.arpa, ip6.arpa) of netif addresses
  37. returns <hostname>.local
  38. MDNS allows UTF-8 names, but it is recommended to stay within ASCII,
  39. since the default case-insensitive comparison assumes this.
  40. Call mdns_resp_announce() every time the IP address on the netif has changed.
  41. Call mdns_resp_restart() every time the network interface comes up after being
  42. down, for example cable connected after being disconnected, administrative
  43. interface comes up after being down, or the device wakes up from sleep.
  44. To stop responding on a netif, run
  45. mdns_resp_remove_netif(struct netif *netif)
  46. Adding services:
  47. ================
  48. The netif first needs to be registered. Then run
  49. mdns_resp_add_service(struct netif *netif, const char *name, const char *service,
  50. enum mdns_sd_proto proto, u16_t port,
  51. service_get_txt_fn_t txt_fn, void *txt_userdata);
  52. The name and service pointers will be copied. Name refers to the name of the
  53. service instance, and service is the type of service, like _http
  54. proto can be DNSSD_PROTO_UDP or DNSSD_PROTO_TCP which represent _udp and _tcp.
  55. If this call returns successfully, the following queries will be answered:
  56. - _services._dns-sd._udp.local type PTR returns <service>.<proto>.local
  57. - <service>.<proto>.local type PTR returns <name>.<service>.<proto>.local
  58. - <name>.<service>.<proto>.local type SRV returns hostname and port of service
  59. - <name>.<service>.<proto>.local type TXT builds text strings by calling txt_fn
  60. with the supplied userdata. The callback adds strings to the reply by calling
  61. mdns_resp_add_service_txtitem(struct mdns_service *service, char *txt,
  62. int txt_len). Example callback method:
  63. static void srv_txt(struct mdns_service *service, void *txt_userdata)
  64. {
  65. res = mdns_resp_add_service_txtitem(service, "path=/", 6);
  66. LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return);
  67. }
  68. Since a hostname struct is used for TXT storage each single item can be max
  69. 63 bytes long, and the total max length (including length bytes for each
  70. item) is 255 bytes.
  71. If your device runs a webserver on port 80, an example call might be:
  72. mdns_resp_add_service(netif, "myweb", "_http"
  73. DNSSD_PROTO_TCP, 80, srv_txt, NULL);
  74. which will publish myweb._http._tcp.local for any hosts looking for web servers,
  75. and point them to <hostname>.local:80
  76. Relevant information will be sent as additional records to reduce number of
  77. requests required from a client.
  78. To remove a service from a netif, run
  79. mdns_resp_del_service(struct netif *netif, u8_t slot)