From 50467dc5ea81c4e373d1f8d25524365b58269fdc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:33:47 -0800 Subject: [PATCH 1/8] chore(quest): claim quest/m1/precut-fixes/whep-egress-end.md Quest-Claim: bcc51ea5194d66f1 From 580060a1b67d11449472532a34d3bd42a9c61ffd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:49:36 -0800 Subject: [PATCH 2/8] fix(webrtc): a WHEP viewer ends with its broadcast and costs one line per kind A clean catalog end now closes the egress session (RTCP BYE and DTLS close_notify) once every pump has written what it holds. A WHEP answer accepts at most one audio and one video line, rejecting the rest as data channels are. The server mux routes a STUN binding request by its ufrag, so a re-offer from the same address reaches its new session. Co-Authored-By: Claude Opus 5.5 --- doc/bin/webrtc.md | 10 +- quest/m1/export-evicts-every-group.md | 30 ++++++ quest/m1/precut-fixes/whep-egress-end.md | 37 ------- quest/m3/whip-client-delete.md | 27 +++++ rs/moq-webrtc/src/client/mod.rs | 4 +- rs/moq-webrtc/src/egress.rs | 23 +++- rs/moq-webrtc/src/lib.rs | 76 ++++++++++--- rs/moq-webrtc/src/sdp.rs | 131 +++++++++++++++++++---- rs/moq-webrtc/src/server/mod.rs | 2 + rs/moq-webrtc/src/server/mux.rs | 66 +++++++++--- rs/moq-webrtc/src/server/whep.rs | 5 +- rs/moq-webrtc/src/server/whip.rs | 2 +- rs/moq-webrtc/src/session.rs | 34 ++++-- 13 files changed, 343 insertions(+), 104 deletions(-) create mode 100644 quest/m1/export-evicts-every-group.md delete mode 100644 quest/m1/precut-fixes/whep-egress-end.md create mode 100644 quest/m3/whip-client-delete.md diff --git a/doc/bin/webrtc.md b/doc/bin/webrtc.md index 11fe8530128..e16f8adfe71 100644 --- a/doc/bin/webrtc.md +++ b/doc/bin/webrtc.md @@ -28,7 +28,9 @@ receives the largest enabled video rendition (then highest bitrate) in its negot the highest-bitrate enabled Opus rendition (then sample rate, then channels). It follows the catalog: a peer whose rendition is disabled moves to the next best at that rendition's first keyframe, a better one that re-enables wins the peer back, -and with none enabled the peer receives nothing until one is. `--cors-origin` +and with none enabled the peer receives nothing until one is. A WHEP offer gets one +audio and one video line at most: the answer rejects the rest, which would only repeat +the same media. `--cors-origin` opens the endpoint to browsers on other origins. Every session shares one UDP media port, `--udp-bind` (OS-picked by default), bound at startup so a taken port fails before the listener reports ready. Behind NAT, `--public-addr` advertises @@ -43,7 +45,11 @@ reconnects while its stale session lingers replaces it at once. An export stays on the publisher it started with. A replacement publisher never reaches its session, so timestamps and codec state never reset mid-session. Once the original publisher leaves, the session ends with an error, and the -player or `export webrtc --connect` must reconnect to reach the replacement. +player or `export webrtc --connect` must reconnect to reach the replacement. A +publisher that ends cleanly, such as an RTMP `deleteStream`, ends the session +cleanly once its last frames are sent: the peer connection closes with an RTCP BYE +and a DTLS close\_notify, which a browser reports as its `RTCDtlsTransport` state +turning `closed`. `--connect` runs until its session ends. Built on [str0m](https://github.com/algesten/str0m), with DTLS and SRTP on [rind](https://github.com/kixel-ai/rind); the library is diff --git a/quest/m1/export-evicts-every-group.md b/quest/m1/export-evicts-every-group.md new file mode 100644 index 00000000000..7415614c30b --- /dev/null +++ b/quest/m1/export-evicts-every-group.md @@ -0,0 +1,30 @@ +# [M] A live export reads each group to its end instead of evicting it + +## Goal + +A container export following a steady live broadcast reads every group to its +clean end: it logs no `current group evicted` and counts no loss while nothing +is lost. Each group's tail frames reach the export. + +## Plan + +Seen 2026-11-17 while validating +[moq-dev/moq#9049](https://github.com/moq-dev/moq/pull/9049) on `main`: a +local `moq-relay`, ffmpeg pushing 30 fps H.264 with a 1 s GOP over RTMP into +`moq import rtmp`, and `moq export webrtc --listen` from the relay (both on +moq-lite-06, WebTransport). The export logged `current group evicted; skipping +to next buffered group ... error=Moq(Stream(Old))` from +`rs/moq-mux/src/container/consumer.rs` once per group (33 in 33 s), while +Chrome still decoded about 30 fps. Every evicted group calls `lose()`, so the +export's loss accounting reports a loss per group. + +Reproduce first, then find which side ends a finished group as `Old`: the +relay hop, `moq-rtmp`'s group writes, or the consumer reading a group whose +successor started. Recent candidates touching this path: +[#8910](https://github.com/moq-dev/moq/pull/8910) (an ordered track reports +the groups it gives up on) and +[#8961](https://github.com/moq-dev/moq/pull/8961) (a group's end in one word). + +## Related + +- [Export order](/quest/m3/export-order.md) - the same warning after an outage, where the eviction is real diff --git a/quest/m1/precut-fixes/whep-egress-end.md b/quest/m1/precut-fixes/whep-egress-end.md deleted file mode 100644 index 7fd4c665c6b..00000000000 --- a/quest/m1/precut-fixes/whep-egress-end.md +++ /dev/null @@ -1,37 +0,0 @@ -# [M] A WHEP viewer ends with its broadcast and costs one line per kind - -## Goal - -A WHEP session ends, closing its peer connection, once its broadcast's -catalog ends cleanly and the pumps have written what they hold, so a viewer -learns to reconnect instead of freezing forever. An offer is answered with at -most one audio and one video media line, so one viewer cannot multiply the -server's per-viewer work. A client re-offering from the same address reaches -its new session. - -## Plan - -Found by the 2026-11-17 pre-cut correctness audit, from source; reproduce -first. All are on `release` too. - -- `rs/moq-webrtc/src/session.rs` `run_loop`: a clean catalog end clears - `catalog_end` and keeps looping on `writes_rx`, whose sender - `EgressSource` holds for the session's life, so the session never ends. - [moq-dev/moq#6649](https://github.com/moq-dev/moq/pull/6649) said - `Response::run` fails once the publisher leaves; that holds only for an - unclean leave. Recommended: each pump owns its sender, and the session ends - `Ok` and closes once the writes channel drains after the catalog ends. - Repro: RTMP `deleteStream` while a WHEP viewer plays. -- `session.rs`, `egress.rs`, `sdp.rs`: an offer with K video lines gets K - pumps, each with its own subscription, packetization, encryption, and - 3000-packet buffer. Recommended: reject extra lines as data channels - already are. [RTC pacing](/quest/m3/rtc-pacing.md) caps bandwidth only. -- `server/mux.rs`: a cached source address outranks the STUN username, so a - re-offer from the same address goes to the old session. Route binding - requests by username. - -Public API: none. Wire: none. - -## Related - -- [RTC pacing](/quest/m3/rtc-pacing.md) - paces WHEP viewers to a cap diff --git a/quest/m3/whip-client-delete.md b/quest/m3/whip-client-delete.md new file mode 100644 index 00000000000..a64e8c07b19 --- /dev/null +++ b/quest/m3/whip-client-delete.md @@ -0,0 +1,27 @@ +# [S] A WHIP client ends its resource with a DELETE + +## Goal + +`moq export webrtc --connect` (moq-webrtc's WHIP client) sends a `DELETE` to +the resource URL the server returned once its broadcast ends cleanly, so the +WHIP server finishes the ingested broadcast and serves it for its linger +rather than aborting it. + +## Plan + +Since [moq-dev/moq#9049](https://github.com/moq-dev/moq/pull/9049), a WHIP +client whose broadcast ends cleanly closes its peer connection (RTCP BYE and +DTLS close_notify) and returns `Ok`; before it, the client never ended. The +WHIP server reads that close like a vanished peer: `Session::run` aborts the +sink with `session closed`, so viewers see an abort instead of a finished +broadcast. RFC 9725 ends a session with an HTTP `DELETE` on the `Location` +the `POST` returned, which the server already treats as a clean end +(`AcceptedSession::run`). + +Recommended: `client::whip::dial` keeps the answer's `Location` (resolved +against the dialed URL, keeping its query token), and after `Session::run` +returns `Ok` sends the `DELETE` before returning. Rejected: treating a +peer's close_notify as a clean end on the server, which RFC 9725 does not +ask for and would finish a broadcast a browser abandoned with `pc.close()`. + +Public API: none. Wire: none. diff --git a/rs/moq-webrtc/src/client/mod.rs b/rs/moq-webrtc/src/client/mod.rs index 72d2fc67987..dfe6e6ab3be 100644 --- a/rs/moq-webrtc/src/client/mod.rs +++ b/rs/moq-webrtc/src/client/mod.rs @@ -74,7 +74,9 @@ impl Client { /// `client publish`: push the broadcast `source` resolved to a remote WHIP endpoint, /// until the session ends. /// - /// It stays on that publisher instance, and fails once the instance's route dies. + /// It stays on that publisher instance, and fails once the instance's route dies. A + /// broadcast that ends cleanly closes the peer connection once its last frames are sent, + /// and ends `Ok`. /// The source's origin resolves a rendition whose catalog `broadcast` field references /// a sibling broadcast. pub async fn publish(&self, url: Url, source: moq_mux::Source) -> crate::Result<()> { diff --git a/rs/moq-webrtc/src/egress.rs b/rs/moq-webrtc/src/egress.rs index 0bebcb21c50..04c21cb18f0 100644 --- a/rs/moq-webrtc/src/egress.rs +++ b/rs/moq-webrtc/src/egress.rs @@ -97,7 +97,9 @@ pub struct EgressSource { catalog: watch::Receiver, /// Why the catalog stopped, until the session takes it. catalog_end: Option>>, - writes_tx: mpsc::Sender, + /// Cloned into each pump until [`Self::end`], so the writes channel closes once the + /// catalog ended and every pump has written what it holds. + writes_tx: Option>, writes_rx: Option>, } @@ -122,7 +124,7 @@ impl EgressSource { source, catalog, catalog_end: Some(catalog_end), - writes_tx: tx, + writes_tx: Some(tx), writes_rx: Some(rx), }) } @@ -139,12 +141,23 @@ impl EgressSource { self.catalog_end.take().expect("EgressSource catalog_end already taken") } + /// Open no more lines, as the catalog ended cleanly: the writes channel closes once every + /// pump has served its rendition to the end. + pub(crate) fn end(&mut self) { + self.writes_tx = None; + } + /// Spawn a pump task for a newly added (sendonly) media line. /// /// `mid` and `formats` come from str0m's negotiated state, `formats` in the peer's order. /// The pump serves the best enabled catalog rendition in any of them and forwards every - /// frame as a [`WriteRequest`] in that rendition's format. + /// frame as a [`WriteRequest`] in that rendition's format. A line added after + /// [`Self::end`] serves nothing. pub fn on_track(&mut self, mid: Mid, formats: Vec) -> Result<()> { + let Some(tx) = &self.writes_tx else { + tracing::debug!(?mid, "egress track added after the catalog ended; ignored"); + return Ok(()); + }; if !formats .iter() .any(|format| format.codec == Codec::Opus || video_kind(format.codec).is_some()) @@ -157,7 +170,7 @@ impl EgressSource { formats, source: self.source.clone(), catalog: self.catalog.clone(), - tx: self.writes_tx.clone(), + tx: tx.clone(), playhead: None, }; // Subscribing blocks on SUBSCRIBE_OK, so keep it inside the pump task to keep this @@ -663,7 +676,7 @@ mod tests { source, catalog: watch::channel(catalog).1, catalog_end: None, - writes_tx, + writes_tx: Some(writes_tx), writes_rx: Some(writes_rx), }; diff --git a/rs/moq-webrtc/src/lib.rs b/rs/moq-webrtc/src/lib.rs index 58dddccf3e7..b4794f8c34f 100644 --- a/rs/moq-webrtc/src/lib.rs +++ b/rs/moq-webrtc/src/lib.rs @@ -183,20 +183,17 @@ mod tests { drop(route); } - /// A WHEP viewer stays on the instance its session resolved: a publisher taking over the - /// path never reaches it, so the session never resets its timestamps or codec state, and it - /// fails once that instance's route dies. - #[tokio::test] - async fn a_whep_viewer_stays_on_its_instance_until_the_route_dies() { - let origin = moq_tokio::origin::spawn(); - let (old_route, mut old, old_catalog) = publish_opus(&origin, moq_net::Instance::random()); - push(&mut *old, 20_000, OPUS_PACKET); + /// A WHEP endpoint for `source` on `origin`, with each accepted session's task. + type Sessions = tokio::sync::mpsc::Receiver>>; + /// Serve WHEP plays of `source` from `origin`, returning the endpoint's URL, its HTTP task, + /// and the task running each session it accepts. + async fn serve_whep(origin: &moq_net::origin::Producer) -> (url::Url, tokio::task::JoinHandle<()>, Sessions) { let server = Server::bind("127.0.0.1:0".parse().unwrap(), server::Config::default()) .await .unwrap(); let whep_origin = origin.consume(); - let (sessions_tx, mut sessions) = tokio::sync::mpsc::channel(1); + let (sessions_tx, sessions) = tokio::sync::mpsc::channel(1); let app = Router::new().route( "/whep/source", axum::routing::post(move |offer: String| { @@ -217,22 +214,42 @@ mod tests { }), ); let (address, http) = serve(app).await; + let url = format!("http://{address}/whep/source").parse().expect("WHEP URL"); + (url, http, sessions) + } + /// A WHEP viewer pulling `url` into a fresh broadcast: its task, and its first Opus rendition. + async fn view(url: url::Url) -> (tokio::task::JoinHandle>, Track) { let output_origin = moq_tokio::origin::spawn(); let output = output_origin .route("output", moq_net::origin::RouteOptions::hidden()) .expect("create output broadcast"); let output_consumer = output.broadcast().consume(); let client = Client::new(client::Config::default()).unwrap(); - let url = format!("http://{address}/whep/source").parse().expect("WHEP URL"); let mut fill = output.broadcast().clone(); let catalog = moq_mux::catalog::Producer::new(&mut fill, Default::default()).unwrap(); - let viewer = tokio::spawn(async move { client.pull(url, &fill, &catalog).await }); + let viewer = tokio::spawn(async move { + let _output = output; + client.pull(url, &fill, &catalog).await + }); + (viewer, opus_output(&output_consumer).await) + } + + /// A WHEP viewer stays on the instance its session resolved: a publisher taking over the + /// path never reaches it, so the session never resets its timestamps or codec state, and it + /// fails once that instance's route dies. + #[tokio::test] + async fn a_whep_viewer_stays_on_its_instance_until_the_route_dies() { + let origin = moq_tokio::origin::spawn(); + let (old_route, mut old, old_catalog) = publish_opus(&origin, moq_net::Instance::random()); + push(&mut *old, 20_000, OPUS_PACKET); + + let (url, http, mut sessions) = serve_whep(&origin).await; + let (viewer, mut played) = view(url).await; let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await .expect("WHEP negotiation timed out") .unwrap(); - let mut played = opus_output(&output_consumer).await; assert_eq!(next_payload(&mut played).await, OPUS_PACKET); let _new = take_over(&origin).await; @@ -253,6 +270,41 @@ mod tests { http.abort(); } + /// A broadcast that ends cleanly ends its WHEP sessions once they have sent what they hold, + /// closing the peer connection so the viewer learns to reconnect rather than freeze. + #[tokio::test] + async fn a_whep_viewer_ends_with_its_broadcast() { + let origin = moq_tokio::origin::spawn(); + let (_route, mut bridge, mut catalog) = publish_opus(&origin, moq_net::Instance::random()); + push(&mut *bridge, 20_000, OPUS_PACKET); + + let (url, http, mut sessions) = serve_whep(&origin).await; + let (viewer, mut played) = view(url).await; + let session = kio::clock::timeout(TIMEOUT, sessions.recv()) + .await + .expect("WHEP negotiation timed out") + .unwrap(); + assert_eq!(next_payload(&mut played).await, OPUS_PACKET); + + // The publisher's final packet, then its clean end, as an RTMP `deleteStream` does. + push(&mut *bridge, 40_000, NEW_PACKET); + let _finished = bridge.finish().unwrap(); + catalog.finish().unwrap(); + assert_eq!(next_payload(&mut played).await, NEW_PACKET, "the final packet was lost"); + + kio::clock::timeout(TIMEOUT, session) + .await + .expect("the session outlived its broadcast") + .unwrap() + .expect("the session ended cleanly"); + kio::clock::timeout(TIMEOUT, viewer) + .await + .expect("the viewer never saw its session close") + .unwrap() + .expect("the viewer saw a clean close"); + http.abort(); + } + /// A WHIP push stays on the instance it resolved the same way, and /// [`Client::publish`] fails once that instance's route dies. #[tokio::test] diff --git a/rs/moq-webrtc/src/sdp.rs b/rs/moq-webrtc/src/sdp.rs index 7c871f74acc..e163454128f 100644 --- a/rs/moq-webrtc/src/sdp.rs +++ b/rs/moq-webrtc/src/sdp.rs @@ -2,17 +2,18 @@ //! //! WHIP/WHEP both shovel SDP between a peer and str0m as `application/sdp` //! request/response bodies. On top of str0m's offer/answer parse/serialize this -//! refuses data channels and repairs the answer's text where str0m's falls short. +//! refuses data channels and surplus media lines, and repairs the answer's text where +//! str0m's falls short. use std::{borrow::Cow, collections::HashSet}; use crate::{Error, Result}; -/// An offer's data channel sections, each answered rejected beside str0m's answer. +/// An offer's sections the gateway refuses, each answered rejected beside str0m's answer. /// -/// The gateway carries no data channels, and str0m has no setting that refuses one: it -/// accepts an offered `m=application` section and runs SCTP that nobody reads. So -/// [`parse_offer`] takes those sections out before str0m sees the offer, and +/// str0m has no setting that refuses a section: it accepts an offered `m=application` +/// section and runs SCTP that nobody reads, and gives every media line its own send +/// buffer. So [`parse_offer`] takes those sections out before str0m sees the offer, and /// [`render_answer`] puts each back at its index, with port 0 and out of the BUNDLE /// group. #[derive(Debug, Default)] @@ -22,9 +23,20 @@ pub struct Refused { sections: Vec<(usize, String)>, } -/// Parse an `application/sdp` body as an offer, setting its data channels aside. -pub fn parse_offer(body: &str) -> Result<(str0m::change::SdpOffer, Refused)> { - let (body, refused) = refuse_channels(body); +/// Which of an offer's audio and video sections the answer accepts. +#[derive(Clone, Copy, Debug)] +pub enum Lines { + /// Every one: an ingest publishes each line as a rendition of its own. + Every, + /// The first of each kind: an egress serves one rendition per kind, so another line + /// would only repeat a viewer's subscription, packetization, and encryption. + FirstOfKind, +} + +/// Parse an `application/sdp` body as an offer, setting aside its data channels and any +/// media section `lines` refuses. +pub fn parse_offer(body: &str, lines: Lines) -> Result<(str0m::change::SdpOffer, Refused)> { + let (body, refused) = refuse(body, lines); let offer = str0m::change::SdpOffer::from_sdp_string(&body).map_err(|err| Error::InvalidSdp(err.to_string()))?; Ok((offer, refused)) } @@ -63,14 +75,19 @@ fn port<'a>(section: &[&'a str]) -> Option<&'a str> { section.first()?.split_whitespace().nth(1) } -/// Take each `m=application` section out of an offer, and its mid out of the BUNDLE group. +/// Take each `m=application` section out of an offer, and each audio or video section +/// `lines` refuses, with its mid out of the BUNDLE group. /// /// A refused section in the group keeps its share of the group's transport: each /// attribute no other section or the session carries moves to the session, as do its /// candidates, which str0m reads from either. -fn refuse_channels(offer: &str) -> (Cow<'_, str>, Refused) { +fn refuse(offer: &str, lines: Lines) -> (Cow<'_, str>, Refused) { let mut refused = Refused::default(); - if !offer.contains("m=application") { + let surplus = match lines { + Lines::Every => false, + Lines::FirstOfKind => offer.matches("m=audio").nth(1).is_some() || offer.matches("m=video").nth(1).is_some(), + }; + if !surplus && !offer.contains("m=application") { return (Cow::Borrowed(offer), refused); } @@ -85,18 +102,34 @@ fn refuse_channels(offer: &str) -> (Cow<'_, str>, Refused) { let mut kept = Vec::new(); let mut mids = HashSet::new(); let mut transport = Vec::new(); + let (mut audio, mut video) = (false, false); for (index, section) in sections.into_iter().enumerate() { let mut tokens = section[0].split_whitespace(); - if tokens.next() != Some("m=application") { + let media = tokens.next().unwrap_or_default(); + // A disabled (port 0) section counts too: a max-bundle offer disables every section + // but its first, marking them bundle-only, and str0m still accepts them. + let seen = match media { + "m=audio" => Some(&mut audio), + "m=video" => Some(&mut video), + _ => None, + }; + let keep = match seen { + Some(seen) => matches!(lines, Lines::Every) || !std::mem::replace(seen, true), + None => media != "m=application", + }; + if keep { kept.push(section); continue; } - // m=application ...; the answer echoes the proto and formats. + // m= ...; the answer echoes the media and proto. A data + // channel echoes its formats, and a media line takes the placeholder static payload + // [`render_answer`] gives one str0m rejects, since a dynamic one needs its `a=rtpmap`. let _port = tokens.next(); - let mut answer = format!( - "m=application 0 {}\r\nc=IN IP4 0.0.0.0\r\n", - tokens.collect::>().join(" ") - ); + let formats = match media { + "m=application" => tokens.collect::>().join(" "), + _ => format!("{} 0", tokens.next().unwrap_or_default()), + }; + let mut answer = format!("{media} 0 {formats}\r\nc=IN IP4 0.0.0.0\r\n"); let mid = section.iter().find_map(|line| line.trim_end().strip_prefix("a=mid:")); if let Some(mid) = mid { answer.push_str(&format!("a=mid:{mid}\r\n")); @@ -109,6 +142,10 @@ fn refuse_channels(offer: &str) -> (Cow<'_, str>, Refused) { ); } } + // A media section names its direction, which an offerer such as str0m requires. + if media != "m=application" { + answer.push_str("a=inactive\r\n"); + } refused.sections.push((index, answer)); } @@ -307,7 +344,7 @@ mod tests { /// Answer `offer` as the WHIP server does, with one local candidate. fn answer(offer: &str) -> String { - let (offer, refused) = parse_offer(offer).unwrap(); + let (offer, refused) = parse_offer(offer, super::Lines::Every).unwrap(); let mut rtc = crate::session::rtc_with_codecs(&[str0m::format::Codec::Opus]).unwrap(); let candidate = Candidate::host(([1, 1, 1, 1], 1000).into(), "udp").unwrap(); rtc.add_local_candidate(candidate); @@ -434,7 +471,7 @@ mod tests { let video = api.add_media(MediaKind::Video, Direction::RecvOnly, None, None, None); let (offer, pending) = api.apply().unwrap(); - let (offer, refused) = parse_offer(&offer.to_sdp_string()).unwrap(); + let (offer, refused) = parse_offer(&offer.to_sdp_string(), super::Lines::Every).unwrap(); let mut rtc = crate::session::rtc_with_codecs(&[str0m::format::Codec::Vp8]).unwrap(); rtc.add_local_candidate(Candidate::host(([1, 1, 1, 1], 1000).into(), "udp").unwrap()); let answer = render_answer(&rtc.sdp_api().accept_offer(offer).unwrap(), &refused); @@ -450,6 +487,58 @@ mod tests { offerer.sdp_api().accept_answer(pending, answer).unwrap(); } + /// A play answers the first audio and video line and rejects the rest beside them, so a + /// viewer cannot multiply its work, while an ingest keeps every line as a rendition. + #[test] + fn a_play_answers_one_line_per_kind() { + use str0m::format::Codec; + + let now = kio::clock::now().into_std(); + let mut offerer = crate::session::rtc_config().unwrap().build(now); + let mut api = offerer.sdp_api(); + let kinds = [ + MediaKind::Video, + MediaKind::Audio, + MediaKind::Video, + MediaKind::Audio, + MediaKind::Video, + ]; + let mids: Vec<_> = (kinds.into_iter()) + .map(|kind| api.add_media(kind, Direction::RecvOnly, None, None, None)) + .collect(); + let (offer, pending) = api.apply().unwrap(); + let offer = offer.to_sdp_string(); + + let answer = |lines| { + let (offer, refused) = parse_offer(&offer, lines).unwrap(); + let mut rtc = crate::session::rtc_with_codecs(&[Codec::Opus, Codec::Vp8]).unwrap(); + rtc.add_local_candidate(Candidate::host(([1, 1, 1, 1], 1000).into(), "udp").unwrap()); + render_answer(&rtc.sdp_api().accept_offer(offer).unwrap(), &refused) + }; + let ports = |answer: &str| -> Vec { + (answer.split("\r\nm=").skip(1)) + .map(|section| section.split_whitespace().nth(1).unwrap().to_string()) + .collect() + }; + + let played = answer(super::Lines::FirstOfKind); + assert_eq!(ports(&played), ["9", "9", "0", "0", "0"], "{played}"); + for (mid, kind) in mids.iter().zip(["video", "audio"].into_iter().cycle()).skip(2) { + let rejected = format!("m={kind} 0 UDP/TLS/RTP/SAVPF 0\r\n"); + let at = played.find(&format!("a=mid:{mid}\r\n")).unwrap(); + assert!(played[..at].rfind("m=") == played[..at].rfind(&rejected), "{played}"); + } + assert!( + played.contains(&format!("a=group:BUNDLE {} {}\r\n", mids[0], mids[1])), + "{played}" + ); + let accepted = SdpAnswer::from_sdp_string(&played).unwrap(); + offerer.sdp_api().accept_answer(pending, accepted).unwrap(); + + let ingested = answer(super::Lines::Every); + assert_eq!(ports(&ingested), ["9"; 5], "{ingested}"); + } + /// An offer of only a data channel has nothing to answer. #[test] fn an_offer_of_only_a_data_channel_has_no_bundle() { @@ -461,7 +550,7 @@ mod tests { m=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n\ c=IN IP4 0.0.0.0\r\n\ a=mid:dc\r\n"; - let (body, refused) = super::refuse_channels(offer); + let (body, refused) = super::refuse(offer, super::Lines::Every); assert_eq!(body, "v=0\r\no=- 1 2 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\n"); assert_eq!(refused.sections.len(), 1); } @@ -492,7 +581,7 @@ mod tests { } // Quadratic work here takes ~10^10 comparisons, past nextest's timeout. - let (body, refused) = super::refuse_channels(&offer); + let (body, refused) = super::refuse(&offer, super::Lines::Every); assert_eq!(refused.sections.len(), LINES + 1); assert_eq!(body.matches("a=ice-ufrag:").count(), 1); assert_eq!(body.matches("a=setup:").count(), 1); diff --git a/rs/moq-webrtc/src/server/mod.rs b/rs/moq-webrtc/src/server/mod.rs index c243d01e492..aa0525cbac5 100644 --- a/rs/moq-webrtc/src/server/mod.rs +++ b/rs/moq-webrtc/src/server/mod.rs @@ -38,6 +38,8 @@ pub struct Response { impl Response { /// Run the negotiated media session until the peer disconnects, DELETE terminates it, or it errors. + /// A WHEP session also ends `Ok` once its broadcast ends cleanly and it has sent the last + /// frames, closing the peer connection. pub async fn run(self) -> Result<()> { self.session.run().await } diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index 4c05ac2e39d..ac330e52e93 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -10,9 +10,10 @@ //! Routing key: ICE. A session is registered under the local ICE ufrag we mint //! for it ([`IceCreds::new`]). The peer's first packets are STUN binding //! requests whose USERNAME is `:`, so we parse the local -//! ufrag out of the STUN message and look the session up. Once we've seen a -//! source address we cache `addr -> session`, so subsequent DTLS/RTP/RTCP (which -//! carry no ufrag) route by address on the fast path. +//! ufrag out of the STUN message and look the session up. Each binding request +//! caches `addr -> session`, so subsequent DTLS/RTP/RTCP (which carry no ufrag) +//! route by address. A binding request always routes by its ufrag, so a peer that +//! re-offers from the same address moves the address to its new session. //! //! Backpressure mirrors a UDP socket buffer: each session has a bounded inbox //! and a full inbox drops the datagram (WebRTC tolerates loss). A closed inbox @@ -91,12 +92,15 @@ impl Drop for Registration { } impl Registry { + /// The session a datagram from `src` goes to: the one a binding request's `ufrag` names, + /// which then holds `src`, or else whichever holds `src`. fn route(&mut self, src: SocketAddr, ufrag: Option<&str>) -> Option> { - let entry = self.by_addr.get(&src).cloned().or_else(|| { - let entry = self.by_ufrag.get(ufrag?).cloned()?; + if let Some(entry) = ufrag.and_then(|ufrag| self.by_ufrag.get(ufrag)) { + let entry = entry.clone(); self.by_addr.insert(src, entry.clone()); - Some(entry) - })?; + return Some(entry); + } + let entry = self.by_addr.get(&src).cloned()?; if entry.sender.is_closed() { self.by_addr.remove(&src); return None; @@ -187,12 +191,8 @@ async fn demux(socket: Arc, registry: Arc>) { let src = crate::ice::canonical(src); let data = &buf[..len]; - // Cached peers need one routing lock; only a new address parses STUN. - let entry = registry.lock().unwrap().route(src, None); - let entry = entry.or_else(|| { - let ufrag = local_ufrag(data)?; - registry.lock().unwrap().route(src, Some(&ufrag)) - }); + let ufrag = local_ufrag(data); + let entry = registry.lock().unwrap().route(src, ufrag.as_deref()); let Some(entry) = entry else { continue; }; @@ -212,6 +212,10 @@ async fn demux(socket: Arc, registry: Arc>) { /// Extract the local ICE ufrag from a STUN binding request, if `data` is one. /// The USERNAME is `:`; we route on the local half. fn local_ufrag(data: &[u8]) -> Option { + // Only STUN starts with 0..=3 (RFC 7983), so media skips the parse. + if *data.first()? > 3 { + return None; + } let msg = StunMessage::parse(data).ok()?; if !msg.is_binding_request() { return None; @@ -297,6 +301,42 @@ mod tests { assert!(registry.route(address_a, Some(&a.ufrag)).is_none()); } + /// A peer re-offering from the address its old session still holds reaches its new + /// session: a binding request routes by the session it names, and moves the address there. + #[tokio::test] + async fn a_reoffer_from_the_same_address_reaches_its_new_session() { + let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); + let port = mux.local_addr().port(); + let (old, mut old_inbound, _old_registration) = mux.register(); + let (new, mut new_inbound, _new_registration) = mux.register(); + let peer = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); + let recv = async |inbound: &mut Inbound| { + kio::clock::timeout(Duration::from_secs(5), inbound.packets.recv()) + .await + .expect("the demux routes the datagram") + .expect("session inbox stayed open") + .0 + }; + + let request = binding_request(&old); + peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + assert_eq!(recv(&mut old_inbound).await, request); + + let request = binding_request(&new); + peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + assert_eq!(recv(&mut new_inbound).await, request); + + // What follows, such as DTLS, carries no ufrag and follows the address to its new session. + peer.send_to(&[22, 254, 253], (Ipv4Addr::LOCALHOST, port)) + .await + .unwrap(); + assert_eq!(recv(&mut new_inbound).await, [22, 254, 253]); + assert!( + old_inbound.packets.try_recv().is_err(), + "the old session kept the address" + ); + } + /// An IPv4 peer reaching a dual-stack (`[::]`) mux must route to its session /// with its source unmapped to the `V4` it really is. Without the /// canonicalization the source stays `::ffff:127.0.0.1`, which pairs the peer diff --git a/rs/moq-webrtc/src/server/whep.rs b/rs/moq-webrtc/src/server/whep.rs index dea1c9d3bd5..57f0718f7e5 100644 --- a/rs/moq-webrtc/src/server/whep.rs +++ b/rs/moq-webrtc/src/server/whep.rs @@ -139,7 +139,8 @@ impl<'a> Request<'a> { /// enforced by moq-net exactly as for a native session; the bundled [`Server::subscribe_router`] /// passes the server's own (unauthenticated) consumer. It parses the offer, /// resolves the broadcast on `subscriber`, restricts the answer to the codecs the -/// catalog actually has, registers a media session on the shared mux, and +/// catalog actually has and to at most one audio and one video line (rejecting the +/// rest, as it does data channels), registers a media session on the shared mux, and /// returns the SDP answer plus an opaque `resource_id` for the WHEP `Location` /// header. The caller must run the returned [`Response`] to drive the MoQ->RTP /// session. Mirrors [`whip::accept`](super::whip::accept). @@ -158,7 +159,7 @@ pub async fn accept(server: &Server, request: Request<'_>) -> Result { broadcast, offer, } = request; - let (offer, refused) = sdp::parse_offer(offer)?; + let (offer, refused) = sdp::parse_offer(offer, sdp::Lines::FirstOfKind)?; let source = resolve(subscriber, broadcast).await?; let codecs = source.catalog_codecs(); diff --git a/rs/moq-webrtc/src/server/whip.rs b/rs/moq-webrtc/src/server/whip.rs index 39cda0b218c..289f3ea17c2 100644 --- a/rs/moq-webrtc/src/server/whip.rs +++ b/rs/moq-webrtc/src/server/whip.rs @@ -161,7 +161,7 @@ pub async fn accept(server: &Server, request: Request<'_>) -> Result { offer, mut ingest, } = request; - let (offer, refused) = sdp::parse_offer(offer)?; + let (offer, refused) = sdp::parse_offer(offer, sdp::Lines::Every)?; // Create the broadcast hidden up front, so an out-of-scope offer is refused before // any mux work. It announces only once negotiation succeeds (below). diff --git a/rs/moq-webrtc/src/session.rs b/rs/moq-webrtc/src/session.rs index af0e9816dbc..5b14361780b 100644 --- a/rs/moq-webrtc/src/session.rs +++ b/rs/moq-webrtc/src/session.rs @@ -191,7 +191,8 @@ impl Session { } } - /// Drive the media session until it fails or the peer disconnects, which is `Ok`. + /// Drive the media session until it fails, the peer disconnects, or an egress has sent its + /// broadcast's end and closed; the last two are `Ok`. pub async fn run(&mut self) -> Result<()> { let result = self.run_loop().await; // Any end but the peer's DELETE, a disconnect or an offer that never connected @@ -207,7 +208,7 @@ impl Session { result } - /// Returns `Ok` only when the peer disconnects. + /// Returns `Ok` only when the peer disconnects or an egress closed after its broadcast's end. async fn run_loop(&mut self) -> Result<()> { loop { let remb_at = self.rate.as_ref().and_then(IngestRate::remb_at); @@ -216,9 +217,10 @@ impl Session { biased; // A failed catalog, as every subscription fails once the source's route dies, - // means nothing more will flow, so fail rather than idle. A clean end keeps - // serving what the pumps still hold. Polled before the writes, which could - // otherwise stay ready and starve it; it completes once. + // means nothing more will flow, so fail rather than idle. A clean end opens no + // more lines, and the pumps serve what they still hold until the writes run dry. + // Polled before the writes, which could otherwise stay ready and starve it; it + // completes once. ended = async { match self.catalog_end.as_mut() { Some(end) => end.await, @@ -229,17 +231,29 @@ impl Session { if let Ok(Err(err)) = ended { return Err(err); } + if let MediaRole::Egress(source) = &mut self.role { + source.end(); + } } - Some(req) = async { + req = async { match self.writes_rx.as_mut() { Some(rx) => rx.recv().await, None => std::future::pending::>().await, } - } => { - let wallclock = self.egress_clock.wallclock(req.time, Instant::now()); - crate::egress::dispatch(&mut self.agent.rtc_mut(), req, wallclock); - } + } => match req { + Some(req) => { + let wallclock = self.egress_clock.wallclock(req.time, Instant::now()); + crate::egress::dispatch(&mut self.agent.rtc_mut(), req, wallclock); + } + // The catalog ended and every pump has written its last frame, which str0m + // already sent: close with an RTCP BYE and a DTLS close_notify, so the peer + // learns at once that the broadcast is over, and end once they are out. + None => { + self.writes_rx = None; + self.agent.rtc_mut().close().map_err(Error::rtc)?; + } + }, cap = async { match self.rate.as_mut() { From c24ec2362256262c757fc0a882818bf51fc6238f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 18:08:36 -0800 Subject: [PATCH 3/8] fix(webrtc): a close sends what was written, and a forged request moves no address Review round 1: the session closes through Agent::close, which waits until str0m is connected with nothing due, since it packetizes a written frame only on a timeout. A clean catalog end waits for the peer to connect, when str0m opens the negotiated lines, before the source stops opening lines. The mux moves a cached address only for a binding request signed with the named session's password. Co-Authored-By: Claude Opus 5.5 --- quest/m3/webrtc-clean-end.md | 41 ++++++++++++ quest/m3/whip-client-delete.md | 27 -------- rs/moq-webrtc/src/ice/agent.rs | 108 +++++++++++++++++++++++++++++++- rs/moq-webrtc/src/lib.rs | 93 ++++++++++++++++++++++++--- rs/moq-webrtc/src/server/mux.rs | 93 +++++++++++++++++++-------- rs/moq-webrtc/src/session.rs | 68 +++++++++++++------- 6 files changed, 346 insertions(+), 84 deletions(-) create mode 100644 quest/m3/webrtc-clean-end.md delete mode 100644 quest/m3/whip-client-delete.md diff --git a/quest/m3/webrtc-clean-end.md b/quest/m3/webrtc-clean-end.md new file mode 100644 index 00000000000..9a379d90e4d --- /dev/null +++ b/quest/m3/webrtc-clean-end.md @@ -0,0 +1,41 @@ +# [S] A clean end crosses the WebRTC gateway as a clean end + +## Goal + +A broadcast that ends cleanly on one side of a WebRTC session ends cleanly +on the other. moq-webrtc's WHIP client (`moq export webrtc --connect`) ends +its resource with a `DELETE`, so the WHIP server finishes the ingested +broadcast and serves it for its linger. Its WHEP client +(`moq import webrtc --connect`) finishes its output when the server closes +cleanly, keeping the frames it already received, instead of aborting it. + +## Plan + +Since [moq-dev/moq#9049](https://github.com/moq-dev/moq/pull/9049), an egress +whose broadcast ends cleanly closes its peer connection (RTCP BYE and DTLS +close_notify) once its last frames are sent; before it, the session never +ended. An ingest reads that close like a vanished peer: `Session::run` aborts +the sink with `session closed`. So a WHIP server aborts a broadcast whose +publisher ended cleanly, and moq's WHEP client aborts its output, which +fails any read of a frame it received but its readers had not reached yet +(seen while writing #9049's tests). + +- WHIP client: `client::whip::dial` keeps the answer's `Location` (resolved + against the dialed URL, keeping its query token), and after `Session::run` + returns `Ok` sends the `DELETE` (RFC 9725) before returning. +- WHEP client: a WHEP server has no request to end a session with, so the + server's close_notify (str0m's `Event::Closed`) is its only clean end. + Recommended: an ingest session that sees `Event::Closed` returns as the + peer's clean end, and `Client::pull` finishes the sink as `AcceptedSession` + does after a `DELETE`. + +Public API: none. Wire: the WHIP client sends a `DELETE`. + +## Waiting + +- Maintainer: whether a WHIP server also reads a publisher's close_notify as + a clean end, finishing and lingering the broadcast, or only a `DELETE`. A + browser's `pc.close()` sends close_notify without a `DELETE`. Recommended: + both, since close_notify is a deliberate end and only a vanished peer + should abort; rejected: `DELETE` only, which RFC 9725 names but which + aborts the broadcast of every publisher that skips it. diff --git a/quest/m3/whip-client-delete.md b/quest/m3/whip-client-delete.md deleted file mode 100644 index a64e8c07b19..00000000000 --- a/quest/m3/whip-client-delete.md +++ /dev/null @@ -1,27 +0,0 @@ -# [S] A WHIP client ends its resource with a DELETE - -## Goal - -`moq export webrtc --connect` (moq-webrtc's WHIP client) sends a `DELETE` to -the resource URL the server returned once its broadcast ends cleanly, so the -WHIP server finishes the ingested broadcast and serves it for its linger -rather than aborting it. - -## Plan - -Since [moq-dev/moq#9049](https://github.com/moq-dev/moq/pull/9049), a WHIP -client whose broadcast ends cleanly closes its peer connection (RTCP BYE and -DTLS close_notify) and returns `Ok`; before it, the client never ended. The -WHIP server reads that close like a vanished peer: `Session::run` aborts the -sink with `session closed`, so viewers see an abort instead of a finished -broadcast. RFC 9725 ends a session with an HTTP `DELETE` on the `Location` -the `POST` returned, which the server already treats as a clean end -(`AcceptedSession::run`). - -Recommended: `client::whip::dial` keeps the answer's `Location` (resolved -against the dialed URL, keeping its query token), and after `Session::run` -returns `Ok` sends the `DELETE` before returning. Rejected: treating a -peer's close_notify as a clean end on the server, which RFC 9725 does not -ask for and would finish a broadcast a browser abandoned with `pc.close()`. - -Public API: none. Wire: none. diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index 74a9bd3b520..9346cf9c1ae 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -72,6 +72,8 @@ pub struct Agent { /// Datagrams the socket refused for lack of buffer space, sent first next time. outbox: VecDeque<(Vec, SocketAddr)>, phase: Phase, + /// Close once connected with nothing due, which [`Agent::close`] asked for. + closing: bool, } impl Agent { @@ -94,6 +96,7 @@ impl Agent { timeout: now, outbox: VecDeque::new(), phase: Phase::Establishing { started: now }, + closing: false, }; // str0m's ICE has no clock, and asks for no timeout, until its first one. Without // it a side that hears no checks, such as one facing an ICE-lite peer, would never @@ -117,6 +120,21 @@ impl Agent { RtcMut(self) } + /// An event str0m already surfaced, without driving the connection. + pub fn queued(&mut self) -> Option { + self.events.pop_front() + } + + /// Close the connection with an RTCP BYE and a DTLS close_notify once it has sent + /// everything written to it, after which [`Agent::next`] reports [`Error::Closed`]. + /// + /// str0m packetizes a written frame only on a timeout, and holds media until DTLS and + /// SRTP are up, so the close waits for a connection with nothing due. A peer that never + /// connects still ends with [`Error::Timeout`]. + pub fn close(&mut self) { + self.closing = true; + } + /// Drive the connection until str0m surfaces an [`Event`]. ICE disconnection is /// reported as [`Error::Closed`] instead. /// @@ -127,9 +145,7 @@ impl Agent { if let Some(event) = self.events.pop_front() { return Ok(event); } - if let Phase::Failed(err) = &self.phase { - return Err(err.clone()); - } + // Sent before reporting an end too, which may have queued a close_notify. while let Some((data, dst)) = self.outbox.front() { match self.socket.send_to(data, *dst).await { Ok(_) => {} @@ -137,6 +153,9 @@ impl Agent { } self.outbox.pop_front(); } + if let Phase::Failed(err) = &self.phase { + return Err(err.clone()); + } // A dead Rtc (DTLS failure, explicit disconnect) reports a never-firing // timeout instead of an error, which would park this loop forever. @@ -152,6 +171,15 @@ impl Agent { let deadline = establish.map_or(self.timeout, |at| at.min(self.timeout)); let now = Instant::now(); + // Once SRTP is up with nothing due, str0m has packetized and sent every frame + // written to it. + if deadline > now && self.rtc.is_connected() && std::mem::take(&mut self.closing) { + let result = self.rtc_mut().close(); + if let Err(err) = result { + self.fail(Error::rtc(err)); + } + continue; + } if deadline > now { tokio::select! { packet = self.inbound.packets.recv() => { @@ -441,6 +469,80 @@ mod tests { } } + /// A close sends every frame written before it: str0m packetizes a frame only on a + /// timeout, so closing at once would drop what it still queued. + #[tokio::test] + async fn a_close_sends_what_was_written_first() { + use str0m::media::{Direction, Frequency, MediaKind, MediaTime}; + const FRAMES: u64 = 50; + + let now = Instant::now().into_std(); + let mut sender = crate::session::rtc_config().unwrap().build(now); + let mut receiver = crate::session::rtc_config().unwrap().build(now); + // Candidates go into the SDP, so bind before the exchange. + let sender_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap()); + let receiver_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap()); + for (rtc, socket) in [(&mut sender, &sender_socket), (&mut receiver, &receiver_socket)] { + let local = socket.local_addr().unwrap(); + rtc.add_local_candidate(str0m::Candidate::host(local, "udp").unwrap()); + } + let mut api = sender.sdp_api(); + let mid = api.add_media(MediaKind::Audio, Direction::SendOnly, None, None, None); + let (offer, pending) = api.apply().unwrap(); + let answer = receiver.sdp_api().accept_offer(offer).unwrap(); + sender.sdp_api().accept_answer(pending, answer).unwrap(); + let agent = |rtc, socket: Arc| { + let local = socket.local_addr().unwrap(); + Agent::new( + rtc, + Socket::new(socket.clone(), vec![local], crate::ice::reader(socket)).unwrap(), + ) + .unwrap() + }; + let mut sender = agent(sender, sender_socket); + let mut receiver = agent(receiver, receiver_socket); + + while !(sender.rtc().is_connected() && receiver.rtc().is_connected()) { + tokio::select! { + event = sender.next() => { event.unwrap(); } + event = receiver.next() => { event.unwrap(); } + } + } + + // Every frame written in one go, as a session drains its writes, then the close. + let mut rtc = sender.rtc_mut(); + let pt = rtc.writer(mid).unwrap().payload_params().next().unwrap().pt(); + for n in 0..FRAMES { + let time = MediaTime::new(960 * n, Frequency::FORTY_EIGHT_KHZ); + rtc.writer(mid) + .unwrap() + .write(pt, now, time, vec![0xfc, n as u8]) + .unwrap(); + } + drop(rtc); + sender.close(); + + let mut received = 0; + let mut sending = true; + loop { + tokio::select! { + event = sender.next(), if sending => match event { + Ok(_) => {} + Err(Error::Closed) => sending = false, + Err(err) => panic!("the sender failed: {err:?}"), + }, + event = receiver.next() => match event { + Ok(Event::MediaData(_)) => received += 1, + Ok(_) => {} + // The sender's close_notify ends the receiver. + Err(Error::Closed) => break, + Err(err) => panic!("the receiver failed: {err:?}"), + }, + } + } + assert_eq!(received, FRAMES, "frames written before the close were lost"); + } + #[test] fn mapped_addresses_round_trip() { let v4: SocketAddr = "1.2.3.4:5000".parse().unwrap(); diff --git a/rs/moq-webrtc/src/lib.rs b/rs/moq-webrtc/src/lib.rs index b4794f8c34f..b58012f8aa9 100644 --- a/rs/moq-webrtc/src/lib.rs +++ b/rs/moq-webrtc/src/lib.rs @@ -235,6 +235,57 @@ mod tests { (viewer, opus_output(&output_consumer).await) } + /// A bare str0m WHEP viewer of `url`, an audio line alone: each payload it receives, until + /// the channel closes with the server's close. Unlike [`Client::pull`], which aborts its + /// output when the session ends, it keeps everything that arrived. + async fn raw_view(url: url::Url) -> tokio::sync::mpsc::UnboundedReceiver> { + use str0m::media::{Direction, MediaKind}; + + let socket = crate::session::bind_udp().await.unwrap(); + let mut rtc = crate::session::rtc_config() + .unwrap() + .build(kio::clock::now().into_std()); + for addr in socket.locals() { + rtc.add_local_candidate(str0m::Candidate::host(*addr, "udp").unwrap()); + } + let mut api = rtc.sdp_api(); + api.add_media(MediaKind::Audio, Direction::RecvOnly, None, None, None); + let (offer, pending) = api.apply().unwrap(); + let client = Client::new(client::Config::default()).unwrap(); + let answer = (client.http().post(url)) + .header(reqwest::header::CONTENT_TYPE, "application/sdp") + .body(offer.to_sdp_string()) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + let answer = str0m::change::SdpAnswer::from_sdp_string(&answer).unwrap(); + rtc.sdp_api().accept_answer(pending, answer).unwrap(); + let mut agent = crate::ice::Agent::new(rtc, socket).unwrap(); + + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); + tokio::spawn(async move { + loop { + match agent.next().await { + Ok(str0m::Event::MediaData(data)) => tx.send(data.data.to_vec()).unwrap(), + Ok(_) => {} + Err(crate::ice::Error::Closed) => return, + Err(err) => panic!("the viewer failed: {err:?}"), + } + } + }); + rx + } + + /// The next payload `viewer` receives, or `None` once the server closed. + async fn received(viewer: &mut tokio::sync::mpsc::UnboundedReceiver>) -> Option> { + kio::clock::timeout(TIMEOUT, viewer.recv()) + .await + .expect("the viewer neither received nor closed") + } + /// A WHEP viewer stays on the instance its session resolved: a publisher taking over the /// path never reaches it, so the session never resets its timestamps or codec state, and it /// fails once that instance's route dies. @@ -279,29 +330,57 @@ mod tests { push(&mut *bridge, 20_000, OPUS_PACKET); let (url, http, mut sessions) = serve_whep(&origin).await; - let (viewer, mut played) = view(url).await; + let mut viewer = raw_view(url).await; let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await .expect("WHEP negotiation timed out") .unwrap(); - assert_eq!(next_payload(&mut played).await, OPUS_PACKET); + assert_eq!(received(&mut viewer).await.unwrap(), OPUS_PACKET); // The publisher's final packet, then its clean end, as an RTMP `deleteStream` does. push(&mut *bridge, 40_000, NEW_PACKET); let _finished = bridge.finish().unwrap(); catalog.finish().unwrap(); - assert_eq!(next_payload(&mut played).await, NEW_PACKET, "the final packet was lost"); - + assert_eq!( + received(&mut viewer).await.as_deref(), + Some(NEW_PACKET), + "the final packet was lost" + ); + assert_eq!(received(&mut viewer).await, None, "the viewer was not closed"); kio::clock::timeout(TIMEOUT, session) .await .expect("the session outlived its broadcast") .unwrap() .expect("the session ended cleanly"); - kio::clock::timeout(TIMEOUT, viewer) + http.abort(); + } + + /// A viewer joining once the catalog ended, while a track still sends, plays that track to + /// its end before its session closes. + #[tokio::test] + async fn a_whep_viewer_joining_after_the_catalog_ends_plays_the_rest() { + let origin = moq_tokio::origin::spawn(); + let (_route, mut bridge, mut catalog) = publish_opus(&origin, moq_net::Instance::random()); + push(&mut *bridge, 20_000, OPUS_PACKET); + catalog.finish().unwrap(); + + let (url, http, mut sessions) = serve_whep(&origin).await; + let mut viewer = raw_view(url).await; + let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await - .expect("the viewer never saw its session close") + .expect("WHEP negotiation timed out") + .unwrap(); + // The line subscribed at the group it found, so it plays that group. + assert_eq!(received(&mut viewer).await.as_deref(), Some(OPUS_PACKET)); + push(&mut *bridge, 40_000, NEW_PACKET); + let _finished = bridge.finish().unwrap(); + assert_eq!(received(&mut viewer).await.as_deref(), Some(NEW_PACKET)); + assert_eq!(received(&mut viewer).await, None, "the viewer was not closed"); + kio::clock::timeout(TIMEOUT, session) + .await + .expect("the session outlived its broadcast") .unwrap() - .expect("the viewer saw a clean close"); + .expect("the session ended cleanly"); http.abort(); } diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index ac330e52e93..f6ce9db3a4e 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -10,10 +10,11 @@ //! Routing key: ICE. A session is registered under the local ICE ufrag we mint //! for it ([`IceCreds::new`]). The peer's first packets are STUN binding //! requests whose USERNAME is `:`, so we parse the local -//! ufrag out of the STUN message and look the session up. Each binding request -//! caches `addr -> session`, so subsequent DTLS/RTP/RTCP (which carry no ufrag) -//! route by address. A binding request always routes by its ufrag, so a peer that -//! re-offers from the same address moves the address to its new session. +//! ufrag out of the STUN message and look the session up. A binding request +//! signed with that session's password caches `addr -> session`, so subsequent +//! DTLS/RTP/RTCP (which carry no ufrag) route by address. A binding request always +//! routes by its ufrag, so a peer that re-offers from the same address moves the +//! address to its new session, while a forged one cannot move another peer's. //! //! Backpressure mirrors a UDP socket buffer: each session has a bounded inbox //! and a full inbox drops the datagram (WebRTC tolerates loss). A closed inbox @@ -33,8 +34,9 @@ use crate::Result; use crate::session::advertised_candidates; /// Per-session routing table, shared between the demux task and the accept path. -#[derive(Default)] struct Registry { + /// Checks a binding request's MESSAGE-INTEGRITY, as str0m does. + hmac: &'static dyn str0m::crypto::Sha1HmacProvider, /// Local ICE ufrag -> session inbox. Populated at registration, the only /// way a brand-new peer (STUN binding request) finds its session. by_ufrag: HashMap>, @@ -45,6 +47,8 @@ struct Registry { struct Entry { ufrag: String, + /// The session's ICE password, which only its peer can sign a binding request with. + pass: String, sender: mpsc::Sender, } @@ -92,12 +96,20 @@ impl Drop for Registration { } impl Registry { - /// The session a datagram from `src` goes to: the one a binding request's `ufrag` names, - /// which then holds `src`, or else whichever holds `src`. - fn route(&mut self, src: SocketAddr, ufrag: Option<&str>) -> Option> { - if let Some(entry) = ufrag.and_then(|ufrag| self.by_ufrag.get(ufrag)) { - let entry = entry.clone(); - self.by_addr.insert(src, entry.clone()); + /// The session a datagram from `src` goes to: the one a binding `request` names, or else + /// whichever holds `src`. A request signed with that session's password moves `src` to + /// it, so a peer re-offering from the same address reaches its new session; a forged one + /// still reaches the session it names, which drops it, but leaves `src` where it was. + fn route(&mut self, src: SocketAddr, request: Option<&StunMessage>) -> Option> { + let named = request.and_then(|request| { + let (local, _remote) = request.split_username()?; + Some((request, self.by_ufrag.get(local)?.clone())) + }); + if let Some((request, entry)) = named { + let hmac = self.hmac; + if request.verify(entry.pass.as_bytes(), |key, payloads| hmac.sha1_hmac(key, payloads)) { + self.by_addr.insert(src, entry.clone()); + } return Some(entry); } let entry = self.by_addr.get(&src).cloned()?; @@ -119,7 +131,14 @@ impl Mux { let local = socket.local_addr()?; let candidates = advertised_candidates(ice_candidates, local)?; - let registry = Arc::new(Mutex::new(Registry::default())); + moq_crypto::cpu::check()?; + // rind-str0m repeats the check above, so its error is unreachable here. + let crypto = rind_str0m::default_provider().expect("the CPU check passed"); + let registry = Arc::new(Mutex::new(Registry { + hmac: crypto.sha1_hmac_provider, + by_ufrag: HashMap::new(), + by_addr: HashMap::new(), + })); let demux = tokio::spawn(demux(socket.clone(), registry.clone())); tracing::info!(?candidates, bound = %local, "webrtc media mux listening"); @@ -146,6 +165,7 @@ impl Mux { let (tx, rx) = mpsc::channel(crate::ice::INBOX); let entry = Arc::new(Entry { ufrag: creds.ufrag.clone(), + pass: creds.pass.clone(), sender: tx, }); self.registry @@ -191,8 +211,8 @@ async fn demux(socket: Arc, registry: Arc>) { let src = crate::ice::canonical(src); let data = &buf[..len]; - let ufrag = local_ufrag(data); - let entry = registry.lock().unwrap().route(src, ufrag.as_deref()); + let request = parse_binding(data); + let entry = registry.lock().unwrap().route(src, request.as_ref()); let Some(entry) = entry else { continue; }; @@ -209,18 +229,15 @@ async fn demux(socket: Arc, registry: Arc>) { } } -/// Extract the local ICE ufrag from a STUN binding request, if `data` is one. -/// The USERNAME is `:`; we route on the local half. -fn local_ufrag(data: &[u8]) -> Option { +/// `data` as a STUN binding request, if it is one. Its USERNAME is +/// `:`; we route on the local half. +fn parse_binding(data: &[u8]) -> Option> { // Only STUN starts with 0..=3 (RFC 7983), so media skips the parse. if *data.first()? > 3 { return None; } let msg = StunMessage::parse(data).ok()?; - if !msg.is_binding_request() { - return None; - } - msg.split_username().map(|(local, _remote)| local.to_string()) + msg.is_binding_request().then_some(msg) } #[cfg(test)] @@ -265,12 +282,18 @@ mod tests { let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); let (a, mut inbound_a, registration_a) = mux.register(); let (b, mut inbound_b, _registration_b) = mux.register(); + let unknown = IceCreds::new(); + let (request_a, request_b, request_unknown) = + (binding_request(&a), binding_request(&b), binding_request(&unknown)); + fn parse(request: &[u8]) -> StunMessage<'_> { + parse_binding(request).expect("a binding request") + } let address_a = "127.0.0.1:10001".parse().unwrap(); let address_b = "127.0.0.1:10002".parse().unwrap(); let mut registry = mux.registry.lock().unwrap(); - assert!(registry.route(address_a, Some("unknown")).is_none()); + assert!(registry.route(address_a, Some(&parse(&request_unknown))).is_none()); registry - .route(address_a, Some(&a.ufrag)) + .route(address_a, Some(&parse(&request_a))) .unwrap() .sender .try_send((vec![1], address_a)) @@ -283,7 +306,7 @@ mod tests { .try_send((vec![2], address_a)) .unwrap(); registry - .route(address_b, Some(&b.ufrag)) + .route(address_b, Some(&parse(&request_b))) .unwrap() .sender .try_send((vec![3], address_b)) @@ -298,11 +321,12 @@ mod tests { drop(registration_a); let mut registry = mux.registry.lock().unwrap(); assert!(registry.route(address_a, None).is_none()); - assert!(registry.route(address_a, Some(&a.ufrag)).is_none()); + assert!(registry.route(address_a, Some(&parse(&request_a))).is_none()); } /// A peer re-offering from the address its old session still holds reaches its new - /// session: a binding request routes by the session it names, and moves the address there. + /// session: a binding request routes by the session it names, and one signed with that + /// session's password moves the address there. #[tokio::test] async fn a_reoffer_from_the_same_address_reaches_its_new_session() { let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); @@ -322,6 +346,23 @@ mod tests { peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); assert_eq!(recv(&mut old_inbound).await, request); + // A request naming another session without its password, as one forged from this + // address would, reaches that session but leaves the address where it was. + let forged = binding_request(&IceCreds { + ufrag: new.ufrag.clone(), + pass: old.pass.clone(), + }); + peer.send_to(&forged, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + assert_eq!(recv(&mut new_inbound).await, forged); + peer.send_to(&[22, 254, 253], (Ipv4Addr::LOCALHOST, port)) + .await + .unwrap(); + assert_eq!( + recv(&mut old_inbound).await, + [22, 254, 253], + "a forged request moved the address" + ); + let request = binding_request(&new); peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); assert_eq!(recv(&mut new_inbound).await, request); diff --git a/rs/moq-webrtc/src/session.rs b/rs/moq-webrtc/src/session.rs index 5b14361780b..540a5df4135 100644 --- a/rs/moq-webrtc/src/session.rs +++ b/rs/moq-webrtc/src/session.rs @@ -108,9 +108,8 @@ pub struct Session { /// sessions; pumps send frames here, the main loop forwards them into /// str0m's [`Writer`](str0m::media::Writer). writes_rx: Option>, - /// Why the egress source's catalog stopped. `Some` only for [`MediaRole::Egress`] sessions, - /// until it does. - catalog_end: Option>>, + /// Where an egress source's catalog stands. + catalog: Catalog, /// Rebases each ingested track's raw RTP timestamps onto one session /// timeline so audio and video stay in sync. Unused by egress sessions. ingest_clock: IngestClock, @@ -133,7 +132,7 @@ impl Session { role: MediaRole::Ingest(sink), opened_media: HashSet::new(), writes_rx: None, - catalog_end: None, + catalog: Catalog::Settled, ingest_clock: IngestClock::default(), egress_clock: EgressClock::default(), rate: None, @@ -151,7 +150,7 @@ impl Session { role: MediaRole::Egress(Box::new(source)), opened_media: HashSet::new(), writes_rx: Some(writes_rx), - catalog_end: Some(catalog_end), + catalog: Catalog::Following(catalog_end), ingest_clock: IngestClock::default(), egress_clock: EgressClock::default(), rate: None, @@ -217,23 +216,20 @@ impl Session { biased; // A failed catalog, as every subscription fails once the source's route dies, - // means nothing more will flow, so fail rather than idle. A clean end opens no - // more lines, and the pumps serve what they still hold until the writes run dry. - // Polled before the writes, which could otherwise stay ready and starve it; it - // completes once. + // means nothing more will flow, so fail rather than idle. A clean end lets the + // pumps serve what they still hold until the writes run dry. Polled before the + // writes, which could otherwise stay ready and starve it; it completes once. ended = async { - match self.catalog_end.as_mut() { - Some(end) => end.await, - None => std::future::pending().await, + match &mut self.catalog { + Catalog::Following(end) => end.await, + _ => std::future::pending().await, } } => { - self.catalog_end = None; if let Ok(Err(err)) = ended { return Err(err); } - if let MediaRole::Egress(source) = &mut self.role { - source.end(); - } + self.catalog = Catalog::Ended; + self.settle()?; } req = async { @@ -246,12 +242,11 @@ impl Session { let wallclock = self.egress_clock.wallclock(req.time, Instant::now()); crate::egress::dispatch(&mut self.agent.rtc_mut(), req, wallclock); } - // The catalog ended and every pump has written its last frame, which str0m - // already sent: close with an RTCP BYE and a DTLS close_notify, so the peer - // learns at once that the broadcast is over, and end once they are out. + // The catalog ended and every pump has written its last frame: close once + // str0m has sent them, so the peer learns at once that the broadcast is over. None => { self.writes_rx = None; - self.agent.rtc_mut().close().map_err(Error::rtc)?; + self.agent.close(); } }, @@ -277,7 +272,10 @@ impl Session { } => self.send_remb(), event = self.agent.next() => match event { - Ok(event) => self.handle_event(event)?, + Ok(event) => { + self.handle_event(event)?; + self.settle()?; + } Err(crate::ice::Error::Closed) => return Ok(()), Err(crate::ice::Error::Timeout) => return Err(Error::IceTimeout), Err(crate::ice::Error::Rtc(err)) => return Err(Error::Rtc(err)), @@ -286,6 +284,24 @@ impl Session { } } + /// Once the catalog ended and the peer connected, open the negotiated lines and stop the + /// source opening more, so its writes run dry once those lines' pumps finish. str0m opens + /// the lines only once SRTP is up, all in the drain that connected, so a catalog that ends + /// before the peer connects still has its lines served. + fn settle(&mut self) -> Result<()> { + if !matches!(self.catalog, Catalog::Ended) || !self.agent.rtc().is_connected() { + return Ok(()); + } + self.catalog = Catalog::Settled; + while let Some(event) = self.agent.queued() { + self.handle_event(event)?; + } + if let MediaRole::Egress(source) = &mut self.role { + source.end(); + } + Ok(()) + } + fn handle_event(&mut self, event: Event) -> Result<()> { match event { Event::IceConnectionStateChange(state) => { @@ -464,6 +480,16 @@ impl Session { } } +/// Where an egress source's catalog stands. +enum Catalog { + /// Followed until it stops, for why. + Following(tokio::task::JoinHandle>), + /// Ended cleanly, with the source still opening lines until the peer connects. + Ended, + /// Opening no more lines: an ingest, or an egress whose catalog ended. + Settled, +} + /// An ingest's cap: the meter that closes it past the cap plus burst, and the REMB that /// steers the publisher under it. struct IngestRate { From 61225b4a7d4d7f44c8e3bbd798644bef867cdc89 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 18:26:49 -0800 Subject: [PATCH 4/8] fix(webrtc): a signed request only claims an address another session holds A binding request signed by the session it names still takes an address nobody holds, but for one a live session holds it only claims it: the claimant gets the answers to its checks, and takes the address once its ICE connects on that path, or the holder ends. A session owner spoofing a viewer's address can sign its request, but never sees the answers its checks need, so it moves nothing. Co-Authored-By: Claude Opus 5.5 --- rs/moq-webrtc/src/ice/agent.rs | 8 + rs/moq-webrtc/src/ice/wire.rs | 10 +- rs/moq-webrtc/src/lib.rs | 44 +++++- rs/moq-webrtc/src/server/mod.rs | 1 + rs/moq-webrtc/src/server/mux.rs | 257 ++++++++++++++++++++++---------- 5 files changed, 234 insertions(+), 86 deletions(-) diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index 9346cf9c1ae..bd19ba6ebc1 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -279,12 +279,20 @@ impl Agent { tracing::debug!(%src, len = data.len(), "dropped unrecognized datagram"); return; }; + let connecting = matches!(self.phase, Phase::Establishing { .. }); let result = self .rtc_mut() .handle_input(Input::Receive(Instant::now().into_std(), receive)); if let Err(err) = result { self.fail(Error::rtc(err)); } + // str0m connects only on a path its checks validated, so `src` is the peer's. + if connecting + && matches!(self.phase, Phase::Connected) + && let Some(route) = &self.inbound.route + { + route.confirm(src); + } } } diff --git a/rs/moq-webrtc/src/ice/wire.rs b/rs/moq-webrtc/src/ice/wire.rs index b6827718426..3173d05afdd 100644 --- a/rs/moq-webrtc/src/ice/wire.rs +++ b/rs/moq-webrtc/src/ice/wire.rs @@ -51,6 +51,9 @@ impl Socket { pub struct Inbound { /// Ends once every sender is dropped, which ends the agent. pub packets: mpsc::Receiver, + /// Where a shared socket routes them, which the agent confirms the path its ICE + /// connected on with. `None` for a socket the agent owns. + pub(crate) route: Option, } /// Pump every datagram from a socket the caller owns 1:1 into a queue for @@ -77,7 +80,10 @@ pub fn reader(socket: Arc) -> Inbound { } } }); - Inbound { packets: rx } + Inbound { + packets: rx, + route: None, + } } #[cfg(test)] @@ -88,7 +94,7 @@ mod tests { async fn construction_refuses_a_socket_without_advertised_candidates() { let udp = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap()); let (_, packets) = mpsc::channel(INBOX); - let inbound = Inbound { packets }; + let inbound = Inbound { packets, route: None }; let Err(err) = Socket::new(udp, Vec::new(), inbound) else { panic!("an unusable socket escaped construction"); }; diff --git a/rs/moq-webrtc/src/lib.rs b/rs/moq-webrtc/src/lib.rs index b58012f8aa9..d2a4dbbe7aa 100644 --- a/rs/moq-webrtc/src/lib.rs +++ b/rs/moq-webrtc/src/lib.rs @@ -239,9 +239,22 @@ mod tests { /// the channel closes with the server's close. Unlike [`Client::pull`], which aborts its /// output when the session ends, it keeps everything that arrived. async fn raw_view(url: url::Url) -> tokio::sync::mpsc::UnboundedReceiver> { + let udp = std::sync::Arc::new(tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap()); + raw_view_on(url, udp).await.1 + } + + /// [`raw_view`] from `udp`, with the task driving it, which ends with the server's close. + async fn raw_view_on( + url: url::Url, + udp: std::sync::Arc, + ) -> ( + tokio::task::JoinHandle<()>, + tokio::sync::mpsc::UnboundedReceiver>, + ) { use str0m::media::{Direction, MediaKind}; - let socket = crate::session::bind_udp().await.unwrap(); + let local = udp.local_addr().unwrap(); + let socket = crate::ice::Socket::new(udp.clone(), vec![local], crate::ice::reader(udp)).unwrap(); let mut rtc = crate::session::rtc_config() .unwrap() .build(kio::clock::now().into_std()); @@ -266,7 +279,7 @@ mod tests { let mut agent = crate::ice::Agent::new(rtc, socket).unwrap(); let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); - tokio::spawn(async move { + let task = tokio::spawn(async move { loop { match agent.next().await { Ok(str0m::Event::MediaData(data)) => tx.send(data.data.to_vec()).unwrap(), @@ -276,7 +289,7 @@ mod tests { } } }); - rx + (task, rx) } /// The next payload `viewer` receives, or `None` once the server closed. @@ -384,6 +397,31 @@ mod tests { http.abort(); } + /// A viewer re-offering from the address its abandoned session still holds plays on its + /// new session: the new session's ICE confirms the path, so the address moves to it. + #[tokio::test] + async fn a_reoffer_from_the_same_address_plays() { + let origin = moq_tokio::origin::spawn(); + let (_route, mut bridge, _catalog) = publish_opus(&origin, moq_net::Instance::random()); + push(&mut *bridge, 20_000, OPUS_PACKET); + + let (url, http, mut sessions) = serve_whep(&origin).await; + let udp = std::sync::Arc::new(tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap()); + let (old, mut viewer) = raw_view_on(url.clone(), udp.clone()).await; + let _old_session = sessions.recv().await.unwrap(); + assert_eq!(received(&mut viewer).await.as_deref(), Some(OPUS_PACKET)); + + // The peer drops its connection without a word, then offers again from the same address. + old.abort(); + let _ = old.await; + let (_new, mut viewer) = raw_view_on(url, udp).await; + let _new_session = sessions.recv().await.unwrap(); + push(&mut *bridge, 40_000, NEW_PACKET); + let played = received(&mut viewer).await.expect("the new session never played"); + assert!([OPUS_PACKET, NEW_PACKET].contains(&played.as_slice()), "{played:?}"); + http.abort(); + } + /// A WHIP push stays on the instance it resolved the same way, and /// [`Client::publish`] fails once that instance's route dies. #[tokio::test] diff --git a/rs/moq-webrtc/src/server/mod.rs b/rs/moq-webrtc/src/server/mod.rs index aa0525cbac5..6e0bdd1290a 100644 --- a/rs/moq-webrtc/src/server/mod.rs +++ b/rs/moq-webrtc/src/server/mod.rs @@ -24,6 +24,7 @@ use tokio::sync::oneshot; use crate::Result; use mux::Mux; +pub(crate) use mux::Route; /// The result of a WHIP/WHEP [`whip::accept`] / [`whep::accept`]: the SDP answer /// to return to the client, plus an opaque resource id for the `Location` header diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index f6ce9db3a4e..f25bd2ce6b8 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -12,9 +12,15 @@ //! requests whose USERNAME is `:`, so we parse the local //! ufrag out of the STUN message and look the session up. A binding request //! signed with that session's password caches `addr -> session`, so subsequent -//! DTLS/RTP/RTCP (which carry no ufrag) route by address. A binding request always -//! routes by its ufrag, so a peer that re-offers from the same address moves the -//! address to its new session, while a forged one cannot move another peer's. +//! DTLS/RTP/RTCP (which carry no ufrag) route by address. +//! +//! A binding request always routes by its ufrag, but one for an address another +//! live session holds only claims it: anyone holding a session can sign a request, +//! and a spoofed source proves nothing. The claim takes the address once the +//! claimant's own ICE connects on a datagram from it ([`Route::confirm`]), which +//! needs the answers to its checks (STUN responses go to both sessions meanwhile), +//! or once the holder ends. So a peer that re-offers from the same address reaches +//! its new session, and a forged request moves no one's traffic. //! //! Backpressure mirrors a UDP socket buffer: each session has a bounded inbox //! and a full inbox drops the datagram (WebRTC tolerates loss). A closed inbox @@ -43,6 +49,9 @@ struct Registry { /// Source address -> session inbox. Cached after first contact so non-STUN /// packets (DTLS/RTP/RTCP, which carry no ufrag) route without parsing. by_addr: HashMap>, + /// Source address -> the session a signed binding request claimed it for while + /// another session holds it, until [`Route::confirm`] or the holder's end. + claims: HashMap>, } struct Entry { @@ -92,32 +101,101 @@ impl Drop for Registration { registry .by_addr .retain(|_, entry| entry.ufrag != self.ufrag && !entry.sender.is_closed()); + registry + .claims + .retain(|_, entry| entry.ufrag != self.ufrag && !entry.sender.is_closed()); + } +} + +/// A session's handle on its routing, which it confirms an address on. +pub(crate) struct Route { + ufrag: String, + registry: Arc>, +} + +impl Route { + /// Take `src` if this session claimed it: its ICE connected on a datagram from `src`, + /// which str0m accepts only for a path its checks validated. + pub(crate) fn confirm(&self, src: SocketAddr) { + let mut registry = self.registry.lock().unwrap(); + if registry.claims.get(&src).is_some_and(|claim| claim.ufrag == self.ufrag) { + let claim = registry.claims.remove(&src).expect("the claim was found"); + registry.by_addr.insert(src, claim); + } } } impl Registry { - /// The session a datagram from `src` goes to: the one a binding `request` names, or else - /// whichever holds `src`. A request signed with that session's password moves `src` to - /// it, so a peer re-offering from the same address reaches its new session; a forged one - /// still reaches the session it names, which drops it, but leaves `src` where it was. - fn route(&mut self, src: SocketAddr, request: Option<&StunMessage>) -> Option> { - let named = request.and_then(|request| { - let (local, _remote) = request.split_username()?; - Some((request, self.by_ufrag.get(local)?.clone())) - }); - if let Some((request, entry)) = named { - let hmac = self.hmac; - if request.verify(entry.pass.as_bytes(), |key, payloads| hmac.sha1_hmac(key, payloads)) { - self.by_addr.insert(src, entry.clone()); + /// The sessions a datagram from `src` goes to. + /// + /// A binding request goes to the session it names. Signed with that session's password, + /// it takes `src` if no live session holds it, or else claims it. A forged one moves + /// nothing, and the named session drops it. A STUN answer goes to both the holder and + /// the claimant, since either may have sent the check; anything else to the holder. + fn route(&mut self, src: SocketAddr, datagram: &Datagram) -> [Option>; 2] { + match datagram { + Datagram::Request(request) => { + let named = (request.split_username()) + .and_then(|(local, _remote)| self.by_ufrag.get(local)) + .cloned(); + let Some(entry) = named else { + return [self.holder(src), None]; + }; + let hmac = self.hmac; + if request.verify(entry.pass.as_bytes(), |key, payloads| hmac.sha1_hmac(key, payloads)) { + match self.holder(src) { + None => { + self.by_addr.insert(src, entry.clone()); + } + Some(holder) if Arc::ptr_eq(&holder, &entry) => { + self.claims.remove(&src); + } + Some(_) => { + self.claims.insert(src, entry.clone()); + } + } + } + [Some(entry), None] } - return Some(entry); + Datagram::Answer => [self.holder(src), self.claims.get(&src).cloned()], + Datagram::Other => [self.holder(src), None], } - let entry = self.by_addr.get(&src).cloned()?; - if entry.sender.is_closed() { - self.by_addr.remove(&src); - return None; + } + + /// The live session holding `src`: its holder, or once that ends, its claimant. + fn holder(&mut self, src: SocketAddr) -> Option> { + if let Some(entry) = self.by_addr.get(&src) + && !entry.sender.is_closed() + { + return Some(entry.clone()); + } + self.by_addr.remove(&src); + let claim = self.claims.remove(&src).filter(|claim| !claim.sender.is_closed())?; + self.by_addr.insert(src, claim.clone()); + Some(claim) + } +} + +/// What a datagram is, as far as routing it goes. +enum Datagram<'a> { + /// A STUN binding request, which names its session. + Request(Box>), + /// Any other STUN message, such as the answer to a check. + Answer, + /// DTLS, RTP, or RTCP, which carries no session. + Other, +} + +impl<'a> Datagram<'a> { + fn parse(data: &'a [u8]) -> Self { + // Only STUN starts with 0..=3 (RFC 7983), so media skips the parse. + if data.first().is_none_or(|first| *first > 3) { + return Self::Other; + } + match StunMessage::parse(data) { + Ok(message) if message.is_binding_request() => Self::Request(Box::new(message)), + _ => Self::Answer, } - Some(entry) } } @@ -138,6 +216,7 @@ impl Mux { hmac: crypto.sha1_hmac_provider, by_ufrag: HashMap::new(), by_addr: HashMap::new(), + claims: HashMap::new(), })); let demux = tokio::spawn(demux(socket.clone(), registry.clone())); @@ -177,7 +256,15 @@ impl Mux { ufrag: creds.ufrag.clone(), registry: self.registry.clone(), }; - (creds, Inbound { packets: rx }, registration) + let route = Route { + ufrag: creds.ufrag.clone(), + registry: self.registry.clone(), + }; + let inbound = Inbound { + packets: rx, + route: Some(route), + }; + (creds, inbound, registration) } /// The shared socket, handed to each session for sending. @@ -211,35 +298,16 @@ async fn demux(socket: Arc, registry: Arc>) { let src = crate::ice::canonical(src); let data = &buf[..len]; - let request = parse_binding(data); - let entry = registry.lock().unwrap().route(src, request.as_ref()); - let Some(entry) = entry else { - continue; - }; - - // Bounded like a socket buffer: drop on full (WebRTC tolerates loss), - // evict on closed (the session ended). - match entry.sender.try_send((data.to_vec(), src)) { - Ok(()) => {} - Err(mpsc::error::TrySendError::Full(_)) => {} - Err(mpsc::error::TrySendError::Closed(_)) => { - registry.lock().unwrap().by_addr.remove(&src); - } + let datagram = Datagram::parse(data); + let targets = registry.lock().unwrap().route(src, &datagram); + for entry in targets.into_iter().flatten() { + // Bounded like a socket buffer: drop on full, as WebRTC tolerates loss. A closed + // inbox is evicted on its address's next route. + let _ = entry.sender.try_send((data.to_vec(), src)); } } } -/// `data` as a STUN binding request, if it is one. Its USERNAME is -/// `:`; we route on the local half. -fn parse_binding(data: &[u8]) -> Option> { - // Only STUN starts with 0..=3 (RFC 7983), so media skips the parse. - if *data.first()? > 3 { - return None; - } - let msg = StunMessage::parse(data).ok()?; - msg.is_binding_request().then_some(msg) -} - #[cfg(test)] mod tests { use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; @@ -285,28 +353,30 @@ mod tests { let unknown = IceCreds::new(); let (request_a, request_b, request_unknown) = (binding_request(&a), binding_request(&b), binding_request(&unknown)); - fn parse(request: &[u8]) -> StunMessage<'_> { - parse_binding(request).expect("a binding request") + fn parse(request: &[u8]) -> Datagram<'_> { + let datagram = Datagram::parse(request); + assert!(matches!(datagram, Datagram::Request(_)), "not a binding request"); + datagram } let address_a = "127.0.0.1:10001".parse().unwrap(); let address_b = "127.0.0.1:10002".parse().unwrap(); let mut registry = mux.registry.lock().unwrap(); - assert!(registry.route(address_a, Some(&parse(&request_unknown))).is_none()); - registry - .route(address_a, Some(&parse(&request_a))) + assert!(registry.route(address_a, &parse(&request_unknown))[0].is_none()); + registry.route(address_a, &parse(&request_a))[0] + .clone() .unwrap() .sender .try_send((vec![1], address_a)) .unwrap(); // A cached address routes without a ufrag. - registry - .route(address_a, None) + registry.route(address_a, &Datagram::Other)[0] + .clone() .unwrap() .sender .try_send((vec![2], address_a)) .unwrap(); - registry - .route(address_b, Some(&parse(&request_b))) + registry.route(address_b, &parse(&request_b))[0] + .clone() .unwrap() .sender .try_send((vec![3], address_b)) @@ -320,20 +390,21 @@ mod tests { // keep routing traffic after the peer ends. drop(registration_a); let mut registry = mux.registry.lock().unwrap(); - assert!(registry.route(address_a, None).is_none()); - assert!(registry.route(address_a, Some(&parse(&request_a))).is_none()); + assert!(registry.route(address_a, &Datagram::Other)[0].is_none()); + assert!(registry.route(address_a, &parse(&request_a))[0].is_none()); } - /// A peer re-offering from the address its old session still holds reaches its new - /// session: a binding request routes by the session it names, and one signed with that - /// session's password moves the address there. + /// A signed binding request for an address another session holds only claims it: the + /// claimant gets the answers to its checks, and takes the address once its ICE confirms the + /// path, or the holder ends. A forged request claims nothing. #[tokio::test] - async fn a_reoffer_from_the_same_address_reaches_its_new_session() { + async fn a_claimed_address_moves_once_confirmed() { let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); let port = mux.local_addr().port(); let (old, mut old_inbound, _old_registration) = mux.register(); let (new, mut new_inbound, _new_registration) = mux.register(); let peer = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); + let send = async |data: &[u8]| peer.send_to(data, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); let recv = async |inbound: &mut Inbound| { kio::clock::timeout(Duration::from_secs(5), inbound.packets.recv()) .await @@ -341,43 +412,67 @@ mod tests { .expect("session inbox stayed open") .0 }; + const DTLS: &[u8] = &[22, 254, 253]; + const ANSWER: &[u8] = &[1, 1, 0, 0]; let request = binding_request(&old); - peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + send(&request).await; assert_eq!(recv(&mut old_inbound).await, request); - // A request naming another session without its password, as one forged from this - // address would, reaches that session but leaves the address where it was. + // Forged: it reaches the session it names, which drops it, and claims nothing. let forged = binding_request(&IceCreds { ufrag: new.ufrag.clone(), pass: old.pass.clone(), }); - peer.send_to(&forged, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + send(&forged).await; assert_eq!(recv(&mut new_inbound).await, forged); - peer.send_to(&[22, 254, 253], (Ipv4Addr::LOCALHOST, port)) - .await - .unwrap(); - assert_eq!( - recv(&mut old_inbound).await, - [22, 254, 253], - "a forged request moved the address" - ); + send(ANSWER).await; + assert_eq!(recv(&mut old_inbound).await, ANSWER); + // Signed: a claim, so the new session gets the answers to its checks, while the old one + // keeps the address. let request = binding_request(&new); - peer.send_to(&request, (Ipv4Addr::LOCALHOST, port)).await.unwrap(); + send(&request).await; assert_eq!(recv(&mut new_inbound).await, request); + send(ANSWER).await; + assert_eq!(recv(&mut old_inbound).await, ANSWER); + assert_eq!(recv(&mut new_inbound).await, ANSWER); + send(DTLS).await; + assert_eq!(recv(&mut old_inbound).await, DTLS, "a claim moved the address"); - // What follows, such as DTLS, carries no ufrag and follows the address to its new session. - peer.send_to(&[22, 254, 253], (Ipv4Addr::LOCALHOST, port)) - .await - .unwrap(); - assert_eq!(recv(&mut new_inbound).await, [22, 254, 253]); + // Confirmed: the address moves. + let peer_addr = peer.local_addr().unwrap(); + new_inbound.route.as_ref().unwrap().confirm(peer_addr); + send(DTLS).await; + assert_eq!(recv(&mut new_inbound).await, DTLS); assert!( old_inbound.packets.try_recv().is_err(), "the old session kept the address" ); } + /// A claimant takes the address once its holder ends, confirmed or not. + #[tokio::test] + async fn a_claim_outlives_its_holder() { + let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); + let (old, old_inbound, old_registration) = mux.register(); + let (new, mut new_inbound, _new_registration) = mux.register(); + let (request_old, request_new) = (binding_request(&old), binding_request(&new)); + let address = "127.0.0.1:10001".parse().unwrap(); + let mut registry = mux.registry.lock().unwrap(); + registry.route(address, &Datagram::parse(&request_old)); + registry.route(address, &Datagram::parse(&request_new)); + drop(registry); + drop((old_inbound, old_registration)); + + let mut registry = mux.registry.lock().unwrap(); + let [holder, claimant] = registry.route(address, &Datagram::Other); + assert!(claimant.is_none()); + holder.unwrap().sender.try_send((vec![1], address)).unwrap(); + drop(registry); + assert_eq!(new_inbound.packets.try_recv().unwrap().0, [1]); + } + /// An IPv4 peer reaching a dual-stack (`[::]`) mux must route to its session /// with its source unmapped to the `V4` it really is. Without the /// canonicalization the source stays `::ffff:127.0.0.1`, which pairs the peer From 039eb1dc3c6f6b13200e0b53c0416ede0c27569e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:32:42 -0800 Subject: [PATCH 5/8] fix(webrtc): a claimed address moves only on an answer from where its check went str0m's ICE matches a binding response to its check by transaction ID alone, so a session could answer its own check from a spoofed victim address, connect, and confirm the victim's address. The agent now records where each check went while establishing, and confirms only on a success response from that destination or a request (whose source defines its pair). Records the maintainer's decision on quest/m3/webrtc-clean-end.md: a WHIP server treats a publisher's close_notify as a clean end. Co-Authored-By: Claude Opus 5.5 --- quest/m3/webrtc-clean-end.md | 24 +++--- rs/moq-webrtc/src/ice/agent.rs | 70 ++++++++++++++-- rs/moq-webrtc/src/server/mux.rs | 143 ++++++++++++++++++++++++++++++-- 3 files changed, 211 insertions(+), 26 deletions(-) diff --git a/quest/m3/webrtc-clean-end.md b/quest/m3/webrtc-clean-end.md index 9a379d90e4d..30bef4bcd40 100644 --- a/quest/m3/webrtc-clean-end.md +++ b/quest/m3/webrtc-clean-end.md @@ -23,19 +23,17 @@ fails any read of a frame it received but its readers had not reached yet - WHIP client: `client::whip::dial` keeps the answer's `Location` (resolved against the dialed URL, keeping its query token), and after `Session::run` returns `Ok` sends the `DELETE` (RFC 9725) before returning. +- WHIP server: a publisher's close_notify (str0m's `Event::Closed`) is a + clean end, like a `DELETE`: the server finishes the broadcast and lingers + it. A browser's `pc.close()` sends close_notify without a `DELETE`. Only a + peer that vanishes (ICE consent failure, establishment timeout) aborts. + Decided on #9049: ✅ both close_notify and `DELETE` end cleanly (rejected: + `DELETE` only, which RFC 9725 names but which aborts the broadcast of every + publisher that skips it). - WHEP client: a WHEP server has no request to end a session with, so the - server's close_notify (str0m's `Event::Closed`) is its only clean end. - Recommended: an ingest session that sees `Event::Closed` returns as the - peer's clean end, and `Client::pull` finishes the sink as `AcceptedSession` - does after a `DELETE`. + server's close_notify is its only clean end. An ingest session that sees + `Event::Closed` returns as the peer's clean end, and `Client::pull` + finishes the sink as `AcceptedSession` does after a `DELETE`. The WHIP + server's ingest takes the same path. Public API: none. Wire: the WHIP client sends a `DELETE`. - -## Waiting - -- Maintainer: whether a WHIP server also reads a publisher's close_notify as - a clean end, finishing and lingering the broadcast, or only a `DELETE`. A - browser's `pc.close()` sends close_notify without a `DELETE`. Recommended: - both, since close_notify is a deliberate end and only a vanished peer - should abort; rejected: `DELETE` only, which RFC 9725 names but which - aborts the broadcast of every publisher that skips it. diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index bd19ba6ebc1..80737e2eeeb 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use std::time::Duration; use kio::clock::Instant; +use str0m::ice::{StunMessage, TransId}; use str0m::net::{Protocol, Receive}; use str0m::{Event, IceConnectionState, Input, Output, Rtc}; use tokio::net::UdpSocket; @@ -19,6 +20,12 @@ use super::{Inbound, Socket}; /// one. Set above str0m's own exhaustion so a slow but real peer is not clipped. const ESTABLISH_TIMEOUT: Duration = Duration::from_secs(30); +/// How many of its latest binding requests an establishing agent remembers the +/// destination of. str0m paces a new check every 50 ms, so this spans the last few +/// seconds of checks and their retransmits; a response to an older one only fails to +/// confirm, and the address then waits for its holder to end. +const CHECKS: usize = 64; + /// Why an [`Agent`] stopped. #[derive(Clone, Debug, thiserror::Error)] pub enum Error { @@ -47,6 +54,8 @@ enum Phase { /// ICE has yet to connect; the [`ESTABLISH_TIMEOUT`] runs from `started`. Establishing { started: Instant, + /// Where each recent binding request went, newest last, at most [`CHECKS`]. + checks: VecDeque<(TransId, SocketAddr)>, }, Connected, /// The connection ended, for this reason: every later [`Agent::next`] reports it. @@ -95,7 +104,10 @@ impl Agent { events: VecDeque::new(), timeout: now, outbox: VecDeque::new(), - phase: Phase::Establishing { started: now }, + phase: Phase::Establishing { + started: now, + checks: VecDeque::new(), + }, closing: false, }; // str0m's ICE has no clock, and asks for no timeout, until its first one. Without @@ -205,7 +217,7 @@ impl Agent { /// When [`ESTABLISH_TIMEOUT`] expires, or `None` once ICE connected or the connection ended. fn establish(&self) -> Option { match &self.phase { - Phase::Establishing { started } => Some(started.saturating_add(ESTABLISH_TIMEOUT)), + Phase::Establishing { started, .. } => Some(started.saturating_add(ESTABLISH_TIMEOUT)), _ => None, } } @@ -230,7 +242,19 @@ impl Agent { self.timeout = Instant::from_std(timeout); return; } - Output::Transmit(transmit) => self.send(&transmit.contents, transmit.destination), + Output::Transmit(transmit) => { + // Only a shared socket's route needs to know where checks went. + if self.inbound.route.is_some() + && let Phase::Establishing { checks, .. } = &mut self.phase + && let Some(check) = stun(&transmit.contents).filter(|m| m.is_binding_request()) + { + if checks.len() == CHECKS { + checks.pop_front(); + } + checks.push_back((check.trans_id(), transmit.destination)); + } + self.send(&transmit.contents, transmit.destination) + } Output::Event(event) => { if let str0m::Event::IceConnectionStateChange(state) = &event { tracing::debug!(?state, "ice state"); @@ -279,15 +303,17 @@ impl Agent { tracing::debug!(%src, len = data.len(), "dropped unrecognized datagram"); return; }; - let connecting = matches!(self.phase, Phase::Establishing { .. }); + let symmetric = match &self.phase { + Phase::Establishing { checks, .. } if self.inbound.route.is_some() => is_symmetric(checks, data, src), + _ => false, + }; let result = self .rtc_mut() .handle_input(Input::Receive(Instant::now().into_std(), receive)); if let Err(err) = result { self.fail(Error::rtc(err)); } - // str0m connects only on a path its checks validated, so `src` is the peer's. - if connecting + if symmetric && matches!(self.phase, Phase::Connected) && let Some(route) = &self.inbound.route { @@ -296,6 +322,33 @@ impl Agent { } } +/// Whether a datagram that connects ICE proves the peer is at `src`, the check RFC 8445 +/// §7.2.5.2.1 asks for and str0m skips: it pairs a response with its check by transaction +/// ID alone, so a peer could answer its own check from a spoofed source. +/// +/// A success response proves it when its check went to `src`, since only `src` saw that +/// transaction ID. A request does too: a controlling agent connects only on a response, and +/// a controlled one on a request only for that request's own pair, which is valid once a +/// check sent to `src` was answered. +fn is_symmetric(checks: &VecDeque<(TransId, SocketAddr)>, data: &[u8], src: SocketAddr) -> bool { + let Some(message) = stun(data) else { + return false; + }; + if message.is_binding_request() { + return true; + } + let id = message.trans_id(); + message.is_successful_binding_response() && checks.iter().any(|check| *check == (id, src)) +} + +/// Parse a datagram as STUN, which alone starts with 0..=3 (RFC 7983). +fn stun(data: &[u8]) -> Option> { + if data.first().is_none_or(|first| *first > 3) { + return None; + } + StunMessage::parse(data).ok() +} + /// Mutable access to an [`Agent`]'s connection, which drains it when dropped. pub struct RtcMut<'a>(&'a mut Agent); @@ -413,7 +466,10 @@ mod tests { // Hold the deadline past the handshake's retransmissions, so DTLS // deterministically gives up first. let held = Instant::now().saturating_add(Duration::from_secs(3600)); - agent.phase = Phase::Establishing { started: held }; + agent.phase = Phase::Establishing { + started: held, + checks: VecDeque::new(), + }; let err = loop { match agent.next().await { diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index f25bd2ce6b8..b4c586c7041 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -17,10 +17,12 @@ //! A binding request always routes by its ufrag, but one for an address another //! live session holds only claims it: anyone holding a session can sign a request, //! and a spoofed source proves nothing. The claim takes the address once the -//! claimant's own ICE connects on a datagram from it ([`Route::confirm`]), which -//! needs the answers to its checks (STUN responses go to both sessions meanwhile), -//! or once the holder ends. So a peer that re-offers from the same address reaches -//! its new session, and a forged request moves no one's traffic. +//! claimant's own ICE connects on a datagram from it that proves the peer is there +//! ([`Route::confirm`]): the answer to a check sent to that address, which only it +//! saw (STUN responses go to both sessions meanwhile), or once the holder ends. So +//! a peer that re-offers from the same address reaches its new session, and neither +//! a forged request nor a session answering its own check from a spoofed source +//! moves anyone's traffic. //! //! Backpressure mirrors a UDP socket buffer: each session has a bounded inbox //! and a full inbox drops the datagram (WebRTC tolerates loss). A closed inbox @@ -114,8 +116,8 @@ pub(crate) struct Route { } impl Route { - /// Take `src` if this session claimed it: its ICE connected on a datagram from `src`, - /// which str0m accepts only for a path its checks validated. + /// Take `src` if this session claimed it: its ICE connected on a datagram that proves + /// the peer is at `src` (see `ice::agent::is_symmetric`). pub(crate) fn confirm(&self, src: SocketAddr) { let mut registry = self.registry.lock().unwrap(); if registry.claims.get(&src).is_some_and(|claim| claim.ufrag == self.ufrag) { @@ -451,6 +453,135 @@ mod tests { ); } + /// A session that answers its own check from a victim's address, while the check went to + /// the attacker's real one, connects but never takes the victim's address. The same answer + /// from the address the check went to, as a peer re-offering from it sends, takes it. + #[tokio::test] + async fn only_an_answer_from_where_the_check_went_moves_an_address() { + assert!(!take_over(true).await, "a spoofed answer moved the victim's address"); + assert!(take_over(false).await, "a genuine re-offer stayed with the old session"); + } + + /// Connect a str0m offerer to a new session on a mux where another session holds an + /// address, and report whether the new session took it. The offerer's answers to the + /// session's checks wait until it nominates, so the session's ICE connects on one of + /// them. With `spoof`, the offerer lives elsewhere and sends those answers, and one + /// signed request, from the held address; without, it lives at the held address. + async fn take_over(spoof: bool) -> bool { + use crate::ice::{Agent, Socket}; + use kio::clock::Instant; + use str0m::net::{Protocol, Receive}; + use str0m::{Candidate, Event, Input, Output}; + + let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); + let local = mux.local_addr(); + let real = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); + let real_addr = real.local_addr().unwrap(); + // Kept open so checks sent to the victim land somewhere. + let victim_socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); + let held = if spoof { + victim_socket.local_addr().unwrap() + } else { + real_addr + }; + + let (victim, _victim_inbound, _victim_registration) = mux.register(); + let deliver = |data: &[u8], src: SocketAddr| { + let targets = mux.registry.lock().unwrap().route(src, &Datagram::parse(data)); + for entry in targets.into_iter().flatten() { + let _ = entry.sender.try_send((data.to_vec(), src)); + } + }; + deliver(&binding_request(&victim), held); + + let now = Instant::now().into_std(); + let (creds, inbound, _registration) = mux.register(); + let ufrag = creds.ufrag.clone(); + let mut answerer = crate::session::rtc_config() + .unwrap() + .set_local_ice_credentials(creds) + .build(now); + answerer.add_local_candidate(Candidate::host(local, "udp").unwrap()); + let mut offerer = crate::session::rtc_config().unwrap().build(now); + offerer.add_local_candidate(Candidate::host(real_addr, "udp").unwrap()); + let mut api = offerer.sdp_api(); + api.add_media( + str0m::media::MediaKind::Audio, + str0m::media::Direction::RecvOnly, + None, + None, + None, + ); + let (offer, pending) = api.apply().unwrap(); + let answer = answerer.sdp_api().accept_offer(offer).unwrap(); + offerer.sdp_api().accept_answer(pending, answer).unwrap(); + let mut agent = Agent::new(answerer, Socket::new(mux.socket(), vec![local], inbound).unwrap()).unwrap(); + + let mut claimed = !spoof; + let mut nominated = false; + let mut answers: Vec> = Vec::new(); + let mut buf = vec![0u8; 2048]; + let connect = async { + loop { + let timeout = loop { + match offerer.poll_output().unwrap() { + Output::Timeout(timeout) => break timeout, + Output::Event(_) => {} + Output::Transmit(transmit) => { + let data = transmit.contents.to_vec(); + let Ok(message) = StunMessage::parse(&data) else { + continue; + }; + if message.is_binding_request() { + if !claimed { + deliver(&data, held); + claimed = true; + } + nominated |= message.use_candidate(); + deliver(&data, real_addr); + if nominated { + for answer in answers.drain(..) { + deliver(&answer, held); + } + } + } else if nominated { + deliver(&data, held); + } else { + answers.push(data); + } + } + } + }; + tokio::select! { + received = real.recv_from(&mut buf) => { + let (len, src) = received.unwrap(); + let now = Instant::now().into_std(); + if let Ok(receive) = Receive::new(Protocol::Udp, src, real_addr, &buf[..len]) { + offerer.handle_input(Input::Receive(now, receive)).unwrap(); + } + } + _ = kio::clock::sleep_until(Instant::from_std(timeout)) => { + offerer.handle_input(Input::Timeout(Instant::now().into_std())).unwrap(); + } + event = agent.next() => { + if let Event::IceConnectionStateChange(state) = event.unwrap() + && state.is_connected() + { + return; + } + } + } + } + }; + kio::clock::timeout(Duration::from_secs(10), connect) + .await + .expect("the session's ICE connects"); + assert!(nominated, "ICE connected before the offerer nominated"); + + let registry = mux.registry.lock().unwrap(); + registry.by_addr.get(&held).is_some_and(|entry| entry.ufrag == ufrag) + } + /// A claimant takes the address once its holder ends, confirmed or not. #[tokio::test] async fn a_claim_outlives_its_holder() { From 30b5e6303bc0f5743e007df4b41eefab2949d2e7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:43:13 -0800 Subject: [PATCH 6/8] fix(webrtc): confirm only an address that answered the session's own check A connecting request proved nothing: a spoofed request could switch str0m's ICE to controlling, which then nominates the valid pair and reports the connection on a later spoofed request from the victim. The agent now confirms `src` only when `src` itself answered a check sent to it, whatever datagram connected ICE. Co-Authored-By: Claude Opus 5.5 --- rs/moq-webrtc/src/ice/agent.rs | 43 ++++++++------- rs/moq-webrtc/src/server/mux.rs | 93 +++++++++++++++++++++++++-------- 2 files changed, 97 insertions(+), 39 deletions(-) diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index 80737e2eeeb..16ff0e89fd5 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -21,9 +21,10 @@ use super::{Inbound, Socket}; const ESTABLISH_TIMEOUT: Duration = Duration::from_secs(30); /// How many of its latest binding requests an establishing agent remembers the -/// destination of. str0m paces a new check every 50 ms, so this spans the last few -/// seconds of checks and their retransmits; a response to an older one only fails to -/// confirm, and the address then waits for its holder to end. +/// destination of, and how many addresses that answered one. str0m paces a new check +/// every 50 ms, so this spans the last few seconds of checks and their retransmits; an +/// answer to an older one only fails to confirm, and the address then waits for its +/// holder to end. const CHECKS: usize = 64; /// Why an [`Agent`] stopped. @@ -56,6 +57,8 @@ enum Phase { started: Instant, /// Where each recent binding request went, newest last, at most [`CHECKS`]. checks: VecDeque<(TransId, SocketAddr)>, + /// The addresses that answered one of `checks` themselves, at most [`CHECKS`]. + answered: Vec, }, Connected, /// The connection ended, for this reason: every later [`Agent::next`] reports it. @@ -107,6 +110,7 @@ impl Agent { phase: Phase::Establishing { started: now, checks: VecDeque::new(), + answered: Vec::new(), }, closing: false, }; @@ -303,17 +307,22 @@ impl Agent { tracing::debug!(%src, len = data.len(), "dropped unrecognized datagram"); return; }; - let symmetric = match &self.phase { - Phase::Establishing { checks, .. } if self.inbound.route.is_some() => is_symmetric(checks, data, src), - _ => false, - }; + let mut proven = false; + if self.inbound.route.is_some() + && let Phase::Establishing { checks, answered, .. } = &mut self.phase + { + if answers_check(checks, data, src) && !answered.contains(&src) && answered.len() < CHECKS { + answered.push(src); + } + proven = answered.contains(&src); + } let result = self .rtc_mut() .handle_input(Input::Receive(Instant::now().into_std(), receive)); if let Err(err) = result { self.fail(Error::rtc(err)); } - if symmetric + if proven && matches!(self.phase, Phase::Connected) && let Some(route) = &self.inbound.route { @@ -322,21 +331,18 @@ impl Agent { } } -/// Whether a datagram that connects ICE proves the peer is at `src`, the check RFC 8445 -/// §7.2.5.2.1 asks for and str0m skips: it pairs a response with its check by transaction +/// Whether a datagram from `src` answers a check this agent sent to `src`, which proves +/// the peer is there: only `src` saw that check's transaction ID. That is the check RFC 8445 +/// §7.2.5.2.1 asks for and str0m skips, pairing a response with its check by transaction /// ID alone, so a peer could answer its own check from a spoofed source. /// -/// A success response proves it when its check went to `src`, since only `src` saw that -/// transaction ID. A request does too: a controlling agent connects only on a response, and -/// a controlled one on a request only for that request's own pair, which is valid once a -/// check sent to `src` was answered. -fn is_symmetric(checks: &VecDeque<(TransId, SocketAddr)>, data: &[u8], src: SocketAddr) -> bool { +/// Only an answered address is confirmed. The datagram that connects ICE proves nothing by +/// itself: once any pair is valid, str0m may report the connection while handling another +/// datagram, such as a spoofed request after one switched its role to controlling. +fn answers_check(checks: &VecDeque<(TransId, SocketAddr)>, data: &[u8], src: SocketAddr) -> bool { let Some(message) = stun(data) else { return false; }; - if message.is_binding_request() { - return true; - } let id = message.trans_id(); message.is_successful_binding_response() && checks.iter().any(|check| *check == (id, src)) } @@ -469,6 +475,7 @@ mod tests { agent.phase = Phase::Establishing { started: held, checks: VecDeque::new(), + answered: Vec::new(), }; let err = loop { diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index b4c586c7041..9a72424fe46 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -117,7 +117,7 @@ pub(crate) struct Route { impl Route { /// Take `src` if this session claimed it: its ICE connected on a datagram that proves - /// the peer is at `src` (see `ice::agent::is_symmetric`). + /// the peer is at `src`, as the answer to a check sent there (see `ice::agent::answers_check`). pub(crate) fn confirm(&self, src: SocketAddr) { let mut registry = self.registry.lock().unwrap(); if registry.claims.get(&src).is_some_and(|claim| claim.ufrag == self.ufrag) { @@ -453,21 +453,42 @@ mod tests { ); } - /// A session that answers its own check from a victim's address, while the check went to - /// the attacker's real one, connects but never takes the victim's address. The same answer - /// from the address the check went to, as a peer re-offering from it sends, takes it. + /// A session whose peer can send from a victim's address, but receives only at its own, + /// connects yet never takes the victim's address. Only a peer at that address, as one + /// re-offering from it is, takes it. #[tokio::test] - async fn only_an_answer_from_where_the_check_went_moves_an_address() { - assert!(!take_over(true).await, "a spoofed answer moved the victim's address"); - assert!(take_over(false).await, "a genuine re-offer stayed with the old session"); + async fn only_an_address_that_answered_a_check_moves() { + assert!( + take_over(Attack::None).await, + "a genuine re-offer stayed with the old session" + ); + assert!( + !take_over(Attack::SpoofedAnswer).await, + "a spoofed answer moved the victim's address" + ); + assert!( + !take_over(Attack::RoleSwitch).await, + "a spoofed request moved the victim's address" + ); + } + + /// What the peer in [`take_over`] does. + #[derive(PartialEq)] + enum Attack { + /// Lives at the held address, as a peer re-offering from it does. + None, + /// Lives elsewhere, and answers the session's checks from the held address once it + /// nominated, so ICE connects on a spoofed answer. + SpoofedAnswer, + /// Lives elsewhere and never nominates. Once the session's check is answered, two + /// spoofed requests from the held address switch it to controlling, so it nominates + /// the valid pair on the first and reports the connection on the second. + RoleSwitch, } /// Connect a str0m offerer to a new session on a mux where another session holds an - /// address, and report whether the new session took it. The offerer's answers to the - /// session's checks wait until it nominates, so the session's ICE connects on one of - /// them. With `spoof`, the offerer lives elsewhere and sends those answers, and one - /// signed request, from the held address; without, it lives at the held address. - async fn take_over(spoof: bool) -> bool { + /// address, as `attack` says, and report whether the new session took that address. + async fn take_over(attack: Attack) -> bool { use crate::ice::{Agent, Socket}; use kio::clock::Instant; use str0m::net::{Protocol, Receive}; @@ -479,10 +500,9 @@ mod tests { let real_addr = real.local_addr().unwrap(); // Kept open so checks sent to the victim land somewhere. let victim_socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap(); - let held = if spoof { - victim_socket.local_addr().unwrap() - } else { - real_addr + let held = match attack { + Attack::None => real_addr, + _ => victim_socket.local_addr().unwrap(), }; let (victim, _victim_inbound, _victim_registration) = mux.register(); @@ -496,7 +516,7 @@ mod tests { let now = Instant::now().into_std(); let (creds, inbound, _registration) = mux.register(); - let ufrag = creds.ufrag.clone(); + let session = creds.clone(); let mut answerer = crate::session::rtc_config() .unwrap() .set_local_ice_credentials(creds) @@ -517,8 +537,22 @@ mod tests { offerer.sdp_api().accept_answer(pending, answer).unwrap(); let mut agent = Agent::new(answerer, Socket::new(mux.socket(), vec![local], inbound).unwrap()).unwrap(); - let mut claimed = !spoof; + // A request the session accepts from `held`, which claims it. With `switch`, it also + // tells the session to control, as a peer with a larger tie-breaker does. + let spoofed_request = |username: &str, switch: bool| { + let request = StunMessage::binding_request(username, TransId::new(), !switch, u64::MAX, 1, false); + let mut buf = vec![0u8; 1024]; + let len = request + .to_bytes(Some(session.pass.as_bytes()), &mut buf, sha1_hmac) + .unwrap(); + buf.truncate(len); + buf + }; + + let mut claimed = attack == Attack::None; let mut nominated = false; + let mut switched = false; + let mut username = String::new(); let mut answers: Vec> = Vec::new(); let mut buf = vec![0u8; 2048]; let connect = async { @@ -533,10 +567,14 @@ mod tests { continue; }; if message.is_binding_request() { + username = message.username().unwrap().to_string(); if !claimed { - deliver(&data, held); + deliver(&spoofed_request(&username, false), held); claimed = true; } + if message.use_candidate() && attack == Attack::RoleSwitch { + continue; + } nominated |= message.use_candidate(); deliver(&data, real_addr); if nominated { @@ -544,6 +582,13 @@ mod tests { deliver(&answer, held); } } + } else if attack == Attack::RoleSwitch { + deliver(&data, real_addr); + if !switched { + deliver(&spoofed_request(&username, true), held); + deliver(&spoofed_request(&username, true), held); + switched = true; + } } else if nominated { deliver(&data, held); } else { @@ -576,10 +621,16 @@ mod tests { kio::clock::timeout(Duration::from_secs(10), connect) .await .expect("the session's ICE connects"); - assert!(nominated, "ICE connected before the offerer nominated"); + match attack { + Attack::RoleSwitch => assert!(!nominated && switched, "the offerer nominated"), + _ => assert!(nominated, "ICE connected before the offerer nominated"), + } let registry = mux.registry.lock().unwrap(); - registry.by_addr.get(&held).is_some_and(|entry| entry.ufrag == ufrag) + registry + .by_addr + .get(&held) + .is_some_and(|entry| entry.ufrag == session.ufrag) } /// A claimant takes the address once its holder ends, confirmed or not. From 39a5beaf561c461f589020f7e11e8c5cac92df48 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:53:23 -0800 Subject: [PATCH 7/8] refactor(webrtc): confirm every answered claim when ICE connects, however it connected Co-Authored-By: Claude Opus 5.5 --- rs/moq-webrtc/src/ice/agent.rs | 29 +++++++++++++++-------------- rs/moq-webrtc/src/server/mux.rs | 10 +++++----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index 16ff0e89fd5..a8bc8803765 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -266,6 +266,12 @@ impl Agent { return self.fail(Error::Closed); } if state.is_connected() { + // Take each claimed address the peer proved it is at. + if let Phase::Establishing { answered, .. } = &self.phase + && let Some(route) = &self.inbound.route + { + answered.iter().for_each(|addr| route.confirm(*addr)); + } self.phase = Phase::Connected; } } @@ -307,14 +313,14 @@ impl Agent { tracing::debug!(%src, len = data.len(), "dropped unrecognized datagram"); return; }; - let mut proven = false; + // Before str0m sees it, since the answer may be what connects ICE. if self.inbound.route.is_some() && let Phase::Establishing { checks, answered, .. } = &mut self.phase + && answered.len() < CHECKS + && !answered.contains(&src) + && answers_check(checks, data, src) { - if answers_check(checks, data, src) && !answered.contains(&src) && answered.len() < CHECKS { - answered.push(src); - } - proven = answered.contains(&src); + answered.push(src); } let result = self .rtc_mut() @@ -322,12 +328,6 @@ impl Agent { if let Err(err) = result { self.fail(Error::rtc(err)); } - if proven - && matches!(self.phase, Phase::Connected) - && let Some(route) = &self.inbound.route - { - route.confirm(src); - } } } @@ -336,9 +336,10 @@ impl Agent { /// §7.2.5.2.1 asks for and str0m skips, pairing a response with its check by transaction /// ID alone, so a peer could answer its own check from a spoofed source. /// -/// Only an answered address is confirmed. The datagram that connects ICE proves nothing by -/// itself: once any pair is valid, str0m may report the connection while handling another -/// datagram, such as a spoofed request after one switched its role to controlling. +/// Once ICE connects, the agent confirms every claimed address that answered, never the +/// source of whatever datagram connected it: once any pair is valid, str0m may report the +/// connection while handling another datagram, such as a spoofed request after one switched +/// its role to controlling. fn answers_check(checks: &VecDeque<(TransId, SocketAddr)>, data: &[u8], src: SocketAddr) -> bool { let Some(message) = stun(data) else { return false; diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index 9a72424fe46..d4b396f1934 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -17,9 +17,9 @@ //! A binding request always routes by its ufrag, but one for an address another //! live session holds only claims it: anyone holding a session can sign a request, //! and a spoofed source proves nothing. The claim takes the address once the -//! claimant's own ICE connects on a datagram from it that proves the peer is there -//! ([`Route::confirm`]): the answer to a check sent to that address, which only it -//! saw (STUN responses go to both sessions meanwhile), or once the holder ends. So +//! claimant's own ICE connects, if the address answered one of the claimant's checks +//! ([`Route::confirm`]), which proves its peer is there since only that address saw the +//! check (STUN responses go to both sessions meanwhile), or once the holder ends. So //! a peer that re-offers from the same address reaches its new session, and neither //! a forged request nor a session answering its own check from a spoofed source //! moves anyone's traffic. @@ -116,8 +116,8 @@ pub(crate) struct Route { } impl Route { - /// Take `src` if this session claimed it: its ICE connected on a datagram that proves - /// the peer is at `src`, as the answer to a check sent there (see `ice::agent::answers_check`). + /// Take `src` if this session claimed it: its ICE connected, and `src` answered a check + /// the session sent there, which proves its peer is at `src` (see `ice::agent::answers_check`). pub(crate) fn confirm(&self, src: SocketAddr) { let mut registry = self.registry.lock().unwrap(); if registry.claims.get(&src).is_some_and(|claim| claim.ufrag == self.ufrag) { From 456f4e3cad7e452b74fa74af4220fe22b1d4e400 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 03:34:09 -0800 Subject: [PATCH 8/8] fix(webrtc): a holder's refresh keeps another session's claim Review round: a holder refreshing its own address no longer drops the claim another session holds on it, so a claimant whose answer was already queued still confirms. The close test drains after each write as str0m requires, the clean-close tests await the viewer's task so a panic fails them, and the real-socket mux tests opt into the host clock. Co-Authored-By: Claude Opus 5.5 --- rs/moq-webrtc/src/ice/agent.rs | 10 ++++------ rs/moq-webrtc/src/lib.rs | 32 ++++++++++++++++++++++++-------- rs/moq-webrtc/src/server/mux.rs | 27 ++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/rs/moq-webrtc/src/ice/agent.rs b/rs/moq-webrtc/src/ice/agent.rs index a8bc8803765..794fa70fe0a 100644 --- a/rs/moq-webrtc/src/ice/agent.rs +++ b/rs/moq-webrtc/src/ice/agent.rs @@ -581,17 +581,15 @@ mod tests { } } - // Every frame written in one go, as a session drains its writes, then the close. - let mut rtc = sender.rtc_mut(); - let pt = rtc.writer(mid).unwrap().payload_params().next().unwrap().pt(); + // Every frame written back to back, each through its own guard, which drains it as + // str0m requires before the next write, then the close. + let pt = (sender.rtc_mut().writer(mid).unwrap().payload_params().next().unwrap()).pt(); for n in 0..FRAMES { let time = MediaTime::new(960 * n, Frequency::FORTY_EIGHT_KHZ); - rtc.writer(mid) - .unwrap() + (sender.rtc_mut().writer(mid).unwrap()) .write(pt, now, time, vec![0xfc, n as u8]) .unwrap(); } - drop(rtc); sender.close(); let mut received = 0; diff --git a/rs/moq-webrtc/src/lib.rs b/rs/moq-webrtc/src/lib.rs index d2a4dbbe7aa..1da2ab22d80 100644 --- a/rs/moq-webrtc/src/lib.rs +++ b/rs/moq-webrtc/src/lib.rs @@ -235,15 +235,21 @@ mod tests { (viewer, opus_output(&output_consumer).await) } - /// A bare str0m WHEP viewer of `url`, an audio line alone: each payload it receives, until - /// the channel closes with the server's close. Unlike [`Client::pull`], which aborts its - /// output when the session ends, it keeps everything that arrived. - async fn raw_view(url: url::Url) -> tokio::sync::mpsc::UnboundedReceiver> { + /// A bare str0m WHEP viewer of `url`, an audio line alone: the task driving it, which ends + /// with the server's close, and each payload it receives, until the channel closes with that + /// task. Unlike [`Client::pull`], which aborts its output when the session ends, it keeps + /// everything that arrived. + async fn raw_view( + url: url::Url, + ) -> ( + tokio::task::JoinHandle<()>, + tokio::sync::mpsc::UnboundedReceiver>, + ) { let udp = std::sync::Arc::new(tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap()); - raw_view_on(url, udp).await.1 + raw_view_on(url, udp).await } - /// [`raw_view`] from `udp`, with the task driving it, which ends with the server's close. + /// [`raw_view`] from `udp`. async fn raw_view_on( url: url::Url, udp: std::sync::Arc, @@ -343,7 +349,7 @@ mod tests { push(&mut *bridge, 20_000, OPUS_PACKET); let (url, http, mut sessions) = serve_whep(&origin).await; - let mut viewer = raw_view(url).await; + let (task, mut viewer) = raw_view(url).await; let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await .expect("WHEP negotiation timed out") @@ -360,6 +366,11 @@ mod tests { "the final packet was lost" ); assert_eq!(received(&mut viewer).await, None, "the viewer was not closed"); + // The channel also closes if the viewer panicked, so only its task tells a clean close. + kio::clock::timeout(TIMEOUT, task) + .await + .expect("the viewer outlived its channel") + .expect("the viewer failed"); kio::clock::timeout(TIMEOUT, session) .await .expect("the session outlived its broadcast") @@ -378,7 +389,7 @@ mod tests { catalog.finish().unwrap(); let (url, http, mut sessions) = serve_whep(&origin).await; - let mut viewer = raw_view(url).await; + let (task, mut viewer) = raw_view(url).await; let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await .expect("WHEP negotiation timed out") @@ -389,6 +400,11 @@ mod tests { let _finished = bridge.finish().unwrap(); assert_eq!(received(&mut viewer).await.as_deref(), Some(NEW_PACKET)); assert_eq!(received(&mut viewer).await, None, "the viewer was not closed"); + // The channel also closes if the viewer panicked, so only its task tells a clean close. + kio::clock::timeout(TIMEOUT, task) + .await + .expect("the viewer outlived its channel") + .expect("the viewer failed"); kio::clock::timeout(TIMEOUT, session) .await .expect("the session outlived its broadcast") diff --git a/rs/moq-webrtc/src/server/mux.rs b/rs/moq-webrtc/src/server/mux.rs index d4b396f1934..1c522c62ab5 100644 --- a/rs/moq-webrtc/src/server/mux.rs +++ b/rs/moq-webrtc/src/server/mux.rs @@ -149,9 +149,9 @@ impl Registry { None => { self.by_addr.insert(src, entry.clone()); } - Some(holder) if Arc::ptr_eq(&holder, &entry) => { - self.claims.remove(&src); - } + // The holder refreshing its own address leaves any claim on it, which + // waits for its claimant to confirm, end, or be replaced. + Some(holder) if Arc::ptr_eq(&holder, &entry) => {} Some(_) => { self.claims.insert(src, entry.clone()); } @@ -401,6 +401,7 @@ mod tests { /// path, or the holder ends. A forged request claims nothing. #[tokio::test] async fn a_claimed_address_moves_once_confirmed() { + kio::clock::host_time(); let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); let port = mux.local_addr().port(); let (old, mut old_inbound, _old_registration) = mux.register(); @@ -633,6 +634,25 @@ mod tests { .is_some_and(|entry| entry.ufrag == session.ufrag) } + /// A claim survives its holder refreshing the address, so the claimant can still confirm it. + #[tokio::test] + async fn a_claim_survives_its_holder_refreshing() { + let mux = Mux::bind("127.0.0.1:0".parse().unwrap(), &[]).await.unwrap(); + let (old, _old_inbound, _old_registration) = mux.register(); + let (new, new_inbound, _new_registration) = mux.register(); + let (request_old, request_new) = (binding_request(&old), binding_request(&new)); + let address = "127.0.0.1:10001".parse().unwrap(); + let mut registry = mux.registry.lock().unwrap(); + registry.route(address, &Datagram::parse(&request_old)); + registry.route(address, &Datagram::parse(&request_new)); + registry.route(address, &Datagram::parse(&request_old)); + drop(registry); + + new_inbound.route.as_ref().unwrap().confirm(address); + let [holder, _] = mux.registry.lock().unwrap().route(address, &Datagram::Other); + assert_eq!(holder.unwrap().ufrag, new.ufrag, "the refresh dropped the claim"); + } + /// A claimant takes the address once its holder ends, confirmed or not. #[tokio::test] async fn a_claim_outlives_its_holder() { @@ -661,6 +681,7 @@ mod tests { /// against the wrong-family ICE host candidate. #[tokio::test] async fn dual_stack_mux_routes_an_ipv4_peer_with_a_canonical_source() { + kio::clock::host_time(); let bind = SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0); let advertise = [ SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),