Skip to content

Commit

Permalink
rename config::Endpoint to config::DirectAddress
Browse files Browse the repository at this point in the history
Also for EndpointType -> DirectAddressType
  • Loading branch information
flub committed Jun 17, 2024
1 parent 1d5450e commit 1390884
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
9 changes: 6 additions & 3 deletions iroh-gossip/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type ProtoMessage = proto::Message<PublicKey>;
#[derive(Debug, Clone)]
pub struct Gossip {
to_actor_tx: mpsc::Sender<ToActor>,
on_endpoints_tx: mpsc::Sender<Vec<iroh_net::config::Endpoint>>,
on_endpoints_tx: mpsc::Sender<Vec<iroh_net::config::DirectAddress>>,
_actor_handle: Arc<JoinHandle<anyhow::Result<()>>>,
max_message_size: usize,
}
Expand Down Expand Up @@ -247,7 +247,10 @@ impl Gossip {
/// to us.
///
/// This is only best effort, and will drop new events if backed up.
pub fn update_endpoints(&self, endpoints: &[iroh_net::config::Endpoint]) -> anyhow::Result<()> {
pub fn update_endpoints(
&self,
endpoints: &[iroh_net::config::DirectAddress],
) -> anyhow::Result<()> {
let endpoints = endpoints.to_vec();
self.on_endpoints_tx
.try_send(endpoints)
Expand Down Expand Up @@ -342,7 +345,7 @@ struct Actor {
/// Input events to the state (emitted from the connection loops)
in_event_rx: mpsc::Receiver<InEvent>,
/// Updates of discovered endpoint addresses
on_endpoints_rx: mpsc::Receiver<Vec<iroh_net::config::Endpoint>>,
on_endpoints_rx: mpsc::Receiver<Vec<iroh_net::config::DirectAddress>>,
/// Queued timers
timers: Timers<Timer>,
/// Currently opened quinn connections to peers
Expand Down
55 changes: 35 additions & 20 deletions iroh-net/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,56 @@ use crate::relay::RelayUrl;

use super::portmapper;

// TODO: This re-uses "Endpoint" again, a term that already means "a quic endpoint" and "a
// magicsock endpoint". this time it means "an IP address on which our local magicsock
// endpoint is listening". Name this better.
/// An endpoint IPPort and an associated type.
/// A *direct address* on which an iroh-node might be contactable.
///
/// Direct addresses are UDP socket addresses on which an iroh-net node could potentially be
/// contacted. These can come from various sources depending on the network topology of the
/// iroh-net node, see [`DirectAddressType`] for the several kinds of sources.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Endpoint {
/// The address of the endpoint.
pub struct DirectAddress {
/// The address.
pub addr: SocketAddr,
/// The kind of endpoint.
pub typ: EndpointType,
/// The origin of this direct address.
pub typ: DirectAddressType,
}

/// Type of endpoint.
/// The type of direct address.
///
/// These are the various sources or origins from which an iroh-net node might have found a
/// possible [`DirectAddress`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum EndpointType {
/// Endpoint kind has not been determined yet.
pub enum DirectAddressType {
/// Not yet determined..
Unknown,
/// Endpoint is bound to a local address.
/// A locally bound socket address.
Local,
/// Endpoint has a publicly reachable address found via STUN.
/// Public internet address discovered via STUN.
///
/// When possible an iroh-net node will perform STUN to discover which is the address
/// from which it sends data on the public internet. This can be different from locally
/// bound addresses when the node is on a local network wich performs NAT or similar.
Stun,
/// Endpoint uses a port mapping in the router.
/// An address assigned by the router using port mapping.
///
/// When possible an iroh-net node will request a port mapping from the local router to
/// get a publicly routable direct address.
Portmapped,
/// Hard NAT: STUN'ed IPv4 address + local fixed port.
///
/// It is possible to configure iroh-net to bound to a specific port and independently
/// configure the router to forward this port to the iroh-net node. This indicates a
/// situation like this, which still uses STUN to discover the public address.
Stun4LocalPort,
}

impl Display for EndpointType {
impl Display for DirectAddressType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EndpointType::Unknown => write!(f, "?"),
EndpointType::Local => write!(f, "local"),
EndpointType::Stun => write!(f, "stun"),
EndpointType::Portmapped => write!(f, "portmap"),
EndpointType::Stun4LocalPort => write!(f, "stun4localport"),
DirectAddressType::Unknown => write!(f, "?"),
DirectAddressType::Local => write!(f, "local"),
DirectAddressType::Stun => write!(f, "stun"),
DirectAddressType::Portmapped => write!(f, "portmap"),
DirectAddressType::Stun4LocalPort => write!(f, "stun4localport"),
}
}
}
Expand Down
49 changes: 32 additions & 17 deletions iroh-net/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ pub struct DirectAdressesStream {
}

impl Stream for DirectAdressesStream {
type Item = Vec<config::Endpoint>;
type Item = Vec<config::DirectAddress>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = &mut *self;
Expand Down Expand Up @@ -1583,7 +1583,7 @@ enum DiscoBoxError {
type RelayRecvResult = Result<(PublicKey, quinn_udp::RecvMeta, Bytes), io::Error>;

/// Reports whether x and y represent the same set of endpoints. The order doesn't matter.
fn endpoint_sets_equal(xs: &[config::Endpoint], ys: &[config::Endpoint]) -> bool {
fn endpoint_sets_equal(xs: &[config::DirectAddress], ys: &[config::DirectAddress]) -> bool {
if xs.is_empty() && ys.is_empty() {
return true;
}
Expand All @@ -1599,7 +1599,7 @@ fn endpoint_sets_equal(xs: &[config::Endpoint], ys: &[config::Endpoint]) -> bool
return true;
}
}
let mut m: HashMap<&config::Endpoint, usize> = HashMap::new();
let mut m: HashMap<&config::DirectAddress, usize> = HashMap::new();
for x in xs {
*m.entry(x).or_default() |= 1;
}
Expand Down Expand Up @@ -1963,7 +1963,7 @@ impl Actor {
#[allow(clippy::map_entry)]
if !$already.contains_key(&$ipp) {
$already.insert($ipp, $et);
$eps.push(config::Endpoint {
$eps.push(config::DirectAddress {
addr: $ipp,
typ: $et,
});
Expand All @@ -1974,13 +1974,23 @@ impl Actor {
let maybe_port_mapped = *portmap_watcher.borrow();

if let Some(portmap_ext) = maybe_port_mapped.map(SocketAddr::V4) {
add_addr!(already, eps, portmap_ext, config::EndpointType::Portmapped);
add_addr!(
already,
eps,
portmap_ext,
config::DirectAddressType::Portmapped
);
self.set_net_info_have_port_map().await;
}

if let Some(nr) = nr {
if let Some(global_v4) = nr.global_v4 {
add_addr!(already, eps, global_v4.into(), config::EndpointType::Stun);
add_addr!(
already,
eps,
global_v4.into(),
config::DirectAddressType::Stun
);

// If they're behind a hard NAT and are using a fixed
// port locally, assume they might've added a static
Expand All @@ -1994,12 +2004,17 @@ impl Actor {
already,
eps,
addr.into(),
config::EndpointType::Stun4LocalPort
config::DirectAddressType::Stun4LocalPort
);
}
}
if let Some(global_v6) = nr.global_v6 {
add_addr!(already, eps, global_v6.into(), config::EndpointType::Stun);
add_addr!(
already,
eps,
global_v6.into(),
config::DirectAddressType::Stun
);
}
}
let local_addr_v4 = self.pconn4.local_addr().ok();
Expand Down Expand Up @@ -2057,7 +2072,7 @@ impl Actor {
already,
eps,
SocketAddr::new(ip, port),
config::EndpointType::Local
config::DirectAddressType::Local
);
}
}
Expand All @@ -2067,7 +2082,7 @@ impl Actor {
already,
eps,
SocketAddr::new(ip, port),
config::EndpointType::Local
config::DirectAddressType::Local
);
}
}
Expand All @@ -2079,15 +2094,15 @@ impl Actor {
if let Some(addr) = local_addr_v4 {
// Our local endpoint is bound to a particular address.
// Do not offer addresses on other local interfaces.
add_addr!(already, eps, addr, config::EndpointType::Local);
add_addr!(already, eps, addr, config::DirectAddressType::Local);
}
}

if !is_unspecified_v6 {
if let Some(addr) = local_addr_v6 {
// Our local endpoint is bound to a particular address.
// Do not offer addresses on other local interfaces.
add_addr!(already, eps, addr, config::EndpointType::Local);
add_addr!(already, eps, addr, config::DirectAddressType::Local);
}
}

Expand Down Expand Up @@ -2409,7 +2424,7 @@ fn bind(port: u16) -> Result<(UdpConn, Option<UdpConn>)> {
struct DiscoveredEndpoints {
/// Records the endpoints found during the previous
/// endpoint discovery. It's used to avoid duplicate endpoint change notifications.
last_endpoints: Vec<config::Endpoint>,
last_endpoints: Vec<config::DirectAddress>,

/// The last time the endpoints were updated, even if there was no change.
last_endpoints_time: Option<Instant>,
Expand All @@ -2422,18 +2437,18 @@ impl PartialEq for DiscoveredEndpoints {
}

impl DiscoveredEndpoints {
fn new(endpoints: Vec<config::Endpoint>) -> Self {
fn new(endpoints: Vec<config::DirectAddress>) -> Self {
Self {
last_endpoints: endpoints,
last_endpoints_time: Some(Instant::now()),
}
}

fn into_iter(self) -> impl Iterator<Item = config::Endpoint> {
fn into_iter(self) -> impl Iterator<Item = config::DirectAddress> {
self.last_endpoints.into_iter()
}

fn iter(&self) -> impl Iterator<Item = &config::Endpoint> + '_ {
fn iter(&self) -> impl Iterator<Item = &config::DirectAddress> + '_ {
self.last_endpoints.iter()
}

Expand Down Expand Up @@ -2659,7 +2674,7 @@ pub(crate) mod tests {
#[instrument(skip_all)]
async fn mesh_stacks(stacks: Vec<MagicStack>) -> Result<CallOnDrop> {
/// Registers endpoint addresses of a node to all other nodes.
fn update_eps(stacks: &[MagicStack], my_idx: usize, new_eps: Vec<config::Endpoint>) {
fn update_eps(stacks: &[MagicStack], my_idx: usize, new_eps: Vec<config::DirectAddress>) {
let me = &stacks[my_idx];
for (i, m) in stacks.iter().enumerate() {
if i == my_idx {
Expand Down

0 comments on commit 1390884

Please sign in to comment.