java.lang.Object
net.i2p.router.time.NtpClient
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
FieldsModifier and TypeFieldDescriptionprivate static final intHow long a reply may take: 10 seconds.private static final booleanWhether DoH is used when the property is unset.Map of server IPs to reasons for receiving a "Kiss of Death" (KoD) message.private static final intShortest a valid NTP packet can be, in bytes.private static final intThe standard NTP port.private static final intOffset of the originate timestamp in a packet.private static final intOffset of the transmit timestamp in a packet.private static final StringProperty selecting DNS-over-HTTPS for server lookups.(package private) static final doubleDifference between the Unix epoch and Jan 1 1900, the epoch used by NTP.Constructor Summary
ConstructorsConstructorDescriptionThe 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 TypeMethodDescription(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 voidMain entry point for the NtpClient program.
Field Details
SECONDS_1900_TO_EPOCH
static final double SECONDS_1900_TO_EPOCHDifference between the Unix epoch and Jan 1 1900, the epoch used by NTP.- See Also:
NTP_PORT
DEFAULT_TIMEOUT
private static final int DEFAULT_TIMEOUTHow long a reply may take: 10 seconds.- See Also:
OFF_ORIGTIME
private static final int OFF_ORIGTIMEOffset of the originate timestamp in a packet.- See Also:
OFF_TXTIME
private static final int OFF_TXTIMEOffset of the transmit timestamp in a packet.- See Also:
MIN_PKT_LEN
private static final int MIN_PKT_LENShortest a valid NTP packet can be, in bytes.- See Also:
kisses
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_HTTPSWhether 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 orderperServerTimeout- how long to wait for each server's reply, in millisecondspreferIPv6- if true, query the server's IPv6 address firstlog- 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 querytimeout- the socket timeout in mspreferIPv6- true to prefer an IPv6 address for the serverlog- may be null- Returns:
- time in rv[0] and stratum in rv[1], or null for error
main
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