Skip to main content

ts_runtime/
lib.rs

1#![doc = include_str!("../README.md")]
2
3extern crate ts_netstack_smoltcp as netstack;
4
5use core::time::Duration;
6use std::sync::Arc;
7
8use kameo::{
9    actor::{ActorRef, Spawn, WeakActorRef},
10    mailbox::Signal,
11};
12use netstack::netcore::Channel;
13use tokio::sync::watch;
14
15use crate::{
16    control_runner::ControlRunner, dataplane::DataplaneActor, direct::DirectManager,
17    forwarder_actor::ForwarderActor, multiderp::Multiderp, netstack_actor::NetstackActor,
18};
19
20/// Pcap stream framer for debug packet capture (`CapturePcap`).
21pub mod capture;
22/// Control runner.
23pub mod control_runner;
24mod dataplane;
25mod derp_latency;
26/// Device connection-state tracking ([`DeviceState`]) and typed registration outcome
27/// ([`RegistrationError`]).
28pub mod device_state;
29mod direct;
30mod env;
31mod error;
32/// Fallback TCP handler registry (`tsnet.Server.RegisterFallbackTCPHandler` parity).
33pub mod fallback_tcp;
34mod forwarder_actor;
35/// Client-side Funnel ingress termination (`tsnet`'s `ListenFunnel` data path).
36pub mod funnel;
37mod magic_dns;
38mod multiderp;
39mod netstack_actor;
40mod packetfilter;
41pub mod peer_tracker;
42mod peerapi;
43mod peerapi_doh;
44mod route_updater;
45/// Stored Serve config + accept-loop runtime (`tsnet`'s `Get/SetServeConfig` + serving runtime).
46pub mod serve;
47mod src_filter;
48/// Netmap status snapshot, WhoIs, and watcher types.
49pub mod status;
50/// Taildrop peer-to-peer file transfer store.
51pub mod taildrop;
52pub mod taildrop_send;
53/// Tailnet-Lock (TKA) chain-sync orchestration: bootstrap + offer/send driver (the runtime layer
54/// that bridges the `ts_control` sync RPCs and the `ts_tka` chain logic).
55mod tka_sync;
56#[cfg(feature = "tun")]
57mod tun_actor;
58
59pub use device_state::{DeviceState, RegistrationError};
60pub(crate) use env::Env;
61pub use error::{Error, ErrorKind};
62pub use status::{FileTarget, NetcheckReport, RegionLatency, Status, StatusNode, WhoIs};
63pub use ts_dataplane::{CaptureHook, CapturePath};
64
65use crate::peer_tracker::PeerTracker;
66
67/// The runtime for a tailscale device.
68pub struct Runtime {
69    /// Reference to the control actor.
70    pub control: ActorRef<ControlRunner>,
71    dataplane: ActorRef<DataplaneActor>,
72    /// Reference to the direct (disco/UDP underlay) manager, retained so [`Runtime::rebind`] can
73    /// ask it to re-bind the underlay socket on a network/link change.
74    direct: ActorRef<DirectManager>,
75    /// Reference to the application netstack actor. `None` in TUN transport mode, where there is
76    /// no userspace application netstack (the application data path is a real kernel TUN device).
77    netstack: Option<WeakActorRef<NetstackActor>>,
78    /// Reference to the peer tracker for peer lookups.
79    pub peer_tracker: WeakActorRef<PeerTracker>,
80    /// Fallback TCP handler registry, bound to the application netstack. `None` in TUN transport
81    /// mode (no application netstack exists to attach it to).
82    fallback_tcp: Option<fallback_tcp::FallbackTcpManager>,
83    /// Reference to the forwarder actor, retained so [`Runtime::set_advertise_routes`] can push a
84    /// new accept/dial route table onto the running forwarder (the local half of advertising
85    /// routes). Without this the strong ref would drop after the startup `GetChannel` and the
86    /// forwarder would be reachable only via the message bus.
87    forwarder: ActorRef<ForwarderActor>,
88    /// Reference to the multiderp manager, retained so [`Runtime::status`] can resolve each
89    /// relayed peer's DERP region id to its region **code** (`ipnstate.PeerStatus.Relay`). Without
90    /// this the strong ref would drop after startup (it is cloned into the direct manager + route
91    /// updater) and the region-code map would be unreachable.
92    multiderp: ActorRef<Multiderp>,
93    env: Env,
94    shutdown: watch::Sender<bool>,
95    /// Sender side of the exit-node selector `watch` cell. Held privately here (not on the cloned
96    /// `Env`, which keeps only the read side) so that only `Runtime::set_exit_node` can mutate the
97    /// selection; the route updater and source filter re-read it via [`Env::exit_node`].
98    exit_node_tx: watch::Sender<Option<ts_control::ExitNodeSelector>>,
99    /// Receiver mirroring the *active* (resolved + fail-closed) exit node's stable id, fed by the
100    /// route updater. Read by [`Runtime::status`] / [`Runtime::active_exit_node`] to report which
101    /// exit node traffic is actually egressing through (vs. the merely-configured selector).
102    active_exit_rx: watch::Receiver<Option<ts_control::StableNodeId>>,
103    /// Receiver for the device connection-state cell, fed by the control runner. Read by
104    /// [`Runtime::watch_state`] and [`Runtime::wait_until_running`].
105    state_rx: watch::Receiver<DeviceState>,
106    /// Receiver for the retained peer-capability grants, fed by the packet-filter updater. Read by
107    /// [`Runtime::whois`] to resolve the flow-scoped cap map (Go `apitype.WhoIsResponse.CapMap`).
108    cap_grants_rx: watch::Receiver<packetfilter::CapGrants>,
109}
110
111impl Runtime {
112    /// Spawn a new runtime with the given parameters for connecting to a tailnet.
113    pub async fn spawn(
114        config: ts_control::Config,
115        auth_key: Option<String>,
116        keys: ts_keys::NodeState,
117    ) -> Result<Self, Error> {
118        let (shutdown_tx, shutdown_rx) = watch::channel(false);
119
120        // The exit-node selector is a live `watch` cell so `Device::set_exit_node` can change it at
121        // runtime. `new_with_exit_tx` returns the `Sender` (mutation capability) separately so it is
122        // retained privately on the `Runtime`, while only the `Receiver` (the readers' contract)
123        // lives on the cloned `Env`. The initial value comes from `ForwarderConfig.exit_node`.
124        let (env, exit_node_tx) = Env::new_with_exit_tx(
125            keys,
126            shutdown_rx,
127            env::ForwarderConfig::from_control_config(&config),
128        );
129
130        // Both userspace netstacks (application + forwarder) share one netstack config. Honor the
131        // per-deployment TCP buffer knob when set, otherwise fall back to the netstack default.
132        let netstack_config = netstack_config_from(config.tcp_buffer_size);
133
134        let dataplane = DataplaneActor::spawn(env.clone());
135
136        let (netstack_id, netstack_up, netstack_down) =
137            dataplane.ask(dataplane::NewOverlayTransport).await?;
138
139        // A second overlay transport feeds the dedicated any-IP forwarder netstack. Inbound packets
140        // for advertised subnet routes / the exit-node default route are routed here (see
141        // `route_updater`), keeping forwarded flows off the application netstack.
142        let (forwarder_id, forwarder_up, forwarder_down) =
143            dataplane.ask(dataplane::NewOverlayTransport).await?;
144
145        let multiderp = Multiderp::spawn((env.clone(), dataplane.clone()));
146
147        // Spawn the direct (disco) underlay manager before the route updater. Its `on_start`
148        // binds the UDP socket and registers its transport synchronously, so by the time the
149        // route updater asks it for the direct transport id it is guaranteed to be available.
150        let direct = DirectManager::spawn((env.clone(), dataplane.clone(), multiderp.clone()));
151
152        // Spawn the forwarder before the route updater. Its `on_start` builds the forwarder
153        // netstack, enables any-IP acceptance, and starts the per-port accept loops synchronously,
154        // so by the time the route updater begins delivering advertised prefixes to
155        // `forwarder_id` the netstack is already draining its transport.
156        let forwarder = ForwarderActor::spawn((
157            env.clone(),
158            netstack_config.clone(),
159            forwarder_up,
160            forwarder_down,
161        ));
162        // Force `on_start` to finish (any-IP enabled, accept loops live) before the route updater
163        // can route the first inbound flow to `forwarder_id`: an `ask` blocks until the actor has
164        // started.
165        //
166        // The forwarder netstack's overlay `Channel` is reused by the TUN application path for
167        // recursive / exit-node-DoH MagicDNS forwarding (TUN mode has no application netstack of its
168        // own, but the forwarder netstack runs in both modes and egresses over the overlay — the
169        // anti-leak property `forward_query`/`forward_doh` require). Only the `tun` Tun arm consumes
170        // it, so it is unused when the `tun` feature is off — allow that without warn-as-error.
171        #[cfg_attr(not(feature = "tun"), allow(unused_variables))]
172        let (forwarder_channel,) = forwarder.ask(forwarder_actor::GetChannel).await?;
173
174        // The route updater is the single authoritative resolver of the active (resolved,
175        // fail-closed) exit node; it publishes the resolved stable id into this watch cell so
176        // `Runtime::status` can report which exit is actually engaged (not just configured).
177        let (active_exit_tx, active_exit_rx) = watch::channel(None);
178        route_updater::RouteUpdater::spawn((
179            multiderp.clone(),
180            direct.clone(),
181            env.clone(),
182            netstack_id,
183            forwarder_id,
184            active_exit_tx,
185        ));
186        // The packet-filter updater also surfaces the retained cap-grants (for flow-scoped WhoIs)
187        // through a `watch` cell whose receiver the `Runtime` holds — the bus has no replay, so a
188        // `watch` is how `Runtime::whois` reads the current grants on demand.
189        let (cap_grants_tx, cap_grants_rx) = watch::channel(Default::default());
190        packetfilter::PacketfilterUpdater::spawn((env.clone(), cap_grants_tx));
191        src_filter::SourceFilterUpdater::spawn(env.clone());
192        let peer_tracker = PeerTracker::spawn(env.clone()).downgrade();
193
194        // Select the application data path from the transport mode. The forwarder/egress path
195        // above is UNCHANGED in both modes — TUN mode only swaps the application data path, never
196        // the forwarder. `config` is moved into `ControlRunner::spawn` below, so branch on a
197        // borrow and clone the small `TunConfig` where needed before the move.
198        //
199        // - Netstack (the default, and the only reachable arm when the `tun` feature is off):
200        //   spawn the application netstack + MagicDNS responder + fallback-TCP registry, all on
201        //   the `netstack_up`/`netstack_down` overlay seam.
202        // - Tun: spawn `TunActor` on that same overlay seam instead; no application netstack and
203        //   no MagicDNS responder exist, and `netstack`/`fallback_tcp` are `None`.
204        // - Tun requested but built without the `tun` feature: hard-error (a config/build
205        //   mismatch knowable at spawn time). NEVER silently fall back to netstack.
206        let (netstack, fallback_tcp) = match &config.transport_mode {
207            ts_control::TransportMode::Netstack => {
208                let netstack = NetstackActor::spawn((
209                    env.clone(),
210                    netstack_config,
211                    netstack_up,
212                    netstack_down,
213                ));
214
215                // Fetch the netstack channel while we still hold the strong ActorRef, then spawn
216                // the MagicDNS responder on it. Fire-and-forget: like src_filter/route_updater,
217                // it's owned by the message bus and isn't stored on `Runtime`.
218                let (channel,) = netstack.ask(netstack_actor::GetChannel).await?;
219                // The fallback-TCP registry attaches to the application netstack — the same one
220                // that carries the embedder's explicit `Device::tcp_listen` sockets — so a
221                // fallback handler sees exactly the inbound flows no explicit listener matched.
222                let fallback_tcp = fallback_tcp::FallbackTcpManager::new(channel.clone());
223                magic_dns::MagicDnsActor::spawn((env.clone(), channel));
224
225                (Some(netstack.downgrade()), Some(fallback_tcp))
226            }
227
228            #[cfg(feature = "tun")]
229            ts_control::TransportMode::Tun(tun_cfg) => {
230                // Reuse the same `netstack_up`/`netstack_down` overlay-transport pair that would
231                // have fed the netstack — it is just the application-side overlay seam (the name
232                // is historical). No NetstackActor / MagicDnsActor is spawned.
233                tun_actor::TunActor::spawn((
234                    env.clone(),
235                    tun_cfg.clone(),
236                    netstack_up,
237                    netstack_down,
238                    // Host-route gating inputs derived from `Env`: subnet routes are only steered
239                    // into the TUN when `--accept-routes` is set, and the host `/0` only when the
240                    // embedder configured an exit node. See `tun_actor::host_routes_from_node`.
241                    tun_actor::HostRouteGating {
242                        accept_routes: env.accept_routes,
243                        exit_node_configured: env.exit_node().is_some(),
244                    },
245                    // Reuse the forwarder netstack's overlay `Channel` for recursive / exit-node-DoH
246                    // MagicDNS forwarding in the TUN datapath (TUN mode has no application netstack
247                    // Channel of its own). Egresses over the overlay — anti-leak preserved.
248                    forwarder_channel.clone(),
249                ));
250
251                (None, None)
252            }
253
254            #[cfg(not(feature = "tun"))]
255            ts_control::TransportMode::Tun(_) => {
256                return Err(Error {
257                    kind: ErrorKind::TunUnavailable,
258                    target_actor: None,
259                    message_ty: None,
260                });
261            }
262        };
263
264        // Device connection-state cell. Created here (not inside the actor) so the control runner's
265        // `on_start` can publish `Failed`/`NeedsLogin` and still return `Err` without the sender
266        // being tied to a `Self` that never gets constructed on a hard registration failure.
267        let (state_tx, state_rx) = watch::channel(DeviceState::Connecting);
268
269        let control = ControlRunner::spawn(control_runner::Params {
270            config,
271            auth_key,
272            env: env.clone(),
273            state_tx,
274        });
275
276        Ok(Self {
277            control,
278            dataplane,
279            direct,
280            peer_tracker,
281            fallback_tcp,
282            forwarder,
283            multiderp,
284            netstack,
285            env,
286            shutdown: shutdown_tx,
287            exit_node_tx,
288            active_exit_rx,
289            state_rx,
290            cap_grants_rx,
291        })
292    }
293
294    /// Register a fallback TCP handler consulted for every inbound TCP flow that matches no
295    /// explicit listener (`tsnet.Server.RegisterFallbackTCPHandler` parity).
296    ///
297    /// The returned [`fallback_tcp::FallbackTcpHandle`] deregisters the handler when dropped. See
298    /// [`fallback_tcp`] for the dispatch contract and anti-leak guarantees.
299    ///
300    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
301    /// application netstack to attach a fallback handler to.
302    pub fn register_fallback_tcp_handler(
303        &self,
304        cb: Arc<
305            dyn Fn(core::net::SocketAddr, core::net::SocketAddr) -> fallback_tcp::FallbackDecision
306                + Send
307                + Sync,
308        >,
309    ) -> Result<fallback_tcp::FallbackTcpHandle, Error> {
310        Ok(self
311            .fallback_tcp
312            .as_ref()
313            .ok_or(Error {
314                kind: ErrorKind::UnsupportedInTunMode,
315                target_actor: None,
316                message_ty: None,
317            })?
318            .register(cb))
319    }
320
321    /// Get a channel to send commands to the netstack.
322    ///
323    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
324    /// application netstack.
325    pub async fn channel(&self) -> Result<Channel, Error> {
326        let (channel,) = self
327            .netstack
328            .as_ref()
329            .ok_or(Error {
330                kind: ErrorKind::UnsupportedInTunMode,
331                target_actor: None,
332                message_ty: None,
333            })?
334            .upgrade()
335            .ok_or(Error {
336                kind: ErrorKind::ActorGone,
337                target_actor: None,
338                message_ty: None,
339            })?
340            .ask(netstack_actor::GetChannel)
341            .await?;
342
343        Ok(channel)
344    }
345
346    /// The Taildrop file store, if Taildrop is enabled (`taildrop_dir` configured and the store
347    /// initialized). `None` when disabled — fail-closed. Shared with the peerAPI Taildrop server so
348    /// the embedder's read APIs and the receive path see the same on-disk store.
349    pub fn taildrop_store(&self) -> Option<Arc<crate::taildrop::TaildropStore>> {
350        self.env.taildrop_store.clone()
351    }
352
353    /// The shared Funnel ingress slot the peerAPI `/v0/ingress` route reads per connection.
354    ///
355    /// `Device::listen_funnel` installs a [`FunnelManager`](crate::funnel::FunnelManager)'s sink here
356    /// to make the route live (the peerAPI server is already running from startup). Returns a clone of
357    /// the runtime-lifetime `Arc` so the device can write the slot without restarting the server. See
358    /// [`crate::funnel`] for the ingress data path.
359    pub fn funnel_ingress_slot(&self) -> crate::funnel::FunnelIngressSlot {
360        self.env.funnel_ingress.clone()
361    }
362
363    /// The shared "Funnel ingress listener active" flag (the same `Arc` the control session reads to
364    /// set `HostInfo.IngressEnabled`). `Device::listen_funnel` flips it `true` while a funnel listener
365    /// is up so control routes Funnel traffic to this node; clearing it advertises no live endpoint.
366    pub fn ingress_active_flag(&self) -> std::sync::Arc<std::sync::atomic::AtomicBool> {
367        self.env.ingress_active.clone()
368    }
369
370    /// Install (`Some`) or clear (`None`) the debug packet-capture hook on the running dataplane.
371    /// `Some(hook)` tees every plaintext packet crossing the datapath to `hook` until it is cleared;
372    /// `None` stops capture. Mirrors Go `tstun.Wrapper.InstallCaptureHook` / `ClearCaptureSink`.
373    pub async fn install_capture(
374        &self,
375        hook: Option<ts_dataplane::CaptureHook>,
376    ) -> Result<(), Error> {
377        self.dataplane
378            .ask(dataplane::InstallCapture { hook })
379            .await
380            .map_err(Into::into)
381    }
382
383    /// Re-bind the underlay UDP socket after a network/link change (Wi-Fi switch, sleep/wake). The
384    /// embedder's own link monitor calls this (the engine owns the socket re-bind; the embedder owns
385    /// OS netmon). Re-binds the socket (same-port-preferred, IPv4-only invariant preserved) and
386    /// resets the now-stale local NAT mapping — clearing learned reflexive addresses and every
387    /// confirmed direct path while keeping candidate endpoints, so peers re-probe over the new socket
388    /// and relay over DERP (never a direct host dial) until a path re-confirms. Peers, control, the
389    /// netmap, disco state, and DERP are untouched. A no-op when the underlay is inert (bind failed
390    /// at startup, DERP-only). Mirrors Go magicsock `Conn.Rebind` + `resetEndpointStates`.
391    pub async fn rebind(&self) -> Result<(), Error> {
392        self.direct.ask(direct::Rebind).await.map_err(Error::from)
393    }
394
395    /// A snapshot of the local netmap: this node plus every known peer.
396    ///
397    /// Combines the self node held by the control runner with the peer set held by the peer
398    /// tracker. Mirrors tsnet's `LocalClient::Status`.
399    ///
400    /// `self_node` is `None` until the first netmap update has been received from control. Peer
401    /// entries carry no online/user/capability data (see the [`status`] module docs for that gap).
402    pub async fn status(&self) -> Result<Status, Error> {
403        let self_node_domain = self.control.ask(control_runner::SelfNode).await?;
404        // The MagicDNS suffix is the self node's FQDN minus its host label — already split into
405        // `Node.tailnet` at decode time (Go derives it the same way in `NetworkMap.MagicDNSSuffix`).
406        // Capture it before the domain `Node` is mapped away into a `StatusNode`.
407        let magic_dns_suffix = self_node_domain.as_ref().and_then(|n| n.tailnet.clone());
408        let self_node = self_node_domain.as_ref().map(StatusNode::from_node);
409
410        let peers_with_ids = self
411            .peer_tracker
412            .upgrade()
413            .ok_or(Error {
414                kind: ErrorKind::ActorGone,
415                target_actor: None,
416                message_ty: None,
417            })?
418            .ask(peer_tracker::GetStatus)
419            .await?;
420
421        // Join per-peer connectivity (Go `PeerStatus.CurAddr`): one batched query to the direct
422        // manager for every peer's current trusted direct endpoint, then fill `cur_addr` on each
423        // `StatusNode`. A peer absent from the map is relayed via DERP (`cur_addr = None`). This is a
424        // live snapshot — the direct path can expire/re-confirm between calls (matches Go's snapshot
425        // semantics). The `watch_netmap` stream intentionally carries no connectivity (it is a netmap
426        // watch, not a path-state watch, and does not re-fire on direct↔relay flips).
427        let ids: Vec<ts_transport::PeerId> = peers_with_ids.iter().map(|(id, _)| *id).collect();
428        let best_addrs = self
429            .direct
430            .ask(direct::BestAddrs { ids: ids.clone() })
431            .await
432            .unwrap_or_default();
433
434        // For the peers with NO direct path (relayed via DERP), resolve the region CODE they relay
435        // through (Go `PeerStatus.Relay`). One batched ask to multiderp; `cur_addr` and `relay` are
436        // mutually exclusive for a routed peer, mirroring Go's empty-vs-set strings.
437        let relay_ids: Vec<ts_transport::PeerId> = ids
438            .into_iter()
439            .filter(|id| !best_addrs.contains_key(id))
440            .collect();
441        let relay_codes = if relay_ids.is_empty() {
442            Default::default()
443        } else {
444            self.multiderp
445                .ask(multiderp::RelayCodesForPeers { ids: relay_ids })
446                .await
447                .unwrap_or_default()
448        };
449
450        let peers = peers_with_ids
451            .into_iter()
452            .map(|(id, mut node)| match best_addrs.get(&id).copied() {
453                Some(addr) => {
454                    node.cur_addr = Some(addr);
455                    node
456                }
457                None => {
458                    node.relay = relay_codes.get(&id).cloned();
459                    node
460                }
461            })
462            .collect();
463
464        Ok(Status {
465            self_node,
466            peers,
467            active_exit_node: self.active_exit_node(),
468            magic_dns_suffix,
469        })
470    }
471
472    /// List the tailnet peers this node can Taildrop a file *to* (Go LocalAPI `FileTargets`).
473    ///
474    /// Mirrors the upstream send-path filter (`feature/taildrop` `Extension::FileTargets`): a peer
475    /// qualifies when it advertises a reachable peerAPI **and** is either owned by the same user as
476    /// this node **or** explicitly granted the file-sharing-target capability. The whole list is
477    /// gated on this node holding the file-sharing capability (control sets it when the admin enables
478    /// Taildrop) — absent that, an empty list (fail-closed, not an error, matching how the receive
479    /// store returns empty when disabled). Results are sorted by the peer's MagicDNS name.
480    ///
481    /// Targets are listed regardless of current online state (upstream's `FileTargets` does not gate
482    /// on online either; an offline target's send will simply time out). The self node is never
483    /// included. Returns empty before the first netmap.
484    ///
485    /// Divergence from Go: the upstream filter also excludes `tvOS` peers, which this fork cannot
486    /// reproduce (the domain node carries no OS string); the impact is negligible — the actual send
487    /// fail-closes if such a peer refused the transfer.
488    pub async fn file_targets(&self) -> Result<Vec<FileTarget>, Error> {
489        // Node-level gate: this node must hold the file-sharing capability (Taildrop enabled by the
490        // admin). Read it off the self node's cap map, like Go's `hasCapFileSharing()`.
491        let self_node = self.control.ask(control_runner::SelfNode).await?;
492        let Some(self_node) = self_node else {
493            return Ok(Vec::new()); // no netmap yet
494        };
495        if !self_node.can_share_files() {
496            return Ok(Vec::new()); // Taildrop not enabled for the tailnet — fail-closed
497        }
498        let self_user_id = self_node.user_id;
499
500        let peers = self
501            .peer_tracker
502            .upgrade()
503            .ok_or(Error {
504                kind: ErrorKind::ActorGone,
505                target_actor: None,
506                message_ty: None,
507            })?
508            .ask(peer_tracker::AllPeers)
509            .await?;
510
511        // Eligibility + ordering live in `build_file_targets` (pure, unit-tested in `status`).
512        Ok(status::build_file_targets(peers, self_user_id))
513    }
514
515    /// The stable id of the exit node traffic is currently egressing through, or `None` if none is
516    /// engaged. This is the route updater's resolved + fail-closed answer (see
517    /// [`Status::active_exit_node`](crate::status::Status::active_exit_node)): it differs from the
518    /// configured [`exit_node`](Self::exit_node) selector, which may name a peer that is absent or
519    /// no longer advertising a default route (in which case egress is dropped and this returns
520    /// `None`).
521    pub fn active_exit_node(&self) -> Option<ts_control::StableNodeId> {
522        self.active_exit_rx.borrow().clone()
523    }
524
525    /// Request an OIDC ID token from control scoped to `audience` (workload-identity federation).
526    ///
527    /// Returns the signed JWT, or the token RPC's own [`ts_control::IdTokenError`]. The kameo
528    /// delegated-reply send error is flattened: a handler error carries the real `IdTokenError`,
529    /// any other send failure (actor shutdown / mailbox closed) is surfaced as
530    /// [`ts_control::IdTokenError::NetworkError`].
531    pub async fn fetch_id_token(
532        &self,
533        audience: String,
534    ) -> Result<String, ts_control::IdTokenError> {
535        self.control
536            .ask(control_runner::FetchIdToken { audience })
537            .await
538            .map_err(flatten_send_err)
539    }
540
541    /// Log this node out of the tailnet: deregister it by expiring its current node key.
542    ///
543    /// Forwards to the control runner, which re-POSTs `/machine/register` with a past expiry over a
544    /// fresh Noise channel. This is a control-plane state change only — it does NOT shut the runtime
545    /// down (the caller follows with [`graceful_shutdown`](Self::graceful_shutdown)) and does not
546    /// touch the on-disk node key. The kameo delegated-reply send error is flattened the same way as
547    /// [`fetch_id_token`](Self::fetch_id_token): a handler error carries the real
548    /// [`ts_control::LogoutError`]; any other send failure (actor shutdown / mailbox closed) is
549    /// surfaced as [`ts_control::LogoutError::NetworkError`].
550    pub async fn logout(&self) -> Result<(), ts_control::LogoutError> {
551        self.control
552            .ask(control_runner::Logout)
553            .await
554            .map_err(flatten_logout_send_err)
555    }
556
557    /// Publish a `TXT` DNS record for this node via control's `/machine/set-dns` (Go
558    /// `LocalClient.SetDNS`).
559    ///
560    /// Forwards to the control runner, which POSTs the record over a fresh Noise channel. The kameo
561    /// delegated-reply send error is flattened the same way as [`fetch_id_token`](Self::fetch_id_token):
562    /// a handler error carries the real [`ts_control::SetDnsError`]; any other send failure (actor
563    /// shutdown / mailbox closed) is surfaced as [`ts_control::SetDnsError::NetworkError`].
564    pub async fn set_dns(
565        &self,
566        name: String,
567        value: String,
568    ) -> Result<(), ts_control::SetDnsError> {
569        self.control
570            .ask(control_runner::SetDns { name, value })
571            .await
572            .map_err(flatten_set_dns_send_err)
573    }
574
575    /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` (`acme` feature).
576    ///
577    /// Mirrors [`fetch_id_token`](Self::fetch_id_token): forwards to the control runner, which runs
578    /// the client-side ACME DNS-01 flow on a spawned task and publishes the challenge TXT via the
579    /// node's set-dns RPC. The kameo delegated-reply send error is flattened — a handler error
580    /// carries the real [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox
581    /// closed) is surfaced as a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control
582    /// plane 501s on set-dns.
583    #[cfg(feature = "acme")]
584    pub async fn get_certificate(
585        &self,
586        name: String,
587    ) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
588        self.control
589            .ask(control_runner::GetCertificate { name })
590            .await
591            .map_err(flatten_cert_send_err)
592    }
593
594    /// Resolve which node owns a tailnet source address.
595    ///
596    /// Maps the destination IP of `addr` to its owning node. Mirrors tsnet's `LocalClient::WhoIs`.
597    /// Returns `None` if no peer holds that tailnet IP.
598    ///
599    /// The returned [`WhoIs`] additionally carries the **flow-scoped** peer-capability grants
600    /// ([`WhoIs::cap_map`], Go `apitype.WhoIsResponse.CapMap`): the caps control's packet-filter
601    /// application rules authorize for traffic from THIS node (the flow source) to `addr` (the
602    /// destination). Empty when no grant matches. (The node-level cap map rides
603    /// [`WhoIs::capabilities`].)
604    pub async fn whois(&self, addr: core::net::SocketAddr) -> Result<Option<WhoIs>, Error> {
605        let whois = self
606            .peer_tracker
607            .upgrade()
608            .ok_or(Error {
609                kind: ErrorKind::ActorGone,
610                target_actor: None,
611                message_ty: None,
612            })?
613            .ask(peer_tracker::Whois { addr })
614            .await?;
615
616        let Some(mut whois) = whois else {
617            return Ok(None);
618        };
619
620        // Fill the flow-scoped cap map: src = this node's own tailnet IP (of the dst's family),
621        // dst = the queried address. A grant applies when src ∈ its src prefixes AND dst ∈ its dst
622        // prefixes (Go `Filter.CapsWithValues`). Resolve our own IP from the self node; if it isn't
623        // known yet, leave the map empty (no grants resolvable without a source).
624        let dst = addr.ip();
625        if let Some(self_node) = self.control.ask(control_runner::SelfNode).await? {
626            let src: core::net::IpAddr = if dst.is_ipv6() {
627                self_node.tailnet_address.ipv6.addr().into()
628            } else {
629                self_node.tailnet_address.ipv4.addr().into()
630            };
631            let grants = self.cap_grants_rx.borrow();
632            whois.cap_map = ts_packetfilter_state::caps_for(&grants, src, dst);
633        }
634
635        Ok(Some(whois))
636    }
637
638    /// The current direct-path status to the peer holding tailnet IP `dst`: its confirmed direct UDP
639    /// endpoint and that path's last-measured RTT, or `None` when there is no direct path right now
640    /// (the peer is relayed via DERP, is unknown, or has no disco key).
641    ///
642    /// The latency is the RTT of the most recent disco ping/pong that confirmed the path — a live
643    /// snapshot up to one probe interval stale, NOT a fresh on-demand round-trip (that is a separate,
644    /// heavier capability). Mirrors the direct-path latency Go surfaces for `ipnstate.PeerStatus`.
645    pub async fn direct_path(
646        &self,
647        dst: core::net::IpAddr,
648    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
649        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
650            kind: ErrorKind::ActorGone,
651            target_actor: None,
652            message_ty: None,
653        })?;
654
655        // Resolve the tailnet IP to its node, then to its disco key. No node / no disco key ⇒ no
656        // direct path is possible (a peer with no disco key can only be reached via DERP).
657        let Some(node) = peer_tracker
658            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
659            .await?
660        else {
661            return Ok(None);
662        };
663        let Some(disco) = node.disco_key else {
664            return Ok(None);
665        };
666
667        self.direct
668            .ask(direct::DirectPathLatency { disco })
669            .await
670            .map_err(Into::into)
671    }
672
673    /// Send a disco ping to the peer holding tailnet IP `dst` **now** and await the pong, returning
674    /// the fresh round-trip latency and the endpoint that answered, or `None` if no pong arrives
675    /// within `timeout` (or the peer is unknown / has no disco key / no candidate path). This is the
676    /// true on-demand `PingType::Disco` (Go `tailscale ping`), as opposed to
677    /// [`direct_path`](Self::direct_path) which reports the last periodic probe's RTT.
678    ///
679    /// The ping round-trip is awaited OFF the direct manager's mailbox (we take a `MagicSock` handle
680    /// and await on it directly), so a slow/timing-out ping never blocks the actor.
681    pub async fn ping_disco(
682        &self,
683        dst: core::net::IpAddr,
684        timeout: Duration,
685    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
686        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
687            kind: ErrorKind::ActorGone,
688            target_actor: None,
689            message_ty: None,
690        })?;
691
692        let Some(node) = peer_tracker
693            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
694            .await?
695        else {
696            return Ok(None);
697        };
698        let Some(disco) = node.disco_key else {
699            return Ok(None);
700        };
701
702        // Cheap synchronous handle fetch, then await the ping OFF the actor mailbox.
703        let Some(sock) = self.direct.ask(direct::SockHandle).await? else {
704            return Ok(None);
705        };
706        // A `ping_now` error is an underlay UDP send failure (not an actor problem); surface it as a
707        // reply-level error. A timed-out / unanswered ping is `Ok(None)`, not an error.
708        sock.ping_now(&disco, timeout).await.map_err(|_| Error {
709            kind: ErrorKind::ReplyErr,
710            target_actor: None,
711            message_ty: None,
712        })
713    }
714
715    /// Change the selected exit node at runtime (the equivalent of Go `tsnet`'s
716    /// `LocalClient.EditPrefs(ExitNodeID/ExitNodeIP)`), without recreating the device.
717    ///
718    /// Updates the live exit-node selector, then asks the peer tracker to re-broadcast the current
719    /// peer set so the route updater and source filter re-resolve the new selector immediately.
720    /// `None` clears the exit node (internet-bound traffic is then dropped, fail-closed, unless this
721    /// node egresses directly). The selection is re-resolved against the live peer set, so passing a
722    /// selector for a peer not yet in the netmap simply takes effect once that peer appears.
723    pub async fn set_exit_node(
724        &self,
725        selector: Option<ts_control::ExitNodeSelector>,
726    ) -> Result<(), Error> {
727        // Update the live cell every reader borrows from. `send_replace` keeps the value current
728        // even with no active receivers (none can have dropped while the runtime is up, but it is
729        // the right non-failing primitive here).
730        self.exit_node_tx.send_replace(selector);
731
732        // Trigger an immediate re-resolution: the route updater (outbound routes + DoH delegation)
733        // and the source filter (inbound validation) both recompute on an `Arc<PeerState>`, so a
734        // re-broadcast applies the new exit without waiting for the next netmap update.
735        self.peer_tracker
736            .upgrade()
737            .ok_or(Error {
738                kind: ErrorKind::ActorGone,
739                target_actor: None,
740                message_ty: None,
741            })?
742            .ask(peer_tracker::RepublishState)
743            .await
744            .map_err(Into::into)
745    }
746
747    /// The currently-selected exit node, or `None` if none is selected.
748    pub fn exit_node(&self) -> Option<ts_control::ExitNodeSelector> {
749        self.env.exit_node()
750    }
751
752    /// Change the set of subnet routes this node advertises at runtime (Go `tailscale set
753    /// --advertise-routes`). Applies BOTH halves together so the wire and the data path agree:
754    ///
755    /// 1. **Wire** — re-advertise `Hostinfo.RoutableIPs` to control on the live map-poll connection
756    ///    (so control grants the node the subnet-router role for exactly these prefixes).
757    /// 2. **Local** — swap the forwarder's accept/dial route table (so the node actually forwards the
758    ///    prefixes it advertises). New flows see the new set; in-flight flows keep their routing.
759    ///
760    /// `routes` is filtered to the IPv4-only, deduplicated set this fork can honor (IPv6 prefixes are
761    /// dropped under the IPv6-off posture — we never advertise a route we won't forward), so the wire
762    /// and forwarder are fed the identical final set. This sets the explicit subnet prefixes only; it
763    /// does NOT touch the exit-node `0.0.0.0/0` advertisement (a separate concern).
764    pub async fn set_advertise_routes(&self, routes: Vec<ipnet::IpNet>) -> Result<(), Error> {
765        // IPv4-only + dedup, mirroring `ts_control::Config::advertised_routes` so the wire grant and
766        // the forwarder accept set never disagree.
767        let filtered = filter_advertise_routes(routes);
768
769        // Local half first: start forwarding the prefixes before control grants them, so there is no
770        // window where control has granted a route the node black-holes. (The reverse order would
771        // briefly advertise a route we don't yet forward.) New flows pick up the table immediately.
772        self.forwarder
773            .ask(forwarder_actor::UpdateRoutes {
774                routes: filtered.clone(),
775            })
776            .await?;
777
778        // Wire half: re-advertise to control on the live map-poll connection.
779        self.control
780            .ask(control_runner::SetAdvertiseRoutes { routes: filtered })
781            .await
782            .map_err(Into::into)
783    }
784
785    /// Subscribe to netmap peer-change events.
786    ///
787    /// Returns a [`watch::Receiver`] whose value is the current set of peer [`StatusNode`]s,
788    /// updated on every netmap state update from control. Mirrors tsnet's `WatchIPNBus`. Await
789    /// [`watch::Receiver::changed`](tokio::sync::watch::Receiver::changed) to react to peers
790    /// joining, leaving, or changing.
791    pub async fn watch_netmap(&self) -> Result<watch::Receiver<Vec<StatusNode>>, Error> {
792        self.peer_tracker
793            .upgrade()
794            .ok_or(Error {
795                kind: ErrorKind::ActorGone,
796                target_actor: None,
797                message_ty: None,
798            })?
799            .ask(peer_tracker::WatchNetmap)
800            .await
801            .map_err(Into::into)
802    }
803
804    /// The current device connection-[`DeviceState`].
805    pub fn device_state(&self) -> DeviceState {
806        self.state_rx.borrow().clone()
807    }
808
809    /// Watch the device connection-[`DeviceState`] (`Connecting` → `Running` / `NeedsLogin` /
810    /// `Expired` / `Failed`).
811    ///
812    /// Returns a [`watch::Receiver`]; await
813    /// [`changed`](tokio::sync::watch::Receiver::changed) to react push-style to control connection
814    /// transitions instead of polling [`status`](Self::status). The initial value is the current
815    /// state. Note: a transient per-reconnect dip back to `Connecting` is **not** currently
816    /// emitted (control transparently reconnects below this layer); the state reflects registration
817    /// outcome and node-key expiry.
818    pub fn watch_state(&self) -> watch::Receiver<DeviceState> {
819        self.state_rx.clone()
820    }
821
822    /// Wait until the device finishes registering, returning a typed outcome.
823    ///
824    /// Resolves `Ok(())` once the device reaches [`DeviceState::Running`]. Returns a typed
825    /// [`RegistrationError`] otherwise — the actionable distinction between "retry", "re-pair", and
826    /// "drive interactive login" that replaces polling [`ipv4_addr`](Self::ipv4_addr) in a loop:
827    /// - `AuthRejected` — bad/expired/unknown auth key. **Permanent** (re-pair).
828    /// - `NeedsLogin(url)` — interactive authorization required (no usable auth key). **Not
829    ///   permanent**: the runtime keeps retrying and will reach `Running` once the user authorizes
830    ///   the URL. An **auth-key** caller should treat this as a failure; an **interactive** caller
831    ///   should ignore this return and instead drive the flow via [`watch_state`](Self::watch_state)
832    ///   (this method returns the URL eagerly rather than blocking for the whole login).
833    /// - `NetworkUnreachable` — control unreachable. **Transient** (retry).
834    /// - `Timeout` — no settled state within `timeout`.
835    ///
836    /// `KeyExpired` is not produced by this initial wait (a node key expires only *after* it has
837    /// come up); observe post-registration expiry via [`watch_state`](Self::watch_state).
838    /// `timeout` of `None` waits indefinitely for a settled state.
839    pub async fn wait_until_running(
840        &self,
841        timeout: Option<Duration>,
842    ) -> Result<(), RegistrationError> {
843        device_state::wait_for_running(self.state_rx.clone(), timeout).await
844    }
845
846    /// Attempt to shut down the runtime gracefully.
847    ///
848    /// Returns false if the shutdown timed out. It is still shut down if it timed out, just
849    /// more violently and with possible resource leaks.
850    pub async fn graceful_shutdown(self, timeout: Option<Duration>) -> bool {
851        self.shutdown.send_replace(true);
852
853        async fn _shutdown_all(runtime: Runtime) {
854            // See the note in `Drop` for why we only need to stop these actors to bring down the
855            // whole runtime.
856
857            let _ignore = runtime.control.stop_gracefully().await;
858            let _ignore = runtime.dataplane.stop_gracefully().await;
859            let _ignore = runtime.env.bus.stop_gracefully().await;
860
861            tokio::join![
862                runtime.control.wait_for_shutdown(),
863                runtime.dataplane.wait_for_shutdown(),
864                runtime.env.bus.wait_for_shutdown(),
865            ];
866        }
867
868        let fut = _shutdown_all(self);
869
870        match timeout {
871            Some(timeout) => tokio::time::timeout(timeout, fut).await.is_ok(),
872            None => {
873                fut.await;
874                true
875            }
876        }
877    }
878}
879
880impl Drop for Runtime {
881    fn drop(&mut self) {
882        // We must have already run `graceful_shutdown`: on the happy path, this does nothing, but
883        // if it timed out, we need to make sure the actors are dead so we don't leak them and their
884        // dependents.
885        if *self.shutdown.borrow() {
886            self.control.kill();
887            self.dataplane.kill();
888            self.env.bus.kill();
889            return;
890        }
891
892        self.shutdown.send_replace(true);
893
894        // Actors shut down when the last ActorRef to them is dropped (as nothing can send them
895        // messages anymore). If we don't hold an ActorRef in Runtime, in general the only thing
896        // that has one is the MessageBus, which each actor subscribes to for a subset of messages.
897        // Hence, if we shut down the bus, most actors die as well.
898
899        // First shut down the actors we have an ActorRef to:
900        try_shutdown(&self.control);
901        try_shutdown(&self.dataplane);
902
903        // Then shutdown the message bus, stopping the rest of the actors:
904        try_shutdown(&self.env.bus);
905    }
906}
907
908fn try_shutdown(a: &ActorRef<impl kameo::Actor>) {
909    if let Err(e) = a.mailbox_sender().try_send(Signal::Stop) {
910        tracing::error!(error = %e, "graceful shutdown failed, killing actor");
911        a.kill();
912    }
913}
914
915/// Build the netstack config shared by both userspace netstacks (application + forwarder) from the
916/// per-deployment `tcp_buffer_size` knob.
917///
918/// `None` keeps the netstack default (256 KiB/direction); `Some(n)` overrides it (e.g. a smaller
919/// window on a memory-constrained exit node forwarding many concurrent flows — see
920/// [`netstack::netcore::Config::tcp_buffer_size`]). Factored out of [`Runtime::spawn`] so the
921/// None-default / Some-override mapping is unit-testable without standing up the actor system.
922fn netstack_config_from(tcp_buffer_size: Option<usize>) -> netstack::netcore::Config {
923    let mut c = netstack::netcore::Config::default();
924    if let Some(tcp_buffer_size) = tcp_buffer_size {
925        c.tcp_buffer_size = tcp_buffer_size;
926    }
927    c
928}
929
930/// Filter a requested advertise-route set to the IPv4-only, deduplicated set this fork can honor,
931/// mirroring [`ts_control::Config::advertised_routes`] so a runtime `set_advertise_routes` feeds the
932/// wire (control grant) and the forwarder (accept/dial table) the identical final set. IPv6 prefixes
933/// are dropped under the IPv6-off posture — we never advertise a route we won't forward. Order is
934/// preserved (first occurrence wins). Factored out so the filter is unit-testable without an actor.
935fn filter_advertise_routes(routes: Vec<ipnet::IpNet>) -> Vec<ipnet::IpNet> {
936    let mut filtered: Vec<ipnet::IpNet> = Vec::new();
937    for net in routes {
938        if matches!(net, ipnet::IpNet::V4(_)) {
939            if !filtered.contains(&net) {
940                filtered.push(net);
941            }
942        } else {
943            tracing::warn!(prefix = %net, "dropping IPv6 advertise route (IPv6-off posture)");
944        }
945    }
946    filtered
947}
948
949/// Flatten a kameo delegated-reply [`SendError`] for the id-token RPC into the RPC's own
950/// [`ts_control::IdTokenError`].
951///
952/// A [`SendError::HandlerError`](kameo::error::SendError::HandlerError) carries the real
953/// `IdTokenError` produced by the handler and is surfaced verbatim. Any other send failure (actor
954/// not running / stopped, mailbox full, send timeout) is a delivery problem rather than an RPC
955/// result, so it collapses to a transient [`ts_control::IdTokenError::NetworkError`]. Factored out
956/// of [`Runtime::fetch_id_token`] so this mapping is unit-testable without standing up an actor.
957fn flatten_send_err<M>(
958    e: kameo::error::SendError<M, ts_control::IdTokenError>,
959) -> ts_control::IdTokenError {
960    match e {
961        kameo::error::SendError::HandlerError(err) => err,
962        _ => ts_control::IdTokenError::NetworkError,
963    }
964}
965
966/// Flatten a kameo `SendError` from the `Logout` ask into a [`ts_control::LogoutError`].
967///
968/// A `HandlerError` carries the real `LogoutError` from the control RPC and is surfaced verbatim;
969/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
970/// problem, not a logout result — collapses to the transient [`ts_control::LogoutError::NetworkError`]
971/// (logout is idempotent, so a retry after a delivery failure is safe). Factored out of
972/// [`Runtime::logout`] so the mapping is unit-testable without standing up an actor.
973fn flatten_logout_send_err<M>(
974    e: kameo::error::SendError<M, ts_control::LogoutError>,
975) -> ts_control::LogoutError {
976    match e {
977        kameo::error::SendError::HandlerError(err) => err,
978        _ => ts_control::LogoutError::NetworkError,
979    }
980}
981
982/// Flatten a kameo `SendError` from the `SetDns` ask into a [`ts_control::SetDnsError`].
983///
984/// A `HandlerError` carries the real `SetDnsError` from the set-dns RPC and is surfaced verbatim;
985/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
986/// problem, not a publish result — collapses to the transient
987/// [`ts_control::SetDnsError::NetworkError`]. Factored out of [`Runtime::set_dns`] so the mapping is
988/// unit-testable without standing up an actor.
989fn flatten_set_dns_send_err<M>(
990    e: kameo::error::SendError<M, ts_control::SetDnsError>,
991) -> ts_control::SetDnsError {
992    match e {
993        kameo::error::SendError::HandlerError(err) => err,
994        _ => ts_control::SetDnsError::NetworkError,
995    }
996}
997
998/// Flatten a kameo `SendError` from the `GetCertificate` ask into a [`ts_control::CertError`].
999///
1000/// A `HandlerError` carries the real `CertError` produced by the ACME issuance and is surfaced
1001/// verbatim. `CertError` has no transient-network variant, so any other send failure (actor not
1002/// running / stopped, mailbox full, send timeout) — a delivery problem rather than an issuance
1003/// result — collapses to a [`ts_control::CertError::Io`]. Factored out of
1004/// [`Runtime::get_certificate`] so this mapping is unit-testable without standing up an actor.
1005#[cfg(feature = "acme")]
1006fn flatten_cert_send_err<M>(
1007    e: kameo::error::SendError<M, ts_control::CertError>,
1008) -> ts_control::CertError {
1009    match e {
1010        kameo::error::SendError::HandlerError(err) => err,
1011        _ => ts_control::CertError::Io(std::io::Error::other(
1012            "control runner unavailable for certificate issuance",
1013        )),
1014    }
1015}
1016
1017#[cfg(test)]
1018mod tests {
1019    use super::*;
1020
1021    /// `None` must leave the netstack's own default TCP window in place (the 256 KiB throughput
1022    /// default), and must not silently coerce to some other value.
1023    #[test]
1024    fn netstack_config_none_uses_netstack_default() {
1025        let default = netstack::netcore::Config::default();
1026        let built = netstack_config_from(None);
1027        assert_eq!(
1028            built.tcp_buffer_size, default.tcp_buffer_size,
1029            "None must inherit the netstack default TCP buffer size"
1030        );
1031    }
1032
1033    /// `Some(n)` must override the TCP window (the memory-vs-throughput knob exit-node operators
1034    /// reach for), reaching the config that both netstacks are built from.
1035    #[test]
1036    fn netstack_config_some_overrides_buffer() {
1037        let built = netstack_config_from(Some(64 * 1024));
1038        assert_eq!(
1039            built.tcp_buffer_size,
1040            64 * 1024,
1041            "Some(n) must override the TCP buffer size that both netstacks use"
1042        );
1043    }
1044
1045    /// `set_advertise_routes` must feed the wire and the forwarder the IDENTICAL filtered set:
1046    /// IPv4-only (IPv6 dropped under the IPv6-off posture), deduplicated, order preserved.
1047    #[test]
1048    fn filter_advertise_routes_keeps_v4_dedups_drops_v6() {
1049        let v4a: ipnet::IpNet = "10.0.0.0/24".parse().unwrap();
1050        let v4b: ipnet::IpNet = "192.168.1.0/24".parse().unwrap();
1051        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1052
1053        // Mixed input with a duplicate v4 and a v6 prefix.
1054        let out = filter_advertise_routes(vec![v4a, v6, v4b, v4a]);
1055
1056        assert_eq!(
1057            out,
1058            vec![v4a, v4b],
1059            "v6 dropped, duplicate v4 collapsed, first-occurrence order preserved"
1060        );
1061    }
1062
1063    /// An all-IPv6 request filters to empty (we never advertise a route we won't forward) rather
1064    /// than erroring — clearing the advertised set is a legitimate outcome.
1065    #[test]
1066    fn filter_advertise_routes_all_v6_is_empty() {
1067        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1068        assert!(filter_advertise_routes(vec![v6]).is_empty());
1069    }
1070
1071    /// A `HandlerError` carries the real `IdTokenError` from the RPC handler and must pass through
1072    /// verbatim, not be flattened to a generic network error. Using an `Internal(_)` payload (not
1073    /// `NetworkError`) makes the passthrough observable: a buggy flatten that always returned
1074    /// `NetworkError` would fail this assertion.
1075    #[test]
1076    fn flatten_send_err_handler_error_passes_through() {
1077        // Build an `Internal(_)` payload via the public `From<Utf8Error>` conversion (no extra
1078        // deps): it is distinct from the `_ => NetworkError` fallback, so a buggy flatten that
1079        // always returned `NetworkError` would fail this assertion.
1080        // Route the invalid bytes through a runtime Vec so the `invalid_from_utf8` lint (which only
1081        // fires on compile-time-known literals) doesn't flag this intentional bad input.
1082        let bytes = vec![0xffu8, 0xfe];
1083        let utf8_err = core::str::from_utf8(&bytes).unwrap_err();
1084        let inner = ts_control::IdTokenError::from(utf8_err);
1085        assert!(matches!(inner, ts_control::IdTokenError::Internal(_)));
1086        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1087            kameo::error::SendError::HandlerError(inner.clone());
1088        assert_eq!(flatten_send_err(e), inner);
1089    }
1090
1091    /// A non-handler send failure (actor stopped) is a delivery problem, not an RPC result, so it
1092    /// must collapse to a transient `NetworkError`.
1093    #[test]
1094    fn flatten_send_err_actor_stopped_is_network_error() {
1095        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1096            kameo::error::SendError::ActorStopped;
1097        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1098    }
1099
1100    /// `ActorNotRunning` (the message bounces back undelivered) is likewise a delivery failure and
1101    /// must map to a transient `NetworkError`.
1102    #[test]
1103    fn flatten_send_err_actor_not_running_is_network_error() {
1104        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1105            kameo::error::SendError::ActorNotRunning(control_runner::FetchIdToken {
1106                audience: "sts.amazonaws.com".to_string(),
1107            });
1108        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1109    }
1110
1111    /// A `HandlerError` from the logout RPC carries the real `LogoutError` and must pass through
1112    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1113    /// passthrough observable.
1114    #[test]
1115    fn flatten_logout_send_err_handler_error_passes_through() {
1116        let inner = ts_control::LogoutError::Internal(ts_control::LogoutInternalErrorKind::Http);
1117        assert!(matches!(inner, ts_control::LogoutError::Internal(_)));
1118        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1119            kameo::error::SendError::HandlerError(inner.clone());
1120        assert_eq!(flatten_logout_send_err(e), inner);
1121    }
1122
1123    /// A non-handler send failure (actor stopped) is a delivery problem, not a logout result, and
1124    /// collapses to a transient `NetworkError` (logout is idempotent, so a retry is safe).
1125    #[test]
1126    fn flatten_logout_send_err_actor_stopped_is_network_error() {
1127        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1128            kameo::error::SendError::ActorStopped;
1129        assert_eq!(
1130            flatten_logout_send_err(e),
1131            ts_control::LogoutError::NetworkError
1132        );
1133    }
1134
1135    /// A `HandlerError` from the set-dns RPC carries the real `SetDnsError` and must pass through
1136    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1137    /// passthrough observable.
1138    #[test]
1139    fn flatten_set_dns_send_err_handler_error_passes_through() {
1140        let inner = ts_control::SetDnsError::Internal(ts_control::SetDnsInternalErrorKind::Http);
1141        assert!(matches!(inner, ts_control::SetDnsError::Internal(_)));
1142        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1143            kameo::error::SendError::HandlerError(inner.clone());
1144        assert_eq!(flatten_set_dns_send_err(e), inner);
1145    }
1146
1147    /// A non-handler send failure (actor stopped) is a delivery problem, not a publish result, and
1148    /// collapses to a transient `NetworkError`.
1149    #[test]
1150    fn flatten_set_dns_send_err_actor_stopped_is_network_error() {
1151        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1152            kameo::error::SendError::ActorStopped;
1153        assert_eq!(
1154            flatten_set_dns_send_err(e),
1155            ts_control::SetDnsError::NetworkError
1156        );
1157    }
1158}