diff --git a/network/src/address.rs b/network/src/address.rs new file mode 100644 index 0000000000..fe83c03b61 --- /dev/null +++ b/network/src/address.rs @@ -0,0 +1,39 @@ +use p2p::multiaddr::{MultiAddr, Protocol}; + +pub struct NetworkAddresses { + pub regular_addresses: Vec, + + // onion addresses can't be solved by multiaddr_to_socketaddr or socketaddr_to_multiaddr + pub onion_addresses: Vec, +} + +// convert Vec to NetworkAddresses +impl From> for NetworkAddresses { + fn from(addresses: Vec) -> Self { + let mut regular_addresses = Vec::new(); + let mut onion_addresses = Vec::new(); + for address in addresses { + if address + .iter() + .any(|proto| matches!(proto, Protocol::Onion(_))) + { + onion_addresses.push(address); + } else { + regular_addresses.push(address); + } + } + NetworkAddresses { + regular_addresses, + onion_addresses, + } + } +} + +// convert NetworkAddresses to Vec +impl From for Vec { + fn from(addresses: NetworkAddresses) -> Self { + let mut result = addresses.regular_addresses; + result.extend(addresses.onion_addresses); + result + } +} diff --git a/network/src/lib.rs b/network/src/lib.rs index 90be01fbcd..db60bf34c7 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -6,6 +6,7 @@ //! And implemented several basic protocols: identify, discovery, ping, feeler, disconnect_message //! +mod address; mod behaviour; /// compress module pub mod compress; diff --git a/network/src/network.rs b/network/src/network.rs index 142523345c..a5cd199926 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,4 +1,5 @@ //! Global state struct and start function +use crate::address::NetworkAddresses; use crate::errors::Error; #[cfg(not(target_family = "wasm"))] use crate::errors::P2PError; @@ -74,7 +75,7 @@ pub struct NetworkState { pub(crate) peer_registry: RwLock, pub(crate) peer_store: Mutex, /// Node listened addresses - pub(crate) listened_addrs: RwLock>, + pub(crate) listened_addrs: RwLock, dialing_addrs: RwLock>, /// Node public addresses, /// includes manually public addrs and remote peer observed addrs diff --git a/network/src/peer.rs b/network/src/peer.rs index fe7ee3dfb3..3c649c7ef3 100644 --- a/network/src/peer.rs +++ b/network/src/peer.rs @@ -1,3 +1,4 @@ +use crate::address::NetworkAddresses; use crate::network_group::Group; use crate::{ multiaddr::Multiaddr, protocols::identify::Flags, ProtocolId, ProtocolVersion, SessionType, @@ -21,7 +22,7 @@ pub struct Peer { /// Peer address pub connected_addr: Multiaddr, /// Peer listen addresses - pub listened_addrs: Vec, + pub listened_addrs: NetworkAddresses, /// Peer info from identify protocol message pub identify_info: Option, /// Ping/Pong message last received time