-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
182 lines (163 loc) · 5 KB
/
parser.ts
File metadata and controls
182 lines (163 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* NTP Timestamp Parser
* Handles conversion between NTP timestamps and JavaScript Date objects
*/
export interface RawNTPTimestamp {
seconds: number;
fraction: number;
}
export interface ParsedNTPTimestamp {
raw: RawNTPTimestamp;
unix: {
seconds: number;
milliseconds: number;
};
date: Date;
iso: string;
local: string;
timestamp: number;
}
export interface NTPPacketHeader {
leapIndicator: number;
versionNumber: number;
mode: number;
stratum: number;
pollInterval: number;
precision: number;
rootDelay: number;
rootDispersion: number;
referenceId: string;
}
export interface NTPTimestamps {
reference: ParsedNTPTimestamp;
originate: ParsedNTPTimestamp;
receive: ParsedNTPTimestamp;
transmit: ParsedNTPTimestamp;
}
export interface ParsedNTPPacket {
header: NTPPacketHeader;
timestamps: NTPTimestamps;
roundTripDelay: number | null;
clockOffset: number | null;
}
export class NTPParser {
/**
* NTP epoch starts on January 1, 1900
* Unix epoch starts on January 1, 1970
* The difference is 2,208,988,800 seconds
*/
private static readonly NTP_EPOCH_OFFSET = 2208988800;
/**
* Parse raw NTP response buffer (48 bytes)
*
* Packet structure:
* - Bytes 0-3: LI, VN, Mode, Stratum, Poll, Precision
* - Bytes 4-7: Root Delay
* - Bytes 8-11: Root Dispersion
* - Bytes 12-15: Reference ID
* - Bytes 16-23: Reference Timestamp
* - Bytes 24-31: Originate Timestamp
* - Bytes 32-39: Receive Timestamp
* - Bytes 40-47: Transmit Timestamp
*/
static parsePacket(buffer: Buffer): ParsedNTPPacket {
if (buffer.length < 48) {
throw new Error('Invalid NTP packet: minimum 48 bytes required');
}
// Parse header
const firstByte = buffer[0];
const leapIndicator = (firstByte >> 6) & 0b11;
const versionNumber = (firstByte >> 3) & 0b111;
const mode = firstByte & 0b111;
const stratum = buffer[1];
const pollInterval = buffer[2];
const precision = buffer[3];
// Parse timestamps
const referenceTimestamp = this.parseNTPTimestamp(buffer, 16);
const originateTimestamp = this.parseNTPTimestamp(buffer, 24);
const receiveTimestamp = this.parseNTPTimestamp(buffer, 32);
const transmitTimestamp = this.parseNTPTimestamp(buffer, 40);
// Parse root delay and dispersion
const rootDelay = buffer.readUInt32BE(4);
const rootDispersion = buffer.readUInt32BE(8);
const referenceId = buffer.readUInt32BE(12);
return {
header: {
leapIndicator,
versionNumber,
mode,
stratum,
pollInterval,
precision,
rootDelay,
rootDispersion,
referenceId: this.formatReferenceId(referenceId, stratum),
},
timestamps: {
reference: referenceTimestamp,
originate: originateTimestamp,
receive: receiveTimestamp,
transmit: transmitTimestamp,
},
roundTripDelay: null,
clockOffset: null,
};
}
/**
* Parse 8-byte NTP timestamp (64-bit fixed-point: 32-bit seconds, 32-bit fraction)
*/
private static parseNTPTimestamp(buffer: Buffer, offset: number): ParsedNTPTimestamp {
const seconds = buffer.readUInt32BE(offset);
const fraction = buffer.readUInt32BE(offset + 4);
// Convert fraction to milliseconds (32-bit fraction / 2^32 * 1000)
const milliseconds = (fraction / 0x100000000) * 1000;
// Convert NTP timestamp to Unix timestamp
const unixSeconds = seconds - this.NTP_EPOCH_OFFSET;
const date = new Date((unixSeconds * 1000) + milliseconds);
return {
raw: { seconds, fraction },
unix: {
seconds: unixSeconds,
milliseconds,
},
date,
iso: date.toISOString(),
local: date.toString(),
timestamp: date.getTime(),
};
}
/**
* Format reference ID based on stratum
*/
private static formatReferenceId(id: number, stratum: number): string {
if (stratum === 0) return 'KISS';
if (stratum === 1) {
// First 4 bytes are ASCII
const bytes = Buffer.alloc(4);
bytes.writeUInt32BE(id);
return bytes.toString('ascii').replace(/\x00/g, '');
}
return `0x${id.toString(16).toUpperCase().padStart(8, '0')}`;
}
/**
* Calculate round-trip delay and clock offset
*/
static calculateMetrics(
parsed: ParsedNTPPacket,
clientOriginateTime: Date,
clientReceiveTime: Date
): { roundTripDelay: number; clockOffset: number; roundTripDelayMs: number; clockOffsetMs: number } {
const t1 = clientOriginateTime.getTime() / 1000;
const t2 = parsed.timestamps.receive.unix.seconds + parsed.timestamps.receive.unix.milliseconds / 1000;
const t3 = parsed.timestamps.transmit.unix.seconds + parsed.timestamps.transmit.unix.milliseconds / 1000;
const t4 = clientReceiveTime.getTime() / 1000;
const roundTripDelay = (t4 - t1) - (t3 - t2);
const clockOffset = ((t2 - t1) + (t3 - t4)) / 2;
return {
roundTripDelay,
clockOffset,
roundTripDelayMs: roundTripDelay * 1000,
clockOffsetMs: clockOffset * 1000,
};
}
}