ntp_usg
A Network Time Protocol (NTP) packet parsing and client library written in Rust.
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, and interleaved mode (RFC 9769)
- 🌐 IPv6 Dual-Stack: Automatic IPv4/IPv6 socket binding
- 🧩
no_stdSupport: Core parsing works withoutstdoralloc - ⏱️ Clock Adjustment: Platform-native slew/step correction (Linux, macOS, Windows)
- 🦀 Modern Rust: Edition 2024 with MSRV 1.93
- ✅ Well Tested: CI/CD on Linux, macOS, and Windows
Installation
Add this to your Cargo.toml:
[]
= "2.0"
Minimum Supported Rust Version (MSRV): 1.93 Edition: 2024
Feature Flags
| Feature | Default | Description |
|---|---|---|
std |
Yes | Full I/O, networking, and byteorder-based APIs |
alloc |
No | Vec-based extension field types without full std |
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) |
clock |
No | System clock slew/step adjustment (Linux, macOS, Windows) |
For no_std environments, disable default features:
[]
= { = "2.0", = false } # core parsing only
= { = "2.0", = false, = ["alloc"] } # + Vec-based types
Usage
Basic Example
use TimeZone;
Custom Timeout
use Duration;
let response = request_with_timeout?;
Async with Tokio
Enable the tokio feature:
[]
= { = "2.0", = ["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:
[]
= { = "2.0", = ["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:
[]
= { = "2.0", = ["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:
[]
= { = "2.0", = ["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?;
Multiple Servers
See examples/multiple_servers.rs for a complete example of querying multiple NTP servers.
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)
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 functionality
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.