Expand description
Rust SNTP client
§Overview
This crate provides a method for sending requests to NTP servers and process responses, extracting received timestamp. Supported SNTP protocol versions:
§Usage
Put this in your Cargo.toml
:
[dependencies]
sntpc = "0.3.9"
§Features
Sntpc supports several features:
std
: includes functionality that depends on the standard libraryutils
: includes functionality that mostly OS specific and allows system time synclog
: enables library debug output during executionasync
: enables asynchronous feature support
Warning:
utils
feature is not stable and may change in the future.
§Details
There are multiple approaches how the library can be used:
- under environments where a networking stuff is hidden in system/RTOS kernel,
get_time
can be used since it encapsulates network I/O - under environments where TCP/IP stack requires to call some helper functions like
poll
,wait
, etc. and/or there are no options to perform I/O operations within a single call,sntp_send_request
andsntp_process_response
can be used
As sntpc
supports no_std
environment as well, it was
decided to provide a set of traits to implement for a network object (UdpSocket
)
and timestamp generator:
NtpUdpSocket
trait should be implemented forUdpSocket
-like objects for the library to be able to send and receive data from NTP serversNtpTimestampGenerator
trait should be implemented for timestamp generator objects to provide the library with system related timestamps
§Logging support
Library debug logs can be enabled in executables by enabling log
feature. Server
addresses, response payload will be printed.
§Example
use std::net::UdpSocket;
use std::time::Duration;
fn main() {
let socket =
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
socket
.set_read_timeout(Some(Duration::from_secs(2)))
.expect("Unable to set UDP socket read timeout");
let result = sntpc::simple_get_time("time.google.com:123", &socket);
match result {
Ok(time) => {
println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
}
Err(err) => println!("Err: {:?}", err),
}
}
For more complex example with custom timestamp generator and UDP socket implementation, see
examples/smoltcp_request.rs
.
For usage SNTP-client in an asynchronous environment, see examples/tokio.rs
Structs§
- SNTP client context that contains of objects that may be required for client’s operation
- SNTP request result representation
- Preserve SNTP request sending operation result required during receiving and processing state
- Standard library timestamp generator wrapper type that relies on
std::time
to provide timestamps during SNTP client operations
Enums§
- The error type for SNTP client Errors originate on network layer or during processing response from a NTP server
Traits§
- A trait encapsulating timestamp generator’s operations
- A trait encapsulating UDP socket interface required for SNTP client operations
Functions§
- Convert second fraction value to microseconds value
- Convert second fraction value to milliseconds value
- Convert second fraction value to nanoseconds value
- Convert second fraction value to picoseconds value
- Send request to a NTP server with the given address and process the response in a single call
- Supplementary
get_time
alternative that wraps provided UDP socket into a wrapper type that implements necessary traits for SNTP client proper operation - Process SNTP response from a server
- Send SNTP request to a server
Type Aliases§
- SNTP library result type