diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b52300..0959226 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,16 @@ All notable changes to Veil. Dates are dd/mm/yyyy. port opening is on. - The log says when each media link to another member comes up and how (direct, punched, relayed), and once if it has not come up after 20 seconds, on both sides of the link. +- **The host is warned when guests cannot see each other.** Guests reach each other through + the host's own rendezvous. If someone is in the call but has not registered there after 10 + seconds, the host sees "Guests may not see each other" and veil.log names who. Before, the + warning came only when the router refused the second port, and a port that opened but + could not be reached went unnoticed. +- Guests log whether they registered with the call's rendezvous, and why not. +- **A guest who joins is no longer missed by the others' first dial.** Members ask the + rendezvous to introduce them to a new guest as soon as it joins, often before the guest has + registered there. That request was dropped, so the first attempt waited out its timeout. The + rendezvous now passes it on when the guest registers. ## 0.1.18, 05/12/2026 diff --git a/crates/veil-app/src/demo.rs b/crates/veil-app/src/demo.rs index ad8993b..e8c5e22 100644 --- a/crates/veil-app/src/demo.rs +++ b/crates/veil-app/src/demo.rs @@ -82,6 +82,7 @@ pub fn call(your_name: &str, is_host: bool, now: Instant) -> Call { router: crate::model::RouterPort::Off, reach: crate::model::Reachable::Unknown, server_down: false, + members_apart: false, outbox: Vec::new(), unread: 0, moderators: Vec::new(), diff --git a/crates/veil-app/src/engine/helper_evidence.rs b/crates/veil-app/src/engine/helper_evidence.rs index f5d446e..9086e04 100644 --- a/crates/veil-app/src/engine/helper_evidence.rs +++ b/crates/veil-app/src/engine/helper_evidence.rs @@ -23,23 +23,41 @@ pub(super) struct HelperWatch { impl HelperWatch { /// The evidence at start. Write it with [`Self::write`]. pub fn start(call: &CallNode, needed: bool, hosting: bool) -> Self { - Self { + let watch = Self { connected: call.has_rendezvous(), issue: call.helper_issue(), needed, hosting, + }; + // Hosts log their helper in the host loop. + if needed && !hosting { + crate::note!("{}", watch.line()); } + watch } /// Rewrites the row when the live state differs. Returns the new connection state. pub fn refresh(&mut self, shared: &Shared, call: &CallNode) -> bool { if self.observe(call.has_rendezvous(), call.helper_issue()) { let watch = *self; + if watch.needed && !watch.hosting { + crate::note!("{}", watch.line()); + } shared.diagnose(|d| watch.write(d)); } self.connected } + /// The log line for this state. Other members reach this one through the rendezvous, so + /// a missing registration is what a failed member-to-member link most often comes down to. + fn line(self) -> String { + match (self.connected, self.issue) { + (true, _) => String::from("rendezvous: registered in the call's room"), + (false, Some(issue)) => format!("rendezvous: not registered: {issue:?}"), + (false, None) => String::from("rendezvous: not registered yet"), + } + } + /// Whether `connected` and `issue` change what the report shows. fn observe(&mut self, connected: bool, issue: Option) -> bool { // A lost connection with no recorded kind yet keeps the last typed one. @@ -118,6 +136,16 @@ mod tests { assert_eq!(report.problem, Some(Problem::NoUdpReply)); } + #[test] + fn the_log_line_says_whether_and_why_not_registered() { + let mut watch = watch(); + assert_eq!(watch.line(), "rendezvous: not registered: Identity"); + watch.issue = None; + assert_eq!(watch.line(), "rendezvous: not registered yet"); + watch.connected = true; + assert_eq!(watch.line(), "rendezvous: registered in the call's room"); + } + #[test] fn a_guest_row_does_not_touch_hosting() { let mut watch = HelperWatch { diff --git a/crates/veil-app/src/engine/host.rs b/crates/veil-app/src/engine/host.rs index 4d44ae6..d25c607 100644 --- a/crates/veil-app/src/engine/host.rs +++ b/crates/veil-app/src/engine/host.rs @@ -56,6 +56,7 @@ use veil_net::{Node, NodeAddr}; use veil_rendezvous::{Refusal, RendezvousServer, room_tag}; use veil_security::Identity; +mod apart; mod forwarder_check; mod hosting; mod moderated; @@ -129,8 +130,8 @@ pub async fn run( (Some(node), Some(_)) => node.resolve_ipv6().await, _ => None, }; - let (rendezvous, rendezvous6) = match configured { - Some(addr) => (addr, None), + let (rendezvous, rendezvous6, own_room) = match configured { + Some(addr) => (addr, None, None), None => own_server(lan, Own::Rendezvous, &identity, room_id, config.local_only, v6)?, }; let forwarder = match ( @@ -271,6 +272,8 @@ pub async fn run( v6_port, rendezvous6, relay_v6_port, + own_room, + apart: apart::Unregistered::default(), at6, reach, persistent, @@ -362,7 +365,7 @@ enum Own { /// Starts a rendezvous or forwarder for this call only: it serves `host`'s room `room_id` and /// refuses everyone else's, so strangers who find its port cannot use this upload. /// Returns its node and, for a rendezvous given the host's public IPv6 address `v6`, that -/// address when it listens there too. +/// address when it listens there too, and its room to read who registered. fn own_server( lan: Option, kind: Own, @@ -370,7 +373,7 @@ fn own_server( room_id: [u8; 16], local_only: bool, v6: Option, -) -> Result<(NodeAddr, Option), String> { +) -> Result<(NodeAddr, Option, Option), String> { let what = match kind { Own::Rendezvous => "rendezvous", Own::Forwarder => "forwarder", @@ -404,19 +407,25 @@ fn own_server( }; // `bind_any` falls back to IPv4 where the system has no dual-stack socket. let v6 = v6.filter(|_| dual && !local_only && node.ipv6()); + let mut own_room = None; match kind { - Own::Rendezvous => drop(tokio::spawn( - RendezvousServer::new(node) - .only_room(room_tag(&room_id), host.public()) - .run(), - )), + Own::Rendezvous => { + let tag = room_tag(&room_id); + let server = RendezvousServer::new(node).only_room(tag, host.public()); + own_room = Some(apart::OwnRoom { + roster: server.roster(), + tag, + host: host.public(), + }); + drop(tokio::spawn(server.run())); + } Own::Forwarder => drop(tokio::spawn( ForwarderServer::new(node, Limits::default()) .only_host(host.public()) .run(), )), } - Ok((addr, v6)) + Ok((addr, v6, own_room)) } /// The link to show for `full`: a short one when that works as well, otherwise `full` itself. diff --git a/crates/veil-app/src/engine/host/apart.rs b/crates/veil-app/src/engine/host/apart.rs new file mode 100644 index 0000000..397a808 --- /dev/null +++ b/crates/veil-app/src/engine/host/apart.rs @@ -0,0 +1,137 @@ +//! Spots members who are in the call but not registered on the host's own rendezvous. +//! +//! Members link to each other only through the host's rendezvous: introductions for a punch, +//! and the relay when that fails. A member the rendezvous has never seen can reach the host +//! but no other member. Router answers cannot show this (a port can map and still be +//! unreachable), so the host reads who actually registered (`veil_rendezvous::Roster`). +//! +//! Invariants: +//! - A member gets [`GRACE`] to register after it is first seen in the call, since joiners +//! register while they connect and a rejoin re-registers. +//! - Each member is reported once when it goes overdue, and once when it registers after that. +//! - "Apart" needs two or more other members: with one, there is nobody to be apart from. +//! - Only ever compares keys; addresses never leave the rendezvous. + +use std::collections::{HashMap, HashSet}; +use std::time::{Duration, Instant}; + +use veil_rendezvous::Roster; +use veil_security::PublicId; + +/// The host's own rendezvous room, read for who registered in it. +pub(in crate::engine) struct OwnRoom { + pub roster: Roster, + pub tag: [u8; 32], + pub host: PublicId, +} + +impl OwnRoom { + pub fn registered(&self) -> Vec { + self.roster.registered(self.tag, self.host) + } +} + +/// How long a member may be in the call without registering before it counts as overdue. +pub(super) const GRACE: Duration = Duration::from_secs(10); + +/// What changed in one check. +#[derive(Debug, Default, PartialEq, Eq)] +pub(in crate::engine) struct Change { + /// Members that just went overdue. + pub late: Vec, + /// Overdue members that have now registered. + pub back: Vec, + /// Whether members are apart now: one is overdue and there is someone to be apart from. + pub apart: bool, +} + +#[derive(Default)] +pub(in crate::engine) struct Unregistered { + /// Members not registered, with when that was first seen. + since: HashMap, + /// Members already reported overdue. + reported: HashSet, + /// Whether the host is shown the warning now. + pub shown: bool, +} + +impl Unregistered { + /// Compares `others` (everyone in the call but the host) with the keys `registered` on + /// the rendezvous at `now`. + pub fn check(&mut self, others: &[PublicId], registered: &[PublicId], now: Instant) -> Change { + let registered: HashSet<&PublicId> = registered.iter().collect(); + let mut change = Change::default(); + self.since.retain(|member, _| others.contains(member)); + self.reported.retain(|member| others.contains(member)); + for member in others { + if registered.contains(member) { + self.since.remove(member); + if self.reported.remove(member) { + change.back.push(*member); + } + continue; + } + let since = *self.since.entry(*member).or_insert(now); + if now.duration_since(since) >= GRACE && self.reported.insert(*member) { + change.late.push(*member); + } + } + change.apart = !self.reported.is_empty() && others.len() >= 2; + change + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn key(n: u8) -> PublicId { + PublicId([n; 32]) + } + + #[test] + fn a_member_gets_the_grace_period_then_is_reported_once() { + let mut watch = Unregistered::default(); + let start = Instant::now(); + let others = [key(1), key(2)]; + let registered = [key(1)]; + let change = watch.check(&others, ®istered, start); + assert!( + change.late.is_empty() && !change.apart, + "still within the grace period" + ); + let change = watch.check(&others, ®istered, start + GRACE); + assert_eq!(change.late, vec![key(2)]); + assert!(change.apart); + let change = watch.check(&others, ®istered, start + GRACE * 2); + assert!(change.late.is_empty(), "reported once"); + assert!(change.apart, "still apart"); + } + + #[test] + fn registering_late_is_reported_and_ends_apart() { + let mut watch = Unregistered::default(); + let start = Instant::now(); + let others = [key(1), key(2)]; + watch.check(&others, &[key(1)], start); + watch.check(&others, &[key(1)], start + GRACE); + let change = watch.check(&others, &[key(1), key(2)], start + GRACE * 2); + assert_eq!(change.back, vec![key(2)]); + assert!(!change.apart); + } + + #[test] + fn one_other_member_is_never_apart_and_leavers_are_forgotten() { + let mut watch = Unregistered::default(); + let start = Instant::now(); + let change = watch.check(&[key(1)], &[], start + GRACE); + assert!(!change.apart, "nobody to be apart from"); + watch.check(&[key(1)], &[], start + GRACE * 2); + let change = watch.check(&[key(3), key(4)], &[key(3), key(4)], start + GRACE * 3); + assert!( + change.back.is_empty(), + "a member who left is not reported back" + ); + assert!(!change.apart); + } +} diff --git a/crates/veil-app/src/engine/host/hosting.rs b/crates/veil-app/src/engine/host/hosting.rs index 79875f3..f0fcd3b 100644 --- a/crates/veil-app/src/engine/host/hosting.rs +++ b/crates/veil-app/src/engine/host/hosting.rs @@ -19,6 +19,7 @@ use veil_call::{CallNode, HostEvent, HostHandle, RelayAddrs}; use veil_net::NodeAddr; use veil_security::PublicId; +use super::apart::{OwnRoom, Unregistered}; use super::forwarder_check::{PendingPlan, checked_plan, finish_checked_plan, own_check}; use super::{ check, close_router, hosted_addresses, moderated, new_invite, next_router, next_v6, @@ -65,6 +66,9 @@ pub(in crate::engine) struct Hosting { pub rendezvous6: Option, /// That rendezvous port's IPv6 pinhole, asked for with the call's. pub relay_v6_port: Option, + /// The host's own rendezvous room, when it runs one (`apart.rs`). + pub own_room: Option, + pub apart: Unregistered, pub at6: Option, pub reach: Reachability, pub persistent: bool, @@ -117,6 +121,28 @@ impl Hosting { self.shared.update(|view| view.handover.choices = shown); } + /// Notes members in the call that never registered on this host's own rendezvous, so other + /// members cannot reach them, and warns while any are (`apart.rs`). + fn check_apart(&mut self, roster: &[(L, PublicId)], me: PublicId) { + let Some(own) = &self.own_room else { return }; + let others: Vec = roster.iter().map(|(_, d)| *d).filter(|d| *d != me).collect(); + let change = self.apart.check(&others, &own.registered(), Instant::now()); + for member in &change.late { + crate::note!( + "rendezvous: {} is in the call but not registered here, so other members cannot reach it", + short(member) + ); + } + for member in &change.back { + crate::note!("rendezvous: {} registered", short(member)); + } + if change.apart != self.apart.shown { + self.apart.shown = change.apart; + let apart = change.apart; + self.shared.update(|view| view.members_apart = apart); + } + } + /// Runs the call until this device leaves it, ends it or hands it over. pub async fn run(mut self, commands: &mut UnboundedReceiver) -> Result<(), String> { let me = self.me; @@ -267,6 +293,7 @@ impl Hosting { let down = self.server_down; self.shared.update(|view| view.server_down = down); } + self.check_apart(&roster, me); if self.persistent && handing.is_none() && let Ok(choices) = self.host.choices().await { diff --git a/crates/veil-app/src/engine/host/successor.rs b/crates/veil-app/src/engine/host/successor.rs index 2aa713a..9b9610c 100644 --- a/crates/veil-app/src/engine/host/successor.rs +++ b/crates/veil-app/src/engine/host/successor.rs @@ -23,6 +23,7 @@ use veil_call::{CallNode, HostEvent, HostHandle, Promotion, RelayAddrs}; use veil_net::NodeAddr; use veil_security::Identity; +use super::apart::{OwnRoom, Unregistered}; use super::hosting::Hosting; use super::{Own, check, hosted_addresses, new_invite, own_server, publish_addresses}; use crate::engine::helper_evidence::HelperWatch; @@ -38,6 +39,7 @@ pub(in crate::engine) struct Prepared { pub contact: Vec, rendezvous: NodeAddr, rendezvous6: Option, + own_room: Option, configured: Option, forwarder: Option, relay: RelayAddrs, @@ -62,8 +64,8 @@ pub(in crate::engine) async fn prepare( veil_net::family::global_ipv6() }; let configured = resolve(config.rendezvous.as_ref(), "rendezvous").await?; - let (rendezvous, rendezvous6) = match configured { - Some(addr) => (addr, None), + let (rendezvous, rendezvous6, own_room) = match configured { + Some(addr) => (addr, None, None), None => own_server(lan, Own::Rendezvous, identity, room_id, config.local_only, v6)?, }; let forwarder = match (resolve(config.forwarder.as_ref(), "forwarder").await?, configured) { @@ -83,6 +85,7 @@ pub(in crate::engine) async fn prepare( contact, rendezvous, rendezvous6, + own_room, configured, forwarder, relay, @@ -144,6 +147,7 @@ pub(in crate::engine) async fn take_over( let Prepared { rendezvous, rendezvous6, + own_room, configured, forwarder, relay, @@ -211,6 +215,8 @@ pub(in crate::engine) async fn take_over( v6_port, rendezvous6, relay_v6_port, + own_room, + apart: Unregistered::default(), at6: None, reach, persistent: true, @@ -237,6 +243,7 @@ mod tests { contact: seal(&me, room, 3, &addrs, &relay).unwrap(), rendezvous: node, rendezvous6: None, + own_room: None, configured: None, forwarder: None, relay: relay.clone(), diff --git a/crates/veil-app/src/engine/view.rs b/crates/veil-app/src/engine/view.rs index 4e58d1b..52eaae7 100644 --- a/crates/veil-app/src/engine/view.rs +++ b/crates/veil-app/src/engine/view.rs @@ -105,6 +105,8 @@ pub struct View { pub reach: Reachable, /// See `Call::server_down`. pub server_down: bool, + /// See `Call::members_apart`. + pub members_apart: bool, /// Who the host named moderators (`engine/moderation.rs`). pub moderators: Vec, /// The host named this device a moderator. @@ -189,6 +191,7 @@ impl Shared { router: RouterPort::Off, reach: Reachable::Unknown, server_down: false, + members_apart: false, moderators: Vec::new(), you_moderate: false, hosting: false, @@ -389,6 +392,7 @@ pub fn apply( call.router.clone_from(&view.router); call.reach.clone_from(&view.reach); call.server_down = view.server_down; + call.members_apart = view.members_apart; call.epoch = view.epoch; call.group.clone_from(&view.group); call.moderators = view @@ -504,6 +508,7 @@ mod tests { router: RouterPort::Off, reach: Reachable::Unknown, server_down: false, + members_apart: false, moderators: vec![PublicId([2; 32])], you_moderate: false, hosting: false, diff --git a/crates/veil-app/src/model.rs b/crates/veil-app/src/model.rs index d88522e..95be511 100644 --- a/crates/veil-app/src/model.rs +++ b/crates/veil-app/src/model.rs @@ -185,6 +185,9 @@ pub struct Call { pub reach: Reachable, /// For a host: the rendezvous server it uses is not answering, and Veil keeps trying. pub server_down: bool, + /// For a host: someone in the call never reached its own rendezvous, so the others cannot + /// reach them (`engine/host/apart.rs`). + pub members_apart: bool, /// Decisions and chat not yet handed to the engine. Always empty in a demo. pub outbox: Vec, /// Chat messages not yet seen with the chat panel open. @@ -236,6 +239,7 @@ impl Call { router: RouterPort::Off, reach: Reachable::Unknown, server_down: false, + members_apart: false, outbox: Vec::new(), unread: 0, moderators: Vec::new(), diff --git a/crates/veil-app/src/screens/panels.rs b/crates/veil-app/src/screens/panels.rs index 5124403..562b5a6 100644 --- a/crates/veil-app/src/screens/panels.rs +++ b/crates/veil-app/src/screens/panels.rs @@ -148,9 +148,29 @@ fn reach(ui: &mut egui::Ui, reach: &Reachable) { pub(crate) fn host_status(view: &CallView, ui: &mut egui::Ui) { server(ui, view.call.server_down); router(ui, &view.call.router); + apart(ui, view.call.members_apart, &view.call.router); reach(ui, &view.call.reach); } +/// For a host: someone in the call never reached the host's own rendezvous, so the others +/// cannot link to them. Not shown again when the router line already says so. +fn apart(ui: &mut egui::Ui, apart: bool, port: &RouterPort) { + if !apart || matches!(port, RouterPort::GuestsApart(_)) { + return; + } + ui.add_space(4.0); + ui.label( + RichText::new("Guests may not see each other") + .small() + .color(theme::WARN), + ) + .on_hover_text( + "Someone reached you but not your rendezvous, the server guests use to reach each \ + other, so the others cannot see or hear them. veil.log names who. To fix this, add \ + a public veil-node under Settings > Connection > Rendezvous and relay.", + ); +} + fn people(view: &mut CallView, playback: &Playback, ui: &mut egui::Ui, now: Instant) { section(ui, Icon::Link, "Invite"); let copy = pill( diff --git a/crates/veil-rendezvous/src/lib.rs b/crates/veil-rendezvous/src/lib.rs index 37d98e5..f4d518a 100644 --- a/crates/veil-rendezvous/src/lib.rs +++ b/crates/veil-rendezvous/src/lib.rs @@ -44,6 +44,7 @@ pub mod limit; pub mod protocol; pub mod reach; mod room; +pub mod roster; pub mod server; mod sessions; mod streams; @@ -54,6 +55,7 @@ use std::fmt; pub use client::{Punch, Reach, RendezvousClient, check_reach, punch}; pub use invites::{FetchError, fetch_invite, store_invite}; pub use protocol::{Ask, Refusal, room_tag}; +pub use roster::Roster; pub use server::RendezvousServer; pub use socket::RelaySocket; diff --git a/crates/veil-rendezvous/src/roster.rs b/crates/veil-rendezvous/src/roster.rs new file mode 100644 index 0000000..8a4a829 --- /dev/null +++ b/crates/veil-rendezvous/src/roster.rs @@ -0,0 +1,42 @@ +//! Who is registered on a rendezvous server, for the participant that runs it. +//! +//! A host runs its own rendezvous for its call. Members reach each other only through it, so +//! a member that is in the call but not registered here cannot be introduced or relayed to. +//! The host reads this to warn about that, instead of guessing from router answers. +//! +//! Invariants: +//! - Keys only. No addresses, rooms of other hosts or relay details leave the server. +//! - Holds the server's state weakly, so it never keeps a stopped server's rooms alive. Once +//! the server is gone every room reads as empty. + +use std::sync::{Arc, Mutex, Weak}; + +use veil_security::PublicId; + +use crate::server::State; + +/// A read-only view of the keys registered on one server. Cheap to clone. +#[derive(Clone)] +pub struct Roster(Weak>); + +impl Roster { + pub(crate) fn new(state: &Arc>) -> Self { + Self(Arc::downgrade(state)) + } + + /// The keys registered in the room `room_tag` hosted by `host`, the host included. Empty + /// when the room is not open or the server has stopped. + pub fn registered(&self, room_tag: [u8; 32], host: PublicId) -> Vec { + let Some(state) = self.0.upgrade() else { + return Vec::new(); + }; + let Ok(state) = state.lock() else { + return Vec::new(); + }; + state + .rooms + .get(&(room_tag, host)) + .map(|room| room.members.keys().copied().collect()) + .unwrap_or_default() + } +} diff --git a/crates/veil-rendezvous/src/server.rs b/crates/veil-rendezvous/src/server.rs index 5002d87..75ee4bb 100644 --- a/crates/veil-rendezvous/src/server.rs +++ b/crates/veil-rendezvous/src/server.rs @@ -7,7 +7,8 @@ //! Consent: an introduction or relay session is only a request until its target accepts //! (`ClientMsg::Accept`). Until then the asker learns nothing about the target, not even //! that it is online, so holding an invite is not enough to learn members' IP addresses or -//! to fill their relay slots. Requests expire after `REQUEST_TTL`; each member may have +//! to fill their relay slots. A request for a member not yet registered reaches it when it +//! registers. Requests expire after `REQUEST_TTL`; each member may have //! `MAX_REQUESTS_PER_MEMBER` open, and `sessions.rs` limits who holds relay sessions. //! A link must open its control stream within `CONTROL_WAIT` and register within //! `UNREGISTERED_WAIT`, or it is closed. @@ -65,8 +66,8 @@ const OUTBOX: usize = 64; /// A link that opens no control stream in this time is dropped. const CONTROL_WAIT: Duration = Duration::from_secs(10); -struct State { - rooms: HashMap, +pub(crate) struct State { + pub(crate) rooms: HashMap, sessions: Sessions, /// Requests waiting for their target, scoped to the registered room and host. requests: HashMap<(RoomKey, PublicId, PublicId, Ask), Instant>, @@ -192,6 +193,11 @@ impl RendezvousServer { self } + /// Who is registered here, for the participant running this server (`roster.rs`). + pub fn roster(&self) -> crate::roster::Roster { + crate::roster::Roster::new(&self.state) + } + pub fn id(&self) -> PublicId { self.node.id() } @@ -417,7 +423,26 @@ fn handle( }, ); *room = Some(key); - vec![(outbox.clone(), ServerMsg::Registered { observed })] + let mut out = vec![(outbox.clone(), ServerMsg::Registered { observed })]; + // A member asked for this one before it registered, as when a member dials a + // joiner the moment the plan names it: pass the request on now, or it is lost and + // the asker waits out its answer timeout. Only the target learns of it, as always. + let now = Instant::now(); + out.extend( + s.requests + .iter() + .filter(|((r, _, to, _), at)| { + *r == key && *to == me && now.duration_since(**at) < REQUEST_TTL + }) + .map(|((_, from, _, kind), _)| { + let request = ServerMsg::Request { + from: *from, + kind: *kind, + }; + (outbox.clone(), request) + }), + ); + out } ClientMsg::Allow { members, passes } => allow(&mut s, room, me, members, passes, outbox), diff --git a/crates/veil-rendezvous/tests/loopback.rs b/crates/veil-rendezvous/tests/loopback.rs index fa027c1..542e1c9 100644 --- a/crates/veil-rendezvous/tests/loopback.rs +++ b/crates/veil-rendezvous/tests/loopback.rs @@ -118,6 +118,26 @@ async fn introduction_lets_two_members_connect_directly() { assert_eq!(with_timeout(accept).await.unwrap(), a_key); } +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_request_made_before_the_target_registers_reaches_it_when_it_does() { + let server = start_server(); + let room = room_tag(&[9; 16]); + let mut a = host(server, room).await; + let a_key = a.identity.public(); + // The joiner is admitted (allowed) but its presence has not registered yet. + let late = Identity::generate(); + let late_key = late.public(); + a.allowed.push(late_key); + a.client.allow(a.allowed.clone(), Vec::new()).await.unwrap(); + + let asking = tokio::spawn(async move { with_timeout(a.client.introduce(late_key)).await }); + tokio::time::sleep(Duration::from_millis(300)).await; + let mut b = register(server, late, room, a_key, None).await.unwrap(); + consent(&mut b, a_key, Ask::Introduce).await; + let plan = asking.await.unwrap().unwrap(); + assert_eq!(plan.peer, late_key); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn a_punch_does_not_wait_as_long_as_a_notice_says() { let node = Node::bind(&Identity::generate(), localhost()).unwrap(); diff --git a/crates/veil-rendezvous/tests/roster.rs b/crates/veil-rendezvous/tests/roster.rs new file mode 100644 index 0000000..f4603c4 --- /dev/null +++ b/crates/veil-rendezvous/tests/roster.rs @@ -0,0 +1,65 @@ +//! The roster a host reads from its own rendezvous: who is registered in its room. +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use std::net::SocketAddr; + +use veil_net::Node; +use veil_rendezvous::{RendezvousClient, RendezvousServer, room_tag}; +use veil_security::Identity; + +fn localhost() -> SocketAddr { + "127.0.0.1:0".parse().unwrap() +} + +#[tokio::test] +async fn the_roster_lists_registered_keys_and_empties_when_the_server_stops() { + let server = RendezvousServer::new(Node::bind(&Identity::generate(), localhost()).unwrap()); + let (at, key, roster) = (server.local_addr().unwrap(), server.id(), server.roster()); + let running = tokio::spawn(server.run()); + let room = room_tag(&[7; 16]); + + let host = Identity::generate(); + let host_node = Node::bind(&host, localhost()).unwrap(); + let mut host_client = RendezvousClient::connect(&host_node, &[at], key) + .await + .unwrap(); + let local = host_node.local_addr().unwrap(); + host_client + .register(room, host.public(), None, vec![local]) + .await + .unwrap(); + + let guest = Identity::generate(); + let absent = Identity::generate(); + host_client + .allow(vec![guest.public(), absent.public()], Vec::new()) + .await + .unwrap(); + let guest_node = Node::bind(&guest, localhost()).unwrap(); + let mut guest_client = RendezvousClient::connect(&guest_node, &[at], key) + .await + .unwrap(); + let local = guest_node.local_addr().unwrap(); + guest_client + .register(room, host.public(), None, vec![local]) + .await + .unwrap(); + + let mut seen = roster.registered(room, host.public()); + seen.sort(); + let mut want = vec![host.public(), guest.public()]; + want.sort(); + assert_eq!(seen, want, "allowed but never registered is not listed"); + assert!( + roster + .registered(room_tag(&[8; 16]), host.public()) + .is_empty() + ); + + running.abort(); + let _ = running.await; + assert!( + roster.registered(room, host.public()).is_empty(), + "a stopped server lists nobody" + ); +}