@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
@@ -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,17 +53,32 @@ 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
  },
72
+ //Default 01000000: the S-format bits match() requires, acknowledging N(R) 0.
54
73
  controlField: {
55
74
  type: 'string',
56
75
  label: 'Control Field',
76
+ default: '01000000',
57
77
  decode: () => {
58
78
  this.instance.controlField.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4)));
59
79
  },
60
80
  encode: () => {
61
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
81
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('01000000', (nodePath) => this.recordError(nodePath, 'Not Found'))));
62
82
  }
63
83
  },
64
84
  //Receive sequence number N(R): a 15-bit value in control octets 3-4, stored
@@ -75,8 +95,6 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
75
95
  apciType: {
76
96
  type: 'string',
77
97
  label: 'APCI Type',
78
- minimum: 0,
79
- maximum: 3,
80
98
  decode: () => {
81
99
  const controlType = this.readBits(2, 1, 7, 1);
82
100
  switch (controlType) {
@@ -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,24 +54,39 @@ 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
  },
73
+ //Default 07000000 (STARTDT act): a U-frame needs both low bits of the first control octet set,
74
+ //which match() requires. The default sits here, not on apciType, which overrides this field
75
+ //when given and leaves it alone when left out.
55
76
  controlField: {
56
77
  type: 'string',
57
78
  label: 'Control Field',
79
+ default: '07000000',
58
80
  decode: () => {
59
81
  this.instance.controlField.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4)));
60
82
  },
61
83
  encode: () => {
62
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('00000000', (nodePath) => this.recordError(nodePath, 'Not Found'))));
84
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('07000000', (nodePath) => this.recordError(nodePath, 'Not Found'))));
63
85
  }
64
86
  },
