Skip to content

Commit

Permalink
Fixed looping problems with pfring, extended looping example and vers…
Browse files Browse the repository at this point in the history
…ion bump
  • Loading branch information
szymonwieloch committed Aug 11, 2019
1 parent dc7d6e2 commit 53408bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rawsock"
version = "0.2.1"
version = "0.3.0"
edition = "2018"
authors = ["Szymon Wieloch <szymon.wieloch@gmail.com>"]
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."
Expand Down
28 changes: 27 additions & 1 deletion examples/thread_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -36,4 +39,27 @@ fn dynamic_loop(){
println!("Loop is broken");
}).unwrap();

}

fn static_loop(){
let lib = open_library::<pcap::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();
}
4 changes: 2 additions & 2 deletions src/pfring/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<F>(& self, callback: F) -> Result<(), Error> where F: FnMut(&BorrowedPacket) {
let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_static::<F>, transmute(& callback), 0)};
let result = unsafe{self.dll.pfring_loop(self.handle, on_received_packet_static::<F>, transmute(& callback), 1)};
if result == SUCCESS {
Ok(())
} else {
Expand Down

0 comments on commit 53408bd

Please sign in to comment.