NTPClient.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.ntp;

  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.net.SocketException;
  21. import java.net.UnknownHostException;
  22. import java.text.NumberFormat;
  23. import java.time.Duration;

  24. import org.apache.commons.net.ntp.NTPUDPClient;
  25. import org.apache.commons.net.ntp.NtpUtils;
  26. import org.apache.commons.net.ntp.NtpV3Packet;
  27. import org.apache.commons.net.ntp.TimeInfo;
  28. import org.apache.commons.net.ntp.TimeStamp;

  29. /**
  30.  * This is an example program demonstrating how to use the NTPUDPClient class. This program sends a Datagram client request packet to a Network time Protocol
  31.  * (NTP) service port on a specified server, retrieves the time, and prints it to standard output along with the fields from the NTP message header (e.g.
  32.  * stratum level, reference id, poll interval, root delay, mode, ...) See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A> for details.
  33.  * <p>
  34.  * Usage: NTPClient <hostname-or-address-list>
  35.  * </p>
  36.  * <p>
  37.  * Example: NTPClient clock.psu.edu
  38.  * </p>
  39.  */
  40. public final class NTPClient {

  41.     private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");

  42.     public static void main(final String[] args) {
  43.         if (args.length == 0) {
  44.             System.err.println("Usage: NTPClient <hostname-or-address-list>");
  45.             System.exit(1);
  46.         }

  47.         final NTPUDPClient client = new NTPUDPClient();
  48.         // We want to timeout if a response takes longer than 10 seconds
  49.         client.setDefaultTimeout(Duration.ofSeconds(10));
  50.         try {
  51.             client.open();
  52.             for (final String arg : args) {
  53.                 System.out.println();
  54.                 try {
  55.                     final InetAddress hostAddr = InetAddress.getByName(arg);
  56.                     System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
  57.                     final TimeInfo info = client.getTime(hostAddr);
  58.                     processResponse(info);
  59.                 } catch (final IOException ioe) {
  60.                     ioe.printStackTrace();
  61.                 }
  62.             }
  63.         } catch (final SocketException e) {
  64.             e.printStackTrace();
  65.         }

  66.         client.close();
  67.     }

  68.     /**
  69.      * Process <code>TimeInfo</code> object and print its details.
  70.      *
  71.      * @param info <code>TimeInfo</code> object.
  72.      */
  73.     public static void processResponse(final TimeInfo info) {
  74.         final NtpV3Packet message = info.getMessage();
  75.         final int stratum = message.getStratum();
  76.         final String refType;
  77.         if (stratum <= 0) {
  78.             refType = "(Unspecified or Unavailable)";
  79.         } else if (stratum == 1) {
  80.             refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
  81.         } else {
  82.             refType = "(Secondary Reference; e.g. via NTP or SNTP)";
  83.         }
  84.         // stratum should be 0..15...
  85.         System.out.println(" Stratum: " + stratum + " " + refType);
  86.         final int version = message.getVersion();
  87.         final int li = message.getLeapIndicator();
  88.         System.out.println(" leap=" + li + ", version=" + version + ", precision=" + message.getPrecision());

  89.         System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
  90.         final int poll = message.getPoll();
  91.         // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
  92.         System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " seconds" + " (2 ** " + poll + ")");
  93.         final double disp = message.getRootDispersionInMillisDouble();
  94.         System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble()) + ", rootdispersion(ms): " + numberFormat.format(disp));

  95.         final int refId = message.getReferenceId();
  96.         String refAddr = NtpUtils.getHostAddress(refId);
  97.         String refName = null;
  98.         if (refId != 0) {
  99.             if (refAddr.equals("127.127.1.0")) {
  100.                 refName = "LOCAL"; // This is the ref address for the Local Clock
  101.             } else if (stratum >= 2) {
  102.                 // If reference id has 127.127 prefix then it uses its own reference clock
  103.                 // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
  104.                 // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
  105.                 if (!refAddr.startsWith("127.127")) {
  106.                     try {
  107.                         final InetAddress addr = InetAddress.getByName(refAddr);
  108.                         final String name = addr.getHostName();
  109.                         if (name != null && !name.equals(refAddr)) {
  110.                             refName = name;
  111.                         }
  112.                     } catch (final UnknownHostException e) {
  113.                         // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
  114.                         // ref not valid host maybe it's a reference clock name?
  115.                         // otherwise just show the ref IP address.
  116.                         refName = NtpUtils.getReferenceClock(message);
  117.                     }
  118.                 }
  119.             } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
  120.                 refName = NtpUtils.getReferenceClock(message);
  121.                 // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
  122.             }
  123.             // otherwise give up on naming the beast...
  124.         }
  125.         if (refName != null && refName.length() > 1) {
  126.             refAddr += " (" + refName + ")";
  127.         }
  128.         System.out.println(" Reference Identifier:\t" + refAddr);

  129.         final TimeStamp refNtpTime = message.getReferenceTimeStamp();
  130.         System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());

  131.         // Originate Time is time request sent by client (t1)
  132.         final TimeStamp origNtpTime = message.getOriginateTimeStamp();
  133.         System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());

  134.         final long destTimeMillis = info.getReturnTime();
  135.         // Receive Time is time request received by server (t2)
  136.         final TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
  137.         System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());

  138.         // Transmit time is time reply sent by server (t3)
  139.         final TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
  140.         System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());

  141.         // Destination time is time reply received by client (t4)
  142.         final TimeStamp destNtpTime = TimeStamp.getNtpTime(destTimeMillis);
  143.         System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());

  144.         info.computeDetails(); // compute offset/delay if not already done
  145.         final Long offsetMillis = info.getOffset();
  146.         final Long delayMillis = info.getDelay();
  147.         final String delay = delayMillis == null ? "N/A" : delayMillis.toString();
  148.         final String offset = offsetMillis == null ? "N/A" : offsetMillis.toString();

  149.         System.out.println(" Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset); // offset in ms
  150.     }

  151. }