001: package org.apache.commons.net.ntp;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /***
020: * Common NtpUtils Helper class.
021: *
022: * @author Jason Mathews, MITRE Corp
023: *
024: * @version $Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $
025: */
026: public final class NtpUtils {
027:
028: /***
029: * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
030: *
031: * @param address the 32-bit address
032: * @return the raw IP address in a string format.
033: */
034: public static String getHostAddress(int address) {
035: return ((address >>> 24) & 0xFF) + "."
036: + ((address >>> 16) & 0xFF) + "."
037: + ((address >>> 8) & 0xFF) + "."
038: + ((address >>> 0) & 0xFF);
039: }
040:
041: /***
042: * Returns NTP packet reference identifier as IP address.
043: *
044: * @param packet NTP packet
045: * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format.
046: */
047: public static String getRefAddress(NtpV3Packet packet) {
048: int address = (packet == null) ? 0 : packet.getReferenceId();
049: return getHostAddress(address);
050: }
051:
052: /***
053: * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is
054: * invalid (non-ASCII character) then returns empty string "".
055: * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive
056: * List of Clock Drivers</A>.
057: *
058: * @param message
059: * @return reference clock string if primary NTP server
060: */
061: public static String getReferenceClock(NtpV3Packet message) {
062: if (message == null)
063: return "";
064: int refId = message.getReferenceId();
065: if (refId == 0)
066: return "";
067: StringBuffer buf = new StringBuffer(4);
068: // start at highest-order byte (0x4c434c00 -> LCL)
069: for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8) {
070: char c = (char) ((refId >>> shiftBits) & 0xff);
071: if (c == 0)
072: break; // 0-terminated ASCII string
073: if (!Character.isLetterOrDigit(c))
074: return "";
075: buf.append(c);
076: }
077: return buf.toString();
078: }
079:
080: /***
081: * Return human-readable name of message mode type (RFC 1305).
082: *
083: * @param mode
084: * @return mode name
085: */
086: public static String getModeName(int mode) {
087: switch (mode) {
088: case NtpV3Packet.MODE_RESERVED:
089: return "Reserved";
090: case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
091: return "Symmetric Active";
092: case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
093: return "Symmetric Passive";
094: case NtpV3Packet.MODE_CLIENT:
095: return "Client";
096: case NtpV3Packet.MODE_SERVER:
097: return "Server";
098: case NtpV3Packet.MODE_BROADCAST:
099: return "Broadcast";
100: case NtpV3Packet.MODE_CONTROL_MESSAGE:
101: return "Control";
102: case NtpV3Packet.MODE_PRIVATE:
103: return "Private";
104: default:
105: return "Unknown";
106: }
107: }
108:
109: }
|