@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
@@ -27,11 +27,13 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
27
27
  this.SCHEMA = {
28
28
  type: 'object',
29
29
  properties: {
30
+ //Default 0x68: the start byte of every IEC 104 APDU, and what match() requires.
30
31
  startByte: {
31
32
  type: 'integer',
32
33
  label: 'Start Byte',
33
34
  minimum: 0,
34
35
  maximum: 255,
36
+ default: 104,
35
37
  decode: () => {
36
38
  this.instance.startByte.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
37
39
  },
@@ -39,8 +41,11 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
39
41
  this.writeBytes(0, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.startByte.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'))));
40
42
  }
41
43
  },
44
+ //The octet count after this field (the 4-octet control field). Written as given; left out,
45
+ //it is what this layer writes after it.
42
46
  apduLength: {
43
47
  type: 'integer',
48
+ autoCalculated: true,
44
49
  label: 'APDU Length',
45
50
  minimum: 0,
46
51
  maximum: 255,
@@ -48,7 +53,20 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
48
53
  this.instance.apduLength.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
49
54
  },
50
55
  encode: () => {
51
- this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.apduLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'))));
56
+ if (!this.isLeftOut(this.instance.apduLength)) {
57
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.apduLength.getValue(0)));
58
+ return;
59
+ }
60
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(0));
61
+ this.addPostSelfEncodeHandler(() => {
62
+ let apduLength = this.length - 2;
63
+ if (apduLength > 255) {
64
+ this.recordError(this.instance.apduLength.getPath(), 'Maximum value is 255');
65
+ apduLength = 255;
66
+ }
67
+ this.instance.apduLength.setValue(apduLength);
68
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(apduLength));
69
+ });
52
70
  }
53
71
  },
54
72
  controlField: {
@@ -28,11 +28,13 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
28
28
  this.SCHEMA = {
29
29
  type: 'object',
30
30
  properties: {
31
+ //Default 0x68: the start byte of every IEC 104 APDU, and what match() requires.
31
32
  startByte: {
32
33
  type: 'integer',
33
34
  label: 'Start Byte',
34
35
  minimum: 0,
35
36
  maximum: 255,
37
+ default: 104,
36
38
  decode: () => {
37
39
  this.instance.startByte.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
38
40
  },
@@ -40,8 +42,11 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
40
42
  this.writeBytes(0, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.startByte.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'))));
41
43
  }
42
44
  },
45
+ //The octet count after this field (the 4-octet control field). Written as given; left out,
46
+ //it is what this layer writes after it.
43
47
  apduLength: {
44
48
  type: 'integer',
49
+ autoCalculated: true,
45
50
  label: 'APDU Length',
46
51
  minimum: 0,
47
52
  maximum: 255,
@@ -49,7 +54,20 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
49
54
  this.instance.apduLength.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
50
55
  },
51
56
  encode: () => {
52
- this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.apduLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'))));
57
+ if (!this.isLeftOut(this.instance.apduLength)) {
58
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.apduLength.getValue(0)));
59
+ return;
60
+ }
61
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(0));
62
+ this.addPostSelfEncodeHandler(() => {
63
+ let apduLength = this.length - 2;
64
+ if (apduLength > 255) {
65
+ this.recordError(this.instance.apduLength.getPath(), 'Maximum value is 255');
66
+ apduLength = 255;
67
+ }
68
+ this.instance.apduLength.setValue(apduLength);
69
+ this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(apduLength));
70
+ });
53
71
  }
54
72
  },
55
73
  controlField: {
@@ -42,8 +42,8 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
42
42
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
43
43
  },
