@netkitty/codec 2.0.0 → 2.1.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 (87) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/README.md +10 -6
  3. package/README.zh-CN.md +9 -6
  4. package/dist/Codec.js +4 -1
  5. package/dist/abstracts/BaseHeader.d.ts +26 -0
  6. package/dist/abstracts/BaseHeader.js +53 -0
  7. package/dist/headers/AH.js +3 -2
  8. package/dist/headers/BFD.d.ts +2 -1
  9. package/dist/headers/BFD.js +30 -4
  10. package/dist/headers/BGP.js +5 -1
  11. package/dist/headers/C37118.d.ts +3 -2
  12. package/dist/headers/C37118.js +34 -15
  13. package/dist/headers/CDP.d.ts +3 -3
  14. package/dist/headers/CDP.js +46 -10
  15. package/dist/headers/CMS.js +25 -10
  16. package/dist/headers/COTP.js +2 -3
  17. package/dist/headers/CoAP.js +20 -2
  18. package/dist/headers/DCCP.d.ts +3 -2
  19. package/dist/headers/DCCP.js +60 -8
  20. package/dist/headers/DNP3.d.ts +5 -3
  21. package/dist/headers/DNP3.js +62 -7
  22. package/dist/headers/EIGRP.d.ts +3 -2
  23. package/dist/headers/EIGRP.js +31 -5
  24. package/dist/headers/EthernetII.js +1 -0
  25. package/dist/headers/GELF.js +1 -0
  26. package/dist/headers/GENEVE.js +1 -0
  27. package/dist/headers/GRE.js +1 -0
  28. package/dist/headers/GTPv2C.js +1 -0
  29. package/dist/headers/Goose.js +4 -4
  30. package/dist/headers/HSR.js +1 -0
  31. package/dist/headers/HTTP2.js +1 -0
  32. package/dist/headers/ICMP.js +2 -1
  33. package/dist/headers/ICMPv6.js +10 -16
  34. package/dist/headers/IEC104_I_Frame.js +189 -160
  35. package/dist/headers/IEC104_S_Frame.js +19 -1
  36. package/dist/headers/IEC104_U_Frame.js +19 -1
  37. package/dist/headers/IEC61850SampledValues.js +4 -4
  38. package/dist/headers/IGMP.d.ts +8 -7
  39. package/dist/headers/IGMP.js +57 -19
  40. package/dist/headers/IPFIX.js +1 -1
  41. package/dist/headers/IPv4.js +26 -23
  42. package/dist/headers/IPv6.js +4 -1
  43. package/dist/headers/IPv6HopByHopOptions.js +3 -1
  44. package/dist/headers/ISAKMP.js +1 -0
  45. package/dist/headers/Kerberos.js +1 -0
  46. package/dist/headers/L2TP.js +1 -0
  47. package/dist/headers/LDAP.js +1 -1
  48. package/dist/headers/LLC.js +1 -0
  49. package/dist/headers/NHRP.d.ts +1 -1
  50. package/dist/headers/NHRP.js +29 -4
  51. package/dist/headers/OSPF.d.ts +3 -3
  52. package/dist/headers/OSPF.js +72 -7
  53. package/dist/headers/PIM.d.ts +3 -3
  54. package/dist/headers/PIM.js +55 -6
  55. package/dist/headers/PPPoEDiscovery.js +2 -0
  56. package/dist/headers/PTP.js +5 -1
  57. package/dist/headers/RDP.js +2 -0
  58. package/dist/headers/RGoose.js +2 -0
  59. package/dist/headers/RSVP.d.ts +3 -3
  60. package/dist/headers/RSVP.js +56 -42
  61. package/dist/headers/RTCP.js +1 -0
  62. package/dist/headers/SCTP.d.ts +2 -2
  63. package/dist/headers/SCTP.js +29 -5
  64. package/dist/headers/SNAP.js +2 -2
  65. package/dist/headers/SNMP.js +2 -2
  66. package/dist/headers/STUN.js +1 -1
  67. package/dist/headers/SercosIII.d.ts +3 -2
  68. package/dist/headers/SercosIII.js +41 -5
  69. package/dist/headers/TCP.js +13 -58
  70. package/dist/headers/TLS_Alert.js +20 -2
  71. package/dist/headers/TLS_ApplicationData.d.ts +3 -3
  72. package/dist/headers/TLS_ApplicationData.js +25 -6
  73. package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
  74. package/dist/headers/TLS_ChangeCipherSpec.js +24 -4
  75. package/dist/headers/TLS_Handshake.js +39 -6
  76. package/dist/headers/TLS_Heartbeat.d.ts +4 -3
  77. package/dist/headers/TLS_Heartbeat.js +40 -7
  78. package/dist/headers/TPKT.js +1 -1
  79. package/dist/headers/UDP.js +17 -61
  80. package/dist/headers/VLAN_802dot1Q.js +1 -0
  81. package/dist/headers/VRRP.d.ts +2 -1
  82. package/dist/headers/VRRP.js +45 -9
  83. package/dist/helper/Checksum.d.ts +15 -0
  84. package/dist/helper/Checksum.js +56 -0
  85. package/dist/helper/Crc.d.ts +18 -0
  86. package/dist/helper/Crc.js +58 -0
  87. package/package.json +2 -2
