Skip to content

Commit

Permalink
fix(error): error when 0 interfaces to listen to are found (#69)
Browse files Browse the repository at this point in the history
* fix(error): error when 0 interfaces to listen to are found

* style(clippy): clippy

* fix(error): differentiate between privileges errors and other errors

* style(clippy): clippy
  • Loading branch information
imsnif authored Jan 4, 2020
1 parent cf64a81 commit b8e9835
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ::std::io::{self, stdin, Write};
use ::termion::event::Event;
use ::termion::input::TermRead;

use ::std::io::ErrorKind;
use ::std::time;

use signal_hook::iterator::Signals;
Expand Down Expand Up @@ -32,23 +33,17 @@ impl Iterator for KeyboardEvents {

fn get_datalink_channel(
interface: &NetworkInterface,
) -> Result<Box<dyn DataLinkReceiver>, failure::Error> {
) -> Result<Box<dyn DataLinkReceiver>, std::io::Error> {
let mut config = Config::default();
config.read_timeout = Some(time::Duration::new(1, 0));

match datalink::channel(interface, config) {
Ok(Ethernet(_tx, rx)) => Ok(rx),
Ok(_) => failure::bail!("Unknown interface type"),
Err(e) => {
match e.kind() {
std::io::ErrorKind::PermissionDenied => failure::bail!("Failed to listen on network interface due to permission error. Try running with sudo"),
_ => failure::bail!(
"Failed to listen on network interface {}: {}",
interface.name,
e
),
}
}
Ok(_) => Err(std::io::Error::new(
ErrorKind::Other,
"Unsupported interface type",
)),
Err(e) => Err(e),
}
}

Expand Down Expand Up @@ -103,10 +98,26 @@ pub fn get_input(

let network_frames = network_interfaces
.iter()
.map(|iface| get_datalink_channel(iface))
.map(|iface| get_datalink_channel(iface));

let available_network_frames = network_frames
.clone()
.filter_map(Result::ok)
.collect::<Vec<_>>();

if available_network_frames.is_empty() {
for iface in network_frames {
if let Some(iface_error) = iface.err() {
if let ErrorKind::PermissionDenied = iface_error.kind() {
failure::bail!(
"Insufficient permissions to listen on network interface(s). Try running with sudo.",
)
}
}
}
failure::bail!("Failed to find any network interface to listen on.");
}

let keyboard_events = Box::new(KeyboardEvents);
let write_to_stdout = create_write_to_stdout();
let (on_winch, cleanup) = sigwinch();
Expand All @@ -120,7 +131,7 @@ pub fn get_input(

Ok(OsInputOutput {
network_interfaces,
network_frames,
network_frames: available_network_frames,
get_open_sockets,
keyboard_events,
dns_client,
Expand Down

0 comments on commit b8e9835

Please # to comment.