diff --git a/tuic/src/model/authenticate.rs b/tuic/src/model/authenticate.rs index 305cd40..b913692 100644 --- a/tuic/src/model/authenticate.rs +++ b/tuic/src/model/authenticate.rs @@ -1,5 +1,6 @@ use super::side::{self, Side}; use crate::protocol::{Authenticate as AuthenticateHeader, Header}; +use std::fmt::{Debug, Formatter, Result as FmtResult}; use uuid::Uuid; /// The model of the `Authenticate` command @@ -36,6 +37,15 @@ impl Authenticate { } } +impl Debug for Authenticate { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Tx(tx) = &self.inner else { unreachable!() }; + f.debug_struct("Authenticate") + .field("header", &tx.header) + .finish() + } +} + struct Rx { uuid: Uuid, token: [u8; 32], @@ -72,6 +82,16 @@ impl Authenticate { } } +impl Debug for Authenticate { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Rx(rx) = &self.inner else { unreachable!() }; + f.debug_struct("Authenticate") + .field("uuid", &rx.uuid) + .field("token", &rx.token) + .finish() + } +} + /// The trait for exporting keying material pub trait KeyingMaterialExporter { /// Exports keying material diff --git a/tuic/src/model/connect.rs b/tuic/src/model/connect.rs index a40c89b..5c029d6 100644 --- a/tuic/src/model/connect.rs +++ b/tuic/src/model/connect.rs @@ -1,6 +1,7 @@ use super::side::{self, Side}; use crate::protocol::{Address, Connect as ConnectHeader, Header}; use register_count::Register; +use std::fmt::{Debug, Formatter, Result as FmtResult}; /// The model of the `Connect` command pub struct Connect { @@ -31,6 +32,15 @@ impl Connect { } } +impl Debug for Connect { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Tx(tx) = &self.inner else { unreachable!() }; + f.debug_struct("Connect") + .field("header", &tx.header) + .finish() + } +} + struct Rx { addr: Address, _task_reg: Register, @@ -53,3 +63,10 @@ impl Connect { &rx.addr } } + +impl Debug for Connect { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Rx(rx) = &self.inner else { unreachable!() }; + f.debug_struct("Connect").field("addr", &rx.addr).finish() + } +} diff --git a/tuic/src/model/dissociate.rs b/tuic/src/model/dissociate.rs index cb51c50..5d6dcb2 100644 --- a/tuic/src/model/dissociate.rs +++ b/tuic/src/model/dissociate.rs @@ -1,5 +1,6 @@ use super::side::{self, Side}; use crate::protocol::{Dissociate as DissociateHeader, Header}; +use std::fmt::{Debug, Formatter, Result as FmtResult}; /// The model of the `Dissociate` command pub struct Dissociate { @@ -28,6 +29,15 @@ impl Dissociate { } } +impl Debug for Dissociate { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Tx(tx) = &self.inner else { unreachable!() }; + f.debug_struct("Dissociate") + .field("header", &tx.header) + .finish() + } +} + struct Rx { assoc_id: u16, } @@ -46,3 +56,12 @@ impl Dissociate { rx.assoc_id } } + +impl Debug for Dissociate { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Rx(rx) = &self.inner else { unreachable!() }; + f.debug_struct("Dissociate") + .field("assoc_id", &rx.assoc_id) + .finish() + } +} diff --git a/tuic/src/model/heartbeat.rs b/tuic/src/model/heartbeat.rs index 2d80081..5bf6ce2 100644 --- a/tuic/src/model/heartbeat.rs +++ b/tuic/src/model/heartbeat.rs @@ -1,5 +1,6 @@ use super::side::{self, Side}; use crate::protocol::{Header, Heartbeat as HeartbeatHeader}; +use std::fmt::{Debug, Formatter, Result as FmtResult}; pub struct Heartbeat { inner: Side, @@ -27,6 +28,15 @@ impl Heartbeat { } } +impl Debug for Heartbeat { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Tx(tx) = &self.inner else { unreachable!() }; + f.debug_struct("Heartbeat") + .field("header", &tx.header) + .finish() + } +} + struct Rx; impl Heartbeat { @@ -37,3 +47,9 @@ impl Heartbeat { } } } + +impl Debug for Heartbeat { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + f.debug_struct("Heartbeat").finish() + } +} diff --git a/tuic/src/model/mod.rs b/tuic/src/model/mod.rs index b69caa9..620ef13 100644 --- a/tuic/src/model/mod.rs +++ b/tuic/src/model/mod.rs @@ -8,6 +8,7 @@ use parking_lot::Mutex; use register_count::{Counter, Register}; use std::{ collections::HashMap, + fmt::{Debug, Formatter, Result as FmtResult}, mem, sync::{ atomic::{AtomicU16, Ordering}, @@ -33,6 +34,7 @@ pub use self::{ }; /// An abstraction of a TUIC connection, with packet fragmentation management and task counters. No I/O operation is involved internally +#[derive(Clone)] pub struct Connection { udp_sessions: Arc>>, task_connect_count: Counter, @@ -160,6 +162,19 @@ where } } +impl Debug for Connection +where + B: AsRef<[u8]> + Debug, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + f.debug_struct("Connection") + .field("udp_sessions", &self.udp_sessions) + .field("task_connect_count", &self.task_connect_count()) + .field("task_associate_count", &self.task_associate_count()) + .finish() + } +} + /// Abstracts the side of a task pub mod side { /// The side of a task that sends data @@ -268,6 +283,17 @@ where } } +impl Debug for UdpSessions +where + B: AsRef<[u8]> + Debug, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + f.debug_struct("UdpSessions") + .field("sessions", &self.sessions) + .finish() + } +} + struct UdpSession { pkt_buf: HashMap>, next_pkt_id: AtomicU16, @@ -343,6 +369,19 @@ where } } +impl Debug for UdpSession +where + B: AsRef<[u8]> + Debug, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + f.debug_struct("UdpSession") + .field("pkt_buf", &self.pkt_buf) + .field("next_pkt_id", &self.next_pkt_id) + .finish() + } +} + +#[derive(Debug)] struct PacketBuffer { buf: Vec>, frag_total: u8, @@ -419,6 +458,7 @@ where } /// A complete packet that can be assembled +#[derive(Debug)] pub struct Assemblable { buf: Vec>, addr: Address, diff --git a/tuic/src/model/packet.rs b/tuic/src/model/packet.rs index b763ea1..e357cf6 100644 --- a/tuic/src/model/packet.rs +++ b/tuic/src/model/packet.rs @@ -4,7 +4,12 @@ use super::{ }; use crate::protocol::{Address, Header, Packet as PacketHeader}; use parking_lot::Mutex; -use std::{marker::PhantomData, slice, sync::Arc}; +use std::{ + fmt::{Debug, Formatter, Result as FmtResult}, + marker::PhantomData, + slice, + sync::Arc, +}; pub struct Packet { inner: Side>, @@ -53,6 +58,18 @@ impl Packet { } } +impl Debug for Packet { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Tx(tx) = &self.inner else { unreachable!() }; + f.debug_struct("Packet") + .field("assoc_id", &tx.assoc_id) + .field("pkt_id", &tx.pkt_id) + .field("addr", &tx.addr) + .field("max_pkt_size", &tx.max_pkt_size) + .finish() + } +} + struct Rx { sessions: Arc>>, assoc_id: u16, @@ -90,6 +107,7 @@ where } } + /// Reassembles the packet. If the packet is not complete yet, `None` is returned. pub fn assemble(self, data: B) -> Result>, AssembleError> { let Side::Rx(rx) = self.inner else { unreachable!() }; let mut sessions = rx.sessions.lock(); @@ -105,23 +123,41 @@ where ) } + /// Returns the UDP session ID pub fn assoc_id(&self) -> u16 { let Side::Rx(rx) = &self.inner else { unreachable!() }; rx.assoc_id } + /// Returns the address pub fn addr(&self) -> &Address { let Side::Rx(rx) = &self.inner else { unreachable!() }; &rx.addr } + /// Returns the size of the (fragmented) packet pub fn size(&self) -> u16 { let Side::Rx(rx) = &self.inner else { unreachable!() }; rx.size } } +impl Debug for Packet { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let Side::Rx(rx) = &self.inner else { unreachable!() }; + f.debug_struct("Packet") + .field("assoc_id", &rx.assoc_id) + .field("pkt_id", &rx.pkt_id) + .field("frag_total", &rx.frag_total) + .field("frag_id", &rx.frag_id) + .field("size", &rx.size) + .field("addr", &rx.addr) + .finish() + } +} + /// Iterator over fragments of a packet +#[derive(Debug)] pub struct Fragments<'a, P> where P: 'a,