From f6e618e46bfc5f5e5ac9d86cf506e814b9279d0e Mon Sep 17 00:00:00 2001 From: SajjadPourali Date: Mon, 4 Mar 2024 16:26:34 -0500 Subject: [PATCH] Prevent creating closed sockets --- examples/tun_wintun.rs | 1 - src/lib.rs | 3 +++ src/stream/tcp.rs | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/tun_wintun.rs b/examples/tun_wintun.rs index 9d9154c..397cc85 100644 --- a/examples/tun_wintun.rs +++ b/examples/tun_wintun.rs @@ -62,7 +62,6 @@ async fn main() -> Result<(), Box> { } }; println!("==== New TCP connection ===="); - dbg!(tcp.local_addr()); tokio::spawn(async move { let _ = tokio::io::copy_bidirectional(&mut tcp, &mut s).await; println!("====== end tcp connection ======"); diff --git a/src/lib.rs b/src/lib.rs index 5c90d2e..694faf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,6 +119,9 @@ impl IpStack { IpStackPacketProtocol::Tcp(h) => { match IpStackTcpStream::new(packet.src_addr(),packet.dst_addr(),h, pkt_sender.clone(),config.mtu,config.tcp_timeout).await{ Ok(stream) => { + if stream.is_closed(){ + continue; + } entry.insert(stream.stream_sender()); accept_sender.send(IpStackStream::Tcp(stream))?; } diff --git a/src/stream/tcp.rs b/src/stream/tcp.rs index e5cdddc..54a0f00 100644 --- a/src/stream/tcp.rs +++ b/src/stream/tcp.rs @@ -196,6 +196,9 @@ impl IpStackTcpStream { pub fn peer_addr(&self) -> SocketAddr { self.dst_addr } + pub(crate) fn is_closed(&self) -> bool { + matches!(self.tcb.get_state(), TcpState::Closed) + } } impl AsyncRead for IpStackTcpStream {