@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
@@ -1,6 +1,39 @@
1
1
  import { NetKittyError } from '@netkitty/errors';
2
- /** Thrown at the encode entry point when an input fails Ajv shape validation against the header schema. */
2
+ /** One Ajv validation failure, plus the dotted field path the codec uses for its other errors. */
3
+ export type CodecSchemaValidationIssue = {
4
+ /** Ajv JSON Pointer to the offending value inside the layer's data, e.g. '/flags/rb'. */
5
+ instancePath: string;
6
+ /** Ajv JSON Pointer to the failing keyword inside the schema. */
7
+ schemaPath: string;
8
+ /** The failing JSON Schema keyword, e.g. 'minLength', 'enum', 'format'. */
9
+ keyword: string;
10
+ /** Keyword-specific details from Ajv, e.g. {limit: 7}. */
11
+ params: Record<string, unknown>;
12
+ message: string;
13
+ /** Dotted field path inside the layer, e.g. 'flags.rb' or 'options[0].kind'. */
14
+ path: string;
15
+ };
16
+ /**
17
+ * Thrown at the encode entry point when an input fails Ajv shape validation against the header schema.
18
+ * `layerId` / `layerIndex` name the input layer, `path` the field inside it (same dotted form as
19
+ * `result.errors[].path`), and `errors` carries Ajv's own error objects. `message` is Ajv's message for
20
+ * the first failure.
21
+ */
3
22
  export declare class CodecSchemaValidateError extends NetKittyError {
4
23
  errno: number;
5
24
  code: string;
25
+ /** PROTOCOL_ID of the input layer that failed validation, e.g. 'ipv4'. */
26
+ layerId: string;
27
+ /** Index of that layer in the encode() input array; -1 when validate() was called directly. */
28
+ layerIndex: number;
29
+ /** Dotted path of the failing field inside the layer, e.g. 'sip'. '' when the layer data itself failed. */
30
+ path: string;
31
+ /** Every Ajv error reported for this layer (validation stops at the first failing keyword). */
32
+ errors: CodecSchemaValidationIssue[];
33
+ constructor(message?: string, details?: {
34
+ layerId?: string;
35
+ layerIndex?: number;
36
+ path?: string;
37
+ errors?: CodecSchemaValidationIssue[];
38
+ });
6
39
  }
@@ -2,12 +2,35 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CodecSchemaValidateError = void 0;
4
4
  const errors_1 = require("@netkitty/errors");
