Skip to content

Commit

Permalink
added first version of packet builder with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilumary committed Jul 28, 2024
1 parent 56c9efe commit 8279ac0
Show file tree
Hide file tree
Showing 2 changed files with 330 additions and 96 deletions.
54 changes: 3 additions & 51 deletions project/src/quic/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl ServerConfig {
pub fn local_server(addr: &str) -> Self {
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
let key = rustls::pki_types::PrivateKeyDer::Pkcs8(cert.key_pair.serialize_der().into());
println!("{:x?}", cert.cert.der());
let cert = rustls::pki_types::CertificateDer::from(cert.cert);

Self::new(addr, cert, key)
Expand Down Expand Up @@ -314,7 +315,7 @@ impl Connection {
//cut off trailing 0s from buffer, substract 1 extra beacuse packet num length of 1 is
//encoded as 0...
let (mut payload_cipher, _) = payload_cipher
.split_at(head.packet_length - head.packet_num_length as usize - 1)
.split_at(head.length - head.packet_num_length as usize - 1)
.unwrap();

//payload cipher must be exact size without zeros from the buffer beeing to big!
Expand Down Expand Up @@ -587,7 +588,6 @@ impl Inner {

//accepts new connection, passes payload to process_payload
fn accept(&mut self, header: &Header, packet_raw: &mut [u8]) -> Result<(), terror::Error> {
//initial packet cant be coalesced, TODO investigate for 0-rtt
self.process_payload(header, packet_raw)?;

self.generate_crypto_data(SPACE_ID_INITIAL);
Expand Down Expand Up @@ -851,7 +851,6 @@ impl Inner {
//pre-encode header
match Header::new_long_header(
0x00,
packet_num_length,
1,
self.packet_spaces[packet_number_space].next_pkt_num,
dcid,
Expand Down Expand Up @@ -1073,52 +1072,5 @@ impl From<Vec<u8>> for ConnectionId {

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

#[test]
fn create_long_initial_header() {
let result = Header::new_long_header(
0x00,
0x03,
1,
0,
&ConnectionId::from_vec(Vec::new()),
&ConnectionId::from_vec(Vec::new()),
None,
1280,
)
.unwrap();

assert_eq!(result.hf, 0b11000011);
}

#[test]
fn create_short_initial_header() {
let result = Header::new_short_header(
0x03,
0x01,
0x01,
1,
0,
&ConnectionId::from_vec(Vec::new()),
1280,
)
.unwrap();

assert_eq!(result.hf, 0b01100111);
}

#[test]
fn test_length_matching() {
let x = 256;
let mut length = 0;

match x {
..=0xff => length = 0,
0x0100..=0xffff => length = 1,
_ => unreachable!("unreachable size of next packet num"),
}

assert_eq!(length, 1);
}
// use super::*;
}
Loading

0 comments on commit 8279ac0

Please sign in to comment.