001package 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 * @version $Revision: 1652855 $
024 */
025public 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 the message to check
060     * @return reference clock string if primary NTP server
061     */
062    public static String getReferenceClock(NtpV3Packet message) {
063        if (message == null) {
064            return "";
065        }
066        int refId = message.getReferenceId();
067        if (refId == 0) {
068            return "";
069        }
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) { // 0-terminated ASCII string
076                break;
077            }
078            if (!Character.isLetterOrDigit(c)) {
079                return "";
080            }
081            buf.append(c);
082        }
083        return buf.toString();
084    }
085
086    /***
087     * Return human-readable name of message mode type (RFC 1305).
088     *
089     * @param mode the mode type
090     * @return mode name
091     */
092    public static String getModeName(int mode)
093    {
094        switch (mode) {
095            case NtpV3Packet.MODE_RESERVED:
096                return "Reserved";
097            case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
098                return "Symmetric Active";
099            case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
100                return "Symmetric Passive";
101            case NtpV3Packet.MODE_CLIENT:
102                return "Client";
103            case NtpV3Packet.MODE_SERVER:
104                return "Server";
105            case NtpV3Packet.MODE_BROADCAST:
106                return "Broadcast";
107            case NtpV3Packet.MODE_CONTROL_MESSAGE:
108                return "Control";
109            case NtpV3Packet.MODE_PRIVATE:
110                return "Private";
111            default:
112                return "Unknown";
113        }
114    }
115
116}