Skip to content

vpetrigo/sntpc

Repository files navigation

sntpc test

Simple Rust SNTP client


This crate provides a method for sending requests to NTP servers and process responses, extracting received timestamp.

Supported SNTP protocol versions:

Documentation


More information about this crate can be found in the crate documentation

Usage example

use std::net::UdpSocket;
use std::thread;
use std::time::Duration;

#[allow(dead_code)]
const POOL_NTP_ADDR: &str = "pool.ntp.org:123";
#[allow(dead_code)]
const GOOGLE_NTP_ADDR: &str = "time.google.com:123";

fn main() {
    for _ in 0..5 {
        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(POOL_NTP_ADDR, &socket);

        match result {
            Ok(time) => {
                assert_ne!(time.sec(), 0);
                let seconds = time.sec();
                let microseconds =
                    u64::from(time.sec_fraction()) * 1_000_000 / u64::from(u32::MAX);
                println!("Got time: {seconds}.{microseconds}");
            }
            Err(err) => println!("Err: {err:?}"),
        }

        thread::sleep(Duration::new(15, 0));
    }
}

You can find this example as well as other example projects in the example directory.

no_std support


Currently, there are basic no_std support available, thanks to no-std-net crate. There is an example available on how to use [smoltcp][smoltcp] stack and that should provide general idea on how to bootstrap no_std networking and timestamping tools for sntpc library usage

async support


Feature async_tokio allows to use crate together with tokio. There is an example: examples/tokio.rs.

There is also no_std support with feature async, but it requires Rust >= 1.75-nightly version. The example can be found in separate repository.

Contribution


Contributions are always welcome! If you have an idea, it's best to float it by me before working on it to ensure no effort is wasted. If there's already an open issue for it, knock yourself out. See the contributing section for additional details

Thanks

  1. Frank A. Stevenson: for implementing stricter adherence to RFC4330 verification scheme
  2. Timothy Mertz: for fixing possible overflow in offset calculation
  3. HannesH: for fixing a typo in the README.md
  4. Richard Penney: for adding two indicators of the NTP server's accuracy into the NtpResult structure
  5. Vitali Pikulik: for adding async support
  6. tsingwong: for fixing invalid link in the README.md
  7. Robert Bastian: for fixing the overflow issue in the calculate_offset

Really appreciate all your efforts! Please let me know if I forgot someone.

License


This project is licensed under The 3-Clause BSD License
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in time by you, as defined in the 3-Clause BSD license, shall be licensed as above, without any additional terms or conditions.