Skip to content

Rework nts #1911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ clock-steering = "0.2.1"
pps-time = "0.2.3"

# TLS
rustls23 = { package = "rustls", version = "0.23.16", features = ["logging", "std", "tls12"] }
rustls23 = { package = "rustls", version = "0.23.16", features = ["logging", "std"] }
rustls-platform-verifier = "0.5.0"
tokio-rustls = { version = "0.26.0", features = ["logging", "tls12"] } # testing only
tokio-rustls = { version = "0.26.0", features = ["logging"] }

# crypto
aead = "0.5.0"
Expand Down
2 changes: 2 additions & 0 deletions ntp-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ nts-pool = []
md-5.workspace = true
rand.workspace = true
tracing.workspace = true
tokio = { workspace = true, features = ["io-util"] }
tokio-rustls.workspace = true
serde.workspace = true
rustls23.workspace = true
rustls-platform-verifier.workspace = true
Expand Down
61 changes: 59 additions & 2 deletions ntp-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod identifiers;
mod io;
mod ipfilter;
mod keyset;
mod nts;
mod nts_record;
mod packet;
mod server;
Expand All @@ -33,6 +34,60 @@ pub(crate) mod exitcode {
pub const SOFTWARE: i32 = 70;
}

const NTP_DEFAULT_PORT: u16 = 123;

// This is a mod so we can control visibility for the moment, but these really are intended to be the top-level things.
mod generic {
use std::fmt::Display;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum NtpVersion {
V3,
V4,
V5,
}

impl NtpVersion {
pub fn as_u8(self) -> u8 {
self.into()
}
}

#[derive(Debug)]
pub struct InvalidNtpVersion(u8);

impl Display for InvalidNtpVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid NTP version: {}", self.0)
}
}

impl std::error::Error for InvalidNtpVersion {}

impl TryFrom<u8> for NtpVersion {
type Error = InvalidNtpVersion;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
3 => Ok(NtpVersion::V3),
4 => Ok(NtpVersion::V4),
5 => Ok(NtpVersion::V5),
e => Err(InvalidNtpVersion(e)),
}
}
}

impl From<NtpVersion> for u8 {
fn from(value: NtpVersion) -> Self {
match value {
NtpVersion::V3 => 3,
NtpVersion::V4 => 4,
NtpVersion::V5 => 5,
}
}
}
}

mod exports {
pub use super::algorithm::{
AlgorithmConfig, KalmanClockController, KalmanControllerMessage, KalmanSourceController,
Expand Down Expand Up @@ -82,15 +137,17 @@ mod exports {
#[cfg(feature = "__internal-fuzz")]
pub use super::nts_record::fuzz_key_exchange_server_decoder;
pub use super::nts_record::{
KeyExchangeClient, KeyExchangeError, KeyExchangeResult, KeyExchangeServer, NtpVersion,
NtsRecord, NtsRecordDecoder, WriteError,
KeyExchangeClient, KeyExchangeError, KeyExchangeResult, KeyExchangeServer, NtsRecord,
NtsRecordDecoder, WriteError,
};

pub use super::cookiestash::MAX_COOKIES;

pub mod v5 {
pub use crate::packet::v5::server_reference_id::{BloomFilter, ServerId};
}

pub use super::generic::NtpVersion;
}

#[cfg(feature = "__internal-api")]
Expand Down
Loading
Loading