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),
|
InvalidNumber(#[from] io::Error),
|
||||||
#[error("Error parsing packet")]
|
#[error("Error parsing packet")]
|
||||||
InvalidPacket(#[from] ReadError),
|
InvalidPacket(#[from] ReadError),
|
||||||
|
#[error("Invalid protocol")]
|
||||||
|
InvalidProtocol(String),
|
||||||
|
#[error("Invalid packet type")]
|
||||||
|
InvalidType(String),
|
||||||
|
#[error("No transport")]
|
||||||
|
NoTransport,
|
||||||
#[error("Socket error")]
|
#[error("Socket error")]
|
||||||
Socket(#[from] Errno),
|
Socket(#[from] Errno),
|
||||||
#[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")]
|
#[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")]
|
||||||
@@ -98,11 +104,9 @@ pub trait PingListener {
|
|||||||
|
|
||||||
let reply = match self.parse_packet(id, addr, buf) {
|
let reply = match self.parse_packet(id, addr, buf) {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
error!(
|
// parse_packet will throw an error if it's an unknown protocol etc.
|
||||||
"Something went wrong while parsing packet: {}",
|
// so just quietly move on
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+9
-6
@@ -51,15 +51,18 @@ impl PingListener for PingerICMPEchoListener {
|
|||||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
+ (time_now.tv_nsec() as f64 / 1e9),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => {
|
type_ => {
|
||||||
todo!()
|
return Err(PingError::InvalidType(format!("{:?}", type_)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Some(Icmpv6(_)) => {
|
Some(Icmpv6(slice)) => {
|
||||||
todo!()
|
return Err(PingError::InvalidProtocol(format!("{:?}", slice)));
|
||||||
}
|
}
|
||||||
_ => {
|
Some(type_) => {
|
||||||
unimplemented!()
|
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::pinger::{PingError, PingListener, PingReply, PingSender};
|
||||||
use crate::utils::Utils;
|
use crate::utils::Utils;
|
||||||
use etherparse::icmpv4::TimestampMessage;
|
use etherparse::icmpv4::TimestampMessage;
|
||||||
use etherparse::TransportSlice::Icmpv4;
|
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
||||||
use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket};
|
use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket};
|
||||||
use nix::time::{clock_gettime, ClockId};
|
use nix::time::{clock_gettime, ClockId};
|
||||||
|
|
||||||
@@ -52,12 +52,18 @@ impl PingListener for PingerICMPTimestampListener {
|
|||||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
+ (time_now.tv_nsec() as f64 / 1e9),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => {
|
type_ => {
|
||||||
unimplemented!()
|
return Err(PingError::InvalidType(format!("{:?}", type_)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {
|
Some(Icmpv6(slice)) => {
|
||||||
unimplemented!()
|
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