View Javadoc
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  
18  package org.apache.commons.net.examples.ntp;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.SocketException;
23  import java.net.UnknownHostException;
24  import java.text.NumberFormat;
25  import java.time.Duration;
26  
27  import org.apache.commons.net.ntp.NTPUDPClient;
28  import org.apache.commons.net.ntp.NtpUtils;
29  import org.apache.commons.net.ntp.NtpV3Packet;
30  import org.apache.commons.net.ntp.TimeInfo;
31  import org.apache.commons.net.ntp.TimeStamp;
32  
33  /**
34   * 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
35   * (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.
36   * 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.
37   * <p>
38   * Usage: NTPClient <hostname-or-address-list>
39   * </p>
40   * <p>
41   * Example: NTPClient clock.psu.edu
42   * </p>
43   */
44  public final class NTPClient {
45  
46      private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
47  
48      public static void main(final String[] args) {
49          if (args.length == 0) {
50              System.err.println("Usage: NTPClient <hostname-or-address-list>");
51              System.exit(1);
52          }
53  
54          final NTPUDPClient client = new NTPUDPClient();
55          // We want to timeout if a response takes longer than 10 seconds
56          client.setDefaultTimeout(Duration.ofSeconds(10));
57          try {
58              client.open();
59              for (final String arg : args) {
60                  System.out.println();
61                  try {
62                      final InetAddress hostAddr = InetAddress.getByName(arg);
63                      System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
64                      final TimeInfo info = client.getTime(hostAddr);
65                      processResponse(info);
66                  } catch (final IOException ioe) {
67                      ioe.printStackTrace();
68                  }
69              }
70          } catch (final SocketException e) {
71              e.printStackTrace();
72          }
73  
74          client.close();
75      }
76  
77      /**
78       * Process <code>TimeInfo</code> object and print its details.
79       *
80       * @param info <code>TimeInfo</code> object.
81       */
82      public static void processResponse(final TimeInfo info) {
83          final NtpV3Packet message = info.getMessage();
84          final int stratum = message.getStratum();
85          final String refType;
86          if (stratum <= 0) {
87              refType = "(Unspecified or Unavailable)";
88          } else if (stratum == 1) {
89              refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
90          } else {
91              refType = "(Secondary Reference; e.g. via NTP or SNTP)";
92          }
93          // stratum should be 0..15...
94          System.out.println(" Stratum: " + stratum + " " + refType);
95          final int version = message.getVersion();
96          final int li = message.getLeapIndicator();
97          System.out.println(" leap=" + li + ", version=" + version + ", precision=" + message.getPrecision());
98  
99          System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
100         final int poll = message.getPoll();
101         // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
102         System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " seconds" + " (2 ** " + poll + ")");
103         final double disp = message.getRootDispersionInMillisDouble();
104         System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble()) + ", rootdispersion(ms): " + numberFormat.format(disp));
105 
106         final int refId = message.getReferenceId();
107         String refAddr = NtpUtils.getHostAddress(refId);
108         String refName = null;
109         if (refId != 0) {
110             if (refAddr.equals("127.127.1.0")) {
111                 refName = "LOCAL"; // This is the ref address for the Local Clock
112             } else if (stratum >= 2) {
113                 // If reference id has 127.127 prefix then it uses its own reference clock
114                 // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
115                 // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
116                 if (!refAddr.startsWith("127.127")) {
117                     try {
118                         final InetAddress addr = InetAddress.getByName(refAddr);
119                         final String name = addr.getHostName();
120                         if (name != null && !name.equals(refAddr)) {
121                             refName = name;
122                         }
123                     } catch (final UnknownHostException e) {
124                         // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
125                         // ref not valid host maybe it's a reference clock name?
126                         // otherwise just show the ref IP address.
127                         refName = NtpUtils.getReferenceClock(message);
128                     }
129                 }
130             } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
131                 refName = NtpUtils.getReferenceClock(message);
132                 // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
133             }
134             // otherwise give up on naming the beast...
135         }
136         if (refName != null && refName.length() > 1) {
137             refAddr += " (" + refName + ")";
138         }
139         System.out.println(" Reference Identifier:\t" + refAddr);
140 
141         final TimeStamp refNtpTime = message.getReferenceTimeStamp();
142         System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());
143 
144         // Originate Time is time request sent by client (t1)
145         final TimeStamp origNtpTime = message.getOriginateTimeStamp();
146         System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());
147 
148         final long destTimeMillis = info.getReturnTime();
149         // Receive Time is time request received by server (t2)
150         final TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
151         System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());
152 
153         // Transmit time is time reply sent by server (t3)
154         final TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
155         System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());
156 
157         // Destination time is time reply received by client (t4)
158         final TimeStamp destNtpTime = TimeStamp.getNtpTime(destTimeMillis);
159         System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());
160 
161         info.computeDetails(); // compute offset/delay if not already done
162         final Long offsetMillis = info.getOffset();
163         final Long delayMillis = info.getDelay();
164         final String delay = delayMillis == null ? "N/A" : delayMillis.toString();
165         final String offset = offsetMillis == null ? "N/A" : offsetMillis.toString();
166 
167         System.out.println(" Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset); // offset in ms
168     }
169 
170 }