Skip to content

Commit 906250b

Browse files
authored
refactor(iroh, iroh-net-report)!: Make ports more private (#3207)
## Description I find this whole port business of these mapped addresses very confusing. This attempts to document them better, make it clearer that these SocketAddrs are made up and are not routable. ## Breaking Changes ### iroh-net-report - `MAPPED_ADDR_PORT` is removed. - `IpMappedAddr::socket_addr` -> `IpMappedAddr::private_socket_addr` ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] All breaking changes documented. - [x] List all breaking changes in the above "Breaking Changes" section.
1 parent 2a49265 commit 906250b

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

iroh-net-report/src/ip_mapped_addrs.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ use std::{
77
},
88
};
99

10-
/// The dummy port used for all mapped addresses
11-
pub const MAPPED_ADDR_PORT: u16 = 12345;
12-
1310
/// Can occur when converting a [`SocketAddr`] to an [`IpMappedAddr`]
1411
#[derive(Debug, thiserror::Error)]
1512
#[error("Failed to convert")]
@@ -32,6 +29,9 @@ impl IpMappedAddr {
3229
/// The Subnet ID used in our Unique Local Addresses.
3330
const ADDR_SUBNET: [u8; 2] = [0, 1];
3431

32+
/// The dummy port used for all mapped addresses.
33+
const MAPPED_ADDR_PORT: u16 = 12345;
34+
3535
/// Generates a globally unique fake UDP address.
3636
///
3737
/// This generates a new IPv6 address in the Unique Local Address range (RFC 4193)
@@ -48,9 +48,15 @@ impl IpMappedAddr {
4848
Self(Ipv6Addr::from(addr))
4949
}
5050

51-
/// Return a [`SocketAddr`] from the [`IpMappedAddr`].
52-
pub fn socket_addr(&self) -> SocketAddr {
53-
SocketAddr::new(IpAddr::from(self.0), MAPPED_ADDR_PORT)
51+
/// Returns a consistent [`SocketAddr`] for the [`IpMappedAddr`].
52+
///
53+
/// This does not have a routable IP address.
54+
///
55+
/// This uses a made-up, but fixed port number. The [IpMappedAddresses`] map this is
56+
/// made for creates a unique [`IpMappedAddr`] for each IP+port and thus does not use
57+
/// the port to map back to the original [`SocketAddr`].
58+
pub fn private_socket_addr(&self) -> SocketAddr {
59+
SocketAddr::new(IpAddr::from(self.0), Self::MAPPED_ADDR_PORT)
5460
}
5561
}
5662

iroh-net-report/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub mod portmapper {
6464
}
6565
}
6666

67-
pub use ip_mapped_addrs::{IpMappedAddr, IpMappedAddrError, IpMappedAddresses, MAPPED_ADDR_PORT};
67+
pub use ip_mapped_addrs::{IpMappedAddr, IpMappedAddrError, IpMappedAddresses};
6868
pub use metrics::Metrics;
6969
pub use options::Options;
7070
pub use reportgen::QuicConfig;

iroh-net-report/src/reportgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ fn maybe_to_mapped_addr(
968968
addr: SocketAddr,
969969
) -> SocketAddr {
970970
if let Some(ip_mapped_addrs) = ip_mapped_addrs.as_ref() {
971-
return ip_mapped_addrs.get_or_register(addr).socket_addr();
971+
return ip_mapped_addrs.get_or_register(addr).private_socket_addr();
972972
}
973973
addr
974974
}

