diff options
Diffstat (limited to 'src/transport/libp2p.rs')
| -rw-r--r-- | src/transport/libp2p.rs | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/src/transport/libp2p.rs b/src/transport/libp2p.rs index 76d07ff..237fd8a 100644 --- a/src/transport/libp2p.rs +++ b/src/transport/libp2p.rs @@ -201,8 +201,8 @@ struct Book { failed_relays: HashMap<PeerId, tokio::time::Instant>, /// Relays that granted us a slot we hold. slots: std::collections::HashSet<PeerId>, - /// Open connections: to whom, and whether through a relay. - connections: HashMap<ConnectionId, (PeerId, bool)>, + /// Open connections: to whom, whether through a relay, and the remote address. + connections: HashMap<ConnectionId, (PeerId, bool, Multiaddr)>, /// What AutoNAT says, and the address it confirmed while public. reachability: Reachability, public_address: Option<Multiaddr>, @@ -245,7 +245,7 @@ impl Book { /// How we are connected to `peer`: directly if any connection is direct. fn connection(&self, peer: &PeerId) -> Option<Connection> { let mut found = None; - for (connected, relayed) in self.connections.values() { + for (connected, relayed, _) in self.connections.values() { if connected == peer { if !relayed { return Some(Connection::Direct); @@ -256,9 +256,23 @@ impl Book { found } + /// The remote address of a connection to `peer`: a direct one if there is one. + fn location(&self, peer: &PeerId) -> Option<&Multiaddr> { + let mut found = None; + for (connected, relayed, address) in self.connections.values() { + if connected == peer { + if !relayed { + return Some(address); + } + found = Some(address); + } + } + found + } + fn stats(&self) -> Libp2pStats { let mut connections = Connections::default(); - for (_, relayed) in self.connections.values() { + for (_, relayed, _) in self.connections.values() { if *relayed { connections.relayed += 1; } else { @@ -966,7 +980,10 @@ async fn run( let mut book = lock(&book); book.touch(peer_id); let relayed = endpoint.is_relayed(); - book.connections.insert(connection_id, (peer_id, relayed)); + book.connections.insert( + connection_id, + (peer_id, relayed, endpoint.get_remote_address().clone()), + ); debug!( peer = %peer_id, connection = %connection_id, @@ -1297,6 +1314,11 @@ impl Transport for Libp2pTransport { lock(&self.book).connection(&peer) } + fn location(&self, target: &str) -> Option<String> { + let peer = PeerId::from_str(target).ok()?; + lock(&self.book).location(&peer).map(Multiaddr::to_string) + } + fn stats(&self) -> TransportStats { TransportStats { libp2p: Some(lock(&self.book).stats()), @@ -1826,8 +1848,9 @@ mod tests { let mut id = 0; let mut connect = |book: &mut Book, peer: PeerId, relayed: bool| { id += 1; + let address: Multiaddr = format!("/ip4/10.0.0.{id}/tcp/1").parse().unwrap(); book.connections - .insert(ConnectionId::new_unchecked(id), (peer, relayed)); + .insert(ConnectionId::new_unchecked(id), (peer, relayed, address)); }; connect(&mut book, relayed_only, true); connect(&mut book, both, true); @@ -1835,6 +1858,10 @@ mod tests { assert_eq!(book.connection(&relayed_only), Some(Connection::Relayed)); assert_eq!(book.connection(&both), Some(Connection::Direct)); assert_eq!(book.connection(&peer()), None); + assert_eq!( + book.location(&both).map(|address| address.to_string()), + Some("/ip4/10.0.0.3/tcp/1".to_owned()) + ); let connections = book.stats().connections; assert_eq!((connections.direct, connections.relayed), (1, 2)); } |
