Skip to content

Commit

Permalink
Simplified generation of measurements.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 authored and rnijveld committed May 15, 2024
1 parent 0dbf8f1 commit d7d2aeb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 82 deletions.
6 changes: 0 additions & 6 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ path = "fuzz_targets/encrypted_server_parsing.rs"
test = false
doc = false

[[bin]]
name = "tuple_from_packet"
path = "fuzz_targets/tuple_from_packet.rs"
test = false
doc = false

[[bin]]
name = "duration_from_float"
path = "fuzz_targets/duration_from_float.rs"
Expand Down
27 changes: 0 additions & 27 deletions fuzz/fuzz_targets/tuple_from_packet.rs

This file was deleted.

18 changes: 16 additions & 2 deletions ntp-proto/src/algorithm/kalman/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ enum SourceStateInner {
#[derive(Debug, Clone)]
pub(super) struct SourceState(SourceStateInner);

const MIN_DELAY: NtpDuration = NtpDuration::from_exponent(-18);

impl SourceState {
pub fn new() -> Self {
SourceState(SourceStateInner::Initial(InitialSourceFilter {
Expand All @@ -418,6 +420,18 @@ impl SourceState {

// Returs whether the clock may need adjusting.
pub fn update_self_using_measurement(
&mut self,
source_defaults_config: &SourceDefaultsConfig,
algo_config: &AlgorithmConfig,
mut measurement: Measurement,
) -> bool {
// preprocessing
measurement.delay = measurement.delay.max(MIN_DELAY);

self.update_self_using_raw_measurement(source_defaults_config, algo_config, measurement)
}

fn update_self_using_raw_measurement(
&mut self,
source_defaults_config: &SourceDefaultsConfig,
algo_config: &AlgorithmConfig,
Expand Down Expand Up @@ -808,7 +822,7 @@ mod tests {
source.process_offset_steering(20e-3);
assert!(source.snapshot(0_usize).unwrap().state.ventry(0).abs() < 1e-7);

source.update_self_using_measurement(
source.update_self_using_raw_measurement(
&SourceDefaultsConfig::default(),
&AlgorithmConfig::default(),
Measurement {
Expand Down Expand Up @@ -865,7 +879,7 @@ mod tests {

source.progress_filtertime(base - NtpDuration::from_seconds(10e-3)); // should succeed

source.update_self_using_measurement(
source.update_self_using_raw_measurement(
&SourceDefaultsConfig::default(),
&AlgorithmConfig::default(),
Measurement {
Expand Down
2 changes: 0 additions & 2 deletions ntp-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ mod exports {
FilterAction, FilterList, IpSubnet, Server, ServerAction, ServerConfig, ServerReason,
ServerResponse, ServerStatHandler, SubnetParseError,
};
#[cfg(feature = "__internal-fuzz")]
pub use super::source::fuzz_measurement_from_packet;
#[cfg(feature = "__internal-test")]
pub use super::source::{source_snapshot, Measurement};
pub use super::source::{
Expand Down
46 changes: 4 additions & 42 deletions ntp-proto/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ impl Measurement {
send_timestamp: NtpTimestamp,
recv_timestamp: NtpTimestamp,
local_clock_time: NtpInstant,
precision: NtpDuration,
) -> Self {
Self {
delay: ((recv_timestamp - send_timestamp)
- (packet.transmit_timestamp() - packet.receive_timestamp()))
.max(precision),
- (packet.transmit_timestamp() - packet.receive_timestamp())),
offset: ((packet.receive_timestamp() - send_timestamp)
+ (packet.transmit_timestamp() - recv_timestamp))
/ 2,
Expand Down Expand Up @@ -715,13 +713,8 @@ impl NtpSource {
}

// generate a measurement
let measurement = Measurement::from_packet(
&message,
send_time,
recv_time,
local_clock_time,
self.system_snapshot.time_snapshot.precision,
);
let measurement =
Measurement::from_packet(&message, send_time, recv_time, local_clock_time);

// Process new cookies
if let Some(nts) = self.nts.as_mut() {
Expand Down Expand Up @@ -769,34 +762,6 @@ impl NtpSource {
}
}

#[cfg(feature = "__internal-fuzz")]
pub fn fuzz_measurement_from_packet(
client: u64,
client_interval: u32,
server: u64,
server_interval: u32,
client_precision: i8,
server_precision: i8,
) {
let mut packet = NtpPacket::test();
packet.set_origin_timestamp(NtpTimestamp::from_fixed_int(client));
packet.set_receive_timestamp(NtpTimestamp::from_fixed_int(server));
packet.set_transmit_timestamp(NtpTimestamp::from_fixed_int(
server.wrapping_add(server_interval as u64),
));
packet.set_precision(server_precision);

let result = Measurement::from_packet(
&packet,
NtpTimestamp::from_fixed_int(client),
NtpTimestamp::from_fixed_int(client.wrapping_add(client_interval as u64)),
NtpInstant::now(),
NtpDuration::from_exponent(client_precision),
);

assert!(result.delay >= NtpDuration::ZERO);
}

#[cfg(test)]
mod test {
use crate::{packet::NoCipher, time_types::PollIntervalLimits, NtpClock};
Expand Down Expand Up @@ -860,7 +825,6 @@ mod test {
NtpTimestamp::from_fixed_int(0),
NtpTimestamp::from_fixed_int(3),
instant,
NtpDuration::from_exponent(-32),
);
assert_eq!(result.offset, NtpDuration::from_fixed_int(0));
assert_eq!(result.delay, NtpDuration::from_fixed_int(2));
Expand All @@ -872,7 +836,6 @@ mod test {
NtpTimestamp::from_fixed_int(0),
NtpTimestamp::from_fixed_int(3),
instant,
NtpDuration::from_exponent(-32),
);
assert_eq!(result.offset, NtpDuration::from_fixed_int(1));
assert_eq!(result.delay, NtpDuration::from_fixed_int(2));
Expand All @@ -884,10 +847,9 @@ mod test {
NtpTimestamp::from_fixed_int(0),
NtpTimestamp::from_fixed_int(3),
instant,
NtpDuration::from_exponent(-32),
);
assert_eq!(result.offset, NtpDuration::from_fixed_int(1));
assert_eq!(result.delay, NtpDuration::from_fixed_int(1));
assert_eq!(result.delay, NtpDuration::from_fixed_int(-2));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions ntp-proto/src/time_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl NtpTimestamp {
self - other < NtpDuration::ZERO
}

#[cfg(any(test, feature = "__internal-fuzz"))]
#[cfg(test)]
pub(crate) const fn from_fixed_int(timestamp: u64) -> NtpTimestamp {
NtpTimestamp { timestamp }
}
Expand Down Expand Up @@ -306,12 +306,12 @@ impl NtpDuration {
}

/// Interpret an exponent `k` as `2^k` seconds, expressed as an NtpDuration
pub fn from_exponent(input: i8) -> Self {
pub const fn from_exponent(input: i8) -> Self {
Self {
duration: match input {
exp if exp > 30 => std::i64::MAX,
exp if exp > 0 && exp <= 30 => 0x1_0000_0000_i64 << exp,
exp if (-32..=0).contains(&exp) => 0x1_0000_0000_i64 >> -exp,
exp if exp >= -32 && exp <= 0 => 0x1_0000_0000_i64 >> -exp,
_ => 0,
},
}
Expand Down

0 comments on commit d7d2aeb

Please sign in to comment.