@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
@@ -26,9 +26,10 @@ var TLSver;
26
26
  *
27
27
  * Per RFC 6520 §4 the padding is at least 16 octets and fills the record after the type and payload, so its
28
28
  * length is derived as recordLength - 3 - payloadLength and preserved for a byte-exact re-encode. The
29
- * payload and padding are kept verbatim and lengths are honored as given, so a well-formed record
30
- * round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a Content Type
31
- * of 0x18 and a recognized legacy Version (0x0300-0x0304).
29
+ * payload and padding are kept verbatim and lengths are written as given (left out, the record Length is
30
+ * the octets after the record header and the Payload Length the payload's octet count), so a well-formed
31
+ * record round-trips byte-for-byte. In the heuristic chain (`heuristicFallback`), match() requires a
32
+ * Content Type of 0x18 and a recognized legacy Version (0x0300-0x0304).
32
33
  */
33
34
  class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
34
35
  constructor() {
@@ -36,11 +37,13 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
36
37
  this.SCHEMA = {
37
38
  type: 'object',
38
39
  properties: {
40
+ //Default 24 (heartbeat): the content type match() requires.
39
41
  contentType: {
40
42
  type: 'integer',
41
43
  label: 'Content Type',
42
44
  minimum: 0,
43
45
  maximum: 255,
46
+ default: 24,
44
47
  decode: () => {
45
48
  this.instance.contentType.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
46
49
  },
@@ -52,6 +55,10 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
52
55
  version: {
53
56
  type: 'string',
54
57
  label: 'Version',
58
+ //Closed set: decode stores one of these names, or '0' for an unrecognized version.
59
+ enum: [...Object.values(TLSver), '0'],
60
+ //Default TLS 1.2 (0x0303, also the record version of TLS 1.3): match() requires a known version.
61
+ default: TLSver.TLS_1_2,
55
62
  minimum: 0,
56
63
  maximum: 65535,
57
64
  decode: () => {
@@ -116,6 +123,9 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
116
123
  }
117
124
  break;
118
125
  default: {
126
+ //'0' is what decode stores for an unrecognized version; anything else is not a version.
127
+ if (`${version}` !== '0')
128
+ this.recordError(this.instance.version.getPath(), `Unknown TLS version '${version}', encoded as 0`, 'error');
119
129
  this.writeBytes(1, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
120
130
  }
121
131
  }
@@ -123,6 +133,7 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
123
133
  },
124
134
  length: {
125
135
  type: 'integer',
136
+ autoCalculated: true,
126
137
  label: 'Length',
127
138
  minimum: 0,
128
139
  maximum: 65535,
@@ -130,8 +141,21 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
130
141
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(3, 2)));
131
142
  },
132
143
  encode: () => {
133
- const length = this.instance.length.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
134
- this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
144
+ if (!this.isLeftOut(this.instance.length)) {
145
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.length.getValue(0)));
146
+ return;
147
+ }
148
+ //The record payload: every octet this layer writes after the 5-octet record header.
149
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
150
+ this.addPostSelfEncodeHandler(() => {
151
+ let length = this.length - 5;
152
+ if (length > 65535) {
153
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
154
+ length = 65535;
155
+ }
156
+ this.instance.length.setValue(length);
157
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
158
+ });
135
159
  }
136
160
  },
137
161
  heartbeatType: {
@@ -171,14 +195,19 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
171
195
  }
172
196
  break;
173
197
  default: {
198
+ //Not a known name: a numeric type (what decode stores for an unnamed one).
174
199
  const heartbeatType1 = parseInt(heartbeatType);
175
- this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(heartbeatType1));
200
+ const known = /^\d+$/.test(`${heartbeatType}`) && heartbeatType1 <= 255;
201
+ if (!known)
202
+ this.recordError(this.instance.heartbeatType.getPath(), `Unknown heartbeat type '${heartbeatType}', encoded as 0`, 'error');
203
+ this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(known ? heartbeatType1 : 0));
176
204
  }
177
205
  }
178
206
  }
179
207
  },
180
208
  payloadLength: {
181
209
  type: 'integer',
210
+ autoCalculated: true,
182
211
  label: 'Payload Length',
183
212
  minimum: 0,
184
213
  maximum: 65535,
@@ -186,7 +215,19 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
186
215
  this.instance.payloadLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
187
216
  },
