Skip to content

Commit

Permalink
Prevent creating closed sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
SajjadPourali committed Mar 4, 2024
1 parent 8a294de commit f6e618e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/tun_wintun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
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 ======");
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
}
Expand Down
3 changes: 3 additions & 0 deletions src/stream/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f6e618e

Please sign in to comment.