44
44
  encode: () => {
45
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
46
- if (length > 0) {
45
+ if (!this.isLeftOut(this.instance.length)) {
46
+ const length = this.instance.length.getValue(0);
47
47
  this.instance.length.setValue(length);
48
48
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
49
49
  }
@@ -132,7 +132,7 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
132
132
  //the original bytes means keeping it).
133
133
  const rawBuffer = Buffer.from(this.instance.svPdu.raw.getValue().toString(), 'hex');
134
134
  this.writeBytes(8, rawBuffer);
135
- if (this.instance.length.getValue() > 0)
135
+ if (!this.isLeftOut(this.instance.length))
136
136
  return;
137
137
  this.instance.length.setValue(8 + rawBuffer.length);
138
138
  return;
@@ -146,7 +146,7 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
146
146
  * Update the length only if it is not set
147
147
  * Update length(APPID's length + Length's length + Reserved1's length + Reserved2's length + svPdu's length)
148
148
  */
149
- if (this.instance.length.getValue() > 0)
149
+ if (!this.isLeftOut(this.instance.length))
150
150
  return;
151
151
  this.instance.length.setValue(2 + 2 + 2 + 2 + svPduBuffer.length);
152
152
  },
@@ -8,16 +8,17 @@ import { DemuxProducer } from '../types/DemuxProducer';
8
8
  *
9
9
  * - v1/v2 (Membership Query with total length 8 / Report 0x12 / Report 0x16 / Leave 0x17): a single
10
10
  * 4-byte Group Address (dotted-quad).
11
- * - v3 Membership Query (Type 0x11 with an IP payload ≥ 12 bytes): Group Address (4) + Resv/S/QRV (1) +
12
- * QQIC (1) + Number of Sources (2) + that many 4-byte source addresses, kept verbatim as `sources`
13
- * hex (bounded by the source count and the IP payload).
11
+ * - v3 Membership Query (Type 0x11 with an IP payload ≥ 12 bytes; on encode, Type 0x11 with any v3
12
+ * field given): Group Address (4) + Resv/S/QRV (1) + QQIC (1) + Number of Sources (2) + that many
13
+ * 4-byte source addresses, kept verbatim as `sources` hex (bounded by the source count and the IP
14
+ * payload).
14
15
  * - v3 Membership Report (Type 0x22): 2 reserved bytes + Number of Group Records (2) + the group
15
16
  * records, kept verbatim as `records` hex (bounded by the IP payload).
16
17
  *
17
- * The Checksum is honored verbatim, never recomputed (encode is a faithful executor); a well-formed
18
- * message round-trips byte-for-byte. The message is bounded by the enclosing IPv4 datagram (total
19
- * length − IP header length) so trailing padding is left to the codec's recursion / RawData. The v3
20
- * source/record counts are honor-else-derive; the v3 record structures are kept as bounded hex.
18
+ * The Checksum is computed over the whole IGMP message when left out, else written as given; a
19
+ * well-formed message round-trips byte-for-byte. The message is bounded by the enclosing IPv4 datagram
20
+ * (total length − IP header length) so trailing padding is left to the codec's recursion / RawData. The
21
+ * v3 source count is honor-else-derive; the v3 record structures are kept as bounded hex.
21
22
  */
22
23
  export declare class IGMP extends BaseHeader {
23
24
  #private;
@@ -8,6 +8,7 @@ const BufferToIP_1 = require("../helper/BufferToIP");
8
8
  const IPToBuffer_1 = require("../helper/IPToBuffer");
9
9
  const BufferToNumber_1 = require("../helper/BufferToNumber");
10
10
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
11
+ const Checksum_1 = require("../helper/Checksum");
11
12
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
12
13
  /**
13
14
  * IGMP — Internet Group Management Protocol (IGMPv2 RFC 2236, IGMPv3 RFC 3376), carried directly over
@@ -16,16 +17,17 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
16
17
  *
17
18
  * - v1/v2 (Membership Query with total length 8 / Report 0x12 / Report 0x16 / Leave 0x17): a single
18
19
  * 4-byte Group Address (dotted-quad).
19
- * - v3 Membership Query (Type 0x11 with an IP payload ≥ 12 bytes): Group Address (4) + Resv/S/QRV (1) +
20
- * QQIC (1) + Number of Sources (2) + that many 4-byte source addresses, kept verbatim as `sources`
21
- * hex (bounded by the source count and the IP payload).
20
+ * - v3 Membership Query (Type 0x11 with an IP payload ≥ 12 bytes; on encode, Type 0x11 with any v3
21
+ * field given): Group Address (4) + Resv/S/QRV (1) + QQIC (1) + Number of Sources (2) + that many
22
+ * 4-byte source addresses, kept verbatim as `sources` hex (bounded by the source count and the IP
23
+ * payload).
22
24
  * - v3 Membership Report (Type 0x22): 2 reserved bytes + Number of Group Records (2) + the group
23
25
  * records, kept verbatim as `records` hex (bounded by the IP payload).
24
26
  *
25
- * The Checksum is honored verbatim, never recomputed (encode is a faithful executor); a well-formed
26
- * message round-trips byte-for-byte. The message is bounded by the enclosing IPv4 datagram (total
27
- * length − IP header length) so trailing padding is left to the codec's recursion / RawData. The v3
28
- * source/record counts are honor-else-derive; the v3 record structures are kept as bounded hex.
27
+ * The Checksum is computed over the whole IGMP message when left out, else written as given; a
28
+ * well-formed message round-trips byte-for-byte. The message is bounded by the enclosing IPv4 datagram
29
+ * (total length − IP header length) so trailing padding is left to the codec's recursion / RawData. The
30
+ * v3 source count is honor-else-derive; the v3 record structures are kept as bounded hex.
29
31
  */
30
32
  class IGMP extends BaseHeader_1.BaseHeader {
31
33
  constructor() {
@@ -65,6 +67,18 @@ class IGMP extends BaseHeader_1.BaseHeader {
65
67
  const type = this.instance.type.getValue(0);
66
68
  return type === 0x11 && this.#available() >= 12;
67
69
  }
70
+ /**
71
+ * Encode-side counterpart of #isV3Query(): Type 0x11 with any v3 Query field given. The shape comes
72
+ * from the message's own fields, never from the IP length, which is not computed yet when IGMP
73
+ * encodes (it is filled in a post-packet handler once every layer is written).
74
+ */
75
+ #encodesV3Query() {
76
+ const type = this.instance.type.getValue(0);
77
+ if (type !== 0x11)
78
+ return false;
79
+ return !this.isLeftOut(this.instance.resvSQRV) || !this.isLeftOut(this.instance.qqic)
80
+ || !this.isLeftOut(this.instance.numSources) || !this.isLeftOut(this.instance.sources);
81
+ }
68
82
  /** True when this is an IGMPv3 Membership Report (Type 0x22): reserved + group records, no group address. */
69
83
  #isV3Report() {
70
84
  const type = this.instance.type.getValue(0);
@@ -78,9 +92,33 @@ class IGMP extends BaseHeader_1.BaseHeader {
78
92
  //==== Fixed 4-byte header (RFC 2236 §2, RFC 3376 §4) ====
79
93
  type: this.fieldUInt('type', 0, 1, 'Type'),
80
94
  maxRespCode: this.fieldUInt('maxRespCode', 1, 1, 'Max Resp Code'),
81
- //Honored verbatim: the IGMP checksum (ones-complement over the whole message) is never
82
- //recomputed, so a captured packet round-trips byte-for-byte even if it lies.
83
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
95
+ //Internet checksum over the whole IGMP message (RFC 2236 §2.3, RFC 3376 §4.1.2), computed
96
+ //once every field is written when left out; a given value is written as given.
97
+ checksum: {
98
+ type: 'integer',
99
+ autoCalculated: true,
100
+ label: 'Checksum',
101
+ minimum: 0,
102
+ maximum: 65535,
103
+ decode: function () {
104
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
105
+ },
106
+ encode: function () {
107
+ if (!this.isLeftOut(this.instance.checksum)) {
108
+ const checksum = this.instance.checksum.getValue(0);
109
+ this.instance.checksum.setValue(checksum);
110
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
111
+ return;
112
+ }
113
+ this.instance.checksum.setValue(0);
114
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
115
+ this.addPostSelfEncodeHandler(() => {
116
+ const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
117
+ this.instance.checksum.setValue(checksum);
118
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
119
+ });
120
+ }
121
+ },
84
122
  //==== Group Address (offset 4): present for every type except the v3 Report (0x22),
85
123
  //whose bytes 4-7 are reserved + record count instead. ====
86
124
  groupAddress: {
@@ -102,7 +140,8 @@ class IGMP extends BaseHeader_1.BaseHeader {
102
140
  this.writeBytes(4, (0, IPToBuffer_1.IPv4ToBuffer)(groupAddress));
103
141
  }
104
142
  },
105
- //==== IGMPv3 Membership Query body (RFC 3376 §4.1), decoded only when #isV3Query() ====
143
+ //==== IGMPv3 Membership Query body (RFC 3376 §4.1), decoded only when #isV3Query() and
144
+ //encoded only when #encodesV3Query() ====
106
145
  resvSQRV: {
107
146
  type: 'integer',
108
147
  label: 'Resv/S/QRV',
@@ -114,7 +153,7 @@ class IGMP extends BaseHeader_1.BaseHeader {
114
153
  this.instance.resvSQRV.setValue(this.readBytes(8, 1)[0] ?? 0);
115
154
  },
116
155
  encode: function () {
117
- if (!this.#isV3Query())
156
+ if (!this.#encodesV3Query())
118
157
  return;
119
158
  this.writeBytes(8, Buffer.from([this.instance.resvSQRV.getValue(0) & 0xff]));
120
159
  }
@@ -130,7 +169,7 @@ class IGMP extends BaseHeader_1.BaseHeader {
130
169
  this.instance.qqic.setValue(this.readBytes(9, 1)[0] ?? 0);
131
170
  },
132
171
  encode: function () {
133
- if (!this.#isV3Query())
172
+ if (!this.#encodesV3Query())
134
173
  return;
135
174
  this.writeBytes(9, Buffer.from([this.instance.qqic.getValue(0) & 0xff]));
136
175
  }
@@ -147,14 +186,13 @@ class IGMP extends BaseHeader_1.BaseHeader {
147
186
  this.instance.numSources.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(10, 2)));
148
187
  },
149
188
  encode: function () {
150
- if (!this.#isV3Query())
189
+ if (!this.#encodesV3Query())
151
190
  return;
152
191
  //honor-else-derive: honored when supplied (a crafted message may lie); else the
153
192
  //number of 4-byte source addresses carried in the `sources` hex.
154
- const provided = this.instance.numSources.getValue();
155
- let value = (provided !== undefined && provided !== null)
156
- ? provided
157
- : Math.floor((0, HexToBuffer_1.HexToBuffer)(this.instance.sources.getValue('')).length / 4);
193
+ let value = this.isLeftOut(this.instance.numSources)
194
+ ? Math.floor((0, HexToBuffer_1.HexToBuffer)(this.instance.sources.getValue('')).length / 4)
195
+ : this.instance.numSources.getValue(0);
158
196
  if (value > 65535) {
159
197
  this.recordError(this.instance.numSources.getPath(), 'Maximum value is 65535');
160
198
  value = 65535;
@@ -184,7 +222,7 @@ class IGMP extends BaseHeader_1.BaseHeader {
184
222
  this.instance.sources.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(12, byteLength)));
185
223
  },
186
224
  encode: function () {
187
- if (!this.#isV3Query())
225
+ if (!this.#encodesV3Query())
188
226
  return;
189
227
  const sources = this.instance.sources.getValue('');
190
228
  if (sources)
@@ -59,7 +59,7 @@ class IPFIX extends BaseHeader_1.BaseHeader {
59
59
  //honored (a crafted message may lie); otherwise a placeholder is written now and
60
60
  //the real total is filled in after every Set has encoded (post-self-encode).
61
61
  const messageLength = this.instance.length.getValue(0);
62
- if (messageLength) {
62
+ if (!this.isLeftOut(this.instance.length)) {
63
63
  this.instance.length.setValue(messageLength);
64
64
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(messageLength));
65
65
  }
@@ -56,6 +56,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
56
56
  summary: '${sip} → ${dip}',
57
57
  properties: {
58
58
  version: {
59
+ default: 4,
59
60
  type: 'integer',
60
61
  label: 'Version',
61
62
  minimum: 0,
@@ -80,24 +81,25 @@ class IPv4 extends BaseHeader_1.BaseHeader {
80
81
  autoCalculated: true,
81
82
  label: 'Header Length',
82
83
  //The field floor (IHL 0), not the protocol minimum of 20: a decoded malformed header (IHL
83
- //1–4) must re-encode. 0 means "compute"; a value below 20 is written as given, flagged.
84
+ //0–4) must re-encode. Left out means "compute"; a value below 20 is written as given, flagged.
84
85
  minimum: 0,
85
86
  maximum: 60,
86
87
  decode: function () {
87
88
  this.instance.hdrLen.setValue(this.readBits(0, 1, 4, 4) * 4);
88
89
  },
89
90
  encode: function () {
90
- let headerLength = this.instance.hdrLen.getValue(0);
91
- if (headerLength && headerLength < 20)
92
- this.recordError(this.instance.hdrLen.getPath(), 'Header length should be at least 20');
93
- if (headerLength)
94
- headerLength = headerLength ? Math.floor(headerLength / 4) : 0;
95
- this.writeBits(0, 1, 4, 4, headerLength);
96
- if (!headerLength)
97
- this.addPostSelfEncodeHandler(() => {
98
- this.instance.hdrLen.setValue(this.length);
99
- this.writeBits(0, 1, 4, 4, Math.floor(this.length / 4));
100
- }, 10);
91
+ if (!this.isLeftOut(this.instance.hdrLen)) {
92
+ const headerLength = this.instance.hdrLen.getValue(0);
93
+ if (headerLength < 20)
94
+ this.recordError(this.instance.hdrLen.getPath(), 'Header length should be at least 20');
95
+ this.writeBits(0, 1, 4, 4, Math.floor(headerLength / 4));
96
+ return;
97
+ }
98
+ this.writeBits(0, 1, 4, 4, 0);
99
+ this.addPostSelfEncodeHandler(() => {
100
+ this.instance.hdrLen.setValue(this.length);
101
+ this.writeBits(0, 1, 4, 4, Math.floor(this.length / 4));
102
+ }, 10);
101
103
  }
102
104
  },
103
105
  dsfield: {
@@ -142,13 +144,12 @@ class IPv4 extends BaseHeader_1.BaseHeader {
142
144
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
143
145
  },
144
146
  encode: function () {
145
- //This field's real value needs down stream codec invoke recode to fill
146
- const length = this.instance.length.getValue(0);
147
- if (length) {
148
- this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
147
+ //Left out: the total length is only known once every layer above is encoded
148
+ if (!this.isLeftOut(this.instance.length)) {
149
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
149
150
  }
150
151
  else {
151
- this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
152
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
152
153
  this.addPostPacketEncodeHandler(() => {
153
154
  let startCount = false;
154
155
  let totalLength = 0;
@@ -196,6 +197,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
196
197
  }
197
198
  },
198
199
  df: {
200
+ default: 1,
199
201
  type: 'integer',
200
202
  enum: [0, 1],
201
203
  label: 'Don\'t fragment',
@@ -252,6 +254,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
252
254
  }
253
255
  },
254
256
  protocol: {
257
+ autoCalculated: true,
255
258
  type: 'integer',
256
259
  label: 'Protocol',
257
260
  minimum: 0,
@@ -275,16 +278,16 @@ class IPv4 extends BaseHeader_1.BaseHeader {
275
278
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(10, 2)));
276
279
  },
277
280
  encode: function () {
278
- let checksum = this.instance.checksum.getValue(0);
279
- checksum = checksum > 65535 ? 65535 : checksum;
280
- checksum = checksum < 0 ? 0 : checksum;
281
- if (checksum) {
281
+ if (!this.isLeftOut(this.instance.checksum)) {
282
+ let checksum = this.instance.checksum.getValue(0);
283
+ checksum = checksum > 65535 ? 65535 : checksum;
284
+ checksum = checksum < 0 ? 0 : checksum;
282
285
  this.instance.checksum.setValue(checksum);
283
286
  this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
284
287
  }
285
288
  else {
286
- this.instance.checksum.setValue(checksum);
287
- this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
289
+ this.instance.checksum.setValue(0);
290
+ this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
288
291
  this.addPostPacketEncodeHandler(() => {
289
292
  const calcChecksum = this.calculateIPv4Checksum(this.packet.subarray(this.startPos, this.endPos));
290
293
  this.instance.checksum.setValue(calcChecksum);
@@ -31,6 +31,7 @@ class IPv6 extends BaseHeader_1.BaseHeader {
31
31
  type: 'object',
32
32
  properties: {
33
33
  version: {
34
+ default: 6,
34
35
  type: 'integer',
35
36
  label: 'Version',
36
37
  minimum: 0,
@@ -102,10 +103,11 @@ class IPv6 extends BaseHeader_1.BaseHeader {
102
103
  this.instance.plen.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(4, 2)));
103
104
  },
104
105
  encode: () => {
106
+ const leftOut = this.isLeftOut(this.instance.plen);
105
107
  const plen = this.instance.plen.getValue(0);
106
108
  this.instance.plen.setValue(plen);
107
109
  this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(plen));
108
- if (!plen) {
110
+ if (leftOut) {
109
111
  this.addPostPacketEncodeHandler(() => {
110
112
  let startCount = false;
111
113
  let totalLength = 0;
@@ -126,6 +128,7 @@ class IPv6 extends BaseHeader_1.BaseHeader {
126
128
  }
127
129
  },
128
130
  nxt: {
131
+ autoCalculated: true,
129
132
  type: 'integer',
130
133
  label: 'Next Header',
131
134
  minimum: 0,
@@ -45,6 +45,7 @@ class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
45
45
  type: 'object',
46
46
  properties: {
47
47
  nxt: {
48
+ autoCalculated: true,
48
49
  type: 'integer',
49
50
  label: 'Next Header',
50
51
  minimum: 0,
@@ -68,10 +69,11 @@ class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
68
69
  this.instance.len.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
69
70
  },
70
71
  encode: () => {
72
+ const leftOut = this.isLeftOut(this.instance.len);
71
73
  const len = this.instance.len.getValue(0);
72
74
  this.instance.len.setValue(len);
73
75
  this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(len));
74
- if (!len) {
76
+ if (leftOut) {
75
77
  this.addPostSelfEncodeHandler(() => {
76
78
  //Total header = 2 (nxt+len) + options area, and must be an integral
77
79
  //multiple of 8 octets (RFC 8200 §4.3). items encode already padded the
@@ -67,6 +67,7 @@ class ISAKMP extends BaseHeader_1.BaseHeader {
67
67
  label: 'Version',
68
68
  properties: {
69
69
  major: {
70
+ default: 2,
70
71
  type: 'integer', label: 'Major', minimum: 0, maximum: 15,
71
72
  decode: function () { this.instance.version.major.setValue(this.readBits(17, 1, 0, 4)); },
72
73
  encode: function () { this.writeBits(17, 1, 0, 4, this.instance.version.major.getValue(2)); }
@@ -136,6 +136,7 @@ class Kerberos extends BaseHeader_1.BaseHeader {
136
136
  //The message-type application tag (0x6a AS-REQ … 0x7e KRB-ERROR). It is the outer BER tag
137
137
  //of the message, so it is decoded/re-emitted together with the BER length and body below.
138
138
  msgType: {
139
+ default: 0x6a,
139
140
  type: 'integer',
140
141
  label: 'Message Type',
141
142
  minimum: 0,
@@ -76,6 +76,7 @@ class L2TP extends BaseHeader_1.BaseHeader {
76
76
  offset: this.#flagBit('offset', 6, 'Offset Present'),
77
77
  priority: this.#flagBit('priority', 7, 'Priority'),
78
78
  version: {
79
+ default: 2,
79
80
  type: 'integer',
80
81
  label: 'Version',
81
82
  minimum: 0,
@@ -190,7 +190,7 @@ class LDAP extends BaseHeader_1.BaseHeader {
190
190
  //The protocolOp CHOICE tag: 0x60 bindRequest, 0x61 bindResponse, 0x63 searchRequest,
191
191
  //0x64 searchResultEntry, 0x65 searchResultDone, 0x66 modifyRequest, 0x67 modifyResponse,
192
192
  //0x68 addRequest, 0x69 addResponse, 0x4a delRequest, 0x42 unbindRequest, 0x73 extendedReq …
193
- protocolOpTag: { type: 'integer', label: 'Protocol Op Tag', minimum: 0, maximum: 255 },
193
+ protocolOpTag: { type: 'integer', label: 'Protocol Op Tag', minimum: 0, maximum: 255, default: 0x60 },
194
194
  //The protocolOp body (op length/content + any controls) kept verbatim as hex — see decode.
195
195
  protocolOpData: { type: 'string', label: 'Protocol Op Data', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
196
196
  }
@@ -50,6 +50,7 @@ class LLC extends BaseHeader_1.BaseHeader {
50
50
  //Supervisory). Stored as hex preserving its on-wire length so encode reproduces it exactly;
51
51
  //because the non-dry read grows headerLength, the child layer starts at the correct offset.
52
52
  control: {
53
+ default: '03',
53
54
  type: 'string',
54
55
  label: 'Control',
55
56
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
@@ -16,7 +16,7 @@ import { DemuxProducer } from '../types/DemuxProducer';
16
16
  * driven by ar$shtl and the per-entry CIE type/length octets) and carry cross-field context, so this
17
17
  * codec keeps them verbatim as `body` hex (byte-perfect) and does not sub-decode them. Packet Length is
18
18
  * auto-computed from the body on encode when not supplied, else honored verbatim (a crafted packet may
19
- * lie); the checksum is honored verbatim, never recomputed (encode is a faithful executor). The body is
19
+ * lie); likewise the checksum is computed over the whole NHRP packet when left out. The body is
20
20
  * bounded by both Packet Length and the enclosing IP payload, so trailing bytes are left to the codec's
21
21
  * recursion / RawData. A well-formed packet round-trips byte-for-byte.
22
22
  */
@@ -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
  * NHRP — NBMA Next Hop Resolution Protocol (RFC 2332), carried directly over IP as protocol 54. Every
@@ -22,7 +23,7 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
22
23
  * driven by ar$shtl and the per-entry CIE type/length octets) and carry cross-field context, so this
23
24
  * codec keeps them verbatim as `body` hex (byte-perfect) and does not sub-decode them. Packet Length is
24
25
  * auto-computed from the body on encode when not supplied, else honored verbatim (a crafted packet may
25
- * lie); the checksum is honored verbatim, never recomputed (encode is a faithful executor). The body is
26
+ * lie); likewise the checksum is computed over the whole NHRP packet when left out. The body is
26
27
  * bounded by both Packet Length and the enclosing IP payload, so trailing bytes are left to the codec's
27
28
  * recursion / RawData. A well-formed packet round-trips byte-for-byte.
28
29
  */
@@ -111,9 +112,33 @@ class NHRP extends BaseHeader_1.BaseHeader {
111
112
  this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
112
113
  }
113
114
  },
114
- //Honored verbatim: the NHRP checksum (ones-complement over the whole packet) is never
115
- //recomputed, so a captured packet round-trips byte-for-byte.
116
- checksum: this.fieldUInt('checksum', 12, 2, 'Checksum'),
115
+ //Internet checksum over the whole NHRP packet from the Fixed Header on (RFC 2332 §5.2.0.1),
116
+ //computed once every field is written when left out; a given value is written as given.
117
+ checksum: {
118
+ type: 'integer',
119
+ autoCalculated: true,
120
+ label: 'Checksum',
121
+ minimum: 0,
122
+ maximum: 65535,
123
+ decode: function () {
124
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2)));
125
+ },
126
+ encode: function () {
127
+ if (!this.isLeftOut(this.instance.checksum)) {
128
+ const checksum = this.instance.checksum.getValue(0);
129
+ this.instance.checksum.setValue(checksum);
130
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
131
+ return;
132
+ }
133
+ this.instance.checksum.setValue(0);
134
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
135
+ this.addPostSelfEncodeHandler(() => {
136
+ const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
137
+ this.instance.checksum.setValue(checksum);
138
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
139
+ });
140
+ }
141
+ },
117
142
  extensionOffset: this.fieldUInt('extensionOffset', 14, 2, 'Extension Offset'),
118
143
  opVersion: this.fieldUInt('opVersion', 16, 1, 'Version'),
119
144
  opType: this.fieldUInt('opType', 17, 1, 'Operation Type'),
@@ -6,9 +6,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
6
6
  * decodes the 24-octet common header: an 8-bit Version (`version`, offset 0), an 8-bit Message Type
7
7
  * (`type`, offset 1; 1 Hello, 2 Database Description, 3 LS Request, 4 LS Update, 5 LS Ack), a 16-bit
8
8
  * Packet Length (`packetLength`, offset 2), the Source Router ID (`routerId`, IPv4, offset 4), the Area
9
- * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12, honored verbatim and never
10
- * recomputed so a captured packet round-trips), a 16-bit Auth Type (`auType`, offset 14) and the 8-octet
11
- * Authentication field (`auth`, hex, offset 16).
9
+ * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12), a 16-bit Auth Type (`auType`,
10
+ * offset 14) and the 8-octet Authentication field (`auth`, hex, offset 16). Packet Length and Checksum
11
+ * are computed on encode when left out and written as given otherwise.
12
12
  *
13
13
  * For a Hello (type 1) the body is decoded field-by-field: Network Mask (offset 24), 16-bit Hello Interval
14
14
  * (offset 28), 8-bit Options (offset 30), 8-bit Router Priority (offset 31), 32-bit Router Dead Interval