I'm using pcap library to capture wifi packet in a thread and transferring the packet to an another thread using channels. While moving packet via sender to receiver thread, I get the error message cap does not live long enough. I have attached the code snippet. There is some extra piece of code to match error line numbers.
I have installed source from the latest master branch.
extern crate pcap;
extern crate argparse;
use std::sync::{Arc, Mutex};
use std::thread;
use std::sync::mpsc;
fn start_hub(){
println!("starting the pcap")
}
fn sniff(sender: &mpsc::Sender<pcap::Packet>){
println!("Sniff");
for device in pcap::Device::list().unwrap() {
println!("device");
if device.name == "wlan0" {
println!("Found device! {:?}", device);
let mut cap = device.open().unwrap();
loop{
while let Ok(packet) = cap.next() {
println!("got packet! {:?}", packet.data);
let c = packet.clone();
sender.send(c).unwrap();
}
}
}
}
}
fn depositer(receiver: &mpsc::Receiver<pcap::Packet>){
loop{
println!("Received: {:?}", receiver.recv().unwrap());
}
}
fn hub(){
println!("Hub");
}
fn fetcher(){
println!("Fetcher");
}
fn start(){
println!("Start");
// ZMQ socket
// Pcap library capture
let data = Arc::new(Mutex::new(0));
let (sender, receiver) = mpsc::channel();
let sniffer_handle: thread::JoinHandle<()>;
let depositer_handle: thread::JoinHandle<()>;
let hub_handle: thread::JoinHandle<()>;
let fetcher_handle: thread::JoinHandle<()>;
// Start sniffer
sniffer_handle = thread::spawn(move || {
sniff(&sender);
});
// Start depositer
depositer_handle = thread::spawn(move || {
depositer(&receiver);
});
// Start Hub
hub_handle = thread::spawn(|| hub());
// Start fetcher
fetcher_handle = thread::spawn(|| fetcher());
// Join all threads
sniffer_handle.join().unwrap();
depositer_handle.join().unwrap();
hub_handle.join().unwrap();
fetcher_handle.join().unwrap();
}
fn top(){
println!("top");
}
fn invalid(){
println!("Invalid args")
}
pub fn parse_arguments(){
// Attach all commands
let mut verbose = false;
let mut command = "".to_string();
{
let mut parser = argparse::ArgumentParser::new();
parser.set_description("imon is a command line utility to monitor internet data consumption");
parser.refer(&mut verbose)
.add_option(&["-v", "--verbose"], argparse::StoreTrue,
"Be verbose");
parser.refer(&mut command).required()
.add_argument("command", argparse::Store,
r#"Command to run (either "start" or "top")"#);
parser.parse_args_or_exit();
}
// Get all commands from user
match command.as_ref() {
"start" => start(),
"top" => top(),
_ => invalid(),
}
}
Output:
user@user-ThinkPad-T400 ~/c/imon> sudo ~/.cargo/bin/cargo run --bin imon -- start
[sudo] password for user:
Compiling imon v0.1.0 (file:///home/user/code/imon)
error: `cap` does not live long enough
--> src/cli.rs:24:40
|
24 | while let Ok(packet) = cap.next() {
| ^^^
|
note: reference must be valid for the anonymous lifetime #2 defined on the block at 16:45...
--> src/cli.rs:16:46
|
16 | fn sniff(sender: &mpsc::Sender<pcap::Packet>){
| ^
note: ...but borrowed value is only valid for the block suffix following statement 1 at 22:49
--> src/cli.rs:22:50
|
22 | let mut cap = device.open().unwrap();
| ^
error: aborting due to previous error
error: Could not compile `imon`.
To learn more, run the command again with --verbose.
user@user-ThinkPad-T400 ~/c/imon>
I'm using
pcaplibrary to capture wifi packet in a thread and transferring thepacketto an another thread usingchannels. While moving packet via sender to receiver thread, I get the error messagecap does not live long enough. I have attached the code snippet. There is some extra piece of code to match error line numbers.I have installed source from the latest master branch.
Output: