Skip to main content

soil_network/
network_state.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Information about the networking, for diagnostic purposes.
8//!
9//! **Warning**: These APIs are not stable.
10
11use libp2p::{
12	core::{ConnectedPoint, Endpoint as CoreEndpoint},
13	Multiaddr,
14};
15use serde::{Deserialize, Serialize};
16use std::{
17	collections::{HashMap, HashSet},
18	time::Duration,
19};
20
21/// Returns general information about the networking.
22///
23/// Meant for general diagnostic purposes.
24///
25/// **Warning**: This API is not stable.
26#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct NetworkState {
29	/// PeerId of the local node.
30	pub peer_id: String,
31	/// List of addresses the node is currently listening on.
32	pub listened_addresses: HashSet<Multiaddr>,
33	/// List of addresses the node knows it can be reached as.
34	pub external_addresses: HashSet<Multiaddr>,
35	/// List of node we're connected to.
36	pub connected_peers: HashMap<String, Peer>,
37	/// List of node that we know of but that we're not connected to.
38	pub not_connected_peers: HashMap<String, NotConnectedPeer>,
39	/// State of the peerset manager.
40	pub peerset: serde_json::Value,
41}
42
43/// Part of the `NetworkState` struct. Unstable.
44#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub struct Peer {
47	/// How we are connected to the node.
48	pub endpoint: PeerEndpoint,
49	/// Node information, as provided by the node itself. Can be empty if not known yet.
50	pub version_string: Option<String>,
51	/// Latest ping duration with this node.
52	pub latest_ping_time: Option<Duration>,
53	/// List of addresses known for this node.
54	pub known_addresses: HashSet<Multiaddr>,
55}
56
57/// Part of the `NetworkState` struct. Unstable.
58#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase")]
60pub struct NotConnectedPeer {
61	/// List of addresses known for this node.
62	pub known_addresses: HashSet<Multiaddr>,
63	/// Node information, as provided by the node itself, if we were ever connected to this node.
64	pub version_string: Option<String>,
65	/// Latest ping duration with this node, if we were ever connected to this node.
66	pub latest_ping_time: Option<Duration>,
67}
68
69/// Part of the `NetworkState` struct. Unstable.
70#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub enum PeerEndpoint {
73	/// We are dialing the given address.
74	Dialing(Multiaddr, Endpoint),
75	/// We are listening.
76	Listening {
77		/// Local address of the connection.
78		local_addr: Multiaddr,
79		/// Address data is sent back to.
80		send_back_addr: Multiaddr,
81	},
82}
83
84/// Part of the `NetworkState` struct. Unstable.
85#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub enum Endpoint {
88	/// The socket comes from a dialer.
89	Dialer,
90	/// The socket comes from a listener.
91	Listener,
92}
93
94impl From<ConnectedPoint> for PeerEndpoint {
95	fn from(endpoint: ConnectedPoint) -> Self {
96		match endpoint {
97			ConnectedPoint::Dialer { address, role_override, port_use: _ } => {
98				Self::Dialing(address, role_override.into())
99			},
100			ConnectedPoint::Listener { local_addr, send_back_addr } => {
101				Self::Listening { local_addr, send_back_addr }
102			},
103		}
104	}
105}
106
107impl From<CoreEndpoint> for Endpoint {
108	fn from(endpoint: CoreEndpoint) -> Self {
109		match endpoint {
110			CoreEndpoint::Dialer => Self::Dialer,
111			CoreEndpoint::Listener => Self::Listener,
112		}
113	}
114}