iroh_net::endpoint

Struct Endpoint

Source
pub struct Endpoint { /* private fields */ }
Expand description

Controls an iroh-net node, establishing connections with other nodes.

This is the main API interface to create connections to, and accept connections from other iroh-net nodes. The connections are peer-to-peer and encrypted, a Relay server is used to make the connections reliable. See the [crate docs] for a more detailed overview of iroh-net.

It is recommended to only create a single instance per application. This ensures all the connections made share the same peer-to-peer connections to other iroh-net nodes, while still remaining independent connections. This will result in more optimal network behaviour.

New connections are typically created using the Endpoint::connect and Endpoint::accept methods. Once established, the Connection gives access to most QUIC features. Individual streams to send data to the peer are created using the Connection::open_bi, Connection::accept_bi, Connection::open_uni and Connection::open_bi functions.

Note that due to the light-weight properties of streams a stream will only be accepted once the initiating peer has sent some data on it.

Implementations§

Source§

impl Endpoint

Source

pub fn builder() -> Builder

Returns the builder for an Endpoint, with a production configuration.

Source

pub fn set_alpns(&self, alpns: Vec<Vec<u8>>) -> Result<()>

Sets the list of accepted ALPN protocols.

This will only affect new incoming connections. Note that this overrides the current list of ALPNs.

Source

pub async fn connect( &self, node_addr: impl Into<NodeAddr>, alpn: &[u8], ) -> Result<Connection>

Connects to a remote Endpoint.

A value that can be converted into a NodeAddr is required. This can be either a NodeAddr, a NodeId or a iroh_base::ticket::NodeTicket.

The NodeAddr must contain the NodeId to dial and may also contain a RelayUrl and direct addresses. If direct addresses are provided, they will be used to try and establish a direct connection without involving a relay server.

If neither a RelayUrl or direct addresses are configured in the NodeAddr it may still be possible a connection can be established. This depends on other calls to Endpoint::add_node_addr which may provide contact information, or via the Discovery service configured using Builder::discovery. The discovery service will also be used if the remote node is not reachable on the provided direct addresses and there is no RelayUrl.

If addresses or relay servers are neither provided nor can be discovered, the connection attempt will fail with an error.

The alpn, or application-level protocol identifier, is also required. The remote endpoint must support this alpn, otherwise the connection attempt will fail with an error.

Source

pub async fn connect_by_node_id( &self, node_id: NodeId, alpn: &[u8], ) -> Result<Connection>

👎Deprecated since 0.27.0: Please use connect directly with a NodeId. This fn will be removed in 0.28.0.

Connects to a remote endpoint, using just the nodes’s NodeId.

This is a convenience function for Endpoint::connect. It relies on addressing information being provided by either the discovery service or using Endpoint::add_node_addr. See Endpoint::connect for the details of how it uses the discovery service to establish a connection to a remote node.

Source

pub fn accept(&self) -> Accept<'_>

Accepts an incoming connection on the endpoint.

Only connections with the ALPNs configured in Builder::alpns will be accepted. If multiple ALPNs have been configured the ALPN can be inspected before accepting the connection using Connecting::alpn.

The returned future will yield None if the endpoint is closed by calling Endpoint::close.

Source

pub fn add_node_addr(&self, node_addr: NodeAddr) -> Result<()>

Informs this Endpoint about addresses of the iroh-net node.

This updates the local state for the remote node. If the provided NodeAddr contains a RelayUrl this will be used as the new relay server for this node. If it contains any new IP endpoints they will also be stored and tried when next connecting to this node. Any address that matches this node’s direct addresses will be silently ignored.

See also Endpoint::add_node_addr_with_source.

§Errors

Will return an error if we attempt to add our own PublicKey to the node map or if the direct addresses are a subset of ours.

Source

