From 79a002518c97b57a431fc5639e1364e04988b397 Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:37:53 -0500 Subject: [PATCH] pcap: preserve capture and protocol source history Port relevant PCAP implementation and regression snapshots from Craft Anything 43bd04745f42daefd9dea16756b13816fa7485a6. --- crates/pcap/src/lib.rs | 2 +- crates/pcap/tests/pcap_tcp_ecn.rs | 33 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 crates/pcap/tests/pcap_tcp_ecn.rs diff --git a/crates/pcap/src/lib.rs b/crates/pcap/src/lib.rs index 5ba79249b..065663423 100644 --- a/crates/pcap/src/lib.rs +++ b/crates/pcap/src/lib.rs @@ -716,7 +716,7 @@ fn ipv6(p: &[u8], d: &mut Decoded) { } fn tcp_flags(f: u8) -> String { - let names = [(0x02, "SYN"), (0x10, "ACK"), (0x01, "FIN"), (0x04, "RST"), (0x08, "PSH"), (0x20, "URG")]; + let names = [(0x02, "SYN"), (0x10, "ACK"), (0x01, "FIN"), (0x04, "RST"), (0x08, "PSH"), (0x20, "URG"), (0x40, "ECE"), (0x80, "CWR")]; names.iter().filter(|(b, _)| f & b != 0).map(|(_, n)| *n).collect::>().join(",") } diff --git a/crates/pcap/tests/pcap_tcp_ecn.rs b/crates/pcap/tests/pcap_tcp_ecn.rs new file mode 100644 index 000000000..e8668af1d --- /dev/null +++ b/crates/pcap/tests/pcap_tcp_ecn.rs @@ -0,0 +1,33 @@ +use craft_anything_formats::pcap::decode; + +/// Decode a raw IPv4 packet with a TCP header and the specified control bits. +fn flags_on_wire(bits: u8) -> String { + let mut packet = [0u8; 40]; + packet[0] = 0x45; + packet[2..4].copy_from_slice(&40u16.to_be_bytes()); + packet[8] = 64; + packet[9] = 6; + packet[20..24].copy_from_slice(&[0xc0, 0, 0x01, 0xbb]); + packet[32] = 0x50; + packet[33] = bits; + decode(228, &packet).transport.unwrap().tcp_flags.unwrap() +} + +/// An ECN-capable SYN shows both classic ECN flags. +#[test] +fn reports_ecn_setup_on_syn() { + assert_eq!(flags_on_wire(0xc2), "SYN,ECE,CWR"); +} + +/// The receiver and sender can signal ECE and CWR independently on ACKs. +#[test] +fn distinguishes_ecn_echo_and_window_reduction() { + assert_eq!(flags_on_wire(0x50), "ACK,ECE"); + assert_eq!(flags_on_wire(0x90), "ACK,CWR"); +} + +/// Existing non-ECN packets keep their flag strings. +#[test] +fn keeps_ordinary_ack_flags() { + assert_eq!(flags_on_wire(0x12), "SYN,ACK"); +}