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