188
217
  encode: () => {
189
- const payloadLength = this.instance.payloadLength.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
218
+ //Computed right here, not after the layer: padding is placed after payloadLength octets.
219
+ let payloadLength;
220
+ if (!this.isLeftOut(this.instance.payloadLength)) {
221
+ payloadLength = this.instance.payloadLength.getValue(0);
222
+ }
223
+ else {
224
+ payloadLength = (0, HexToBuffer_1.HexToBuffer)(this.instance.payloadMessage.getValue('')).length;
225
+ if (payloadLength > 65535) {
226
+ this.recordError(this.instance.payloadLength.getPath(), 'Maximum value is 65535');
227
+ payloadLength = 65535;
228
+ }
229
+ this.instance.payloadLength.setValue(payloadLength);
230
+ }
190
231
  this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(payloadLength));
191
232
  }
192
233
  },
@@ -199,7 +240,8 @@ class TLS_Heartbeat extends BaseHeader_1.BaseHeader {
199
240
  this.instance.payloadMessage.setValue(payloadMessage);
200
241
  },
201
242
  encode: () => {
202
- const payloadMessage = this.instance.payloadMessage.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
243
+ //Left out, the message carries no payload (Payload Length 0 when computed).
244
+ const payloadMessage = this.instance.payloadMessage.getValue('', (nodePath) => this.recordError(nodePath, 'Not Found'));
203
245
  this.writeBytes(8, (0, HexToBuffer_1.HexToBuffer)(payloadMessage));
204
246
  }
205
247
  },
