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.ntp;
19  
20  /**
21   * Common NtpUtils Helper class.
22   */
23  public final class NtpUtils {
24  
25      /**
26       * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
27       *
28       * @param address the 32-bit address
29       * @return the raw IP address in a string format.
30       */
31      public static String getHostAddress(final int address) {
32          return (address >>> 24 & 0xFF) + "." + (address >>> 16 & 0xFF) + "." + (address >>> 8 & 0xFF) + "." + (address >>> 0 & 0xFF);
33      }
34  
35      /**
36       * Return human-readable name of message mode type (RFC 1305).
37       *
38       * @param mode the mode type
39       * @return mode name
40       */
41      public static String getModeName(final int mode) {
42          switch (mode) {
43          case NtpV3Packet.MODE_RESERVED:
44              return "Reserved";
45          case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
46              return "Symmetric Active";
47          case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
48              return "Symmetric Passive";
49          case NtpV3Packet.MODE_CLIENT:
50              return "Client";
51          case NtpV3Packet.MODE_SERVER:
52              return "Server";
53          case NtpV3Packet.MODE_BROADCAST:
54              return "Broadcast";
55          case NtpV3Packet.MODE_CONTROL_MESSAGE:
56              return "Control";
57          case NtpV3Packet.MODE_PRIVATE:
58              return "Private";
59          default:
60              return "Unknown";
61          }
62      }
63  
64      /**
65       * Returns NTP packet reference identifier as IP address.
66       *
67       * @param packet NTP packet
68       * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format.
69       */
70      public static String getRefAddress(final NtpV3Packet packet) {
71          final int address = packet == null ? 0 : packet.getReferenceId();
72          return getHostAddress(address);
73      }
74  
75      /**
76       * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is invalid (non-ASCII character) then returns empty string "". For details refer to
77       * the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive List of Clock Drivers</A>.
78       *
79       * @param message the message to check
80       * @return reference clock string if primary NTP server
81       */
82      public static String getReferenceClock(final NtpV3Packet message) {
83          if (message == null) {
84              return "";
85          }
86          final int refId = message.getReferenceId();
87          if (refId == 0) {
88              return "";
89          }
90          final StringBuilder buf = new StringBuilder(4);
91          // start at highest-order byte (0x4c434c00 -> LCL)
92          for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8) {
93              final char c = (char) (refId >>> shiftBits & 0xff);
94              if (c == 0) { // 0-terminated ASCII string
95                  break;
96              }
97              if (!Character.isLetterOrDigit(c)) {
98                  return "";
99              }
100             buf.append(c);
101         }
102         return buf.toString();
103     }
104 
105 }