From 5abc71c8cd122b439c2b69d1db8b2e78ef368c7c Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 16:59:00 -0800 Subject: [PATCH 1/6] Add stratum info Include the stratum info for a given ntp server --- lib/ntp-client.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/ntp-client.js b/lib/ntp-client.js index 4b9a840..3cb037b 100644 --- a/lib/ntp-client.js +++ b/lib/ntp-client.js @@ -21,11 +21,10 @@ exports.ntpReplyTimeout = 10000; /** - * Fetches the current NTP Time from the given server and port. + * Fetches the current NTP Time from the given server and port, including stratum. * @param {string} server IP/Hostname of the remote NTP Server * @param {number} port Remote NTP Server port number - * @param {function(Object, Date)} callback(err, date) Async callback for - * the result date or eventually error. + * @param {function(Object, Date, number)} callback(err, date, stratum) Async callback for the result date, stratum, or error. */ exports.getNetworkTime = function (server, port, callback) { if (callback === null || typeof callback !== "function") { @@ -85,6 +84,9 @@ clearTimeout(timeout); client.close(); + // Extract Stratum (byte 1) + var stratum = msg[1]; + // Offset to get to the "Transmit Timestamp" field (time at which the reply // departed the server for the client, in 64-bit timestamp format." var offsetTransmitTime = 40, @@ -107,7 +109,7 @@ var date = new Date("Jan 01 1900 GMT"); date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds); - callback(null, date); + callback(null, date, stratum); }); }); }; From 1dce0bec9f13aef18c32931a5326e404bcb13c50 Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 17:06:13 -0800 Subject: [PATCH 2/6] Update README.md - Update examples to add a stratum example - Add self as contributor --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 8e83c17..a060734 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Pure Javascript implementation of the NTP Client Protocol ## Getting Started Install the module with: `npm install ntp-client` +## Examples +### Get just the time ```javascript var ntpClient = require('ntp-client'); @@ -19,9 +21,28 @@ ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date) { }); ``` +### Get the time and the server's stratum +Get the server's startum: +```javascript +var ntpClient = require('ntp-client'); + +ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date, stratum) { + if(err) { + console.error(err); + return; + } + + console.log("Current time : "); + console.log(date); // Mon Jul 08 2013 21:31:31 GMT+0200 (Paris, Madrid (heure d’été)) + console.log("Current stratum : "); + console.log(stratum); // 3 +}); +``` + ## Contributors * Clément Bourgeois (https://github.com/moonpyk) * Callan Bryant (https://github.com/naggie) + * SomKen (https://github.com/somken) ## License Copyright (c) 2014 Clément Bourgeois From d109113cbb7588b72b4b7245511144317c1da460 Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 17:07:42 -0800 Subject: [PATCH 3/6] Update ntp-client.js - Update ntpdata to use Buffer.alloc - Add stratum info to exports.demo --- lib/ntp-client.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/ntp-client.js b/lib/ntp-client.js index 3cb037b..07e6dc7 100644 --- a/lib/ntp-client.js +++ b/lib/ntp-client.js @@ -35,7 +35,7 @@ port = port || exports.defaultNtpPort; var client = dgram.createSocket("udp4"), - ntpData = new Buffer(48); + ntpData = Buffer.alloc(48); // RFC 2030 -> LI = 0 (no warning, 2 bits), VN = 3 (IPv4 only, 3 bits), Mode = 3 (Client Mode, 3 bits) -> 1 byte // -> rtol(LI, 6) ^ rotl(VN, 3) ^ rotl(Mode, 0) @@ -48,13 +48,9 @@ var timeout = setTimeout(function () { client.close(); - callback("Timeout waiting for NTP response.", null); + callback("Timeout waiting for NTP response.", null, null); }, exports.ntpReplyTimeout); - // Some errors can happen before/after send() or cause send() to was impossible. - // Some errors will also be given to the send() callback. - // We keep a flag, therefore, to prevent multiple callbacks. - // NOTE : the error callback is not generalised, as the client has to lose the connection also, apparently. var errorFired = false; client.on('error', function (err) { @@ -62,7 +58,7 @@ return; } - callback(err, null); + callback(err, null, null); errorFired = true; clearTimeout(timeout); @@ -74,7 +70,7 @@ return; } clearTimeout(timeout); - callback(err, null); + callback(err, null, null); errorFired = true; client.close(); return; @@ -85,10 +81,10 @@ client.close(); // Extract Stratum (byte 1) - var stratum = msg[1]; + var stratum = msg[1]; // Offset to get to the "Transmit Timestamp" field (time at which the reply - // departed the server for the client, in 64-bit timestamp format." + // departed the server for the client, in 64-bit timestamp format) var offsetTransmitTime = 40, intpart = 0, fractpart = 0; @@ -109,6 +105,7 @@ var date = new Date("Jan 01 1900 GMT"); date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds); + // Return date and stratum in callback callback(null, date, stratum); }); }); @@ -118,13 +115,14 @@ exports.getNetworkTime( exports.defaultNtpServer, exports.defaultNtpPort, - function (err, date) { + function (err, date, stratum) { if (err) { console.error(err); return; } - console.log(date); + console.log('NTP Date:', date); + console.log('Stratum:', stratum); }); }; }(exports)); From f6465ed4827ec43902399811383e88cfa00b32af Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 17:11:12 -0800 Subject: [PATCH 4/6] Update ntp-client.js Add back removed comment --- lib/ntp-client.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/ntp-client.js b/lib/ntp-client.js index 07e6dc7..e19cc24 100644 --- a/lib/ntp-client.js +++ b/lib/ntp-client.js @@ -51,6 +51,10 @@ callback("Timeout waiting for NTP response.", null, null); }, exports.ntpReplyTimeout); + // Some errors can happen before/after send() or cause send() to was impossible. + // Some errors will also be given to the send() callback. + // We keep a flag, therefore, to prevent multiple callbacks. + // NOTE : the error callback is not generalised, as the client has to lose the connection also, apparently. var errorFired = false; client.on('error', function (err) { From 8bc1560c1e07911333eec35c57808ba40a81787d Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 17:27:57 -0800 Subject: [PATCH 5/6] Update ntp-client_test.js - Add stratum test --- test/ntp-client_test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/ntp-client_test.js b/test/ntp-client_test.js index 26c8d00..547f282 100644 --- a/test/ntp-client_test.js +++ b/test/ntp-client_test.js @@ -61,6 +61,26 @@ }; + exports.validNTPServerStratum = function (test) { + ntpClient.getNetworkTime(ntpClient.defaultNtpServer, ntpClient.defaultNtpPort, function (err, date, stratum) { + console.log(); + console.log("System reported : %s", new Date()); + + test.ok(err === null); + test.ok(date !== null); + test.ok(stratum !== null); + test.ok(stratum >= 0); + + console.log("NTP Reported : %s", date); + console.log("Stratum Reported : %s", stratum); + + // I won't test returned datetime against the system datetime + // this is the whole purpose of NTP : putting clocks in sync. + test.done(); + }); + + }; + exports.invalidNTPServer = function (test) { // I'm pretty sure there is no NTP Server listening at google.com ntpClient.getNetworkTime("google.com", 123, function (err, date) { From f260841b4fb6d108a087b829a2e24d86160711ed Mon Sep 17 00:00:00 2001 From: SomKen Date: Sun, 29 Dec 2024 17:28:46 -0800 Subject: [PATCH 6/6] Update package.json - Update version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ce70a50..adaebcc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ntp-client", "description": "Pure Javascript implementation of the NTP Client Protocol", - "version": "0.5.3", + "version": "0.5.4", "homepage": "https://github.com/moonpyk/node-ntp-client", "author": { "name": "Clément Bourgeois",