Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions neqo-transport/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,12 +713,18 @@ impl Connection {
self.crypto.tls.peer_certificate()
}

/// Call by application when the peer cert has been verified
/// Call by application when the peer cert has been verified.
///
/// This panics if there is no active peer. It's OK to call this
/// when authentication isn't needed, that will likely only cause
/// the connection to fail. However, if no packets have been
/// exchanged, it's not OK.
pub fn authenticated(&mut self, status: AuthenticationStatus, now: Instant) {
qinfo!([self], "Authenticated {:?}", status);
self.crypto.tls.authenticated(status);
let res = self.handshake(now, PNSpace::Handshake, None);
self.absorb_error(now, res);
self.process_saved(now);
}

/// Get the role of the connection.
Expand Down Expand Up @@ -828,11 +834,11 @@ impl Connection {
}
}

/// Call in to process activity on the connection. Either new packets have
/// arrived or a timeout has expired (or both).
/// Process new input datagrams on the connection.
pub fn process_input(&mut self, d: Datagram, now: Instant) {
let res = self.input(d, now);
self.absorb_error(now, res);
self.process_saved(now);
self.cleanup_streams();
}

Expand Down Expand Up @@ -925,6 +931,7 @@ impl Connection {
if let Some(d) = dgram {
let res = self.input(d, now);
self.absorb_error(now, res);
self.process_saved(now);
}
self.process_output(now)
}
Expand Down Expand Up @@ -1030,12 +1037,15 @@ impl Connection {
}
}

/// Process any saved packets if the appropriate keys are available.
fn process_saved(&mut self, cspace: CryptoSpace) {
if self.crypto.states.rx_hp(cspace).is_some() {
for saved in self.saved_datagrams.take_saved(cspace) {
qtrace!([self], "process saved @{:?}: {:?}", saved.t, saved.d);
self.process_input(saved.d, saved.t);
/// Process any saved datagrams that might be available for processing.
fn process_saved(&mut self, now: Instant) {
while let Some(cspace) = self.saved_datagrams.available() {
qdebug!([self], "process saved for space {:?}", cspace);
debug_assert!(self.crypto.states.rx_hp(cspace).is_some());
for saved in self.saved_datagrams.take_saved() {
qtrace!([self], "input saved @{:?}: {:?}", saved.t, saved.d);
let res = self.input(saved.d, saved.t);
self.absorb_error(now, res);
}
}
}
Expand Down Expand Up @@ -1339,10 +1349,11 @@ impl Connection {
}

fn start_handshake(&mut self, packet: &PublicPacket, d: &Datagram) -> Res<()> {
qtrace!([self], "starting handshake");
debug_assert_eq!(packet.packet_type(), PacketType::Initial);
self.remote_initial_source_cid = Some(ConnectionId::from(packet.scid()));
if self.role == Role::Server {
assert_eq!(packet.packet_type(), PacketType::Initial);

if self.role == Role::Server {
// A server needs to accept the client's selected CID during the handshake.
self.valid_cids.push(ConnectionId::from(packet.dcid()));
// Install a path.
Expand All @@ -1364,6 +1375,7 @@ impl Connection {
.expect("should have a path for sending Initial");
p.set_remote_cid(packet.scid());
}

self.set_state(State::Handshaking);
Ok(())
}
Expand Down Expand Up @@ -1886,7 +1898,7 @@ impl Connection {
self.set_initial_limits();
}
if self.crypto.install_keys(self.role)? {
self.process_saved(CryptoSpace::Handshake);
self.saved_datagrams.make_available(CryptoSpace::Handshake);
}
}

Expand Down Expand Up @@ -2282,7 +2294,8 @@ impl Connection {
self.process_tps()?;
self.set_state(State::Connected);
self.create_resumption_token(now);
self.process_saved(CryptoSpace::ApplicationData);
self.saved_datagrams
.make_available(CryptoSpace::ApplicationData);
self.stats.borrow_mut().resumed = self.crypto.tls.info().unwrap().resumed();
if self.role == Role::Server {
self.state_signaling.handshake_done();
Expand Down
19 changes: 17 additions & 2 deletions neqo-transport/src/connection/saved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct SavedDatagram {
pub struct SavedDatagrams {
handshake: Vec<SavedDatagram>,
application_data: Vec<SavedDatagram>,
available: Option<CryptoSpace>,
}

impl SavedDatagrams {
Expand All @@ -51,7 +52,21 @@ impl SavedDatagrams {
}
}

pub fn take_saved(&mut self, cspace: CryptoSpace) -> Vec<SavedDatagram> {
mem::take(self.store(cspace))
pub fn make_available(&mut self, cspace: CryptoSpace) {
debug_assert_ne!(cspace, CryptoSpace::ZeroRtt);
debug_assert_ne!(cspace, CryptoSpace::Initial);
if !self.store(cspace).is_empty() {
self.available = Some(cspace);
}
}

pub fn available(&self) -> Option<CryptoSpace> {
self.available
}

pub fn take_saved(&mut self) -> Vec<SavedDatagram> {
self.available
.take()
.map_or_else(Vec::new, |cspace| mem::take(self.store(cspace)))
}
}