@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,14 +6,17 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
6
6
  const BufferToIP_1 = require("../helper/BufferToIP");
7
7
  const IPToBuffer_1 = require("../helper/IPToBuffer");
8
8
  const BufferToHex_1 = require("../helper/BufferToHex");
9
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
10
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
11
+ const Checksum_1 = require("../helper/Checksum");
9
12
  /**
10
13
  * OSPF — Open Shortest Path First (OSPFv2, RFC 2328), carried directly on IP as protocol 89. This codec
11
14
  * decodes the 24-octet common header: an 8-bit Version (`version`, offset 0), an 8-bit Message Type
12
15
  * (`type`, offset 1; 1 Hello, 2 Database Description, 3 LS Request, 4 LS Update, 5 LS Ack), a 16-bit
13
16
  * Packet Length (`packetLength`, offset 2), the Source Router ID (`routerId`, IPv4, offset 4), the Area
14
- * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12, honored verbatim and never
15
- * recomputed so a captured packet round-trips), a 16-bit Auth Type (`auType`, offset 14) and the 8-octet
16
- * Authentication field (`auth`, hex, offset 16).
17
+ * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12), a 16-bit Auth Type (`auType`,
18
+ * offset 14) and the 8-octet Authentication field (`auth`, hex, offset 16). Packet Length and Checksum
19
+ * are computed on encode when left out and written as given otherwise.
17
20
  *
18
21
  * For a Hello (type 1) the body is decoded field-by-field: Network Mask (offset 24), 16-bit Hello Interval
19
22
  * (offset 28), 8-bit Options (offset 30), 8-bit Router Priority (offset 31), 32-bit Router Dead Interval
@@ -69,6 +72,19 @@ class OSPF extends BaseHeader_1.BaseHeader {
69
72
  end = 24;
70
73
  return end;
71
74
  }
75
+ /**
76
+ * The OSPFv2 checksum (RFC 2328 §A.3.1): the Internet checksum of the whole packet except the 64-bit
77
+ * Authentication field (bytes 16-23). With cryptographic authentication (AuType 2) the checksum is
78
+ * not calculated and is 0 (§D.4.3). Called once every field is written, with the checksum still zero.
79
+ * @private
80
+ */
81
+ #calculateChecksum() {
82
+ const auType = this.instance.auType.getValue(0);
83
+ if (auType === 2)
84
+ return 0;
85
+ const packet = this.packet.subarray(this.startPos, this.endPos);
86
+ return (0, Checksum_1.InternetChecksum)(packet.subarray(0, 16), packet.subarray(24));
87
+ }
72
88
  static #schemaCache;
73
89
  //Class-cached SCHEMA (field closures are plain functions taking dynamic `this` via .call(this)).
74
90
  get SCHEMA() {
@@ -82,7 +98,32 @@ class OSPF extends BaseHeader_1.BaseHeader {
82
98
  //==== Common header (24 bytes, RFC 2328 §A.3.1) ====
83
99
  version: this.fieldUInt('version', 0, 1, 'Version'),
84
100
  type: this.fieldUInt('type', 1, 1, 'Message Type'),
85
- packetLength: this.fieldUInt('packetLength', 2, 2, 'Packet Length'),
101
+ //The whole OSPF packet including this header; left out, the bytes actually written, filled
102
+ //once every field is encoded (before the checksum); a given value is written as given.
103
+ packetLength: {
104
+ type: 'integer',
105
+ autoCalculated: true,
106
+ label: 'Packet Length',
107
+ minimum: 0,
108
+ maximum: 65535,
109
+ decode: function () {
110
+ this.instance.packetLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
111
+ },
112
+ encode: function () {
113
+ if (!this.isLeftOut(this.instance.packetLength)) {
114
+ const packetLength = this.instance.packetLength.getValue(0);
115
+ this.instance.packetLength.setValue(packetLength);
116
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(packetLength));
117
+ return;
118
+ }
119
+ this.instance.packetLength.setValue(0);
120
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
121
+ this.addPostSelfEncodeHandler(() => {
122
+ this.instance.packetLength.setValue(this.length);
123
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(this.length));
124
+ }, 1);
125
+ }
126
+ },
86
127
  routerId: {
87
128
  type: 'string',
88
129
  label: 'Source Router ID',
@@ -113,9 +154,33 @@ class OSPF extends BaseHeader_1.BaseHeader {
113
154
  this.writeBytes(8, (0, IPToBuffer_1.IPv4ToBuffer)(areaId));
114
155
  }
115
156
  },
