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: 1299238 $
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 }
68 int refId = message.getReferenceId();
69 if (refId == 0) {
70 return "";
71 }
72 StringBuilder buf = new StringBuilder(4);
73 // start at highest-order byte (0x4c434c00 -> LCL)
74 for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
75 {
76 char c = (char) ((refId >>> shiftBits) & 0xff);
77 if (c == 0) { // 0-terminated ASCII string
78 break;
79 }
80 if (!Character.isLetterOrDigit(c)) {
81 return "";
82 }
83 buf.append(c);
84 }
85 return buf.toString();
86 }
87
88 /***
89 * Return human-readable name of message mode type (RFC 1305).
90 *
91 * @param mode
92 * @return mode name
93 */
94 public static String getModeName(int mode)
95 {
96 switch (mode) {
97 case NtpV3Packet.MODE_RESERVED:
98 return "Reserved";
99 case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
100 return "Symmetric Active";
101 case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
102 return "Symmetric Passive";
103 case NtpV3Packet.MODE_CLIENT:
104 return "Client";
105 case NtpV3Packet.MODE_SERVER:
106 return "Server";
107 case NtpV3Packet.MODE_BROADCAST:
108 return "Broadcast";
109 case NtpV3Packet.MODE_CONTROL_MESSAGE:
110 return "Control";
111 case NtpV3Packet.MODE_PRIVATE:
112 return "Private";
113 default:
114 return "Unknown";
115 }
116 }
117
118 }