iroh/src/endpoint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl Endpoint {
754754
let server_name = &format!("{}.iroh.invalid", BASE32_DNSSEC.encode(node_id.as_bytes()));
755755
let connect = self.msock.endpoint().connect_with(
756756
client_config,
757-
mapped_addr.socket_addr(),
757+
mapped_addr.private_socket_addr(),
758758
server_name,
759759
)?;
760760

iroh/src/magicsock.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ impl MagicSock {
958958
"UDP recv QUIC address discovery packets",
959959
);
960960
quic_packets_total += quic_datagram_count;
961-
meta.addr = ip_mapped_addr.socket_addr();
961+
meta.addr = ip_mapped_addr.private_socket_addr();
962962
} else {
963963
warn!(
964964
src = ?meta.addr,
@@ -980,7 +980,7 @@ impl MagicSock {
980980
"UDP recv quic packets",
981981
);
982982
quic_packets_total += quic_datagram_count;
983-
meta.addr = quic_mapped_addr.socket_addr();
983+
meta.addr = quic_mapped_addr.private_socket_addr();
984984
}
985985
}
986986
} else {
@@ -1094,7 +1094,7 @@ impl MagicSock {
10941094
let meta = quinn_udp::RecvMeta {
10951095
len: dm.buf.len(),
10961096
stride: dm.buf.len(),
1097-
addr: quic_mapped_addr.socket_addr(),
1097+
addr: quic_mapped_addr.private_socket_addr(),
10981098
dst_ip,
10991099
ecn: None,
11001100
};
@@ -3211,9 +3211,6 @@ fn split_packets(transmit: &quinn_udp::Transmit) -> RelayContents {
32113211
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
32123212
pub(crate) struct NodeIdMappedAddr(Ipv6Addr);
32133213

3214-
/// The dummy port used for all [`NodeIdMappedAddr`]s
3215-
pub const NODE_ID_MAPPED_PORT: u16 = 12345;
3216-
32173214
/// Can occur when converting a [`SocketAddr`] to an [`NodeIdMappedAddr`]
32183215
#[derive(Debug, thiserror::Error)]
32193216
#[error("Failed to convert")]
@@ -3230,6 +3227,9 @@ impl NodeIdMappedAddr {
32303227
/// The Subnet ID used in our Unique Local Addresses.
32313228
const ADDR_SUBNET: [u8; 2] = [0; 2];
32323229

3230+
/// The dummy port used for all [`NodeIdMappedAddr`]s.
3231+
const NODE_ID_MAPPED_PORT: u16 = 12345;
3232+
32333233
/// Generates a globally unique fake UDP address.
32343234
///
32353235
/// This generates and IPv6 Unique Local Address according to RFC 4193.
@@ -3245,9 +3245,15 @@ impl NodeIdMappedAddr {
32453245
Self(Ipv6Addr::from(addr))
32463246
}
32473247

3248-
/// Return the [`SocketAddr`] from the [`NodeIdMappedAddr`]
3249-
pub(crate) fn socket_addr(&self) -> SocketAddr {
3250-
SocketAddr::new(IpAddr::from(self.0), NODE_ID_MAPPED_PORT)
3248+
/// Returns a consistent [`SocketAddr`] for the [`NodeIdMappedAddr`].
3249+
///
3250+
/// This socket address does not have a routable IP address.
3251+
///
3252+
/// This uses a made-up port number, since the port does not play a role in looking up
3253+
/// the node in the [`NodeMap`]. This socket address is only to be used to pass into
3254+
/// Quinn.
3255+
pub(crate) fn private_socket_addr(&self) -> SocketAddr {
3256+
SocketAddr::new(IpAddr::from(self.0), Self::NODE_ID_MAPPED_PORT)
32513257
}
32523258
}
32533259

@@ -4372,7 +4378,11 @@ mod tests {
43724378
tls::make_client_config(&ep_secret_key, Some(node_id), alpns, None, true)?;
43734379
let mut client_config = quinn::ClientConfig::new(Arc::new(quic_client_config));
43744380
client_config.transport_config(transport_config);
4375-
let connect = ep.connect_with(client_config, mapped_addr.socket_addr(), "localhost")?;
4381+
let connect = ep.connect_with(
4382+
client_config,
4383+
mapped_addr.private_socket_addr(),
4384+
"localhost",
4385+
)?;
43764386
let connection = connect.await?;
43774387
Ok(connection)
43784388
}

0 commit comments

Comments
 (0)