View Javadoc
1   package org.apache.commons.net.ntp;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  
20  /***
21   * Common NtpUtils Helper class.
22   *
23   * @version $Revision: 1652855 $
24   */
25  public final class NtpUtils {
26  
27      /***
28        * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
29        *
30        * @param address  the 32-bit address
31        * @return  the raw IP address in a string format.
32        */
33       public static String getHostAddress(int address)
34       {
35            return ((address >>> 24) & 0xFF) + "." +
36                   ((address >>> 16) & 0xFF) + "." +
37                   ((address >>>  8) & 0xFF) + "." +
38                   ((address >>>  0) & 0xFF);
39       }
40  
41      /***
42       * Returns NTP packet reference identifier as IP address.
43       *
44       * @param packet  NTP packet
45       * @return  the packet reference id (as IP address) in "%d.%d.%d.%d" format.
46       */
47       public static String getRefAddress(NtpV3Packet packet)
48       {
49           int address = (packet == null) ? 0 : packet.getReferenceId();
50           return getHostAddress(address);
51       }
52  
53      /***
54       * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is
55       * invalid (non-ASCII character) then returns empty string "".
56       * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive
57       * List of Clock Drivers</A>.
58       *
59       * @param message the message to check
60       * @return reference clock string if primary NTP server
61       */
62      public static String getReferenceClock(NtpV3Packet message) {
63          if (message == null) {
64              return "";
65          }
66          int refId = message.getReferenceId();
67          if (refId == 0) {
68              return "";
69          }
70          StringBuilder buf = new StringBuilder(4);
71          // start at highest-order byte (0x4c434c00 -> LCL)
72          for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
73          {
74              char c = (char) ((refId >>> shiftBits) & 0xff);
75              if (c == 0) { // 0-terminated ASCII string
76                  break;
77              }
78              if (!Character.isLetterOrDigit(c)) {
79                  return "";
80              }
81              buf.append(c);
82          }
83          return buf.toString();
84      }
85  
86      /***
87       * Return human-readable name of message mode type (RFC 1305).
88       *
89       * @param mode the mode type
90       * @return mode name
91       */
92      public static String getModeName(int mode)
93      {
94          switch (mode) {
95              case NtpV3Packet.MODE_RESERVED:
96                  return "Reserved";
97              case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
98                  return "Symmetric Active";
99              case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
100                 return "Symmetric Passive";
101             case NtpV3Packet.MODE_CLIENT:
102                 return "Client";
103             case NtpV3Packet.MODE_SERVER:
104                 return "Server";
105             case NtpV3Packet.MODE_BROADCAST:
106                 return "Broadcast";
107             case NtpV3Packet.MODE_CONTROL_MESSAGE:
108                 return "Control";
109             case NtpV3Packet.MODE_PRIVATE:
110                 return "Private";
111             default:
112                 return "Unknown";
113         }
114     }
115 
116 }