@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
@@ -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)));
@@ -15,6 +15,12 @@ export declare class TLS_Handshake extends BaseHeader {
15
15
  * verbatim; a structured Hello (messagedata.version set) turns this false.
16
16
  */
17
17
  protected messagedataRawFallbackActive(): boolean;
18
+ /**
19
+ * Throw when a Hello body runs past the captured bytes: a record split across TCP segments carries
20
+ * only part of it here. Structured, its cut-short lists would re-encode with lengths computed from
21
+ * what was captured instead of the ones on the wire, so the caller keeps the body as raw.
22
+ */
23
+ protected assertHelloBodyCaptured(end: number): void;
18
24
  /**
19
25
  * Structurally decode a ClientHello (handshake type 1) body starting at record offset 9.
20
26
  * Throws on any bound overrun / trailing bytes so the caller can fall back to raw.
@@ -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: {
@@ -399,14 +432,28 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
399
432
  return;
400
433
  }
401
434
  const handshakeType = this.instance.handshakeType.getValue();
435
+ //Only a ClientHello or ServerHello body is built from fields. Any other type's body is
436
+ //its raw bytes, empty when left out: a HelloRequest or ServerHelloDone has no body
437
+ //(RFC 5246 7.4.1.1, 7.4.5). Hello fields given for it are reported, not written.
438
+ if (handshakeType !== HandshakeType.Handshake_2 && handshakeType !== HandshakeType.Handshake_3) {
439
+ for (const field of ['version', 'random', 'sessionId', 'cipherSuites', 'cipherSuite', 'compressionMethods', 'compressionMethod', 'extensions']) {
440
+ if (!md[field].isUndefined())
441
+ this.recordError(md[field].getPath(), 'Only a ClientHello or ServerHello body carries this field, not written');
442
+ }
443
+ if (!this.isLeftOut(md.raw))
444
+ this.writeBytes(9, (0, HexToBuffer_1.HexToBuffer)(md.raw.getValue().toString()));
445
+ return;
446
+ }
447
+ //Left out, the fixed-width Hello fields are zeros of their size (a 32-octet random, the
448
+ //null cipher suite and compression method), so the body still parses as a Hello.
402
449
  const parts = [];
403
450
  parts.push(this.tlsVersionStringToBuffer(md.version.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
404
- parts.push((0, HexToBuffer_1.HexToBuffer)(md.random.getValue('')));
451
+ parts.push(this.isLeftOut(md.random) ? Buffer.alloc(32, 0) : (0, HexToBuffer_1.HexToBuffer)(md.random.getValue()));
405
452
  const sessionId = (0, HexToBuffer_1.HexToBuffer)(md.sessionId.getValue(''));
406
453
  parts.push((0, NumberToBuffer_1.UInt8ToBuffer)(sessionId.length), sessionId);
407
454
  if (handshakeType === HandshakeType.Handshake_3) {
408
- parts.push((0, HexToBuffer_1.HexToBuffer)(md.cipherSuite.getValue('')));
409
- parts.push((0, HexToBuffer_1.HexToBuffer)(md.compressionMethod.getValue('')));
455
+ parts.push(this.isLeftOut(md.cipherSuite) ? Buffer.alloc(2, 0) : (0, HexToBuffer_1.HexToBuffer)(md.cipherSuite.getValue()));
456
+ parts.push(this.isLeftOut(md.compressionMethod) ? Buffer.alloc(1, 0) : (0, HexToBuffer_1.HexToBuffer)(md.compressionMethod.getValue()));
410
457
  }
411
458
  else {
412
459
  const cipherSuites = Buffer.concat(md.cipherSuites.getValue([]).map((code) => (0, HexToBuffer_1.HexToBuffer)(code)));
@@ -481,6 +528,15 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
481
528
  return false;
482
529
  return md.version.isUndefined();
483
530
  }
531
+ /**
532
+ * Throw when a Hello body runs past the captured bytes: a record split across TCP segments carries
533
+ * only part of it here. Structured, its cut-short lists would re-encode with lengths computed from
534
+ * what was captured instead of the ones on the wire, so the caller keeps the body as raw.
535
+ */
536
+ assertHelloBodyCaptured(end) {
537
+ if (end > this.packet.length - this.startPos)
538
+ throw new Error('the body continues past the captured bytes');
539
+ }
484
540
  /**
485
541
  * Structurally decode a ClientHello (handshake type 1) body starting at record offset 9.
486
542
  * Throws on any bound overrun / trailing bytes so the caller can fall back to raw.
@@ -488,6 +544,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
488
544
  decodeClientHello(bodyLength) {
489
545
  let p = 9;
490
546
  const end = 9 + bodyLength;
547
+ this.assertHelloBodyCaptured(end);
491
548
  const need = (n) => {
492
549
  if (n < 0 || p + n > end)
493
550
  throw new Error('handshake body overrun');
@@ -551,6 +608,7 @@ class TLS_Handshake extends BaseHeader_1.BaseHeader {
551
608
  decodeServerHello(bodyLength) {
552
609
  let p = 9;
553
610
  const end = 9 + bodyLength;
611
+ this.assertHelloBodyCaptured(end);
554
612
  const need = (n) => {
555
613
  if (n < 0 || p + n > end)
556
614
  throw new Error('handshake body overrun');
@@ -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
  }
@@ -45,13 +45,15 @@ class TacacsPlus extends BaseHeader_1.BaseHeader {
45
45
  type: 'object',
46
46
  label: 'Version',
47
47
  properties: {
48
+ //Default 0xc: the TACACS+ major version (RFC 8907 §4.1), and what match() requires.
48
49
  major: {
49
50
  type: 'integer',
50
51
  label: 'Major Version',
51
52
  minimum: 0,
52
53
  maximum: 15,
54
+ default: 0xc,
53
55
  decode: function () { this.instance.version.major.setValue(this.readBits(0, 1, 0, 4)); },
54
- encode: function () { this.writeBits(0, 1, 0, 4, this.instance.version.major.getValue(0)); }
56
+ encode: function () { this.writeBits(0, 1, 0, 4, this.instance.version.major.getValue(0xc)); }
55
57
  },
56
58
  minor: {
57
59
  type: 'integer',
@@ -0,0 +1,30 @@
1
+ import { ProtocolJSONSchema } from '../schema/ProtocolJSONSchema';
2
+ import { BaseHeader } from '../abstracts/BaseHeader';
3
+ /**
4
+ * The bytes a frame carries past the end its own length fields give: Ethernet padding up to the
5
+ * 60-octet minimum frame, or a trailer a device appended. Decode puts them here when an IPv4 Total
6
+ * Length, an IPv6 Payload Length or an 802.3 length ends before the captured frame does (see
7
+ * BaseHeader.payloadEnd), so the layers above see only their own payload. The bytes are kept verbatim
8
+ * as `data` hex.
9
+ *
10
+ * Encode writes nothing in layer order: the codec calls appendToPacket() once every packet-level
11
+ * post-encode handler has run, so no computed length or checksum counts these bytes. Trailer layers
12
+ * therefore always land at the end of the packet, in input order. Not a protocol
13
+ * (`isProtocol = false`) and never chosen by decode dispatch (match() is always false): the codec
14
+ * places it.
15
+ */
16
+ export declare class Trailer extends BaseHeader {
17
+ readonly SCHEMA: ProtocolJSONSchema;
18
+ readonly id: string;
19
+ readonly name: string;
20
+ readonly nickname: string;
21
+ readonly isProtocol: boolean;
22
+ match(): boolean;
23
+ /**
24
+ * Write the trailer bytes at the current end of the packet. The codec calls this after every
25
+ * packet-level post-encode handler, so none of them counted the bytes.
26
+ * @param followsLayers true when the input placed other layers after this one: their bytes now
27
+ * come first, which is reported as a warning.
28
+ */
29
+ appendToPacket(followsLayers: boolean): void;
30
+ }