1 package org.apache.commons.net.ntp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * Common NtpUtils Helper class.
20 *
21 * @author Jason Mathews, MITRE Corp
22 *
23 * @version $Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $
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
60 * @return reference clock string if primary NTP server
61 */
62 public static String getReferenceClock(NtpV3Packet message) {
63 if (message == null)
64 return "";
65 int refId = message.getReferenceId();
66 if (refId == 0)
67 return "";
68 StringBuffer buf = new StringBuffer(4);
69
70 for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
71 {
72 char c = (char) ((refId >>> shiftBits) & 0xff);
73 if (c == 0) break;
74 if (!Character.isLetterOrDigit(c))
75 return "";
76 buf.append(c);
77 }
78 return buf.toString();
79 }
80
81 /***
82 * Return human-readable name of message mode type (RFC 1305).
83 *
84 * @param mode
85 * @return mode name
86 */
87 public static String getModeName(int mode)
88 {
89 switch (mode) {
90 case NtpV3Packet.MODE_RESERVED:
91 return "Reserved";
92 case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
93 return "Symmetric Active";
94 case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
95 return "Symmetric Passive";
96 case NtpV3Packet.MODE_CLIENT:
97 return "Client";
98 case NtpV3Packet.MODE_SERVER:
99 return "Server";
100 case NtpV3Packet.MODE_BROADCAST:
101 return "Broadcast";
102 case NtpV3Packet.MODE_CONTROL_MESSAGE:
103 return "Control";
104 case NtpV3Packet.MODE_PRIVATE:
105 return "Private";
106 default:
107 return "Unknown";
108 }
109 }
110
111 }