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 * @author Jason Mathews, MITRE Corp
024 *
025 * @version $Revision: 1230358 $ $Date: 2012-01-12 01:51:02 +0000 (Thu, 12 Jan 2012) $
026 */
027public 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        }
068        int refId = message.getReferenceId();
069        if (refId == 0) {
070            return "";
071        }
072        StringBuilder buf = new StringBuilder(4);
073        // start at highest-order byte (0x4c434c00 -> LCL)
074        for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
075        {
076            char c = (char) ((refId >>> shiftBits) & 0xff);
077            if (c == 0) { // 0-terminated ASCII string
078                break;
079            }
080            if (!Character.isLetterOrDigit(c)) {
081                return "";
082            }
083            buf.append(c);
084        }
085        return buf.toString();
086    }
087
088    /***
089     * Return human-readable name of message mode type (RFC 1305).
090     *
091     * @param mode
092     * @return mode name
093     */
094    public static String getModeName(int mode)
095    {
096        switch (mode) {
097            case NtpV3Packet.MODE_RESERVED:
098                return "Reserved";
099            case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
100                return "Symmetric Active";
101            case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
102                return "Symmetric Passive";
103            case NtpV3Packet.MODE_CLIENT:
104                return "Client";
105            case NtpV3Packet.MODE_SERVER:
106                return "Server";
107            case NtpV3Packet.MODE_BROADCAST:
108                return "Broadcast";
109            case NtpV3Packet.MODE_CONTROL_MESSAGE:
110                return "Control";
111            case NtpV3Packet.MODE_PRIVATE:
112                return "Private";
113            default:
114                return "Unknown";
115        }
116    }
117
118}