@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
@@ -7,6 +7,7 @@ const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const BufferToNumber_1 = require("../helper/BufferToNumber");
8
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
9
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
10
+ const Crc_1 = require("../helper/Crc");
10
11
  /**
11
12
  * C37.118 — IEEE Std C37.118.2 synchrophasor data transfer (PMU/PDC), typically TCP port 4712 and UDP
12
13
  * port 4713. Every frame shares a 14-byte common header: SYNC (a 0xAA lead-in byte, then a byte holding
@@ -17,8 +18,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
17
18
  *
18
19
  * The frame body between the header and the CHK is frame-type-specific (a configuration frame defines
19
20
  * the data layout for the data frames that follow — cross-frame state, not a single-packet concern), so
20
- * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE, and keeps the CHK verbatim
21
- * (never recomputed). A well-formed frame round-trips byte-for-byte.
21
+ * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE. FRAMESIZE and CHK are written
22
+ * as given; left out, FRAMESIZE is the actual frame length and CHK the CRC-CCITT (CRC-16/CCITT-FALSE:
23
+ * polynomial 0x1021, initial 0xFFFF) of every octet before it. A well-formed frame round-trips byte-for-byte.
22
24
  */
23
25
  class C37118 extends BaseHeader_1.BaseHeader {
24
26
  constructor() {
@@ -40,7 +42,8 @@ class C37118 extends BaseHeader_1.BaseHeader {
40
42
  type: 'object',
41
43
  summary: 'C37.118 type=${sync.frameType} id=${idcode}',
42
44
  properties: {
43
- leadIn: this.fieldHex('leadIn', 0, 1, 'Lead-in (0xAA)'),
45
+ //Default 0xAA: the SYNC lead-in every frame begins with, and what match() requires.
46
+ leadIn: { ...this.fieldHex('leadIn', 0, 1, 'Lead-in (0xAA)'), default: 'aa' },
44
47
  //SYNC byte 1 (MSB first): reserved bit, 3-bit Frame Type, 4-bit Version.
45
48
  sync: {
46
49
  type: 'object',
@@ -56,8 +59,9 @@ class C37118 extends BaseHeader_1.BaseHeader {
56
59
  decode: function () { this.instance.sync.frameType.setValue(this.readBits(1, 1, 1, 3)); },
57
60
  encode: function () { this.writeBits(1, 1, 1, 3, this.instance.sync.frameType.getValue(0)); }
58
61
  },
62
+ //Default 1 (IEEE C37.118-2005), the version encode writes when it is left out.
59
63
  version: {
60
- type: 'integer', label: 'Version', minimum: 0, maximum: 15,
64
+ type: 'integer', label: 'Version', minimum: 0, maximum: 15, default: 1,
61
65
  decode: function () { this.instance.sync.version.setValue(this.readBits(1, 1, 4, 4)); },
62
66
  encode: function () { this.writeBits(1, 1, 4, 4, this.instance.sync.version.getValue(1)); }
63
67
  }
@@ -74,11 +78,20 @@ class C37118 extends BaseHeader_1.BaseHeader {
74
78
  },
75
79
  encode: function () {
76
80
  //Honored when supplied (a crafted frame may lie); else derived from the actual
77
- //body + CHK (FRAMESIZE is the total frame length: 14-byte header + body + CHK).
78
- const provided = this.instance.framesize.getValue();
79
- const value = (provided !== undefined && provided !== null)
80
- ? provided
81
- : 14 + (0, HexToBuffer_1.HexToBuffer)(this.instance.body.getValue('')).length + (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')).length;
81
+ //body + CHK (FRAMESIZE is the total frame length: 14-byte header + body + CHK, the
82
+ //CHK being 2 bytes when it is left out to be computed).
83
+ let value;
84
+ if (!this.isLeftOut(this.instance.framesize)) {
85
+ value = this.instance.framesize.getValue(0);
86
+ }
87
+ else {
88
+ const chkLength = this.isLeftOut(this.instance.chk) ? 2 : (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')).length;
89
+ value = 14 + (0, HexToBuffer_1.HexToBuffer)(this.instance.body.getValue('')).length + chkLength;
90
+ if (value > 65535) {
91
+ this.recordError(this.instance.framesize.getPath(), 'Maximum value is 65535');
92
+ value = 65535;
93
+ }
94
+ }
82
95
  this.instance.framesize.setValue(value);
83
96
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
84
97
  }
@@ -122,14 +135,20 @@ class C37118 extends BaseHeader_1.BaseHeader {
122
135
  const bodyBuffer = (0, HexToBuffer_1.HexToBuffer)(body);
123
136
  if (bodyBuffer.length)
124
137
  this.writeBytes(14, bodyBuffer);
125
- const chk = this.instance.chk.getValue('');
126
- if (chk)
127
- this.writeBytes(14 + bodyBuffer.length, (0, HexToBuffer_1.HexToBuffer)(chk));
138
+ const chkOffset = 14 + bodyBuffer.length;
139
+ if (!this.isLeftOut(this.instance.chk)) {
140
+ this.writeBytes(chkOffset, (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')));
141
+ return;
142
+ }
143
+ //Every octet before the CHK (header, FRAMESIZE included, and body) is written by now.
144
+ const chk = (0, NumberToBuffer_1.UInt16ToBuffer)((0, Crc_1.Crc16CcittFalse)(this.readBytes(0, chkOffset)));
145
+ this.instance.chk.setValue((0, BufferToHex_1.BufferToHex)(chk));
146
+ this.writeBytes(chkOffset, chk);
128
147
  }
129
148
  },
130
- //The check word (CRC-CCITT), honored verbatim (never recomputed) — populated by the body
131
- //master since its offset (FRAMESIZE - 2) is frame-length dependent.
132
- chk: { type: 'string', label: 'Check', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
149
+ //The check word (CRC-CCITT, big-endian) — encoded by the body master since its offset
150
+ //(FRAMESIZE - 2) is frame-length dependent; computed there when left out.
151
+ chk: { type: 'string', autoCalculated: true, label: 'Check', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
133
152
  }
134
153
  };
135
154
  }
@@ -9,9 +9,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
9
9
  * Capabilities, 0x0005 Software Version, 0x0006 Platform.
10
10
  *
11
11
  * The TLV values are kept verbatim as hex (device names, address lists and capability bitmaps need no
12
- * form structure), the Checksum is honored verbatim (never recomputed), and the TLV walk is bounded by
13
- * the captured bytes so a corrupt Length can't run past the frame. A well-formed CDP frame round-trips
14
- * byte-for-byte.
12
+ * form structure), and the TLV walk is bounded by the captured bytes so a corrupt Length can't run past
13
+ * the frame. The Checksum and each TLV Length are computed on encode when left out and written as given
14
+ * otherwise. A well-formed CDP frame round-trips byte-for-byte.
15
15
  */
16
16
  export declare class CDP extends BaseHeader {
17
17
  #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
  * 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).
@@ -47,8 +47,9 @@ class Codesys extends BaseHeader_1.BaseHeader {
47
47
  summary: 'CODESYS len=${length}',
48
48
  properties: {
49
49
  //The 4-byte block-driver magic (00 01 17 e8). Kept verbatim (not enum-constrained) so any
50
- //observed frame round-trips even if a variant carries a different signature.
51
- magic: this.fieldHex('magic', 0, 4, 'Block Driver Magic'),
50
+ //observed frame round-trips even if a variant carries a different signature. Default
51
+ //000117e8: what match() requires.
52
+ magic: { ...this.fieldHex('magic', 0, 4, 'Block Driver Magic'), default: Codesys.#MAGIC },
52
53
  //Block-driver payload byte count that follows the 8-byte header. LITTLE-ENDIAN 32-bit.
53
54
  //Honored when supplied (a crafted frame may lie); else derived from the payload bytes.
54
55
  length: {
@@ -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'),
@@ -53,7 +53,8 @@ class DHCP extends BaseHeader_1.BaseHeader {
53
53
  chaddr: this.fieldHex('chaddr', 28, 16, 'Client Hardware Address'),
54
54
  sname: this.fieldHex('sname', 44, 64, 'Server Host Name'),
55
55
  file: this.fieldHex('file', 108, 128, 'Boot File Name'),
56
- magicCookie: this.fieldHex('magicCookie', 236, 4, 'Magic Cookie'),
56
+ //Default 0x63825363: the DHCP Magic Cookie (RFC 2131 §3), and what match() requires.
57
+ magicCookie: { ...this.fieldHex('magicCookie', 236, 4, 'Magic Cookie'), default: '63825363' },
57
58
  options: {
58
59
  type: 'array',
59
60
  label: 'Options',
@@ -9,11 +9,18 @@ import { DemuxProducer } from '../types/DemuxProducer';
9
9
  * Source address, and a header CRC. The user data that follows is carried in blocks of up to 16 octets
10
10
  * each with its own trailing 2-byte CRC.
11
11
  *
12
- * This codec decodes the link header structurally and keeps the data-block region (transport header +
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.
12
+ * This codec decodes the link header structurally and carries the data-block region in one of two hex
13
+ * fields. `userData` is the user data with the block CRCs removed (transport header + application data,
14
+ * not decoded further: reassembling the transport/application layer is a cross-frame concern); `payload`
15
+ * is the region verbatim, block CRCs included. Decode emits exactly one of them: `userData` when the
16
+ * region is complete and every block CRC verifies, otherwise `payload` plus a warning naming each bad or
17
+ * truncated block. On encode a given `payload` wins and is written as given (how a frame with a wrong
18
+ * block CRC is crafted; a `userData` given alongside it is ignored with a warning); otherwise `userData`
19
+ * is split into 16-octet blocks, each followed by its CRC-16/DNP, low octet first. Length and the header
20
+ * CRC are written as given; left out, Length counts the user data octets (the block CRCs excluded) and
21
+ * the header CRC is computed (CRC-16/DNP over the first 8 octets). A frame round-trips byte-for-byte
22
+ * either way; the frame's extent is derived from the Length field so a pipelined/trailing frame is left
23
+ * to RawData.
17
24
  */
18
25
  export declare class DNP3 extends BaseHeader {
19
26
  #private;