gets frames

This commit is contained in:
Alon Levy 2023-12-29 02:22:07 +02:00
parent 68757371a0
commit 45dfbd6d66
3 changed files with 92 additions and 2 deletions

View File

@ -234,6 +234,26 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 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]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.11.2" version = "1.11.2"
@ -264,6 +284,7 @@ dependencies = [
"anyhow", "anyhow",
"pcap-parser", "pcap-parser",
"pyo3", "pyo3",
"serde",
] ]
[[package]] [[package]]

View File

@ -9,3 +9,4 @@ edition = "2021"
anyhow = "1.0.77" anyhow = "1.0.77"
pcap-parser = { version = "0.14.1", features = ["data"] } pcap-parser = { version = "0.14.1", features = ["data"] }
pyo3 = "0.20.0" pyo3 = "0.20.0"
serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] }

View File

@ -1,9 +1,10 @@
use std::fs::File; use std::fs::File;
use std::io::Write;
use pcap_parser::traits::PcapReaderIterator; use pcap_parser::traits::PcapReaderIterator;
use pcap_parser::*; use pcap_parser::*;
fn main() -> anyhow::Result<()> { fn get_packets_without_udp_header() -> anyhow::Result<(Vec<Vec<u8>>, usize, usize)> {
let file = File::open("in.pcap")?; let file = File::open("in.pcap")?;
let mut cap = PcapNGReader::new(65535, file)?; let mut cap = PcapNGReader::new(65535, file)?;
let mut i = 0; 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<Self> {
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::<Self>();
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::<Header>();
fn main() -> anyhow::Result<()> {
let (data, i, size) = get_packets_without_udp_header()?;
println!("found {} packets, saved {}, {} size", i, data.len(), size); 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(()) Ok(())
} }