Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle disconnected interfaces (and periodically attempt to re-connect) #191

Merged
merged 4 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/network/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use ::pnet::packet::udp::UdpPacket;
use ::pnet::packet::Packet;

use ::ipnetwork::IpNetwork;
use ::std::io::{self, Result};
use ::std::net::{IpAddr, SocketAddr};
use ::std::thread::park_timeout;

use crate::network::{Connection, Protocol};
use crate::os::shared::get_datalink_channel;

const PACKET_WAIT_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(10);
const CHANNEL_RESET_DELAY: std::time::Duration = std::time::Duration::from_millis(1000);

#[derive(Debug)]
pub struct Segment {
Expand Down Expand Up @@ -96,7 +102,20 @@ impl Sniffer {
}
}
pub fn next(&mut self) -> Option<Segment> {
let bytes = self.network_frames.next().ok()?;
let bytes = match self.network_frames.next() {
Ok(bytes) => bytes,
Err(err) => match err.kind() {
std::io::ErrorKind::TimedOut => {
park_timeout(PACKET_WAIT_TIMEOUT);
return None;
}
_ => {
park_timeout(CHANNEL_RESET_DELAY);
self.reset_channel().ok();
return None;
}
},
};
// See https://github.com/libpnet/libpnet/blob/master/examples/packetdump.rs
// VPN interfaces (such as utun0, utun1, etc) have POINT_TO_POINT bit set to 1
let payload_offset = if (self.network_interface.is_loopback()
Expand Down Expand Up @@ -133,6 +152,11 @@ impl Sniffer {
}
}
}
pub fn reset_channel(&mut self) -> Result<()> {
self.network_frames = get_datalink_channel(&self.network_interface)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Interface not available"))?;
Ok(())
}
fn handle_v6(ip_packet: Ipv6Packet, network_interface: &NetworkInterface) -> Option<Segment> {
let (protocol, source_port, destination_port, data_length) =
extract_transport_protocol!(ip_packet);
Expand Down
2 changes: 1 addition & 1 deletion src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ mod lsof_utils;
pub(self) mod windows;

mod errors;
mod shared;
pub(crate) mod shared;

pub use shared::*;
2 changes: 1 addition & 1 deletion src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Iterator for TerminalEvents {
}
}

fn get_datalink_channel(
pub(crate) fn get_datalink_channel(
interface: &NetworkInterface,
) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceErrorKind> {
let mut config = Config::default();
Expand Down