buildNtpPacket static method

Uint8List buildNtpPacket(
  1. DateTime originateTime
)

Builds a 48-byte NTP request packet according to the protocol specification (RFC 5905).

Implementation

static Uint8List buildNtpPacket(DateTime originateTime) {
  final packet = Uint8List(48);
  // LI = 0 (no warning), VN = 3 (version 3), Mode = 3 (client)
  packet[0] = 0x1B;

  final byteData = ByteData.view(packet.buffer);
  writeNtpTimestamp(byteData, 40, originateTime);

  // RFC 5905 recommends randomizing the last byte of the transmit timestamp
  // to prevent off-path spoofing attacks.
  packet[47] = _random.nextInt(256);

  return packet;
}