summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2020-01-04 19:59:12 +0100
committerGitHub <noreply@github.com>2020-01-04 19:59:12 +0100
commitb8e98356abb158ec4499edd3a384d389dd8cb0c3 (patch)
tree831b00dd8ec7475dcbec4f975faf47dfc95d942b
parentcf64a81119aea1ec6f3bb432fcdb7078da11e709 (diff)
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
-rw-r--r--src/os/shared.rs39
1 files changed, 25 insertions, 14 deletions
diff --git a/src/os/shared.rs b/src/os/shared.rs
index 8fd1300..1f71c61 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<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),
}
}
@@ -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();
@@ -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,