1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.tracing.service.ntp; 26 27 import java.net.*; 28 29 33 public class NtpHeader { 34 37 public static final byte[] defaultHeaderData = { (byte)0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 38 42 public static final NtpHeader defaultHeader = new NtpHeader(defaultHeaderData); 43 private byte[] data; 44 47 private static final byte RI_IP_ADDRESS = 0; 48 51 private static final byte RI_CODE = 1; 52 55 private static final byte RI_OTHER = 2; 56 private int mp(byte b) { 57 int bb = b; 58 return (bb < 0) ? 256 + bb : bb; 59 } 60 61 64 public NtpHeader(byte[] data) { 65 this.data = data; 66 } 67 68 71 public byte[] getData() { 72 return data; 73 } 74 75 public int getLeapYearIndicator() { 76 return (int)((data[0] & 0xc0) >>> 6); 77 } 78 79 public int getVersionNumber() { 80 return (int)((data[0] & 0x38) >>> 3); 81 } 82 83 public int getMode() { 84 return (int)(data[0] & 0x07); 85 } 86 87 public int getStratum() { 88 return (int)data[1]; 89 } 90 91 public int getPollInterval() { 92 return (int)Math.round(Math.pow(2, data[2])); 93 } 94 95 98 public double getPrecision() { 99 return 1000 * Math.pow(2, data[3]); 100 } 101 102 105 public double getRootDelay() { 106 int temp = 0; 107 temp = 256 * (256 * (256 * data[4] + data[5]) + data[6]) + data[7]; 108 return 1000 * (((double)temp) / 0x10000); 109 } 110 111 114 public double getRootDispersion() { 115 long temp = 0; 116 temp = 256 * (256 * (256 * data[8] + data[9]) + data[10]) + data[11]; 117 return 1000 * (((double)temp) / 0x10000); 118 } 119 120 123 private int getReferenceIdentifierType() { 124 125 if (getMode() == NtpInfo.MODE_CLIENT) { 126 return RI_OTHER; 127 } else if (getStratum() < 2) { 128 return RI_CODE; 129 } else if (getVersionNumber() <= 3) { 130 return RI_IP_ADDRESS; 131 } else { 132 return RI_OTHER; 133 } 134 } 135 136 private InetAddress getReferenceAddress() throws IllegalArgumentException , UnknownHostException { 137 138 if (getReferenceIdentifierType() != RI_IP_ADDRESS) { 139 throw new IllegalArgumentException (); 140 } 141 String temp = "" + mp(data[12]) + "." + mp(data[13]) + "." + mp(data[14]) + "." + mp(data[15]); 142 return InetAddress.getByName(temp); 143 } 144 145 private String getReferenceCode() throws IllegalArgumentException { 146 147 if (getReferenceIdentifierType() != RI_CODE) { 148 throw new IllegalArgumentException (); 149 } 150 int codeLength = 0; 151 int index = 12; 152 boolean zeroFound = false; 153 154 while ((!zeroFound) && (index <= 15)) { 155 156 if (data[index] == 0) { 157 zeroFound = true; 158 } else { 159 index++; 160 codeLength++; 161 } 162 } 163 return new String (data, 12, codeLength); 164 } 165 166 private byte[] getReferenceData() { 167 byte[] temp = new byte[4]; 168 temp[0] = data[12]; 169 temp[1] = data[13]; 170 temp[2] = data[14]; 171 temp[3] = data[15]; 172 return temp; 173 } 174 175 180 public Object getReferenceIdentifier() { 181 182 if (getReferenceIdentifierType() == RI_IP_ADDRESS) { 183 try { 184 return getReferenceAddress(); 185 } catch (Exception e) { 186 return getReferenceData(); 187 } 188 } else if (getReferenceIdentifierType() == RI_CODE) { 189 return getReferenceCode(); 190 } else { 191 return getReferenceData(); 192 } 193 } 194 195 public String toString() { 196 String s = "Leap year indicator : " + getLeapYearIndicator() + "\n" + "Version number : " + getVersionNumber() + "\n" + "Mode : " + getMode() + "\n" + "Stratum : " + getStratum() + "\n" + "Poll interval : " + getPollInterval() + " s\n" + "Precision : " + getPrecision() + " ms\n" + "Root delay : " + getRootDelay() + " ms\n" + "Root dispersion : " + getRootDispersion() + " ms\n"; 197 Object o = getReferenceIdentifier(); 198 199 if (o instanceof InetAddress) { 200 s = s + "Reference address : " + (InetAddress)o; 201 } else if (o instanceof String ) { 202 s = s + "Reference code : " + (String )o; 203 } else { 204 byte[] temp = (byte[])o; 205 s = s + "Reference data : " + temp[0] + " " + temp[1] + " " + temp[2] + " " + temp[3]; 206 } 207 return s; 208 } 209 } 210 | Popular Tags |