@netkitty/codec 1.2.1 → 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 (205) hide show
  1. package/CHANGELOG.md +295 -0
  2. package/README.md +62 -10
  3. package/README.zh-CN.md +55 -9
  4. package/dist/Codec.d.ts +3 -2
  5. package/dist/Codec.js +109 -10
  6. package/dist/abstracts/BaseHeader.d.ts +54 -2
  7. package/dist/abstracts/BaseHeader.js +185 -43
  8. package/dist/errors/CodecSchemaValidateError.d.ts +34 -1
  9. package/dist/errors/CodecSchemaValidateError.js +26 -3
  10. package/dist/headers/ADSAMS.js +2 -0
  11. package/dist/headers/AH.js +4 -2
  12. package/dist/headers/AMQP.js +2 -1
  13. package/dist/headers/ARP.js +16 -10
  14. package/dist/headers/BACnetIP.js +1 -0
  15. package/dist/headers/BFD.d.ts +2 -1
  16. package/dist/headers/BFD.js +30 -4
  17. package/dist/headers/BGP.js +6 -1
  18. package/dist/headers/Babel.js +2 -1
  19. package/dist/headers/BitTorrent.d.ts +2 -0
  20. package/dist/headers/BitTorrent.js +4 -2
  21. package/dist/headers/C37118.d.ts +3 -2
  22. package/dist/headers/C37118.js +35 -15
  23. package/dist/headers/CAPWAP.js +1 -0
  24. package/dist/headers/CDP.d.ts +3 -3
  25. package/dist/headers/CDP.js +47 -11
  26. package/dist/headers/CMS.js +27 -12
  27. package/dist/headers/COTP.d.ts +2 -0
  28. package/dist/headers/COTP.js +8 -4
  29. package/dist/headers/CassandraCQL.js +1 -0
  30. package/dist/headers/CoAP.js +20 -2
  31. package/dist/headers/Codesys.js +1 -0
  32. package/dist/headers/Collectd.js +1 -1
  33. package/dist/headers/DCCP.d.ts +3 -2
  34. package/dist/headers/DCCP.js +61 -8
  35. package/dist/headers/DHCPFailover.js +1 -0
  36. package/dist/headers/DNP3.d.ts +5 -3
  37. package/dist/headers/DNP3.js +62 -7
  38. package/dist/headers/DNS.js +3 -3
  39. package/dist/headers/DTLS.js +1 -0
  40. package/dist/headers/Diameter.js +3 -2
  41. package/dist/headers/EAPOL.js +1 -1
  42. package/dist/headers/EIGRP.d.ts +3 -2
  43. package/dist/headers/EIGRP.js +32 -6
  44. package/dist/headers/ENIP.js +1 -0
  45. package/dist/headers/Elasticsearch.js +1 -0
  46. package/dist/headers/EtherCAT.js +1 -0
  47. package/dist/headers/EthernetII.js +7 -0
  48. package/dist/headers/FTP.js +6 -6
  49. package/dist/headers/Finger.js +3 -3
  50. package/dist/headers/FoundationFieldbusHSE.js +1 -0
  51. package/dist/headers/GELF.js +8 -1
  52. package/dist/headers/GENEVE.js +2 -1
  53. package/dist/headers/GLBP.js +1 -1
  54. package/dist/headers/GRE.js +1 -0
  55. package/dist/headers/GTPU.js +1 -0
  56. package/dist/headers/GTPv2C.js +2 -0
  57. package/dist/headers/GitProtocol.js +2 -2
  58. package/dist/headers/Goose.js +24 -12
  59. package/dist/headers/Gopher.js +6 -6
  60. package/dist/headers/HARTIP.js +1 -0
  61. package/dist/headers/HSR.js +3 -0
  62. package/dist/headers/HTTP.js +6 -6
  63. package/dist/headers/HTTP2.js +2 -0
  64. package/dist/headers/ICMP.js +3 -1
  65. package/dist/headers/ICMPv6.js +11 -16
  66. package/dist/headers/IEC104_I_Frame.js +215 -163
  67. package/dist/headers/IEC104_S_Frame.js +26 -3
  68. package/dist/headers/IEC104_U_Frame.js +33 -4
  69. package/dist/headers/IEC61850SampledValues.js +6 -5
  70. package/dist/headers/IGMP.d.ts +8 -7
  71. package/dist/headers/IGMP.js +58 -19
  72. package/dist/headers/IMAP.js +5 -5
  73. package/dist/headers/IPFIX.js +3 -2
  74. package/dist/headers/IPv4.js +41 -21
  75. package/dist/headers/IPv6.js +5 -1
  76. package/dist/headers/IPv6HopByHopOptions.js +15 -12
  77. package/dist/headers/IRC.js +4 -4
  78. package/dist/headers/ISAKMP.js +3 -1
  79. package/dist/headers/ISCSI.js +2 -0
  80. package/dist/headers/ISIS.js +2 -1
  81. package/dist/headers/ISOSession.d.ts +2 -0
  82. package/dist/headers/ISOSession.js +14 -10
  83. package/dist/headers/Ident.js +8 -8
  84. package/dist/headers/KNXnetIP.js +1 -0
  85. package/dist/headers/Kafka.js +1 -0
  86. package/dist/headers/Kerberos.js +2 -0
  87. package/dist/headers/L2TP.js +3 -2
  88. package/dist/headers/LACP.js +1 -1
  89. package/dist/headers/LDAP.js +3 -4
  90. package/dist/headers/LDP.js +1 -0
  91. package/dist/headers/LLC.d.ts +2 -0
  92. package/dist/headers/LLC.js +3 -0
  93. package/dist/headers/LPD.js +3 -3
  94. package/dist/headers/MGCP.js +7 -7
  95. package/dist/headers/MMS.d.ts +2 -0
  96. package/dist/headers/MMS.js +10 -6
  97. package/dist/headers/MQTT.js +1 -0
  98. package/dist/headers/MQTTSN.js +1 -0
  99. package/dist/headers/MarkerProtocol.js +1 -1
  100. package/dist/headers/Megaco.js +3 -3
  101. package/dist/headers/Memcached.js +3 -2
  102. package/dist/headers/ModbusTCP.js +1 -0
  103. package/dist/headers/ModbusUDP.js +1 -0
  104. package/dist/headers/MongoDB.js +1 -0
  105. package/dist/headers/MySQL.js +1 -0
  106. package/dist/headers/NATS.js +2 -2
  107. package/dist/headers/NBDS.js +1 -0
  108. package/dist/headers/NBSS.js +1 -0
  109. package/dist/headers/NFS.js +1 -0
  110. package/dist/headers/NHRP.d.ts +1 -1
  111. package/dist/headers/NHRP.js +30 -4
  112. package/dist/headers/NNTP.js +6 -6
  113. package/dist/headers/NetFlowV5.js +1 -0
  114. package/dist/headers/OLSR.js +2 -1
  115. package/dist/headers/OPCUA.js +1 -0
  116. package/dist/headers/OSPF.d.ts +3 -3
  117. package/dist/headers/OSPF.js +72 -7
  118. package/dist/headers/OpenFlow.js +1 -0
  119. package/dist/headers/OpenVPN.js +1 -0
  120. package/dist/headers/PCWorx.js +1 -0
  121. package/dist/headers/PIM.d.ts +3 -3
  122. package/dist/headers/PIM.js +55 -6
  123. package/dist/headers/POP3.js +5 -5
  124. package/dist/headers/PPPoEDiscovery.js +4 -1
  125. package/dist/headers/PPPoESession.js +1 -0
  126. package/dist/headers/PPTP.js +1 -0
  127. package/dist/headers/PTP.js +6 -1
  128. package/dist/headers/PostgreSQL.js +2 -0
  129. package/dist/headers/QUIC.js +2 -0
  130. package/dist/headers/RADIUS.js +1 -0
  131. package/dist/headers/RDP.d.ts +2 -0
  132. package/dist/headers/RDP.js +6 -1
  133. package/dist/headers/RFB.js +4 -4
  134. package/dist/headers/RGoose.js +8 -1
  135. package/dist/headers/RSVP.d.ts +3 -3
  136. package/dist/headers/RSVP.js +59 -44
  137. package/dist/headers/RTCP.js +2 -0
  138. package/dist/headers/RTP.js +2 -1
  139. package/dist/headers/RTPS.d.ts +2 -0
  140. package/dist/headers/RTPS.js +4 -1
  141. package/dist/headers/RTSP.js +7 -7
  142. package/dist/headers/RawData.js +1 -1
  143. package/dist/headers/Redis.js +4 -4
  144. package/dist/headers/Rlogin.js +5 -5
  145. package/dist/headers/Rsync.js +2 -2
  146. package/dist/headers/S7comm.d.ts +2 -0
  147. package/dist/headers/S7comm.js +5 -1
  148. package/dist/headers/SCTP.d.ts +2 -2
  149. package/dist/headers/SCTP.js +31 -7
  150. package/dist/headers/SFlow.js +2 -1
  151. package/dist/headers/SIP.js +6 -6
  152. package/dist/headers/SLMP.js +1 -0
  153. package/dist/headers/SLP.js +2 -0
  154. package/dist/headers/SLPv1.js +1 -0
  155. package/dist/headers/SMB1.js +1 -0
  156. package/dist/headers/SMB2.js +1 -0
  157. package/dist/headers/SMPP.js +1 -0
  158. package/dist/headers/SMTP.js +6 -6
  159. package/dist/headers/SNAP.js +18 -5
  160. package/dist/headers/SNMP.js +6 -6
  161. package/dist/headers/SOCKS4.js +11 -5
  162. package/dist/headers/SSDP.js +7 -7
  163. package/dist/headers/SSH.js +5 -4
  164. package/dist/headers/STOMP.js +5 -5
  165. package/dist/headers/STUN.js +3 -1
  166. package/dist/headers/SercosIII.d.ts +3 -2
  167. package/dist/headers/SercosIII.js +41 -5
  168. package/dist/headers/Skinny.js +1 -0
  169. package/dist/headers/StatsD.js +4 -4
  170. package/dist/headers/SunRPC.js +1 -0
  171. package/dist/headers/Syslog.js +2 -2
  172. package/dist/headers/TCP.js +31 -64
  173. package/dist/headers/TLS_Alert.js +42 -12
  174. package/dist/headers/TLS_ApplicationData.d.ts +3 -3
  175. package/dist/headers/TLS_ApplicationData.js +32 -8
  176. package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
  177. package/dist/headers/TLS_ChangeCipherSpec.js +33 -8
  178. package/dist/headers/TLS_Handshake.js +78 -30
  179. package/dist/headers/TLS_Heartbeat.d.ts +4 -3
  180. package/dist/headers/TLS_Heartbeat.js +50 -8
  181. package/dist/headers/TPKT.js +4 -2
  182. package/dist/headers/TacacsPlus.js +1 -0
  183. package/dist/headers/Telnet.js +2 -1
  184. package/dist/headers/Teredo.js +2 -2
  185. package/dist/headers/UDP.js +20 -56
  186. package/dist/headers/VLAN_802dot1Q.js +8 -0
  187. package/dist/headers/VRRP.d.ts +2 -1
  188. package/dist/headers/VRRP.js +45 -9
  189. package/dist/headers/WCCP.js +1 -0
  190. package/dist/headers/WHOIS.js +3 -3
  191. package/dist/headers/WSDiscovery.js +2 -2
  192. package/dist/headers/XMPP.js +3 -3
  193. package/dist/headers/Zabbix.js +1 -0
  194. package/dist/helper/Checksum.d.ts +15 -0
  195. package/dist/helper/Checksum.js +56 -0
  196. package/dist/helper/Crc.d.ts +18 -0
  197. package/dist/helper/Crc.js +58 -0
  198. package/dist/index.d.ts +3 -3
  199. package/dist/lib/SchemaValidator.d.ts +31 -0
  200. package/dist/lib/SchemaValidator.js +137 -0
  201. package/dist/schema/ProtocolFieldJSONSchema.d.ts +4 -1
  202. package/dist/schema/ProtocolJSONSchema.d.ts +1 -1
  203. package/dist/types/CodecErrorInfo.d.ts +11 -0
  204. package/dist/types/LayerGraph.d.ts +14 -1
  205. 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: {
@@ -66,6 +84,7 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
66
84
  //precisely to acknowledge this sequence number); controlField holds the authoritative bytes.
67
85
  rxSequence: {
68
86
  type: 'integer',
87
+ readOnly: true,
69
88
  label: 'Rx Sequence Number N(R)',
70
89
  decode: () => {
71
90
  this.instance.rxSequence.setValue(this.readBytes(4, 2).readUInt16LE() >> 1);
@@ -91,7 +110,7 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
91
110
  }
92
111
  },
93
112
  encode: () => {
94
- const controlType = this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
113
+ const controlType = this.instance.apciType.getValue('S-Format', (nodePath) => this.recordError(nodePath, 'Not Found'));
95
114
  switch (controlType) {
96
115
  case 'S-Format':
97
116
  {
@@ -99,7 +118,11 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
99
118
  }
100
119
  break;
101
120
  default: {
102
- this.writeBits(2, 1, 7, 1, this.instance.apciType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
121
+ //Not the label: the raw format bit decode stores for an illegal frame (0/1).
122
+ const formatBit = /^[01]$/.test(`${controlType}`) ? Number(controlType) : NaN;
123
+ if (formatBit !== 0 && formatBit !== 1)
124
+ this.recordError(this.instance.apciType.getPath(), `Unknown APCI type '${controlType}', encoded as S-Format`, 'error');
125
+ this.writeBits(2, 1, 7, 1, formatBit === 0 ? 0 : 1);
103
126
  }
104
127
  }
105
128
  }
@@ -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: {
@@ -59,7 +77,7 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
59
77
  this.instance.controlField.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4)));
