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 */ 024public final class NtpUtils { 025 026 /** 027 * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format. 028 * 029 * @param address the 32-bit address 030 * @return the raw IP address in a string format. 031 */ 032 public static String getHostAddress(final int address) 033 { 034 return ((address >>> 24) & 0xFF) + "." + 035 ((address >>> 16) & 0xFF) + "." + 036 ((address >>> 8) & 0xFF) + "." + 037 ((address >>> 0) & 0xFF); 038 } 039 040 /** 041 * Returns NTP packet reference identifier as IP address. 042 * 043 * @param packet NTP packet 044 * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format. 045 */ 046 public static String getRefAddress(final NtpV3Packet packet) 047 { 048 final int address = (packet == null) ? 0 : packet.getReferenceId(); 049 return getHostAddress(address); 050 } 051 052 /** 053 * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is 054 * invalid (non-ASCII character) then returns empty string "". 055 * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive 056 * List of Clock Drivers</A>. 057 * 058 * @param message the message to check 059 * @return reference clock string if primary NTP server 060 */ 061 public static String getReferenceClock(final NtpV3Packet message) { 062 if (message == null) { 063 return ""; 064 } 065 final int refId = message.getReferenceId(); 066 if (refId == 0) { 067 return ""; 068 } 069 final StringBuilder buf = new StringBuilder(4); 070 // start at highest-order byte (0x4c434c00 -> LCL) 071 for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8) 072 { 073 final char c = (char) ((refId >>> shiftBits) & 0xff); 074 if (c == 0) { // 0-terminated ASCII string 075 break; 076 } 077 if (!Character.isLetterOrDigit(c)) { 078 return ""; 079 } 080 buf.append(c); 081 } 082 return buf.toString(); 083 } 084 085 /** 086 * Return human-readable name of message mode type (RFC 1305). 087 * 088 * @param mode the mode type 089 * @return mode name 090 */ 091 public static String getModeName(final int mode) 092 { 093 switch (mode) { 094 case NtpV3Packet.MODE_RESERVED: 095 return "Reserved"; 096 case NtpV3Packet.MODE_SYMMETRIC_ACTIVE: 097 return "Symmetric Active"; 098 case NtpV3Packet.MODE_SYMMETRIC_PASSIVE: 099 return "Symmetric Passive"; 100 case NtpV3Packet.MODE_CLIENT: 101 return "Client"; 102 case NtpV3Packet.MODE_SERVER: 103 return "Server"; 104 case NtpV3Packet.MODE_BROADCAST: 105 return "Broadcast"; 106 case NtpV3Packet.MODE_CONTROL_MESSAGE: 107 return "Control"; 108 case NtpV3Packet.MODE_PRIVATE: 109 return "Private"; 110 default: 111 return "Unknown"; 112 } 113 } 114 115}