default

NTPv4 packet header encoding and decoding (RFC 5905).

Examples

Round-trip a client request packet

import { assertEquals } from "@std/assert";
import { ntpPacket, NTP_MODE, NTP_PACKET_SIZE } from "@binstruct/ntp";

const coder = ntpPacket();
const packet = {
  leapVersionMode: { leapIndicator: 0, version: 4, mode: NTP_MODE.CLIENT },
  stratum: 0,
  poll: 4,
  precision: -20,
  rootDelay: 0,
  rootDispersion: 0,
  referenceId: 0,
  referenceTimestamp: 0n,
  originTimestamp: 0n,
  receiveTimestamp: 0n,
  transmitTimestamp: 0xe4c5c46700000000n,
};

const buffer = new Uint8Array(NTP_PACKET_SIZE);
const written = coder.encode(packet, buffer);
const [decoded, read] = coder.decode(buffer);

assertEquals(written, NTP_PACKET_SIZE);
assertEquals(read, NTP_PACKET_SIZE);
assertEquals(decoded, packet);
assertEquals(decoded.leapVersionMode.mode, NTP_MODE.CLIENT);

Functions

f
ntpPacket(): Coder<NtpPacket>

Creates a coder for a fixed 48-byte NTPv4 packet header.

Interfaces

Variables

v
NTP_LEAP_INDICATOR: { NO_WARNING: number; LAST_MINUTE_61: number; LAST_MINUTE_59: number; UNSYNCHRONIZED: number; }

leapIndicator values carried in the first byte of an NTP header (RFC 5905 §7.3), warning of an impending leap second.

v
NTP_PACKET_SIZE: 48

Size in bytes of a fixed NTPv4 header, before any extension fields or MAC.

v
NTP_PORT: 123

The IANA-assigned UDP (and TCP) port for the Network Time Protocol.