use std::error::Error; use std::io; use std::sync::mpsc::Receiver; use log::{error, info}; use quicktap::drivers::tun::TunDevice; use quicktap::drivers::tungeneric::GenericDriver; use crate::command::PacketThreadCommand; pub fn packet_thread(mut device: TunDevice, rx: Receiver) -> Result<(), Box> { loop { // Check command channel let command = match rx.try_recv() { Ok(d) => Some(d), Err(e) if matches!(e, std::sync::mpsc::TryRecvError::Empty) => None, Err(e) => return Err(e.into()) }; if let Some(cmd) = command { if let PacketThreadCommand::Stop = cmd { info!("packet thread: recieved command to stop by control process"); return Ok(()) } } let packet = match device.read() { Ok(p) => Some(p), Err(e) => match e.kind() { io::ErrorKind::WouldBlock => None, _ => { error!("Error while reading packets from interface: {}", e); return Err(e.into()); } } }; if let Some(p) = packet { info!("recv_packet_on_tun {:?}", p.header); // parse and route } } }