@@ -5,6 +5,7 @@ const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
8
+ const Crc_1 = require("../helper/Crc");
8
9
  /**
9
10
  * Sercos III — the real-time industrial Ethernet of the Sercos automation bus (IEC 61491 / IEC 61158,
10
11
  * IEC 61784 CPF-16), carried directly in an Ethernet II frame with EtherType 0x88CD (an Ethernet child —
@@ -24,8 +25,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
24
25
  * Byte 1 — Phase field (the Master Sync Telegram, MST):
25
26
  * - the Phase occupies mask 0x8f (bit 7 = phase switching, bits 0..3 = communication phase CP0..CP4),
26
27
  * a non-contiguous field kept as one value; the Cycle Count occupies mask 0x70 (bits 4..6).
27
- * Bytes 2..5 — CRC32 of the MST, kept verbatim (little-endian on the wire per the dissector) and never
28
- * recomputed, so a captured frame round-trips byte-for-byte.
28
+ * Bytes 2..5 — CRC32 of the MST, little-endian on the wire (per the dissector): CRC-32 (IEEE 802.3) over
29
+ * the Ethernet header (destination, source, EtherType) and bytes 0..1. Written as given; computed only
30
+ * when left out (checked against a real capture: the openSAFETY-over-Sercos III Wireshark sample).
29
31
  *
30
32
  * Everything after the 6-byte header (the phase-dependent per-device connection / service-channel data) is
31
33
  * command- and topology-dependent, so this slice keeps it verbatim as `data` hex (byte-perfect), bounded by
@@ -50,6 +52,16 @@ class SercosIII extends BaseHeader_1.BaseHeader {
50
52
  get SCHEMA() {
51
53
  return (SercosIII.#schemaCache ??= SercosIII.#buildSchema());
52
54
  }
55
+ /**
56
+ * The octets the MST CRC32 covers: the 14-byte Ethernet header beneath (destination, source, EtherType)
57
+ * followed by the Telegram Type and Phase octets. undefined when this layer does not sit directly on
58
+ * an Ethernet header.
59
+ */
60
+ #mstCrcCoverage() {
61
+ if (!this.prevCodecModule || this.prevCodecModule.id !== 'eth' || this.startPos < 14)
62
+ return undefined;
63
+ return this.packet.subarray(this.startPos - 14, this.startPos + 2);
64
+ }
53
65
  static #buildSchema() {
54
66
  return {
55
67
  type: 'object',
@@ -161,9 +173,33 @@ class SercosIII extends BaseHeader_1.BaseHeader {
161
173
  }
162
174
  },
163
175
  //==== Bytes 2..5: MST CRC32 ====
