This commit is contained in:
JOLIMAITRE Matthieu 2024-06-21 02:59:28 +02:00
parent 49483c87a9
commit d53d053f43
3 changed files with 32 additions and 2 deletions

View file

@ -5,16 +5,30 @@ use pcap::Capture;
fn main() -> Result<(), Box<dyn Error>> {
let Args { file, cmd } = Args::parse();
let pcap = Capture::from_file(file)?;
let mut pcap = Capture::from_file(file)?;
match cmd {
Cmd::Links => {
for record in pcap.list_datalinks()? {
let name = record.get_name().unwrap_or_else(|_| "<unnamed>".into());
let description = record.get_description().unwrap_or_default();
println!("link {name} {description}")
println!("link {name} {description}")
}
}
Cmd::Stats => {
let mut total_ipv4 = 0;
let mut total = 0;
while let Ok(packet) = pcap.next_packet() {
total += 1;
let ether_type = &packet.data[20..][..2];
if ether_type == [0x08, 0x00] {
total_ipv4 += 1;
}
}
println!("Count: {total: >9}");
println!("Count IPv4: {total_ipv4: >9}");
}
}
Ok(())
@ -31,4 +45,5 @@ struct Args {
#[derive(Subcommand)]
enum Cmd {
Links,
Stats,
}