1 package org.apache.commons.net.ntp; 2 17 import java.net.DatagramPacket ; 18 19 28 public class NtpV3Impl implements NtpV3Packet 29 { 30 31 private static final int MODE_INDEX = 0; 32 private static final int MODE_SHIFT = 0; 33 34 private static final int VERSION_INDEX = 0; 35 private static final int VERSION_SHIFT = 3; 36 37 private static final int LI_INDEX = 0; 38 private static final int LI_SHIFT = 6; 39 40 private static final int STRATUM_INDEX = 1; 41 private static final int POLL_INDEX = 2; 42 private static final int PRECISION_INDEX = 3; 43 44 private static final int ROOT_DELAY_INDEX = 4; 45 private static final int ROOT_DISPERSION_INDEX = 8; 46 private static final int REFERENCE_ID_INDEX = 12; 47 48 private static final int REFERENCE_TIMESTAMP_INDEX = 16; 49 private static final int ORIGINATE_TIMESTAMP_INDEX = 24; 50 private static final int RECEIVE_TIMESTAMP_INDEX = 32; 51 private static final int TRANSMIT_TIMESTAMP_INDEX = 40; 52 53 private static final int KEY_IDENTIFIER_INDEX = 48; 54 private static final int MESSAGE_DIGEST = 54; 55 56 private byte[] buf = new byte[48]; 57 58 private DatagramPacket dp; 59 60 61 public NtpV3Impl() 62 { 63 } 64 65 71 public int getMode() 72 { 73 return (ui(buf[MODE_INDEX]) >> MODE_SHIFT) & 0x7; 74 } 75 76 81 public String getModeName() 82 { 83 return NtpUtils.getModeName(getMode()); 84 } 85 86 90 public void setMode(int mode) 91 { 92 buf[MODE_INDEX] = (byte) (buf[MODE_INDEX] & 0xF8 | mode & 0x7); 93 } 94 95 104 public int getLeapIndicator() 105 { 106 return (ui(buf[LI_INDEX]) >> LI_SHIFT) & 0x3; 107 } 108 109 113 public void setLeapIndicator(int li) 114 { 115 buf[LI_INDEX] = (byte) (buf[LI_INDEX] & 0x3F | ((li & 0x3) << LI_SHIFT)); 116 } 117 118 127 public int getPoll() 128 { 129 return (int) (buf[POLL_INDEX]); 130 } 131 132 137 public void setPoll(int poll) 138 { 139 buf[POLL_INDEX] = (byte) (poll & 0xFF); 140 } 141 142 149 public int getPrecision() 150 { 151 return (int) buf[PRECISION_INDEX]; 152 } 153 154 158 public void setPrecision(int precision) 159 { 160 buf[PRECISION_INDEX] = (byte) (precision & 0xFF); 161 } 162 163 168 public int getVersion() 169 { 170 return (ui(buf[VERSION_INDEX]) >> VERSION_SHIFT) & 0x7; 171 } 172 173 178 public void setVersion(int version) 179 { 180 buf[VERSION_INDEX] = (byte) (buf[VERSION_INDEX] & 0xC7 | ((version & 0x7) << VERSION_SHIFT)); 181 } 182 183 190 public int getStratum() 191 { 192 return ui(buf[STRATUM_INDEX]); 193 } 194 195 200 public void setStratum(int stratum) 201 { 202 buf[STRATUM_INDEX] = (byte) (stratum & 0xFF); 203 } 204 205 212 public int getRootDelay() 213 { 214 return getInt(ROOT_DELAY_INDEX); 215 } 216 217 225 public double getRootDelayInMillisDouble() 226 { 227 double l = getRootDelay(); 228 return l / 65.536; 229 } 230 231 235 public int getRootDispersion() 236 { 237 return getInt(ROOT_DISPERSION_INDEX); 238 } 239 240 245 public long getRootDispersionInMillis() 246 { 247 long l = getRootDispersion(); 248 return (l * 1000) / 65536L; 249 } 250 251 257 public double getRootDispersionInMillisDouble() 258 { 259 double l = getRootDispersion(); 260 return l / 65.536; 261 } 262 263 269 public void setReferenceId(int refId) 270 { 271 for (int i = 3; i >= 0; i--) { 272 buf[REFERENCE_ID_INDEX + i] = (byte) (refId & 0xff); 273 refId >>>= 8; } 275 } 276 277 283 public int getReferenceId() 284 { 285 return getInt(REFERENCE_ID_INDEX); 286 } 287 288 296 public String getReferenceIdString() 297 { 298 int version = getVersion(); 299 int stratum = getStratum(); 300 if (version == VERSION_3 || version == VERSION_4) { 301 if (stratum == 0 || stratum == 1) { 302 return idAsString(); } 304 if (version == VERSION_4) 306 return idAsHex(); 307 } 308 309 if (stratum >= 2) { 312 return idAsIPAddress(); 313 } 314 return idAsHex(); 315 } 316 317 321 private String idAsIPAddress() 322 { 323 return ui(buf[REFERENCE_ID_INDEX]) + "." + 324 ui(buf[REFERENCE_ID_INDEX + 1]) + "." + 325 ui(buf[REFERENCE_ID_INDEX + 2]) + "." + 326 ui(buf[REFERENCE_ID_INDEX + 3]); 327 } 328 329 private String idAsString() 330 { 331 String id = ""; 332 for (int i = 0; i <= 3; i++) { 333 char c = (char) buf[REFERENCE_ID_INDEX + i]; 334 if (c == 0) break; id = id + c; 336 } 337 return id; 338 } 339 340 private String idAsHex() 341 { 342 return Integer.toHexString(getReferenceId()); 343 } 344 345 351 public TimeStamp getTransmitTimeStamp() 352 { 353 return getTimestamp(TRANSMIT_TIMESTAMP_INDEX); 354 } 355 356 362 public void setTransmitTime(TimeStamp ts) 363 { 364 setTimestamp(TRANSMIT_TIMESTAMP_INDEX, ts); 365 } 366 367 373 public void setOriginateTimeStamp(TimeStamp ts) 374 { 375 setTimestamp(ORIGINATE_TIMESTAMP_INDEX, ts); 376 } 377 378 384 public TimeStamp getOriginateTimeStamp() 385 { 386 return getTimestamp(ORIGINATE_TIMESTAMP_INDEX); 387 } 388 389 395 public TimeStamp getReferenceTimeStamp() 396 { 397 return getTimestamp(REFERENCE_TIMESTAMP_INDEX); 398 } 399 400 406 public void setReferenceTime(TimeStamp ts) 407 { 408 setTimestamp(REFERENCE_TIMESTAMP_INDEX, ts); 409 } 410 411 417 public TimeStamp getReceiveTimeStamp() 418 { 419 return getTimestamp(RECEIVE_TIMESTAMP_INDEX); 420 } 421 422 428 public void setReceiveTimeStamp(TimeStamp ts) 429 { 430 setTimestamp(RECEIVE_TIMESTAMP_INDEX, ts); 431 } 432 433 439 public String getType() 440 { 441 return "NTP"; 442 } 443 444 447 private int getInt(int index) 448 { 449 int i = ui(buf[index]) << 24 | 450 ui(buf[index + 1]) << 16 | 451 ui(buf[index + 2]) << 8 | 452 ui(buf[index + 3]); 453 454 return i; 455 } 456 457 463 private TimeStamp getTimestamp(int index) 464 { 465 return new TimeStamp(getLong(index)); 466 } 467 468 473 private long getLong(int index) 474 { 475 long i = ul(buf[index]) << 56 | 476 ul(buf[index + 1]) << 48 | 477 ul(buf[index + 2]) << 40 | 478 ul(buf[index + 3]) << 32 | 479 ul(buf[index + 4]) << 24 | 480 ul(buf[index + 5]) << 16 | 481 ul(buf[index + 6]) << 8 | 482 ul(buf[index + 7]); 483 return i; 484 } 485 486 492 private void setTimestamp(int index, TimeStamp t) 493 { 494 long ntpTime = (t == null) ? 0 : t.ntpValue(); 495 for (int i = 7; i >= 0; i--) { 498 buf[index + i] = (byte) (ntpTime & 0xFF); 499 ntpTime >>>= 8; } 501 } 503 504 509 public DatagramPacket getDatagramPacket() 510 { 511 if (dp == null) 512 synchronized(this) { 513 if (dp == null) { 514 dp = new DatagramPacket (buf, buf.length); 515 dp.setPort(NTP_PORT); 516 } 517 } 518 return dp; 519 } 520 521 526 public void setDatagramPacket(DatagramPacket srcDp) 527 { 528 byte[] incomingBuf = srcDp.getData(); 529 int len = srcDp.getLength(); 530 if (len > buf.length) 531 len = buf.length; 532 533 System.arraycopy(incomingBuf, 0, buf, 0, len); 534 } 535 536 544 protected final static int ui(byte b) 545 { 546 int i = b & 0xFF; 547 return i; 548 } 549 550 558 protected final static long ul(byte b) 559 { 560 long i = b & 0xFF; 561 return i; 562 } 563 564 569 public String toString() 570 { 571 return "[" + 572 "version:" + getVersion() + 573 ", mode:" + getMode() + 574 ", poll:" + getPoll() + 575 ", precision:" + getPrecision() + 576 ", delay:" + getRootDelay() + 577 ", dispersion(ms):" + getRootDispersionInMillisDouble() + 578 ", id:" + getReferenceIdString() + 579 ", xmitTime:" + getTransmitTimeStamp().toDateString() + 580 " ]"; 581 } 582 583 } 584 | Popular Tags |