| 57 | const ms = (sec - NTP_EPOCH_OFFSET) * 1000 + Math.floor((frac / 2 ** 32) * 1000); |
| 58 | return ms; |
| 59 | } |
| 60 | |
| 61 | async function queryOnce(host: string, timeoutMs = 2500): Promise<NtpResult> { |
| 62 | return new Promise((resolve, reject) => { |
| 63 | const socket = dgram.createSocket("udp4"); |
| 64 | let timeoutTimer: NodeJS.Timeout | null = null; |
| 65 | |
| 66 | const packet = Buffer.alloc(48); |
| 67 | // LI = 0 (no warning), VN = 4, Mode = 3 (client) -> 0b00 100 011 = 0x23 |
| 68 | packet[0] = 0x23; |
| 69 | // Transmit timestamp: set to client time (optional but good practice) |