Improve pinger error handling
This commit is contained in:
1 parent
290450ac1e
commit
9a1901b213
3 files changed
+29
-16
No files matched your search
+9
-5
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
+9
-6
@@ -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);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
+11
-5
@@ -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);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user