From 9a1901b2136d1fa66d2ddcb06c9b329816382019 Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Thu, 13 Oct 2022 17:16:01 +0200 Subject: [PATCH] Improve pinger error handling --- src/pinger.rs | 14 +++++++++----- src/pinger_icmp.rs | 15 +++++++++------ src/pinger_icmp_ts.rs | 16 +++++++++++----- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/pinger.rs b/src/pinger.rs index 67866e1..ef895e3 100644 --- a/src/pinger.rs +++ b/src/pinger.rs @@ -21,6 +21,12 @@ pub enum PingError { InvalidNumber(#[from] io::Error), #[error("Error parsing packet")] InvalidPacket(#[from] ReadError), + #[error("Invalid protocol")] + InvalidProtocol(String), + #[error("Invalid packet type")] + InvalidType(String), + #[error("No transport")] + NoTransport, #[error("Socket error")] Socket(#[from] Errno), #[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")] @@ -98,11 +104,9 @@ pub trait PingListener { let reply = match self.parse_packet(id, addr, buf) { Ok(val) => val, - Err(e) => { - error!( - "Something went wrong while parsing packet: {}", - e.to_string() - ); + Err(_) => { + // parse_packet will throw an error if it's an unknown protocol etc. + // so just quietly move on continue; } }; diff --git a/src/pinger_icmp.rs b/src/pinger_icmp.rs index de8d711..412ea94 100644 --- a/src/pinger_icmp.rs +++ b/src/pinger_icmp.rs @@ -51,15 +51,18 @@ impl PingListener for PingerICMPEchoListener { + (time_now.tv_nsec() as f64 / 1e9), }); } - _ => { - todo!() + type_ => { + return Err(PingError::InvalidType(format!("{:?}", type_))); } }, - Some(Icmpv6(_)) => { - todo!() + Some(Icmpv6(slice)) => { + return Err(PingError::InvalidProtocol(format!("{:?}", slice))); } - _ => { - unimplemented!() + Some(type_) => { + return Err(PingError::InvalidProtocol(format!("{:?}", type_))); + } + None => { + return Err(PingError::NoTransport); } }, } diff --git a/src/pinger_icmp_ts.rs b/src/pinger_icmp_ts.rs index 96e47fb..1d267ba 100644 --- a/src/pinger_icmp_ts.rs +++ b/src/pinger_icmp_ts.rs @@ -3,7 +3,7 @@ use std::net::IpAddr; use crate::pinger::{PingError, PingListener, PingReply, PingSender}; use crate::utils::Utils; use etherparse::icmpv4::TimestampMessage; -use etherparse::TransportSlice::Icmpv4; +use etherparse::TransportSlice::{Icmpv4, Icmpv6}; use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket}; use nix::time::{clock_gettime, ClockId}; @@ -52,12 +52,18 @@ impl PingListener for PingerICMPTimestampListener { + (time_now.tv_nsec() as f64 / 1e9), }); } - _ => { - unimplemented!() + type_ => { + return Err(PingError::InvalidType(format!("{:?}", type_))); } }, - _ => { - unimplemented!() + Some(Icmpv6(slice)) => { + return Err(PingError::InvalidType(format!("{:?}", slice))); + } + Some(type_) => { + return Err(PingError::InvalidType(format!("{:?}", type_))); + } + None => { + return Err(PingError::NoTransport); } }, }