Skip to content

Commit

Permalink
Add pcap probe for probing network traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
goodhoko committed Sep 15, 2024
1 parent 660f665 commit 3ee6686
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"crates/test_probe",
"crates/ptrace_probe",
"crates/log_probe",
"crates/pcap_probe",
]

# Specify a subset of member crates that compile on all supported architectures.
Expand All @@ -16,6 +17,7 @@ default-members = [
"crates/composer",
"crates/test_probe",
"crates/log_probe",
"crates/pcap_probe",
]

# Explicitly set resolver due to virtual workspace, see
Expand Down
11 changes: 11 additions & 0 deletions crates/pcap_probe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "pcap_probe"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.2", features = ["derive"] }
color-eyre = "0.6"
composer_api = { path = "../composer_api" }
eyre = "0.6"
pcap = "2.2.0"
54 changes: 54 additions & 0 deletions crates/pcap_probe/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![warn(clippy::all, clippy::clone_on_ref_ptr)]

use std::time::Duration;

use clap::{command, Parser};
use composer_api::{Client, Event, EventKind, Packet};
use eyre::Result;
use pcap::Capture;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Args {
/// Server address to receive events.
#[arg(short, long)]
address: Option<String>,
}

fn main() -> Result<()> {
color_eyre::install()?;

let args = Args::parse();
let client = match args.address {
Some(address) => Client::new(address),
None => Client::try_default(),
}?;

let device = pcap::Device::lookup()
.expect("cal list devices")
.expect("there's a default network device");
println!("using device: {device:?}");

let mut capture = Capture::from_device(device)
.unwrap()
.immediate_mode(true)
.open()
.expect("can open the device for capture");

while let Ok(cap) = capture.next_packet() {
let ts = Duration::new(
cap.header.ts.tv_sec.unsigned_abs(),
// One microsecond is 1000 nanoseconds.
cap.header.ts.tv_usec.unsigned_abs() * 1000,

Check failure on line 43 in crates/pcap_probe/src/main.rs

View workflow job for this annotation

GitHub Actions / Run Tests

mismatched types

Check failure on line 43 in crates/pcap_probe/src/main.rs

View workflow job for this annotation

GitHub Actions / Run Clippy

mismatched types
);

let event = Event::with_timestamp(EventKind::TestTick, ts);

if let Err(err) = client.send(&Packet::from_event(event)) {
eprintln!("Could not send packet {:?}", err)
};
}

Ok(())
}

0 comments on commit 3ee6686

Please sign in to comment.