Skip to content

Commit

Permalink
refactored quic_error
Browse files Browse the repository at this point in the history
  • Loading branch information
ilumary committed May 30, 2024
1 parent 230b018 commit c84ca3e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
6 changes: 3 additions & 3 deletions project/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use quic::{quic_error, ServerConfig};
use quic::{terror, ServerConfig};

#[tokio::main]
async fn main() -> Result<(), quic_error::Error> {
async fn main() -> Result<(), terror::Error> {
let mut server = ServerConfig::local_server("127.0.0.1:34254")
.with_supported_protocols(vec!["hq-29".to_owned()])
.build()
Expand All @@ -10,7 +10,7 @@ async fn main() -> Result<(), quic_error::Error> {
while let Some(_connection) = server.accept().await {
println!("new connection!");
}

server.stop().await;

Ok(())
Expand Down
46 changes: 20 additions & 26 deletions project/src/quic/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod packet;
pub mod quic_error;
pub mod terror;
mod token;
mod transport_parameters;

Expand Down Expand Up @@ -147,7 +147,7 @@ impl ServerConfig {
self
}

pub async fn build(self) -> Result<Server, quic_error::Error> {
pub async fn build(self) -> Result<Server, terror::Error> {
let mut endpoint = Endpoint::new(
self.address.as_ref(),
Some(Arc::new(
Expand All @@ -167,8 +167,8 @@ pub struct Endpoint {
socket: Arc<TokioUdpSocket>,

//task handles for recv and send loops
recv_loop_handle: Option<JoinHandle<Result<u64, quic_error::Error>>>,
send_loop_handle: Option<JoinHandle<Result<u64, quic_error::Error>>>,
recv_loop_handle: Option<JoinHandle<Result<u64, terror::Error>>>,
send_loop_handle: Option<JoinHandle<Result<u64, terror::Error>>>,

//stores connection channel sender handles
distributor: TSDistributor,
Expand Down Expand Up @@ -273,7 +273,7 @@ impl Endpoint {
let size = match send_socket.send_to(&msg.0, &msg.1).await {
Ok(size) => size,
Err(error) => {
return Err(quic_error::Error::socket_error(format!("{}", error)));
return Err(terror::Error::socket_error(format!("{}", error)));
}
};

Expand Down Expand Up @@ -306,7 +306,7 @@ impl Connection {
async fn early_connection(
inital_datagram: packet::EarlyDatagram,
tsd: TSDistributor,
) -> Result<Self, quic_error::Error> {
) -> Result<Self, terror::Error> {
let (mut buffer, src_addr, mut head) = inital_datagram;
let (hmac_reset_key, server_config) = {
let t = tsd.read().await;
Expand Down Expand Up @@ -422,7 +422,7 @@ impl Connection {
version: Version,
side: Side,
dcid: &ConnectionId,
) -> Result<Keys, quic_error::Error> {
) -> Result<Keys, terror::Error> {
println!(
"{:?} available cipher suites: {:?}",
&server_cfg.crypto_provider().cipher_suites.len(),
Expand All @@ -432,9 +432,7 @@ impl Connection {
let cipher_suites = &server_cfg.crypto_provider().cipher_suites;

if cipher_suites.is_empty() {
return Err(quic_error::Error::no_cipher_suite(
"no supported cipher suites",
));
return Err(terror::Error::no_cipher_suite("no supported cipher suites"));
}

//use first availible tls 1.3 crypto suite for now
Expand All @@ -448,7 +446,7 @@ impl Connection {
));
}

Err(quic_error::Error::no_cipher_suite(
Err(terror::Error::no_cipher_suite(
"no available tls 1.3 cipher suite",
))
}
Expand All @@ -457,7 +455,7 @@ impl Connection {
async fn start(
mut self,
mut recv_q: mpsc::Receiver<packet::EarlyDatagram>,
) -> Result<Self, quic_error::Error> {
) -> Result<Self, terror::Error> {
self.inner.loop_handle = Some(tokio::spawn(async move {
while let Some(msg) = recv_q.recv().await {
println!("received datagram inside connection {:?}", msg.1);
Expand All @@ -479,7 +477,7 @@ impl Connection {
}

struct Inner {
loop_handle: Option<JoinHandle<Result<u64, quic_error::Error>>>,
loop_handle: Option<JoinHandle<Result<u64, terror::Error>>>,

//side
side: Side,
Expand Down Expand Up @@ -562,11 +560,7 @@ impl Inner {
}

//fully decrypts packet header and payload, does tls stuff and passes packet to process_payload
fn recv(
&mut self,
header: &mut Header,
payload_raw: &mut [u8],
) -> Result<(), quic_error::Error> {
fn recv(&mut self, header: &mut Header, payload_raw: &mut [u8]) -> Result<(), terror::Error> {
let mut payload = octets::OctetsMut::with_slice(payload_raw);
//decrypt header and payload

Expand All @@ -578,7 +572,7 @@ impl Inner {
}

//accepts new connection, passes payload to process_payload
fn accept(&mut self, header: &Header, payload_raw: &mut [u8]) -> Result<(), quic_error::Error> {
fn accept(&mut self, header: &Header, payload_raw: &mut [u8]) -> Result<(), terror::Error> {
let mut payload = octets::OctetsMut::with_slice(payload_raw);

self.process_payload(header, &mut payload);
Expand Down Expand Up @@ -778,7 +772,7 @@ impl Inner {
self.current_space += 1;
}

fn fill_datagram(&mut self, buffer: &mut [u8]) -> Result<usize, quic_error::Error> {
fn fill_datagram(&mut self, buffer: &mut [u8]) -> Result<usize, terror::Error> {
// detect if packet needs to be sent if current space is handshake and initial has outstanding_crypto_data
let mut size: usize = 0;

Expand All @@ -793,7 +787,7 @@ impl Inner {
&mut self,
buffer: &mut [u8],
packet_number_space: usize,
) -> Result<usize, quic_error::Error> {
) -> Result<usize, terror::Error> {
println!("Building packet in space {:#x}", packet_number_space);

let mut buf = octets::OctetsMut::with_slice(buffer);
Expand Down Expand Up @@ -827,7 +821,7 @@ impl Inner {
{
Ok(()) => (),
Err(error) => {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"buffer has unsufficient size to encode header: {}",
error
)))
Expand All @@ -851,7 +845,7 @@ impl Inner {
}

if (payload_length + header_length) > (max_packet_size - packet_num_length as usize) {
return Err(quic_error::Error::packet_size_error(format!(
return Err(terror::Error::packet_size_error(format!(
"max packet length exceeded with {}",
payload_length
)));
Expand All @@ -877,7 +871,7 @@ impl Inner {
&mut self,
buf: &mut octets::OctetsMut<'_>,
packet_number_space: usize,
) -> Result<usize, quic_error::Error> {
) -> Result<usize, terror::Error> {
let mut size = 0;

//CRYPTO
Expand All @@ -887,7 +881,7 @@ impl Inner {
match packet::encode_frame(&CryptoFrame::new(0, crypto_buffer.to_vec()), buf) {
Ok(s) => s,
Err(error) => {
return Err(quic_error::Error::packet_size_error(format!(
return Err(terror::Error::packet_size_error(format!(
"insufficient sized buffer for CRYPTO frame ({})",
error
)))
Expand Down Expand Up @@ -921,7 +915,7 @@ impl Inner {
let s = match packet::encode_frame(&ack_frame, buf) {
Ok(s) => s,
Err(error) => {
return Err(quic_error::Error::packet_size_error(format!(
return Err(terror::Error::packet_size_error(format!(
"insufficient sized buffer for ack frame ({})",
error
)))
Expand Down
16 changes: 8 additions & 8 deletions project/src/quic/packet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{quic_error, token::StatelessResetToken, ConnectionId, TSDistributor};
use crate::{terror, token::StatelessResetToken, ConnectionId, TSDistributor};

use rustls::quic::HeaderProtectionKey;

Expand Down Expand Up @@ -399,16 +399,16 @@ impl Header {
scid: &ConnectionId,
token: Option<Vec<u8>>,
packet_length: usize,
) -> Result<Self, quic_error::Error> {
) -> Result<Self, terror::Error> {
if !matches!(long_header_type, 0x00..=0x03) {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"unsupported long header type {:?}",
long_header_type
)));
}

if !matches!(packet_num_length, 0x00..=MAX_PKT_NUM_LEN) {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"unsupported packet number length {:?}",
packet_num_length
)));
Expand Down Expand Up @@ -437,23 +437,23 @@ impl Header {
packet_num: u32,
dcid: &ConnectionId,
packet_length: usize,
) -> Result<Self, quic_error::Error> {
) -> Result<Self, terror::Error> {
if !matches!(spin_bit, 0x00 | 0x01) {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"unsupported header spin bit {:?}",
spin_bit
)));
}

if !matches!(key_phase, 0x00 | 0x01) {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"unsupported header key phase {:?}",
key_phase
)));
}

if !matches!(packet_num_length, 0x00..=MAX_PKT_NUM_LEN) {
return Err(quic_error::Error::header_encoding_error(format!(
return Err(terror::Error::header_encoding_error(format!(
"unsupported packet number length {:?}",
packet_num_length
)));
Expand Down
17 changes: 8 additions & 9 deletions project/src/quic/quic_error.rs → project/src/quic/terror.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(non_snake_case)]
use std::error::Error as SError;
use std::fmt;

#[derive(Debug)]
Expand All @@ -8,7 +7,7 @@ pub struct Error {
msg: String,
}

macro_rules! quic_error {
macro_rules! taurus_error {
($name:ident, $code:expr) => {
pub fn $name<T>(reason: T) -> Self
where
Expand All @@ -23,12 +22,12 @@ macro_rules! quic_error {
}

impl Error {
quic_error!(fatal, 0x00);
quic_error!(unknown_connection, 0x01);
quic_error!(socket_error, 0x02);
quic_error!(header_encoding_error, 0x03);
quic_error!(packet_size_error, 0x04);
quic_error!(no_cipher_suite, 0x05);
taurus_error!(fatal, 0x00);
taurus_error!(unknown_connection, 0x01);
taurus_error!(socket_error, 0x02);
taurus_error!(header_encoding_error, 0x03);
taurus_error!(packet_size_error, 0x04);
taurus_error!(no_cipher_suite, 0x05);
}

impl fmt::Display for Error {
Expand All @@ -37,7 +36,7 @@ impl fmt::Display for Error {
}
}

impl SError for Error {
impl std::error::Error for Error {
fn description(&self) -> &str {
&self.msg
}
Expand Down

0 comments on commit c84ca3e

Please sign in to comment.