@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
@@ -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
  * CDP — Cisco Discovery Protocol, carried in an 802.3 frame over LLC/SNAP with OUI 0x00000C and PID
@@ -15,9 +16,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
15
16
  * Capabilities, 0x0005 Software Version, 0x0006 Platform.
16
17
  *
17
18
  * The TLV values are kept verbatim as hex (device names, address lists and capability bitmaps need no
18
- * form structure), the Checksum is honored verbatim (never recomputed), and the TLV walk is bounded by
19
- * the captured bytes so a corrupt Length can't run past the frame. A well-formed CDP frame round-trips
20
- * byte-for-byte.
19
+ * form structure), and the TLV walk is bounded by the captured bytes so a corrupt Length can't run past
20
+ * the frame. The Checksum and each TLV Length are computed on encode when left out and written as given
21
+ * otherwise. A well-formed CDP frame round-trips byte-for-byte.
21
22
  */
22
23
  class CDP extends BaseHeader_1.BaseHeader {
23
24
  constructor() {
@@ -33,6 +34,21 @@ class CDP extends BaseHeader_1.BaseHeader {
33
34
  get SCHEMA() {
34
35
  return (CDP.#schemaCache ??= CDP.#buildSchema());
35
36
  }
37
+ /**
38
+ * The CDP checksum: the Internet checksum of the whole CDP message, except for an odd trailing
39
+ * octet. RFC 1071 pads it with a zero octet (word 0xXX00); Cisco adds it as a signed low byte
40
+ * instead, which comes out as the word 0x00XX below 0x80 and 0xFF(XX-1) from 0x80 on. This is what
41
+ * Wireshark's CDP dissector checks, and real Cisco captures carry it.
42
+ * @param message the CDP message with the checksum field zeroed
43
+ * @private
44
+ */
45
+ static #checksum(message) {
46
+ if (message.length % 2 === 0)
47
+ return (0, Checksum_1.InternetChecksum)(message);
48
+ const last = message[message.length - 1];
49
+ const finalWord = (last & 0x80) ? Buffer.from([0xFF, last - 1]) : Buffer.from([0x00, last]);
50
+ return (0, Checksum_1.InternetChecksum)(message.subarray(0, message.length - 1), finalWord);
51
+ }
36
52
  static #buildSchema() {
37
53
  return {
38
54
  type: 'object',
@@ -40,9 +56,29 @@ class CDP extends BaseHeader_1.BaseHeader {
40
56
  properties: {
41
57
  version: this.fieldUInt('version', 0, 1, 'Version'),
42
58
  ttl: this.fieldUInt('ttl', 1, 1, 'TTL'),
43
- //Honored verbatim — the ones-complement checksum is never recomputed, so a captured frame
44
- //round-trips byte-for-byte.
45
- checksum: this.fieldHex('checksum', 2, 2, 'Checksum'),
59
+ //Left out, computed once every TLV is written (#checksum); a given value is written as given.
60
+ checksum: {
61
+ type: 'string',
62
+ autoCalculated: true,
63
+ label: 'Checksum',
64
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
65
+ decode: function () {
66
+ this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2)));
67
+ },
68
+ encode: function () {
69
+ if (!this.isLeftOut(this.instance.checksum)) {
70
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.checksum.getValue('0000')));
71
+ return;
72
+ }
73
+ this.instance.checksum.setValue('0000');
74
+ this.writeBytes(2, Buffer.alloc(2, 0));
75
+ this.addPostSelfEncodeHandler(() => {
76
+ const checksum = (0, NumberToBuffer_1.UInt16ToBuffer)(CDP.#checksum(this.packet.subarray(this.startPos, this.endPos)));
77
+ this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(checksum));
78
+ this.writeBytes(2, checksum);
79
+ });
80
+ }
81
+ },
46
82
  tlvs: {
47
83
  type: 'array',
48
84
  label: 'TLVs',
@@ -87,10 +123,10 @@ class CDP extends BaseHeader_1.BaseHeader {
87
123
  let offset = 4;
88
124
  for (const tlv of tlvs) {
89
125
  const valueBuffer = (0, HexToBuffer_1.HexToBuffer)(tlv && tlv.value ? String(tlv.value) : '');
90
- const providedLength = Number(tlv && tlv.length);
91
- const lengthValue = (Number.isFinite(providedLength) && providedLength > 0)
92
- ? providedLength
93
- : 4 + valueBuffer.length;
126
+ //Length written as given, 0 included (a crafted TLV may lie); left out, derived
127
+ //from the 4-byte header + value.
128
+ const lengthLeftOut = !tlv || tlv.length === undefined || tlv.length === null;
129
+ const lengthValue = lengthLeftOut ? 4 + valueBuffer.length : (Number(tlv.length) || 0);
94
130
  const type = Number(tlv && tlv.type) || 0;
95
131
  this.writeBytes(offset, (0, NumberToBuffer_1.UInt16ToBuffer)(type & 0xffff));
96
132
  this.writeBytes(offset + 2, (0, NumberToBuffer_1.UInt16ToBuffer)(lengthValue > 65535 ? 65535 : lengthValue));
@@ -66,9 +66,9 @@ class CMS extends BaseHeader_1.BaseHeader {
66
66
  err: this.#bit('err', 2, 'Err'),
67
67
  //bak: reserved.
68
68
  bak: this.#bit('bak', 3, 'bak'),
69
- //Protocol type PI — the low nibble, 0x01 for DL/T 2811.
69
+ //Protocol type PI — the low nibble, 0x01 for DL/T 2811 (the default, and what match() requires).
70
70
  protocolType: {
71
- type: 'integer', label: 'Protocol Type', minimum: 0, maximum: 15,
71
+ type: 'integer', label: 'Protocol Type', minimum: 0, maximum: 15, default: 1,
72
72
  decode: function () {
73
73
  this.instance.protocolType.setValue(this.readBits(0, 1, 4, 4));
74
74
  },
@@ -79,20 +79,35 @@ class CMS extends BaseHeader_1.BaseHeader {
79
79
  //Service Code SC (byte 2) — the service in the data area (Table 1).
80
80
  serviceCode: this.fieldUInt('serviceCode', 1, 1, 'Service Code'),
81
81
  //Frame Length FL (bytes 3-4, LITTLE-ENDIAN) — the APDU length excluding the 4-byte APCH,
82
- //i.e. the ASDU length. Honored verbatim.
82
+ //i.e. the ASDU length. Written as given; left out, it is the ASDU this layer writes
83
+ //(ReqID + service data), known once the whole layer is encoded.
83
84
  frameLength: {
84
- type: 'integer', label: 'Frame Length', minimum: 0, maximum: 65535,
85
+ type: 'integer', autoCalculated: true, label: 'Frame Length', minimum: 0, maximum: 65535,
85
86
  decode: function () {
86
87
  const b = this.readBytes(2, 2);
87
88
  this.instance.frameLength.setValue((b[0] | (b[1] << 8)) & 0xffff);
88
89
  },
89
90
  encode: function () {
90
- let value = this.instance.frameLength.getValue(0);
91
- if (value > 65535)
92
- value = 65535;
93
- if (value < 0)
94
- value = 0;
95
- this.writeBytes(2, Buffer.from([value & 0xff, (value >> 8) & 0xff]));
91
+ const write = (value) => this.writeBytes(2, Buffer.from([value & 0xff, (value >> 8) & 0xff]));
92
+ if (!this.isLeftOut(this.instance.frameLength)) {
93
+ let value = this.instance.frameLength.getValue(0);
94
+ if (value > 65535)
95
+ value = 65535;
96
+ if (value < 0)
97
+ value = 0;
98
+ write(value);
99
+ return;
100
+ }
101
+ write(0);
102
+ this.addPostSelfEncodeHandler(() => {
103
+ let value = this.length - 4;
104
+ if (value > 65535) {
105
+ this.recordError(this.instance.frameLength.getPath(), 'Maximum value is 65535');
106
+ value = 65535;
107
+ }
108
+ this.instance.frameLength.setValue(value);
109
+ write(value);
110
+ });
96
111
  }
97
112
  },
98
113
  //==== ASDU ====
@@ -62,9 +62,8 @@ class COTP extends BaseHeader_1.BaseHeader {
62
62
  this.instance.li.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
63
63
  },
64
64
  encode: function () {
65
- const provided = this.instance.li.getValue(0);
66
- let value = provided;
67
- if (!value) {
65
+ let value = this.instance.li.getValue(0);
66
+ if (this.isLeftOut(this.instance.li)) {
68
67
  const dt = COTP.#isDT(this.instance.pduType.getValue(0));
69
68
  const headerRest = (0, HexToBuffer_1.HexToBuffer)(this.instance.headerRest.getValue(''));
70
69
  //pduType(1) + (DT ? the EOT/TPDU-NR octet(1) : 0) + verbatim header bytes.
@@ -78,9 +78,27 @@ class CoAP extends BaseHeader_1.BaseHeader {
78
78
  summary: 'CoAP type=${type} code=${code}',
79
79
  properties: {
80
80
  //Byte 0 (MSB first): Version (bits 7:6, always 1), Type (bits 5:4), Token Length (bits 3:0).
81
- version: this.#bitField('version', 0, 2, 3, 'Version'),
81
+ //Default version 1: the only CoAP version (RFC 7252), and what match() requires.
82
+ version: { ...this.#bitField('version', 0, 2, 3, 'Version'), default: 1 },
82
83
  type: this.#bitField('type', 2, 2, 3, 'Type'),
83
- tokenLength: this.#bitField('tokenLength', 4, 4, 15, 'Token Length'),
84
+ //Written as given (a crafted TKL may disagree with the token); left out, the token's octet count.
85
+ tokenLength: {
86
+ ...this.#bitField('tokenLength', 4, 4, 15, 'Token Length'),
87
+ autoCalculated: true,
88
+ encode: function () {
89
+ if (!this.isLeftOut(this.instance.tokenLength)) {
90
+ this.writeBits(0, 1, 4, 4, this.instance.tokenLength.getValue(0));
91
+ return;
92
+ }
93
+ let tokenLength = (0, HexToBuffer_1.HexToBuffer)(this.instance.token.getValue('')).length;
94
+ if (tokenLength > 8)
95
+ this.recordError(this.instance.tokenLength.getPath(), 'A CoAP token is at most 8 octets (RFC 7252 section 3)');
96
+ if (tokenLength > 15)
97
+ tokenLength = 15;
98
+ this.instance.tokenLength.setValue(tokenLength);
99
+ this.writeBits(0, 1, 4, 4, tokenLength);
100
+ }
101
+ },
84
102
  //Byte 1: Code — a 3-bit class and 5-bit detail packed as one octet (e.g. 0x01 = 0.01 GET).
85
103
  code: this.fieldUInt('code', 1, 1, 'Code'),
86
104
  //Bytes 2-3: Message ID (big-endian uint16).
@@ -15,8 +15,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
15
15
  * the header — Reserved, the Sequence/Ack numbers, per-type fields and options — verbatim as
16
16
  * `headerOptions` hex (bytes 9 .. DataOffset*4). Application data after the header is kept as `payload`
17
17
  * hex, bounded by the enclosing IP datagram so an inflated Data Offset can't read past the IP payload.
18
- * The Checksum is honored verbatim, never recomputed; Data Offset is honored when supplied, else derived
19
- * from the actual bytes. A well-formed datagram round-trips byte-for-byte.
18
+ * The Checksum and Data Offset are computed on encode when left out (the checksum per RFC 4340 §9, Data
19
+ * Offset from the actual bytes) and written as given otherwise. A well-formed datagram round-trips
20
+ * byte-for-byte.
20
21
  */
21
22
  export declare class DCCP extends BaseHeader {
22
23
  #private;
@@ -4,7 +4,9 @@ exports.DCCP = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
7
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
8
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
9
11
  /**
10
12
  * DCCP — Datagram Congestion Control Protocol (RFC 4340), carried directly over IP as protocol 33. The
@@ -20,8 +22,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
20
22
  * the header — Reserved, the Sequence/Ack numbers, per-type fields and options — verbatim as
21
23
  * `headerOptions` hex (bytes 9 .. DataOffset*4). Application data after the header is kept as `payload`
22
24
  * hex, bounded by the enclosing IP datagram so an inflated Data Offset can't read past the IP payload.
23
- * The Checksum is honored verbatim, never recomputed; Data Offset is honored when supplied, else derived
24
- * from the actual bytes. A well-formed datagram round-trips byte-for-byte.
25
+ * The Checksum and Data Offset are computed on encode when left out (the checksum per RFC 4340 §9, Data
26
+ * Offset from the actual bytes) and written as given otherwise. A well-formed datagram round-trips
27
+ * byte-for-byte.
25
28
  */
26
29
  class DCCP extends BaseHeader_1.BaseHeader {
27
30
  constructor() {
@@ -73,6 +76,28 @@ class DCCP extends BaseHeader_1.BaseHeader {
73
76
  end = 0;
74
77
  return end;
75
78
  }
79
+ /**
80
+ * The DCCP checksum (RFC 4340 §9): the Internet checksum of the nearest IP layer's pseudo-header
81
+ * (protocol 33, length = the whole DCCP packet), the DCCP header and the application data Checksum
82
+ * Coverage selects — all of it when CsCov is 0, else the first (CsCov - 1) × 4 bytes after the Data
83
+ * Offset × 4 header, capped at the packet. Called once every field is written, checksum still zero.
84
+ */
85
+ #calculateChecksum() {
86
+ const packet = this.packet.subarray(this.startPos, this.endPos);
87
+ const pseudoHeader = this.ipPseudoHeader(33, packet.length);
88
+ if (!pseudoHeader) {
89
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute DCCP checksum: no IP layer beneath DCCP');
90
+ return 0;
91
+ }
92
+ const cscov = this.instance.cscov.getValue(0);
93
+ let covered = packet.length;
94
+ if (cscov) {
95
+ const coverage = (this.instance.dataOffset.getValue(0) + cscov - 1) * 4;
96
+ if (coverage < covered)
97
+ covered = coverage;
98
+ }
99
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, packet.subarray(0, covered));
100
+ }
76
101
  /** A big-endian bit-slice within a single octet at `offset`, read/written in place. */
77
102
  static #bitField(name, offset, bitOffset, bitLength, label) {
78
103
  const maximum = (2 ** bitLength) - 1;
@@ -105,7 +130,8 @@ class DCCP extends BaseHeader_1.BaseHeader {
105
130
  dstPort: this.fieldUInt('dstPort', 2, 2, 'Destination Port'),
106
131
  //Data Offset (32-bit words) = the length of the whole DCCP header incl. options. Honored
107
132
  //when supplied (a crafted datagram may lie); else derived from the fixed 9-byte prefix
108
- //plus the verbatim headerOptions bytes, rounded up to a word.
133
+ //plus the verbatim headerOptions bytes, rounded up to a word, and the header is
134
+ //zero-filled up to that word so it is as long as the Data Offset says.
109
135
  dataOffset: {
110
136
  type: 'integer',
111
137
  autoCalculated: true,
@@ -122,8 +148,10 @@ class DCCP extends BaseHeader_1.BaseHeader {
122
148
  value = provided;
123
149
  }
124
150
  else {
125
- const optBytes = (0, HexToBuffer_1.HexToBuffer)(this.instance.headerOptions.getValue('')).length;
126
- value = Math.ceil((9 + optBytes) / 4);
151
+ const headerBytes = 9 + (0, HexToBuffer_1.HexToBuffer)(this.instance.headerOptions.getValue('')).length;
152
+ value = Math.ceil(headerBytes / 4);
153
+ if (value * 4 > headerBytes)
154
+ this.writeBytes(headerBytes, Buffer.alloc(value * 4 - headerBytes, 0));
127
155
  }
128
156
  if (value > 255) {
129
157
  this.recordError(this.instance.dataOffset.getPath(), 'Maximum value is 255');
@@ -138,9 +166,33 @@ class DCCP extends BaseHeader_1.BaseHeader {
138
166
  //Byte 5: CCVal (high 4 bits) + CsCov (low 4 bits).
139
167
  ccval: this.#bitField('ccval', 5, 0, 4, 'CCVal'),
140
168
  cscov: this.#bitField('cscov', 5, 4, 4, 'Checksum Coverage'),
141
- //Honored verbatim: the DCCP checksum (ones-complement over pseudo-header + coverage) is
142
- //never recomputed, so a captured datagram round-trips byte-for-byte.
143
- checksum: this.fieldUInt('checksum', 6, 2, 'Checksum'),
169
+ //Left out, computed once every field is written (#calculateChecksum); a given value is
170
+ //written as given, so a captured datagram round-trips byte-for-byte.
171
+ checksum: {
172
+ type: 'integer',
173
+ autoCalculated: true,
174
+ label: 'Checksum',
175
+ minimum: 0,
176
+ maximum: 65535,
177
+ decode: function () {
178
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
179
+ },
180
+ encode: function () {
181
+ if (!this.isLeftOut(this.instance.checksum)) {
182
+ const checksum = this.instance.checksum.getValue(0);
183
+ this.instance.checksum.setValue(checksum);
184
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
185
+ return;
186
+ }
187
+ this.instance.checksum.setValue(0);
188
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
189
+ this.addPostSelfEncodeHandler(() => {
190
+ const checksum = this.#calculateChecksum();
191
+ this.instance.checksum.setValue(checksum);
192
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
193
+ });
194
+ }
195
+ },
144
196
  //Byte 8: Res (3 bits) + Type (4 bits) + X (1 bit, Extended Sequence Numbers).
145
197
  res: this.#bitField('res', 8, 0, 3, 'Reserved'),
146
198
  type: this.#bitField('type', 8, 3, 4, 'Type'),
@@ -11,9 +11,11 @@ import { DemuxProducer } from '../types/DemuxProducer';
11
11
  *
12
12
  * This codec decodes the link header structurally and keeps the data-block region (transport header +
13
13
  * application data + the per-block CRCs) verbatim as `payload` hex — de-blocking those CRCs to recover
14
- * the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The CRCs
15
- * are honored verbatim (never recomputed), so a well-formed frame round-trips byte-for-byte; the frame's
16
- * extent is derived from the Length field so a pipelined/trailing frame is left to RawData.
14
+ * the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The block
15
+ * CRCs stay inside `payload` exactly as given. Length and the header CRC are written as given; left out,
16
+ * Length counts the payload's data octets (its block CRCs excluded) and the header CRC is computed
17
+ * (CRC-16/DNP over the first 8 octets). A well-formed frame round-trips byte-for-byte; the frame's extent is
18
+ * derived from the Length field so a pipelined/trailing frame is left to RawData.
17
19
  */
18
20
  export declare class DNP3 extends BaseHeader {
19
21
  #private;
@@ -5,6 +5,9 @@ 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 BufferToNumber_1 = require("../helper/BufferToNumber");
9
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
10
+ const Crc_1 = require("../helper/Crc");
8
11
  /**
9
12
  * DNP3 — Distributed Network Protocol 3 (IEEE 1815), a SCADA protocol common in electric/water
10
13
  * utilities, on TCP/UDP port 20000. Each frame begins with a 10-byte Data Link Layer header: the start
@@ -15,9 +18,11 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
15
18
  *
16
19
  * This codec decodes the link header structurally and keeps the data-block region (transport header +
17
20
  * application data + the per-block CRCs) verbatim as `payload` hex — de-blocking those CRCs to recover
18
- * the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The CRCs
19
- * are honored verbatim (never recomputed), so a well-formed frame round-trips byte-for-byte; the frame's
20
- * extent is derived from the Length field so a pipelined/trailing frame is left to RawData.
21
+ * the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The block
22
+ * CRCs stay inside `payload` exactly as given. Length and the header CRC are written as given; left out,
23
+ * Length counts the payload's data octets (its block CRCs excluded) and the header CRC is computed
24
+ * (CRC-16/DNP over the first 8 octets). A well-formed frame round-trips byte-for-byte; the frame's extent is
25
+ * derived from the Length field so a pipelined/trailing frame is left to RawData.
21
26
  */
22
27
  class DNP3 extends BaseHeader_1.BaseHeader {
23
28
  constructor() {
@@ -56,13 +61,44 @@ class DNP3 extends BaseHeader_1.BaseHeader {
56
61
  const blocks = Math.ceil(userData / 16);
57
62
  return 10 + userData + blocks * 2;
58
63
  }
64
+ /** The user data octets a data-block region of `payloadOctets` carries: every block is up to 16 data
65
+ * octets followed by their 2-octet CRC. A trailing block too short to hold a data octet and its CRC
66
+ * (malformed) contributes none. */
67
+ static #userDataOctets(payloadOctets) {
68
+ const rest = payloadOctets % 18;
69
+ return Math.floor(payloadOctets / 18) * 16 + (rest > 2 ? rest - 2 : 0);
70
+ }
59
71
  static #buildSchema() {
60
72
  return {
61
73
  type: 'object',
62
74
  summary: 'DNP3 func=${control.functionCode} ${source}->${destination}',
63
75
  properties: {
64
- start: this.fieldHex('start', 0, 2, 'Start'),
65
- length: this.fieldUInt('length', 2, 1, 'Length'),
76
+ //Default 0x0564: the start octets every DNP3 frame begins with, and what match() requires.
77
+ start: { ...this.fieldHex('start', 0, 2, 'Start'), default: '0564' },
78
+ //Octets from Control through the user data, the block CRCs excluded: 5 + user data octets.
79
+ length: {
80
+ type: 'integer',
81
+ autoCalculated: true,
82
+ label: 'Length',
83
+ minimum: 0,
84
+ maximum: 255,
85
+ decode: function () {
86
+ this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(2, 1)));
87
+ },
88
+ encode: function () {
89
+ if (!this.isLeftOut(this.instance.length)) {
90
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.length.getValue(0)));
91
+ return;
92
+ }
93
+ let length = 5 + DNP3.#userDataOctets((0, HexToBuffer_1.HexToBuffer)(this.instance.payload.getValue('')).length);
94
+ if (length > 255) {
95
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 255');
96
+ length = 255;
97
+ }
98
+ this.instance.length.setValue(length);
99
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt8ToBuffer)(length));
100
+ }
101
+ },
66
102
  //Control octet (byte 3, MSB first): DIR / PRM / FCB / FCV(or DFC) / Function Code(4 bits).
67
103
  control: {
68
104
  type: 'object',
@@ -111,8 +147,27 @@ class DNP3 extends BaseHeader_1.BaseHeader {
111
147
  this.writeBytes(6, Buffer.from([value & 0xff, (value >> 8) & 0xff]));
112
148
  }
113
149
  },
114
- //Header CRC over the preceding 8 octets, honored verbatim (never recomputed).
115
- headerCrc: this.fieldHex('headerCrc', 8, 2, 'Header CRC'),
150
+ //Header CRC: CRC-16/DNP over the preceding 8 octets (start through source), low octet first.
151
+ //Those octets are all written when this runs, so a left-out CRC is computed right here.
152
+ headerCrc: {
153
+ type: 'string',
154
+ autoCalculated: true,
155
+ label: 'Header CRC',
156
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
157
+ decode: function () {
158
+ this.instance.headerCrc.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(8, 2)));
159
+ },
160
+ encode: function () {
161
+ if (!this.isLeftOut(this.instance.headerCrc)) {
162
+ this.writeBytes(8, (0, HexToBuffer_1.HexToBuffer)(this.instance.headerCrc.getValue()));
163
+ return;
164
+ }
165
+ const crc = (0, Crc_1.Crc16Dnp)(this.readBytes(0, 8));
166
+ const crcBuffer = Buffer.from([crc & 0xff, (crc >> 8) & 0xff]);
167
+ this.instance.headerCrc.setValue((0, BufferToHex_1.BufferToHex)(crcBuffer));
168
+ this.writeBytes(8, crcBuffer);
169
+ }
170
+ },
116
171
  //The data-block region: transport header + application data + per-block CRCs, kept verbatim.
117
172
  //Bounded by the Length-derived frame extent so a pipelined/trailing frame is left to raw.
118
173
  payload: {
@@ -13,8 +13,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
13
13
  * OSPF #available) — a lying Length near the end never reads into a trailing FCS/padding. TLV values are
14
14
  * kept verbatim as hex (per-TLV route/parameter bodies are opaque here), so every TLV round-trips
15
15
  * byte-for-byte; the Length is honored when supplied (a crafted TLV may lie) else derived from the value
16
- * byte count. The Checksum is honored verbatim, never recomputed (encode is a faithful executor). A
17
- * well-formed packet round-trips byte-for-byte. Leaf header — TLV sub-structures are not sub-decoded.
16
+ * byte count. The Checksum is computed over the whole EIGRP packet when left out (no pseudo-header, over
17
+ * IPv4 or IPv6), else written as given. A well-formed packet round-trips byte-for-byte. Leaf header —
18
+ * TLV sub-structures are not sub-decoded.
18
19
  */
19
20
  export declare class EIGRP extends BaseHeader {
20
21
  #private;
@@ -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
  * EIGRP — Enhanced Interior Gateway Routing Protocol (Cisco), carried directly over IP as protocol 88.
@@ -19,8 +20,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
19
20
  * OSPF #available) — a lying Length near the end never reads into a trailing FCS/padding. TLV values are
20
21
  * kept verbatim as hex (per-TLV route/parameter bodies are opaque here), so every TLV round-trips
21
22
  * byte-for-byte; the Length is honored when supplied (a crafted TLV may lie) else derived from the value
22
- * byte count. The Checksum is honored verbatim, never recomputed (encode is a faithful executor). A
23
- * well-formed packet round-trips byte-for-byte. Leaf header — TLV sub-structures are not sub-decoded.
23
+ * byte count. The Checksum is computed over the whole EIGRP packet when left out (no pseudo-header, over
24
+ * IPv4 or IPv6), else written as given. A well-formed packet round-trips byte-for-byte. Leaf header —
25
+ * TLV sub-structures are not sub-decoded.
24
26
  */
25
27
  class EIGRP extends BaseHeader_1.BaseHeader {
26
28
  constructor() {
@@ -66,9 +68,33 @@ class EIGRP extends BaseHeader_1.BaseHeader {
66
68
  //==== Fixed 20-byte header ====
67
69
  version: this.fieldUInt('version', 0, 1, 'Version'),
68
70
  opcode: this.fieldUInt('opcode', 1, 1, 'Opcode'),
69
- //Honored verbatim: the EIGRP checksum (ones-complement over the whole packet with the
70
- //checksum field zeroed) is never recomputed, so a captured packet round-trips byte-for-byte.
71
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
71
+ //Internet checksum over the whole EIGRP packet (header + TLVs, checksum field zeroed),
72
+ //computed once every field is written when left out; a given value is written as given.
73
+ checksum: {
74
+ type: 'integer',
75
+ autoCalculated: true,
76
+ label: 'Checksum',
77
+ minimum: 0,
78
+ maximum: 65535,
79
+ decode: function () {
80
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
81
+ },
82
+ encode: function () {
83
+ if (!this.isLeftOut(this.instance.checksum)) {
84
+ const checksum = this.instance.checksum.getValue(0);
85
+ this.instance.checksum.setValue(checksum);
86
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
87
+ return;
88
+ }
89
+ this.instance.checksum.setValue(0);
90
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
91
+ this.addPostSelfEncodeHandler(() => {
92
+ const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
93
+ this.instance.checksum.setValue(checksum);
94
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
95
+ });
96
+ }
97
+ },
72
98
  flags: this.fieldUInt('flags', 4, 4, 'Flags'),
73
99
  sequence: this.fieldUInt('sequence', 8, 4, 'Sequence'),
74
100
  ack: this.fieldUInt('ack', 12, 4, 'Acknowledge'),
@@ -79,6 +79,7 @@ class EthernetII extends BaseHeader_1.BaseHeader {
79
79
  }
80
80
  },
81
81
  etherType: {
82
+ autoCalculated: true,
82
83
  type: 'string',
83
84
  minLength: 4,
84
85
  maxLength: 4,
@@ -104,6 +104,7 @@ class GELF extends BaseHeader_1.BaseHeader {
104
104
  //when the form is chunked; otherwise it stays unset so it neither decodes phantom values
105
105
  //nor writes bytes for the gzip/uncompressed forms.
106
106
  magic: {
107
+ default: '1e0f',
107
108
  type: 'string',
108
109
  label: 'Magic',
109
110
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
@@ -86,6 +86,7 @@ class GENEVE extends BaseHeader_1.BaseHeader {
86
86
  //Protocol Type is the EtherType of the inner frame — stored as a lowercase hex string
87
87
  //(like eth.etherType) so it can drive the `ethertype` demux dimension to the inner codec.
88
88
  protocolType: {
89
+ autoCalculated: true,
89
90
  type: 'string', label: 'Protocol Type', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
90
91
  decode: function () { this.instance.protocolType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2))); },
91
92
  encode: function () {
@@ -109,6 +109,7 @@ class GRE extends BaseHeader_1.BaseHeader {
109
109
  //Protocol Type is the EtherType of the inner frame — stored as a lowercase hex string
110
110
  //(like eth.etherType) so it can drive the `ethertype` demux dimension to the inner codec.
111
111
  protocolType: {
112
+ autoCalculated: true,
112
113
  type: 'string', label: 'Protocol Type', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
113
114
  decode: function () { this.instance.protocolType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2))); },
114
115
  encode: function () {
@@ -90,6 +90,7 @@ class GTPv2C extends BaseHeader_1.BaseHeader {
90
90
  label: 'Flags',
91
91
  properties: {
92
92
  version: {
93
+ default: 2,
93
94
  type: 'integer',
94
95
  label: 'Version',
95
96
  minimum: 0,
@@ -63,8 +63,8 @@ class Goose extends BaseHeader_1.BaseHeader {
63
63
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
64
64
  },
65
65
  encode: () => {
66
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
67
- if (length > 0) {
66
+ if (!this.isLeftOut(this.instance.length)) {
67
+ const length = this.instance.length.getValue(0);
68
68
  this.instance.length.setValue(length);
69
69
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
70
70
  }
@@ -169,7 +169,7 @@ class Goose extends BaseHeader_1.BaseHeader {
169
169
  //decoded (it may itself be inconsistent in a malformed frame).
170
170
  const rawBuffer = Buffer.from(this.instance.goosePdu.raw.getValue().toString(), 'hex');
171
171
  this.writeBytes(8, rawBuffer);
172
- if (this.instance.length.getValue() > 0)
172
+ if (!this.isLeftOut(this.instance.length))
173
173
  return;
174
174
  this.instance.length.setValue(8 + rawBuffer.length);
175
175
  return;
@@ -183,7 +183,7 @@ class Goose extends BaseHeader_1.BaseHeader {
183
183
  * Update the length only if it is not set
184
184
  * Update length(APPID's length + Length's length + Reserved1's length + Reserved2's length + goosePdu's length)
185
185
  */
186
- if (this.instance.length.getValue() > 0)
186
+ if (!this.isLeftOut(this.instance.length))
187
187
  return;
188
188
  this.instance.length.setValue(2 + 2 + 2 + 2 + goosePduBuffer.length);
189
189
  },
@@ -64,6 +64,7 @@ class HSR extends BaseHeader_1.BaseHeader {
64
64
  },
65
65
  seqNr: BaseHeader_1.BaseHeader.fieldUInt('seqNr', 2, 2, 'Sequence Number'),
66
66
  etherType: {
67
+ autoCalculated: true,
67
68
  type: 'string',
68
69
  minLength: 4,
69
70
  maxLength: 4,
@@ -61,6 +61,7 @@ class HTTP2 extends BaseHeader_1.BaseHeader {
61
61
  //The 24-byte connection preface, kept verbatim. match() requires it, so it is always
62
62
  //present on decode; the default lets a crafted frame re-emit the standard preface.
63
63
  preface: {
64
+ default: HTTP2.#PREFACE_HEX,
64
65
  type: 'string',
65
66
  label: 'Connection Preface',
66
67
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
@@ -50,10 +50,11 @@ class ICMP extends BaseHeader_1.BaseHeader {
50
50
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
51
51
  },
52
52
  encode: () => {
53
+ const leftOut = this.isLeftOut(this.instance.checksum);
53
54
  let checksum = this.instance.checksum.getValue(0);
54
55
  this.instance.checksum.setValue(checksum);
55
56
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
56
- if (!checksum) {
57
+ if (leftOut) {
57
58
  checksum = this.calculateIcmpChecksum();
58
59
  this.instance.checksum.setValue(checksum);
59
60
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));