Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions dc/s2n-quic-dc/src/packet/uds/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
path::secret::schedule::Ciphersuite,
};
use s2n_codec::{DecoderBufferMut, DecoderBufferMutResult, DecoderError};
use s2n_quic_core::{dc::ApplicationParams, varint::VarInt};
use s2n_quic_core::{dc::ApplicationParams, time::Timestamp, varint::VarInt};

#[derive(Clone, Debug)]
pub struct Packet {
Expand All @@ -15,6 +15,7 @@ pub struct Packet {
export_secret: Vec<u8>,
application_params_version: u8,
application_params: ApplicationParams,
queue_time: Timestamp,
payload: Vec<u8>,
}

Expand Down Expand Up @@ -44,6 +45,11 @@ impl Packet {
&self.application_params
}

#[inline]
pub fn queue_time(&self) -> &Timestamp {
&self.queue_time
}

#[inline]
pub fn payload(&self) -> &[u8] {
&self.payload
Expand Down Expand Up @@ -75,6 +81,8 @@ impl Packet {

let (application_params, buffer) = buffer.decode::<ApplicationParams>()?;

let (queue_time, buffer) = buffer.decode::<Timestamp>()?;

let (payload_slice, buffer) = buffer.decode_slice_with_len_prefix::<VarInt>()?;
let payload = payload_slice.into_less_safe_slice().to_vec();

Expand All @@ -84,6 +92,7 @@ impl Packet {
export_secret,
application_params_version,
application_params,
queue_time,
payload,
};

Expand All @@ -93,6 +102,8 @@ impl Packet {

#[cfg(test)]
mod tests {
use std::time::Duration;

use crate::{
packet::uds::{
decoder,
Expand All @@ -101,14 +112,15 @@ mod tests {
path::secret::schedule::Ciphersuite,
};
use s2n_codec::{DecoderBufferMut, DecoderError, EncoderLenEstimator};
use s2n_quic_core::dc;
use s2n_quic_core::{dc, time::Timestamp};

#[test]
fn test_encode_decode() {
let ciphersuite = Ciphersuite::AES_GCM_128_SHA256;
let export_secret = b"secret_data";
let application_params = dc::testing::TEST_APPLICATION_PARAMS;
let payload = b"payload_with_data";
let time = unsafe { Timestamp::from_duration(Duration::new(10, 0)) };

// Encode
let mut estimator = EncoderLenEstimator::new(usize::MAX);
Expand All @@ -117,6 +129,7 @@ mod tests {
&ciphersuite,
export_secret,
&application_params,
time,
payload,
);
let mut buffer = vec![0u8; expected_size];
Expand All @@ -126,6 +139,7 @@ mod tests {
&ciphersuite,
export_secret,
&application_params,
time,
payload,
);
assert_eq!(encoded_size, expected_size);
Expand Down
5 changes: 4 additions & 1 deletion dc/s2n-quic-dc/src/packet/uds/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::path::secret::schedule::Ciphersuite;
use s2n_codec::Encoder;
use s2n_quic_core::{dc::ApplicationParams, varint::VarInt};
use s2n_quic_core::{dc::ApplicationParams, time::Timestamp, varint::VarInt};

pub const PACKET_VERSION: u8 = 0;
pub const APP_PARAMS_VERSION: u8 = 0;
Expand All @@ -14,6 +14,7 @@ pub fn encode<E: Encoder>(
ciphersuite: &Ciphersuite,
export_secret: &[u8],
application_params: &ApplicationParams,
queue_time: Timestamp,
payload: &[u8],
) -> usize {
let start_len = encoder.len();
Expand All @@ -29,6 +30,8 @@ pub fn encode<E: Encoder>(

encoder.encode(application_params);

encoder.encode(&queue_time);

encoder.encode_with_len_prefix::<VarInt, _>(&payload);

encoder.len() - start_len
Expand Down
2 changes: 2 additions & 0 deletions dc/s2n-quic-dc/src/stream/server/tokio/tcp/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ where
&ciphersuite,
&export_secret,
&application_params,
queue_time,
recv_buffer,
);
let mut buffer = vec![0u8; size];
Expand All @@ -798,6 +799,7 @@ where
&ciphersuite,
&export_secret,
&application_params,
queue_time,
recv_buffer,
);

Expand Down
19 changes: 19 additions & 0 deletions quic/s2n-quic-core/src/time/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

//! Defines time related datatypes and functions

use s2n_codec::{decoder_value, DecoderError, Encoder, EncoderValue};

use crate::recovery::K_GRANULARITY;
use core::{fmt, num::NonZeroU64, time::Duration};

Expand Down Expand Up @@ -186,6 +188,23 @@ impl core::ops::SubAssign<Duration> for Timestamp {
}
}

decoder_value!(
impl<'a> Timestamp {
fn decode(buffer: Buffer) -> Result<Self> {
let (time, buffer) = buffer.decode::<u64>()?;
let time = NonZeroU64::new(time)
.ok_or(DecoderError::InvariantViolation("Timestamp cannot be 0"))?;
Ok((Self(time), buffer))
}
}
);

impl EncoderValue for Timestamp {
fn encode<E: Encoder>(&self, buffer: &mut E) {
buffer.encode(&self.0.get());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we shouldn't add these impls. Instead, let's capture SystemTime before/after send/recv on the unix socket.

}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading