JFLib

netpkt/ntp.rs#

Source: lib/netpkt/ntp.rs

ntp -- the Network Time Protocol packet (RFC 5905: the 48-byte header; extension fields and
MACs are not read)

  Ts(u64)                  32.32 fixed-point seconds since 1900 (era 0, until 2036);
                           from_unix_ns / to_unix_ns round to the nearest step, so whole
                           nanoseconds survive the round trip
  NtpView::parse(b)        li, vn, mode, stratum, poll, precision, root_delay, root_disp,
                           refid, reference, origin, receive, transmit
  Ntp { .. }               read / to_bytes / client(vn, transmit)
  offset_delay(t1, t2, t3, t4) -> (offset, delay)   the on-wire calculation (RFC 5905 8)
  civil(unix_secs) -> (year, month, day, hour, minute, second)   UTC, for dissectors
  MODE_* / LI_*; mode_name
NTP_LEN: usize = 48
/// seconds from 1900-01-01 to 1970-01-01
UNIX_OFFSET: u64 = 2_208_988_800
MODE_SYM_ACTIVE: u8 = 1
MODE_SYM_PASSIVE: u8 = 2
MODE_CLIENT: u8 = 3
MODE_SERVER: u8 = 4
MODE_BROADCAST: u8 = 5
LI_NONE: u8 = 0
/// the clock is not synchronised
LI_ALARM: u8 = 3
  • fn mode_name(m: u8) -> &'static str

  • struct Ts(pub u64) an NTP timestamp

impl Ts

  • fn from_unix_ns(ns: i64) -> Ts nanoseconds since 1970 (era 0)

  • fn to_unix_ns(self) -> i64

  • fn secs(self) -> u32

  • fn frac(self) -> u32

  • struct NtpView<'a> a received NTP packet

impl<'a> NtpView<'a>

  • fn parse(b: &'a [u8]) -> Option<NtpView<'a>>

  • fn li(&self) -> u8

  • fn vn(&self) -> u8

  • fn mode(&self) -> u8

  • fn stratum(&self) -> u8

  • fn poll(&self) -> i8

  • fn precision(&self) -> i8

  • fn root_delay(&self) -> u32 16.16 seconds

  • fn root_disp(&self) -> u32

  • fn refid(&self) -> [u8; 4]

  • fn reference(&self) -> Ts

  • fn origin(&self) -> Ts

  • fn receive(&self) -> Ts

  • fn transmit(&self) -> Ts

  • struct Ntp an NTP packet to write

    • li: u8
    • vn: u8
    • mode: u8
    • stratum: u8
    • poll: i8
    • precision: i8
    • root_delay: u32
    • root_disp: u32
    • refid: [u8; 4]
    • reference: Ts
    • origin: Ts
    • receive: Ts
    • transmit: Ts

impl Ntp

  • fn client(vn: u8, transmit: Ts) -> Ntp a client request: only the version, the mode and the transmit time (RFC 4330 style, as SNTP clients and chrony send)

  • fn read(v: &NtpView) -> Ntp

  • fn to_bytes(&self) -> Vec<u8>

  • fn offset_delay(t1: i64, t2: i64, t3: i64, t4: i64) -> (i64, i64) the clock offset and the round-trip delay from the four times of an exchange (client send t1, server receive t2, server send t3, client receive t4; all in ns)

  • fn civil(unix_secs: i64) -> (i32, u8, u8, u8, u8, u8) the UTC calendar date and time of a Unix time (days from civil, H. Hinnant)

Source: docs/API.md in the JFLib repository.