Skip to content

Commit b5a8b63

Browse files
kgoderiscursoragent
andcommitted
Docs: reframe advertiser SPI around shared/native mDNS, not port lock.
Update NATIVE_DNSSD.md, README, and javadocs for the HAP vs openHAB split: HomekitAdvertiser is the inject point; native DNS-SD is optional optimization; JmDNS 3.6.4 was a temporary false lead for hard conflicts. Co-authored-by: Cursor <[email protected]>
1 parent 465274d commit b5a8b63

5 files changed

Lines changed: 150 additions & 65 deletions

File tree

‎CHANGES.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# HAP-Java 2.0.10
22
* Add optional Mute characteristic to Doorbell service
3-
* Add pluggable `HomekitAdvertiser` with native DNS-SD / Bonjour implementation (`DnssdHomekitAdvertiser`) so macOS can advertise `_hap._tcp` without binding UDP 5353 [#212](https://github.com/hap-java/HAP-Java/issues/212) (see [docs/NATIVE_DNSSD.md](docs/NATIVE_DNSSD.md))
4-
* Extract shared HAP TXT helpers (`HapDnsTxt`) with stable key order; JmDNS and native advertisers publish the same property set
5-
* JNA (`net.java.dev.jna:jna`) is **optional** for `nativeDnssd()`; JmDNS-only consumers do not need it. Applications should choose `jmdns(...)` or `nativeDnssd(InetAddress)` explicitly (`auto(...)` is deprecated)
6-
* `nativeDnssd(InetAddress)` scopes DNS-SD by interface index only and keeps SRV `host` null (system hostname). Passing an IP literal as host broke pairing (SRV target with no A record)
3+
* Add pluggable `HomekitAdvertiser` SPI plus JmDNS and optional native DNS-SD / Bonjour (`DnssdHomekitAdvertiser`) backends so embeds can share host mDNS or avoid a second multicast stack [#212](https://github.com/hap-java/HAP-Java/issues/212) (see [docs/NATIVE_DNSSD.md](docs/NATIVE_DNSSD.md))
4+
* Extract shared HAP TXT helpers (`HapDnsTxt`) with stable key order; all advertisers publish the same property set
5+
* JNA (`net.java.dev.jna:jna`) is **optional** for `nativeDnssd()`; JmDNS-only consumers do not need it. Applications choose `jmdns(...)` or `nativeDnssd(...)` explicitly (`auto(...)` is deprecated)
6+
* `nativeDnssd(InetAddress)` scopes by interface index only and keeps SRV `host` null (system hostname). An IP literal as host broke pairing (SRV target with no A record)
77

88
# HAP-Java 2.0.9
99
* Fix Set Duration and Remaining Duration characteristics to advertise format `uint32` instead of `int` [#202](https://github.com/hap-java/HAP-Java/issues/202)

‎README.md‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@ Include HAP-Java in your project using maven:
2525
After that, check out the [Sample](https://github.com/hap-java/HAP-Java/tree/sample) and
2626
read the [Javadoc](https://hap-java.github.io/HAP-Java/apidocs/index.html)
2727

28-
Native DNS-SD / Bonjour (macOS)
28+
HomeKit mDNS advertising
2929
=========
3030

31-
JmDNS binds UDP **5353**. On macOS that port is owned by `mDNSResponder`, so dedicated JmDNS advertising can fail with “Address already in use” and Home never sees `_hap._tcp`.
32-
33-
This build adds a native advertiser that registers through Bonjour without binding 5353. Choose the advertiser explicitly in your application (e.g. openHAB); declare optional JNA when using the native path:
31+
HAP-Java publishes `_hap._tcp` through a pluggable `HomekitAdvertiser`: share a host **JmDNS**, use a dedicated JmDNS, or optionally register via **native DNS-SD** (Bonjour / Avahi) so the process does not run a second multicast stack. Prefer sharing the host’s mDNS when one exists (e.g. openHAB); native support is an optimization for clean system integration, not a workaround for a permanent port lock.
3432

3533
```java
3634
import io.github.hapjava.server.HomekitAdvertisers;
3735
import io.github.hapjava.server.impl.HomekitServer;
3836

39-
// Scope native registration to the HAP bind address, or use jmdns(sharedJmDNS) to force JmDNS:
37+
// Share host JmDNS, or nativeDnssd(bindAddress) for system DNS-SD (optional JNA):
4038
HomekitServer server =
41-
new HomekitServer(bindAddress, 9124, HomekitAdvertisers.nativeDnssd(bindAddress));
39+
new HomekitServer(bindAddress, 9124, HomekitAdvertisers.jmdns(sharedJmDNS));
4240
```
4341

44-
Full details: [docs/NATIVE_DNSSD.md](docs/NATIVE_DNSSD.md).
42+
Architecture, openHAB split, and native notes: [docs/NATIVE_DNSSD.md](docs/NATIVE_DNSSD.md).
4543

4644
Supported HomeKit Accessories
4745
=========

‎docs/NATIVE_DNSSD.md‎

Lines changed: 128 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,113 @@
1-
# Native DNS-SD / Bonjour advertiser
1+
# HomeKit advertising backends (JmDNS & native DNS-SD)
22

3-
## Problem
3+
## Motivation
44

5-
HAP-Java historically advertises `_hap._tcp` with **JmDNS**, which opens a multicast UDP socket on **port 5353**.
5+
HAP-Java must publish `_hap._tcp` so Home / controllers can find the accessory. Historically that was done with a **dedicated JmDNS** instance, which opens its own multicast UDP stack on port 5353.
66

7-
On **macOS**, Apple’s `mDNSResponder` already owns `*:5353`. A second process (or a JVM) that tries to bind 5353 gets `Address already in use`, and Home / Controllers never see the accessory — even when the HAP TCP port (e.g. 9124) is listening.
7+
That works on many hosts, but it is often **suboptimal**:
88

9-
Typical symptoms on macOS: empty `dns-sd -B _hap._tcp` results while `lsof -iUDP:5353` only lists `mDNSResponder`.
9+
* The OS already runs an mDNS responder (**mDNSResponder** on macOS, **Avahi** on many Linux systems).
10+
* A second, in-process multicast stack duplicates work, competes for the same LAN traffic, and is harder to reason about when the host (or another library) already owns discovery.
11+
* Embedders such as **openHAB** already maintain a **shared** mDNS service for the whole runtime. HomeKit should reuse that policy rather than always spawning a private JmDNS.
1012

11-
## Solution
13+
This work therefore:
1214

13-
This library adds a pluggable **`HomekitAdvertiser`** and a native implementation **`DnssdHomekitAdvertiser`** that calls Apple’s DNS-SD API (`DNSServiceRegister`) via JNA:
15+
1. Introduces a pluggable **`HomekitAdvertiser`** SPI so any backend can publish HAP DNS-SD records.
16+
2. Ships a **JmDNS** backend (default / classic) and an optional **native DNS-SD** backend (`DNSServiceRegister` via JNA) for standalone embeds that want to talk to the system daemon directly.
17+
3. Leaves **platform policy** (which backend, shared vs dedicated) to the application — especially openHAB.
1418

15-
| Implementation | Class | Binds UDP 5353? | Typical platform |
19+
### What this is *not*
20+
21+
Hard “UDP 5353 is exclusively locked forever on macOS” is **not** the durable motivation. mDNSResponder typically listens with `SO_REUSEPORT`; a correct JmDNS can share the port. A temporary **JmDNS 3.6.4** regression in openHAB snapshots could fail to bind and looked like a permanent conflict; **3.6.5** and other releases behave normally. Prefer framing native / shared advertising as **optimization and clean host integration**, not as the only way to avoid an unfixable port clash.
22+
23+
## Architecture split
24+
25+
| Concern | Belongs in |
26+
| --- | --- |
27+
| HAP advertise lifecycle (`advertise` / `stop` / TXT updates for `_hap._tcp`) | **HAP-Java** (`HomekitAdvertiser`) |
28+
| Default backends: JmDNS; optional Bonjour / dns_sd (Avahi compat) | **HAP-Java** (`HomekitAdvertisers`, implementations) |
29+
| Shared mDNS for *all* bindings, JmDNS version, future native Bonjour for the runtime | **openHAB core** (`MDNSService` / equivalent) |
30+
| Choosing shared OH mDNS vs dedicated JmDNS vs system DNS-SD for HomeKit | **openHAB `io.homekit`** (config / wiring) |
31+
32+
### Why the split
33+
34+
* **openHAB** already centralizes mDNS. If the runtime grows a native Bonjour/Avahi backend, every binding benefits — HomeKit should not be the only place that invents that.
35+
* **HAP-Java** is also used **outside** openHAB. It still needs a way to advertise when no host mDNS service exists, plus a way for the host to **inject** whatever it does provide.
36+
* Native DNS-SD **inside HAP** is therefore an optional convenience for standalone / non-OH embeds, not the place where openHAB solves fleet-wide mDNS on macOS.
37+
38+
```text
39+
┌─────────────────────────────────────┐
40+
│ Application │
41+
│ (openHAB io.homekit, other embeds) │
42+
│ picks / implements HomekitAdvertiser│
43+
└─────────────────┬───────────────────┘
44+
│ injects
45+
▼
46+
┌──────────────────────┐ ┌─────────────────────────────┐
47+
│ openHAB core mDNS │ │ HAP-Java HomekitServer │
48+
│ (shared JmDNS today; │───▶│ uses HomekitAdvertiser SPI │
49+
│ native later?) │ └─────────────────────────────┘
50+
└──────────────────────┘
51+
▲
52+
HAP-Java backends │ optional defaults
53+
┌───────────────────────────┴───────────────────────────┐
54+
│ JmdnsHomekitAdvertiser DnssdHomekitAdvertiser │
55+
│ (javax.jmdns) (JNA → dns_sd / Bonjour) │
56+
└───────────────────────────────────────────────────────┘
57+
```
58+
59+
## Is `HomekitAdvertiser` enough for a non-JmDNS openHAB backend?
60+
61+
**Yes.** The SPI is intentionally backend-agnostic:
62+
63+
```text
64+
advertise(label, category, mac, port, configurationIndex, setupId)
65+
stop()
66+
setDiscoverable(boolean)
67+
setMac(String)
68+
setConfigurationIndex(int)
69+
```
70+
71+
It does **not** expose `JmDNS`, sockets, or DNS-SD handles. openHAB can:
72+
73+
1. Keep using `HomekitAdvertisers.jmdns(sharedJmDNS)` when core still speaks JmDNS.
74+
2. Implement `HomekitAdvertiser` in `io.homekit` (or core) as a thin wrapper around a future native / Avahi / core API that is **not** JmDNS.
75+
3. Optionally call HAP’s `nativeDnssd(...)` only for experiments or when core does not yet provide a native path — without making that the long-term OH design.
76+
77+
So HAP-Java’s contract is: **accept an interface for whatever mDNS backend you want**. Backends *shipped here* are JmDNS and native DNS-SD (Bonjour / Avahi `libdns_sd`). Backends *shipped by the host* can wrap the same or different mechanisms.
78+
79+
## Backends
80+
81+
| Backend | Class / factory | UDP 5353 in-process? | Role |
1682
| --- | --- | --- | --- |
17-
| JmDNS (default / classic) | `JmdnsHomekitAdvertiser` | Yes (unless sharing an existing `JmDNS`) | Linux containers, Windows |
18-
| Native DNS-SD | `DnssdHomekitAdvertiser` | **No** — registers through the system daemon | **macOS** (mDNSResponder), Linux with Avahi `libdns_sd` |
83+
| JmDNS (shared instance) | `HomekitAdvertisers.jmdns(JmDNS)` | Reuses caller’s socket | Preferred when the host already has JmDNS (e.g. openHAB `useOHmDNS`) |
84+
| JmDNS (dedicated) | `HomekitAdvertisers.jmdns(InetAddress)` or classic `HomekitServer` ctors | Yes — own multicast stack | Simple standalone default |
85+
| Native DNS-SD | `HomekitAdvertisers.nativeDnssd()` / `nativeDnssd(InetAddress)` | No — registers via system daemon | Optional; macOS Bonjour / Avahi dns_sd compat |
86+
| Host-provided | Application implements `HomekitAdvertiser` | Whatever the host does | openHAB (or others) wrap core mDNS |
1987

20-
TXT records (`sf`, `id`, `md`, `sh`, `c#`, `s#`, `ff`, `ci`, `pv`) are shared via `HapDnsTxt` so both advertisers publish the same HAP payload.
88+
TXT records (`sf`, `id`, `md`, `sh`, `c#`, `s#`, `ff`, `ci`, `pv`) are built once via `HapDnsTxt` so every backend publishes the same HAP payload.
2189

22-
**Policy belongs in the application** (e.g. openHAB): choose `jmdns(...)` or `nativeDnssd(...)` explicitly. Do not rely on HAP-Java to auto-pick.
90+
**Policy belongs in the application.** Do not rely on HAP-Java to auto-pick (`auto(...)` is deprecated).
2391

2492
## Quick start
2593

26-
Declare the optional JNA dependency if you use the native advertiser (JmDNS-only consumers can omit it):
94+
### Share an existing JmDNS (typical openHAB path)
95+
96+
```java
97+
HomekitServer server =
98+
new HomekitServer(bind, 9124, HomekitAdvertisers.jmdns(sharedJmDNS));
99+
```
100+
101+
### Dedicated JmDNS (standalone default)
102+
103+
```java
104+
HomekitServer server = new HomekitServer(bind, 9124); // classic behaviour
105+
// or: HomekitAdvertisers.jmdns(bind)
106+
```
107+
108+
### Optional native DNS-SD (standalone / explicit)
109+
110+
Declare optional JNA if you use the native advertiser (JmDNS-only consumers can omit it):
27111

28112
```xml
29113
<dependency>
@@ -34,43 +118,42 @@ Declare the optional JNA dependency if you use the native advertiser (JmDNS-only
34118
```
35119

36120
```java
37-
import io.github.hapjava.server.HomekitAdvertisers;
38-
import io.github.hapjava.server.impl.HomekitServer;
39-
import java.net.InetAddress;
40-
41-
InetAddress bind = InetAddress.getByName("192.168.0.6");
42-
43-
// Prefer scoping native registration to the HAP bind interface (SRV host stays system default):
121+
// Scope by bind interface; SRV host stays null (system hostname + A/AAAA).
44122
HomekitServer server =
45123
new HomekitServer(bind, 9124, HomekitAdvertisers.nativeDnssd(bind));
46-
47-
// Or share an existing JmDNS (e.g. openHAB's):
48-
// HomekitServer server =
49-
// new HomekitServer(bind, 9124, HomekitAdvertisers.jmdns(sharedJmDNS));
50124
```
51125

52-
Check whether the native library **loads** (does not prove registration will succeed on the LAN):
53-
54126
```java
55127
if (HomekitAdvertisers.isNativeDnssdSupported()) {
56-
// safe to construct nativeDnssd(...)
128+
// JNA + dns_sd loaded — not a guarantee that registration will succeed on the LAN
57129
}
58130
```
59131

60132
Existing constructors that take only `InetAddress` / `JmDNS` keep the previous JmDNS behaviour (backward compatible).
61133

134+
## openHAB wiring (target shape)
135+
136+
Suggested choices in `io.homekit` (names illustrative):
137+
138+
| User / config intent | Implementation |
139+
| --- | --- |
140+
| Use openHAB’s shared mDNS | `HomekitAdvertisers.jmdns(ohJmDNS)` or wrap core API as `HomekitAdvertiser` |
141+
| Dedicated in-process JmDNS | `HomekitAdvertisers.jmdns(localAddress)` |
142+
| System mDNS (Bonjour / Avahi) | Prefer **core-provided** advertiser when it exists; until then optionally `nativeDnssd(bind)` as a stopgap |
143+
144+
Fleet-wide native Bonjour for every binding remains an **openHAB core** enhancement, not a HomeKit-only fork of DNS-SD.
145+
62146
## Verifying on macOS (manual)
63147

64-
There is no automated live IT in CI (ubuntu runners lack `libdns_sd` / Bonjour). After starting a server with `HomekitAdvertisers.nativeDnssd(bind)`:
148+
There is no automated live IT in CI (ubuntu runners lack Bonjour). After starting with a chosen advertiser:
65149

66150
```bash
67151
dns-sd -B _hap._tcp local.
68152
dns-sd -L "<instance name>" _hap._tcp local.
69-
# or
70153
dns-sd -Z _hap._tcp local.
71154
```
72155

73-
You should see the bridge/accessory instance and HAP TXT keys. No Java process should be listening on UDP 5353 (`lsof -iUDP:5353` should only show `mDNSResponder`).
156+
With the **native** backend, the Java process should not hold a UDP 5353 socket (`lsof -iUDP:5353` typically only shows `mDNSResponder`). With **shared JmDNS**, expect the host process to share 5353 via reuse — that is normal.
74157

75158
## API surface
76159

@@ -88,12 +171,12 @@ setConfigurationIndex(int)
88171

89172
| Factory | Behaviour |
90173
| --- | --- |
91-
| `jmdns(JmDNS)` | Wrap an existing JmDNS (force JmDNS / share openHAB’s instance) |
174+
| `jmdns(JmDNS)` | Wrap an existing JmDNS (share host instance) |
92175
| `jmdns(InetAddress)` | Create dedicated JmDNS on that address |
93176
| `nativeDnssd()` | Native Bonjour on **all** interfaces (`interfaceIndex = 0`, default host) |
94177
| `nativeDnssd(InetAddress)` | Native Bonjour on that address’s interface index; SRV host = system default (`null`) |
95178
| `isNativeDnssdSupported()` | Whether JNA + dns_sd **load** (not a live register probe) |
96-
| `auto(InetAddress)` | **Deprecated** — prefer an explicit `jmdns` / `nativeDnssd` choice |
179+
| `auto(InetAddress)` | **Deprecated** — application must choose |
97180

98181
### `HomekitServer` constructors
99182

@@ -104,14 +187,14 @@ new HomekitServer(InetAddress localAddress, int port, int nThreads, HomekitAdver
104187

105188
When an advertiser is supplied, `createBridge` / `createStandaloneAccessory` use it and do **not** open a private JmDNS socket.
106189

107-
## Implementation notes
190+
## Native DNS-SD implementation notes
108191

109-
* **JNA** is an **optional** dependency. It loads `System` on macOS/Darwin and `dns_sd` elsewhere (Avahi Bonjour compatibility). Missing JNA surfaces as `LinkageError` and `isNativeDnssdSupported()` returns `false`.
110-
* `DNSServiceRegister` receives the TCP port in **network byte order**.
111-
* Default `nativeDnssd()` uses `interfaceIndex = 0` (all interfaces) and `host = null` (system hostname). Prefer `nativeDnssd(InetAddress)` to limit the interface index when the HAP TCP listener is bound to one LAN address — still with `host = null`. Never pass a raw IP as DNS-SD `host` (no A record → Home hangs on “Setting up”).
112-
* TXT is RFC 6763 length-prefixed `key=value` bytes (`HapDnsTxt.encodeTxt`). Key insertion order is stable (`LinkedHashMap`); RFC 6763 does not require a particular pair order.
192+
* **JNA** is **optional**. Loads `System` on macOS/Darwin and `dns_sd` elsewhere (Avahi Bonjour compatibility). Missing JNA → `LinkageError`; `isNativeDnssdSupported()` returns `false`.
193+
* `DNSServiceRegister` expects the TCP port in **network byte order**.
194+
* `nativeDnssd(InetAddress)` only sets **interface index**; **`host` stays `null`**. Never pass a raw IP as DNS-SD `host` — the daemon does not synthesize an A record, and Home hangs on “Setting up” after accepting the QR code.
195+
* TXT is RFC 6763 length-prefixed `key=value` (`HapDnsTxt.encodeTxt`). Key insertion order is stable; RFC 6763 does not require a particular pair order.
113196
* A daemon thread runs `DNSServiceProcessResult` until `DNSServiceRefDeallocate` on `stop()` / re-register.
114-
* Re-registration (discoverability, MAC, `c#`) deallocates and registers again — same pattern as the JmDNS advertiser.
197+
* Re-registration (discoverability, MAC, `c#`) matches the JmDNS advertiser pattern.
115198

116199
## Tests
117200

@@ -129,11 +212,13 @@ mvn test
129212

130213
| Symptom | Likely cause |
131214
| --- | --- |
132-
| `Address already in use` / “no suitable mDNS” | Still using dedicated JmDNS against mDNSResponder — switch to `nativeDnssd(bind)` or share an existing JmDNS |
133-
| `UnsupportedOperationException: Native DNS-SD` | No JNA on classpath, or no `libSystem`/`libdns_sd` on this JVM/OS |
134-
| `DNSServiceRegister failed with error code …` | Bad name/type, permissions, or daemon unavailable — check Console / `mDNSResponder` |
135-
| Browse empty after register / too many interfaces | Prefer `nativeDnssd(localAddress)` instead of all-interfaces `nativeDnssd()` |
215+
| Extra multicast / busy 5353 / confusing dual stacks | Dedicated JmDNS while the OS or host already runs mDNS — share host JmDNS or use native / host advertiser |
216+
| `Address already in use` on `JmDNS.create` | Often a JmDNS/OS edge case (e.g. historical 3.6.4); try sharing host mDNS, upgrading JmDNS, or native DNS-SD for standalone |
217+
| `UnsupportedOperationException: Native DNS-SD` | No JNA on classpath, or no `libSystem` / `libdns_sd` |
218+
| `DNSServiceRegister failed with error code …` | Bad name/type, permissions, or daemon unavailable |
219+
| QR accepted, Home hangs on “Setting up” | SRV `host` set to a raw IP (no A record) — use `host = null` / `nativeDnssd(InetAddress)` as documented |
220+
| Browse empty / too many interfaces | Prefer `nativeDnssd(localAddress)` to scope interface index |
136221

137222
## License
138223

139-
Same MIT license as HAP-Java. Native symbols are provided by the host OS; this library only calls them through JNA.
224+
Same MIT license as HAP-Java. Native symbols are provided by the host OS; this library only calls them through JNA when the optional native backend is used.

0 commit comments

Comments
 (0)