Skip to content

Commit

Permalink
build: bump pcap-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Mar 13, 2024
1 parent 3bb31fb commit bddc812
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pnet_macros_support = "0.34.0"
pnet_macros = "0.34.0"

# using custom forked version of pcap-rs for fixing audits
pcap = { version = "1.1.0", git = "https://github.com/SkuldNorniern/pcap", rev = "40f1163" }
# pcap = { version = "1.1.0", git = "https://github.com/SkuldNorniern/pcap", rev = "40f1163" }

pcap = "1.2.0"

chrono = { version = "0.4.29", default-features = false, features = ["clock"] }
libc = "0.2"
Expand All @@ -35,7 +37,7 @@ fluereflow = { version = "0.3.2", path = "./fluereflow" }
ratatui = { version = "0.25.0", features = ["all-widgets"] }
crossterm = "0.27"
dirs = "5.0.1"
log = "0.4.20"
log = { version = "0.4.21", features = ["std"]}

[workspace]
members = [
Expand Down
43 changes: 20 additions & 23 deletions src/net/capture.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
use pcap::{Active, Address, Capture, Device, Error as PcapError};
use std::{fmt, time::Instant};

use crate::net::NetError;

use std::fmt;
use std::time::Instant;
use pcap::{Active, Address, Capture, Device, Error as PcapError};

#[derive(Debug)]
pub enum DeviceError {
Cap(PcapError),
DeviceNotFound(String),
InitializationError(),
InvalidDeviceIndex(usize),
}

impl From<PcapError> for DeviceError {
fn from(err: PcapError) -> Self {
DeviceError::Cap(err)
}
}
impl fmt::Display for DeviceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DeviceError::Cap(err) => err.fmt(f),
DeviceError::DeviceNotFound(err) => write!(f, "Device not found: {}", err),
DeviceError::InitializationError() => write!(f, "Initialization error"),
DeviceError::InvalidDeviceIndex(err) => write!(f, "Invalid device index: {}", err),
}
}
Expand All @@ -35,7 +27,7 @@ pub struct CaptureDevice {
}

impl CaptureDevice {
pub fn new(device: Device) -> Result<CaptureDevice, DeviceError> {
pub fn new(device: Device) -> Result<CaptureDevice, PcapError> {
let capture = initialize_capture(device.clone())?;

Ok(CaptureDevice {
Expand All @@ -47,23 +39,27 @@ impl CaptureDevice {
}
}

pub fn list_devices() -> Result<Vec<Device>, DeviceError> {
Device::list().map_err(DeviceError::Cap)
impl Drop for CaptureDevice {
fn drop(&mut self) {
println!("Closing capture session for device {}", self.name);
// self.capture.;
}
}

pub fn find_device(identifier: &str) -> Result<Device, DeviceError> {
pub fn find_device(identifier: &str) -> Result<Device, NetError> {
let start = Instant::now();
println!("Requested Device: {}", identifier);

let devices = list_devices()?;
let devices = Device::list()?;

if let Ok(index) = identifier.parse::<usize>() {
if let Some(device) = devices.get(index) {
let duration = start.elapsed();
println!("Device {} captured in {:?}", device.name, duration);
return Ok(device.clone());
} else {
return Err(DeviceError::InvalidDeviceIndex(index));
return Err(NetError::DeviceError(DeviceError::InvalidDeviceIndex(
index,
)));
}
}

Expand All @@ -75,12 +71,13 @@ pub fn find_device(identifier: &str) -> Result<Device, DeviceError> {
}
}

Err(DeviceError::DeviceNotFound(identifier.to_string()))
Err(NetError::DeviceError(DeviceError::DeviceNotFound(
identifier.to_string(),
)))
}

fn initialize_capture(device: Device) -> Result<Capture<Active>, DeviceError> {
Ok(Capture::from_device(device)
.map_err(DeviceError::Cap)?
fn initialize_capture(device: Device) -> Result<Capture<Active>, PcapError> {
Ok(Capture::from_device(device)?
.promisc(true)
.snaplen(1024)
.timeout(60000)
Expand Down
4 changes: 2 additions & 2 deletions src/net/live_fluereflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub async fn online_packet_capture(arg: Args) {
.expect("Failed to load plugins");

let interface = find_device(interface_name.as_str()).unwrap();
let cap_device = CaptureDevice::new(interface.clone()).unwrap();
let mut cap = cap_device.capture;
let mut cap_device = CaptureDevice::new(interface.clone()).unwrap();
let cap = &mut cap_device.capture;

let file_dir = "./output";
match fs::create_dir_all(<&str>::clone(&file_dir)) {
Expand Down
4 changes: 2 additions & 2 deletions src/net/online_fluereflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub async fn packet_capture(arg: Args) {
.expect("Failed to load plugins");

let interface = find_device(interface_name.as_str()).unwrap();
let cap_device = CaptureDevice::new(interface.clone()).unwrap();
let mut cap = cap_device.capture;
let mut cap_device = CaptureDevice::new(interface.clone()).unwrap();
let cap = &mut cap_device.capture;
// let mut cp_device
// let mut cap = Capture::from_device(interface)
// .unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/net/packet_pcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub async fn pcap_capture(args: Args) {
let verbose = args.verbose.unwrap();

let interface = find_device(interface_name.as_str()).unwrap();
let cap_device = CaptureDevice::new(interface.clone()).unwrap();
let mut cap = cap_device.capture;
let mut cap_device = CaptureDevice::new(interface.clone()).unwrap();
let cap = &mut cap_device.capture;

let file_dir = "./output";
let mut packet_count = 0;
Expand Down

0 comments on commit bddc812

Please sign in to comment.