@@ -38,10 +38,12 @@ class TPKT extends BaseHeader_1.BaseHeader {
38
38
  type: 'object',
39
39
  summary: 'TPKT v${version} len=${length}',
40
40
  properties: {
41
- version: this.fieldUInt('version', 0, 1, 'Version'),
41
+ //Default 3: the only TPKT version (RFC 1006), and what match() requires.
42
+ version: { ...this.fieldUInt('version', 0, 1, 'Version'), default: 3 },
42
43
  reserved: this.fieldUInt('reserved', 1, 1, 'Reserved'),
43
44
  length: {
44
45
  type: 'integer',
46
+ autoCalculated: true,
45
47
  label: 'Length',
46
48
  minimum: 0,
47
49
  maximum: 65535,
@@ -54,7 +56,7 @@ class TPKT extends BaseHeader_1.BaseHeader {
54
56
  //derive it after every layer has encoded, summing this header plus all layers
55
57
  //stacked on top of it — the same approach UDP uses for its length.
56
58
  const length = this.instance.length.getValue(0);
57
- if (length) {
59
+ if (!this.isLeftOut(this.instance.length)) {
58
60
  this.instance.length.setValue(length);
59
61
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
60
62
  }
@@ -69,6 +69,7 @@ class TacacsPlus extends BaseHeader_1.BaseHeader {
69
69
  sessionId: this.fieldUInt('sessionId', 4, 4, 'Session Id'),
70
70
  length: {
71
71
  type: 'integer',
72
+ autoCalculated: true,
72
73
  label: 'Length',
73
74
  minimum: 0,
74
75
  maximum: 4294967295,
@@ -137,9 +137,10 @@ class Telnet extends BaseHeader_1.BaseHeader {
137
137
  //Display-only metadata parsed from the leading IAC command run on decode (no encode —
138
138
  //populated by the `message` field below, never read back). isNegotiation flags a payload
139
139
  //that opens with an IAC byte; commands lists the leading command sequences for the UI.
140
- isNegotiation: { type: 'boolean', label: 'Is Negotiation', default: false },
140
+ isNegotiation: { type: 'boolean', label: 'Is Negotiation', readOnly: true, default: false },
141
141
  commands: {
142
142
  type: 'array',
143
+ readOnly: true,
143
144
  label: 'IAC Commands',
144
145
  items: {
145
146
  type: 'object',
@@ -106,8 +106,8 @@ class Teredo extends BaseHeader_1.BaseHeader {
106
106
  type: 'object',
107
107
  label: 'Authentication Indication',
108
108
  properties: {
109
- clientIdLength: { type: 'integer', label: 'Client ID Length', minimum: 0, maximum: 255 },
110
- authDataLength: { type: 'integer', label: 'Authentication Value Length', minimum: 0, maximum: 255 },
109
+ clientIdLength: { type: 'integer', label: 'Client ID Length', readOnly: true, minimum: 0, maximum: 255 },
110
+ authDataLength: { type: 'integer', label: 'Authentication Value Length', readOnly: true, minimum: 0, maximum: 255 },
111
111
  clientId: { type: 'string', label: 'Client Identifier', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
112
112
  authData: { type: 'string', label: 'Authentication Value', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
113
113
  nonce: { type: 'string', label: 'Nonce', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX },
@@ -4,7 +4,7 @@ exports.UDP = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToNumber_1 = require("../helper/BufferToNumber");
6
6
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
7
- const IPToBuffer_1 = require("../helper/IPToBuffer");
7
+ const Checksum_1 = require("../helper/Checksum");
8
8
  class UDP extends BaseHeader_1.BaseHeader {
9
9
  constructor() {
10
10
  super(...arguments);
@@ -24,55 +24,17 @@ class UDP extends BaseHeader_1.BaseHeader {
24
24
  * @param udpHeaderBuffer
25
25
  */
26
26
  calculateUDPChecksum(udpHeaderBuffer) {
27
- const udpHeaderLength = udpHeaderBuffer.length;
28
- const ipVersion = this.prevCodecModule.instance.version.getValue();
29
- let pseudoHeaderBuffer = Buffer.from([]);
30
- const sourceIp = this.prevCodecModule.instance.sip.getValue();
31
- const destinationIp = this.prevCodecModule.instance.dip.getValue();
32
- if (ipVersion === 4) {
33
- //4 Bytes
34
- const sourceIPv4Buffer = Buffer.from(sourceIp.split('.').map((value) => parseInt(value)));
35
- const destinationIPv4Buffer = Buffer.from(destinationIp.split('.').map((value) => parseInt(value)));
36
- //IPv4 Pseudo header
37
- pseudoHeaderBuffer = Buffer.concat([
38
- sourceIPv4Buffer,
39
- destinationIPv4Buffer,
40
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
41
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
42
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
43
- Buffer.from([udpHeaderLength & 0xFF])
44
- ]);
45
- }
46
- else if (ipVersion === 6) {
47
- //16 Bytes
48
- const sourceIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(sourceIp);
49
- const destinationIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(destinationIp);
50
- //IPv6 Pseudo header
51
- pseudoHeaderBuffer = Buffer.concat([
52
- sourceIPv6Buffer,
53
- destinationIPv6Buffer,
54
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
55
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
56
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
57
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
58
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
59
- Buffer.from([udpHeaderLength & 0xFF])
60
- ]);
61
- }
62
- else {
27
+ //The pseudo-header comes from the nearest IPv4/IPv6 layer beneath, past AH or IPv6 extension
28
+ //headers. Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack,
29
+ //so record it and leave the checksum 0 instead of dereferencing a missing layer.
30
+ const pseudoHeader = this.ipPseudoHeader(17, udpHeaderBuffer.length);
31
+ if (!pseudoHeader) {
32
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute UDP checksum: no IP layer beneath UDP');
63
33
  return 0;
64
34
  }
65
- let dataBuffer = Buffer.concat([pseudoHeaderBuffer, udpHeaderBuffer]);
66
- if (dataBuffer.length % 2)
67
- dataBuffer = Buffer.concat([dataBuffer, Buffer.from('00', 'hex')]);
68
- const data = Uint8Array.from(dataBuffer);
69
- let sum = 0;
70
- for (let i = 0; i < data.length; i += 2)
71
- sum += (data[i] << 8) + (data[i + 1] || 0);
72
- while (sum > 0xFFFF)
73
- sum = (sum & 0xFFFF) + (sum >>> 16);
74
- const _cs = (~sum) & 0xFFFF;
75
- return _cs === 0 ? 0xFFFF : _cs;
35
+ //A computed 0x0000 is sent as 0xFFFF: 0 means "no checksum" (RFC 768).
36
+ const checksum = (0, Checksum_1.InternetChecksum)(pseudoHeader, udpHeaderBuffer);
37
+ return checksum === 0 ? 0xFFFF : checksum;
76
38
  }
77
39
  static #schemaCache;
78
40
  //Class-cached SCHEMA (④): field closures are functions taking dynamic `this` via .call(this).
@@ -90,6 +52,7 @@ class UDP extends BaseHeader_1.BaseHeader {
90
52
  dstport: this.fieldUInt('dstport', 2, 2, 'Destination Port'),
91
53
  length: {
92
54
  type: 'integer',
55
+ autoCalculated: true,
93
56
  label: 'Length',
94
57
  minimum: 0,
95
58
  maximum: 65535,
@@ -97,14 +60,14 @@ class UDP extends BaseHeader_1.BaseHeader {
97
60
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(4, 2)));
98
61
  },
99
62
  encode: function () {
100
- const length = this.instance.length.getValue(0);
101
- if (length) {
63
+ if (!this.isLeftOut(this.instance.length)) {
64
+ const length = this.instance.length.getValue(0);
102
65
  this.instance.length.setValue(length);
103
66
  this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
104
67
  }
105
68
  else {
106
- this.instance.length.setValue(length);
107
- this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
69
+ this.instance.length.setValue(0);
70
+ this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
108
71
  this.addPostPacketEncodeHandler(() => {
109
72
  let startCount = false;
110
73
  let udpLength = 0;
@@ -122,6 +85,7 @@ class UDP extends BaseHeader_1.BaseHeader {
122
85
  },
123
86
  checksum: {
124
87
  type: 'integer',
88
+ autoCalculated: true,
125
89
  label: 'Checksum',
126
90
  minimum: 0,
127
91
  maximum: 65535,
@@ -129,14 +93,14 @@ class UDP extends BaseHeader_1.BaseHeader {
129
93
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
130
94
  },
131
95
  encode: function () {
132
- const checksum = this.instance.checksum.getValue(0);
133
- if (checksum) {
96
+ if (!this.isLeftOut(this.instance.checksum)) {
97
+ const checksum = this.instance.checksum.getValue(0);
134
98
  this.instance.checksum.setValue(checksum);
135
99
  this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
136
100
  }
137
101
  else {
138
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
139
- this.instance.checksum.setValue(checksum);
102
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
103
+ this.instance.checksum.setValue(0);
140
104
  this.addPostPacketEncodeHandler(() => {
141
105
  let startCount = false;
142
106
  let udpHeaderWithDataLength = 0;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VLAN_802dot1Q = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
+ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
5
6
  const NumberToHex_1 = require("../helper/NumberToHex");
6
7
  const BufferToHex_1 = require("../helper/BufferToHex");
7
8
  /**
@@ -64,14 +65,18 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
64
65
  }
65
66
  },
66
67
  etherType: {
68
+ autoCalculated: true,
67
69
  type: 'string',
68
70
  minLength: 4,
69
71
  maxLength: 4,
70
72
  label: 'EtherType',
73
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
71
74
  decode: () => {
72
75
  this.instance.etherType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2)));
73
76
  },
74
77
  encode: () => {
78
+ if (this.instance.etherType.isUndefined())
79
+ return this.encodeOmittedEtherType(this.instance.etherType, 2);
75
80
  const etherType = this.instance.etherType.getValue((0, NumberToHex_1.UInt16ToHex)(0x0000), (nodePath) => this.recordError(nodePath, 'Not Found'));
76
81
  const typeBuffer = Buffer.from(etherType, 'hex');
77
82
  if (typeBuffer.length < 2)
@@ -90,6 +95,9 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
90
95
  match() {
91
96
  if (!this.prevCodecModule)
92
97
  return false;
98
+ //Fewer bytes than the 4-octet tag cannot be reproduced by these fields: leave them to raw.
99
+ if (this.packet.length - this.startPos < 4)
100
+ return false;
93
101
  return this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x8100);
94
102
  }
95
103
  }
@@ -12,7 +12,8 @@ import { DemuxProducer } from '../types/DemuxProducer';
12
12
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
13
13
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
14
14
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
15
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
15
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
16
+ * written as given otherwise.
16
17
  */
17
18
  export declare class VRRP extends BaseHeader {
18
19
  #private;
@@ -9,6 +9,7 @@ const BufferToIP_1 = require("../helper/BufferToIP");
9
9
  const IPToBuffer_1 = require("../helper/IPToBuffer");
10
10
  const BufferToHex_1 = require("../helper/BufferToHex");
11
11
  const HexToBuffer_1 = require("../helper/HexToBuffer");
12
+ const Checksum_1 = require("../helper/Checksum");
12
13
  /**
13
14
  * VRRP — Virtual Router Redundancy Protocol (v2 RFC 3768, v3 RFC 5798), carried directly over IP as
14
15
  * protocol 112. A fixed 8-byte header — Version (high nibble of byte 0) + Type (low nibble), Virtual
@@ -20,7 +21,8 @@ const HexToBuffer_1 = require("../helper/HexToBuffer");
20
21
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
21
22
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
22
23
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
23
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
24
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
25
+ * written as given otherwise.
24
26
  */
25
27
  class VRRP extends BaseHeader_1.BaseHeader {
26
28
  constructor() {
@@ -54,6 +56,27 @@ class VRRP extends BaseHeader_1.BaseHeader {
54
56
  }
55
57
  return this.packet.length - this.startPos;
56
58
  }
59
+ /**
60
+ * The VRRP checksum: the Internet checksum of the whole VRRP message (v2, RFC 3768 §5.3.8, and v3
61
+ * over IPv4); v3 over IPv6 prepends the IPv6 pseudo-header (protocol 112, upper-layer length = the
62
+ * message length). RFC 5798 was ambiguous for v3 over IPv4 and some implementations added an IPv4
63
+ * pseudo-header; RFC 9568, which obsoletes it, says there is none, as Wireshark checks by default.
64
+ * Called once every field is written, with the checksum still zero.
65
+ * @private
66
+ */
67
+ #calculateChecksum() {
68
+ const message = this.packet.subarray(this.startPos, this.endPos);
69
+ const version = this.instance.version.getValue(2);
70
+ const ip = this.nearestIPModule();
71
+ if (version !== 3 || (ip && ip.id === 'ipv4'))
72
+ return (0, Checksum_1.InternetChecksum)(message);
73
+ const pseudoHeader = this.ipPseudoHeader(112, message.length);
74
+ if (!pseudoHeader) {
75
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute VRRP v3 checksum: no IP layer beneath VRRP');
76
+ return 0;
77
+ }
78
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, message);
79
+ }
57
80
  static #schemaCache;
58
81
  //Class-cached SCHEMA: field closures are plain functions taking dynamic `this` via .call(this), so
59
82
  //the structure is shareable and never rebuilt per packet. VRRP carries both v2 (RFC 3768) and v3
@@ -73,6 +96,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
73
96
  label: 'Version',
74
97
  minimum: 0,
75
98
  maximum: 15,
99
+ default: 2,
76
100
  decode: function () {
77
101
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
78
102
  const version = this.instance.version.getValue(0);
@@ -92,6 +116,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
92
116
  label: 'Type',
93
117
  minimum: 0,
94
118
  maximum: 15,
119
+ default: 1,
95
120
  decode: function () {
96
121
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
97
122
  },
@@ -131,6 +156,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
131
156
  label: 'Adver Int (seconds)',
132
157
  minimum: 0,
133
158
  maximum: 255,
159
+ default: 1,
134
160
  //v2 only: byte 5, advertisement interval in whole seconds (RFC 3768 §5.3.7).
135
161
  decode: function () {
136
162
  const version = this.instance.version.getValue(0);
@@ -215,21 +241,31 @@ class VRRP extends BaseHeader_1.BaseHeader {
215
241
  },
216
242
  checksum: {
217
243
  type: 'integer',
244
+ autoCalculated: true,
218
245
  label: 'Checksum',
219
246
  minimum: 0,
220
247
  maximum: 65535,
221
- //Honoured verbatim (faithful executor): decode reads it, encode writes the same value
222
- //back untouched. VRRP checksums differ by version (v2 over the message only, v3 over
223
- //an IP pseudo-header too), so recomputation is left to the caller.
248
+ //Left out, computed once every field is written (#calculateChecksum: v2 over the
249
+ //message only, v3 over an IP pseudo-header too); a given value is written as given.
224
250
  decode: function () {
225
251
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
226
252
  },
227
253
  encode: function () {
228
- let checksum = this.instance.checksum.getValue(0);
229
- checksum = checksum > 65535 ? 65535 : checksum;
230
- checksum = checksum < 0 ? 0 : checksum;
231
- this.instance.checksum.setValue(checksum);
232
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
254
+ if (!this.isLeftOut(this.instance.checksum)) {
255
+ let checksum = this.instance.checksum.getValue(0);
256
+ checksum = checksum > 65535 ? 65535 : checksum;
257
+ checksum = checksum < 0 ? 0 : checksum;
258
+ this.instance.checksum.setValue(checksum);
259
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
260
+ return;
261
+ }
262
+ this.instance.checksum.setValue(0);
263
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
264
+ this.addPostSelfEncodeHandler(() => {
265
+ const checksum = this.#calculateChecksum();
266
+ this.instance.checksum.setValue(checksum);
267
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
268
+ });
233
269
  }
234
270
  },
235
271
  addresses: {
@@ -57,6 +57,7 @@ class WCCP extends BaseHeader_1.BaseHeader {
57
57
  version: this.fieldUInt('version', 4, 2, 'Version'),
58
58
  length: {
59
59
  type: 'integer',
60
+ autoCalculated: true,
60
61
  label: 'Length',
61
62
  minimum: 0,
62
63
  maximum: 65535,
@@ -81,10 +81,10 @@ class WHOIS extends BaseHeader_1.BaseHeader {
81
81
  //Display-only metadata parsed from the verbatim payload on decode (no encode — populated
82
82
  //by the `message` field below, never read back). `query` is the first line (the request
83
83
  //domain or a response's opening line); `isQuery` flags the single-line request shape.
84
- isQuery: { type: 'boolean', label: 'Is Query', default: false },
85
- query: { type: 'string', label: 'Query', default: '' },
84
+ isQuery: { type: 'boolean', label: 'Is Query', readOnly: true, default: false },
85
+ query: { type: 'string', label: 'Query', readOnly: true, default: '' },
86
86
  //Drives the one-line summary: the first line of the payload, else 'response'.
87
- summaryInfo: { type: 'string', label: 'Summary', hidden: true, default: '' },
87
+ summaryInfo: { type: 'string', label: 'Summary', readOnly: true, hidden: true, default: '' },
88
88
  //The whole raw payload is the single source of truth: decoded verbatim to hex and
89
89
  //re-emitted untouched (byte-perfect for any WHOIS payload). The first line is parsed into
90
90
  //the display-only metadata above, which carry no codec of their own.
@@ -96,8 +96,8 @@ class WSDiscovery extends BaseHeader_1.BaseHeader {
96
96
  //Display-only metadata parsed from the WS-Addressing Action on decode (no encode —
97
97
  //populated by the message field above, never read back). action is the full Action URI;
98
98
  //messageType is its short trailing segment (Probe / Hello / Bye / …).
99
- action: { type: 'string', label: 'Action' },
100
- messageType: { type: 'string', label: 'Message Type' }
99
+ action: { type: 'string', label: 'Action', readOnly: true },
100
+ messageType: { type: 'string', label: 'Message Type', readOnly: true }
101
101
  }
102
102
  };
103
103
  }
@@ -124,9 +124,9 @@ class XMPP extends BaseHeader_1.BaseHeader {
124
124
  //message field above, never read back). rootElement is the first element's tag name (e.g.
125
125
  //'stream:stream', 'message', 'iq', 'presence'); isStreamHeader flags the stream preamble;
126
126
  //hasXmlDeclaration flags a leading `<?xml … ?>`.
127
- rootElement: { type: 'string', label: 'Root Element' },
128
- isStreamHeader: { type: 'boolean', label: 'Is Stream Header' },
129
- hasXmlDeclaration: { type: 'boolean', label: 'Has XML Declaration' }
127
+ rootElement: { type: 'string', label: 'Root Element', readOnly: true },
128
+ isStreamHeader: { type: 'boolean', label: 'Is Stream Header', readOnly: true },
129
+ hasXmlDeclaration: { type: 'boolean', label: 'Has XML Declaration', readOnly: true }
130
130
  }
131
131
  };
132
132
  }
@@ -85,6 +85,7 @@ class Zabbix extends BaseHeader_1.BaseHeader {
85
85
  flags: this.fieldUInt('flags', 4, 1, 'Flags'),
86
86
  dataLength: {
87
87
  type: 'integer',
88
+ autoCalculated: true,
88
89
  label: 'Data Length',
89
90
  minimum: 0,
90
91
  maximum: 4294967295,
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
3
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
4
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
5
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
6
+ * @param buffers
7
+ */
8
+ export declare function InternetChecksum(...buffers: Buffer[]): number;
9
+ /**
10
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
11
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
12
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
13
+ * @param buffers
14
+ */
15
+ export declare function CRC32c(...buffers: Buffer[]): number;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternetChecksum = InternetChecksum;
4
+ exports.CRC32c = CRC32c;
5
+ /**
6
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
7
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
8
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
9
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
10
+ * @param buffers
11
+ */
12
+ function InternetChecksum(...buffers) {
13
+ let sum = 0;
14
+ let odd = -1;
15
+ for (const buffer of buffers) {
16
+ for (let i = 0; i < buffer.length; i++) {
17
+ if (odd < 0) {
18
+ odd = buffer[i];
19
+ }
20
+ else {
21
+ sum += (odd << 8) | buffer[i];
22
+ odd = -1;
23
+ }
24
+ }
25
+ }
26
+ if (odd >= 0)
27
+ sum += odd << 8;
28
+ while (sum > 0xFFFF)
29
+ sum = (sum & 0xFFFF) + (sum >>> 16);
30
+ return (~sum) & 0xFFFF;
31
+ }
32
+ /** CRC-32C (Castagnoli) lookup table for the reflected polynomial 0x82F63B78, built once. */
33
+ const CRC32C_TABLE = (() => {
34
+ const table = new Uint32Array(256);
35
+ for (let n = 0; n < 256; n++) {
36
+ let crc = n;
37
+ for (let k = 0; k < 8; k++)
38
+ crc = (crc & 1) ? (crc >>> 1) ^ 0x82F63B78 : crc >>> 1;
39
+ table[n] = crc >>> 0;
40
+ }
41
+ return table;
42
+ })();
43
+ /**
44
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
45
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
46
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
47
+ * @param buffers
48
+ */
49
+ function CRC32c(...buffers) {
50
+ let crc = 0xFFFFFFFF;
51
+ for (const buffer of buffers) {
52
+ for (let i = 0; i < buffer.length; i++)
53
+ crc = CRC32C_TABLE[(crc ^ buffer[i]) & 0xFF] ^ (crc >>> 8);
54
+ }
55
+ return (crc ^ 0xFFFFFFFF) >>> 0;
56
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * CRC-32 as used by IEEE 802.3 / zlib (CRC-32/ISO-HDLC): polynomial 0x04C11DB7 processed reflected,
3
+ * initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF. Check value: Crc32('123456789') = 0xCBF43926.
4
+ * @param buffer
5
+ */
6
+ export declare function Crc32(buffer: Buffer): number;
7
+ /**
8
+ * CRC-16/DNP (IEEE 1815 data link layer): polynomial 0x3D65 processed reflected, initial value 0,
9
+ * final XOR 0xFFFF. DNP3 sends it low octet first. Check value: Crc16Dnp('123456789') = 0xEA82.
10
+ * @param buffer
11
+ */
12
+ export declare function Crc16Dnp(buffer: Buffer): number;
13
+ /**
14
+ * CRC-16/CCITT-FALSE (the IEEE C37.118.2 CHK): polynomial 0x1021 processed MSB-first, initial value
15
+ * 0xFFFF, no final XOR. Check value: Crc16CcittFalse('123456789') = 0x29B1.
16
+ * @param buffer
17
+ */
18
+ export declare function Crc16CcittFalse(buffer: Buffer): number;