Skip to content

Commit 6d49482

Browse files
author
“ramfox”
committed
refactor: remove magicsock / quinn::Endpoint circular dependency
`MagicSock::spawn` creates a `quinn::Endpoint`. `MagicSock` is now an `AsyncUdpSocket`, and can be passed into the `quinn::Endpoint`. `magicsock::Handle` now owns the `quinn::Endpoint`, and `iroh::Endpoint` interacts with the `quinn::Endpoint` through `Handle::endpoint()`. This allows us to pass the `quinn::Endpoint` to the `magicsock::Actor` for use in QAD, without any circular dependencies.
1 parent 2f08eaf commit 6d49482

File tree

5 files changed

+145
-143
lines changed

5 files changed

+145
-143
lines changed

iroh-net-report/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl Actor {
645645
quic_config,
646646
..
647647
} = opts;
648-
trace!("Attempting probes for protocols {protos:#?}");
648+
trace!("Attempting probes for protocols {protocols:#?}");
649649
if self.current_report_run.is_some() {
650650
response_tx
651651
.send(Err(anyhow!(

iroh/examples/listen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
2828
// Use `RelayMode::Custom` to pass in a `RelayMap` with custom relay urls.
2929
// Use `RelayMode::Disable` to disable holepunching and relaying over HTTPS
3030
// If you want to experiment with relaying using your own relay server, you must pass in the same custom relay url to both the `listen` code AND the `connect` code
31-
.relay_mode(RelayMode::Default)
31+
.relay_mode(RelayMode::Staging)
3232
// you can choose a port to bind to, but passing in `0` will bind the socket to a random available port
3333
.bind()
3434
.await?;

iroh/src/endpoint.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ impl Builder {
163163
1 => Some(discovery.into_iter().next().expect("checked length")),
164164
_ => Some(Box::new(ConcurrentDiscovery::from_services(discovery))),
165165
};
166+
let server_config = static_config.create_server_config(self.alpn_protocols)?;
167+
166168
let msock_opts = magicsock::Options {
167169
addr_v4: self.addr_v4,
168170
addr_v6: self.addr_v6,
@@ -172,12 +174,13 @@ impl Builder {
172174
discovery,
173175
proxy_url: self.proxy_url,
174176
dns_resolver,
177+
server_config,
175178
#[cfg(any(test, feature = "test-utils"))]
176179
insecure_skip_relay_cert_verify: self.insecure_skip_relay_cert_verify,
177180
#[cfg(any(test, feature = "test-utils"))]
178181
path_selection: self.path_selection,
179182
};
180-
Endpoint::bind(static_config, msock_opts, self.alpn_protocols).await
183+
Endpoint::bind(static_config, msock_opts).await
181184
}
182185

183186
// # The very common methods everyone basically needs.
@@ -442,7 +445,6 @@ impl Builder {
442445
self
443446
}
444447
}
445-
446448
/// Configuration for a [`quinn::Endpoint`] that cannot be changed at runtime.
447449
#[derive(Debug)]
448450
struct StaticConfig {
@@ -453,7 +455,7 @@ struct StaticConfig {
453455

454456
impl StaticConfig {
455457
/// Create a [`quinn::ServerConfig`] with the specified ALPN protocols.
456-
fn create_server_config(&self, alpn_protocols: Vec<Vec<u8>>) -> Result<ServerConfig> {
458+
fn create_server_config(&self, alpn_protocols: Vec<Vec<u8>>) -> Result<quinn::ServerConfig> {
457459
let server_config = make_server_config(
458460
&self.secret_key,
459461
alpn_protocols,
@@ -506,7 +508,6 @@ pub fn make_server_config(
506508
#[derive(Clone, Debug)]
507509
pub struct Endpoint {
508510
msock: Handle,
509-
endpoint: quinn::Endpoint,
510511
rtt_actor: Arc<rtt_actor::RttHandle>,
511512
static_config: Arc<StaticConfig>,
512513
}
@@ -528,39 +529,16 @@ impl Endpoint {
528529
/// This is for internal use, the public interface is the [`Builder`] obtained from
529530
/// [Self::builder]. See the methods on the builder for documentation of the parameters.
530531
#[instrument("ep", skip_all, fields(me = %static_config.secret_key.public().fmt_short()))]
531-
async fn bind(
532-
static_config: StaticConfig,
533-
msock_opts: magicsock::Options,
534-
initial_alpns: Vec<Vec<u8>>,
535-
) -> Result<Self> {
532+
async fn bind(static_config: StaticConfig, msock_opts: magicsock::Options) -> Result<Self> {
536533
let msock = magicsock::MagicSock::spawn(msock_opts).await?;
537534
trace!("created magicsock");
538-
539-
let server_config = static_config.create_server_config(initial_alpns)?;
540-
541-
let mut endpoint_config = quinn::EndpointConfig::default();
542-
// Setting this to false means that quinn will ignore packets that have the QUIC fixed bit
543-
// set to 0. The fixed bit is the 3rd bit of the first byte of a packet.
544-
// For performance reasons and to not rewrite buffers we pass non-QUIC UDP packets straight
545-
// through to quinn. We set the first byte of the packet to zero, which makes quinn ignore
546-
// the packet if grease_quic_bit is set to false.
547-
endpoint_config.grease_quic_bit(false);
548-
549-
let endpoint = quinn::Endpoint::new_with_abstract_socket(
550-
endpoint_config,
551-
Some(server_config),
552-
Arc::new(msock.clone()),
553-
Arc::new(quinn::TokioRuntime),
554-
)?;
555535
trace!("created quinn endpoint");
556536
debug!(version = env!("CARGO_PKG_VERSION"), "iroh Endpoint created");
557537
let ep = Self {
558538
msock: msock.clone(),
559-
endpoint: endpoint.clone(),
560539
rtt_actor: Arc::new(rtt_actor::RttHandle::new()),
561540
static_config: Arc::new(static_config),
562541
};
563-
msock.set_quic_endpoint(Some(endpoint));
564542
Ok(ep)
565543
}
566544

@@ -570,7 +548,7 @@ impl Endpoint {
570548
/// Note that this *overrides* the current list of ALPNs.
571549
pub fn set_alpns(&self, alpns: Vec<Vec<u8>>) -> Result<()> {
572550
let server_config = self.static_config.create_server_config(alpns)?;
573-
self.endpoint.set_server_config(Some(server_config));
551+
self.msock.endpoint().set_server_config(Some(server_config));
574552
Ok(())
575553
}
576554

@@ -668,7 +646,8 @@ impl Endpoint {
668646

669647
// TODO: We'd eventually want to replace "localhost" with something that makes more sense.
670648
let connect = self
671-
.endpoint
649+
.msock
650+
.endpoint()
672651
.connect_with(client_config, addr.0, "localhost")?;
673652

674653
let connection = connect
@@ -698,7 +677,7 @@ impl Endpoint {
698677
/// [`Endpoint::close`].
699678
pub fn accept(&self) -> Accept<'_> {
700679
Accept {
701-
inner: self.endpoint.accept(),
680+
inner: self.msock.endpoint().accept(),
702681
ep: self.clone(),
703682
}
704683
}
@@ -1066,7 +1045,7 @@ impl Endpoint {
10661045
}
10671046
#[cfg(test)]
10681047
pub(crate) fn endpoint(&self) -> &quinn::Endpoint {
1069-
&self.endpoint
1048+
self.msock.endpoint()
10701049
}
10711050
}
10721051

0 commit comments

Comments
 (0)