Skip to main content

weirdnet_net/
node.rs

1//! The libp2p node: TCP and QUIC (or an in-memory transport for tests) with Noise and
2//! Yamux; mDNS on the local network; Kademlia, identify and ping; relay client and
3//! (opt-in) relay server with DCUtR hole punching; AutoNAT; and request-response on
4//! the profile's protocol id, carrying the application's own request and response types
5//! (CBOR).
6//!
7//! The swarm runs on its own thread with a small Tokio runtime. [`Node`] is a blocking
8//! handle (the CLI and TUI are synchronous): requests wait for their answer. Requests
9//! that come in are answered by a [`Handler`] on Tokio's blocking pool.
10use crate::addr::{addr_peer, reach_rank, routable, shareable};
11use crate::error::{NetError, Result};
12use crate::names::{Entry, Lookup, Names, NamesRequest, NamesResponse};
13use crate::profile::Profile;
14use futures::StreamExt;
15use libp2p::core::muxing::StreamMuxerBox;
16use libp2p::core::transport::{Boxed, MemoryTransport, Transport};
17use libp2p::core::upgrade::Version;
18use libp2p::identity::Keypair;
19use libp2p::kad::{self, store::MemoryStore, store::RecordStore};
20use libp2p::multiaddr::Protocol;
21use libp2p::request_response::{self, OutboundRequestId, ProtocolSupport};
22use libp2p::swarm::behaviour::toggle::Toggle;
23use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
24use libp2p::{Multiaddr, PeerId, StreamProtocol, Swarm, autonat, dcutr, identify, mdns, noise, ping, relay, tcp, yamux};
25use serde::de::DeserializeOwned;
26use serde::{Deserialize, Serialize};
27use std::collections::{HashMap, HashSet};
28use std::sync::{Arc, Mutex};
29use std::time::Duration;
30use tokio::sync::{mpsc, oneshot};
31
32const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
33const REQUEST_MAX: u64 = 8 * 1024 * 1024;
34const RESPONSE_MAX: u64 = 256 * 1024 * 1024;
35
36/// How a node runs.
37#[derive(Debug, Clone, Default)]
38pub struct NodeConfig {
39    /// Addresses to listen on (none: only dial out).
40    pub listen: Vec<Multiaddr>,
41    /// Find and announce peers on the local network.
42    pub mdns: bool,
43    /// Relay traffic for other nodes (and say so).
44    pub relay_server: bool,
45    /// Kademlia bootstrap nodes (`/ip4/…/tcp/…/p2p/…`).
46    pub bootstrap: Vec<Multiaddr>,
47    /// Relays to reserve a slot on (`/…/p2p/RELAY`); reachable through them behind NAT.
48    pub relays: Vec<Multiaddr>,
49    /// The in-memory transport (tests) instead of TCP and QUIC.
50    pub memory: bool,
51    /// This node's label (in identify; empty: none).
52    pub label: String,
53    /// Addresses others reach us at from outside (a DNS name or public IP forwarded to
54    /// this node), announced and put in invites before the local ones.
55    pub external: Vec<Multiaddr>,
56    /// The application's version, in the identify agent (`NAME/VERSION`); empty: this
57    /// crate's version.
58    pub version: String,
59    /// What the relay server (`relay_server`) allows; the default is libp2p's.
60    pub relay_limits: RelayLimits,
61    /// Serve the names protocol (rendezvous for signed peer records) from this store.
62    pub names_server: Option<Names>,
63    /// Names servers to register with and look users up on (`…/p2p/ID`), besides the
64    /// relays this node holds a reservation on.
65    pub names_servers: Vec<Multiaddr>,
66}
67
68/// Limits of the relay server: how many reservations and circuits it holds, and how long
69/// and how much one circuit may carry. [`RelayLimits::default`] keeps libp2p's defaults
70/// (circuits for hole punching and small requests: 2 minutes, 128 KiB);
71/// [`RelayLimits::unlimited`] caps nothing, so circuits carry data.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub struct RelayLimits {
74    /// Reservations held at once, and per peer.
75    pub max_reservations: usize,
76    pub max_reservations_per_peer: usize,
77    /// How long a reservation lasts before the client renews it.
78    pub reservation_duration: Duration,
79    /// Circuits open at once, and per peer.
80    pub max_circuits: usize,
81    pub max_circuits_per_peer: usize,
82    /// How long one circuit may stay open (at most `u32::MAX` seconds).
83    pub max_circuit_duration: Duration,
84    /// How many bytes one circuit may carry in each direction; 0: no limit.
85    pub max_circuit_bytes: u64,
86    /// libp2p's rate limiters on new reservations and circuits (per peer and per IP).
87    pub rate_limits: bool,
88}
89
90impl Default for RelayLimits {
91    /// libp2p's defaults.
92    fn default() -> Self {
93        let c = relay::Config::default();
94        RelayLimits {
95            max_reservations: c.max_reservations,
96            max_reservations_per_peer: c.max_reservations_per_peer,
97            reservation_duration: c.reservation_duration,
98            max_circuits: c.max_circuits,
99            max_circuits_per_peer: c.max_circuits_per_peer,
100            max_circuit_duration: c.max_circuit_duration,
101            max_circuit_bytes: c.max_circuit_bytes,
102            rate_limits: true,
103        }
104    }
105}
106
107impl RelayLimits {
108    /// Longest circuit duration libp2p accepts.
109    pub const MAX_CIRCUIT_DURATION: Duration = Duration::from_secs(u32::MAX as u64);
110
111    /// No caps: as many reservations and circuits as clients ask for, no byte limit, the
112    /// longest circuit libp2p accepts, and no rate limiters. Reservations still last an hour
113    /// before the client renews them.
114    pub fn unlimited() -> Self {
115        RelayLimits {
116            max_reservations: usize::MAX,
117            max_reservations_per_peer: usize::MAX,
118            max_circuits: usize::MAX,
119            max_circuits_per_peer: usize::MAX,
120            max_circuit_duration: Self::MAX_CIRCUIT_DURATION,
121            max_circuit_bytes: 0,
122            rate_limits: false,
123            ..Self::default()
124        }
125    }
126
127    /// Whether nothing is capped (see [`RelayLimits::unlimited`]).
128    pub fn is_unlimited(&self) -> bool {
129        *self == Self::unlimited()
130    }
131
132    /// Refuse limits libp2p cannot apply (a zero count or duration, or a circuit duration
133    /// over `u32::MAX` seconds).
134    pub fn check(&self) -> Result<()> {
135        if self.max_reservations == 0 || self.max_reservations_per_peer == 0 || self.max_circuits == 0 || self.max_circuits_per_peer == 0 {
136            return Err(NetError::invalid("relay limits", "reservation and circuit counts must be at least 1"));
137        }
138        if self.reservation_duration.is_zero() || self.max_circuit_duration.is_zero() {
139            return Err(NetError::invalid("relay limits", "durations must be longer than zero"));
140        }
141        if self.max_circuit_duration > Self::MAX_CIRCUIT_DURATION {
142            return Err(NetError::invalid("relay limits", "a circuit may last at most u32::MAX seconds"));
143        }
144        Ok(())
145    }
146
147    fn config(&self) -> relay::Config {
148        let mut c = relay::Config {
149            max_reservations: self.max_reservations,
150            max_reservations_per_peer: self.max_reservations_per_peer,
151            reservation_duration: self.reservation_duration,
152            max_circuits: self.max_circuits,
153            max_circuits_per_peer: self.max_circuits_per_peer,
154            max_circuit_duration: self.max_circuit_duration,
155            max_circuit_bytes: self.max_circuit_bytes,
156            ..relay::Config::default()
157        };
158        if !self.rate_limits {
159            c.reservation_rate_limiters.clear();
160            c.circuit_src_rate_limiters.clear();
161        }
162        c
163    }
164}
165
166impl NodeConfig {
167    /// TCP and QUIC on every interface, IPv4 and IPv6, on `port`.
168    pub fn default_listen(port: u16) -> Vec<Multiaddr> {
169        [
170            format!("/ip4/0.0.0.0/tcp/{port}"),
171            format!("/ip4/0.0.0.0/udp/{port}/quic-v1"),
172            format!("/ip6/::/tcp/{port}"),
173            format!("/ip6/::/udp/{port}/quic-v1"),
174        ]
175        .iter()
176        .filter_map(|a| a.parse().ok())
177        .collect()
178    }
179}
180
181/// A peer seen by the node, for the handler to remember.
182#[derive(Debug, Clone)]
183pub struct Sighting {
184    pub peer: PeerId,
185    pub addrs: Vec<String>,
186    pub caps: Option<Vec<String>>,
187    pub agent: Option<String>,
188    pub source: &'static str,
189}
190
191/// What a node carries: the application's request and response types (sent as CBOR).
192pub trait Message: Serialize + DeserializeOwned + std::fmt::Debug + Clone + Send + 'static {}
193impl<T: Serialize + DeserializeOwned + std::fmt::Debug + Clone + Send + 'static> Message for T {}
194
195/// Answers requests from peers. The peer id is the one Noise authenticated.
196pub trait Handler<Q, R>: Send + Sync + 'static {
197    fn request(&self, from: &PeerId, req: Q) -> R;
198    /// A peer was discovered or identified.
199    fn seen(&self, _s: Sighting) {}
200}
201
202/// What the node is doing.
203#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
204pub struct Status {
205    pub peer_id: String,
206    pub listening: Vec<String>,
207    pub external: Vec<String>,
208    pub connected: Vec<String>,
209    /// AutoNAT: `public`, `private` or `unknown`.
210    pub nat: String,
211    /// Relays holding a reservation for us.
212    pub reservations: Vec<String>,
213    pub relay_server: bool,
214    pub mdns: bool,
215    pub started_at: i64,
216}
217
218// rustc 1.100 flags code the `NetworkBehaviour` derive generates (matches on
219// uninhabited events) as unreachable; the allow has to sit on a module to reach it.
220#[allow(unreachable_code)]
221mod wire {
222    use super::*;
223
224    #[derive(NetworkBehaviour)]
225    pub(super) struct Behaviour<Q: Message, R: Message> {
226        pub(super) rr: request_response::cbor::Behaviour<Q, R>,
227        pub(super) names: request_response::cbor::Behaviour<NamesRequest, NamesResponse>,
228        pub(super) identify: identify::Behaviour,
229        pub(super) ping: ping::Behaviour,
230        pub(super) kad: kad::Behaviour<MemoryStore>,
231        pub(super) mdns: Toggle<mdns::tokio::Behaviour>,
232        pub(super) relay_client: relay::client::Behaviour,
233        pub(super) relay_server: Toggle<relay::Behaviour>,
234        pub(super) dcutr: dcutr::Behaviour,
235        pub(super) autonat: autonat::Behaviour,
236    }
237}
238use wire::{Behaviour, BehaviourEvent};
239
240enum Cmd<Q, R> {
241    Request { peer: PeerId, addrs: Vec<Multiaddr>, wire: Q, reply: oneshot::Sender<Result<R>> },
242    Names { peer: PeerId, addrs: Vec<Multiaddr>, req: NamesRequest, reply: oneshot::Sender<Result<NamesResponse>> },
243    PutEntry { entry: Entry, reply: oneshot::Sender<Result<()>> },
244    GetEntries { user: String, reply: oneshot::Sender<Result<Vec<Entry>>> },
245    FindPeer { peer: PeerId, reply: oneshot::Sender<Result<Vec<Multiaddr>>> },
246    KnownAddrs { reply: oneshot::Sender<Result<Vec<(PeerId, Multiaddr)>>> },
247    Dial { addr: Multiaddr, reply: oneshot::Sender<Result<PeerId>> },
248    Listen { addr: Multiaddr, reply: oneshot::Sender<Result<Multiaddr>> },
249    AddRelay { addr: Multiaddr },
250    Stop,
251}
252
253/// A running node.
254pub struct Node<Q: Message, R: Message> {
255    peer_id: PeerId,
256    profile: &'static Profile,
257    names_list: Vec<Multiaddr>,
258    tx: mpsc::UnboundedSender<Cmd<Q, R>>,
259    status: Arc<Mutex<Status>>,
260    thread: Option<std::thread::JoinHandle<()>>,
261}
262
263fn net_err(e: impl std::fmt::Display) -> NetError {
264    NetError::net(e)
265}
266
267fn stopped() -> NetError {
268    NetError::Network("the node has stopped".into())
269}
270
271fn behaviour<Q: Message, R: Message>(
272    p: &Profile,
273    key: &Keypair,
274    relay_client: relay::client::Behaviour,
275    cfg: &NodeConfig,
276) -> Result<Behaviour<Q, R>> {
277    let peer = key.public().to_peer_id();
278    let rr = request_response::cbor::Behaviour::with_codec(
279        request_response::cbor::codec::Codec::default().set_request_size_maximum(REQUEST_MAX).set_response_size_maximum(RESPONSE_MAX),
280        [(StreamProtocol::new(p.protocol), ProtocolSupport::Full)],
281        request_response::Config::default().with_request_timeout(REQUEST_TIMEOUT),
282    );
283    let version = if cfg.version.is_empty() { env!("CARGO_PKG_VERSION") } else { cfg.version.as_str() };
284    let agent = p.agent(version, &cfg.label);
285    // A relay with explicit external addresses announces only those, not every
286    // interface it listens on (private VPC and loopback ones are useless to clients).
287    let hide = cfg.relay_server && !cfg.external.is_empty();
288    let identify =
289        identify::Behaviour::new(identify::Config::new(p.identify.into(), key.public()).with_agent_version(agent).with_hide_listen_addrs(hide));
290    let mut kcfg = kad::Config::new(StreamProtocol::new(p.kad));
291    kcfg.set_query_timeout(Duration::from_secs(30));
292    // every record and provider a peer puts is checked first (peer records, relay keys)
293    kcfg.set_record_filtering(kad::StoreInserts::FilterBoth);
294    let names = request_response::cbor::Behaviour::with_codec(
295        request_response::cbor::codec::Codec::default().set_request_size_maximum(64 * 1024).set_response_size_maximum(64 * 1024),
296        [(StreamProtocol::new(p.names), if cfg.names_server.is_some() { ProtocolSupport::Full } else { ProtocolSupport::Outbound })],
297        request_response::Config::default().with_request_timeout(Duration::from_secs(20)),
298    );
299    let kad = kad::Behaviour::with_config(peer, MemoryStore::new(peer), kcfg);
300    let mdns = if cfg.mdns && !cfg.memory { Some(mdns::tokio::Behaviour::new(mdns::Config::default(), peer).map_err(net_err)?) } else { None };
301    let relay_server = cfg.relay_server.then(|| relay::Behaviour::new(peer, cfg.relay_limits.config()));
302    let autonat = autonat::Behaviour::new(peer, autonat::Config { only_global_ips: !cfg.memory, ..Default::default() });
303    Ok(Behaviour {
304        rr,
305        names,
306        identify,
307        ping: ping::Behaviour::new(ping::Config::new()),
308        kad,
309        mdns: mdns.into(),
310        relay_client,
311        relay_server: relay_server.into(),
312        dcutr: dcutr::Behaviour::new(peer),
313        autonat,
314    })
315}
316
317type BoxErr = Box<dyn std::error::Error + Send + Sync>;
318
319fn memory_transport(key: &Keypair) -> std::result::Result<Boxed<(PeerId, StreamMuxerBox)>, BoxErr> {
320    Ok(MemoryTransport::default()
321        .upgrade(Version::V1)
322        .authenticate(noise::Config::new(key).map_err(|e| Box::new(e) as BoxErr)?)
323        .multiplex(yamux::Config::default())
324        .timeout(Duration::from_secs(20))
325        .boxed())
326}
327
328fn build_swarm<Q: Message, R: Message>(p: &'static Profile, key: Keypair, cfg: &NodeConfig) -> Result<Swarm<Behaviour<Q, R>>> {
329    let idle = |c: libp2p::swarm::Config| c.with_idle_connection_timeout(Duration::from_secs(120));
330    let swarm = if cfg.memory {
331        let cfg2 = cfg.clone();
332        libp2p::SwarmBuilder::with_existing_identity(key)
333            .with_tokio()
334            .with_other_transport(memory_transport)
335            .map_err(net_err)?
336            .with_relay_client(noise::Config::new, yamux::Config::default)
337            .map_err(net_err)?
338            .with_behaviour(move |key, relay| behaviour(p, key, relay, &cfg2).map_err(|e| Box::new(e) as BoxErr))
339            .map_err(net_err)?
340            .with_swarm_config(idle)
341            .build()
342    } else {
343        let cfg2 = cfg.clone();
344        libp2p::SwarmBuilder::with_existing_identity(key)
345            .with_tokio()
346            .with_tcp(tcp::Config::default().nodelay(true), noise::Config::new, yamux::Config::default)
347            .map_err(net_err)?
348            .with_quic()
349            .with_dns()
350            .map_err(net_err)?
351            .with_relay_client(noise::Config::new, yamux::Config::default)
352            .map_err(net_err)?
353            .with_behaviour(move |key, relay| behaviour(p, key, relay, &cfg2).map_err(|e| Box::new(e) as BoxErr))
354            .map_err(net_err)?
355            .with_swarm_config(idle)
356            .build()
357    };
358    Ok(swarm)
359}
360
361impl<Q: Message, R: Message> Node<Q, R> {
362    /// Start a node speaking `profile`; returns once its listeners are up (or failed).
363    pub fn start(profile: &'static Profile, key: Keypair, cfg: NodeConfig, handler: Arc<dyn Handler<Q, R>>) -> Result<Self> {
364        if cfg.relay_server {
365            cfg.relay_limits.check()?;
366        }
367        let peer_id = key.public().to_peer_id();
368        let status = Arc::new(Mutex::new(Status {
369            peer_id: peer_id.to_string(),
370            nat: "unknown".into(),
371            relay_server: cfg.relay_server,
372            mdns: cfg.mdns && !cfg.memory,
373            started_at: crate::now_ms(),
374            ..Default::default()
375        }));
376        let (tx, rx) = mpsc::unbounded_channel();
377        let (ready_tx, ready_rx) = std::sync::mpsc::channel::<Result<()>>();
378        let st = status.clone();
379        let names_list = cfg.names_servers.clone();
380        let thread = std::thread::Builder::new()
381            .name(profile.thread.into())
382            .spawn(move || {
383                let rt = match tokio::runtime::Builder::new_multi_thread()
384                    .worker_threads(2)
385                    .enable_all()
386                    .thread_name(format!("{}-rt", profile.thread))
387                    .build()
388                {
389                    Ok(rt) => rt,
390                    Err(e) => {
391                        let _ = ready_tx.send(Err(net_err(e)));
392                        return;
393                    }
394                };
395                rt.block_on(async move {
396                    let swarm = match build_swarm(profile, key, &cfg) {
397                        Ok(mut s) => {
398                            for a in &cfg.external {
399                                s.add_external_address(a.clone());
400                            }
401                            s
402                        }
403                        Err(e) => {
404                            let _ = ready_tx.send(Err(e));
405                            return;
406                        }
407                    };
408                    let mut l = Loop::new(profile, swarm, cfg, handler, st);
409                    l.run(rx, ready_tx).await;
410                });
411                rt.shutdown_timeout(Duration::from_secs(2));
412            })
413            .map_err(net_err)?;
414        match ready_rx.recv_timeout(Duration::from_secs(20)) {
415            Ok(Ok(())) => Ok(Node { peer_id, profile, names_list, tx, status, thread: Some(thread) }),
416            Ok(Err(e)) => {
417                let _ = thread.join();
418                Err(e)
419            }
420            Err(_) => Err(NetError::Network("the node did not start in time".into())),
421        }
422    }
423
424    pub fn peer_id(&self) -> PeerId {
425        self.peer_id
426    }
427
428    pub fn status(&self) -> Status {
429        self.status.lock().map(|s| s.clone()).unwrap_or_default()
430    }
431
432    /// Addresses others can use to reach this node (with `/p2p/ID`).
433    pub fn reachable_addrs(&self) -> Vec<String> {
434        let s = self.status();
435        let mut v: Vec<String> = vec![];
436        for a in s.external.iter().chain(s.listening.iter()) {
437            // a circuit through a relay is only useful if the relay's own address is routable
438            if let Ok(m) = a.parse::<Multiaddr>()
439                && shareable(&m)
440                && (!m.iter().any(|p| p == Protocol::P2pCircuit) || routable(&m))
441            {
442                let full = if addr_peer(&m).is_some() { m } else { m.with(Protocol::P2p(self.peer_id)) };
443                let t = full.to_string();
444                if !v.contains(&t) {
445                    v.push(t);
446                }
447            }
448        }
449        v
450    }
451
452    fn call<T>(&self, make: impl FnOnce(oneshot::Sender<Result<T>>) -> Cmd<Q, R>, wait: Duration) -> Result<T> {
453        let (tx, rx) = oneshot::channel();
454        self.tx.send(make(tx)).map_err(|_| stopped())?;
455        let deadline = std::time::Instant::now() + wait;
456        let mut rx = rx;
457        loop {
458            match rx.try_recv() {
459                Ok(r) => return r,
460                Err(oneshot::error::TryRecvError::Closed) => return Err(stopped()),
461                Err(oneshot::error::TryRecvError::Empty) => {
462                    if std::time::Instant::now() > deadline {
463                        return Err(NetError::Network("timed out".into()));
464                    }
465                    std::thread::sleep(Duration::from_millis(5));
466                }
467            }
468        }
469    }
470
471    /// Send a request and wait for the answer. `addrs` are tried if not connected yet.
472    pub fn request(&self, peer: PeerId, addrs: &[Multiaddr], wire: Q) -> Result<R> {
473        let addrs = addrs.to_vec();
474        self.call(|reply| Cmd::Request { peer, addrs, wire, reply }, REQUEST_TIMEOUT + Duration::from_secs(5))
475    }
476
477    /// Ask a names server (a relay, usually) something; `addrs` are tried if not connected.
478    pub fn names_request(&self, server: PeerId, addrs: &[Multiaddr], req: NamesRequest) -> Result<NamesResponse> {
479        let addrs = addrs.to_vec();
480        self.call(|reply| Cmd::Names { peer: server, addrs, req, reply }, Duration::from_secs(30))
481    }
482
483    /// Register a record or withdrawal on a names server.
484    pub fn register(&self, server: PeerId, addrs: &[Multiaddr], entry: Entry) -> Result<()> {
485        match self.names_request(server, addrs, NamesRequest::Register(Box::new(entry)))? {
486            NamesResponse::Registered => Ok(()),
487            NamesResponse::Refused(why) => Err(NetError::Network(format!("the names server refused: {why}"))),
488            NamesResponse::Found(_) => Err(NetError::Network("unexpected answer from the names server".into())),
489        }
490    }
491
492    /// A user's entry on one names server.
493    pub fn discover(&self, server: PeerId, addrs: &[Multiaddr], user: &str) -> Result<Option<Entry>> {
494        match self.names_request(server, addrs, NamesRequest::Discover { user_id: user.into() })? {
495            NamesResponse::Found(e) => Ok(e.map(|e| *e)),
496            NamesResponse::Refused(why) => Err(NetError::Network(format!("the names server refused: {why}"))),
497            NamesResponse::Registered => Err(NetError::Network("unexpected answer from the names server".into())),
498        }
499    }
500
501    /// Put a record or withdrawal in the DHT (checked first, like every storing node does).
502    /// Records belong in the DHT only while the device hosts something public or private.
503    pub fn put_entry(&self, entry: Entry) -> Result<()> {
504        self.call(|reply| Cmd::PutEntry { entry, reply }, Duration::from_secs(40))
505    }
506
507    /// Every entry the DHT returns for a user (unchecked; see [`Profile::newest`]).
508    pub fn get_entries(&self, user: &str) -> Result<Vec<Entry>> {
509        let user = user.to_string();
510        self.call(|reply| Cmd::GetEntries { user, reply }, Duration::from_secs(40))
511    }
512
513    /// Where a peer is, by its key alone: what the DHT knows of it.
514    pub fn find_peer(&self, peer: PeerId) -> Result<Vec<Multiaddr>> {
515        self.call(|reply| Cmd::FindPeer { peer, reply }, Duration::from_secs(40))
516    }
517
518    /// Names servers to ask: the configured ones and the relays holding our reservation.
519    fn names_servers(&self) -> Vec<(PeerId, Vec<Multiaddr>)> {
520        let known = self.call(|reply| Cmd::KnownAddrs { reply }, Duration::from_secs(5)).unwrap_or_default();
521        let mut out: Vec<(PeerId, Vec<Multiaddr>)> = vec![];
522        let reserved: Vec<PeerId> = self.status().reservations.iter().filter_map(|r| r.parse().ok()).collect();
523        for (p, a) in known.into_iter() {
524            if reserved.contains(&p) || self.names_list.iter().any(|n| addr_peer(n) == Some(p)) {
525                match out.iter_mut().find(|(q, _)| *q == p) {
526                    Some((_, v)) => v.push(a),
527                    None => out.push((p, vec![a])),
528                }
529            }
530        }
531        for n in &self.names_list {
532            if let Some(p) = addr_peer(n)
533                && !out.iter().any(|(q, _)| *q == p)
534            {
535                out.push((p, vec![n.clone()]));
536            }
537        }
538        out
539    }
540
541    /// Register an entry with every names server this node uses (its relays and the
542    /// configured ones); returns how many took it.
543    pub fn register_everywhere(&self, entry: &Entry) -> usize {
544        self.names_servers().into_iter().filter(|(p, a)| self.register(*p, a, entry.clone()).is_ok()).count()
545    }
546
547    /// Find a user's freshest valid record: on this node's names servers, then in the DHT.
548    pub fn lookup(&self, user: &str) -> Lookup {
549        let mut found: Vec<Entry> = vec![];
550        for (p, a) in self.names_servers() {
551            if let Ok(Some(e)) = self.discover(p, &a, user) {
552                found.push(e);
553            }
554        }
555        if let Ok(v) = self.get_entries(user) {
556            found.extend(v);
557        }
558        self.profile.newest(user, &found, crate::now_ms())
559    }
560
561    /// Send a request to a peer known only by its key: when not connected, find its
562    /// addresses in the DHT and try circuits through this node's relays and names servers.
563    pub fn request_key(&self, peer: PeerId, wire: Q) -> Result<R> {
564        if self.status().connected.contains(&peer.to_string()) {
565            return self.request(peer, &[], wire);
566        }
567        let mut addrs = self.find_peer(peer).unwrap_or_default();
568        for (_, a) in self.names_servers() {
569            for base in a {
570                let base: Multiaddr = base.iter().take_while(|p| *p != Protocol::P2pCircuit).collect();
571                addrs.push(base.with(Protocol::P2pCircuit).with(Protocol::P2p(peer)));
572            }
573        }
574        self.request(peer, &addrs, wire)
575    }
576
577    /// Send a request to a user's node: look up its record and dial its addresses
578    /// (circuits and direct ones together). Returns the node that answered.
579    pub fn request_user(&self, user: &str, wire: Q) -> Result<(PeerId, R)> {
580        match self.lookup(user) {
581            Lookup::Found(r) => {
582                let peer: PeerId = r.cert.peer_id.parse().map_err(|_| NetError::invalid("peer record", "bad peer id"))?;
583                let addrs = crate::addr::parse_addrs(&r.addrs);
584                Ok((peer, self.request(peer, &addrs, wire)?))
585            }
586            Lookup::Withdrawn(_) => Err(NetError::Network(format!("{user} withdrew its record"))),
587            Lookup::NotFound => Err(NetError::Network(format!("no record for {user}"))),
588        }
589    }
590
591    /// Connect to an address; returns the peer there.
592    pub fn dial(&self, addr: Multiaddr) -> Result<PeerId> {
593        self.call(|reply| Cmd::Dial { addr, reply }, Duration::from_secs(30))
594    }
595
596    /// Listen on one more address; returns the address it got.
597    pub fn listen(&self, addr: Multiaddr) -> Result<Multiaddr> {
598        self.call(|reply| Cmd::Listen { addr, reply }, Duration::from_secs(20))
599    }
600
601    /// Reserve a slot on a relay (`…/p2p/RELAY`).
602    pub fn add_relay(&self, addr: Multiaddr) -> Result<()> {
603        self.tx.send(Cmd::AddRelay { addr }).map_err(|_| stopped())
604    }
605
606    /// Whether the node thread still runs.
607    pub fn running(&self) -> bool {
608        self.thread.as_ref().is_some_and(|t| !t.is_finished())
609    }
610
611    pub fn stop(mut self) {
612        self.shutdown();
613    }
614
615    /// Ask the node to stop without waiting (others may still hold it).
616    pub fn halt(&self) {
617        let _ = self.tx.send(Cmd::Stop);
618    }
619
620    fn shutdown(&mut self) {
621        let _ = self.tx.send(Cmd::Stop);
622        if let Some(t) = self.thread.take() {
623            let _ = t.join();
624        }
625    }
626}
627
628impl<Q: Message, R: Message> Drop for Node<Q, R> {
629    fn drop(&mut self) {
630        self.shutdown();
631    }
632}
633
634/// A DHT lookup collecting entries, and who waits for them.
635type Gathering = (Vec<Entry>, oneshot::Sender<Result<Vec<Entry>>>);
636/// A DHT search for one peer's addresses.
637type Finding = (PeerId, oneshot::Sender<Result<Vec<Multiaddr>>>);
638
639struct Loop<Q: Message, R: Message> {
640    profile: &'static Profile,
641    swarm: Swarm<Behaviour<Q, R>>,
642    cfg: NodeConfig,
643    handler: Arc<dyn Handler<Q, R>>,
644    status: Arc<Mutex<Status>>,
645    pending: HashMap<OutboundRequestId, oneshot::Sender<Result<R>>>,
646    dials: Vec<(Multiaddr, Option<PeerId>, oneshot::Sender<Result<PeerId>>)>,
647    listens: HashMap<libp2p::core::transport::ListenerId, oneshot::Sender<Result<Multiaddr>>>,
648    relays: HashSet<PeerId>,
649    relay_listening: HashSet<PeerId>,
650    /// Each relay's circuit listener and when it was asked for (to retry a stuck one).
651    relay_listeners: HashMap<PeerId, (Multiaddr, libp2p::core::transport::ListenerId, std::time::Instant)>,
652    resp_tx: mpsc::UnboundedSender<(request_response::ResponseChannel<R>, R)>,
653    resp_rx: mpsc::UnboundedReceiver<(request_response::ResponseChannel<R>, R)>,
654    names_pending: HashMap<OutboundRequestId, oneshot::Sender<Result<NamesResponse>>>,
655    names_tx: mpsc::UnboundedSender<(request_response::ResponseChannel<NamesResponse>, NamesResponse)>,
656    names_rx: mpsc::UnboundedReceiver<(request_response::ResponseChannel<NamesResponse>, NamesResponse)>,
657    puts: HashMap<kad::QueryId, oneshot::Sender<Result<()>>>,
658    gets: HashMap<kad::QueryId, Gathering>,
659    finds: HashMap<kad::QueryId, Finding>,
660    addr_book: HashMap<PeerId, Vec<Multiaddr>>,
661}
662
663impl<Q: Message, R: Message> Loop<Q, R> {
664    fn new(
665        profile: &'static Profile,
666        swarm: Swarm<Behaviour<Q, R>>,
667        cfg: NodeConfig,
668        handler: Arc<dyn Handler<Q, R>>,
669        status: Arc<Mutex<Status>>,
670    ) -> Self {
671        let (resp_tx, resp_rx) = mpsc::unbounded_channel();
672        let (names_tx, names_rx) = mpsc::unbounded_channel();
673        Loop {
674            profile,
675            swarm,
676            cfg,
677            handler,
678            status,
679            pending: HashMap::new(),
680            dials: vec![],
681            listens: HashMap::new(),
682            relays: HashSet::new(),
683            relay_listening: HashSet::new(),
684            relay_listeners: HashMap::new(),
685            resp_tx,
686            resp_rx,
687            names_pending: HashMap::new(),
688            names_tx,
689            names_rx,
690            puts: HashMap::new(),
691            gets: HashMap::new(),
692            finds: HashMap::new(),
693            addr_book: HashMap::new(),
694        }
695    }
696
697    fn update(&self, f: impl FnOnce(&mut Status)) {
698        if let Ok(mut s) = self.status.lock() {
699            f(&mut s);
700        }
701    }
702
703    fn refresh(&self) {
704        let connected: Vec<String> = self.swarm.connected_peers().map(|p| p.to_string()).collect();
705        let listening: Vec<String> = self.swarm.listeners().map(|a| a.to_string()).collect();
706        let external: Vec<String> = self.swarm.external_addresses().map(|a| a.to_string()).collect();
707        self.update(|s| {
708            s.connected = connected;
709            s.listening = listening;
710            s.external = external;
711        });
712    }
713
714    fn seen(&self, s: Sighting) {
715        let h = self.handler.clone();
716        tokio::task::spawn_blocking(move || h.seen(s));
717    }
718
719    async fn run(&mut self, mut rx: mpsc::UnboundedReceiver<Cmd<Q, R>>, ready: std::sync::mpsc::Sender<Result<()>>) {
720        // listen
721        let mut waiting = 0usize;
722        let mut ready = Some(ready);
723        for a in self.cfg.listen.clone() {
724            match self.swarm.listen_on(a.clone()) {
725                Ok(_) => waiting += 1,
726                Err(e) => tracing::warn!("network: cannot listen on {a}: {e}"),
727            }
728        }
729        if !self.cfg.listen.is_empty() && waiting == 0 {
730            if let Some(r) = ready.take() {
731                let _ = r.send(Err(NetError::Network("could not listen on any address".into())));
732            }
733            return;
734        }
735        // relays first: a reservation dials its relay, and a second dial to the same peer
736        // already under way would make the relay client skip its own
737        for r in self.cfg.relays.clone() {
738            self.add_relay(r);
739        }
740        for b in self.cfg.bootstrap.clone() {
741            if let Some(p) = addr_peer(&b) {
742                self.swarm.behaviour_mut().kad.add_address(&p, b.clone());
743                if !self.relay_listening.contains(&p) {
744                    let _ = self.swarm.dial(b);
745                }
746            }
747        }
748        if self.cfg.relay_server {
749            self.swarm.behaviour_mut().kad.set_mode(Some(kad::Mode::Server));
750        }
751        if waiting == 0
752            && let Some(r) = ready.take()
753        {
754            let _ = r.send(Ok(()));
755        }
756        let ready_deadline = tokio::time::sleep(Duration::from_secs(5));
757        tokio::pin!(ready_deadline);
758        let mut tick = tokio::time::interval(Duration::from_secs(60));
759        let mut check = tokio::time::interval(Duration::from_secs(1));
760        loop {
761            tokio::select! {
762                cmd = rx.recv() => match cmd {
763                    None | Some(Cmd::Stop) => break,
764                    Some(c) => self.command(c),
765                },
766                Some((ch, resp)) = self.resp_rx.recv() => {
767                    let _ = self.swarm.behaviour_mut().rr.send_response(ch, resp);
768                }
769                Some((ch, resp)) = self.names_rx.recv() => {
770                    let _ = self.swarm.behaviour_mut().names.send_response(ch, resp);
771                }
772                ev = self.swarm.select_next_some() => {
773                    let new_listen = matches!(ev, SwarmEvent::NewListenAddr { .. });
774                    self.event(ev);
775                    if new_listen && waiting > 0 {
776                        waiting -= 1;
777                        if waiting == 0 && let Some(r) = ready.take() {
778                            let _ = r.send(Ok(()));
779                        }
780                    }
781                }
782                _ = &mut ready_deadline, if ready.is_some() => {
783                    if let Some(r) = ready.take() {
784                        let _ = r.send(Ok(()));
785                    }
786                }
787                _ = tick.tick() => self.periodic(),
788                _ = check.tick() => self.retry_stuck_reservations(),
789            }
790        }
791        for (_, tx) in self.pending.drain() {
792            let _ = tx.send(Err(NetError::Network("the node stopped".into())));
793        }
794        for (_, tx) in self.names_pending.drain() {
795            let _ = tx.send(Err(NetError::Network("the node stopped".into())));
796        }
797    }
798
799    fn periodic(&mut self) {
800        if let Some(n) = &self.cfg.names_server {
801            n.0.prune(crate::now_ms());
802        }
803        let k = &mut self.swarm.behaviour_mut().kad;
804        // With an empty routing table there is no one to bootstrap from; asking
805        // anyway only makes libp2p warn "No known peers" every tick.
806        if routing_empty(k) {
807            return;
808        }
809        if k.bootstrap().is_ok() {
810            k.get_providers(kad::RecordKey::new(&self.profile.relay_key));
811            if self.cfg.relay_server {
812                let _ = k.start_providing(kad::RecordKey::new(&self.profile.relay_key));
813            }
814        }
815    }
816
817    fn add_relay(&mut self, addr: Multiaddr) {
818        let Some(peer) = addr_peer(&addr) else {
819            tracing::warn!("network: relay address {addr} has no /p2p/ peer id");
820            return;
821        };
822        if peer == *self.swarm.local_peer_id() || !self.relay_listening.insert(peer) {
823            return;
824        }
825        self.relays.insert(peer);
826        self.swarm.add_peer_address(peer, addr.clone());
827        let circuit = addr.with(Protocol::P2pCircuit);
828        match self.swarm.listen_on(circuit.clone()) {
829            Ok(id) => {
830                self.relay_listeners.insert(peer, (circuit, id, std::time::Instant::now()));
831            }
832            Err(e) => {
833                tracing::warn!("network: cannot listen through relay {circuit}: {e}");
834                self.relay_listening.remove(&peer);
835            }
836        }
837    }
838
839    /// A reservation asked for while another dial to the relay was under way is never sent
840    /// (libp2p's relay client skips its own dial then); once connected, ask again.
841    fn retry_stuck_reservations(&mut self) {
842        let reserved = self.status.lock().map(|s| s.reservations.clone()).unwrap_or_default();
843        let stuck: Vec<PeerId> = self
844            .relay_listeners
845            .iter()
846            .filter(|(p, (_, _, at))| at.elapsed() > Duration::from_secs(3) && !reserved.contains(&p.to_string()) && self.swarm.is_connected(p))
847            .map(|(p, _)| *p)
848            .collect();
849        for p in stuck {
850            if let Some((circuit, id, _)) = self.relay_listeners.remove(&p) {
851                tracing::debug!("network: asking relay {p} for a reservation again");
852                self.swarm.remove_listener(id);
853                match self.swarm.listen_on(circuit.clone()) {
854                    Ok(id) => {
855                        self.relay_listeners.insert(p, (circuit, id, std::time::Instant::now()));
856                    }
857                    Err(e) => tracing::warn!("network: cannot listen through relay {circuit}: {e}"),
858                }
859            }
860        }
861    }
862
863    fn command(&mut self, c: Cmd<Q, R>) {
864        match c {
865            Cmd::Request { peer, addrs, wire, reply } => {
866                for a in addrs {
867                    self.swarm.add_peer_address(peer, a);
868                }
869                let id = self.swarm.behaviour_mut().rr.send_request(&peer, wire);
870                self.pending.insert(id, reply);
871            }
872            Cmd::Names { peer, addrs, req, reply } => {
873                for a in addrs {
874                    self.swarm.add_peer_address(peer, a);
875                }
876                let id = self.swarm.behaviour_mut().names.send_request(&peer, req);
877                self.names_pending.insert(id, reply);
878            }
879            Cmd::PutEntry { entry, reply } => {
880                let key = kad::RecordKey::new(&self.profile.record_key(entry.user_id()));
881                let existing = self.swarm.behaviour_mut().kad.store_mut().get(&key).and_then(|r| Entry::decode(&r.value).ok());
882                if let Err(e) = self.profile.accept(existing.as_ref(), &entry, crate::now_ms()) {
883                    let _ = reply.send(Err(e));
884                    return;
885                }
886                let rec = record_of(key, &entry);
887                match self.swarm.behaviour_mut().kad.put_record(rec, kad::Quorum::One) {
888                    Ok(q) => {
889                        self.puts.insert(q, reply);
890                    }
891                    Err(e) => {
892                        let _ = reply.send(Err(net_err(e)));
893                    }
894                }
895            }
896            Cmd::GetEntries { user, reply } => {
897                let q = self.swarm.behaviour_mut().kad.get_record(kad::RecordKey::new(&self.profile.record_key(&user)));
898                self.gets.insert(q, (vec![], reply));
899            }
900            Cmd::FindPeer { peer, reply } => {
901                let q = self.swarm.behaviour_mut().kad.get_closest_peers(peer);
902                self.finds.insert(q, (peer, reply));
903            }
904            Cmd::KnownAddrs { reply } => {
905                let v = self.addr_book.iter().flat_map(|(p, a)| a.iter().map(|a| (*p, a.clone()))).collect();
906                let _ = reply.send(Ok(v));
907            }
908            Cmd::Dial { addr, reply } => {
909                let peer = addr_peer(&addr);
910                match self.swarm.dial(addr.clone()) {
911                    Ok(()) => self.dials.push((addr, peer, reply)),
912                    Err(e) => {
913                        let _ = reply.send(Err(net_err(e)));
914                    }
915                }
916            }
917            Cmd::Listen { addr, reply } => match self.swarm.listen_on(addr) {
918                Ok(id) => {
919                    self.listens.insert(id, reply);
920                }
921                Err(e) => {
922                    let _ = reply.send(Err(net_err(e)));
923                }
924            },
925            Cmd::AddRelay { addr } => self.add_relay(addr),
926            Cmd::Stop => {}
927        }
928    }
929
930    fn event(&mut self, ev: SwarmEvent<BehaviourEvent<Q, R>>) {
931        match ev {
932            SwarmEvent::NewListenAddr { listener_id, address } => {
933                tracing::info!("network: listening on {address}");
934                // a relay announces where it listens: reservations carry those addresses
935                if self.cfg.relay_server && self.cfg.external.is_empty() && routable(&address) && !address.iter().any(|p| p == Protocol::P2pCircuit) {
936                    self.swarm.add_external_address(address.clone());
937                }
938                if let Some(tx) = self.listens.remove(&listener_id) {
939                    let _ = tx.send(Ok(address));
940                }
941                self.refresh();
942            }
943            SwarmEvent::ListenerClosed { listener_id, .. } => {
944                if let Some(tx) = self.listens.remove(&listener_id) {
945                    let _ = tx.send(Err(NetError::Network("listener closed".into())));
946                }
947                self.refresh();
948            }
949            SwarmEvent::ExternalAddrConfirmed { address } => {
950                // a relay keeps to its configured addresses (or at least routable ones)
951                if self.cfg.relay_server && (!routable(&address) || (!self.cfg.external.is_empty() && !self.cfg.external.contains(&address))) {
952                    self.swarm.remove_external_address(&address);
953                }
954                self.refresh()
955            }
956            SwarmEvent::ExternalAddrExpired { .. } | SwarmEvent::ExpiredListenAddr { .. } => self.refresh(),
957            SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } => {
958                let addr = endpoint.get_remote_address().clone();
959                if endpoint.is_dialer() {
960                    let a = if addr_peer(&addr) == Some(peer_id) { addr.clone() } else { addr.clone().with(Protocol::P2p(peer_id)) };
961                    let book = self.addr_book.entry(peer_id).or_default();
962                    if !book.contains(&a) {
963                        book.push(a);
964                    }
965                }
966                let mut i = 0;
967                while i < self.dials.len() {
968                    let (a, p, _) = &self.dials[i];
969                    if *a == addr || p.is_some_and(|p| p == peer_id) {
970                        let (_, _, tx) = self.dials.remove(i);
971                        let _ = tx.send(Ok(peer_id));
972                    } else {
973                        i += 1;
974                    }
975                }
976                self.refresh();
977            }
978            SwarmEvent::ConnectionClosed { .. } => self.refresh(),
979            SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
980                let msg = error.to_string();
981                let mut i = 0;
982                while i < self.dials.len() {
983                    if peer_id.is_some() && self.dials[i].1 == peer_id {
984                        let (_, _, tx) = self.dials.remove(i);
985                        let _ = tx.send(Err(net_err(&msg)));
986                    } else {
987                        i += 1;
988                    }
989                }
990                if peer_id.is_none() && !self.dials.is_empty() {
991                    // an address without /p2p/: the oldest dial failed
992                    let (_, _, tx) = self.dials.remove(0);
993                    let _ = tx.send(Err(net_err(&msg)));
994                }
995            }
996            SwarmEvent::Behaviour(b) => self.behaviour(b),
997            _ => {}
998        }
999    }
1000
1001    fn behaviour(&mut self, ev: BehaviourEvent<Q, R>) {
1002        match ev {
1003            BehaviourEvent::Rr(request_response::Event::Message { peer, message, .. }) => match message {
1004                request_response::Message::Request { request, channel, .. } => {
1005                    let h = self.handler.clone();
1006                    let tx = self.resp_tx.clone();
1007                    tokio::task::spawn_blocking(move || {
1008                        let resp = h.request(&peer, request);
1009                        let _ = tx.send((channel, resp));
1010                    });
1011                }
1012                request_response::Message::Response { request_id, response } => {
1013                    if let Some(tx) = self.pending.remove(&request_id) {
1014                        let _ = tx.send(Ok(response));
1015                    }
1016                }
1017            },
1018            BehaviourEvent::Rr(request_response::Event::OutboundFailure { request_id, error, peer, .. }) => {
1019                if let Some(tx) = self.pending.remove(&request_id) {
1020                    let _ = tx.send(Err(NetError::Network(format!("{} could not be reached ({error})", crate::short_peer_id(&peer)))));
1021                }
1022            }
1023            BehaviourEvent::Rr(request_response::Event::InboundFailure { peer, error, .. }) => {
1024                tracing::debug!("network: inbound request from {peer} failed: {error}");
1025            }
1026            BehaviourEvent::Names(request_response::Event::Message { peer, message, .. }) => match message {
1027                request_response::Message::Request { request, channel, .. } => {
1028                    let Some(store) = self.cfg.names_server.clone() else { return };
1029                    let tx = self.names_tx.clone();
1030                    let profile = self.profile;
1031                    tokio::task::spawn_blocking(move || {
1032                        let resp = crate::names::serve(profile, &*store.0, &peer, request, crate::now_ms());
1033                        let _ = tx.send((channel, resp));
1034                    });
1035                }
1036                request_response::Message::Response { request_id, response } => {
1037                    if let Some(tx) = self.names_pending.remove(&request_id) {
1038                        let _ = tx.send(Ok(response));
1039                    }
1040                }
1041            },
1042            BehaviourEvent::Names(request_response::Event::OutboundFailure { request_id, error, peer, .. }) => {
1043                if let Some(tx) = self.names_pending.remove(&request_id) {
1044                    let _ = tx.send(Err(NetError::Network(format!("names server {} could not be reached ({error})", crate::short_peer_id(&peer)))));
1045                }
1046            }
1047            BehaviourEvent::Kad(kad::Event::InboundRequest { request }) => match request {
1048                kad::InboundRequest::PutRecord { record: Some(rec), source, .. } => self.inbound_put(rec, source),
1049                kad::InboundRequest::AddProvider { record: Some(rec) } => {
1050                    // the only provider key nodes announce is the relay key
1051                    if rec.key.as_ref() == self.profile.relay_key.as_bytes() {
1052                        let _ = self.swarm.behaviour_mut().kad.store_mut().add_provider(rec);
1053                    }
1054                }
1055                _ => {}
1056            },
1057            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::PutRecord(r), .. }) => {
1058                if let Some(tx) = self.puts.remove(&id) {
1059                    let _ = tx.send(r.map(|_| ()).map_err(net_err));
1060                }
1061            }
1062            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::GetRecord(r), step, .. }) => {
1063                let done = match r {
1064                    Ok(kad::GetRecordOk::FoundRecord(pr)) => {
1065                        if let Some((v, _)) = self.gets.get_mut(&id)
1066                            && let Ok(e) = Entry::decode(&pr.record.value)
1067                        {
1068                            v.push(e);
1069                        }
1070                        step.last
1071                    }
1072                    _ => true,
1073                };
1074                if done && let Some((v, tx)) = self.gets.remove(&id) {
1075                    let _ = tx.send(Ok(v));
1076                    if let Some(mut q) = self.swarm.behaviour_mut().kad.query_mut(&id) {
1077                        q.finish();
1078                    }
1079                }
1080            }
1081            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::GetClosestPeers(r), .. }) => {
1082                if let Some((peer, tx)) = self.finds.remove(&id) {
1083                    let addrs = match r {
1084                        Ok(ok) => ok.peers,
1085                        Err(kad::GetClosestPeersError::Timeout { peers, .. }) => peers,
1086                    }
1087                    .into_iter()
1088                    .filter(|i| i.peer_id == peer)
1089                    .flat_map(|i| i.addrs)
1090                    .map(|a| if addr_peer(&a) == Some(peer) { a } else { a.with(Protocol::P2p(peer)) })
1091                    .collect();
1092                    let _ = tx.send(Ok(addrs));
1093                }
1094            }
1095            BehaviourEvent::Mdns(mdns::Event::Discovered(list)) => {
1096                let mut by_peer: HashMap<PeerId, Vec<Multiaddr>> = HashMap::new();
1097                for (p, a) in list {
1098                    by_peer.entry(p).or_default().push(a);
1099                }
1100                for (p, addrs) in by_peer {
1101                    for a in &addrs {
1102                        self.swarm.add_peer_address(p, a.clone());
1103                        self.swarm.behaviour_mut().kad.add_address(&p, a.clone());
1104                    }
1105                    if !self.swarm.is_connected(&p) {
1106                        let _ = self.swarm.dial(p);
1107                    }
1108                    self.seen(Sighting { peer: p, addrs: addrs.iter().map(|a| a.to_string()).collect(), caps: None, agent: None, source: "mdns" });
1109                }
1110            }
1111            BehaviourEvent::Identify(identify::Event::Received { peer_id, info, .. }) => {
1112                let protos: Vec<String> = info.protocols.iter().map(|p| p.to_string()).collect();
1113                let mut caps = vec![];
1114                if protos.iter().any(|p| p == self.profile.protocol) {
1115                    caps.push(self.profile.name.to_string());
1116                }
1117                if protos.iter().any(|p| *p == relay::HOP_PROTOCOL_NAME.to_string()) {
1118                    caps.push("relay".to_string());
1119                }
1120                if protos.iter().any(|p| p == self.profile.kad) {
1121                    caps.push("kad".to_string());
1122                    for a in &info.listen_addrs {
1123                        self.swarm.behaviour_mut().kad.add_address(&peer_id, a.clone());
1124                    }
1125                }
1126                let is_relay = caps.iter().any(|c| c == "relay");
1127                let addrs: Vec<String> = info.listen_addrs.iter().filter(|a| shareable(a)).map(|a| a.to_string()).collect();
1128                self.seen(Sighting { peer: peer_id, addrs, caps: Some(caps), agent: Some(info.agent_version), source: "identify" });
1129                // behind NAT: use relays we meet
1130                let private = self.status.lock().map(|s| s.nat == "private").unwrap_or(false);
1131                if is_relay
1132                    && private
1133                    && self.relay_listening.is_empty()
1134                    && let Some(a) = info.listen_addrs.iter().filter(|a| shareable(a)).min_by_key(|a| reach_rank(a))
1135                {
1136                    self.add_relay(a.clone().with(Protocol::P2p(peer_id)));
1137                }
1138            }
1139            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed {
1140                result: kad::QueryResult::GetProviders(Ok(kad::GetProvidersOk::FoundProviders { providers, .. })),
1141                ..
1142            }) => {
1143                for p in providers {
1144                    if p != *self.swarm.local_peer_id() {
1145                        self.seen(Sighting { peer: p, addrs: vec![], caps: Some(vec!["relay".into(), "kad".into()]), agent: None, source: "kad" });
1146                    }
1147                }
1148            }
1149            BehaviourEvent::Autonat(autonat::Event::StatusChanged { new, .. }) => {
1150                let nat = match new {
1151                    autonat::NatStatus::Public(_) => "public",
1152                    autonat::NatStatus::Private => "private",
1153                    autonat::NatStatus::Unknown => "unknown",
1154                };
1155                self.update(|s| s.nat = nat.into());
1156                if nat == "private" {
1157                    let relays: Vec<PeerId> = self.relays.iter().copied().collect();
1158                    tracing::info!("network: behind NAT; {} relay(s) known", relays.len());
1159                }
1160            }
1161            BehaviourEvent::RelayClient(relay::client::Event::ReservationReqAccepted { relay_peer_id, .. }) => {
1162                tracing::info!("network: reservation on relay {relay_peer_id}");
1163                self.update(|s| {
1164                    let r = relay_peer_id.to_string();
1165                    if !s.reservations.contains(&r) {
1166                        s.reservations.push(r);
1167                    }
1168                });
1169            }
1170            BehaviourEvent::Dcutr(e) => {
1171                tracing::debug!("network: hole punch with {}: {}", e.remote_peer_id, if e.result.is_ok() { "direct" } else { "failed" });
1172            }
1173            _ => {}
1174        }
1175    }
1176}
1177
1178/// A Kademlia record holding a names entry, expiring with it.
1179fn record_of(key: kad::RecordKey, entry: &Entry) -> kad::Record {
1180    let mut rec = kad::Record::new(key, entry.encode());
1181    let left = (entry.expires() - crate::now_ms()).max(0) as u64;
1182    rec.expires = Some(std::time::Instant::now() + Duration::from_millis(left));
1183    rec
1184}
1185
1186impl<Q: Message, R: Message> Loop<Q, R> {
1187    /// A peer put a record: keep it only if it is a valid names entry under its own key
1188    /// that supersedes what we hold (see [`Profile::accept`]).
1189    fn inbound_put(&mut self, rec: kad::Record, source: PeerId) {
1190        let Ok(entry) = Entry::decode(&rec.value) else {
1191            tracing::debug!("network: refused a DHT record from {source}: not a names entry");
1192            return;
1193        };
1194        let key = kad::RecordKey::new(&self.profile.record_key(entry.user_id()));
1195        if rec.key != key {
1196            tracing::debug!("network: refused a DHT record from {source}: wrong key");
1197            return;
1198        }
1199        let store = self.swarm.behaviour_mut().kad.store_mut();
1200        let existing = store.get(&key).and_then(|r| Entry::decode(&r.value).ok());
1201        match self.profile.accept(existing.as_ref(), &entry, crate::now_ms()) {
1202            Ok(()) => {
1203                let mut r = record_of(key, &entry);
1204                r.publisher = rec.publisher;
1205                let _ = store.put(r);
1206            }
1207            Err(e) => tracing::debug!("network: refused a DHT record from {source}: {e}"),
1208        }
1209    }
1210}
1211
1212/// True when the Kademlia routing table holds no peers yet.
1213fn routing_empty<S: kad::store::RecordStore + Send + 'static>(k: &mut kad::Behaviour<S>) -> bool {
1214    k.kbuckets().all(|b| b.num_entries() == 0)
1215}