164
- //Honored verbatim (little-endian on the wire) and never recomputed, so a captured frame
165
- //round-trips byte-for-byte.
166
- crc32: this.fieldHex('crc32', 2, 4, 'CRC32'),
176
+ //Little-endian on the wire. Bytes 0..1 are already written when this runs, so a left-out
177
+ //CRC is computed right here; a given one (any value, a wrong one included) is written as is.
178
+ crc32: {
179
+ type: 'string',
180
+ autoCalculated: true,
181
+ label: 'CRC32',
182
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
183
+ decode: function () {
184
+ this.instance.crc32.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4)));
185
+ },
186
+ encode: function () {
187
+ if (!this.isLeftOut(this.instance.crc32)) {
188
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.crc32.getValue()));
189
+ return;
190
+ }
191
+ const crc = Buffer.alloc(4);
192
+ const covered = this.#mstCrcCoverage();
193
+ if (covered) {
194
+ crc.writeUInt32LE((0, Crc_1.Crc32)(covered), 0);
195
+ }
196
+ else {
197
+ this.recordError(this.instance.crc32.getPath(), 'Cannot compute CRC32: no Ethernet header beneath Sercos III');
198
+ }
199
+ this.instance.crc32.setValue((0, BufferToHex_1.BufferToHex)(crc));
200
+ this.writeBytes(2, crc);
201
+ }
202
+ },
167
203
  //==== Bytes 6..end: per-device payload ====
168
204
  //Kept verbatim (byte-perfect). Sercos III has no length field of its own — it runs to the end
169
205
  //of the Ethernet payload (like LLDP/GOOSE), so the body is bounded by the captured frame.
@@ -4,7 +4,7 @@ exports.TCP = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToNumber_1 = require("../helper/BufferToNumber");
6
6
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
7
- const IPToBuffer_1 = require("../helper/IPToBuffer");
7
+ const Checksum_1 = require("../helper/Checksum");
8
8
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
9
9
  const NumberToHex_1 = require("../helper/NumberToHex");
10
10
  const HexToNumber_1 = require("../helper/HexToNumber");
