//! SNTP (RFC 4330, NTP version 4 as a client): the time of day asked of a //! server over UDP, and the answer turned into milliseconds since 1970. //! The caller sends [`request`]'s packet, notes how long the answer took, //! and gives both to [`answer`]. /// The port NTP servers listen on. pub const PORT: u16 = 123; /// A packet's size without extensions. pub const SIZE: usize = 48; /// Seconds from 1900, where NTP counts from, to 1970. const FROM_1900: u64 = 2_208_988_800; /// A client's request: version 4, mode 3 (client), and `cookie` as its /// transmit time, which a server sends back as the originate time. A /// random cookie, rather than the clock it is meant to set, is what RFC /// 4330 suggests (section 6), and lets [`answer`] tell an answer to this /// request from one sent by someone else. pub fn request(cookie: u64) -> [u8; SIZE] { let mut packet = [0u8; SIZE]; packet[0] = 4 << 3 | 3; packet[40..48].copy_from_slice(&cookie.to_be_bytes()); packet } /// Why an answer is not used. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Refused { /// Shorter than a packet, or not a server's (mode 4). NotAnAnswer, /// Not to the request with this cookie. NotOurs, /// The server says it has no time to give: stratum 0 (a "kiss of /// death", which also asks to be asked less), or its clock is not /// synchronised (leap indicator 3). Unsynchronised, } /// The time an answer gives, milliseconds since 1970, as it was when the /// answer arrived: the server's transmit time, plus half of what the /// round trip took apart from the time the server held the request. /// `elapsed_ms` is how long it was from sending the request to receiving /// the answer, by a clock of the caller's own. pub fn answer(packet: &[u8], cookie: u64, elapsed_ms: u64) -> Result { if packet.len() < SIZE || packet[0] & 7 != 4 { return Err(Refused::NotAnAnswer); } if timestamp(&packet[24..32]) != cookie { return Err(Refused::NotOurs); } if packet[1] == 0 || packet[0] >> 6 == 3 { return Err(Refused::Unsynchronised); } let received = millis(timestamp(&packet[32..40])); let transmitted = millis(timestamp(&packet[40..48])); let held = transmitted.saturating_sub(received); Ok(transmitted + elapsed_ms.saturating_sub(held) / 2) } fn timestamp(bytes: &[u8]) -> u64 { u64::from_be_bytes(bytes.try_into().unwrap()) } /// An NTP timestamp (seconds since 1900 above, a fraction below) as /// milliseconds since 1970. The seconds wrap in 2036: a time that reads /// before 1970 is in the era after that. fn millis(timestamp: u64) -> u64 { let seconds = timestamp >> 32; let fraction = timestamp & 0xffff_ffff; let seconds = if seconds < FROM_1900 { seconds + (1 << 32) - FROM_1900 } else { seconds - FROM_1900 }; seconds * 1000 + ((fraction * 1000) >> 32) } #[cfg(test)] mod tests { use super::*; /// A server's answer to `cookie`, received at `received` and sent at /// `transmitted` (NTP seconds, and milliseconds of them). fn server(cookie: u64, received: (u64, u64), transmitted: (u64, u64)) -> [u8; SIZE] { let stamp = |(s, ms): (u64, u64)| (s << 32 | ((ms << 32) + 999) / 1000).to_be_bytes(); let mut p = [0u8; SIZE]; p[0] = 4 << 3 | 4; p[1] = 2; p[24..32].copy_from_slice(&cookie.to_be_bytes()); p[32..40].copy_from_slice(&stamp(received)); p[40..48].copy_from_slice(&stamp(transmitted)); p } #[test] fn a_request() { let p = request(0x0102_0304_0506_0708); assert_eq!(p[0], 0x23, "version 4, client"); assert_eq!(&p[40..], &[1, 2, 3, 4, 5, 6, 7, 8]); assert!(p[1..40].iter().all(|&b| b == 0)); } #[test] fn an_answer_corrected_for_the_trip() { // 29 November 2026 12:00:00 UTC is 1_795_953_600 since 1970. let s = 1_795_953_600 + FROM_1900; // Held 10 ms at the server, 110 ms there and back: 50 ms each way. let p = server(7, (s, 500), (s, 510)); assert_eq!(answer(&p, 7, 110), Ok(1_795_953_600_000 + 510 + 50)); } #[test] fn answers_not_used() { let s = 1_795_953_600 + FROM_1900; let p = server(7, (s, 0), (s, 0)); assert_eq!(answer(&p, 8, 10), Err(Refused::NotOurs)); assert_eq!(answer(&p[..40], 7, 10), Err(Refused::NotAnAnswer)); let mut kiss = p; kiss[1] = 0; assert_eq!(answer(&kiss, 7, 10), Err(Refused::Unsynchronised)); let mut alarm = p; alarm[0] |= 3 << 6; assert_eq!(answer(&alarm, 7, 10), Err(Refused::Unsynchronised)); let mut client = p; client[0] = 4 << 3 | 3; assert_eq!(answer(&client, 7, 10), Err(Refused::NotAnAnswer)); } #[test] fn after_2036() { // 7 February 2036 06:28:16 UTC wraps the seconds to 0. assert_eq!(millis(0), ((1u64 << 32) - FROM_1900) * 1000); assert_eq!(millis(5 << 32), ((1u64 << 32) - FROM_1900 + 5) * 1000); } }