Skip to main content

request_with_timeout

Function request_with_timeout 

Source
pub fn request_with_timeout<A: ToSocketAddrs>(
    addr: A,
    timeout: Duration,
) -> Result<NtpResult>
Expand description

Send a blocking request to an NTP server with a configurable timeout.

Constructs an NTPv4 client-mode packet, sends it to the specified server, and validates the response per RFC 5905. Returns the parsed response along with computed clock offset and round-trip delay.

§Arguments

  • addr - Any valid socket address (e.g., "time.nist.gov:123" or "192.168.1.1:123")
  • timeout - Maximum duration to wait for both sending and receiving the NTP packet

§Returns

Returns an NtpResult containing the server’s response packet and computed timing information, or an error if the server cannot be reached or the response is invalid.

§Examples

// Request time with a 10 second timeout
let result = ntp::request_with_timeout("time.nist.gov:123", Duration::from_secs(10))?;
println!("Offset: {:.6} seconds", result.offset_seconds);
println!("Delay: {:.6} seconds", result.delay_seconds);

§Errors

Returns io::Error if:

  • Cannot bind to local UDP socket
  • Network timeout (specified duration exceeded)
  • Invalid NTP packet response
  • DNS resolution fails
  • Response source address does not match the target server
  • Response origin timestamp does not match our request (anti-replay)
  • Server responds with unexpected mode or zero transmit timestamp
  • Server reports unsynchronized clock (LI=Unknown with non-zero stratum)
  • Server sent a Kiss-o’-Death packet (see KissOfDeathError)