@netkitty/codec 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/README.md +31 -7
  3. package/README.zh-CN.md +28 -7
  4. package/dist/Codec.d.ts +4 -3
  5. package/dist/Codec.js +48 -6
  6. package/dist/PacketHeaders.d.ts +2 -0
  7. package/dist/PacketHeaders.js +8 -4
  8. package/dist/abstracts/BaseHeader.d.ts +43 -0
  9. package/dist/abstracts/BaseHeader.js +75 -0
  10. package/dist/headers/AH.js +3 -2
  11. package/dist/headers/ARP.d.ts +3 -1
  12. package/dist/headers/ARP.js +8 -1
  13. package/dist/headers/BACnetIP.js +2 -1
  14. package/dist/headers/BFD.d.ts +2 -1
  15. package/dist/headers/BFD.js +30 -4
  16. package/dist/headers/BGP.js +8 -3
  17. package/dist/headers/Babel.js +1 -1
  18. package/dist/headers/C37118.d.ts +3 -2
  19. package/dist/headers/C37118.js +34 -15
  20. package/dist/headers/CDP.d.ts +3 -3
  21. package/dist/headers/CDP.js +46 -10
  22. package/dist/headers/CMS.js +25 -10
  23. package/dist/headers/COTP.js +2 -3
  24. package/dist/headers/CoAP.js +20 -2
  25. package/dist/headers/Codesys.js +3 -2
  26. package/dist/headers/DCCP.d.ts +3 -2
  27. package/dist/headers/DCCP.js +60 -8
  28. package/dist/headers/DHCP.js +2 -1
  29. package/dist/headers/DNP3.d.ts +12 -5
  30. package/dist/headers/DNP3.js +153 -18
  31. package/dist/headers/DTLS.js +5 -3
  32. package/dist/headers/Diameter.js +2 -1
  33. package/dist/headers/EIGRP.d.ts +3 -2
  34. package/dist/headers/EIGRP.js +31 -5
  35. package/dist/headers/Elasticsearch.js +3 -2
  36. package/dist/headers/EthernetII.d.ts +1 -0
  37. package/dist/headers/EthernetII.js +4 -0
  38. package/dist/headers/GELF.js +1 -0
  39. package/dist/headers/GENEVE.js +1 -0
  40. package/dist/headers/GRE.js +1 -0
  41. package/dist/headers/GTPv2C.js +1 -0
  42. package/dist/headers/Goose.d.ts +31 -7
  43. package/dist/headers/Goose.js +413 -172
  44. package/dist/headers/HARTIP.js +2 -2
  45. package/dist/headers/HSR.js +1 -0
  46. package/dist/headers/HTTP2.js +1 -0
  47. package/dist/headers/ICMP.js +2 -1
  48. package/dist/headers/ICMPv6.js +10 -16
  49. package/dist/headers/IEC104_I_Frame.d.ts +28 -0
  50. package/dist/headers/IEC104_I_Frame.js +434 -319
  51. package/dist/headers/IEC104_S_Frame.js +22 -4
  52. package/dist/headers/IEC104_U_Frame.js +24 -4
  53. package/dist/headers/IEC61850SampledValues.d.ts +1 -0
  54. package/dist/headers/IEC61850SampledValues.js +20 -5
  55. package/dist/headers/IGMP.d.ts +8 -7
  56. package/dist/headers/IGMP.js +57 -19
  57. package/dist/headers/IPFIX.js +3 -2
  58. package/dist/headers/IPv4.d.ts +1 -0
  59. package/dist/headers/IPv4.js +32 -23
  60. package/dist/headers/IPv6.d.ts +1 -0
  61. package/dist/headers/IPv6.js +10 -1
  62. package/dist/headers/IPv6HopByHopOptions.js +11 -1
  63. package/dist/headers/ISAKMP.js +1 -0
  64. package/dist/headers/KNXnetIP.js +3 -2
  65. package/dist/headers/Kerberos.js +1 -0
  66. package/dist/headers/L2TP.js +1 -0
  67. package/dist/headers/LACP.js +2 -1
  68. package/dist/headers/LDAP.js +1 -1
  69. package/dist/headers/LDP.js +2 -2
  70. package/dist/headers/LLC.js +1 -0
  71. package/dist/headers/MQTT.js +3 -1
  72. package/dist/headers/MarkerProtocol.js +2 -1
  73. package/dist/headers/NHRP.d.ts +1 -1
  74. package/dist/headers/NHRP.js +29 -4
  75. package/dist/headers/NetFlowV5.js +2 -1
  76. package/dist/headers/OPCUA.js +4 -0
  77. package/dist/headers/OSPF.d.ts +5 -5
  78. package/dist/headers/OSPF.js +98 -12
  79. package/dist/headers/OSPFv3.d.ts +37 -0
  80. package/dist/headers/OSPFv3.js +314 -0
  81. package/dist/headers/PIM.d.ts +3 -3
  82. package/dist/headers/PIM.js +55 -6
  83. package/dist/headers/PPPoEDiscovery.js +2 -0
  84. package/dist/headers/PPTP.js +2 -2
  85. package/dist/headers/PTP.js +5 -1
  86. package/dist/headers/QUIC.js +8 -3
  87. package/dist/headers/RDP.js +2 -0
  88. package/dist/headers/RGoose.js +4 -1
  89. package/dist/headers/RIP.js +3 -2
  90. package/dist/headers/RIPng.js +3 -2
  91. package/dist/headers/RSVP.d.ts +3 -3
  92. package/dist/headers/RSVP.js +56 -42
  93. package/dist/headers/RTCP.js +4 -2
  94. package/dist/headers/RTP.js +3 -2
  95. package/dist/headers/SCTP.d.ts +2 -2
  96. package/dist/headers/SCTP.js +29 -5
  97. package/dist/headers/SFlow.js +4 -2
  98. package/dist/headers/SLMP.js +2 -1
  99. package/dist/headers/SLP.js +2 -1
  100. package/dist/headers/SLPv1.js +2 -1
  101. package/dist/headers/SMB1.js +3 -2
  102. package/dist/headers/SMB2.js +3 -2
  103. package/dist/headers/SNAP.js +2 -2
  104. package/dist/headers/SNMP.js +2 -2
  105. package/dist/headers/STUN.js +3 -2
  106. package/dist/headers/SercosIII.d.ts +3 -2
  107. package/dist/headers/SercosIII.js +41 -5
  108. package/dist/headers/TCP.js +13 -58
  109. package/dist/headers/TLS_Alert.js +20 -2
  110. package/dist/headers/TLS_ApplicationData.d.ts +3 -3
  111. package/dist/headers/TLS_ApplicationData.js +25 -6
  112. package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
  113. package/dist/headers/TLS_ChangeCipherSpec.js +24 -4
  114. package/dist/headers/TLS_Handshake.d.ts +6 -0
  115. package/dist/headers/TLS_Handshake.js +67 -9
  116. package/dist/headers/TLS_Heartbeat.d.ts +4 -3
  117. package/dist/headers/TLS_Heartbeat.js +40 -7
  118. package/dist/headers/TPKT.js +1 -1
  119. package/dist/headers/TacacsPlus.js +3 -1
  120. package/dist/headers/Trailer.d.ts +30 -0
  121. package/dist/headers/Trailer.js +63 -0
  122. package/dist/headers/UDP.js +17 -61
  123. package/dist/headers/VLAN_802dot1Q.d.ts +1 -0
  124. package/dist/headers/VLAN_802dot1Q.js +4 -0
  125. package/dist/headers/VRRP.d.ts +2 -1
  126. package/dist/headers/VRRP.js +45 -9
  127. package/dist/headers/WakeOnLAN.js +2 -1
  128. package/dist/headers/WireGuard.js +3 -2
  129. package/dist/headers/Zabbix.js +2 -2
  130. package/dist/helper/Checksum.d.ts +15 -0
  131. package/dist/helper/Checksum.js +56 -0
  132. package/dist/helper/Crc.d.ts +18 -0
  133. package/dist/helper/Crc.js +58 -0
  134. package/dist/lib/BERElement.d.ts +51 -0
  135. package/dist/lib/BERElement.js +125 -0
  136. package/package.json +2 -2
