| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.net.ntp.NtpUtils |
|
|
| 1 | package org.apache.commons.net.ntp; |
|
| 2 | /* |
|
| 3 | * Copyright 2001-2005 The Apache Software Foundation |
|
| 4 | * |
|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 6 | * you may not use this file except in compliance with the License. |
|
| 7 | * 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 | ||
| 18 | /*** |
|
| 19 | * Common NtpUtils Helper class. |
|
| 20 | * |
|
| 21 | * @author Jason Mathews, MITRE Corp |
|
| 22 | * |
|
| 23 | * @version $Revision: 165675 $ $Date: 2005-05-02 21:09:55 +0100 (Mon, 02 May 2005) $ |
|
| 24 | */ |
|
| 25 | 0 | public final class NtpUtils { |
| 26 | ||
| 27 | /*** |
|
| 28 | * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format. |
|
| 29 | * |
|
| 30 | * @param address the 32-bit address |
|
| 31 | * @return the raw IP address in a string format. |
|
| 32 | */ |
|
| 33 | public static String getHostAddress(int address) |
|
| 34 | { |
|
| 35 | 0 | return ((address >>> 24) & 0xFF) + "." + |
| 36 | ((address >>> 16) & 0xFF) + "." + |
|
| 37 | ((address >>> 8) & 0xFF) + "." + |
|
| 38 | ((address >>> 0) & 0xFF); |
|
| 39 | } |
|
| 40 | ||
| 41 | /*** |
|
| 42 | * Returns NTP packet reference identifier as IP address. |
|
| 43 | * |
|
| 44 | * @param packet NTP packet |
|
| 45 | * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format. |
|
| 46 | */ |
|
| 47 | public static String getRefAddress(NtpV3Packet packet) |
|
| 48 | { |
|
| 49 | 0 | int address = (packet == null) ? 0 : packet.getReferenceId(); |
| 50 | 0 | return getHostAddress(address); |
| 51 | } |
|
| 52 | ||
| 53 | /*** |
|
| 54 | * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is |
|
| 55 | * invalid (non-ASCII character) then returns empty string "". |
|
| 56 | * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive |
|
| 57 | * List of Clock Drivers</A>. |
|
| 58 | * |
|
| 59 | * @param message |
|
| 60 | * @return reference clock string if primary NTP server |
|
| 61 | */ |
|
| 62 | public static String getReferenceClock(NtpV3Packet message) { |
|
| 63 | 0 | if (message == null) |
| 64 | 0 | return ""; |
| 65 | 0 | int refId = message.getReferenceId(); |
| 66 | 0 | if (refId == 0) |
| 67 | 0 | return ""; |
| 68 | 0 | StringBuffer buf = new StringBuffer(4); |
| 69 | // start at highest-order byte (0x4c434c00 -> LCL) |
|
| 70 | 0 | for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8) |
| 71 | { |
|
| 72 | 0 | char c = (class="keyword">char) ((refId >>> shiftBits) & 0xff); |
| 73 | 0 | if (c == 0) break; // 0-terminated ASCII string |
| 74 | 0 | if (!Character.isLetterOrDigit(c)) |
| 75 | 0 | return ""; |
| 76 | 0 | buf.append(c); |
| 77 | } |
|
| 78 | 0 | return buf.toString(); |
| 79 | } |
|
| 80 | ||
| 81 | /*** |
|
| 82 | * Return human-readable name of message mode type (RFC 1305). |
|
| 83 | * |
|
| 84 | * @param mode |
|
| 85 | * @return mode name |
|
| 86 | */ |
|
| 87 | public static String getModeName(int mode) |
|
| 88 | { |
|
| 89 | 0 | switch (mode) { |
| 90 | case NtpV3Packet.MODE_RESERVED: |
|
| 91 | 0 | return "Reserved"; |
| 92 | case NtpV3Packet.MODE_SYMMETRIC_ACTIVE: |
|
| 93 | 0 | return "Symmetric Active"; |
| 94 | case NtpV3Packet.MODE_SYMMETRIC_PASSIVE: |
|
| 95 | 0 | return "Symmetric Passive"; |
| 96 | case NtpV3Packet.MODE_CLIENT: |
|
| 97 | 0 | return "Client"; |
| 98 | case NtpV3Packet.MODE_SERVER: |
|
| 99 | 0 | return "Server"; |
| 100 | case NtpV3Packet.MODE_BROADCAST: |
|
| 101 | 0 | return "Broadcast"; |
| 102 | case NtpV3Packet.MODE_CONTROL_MESSAGE: |
|
| 103 | 0 | return "Control"; |
| 104 | case NtpV3Packet.MODE_PRIVATE: |
|
| 105 | 0 | return "Private"; |
| 106 | default: |
|
| 107 | 0 | return "Unknown"; |
| 108 | } |
|
| 109 | } |
|
| 110 | ||
| 111 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |