    001    package org.apache.commons.net.ntp;
    002    /*
    003     * Licensed to the Apache Software Foundation (ASF) under one or more
    004     * contributor license agreements.  See the NOTICE file distributed with
    005     * this work for additional information regarding copyright ownership.
    006     * The ASF licenses this file to You under the Apache License, Version 2.0
    007     * (the "License"); you may not use this file except in compliance with
    008     * the License.  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    /***
    021     * Common NtpUtils Helper class.
    022     *
    023     * @author Jason Mathews, MITRE Corp
    024     *
    025     * @version $Revision: 929344 $ $Date: 2010-03-31 01:20:49 +0100 (Wed, 31 Mar 2010) $
    026     */
    027    public final class NtpUtils {
    028
    029        /***
    030          * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
    031          *
    032          * @param address  the 32-bit address
    033          * @return  the raw IP address in a string format.
    034          */
    035         public static String getHostAddress(int address)
    036         {
    037              return ((address >>> 24) & 0xFF) + "." +
    038                     ((address >>> 16) & 0xFF) + "." +
    039                     ((address >>>  8) & 0xFF) + "." +
    040                     ((address >>>  0) & 0xFF);
    041         }
    042
    043        /***
    044         * Returns NTP packet reference identifier as IP address.
    045         *
    046         * @param packet  NTP packet
    047         * @return  the packet reference id (as IP address) in "%d.%d.%d.%d" format.
    048         */
    049         public static String getRefAddress(NtpV3Packet packet)
    050         {
    051             int address = (packet == null) ? 0 : packet.getReferenceId();
    052             return getHostAddress(address);
    053         }
    054
    055        /***
    056         * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is
    057         * invalid (non-ASCII character) then returns empty string "".
    058         * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive
    059         * List of Clock Drivers</A>.
    060         *
    061         * @param message
    062         * @return reference clock string if primary NTP server
    063         */
    064        public static String getReferenceClock(NtpV3Packet message) {
    065            if (message == null)
    066                return "";
    067            int refId = message.getReferenceId();
    068            if (refId == 0)
    069                return "";
    070            StringBuilder buf = new StringBuilder(4);
    071            // start at highest-order byte (0x4c434c00 -> LCL)
    072            for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
    073            {
    074                char c = (char) ((refId >>> shiftBits) & 0xff);
    075                if (c == 0) break; // 0-terminated ASCII string
    076                if (!Character.isLetterOrDigit(c))
    077                    return "";
    078                buf.append(c);
    079            }
    080            return buf.toString();
    081        }
    082
    083        /***
    084         * Return human-readable name of message mode type (RFC 1305).
    085         *
    086         * @param mode
    087         * @return mode name
    088         */
    089        public static String getModeName(int mode)
    090        {
    091            switch (mode) {
    092                case NtpV3Packet.MODE_RESERVED:
    093                    return "Reserved";
    094                case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
    095                    return "Symmetric Active";
    096                case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
    097                    return "Symmetric Passive";
    098                case NtpV3Packet.MODE_CLIENT:
    099                    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    }