iroh/socket/remote_map/remote_state/
remote_info.rs

1use iroh_base::{EndpointId, TransportAddr};
2
3/// Information about a remote endpoint.
4///
5/// This information is a snapshot in time, i.e. it is not updating and may
6/// already be outdated by the time you are reading this. Updated information
7/// can only be retrieved by calling [`Endpoint::remote_info`] again.
8///
9/// [`Endpoint::remote_info`]: crate::Endpoint::remote_info
10#[derive(Debug, Clone)]
11pub struct RemoteInfo {
12    pub(super) endpoint_id: EndpointId,
13    pub(super) addrs: Vec<TransportAddrInfo>,
14}
15
16impl RemoteInfo {
17    /// Returns the remote's endpoint id.
18    pub fn id(&self) -> EndpointId {
19        self.endpoint_id
20    }
21
22    /// Returns an iterator over known all addresses for this remote.
23    ///
24    /// Note that this may include outdated or unusable addresses.
25    pub fn addrs(&self) -> impl Iterator<Item = &TransportAddrInfo> {
26        self.addrs.iter()
27    }
28
29    /// Converts into an iterator over known all addresses for this remote.
30    ///
31    /// Note that this may include outdated or unusable addresses. You can use [`TransportAddrInfo::usage`]
32    /// to filter for addresses that are actively used.
33    ///
34    /// You can use this to construct an [`EndpointAddr`] for this remote:
35    ///
36    /// ```no_run
37    /// # #[cfg(with_crypto_provider)]
38    /// # {
39    /// # use iroh::{Endpoint, EndpointId, EndpointAddr, endpoint::presets};
40    /// # #[tokio::main]
41    /// # async fn main() {
42    /// # let endpoint = Endpoint::bind(presets::N0).await.unwrap();
43    /// # let remote_id = EndpointId::from_bytes(&[0u8; 32]).unwrap();
44    /// let info = endpoint.remote_info(remote_id).await.unwrap();
45    /// let addr = EndpointAddr::from_parts(info.id(), info.into_addrs().map(|addr| addr.into_addr()));
46    /// # }
47    /// # }
48    /// ```
49    ///
50    /// [`EndpointAddr`]: crate::EndpointAddr
51    pub fn into_addrs(self) -> impl Iterator<Item = TransportAddrInfo> {
52        self.addrs.into_iter()
53    }
54}
55
56/// Address of a remote with some metadata
57#[derive(Debug, Clone)]
58pub struct TransportAddrInfo {
59    pub(super) addr: TransportAddr,
60    pub(super) usage: TransportAddrUsage,
61}
62
63impl TransportAddrInfo {
64    /// Returns the [`TransportAddr`].
65    pub fn addr(&self) -> &TransportAddr {
66        &self.addr
67    }
68
69    /// Converts into [`TransportAddr`].
70    pub fn into_addr(self) -> TransportAddr {
71        self.addr
72    }
73
74    /// Returns information how this address is used.
75    pub fn usage(&self) -> TransportAddrUsage {
76        self.usage
77    }
78}
79
80impl From<TransportAddrInfo> for TransportAddr {
81    fn from(value: TransportAddrInfo) -> Self {
82        value.addr
83    }
84}
85
86/// Information how a transport address is used.
87#[derive(Debug, Copy, Clone)]
88#[non_exhaustive]
89pub enum TransportAddrUsage {
90    /// The address is in active use.
91    Active,
92    /// The address is not currently used.
93    Inactive,
94}