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, circuit_relay, reach_rank, routable, shareable};
11use crate::error::{NetError, Result};
12use crate::events::{Event, Events, Hub, StatusWatch};
13use crate::names::{Entry, Lookup, Names, NamesRequest, NamesResponse};
14use crate::profile::Profile;
15use crate::stream::transport::Control;
16use crate::stream::{Incoming, Limits, Path, Stream};
17use crate::wss::WssTls;
18use futures::StreamExt;
19use libp2p::core::muxing::StreamMuxerBox;
20use libp2p::core::transport::{Boxed, MemoryTransport, Transport};
21use libp2p::core::upgrade::Version;
22use libp2p::identity::Keypair;
23use libp2p::kad::{self, store::MemoryStore, store::RecordStore};
24use libp2p::multiaddr::Protocol;
25use libp2p::request_response::{self, OutboundRequestId, ProtocolSupport};
26use libp2p::swarm::behaviour::toggle::Toggle;
27use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
28use libp2p::{Multiaddr, PeerId, StreamProtocol, Swarm, autonat, dcutr, identify, mdns, noise, ping, relay, yamux};
29use serde::de::DeserializeOwned;
30use serde::{Deserialize, Serialize};
31use std::collections::{HashMap, HashSet};
32use std::sync::{Arc, Mutex};
33use std::time::Duration;
34use tokio::sync::{mpsc, oneshot};
35
36/// Request-response defaults ([`NodeConfig::request_max`] and the rest, when left at zero).
37pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
38pub const REQUEST_MAX: u64 = 8 * 1024 * 1024;
39pub const RESPONSE_MAX: u64 = 256 * 1024 * 1024;
40
41/// How a node runs.
42#[derive(Debug, Clone, Default)]
43pub struct NodeConfig {
44    /// Addresses to listen on (none: only dial out).
45    pub listen: Vec<Multiaddr>,
46    /// Find and announce peers on the local network.
47    pub mdns: bool,
48    /// Relay traffic for other nodes (and say so).
49    pub relay_server: bool,
50    /// Kademlia bootstrap nodes (`/ip4/…/tcp/…/p2p/…`).
51    pub bootstrap: Vec<Multiaddr>,
52    /// Relays to reserve a slot on (`/…/p2p/RELAY`); reachable through them behind NAT.
53    pub relays: Vec<Multiaddr>,
54    /// The in-memory transport (tests) instead of TCP and QUIC.
55    pub memory: bool,
56    /// This node's label (in identify; empty: none).
57    pub label: String,
58    /// Addresses others reach us at from outside (a DNS name or public IP forwarded to
59    /// this node), announced and put in invites before the local ones.
60    pub external: Vec<Multiaddr>,
61    /// The application's version, in the identify agent (`NAME/VERSION`); empty: this
62    /// crate's version.
63    pub version: String,
64    /// What the relay server (`relay_server`) allows; the default is libp2p's.
65    pub relay_limits: RelayLimits,
66    /// Serve the names protocol (rendezvous for signed peer records) from this store.
67    pub names_server: Option<Names>,
68    /// Names servers to register with and look users up on (`…/p2p/ID`), besides the
69    /// relays this node holds a reservation on.
70    pub names_servers: Vec<Multiaddr>,
71    /// Largest request on the profile's request-response protocol (0: [`REQUEST_MAX`]).
72    /// Bulk data belongs on a [stream](crate::stream) instead.
73    pub request_max: u64,
74    /// Largest response (0: [`RESPONSE_MAX`]).
75    pub response_max: u64,
76    /// How long a request waits for its response (zero: [`REQUEST_TIMEOUT`]).
77    pub request_timeout: Duration,
78    /// WebSocket over TLS: the certificate to listen with (put `/…/tcp/443/tls/ws` in
79    /// `listen`) and extra roots to trust when dialing. Without it the node still dials WSS.
80    pub wss: Option<WssTls>,
81    /// DNS servers for `/dns` and `/dnsaddr` addresses (empty: the system's, or public
82    /// resolvers where the system has none to read, as on Android).
83    pub dns_servers: Vec<std::net::SocketAddr>,
84}
85
86impl NodeConfig {
87    fn req_max(&self) -> u64 {
88        if self.request_max == 0 { REQUEST_MAX } else { self.request_max }
89    }
90
91    fn resp_max(&self) -> u64 {
92        if self.response_max == 0 { RESPONSE_MAX } else { self.response_max }
93    }
94
95    fn req_timeout(&self) -> Duration {
96        if self.request_timeout.is_zero() { REQUEST_TIMEOUT } else { self.request_timeout }
97    }
98}
99
100/// Limits of the relay server: how many reservations and circuits it holds, and how long
101/// and how much one circuit may carry. [`RelayLimits::default`] keeps libp2p's defaults
102/// (circuits for hole punching and small requests: 2 minutes, 128 KiB);
103/// [`RelayLimits::unlimited`] caps nothing, so circuits carry data.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub struct RelayLimits {
106    /// Reservations held at once, and per peer.
107    pub max_reservations: usize,
108    pub max_reservations_per_peer: usize,
109    /// How long a reservation lasts before the client renews it.
110    pub reservation_duration: Duration,
111    /// Circuits open at once, and per peer.
112    pub max_circuits: usize,
113    pub max_circuits_per_peer: usize,
114    /// How long one circuit may stay open (at most `u32::MAX` seconds).
115    pub max_circuit_duration: Duration,
116    /// How many bytes one circuit may carry in each direction; 0: no limit.
117    pub max_circuit_bytes: u64,
118    /// libp2p's rate limiters on new reservations and circuits (per peer and per IP).
119    pub rate_limits: bool,
120}
121
122impl Default for RelayLimits {
123    /// libp2p's defaults.
124    fn default() -> Self {
125        let c = relay::Config::default();
126        RelayLimits {
127            max_reservations: c.max_reservations,
128            max_reservations_per_peer: c.max_reservations_per_peer,
129            reservation_duration: c.reservation_duration,
130            max_circuits: c.max_circuits,
131            max_circuits_per_peer: c.max_circuits_per_peer,
132            max_circuit_duration: c.max_circuit_duration,
133            max_circuit_bytes: c.max_circuit_bytes,
134            rate_limits: true,
135        }
136    }
137}
138
139impl RelayLimits {
140    /// Longest circuit duration libp2p accepts.
141    pub const MAX_CIRCUIT_DURATION: Duration = Duration::from_secs(u32::MAX as u64);
142
143    /// No caps: as many reservations and circuits as clients ask for, no byte limit, the
144    /// longest circuit libp2p accepts, and no rate limiters. Reservations still last an hour
145    /// before the client renews them.
146    pub fn unlimited() -> Self {
147        RelayLimits {
148            max_reservations: usize::MAX,
149            max_reservations_per_peer: usize::MAX,
150            max_circuits: usize::MAX,
151            max_circuits_per_peer: usize::MAX,
152            max_circuit_duration: Self::MAX_CIRCUIT_DURATION,
153            max_circuit_bytes: 0,
154            rate_limits: false,
155            ..Self::default()
156        }
157    }
158
159    /// Whether nothing is capped (see [`RelayLimits::unlimited`]).
160    pub fn is_unlimited(&self) -> bool {
161        *self == Self::unlimited()
162    }
163
164    /// Refuse limits libp2p cannot apply (a zero count or duration, or a circuit duration
165    /// over `u32::MAX` seconds).
166    pub fn check(&self) -> Result<()> {
167        if self.max_reservations == 0 || self.max_reservations_per_peer == 0 || self.max_circuits == 0 || self.max_circuits_per_peer == 0 {
168            return Err(NetError::invalid("relay limits", "reservation and circuit counts must be at least 1"));
169        }
170        if self.reservation_duration.is_zero() || self.max_circuit_duration.is_zero() {
171            return Err(NetError::invalid("relay limits", "durations must be longer than zero"));
172        }
173        if self.max_circuit_duration > Self::MAX_CIRCUIT_DURATION {
174            return Err(NetError::invalid("relay limits", "a circuit may last at most u32::MAX seconds"));
175        }
176        Ok(())
177    }
178
179    fn config(&self) -> relay::Config {
180        let mut c = relay::Config {
181            max_reservations: self.max_reservations,
182            max_reservations_per_peer: self.max_reservations_per_peer,
183            reservation_duration: self.reservation_duration,
184            max_circuits: self.max_circuits,
185            max_circuits_per_peer: self.max_circuits_per_peer,
186            max_circuit_duration: self.max_circuit_duration,
187            max_circuit_bytes: self.max_circuit_bytes,
188            ..relay::Config::default()
189        };
190        if !self.rate_limits {
191            c.reservation_rate_limiters.clear();
192            c.circuit_src_rate_limiters.clear();
193        }
194        c
195    }
196}
197
198impl NodeConfig {
199    /// TCP and QUIC on every interface, IPv4 and IPv6, on `port`.
200    pub fn default_listen(port: u16) -> Vec<Multiaddr> {
201        [
202            format!("/ip4/0.0.0.0/tcp/{port}"),
203            format!("/ip4/0.0.0.0/udp/{port}/quic-v1"),
204            format!("/ip6/::/tcp/{port}"),
205            format!("/ip6/::/udp/{port}/quic-v1"),
206        ]
207        .iter()
208        .filter_map(|a| a.parse().ok())
209        .collect()
210    }
211}
212
213/// A peer seen by the node, for the handler to remember.
214#[derive(Debug, Clone)]
215pub struct Sighting {
216    pub peer: PeerId,
217    pub addrs: Vec<String>,
218    pub caps: Option<Vec<String>>,
219    pub agent: Option<String>,
220    pub source: &'static str,
221}
222
223/// What a node carries: the application's request and response types (sent as CBOR).
224pub trait Message: Serialize + DeserializeOwned + std::fmt::Debug + Clone + Send + 'static {}
225impl<T: Serialize + DeserializeOwned + std::fmt::Debug + Clone + Send + 'static> Message for T {}
226
227/// Answers requests from peers. The peer id is the one Noise authenticated.
228pub trait Handler<Q, R>: Send + Sync + 'static {
229    fn request(&self, from: &PeerId, req: Q) -> R;
230    /// A peer was discovered or identified.
231    fn seen(&self, _s: Sighting) {}
232}
233
234/// What the node is doing.
235#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
236pub struct Status {
237    pub peer_id: String,
238    pub listening: Vec<String>,
239    pub external: Vec<String>,
240    pub connected: Vec<String>,
241    /// AutoNAT: `public`, `private` or `unknown`.
242    pub nat: String,
243    /// Relays holding a reservation for us.
244    pub reservations: Vec<String>,
245    pub relay_server: bool,
246    pub mdns: bool,
247    pub started_at: i64,
248    /// Paused by the app ([`Node::pause`]).
249    #[serde(default)]
250    pub paused: bool,
251}
252
253// rustc 1.100 flags code the `NetworkBehaviour` derive generates (matches on
254// uninhabited events) as unreachable; the allow has to sit on a module to reach it.
255#[allow(unreachable_code)]
256mod wire {
257    use super::*;
258
259    #[derive(NetworkBehaviour)]
260    pub(super) struct Behaviour<Q: Message, R: Message> {
261        pub(super) rr: request_response::cbor::Behaviour<Q, R>,
262        pub(super) names: request_response::cbor::Behaviour<NamesRequest, NamesResponse>,
263        pub(super) identify: identify::Behaviour,
264        pub(super) ping: ping::Behaviour,
265        pub(super) kad: kad::Behaviour<MemoryStore>,
266        pub(super) mdns: Toggle<mdns::tokio::Behaviour>,
267        pub(super) relay_client: relay::client::Behaviour,
268        pub(super) relay_server: Toggle<relay::Behaviour>,
269        pub(super) dcutr: dcutr::Behaviour,
270        pub(super) autonat: autonat::Behaviour,
271        pub(super) streams: crate::stream::transport::Behaviour,
272    }
273}
274use wire::{Behaviour, BehaviourEvent};
275
276enum Cmd<Q, R> {
277    Request { peer: PeerId, addrs: Vec<Multiaddr>, wire: Q, reply: oneshot::Sender<Result<R>> },
278    Names { peer: PeerId, addrs: Vec<Multiaddr>, req: NamesRequest, reply: oneshot::Sender<Result<NamesResponse>> },
279    PutEntry { entry: Entry, reply: oneshot::Sender<Result<()>> },
280    GetEntries { user: String, reply: oneshot::Sender<Result<Vec<Entry>>> },
281    FindPeer { peer: PeerId, reply: oneshot::Sender<Result<Vec<Multiaddr>>> },
282    KnownAddrs { reply: oneshot::Sender<Result<Vec<(PeerId, Multiaddr)>>> },
283    Dial { addr: Multiaddr, reply: oneshot::Sender<Result<PeerId>> },
284    Connect { peer: PeerId, addrs: Vec<Multiaddr>, reply: oneshot::Sender<Result<PeerId>> },
285    DropPath { peer: PeerId, path: Path, reply: oneshot::Sender<Result<usize>> },
286    Paths { peer: PeerId, reply: oneshot::Sender<Result<Vec<Path>>> },
287    Pause { reply: oneshot::Sender<Result<()>> },
288    Resume { reply: oneshot::Sender<Result<()>> },
289    NetworkChanged { reply: oneshot::Sender<Result<()>> },
290    Listen { addr: Multiaddr, reply: oneshot::Sender<Result<Multiaddr>> },
291    AddRelay { addr: Multiaddr },
292    Provide { key: Vec<u8>, reply: oneshot::Sender<Result<()>> },
293    PutLocator { rec: crate::locator::LocatorRecord, reply: oneshot::Sender<Result<()>> },
294    GetLocators { key: Vec<u8>, reply: oneshot::Sender<Result<Vec<crate::locator::LocatorRecord>>> },
295    StopProviding { key: Vec<u8> },
296    Providers { key: Vec<u8>, reply: oneshot::Sender<Result<Vec<PeerId>>> },
297    Stop,
298}
299
300type Providing = (Vec<PeerId>, oneshot::Sender<Result<Vec<PeerId>>>);
301
302/// A running node.
303pub struct Node<Q: Message, R: Message> {
304    peer_id: PeerId,
305    profile: &'static Profile,
306    names_list: Vec<Multiaddr>,
307    tx: mpsc::UnboundedSender<Cmd<Q, R>>,
308    status: Arc<Mutex<Status>>,
309    request_timeout: Duration,
310    streams: (tokio::runtime::Handle, Control),
311    hub: Hub,
312    status_rx: tokio::sync::watch::Receiver<Status>,
313    thread: Option<std::thread::JoinHandle<()>>,
314}
315
316fn net_err(e: impl std::fmt::Display) -> NetError {
317    NetError::net(e)
318}
319
320fn stopped() -> NetError {
321    NetError::Network("the node has stopped".into())
322}
323
324fn behaviour<Q: Message, R: Message>(
325    p: &Profile,
326    key: &Keypair,
327    relay_client: relay::client::Behaviour,
328    cfg: &NodeConfig,
329) -> Result<Behaviour<Q, R>> {
330    let peer = key.public().to_peer_id();
331    let rr = request_response::cbor::Behaviour::with_codec(
332        request_response::cbor::codec::Codec::default().set_request_size_maximum(cfg.req_max()).set_response_size_maximum(cfg.resp_max()),
333        [(StreamProtocol::new(p.protocol), ProtocolSupport::Full)],
334        request_response::Config::default().with_request_timeout(cfg.req_timeout()),
335    );
336    let version = if cfg.version.is_empty() { env!("CARGO_PKG_VERSION") } else { cfg.version.as_str() };
337    let agent = p.agent(version, &cfg.label);
338    // A relay with explicit external addresses announces only those, not every
339    // interface it listens on (private VPC and loopback ones are useless to clients).
340    let hide = cfg.relay_server && !cfg.external.is_empty();
341    let identify =
342        identify::Behaviour::new(identify::Config::new(p.identify.into(), key.public()).with_agent_version(agent).with_hide_listen_addrs(hide));
343    let mut kcfg = kad::Config::new(StreamProtocol::new(p.kad));
344    kcfg.set_query_timeout(Duration::from_secs(30));
345    // every record and provider a peer puts is checked first (peer records, relay keys)
346    kcfg.set_record_filtering(kad::StoreInserts::FilterBoth);
347    let names = request_response::cbor::Behaviour::with_codec(
348        request_response::cbor::codec::Codec::default().set_request_size_maximum(64 * 1024).set_response_size_maximum(64 * 1024),
349        [(StreamProtocol::new(p.names), if cfg.names_server.is_some() { ProtocolSupport::Full } else { ProtocolSupport::Outbound })],
350        request_response::Config::default().with_request_timeout(Duration::from_secs(20)),
351    );
352    let kad = kad::Behaviour::with_config(peer, MemoryStore::new(peer), kcfg);
353    let mdns = if cfg.mdns && !cfg.memory { Some(mdns::tokio::Behaviour::new(mdns::Config::default(), peer).map_err(net_err)?) } else { None };
354    let relay_server = cfg.relay_server.then(|| relay::Behaviour::new(peer, cfg.relay_limits.config()));
355    let autonat = autonat::Behaviour::new(peer, autonat::Config { only_global_ips: !cfg.memory, ..Default::default() });
356    Ok(Behaviour {
357        rr,
358        names,
359        identify,
360        ping: ping::Behaviour::new(ping::Config::new()),
361        kad,
362        mdns: mdns.into(),
363        relay_client,
364        relay_server: relay_server.into(),
365        dcutr: dcutr::Behaviour::new(peer),
366        autonat,
367        streams: crate::stream::transport::Behaviour::new(),
368    })
369}
370
371type BoxErr = Box<dyn std::error::Error + Send + Sync>;
372
373fn memory_transport(key: &Keypair) -> std::result::Result<Boxed<(PeerId, StreamMuxerBox)>, BoxErr> {
374    Ok(MemoryTransport::default()
375        .upgrade(Version::V1)
376        .authenticate(noise::Config::new(key).map_err(|e| Box::new(e) as BoxErr)?)
377        .multiplex(yamux::Config::default())
378        .timeout(Duration::from_secs(20))
379        .boxed())
380}
381
382fn build_swarm<Q: Message, R: Message>(p: &'static Profile, key: Keypair, cfg: &NodeConfig) -> Result<Swarm<Behaviour<Q, R>>> {
383    let idle = |c: libp2p::swarm::Config| c.with_idle_connection_timeout(Duration::from_secs(120));
384    let swarm = if cfg.memory {
385        let cfg2 = cfg.clone();
386        libp2p::SwarmBuilder::with_existing_identity(key)
387            .with_tokio()
388            .with_other_transport(memory_transport)
389            .map_err(net_err)?
390            .with_relay_client(noise::Config::new, yamux::Config::default)
391            .map_err(net_err)?
392            .with_behaviour(move |key, relay| behaviour(p, key, relay, &cfg2).map_err(|e| Box::new(e) as BoxErr))
393            .map_err(net_err)?
394            .with_swarm_config(idle)
395            .build()
396    } else {
397        let cfg2 = cfg.clone();
398        let wss_tls = cfg.wss.clone().unwrap_or_default();
399        let dns = dns_config(&cfg.dns_servers);
400        wss_tls.config()?;
401        libp2p::SwarmBuilder::with_existing_identity(key)
402            .with_tokio()
403            .with_other_transport(|key| crate::wss::transport(key, &wss_tls, dns))
404            .map_err(net_err)?
405            .with_relay_client(noise::Config::new, yamux::Config::default)
406            .map_err(net_err)?
407            .with_behaviour(move |key, relay| behaviour(p, key, relay, &cfg2).map_err(|e| Box::new(e) as BoxErr))
408            .map_err(net_err)?
409            .with_swarm_config(idle)
410            .build()
411    };
412    Ok(swarm)
413}
414
415impl<Q: Message, R: Message> Node<Q, R> {
416    /// Start a node speaking `profile`; returns once its listeners are up (or failed).
417    pub fn start(profile: &'static Profile, key: Keypair, cfg: NodeConfig, handler: Arc<dyn Handler<Q, R>>) -> Result<Self> {
418        if cfg.relay_server {
419            cfg.relay_limits.check()?;
420        }
421        let peer_id = key.public().to_peer_id();
422        let status = Arc::new(Mutex::new(Status {
423            peer_id: peer_id.to_string(),
424            nat: "unknown".into(),
425            relay_server: cfg.relay_server,
426            mdns: cfg.mdns && !cfg.memory,
427            started_at: crate::now_ms(),
428            ..Default::default()
429        }));
430        let (tx, rx) = mpsc::unbounded_channel();
431        let (ready_tx, ready_rx) = std::sync::mpsc::channel::<Result<()>>();
432        let st = status.clone();
433        let names_list = cfg.names_servers.clone();
434        let request_timeout = cfg.req_timeout();
435        let (streams_tx, streams_rx) = std::sync::mpsc::channel::<(tokio::runtime::Handle, Control)>();
436        let hub = Hub::default();
437        let (status_tx, status_rx) = tokio::sync::watch::channel(status.lock().map(|s| s.clone()).unwrap_or_default());
438        let hub2 = hub.clone();
439        let thread = std::thread::Builder::new()
440            .name(profile.thread.into())
441            .spawn(move || {
442                let rt = match tokio::runtime::Builder::new_multi_thread()
443                    .worker_threads(2)
444                    .enable_all()
445                    .thread_name(format!("{}-rt", profile.thread))
446                    .build()
447                {
448                    Ok(rt) => rt,
449                    Err(e) => {
450                        let _ = ready_tx.send(Err(net_err(e)));
451                        return;
452                    }
453                };
454                rt.block_on(async move {
455                    let swarm = match build_swarm(profile, key, &cfg) {
456                        Ok(mut s) => {
457                            for a in &cfg.external {
458                                s.add_external_address(a.clone());
459                            }
460                            let _ = streams_tx.send((tokio::runtime::Handle::current(), s.behaviour().streams.control()));
461                            s
462                        }
463                        Err(e) => {
464                            let _ = ready_tx.send(Err(e));
465                            return;
466                        }
467                    };
468                    let mut l = Loop::new(profile, swarm, cfg, handler, st);
469                    l.hub = hub2;
470                    l.status_tx = Some(status_tx);
471                    l.run(rx, ready_tx).await;
472                });
473                rt.shutdown_timeout(Duration::from_secs(2));
474            })
475            .map_err(net_err)?;
476        match ready_rx.recv_timeout(Duration::from_secs(20)) {
477            Ok(Ok(())) => {
478                let streams = streams_rx.recv().map_err(|_| stopped())?;
479                Ok(Node { peer_id, profile, names_list, tx, status, request_timeout, streams, hub, status_rx, thread: Some(thread) })
480            }
481            Ok(Err(e)) => {
482                let _ = thread.join();
483                Err(e)
484            }
485            Err(_) => Err(NetError::Network("the node did not start in time".into())),
486        }
487    }
488
489    /// The identifier profile the node speaks.
490    pub fn profile(&self) -> &'static Profile {
491        self.profile
492    }
493
494    pub fn peer_id(&self) -> PeerId {
495        self.peer_id
496    }
497
498    pub fn status(&self) -> Status {
499        self.status.lock().map(|s| s.clone()).unwrap_or_default()
500    }
501
502    /// Addresses others can use to reach this node (with `/p2p/ID`).
503    pub fn reachable_addrs(&self) -> Vec<String> {
504        let s = self.status();
505        let mut v: Vec<String> = vec![];
506        for a in s.external.iter().chain(s.listening.iter()) {
507            // a circuit through a relay is only useful if the relay's own address is routable
508            if let Ok(m) = a.parse::<Multiaddr>()
509                && shareable(&m)
510                && (!m.iter().any(|p| p == Protocol::P2pCircuit) || routable(&m))
511            {
512                let full = if addr_peer(&m).is_some() { m } else { m.with(Protocol::P2p(self.peer_id)) };
513                let t = full.to_string();
514                if !v.contains(&t) {
515                    v.push(t);
516                }
517            }
518        }
519        v
520    }
521
522    fn call<T>(&self, make: impl FnOnce(oneshot::Sender<Result<T>>) -> Cmd<Q, R>, wait: Duration) -> Result<T> {
523        let (tx, rx) = oneshot::channel();
524        self.tx.send(make(tx)).map_err(|_| stopped())?;
525        let deadline = std::time::Instant::now() + wait;
526        let mut rx = rx;
527        loop {
528            match rx.try_recv() {
529                Ok(r) => return r,
530                Err(oneshot::error::TryRecvError::Closed) => return Err(stopped()),
531                Err(oneshot::error::TryRecvError::Empty) => {
532                    if std::time::Instant::now() > deadline {
533                        return Err(NetError::Network("timed out".into()));
534                    }
535                    std::thread::sleep(Duration::from_millis(5));
536                }
537            }
538        }
539    }
540
541    /// Send a request and wait for the answer. `addrs` are tried if not connected yet.
542    pub fn request(&self, peer: PeerId, addrs: &[Multiaddr], wire: Q) -> Result<R> {
543        let addrs = addrs.to_vec();
544        self.call(|reply| Cmd::Request { peer, addrs, wire, reply }, self.request_timeout + Duration::from_secs(5))
545    }
546
547    /// Ask a names server (a relay, usually) something; `addrs` are tried if not connected.
548    pub fn names_request(&self, server: PeerId, addrs: &[Multiaddr], req: NamesRequest) -> Result<NamesResponse> {
549        let addrs = addrs.to_vec();
550        self.call(|reply| Cmd::Names { peer: server, addrs, req, reply }, Duration::from_secs(30))
551    }
552
553    /// Register a record or withdrawal on a names server.
554    pub fn register(&self, server: PeerId, addrs: &[Multiaddr], entry: Entry) -> Result<()> {
555        match self.names_request(server, addrs, NamesRequest::Register(Box::new(entry)))? {
556            NamesResponse::Registered => Ok(()),
557            NamesResponse::Refused(why) => Err(NetError::Network(format!("the names server refused: {why}"))),
558            NamesResponse::Found(_) => Err(NetError::Network("unexpected answer from the names server".into())),
559        }
560    }
561
562    /// A user's entry on one names server.
563    pub fn discover(&self, server: PeerId, addrs: &[Multiaddr], user: &str) -> Result<Option<Entry>> {
564        match self.names_request(server, addrs, NamesRequest::Discover { user_id: user.into() })? {
565            NamesResponse::Found(e) => Ok(e.map(|e| *e)),
566            NamesResponse::Refused(why) => Err(NetError::Network(format!("the names server refused: {why}"))),
567            NamesResponse::Registered => Err(NetError::Network("unexpected answer from the names server".into())),
568        }
569    }
570
571    /// Put a record or withdrawal in the DHT (checked first, like every storing node does).
572    /// Records belong in the DHT only while the device hosts something public or private.
573    pub fn put_entry(&self, entry: Entry) -> Result<()> {
574        self.call(|reply| Cmd::PutEntry { entry, reply }, Duration::from_secs(40))
575    }
576
577    /// Put a private site's locator record in the DHT (checked first, as storing nodes do).
578    pub fn put_locator(&self, rec: crate::locator::LocatorRecord) -> Result<()> {
579        self.call(|reply| Cmd::PutLocator { rec, reply }, Duration::from_secs(40))
580    }
581
582    /// The locator records the DHT holds for `locator` today and yesterday (checked but not
583    /// opened; see [`LocatorKeys::open`](crate::locator::LocatorKeys::open)).
584    pub fn get_locators(&self, locator: &crate::PublicKey) -> Result<Vec<crate::locator::LocatorRecord>> {
585        let mut out = vec![];
586        for key in crate::locator::lookup_keys(locator, crate::now_ms()) {
587            out.extend(self.call(|reply| Cmd::GetLocators { key, reply }, Duration::from_secs(40))?);
588        }
589        Ok(out)
590    }
591
592    /// Every entry the DHT returns for a user (unchecked; see [`Profile::newest`]).
593    pub fn get_entries(&self, user: &str) -> Result<Vec<Entry>> {
594        let user = user.to_string();
595        self.call(|reply| Cmd::GetEntries { user, reply }, Duration::from_secs(40))
596    }
597
598    /// Where a peer is, by its key alone: what the DHT knows of it.
599    pub fn find_peer(&self, peer: PeerId) -> Result<Vec<Multiaddr>> {
600        self.call(|reply| Cmd::FindPeer { peer, reply }, Duration::from_secs(40))
601    }
602
603    /// Announce in the DHT that this node provides `key` (see
604    /// [`content::provider_key`](crate::content::provider_key)); kept and re-announced until
605    /// [`stop_providing`](Self::stop_providing) or the node stops.
606    pub fn provide(&self, key: &[u8]) -> Result<()> {
607        let key = key.to_vec();
608        self.call(|reply| Cmd::Provide { key, reply }, Duration::from_secs(5))
609    }
610
611    pub fn stop_providing(&self, key: &[u8]) -> Result<()> {
612        self.tx.send(Cmd::StopProviding { key: key.to_vec() }).map_err(|_| stopped())
613    }
614
615    /// The peers the DHT knows to provide `key` (this node excluded).
616    pub fn providers(&self, key: &[u8]) -> Result<Vec<PeerId>> {
617        let key = key.to_vec();
618        self.call(|reply| Cmd::Providers { key, reply }, Duration::from_secs(40))
619    }
620
621    /// Names servers to ask: the configured ones and the relays holding our reservation.
622    fn names_servers(&self) -> Vec<(PeerId, Vec<Multiaddr>)> {
623        let known = self.call(|reply| Cmd::KnownAddrs { reply }, Duration::from_secs(5)).unwrap_or_default();
624        let mut out: Vec<(PeerId, Vec<Multiaddr>)> = vec![];
625        let reserved: Vec<PeerId> = self.status().reservations.iter().filter_map(|r| r.parse().ok()).collect();
626        for (p, a) in known.into_iter() {
627            if reserved.contains(&p) || self.names_list.iter().any(|n| addr_peer(n) == Some(p)) {
628                match out.iter_mut().find(|(q, _)| *q == p) {
629                    Some((_, v)) => v.push(a),
630                    None => out.push((p, vec![a])),
631                }
632            }
633        }
634        for n in &self.names_list {
635            if let Some(p) = addr_peer(n)
636                && !out.iter().any(|(q, _)| *q == p)
637            {
638                out.push((p, vec![n.clone()]));
639            }
640        }
641        out
642    }
643
644    /// Register an entry with every names server this node uses (its relays and the
645    /// configured ones); returns how many took it.
646    pub fn register_everywhere(&self, entry: &Entry) -> usize {
647        self.names_servers().into_iter().filter(|(p, a)| self.register(*p, a, entry.clone()).is_ok()).count()
648    }
649
650    /// Find a user's freshest valid record: on this node's names servers, then in the DHT.
651    pub fn lookup(&self, user: &str) -> Lookup {
652        let mut found: Vec<Entry> = vec![];
653        for (p, a) in self.names_servers() {
654            if let Ok(Some(e)) = self.discover(p, &a, user) {
655                found.push(e);
656            }
657        }
658        if let Ok(v) = self.get_entries(user) {
659            found.extend(v);
660        }
661        self.profile.newest(user, &found, crate::now_ms())
662    }
663
664    /// Send a request to a peer known only by its key: when not connected, find its
665    /// addresses in the DHT and try circuits through this node's relays and names servers.
666    pub fn request_key(&self, peer: PeerId, wire: Q) -> Result<R> {
667        if self.status().connected.contains(&peer.to_string()) {
668            return self.request(peer, &[], wire);
669        }
670        let mut addrs = self.find_peer(peer).unwrap_or_default();
671        for (_, a) in self.names_servers() {
672            for base in a {
673                let base: Multiaddr = base.iter().take_while(|p| *p != Protocol::P2pCircuit).collect();
674                addrs.push(base.with(Protocol::P2pCircuit).with(Protocol::P2p(peer)));
675            }
676        }
677        self.request(peer, &addrs, wire)
678    }
679
680    /// Send a request to a user's node: look up its record and dial its addresses
681    /// (circuits and direct ones together). Returns the node that answered.
682    pub fn request_user(&self, user: &str, wire: Q) -> Result<(PeerId, R)> {
683        match self.lookup(user) {
684            Lookup::Found(r) => {
685                let peer: PeerId = r.cert.peer_id.parse().map_err(|_| NetError::invalid("peer record", "bad peer id"))?;
686                let addrs = crate::addr::parse_addrs(&r.addrs);
687                Ok((peer, self.request(peer, &addrs, wire)?))
688            }
689            Lookup::Withdrawn(_) => Err(NetError::Network(format!("{user} withdrew its record"))),
690            Lookup::NotFound => Err(NetError::Network(format!("no record for {user}"))),
691        }
692    }
693
694    /// Connect to an address; returns the peer there.
695    pub fn dial(&self, addr: Multiaddr) -> Result<PeerId> {
696        self.call(|reply| Cmd::Dial { addr, reply }, Duration::from_secs(30))
697    }
698
699    /// Connect to `peer` if not connected yet, trying `addrs` (or the addresses the node knows).
700    pub fn connect(&self, peer: PeerId, addrs: &[Multiaddr]) -> Result<()> {
701        let addrs = addrs.to_vec();
702        self.call(|reply| Cmd::Connect { peer, addrs, reply }, Duration::from_secs(30)).map(|_| ())
703    }
704
705    /// Close this node's direct or relayed connections to `peer`; returns how many. Streams on
706    /// them move to another path (or wait for one to come back).
707    pub fn drop_path(&self, peer: PeerId, path: Path) -> Result<usize> {
708        self.call(|reply| Cmd::DropPath { peer, path, reply }, Duration::from_secs(5))
709    }
710
711    /// Events from now on (connections, listeners, reservations, NAT, lifecycle).
712    pub fn events(&self) -> Events {
713        self.hub.subscribe()
714    }
715
716    /// The status, and a way to wait for it to change.
717    pub fn watch_status(&self) -> StatusWatch {
718        StatusWatch { rx: self.status_rx.clone() }
719    }
720
721    /// The app goes to the background: close every connection and listener and give up relay
722    /// reservations. Requests and streams opened while paused still dial out.
723    pub fn pause(&self) -> Result<()> {
724        self.call(|reply| Cmd::Pause { reply }, Duration::from_secs(5))
725    }
726
727    /// Back in the foreground: listen again, reserve on the relays again, rejoin the DHT.
728    pub fn resume(&self) -> Result<()> {
729        self.call(|reply| Cmd::Resume { reply }, Duration::from_secs(5))
730    }
731
732    /// The device moved to another network (Wi-Fi to cellular, a new address): drop the
733    /// connections made on the old one and make them again. Streams carry on once a path is back.
734    pub fn network_changed(&self) -> Result<()> {
735        self.call(|reply| Cmd::NetworkChanged { reply }, Duration::from_secs(5))
736    }
737
738    /// [`Node::request`] for async callers (any executor).
739    pub async fn request_async(&self, peer: PeerId, addrs: &[Multiaddr], wire: Q) -> Result<R> {
740        let addrs = addrs.to_vec();
741        self.call_async(|reply| Cmd::Request { peer, addrs, wire, reply }, self.request_timeout + Duration::from_secs(5)).await
742    }
743
744    /// [`Node::connect`] for async callers.
745    pub async fn connect_async(&self, peer: PeerId, addrs: &[Multiaddr]) -> Result<()> {
746        let addrs = addrs.to_vec();
747        self.call_async(|reply| Cmd::Connect { peer, addrs, reply }, Duration::from_secs(30)).await.map(|_| ())
748    }
749
750    /// [`Node::dial`] for async callers.
751    pub async fn dial_async(&self, addr: Multiaddr) -> Result<PeerId> {
752        self.call_async(|reply| Cmd::Dial { addr, reply }, Duration::from_secs(30)).await
753    }
754
755    async fn call_async<T: Send + 'static>(&self, make: impl FnOnce(oneshot::Sender<Result<T>>) -> Cmd<Q, R>, wait: Duration) -> Result<T> {
756        let (tx, rx) = oneshot::channel();
757        self.tx.send(make(tx)).map_err(|_| stopped())?;
758        let rt = self.streams.0.clone();
759        rt.spawn(async move { tokio::time::timeout(wait, rx).await })
760            .await
761            .map_err(|_| stopped())?
762            .map_err(|_| NetError::Network("timed out".into()))?
763            .map_err(|_| stopped())?
764    }
765
766    /// The connections to `peer` now, by kind (one entry per connection).
767    pub fn paths(&self, peer: PeerId) -> Result<Vec<Path>> {
768        self.call(|reply| Cmd::Paths { peer, reply }, Duration::from_secs(5))
769    }
770
771    /// Accept streams of `protocol` (an id of the application's, such as `/myapp/sync/1`).
772    /// Other peers' streams of protocols nobody accepts are refused.
773    pub fn accept_streams(&self, protocol: &str, limits: Limits) -> Result<Incoming> {
774        let p = StreamProtocol::try_from_owned(protocol.to_string()).map_err(|e| NetError::Network(format!("bad protocol id {protocol}: {e}")))?;
775        let (rt, control) = &self.streams;
776        crate::stream::accept(control.clone(), p, limits, rt)
777    }
778
779    /// Open a stream of `protocol` to `peer` (connecting through `addrs` if needed). It starts
780    /// on a direct connection when there is one and moves there later if one comes up.
781    pub fn open_stream(&self, peer: PeerId, addrs: &[Multiaddr], protocol: &str, limits: Limits) -> Result<Stream> {
782        futures::executor::block_on(self.open_stream_async(peer, addrs, protocol, limits))
783    }
784
785    /// Forwarded HTTPS connections for this node's sites ([`crate::ingress`]).
786    pub fn accept_ingress(&self) -> Result<crate::ingress::IngressIncoming> {
787        Ok(crate::ingress::IngressIncoming { inc: self.accept_streams(crate::ingress::INGRESS_PROTOCOL, Limits::default())? })
788    }
789
790    /// Register this node's sites with an ingress peer: `reg` carries the user's grants for
791    /// that ingress, the user's certificate for this node and where the ingress reaches it.
792    pub fn register_ingress(&self, ingress: PeerId, addrs: &[Multiaddr], reg: &crate::ingress::Registration) -> Result<crate::ingress::Registered> {
793        use futures::AsyncWriteExt;
794        let body = serde_json::to_vec(reg).map_err(|e| NetError::invalid("registration", e.to_string()))?;
795        let mut s = self.open_stream(ingress, addrs, crate::ingress::GRANT_PROTOCOL, Limits::default())?;
796        let rt = self.streams.0.clone();
797        rt.block_on(async move {
798            crate::ingress::write_msg(&mut s, &body).await.map_err(NetError::net)?;
799            let _ = s.close().await;
800            let b = tokio::time::timeout(Duration::from_secs(60), crate::ingress::read_msg(&mut s, crate::ingress::MAX_REGISTRATION))
801                .await
802                .map_err(|_| NetError::Network("no answer from the ingress".into()))?
803                .map_err(NetError::net)?;
804            serde_json::from_slice(&b).map_err(|e| NetError::invalid("registration answer", e.to_string()))
805        })
806    }
807
808    pub(crate) fn runtime(&self) -> tokio::runtime::Handle {
809        self.streams.0.clone()
810    }
811
812    pub(crate) fn opener(&self, protocol: &str, limits: Limits) -> Opener {
813        let tx = self.tx.clone();
814        let control = self.streams.1.clone();
815        let p = StreamProtocol::try_from_owned(protocol.to_string()).expect("a valid protocol id");
816        Arc::new(move |peer, addrs| {
817            let (tx, control, p) = (tx.clone(), control.clone(), p.clone());
818            Box::pin(async move {
819                let (rtx, rrx) = oneshot::channel();
820                tx.send(Cmd::<Q, R>::Connect { peer, addrs, reply: rtx }).map_err(|_| stopped())?;
821                tokio::time::timeout(Duration::from_secs(30), rrx)
822                    .await
823                    .map_err(|_| NetError::Network(format!("cannot reach {peer} in time")))?
824                    .map_err(|_| stopped())??;
825                crate::stream::open(control, peer, p, limits).await
826            })
827        })
828    }
829
830    /// [`Node::open_stream`] for async callers (any executor).
831    pub async fn open_stream_async(&self, peer: PeerId, addrs: &[Multiaddr], protocol: &str, limits: Limits) -> Result<Stream> {
832        let p = StreamProtocol::try_from_owned(protocol.to_string()).map_err(|e| NetError::Network(format!("bad protocol id {protocol}: {e}")))?;
833        let (tx, rx) = oneshot::channel();
834        self.tx.send(Cmd::Connect { peer, addrs: addrs.to_vec(), reply: tx }).map_err(|_| stopped())?;
835        let (rt, control) = &self.streams;
836        let control = control.clone();
837        rt.spawn(async move {
838            tokio::time::timeout(Duration::from_secs(30), rx)
839                .await
840                .map_err(|_| NetError::Network(format!("cannot reach {peer} in time")))?
841                .map_err(|_| stopped())??;
842            crate::stream::open(control, peer, p, limits).await
843        })
844        .await
845        .map_err(|_| stopped())?
846    }
847
848    /// Listen on one more address; returns the address it got.
849    pub fn listen(&self, addr: Multiaddr) -> Result<Multiaddr> {
850        self.call(|reply| Cmd::Listen { addr, reply }, Duration::from_secs(20))
851    }
852
853    /// Reserve a slot on a relay (`…/p2p/RELAY`).
854    pub fn add_relay(&self, addr: Multiaddr) -> Result<()> {
855        self.tx.send(Cmd::AddRelay { addr }).map_err(|_| stopped())
856    }
857
858    /// Whether the node thread still runs.
859    pub fn running(&self) -> bool {
860        self.thread.as_ref().is_some_and(|t| !t.is_finished())
861    }
862
863    pub fn stop(mut self) {
864        self.shutdown();
865    }
866
867    /// Ask the node to stop without waiting (others may still hold it).
868    pub fn halt(&self) {
869        let _ = self.tx.send(Cmd::Stop);
870    }
871
872    fn shutdown(&mut self) {
873        let _ = self.tx.send(Cmd::Stop);
874        if let Some(t) = self.thread.take() {
875            let _ = t.join();
876        }
877    }
878}
879
880impl<Q: Message, R: Message> Drop for Node<Q, R> {
881    fn drop(&mut self) {
882        self.shutdown();
883    }
884}
885
886/// The resolver: the given servers, else the system's, else Cloudflare's and Google's.
887fn dns_config(servers: &[std::net::SocketAddr]) -> (libp2p::dns::ResolverConfig, libp2p::dns::ResolverOpts) {
888    use hickory_resolver::config::{CLOUDFLARE, GOOGLE, NameServerConfig, ResolverConfig};
889    if !servers.is_empty() {
890        let ns = servers
891            .iter()
892            .map(|a| {
893                let mut n = NameServerConfig::udp_and_tcp(a.ip());
894                for c in &mut n.connections {
895                    c.port = a.port();
896                }
897                n
898            })
899            .collect();
900        return (ResolverConfig::from_name_servers(ns), Default::default());
901    }
902    match hickory_resolver::system_conf::read_system_conf() {
903        Ok((c, o)) if !c.name_servers().is_empty() => (c, o),
904        _ => {
905            let ns = CLOUDFLARE.udp_and_tcp().chain(GOOGLE.udp_and_tcp()).collect();
906            (ResolverConfig::from_name_servers(ns), Default::default())
907        }
908    }
909}
910
911/// Opens a stream of one protocol to a peer, connecting first (held by services like the ingress).
912pub(crate) type Opener = Arc<dyn Fn(PeerId, Vec<Multiaddr>) -> futures::future::BoxFuture<'static, Result<Stream>> + Send + Sync>;
913
914/// A DHT lookup collecting entries, and who waits for them.
915type Locating = (Vec<crate::locator::LocatorRecord>, oneshot::Sender<Result<Vec<crate::locator::LocatorRecord>>>);
916type Gathering = (Vec<Entry>, oneshot::Sender<Result<Vec<Entry>>>);
917/// A DHT search for one peer's addresses.
918type Finding = (PeerId, oneshot::Sender<Result<Vec<Multiaddr>>>);
919
920struct Loop<Q: Message, R: Message> {
921    profile: &'static Profile,
922    swarm: Swarm<Behaviour<Q, R>>,
923    cfg: NodeConfig,
924    handler: Arc<dyn Handler<Q, R>>,
925    status: Arc<Mutex<Status>>,
926    pending: HashMap<OutboundRequestId, oneshot::Sender<Result<R>>>,
927    dials: Vec<(Multiaddr, Option<PeerId>, oneshot::Sender<Result<PeerId>>)>,
928    listens: HashMap<libp2p::core::transport::ListenerId, oneshot::Sender<Result<Multiaddr>>>,
929    relays: HashSet<PeerId>,
930    relay_listening: HashSet<PeerId>,
931    /// Each relay's circuit listener and when it was asked for (to retry a stuck one).
932    relay_listeners: HashMap<PeerId, (Multiaddr, libp2p::core::transport::ListenerId, std::time::Instant)>,
933    resp_tx: mpsc::UnboundedSender<(request_response::ResponseChannel<R>, R)>,
934    resp_rx: mpsc::UnboundedReceiver<(request_response::ResponseChannel<R>, R)>,
935    names_pending: HashMap<OutboundRequestId, oneshot::Sender<Result<NamesResponse>>>,
936    names_tx: mpsc::UnboundedSender<(request_response::ResponseChannel<NamesResponse>, NamesResponse)>,
937    names_rx: mpsc::UnboundedReceiver<(request_response::ResponseChannel<NamesResponse>, NamesResponse)>,
938    puts: HashMap<kad::QueryId, oneshot::Sender<Result<()>>>,
939    gets: HashMap<kad::QueryId, Gathering>,
940    finds: HashMap<kad::QueryId, Finding>,
941    provs: HashMap<kad::QueryId, Providing>,
942    lgets: HashMap<kad::QueryId, Locating>,
943    addr_book: HashMap<PeerId, Vec<Multiaddr>>,
944    /// What connected peers say they listen on (identify), for provider records without addresses.
945    heard: HashMap<PeerId, Vec<Multiaddr>>,
946    /// Commands that go through a relay we are still connecting to (a bootstrap dial to the
947    /// same relay under way makes libp2p's relay transport skip its own dial and fail).
948    deferred: Vec<(PeerId, std::time::Instant, Cmd<Q, R>)>,
949    /// Open connections: whose, and whether through a relay.
950    conns: HashMap<libp2p::swarm::ConnectionId, (PeerId, bool)>,
951    hub: Hub,
952    status_tx: Option<tokio::sync::watch::Sender<Status>>,
953    /// Listeners up now (paused nodes close them).
954    listeners: HashSet<libp2p::core::transport::ListenerId>,
955    paused: bool,
956    /// Relays whose reservation went away, and when to ask again.
957    relay_retry: Vec<(Multiaddr, std::time::Instant, Duration)>,
958    /// How long to wait before asking a relay again; reset when it accepts.
959    relay_backoff: HashMap<PeerId, Duration>,
960}
961
962fn path_of(relayed: bool) -> Path {
963    if relayed { Path::Relayed } else { Path::Direct }
964}
965
966fn endpoint_relayed(e: &libp2p::core::ConnectedPoint) -> bool {
967    let circuit = |a: &Multiaddr| a.iter().any(|p| p == Protocol::P2pCircuit);
968    match e {
969        libp2p::core::ConnectedPoint::Dialer { address, .. } => circuit(address),
970        libp2p::core::ConnectedPoint::Listener { local_addr, send_back_addr } => circuit(local_addr) || circuit(send_back_addr),
971    }
972}
973
974/// How long a command waits for its relay connection before it is tried anyway.
975const RELAY_WAIT: Duration = Duration::from_secs(10);
976
977impl<Q: Message, R: Message> Loop<Q, R> {
978    fn new(
979        profile: &'static Profile,
980        swarm: Swarm<Behaviour<Q, R>>,
981        cfg: NodeConfig,
982        handler: Arc<dyn Handler<Q, R>>,
983        status: Arc<Mutex<Status>>,
984    ) -> Self {
985        let (resp_tx, resp_rx) = mpsc::unbounded_channel();
986        let (names_tx, names_rx) = mpsc::unbounded_channel();
987        Loop {
988            profile,
989            swarm,
990            cfg,
991            handler,
992            status,
993            pending: HashMap::new(),
994            dials: vec![],
995            listens: HashMap::new(),
996            relays: HashSet::new(),
997            relay_listening: HashSet::new(),
998            relay_listeners: HashMap::new(),
999            resp_tx,
1000            resp_rx,
1001            names_pending: HashMap::new(),
1002            names_tx,
1003            names_rx,
1004            puts: HashMap::new(),
1005            gets: HashMap::new(),
1006            finds: HashMap::new(),
1007            provs: HashMap::new(),
1008            lgets: HashMap::new(),
1009            addr_book: HashMap::new(),
1010            heard: HashMap::new(),
1011            deferred: vec![],
1012            conns: HashMap::new(),
1013            hub: Hub::default(),
1014            status_tx: None,
1015            listeners: HashSet::new(),
1016            paused: false,
1017            relay_retry: vec![],
1018            relay_backoff: HashMap::new(),
1019        }
1020    }
1021
1022    fn update(&self, f: impl FnOnce(&mut Status)) {
1023        if let Ok(mut s) = self.status.lock() {
1024            f(&mut s);
1025            if let Some(tx) = &self.status_tx {
1026                tx.send_if_modified(|old| {
1027                    let changed = *old != *s;
1028                    if changed {
1029                        *old = s.clone();
1030                    }
1031                    changed
1032                });
1033            }
1034        }
1035    }
1036
1037    fn refresh(&self) {
1038        let connected: Vec<String> = self.swarm.connected_peers().map(|p| p.to_string()).collect();
1039        let listening: Vec<String> = self.swarm.listeners().map(|a| a.to_string()).collect();
1040        let external: Vec<String> = self.swarm.external_addresses().map(|a| a.to_string()).collect();
1041        self.update(|s| {
1042            s.connected = connected;
1043            s.listening = listening;
1044            s.external = external;
1045        });
1046    }
1047
1048    fn seen(&self, s: Sighting) {
1049        let h = self.handler.clone();
1050        tokio::task::spawn_blocking(move || h.seen(s));
1051    }
1052
1053    async fn run(&mut self, mut rx: mpsc::UnboundedReceiver<Cmd<Q, R>>, ready: std::sync::mpsc::Sender<Result<()>>) {
1054        // listen
1055        let mut waiting = 0usize;
1056        let mut ready = Some(ready);
1057        for a in self.cfg.listen.clone() {
1058            match self.swarm.listen_on(a.clone()) {
1059                Ok(_) => waiting += 1,
1060                Err(e) => tracing::warn!("network: cannot listen on {a}: {e}"),
1061            }
1062        }
1063        if !self.cfg.listen.is_empty() && waiting == 0 {
1064            if let Some(r) = ready.take() {
1065                let _ = r.send(Err(NetError::Network("could not listen on any address".into())));
1066            }
1067            return;
1068        }
1069        // relays first: a reservation dials its relay, and a second dial to the same peer
1070        // already under way would make the relay client skip its own
1071        for r in self.cfg.relays.clone() {
1072            self.add_relay(r);
1073        }
1074        for b in self.cfg.bootstrap.clone() {
1075            if let Some(p) = addr_peer(&b) {
1076                self.swarm.behaviour_mut().kad.add_address(&p, b.clone());
1077                if !self.relay_listening.contains(&p) {
1078                    let _ = self.swarm.dial(b);
1079                }
1080            }
1081        }
1082        if self.cfg.relay_server {
1083            self.swarm.behaviour_mut().kad.set_mode(Some(kad::Mode::Server));
1084        }
1085        if waiting == 0
1086            && let Some(r) = ready.take()
1087        {
1088            let _ = r.send(Ok(()));
1089        }
1090        let ready_deadline = tokio::time::sleep(Duration::from_secs(5));
1091        tokio::pin!(ready_deadline);
1092        let mut tick = tokio::time::interval(Duration::from_secs(60));
1093        let mut check = tokio::time::interval(Duration::from_secs(1));
1094        loop {
1095            tokio::select! {
1096                cmd = rx.recv() => match cmd {
1097                    None | Some(Cmd::Stop) => break,
1098                    Some(c) => self.command(c),
1099                },
1100                Some((ch, resp)) = self.resp_rx.recv() => {
1101                    let _ = self.swarm.behaviour_mut().rr.send_response(ch, resp);
1102                }
1103                Some((ch, resp)) = self.names_rx.recv() => {
1104                    let _ = self.swarm.behaviour_mut().names.send_response(ch, resp);
1105                }
1106                ev = self.swarm.select_next_some() => {
1107                    let new_listen = matches!(ev, SwarmEvent::NewListenAddr { .. });
1108                    self.event(ev);
1109                    if new_listen && waiting > 0 {
1110                        waiting -= 1;
1111                        if waiting == 0 && let Some(r) = ready.take() {
1112                            let _ = r.send(Ok(()));
1113                        }
1114                    }
1115                }
1116                _ = &mut ready_deadline, if ready.is_some() => {
1117                    if let Some(r) = ready.take() {
1118                        let _ = r.send(Ok(()));
1119                    }
1120                }
1121                _ = tick.tick() => self.periodic(),
1122                _ = check.tick() => {
1123                    self.retry_stuck_reservations();
1124                    self.retry_lost_reservations();
1125                    self.release_deferred(None);
1126                }
1127            }
1128        }
1129        for (_, tx) in self.pending.drain() {
1130            let _ = tx.send(Err(NetError::Network("the node stopped".into())));
1131        }
1132        for (_, tx) in self.names_pending.drain() {
1133            let _ = tx.send(Err(NetError::Network("the node stopped".into())));
1134        }
1135    }
1136
1137    fn periodic(&mut self) {
1138        if let Some(n) = &self.cfg.names_server {
1139            n.0.prune(crate::now_ms());
1140        }
1141        if self.paused {
1142            return;
1143        }
1144        let k = &mut self.swarm.behaviour_mut().kad;
1145        // With an empty routing table there is no one to bootstrap from; asking
1146        // anyway only makes libp2p warn "No known peers" every tick.
1147        if routing_empty(k) {
1148            return;
1149        }
1150        if k.bootstrap().is_ok() {
1151            k.get_providers(kad::RecordKey::new(&self.profile.relay_key));
1152            if self.cfg.relay_server {
1153                let _ = k.start_providing(kad::RecordKey::new(&self.profile.relay_key));
1154            }
1155        }
1156    }
1157
1158    fn add_relay(&mut self, addr: Multiaddr) {
1159        let Some(peer) = addr_peer(&addr) else {
1160            tracing::warn!("network: relay address {addr} has no /p2p/ peer id");
1161            return;
1162        };
1163        if peer == *self.swarm.local_peer_id() || !self.relay_listening.insert(peer) {
1164            return;
1165        }
1166        self.relays.insert(peer);
1167        self.swarm.add_peer_address(peer, addr.clone());
1168        let circuit = addr.with(Protocol::P2pCircuit);
1169        match self.swarm.listen_on(circuit.clone()) {
1170            Ok(id) => {
1171                self.relay_listeners.insert(peer, (circuit, id, std::time::Instant::now()));
1172            }
1173            Err(e) => {
1174                tracing::warn!("network: cannot listen through relay {circuit}: {e}");
1175                self.relay_listening.remove(&peer);
1176            }
1177        }
1178    }
1179
1180    /// A relay's circuit listener closed (its connection went away, or we paused): the
1181    /// reservation is gone; ask again later unless paused.
1182    fn relay_listener_closed(&mut self, id: libp2p::core::transport::ListenerId) {
1183        let Some(relay) = self.relay_listeners.iter().find(|(_, (_, l, _))| *l == id).map(|(p, _)| *p) else { return };
1184        let Some((mut circuit, _, _)) = self.relay_listeners.remove(&relay) else { return };
1185        self.relay_listening.remove(&relay);
1186        let r = relay.to_string();
1187        self.update(|s| s.reservations.retain(|x| *x != r));
1188        self.hub.send(Event::ReservationLost { relay });
1189        circuit.pop();
1190        if !self.paused {
1191            let wait = self.relay_backoff.get(&relay).copied().unwrap_or(Duration::from_secs(1));
1192            self.relay_backoff.insert(relay, (wait * 2).min(Duration::from_secs(60)));
1193            self.relay_retry.retain(|(a, _, _)| *a != circuit);
1194            self.relay_retry.push((circuit, std::time::Instant::now() + wait, wait));
1195        }
1196    }
1197
1198    fn retry_lost_reservations(&mut self) {
1199        if self.paused {
1200            return;
1201        }
1202        let now = std::time::Instant::now();
1203        let (due, keep): (Vec<_>, Vec<_>) = std::mem::take(&mut self.relay_retry).into_iter().partition(|(_, at, _)| *at <= now);
1204        self.relay_retry = keep;
1205        for (addr, _, wait) in due {
1206            let peer = addr_peer(&addr);
1207            self.add_relay(addr.clone());
1208            // not listening through it (couldn't ask): try again, backing off to a minute
1209            if peer.is_some_and(|p| !self.relay_listening.contains(&p)) {
1210                let wait = (wait * 2).min(Duration::from_secs(60));
1211                self.relay_retry.push((addr, now + wait, wait));
1212            }
1213        }
1214    }
1215
1216    /// Close every connection and listener (relay circuits too).
1217    fn go_quiet(&mut self) {
1218        for id in self.listeners.clone() {
1219            self.swarm.remove_listener(id);
1220        }
1221        for (_, (_, id, _)) in self.relay_listeners.clone() {
1222            self.swarm.remove_listener(id);
1223        }
1224        let peers: Vec<PeerId> = self.swarm.connected_peers().copied().collect();
1225        for p in peers {
1226            let _ = self.swarm.disconnect_peer_id(p);
1227        }
1228    }
1229
1230    /// Listen, reserve and bootstrap again.
1231    fn wake(&mut self, listen: bool) {
1232        if listen {
1233            for a in self.cfg.listen.clone() {
1234                if let Err(e) = self.swarm.listen_on(a.clone()) {
1235                    tracing::warn!("network: cannot listen on {a}: {e}");
1236                }
1237            }
1238        }
1239        let relays: Vec<Multiaddr> = self.cfg.relays.iter().cloned().chain(self.relay_retry.drain(..).map(|(a, _, _)| a)).collect();
1240        for r in relays {
1241            self.add_relay(r);
1242        }
1243        for b in self.cfg.bootstrap.clone() {
1244            if let Some(p) = addr_peer(&b)
1245                && !self.relay_listening.contains(&p)
1246            {
1247                let _ = self.swarm.dial(b);
1248            }
1249        }
1250        let k = &mut self.swarm.behaviour_mut().kad;
1251        if !routing_empty(k) {
1252            let _ = k.bootstrap();
1253        }
1254    }
1255
1256    /// A reservation asked for while another dial to the relay was under way is never sent
1257    /// (libp2p's relay client skips its own dial then); once connected, ask again.
1258    fn retry_stuck_reservations(&mut self) {
1259        let reserved = self.status.lock().map(|s| s.reservations.clone()).unwrap_or_default();
1260        let stuck: Vec<PeerId> = self
1261            .relay_listeners
1262            .iter()
1263            .filter(|(p, (_, _, at))| at.elapsed() > Duration::from_secs(3) && !reserved.contains(&p.to_string()) && self.swarm.is_connected(p))
1264            .map(|(p, _)| *p)
1265            .collect();
1266        for p in stuck {
1267            if let Some((circuit, id, _)) = self.relay_listeners.remove(&p) {
1268                tracing::debug!("network: asking relay {p} for a reservation again");
1269                self.swarm.remove_listener(id);
1270                match self.swarm.listen_on(circuit.clone()) {
1271                    Ok(id) => {
1272                        self.relay_listeners.insert(p, (circuit, id, std::time::Instant::now()));
1273                    }
1274                    Err(e) => tracing::warn!("network: cannot listen through relay {circuit}: {e}"),
1275                }
1276            }
1277        }
1278    }
1279
1280    fn command(&mut self, c: Cmd<Q, R>) {
1281        let addrs: Vec<Multiaddr> = match &c {
1282            Cmd::Request { addrs, .. } | Cmd::Names { addrs, .. } => addrs.clone(),
1283            Cmd::Dial { addr, .. } => vec![addr.clone()],
1284            Cmd::Connect { peer, addrs, .. } if !self.swarm.is_connected(peer) => addrs.clone(),
1285            _ => vec![],
1286        };
1287        // through a relay we aren't connected to yet: connect to it first (or wait for the
1288        // dial already under way), then send
1289        let waiting = addrs.iter().filter_map(circuit_relay).find(|(r, _)| *r != *self.swarm.local_peer_id() && !self.swarm.is_connected(r));
1290        if let Some((relay, raddr)) = waiting {
1291            use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
1292            let _ = self.swarm.dial(DialOpts::peer_id(relay).addresses(vec![raddr]).condition(PeerCondition::DisconnectedAndNotDialing).build());
1293            self.deferred.push((relay, std::time::Instant::now(), c));
1294            return;
1295        }
1296        self.execute(c);
1297    }
1298
1299    /// Send the commands that waited for `relay` (connected, or its dial failed), or every
1300    /// one that waited too long (`None`).
1301    fn release_deferred(&mut self, relay: Option<PeerId>) {
1302        let (ready, keep): (Vec<_>, Vec<_>) =
1303            std::mem::take(&mut self.deferred).into_iter().partition(|(r, at, _)| relay.map_or(at.elapsed() > RELAY_WAIT, |p| p == *r));
1304        self.deferred = keep;
1305        for (_, _, c) in ready {
1306            self.execute(c);
1307        }
1308    }
1309
1310    fn execute(&mut self, c: Cmd<Q, R>) {
1311        match c {
1312            Cmd::Request { peer, addrs, wire, reply } => {
1313                for a in addrs {
1314                    self.swarm.add_peer_address(peer, a);
1315                }
1316                let id = self.swarm.behaviour_mut().rr.send_request(&peer, wire);
1317                self.pending.insert(id, reply);
1318            }
1319            Cmd::Names { peer, addrs, req, reply } => {
1320                for a in addrs {
1321                    self.swarm.add_peer_address(peer, a);
1322                }
1323                let id = self.swarm.behaviour_mut().names.send_request(&peer, req);
1324                self.names_pending.insert(id, reply);
1325            }
1326            Cmd::PutEntry { entry, reply } => {
1327                let key = kad::RecordKey::new(&self.profile.record_key(entry.user_id()));
1328                let existing = self.swarm.behaviour_mut().kad.store_mut().get(&key).and_then(|r| Entry::decode(&r.value).ok());
1329                if let Err(e) = self.profile.accept(existing.as_ref(), &entry, crate::now_ms()) {
1330                    let _ = reply.send(Err(e));
1331                    return;
1332                }
1333                let rec = record_of(key, &entry);
1334                match self.swarm.behaviour_mut().kad.put_record(rec, kad::Quorum::One) {
1335                    Ok(q) => {
1336                        self.puts.insert(q, reply);
1337                    }
1338                    Err(e) => {
1339                        let _ = reply.send(Err(net_err(e)));
1340                    }
1341                }
1342            }
1343            Cmd::PutLocator { rec, reply } => {
1344                let now = crate::now_ms();
1345                let key = match rec.key() {
1346                    Ok(k) => k,
1347                    Err(e) => {
1348                        let _ = reply.send(Err(e));
1349                        return;
1350                    }
1351                };
1352                let rkey = kad::RecordKey::new(&key);
1353                let existing =
1354                    self.swarm.behaviour_mut().kad.store_mut().get(&rkey).and_then(|r| crate::locator::LocatorRecord::decode(&r.value).ok());
1355                if let Err(e) = crate::locator::accept_locator(&key, existing.as_ref(), &rec, now) {
1356                    let _ = reply.send(Err(e));
1357                    return;
1358                }
1359                match self.swarm.behaviour_mut().kad.put_record(locator_record_of(rkey, &rec), kad::Quorum::One) {
1360                    Ok(q) => {
1361                        self.puts.insert(q, reply);
1362                    }
1363                    Err(e) => {
1364                        let _ = reply.send(Err(net_err(e)));
1365                    }
1366                }
1367            }
1368            Cmd::GetLocators { key, reply } => {
1369                let q = self.swarm.behaviour_mut().kad.get_record(kad::RecordKey::new(&key));
1370                self.lgets.insert(q, (vec![], reply));
1371            }
1372            Cmd::GetEntries { user, reply } => {
1373                let q = self.swarm.behaviour_mut().kad.get_record(kad::RecordKey::new(&self.profile.record_key(&user)));
1374                self.gets.insert(q, (vec![], reply));
1375            }
1376            Cmd::FindPeer { peer, reply } => {
1377                let q = self.swarm.behaviour_mut().kad.get_closest_peers(peer);
1378                self.finds.insert(q, (peer, reply));
1379            }
1380            Cmd::KnownAddrs { reply } => {
1381                let v = self.addr_book.iter().flat_map(|(p, a)| a.iter().map(|a| (*p, a.clone()))).collect();
1382                let _ = reply.send(Ok(v));
1383            }
1384            Cmd::Dial { addr, reply } => {
1385                let peer = addr_peer(&addr);
1386                match self.swarm.dial(addr.clone()) {
1387                    Ok(()) => self.dials.push((addr, peer, reply)),
1388                    Err(e) => {
1389                        let _ = reply.send(Err(net_err(e)));
1390                    }
1391                }
1392            }
1393            Cmd::Connect { peer, addrs, reply } => {
1394                use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
1395                if self.swarm.is_connected(&peer) {
1396                    let _ = reply.send(Ok(peer));
1397                    return;
1398                }
1399                for a in &addrs {
1400                    self.swarm.add_peer_address(peer, a.clone());
1401                }
1402                let first = addrs.first().cloned().unwrap_or_else(|| Multiaddr::empty().with(Protocol::P2p(peer)));
1403                match self.swarm.dial(DialOpts::peer_id(peer).addresses(addrs).condition(PeerCondition::DisconnectedAndNotDialing).build()) {
1404                    Ok(()) | Err(libp2p::swarm::DialError::DialPeerConditionFalse(_)) => self.dials.push((first, Some(peer), reply)),
1405                    Err(e) => {
1406                        let _ = reply.send(Err(net_err(e)));
1407                    }
1408                }
1409            }
1410            Cmd::DropPath { peer, path, reply } => {
1411                let ids: Vec<_> =
1412                    self.conns.iter().filter(|(_, (p, relayed))| *p == peer && (*relayed == (path == Path::Relayed))).map(|(c, _)| *c).collect();
1413                let n = ids.iter().filter(|c| self.swarm.close_connection(**c)).count();
1414                let _ = reply.send(Ok(n));
1415            }
1416            Cmd::Pause { reply } => {
1417                if !self.paused {
1418                    self.paused = true;
1419                    // relays to come back to
1420                    let mut back: Vec<Multiaddr> = self
1421                        .relay_listeners
1422                        .values()
1423                        .map(|(c, _, _)| {
1424                            let mut c = c.clone();
1425                            c.pop();
1426                            c
1427                        })
1428                        .collect();
1429                    back.extend(self.relay_retry.drain(..).map(|(a, _, _)| a));
1430                    self.go_quiet();
1431                    let now = std::time::Instant::now();
1432                    self.relay_retry = back.into_iter().map(|a| (a, now, Duration::from_secs(1))).collect();
1433                    self.update(|s| s.paused = true);
1434                    self.hub.send(Event::Paused);
1435                }
1436                let _ = reply.send(Ok(()));
1437            }
1438            Cmd::Resume { reply } => {
1439                if self.paused {
1440                    self.paused = false;
1441                    self.update(|s| s.paused = false);
1442                    self.wake(true);
1443                    self.hub.send(Event::Resumed);
1444                }
1445                let _ = reply.send(Ok(()));
1446            }
1447            Cmd::NetworkChanged { reply } => {
1448                if !self.paused {
1449                    let peers: Vec<PeerId> = self.swarm.connected_peers().copied().collect();
1450                    for p in peers {
1451                        let _ = self.swarm.disconnect_peer_id(p);
1452                    }
1453                    self.wake(false);
1454                }
1455                self.hub.send(Event::NetworkChanged);
1456                let _ = reply.send(Ok(()));
1457            }
1458            Cmd::Paths { peer, reply } => {
1459                let v = self.conns.values().filter(|(p, _)| *p == peer).map(|(_, r)| if *r { Path::Relayed } else { Path::Direct }).collect();
1460                let _ = reply.send(Ok(v));
1461            }
1462            Cmd::Listen { addr, reply } => match self.swarm.listen_on(addr) {
1463                Ok(id) => {
1464                    self.listens.insert(id, reply);
1465                }
1466                Err(e) => {
1467                    let _ = reply.send(Err(net_err(e)));
1468                }
1469            },
1470            Cmd::AddRelay { addr } => self.add_relay(addr),
1471            Cmd::Provide { key, reply } => {
1472                let r = self.swarm.behaviour_mut().kad.start_providing(kad::RecordKey::new(&key)).map(|_| ()).map_err(net_err);
1473                let _ = reply.send(r);
1474            }
1475            Cmd::StopProviding { key } => self.swarm.behaviour_mut().kad.stop_providing(&kad::RecordKey::new(&key)),
1476            Cmd::Providers { key, reply } => {
1477                let q = self.swarm.behaviour_mut().kad.get_providers(kad::RecordKey::new(&key));
1478                self.provs.insert(q, (vec![], reply));
1479            }
1480            Cmd::Stop => {}
1481        }
1482    }
1483
1484    fn event(&mut self, ev: SwarmEvent<BehaviourEvent<Q, R>>) {
1485        match ev {
1486            SwarmEvent::NewListenAddr { listener_id, address } => {
1487                tracing::info!("network: listening on {address}");
1488                self.listeners.insert(listener_id);
1489                self.hub.send(Event::Listening(address.clone()));
1490                // a relay announces where it listens: reservations carry those addresses
1491                if self.cfg.relay_server && self.cfg.external.is_empty() && routable(&address) && !address.iter().any(|p| p == Protocol::P2pCircuit) {
1492                    self.swarm.add_external_address(address.clone());
1493                }
1494                if let Some(tx) = self.listens.remove(&listener_id) {
1495                    let _ = tx.send(Ok(address));
1496                }
1497                self.refresh();
1498            }
1499            SwarmEvent::ListenerClosed { listener_id, addresses, .. } => {
1500                self.listeners.remove(&listener_id);
1501                for a in addresses {
1502                    self.hub.send(Event::ListenClosed(a));
1503                }
1504                if let Some(tx) = self.listens.remove(&listener_id) {
1505                    let _ = tx.send(Err(NetError::Network("listener closed".into())));
1506                }
1507                self.relay_listener_closed(listener_id);
1508                self.refresh();
1509            }
1510            SwarmEvent::ExternalAddrConfirmed { address } => {
1511                // a relay keeps to its configured addresses (or at least routable ones)
1512                if self.cfg.relay_server && (!routable(&address) || (!self.cfg.external.is_empty() && !self.cfg.external.contains(&address))) {
1513                    self.swarm.remove_external_address(&address);
1514                } else {
1515                    self.hub.send(Event::External { addr: address, confirmed: true });
1516                }
1517                self.refresh()
1518            }
1519            SwarmEvent::ExternalAddrExpired { address } => {
1520                self.hub.send(Event::External { addr: address, confirmed: false });
1521                self.refresh()
1522            }
1523            SwarmEvent::ExpiredListenAddr { address, .. } => {
1524                self.hub.send(Event::ListenClosed(address));
1525                self.refresh()
1526            }
1527            SwarmEvent::ConnectionEstablished { peer_id, endpoint, connection_id, .. } => {
1528                self.release_deferred(Some(peer_id));
1529                let relayed = endpoint_relayed(&endpoint);
1530                self.conns.insert(connection_id, (peer_id, relayed));
1531                self.hub.send(Event::Connected { peer: peer_id, path: path_of(relayed) });
1532                let addr = endpoint.get_remote_address().clone();
1533                if endpoint.is_dialer() {
1534                    let a = if addr_peer(&addr) == Some(peer_id) { addr.clone() } else { addr.clone().with(Protocol::P2p(peer_id)) };
1535                    let book = self.addr_book.entry(peer_id).or_default();
1536                    if !book.contains(&a) {
1537                        book.push(a);
1538                    }
1539                }
1540                let mut i = 0;
1541                while i < self.dials.len() {
1542                    let (a, p, _) = &self.dials[i];
1543                    if *a == addr || p.is_some_and(|p| p == peer_id) {
1544                        let (_, _, tx) = self.dials.remove(i);
1545                        let _ = tx.send(Ok(peer_id));
1546                    } else {
1547                        i += 1;
1548                    }
1549                }
1550                self.refresh();
1551            }
1552            SwarmEvent::ConnectionClosed { connection_id, peer_id, num_established, .. } => {
1553                if let Some((_, relayed)) = self.conns.remove(&connection_id) {
1554                    self.hub.send(Event::Disconnected { peer: peer_id, path: path_of(relayed), remaining: num_established });
1555                }
1556                if num_established == 0 {
1557                    self.heard.remove(&peer_id);
1558                }
1559                self.refresh()
1560            }
1561            SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
1562                if let Some(p) = peer_id
1563                    && !self.swarm.is_connected(&p)
1564                {
1565                    self.release_deferred(Some(p));
1566                }
1567                let msg = error.to_string();
1568                let mut i = 0;
1569                while i < self.dials.len() {
1570                    if peer_id.is_some() && self.dials[i].1 == peer_id {
1571                        let (_, _, tx) = self.dials.remove(i);
1572                        let _ = tx.send(Err(net_err(&msg)));
1573                    } else {
1574                        i += 1;
1575                    }
1576                }
1577                if peer_id.is_none() && !self.dials.is_empty() {
1578                    // an address without /p2p/: the oldest dial failed
1579                    let (_, _, tx) = self.dials.remove(0);
1580                    let _ = tx.send(Err(net_err(&msg)));
1581                }
1582            }
1583            SwarmEvent::Behaviour(b) => self.behaviour(b),
1584            _ => {}
1585        }
1586    }
1587
1588    fn behaviour(&mut self, ev: BehaviourEvent<Q, R>) {
1589        match ev {
1590            BehaviourEvent::Rr(request_response::Event::Message { peer, message, .. }) => match message {
1591                request_response::Message::Request { request, channel, .. } => {
1592                    let h = self.handler.clone();
1593                    let tx = self.resp_tx.clone();
1594                    tokio::task::spawn_blocking(move || {
1595                        let resp = h.request(&peer, request);
1596                        let _ = tx.send((channel, resp));
1597                    });
1598                }
1599                request_response::Message::Response { request_id, response } => {
1600                    if let Some(tx) = self.pending.remove(&request_id) {
1601                        let _ = tx.send(Ok(response));
1602                    }
1603                }
1604            },
1605            BehaviourEvent::Rr(request_response::Event::OutboundFailure { request_id, error, peer, .. }) => {
1606                if let Some(tx) = self.pending.remove(&request_id) {
1607                    let _ = tx.send(Err(NetError::Network(format!("{} could not be reached ({error})", crate::short_peer_id(&peer)))));
1608                }
1609            }
1610            BehaviourEvent::Rr(request_response::Event::InboundFailure { peer, error, .. }) => {
1611                tracing::debug!("network: inbound request from {peer} failed: {error}");
1612            }
1613            BehaviourEvent::Names(request_response::Event::Message { peer, message, .. }) => match message {
1614                request_response::Message::Request { request, channel, .. } => {
1615                    let Some(store) = self.cfg.names_server.clone() else { return };
1616                    let tx = self.names_tx.clone();
1617                    let profile = self.profile;
1618                    tokio::task::spawn_blocking(move || {
1619                        let resp = crate::names::serve(profile, &*store.0, &peer, request, crate::now_ms());
1620                        let _ = tx.send((channel, resp));
1621                    });
1622                }
1623                request_response::Message::Response { request_id, response } => {
1624                    if let Some(tx) = self.names_pending.remove(&request_id) {
1625                        let _ = tx.send(Ok(response));
1626                    }
1627                }
1628            },
1629            BehaviourEvent::Names(request_response::Event::OutboundFailure { request_id, error, peer, .. }) => {
1630                if let Some(tx) = self.names_pending.remove(&request_id) {
1631                    let _ = tx.send(Err(NetError::Network(format!("names server {} could not be reached ({error})", crate::short_peer_id(&peer)))));
1632                }
1633            }
1634            BehaviourEvent::Kad(kad::Event::InboundRequest { request }) => match request {
1635                kad::InboundRequest::PutRecord { record: Some(rec), source, .. } => self.inbound_put(rec, source),
1636                kad::InboundRequest::AddProvider { record: Some(rec) } => {
1637                    // nodes announce the relay key and content roots, nothing else
1638                    if rec.key.as_ref() == self.profile.relay_key.as_bytes() || crate::content::is_provider_key(rec.key.as_ref()) {
1639                        let mut rec = rec;
1640                        // a provider that sent no addresses is answered with the ones we know,
1641                        // or others could not reach it
1642                        if rec.addresses.is_empty() {
1643                            rec.addresses = self.heard.get(&rec.provider).or_else(|| self.addr_book.get(&rec.provider)).cloned().unwrap_or_default();
1644                        }
1645                        let _ = self.swarm.behaviour_mut().kad.store_mut().add_provider(rec);
1646                    }
1647                }
1648                _ => {}
1649            },
1650            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::PutRecord(r), .. }) => {
1651                if let Some(tx) = self.puts.remove(&id) {
1652                    let _ = tx.send(r.map(|_| ()).map_err(net_err));
1653                }
1654            }
1655            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::GetRecord(r), step, .. }) => {
1656                let now = crate::now_ms();
1657                let done = match r {
1658                    Ok(kad::GetRecordOk::FoundRecord(pr)) => {
1659                        if let Some((v, _)) = self.lgets.get_mut(&id)
1660                            && let Ok(l) = crate::locator::LocatorRecord::decode(&pr.record.value)
1661                            && crate::locator::accept_locator(pr.record.key.as_ref(), None, &l, now).is_ok()
1662                        {
1663                            v.push(l);
1664                        }
1665                        if let Some((v, _)) = self.gets.get_mut(&id)
1666                            && let Ok(e) = Entry::decode(&pr.record.value)
1667                        {
1668                            v.push(e);
1669                        }
1670                        step.last
1671                    }
1672                    _ => true,
1673                };
1674                if done && let Some((v, tx)) = self.lgets.remove(&id) {
1675                    let _ = tx.send(Ok(v));
1676                    if let Some(mut q) = self.swarm.behaviour_mut().kad.query_mut(&id) {
1677                        q.finish();
1678                    }
1679                }
1680                if done && let Some((v, tx)) = self.gets.remove(&id) {
1681                    let _ = tx.send(Ok(v));
1682                    if let Some(mut q) = self.swarm.behaviour_mut().kad.query_mut(&id) {
1683                        q.finish();
1684                    }
1685                }
1686            }
1687            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::GetClosestPeers(r), .. }) => {
1688                if let Some((peer, tx)) = self.finds.remove(&id) {
1689                    let addrs = match r {
1690                        Ok(ok) => ok.peers,
1691                        Err(kad::GetClosestPeersError::Timeout { peers, .. }) => peers,
1692                    }
1693                    .into_iter()
1694                    .filter(|i| i.peer_id == peer)
1695                    .flat_map(|i| i.addrs)
1696                    .map(|a| if addr_peer(&a) == Some(peer) { a } else { a.with(Protocol::P2p(peer)) })
1697                    .collect();
1698                    let _ = tx.send(Ok(addrs));
1699                }
1700            }
1701            BehaviourEvent::Mdns(mdns::Event::Discovered(list)) => {
1702                let mut by_peer: HashMap<PeerId, Vec<Multiaddr>> = HashMap::new();
1703                for (p, a) in list {
1704                    by_peer.entry(p).or_default().push(a);
1705                }
1706                for (p, addrs) in by_peer {
1707                    for a in &addrs {
1708                        self.swarm.add_peer_address(p, a.clone());
1709                        self.swarm.behaviour_mut().kad.add_address(&p, a.clone());
1710                    }
1711                    if !self.paused && !self.swarm.is_connected(&p) {
1712                        let _ = self.swarm.dial(p);
1713                    }
1714                    self.seen(Sighting { peer: p, addrs: addrs.iter().map(|a| a.to_string()).collect(), caps: None, agent: None, source: "mdns" });
1715                }
1716            }
1717            BehaviourEvent::Identify(identify::Event::Received { peer_id, info, .. }) => {
1718                let protos: Vec<String> = info.protocols.iter().map(|p| p.to_string()).collect();
1719                let mut caps = vec![];
1720                if protos.iter().any(|p| p == self.profile.protocol) {
1721                    caps.push(self.profile.name.to_string());
1722                }
1723                if protos.iter().any(|p| *p == relay::HOP_PROTOCOL_NAME.to_string()) {
1724                    caps.push("relay".to_string());
1725                }
1726                if protos.iter().any(|p| p == self.profile.kad) {
1727                    caps.push("kad".to_string());
1728                    for a in &info.listen_addrs {
1729                        self.swarm.behaviour_mut().kad.add_address(&peer_id, a.clone());
1730                    }
1731                }
1732                let is_relay = caps.iter().any(|c| c == "relay");
1733                let addrs: Vec<String> = info.listen_addrs.iter().filter(|a| shareable(a)).map(|a| a.to_string()).collect();
1734                self.heard.insert(peer_id, info.listen_addrs.iter().filter(|a| shareable(a)).take(8).cloned().collect());
1735                self.seen(Sighting { peer: peer_id, addrs, caps: Some(caps), agent: Some(info.agent_version), source: "identify" });
1736                // behind NAT: use relays we meet
1737                let private = self.status.lock().map(|s| s.nat == "private").unwrap_or(false);
1738                if is_relay
1739                    && private
1740                    && !self.paused
1741                    && self.relay_listening.is_empty()
1742                    && let Some(a) = info.listen_addrs.iter().filter(|a| shareable(a)).min_by_key(|a| reach_rank(a))
1743                {
1744                    self.add_relay(a.clone().with(Protocol::P2p(peer_id)));
1745                }
1746            }
1747            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed { id, result: kad::QueryResult::GetProviders(r), step, .. })
1748                if self.provs.contains_key(&id) =>
1749            {
1750                let me = *self.swarm.local_peer_id();
1751                let done = step.last || !matches!(r, Ok(kad::GetProvidersOk::FoundProviders { .. }));
1752                if let Some((found, _)) = self.provs.get_mut(&id)
1753                    && let Ok(kad::GetProvidersOk::FoundProviders { providers, .. }) = r
1754                {
1755                    for p in providers {
1756                        if p != me && !found.contains(&p) {
1757                            found.push(p);
1758                        }
1759                    }
1760                }
1761                if done && let Some((found, reply)) = self.provs.remove(&id) {
1762                    let _ = reply.send(Ok(found));
1763                }
1764            }
1765            BehaviourEvent::Kad(kad::Event::OutboundQueryProgressed {
1766                result: kad::QueryResult::GetProviders(Ok(kad::GetProvidersOk::FoundProviders { providers, .. })),
1767                ..
1768            }) => {
1769                for p in providers {
1770                    if p != *self.swarm.local_peer_id() {
1771                        self.seen(Sighting { peer: p, addrs: vec![], caps: Some(vec!["relay".into(), "kad".into()]), agent: None, source: "kad" });
1772                    }
1773                }
1774            }
1775            BehaviourEvent::Autonat(autonat::Event::StatusChanged { new, .. }) => {
1776                let nat = match new {
1777                    autonat::NatStatus::Public(_) => "public",
1778                    autonat::NatStatus::Private => "private",
1779                    autonat::NatStatus::Unknown => "unknown",
1780                };
1781                self.update(|s| s.nat = nat.into());
1782                self.hub.send(Event::Nat(nat.into()));
1783                if nat == "private" {
1784                    let relays: Vec<PeerId> = self.relays.iter().copied().collect();
1785                    tracing::info!("network: behind NAT; {} relay(s) known", relays.len());
1786                }
1787            }
1788            BehaviourEvent::RelayClient(relay::client::Event::ReservationReqAccepted { relay_peer_id, .. }) => {
1789                tracing::info!("network: reservation on relay {relay_peer_id}");
1790                self.hub.send(Event::Reserved { relay: relay_peer_id });
1791                self.relay_backoff.remove(&relay_peer_id);
1792                self.update(|s| {
1793                    let r = relay_peer_id.to_string();
1794                    if !s.reservations.contains(&r) {
1795                        s.reservations.push(r);
1796                    }
1797                });
1798            }
1799            BehaviourEvent::Streams(e) => match e {},
1800            BehaviourEvent::Dcutr(e) => {
1801                tracing::debug!("network: hole punch with {}: {}", e.remote_peer_id, if e.result.is_ok() { "direct" } else { "failed" });
1802            }
1803            _ => {}
1804        }
1805    }
1806}
1807
1808/// A Kademlia record holding a names entry, expiring with it.
1809fn record_of(key: kad::RecordKey, entry: &Entry) -> kad::Record {
1810    let mut rec = kad::Record::new(key, entry.encode());
1811    let left = (entry.expires() - crate::now_ms()).max(0) as u64;
1812    rec.expires = Some(std::time::Instant::now() + Duration::from_millis(left));
1813    rec
1814}
1815
1816/// A Kademlia record holding a locator record, expiring with it.
1817fn locator_record_of(key: kad::RecordKey, l: &crate::locator::LocatorRecord) -> kad::Record {
1818    let mut rec = kad::Record::new(key, l.encode());
1819    let left = (l.expires - crate::now_ms()).max(0) as u64;
1820    rec.expires = Some(std::time::Instant::now() + Duration::from_millis(left));
1821    rec
1822}
1823
1824impl<Q: Message, R: Message> Loop<Q, R> {
1825    /// A peer put a locator record: keep it only if it checks out under its key.
1826    fn inbound_locator(&mut self, rec: kad::Record, l: crate::locator::LocatorRecord, source: PeerId) {
1827        let store = self.swarm.behaviour_mut().kad.store_mut();
1828        let existing = store.get(&rec.key).and_then(|r| crate::locator::LocatorRecord::decode(&r.value).ok());
1829        match crate::locator::accept_locator(rec.key.as_ref(), existing.as_ref(), &l, crate::now_ms()) {
1830            Ok(()) => {
1831                let mut r = locator_record_of(rec.key.clone(), &l);
1832                r.publisher = rec.publisher;
1833                let _ = store.put(r);
1834            }
1835            Err(e) => tracing::debug!("network: refused a locator record from {source}: {e}"),
1836        }
1837    }
1838
1839    /// A peer put a record: keep it only if it is a valid names entry under its own key
1840    /// that supersedes what we hold (see [`Profile::accept`]).
1841    fn inbound_put(&mut self, rec: kad::Record, source: PeerId) {
1842        let Ok(entry) = Entry::decode(&rec.value) else {
1843            if let Ok(l) = crate::locator::LocatorRecord::decode(&rec.value) {
1844                return self.inbound_locator(rec, l, source);
1845            }
1846            tracing::debug!("network: refused a DHT record from {source}: not a names entry");
1847            return;
1848        };
1849        let key = kad::RecordKey::new(&self.profile.record_key(entry.user_id()));
1850        if rec.key != key {
1851            tracing::debug!("network: refused a DHT record from {source}: wrong key");
1852            return;
1853        }
1854        let store = self.swarm.behaviour_mut().kad.store_mut();
1855        let existing = store.get(&key).and_then(|r| Entry::decode(&r.value).ok());
1856        match self.profile.accept(existing.as_ref(), &entry, crate::now_ms()) {
1857            Ok(()) => {
1858                let mut r = record_of(key, &entry);
1859                r.publisher = rec.publisher;
1860                let _ = store.put(r);
1861            }
1862            Err(e) => tracing::debug!("network: refused a DHT record from {source}: {e}"),
1863        }
1864    }
1865}
1866
1867/// True when the Kademlia routing table holds no peers yet.
1868fn routing_empty<S: kad::store::RecordStore + Send + 'static>(k: &mut kad::Behaviour<S>) -> bool {
1869    k.kbuckets().all(|b| b.num_entries() == 0)
1870}