From b625d7b847a2c7370c477effec5c4f053f57f0d7 Mon Sep 17 00:00:00 2001 From: Wiktor Mazur Date: Thu, 12 Sep 2019 19:27:36 +0200 Subject: [PATCH] Included debug messages, fixed problem with some network interfaces --- Cargo.lock | 1 + lib/packet-sniffer/Cargo.toml | 3 ++- lib/packet-sniffer/src/lib.rs | 14 ++++++++++++-- src/lib.rs | 3 ++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3576f54..bf25225 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -404,6 +404,7 @@ dependencies = [ name = "packet_sniffer" version = "0.1.0" dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "pnet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/lib/packet-sniffer/Cargo.toml b/lib/packet-sniffer/Cargo.toml index b832f5d..ec6feee 100644 --- a/lib/packet-sniffer/Cargo.toml +++ b/lib/packet-sniffer/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -pnet = "0.22.0" \ No newline at end of file +pnet = "0.22.0" +log = { version = "0.4", features = ["std"] } \ No newline at end of file diff --git a/lib/packet-sniffer/src/lib.rs b/lib/packet-sniffer/src/lib.rs index 2f80094..ce5e83f 100644 --- a/lib/packet-sniffer/src/lib.rs +++ b/lib/packet-sniffer/src/lib.rs @@ -15,6 +15,8 @@ use pnet::packet::udp; use pnet::packet::Packet; use pnet::util::MacAddr; +use log::*; + #[derive(Clone, Debug)] pub struct UdpPacket { pub interface_name: String, @@ -35,6 +37,8 @@ pub fn receive(tx: Sender) { .into_iter() .filter(|i| !i.is_loopback()); + debug!("Found interfaces: {:?}", interfaces); + for interface in interfaces { let tx = shared_tx.clone(); let config = pnet::datalink::Config{ @@ -46,8 +50,14 @@ pub fn receive(tx: Sender) { // Create a channel to receive on let (_, mut rx) = match datalink::channel(&interface, config) { Ok(Ethernet(tx, rx)) => (tx, rx), - Ok(_) => panic!("packetdump: unhandled channel type: {}"), - Err(e) => panic!("packetdump: unable to create channel: {}", e), + Ok(_) => { + warn!("packetdump: unhandled channel type"); + continue; + }, + Err(e) => { + warn!("packetdump: unable to create channel: {}", e); + continue; + }, }; thread::spawn(move || { diff --git a/src/lib.rs b/src/lib.rs index 3d30b7a..10cad31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,7 +85,8 @@ fn initialize(_py: Python) -> PyResult { let (tx, rx): (Sender, Receiver) = channel(); packet_sniffer::receive(tx); - + info!("Listening to network packets..."); + loop { if let Ok(packet) = rx.recv() { if packet.destination_port != 5056 && packet.source_port != 5056 {