60
78
  },
61
79
  encode: () => {
62
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
80
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('00000000', (nodePath) => this.recordError(nodePath, 'Not Found'))));
63
81
  }
64
82
  },
65
83
  apciType: {
@@ -107,7 +125,10 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
107
125
  }
108
126
  },
109
127
  encode: () => {
110
- const controlType = this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
128
+ //Left out: controlField (encoded before this) is the verbatim control word; keep it.
129
+ if (this.instance.apciType.isUndefined())
130
+ return;
131
+ const controlType = this.instance.apciType.getValue('00000000');
111
132
  switch (controlType) {
112
133
  case 'U-Format Type:Test Frame Activation':
113
134
  {
@@ -140,7 +161,15 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
140
161
  }
141
162
  break;
142
163
  default: {
143
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
164
+ //Not a known label: the verbatim 4-octet control word decode stores for an
165
+ //unrecognized U-frame. Anything else cannot be encoded; keep the 4-octet framing.
166
+ if (/^[0-9a-fA-F]{8}$/.test(`${controlType}`)) {
167
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(controlType));
168
+ }
169
+ else {
170
+ this.recordError(this.instance.apciType.getPath(), `Unknown APCI type '${controlType}', control field encoded as 00000000`, 'error');
171
+ this.writeBytes(2, Buffer.alloc(4, 0));
172
+ }
144
173
  }
145
174
  }