@@ -39,59 +39,14 @@ class TCP extends BaseHeader_1.BaseHeader {
39
39
  * @protected
40
40
  */
41
41
  calculateTCPChecksum(tcpHeaderBuffer) {
42
- const tcpHeaderLength = tcpHeaderBuffer.length;
43
- //The checksum needs the IP pseudo-header from the layer below. Building a malformed
44
- //stack (e.g. TCP with no IP beneath) is a legitimate use case, so record an error and
45
- //skip the checksum rather than dereferencing a missing previous layer and throwing.
46
- if (!this.prevCodecModule) {
42
+ //The pseudo-header comes from the nearest IPv4/IPv6 layer beneath, past AH or IPv6 extension
43
+ //headers. A TCP layer with no IP beneath is a legitimate crafted stack: record it and leave 0.
44
+ const pseudoHeader = this.ipPseudoHeader(6, tcpHeaderBuffer.length);
45
+ if (!pseudoHeader) {
47
46
  this.recordError(this.instance.checksum.getPath(), 'Cannot compute TCP checksum: no IP layer beneath TCP');
48
47
  return 0;
49
48
  }
50
- const ipVersion = this.prevCodecModule.instance.version.getValue();
51
- let pseudoHeaderBuffer = Buffer.from([]);
52
- const sourceIp = this.prevCodecModule.instance.sip.getValue();
53
- const destinationIp = this.prevCodecModule.instance.dip.getValue();
54
- if (ipVersion === 4) {
55
- //4 Bytes
56
- const sourceIPv4Buffer = Buffer.from(sourceIp.split('.').map((value) => parseInt(value)));
57
- const destinationIPv4Buffer = Buffer.from(destinationIp.split('.').map((value) => parseInt(value)));
58
- //IPv4 Pseudo header
59
- pseudoHeaderBuffer = Buffer.concat([
60
- sourceIPv4Buffer,
61
- destinationIPv4Buffer,
62
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
63
- (0, NumberToBuffer_1.UInt8ToBuffer)(6), //Protocol type (TCP = 6)
64
- Buffer.from([(tcpHeaderLength >> 8) & 0xFF]),
65
- Buffer.from([tcpHeaderLength & 0xFF])
66
- ]);
67
- }
68
- else if (ipVersion === 6) {
69
- //16 Bytes
70
- const sourceIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(sourceIp);
71
- const destinationIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(destinationIp);
72
- //IPv6 Pseudo header
73
- pseudoHeaderBuffer = Buffer.concat([
74
- sourceIPv6Buffer,
75
- destinationIPv6Buffer,
76
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
77
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
78
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
79
- (0, NumberToBuffer_1.UInt8ToBuffer)(6), //Protocol type (TCP = 6)
80
- Buffer.from([(tcpHeaderLength >> 8) & 0xFF]),
81
- Buffer.from([tcpHeaderLength & 0xFF])
82
- ]);
83
- }
84
- else {
85
- return 0;
86
- }
87
- const dataBuffer = Buffer.concat([pseudoHeaderBuffer, tcpHeaderBuffer]);
88
- const data = Uint8Array.from(dataBuffer);
89
- let sum = 0;
90
- for (let i = 0; i < data.length; i += 2)
91
- sum += (data[i] << 8) + (data[i + 1] || 0);
92
- while (sum >>> 16)
93
- sum = (sum & 0xFFFF) + (sum >>> 16);
94
- return (~sum) & 0xFFFF;
49
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, tcpHeaderBuffer);
95
50
  }
96
51
  static #schemaCache;
97
52
  //Class-cached SCHEMA (④): field closures are functions taking dynamic `this` via .call(this).
@@ -161,10 +116,10 @@ class TCP extends BaseHeader_1.BaseHeader {
161
116
  this.instance.hdrLen.setValue(this.readBits(12, 1, 0, 4) * 4);
162
117
  },
163
118
  encode: function () {
164
- const hdrLen = this.instance.hdrLen.getValue(0);
165
- if (hdrLen) {
119
+ if (!this.isLeftOut(this.instance.hdrLen)) {
120
+ const hdrLen = this.instance.hdrLen.getValue(0);
166
121
  this.instance.hdrLen.setValue(hdrLen);
167
- this.writeBits(12, 1, 0, 4, hdrLen ? Math.floor(hdrLen / 4) : 0);
122
+ this.writeBits(12, 1, 0, 4, Math.floor(hdrLen / 4));
168
123
  }
169
124
  else {
170
125
  this.addPostSelfEncodeHandler(() => {
@@ -342,14 +297,14 @@ class TCP extends BaseHeader_1.BaseHeader {
342
297
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(16, 2)));
343
298
  },
344
299
  encode: function () {
345
- const checksum = this.instance.checksum.getValue(0);
346
- if (checksum) {
300
+ if (!this.isLeftOut(this.instance.checksum)) {
301
+ const checksum = this.instance.checksum.getValue(0);
347
302
  this.instance.checksum.setValue(checksum);
348
303
  this.writeBytes(16, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
349
304
  }
350
305
  else {
351
- this.instance.checksum.setValue(checksum);
352
- this.writeBytes(16, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
306
+ this.instance.checksum.setValue(0);
307
+ this.writeBytes(16, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
353
308
  this.addPostPacketEncodeHandler(() => {
354
309
  //Calculate checksum after packet encoded
355
310
  let startPos = 0;
@@ -68,11 +68,13 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
68
68
  this.SCHEMA = {
69
69
  type: 'object',
70
70
  properties: {
71
+ //Default 21 (alert): the content type match() requires.
71
72
  contentType: {
72
73
  type: 'integer',
73
74
  label: 'Content Type',
74
75
  minimum: 0,
75
76
  maximum: 65535,
77
+ default: 21,
76
78
  decode: () => {
77
79
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
78
80
  },
@@ -86,6 +88,8 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
86
88
  label: 'Legacy Version',
87
89
  //Closed set: decode stores one of these names, or '0' for an unrecognized version.
88
90
  enum: [...Object.values(TLSver), '0'],
91
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
92
+ default: TLSver.TLS_1_2,
89
93
  minimum: 0,
90
94
  maximum: 65535,
91
95
  decode: () => {
@@ -166,6 +170,7 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
166
170
  },
167
171
  length: {
168
172
  type: 'integer',
173
+ autoCalculated: true,
169
174
  label: 'Length',
170
175
  minimum: 0,
171
176
  maximum: 65535,
@@ -173,8 +178,21 @@ class TLS_Alert extends BaseHeader_1.BaseHeader {
173
178
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
174
179
  },
175
180
  encode: () => {
176
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
177
- this.writeBytes(3, length);
181
+ if (!this.isLeftOut(this.instance.length)) {
182
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
183
+ return;
184
+ }
185
+ //The record payload: every octet this layer writes after the 5-octet record header.
186
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
187
+ this.addPostSelfEncodeHandler(() => {
188
+ let length = this.length - 5;
189
+ if (length > 65535) {
190
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
191
+ length = 65535;
192
+ }
193
+ this.instance.length.setValue(length);
194
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
195
+ });
178
196
  }
179
197
  },
180
198
  levelType: {
@@ -9,9 +9,9 @@ import { BaseHeader } from '../abstracts/BaseHeader';
9
9
  * `length` octets).
10
10
  *
11
11
  * The body is encrypted and opaque, so it is kept verbatim as `appData` hex (byte-perfect) and never
12
- * sub-decoded; the Length is honored as given. A well-formed record round-trips byte-for-byte. In the
13
- * heuristic chain (`heuristicFallback`), match() requires a Content Type of 0x17 and a recognized legacy
14
- * Version (0x0300-0x0304).
12
+ * sub-decoded; the Length is written as given, and left out it is the body's octet count. A well-formed
13
+ * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
14
+ * Content Type of 0x17 and a recognized legacy Version (0x0300-0x0304).
15
15
  */
16
16
  export declare class TLS_ApplicationData extends BaseHeader {
17
17
  SCHEMA: ProtocolJSONSchema;
@@ -23,9 +23,9 @@ var TLSver;
23
23
  * `length` octets).
24
24
  *
25
25
  * The body is encrypted and opaque, so it is kept verbatim as `appData` hex (byte-perfect) and never
26
- * sub-decoded; the Length is honored as given. A well-formed record round-trips byte-for-byte. In the
27
- * heuristic chain (`heuristicFallback`), match() requires a Content Type of 0x17 and a recognized legacy
28
- * Version (0x0300-0x0304).
26
+ * sub-decoded; the Length is written as given, and left out it is the body's octet count. A well-formed
27
+ * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
28
+ * Content Type of 0x17 and a recognized legacy Version (0x0300-0x0304).
29
29
  */
30
30
  class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
31
31
  constructor() {
@@ -33,11 +33,13 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
33
33
  this.SCHEMA = {
34
34
  type: 'object',
35
35
  properties: {
36
+ //Default 23 (application data): the content type match() requires.
36
37
  contentType: {
37
38
  type: 'integer',
38
39
  label: 'Content Type',
39
40
  minimum: 0,
40
41
  maximum: 255,
42
+ default: 23,
41
43
  decode: () => {
42
44
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
43
45
  },
@@ -51,6 +53,8 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
51
53
  label: 'Legacy Version',
52
54
  //Closed set: decode stores one of these names, or '0' for an unrecognized version.
53
55
  enum: [...Object.values(TLSver), '0'],
56
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
57
+ default: TLSver.TLS_1_2,
54
58
  minimum: 0,
55
59
  maximum: 65535,
56
60
  decode: () => {
@@ -130,6 +134,7 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
130
134
  },
131
135
  length: {
132
136
  type: 'integer',
137
+ autoCalculated: true,
133
138
  label: 'Length',
134
139
  minimum: 0,
135
140
  maximum: 65535,
@@ -137,8 +142,21 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
137
142
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
138
143
  },
139
144
  encode: () => {
140
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
141
- this.writeBytes(3, length);
145
+ if (!this.isLeftOut(this.instance.length)) {
146
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
147
+ return;
148
+ }
149
+ //The record payload: every octet this layer writes after the 5-octet record header.
150
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
151
+ this.addPostSelfEncodeHandler(() => {
152
+ let length = this.length - 5;
153
+ if (length > 65535) {
154
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
155
+ length = 65535;
156
+ }
157
+ this.instance.length.setValue(length);
158
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
159
+ });
142
160
  }
143
161
  },
144
162
  appData: {
@@ -150,7 +168,8 @@ class TLS_ApplicationData extends BaseHeader_1.BaseHeader {
150
168
  this.instance.appData.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(5, this.instance.length.getValue())));
151
169
  },
152
170
  encode: () => {
153
- const appData = (0, HexToBuffer_1.HexToBuffer)(this.instance.appData.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found')));
171
+ //Left out, the record carries no data (an empty record, Length 0 when computed).
172
+ const appData = (0, HexToBuffer_1.HexToBuffer)(this.instance.appData.getValue('', (nodePath) => this.recordError(nodePath, 'Not Found')));
154
173
  this.writeBytes(5, appData);
155
174
  }
156
175
  }
@@ -8,8 +8,8 @@ import { BaseHeader } from '../abstracts/BaseHeader';
8
8
  * else "0"), a 16-bit record Length (`length`, offset 3), and the message body
9
9
  * (`change_cipher_spec_message`, offset 5, `length` octets — canonically the single byte 0x01).
10
10
  *
11
- * The body is kept verbatim as hex (byte-perfect) and the Length is honored as given, so a well-formed
12
- * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
11
+ * The body is kept verbatim as hex (byte-perfect) and the Length is written as given (left out, it is the
12
+ * body's octet count), so a well-formed record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
13
13
  * Content Type of 0x14 and a recognized legacy Version (0x0300-0x0304).
14
14
  */
15
15
  export declare class TLS_ChangeCipherSpec extends BaseHeader {
@@ -22,8 +22,8 @@ var TLSver;
22
22
  * else "0"), a 16-bit record Length (`length`, offset 3), and the message body
23
23
  * (`change_cipher_spec_message`, offset 5, `length` octets — canonically the single byte 0x01).
24
24
  *
25
- * The body is kept verbatim as hex (byte-perfect) and the Length is honored as given, so a well-formed
26
- * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
25
+ * The body is kept verbatim as hex (byte-perfect) and the Length is written as given (left out, it is the
26
+ * body's octet count), so a well-formed record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
27
27
  * Content Type of 0x14 and a recognized legacy Version (0x0300-0x0304).
28
28
  */
29
29
  class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
@@ -32,11 +32,13 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
32
32
  this.SCHEMA = {
33
33
  type: 'object',
34
34
  properties: {
35
+ //Default 20 (change cipher spec): the content type match() requires.
35
36
  contentType: {
36
37
  type: 'integer',
37
38
  label: 'Content Type',
38
39
  minimum: 0,
39
40
  maximum: 255,
41
+ default: 20,
40
42
  decode: () => {
41
43
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
42
44
  },
@@ -50,6 +52,8 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
50
52
  label: 'Legacy Version',
51
53
  //Closed set: decode stores one of these names, or '0' for an unrecognized version.
52
54
  enum: [...Object.values(TLSver), '0'],
55
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
56
+ default: TLSver.TLS_1_2,
53
57
  minimum: 0,
54
58
  maximum: 65535,
55
59
  decode: () => {
@@ -129,6 +133,7 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
129
133
  },
130
134
  length: {
131
135
  type: 'integer',
136
+ autoCalculated: true,
132
137
  label: 'Length',
133
138
  minimum: 0,
134
139
  maximum: 65535,
@@ -136,13 +141,28 @@ class TLS_ChangeCipherSpec extends BaseHeader_1.BaseHeader {
136
141
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
137
142
  },
138
143
  encode: () => {
139
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
140
- this.writeBytes(3, length);
144
+ if (!this.isLeftOut(this.instance.length)) {
145
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
146
+ return;
147
+ }
148
+ //The record payload: every octet this layer writes after the 5-octet record header.
149
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
150
+ this.addPostSelfEncodeHandler(() => {
151
+ let length = this.length - 5;
152
+ if (length > 65535) {
153
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
154
+ length = 65535;
155
+ }
156
+ this.instance.length.setValue(length);
157
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
158
+ });
141
159
  }
142
160
  },
143
161
  change_cipher_spec_message: {
144
162
  type: 'string',
145
163
  label: 'Change Cipher Spec Message',
164
+ //Default 0x01: the one-octet change_cipher_spec message (RFC 5246 section 7.1).
165
+ default: '01',
146
166
  decode: () => {
147
167
  const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
148
168
  this.instance.change_cipher_spec_message.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(5, length)));
@@ -35,11 +35,13 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
35
35
  this.SCHEMA = {
36
36
  type: 'object',
37
37
  properties: {
38
+ //Default 22 (handshake): the content type match() requires.
38
39
  contentType: {
39
40
  type: 'integer',
40
41
  label: 'Content Type',
41
42
  minimum: 0,
42
43
  maximum: 255,
44
+ default: 22,
43
45
  decode: () => {
44
46
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToInt8)(this.readBytes(0, 1)));
45
47
  },
@@ -53,6 +55,8 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
53
55
  label: 'Legacy Version',
54
56
  //Closed set: decode stores one of these names, or '0' for an unrecognized version.
55
57
  enum: [...Object.values(TLSver), '0'],
58
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
59
+ default: TLSver.TLS_1_2,
56
60
  minimum: 0,
57
61
  maximum: 65535,
58
62
  decode: () => {
@@ -132,6 +136,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
132
136
  },
133
137
  length: {
134
138
  type: 'integer',
139
+ autoCalculated: true,
135
140
  label: 'Length',
136
141
  minimum: 0,
137
142
  maximum: 65535,
@@ -139,8 +144,21 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
139
144
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
140
145
  },
141
146
  encode: () => {
142
- const length = (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
143
- this.writeBytes(3, length);
147
+ if (!this.isLeftOut(this.instance.length)) {
148
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
149
+ return;
150
+ }
151
+ //The record payload: every octet this layer writes after the 5-octet record header.
152
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
153
+ this.addPostSelfEncodeHandler(() => {
154
+ let length = this.length - 5;
155
+ if (length > 65535) {
156
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
157
+ length = 65535;
158
+ }
159
+ this.instance.length.setValue(length);
160
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
161
+ });
144
162
  }
145
163
  },
146
164
  handshakeType: {
@@ -302,8 +320,12 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
302
320
  }
303
321
  }
304
322
  },
323
+ //The handshake message length. A fragment's may exceed the body this record carries (the
324
+ //message continues in later records), so it is written as given; left out, it is the body
325
+ //this layer writes after the 4-octet handshake header.
305
326
  handshakeLength: {
306
327
  type: 'integer',
328
+ autoCalculated: true,
307
329
  label: 'Length',
308
330
  minimum: 0,
309
331
  maximum: 16777215,
@@ -311,10 +333,21 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
311
333
  this.instance.handshakeLength.setValue((0, BufferToNumber_1.BufferToUInt32)(this.readBytes(6, 3)));
312
334
  },
313
335
  encode: () => {
314
- const length1 = (this.instance.handshakeLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
315
- const length2 = length1.toString(16).padStart(6, '0');
316
- const length3 = (0, HexToBuffer_1.HexToBuffer)(length2);
317
- this.writeBytes(6, length3);
336
+ const write = (length) => this.writeBytes(6, (0, HexToBuffer_1.HexToBuffer)(length.toString(16).padStart(6, '0')));
337
+ if (!this.isLeftOut(this.instance.handshakeLength)) {
338
+ write(this.instance.handshakeLength.getValue(0));
339
+ return;
340
+ }
341
+ write(0);
342
+ this.addPostSelfEncodeHandler(() => {
343
+ let length = this.length - 9;
344
+ if (length > 16777215) {
345
+ this.recordError(this.instance.handshakeLength.getPath(), 'Maximum value is 16777215');
346
+ length = 16777215;
347
+ }
348
+ this.instance.handshakeLength.setValue(length);
349
+ write(length);
350
+ });
318
351
  }
319
352
  },
320
353
  messagedata: {
@@ -12,9 +12,10 @@ import { BaseHeader } from '../abstracts/BaseHeader';
12
12
  *
13
13
  * Per RFC 6520 §4 the padding is at least 16 octets and fills the record after the type and payload, so its
14
14
  * length is derived as recordLength - 3 - payloadLength and preserved for a byte-exact re-encode. The
15
- * payload and padding are kept verbatim and lengths are honored as given, so a well-formed record
16
- * round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a Content Type
17
- * of 0x18 and a recognized legacy Version (0x0300-0x0304).
15
+ * payload and padding are kept verbatim and lengths are written as given (left out, the record Length is
16
+ * the octets after the record header and the Payload Length the payload's octet count), so a well-formed
17
+ * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
18
+ * Content Type of 0x18 and a recognized legacy Version (0x0300-0x0304).
18
19
  */
19
20
  export declare class TLS_Heartbeat extends BaseHeader {
20
21
  SCHEMA: ProtocolJSONSchema;
@@ -26,9 +26,10 @@ var TLSver;
26
26
  *
27
27
  * Per RFC 6520 §4 the padding is at least 16 octets and fills the record after the type and payload, so its
28
28
  * length is derived as recordLength - 3 - payloadLength and preserved for a byte-exact re-encode. The
29
- * payload and padding are kept verbatim and lengths are honored as given, so a well-formed record
30
- * round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a Content Type
31
- * of 0x18 and a recognized legacy Version (0x0300-0x0304).
29
+ * payload and padding are kept verbatim and lengths are written as given (left out, the record Length is
30
+ * the octets after the record header and the Payload Length the payload's octet count), so a well-formed
31
+ * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
32
+ * Content Type of 0x18 and a recognized legacy Version (0x0300-0x0304).
32
33
  */
33
34
  class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
34
35
  constructor() {
@@ -36,11 +37,13 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
36
37
  this.SCHEMA = {
37
38
  type: 'object',
38
39
  properties: {
40
+ //Default 24 (heartbeat): the content type match() requires.
39
41
  contentType: {
40
42
  type: 'integer',
41
43
  label: 'Content Type',
42
44
  minimum: 0,
43
45
  maximum: 255,
46
+ default: 24,
44
47
  decode: () => {
45
48
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
46
49
  },
@@ -54,6 +57,8 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
54
57
  label: 'Version',
55
58
  //Closed set: decode stores one of these names, or '0' for an unrecognized version.
56
59
  enum: [...Object.values(TLSver), '0'],
60
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
61
+ default: TLSver.TLS_1_2,
57
62
  minimum: 0,
58
63
  maximum: 65535,
59
64
  decode: () => {
@@ -128,6 +133,7 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
128
133
  },
129
134
  length: {
130
135
  type: 'integer',
136
+ autoCalculated: true,
131
137
  label: 'Length',
132
138
  minimum: 0,
133
139
  maximum: 65535,
@@ -135,8 +141,21 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
135
141
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
136
142
  },
137
143
  encode: () => {
138
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
139
- this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
144
+ if (!this.isLeftOut(this.instance.length)) {
145
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
146
+ return;
147
+ }
148
+ //The record payload: every octet this layer writes after the 5-octet record header.
149
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
150
+ this.addPostSelfEncodeHandler(() => {
151
+ let length = this.length - 5;
152
+ if (length > 65535) {
153
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
154
+ length = 65535;
155
+ }
156
+ this.instance.length.setValue(length);
157
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
158
+ });
140
159
  }
141
160
  },
142
161
  heartbeatType: {
@@ -188,6 +207,7 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
188
207
  },
189
208
  payloadLength: {
190
209
  type: 'integer',
210
+ autoCalculated: true,
191
211
  label: 'Payload Length',
192
212
  minimum: 0,
193
213
  maximum: 65535,
@@ -195,7 +215,19 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
195
215
  this.instance.payloadLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
196
216
  },
197
217
  encode: () => {
198
- const payloadLength = this.instance.payloadLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
218
+ //Computed right here, not after the layer: padding is placed after payloadLength octets.
219
+ let payloadLength;
220
+ if (!this.isLeftOut(this.instance.payloadLength)) {
221
+ payloadLength = this.instance.payloadLength.getValue(0);
222
+ }
223
+ else {
224
+ payloadLength = (0, HexToBuffer_1.HexToBuffer)(this.instance.payloadMessage.getValue('')).length;
225
+ if (payloadLength > 65535) {
226
+ this.recordError(this.instance.payloadLength.getPath(), 'Maximum value is 65535');
227
+ payloadLength = 65535;
228
+ }
229
+ this.instance.payloadLength.setValue(payloadLength);
230
+ }
199
231
  this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(payloadLength));
200
232
  }
201
233
  },
@@ -208,7 +240,8 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
208
240
  this.instance.payloadMessage.setValue(payloadMessage);
209
241
  },
210
242
  encode: () => {
211
- const payloadMessage = this.instance.payloadMessage.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
243
+ //Left out, the message carries no payload (Payload Length 0 when computed).
244
+ const payloadMessage = this.instance.payloadMessage.getValue('', (nodePath) => this.recordError(nodePath, 'Not Found'));
212
245
  this.writeBytes(8, (0, HexToBuffer_1.HexToBuffer)(payloadMessage));
213
246
  }
214
247
  },
@@ -56,7 +56,7 @@ class TPKT extends BaseHeader_1.BaseHeader {
56
56
  //derive it after every layer has encoded, summing this header plus all layers
57
57
  //stacked on top of it — the same approach UDP uses for its length.
58
58
  const length = this.instance.length.getValue(0);
59
- if (length) {
59
+ if (!this.isLeftOut(this.instance.length)) {
60
60
  this.instance.length.setValue(length);
61
61
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
62
62
  }