NtpUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.ntp;

  18. /**
  19.  * Common NtpUtils Helper class.
  20.  */
  21. public final class NtpUtils {

  22.     /**
  23.      * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
  24.      *
  25.      * @param address the 32-bit address
  26.      * @return the raw IP address in a string format.
  27.      */
  28.     public static String getHostAddress(final int address) {
  29.         return (address >>> 24 & 0xFF) + "." + (address >>> 16 & 0xFF) + "." + (address >>> 8 & 0xFF) + "." + (address >>> 0 & 0xFF);
  30.     }

  31.     /**
  32.      * Return human-readable name of message mode type (RFC 1305).
  33.      *
  34.      * @param mode the mode type
  35.      * @return mode name
  36.      */
  37.     public static String getModeName(final int mode) {
  38.         switch (mode) {
  39.         case NtpV3Packet.MODE_RESERVED:
  40.             return "Reserved";
  41.         case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
  42.             return "Symmetric Active";
  43.         case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
  44.             return "Symmetric Passive";
  45.         case NtpV3Packet.MODE_CLIENT:
  46.             return "Client";
  47.         case NtpV3Packet.MODE_SERVER:
  48.             return "Server";
  49.         case NtpV3Packet.MODE_BROADCAST:
  50.             return "Broadcast";
  51.         case NtpV3Packet.MODE_CONTROL_MESSAGE:
  52.             return "Control";
  53.         case NtpV3Packet.MODE_PRIVATE:
  54.             return "Private";
  55.         default:
  56.             return "Unknown";
  57.         }
  58.     }

  59.     /**
  60.      * Returns NTP packet reference identifier as IP address.
  61.      *
  62.      * @param packet NTP packet
  63.      * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format.
  64.      */
  65.     public static String getRefAddress(final NtpV3Packet packet) {
  66.         final int address = packet == null ? 0 : packet.getReferenceId();
  67.         return getHostAddress(address);
  68.     }

  69.     /**
  70.      * Gets refId as reference clock string (e.g. GPS, WWV, LCL). If string is invalid (non-ASCII character) then returns empty string "". For details refer to
  71.      * the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive List of Clock Drivers</A>.
  72.      *
  73.      * @param message the message to check
  74.      * @return reference clock string if primary NTP server
  75.      */
  76.     public static String getReferenceClock(final NtpV3Packet message) {
  77.         if (message == null) {
  78.             return "";
  79.         }
  80.         final int refId = message.getReferenceId();
  81.         if (refId == 0) {
  82.             return "";
  83.         }
  84.         final StringBuilder buf = new StringBuilder(4);
  85.         // start at highest-order byte (0x4c434c00 -> LCL)
  86.         for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8) {
  87.             final char c = (char) (refId >>> shiftBits & 0xff);
  88.             if (c == 0) { // 0-terminated ASCII string
  89.                 break;
  90.             }
  91.             if (!Character.isLetterOrDigit(c)) {
  92.                 return "";
  93.             }
  94.             buf.append(c);
  95.         }
  96.         return buf.toString();
  97.     }

  98. }