- OCaml 100%
mdns #
mDNS/DNS-SD service discovery (RFC 6762/6763) for OCaml.
Overview #
Printers, smart plugs and media servers on a local network announce
themselves with Multicast DNS (RFC 6762) and DNS-based Service Discovery
(RFC 6763), so a client can find them with no DNS server and no
configuration. mdns sends a DNS-SD query to the mDNS multicast group over
Eio, collects the answers for a given time, and parses their PTR, SRV, TXT, A
and AAAA records.
Queries are encoded with the dns library. Responses are parsed here over
raw domain names: a DNS-SD instance name such as My Device._http._tcp.local
may contain spaces, and a hostname may not.
The parser drops records it does not understand rather than failing, and a
fuzz test exercises it with Alcobar.
The package also installs mdns-query, a command-line tool that runs one
query and prints the answers.
Installation #
Install with opam:
$ opam install mdns
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install mdns
Usage #
(* Discover HTTP services on the local network. *)
let run () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let name = Domain_name.of_string_exn "_http._tcp.local" in
let responses =
Mdns.query ~sw
~net:(Eio.Stdenv.net env)
~clock:(Eio.Stdenv.clock env)
~timeout:2.0 name
in
let merged = Mdns.merge responses in
List.iter
(fun (_, instance) -> Fmt.pr "Found: %a@." Domain_name.pp instance)
merged.Mdns.ptrs
API #
Mdns.query ~sw ~net ~clock ~timeout namesends a query and collects the responses that arrive before the timeout.Mdns.merge responsescombines several responses into one.Mdns.parse bufparses a raw mDNS packet.Mdns.encode_query nameencodes a PTR query packet.
Related Work #
- ocaml-dns is the DNS library this package encodes queries with. It is a full DNS client and server, but it does not do mDNS multicast, and its hostname type refuses DNS-SD instance names with spaces.
Licence #
ISC License. See LICENSE.md for details.