You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b5a8b63
Browse filesBrowse the repository at this point in the historyBrowse files
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]>
Copy file name to clipboardExpand all lines: CHANGES.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# HAP-Java 2.0.10
2
2
* 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)
7
7
8
8
# HAP-Java 2.0.9
9
9
* Fix Set Duration and Remaining Duration characteristics to advertise format `uint32` instead of `int`[#202](https://github.com/hap-java/HAP-Java/issues/202)
Copy file name to clipboardExpand all lines: README.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,23 +25,21 @@ Include HAP-Java in your project using maven:
25
25
After that, check out the [Sample](https://github.com/hap-java/HAP-Java/tree/sample) and
26
26
read the [Javadoc](https://hap-java.github.io/HAP-Java/apidocs/index.html)
27
27
28
-
Native DNS-SD / Bonjour (macOS)
28
+
HomeKit mDNS advertising
29
29
=========
30
30
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.
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.
6
6
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**:
8
8
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.
10
12
11
-
## Solution
13
+
This work therefore:
12
14
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.
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.
| 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.
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 |
16
82
| --- | --- | --- | --- |
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 |
19
87
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.
21
89
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).
23
91
24
92
## Quick start
25
93
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)
| 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
+
62
146
## Verifying on macOS (manual)
63
147
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:
65
149
66
150
```bash
67
151
dns-sd -B _hap._tcp local.
68
152
dns-sd -L "<instance name>" _hap._tcp local.
69
-
# or
70
153
dns-sd -Z _hap._tcp local.
71
154
```
72
155
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.
|`nativeDnssd(InetAddress)`| Native Bonjour on that address’s interface index; SRV host = system default (`null`) |
95
178
|`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|
97
180
98
181
### `HomekitServer` constructors
99
182
@@ -104,14 +187,14 @@ new HomekitServer(InetAddress localAddress, int port, int nThreads, HomekitAdver
104
187
105
188
When an advertiser is supplied, `createBridge` / `createStandaloneAccessory` use it and do **not** open a private JmDNS socket.
106
189
107
-
## Implementation notes
190
+
## Native DNS-SD implementation notes
108
191
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.
113
196
* 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.
115
198
116
199
## Tests
117
200
@@ -129,11 +212,13 @@ mvn test
129
212
130
213
| Symptom | Likely cause |
131
214
| --- | --- |
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 |
136
221
137
222
## License
138
223
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