mdns.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 needs a strncasecmp() implementation. If you have one, define
  24. LWIP_MDNS_STRNCASECMP 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, char *hostname, u32_t dns_ttl)
  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. Answers will use the supplied TTL (in seconds)
  39. MDNS allows UTF-8 names, but it is recommended to stay within ASCII,
  40. since the default case-insensitive comparison assumes this.
  41. It is recommended to call this function after an IPv4 address has been set,
  42. since there is currently no check if the v4 address is valid.
  43. Call mdns_resp_netif_settings_changed() every time the IP address
  44. on the netif has changed.
  45. To stop responding on a netif, run
  46. mdns_resp_remove_netif(struct netif *netif)
  47. Adding services:
  48. ================
  49. The netif first needs to be registered. Then run
  50. mdns_resp_add_service(struct netif *netif, char *name, char *service,
  51. u16_t proto, u16_t port, u32_t dns_ttl,
  52. service_get_txt_fn_t txt_fn, void *txt_userdata);
  53. The name and service pointers will be copied. Name refers to the name of the
  54. service instance, and service is the type of service, like _http
  55. proto can be DNSSD_PROTO_UDP or DNSSD_PROTO_TCP which represent _udp and _tcp.
  56. If this call returns successfully, the following queries will be answered:
  57. - _services._dns-sd._udp.local type PTR returns <service>.<proto>.local
  58. - <service>.<proto>.local type PTR returns <name>.<service>.<proto>.local
  59. - <name>.<service>.<proto>.local type SRV returns hostname and port of service
  60. - <name>.<service>.<proto>.local type TXT builds text strings by calling txt_fn
  61. with the supplied userdata. The callback adds strings to the reply by calling
  62. mdns_resp_add_service_txtitem(struct mdns_service *service, char *txt,
  63. int txt_len). Example callback method:
  64. static void srv_txt(struct mdns_service *service, void *txt_userdata)
  65. {
  66. res = mdns_resp_add_service_txtitem(service, "path=/", 6);
  67. LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return);
  68. }
  69. Since a hostname struct is used for TXT storage each single item can be max
  70. 63 bytes long, and the total max length (including length bytes for each
  71. item) is 255 bytes.
  72. If your device runs a webserver on port 80, an example call might be:
  73. mdns_resp_add_service(netif, "myweb", "_http"
  74. DNSSD_PROTO_TCP, 80, 3600, srv_txt, NULL);
  75. which will publish myweb._http._tcp.local for any hosts looking for web servers,
  76. and point them to <hostname>.local:80
  77. Relevant information will be sent as additional records to reduce number of
  78. requests required from a client.
  79. Removing services is currently not supported. Services are removed when the
  80. netif is removed.