From 6dbae4b54a4b7965c03c85817521f6a1cc5c323f Mon Sep 17 00:00:00 2001 From: SajjadPourali Date: Wed, 24 Apr 2024 12:54:37 -0400 Subject: [PATCH] Change the visibility of Tcb variables and remove unused macros --- src/stream/tcb.rs | 24 ++++++++++++++---------- src/stream/tcp.rs | 12 ++++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/stream/tcb.rs b/src/stream/tcb.rs index 7062c50..ff44486 100644 --- a/src/stream/tcb.rs +++ b/src/stream/tcb.rs @@ -5,19 +5,17 @@ use tokio::time::Sleep; const MAX_UNACK: u32 = 1024 * 16; // 16KB const READ_BUFFER_SIZE: usize = 1024 * 16; // 16KB -#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)] +#[derive(Clone, Debug, PartialEq)] pub enum TcpState { SynReceived(bool), // bool means if syn/ack is sent - #[default] Established, FinWait1(bool), FinWait2(bool), // bool means waiting for ack Closed, } -#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)] +#[derive(Clone, Debug, PartialEq)] pub(super) enum PacketStatus { - #[default] WindowUpdate, Invalid, RetransmissionRequest, @@ -28,16 +26,16 @@ pub(super) enum PacketStatus { #[derive(Debug)] pub(super) struct Tcb { - pub(super) seq: u32, + seq: u32, pub(super) retransmission: Option, - pub(super) ack: u32, - pub(super) last_ack: u32, + ack: u32, + last_ack: u32, pub(super) timeout: Pin>, tcp_timeout: Duration, recv_window: u16, - pub(super) send_window: u16, + send_window: u16, state: TcpState, - pub(super) avg_send_window: (u64, u64), + avg_send_window: (u64, u64), // (avg, count) pub(super) inflight_packets: Vec, unordered_packets: BTreeMap, } @@ -102,11 +100,14 @@ impl Tcb { pub(super) fn get_ack(&self) -> u32 { self.ack } + pub(super) fn get_last_ack(&self) -> u32 { + self.last_ack + } pub(super) fn change_state(&mut self, state: TcpState) { self.state = state; } pub(super) fn get_state(&self) -> TcpState { - self.state + self.state.clone() } pub(super) fn change_send_window(&mut self, window: u16) { let avg_send_window = ((self.avg_send_window.0 * self.avg_send_window.1) + window as u64) @@ -118,6 +119,9 @@ impl Tcb { pub(super) fn get_send_window(&self) -> u16 { self.send_window } + pub(super) fn get_avg_send_window(&self) -> u64 { + self.avg_send_window.0 + } pub(super) fn change_recv_window(&mut self, window: u16) { self.recv_window = window; } diff --git a/src/stream/tcp.rs b/src/stream/tcp.rs index a7bcd77..b5aa424 100644 --- a/src/stream/tcp.rs +++ b/src/stream/tcp.rs @@ -255,7 +255,7 @@ impl AsyncRead for IpStackTcpStream { continue; } else if matches!(self.shutdown, Shutdown::Pending(_)) && self.tcb.get_state() == TcpState::Established - && self.tcb.last_ack == self.tcb.seq + && self.tcb.get_last_ack() == self.tcb.get_seq() { self.packet_to_send = Some(self.create_rev_packet(FIN | ACK, TTL, None, Vec::new())?); @@ -434,7 +434,7 @@ impl AsyncWrite for IpStackTcpStream { } self.tcb.reset_timeout(); - if (self.tcb.send_window as u64) < self.tcb.avg_send_window.0 / 2 + if (self.tcb.get_send_window() as u64) < self.tcb.get_avg_send_window() / 2 || self.tcb.is_send_buffer_full() { self.write_notify = Some(cx.waker().clone()); @@ -449,7 +449,7 @@ impl AsyncWrite for IpStackTcpStream { } let packet = self.create_rev_packet(PSH | ACK, TTL, None, buf.to_vec())?; - let seq = self.tcb.seq; + let seq = self.tcb.get_seq(); let payload_len = packet.payload.len(); let payload = packet.payload.clone(); self.packet_sender @@ -482,9 +482,9 @@ impl AsyncWrite for IpStackTcpStream { } else if let Some(_i) = self.tcb.retransmission { { warn!("{}", _i); - warn!("{}", self.tcb.seq); - warn!("{}", self.tcb.last_ack); - warn!("{}", self.tcb.ack); + warn!("{}", self.tcb.get_seq()); + warn!("{}", self.tcb.get_last_ack()); + warn!("{}", self.tcb.get_ack()); for p in self.tcb.inflight_packets.iter() { warn!("{}", p.seq); warn!("{}", p.payload.len());