fix(probe): preserve measurement through wrap and network outages
Build / build (pull_request) Successful in 44s
DevSkim / DevSkim (pull_request) Canceled after 0s
rust-clippy analyze / Run rust-clippy analyzing (pull_request) Canceled after 0s

This commit is contained in:
Nils Andreas Svee committed 2026-09-24 16:51:53 +07:00
1 parent 8fb051d48f
commit bd0a89c694
5 files changed
+153 -18

No files matched your search

+48 -3
View File
@@ -19,6 +19,20 @@ pub struct PingerICMPTimestampListener {}
pub struct PingerICMPTimestampSender {}
const MS_PER_DAY: i64 = 86_400_000;
// RFC 792 timestamps wrap at midnight UTC; real one-way delays are below half a day.
fn timestamp_delta(later: i64, earlier: i64) -> i64 {
let delta = later - earlier;
if delta < -MS_PER_DAY / 2 {
delta + MS_PER_DAY
} else if delta > MS_PER_DAY / 2 {
delta - MS_PER_DAY
} else {
delta
}
}
impl PingListener for PingerICMPTimestampListener {
// Result: RTT, down time, up time
fn parse_packet(
@@ -46,12 +60,19 @@ impl PingListener for PingerICMPTimestampListener {
});
}
// The high bit marks a timestamp that is not milliseconds since midnight UTC.
if receive & 0x8000_0000 != 0 || transmit & 0x8000_0000 != 0 {
return Err(PingError::InvalidPacket(
"non-standard RFC 792 timestamp".into(),
));
}
let time_now = Time::new(ClockId::Realtime);
let time_since_midnight = time_now.get_time_since_midnight();
let rtt = (time_since_midnight - originate as i64) as f64;
let dl_time = (time_since_midnight - transmit as i64) as f64;
let ul_time = (receive as i64 - originate as i64) as f64;
let rtt = timestamp_delta(time_since_midnight, originate as i64) as f64;
let dl_time = timestamp_delta(time_since_midnight, transmit as i64) as f64;
let ul_time = timestamp_delta(receive as i64, originate as i64) as f64;
Ok(PingReply {
reflector,
@@ -88,3 +109,27 @@ impl PingSender for PingerICMPTimestampSender {
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use icmp_socket2::packet::WithTimestampReply;
#[test]
fn timestamps_wrap_at_midnight_in_both_directions() {
assert_eq!(timestamp_delta(20, 86_399_900), 120);
assert_eq!(timestamp_delta(86_399_900, 20), -120);
}
#[test]
fn non_standard_rfc_792_timestamps_are_rejected() {
let packet = Icmpv4Packet::with_timestamp_reply(7, 1, 10, 0x8000_0001, 20).unwrap();
let result = PingerICMPTimestampListener {}.parse_packet(
7,
"192.0.2.1".parse().unwrap(),
MeasurementType::IcmpTimestamps,
packet,
);
assert!(matches!(result, Err(PingError::InvalidPacket(_))));
}
}