Skip to main content

ntp_proto/protocol/
mod.rs

1//! Types and constants that precisely match the specification.
2//!
3//! Provides `ReadBytes` and `WriteBytes` implementations which extend the byteorder crate
4//! `WriteBytesExt` and `ReadBytesExt` traits with the ability to read and write types from the NTP
5//! protocol respectively.
6//!
7//! Documentation is largely derived (and often copied directly) from IETF RFC 5905.
8
9/// NTP port number.
10pub const PORT: u8 = 123;
11
12/// Frequency tolerance PHI (s/s).
13pub const TOLERANCE: f64 = 15e-6;
14
15/// Minimum poll exponent (16 s).
16pub const MINPOLL: u8 = 4;
17
18/// Maximum poll exponent (36 h).
19pub const MAXPOLL: u8 = 17;
20
21/// Maximum dispersion (16 s).
22pub const MAXDISP: f64 = 16.0;
23
24/// Minimum dispersion increment (s).
25pub const MINDISP: f64 = 0.005;
26
27/// Distance threshold (1 s).
28pub const MAXDIST: u8 = 1;
29
30/// Maximum stratum number.
31pub const MAXSTRAT: u8 = 16;
32
33// Convert an ascii string to a big-endian u32.
34macro_rules! code_to_u32 {
35    ($w:expr) => {
36        (($w[3] as u32) << 0)
37            | (($w[2] as u32) << 8)
38            | (($w[1] as u32) << 16)
39            | (($w[0] as u32) << 24)
40            | ((*$w as [u8; 4])[0] as u32 * 0)
41    };
42}
43
44pub(crate) fn be_u32_to_bytes(u: u32) -> [u8; 4] {
45    [
46        (u >> 24 & 0xff) as u8,
47        (u >> 16 & 0xff) as u8,
48        (u >> 8 & 0xff) as u8,
49        (u & 0xff) as u8,
50    ]
51}
52
53#[cfg(feature = "ntpv5")]
54pub mod bloom;
55mod bytes;
56#[cfg(feature = "std")]
57mod io;
58#[cfg(feature = "std")]
59pub(crate) mod md5;
60#[cfg(feature = "ntpv5")]
61pub mod ntpv5;
62mod traits;
63mod types;
64
65pub use self::traits::*;
66pub use self::types::*;