5
- /** Thrown at the encode entry point when an input fails Ajv shape validation against the header schema. */
5
+ /**
6
+ * Thrown at the encode entry point when an input fails Ajv shape validation against the header schema.
7
+ * `layerId` / `layerIndex` name the input layer, `path` the field inside it (same dotted form as
8
+ * `result.errors[].path`), and `errors` carries Ajv's own error objects. `message` is Ajv's message for
9
+ * the first failure.
10
+ */
6
11
  class CodecSchemaValidateError extends errors_1.NetKittyError {
7
- constructor() {
8
- super(...arguments);
12
+ constructor(message, details) {
13
+ super(message);
9
14
  this.errno = errors_1.ErrorCode.E_CODEC_SCHEMA_VALIDATE.errno;
10
15
  this.code = errors_1.ErrorCode.E_CODEC_SCHEMA_VALIDATE.code;
16
+ /** PROTOCOL_ID of the input layer that failed validation, e.g. 'ipv4'. */
17
+ this.layerId = '';
18
+ /** Index of that layer in the encode() input array; -1 when validate() was called directly. */
19
+ this.layerIndex = -1;
20
+ /** Dotted path of the failing field inside the layer, e.g. 'sip'. '' when the layer data itself failed. */
21
+ this.path = '';
22
+ /** Every Ajv error reported for this layer (validation stops at the first failing keyword). */
23
+ this.errors = [];
24
+ if (details) {
25
+ if (details.layerId !== undefined)
26
+ this.layerId = details.layerId;
27
+ if (details.layerIndex !== undefined)
28
+ this.layerIndex = details.layerIndex;
29
+ if (details.path !== undefined)
30
+ this.path = details.path;
31
+ if (details.errors !== undefined)
32
+ this.errors = details.errors;
33
+ }
11
34
  }
12
35
  }
13
36
  exports.CodecSchemaValidateError = CodecSchemaValidateError;
@@ -103,6 +103,7 @@ class ADSAMS extends BaseHeader_1.BaseHeader {
103
103
  reserved: this.fieldHex('reserved', 0, 2, 'Reserved'),
104
104
  length: {
105
105
  type: 'integer',
106
+ autoCalculated: true,
106
107
  label: 'AMS/TCP Length',
107
108
  minimum: 0,
108
109
  maximum: 4294967295,
@@ -142,6 +143,7 @@ class ADSAMS extends BaseHeader_1.BaseHeader {
142
143
  stateFlags: this.#fieldUInt16LE('stateFlags', 24, 'State Flags'),
143
144
  dataLength: {
144
145
  type: 'integer',
146
+ autoCalculated: true,
145
147
  label: 'Data Length',
146
148
  minimum: 0,
147
149
  maximum: 4294967295,
@@ -69,11 +69,12 @@ class AH extends BaseHeader_1.BaseHeader {
69
69
  //Next Header is an IP-protocol number (the type of the protected payload); stored as
70
70
  //`nxt` — the field name IPv6 extension headers use — so the `ipproto` demux routes the
71
71
  //inner layer and TCP/UDP's `nxt`-aware match() accepts AH as a parent.
72
- nxt: this.fieldUInt('nxt', 0, 1, 'Next Header'),
72
+ nxt: { ...this.fieldUInt('nxt', 0, 1, 'Next Header'), autoCalculated: true },
73
73
  //AH total length in 4-octet words minus 2. Honored-else-derived: a supplied value is
74
74
  //kept verbatim, an absent (0) one is recomputed from the encoded ICV length.
75
75
  payloadLen: {
76
76
  type: 'integer',
77
+ autoCalculated: true,
77
78
  label: 'Payload Length',
78
79
  minimum: 0,
79
80
  maximum: 255,
@@ -81,10 +82,11 @@ class AH extends BaseHeader_1.BaseHeader {
81
82
  this.instance.payloadLen.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
82
83
  },
83
84
  encode: function () {
85
+ const leftOut = this.isLeftOut(this.instance.payloadLen);
84
86
  const payloadLen = this.instance.payloadLen.getValue(0);
85
87
  this.instance.payloadLen.setValue(payloadLen);
86
88
  this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(payloadLen));
87
- if (!payloadLen) {
89
+ if (leftOut) {
88
90
  this.addPostSelfEncodeHandler(() => {
89
91
  //Fixed prefix is 12 octets (3 words); total = 12 + ICV octets, and
90
92
  //Payload Len = totalWords - 2 = (12 + icvBytes)/4 - 2 = icvBytes/4 + 1.
@@ -115,7 +115,7 @@ class AMQP extends BaseHeader_1.BaseHeader {
115
115
  },
116
116
  //Decode-only display string for the Info summary (populated above); carries no encode, so
117
117
  //it never affects the emitted bytes.
118
- info: { type: 'string', label: 'Info' },
118
+ info: { type: 'string', label: 'Info', readOnly: true },
119
119
  // ===== Protocol-header handshake form ("AMQP" + four version octets) =====
120
120
  protocol: {
121
121
  type: 'string',
@@ -208,6 +208,7 @@ class AMQP extends BaseHeader_1.BaseHeader {
208
208
  //recorded), and a well-formed frame (Length == payload bytes) never triggers it.
209
209
  length: {
210
210
  type: 'integer',
211
+ autoCalculated: true,
211
212
  label: 'Length',
212
213
  minimum: 0,
213
214
  maximum: 4294967295,
@@ -133,9 +133,12 @@ class ARP extends BaseHeader_1.BaseHeader {
133
133
  }
134
134
  },
135
135
  opcode: {
136
- type: 'number',
136
+ type: 'integer',
137
137
  label: 'Opcode',
138
- enum: [1, 2, 3, 4],
138
+ //A value field (any 16-bit opcode can be decoded and must re-encode); 1–4 are the
139
+ //request/reply/RARP codes, anything else is flagged, not rejected.
140
+ minimum: 0,
141
+ maximum: 65535,
139
142
  decode: () => {
140
143
  this.instance.opcode.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
141
144
  if (![1, 2, 3, 4].includes(this.instance.opcode.getValue()))
@@ -158,13 +161,13 @@ class ARP extends BaseHeader_1.BaseHeader {
158
161
  label: 'MAC address',
159
162
  minLength: 17,
160
163
  maxLength: 17,
161
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
164
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.MAC,
162
165
  decode: () => {
163
166
  const macAddrBuffer = Buffer.alloc(6, this.readBytes(8, 6));
164
167
  this.instance.sender.mac.setValue(Array.from(macAddrBuffer).map((value) => (0, NumberToHex_1.UInt8ToHex)(value)).join(':'));
165
168
  },
166
169
  encode: () => {
167
- const macStr = this.instance.sender.mac.getValue();
170
+ const macStr = this.instance.sender.mac.getValue('00:00:00:00:00:00', (nodePath) => this.recordError(nodePath, 'Not Found'));
168
171
  const rawMacBuffer = Buffer.from(macStr.split(':').map((value) => parseInt(value, 16)).map((value) => value ? value : 0));
169
172
  if (rawMacBuffer.length !== 6)
170
173
  this.recordError(this.instance.sender.mac.getPath(), 'Invalid MAC address length');
@@ -176,13 +179,13 @@ class ARP extends BaseHeader_1.BaseHeader {
176
179
  label: 'IP address',
177
180
  minLength: 7,
178
181
  maxLength: 15,
179
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
182
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.IPv4,
180
183
  decode: () => {
181
184
  const ipv4Buffer = Buffer.alloc(4, this.readBytes(14, 4));
182
185
  this.instance.sender.ipv4.setValue((0, BufferToIP_1.BufferToIPv4)(ipv4Buffer));
183
186
  },
184
187
  encode: () => {
185
- const ipv4Str = this.instance.sender.ipv4.getValue();
188
+ const ipv4Str = this.instance.sender.ipv4.getValue('0.0.0.0', (nodePath) => this.recordError(nodePath, 'Not Found'));
186
189
  this.writeBytes(14, (0, IPToBuffer_1.IPv4ToBuffer)(ipv4Str));
187
190
  }
188
191
  }
@@ -197,13 +200,13 @@ class ARP extends BaseHeader_1.BaseHeader {
197
200
  label: 'MAC address',
198
201
  minLength: 17,
199
202
  maxLength: 17,
200
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
203
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.MAC,
201
204
  decode: () => {
202
205
  const macAddrBuffer = Buffer.alloc(6, this.readBytes(18, 6));
203
206
  this.instance.target.mac.setValue(Array.from(macAddrBuffer).map((value) => (0, NumberToHex_1.UInt8ToHex)(value)).join(':'));
204
207
  },
205
208
  encode: () => {
206
- const macStr = this.instance.target.mac.getValue();
209
+ const macStr = this.instance.target.mac.getValue('00:00:00:00:00:00', (nodePath) => this.recordError(nodePath, 'Not Found'));
207
210
  const rawMacBuffer = Buffer.from(macStr.split(':').map((value) => parseInt(value, 16)).map((value) => value ? value : 0));
208
211
  if (rawMacBuffer.length !== 6)
209
212
  this.recordError(this.instance.target.mac.getPath(), 'Invalid MAC address length');
@@ -215,13 +218,13 @@ class ARP extends BaseHeader_1.BaseHeader {
215
218
  label: 'IP address',
216
219
  minLength: 7,
217
220
  maxLength: 15,
218
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
221
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.IPv4,
219
222
  decode: () => {
220
223
  const ipv4Buffer = Buffer.alloc(4, this.readBytes(24, 4));
221
224
  this.instance.target.ipv4.setValue((0, BufferToIP_1.BufferToIPv4)(ipv4Buffer));
222
225
  },
223
226
  encode: () => {
224
- const ipv4Str = this.instance.target.ipv4.getValue();
227
+ const ipv4Str = this.instance.target.ipv4.getValue('0.0.0.0', (nodePath) => this.recordError(nodePath, 'Not Found'));
225
228
  this.writeBytes(24, (0, IPToBuffer_1.IPv4ToBuffer)(ipv4Str));
226
229
  }
227
230
  }
@@ -237,6 +240,9 @@ class ARP extends BaseHeader_1.BaseHeader {
237
240
  match() {
238
241
  if (!this.prevCodecModule)
239
242
  return false;
243
+ //The fields model the 28-octet Ethernet/IPv4 ARP packet; a shorter one is left to raw (byte-exact).
244
+ if (this.packet.length - this.startPos < 28)
245
+ return false;
240
246
  return this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x0806);
241
247
  }
242
248
  }
@@ -44,6 +44,7 @@ class BACnetIP extends BaseHeader_1.BaseHeader {
44
44
  'function': this.fieldUInt('function', 1, 1, 'Function'),
45
45
  length: {
46
46
  type: 'integer',
47
+ autoCalculated: true,
47
48
  label: 'Length',
48
49
  minimum: 0,
49
50
  maximum: 65535,
@@ -13,7 +13,8 @@ import { DemuxProducer } from '../types/DemuxProducer';
13
13
  * If the A (Authentication Present) flag is set an optional Authentication Section follows the
14
14
  * mandatory 24 bytes (Auth Type, Auth Len, then type-specific auth data). It is kept verbatim as
15
15
  * `authSection` hex so any auth variant round-trips byte-for-byte; when A is clear it is empty. The
16
- * Length field is honoured verbatim (not recomputed) so a captured packet re-encodes byte-for-byte.
16
+ * Length field is written as given (so a captured packet re-encodes byte-for-byte); left out, it is the
17
+ * 24 mandatory bytes plus the authentication section.
17
18
  * BFD Control is a leaf — nothing rides on top of it.
18
19
  */
19
20
  export declare class BFD extends BaseHeader {
@@ -5,6 +5,8 @@ const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
8
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
9
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
8
10
  /**
9
11
  * BFD — Bidirectional Forwarding Detection Control (RFC 5880), UDP port 3784 (single-hop) and 4784
10
12
  * (multi-hop). The mandatory section is 24 bytes: a first byte carrying Version[3] (= 1) and a 5-bit
@@ -17,7 +19,8 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
17
19
  * If the A (Authentication Present) flag is set an optional Authentication Section follows the
18
20
  * mandatory 24 bytes (Auth Type, Auth Len, then type-specific auth data). It is kept verbatim as
19
21
  * `authSection` hex so any auth variant round-trips byte-for-byte; when A is clear it is empty. The
20
- * Length field is honoured verbatim (not recomputed) so a captured packet re-encodes byte-for-byte.
22
+ * Length field is written as given (so a captured packet re-encodes byte-for-byte); left out, it is the
23
+ * 24 mandatory bytes plus the authentication section.
21
24
  * BFD Control is a leaf — nothing rides on top of it.
22
25
  */
23
26
  class BFD extends BaseHeader_1.BaseHeader {
@@ -70,11 +73,13 @@ class BFD extends BaseHeader_1.BaseHeader {
70
73
  properties: {
71
74
  //Byte 0: Version (top 3 bits, = 1 for RFC 5880) + Diagnostic (low 5 bits). MSB-first,
72
75
  //so version is bitOffset 0..2 and diagnostic bitOffset 3..7 of a 1-octet window.
76
+ //Default 1: the RFC 5880 version, which encode writes when it is left out.
73
77
  version: {
74
78
  type: 'integer',
75
79
  label: 'Version',
76
80
  minimum: 0,
77
81
  maximum: 7,
82
+ default: 1,
78
83
  decode: function () { this.instance.version.setValue(this.readBits(0, 1, 0, 3)); },
79
84
  encode: function () { this.writeBits(0, 1, 0, 3, this.instance.version.getValue(1)); }
80
85
  },
@@ -108,9 +113,30 @@ class BFD extends BaseHeader_1.BaseHeader {
108
113
  }
109
114
  },
110
115
  detectMult: this.fieldUInt('detectMult', 2, 1, 'Detect Mult'),
111
- //Length of the entire BFD Control packet in bytes. Honoured verbatim (a real no-auth
112
- //packet carries 24) — not recomputed, so a captured packet re-encodes byte-for-byte.
113
- length: this.fieldUInt('length', 3, 1, 'Length'),
116
+ //Length of the entire BFD Control packet in bytes (a real no-auth packet carries 24).
117
+ length: {
118
+ type: 'integer',
119
+ autoCalculated: true,
120
+ label: 'Length',
121
+ minimum: 0,
122
+ maximum: 255,
123
+ decode: function () {
124
+ this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(3, 1)));
125
+ },
126
+ encode: function () {
127
+ if (!this.isLeftOut(this.instance.length)) {
128
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.length.getValue(0)));
129
+ return;
130
+ }
131
+ let length = 24 + (0, HexToBuffer_1.HexToBuffer)(this.instance.authSection.getValue('')).length;
132
+ if (length > 255) {
133
+ this.recordError(this.instance.length.getPath(), 'Maximum value is 255');
134
+ length = 255;
135
+ }
136
+ this.instance.length.setValue(length);
137
+ this.writeBytes(3, (0, NumberToBuffer_1.UInt8ToBuffer)(length));
138
+ }
139
+ },
114
140
  myDiscriminator: this.fieldUInt('myDiscriminator', 4, 4, 'My Discriminator'),
115
141
  yourDiscriminator: this.fieldUInt('yourDiscriminator', 8, 4, 'Your Discriminator'),
116
142
  desiredMinTxInterval: this.fieldUInt('desiredMinTxInterval', 12, 4, 'Desired Min TX Interval'),
@@ -45,8 +45,11 @@ class BGP extends BaseHeader_1.BaseHeader {
45
45
  marker: this.fieldHex('marker', 0, 16, 'Marker'),
46
46
  length: {
47
47
  type: 'integer',
48
+ autoCalculated: true,
48
49
  label: 'Length',
49
- minimum: 19,
50
+ //The field floor, not the 19-byte minimum message: a given value is written as
51
+ //given (flagged when too short) so a malformed message can be crafted.
52
+ minimum: 0,
50
53
  maximum: 65535,
51
54
  decode: function () {
52
55
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(16, 2)));
@@ -62,6 +65,8 @@ class BGP extends BaseHeader_1.BaseHeader {
62
65
  this.recordError(this.instance.length.getPath(), 'Maximum value is 65535');
63
66
  value = 65535;
64
67
  }
68
+ if (value < 19)
69
+ this.recordError(this.instance.length.getPath(), 'Length should be at least 19');
65
70
  this.instance.length.setValue(value);
66
71
  this.writeBytes(16, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
67
72
  }
@@ -70,6 +70,7 @@ class Babel extends BaseHeader_1.BaseHeader {
70
70
  //correct even though it encodes before the `tlvs` field writes them.
71
71
  bodyLength: {
72
72
  type: 'integer',
73
+ autoCalculated: true,
73
74
  label: 'Body Length',
74
75
  minimum: 0,
75
76
  maximum: 65535,
@@ -119,7 +120,7 @@ class Babel extends BaseHeader_1.BaseHeader {
119
120
  label: 'TLV',
120
121
  properties: {
121
122
  type: { type: 'integer', label: 'Type', minimum: 0, maximum: 255 },
122
- length: { type: 'integer', label: 'Length', minimum: 0, maximum: 255 },
123
+ length: { type: 'integer', label: 'Length', autoCalculated: true, minimum: 0, maximum: 255 },
123
124
  value: { type: 'string', label: 'Value', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
124
125
  }
125
126
  },
@@ -1,4 +1,5 @@
1
1
  import { BaseHeader } from '../abstracts/BaseHeader';
2
+ import { HeuristicParent } from '../types/LayerGraph';
2
3
  import { ProtocolJSONSchema } from '../schema/ProtocolJSONSchema';
3
4
  import { DemuxProducer } from '../types/DemuxProducer';
4
5
  /**
@@ -22,6 +23,7 @@ export declare class BitTorrent extends BaseHeader {
22
23
  readonly name: string;
23
24
  readonly nickname: string;
24
25
  readonly matchKeys: string[];
26
+ readonly heuristicParents: HeuristicParent[];
25
27
  readonly heuristicFallback: boolean;
26
28
  match(): boolean;
27
29
  readonly demuxProducers: DemuxProducer[];
@@ -26,6 +26,8 @@ class BitTorrent extends BaseHeader_1.BaseHeader {
26
26
  //No fixed port — BitTorrent peers connect on dynamic/ephemeral ports. Selection is by the
27
27
  //content signature (0x13 + "BitTorrent protocol") via heuristicFallback + match().
28
28
  this.matchKeys = [];
29
+ //Editor graph: matched by content, so list it under these parents in allowedNextLayers().
30
+ this.heuristicParents = ['tcp'];
29
31
  this.heuristicFallback = true;
30
32
  //A leaf header — only the handshake is decoded; subsequent peer messages are left to raw.
31
33
  this.demuxProducers = [];
@@ -41,10 +43,10 @@ class BitTorrent extends BaseHeader_1.BaseHeader {
41
43
  properties: {
42
44
  //Protocol string length — always 0x13 (19) for the BEP 3 handshake. Kept as a plain uint8
43
45
  //(a crafted handshake may lie); the signature match already guards real traffic.
44
- pstrlen: this.fieldUInt('pstrlen', 0, 1, 'Protocol String Length'),
46
+ pstrlen: { ...this.fieldUInt('pstrlen', 0, 1, 'Protocol String Length'), default: 19 },
45
47
  //The 19-byte protocol identifier "BitTorrent protocol". Kept verbatim as hex so any
46
48
  //byte edit round-trips; the human-readable form is UI enrichment for later.
47
- pstr: this.fieldHex('pstr', 1, 19, 'Protocol String'),
49
+ pstr: { ...this.fieldHex('pstr', 1, 19, 'Protocol String'), default: '426974546f7272656e742070726f746f636f6c' },
48
50
  //8 reserved bytes carrying extension bit-flags (BEP 4/5/6/10: DHT, Fast, Extension
49
51
  //Protocol). Opaque here and re-emitted verbatim.
50
52
  reserved: this.fieldHex('reserved', 20, 8, 'Reserved'),
@@ -11,8 +11,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
11
11
  *
12
12
  * The frame body between the header and the CHK is frame-type-specific (a configuration frame defines
13
13
  * the data layout for the data frames that follow — cross-frame state, not a single-packet concern), so
14
- * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE, and keeps the CHK verbatim
15
- * (never recomputed). A well-formed frame round-trips byte-for-byte.
14
+ * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE. FRAMESIZE and CHK are written
15
+ * as given; left out, FRAMESIZE is the actual frame length and CHK the CRC-CCITT (CRC-16/CCITT-FALSE:
16
+ * polynomial 0x1021, initial 0xFFFF) of every octet before it. A well-formed frame round-trips byte-for-byte.
16
17
  */
17
18
  export declare class C37118 extends BaseHeader {
18
19
  #private;
@@ -7,6 +7,7 @@ const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const BufferToNumber_1 = require("../helper/BufferToNumber");
8
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
9
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
10
+ const Crc_1 = require("../helper/Crc");
10
11
  /**
11
12
  * C37.118 — IEEE Std C37.118.2 synchrophasor data transfer (PMU/PDC), typically TCP port 4712 and UDP
12
13
  * port 4713. Every frame shares a 14-byte common header: SYNC (a 0xAA lead-in byte, then a byte holding
@@ -17,8 +18,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
17
18
  *
18
19
  * The frame body between the header and the CHK is frame-type-specific (a configuration frame defines
19
20
  * the data layout for the data frames that follow — cross-frame state, not a single-packet concern), so
20
- * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE, and keeps the CHK verbatim
21
- * (never recomputed). A well-formed frame round-trips byte-for-byte.
21
+ * this codec keeps the body verbatim as `body` hex, bounded by FRAMESIZE. FRAMESIZE and CHK are written
22
+ * as given; left out, FRAMESIZE is the actual frame length and CHK the CRC-CCITT (CRC-16/CCITT-FALSE:
23
+ * polynomial 0x1021, initial 0xFFFF) of every octet before it. A well-formed frame round-trips byte-for-byte.
22
24
  */
23
25
  class C37118 extends BaseHeader_1.BaseHeader {
24
26
  constructor() {
@@ -40,7 +42,8 @@ class C37118 extends BaseHeader_1.BaseHeader {
40
42
  type: 'object',
41
43
  summary: 'C37.118 type=${sync.frameType} id=${idcode}',
42
44
  properties: {
43
- leadIn: this.fieldHex('leadIn', 0, 1, 'Lead-in (0xAA)'),
45
+ //Default 0xAA: the SYNC lead-in every frame begins with, and what match() requires.
46
+ leadIn: { ...this.fieldHex('leadIn', 0, 1, 'Lead-in (0xAA)'), default: 'aa' },
44
47
  //SYNC byte 1 (MSB first): reserved bit, 3-bit Frame Type, 4-bit Version.
45
48
  sync: {
46
49
  type: 'object',
@@ -56,8 +59,9 @@ class C37118 extends BaseHeader_1.BaseHeader {
56
59
  decode: function () { this.instance.sync.frameType.setValue(this.readBits(1, 1, 1, 3)); },
57
60
  encode: function () { this.writeBits(1, 1, 1, 3, this.instance.sync.frameType.getValue(0)); }
58
61
  },
62
+ //Default 1 (IEEE C37.118-2005), the version encode writes when it is left out.
59
63
  version: {
60
- type: 'integer', label: 'Version', minimum: 0, maximum: 15,
64
+ type: 'integer', label: 'Version', minimum: 0, maximum: 15, default: 1,
61
65
  decode: function () { this.instance.sync.version.setValue(this.readBits(1, 1, 4, 4)); },
62
66
  encode: function () { this.writeBits(1, 1, 4, 4, this.instance.sync.version.getValue(1)); }
63
67
  }
@@ -65,6 +69,7 @@ class C37118 extends BaseHeader_1.BaseHeader {
65
69
  },
66
70
  framesize: {
67
71
  type: 'integer',
72
+ autoCalculated: true,
68
73
  label: 'Frame Size',
69
74
  minimum: 0,
70
75
  maximum: 65535,
@@ -73,11 +78,20 @@ class C37118 extends BaseHeader_1.BaseHeader {
73
78
  },
74
79
  encode: function () {
75
80
  //Honored when supplied (a crafted frame may lie); else derived from the actual
76
- //body + CHK (FRAMESIZE is the total frame length: 14-byte header + body + CHK).
77
- const provided = this.instance.framesize.getValue();
78
- const value = (provided !== undefined && provided !== null)
79
- ? provided
80
- : 14 + (0, HexToBuffer_1.HexToBuffer)(this.instance.body.getValue('')).length + (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')).length;
81
+ //body + CHK (FRAMESIZE is the total frame length: 14-byte header + body + CHK, the
82
+ //CHK being 2 bytes when it is left out to be computed).
83
+ let value;
84
+ if (!this.isLeftOut(this.instance.framesize)) {
85
+ value = this.instance.framesize.getValue(0);
86
+ }
87
+ else {
88
+ const chkLength = this.isLeftOut(this.instance.chk) ? 2 : (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')).length;
89
+ value = 14 + (0, HexToBuffer_1.HexToBuffer)(this.instance.body.getValue('')).length + chkLength;
90
+ if (value > 65535) {
91
+ this.recordError(this.instance.framesize.getPath(), 'Maximum value is 65535');
92
+ value = 65535;
93
+ }
94
+ }
81
95
  this.instance.framesize.setValue(value);
82
96
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
83
97
  }
@@ -121,14 +135,20 @@ class C37118 extends BaseHeader_1.BaseHeader {
121
135
  const bodyBuffer = (0, HexToBuffer_1.HexToBuffer)(body);
122
136
  if (bodyBuffer.length)
123
137
  this.writeBytes(14, bodyBuffer);
124
- const chk = this.instance.chk.getValue('');
125
- if (chk)
126
- this.writeBytes(14 + bodyBuffer.length, (0, HexToBuffer_1.HexToBuffer)(chk));
138
+ const chkOffset = 14 + bodyBuffer.length;
139
+ if (!this.isLeftOut(this.instance.chk)) {
140
+ this.writeBytes(chkOffset, (0, HexToBuffer_1.HexToBuffer)(this.instance.chk.getValue('')));
141
+ return;
142
+ }
143
+ //Every octet before the CHK (header, FRAMESIZE included, and body) is written by now.
144
+ const chk = (0, NumberToBuffer_1.UInt16ToBuffer)((0, Crc_1.Crc16CcittFalse)(this.readBytes(0, chkOffset)));
145
+ this.instance.chk.setValue((0, BufferToHex_1.BufferToHex)(chk));
146
+ this.writeBytes(chkOffset, chk);
127
147
  }
128
148
  },
129
- //The check word (CRC-CCITT), honored verbatim (never recomputed) — populated by the body
130
- //master since its offset (FRAMESIZE - 2) is frame-length dependent.
131
- chk: { type: 'string', label: 'Check', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
149
+ //The check word (CRC-CCITT, big-endian) — encoded by the body master since its offset
150
+ //(FRAMESIZE - 2) is frame-length dependent; computed there when left out.
151
+ chk: { type: 'string', autoCalculated: true, label: 'Check', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
132
152
  }
133
153
  };
134
154
  }
@@ -106,6 +106,7 @@ class CAPWAP extends BaseHeader_1.BaseHeader {
106
106
  //header plus the verbatim optional (headerRemainder) bytes. HLEN × 4 delimits the header.
107
107
  hlen: {
108
108
  type: 'integer',
109
+ autoCalculated: true,
109
110
  label: 'HLEN',
110
111
  minimum: 0,
111
112
  maximum: 31,
@@ -9,9 +9,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
9
9
  * Capabilities, 0x0005 Software Version, 0x0006 Platform.
10
10
  *
11
11
  * The TLV values are kept verbatim as hex (device names, address lists and capability bitmaps need no
12
- * form structure), the Checksum is honored verbatim (never recomputed), and the TLV walk is bounded by
13
- * the captured bytes so a corrupt Length can't run past the frame. A well-formed CDP frame round-trips
14
- * byte-for-byte.
12
+ * form structure), and the TLV walk is bounded by the captured bytes so a corrupt Length can't run past
13
+ * the frame. The Checksum and each TLV Length are computed on encode when left out and written as given
14
+ * otherwise. A well-formed CDP frame round-trips byte-for-byte.
15
15
  */
16
16
  export declare class CDP extends BaseHeader {
17
17
  #private;