pub fn add_node_addr_with_source( &self, node_addr: NodeAddr, source: &'static str, ) -> Result<()>

Informs this Endpoint about addresses of the iroh-net node, noting the source.

This updates the local state for the remote node. If the provided NodeAddr contains a RelayUrl this will be used as the new relay server for this node. If it contains any new IP endpoints they will also be stored and tried when next connecting to this node. Any address that matches this node’s direct addresses will be silently ignored. The source is used for logging exclusively and will not be stored.

§Errors

Will return an error if we attempt to add our own PublicKey to the node map or if the direct addresses are a subset of ours.

Source

pub fn secret_key(&self) -> &SecretKey

Returns the secret_key of this endpoint.

Source

pub fn node_id(&self) -> NodeId

Returns the node id of this endpoint.

This ID is the unique addressing information of this node and other peers must know it to be able to connect to this node.

Source

pub async fn node_addr(&self) -> Result<NodeAddr>

Returns the current NodeAddr for this endpoint.

The returned NodeAddr will have the current RelayUrl and local IP endpoints as they would be returned by Endpoint::home_relay and Endpoint::direct_addresses.

Source

pub fn home_relay(&self) -> Option<RelayUrl>

Returns the RelayUrl of the Relay server used as home relay.

Every endpoint has a home Relay server which it chooses as the server with the lowest latency out of the configured servers provided by Builder::relay_mode. This is the server other iroh-net nodes can use to reliably establish a connection to this node.

Returns None if we are not connected to any Relay server.

Note that this will be None right after the Endpoint is created since it takes some time to connect to find and connect to the home relay server. Use Endpoint::watch_home_relay to wait until the home relay server is available.

Source

pub fn watch_home_relay(&self) -> impl Stream<Item = RelayUrl>

Watches for changes to the home relay.

If there is currently a home relay it will be yielded immediately as the first item in the stream. This makes it possible to use this function to wait for the initial home relay to be known.

Note that it is not guaranteed that a home relay will ever become available. If no servers are configured with Builder::relay_mode this stream will never yield an item.

Source

pub fn direct_addresses(&self) -> DirectAddrsStream

Returns the direct addresses of this Endpoint.

The direct addresses of the Endpoint are those that could be used by other iroh-net nodes to establish direct connectivity, depending on the network situation. The yielded lists of direct addresses contain both the locally-bound addresses and the Endpoint’s publicly reachable addresses discovered through mechanisms such as STUN and port mapping. Hence usually only a subset of these will be applicable to a certain remote iroh-net node.

The Endpoint continuously monitors the direct addresses for changes as its own location in the network might change. Whenever changes are detected this stream will yield a new list of direct addresses.

When issuing the first call to this method the first direct address discovery might still be underway, in this case the first item of the returned stream will not be immediately available. Once this first set of local IP endpoints are discovered the stream will always return the first set of IP endpoints immediately, which are the most recently discovered IP endpoints.

§Examples

To get the current endpoints, drop the stream after the first item was received:

use futures_lite::StreamExt;
use iroh_net::Endpoint;

let mep =  Endpoint::builder().bind().await.unwrap();
let _addrs = mep.direct_addresses().next().await;
Source

pub fn bound_sockets(&self) -> (SocketAddr, Option<SocketAddr>)

Returns the local socket addresses on which the underlying sockets are bound.

The Endpoint always binds on an IPv4 address and also tries to bind on an IPv6 address if available.

Source

pub fn remote_info(&self, node_id: NodeId) -> Option<RemoteInfo>

Returns information about the remote node identified by a NodeId.

The Endpoint keeps some information about remote iroh-net nodes, which it uses to find the best path to a node. Having information on a remote node, however, does not mean we have ever connected to it to or even whether a connection is even possible. The information about a remote node will change over time, as the Endpoint learns more about the node. Future calls may return different information. Furthermore, node information may even be completely evicted as it becomes stale.

See also Endpoint::remote_info_iter which returns information on all nodes known by this Endpoint.

Source

pub fn remote_info_iter(&self) -> impl Iterator<Item = RemoteInfo>

Returns information about all the remote nodes this Endpoint knows about.

This returns the same information as Endpoint::remote_info for each node known to this Endpoint.

The Endpoint keeps some information about remote iroh-net nodes, which it uses to find the best path to a node. This returns all the nodes it knows about, regardless of whether a connection was ever made or is even possible.

See also Endpoint::remote_info to only retrieve information about a single node.

Source

pub fn conn_type_stream(&self, node_id: NodeId) -> Result<ConnectionTypeStream>

Returns a stream that reports connection type changes for the remote node.

This returns a stream of ConnectionType items, each time the underlying connection to a remote node changes it yields an item. These connection changes are when the connection switches between using the Relay server and a direct connection.

If there is currently a connection with the remote node the first item in the stream will yield immediately returning the current connection type.

Note that this does not guarantee each connection change is yielded in the stream. If the connection type changes several times before this stream is polled only the last recorded state is returned. This can be observed e.g. right at the start of a connection when the switch from a relayed to a direct connection can be so fast that the relayed state is never exposed.

§Errors

Will error if we do not have any address information for the given node_id.

Source

pub fn dns_resolver(&self) -> &DnsResolver

Returns the DNS resolver used in this Endpoint.

See Builder::discovery.

Source

pub fn discovery(&self) -> Option<&dyn Discovery>

Returns the discovery mechanism, if configured.

See Builder::dns_resolver.

Source

pub async fn network_change(&self)

Notifies the system of potential network changes.

On many systems iroh is able to detect network changes by itself, however some systems like android do not expose this functionality to native code. Android does however provide this functionality to Java code. This function allows for notifying iroh of any potential network changes like this.

Even when the network did not change, or iroh was already able to detect the network change itself, there is no harm in calling this function.

Source

pub async fn close(self, error_code: VarInt, reason: &[u8]) -> Result<()>

Closes the QUIC endpoint and the magic socket.

This will close all open QUIC connections with the provided error_code and reason. See quinn::Connection for details on how these are interpreted.

It will then wait for all connections to actually be shutdown, and afterwards close the magic socket.

Returns an error if closing the magic socket failed. TODO: Document error cases.

Trait Implementations§

Source§

impl Clone for Endpoint

Source§

fn clone(&self) -> Endpoint

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Endpoint

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T