0conf
2026-11-29
Pure Common Lisp mDNS (RFC 6762) and DNS-SD (RFC 6763) implementation.
0conf
A pure Common Lisp implementation of mDNS (RFC 6762) and DNS-SD (RFC 6763) — "zeroconf" service advertisement and discovery on the local link.
SBCL only (the multicast transport uses sb-bsd-sockets + sb-alien).
Layout
| File | Layer | Status |
|---|---|---|
src/octets.lisp | Byte cursor + DNS name (de)compression | ✅ |
src/records.lisp | CLOS records: A, AAAA, PTR, SRV, TXT, NSEC | ✅ |
src/message.lisp | DNS header + four sections | ✅ |
src/cache.lisp | TTL cache + cache-flush + goodbye | ✅ |
src/service-info.lisp | DNS-SD expand ↔ reassemble | ✅ |
src/transport.lisp | Per-interface IPv4+IPv6 multicast (getifaddrs+setsockopt FFI) | ✅ |
src/responder.lisp | Listener, answer, probe+conflict-rename, announce, goodbye | ✅ |
src/browser.lisp | browse-once snapshot + live async browse-services | ✅ |
Use
(ql:quickload :0conf) ; or (asdf:load-system :0conf) ;; One-shot snapshot — discover printers on the LAN for 3s: (0conf:browse-once "_ipp._tcp.local" :timeout 3.0) ;; Resolve one known instance to a service-info (host/port/txt/addresses): (0conf:resolve "Front Desk Printer._ipp._tcp.local") ;; Live browsing — callbacks as services come and go (needs a running responder): (let* ((r (0conf:start-responder (0conf:make-responder))) (b (0conf:browse-services r "_ipp._tcp.local" :on-add (lambda (info) (format t "+ ~A~%" (0conf:service-instance-name info))) :on-remove (lambda (name) (format t "- ~A~%" name))))) ;; ... later ... (0conf:stop-browse b) (0conf:stop r)) ;; Advertise a service (:host defaults to this machine's .local name): (let* ((info (0conf:make-service-info :type "_http._tcp.local" :name "My Widget" :port 8080 :addresses (list (0conf:parse-ipv4 "192.168.1.42")) :txt '(("path" . "/status")))) (r (0conf:start :services (list info)))) ;; update the TXT later without re-registering: (0conf:update-service-txt r info '(("path" . "/status") ("load" . "0.4"))) ;; ... later ... (0conf:stop r))
Command-line tool
The 0conf/cli system builds a small standalone 0conf command:
scripts/build-cli.sh # build ./0conf (add --sign on macOS, see below) ./0conf browse # list the DNS-SD service types on the LAN ./0conf browse _airplay._tcp # list instances of a type (host / port / TXT) ./0conf monitor # live, self-updating view of all services (Ctrl-C to quit) ./0conf resolve "Front Desk._ipp._tcp.local" ./0conf publish _http._tcp "My Widget" 8080 --txt path=/status # advertise until Ctrl-C ./0conf interfaces # list usable network interfaces (pick a value for -i) ./0conf --version | help
Global options: -i/--interface <if> (pin the egress interface — a NIC name
like en0, or the raw form: a dotted-quad for IPv4, an interface index for
IPv6), --timeout <secs>, --json
(machine-readable output for every command), --color auto|always|never (also
honors NO_COLOR), and -6/--ipv6 / -4/--ipv4 for browse/resolve.
Exit codes: 0 found, 1 not-found, 2 usage error, 130 on Ctrl-C. When output is
piped or --json is set, no ANSI escapes are emitted.
./0conf browse --json | jq . # scriptable ./0conf -i en0 browse _ipp._tcp # pin the interface (or -i 192.168.1.42) ./0conf monitor --once --timeout 5 --json # one snapshot, then exit ./0conf monitor --type _googlecast._tcp # watch just one type
monitor starts a responder (one socket per interface, so it watches every link;
pin it with -i), keeps a live browser per type, and redraws a grouped dashboard
— with an age column — as services appear, change, and disappear, like dns-sd -B
/ avahi-browse -a. It browses both the types the DNS-SD meta-query enumerates
and a built-in list of well-known types (printers, Chromecast, HomeKit, SSH,
SMB, …), since some devices answer a direct browse but ignore the meta-query.
--once prints a single snapshot and exits; --for <secs> runs for a bounded
time; when piped it streams +/- add/remove lines instead of a redraw. (On a
client-isolated network — common at cafés — you'll still only see your own device;
that's the access point blocking peer traffic, not the tool.)
publish advertises a service (probing + conflict-renaming per RFC 6762) and
sends a goodbye when you stop it; --txt k=v (repeatable), --subtype s,
--host h, --no-probe.
browse with no argument runs the DNS-SD meta-query
(_services._dns-sd._udp.local, RFC 6763 §9)
to enumerate the service types on the link — the same thing dns-sd -B does.
On macOS a raw binary needs Local Network / multicast access to send mDNS:
scripts/build-cli.sh --sign ad-hoc-signs the executable with the
com.apple.developer.networking.multicast entitlement, and the first run prompts
for Local Network access.
Tutorial
doc/tutorial.org is a literate, tangle-able walkthrough of
the pure API (wire codec, service expansion, cache, reassembly, NFC names). It
tangles into a self-checking doc/tutorial.lisp:
emacs --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "doc/tutorial.org")' sbcl --non-interactive --load doc/tutorial.lisp # 15 checks, all self-asserting
CI tangles and runs it on every push, so the tutorial can't drift from working code.
Coverage
CI generates an sb-cover report each
run: a per-file table is posted to the run summary, the full HTML is uploaded as
the coverage-html artifact, and the numbers are pushed to
Coveralls. Reproduce locally:
sbcl --non-interactive --load scripts/coverage.lisp # writes coverage/cover-index.html python3 scripts/coverage-summary.py # prints the summary table
The library proper sits around ~87% of expressions (octets 98%, browser 94%,
records 93%, service-info 92%, transport 86%); the untested remainder is
mostly the live multicast send/join paths (no multicast fabric in CI) and pure
declaration files. Counting src/cli.lisp — barely tested, and mostly terminal
I/O — the whole tree is ~65%.
To post to Coveralls yourself you need cl-coveralls, which is deliberately
kept out of ocicl.csv (it pulls ~70 systems that only this path wants):
ocicl install cl-coveralls COVERALLS=1 sbcl --non-interactive --load scripts/coveralls.lisp
The badge reads lower than the table above it: Coveralls scores lines, while
sb-cover — and so coverage-summary.py — scores expressions, of which a
line can hold several.
Outside CI that is a dry run — it prints the JSON payload instead of uploading.
CI uploads from the Linux job, authenticating with the COVERALLS_REPO_TOKEN
repository secret; the step no-ops when the secret is unavailable, as on a pull
request from a fork.
Develop
Dependencies are managed with ocicl:
ocicl install # restore deps from ocicl.csv sbcl --eval '(asdf:test-system :0conf)' # 159 tests / 431 checks, pure + loopback
The test suite is mostly pure (codec / records / cache / DNS-SD), plus
loopback-socket integration tests (real send/receive, a full query→responder→
answer flow, browse-once, resolve), guarded socket-construction tests, and
interop fixtures — real mDNS packets captured from Apple's mDNSResponder,
decoded to prove we parse genuine Bonjour wire data (real name compression, the
QU bit, header flags), not just our own encoder's output.
Platform notes
- macOS 15+ multicast entitlement. Sending/receiving multicast requires the
com.apple.developer.networking.multicastentitlement on a signed binary. A rawsbclgetsEHOSTUNREACHon multicast send even though routing is fine and the entitled systemmDNSResponderworks. Ship 0conf inside a signed app bundle that declares the entitlement (andNSLocalNetworkUsageDescription) for live use; the codec/cache/DNS-SD layers are fully exercised without it. - VPN / multi-homed hosts. If the default route points at a tunnel (e.g.
Tailscale
utun*), pass:interface "192.168.x.y"to select the real LAN NIC for multicast egress (IP_MULTICAST_IF).
Design notes
- Reference model: architecture mirrors python-zeroconf; the wire codec follows mafintosh/dns-packet; Apple's mDNSResponder and the RFCs are the correctness oracle.
- Per-interface, dual-stack:
list-interfacesenumerates usable NICs viagetifaddrs(skipping loopback and point-to-point/VPN tunnels);start-responderopens one IPv4 and/or IPv6 socket per interface, joins the group on that specific interface, pins egress to it (IP_MULTICAST_IF), runs a listener per socket, answers on the socket a query arrived on, and broadcasts announcements out every interface — correct on multi-homed / VPN hosts. Best-effort: a failed join on one NIC never aborts the others, and it falls back to a single all-interfaces socket per family when enumeration yields nothing. - Character set & names: all text is UTF-8 (RFC 6762 §16 / RFC 6763), with
names normalized to Unicode NFC — on encode and decode — via SBCL's built-in
sb-unicode(no external dependency). Names use RFC 4343 presentation escaping, so a DNS-SD instance label containing a dot (My Printer 2.0) round-trips correctly. IPv6 parsing handles::compression and embedded-IPv4 (::ffff:1.2.3.4). - DNS-SD extras: service subtypes (
_printer._sub._http._tcp.local) via a:subtypeslist, and binary TXT values — a TXT value may be an octet vector, round-tripping as bytes rather than being forced through UTF-8. - Negative responses:
service-info-recordsemits NSEC records (instance name → SRV+TXT, host name → the address families present), and the responder also attaches them on demand (RFC 6762 §6.1): a positive answer carries the name's NSEC in Additional, and a query for a type we don't hold at a name we own is answered with the NSEC as a negative response. - Response timing & suppression: answers to all of a query's questions are aggregated into one response (§7.4); multicast responses are delayed a random 20–120ms (§6), extended when the query's TC bit signals more known-answers are coming (§7.2); known-answers spanning multiple packets are reassembled per source before suppression; and an answer a peer multicasts during our delay is dropped (duplicate-response suppression, §6). Cache-flush eviction spares records received within the last second so multi-packet responses aren't self-destructive (§10.2).
- Legacy unicast queries (§6.7): a query from a source port other than 5353 gets a unicast reply with the query id echoed, the question repeated, and TTLs capped at 10s.
- Probing & conflicts: the responder probes both the service instance name
and the host name, renaming on collision (
Foo→Foo (2),myhost.local→myhost-2.local) and re-probing (RFC 6762 §8.1, §9). Both cases are handled: a response means the name is already owned (unconditional conflict), and a simultaneous prober's query is resolved by lexicographic tiebreaking of the proposed record sets (§8.2) — we rename only if our data loses. - Conflicts after announcing (§9): the probe window is not the only time a peer can contradict us. Any response asserting different rdata at a name we hold a unique record for puts that name back into probing, on a separate resolver thread (probing sleeps for seconds; a listener must stay free to receive), and every service backed by the name is re-registered under whatever the probe settles on. Identical rdata is never a conflict, so neither our own looped-back multicast nor a proxy answering on our behalf makes us rename; a goodbye is a withdrawal rather than a competing claim; and the comparison is against the whole rrset, so a multi-homed host does not conflict with its own second address. Fifteen conflicts within ten seconds trips the §8.1 backstop — five seconds of wait before each further probe attempt.
- Message size (§17): every outgoing record set is split across as many packets as it needs, each kept under 1400 bytes (the RFC allows up to the MTU less IP+UDP headers, but advises staying under 1500). Because name compression makes a record's encoded length depend on what precedes it, each candidate packet is measured by encoding it rather than by summing per-record sizes — bisected, since appending a record can only lengthen the encoding. A single record too large to share a datagram travels alone, as §17 permits, and anything past the 9000-byte hard ceiling is dropped at the transport instead of being put on the wire; a probe that would be dropped signals instead, since a probe nobody sent looks exactly like a probe nobody answered. Additionals ride in whatever room is left after the answers rather than stranding themselves in a trailing packet with no answer to support (RFC 6763 §12). A query carrying a known-answer list splits the §7.2 way: the question rides in the first packet only, and every packet but the last sets the TC bit — the sending half of the multipacket known-answer suppression the responder already understood on receive.
- Live browsing:
browse-servicesattaches to a running responder and fires add/update/remove callbacks as instances appear, change, and vanish. Discovery uses a backing-off PTR query (§5.2) with known-answer suppression, plus TTL-refresh queries once a tracked record passes 80% of its lifetime so long-watched services don't spuriously expire; the current set is recomputed from the cache on a short poll and diffed. A background sweeper expires stale cache entries so removals happen on time. Cross-thread cache access is guarded by the responder lock. - Interfaces are re-enumerated, not fixed at startup: every few seconds the
responder diffs the sockets it holds against the interfaces that are actually
there, opening one for each new link and retiring the ones whose link is gone —
so joining a Wi-Fi network, a VPN coming up, or docking a laptop is picked up
without a restart. A NIC that keeps its name but takes a new address (a DHCP
renewal) counts as a new link, since that is what
IP_MULTICAST_IFis pinned to. A new link is a startup as far as our names go (§8.1), so the services are put back through probing on it and re-announced. Two safeguards: an enumeration that comes back empty means "no information", not "no interfaces", and if we wanted new sockets but could not open a single one, the close list is ignored — neither a transientgetifaddrsfailure nor a link whose joins are all failing can leave the responder deaf. - Which link did this arrive on? Every socket binds
INADDR_ANY:5353withSO_REUSEPORT, so the kernel is free to hand a multicast datagram to any socket in the reuse group — not necessarily the one that joined the group on the arrival interface. The socket a packet was read from therefore does not identify the link, and replying on it can put the answer on the wrong one. So the sockets ask forIP_PKTINFO/IPV6_PKTINFOand receive viarecvmsg, which attaches the receiving interface index; the reply goes out the socket pinned to that link, falling back to the arrival socket when the kernel does not say (index 0, the API's "unspecified", counts as not saying). Thecmsghdrlayout differs between Darwin and Linux —cmsg_lenis 4 octets on one and 8 on the other, moving the level and type fields with it — so the layout is kept as data and the walk over the ancillary data is a pure function, exercised against both platforms' shapes wherever the suite runs. - A cap on sockets: a host with very many interfaces would otherwise get a
socket and a listener thread per NIC per family;
*max-interface-sockets*(32) bounds it, and reports once rather than dropping links silently. - Remaining follow-ups:
format-ipv6writes every group out (fe80:0:0:0:0:0:0:1) rather than the RFC 5952::form — only cosmetic, asparse-ipv6reads either.