146
175
  }
@@ -36,13 +36,14 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
36
36
  },
37
37
  length: {
38
38
  type: 'integer',
39
+ autoCalculated: true,
39
40
  label: 'Length',
40
41
  decode: () => {
41
42
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
42
43
  },
43
44
  encode: () => {
44
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
45
- if (length > 0) {
45
+ if (!this.isLeftOut(this.instance.length)) {
46
+ const length = this.instance.length.getValue(0);
46
47
  this.instance.length.setValue(length);
47
48
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
48
49
  }
@@ -131,7 +132,7 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
131
132
  //the original bytes means keeping it).
132
133
  const rawBuffer = Buffer.from(this.instance.svPdu.raw.getValue().toString(), 'hex');
133
134
  this.writeBytes(8, rawBuffer);
134
- if (this.instance.length.getValue() > 0)
135
+ if (!this.isLeftOut(this.instance.length))
135
136
  return;
136
137
  this.instance.length.setValue(8 + rawBuffer.length);
137
138
  return;
@@ -145,14 +146,14 @@ class IEC61850SampledValues extends BaseHeader_1.BaseHeader {
145
146
  * Update the length only if it is not set
146
147
  * Update length(APPID's length + Length's length + Reserved1's length + Reserved2's length + svPdu's length)
147
148
  */
148
- if (this.instance.length.getValue() > 0)
149
+ if (!this.isLeftOut(this.instance.length))
149
150
  return;
150
151
  this.instance.length.setValue(2 + 2 + 2 + 2 + svPduBuffer.length);
151
152
  },
152
153
  properties: {
153
154
  noASDU: {
154
155
  type: 'integer',
155
- minimum: 1,
156
+ minimum: 0,
156
157
  maximum: 65535,
157
158
  label: 'Number of ASDU',
158
159
  decode: () => {
@@ -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,13 +169,14 @@ 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
  }
137
176
  },
138
177
  numSources: {
139
178
  type: 'integer',
179
+ autoCalculated: true,
140
180
  label: 'Number of Sources',
141
181
  minimum: 0,
142
182
  maximum: 65535,
@@ -146,14 +186,13 @@ class IGMP extends BaseHeader_1.BaseHeader {
146
186
  this.instance.numSources.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(10, 2)));
147
187
  },
148
188
  encode: function () {
149
- if (!this.#isV3Query())
189
+ if (!this.#encodesV3Query())
150
190
  return;
151
191
  //honor-else-derive: honored when supplied (a crafted message may lie); else the
152
192
  //number of 4-byte source addresses carried in the `sources` hex.
153
- const provided = this.instance.numSources.getValue();
154
- let value = (provided !== undefined && provided !== null)
155
- ? provided
156
- : 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);
157
196
  if (value > 65535) {
158
197
  this.recordError(this.instance.numSources.getPath(), 'Maximum value is 65535');
159
198
  value = 65535;
@@ -183,7 +222,7 @@ class IGMP extends BaseHeader_1.BaseHeader {
183
222
  this.instance.sources.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(12, byteLength)));
184
223
  },
185
224
  encode: function () {
186
- if (!this.#isV3Query())
225
+ if (!this.#encodesV3Query())
187
226
  return;
188
227
  const sources = this.instance.sources.getValue('');
189
228
  if (sources)
@@ -149,11 +149,11 @@ class IMAP extends BaseHeader_1.BaseHeader {
149
149
  //message field above, never read back). kind distinguishes the four line shapes; tag is the
150
150
  //command/response tag (empty for untagged/continuation); command/status/text describe the
151
151
  //recognized tokens.
152
- kind: { type: 'string', label: 'Kind' },
153
- tag: { type: 'string', label: 'Tag' },
154
- command: { type: 'string', label: 'Command' },
155
- status: { type: 'string', label: 'Status' },
156
- text: { type: 'string', label: 'Text' }
152
+ kind: { type: 'string', label: 'Kind', readOnly: true },
153
+ tag: { type: 'string', label: 'Tag', readOnly: true },
154
+ command: { type: 'string', label: 'Command', readOnly: true },
155
+ status: { type: 'string', label: 'Status', readOnly: true },
156
+ text: { type: 'string', label: 'Text', readOnly: true }
157
157
  }
158
158
  };
159
159
  }
@@ -47,6 +47,7 @@ class IPFIX extends BaseHeader_1.BaseHeader {
47
47
  version: this.fieldUInt('version', 0, 2, 'Version'),
48
48
  length: {
49
49
  type: 'integer',
50
+ autoCalculated: true,
50
51
  label: 'Length',
51
52
  minimum: 0,
52
53
  maximum: 65535,
@@ -58,7 +59,7 @@ class IPFIX extends BaseHeader_1.BaseHeader {
58
59
  //honored (a crafted message may lie); otherwise a placeholder is written now and
59
60
  //the real total is filled in after every Set has encoded (post-self-encode).
60
61
  const messageLength = this.instance.length.getValue(0);
61
- if (messageLength) {
62
+ if (!this.isLeftOut(this.instance.length)) {
62
63
  this.instance.length.setValue(messageLength);
63
64
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(messageLength));
64
65
  }
@@ -85,7 +86,7 @@ class IPFIX extends BaseHeader_1.BaseHeader {
85
86
  label: 'Set',
86
87
  properties: {
87
88
  setId: { type: 'integer', label: 'Set ID', minimum: 0, maximum: 65535 },
88
- setLength: { type: 'integer', label: 'Set Length', minimum: 0, maximum: 65535 },
89
+ setLength: { type: 'integer', label: 'Set Length', autoCalculated: true, minimum: 0, maximum: 65535 },
89
90
  body: { type: 'string', label: 'Body', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
90
91
  }
91
92
  },
@@ -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,
@@ -77,22 +78,28 @@ class IPv4 extends BaseHeader_1.BaseHeader {
77
78
  },
78
79
  hdrLen: {
79
80
  type: 'integer',
81
+ autoCalculated: true,
80
82
  label: 'Header Length',
81
- minimum: 20,
83
+ //The field floor (IHL 0), not the protocol minimum of 20: a decoded malformed header (IHL
84
+ //0–4) must re-encode. Left out means "compute"; a value below 20 is written as given, flagged.
85
+ minimum: 0,
82
86
  maximum: 60,
83
87
  decode: function () {
84
88
  this.instance.hdrLen.setValue(this.readBits(0, 1, 4, 4) * 4);
85
89
  },
86
90
  encode: function () {
87
- let headerLength = this.instance.hdrLen.getValue(0);
88
- if (headerLength)
89
- headerLength = headerLength ? Math.floor(headerLength / 4) : 0;
90
- this.writeBits(0, 1, 4, 4, headerLength);
91
- if (!headerLength)
92
- this.addPostSelfEncodeHandler(() => {
93
- this.instance.hdrLen.setValue(this.length);
94
- this.writeBits(0, 1, 4, 4, Math.floor(this.length / 4));
95
- }, 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);
96
103
  }
97
104
  },
98
105
  dsfield: {
@@ -129,6 +136,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
129
136
  },
130
137
  length: {
131
138
  type: 'integer',
139
+ autoCalculated: true,
132
140
  label: 'Total Length',
133
141
  minimum: 0,
134
142
  maximum: 65535,
@@ -136,13 +144,12 @@ class IPv4 extends BaseHeader_1.BaseHeader {
136
144
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
137
145
  },
138
146
  encode: function () {
139
- //This field's real value needs down stream codec invoke recode to fill
140
- const length = this.instance.length.getValue(0);
141
- if (length) {
142
- 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)));
143
150
  }
144
151
  else {
145
- this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
152
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
146
153
  this.addPostPacketEncodeHandler(() => {
147
154
  let startCount = false;
148
155
  let totalLength = 0;
@@ -190,6 +197,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
190
197
  }
191
198
  },
192
199
  df: {
200
+ default: 1,
193
201
  type: 'integer',
194
202
  enum: [0, 1],
195
203
  label: 'Don\'t fragment',
@@ -246,6 +254,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
246
254
  }
247
255
  },
248
256
  protocol: {
257
+ autoCalculated: true,
249
258
  type: 'integer',
250
259
  label: 'Protocol',
251
260
  minimum: 0,
@@ -261,6 +270,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
261
270
  },
262
271
  checksum: {
263
272
  type: 'integer',
273
+ autoCalculated: true,
264
274
  label: 'Header Checksum',
265
275
  minimum: 0,
266
276
  maximum: 65535,
@@ -268,16 +278,16 @@ class IPv4 extends BaseHeader_1.BaseHeader {
268
278
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(10, 2)));
269
279
  },
270
280
  encode: function () {
271
- let checksum = this.instance.checksum.getValue(0);
272
- checksum = checksum > 65535 ? 65535 : checksum;
273
- checksum = checksum < 0 ? 0 : checksum;
274
- 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;
275
285
  this.instance.checksum.setValue(checksum);
276
286
  this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
277
287
  }
278
288
  else {
279
- this.instance.checksum.setValue(checksum);
280
- this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
289
+ this.instance.checksum.setValue(0);
290
+ this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
281
291
  this.addPostPacketEncodeHandler(() => {
282
292
  const calcChecksum = this.calculateIPv4Checksum(this.packet.subarray(this.startPos, this.endPos));
283
293
  this.instance.checksum.setValue(calcChecksum);
@@ -320,6 +330,7 @@ class IPv4 extends BaseHeader_1.BaseHeader {
320
330
  },
321
331
  options: {
322
332
  type: 'string',
333
+ byteAlignment: 4,
323
334
  label: 'Options',
324
335
  minLength: 0,
325
336
  maxLength: 40 * 2,
@@ -352,6 +363,15 @@ class IPv4 extends BaseHeader_1.BaseHeader {
352
363
  match() {
353
364
  if (!this.prevCodecModule)
354
365
  return false;
366
+ //A header these fields cannot reproduce is left to raw (byte-exact) instead: IHL 0 — the encode
367
+ //"compute" sentinel, so it would come back as 5 — or fewer bytes than the header (20, or IHL×4),
368
+ //since encode always writes a whole header. IHL 1–4 is kept (it re-encodes as given).
369
+ const available = this.packet.length - this.startPos;
370
+ if (available < 20)
371
+ return false;
372
+ const ihl = this.readBytes(0, 1, true)[0] & 0x0f;
373
+ if (ihl === 0 || available < ihl * 4)
374
+ return false;
355
375
  //Normal path (unchanged): a parent that demuxed to IPv4 via its etherType field.
356
376
  if (this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x0800))
357
377
  return true;
@@ -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,
@@ -94,6 +95,7 @@ class IPv6 extends BaseHeader_1.BaseHeader {
94
95
  },
95
96
  plen: {
96
97
  type: 'integer',
98
+ autoCalculated: true,
97
99
  label: 'Payload Length',
98
100
  minimum: 0,
99
101
  maximum: 65535,
@@ -101,10 +103,11 @@ class IPv6 extends BaseHeader_1.BaseHeader {
101
103
  this.instance.plen.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(4, 2)));
102
104
  },
103
105
  encode: () => {
106
+ const leftOut = this.isLeftOut(this.instance.plen);
104
107
  const plen = this.instance.plen.getValue(0);
105
108
  this.instance.plen.setValue(plen);
106
109
  this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(plen));
107
- if (!plen) {
110
+ if (leftOut) {
108
111
  this.addPostPacketEncodeHandler(() => {
109
112
  let startCount = false;
110
113
  let totalLength = 0;
@@ -125,6 +128,7 @@ class IPv6 extends BaseHeader_1.BaseHeader {
125
128
  }
126
129
  },
127
130
  nxt: {
131
+ autoCalculated: true,
128
132
  type: 'integer',
129
133
  label: 'Next Header',
130
134
  minimum: 0,