@@ -0,0 +1,314 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OSPFv3 = void 0;
4
+ const BaseHeader_1 = require("../abstracts/BaseHeader");
5
+ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
6
+ const BufferToIP_1 = require("../helper/BufferToIP");
7
+ const IPToBuffer_1 = require("../helper/IPToBuffer");
8
+ const BufferToHex_1 = require("../helper/BufferToHex");
9
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
10
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
11
+ const Checksum_1 = require("../helper/Checksum");
12
+ /**
13
+ * OSPFv3 — Open Shortest Path First for IPv6 (RFC 5340), carried on IPv6 as Next Header 89 (the same
14
+ * protocol number as OSPFv2, told apart by the Version octet). This codec decodes the 16-octet common
15
+ * header (RFC 5340 §A.3.1): an 8-bit Version (`version`, offset 0, 3), an 8-bit Message Type (`type`,
16
+ * offset 1; 1 Hello, 2 Database Description, 3 LS Request, 4 LS Update, 5 LS Ack), a 16-bit Packet Length
17
+ * (`packetLength`, offset 2), the Router ID (`routerId`, offset 4) and Area ID (`areaId`, offset 8) in
18
+ * dotted-quad form, a 16-bit Checksum (`checksum`, offset 12), an 8-bit Instance ID (`instanceId`, offset
19
+ * 14) and a Reserved octet (`reserved`, hex, offset 15). Unlike OSPFv2 there is no authentication in the
20
+ * header (RFC 4552 IPsec, or the RFC 7166 trailer after the packet, does that job). Packet Length and
21
+ * Checksum are computed on encode when left out and written as given otherwise; the checksum is the
22
+ * Internet checksum of the whole packet with the IPv6 pseudo-header prepended, its Upper-Layer Packet
23
+ * Length being the Packet Length field (§A.3.1).
24
+ *
25
+ * For a Hello (type 1, §A.3.2) the body is decoded field by field: Interface ID (offset 16), Router
26
+ * Priority (offset 20), the 24-bit Options (offset 21; V6 0x01, E 0x02, MC 0x04, N 0x08, R 0x10, DC 0x20,
27
+ * AF 0x100, L 0x200, AT 0x400), Hello Interval and Router Dead Interval (16 bits each, offsets 24 and 26),
28
+ * Designated and Backup Designated Router IDs (offsets 28 and 32), then the Router IDs of the neighbours
29
+ * (4 octets each from offset 36). Built from defaults, a Hello carries priority 1, Options V6|E|R, hello
30
+ * interval 10 and dead interval 40. For all other types the body is kept verbatim as `rawBody` hex from
31
+ * offset 16. Both are bounded by min(Packet Length, the end of the IPv6 payload) and never below the
32
+ * 16-octet header, so a lying length can't read past the real IP payload. match() fires on Next Header
33
+ * 89 with an IPv6 layer beneath (directly, or behind AH / an extension header), Version 3 and a full
34
+ * common header present.
35
+ */
36
+ class OSPFv3 extends BaseHeader_1.BaseHeader {
37
+ constructor() {
38
+ super(...arguments);
39
+ this.id = 'ospfv3';
40
+ this.matchKeys = ['ipproto:89'];
41
+ this.name = 'Open Shortest Path First version 3';
42
+ this.nickname = 'OSPFv3';
43
+ //A leaf header (LSA bodies are kept as rawBody).
44
+ this.demuxProducers = [];
45
+ }
46
+ /**
47
+ * Bytes of OSPFv3 the IPv6 layer beneath says are available: up to the end of its payload (Payload
48
+ * Length after its 40-octet header), so AH or an extension header in between is accounted for. The
49
+ * captured remainder when there is no Payload Length to go by.
50
+ * @private
51
+ */
52
+ #available() {
53
+ let available = this.packet.length - this.startPos;
54
+ const ip = this.nearestIPModule();
55
+ if (ip && ip.id === 'ipv6') {
56
+ const payloadLength = ip.instance.plen.getValue(0);
57
+ const ipPayloadEnd = ip.startPos + ip.length + payloadLength - this.startPos;
58
+ if (payloadLength > 0 && ipPayloadEnd < available)
59
+ available = ipPayloadEnd;
60
+ }
61
+ return available < 0 ? 0 : available;
62
+ }
63
+ /**
64
+ * Header-relative end offset of the OSPFv3 packet body: the Packet Length field, clamped down to the
65
+ * bytes the IPv6 layer made available (#available). Never less than the 16-octet common header. Both
66
+ * the Hello neighbour list and the raw-body fallback are bounded by this.
67
+ * @private
68
+ */
69
+ #bodyEnd() {
70
+ let end = this.instance.packetLength.getValue(0);
71
+ const available = this.#available();
72
+ if (available < end)
73
+ end = available;
74
+ if (end < 16)
75
+ end = 16;
76
+ return end;
77
+ }
78
+ /** Whether this is a Hello (type 1), whose body is decoded field by field. */
79
+ #isHello() {
80
+ const type = this.instance.type.getValue(0);
81
+ return type === 1;
82
+ }
83
+ /**
84
+ * The OSPFv3 checksum (RFC 5340 §A.3.1): the Internet checksum of the whole packet with the IPv6
85
+ * pseudo-header (Next Header 89, Upper-Layer Packet Length = the Packet Length field) prepended.
86
+ * Called once every field is written, with the checksum still zero.
87
+ * @private
88
+ */
89
+ #calculateChecksum() {
90
+ const ip = this.nearestIPModule();
91
+ if (!ip || ip.id !== 'ipv6') {
92
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute OSPFv3 checksum: no IPv6 layer beneath OSPFv3');
93
+ return 0;
94
+ }
95
+ const pseudoHeader = this.ipPseudoHeader(89, this.instance.packetLength.getValue(0));
96
+ if (!pseudoHeader) {
97
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute OSPFv3 checksum: the IPv6 addresses beneath are unusable');
98
+ return 0;
99
+ }
100
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, this.packet.subarray(this.startPos, this.endPos));
101
+ }
102
+ static #schemaCache;
103
+ //Class-cached SCHEMA (field closures are plain functions taking dynamic `this` via .call(this)).
104
+ get SCHEMA() {
105
+ return (OSPFv3.#schemaCache ??= OSPFv3.#buildSchema());
106
+ }
107
+ /**
108
+ * An unsigned big-endian Hello field of `byteLength` octets at `offset`, decoded and encoded only for
109
+ * a Hello (type 1). Left out, it is `defaultValue` (also the schema default).
110
+ * @private
111
+ */
112
+ static #helloUInt(name, offset, byteLength, label, defaultValue) {
113
+ return {
114
+ type: 'integer',
115
+ label: label,
116
+ minimum: 0,
117
+ maximum: 2 ** (byteLength * 8) - 1,
118
+ default: defaultValue,
119
+ decode: function () {
120
+ if (!this.#isHello())
121
+ return;
122
+ this.instance.hello[name].setValue(this.readBits(offset, byteLength, 0, byteLength * 8));
123
+ },
124
+ encode: function () {
125
+ if (!this.#isHello())
126
+ return;
127
+ const value = this.instance.hello[name].getValue(defaultValue);
128
+ this.instance.hello[name].setValue(value);
129
+ this.writeBits(offset, byteLength, 0, byteLength * 8, value);
130
+ }
131
+ };
132
+ }
133
+ /**
134
+ * A Router ID of the Hello body (dotted quad) at `offset`, decoded and encoded only for a Hello
135
+ * (type 1). Left out, it is 0.0.0.0 (none).
136
+ * @private
137
+ */
138
+ static #helloRouterId(name, offset, label) {
139
+ return {
140
+ type: 'string',
141
+ label: label,
142
+ minLength: 7,
143
+ maxLength: 15,
144
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.IPv4,
145
+ decode: function () {
146
+ if (!this.#isHello())
147
+ return;
148
+ this.instance.hello[name].setValue((0, BufferToIP_1.BufferToIPv4)(this.readBytes(offset, 4)));
149
+ },
150
+ encode: function () {
151
+ if (!this.#isHello())
152
+ return;
153
+ const routerId = this.instance.hello[name].getValue('0.0.0.0');
154
+ this.instance.hello[name].setValue(routerId);
155
+ this.writeBytes(offset, (0, IPToBuffer_1.IPv4ToBuffer)(routerId));
156
+ }
157
+ };
158
+ }
159
+ static #buildSchema() {
160
+ return {
161
+ type: 'object',
162
+ summary: 'OSPFv3 type=${type} rid=${routerId}',
163
+ properties: {
164
+ //==== Common header (16 bytes, RFC 5340 §A.3.1) ====
165
+ //Default 3, what match() requires.
166
+ version: { ...this.fieldUInt('version', 0, 1, 'Version'), default: 3 },
167
+ type: { ...this.fieldUInt('type', 1, 1, 'Message Type'), default: 1 },
168
+ //The whole OSPFv3 packet including this header; left out, the bytes actually written, filled
169
+ //once every field is encoded (before the checksum); a given value is written as given.
170
+ packetLength: {
171
+ type: 'integer',
172
+ autoCalculated: true,
173
+ label: 'Packet Length',
174
+ minimum: 0,
175
+ maximum: 65535,
176
+ decode: function () {
177
+ this.instance.packetLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
178
+ },
179
+ encode: function () {
180
+ if (!this.isLeftOut(this.instance.packetLength)) {
181
+ const packetLength = this.instance.packetLength.getValue(0);
182
+ this.instance.packetLength.setValue(packetLength);
183
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(packetLength));
184
+ return;
185
+ }
186
+ this.instance.packetLength.setValue(0);
187
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
188
+ this.addPostSelfEncodeHandler(() => {
189
+ this.instance.packetLength.setValue(this.length);
190
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(this.length));
191
+ }, 1);
192
+ }
193
+ },
194
+ routerId: this.fieldIPv4('routerId', 4, 'Router ID'),
195
+ areaId: this.fieldIPv4('areaId', 8, 'Area ID'),
196
+ //Left out, computed after the Packet Length once every field is written
197
+ //(#calculateChecksum); a given value is written as given.
198
+ checksum: {
199
+ type: 'integer',
200
+ autoCalculated: true,
201
+ label: 'Checksum',
202
+ minimum: 0,
203
+ maximum: 65535,
204
+ decode: function () {
205
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2)));
206
+ },
207
+ encode: function () {
208
+ if (!this.isLeftOut(this.instance.checksum)) {
209
+ const checksum = this.instance.checksum.getValue(0);
210
+ this.instance.checksum.setValue(checksum);
211
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
212
+ return;
213
+ }
214
+ this.instance.checksum.setValue(0);
215
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
216
+ this.addPostSelfEncodeHandler(() => {
217
+ const checksum = this.#calculateChecksum();
218
+ this.instance.checksum.setValue(checksum);
219
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
220
+ }, 2);
221
+ }
222
+ },
223
+ //Distinguishes OSPFv3 instances on one link; 0 is the base IPv6 unicast instance.
224
+ instanceId: { ...this.fieldUInt('instanceId', 14, 1, 'Instance ID'), default: 0 },
225
+ //0 on transmit; kept verbatim so a non-canonical packet still round-trips.
226
+ reserved: this.fieldHex('reserved', 15, 1, 'Reserved'),
227
+ //==== Hello body (RFC 5340 §A.3.2), decoded only for type 1 ====
228
+ hello: {
229
+ type: 'object',
230
+ label: 'Hello Packet',
231
+ properties: {
232
+ interfaceId: this.#helloUInt('interfaceId', 16, 4, 'Interface ID', 0),
233
+ routerPriority: this.#helloUInt('routerPriority', 20, 1, 'Router Priority', 1),
234
+ //24 bits, RFC 5340 §A.2: V6 0x01, E 0x02, MC 0x04, N 0x08, R 0x10, DC 0x20, plus
235
+ //AF 0x100 (RFC 5838), L 0x200 (RFC 5613), AT 0x400 (RFC 7166). Default V6|E|R.
236
+ options: this.#helloUInt('options', 21, 3, 'Options', 0x13),
237
+ helloInterval: this.#helloUInt('helloInterval', 24, 2, 'Hello Interval', 10),
238
+ routerDeadInterval: this.#helloUInt('routerDeadInterval', 26, 2, 'Router Dead Interval', 40),
239
+ designatedRouter: this.#helloRouterId('designatedRouter', 28, 'Designated Router ID'),
240
+ backupDesignatedRouter: this.#helloRouterId('backupDesignatedRouter', 32, 'Backup Designated Router ID'),
241
+ neighbors: {
242
+ type: 'array',
243
+ label: 'Neighbor IDs',
244
+ items: {
245
+ type: 'string',
246
+ minLength: 7,
247
+ maxLength: 15,
248
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.IPv4
249
+ },
250
+ //Neighbour Router IDs run from the end of the fixed Hello fields (offset 36)
251
+ //to the Packet Length, 4 bytes each. Bounded by #bodyEnd so a corrupt length
252
+ //field can't read past the IPv6 payload.
253
+ decode: function () {
254
+ if (!this.#isHello())
255
+ return;
256
+ const end = this.#bodyEnd();
257
+ const neighbors = [];
258
+ for (let offset = 36; offset + 4 <= end; offset += 4)
259
+ neighbors.push((0, BufferToIP_1.BufferToIPv4)(this.readBytes(offset, 4)));
260
+ this.instance.hello.neighbors.setValue(neighbors);
261
+ },
262
+ encode: function () {
263
+ if (!this.#isHello())
264
+ return;
265
+ const neighbors = this.instance.hello.neighbors.getValue([]);
266
+ neighbors.forEach((neighbor, i) => this.writeBytes(36 + i * 4, (0, IPToBuffer_1.IPv4ToBuffer)(neighbor)));
267
+ }
268
+ }
269
+ }
270
+ },
271
+ //==== Raw body for types 2-5 (DD / LS Request / LS Update / LS Ack) ====
272
+ //Kept verbatim, byte-perfect. Bounded by #bodyEnd.
273
+ rawBody: {
274
+ type: 'string',
275
+ label: 'Body',
276
+ minLength: 0,
277
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
278
+ decode: function () {
279
+ if (this.#isHello())
280
+ return;
281
+ const end = this.#bodyEnd();
282
+ if (end <= 16)
283
+ return;
284
+ this.instance.rawBody.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(16, end - 16)));
285
+ },
286
+ encode: function () {
287
+ if (this.#isHello())
288
+ return;
289
+ if (this.instance.rawBody.isUndefined())
290
+ return;
291
+ this.writeBytes(16, Buffer.from(this.instance.rawBody.getValue(''), 'hex'));
292
+ }
293
+ }
294
+ }
295
+ };
296
+ }
297
+ match() {
298
+ if (!this.prevCodecModule)
299
+ return false;
300
+ //OSPFv3 runs over IPv6 (RFC 5340): Next Header 89 from the IPv6 header itself or from AH / an
301
+ //extension header after it, with an IPv6 layer beneath. Version 3 tells it from OSPFv2, which
302
+ //shares the protocol number; a full 16-octet common header must be present.
303
+ const nextHeader = this.prevCodecModule.instance.nxt.getValue(0);
304
+ if (nextHeader !== 89)
305
+ return false;
306
+ const ip = this.nearestIPModule();
307
+ if (!ip || ip.id !== 'ipv6')
308
+ return false;
309
+ if (this.#available() < 16)
310
+ return false;
311
+ return this.readBytes(0, 1, true)[0] === 3;
312
+ }
313
+ }
314
+ exports.OSPFv3 = OSPFv3;
@@ -11,9 +11,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
11
11
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
12
12
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
13
13
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
14
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
15
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
16
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
14
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
15
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
16
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
17
17
  */
