Skip to content

Commit cf58083

Browse files
committed
access application context from tls
1 parent adc7ba9 commit cf58083

File tree

9 files changed

+40
-2
lines changed

9 files changed

+40
-2
lines changed

quic/s2n-quic-core/src/crypto/tls.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use alloc::vec::Vec;
66
#[cfg(feature = "alloc")]
77
pub use bytes::{Bytes, BytesMut};
8-
use core::fmt::Debug;
8+
use core::{any::Any, fmt::Debug};
99
use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned};
1010

1111
mod error;
@@ -130,6 +130,8 @@ pub trait Context<Crypto: crate::crypto::CryptoSuite> {
130130
//# peer's Finished message.
131131
fn on_handshake_complete(&mut self) -> Result<(), crate::transport::Error>;
132132

133+
/// Transfer application context from TLS connection to quic connection
134+
fn on_application_context(&mut self, _context: Option<Box<dyn Any + Send + Sync>>) {}
133135
fn on_tls_exporter_ready(
134136
&mut self,
135137
session: &impl TlsSession,

quic/s2n-quic-tls/src/session.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use s2n_tls::{
1616
enums::{Blinding, Mode},
1717
error::{Error, ErrorType},
1818
};
19+
use std::any::Any;
1920

2021
#[derive(Debug)]
2122
pub struct Session {
@@ -155,6 +156,10 @@ impl tls::Session for Session {
155156
context.on_tls_exporter_ready(self)?;
156157
self.handshake_complete = true;
157158
}
159+
// TODO Add new s2n-tls new api, take and put in quic::connection
160+
let ctx: Option<Box<dyn Any + Send + Sync>> =
161+
self.connection.application_context_owned();
162+
context.on_application_context(ctx);
158163
Poll::Ready(Ok(()))
159164
}
160165
Poll::Ready(Err(e)) => Poll::Ready(Err(e

quic/s2n-quic-transport/src/connection/api_provider.rs

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use s2n_quic_core::{
2121
query::{Query, QueryMut},
2222
stream::{ops, StreamId, StreamType},
2323
};
24+
use std::any::Any;
2425

2526
/// A dynamically dispatched connection API
2627
pub(crate) type ConnectionApi = Arc<dyn ConnectionApiProvider>;
@@ -58,6 +59,10 @@ pub(crate) trait ConnectionApiProvider: Sync + Send {
5859

5960
fn application_protocol(&self) -> Result<Bytes, connection::Error>;
6061

62+
fn take_application_context(
63+
&mut self,
64+
) -> Result<Option<Box<dyn Any + Send + Sync>>, connection::Error>;
65+
6166
fn id(&self) -> u64;
6267

6368
fn ping(&self) -> Result<(), connection::Error>;

quic/s2n-quic-transport/src/connection/connection_container.rs

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use s2n_quic_core::{
4141
transport,
4242
};
4343
use smallvec::SmallVec;
44+
use std::any::Any;
4445

4546
// Intrusive list adapter for managing the list of `done` connections
4647
intrusive_adapter!(DoneConnectionsAdapter<C, L> = Arc<ConnectionNode<C, L>>: ConnectionNode<C, L> {
@@ -310,6 +311,12 @@ impl<C: connection::Trait, L: connection::Lock<C>> ConnectionApiProvider for Con
310311
self.api_read_call(|conn| Ok(conn.application_protocol()))
311312
}
312313

314+
fn take_application_context(
315+
&mut self,
316+
) -> Result<Option<Box<dyn Any + Send + Sync>>, connection::Error> {
317+
self.api_write_call(|conn| Ok(conn.take_application_context()))
318+
}
319+
313320
fn id(&self) -> u64 {
314321
self.internal_connection_id.into()
315322
}

quic/s2n-quic-transport/src/connection/connection_container/tests.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ impl connection::Trait for TestConnection {
6262
fn new(_params: connection::Parameters<Self::Config>) -> Result<Self, connection::Error> {
6363
Ok(Self::default())
6464
}
65-
65+
fn take_application_context(&mut self) -> Option<Box<dyn std::any::Any + Send + Sync>>{
66+
todo!()
67+
}
6668
fn internal_connection_id(&self) -> InternalConnectionId {
6769
todo!()
6870
}

quic/s2n-quic-transport/src/connection/connection_impl.rs

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use s2n_quic_core::{
6161
time::{timer, Timestamp},
6262
transport,
6363
};
64+
use std::any::Any;
6465

6566
/// Possible states for handing over a connection from the endpoint to the
6667
/// application.
@@ -1944,6 +1945,10 @@ impl<Config: endpoint::Config> connection::Trait for ConnectionImpl<Config> {
19441945
self.space_manager.application_protocol.clone()
19451946
}
19461947

1948+
fn take_application_context(&mut self) -> Option<Box<dyn Any + Send + Sync>> {
1949+
self.space_manager.application_context.take()
1950+
}
1951+
19471952
fn ping(&mut self) -> Result<(), connection::Error> {
19481953
self.error?;
19491954

quic/s2n-quic-transport/src/connection/connection_trait.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use s2n_quic_core::{
3636
query,
3737
time::Timestamp,
3838
};
39+
use std::any::Any;
3940

4041
/// A trait which represents an internally used `Connection`
4142
pub trait ConnectionTrait: 'static + Send + Sized {
@@ -505,6 +506,7 @@ pub trait ConnectionTrait: 'static + Send + Sized {
505506
>,
506507
&path::Path<Self::Config>,
507508
);
509+
fn take_application_context(&mut self) -> Option<Box<dyn Any + Send + Sync>>;
508510
}
509511

510512
/// A lock that synchronizes connection state between the QUIC endpoint thread and application

quic/s2n-quic-transport/src/space/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use core::{
1616
ops::RangeInclusive,
1717
task::{Poll, Waker},
1818
};
19+
use std::any::Any;
1920
use s2n_codec::DecoderBufferMut;
2021
use s2n_quic_core::{
2122
application::ServerName,
@@ -63,6 +64,7 @@ pub struct PacketSpaceManager<Config: endpoint::Config> {
6364
retry_cid: Option<Box<PeerId>>,
6465
initial: Option<Box<InitialSpace<Config>>>,
6566
handshake: Option<Box<HandshakeSpace<Config>>>,
67+
pub application_context: Option<Box<dyn Any + Send + Sync>>,
6668
application: Option<Box<ApplicationSpace<Config>>>,
6769
zero_rtt_crypto:
6870
Option<Box<<<Config::TLSEndpoint as tls::Endpoint>::Session as CryptoSuite>::ZeroRttKey>>,
@@ -124,6 +126,7 @@ impl<Config: endpoint::Config> PacketSpaceManager<Config> {
124126
session,
125127
initial_cid,
126128
}),
129+
application_context: None,
127130
retry_cid: None,
128131
initial: Some(Box::new(InitialSpace::new(
129132
initial_key,
@@ -258,6 +261,7 @@ impl<Config: endpoint::Config> PacketSpaceManager<Config> {
258261
handshake: &mut self.handshake,
259262
application: &mut self.application,
260263
zero_rtt_crypto: &mut self.zero_rtt_crypto,
264+
application_context: &mut self.application_context,
261265
path_manager,
262266
handshake_status: &mut self.handshake_status,
263267
local_id_registry,
@@ -303,6 +307,7 @@ impl<Config: endpoint::Config> PacketSpaceManager<Config> {
303307
retry_cid: self.retry_cid.as_deref(),
304308
initial: &mut self.initial,
305309
handshake: &mut self.handshake,
310+
application_context: &mut self.application_context,
306311
application: &mut self.application,
307312
zero_rtt_crypto: &mut self.zero_rtt_crypto,
308313
path_manager,

quic/s2n-quic-transport/src/space/session_context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use s2n_quic_core::{
4141
Error,
4242
},
4343
};
44+
use std::any::Any;
4445

4546
pub struct SessionContext<'a, Config: endpoint::Config, Pub: event::ConnectionPublisher> {
4647
pub now: Timestamp,
@@ -62,6 +63,7 @@ pub struct SessionContext<'a, Config: endpoint::Config, Pub: event::ConnectionPu
6263
pub publisher: &'a mut Pub,
6364
pub datagram: &'a mut Config::DatagramEndpoint,
6465
pub dc: &'a mut Config::DcEndpoint,
66+
pub application_context: &'a mut Option<Box<dyn Any + Send + Sync>>,
6567
}
6668

6769
impl<Config: endpoint::Config, Pub: event::ConnectionPublisher> SessionContext<'_, Config, Pub> {
@@ -697,4 +699,7 @@ impl<Config: endpoint::Config, Pub: event::ConnectionPublisher>
697699

698700
Ok(())
699701
}
702+
fn on_application_context(&mut self, context: Option<Box<dyn Any + Send + Sync>>) {
703+
*self.application_context = context;
704+
}
700705
}

0 commit comments

Comments
 (0)