Skip to content

Commit

Permalink
feat(iroh-net): add more details to tracked endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jun 29, 2023
1 parent 0be0480 commit dfd946e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions iroh-net/src/hp/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ mod timer;
mod udp_actor;

pub use self::conn::{Conn, Options};
pub use self::endpoint::EndpointInfo;
pub use self::timer::Timer;
20 changes: 11 additions & 9 deletions iroh-net/src/hp/magicsock/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{

use super::{
derp_actor::{DerpActor, DerpActorMessage, DerpReadResult},
endpoint::{Options as EndpointOptions, PeerMap},
endpoint::{EndpointInfo, Options as EndpointOptions, PeerMap},
rebinding_conn::RebindingUdpConn,
udp_actor::{IpPacket, NetworkReadResult, NetworkSource, UdpActor, UdpActorMessage},
};
Expand Down Expand Up @@ -365,7 +365,7 @@ impl Conn {
Ok(c)
}

pub async fn tracked_endpoints(&self) -> Result<Vec<key::node::PublicKey>> {
pub async fn tracked_endpoints(&self) -> Result<Vec<EndpointInfo>> {
let (s, r) = sync::oneshot::channel();
self.actor_sender
.send(ActorMessage::TrackedEndpoints(s))
Expand Down Expand Up @@ -741,7 +741,7 @@ impl Drop for WgGuard {
#[derive(Debug)]
pub(super) enum ActorMessage {
SetDerpMap(Option<DerpMap>, sync::oneshot::Sender<()>),
TrackedEndpoints(sync::oneshot::Sender<Vec<key::node::PublicKey>>),
TrackedEndpoints(sync::oneshot::Sender<Vec<EndpointInfo>>),
LocalEndpoints(sync::oneshot::Sender<Vec<cfg::Endpoint>>),
GetMappingAddr(
key::node::PublicKey,
Expand Down Expand Up @@ -893,11 +893,7 @@ impl Actor {
let _ = s.send(());
}
ActorMessage::TrackedEndpoints(s) => {
let eps: Vec<_> = self
.peer_map
.endpoints()
.map(|(_, ep)| ep.public_key.clone())
.collect();
let eps: Vec<_> = self.peer_map.endpoints().map(|(_, ep)| ep.info()).collect();
let _ = s.send(eps);
}
ActorMessage::LocalEndpoints(s) => {
Expand Down Expand Up @@ -2739,7 +2735,13 @@ mod tests {
}

async fn tracked_endpoints(&self) -> Vec<key::node::PublicKey> {
self.conn.tracked_endpoints().await.unwrap_or_default()
self.conn
.tracked_endpoints()
.await
.unwrap_or_default()
.into_iter()
.map(|ep| ep.public_key)
.collect()
}

fn public(&self) -> key::node::PublicKey {
Expand Down
35 changes: 35 additions & 0 deletions iroh-net/src/hp/magicsock/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ impl Endpoint {
&self.public_key
}

/// Returns info about this endpoint
pub fn info(&self) -> EndpointInfo {
let addrs = self
.endpoint_state
.keys()
.filter_map(|addr| match addr {
SendAddr::Udp(addr) => Some(*addr),
_ => None,
})
.collect();

EndpointInfo {
public_key: self.public_key.clone(),
derp_addr: self.derp_addr,
addrs,
has_direct_connection: self.is_best_addr_valid(Instant::now()),
latency: self.best_addr.as_ref().and_then(|a| a.latency),
}
}

/// Returns the address(es) that should be used for sending the next packet.
/// Zero, one, or both of UDP address and DERP addr may be non-zero.
fn addr_for_send(&mut self, now: &Instant) -> (Option<SocketAddr>, Option<u16>, bool) {
Expand Down Expand Up @@ -1008,6 +1028,21 @@ struct EndpointState {
index: Index,
}

/// Details about an Endpoint
#[derive(Debug, Clone)]
pub struct EndpointInfo {
/// The public key of the endpoint.
pub public_key: key::node::PublicKey,
/// Derp region, if available.
pub derp_addr: Option<u16>,
/// List of addresses this node might be reachable under.
pub addrs: Vec<SocketAddr>,
/// Is this node currently direcly reachable?
pub has_direct_connection: bool,
/// Current latency information, for a direct connection if available.
pub latency: Option<Duration>,
}

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Hash)]
enum Index {
#[default]
Expand Down

0 comments on commit dfd946e

Please sign in to comment.