18
18
  export declare class PIM extends BaseHeader {
19
19
  #private;
@@ -4,6 +4,9 @@ exports.PIM = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
8
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
7
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
8
11
  /**
9
12
  * PIM — Protocol Independent Multicast, Sparse Mode version 2 (RFC 7761), carried directly over IP as
@@ -15,9 +18,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
15
18
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
16
19
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
17
20
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
18
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
19
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
20
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
22
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
23
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
24
  */
22
25
  class PIM extends BaseHeader_1.BaseHeader {
23
26
  constructor() {
@@ -57,6 +60,27 @@ class PIM extends BaseHeader_1.BaseHeader {
57
60
  }
58
61
  return available < 0 ? 0 : available;
59
62
  }
63
+ /**
64
+ * The PIM checksum (RFC 7761 §4.9): the Internet checksum of the whole PIM message, except that a
65
+ * Register (type 1) covers only its 8-byte header, not the encapsulated data packet. Over IPv6 the
66
+ * IPv6 pseudo-header (next header 103, upper-layer length = the bytes covered) is prepended; over
67
+ * IPv4 there is none. Called once every field is written, with the checksum field still zero.
68
+ * @private
69
+ */
70
+ #calculateChecksum() {
71
+ const message = this.packet.subarray(this.startPos, this.endPos);
72
+ const type = this.instance.type.getValue(0);
73
+ const covered = type === 1 ? message.subarray(0, 8) : message;
74
+ const ip = this.nearestIPModule();
75
+ if (!ip || ip.id !== 'ipv6')
76
+ return (0, Checksum_1.InternetChecksum)(covered);
77
+ const pseudoHeader = this.ipPseudoHeader(103, covered.length);
78
+ if (!pseudoHeader) {
79
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute PIM checksum: the IPv6 addresses beneath are unusable');
80
+ return 0;
81
+ }
82
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, covered);
83
+ }
60
84
  static #buildSchema() {
