From 45dfbd6d6674f0691d76bc1193a5750b76095d04 Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Fri, 29 Dec 2023 02:22:07 +0200 Subject: [PATCH] gets frames --- thermalcamdecoder/Cargo.lock | 21 ++++++++++ thermalcamdecoder/Cargo.toml | 1 + thermalcamdecoder/src/main.rs | 72 ++++++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/thermalcamdecoder/Cargo.lock b/thermalcamdecoder/Cargo.lock index 76ab780..051e686 100644 --- a/thermalcamdecoder/Cargo.lock +++ b/thermalcamdecoder/Cargo.lock @@ -234,6 +234,26 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "smallvec" version = "1.11.2" @@ -264,6 +284,7 @@ dependencies = [ "anyhow", "pcap-parser", "pyo3", + "serde", ] [[package]] diff --git a/thermalcamdecoder/Cargo.toml b/thermalcamdecoder/Cargo.toml index a5ecd55..ca3dac9 100644 --- a/thermalcamdecoder/Cargo.toml +++ b/thermalcamdecoder/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" anyhow = "1.0.77" pcap-parser = { version = "0.14.1", features = ["data"] } pyo3 = "0.20.0" +serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] } diff --git a/thermalcamdecoder/src/main.rs b/thermalcamdecoder/src/main.rs index add8422..66861eb 100644 --- a/thermalcamdecoder/src/main.rs +++ b/thermalcamdecoder/src/main.rs @@ -1,9 +1,10 @@ use std::fs::File; +use std::io::Write; use pcap_parser::traits::PcapReaderIterator; use pcap_parser::*; -fn main() -> anyhow::Result<()> { +fn get_packets_without_udp_header() -> anyhow::Result<(Vec>, usize, usize)> { let file = File::open("in.pcap")?; let mut cap = PcapNGReader::new(65535, file)?; let mut i = 0; @@ -54,7 +55,74 @@ fn main() -> anyhow::Result<()> { } } } + Ok((data, i, size)) +} + +#[repr(C, packed)] +#[derive(Clone, Debug)] +struct Header { + c1: u32, + c2: u16, + part: u16, + a: u16, + ffaa: u16, + b: u16, + c: u16, + d: u16, +} + +impl Header { + fn read(data: &[u8]) -> anyhow::Result { + Ok(Header { + c1: u32::from_be_bytes([data[0], data[1], data[2], data[3]]), + c2: u16::from_be_bytes([data[4], data[5]]), + part: u16::from_be_bytes([data[6], data[7]]), + a: u16::from_be_bytes([data[8], data[9]]), + ffaa: u16::from_be_bytes([data[10], data[11]]), + b: u16::from_be_bytes([data[12], data[13]]), + c: u16::from_be_bytes([data[14], data[15]]), + d: u16::from_be_bytes([data[16], data[17]]), + }) + } + + fn read_via_cast(data: &[u8]) -> anyhow::Result<&Self> { + let size = std::mem::size_of::(); + if data.len() < size { + return Err(anyhow::anyhow!("not large enough")); + } + let mem = &data[..size].as_ptr(); + Ok(unsafe { &*mem.cast() }) + } +} + +const HDR_SIZE: usize = std::mem::size_of::
(); + +fn main() -> anyhow::Result<()> { + let (data, i, size) = get_packets_without_udp_header()?; + println!("found {} packets, saved {}, {} size", i, data.len(), size); - println!("{:+?}", data); + let mut dump = File::create_new("dump.bin"); + let mut frames = vec![]; + let mut parts = None; + for packet in data.iter() { + if let Ok(ref mut dump) = dump { + dump.write_all(&packet); + } + let hdr = Header::read(packet)?; + let data = packet[HDR_SIZE..].to_vec(); + match parts { + None => { + parts = Some(vec![data]); + } + Some(ref mut v) => { + v.push(data); + if hdr.part == 0 { + frames.push(parts.take()); + } + } + } + } + println!("found {} frames", frames.len()); + Ok(()) }