ci / web (pull_request) Successful in 1m4s
apple / swift (pull_request) Successful in 1m33s
apple / screenshots (pull_request) Skipped
ci / rust-arm64 (pull_request) Successful in 1m32s
ci / docs-site (pull_request) Successful in 4m16s
android / android (pull_request) Successful in 6m25s
windows / build (x86_64-pc-windows-msvc) (pull_request) Failing after 7m14s
windows / build (aarch64-pc-windows-msvc) (pull_request) Failing after 3m30s
ci / rust (pull_request) Successful in 15m13s
A field report from an iPad: the host is not found on first run, and
restarting the client finds it. Pull-to-refresh appeared to do nothing.
Both were real. The Apple client's discovery had three ways to go
permanently deaf, each needing an app relaunch to clear:
- A failed resolve was never retried. `browseResultsChangedHandler`
only fires when the result SET changes, and a host whose resolve
failed is still in the set — so nothing ever re-offered it.
- A stuck resolve never ended. `NWConnection` has no timeout, so the
throwaway UDP flow used to resolve an address could sit in
`.preparing`/`.waiting` forever, and a service with a connection in
flight was skipped.
- `NWBrowser` parking in `.waiting` was ignored (only `.failed`
re-armed). On iOS that is where the local-network privacy prompt
lands on first launch after install: the browse starts, the system
asks, and the browser waits. Granting does not revive that browser —
only a new one sees the grant. That is the reported first-run bug.
HostDiscovery now runs a 1 Hz sweep that times out stuck resolves,
retries failed ones on a 1→30 s backoff, and re-arms a browser that
stopped working; the advert's TXT is re-read on every browse report, so
a host that re-keys or flips its pairing policy is followed. Returning
to the foreground re-arms the browse (iOS/tvOS: `onAppear` does not
fire across background/foreground, and a suspended browse stays dead).
Pull-to-refresh did nothing because there was no `.refreshable` in the
client at all. Added, plus the explicit control the report asked for:
a toolbar Refresh on iOS/macOS, an action-row button on tvOS, a Rescan
tile in the gamepad launcher, Scan Again on the empty state, a
header-bar button in the GTK client, a hosts-page button on Windows,
and Scan again on Android. Decky already had one.
The desktop/Android browses needed a rescan trigger to make those
buttons mean anything: mdns-sd re-queries on a doubling backoff capped
at ONE HOUR, so a long-lived browse is effectively passive and a host
that appears later can stay invisible. `discovery::Rescan` forces a
fresh query; the wake-and-wait loops use it too, so a host that just
booted is noticed in seconds rather than at the next backoff tick.
Also fixed, found on the way: clients/windows/src/discovery.rs is a
second copy of the browse that d0fa8bd3 ("pin mDNS discovery to IPv4 on
every client") missed. It took an arbitrary first address, so when a
host's OS responder answered AAAA the Windows GUI rendered a card that
failed on every click. It also never noticed a dropped receiver, leaking
a thread and a :5353 socket per wake-and-wait.
Gates: Apple macOS + iOS (arm64-apple-ios17.0, proven non-vacuous) build
clean, 195 tests pass incl. a new one asserting a rescan re-finds a
still-advertising host. On .21: fmt, clippy --all-targets -D warnings
and build clean for pf-client-core + client-linux + client-session,
117 tests pass. Android :kit: and :app: compileDebugKotlin clean.
The Windows client is UNGATED — its CI runner was unreachable.
138 lines
6.5 KiB
Rust
138 lines
6.5 KiB
Rust
//! LAN host discovery: browse the host's mDNS advert (`_punktfunk._udp`, TXT keys
|
|
//! `fp`/`pair`/`id` — see the host crate's `discovery.rs`) on a worker thread and stream
|
|
//! results to the UI. Ported verbatim from the GTK client (`mdns-sd` is cross-platform).
|
|
|
|
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
/// DNS-SD service type punktfunk hosts advertise (host side: `punktfunk_host::discovery`).
|
|
const SERVICE_TYPE: &str = "_punktfunk._udp.local.";
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct DiscoveredHost {
|
|
/// Stable row key: the advertised host id, falling back to the mDNS fullname.
|
|
pub key: String,
|
|
pub name: String,
|
|
pub addr: String,
|
|
pub port: u16,
|
|
/// Host certificate fingerprint to pin (lowercase hex), empty if not advertised.
|
|
pub fp_hex: String,
|
|
/// Pairing requirement: `"required"` or `"optional"`.
|
|
pub pair: String,
|
|
/// Wake-on-LAN MAC(s) from the mDNS `mac` TXT (comma-separated `aa:bb:cc:dd:ee:ff`), which the
|
|
/// hosts page persists onto the matching saved host so it can wake it later. Empty if absent.
|
|
pub mac: Vec<String>,
|
|
/// The host's OS-identity chain from the mDNS `os` TXT (`windows` | `macos` |
|
|
/// `linux[/<family>][/<id>]`), sanitized — drives the host tile's OS mark and is
|
|
/// persisted like `mac`. Empty if absent (older host).
|
|
pub os: String,
|
|
}
|
|
|
|
/// Forces the running browse to re-query now — the hosts page's Refresh. Mirrors
|
|
/// `pf_client_core::discovery::Rescan`; see there for why a client needs one (`mdns-sd` re-queries
|
|
/// on a backoff that doubles out to an hour, so a long-lived browse is effectively passive).
|
|
#[derive(Clone, Debug)]
|
|
pub struct Rescan(Arc<AtomicBool>);
|
|
|
|
impl Rescan {
|
|
/// Ask the browse thread to put a fresh query on the wire. Coalesces; returns immediately.
|
|
pub fn request(&self) {
|
|
self.0.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
/// Browse continuously for the app's lifetime, with a handle that forces an immediate re-query.
|
|
/// The thread exits when the receiver is dropped (the send fails) or the daemon dies.
|
|
pub fn browse() -> (async_channel::Receiver<DiscoveredHost>, Rescan) {
|
|
let (tx, rx) = async_channel::unbounded();
|
|
let flag = Arc::new(AtomicBool::new(false));
|
|
let requested = flag.clone();
|
|
std::thread::Builder::new()
|
|
.name("punktfunk-mdns".into())
|
|
.spawn(move || {
|
|
let daemon = match ServiceDaemon::new() {
|
|
Ok(d) => d,
|
|
Err(e) => {
|
|
tracing::warn!(error = %e, "mDNS daemon failed — discovery disabled");
|
|
return;
|
|
}
|
|
};
|
|
let mut receiver = match daemon.browse(SERVICE_TYPE) {
|
|
Ok(r) => r,
|
|
Err(e) => {
|
|
tracing::warn!(error = %e, "mDNS browse failed — discovery disabled");
|
|
return;
|
|
}
|
|
};
|
|
loop {
|
|
// The worker has to notice that its consumer went away even when NOTHING is
|
|
// arriving — the normal state of a LAN with no hosts on it. The old blocking
|
|
// `recv()` only ever learned that from a failed send, so a bounded consumer (the
|
|
// wake-and-wait below spawns one browse per wake) left this thread and its daemon
|
|
// — another thread, and a socket bound to :5353 — running for the app's lifetime.
|
|
// Checked at the TOP so the `continue` arms below can't skip it either.
|
|
if tx.is_closed() {
|
|
break;
|
|
}
|
|
// Re-browsing the same type replaces the daemon's listener: it replays the cache
|
|
// into the new channel, queries immediately, and resets the backoff.
|
|
if requested.swap(false, Ordering::Relaxed) {
|
|
match daemon.browse(SERVICE_TYPE) {
|
|
Ok(r) => receiver = r,
|
|
Err(e) => tracing::warn!(error = %e, "mDNS rescan failed"),
|
|
}
|
|
}
|
|
let event = match receiver.recv_timeout(Duration::from_millis(250)) {
|
|
Ok(event) => event,
|
|
Err(_) if receiver.is_disconnected() && receiver.is_empty() => break,
|
|
Err(_) => continue, // timed out — go round and look for a rescan request
|
|
};
|
|
if let ServiceEvent::ServiceResolved(info) = event {
|
|
let props = info.get_properties();
|
|
let val = |k: &str| props.get_property_val_str(k).unwrap_or("").to_string();
|
|
// IPv4 only, like every other client (`pf_client_core::discovery`): the core
|
|
// dials `format!("{host}:{port}").parse::<SocketAddr>()`, which cannot parse a
|
|
// bare IPv6 literal, and the host stack binds IPv4 sockets exclusively. Taking
|
|
// an arbitrary first address here rendered cards that failed on every click,
|
|
// because a host's OS responder commonly answers AAAA for its hostname.
|
|
let Some(addr) = info.get_addresses_v4().iter().next().map(|a| a.to_string())
|
|
else {
|
|
continue;
|
|
};
|
|
let id = val("id");
|
|
let host = DiscoveredHost {
|
|
key: if id.is_empty() {
|
|
info.get_fullname().to_string()
|
|
} else {
|
|
id
|
|
},
|
|
name: info
|
|
.get_fullname()
|
|
.split('.')
|
|
.next()
|
|
.unwrap_or("?")
|
|
.to_string(),
|
|
addr,
|
|
port: info.get_port(),
|
|
fp_hex: val("fp"),
|
|
pair: val("pair"),
|
|
mac: val("mac")
|
|
.split(',')
|
|
.map(|s| s.trim().to_string())
|
|
.filter(|s| !s.is_empty())
|
|
.collect(),
|
|
os: pf_client_core::os::sanitize_os(&val("os")),
|
|
};
|
|
if tx.send_blocking(host).is_err() {
|
|
break; // UI gone — stop browsing
|
|
}
|
|
}
|
|
}
|
|
let _ = daemon.shutdown();
|
|
})
|
|
.expect("spawn mdns thread");
|
|
(rx, Rescan(flag))
|
|
}
|