Skip to content

Commit 50fdda3

Browse files
refactor: disallow certain Source variants to be constructed externally
Fixes #3639
1 parent d0707e8 commit 50fdda3

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

iroh/src/magicsock/endpoint_map.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,8 @@ impl EndpointMap {
209209
/// address through multiple means.
210210
#[derive(Serialize, Deserialize, strum::Display, Debug, Clone, Eq, PartialEq, Hash)]
211211
#[strum(serialize_all = "kebab-case")]
212+
#[allow(private_interfaces)]
212213
pub enum Source {
213-
/// Address was loaded from the fs.
214-
Saved,
215214
/// An endpoint communicated with us first via UDP.
216215
Udp,
217216
/// An endpoint communicated with us first via relay.
@@ -231,18 +230,34 @@ pub enum Source {
231230
name: String,
232231
},
233232
/// The address was advertised by a call-me-maybe DISCO message.
234-
CallMeMaybe,
233+
#[strum(serialize = "CallMeMaybe")]
234+
CallMeMaybe {
235+
/// private marker
236+
_0: Private,
237+
},
235238
/// We received a ping on the path.
236-
Ping,
239+
#[strum(serialize = "Ping")]
240+
Ping {
241+
/// private marker
242+
_0: Private,
243+
},
237244
/// We established a connection on this address.
238245
///
239246
/// Currently this means the path was in uses as [`PathId::ZERO`] when the a connection
240247
/// was added to the `EndpointStateActor`.
241248
///
242249
/// [`PathId::ZERO`]: quinn_proto::PathId::ZERO
243-
Connection,
250+
#[strum(serialize = "Connection")]
251+
Connection {
252+
/// private marker
253+
_0: Private,
254+
},
244255
}
245256

257+
/// Helper to ensure certain `Source` variants can not be constructed externally.
258+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
259+
struct Private;
260+
246261
/// An (Ip, Port) pair.
247262
///
248263
/// NOTE: storing an [`IpPort`] is safer than storing a [`SocketAddr`] because for IPv6 socket

iroh/src/magicsock/endpoint_map/endpoint_state.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{
3131
endpoint::DirectAddr,
3232
magicsock::{
3333
DiscoState, HEARTBEAT_INTERVAL, MagicsockMetrics, PATH_MAX_IDLE_TIMEOUT,
34+
endpoint_map::Private,
3435
mapped_addrs::{AddrMap, MappedAddr, RelayMappedAddr},
3536
transports::{self, OwnedTransmit, TransportsSender},
3637
},
@@ -378,7 +379,7 @@ impl EndpointStateActor {
378379
.entry(path_remote)
379380
.or_default()
380381
.sources
381-
.insert(Source::Connection, Instant::now());
382+
.insert(Source::Connection { _0: Private }, Instant::now());
382383
self.select_path();
383384

384385
if path_remote_is_ip {
@@ -439,7 +440,8 @@ impl EndpointStateActor {
439440
let ping = disco::Ping::new(self.local_endpoint_id);
440441

441442
let path = self.paths.entry(dst.clone()).or_default();
442-
path.sources.insert(Source::CallMeMaybe, now);
443+
path.sources
444+
.insert(Source::CallMeMaybe { _0: Private }, now);
443445
path.ping_sent = Some(ping.tx_id);
444446

445447
event!(
@@ -481,7 +483,8 @@ impl EndpointStateActor {
481483
.await;
482484

483485
let path = self.paths.entry(src).or_default();
484-
path.sources.insert(Source::Ping, Instant::now());
486+
path.sources
487+
.insert(Source::Ping { _0: Private }, Instant::now());
485488

486489
trace!("ping received, triggering holepunching");
487490
self.trigger_holepunching().await;
@@ -621,12 +624,12 @@ impl EndpointStateActor {
621624
.filter_map(|(addr, state)| {
622625
if state
623626
.sources
624-
.get(&Source::CallMeMaybe)
627+
.get(&Source::CallMeMaybe { _0: Private })
625628
.map(|when| when.elapsed() <= CALL_ME_MAYBE_VALIDITY)
626629
.unwrap_or_default()
627630
|| state
628631
.sources
629-
.get(&Source::Ping)
632+
.get(&Source::Ping { _0: Private })
630633
.map(|when| when.elapsed() <= CALL_ME_MAYBE_VALIDITY)
631634
.unwrap_or_default()
632635
{
@@ -824,7 +827,7 @@ impl EndpointStateActor {
824827
.entry(path_remote.clone())
825828
.or_default()
826829
.sources
827-
.insert(Source::Connection, Instant::now());
830+
.insert(Source::Connection { _0: Private }, Instant::now());
828831
}
829832

830833
self.select_path();

0 commit comments

Comments
 (0)