Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

build(deps): vendor patched tailscale crates

RoversX698bf4213a33
diff --git a/deps/tailscale/Cargo.toml b/deps/tailscale/Cargo.toml
new file mode 100644
index 00000000..34245c14
--- /dev/null
+++ b/deps/tailscale/Cargo.toml
@@ -0,0 +1,137 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2024"
+rust-version = "1.92.0"
+name = "tailscale"
+version = "0.6.1"
+build = false
+autolib = false
+autobins = false
+autoexamples = false
+autotests = false
+autobenches = false
+description = "A work-in-progress Tailscale implementation"
+readme = "README.md"
+keywords = [
+ "tailscale",
+ "networking",
+]
+categories = [
+ "network-programming",
+ "api-bindings",
+ "authentication",
+ "asynchronous",
+]
+license = "BSD-3-Clause"
+repository = "https://github.com/tailscale/tailscale-rs"
+
+[package.metadata.docs.rs]
+all-features = true
+
+[features]
+axum = ["dep:axum"]
+ssh = [
+ "dep:russh",
+ "dep:bytes",
+ "dep:ratatui",
+]
+
+[lib]
+name = "tailscale"
+path = "src/lib.rs"
+
+[dependencies.axum]
+version = "0.8"
+optional = true
+
+[dependencies.bytes]
+version = "1"
+optional = true
+
+[dependencies.rand]
+version = "0.10"
+
+[dependencies.ratatui]
+version = "0.30"
+optional = true
+
+[dependencies.russh]
+version = "0.60"
+features = [
+ "flate2",
+ "aws-lc-rs",
+]
+optional = true
+default-features = false
+
+[dependencies.serde]
+version = "1"
+features = ["derive"]
+
+[dependencies.serde_json]
+version = "1"
+
+[dependencies.thiserror]
+version = "2"
+default-features = false
+
+[dependencies.tokio]
+version = "1"
+features = [
+ "rt-multi-thread",
+ "macros",
+ "fs",
+]
+default-features = false
+
+[dependencies.tracing]
+version = "0.1"
+features = ["attributes"]
+default-features = false
+
+[dependencies.ts_control]
+version = "0.6.1"
+
+[dependencies.ts_keys]
+version = "0.6.1"
+
+[dependencies.ts_netstack_smoltcp]
+version = "0.6.1"
+features = ["tokio"]
+
+[dependencies.ts_runtime]
+version = "0.6.1"
+
+[dependencies.url]
+version = "2"
+default-features = false
+
+[lints.clippy]
+multiple_crate_versions = "allow"
+too_many_arguments = "allow"
+
+[lints.clippy.cargo]
+level = "warn"
+priority = -1
+
+[lints.rust]
+closure_returning_async_block = "warn"
+let-underscore = "warn"
+missing_docs = "warn"
+
+[lints.rustdoc]
+bare_urls = "deny"
+broken_intra_doc_links = "deny"
+invalid_html_tags = "deny"
+private_intra_doc_links = "deny"
+
diff --git a/deps/tailscale/LICENSE b/deps/tailscale/LICENSE
new file mode 100644
index 00000000..adcd4c5b
--- /dev/null
+++ b/deps/tailscale/LICENSE
@@ -0,0 +1,28 @@
+BSD 3-Clause License
+
+Copyright (c) 2026 Tailscale Inc & contributors.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/deps/tailscale/PATENTS b/deps/tailscale/PATENTS
new file mode 100644
index 00000000..560a2b8f
--- /dev/null
+++ b/deps/tailscale/PATENTS
@@ -0,0 +1,24 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Tailscale Inc. as part of the Tailscale project.
+
+Tailscale Inc. hereby grants to You a perpetual, worldwide,
+non-exclusive, no-charge, royalty-free, irrevocable (except as stated
+in this section) patent license to make, have made, use, offer to
+sell, sell, import, transfer and otherwise run, modify and propagate
+the contents of this implementation of Tailscale, where such license
+applies only to those patent claims, both currently owned or
+controlled by Tailscale Inc. and acquired in the future, licensable
+by Tailscale Inc. that are necessarily infringed by this
+implementation of Tailscale. This grant does not include claims that
+would be infringed only as a consequence of further modification of
+this implementation. If you or your agent or exclusive licensee
+institute or order or agree to the institution of patent litigation
+against any entity (including a cross-claim or counterclaim in a
+lawsuit) alleging that this implementation of Tailscale or any code
+incorporated within this implementation of Tailscale constitutes
+direct or contributory patent infringement, or inducement of patent
+infringement, then any patent rights granted to you under this License
+for this implementation of Tailscale shall terminate as of the date
+such litigation is filed.
diff --git a/deps/tailscale/src/axum.rs b/deps/tailscale/src/axum.rs
new file mode 100644
index 00000000..3a572500
--- /dev/null
+++ b/deps/tailscale/src/axum.rs
@@ -0,0 +1,71 @@
+//! Support for the [`axum`] http server wrapping [`netstack::TcpListener`].
+//!
+//! # Example
+//!
+//! ```rust,no_run
+//! # #[tokio::main]
+//! # async fn main() -> Result<(), Box<dyn core::error::Error>> {
+//! # use tailscale::{Config, Device};
+//! let dev = Device::new(
+//! &Config::default_with_key_file("tsrs_keys.json").await?,
+//! Some("YOUR_AUTH_KEY".to_owned()),
+//! ).await?;
+//!
+//! let listener = dev.tcp_listen((dev.ipv4_addr().await?, 80).into()).await?;
+//! let listener: tailscale::axum::Listener = listener.into();
+//!
+//! async fn index() -> &'static str { "Hello world!" }
+//! let router = axum::Router::new().route("/", axum::routing::get(index));
+//!
+//! axum::serve(listener, router).await?;
+//! # Ok(())
+//! # }
+//! ```
+
+use std::net::SocketAddr;
+
+use crate::netstack;
+
+/// Wrapper type implementing [`axum::serve::Listener`] on [`netstack::TcpListener`].
+#[derive(Debug)]
+pub struct Listener(netstack::TcpListener);
+
+impl From<netstack::TcpListener> for Listener {
+ fn from(listener: netstack::TcpListener) -> Self {
+ Self(listener)
+ }
+}
+
+impl From<Listener> for netstack::TcpListener {
+ fn from(listener: Listener) -> Self {
+ listener.0
+ }
+}
+
+impl AsRef<netstack::TcpListener> for Listener {
+ fn as_ref(&self) -> &netstack::TcpListener {
+ &self.0
+ }
+}
+
+impl axum::serve::Listener for Listener {
+ type Io = netstack::TcpStream;
+ type Addr = SocketAddr;
+
+ async fn accept(&mut self) -> (Self::Io, Self::Addr) {
+ let stream = loop {
+ match self.0.accept().await {
+ Ok(stream) => break stream,
+ Err(e) => tracing::error!(err = %e, "tcp accept"),
+ }
+ };
+
+ let addr = stream.remote_addr();
+
+ (stream, addr)
+ }
+
+ fn local_addr(&self) -> std::io::Result<Self::Addr> {
+ Ok(self.0.local_addr())
+ }
+}
diff --git a/deps/tailscale/src/config.rs b/deps/tailscale/src/config.rs
new file mode 100644
index 00000000..6a820def
--- /dev/null
+++ b/deps/tailscale/src/config.rs
@@ -0,0 +1,218 @@
+//! Types and utilities for configuring a Tailscale [`Device`](crate::Device).
+
+use std::path::Path;
+
+use ts_keys::PersistState;
+
+const CONTROL_URL_VAR: &str = "TS_CONTROL_URL";
+const HOSTNAME_VAR: &str = "TS_HOSTNAME";
+const AUTHKEY_VAR: &str = "TS_AUTH_KEY";
+
+/// Config for connecting to Tailscale.
+pub struct Config {
+ /// The cryptographic keys representing this node's identity.
+ pub key_state: PersistState,
+
+ // TODO(npry): let clients also define an app name once the sdk-level name moves
+ // to a dedicated field
+ /// The name of this client.
+ ///
+ /// This is reported to control in the `Hostinfo.App` field.
+ pub client_name: Option<String>,
+
+ /// The URL of the control server to connect to.
+ pub control_server_url: url::Url,
+
+ /// The hostname this node will request.
+ ///
+ /// If left blank, uses the hostname reported by the OS.
+ pub requested_hostname: Option<String>,
+
+ /// Tags this node will request.
+ pub requested_tags: Vec<String>,
+
+ /// Whether this node should register itself as ephemeral.
+ ///
+ /// This means that the node will be deleted from the tailnet after it is offline for a brief
+ /// period. It can be registered again with the same keys in the future but will have a
+ /// different node id.
+ ///
+ /// See the [KB article on ephemeral nodes] for more information.
+ ///
+ /// [KB article on ephemeral nodes]: https://tailscale.com/docs/features/ephemeral-nodes
+ pub ephemeral: bool,
+}
+
+impl Config {
+ /// Create a new config with its [`key_state`](Config::key_state) populated from the specified key file and using
+ /// default options for other configuration.
+ ///
+ /// See [`load_key_file`] for more details and an alternative with more options for reading
+ /// the key file.
+ pub async fn default_with_key_file(p: impl AsRef<Path>) -> Result<Self, crate::Error> {
+ Ok(Config {
+ key_state: load_key_file(p, Default::default()).await?,
+ ..Default::default()
+ })
+ }
+
+ /// Construct a default config, setting certain fields from environment variables.
+ ///
+ /// The fields are only set if the corresponding environment variable is present, using
+ /// the default value otherwise.
+ ///
+ /// Loads:
+ ///
+ /// - `control_server_url` from `TS_CONTROL_URL`
+ /// - `requested_hostname` from `TS_HOSTNAME`
+ pub fn default_from_env() -> Config {
+ let mut config = Config::default();
+
+ if let Ok(u) = std::env::var(CONTROL_URL_VAR) {
+ match u.parse() {
+ Ok(u) => config.control_server_url = u,
+ Err(e) => {
+ tracing::error!(error = %e, "parsing {CONTROL_URL_VAR} (fall back to default value)");
+ }
+ }
+ };
+
+ config.requested_hostname = std::env::var(HOSTNAME_VAR).ok();
+
+ config
+ }
+}
+
+/// Load an auth key from the `TS_AUTH_KEY` environment variable.
+pub fn auth_key_from_env() -> Option<String> {
+ std::env::var(AUTHKEY_VAR).ok()
+}
+
+/// Load key state from a path on the filesystem, or create a file with a new key state if
+/// one doesn't exist.
+///
+/// The `bad_format` argument allows you to specify whether an existing file should be
+/// overwritten if the contents can't be parsed.
+pub async fn load_key_file(
+ p: impl AsRef<Path>,
+ bad_format: BadFormatBehavior,
+) -> Result<PersistState, crate::Error> {
+ let p = p.as_ref();
+
+ tracing::trace!(key_file = %p.display(), "loading key file");
+
+ let key_file = load_or_init::<KeyFile>(&p, Default::default, bad_format).await?;
+ Ok(key_file.key_state)
+}
+
+#[derive(serde::Deserialize, serde::Serialize, Default)]
+struct KeyFile {
+ key_state: PersistState,
+}
+
+impl From<&Config> for ts_control::Config {
+ fn from(value: &Config) -> ts_control::Config {
+ ts_control::Config {
+ client_name: value.client_name.clone(),
+ hostname: value.requested_hostname.clone(),
+ server_url: value.control_server_url.clone(),
+ tags: value.requested_tags.clone(),
+ ephemeral: value.ephemeral,
+ }
+ }
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ key_state: Default::default(),
+ client_name: None,
+ control_server_url: ts_control::DEFAULT_CONTROL_SERVER.clone(),
+ requested_hostname: None,
+ requested_tags: vec![],
+ ephemeral: false,
+ }
+ }
+}
+
+/// What to do if the key file can't be parsed.
+///
+/// Default behavior: return an error.
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
+pub enum BadFormatBehavior {
+ /// Return an error.
+ #[default]
+ Error,
+
+ /// Overwrite the file with a newly-generated set of keys.
+ Overwrite,
+}
+
+/// Attempt to load a file from a path. If it doesn't exist, create it with the
+/// specified default value.
+#[tracing::instrument(skip_all, fields(?bad_format_behavior, path = %path.as_ref().display()))]
+async fn load_or_init<KeyState>(
+ path: impl AsRef<Path>,
+ default: impl FnOnce() -> KeyState,
+ bad_format_behavior: BadFormatBehavior,
+) -> Result<KeyState, crate::Error>
+where
+ KeyState: serde::Serialize + serde::de::DeserializeOwned,
+{
+ let path = path.as_ref();
+
+ tokio::fs::create_dir_all(path.parent().unwrap())
+ .await
+ .map_err(|e| {
+ tracing::error!(error = %e, "creating parent dirs for key file");
+ crate::Error::KeyFileWrite
+ })?;
+
+ match tokio::fs::read(path).await {
+ Ok(contents) => match serde_json::from_slice::<KeyState>(&contents) {
+ Ok(state) => return Ok(state),
+ Err(e) => match bad_format_behavior {
+ BadFormatBehavior::Error => {
+ tracing::error!(error = %e, "parsing key file");
+ return Err(crate::Error::KeyFileRead);
+ }
+ BadFormatBehavior::Overwrite => {
+ tracing::warn!(
+ error = %e,
+ config_file_contents_len = contents.len(),
+ "failed loading version from key file, overwriting",
+ );
+ }
+ },
+ },
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
+ Err(e) => {
+ tracing::error!(error = %e, path = %path.display(), "reading key file");
+ return Err(crate::Error::KeyFileRead);
+ }
+ }
+
+ let value = default();
+ try_write(path, &value).await?;
+ Ok(value)
+}
+
+async fn try_write(
+ path: impl AsRef<Path>,
+ value: &impl serde::Serialize,
+) -> Result<(), crate::Error> {
+ tokio::fs::write(
+ path,
+ serde_json::to_vec(value).map_err(|e| {
+ tracing::error!(error = %e, "serializing key state");
+ crate::Error::KeyFileWrite
+ })?,
+ )
+ .await
+ .map_err(|e| {
+ tracing::error!(error = %e, "saving key state");
+ crate::Error::KeyFileWrite
+ })?;
+
+ Ok(())
+}
diff --git a/deps/tailscale/src/error.rs b/deps/tailscale/src/error.rs
new file mode 100644
index 00000000..9c781ad6
--- /dev/null
+++ b/deps/tailscale/src/error.rs
@@ -0,0 +1,120 @@
+use std::fmt;
+
+use crate::netstack::Error as NetstackError;
+
+/// Errors that may occur while interacting with a device.
+#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
+pub enum Error {
+ /// An operation timed-out.
+ ///
+ /// This error can often be handled by retrying.
+ #[error("operation timed-out")]
+ Timeout,
+
+ /// A connection was reset.
+ ///
+ /// This error can often be handled by retrying.
+ #[error("connection reset")]
+ ConnectionReset,
+
+ /// An error reading or parsing the key file.
+ #[error("an error reading or parsing the key file")]
+ KeyFileRead,
+
+ /// An error writing out the key file.
+ #[error("an error writing out the key file")]
+ KeyFileWrite,
+
+ /// An error occurred which can not be anticipated or handled by a library user.
+ ///
+ /// This is likely due to a bug in our code or a rare and unexpected error.
+ ///
+ /// [`InternalErrorKind`] is intended to be informational (might be used to improve error reporting
+ /// in logs or to the end-user), rather then inspected during handling.
+ #[error("internal error ({0})")]
+ Internal(InternalErrorKind),
+}
+
+/// Informational detail on the kind of internal error.
+#[non_exhaustive]
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum InternalErrorKind {
+ /// Invalid socket state.
+ InvalidSocketState,
+ /// Response type mismatched to request type.
+ InternalResponseMismatch,
+ /// Channel closed.
+ InternalChannelClosed,
+ /// Handle to invalid TCP listener.
+ BadListenerHandle,
+ /// Handle to invalid socket.
+ BadSocketHandle,
+ /// Bad request.
+ BadRequest,
+ /// Buffer is full, cannot read in packet.
+ BufferFull,
+ /// Actor missing or shutdown.
+ Actor,
+}
+
+impl fmt::Display for InternalErrorKind {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ InternalErrorKind::InvalidSocketState => write!(f, "invalid socket state"),
+ InternalErrorKind::InternalResponseMismatch => {
+ write!(f, "response type mismatched to request type")
+ }
+ InternalErrorKind::InternalChannelClosed => write!(f, "channel closed"),
+ InternalErrorKind::BadListenerHandle => write!(f, "handle to invalid TCP listener"),
+ InternalErrorKind::BadSocketHandle => write!(f, "handle to invalid socket"),
+ InternalErrorKind::BadRequest => write!(f, "bad request"),
+ InternalErrorKind::BufferFull => write!(f, "buffer full"),
+ InternalErrorKind::Actor => write!(f, "actor missing or shutdown"),
+ }
+ }
+}
+
+impl From<crate::netstack::InternalErrorKind> for InternalErrorKind {
+ fn from(e: crate::netstack::InternalErrorKind) -> Self {
+ match e {
+ crate::netstack::InternalErrorKind::InvalidSocketState => {
+ InternalErrorKind::InvalidSocketState
+ }
+ crate::netstack::InternalErrorKind::InternalResponseMismatch => {
+ InternalErrorKind::InternalResponseMismatch
+ }
+ crate::netstack::InternalErrorKind::InternalChannelClosed => {
+ InternalErrorKind::InternalChannelClosed
+ }
+ crate::netstack::InternalErrorKind::BadListenerHandle => {
+ InternalErrorKind::BadListenerHandle
+ }
+ crate::netstack::InternalErrorKind::BadSocketHandle => {
+ InternalErrorKind::BadSocketHandle
+ }
+ crate::netstack::InternalErrorKind::BufferFull => InternalErrorKind::BufferFull,
+ _ => unreachable!(),
+ }
+ }
+}
+
+impl From<ts_runtime::Error> for Error {
+ fn from(value: ts_runtime::Error) -> Self {
+ match value.kind {
+ ts_runtime::ErrorKind::Timeout => Error::Timeout,
+ ts_runtime::ErrorKind::ActorGone
+ | ts_runtime::ErrorKind::MailboxFull
+ | ts_runtime::ErrorKind::ReplyErr => Error::Internal(InternalErrorKind::Actor),
+ }
+ }
+}
+
+impl From<NetstackError> for Error {
+ fn from(value: NetstackError) -> Self {
+ match value {
+ NetstackError::Internal(k) => Error::Internal(k.into()),
+ NetstackError::ConnectionReset => Error::ConnectionReset,
+ NetstackError::BadRequest(_) => Error::Internal(InternalErrorKind::BadRequest),
+ }
+ }
+}
diff --git a/deps/tailscale/src/lib.rs b/deps/tailscale/src/lib.rs
new file mode 100644
index 00000000..7b5b6aaa
--- /dev/null
+++ b/deps/tailscale/src/lib.rs
@@ -0,0 +1,435 @@
+//! A work-in-progress [Tailscale](https://tailscale.com/blog/how-tailscale-works) library.
+//!
+//! `tailscale` allows Rust programs to connect to a tailnet and exchange traffic with peers over
+//! TCP and UDP. It can communicate with other `tailscale`-based peers, `tailscaled` (the Tailscale
+//! Go client), `tsnet`, and `libtailscale` via public DERP servers.
+//!
+//! <div class="warning">
+//! `tailscale` is unstable and insecure.
+//!
+//! We welcome enthusiasm and interest, but please **do not** build production software using these
+//! libraries or rely on it for data privacy until we have a chance to batten down some hatches and
+//! complete a third-party audit.
+//!
+//! See the [Caveats section](#caveats) for more details.
+//! </div>
+//!
+//! For language bindings, see the following crates:
+//!
+//! - C: [ts_ffi](https://docs.rs/ts_ffi)
+//! - Python: [ts_python](https://docs.rs/ts_python)
+//! - Elixir: [ts_elixir](https://docs.rs/ts_elixir)
+//!
+//! For instructions on how to run tests, lints, etc., see [CONTRIBUTING.md]. For the high-level
+//! architecture and repository layout, see [ARCHITECTURE.md].
+//!
+//! ## Code Sample
+//!
+//! A simple UDP client that periodically sends messages to a tailnet peer at `100.64.0.1:5678`:
+//!
+//! ```no_run
+//! # use std::{
+//! # time::Duration,
+//! # net::Ipv4Addr,
+//! # error::Error,
+//! # };
+//! #
+//! # #[tokio::main]
+//! # async fn main() -> Result<(), Box<dyn Error>> {
+//! // Open a new connection to the tailnet
+//! let dev = tailscale::Device::new(
+//! &tailscale::Config::default_with_key_file("tsrs_keys.json").await?,
+//! Some("YOUR_AUTH_KEY_HERE".to_owned()),
+//! ).await?;
+//!
+//! // Bind a UDP socket on our tailnet IP, port 1234
+//! let sock = dev.udp_bind((dev.ipv4_addr().await?, 1234).into()).await?;
+//!
+//! // Send a packet containing "hello, world!" to 100.64.0.1:5678 once per second
+//! loop {
+//! sock.send_to((Ipv4Addr::new(100, 64, 0, 1), 5678).into(), b"hello, world!").await?;
+//! tokio::time::sleep(Duration::from_secs(1)).await;
+//! }
+//! # }
+//! ```
+//!
+//! Additional examples of using the `tailscale` crate can be found in the [`examples/`] directory.
+//!
+//! ## Using `tailscale`
+//!
+//! Under the hood, we use Tokio for our async runtime. You must also use Tokio, any kind and most
+//! configurations of Tokio runtimes should work, but there must be one available when you call any
+//! async API functions. The easiest way to do this is to use `#[tokio::main]`, see the
+//! [Tokio docs](https://docs.rs/tokio) for more information. In the future, we would like to limit
+//! our reliance on Tokio so that there are alternatives for users of other async runtimes.
+//!
+//! ## Caveats
+//!
+//! This software is still a work-in-progress! We are providing it in the open at this stage out of
+//! a belief in open-source and to see where the community runs with it, but please be aware of a
+//! few important considerations:
+//!
+//! - This implementation contains unaudited cryptography and hasn't undergone a comprehensive
+//! security analysis. Conservatively, assume there could be a critical security hole meaning
+//! anything you send or receive could be in the clear on the public Internet.
+//! - There are no compatibility guarantees at the moment. This is early-days software - we may
+//! break dependent code in order to get things right.
+//! - We currently rely on DERP relays for all communication. Direct connections via NAT
+//! holepunching will be a seamless upgrade in the future, but for now, this puts a cap on data
+//! throughput.
+//!
+//! ## Feature Flags
+//!
+//! - `axum`: enables the [`axum`] module, which enables you to run an [`axum` HTTP server] on top
+//! of a [`netstack::TcpListener`].
+//!
+//! ## Platform Support
+//!
+//! `tailscale` currently supports the following platforms:
+//!
+//! - Linux (x86_64 and ARM64)
+//! - macOS (ARM64)
+//!
+//! ## Component crates
+//!
+//! The following crates are part of the tailscale-rs project and are dependencies of this one. For
+//! many tasks, just this crate should be sufficient and these other crates are an implementation detail.
+//! There are other crates too, see [ARCHITECTURE.md]
+//! or the [GitHub repo](https://github.com/tailscale/tailscale-rs).
+//!
+//! - [ts_runtime](https://docs.rs/ts_runtime): for each API-level `Device`, the runtime uses an actor
+//! architecture to manage the lifecycle of the control client, data plane components, netstack, etc.
+//! A message bus passes updates and communications between these top-level actors.
+//! - [ts_netcheck](https://docs.rs/ts_netcheck): checks network availability and reports latency to
+//! DERP servers in different regions.
+//! - [ts_netstack_smoltcp](https://docs.rs/ts_netstack_smoltcp): a [smoltcp](https://docs.rs/smoltcp)-based
+//! network stack that processes Layer 3+ packets to/from the overlay network.
+//! - [ts_control](https://docs.rs/ts_control): control plane client that handles registration,
+//! authorization/authentication, configuration, and streaming updates.
+//! - [ts_dataplane](https://docs.rs/ts_dataplane): wires all the individual data plane functions together,
+//! flowing inbound and outbound packets through the components in the correct order.
+//! - [ts_tunnel](https://docs.rs/ts_tunnel): a partial implementation of the WireGuard specification
+//! that protects all data plane traffic, and is interoperable with other WireGuard clients, including Tailscale clients.
+//! - [ts_cli_util](https://docs.rs/ts_cli_util): helpers for writing command line tools and initializing
+//! logging, used in examples.
+//! - [ts_disco_protocol](https://docs.rs/ts_disco_protocol): incomplete implementation of Tailscale's
+//! discovery protocol (disco).
+//!
+//! [ARCHITECTURE.md]: https://github.com/tailscale/tailscale-rs/blob/main/ARCHITECTURE.md
+//! [CONTRIBUTING.md]: https://github.com/tailscale/tailscale-rs/blob/main/CONTRIBUTING.md
+//! [`examples/`]: https://github.com/tailscale/tailscale-rs/blob/main/examples/README.md
+//! [open an issue]: https://github.com/tailscale/tailscale-rs/issues
+//! [`axum` HTTP server]: https://docs.rs/axum/latest/axum/
+
+use std::{
+ net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
+ time::Duration,
+};
+
+#[doc(inline)]
+pub use config::Config;
+#[doc(inline)]
+pub use error::{Error, InternalErrorKind};
+#[doc(inline)]
+pub use ts_control::Node as NodeInfo;
+use ts_netstack_smoltcp::{CreateSocket, netcore::Channel};
+use ts_runtime::Spawn;
+
+#[cfg(feature = "axum")]
+pub mod axum;
+pub mod config;
+mod error;
+#[cfg(feature = "ssh")]
+pub mod ssh;
+
+/// How a program connects to a tailnet and communicates with peers.
+///
+/// The `Device` connects to the control plane, registers itself with the tailnet, and communicates
+/// with tailnet peers. Its tailnet identity is determined by the key state provided at
+/// construction-time.
+pub struct Device {
+ runtime: ts_runtime::ActorRef<ts_runtime::Runtime>,
+ channel: Channel,
+}
+
+/// Whether this node is authorized to join the tailnet.
+pub enum AuthState {
+ /// The node has successfully registered with the control server.
+ Authorized,
+
+ /// The node has contacted the control server but was not authorized to register. The user
+ /// should visit the contained URL in a browser to interactively authorize the machine.
+ ///
+ /// Authorization failure occurs when the control server doesn't recognize the node's keys
+ /// and no valid auth token was provided. This can happen in a previously-working configuration
+ /// if the node's authorization has expired, or if it was registered as ephemeral and has been
+ /// offline long enough to be deleted.
+ NotAuthorized(url::Url),
+}
+
+impl Device {
+ /// Create a device from the given [`Config`] and auth key.
+ ///
+ /// Internally, this will spawn multiple asynchronous actors onto a Tokio runtime.
+ ///
+ /// # Example
+ ///
+ /// ```rust,no_run
+ /// # #[tokio::main]
+ /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// # use tailscale::*;
+ /// let dev = Device::new(
+ /// &Config::default_with_key_file("tsrs_keys.json").await?,
+ /// Some("MY_AUTH_KEY".to_string()),
+ /// ).await?;
+ /// # Ok(()) }
+ /// ```
+ pub async fn new(config: &Config, auth_key: Option<String>) -> Result<Self, Error> {
+ tracing::warn!(
+ "tailscale-rs is early software in active development, expect more bugs and missing features than usual."
+ );
+
+ let keys = (&config.key_state).into();
+ let rt = ts_runtime::Runtime::spawn(ts_runtime::Config {
+ control_config: config.into(),
+ auth_key,
+ keys,
+ });
+
+ rt.wait_for_startup_result()
+ .await
+ .map_err(ts_runtime::Error::from)?;
+
+ let (channel,) = rt
+ .ask(ts_runtime::GetChannel)
+ .await
+ .map_err(ts_runtime::Error::from)?;
+
+ Ok(Self {
+ runtime: rt,
+ channel,
+ })
+ }
+
+ /// Report whether this node has been authorized to join the tailnet.
+ ///
+ /// If the node is not authorized, the returned [`AuthState::NotAuthorized`] contains a URL the
+ /// user can visit in the browser to interactively authorize the node.
+ ///
+ /// The future waits until the control server has been successfully contacted before returning,
+ /// i.e. it will remain pending in a network-down scenario.
+ pub async fn is_authorized(&self) -> Result<AuthState, Error> {
+ let ret = self
+ .runtime
+ .ask(ts_runtime::control_runner::AuthUrl)
+ .await
+ .map_err(ts_runtime::Error::from)?;
+
+ Ok(match ret {
+ Some(url) => AuthState::NotAuthorized(url),
+ None => AuthState::Authorized,
+ })
+ }
+
+ /// The current authorization state, or None while contacting control.
+ /// This does not queue a request waiting for registration to finish.
+ pub async fn current_auth_state(&self) -> Result<Option<AuthState>, Error> {
+ let state = self
+ .runtime
+ .ask(ts_runtime::CurrentAuthUrl)
+ .await
+ .map_err(ts_runtime::Error::from)?;
+ Ok(state.map(|url| match url {
+ Some(url) => AuthState::NotAuthorized(url),
+ None => AuthState::Authorized,
+ }))
+ }
+
+ /// Why control turned this device away, once it has: it does not try again by itself.
+ pub async fn registration_error(&self) -> Result<Option<String>, Error> {
+ self.runtime
+ .ask(ts_runtime::CurrentRegistrationError)
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// Our node from the latest netmap, or None before the first map.
+ /// Unlike self_node, this does not wait for control to send a map.
+ pub async fn current_self_node(&self) -> Result<Option<NodeInfo>, Error> {
+ self.runtime
+ .ask(ts_runtime::CurrentSelfNode)
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// Get this [`Device`]'s IPv4 tailnet address.
+ pub async fn ipv4_addr(&self) -> Result<Ipv4Addr, Error> {
+ self.runtime
+ .ask(ts_runtime::Ipv4)
+ .await
+ .map_err(ts_runtime::Error::from)?
+ .ok_or(Error::Internal(InternalErrorKind::Actor))
+ }
+
+ /// Get this [`Device`]'s IPv6 tailnet address.
+ pub async fn ipv6_addr(&self) -> Result<Ipv6Addr, Error> {
+ self.runtime
+ .ask(ts_runtime::Ipv6)
+ .await
+ .map_err(ts_runtime::Error::from)?
+ .ok_or(Error::Internal(InternalErrorKind::Actor))
+ }
+
+ /// Bind a UDP socket to the specified [`SocketAddr`].
+ pub async fn udp_bind(&self, socket_addr: SocketAddr) -> Result<netstack::UdpSocket, Error> {
+ self.channel.udp_bind(socket_addr).await.map_err(Into::into)
+ }
+
+ /// Bind a TCP listener to the specified [`SocketAddr`].
+ pub async fn tcp_listen(
+ &self,
+ socket_addr: SocketAddr,
+ ) -> Result<netstack::TcpListener, Error> {
+ self.channel
+ .tcp_listen(socket_addr)
+ .await
+ .map_err(Into::into)
+ }
+
+ /// Connect to a TCP socket at the remote address.
+ pub async fn tcp_connect(&self, remote: SocketAddr) -> Result<netstack::TcpStream, Error> {
+ let ip: IpAddr = match remote.is_ipv4() {
+ true => self.ipv4_addr().await?.into(),
+ false => self.ipv6_addr().await?.into(),
+ };
+
+ // TODO(npry): collision checking
+ let ephemeral_port = rand::random_range(49152..=u16::MAX);
+
+ self.channel
+ .tcp_connect((ip, ephemeral_port).into(), remote)
+ .await
+ .map_err(Into::into)
+ }
+
+ /// Get our node info.
+ pub async fn self_node(&self) -> Result<NodeInfo, Error> {
+ self.runtime
+ .ask(ts_runtime::SelfNode)
+ .await
+ .map_err(ts_runtime::Error::from)?
+ .ok_or(Error::Internal(InternalErrorKind::Actor))
+ }
+
+ /// Look up a peer by name.
+ pub async fn peer_by_name(&self, name: &str) -> Result<Option<NodeInfo>, Error> {
+ self.runtime
+ .ask(ts_runtime::peer_tracker::PeerByName {
+ name: name.to_string(),
+ })
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// Look up a peer by ip.
+ pub async fn peer_by_tailnet_ip(&self, ip: IpAddr) -> Result<Option<NodeInfo>, Error> {
+ self.runtime
+ .ask(ts_runtime::peer_tracker::PeerByTailnetIp { ip })
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// Every peer in the tailnet as of the latest update from control, or None before the first
+ /// netmap.
+ pub async fn peers(&self) -> Result<Option<Vec<NodeInfo>>, Error> {
+ self.runtime
+ .ask(ts_runtime::peer_tracker::AllPeers)
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// Look up the peer(s) with the most-specific route matches for `ip`.
+ pub async fn peers_with_route(&self, ip: IpAddr) -> Result<Vec<NodeInfo>, Error> {
+ self.runtime
+ .ask(ts_runtime::peer_tracker::PeerByAcceptedRoute { ip })
+ .await
+ .map_err(ts_runtime::Error::from)
+ .map_err(Into::into)
+ }
+
+ /// [`Device::shutdown`] for a device still shared: stops its runtime gracefully within
+ /// `timeout`, else violently, and reports which.
+ pub async fn stop(&self, timeout: Duration) -> bool {
+ if tokio::time::timeout(timeout, async {
+ let _ = self.runtime.stop_gracefully().await;
+ self.runtime.wait_for_shutdown().await;
+ })
+ .await
+ .is_err()
+ {
+ self.runtime.kill();
+ return false;
+ }
+ true
+ }
+
+ /// Attempt to gracefully shut down this device's runtime.
+ ///
+ /// Reports whether the device was fully shut down before the timeout. It is still shut
+ /// down if it timed out, just more violently and with potential resource leaks.
+ ///
+ /// If `timeout` is `None`, then shutdown will never time-out.
+ pub async fn shutdown(self, timeout: Option<Duration>) -> bool {
+ match timeout {
+ Some(timeout) => {
+ if tokio::time::timeout(timeout, async {
+ let _ = self.runtime.stop_gracefully().await;
+ self.runtime.wait_for_shutdown().await;
+ })
+ .await
+ .is_err()
+ {
+ self.runtime.kill();
+
+ return false;
+ }
+
+ true
+ }
+ None => {
+ let _ = self.runtime.stop_gracefully().await;
+ self.runtime.wait_for_shutdown().await;
+
+ true
+ }
+ }
+ }
+}
+
+/// Command-channel-driven userspace network stack.
+///
+/// This is an opinionated wrapper around [smoltcp](https://docs.rs/smoltcp) that provides an
+/// easier-to-integrate, more-portable API.
+pub mod netstack {
+ #[doc(inline)]
+ pub use ts_netstack_smoltcp::netcore::Error;
+ #[doc(inline)]
+ pub use ts_netstack_smoltcp::netcore::InternalErrorKind;
+ #[doc(inline)]
+ pub use ts_netstack_smoltcp::netsock::{TcpListener, TcpStream, UdpSocket};
+}
+
+/// Tailscale cryptographic key types.
+pub mod keys {
+ #[doc(inline)]
+ pub use ts_keys::{
+ DiscoKeyPair, DiscoPrivateKey, DiscoPublicKey, Export, MachineKeyPair, MachinePrivateKey,
+ MachinePublicKey, NetworkLockKeyPair, NetworkLockPrivateKey, NetworkLockPublicKey,
+ NodeKeyPair, NodePrivateKey, NodePublicKey, NodeState, PersistState,
+ };
+}
diff --git a/deps/tailscale/src/ssh/channel_server.rs b/deps/tailscale/src/ssh/channel_server.rs
new file mode 100644
index 00000000..7230eef7
--- /dev/null
+++ b/deps/tailscale/src/ssh/channel_server.rs
@@ -0,0 +1,291 @@
+use std::{collections::HashMap, marker::PhantomData, net::SocketAddr, sync::Arc};
+
+use russh::{
+ Channel, ChannelId, Pty, Sig,
+ server::{Auth, Handle, Msg, Session},
+};
+use tokio::{
+ sync::{mpsc, mpsc::UnboundedSender},
+ task::JoinSet,
+};
+
+use crate::{Device, ssh::TailnetServer};
+
+type Request = (ChannelId, ChannelEvent);
+
+/// Handler for a channel session.
+pub trait ChannelHandler: Sized {
+ /// Error this handler produces.
+ type Error: Into<std::io::Error> + std::error::Error;
+
+ /// Construct a new per-channel handler.
+ fn new(
+ handle: tokio::runtime::Handle,
+ channel_id: ChannelId,
+ session: Handle,
+ dev: Arc<Device>,
+ ) -> Result<Self, Self::Error>;
+
+ /// Handle an event from the channel.
+ fn handle_event(
+ &mut self,
+ event: &ChannelEvent,
+ ) -> impl Future<Output = Result<(), Self::Error>> + Send;
+}
+
+/// Implementation of [`russh::server::Handler`] which provides per-channel session
+/// handlers using a parametric [`ChannelHandler`].
+///
+/// Primary motivation is to support custom console or TUI sessions over tailnet SSH
+/// connections.
+///
+/// # Authentication and authorization
+///
+/// **WARNING**: [`ChannelServer`] currently does _no_ authentication or authorization checks
+/// internally. It is still protected by the policy file's network-layer rules, but it does
+/// not currently support the `ssh` policy block or application capabilities.
+pub struct ChannelServer<H> {
+ channel_state: HashMap<ChannelId, ChannelState>,
+ remote: SocketAddr,
+ dev: Arc<Device>,
+ _handler: PhantomSend<H>,
+}
+
+struct PhantomSend<H>(PhantomData<H>);
+
+// SAFETY: H is a phantom type, it's never actually sent
+unsafe impl<H> Send for PhantomSend<H> {}
+
+#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
+#[error("no such channel")]
+struct NoChannel;
+
+/// State of a channel in [`ChannelServer`].
+struct ChannelState {
+ channel: ChannelId,
+ tx: UnboundedSender<Request>,
+ _joinset: JoinSet<()>,
+}
+
+impl ChannelState {
+ fn send(&self, event: ChannelEvent) {
+ if self.tx.send((self.channel, event)).is_err() {
+ tracing::error!(channel = %self.channel, "failed to send event");
+ }
+ }
+}
+
+impl<H> ChannelServer<H> {
+ fn get_channel(
+ &mut self,
+ id: ChannelId,
+ ) -> Result<&mut ChannelState, Box<dyn std::error::Error + Send + Sync + 'static>> {
+ self.channel_state.get_mut(&id).ok_or(Box::new(NoChannel))
+ }
+}
+
+impl<H> TailnetServer for ChannelServer<H> {
+ fn new_client(dev: Arc<Device>, addr: SocketAddr) -> Self {
+ Self {
+ channel_state: Default::default(),
+ dev,
+ remote: addr,
+ _handler: PhantomSend(PhantomData),
+ }
+ }
+}
+
+/// An event that may be generated by a channel connected to a [`ChannelServer`].
+#[derive(Debug, Clone)]
+pub enum ChannelEvent {
+ /// Data was received over the channel.
+ Data(Vec<u8>),
+ /// A resize event occurred.
+ Resize {
+ /// The new width of the tty.
+ width: u16,
+ /// The new height of the tty.
+ height: u16,
+ },
+ /// A signal was sent over the channel.
+ Signal(Sig),
+ /// The channel was closed.
+ Close,
+ /// The channel received EOF.
+ Eof,
+}
+
+impl<H> russh::server::Handler for ChannelServer<H>
+where
+ H: ChannelHandler + Send,
+ H::Error: Send,
+{
+ type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
+
+ #[tracing::instrument(skip_all, fields(user = %_user, remote = ?self.remote))]
+ async fn auth_none(&mut self, _user: &str) -> Result<Auth, Self::Error> {
+ let Some(peer) = self.dev.peer_by_tailnet_ip(self.remote.ip()).await? else {
+ tracing::error!("remote ip does not match a known peer");
+ return Ok(Auth::reject());
+ };
+
+ tracing::debug!(?peer, "accept ssh connection");
+
+ Ok(Auth::Accept)
+ }
+
+ async fn channel_open_session(
+ &mut self,
+ channel: Channel<Msg>,
+ session: &mut Session,
+ ) -> Result<bool, Self::Error> {
+ tracing::debug!(channel = ?channel.id(), "new session");
+
+ let (tx, mut rx) = mpsc::unbounded_channel::<Request>();
+ let mut joinset = JoinSet::new();
+
+ let (channel_id, session_handle) = (channel.id(), session.handle());
+ let dev = self.dev.clone();
+
+ joinset.spawn(async move {
+ let rt = tokio::runtime::Handle::current();
+
+ let mut handler = match H::new(rt, channel_id, session_handle.clone(), dev) {
+ Ok(handler) => handler,
+ Err(e) => {
+ let e = e.into();
+ tracing::error!(error = %e, %channel_id, "spawning channel handler");
+
+ if session_handle.close(channel_id).await.is_err() {
+ tracing::error!("failed closing channel after handler init error");
+ };
+
+ return;
+ }
+ };
+
+ while let Some((_channel, evt)) = rx.recv().await {
+ let result = handler.handle_event(&evt).await;
+
+ if let Err(e) = result {
+ let e = e.into();
+ tracing::error!(error = %e, %channel_id, ?evt, "handling event");
+
+ if session_handle.close(channel_id).await.is_err() {
+ tracing::error!("failed closing channel after event handler error");
+ };
+
+ break;
+ }
+ }
+
+ tracing::debug!(?channel_id, "closed");
+ });
+
+ self.channel_state.insert(
+ channel.id(),
+ ChannelState {
+ channel: channel.id(),
+ tx,
+ _joinset: joinset,
+ },
+ );
+
+ session.channel_success(channel.id())?;
+
+ Ok(true)
+ }
+
+ async fn channel_close(
+ &mut self,
+ channel: ChannelId,
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ tracing::trace!(?channel, "session closed");
+
+ self.get_channel(channel)?.send(ChannelEvent::Close);
+ self.channel_state.remove(&channel);
+
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+
+ async fn signal(
+ &mut self,
+ channel: ChannelId,
+ signal: Sig,
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ self.get_channel(channel)?
+ .send(ChannelEvent::Signal(signal));
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+
+ async fn data(
+ &mut self,
+ channel: ChannelId,
+ data: &[u8],
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ self.get_channel(channel)?
+ .send(ChannelEvent::Data(data.into()));
+
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+
+ async fn channel_eof(
+ &mut self,
+ channel: ChannelId,
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ self.get_channel(channel)?.send(ChannelEvent::Eof);
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+
+ async fn window_change_request(
+ &mut self,
+ channel: ChannelId,
+ col_width: u32,
+ row_height: u32,
+ _: u32,
+ _: u32,
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ self.get_channel(channel)?.send(ChannelEvent::Resize {
+ width: col_width as _,
+ height: row_height as _,
+ });
+
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+
+ async fn pty_request(
+ &mut self,
+ channel: ChannelId,
+ _: &str,
+ col_width: u32,
+ row_height: u32,
+ _: u32,
+ _: u32,
+ _: &[(Pty, u32)],
+ session: &mut Session,
+ ) -> Result<(), Self::Error> {
+ self.get_channel(channel)?.send(ChannelEvent::Resize {
+ width: col_width as _,
+ height: row_height as _,
+ });
+
+ session.channel_success(channel)?;
+
+ Ok(())
+ }
+}
diff --git a/deps/tailscale/src/ssh/channel_write.rs b/deps/tailscale/src/ssh/channel_write.rs
new file mode 100644
index 00000000..0b065276
--- /dev/null
+++ b/deps/tailscale/src/ssh/channel_write.rs
@@ -0,0 +1,46 @@
+use std::io::{ErrorKind, Write};
+
+use bytes::BytesMut;
+
+/// Wrapper that implements [`Write`] for a [`russh`] channel.
+///
+/// Needed to support a `crossterm` terminal driver, which isn't async-aware.
+///
+/// This is basically `tokio_util::SyncIoBridge`, except that [`russh::server::Handle`]
+/// doesn't natively support [`tokio::io::AsyncWrite`], so it's all just done here.
+pub struct ChannelWrite {
+ rt: tokio::runtime::Handle,
+ buf: BytesMut,
+ channel_id: russh::ChannelId,
+ handle: russh::server::Handle,
+}
+
+impl ChannelWrite {
+ pub fn new(
+ runtime: tokio::runtime::Handle,
+ handle: russh::server::Handle,
+ channel_id: russh::ChannelId,
+ ) -> Self {
+ Self {
+ rt: runtime,
+ buf: BytesMut::new(),
+ handle,
+ channel_id,
+ }
+ }
+}
+
+impl Write for ChannelWrite {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.buf.extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ tokio::task::block_in_place(|| {
+ self.rt
+ .block_on(self.handle.data(self.channel_id, self.buf.split()))
+ })
+ .map_err(|_| ErrorKind::BrokenPipe.into())
+ }
+}
diff --git a/deps/tailscale/src/ssh/mod.rs b/deps/tailscale/src/ssh/mod.rs
new file mode 100644
index 00000000..13639ca4
--- /dev/null
+++ b/deps/tailscale/src/ssh/mod.rs
@@ -0,0 +1,112 @@
+//! Support for tailnet-native, in-process SSH servers.
+//!
+//! # Overview
+//!
+//! This module (`tailscale::ssh`) holds helpers for running SSH servers on the tailnet
+//! using [`russh`]. They delegate their functionality to the [`Handler`] trait, which is
+//! `russh`'s notion of a _connection_ handler, i.e. a single incoming TCP connection gets
+//! a single instance of [`Handler`].
+//!
+//! ## Channels
+//!
+//! SSH has a nested notion of channels, which are multiplexed over a single connection.
+//! The terminal session you open over a normal machine-to-machine ssh connection runs in a
+//! channel, and in principle, you can have multiple channels open on the same connection.
+//!
+//! The `channel_server` module provides a [`ChannelServer`] type that separates out the
+//! per-channel handler logic from `russh`'s monolithic [`Handler`]. Channel handler logic
+//! is supported here by [`ChannelHandler`], which is passed into [`ChannelServer`] and
+//! processes a [`ChannelEvent`] stream for each channel that's opened.
+//!
+//! ## Terminal applications
+//!
+//! Support for building per-channel terminal application is provided by [`RatatuiTerm`],
+//! which implements [`ChannelHandler`] to drive a
+//! [`ratatui::Terminal`][::ratatui::Terminal]. The user provides an implementation of
+//! [`RatatuiApp`] that consumes input data and supports draws to the screen, and the
+//! [`RatatuiTerm`] drives it automatically.
+
+pub extern crate russh;
+
+use std::{fmt::Debug, net::SocketAddr, sync::Arc};
+
+use russh::server::Handler;
+
+mod channel_server;
+mod channel_write;
+mod ratatui;
+
+pub use channel_server::{ChannelEvent, ChannelHandler, ChannelServer};
+pub use ratatui::{RatatuiApp, RatatuiEnv, RatatuiTerm};
+
+/// Trait to construct a new [`Handler`] from a Tailscale [`Device`][crate::Device] and
+/// the address of a connecting client.
+///
+/// Rephrasing of [`russh::server::Server`] that includes the Tailscale device as an
+/// argument and skips the support for off-tailnet IP and Unix sockets.
+pub trait TailnetServer {
+ /// Construct a new handler.
+ fn new_client(dev: Arc<crate::Device>, addr: SocketAddr) -> Self;
+}
+
+impl crate::Device {
+ /// Serve an ssh service on the given TCP address.
+ ///
+ /// This is a minimal helper that just wires up the relevant pieces. All the
+ /// authentication and actual SSH server logic must be implemented by the caller in
+ /// the `TailnetServer` (`H`) and configured by `config`.
+ pub async fn serve_ssh<H>(
+ self: Arc<Self>,
+ config: russh::server::Config,
+ listen_addr: SocketAddr,
+ ) -> Result<(), crate::Error>
+ where
+ H: TailnetServer + Handler + Send + 'static,
+ H::Error: Debug,
+ {
+ let config = Arc::new(config);
+ let listener = self.tcp_listen(listen_addr).await?;
+
+ tracing::info!(%listen_addr, "ssh server listening");
+
+ loop {
+ let conn = listener.accept().await?;
+
+ let handler = H::new_client(self.clone(), conn.remote_addr());
+ let config = config.clone();
+
+ tokio::task::spawn(async move {
+ let sess = match russh::server::run_stream(config, conn, handler).await {
+ Ok(sess) => sess,
+ Err(e) => {
+ tracing::error!(error = ?e, "establishing session");
+ return;
+ }
+ };
+
+ match sess.await {
+ Ok(()) => {}
+ Err(e) => {
+ tracing::error!(error = ?e, "running ssh session");
+ }
+ }
+ });
+ }
+ }
+
+ /// Serve an SSH TUI service on the given TCP address.
+ ///
+ /// Wrapper around [`serve_ssh`][crate::Device::serve_ssh] to specifically use
+ /// [`ChannelServer`] around a [`RatatuiTerm`] using `App`.
+ pub async fn serve_ssh_tui<App>(
+ self: Arc<Self>,
+ config: russh::server::Config,
+ listen_addr: SocketAddr,
+ ) -> Result<(), crate::Error>
+ where
+ App: RatatuiApp + Default + Send + 'static,
+ {
+ self.serve_ssh::<ChannelServer<RatatuiTerm<App>>>(config, listen_addr)
+ .await
+ }
+}
diff --git a/deps/tailscale/src/ssh/ratatui.rs b/deps/tailscale/src/ssh/ratatui.rs
new file mode 100644
index 00000000..6559f191
--- /dev/null
+++ b/deps/tailscale/src/ssh/ratatui.rs
@@ -0,0 +1,160 @@
+use std::sync::Arc;
+
+use ratatui::{Terminal, TerminalOptions, Viewport, backend::CrosstermBackend, layout::Rect};
+use russh::{ChannelId, Sig, server::Handle};
+
+use crate::{
+ Device,
+ ssh::{ChannelEvent, ChannelHandler, channel_write::ChannelWrite},
+};
+
+type Backend = CrosstermBackend<ChannelWrite>;
+
+/// Terminal environment for [`RatatuiApp`].
+pub trait RatatuiEnv {
+ /// Request that the terminal close.
+ fn close(&self) -> impl Future<Output = ()> + Send;
+
+ /// Get a reference to the Tailscale [`Device`] this is running in.
+ fn tailscale(&self) -> &Device;
+}
+
+/// A [`ratatui`] application designed to be driven by a
+/// [`ChannelServer`][crate::ssh::ChannelServer].
+pub trait RatatuiApp {
+ /// Process new input from the channel.
+ fn input(
+ &mut self,
+ data: &[u8],
+ env: impl RatatuiEnv + Send,
+ ) -> impl Future<Output = ()> + Send;
+
+ /// Render the app to the [`ratatui::Frame`].
+ fn draw(&mut self, frame: &mut ratatui::Frame);
+}
+
+/// A [`ChannelHandler`] that runs a [`RatatuiApp`].
+pub struct RatatuiTerm<Io> {
+ channel_id: ChannelId,
+ session: Handle,
+ term: Terminal<Backend>,
+ dev: Arc<Device>,
+ io: Io,
+}
+
+struct Env<'a> {
+ channel_id: ChannelId,
+ session: &'a Handle,
+ dev: &'a Device,
+}
+
+impl RatatuiEnv for Env<'_> {
+ async fn close(&self) {
+ if self.session.close(self.channel_id).await.is_err() {
+ tracing::error!("channel closed while closing ratatui app");
+ }
+ }
+
+ fn tailscale(&self) -> &Device {
+ self.dev
+ }
+}
+
+impl<Io> RatatuiTerm<Io>
+where
+ Io: RatatuiApp,
+{
+ fn refresh(&mut self) -> std::io::Result<()> {
+ self.term.clear()?;
+ self.draw()?;
+
+ Ok(())
+ }
+
+ fn draw(&mut self) -> std::io::Result<()> {
+ self.term.draw(|frame| self.io.draw(frame))?;
+
+ Ok(())
+ }
+}
+
+impl<Io> ChannelHandler for RatatuiTerm<Io>
+where
+ Io: RatatuiApp + Default + Send,
+{
+ type Error = std::io::Error;
+
+ fn new(
+ rt: tokio::runtime::Handle,
+ channel_id: ChannelId,
+ session: Handle,
+ dev: Arc<Device>,
+ ) -> Result<Self, Self::Error> {
+ let mut term = Self {
+ term: make_term(rt, session.clone(), channel_id)?,
+ dev,
+ channel_id,
+ session,
+ io: Default::default(),
+ };
+ term.refresh()?;
+
+ Ok(term)
+ }
+
+ async fn handle_event(&mut self, event: &ChannelEvent) -> Result<(), Self::Error> {
+ match event {
+ ChannelEvent::Data(d) => {
+ self.io
+ .input(
+ d,
+ Env {
+ dev: &self.dev,
+ channel_id: self.channel_id,
+ session: &self.session,
+ },
+ )
+ .await;
+
+ self.draw()?;
+ }
+ ChannelEvent::Resize { width, height } => {
+ self.term.resize(Rect::new(0, 0, *width, *height))?;
+ self.draw()?;
+ }
+ ChannelEvent::Eof
+ | ChannelEvent::Signal(Sig::ABRT | Sig::QUIT | Sig::TERM | Sig::KILL | Sig::INT) => {
+ tracing::debug!(?event, channel_id = %self.channel_id, "close channel");
+
+ if self.session.close(self.channel_id).await.is_err() {
+ tracing::error!("session already shut down");
+
+ return Err(std::io::ErrorKind::BrokenPipe.into());
+ }
+ }
+ ChannelEvent::Signal(sig) => {
+ tracing::debug!(?sig, "unhandled signal");
+ }
+ ChannelEvent::Close => {
+ self.term.clear()?;
+ }
+ }
+
+ Ok(())
+ }
+}
+
+fn make_term(
+ rt: tokio::runtime::Handle,
+ session_handle: Handle,
+ channel_id: ChannelId,
+) -> Result<Terminal<Backend>, <Backend as ratatui::backend::Backend>::Error> {
+ let terminal_handle = ChannelWrite::new(rt, session_handle, channel_id);
+ let backend = CrosstermBackend::new(terminal_handle);
+
+ let options = TerminalOptions {
+ viewport: Viewport::Fixed(Rect::default()),
+ };
+
+ Terminal::with_options(backend, options)
+}
diff --git a/deps/ts_control/Cargo.toml b/deps/ts_control/Cargo.toml
new file mode 100644
index 00000000..884c527c
--- /dev/null
+++ b/deps/ts_control/Cargo.toml
@@ -0,0 +1,169 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2024"
+rust-version = "1.92.0"
+name = "ts_control"
+version = "0.6.1"
+build = false
+autolib = false
+autobins = false
+autoexamples = false
+autotests = false
+autobenches = false
+description = "tailscale control client"
+readme = "README.md"
+keywords = [
+ "tailscale",
+ "control",
+]
+categories = [
+ "api-bindings",
+ "network-programming",
+]
+license = "BSD-3-Clause"
+repository = "https://github.com/tailscale/tailscale-rs"
+
+[features]
+insecure-derp = ["ts_derp/insecure-for-tests"]
+insecure-keyfetch = []
+
+[lib]
+name = "ts_control"
+path = "src/lib.rs"
+
+[dependencies.bytes]
+version = "1"
+
+[dependencies.chrono]
+version = "0.4"
+features = [
+ "clock",
+ "serde",
+]
+default-features = false
+
+[dependencies.futures-util]
+version = "0.3"
+default-features = false
+
+[dependencies.gethostname]
+version = "1.1"
+
+[dependencies.ipnet]
+version = "2.12"
+features = ["serde"]
+default-features = false
+
+[dependencies.lazy_static]
+version = "1.5"
+
+[dependencies.pin-project-lite]
+version = "0.2"
+
+[dependencies.serde]
+version = "1"
+features = ["derive"]
+
+[dependencies.serde_json]
+version = "1"
+
+[dependencies.thiserror]
+version = "2"
+default-features = false
+
+[dependencies.tokio]
+version = "1"
+features = [
+ "rt-multi-thread",
+ "macros",
+]
+default-features = false
+
+[dependencies.tokio-util]
+version = "0.7"
+features = ["time"]
+default-features = false
+
+[dependencies.tracing]
+version = "0.1"
+features = ["attributes"]
+default-features = false
+
+[dependencies.ts_bitset]
+version = "0.6.1"
+default-features = false
+
+[dependencies.ts_capabilityversion]
+version = "0.6.1"
+features = ["serde"]
+
+[dependencies.ts_control_noise]
+version = "0.6.1"
+
+[dependencies.ts_control_serde]
+version = "0.6.1"
+
+[dependencies.ts_derp]
+version = "0.6.1"
+
+[dependencies.ts_dynbitset]
+version = "0.6.1"
+
+[dependencies.ts_http_util]
+version = "0.6.1"
+
+[dependencies.ts_keys]
+version = "0.6.1"
+
+[dependencies.ts_packet]
+version = "0.6.1"
+
+[dependencies.ts_packetfilter]
+version = "0.6.1"
+
+[dependencies.ts_packetfilter_state]
+version = "0.6.1"
+
+[dependencies.ts_tls_util]
+version = "0.6.1"
+
+[dependencies.url]
+version = "2"
+features = [
+ "serde",
+ "std",
+]
+default-features = false
+
+[dependencies.zerocopy]
+version = "0.8"
+features = ["derive"]
+
+[lints.clippy]
+multiple_crate_versions = "allow"
+too_many_arguments = "allow"
+
+[lints.clippy.cargo]
+level = "warn"
+priority = -1
+
+[lints.rust]
+closure_returning_async_block = "warn"
+let-underscore = "warn"
+missing_docs = "warn"
+
+[lints.rustdoc]
+bare_urls = "deny"
+broken_intra_doc_links = "deny"
+invalid_html_tags = "deny"
+private_intra_doc_links = "deny"
diff --git a/deps/ts_control/LICENSE b/deps/ts_control/LICENSE
new file mode 100644
index 00000000..adcd4c5b
--- /dev/null
+++ b/deps/ts_control/LICENSE
@@ -0,0 +1,28 @@
+BSD 3-Clause License
+
+Copyright (c) 2026 Tailscale Inc & contributors.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/deps/ts_control/PATENTS b/deps/ts_control/PATENTS
new file mode 100644
index 00000000..560a2b8f
--- /dev/null
+++ b/deps/ts_control/PATENTS
@@ -0,0 +1,24 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Tailscale Inc. as part of the Tailscale project.
+
+Tailscale Inc. hereby grants to You a perpetual, worldwide,
+non-exclusive, no-charge, royalty-free, irrevocable (except as stated
+in this section) patent license to make, have made, use, offer to
+sell, sell, import, transfer and otherwise run, modify and propagate
+the contents of this implementation of Tailscale, where such license
+applies only to those patent claims, both currently owned or
+controlled by Tailscale Inc. and acquired in the future, licensable
+by Tailscale Inc. that are necessarily infringed by this
+implementation of Tailscale. This grant does not include claims that
+would be infringed only as a consequence of further modification of
+this implementation. If you or your agent or exclusive licensee
+institute or order or agree to the institution of patent litigation
+against any entity (including a cross-claim or counterclaim in a
+lawsuit) alleging that this implementation of Tailscale or any code
+incorporated within this implementation of Tailscale constitutes
+direct or contributory patent infringement, or inducement of patent
+infringement, then any patent rights granted to you under this License
+for this implementation of Tailscale shall terminate as of the date
+such litigation is filed.
diff --git a/deps/ts_control/src/client/connect.rs b/deps/ts_control/src/client/connect.rs
new file mode 100644
index 00000000..96e9bf29
--- /dev/null
+++ b/deps/ts_control/src/client/connect.rs
@@ -0,0 +1,389 @@
+use alloc::string::String;
+use core::{fmt, str::FromStr};
+
+use bytes::Bytes;
+use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};
+use ts_capabilityversion::CapabilityVersion;
+use ts_http_util::{BytesBody, ClientExt, EmptyBody, HeaderName, HeaderValue, Http2, ResponseExt};
+use ts_keys::{MachineKeyPair, MachinePublicKey};
+use url::Url;
+use zerocopy::network_endian::U32;
+
+use crate::client::prefixed_reader::PrefixedReader;
+
+const CHALLENGE_MAGIC: [u8; 5] = [0xFF, 0xFF, 0xFF, b'T', b'S'];
+const HANDSHAKE_HEADER_KEY: &str = "X-Tailscale-Handshake";
+const MAX_CHALLENGE_LENGTH: usize = 1024;
+const UPGRADE_HEADER_VALUE: &str = "tailscale-control-protocol";
+
+lazy_static::lazy_static! {
+ /// The version of the control protocol this node will use to communicate with the control
+ /// plane; corresponds to the node's capability version.
+ pub static ref CONTROL_PROTOCOL_VERSION: String = format!("Tailscale Control Protocol v{}", CapabilityVersion::CURRENT);
+}
+
+/// Error encountered while connecting to the control server.
+#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
+pub enum ConnectionError {
+ /// An internal error occurred.
+ #[error("internal error during connection: {0}")]
+ Internal(InternalErrorKind),
+ /// Connection failed due to network conditions; retrying later may solve the problem.
+ #[error("Network error")]
+ NetworkError,
+}
+
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum InternalErrorKind {
+ Url,
+ Http,
+ SerDe,
+ MessageFormat,
+ Io,
+ ChallengeLength,
+ NoiseHandshake,
+}
+
+impl fmt::Display for InternalErrorKind {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ InternalErrorKind::Url => write!(f, "URL parsing error"),
+ InternalErrorKind::Http => write!(f, "unsuccessful HTTP request or upgrade"),
+ InternalErrorKind::SerDe => write!(f, "serialization/deserialization error"),
+ InternalErrorKind::MessageFormat => write!(f, "message format error"),
+ InternalErrorKind::Io => write!(f, "I/O error"),
+ InternalErrorKind::ChallengeLength => write!(f, "challenge too long"),
+ InternalErrorKind::NoiseHandshake => write!(f, "error in Noise handshake"),
+ }
+ }
+}
+
+impl ConnectionError {
+ fn io_error(field: &'static str, stage: &'static str, err: std::io::Error) -> Self {
+ tracing::error!("could not read {field} from {stage} message: {err}");
+
+ if crate::is_network_error(&err) {
+ ConnectionError::NetworkError
+ } else {
+ ConnectionError::Internal(InternalErrorKind::Io)
+ }
+ }
+}
+
+impl From<serde_json::Error> for ConnectionError {
+ fn from(error: serde_json::Error) -> Self {
+ tracing::error!(%error, "deserialization error");
+ ConnectionError::Internal(InternalErrorKind::SerDe)
+ }
+}
+
+impl From<ts_http_util::Error> for ConnectionError {
+ fn from(error: ts_http_util::Error) -> Self {
+ tracing::error!(%error, "http error connecting to control server");
+
+ if crate::http_error_is_recoverable(error) {
+ ConnectionError::NetworkError
+ } else {
+ ConnectionError::Internal(InternalErrorKind::Http)
+ }
+ }
+}
+
+impl From<url::ParseError> for ConnectionError {
+ fn from(error: url::ParseError) -> Self {
+ tracing::error!(%error, "bad URL");
+ ConnectionError::Internal(InternalErrorKind::Url)
+ }
+}
+
+impl From<ts_control_noise::Error> for ConnectionError {
+ fn from(error: ts_control_noise::Error) -> Self {
+ match error {
+ ts_control_noise::Error::BadFormat => {
+ ConnectionError::Internal(InternalErrorKind::MessageFormat)
+ }
+ ts_control_noise::Error::HandshakeFailed => {
+ ConnectionError::Internal(InternalErrorKind::NoiseHandshake)
+ }
+ ts_control_noise::Error::Io(error) => {
+ tracing::error!(%error, "IO error in Noise communication");
+ ConnectionError::Internal(InternalErrorKind::Io)
+ }
+ }
+ }
+}
+
+impl From<ConnectionError> for crate::Error {
+ fn from(e: ConnectionError) -> Self {
+ match e {
+ ConnectionError::Internal(k) => {
+ crate::Error::Internal(k.into(), crate::Operation::ConnectToControlServer)
+ }
+ ConnectionError::NetworkError => {
+ crate::Error::NetworkError(crate::Operation::ConnectToControlServer)
+ }
+ }
+ }
+}
+
+impl From<InternalErrorKind> for crate::InternalErrorKind {
+ fn from(e: InternalErrorKind) -> Self {
+ match e {
+ InternalErrorKind::Url => crate::InternalErrorKind::Url,
+ InternalErrorKind::Http => crate::InternalErrorKind::Http,
+ InternalErrorKind::SerDe => crate::InternalErrorKind::SerDe,
+ InternalErrorKind::MessageFormat => crate::InternalErrorKind::MessageFormat,
+ InternalErrorKind::Io => crate::InternalErrorKind::Io,
+ InternalErrorKind::ChallengeLength => crate::InternalErrorKind::Challenge,
+ InternalErrorKind::NoiseHandshake => crate::InternalErrorKind::NoiseHandshake,
+ }
+ }
+}
+
+#[derive(serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "camelCase")]
+struct ControlPublicKeys {
+ legacy_public_key: MachinePublicKey,
+ public_key: MachinePublicKey,
+}
+
+impl fmt::Display for ControlPublicKeys {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.public_key)
+ }
+}
+
+/// Establish a connection to the control server.
+///
+/// Does not make use of use [`ControlDialer`][crate::ControlDialer] and so should not be used for
+/// long-duration connections that may need to reconnect (e.g. listening for netmap changes).
+#[tracing::instrument(skip_all, fields(%control_url), err)]
+pub async fn connect(
+ control_url: &Url,
+ machine_keys: &MachineKeyPair,
+) -> Result<Http2<BytesBody>, ConnectionError> {
+ let h1_client = connect_h1(control_url).await?;
+
+ let control_public_key = fetch_control_key(control_url).await?;
+
+ let (handshake, init_msg) = ts_control_noise::Handshake::initialize(
+ &CONTROL_PROTOCOL_VERSION,
+ machine_keys,
+ &control_public_key,
+ CapabilityVersion::CURRENT,
+ );
+
+ let conn = upgrade_ts2021(control_url, &init_msg, handshake, machine_keys, h1_client).await?;
+
+ // The early payload (challenge packet) is optional. The server may send
+ // the magic prefix [FF FF FF 'T' 'S'] followed by a JSON challenge, or it
+ // may go straight to HTTP/2 (whose first frame starts with different bytes).
+ // Read the first 9 bytes (same size as an HTTP/2 frame header) and check.
+ let conn = read_challenge_packet(conn).await?;
+
+ let h2_conn = ts_http_util::http2::connect(conn).await?;
+ Ok(h2_conn)
+}
+
+/// Connect an HTTP/1.1 client to the control server, using TLS for https://
+/// URLs and plain TCP for http:// URLs.
+async fn connect_h1(url: &Url) -> Result<ts_http_util::Http1<EmptyBody>, ConnectionError> {
+ if url.scheme() == "http" {
+ Ok(ts_http_util::http1::connect_tcp(url).await?)
+ } else {
+ Ok(ts_http_util::http1::connect_tls(url).await?)
+ }
+}
+
+/// Fetch the control server's [`MachinePublicKey`], which is used to encrypt the Noise connection
+/// to the control server.
+///
+/// If the `insecure-keyfetch` feature flag is not enabled, this forces the key url scheme to HTTPS
+/// and validates the server's certificate. This is a critical, load-bearing requirement for
+/// security: if the control server key is MITMed, it's game over. `insecure-keyfetch` is provided
+/// ONLY for integration testing, where it's not practical to issue valid certs.
+#[tracing::instrument(skip_all, fields(%control_url), ret, err, level = "trace")]
+pub async fn fetch_control_key(control_url: &Url) -> Result<MachinePublicKey, ConnectionError> {
+ let mut key_url = control_url.join("/key")?;
+
+ #[cfg(not(feature = "insecure-keyfetch"))]
+ key_url.set_scheme("https").unwrap();
+
+ if key_url.scheme() == "http" {
+ tracing::warn!("fetching control key over insecure http");
+ }
+
+ key_url
+ .query_pairs_mut()
+ .extend_pairs([("v", CapabilityVersion::CURRENT.to_string())]);
+
+ let client = connect_h1(&key_url).await?;
+ let response = client.get(&key_url, None).await?;
+ if !response.status().is_success() {
+ let status = response.status();
+ tracing::error!(
+ status_code = status.as_str(),
+ "failed to retrieve control server machine public key"
+ );
+
+ return Err(ConnectionError::Internal(InternalErrorKind::Http));
+ }
+
+ let control_keys: ControlPublicKeys = serde_json::from_slice(&response.collect_bytes().await?)?;
+ let control_public_key = control_keys.public_key;
+
+ Ok(control_public_key)
+}
+
+/// Execute the TS2021 upgrade: make an HTTP upgrade request over `h1_client` and perform a Noise
+/// handshake to establish an encrypted channel to control.
+#[tracing::instrument(skip_all, fields(%control_url, %init_msg), err)]
+pub async fn upgrade_ts2021(
+ control_url: &Url,
+ init_msg: &str,
+ handshake: ts_control_noise::Handshake,
+ machine_key: &MachineKeyPair,
+ h1_client: impl ts_http_util::Client<EmptyBody>,
+) -> Result<impl AsyncRead + AsyncWrite + Unpin + 'static, ConnectionError> {
+ let ts2021_url = control_url.join("/ts2021")?;
+
+ tracing::trace!(
+ %ts2021_url,
+ "started NoiseIK handshake, upgrading to TS2021"
+ );
+
+ let resp = h1_client
+ .send(ts_http_util::make_upgrade_req(
+ &ts2021_url,
+ UPGRADE_HEADER_VALUE,
+ [(
+ HeaderName::from_str(HANDSHAKE_HEADER_KEY).unwrap(),
+ HeaderValue::from_str(init_msg).expect("handshake header is valid"),
+ )],
+ )?)
+ .await?;
+
+ let upgraded = ts_http_util::do_upgrade(resp).await.map_err(|error| {
+ tracing::error!(%error, "could not upgrade control connection to TS2021 protocol");
+ ConnectionError::Internal(InternalErrorKind::Http)
+ })?;
+
+ let conn = handshake.complete(upgraded, machine_key).await?;
+
+ tracing::debug!("upgraded control connection from HTTP/1.1 to TS2021");
+
+ Ok(conn)
+}
+
+/// Read the optional early payload (challenge packet) from the server.
+///
+/// The server may send a challenge packet with magic prefix [FF FF FF 'T' 'S'] followed
+/// by a JSON payload, or it may go straight to HTTP/2. This function checks for the magic header
+/// and consumes the payload if present, otherwise chaining the bytes back for consumption by the
+/// HTTP/2 parser.
+#[tracing::instrument(skip_all, err, level = "trace")]
+pub async fn read_challenge_packet<Conn>(
+ mut conn: Conn,
+) -> Result<PrefixedReader<Conn>, ConnectionError>
+where
+ Conn: AsyncRead + Unpin,
+{
+ let mut magic = [0u8; CHALLENGE_MAGIC.len()];
+
+ conn.read_exact(&mut magic)
+ .await
+ .map_err(|err| ConnectionError::io_error("header", "early_payload", err))?;
+
+ // This isn't an early challenge payload, it's the start of the HTTP/2 header -- chain it back
+ if magic != CHALLENGE_MAGIC {
+ return Ok(PrefixedReader::new(conn, Bytes::copy_from_slice(&magic)));
+ }
+
+ let mut challenge_len: U32 = 0.into();
+ conn.read_exact(challenge_len.as_mut())
+ .await
+ .map_err(|err| ConnectionError::io_error("length", "challenge", err))?;
+
+ let challenge_len = challenge_len.get() as usize;
+ if challenge_len > MAX_CHALLENGE_LENGTH {
+ tracing::error!(
+ challenge_len,
+ "invalid challenge length; must be less than {MAX_CHALLENGE_LENGTH} bytes"
+ );
+ return Err(ConnectionError::Internal(
+ InternalErrorKind::ChallengeLength,
+ ));
+ }
+
+ // Read and discard the challenge JSON.
+ let mut limited = conn.take(challenge_len as _);
+ tokio::io::copy(&mut limited, &mut tokio::io::sink())
+ .await
+ .map_err(|err| ConnectionError::io_error("body", "challenge", err))?;
+
+ tracing::trace!(
+ n_bytes = challenge_len,
+ "read and discarded early challenge payload"
+ );
+
+ Ok(PrefixedReader::new(
+ limited.into_inner(),
+ Default::default(),
+ ))
+}
+
+#[cfg(test)]
+mod tests {
+ use tokio::io::{AsyncReadExt, AsyncWriteExt, duplex};
+
+ use super::*;
+
+ /// Build a challenge packet: magic + big-endian length + JSON body.
+ fn make_challenge(json: &[u8]) -> Vec<u8> {
+ let mut buf = Vec::new();
+ buf.extend_from_slice(&CHALLENGE_MAGIC);
+ buf.extend_from_slice(&(json.len() as u32).to_be_bytes());
+ buf.extend_from_slice(json);
+ buf
+ }
+
+ /// Test that when the server sends an early challenge packet (production control
+ /// server behavior), the magic+length+JSON is consumed and subsequent HTTP/2 data
+ /// is passed through unmodified.
+ #[tokio::test]
+ async fn challenge_present() {
+ let json = b"{\"nodeKeyChallenge\":\"test\"}";
+ let payload = b"HTTP/2 data after challenge";
+
+ let mut data = make_challenge(json);
+ data.extend_from_slice(payload);
+
+ let (mut writer, reader) = duplex(1024);
+ writer.write_all(&data).await.unwrap();
+ drop(writer);
+
+ let mut conn = read_challenge_packet(reader).await.unwrap();
+
+ let mut out = Vec::new();
+ conn.read_to_end(&mut out).await.unwrap();
+ assert_eq!(out, payload);
+ }
+
+ /// Test that when the server skips the early challenge and goes straight to HTTP/2
+ /// (testcontrol behavior), all bytes are preserved -- the 9-byte peek that didn't
+ /// match the magic is chained back so the HTTP/2 parser sees the full stream.
+ #[tokio::test]
+ async fn challenge_absent() {
+ let payload = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
+
+ let (mut writer, reader) = duplex(1024);
+ writer.write_all(payload).await.unwrap();
+ drop(writer);
+
+ let mut conn = read_challenge_packet(reader).await.unwrap();
+
+ let mut out = Vec::new();
+ conn.read_to_end(&mut out).await.unwrap();
+ assert_eq!(out, payload);
+ }
+}
diff --git a/deps/ts_control/src/client/map_stream.rs b/deps/ts_control/src/client/map_stream.rs
new file mode 100644
index 00000000..a3c7dedb
--- /dev/null
+++ b/deps/ts_control/src/client/map_stream.rs
@@ -0,0 +1,280 @@
+use alloc::collections::BTreeMap;
+
+use bytes::Bytes;
+use futures_util::Stream;
+use tokio::io::{AsyncRead, AsyncReadExt};
+use ts_control_serde::{MapRequest, MapResponse, PingRequest};
+use ts_http_util::{BytesBody, ClientExt, Http2, ResponseExt};
+use ts_packet::PacketMut;
+use ts_packetfilter as pf;
+use ts_packetfilter_state as pf_state;
+use url::Url;
+
+use crate::{DialPlan, NodeId, NodeStatus};
+
+#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
+pub enum MapStreamError {
+ #[error("serialization error")]
+ SerDe,
+ #[error("unsuccessful HTTP request or upgrade")]
+ Http,
+ #[error("Network error")]
+ NetworkError,
+}
+
+impl From<serde_json::Error> for MapStreamError {
+ fn from(error: serde_json::Error) -> Self {
+ tracing::error!(%error, "serialization error sending map request");
+ MapStreamError::SerDe
+ }
+}
+
+impl From<ts_http_util::Error> for MapStreamError {
+ fn from(error: ts_http_util::Error) -> Self {
+ tracing::error!(%error, "http error sending map request");
+
+ if crate::http_error_is_recoverable(error) {
+ MapStreamError::NetworkError
+ } else {
+ MapStreamError::Http
+ }
+ }
+}
+
+impl From<MapStreamError> for crate::Error {
+ fn from(e: MapStreamError) -> Self {
+ match e {
+ MapStreamError::SerDe => crate::Error::Internal(
+ crate::InternalErrorKind::SerDe,
+ crate::Operation::MapRequest,
+ ),
+ MapStreamError::Http => {
+ crate::Error::Internal(crate::InternalErrorKind::Http, crate::Operation::MapRequest)
+ }
+ MapStreamError::NetworkError => {
+ crate::Error::NetworkError(crate::Operation::MapRequest)
+ }
+ }
+ }
+}
+
+/// An update to the peers recorded in the netmap.
+#[derive(Debug)]
+pub enum PeerUpdate {
+ /// Complete peer state.
+ Full(Vec<crate::Node>),
+
+ /// Delta update to the peer state.
+ Delta {
+ /// Peers with a few changed fields in the state.
+ patch: Vec<crate::NodeUpdate>,
+ /// Peers added to or completely changed in the state.
+ upsert: Vec<crate::Node>,
+ /// Peer [`NodeId`]s removed from the state.
+ remove: Vec<NodeId>,
+ },
+}
+
+/// The components of a packet filter update.
+///
+/// These can't be merged into a single map due to the update rules.
+pub type FilterUpdate = (Option<pf::Ruleset>, BTreeMap<String, Option<pf::Ruleset>>);
+
+/// An update to the netmap state produced from a mapresponse.
+#[derive(Debug)]
+pub struct StateUpdate {
+ /// New derp map is available.
+ pub derp: Option<crate::DerpMap>,
+ /// New self-node.
+ pub node: Option<crate::Node>,
+ /// Updates to the set of peers in the netmap.
+ pub peer_update: Option<PeerUpdate>,
+ /// Send a ping request.
+ pub ping: Option<PingRequest>,
+ /// Update to the packet filter.
+ pub packetfilter: Option<FilterUpdate>,
+ /// This URL should be displayed to the user or opened in their browser automatically.
+ pub pop_browser_url: Option<Url>,
+ /// New dial plan sent by control.
+ pub dial_plan: Option<DialPlan>,
+}
+
+/// Read a stream of netmap responses from `reader`, converting them into `StateUpdate`s.
+pub fn map_stream(reader: impl AsyncRead + Unpin) -> impl Stream<Item = StateUpdate> {
+ futures_util::stream::unfold(reader, async |mut reader| {
+ let msg_len = reader
+ .read_u32_le()
+ .await
+ .inspect_err(|e| {
+ tracing::error!(error = %e, "could not read netmap length");
+ })
+ .ok()?;
+
+ let mut buf = PacketMut::new(msg_len as usize);
+ tracing::trace!(?msg_len, "reading netmap");
+
+ reader
+ .read_exact(buf.as_mut())
+ .await
+ .inspect_err(|e| {
+ tracing::error!(error = %e, "could not read netmap");
+ })
+ .ok()?;
+
+ let map_response: MapResponse = serde_json::from_slice(buf.as_ref())
+ .inspect_err(|e| {
+ tracing::error!(error = %e, "deserializing netmap");
+ })
+ .ok()?;
+
+ tracing::trace!(?msg_len, ?map_response);
+
+ let packetfilter = packet_filter(&map_response);
+
+ fn nonempty<T>(x: &Option<Vec<T>>) -> bool {
+ x.as_ref().is_some_and(|x| !x.is_empty())
+ }
+
+ let peer_update = if let Some(full_map) = map_response.peers {
+ Some(PeerUpdate::Full(full_map.iter().map(Into::into).collect()))
+ } else if nonempty(&map_response.peers_removed)
+ || nonempty(&map_response.peers_changed)
+ || !map_response.peer_seen_change.is_empty()
+ || !map_response.online_change.is_empty()
+ || nonempty(&map_response.peers_changed_patch)
+ {
+ let mut updates: BTreeMap<NodeId, crate::NodeUpdate> = BTreeMap::new();
+ for (id, seen) in map_response.peer_seen_change {
+ let status = if seen {
+ // Not online, and no timestamp provided by control, so we have to estimate.
+ NodeStatus::new(Some(false), None)
+ } else {
+ // We don't know whether the node is online or offline from this field.
+ NodeStatus::Unknown
+ };
+ updates
+ .entry(id)
+ .and_modify(|u| u.status = status)
+ .or_insert(crate::NodeUpdate {
+ id,
+ status,
+ ..Default::default()
+ });
+ }
+
+ for (id, online) in map_response.online_change {
+ let status = NodeStatus::new(Some(online), None);
+ updates
+ .entry(id)
+ .and_modify(|u| u.status = status)
+ .or_insert(crate::NodeUpdate {
+ id,
+ status,
+ ..Default::default()
+ });
+ }
+
+ let mut patches = map_response
+ .peers_changed_patch
+ .unwrap_or_default()
+ .iter()
+ .map(|x| (x.node_id, crate::NodeUpdate::from(x)))
+ .collect();
+ updates.append(&mut patches);
+
+ Some(PeerUpdate::Delta {
+ patch: updates.values().cloned().collect(),
+ remove: map_response.peers_removed.unwrap_or_default(),
+ upsert: map_response
+ .peers_changed
+ .unwrap_or_default()
+ .iter()
+ .map(Into::into)
+ .collect(),
+ })
+ } else {
+ None
+ };
+
+ Some((
+ StateUpdate {
+ peer_update,
+ node: map_response.node.as_ref().map(Into::into),
+ derp: map_response
+ .derp_map
+ .as_ref()
+ .map(|x| crate::convert_derp_map(x).collect()),
+ ping: map_response.ping_request,
+ packetfilter,
+ pop_browser_url: map_response.pop_browser_url.and_then(|u| {
+ u.parse()
+ .inspect_err(|e| tracing::error!(error = %e, "invalid pop browser url"))
+ .ok()
+ }),
+ dial_plan: map_response.control_dial_plan.map(Into::into),
+ },
+ reader,
+ ))
+ })
+}
+
+fn packet_filter(map_response: &MapResponse<'_>) -> Option<FilterUpdate> {
+ if map_response.packet_filter.is_none() && map_response.packet_filters.is_empty() {
+ return None;
+ }
+
+ Some((
+ map_response
+ .packet_filter
+ .as_ref()
+ .map(|x| pf_state::rules_to_pf(x).collect()),
+ map_response
+ .packet_filters
+ .iter()
+ .map(|(rule_name, rules)| {
+ (
+ rule_name.to_string(),
+ rules
+ .as_ref()
+ .map(|x| Some(pf_state::rules_to_pf(x).collect()))
+ .unwrap_or_default(),
+ )
+ })
+ .collect(),
+ ))
+}
+
+/// Send a [`MapRequest`] on the given HTTP2 connection.
+#[tracing::instrument(skip_all, fields(map_url = %url.as_str()))]
+pub async fn send_map_request(
+ map_request: MapRequest<'_>,
+ url: &Url,
+ http2_conn: &Http2<BytesBody>,
+) -> Result<impl AsyncRead + 'static + use<>, MapStreamError> {
+ tracing::debug!("sending map request to control server...");
+
+ let body = if cfg!(debug_assertions) {
+ serde_json::to_string_pretty(&map_request)?
+ } else {
+ serde_json::to_string(&map_request)?
+ };
+ tracing::trace!(
+ %body,
+ "sending map request"
+ );
+
+ let resp = http2_conn.post(url, None, Bytes::from(body).into()).await?;
+
+ let status = resp.status();
+ tracing::trace!(?status, "received map response");
+
+ if !status.is_success() {
+ tracing::error!(
+ status = status.as_u16(),
+ "failed to register map updates with unsuccessful HTTP status code"
+ );
+ return Err(MapStreamError::Http);
+ }
+
+ Ok(resp.into_read())
+}
diff --git a/deps/ts_control/src/client/mod.rs b/deps/ts_control/src/client/mod.rs
new file mode 100644
index 00000000..ca1d5e9e
--- /dev/null
+++ b/deps/ts_control/src/client/mod.rs
@@ -0,0 +1,51 @@
+//! Functionality for connecting to and communicating with the Tailscale control server.
+
+use futures_util::Stream;
+use ts_http_util::{BytesBody, Http2};
+use url::Url;
+
+use crate::{Error, map_request_builder::MapRequestBuilder};
+
+mod connect;
+mod map_stream;
+mod ping;
+mod prefixed_reader;
+mod register;
+
+pub use connect::{
+ CONTROL_PROTOCOL_VERSION, connect, fetch_control_key, read_challenge_packet, upgrade_ts2021,
+};
+pub use map_stream::{FilterUpdate, PeerUpdate, StateUpdate, map_stream, send_map_request};
+pub use ping::handle_ping;
+pub use register::{RegistrationError, register};
+
+/// Type of the underlying http2 connection to the control server.
+pub type HttpConn = Http2<BytesBody>;
+
+/// Start the netmap stream. This connection should already be registered successfully.
+///
+/// This is a convenience helper for callers without a strong opinion on the information
+/// included in their initial map request: sophisticated callers that have hostinfo/netinfo or other
+/// fields to populate should manually [`send_map_request`] with their desired request and
+/// [`map_stream`] on the result.
+pub async fn start_stream(
+ control_url: &Url,
+ node_keys: &ts_keys::NodeState,
+ config: &crate::Config,
+ conn: HttpConn,
+) -> Result<impl Stream<Item = StateUpdate> + use<>, Error> {
+ let builder = MapRequestBuilder::new(node_keys).as_stream();
+
+ let request = if let Some(hostname) = &config.hostname {
+ builder.hostname(hostname)
+ } else {
+ builder
+ }
+ .build();
+
+ let map_url = control_url.join("machine/map").unwrap();
+
+ let reader = send_map_request(request, &map_url, &conn).await?;
+ tracing::info!("netmap stream started");
+ Ok(map_stream(reader))
+}
diff --git a/deps/ts_control/src/client/ping.rs b/deps/ts_control/src/client/ping.rs
new file mode 100644
index 00000000..9789415c
--- /dev/null
+++ b/deps/ts_control/src/client/ping.rs
@@ -0,0 +1,138 @@
+use alloc::string::String;
+
+use ts_control_serde::PingType;
+use ts_http_util::{BytesBody, ClientExt, Http2, Request};
+use url::Url;
+
+use crate::StateUpdate;
+
+/// Path requested in HTTP GET via a Control-to-Node (C2N) [`ts_control_serde::PingRequest`] to
+/// invoke the C2N echo handler.
+const C2N_PATH_ECHO: &str = "/echo";
+/// HTTP 400 Bad Request response sent for all unimplemented C2N methods/paths.
+const C2N_PATH_UNKNOWN: &str = "HTTP/1.1 400 Bad Request\r\n\r\nunknown c2n path";
+/// The start of an HTTP/1.1 200 response with no headers, just missing the body. Intended for use
+/// with C2N echo responses, which can append the request body.
+const C2N_RESPONSE_ECHO_PREAMBLE: &str = "HTTP/1.1 200 OK\r\n\r\n";
+
+#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
+pub enum PingError {
+ #[error("HTTP error")]
+ Http,
+ #[error("URL parsing error")]
+ Url,
+ #[error("Ping request with invalid format (missing payload)")]
+ MessageFormat,
+ #[error("Network error")]
+ NetworkError,
+}
+
+impl From<ts_http_util::Error> for PingError {
+ fn from(error: ts_http_util::Error) -> Self {
+ tracing::error!(%error, "HTTP error handling ping");
+
+ if crate::http_error_is_recoverable(error) {
+ PingError::NetworkError
+ } else {
+ PingError::Http
+ }
+ }
+}
+
+impl From<url::ParseError> for PingError {
+ fn from(error: url::ParseError) -> Self {
+ tracing::error!(%error, "Error parsing URL");
+ PingError::Url
+ }
+}
+
+/// Parses the payload of a Control-to-Node (C2N) [`ts_control_serde::PingRequest`] as an HTTP/1.1
+/// request, or returns an error.
+fn parse_c2n_ping(payload: &str) -> Result<Request<String>, PingError> {
+ let req = ts_http_util::http1::parse_request(payload.as_bytes())?;
+ tracing::trace!(
+ payload_len = req.body().len(),
+ payload = req.body(),
+ "extracted payload from ping request body"
+ );
+ Ok(req)
+}
+
+/// Handles [`ts_control_serde::PingRequest`]s from the control plane to this Tailscale node.
+/// Currently only handles Control-to-Node (C2N) echo requests; non-C2N requests are skipped with a
+/// warning, while C2N requests for an unhandled path return a "400 Bad Request" to the control
+/// plane.
+///
+/// ## C2N Mechanism
+///
+/// The C2N mechanism provides a way for the control plane to query a Tailscale node about their
+/// local state, or request changes to the node state. A lot of debugging and metrics-related
+/// features are implemented via this mechanism, along with a number of knobs such as changing the
+/// netfilter implementation or forcing a logs flush in the Tailscale Go client.
+///
+/// Ping requests of type [`PingType::C2N`] contain an entire HTTP/1.1 request as their payload.
+/// The method and path of this request determine which handler is invoked; for example, in the
+/// Tailscale Go client, "GET /echo ..." invokes the C2N echo handler, while
+/// "POST /netfilter-kind ..." changes the netfilter implementation the client uses (on Linux only).
+/// The handler must return a full HTTP response to the request containing the requested data and/or
+/// status - for example, `HTTP/1.1 200 OK <body>` or `HTTP/1.1 400 Bad Request`.
+///
+/// `tailscale-rs` returns an HTTP 400 Bad Request status with an error message to the control
+/// plane for any unimplemented C2N methods/paths.
+pub async fn handle_ping(
+ state: &StateUpdate,
+ control_url: &Url,
+ http2_client: &Http2<BytesBody>,
+) -> Result<(), PingError> {
+ let Some(ping_request) = &state.ping else {
+ return Ok(());
+ };
+
+ tracing::trace!(request = ?ping_request, "handling ping request");
+ for typ in &ping_request.types {
+ if typ != &PingType::C2N {
+ tracing::warn!(ping_type = ?typ, "ignoring unsupported ping type");
+ continue;
+ }
+
+ let ping_request_body = ping_request.payload.as_ref().ok_or_else(|| {
+ tracing::error!("message format error in ping request: missing payload");
+ PingError::MessageFormat
+ })?;
+ let c2n_request = match parse_c2n_ping(ping_request_body) {
+ Ok(c2n_request) => {
+ tracing::trace!(?c2n_request, "parsed c2n ping");
+ c2n_request
+ }
+ Err(_) => {
+ tracing::warn!(?ping_request_body, "ignoring malformed c2n ping");
+ continue;
+ }
+ };
+
+ let c2n_request_path = c2n_request.uri().path();
+ let c2n_response = match c2n_request_path {
+ C2N_PATH_ECHO => {
+ tracing::trace!(c2n_request_path, "handling c2n echo");
+ format!("{}{}", C2N_RESPONSE_ECHO_PREAMBLE, c2n_request.body())
+ }
+ _ => {
+ tracing::debug!(c2n_request_path, "no handler for c2n path");
+ C2N_PATH_UNKNOWN.to_string()
+ }
+ };
+
+ let ping_response_url = control_url.join(ping_request.url.path())?;
+ tracing::trace!(%ping_response_url, ?c2n_response, "posting c2n response");
+ let response = http2_client
+ .post(&ping_response_url, None, c2n_response.into())
+ .await?;
+ if !response.status().is_success() {
+ tracing::error!(status = %response.status(), "responding to c2n ping");
+ } else {
+ tracing::debug!("c2n response sent");
+ }
+ }
+
+ Ok(())
+}
diff --git a/deps/ts_control/src/client/prefixed_reader.rs b/deps/ts_control/src/client/prefixed_reader.rs
new file mode 100644
index 00000000..046cc5cf
--- /dev/null
+++ b/deps/ts_control/src/client/prefixed_reader.rs
@@ -0,0 +1,58 @@
+use core::task::Context;
+use std::{io, pin::Pin, task::Poll};
+
+use bytes::{BufMut, Bytes};
+use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
+
+pin_project_lite::pin_project! {
+ /// I/O wrapper that has data prefixed before the inner reader.
+ ///
+ /// This can't use [`tokio::io::Chain`] because it doesn't impl `AsyncWrite` for the inner type,
+ /// and we need both.
+ pub struct PrefixedReader<T> {
+ prefix: Bytes,
+ #[pin]
+ inner: T,
+ }
+}
+
+impl<T> PrefixedReader<T> {
+ pub fn new(inner: T, prefix: Bytes) -> PrefixedReader<T> {
+ PrefixedReader { prefix, inner }
+ }
+}
+
+impl<T: AsyncRead> AsyncRead for PrefixedReader<T> {
+ fn poll_read(
+ self: Pin<&mut Self>,
+ cx: &mut Context<'_>,
+ buf: &mut ReadBuf<'_>,
+ ) -> Poll<io::Result<()>> {
+ let slf = self.project();
+
+ if slf.prefix.is_empty() {
+ return slf.inner.poll_read(cx, buf);
+ }
+
+ buf.put(slf.prefix);
+ Poll::Ready(Ok(()))
+ }
+}
+
+impl<T: AsyncWrite> AsyncWrite for PrefixedReader<T> {
+ fn poll_write(
+ self: Pin<&mut Self>,
+ cx: &mut Context<'_>,
+ buf: &[u8],
+ ) -> Poll<io::Result<usize>> {
+ self.project().inner.poll_write(cx, buf)
+ }
+
+ fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
+ self.project().inner.poll_flush(cx)
+ }
+
+ fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
+ self.project().inner.poll_shutdown(cx)
+ }
+}
diff --git a/deps/ts_control/src/client/register.rs b/deps/ts_control/src/client/register.rs
new file mode 100644
index 00000000..40112506
--- /dev/null
+++ b/deps/ts_control/src/client/register.rs
@@ -0,0 +1,202 @@
+use std::fmt;
+
+use bytes::Bytes;
+use ts_capabilityversion::CapabilityVersion;
+use ts_control_serde::{HostInfo, RegisterAuth, RegisterRequest, RegisterResponse};
+use ts_http_util::{BytesBody, ClientExt, Http2, ResponseExt};
+use url::Url;
+
+const LOAD_BALANCER_HEADER_KEY: &str = "Ts-Lb";
+
+/// Error registering this node with the control server.
+#[derive(Debug, thiserror::Error, Clone, Eq, PartialEq)]
+pub enum RegistrationError {
+ /// The machine's keys weren't authorized to join the tailnet.
+ ///
+ /// The contained URL, if present, may be visited by the user in a browser to interactively
+ /// authenticate the machine.
+ #[error("machine was not authorized by control to join tailnet")]
+ MachineNotAuthorized(Option<Url>),
+
+ /// A network error occurred. Retrying later may resolve the problem.
+ #[error("Network error")]
+ NetworkError,
+
+ /// An internal error occurred.
+ #[error("error during registration: {0}")]
+ Internal(InternalErrorKind),
+}
+
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum InternalErrorKind {
+ Url,
+ SerDe,
+ Utf8,
+ Http,
+}
+
+impl fmt::Display for InternalErrorKind {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ InternalErrorKind::Url => write!(f, "URL parsing error"),
+ InternalErrorKind::SerDe => write!(f, "serialization/deserialization error"),
+ InternalErrorKind::Utf8 => write!(f, "invalid UTF8"),
+ InternalErrorKind::Http => write!(f, "unsuccessful HTTP request or upgrade"),
+ }
+ }
+}
+
+impl From<url::ParseError> for RegistrationError {
+ fn from(error: url::ParseError) -> Self {
+ tracing::error!(%error, "bad URL");
+ RegistrationError::Internal(InternalErrorKind::Url)
+ }
+}
+
+impl From<serde_json::Error> for RegistrationError {
+ fn from(error: serde_json::Error) -> Self {
+ tracing::error!(%error, "serialization/deserialization error in registration");
+ RegistrationError::Internal(InternalErrorKind::SerDe)
+ }
+}
+
+impl From<ts_http_util::Error> for RegistrationError {
+ fn from(error: ts_http_util::Error) -> Self {
+ tracing::error!(%error, "http error sending registration request");
+
+ if crate::http_error_is_recoverable(error) {
+ RegistrationError::NetworkError
+ } else {
+ RegistrationError::Internal(InternalErrorKind::Http)
+ }
+ }
+}
+
+impl From<core::str::Utf8Error> for RegistrationError {
+ fn from(error: core::str::Utf8Error) -> Self {
+ tracing::error!(%error, "utf8 error in registration response");
+ RegistrationError::Internal(InternalErrorKind::Utf8)
+ }
+}
+
+impl From<RegistrationError> for crate::Error {
+ fn from(e: RegistrationError) -> Self {
+ match e {
+ RegistrationError::MachineNotAuthorized(Some(u)) => {
+ crate::Error::MachineNotAuthorized(u)
+ }
+ RegistrationError::MachineNotAuthorized(None) => crate::Error::Internal(
+ crate::InternalErrorKind::MachineAuthorization,
+ crate::Operation::Registration,
+ ),
+ RegistrationError::Internal(k) => {
+ crate::Error::Internal(k.into(), crate::Operation::Registration)
+ }
+ RegistrationError::NetworkError => {
+ crate::Error::NetworkError(crate::Operation::Registration)
+ }
+ }
+ }
+}
+
+impl From<InternalErrorKind> for crate::InternalErrorKind {
+ fn from(e: InternalErrorKind) -> Self {
+ match e {
+ InternalErrorKind::Url => crate::InternalErrorKind::Url,
+ InternalErrorKind::SerDe => crate::InternalErrorKind::SerDe,
+ InternalErrorKind::Utf8 => crate::InternalErrorKind::Utf8,
+ InternalErrorKind::Http => crate::InternalErrorKind::Http,
+ }
+ }
+}
+
+/// Send a request to the control server to register this device.
+///
+/// If the `followup` argument is present, the request blocks in a long-poll until the user
+/// authorizes the device using a browser. The url should have been produced by a previous call to
+/// `register` via [`RegistrationError::MachineNotAuthorized`].
+#[tracing::instrument(skip_all, fields(%control_url))]
+pub async fn register(
+ config: &crate::Config,
+ control_url: &Url,
+ auth_key: Option<&str>,
+ followup: Option<Url>,
+ node_keystate: &ts_keys::NodeState,
+ http2_conn: &Http2<BytesBody>,
+) -> Result<(), RegistrationError> {
+ let node_public_key = node_keystate.node_keys.public;
+ let network_lock_public_key = node_keystate.network_lock_keys.public;
+
+ let register_req = RegisterRequest {
+ version: CapabilityVersion::CURRENT,
+ node_key: node_public_key,
+ hostinfo: HostInfo {
+ hostname: config.hostname.as_deref(),
+ app: &config.format_client_name(),
+ ipn_version: crate::PKG_VERSION,
+ ..Default::default()
+ },
+ nl_key: Some(network_lock_public_key),
+ auth: auth_key.map(RegisterAuth::from),
+ followup,
+ ephemeral: config.ephemeral,
+ ..Default::default()
+ };
+
+ let body = if cfg!(debug_assertions) {
+ serde_json::to_string_pretty(&register_req)?
+ } else {
+ serde_json::to_string(&register_req)?
+ };
+
+ let register_url = control_url.join("machine/register")?;
+ tracing::trace!(
+ url = %register_url.as_str(),
+ %body,
+ "sending registration request"
+ );
+
+ let response = http2_conn
+ .post(
+ &register_url,
+ [(
+ LOAD_BALANCER_HEADER_KEY.parse().unwrap(),
+ node_public_key.to_string().parse().unwrap(),
+ )],
+ Bytes::from(body).into(),
+ )
+ .await?;
+
+ let status = response.status();
+
+ tracing::debug!(%status, "received registration response");
+
+ if !status.is_success() {
+ // Attempt to collect the body to log the error, truncating to prevent spamming the logs.
+ let mut body = response.collect_bytes().await.unwrap_or_default();
+ body.truncate(512);
+ let body = core::str::from_utf8(&body).unwrap_or("<invalid utf8>");
+ tracing::error!(%body, %status, "registration failed");
+
+ return Err(RegistrationError::Internal(InternalErrorKind::Http));
+ }
+
+ let body = response.collect_bytes().await?;
+ let body = core::str::from_utf8(&body)?;
+
+ tracing::trace!(registration_response_body = %body);
+
+ let register_resp: RegisterResponse = serde_json::from_str(body)?;
+
+ if !register_resp.machine_authorized {
+ if !register_resp.auth_url.is_empty() {
+ Err(RegistrationError::MachineNotAuthorized(Some(
+ register_resp.auth_url.parse()?,
+ )))
+ } else {
+ Err(RegistrationError::MachineNotAuthorized(None))
+ }
+ } else {
+ Ok(())
+ }
+}
diff --git a/deps/ts_control/src/config.rs b/deps/ts_control/src/config.rs
new file mode 100644
index 00000000..b70c98fe
--- /dev/null
+++ b/deps/ts_control/src/config.rs
@@ -0,0 +1,69 @@
+use core::fmt::Debug;
+
+use url::Url;
+
+lazy_static::lazy_static! {
+ /// The default [`Url`] of the control plane server (aka "coordination server").
+ pub static ref DEFAULT_CONTROL_SERVER: Url = Url::parse("https://controlplane.tailscale.com/").unwrap();
+}
+
+/// Configuration for the control server.
+#[derive(Clone, serde::Serialize, serde::Deserialize)]
+pub struct Config {
+ /// The URL of the control server to connect to.
+ pub server_url: Url,
+
+ /// The hostname of the current node.
+ pub hostname: Option<String>,
+
+ /// A name for this type of client.
+ ///
+ /// This will be reported to the control server in the `HostInfo.App` field.
+ pub client_name: Option<String>,
+
+ /// Tags to request from the control server.
+ pub tags: Vec<String>,
+
+ /// Whether this node is registered as ephemeral.
+ ///
+ /// Ephemeral nodes are deleted by control after a short period of inactivity.
+ pub ephemeral: bool,
+}
+
+impl Config {
+ /// Get the full client name as a string.
+ ///
+ /// This takes the form `tailscale-rs ({client_name})`, where the parenthetical is only
+ /// provided if self.client_name is set.
+ pub fn format_client_name(&self) -> String {
+ let mut full_name = "tailscale-rs".to_owned();
+ if let Some(client_name) = &self.client_name {
+ full_name.push_str(&format!(" ({client_name})"));
+ }
+
+ full_name
+ }
+}
+
+impl Debug for Config {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ f.debug_struct("Config")
+ .field("hostname", &self.hostname)
+ .field("server_url", &self.server_url.as_str())
+ .field("client_name", &self.client_name)
+ .field("ephemeral", &self.ephemeral)
+ .finish()
+ }
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ server_url: DEFAULT_CONTROL_SERVER.clone(),
+ hostname: gethostname::gethostname().into_string().ok(),
+ client_name: None,
+ tags: Default::default(),
+ ephemeral: false,
+ }
+ }
+}
diff --git a/deps/ts_control/src/control_dialer.rs b/deps/ts_control/src/control_dialer.rs
new file mode 100644
index 00000000..13a2b444
--- /dev/null
+++ b/deps/ts_control/src/control_dialer.rs
@@ -0,0 +1,266 @@
+use core::fmt::{Debug, Formatter};
+use std::time::Instant;
+
+use tokio::{
+ io::{AsyncRead, AsyncWrite},
+ net::TcpStream,
+};
+use tokio_util::future::FutureExt;
+use ts_bitset::BitsetDyn;
+use ts_capabilityversion::CapabilityVersion;
+use ts_http_util::{BytesBody, Http2};
+use url::Url;
+
+use crate::{DialCandidate, DialMode, DialPlan, Error, InternalErrorKind, Operation};
+
+/// Manages state for control dial plan and handles selection of successive dial candidates.
+pub struct ControlDialer {
+ plan: DialPlan,
+ epoch: usize,
+ timestamp: Instant,
+ attempted_candidates: ts_dynbitset::DynBitset,
+}
+
+impl Default for ControlDialer {
+ fn default() -> Self {
+ Self {
+ plan: DialPlan::default(),
+ epoch: 0,
+ timestamp: Instant::now(),
+ attempted_candidates: Default::default(),
+ }
+ }
+}
+
+/// Creates a TCP connection on the basis of a specific [`DialCandidate`].
+///
+/// Produced by [`ControlDialer::next_dialer`].
+pub trait TcpDialer {
+ /// Open a TCP connection using the [`DialCandidate`] assigned to this dialer.
+ ///
+ /// - `host` is used if the [`DialCandidate`] requires DNS lookup.
+ /// **Ignored** for plain IP [`DialCandidate`]s.
+ /// - `port` is the TCP port to connect to.
+ ///
+ /// Calling this function marks the current candidate as "attempted": the next call to
+ /// [`ControlDialer::next_dialer`] will use the next available candidate.
+ fn dial(
+ self,
+ host: &str,
+ port: u16,
+ ) -> impl Future<Output = tokio::io::Result<TcpStream>> + Send;
+}
+
+enum ControlTcpDialer<'a> {
+ UseDns,
+ Planned {
+ attempted: &'a mut ts_dynbitset::DynBitset,
+ candidate: &'a DialCandidate,
+ index: usize,
+ },
+}
+
+impl Debug for ControlTcpDialer<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ ControlTcpDialer::UseDns => write!(f, "TcpDialer::Dns"),
+ ControlTcpDialer::Planned { candidate, .. } => match &candidate.mode {
+ DialMode::Ip(ip) => f.debug_tuple("TcpDialer::Ip").field(ip).finish(),
+ DialMode::Ace { ip: Some(ip), host } => f
+ .debug_tuple("TcpDialer::Ace")
+ .field(ip)
+ .field(host)
+ .finish(),
+ DialMode::Ace { host, .. } => f.debug_tuple("TcpDialer::Ace").field(host).finish(),
+ },
+ }
+ }
+}
+
+impl TcpDialer for ControlTcpDialer<'_> {
+ async fn dial(self, host: &str, port: u16) -> tokio::io::Result<TcpStream> {
+ match self {
+ ControlTcpDialer::UseDns => TcpStream::connect(format!("{host}:{port}")).await,
+ ControlTcpDialer::Planned {
+ candidate,
+ attempted: used,
+ index,
+ } => {
+ used.set(index);
+
+ match candidate.mode {
+ DialMode::Ip(ip) => {
+ TcpStream::connect((ip, port))
+ .timeout(candidate.timeout)
+ .await?
+ }
+ DialMode::Ace { .. } => {
+ unimplemented!()
+ }
+ }
+ }
+ }
+ }
+}
+
+impl ControlDialer {
+ /// Update the stored dial plan with the new `plan`.
+ ///
+ /// Returns whether the dial plan changed. Resubmission of the same dial plan is
+ /// idempotent.
+ pub fn update_dial_plan(&mut self, plan: &DialPlan) -> bool {
+ if &self.plan == plan {
+ return false;
+ }
+
+ self.plan = plan.clone();
+ self.epoch += 1;
+ self.timestamp = Instant::now();
+
+ true
+ }
+
+ /// Clear the set of attempted dial candidates.
+ ///
+ /// This will cause future connection attempts to retry all available dialers in
+ /// priority order.
+ pub fn clear_attempted(&mut self) {
+ self.attempted_candidates.clear_all();
+ }
+
+ /// Get the next dialer candidate from the dial plan.
+ ///
+ /// If all dialers have already been tried, falls back to system DNS.
+ ///
+ /// NB: the returned [`TcpDialer`] does not mark its corresponding candidate as having
+ /// been attempted until [`TcpDialer::dial`] is called -- it is fine semantically to
+ /// drop the returned dialer without calling `dial`.
+ pub fn next_dialer(&mut self) -> impl TcpDialer + Debug {
+ match &self.plan {
+ DialPlan::UseDns => ControlTcpDialer::UseDns,
+ DialPlan::Plan(candidates) => {
+ let mut selected_candidate: Option<(usize, usize, &DialCandidate)> = None;
+ let now = Instant::now();
+
+ // TODO(npry): ensure candidate sorting, optimistically stop early
+ for (i, candidate) in candidates.iter().enumerate() {
+ if self.attempted_candidates.test(i) {
+ continue;
+ }
+
+ let start_after = self.timestamp + candidate.start_delay_sec;
+ if start_after > now {
+ continue;
+ }
+
+ if matches!(candidate.mode, DialMode::Ace { .. }) {
+ // TODO(npry): ACE unsupported
+ continue;
+ }
+
+ if selected_candidate.is_none_or(|(prio, _idx, elem)| prio < elem.priority) {
+ selected_candidate = Some((candidate.priority, i, candidate));
+ }
+ }
+
+ let (i, candidate) = match selected_candidate {
+ Some((_prio, i, elem)) => (i, elem),
+ None => {
+ tracing::warn!(
+ "no dialer candidates available: falling back to system dns"
+ );
+ return ControlTcpDialer::UseDns;
+ }
+ };
+
+ ControlTcpDialer::Planned {
+ candidate,
+ index: i,
+ attempted: &mut self.attempted_candidates,
+ }
+ }
+ }
+ }
+
+ /// Convenience wrapper for [`next_dialer`][ControlDialer::next_dialer] followed by
+ /// [`complete_connection`].
+ #[tracing::instrument(skip_all, fields(control_url = %url))]
+ pub async fn full_connect_next(
+ &mut self,
+ url: &Url,
+ machine_keys: &ts_keys::MachineKeyPair,
+ ) -> Result<Http2<BytesBody>, Error> {
+ let next = self.next_dialer();
+ tracing::trace!(selected_control_dialer = ?next);
+
+ let host = url.host_str().ok_or(Error::InvalidUrl(url.clone()))?;
+ let port = url
+ .port_or_known_default()
+ .ok_or_else(|| Error::InvalidUrl(url.clone()))?;
+
+ let conn = next.dial(host, port).await.map_err(|e| {
+ tracing::error!(error = %e, %url, %host, port, "dialing tcp");
+ Error::Internal(InternalErrorKind::Io, Operation::ConnectToControlServer)
+ })?;
+
+ tracing::debug!(
+ remote_endpoint = ?conn.peer_addr(),
+ "tcp connection to control"
+ );
+
+ let client = complete_connection(url, machine_keys, conn).await?;
+
+ Ok(client)
+ }
+}
+
+/// Complete a connection to control over the supplied I/O `stream`.
+///
+/// Establishes an http1 connection over `stream`, wrapping it in a TLS connection if
+/// `url`'s scheme is `https`. Then upgrades the connection over ts2021 and establishes an
+/// inner http2 connection.
+pub async fn complete_connection<Io>(
+ url: &Url,
+ machine_keys: &ts_keys::MachineKeyPair,
+ stream: Io,
+) -> Result<Http2<BytesBody>, Error>
+where
+ Io: AsyncRead + AsyncWrite + Send + Unpin + 'static,
+{
+ let h1_client = match url.scheme() {
+ "https" => {
+ let conn = ts_tls_util::connect(
+ ts_tls_util::server_name(url).ok_or_else(|| Error::InvalidUrl(url.clone()))?,
+ stream,
+ )
+ .await
+ .map_err(|e| {
+ tracing::error!(error = %e, "establishing tls connection");
+ Error::io_error(e, Operation::ConnectToControlServer)
+ })?;
+ ts_http_util::http1::connect(conn).await?
+ }
+ "http" => ts_http_util::http1::connect(stream).await?,
+ other => {
+ tracing::error!(invalid_scheme = other);
+ return Err(Error::InvalidUrl(url.clone()));
+ }
+ };
+ let control_public_key = crate::client::fetch_control_key(url).await?;
+
+ let (handshake, init_msg) = ts_control_noise::Handshake::initialize(
+ &crate::client::CONTROL_PROTOCOL_VERSION,
+ machine_keys,
+ &control_public_key,
+ CapabilityVersion::CURRENT,
+ );
+
+ let conn =
+ crate::client::upgrade_ts2021(url, &init_msg, handshake, machine_keys, h1_client).await?;
+ let conn = crate::client::read_challenge_packet(conn).await?;
+
+ let h2_conn = ts_http_util::http2::connect(conn).await?;
+ tracing::debug!("http2 connection to control established");
+
+ Ok(h2_conn)
+}
diff --git a/deps/ts_control/src/derp.rs b/deps/ts_control/src/derp.rs
new file mode 100644
index 00000000..036fcdb8
--- /dev/null
+++ b/deps/ts_control/src/derp.rs
@@ -0,0 +1,89 @@
+use alloc::collections::BTreeMap;
+
+use ts_derp::TlsValidationConfig;
+
+/// The full derp state, a map of [`ts_derp::RegionId`]s to [`Region`]s.
+pub type Map = BTreeMap<ts_derp::RegionId, Region>;
+
+/// Convert a derp map from the [`ts_control_serde`] representation to the [`ts_derp`]
+/// representation.
+pub fn convert_derp_map(
+ derp_map: &ts_control_serde::DerpMap<'_>,
+) -> impl Iterator<Item = (ts_derp::RegionId, Region)> {
+ derp_map.regions.iter().map(|(id, region)| {
+ let id = ts_derp::RegionId((*id).into());
+ let region: Region = region.into();
+
+ (id, region)
+ })
+}
+
+/// A single derp [`Region`], holding the region info and all the server info.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Region {
+ /// The info for this region.
+ pub info: ts_derp::RegionInfo,
+
+ /// Servers in this region.
+ pub servers: Vec<ts_derp::ServerConnInfo>,
+}
+
+impl From<&ts_control_serde::DerpRegion<'_>> for Region {
+ fn from(region: &ts_control_serde::DerpRegion<'_>) -> Self {
+ let info = region_info(region);
+ let servers = region.nodes.iter().map(server).collect();
+
+ Region { info, servers }
+ }
+}
+
+fn region_info(region: &ts_control_serde::DerpRegion) -> ts_derp::RegionInfo {
+ ts_derp::RegionInfo {
+ name: region.name.to_string(),
+ code: region.code.to_string(),
+ no_measure_no_home: region.no_measure_no_home,
+ }
+}
+
+fn server(server: &ts_control_serde::DerpServer) -> ts_derp::ServerConnInfo {
+ const DEFAULT_TLS_PORT: u16 = 443;
+
+ let https_port = match server.derp_port {
+ 0 => DEFAULT_TLS_PORT,
+ port => port,
+ };
+
+ #[cfg_attr(not(feature = "insecure-derp"), allow(unused_mut))]
+ let mut tls_config =
+ TlsValidationConfig::from_str(server.cert_name.unwrap_or_default(), server.hostname);
+
+ #[cfg(feature = "insecure-derp")]
+ if server.insecure_for_tests {
+ tls_config = TlsValidationConfig::InsecureForTests;
+ };
+
+ ts_derp::ServerConnInfo {
+ hostname: server.hostname.to_string(),
+ https_port,
+ stun_port: server.stun_port.into(),
+ supports_port_80: server.can_port_80,
+
+ ipv4: convert_ip_usage(server.ipv4),
+ ipv6: convert_ip_usage(server.ipv6),
+
+ stun_only: server.stun_only,
+
+ tls_validation_config: tls_config,
+ }
+}
+
+fn convert_ip_usage<T>(ip: ts_control_serde::DerpIpUsage<T>) -> ts_derp::IpUsage<T>
+where
+ T: Copy,
+{
+ match ip {
+ ts_control_serde::DerpIpUsage::Disable => ts_derp::IpUsage::Disable,
+ ts_control_serde::DerpIpUsage::UseDns => ts_derp::IpUsage::UseDns,
+ ts_control_serde::DerpIpUsage::FixedAddr(ip) => ts_derp::IpUsage::FixedAddr(ip),
+ }
+}
diff --git a/deps/ts_control/src/dial_plan.rs b/deps/ts_control/src/dial_plan.rs
new file mode 100644
index 00000000..04288901
--- /dev/null
+++ b/deps/ts_control/src/dial_plan.rs
@@ -0,0 +1,108 @@
+use core::{net::IpAddr, time::Duration};
+
+use ts_control_serde::{ControlDialPlan, ControlIpCandidate};
+
+/// A plan to connect to the control plane, supplied as part of a netmap update.
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub enum DialPlan {
+ /// Use system DNS to resolve the control plane's IP address.
+ #[default]
+ UseDns,
+
+ /// Use the contained plan to connect to the control plane.
+ Plan(Vec<DialCandidate>),
+}
+
+/// A candidate endpoint for a control plane connection.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+pub struct DialCandidate {
+ // NOTE(npry): field order in this struct is load-bearing for the PartialOrd derive.
+ // The relevant initial tuple is (priority, start_delay_sec).
+ /// The priority of this candidate.
+ ///
+ /// Higher priorities are preferred over lower ones.
+ pub priority: usize,
+
+ /// How long to delay before attempting to use this candidate.
+ pub start_delay_sec: Duration,
+ /// Timeout before giving up on this candidate.
+ pub timeout: Duration,
+
+ /// The mode with which to connect.
+ pub mode: DialMode,
+}
+
+/// The mode with which to connect to the control plane.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+pub enum DialMode {
+ /// Connect directly via the indicated IP address (do not resolve using DNS).
+ Ip(IpAddr),
+
+ /// Connect to control over HTTPS using the proxy CONNECT method via the selected host.
+ Ace {
+ /// The hostname to connect to.
+ ///
+ /// This is always set as the HTTP request's `Host`.
+ host: String,
+
+ /// If present, the resolved address of the host to connect to.
+ ip: Option<IpAddr>,
+ },
+}
+
+impl From<ControlDialPlan<'_>> for DialPlan {
+ fn from(value: ControlDialPlan<'_>) -> Self {
+ (&value).into()
+ }
+}
+
+impl TryFrom<ControlIpCandidate<'_>> for DialCandidate {
+ type Error = ();
+
+ fn try_from(value: ControlIpCandidate<'_>) -> Result<Self, Self::Error> {
+ (&value).try_into()
+ }
+}
+
+impl From<&ControlDialPlan<'_>> for DialPlan {
+ fn from(value: &ControlDialPlan<'_>) -> Self {
+ let mut plan_candidates = value
+ .candidates
+ .iter()
+ .filter_map(|x| x.try_into().ok())
+ .collect::<Vec<DialCandidate>>();
+
+ // sort in decreasing priority order
+ plan_candidates.sort_by(|a, b| a.cmp(b).reverse());
+
+ if plan_candidates.is_empty() {
+ DialPlan::UseDns
+ } else {
+ DialPlan::Plan(plan_candidates)
+ }
+ }
+}
+
+impl TryFrom<&ControlIpCandidate<'_>> for DialCandidate {
+ type Error = ();
+
+ fn try_from(value: &ControlIpCandidate<'_>) -> Result<Self, Self::Error> {
+ let mode = if let Some(ace_host) = value.ace_host {
+ DialMode::Ace {
+ host: ace_host.to_string(),
+ ip: value.ip,
+ }
+ } else if let Some(ip) = value.ip {
+ DialMode::Ip(ip)
+ } else {
+ return Err(());
+ };
+
+ Ok(Self {
+ mode,
+ timeout: value.dial_timeout_sec,
+ start_delay_sec: value.dial_start_delay_sec,
+ priority: value.priority.clamp(0, 256) as _,
+ })
+ }
+}
diff --git a/deps/ts_control/src/lib.rs b/deps/ts_control/src/lib.rs
new file mode 100644
index 00000000..57789b6f
--- /dev/null
+++ b/deps/ts_control/src/lib.rs
@@ -0,0 +1,182 @@
+//! Client for the Tailscale control plane.
+
+extern crate alloc;
+
+/// Package version of `ts_control` as reported by cargo.
+// TODO(npry): this is used to populate Hostinfo.ipn_version, which requests "long format":
+// attach build info and whatever else that entails
+pub const PKG_VERSION: &str = if let Some(version) = option_env!("CARGO_PKG_VERSION") {
+ version
+} else {
+ ""
+};
+
+pub mod client;
+mod config;
+mod control_dialer;
+mod derp;
+mod dial_plan;
+mod map_request_builder;
+mod node;
+
+use std::fmt;
+
+pub use client::{FilterUpdate, PeerUpdate, RegistrationError, StateUpdate, connect, register};
+#[doc(inline)]
+pub use config::{Config, DEFAULT_CONTROL_SERVER};
+pub use control_dialer::{ControlDialer, TcpDialer, complete_connection};
+pub use derp::{Map as DerpMap, Region as DerpRegion, convert_derp_map};
+pub use dial_plan::{DialCandidate, DialMode, DialPlan};
+pub use map_request_builder::MapRequestBuilder;
+pub use node::{
+ DeviceInfo, Id as NodeId, Node, NodeLastSeen, NodeStatus, NodeUpdate, StableId as StableNodeId,
+ TailnetAddress,
+};
+pub use ts_control_serde::{Endpoint, EndpointType, MapRequest};
+
+/// An error which occurred while connecting to the control server or control plane.
+#[derive(Debug, thiserror::Error, Clone, Eq, PartialEq)]
+pub enum Error {
+ /// A machine was not authorized by control to join tailnet; authorize via the supplied URL.
+ #[error("machine was not authorized by control to join tailnet, authorize at {0}")]
+ MachineNotAuthorized(url::Url),
+
+ /// The user supplied an invalid URL.
+ #[error("invalid URL: {0}")]
+ InvalidUrl(url::Url),
+
+ /// Some kind of networking error.
+ ///
+ /// These might be addressed by retrying, or might be an unresolvable error.
+ ///
+ /// [`Operation`] is intended to be informational, rather then inspected during handling.
+ #[error("a networking error occurred in {0}")]
+ NetworkError(Operation),
+
+ /// An internal error that users of the library are not expected to handle.
+ ///
+ /// [`InternalErrorKind`] and [`Operation`] are intended to be informational, rather then
+ /// inspected during handling.
+ #[error("{0} error in {1}")]
+ Internal(InternalErrorKind, Operation),
+}
+
+impl Error {
+ fn io_error(err: std::io::Error, op: Operation) -> Self {
+ if crate::is_network_error(&err) {
+ Error::NetworkError(op)
+ } else {
+ Error::Internal(InternalErrorKind::Io, op)
+ }
+ }
+}
+
+/// What kind of internal error has occurred.
+///
+/// This is intended to be useful for reporting a crash to an end user, rather than being handled.
+#[non_exhaustive]
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum InternalErrorKind {
+ /// An error in URL parsing.
+ Url,
+ /// An unsuccessful HTTP request or upgrade.
+ Http,
+ /// An error in serialization or deserialization.
+ SerDe,
+ /// An error in I/O.
+ Io,
+ /// An invalid message format.
+ MessageFormat,
+ /// An error parsing a string as UTF8.
+ Utf8,
+ /// Noise framework handshake.
+ NoiseHandshake,
+ /// Tailscale challenge packet.
+ Challenge,
+ /// The user's machine was not authorized to register with a Tailnet and there is no URL for
+ /// the user to authorize at.
+ MachineAuthorization,
+}
+
+impl fmt::Display for InternalErrorKind {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ InternalErrorKind::Url => write!(f, "URL parsing error"),
+ InternalErrorKind::Http => write!(f, "unsuccessful HTTP request or upgrade"),
+ InternalErrorKind::SerDe => write!(f, "serialization/deserialization error"),
+ InternalErrorKind::Io => write!(f, "I/O error"),
+ InternalErrorKind::MessageFormat => write!(f, "message format error"),
+ InternalErrorKind::Utf8 => write!(f, "invalid UTF8"),
+ InternalErrorKind::NoiseHandshake => write!(f, "error in Noise handshake"),
+ InternalErrorKind::Challenge => write!(f, "error with Tailscale challenge packet"),
+ InternalErrorKind::MachineAuthorization => {
+ write!(f, "machine not authorized to register with Tailnet")
+ }
+ }
+ }
+}
+
+/// The phase of connecting the control plane to a Tailnet in which an error occurs.
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum Operation {
+ /// Requesting a net map.
+ MapRequest,
+ /// Connecting to a control server.
+ ConnectToControlServer,
+ /// Registering the user's device with a Tailnet.
+ Registration,
+}
+
+impl fmt::Display for Operation {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Operation::MapRequest => write!(f, "net map request"),
+ Operation::ConnectToControlServer => write!(f, "connection to control server"),
+ Operation::Registration => write!(f, "registration"),
+ }
+ }
+}
+
+impl From<ts_http_util::Error> for Error {
+ fn from(error: ts_http_util::Error) -> Self {
+ tracing::error!(%error, "http error");
+
+ if http_error_is_recoverable(error) {
+ Error::NetworkError(Operation::ConnectToControlServer)
+ } else {
+ Error::Internal(InternalErrorKind::Http, Operation::ConnectToControlServer)
+ }
+ }
+}
+
+/// Returns true if the input io error should be classed as a network error.
+fn is_network_error(err: &std::io::Error) -> bool {
+ use std::io::ErrorKind::*;
+ matches!(
+ err.kind(),
+ ConnectionRefused
+ | ConnectionReset
+ | HostUnreachable
+ | NetworkUnreachable
+ | ConnectionAborted
+ | NotConnected
+ | TimedOut
+ | AddrNotAvailable
+ | Interrupted
+ | NetworkDown
+ )
+}
+
+/// Returns true if the error is likely to be a transient network error.
+fn http_error_is_recoverable(error: ts_http_util::Error) -> bool {
+ match error {
+ ts_http_util::Error::Io => true,
+ ts_http_util::Error::InvalidInput
+ // A TCP timeout (recoverable) should get classed as an IO error, so any other kind of
+ // timeout is probably not.
+ | ts_http_util::Error::Timeout
+ | ts_http_util::Error::InvalidResponse => false,
+ // In the future, this might be recoverable with a reset.
+ ts_http_util::Error::ConnectionClosed => false,
+ }
+}
diff --git a/deps/ts_control/src/map_request_builder.rs b/deps/ts_control/src/map_request_builder.rs
new file mode 100644
index 00000000..33780cd8
--- /dev/null
+++ b/deps/ts_control/src/map_request_builder.rs
@@ -0,0 +1,109 @@
+use ts_capabilityversion::CapabilityVersion;
+use ts_control_serde::{Endpoint, HostInfo, MapRequest, NetInfo};
+
+/// Builder type for [`MapRequest`]s; smooths over the annoying parts of creating a request.
+#[derive(Debug, Clone)]
+pub struct MapRequestBuilder<'a> {
+ req: MapRequest<'a>,
+}
+
+impl<'a> MapRequestBuilder<'a> {
+ /// Create a new [`MapRequestBuilder`]. By default:
+ /// - [`MapRequest::keep_alive`] is `false`
+ /// - [`MapRequest::omit_peers`] is `true`
+ /// - [`MapRequest::stream`] is `false`
+ /// - [`MapRequest::host_info`]:
+ /// - [`HostInfo::hostname`] is populated from [`Config::hostname`][crate::Config::hostname]
+ /// - [`HostInfo::net_info`] is `None`, therefore:
+ /// - [`NetInfo::derp_latency`] is not populated
+ /// - [`NetInfo::preferred_derp`] is not populated
+ pub fn new(key_state: &ts_keys::NodeState) -> Self {
+ Self {
+ req: MapRequest {
+ version: CapabilityVersion::CURRENT,
+
+ keep_alive: false,
+ omit_peers: true,
+ stream: false,
+
+ node_key: key_state.node_keys.public,
+ disco_key: key_state.disco_keys.public,
+
+ host_info: Some(HostInfo::default()),
+ ..Default::default()
+ },
+ }
+ }
+
+ /// Consumes this [`MapRequestBuilder`] and returns a [`MapRequest`] with the configured
+ /// values.
+ pub fn build(self) -> MapRequest<'a> {
+ self.req
+ }
+
+ /// Set the [`MapRequest::keep_alive`] field.
+ pub const fn keep_alive(mut self, value: bool) -> Self {
+ self.req.keep_alive = value;
+ self
+ }
+
+ /// Set the [`MapRequest::omit_peers`] field.
+ pub const fn omit_peers(mut self, value: bool) -> Self {
+ self.req.omit_peers = value;
+ self
+ }
+
+ /// Set the [`MapRequest::stream`] field.
+ pub const fn stream(mut self, value: bool) -> Self {
+ self.req.stream = value;
+ self
+ }
+
+ /// Set the [`MapRequest::endpoints`] field.
+ pub fn endpoints(mut self, endpoints: Vec<Endpoint>) -> Self {
+ self.req.endpoints = endpoints;
+ self
+ }
+
+ /// Set the `stream`, `omit_peers`, and `keep_alive` flags so that this is treated as
+ /// a regular request to the API to update some node parameter (rather than as the long
+ /// poll stream).
+ pub const fn as_request(self) -> Self {
+ self.stream(false).omit_peers(true).keep_alive(false)
+ }
+
+ /// Set the `stream`, `omit_peers`, and `keep_alive` flags so that this is treated as
+ /// a long-poll stream.
+ pub const fn as_stream(self) -> Self {
+ self.stream(true).omit_peers(false).keep_alive(true)
+ }
+
+ /// Set the [`HostInfo::hostname`] field.
+ pub fn hostname(mut self, hostname: &'a str) -> Self {
+ self.host_info_mut().hostname = Some(hostname);
+ self
+ }
+
+ /// Set the [`NetInfo::preferred_derp`] field (inside [`MapRequest::host_info`] ->
+ /// [`HostInfo::net_info`]).
+ pub fn preferred_derp(mut self, value: ts_derp::RegionId) -> Self {
+ self.net_info_mut().preferred_derp = Some(value.0.into());
+ self
+ }
+
+ /// Set the [`NetInfo::derp_latency`] field (inside [`MapRequest::host_info`] ->
+ /// [`HostInfo::net_info`]).
+ pub fn derp_latencies(mut self, value: impl IntoIterator<Item = (&'a str, f64)>) -> Self {
+ self.net_info_mut().derp_latency = Some(value.into_iter().collect());
+
+ self
+ }
+
+ fn host_info_mut(&mut self) -> &mut HostInfo<'a> {
+ self.req.host_info.get_or_insert_default()
+ }
+
+ fn net_info_mut(&mut self) -> &mut NetInfo<'a> {
+ self.host_info_mut().net_info.get_or_insert_default()
+ }
+}
diff --git a/deps/ts_control/src/node.rs b/deps/ts_control/src/node.rs
new file mode 100644
index 00000000..4d6d7b65
--- /dev/null
+++ b/deps/ts_control/src/node.rs
@@ -0,0 +1,471 @@
+use alloc::collections::BTreeMap;
+use core::{
+ fmt,
+ net::{IpAddr, SocketAddr},
+};
+
+use chrono::{DateTime, Utc};
+use ts_capabilityversion::CapabilityVersion;
+use ts_keys::{DiscoPublicKey, MachinePublicKey, NodePublicKey};
+
+const LAST_SEEN_FORMAT: &str = "%F %T %Z";
+
+/// The unique id of a node.
+pub type Id = i64;
+
+/// The stable ID of a node.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
+pub struct StableId(pub String);
+
+/// Timestamps indicating when an offline node was last connected to the control plane. Only applies
+/// to nodes that are offline (disconnected from the control plane).
+///
+/// If populated, most `control` timestamp values are only accurate to a resolution of ~10 minutes
+/// for privacy reasons. See each variant for important information on comparison of timestamp
+/// values and which clock is used to generate a timestamp.
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub struct NodeLastSeen {
+ /// The last time the node was connected to the control plane, as reported by the control plane
+ /// itself. In some cases, the control plane does not report a "last seen" timestamp, just that
+ /// a node is offline; in those cases, this field will be `None`.
+ ///
+ /// The timestamp may be rounded to the nearest 10-minute boundary before being reported to this
+ /// device by the control plane; in other words, many `control` timestamps will be aligned to
+ /// 10-minute boundaries. This is a privacy-preserving measure implemented by the control plane.
+ /// Note that we have seen some non-rounded timestamps being reported from the control plane,
+ /// although it's unclear if this is a bug or by design.
+ ///
+ /// Note that the timestamp value was generated by the control plane. The control plane's
+ /// "clock" isn't synchronized with the local device's clock, so the timestamp value may be
+ /// much more than 5 minutes in the past/future when compared to the `estimated` timestamp/local
+ /// device clock. Do not directly compare `NodeLastSeen::control` timestamps with
+ /// `NodeLastSeen::estimated` timestamps.
+ pub control: Option<DateTime<Utc>>,
+ /// The last time the node was connected to the control plane, as estimated by this device. The
+ /// timestamp is roughly the time this device was notified the node was offline by the control
+ /// plane, using the local device clock.
+ ///
+ /// Note that the timestamp value was generated by the local device clock. The control plane's
+ /// "clock" isn't synchronized with the local device's clock, so the timestamp value may differ
+ /// greatly from the `control` timestamp/control plane clock. Do not directly compare
+ /// `NodeLastSeen::control` timestamps with `NodeLastSeen::estimated` timestamps.
+ pub estimated: DateTime<Utc>,
+}
+
+impl Default for NodeLastSeen {
+ fn default() -> Self {
+ Self {
+ control: None,
+ // We intentionally don't fuzz the estimated timestamp value because the Go client code
+ // doesn't either.
+ // See: https://github.com/tailscale/tailscale/blob/ee0a03b140021541495b25bdb6642b589431758b/control/controlclient/map.go#L753-L764
+ estimated: Utc::now(),
+ }
+ }
+}
+
+impl fmt::Display for NodeLastSeen {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "last seen: {} (by control: ",
+ self.estimated.format(LAST_SEEN_FORMAT)
+ )?;
+ match self.control {
+ None => write!(f, "unknown)"),
+ Some(dt) => write!(f, "{})", dt.format(LAST_SEEN_FORMAT)),
+ }
+ }
+}
+
+impl NodeLastSeen {
+ /// Construct a new `NodeLastSeen` with an optional "last seen by control" timestamp. The
+ /// `estimated` timestamp is always set to the current UTC date/time.
+ pub fn new(control: Option<DateTime<Utc>>) -> Self {
+ Self {
+ control,
+ ..Default::default()
+ }
+ }
+}
+
+/// Whether a node is online (connected to the control plane) or offline (disconnected from the
+/// control plane).
+#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
+pub enum NodeStatus {
+ /// The node may be online or offline; the control plane hasn't informed us yet.
+ #[default]
+ Unknown,
+ /// The node is online (connected to the control plane).
+ Online,
+ /// The node is offline (disconnected from the control plane).
+ Offline(NodeLastSeen),
+}
+
+impl fmt::Display for NodeStatus {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ NodeStatus::Unknown => write!(f, "unknown"),
+ NodeStatus::Online => write!(f, "online"),
+ NodeStatus::Offline(nls) => write!(f, "offline, {nls}"),
+ }
+ }
+}
+
+impl NodeStatus {
+ /// Construct a new `NodeStatus` from a combination of online and last-seen timestamp.
+ ///
+ /// The netmap messages we get from the control plane have multiple ways to indicate a peer is
+ /// online and/or the timestamp the peer was last connected to the control plane. This method
+ /// wraps the sometimes-painful logic of creating a `NodeStatus` from the various combinations
+ /// of netmap field values.
+ pub fn new(online: Option<bool>, last_seen: Option<DateTime<Utc>>) -> Self {
+ match (online, last_seen) {
+ (Some(true), None) => Self::Online,
+ (Some(false), None) => Self::Offline(NodeLastSeen::new(None)),
+ (Some(false), Some(dt)) | (None, Some(dt)) => {
+ Self::Offline(NodeLastSeen::new(Some(dt)))
+ }
+ (None, None) => Self::Unknown,
+ (online, last_seen) => {
+ tracing::warn!(
+ ?online,
+ ?last_seen,
+ "unexpected combination of online/last_seen states"
+ );
+ Self::Unknown
+ }
+ }
+ }
+}
+
+/// A node in a tailnet.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Node {
+ /// The node's id.
+ pub id: Id,
+ /// The node's stable id.
+ pub stable_id: StableId,
+ /// The node's hostname.
+ pub hostname: String,
+
+ /// The node's capability version.
+ pub capability_version: CapabilityVersion,
+
+ /// Whether the node is connected to the control plane or not. If [`NodeStatus::Unknown`], the
+ /// control plane hasn't told us yet.
+ ///
+ /// This field does _not_ indicate whether the node is reachable or visible from this device.
+ pub status: NodeStatus,
+
+ /// The tailnet this node belongs to.
+ pub tailnet: Option<String>,
+
+ /// The node capabilities assigned to this node.
+ pub node_capabilities: BTreeMap<String, Vec<String>>,
+
+ /// The tags assigned to this node.
+ pub tags: Vec<String>,
+
+ /// The address of the node in the tailnet.
+ pub tailnet_address: TailnetAddress,
+
+ /// The node's [`NodePublicKey`].
+ pub node_key: NodePublicKey,
+ /// The node key's expiration.
+ pub node_key_expiry: Option<DateTime<Utc>>,
+
+ /// The node's [`MachinePublicKey`], if known.
+ pub machine_key: Option<MachinePublicKey>,
+ /// The node's [`DiscoPublicKey`], if known.
+ pub disco_key: Option<DiscoPublicKey>,
+ /// The signature of the node's public key with the Tailnet Lock signing key, if Tailnet Lock
+ /// is enabled and the signature is known.
+ pub tailnet_lock_key_signature: Option<Vec<u8>>,
+
+ /// The routes this node accepts traffic for.
+ pub accepted_routes: Vec<ipnet::IpNet>,
+ /// The underlay addresses this node is reachable on (`Endpoints` in Go).
+ pub underlay_addresses: Vec<SocketAddr>,
+
+ /// The DERP region for this node, if known.
+ pub derp_region: Option<ts_derp::RegionId>,
+
+ /// What the node reports about the device it runs on.
+ pub device: DeviceInfo,
+}
+
+/// What a node reports about the device it runs on, from its Hostinfo. A field
+/// the node leaves out is empty.
+#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
+pub struct DeviceInfo {
+ /// The operating system, as Tailscale names it: "macOS", "iOS", "android", "linux",
+ /// "windows", ...
+ pub os: String,
+ /// The Linux distribution: "debian", "ubuntu", "nixos", ...
+ pub distro: String,
+ /// The model of a phone or tablet: "iPhone12,3", "Pixel 3a", ...
+ pub device_model: String,
+}
+
+impl Node {
+ /// Apply the given partial update to this `Node`.
+ ///
+ /// # Panics
+ /// If `update.id` does not match `self.id`. The caller must guarantee this method is only
+ /// called with [`NodeUpdate`]s for this `Node`.
+ #[tracing::instrument(skip_all, fields(id = self.stable_id.0, hostname = self.hostname))]
+ pub fn apply_update(&mut self, update: &NodeUpdate) {
+ assert_eq!(self.id, update.id, "node update ID != node ID");
+
+ if update.status != NodeStatus::Unknown {
+ tracing::debug!(old_status = %self.status, "peer {}", update.status);
+ self.status = update.status;
+ }
+
+ if let Some(cap) = update.cap {
+ tracing::debug!(old=?self.capability_version, new=?cap, "updating capability version");
+ self.capability_version = cap;
+ }
+
+ if let Some(cap_map) = update.cap_map.as_ref() {
+ tracing::debug!(old=?self.node_capabilities, new=?cap_map, "updating node capabilities");
+ self.node_capabilities = cap_map.clone();
+ }
+
+ if let Some(derp_region) = update.derp_region {
+ tracing::debug!(old=?self.derp_region, new=?derp_region, "updating derp home region");
+ self.derp_region = Some(derp_region);
+ }
+
+ if let Some(disco_key) = update.disco_key {
+ tracing::debug!(old=?self.disco_key, new=?disco_key, "updating disco key");
+ self.disco_key = Some(disco_key);
+ }
+
+ if let Some(node_key) = update.node_key {
+ tracing::debug!(old=?self.node_key, new=?node_key, "updating node key");
+ self.node_key = node_key;
+ }
+
+ if let Some(node_key_expiry) = update.node_key_expiry {
+ tracing::debug!(old=?self.node_key_expiry, new=?node_key_expiry, "updating node key expiry");
+ self.node_key_expiry = Some(node_key_expiry);
+ }
+
+ if let Some(tl_sig) = &update.tailnet_lock_key_signature {
+ tracing::debug!(old=?self.tailnet_lock_key_signature, new=?tl_sig, "updating tailnet lock key signature");
+ self.tailnet_lock_key_signature = Some(tl_sig.clone());
+ }
+
+ if let Some(underlay_addresses) = &update.underlay_addresses {
+ tracing::debug!(old=?self.underlay_addresses, new=?underlay_addresses, "updating underlay addresses");
+ self.underlay_addresses = underlay_addresses.clone();
+ }
+ }
+
+ /// The fully-qualified domain name of the node.
+ ///
+ /// This is a string of the form `$HOST.$TAILNET_DOMAIN.`. For tailnets controlled by
+ /// Tailscale's control plane, this usually means `$HOST.tail1234.ts.net.`
+ ///
+ /// The `trailing_dot` parameter specifies whether to include the trailing dot in the
+ /// fqdn. This is included by the definition of FQDN, and is the way the Go codebase
+ /// formats this field, but the parameter is included to allow turning it off for use
+ /// in contexts that expect it to be absent.
+ pub fn fqdn(&self, trailing_dot: bool) -> String {
+ let dot = if trailing_dot { "." } else { "" };
+ match &self.tailnet {
+ Some(tailnet) => format!("{}.{tailnet}{dot}", self.hostname),
+ None => format!("{}{dot}", self.hostname),
+ }
+ }
+
+ /// The fully-qualified domain name of the node, only returning `Some` if the tailnet
+ /// component is present.
+ ///
+ /// See [`Node::fqdn`].
+ pub fn fqdn_opt(&self, trailing_dot: bool) -> Option<String> {
+ let dot = if trailing_dot { "." } else { "" };
+ let tailnet = self.tailnet.as_deref()?;
+
+ Some(format!("{}.{tailnet}{dot}", self.hostname))
+ }
+
+ /// Report whether this node matches the given `name`.
+ ///
+ /// `name` is checked for equality with both this node's bare hostname and its fqdn. A
+ /// trailing `.` may be present.
+ pub fn matches_name(&self, name: &str) -> bool {
+ // This approach is taken to avoid allocating a buffer just for the sake of making this
+ // comparison: try to chop `.tailnet.` off of the end of `name` and compare the
+ // remainder to our hostname. If `.tailnet.` doesn't match `name`, we'll end up comparing
+ // our hostname to `hostname.other_tailnet.`, which won't succeed. If `name` was just the
+ // hostname, nothing will have been chopped, so the comparison will still be hostname-to-
+ // hostname.
+
+ let name = name.strip_suffix('.').unwrap_or(name);
+
+ let name = if let Some(tailnet) = &self.tailnet {
+ name.strip_suffix(tailnet.as_str())
+ .and_then(|name| name.strip_suffix('.'))
+ .unwrap_or(name)
+ } else {
+ name
+ };
+
+ name == self.hostname
+ }
+}
+
+/// Addresses for a node within a tailnet.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct TailnetAddress {
+ /// The IPv4 address of the node in the tailnet.
+ pub ipv4: ipnet::Ipv4Net,
+ /// The IPv6 address of the node in the tailnet.
+ pub ipv6: ipnet::Ipv6Net,
+}
+
+impl TailnetAddress {
+ /// Report whether `addr` matches either address in this [`TailnetAddress`].
+ pub fn contains(&self, addr: IpAddr) -> bool {
+ match addr {
+ IpAddr::V4(a) => self.ipv4.addr() == a,
+ IpAddr::V6(a) => self.ipv6.addr() == a,
+ }
+ }
+}
+
+impl From<&ts_control_serde::Node<'_>> for Node {
+ fn from(value: &ts_control_serde::Node) -> Self {
+ let fqdn_without_trailing_dot = value.name.strip_suffix('.').unwrap_or(value.name);
+
+ let (hostname, tailnet) = match fqdn_without_trailing_dot.split_once('.') {
+ Some((hostname, tailnet)) => (hostname, Some(tailnet.to_owned())),
+ None => (fqdn_without_trailing_dot, None),
+ };
+
+ Self {
+ id: value.id,
+ stable_id: StableId(value.stable_id.0.to_string()),
+ hostname: hostname.to_owned(),
+
+ capability_version: value.cap,
+
+ status: NodeStatus::new(value.online, value.last_seen),
+ tailnet,
+
+ node_capabilities: value
+ .cap_map
+ .iter()
+ .map(|(k, v)| (k.to_string(), v.into()))
+ .collect(),
+ tags: value
+ .tags
+ .as_ref()
+ .map(|x| x.iter().map(|x| x.to_string()).collect())
+ .unwrap_or_default(),
+
+ tailnet_address: TailnetAddress {
+ ipv4: value.addresses.0,
+ ipv6: value.addresses.1,
+ },
+ node_key: value.key,
+ node_key_expiry: value.key_expiry,
+ machine_key: value.machine,
+ disco_key: value.disco_key,
+ tailnet_lock_key_signature: value.key_signature.as_ref().map(|s| Vec::<u8>::from(*s)),
+
+ accepted_routes: value
+ .allowed_ips
+ .clone()
+ .unwrap_or_else(|| vec![value.addresses.0.into(), value.addresses.1.into()]),
+ underlay_addresses: value.endpoints.clone(),
+
+ // legacy_derp_string is still in practical use as of 3/2026
+ #[allow(deprecated)]
+ derp_region: value
+ .home_derp
+ .or(value.legacy_derp_string)
+ .or_else(|| value.host_info.net_info.as_ref()?.preferred_derp)
+ .map(|x| ts_derp::RegionId(x.into())),
+
+ device: DeviceInfo {
+ os: value.host_info.os.to_owned(),
+ distro: value.host_info.distro.to_owned(),
+ device_model: value.host_info.device_model.to_owned(),
+ },
+ }
+ }
+}
+
+/// A partial update to a [`Node`] in a tailnet.
+///
+/// Devices should reject any `NodeUpdate` that it doesn't have a corresponding [`Node`] for. This
+/// type combines multiple different update/patch-style fields from netmap messages into a single
+/// type to simplify update handling for interested components, such as the peer tracker.
+#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
+pub struct NodeUpdate {
+ /// The node's id.
+ pub id: Id,
+
+ /// Whether this node is online or offline (with last-seen timestamp), according to the control
+ /// plane. If [`NodeStatus::Unknown`], has not changed.
+ pub status: NodeStatus,
+
+ /// The DERP region for this node. If `None`, has not changed.
+ pub derp_region: Option<ts_derp::RegionId>,
+
+ /// The node's capability version. If `None`, has not changed.
+ pub cap: Option<CapabilityVersion>,
+ /// The node's capabilities (node caps, not peer caps). If `None`, has not changed.
+ pub cap_map: Option<BTreeMap<String, Vec<String>>>,
+
+ /// The node's [`NodePublicKey`]. If `None`, has not changed.
+ pub node_key: Option<NodePublicKey>,
+ /// The node key's expiration. If `None`, has not changed.
+ pub node_key_expiry: Option<DateTime<Utc>>,
+
+ /// The node's [`DiscoPublicKey`]. If `None`, has not changed.
+ pub disco_key: Option<DiscoPublicKey>,
+
+ /// The node's key signature for Tailnet Lock. If `None`, has not changed.
+ pub tailnet_lock_key_signature: Option<Vec<u8>>,
+
+ /// The underlay addresses this node is reachable on (`Endpoints` in Go). If `None`, has not
+ /// changed.
+ pub underlay_addresses: Option<Vec<SocketAddr>>,
+}
+
+impl From<&ts_control_serde::PeerChange<'_>> for NodeUpdate {
+ fn from(value: &ts_control_serde::PeerChange<'_>) -> Self {
+ let cap_map = value.cap_map.as_ref().map(|m| {
+ m.iter()
+ .map(|(name, values)| {
+ (
+ String::from(*name),
+ values
+ .0
+ .iter()
+ .map(|v| v.to_string())
+ .collect::<Vec<String>>(),
+ )
+ })
+ .collect()
+ });
+
+ Self {
+ id: value.node_id,
+ status: NodeStatus::new(value.online, value.last_seen),
+ derp_region: value.derp_region.map(|x| ts_derp::RegionId(x.into())),
+ cap: value.cap,
+ cap_map,
+ node_key: value.key,
+ node_key_expiry: value.key_expiry,
+ disco_key: value.disco_key,
+ tailnet_lock_key_signature: value.key_signature.map(|x| x.into()),
+ underlay_addresses: value.endpoints.clone(),
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/Cargo.toml b/deps/ts_netstack_smoltcp_core/Cargo.toml
new file mode 100644
index 00000000..a6c9db61
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/Cargo.toml
@@ -0,0 +1,123 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2024"
+rust-version = "1.92.0"
+name = "ts_netstack_smoltcp_core"
+version = "0.6.1"
+build = false
+autolib = false
+autobins = false
+autoexamples = false
+autotests = false
+autobenches = false
+description = "command-channel-based userspace netstack built on smoltcp (core functionality)"
+readme = "README.md"
+keywords = [
+ "tailscale",
+ "network",
+ "smoltcp",
+ "netstack",
+]
+categories = [
+ "no-std",
+ "network-programming",
+]
+license = "BSD-3-Clause"
+repository = "https://github.com/tailscale/tailscale-rs"
+
+[features]
+std = []
+
+[lib]
+name = "ts_netstack_smoltcp_core"
+path = "src/lib.rs"
+
+[[test]]
+name = "udp"
+path = "tests/udp.rs"
+
+[dependencies.bytes]
+version = "1"
+
+[dependencies.flume]
+version = "0.12"
+
+[dependencies.heapless]
+version = "0.9"
+
+[dependencies.smoltcp]
+version = "0.13"
+features = [
+ "alloc",
+ "proto-ipv6",
+ "proto-ipv6-fragmentation",
+ "proto-ipv4",
+ "proto-ipv4-fragmentation",
+ "medium-ip",
+ "socket-tcp",
+ "socket-udp",
+ "socket-icmp",
+ "socket-raw",
+]
+default-features = false
+
+[dependencies.thiserror]
+version = "2"
+default-features = false
+
+[dependencies.tracing]
+version = "0.1"
+features = ["attributes"]
+default-features = false
+
+[dev-dependencies.smoltcp]
+version = "0.13"
+features = [
+ "alloc",
+ "proto-ipv6",
+ "proto-ipv6-fragmentation",
+ "proto-ipv4",
+ "proto-ipv4-fragmentation",
+ "medium-ip",
+ "socket-tcp",
+ "socket-udp",
+ "socket-icmp",
+ "socket-raw",
+ "iface-max-addr-count-4",
+ "log",
+ "verbose",
+]
+default-features = false
+
+[dev-dependencies.tracing-panic]
+version = "0.1"
+features = ["capture-backtrace"]
+
+[lints.clippy]
+multiple_crate_versions = "allow"
+too_many_arguments = "allow"
+
+[lints.clippy.cargo]
+level = "warn"
+priority = -1
+
+[lints.rust]
+closure_returning_async_block = "warn"
+let-underscore = "warn"
+missing_docs = "warn"
+
+[lints.rustdoc]
+bare_urls = "deny"
+broken_intra_doc_links = "deny"
+invalid_html_tags = "deny"
+private_intra_doc_links = "deny"
diff --git a/deps/ts_netstack_smoltcp_core/LICENSE b/deps/ts_netstack_smoltcp_core/LICENSE
new file mode 100644
index 00000000..adcd4c5b
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/LICENSE
@@ -0,0 +1,28 @@
+BSD 3-Clause License
+
+Copyright (c) 2026 Tailscale Inc & contributors.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/deps/ts_netstack_smoltcp_core/PATENTS b/deps/ts_netstack_smoltcp_core/PATENTS
new file mode 100644
index 00000000..560a2b8f
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/PATENTS
@@ -0,0 +1,24 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Tailscale Inc. as part of the Tailscale project.
+
+Tailscale Inc. hereby grants to You a perpetual, worldwide,
+non-exclusive, no-charge, royalty-free, irrevocable (except as stated
+in this section) patent license to make, have made, use, offer to
+sell, sell, import, transfer and otherwise run, modify and propagate
+the contents of this implementation of Tailscale, where such license
+applies only to those patent claims, both currently owned or
+controlled by Tailscale Inc. and acquired in the future, licensable
+by Tailscale Inc. that are necessarily infringed by this
+implementation of Tailscale. This grant does not include claims that
+would be infringed only as a consequence of further modification of
+this implementation. If you or your agent or exclusive licensee
+institute or order or agree to the institution of patent litigation
+against any entity (including a cross-claim or counterclaim in a
+lawsuit) alleging that this implementation of Tailscale or any code
+incorporated within this implementation of Tailscale constitutes
+direct or contributory patent infringement, or inducement of patent
+infringement, then any patent rights granted to you under this License
+for this implementation of Tailscale shall terminate as of the date
+such litigation is filed.
diff --git a/deps/ts_netstack_smoltcp_core/src/command/channel.rs b/deps/ts_netstack_smoltcp_core/src/command/channel.rs
new file mode 100644
index 00000000..cba8cd31
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/channel.rs
@@ -0,0 +1,95 @@
+use core::borrow::Borrow;
+
+use smoltcp::iface::SocketHandle;
+
+use crate::{ChannelClosedError, Command, Error, Netstack, Response, command::Request};
+
+/// Channel type through which commands to the netstack flow.
+// NOTE(npry): command channels are weak because the netstack holds a strong sender handle
+// internally, as flume doesn't support cloning out senders from just a receiver. If this
+// ever fails to upgrade, it means that the netstack we're talking to has been dropped, which is
+// pretty error-shaped (the commands you submit over the channel will never complete). Maintaining
+// this as a weak channel forces callers to handle the upgrade-failure case.
+pub type Channel = flume::WeakSender<Request>;
+
+/// Helper methods for types that can provide a [`Channel`].
+pub trait HasChannel {
+ /// Retrieve a [`Channel`] borrow.
+ ///
+ /// This is the only required method for this trait.
+ fn borrow_channel(&self) -> impl Borrow<Channel> + Send;
+
+ /// Clone a new command channel.
+ fn command_channel(&self) -> Channel {
+ self.borrow_channel().borrow().clone()
+ }
+
+ /// Send a request through the channel.
+ ///
+ /// Convenience wrapper around [`request_blocking`][crate::request_blocking].
+ fn request_blocking(
+ &self,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+ ) -> Result<Response, ChannelClosedError> {
+ crate::request_blocking(self.borrow_channel().borrow(), handle, command)
+ }
+
+ /// Asynchronously send a request through the channel.
+ ///
+ /// Convenience wrapper around [`request`][crate::request].
+ fn request(
+ &self,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+ ) -> impl Future<Output = Result<Response, Error>> + Send {
+ let ch = self.command_channel();
+ let command = command.into();
+
+ async move { crate::request(&ch, handle, command).await }
+ }
+
+ /// Send a nonblocking request, discarding any response.
+ ///
+ /// This is mainly intended for use in [`Drop`] implementations, as they can't be `async`
+ /// and it would be frequently be surprising if they blocked the calling thread.
+ ///
+ /// Convenience wrapper around [`request_nonblocking`][crate::request_nonblocking].
+ fn request_nonblocking(
+ &self,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+ ) -> Result<(), ChannelClosedError> {
+ crate::request_nonblocking(self.borrow_channel().borrow(), handle, command)
+ }
+}
+
+impl HasChannel for Netstack {
+ fn borrow_channel(&self) -> impl Borrow<Channel> + Send {
+ self.command_tx.downgrade()
+ }
+}
+
+impl HasChannel for Channel {
+ fn borrow_channel(&self) -> impl Borrow<Channel> + Send {
+ self
+ }
+}
+
+impl<T> HasChannel for &T
+where
+ T: HasChannel,
+{
+ fn borrow_channel(&self) -> impl Borrow<Channel> + Send {
+ (**self).borrow_channel()
+ }
+}
+
+impl<T> HasChannel for &mut T
+where
+ T: HasChannel,
+{
+ fn borrow_channel(&self) -> impl Borrow<Channel> + Send {
+ (**self).borrow_channel()
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/error.rs b/deps/ts_netstack_smoltcp_core/src/command/error.rs
new file mode 100644
index 00000000..c404f228
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/error.rs
@@ -0,0 +1,245 @@
+use core::fmt;
+
+use smoltcp::socket::{
+ icmp::BindError as IcmpBindError,
+ raw::BindError as RawBindError,
+ tcp::{ConnectError, ListenError, SendError as TcpSendError},
+ udp::{BindError as UdpBindError, SendError as UdpSendError},
+};
+
+use crate::command;
+
+/// Error while interacting with the netstack.
+#[derive(thiserror::Error, Debug, Clone, Copy, Eq, PartialEq)]
+pub enum Error {
+ /// The request contained invalid parameters. Retrying will not resolve this issue.
+ ///
+ /// Common causes for this error are that a specified address was invalid, there is not enough
+ /// memory available in the current configuration to complete the request, or the packet to be
+ /// sent was too large for the socket's buffer capacity.
+ #[error("invalid request ({0})")]
+ BadRequest(BadRequestReason),
+
+ /// An internal error occurred.
+ ///
+ /// These errors are either unrecoverable, due to a bug in our code, or caused by a rare
+ /// and intermittent issue.
+ #[error("an internal error occurred: {0}")]
+ Internal(InternalErrorKind),
+
+ /// A TCP connection was reset.
+ ///
+ /// These errors can often be handled by retrying.
+ #[error("connection reset")]
+ ConnectionReset,
+}
+
+impl Error {
+ /// Error constructor for the wrong type of command response.
+ pub fn wrong_type() -> Self {
+ Error::Internal(InternalErrorKind::InternalResponseMismatch)
+ }
+
+ /// Error constructor for an invalid socket state.
+ pub fn invalid_socket_state() -> Self {
+ Error::Internal(InternalErrorKind::InvalidSocketState)
+ }
+
+ /// Error constructor for a missing TCP listener.
+ pub fn missing_listener() -> Self {
+ Error::Internal(InternalErrorKind::BadListenerHandle)
+ }
+
+ /// Error constructor for a missing socket.
+ pub fn missing_socket() -> Self {
+ Error::Internal(InternalErrorKind::BadSocketHandle)
+ }
+
+ /// Error constructor for an over-size packet.
+ pub fn big_packet() -> Self {
+ Error::BadRequest(BadRequestReason::BigPacket)
+ }
+
+ /// Error constructor for out-of-memory (likely in a specific component, rather than a system OOM).
+ pub fn buffer_full() -> Self {
+ Error::Internal(InternalErrorKind::BufferFull)
+ }
+
+ /// Error constructor for an unaddressable packet.
+ pub fn unaddressable() -> Self {
+ Error::BadRequest(BadRequestReason::Unaddressable)
+ }
+
+ /// Error constructor for a zero-sized buffer.
+ pub fn zero_buffer() -> Self {
+ Error::BadRequest(BadRequestReason::ZeroSizeBuffer)
+ }
+
+ /// Tried to send a packet with the wrong IP version.
+ pub const fn wrong_ip_version() -> Self {
+ Error::BadRequest(BadRequestReason::WrongIpVersion)
+ }
+}
+
+/// Informational detail on the kind of internal error.
+///
+/// This detail is unlikely to help in error recovery but could be used in logs or user-facing
+/// reporting to add detail to an error report.
+// If adding variants, be sure to add them to `tailscale::error::InternalErrorKind` too (including
+// the `From` impl, which will panic otherwise).
+#[non_exhaustive]
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum InternalErrorKind {
+ /// Invalid socket state.
+ InvalidSocketState,
+ /// Response type mismatched to request type.
+ InternalResponseMismatch,
+ /// Channel closed.
+ InternalChannelClosed,
+ /// Handle to invalid TCP listener.
+ BadListenerHandle,
+ /// Handle to invalid socket.
+ BadSocketHandle,
+ /// buffer full
+ BufferFull,
+}
+
+impl fmt::Display for InternalErrorKind {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ InternalErrorKind::InvalidSocketState => write!(f, "invalid socket state"),
+ InternalErrorKind::InternalResponseMismatch => {
+ write!(f, "response type mismatched to request type")
+ }
+ InternalErrorKind::InternalChannelClosed => write!(f, "channel closed"),
+ InternalErrorKind::BadListenerHandle => write!(f, "handle to invalid TCP listener"),
+ InternalErrorKind::BadSocketHandle => write!(f, "handle to invalid socket"),
+ InternalErrorKind::BufferFull => write!(f, "buffer full"),
+ }
+ }
+}
+
+/// The reason for an [`Error::BadRequest`] error.
+#[non_exhaustive]
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum BadRequestReason {
+ /// Packet unaddressable.
+ Unaddressable,
+ /// A packet is too large to ever fit in socket buffer; even if the buffer is emptied, this error
+ /// will still occur.
+ BigPacket,
+ /// The size of a user-supplied buffer is zero, so cannot store a packet.
+ ZeroSizeBuffer,
+ /// The requested IP was the wrong type.
+ ///
+ /// I.e., tried to send to an IPv6 peer on an IPv4 socket or vice versa.
+ WrongIpVersion,
+}
+
+impl fmt::Display for BadRequestReason {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ BadRequestReason::Unaddressable => write!(f, "packet unaddressable"),
+ BadRequestReason::BigPacket => {
+ write!(f, "packet is too large to ever fit in socket buffer")
+ }
+ BadRequestReason::ZeroSizeBuffer => {
+ write!(f, "the size of a user-supplied buffer is zero")
+ }
+ BadRequestReason::WrongIpVersion => {
+ write!(f, "wrong ip version")
+ }
+ }
+ }
+}
+
+impl From<Error> for command::Response {
+ fn from(value: Error) -> Self {
+ command::Response::Error(value)
+ }
+}
+
+impl<T> From<flume::SendError<T>> for Error {
+ fn from(_: flume::SendError<T>) -> Self {
+ Error::Internal(InternalErrorKind::InternalChannelClosed)
+ }
+}
+
+impl From<flume::RecvError> for Error {
+ fn from(_: flume::RecvError) -> Self {
+ Error::Internal(InternalErrorKind::InternalChannelClosed)
+ }
+}
+
+impl From<ListenError> for Error {
+ fn from(value: ListenError) -> Self {
+ match value {
+ ListenError::InvalidState => Error::invalid_socket_state(),
+ ListenError::Unaddressable => Error::unaddressable(),
+ }
+ }
+}
+
+impl From<ConnectError> for Error {
+ fn from(value: ConnectError) -> Self {
+ match value {
+ ConnectError::InvalidState => Error::invalid_socket_state(),
+ ConnectError::Unaddressable => Error::unaddressable(),
+ }
+ }
+}
+
+impl From<UdpBindError> for Error {
+ fn from(value: UdpBindError) -> Self {
+ match value {
+ UdpBindError::InvalidState => Error::invalid_socket_state(),
+ UdpBindError::Unaddressable => Error::unaddressable(),
+ }
+ }
+}
+
+impl From<RawBindError> for Error {
+ fn from(value: RawBindError) -> Self {
+ match value {
+ RawBindError::InvalidState => Error::invalid_socket_state(),
+ RawBindError::Unaddressable => Error::unaddressable(),
+ }
+ }
+}
+
+impl From<IcmpBindError> for Error {
+ fn from(value: IcmpBindError) -> Self {
+ match value {
+ IcmpBindError::InvalidState => Error::invalid_socket_state(),
+ IcmpBindError::Unaddressable => Error::unaddressable(),
+ }
+ }
+}
+
+impl From<TcpSendError> for Error {
+ fn from(value: TcpSendError) -> Self {
+ match value {
+ TcpSendError::InvalidState => Error::ConnectionReset,
+ }
+ }
+}
+
+impl From<UdpSendError> for Error {
+ fn from(value: UdpSendError) -> Self {
+ match value {
+ UdpSendError::Unaddressable => Error::unaddressable(),
+ UdpSendError::BufferFull => Error::buffer_full(),
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl From<Error> for std::io::Error {
+ fn from(value: Error) -> Self {
+ match value {
+ Error::BadRequest(_) => std::io::ErrorKind::InvalidInput.into(),
+ Error::ConnectionReset => std::io::ErrorKind::ConnectionReset.into(),
+ other => std::io::Error::other(other),
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/mod.rs b/deps/ts_netstack_smoltcp_core/src/command/mod.rs
new file mode 100644
index 00000000..e881bd30
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/mod.rs
@@ -0,0 +1,138 @@
+//! Command API definition.
+
+use core::fmt::Debug;
+
+use flume::Sender;
+use smoltcp::iface::SocketHandle;
+
+mod channel;
+mod error;
+pub mod raw;
+mod request;
+pub mod stack_control;
+pub mod tcp;
+pub mod udp;
+
+pub use channel::{Channel, HasChannel};
+pub use error::{Error, InternalErrorKind};
+pub use request::{ChannelClosedError, request, request_blocking, request_nonblocking};
+
+/// Request to a netstack bearing a command to execute.
+///
+/// This wraps [`Command`] to provide additional common metadata, including the handle to
+/// the relevant socket (if there is one) and a channel through which the netstack should
+/// respond to the request.
+pub struct Request {
+ /// Socket handle this message is associated with, if any.
+ pub handle: Option<SocketHandle>,
+ /// The command to run.
+ pub command: Command,
+ /// Oneshot response channel.
+ pub resp: Sender<Response>,
+}
+
+/// Command that a netstack should execute.
+#[derive(Debug)]
+pub enum Command {
+ /// Commands that configure the network stack itself (e.g. set interface IPs).
+ StackControl(stack_control::Command),
+
+ /// Commands for TCP listeners.
+ TcpListen(tcp::listen::Command),
+ /// Commands for TCP streams.
+ TcpStream(tcp::stream::Command),
+ /// Commands for UDP sockets.
+ Udp(udp::Command),
+ /// Commands for raw sockets.
+ Raw(raw::Command),
+}
+
+/// Response to a command.
+#[derive(Debug)]
+pub enum Response {
+ /// Unit return: operation succeeded with nothing to report.
+ Ok,
+
+ /// Operation failed with the given error.
+ Error(Error),
+
+ /// Operation is waiting for data: requeue it and try it again later.
+ ///
+ /// This variant is never returned to the caller over the response channel, it is just
+ /// used for signaling internally.
+ WouldBlock {
+ /// The socket handle to retry against.
+ handle: Option<SocketHandle>,
+ /// The command to retry.
+ command: Command,
+ },
+
+ /// Responses for UDP sockets.
+ Udp(udp::Response),
+ /// Responses for TCP listeners.
+ TcpListen(tcp::listen::Response),
+ /// Responses for TCP streams.
+ TcpStream(tcp::stream::Response),
+ /// Responses for raw sockets.
+ Raw(raw::Response),
+}
+
+macro_rules! impl_try_from {
+ ($ty:ty, $variant:ident) => {
+ impl TryFrom<Response> for $ty {
+ type Error = Error;
+
+ fn try_from(response: Response) -> Result<Self, Self::Error> {
+ match response {
+ Response::$variant(resp) => Ok(resp),
+ Response::Error(e) => Err(e),
+ _ => Err(Error::wrong_type()),
+ }
+ }
+ }
+ };
+}
+
+impl_try_from!(udp::Response, Udp);
+impl_try_from!(raw::Response, Raw);
+impl_try_from!(tcp::stream::Response, TcpStream);
+impl_try_from!(tcp::listen::Response, TcpListen);
+
+impl Response {
+ /// Check if this response is the `Ok` variant, returning an error if not.
+ pub fn to_ok(self) -> Result<(), Error> {
+ match self {
+ Response::Ok => Ok(()),
+ Response::Error(e) => Err(e),
+ _ => Err(Error::wrong_type()),
+ }
+ }
+}
+
+/// Attempt to convert a [`Response`] into a specific variant.
+///
+/// The second argument is a pattern, as if it were being used in a match expression.
+///
+/// # Example
+///
+/// ```rust
+/// extern crate ts_netstack_smoltcp_core as netcore;
+/// use netcore::{tcp, try_response_as};
+/// # fn main() -> Result<(), netcore::Error> {
+/// # let resp: netcore::Response = tcp::stream::Response::Sent { n: 42 }.into();
+///
+/// try_response_as!(resp, tcp::stream::Response::Sent { n });
+/// assert_eq!(n, 42);
+/// # Ok(())
+/// # }
+/// ```
+#[macro_export]
+macro_rules! try_response_as {
+ ($resp:expr, $pat:pat) => {
+ let $pat = $resp.try_into()? else {
+ ::tracing::error!("invalid response type");
+
+ return Err($crate::Error::wrong_type().into());
+ };
+ };
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/raw.rs b/deps/ts_netstack_smoltcp_core/src/command/raw.rs
new file mode 100644
index 00000000..7e3b1efa
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/raw.rs
@@ -0,0 +1,107 @@
+//! Commands for raw sockets.
+
+use core::{
+ fmt::{Debug, Formatter},
+ num::NonZeroUsize,
+};
+
+use bytes::Bytes;
+use smoltcp::{iface::SocketHandle, wire};
+
+use crate::command;
+
+/// Commands to control raw IP sockets.
+pub enum Command {
+ /// Open a raw socket with the given IP version and protocol.
+ ///
+ /// This socket should be expected to intercept _all_ matching traffic, overriding
+ /// protocol-specific sockets.
+ Open {
+ /// The IP version the socket will match.
+ ip_version: wire::IpVersion,
+ /// The IP protocol the socket will match.
+ protocol: wire::IpProtocol,
+ },
+ /// Send a raw IP packet.
+ Send {
+ /// The raw packet bytes.
+ buf: Bytes,
+ },
+ /// Receive a raw IP packet.
+ Recv {
+ /// If `Some`, limit the max length of the response to this length.
+ ///
+ /// [`Response::Recv::truncated`] will be populated if there was not enough space
+ /// available.
+ max_len: Option<NonZeroUsize>,
+ },
+ /// Close the socket.
+ ///
+ /// This message must be sent once socket operations are complete to avoid a resource
+ /// leak; we have no other way to detect that the remote socket channel has closed.
+ Close,
+}
+
+impl From<Command> for command::Command {
+ fn from(value: Command) -> Self {
+ command::Command::Raw(value)
+ }
+}
+
+impl Debug for Command {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Open {
+ ip_version,
+ protocol,
+ } => f
+ .debug_struct("Open")
+ .field("ip_version", ip_version)
+ .field("protocol", protocol)
+ .finish(),
+ Self::Send { buf } => f.debug_struct("Send").field("buf_len", &buf.len()).finish(),
+ Self::Recv { max_len } => f.debug_struct("Recv").field("max_len", max_len).finish(),
+ Self::Close => f.write_str("Close"),
+ }
+ }
+}
+
+/// Responses to raw socket [`Command`]s.
+pub enum Response {
+ /// Socket was opened successfully.
+ Opened {
+ /// The handle of the opened socket.
+ handle: SocketHandle,
+ },
+
+ /// An IP packet was received.
+ Recv {
+ /// The raw packet payload.
+ buf: Bytes,
+
+ /// If `Some`, the `buf` field was truncated because the [`Command::Recv::max_len`]
+ /// was shorter than the actual received packet length.
+ ///
+ /// Holds the original length of `buf` before truncation.
+ truncated: Option<usize>,
+ },
+}
+
+impl From<Response> for command::Response {
+ fn from(value: Response) -> Self {
+ Self::Raw(value)
+ }
+}
+
+impl Debug for Response {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Opened { handle } => f.debug_struct("Opened").field("handle", handle).finish(),
+ Self::Recv { buf, truncated } => f
+ .debug_struct("Recv")
+ .field("buf_len", &buf.len())
+ .field("truncated", truncated)
+ .finish(),
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/request.rs b/deps/ts_netstack_smoltcp_core/src/command/request.rs
new file mode 100644
index 00000000..c33b783e
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/request.rs
@@ -0,0 +1,161 @@
+//! Helpers to simplify sending requests.
+
+use core::borrow::Borrow;
+
+use smoltcp::iface::SocketHandle;
+
+use crate::{
+ Error,
+ command::{Command, Request, Response},
+};
+
+/// Helper trait to abstract over [`flume::Sender`] and [`flume::WeakSender`].
+pub trait UpgradableChannel {
+ /// Attempt to upgrade this value to [`flume::Sender`].
+ fn upgrade(&self) -> Result<impl Borrow<flume::Sender<Request>>, ChannelClosedError>;
+}
+
+/// An error signifying that the remote end of the channel has closed.
+#[derive(Debug, thiserror::Error, Clone, Copy, Eq, PartialEq)]
+#[error("the remote end of the channel has closed")]
+pub struct ChannelClosedError;
+
+impl From<ChannelClosedError> for Error {
+ fn from(_: ChannelClosedError) -> Self {
+ Error::Internal(crate::InternalErrorKind::InternalChannelClosed)
+ }
+}
+
+#[cfg(feature = "std")]
+impl From<ChannelClosedError> for std::io::Error {
+ fn from(_: ChannelClosedError) -> Self {
+ std::io::ErrorKind::BrokenPipe.into()
+ }
+}
+
+impl UpgradableChannel for flume::Sender<Request> {
+ fn upgrade(&self) -> Result<impl Borrow<flume::Sender<Request>>, ChannelClosedError> {
+ Ok(self)
+ }
+}
+
+impl UpgradableChannel for flume::WeakSender<Request> {
+ fn upgrade(&self) -> Result<impl Borrow<flume::Sender<Request>>, ChannelClosedError> {
+ flume::WeakSender::<Request>::upgrade(self).ok_or(ChannelClosedError)
+ }
+}
+
+impl<T> UpgradableChannel for &T
+where
+ T: UpgradableChannel + ?Sized,
+{
+ fn upgrade(&self) -> Result<impl Borrow<flume::Sender<Request>>, ChannelClosedError> {
+ T::upgrade(self)
+ }
+}
+
+/// Synchronously make a request over the given command channel.
+///
+/// Blocks on command submission and on receipt of a [`Response`].
+pub fn request_blocking(
+ command_tx: impl UpgradableChannel,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+) -> Result<Response, ChannelClosedError> {
+ // wrapper to minimize monomorphization impact
+ fn _request_blocking(
+ command_tx: &flume::Sender<Request>,
+ handle: Option<SocketHandle>,
+ command: Command,
+ ) -> Result<Response, ChannelClosedError> {
+ let (resp_tx, resp_rx) = flume::bounded(1);
+
+ command_tx
+ .send(Request {
+ handle,
+ command,
+ resp: resp_tx,
+ })
+ .map_err(|_| ChannelClosedError)?;
+
+ resp_rx.recv().map_err(|_| ChannelClosedError)
+ }
+
+ let ch = command_tx.upgrade()?;
+ let ch = ch.borrow();
+
+ _request_blocking(ch, handle, command.into())
+}
+
+/// Make a request over the given command channel.
+///
+/// Blocks on command submission and the receipt of a [`Response`].
+pub fn request(
+ command_tx: impl UpgradableChannel,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+) -> impl Future<Output = Result<Response, Error>> + Send {
+ // wrapper to minimize monomorphization impact
+ async fn _request(
+ command_tx: &flume::Sender<Request>,
+ handle: Option<SocketHandle>,
+ command: Command,
+ ) -> Result<Response, Error> {
+ let (resp_tx, resp_rx) = flume::bounded(1);
+
+ command_tx
+ .send_async(Request {
+ handle,
+ command,
+ resp: resp_tx,
+ })
+ .await?;
+
+ resp_rx.recv_async().await.map_err(Error::from)
+ }
+
+ // impl Future and the returned async block below are required to do this bit of work upgrading
+ // the channel and converting the command outside the async context for lifetime reasons
+ let ch = command_tx.upgrade().map(|x| x.borrow().clone());
+ let command = command.into();
+
+ async move {
+ let ch = ch?;
+ _request(&ch, handle, command).await
+ }
+}
+
+/// Send a request without blocking on the response.
+///
+/// Returns an error only if the request can't be sent.
+///
+/// Mainly intended for use in [`Drop`] implementations, as they can't be `async`
+/// and it would be frequently be surprising if they blocked the calling thread.
+pub fn request_nonblocking(
+ command_tx: impl UpgradableChannel,
+ handle: Option<SocketHandle>,
+ command: impl Into<Command>,
+) -> Result<(), ChannelClosedError> {
+ // wrapper to minimize monomorphization impact
+ fn _request_nonblocking(
+ command_tx: &flume::Sender<Request>,
+ handle: Option<SocketHandle>,
+ command: Command,
+ ) -> Result<(), ChannelClosedError> {
+ let (resp_tx, _resp_rx) = flume::bounded(1);
+
+ match command_tx.try_send(Request {
+ handle,
+ command,
+ resp: resp_tx,
+ }) {
+ Ok(_) | Err(flume::TrySendError::Full(_)) => Ok(()),
+ Err(flume::TrySendError::Disconnected(_)) => Err(ChannelClosedError),
+ }
+ }
+
+ let ch = command_tx.upgrade()?;
+ let ch = ch.borrow();
+
+ _request_nonblocking(ch, handle, command.into())
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/stack_control.rs b/deps/ts_netstack_smoltcp_core/src/command/stack_control.rs
new file mode 100644
index 00000000..3159a541
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/stack_control.rs
@@ -0,0 +1,29 @@
+//! Commands that mutate network stack configuration.
+
+use alloc::vec::Vec;
+use core::net::IpAddr;
+
+use crate::command;
+
+/// Mutate the network stack's configuration.
+#[derive(Debug)]
+pub enum Command {
+ /// Set the network interface's IPs.
+ SetIps {
+ /// IPs to assign to the netstack's interface.
+ ///
+ /// If the netstack was configured with
+ /// [`Config::loopback`](crate::Config::loopback) enabled, the loopback addresses
+ /// should not be included here.
+ ///
+ /// May fail if `smoltcp` was not configured with a sufficient
+ /// `iface-max-addr-count-*` (feature flag).
+ new_ips: Vec<IpAddr>,
+ },
+}
+
+impl From<Command> for command::Command {
+ fn from(command: Command) -> Self {
+ command::Command::StackControl(command)
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/tcp/listen.rs b/deps/ts_netstack_smoltcp_core/src/command/tcp/listen.rs
new file mode 100644
index 00000000..31b4a4b8
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/tcp/listen.rs
@@ -0,0 +1,62 @@
+//! TCP listener commands.
+
+use core::net::SocketAddr;
+
+use smoltcp::iface::SocketHandle;
+
+use crate::{command, socket_impl::tcp::ListenerHandle};
+
+/// Commands to control TCP listeners.
+#[derive(Debug)]
+pub enum Command {
+ /// Begin listening on the given endpoint.
+ Listen {
+ /// The endpoint to begin listening on.
+ local_endpoint: SocketAddr,
+ },
+
+ /// Accept an incoming connection on the given listener.
+ ///
+ /// Response channel blocks until a connection is made.
+ Accept {
+ /// The handle of the listener to accept on.
+ handle: ListenerHandle,
+ },
+
+ /// Close the given listener.
+ ///
+ /// Happy-path response: [`Response::Ok`][command::Response::Ok].
+ Close {
+ /// The handle of hte listener to close.
+ handle: ListenerHandle,
+ },
+}
+
+impl From<Command> for command::Command {
+ fn from(value: Command) -> Self {
+ command::Command::TcpListen(value)
+ }
+}
+
+/// Responses to TCP listener [`Command`]s.
+#[derive(Debug)]
+pub enum Response {
+ /// Successfully listening on the requested endpoint.
+ Listening {
+ /// Handle of the new listener.
+ handle: ListenerHandle,
+ },
+ /// Successfully accepted an incoming TCP connection.
+ Accepted {
+ /// Address of the remote that initiated the connection.
+ remote: SocketAddr,
+ /// Handle of the new TCP connection.
+ handle: SocketHandle,
+ },
+}
+
+impl From<Response> for command::Response {
+ fn from(value: Response) -> Self {
+ Self::TcpListen(value)
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/tcp/mod.rs b/deps/ts_netstack_smoltcp_core/src/command/tcp/mod.rs
new file mode 100644
index 00000000..f8ecd5e9
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/tcp/mod.rs
@@ -0,0 +1,4 @@
+//! TCP listener and stream commands.
+
+pub mod listen;
+pub mod stream;
diff --git a/deps/ts_netstack_smoltcp_core/src/command/tcp/stream.rs b/deps/ts_netstack_smoltcp_core/src/command/tcp/stream.rs
new file mode 100644
index 00000000..fd53da21
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/tcp/stream.rs
@@ -0,0 +1,137 @@
+//! Messages for TCP streams.
+
+use core::{
+ fmt::{Debug, Formatter},
+ net::SocketAddr,
+};
+
+use bytes::Bytes;
+use smoltcp::iface::SocketHandle;
+
+use crate::command;
+
+/// The connection was reset or closed.
+#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
+#[error("connection reset or closed")]
+pub struct ConnectionResetOrClosed;
+
+#[cfg(feature = "std")]
+impl From<ConnectionResetOrClosed> for std::io::Error {
+ fn from(_: ConnectionResetOrClosed) -> Self {
+ std::io::ErrorKind::ConnectionReset.into()
+ }
+}
+
+/// Commands for controlling TCP streams.
+pub enum Command {
+ /// Connect from the given local endpoint to the given remote endpoint.
+ ///
+ /// Neither endpoint may use an unspecified (zero) port.
+ Connect {
+ /// The local endpoint to connect from.
+ local_endpoint: SocketAddr,
+ /// The remote endpoint to connect to.
+ remote_endpoint: SocketAddr,
+ },
+
+ /// Receive data incoming on the socket.
+ ///
+ /// Blocks until at least one byte can be received (or the remote closes its end of the
+ /// stream).
+ Recv {
+ /// If `Some`, limit the length of the received data to at most the contained value.
+ /// Otherwise, no limit.
+ ///
+ /// The payload may contain less data than specified here.
+ ///
+ /// Intended to be used to emulate socket APIs where the caller provides a byte
+ /// buffer -- e.g. `sock.recv(&mut [u8])` -- where providing more data than the
+ /// available buffer length would cause data loss.
+ max_len: Option<usize>,
+ },
+
+ /// Send bytes over the connection.
+ ///
+ /// Blocks until at least one byte can be sent.
+ Send {
+ /// Bytes to send over the connection.
+ buf: Bytes,
+ },
+
+ /// Close this connection.
+ ///
+ /// This message causes the connection to enter the closing state, but responds
+ /// immediately -- the netstack does not wait to respond until the state machine
+ /// finishes closing gracefully (this occurs in the background).
+ Close,
+}
+
+impl Debug for Command {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Connect {
+ local_endpoint,
+ remote_endpoint,
+ } => f
+ .debug_struct("Connect")
+ .field("local_endpoint", local_endpoint)
+ .field("remote_endpoint", remote_endpoint)
+ .finish(),
+ Self::Recv { max_len } => f.debug_struct("Recv").field("max_len", max_len).finish(),
+ Self::Send { buf } => f.debug_struct("Send").field("buf_len", &buf.len()).finish(),
+ Self::Close => f.write_str("Close"),
+ }
+ }
+}
+
+impl From<Command> for command::Command {
+ fn from(value: Command) -> Self {
+ command::Command::TcpStream(value)
+ }
+}
+
+/// Responses to TCP stream [`Command`]s.
+pub enum Response {
+ /// Connection opened successfully.
+ Connected {
+ /// Handle for the newly created socket.
+ handle: SocketHandle,
+ },
+
+ /// Sent `n` bytes over the connection
+ Sent {
+ /// The number of bytes of the original buffer that were accepted to be sent.
+ ///
+ /// Always at least 1.
+ n: usize,
+ },
+
+ /// Received the contained bytes from the remote.
+ Recv {
+ /// Bytes received from the remote.
+ buf: Bytes,
+ },
+
+ /// The remote has closed the sending side of its connection and will not send any more
+ /// data; EOF.
+ Finished,
+}
+
+impl From<Response> for command::Response {
+ fn from(value: Response) -> Self {
+ Self::TcpStream(value)
+ }
+}
+
+impl Debug for Response {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Connected { handle } => {
+ f.debug_struct("Connected").field("handle", handle).finish()
+ }
+ Self::Sent { n } => f.debug_struct("Sent").field("n", n).finish(),
+ Self::Recv { buf } => f.debug_struct("Recv").field("buf_len", &buf.len()).finish(),
+ Self::Finished => f.write_str("Finished"),
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/command/udp.rs b/deps/ts_netstack_smoltcp_core/src/command/udp.rs
new file mode 100644
index 00000000..de50369d
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/command/udp.rs
@@ -0,0 +1,122 @@
+//! Commands for UDP sockets.
+
+use core::{
+ fmt::{Debug, Formatter},
+ net::SocketAddr,
+ num::NonZeroUsize,
+};
+
+use bytes::Bytes;
+use smoltcp::iface::SocketHandle;
+
+use crate::command;
+
+/// Commands driving UDP sockets.
+pub enum Command {
+ /// Bind a new socket to the given endpoint.
+ Bind {
+ /// The endpoint to bind to.
+ ///
+ /// The port on this endpoint may not be unspecified.
+ endpoint: SocketAddr,
+ },
+
+ /// Send a message to the given endpoint.
+ Send {
+ /// The endpoint to send the message to.
+ endpoint: SocketAddr,
+ /// The message payload.
+ buf: Bytes,
+ },
+
+ /// Receive a packet incoming on the socket.
+ Recv {
+ /// If `Some`, limit the length of the received packet to at most the contained
+ /// value.
+ ///
+ /// If the actual received length of the packet was longer, this may lead to
+ /// payload truncation, indicated by the presence of the
+ /// [`Response::RecvFrom::truncated`] field.
+ ///
+ /// For use in emulating an API like `recv_from(&mut [u8])`, where the caller
+ /// supplies the buffer.
+ max_len: Option<NonZeroUsize>,
+ },
+
+ /// Close the socket.
+ Close,
+}
+
+impl Debug for Command {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Bind { endpoint } => f.debug_struct("Bind").field("endpoint", endpoint).finish(),
+ Self::Send { endpoint, buf } => f
+ .debug_struct("Send")
+ .field("endpoint", endpoint)
+ .field("buf_len", &buf.len())
+ .finish(),
+ Self::Recv { max_len } => f.debug_struct("Recv").field("max_len", max_len).finish(),
+ Self::Close => f.debug_struct("Close").finish(),
+ }
+ }
+}
+
+impl From<Command> for command::Command {
+ fn from(value: Command) -> Self {
+ command::Command::Udp(value)
+ }
+}
+
+/// UDP response messages.
+pub enum Response {
+ /// Socket was bound successfully.
+ Bound {
+ /// The bound socket's handle.
+ handle: SocketHandle,
+ /// The local endpoint to which the socket was bound.
+ local: SocketAddr,
+ },
+ /// A packet was received.
+ RecvFrom {
+ /// The remote address that sent the packet.
+ remote: SocketAddr,
+
+ /// The packet payload.
+ buf: Bytes,
+
+ /// If present, indicates that the packet held more data than is in the `buf` field,
+ /// which was truncated because [`Command::Recv::max_len`] was set.
+ ///
+ /// The contained value is the original length of the packet before truncation.
+ truncated: Option<usize>,
+ },
+}
+
+impl From<Response> for command::Response {
+ fn from(value: Response) -> Self {
+ Self::Udp(value)
+ }
+}
+
+impl Debug for Response {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::Bound { handle, local } => f
+ .debug_struct("Bound")
+ .field("handle", handle)
+ .field("local", local)
+ .finish(),
+ Self::RecvFrom {
+ remote,
+ buf,
+ truncated,
+ } => f
+ .debug_struct("RecvFrom")
+ .field("remote", remote)
+ .field("buf_len", &buf.len())
+ .field("truncated", truncated)
+ .finish(),
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/config.rs b/deps/ts_netstack_smoltcp_core/src/config.rs
new file mode 100644
index 00000000..35a27074
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/config.rs
@@ -0,0 +1,47 @@
+/// Netstack configuration.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Config {
+ /// Capacity of the command channel.
+ ///
+ /// If `None`, the channel is unbounded.
+ pub command_channel_capacity: Option<usize>,
+
+ /// Maximum transmission unit of the underlying net device.
+ pub mtu: usize,
+
+ /// Assign the IPv4 and IPv6 loopback addresses to the interface.
+ pub loopback: bool,
+
+ /// The default size of buffer allocated for each UDP socket created.
+ pub udp_buffer_size: usize,
+ /// The default number of pending messages supported for each UDP socket created.
+ pub udp_message_count: usize,
+
+ /// The default size of buffer allocated for each TCP socket created.
+ pub tcp_buffer_size: usize,
+
+ /// The default size of buffer allocated for each raw socket.
+ pub raw_buffer_size: usize,
+ /// The default number of pending messages supported for each raw socket.
+ pub raw_message_count: usize,
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ command_channel_capacity: Some(32),
+
+ mtu: 1280,
+
+ loopback: false,
+
+ udp_buffer_size: 1024 * 4,
+ udp_message_count: 32,
+
+ tcp_buffer_size: 1024 * 16,
+
+ raw_buffer_size: 1024 * 4,
+ raw_message_count: 32,
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/lib.rs b/deps/ts_netstack_smoltcp_core/src/lib.rs
new file mode 100644
index 00000000..bc7f78e9
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/lib.rs
@@ -0,0 +1,643 @@
+//! Command-channel-based userspace netstack built on smoltcp (core functionality).
+#![no_std]
+
+extern crate alloc;
+
+#[cfg(feature = "std")]
+extern crate std;
+
+pub extern crate flume;
+pub extern crate smoltcp;
+
+use alloc::{
+ collections::{BTreeMap, VecDeque},
+ vec,
+ vec::Vec,
+};
+use core::{
+ net::IpAddr,
+ pin::Pin,
+ task::{Context, Poll},
+};
+
+use smoltcp::{
+ iface::{PollIngressSingleResult, PollResult, SocketHandle},
+ wire::{HardwareAddress, IpAddress},
+};
+
+mod command;
+mod config;
+mod pipe;
+mod socket_impl;
+mod stack_control_impl;
+mod util;
+mod wake_device;
+
+#[doc(inline)]
+pub use command::{
+ Channel, ChannelClosedError, Command, Error, HasChannel, InternalErrorKind, Request, Response,
+ raw, request, request_blocking, request_nonblocking, stack_control, tcp, udp,
+};
+pub use config::Config;
+pub use pipe::{Pipe, PipeDev};
+pub use socket_impl::tcp::ListenerHandle as TcpListenerHandle;
+pub use stack_control_impl::NetstackControl;
+use util::NoopCapDev;
+pub use util::{DisplayExt, DisplayToDebug, OptionExt, ResultExt};
+pub use wake_device::AsyncWakeDevice;
+
+/// Internally i/o free userspace network stack built around `smoltcp`.
+pub struct Netstack {
+ config: Config,
+
+ iface: smoltcp::iface::Interface,
+ socket_set: smoltcp::iface::SocketSet<'static>,
+
+ command_rx: flume::Receiver<Request>,
+ // Need to hold the sender to avoid closing the channel.
+ command_tx: flume::Sender<Request>,
+
+ /// Commands pending in a wouldblock state: to be processed again in the future for
+ /// completion.
+ blocked_commands: VecDeque<Request>,
+
+ /// Set of TCP socket handles that are expected to close in the future, held onto for
+ /// graceful shutdown.
+ pending_tcp_closes: Vec<SocketHandle>,
+
+ /// Active TCP listeners.
+ ///
+ /// These are registered here so that they can be polled to accept incoming connections
+ /// without an explicit accept command: internally accepted connections are stored in
+ /// a queue in the state, and the accept command just dequeues and returns the first
+ /// ready one.
+ tcp_listeners: BTreeMap<socket_impl::tcp::ListenerHandle, socket_impl::tcp::TcpListenerState>,
+ next_tcp_listener_id: usize,
+}
+
+impl Netstack {
+ /// Construct a netstack with the given config and starting instant.
+ ///
+ /// # Panics
+ ///
+ /// If `ns_config.loopback` is set and smoltcp's `iface-max-addr-count` (feature flag)
+ /// is less than 2.
+ pub fn new(ns_config: Config, now: smoltcp::time::Instant) -> Netstack {
+ let config = smoltcp::iface::Config::new(HardwareAddress::Ip);
+
+ let mut iface = smoltcp::iface::Interface::new(
+ config,
+ &mut NoopCapDev::with_caps(|caps| {
+ caps.max_transmission_unit = ns_config.mtu;
+ }),
+ now,
+ );
+
+ if ns_config.loopback {
+ iface.update_ip_addrs(|addrs| {
+ if !set_loopback(addrs) {
+ panic!();
+ }
+ });
+ }
+
+ let (tx, rx) = match ns_config.command_channel_capacity {
+ Some(cap) => flume::bounded(cap),
+ None => flume::unbounded(),
+ };
+
+ Netstack {
+ iface,
+ socket_set: smoltcp::iface::SocketSet::new(vec![]),
+ command_tx: tx,
+ command_rx: rx,
+ config: ns_config,
+ blocked_commands: Default::default(),
+ pending_tcp_closes: Default::default(),
+ tcp_listeners: Default::default(),
+ next_tcp_listener_id: 0,
+ }
+ }
+
+ /// Report the next time the netstack should be polled.
+ pub fn poll_at(&mut self, now: smoltcp::time::Instant) -> Option<smoltcp::time::Instant> {
+ self.iface.poll_at(now, &self.socket_set)
+ }
+
+ /// Report the amount of time until the netstack should next be polled.
+ pub fn poll_delay(&mut self, now: smoltcp::time::Instant) -> Option<core::time::Duration> {
+ self.iface
+ .poll_delay(now, &self.socket_set)
+ .map(|x| x.into())
+ }
+
+ /// Process all commands available in the command queue.
+ #[tracing::instrument(skip_all)]
+ pub fn process_cmds(&mut self) {
+ while let Ok(cmd) = self.command_rx.try_recv() {
+ self.process_one_cmd(cmd);
+ }
+ }
+
+ /// Synchronously block for a single command over the channel.
+ #[tracing::instrument(skip_all, fields(?timeout))]
+ pub fn wait_for_cmd_blocking(
+ &mut self,
+ timeout: Option<core::time::Duration>,
+ ) -> Result<Request, flume::RecvTimeoutError> {
+ if let Some(timeout) = timeout {
+ self.command_rx.recv_timeout(timeout)
+ } else {
+ self.command_rx
+ .recv()
+ .map_err(|flume::RecvError::Disconnected| flume::RecvTimeoutError::Disconnected)
+ }
+ }
+
+ /// Asynchronously wait for a single command over the channel.
+ #[tracing::instrument(skip_all)]
+ pub fn wait_for_cmd(&self) -> impl Future<Output = Option<Request>> + use<> {
+ let rx = self.command_rx.clone();
+
+ async move { rx.recv_async().await.ok() }
+ }
+
+ /// Set the IP addresses for this interface.
+ ///
+ /// Loopback addresses are automatically appended if indicated by [`Config::loopback`].
+ ///
+ /// The return value reports whether the operation was successful: if not, it was
+ /// because there wasn't enough storage configured in smoltcp's feature flags for the
+ /// number of submitted interface IPs.
+ pub fn direct_set_ips(&mut self, ips: impl IntoIterator<Item = IpAddr>) -> bool {
+ const fn full_prefix_len(is_ipv4: bool) -> u8 {
+ if is_ipv4 { 32 } else { 128 }
+ }
+
+ let mut ok = true;
+
+ self.iface.update_ip_addrs(|stored_ips| {
+ stored_ips.clear();
+
+ for ip in ips.into_iter() {
+ let cidr = smoltcp::wire::IpCidr::new(ip.into(), full_prefix_len(ip.is_ipv4()));
+
+ if stored_ips.push(cidr).is_err() {
+ ok = false;
+ break;
+ }
+ }
+
+ if ok && self.config.loopback {
+ ok = ok && set_loopback(stored_ips);
+ }
+ });
+
+ ok
+ }
+
+ /// Process a single command.
+ #[tracing::instrument(skip_all, fields(?command, ?handle))]
+ pub fn process_one_cmd(
+ &mut self,
+ Request {
+ command,
+ handle,
+ resp,
+ }: Request,
+ ) {
+ let cmd_resp = match command {
+ Command::StackControl(cmd) => self.process_stack_control(cmd),
+ Command::Udp(udp) => self.process_udp(udp, handle),
+ Command::TcpStream(tcp) => self.process_tcp_stream(tcp, handle),
+ Command::TcpListen(listen) => self.process_tcp_listen(listen, handle),
+ Command::Raw(raw) => self.process_raw(raw, handle),
+ };
+ tracing::trace!(?cmd_resp, "command processed");
+
+ match cmd_resp {
+ Response::WouldBlock { command, handle } => {
+ self.blocked_commands.push_back(Request {
+ command,
+ handle,
+ resp,
+ });
+ }
+ otherwise => {
+ if let Response::Error(e) = &otherwise {
+ tracing::debug!(error = %e, "command error");
+ }
+
+ if let Err(resp) = resp.send(otherwise) {
+ tracing::debug!(resp = ?resp.0, "response channel closed");
+ }
+ }
+ }
+ }
+
+ /// Poll the lower device to send and receive packets, and attempt to complete any
+ /// blocked socket commands.
+ ///
+ /// Returns whether there were any updates to socket state.
+ #[tracing::instrument(skip_all, fields(%now))]
+ pub fn poll_device_io(
+ &mut self,
+ now: smoltcp::time::Instant,
+ dev: &mut impl smoltcp::phy::Device,
+ ) -> bool {
+ use smoltcp::iface::{PollIngressSingleResult, PollResult};
+
+ // Goal of this function: complete all _synchronously_ available work given what is queued
+ // in the underlying device and the netstack state. It may be a long time until this
+ // function is called again, so leaving any available work for the next iteration could
+ // potentially stall out sockets with I/O in flight until the next poll.
+ //
+ // It requires a bit of care to determine when it's possible that the netstack made
+ // progress and should try to communicate with the lower dev. Specifically, while (as of
+ // smoltcp 0.12) packet egress does not itself perform loopback (outgoing packets are always
+ // emitted to the underlying device, even if the address belongs to this node or a route
+ // points back at us), it's possible that `dev` could synchronously perform loopback after
+ // egressing packets, i.e. they would immediately (synchronously) become available for
+ // ingress again. This means that after we perform egress and any sockets make progress, we
+ // need to try ingress. And after any ingress, TCP state machines may have (synchronously)
+ // made progress and want to emit packets -- hence, we need to poll egress again.
+ //
+ // This is why this function is structured as a loop: we need to keep polling until both
+ // ingress and egress report that they are done and no sockets have changed state.
+ // Unfortunately poll_egress is O(n) in the number of sockets, but that's an unavoidable
+ // cost of correctness given the design of smoltcp.
+
+ let mut changed = false;
+
+ self.pump_waiters();
+
+ 'outer: loop {
+ let mut changed_this_iter = false;
+
+ let span_ingress = tracing::trace_span!("ingress");
+
+ span_ingress.in_scope(|| {
+ loop {
+ match self
+ .iface
+ .poll_ingress_single(now, dev, &mut self.socket_set)
+ {
+ PollIngressSingleResult::None => {
+ break;
+ }
+ PollIngressSingleResult::PacketProcessed => {}
+ PollIngressSingleResult::SocketStateChanged => {
+ changed = true;
+ changed_this_iter = true;
+ self.pump_tcp_accept();
+
+ tracing::trace!("socket state changed");
+ }
+ }
+ }
+ });
+
+ if changed_this_iter {
+ // TODO(npry): need to validate through more thorough inspection of smoltcp
+ // source, but I don't _think_ the below comment is true: since receive()
+ // provides a TxToken, TCP devices should actually not need to be pumped. We
+ // may still want to call pump_blocked_commands to ready receives. Leaving
+ // this in for now as a conservative measure to ensure correctness, even if
+ // it costs a bit of performance.
+
+ // Ingress can cause egress: TCP state machines may have advanced and want to send
+ // packets now.
+ //
+ // Also unblocks any sockets that were waiting for a recv.
+ self.pump_waiters();
+ }
+
+ let _span = tracing::trace_span!("egress").entered();
+
+ match self.iface.poll_egress(now, dev, &mut self.socket_set) {
+ PollResult::SocketStateChanged => {
+ changed = true;
+ tracing::trace!("socket state changed");
+
+ // Egress may have opened capacity in packet buffers for
+ // e.g. pending TCP or UDP sends, which we may be able to complete + send now.
+ //
+ // Need to fall through to the top of the loop in this case to ensure that
+ // if the underlying device synchronously looped back any packets, they're
+ // ingressed and processed by any waiting sockets.
+ self.pump_waiters();
+ }
+ PollResult::None => break 'outer,
+ }
+ }
+
+ if changed {
+ self.drain_tcp_closes();
+ }
+
+ changed
+ }
+
+ /// Poll the device for all synchronously-available packets and process them.
+ ///
+ /// The return value indicates whether packets were consumed and if sockets made
+ /// progress:
+ ///
+ /// - `Poll::Pending`: no packets were processed because we were waiting for them to
+ /// arrive from `dev.
+ /// - `Poll::Ready(false)`: we received packets but socket state did not make progress
+ /// - `Poll::Ready(true)`: we received packets and socket state _did_ make progress
+ ///
+ /// When this function returns, it is guaranteed that all packets currently available to
+ /// receive from `dev` have been processed.
+ #[tracing::instrument(skip_all, fields(%now), ret, level = "trace")]
+ pub fn poll_device_ingress_async(
+ &mut self,
+ cx: &mut core::task::Context<'_>,
+ now: smoltcp::time::Instant,
+ mut dev: Pin<&mut (impl AsyncWakeDevice + smoltcp::phy::Device + Unpin)>,
+ ) -> Poll<bool> {
+ let mut changed = false;
+ let mut polled_successfully = false;
+
+ loop {
+ match dev.as_mut().poll_rx(cx) {
+ Poll::Ready(()) => {
+ polled_successfully = true;
+ }
+ Poll::Pending => {
+ // If we get a pending now but we have already polled successfully, don't return
+ // pending (we need to report that we made progress).
+ return if polled_successfully {
+ Poll::Ready(changed)
+ } else {
+ Poll::Pending
+ };
+ }
+ }
+
+ let _span = tracing::trace_span!("poll_ingress_single").entered();
+ match self
+ .iface
+ .poll_ingress_single(now, dev.as_mut().get_mut(), &mut self.socket_set)
+ {
+ PollIngressSingleResult::None => {
+ break;
+ }
+ PollIngressSingleResult::PacketProcessed => {}
+ PollIngressSingleResult::SocketStateChanged => {
+ changed = true;
+ self.pump_tcp_accept();
+ tracing::trace!("socket state changed");
+ }
+ }
+ }
+
+ if changed {
+ // TODO(npry): need to validate through more thorough inspection of smoltcp
+ // source, but I don't _think_ the below comment is true: since receive()
+ // provides a TxToken, TCP devices should actually not need to be pumped. We may
+ // still want to call pump_blocked_commands to ready receives. Leaving this in
+ // for now as a conservative measure to ensure correctness, even if it costs a
+ // bit of performance.
+
+ // Ingress can cause egress: TCP state machines may have advanced and want to send
+ // packets now.
+ //
+ // Also unblocks any sockets that were waiting for a recv.
+ self.pump_waiters();
+ }
+
+ Poll::Ready(changed)
+ }
+
+ /// Send all packets the netstack wants to transmit on the network.
+ ///
+ /// Returns:
+ ///
+ /// - `Poll::Pending` if _no_ packets could be sent because `dev` wasn't ready (if
+ /// [`AsyncWakeDevice::poll_tx`] returns `Poll::Pending`)
+ /// - `Poll::Ready(false)` if `dev` was ready but no packets needed to be sent
+ /// - `Poll::Ready(true)` if `dev` was ready and packets were sent
+ #[tracing::instrument(skip_all, fields(%now), ret, level = "trace")]
+ pub fn poll_device_egress_async(
+ &mut self,
+ cx: &mut core::task::Context<'_>,
+ now: smoltcp::time::Instant,
+ mut dev: Pin<&mut (impl AsyncWakeDevice + smoltcp::phy::Device + Unpin)>,
+ ) -> Poll<bool> {
+ core::task::ready!(dev.as_mut().poll_tx(cx));
+
+ match self
+ .iface
+ .poll_egress(now, dev.as_mut().get_mut(), &mut self.socket_set)
+ {
+ PollResult::SocketStateChanged => {
+ tracing::trace!("socket state changed");
+
+ // Egress may have opened capacity in packet buffers for
+ // e.g. pending TCP or UDP sends, which we may be able to complete + send now.
+ self.pump_waiters();
+
+ Poll::Ready(true)
+ }
+ PollResult::None => Poll::Ready(false),
+ }
+ }
+
+ /// Attempt to make progress on internal state that is blocking on I/O.
+ ///
+ /// Calls [`Netstack::pump_blocked_commands`] and [`Netstack::pump_tcp_accept`].
+ #[tracing::instrument(skip_all, level = "trace")]
+ fn pump_waiters(&mut self) {
+ // Pump accept first, then commands: blocked commands will have tried to run once, so they
+ // will have created any listeners already (i.e. they can't affect the TCP accept loop).
+ // Accepts however can unblock waiting commands.
+ self.pump_tcp_accept();
+ self.pump_blocked_commands();
+ }
+
+ /// Reprocess all blocked socket commands.
+ ///
+ /// This is `O(n)` in the number of socket commands in the queue: it attempts to run
+ /// all of them. Any that return [`Command::WouldBlock`] are requeued as normal.
+ #[tracing::instrument(skip_all, level = "trace")]
+ fn pump_blocked_commands(&mut self) {
+ // NB: we pop_front here and push_back in process_one_cmd: since we're taking len()
+ // elements, we see everything that is currently in the deque exactly once, no matter
+ // how many of them end up still blocked and pushed onto the back.
+ for _ in 0..self.blocked_commands.len() {
+ let cmd = self.blocked_commands.pop_front().unwrap();
+ self.process_one_cmd(cmd);
+ }
+ }
+
+ /// Attempt to send and receive packets on `dev`.
+ ///
+ /// The future becomes ready when `dev` sends or receives packets on the network. All
+ /// synchronously-available network I/O is always performed.
+ ///
+ /// Assumes that alarms are handled separately and that `now` does not advance
+ /// over the course of the polled future.
+ pub fn wait_io_async<'stack, 'dev, D>(
+ &'stack mut self,
+ now: smoltcp::time::Instant,
+ dev: &'dev mut D,
+ ) -> IoPoller<'stack, 'dev, D>
+ where
+ D: AsyncWakeDevice + smoltcp::phy::Device + Unpin,
+ {
+ IoPoller {
+ stack: self,
+ now,
+ egress_done: false,
+ dev: Pin::new(dev),
+ }
+ }
+}
+
+/// A future that becomes ready when the contained `smoltcp::phy::Device` sends or receives
+/// packets on the network.
+///
+/// The future always completes _all_ synchronously-available network I/O, i.e. it polls
+/// until `dev.poll_rx` and `dev.poll_tx` return `Poll::Pending`, and/or until the netstack
+/// reports that there are no more packets to be sent.
+///
+/// # Cancel safety
+///
+/// This future is completely cancel-safe.
+pub struct IoPoller<'stack, 'dev, D> {
+ stack: &'stack mut Netstack,
+ /// Whether egress has returned `Poll::Ready(false)`, indicating that it is not waiting
+ /// for transmit capacity in `dev` but just has no more work to do with
+ /// currently-available data.
+ ///
+ /// This state should not change with repeated calls to `poll` because socket commands
+ /// are the only reason `smoltcp` calls `dev.transmit()`, and we don't expect any new
+ /// commands to be processed while this future is being `poll`ed.
+ ///
+ /// We don't store a similar flag for ingress because ingress readiness is controlled
+ /// by the network: if new packets arrive between calls to `poll`, `poll_ingress` may
+ /// return `Poll::Ready(true)` where it had previously returned `Poll::Ready(false)`.
+ ///
+ /// By contrast, while send capacity may become available in the future
+ /// (`Poll::Pending` -> `Poll::Ready(_)`), having _things to send_ (`Poll::Ready(true)`)
+ /// will not arise from a `Poll::Ready(false)` state without commands being processed.
+ egress_done: bool,
+
+ /// The current instant to use while polling this future.
+ ///
+ /// Alarms (e.g. for TCP retransmit) must be set externally.
+ now: smoltcp::time::Instant,
+
+ dev: Pin<&'dev mut D>,
+}
+
+impl<D> Future for IoPoller<'_, '_, D>
+where
+ D: AsyncWakeDevice + smoltcp::phy::Device + Unpin,
+{
+ type Output = ();
+
+ #[tracing::instrument(skip_all, fields(%self.now), ret, level = "trace")]
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ // Logic here: future resolves when _either_ egress or ingress has made progress.
+ // If neither makes forward progress (either because pending or nothing to do), return
+ // pending.
+
+ let Self {
+ now,
+ stack,
+ dev,
+ egress_done,
+ ..
+ } = self.get_mut();
+
+ stack.pump_waiters();
+
+ let now = *now;
+
+ let mut ingress_pending = false;
+ let mut egress_pending = false;
+ let mut progress_made = false;
+
+ while !(ingress_pending && (*egress_done || egress_pending)) {
+ tracing::trace_span!("poll_loop", ingress_pending, egress_done, egress_pending);
+
+ if !ingress_pending {
+ match stack.poll_device_ingress_async(cx, now, dev.as_mut()) {
+ // `dev` doesn't have any packets to receive
+ Poll::Pending => ingress_pending = true,
+ // `dev` received packets but no sockets made progress. This means that
+ // `dev` is completely drained, so polling ingress again should return
+ // `Poll::Pending`.
+ Poll::Ready(false) => {
+ ingress_pending = true;
+ egress_pending = false;
+ }
+ // We received packets and socket state was updated as a result. Reset
+ // egress_pending as well to cover the possibility that successful receives
+ // made transmit capacity available (loopback case).
+ Poll::Ready(true) => {
+ ingress_pending = false;
+ egress_pending = false;
+ progress_made = true;
+ }
+ }
+ }
+
+ if !(*egress_done || egress_pending) {
+ match stack.poll_device_egress_async(cx, now, dev.as_mut()) {
+ // `dev` isn't ready to accept transmits, don't bother trying egress again this
+ // poll.
+ Poll::Pending => egress_pending = true,
+ // Not blocked, we just have no packets to send. Don't bother polling egress
+ // again, we should not be able to make progress.
+ Poll::Ready(false) => {
+ *egress_done = true;
+ egress_pending = false;
+ }
+ // We successfully sent packet(s). Reset ingress_pending as well to cover
+ // possibility of synchronous loopback.
+ Poll::Ready(true) => {
+ egress_pending = false;
+ ingress_pending = false;
+ progress_made = true;
+ }
+ }
+ }
+ }
+
+ if progress_made {
+ stack.drain_tcp_closes();
+ Poll::Ready(())
+ } else {
+ Poll::Pending
+ }
+ }
+}
+
+fn set_loopback<const N: usize>(ips: &mut heapless::Vec<smoltcp::wire::IpCidr, N>) -> bool {
+ if ips.capacity() < 2 {
+ return false;
+ }
+
+ if ips
+ .push(smoltcp::wire::IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
+ .is_err()
+ {
+ return false;
+ }
+
+ if ips
+ .push(smoltcp::wire::IpCidr::new(
+ IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1),
+ 128,
+ ))
+ .is_err()
+ {
+ return false;
+ }
+
+ true
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/pipe.rs b/deps/ts_netstack_smoltcp_core/src/pipe.rs
new file mode 100644
index 00000000..746f03a9
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/pipe.rs
@@ -0,0 +1,131 @@
+use bytes::{Bytes, BytesMut};
+use smoltcp::{
+ phy::{ChecksumCapabilities, DeviceCapabilities, Medium},
+ time::Instant,
+};
+
+/// Dumb bidirectional pipe carrying [`Bytes`] (comes in pairs).
+///
+/// Used in [`PipeDev`] to provide a simple [`smoltcp::phy::Device`] implementation.
+///
+/// # Example
+///
+/// ```rust
+/// # use ts_netstack_smoltcp_core::Pipe;
+/// # use bytes::Bytes;
+///
+/// let (p1, p2) = Pipe::unbounded();
+///
+/// // Send on one channel
+/// p1.tx.try_send(Bytes::copy_from_slice(b"hello"));
+///
+/// // Receive on the other
+/// assert_eq!(p2.rx.recv().unwrap().as_ref(), b"hello");
+/// ```
+pub struct Pipe {
+ /// Sender to be received by the remote end of the pipe.
+ pub tx: flume::Sender<Bytes>,
+ /// Receiver for messages from the remote end of the pipe.
+ pub rx: flume::Receiver<Bytes>,
+}
+
+impl Pipe {
+ /// Construct a pipe with unbounded capacity.
+ pub fn unbounded() -> (Pipe, Pipe) {
+ let (tx1, rx1) = flume::unbounded();
+ let (tx2, rx2) = flume::unbounded();
+
+ (Pipe { tx: tx1, rx: rx2 }, Pipe { tx: tx2, rx: rx1 })
+ }
+
+ /// Construct a new pipe that can contain at most `limit` packets.
+ pub fn bounded(limit: usize) -> (Pipe, Pipe) {
+ let (tx1, rx1) = flume::bounded(limit);
+ let (tx2, rx2) = flume::bounded(limit);
+
+ (Pipe { tx: tx1, rx: rx2 }, Pipe { tx: tx2, rx: rx1 })
+ }
+}
+
+pub struct TxToken(flume::Sender<Bytes>);
+
+impl smoltcp::phy::TxToken for TxToken {
+ fn consume<R, F>(self, len: usize, f: F) -> R
+ where
+ F: FnOnce(&mut [u8]) -> R,
+ {
+ let mut b = BytesMut::zeroed(len);
+
+ let ret = f(&mut b);
+ if self.0.send(b.freeze()).is_err() {
+ tracing::warn!("remote end of dropped on send");
+ }
+
+ ret
+ }
+}
+
+pub struct RxToken(Bytes);
+
+impl smoltcp::phy::RxToken for RxToken {
+ fn consume<R, F>(self, f: F) -> R
+ where
+ F: FnOnce(&[u8]) -> R,
+ {
+ f(&self.0)
+ }
+}
+
+/// Wrapper around [`Pipe`] to implement [`smoltcp::phy::Device`].
+pub struct PipeDev {
+ /// End of a pipe that will be directly connected to the netstack, receiving packets
+ /// to be sent and supplying packets to be received.
+ pub pipe: Pipe,
+
+ /// The type of network frame the pipe will carry.
+ ///
+ /// For our purposes, this will typically be [`Medium::Ip`].
+ pub medium: Medium,
+
+ /// The maximum packet size to be transmitted through the pipe.
+ ///
+ /// The implementation does not check or limit the actual size of packets flowing
+ /// through it, this field is just informational for
+ /// [`smoltcp::phy::Device::capabilities`].
+ pub mtu: usize,
+}
+
+impl smoltcp::phy::Device for PipeDev {
+ type RxToken<'a>
+ = RxToken
+ where
+ Self: 'a;
+
+ type TxToken<'a>
+ = TxToken
+ where
+ Self: 'a;
+
+ fn receive(&mut self, timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
+ let tx = self.transmit(timestamp)?;
+ let b = self.pipe.rx.try_recv().ok()?;
+
+ Some((RxToken(b), tx))
+ }
+
+ fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
+ let sender = (!self.pipe.tx.is_disconnected()).then(|| self.pipe.tx.clone())?;
+
+ Some(TxToken(sender))
+ }
+
+ fn capabilities(&self) -> DeviceCapabilities {
+ let mut caps = DeviceCapabilities::default();
+
+ caps.max_transmission_unit = self.mtu;
+ caps.medium = self.medium;
+ caps.checksum = ChecksumCapabilities::ignored();
+
+ caps
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/mod.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/mod.rs
new file mode 100644
index 00000000..754d1ea1
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/mod.rs
@@ -0,0 +1,22 @@
+//! Socket command handler implementations for [`Netstack`][crate::Netstack].
+
+/// Unwrap an optional handle or log an error and return
+/// [`Error::BadRequest`][crate::command::Error::BadRequest].
+macro_rules! unwrap_handle {
+ ($handle:expr) => {{
+ let handle = $handle;
+
+ match handle {
+ Some(handle) => handle,
+ None => {
+ tracing::error!(?handle, "no socket handle");
+
+ return $crate::command::Error::missing_socket().into();
+ }
+ }
+ }};
+}
+
+pub mod raw;
+pub mod tcp;
+pub mod udp;
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/raw.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/raw.rs
new file mode 100644
index 00000000..21faa746
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/raw.rs
@@ -0,0 +1,107 @@
+use alloc::vec;
+
+use bytes::Bytes;
+use smoltcp::{iface::SocketHandle, socket::raw};
+
+use crate::{
+ Netstack, Response,
+ command::Error,
+ raw::{Command as RawSocketCommand, Response as RawSocketResponse},
+};
+
+impl Netstack {
+ /// Process a raw socket command.
+ #[tracing::instrument(skip_all, fields(?raw, ?handle), level = "debug")]
+ pub(crate) fn process_raw(
+ &mut self,
+ raw: RawSocketCommand,
+ handle: Option<SocketHandle>,
+ ) -> Response {
+ match raw {
+ RawSocketCommand::Open {
+ ip_version,
+ protocol,
+ } => {
+ let sock = raw::Socket::new(
+ Some(ip_version),
+ Some(protocol),
+ self.raw_buffer(),
+ self.raw_buffer(),
+ );
+ let handle = self.socket_set.add(sock);
+
+ RawSocketResponse::Opened { handle }.into()
+ }
+ RawSocketCommand::Send { buf } => {
+ let sock = self
+ .socket_set
+ .get_mut::<raw::Socket>(unwrap_handle!(handle));
+
+ if buf.len() > sock.payload_send_capacity() {
+ tracing::error!(
+ len = buf.len(),
+ capacity = sock.payload_send_capacity(),
+ "send can never succeed, packet size is greater than socket buffer cap"
+ );
+
+ return Response::Error(Error::big_packet());
+ }
+
+ match sock.send_slice(&buf) {
+ Ok(()) => Response::Ok,
+ Err(raw::SendError::BufferFull) => Response::WouldBlock {
+ command: RawSocketCommand::Send { buf }.into(),
+ handle,
+ },
+ }
+ }
+ RawSocketCommand::Recv { max_len } => {
+ let sock = self
+ .socket_set
+ .get_mut::<raw::Socket>(unwrap_handle!(handle));
+
+ match sock.recv() {
+ Ok(mut buf) => {
+ let mut trunc = None;
+
+ if let Some(max_len) = max_len {
+ let max_len = max_len.get();
+
+ if max_len < buf.len() {
+ tracing::warn!(max_len, pkt_len = buf.len(), "truncating packet");
+
+ trunc = Some(buf.len());
+ buf = &buf[..max_len];
+ }
+ }
+
+ RawSocketResponse::Recv {
+ buf: Bytes::copy_from_slice(buf),
+ truncated: trunc,
+ }
+ .into()
+ }
+ Err(raw::RecvError::Exhausted) => Response::WouldBlock {
+ command: RawSocketCommand::Recv { max_len }.into(),
+ handle,
+ },
+ Err(raw::RecvError::Truncated) => {
+ // this can't occur for recv()
+ unreachable!()
+ }
+ }
+ }
+ RawSocketCommand::Close => {
+ self.socket_set.remove(unwrap_handle!(handle));
+ Response::Ok
+ }
+ }
+ }
+
+ fn raw_buffer(&self) -> raw::PacketBuffer<'static> {
+ raw::PacketBuffer::new(
+ vec![raw::PacketMetadata::EMPTY; self.config.raw_message_count],
+ vec![0; self.config.raw_buffer_size],
+ )
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/listener.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/listener.rs
new file mode 100644
index 00000000..1b00b261
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/listener.rs
@@ -0,0 +1,302 @@
+use alloc::{collections::VecDeque, vec};
+use core::net::SocketAddr;
+
+use smoltcp::{iface::SocketHandle, socket::tcp};
+
+use crate::{
+ Netstack,
+ command::{
+ Error, Response,
+ tcp::listen::{Command as TcpListenCommand, Response as TcpListenResponse},
+ },
+};
+
+/// Opaque handle to a TCP listener.
+#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct ListenerHandle(usize);
+
+/// State for a particular TCP listener, supporting the abstraction of a single persistent
+/// listener object that can spin off connections by calling `accept`.
+///
+/// `smoltcp` doesn't provide a TCP listener abstraction, just plain sockets. Each one has
+/// its own state machine, which can be in the `LISTENING` state, i.e. waiting for a
+/// connection. But once it's `ESTABLISHED`, you need to create a new `LISTENING` socket in
+/// order to accept a new connection.
+pub struct TcpListenerState {
+ /// The local endpoint on which this listener is listening.
+ local_endpoint: SocketAddr,
+
+ /// Socket currently in listening state and waiting for a new connection.
+ current_socket_handle: SocketHandle,
+
+ /// Sockets which have transitioned from `LISTEN` to `SYN-RECEIVED` (half-open) and are waiting
+ /// to become `ESTABLISHED`; in other words, the socket received a `SYN` and replied with a
+ /// `SYN-ACK`, and is awaiting an `ACK` to complete the handshake.
+ ///
+ /// Note that sockets in this queue can [transition back to the `LISTEN` state if the remote
+ /// replies with a `RST` rather than an `ACK`](https://www.rfc-editor.org/rfc/rfc793#page-70).
+ /// Sockets that return to `LISTEN` should be removed from this queue/dropped (not `close()`d);
+ /// the listener has already opened a new socket in the `LISTEN` state.
+ half_open_queue: VecDeque<SocketHandle>,
+
+ /// Sockets which have transitioned from `SYN-RECEIVED` (half-open) to `ESTABLISHED`
+ /// (full-open); in other words, the socket has received an `ACK` from the remote completing the
+ /// three-way handshake. Sockets in this queue are waiting for a call to
+ /// [`Netstack::process_tcp_listen()`] with a [`TcpListenCommand::Accept`] command, which will
+ /// dequeue a socket and return it to become a [`TcpStream`].
+ ///
+ /// [`TcpStream`]: [::ts_netstack_smoltcp_socket::tcp::stream::TcpStream]
+ accept_queue: VecDeque<SocketHandle>,
+}
+
+impl Netstack {
+ /// Process a TCP listener command.
+ #[tracing::instrument(skip_all, fields(?cmd), level = "debug")]
+ pub(crate) fn process_tcp_listen(
+ &mut self,
+ cmd: TcpListenCommand,
+ handle: Option<SocketHandle>,
+ ) -> Response {
+ debug_assert!(handle.is_none());
+
+ match cmd {
+ TcpListenCommand::Listen { local_endpoint } => {
+ let mut listener = tcp::Socket::new(self.tcp_buffer(), self.tcp_buffer());
+ super::notice_lost_peers(&mut listener);
+
+ if let Err(e) = listener.listen(local_endpoint) {
+ return Response::Error(e.into());
+ }
+
+ let socket_handle = self.socket_set.add(listener);
+
+ let listener_handle = ListenerHandle(self.next_tcp_listener_id);
+ self.next_tcp_listener_id += 1;
+
+ self.tcp_listeners.insert(
+ listener_handle,
+ TcpListenerState {
+ current_socket_handle: socket_handle,
+ local_endpoint,
+ half_open_queue: Default::default(),
+ accept_queue: Default::default(),
+ },
+ );
+
+ TcpListenResponse::Listening {
+ handle: listener_handle,
+ }
+ .into()
+ }
+ TcpListenCommand::Accept { handle } => {
+ let Some(listener) = self.tcp_listeners.get_mut(&handle) else {
+ tracing::error!(?handle, "listener does not exist");
+ return Error::missing_listener().into();
+ };
+
+ // Iterate the half-open queue, re-queueing any socket handles that are still in
+ // `SYN-RECEIVED`. Move any sockets in `ESTABLISHED` to the `accept_queue`, and
+ // close any sockets in `CLOSE-WAIT` or that moved back to `LISTEN`. All other
+ // states are unexpected.
+ listener.half_open_queue.retain(|half_open| {
+ let sock = self.socket_set.get_mut::<tcp::Socket>(*half_open);
+ let state = sock.state();
+ let _span = tracing::trace_span!(
+ "half_open_queue",
+ accept_queue_len = listener.accept_queue.len(),
+ pending_closes = self.pending_tcp_closes.len(),
+ ?half_open,
+ ?state
+ )
+ .entered();
+
+ match state {
+ tcp::State::SynReceived => {
+ tracing::trace!("half-open socket unchanged, re-queueing");
+ true
+ }
+ tcp::State::Established => {
+ tracing::trace!("half-open socket ready, moving to accept queue");
+ listener.accept_queue.push_back(*half_open);
+ false
+ }
+ tcp::State::CloseWait | tcp::State::Listen => {
+ // Closing a socket that moved back to LISTEN ensures there's only a
+ // single LISTEN socket at a time for a specific endpoint; dropping the
+ // socket handle doesn't remove the socket from the socket set, meaning
+ // smoltcp would have multiple LISTEN sockets open for the same
+ // endpoint, and likely (depending on socket set order) delivers packets
+ // to a socket the netstack isn't tracking in any of its queues. In that
+ // state, TCP handshakes still complete and clients are able to send
+ // their initial packet(s), because smoltcp is still tracking the
+ // socket. However, this function does not move the socket through the
+ // accept loop and surface data to callers, as it has no handle to the
+ // socket in its queues.
+ tracing::trace!("half-open socket moved to {state}, closing");
+ sock.close();
+ self.pending_tcp_closes.push(*half_open);
+ if self.pending_tcp_closes.len() > 10000 {
+ tracing::warn!("large number of pending closes");
+ }
+ false
+ }
+ _ => {
+ tracing::warn!("half-open socket in unexpected state, dropping");
+ false
+ }
+ }
+ });
+
+ // De-queue a single socket in the `ESTABLISHED` state from the `accept_queue` and
+ // return it to become a `TcpStream`.
+ while let Some(accept) = listener.accept_queue.pop_front() {
+ let sock = self.socket_set.get_mut::<tcp::Socket>(accept);
+ let state = sock.state();
+ let _span = tracing::trace_span!(
+ "accept_queue",
+ half_open_queue_len = listener.half_open_queue.len(),
+ accept_queue_len = listener.accept_queue.len(),
+ pending_closes = self.pending_tcp_closes.len(),
+ ?accept,
+ ?state
+ )
+ .entered();
+
+ match state {
+ tcp::State::Established => {
+ tracing::trace!("accept socket accepted, returning")
+ }
+ tcp::State::CloseWait => {
+ tracing::trace!(?state, "accept socket no longer established, closing");
+ sock.close();
+ self.pending_tcp_closes.push(accept);
+ continue;
+ }
+ _ => {
+ tracing::warn!(?state, "accept socket in unexpected state, dropping");
+ continue;
+ }
+ }
+
+ let remote = sock.remote_endpoint().unwrap();
+ return TcpListenResponse::Accepted {
+ handle: accept,
+ remote: SocketAddr::new(remote.addr.into(), remote.port),
+ }
+ .into();
+ }
+
+ tracing::trace!("accept queue empty");
+
+ Response::WouldBlock {
+ handle: None,
+ command: TcpListenCommand::Accept { handle }.into(),
+ }
+ }
+ TcpListenCommand::Close { handle } => {
+ let Some(listener) = self.tcp_listeners.remove(&handle) else {
+ tracing::error!(?handle, "listener does not exist");
+ return Error::missing_listener().into();
+ };
+
+ let sock = self
+ .socket_set
+ .get_mut::<tcp::Socket>(listener.current_socket_handle);
+
+ sock.close();
+
+ self.pending_tcp_closes.push(listener.current_socket_handle);
+
+ let accept_handles = listener
+ .half_open_queue
+ .iter()
+ .chain(listener.accept_queue.iter())
+ .copied();
+ for pending_accept in accept_handles {
+ let sock = self.socket_set.get_mut::<tcp::Socket>(pending_accept);
+ sock.close();
+
+ self.pending_tcp_closes.push(pending_accept);
+ }
+
+ Response::Ok
+ }
+ }
+ }
+
+ /// Attempt to accept a TCP connection for all TCP listeners.
+ #[tracing::instrument(skip_all)]
+ pub(crate) fn pump_tcp_accept(&mut self) {
+ for listener in self.tcp_listeners.values_mut() {
+ let sock = self
+ .socket_set
+ .get_mut::<tcp::Socket>(listener.current_socket_handle);
+
+ let state = sock.state();
+ let _span = tracing::trace_span!(
+ "pump_one_tcp_listener",
+ current_socket = ?listener.current_socket_handle,
+ current_socket_state = %state,
+ accept_queue_len = listener.accept_queue.len(),
+ listening_on = %listener.local_endpoint,
+ )
+ .entered();
+
+ match sock.state() {
+ tcp::State::Listen => {
+ tracing::trace!("listening");
+ continue;
+ }
+
+ tcp::State::SynReceived => {
+ tracing::trace!("socket pending, not yet established");
+ listener
+ .half_open_queue
+ .push_back(listener.current_socket_handle);
+ }
+
+ tcp::State::Established => {
+ tracing::trace!("connection established");
+ listener
+ .accept_queue
+ .push_back(listener.current_socket_handle);
+ }
+
+ state => {
+ tracing::warn!(
+ current_socket = ?listener.current_socket_handle,
+ current_socket_state = %state,
+ half_open_queue_len = listener.half_open_queue.len(),
+ accept_queue_len = listener.accept_queue.len(),
+ listening_on = %listener.local_endpoint,
+ "partially-established listening socket reset or closed");
+ sock.close();
+ self.pending_tcp_closes.push(listener.current_socket_handle);
+ }
+ }
+
+ // fallthrough: socket has either closed or been established -- create a new listen
+ // socket
+
+ let mut new_listener = tcp::Socket::new(
+ tcp::SocketBuffer::new(vec![0; self.config.tcp_buffer_size]),
+ tcp::SocketBuffer::new(vec![0; self.config.tcp_buffer_size]),
+ );
+ super::notice_lost_peers(&mut new_listener);
+
+ if let Err(e) = new_listener.listen(listener.local_endpoint) {
+ // invariant failure: the only variants for ListenError are
+ // InvalidState and Unaddressable. InvalidState isn't possible here because we just
+ // created the socket. Unaddressable only occurs if listener.local_endpoint has
+ // an unspecified (zero) port and/or address. but we're currently replacing a socket
+ // with the _same_ local_endpoint, and it clearly wasn't invalid before, so
+ // Unaddressable shouldn't be possible either. this should always succeed.
+ panic!("opening new listen socket for accept: {e}");
+ }
+
+ let socket_handle = self.socket_set.add(new_listener);
+ listener.current_socket_handle = socket_handle;
+ tracing::trace!(new_handle = ?socket_handle, "replaced active listen socket");
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/mod.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/mod.rs
new file mode 100644
index 00000000..d49341a3
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/mod.rs
@@ -0,0 +1,29 @@
+use alloc::vec;
+
+use smoltcp::socket::tcp;
+
+use crate::Netstack;
+
+mod listener;
+mod stream;
+
+pub use listener::{ListenerHandle, TcpListenerState};
+
+impl Netstack {
+ fn tcp_buffer(&self) -> tcp::SocketBuffer<'static> {
+ tcp::SocketBuffer::new(vec![0; self.config.tcp_buffer_size])
+ }
+}
+
+/// ThinkTerm: how long a connection may go without a packet from its peer -- an answer to what
+/// it sent, or to a keep-alive -- before it is aborted, and how long it may sit idle before a
+/// keep-alive asks. Without them a peer that goes away without a word, a laptop put to sleep,
+/// leaves the connection retrying forever, and a connect nobody waits for any more lingers too.
+const PEER_TIMEOUT: smoltcp::time::Duration = smoltcp::time::Duration::from_secs(600);
+const KEEP_ALIVE: smoltcp::time::Duration = smoltcp::time::Duration::from_secs(120);
+
+/// ThinkTerm: has `socket` notice a peer that went away, as the system's own TCP would.
+fn notice_lost_peers(socket: &mut tcp::Socket) {
+ socket.set_timeout(Some(PEER_TIMEOUT));
+ socket.set_keep_alive(Some(KEEP_ALIVE));
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/stream.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/stream.rs
new file mode 100644
index 00000000..0722568a
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/tcp/stream.rs
@@ -0,0 +1,156 @@
+use bytes::Bytes;
+use smoltcp::{iface::SocketHandle, socket::tcp};
+
+use crate::{
+ Netstack,
+ command::{
+ Error, Response,
+ tcp::stream::{Command as TcpStreamCommand, Response as TcpStreamResponse},
+ },
+};
+
+impl Netstack {
+ /// Process a TCP stream command.
+ #[tracing::instrument(skip_all, fields(?cmd, ?handle), level = "debug")]
+ pub(crate) fn process_tcp_stream(
+ &mut self,
+ cmd: TcpStreamCommand,
+ handle: Option<SocketHandle>,
+ ) -> Response {
+ match cmd {
+ TcpStreamCommand::Connect {
+ remote_endpoint,
+ local_endpoint,
+ } => {
+ if remote_endpoint.is_ipv4() != local_endpoint.is_ipv4() {
+ return Response::Error(Error::wrong_ip_version());
+ }
+
+ // Only occurs if we're polling a `WouldBlock`.
+ if let Some(handle) = handle {
+ return self.check_conn(
+ handle,
+ TcpStreamCommand::Connect {
+ local_endpoint,
+ remote_endpoint,
+ },
+ );
+ }
+
+ let mut sock = tcp::Socket::new(self.tcp_buffer(), self.tcp_buffer());
+ super::notice_lost_peers(&mut sock);
+
+ if let Err(e) = sock.connect(self.iface.context(), remote_endpoint, local_endpoint)
+ {
+ tracing::error!(error = %e, "tcp connect");
+ return Response::Error(e.into());
+ }
+
+ let handle = self.socket_set.add(sock);
+
+ Response::WouldBlock {
+ handle: Some(handle),
+ command: TcpStreamCommand::Connect {
+ local_endpoint,
+ remote_endpoint,
+ }
+ .into(),
+ }
+ }
+
+ TcpStreamCommand::Send { buf } => {
+ let handle = handle.unwrap();
+ let sock = self.socket_set.get_mut::<tcp::Socket>(handle);
+
+ match sock.send_slice(&buf) {
+ Ok(0) => Response::WouldBlock {
+ handle: Some(handle),
+ command: TcpStreamCommand::Send { buf }.into(),
+ },
+ Ok(n) => TcpStreamResponse::Sent { n }.into(),
+ Err(tcp::SendError::InvalidState) => {
+ tracing::error!(state = %sock.state(), "invalid socket state for send");
+ Response::Error(Error::invalid_socket_state())
+ }
+ }
+ }
+
+ TcpStreamCommand::Recv { max_len } => {
+ let handle = handle.unwrap();
+ let sock = self.socket_set.get_mut::<tcp::Socket>(handle);
+
+ match sock.recv(|buf| {
+ let mut len = buf.len();
+
+ if let Some(max_len) = max_len {
+ len = len.min(max_len);
+ }
+
+ (len, Bytes::copy_from_slice(&buf[..len]))
+ }) {
+ Ok(buf) if buf.is_empty() => Response::WouldBlock {
+ handle: Some(handle),
+ command: TcpStreamCommand::Recv { max_len }.into(),
+ },
+ Ok(buf) => TcpStreamResponse::Recv { buf }.into(),
+ Err(tcp::RecvError::Finished) => TcpStreamResponse::Finished.into(),
+ Err(tcp::RecvError::InvalidState) => {
+ tracing::error!(state = %sock.state(), "invalid socket state for recv");
+ Response::Error(Error::invalid_socket_state())
+ }
+ }
+ }
+
+ TcpStreamCommand::Close => {
+ let handle = handle.unwrap();
+
+ let sock = self.socket_set.get_mut::<tcp::Socket>(handle);
+ sock.close();
+
+ self.pending_tcp_closes.push(handle);
+
+ Response::Ok
+ }
+ }
+ }
+
+ /// Drop all TCP sockets that have finished closing.
+ #[tracing::instrument(skip_all)]
+ pub(crate) fn drain_tcp_closes(&mut self) {
+ self.pending_tcp_closes.retain(|&handle| {
+ let state = {
+ let sock = self.socket_set.get::<tcp::Socket>(handle);
+ sock.state()
+ };
+
+ let should_remove = state == tcp::State::Closed;
+ if should_remove {
+ self.socket_set.remove(handle);
+ }
+
+ !should_remove
+ });
+ }
+
+ fn check_conn(&mut self, handle: SocketHandle, orig_cmd: TcpStreamCommand) -> Response {
+ let sock = self.socket_set.get_mut::<tcp::Socket>(handle);
+
+ match sock.state() {
+ tcp::State::Established => {
+ tracing::trace!("connection succeeded");
+ TcpStreamResponse::Connected { handle }.into()
+ }
+
+ tcp::State::SynReceived | tcp::State::SynSent => Response::WouldBlock {
+ handle: Some(handle),
+ command: orig_cmd.into(),
+ },
+
+ _ => {
+ tracing::warn!("connecting socket was reset or closed");
+ self.pending_tcp_closes.push(handle);
+ Response::Error(Error::ConnectionReset)
+ }
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/socket_impl/udp.rs b/deps/ts_netstack_smoltcp_core/src/socket_impl/udp.rs
new file mode 100644
index 00000000..6632b46f
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/socket_impl/udp.rs
@@ -0,0 +1,135 @@
+use alloc::vec;
+use core::net::SocketAddr;
+
+use bytes::Bytes;
+use smoltcp::{iface::SocketHandle, socket::udp, wire::IpVersion};
+
+use crate::command::{
+ Error, Response,
+ udp::{Command as UdpCommand, Response as UdpResponse},
+};
+
+impl crate::Netstack {
+ /// Process a UDP socket command.
+ #[tracing::instrument(skip_all, fields(?handle, ?cmd), level = "debug")]
+ pub(crate) fn process_udp(
+ &mut self,
+ cmd: UdpCommand,
+ handle: Option<SocketHandle>,
+ ) -> Response {
+ match cmd {
+ UdpCommand::Bind { endpoint } => {
+ let mut sock = udp::Socket::new(self.udp_buffer(), self.udp_buffer());
+
+ if endpoint.port() == 0 {
+ tracing::error!(?endpoint, "udp bind: zero port");
+ return Response::Error(Error::unaddressable());
+ }
+
+ // The two possible failure cases for `bind` are that the port is zero or the socket
+ // was already open. Those are handled, so failure is impossible here.
+ sock.bind(endpoint).unwrap();
+
+ let handle = self.socket_set.add(sock);
+
+ UdpResponse::Bound {
+ local: endpoint,
+ handle,
+ }
+ .into()
+ }
+ UdpCommand::Send { endpoint, buf } => {
+ let handle = handle.unwrap();
+
+ let sock = self.socket_set.get_mut::<udp::Socket>(handle);
+ let sock_is_v4 = sock
+ .endpoint()
+ .addr
+ .is_some_and(|ep| ep.version() == IpVersion::Ipv4);
+
+ if endpoint.is_ipv4() != sock_is_v4 {
+ return Response::Error(Error::wrong_ip_version());
+ }
+
+ if buf.len() > sock.payload_send_capacity() {
+ tracing::error!(
+ len = buf.len(),
+ socket_capacity = sock.payload_send_capacity(),
+ "requested message size overflows socket capacity",
+ );
+
+ return Response::Error(Error::big_packet());
+ }
+
+ match sock.send_slice(&buf, endpoint) {
+ Ok(_n) => Response::Ok,
+ // This means that the _current_ buffer is too full, but since we checked if we
+ // had send capacity, it should be available in the future, so just punt and
+ // wouldblock until then.
+ Err(udp::SendError::BufferFull) => Response::WouldBlock {
+ command: UdpCommand::Send { buf, endpoint }.into(),
+ handle: Some(handle),
+ },
+ Err(udp::SendError::Unaddressable) => {
+ tracing::error!(?endpoint, "invalid endpoint");
+ Response::Error(Error::unaddressable())
+ }
+ }
+ }
+ UdpCommand::Recv { max_len } => {
+ let sock = self
+ .socket_set
+ .get_mut::<udp::Socket>(unwrap_handle!(handle));
+
+ match sock.recv() {
+ Ok((b, meta)) => {
+ let mut len = b.len();
+ let mut truncated = None;
+
+ if let Some(max_len) = max_len {
+ let max_len = max_len.get();
+
+ if len > max_len {
+ truncated = Some(len);
+ tracing::warn!(len, max_len, "udp read truncated");
+ }
+
+ len = max_len.min(len);
+ }
+
+ UdpResponse::RecvFrom {
+ remote: SocketAddr::new(meta.endpoint.addr.into(), meta.endpoint.port),
+ buf: Bytes::copy_from_slice(&b[..len]),
+ truncated,
+ }
+ .into()
+ }
+ Err(udp::RecvError::Exhausted) => Response::WouldBlock {
+ command: UdpCommand::Recv { max_len }.into(),
+ handle,
+ },
+ Err(udp::RecvError::Truncated) => {
+ // this can't occur for recv() as we have a view into the backing
+ // socketbuffer storage. truncated only occurs for recv_slice().
+ unreachable!()
+ }
+ }
+ }
+ UdpCommand::Close => {
+ // NOTE(npry): smoltcp supports socket reuse via `socket.close()`, which puts the
+ // socket in a valid state to re-bound. We don't support that for API simplicity,
+ // but we could in principle if there was a motivating reason.
+ self.socket_set.remove(unwrap_handle!(handle));
+
+ Response::Ok
+ }
+ }
+ }
+
+ fn udp_buffer(&self) -> udp::PacketBuffer<'static> {
+ udp::PacketBuffer::new(
+ vec![udp::PacketMetadata::EMPTY; self.config.udp_message_count],
+ vec![0; self.config.udp_buffer_size],
+ )
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/stack_control_impl.rs b/deps/ts_netstack_smoltcp_core/src/stack_control_impl.rs
new file mode 100644
index 00000000..9f1df0d8
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/stack_control_impl.rs
@@ -0,0 +1,63 @@
+use core::net::IpAddr;
+
+use crate::{Error, HasChannel, Netstack, Response, command::stack_control};
+
+/// Extension methods for control channels ([`HasChannel`]) supporting ergonomic operations
+/// around [`stack_control::Command`]s.
+pub trait NetstackControl: HasChannel {
+ /// Set the IPs for the netstack interface.
+ ///
+ /// The netstack must have space to store all the provided IPs -- this is controlled
+ /// by `smoltcp`'s `iface-max-addr-count-*` features. You must configure this as the end
+ /// user because it is global to all usages of this version of `smoltcp`.
+ fn set_ips_blocking(&self, ips: impl IntoIterator<Item = IpAddr>) -> Result<(), Error> {
+ self.request_blocking(
+ None,
+ stack_control::Command::SetIps {
+ new_ips: ips.into_iter().collect(),
+ },
+ )?
+ .to_ok()
+ }
+
+ /// Set the IPs for the netstack interface.
+ ///
+ /// The netstack must have space to store all the provided IPs -- this is controlled
+ /// by `smoltcp`'s `iface-max-addr-count-*` features. You must configure this as the end
+ /// user because it is global to all usages of this version of `smoltcp`.
+ fn set_ips(
+ &self,
+ ips: impl IntoIterator<Item = IpAddr>,
+ ) -> impl Future<Output = Result<(), Error>> + Send {
+ let channel = self.command_channel();
+ let new_ips = ips.into_iter().collect();
+
+ async move {
+ crate::request(channel, None, stack_control::Command::SetIps { new_ips })
+ .await?
+ .to_ok()
+ }
+ }
+}
+
+impl<T> NetstackControl for T where T: HasChannel {}
+
+impl Netstack {
+ #[tracing::instrument(skip(self), level = "debug")]
+ pub(crate) fn process_stack_control(&mut self, command: stack_control::Command) -> Response {
+ match command {
+ stack_control::Command::SetIps { new_ips } => {
+ if !self.direct_set_ips(new_ips.iter().copied()) {
+ tracing::error!(
+ ?new_ips,
+ "not enough address storage space configured in smoltcp"
+ );
+
+ return Error::buffer_full().into();
+ }
+
+ Response::Ok
+ }
+ }
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/util/mod.rs b/deps/ts_netstack_smoltcp_core/src/util/mod.rs
new file mode 100644
index 00000000..a8ed83a8
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/util/mod.rs
@@ -0,0 +1,110 @@
+use core::fmt::{Debug, Display, Formatter};
+
+mod noop_cap_dev;
+pub use noop_cap_dev::NoopCapDev;
+
+/// Utility struct providing [`Debug`] using a type's [`Display`].
+pub struct DisplayToDebug<'a>(pub &'a dyn Display);
+
+impl<'a> Debug for DisplayToDebug<'a> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
+/// Extension methods for implementors of [`Display`].
+pub trait DisplayExt {
+ /// Return a wrapper around this value that provides [`Debug`] using its [`Display`]
+ /// implementation.
+ fn as_display_debug(&self) -> DisplayToDebug<'_>;
+}
+
+impl<T> DisplayExt for T
+where
+ T: Display,
+{
+ fn as_display_debug(&self) -> DisplayToDebug<'_> {
+ DisplayToDebug(self)
+ }
+}
+
+/// Extension methods for [`Result`].
+pub trait ResultExt {
+ /// The value type in the result.
+ type T;
+
+ /// Convert this result to an [`Option`] using [`Result::ok`], tracing the error if
+ /// there was one.
+ fn ok_trace(self) -> Option<Self::T>;
+
+ /// Convert this result to an [`Option`] using [`Result::ok`], tracing the error if
+ /// there was one, along with the provided context `ctx`.
+ fn ok_ctx(self, ctx: impl Display) -> Option<Self::T>;
+}
+
+impl<T, E> ResultExt for Result<T, E>
+where
+ E: Display,
+{
+ type T = T;
+
+ fn ok_trace(self) -> Option<T> {
+ self.inspect_err(|e| tracing::error!(error = %e)).ok()
+ }
+
+ fn ok_ctx(self, ctx: impl Display) -> Option<T> {
+ self.inspect_err(|e| tracing::error!(error = %e, "{ctx}"))
+ .ok()
+ }
+}
+
+/// Utility struct providing cleaner [`Debug`] formatting for [`Option`].
+///
+/// Delegates to an inner [`Display`] if this is `Some`, otherwise formats as `None`.
+pub struct OptionDisplayDebug<'a, T>(pub &'a Option<T>);
+
+impl<'a, T> Debug for OptionDisplayDebug<'a, T>
+where
+ T: Display,
+{
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ match self.0 {
+ Some(t) => t.fmt(f),
+ None => write!(f, "None"),
+ }
+ }
+}
+
+/// Extension methods for [`Option`].
+pub trait OptionExt {
+ /// The type contained in the `Option`.
+ type T;
+
+ /// Emit a trace with the given `ctx` if this option is `None`.
+ fn trace_none(self, ctx: impl Display) -> Self;
+
+ /// Wrap this option in a [`Debug`] implementation that calls [`Display::fmt`] on the
+ /// value if this is `Some`.
+ fn map_display(&self) -> impl Debug
+ where
+ Self::T: Display;
+}
+
+impl<T> OptionExt for Option<T> {
+ type T = T;
+
+ fn trace_none(self, ctx: impl Display) -> Self {
+ if self.is_none() {
+ tracing::error!("{ctx}");
+ }
+
+ self
+ }
+
+ fn map_display(&self) -> impl Debug
+ where
+ T: Display,
+ {
+ OptionDisplayDebug(self)
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/util/noop_cap_dev.rs b/deps/ts_netstack_smoltcp_core/src/util/noop_cap_dev.rs
new file mode 100644
index 00000000..baf438fb
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/util/noop_cap_dev.rs
@@ -0,0 +1,84 @@
+use smoltcp::{
+ phy::{DeviceCapabilities, Medium},
+ time::Instant,
+};
+
+/// [`smoltcp::iface::Interface::new`] does not store the dev on the interface, it just
+/// queries and stores its capabilities, which most relevantly include MTU, "medium" (frame
+/// type), and checksum behavior. This type is a hack to enable us to "rewrite" this
+/// constructor signature to just take the expected [`DeviceCapabilities`] directly, which
+/// lets us avoid supplying the net device at netstack construction, as it's not actually
+/// necessary at that point.
+pub struct NoopCapDev {
+ pub device_capabilities: DeviceCapabilities,
+}
+
+impl NoopCapDev {
+ /// Default caps for this crate's expected operation (layer 3).
+ pub fn default_caps() -> DeviceCapabilities {
+ let mut caps = DeviceCapabilities::default();
+ caps.medium = Medium::Ip;
+ caps.max_transmission_unit = 1500;
+
+ caps
+ }
+
+ /// Construct a [`NoopCapDev`] with the given modifications to its
+ /// [`DeviceCapabilities`].
+ pub fn with_caps(f: impl FnOnce(&mut DeviceCapabilities)) -> Self {
+ let mut caps = Self::default_caps();
+ f(&mut caps);
+
+ Self {
+ device_capabilities: caps,
+ }
+ }
+}
+
+impl Default for NoopCapDev {
+ fn default() -> Self {
+ Self::with_caps(|_| {})
+ }
+}
+
+impl smoltcp::phy::Device for NoopCapDev {
+ type RxToken<'a>
+ = NoopCapDev
+ where
+ Self: 'a;
+
+ type TxToken<'a>
+ = NoopCapDev
+ where
+ Self: 'a;
+
+ fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
+ None
+ }
+
+ fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
+ None
+ }
+
+ fn capabilities(&self) -> DeviceCapabilities {
+ self.device_capabilities.clone()
+ }
+}
+
+impl smoltcp::phy::RxToken for NoopCapDev {
+ fn consume<R, F>(self, _f: F) -> R
+ where
+ F: FnOnce(&[u8]) -> R,
+ {
+ unreachable!("using noop dummy device for rx")
+ }
+}
+
+impl smoltcp::phy::TxToken for NoopCapDev {
+ fn consume<R, F>(self, _len: usize, _f: F) -> R
+ where
+ F: FnOnce(&mut [u8]) -> R,
+ {
+ unreachable!("using noop dummy device for tx")
+ }
+}
diff --git a/deps/ts_netstack_smoltcp_core/src/wake_device.rs b/deps/ts_netstack_smoltcp_core/src/wake_device.rs
new file mode 100644
index 00000000..5f2758de
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/src/wake_device.rs
@@ -0,0 +1,27 @@
+use core::{
+ pin::Pin,
+ task::{Context, Poll},
+};
+
+/// Indicates when a [`smoltcp::phy::Device`] may have new data available.
+///
+/// The point of this is to bolt a `poll` mechanism onto [`smoltcp::phy::Device`], as it
+/// internally lacks any awareness of an external event loop.
+pub trait AsyncWakeDevice {
+ /// Poll the device to make progress on [`smoltcp::phy::Device::receive`].
+ ///
+ /// Must only return [`Poll::Ready`] when it's possible to call `receive` and get a
+ /// token. Callers may assume they can call `.unwrap()` without panicking in this case.
+ ///
+ /// This implies that _both_ `RxToken` and `TxToken` are available (i.e.
+ /// [`AsyncWakeDevice::poll_tx`] would return `Poll::Ready` as well), since `receive`
+ /// needs to return both tokens.
+ fn poll_rx(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
+
+ /// Poll the device to make progress on [`smoltcp::phy::Device::transmit`].
+ ///
+ /// Must only return [`Poll::Ready`] when it's possible to call `transmit` and get a
+ /// token. Callers may assume that they can call `.unwrap()` without panicking in this
+ /// case.
+ fn poll_tx(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
+}
diff --git a/deps/ts_netstack_smoltcp_core/tests/udp.rs b/deps/ts_netstack_smoltcp_core/tests/udp.rs
new file mode 100644
index 00000000..dcae1625
--- /dev/null
+++ b/deps/ts_netstack_smoltcp_core/tests/udp.rs
@@ -0,0 +1,188 @@
+//! Basic smoke tests for udp sockets.
+
+use core::net::SocketAddr;
+
+use bytes::Bytes;
+use smoltcp::{phy::Medium, time::Instant, wire};
+
+extern crate ts_netstack_smoltcp_core as netcore;
+
+use netcore::{HasChannel, Netstack, Pipe, PipeDev, Response, udp};
+
+// Rough schematic:
+//
+// UDP SOCKET
+// | ^^^
+// Cmd chan -> | |||.. <- Response channels (many, one per command)
+// v |||
+// ----------- smoltcp land: inside the box, things look like smoltcp expects.
+// | STACK | our Netstack is running in a thread in here just driving smoltcp forward.
+// | v |
+// | PIPE ("net" end, driven by STACK; wrapped by PipeDev)
+// | tx |^ rx|
+// -----------
+// || <- up/down channel |< smoltcp sees everything below it as "the network",
+// || |< i.e. the thing that actually transports the packets
+// rx v| tx |< it emits to their addressee, with whatever meaning
+// PIPE ("phy" end, |< that happens to have in your scenario (could be
+// driven directly |< switched ethernet, routing/forwarding through the
+// by this test) |< Internet, whatever -- it doesn't care). in this test
+// |< we're inspecting the packets and looping back the one
+// |< we know is addressed to the socket
+
+#[test]
+fn udp_by_steps() -> ts_cli_util::Result<()> {
+ ts_cli_util::init_tracing();
+ std::panic::set_hook(Box::new(tracing_panic::panic_hook));
+
+ let mut stack = Netstack::new(
+ netcore::Config {
+ loopback: true,
+ ..Default::default()
+ },
+ Instant::ZERO,
+ );
+
+ let channel = stack.command_channel();
+
+ let jh = std::thread::spawn(move || {
+ let _guard = tracing::info_span!("command thread").entered();
+
+ let ep = SocketAddr::from(([127, 0, 0, 1], 1000));
+
+ let Response::Udp(udp::Response::Bound { handle, local }) =
+ channel.request_blocking(None, udp::Command::Bind { endpoint: ep })?
+ else {
+ unreachable!();
+ };
+
+ tracing::debug!(%handle, %local, "socket bound");
+ assert_eq!(local, ep);
+
+ channel.request_blocking(
+ Some(handle),
+ udp::Command::Send {
+ endpoint: ep,
+ buf: Bytes::copy_from_slice(b"hello"),
+ },
+ )?;
+
+ let Response::Udp(udp::Response::RecvFrom {
+ buf,
+ remote,
+ truncated,
+ }) = channel.request_blocking(Some(handle), udp::Command::Recv { max_len: None })?
+ else {
+ unreachable!();
+ };
+
+ assert!(truncated.is_none());
+
+ tracing::debug!(who = %remote, buf = %core::str::from_utf8(&buf)?, "packet received");
+
+ // Default configuration has no routes, but that shouldn't impede this packet from going
+ // out, since we're single-interface L3 (functionally point-to-point): we can't actually
+ // make a forwarding decision, everything must just go out through the one pipe
+ channel.request_blocking(
+ Some(handle),
+ udp::Command::Send {
+ endpoint: SocketAddr::from(([1, 2, 3, 4], 53)),
+ buf: Bytes::copy_from_slice(b"hello"),
+ },
+ )?;
+
+ Ok(()) as ts_cli_util::Result<()>
+ });
+
+ let _guard = tracing::info_span!("netstack driver").entered();
+
+ wait_and_process_cmd(&mut stack)?; // bind
+ wait_and_process_cmd(&mut stack)?; // send
+
+ let (net, phy) = Pipe::unbounded();
+ let mut net = PipeDev {
+ pipe: net,
+ medium: Medium::Ip,
+ mtu: 1536,
+ };
+
+ stack.poll_device_io(Instant::ZERO, &mut net); // send goes out
+
+ let pkt = phy.rx.try_recv()?; // the sent packet
+ let repr = parse_ip_repr(&pkt).unwrap();
+ tracing::debug!(?repr, ?pkt, "outgoing packet");
+
+ phy.tx.try_send(pkt)?; // loop back the send
+ tracing::debug!("looped back packet");
+ stack.poll_device_io(Instant::ZERO, &mut net);
+
+ wait_and_process_cmd(&mut stack)?; // recv
+ stack.poll_device_io(Instant::ZERO, &mut net);
+
+ wait_and_process_cmd(&mut stack)?; // send to 1.2.3.4:53
+ stack.poll_device_io(Instant::ZERO, &mut net);
+
+ let pkt = phy.rx.try_recv()?; // the sent packet
+ let repr = parse_ip_repr(&pkt).unwrap();
+ tracing::debug!(?repr, ?pkt, "outgoing packet");
+
+ jh.join().unwrap()?;
+
+ Ok(())
+}
+
+fn wait_and_process_cmd(stack: &mut Netstack) -> ts_cli_util::Result<()> {
+ let cmd = stack.wait_for_cmd_blocking(None)?;
+ stack.process_one_cmd(cmd);
+
+ Ok(())
+}
+
+fn parse_ip_repr(overlay_ip_pkt: &[u8]) -> Option<wire::IpRepr> {
+ if overlay_ip_pkt.is_empty() {
+ tracing::warn!("empty ip packet");
+ return None;
+ }
+
+ let version = *overlay_ip_pkt.first().unwrap();
+ let version = version >> 4; // high 4 bits are the version field
+
+ let repr: wire::IpRepr = match version {
+ 4 => {
+ let pkt = wire::Ipv4Packet::new_checked(overlay_ip_pkt);
+ let pkt = match pkt {
+ Ok(pkt) => pkt,
+ Err(e) => {
+ tracing::error!(err = %e, "invalid ipv4 packet");
+ return None;
+ }
+ };
+
+ let repr = wire::Ipv4Repr::parse(&pkt, &Default::default()).ok()?;
+ repr.into()
+ }
+ 6 => {
+ let pkt = wire::Ipv6Packet::new_checked(overlay_ip_pkt);
+ let pkt = match pkt {
+ Ok(pkt) => pkt,
+ Err(e) => {
+ tracing::error!(err = %e, "invalid ipv6 packet");
+ return None;
+ }
+ };
+
+ let repr = wire::Ipv6Repr::parse(&pkt).ok()?;
+ repr.into()
+ }
+ _ => {
+ tracing::error!(
+ version,
+ packet_len = overlay_ip_pkt.len(),
+ "unknown ip version"
+ );
+ return None;
+ }
+ };
+
+ Some(repr)
+}
diff --git a/deps/ts_runtime/Cargo.toml b/deps/ts_runtime/Cargo.toml
new file mode 100644
index 00000000..f7d12917
--- /dev/null
+++ b/deps/ts_runtime/Cargo.toml
@@ -0,0 +1,193 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2024"
+rust-version = "1.92.0"
+name = "ts_runtime"
+version = "0.6.1"
+build = false
+autolib = false
+autobins = false
+autoexamples = false
+autotests = false
+autobenches = false
+description = "tailscale runtime"
+readme = "README.md"
+keywords = [
+ "tailscale",
+ "runtime",
+]
+categories = ["network-programming"]
+license = "BSD-3-Clause"
+repository = "https://github.com/tailscale/tailscale-rs"
+
+[package.metadata.cargo-machete]
+ignored = ["tokio-test"]
+
+[features]
+console = ["kameo/console"]
+otel = [
+ "kameo/otel",
+ "kameo/tracing",
+]
+
+[lib]
+name = "ts_runtime"
+path = "src/lib.rs"
+
+[dependencies.bytes]
+version = "1"
+
+[dependencies.futures]
+version = "0.3"
+default-features = false
+
+[dependencies.futures-util]
+version = "0.3"
+default-features = false
+
+[dependencies.ipnet]
+version = "2.12"
+default-features = false
+
+[dependencies.itertools]
+version = "0.14"
+
+[dependencies.kameo]
+version = "0.21"
+
+[dependencies.kameo_actors]
+version = "0.6"
+
+[dependencies.rand]
+version = "0.10"
+
+[dependencies.smallvec]
+version = "1.15"
+
+[dependencies.smol_str]
+version = "0.3"
+
+[dependencies.thiserror]
+version = "2"
+default-features = false
+
+[dependencies.tokio]
+version = "1"
+features = [
+ "rt-multi-thread",
+ "macros",
+ "time",
+]
+default-features = false
+
+[dependencies.tokio-stream]
+version = "0.1"
+features = [
+ "time",
+ "sync",
+]
+default-features = false
+
+[dependencies.tokio-util]
+version = "0.7"
+features = ["rt"]
+default-features = false
+
+[dependencies.tracing]
+version = "0.1"
+features = ["attributes"]
+default-features = false
+
+[dependencies.ts_bart]
+version = "0.6.1"
+
+[dependencies.ts_bart_packetfilter]
+version = "0.6.1"
+
+[dependencies.ts_control]
+version = "0.6.1"
+
+[dependencies.ts_dataplane]
+version = "0.6.1"
+
+[dependencies.ts_derp]
+version = "0.6.1"
+
+[dependencies.ts_disco_protocol]
+version = "0.6.1"
+
+[dependencies.ts_hexdump]
+version = "0.6.1"
+
+[dependencies.ts_keys]
+version = "0.6.1"
+
+[dependencies.ts_netcheck]
+version = "0.6.1"
+
+[dependencies.ts_netmon]
+version = "0.6.1"
+
+[dependencies.ts_netstack_smoltcp]
+version = "0.6.1"
+features = ["tokio"]
+
+[dependencies.ts_overlay_router]
+version = "0.6.1"
+
+[dependencies.ts_packet]
+version = "0.6.1"
+
+[dependencies.ts_packetfilter]
+version = "0.6.1"
+features = ["checking-filter"]
+
+[dependencies.ts_packetfilter_state]
+version = "0.6.1"
+
+[dependencies.ts_transport]
+version = "0.6.1"
+
+[dependencies.ts_tunnel]
+version = "0.6.1"
+
+[dependencies.url]
+version = "2"
+default-features = false
+
+[dependencies.yoke]
+version = "0.8"
+default-features = false
+
+[dependencies.zerocopy]
+version = "0.8"
+features = ["derive"]
+
+[lints.clippy]
+multiple_crate_versions = "allow"
+too_many_arguments = "allow"
+
+[lints.clippy.cargo]
+level = "warn"
+priority = -1
+
+[lints.rust]
+closure_returning_async_block = "warn"
+let-underscore = "warn"
+missing_docs = "warn"
+
+[lints.rustdoc]
+bare_urls = "deny"
+broken_intra_doc_links = "deny"
+invalid_html_tags = "deny"
+private_intra_doc_links = "deny"
diff --git a/deps/ts_runtime/LICENSE b/deps/ts_runtime/LICENSE
new file mode 100644
index 00000000..adcd4c5b
--- /dev/null
+++ b/deps/ts_runtime/LICENSE
@@ -0,0 +1,28 @@
+BSD 3-Clause License
+
+Copyright (c) 2026 Tailscale Inc & contributors.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/deps/ts_runtime/PATENTS b/deps/ts_runtime/PATENTS
new file mode 100644
index 00000000..560a2b8f
--- /dev/null
+++ b/deps/ts_runtime/PATENTS
@@ -0,0 +1,24 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Tailscale Inc. as part of the Tailscale project.
+
+Tailscale Inc. hereby grants to You a perpetual, worldwide,
+non-exclusive, no-charge, royalty-free, irrevocable (except as stated
+in this section) patent license to make, have made, use, offer to
+sell, sell, import, transfer and otherwise run, modify and propagate
+the contents of this implementation of Tailscale, where such license
+applies only to those patent claims, both currently owned or
+controlled by Tailscale Inc. and acquired in the future, licensable
+by Tailscale Inc. that are necessarily infringed by this
+implementation of Tailscale. This grant does not include claims that
+would be infringed only as a consequence of further modification of
+this implementation. If you or your agent or exclusive licensee
+institute or order or agree to the institution of patent litigation
+against any entity (including a cross-claim or counterclaim in a
+lawsuit) alleging that this implementation of Tailscale or any code
+incorporated within this implementation of Tailscale constitutes
+direct or contributory patent infringement, or inducement of patent
+infringement, then any patent rights granted to you under this License
+for this implementation of Tailscale shall terminate as of the date
+such litigation is filed.
diff --git a/deps/ts_runtime/src/control_runner.rs b/deps/ts_runtime/src/control_runner.rs
new file mode 100644
index 00000000..41676edd
--- /dev/null
+++ b/deps/ts_runtime/src/control_runner.rs
@@ -0,0 +1,571 @@
+use core::net::{Ipv4Addr, Ipv6Addr};
+use std::sync::Arc;
+
+use futures::StreamExt;
+use kameo::{
+ actor::{ActorRef, Spawn},
+ message::{Context, StreamMessage},
+ prelude::{Message, ReplySender},
+ reply::DelegatedReply,
+ supervision::RestartPolicy,
+};
+use ts_control::{
+ ControlDialer, DialPlan, Endpoint, Error as ControlError, Node, RegistrationError, StateUpdate,
+ client::{HttpConn, handle_ping, map_stream, send_map_request},
+};
+
+use crate::{
+ Task,
+ derp_latency::{DerpLatencyMeasurement, DerpLatencyMeasurer},
+ direct,
+ env::Env,
+};
+
+/// Actor responsible for maintaining the connection to control.
+///
+/// This actor is responsible for proxying the map response stream onto the message bus.
+pub struct ControlRunner {
+ params: Params,
+ state: RegState,
+
+ derp_latency_measurement: Option<DerpLatencyMeasurement>,
+
+ endpoints: Vec<Endpoint>,
+
+ self_node: Option<Node>,
+ pending_node_requests: Vec<PendingNodeRequest>,
+}
+
+enum RegState {
+ NotRegistered {
+ pending_auth_requests: Vec<ReplySender<Option<url::Url>>>,
+ },
+ AuthRequired(url::Url),
+ Registered(HttpConn),
+ /// Control turned the device away, for the reason given. The runner does not try again;
+ /// it stays to say why (see `current_registration_error`) rather than stopping, which would
+ /// leave every question to it unanswered and the reason only in the log.
+ Failed(String),
+}
+
+/// Control runner args.
+#[derive(Clone)]
+pub struct Params {
+ /// Control config.
+ pub(crate) config: ts_control::Config,
+
+ /// Auth key (if needed).
+ pub(crate) auth_key: Option<String>,
+
+ /// The [`Env`] for this actor.
+ pub(crate) env: Env,
+}
+
+#[doc(hidden)]
+#[derive(Debug, Clone, thiserror::Error)]
+pub enum ControlRunnerError {
+ #[error(transparent)]
+ Control(#[from] ControlError),
+
+ #[error(transparent)]
+ Crate(#[from] crate::Error),
+}
+
+#[derive(Clone)]
+struct AuthRequired(url::Url);
+
+struct RegisterResult(Result<HttpConn, RegistrationError>);
+
+impl kameo::Actor for ControlRunner {
+ type Args = Params;
+ type Error = ControlRunnerError;
+
+ async fn on_start(params: Params, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let client = params
+ .env
+ .ask::<DialerActor, _>(
+ None,
+ DialNext {
+ url: params.config.server_url.clone(),
+ },
+ true,
+ )
+ .await?;
+
+ Task::supervise_with(&slf, {
+ let aref = slf.downgrade();
+ let client = client.clone();
+ let params = params.clone();
+
+ move || {
+ let aref = aref.clone();
+ let client = client.clone();
+ let params = params.clone();
+
+ async move {
+ let mut followup = None;
+
+ loop {
+ let result = ts_control::register(
+ &params.config,
+ &params.config.server_url,
+ params.auth_key.as_deref(),
+ followup,
+ &params.env.keys,
+ &client,
+ )
+ .await;
+
+ if let Err(RegistrationError::MachineNotAuthorized(Some(u))) = result {
+ tracing::warn!(auth_url = %u, "machine not authorized");
+ followup = Some(u.clone());
+
+ let Some(aref) = aref.upgrade() else {
+ // if the control runner is dead, we should die shortly, no reason
+ // to keep running.
+ return;
+ };
+
+ if let Err(e) = aref.tell(AuthRequired(u)).await {
+ tracing::error!(error = %e, "failed to tell control runner required auth");
+ }
+
+ continue;
+ }
+
+ let Some(aref) = aref.upgrade() else {
+ return;
+ };
+ drop(aref.tell(RegisterResult(result.map(|_| client))).await);
+
+ break;
+ }
+ }
+ }
+ })
+ .restart_policy(RestartPolicy::Transient)
+ .spawn()
+ .await;
+
+ params.env.subscribe::<DerpLatencyMeasurement>(&slf).await?;
+ params.env.subscribe::<direct::NewEndpoints>(&slf).await?;
+
+ DerpLatencyMeasurer::supervise(&slf, params.env.clone())
+ .spawn()
+ .await;
+
+ params.env.register(None, &slf).await?;
+
+ Ok(Self {
+ state: RegState::NotRegistered {
+ pending_auth_requests: Default::default(),
+ },
+ params,
+ derp_latency_measurement: None,
+ endpoints: Default::default(),
+ self_node: None,
+ pending_node_requests: Default::default(),
+ })
+ }
+}
+
+#[kameo::messages]
+impl ControlRunner {
+ /// Fetch the IPv4 address for this tailscale device.
+ #[message(ctx)]
+ pub fn ipv4(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<Ipv4Addr>>>,
+ ) -> DelegatedReply<Option<Ipv4Addr>> {
+ if let Some(node) = &self.self_node {
+ return ctx.reply(Some(node.tailnet_address.ipv4.addr()));
+ }
+
+ let (deleg, replier) = ctx.reply_sender();
+ if let Some(replier) = replier {
+ self.pending_node_requests
+ .push(PendingNodeRequest::Ipv4(replier));
+ }
+
+ deleg
+ }
+
+ /// Fetch the IPv6 address for this tailscale device.
+ #[message(ctx)]
+ pub fn ipv6(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<Ipv6Addr>>>,
+ ) -> DelegatedReply<Option<Ipv6Addr>> {
+ if let Some(node) = &self.self_node {
+ return ctx.reply(Some(node.tailnet_address.ipv6.addr()));
+ }
+
+ let (deleg, replier) = ctx.reply_sender();
+ if let Some(replier) = replier {
+ self.pending_node_requests
+ .push(PendingNodeRequest::Ipv6(replier));
+ }
+
+ deleg
+ }
+
+ /// Fetch the self node for this tailscale device.
+ #[message(ctx)]
+ pub fn self_node(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
+ ) -> DelegatedReply<Option<Node>> {
+ if let Some(node) = &self.self_node {
+ return ctx.reply(Some(node.clone()));
+ }
+
+ let (deleg, replier) = ctx.reply_sender();
+ if let Some(replier) = replier {
+ self.pending_node_requests
+ .push(PendingNodeRequest::SelfNode(replier));
+ }
+
+ deleg
+ }
+
+ /// The node from the latest netmap, without waiting for one to arrive.
+ #[message]
+ pub fn current_self_node(&mut self) -> Option<Node> {
+ self.self_node.clone()
+ }
+
+ /// The current registration state: None while connecting, Some(None)
+ /// once registered, or Some(Some(url)) while approval is needed.
+ /// Unlike auth_url, this does not retain a pending request.
+ #[message]
+ pub fn current_auth_url(&mut self) -> Option<Option<url::Url>> {
+ match &self.state {
+ RegState::Registered(..) => Some(None),
+ RegState::AuthRequired(url) => Some(Some(url.clone())),
+ RegState::NotRegistered { .. } | RegState::Failed(..) => None,
+ }
+ }
+
+ /// Why control turned this device away, once it has.
+ #[message]
+ pub fn current_registration_error(&mut self) -> Option<String> {
+ match &self.state {
+ RegState::Failed(reason) => Some(reason.clone()),
+ _ => None,
+ }
+ }
+
+ /// Wait for a report of whether interactive auth is needed, and if so, what the URL is.
+ #[message(ctx)]
+ pub fn auth_url(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<url::Url>>>,
+ ) -> DelegatedReply<Option<url::Url>> {
+ match &mut self.state {
+ RegState::Registered(..) => ctx.reply(None),
+ RegState::AuthRequired(u) => ctx.reply(Some(u.clone())),
+ RegState::NotRegistered {
+ pending_auth_requests,
+ } => {
+ let (deleg, replier) = ctx.reply_sender();
+ if let Some(replier) = replier {
+ pending_auth_requests.push(replier);
+ }
+
+ deleg
+ }
+ // Turned away: no answer will come, and the request goes with its reply.
+ RegState::Failed(..) => ctx.reply_sender().0,
+ }
+ }
+}
+
+impl ControlRunner {
+ /// Call `f` with a map request built from the current control actor state.
+ ///
+ /// `stream` dictates whether the request is built for a streaming netmap response or as a
+ /// request to update to this node's fields in control.
+ ///
+ /// This takes a closure rather than returning the built request for lifetime reasons.
+ async fn with_map_request<T>(
+ &self,
+ stream: bool,
+ f: impl AsyncFnOnce(ts_control::MapRequest) -> T,
+ ) -> T {
+ let mut mrb = ts_control::MapRequestBuilder::new(&self.params.env.keys);
+
+ mrb = if stream {
+ mrb.as_stream()
+ } else {
+ mrb.as_request()
+ };
+
+ mrb = mrb.endpoints(self.endpoints.clone());
+
+ if let Some(hostname) = self.params.config.hostname.as_deref() {
+ mrb = mrb.hostname(hostname);
+ }
+
+ if let Some(latency) = &self.derp_latency_measurement {
+ if let Some(result) = latency.measurement.first() {
+ mrb = mrb.preferred_derp(result.id);
+ };
+
+ let iter = latency.measurement.iter().map(|result| {
+ (
+ result.latency_map_key.as_str(),
+ result.latency.as_secs_f64(),
+ )
+ });
+
+ mrb = mrb.derp_latencies(iter);
+ }
+
+ let client_name = self.params.config.format_client_name();
+
+ let mut request = mrb.build();
+ let host_info = request.host_info.get_or_insert_default();
+ host_info.app = &client_name;
+ host_info.ipn_version = ts_control::PKG_VERSION;
+
+ f(request).await
+ }
+
+ async fn update_map_request(&self) {
+ let RegState::Registered(conn) = &self.state else {
+ tracing::debug!("attempt to update map request while not registered");
+ return;
+ };
+
+ self.with_map_request(false, async |req| {
+ send_map_request(
+ req,
+ &self.params.config.server_url.join("machine/map").unwrap(),
+ conn,
+ )
+ .await
+ })
+ .await
+ .unwrap();
+ }
+}
+
+impl Message<AuthRequired> for ControlRunner {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ AuthRequired(auth_url): AuthRequired,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let RegState::NotRegistered {
+ pending_auth_requests,
+ } = core::mem::replace(&mut self.state, RegState::AuthRequired(auth_url.clone()))
+ else {
+ tracing::warn!("got duplicate authrequired message");
+ return;
+ };
+
+ for req in pending_auth_requests.into_iter() {
+ req.send(Some(auth_url.clone()));
+ }
+ }
+}
+
+impl Message<RegisterResult> for ControlRunner {
+ type Reply = ();
+
+ #[tracing::instrument(skip_all, fields(result = ?msg.0))]
+ async fn handle(&mut self, msg: RegisterResult, ctx: &mut Context<Self, Self::Reply>) {
+ if matches!(self.state, RegState::Registered(..)) {
+ tracing::warn!("got register result after already in registered state");
+ return;
+ }
+
+ let conn = match msg.0 {
+ Ok(conn) => conn,
+ Err(e) => {
+ tracing::error!(error = %e, "unable to register with control server");
+ // Pending auth requests go unanswered with it.
+ self.state = RegState::Failed(registration_failure(&e));
+ return;
+ }
+ };
+
+ let old_state = core::mem::replace(&mut self.state, RegState::Registered(conn.clone()));
+
+ if let RegState::NotRegistered {
+ pending_auth_requests,
+ } = old_state
+ {
+ for req in pending_auth_requests {
+ req.send(None);
+ }
+ }
+
+ let reader = self
+ .with_map_request(true, async |req| {
+ let map_url = self.params.config.server_url.join("machine/map").unwrap();
+
+ send_map_request(req, &map_url, &conn).await
+ })
+ .await
+ .unwrap();
+
+ let stream = map_stream(reader).map(Arc::new);
+
+ ctx.actor_ref().attach_stream(stream.boxed(), (), ());
+ }
+}
+
+/// What a failed registration says to whoever looks at the device.
+fn registration_failure(err: &RegistrationError) -> String {
+ match err {
+ RegistrationError::MachineNotAuthorized(None) => {
+ "the control server turned this machine away: the auth key may be wrong, used up or \
+ expired"
+ .to_string()
+ }
+ RegistrationError::NetworkError => {
+ "the control server could not be reached while signing in".to_string()
+ }
+ other => format!("signing in failed: {other}"),
+ }
+}
+
+enum PendingNodeRequest {
+ Ipv4(ReplySender<Option<Ipv4Addr>>),
+ Ipv6(ReplySender<Option<Ipv6Addr>>),
+ SelfNode(ReplySender<Option<Node>>),
+}
+
+impl Message<StreamMessage<Arc<StateUpdate>, (), ()>> for ControlRunner {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<Arc<StateUpdate>, (), ()>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let msg = match msg {
+ StreamMessage::Started(_) => {
+ tracing::trace!("started listening to state updates");
+ return;
+ }
+
+ StreamMessage::Next(msg) => msg,
+
+ StreamMessage::Finished(_) => {
+ tracing::error!("state update stream terminated");
+ ctx.stop();
+ return;
+ }
+ };
+
+ if let RegState::Registered(conn) = &self.state {
+ let _ = handle_ping(&msg, &self.params.config.server_url, conn).await;
+ }
+
+ if let Some(dial_plan) = &msg.dial_plan
+ && self
+ .params
+ .env
+ .ask::<DialerActor, _>(
+ None,
+ UpdateDialPlan {
+ dial_plan: dial_plan.clone(),
+ },
+ true,
+ )
+ .await
+ .unwrap()
+ {
+ tracing::trace!(new_dial_plan = ?dial_plan);
+ }
+
+ if let Some(node) = msg.node.as_ref() {
+ self.self_node = Some(node.clone());
+ }
+
+ if let Err(e) = self.params.env.publish(msg).await {
+ tracing::error!(error = %e, "publishing netmap update");
+ }
+
+ if let Some(node) = &self.self_node {
+ for req in self.pending_node_requests.drain(..) {
+ match req {
+ PendingNodeRequest::Ipv4(sender) => {
+ sender.send(Some(node.tailnet_address.ipv4.addr()));
+ }
+ PendingNodeRequest::Ipv6(sender) => {
+ sender.send(Some(node.tailnet_address.ipv6.addr()));
+ }
+ PendingNodeRequest::SelfNode(sender) => {
+ sender.send(Some(node.clone()));
+ }
+ }
+ }
+ }
+ }
+}
+
+impl Message<direct::NewEndpoints> for ControlRunner {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: direct::NewEndpoints, _ctx: &mut Context<Self, Self::Reply>) {
+ if self.endpoints == msg.0.as_ref() {
+ return;
+ }
+
+ self.endpoints = msg.0.to_vec();
+ self.update_map_request().await;
+ }
+}
+
+impl Message<DerpLatencyMeasurement> for ControlRunner {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: DerpLatencyMeasurement, _ctx: &mut Context<Self, Self::Reply>) {
+ if self.derp_latency_measurement.as_ref() == Some(&msg) {
+ return;
+ }
+
+ self.derp_latency_measurement = Some(msg);
+ self.update_map_request().await;
+ }
+}
+
+/// Control server dialer.
+pub struct DialerActor {
+ dialer: ControlDialer,
+ env: Env,
+}
+
+impl kameo::Actor for DialerActor {
+ type Args = Env;
+ type Error = crate::Error;
+
+ async fn on_start(env: Env, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ dialer: Default::default(),
+ env,
+ })
+ }
+}
+
+#[kameo::messages]
+impl DialerActor {
+ #[message]
+ async fn dial_next(&mut self, url: url::Url) -> Result<HttpConn, ts_control::Error> {
+ self.dialer
+ .full_connect_next(&url, &self.env.keys.machine_keys)
+ .await
+ }
+
+ #[message]
+ fn update_dial_plan(&mut self, dial_plan: DialPlan) -> bool {
+ self.dialer.update_dial_plan(&dial_plan)
+ }
+}
diff --git a/deps/ts_runtime/src/dataplane.rs b/deps/ts_runtime/src/dataplane.rs
new file mode 100644
index 00000000..4d46c383
--- /dev/null
+++ b/deps/ts_runtime/src/dataplane.rs
@@ -0,0 +1,338 @@
+use std::{
+ fmt::{Debug, Formatter},
+ sync::Arc,
+};
+
+use futures_util::StreamExt;
+use kameo::{
+ actor::{ActorRef, Spawn},
+ message::{Context, Message, StreamMessage},
+};
+use ts_dataplane::async_tokio::{
+ ActivePeers, DiscoBatch, FromOverlay, FromUnderlay, Rx, StunBatch, ToOverlay, ToUnderlay, Tx,
+};
+use ts_disco_protocol::{Packet, Plaintext};
+use ts_hexdump::{AsHexExt, Case};
+use ts_packet::PacketMut;
+use ts_transport::{DynEndpoint, OverlayTransportId, UnderlayTransportId};
+
+use crate::{
+ Error, Task,
+ env::Env,
+ packetfilter::PacketFilterState,
+ peer_tracker::PeerState,
+ route_updater::{PeerRouteUpdate, SelfRouteUpdate},
+ src_filter::SourceFilterState,
+};
+
+pub struct DataplaneActor {
+ dataplane: Arc<ts_dataplane::async_tokio::DataPlane>,
+ env: Env,
+}
+
+#[kameo::messages]
+impl DataplaneActor {
+ #[message]
+ pub async fn new_overlay_transport(
+ &self,
+ ) -> (OverlayTransportId, Tx<FromOverlay>, Rx<ToOverlay>) {
+ self.dataplane.new_overlay_transport().await
+ }
+
+ #[message]
+ pub async fn new_underlay_transport(
+ &self,
+ ) -> (UnderlayTransportId, Rx<ToUnderlay>, Tx<FromUnderlay>) {
+ self.dataplane.new_underlay_transport().await
+ }
+
+ /// Send a message directly to an underlay transport, bypassing the routing layer.
+ #[message]
+ pub async fn send_underlay(
+ &self,
+ underlay_id: UnderlayTransportId,
+ endpoint: DynEndpoint,
+ bufs: Vec<PacketMut>,
+ ) -> bool {
+ self.dataplane
+ .send_underlay(underlay_id, endpoint, bufs)
+ .await
+ }
+}
+
+pub type DiscoPacket = yoke::Yoke<&'static Packet<Plaintext>, ts_packet::Packet>;
+
+#[derive(Clone)]
+pub struct IncomingDiscoMsg {
+ pub transport: UnderlayTransportId,
+ pub sender: DynEndpoint,
+ pub packet: DiscoPacket,
+}
+
+impl Debug for IncomingDiscoMsg {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ self.packet.get().fmt(f)
+ }
+}
+
+impl AsRef<Packet<Plaintext>> for IncomingDiscoMsg {
+ fn as_ref(&self) -> &Packet<Plaintext> {
+ self.packet.get()
+ }
+}
+
+struct DiscoInternal(DiscoBatch);
+
+#[derive(Debug, Clone)]
+#[expect(dead_code)]
+pub struct IncomingStunMsg {
+ pub transport: UnderlayTransportId,
+ pub sender: DynEndpoint,
+ pub pkt: ts_packet::Packet,
+}
+
+struct StunInternal(StunBatch);
+
+impl kameo::Actor for DataplaneActor {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let (dataplane, disco, stun, active_peers) =
+ ts_dataplane::async_tokio::DataPlane::new(env.keys.node_keys.clone());
+
+ let dataplane = Arc::new(dataplane);
+
+ slf.attach_stream(
+ tokio_stream::wrappers::UnboundedReceiverStream::new(disco).map(DiscoInternal),
+ (),
+ (),
+ );
+
+ slf.attach_stream(
+ tokio_stream::wrappers::UnboundedReceiverStream::new(stun).map(StunInternal),
+ (),
+ (),
+ );
+
+ slf.attach_stream(
+ tokio_stream::wrappers::WatchStream::new(active_peers),
+ (),
+ (),
+ );
+
+ env.subscribe::<PeerRouteUpdate>(&slf).await?;
+ env.subscribe::<SelfRouteUpdate>(&slf).await?;
+ env.subscribe::<PacketFilterState>(&slf).await?;
+ env.subscribe::<SourceFilterState>(&slf).await?;
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+
+ let task_dataplane = dataplane.clone();
+
+ Task::spawn_link(&slf, async move {
+ task_dataplane.run().await;
+ })
+ .await;
+
+ tracing::trace!("dataplane running");
+ env.register(None, &slf).await?;
+
+ Ok(Self { dataplane, env })
+ }
+}
+
+impl Message<StreamMessage<DiscoInternal, (), ()>> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<DiscoInternal, (), ()>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let (transport_id, ep, bufs) = match msg {
+ StreamMessage::Next(pkt) => pkt.0,
+ _ => return,
+ };
+
+ for mut buf in bufs {
+ let pkt = tracing::debug_span!(
+ "disco_ingest",
+ ?transport_id,
+ ?ep,
+ sender_disco = tracing::field::Empty
+ )
+ .in_scope::<_, Option<IncomingDiscoMsg>>(|| {
+ let pkt = match Packet::from_encrypted_bytes_mut(buf.as_mut()) {
+ Ok(pkt) => pkt,
+ Err(e) => {
+ tracing::error!(error = %e, "parsing disco message:\n{}",
+ buf.iter().hexdump_string(Case::Lower)
+ );
+ return None;
+ }
+ };
+
+ tracing::Span::current()
+ .record("sender_disco", tracing::field::display(pkt.sender_pubkey()));
+
+ if let Err(e) = pkt.decrypt_in_place(&self.env.keys.disco_keys.private) {
+ tracing::error!(error = %e, "decrypting disco message");
+ return None;
+ };
+
+ let pkt = yoke::Yoke::<&'static Packet<Plaintext>, _>::try_attach_to_cart(
+ buf.freeze(),
+ // SAFETY: we just parsed this from the same buffer, so type/version are set correctly.
+ |buf| unsafe { Packet::from_bytes_unchecked(buf) },
+ )
+ .unwrap();
+
+ let pkt = IncomingDiscoMsg {
+ transport: transport_id,
+ sender: ep.clone(),
+ packet: pkt,
+ };
+
+ tracing::trace!(?pkt, "decrypted disco message");
+
+ Some(pkt)
+ });
+
+ if let Some(pkt) = pkt {
+ self.env.publish_noretain(pkt).await.unwrap();
+ }
+ }
+ }
+}
+
+impl Message<StreamMessage<StunInternal, (), ()>> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<StunInternal, (), ()>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let (transport_id, ep, pkts) = match msg {
+ StreamMessage::Next(pkt) => pkt.0,
+ _ => return,
+ };
+
+ for pkt in pkts {
+ self.env
+ .publish_noretain(IncomingStunMsg {
+ transport: transport_id,
+ pkt: pkt.freeze(),
+ sender: ep.clone(),
+ })
+ .await
+ .unwrap();
+ }
+ }
+}
+
+impl Message<StreamMessage<ActivePeers, (), ()>> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<ActivePeers, (), ()>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let peers = match msg {
+ StreamMessage::Next(pkt) => pkt,
+ StreamMessage::Finished(_) => {
+ // TODO(npry): apply to all stream msg handlers
+ tracing::warn!("active peers stream died, killing dataplane");
+ ctx.stop();
+ return;
+ }
+ StreamMessage::Started(_) => {
+ return;
+ }
+ };
+
+ tracing::debug!(active_peers = ?peers, "new active_peers");
+
+ self.env.publish(peers).await.unwrap();
+ }
+}
+
+impl Message<PeerRouteUpdate> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: PeerRouteUpdate, _ctx: &mut Context<Self, Self::Reply>) {
+ tracing::trace!("applying peer route update");
+
+ let dp = &mut *self.dataplane.inner().await;
+ dp.or_out.swap(msg.inner.overlay_out_routes.clone());
+
+ dp.ur_out.table = msg.inner.underlay_routes.clone();
+ }
+}
+
+impl Message<SelfRouteUpdate> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: SelfRouteUpdate, _ctx: &mut Context<Self, Self::Reply>) {
+ {
+ let dp = &mut *self.dataplane.inner().await;
+ dp.or_in.swap(msg.overlay_in_routes.as_ref().clone());
+ }
+
+ tracing::trace!("applied self route update");
+ }
+}
+
+impl Message<PacketFilterState> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: PacketFilterState, _ctx: &mut Context<Self, Self::Reply>) {
+ {
+ let dp = &mut *self.dataplane.inner().await;
+ dp.packet_filter = msg.0;
+ }
+
+ tracing::trace!("applied new packet filter");
+ }
+}
+
+impl Message<SourceFilterState> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: SourceFilterState, _ctx: &mut Context<Self, Self::Reply>) {
+ {
+ let dp = &mut *self.dataplane.inner().await;
+ dp.src_filter_in = msg.0;
+ }
+
+ tracing::trace!("applied new source filter");
+ }
+}
+
+impl Message<Arc<PeerState>> for DataplaneActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: Arc<PeerState>, _ctx: &mut Context<Self, Self::Reply>) {
+ {
+ let mut dp = self.dataplane.inner().await;
+ let wg = &mut dp.wireguard;
+
+ for &upsert in &msg.upserts {
+ let (_, node) = msg.peers.get(&upsert).unwrap();
+
+ wg.upsert_peer(ts_tunnel::PeerConfig::new(
+ ts_tunnel::PeerId(upsert.0),
+ node.node_key,
+ [0u8; 32],
+ ));
+ }
+
+ for delete in &msg.deletions {
+ wg.remove_peer(ts_tunnel::PeerId(delete.0));
+ }
+ }
+
+ tracing::trace!("applied new peer state");
+ }
+}
diff --git a/deps/ts_runtime/src/derp_latency.rs b/deps/ts_runtime/src/derp_latency.rs
new file mode 100644
index 00000000..38e2e8ee
--- /dev/null
+++ b/deps/ts_runtime/src/derp_latency.rs
@@ -0,0 +1,62 @@
+use std::sync::Arc;
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+use ts_netcheck::RegionResult;
+
+use crate::{Error, env::Env};
+
+#[derive(Clone, PartialEq, Eq)]
+pub struct DerpLatencyMeasurement {
+ pub measurement: Arc<Vec<RegionResult>>,
+}
+
+pub struct DerpLatencyMeasurer {
+ env: Env,
+}
+
+impl kameo::Actor for DerpLatencyMeasurer {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Env, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ tracing::trace!("derp latency measurer running");
+
+ Ok(Self { env })
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for DerpLatencyMeasurer {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ state_update: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let Some(derp_map) = &state_update.derp else {
+ return;
+ };
+
+ tracing::trace!("new derp map: beginning measurement");
+
+ let latencies = ts_netcheck::measure_derp_map(derp_map, &Default::default()).await;
+
+ tracing::trace!(?latencies, "measurement complete");
+
+ if let Err(e) = self
+ .env
+ .publish(DerpLatencyMeasurement {
+ measurement: Arc::new(latencies),
+ })
+ .await
+ {
+ tracing::error!(error = %e, "publishing");
+ };
+ }
+}
diff --git a/deps/ts_runtime/src/direct.rs b/deps/ts_runtime/src/direct.rs
new file mode 100644
index 00000000..1c377330
--- /dev/null
+++ b/deps/ts_runtime/src/direct.rs
@@ -0,0 +1,415 @@
+//! Direct UDP transport actor.
+//!
+//! This is an involved wrapper around a [`UdpSocket`] to make it function as an underlay transport.
+//! Technically, it's actually two sockets, an IPv4 and an IPv6 (if available). Both are bound
+//! to the unspecified address for their network type (`0.0.0.0` or `::`), and the socket to use to
+//! send a packet is determined by its address type.
+//!
+//! The primary function of this actor is just to bridge UDP tx/rx to its dataplane queues. But
+//! also, because it's the actor that holds the UDP socket(s) (and hence knows the ports each is
+//! bound on), it's additionally responsible for reporting this node's UDP endpoints on the bus (the
+//! control actor reports these to control, and the path discovery actor uses this to set its
+//! `CallMeMaybe` endpoints). It just aggregates the netmon and stun discovered addresses to
+//! (combining the netmon addresses with the local port) to make this report.
+//!
+//! No disco concerns are directly handled by this actor; these are done in [`disco`][crate::disco]
+//! (incoming `CallMeMaybe` and `Ping`s from peers) and [`path_discoverer`][crate::path_discoverer]
+//! (PD for outgoing traffic). Other parts of the system can trigger underlay packets to be sent
+//! by this actor directly via [`dataplane::SendUnderlay`][crate::dataplane::SendUnderlay] (only to
+//! be used when absolutely necessary, i.e. disco).
+
+use std::{
+ borrow::Borrow,
+ collections::HashMap,
+ io,
+ io::ErrorKind,
+ net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
+ sync::Arc,
+};
+
+use bytes::BytesMut;
+use futures::Stream;
+use futures_util::StreamExt;
+use itertools::Itertools;
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message, StreamMessage},
+};
+use tokio::net::UdpSocket;
+use tokio_stream::wrappers::UnboundedReceiverStream;
+use ts_bart::RoutingTable;
+use ts_control::{Endpoint, EndpointType};
+use ts_dataplane::async_tokio::{FromUnderlay, ToUnderlay, Tx};
+use ts_packet::PacketMut;
+use ts_transport::{DynEndpoint, UnderlayTransportId};
+
+use crate::{
+ dataplane::DataplaneActor,
+ disco::SendDisco,
+ env::Env,
+ netmon,
+ peer_tracker::{PeerDb, PeerState},
+ stunner::StunAddress,
+};
+
+pub struct DirectActor {
+ transport_id: UnderlayTransportId,
+
+ sock4: Arc<UdpSocket>,
+ sock6: Option<Arc<UdpSocket>>,
+
+ tx: Tx<FromUnderlay>,
+
+ peers: Arc<PeerDb>,
+
+ stun_addr: Option<SocketAddr>,
+ addrs_v4: Vec<Endpoint>,
+ addrs_v6: Vec<Endpoint>,
+
+ reachable: ts_bart::Table<()>,
+
+ env: Env,
+}
+
+#[derive(Clone, Debug)]
+pub struct NewEndpoints(pub Arc<[Endpoint]>);
+
+struct UdpRx(io::Result<HashMap<SocketAddr, Vec<PacketMut>>>);
+struct UdpTx(ToUnderlay);
+
+impl DirectActor {
+ async fn publish_endpoints(&self) -> Result<(), crate::Error> {
+ self.env
+ .publish(NewEndpoints(
+ self.addrs_v4
+ .iter()
+ .copied()
+ .chain(self.addrs_v6.iter().copied())
+ .chain(self.stun_addr.into_iter().map(|addr| Endpoint {
+ endpoint: addr,
+ ty: EndpointType::Stun,
+ }))
+ .collect(),
+ ))
+ .await
+ }
+
+ fn sock(&self, ipv4: bool) -> Option<&UdpSocket> {
+ if ipv4 {
+ Some(self.sock4.as_ref())
+ } else {
+ self.sock6.as_deref()
+ }
+ }
+}
+
+impl kameo::Actor for DirectActor {
+ type Args = Env;
+ type Error = crate::Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let (id, rx, tx) = env
+ .ask::<DataplaneActor, _>(None, crate::dataplane::NewUnderlayTransport, true)
+ .await?;
+
+ let sock4 = UdpSocket::bind("0.0.0.0:0").await.unwrap();
+ let sock4 = Arc::new(sock4);
+ tracing::debug!(transport_id = ?id, local_addr = %sock4.local_addr().unwrap(), "direct udp4 socket bound");
+
+ slf.attach_stream(udp_rx(sock4.clone()).boxed(), (), ());
+
+ let sock6 = UdpSocket::bind("[::]:0").await.ok().map(Arc::new);
+ if let Some(sock6) = sock6.as_ref() {
+ tracing::debug!(transport_id = ?id, local_addr = %sock6.local_addr().unwrap(), "direct udp6 socket bound");
+ slf.attach_stream(udp_rx(sock6.clone()).boxed(), (), ());
+ } else {
+ tracing::debug!("could not bind ipv6 direct");
+ }
+
+ slf.attach_stream(UnboundedReceiverStream::new(rx).map(UdpTx), (), ());
+
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+ env.subscribe::<StunAddress>(&slf).await?;
+ env.subscribe::<Arc<netmon::State>>(&slf).await?;
+ env.subscribe::<SendDisco>(&slf).await?;
+
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ transport_id: id,
+ sock4,
+ sock6,
+ env,
+ peers: Default::default(),
+ stun_addr: None,
+ addrs_v4: vec![],
+ addrs_v6: vec![],
+ reachable: Default::default(),
+ tx,
+ })
+ }
+}
+
+fn udp_rx(sock: impl Borrow<UdpSocket>) -> impl Stream<Item = UdpRx> {
+ // Required capacity to receive a UDP datagram. Any smaller and data may technically be dropped
+ // with a large MTU or a large fragmented packet. We keep the buffer capacity topped off to this
+ // size each time we try a receive in the loop below.
+ const REQUIRED_CAPACITY: usize = u16::MAX as _;
+
+ let buf = BytesMut::zeroed(REQUIRED_CAPACITY);
+
+ futures_util::stream::try_unfold((sock, buf), async |(sock, mut buf)| {
+ let mut v = vec![];
+
+ // Batch receive: wait until any packets are available from the socket, then try
+ // to read as many as possible until we exhaust the underlying buffer and see a wouldblock.
+ {
+ let sock = sock.borrow();
+ sock.readable().await?;
+
+ loop {
+ if buf.len() < REQUIRED_CAPACITY {
+ buf.resize(REQUIRED_CAPACITY, 0);
+ }
+
+ let (n, who) = match sock.try_recv_from(&mut buf) {
+ Ok((n, who)) => (n, who),
+ Err(e) if e.kind() == ErrorKind::WouldBlock => {
+ break;
+ }
+ Err(e) => return Err(e),
+ };
+
+ let pkt_buf = buf.split_to(n);
+ v.push((who, PacketMut::from(pkt_buf)));
+ }
+ };
+
+ tracing::trace!(n_pkts = v.len(), "udp rx batch");
+
+ Ok(Some((v.into_iter().into_group_map(), (sock, buf))))
+ })
+ .map(UdpRx)
+}
+
+impl Message<Arc<PeerState>> for DirectActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: Arc<PeerState>, _ctx: &mut Context<Self, Self::Reply>) {
+ self.peers = msg.peers.clone();
+ }
+}
+
+impl Message<SendDisco> for DirectActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ SendDisco { ep, buf }: SendDisco,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(ep) = ep.as_udp() else {
+ return;
+ };
+
+ let Some(sock) = self.sock(ep.is_ipv4()) else {
+ tracing::trace!(?ep, "can't send disco, no ipv6");
+ return;
+ };
+
+ if let Err(e) = sock.send_to(&buf, ep).await {
+ tracing::warn!(?ep, error = %e, ?sock, "sending disco msg");
+ } else {
+ tracing::trace!(?ep, "sent disco msg");
+ }
+ }
+}
+
+const CGNAT_RANGE: ipnet::Ipv4Net = ipnet::Ipv4Net::new_assert(Ipv4Addr::new(100, 64, 0, 0), 10);
+const TS_IP6_ULA: ipnet::Ipv6Net =
+ ipnet::Ipv6Net::new_assert(Ipv6Addr::new(0xfd7a, 0x115c, 0xa1e0, 0, 0, 0, 0, 0), 48);
+
+fn is_tailscale(ip: &IpAddr) -> bool {
+ match ip {
+ IpAddr::V4(v4) => CGNAT_RANGE.contains(v4),
+ IpAddr::V6(v6) => TS_IP6_ULA.contains(v6),
+ }
+}
+
+impl Message<Arc<netmon::State>> for DirectActor {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: Arc<netmon::State>, _ctx: &mut Context<Self, Self::Reply>) {
+ self.addrs_v4.clear();
+ self.addrs_v6.clear();
+ self.reachable.clear();
+
+ for (_id, addr) in msg.up_addrs() {
+ let ip = addr.addr();
+
+ let invalid = match ip {
+ IpAddr::V4(v4) => {
+ v4.is_broadcast()
+ || v4.is_loopback()
+ || v4.is_unspecified()
+ || v4.is_documentation()
+ || v4.is_multicast()
+ }
+ IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified() || v6.is_loopback(),
+ };
+
+ if invalid {
+ continue;
+ }
+
+ // NOTE(npry): this might be overly defensive -- while it probably makes sense to avoid
+ // nesting our traffic through a VPN tun device if possible, this does cut off a
+ // potential connectivity path, and we don't actually know for sure that it's tailscale.
+ // Notionally, it's better to connect through another tailnet (via tailscaled) or a
+ // 3rd-party VPN than not at all, but it may also make debugging horrible and this is
+ // likely an edge case, so skip for the time being.
+ if is_tailscale(&ip) {
+ continue;
+ }
+
+ let Some(port) = self
+ .sock(ip.is_ipv4())
+ .map(|x| x.local_addr().unwrap().port())
+ else {
+ continue;
+ };
+
+ let sockaddr = SocketAddr::new(ip, port);
+
+ let ty = match ip {
+ IpAddr::V4(x) => {
+ if x.is_link_local() || x.is_private() {
+ EndpointType::Local
+ } else {
+ EndpointType::Unknown
+ }
+ }
+ IpAddr::V6(x) => {
+ if x.is_unique_local() || x.is_unicast_link_local() {
+ EndpointType::Local
+ } else {
+ EndpointType::Unknown
+ }
+ }
+ };
+
+ let ep = Endpoint {
+ ty,
+ endpoint: sockaddr,
+ };
+
+ if ep.endpoint.is_ipv4() {
+ self.addrs_v4.push(ep);
+ } else if self.sock6.is_some() {
+ self.addrs_v6.push(ep);
+ }
+
+ self.reachable.insert(addr, ());
+ }
+
+ self.publish_endpoints().await.unwrap()
+ }
+}
+
+impl Message<StunAddress> for DirectActor {
+ type Reply = ();
+
+ async fn handle(&mut self, _msg: StunAddress, _ctx: &mut Context<Self, Self::Reply>) {
+ // TODO(npry): currently we're STUNning with the wrong socket (the global one in the
+ // stunner), so the local addr + NAT mapping is going to be wrong. don't add the STUNned
+ // address to our endpoints until it can actually reach this socket.
+
+ // self.stun_addr = Some(msg.addr);
+ // self.publish_endpoints().await.unwrap()
+ }
+}
+
+impl Message<StreamMessage<UdpTx, (), ()>> for DirectActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<UdpTx, (), ()>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let (ep_info, pkts) = match msg {
+ StreamMessage::Next(msg) => msg.0,
+ StreamMessage::Finished(_) => {
+ tracing::warn!("udp tx stream shut down, closing");
+ ctx.stop();
+ return;
+ }
+ _ => return,
+ };
+
+ if pkts.is_empty() {
+ return;
+ }
+
+ let Some(addr) = ep_info.as_udp() else {
+ tracing::warn!(?ep_info, "invalid endpoint info for udp direct");
+ return;
+ };
+
+ let Some(sock) = self.sock(addr.is_ipv4()) else {
+ tracing::debug!("trying to send ipv6 traffic without ipv6 connectivity");
+ return;
+ };
+
+ tracing::trace!(?ep_info, selected_addr = %addr, n_pkts = pkts.len(), "udp tx batch");
+
+ for pkt in pkts {
+ if let Err(e) = sock.send_to(&pkt, addr).await {
+ tracing::error!(error = %e, "sending packet");
+ }
+ }
+ }
+}
+
+impl Message<StreamMessage<UdpRx, (), ()>> for DirectActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<UdpRx, (), ()>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let msg = match msg {
+ StreamMessage::Next(msg) => msg.0,
+ StreamMessage::Finished(_) => {
+ tracing::warn!("udp rx stream shut down, closing");
+ return;
+ }
+ _ => return,
+ };
+
+ const ACCEPTABLE_ERR: &[io::ErrorKind] = {
+ use io::ErrorKind::*;
+ &[NetworkDown, NetworkUnreachable, HostUnreachable, TimedOut]
+ };
+
+ match msg {
+ Ok(mp) => {
+ for (ep, pkts) in mp {
+ tracing::trace!(who = %ep, n_pkts = pkts.len(), "udp rx batch (by sender)");
+
+ self.tx
+ .send((self.transport_id, DynEndpoint::udp(ep), pkts))
+ .unwrap();
+ }
+ }
+ Err(e) if ACCEPTABLE_ERR.contains(&e.kind()) => {
+ tracing::error!(error = %e, "udp receive error");
+ }
+ Err(e) => {
+ tracing::error!(error = %e, "unrecoverable error, die");
+ panic!()
+ }
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/disco.rs b/deps/ts_runtime/src/disco.rs
new file mode 100644
index 00000000..baf56894
--- /dev/null
+++ b/deps/ts_runtime/src/disco.rs
@@ -0,0 +1,148 @@
+use bytes::{Bytes, BytesMut};
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+use ts_disco_protocol::{MessageType, Ping, Pong};
+use ts_keys::{DiscoKeyPair, DiscoPublicKey};
+use ts_transport::DynEndpoint;
+use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes};
+
+use crate::{dataplane::IncomingDiscoMsg, env::Env};
+
+pub struct Disco {
+ env: Env,
+}
+
+impl kameo::Actor for Disco {
+ type Args = Env;
+ type Error = crate::Error;
+
+ async fn on_start(env: Env, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<IncomingDiscoMsg>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ Ok(Self { env })
+ }
+}
+
+#[derive(Clone)]
+pub struct SendDisco {
+ pub buf: Bytes,
+ pub ep: DynEndpoint,
+}
+
+impl Message<IncomingDiscoMsg> for Disco {
+ type Reply = ();
+
+ #[tracing::instrument(skip_all, fields(ty = ?msg.packet.get().ty(), sender = ?msg.sender))]
+ async fn handle(&mut self, msg: IncomingDiscoMsg, _ctx: &mut Context<Self, Self::Reply>) {
+ let pkt = msg.packet.get();
+
+ match pkt.ty() {
+ Some(MessageType::CallMeMaybe) => {
+ if !msg.sender.is_disco_coordinator() {
+ tracing::warn!("call me maybe received but from non-derp endpoint");
+ return;
+ };
+
+ let cmm = pkt.as_msg::<ts_disco_protocol::CallMeMaybe>().unwrap();
+
+ for ep in &cmm.endpoints {
+ let addr = ep.socket_addr();
+ let ep = DynEndpoint::udp(ep.socket_addr());
+
+ let tx_id = rand::random();
+
+ let buf = Self::mk_pkt::<Ping>(
+ Ping::size_with_padding(0),
+ &self.env.keys.disco_keys,
+ pkt.sender_pubkey(),
+ |ping| {
+ ping.node_key = self.env.keys.node_keys.public;
+ ping.tx_id = tx_id;
+ },
+ );
+
+ tracing::debug!(
+ %addr,
+ ?ep,
+ tx_id = ?format_args!("{:x?}", ts_hexdump::IterFmt::contiguous(&tx_id)),
+ "sent callmemaybe response ping"
+ );
+
+ self.env
+ .publish_noretain(SendDisco {
+ buf: buf.clone(),
+ ep,
+ })
+ .await
+ .unwrap();
+ }
+ }
+ Some(MessageType::Ping) => {
+ let ping = pkt.as_msg::<Ping>().unwrap();
+ tracing::debug!(?ping, "got ping");
+
+ let Some(sender) = msg.sender.as_udp() else {
+ tracing::warn!(?ping, "ping is from non-udp peer, bail");
+ return;
+ };
+
+ let buf = Self::mk_pkt::<Pong>(
+ Pong::size(),
+ &self.env.keys.disco_keys,
+ pkt.sender_pubkey(),
+ |pong| {
+ pong.tx_id = ping.tx_id;
+ pong.src = sender.into();
+ },
+ );
+
+ self.env
+ .publish_noretain(SendDisco {
+ buf,
+ ep: msg.sender,
+ })
+ .await
+ .unwrap();
+ }
+ Some(MessageType::Pong) => {
+ // ignore, handled by path discovery
+ }
+ Some(_ty) => {
+ tracing::trace!("unhandled disco type");
+ }
+ None => {
+ tracing::warn!("unknown disco type");
+ }
+ }
+ }
+}
+
+impl Disco {
+ pub fn mk_pkt<Msg>(
+ payload_size: usize,
+ disco_keys: &DiscoKeyPair,
+ rcpt_key: &DiscoPublicKey,
+ f: impl FnOnce(&mut Msg),
+ ) -> Bytes
+ where
+ Msg: ?Sized
+ + ts_disco_protocol::Message
+ + Immutable
+ + TryFromBytes
+ + IntoBytes
+ + KnownLayout,
+ {
+ let msg_size = ts_disco_protocol::Packet::size_for_message(payload_size);
+ let mut buf = BytesMut::zeroed(msg_size);
+
+ let resp = ts_disco_protocol::Packet::init_from_bytes::<Msg>(&mut buf, f).unwrap();
+
+ resp.encrypt_in_place(&disco_keys.private, rcpt_key, rand::random())
+ .unwrap();
+
+ buf.freeze()
+ }
+}
diff --git a/deps/ts_runtime/src/env.rs b/deps/ts_runtime/src/env.rs
new file mode 100644
index 00000000..5f53152d
--- /dev/null
+++ b/deps/ts_runtime/src/env.rs
@@ -0,0 +1,432 @@
+use core::any::Any;
+use std::sync::Arc;
+use std::time::Duration;
+
+use kameo::{
+ Reply,
+ actor::{ActorRef, Spawn, WeakActorRef},
+ error::SendError,
+ message::{Context, Message},
+ reply::DelegatedReply,
+};
+use kameo_actors::scheduler::Scheduler;
+use smol_str::SmolStr;
+
+use crate::{
+ Error, ErrorKind,
+ error::ResultExt,
+ registry,
+ registry::{ErasedWeakRef, Forward, Registry},
+ retained_bus::RetainedBus,
+};
+
+/// How long `Env::ask` waits before its first retry at an actor that is not running, and the
+/// longest it waits between two.
+const ASK_RETRY_FIRST: Duration = Duration::from_millis(10);
+const ASK_RETRY_MAX: Duration = Duration::from_secs(1);
+
+#[derive(Clone)]
+pub struct Env {
+ pub bus: ActorRef<RetainedBus>,
+ pub scheduler: ActorRef<Scheduler>,
+ pub registry: ActorRef<Registry>,
+
+ pub keys: Arc<ts_keys::NodeState>,
+}
+
+impl Env {
+ pub fn new(keys: ts_keys::NodeState) -> Self {
+ Self {
+ bus: RetainedBus::spawn_default(),
+ scheduler: Scheduler::spawn_default(),
+ registry: Registry::spawn_default(),
+ keys: Arc::new(keys),
+ }
+ }
+
+ pub async fn subscribe<M>(&self, slf: &ActorRef<impl Message<M>>) -> Result<(), Error>
+ where
+ M: Clone + Send + 'static,
+ {
+ self.bus
+ .tell(crate::retained_bus::Register(slf.clone().recipient::<M>()))
+ .await
+ .with_actor_info(&self.bus)?;
+
+ Ok(())
+ }
+
+ pub async fn publish<M>(&self, msg: M) -> Result<(), Error>
+ where
+ M: Clone + Send + 'static,
+ {
+ self.bus
+ .tell(crate::retained_bus::Publish::retained(msg))
+ .await
+ .with_actor_info(&self.bus)?;
+
+ Ok(())
+ }
+
+ pub async fn publish_noretain<M>(&self, msg: M) -> Result<(), Error>
+ where
+ M: Clone + Send + 'static,
+ {
+ self.bus
+ .tell(crate::retained_bus::Publish::unretained(msg))
+ .await
+ .with_actor_info(&self.bus)?;
+
+ Ok(())
+ }
+
+ /// Register an actor in the registry under the specified `name`. If `None`, the actor is
+ /// registered as the canonical actor of type `A`.
+ ///
+ /// If the return value is `Ok(Some(_))`, this registration replaced an existing registration
+ /// for the same name. The actor ref is erased, meaning that you must try to downcast it to a
+ /// `WeakActorRef<B>` for some actor type `B` if you need an `ActorRef` to send messages to.
+ pub async fn register<A>(
+ &self,
+ name: Option<SmolStr>,
+ slf: &ActorRef<A>,
+ ) -> Result<Option<ErasedWeakRef>, Error>
+ where
+ A: kameo::Actor,
+ {
+ self.registry
+ .ask(registry::Register::new(name, slf))
+ .await
+ .with_actor_info(&self.registry)
+ }
+
+ /// Look up an actor in the registry.
+ pub async fn lookup_opt<A>(
+ &self,
+ name: Option<SmolStr>,
+ ) -> Result<Option<WeakActorRef<A>>, Error>
+ where
+ A: kameo::Actor + Any,
+ {
+ let aref = self
+ .registry
+ .ask(registry::Lookup::<A>::new(name))
+ .await
+ .with_actor_info(&self.registry)?;
+
+ Ok(aref)
+ }
+
+ /// Look up an actor in the registry.
+ ///
+ /// Reports an error if the actor is in the registry but dead.
+ pub async fn lookup<A>(&self, name: Option<SmolStr>) -> Result<ActorRef<A>, Error>
+ where
+ A: kameo::Actor + Any,
+ {
+ let aref = self.lookup_opt::<A>(name).await?;
+ self.resolve_aref(aref)
+ }
+
+ /// Ensure the given actor exists in the registry.
+ ///
+ /// If it doesn't, create the actor using the given function to populate its args.
+ pub async fn ensure<A, F, Fut>(
+ &self,
+ name: Option<SmolStr>,
+ args_builder: F,
+ ) -> Result<(bool, ActorRef<A>), Error>
+ where
+ A: kameo::Actor,
+ F: FnOnce() -> Fut + Send + 'static,
+ Fut: Future<Output = A::Args> + Send + 'static,
+ {
+ let (created, aref) = self
+ .registry
+ .ask(registry::Ensure::new(name, args_builder))
+ .await?;
+
+ Ok((created, aref))
+ }
+
+ /// Send an ask to an actor in the registry.
+ ///
+ /// # Parameters
+ ///
+ /// - `name`: the name of the actor to send the message to. If `None`, uses the canonical actor.
+ /// - `msg`: the message to send.
+ /// - `wait`: whether to wait for the actor to be registered if it's not present in the
+ /// registry.
+ pub async fn ask<A, M>(
+ &self,
+ name: Option<SmolStr>,
+ mut msg: M,
+ wait: bool,
+ ) -> Result<<A::Reply as kameo::Reply>::Ok, Error>
+ where
+ A: Message<M> + Any,
+ M: Send + 'static,
+ {
+ let mk_forward = |msg| Forward::<A, _>::new(name.clone(), msg);
+
+ // If we aren't waiting, this just looks like `return self.registry.ask(forward).await?`
+ //
+ // If we are, the idea is to prefer to use `registry::Forward` to do both the lookup and
+ // forward steps inside the registry (atomically from the rest of the system's perspective).
+ // We could do them here, but there could be a race in between (we wouldn't know if the
+ // ActorRef for a given name is still alive and owning the name by the time we call it). If
+ // the forward fails, we use a lookup to determine if there has been activity on the name,
+ // but we throw away the result and just retry the forward for the previously discussed
+ // reasons.
+ //
+ // ThinkTerm: an actor that stopped for good keeps its name in the registry, so the lookup
+ // answers at once and the forward fails again; each retry waits a little longer, up to a
+ // second, rather than spin.
+ let mut pause = ASK_RETRY_FIRST;
+ loop {
+ let result = self.registry.ask(mk_forward(msg)).await;
+
+ match result {
+ Ok(x) => return Ok(x),
+ Err(SendError::HandlerError(SendError::ActorNotRunning(m))) if wait => {
+ msg = m;
+ }
+ Err(SendError::HandlerError(e)) => {
+ return Err(e).with_actor_info(&self.registry);
+ }
+ // The registry itself didn't take the request or answer it, e.g. while the
+ // runtime shuts down.
+ Err(e) => {
+ return Err(e).with_actor_info(&self.registry);
+ }
+ }
+
+ tracing::trace!("in ask: actor not available, wait until register");
+ self.registry
+ .ask(registry::Lookup::<A>::new(name.clone()).wait(true))
+ .await
+ .with_actor_info(&self.registry)?;
+ tokio::time::sleep(pause).await;
+ pause = (pause * 2).min(ASK_RETRY_MAX);
+ }
+ }
+
+ /// Send a tell to an actor in the registry.
+ ///
+ /// The result provides no feedback about whether the actor was running or not.
+ pub async fn tell<A, M>(&self, name: Option<SmolStr>, msg: M) -> Result<(), Error>
+ where
+ A: Message<M> + Any,
+ M: Send + 'static,
+ {
+ self.registry
+ .tell(Forward::<A, _>::new(name, msg))
+ .await
+ .with_actor_info(&self.registry)
+ }
+
+ /// Forward a message to an actor through the registry.
+ ///
+ /// The reply comes from a task that asks on the caller's behalf, not from kameo's
+ /// `Context::forward`, which in kameo 0.21 panics if the registry's mailbox is closed.
+ pub fn forward<A, M>(
+ &self,
+ ctx: &mut Context<impl kameo::Actor, RegForwarded<A, M>>,
+ name: Option<SmolStr>,
+ msg: M,
+ ) -> RegForwarded<A, M>
+ where
+ A: Message<M> + Any,
+ M: Send + 'static,
+ {
+ let env = self.clone();
+ ctx.spawn(async move { env.ask::<A, M>(name, msg, false).await })
+ }
+
+ /// Wait for a specific actor of type `A` to appear in the registry.
+ pub async fn wait<A>(&self, name: Option<SmolStr>) -> Result<(), Error>
+ where
+ A: kameo::Actor,
+ {
+ self.registry
+ .ask(registry::Lookup::<A>::new(name).wait(true))
+ .await?;
+
+ Ok(())
+ }
+
+ fn resolve_aref<A>(&self, aref: Option<WeakActorRef<A>>) -> Result<ActorRef<A>, Error>
+ where
+ A: kameo::Actor,
+ {
+ let aref = aref.ok_or_else(|| Error {
+ kind: ErrorKind::ActorGone,
+ message_ty: Some("Lookup"),
+ target_actor: Some((&self.registry).into()),
+ })?;
+
+ let aref = aref.upgrade().ok_or_else(|| Error {
+ kind: ErrorKind::ActorGone,
+ message_ty: None,
+ target_actor: Some((&aref).into()),
+ })?;
+
+ Ok(aref)
+ }
+}
+
+pub type RegForwarded<A, M> =
+ DelegatedReply<Result<<<A as Message<M>>::Reply as Reply>::Ok, Error>>;
+
+#[cfg(test)]
+mod tests {
+ use kameo::{actor::Spawn, error::Infallible, mailbox};
+ use tokio::sync::Notify;
+
+ use super::*;
+
+ /// Answers [`Ping`], and stays in [`Hold`] until notified.
+ struct Target(Arc<Notify>);
+
+ impl kameo::Actor for Target {
+ type Args = Arc<Notify>;
+ type Error = Infallible;
+
+ async fn on_start(release: Self::Args, _: ActorRef<Self>) -> Result<Self, Self::Error> {
+ Ok(Self(release))
+ }
+ }
+
+ struct Hold;
+
+ impl Message<Hold> for Target {
+ type Reply = ();
+
+ async fn handle(&mut self, _: Hold, _: &mut Context<Self, Self::Reply>) {
+ self.0.notified().await;
+ }
+ }
+
+ struct Ping;
+
+ impl Message<Ping> for Target {
+ type Reply = u32;
+
+ async fn handle(&mut self, _: Ping, _: &mut Context<Self, Self::Reply>) -> u32 {
+ 7
+ }
+ }
+
+ /// Forwards [`Ping`] through the registry, as the runtime does for the device.
+ struct Front(Env);
+
+ impl kameo::Actor for Front {
+ type Args = Env;
+ type Error = Infallible;
+
+ async fn on_start(env: Self::Args, _: ActorRef<Self>) -> Result<Self, Self::Error> {
+ Ok(Self(env))
+ }
+ }
+
+ impl Message<Ping> for Front {
+ type Reply = RegForwarded<Target, Ping>;
+
+ async fn handle(&mut self, msg: Ping, ctx: &mut Context<Self, Self::Reply>) -> Self::Reply {
+ self.0.forward::<Target, Ping>(ctx, None, msg)
+ }
+ }
+
+ async fn env_with(target: &ActorRef<Target>) -> Env {
+ let env = Env::new(ts_keys::NodeState::generate());
+ env.register(None, target).await.unwrap();
+ env
+ }
+
+ /// The kind of error the front got back, if it got one from forwarding.
+ async fn front_error(env: &Env) -> Option<ErrorKind> {
+ match Front::spawn(env.clone()).ask(Ping).await {
+ Err(SendError::HandlerError(err)) => Some(err.kind),
+ _ => None,
+ }
+ }
+
+ /// Whether the registry still forwards, to a second target.
+ async fn registry_works(env: &Env) -> bool {
+ let other = Target::spawn(Arc::default());
+ env.register(Some("other".into()), &other).await.unwrap();
+ let answer = env
+ .ask::<Target, _>(Some("other".into()), Ping, false)
+ .await;
+ answer == Ok(7)
+ }
+
+ #[tokio::test]
+ async fn a_forward_gets_the_actors_reply() {
+ let target = Target::spawn(Arc::default());
+ let env = env_with(&target).await;
+
+ assert_eq!(env.ask::<Target, _>(None, Ping, false).await, Ok(7));
+ assert_eq!(Front::spawn(env).ask(Ping).await.ok(), Some(7));
+ }
+
+ #[tokio::test]
+ async fn a_full_mailbox_is_an_error_and_the_registry_carries_on() {
+ let target = Target::spawn_with_mailbox(Arc::default(), mailbox::bounded(1));
+ let env = env_with(&target).await;
+ // One hold being handled and one waiting fill a mailbox of one.
+ target.tell(Hold).await.unwrap();
+ target.tell(Hold).await.unwrap();
+
+ let err = env.ask::<Target, _>(None, Ping, false).await.unwrap_err();
+ assert_eq!(err.kind, ErrorKind::MailboxFull);
+ assert_eq!(front_error(&env).await, Some(ErrorKind::MailboxFull));
+ assert!(registry_works(&env).await);
+ }
+
+ #[tokio::test]
+ async fn a_stopped_actor_is_an_error_and_the_registry_carries_on() {
+ let target = Target::spawn(Arc::default());
+ let env = env_with(&target).await;
+ target.stop_gracefully().await.unwrap();
+ target.wait_for_shutdown().await;
+
+ // `target` keeps the ref alive, so the registry still finds the actor, its mailbox closed.
+ let err = env.ask::<Target, _>(None, Ping, false).await.unwrap_err();
+ assert_eq!(err.kind, ErrorKind::ActorGone);
+ assert_eq!(front_error(&env).await, Some(ErrorKind::ActorGone));
+ assert!(registry_works(&env).await);
+ }
+
+ #[tokio::test]
+ async fn an_ask_that_waits_comes_through_once_a_new_actor_takes_the_name() {
+ let target = Target::spawn(Arc::default());
+ let env = env_with(&target).await;
+ target.stop_gracefully().await.unwrap();
+ target.wait_for_shutdown().await;
+
+ let waiting = tokio::spawn({
+ let env = env.clone();
+ async move { env.ask::<Target, _>(None, Ping, true).await }
+ });
+ // Retrying at the stopped one, a pause at a time.
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ assert!(!waiting.is_finished());
+ let fresh = Target::spawn(Arc::default());
+ env.register(None, &fresh).await.unwrap();
+ let answer = tokio::time::timeout(Duration::from_secs(3), waiting).await;
+ assert_eq!(answer.unwrap().unwrap(), Ok(7));
+ }
+
+ #[tokio::test]
+ async fn a_stopped_registry_is_an_error() {
+ let target = Target::spawn(Arc::default());
+ let env = env_with(&target).await;
+ env.registry.stop_gracefully().await.unwrap();
+ env.registry.wait_for_shutdown().await;
+
+ let err = env.ask::<Target, _>(None, Ping, false).await.unwrap_err();
+ assert_eq!(err.kind, ErrorKind::ActorGone);
+ assert_eq!(front_error(&env).await, Some(ErrorKind::ActorGone));
+ }
+}
diff --git a/deps/ts_runtime/src/error.rs b/deps/ts_runtime/src/error.rs
new file mode 100644
index 00000000..5146217a
--- /dev/null
+++ b/deps/ts_runtime/src/error.rs
@@ -0,0 +1,212 @@
+use core::fmt::{Formatter, Write};
+
+use kameo::{
+ Actor,
+ actor::{ActorRef, WeakActorRef},
+ error::{HookError, Infallible, SendError},
+};
+
+pub(crate) trait ResultExt {
+ type T;
+
+ /// Attach actor info to this result if it's an error.
+ fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Result<Self::T, Error>;
+}
+
+impl<T, E> ResultExt for Result<T, E>
+where
+ E: Into<Error>,
+{
+ type T = T;
+
+ fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Result<T, Error> {
+ self.map_err(|e| e.into().with_actor_info(aref))
+ }
+}
+
+/// Errors that may occur while executing or interacting with the runtime.
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub struct Error {
+ /// Kind of error this is.
+ pub kind: ErrorKind,
+
+ /// Rust type name of sent message that caused the error.
+ ///
+ /// May not be populated if this was not generated as a result of sending a message.
+ pub message_ty: Option<&'static str>,
+
+ /// Actor to which the message was being sent (optional).
+ ///
+ /// May not be populated if either this wasn't from an actor message, or the actor ref
+ /// wasn't available when the error was constructed.
+ pub target_actor: Option<ActorInfo>,
+}
+
+impl Error {
+ /// Attach information about a destination actor to this error.
+ ///
+ /// Typically, `aref` will be `&ActorRef`.
+ pub fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Self {
+ Self {
+ target_actor: Some(aref.into()),
+ ..self
+ }
+ }
+}
+
+impl core::fmt::Display for Error {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ // format is "sending MESSAGE_TYPE to ACTOR: ERROR_DESC", the complexity is to account for
+ // independently-optional fields
+
+ if let Some(ty) = self.message_ty {
+ write!(f, "sending {ty}")?;
+
+ if self.target_actor.is_some() {
+ f.write_char(' ')?;
+ }
+ }
+
+ if let Some(actor) = &self.target_actor {
+ write!(f, "{actor}")?;
+ }
+
+ if self.message_ty.is_some() || self.target_actor.is_some() {
+ f.write_str(": ")?;
+ }
+
+ self.kind.fmt(f)
+ }
+}
+
+impl core::error::Error for Error {}
+
+impl<M, E> From<SendError<M, E>> for Error {
+ fn from(err: SendError<M, E>) -> Self {
+ Self {
+ kind: err.into(),
+ message_ty: Some(core::any::type_name::<M>()),
+ target_actor: None,
+ }
+ }
+}
+
+impl<E> From<HookError<E>> for Error {
+ fn from(value: HookError<E>) -> Self {
+ match value {
+ HookError::Error(_) => Self {
+ kind: ErrorKind::ReplyErr,
+ target_actor: None,
+ message_ty: Some("ActorStartOrStop"),
+ },
+ HookError::Panicked(_) => Self {
+ kind: ErrorKind::ActorGone,
+ target_actor: None,
+ message_ty: Some("ActorStartOrStop"),
+ },
+ }
+ }
+}
+
+impl From<Infallible> for Error {
+ fn from(_: Infallible) -> Self {
+ unreachable!()
+ }
+}
+
+/// Info about an actor that caused an error.
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub struct ActorInfo {
+ /// The Rust type name of the actor.
+ pub ty: &'static str,
+
+ /// The actor's unique id.
+ pub id: kameo::actor::ActorId,
+}
+
+impl<A> From<&ActorRef<A>> for ActorInfo
+where
+ A: Actor,
+{
+ fn from(aref: &ActorRef<A>) -> Self {
+ Self {
+ ty: core::any::type_name::<A>(),
+ id: aref.id(),
+ }
+ }
+}
+
+impl<A> From<ActorRef<A>> for ActorInfo
+where
+ A: Actor,
+{
+ fn from(aref: ActorRef<A>) -> Self {
+ Self::from(&aref)
+ }
+}
+
+impl<A> From<&WeakActorRef<A>> for ActorInfo
+where
+ A: Actor,
+{
+ fn from(aref: &WeakActorRef<A>) -> Self {
+ Self {
+ ty: core::any::type_name::<A>(),
+ id: aref.id(),
+ }
+ }
+}
+
+impl<A> From<WeakActorRef<A>> for ActorInfo
+where
+ A: Actor,
+{
+ fn from(aref: WeakActorRef<A>) -> Self {
+ Self::from(&aref)
+ }
+}
+
+impl core::fmt::Display for ActorInfo {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ write!(f, "{}({})", self.ty, self.id)
+ }
+}
+
+/// Kinds of errors that may occur while running the runtime.
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub enum ErrorKind {
+ /// An actor that was expected to be available was unreachable, probably because it
+ /// either panicked or exited. It's unlikely this operation can be retried successfully.
+ ActorGone,
+
+ /// The actor replied with an error.
+ ReplyErr,
+
+ /// The target actor's mailbox was full.
+ MailboxFull,
+
+ /// An operation timed out.
+ Timeout,
+}
+
+impl core::fmt::Display for ErrorKind {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Self::ActorGone => write!(f, "expected actor is unreachable"),
+ Self::ReplyErr => write!(f, "actor replied with an error"),
+ Self::MailboxFull => write!(f, "actor's mailbox was full"),
+ Self::Timeout => write!(f, "operation timed out"),
+ }
+ }
+}
+
+impl<M, E> From<SendError<M, E>> for ErrorKind {
+ fn from(err: SendError<M, E>) -> Self {
+ match err {
+ SendError::ActorNotRunning(_) | SendError::ActorStopped => ErrorKind::ActorGone,
+ SendError::HandlerError(_) => ErrorKind::ReplyErr,
+ SendError::MailboxFull(..) => ErrorKind::MailboxFull,
+ SendError::Timeout(_) => ErrorKind::Timeout,
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/lib.rs b/deps/ts_runtime/src/lib.rs
new file mode 100644
index 00000000..ca6f97c8
--- /dev/null
+++ b/deps/ts_runtime/src/lib.rs
@@ -0,0 +1,192 @@
+//! Tailscale runtime.
+
+extern crate ts_netstack_smoltcp as netstack;
+
+use std::sync::Arc;
+
+use kameo::message::Context;
+use tokio::sync::Mutex;
+
+use crate::{
+ control_runner::ControlRunner, dataplane::DataplaneActor, env::RegForwarded,
+ multiderp::Multiderp, netstack_actor::NetstackActor, peer_tracker::PeerTracker,
+};
+
+/// Control runner.
+pub mod control_runner;
+mod dataplane;
+mod derp_latency;
+mod direct;
+mod disco;
+mod env;
+mod error;
+mod multiderp;
+mod netmon;
+mod netstack_actor;
+mod packetfilter;
+mod path_discoverer;
+pub mod peer_tracker;
+mod registry;
+mod retained_bus;
+mod route_updater;
+mod src_filter;
+mod stunner;
+mod task;
+
+pub(crate) use env::Env;
+pub use error::{Error, ErrorKind};
+pub use kameo::actor::{ActorRef, Spawn};
+pub use registry::Registry;
+pub use task::{ErasedTask, Task};
+
+/// The runtime for a tailscale device.
+pub struct Runtime {
+ env: Env,
+}
+
+/// Configuration for starting a [`Runtime`].
+#[derive(Debug, Clone)]
+pub struct Config {
+ /// The control configuration to use.
+ pub control_config: ts_control::Config,
+ /// The auth key to use to connect to the control server.
+ pub auth_key: Option<String>,
+ /// The keys to use.
+ pub keys: ts_keys::NodeState,
+}
+
+impl kameo::Actor for Runtime {
+ type Error = Error;
+ type Args = Config;
+
+ async fn on_start(config: Config, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let env = Env::new(config.keys);
+
+ env.bus.link(&slf).await;
+ env.scheduler.link(&slf).await;
+ env.registry.link(&slf).await;
+
+ #[cfg(feature = "console")]
+ {
+ // Runs detached.
+ if let Err(e) =
+ kameo::console::serve((core::net::Ipv4Addr::new(127, 0, 0, 1), 9999)).await
+ {
+ tracing::error!(error = %e, "console died");
+ };
+ }
+
+ DataplaneActor::supervise(&slf, env.clone()).spawn().await;
+
+ let (netstack_id, netstack_up, netstack_down) = env
+ .ask::<DataplaneActor, _>(None, dataplane::NewOverlayTransport, true)
+ .await?;
+
+ disco::Disco::supervise(&slf, env.clone()).spawn().await;
+
+ Multiderp::supervise(&slf, env.clone()).spawn().await;
+ direct::DirectActor::supervise(&slf, env.clone())
+ .spawn()
+ .await;
+
+ route_updater::RouteUpdater::supervise(&slf, (env.clone(), netstack_id))
+ .spawn()
+ .await;
+ packetfilter::PacketfilterUpdater::supervise(&slf, env.clone())
+ .spawn()
+ .await;
+ src_filter::SourceFilterUpdater::supervise(&slf, env.clone())
+ .spawn()
+ .await;
+ stunner::Stunner::supervise(&slf, env.clone()).spawn().await;
+ path_discoverer::PathDiscoverer::supervise(&slf, env.clone())
+ .spawn()
+ .await;
+
+ if let Some(mon) = ts_netmon::platform_mon() {
+ netmon::NetmonActor::supervise(&slf, (env.clone(), Arc::new(mon)))
+ .spawn()
+ .await;
+ }
+
+ PeerTracker::supervise(&slf, env.clone()).spawn().await;
+
+ NetstackActor::supervise(
+ &slf,
+ (
+ env.clone(),
+ netstack::netcore::Config {
+ tcp_buffer_size: 64 * 1024,
+ command_channel_capacity: Some(128),
+ ..Default::default()
+ },
+ netstack_id,
+ netstack_up,
+ Arc::new(Mutex::new(netstack_down)),
+ ),
+ )
+ .spawn()
+ .await;
+
+ control_runner::DialerActor::supervise(&slf, env.clone())
+ .spawn()
+ .await;
+
+ ControlRunner::supervise(
+ &slf,
+ control_runner::Params {
+ config: config.control_config,
+ auth_key: config.auth_key,
+ env: env.clone(),
+ },
+ )
+ .spawn()
+ .await;
+
+ // Actors we forward messages for:
+ env.wait::<ControlRunner>(None).await?;
+ env.wait::<NetstackActor>(None).await?;
+ env.wait::<PeerTracker>(None).await?;
+
+ Ok(Self { env })
+ }
+}
+
+macro_rules! forward {
+ ($actor:ty, $($msg:ty),* $(,)?) => {
+ $(
+ pub use $msg;
+
+ impl kameo::message::Message<$msg> for Runtime {
+ type Reply = RegForwarded<$actor, $msg>;
+
+ async fn handle(
+ &mut self,
+ msg: $msg,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) -> RegForwarded<$actor, $msg> {
+ self.env.forward::<$actor, $msg>(ctx, None, msg)
+ }
+ }
+ )*
+ };
+}
+
+forward!(NetstackActor, netstack_actor::GetChannel);
+forward!(
+ ControlRunner,
+ control_runner::Ipv4,
+ control_runner::Ipv6,
+ control_runner::SelfNode,
+ control_runner::AuthUrl,
+ control_runner::CurrentSelfNode,
+ control_runner::CurrentAuthUrl,
+ control_runner::CurrentRegistrationError,
+);
+forward!(
+ PeerTracker,
+ peer_tracker::PeerByName,
+ peer_tracker::PeerByTailnetIp,
+ peer_tracker::PeerByAcceptedRoute,
+ peer_tracker::AllPeers
+);
diff --git a/deps/ts_runtime/src/multiderp/mod.rs b/deps/ts_runtime/src/multiderp/mod.rs
new file mode 100644
index 00000000..048f9d5f
--- /dev/null
+++ b/deps/ts_runtime/src/multiderp/mod.rs
@@ -0,0 +1,252 @@
+mod uniderp;
+
+use std::{
+ collections::HashMap,
+ sync::Arc,
+ time::{Duration, Instant},
+};
+
+use kameo::{
+ actor::{ActorRef, Spawn, WeakActorRef},
+ error::ActorStopReason,
+ message::{Context, Message},
+};
+use kameo_actors::scheduler::SetTimeout;
+use ts_control::DerpRegion;
+use ts_derp::RegionId;
+use ts_packet::PacketMut;
+use ts_transport::UnderlayTransportId;
+
+use crate::{
+ Env, Error,
+ dataplane::{DataplaneActor, SendUnderlay},
+ disco::SendDisco,
+ multiderp::uniderp::Uniderp,
+ peer_tracker::{PeerDb, PeerState},
+};
+
+/// Consumes derp map updates and spawns an actor per region that runs an underlay transport.
+/// Also consumes home derp indications (for this node) to notify the relevant task that it
+/// should keep the transport awake even if there is no traffic.
+///
+/// Other than the home task (which is always kept alive to receive packets), the transport
+/// tasks keep the connection alive as long as there is traffic sent or received, and for a
+/// short grace period afterward. Connections are otherwise closed not in use.
+pub struct Multiderp {
+ region_map: HashMap<RegionId, UnderlayTransportId>,
+ env: Env,
+ debounce_state: DebounceState,
+ peer_state: Arc<PeerDb>,
+}
+
+impl kameo::Actor for Multiderp {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+ env.subscribe::<SendDisco>(&slf).await?;
+
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ env,
+ region_map: Default::default(),
+ debounce_state: Default::default(),
+ peer_state: Default::default(),
+ })
+ }
+
+ async fn on_stop(
+ &mut self,
+ _: WeakActorRef<Self>,
+ _: ActorStopReason,
+ ) -> Result<(), Self::Error> {
+ self.env.publish(DerpTransportMap::default()).await?;
+
+ Ok(())
+ }
+}
+
+impl Multiderp {
+ #[tracing::instrument(skip_all, fields(region_id = %id))]
+ async fn ensure_region(&mut self, slf: &ActorRef<Self>, id: RegionId, region: &DerpRegion) {
+ if self
+ .env
+ .lookup_opt::<Uniderp>(Some(Uniderp::name(id)))
+ .await
+ .unwrap()
+ .is_some()
+ {
+ return;
+ }
+
+ tracing::trace!("spawn new uniderp");
+ Uniderp::supervise(
+ slf,
+ uniderp::Args {
+ env: self.env.clone(),
+ region: region.clone(),
+ region_id: id,
+ },
+ )
+ .spawn()
+ .await;
+ }
+
+ /// Trailing-edge debounce for region id -> transport id map updates.
+ ///
+ /// Delays these messages slightly in the interest of coalescing large quantities of updates
+ /// from derp map updates and uniderp spawns to avoid repetitive, partial downstream route
+ /// recomputations.
+ async fn debounce_map_publish(&mut self, slf: &ActorRef<Self>) {
+ const DEBOUNCE_DUR: Duration = Duration::from_millis(25);
+
+ if self.debounce_state.publish_enqueued {
+ return;
+ }
+
+ self.env
+ .scheduler
+ .ask(SetTimeout::new(
+ slf.downgrade(),
+ DEBOUNCE_DUR,
+ DebouncedPublish,
+ ))
+ .await
+ .unwrap();
+
+ self.debounce_state.publish_enqueued = true;
+ }
+
+ async fn do_map_publish(&mut self) {
+ self.env
+ .publish(DerpTransportMap(Arc::new(self.region_map.clone())))
+ .await
+ .unwrap();
+
+ self.debounce_state.last_publish = Some(Instant::now());
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for Multiderp {
+ type Reply = ();
+
+ #[tracing::instrument(skip_all, name = "multiderp map update")]
+ async fn handle(
+ &mut self,
+ msg: Arc<ts_control::StateUpdate>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(derp_map) = &msg.derp else {
+ return;
+ };
+
+ for (id, region) in derp_map {
+ self.ensure_region(ctx.actor_ref(), *id, region).await;
+ }
+ }
+}
+
+#[derive(Clone, Debug, Default)]
+pub struct DerpTransportMap(pub Arc<HashMap<RegionId, UnderlayTransportId>>);
+
+struct SetRegionTransportId(RegionId, Option<UnderlayTransportId>);
+
+impl Message<SetRegionTransportId> for Multiderp {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ SetRegionTransportId(region, id): SetRegionTransportId,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let pre = self.region_map.get(&region).copied();
+
+ match id {
+ Some(id) => {
+ self.region_map.insert(region, id);
+ }
+ None => {
+ self.region_map.remove(&region);
+ }
+ }
+
+ let post = self.region_map.get(&region).copied();
+
+ if pre != post {
+ self.debounce_map_publish(_ctx.actor_ref()).await;
+ }
+ }
+}
+
+#[derive(Default)]
+struct DebounceState {
+ /// The last time a message was published.
+ last_publish: Option<Instant>,
+
+ /// Whether there is a publish timer currently enqueued.
+ publish_enqueued: bool,
+}
+
+#[derive(Copy, Clone, Debug)]
+struct DebouncedPublish;
+
+impl Message<DebouncedPublish> for Multiderp {
+ type Reply = ();
+
+ async fn handle(&mut self, _: DebouncedPublish, _: &mut Context<Self, Self::Reply>) {
+ self.do_map_publish().await;
+ self.debounce_state.publish_enqueued = false;
+ }
+}
+
+impl Message<Arc<PeerState>> for Multiderp {
+ type Reply = ();
+
+ async fn handle(&mut self, state: Arc<PeerState>, _: &mut Context<Self, Self::Reply>) {
+ self.peer_state = state.peers.clone();
+ }
+}
+
+impl Message<SendDisco> for Multiderp {
+ type Reply = ();
+
+ async fn handle(&mut self, send: SendDisco, _: &mut Context<Self, Self::Reply>) {
+ let Some(nodekey) = send.ep.as_derp() else {
+ return;
+ };
+
+ let Some((_, peer)) = self.peer_state.get(&nodekey) else {
+ tracing::trace!(%nodekey, "no known peer");
+ return;
+ };
+
+ let Some(derp_region) = &peer.derp_region else {
+ tracing::trace!(%nodekey, "peer had no derp region");
+ return;
+ };
+
+ let Some(transport_id) = self.region_map.get(derp_region) else {
+ tracing::trace!(%nodekey, ?derp_region, "derp region had no transport");
+ return;
+ };
+
+ let sent = self
+ .env
+ .ask::<DataplaneActor, _>(
+ None,
+ SendUnderlay {
+ endpoint: send.ep,
+ bufs: vec![PacketMut::from(send.buf.to_vec())],
+ underlay_id: *transport_id,
+ },
+ true,
+ )
+ .await
+ .unwrap();
+
+ tracing::trace!(?sent, %nodekey, ?derp_region, "disco send result");
+ }
+}
diff --git a/deps/ts_runtime/src/multiderp/uniderp.rs b/deps/ts_runtime/src/multiderp/uniderp.rs
new file mode 100644
index 00000000..9354414f
--- /dev/null
+++ b/deps/ts_runtime/src/multiderp/uniderp.rs
@@ -0,0 +1,346 @@
+use std::{
+ sync::{Arc, RwLock},
+ time::{Duration, Instant},
+};
+
+use futures::FutureExt;
+use kameo::{
+ actor::{ActorRef, Spawn, WeakActorRef},
+ error::ActorStopReason,
+ message::{Context, Message},
+};
+use smol_str::SmolStr;
+use tokio::sync::{Mutex, watch};
+use ts_control::DerpRegion;
+use ts_dataplane::async_tokio::{FromUnderlay, Rx, ToUnderlay, Tx};
+use ts_derp::RegionId;
+use ts_keys::NodeKeyPair;
+use ts_packet::PacketMut;
+use ts_transport::{BatchRecvIter, DynEndpoint, UnderlayTransport, UnderlayTransportId};
+
+use crate::{
+ Task,
+ dataplane::{DataplaneActor, NewUnderlayTransport},
+ derp_latency::DerpLatencyMeasurement,
+ env::Env,
+ multiderp::{Multiderp, SetRegionTransportId},
+ peer_tracker::{PeerDb, PeerState},
+ task::ErasedTask,
+};
+
+#[derive(Clone)]
+pub struct Args {
+ pub region_id: RegionId,
+ pub region: DerpRegion,
+ pub env: Env,
+}
+
+/// Single-region derp client.
+pub struct Uniderp {
+ /// Current state to spawn a runner task.
+ ///
+ /// Retained here because we may need to kill and restart the runner if we get updated region
+ /// info.
+ runner_state: Runner,
+
+ /// The transport id this region is responsible for.
+ transport_id: UnderlayTransportId,
+
+ /// The task runner handling the transport for this region.
+ task: ActorRef<ErasedTask>,
+
+ home_derp_tx: watch::Sender<bool>,
+
+ env: Env,
+}
+
+impl Uniderp {
+ pub fn name(region_id: RegionId) -> SmolStr {
+ smol_str::format_smolstr!("derp_region:{region_id}")
+ }
+}
+
+#[kameo::messages]
+impl Uniderp {
+ #[message]
+ pub fn transport_id(&self) -> Option<UnderlayTransportId> {
+ Some(self.transport_id)
+ }
+}
+
+async fn start_runner(mut runner: Runner) {
+ if let Err(e) = runner.run().await {
+ tracing::error!(error = %e, region_id = %runner.region_id, "running derp client");
+ }
+}
+
+impl kameo::Actor for Uniderp {
+ type Args = Args;
+ type Error = crate::Error;
+
+ async fn on_start(args: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let (home_derp_tx, home_derp_rx) = watch::channel(false);
+
+ let (transport_id, from_dataplane, to_dataplane) = args
+ .env
+ .ask::<DataplaneActor, _>(Option::<SmolStr>::None, NewUnderlayTransport, true)
+ .await?;
+
+ args.env
+ .ask::<Multiderp, _>(
+ None,
+ SetRegionTransportId(args.region_id, Some(transport_id)),
+ true,
+ )
+ .await?;
+
+ let runner = Runner {
+ region_id: args.region_id,
+ region: args.region,
+ transport_id,
+ peer_db: Arc::new(RwLock::new(None)),
+ home_derp_rx,
+ to_dataplane,
+ keys: args.env.keys.node_keys.clone(),
+ from_dataplane: Arc::new(Mutex::new(from_dataplane)),
+ };
+
+ let task = Task::spawn_link(&slf, start_runner(runner.clone()).boxed()).await;
+
+ args.env
+ .subscribe::<Arc<ts_control::StateUpdate>>(&slf)
+ .await?;
+ args.env.subscribe::<Arc<PeerState>>(&slf).await?;
+ args.env.subscribe::<DerpLatencyMeasurement>(&slf).await?;
+
+ args.env
+ .register(Some(Self::name(args.region_id)), &slf)
+ .await?;
+
+ Ok(Self {
+ runner_state: runner,
+ transport_id,
+ home_derp_tx,
+ task,
+ env: args.env,
+ })
+ }
+
+ async fn on_stop(
+ &mut self,
+ _: WeakActorRef<Self>,
+ _: ActorStopReason,
+ ) -> Result<(), Self::Error> {
+ self.env
+ .tell::<Multiderp, _>(
+ None,
+ SetRegionTransportId(self.runner_state.region_id, None),
+ )
+ .await?;
+
+ Ok(())
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for Uniderp {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: Arc<ts_control::StateUpdate>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(derp) = &msg.derp else {
+ return;
+ };
+
+ let Some(region) = derp.get(&self.runner_state.region_id) else {
+ tracing::debug!(
+ region_id = ?self.runner_state.region_id,
+ "derp region disappeared from map, stopping"
+ );
+ ctx.stop();
+ return;
+ };
+
+ if &self.runner_state.region == region {
+ return;
+ }
+
+ tracing::debug!(id = %self.runner_state.region_id, "region changed, restarting task");
+ self.runner_state.region = region.clone();
+
+ self.task.unlink(ctx.actor_ref()).await;
+ self.task.stop_gracefully().await.unwrap();
+ self.task
+ .wait_for_shutdown_with_result(|e| {
+ if let Err(e) = e {
+ tracing::error!(error = ?e, "shutting down derp task on region update");
+ }
+ })
+ .await;
+
+ self.task = Task::spawn_link(
+ ctx.actor_ref(),
+ start_runner(self.runner_state.clone()).boxed(),
+ )
+ .await;
+ }
+}
+
+impl Message<Arc<PeerState>> for Uniderp {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: Arc<PeerState>, _ctx: &mut Context<Self, Self::Reply>) {
+ let mut db = self.runner_state.peer_db.write().unwrap();
+ *db = Some(msg.peers.clone());
+ }
+}
+
+impl Message<DerpLatencyMeasurement> for Uniderp {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: DerpLatencyMeasurement, _ctx: &mut Context<Self, Self::Reply>) {
+ let Some(result) = msg.measurement.as_ref().first() else {
+ tracing::trace!("received home derp measurement message but none was set");
+ return;
+ };
+
+ self.home_derp_tx.send_if_modified(|x| {
+ let new_val = result.id == self.runner_state.region_id;
+ let changed = new_val != *x;
+ *x = new_val;
+
+ changed
+ });
+ }
+}
+
+#[derive(Clone)]
+struct Runner {
+ region_id: RegionId,
+ region: DerpRegion,
+ transport_id: UnderlayTransportId,
+ home_derp_rx: watch::Receiver<bool>,
+ to_dataplane: Tx<FromUnderlay>,
+ from_dataplane: Arc<Mutex<Rx<ToUnderlay>>>,
+ peer_db: Arc<RwLock<Option<Arc<PeerDb>>>>,
+ keys: NodeKeyPair,
+}
+
+impl Runner {
+ const INACTIVITY_TIMEOUT: Duration = Duration::from_secs(10);
+
+ #[tracing::instrument(skip_all, fields(region_id = %self.region_id))]
+ async fn run(&mut self) -> Result<(), ts_derp::Error> {
+ loop {
+ let pending = self.wait_for_activity().await;
+ let transport = self.connect(pending).await?;
+ self.run_transport(transport).await?;
+ }
+ }
+
+ #[tracing::instrument(skip_all, level = "trace")]
+ async fn wait_for_activity(&mut self) -> Option<(DynEndpoint, Vec<PacketMut>)> {
+ tracing::trace!("waiting for packet activity or for this to become home derp");
+
+ let mut from_dataplane = self.from_dataplane.lock().await;
+
+ while !*self.home_derp_rx.borrow_and_update() {
+ tokio::select! {
+ _ = self.home_derp_rx.changed() => {
+ tracing::trace!(is_home_derp = *self.home_derp_rx.borrow());
+ },
+
+ from_net = from_dataplane.recv() => {
+ tracing::trace!("received packet to send");
+ return from_net;
+ }
+ }
+ }
+
+ None
+ }
+
+ #[tracing::instrument(skip_all, level = "trace")]
+ async fn connect(
+ &self,
+ pending: Option<(DynEndpoint, Vec<PacketMut>)>,
+ ) -> Result<impl UnderlayTransport<Error = ts_derp::Error> + 'static, ts_derp::Error> {
+ tracing::trace!("establishing derp connection");
+
+ let transport = ts_derp::DefaultClient::connect(&self.region.servers, &self.keys).await?;
+
+ if let Some((info, pending)) = pending {
+ tracing::trace!("sending queued packet");
+ transport.send([(info, pending)]).await?;
+ }
+
+ Ok(transport)
+ }
+
+ #[tracing::instrument(skip_all, fields(transport_id = ?self.transport_id), level = "trace")]
+ async fn run_transport(
+ &mut self,
+ transport: impl UnderlayTransport<Error = ts_derp::Error>,
+ ) -> Result<(), ts_derp::Error> {
+ let mut last_activity = Instant::now();
+ let mut from_dataplane = self.from_dataplane.lock().await;
+
+ loop {
+ let span = tracing::trace_span!("derp_loop");
+
+ let inactivity_timeout =
+ (!*self.home_derp_rx.borrow()).then(|| last_activity + Self::INACTIVITY_TIMEOUT);
+
+ tokio::select! {
+ from_derp = transport.recv() => {
+ last_activity = Instant::now();
+
+ for ret in from_derp.batch_iter() {
+ let (ep, pkts) = ret?;
+ let pkts = pkts.into_iter().collect::<Vec<_>>();
+
+ tracing::trace!(parent: &span, ?ep, len = pkts.len(), "packets from derp server");
+
+ let Ok(()) = self.to_dataplane.send((self.transport_id, ep, pkts)) else {
+ tracing::error!(parent: &span, "underlay receive channel closed");
+ break;
+ };
+ }
+ },
+
+ from_net = from_dataplane.recv() => {
+ last_activity = Instant::now();
+
+ let Some((ep, pkts)) = from_net else {
+ tracing::warn!(parent: &span, "transport queue closed");
+ return Ok(());
+ };
+
+ tracing::trace!(parent: &span, ?ep, packets = pkts.len(), "packets to derp server");
+
+ transport.send([(ep, pkts)]).await?;
+ },
+
+ _ = option_timeout(inactivity_timeout) => {
+ if !*self.home_derp_rx.borrow_and_update() {
+ tracing::trace!(parent: &span, "timed out and not home derp, closing derp conn");
+ return Ok(());
+ }
+ },
+
+ _ = self.home_derp_rx.changed() => {
+ tracing::trace!(is_home_derp = *self.home_derp_rx.borrow());
+ },
+ }
+ }
+ }
+}
+
+async fn option_timeout(duration: Option<Instant>) {
+ match duration {
+ Some(dur) => tokio::time::sleep_until(dur.into()).await,
+ None => core::future::pending().await,
+ }
+}
diff --git a/deps/ts_runtime/src/netmon.rs b/deps/ts_runtime/src/netmon.rs
new file mode 100644
index 00000000..51df9378
--- /dev/null
+++ b/deps/ts_runtime/src/netmon.rs
@@ -0,0 +1,358 @@
+use std::{
+ collections::HashMap,
+ io,
+ sync::{Arc, atomic::AtomicUsize},
+ time::Duration,
+};
+
+use futures::StreamExt;
+use kameo::{
+ Actor,
+ actor::ActorRef,
+ message::{Context, Message, StreamMessage},
+};
+use smol_str::ToSmolStr;
+use ts_netmon::{Event, Family, InterfaceId, Netmon};
+
+use crate::env::Env;
+
+pub struct NetmonActor {
+ mon: Arc<dyn Netmon>,
+ state: State,
+ env: Env,
+ _id: Id,
+}
+
+/// Map from the unique part of a route to its metric.
+pub type Routes = HashMap<ts_netmon::RouteUnique, usize>;
+
+/// The unique id of a [`NetmonActor`] instance.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Id {
+ pub ty: ts_netmon::MonType,
+ pub id: usize,
+}
+
+impl Id {
+ fn new(ty: ts_netmon::MonType) -> Self {
+ static ID: AtomicUsize = AtomicUsize::new(0);
+
+ Self {
+ ty,
+ id: ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
+ }
+ }
+}
+
+/// Netmon state at a point in time.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct State {
+ /// The id of the [`NetmonActor`] that generated this [`State`].
+ pub id: Id,
+
+ /// Interfaces detected on a [`Netmon`].
+ pub interfaces: HashMap<InterfaceId, ts_netmon::Interface>,
+
+ /// Routes per interface.
+ pub routes: HashMap<InterfaceId, Routes>,
+
+ /// Addresses per interface.
+ ///
+ /// To recover the full address with the prefix len, use key's `addr()` as the address
+ /// and the value as the prefix len.
+ ///
+ /// The representation is split this way to support [`Netmon::interface_unique_addrs`].
+ pub addrs: HashMap<InterfaceId, HashMap<ipnet::IpNet, u8>>,
+
+ /// The interface that has the IPv4 default route, if any.
+ pub default_route_interface_v4: Option<InterfaceId>,
+
+ /// The interface that has the IPv6 default route, if any.
+ pub default_route_interface_v6: Option<InterfaceId>,
+}
+
+impl State {
+ /// Iterate the addresses on all interfaces in the `up` state.
+ pub fn up_addrs(&self) -> impl Iterator<Item = (InterfaceId, ipnet::IpNet)> {
+ self.addrs
+ .iter()
+ .filter_map(|(id, addrs)| {
+ let interface = self.interfaces.get(id)?;
+ if !interface.up {
+ return None;
+ }
+
+ Some(
+ addrs
+ .iter()
+ .map(|(ipn, &pfx)| (id.clone(), ipnet::IpNet::new_assert(ipn.addr(), pfx))),
+ )
+ })
+ .flatten()
+ }
+
+ /// Iterate the routes on all interfaces in the `up` state.
+ ///
+ /// Like [`routes`][State::routes], the final element of the tuple is the full per-route
+ /// metric including the interface metric (if present).
+ #[expect(dead_code)]
+ pub fn up_routes(&self) -> impl Iterator<Item = (InterfaceId, ts_netmon::Route, usize)> {
+ self.routes()
+ .filter(|(iid, ..)| self.interfaces.get(iid).is_some_and(|iface| iface.up))
+ }
+
+ /// Iterate all known routes.
+ ///
+ /// The final element of the tuple is the full per-route metric including the interface
+ /// metric (if there is one).
+ ///
+ /// Includes routes on down interfaces.
+ #[expect(dead_code)]
+ pub fn routes(&self) -> impl Iterator<Item = (InterfaceId, ts_netmon::Route, usize)> {
+ self.routes
+ .iter()
+ .filter_map(|(id, routes)| {
+ let interface = self.interfaces.get(id)?;
+
+ Some(routes.iter().map(|((dst, gws), &metric)| {
+ (
+ id.clone(),
+ ts_netmon::Route {
+ gateway: gws.clone(),
+ metric,
+ dst: *dst,
+ },
+ metric
+ + if dst.addr().is_ipv4() {
+ interface.metric_v4
+ } else {
+ interface.metric_v6
+ },
+ )
+ }))
+ })
+ .flatten()
+ }
+}
+
+impl Actor for NetmonActor {
+ type Args = (Env, Arc<dyn Netmon>);
+ type Error = io::Error;
+
+ async fn on_start((env, mon): Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ let id = Id::new(mon.ty());
+
+ slf.attach_stream(
+ {
+ use tokio_stream::StreamExt;
+
+ mon.with_default_route_events()?
+ .chunks_timeout(256, Duration::from_millis(100))
+ .boxed()
+ },
+ (),
+ (),
+ );
+
+ env.register(Some(mon.ty().as_ref().to_smolstr()), &slf)
+ .await
+ .unwrap();
+
+ Ok(Self {
+ env,
+ mon,
+ state: State {
+ id: id.clone(),
+ addrs: Default::default(),
+ routes: Default::default(),
+ interfaces: Default::default(),
+ default_route_interface_v4: None,
+ default_route_interface_v6: None,
+ },
+ _id: id,
+ })
+ }
+}
+
+impl Message<StreamMessage<Vec<io::Result<Event>>, (), ()>> for NetmonActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: StreamMessage<Vec<io::Result<Event>>, (), ()>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let events = match msg {
+ StreamMessage::Started(_) | StreamMessage::Finished(_) => {
+ return;
+ }
+ StreamMessage::Next(val) => val,
+ };
+
+ if events.is_empty() {
+ tracing::warn!("empty netmon event");
+ return;
+ }
+
+ let mut modified = false;
+
+ for event in &events {
+ let event_modified = self.handle_event(event);
+ if event_modified {
+ tracing::trace!(mutating_netmon_event = ?event);
+ }
+
+ modified = modified || event_modified;
+ }
+
+ if !modified {
+ return;
+ }
+
+ tracing::debug!(n_coalesced_events = events.len(), "netmon mutation");
+
+ self.env
+ .publish(Arc::new(self.state.clone()))
+ .await
+ .unwrap();
+ }
+}
+
+impl NetmonActor {
+ fn handle_event(&mut self, event: &io::Result<Event>) -> bool {
+ let event = match event {
+ Ok(event) => event,
+ Err(e) => {
+ tracing::error!(error = %e, "netmon error");
+ return false;
+ }
+ };
+
+ let mut modified = false;
+
+ match &event {
+ Event::RouteUpsert(interface, route) => {
+ let old = self
+ .state
+ .routes
+ .entry(interface.clone())
+ .or_default()
+ .insert(route.unique(), route.metric);
+
+ modified = old.is_none_or(|old| old != route.metric);
+ }
+ Event::RouteRemoved(interface, route) => {
+ let mut empty = false;
+
+ self.state.routes.entry(interface.clone()).and_modify(|r| {
+ modified = r.remove(&route.unique()).is_some();
+ empty = r.is_empty();
+ });
+
+ if empty {
+ self.state.routes.remove(interface);
+ }
+ }
+ Event::AddrUpsert(interface, addr) => {
+ let (addr, pfx_len) = self.split_interface_addr(addr);
+
+ let old = self
+ .state
+ .addrs
+ .entry(interface.clone())
+ .or_default()
+ .insert(addr, pfx_len);
+
+ modified = old.is_none_or(|old| old != pfx_len);
+ }
+ Event::AddrRemoved(interface, addr) => {
+ let mut empty = false;
+ let (addr, _pfx_len) = self.split_interface_addr(addr);
+
+ self.state.addrs.entry(interface.clone()).and_modify(|e| {
+ modified = e.remove(&addr).is_some();
+ empty = e.is_empty();
+ });
+
+ if empty {
+ self.state.addrs.remove(interface);
+ }
+ }
+ Event::InterfaceUpsert(interface) => {
+ let old = self
+ .state
+ .interfaces
+ .insert(interface.id.clone(), interface.clone());
+
+ modified = old.is_none_or(|old| &old != interface);
+ }
+ Event::InterfaceRemoved(interface) => {
+ self.state.interfaces.retain(|x, _| {
+ let ret = x != interface;
+ if !ret {
+ modified = true;
+ }
+
+ ret
+ });
+
+ // NOTE(npry): the below modification of routes and addrs on link deletion is due to
+ // divergence in platform behavior:
+ //
+ // - rtnetlink has a strongly-consistent event ordering model (as it's a single
+ // reliable socket) that orders route and address deletion events before link
+ // deletions, but it doesn't seem to _guarantee_ individual deletions for all
+ // routes and addresses related to a link when it's deleted; the link deletion
+ // itself seems to be intended to clean those up.
+ // - Win32 notify APIs are, on the other hand, free to race between route, address,
+ // and link modifications, but are ordered within each logical resource stream and
+ // appear to exhaustively issue deletion events for all routes and addresses _at
+ // some point_ when a link is deleted. The link deletion event, however, may issue
+ // in an arbitrary order wrt. the other events. The implication of this is that we
+ // technically shouldn't proactively delete the other resources when the link is
+ // removed, since we could be deleting routes and addresses related to a future
+ // generation of the link in a race condition.
+ //
+ // This logic is meant to rectify that and make our state reflect what the platform
+ // has told us it should be regardless of the underlying consistency model.
+ if self.mon.strong_delete_consistency() {
+ modified = modified || self.state.routes.remove(interface).is_some();
+ modified = modified || self.state.addrs.remove(interface).is_some();
+ }
+ }
+ Event::DefaultRouteInterface(iface, family) => {
+ let pre_v4 = self.state.default_route_interface_v4.clone();
+ let pre_v6 = self.state.default_route_interface_v6.clone();
+
+ match family {
+ Family::Ipv4 => self.state.default_route_interface_v4 = iface.clone(),
+ Family::Ipv6 => self.state.default_route_interface_v6 = iface.clone(),
+ }
+
+ if pre_v4 != self.state.default_route_interface_v4
+ || pre_v6 != self.state.default_route_interface_v6
+ {
+ modified = true;
+ }
+ }
+ }
+
+ if !modified {
+ tracing::trace!(?event, "netmon event with no delta");
+ }
+
+ modified
+ }
+
+ /// Split the interface addr according to [`Netmon::interface_unique_addrs`]:
+ ///
+ /// - If interface addresses are unique, returns (addr/0, prefix_len)
+ /// - If interface addresses are not unique, returns (addr/prefix_len, prefix_len)
+ fn split_interface_addr(&self, addr: &ipnet::IpNet) -> (ipnet::IpNet, u8) {
+ if self.mon.interface_unique_addrs() {
+ (ipnet::IpNet::new_assert(addr.addr(), 0), addr.prefix_len())
+ } else {
+ (*addr, addr.prefix_len())
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/netstack_actor.rs b/deps/ts_runtime/src/netstack_actor.rs
new file mode 100644
index 00000000..9a6dfefc
--- /dev/null
+++ b/deps/ts_runtime/src/netstack_actor.rs
@@ -0,0 +1,127 @@
+use std::sync::Arc;
+
+use kameo::{
+ actor::{ActorRef, Spawn},
+ message::{Context, Message},
+};
+use netstack::{
+ HasChannel,
+ netcore::{Channel, NetstackControl},
+};
+use tokio::sync::Mutex;
+use ts_dataplane::async_tokio::{FromOverlay, Rx, ToOverlay, Tx};
+use ts_packet::PacketMut;
+use ts_transport::OverlayTransportId;
+
+use crate::{Error, env::Env, task::Task};
+
+pub struct NetstackActor {
+ channel: Channel,
+}
+
+impl kameo::Actor for NetstackActor {
+ type Args = (
+ Env,
+ netstack::netcore::Config,
+ OverlayTransportId,
+ Tx<FromOverlay>,
+ Arc<Mutex<Rx<ToOverlay>>>,
+ );
+ type Error = Error;
+
+ async fn on_start(
+ (env, config, id, netstack_up, netstack_down): Self::Args,
+ slf: ActorRef<Self>,
+ ) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+
+ let (
+ mut netstack,
+ netstack::WakingPipe {
+ rx: mut netstack_down_rx,
+ tx: netstack_down_tx,
+ },
+ ) = netstack::piped(config);
+ let channel = netstack.command_channel();
+
+ Task::spawn_link(&slf, async move {
+ netstack.run_tokio().await;
+ })
+ .await;
+
+ Task::spawn_link(&slf, async move {
+ while let Some(buf) = netstack_down_rx.recv_async().await {
+ if netstack_up.send((id, vec![buf.to_vec().into()])).is_err() {
+ break;
+ }
+ }
+
+ tracing::warn!("netstack downlink shut down!");
+ })
+ .await;
+
+ Task::spawn_link(&slf, async move {
+ // NetstackActor is supervised, so the same netstack down channel may be handed to
+ // another actor instance if this one panics. We hold this mutex open indefinitely,
+ // relying on the Drop of this future to clean it up when the owning joinset drops.
+ let mut netstack_down = netstack_down.lock().await;
+
+ while let Some(bufs) = netstack_down.recv().await {
+ for buf in bufs {
+ let buf: PacketMut = buf;
+ netstack_down_tx.send_async(buf.as_ref()).await;
+ }
+ }
+
+ tracing::warn!("netstack uplink shut down!");
+ })
+ .await;
+
+ env.register(None, &slf).await?;
+
+ Ok(Self { channel })
+ }
+}
+
+/// Get a netstack command channel.
+#[derive(Debug, Copy, Clone)]
+pub struct GetChannel;
+
+impl Message<GetChannel> for NetstackActor {
+ type Reply = (Channel,);
+
+ async fn handle(
+ &mut self,
+ _: GetChannel,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ (self.channel.clone(),)
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for NetstackActor {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(self_node) = &msg.node else {
+ return;
+ };
+
+ tracing::debug!(new_tailnet_ips = ?self_node.tailnet_address);
+
+ if let Err(e) = self
+ .channel
+ .set_ips([
+ self_node.tailnet_address.ipv4.addr().into(),
+ self_node.tailnet_address.ipv6.addr().into(),
+ ])
+ .await
+ {
+ tracing::error!(error = %e, "setting netstack ips");
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/packetfilter.rs b/deps/ts_runtime/src/packetfilter.rs
new file mode 100644
index 00000000..c7212ea1
--- /dev/null
+++ b/deps/ts_runtime/src/packetfilter.rs
@@ -0,0 +1,60 @@
+use std::sync::Arc;
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+
+use crate::{Error, env::Env};
+
+pub struct PacketfilterUpdater {
+ env: Env,
+ pf_state: ts_packetfilter::CheckingFilter<
+ ts_packetfilter::HashbrownFilter,
+ ts_bart_packetfilter::BartFilter,
+ >,
+}
+
+#[derive(Clone)]
+pub struct PacketFilterState(pub Arc<dyn ts_packetfilter::Filter + Send + Sync>);
+
+impl kameo::Actor for PacketfilterUpdater {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ env,
+ pf_state: Default::default(),
+ })
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for PacketfilterUpdater {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ state_update: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some((pf_ruleset, pf_map)) = &state_update.packetfilter else {
+ return;
+ };
+
+ ts_packetfilter_state::apply_update(&mut self.pf_state, pf_ruleset.clone(), pf_map);
+
+ tracing::trace!(updated_packet_filter = ?self.pf_state.0);
+
+ if let Err(e) = self
+ .env
+ .publish(PacketFilterState(Arc::new(self.pf_state.clone())))
+ .await
+ {
+ tracing::error!(error = %e, "publishing packet filter state");
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/path_discoverer.rs b/deps/ts_runtime/src/path_discoverer.rs
new file mode 100644
index 00000000..e897422b
--- /dev/null
+++ b/deps/ts_runtime/src/path_discoverer.rs
@@ -0,0 +1,585 @@
+//! Peer path discovery agent.
+//!
+//! [`PathDiscoverer`] is the top-level supervisor responsible for creating [`PeerPd`] actors in
+//! response to [`ActivePeers`] updates (one [`PeerPd`] per peer we are trying to perform path
+//! discovery for). As a reminder, [`ActivePeers`] is the set of peers (tracked and published by the
+//! dataplane) that have recent _outgoing_ traffic (this node is trying to send packets to the
+//! peer). Peers not part of the [`ActivePeers`] set have no retained path information.
+//!
+//! [`PeerPd`]'s job is to generate disco traffic to attempt to reach the peer through an avenue
+//! other than DERP. Currently, this means probing for UDP connectivity by measuring RTT of a disco
+//! ping/pong. A `CallMeMaybe` message is sent to the peer before pings go out to attempt to get it
+//! to open a reverse path. Once a path is found, it's published to the bus and interested actors
+//! may consume it -- most importantly, the route updater will update the underlay route table to
+//! use the discovered path, meaning that dataplane traffic will begin using it. If more than one
+//! path is found, the path with the lowest RTT latency is selected and published as the best path
+//! for this peer.
+//!
+//! [`PeerPd`]s attempt to perform path discovery immediately on startup and on a repeating time
+//! interval thereafter. Currently, the timeout period for ping responses _is_ this interval
+//! (`PROBE_PERIOD`). After each interval, the in-flight state is wiped and a new set of
+//! `CallMeMaybe` and `Ping` messages go out; hence, if pongs take more time than the interval to
+//! come back, the [`PeerPd`] will never record them.
+
+use std::{
+ collections::{HashMap, HashSet},
+ sync::Arc,
+ time::{Duration, Instant},
+};
+
+use kameo::{
+ actor::{ActorRef, Spawn, WeakActorRef},
+ error::ActorStopReason,
+ message::{Context, Message},
+ supervision::RestartPolicy,
+};
+use kameo_actors::scheduler::SetInterval;
+use smol_str::SmolStr;
+use tokio::time::MissedTickBehavior;
+use ts_dataplane::async_tokio::ActivePeers;
+use ts_disco_protocol::{CallMeMaybe, Ping, Pong};
+use ts_keys::DiscoPublicKey;
+use ts_transport::{DynEndpoint, PeerId, UnderlayTransportId};
+
+use crate::{
+ Error,
+ dataplane::IncomingDiscoMsg,
+ direct::NewEndpoints,
+ disco::{Disco, SendDisco},
+ env::Env,
+ peer_tracker::PeerState,
+};
+
+/// Top-level actor responsible for coordinating path discovery for peers.
+///
+/// Spawns per-peer child actors to handle discovery, then aggregates and debounces results.
+pub struct PathDiscoverer {
+ active_peers: ActivePeers,
+ best_paths: BestPathMap,
+ env: Env,
+}
+
+pub type BestPathMap = HashMap<PeerId, (UnderlayTransportId, DynEndpoint)>;
+
+#[derive(Clone)]
+pub struct BestPaths(pub Arc<BestPathMap>);
+
+#[derive(Clone)]
+struct PeerBestPath {
+ peer: PeerId,
+ ep: Option<(UnderlayTransportId, DynEndpoint)>,
+}
+
+impl kameo::Actor for PathDiscoverer {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Env, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<ActivePeers>(&slf).await?;
+ env.subscribe::<PeerBestPath>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ env,
+ best_paths: Default::default(),
+ active_peers: Default::default(),
+ })
+ }
+
+ async fn on_stop(
+ &mut self,
+ _: WeakActorRef<Self>,
+ _: ActorStopReason,
+ ) -> Result<(), Self::Error> {
+ self.best_paths.clear();
+ self.publish_best_paths().await;
+
+ Ok(())
+ }
+}
+
+impl PathDiscoverer {
+ async fn publish_best_paths(&self) {
+ self.env
+ .publish(BestPaths(Arc::new(self.best_paths.clone())))
+ .await
+ .unwrap();
+ }
+}
+
+impl Message<ActivePeers> for PathDiscoverer {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: ActivePeers, ctx: &mut Context<Self, Self::Reply>) {
+ for &to_create in msg.difference(&self.active_peers) {
+ tracing::trace!(peer_id = ?to_create, "spawning actor for active peer");
+
+ let aref = PeerPd::supervise(ctx.actor_ref(), (self.env.clone(), to_create))
+ .restart_policy(RestartPolicy::Transient)
+ .spawn()
+ .await;
+
+ aref.wait_for_startup_result().await.unwrap();
+ }
+
+ let mut any_deleted = false;
+ for to_delete in self.active_peers.difference(&msg) {
+ // deletions are handled by the PeerPds themselves
+ any_deleted = any_deleted || self.best_paths.remove(to_delete).is_some();
+ }
+
+ if any_deleted {
+ self.publish_best_paths().await;
+ }
+
+ self.active_peers = msg;
+ }
+}
+
+impl Message<PeerBestPath> for PathDiscoverer {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ PeerBestPath { peer, ep }: PeerBestPath,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let changed = match ep {
+ Some(ep) => {
+ let ret = self.best_paths.insert(peer, ep.clone());
+ ret != Some(ep)
+ }
+ None => self.best_paths.remove(&peer).is_some(),
+ };
+
+ if !changed {
+ return;
+ }
+
+ self.publish_best_paths().await;
+ }
+}
+
+type TxId = [u8; 12];
+
+const PROBE_PERIOD: Duration = Duration::from_millis(5_000);
+
+/// Duration after which peers' "seen endpoints" are cleaned up if they haven't sent traffic.
+const SEEN_ENDPOINTS_TIMEOUT: Duration = Duration::from_millis(30_000);
+
+enum ProbeState {
+ /// This probe is in-flight with the given transaction id. It was sent at `sent`.
+ InFlight { transaction: TxId, sent: Instant },
+ /// This probe succeeded, measuring the indicated `latency`.
+ Success {
+ /// The round-trip latency measured by this probe.
+ latency: Duration,
+ /// The transport used to perform this probe.
+ transport: UnderlayTransportId,
+ },
+}
+
+/// Single-peer path discovery agent.
+struct PeerPd {
+ peer_id: PeerId,
+ disco_key: Option<DiscoPublicKey>,
+
+ /// Indicates whether we've had enough information to send probes yet.
+ ///
+ /// If not, we try to immediately send a probe whenever we get new information that might make
+ /// this possible.
+ ///
+ /// This is used as a heuristic improvement to reduce the delay to first probe in the common
+ /// case. When [`PeerPd`] spawns, there is _usually_ enough info in the system (in retained bus
+ /// messages) to immediately start sending probes for the peer. But since bus messages are sent
+ /// asynchronously and with no guaranteed order, we will need to wait briefly (milliseconds at
+ /// most in a normal system) for all of them to be delivered/until we have enough local info to
+ /// actually send the probes. The message handlers for [`PeerState`] and [`NewEndpoints`] thus
+ /// _try_ to call [`PeerPd::start_probes`] only if this field is `false`; that function
+ /// evaluates whether it has enough info to actually send the probes, and if so, sets this field
+ /// to `true` (preventing anything other than the scheduled probe intervals from triggering
+ /// probes).
+ started_probing: bool,
+
+ /// Records for current-generation probes (in-flight, or newly succeeded).
+ current_probes: HashMap<DynEndpoint, ProbeState>,
+ /// Lookup from transaction id to the endpoint to which it corresponds.
+ ///
+ /// It is expected that we may get incoming pong traffic from endpoints not in this map, e.g.
+ /// from timed out probes.
+ tx_lookup: HashMap<TxId, DynEndpoint>,
+ /// Map holding the previous probe latencies for each endpoint (i.e. from the last probe
+ /// generation). This is used to decide a latency while the next probe is in-flight.
+ previous_probe_latencies: HashMap<(UnderlayTransportId, DynEndpoint), Duration>,
+
+ this_node_endpoints: Arc<[ts_control::Endpoint]>,
+
+ /// Peer's endpoints learned from control which can be used for disco coordination, i.e. which
+ /// can be used to send `CallMeMaybe` messages.
+ ///
+ /// This field is part of a pair with `endpoints_ctrl` -- the two fields are partitioned out of
+ /// the peer's endpoints (reported from control) on the basis of their answer to
+ /// [`Endpoint::is_disco_coordinator`][ts_transport::Endpoint::is_disco_coordinator]. This field
+ /// has those which respond `true`.
+ coord_endpoints_ctrl: HashSet<DynEndpoint>,
+
+ /// Endpoints for this peer derived from the most recent netmap.
+ /// Excludes endpoints in [`PeerPd::disco_endpoints_ctrl`].
+ endpoints_ctrl: HashSet<DynEndpoint>,
+
+ /// Endpoints for this peer derived from the most recent
+ /// [`CallMeMaybe`][ts_disco_protocol::CallMeMaybe] disco message.
+ endpoints_cmm: HashSet<DynEndpoint>,
+
+ /// Endpoints for which we've seen disco traffic arrive from this peer, mapped to the instant at
+ /// which we most recently saw that traffic.
+ ///
+ /// We always try to reach out back to the peer over these endpoints, even if they're not
+ /// sent by control or in a `CallMeMaybe`.
+ seen_peer_endpoints: HashMap<DynEndpoint, Instant>,
+
+ env: Env,
+}
+
+impl PeerPd {
+ pub fn actor_name(peer_id: PeerId) -> SmolStr {
+ smol_str::format_smolstr!("peerpd_{}", peer_id.0)
+ }
+
+ async fn start_probes(&mut self) {
+ let Some(disco_key) = self.disco_key else {
+ tracing::trace!("no disco key known for peer, can't send pings");
+ return;
+ };
+
+ if self.coord_endpoints_ctrl.is_empty() {
+ tracing::trace!("no disco coordinator endpoints, can't send callmemaybes");
+ return;
+ }
+
+ if self.this_node_endpoints.is_empty() {
+ tracing::trace!("no node endpoints, skip");
+ return;
+ }
+
+ self.started_probing = true;
+
+ self.tx_lookup.clear();
+ self.previous_probe_latencies.clear();
+
+ let latencies_was_empty = self.previous_probe_latencies.is_empty();
+
+ for (ep, state) in self.current_probes.drain() {
+ let ProbeState::Success { latency, transport } = state else {
+ tracing::trace!(?ep, "probe did not succeed before next start_probe");
+ continue;
+ };
+
+ tracing::trace!(?ep, ?latency, "valid latency");
+ self.previous_probe_latencies
+ .insert((transport, ep), latency);
+ }
+
+ if latencies_was_empty != self.previous_probe_latencies.is_empty() {
+ let best = self
+ .previous_probe_latencies
+ .iter()
+ .min_by_key(|&(_, dur)| dur);
+
+ self.env
+ .publish_noretain(PeerBestPath {
+ peer: self.peer_id,
+ ep: best.map(|((transport, ep), _latency)| (*transport, ep.clone())),
+ })
+ .await
+ .unwrap();
+ }
+
+ // Send `CallMeMaybe`s on all disco coordinator endpoints. Reminder: coord_endpoints_ctrl is
+ // already filtered at reception-time by Endpoint::is_disco_coordinator.
+ for ep in &self.coord_endpoints_ctrl {
+ let cmm_pkt = Disco::mk_pkt::<CallMeMaybe>(
+ CallMeMaybe::size_for_endpoint_count(self.this_node_endpoints.len()),
+ &self.env.keys.disco_keys,
+ &disco_key,
+ |cmm| {
+ for (tgt, ep) in cmm
+ .endpoints
+ .iter_mut()
+ .zip(self.this_node_endpoints.iter())
+ {
+ *tgt = ep.endpoint.into();
+ }
+ },
+ );
+
+ self.env
+ .publish_noretain(SendDisco {
+ ep: ep.clone(),
+ buf: cmm_pkt,
+ })
+ .await
+ .unwrap();
+
+ tracing::trace!(?ep, sent_eps = ?self.this_node_endpoints, "sent callmemaybe");
+ }
+
+ // Send pings for all non-disco-coordinator endpoints.
+ let mut eps = self.seen_peer_endpoints.keys().collect::<HashSet<_>>();
+ eps.extend(&self.endpoints_cmm);
+ eps.extend(&self.endpoints_ctrl);
+
+ for ep in eps {
+ if ep.is_disco_coordinator() {
+ continue;
+ }
+
+ Self::send_disco_ping(
+ &self.env,
+ ep.clone(),
+ disco_key,
+ &mut self.current_probes,
+ &mut self.tx_lookup,
+ )
+ .await;
+ }
+ }
+
+ // this is an associated fn because we need to do a partial reborrow through self
+ #[tracing::instrument(skip_all, fields(?ep, %disco_key, tx_id = tracing::field::Empty))]
+ async fn send_disco_ping(
+ env: &Env,
+ ep: DynEndpoint,
+ disco_key: DiscoPublicKey,
+ probe_state: &mut HashMap<DynEndpoint, ProbeState>,
+ tx_lookup: &mut HashMap<TxId, DynEndpoint>,
+ ) {
+ // TODO(npry): this currently makes the implicit, awkward, and not-generally-correct
+ // assumption that at most one underlay transport id can carry traffic of a given endpoint
+ // type. this isn't a problem currently because derp (the transport that can currently
+ // have multiple transport ids) doesn't exchange ping/pong traffic, and udp only has one
+ // transport id at the moment, so there's no race (currently, the first latency
+ // report for a given endpoint will win). that's fine conceptually -- we want the fastest
+ // endpoint -- but we should be able to measure all paths concurrently/unambiguously.
+ // intend to refactor so that transaction id allocation occurs in the transport --
+ // probably it reports back to us that it's issued a ping (with timestamp + txid), so we
+ // can correlate it to a possible returned pong
+
+ let tx_id = rand::random();
+ tracing::Span::current().record(
+ "tx_id",
+ tracing::field::debug(ts_hexdump::IterFmt::contiguous(&tx_id)),
+ );
+
+ let ping_buf = Disco::mk_pkt::<Ping>(
+ Ping::size_with_padding(0),
+ &env.keys.disco_keys,
+ &disco_key,
+ |msg| {
+ msg.node_key = env.keys.node_keys.public;
+ msg.tx_id = tx_id;
+ },
+ );
+
+ env.publish_noretain(SendDisco {
+ ep: ep.clone(),
+ buf: ping_buf,
+ })
+ .await
+ .unwrap();
+
+ probe_state.insert(
+ ep.clone(),
+ ProbeState::InFlight {
+ sent: Instant::now(),
+ transaction: tx_id,
+ },
+ );
+ tx_lookup.insert(tx_id, ep);
+
+ tracing::trace!(tx_id = ?format_args!("{:x?}", ts_hexdump::IterFmt::contiguous(&tx_id)), "sent disco ping");
+ }
+
+ fn gc_seen_endpoints(&mut self) {
+ let now = Instant::now();
+
+ self.seen_peer_endpoints.retain(|_k, &mut v| {
+ let age = now - v;
+
+ age <= SEEN_ENDPOINTS_TIMEOUT
+ });
+ }
+}
+
+/// Periodic maintenance message triggering probes.
+#[derive(Clone, Copy)]
+struct ScheduleProbe;
+
+impl kameo::Actor for PeerPd {
+ type Args = (Env, PeerId);
+ type Error = Error;
+
+ async fn on_start(
+ (env, peer_id): Self::Args,
+ slf: ActorRef<Self>,
+ ) -> Result<Self, Self::Error> {
+ env.subscribe::<ActivePeers>(&slf).await?;
+ env.subscribe::<IncomingDiscoMsg>(&slf).await?;
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+ env.subscribe::<NewEndpoints>(&slf).await?;
+
+ env.scheduler
+ .ask(
+ SetInterval::new(slf.downgrade(), PROBE_PERIOD, ScheduleProbe)
+ .set_missed_tick_behaviour(MissedTickBehavior::Skip),
+ )
+ .send()
+ .await?;
+
+ env.register(Some(Self::actor_name(peer_id)), &slf).await?;
+
+ Ok(Self {
+ env,
+ peer_id,
+ started_probing: false,
+ previous_probe_latencies: Default::default(),
+ this_node_endpoints: Default::default(),
+ disco_key: Default::default(),
+ tx_lookup: Default::default(),
+ current_probes: Default::default(),
+ endpoints_cmm: Default::default(),
+ coord_endpoints_ctrl: Default::default(),
+ endpoints_ctrl: Default::default(),
+ seen_peer_endpoints: Default::default(),
+ })
+ }
+}
+
+impl Message<ScheduleProbe> for PeerPd {
+ type Reply = ();
+
+ async fn handle(&mut self, _: ScheduleProbe, _: &mut Context<Self, Self::Reply>) {
+ self.gc_seen_endpoints();
+ self.start_probes().await;
+ }
+}
+
+impl Message<Arc<PeerState>> for PeerPd {
+ type Reply = ();
+
+ async fn handle(&mut self, state: Arc<PeerState>, _: &mut Context<Self, Self::Reply>) {
+ // if this peer was deleted from the netmap, let removal from ActivePeers handle killing
+ // this actor, otherwise control flow to PathDiscoverer may get confused. we can wipe out
+ // our endpoints and stop doing anything because that doesn't interfere.
+
+ if state.deletions.contains(&self.peer_id) {
+ for ep in self
+ .endpoints_ctrl
+ .drain()
+ .chain(self.coord_endpoints_ctrl.drain())
+ {
+ if self.endpoints_cmm.contains(&ep) || self.seen_peer_endpoints.contains_key(&ep) {
+ continue;
+ }
+
+ if let Some(ent) = self.current_probes.remove(&ep)
+ && let ProbeState::InFlight { transaction, .. } = ent
+ {
+ self.tx_lookup.remove(&transaction);
+ }
+ }
+
+ tracing::debug!(peer_id = ?self.peer_id, "peer deletion: state wiped, waiting for disappearance from activepeers");
+
+ return;
+ }
+
+ let Some((_, node)) = state.peers.get(&self.peer_id) else {
+ return;
+ };
+
+ self.disco_key = node.disco_key;
+
+ (self.coord_endpoints_ctrl, self.endpoints_ctrl) = node
+ .underlay_addresses
+ .iter()
+ .copied()
+ .map(DynEndpoint::udp)
+ .chain(core::iter::once(DynEndpoint::derp(node.node_key)))
+ .partition(|x| x.is_disco_coordinator());
+
+ tracing::trace!(peer_id = ?self.peer_id, endpoints_ctrl = ?self.endpoints_ctrl, disco_endpoints_ctrl = ?self.coord_endpoints_ctrl, "endpoint state updated");
+
+ if !self.started_probing {
+ self.start_probes().await;
+ }
+ }
+}
+
+impl Message<NewEndpoints> for PeerPd {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: NewEndpoints, _ctx: &mut Context<Self, Self::Reply>) {
+ self.this_node_endpoints = msg.0;
+
+ if !self.started_probing {
+ self.start_probes().await;
+ }
+ }
+}
+
+impl Message<ActivePeers> for PeerPd {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: ActivePeers, ctx: &mut Context<Self, Self::Reply>) {
+ if !msg.contains(&self.peer_id) {
+ tracing::debug!(peer_id = %self.peer_id, "peer went inactive, stopping path discovery");
+ ctx.stop();
+ }
+ }
+}
+
+impl Message<IncomingDiscoMsg> for PeerPd {
+ type Reply = ();
+
+ #[tracing::instrument(skip_all, fields(sender = ?msg.sender, transport_id = ?msg.transport))]
+ async fn handle(&mut self, msg: IncomingDiscoMsg, _ctx: &mut Context<Self, Self::Reply>) {
+ let pkt = msg.packet.get();
+
+ if Some(pkt.sender_pubkey()) != self.disco_key.as_ref() {
+ return;
+ }
+
+ self.seen_peer_endpoints.insert(msg.sender, Instant::now());
+
+ let Some(pong) = pkt.as_msg::<Pong>() else {
+ return;
+ };
+
+ tracing::trace!("got pong");
+
+ let Some(ep) = self.tx_lookup.remove(&pong.tx_id) else {
+ tracing::trace!(tx_id = ?format_args!("{:x?}", ts_hexdump::IterFmt::contiguous(&pong.tx_id)), "no pong known with this tx id");
+ return;
+ };
+
+ let Some(entry) = self.current_probes.get_mut(&ep) else {
+ // this endpoint must have been removed while the check was in-flight, drop it
+ tracing::trace!(peer_id = ?self.peer_id, ?ep, "drop probe with missing state entry");
+ return;
+ };
+
+ let ProbeState::InFlight { sent, .. } = &entry else {
+ tracing::warn!("got ping response but it wasn't in-flight in endpoint entry");
+ return;
+ };
+
+ let elapsed = Instant::now() - *sent;
+ tracing::trace!(rtt = ?elapsed, endpoint = ?ep, peer_id = %self.peer_id, "measured latency");
+
+ *entry = ProbeState::Success {
+ latency: elapsed,
+ transport: msg.transport,
+ };
+ }
+}
diff --git a/deps/ts_runtime/src/peer_tracker/mod.rs b/deps/ts_runtime/src/peer_tracker/mod.rs
new file mode 100644
index 00000000..2af1624b
--- /dev/null
+++ b/deps/ts_runtime/src/peer_tracker/mod.rs
@@ -0,0 +1,323 @@
+//! Peer delta update tracking.
+
+use std::{collections::HashSet, net::IpAddr, sync::Arc};
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+ reply::ReplySender,
+};
+use ts_control::Node;
+use ts_transport::PeerId;
+
+use crate::{Error, env::Env};
+
+mod peer_db;
+
+pub use peer_db::PeerDb;
+
+/// Actor that tracks peer delta updates and emits new states.
+pub struct PeerTracker {
+ peer_db: PeerDb,
+ seen_state_update: bool,
+ pending_requests: Vec<Pending>,
+ env: Env,
+}
+
+impl PeerTracker {
+ fn peer_by_name_opt(&self, name: &str) -> Option<&Node> {
+ let name = name.trim_end_matches('.');
+ self.peer_db.get(&name).map(|(_id, node)| node)
+ }
+
+ fn peer_by_tailnet_ip_opt(&self, ip: IpAddr) -> Option<&Node> {
+ self.peer_db.get(&ip).map(|(_id, node)| node)
+ }
+}
+
+impl kameo::Actor for PeerTracker {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ peer_db: PeerDb::default(),
+ pending_requests: Default::default(),
+ seen_state_update: false,
+ env,
+ })
+ }
+}
+
+enum Pending {
+ PeerByName(PeerByName, ReplySender<Option<Node>>),
+ AcceptedRoute(PeerByAcceptedRoute, ReplySender<Vec<Node>>),
+ TailnetIp(PeerByTailnetIp, ReplySender<Option<Node>>),
+}
+
+// For messages with arguments, a struct is generated with the args as fields. They aren't
+// documented, and we can't apply attributes directly to the fields. Hence, wrap in a module where
+// docs are turned off everywhere.
+#[allow(missing_docs)]
+mod msg_impl {
+ use std::net::IpAddr;
+
+ use kameo::prelude::DelegatedReply;
+
+ use super::*;
+
+ #[kameo::messages]
+ impl PeerTracker {
+ /// Lookup a peer by name.
+ ///
+ /// Waits until we've received at least one peer update from control.
+ #[message(ctx)]
+ pub async fn peer_by_name(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
+ name: String,
+ ) -> DelegatedReply<Option<Node>> {
+ let (deleg, sender) = ctx.reply_sender();
+ let Some(sender) = sender else { return deleg };
+
+ if !self.seen_state_update {
+ tracing::debug!(query = name, "no peer state seen yet, queueing request");
+
+ self.pending_requests
+ .push(Pending::PeerByName(PeerByName { name }, sender));
+
+ return deleg;
+ }
+
+ sender.send(self.peer_by_name_opt(&name).cloned());
+
+ deleg
+ }
+
+ /// Lookup all peers that accept packets addressed to the given IP.
+ ///
+ /// This includes the peer's tailnet address and any subnet routes it provides. Only
+ /// the peers with the most specific subnet route match that covers `ip` will be
+ /// returned.
+ ///
+ /// E.g., suppose:
+ ///
+ /// - We're querying for `10.1.2.3`
+ /// - `PeerA` and `PeerB` have accepted routes for `10.1.2.0/24`
+ /// - `PeerC` has an accepted route for `10.1.0.0/16`
+ ///
+ /// Only `PeerA` and `PeerB` will be returned, since they have the most specific
+ /// prefix match.
+ #[message(ctx)]
+ pub fn peer_by_accepted_route(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Vec<Node>>>,
+ ip: IpAddr,
+ ) -> DelegatedReply<Vec<Node>> {
+ let (deleg, sender) = ctx.reply_sender();
+ let Some(sender) = sender else { return deleg };
+
+ if !self.seen_state_update {
+ tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
+
+ self.pending_requests
+ .push(Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, sender));
+
+ return deleg;
+ }
+
+ sender.send(
+ self.peer_db
+ .get_route(ip.into())
+ .map(|(_id, node)| node.clone())
+ .collect(),
+ );
+
+ deleg
+ }
+
+ /// Lookup the peer that has the given tailnet IP address.
+ #[message(ctx)]
+ pub fn peer_by_tailnet_ip(
+ &mut self,
+ ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
+ ip: IpAddr,
+ ) -> DelegatedReply<Option<Node>> {
+ let (deleg, sender) = ctx.reply_sender();
+ let Some(sender) = sender else { return deleg };
+
+ if !self.seen_state_update {
+ tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
+
+ self.pending_requests
+ .push(Pending::TailnetIp(PeerByTailnetIp { ip }, sender));
+
+ return deleg;
+ }
+
+ sender.send(self.peer_by_tailnet_ip_opt(ip).cloned());
+
+ deleg
+ }
+
+ /// Every peer we know of, or None before the first netmap. Unlike the lookups, this
+ /// doesn't wait for one.
+ #[message]
+ pub fn all_peers(&mut self) -> Option<Vec<Node>> {
+ self.seen_state_update
+ .then(|| self.peer_db.peers().values().cloned().collect())
+ }
+ }
+}
+
+pub use msg_impl::*;
+
+#[derive(Debug, Clone, Default)]
+pub(crate) struct PeerState {
+ #[allow(unused)]
+ pub deletions: HashSet<PeerId>,
+ #[allow(unused)]
+ pub upserts: HashSet<PeerId>,
+ pub peers: Arc<PeerDb>,
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for PeerTracker {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(peer_update) = &msg.peer_update else {
+ // A whole netmap leaves the peers out when there are none: the tailnet is known to
+ // have none from it. Only a whole one, with the DERP map every first netmap carries:
+ // a tracker restarted mid-stream would take a later update of the node alone for
+ // an empty tailnet, and answer from that until control sent a whole netmap again.
+ if msg.node.is_some() && msg.derp.is_some() {
+ self.service_pending_requests();
+ }
+ return;
+ };
+
+ let mut upserts = HashSet::default();
+ let mut deletions = HashSet::default();
+
+ match peer_update {
+ ts_control::PeerUpdate::Full(new_nodes) => {
+ tracing::trace!("full peer update");
+
+ let new_ids = new_nodes
+ .iter()
+ .map(|x| &x.stable_id)
+ .collect::<HashSet<_>>();
+
+ self.peer_db.retain(|id, peer| {
+ let retain = new_ids.contains(&peer.stable_id);
+
+ if !retain {
+ deletions.insert(id);
+ }
+
+ retain
+ });
+
+ for node in new_nodes {
+ let peer_id = self.peer_db.upsert(node);
+ upserts.insert(peer_id);
+ }
+ }
+
+ ts_control::PeerUpdate::Delta {
+ patch,
+ remove,
+ upsert,
+ } => {
+ tracing::trace!("delta peer update");
+
+ for peer in remove {
+ let Some((id, _node)) = self.peer_db.remove(peer) else {
+ tracing::error!(control_node_id = peer, "removed peer was unknown");
+ continue;
+ };
+
+ deletions.insert(id);
+ }
+
+ for peer in upsert {
+ let id = self.peer_db.upsert(peer);
+
+ upserts.insert(id);
+ }
+
+ for update in patch {
+ if let Some(id) = self.peer_db.patch(update) {
+ upserts.insert(id);
+ } else {
+ tracing::warn!(?update, "no peer for update");
+ }
+ }
+ }
+ }
+
+ tracing::debug!(
+ n_upsert = upserts.len(),
+ n_delete = deletions.len(),
+ peer_count = self.peer_db.peers().len(),
+ "new peer state"
+ );
+
+ self.service_pending_requests();
+
+ if let Err(e) = self
+ .env
+ .publish(Arc::new(PeerState {
+ upserts,
+ deletions,
+ peers: Arc::new(self.peer_db.clone()),
+ }))
+ .await
+ {
+ tracing::error!(error = %e, "publishing peer state update");
+ }
+ }
+}
+
+impl PeerTracker {
+ fn service_pending_requests(&mut self) {
+ if self.seen_state_update {
+ return;
+ }
+
+ self.seen_state_update = true;
+
+ if !self.pending_requests.is_empty() {
+ tracing::debug!(
+ n_pending = self.pending_requests.len(),
+ "state update received, servicing pending requests"
+ );
+ }
+
+ for req in core::mem::take(&mut self.pending_requests) {
+ match req {
+ Pending::PeerByName(PeerByName { name }, reply) => {
+ reply.send(self.peer_by_name_opt(&name).cloned());
+ }
+ Pending::TailnetIp(PeerByTailnetIp { ip }, reply) => {
+ reply.send(self.peer_by_tailnet_ip_opt(ip).cloned());
+ }
+ Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, reply) => {
+ reply.send(
+ self.peer_db
+ .get_route(ip.into())
+ .map(|(_id, node)| node.clone())
+ .collect(),
+ );
+ }
+ }
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/peer_tracker/peer_db.rs b/deps/ts_runtime/src/peer_tracker/peer_db.rs
new file mode 100644
index 00000000..6de8db4b
--- /dev/null
+++ b/deps/ts_runtime/src/peer_tracker/peer_db.rs
@@ -0,0 +1,911 @@
+use std::{
+ collections::HashMap,
+ fmt::{Debug, Formatter},
+ hash::Hash,
+ net::IpAddr,
+};
+
+use ts_bart::{RouteModification, RoutingTable, RoutingTableExt};
+use ts_control::{Node, NodeUpdate, StableNodeId};
+use ts_keys::{DiscoPublicKey, NodePublicKey};
+use ts_transport::PeerId;
+
+mod private {
+ use super::*;
+
+ pub trait Sealed {}
+
+ impl Sealed for PeerId {}
+ impl Sealed for NodePublicKey {}
+ impl Sealed for DiscoPublicKey {}
+ impl Sealed for StableNodeId {}
+ impl Sealed for ts_control::NodeId {}
+ impl Sealed for PeerName {}
+ impl Sealed for &str {}
+ impl Sealed for IpAddr {}
+ impl Sealed for ipnet::IpNet {}
+}
+
+/// A [`Node`] field indexed by [`PeerDb`].
+pub trait IndexedField: Debug + private::Sealed {
+ /// Look up the peer id that has this field.
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId>;
+}
+
+type Index<T> = HashMap<T, PeerId>;
+type PeerName = String;
+
+/// A database that stores a map of peers by [`PeerId`] and multiple indices.
+///
+/// Assumes that _all indexed fields_ are unique per-node, with a few notable exceptions:
+///
+/// - Hostname may be duplicated, though the fqdn (including the tailnet component) may not
+/// be.
+/// - Accepted routes may overlap.
+#[derive(Default, Clone)]
+pub struct PeerDb {
+ peers: HashMap<PeerId, Node>,
+ index_state: IndexState,
+ next_id: u32,
+}
+
+impl Debug for PeerDb {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ self.peers.fmt(f)
+ }
+}
+
+#[derive(Default, Clone)]
+struct IndexState {
+ /// Index on the node's [`NodePublicKey`].
+ nk_idx: Index<NodePublicKey>,
+ /// Index on the [`DiscoPublicKey`], assuming it's known.
+ disco_idx: Index<DiscoPublicKey>,
+ /// Index on the peer [`StableNodeId`].
+ stableid_idx: Index<StableNodeId>,
+ /// Index for the [`ts_control::NodeId`].
+ ///
+ /// This is a numeric ID assigned by control which could overlap across different
+ /// control regions (by contrast to [`StableNodeId`], which should not). We need this
+ /// field because control indicates node patches and deletions by this id rather than
+ /// the stable id.
+ control_idx: Index<ts_control::NodeId>,
+ /// Index on the peer name and FQDN.
+ name_idx: Index<PeerName>,
+ /// Index on the node's tailnet IPv4 and IPv6.
+ ip_idx: ts_bart::Table<PeerId>,
+ /// Index on the node's accepted routes.
+ ///
+ /// These may overlap between nodes, hence this stores a vec of matching node ids for
+ /// each route.
+ route_idx: ts_bart::Table<smallvec::SmallVec<[PeerId; 2]>>,
+}
+
+impl PeerDb {
+ /// Apply an update to a node in the peer db.
+ ///
+ /// The [`NodeUpdate::id`] field is used as the primary key to identify the node.
+ pub fn patch(&mut self, update: &NodeUpdate) -> Option<PeerId> {
+ let id = update.id.lookup(self)?;
+ let mut peer = self.peers.remove(&id).unwrap();
+ peer.apply_update(update);
+ Some(self.upsert(&peer))
+ }
+
+ /// Upsert a node into the peer db.
+ ///
+ /// The [`StableNodeId`] is used as the primary key to identify the node.
+ pub fn upsert(&mut self, new: &Node) -> PeerId {
+ let id = self
+ .index_state
+ .stableid_idx
+ .get(&new.stable_id)
+ .copied()
+ .unwrap_or_else(|| {
+ let id = self.next_id;
+ self.next_id += 1;
+
+ PeerId(id)
+ });
+
+ let old = self.peers.get(&id);
+
+ // no update: same node
+ if old.is_some_and(|x| x == new) {
+ return id;
+ }
+
+ maybe_update_idx(new, old, |x| &x.node_key, &mut self.index_state.nk_idx, id);
+ maybe_update_idx(
+ new,
+ old,
+ |x| &x.stable_id,
+ &mut self.index_state.stableid_idx,
+ id,
+ );
+ maybe_update_idx(new, old, |x| &x.id, &mut self.index_state.control_idx, id);
+
+ maybe_update(
+ new,
+ old,
+ |x| &x.disco_key,
+ &mut self.index_state.disco_idx,
+ |old, idx| {
+ if let Some(key) = &old.disco_key {
+ let old_id = idx.remove(key);
+ assert!(old_id.is_some_and(|old_id| old_id == id));
+ }
+ },
+ |new, idx| {
+ if let Some(key) = &new.disco_key {
+ idx.insert(*key, id);
+ }
+ },
+ );
+
+ // Store both `hostname` and fqdn (no trailing dot) in the `name_idx` index. This _does not_
+ // preserve uniqueness for `hostname`; as documented on external API such as
+ // `tailscale::Device::peer_by_name`, there may be collisions in this field (typically when
+ // nodes are shared into the tailnet with the same name as an existing tailnet device).
+ //
+ // We don't resolve this conflict here and make it the caller's problem to include the fqdn
+ // if there is ambiguity; the index just stores the most recently updated node with a given
+ // hostname.
+ //
+ // Also, this index is overloaded to store both the fqdn and the hostname, but this is
+ // fine since the fqdn always includes `.`, while the hostname never does, so they're always
+ // distinguishable.
+ maybe_update(
+ new,
+ old,
+ |x| (&x.hostname, &x.tailnet),
+ &mut self.index_state.name_idx,
+ |old, idx| {
+ if idx.get(&old.hostname).is_some_and(|&x| x == id) {
+ idx.remove(&old.hostname);
+ }
+
+ if let Some(fqdn) = old.fqdn_opt(false) {
+ let removed_id = idx.remove(&fqdn);
+ assert!(removed_id.is_some_and(|removed_id| removed_id == id));
+ }
+ },
+ |new, idx| {
+ idx.insert(new.hostname.clone(), id);
+
+ if let Some(fqdn) = new.fqdn_opt(false) {
+ idx.insert(fqdn, id);
+ }
+ },
+ );
+
+ maybe_update(
+ new,
+ old,
+ |x| &x.tailnet_address,
+ &mut self.index_state.ip_idx,
+ |old, idx| {
+ let id4 = idx.remove(old.tailnet_address.ipv4.into());
+ let id6 = idx.remove(old.tailnet_address.ipv6.into());
+
+ assert!(id4.is_some_and(|old_id| old_id == id));
+ assert!(id6.is_some_and(|old_id| old_id == id));
+ },
+ |new, idx| {
+ idx.insert(new.tailnet_address.ipv4.into(), id);
+ idx.insert(new.tailnet_address.ipv6.into(), id);
+ },
+ );
+
+ maybe_update(
+ new,
+ old,
+ |x| &x.accepted_routes,
+ &mut self.index_state,
+ |old, idx| {
+ for &route in &old.accepted_routes {
+ idx.remove_route(route, id);
+ }
+ },
+ |new, idx| {
+ for &route in &new.accepted_routes {
+ idx.route_idx.modify(route, |val| {
+ if let Some(val) = val {
+ val.push(id);
+ return RouteModification::Noop;
+ }
+
+ RouteModification::Insert(smallvec::smallvec![id])
+ });
+ }
+ },
+ );
+
+ self.peers.insert(id, new.clone());
+
+ id
+ }
+
+ /// Remove a peer by a given indexed field.
+ pub fn remove(&mut self, field: &dyn IndexedField) -> Option<(PeerId, Node)> {
+ let id = field.lookup(self)?;
+
+ let node = self.peers.remove(&id)?;
+ self.index_state.remove(id, &node);
+
+ Some((id, node))
+ }
+
+ /// Get the node with the given field.
+ pub fn get(&self, field: &dyn IndexedField) -> Option<(PeerId, &Node)> {
+ let id = field.lookup(self)?;
+ let peer = self.peers.get(&id)?;
+
+ Some((id, peer))
+ }
+
+ /// Get the nodes with the closest matching route.
+ pub fn get_route(&self, route: ipnet::IpNet) -> impl Iterator<Item = (PeerId, &Node)> {
+ // this doesn't use IndexedField because more than one result can be returned
+
+ self.index_state
+ .route_idx
+ .lookup_prefix(route)
+ .into_iter()
+ .flat_map(|x| x.iter())
+ .map(|&id| (id, self.peers.get(&id).unwrap()))
+ }
+
+ /// Check whether there is a peer with the given field in the db.
+ pub fn has(&self, field: &dyn IndexedField) -> Option<PeerId> {
+ field.lookup(self)
+ }
+
+ /// Get a reference to the peer map.
+ pub const fn peers(&self) -> &HashMap<PeerId, Node> {
+ &self.peers
+ }
+
+ /// Remove the nodes in the db that don't satisfy the predicate function.
+ pub fn retain(&mut self, mut predicate: impl FnMut(PeerId, &Node) -> bool) {
+ self.peers.retain(|&id, node| {
+ let retain = predicate(id, node);
+
+ if !retain {
+ self.index_state.remove(id, node);
+ }
+
+ retain
+ });
+ }
+}
+
+impl IndexState {
+ fn remove(&mut self, id: PeerId, node: &Node) {
+ self.nk_idx.remove(&node.node_key);
+ self.stableid_idx.remove(&node.stable_id);
+ self.control_idx.remove(&node.id);
+ self.ip_idx.remove(node.tailnet_address.ipv4.into());
+ self.ip_idx.remove(node.tailnet_address.ipv6.into());
+
+ if self.name_idx.get(&node.hostname).is_some_and(|&x| x == id) {
+ self.name_idx.remove(&node.hostname);
+ }
+
+ if let Some(fqdn) = node.fqdn_opt(false) {
+ self.name_idx.remove(&fqdn);
+ }
+
+ for route in &node.accepted_routes {
+ self.remove_route(*route, id);
+ }
+
+ if let Some(disco) = &node.disco_key {
+ self.disco_idx.remove(disco);
+ }
+ }
+
+ /// Remove `route` from the `route_idx`.
+ fn remove_route(&mut self, route: ipnet::IpNet, id: PeerId) {
+ self.route_idx.modify(route, |val| match val {
+ Some(val) => {
+ let mut some_matched = false;
+
+ val.retain(|&mut x| {
+ let ids_match = x == id;
+ if ids_match {
+ some_matched = true;
+ }
+
+ !ids_match
+ });
+
+ assert!(some_matched);
+
+ if val.is_empty() {
+ RouteModification::Remove
+ } else {
+ RouteModification::Noop
+ }
+ }
+ None => RouteModification::Noop,
+ });
+ }
+
+ #[cfg(test)]
+ fn is_empty(&self) -> bool {
+ self.nk_idx.is_empty()
+ && self.stableid_idx.is_empty()
+ && self.control_idx.is_empty()
+ && self.ip_idx.size() == 0
+ && self.name_idx.is_empty()
+ && self.route_idx.size() == 0
+ && self.disco_idx.is_empty()
+ }
+}
+
+/// Attempt to update the `idx` with the `new` node.
+///
+/// The `accessor` selects a set of fields to check (by `PartialEq`) for whether the `new`
+/// node has changed compared to the `old` one:
+///
+/// - If the value returned by `accessor` is the same between `new` and `old`, nothing
+/// happens.
+/// - If the value has changed and `old` is `Some`, `remove(old, idx)` is called.
+/// - If the value has changed, `insert(new, idx)` is called.
+fn maybe_update<'n, T, Idx>(
+ new: &'n Node,
+ old: Option<&'n Node>,
+ accessor: impl Fn(&'n Node) -> T,
+ idx: &mut Idx,
+ mut remove: impl FnMut(&'n Node, &mut Idx),
+ mut insert: impl FnMut(&'n Node, &mut Idx),
+) where
+ T: PartialEq + 'n,
+{
+ match old {
+ Some(old) if accessor(old) == accessor(new) => {
+ return;
+ }
+ Some(x) => {
+ remove(x, idx);
+ }
+ None => {}
+ }
+
+ insert(new, idx)
+}
+
+/// Specialization of [`maybe_update`] to work on [`Index`].
+fn maybe_update_idx<T>(
+ new: &Node,
+ old: Option<&Node>,
+ accessor: impl Fn(&Node) -> &T,
+ idx: &mut Index<T>,
+ new_id: PeerId,
+) where
+ T: Eq + Hash + Clone,
+{
+ maybe_update(
+ new,
+ old,
+ &accessor,
+ idx,
+ |old, idx| {
+ let old_id = idx.remove(accessor(old));
+ assert!(old_id.is_some_and(|old_id| old_id == new_id));
+ },
+ |new, idx| {
+ idx.insert(accessor(new).clone(), new_id);
+ },
+ )
+}
+
+impl IndexedField for PeerId {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ if db.peers.contains_key(self) {
+ Some(*self)
+ } else {
+ None
+ }
+ }
+}
+
+impl IndexedField for NodePublicKey {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.nk_idx.get(self).copied()
+ }
+}
+
+impl IndexedField for DiscoPublicKey {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.disco_idx.get(self).copied()
+ }
+}
+
+impl IndexedField for StableNodeId {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.stableid_idx.get(self).copied()
+ }
+}
+
+impl IndexedField for ts_control::NodeId {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.control_idx.get(self).copied()
+ }
+}
+
+impl IndexedField for PeerName {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.name_idx.get(self).copied()
+ }
+}
+
+impl IndexedField for &str {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.name_idx.get(*self).copied()
+ }
+}
+
+impl IndexedField for IpAddr {
+ fn lookup(&self, db: &PeerDb) -> Option<PeerId> {
+ db.index_state.ip_idx.lookup(*self).copied()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use std::{
+ collections::{BTreeMap, HashMap, HashSet},
+ net::{Ipv4Addr, Ipv6Addr, SocketAddr},
+ num::NonZeroU32,
+ };
+
+ use chrono::DateTime;
+ use proptest::{
+ collection::{hash_set, vec},
+ prelude::{Just, any},
+ strategy::Strategy,
+ };
+ use rand::{
+ RngExt,
+ distr::{Alphanumeric, SampleString},
+ };
+ use ts_capabilityversion::CapabilityVersion;
+ use ts_control::{NodeLastSeen, NodeStatus, TailnetAddress};
+ use ts_keys::MachinePublicKey;
+
+ use super::*;
+
+ fn rand_string(rng: &mut dyn rand::Rng, max_len: usize) -> String {
+ let len = rng.random_range(1..max_len);
+ Alphanumeric.sample_string(rng, len)
+ }
+
+ fn rand_route(rng: &mut dyn rand::Rng) -> ipnet::IpNet {
+ if rng.random::<bool>() {
+ let ip = rand_ipv4(rng);
+ ipnet::Ipv4Net::new(ip, rand::random_range(0..=32))
+ .unwrap()
+ .trunc()
+ .into()
+ } else {
+ let ip = rand_ipv6(rng);
+ ipnet::Ipv6Net::new(ip, rand::random_range(0..=128))
+ .unwrap()
+ .trunc()
+ .into()
+ }
+ }
+
+ fn rand_ipv4(rng: &mut dyn rand::Rng) -> Ipv4Addr {
+ Ipv4Addr::from_octets(rng.random::<[u8; 4]>())
+ }
+
+ fn rand_ipv6(rng: &mut dyn rand::Rng) -> Ipv6Addr {
+ Ipv6Addr::from_segments(rng.random::<[u16; 8]>())
+ }
+
+ fn rand_node() -> Node {
+ let mut rng = rand::rng();
+
+ Node {
+ stable_id: StableNodeId(rand_string(&mut rng, 32)),
+ tailnet_address: TailnetAddress {
+ ipv4: rand_ipv4(&mut rng).into(),
+ ipv6: rand_ipv6(&mut rng).into(),
+ },
+ node_key: NodePublicKey::random(),
+ disco_key: rng.random::<bool>().then_some(DiscoPublicKey::random()),
+ machine_key: rng.random::<bool>().then_some(MachinePublicKey::random()),
+ id: rng.random(),
+ accepted_routes: (0..rng.random_range(0..32))
+ .map(|_| rand_route(&mut rng))
+ .collect(),
+
+ hostname: rand_string(&mut rng, 32),
+ tailnet: rng.random::<bool>().then_some(rand_string(&mut rng, 32)),
+ capability_version: Default::default(),
+
+ status: NodeStatus::Unknown,
+ node_key_expiry: None,
+ underlay_addresses: vec![],
+ tailnet_lock_key_signature: None,
+ derp_region: rng
+ .random::<bool>()
+ .then_some(ts_derp::RegionId(rng.random())),
+
+ tags: (0..rng.random_range(0..8))
+ .map(|_| rand_string(&mut rng, 32))
+ .collect(),
+ node_capabilities: Default::default(),
+ device: Default::default(),
+ }
+ }
+
+ fn validate_indices(db: &PeerDb, node: &Node, id: PeerId) {
+ let ipv4 = IpAddr::from(node.tailnet_address.ipv4.addr());
+ let ipv6 = IpAddr::from(node.tailnet_address.ipv6.addr());
+ let fqdn = node.fqdn_opt(false);
+
+ let mut keys: Vec<&dyn IndexedField> =
+ vec![&id, &node.node_key, &node.stable_id, &node.id, &ipv4, &ipv6];
+
+ if let Some(disco) = &node.disco_key {
+ keys.push(disco);
+ }
+
+ if let Some(fqdn) = &fqdn {
+ keys.push(fqdn);
+ }
+
+ for k in keys {
+ let lookup_id = k.lookup(db).unwrap();
+ assert_eq!(lookup_id, id, "wrong id for key {k:?}");
+
+ let (lookup_id, lookup_node) = db.get(k).unwrap();
+ assert_eq!(lookup_id, id, "wrong id for key {k:?}");
+ assert_eq!(lookup_node, node, "wrong node for key {k:?}");
+ }
+
+ // We don't know if the hostname collides, but it should resolve to something
+ node.hostname.lookup(db).unwrap();
+
+ for &route in &node.accepted_routes {
+ // Generically we don't actually know if this node has the most specific match for this
+ // route, but there should at least be one match, and all matches should have at least
+ // one route that (inclusively) subsets our route.
+
+ let routes = db.get_route(route).collect::<Vec<_>>();
+ assert!(!routes.is_empty());
+
+ for (found_id, found_node) in routes {
+ if found_id == id {
+ assert_eq!(found_node, node);
+ break;
+ }
+
+ let has_subset = found_node
+ .accepted_routes
+ .iter()
+ .any(|found_route| route.contains(found_route));
+
+ assert!(has_subset);
+ }
+ }
+ }
+
+ /// Assert that the node's routes are all present as the most specific routes in the
+ /// db.
+ fn assert_has_routes_exact(db: &PeerDb, node: &Node, id: PeerId) {
+ for &route in &node.accepted_routes {
+ let match_exists = db
+ .get_route(route)
+ .any(|(found_id, found_node)| found_id == id && found_node == node);
+
+ assert!(match_exists);
+ }
+ }
+
+ #[test]
+ fn test_indices() {
+ let mut db = PeerDb::default();
+ let node = rand_node();
+ let id = db.upsert(&node);
+
+ validate_indices(&db, &node, id);
+ assert_has_routes_exact(&db, &node, id);
+ }
+
+ #[test]
+ fn test_names() {
+ let mut db = PeerDb::default();
+
+ let node1 = Node {
+ hostname: "test".to_string(),
+ tailnet: Some("ts.net".to_string()),
+ ..rand_node()
+ };
+ let node2 = Node {
+ hostname: "test".to_string(),
+ tailnet: Some("ts2.net".to_string()),
+ ..rand_node()
+ };
+ let node3 = Node {
+ hostname: "test".to_string(),
+ tailnet: None,
+ ..rand_node()
+ };
+
+ let id1 = db.upsert(&node1);
+ let id2 = db.upsert(&node2);
+ let id3 = db.upsert(&node3);
+
+ let nodes = [(id1, &node1), (id2, &node2), (id3, &node3)];
+
+ for (id, node) in &nodes {
+ validate_indices(&db, node, *id);
+ }
+
+ let (id, node) = db.get(&"test").unwrap();
+ assert!(nodes.iter().any(|(x, _node)| *x == id));
+
+ for &(x, curnode) in &nodes {
+ if x == id {
+ assert_eq!(node, curnode);
+ } else {
+ assert_ne!(node, curnode);
+ }
+ }
+
+ let (id, node) = db.get(&"test.ts.net").unwrap();
+ assert_eq!(id, id1);
+ assert_eq!(node, &node1);
+
+ let (id, node) = db.get(&"test.ts2.net").unwrap();
+ assert_eq!(id, id2);
+ assert_eq!(node, &node2);
+ }
+
+ proptest::prop_compose! {
+ fn capability_version_low()(
+ capver in 3u16..34,
+ ) -> CapabilityVersion {
+ CapabilityVersion::new(capver).unwrap()
+ }
+ }
+
+ proptest::prop_compose! {
+ fn capability_version_high()(
+ capver in 36u16..133,
+ ) -> CapabilityVersion {
+ CapabilityVersion::new(capver).unwrap()
+ }
+ }
+
+ fn capability_version() -> impl Strategy<Value = CapabilityVersion> {
+ proptest::prop_oneof![capability_version_low(), capability_version_high()]
+ }
+
+ proptest::prop_compose! {
+ fn ipv4net()(
+ addr: Ipv4Addr,
+ pfx in 0u8..=32,
+ ) -> ipnet::Ipv4Net {
+ ipnet::Ipv4Net::new(addr, pfx).unwrap().trunc()
+ }
+ }
+
+ proptest::prop_compose! {
+ fn ipv6net()(
+ addr: Ipv6Addr,
+ pfx in 0u8..=32,
+ ) -> ipnet::Ipv6Net {
+ ipnet::Ipv6Net::new(addr, pfx).unwrap().trunc()
+ }
+ }
+
+ fn ipnet() -> impl Strategy<Value = ipnet::IpNet> {
+ proptest::prop_oneof![
+ ipv4net().prop_map(ipnet::IpNet::from),
+ ipv6net().prop_map(ipnet::IpNet::from)
+ ]
+ }
+
+ proptest::prop_compose! {
+ fn last_seen()(
+ control_ns_since_epoch in any::<Option<i64>>(),
+ estimated_ns_since_epoch in any::<i64>(),
+ ) -> NodeLastSeen {
+ NodeLastSeen {
+ control: control_ns_since_epoch.map(DateTime::from_timestamp_nanos),
+ estimated: DateTime::from_timestamp_nanos(estimated_ns_since_epoch),
+ }
+ }
+ }
+
+ fn status() -> impl Strategy<Value = NodeStatus> {
+ proptest::prop_oneof![
+ Just(NodeStatus::Unknown),
+ Just(NodeStatus::Online),
+ last_seen().prop_map(NodeStatus::Offline),
+ ]
+ }
+
+ proptest::prop_compose! {
+ fn domain_segment()(
+ seg in "[[:alpha:]][[:alnum:]]*"
+ ) -> String {
+ seg
+ }
+ }
+
+ proptest::prop_compose! {
+ fn domain(max_count: usize)(
+ segs in proptest::collection::vec(domain_segment(), 0..max_count)
+ ) -> String {
+ segs.join(".")
+ }
+ }
+
+ proptest::prop_compose! {
+ fn node_capabilities(max_values_per_entry: usize)(
+ keys in hash_set(".+", 0..32),
+ values in vec(vec(".+", 0..32), 0..max_values_per_entry),
+ ) -> BTreeMap<String, Vec<String>> {
+ BTreeMap::from_iter(keys.into_iter().zip(values.into_iter()))
+ }
+ }
+
+ type Key = [u8; 32];
+ type TailnetLockSignature = [u8; 32];
+
+ proptest::prop_compose! {
+ // This is set up this way to ensure uniqueness among all the required-unique keys in a
+ // node. The `hash_set`s ensure that all ids AND stable ids AND node keys etc. are unique.
+ fn nodes(n: usize)(
+ id in hash_set(any::<i64>(), n),
+ stable_id in hash_set(".+", n),
+ capability_version in vec(capability_version(), n),
+ tags in vec(hash_set(".+", 0..32), n),
+ node_capabilities in vec(node_capabilities(3), n),
+ accepted_routes in vec(hash_set(ipnet(), 0..32), n),
+ status in vec(status(), n),
+ node_key in hash_set(any::<Key>(), n),
+ machine_key in vec(any::<Option<Key>>(), n),
+ disco_key in vec(any::<Option<Key>>(), n),
+ tailnet_lock_key_signature in vec(any::<Option<TailnetLockSignature>>(), n),
+ ipv4 in hash_set(any::<Ipv4Addr>(), n),
+ ipv6 in hash_set(any::<Ipv6Addr>(), n),
+ name in hash_set(domain_segment(), n),
+ tailnet in vec(domain(5), n),
+ has_tailnet in vec(any::<bool>(), n),
+ derp_region in vec(any::<Option<NonZeroU32>>(), n),
+ underlay_addrs in vec(any::<HashSet<SocketAddr>>(), n),
+ ) -> Vec<Node> {
+ itertools::izip![
+ id,
+ stable_id,
+ capability_version,
+ tags,
+ node_capabilities,
+ accepted_routes,
+ status,
+ node_key,
+ machine_key,
+ disco_key,
+ tailnet_lock_key_signature,
+ ipv4,
+ ipv6,
+ name,
+ tailnet,
+ has_tailnet,
+ derp_region,
+ underlay_addrs,
+ ].map(|(
+ id,
+ stable_id,
+ capability_version,
+ tags,
+ node_capabilities,
+ mut accepted_routes,
+ status,
+ node_key,
+ machine_key,
+ disco_key,
+ tailnet_lock_key_signature,
+ ipv4,
+ ipv6,
+ name,
+ tailnet,
+ has_tailnet,
+ derp_region,
+ underlay_addrs,
+ )| {
+ accepted_routes.insert(ipnet::Ipv4Net::from(ipv4).into());
+ accepted_routes.insert(ipnet::Ipv6Net::from(ipv6).into());
+
+ Node {
+ id,
+ stable_id: StableNodeId(stable_id),
+ capability_version,
+
+ hostname: name,
+ tailnet: has_tailnet.then_some(tailnet),
+
+ status,
+ node_key: NodePublicKey::from_bytes(node_key),
+ disco_key: disco_key.map(DiscoPublicKey::from_bytes),
+ machine_key: machine_key.map(MachinePublicKey::from_bytes),
+ tailnet_lock_key_signature: tailnet_lock_key_signature.map(Into::into),
+
+ node_key_expiry: None,
+
+ tailnet_address: TailnetAddress {
+ ipv4: ipv4.into(),
+ ipv6: ipv6.into(),
+ },
+ tags: tags.into_iter().collect(),
+ node_capabilities,
+
+ derp_region: derp_region.map(ts_derp::RegionId),
+
+ accepted_routes: accepted_routes.into_iter().collect(),
+ underlay_addresses: underlay_addrs.into_iter().collect(),
+ device: Default::default(),
+ }
+ })
+ .collect()
+ }
+ }
+
+ proptest::proptest! {
+ #[test]
+ fn prop_one_node_indices(mut nodes in nodes(1)) {
+ let node = nodes.pop().unwrap();
+
+ let mut db = PeerDb::default();
+ let id = db.upsert(&node);
+
+ validate_indices(&db, &node, id);
+ assert_has_routes_exact(&db, &node, id);
+ }
+
+ #[test]
+ fn prop_many_nodes_indexed(nodes in nodes(16)) {
+ let mut db = PeerDb::default();
+
+ let mut nodes_by_id = HashMap::new();
+
+ for node in &nodes {
+ let id = db.upsert(node);
+ nodes_by_id.insert(id, node.clone());
+ }
+
+ for (id, node) in &nodes_by_id {
+ validate_indices(&db, node, *id);
+ }
+ }
+
+ #[test]
+ fn prop_remove(nodes in nodes(16)) {
+ let mut db = PeerDb::default();
+
+ let mut ids = vec![];
+
+ for node in &nodes {
+ ids.push((db.upsert(node), node));
+ }
+
+ for (id, node) in ids {
+ let (removed_id, removed_node) = db.remove(&id).unwrap();
+
+ proptest::prop_assert_eq!(removed_id, id);
+ proptest::prop_assert_eq!(&removed_node, node);
+ }
+
+ proptest::prop_assert!(db.peers.is_empty());
+ proptest::prop_assert!(db.index_state.is_empty());
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/registry.rs b/deps/ts_runtime/src/registry.rs
new file mode 100644
index 00000000..1ec86671
--- /dev/null
+++ b/deps/ts_runtime/src/registry.rs
@@ -0,0 +1,372 @@
+use core::{
+ any::{Any, TypeId, type_name},
+ marker::PhantomData,
+};
+use std::collections::HashMap;
+
+use kameo::{
+ Reply,
+ actor::{ActorRef, Spawn, WeakActorRef},
+ error::{Infallible, SendError},
+ message::{Context, Message},
+ reply::{BoxReplySender, DelegatedReply},
+};
+use smol_str::SmolStr;
+
+/// Name for a canonical actor instance.
+const CANONICAL: SmolStr = SmolStr::new_static("__canonical__");
+
+/// Complete identifier for an actor registration: the actor type and the user-provided name string.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
+struct Id {
+ actor_ty: TypeId,
+ name: SmolStr,
+}
+
+impl Id {
+ fn new<A>(name: Option<SmolStr>) -> Self
+ where
+ A: Any,
+ {
+ match name {
+ Some(name) => Self::named::<A>(name),
+ None => Self::canonical::<A>(),
+ }
+ }
+
+ const fn canonical<A>() -> Self
+ where
+ A: Any,
+ {
+ Self {
+ actor_ty: TypeId::of::<A>(),
+ name: CANONICAL,
+ }
+ }
+
+ const fn named<A>(name: SmolStr) -> Self
+ where
+ A: Any,
+ {
+ Self {
+ actor_ty: TypeId::of::<A>(),
+ name,
+ }
+ }
+}
+
+/// [`WeakActorRef`] with erased actor type.
+pub type ErasedWeakRef = Box<dyn Any + Send>;
+
+/// An actor registry which itself runs as an actor and provides naming service for the tuple
+/// `(actor_type, name)`.
+///
+/// # Names
+///
+/// When you communicate with this registry, you always explicitly name the type of actor you're
+/// talking about as well as the user-provided string name. This is an affordance for type-safety;
+/// kameo doesn't support fully type-erased actor references or message handlers. As a consequence,
+/// names are permitted to overlap between actors of different types, as there can't be a collision
+/// between them.
+///
+/// The conventional structure of names in this registry includes the idea of a "canonical" actor
+/// which is unique. For actors expected to run as singletons or to have a single special instance,
+/// the `new` function on any of the message types addresses this canonical instance. The canonical
+/// name isn't privileged in any other way (e.g. the registry doesn't prevent you from spawning
+/// named actors if there's a canonical one), it's just a conventional, easily-addressed name for a
+/// special actor if you have one.
+///
+/// # Liveness
+///
+/// This registry does not keep actors alive; all refs are held weakly.
+///
+/// # Comparison to [`kameo::registry`]
+///
+/// We're not using the singleton [`kameo::registry::ACTOR_REGISTRY`] because it's at global scope,
+/// but we need naming services to be scoped to each instance of a tailscale runtime. Rather than
+/// dealing with namespace prefixes, we just run a per-runtime registry.
+///
+/// We don't use [`kameo::registry::ActorRegistry`] for the per-runtime registry because it doesn't
+/// have any built-in synchronization, is hard to customize, and requires manual downcasting on the
+/// part of the user, despite the fact that the contained actor refs are only usable if you know
+/// what kind of messages they can handle (i.e. you essentially must know the actor type a priori).
+#[derive(Default)]
+pub struct Registry {
+ actors: HashMap<Id, ErasedWeakRef>,
+ pending_lookups: HashMap<Id, Vec<BoxReplySender>>,
+}
+
+impl kameo::Actor for Registry {
+ type Args = ();
+ type Error = Infallible;
+
+ async fn on_start(_args: Self::Args, _actor_ref: ActorRef<Self>) -> Result<Self, Self::Error> {
+ Ok(Self::default())
+ }
+}
+
+/// Request to register an actor with a given name.
+///
+/// The registry replies with the [`WeakActorRef`] of an actor that was already registered in this
+/// name if there was one.
+pub struct Register<A>
+where
+ A: kameo::Actor,
+{
+ id: Id,
+ aref: WeakActorRef<A>,
+}
+
+impl<A> Register<A>
+where
+ A: kameo::Actor + Any,
+{
+ /// Construct a register request for the actor of type `A` with the specified `name`, if given,
+ /// or the canonical actor if not.
+ pub fn new(name: Option<SmolStr>, aref: &ActorRef<A>) -> Self {
+ Self {
+ id: Id::new::<A>(name),
+ aref: aref.downgrade(),
+ }
+ }
+}
+
+impl<A> Message<Register<A>> for Registry
+where
+ A: kameo::Actor + Any,
+{
+ type Reply = Option<ErasedWeakRef>;
+
+ async fn handle(
+ &mut self,
+ msg: Register<A>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Option<ErasedWeakRef> {
+ if let Some(pending) = self.pending_lookups.remove(&msg.id) {
+ for sender in pending {
+ drop(sender.send(Ok(Box::new(Some(msg.aref.clone())))));
+ }
+ }
+
+ self.actors.insert(msg.id, Box::new(msg.aref))
+ }
+}
+
+/// Request to unregister an actor for a given name.
+///
+/// The registry replies with the [`WeakActorRef`] of the unregistered actor if there was one.
+pub struct Unregister<A>(Id, PhantomData<A>);
+
+impl<A> Unregister<A>
+where
+ A: Any,
+{
+ /// Unregister an actor of type `A` if it exists in the registry. If `name` is `None`, the
+ /// canonical actor is unregistered.
+ pub fn new(name: Option<SmolStr>) -> Self {
+ Self(Id::new::<A>(name), PhantomData)
+ }
+}
+
+impl<A> Message<Unregister<A>> for Registry
+where
+ A: kameo::Actor,
+{
+ type Reply = Option<WeakActorRef<A>>;
+
+ async fn handle(
+ &mut self,
+ msg: Unregister<A>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Option<WeakActorRef<A>> {
+ let previous = self.actors.remove(&msg.0)?;
+ *previous.downcast().unwrap()
+ }
+}
+
+pub struct Lookup<A> {
+ id: Id,
+ wait: bool,
+ _phantom: PhantomData<A>,
+}
+
+impl<A> Lookup<A>
+where
+ A: Any,
+{
+ /// Look up the actor with the given `name`, or the canonical actor if `name` is `None`.
+ pub fn new(name: Option<SmolStr>) -> Self {
+ Self {
+ id: Id::new::<A>(name),
+ wait: false,
+ _phantom: PhantomData,
+ }
+ }
+
+ /// Wait until an actor is registered with the given name.
+ pub const fn wait(mut self, wait: bool) -> Self {
+ self.wait = wait;
+ self
+ }
+}
+
+impl<A> Message<Lookup<A>> for Registry
+where
+ A: kameo::Actor,
+{
+ type Reply = DelegatedReply<Option<WeakActorRef<A>>>;
+
+ async fn handle(
+ &mut self,
+ msg: Lookup<A>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let (deleg, sender) = ctx.reply_sender();
+
+ if let Some(sender) = sender {
+ let aref = self
+ .actors
+ .get(&msg.id)
+ .map(|x| x.downcast_ref::<WeakActorRef<A>>().unwrap())
+ .cloned();
+
+ match (&aref, msg.wait) {
+ (Some(_), _) | (_, false) => {
+ sender.send(aref);
+ }
+ (None, true) => {
+ self.pending_lookups
+ .entry(msg.id)
+ .or_default()
+ .push(sender.boxed());
+ }
+ }
+ };
+
+ deleg
+ }
+}
+
+/// Request to ensure a given actor exists in the registry.
+///
+/// If the actor doesn't exist, it'll be created with the args produced by the contained function.
+/// The reply type is whether an actor was created and the ref to the actor.
+pub struct Ensure<A, F, Fut> {
+ id: Id,
+ create: F,
+ _phantom: PhantomData<(A, Fut)>,
+}
+
+impl<A, F, Fut> Ensure<A, F, Fut> {
+ /// Construct an [`Ensure`] message.
+ pub fn new(name: Option<SmolStr>, mk_args: F) -> Self
+ where
+ A: Any,
+ {
+ Self {
+ id: Id::new::<A>(name),
+ create: mk_args,
+ _phantom: PhantomData,
+ }
+ }
+}
+
+impl<A, F, Fut> Message<Ensure<A, F, Fut>> for Registry
+where
+ A: kameo::Actor,
+ F: FnOnce() -> Fut + Send + 'static,
+ Fut: Future<Output = A::Args> + Send + 'static,
+{
+ type Reply = (bool, ActorRef<A>);
+
+ async fn handle(
+ &mut self,
+ msg: Ensure<A, F, Fut>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> (bool, ActorRef<A>) {
+ if let Some(aref) = self.actors.get(&msg.id) {
+ return (false, aref.downcast_ref().cloned().unwrap());
+ }
+
+ let args = (msg.create)().await;
+ let aref = A::spawn(args);
+
+ self.actors.insert(msg.id, Box::new(aref.downgrade()));
+
+ (true, aref)
+ }
+}
+
+/// Request to forward a message of type `M` to an actor of type `A` under a particular registered
+/// name.
+pub struct Forward<A, M> {
+ id: Id,
+ message: M,
+ _phantom: PhantomData<A>,
+}
+
+impl<A, M> Forward<A, M>
+where
+ A: Any,
+{
+ /// Construct a new [`Forward`] for the message `M`.
+ pub fn new(name: Option<SmolStr>, m: M) -> Self {
+ Self {
+ id: Id::new::<A>(name),
+ message: m,
+ _phantom: PhantomData,
+ }
+ }
+}
+
+/// The reply to a [`Forward`] that was asked: the actor's own reply, or why the message didn't
+/// reach it.
+pub type Forwarded<A, M> = Result<
+ <<A as Message<M>>::Reply as Reply>::Ok,
+ SendError<M, <<A as Message<M>>::Reply as Reply>::Error>,
+>;
+
+impl<A, M> Message<Forward<A, M>> for Registry
+where
+ A: Message<M>,
+ M: Send + 'static,
+{
+ type Reply = DelegatedReply<Forwarded<A, M>>;
+
+ #[tracing::instrument(skip_all, fields(msgty = type_name::<M>(), actor = type_name::<A>(), name = %msg.id.name
+ ))]
+ async fn handle(
+ &mut self,
+ msg: Forward<A, M>,
+ ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let Some(aref) = self.actors.get(&msg.id) else {
+ tracing::trace!("actor not found");
+ return ctx.reply(Err(SendError::ActorNotRunning(msg.message)));
+ };
+
+ let Some(aref) = aref.downcast_ref::<WeakActorRef<A>>().unwrap().upgrade() else {
+ tracing::trace!("actor dead");
+ return ctx.reply(Err(SendError::ActorNotRunning(msg.message)));
+ };
+
+ // Not `ctx.try_forward`: in kameo 0.21 it panics when the actor's mailbox is full or
+ // closed, as it takes the message back out as a (message, reply sender) pair. Asking from
+ // a task gets the same reply, and an error in those cases.
+ let (deleg, reply) = ctx.reply_sender();
+ match reply {
+ Some(reply) => {
+ tokio::spawn(async move {
+ reply.send(aref.ask(msg.message).try_send().await);
+ });
+ }
+ None => {
+ if let Err(e) = aref.tell(msg.message).try_send() {
+ tracing::trace!(error = ?e, "forward error");
+ }
+ }
+ }
+
+ deleg
+ }
+}
diff --git a/deps/ts_runtime/src/retained_bus.rs b/deps/ts_runtime/src/retained_bus.rs
new file mode 100644
index 00000000..68127781
--- /dev/null
+++ b/deps/ts_runtime/src/retained_bus.rs
@@ -0,0 +1,320 @@
+use core::{
+ any::{Any, TypeId},
+ marker::PhantomData,
+};
+use std::collections::HashMap;
+
+use kameo::{
+ actor::{ActorId, Recipient},
+ message::{Context, Message},
+};
+pub use kameo_actors::message_bus::Register;
+
+/// Type-erased [`SubState<M>`].
+type ErasedSubState = Box<dyn Any + Send>;
+
+/// A version of [`MessageBus`][kameo_actor::message_bus::MessageBus] optionally tracking retained
+/// state.
+///
+/// [`Publish`] messages may optionally set the `retained` flag to indicate that the message should
+/// be retained on the bus. When present, new [`Register`]ed actors receive the current retained
+/// message immediately.
+#[derive(Default, kameo::Actor)]
+pub struct RetainedBus {
+ /// Essentially: `Map<M, SubState<M>>`.
+ subscriptions: HashMap<TypeId, ErasedSubState>,
+}
+
+// The unwraps in this impl block are safe because they are only present on downcasts, which are
+// protected by the invariant that the value type for `TypeId::of::<M>()` is `SubState<M>`.
+impl RetainedBus {
+ fn get<M>(&self) -> Option<&SubState<M>>
+ where
+ M: Send + 'static,
+ {
+ let state = self.subscriptions.get(&TypeId::of::<M>())?;
+ Some(state.downcast_ref().unwrap())
+ }
+
+ fn get_mut<M>(&mut self) -> Option<&mut SubState<M>>
+ where
+ M: Send + 'static,
+ {
+ let state = self.subscriptions.get_mut(&TypeId::of::<M>())?;
+ Some(state.downcast_mut().unwrap())
+ }
+
+ fn entry_or_default<M>(&mut self) -> &mut SubState<M>
+ where
+ M: Send + 'static,
+ {
+ let state = self
+ .subscriptions
+ .entry(TypeId::of::<M>())
+ .or_insert_with(|| Box::new(SubState::<M>::default()));
+
+ state.downcast_mut().unwrap()
+ }
+
+ fn remove<M>(&mut self) -> Option<SubState<M>>
+ where
+ M: Send + 'static,
+ {
+ let ret = *self
+ .subscriptions
+ .remove(&TypeId::of::<M>())?
+ .downcast::<SubState<M>>()
+ .unwrap();
+
+ Some(ret)
+ }
+}
+
+struct SubState<M>
+where
+ M: Send + 'static,
+{
+ retained: Option<M>,
+ recipients: HashMap<ActorId, Recipient<M>>,
+}
+
+impl<M> Default for SubState<M>
+where
+ M: Send + 'static,
+{
+ fn default() -> Self {
+ Self {
+ retained: None,
+ recipients: Default::default(),
+ }
+ }
+}
+
+#[derive(Debug, Copy, Clone, Default)]
+pub struct Publish<M> {
+ pub message: M,
+ pub retained: bool,
+}
+
+impl<M> Publish<M> {
+ /// Construct a new unretained publish.
+ ///
+ /// If there exists a retained message stored in the [`RetainedBus`] for this message type, it
+ /// is neither updated nor cleared by this message.
+ pub const fn unretained(m: M) -> Self {
+ Self {
+ message: m,
+ retained: false,
+ }
+ }
+
+ /// Construct a new retained publish.
+ ///
+ /// It will update the retained message in the [`RetainedBus`].
+ pub const fn retained(m: M) -> Self {
+ Self {
+ message: m,
+ retained: true,
+ }
+ }
+}
+
+impl<M> Message<Publish<M>> for RetainedBus
+where
+ M: Clone + Send + 'static,
+{
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ Publish { message, retained }: Publish<M>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let state = self.entry_or_default::<M>();
+
+ if retained {
+ state.retained = Some(message.clone());
+ }
+
+ let mut any_missing = false;
+
+ for recip in state.recipients.values_mut() {
+ // actor is dead (this is an expected case)
+ if recip.tell(message.clone()).await.is_err() {
+ any_missing = true;
+ }
+ }
+
+ if any_missing {
+ state.recipients.retain(|id, recip| {
+ let retain = recip.is_alive();
+ if !retain {
+ tracing::trace!(
+ ?id,
+ msgty = core::any::type_name::<M>(),
+ "actor gone, remove subscription"
+ );
+ }
+
+ retain
+ });
+ }
+ }
+}
+
+impl<M> Message<Register<M>> for RetainedBus
+where
+ M: Clone + Send + 'static,
+{
+ type Reply = Option<Recipient<M>>;
+
+ async fn handle(
+ &mut self,
+ Register(recip): Register<M>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let state = self.entry_or_default::<M>();
+
+ if let Some(retained) = &state.retained
+ && !state.recipients.contains_key(&recip.id())
+ && let Err(e) = recip.tell(retained.clone()).await
+ {
+ tracing::error!(error = %e);
+ }
+
+ state.recipients.insert(recip.id(), recip)
+ }
+}
+
+/// Get the current retained value for type `M` (if any).
+#[derive(Debug, Copy, Clone, Default)]
+pub struct GetRetained<M>(PhantomData<M>);
+
+impl<M> Message<GetRetained<M>> for RetainedBus
+where
+ M: Clone + Send + 'static,
+{
+ type Reply = Option<M>;
+
+ async fn handle(
+ &mut self,
+ _: GetRetained<M>,
+ _: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let state = self.get::<M>()?;
+ let ret = state.retained.as_ref()?;
+
+ Some(ret.clone())
+ }
+}
+
+pub struct Unregister<M> {
+ actor_id: ActorId,
+ _phantom: PhantomData<M>,
+}
+
+impl<M> Unregister<M> {
+ pub const fn new(id: ActorId) -> Self {
+ Self {
+ actor_id: id,
+ _phantom: PhantomData,
+ }
+ }
+}
+
+impl<M> Message<Unregister<M>> for RetainedBus
+where
+ M: Send + 'static,
+{
+ type Reply = (Option<Recipient<M>>, Option<M>);
+
+ async fn handle(
+ &mut self,
+ Unregister { actor_id, .. }: Unregister<M>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let Some(state) = self.get_mut::<M>() else {
+ return (None, None);
+ };
+
+ let ret = state.recipients.remove(&actor_id);
+
+ let state = if state.recipients.is_empty() {
+ self.remove::<M>().and_then(|state| state.retained)
+ } else {
+ None
+ };
+
+ (ret, state)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use std::time::Duration;
+
+ use kameo::actor::{ActorRef, Spawn};
+ use tokio::sync::oneshot;
+
+ use super::*;
+
+ /// Ensure that the downcasts are of the correct types.
+ #[test]
+ fn basic() {
+ type T = ();
+
+ let mut bus = RetainedBus::default();
+
+ let ent = bus.entry_or_default::<T>();
+ assert!(ent.retained.is_none());
+ assert!(ent.recipients.is_empty());
+
+ assert!(bus.get::<T>().is_some());
+ assert!(bus.get_mut::<T>().is_some());
+
+ let ent = bus.remove::<T>().unwrap();
+ assert!(ent.retained.is_none());
+ assert!(ent.recipients.is_empty());
+ }
+
+ #[tokio::test]
+ async fn bus_basic() {
+ let bus = RetainedBus::spawn_default();
+ bus.wait_for_startup_result().await.unwrap();
+
+ #[derive(kameo::Actor)]
+ struct Dummy(Option<oneshot::Sender<()>>);
+
+ #[kameo::messages]
+ impl Dummy {
+ #[message(ctx)]
+ async fn test(&self, bus: ActorRef<RetainedBus>, ctx: &mut Context<Self, ()>) {
+ bus.ask(Register(ctx.actor_ref().clone().recipient::<Test2>()))
+ .await
+ .unwrap();
+
+ bus.tell(Publish::unretained(Test2)).try_send().unwrap();
+ }
+
+ #[message]
+ fn test2(&mut self) {
+ self.0.take().unwrap().send(()).unwrap();
+ }
+ }
+
+ impl Clone for Test2 {
+ fn clone(&self) -> Self {
+ Self
+ }
+ }
+
+ let (tx, rx) = oneshot::channel();
+ let dummy = Dummy::spawn(Dummy(Some(tx)));
+ dummy.ask(Test { bus: bus.clone() }).await.unwrap();
+
+ tokio::time::timeout(Duration::from_secs(1), rx)
+ .await
+ .unwrap()
+ .unwrap()
+ }
+}
diff --git a/deps/ts_runtime/src/route_updater.rs b/deps/ts_runtime/src/route_updater.rs
new file mode 100644
index 00000000..29ee1207
--- /dev/null
+++ b/deps/ts_runtime/src/route_updater.rs
@@ -0,0 +1,229 @@
+use std::{collections::HashMap, sync::Arc};
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+use ts_bart::RoutingTable;
+use ts_overlay_router::{
+ inbound::RouteAction as InboundRouteAction, outbound::RouteAction as OutboundRouteAction,
+};
+use ts_transport::{DynEndpoint, OverlayTransportId, PeerId, UnderlayTransportId};
+
+use crate::{
+ Error,
+ env::Env,
+ multiderp::DerpTransportMap,
+ path_discoverer::{BestPathMap, BestPaths},
+ peer_tracker::PeerState,
+};
+
+pub struct RouteUpdater {
+ default_overlay_transport: OverlayTransportId,
+ /// Map of all available DERP regions to the ID of the underlay transport that handles that
+ /// region.
+ derp_transport_map: DerpTransportMap,
+ peer_state: Arc<PeerState>,
+ best_paths: Arc<BestPathMap>,
+ env: Env,
+}
+
+impl RouteUpdater {
+ fn build_peer_routes(&self) -> PeerRoutesInner {
+ tracing::trace!(
+ n_peers = self.peer_state.peers.peers().len(),
+ "reconstructing routes for peer update"
+ );
+
+ let mut routes = PeerRoutesInner::default();
+
+ for (id, peer) in self.peer_state.peers.peers() {
+ let span = tracing::trace_span!(
+ "peer_route_update",
+ peer_key = %peer.node_key,
+ region = ?peer.derp_region,
+ underlay_transport = tracing::field::Empty,
+ ep = tracing::field::Empty,
+ peer_id = ?id,
+ )
+ .entered();
+
+ let (transport_id, ep) = if let Some((transport_id, ep)) = self.best_paths.get(id) {
+ (*transport_id, ep.clone())
+ } else if let Some(region) = peer.derp_region
+ && let Some(transport_id) = self.derp_transport_map.0.get(&region)
+ {
+ (*transport_id, DynEndpoint::derp(peer.node_key))
+ } else {
+ // If we would log this error because we don't have a derp map or the peer just
+ // doesn't have a home region set in control, skip doing so because it'll be spammy.
+ if !self.derp_transport_map.0.is_empty() && peer.derp_region.is_some() {
+ tracing::error!("no region stored in multiderp, no underlay route");
+ }
+
+ continue;
+ };
+
+ routes
+ .underlay_routes
+ .insert(*id, (transport_id, ep.clone()));
+
+ span.record("underlay_transport", tracing::field::debug(&transport_id));
+ span.record("ep", tracing::field::debug(&ep));
+
+ tracing::trace!(?ep, ?transport_id, "selected route for peer");
+
+ for route in &peer.accepted_routes {
+ routes
+ .overlay_out_routes
+ .insert(*route, OutboundRouteAction::Wireguard(*id));
+ }
+ }
+
+ routes
+ }
+
+ async fn publish_peer_routes(&mut self) {
+ let new_routes = self.build_peer_routes();
+
+ if let Err(e) = self
+ .env
+ .publish(PeerRouteUpdate {
+ inner: Arc::new(new_routes),
+ })
+ .await
+ {
+ tracing::error!(error = %e, "publishing peer route update");
+ }
+ }
+}
+
+impl kameo::Actor for RouteUpdater {
+ type Args = (Env, OverlayTransportId);
+ type Error = Error;
+
+ async fn on_start(
+ (env, default_transport): Self::Args,
+ slf: ActorRef<Self>,
+ ) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+ env.subscribe::<DerpTransportMap>(&slf).await?;
+ env.subscribe::<BestPaths>(&slf).await?;
+
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ default_overlay_transport: default_transport,
+ derp_transport_map: Default::default(),
+ peer_state: Default::default(),
+ best_paths: Default::default(),
+ env,
+ })
+ }
+}
+
+#[derive(Clone)]
+pub struct SelfRouteUpdate {
+ pub overlay_in_routes: Arc<ts_bart::Table<InboundRouteAction>>,
+}
+
+#[derive(Clone)]
+pub struct PeerRouteUpdate {
+ pub inner: Arc<PeerRoutesInner>,
+}
+
+#[derive(Default)]
+pub struct PeerRoutesInner {
+ pub underlay_routes: HashMap<PeerId, (UnderlayTransportId, DynEndpoint)>,
+ pub overlay_out_routes: ts_bart::Table<OutboundRouteAction>,
+}
+
+impl Message<Arc<PeerState>> for RouteUpdater {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: Arc<PeerState>, _ctx: &mut Context<Self, Self::Reply>) {
+ self.peer_state = msg;
+
+ let new_routes = self.build_peer_routes();
+
+ if let Err(e) = self
+ .env
+ .publish(PeerRouteUpdate {
+ inner: Arc::new(new_routes),
+ })
+ .await
+ {
+ tracing::error!(error = %e, "publishing peer route update");
+ }
+ }
+}
+
+impl Message<DerpTransportMap> for RouteUpdater {
+ type Reply = ();
+
+ async fn handle(&mut self, msg: DerpTransportMap, _ctx: &mut Context<Self, Self::Reply>) {
+ if self.derp_transport_map.0 == msg.0 {
+ return;
+ }
+
+ tracing::debug!("derp transport map changed, building new routes");
+
+ self.derp_transport_map = msg;
+ self.publish_peer_routes().await;
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for RouteUpdater {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let Some(node) = msg.node.as_ref() else {
+ return;
+ };
+
+ let mut out = ts_bart::Table::default();
+
+ tracing::debug!(accepted_routes = ?node.accepted_routes, "populating accepted routes");
+
+ for &accepted_route in &node.accepted_routes {
+ out.insert(
+ accepted_route,
+ InboundRouteAction::ToOverlay(self.default_overlay_transport),
+ );
+ }
+
+ if let Err(e) = self
+ .env
+ .publish(SelfRouteUpdate {
+ overlay_in_routes: Arc::new(out),
+ })
+ .await
+ {
+ tracing::error!(error = %e, "publishing self route update");
+ }
+ }
+}
+
+impl Message<BestPaths> for RouteUpdater {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ BestPaths(new_bps): BestPaths,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ if self.best_paths == new_bps {
+ tracing::trace!("no change to best paths, skip");
+ return;
+ }
+
+ self.best_paths = new_bps;
+
+ self.publish_peer_routes().await;
+ }
+}
diff --git a/deps/ts_runtime/src/src_filter.rs b/deps/ts_runtime/src/src_filter.rs
new file mode 100644
index 00000000..a79722cb
--- /dev/null
+++ b/deps/ts_runtime/src/src_filter.rs
@@ -0,0 +1,57 @@
+use std::sync::Arc;
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+use ts_bart::{RoutingTable, Table};
+use ts_transport::PeerId;
+
+use crate::{Error, env::Env, peer_tracker::PeerState};
+
+pub struct SourceFilterUpdater {
+ env: Env,
+}
+
+impl kameo::Actor for SourceFilterUpdater {
+ type Args = Env;
+ type Error = Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ env.subscribe::<Arc<PeerState>>(&slf).await?;
+ env.register(None, &slf).await?;
+
+ Ok(Self { env })
+ }
+}
+
+#[derive(Clone)]
+pub struct SourceFilterState(pub Arc<Table<PeerId>>);
+
+impl Message<Arc<PeerState>> for SourceFilterUpdater {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ state_update: Arc<PeerState>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) {
+ let mut src_filter = Table::default();
+
+ for (id, node) in state_update.peers.peers() {
+ for route in node.accepted_routes.iter() {
+ src_filter.insert(route.to_owned(), *id);
+ }
+ }
+
+ tracing::trace!(updated_source_filter = ?src_filter);
+
+ if let Err(e) = self
+ .env
+ .publish(SourceFilterState(Arc::new(src_filter)))
+ .await
+ {
+ tracing::error!(error = %e, "publishing source filter state");
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/stunner.rs b/deps/ts_runtime/src/stunner.rs
new file mode 100644
index 00000000..684afbfb
--- /dev/null
+++ b/deps/ts_runtime/src/stunner.rs
@@ -0,0 +1,150 @@
+use std::{
+ net::{IpAddr, SocketAddr},
+ sync::Arc,
+ time::{Duration, Instant},
+};
+
+use kameo::{
+ actor::ActorRef,
+ message::{Context, Message},
+};
+use tokio::time::MissedTickBehavior;
+use ts_derp::IpUsage;
+
+use crate::env::Env;
+
+/// Actor that sends STUN requests to DERP servers to get this node's public IPv4.
+pub struct Stunner {
+ env: Env,
+ stun: Arc<ts_netcheck::StunProber>,
+ servers: Vec<SocketAddr>,
+}
+
+impl Stunner {
+ #[tracing::instrument(skip_all, fields(n_server = self.servers.len()), level = "trace")]
+ async fn try_stun(&self) {
+ if self.servers.is_empty() {
+ tracing::debug!("skipping stun, servers not populated yet");
+ return;
+ }
+
+ for &server in &self.servers {
+ if let Ok(Ok((_dur, x))) =
+ tokio::time::timeout(Duration::from_secs(3), self.stun.measure(server)).await
+ {
+ tracing::debug!(stun_addr = %x);
+
+ self.env
+ .publish(StunAddress {
+ addr: x.ip(),
+ measured: Instant::now(),
+ })
+ .await
+ .unwrap();
+
+ return;
+ }
+ }
+
+ tracing::warn!("failed to stun");
+ }
+}
+
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub struct StunAddress {
+ pub addr: IpAddr,
+ pub measured: Instant,
+}
+
+#[derive(Copy, Clone)]
+struct Tick;
+
+impl kameo::Actor for Stunner {
+ type Args = Env;
+ type Error = crate::Error;
+
+ async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ // panicking in on_start is fine
+ let stun = ts_netcheck::StunProber::try_new().await.unwrap();
+ env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
+
+ env.scheduler
+ .tell(
+ kameo_actors::scheduler::SetInterval::new(
+ slf.downgrade(),
+ Duration::from_secs(20),
+ Tick,
+ )
+ .set_missed_tick_behaviour(MissedTickBehavior::Skip),
+ )
+ .await?;
+
+ env.register(None, &slf).await?;
+
+ Ok(Self {
+ env,
+ servers: vec![],
+ stun: Arc::new(stun),
+ })
+ }
+}
+
+impl Message<Tick> for Stunner {
+ type Reply = ();
+
+ async fn handle(&mut self, _: Tick, _ctx: &mut Context<Self, Self::Reply>) {
+ self.try_stun().await;
+ }
+}
+
+impl Message<Arc<ts_control::StateUpdate>> for Stunner {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ state_update: Arc<ts_control::StateUpdate>,
+ _ctx: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ let Some(derp_map) = &state_update.derp else {
+ return;
+ };
+
+ let was_empty = self.servers.is_empty();
+ self.servers.clear();
+
+ for region in derp_map.values() {
+ for server in &region.servers {
+ let Some(stun_port) = server.stun_port else {
+ continue;
+ };
+
+ let addr = match server.ipv4 {
+ IpUsage::FixedAddr(a) => (a, stun_port).into(),
+ IpUsage::UseDns => {
+ let addr = tokio::net::lookup_host((server.hostname.as_str(), stun_port))
+ .await
+ .ok()
+ .and_then(|mut x| x.next());
+
+ let Some(addr) = addr else {
+ continue;
+ };
+
+ addr
+ }
+ _ => continue,
+ };
+
+ tracing::trace!(%addr, "identified stun server");
+ self.servers.push(addr);
+ }
+ }
+
+ tracing::debug!(n_server = self.servers.len(), "updated stun servers");
+
+ if was_empty && !self.servers.is_empty() {
+ tracing::trace!("stun server set became populated, trying stun now");
+ self.try_stun().await;
+ }
+ }
+}
diff --git a/deps/ts_runtime/src/task.rs b/deps/ts_runtime/src/task.rs
new file mode 100644
index 00000000..4a2f6333
--- /dev/null
+++ b/deps/ts_runtime/src/task.rs
@@ -0,0 +1,246 @@
+use core::{marker::PhantomData, panic::AssertUnwindSafe, pin::Pin};
+
+use futures::FutureExt;
+use kameo::{
+ actor::{ActorRef, WeakActorRef},
+ error::{ActorStopReason, Infallible},
+ message::{Context, Message},
+};
+use tokio_util::task::AbortOnDropHandle;
+
+/// An actor that runs a future and is associated bidirectionally with its lifecycle: the actor
+/// dying stops the task, and the task dying kills the actor.
+///
+/// To support background tasks, [`Task`] holds its own [`ActorRef`] -- it doesn't die if you don't
+/// hold a reference to it.
+///
+/// Kameo doesn't natively provide functionality for this -- [`ActorRef::attach_stream`] is the
+/// closest thing, but it doesn't fit the mental model of a background task that doesn't necessarily
+/// produce messages. So instead this is a thin wrapper around [`tokio::spawn`], and the inner
+/// future kills the actor if the future completes.
+///
+/// [`Task`] implements [`Message<Infallible>`] so it can be addressed as a type-erased
+/// [`Recipient`][kameo::prelude::Recipient] even though it doesn't accept any messages.
+///
+/// If you need to be generic over the task future or would need to name the type of an anonymous
+/// future, it's recommended to box the future and use [`ErasedTask`] as the task type.
+///
+/// # Examples
+///
+/// Spawning a task that shares lifecycle with a parent:
+///
+/// ```
+/// # use ts_runtime::Task;
+/// # use core::time::Duration;
+/// # use futures::FutureExt;
+/// # use kameo::prelude::{Actor, Spawn, ActorRef};
+/// struct Parent;
+///
+/// impl Actor for Parent {
+/// type Error = ();
+/// type Args = ();
+///
+/// async fn on_start(_: (), slf: ActorRef<Self>) -> Result<Self, ()> {
+/// Task::spawn_link(&slf, async move {
+/// for i in 0.. {
+/// println!("{i}");
+/// tokio::time::sleep(Duration::from_millis(25)).await;
+/// }
+/// });
+///
+/// // ... other self-init ...
+///
+/// Ok(Self)
+/// }
+/// }
+///
+/// # tokio_test::block_on(async move {
+/// let parent = Parent::spawn(());
+/// tokio::time::sleep(Duration::from_millis(99)).await;
+/// parent.kill();
+/// // prints 0, 1, 2, 3, then the parent and task are killed
+/// # });
+/// ```
+pub struct Task<Fut>(Option<AbortOnDropHandle<()>>, PhantomData<Fut>);
+
+/// A [`Task`] that was spawned with a boxed future.
+///
+/// This can be used to operate generically over collections of tasks with different worker futures.
+pub type ErasedTask = Task<Pin<Box<dyn Future<Output = ()> + Send + 'static>>>;
+
+/// Run the task future.
+///
+/// The `slf` as a strong [`ActorRef`] held throughout this future is load-bearing for the way we
+/// expect to use [`Task`]: the [`Task`] keeps itself alive as long as its worker future is running
+/// (a strong ref means kameo won't gc it).
+async fn run_fut<Fut>(fut: Fut, slf: ActorRef<Task<Fut>>)
+where
+ Fut: Future<Output = ()> + Send + 'static,
+{
+ match AssertUnwindSafe(fut).catch_unwind().await {
+ Ok(_) => {
+ if slf.stop_gracefully().await.is_err() {
+ slf.kill();
+ }
+ }
+ Err(e) => {
+ slf.kill();
+
+ std::panic::resume_unwind(e)
+ }
+ }
+}
+
+impl<Fut> kameo::Actor for Task<Fut>
+where
+ Fut: Future<Output = ()> + Send + 'static,
+{
+ type Args = Fut;
+ type Error = core::convert::Infallible;
+
+ async fn on_start(fut: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
+ Ok(Self(
+ Some(AbortOnDropHandle::new(tokio::spawn(run_fut(fut, slf)))),
+ PhantomData,
+ ))
+ }
+
+ // In case there's a condition where the actor is retained in memory for a while after it's
+ // stopped, explicitly trigger abort here rather than waiting for the actor to be dropped.
+ //
+ // The AbortOnDropHandle conversely ensures that the task is aborted if `on_stop` is not called.
+ async fn on_stop(
+ &mut self,
+ slf: WeakActorRef<Self>,
+ reason: ActorStopReason,
+ ) -> Result<(), Self::Error> {
+ tracing::trace!(?slf, ?reason, "task stopping");
+
+ // Unwrap is safe: on_stop is called exactly once and the option is always populated by
+ // actor construction.
+ //
+ // This ensures that the task is aborted at the end of this scope if it's not already
+ // finished (checked in the next block).
+ let handle = self.0.take().unwrap();
+
+ if handle.is_finished()
+ && let Err(e) = handle.detach().await
+ {
+ // The task was never canceled because it was still in the abort handle (the
+ // only way this particular task would be canceled), so the error must be due to
+ // a panic. Rethrow it here to make it appear as this actor's panic to kameo.
+ std::panic::resume_unwind(e.into_panic())
+ }
+
+ Ok(())
+ }
+}
+
+impl<Fut> Message<Infallible> for Task<Fut>
+where
+ Fut: Future<Output = ()> + Send + 'static,
+{
+ type Reply = Infallible;
+
+ async fn handle(&mut self, _: Infallible, _: &mut Context<Self, Self::Reply>) -> Infallible {
+ unreachable!()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use std::{
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::Duration,
+ };
+
+ use kameo::{actor::Spawn, error::HookError};
+
+ use super::*;
+
+ #[tokio::test]
+ async fn kill_stops_task() {
+ let (tx, mut rx) = tokio::sync::mpsc::channel(1);
+
+ let task = Task::spawn(async move {
+ loop {
+ tx.send(()).await.unwrap();
+ }
+ });
+
+ // can receive as many messages as we want
+ for _ in 0..5 {
+ rx.recv().await.unwrap();
+ }
+
+ task.kill();
+ task.wait_for_shutdown().await;
+
+ // drain final buffered message
+ rx.recv().await.unwrap();
+
+ assert!(rx.is_closed());
+ assert!(rx.is_empty());
+ }
+
+ #[tokio::test]
+ async fn task_complete_stops_actor() {
+ let notify = Arc::new(tokio::sync::Notify::new());
+ let completed = Arc::new(AtomicBool::new(false));
+
+ let task = Task::spawn({
+ let notify = notify.clone();
+ let completed = completed.clone();
+
+ async move {
+ notify.notified().await;
+ completed.store(true, Ordering::SeqCst);
+ }
+ });
+
+ assert!(task.is_alive());
+ assert!(!completed.load(Ordering::SeqCst));
+
+ notify.notify_one();
+
+ tokio::time::timeout(Duration::from_millis(100), task.wait_for_shutdown())
+ .await
+ .unwrap();
+
+ assert!(!task.is_alive());
+ assert!(completed.load(Ordering::SeqCst));
+
+ let result = task.get_shutdown_result().unwrap();
+ assert!(result.is_ok());
+ }
+
+ #[tokio::test]
+ async fn task_panic_stops_actor() {
+ let notify = Arc::new(tokio::sync::Notify::new());
+
+ let task = Task::spawn({
+ let notify = notify.clone();
+
+ async move {
+ notify.notified().await;
+ panic!();
+ }
+ });
+
+ assert!(task.is_alive());
+
+ notify.notify_one();
+
+ tokio::time::timeout(Duration::from_millis(100), task.wait_for_shutdown())
+ .await
+ .unwrap();
+
+ assert!(!task.is_alive());
+
+ let result = task.get_shutdown_result().unwrap().unwrap_err();
+ assert!(matches!(result, HookError::Panicked(_)));
+ }
+}