diff --git a/Cargo.toml b/Cargo.toml index c59845c..8069bf3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rawsock" -version = "0.2.1" +version = "0.3.0" edition = "2018" authors = ["Szymon Wieloch "] description = "Library for receiving and sending raw packets. While most crate wrap just one library, rawsock allows you to use pcap, wpcap, npcap and pf_ring (pfring) using a consistent API for all of them." diff --git a/examples/thread_loop.rs b/examples/thread_loop.rs index c2cf8f1..a88a223 100644 --- a/examples/thread_loop.rs +++ b/examples/thread_loop.rs @@ -5,13 +5,16 @@ extern crate rawsock; extern crate crossbeam_utils; use crossbeam_utils::thread; -use rawsock::{open_best_library}; +use rawsock::{pcap, open_best_library}; +use rawsock::traits::{Library, StaticInterface, DynamicInterface}; mod commons; +use commons::open_library; use std::thread::sleep; use std::time::Duration; fn main() { dynamic_loop(); + static_loop(); } fn dynamic_loop(){ @@ -36,4 +39,27 @@ fn dynamic_loop(){ println!("Loop is broken"); }).unwrap(); +} + +fn static_loop(){ + let lib = open_library::(); + let ifname = lib.all_interfaces() + .expect("Could not obtain interface list").first() + .expect("There are no available interfaces").name.clone(); + println!("Library version is {}, opening {} interface", lib.version(), &ifname); + let interf = lib.open_interface(&ifname).expect("Could not open pcap interface"); + + //To compile the code we need a thread scope to guarantee that inter2 will not outlive lib + thread::scope(|s| { + s.spawn(|_| { + interf.loop_infinite(|packet|{ + println!("Received packet: {}", packet); + }).expect("Error when running receiving loop"); + }); + println!("Waiting 5 seconds"); + sleep(Duration::from_secs(5)); + println!("Breaking the loop"); + interf.break_loop(); + println!("Loop is broken"); + }).unwrap(); } \ No newline at end of file diff --git a/src/pfring/interface.rs b/src/pfring/interface.rs index 23895fd..755287b 100644 --- a/src/pfring/interface.rs +++ b/src/pfring/interface.rs @@ -97,7 +97,7 @@ impl<'a> traits::DynamicInterface<'a> for Interface<'a> { } fn loop_infinite_dyn(&self, callback: & dyn FnMut(&BorrowedPacket)) -> Result<(), Error> { - let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_dynamic, transmute(& callback), 0)}; + let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_dynamic, transmute(& callback), 1)}; // This is super strange but although pfring_loop specification states that this function // should only return 0, it also returns 1. It happens when it finishes successfully after // a pfring_breakloop() call. @@ -125,7 +125,7 @@ extern "C" fn on_received_packet_dynamic(h: * const PFRingPacketHeader, p: * con impl<'a> traits::StaticInterface<'a> for Interface<'a> { fn loop_infinite(& self, callback: F) -> Result<(), Error> where F: FnMut(&BorrowedPacket) { - let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_static::, transmute(& callback), 0)}; + let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_static::, transmute(& callback), 1)}; if result == SUCCESS { Ok(()) } else {