Skip to content

Commit

Permalink
open Capture from pcap::Device instead of &str
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 31, 2023
1 parent 158ceb6 commit e1e388a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ impl Sniffer {
fn refresh_data(&mut self) -> Command<Message> {
let info_traffic_lock = self.info_traffic.lock().unwrap();
self.runtime_data.all_packets = info_traffic_lock.all_packets;
self.runtime_data.link_type = info_traffic_lock.link_type;
if info_traffic_lock.tot_received_packets + info_traffic_lock.tot_sent_packets == 0 {
drop(info_traffic_lock);
return self.update(Message::Waiting);
Expand All @@ -303,7 +304,6 @@ impl Sniffer {
self.runtime_data.tot_received_bytes = info_traffic_lock.tot_received_bytes;
self.runtime_data.tot_sent_bytes = info_traffic_lock.tot_sent_bytes;
self.runtime_data.dropped_packets = info_traffic_lock.dropped_packets;
self.runtime_data.link_type = info_traffic_lock.link_type;
drop(info_traffic_lock);
let emitted_notifications = notify_and_log(
&mut self.runtime_data,
Expand Down
2 changes: 1 addition & 1 deletion src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ pub fn is_my_address(address_to_lookup: &String, my_interface_addresses: &Vec<Ad

/// Determines if the capture opening resolves into an Error
pub fn get_capture_result(device: &MyDevice) -> (Option<String>, Option<Capture<Active>>) {
let cap_result = Capture::from_device(&*device.name)
let cap_result = Capture::from_device(device.to_pcap_device())
.expect("Capture initialization error\n\r")
.promisc(true)
.snaplen(256) //limit stored packets slice dimension (to keep more in the buffer)
Expand Down
18 changes: 17 additions & 1 deletion src/networking/types/my_device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use pcap::Address;
use pcap::{Address, Device, DeviceFlags};

/// Represents the current inspected device.
/// Used to keep in sync the device addresses in case of changes
Expand All @@ -11,3 +11,19 @@ pub struct MyDevice {
pub desc: Option<String>,
pub addresses: Arc<Mutex<Vec<Address>>>,
}

impl MyDevice {
pub fn to_pcap_device(&self) -> Device {
for device in Device::list().unwrap_or_default() {
if device.name.eq(&self.name) {
return device;
}
}
Device::lookup().unwrap_or(None).unwrap_or(Device {
name: String::new(),
desc: None,
addresses: vec![],
flags: DeviceFlags::empty(),
})
}
}
1 change: 1 addition & 0 deletions src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn parse_packets(

// pcap seems to assign ethernet to all interfaces (at least on macOS)...
let mut link_type = cap.get_datalink();
info_traffic_mutex.lock().unwrap().link_type = link_type;
// ...it'll be confirmed after having parsed the first packet!
let mut is_link_type_confirmed = false;

Expand Down

0 comments on commit e1e388a

Please sign in to comment.