use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;
const LINKTYPE_ETHERNET: u32 = 1;
#[derive(Clone, Debug, Default)]
pub struct Capture {
pub frames: Vec<(u64, Vec<u8>)>,
}
impl Capture {
pub fn read(path: impl AsRef<Path>) -> io::Result<Capture> {
Capture::parse(&std::fs::read(path)?)
}
pub fn parse(data: &[u8]) -> io::Result<Capture> {
let bad = |what: &str| io::Error::new(io::ErrorKind::InvalidData, what.to_string());
let magic: [u8; 4] = data.get(..4).ok_or_else(|| bad("file is too short"))?.try_into().map_err(|_| bad("short read"))?;
let (little, nanos) = match magic {
[0xd4, 0xc3, 0xb2, 0xa1] => (true, false),
[0x4d, 0x3c, 0xb2, 0xa1] => (true, true),
[0xa1, 0xb2, 0xc3, 0xd4] => (false, false),
[0xa1, 0xb2, 0x3c, 0x4d] => (false, true),
[0x0a, 0x0d, 0x0d, 0x0a] => return Err(bad("this is a pcapng file; convert it with `editcap -F pcap`")),
_ => return Err(bad("not a classic pcap file")),
};
let u32_at = |o: usize| -> io::Result<u32> {
let b: [u8; 4] = data.get(o..o + 4).ok_or_else(|| bad("truncated"))?.try_into().map_err(|_| bad("short read"))?;
Ok(if little { u32::from_le_bytes(b) } else { u32::from_be_bytes(b) })
};
if u32_at(20)? != LINKTYPE_ETHERNET {
return Err(bad("only the Ethernet link type is supported"));
}
let mut frames = Vec::new();
let mut off = 24;
while off + 16 <= data.len() {
let secs = u64::from(u32_at(off)?);
let frac = u64::from(u32_at(off + 4)?);
let captured = u32_at(off + 8)? as usize;
let start = off + 16;
let Some(end) = start.checked_add(captured) else { break };
let Some(frame) = data.get(start..end) else { break };
frames.push((secs * 1_000_000_000 + frac * if nanos { 1 } else { 1_000 }, frame.to_vec()));
off = end;
}
Ok(Capture { frames })
}
}
#[derive(Debug)]
pub struct Writer {
out: BufWriter<File>,
}
impl Writer {
pub fn create(path: impl AsRef<Path>) -> io::Result<Writer> {
let mut out = BufWriter::new(File::create(path)?);
out.write_all(&0xa1b2_c3d4u32.to_le_bytes())?;
out.write_all(&2u16.to_le_bytes())?;
out.write_all(&4u16.to_le_bytes())?;
out.write_all(&0i32.to_le_bytes())?;
out.write_all(&0u32.to_le_bytes())?;
out.write_all(&65_535u32.to_le_bytes())?;
out.write_all(&LINKTYPE_ETHERNET.to_le_bytes())?;
Ok(Writer { out })
}
pub fn write(&mut self, nanos: u64, frame: &[u8]) -> io::Result<()> {
let len = u32::try_from(frame.len()).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "frame too long"))?;
self.out.write_all(&((nanos / 1_000_000_000) as u32).to_le_bytes())?;
self.out.write_all(&((nanos % 1_000_000_000 / 1_000) as u32).to_le_bytes())?;
self.out.write_all(&len.to_le_bytes())?;
self.out.write_all(&len.to_le_bytes())?;
self.out.write_all(frame)
}
pub fn finish(mut self) -> io::Result<()> {
self.out.flush()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let dir = std::env::temp_dir().join(format!("iec61850-pcap-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("t.pcap");
let mut w = Writer::create(&path).unwrap();
w.write(1_700_000_000_000_000_000, &[1, 2, 3]).unwrap();
w.write(1_700_000_000_001_000_000, &[4, 5]).unwrap();
w.finish().unwrap();
let c = Capture::read(&path).unwrap();
assert_eq!(c.frames.len(), 2);
assert_eq!(c.frames[0].1, [1, 2, 3]);
assert_eq!(c.frames[1].0 - c.frames[0].0, 1_000_000);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn rejects_what_it_cannot_read() {
assert!(Capture::parse(&[0u8; 4]).is_err());
assert!(Capture::parse(&[0x0a, 0x0d, 0x0d, 0x0a, 0, 0, 0, 0]).unwrap_err().to_string().contains("pcapng"));
let mut wifi = vec![0xd4, 0xc3, 0xb2, 0xa1];
wifi.extend_from_slice(&[0u8; 20]);
wifi[20] = 105; assert!(Capture::parse(&wifi).is_err());
let mut lying = vec![0xd4, 0xc3, 0xb2, 0xa1];
lying.extend_from_slice(&[0u8; 20]);
lying[20] = 1;
lying.extend_from_slice(&[0u8; 8]); lying.extend_from_slice(&u32::MAX.to_le_bytes()); lying.extend_from_slice(&u32::MAX.to_le_bytes());
lying.extend_from_slice(&[1, 2, 3]);
let frames = Capture::parse(&lying).unwrap().frames;
assert!(frames.is_empty(), "{frames:?}");
}
}