@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
@@ -6,6 +6,7 @@ const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const BufferToNumber_1 = require("../helper/BufferToNumber");
8
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
9
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
10
11
  /**
11
12
  * RSVP — Resource ReSerVation Protocol (RFC 2205), carried directly over IP as protocol 46. Every RSVP
@@ -18,9 +19,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
18
19
  *
19
20
  * The object contents are class/C-Type specific and several (SESSION, SENDER_TSPEC, FLOWSPEC, …) need
20
21
  * cross-object and policy context, so this codec keeps each object's contents verbatim as `data` hex
21
- * (byte-perfect) and does not sub-decode them. The RSVP Length is honored verbatim when supplied on
22
- * encode (a crafted message may lie) and derived from the objects otherwise; each object's Length is
23
- * likewise honored-else-derived. The Checksum is honored verbatim, never recomputed. Object walking is
22
+ * (byte-perfect) and does not sub-decode them. The RSVP Length, each object's Length and the Checksum
23
+ * are computed on encode when left out (the Length and Checksum over the bytes actually written), and
24
+ * written as given otherwise, 0 included (a crafted message may lie). Object walking is
24
25
  * doubly bounded — by the RSVP Length field and by the bytes the IP layer actually made available — so a
25
26
  * lying length can't read past the datagram, and trailing / pipelined bytes are left to the codec's
26
27
  * recursion / RawData. A well-formed message round-trips byte-for-byte.
@@ -80,14 +81,6 @@ class RSVP extends BaseHeader_1.BaseHeader {
80
81
  end = available;
81
82
  return end;
82
83
  }
83
- /** Effective on-wire byte length of one object: its Length field when supplied, else 4 + its data. */
84
- static #objectLength(object) {
85
- const provided = Number(object && object.length);
86
- if (Number.isFinite(provided) && provided > 0)
87
- return provided;
88
- const data = object && object.data ? String(object.data) : '';
89
- return 4 + (0, HexToBuffer_1.HexToBuffer)(data).length;
90
- }
91
84
  static #buildSchema() {
