From b8e98356abb158ec4499edd3a384d389dd8cb0c3 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sat, 4 Jan 2020 19:59:12 +0100 Subject: [PATCH] fix(error): error when 0 interfaces to listen to are found (#69) * 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 --- src/os/shared.rs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/os/shared.rs b/src/os/shared.rs index 8fd130026..1f71c61da 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -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; @@ -32,23 +33,17 @@ impl Iterator for KeyboardEvents { fn get_datalink_channel( interface: &NetworkInterface, -) -> Result, failure::Error> { +) -> Result, 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), } } @@ -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::>(); + 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(); @@ -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,