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