Skip to main content

ntp_server/
lib.rs

1// Copyright 2026 U.S. Federal Government (in countries where recognized)
2// SPDX-License-Identifier: Apache-2.0
3
4//! NTP server library with tokio/smol runtime support and NTS-KE.
5//!
6//! This crate provides NTPv4 server implementations using either the tokio
7//! or smol async runtimes, with optional NTS (Network Time Security) support.
8
9#![warn(missing_docs)]
10
11// Re-export protocol types from ntp_proto for convenience.
12pub use ntp_proto::{error, extension, protocol, unix_time};
13
14/// Shared NTS logic re-exported from `ntp_proto`.
15#[cfg(any(feature = "nts", feature = "nts-smol"))]
16pub(crate) use ntp_proto::nts_common;
17
18/// TLS configuration for NTS-KE server (crypto provider selection).
19#[cfg(any(feature = "nts", feature = "nts-smol"))]
20pub(crate) mod tls_config;
21
22/// Default listen address based on the `ipv4` feature flag.
23///
24/// Without `ipv4`: binds to `[::]` (IPv6 dual-stack, accepts both IPv4 and IPv6).
25/// With `ipv4`: binds to `0.0.0.0` (IPv4 only).
26#[cfg(any(feature = "tokio", feature = "smol-runtime"))]
27pub(crate) fn default_listen_addr(port: u16) -> String {
28    #[cfg(not(feature = "ipv4"))]
29    {
30        format!("[::]:{port}")
31    }
32    #[cfg(feature = "ipv4")]
33    {
34        format!("0.0.0.0:{port}")
35    }
36}
37
38/// Socket options for `IPV6_V6ONLY` and DSCP/Traffic Class control.
39#[cfg(any(feature = "tokio", feature = "smol-runtime"))]
40mod socket_opts;
41
42/// Shared types and logic for the NTP server.
43///
44/// Provides request validation, response building, rate limiting, access control,
45/// and interleaved mode tracking per RFC 5905, RFC 8633, and RFC 9769.
46#[cfg(any(feature = "tokio", feature = "smol-runtime"))]
47pub mod server_common;
48
49/// NTP server using the Tokio runtime.
50///
51/// Provides a configurable NTPv4 server that responds to client requests.
52#[cfg(feature = "tokio")]
53pub mod server;
54
55/// NTP server using the smol runtime.
56///
57/// Provides the same server functionality as [`server`] but using the smol
58/// async runtime.
59#[cfg(feature = "smol-runtime")]
60pub mod smol_server;
61
62/// Shared NTS server logic (cookie generation, master key management, NTS request processing).
63#[cfg(any(feature = "nts", feature = "nts-smol"))]
64pub mod nts_server_common;
65
66/// NTS-KE server using the Tokio runtime (RFC 8915).
67///
68/// Provides a TLS 1.3 listener for NTS Key Establishment, distributing cookies
69/// and negotiating AEAD algorithms with NTS clients.
70#[cfg(feature = "nts")]
71pub mod nts_ke_server;
72
73/// NTS-KE server using the smol runtime (RFC 8915).
74///
75/// Provides the same NTS-KE server functionality as [`nts_ke_server`] but
76/// using the smol async runtime and futures-rustls.
77#[cfg(feature = "nts-smol")]
78pub mod smol_nts_ke_server;
79
80/// NTP broadcast mode (mode 5) packet building per RFC 5905 Section 8.
81///
82/// Deprecated by BCP 223 (RFC 8633) but implemented for spec completeness.
83#[cfg(all(
84    feature = "broadcast",
85    any(feature = "tokio", feature = "smol-runtime")
86))]
87pub mod broadcast;
88
89/// IPv6 multicast NTP discovery support.
90///
91/// Extends broadcast mode with IPv6-specific multicast group management
92/// using `socket2` for `IPV6_JOIN_GROUP` socket options.
93#[cfg(feature = "socket-opts")]
94pub mod multicast;