116
- //Honored verbatim: the OSPF checksum (ones-complement over the packet excluding the
117
- //authentication field) is never recomputed, so a captured packet round-trips byte-for-byte.
118
- checksum: this.fieldUInt('checksum', 12, 2, 'Checksum'),
157
+ //Left out, computed after the Packet Length once every field is written
158
+ //(#calculateChecksum); a given value is written as given.
159
+ checksum: {
160
+ type: 'integer',
161
+ autoCalculated: true,
162
+ label: 'Checksum',
163
+ minimum: 0,
164
+ maximum: 65535,
165
+ decode: function () {
166
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2)));
167
+ },
168
+ encode: function () {
169
+ if (!this.isLeftOut(this.instance.checksum)) {
170
+ const checksum = this.instance.checksum.getValue(0);
171
+ this.instance.checksum.setValue(checksum);
172
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
173
+ return;
174
+ }
175
+ this.instance.checksum.setValue(0);
176
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
177
+ this.addPostSelfEncodeHandler(() => {
178
+ const checksum = this.#calculateChecksum();
179
+ this.instance.checksum.setValue(checksum);
180
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
181
+ }, 2);
182
+ }
183
+ },
119
184
  auType: this.fieldUInt('auType', 14, 2, 'Auth Type'),
120
185
  auth: {
121
186
  type: 'string',
@@ -11,9 +11,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
11
11
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
12
12
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
13
13
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
14
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
15
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
16
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
14
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
15
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
16
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
17
17
  */
18
18
  export declare class PIM extends BaseHeader {
19
19
  #private;
@@ -4,6 +4,9 @@ exports.PIM = 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");
8
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
7
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
8
11
  /**
9
12
  * PIM — Protocol Independent Multicast, Sparse Mode version 2 (RFC 7761), carried directly over IP as
@@ -15,9 +18,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
15
18
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
16
19
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
17
20
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
18
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
19
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
20
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
22
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
23
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
24
  */
22
25
  class PIM extends BaseHeader_1.BaseHeader {
23
26
  constructor() {
@@ -57,6 +60,27 @@ class PIM extends BaseHeader_1.BaseHeader {
57
60
  }
58
61
  return available < 0 ? 0 : available;
59
62
  }
63
+ /**
64
+ * The PIM checksum (RFC 7761 §4.9): the Internet checksum of the whole PIM message, except that a
65
+ * Register (type 1) covers only its 8-byte header, not the encapsulated data packet. Over IPv6 the
66
+ * IPv6 pseudo-header (next header 103, upper-layer length = the bytes covered) is prepended; over
67
+ * IPv4 there is none. Called once every field is written, with the checksum field still zero.
68
+ * @private
69
+ */
70
+ #calculateChecksum() {
71
+ const message = this.packet.subarray(this.startPos, this.endPos);
72
+ const type = this.instance.type.getValue(0);
73
+ const covered = type === 1 ? message.subarray(0, 8) : message;
74
+ const ip = this.nearestIPModule();
75
+ if (!ip || ip.id !== 'ipv6')
76
+ return (0, Checksum_1.InternetChecksum)(covered);
77
+ const pseudoHeader = this.ipPseudoHeader(103, covered.length);
78
+ if (!pseudoHeader) {
79
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute PIM checksum: the IPv6 addresses beneath are unusable');
80
+ return 0;
81
+ }
82
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, covered);
83
+ }
60
84
  static #buildSchema() {
61
85
  return {
62
86
  type: 'object',
@@ -68,6 +92,7 @@ class PIM extends BaseHeader_1.BaseHeader {
68
92
  label: 'Version',
69
93
  minimum: 0,
70
94
  maximum: 15,
95
+ default: 2,
71
96
  decode: function () {
72
97
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
73
98
  },
@@ -94,9 +119,33 @@ class PIM extends BaseHeader_1.BaseHeader {
94
119
  //Byte 1: Reserved (0 on transmit for most types, a flags/subtype byte for a few). Kept
95
120
  //verbatim and re-emitted so a non-canonical frame is still byte-perfect.
96
121
  reserved: this.fieldHex('reserved', 1, 1, 'Reserved'),
97
- //Bytes 2-3: Checksum (ones-complement over the PIM message). Honored verbatim, never
98
- //recomputed, so a captured message round-trips byte-for-byte.
99
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
122
+ //Bytes 2-3: Checksum, computed once every field is written when left out (#calculateChecksum);
123
+ //a given value is written as given.
124
+ checksum: {
125
+ type: 'integer',
126
+ autoCalculated: true,
127
+ label: 'Checksum',
128
+ minimum: 0,
129
+ maximum: 65535,
130
+ decode: function () {
131
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
132
+ },
133
+ encode: function () {
134
+ if (!this.isLeftOut(this.instance.checksum)) {
135
+ const checksum = this.instance.checksum.getValue(0);
136
+ this.instance.checksum.setValue(checksum);
137
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
138
+ return;
139
+ }
140
+ this.instance.checksum.setValue(0);
141
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
142
+ this.addPostSelfEncodeHandler(() => {
143
+ const checksum = this.#calculateChecksum();
144
+ this.instance.checksum.setValue(checksum);
145
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
146
+ });
147
+ }
148
+ },
100
149
  //The type-specific body after the 4-byte header, kept verbatim. Bounded by the enclosing
101
150
  //IP payload (#available) so trailing FCS / pipelined data is left to the codec's recursion.
102
151
  body: {
@@ -46,6 +46,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
46
46
  //Byte 0 high nibble: PPPoE Version (1). Written MSB-first; shares byte 0 with Type
47
47
  //(writeBits masks each field so they never clobber).
48
48
  version: {
49
+ default: 1,
49
50
  type: 'integer', label: 'Version', minimum: 0, maximum: 15,
50
51
  decode: function () {
51
52
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
@@ -58,6 +59,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
58
59
  },
59
60
  //Byte 0 low nibble: PPPoE Type (1).
60
61
  type: {
62
+ default: 1,
61
63
  type: 'integer', label: 'Type', minimum: 0, maximum: 15,
62
64
  decode: function () {
63
65
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
@@ -106,7 +106,9 @@ class PTP extends BaseHeader_1.BaseHeader {
106
106
  type: 'integer',
107
107
  autoCalculated: true,
108
108
  label: 'Message Length',
109
- minimum: 34,
109
+ //The field floor, not the 34-byte header: a given value is written as given
110
+ //(flagged when too short) so a malformed message can be crafted.
111
+ minimum: 0,
110
112
  maximum: 65535,
111
113
  decode: function () {
112
114
  this.instance.messageLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
@@ -122,6 +124,8 @@ class PTP extends BaseHeader_1.BaseHeader {
122
124
  this.recordError(this.instance.messageLength.getPath(), 'Maximum value is 65535');
123
125
  value = 65535;
124
126
  }
127
+ if (value < 34)
128
+ this.recordError(this.instance.messageLength.getPath(), 'Message length should be at least 34');
125
129
  this.instance.messageLength.setValue(value);
126
130
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
127
131
  }
@@ -44,6 +44,8 @@ class RDP extends BaseHeader_1.BaseHeader {
44
44
  flags: this.fieldUInt('flags', 1, 1, 'Flags'),
45
45
  //2-byte LITTLE-ENDIAN length — always 8 for the Negotiation. Honored verbatim.
46
46
  length: {
47
+ //Always 8 for the Negotiation structure; left out, encode writes 8.
48
+ autoCalculated: true,
47
49
  type: 'integer', label: 'Length', minimum: 0, maximum: 65535,
48
50
  decode: function () {
49
51
  const bytes = this.readBytes(2, 2);
@@ -121,6 +121,7 @@ class RGoose extends BaseHeader_1.BaseHeader {
121
121
  }
122
122
  },
123
123
  commonHeaderId: {
124
+ default: 0x80,
124
125
  type: 'integer', label: 'Common Session Header Id', minimum: 0, maximum: 255,
125
126
  decode: function () { this.instance.commonHeaderId.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(2, 1))); },
126
127
  encode: function () {
@@ -166,6 +167,7 @@ class RGoose extends BaseHeader_1.BaseHeader {
166
167
  },
167
168
  spduNumber: this.fieldUInt('spduNumber', 8, 4, 'SPDU Number'),
168
169
  version: {
170
+ default: 1,
169
171
  type: 'integer', label: 'Version', minimum: 0, maximum: 65535,
170
172
  decode: function () { this.instance.version.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2))); },
171
173
  encode: function () {
@@ -12,9 +12,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
12
12
  *
13
13
  * The object contents are class/C-Type specific and several (SESSION, SENDER_TSPEC, FLOWSPEC, …) need
14
14
  * cross-object and policy context, so this codec keeps each object's contents verbatim as `data` hex
15
- * (byte-perfect) and does not sub-decode them. The RSVP Length is honored verbatim when supplied on
16
- * encode (a crafted message may lie) and derived from the objects otherwise; each object's Length is
17
- * likewise honored-else-derived. The Checksum is honored verbatim, never recomputed. Object walking is
15
+ * (byte-perfect) and does not sub-decode them. The RSVP Length, each object's Length and the Checksum
16
+ * are computed on encode when left out (the Length and Checksum over the bytes actually written), and
17
+ * written as given otherwise, 0 included (a crafted message may lie). Object walking is
18
18
  * doubly bounded — by the RSVP Length field and by the bytes the IP layer actually made available — so a
19
19
  * lying length can't read past the datagram, and trailing / pipelined bytes are left to the codec's
20
20
  * recursion / RawData. A well-formed message round-trips byte-for-byte.
@@ -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,
@@ -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',
@@ -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
  }
@@ -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