0.4.1 • Published 3 months ago

ntp-packet-parser v0.4.1

Weekly downloads
628
License
GPL-3.0
Repository
github
Last release
3 months ago

NPM downloads Build Status NPM version

NTP packet parser

Library to parse NTP response packets according to RFC 5905 into a easy-to-use structure. It does not apply any validations or calculations regarding the time but solely parses the data.

See buffcode/ntp-time-sync for an implementation.

Installation

yarn add ntp-packet-parser

Usage

ES6 style

import { NtpPacketParser } from "ntp-packet-parser";

/** const udpPacket = new Buffer(...); **/
const result = NtpPacketParser.parse(udpPacket);

Legacy style

var NtpPacketParser = require("ntp-packet-parser").NtpPacketParser;

/** const udpPacket = new Buffer(...); **/
var result = NtpPacketParser.parse(udpPacket);

Structure

The response from NtpPacketParser.parse will return the following object structure:

PropertyTypeDescription
leapIndicatorIntegerWarning of an impending leap second to be inserted or deleted in the last minute of the current month
versionIntegerNTP version number, currently 4
modeIntegerRequest/response mode
stratumIntegerStratum of the server
pollIntegerInteger representing the maximum interval in log2 seconds between successive messages (Note: you need to apply Math.pow(2, <value>) to get the real value)
precisionIntegerInteger representing the precision of the system clock in log2 seconds (Note: you need to apply Math.pow(2, <value>) to get the real value)
rootDelayDateTotal round-trip delay to the reference clock
rootDispersionDateTotal dispersion to the reference clock
referenceIdStringString to identify the particular server or reference clock
referenceTimestampDateTime when the system clock was last set or corrected
originTimestampDateTime at the client when the request departed for the server
receiveTimestampDateTime at the server when the request arrived from the client
transmitTimestampDateTime at the server when the response left for the client

To get the relative time for any Date property, calculate the difference between "Jan 01 1900 GMT" and the given date.

let rootDelayInMilliseconds = result.rootDelay.getTime() - new Date("Jan 01 1900 GMT").getTime(); 

For further explanations on the possible values of these properties please refer to RFC 5909, Page 19ff.

Testing

Some tests regarding structure, response and error handling exist. To run them locally:

yarn test

Contributing

Please file a PR against master.

License

GNU General Public License Version 3

0.4.1

3 months ago

0.4.0

3 months ago

0.3.1

9 months ago

0.3.0

1 year ago

0.2.1

2 years ago

0.2.0

3 years ago

0.1.0

7 years ago