|
| 1 | +# Native DNS-SD / Bonjour advertiser |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +HAP-Java historically advertises `_hap._tcp` with **JmDNS**, which opens a multicast UDP socket on **port 5353**. |
| 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. |
| 8 | + |
| 9 | +Typical symptoms on macOS: empty `dns-sd -B _hap._tcp` results while `lsof -iUDP:5353` only lists `mDNSResponder`. |
| 10 | + |
| 11 | +## Solution |
| 12 | + |
| 13 | +This fork adds a pluggable **`HomekitAdvertiser`** and a native implementation **`DnssdHomekitAdvertiser`** that calls Apple’s DNS-SD API (`DNSServiceRegister`) via JNA: |
| 14 | + |
| 15 | +| Implementation | Class | Binds UDP 5353? | Typical platform | |
| 16 | +| --- | --- | --- | --- | |
| 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` | |
| 19 | + |
| 20 | +TXT records (`sf`, `id`, `md`, `sh`, `c#`, `s#`, `ff`, `ci`, `pv`) are shared via `HapDnsTxt` so both advertisers publish the same HAP payload. |
| 21 | + |
| 22 | +## Quick start |
| 23 | + |
| 24 | +```java |
| 25 | +import io.github.hapjava.server.HomekitAdvertisers; |
| 26 | +import io.github.hapjava.server.impl.HomekitServer; |
| 27 | +import java.net.InetAddress; |
| 28 | + |
| 29 | +InetAddress bind = InetAddress.getByName("192.168.0.6"); |
| 30 | +HomekitServer server = |
| 31 | + new HomekitServer(bind, 9124, HomekitAdvertisers.nativeDnssd()); |
| 32 | + |
| 33 | +// or: prefer native when present, else JmDNS |
| 34 | +HomekitServer auto = |
| 35 | + new HomekitServer(bind, 9124, HomekitAdvertisers.auto(bind)); |
| 36 | +``` |
| 37 | + |
| 38 | +Check availability: |
| 39 | + |
| 40 | +```java |
| 41 | +if (HomekitAdvertisers.isNativeDnssdSupported()) { |
| 42 | + // safe to call nativeDnssd() |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Existing constructors that take only `InetAddress` / `JmDNS` keep the previous JmDNS behaviour (backward compatible). |
| 47 | + |
| 48 | +## Verifying on macOS |
| 49 | + |
| 50 | +After starting a server with `HomekitAdvertisers.nativeDnssd()` (or `auto(...)`): |
| 51 | + |
| 52 | +```bash |
| 53 | +dns-sd -B _hap._tcp local. |
| 54 | +# or |
| 55 | +dns-sd -Z _hap._tcp local. |
| 56 | +``` |
| 57 | + |
| 58 | +You should see the bridge/accessory instance. No Java process should be listening on UDP 5353 (`lsof -iUDP:5353` should only show `mDNSResponder`). |
| 59 | + |
| 60 | +## API surface |
| 61 | + |
| 62 | +### `io.github.hapjava.server.HomekitAdvertiser` |
| 63 | + |
| 64 | +```text |
| 65 | +advertise(label, category, mac, port, configurationIndex, setupId) |
| 66 | +stop() |
| 67 | +setDiscoverable(boolean) |
| 68 | +setMac(String) |
| 69 | +setConfigurationIndex(int) |
| 70 | +``` |
| 71 | + |
| 72 | +### `io.github.hapjava.server.HomekitAdvertisers` |
| 73 | + |
| 74 | +| Factory | Behaviour | |
| 75 | +| --- | --- | |
| 76 | +| `jmdns(JmDNS)` | Wrap an existing JmDNS | |
| 77 | +| `jmdns(InetAddress)` | Create JmDNS on that address | |
| 78 | +| `nativeDnssd()` | Native Bonjour / dns_sd (throws if unsupported) | |
| 79 | +| `auto(InetAddress)` | Native if supported, else JmDNS | |
| 80 | +| `isNativeDnssdSupported()` | Probe without constructing | |
| 81 | + |
| 82 | +### `HomekitServer` constructors |
| 83 | + |
| 84 | +```java |
| 85 | +new HomekitServer(InetAddress localAddress, int port, HomekitAdvertiser advertiser) |
| 86 | +new HomekitServer(InetAddress localAddress, int port, int nThreads, HomekitAdvertiser advertiser) |
| 87 | +``` |
| 88 | + |
| 89 | +When an advertiser is supplied, `createBridge` / `createStandaloneAccessory` use it and do **not** open a private JmDNS socket. |
| 90 | + |
| 91 | +## Implementation notes |
| 92 | + |
| 93 | +* **JNA** loads `System` on macOS/Darwin and `dns_sd` elsewhere (Avahi Bonjour compatibility). |
| 94 | +* `DNSServiceRegister` receives the TCP port in **network byte order**. |
| 95 | +* TXT is RFC 6763 length-prefixed `key=value` bytes (`HapDnsTxt.encodeTxt`). |
| 96 | +* A daemon thread runs `DNSServiceProcessResult` until `DNSServiceRefDeallocate` on `stop()` / re-register. |
| 97 | +* Re-registration (discoverability, MAC, `c#`) deallocates and registers again — same pattern as the JmDNS advertiser. |
| 98 | + |
| 99 | +## Tests |
| 100 | + |
| 101 | +| Test | Purpose | |
| 102 | +| --- | --- | |
| 103 | +| `HapDnsTxtTest` | Required TXT keys + wire encoding | |
| 104 | +| `DnssdHomekitAdvertiserTest` | Mocked JNA register / re-register / stop / port endianness | |
| 105 | +| `JmdnsHomekitAdvertiserTest` | Existing JmDNS behaviour (unchanged) | |
| 106 | +| `HomekitAdvertisersTest` | Factory / support probe | |
| 107 | +| `DnssdLiveAdvertiserIT` | Real register+stop when native DNS-SD is present | |
| 108 | + |
| 109 | +```bash |
| 110 | +mvn test |
| 111 | +``` |
| 112 | + |
| 113 | +## Troubleshooting |
| 114 | + |
| 115 | +| Symptom | Likely cause | |
| 116 | +| --- | --- | |
| 117 | +| `Address already in use` / “no suitable mDNS” | Still using JmDNS against mDNSResponder — switch to `nativeDnssd()` | |
| 118 | +| `UnsupportedOperationException: Native DNS-SD` | No `libSystem`/`libdns_sd` on this JVM/OS | |
| 119 | +| `DNSServiceRegister failed with error code …` | Bad name/type, permissions, or daemon unavailable — check Console / `mDNSResponder` | |
| 120 | +| Browse empty after register | Wrong interface, firewall, or advertiser stopped; confirm with `dns-sd -B _hap._tcp` | |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +Same MIT license as HAP-Java. Native symbols are provided by the host OS; this library only calls them through JNA. |
0 commit comments