ntp_usg
A Network Time Protocol (NTP) library written in Rust, organized as a Cargo workspace with four crates:
| Crate | Lib name | Description |
|---|---|---|
ntp_usg-proto |
ntp_proto |
Protocol types, extension fields, and NTS cryptographic primitives |
ntp_usg-client |
ntp_client |
NTP client (sync, async tokio/smol, NTS, clock adjustment) |
ntp_usg-server |
ntp_server |
NTP server (tokio/smol, NTS-KE) |
ntp_usg-wasm |
ntp_wasm |
Browser/Node.js NTP client via WebAssembly |
Features
🎯 Version 4.10.0 - Production-Grade NTP
- RFC 5905 Full Compliance: Selection, clustering, clock discipline (PLL/FLL), symmetric modes, and broadcast mode
- RFC 4330 SNTP API: Simplified client API for one-off time queries
- RFC 7822 Extension Registry: Generic dispatch system for extension field handlers
- NTPv5 Draft Support: draft-ietf-ntp-ntpv5 client and server with Bloom filter reference IDs
- Roughtime Client: Authenticated coarse time via Ed25519 signatures (draft-ietf-ntp-roughtime)
- Reference Clocks: GPS and PPS drivers for Stratum 1 operation, hardware timestamping
- Post-Quantum NTS: ML-KEM (X25519MLKEM768) key exchange via aws-lc-rs
- WASM Support: Browser/Node.js NTP client via WebAssembly
- Structured Tracing:
tracingintegration with backward-compatiblelogbridge - Custom Error Types: Typed error enums with
io::Errordowncast support
Core Features
- 🔒 Safe & Secure:
#![deny(unsafe_code)]crate-wide; only platform FFI in the optionalclockmodule uses unsafe - 📚 Well Documented: Comprehensive API documentation with examples
- ⚡ Configurable Timeouts: Control request timeouts for different network conditions
- 🔄 Async Ready: Optional async support via Tokio or smol
- 🕐 Y2036 Safe: Era-aware timestamp handling for the NTP 32-bit rollover
- 🌍 Multi-Server Support: Query multiple NTP servers for improved reliability
- 🔐 Network Time Security: NTS (RFC 8915) with TLS 1.3 key establishment and AEAD authentication
- 📡 Continuous Client: Adaptive poll interval, multi-peer selection, and interleaved mode (RFC 9769)
- 🌐 IPv6 Dual-Stack: Automatic IPv4/IPv6 socket binding
- 🧩
no_stdSupport: Core protocol parsing works withoutstdoralloc - ⏱️ Clock Adjustment: Platform-native slew/step correction (Linux, macOS, Windows)
- 📡 NTP Server: Full NTPv4 server with rate limiting, access control, and interleaved mode
- 🔭 Observability: Structured
tracingspans with backward-compatiblelogfacade - 🦀 Modern Rust: Edition 2024 with MSRV 1.93
- ✅ Well Tested: 750+ tests, CI/CD on Linux, macOS, and Windows
Installation
Add the crate(s) you need to your Cargo.toml:
[]
# Protocol types only (also supports no_std)
= "4.10"
# NTP client
= { = "4.10", = ["tokio"] }
# NTP server
= { = "4.10", = ["tokio"] }
Minimum Supported Rust Version (MSRV): 1.93 Edition: 2024
Feature Flags
ntp_usg-proto
| Feature | Default | Description |
|---|---|---|
std |
Yes | Full I/O and byteorder-based APIs |
alloc |
No | Vec-based extension field types without full std |
nts |
No | NTS cryptographic primitives (AEAD, cookie handling) |
ntp_usg-client
| Feature | Default | Description |
|---|---|---|
tokio |
No | Async NTP client using Tokio |
smol-runtime |
No | Async NTP client using smol |
nts |
No | NTS authentication (Tokio + rustls) |
nts-smol |
No | NTS authentication (smol + futures-rustls) |
pq-nts |
No | Post-quantum NTS key exchange (ML-KEM via aws-lc-rs) |
clock |
No | System clock slew/step adjustment (Linux, macOS, Windows) |
discipline |
No | PLL/FLL clock discipline algorithm (implies clock) |
symmetric |
No | Symmetric active/passive mode (RFC 5905 modes 1 & 2) |
broadcast |
No | Broadcast client (mode 5, deprecated by RFC 8633) |
refclock |
No | Reference clock abstraction layer (implies tokio) |
gps |
No | GPS reference clock driver (implies refclock) |
pps |
No | PPS reference clock driver (implies refclock) |
hwts |
No | Hardware timestamping support (implies refclock) |
roughtime |
No | Roughtime client (draft-ietf-ntp-roughtime, implies tokio) |
ntpv5 |
No | NTPv5 draft support (draft-ietf-ntp-ntpv5) |
socket-opts |
No | DSCP and IPV6_V6ONLY socket options via socket2 |
ntp_usg-server
| Feature | Default | Description |
|---|---|---|
tokio |
No | NTP server using Tokio |
smol-runtime |
No | NTP server using smol |
nts |
No | NTS-KE server (Tokio + rustls) |
nts-smol |
No | NTS-KE server (smol + futures-rustls) |
pq-nts |
No | Post-quantum NTS key exchange (ML-KEM via aws-lc-rs) |
symmetric |
No | Symmetric passive mode (RFC 5905 mode 2) |
broadcast |
No | Broadcast mode (mode 5, deprecated by RFC 8633) |
refclock |
No | Reference clock support for Stratum 1 (implies tokio) |
ntpv5 |
No | NTPv5 draft support (draft-ietf-ntp-ntpv5) |
socket-opts |
No | DSCP, IPV6_V6ONLY, and multicast socket options |
For no_std environments, use the proto crate with default features disabled:
[]
= { = "4.10", = false } # core parsing only
= { = "4.10", = false, = ["alloc"] } # + Vec-based types
Usage
SNTP (Simple Network Time Protocol)
For simple, one-off time queries, use the SNTP API (RFC 4330 compliant):
use sntp;
With async:
async
Basic Example (Full NTP)
use TimeZone;
Custom Timeout
use Duration;
let response = request_with_timeout?;
Async with Tokio
Enable the tokio feature:
[]
= { = "4.10", = ["tokio"] }
= { = "1", = ["rt-multi-thread", "macros"] }
async
Continuous Client
The continuous client polls servers with adaptive intervals and supports interleaved mode (RFC 9769):
use NtpClient;
async
NTS (Network Time Security)
Enable the nts feature for authenticated NTP:
[]
= { = "4.10", = ["nts"] }
= { = "1", = ["rt-multi-thread", "macros"] }
use NtsSession;
async
NTS Continuous Client
Combine NTS authentication with the continuous polling client:
use NtpClient;
async
Async with smol
Enable the smol-runtime feature:
[]
= { = "4.10", = ["smol-runtime"] }
= "2"
use Duration;
The smol continuous client uses Arc<RwLock<NtpSyncState>> for state sharing:
use NtpClient;
Clock Adjustment
Enable the clock feature to correct the system clock based on NTP measurements:
[]
= { = "4.10", = ["clock", "tokio"] }
use clock;
// Gradual correction (slew) for small offsets
slew_clock?;
// Immediate correction (step) for large offsets
step_clock?;
// Automatic: slew if |offset| <= 128ms, step otherwise
let method = apply_correction?;
Observability
The library uses tracing for structured diagnostics. To see logs, initialize a subscriber:
// With tracing-subscriber (recommended for new projects):
fmt
.with_env_filter
.init;
// Or with env_logger (backward-compatible via the log bridge):
init;
Set RUST_LOG=ntp_client=debug (or ntp_server=debug) for per-request diagnostics including peer addresses, poll intervals, and NTS session state.
NTP Server
Enable the tokio feature on the server crate:
[]
= { = "4.10", = ["tokio"] }
= { = "1", = ["rt-multi-thread", "macros"] }
use Stratum;
use NtpServer;
async
Multiple Servers
See crates/ntp_usg-client/examples/multiple_servers.rs for a complete example of querying multiple NTP servers.
Examples
Production Examples (v4.10.0+)
The following examples demonstrate production-ready deployments with comprehensive monitoring and error handling:
Multi-Peer Deployment - examples/multi_peer_deployment.rs
Demonstrates RFC 5905 selection, clustering, and combine algorithms with 5 diverse NTP servers. Includes real-time health assessment and offset trend analysis.
NTS Multi-Peer - examples/nts_multi_peer.rs
Mixed NTS-authenticated and standard NTP deployment for maximum security and resilience. Tracks security posture with NTS failure monitoring.
System Daemon - examples/daemon.rs
Production-ready long-running service with structured logging, health-based alerts, and systemd integration documentation.
Basic Examples
Run the included examples to see the library in action:
# Basic request example
# Custom timeout demonstration
# Query multiple servers
# Detailed packet information
# Async concurrent queries (requires tokio feature)
# Continuous client with poll management (requires tokio feature)
# NTS-authenticated request (requires nts feature)
# NTS continuous client (requires nts feature)
# Smol one-shot request
# Smol continuous client
# Clock adjustment (requires root/sudo on Unix, Administrator on Windows)
# NTP server (requires tokio feature)
# NTS server (requires nts feature + TLS certs)
Roadmap
- async support (tokio)
- NTP era handling (Y2036)
- IPv6 dual-stack support
- Continuous client with adaptive polling
- Interleaved mode (RFC 9769)
- Network Time Security (RFC 8915)
- IO-independent parsing (
FromBytes/ToBytestraits) -
no_stdsupport (with optionalalloc) - smol support (one-shot, continuous, and NTS)
- System clock adjustment (slew/step on Linux, macOS, Windows)
- NTP server with NTS-KE
- Workspace restructure (proto, client, server crates)
- Reference clock interface (GPS, PPS)
- Hardware timestamping
- NTPv5 draft support
- Roughtime client
- Post-quantum NTS (ML-KEM)
- WASM support
- Structured tracing
- Custom error types
- FIPS 140-3 validated NTS AEAD (trait abstraction ready, awaiting certified backend)
Contributing
Pull requests and issues are welcome! Please see our GitHub repository for more information.
License
ntp_usg is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.