Crate sntpc

source ·
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.7"

Features

Sntpc supports several features:

  • std: includes functionality that depends on the standard library
  • utils: includes functionality that mostly OS specific and allows system time sync
  • log: enables library debug output during execution
  • async: 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 and sntp_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 for UdpSocket-like objects for the library to be able to send and receive data from NTP servers
  • NtpTimestampGenerator 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

Type Aliases

  • SNTP library result type