From bea9ece40ff1eed7dc2f80d2dda5771b93b03961 Mon Sep 17 00:00:00 2001 From: Sajjad Pourali Date: Mon, 20 Apr 2026 18:06:04 -0700 Subject: [PATCH 1/2] Code refactor + dependency upgrade --- Cargo.toml | 10 +++++----- src/stream/tcp.rs | 42 ++++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc3384c..d7f9566 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ readme = "README.md" [dependencies] ahash = { version = "0.8", default-features = false, features = ["std"] } -etherparse = { version = "0.19", default-features = false, features = ["std"] } +etherparse = { version = "0.20", default-features = false, features = ["std"] } log = { version = "0.4", default-features = false } -rand = { version = "0.9", default-features = false, features = ["thread_rng"] } +rand = { version = "0.10", default-features = false, features = ["thread_rng"] } thiserror = { version = "2.0", default-features = false } -tokio = { version = "1.48", default-features = false, features = [ +tokio = { version = "1.52", default-features = false, features = [ "sync", "rt", "time", @@ -25,11 +25,11 @@ tokio = { version = "1.48", default-features = false, features = [ ] } [dev-dependencies] -clap = { version = "4.5", default-features = false, features = ["derive"] } +clap = { version = "4.6", default-features = false, features = ["derive"] } criterion = { version = "0.8" } # Benchmarks dotenvy = "0.15" env_logger = "0.11" -tokio = { version = "1.48", default-features = false, features = [ +tokio = { version = "1.52", default-features = false, features = [ "rt-multi-thread", ] } tun = { version = "0.8", default-features = false, features = ["async"] } diff --git a/src/stream/tcp.rs b/src/stream/tcp.rs index 60be851..c545978 100644 --- a/src/stream/tcp.rs +++ b/src/stream/tcp.rs @@ -695,14 +695,12 @@ async fn tcp_main_logic_loop( } match state { - TcpState::SynReceived => { - if flags & ACK == ACK { - if len > 0 { - tcb.add_unordered_packet(incoming_seq, payload); - extract_data_n_write_upstream(&up_packet_sender, &mut tcb, network_tuple, &data_tx, &read_notify)?; - } - tcb.change_state(TcpState::Established); + TcpState::SynReceived if flags & ACK == ACK => { + if len > 0 { + tcb.add_unordered_packet(incoming_seq, payload); + extract_data_n_write_upstream(&up_packet_sender, &mut tcb, network_tuple, &data_tx, &read_notify)?; } + tcb.change_state(TcpState::Established); } TcpState::Established => { if flags == ACK { @@ -818,17 +816,15 @@ async fn tcp_main_logic_loop( write_notify.lock().unwrap().take().map(|w| w.wake_by_ref()).unwrap_or(()); } } - TcpState::LastAck => { - if flags & ACK == ACK { - tcb.change_state(TcpState::Closed); - tokio::spawn(async move { - if let Err(e) = exit_notifier.send(()).await { - log::debug!("exit_notifier send failed: {e}"); - } - }); - let new_state = tcb.get_state(); - log::trace!("{network_tuple} {state:?}: Received final ACK, transitioned to {new_state:?}"); - } + TcpState::LastAck if flags & ACK == ACK => { + tcb.change_state(TcpState::Closed); + tokio::spawn(async move { + if let Err(e) = exit_notifier.send(()).await { + log::debug!("exit_notifier send failed: {e}"); + } + }); + let new_state = tcb.get_state(); + log::trace!("{network_tuple} {state:?}: Received final ACK, transitioned to {new_state:?}"); } TcpState::FinWait1 => { if flags & (ACK | FIN) == (ACK | FIN) && len == 0 { @@ -889,12 +885,10 @@ async fn tcp_main_logic_loop( log::trace!("{network_tuple} {state:?}: Some unnormal case, we do nothing here"); } } - TcpState::TimeWait => { - if flags & (ACK | FIN) == (ACK | FIN) { - write_packet_to_device(&up_packet_sender, network_tuple, &tcb, None, ACK, None, None)?; - // wait to timeout, can't call `tcb.change_state(TcpState::Closed);` to change state here - // now we need to wait for the timeout to reach... - } + TcpState::TimeWait if flags & (ACK | FIN) == (ACK | FIN) => { + write_packet_to_device(&up_packet_sender, network_tuple, &tcb, None, ACK, None, None)?; + // wait to timeout, can't call `tcb.change_state(TcpState::Closed);` to change state here + // now we need to wait for the timeout to reach... } _ => {} } // end of match state From df87cb4fcd63c9a19535ec3e1b352681eec76fea Mon Sep 17 00:00:00 2001 From: SajjadPourali Date: Mon, 20 Apr 2026 18:07:42 -0700 Subject: [PATCH 2/2] Bump version to 1.0.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d7f9566..a5cdda3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ authors = ['Narrowlink '] description = 'Asynchronous lightweight userspace implementation of TCP/IP stack for Tun device' name = "ipstack" -version = "1.0.0" +version = "1.0.1" edition = "2024" license = "Apache-2.0" repository = 'https://github.com/narrowlink/ipstack'