-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pcap probe for probing network traffic
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Run Tests
|
||
); | ||
|
||
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(()) | ||
} |