iroh_live_node/lib.rs
1//! MoQ over iroh.
2//!
3//! A [`Node`] publishes broadcasts at paths and subscribes to paths. It
4//! keeps one route table fed by every link it has: direct sessions with peers,
5//! and media relays it is attached to. A path resolves to its cheapest route,
6//! and moq moves a subscription to another route of the same instance when
7//! that route dies.
8//!
9//! ```no_run
10//! use iroh::EndpointId;
11//! use iroh_live_node::{Audience, MoqPreset, Node, NodeConfig, Reach};
12//!
13//! # async fn run(peer: EndpointId) -> Result<(), Box<dyn std::error::Error>> {
14//! let endpoint = iroh::Endpoint::bind(MoqPreset).await?;
15//! let node = Node::new(endpoint, NodeConfig::default());
16//!
17//! // Publish a broadcast this process writes, created in the route table.
18//! let publication = node.publish("demo/studio", Audience::Granted)?;
19//! let _track = publication.broadcast().create_track("video", None).ok_or("the track exists")?;
20//!
21//! // Resolve a peer's broadcast, dialing the peer if no route exists yet.
22//! let subscription = node.subscribe("demo/camera", Reach::Both(peer)).await?;
23//! let remote = subscription.broadcast();
24//! # drop(remote);
25//! # Ok(())
26//! # }
27//! ```
28//!
29//! The main types:
30//!
31//! - [`Node`]: the node. Mount it on a [`Router`](iroh::protocol::Router) with
32//! [`Node::mount`].
33//! - [`Publication`]: a published broadcast. Its [`Audience`] says who sees it.
34//! [`Publication::unpublish_on_drop`] scopes it to a guard.
35//! - [`Subscription`]: a path resolved in the route table.
36//! - [`Session`]: a direct session with a peer. Its [`Grant`] says what the
37//! peer may subscribe to and publish.
38//! - [`RelayLink`]: a media relay the node stays attached to.
39//! - [`MoqPreset`]: iroh's N0 preset with BBR3, to bind the node's endpoint
40//! with. [`Congestion`] names the controllers to compare it against.
41//! [`EndpointConfig`] binds an endpoint with it, a secret key and mDNS.
42//! - [`crypto_provider`]: the rustls crypto provider iroh and moq share,
43//! picked by the `crypto-*` features. [`MinimalPreset`] sets only it.
44//! - [`layout::LiveLayout`]: the `<root>/<peer>/<name>` path convention, with
45//! the policy that lets each peer publish only under its own id.
46//!
47//! Every link runs a connection monitor, and [`Subscription::serving_link`]
48//! returns its latest [`NetworkSample`], with the rates bytes arrive and leave
49//! at. [`Subscription::session`] returns the direct session that serves a
50//! subscription now, and [`Session::sample`] the same reading for a session
51//! the application holds. [`Session::moq`] gives the moq-net session itself,
52//! with its raw statistics, and [`Session::connection`] the iroh connection,
53//! with its paths.
54//!
55//! Every link serves only this node's own publications, never a route it
56//! learned from another link. A peer sees the paths of the publications whose
57//! audience admits it, within its [`Grant`]. The node re-authorizes every
58//! session when a publication or an audience changes, so a peer taken out of
59//! an audience loses the path at once, and what it read there ends with
60//! [`moq_net::Error::Unauthorized`].
61//!
62//! A [`policy`] decides who may hold a session and with which grant, on accept
63//! and on dial (see [`NodeConfig::policy`]). The node asks it again when an
64//! authorization expires, and on [`Node::reauthorize`].
65//! [`Grant::publish_under`] lets a peer publish only at paths that name it, so
66//! it cannot stand in for another peer.
67//!
68//! [`Node::subscribe_ticket`] claims a [`BroadcastTicket`]'s path for its peer
69//! with [`Node::claim_path`], which widens the peer's grant to exactly that
70//! path while the subscription lives. The peer has to own the path (see
71//! [`NodeConfig::layout`]), so a claim never overlaps another peer's paths.
72//!
73//! The grants also keep one publisher from taking over another's readers. moq
74//! moves a reader between routes that claim the same instance, so a peer or
75//! media relay that announces another publisher's instance could take over its
76//! readers. The table's contents prevent it:
77//!
78//! - A direct peer announces only paths its grant lets it publish.
79//! - An instance is a random string that only reaches the links whose grant
80//! lets them see the publication.
81//! - A media relay is trusted with the paths it forwards. Attach only media relays that
82//! keep each publisher to its own paths, as moq-relay's tokens do.
83//!
84//! The futures differ in what dropping them leaves behind:
85//!
86//! | Future | Cancellation safe | When dropped |
87//! |---|---|---|
88//! | [`Node::connect`], [`Node::connect_with`], [`Node::subscribe`], [`Node::subscribe_ticket`], [`Session::subscribe`] | yes | a dial it started continues for other callers |
89//! | [`policy::Queue::next`] | yes | loses nothing |
90//! | [`RelayLink::detach`] | yes | the link's task finishes the close |
91//! | [`Publication::withdrawn`], [`Subscription::closed`], [`Session::closed`] | yes | loses nothing |
92//! | [`ValueReader::latest`], [`ValueReader::next`] | yes | loses nothing |
93//! | [`Node::closed`] | yes | loses nothing |
94//! | `next()` on [`Node::accepted`] | yes | loses nothing |
95//! | [`Node::shutdown`] | no | call it again to finish |
96
97#![forbid(unsafe_code)]
98
99mod actor;
100mod admission;
101mod claim;
102mod crypto;
103mod endpoint;
104mod error;
105mod handshake;
106pub mod layout;
107mod link;
108mod node;
109pub mod policy;
110mod preset;
111mod publish;
112mod reauth;
113mod relay;
114mod route;
115mod session;
116mod state;
117// Helpers for this workspace's tests, not part of the public API.
118#[cfg(feature = "test-util")]
119#[doc(hidden)]
120pub mod test_util;
121mod ticket;
122mod value;
123
124pub use moq_ext_access::{Direction, LinkKind};
125pub use moq_ext_adaptive::NetworkSample;
126pub use moq_ext_json::{ValueError, ValueWriter};
127/// Typed JSON tracks, as [`Node::publish_value`] and [`Subscription::value_track`] use them.
128pub use moq_json as json;
129/// The moq-net this crate builds against.
130pub use moq_net;
131use moq_transport_iroh as transport;
132pub use moq_transport_iroh::{ALPN, HANDSHAKE_TIMEOUT, alpns};
133
134pub use self::{
135 admission::{ConnectOptions, Grant, SessionRequest},
136 claim::PathClaim,
137 crypto::{MinimalPreset, crypto_provider},
138 endpoint::{EndpointConfig, EndpointError, Mdns, load_or_create_secret_key},
139 error::Error,
140 link::{LinkId, NetworkPath, NetworkPathKind, ServingLink},
141 node::{DECIDE_TIMEOUT, Node, NodeConfig, Reach},
142 preset::{Congestion, MoqPreset, UnknownCongestion},
143 publish::{Audience, Publication, UnpublishOnDrop},
144 relay::{RelayConfig, RelayLink, RelayStatus},
145 route::Subscription,
146 session::Session,
147 ticket::BroadcastTicket,
148 value::ValueReader,
149};