From 4ecd73277828516a15e4bdbf6dd707db93064d9a Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Mon, 13 May 2024 17:03:05 +0200 Subject: [PATCH] refactor(iroh-net)!: Rename MagicEndpoint -> Endpoint This renames the MagicEndpoint (and it's builder) into Endpoint. Hopefully providing a more consistent API. --- iroh-blobs/examples/fetch-fsm.rs | 2 +- iroh-blobs/examples/fetch-stream.rs | 2 +- iroh-blobs/examples/provide-bytes.rs | 2 +- iroh-blobs/src/downloader.rs | 8 +- iroh-blobs/src/downloader/get.rs | 8 +- iroh-blobs/src/get.rs | 34 +++---- iroh-blobs/src/get/db.rs | 2 +- iroh-blobs/src/get/error.rs | 20 ++-- iroh-blobs/src/get/request.rs | 2 +- iroh-blobs/src/protocol.rs | 6 +- iroh-blobs/src/provider.rs | 4 +- iroh-cli/src/commands/doctor.rs | 19 ++-- iroh-cli/src/commands/node.rs | 2 +- iroh-docs/src/net.rs | 6 +- iroh-gossip/examples/chat.rs | 10 +- iroh-gossip/src/net.rs | 30 +++--- iroh-net/README.md | 2 +- iroh-net/bench/src/bin/bulk.rs | 8 +- iroh-net/bench/src/lib.rs | 19 ++-- iroh-net/examples/connect-unreliable.rs | 10 +- iroh-net/examples/connect.rs | 10 +- iroh-net/examples/listen-unreliable.rs | 12 +-- iroh-net/examples/listen.rs | 12 +-- iroh-net/src/dialer.rs | 12 +-- iroh-net/src/discovery.rs | 49 +++++----- iroh-net/src/discovery/dns.rs | 12 +-- iroh-net/src/dns.rs | 2 +- .../src/{magic_endpoint.rs => endpoint.rs} | 96 +++++++++---------- .../{magic_endpoint => endpoint}/rtt_actor.rs | 0 iroh-net/src/lib.rs | 6 +- iroh-net/src/magicsock.rs | 10 +- iroh-net/src/magicsock/node_map.rs | 2 +- iroh-net/src/magicsock/node_map/node_state.rs | 2 +- iroh-net/src/test_utils.rs | 4 +- iroh/src/client/node.rs | 2 +- iroh/src/docs_engine.rs | 8 +- iroh/src/docs_engine/live.rs | 10 +- iroh/src/node.rs | 8 +- iroh/src/node/builder.rs | 10 +- iroh/src/node/rpc.rs | 10 +- iroh/src/rpc_protocol.rs | 2 +- iroh/tests/provide.rs | 2 +- 42 files changed, 231 insertions(+), 246 deletions(-) rename iroh-net/src/{magic_endpoint.rs => endpoint.rs} (94%) rename iroh-net/src/{magic_endpoint => endpoint}/rtt_actor.rs (100%) diff --git a/iroh-blobs/examples/fetch-fsm.rs b/iroh-blobs/examples/fetch-fsm.rs index d93d4450213..9ff063af559 100644 --- a/iroh-blobs/examples/fetch-fsm.rs +++ b/iroh-blobs/examples/fetch-fsm.rs @@ -1,6 +1,6 @@ //! An example how to download a single blob or collection from a node and write it to stdout using the `get` finite state machine directly. //! -//! Since this example does not use `iroh-net::MagicEndpoint`, it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. +//! Since this example does not use [`iroh-net::Endpoint`], it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. //! //! Run the provide-bytes example first. It will give instructions on how to run this example properly. use std::net::SocketAddr; diff --git a/iroh-blobs/examples/fetch-stream.rs b/iroh-blobs/examples/fetch-stream.rs index 7bd51efe075..ac064d8d8b4 100644 --- a/iroh-blobs/examples/fetch-stream.rs +++ b/iroh-blobs/examples/fetch-stream.rs @@ -1,6 +1,6 @@ //! An example how to download a single blob or collection from a node and write it to stdout, using a helper method to turn the `get` finite state machine into a stream. //! -//! Since this example does not use `iroh-net::MagicEndpoint`, it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. +//! Since this example does not use [`iroh-net::Endpoint`], it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. //! //! Run the provide-bytes example first. It will give instructions on how to run this example properly. use std::net::SocketAddr; diff --git a/iroh-blobs/examples/provide-bytes.rs b/iroh-blobs/examples/provide-bytes.rs index 8668f8fe893..73f7e6d8e39 100644 --- a/iroh-blobs/examples/provide-bytes.rs +++ b/iroh-blobs/examples/provide-bytes.rs @@ -1,6 +1,6 @@ //! An example that provides a blob or a collection over a Quinn connection. //! -//! Since this example does not use `iroh-net::MagicEndpoint`, it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. +//! Since this example does not use [`iroh-net::Endpoint`], it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses. //! //! Run this example with //! cargo run --example provide-bytes blob diff --git a/iroh-blobs/src/downloader.rs b/iroh-blobs/src/downloader.rs index dc788195f40..53c70f4fe18 100644 --- a/iroh-blobs/src/downloader.rs +++ b/iroh-blobs/src/downloader.rs @@ -40,7 +40,7 @@ use std::{ use futures_lite::{future::BoxedLocal, Stream, StreamExt}; use hashlink::LinkedHashSet; use iroh_base::hash::{BlobFormat, Hash, HashAndFormat}; -use iroh_net::{magic_endpoint, MagicEndpoint, NodeAddr, NodeId}; +use iroh_net::{endpoint, Endpoint, NodeAddr, NodeId}; use tokio::{ sync::{mpsc, oneshot}, task::JoinSet, @@ -324,7 +324,7 @@ pub struct Downloader { impl Downloader { /// Create a new Downloader with the default [`ConcurrencyLimits`] and [`RetryConfig`]. - pub fn new(store: S, endpoint: MagicEndpoint, rt: LocalPoolHandle) -> Self + pub fn new(store: S, endpoint: Endpoint, rt: LocalPoolHandle) -> Self where S: Store, { @@ -334,7 +334,7 @@ impl Downloader { /// Create a new Downloader with custom [`ConcurrencyLimits`] and [`RetryConfig`]. pub fn with_config( store: S, - endpoint: MagicEndpoint, + endpoint: Endpoint, rt: LocalPoolHandle, concurrency_limits: ConcurrencyLimits, retry_config: RetryConfig, @@ -1452,7 +1452,7 @@ impl Queue { } impl Dialer for iroh_net::dialer::Dialer { - type Connection = magic_endpoint::Connection; + type Connection = endpoint::Connection; fn queue_dial(&mut self, node_id: NodeId) { self.queue_dial(node_id, crate::protocol::ALPN) diff --git a/iroh-blobs/src/downloader/get.rs b/iroh-blobs/src/downloader/get.rs index bd7cb04931d..e48370d42c3 100644 --- a/iroh-blobs/src/downloader/get.rs +++ b/iroh-blobs/src/downloader/get.rs @@ -1,6 +1,6 @@ //! [`Getter`] implementation that performs requests over [`Connection`]s. //! -//! [`Connection`]: iroh_net::magic_endpoint::Connection +//! [`Connection`]: iroh_net::endpoint::Connection use crate::{ get::{db::get_to_db, error::GetError}, @@ -9,7 +9,7 @@ use crate::{ use futures_lite::FutureExt; #[cfg(feature = "metrics")] use iroh_metrics::{inc, inc_by}; -use iroh_net::magic_endpoint; +use iroh_net::endpoint; #[cfg(feature = "metrics")] use crate::metrics::Metrics; @@ -32,13 +32,13 @@ impl From for FailureAction { /// [`Getter`] implementation that performs requests over [`Connection`]s. /// -/// [`Connection`]: iroh_net::magic_endpoint::Connection +/// [`Connection`]: iroh_net::endpoint::Connection pub(crate) struct IoGetter { pub store: S, } impl Getter for IoGetter { - type Connection = magic_endpoint::Connection; + type Connection = endpoint::Connection; fn get( &mut self, diff --git a/iroh-blobs/src/get.rs b/iroh-blobs/src/get.rs index cf9378355af..8d41e5f047c 100644 --- a/iroh-blobs/src/get.rs +++ b/iroh-blobs/src/get.rs @@ -20,7 +20,7 @@ use crate::Hash; use anyhow::Result; use bao_tree::io::fsm::BaoContentItem; use bao_tree::ChunkNum; -use iroh_net::magic_endpoint::{self, RecvStream, SendStream}; +use iroh_net::endpoint::{self, RecvStream, SendStream}; use serde::{Deserialize, Serialize}; use tracing::{debug, error}; @@ -72,7 +72,7 @@ pub mod fsm { }; use derive_more::From; use iroh_io::{AsyncSliceWriter, AsyncStreamReader, TokioStreamReader}; - use iroh_net::magic_endpoint::Connection; + use iroh_net::endpoint::Connection; use tokio::io::AsyncWriteExt; type WrappedRecvStream = TrackingReader>; @@ -143,7 +143,7 @@ pub mod fsm { } /// Initiate a new bidi stream to use for the get response - pub async fn next(self) -> Result { + pub async fn next(self) -> Result { let start = Instant::now(); let (writer, reader) = self.connection.open_bi().await?; let reader = TrackingReader::new(TokioStreamReader::new(reader)); @@ -188,7 +188,7 @@ pub mod fsm { RequestTooBig, /// Error when writing the request to the [`SendStream`]. #[error("write: {0}")] - Write(#[from] magic_endpoint::WriteError), + Write(#[from] endpoint::WriteError), /// A generic io error #[error("io {0}")] Io(io::Error), @@ -197,7 +197,7 @@ pub mod fsm { impl ConnectedNextError { fn from_io(cause: io::Error) -> Self { if let Some(inner) = cause.get_ref() { - if let Some(e) = inner.downcast_ref::() { + if let Some(e) = inner.downcast_ref::() { Self::Write(e.clone()) } else { Self::Io(cause) @@ -395,7 +395,7 @@ pub mod fsm { NotFound, /// Quinn read error when reading the size header #[error("read: {0}")] - Read(magic_endpoint::ReadError), + Read(endpoint::ReadError), /// Generic io error #[error("io: {0}")] Io(io::Error), @@ -421,7 +421,7 @@ pub mod fsm { AtBlobHeaderNextError::NotFound } else if let Some(e) = cause .get_ref() - .and_then(|x| x.downcast_ref::()) + .and_then(|x| x.downcast_ref::()) { AtBlobHeaderNextError::Read(e.clone()) } else { @@ -544,7 +544,7 @@ pub mod fsm { /// The [`DecodeError::Io`] variant is just a fallback for any other io error that /// is not actually a [`ReadError`]. /// - /// [`ReadError`]: magic_endpoint::ReadError + /// [`ReadError`]: endpoint::ReadError #[derive(Debug, thiserror::Error)] pub enum DecodeError { /// A chunk was not found or invalid, so the provider stopped sending data @@ -564,7 +564,7 @@ pub mod fsm { LeafHashMismatch(ChunkNum), /// Error when reading from the stream #[error("read: {0}")] - Read(magic_endpoint::ReadError), + Read(endpoint::ReadError), /// A generic io error #[error("io: {0}")] Io(#[from] io::Error), @@ -605,7 +605,7 @@ pub mod fsm { bao_tree::io::DecodeError::LeafHashMismatch(chunk) => Self::LeafHashMismatch(chunk), bao_tree::io::DecodeError::Io(cause) => { if let Some(inner) = cause.get_ref() { - if let Some(e) = inner.downcast_ref::() { + if let Some(e) = inner.downcast_ref::() { Self::Read(e.clone()) } else { Self::Io(cause) @@ -847,7 +847,7 @@ pub mod fsm { } /// Finish the get response, returning statistics - pub async fn next(self) -> result::Result { + pub async fn next(self) -> result::Result { // Shut down the stream let (reader, bytes_read) = self.reader.into_parts(); let mut reader = reader.into_inner(); @@ -884,13 +884,13 @@ pub mod fsm { pub enum GetResponseError { /// Error when opening a stream #[error("connection: {0}")] - Connection(#[from] magic_endpoint::ConnectionError), + Connection(#[from] endpoint::ConnectionError), /// Error when writing the handshake or request to the stream #[error("write: {0}")] - Write(#[from] magic_endpoint::WriteError), + Write(#[from] endpoint::WriteError), /// Error when reading from the stream #[error("read: {0}")] - Read(#[from] magic_endpoint::ReadError), + Read(#[from] endpoint::ReadError), /// Error when decoding, e.g. hash mismatch #[error("decode: {0}")] Decode(bao_tree::io::DecodeError), @@ -911,13 +911,13 @@ impl From for GetResponseError { bao_tree::io::DecodeError::Io(cause) => { // try to downcast to specific quinn errors if let Some(source) = cause.source() { - if let Some(error) = source.downcast_ref::() { + if let Some(error) = source.downcast_ref::() { return Self::Connection(error.clone()); } - if let Some(error) = source.downcast_ref::() { + if let Some(error) = source.downcast_ref::() { return Self::Read(error.clone()); } - if let Some(error) = source.downcast_ref::() { + if let Some(error) = source.downcast_ref::() { return Self::Write(error.clone()); } } diff --git a/iroh-blobs/src/get/db.rs b/iroh-blobs/src/get/db.rs index 5fe0a8e796b..fdfeba7d806 100644 --- a/iroh-blobs/src/get/db.rs +++ b/iroh-blobs/src/get/db.rs @@ -7,7 +7,7 @@ use std::num::NonZeroU64; use futures_lite::StreamExt; use iroh_base::hash::Hash; use iroh_base::rpc::RpcError; -use iroh_net::magic_endpoint::Connection; +use iroh_net::endpoint::Connection; use serde::{Deserialize, Serialize}; use crate::hashseq::parse_hash_seq; diff --git a/iroh-blobs/src/get/error.rs b/iroh-blobs/src/get/error.rs index fcaae08e228..62caf269653 100644 --- a/iroh-blobs/src/get/error.rs +++ b/iroh-blobs/src/get/error.rs @@ -1,6 +1,6 @@ //! Error returned from get operations -use iroh_net::magic_endpoint; +use iroh_net::endpoint; use crate::util::progress::ProgressSendError; @@ -35,10 +35,10 @@ impl From for GetError { } } -impl From for GetError { - fn from(value: magic_endpoint::ConnectionError) -> Self { +impl From for GetError { + fn from(value: endpoint::ConnectionError) -> Self { // explicit match just to be sure we are taking everything into account - use magic_endpoint::ConnectionError; + use endpoint::ConnectionError; match value { e @ ConnectionError::VersionMismatch => { // > The peer doesn't implement any supported version @@ -77,9 +77,9 @@ impl From for GetError { } } -impl From for GetError { - fn from(value: magic_endpoint::ReadError) -> Self { - use magic_endpoint::ReadError; +impl From for GetError { + fn from(value: endpoint::ReadError) -> Self { + use endpoint::ReadError; match value { e @ ReadError::Reset(_) => GetError::RemoteReset(e.into()), ReadError::ConnectionLost(conn_error) => conn_error.into(), @@ -93,9 +93,9 @@ impl From for GetError { } } -impl From for GetError { - fn from(value: magic_endpoint::WriteError) -> Self { - use magic_endpoint::WriteError; +impl From for GetError { + fn from(value: endpoint::WriteError) -> Self { + use endpoint::WriteError; match value { e @ WriteError::Stopped(_) => GetError::RemoteReset(e.into()), WriteError::ConnectionLost(conn_error) => conn_error.into(), diff --git a/iroh-blobs/src/get/request.rs b/iroh-blobs/src/get/request.rs index 27f2e208d49..a0557ec7e64 100644 --- a/iroh-blobs/src/get/request.rs +++ b/iroh-blobs/src/get/request.rs @@ -8,7 +8,7 @@ use crate::{ }; use bao_tree::{ChunkNum, ChunkRanges}; use bytes::Bytes; -use iroh_net::magic_endpoint::Connection; +use iroh_net::endpoint::Connection; use rand::Rng; use super::{fsm, Stats}; diff --git a/iroh-blobs/src/protocol.rs b/iroh-blobs/src/protocol.rs index 5579022de0a..0a5e2a7ca23 100644 --- a/iroh-blobs/src/protocol.rs +++ b/iroh-blobs/src/protocol.rs @@ -340,7 +340,7 @@ //! keep a connection open and reuse it for multiple requests. use bao_tree::{ChunkNum, ChunkRanges}; use derive_more::From; -use iroh_net::magic_endpoint::VarInt; +use iroh_net::endpoint::VarInt; use serde::{Deserialize, Serialize}; mod range_spec; pub use range_spec::{NonEmptyRequestRangeSpecIter, RangeSpec, RangeSpecSeq}; @@ -433,8 +433,8 @@ pub enum Closed { /// [`RecvStream::stop`]. We don't use this explicitly but this is here as /// documentation as to what happened to `0`. /// - /// [`RecvStream`]: iroh_net::magic_endpoint::RecvStream - /// [`RecvStream::stop`]: iroh_net::magic_endpoint::RecvStream::stop + /// [`RecvStream`]: iroh_net::endpoint::RecvStream + /// [`RecvStream::stop`]: iroh_net::endpoint::RecvStream::stop StreamDropped = 0, /// The provider is terminating. /// diff --git a/iroh-blobs/src/provider.rs b/iroh-blobs/src/provider.rs index 854964ffd48..7fe4e13004a 100644 --- a/iroh-blobs/src/provider.rs +++ b/iroh-blobs/src/provider.rs @@ -11,7 +11,7 @@ use iroh_io::stats::{ SliceReaderStats, StreamWriterStats, TrackingSliceReader, TrackingStreamWriter, }; use iroh_io::{AsyncSliceReader, AsyncStreamWriter, TokioStreamWriter}; -use iroh_net::magic_endpoint::{self, RecvStream, SendStream}; +use iroh_net::endpoint::{self, RecvStream, SendStream}; use serde::{Deserialize, Serialize}; use tokio_util::task::LocalPoolHandle; use tracing::{debug, debug_span, info, trace, warn}; @@ -281,7 +281,7 @@ pub trait EventSender: Clone + Sync + Send + 'static { /// Handle a single connection. pub async fn handle_connection( - connection: magic_endpoint::Connection, + connection: endpoint::Connection, db: D, events: E, rt: LocalPoolHandle, diff --git a/iroh-cli/src/commands/doctor.rs b/iroh-cli/src/commands/doctor.rs index e1e8e31e31b..01445e2d337 100644 --- a/iroh-cli/src/commands/doctor.rs +++ b/iroh-cli/src/commands/doctor.rs @@ -32,13 +32,13 @@ use iroh::{ dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery, Discovery, }, dns::default_resolver, + endpoint::{self, Connection, ConnectionTypeStream, RecvStream, SendStream}, key::{PublicKey, SecretKey}, - magic_endpoint::{self, Connection, ConnectionTypeStream, RecvStream, SendStream}, netcheck, portmapper, relay::{RelayMap, RelayMode, RelayUrl}, ticket::NodeTicket, util::CancelOnDrop, - MagicEndpoint, NodeAddr, NodeId, + Endpoint, NodeAddr, NodeId, }, util::{path::IrohPaths, progress::ProgressWriter}, }; @@ -375,7 +375,7 @@ struct Gui { } impl Gui { - fn new(endpoint: MagicEndpoint, node_id: NodeId) -> Self { + fn new(endpoint: Endpoint, node_id: NodeId) -> Self { let mp = MultiProgress::new(); mp.set_draw_target(indicatif::ProgressDrawTarget::stderr()); let counters = mp.add(ProgressBar::hidden()); @@ -418,13 +418,13 @@ impl Gui { } } - fn update_connection_info(target: &ProgressBar, endpoint: &MagicEndpoint, node_id: &NodeId) { + fn update_connection_info(target: &ProgressBar, endpoint: &Endpoint, node_id: &NodeId) { let format_latency = |x: Option| { x.map(|x| format!("{:.6}s", x.as_secs_f64())) .unwrap_or_else(|| "unknown".to_string()) }; let msg = match endpoint.connection_info(*node_id) { - Some(magic_endpoint::ConnectionInfo { + Some(endpoint::ConnectionInfo { relay_url, conn_type, latency, @@ -642,18 +642,18 @@ async fn make_endpoint( secret_key: SecretKey, relay_map: Option, discovery: Option>, -) -> anyhow::Result { +) -> anyhow::Result { tracing::info!( "public key: {}", hex::encode(secret_key.public().as_bytes()) ); tracing::info!("relay map {:#?}", relay_map); - let mut transport_config = magic_endpoint::TransportConfig::default(); + let mut transport_config = endpoint::TransportConfig::default(); transport_config.keep_alive_interval(Some(Duration::from_secs(5))); transport_config.max_idle_timeout(Some(Duration::from_secs(10).try_into().unwrap())); - let endpoint = MagicEndpoint::builder() + let endpoint = Endpoint::builder() .secret_key(secret_key) .alpns(vec![DR_RELAY_ALPN.to_vec()]) .transport_config(transport_config); @@ -764,8 +764,7 @@ async fn accept( match connecting.await { Ok(connection) => { if n == 0 { - let Ok(remote_peer_id) = magic_endpoint::get_remote_node_id(&connection) - else { + let Ok(remote_peer_id) = endpoint::get_remote_node_id(&connection) else { return; }; println!("Accepted connection from {}", remote_peer_id); diff --git a/iroh-cli/src/commands/node.rs b/iroh-cli/src/commands/node.rs index d4136ad3690..4d2b8ad1bf8 100644 --- a/iroh-cli/src/commands/node.rs +++ b/iroh-cli/src/commands/node.rs @@ -10,8 +10,8 @@ use human_time::ToHumanTimeString; use iroh::client::Iroh; use iroh::client::RpcService; use iroh::net::{ + endpoint::{ConnectionInfo, DirectAddrInfo}, key::PublicKey, - magic_endpoint::{ConnectionInfo, DirectAddrInfo}, }; use quic_rpc::ServiceConnection; diff --git a/iroh-docs/src/net.rs b/iroh-docs/src/net.rs index a71b0921af7..a3f90032e1a 100644 --- a/iroh-docs/src/net.rs +++ b/iroh-docs/src/net.rs @@ -5,7 +5,7 @@ use std::{ time::{Duration, Instant}, }; -use iroh_net::{key::PublicKey, magic_endpoint::get_remote_node_id, MagicEndpoint, NodeAddr}; +use iroh_net::{endpoint::get_remote_node_id, key::PublicKey, Endpoint, NodeAddr}; use serde::{Deserialize, Serialize}; use tracing::{debug, error_span, trace, Instrument}; @@ -27,7 +27,7 @@ mod codec; /// Connect to a peer and sync a replica pub async fn connect_and_sync( - endpoint: &MagicEndpoint, + endpoint: &Endpoint, sync: &SyncHandle, namespace: NamespaceId, peer: NodeAddr, @@ -106,7 +106,7 @@ pub enum AcceptOutcome { /// Handle an iroh-docs connection and sync all shared documents in the replica store. pub async fn handle_connection( sync: SyncHandle, - connecting: iroh_net::magic_endpoint::Connecting, + connecting: iroh_net::endpoint::Connecting, accept_cb: F, ) -> Result where diff --git a/iroh-gossip/examples/chat.rs b/iroh-gossip/examples/chat.rs index 4a8eecbc4e5..d3631f10c6c 100644 --- a/iroh-gossip/examples/chat.rs +++ b/iroh-gossip/examples/chat.rs @@ -12,7 +12,7 @@ use iroh_gossip::{ use iroh_net::{ key::{PublicKey, SecretKey}, relay::{RelayMap, RelayMode, RelayUrl}, - MagicEndpoint, NodeAddr, + Endpoint, NodeAddr, }; use serde::{Deserialize, Serialize}; @@ -100,7 +100,7 @@ async fn main() -> anyhow::Result<()> { println!("> using relay servers: {}", fmt_relay_mode(&relay_mode)); // build our magic endpoint - let endpoint = MagicEndpoint::builder() + let endpoint = Endpoint::builder() .secret_key(secret_key) .alpns(vec![GOSSIP_ALPN.to_vec()]) .relay_mode(relay_mode) @@ -189,7 +189,7 @@ async fn subscribe_loop(gossip: Gossip, topic: TopicId) -> anyhow::Result<()> { } } -async fn endpoint_loop(endpoint: MagicEndpoint, gossip: Gossip) { +async fn endpoint_loop(endpoint: Endpoint, gossip: Gossip) { while let Some(conn) = endpoint.accept().await { let gossip = gossip.clone(); tokio::spawn(async move { @@ -200,12 +200,12 @@ async fn endpoint_loop(endpoint: MagicEndpoint, gossip: Gossip) { } } async fn handle_connection( - mut conn: iroh_net::magic_endpoint::Connecting, + mut conn: iroh_net::endpoint::Connecting, gossip: Gossip, ) -> anyhow::Result<()> { let alpn = conn.alpn().await?; let conn = conn.await?; - let peer_id = iroh_net::magic_endpoint::get_remote_node_id(&conn)?; + let peer_id = iroh_net::endpoint::get_remote_node_id(&conn)?; match alpn.as_bytes() { GOSSIP_ALPN => gossip .handle_connection(conn) diff --git a/iroh-gossip/src/net.rs b/iroh-gossip/src/net.rs index ec72f65466e..e415e68c69d 100644 --- a/iroh-gossip/src/net.rs +++ b/iroh-gossip/src/net.rs @@ -6,9 +6,9 @@ use futures_lite::stream::Stream; use genawaiter::sync::{Co, Gen}; use iroh_net::{ dialer::Dialer, + endpoint::{get_remote_node_id, Connection}, key::PublicKey, - magic_endpoint::{get_remote_node_id, Connection}, - AddrInfo, MagicEndpoint, NodeAddr, + AddrInfo, Endpoint, NodeAddr, }; use rand::rngs::StdRng; use rand_core::SeedableRng; @@ -65,8 +65,8 @@ type ProtoMessage = proto::Message; /// /// With the default settings, the protocol will maintain up to 5 peer connections per topic. /// -/// Even though the [`Gossip`] is created from a [`MagicEndpoint`], it does not accept connections -/// itself. You should run an accept loop on the MagicEndpoint yourself, check the ALPN protocol of incoming +/// Even though the [`Gossip`] is created from a [`Endpoint`], it does not accept connections +/// itself. You should run an accept loop on the [`Endpoint`] yourself, check the ALPN protocol of incoming /// connections, and if the ALPN protocol equals [`GOSSIP_ALPN`], forward the connection to the /// gossip actor through [Self::handle_connection]. /// @@ -80,11 +80,7 @@ pub struct Gossip { impl Gossip { /// Spawn a gossip actor and get a handle for it - pub fn from_endpoint( - endpoint: MagicEndpoint, - config: proto::Config, - my_addr: &AddrInfo, - ) -> Self { + pub fn from_endpoint(endpoint: Endpoint, config: proto::Config, my_addr: &AddrInfo) -> Self { let peer_id = endpoint.node_id(); let dialer = Dialer::new(endpoint.clone()); let state = proto::State::new( @@ -136,8 +132,8 @@ impl Gossip { /// /// /// This method only asks for [`PublicKey`]s. You must supply information on how to - /// connect to these peers manually before, by calling [`MagicEndpoint::add_node_addr`] on - /// the underlying [`MagicEndpoint`]. + /// connect to these peers manually before, by calling [`Endpoint::add_node_addr`] on + /// the underlying [`Endpoint`]. /// /// This method returns a future that completes once the request reached the local actor. /// This completion returns a [`JoinTopicFut`] which completes once at least peer was joined @@ -332,7 +328,7 @@ enum ToActor { struct Actor { /// Protocol state state: proto::State, - endpoint: MagicEndpoint, + endpoint: Endpoint, /// Dial machine to connect to peers dialer: Dialer, /// Input messages to the actor @@ -665,8 +661,8 @@ mod test { use super::*; - async fn create_endpoint(relay_map: RelayMap) -> anyhow::Result { - MagicEndpoint::builder() + async fn create_endpoint(relay_map: RelayMap) -> anyhow::Result { + Endpoint::builder() .alpns(vec![GOSSIP_ALPN.to_vec()]) .relay_mode(RelayMode::Custom(relay_map)) .bind(0) @@ -674,7 +670,7 @@ mod test { } async fn endpoint_loop( - endpoint: MagicEndpoint, + endpoint: Endpoint, gossip: Gossip, cancel: CancellationToken, ) -> anyhow::Result<()> { @@ -848,9 +844,9 @@ mod test { /// Runs a relay server with STUN enabled suitable for tests. /// /// The returned `Url` is the url of the relay server in the returned [`RelayMap`], it - /// is always `Some` as that is how the [`MagicEndpoint::connect`] API expects it. + /// is always `Some` as that is how the [`Endpoint::connect`] API expects it. /// - /// [`MagicEndpoint::connect`]: crate::magic_endpoint::MagicEndpoint + /// [`Endpoint::connect`]: crate::endpoint::Endpoint pub(crate) async fn run_relay_and_stun( stun_ip: IpAddr, ) -> Result<(RelayMap, RelayUrl, CleanupDropGuard)> { diff --git a/iroh-net/README.md b/iroh-net/README.md index 0ad1fb9f48e..02618f0aa3a 100644 --- a/iroh-net/README.md +++ b/iroh-net/README.md @@ -1,6 +1,6 @@ # iroh-net -This crate contains the networking support for iroh. Iroh networking is built on direct peer-to-peer [QUIC](https://en.wikipedia.org/wiki/QUIC) connections that use relays and holepunching. The main structure for connection is the `MagicEndpoint` entrypoint. +This crate contains the networking support for iroh. Iroh networking is built on direct peer-to-peer [QUIC](https://en.wikipedia.org/wiki/QUIC) connections that use relays and holepunching. The main structure for connection is the `Endpoint` entrypoint. Peer to peer connectivity is established with the help of a _relay server_. The relay server provides Session Traversal Utilities for NAT [(STUN)](https://en.wikipedia.org/wiki/STUN) for the peers and connection coordination using the [DERP protocol](https://pkg.go.dev/tailscale.com/derp) (Designated Relay for Encrypted Packets protocol). If no direct connection can be established, the connection is relayed via the server. diff --git a/iroh-net/bench/src/bin/bulk.rs b/iroh-net/bench/src/bin/bulk.rs index 3639d98cf6a..f5de15ea397 100644 --- a/iroh-net/bench/src/bin/bulk.rs +++ b/iroh-net/bench/src/bin/bulk.rs @@ -6,8 +6,8 @@ use std::{ use anyhow::{Context, Result}; use clap::Parser; use iroh_net::{ - magic_endpoint::{self, Connection}, - MagicEndpoint, NodeAddr, + endpoint::{self, Connection}, + Endpoint, NodeAddr, }; use tokio::sync::Semaphore; use tracing::{info, trace}; @@ -64,7 +64,7 @@ fn main() { server_thread.join().expect("server thread"); } -async fn server(endpoint: MagicEndpoint, opt: Opt) -> Result<()> { +async fn server(endpoint: Endpoint, opt: Opt) -> Result<()> { let mut server_tasks = Vec::new(); // Handle only the expected amount of clients @@ -75,7 +75,7 @@ async fn server(endpoint: MagicEndpoint, opt: Opt) -> Result<()> { server_tasks.push(tokio::spawn(async move { loop { let (mut send_stream, mut recv_stream) = match connection.accept_bi().await { - Err(magic_endpoint::ConnectionError::ApplicationClosed(_)) => break, + Err(endpoint::ConnectionError::ApplicationClosed(_)) => break, Err(e) => { eprintln!("accepting stream failed: {e:?}"); break; diff --git a/iroh-net/bench/src/lib.rs b/iroh-net/bench/src/lib.rs index e317aed0f3d..25c049ce8c6 100644 --- a/iroh-net/bench/src/lib.rs +++ b/iroh-net/bench/src/lib.rs @@ -3,8 +3,8 @@ use std::{net::SocketAddr, num::ParseIntError, str::FromStr}; use anyhow::{Context, Result}; use bytes::Bytes; use clap::Parser; -use iroh_net::magic_endpoint::{self, Connection, RecvStream, SendStream}; -use iroh_net::{relay::RelayMode, MagicEndpoint, NodeAddr}; +use iroh_net::endpoint::{self, Connection, RecvStream, SendStream}; +use iroh_net::{relay::RelayMode, Endpoint, NodeAddr}; use tokio::runtime::{Builder, Runtime}; use tracing::trace; @@ -22,10 +22,10 @@ pub fn configure_tracing_subscriber() { } /// Creates a server endpoint which runs on the given runtime -pub fn server_endpoint(rt: &tokio::runtime::Runtime, opt: &Opt) -> (NodeAddr, MagicEndpoint) { +pub fn server_endpoint(rt: &tokio::runtime::Runtime, opt: &Opt) -> (NodeAddr, Endpoint) { let _guard = rt.enter(); rt.block_on(async move { - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .alpns(vec![ALPN.to_vec()]) .relay_mode(RelayMode::Disabled) .transport_config(transport_config(opt)) @@ -40,11 +40,8 @@ pub fn server_endpoint(rt: &tokio::runtime::Runtime, opt: &Opt) -> (NodeAddr, Ma } /// Create a client endpoint and client connection -pub async fn connect_client( - server_addr: NodeAddr, - opt: Opt, -) -> Result<(MagicEndpoint, Connection)> { - let endpoint = MagicEndpoint::builder() +pub async fn connect_client(server_addr: NodeAddr, opt: Opt) -> Result<(Endpoint, Connection)> { + let endpoint = Endpoint::builder() .alpns(vec![ALPN.to_vec()]) .relay_mode(RelayMode::Disabled) .transport_config(transport_config(&opt)) @@ -124,10 +121,10 @@ pub fn rt() -> Runtime { Builder::new_current_thread().enable_all().build().unwrap() } -pub fn transport_config(opt: &Opt) -> magic_endpoint::TransportConfig { +pub fn transport_config(opt: &Opt) -> endpoint::TransportConfig { // High stream windows are chosen because the amount of concurrent streams // is configurable as a parameter. - let mut config = magic_endpoint::TransportConfig::default(); + let mut config = endpoint::TransportConfig::default(); config.max_concurrent_uni_streams(opt.max_streams.try_into().unwrap()); config.initial_mtu(opt.initial_mtu); diff --git a/iroh-net/examples/connect-unreliable.rs b/iroh-net/examples/connect-unreliable.rs index 1a8ce141a7e..bd92cf35854 100644 --- a/iroh-net/examples/connect-unreliable.rs +++ b/iroh-net/examples/connect-unreliable.rs @@ -1,4 +1,4 @@ -//! The smallest example showing how to use iroh-net and `MagicEndpoint` to connect to a remote node and pass bytes using unreliable datagrams. +//! The smallest example showing how to use iroh-net and [`iroh_net::Endpoint`] to connect to a remote node and pass bytes using unreliable datagrams. //! //! We use the node ID (the PublicKey of the remote node), the direct UDP addresses, and the relay url to achieve a connection. //! @@ -14,11 +14,11 @@ use iroh_base::base32; use iroh_net::{ key::SecretKey, relay::{RelayMode, RelayUrl}, - MagicEndpoint, NodeAddr, + Endpoint, NodeAddr, }; use tracing::info; -// An example ALPN that we are using to communicate over the `MagicEndpoint` +// An example ALPN that we are using to communicate over the `Endpoint` const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/magic/0"; #[derive(Debug, Parser)] @@ -42,8 +42,8 @@ async fn main() -> anyhow::Result<()> { let secret_key = SecretKey::generate(); println!("secret key: {}", base32::fmt(secret_key.to_bytes())); - // Build a `MagicEndpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. - let endpoint = MagicEndpoint::builder() + // Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. + let endpoint = Endpoint::builder() // The secret key is used to authenticate with other nodes. The PublicKey portion of this secret key is how we identify nodes, often referred to as the `node_id` in our codebase. .secret_key(secret_key) // Set the ALPN protocols this endpoint will accept on incoming connections diff --git a/iroh-net/examples/connect.rs b/iroh-net/examples/connect.rs index 3d9146ac866..278f020ca77 100644 --- a/iroh-net/examples/connect.rs +++ b/iroh-net/examples/connect.rs @@ -1,4 +1,4 @@ -//! The smallest example showing how to use iroh-net and `MagicEndpoint` to connect to a remote node. +//! The smallest example showing how to use iroh-net and [`iroh_net::Endpoint`] to connect to a remote node. //! //! We use the node ID (the PublicKey of the remote node), the direct UDP addresses, and the relay url to achieve a connection. //! @@ -12,10 +12,10 @@ use clap::Parser; use futures_lite::StreamExt; use iroh_base::base32; use iroh_net::relay::RelayUrl; -use iroh_net::{key::SecretKey, relay::RelayMode, MagicEndpoint, NodeAddr}; +use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint, NodeAddr}; use tracing::info; -// An example ALPN that we are using to communicate over the `MagicEndpoint` +// An example ALPN that we are using to communicate over the `Endpoint` const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/magic/0"; #[derive(Debug, Parser)] @@ -39,8 +39,8 @@ async fn main() -> anyhow::Result<()> { let secret_key = SecretKey::generate(); println!("secret key: {}", base32::fmt(secret_key.to_bytes())); - // Build a `MagicEndpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. - let endpoint = MagicEndpoint::builder() + // Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. + let endpoint = Endpoint::builder() // The secret key is used to authenticate with other nodes. The PublicKey portion of this secret key is how we identify nodes, often referred to as the `node_id` in our codebase. .secret_key(secret_key) // Set the ALPN protocols this endpoint will accept on incoming connections diff --git a/iroh-net/examples/listen-unreliable.rs b/iroh-net/examples/listen-unreliable.rs index 58a494d2dca..a4fe0899faa 100644 --- a/iroh-net/examples/listen-unreliable.rs +++ b/iroh-net/examples/listen-unreliable.rs @@ -1,4 +1,4 @@ -//! The smallest example showing how to use iroh-net and `MagicEndpoint` to connect two devices and pass bytes using unreliable datagrams. +//! The smallest example showing how to use iroh-net and [`iroh_net::Endpoint`] to connect two devices and pass bytes using unreliable datagrams. //! //! This example uses the default relay servers to attempt to holepunch, and will use that relay server to relay packets if the two devices cannot establish a direct UDP connection. //! run this example from the project root: @@ -6,10 +6,10 @@ use anyhow::Context; use futures_lite::StreamExt; use iroh_base::base32; -use iroh_net::{key::SecretKey, relay::RelayMode, MagicEndpoint}; +use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint}; use tracing::info; -// An example ALPN that we are using to communicate over the `MagicEndpoint` +// An example ALPN that we are using to communicate over the `Endpoint` const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/magic/0"; #[tokio::main] @@ -19,8 +19,8 @@ async fn main() -> anyhow::Result<()> { let secret_key = SecretKey::generate(); println!("secret key: {}", base32::fmt(secret_key.to_bytes())); - // Build a `MagicEndpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. - let endpoint = MagicEndpoint::builder() + // Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. + let endpoint = Endpoint::builder() // The secret key is used to authenticate with other nodes. The PublicKey portion of this secret key is how we identify nodes, often referred to as the `node_id` in our codebase. .secret_key(secret_key) // set the ALPN protocols this endpoint will accept on incoming connections @@ -66,7 +66,7 @@ async fn main() -> anyhow::Result<()> { while let Some(mut conn) = endpoint.accept().await { let alpn = conn.alpn().await?; let conn = conn.await?; - let node_id = iroh_net::magic_endpoint::get_remote_node_id(&conn)?; + let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?; info!( "new (unreliable) connection from {node_id} with ALPN {alpn} (coming from {})", conn.remote_address() diff --git a/iroh-net/examples/listen.rs b/iroh-net/examples/listen.rs index ccee87df8a7..87f29b212b0 100644 --- a/iroh-net/examples/listen.rs +++ b/iroh-net/examples/listen.rs @@ -1,4 +1,4 @@ -//! The smallest example showing how to use iroh-net and `MagicEndpoint` to connect two devices. +//! The smallest example showing how to use iroh-net and [`iroh_net::MagicEndpoint`] to connect two devices. //! //! This example uses the default relay servers to attempt to holepunch, and will use that relay server to relay packets if the two devices cannot establish a direct UDP connection. //! run this example from the project root: @@ -6,10 +6,10 @@ use anyhow::Context; use futures_lite::StreamExt; use iroh_base::base32; -use iroh_net::{key::SecretKey, relay::RelayMode, MagicEndpoint}; +use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint}; use tracing::{debug, info}; -// An example ALPN that we are using to communicate over the `MagicEndpoint` +// An example ALPN that we are using to communicate over the `Endpoint` const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/magic/0"; #[tokio::main] @@ -19,8 +19,8 @@ async fn main() -> anyhow::Result<()> { let secret_key = SecretKey::generate(); println!("secret key: {}", base32::fmt(secret_key.to_bytes())); - // Build a `MagicEndpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. - let endpoint = MagicEndpoint::builder() + // Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers. + let endpoint = Endpoint::builder() // The secret key is used to authenticate with other nodes. The PublicKey portion of this secret key is how we identify nodes, often referred to as the `node_id` in our codebase. .secret_key(secret_key) // set the ALPN protocols this endpoint will accept on incoming connections @@ -65,7 +65,7 @@ async fn main() -> anyhow::Result<()> { while let Some(mut conn) = endpoint.accept().await { let alpn = conn.alpn().await?; let conn = conn.await?; - let node_id = iroh_net::magic_endpoint::get_remote_node_id(&conn)?; + let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?; info!( "new connection from {node_id} with ALPN {alpn} (coming from {})", conn.remote_address() diff --git a/iroh-net/src/dialer.rs b/iroh-net/src/dialer.rs index f3b5e737b69..0dc21179d23 100644 --- a/iroh-net/src/dialer.rs +++ b/iroh-net/src/dialer.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, pin::Pin, task::Poll}; -use crate::{key::PublicKey, MagicEndpoint, NodeAddr, NodeId}; +use crate::{key::PublicKey, Endpoint, NodeAddr, NodeId}; use anyhow::anyhow; use futures_lite::future::Boxed as BoxFuture; use tokio::task::JoinSet; @@ -11,18 +11,18 @@ use tracing::error; /// Dial nodes and maintain a queue of pending dials /// -/// This wraps a [`MagicEndpoint`], connects to nodes through the endpoint, stores +/// This wraps a [`Endpoint`], connects to nodes through the endpoint, stores /// the pending connect futures and emits finished connect results. #[derive(Debug)] pub struct Dialer { - endpoint: MagicEndpoint, + endpoint: Endpoint, pending: JoinSet<(PublicKey, anyhow::Result)>, pending_dials: HashMap, } impl Dialer { - /// Create a new dialer for a [`MagicEndpoint`] - pub fn new(endpoint: MagicEndpoint) -> Self { + /// Create a new dialer for a [`Endpoint`] + pub fn new(endpoint: Endpoint) -> Self { Self { endpoint, pending: Default::default(), @@ -33,7 +33,7 @@ impl Dialer { /// Start to dial a node. /// /// Note that the node's addresses and/or relay url must be added to the endpoint's - /// addressbook for a dial to succeed, see [`MagicEndpoint::add_node_addr`]. + /// addressbook for a dial to succeed, see [`Endpoint::add_node_addr`]. pub fn queue_dial(&mut self, node_id: NodeId, alpn: &'static [u8]) { if self.is_pending(&node_id) { return; diff --git a/iroh-net/src/discovery.rs b/iroh-net/src/discovery.rs index 6b06799f4c4..0fba47ca294 100644 --- a/iroh-net/src/discovery.rs +++ b/iroh-net/src/discovery.rs @@ -8,18 +8,18 @@ use iroh_base::node_addr::NodeAddr; use tokio::{sync::oneshot, task::JoinHandle}; use tracing::{debug, error_span, warn, Instrument}; -use crate::{AddrInfo, MagicEndpoint, NodeId}; +use crate::{AddrInfo, Endpoint, NodeId}; pub mod dns; pub mod pkarr_publish; -/// Node discovery for [`super::MagicEndpoint`]. +/// Node discovery for [`super::Endpoint`]. /// /// The purpose of this trait is to hook up a node discovery mechanism that /// allows finding information such as the relay URL and direct addresses /// of a node given its [`NodeId`]. /// -/// To allow for discovery, the [`super::MagicEndpoint`] will call `publish` whenever +/// To allow for discovery, the [`super::Endpoint`] will call `publish` whenever /// discovery information changes. If a discovery mechanism requires a periodic /// refresh, it should start its own task. pub trait Discovery: std::fmt::Debug + Send + Sync { @@ -30,7 +30,7 @@ pub trait Discovery: std::fmt::Debug + Send + Sync { /// own task. /// /// This will be called from a tokio task, so it is safe to spawn new tasks. - /// These tasks will be run on the runtime of the [`super::MagicEndpoint`]. + /// These tasks will be run on the runtime of the [`super::Endpoint`]. fn publish(&self, _info: &AddrInfo) {} /// Resolve the [`AddrInfo`] for the given [`NodeId`]. @@ -39,7 +39,7 @@ pub trait Discovery: std::fmt::Debug + Send + Sync { /// work. fn resolve( &self, - _endpoint: MagicEndpoint, + _endpoint: Endpoint, _node_id: NodeId, ) -> Option>> { None @@ -105,7 +105,7 @@ impl Discovery for ConcurrentDiscovery { fn resolve( &self, - endpoint: MagicEndpoint, + endpoint: Endpoint, node_id: NodeId, ) -> Option>> { let streams = self @@ -130,7 +130,7 @@ pub(super) struct DiscoveryTask { impl DiscoveryTask { /// Start a discovery task. - pub fn start(ep: MagicEndpoint, node_id: NodeId) -> Result { + pub fn start(ep: Endpoint, node_id: NodeId) -> Result { ensure!(ep.discovery().is_some(), "No discovery services configured"); let (on_first_tx, on_first_rx) = oneshot::channel(); let me = ep.node_id(); @@ -151,7 +151,7 @@ impl DiscoveryTask { /// if we recently received messages from remote endpoint. If true, the task will abort. /// Otherwise, or if no `delay` is set, the discovery will be started. pub fn maybe_start_after_delay( - ep: &MagicEndpoint, + ep: &Endpoint, node_id: NodeId, delay: Option, ) -> Result> { @@ -195,10 +195,7 @@ impl DiscoveryTask { self.task.abort(); } - fn create_stream( - ep: &MagicEndpoint, - node_id: NodeId, - ) -> Result>> { + fn create_stream(ep: &Endpoint, node_id: NodeId) -> Result>> { let discovery = ep .discovery() .ok_or_else(|| anyhow!("No discovery service configured"))?; @@ -210,7 +207,7 @@ impl DiscoveryTask { /// We need discovery if we have no paths to the node, or if the paths we do have /// have timed out. - fn needs_discovery(ep: &MagicEndpoint, node_id: NodeId) -> bool { + fn needs_discovery(ep: &Endpoint, node_id: NodeId) -> bool { match ep.connection_info(node_id) { // No connection info means no path to node -> start discovery. None => true, @@ -229,7 +226,7 @@ impl DiscoveryTask { } } - async fn run(ep: MagicEndpoint, node_id: NodeId, on_first_tx: oneshot::Sender>) { + async fn run(ep: Endpoint, node_id: NodeId, on_first_tx: oneshot::Sender>) { let mut stream = match Self::create_stream(&ep, node_id) { Ok(stream) => stream, Err(err) => { @@ -344,7 +341,7 @@ mod tests { fn resolve( &self, - endpoint: MagicEndpoint, + endpoint: Endpoint, node_id: NodeId, ) -> Option>> { let addr_info = match self.resolve_wrong { @@ -393,7 +390,7 @@ mod tests { fn resolve( &self, - _endpoint: MagicEndpoint, + _endpoint: Endpoint, _node_id: NodeId, ) -> Option>> { Some(futures_lite::stream::empty().boxed()) @@ -404,7 +401,7 @@ mod tests { /// This is a smoke test for our discovery mechanism. #[tokio::test] - async fn magic_endpoint_discovery_simple_shared() -> anyhow::Result<()> { + async fn endpoint_discovery_simple_shared() -> anyhow::Result<()> { let _guard = iroh_test::logging::setup(); let disco_shared = TestDiscoveryShared::default(); let ep1 = { @@ -426,7 +423,7 @@ mod tests { /// This test adds an empty discovery which provides no addresses. #[tokio::test] - async fn magic_endpoint_discovery_combined_with_empty() -> anyhow::Result<()> { + async fn endpoint_discovery_combined_with_empty() -> anyhow::Result<()> { let _guard = iroh_test::logging::setup(); let disco_shared = TestDiscoveryShared::default(); let ep1 = { @@ -454,7 +451,7 @@ mod tests { /// This is to make sure that as long as one of the discoveries returns a working address, we /// will connect successfully. #[tokio::test] - async fn magic_endpoint_discovery_combined_with_empty_and_wrong() -> anyhow::Result<()> { + async fn endpoint_discovery_combined_with_empty_and_wrong() -> anyhow::Result<()> { let _guard = iroh_test::logging::setup(); let disco_shared = TestDiscoveryShared::default(); let ep1 = { @@ -482,7 +479,7 @@ mod tests { /// This test only has the "lying" discovery. It is here to make sure that this actually fails. #[tokio::test] - async fn magic_endpoint_discovery_combined_wrong_only() -> anyhow::Result<()> { + async fn endpoint_discovery_combined_wrong_only() -> anyhow::Result<()> { let _guard = iroh_test::logging::setup(); let disco_shared = TestDiscoveryShared::default(); let ep1 = { @@ -507,7 +504,7 @@ mod tests { /// This test first adds a wrong address manually (e.g. from an outdated&node_id ticket). /// Connect should still succeed because the discovery service will be invoked (after a delay). #[tokio::test] - async fn magic_endpoint_discovery_with_wrong_existing_addr() -> anyhow::Result<()> { + async fn endpoint_discovery_with_wrong_existing_addr() -> anyhow::Result<()> { let _guard = iroh_test::logging::setup(); let disco_shared = TestDiscoveryShared::default(); let ep1 = { @@ -533,8 +530,8 @@ mod tests { Ok(()) } - async fn new_endpoint(secret: SecretKey, disco: impl Discovery + 'static) -> MagicEndpoint { - MagicEndpoint::builder() + async fn new_endpoint(secret: SecretKey, disco: impl Discovery + 'static) -> Endpoint { + Endpoint::builder() .secret_key(secret) .discovery(Box::new(disco)) .relay_mode(RelayMode::Disabled) @@ -572,7 +569,7 @@ mod test_dns_pkarr { pkarr_dns_state::State, run_relay_server, DnsPkarrServer, }, - AddrInfo, MagicEndpoint, NodeAddr, + AddrInfo, Endpoint, NodeAddr, }; #[tokio::test] @@ -686,9 +683,9 @@ mod test_dns_pkarr { async fn ep_with_discovery( relay_map: &RelayMap, dns_pkarr_server: &DnsPkarrServer, - ) -> Result { + ) -> Result { let secret_key = SecretKey::generate(); - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .relay_mode(RelayMode::Custom(relay_map.clone())) .insecure_skip_relay_cert_verify(true) .secret_key(secret_key.clone()) diff --git a/iroh-net/src/discovery/dns.rs b/iroh-net/src/discovery/dns.rs index e791edc7d30..c07bab08070 100644 --- a/iroh-net/src/discovery/dns.rs +++ b/iroh-net/src/discovery/dns.rs @@ -5,7 +5,7 @@ use futures_lite::stream::Boxed as BoxStream; use crate::{ discovery::{Discovery, DiscoveryItem}, - dns, MagicEndpoint, NodeId, + dns, Endpoint, NodeId, }; /// The n0 testing DNS node origin @@ -15,7 +15,7 @@ pub const N0_DNS_NODE_ORIGIN: &str = "dns.iroh.link"; /// /// When asked to resolve a [`NodeId`], this service performs a lookup in the Domain Name System (DNS). /// -/// It uses the [`MagicEndpoint`]'s DNS resolver to query for `TXT` records under the domain +/// It uses the [`Endpoint`]'s DNS resolver to query for `TXT` records under the domain /// `_iroh..`: /// /// * `_iroh`: is the record name @@ -28,7 +28,7 @@ pub const N0_DNS_NODE_ORIGIN: &str = "dns.iroh.link"; /// * `relay=`: The URL of the home relay server of the node /// /// The DNS resolver defaults to using the nameservers configured on the host system, but can be changed -/// with [`crate::magic_endpoint::MagicEndpointBuilder::dns_resolver`]. +/// with [`crate::endpoint::EndpointBuilder::dns_resolver`]. /// /// [z-base-32]: https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt #[derive(Debug)] @@ -49,11 +49,7 @@ impl DnsDiscovery { } impl Discovery for DnsDiscovery { - fn resolve( - &self, - ep: MagicEndpoint, - node_id: NodeId, - ) -> Option>> { + fn resolve(&self, ep: Endpoint, node_id: NodeId) -> Option>> { let resolver = ep.dns_resolver().clone(); let origin_domain = self.origin_domain.clone(); let fut = async move { diff --git a/iroh-net/src/dns.rs b/iroh-net/src/dns.rs index 14c007060f2..ce31f5d443b 100644 --- a/iroh-net/src/dns.rs +++ b/iroh-net/src/dns.rs @@ -1,5 +1,5 @@ //! This module exports a DNS resolver, which is also the default resolver used in the -//! [`crate::MagicEndpoint`] if no custom resolver is configured. +//! [`crate::Endpoint`] if no custom resolver is configured. use std::net::{IpAddr, Ipv6Addr}; use std::time::Duration; diff --git a/iroh-net/src/magic_endpoint.rs b/iroh-net/src/endpoint.rs similarity index 94% rename from iroh-net/src/magic_endpoint.rs rename to iroh-net/src/endpoint.rs index 77ed98b81a6..ba9320f6ed8 100644 --- a/iroh-net/src/magic_endpoint.rs +++ b/iroh-net/src/endpoint.rs @@ -44,13 +44,13 @@ pub use super::magicsock::{ pub use iroh_base::node_addr::{AddrInfo, NodeAddr}; -/// The delay we add before starting a discovery in [`MagicEndpoint::connect`] if the user provided +/// The delay we add before starting a discovery in [`Endpoint::connect`] if the user provided /// new direct addresses (to try these addresses before starting the discovery). const DISCOVERY_WAIT_PERIOD: Duration = Duration::from_millis(500); -/// Builder for [MagicEndpoint] +/// Builder for [Endpoint] #[derive(Debug)] -pub struct MagicEndpointBuilder { +pub struct EndpointBuilder { secret_key: Option, relay_mode: RelayMode, alpn_protocols: Vec>, @@ -58,14 +58,14 @@ pub struct MagicEndpointBuilder { concurrent_connections: Option, keylog: bool, discovery: Option>, - /// Path for known peers. See [`MagicEndpointBuilder::peers_data_path`]. + /// Path for known peers. See [`EndpointBuilder::peers_data_path`]. peers_path: Option, dns_resolver: Option, #[cfg(any(test, feature = "test-utils"))] insecure_skip_relay_cert_verify: bool, } -impl Default for MagicEndpointBuilder { +impl Default for EndpointBuilder { fn default() -> Self { Self { secret_key: Default::default(), @@ -83,7 +83,7 @@ impl Default for MagicEndpointBuilder { } } -impl MagicEndpointBuilder { +impl EndpointBuilder { /// Set a secret key to authenticate with other peers. /// /// This secret key's public key will be the [PublicKey] of this endpoint. @@ -127,7 +127,7 @@ impl MagicEndpointBuilder { /// configured relay node. If an invalid [`RelayMap`] is provided [`bind`] /// will result in an error. /// - /// [`bind`]: MagicEndpointBuilder::bind + /// [`bind`]: EndpointBuilder::bind pub fn relay_mode(mut self, relay_mode: RelayMode) -> Self { self.relay_mode = relay_mode; self @@ -196,7 +196,7 @@ impl MagicEndpointBuilder { /// The port will be used to bind an IPv4 and, if supported, and IPv6 socket. /// You can pass `0` to let the operating system choose a free port for you. /// NOTE: This will be improved soon to add support for binding on specific addresses. - pub async fn bind(self, bind_port: u16) -> Result { + pub async fn bind(self, bind_port: u16) -> Result { let relay_map = match self.relay_mode { RelayMode::Disabled => RelayMap::empty(), RelayMode::Default => default_relay_map(), @@ -229,7 +229,7 @@ impl MagicEndpointBuilder { #[cfg(any(test, feature = "test-utils"))] insecure_skip_relay_cert_verify: self.insecure_skip_relay_cert_verify, }; - MagicEndpoint::bind(Some(server_config), msock_opts, self.keylog).await + Endpoint::bind(Some(server_config), msock_opts, self.keylog).await } } @@ -254,10 +254,10 @@ pub fn make_server_config( /// to it. It will also keep looking for better connections as the network details of both nodes /// change. /// -/// It is usually only necessary to use a single [`MagicEndpoint`] instance in an application, it +/// It is usually only necessary to use a single [`Endpoint`] instance in an application, it /// means any QUIC endpoints on top will be sharing as much information about nodes as possible. #[derive(Clone, Debug)] -pub struct MagicEndpoint { +pub struct Endpoint { secret_key: Arc, msock: Handle, endpoint: quinn::Endpoint, @@ -266,15 +266,15 @@ pub struct MagicEndpoint { cancel_token: CancellationToken, } -impl MagicEndpoint { - /// Build a MagicEndpoint - pub fn builder() -> MagicEndpointBuilder { - MagicEndpointBuilder::default() +impl Endpoint { + /// Build an [`Endpoint`] + pub fn builder() -> EndpointBuilder { + EndpointBuilder::default() } /// Create a quinn endpoint backed by a magicsock. /// - /// This is for internal use, the public interface is the [`MagicEndpointBuilder`] obtained from + /// This is for internal use, the public interface is the [`EndpointBuilder`] obtained from /// [Self::builder]. See the methods on the builder for documentation of the parameters. async fn bind( server_config: Option, @@ -345,7 +345,7 @@ impl MagicEndpoint { /// Returns the local endpoints as a stream. /// - /// The [`MagicEndpoint`] continuously monitors the local endpoints, the network + /// The [`Endpoint`] continuously monitors the local endpoints, the network /// addresses it can listen on, for changes. Whenever changes are detected this stream /// will yield a new list of endpoints. /// @@ -363,11 +363,11 @@ impl MagicEndpoint { /// To get the current endpoints, drop the stream after the first item was received: /// ``` /// use futures_lite::StreamExt; - /// use iroh_net::MagicEndpoint; + /// use iroh_net::Endpoint; /// /// # let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap(); /// # rt.block_on(async move { - /// let mep = MagicEndpoint::builder().bind(0).await.unwrap(); + /// let mep = Endpoint::builder().bind(0).await.unwrap(); /// let _endpoints = mep.local_endpoints().next().await; /// # }); /// ``` @@ -408,7 +408,7 @@ impl MagicEndpoint { /// with that node over a `Direct` (UDP) or `Relay` (relay) connection. /// /// Connections are currently only pruned on user action (when we explicitly add a new address - /// to the internal addressbook through [`MagicEndpoint::add_node_addr`]), so these connections + /// to the internal addressbook through [`Endpoint::add_node_addr`]), so these connections /// are not necessarily active connections. pub fn connection_infos(&self) -> Vec { self.msock.connection_infos() @@ -457,8 +457,8 @@ impl MagicEndpoint { /// /// If the [`NodeAddr`] contains only [`NodeId`] and no direct addresses and no relay servers, /// a discovery service will be invoked, if configured, to try and discover the node's - /// addressing information. The discovery services must be configured globally per [`MagicEndpoint`] - /// with [`MagicEndpointBuilder::discovery`]. The discovery service will also be invoked if + /// addressing information. The discovery services must be configured globally per [`Endpoint`] + /// with [`EndpointBuilder::discovery`]. The discovery service will also be invoked if /// none of the existing or provided direct addresses are reachable. /// /// If addresses or relay servers are neither provided nor can be discovered, the connection @@ -624,7 +624,7 @@ impl MagicEndpoint { Ok(()) } - /// Get a reference to the DNS resolver used in this [`MagicEndpoint`]. + /// Get a reference to the DNS resolver used in this [`Endpoint`]. pub fn dns_resolver(&self) -> &DnsResolver { self.msock.dns_resolver() } @@ -640,7 +640,7 @@ impl MagicEndpoint { /// Returns an error if closing the magic socket failed. /// TODO: Document error cases. pub async fn close(self, error_code: VarInt, reason: &[u8]) -> Result<()> { - let MagicEndpoint { + let Endpoint { msock, endpoint, cancel_token, @@ -650,7 +650,7 @@ impl MagicEndpoint { tracing::debug!("Closing connections"); endpoint.close(error_code, reason); endpoint.wait_idle().await; - // In case this is the last clone of `MagicEndpoint`, dropping the `quinn::Endpoint` will + // In case this is the last clone of `Endpoint`, dropping the `quinn::Endpoint` will // make it more likely that the underlying socket is not polled by quinn anymore after this drop(endpoint); tracing::debug!("Connections closed"); @@ -683,13 +683,13 @@ impl MagicEndpoint { } } -/// Future produced by [`MagicEndpoint::accept`]. +/// Future produced by [`Endpoint::accept`]. #[derive(Debug)] #[pin_project::pin_project] pub struct Accept<'a> { #[pin] inner: quinn::Accept<'a>, - magic_ep: MagicEndpoint, + magic_ep: Endpoint, } impl<'a> Future for Accept<'a> { @@ -714,7 +714,7 @@ impl<'a> Future for Accept<'a> { pub struct Connecting { #[pin] inner: quinn::Connecting, - magic_ep: MagicEndpoint, + magic_ep: Endpoint, } impl Connecting { @@ -803,7 +803,7 @@ pub fn get_remote_node_id(connection: &quinn::Connection) -> Result { /// /// If we can't notify the actor that will impact performance a little, but we can still /// function. -fn try_send_rtt_msg(conn: &quinn::Connection, magic_ep: &MagicEndpoint) { +fn try_send_rtt_msg(conn: &quinn::Connection, magic_ep: &Endpoint) { // If we can't notify the rtt-actor that's not great but not critical. let Ok(peer_id) = get_remote_node_id(conn) else { warn!(?conn, "failed to get remote node id"); @@ -857,7 +857,7 @@ mod tests { #[tokio::test] async fn test_connect_self() { let _guard = iroh_test::logging::setup(); - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .alpns(vec![TEST_ALPN.to_vec()]) .bind(0) .await @@ -875,7 +875,7 @@ mod tests { } #[tokio::test] - async fn magic_endpoint_connect_close() { + async fn endpoint_connect_close() { let _guard = iroh_test::logging::setup(); let (relay_map, relay_url, _guard) = run_relay_server().await.unwrap(); let server_secret_key = SecretKey::generate(); @@ -885,7 +885,7 @@ mod tests { let relay_map = relay_map.clone(); tokio::spawn( async move { - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .secret_key(server_secret_key) .alpns(vec![TEST_ALPN.to_vec()]) .relay_mode(RelayMode::Custom(relay_map)) @@ -921,7 +921,7 @@ mod tests { let client = tokio::spawn( async move { - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .alpns(vec![TEST_ALPN.to_vec()]) .relay_mode(RelayMode::Custom(relay_map)) .insecure_skip_relay_cert_verify(true) @@ -977,11 +977,11 @@ mod tests { let path = root.join("peers"); /// Create an endpoint for the test. - async fn new_endpoint(secret_key: SecretKey, peers_path: PathBuf) -> MagicEndpoint { + async fn new_endpoint(secret_key: SecretKey, peers_path: PathBuf) -> Endpoint { let mut transport_config = quinn::TransportConfig::default(); transport_config.max_idle_timeout(Some(Duration::from_secs(10).try_into().unwrap())); - MagicEndpoint::builder() + Endpoint::builder() .secret_key(secret_key.clone()) .transport_config(transport_config) .peers_data_path(peers_path) @@ -1017,7 +1017,7 @@ mod tests { } #[tokio::test] - async fn magic_endpoint_relay_connect_loop() { + async fn endpoint_relay_connect_loop() { let _logging_guard = iroh_test::logging::setup(); let start = Instant::now(); let n_clients = 5; @@ -1033,7 +1033,7 @@ mod tests { let relay_map = relay_map.clone(); tokio::spawn( async move { - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .insecure_skip_relay_cert_verify(true) .secret_key(server_secret_key) .alpns(vec![TEST_ALPN.to_vec()]) @@ -1078,7 +1078,7 @@ mod tests { let relay_url = relay_url.clone(); async { info!("client binding"); - let ep = MagicEndpoint::builder() + let ep = Endpoint::builder() .alpns(vec![TEST_ALPN.to_vec()]) .insecure_skip_relay_cert_verify(true) .relay_mode(RelayMode::Custom(relay_map)) @@ -1122,15 +1122,15 @@ mod tests { } #[tokio::test] - async fn magic_endpoint_bidi_send_recv() { + async fn endpoint_bidi_send_recv() { let _logging_guard = iroh_test::logging::setup(); - let ep1 = MagicEndpoint::builder() + let ep1 = Endpoint::builder() .alpns(vec![TEST_ALPN.to_vec()]) .relay_mode(RelayMode::Disabled) .bind(0) .await .unwrap(); - let ep2 = MagicEndpoint::builder() + let ep2 = Endpoint::builder() .alpns(vec![TEST_ALPN.to_vec()]) .relay_mode(RelayMode::Disabled) .bind(0) @@ -1145,7 +1145,7 @@ mod tests { eprintln!("node id 1 {ep1_nodeid}"); eprintln!("node id 2 {ep2_nodeid}"); - async fn connect_hello(ep: MagicEndpoint, dst: NodeAddr) { + async fn connect_hello(ep: Endpoint, dst: NodeAddr) { let conn = ep.connect(dst, TEST_ALPN).await.unwrap(); let (mut send, mut recv) = conn.open_bi().await.unwrap(); send.write_all(b"hello").await.unwrap(); @@ -1154,7 +1154,7 @@ mod tests { assert_eq!(m, b"world"); } - async fn accept_world(ep: MagicEndpoint, src: NodeId) { + async fn accept_world(ep: Endpoint, src: NodeId) { let mut incoming = ep.accept().await.unwrap(); let alpn = incoming.alpn().await.unwrap(); let conn = incoming.await.unwrap(); @@ -1180,13 +1180,13 @@ mod tests { } #[tokio::test] - async fn magic_endpoint_conn_type_stream() { + async fn endpoint_conn_type_stream() { let _logging_guard = iroh_test::logging::setup(); let (relay_map, relay_url, _relay_guard) = run_relay_server().await.unwrap(); let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42); let ep1_secret_key = SecretKey::generate_with_rng(&mut rng); let ep2_secret_key = SecretKey::generate_with_rng(&mut rng); - let ep1 = MagicEndpoint::builder() + let ep1 = Endpoint::builder() .secret_key(ep1_secret_key) .insecure_skip_relay_cert_verify(true) .alpns(vec![TEST_ALPN.to_vec()]) @@ -1194,7 +1194,7 @@ mod tests { .bind(0) .await .unwrap(); - let ep2 = MagicEndpoint::builder() + let ep2 = Endpoint::builder() .secret_key(ep2_secret_key) .insecure_skip_relay_cert_verify(true) .alpns(vec![TEST_ALPN.to_vec()]) @@ -1203,7 +1203,7 @@ mod tests { .await .unwrap(); - async fn handle_direct_conn(ep: MagicEndpoint, node_id: PublicKey) -> Result<()> { + async fn handle_direct_conn(ep: Endpoint, node_id: PublicKey) -> Result<()> { let node_addr = NodeAddr::new(node_id); ep.add_node_addr(node_addr)?; let stream = ep.conn_type_stream(&node_id)?; @@ -1252,7 +1252,7 @@ mod tests { let _ep2_guard = CallOnDrop::new(move || { ep2_abort_handle.abort(); }); - async fn accept(ep: MagicEndpoint) -> NodeId { + async fn accept(ep: Endpoint) -> NodeId { let incoming = ep.accept().await.unwrap(); let conn = incoming.await.unwrap(); get_remote_node_id(&conn).unwrap() diff --git a/iroh-net/src/magic_endpoint/rtt_actor.rs b/iroh-net/src/endpoint/rtt_actor.rs similarity index 100% rename from iroh-net/src/magic_endpoint/rtt_actor.rs rename to iroh-net/src/endpoint/rtt_actor.rs diff --git a/iroh-net/src/lib.rs b/iroh-net/src/lib.rs index e871382ac77..911060df6fc 100644 --- a/iroh-net/src/lib.rs +++ b/iroh-net/src/lib.rs @@ -1,7 +1,7 @@ //! iroh-net provides connectivity for iroh. //! //! This crate is a collection of tools to establish connectivity between peers. At -//! the high level [`MagicEndpoint`] is used to establish a QUIC connection with +//! the high level [`Endpoint`] is used to establish a QUIC connection with //! authenticated peers, relaying and holepunching support. //! //! The "relay-only" feature forces all traffic to send over the relays. We still @@ -16,7 +16,7 @@ pub mod dialer; mod disco; pub mod discovery; pub mod dns; -pub mod magic_endpoint; +pub mod endpoint; mod magicsock; pub mod metrics; pub mod net; @@ -29,7 +29,7 @@ pub mod ticket; pub mod tls; pub mod util; -pub use magic_endpoint::{AddrInfo, MagicEndpoint, NodeAddr}; +pub use endpoint::{AddrInfo, Endpoint, NodeAddr}; pub use iroh_base::key; diff --git a/iroh-net/src/magicsock.rs b/iroh-net/src/magicsock.rs index 5795f5ac223..8e54b0d797b 100644 --- a/iroh-net/src/magicsock.rs +++ b/iroh-net/src/magicsock.rs @@ -53,8 +53,8 @@ use crate::{ disco::{self, SendAddr}, discovery::Discovery, dns::DnsResolver, + endpoint::NodeAddr, key::{PublicKey, SecretKey, SharedSecret}, - magic_endpoint::NodeAddr, net::{interfaces, ip::LocalAddresses, netmon, IpFamily}, netcheck, portmapper, relay::{RelayMap, RelayUrl}, @@ -663,7 +663,7 @@ impl MagicSock { // overwrite the first byte of the packets with zero. // this makes quinn reliably and quickly ignore the packet as long as // [`quinn::EndpointConfig::grease_quic_bit`] is set to `false` - // (which we always do in MagicEndpoint::bind). + // (which we always do in Endpoint::bind). buf[start] = 0u8; } start = end; @@ -2555,7 +2555,7 @@ pub(crate) mod tests { use iroh_test::CallOnDrop; use rand::RngCore; - use crate::{relay::RelayMode, tls, MagicEndpoint}; + use crate::{relay::RelayMode, tls, Endpoint}; use super::*; @@ -2563,7 +2563,7 @@ pub(crate) mod tests { #[derive(Clone)] struct MagicStack { secret_key: SecretKey, - endpoint: MagicEndpoint, + endpoint: Endpoint, } const ALPN: &[u8] = b"n0/test/1"; @@ -2575,7 +2575,7 @@ pub(crate) mod tests { let mut transport_config = quinn::TransportConfig::default(); transport_config.max_idle_timeout(Some(Duration::from_secs(10).try_into().unwrap())); - let endpoint = MagicEndpoint::builder() + let endpoint = Endpoint::builder() .secret_key(secret_key.clone()) .transport_config(transport_config) .relay_mode(relay_mode) diff --git a/iroh-net/src/magicsock/node_map.rs b/iroh-net/src/magicsock/node_map.rs index 8a97595f42b..c17cfccaeb3 100644 --- a/iroh-net/src/magicsock/node_map.rs +++ b/iroh-net/src/magicsock/node_map.rs @@ -641,7 +641,7 @@ impl IpPort { mod tests { use super::node_state::MAX_INACTIVE_DIRECT_ADDRESSES; use super::*; - use crate::{key::SecretKey, magic_endpoint::AddrInfo}; + use crate::{endpoint::AddrInfo, key::SecretKey}; use std::net::Ipv4Addr; /// Test persisting and loading of known nodes. diff --git a/iroh-net/src/magicsock/node_map/node_state.rs b/iroh-net/src/magicsock/node_map/node_state.rs index 336d325bfaf..c72b1118bb4 100644 --- a/iroh-net/src/magicsock/node_map/node_state.rs +++ b/iroh-net/src/magicsock/node_map/node_state.rs @@ -14,8 +14,8 @@ use watchable::{Watchable, WatcherStream}; use crate::{ disco::{self, SendAddr}, + endpoint::AddrInfo, key::PublicKey, - magic_endpoint::AddrInfo, magicsock::{Timer, HEARTBEAT_INTERVAL}, net::ip::is_unicast_link_local, relay::RelayUrl, diff --git a/iroh-net/src/test_utils.rs b/iroh-net/src/test_utils.rs index 988d0eefd4b..0cbf8bd857e 100644 --- a/iroh-net/src/test_utils.rs +++ b/iroh-net/src/test_utils.rs @@ -25,9 +25,9 @@ pub struct CleanupDropGuard(pub(crate) oneshot::Sender<()>); /// Runs a relay server with STUN enabled suitable for tests. /// /// The returned `Url` is the url of the relay server in the returned [`RelayMap`], it -/// is always `Some` as that is how the [`MagicEndpoint::connect`] API expects it. +/// is always `Some` as that is how the [`Endpoint::connect`] API expects it. /// -/// [`MagicEndpoint::connect`]: crate::magic_endpoint::MagicEndpoint +/// [`Endpoint::connect`]: crate::endpoint::Endpoint pub async fn run_relay_server() -> Result<(RelayMap, RelayUrl, CleanupDropGuard)> { let server_key = SecretKey::generate(); let me = server_key.public().fmt_short(); diff --git a/iroh/src/client/node.rs b/iroh/src/client/node.rs index 9bbc2caeb00..6f8460b3764 100644 --- a/iroh/src/client/node.rs +++ b/iroh/src/client/node.rs @@ -5,7 +5,7 @@ use std::{collections::BTreeMap, net::SocketAddr}; use anyhow::Result; use futures_lite::{Stream, StreamExt}; use iroh_base::key::PublicKey; -use iroh_net::{magic_endpoint::ConnectionInfo, relay::RelayUrl, NodeAddr, NodeId}; +use iroh_net::{endpoint::ConnectionInfo, relay::RelayUrl, NodeAddr, NodeId}; use quic_rpc::ServiceConnection; use serde::{Deserialize, Serialize}; diff --git a/iroh/src/docs_engine.rs b/iroh/src/docs_engine.rs index 71b847d0423..ce29668307a 100644 --- a/iroh/src/docs_engine.rs +++ b/iroh/src/docs_engine.rs @@ -11,7 +11,7 @@ use iroh_blobs::{store::EntryStatus, Hash}; use iroh_docs::{actor::SyncHandle, ContentStatus, ContentStatusCallback, Entry, NamespaceId}; use iroh_gossip::net::Gossip; use iroh_net::util::SharedAbortingJoinHandle; -use iroh_net::{key::PublicKey, MagicEndpoint, NodeAddr}; +use iroh_net::{key::PublicKey, Endpoint, NodeAddr}; use serde::{Deserialize, Serialize}; use tokio::sync::{mpsc, oneshot}; use tracing::{error, error_span, Instrument}; @@ -39,7 +39,7 @@ const SUBSCRIBE_CHANNEL_CAP: usize = 256; /// implementations in [rpc]. #[derive(derive_more::Debug, Clone)] pub struct Engine { - pub(crate) endpoint: MagicEndpoint, + pub(crate) endpoint: Endpoint, pub(crate) sync: SyncHandle, to_live_actor: mpsc::Sender, #[allow(dead_code)] @@ -54,7 +54,7 @@ impl Engine { /// This will spawn two tokio tasks for the live sync coordination and gossip actors, and a /// thread for the [`iroh_docs::actor::SyncHandle`]. pub(crate) fn spawn( - endpoint: MagicEndpoint, + endpoint: Endpoint, gossip: Gossip, replica_store: iroh_docs::store::Store, bao_store: B, @@ -178,7 +178,7 @@ impl Engine { /// Handle an incoming iroh-docs connection. pub(super) async fn handle_connection( &self, - conn: iroh_net::magic_endpoint::Connecting, + conn: iroh_net::endpoint::Connecting, ) -> anyhow::Result<()> { self.to_live_actor .send(ToLiveActor::HandleConnection { conn }) diff --git a/iroh/src/docs_engine/live.rs b/iroh/src/docs_engine/live.rs index 67bd7da1dcb..13e16961be2 100644 --- a/iroh/src/docs_engine/live.rs +++ b/iroh/src/docs_engine/live.rs @@ -19,7 +19,7 @@ use iroh_docs::{ }; use iroh_gossip::{net::Gossip, proto::TopicId}; use iroh_net::NodeId; -use iroh_net::{key::PublicKey, MagicEndpoint, NodeAddr}; +use iroh_net::{key::PublicKey, Endpoint, NodeAddr}; use serde::{Deserialize, Serialize}; use tokio::{ sync::{self, mpsc, oneshot}, @@ -75,7 +75,7 @@ pub enum ToLiveActor { reply: sync::oneshot::Sender>, }, HandleConnection { - conn: iroh_net::magic_endpoint::Connecting, + conn: iroh_net::endpoint::Connecting, }, AcceptSyncRequest { namespace: NamespaceId, @@ -133,7 +133,7 @@ pub struct LiveActor { /// Receiver for actor messages. inbox: mpsc::Receiver, sync: SyncHandle, - endpoint: MagicEndpoint, + endpoint: Endpoint, gossip: Gossip, bao_store: B, downloader: Downloader, @@ -168,7 +168,7 @@ impl LiveActor { #[allow(clippy::too_many_arguments)] pub fn new( sync: SyncHandle, - endpoint: MagicEndpoint, + endpoint: Endpoint, gossip: Gossip, bao_store: B, downloader: Downloader, @@ -715,7 +715,7 @@ impl LiveActor { } #[instrument("accept", skip_all)] - pub async fn handle_connection(&mut self, conn: iroh_net::magic_endpoint::Connecting) { + pub async fn handle_connection(&mut self, conn: iroh_net::endpoint::Connecting) { let to_actor_tx = self.sync_actor_tx.clone(); let accept_request_cb = move |namespace, peer| { let to_actor_tx = to_actor_tx.clone(); diff --git a/iroh/src/node.rs b/iroh/src/node.rs index e6928556c5e..05227992e8c 100644 --- a/iroh/src/node.rs +++ b/iroh/src/node.rs @@ -16,7 +16,7 @@ use iroh_base::key::PublicKey; use iroh_blobs::downloader::Downloader; use iroh_blobs::store::Store as BaoStore; use iroh_net::util::AbortingJoinHandle; -use iroh_net::{key::SecretKey, magic_endpoint::LocalEndpointsStream, MagicEndpoint}; +use iroh_net::{endpoint::LocalEndpointsStream, key::SecretKey, Endpoint}; use quic_rpc::transport::flume::FlumeConnection; use quic_rpc::RpcClient; use tokio::sync::{mpsc, RwLock}; @@ -87,7 +87,7 @@ pub struct Node { #[derive(derive_more::Debug)] struct NodeInner { db: D, - endpoint: MagicEndpoint, + endpoint: Endpoint, secret_key: SecretKey, cancel_token: CancellationToken, controller: FlumeConnection, @@ -139,12 +139,12 @@ impl FsNode { } impl Node { - /// Returns the [`MagicEndpoint`] of the node. + /// Returns the [`Endpoint`] of the node. /// /// This can be used to establish connections to other nodes under any /// ALPNs other than the iroh internal ones. This is useful for some advanced /// use cases. - pub fn magic_endpoint(&self) -> &MagicEndpoint { + pub fn endpoint(&self) -> &Endpoint { &self.inner.endpoint } diff --git a/iroh/src/node/builder.rs b/iroh/src/node/builder.rs index 063f955d513..addec034d44 100644 --- a/iroh/src/node/builder.rs +++ b/iroh/src/node/builder.rs @@ -20,7 +20,7 @@ use iroh_net::{ discovery::{dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery, Discovery}, dns::DnsResolver, relay::RelayMode, - MagicEndpoint, + Endpoint, }; use quic_rpc::{ transport::{misc::DummyServerEndpoint, quinn::QuinnServerEndpoint}, @@ -375,7 +375,7 @@ where } }; - let endpoint = MagicEndpoint::builder() + let endpoint = Endpoint::builder() .secret_key(self.secret_key.clone()) .alpns(PROTOCOLS.iter().map(|p| p.to_vec()).collect()) .keylog(self.keylog) @@ -507,7 +507,7 @@ where #[allow(clippy::too_many_arguments)] async fn run( - server: MagicEndpoint, + server: Endpoint, callbacks: Callbacks, mut cb_receiver: mpsc::Receiver, handler: rpc::Handler, @@ -705,7 +705,7 @@ impl Default for GcPolicy { // TODO: Restructure this code to not take all these arguments. #[allow(clippy::too_many_arguments)] async fn handle_connection( - connecting: iroh_net::magic_endpoint::Connecting, + connecting: iroh_net::endpoint::Connecting, alpn: String, node: Arc>, gossip: Gossip, @@ -743,7 +743,7 @@ fn make_rpc_endpoint( transport_config .max_concurrent_bidi_streams(MAX_RPC_STREAMS.into()) .max_concurrent_uni_streams(0u32.into()); - let mut server_config = iroh_net::magic_endpoint::make_server_config( + let mut server_config = iroh_net::endpoint::make_server_config( secret_key, vec![RPC_ALPN.to_vec()], Some(transport_config), diff --git a/iroh/src/node/rpc.rs b/iroh/src/node/rpc.rs index 2ae74099882..f61e0220563 100644 --- a/iroh/src/node/rpc.rs +++ b/iroh/src/node/rpc.rs @@ -25,7 +25,7 @@ use iroh_blobs::{ }; use iroh_io::AsyncSliceReader; use iroh_net::relay::RelayUrl; -use iroh_net::{MagicEndpoint, NodeAddr, NodeId}; +use iroh_net::{Endpoint, NodeAddr, NodeId}; use quic_rpc::{ server::{RpcChannel, RpcServerError}, ServiceEndpoint, @@ -1055,7 +1055,7 @@ impl Handler { async fn download( db: &D, - endpoint: MagicEndpoint, + endpoint: Endpoint, downloader: &Downloader, req: BlobDownloadRequest, progress: FlumeProgressSender, @@ -1095,7 +1095,7 @@ where } async fn download_queued( - endpoint: MagicEndpoint, + endpoint: Endpoint, downloader: &Downloader, hash_and_format: HashAndFormat, nodes: Vec, @@ -1117,7 +1117,7 @@ async fn download_queued( async fn download_direct_from_nodes( db: &D, - endpoint: MagicEndpoint, + endpoint: Endpoint, hash_and_format: HashAndFormat, nodes: Vec, tag: SetTagOption, @@ -1152,7 +1152,7 @@ where async fn download_direct( db: &D, - endpoint: MagicEndpoint, + endpoint: Endpoint, hash_and_format: HashAndFormat, node: NodeAddr, tag: SetTagOption, diff --git a/iroh/src/rpc_protocol.rs b/iroh/src/rpc_protocol.rs index c0cd6c9b261..394374cfc28 100644 --- a/iroh/src/rpc_protocol.rs +++ b/iroh/src/rpc_protocol.rs @@ -19,8 +19,8 @@ use iroh_blobs::{ util::Tag, }; use iroh_net::{ + endpoint::{ConnectionInfo, NodeAddr}, key::PublicKey, - magic_endpoint::{ConnectionInfo, NodeAddr}, relay::RelayUrl, NodeId, }; diff --git a/iroh/tests/provide.rs b/iroh/tests/provide.rs index 2c382732014..dcec072ca9b 100644 --- a/iroh/tests/provide.rs +++ b/iroh/tests/provide.rs @@ -31,7 +31,7 @@ use iroh_blobs::{ /// Create a new endpoint and dial a peer, returning the connection. async fn dial(secret_key: SecretKey, peer: NodeAddr) -> anyhow::Result { - let endpoint = iroh_net::MagicEndpoint::builder() + let endpoint = iroh_net::Endpoint::builder() .secret_key(secret_key) .bind(0) .await?;