65
87
  apciType: {
66
88
  type: 'string',
67
89
  label: 'APCI Type',
68
- minimum: 0,
69
- maximum: 3,
70
90
  decode: () => {
71
91
  const uframeType = (0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4));
72
92
  switch (uframeType) {
@@ -17,4 +17,5 @@ export declare class IEC61850SampledValues extends BaseHeader {
17
17
  readonly name: string;
18
18
  readonly nickname: string;
19
19
  match(): boolean;
20
+ payloadEnd(): number | undefined;
20
21
  }
@@ -13,6 +13,7 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
13
13
  const BufferToNumber_1 = require("../helper/BufferToNumber");
14
14
  const BufferToHex_1 = require("../helper/BufferToHex");
15
15
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
16
+ const BERElement_1 = require("../lib/BERElement");
16
17
  class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
17
18
  constructor() {
18
19
  super(...arguments);
@@ -42,8 +43,8 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
42
43
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
43
44
  },
44
45
  encode: () => {
45
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
46
- if (length > 0) {
46
+ if (!this.isLeftOut(this.instance.length)) {
47
+ const length = this.instance.length.getValue(0);
47
48
  this.instance.length.setValue(length);
48
49
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
49
50
  }
@@ -113,7 +114,14 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
113
114
  //non-standard SV PDU whose BER cannot be parsed is recorded as an error.
114
115
  const buffer = this.readBytes(8, (this.instance.length.getValue()) - 8);
115
116
  try {
116
- this.TLVInstance = node_tlv_1.default.parse(buffer);
117
+ const svPduTLV = node_tlv_1.default.parse(buffer);
118
+ //node-tlv clips an element that runs past the octets available instead of failing,
119
+ //so a savPdu truncated by Length decoded with a shortened last field and lost
120
+ //octets on re-encode. Only BER that fills Length exactly is parsed into fields.
121
+ const malformed = (0, BERElement_1.MalformedBER)(buffer);
122
+ if (malformed)
123
+ throw new Error(malformed);
124
+ this.TLVInstance = svPduTLV;
117
125
  this.TLVChild = this.TLVInstance.getChild();
118
126
  }
119
127
  catch (e) {
@@ -132,7 +140,7 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
132
140
  //the original bytes means keeping it).
133
141
  const rawBuffer = Buffer.from(this.instance.svPdu.raw.getValue().toString(), 'hex');
134
142
  this.writeBytes(8, rawBuffer);
135
- if (this.instance.length.getValue() > 0)
143
+ if (!this.isLeftOut(this.instance.length))
136
144
  return;
137
145
  this.instance.length.setValue(8 + rawBuffer.length);
138
146
  return;
@@ -146,7 +154,7 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
146
154
  * Update the length only if it is not set
147
155
  * Update length(APPID's length + Length's length + Reserved1's length + Reserved2's length + svPdu's length)
148
156
  */
149
- if (this.instance.length.getValue() > 0)
157
+ if (!this.isLeftOut(this.instance.length))
150
158
  return;
151
159
  this.instance.length.setValue(2 + 2 + 2 + 2 + svPduBuffer.length);
152
160
  },
@@ -417,5 +425,12 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
417
425
  return false;
418
426
  return this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x88ba);
419
427
  }
428
+ payloadEnd() {
429
+ //Length counts the octets from APPID on: the 8-octet header plus the savPdu (IEC 61850-9-2).
430
+ //Bytes past it (Ethernet padding, a trailer) are not SV. A Length shorter than the header sets
431
+ //no bound.
432
+ const length = this.instance.length.getValue(0);
433
+ return length >= 8 ? this.startPos + length : undefined;
434
+ }
420
435
  }
421
436
  exports.IEC61850SampledValues = IEC61850SampledValues;
@@ -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)
@@ -44,7 +44,8 @@ class IPFIX extends BaseHeader_1.BaseHeader {
44
44
  properties: {
45
45
  //Version Number (RFC 7011 §3.1): always 10 for IPFIX. Kept as a plain uint16 (clamped in
46
46
  //the closure, no hard enum) so a crafted non-10 value still round-trips byte-for-byte.
47
- version: this.fieldUInt('version', 0, 2, 'Version'),
47
+ //Default 10: what match() requires.
48
+ version: { ...this.fieldUInt('version', 0, 2, 'Version'), default: 10 },
48
49
  length: {
49
50
  type: 'integer',
50
51
  autoCalculated: true,
@@ -59,7 +60,7 @@ class IPFIX extends BaseHeader_1.BaseHeader {
59
60
  //honored (a crafted message may lie); otherwise a placeholder is written now and
60
61
  //the real total is filled in after every Set has encoded (post-self-encode).
61
62
  const messageLength = this.instance.length.getValue(0);
62
- if (messageLength) {
63
+ if (!this.isLeftOut(this.instance.length)) {
63
64
  this.instance.length.setValue(messageLength);
64
65
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(messageLength));
65
66
  }
@@ -16,5 +16,6 @@ export declare class IPv4 extends BaseHeader {
16
16
  readonly demuxProducers: DemuxProducer[];
17
17
  readonly name: string;
18
18
  readonly nickname: string;
19
+ payloadEnd(): number | undefined;
19
20
  match(): boolean;
20
21
  }
@@ -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);
@@ -357,6 +360,12 @@ class IPv4 extends BaseHeader_1.BaseHeader {
357
360
  }
358
361
  };
359
362
  }
363
+ payloadEnd() {
364
+ //Total Length counts this header and its payload. 0 (a segmentation-offload capture) or a value
365
+ //shorter than the header sets no bound.
366
+ const totalLength = this.instance.length.getValue(0);
367
+ return totalLength >= this.length ? this.startPos + totalLength : undefined;
368
+ }
360
369
  match() {
361
370
  if (!this.prevCodecModule)
362
371
  return false;
@@ -25,5 +25,6 @@ export declare class IPv6 extends BaseHeader {
25
25
  readonly demuxProducers: DemuxProducer[];
26
26
  name: string;
27
27
  nickname: string;
28
+ payloadEnd(): number | undefined;
28
29
  match(): boolean;
29
30
  }
@@ -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,
@@ -190,6 +193,12 @@ class IPv6 extends BaseHeader_1.BaseHeader {
190
193
  this.name = 'Internet Protocol Version 6';
191
194
  this.nickname = 'IPv6';
192
195
  }
196
+ payloadEnd() {
197
+ //Payload Length counts everything after this 40-octet header, extension headers included. 0 (a
198
+ //Jumbo Payload, or a segmentation-offload capture) sets no bound.
199
+ const payloadLength = this.instance.plen.getValue(0);
200
+ return payloadLength > 0 ? this.endPos + payloadLength : undefined;
201
+ }
193
202
  match() {
194
203
  if (!this.prevCodecModule)
195
204
  return false;
@@ -34,6 +34,8 @@ var Type;
34
34
  Type["IPv6_DFF_Header"] = "IPv6-DFF-Header";
35
35
  Type["Endpoint_Identification"] = "Endpoint-Identification";
36
36
  })(Type || (Type = {}));
37
+ //Value octets of the options whose item has a fixed-size value, by option type
38
+ const FIXED_OPTION_LENGTHS = { 0x04: 1, 0x05: 2, 0x07: 8, 0x0C: 6, 0x0D: 4, 0x8A: 16 };
37
39
  class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
38
40
  constructor() {
39
41
  super(...arguments);
@@ -45,6 +47,7 @@ class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
45
47
  type: 'object',
46
48
  properties: {
47
49
  nxt: {
50
+ autoCalculated: true,
48
51
  type: 'integer',
49
52
  label: 'Next Header',
50
53
  minimum: 0,
@@ -68,10 +71,11 @@ class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
68
71
  this.instance.len.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
69
72
  },
70
73
  encode: () => {
74
+ const leftOut = this.isLeftOut(this.instance.len);
71
75
  const len = this.instance.len.getValue(0);
72
76
  this.instance.len.setValue(len);
73
77
  this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(len));
74
- if (!len) {
78
+ if (leftOut) {
75
79
  this.addPostSelfEncodeHandler(() => {
76
80
  //Total header = 2 (nxt+len) + options area, and must be an integral
77
81
  //multiple of 8 octets (RFC 8200 §4.3). items encode already padded the
@@ -473,6 +477,12 @@ class IPv6HopByHopOptions extends BaseHeader_1.BaseHeader {
473
477
  * Unknown types become a Custom item so they still round-trip.
474
478
  */
475
479
  decodeHbhOption(type, length, valueHex) {
480
+ //An option whose item has a fixed size (Tunnel Encapsulation Limit 1 octet, Router Alert 2,
481
+ //CALIPSO tag 8, ILNP Nonce 6, Line-Identification 4, Endpoint Identification 16) but arrived
482
+ //with another size is kept as a Custom item: the typed item's schema would reject its value.
483
+ const fixedLength = FIXED_OPTION_LENGTHS[type];
484
+ if (fixedLength !== undefined && valueHex.length !== fixedLength * 2)
485
+ return { type: type, data: valueHex };
476
486
  switch (type) {
477
487
  case 0x01: return { type: Type.PadN, n: length };
478
488
  case 0x04: return { type: Type.Tunnel_Encapsulation_Limit, limit: (0, HexToNumber_1.HexToUInt8)(valueHex) };
@@ -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)); }