java.lang.Object
net.i2p.router.time.NtpClient

public class NtpClientextends Object
NtpClient - an NTP client for Java. This program connects to an NTP server and prints the response to the console.

The local clock offset calculation is implemented according to the SNTP algorithm specified in RFC 2030.

Note that on windows platforms, the current time-of-day timestamp is limited to an resolution of 10ms and adversely affects the accuracy of the results.

Public only for main(String[]), not a public API, not for external use.

2036-compliant as of 0.9.50, see NtpMessage.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
    How long a reply may take: 10 seconds.
    private static final boolean
    Whether DoH is used when the property is unset.
    private static final Map<String,String>
    Map of server IPs to reasons for receiving a "Kiss of Death" (KoD) message.
    private static final int
    Shortest a valid NTP packet can be, in bytes.
    private static final int
    The standard NTP port.
    private static final int
    Offset of the originate timestamp in a packet.
    private static final int
    Offset of the transmit timestamp in a packet.
    private static final String
    Property selecting DNS-over-HTTPS for server lookups.
    (package private) static final double
    Difference between the Unix epoch and Jan 1 1900, the epoch used by NTP.
  • Constructor Summary

    Constructors
    Constructor
    Description
    The offsets, the KoD map and every query method are static and shared, so an instance carries no state of its own.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static long[]
    currentTimeAndStratum(String[] serverNames, int perServerTimeout, boolean preferIPv6, Log log)
    Query the ntp servers, returning the current time from first one we find Hack to return time and stratum.
    private static long[]
    currentTimeAndStratum(String serverName, int timeout, boolean preferIPv6, Log log)
    Query the given NTP server, returning the current internet time and stratum.
    static void
    main(String[] args)
    Main entry point for the NtpClient program.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SECONDS_1900_TO_EPOCH

      static final double SECONDS_1900_TO_EPOCH
      Difference between the Unix epoch and Jan 1 1900, the epoch used by NTP.
      See Also:
    • NTP_PORT

      private static final int NTP_PORT
      The standard NTP port.
      See Also:
    • DEFAULT_TIMEOUT

      private static final int DEFAULT_TIMEOUT
      How long a reply may take: 10 seconds.
      See Also:
    • OFF_ORIGTIME

      private static final int OFF_ORIGTIME
      Offset of the originate timestamp in a packet.
      See Also:
    • OFF_TXTIME

      private static final int OFF_TXTIME
      Offset of the transmit timestamp in a packet.
      See Also:
    • MIN_PKT_LEN

      private static final int MIN_PKT_LEN
      Shortest a valid NTP packet can be, in bytes.
      See Also:
    • kisses

      private static final Map<String,String> kisses
      Map of server IPs to reasons for receiving a "Kiss of Death" (KoD) message. KoD is a special NTP stratum 0 response indicating the server requests the client to stop querying temporarily.
    • PROP_USE_DNS_OVER_HTTPS

      private static final String PROP_USE_DNS_OVER_HTTPS
      Property selecting DNS-over-HTTPS for server lookups.
      See Also:
    • DEFAULT_USE_DNS_OVER_HTTPS

      private static final boolean DEFAULT_USE_DNS_OVER_HTTPS
      Whether DoH is used when the property is unset.
      See Also:
  • Constructor Details

    • NtpClient

      public NtpClient()
      The offsets, the KoD map and every query method are static and shared, so an instance carries no state of its own.
  • Method Details

    • currentTimeAndStratum

      static long[] currentTimeAndStratum(String[] serverNames, int perServerTimeout, boolean preferIPv6, Log log)
      Query the ntp servers, returning the current time from first one we find Hack to return time and stratum.
      Parameters:
      serverNames - the NTP servers to query, tried in random order
      perServerTimeout - how long to wait for each server's reply, in milliseconds
      preferIPv6 - if true, query the server's IPv6 address first
      log - may be null
      Returns:
      time in rv[0] and stratum in rv[1]
      Throws:
      IllegalArgumentException - if none of the servers are reachable
    • currentTimeAndStratum

      private static long[] currentTimeAndStratum(String serverName, int timeout, boolean preferIPv6, Log log)
      Query the given NTP server, returning the current internet time and stratum.
      Parameters:
      serverName - the NTP server host name or address to query
      timeout - the socket timeout in ms
      preferIPv6 - true to prefer an IPv6 address for the server
      log - may be null
      Returns:
      time in rv[0] and stratum in rv[1], or null for error
    • main

      public static void main(String[] args)
      Main entry point for the NtpClient program.

      Usage: java -jar i2p/lib/router.jar NtpClient [-6] [servers...]

      The optional "-6" flag forces IPv6 DNS lookups. If no servers are specified, the default "pool.ntp.org" is used.

      This program queries the specified NTP servers and prints the current time along with stratum and offset information.

      Parameters:
      args - command-line arguments; optionally "-6" followed by list of NTP servers