|
13 | 13 |
|
14 | 14 | exports.defaultNtpPort = 123; |
15 | 15 | exports.defaultNtpServer = "pool.ntp.org"; |
16 | | - |
| 16 | + |
17 | 17 | /** |
18 | 18 | * Amount of acceptable time to await for a response from the remote server. |
19 | 19 | * Configured default to 10 seconds. |
|
24 | 24 | * Fetches the current NTP Time from the given server and port. |
25 | 25 | * @param {string} server IP/Hostname of the remote NTP Server |
26 | 26 | * @param {number} port Remote NTP Server port number |
27 | | - * @param {function(Object, Date)} callback(err, date) Async callback for |
| 27 | + * @param {function(Object, Date)} callback(err, date) Async callback for |
28 | 28 | * the result date or eventually error. |
29 | 29 | */ |
30 | 30 | exports.getNetworkTime = function (server, port, callback) { |
| 31 | + if (callback === null || typeof callback !== "function") { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + server = server || exports.defaultNtpServer; |
| 36 | + port = port || exports.defaultNtpPort; |
| 37 | + |
31 | 38 | var client = dgram.createSocket("udp4"), |
32 | 39 | ntpData = new Buffer(48); |
33 | 40 |
|
|
37 | 44 | ntpData[i] = 0; |
38 | 45 | } |
39 | 46 |
|
40 | | - var timeout = setTimeout(function() { |
| 47 | + var timeout = setTimeout(function () { |
41 | 48 | client.close(); |
42 | 49 | callback("Timeout waiting for NTP response.", null); |
43 | 50 | }, exports.ntpReplyTimeout); |
|
52 | 59 | client.once('message', function (msg) { |
53 | 60 | clearTimeout(timeout); |
54 | 61 | client.close(); |
55 | | - |
| 62 | + |
56 | 63 | // Offset to get to the "Transmit Timestamp" field (time at which the reply |
57 | 64 | // departed the server for the client, in 64-bit timestamp format." |
58 | 65 | var offsetTransmitTime = 40, |
|
0 commit comments