92
85
  return {
93
86
  type: 'object',
@@ -101,6 +94,7 @@ class RSVP extends BaseHeader_1.BaseHeader {
101
94
  label: 'Version',
102
95
  minimum: 0,
103
96
  maximum: 15,
97
+ default: 1,
104
98
  decode: function () {
105
99
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
106
100
  },
@@ -127,10 +121,34 @@ class RSVP extends BaseHeader_1.BaseHeader {
127
121
  }
128
122
  },
129
123
  msgType: this.fieldUInt('msgType', 1, 1, 'Message Type'),
130
- //Honored verbatim: the RSVP checksum (ones-complement over the message) is never
131
- //recomputed, so a captured message round-trips byte-for-byte. A zero value means no
132
- //checksum was transmitted (RFC 2205 §3.1.1).
133
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
124
+ //Internet checksum over the whole RSVP message (RFC 2205 §3.1.1), computed after the Length
125
+ //once every object is written when left out. A given value is written as given; 0 means no
126
+ //checksum was transmitted.
127
+ checksum: {
128
+ type: 'integer',
129
+ autoCalculated: true,
130
+ label: 'Checksum',
131
+ minimum: 0,
132
+ maximum: 65535,
133
+ decode: function () {
134
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
135
+ },
136
+ encode: function () {
137
+ if (!this.isLeftOut(this.instance.checksum)) {
138
+ const checksum = this.instance.checksum.getValue(0);
139
+ this.instance.checksum.setValue(checksum);
140
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
141
+ return;
142
+ }
143
+ this.instance.checksum.setValue(0);
144
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
145
+ this.addPostSelfEncodeHandler(() => {
146
+ const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
147
+ this.instance.checksum.setValue(checksum);
148
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
149
+ }, 2);
150
+ }
151
+ },
134
152
  sendTTL: this.fieldUInt('sendTTL', 4, 1, 'Send TTL'),
135
153
  //Reserved byte — kept verbatim and re-emitted for byte-perfect.
136
154
  reserved: this.fieldUInt('reserved', 5, 1, 'Reserved'),
@@ -144,30 +162,27 @@ class RSVP extends BaseHeader_1.BaseHeader {
144
162
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
145
163
  },
146
164
  encode: function () {
147
- //RSVP Length counts the whole message = 8-byte common header + all objects. Honored
148
- //when supplied (a crafted message may lie); else derived from the objects.
149
- const provided = this.instance.length.getValue();
150
- let value;
151
- if (provided !== undefined && provided !== null) {
152
- value = provided;
153
- }
154
- else {
155
- const objects = this.instance.objects.getValue();
156
- let total = 8;
157
- if (Array.isArray(objects)) {
158
- for (const object of objects)
159
- total += RSVP.#objectLength(object);
165
+ //RSVP Length counts the whole message = 8-byte common header + all objects. A given
166
+ //value is written as given (a crafted message may lie; flagged below the header).
167
+ if (!this.isLeftOut(this.instance.length)) {
168
+ let value = this.instance.length.getValue(0);
169
+ if (value > 65535) {
170
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
171
+ value = 65535;
160
172
  }
161
- value = total;
162
- }
163
- if (value > 65535) {
164
- this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
165
- value = 65535;
173
+ if (value < 8)
174
+ this.recordError(this.instance.length.getPath(), 'Length should be at least 8');
175
+ this.instance.length.setValue(value);
176
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
177
+ return;
166
178
  }
167
- if (value < 8)
168
- value = 8;
169
- this.instance.length.setValue(value);
170
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
179
+ //Left out: the bytes actually written, known once every object is encoded.
180
+ this.instance.length.setValue(0);
181
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
182
+ this.addPostSelfEncodeHandler(() => {
183
+ this.instance.length.setValue(this.length);
184
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(this.length));
185
+ }, 1);
171
186
  }
172
187
  },
173
188
  //==== Objects (RFC 2205 §3.1.2) ====
@@ -228,11 +243,10 @@ class RSVP extends BaseHeader_1.BaseHeader {
228
243
  let offset = 8;
229
244
  for (const object of objects) {
230
245
  const dataBuffer = (0, HexToBuffer_1.HexToBuffer)(object && object.data ? String(object.data) : '');
231
- //Object Length honored when supplied (may lie), else derived from contents.
232
- const providedLength = Number(object && object.length);
233
- const lengthValue = (Number.isFinite(providedLength) && providedLength > 0)
234
- ? providedLength
235
- : 4 + dataBuffer.length;
246
+ //Object Length written as given, 0 included (may lie); left out, derived from
247
+ //the 4-byte header + contents.
248
+ const lengthLeftOut = !object || object.length === undefined || object.length === null;
249
+ const lengthValue = lengthLeftOut ? 4 + dataBuffer.length : (Number(object.length) || 0);
236
250
  const classNum = Number(object && object.classNum) || 0;
237
251
  const cType = Number(object && object.cType) || 0;
238
252
  this.writeBytes(offset, (0, NumberToBuffer_1.UInt16ToBuffer)(lengthValue > 65535 ? 65535 : lengthValue));
@@ -56,6 +56,7 @@ class RTCP extends BaseHeader_1.BaseHeader {
56
56
  //separate field over the same octet window; writeBits read-modify-writes so sequential
57
57
  //encode of the three composes byte 0 correctly.
58
58
  version: {
59
+ default: 2,
59
60
  type: 'integer',
60
61
  label: 'Version',
61
62
  minimum: 0,
@@ -117,8 +118,9 @@ class RTCP extends BaseHeader_1.BaseHeader {
117
118
  }
118
119
  },
119
120
  //Packet Type (200 SR .. 207 XR). A plain uint8 — no hard enum, so an unknown/future type
120
- //still decodes and re-encodes (never-throws).
121
- packetType: this.fieldUInt('packetType', 1, 1, 'Packet Type'),
121
+ //still decodes and re-encodes (never-throws). Default 201 (Receiver Report, complete with
122
+ //no report blocks): match() requires a type in 200..207.
123
+ packetType: { ...this.fieldUInt('packetType', 1, 1, 'Packet Type'), default: 201 },
122
124
  length: {
123
125
  type: 'integer',
124
126
  autoCalculated: true,
@@ -105,8 +105,9 @@ class RTP extends BaseHeader_1.BaseHeader {
105
105
  type: 'object',
106
106
  summary: 'RTP pt=${payloadType} seq=${sequenceNumber} ssrc=${ssrc}',
107
107
  properties: {
108
- //Byte 0 (MSB first): Version(0-1) Padding(2) Extension(3) CSRC Count(4-7).
109
- version: this.#uintBits('version', 0, 0, 2, 'Version'),
108
+ //Byte 0 (MSB first): Version(0-1) Padding(2) Extension(3) CSRC Count(4-7). Default version
109
+ //2: the signature match() requires.
110
+ version: { ...this.#uintBits('version', 0, 0, 2, 'Version'), default: 2 },
110
111
  padding: this.#flagBit('padding', 0, 2, 'Padding'),
111
112
  extension: this.#flagBit('extension', 0, 3, 'Extension'),
112
113
  //CSRC Count: the number of 4-byte CSRC identifiers. Honored when supplied (a crafted
@@ -5,8 +5,8 @@ import { DemuxProducer } from '../types/DemuxProducer';
5
5
  * SCTP — Stream Control Transmission Protocol (RFC 9260), carried directly over IP as protocol 132
6
6
  * (both IPv4 `protocol` and IPv6 `nxt`). The 12-byte common header is all big-endian: Source Port (2) +
7
7
  * Destination Port (2) + Verification Tag (4) + Checksum (4). The Checksum is a CRC32c over the whole
8
- * SCTP packet (with the field zeroed) stored little-endian on the wire — but it is honored verbatim,
9
- * never recomputed (encode is a faithful executor), so it is surfaced as an opaque 4-byte hex string.
8
+ * SCTP packet (with the field zeroed) stored little-endian on the wire, surfaced as the 4-byte hex of
9
+ * the wire bytes; encode computes it when left out and writes a given value as given.
10
10
  *
11
11
  * After the common header comes a chain of chunks until the enclosing IP payload is consumed. Each chunk
12
12
  * is Type(1) + Flags(1) + Length(2) + Value, where Length counts the 4-byte chunk header plus the value
@@ -6,13 +6,14 @@ const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const BufferToNumber_1 = require("../helper/BufferToNumber");
8
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
9
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
10
11
  /**
11
12
  * SCTP — Stream Control Transmission Protocol (RFC 9260), carried directly over IP as protocol 132
12
13
  * (both IPv4 `protocol` and IPv6 `nxt`). The 12-byte common header is all big-endian: Source Port (2) +
13
14
  * Destination Port (2) + Verification Tag (4) + Checksum (4). The Checksum is a CRC32c over the whole
14
- * SCTP packet (with the field zeroed) stored little-endian on the wire — but it is honored verbatim,
15
- * never recomputed (encode is a faithful executor), so it is surfaced as an opaque 4-byte hex string.
15
+ * SCTP packet (with the field zeroed) stored little-endian on the wire, surfaced as the 4-byte hex of
16
+ * the wire bytes; encode computes it when left out and writes a given value as given.
16
17
  *
17
18
  * After the common header comes a chain of chunks until the enclosing IP payload is consumed. Each chunk
18
19
  * is Type(1) + Flags(1) + Length(2) + Value, where Length counts the 4-byte chunk header plus the value
@@ -72,9 +73,32 @@ class SCTP extends BaseHeader_1.BaseHeader {
72
73
  dstPort: this.fieldUInt('dstPort', 2, 2, 'Destination Port'),
73
74
  //32-bit association selector; opaque on the wire, kept verbatim as hex.
74
75
  verificationTag: this.fieldHex('verificationTag', 4, 4, 'Verification Tag'),
75
- //CRC32c over the whole packet (little-endian on the wire) — honored verbatim, never
76
- //recomputed, so it is surfaced as opaque 4-byte hex and round-trips even when crafted/wrong.
77
- checksum: this.fieldHex('checksum', 8, 4, 'Checksum'),
76
+ //CRC32c over the whole packet with this field zeroed (RFC 9260 §6.8), least significant byte
77
+ //first on the wire, kept as the hex of the wire bytes. Left out, computed once every chunk
78
+ //is written; a given value is written as given, so a crafted/wrong one round-trips.
79
+ checksum: {
80
+ type: 'string',
81
+ autoCalculated: true,
82
+ label: 'Checksum',
83
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
84
+ decode: function () {
85
+ this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(8, 4)));
86
+ },
87
+ encode: function () {
88
+ if (!this.isLeftOut(this.instance.checksum)) {
89
+ this.writeBytes(8, (0, HexToBuffer_1.HexToBuffer)(this.instance.checksum.getValue('00000000')));
90
+ return;
91
+ }
92
+ this.instance.checksum.setValue('00000000');
93
+ this.writeBytes(8, Buffer.alloc(4, 0));
94
+ this.addPostSelfEncodeHandler(() => {
95
+ const checksum = Buffer.alloc(4);
96
+ checksum.writeUInt32LE((0, Checksum_1.CRC32c)(this.packet.subarray(this.startPos, this.endPos)));
97
+ this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(checksum));
98
+ this.writeBytes(8, checksum);
99
+ });
100
+ }
101
+ },
78
102
  chunks: {
79
103
  type: 'array',
80
104
  label: 'Chunks',
@@ -105,8 +105,10 @@ class SFlow extends BaseHeader_1.BaseHeader {
105
105
  type: 'object',
106
106
  summary: 'sFlow ${numSamples} samples',
107
107
  properties: {
108
- version: SFlow.fieldUInt('version', 0, 4, 'Version'),
109
- agentAddressType: SFlow.fieldUInt('agentAddressType', 4, 4, 'Agent Address Type'),
108
+ //Default 5: the sFlow v5 version, and what match() requires.
109
+ version: { ...SFlow.fieldUInt('version', 0, 4, 'Version'), default: 5 },
110
+ //Default 1 (IPv4): the agent address layout a left-out type is written with.
111
+ agentAddressType: { ...SFlow.fieldUInt('agentAddressType', 4, 4, 'Agent Address Type'), default: 1 },
110
112
  //The only variable-length field of the fixed header: 4 bytes as a dotted quad for IPv4
111
113
  //(type 1), 16 bytes kept verbatim as hex for IPv6 (type 2). Its length shifts every
112
114
  //downstream offset, so all trailing fields compute their offset from #subAgentIdOffset().
@@ -79,7 +79,8 @@ class SLMP extends BaseHeader_1.BaseHeader {
79
79
  properties: {
80
80
  //The 2-byte frame marker, kept in wire order (big-endian): 0x5000 (`50 00`) request,
81
81
  //0xD000 (`D0 00`) response. Not little-endian — it is a fixed signature, not a value.
82
- subheader: this.fieldUInt('subheader', 0, 2, 'Subheader'),
82
+ //Default 0x5000 (request): what match() requires.
83
+ subheader: { ...this.fieldUInt('subheader', 0, 2, 'Subheader'), default: 0x5000 },
83
84
  networkNo: this.fieldUInt('networkNo', 2, 1, 'Network Number'),
84
85
  stationNo: this.fieldUInt('stationNo', 3, 1, 'Station Number'),
85
86
  //Request-destination Module I/O number (little-endian, e.g. 0x03FF = own station).
@@ -88,7 +88,8 @@ class SLP extends BaseHeader_1.BaseHeader {
88
88
  type: 'object',
89
89
  summary: 'SLP func=${functionId} xid=${xid}',
90
90
  properties: {
91
- version: this.fieldUInt('version', 0, 1, 'Version'),
91
+ //Default 2: the SLPv2 version, and what match() requires.
92
+ version: { ...this.fieldUInt('version', 0, 1, 'Version'), default: 2 },
92
93
  functionId: this.fieldUInt('functionId', 1, 1, 'Function-ID'),
93
94
  length: {
94
95
  type: 'integer',
@@ -53,7 +53,8 @@ class SLPv1 extends BaseHeader_1.BaseHeader {
53
53
  type: 'object',
54
54
  summary: 'SLPv1 func=${functionId} xid=${xid}',
55
55
  properties: {
56
- version: this.fieldUInt('version', 0, 1, 'Version'),
56
+ //Default 1: the SLPv1 version, and what match() requires.
57
+ version: { ...this.fieldUInt('version', 0, 1, 'Version'), default: 1 },
57
58
  functionId: this.fieldUInt('functionId', 1, 1, 'Function'),
58
59
  length: {
59
60
  type: 'integer',
@@ -139,8 +139,9 @@ class SMB1 extends BaseHeader_1.BaseHeader {
139
139
  },
140
140
  //The 4-byte Protocol magic — 0xFF 'S' 'M' 'B' (ff534d42). Kept verbatim so a crafted magic
141
141
  //round-trips; the match() gate enforces the real signature for selection (and separates
142
- //SMB1 from SMB2's 0xFE 'S' 'M' 'B' in the same 445/139 buckets).
143
- protocolId: this.fieldHex('protocolId', 4, 4, 'Protocol Id'),
142
+ //SMB1 from SMB2's 0xFE 'S' 'M' 'B' in the same 445/139 buckets). Default ff534d42: what
143
+ //match() requires.
144
+ protocolId: { ...this.fieldHex('protocolId', 4, 4, 'Protocol Id'), default: 'ff534d42' },
144
145
  //Command (MS-CIFS §2.2.2.1): 0x72 NEGOTIATE, 0x73 SESSION_SETUP_ANDX, 0x75 TREE_CONNECT_ANDX,
145
146
  //… (kept as a plain honored uint — no enum, so an unknown/crafted opcode still round-trips).
146
147
  command: this.fieldUInt('command', 8, 1, 'Command'),
@@ -136,8 +136,9 @@ class SMB2 extends BaseHeader_1.BaseHeader {
136
136
  }
137
137
  },
138
138
  //The 4-byte ProtocolId magic — 0xFE 'S' 'M' 'B' (fe534d42). Kept verbatim so a crafted
139
- //magic round-trips; the match() gate enforces the real signature for selection.
140
- protocolId: this.fieldHex('protocolId', 4, 4, 'Protocol Id'),
139
+ //magic round-trips; the match() gate enforces the real signature for selection. Default
140
+ //fe534d42: what match() requires.
141
+ protocolId: { ...this.fieldHex('protocolId', 4, 4, 'Protocol Id'), default: 'fe534d42' },
141
142
  //StructureSize is always 64 on the wire (MS-SMB2 §2.2.1.1); kept as an honored LE uint so a
142
143
  //crafted value round-trips (no hard constraint that would reject a lying value on re-encode).
143
144
  structureSize: this.#fieldUInt16LE('structureSize', 8, 'Structure Size'),
@@ -37,7 +37,7 @@ class SNAP extends BaseHeader_1.BaseHeader {
37
37
  properties: {
38
38
  //When oui / etherType are left out they are taken from ouiProtocol — the field the editor
39
39
  //graph sets to route a vendor child (snap→cdp is ouiProtocol '00000c2000').
40
- oui: { ...this.fieldHex('oui', 0, 3, 'OUI'), encode: function () {
40
+ oui: { ...this.fieldHex('oui', 0, 3, 'OUI'), autoCalculated: true, encode: function () {
41
41
  const ouiProtocol = this.instance.ouiProtocol.getValue('');
42
42
  const oui = this.instance.oui.getValue(ouiProtocol.length === 10 ? ouiProtocol.substring(0, 6) : '000000');
43
43
  this.instance.oui.setValue(oui);
@@ -46,7 +46,7 @@ class SNAP extends BaseHeader_1.BaseHeader {
46
46
  //Named `etherType` (not `pid`) so the existing ethertype demux + IPv4/ARP/IPv6 match() —
47
47
  //which read the parent's `etherType` field literally — route through SNAP unchanged, exactly
48
48
  //as they do through Ethernet II and 802.1Q.
49
- etherType: { ...this.fieldHex('etherType', 3, 2, 'Protocol Type'), encode: function () {
49
+ etherType: { ...this.fieldHex('etherType', 3, 2, 'Protocol Type'), autoCalculated: true, encode: function () {
50
50
  const ouiProtocol = this.instance.ouiProtocol.getValue('');
51
51
  const etherType = this.instance.etherType.getValue(ouiProtocol.length === 10 ? ouiProtocol.substring(6, 10) : '0000');
52
52
  this.instance.etherType.setValue(etherType);
@@ -272,7 +272,7 @@ class SNMP extends BaseHeader_1.BaseHeader {
272
272
  community: { type: 'string', label: 'Community' },
273
273
  //The PDU context tag: 0xa0 get-request, 0xa1 get-next-request, 0xa2 get-response,
274
274
  //0xa3 set-request, 0xa5 get-bulk-request, 0xa6 inform-request, 0xa7 snmpV2-trap, 0xa8 report.
275
- pduType: { type: 'integer', label: 'PDU Type', minimum: 0, maximum: 255 },
275
+ pduType: { type: 'integer', label: 'PDU Type', minimum: 0, maximum: 255, default: 0xa0 },
276
276
  //Verbatim PDU body for non-RFC-3416 PDUs (SNMPv1 Trap 0xa4 / unknown tags). Hidden and
277
277
  //empty for the common request/response PDUs, which use the structured fields below.
278
278
  pduRaw: { type: 'string', label: 'PDU Body', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX, hidden: true },
@@ -287,7 +287,7 @@ class SNMP extends BaseHeader_1.BaseHeader {
287
287
  label: 'Variable Binding',
288
288
  properties: {
289
289
  oid: { type: 'string', label: 'Object Identifier' },
290
- valueType: { type: 'integer', label: 'Value Type', minimum: 0, maximum: 255 },
290
+ valueType: { type: 'integer', label: 'Value Type', minimum: 0, maximum: 255, default: 0x05 },
291
291
  value: { type: 'string', label: 'Value', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
292
292
  }
293
293
  }
@@ -57,7 +57,7 @@ class STUN extends BaseHeader_1.BaseHeader {
57
57
  },
58
58
  encode: function () {
59
59
  const messageLength = this.instance.messageLength.getValue(0);
60
- if (messageLength) {
60
+ if (!this.isLeftOut(this.instance.messageLength)) {
61
61
  this.instance.messageLength.setValue(messageLength);
62
62
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(messageLength));
63
63
  }
@@ -74,7 +74,8 @@ class STUN extends BaseHeader_1.BaseHeader {
74
74
  }
75
75
  }
76
76
  },
77
- magicCookie: this.fieldHex('magicCookie', 4, 4, 'Magic Cookie'),
77
+ //Default 0x2112A442: the fixed Magic Cookie (RFC 5389 §6), and what match() requires.
78
+ magicCookie: { ...this.fieldHex('magicCookie', 4, 4, 'Magic Cookie'), default: '2112a442' },
78
79
  transactionId: this.fieldHex('transactionId', 8, 12, 'Transaction ID'),
79
80
  attributes: {
80
81
  type: 'array',
@@ -20,8 +20,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
20
20
  * Byte 1 — Phase field (the Master Sync Telegram, MST):
21
21
  * - the Phase occupies mask 0x8f (bit 7 = phase switching, bits 0..3 = communication phase CP0..CP4),
22
22
  * a non-contiguous field kept as one value; the Cycle Count occupies mask 0x70 (bits 4..6).
23
- * Bytes 2..5 — CRC32 of the MST, kept verbatim (little-endian on the wire per the dissector) and never
24
- * recomputed, so a captured frame round-trips byte-for-byte.
23
+ * Bytes 2..5 — CRC32 of the MST, little-endian on the wire (per the dissector): CRC-32 (IEEE 802.3) over
24
+ * the Ethernet header (destination, source, EtherType) and bytes 0..1. Written as given; computed only
25
+ * when left out (checked against a real capture: the openSAFETY-over-Sercos III Wireshark sample).
25
26
  *
26
27
  * Everything after the 6-byte header (the phase-dependent per-device connection / service-channel data) is
27
28
  * command- and topology-dependent, so this slice keeps it verbatim as `data` hex (byte-perfect), bounded by
@@ -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;