61
85
  return {
62
86
  type: 'object',
@@ -68,6 +92,7 @@ class PIM extends BaseHeader_1.BaseHeader {
68
92
  label: 'Version',
69
93
  minimum: 0,
70
94
  maximum: 15,
95
+ default: 2,
71
96
  decode: function () {
72
97
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
73
98
  },
@@ -94,9 +119,33 @@ class PIM extends BaseHeader_1.BaseHeader {
94
119
  //Byte 1: Reserved (0 on transmit for most types, a flags/subtype byte for a few). Kept
95
120
  //verbatim and re-emitted so a non-canonical frame is still byte-perfect.
96
121
  reserved: this.fieldHex('reserved', 1, 1, 'Reserved'),
97
- //Bytes 2-3: Checksum (ones-complement over the PIM message). Honored verbatim, never
98
- //recomputed, so a captured message round-trips byte-for-byte.
99
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
122
+ //Bytes 2-3: Checksum, computed once every field is written when left out (#calculateChecksum);
123
+ //a given value is written as given.
124
+ checksum: {
125
+ type: 'integer',
126
+ autoCalculated: true,
127
+ label: 'Checksum',
128
+ minimum: 0,
129
+ maximum: 65535,
130
+ decode: function () {
131
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
132
+ },
133
+ encode: function () {
134
+ if (!this.isLeftOut(this.instance.checksum)) {
135
+ const checksum = this.instance.checksum.getValue(0);
136
+ this.instance.checksum.setValue(checksum);
137
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
138
+ return;
139
+ }
140
+ this.instance.checksum.setValue(0);
141
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
142
+ this.addPostSelfEncodeHandler(() => {
143
+ const checksum = this.#calculateChecksum();
144
+ this.instance.checksum.setValue(checksum);
145
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
146
+ });
147
+ }
148
+ },
100
149
  //The type-specific body after the 4-byte header, kept verbatim. Bounded by the enclosing
101
150
  //IP payload (#available) so trailing FCS / pipelined data is left to the codec's recursion.
102
151
  body: {
@@ -46,6 +46,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
46
46
  //Byte 0 high nibble: PPPoE Version (1). Written MSB-first; shares byte 0 with Type
47
47
  //(writeBits masks each field so they never clobber).
48
48
  version: {
49
+ default: 1,
49
50
  type: 'integer', label: 'Version', minimum: 0, maximum: 15,
50
51
  decode: function () {
51
52
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
@@ -58,6 +59,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
58
59
  },
59
60
  //Byte 0 low nibble: PPPoE Type (1).
60
61
  type: {
62
+ default: 1,
61
63
  type: 'integer', label: 'Type', minimum: 0, maximum: 15,
62
64
  decode: function () {
63
65
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
@@ -78,8 +78,8 @@ class PPTP extends BaseHeader_1.BaseHeader {
78
78
  //messages, the only PDU type carried on the TCP control connection.
79
79
  pduType: this.fieldUInt('pduType', 2, 2, 'PDU Type'),
80
80
  //The Magic Cookie constant 0x1A2B3C4D. Kept verbatim (honored, not re-derived) so a
81
- //crafted/desynced cookie still round-trips.
82
- magicCookie: this.fieldHex('magicCookie', 4, 4, 'Magic Cookie'),
81
+ //crafted/desynced cookie still round-trips. Default 0x1A2B3C4D: what match() requires.
82
+ magicCookie: { ...this.fieldHex('magicCookie', 4, 4, 'Magic Cookie'), default: '1a2b3c4d' },
83
83
  controlMessageType: this.fieldUInt('controlMessageType', 8, 2, 'Control Message Type'),
84
84
  //Reserved (MUST be 0 per RFC 2637); preserved and re-emitted untouched.
85
85
  reserved0: this.fieldHex('reserved0', 10, 2, 'Reserved0'),
@@ -106,7 +106,9 @@ class PTP extends BaseHeader_1.BaseHeader {
106
106
  type: 'integer',
107
107
  autoCalculated: true,
108
108
  label: 'Message Length',
109
- minimum: 34,
109
+ //The field floor, not the 34-byte header: a given value is written as given
110
+ //(flagged when too short) so a malformed message can be crafted.
111
+ minimum: 0,
110
112
  maximum: 65535,
111
113
  decode: function () {
112
114
  this.instance.messageLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
@@ -122,6 +124,8 @@ class PTP extends BaseHeader_1.BaseHeader {
122
124
  this.recordError(this.instance.messageLength.getPath(), 'Maximum value is 65535');
123
125
  value = 65535;
124
126
  }
127
+ if (value < 34)
128
+ this.recordError(this.instance.messageLength.getPath(), 'Message length should be at least 34');
125
129
  this.instance.messageLength.setValue(value);
126
130
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
127
131
  }
@@ -67,16 +67,20 @@ class QUIC extends BaseHeader_1.BaseHeader {
67
67
  #tailOffset() {
68
68
  return 7 + this.instance.dcidLength.getValue(0) + this.instance.scidLength.getValue(0);
69
69
  }
70
- /** A single flag bit within the first byte (bit 0 = MSB). */
70
+ /**
71
+ * A single flag bit within the first byte (bit 0 = MSB). Default set: the Header Form and Fixed Bit
72
+ * are both 1 in the long header match() requires.
73
+ */
71
74
  static #flagBit(name, bitOffset, label) {
72
75
  return {
73
76
  type: 'boolean',
74
77
  label: label,
78
+ default: true,
75
79
  decode: function () {
76
80
  this.instance.firstByte[name].setValue(!!this.readBits(0, 1, bitOffset, 1));
77
81
  },
78
82
  encode: function () {
79
- const value = !!this.instance.firstByte[name].getValue();
83
+ const value = !!this.instance.firstByte[name].getValue(true);
80
84
  this.instance.firstByte[name].setValue(value);
81
85
  this.writeBits(0, 1, bitOffset, 1, value ? 1 : 0);
82
86
  }
@@ -127,7 +131,8 @@ class QUIC extends BaseHeader_1.BaseHeader {
127
131
  //Version — 0x00000001 = QUIC v1 (RFC 9000), 0x6b3343cf = v2 (RFC 9369), 0xff0000xx = IETF
128
132
  //drafts, 0x00000000 = Version Negotiation. Kept as a lower-case hex string so any value
129
133
  //round-trips verbatim and the field is unbounded (the match gate restricts real selection).
130
- version: this.fieldHex('version', 1, 4, 'Version'),
134
+ //Default QUIC v1, a version match() accepts.
135
+ version: { ...this.fieldHex('version', 1, 4, 'Version'), default: '00000001' },
131
136
  //Destination Connection ID length (0..20 per RFC 9000, but any 0..255 round-trips). Honored
132
137
  //when supplied, else derived from the DCID bytes.
133
138
  dcidLength: {
@@ -44,6 +44,8 @@ class RDP extends BaseHeader_1.BaseHeader {
44
44
  flags: this.fieldUInt('flags', 1, 1, 'Flags'),
45
45
  //2-byte LITTLE-ENDIAN length — always 8 for the Negotiation. Honored verbatim.
46
46
  length: {
47
+ //Always 8 for the Negotiation structure; left out, encode writes 8.
48
+ autoCalculated: true,
47
49
  type: 'integer', label: 'Length', minimum: 0, maximum: 65535,
48
50
  decode: function () {
49
51
  const bytes = this.readBytes(2, 2);
@@ -106,7 +106,8 @@ class RGoose extends BaseHeader_1.BaseHeader {
106
106
  type: 'object',
107
107
  summary: 'R-GOOSE/R-SV spdu=${spduNumber} si=${si}',
108
108
  properties: {
109
- si: this.fieldUInt('si', 0, 1, 'Session Identifier'),
109
+ //Default 0xA1 (non-tunnelled GOOSE): one of the Session Identifiers match() requires.
110
+ si: { ...this.fieldUInt('si', 0, 1, 'Session Identifier'), default: 0xa1 },
110
111
  li: {
111
112
  autoCalculated: true,
112
113
  type: 'integer', label: 'Session Header Length', minimum: 0, maximum: 255,
@@ -121,6 +122,7 @@ class RGoose extends BaseHeader_1.BaseHeader {
121
122
  }
122
123
  },
123
124
  commonHeaderId: {
125
+ default: 0x80,
124
126
  type: 'integer', label: 'Common Session Header Id', minimum: 0, maximum: 255,
125
127
  decode: function () { this.instance.commonHeaderId.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(2, 1))); },
126
128
  encode: function () {
@@ -166,6 +168,7 @@ class RGoose extends BaseHeader_1.BaseHeader {
166
168
  },
167
169
  spduNumber: this.fieldUInt('spduNumber', 8, 4, 'SPDU Number'),
168
170
  version: {
171
+ default: 1,
169
172
  type: 'integer', label: 'Version', minimum: 0, maximum: 65535,
170
173
  decode: function () { this.instance.version.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2))); },
171
174
  encode: function () {
@@ -73,8 +73,9 @@ class RIP extends BaseHeader_1.BaseHeader {
73
73
  type: 'object',
74
74
  summary: 'RIPv2 command ${command}',
75
75
  properties: {
76
- command: this.fieldUInt('command', 0, 1, 'Command'),
77
- version: this.fieldUInt('version', 1, 1, 'Version'),
76
+ //Defaults command 1 (Request) and version 2 (RIPv2): the signature match() requires.
77
+ command: { ...this.fieldUInt('command', 0, 1, 'Command'), default: 1 },
78
+ version: { ...this.fieldUInt('version', 1, 1, 'Version'), default: 2 },
78
79
  reserved: this.fieldUInt('reserved', 2, 2, 'Reserved'),
79
80
  entries: {
80
81
  type: 'array',
@@ -50,8 +50,9 @@ class RIPng extends BaseHeader_1.BaseHeader {
50
50
  summary: 'RIPng cmd=${command} ${rtes.length} RTEs',
51
51
  properties: {
52
52
  //Fixed 4-byte header: Command (1 Request / 2 Response), Version (= 1), 2-byte reserved.
53
- command: this.fieldUInt('command', 0, 1, 'Command'),
54
- version: this.fieldUInt('version', 1, 1, 'Version'),
53
+ //Defaults command 1 (Request) and version 1: the signature match() requires.
54
+ command: { ...this.fieldUInt('command', 0, 1, 'Command'), default: 1 },
55
+ version: { ...this.fieldUInt('version', 1, 1, 'Version'), default: 1 },
55
56
  //Reserved (2 bytes, must be zero per RFC 2080 but kept verbatim so a non-zero value in a
56
57
  //crafted/non-conformant message still round-trips byte-for-byte).
57
58
  reserved: this.fieldUInt('reserved', 2, 2, 'Reserved'),
@@ -12,9 +12,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
12
12
  *
13
13
  * The object contents are class/C-Type specific and several (SESSION, SENDER_TSPEC, FLOWSPEC, …) need
14
14
  * cross-object and policy context, so this codec keeps each object's contents verbatim as `data` hex
15
- * (byte-perfect) and does not sub-decode them. The RSVP Length is honored verbatim when supplied on
16
- * encode (a crafted message may lie) and derived from the objects otherwise; each object's Length is
17
- * likewise honored-else-derived. The Checksum is honored verbatim, never recomputed. Object walking is
15
+ * (byte-perfect) and does not sub-decode them. The RSVP Length, each object's Length and the Checksum
16
+ * are computed on encode when left out (the Length and Checksum over the bytes actually written), and
17
+ * written as given otherwise, 0 included (a crafted message may lie). Object walking is
18
18
  * doubly bounded — by the RSVP Length field and by the bytes the IP layer actually made available — so a
19
19
  * lying length can't read past the datagram, and trailing / pipelined bytes are left to the codec's
20
20
  * recursion / RawData. A well-formed message round-trips byte-for-byte.