Add growing-capture --mac pipeline test; mention --mac in filter/server comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ericandClaude Opus 5.5 committed 2026-11-13 16:07:49 -05:00
1 parent a9e283a87c
commit f21b6c53e8
3 files changed
+91 -2

No files matched your search

+2 -1
View File
@@ -50,7 +50,8 @@ pub struct PacketFilter {
}
impl PacketFilter {
/// The no-`--ip` default: keeps everything, matches without inspecting.
/// The no-`--ip`/`--mac` default: keeps everything, matches without
/// inspecting.
pub fn match_all() -> Self {
Self { match_all: true, v4: Vec::new(), v6: Vec::new(), macs: Vec::new() }
}
+88
View File
@@ -530,4 +530,92 @@ mod tests {
run_pipe(opts, dest_final.clone(), dest_temp).unwrap();
assert_eq!(std::fs::read(&dest_final).unwrap(), eth_header_bytes());
}
/// A record holding an Ethernet frame from `src` to `dst` with
/// `ethertype`, padded to `len` captured bytes.
fn mac_record(dst: [u8; 6], src: [u8; 6], ethertype: u16, len: usize, ts_sec: u32) -> Vec<u8> {
let mut pkt = vec![0u8; len.max(14)];
pkt[0..6].copy_from_slice(&dst);
pkt[6..12].copy_from_slice(&src);
pkt[12..14].copy_from_slice(&ethertype.to_be_bytes());
record_bytes(&pkt, ts_sec)
}
#[test]
fn mac_filtered_growing_capture_carries_only_the_upstream_link() {
// The scenario --mac exists for: a capture still being written, from
// which only the traffic on one switch connection is wanted. The
// source grows in uneven appends that tear records mid-header and
// mid-payload, so the filter must only ever see confirmed records.
const UPSTREAM: [u8; 6] = [0x02, 0, 0, 0, 0, 0xaa];
const OTHER: [u8; 6] = [0x02, 0, 0, 0, 0, 0xbb];
const BROADCAST: [u8; 6] = [0xff; 6];
let mut source = eth_header_bytes();
let mut expected = eth_header_bytes();
for i in 0..60u32 {
let ts = 1_700_000_000 + i;
let len = 64 + i as usize;
let (record, kept) = match i % 4 {
0 => (mac_record(OTHER, UPSTREAM, 0x0800, len, ts), true), // from the switch
1 => (mac_record(UPSTREAM, OTHER, 0x86DD, len, ts), true), // to the switch
2 => (mac_record(BROADCAST, UPSTREAM, 0x0806, len, ts), true), // ARP from the switch
_ => (mac_record(BROADCAST, OTHER, 0x0806, len, ts), false), // someone else's ARP
};
source.extend_from_slice(&record);
if kept {
expected.extend_from_slice(&record);
}
}
let dir = tempdir().unwrap();
let partial = dir.path().join("cap.pcap.partial");
let final_src = dir.path().join("cap.pcap");
std::fs::write(&partial, &source[..10]).unwrap();
let dest_final = dir.path().join("out.pcap.zst");
let dest_temp = dir.path().join("out.pcap.zst.partial");
let opts = SendOptions {
source_path: partial.clone(),
is_partial: true,
final_source_path: final_src.clone(),
compress: true,
chunk_target: 512,
flush_after: crate::chunker::DEFAULT_FLUSH,
level: crate::zstd_frame::DEFAULT_LEVEL,
filter: PacketFilter::parse(&[], &["02:00:00:00:00:aa".to_string()]).unwrap(),
log: Arc::new(Logger::none()),
};
let (tx, rx) = mpsc::channel::<Message>();
let recv_handle = thread::spawn({
let dest_final = dest_final.clone();
let dest_temp = dest_temp.clone();
move || run_recv(Box::new(ChannelSource(rx)), RecvOptions { dest_final, dest_temp, log: Arc::new(Logger::none()) })
});
let send_handle = thread::spawn(move || run_send(opts, Box::new(ChannelSink(tx))));
// Odd-sized appends (an awkward stride relative to the ~80-140 byte
// records) so the visible end lands inside headers and payloads.
let mut written = 10;
for step in [37usize, 501, 13, 977, 250].iter().cycle() {
if written == source.len() {
break;
}
let end = (written + step).min(source.len());
let mut f = std::fs::OpenOptions::new().append(true).open(&partial).unwrap();
f.write_all(&source[written..end]).unwrap();
f.sync_all().unwrap();
written = end;
thread::sleep(std::time::Duration::from_millis(5));
}
std::fs::rename(&partial, &final_src).unwrap();
send_handle.join().unwrap().unwrap();
recv_handle.join().unwrap().unwrap();
let out_bytes = std::fs::read(&dest_final).unwrap();
assert_eq!(zstd::stream::decode_all(out_bytes.as_slice()).unwrap(), expected);
assert!(expected.len() < source.len());
}
}
+1 -1
View File
@@ -39,7 +39,7 @@ fn run_send_server(cli: ServerCli) -> Result<()> {
let flush_after = naming::parse_duration(&cli.flush_interval)?;
// The sending side is the only side that filters -- dropping packets
// before they hit the wire is the whole point. `run_recv` never parses
// what it writes, so --recv has no --ip.
// what it writes, so --recv has no --ip/--mac.
let filter = PacketFilter::parse(&cli.ip, &cli.mac)?;
let opts = SendOptions {