@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
@@ -82,6 +82,7 @@ class NBDS extends BaseHeader_1.BaseHeader {
82
82
  //derived from the body. Meaningless for other types (not present on the wire — left 0).
83
83
  dgmLength: {
84
84
  type: 'integer',
85
+ autoCalculated: true,
85
86
  label: 'Datagram Length',
86
87
  minimum: 0,
87
88
  maximum: 65535,
@@ -72,6 +72,7 @@ class NBSS extends BaseHeader_1.BaseHeader {
72
72
  flags: this.fieldUInt('flags', 1, 1, 'Flags'),
73
73
  length: {
74
74
  type: 'integer',
75
+ autoCalculated: true,
75
76
  label: 'Length',
76
77
  minimum: 0,
77
78
  maximum: 65535,
@@ -135,6 +135,7 @@ class NFS extends BaseHeader_1.BaseHeader {
135
135
  },
136
136
  fragmentLength: {
137
137
  type: 'integer',
138
+ autoCalculated: true,
138
139
  label: 'Fragment Length',
139
140
  minimum: 0,
140
141
  maximum: 2147483647,
@@ -16,7 +16,7 @@ import { DemuxProducer } from '../types/DemuxProducer';
16
16
  * driven by ar$shtl and the per-entry CIE type/length octets) and carry cross-field context, so this
17
17
  * codec keeps them verbatim as `body` hex (byte-perfect) and does not sub-decode them. Packet Length is
18
18
  * auto-computed from the body on encode when not supplied, else honored verbatim (a crafted packet may
19
- * lie); the checksum is honored verbatim, never recomputed (encode is a faithful executor). The body is
19
+ * lie); likewise the checksum is computed over the whole NHRP packet when left out. The body is
20
20
  * bounded by both Packet Length and the enclosing IP payload, so trailing bytes are left to the codec's
21
21
  * recursion / RawData. A well-formed packet round-trips byte-for-byte.
22
22
  */
@@ -6,6 +6,7 @@ const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
7
  const BufferToNumber_1 = require("../helper/BufferToNumber");
8
8
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
9
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
10
11
  /**
11
12
  * NHRP — NBMA Next Hop Resolution Protocol (RFC 2332), carried directly over IP as protocol 54. Every
@@ -22,7 +23,7 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
22
23
  * driven by ar$shtl and the per-entry CIE type/length octets) and carry cross-field context, so this
23
24
  * codec keeps them verbatim as `body` hex (byte-perfect) and does not sub-decode them. Packet Length is
24
25
  * auto-computed from the body on encode when not supplied, else honored verbatim (a crafted packet may
25
- * lie); the checksum is honored verbatim, never recomputed (encode is a faithful executor). The body is
26
+ * lie); likewise the checksum is computed over the whole NHRP packet when left out. The body is
26
27
  * bounded by both Packet Length and the enclosing IP payload, so trailing bytes are left to the codec's
27
28
  * recursion / RawData. A well-formed packet round-trips byte-for-byte.
28
29
  */
@@ -89,6 +90,7 @@ class NHRP extends BaseHeader_1.BaseHeader {
89
90
  hopCount: this.fieldUInt('hopCount', 9, 1, 'Hop Count'),
90
91
  packetSize: {
91
92
  type: 'integer',
93
+ autoCalculated: true,
92
94
  label: 'Packet Length',
93
95
  minimum: 0,
94
96
  maximum: 65535,
@@ -110,9 +112,33 @@ class NHRP extends BaseHeader_1.BaseHeader {
110
112
  this.writeBytes(10, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
111
113
  }
112
114
  },
113
- //Honored verbatim: the NHRP checksum (ones-complement over the whole packet) is never
114
- //recomputed, so a captured packet round-trips byte-for-byte.
115
- checksum: this.fieldUInt('checksum', 12, 2, 'Checksum'),
115
+ //Internet checksum over the whole NHRP packet from the Fixed Header on (RFC 2332 §5.2.0.1),
116
+ //computed once every field is written when left out; a given value is written as given.
117
+ checksum: {
118
+ type: 'integer',
119
+ autoCalculated: true,
120
+ label: 'Checksum',
121
+ minimum: 0,
122
+ maximum: 65535,
123
+ decode: function () {
124
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2)));
125
+ },
126
+ encode: function () {
127
+ if (!this.isLeftOut(this.instance.checksum)) {
128
+ const checksum = this.instance.checksum.getValue(0);
129
+ this.instance.checksum.setValue(checksum);
130
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
131
+ return;
132
+ }
133
+ this.instance.checksum.setValue(0);
134
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
135
+ this.addPostSelfEncodeHandler(() => {
136
+ const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
137
+ this.instance.checksum.setValue(checksum);
138
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
139
+ });
140
+ }
141
+ },
116
142
  extensionOffset: this.fieldUInt('extensionOffset', 14, 2, 'Extension Offset'),
117
143
  opVersion: this.fieldUInt('opVersion', 16, 1, 'Version'),
118
144
  opType: this.fieldUInt('opType', 17, 1, 'Operation Type'),
@@ -130,12 +130,12 @@ class NNTP extends BaseHeader_1.BaseHeader {
130
130
  //message field above, never read back). isReply distinguishes a server reply from a
131
131
  //client command; replyCode/isMultiline/replyText describe a reply, command/argument a
132
132
  //command.
133
- isReply: { type: 'boolean', label: 'Is Reply' },
134
- replyCode: { type: 'integer', label: 'Reply Code', minimum: 0, maximum: 999 },
135
- isMultiline: { type: 'boolean', label: 'Is Multiline' },
136
- replyText: { type: 'string', label: 'Reply Text' },
137
- command: { type: 'string', label: 'Command' },
138
- argument: { type: 'string', label: 'Argument' }
133
+ isReply: { type: 'boolean', label: 'Is Reply', readOnly: true },
134
+ replyCode: { type: 'integer', label: 'Reply Code', readOnly: true, minimum: 0, maximum: 999 },
135
+ isMultiline: { type: 'boolean', label: 'Is Multiline', readOnly: true },
136
+ replyText: { type: 'string', label: 'Reply Text', readOnly: true },
137
+ command: { type: 'string', label: 'Command', readOnly: true },
138
+ argument: { type: 'string', label: 'Argument', readOnly: true }
139
139
  }
140
140
  };
141
141
  }
@@ -88,6 +88,7 @@ class NetFlowV5 extends BaseHeader_1.BaseHeader {
88
88
  version: this.fieldUInt('version', 0, 2, 'Version'),
89
89
  count: {
90
90
  type: 'integer',
91
+ autoCalculated: true,
91
92
  label: 'Count',
92
93
  minimum: 0,
93
94
  maximum: 65535,
@@ -77,6 +77,7 @@ class OLSR extends BaseHeader_1.BaseHeader {
77
77
  properties: {
78
78
  packetLength: {
79
79
  type: 'integer',
80
+ autoCalculated: true,
80
81
  label: 'Packet Length',
81
82
  minimum: 0,
82
83
  maximum: 65535,
@@ -128,7 +129,7 @@ class OLSR extends BaseHeader_1.BaseHeader {
128
129
  //Wire fields are plain integers (no hard enum) so any on-wire value re-encodes.
129
130
  messageType: { type: 'integer', label: 'Message Type', minimum: 0, maximum: 255 },
130
131
  vTime: { type: 'integer', label: 'Vtime', minimum: 0, maximum: 255 },
131
- messageSize: { type: 'integer', label: 'Message Size', minimum: 0, maximum: 65535 },
132
+ messageSize: { type: 'integer', label: 'Message Size', autoCalculated: true, minimum: 0, maximum: 65535 },
132
133
  originatorAddress: { type: 'string', label: 'Originator Address', minLength: 7, maxLength: 15, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.IPv4 },
133
134
  ttl: { type: 'integer', label: 'Time To Live', minimum: 0, maximum: 255 },
134
135
  hopCount: { type: 'integer', label: 'Hop Count', minimum: 0, maximum: 255 },
@@ -93,6 +93,7 @@ class OPCUA extends BaseHeader_1.BaseHeader {
93
93
  //supplied (a crafted message may lie); else derived as 8 + body bytes.
94
94
  messageSize: {
95
95
  type: 'integer',
96
+ autoCalculated: true,
96
97
  label: 'Message Size',
97
98
  minimum: 0,
98
99
  maximum: 4294967295,
@@ -6,9 +6,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
6
6
  * decodes the 24-octet common header: an 8-bit Version (`version`, offset 0), an 8-bit Message Type
7
7
  * (`type`, offset 1; 1 Hello, 2 Database Description, 3 LS Request, 4 LS Update, 5 LS Ack), a 16-bit
8
8
  * Packet Length (`packetLength`, offset 2), the Source Router ID (`routerId`, IPv4, offset 4), the Area
9
- * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12, honored verbatim and never
10
- * recomputed so a captured packet round-trips), a 16-bit Auth Type (`auType`, offset 14) and the 8-octet
11
- * Authentication field (`auth`, hex, offset 16).
9
+ * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12), a 16-bit Auth Type (`auType`,
10
+ * offset 14) and the 8-octet Authentication field (`auth`, hex, offset 16). Packet Length and Checksum
11
+ * are computed on encode when left out and written as given otherwise.
12
12
  *
13
13
  * For a Hello (type 1) the body is decoded field-by-field: Network Mask (offset 24), 16-bit Hello Interval
14
14
  * (offset 28), 8-bit Options (offset 30), 8-bit Router Priority (offset 31), 32-bit Router Dead Interval
@@ -6,14 +6,17 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
6
6
  const BufferToIP_1 = require("../helper/BufferToIP");
7
7
  const IPToBuffer_1 = require("../helper/IPToBuffer");
8
8
  const BufferToHex_1 = require("../helper/BufferToHex");
9
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
10
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
11
+ const Checksum_1 = require("../helper/Checksum");
9
12
  /**
10
13
  * OSPF — Open Shortest Path First (OSPFv2, RFC 2328), carried directly on IP as protocol 89. This codec
11
14
  * decodes the 24-octet common header: an 8-bit Version (`version`, offset 0), an 8-bit Message Type
12
15
  * (`type`, offset 1; 1 Hello, 2 Database Description, 3 LS Request, 4 LS Update, 5 LS Ack), a 16-bit
13
16
  * Packet Length (`packetLength`, offset 2), the Source Router ID (`routerId`, IPv4, offset 4), the Area
14
- * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12, honored verbatim and never
15
- * recomputed so a captured packet round-trips), a 16-bit Auth Type (`auType`, offset 14) and the 8-octet
16
- * Authentication field (`auth`, hex, offset 16).
17
+ * ID (`areaId`, IPv4, offset 8), a 16-bit Checksum (`checksum`, offset 12), a 16-bit Auth Type (`auType`,
18
+ * offset 14) and the 8-octet Authentication field (`auth`, hex, offset 16). Packet Length and Checksum
19
+ * are computed on encode when left out and written as given otherwise.
17
20
  *
18
21
  * For a Hello (type 1) the body is decoded field-by-field: Network Mask (offset 24), 16-bit Hello Interval
19
22
  * (offset 28), 8-bit Options (offset 30), 8-bit Router Priority (offset 31), 32-bit Router Dead Interval
@@ -69,6 +72,19 @@ class OSPF extends BaseHeader_1.BaseHeader {
69
72
  end = 24;
70
73
  return end;
71
74
  }
75
+ /**
76
+ * The OSPFv2 checksum (RFC 2328 §A.3.1): the Internet checksum of the whole packet except the 64-bit
77
+ * Authentication field (bytes 16-23). With cryptographic authentication (AuType 2) the checksum is
78
+ * not calculated and is 0 (§D.4.3). Called once every field is written, with the checksum still zero.
79
+ * @private
80
+ */
81
+ #calculateChecksum() {
82
+ const auType = this.instance.auType.getValue(0);
83
+ if (auType === 2)
84
+ return 0;
85
+ const packet = this.packet.subarray(this.startPos, this.endPos);
86
+ return (0, Checksum_1.InternetChecksum)(packet.subarray(0, 16), packet.subarray(24));
87
+ }
72
88
  static #schemaCache;
73
89
  //Class-cached SCHEMA (field closures are plain functions taking dynamic `this` via .call(this)).
74
90
  get SCHEMA() {
@@ -82,7 +98,32 @@ class OSPF extends BaseHeader_1.BaseHeader {
82
98
  //==== Common header (24 bytes, RFC 2328 §A.3.1) ====
83
99
  version: this.fieldUInt('version', 0, 1, 'Version'),
84
100
  type: this.fieldUInt('type', 1, 1, 'Message Type'),
85
- packetLength: this.fieldUInt('packetLength', 2, 2, 'Packet Length'),
101
+ //The whole OSPF packet including this header; left out, the bytes actually written, filled
102
+ //once every field is encoded (before the checksum); a given value is written as given.
103
+ packetLength: {
104
+ type: 'integer',
105
+ autoCalculated: true,
106
+ label: 'Packet Length',
107
+ minimum: 0,
108
+ maximum: 65535,
109
+ decode: function () {
110
+ this.instance.packetLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
111
+ },
112
+ encode: function () {
113
+ if (!this.isLeftOut(this.instance.packetLength)) {
114
+ const packetLength = this.instance.packetLength.getValue(0);
115
+ this.instance.packetLength.setValue(packetLength);
116
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(packetLength));
117
+ return;
118
+ }
119
+ this.instance.packetLength.setValue(0);
120
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
121
+ this.addPostSelfEncodeHandler(() => {
122
+ this.instance.packetLength.setValue(this.length);
123
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(this.length));
124
+ }, 1);
125
+ }
126
+ },
86
127
  routerId: {
87
128
  type: 'string',
88
129
  label: 'Source Router ID',
@@ -113,9 +154,33 @@ class OSPF extends BaseHeader_1.BaseHeader {
113
154
  this.writeBytes(8, (0, IPToBuffer_1.IPv4ToBuffer)(areaId));
114
155
  }
115
156
  },
116
- //Honored verbatim: the OSPF checksum (ones-complement over the packet excluding the
117
- //authentication field) is never recomputed, so a captured packet round-trips byte-for-byte.
118
- checksum: this.fieldUInt('checksum', 12, 2, 'Checksum'),
157
+ //Left out, computed after the Packet Length once every field is written
158
+ //(#calculateChecksum); a given value is written as given.
159
+ checksum: {
160
+ type: 'integer',
161
+ autoCalculated: true,
162
+ label: 'Checksum',
163
+ minimum: 0,
164
+ maximum: 65535,
165
+ decode: function () {
166
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(12, 2)));
167
+ },
168
+ encode: function () {
169
+ if (!this.isLeftOut(this.instance.checksum)) {
170
+ const checksum = this.instance.checksum.getValue(0);
171
+ this.instance.checksum.setValue(checksum);
172
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
173
+ return;
174
+ }
175
+ this.instance.checksum.setValue(0);
176
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
177
+ this.addPostSelfEncodeHandler(() => {
178
+ const checksum = this.#calculateChecksum();
179
+ this.instance.checksum.setValue(checksum);
180
+ this.writeBytes(12, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
181
+ }, 2);
182
+ }
183
+ },
119
184
  auType: this.fieldUInt('auType', 14, 2, 'Auth Type'),
120
185
  auth: {
121
186
  type: 'string',
@@ -51,6 +51,7 @@ class OpenFlow extends BaseHeader_1.BaseHeader {
51
51
  type: this.fieldUInt('type', 1, 1, 'Type'),
52
52
  length: {
53
53
  type: 'integer',
54
+ autoCalculated: true,
54
55
  label: 'Length',
55
56
  //Full uint16 range: Length is honored verbatim on encode, so any on-wire value
56
57
  //(including a crafted/short one) must re-encode without Ajv rejecting it.
@@ -85,6 +85,7 @@ class OpenVPN extends BaseHeader_1.BaseHeader {
85
85
  //The number of OpenVPN packet bytes that follow this prefix. Absent over UDP.
86
86
  length: {
87
87
  type: 'integer',
88
+ autoCalculated: true,
88
89
  label: 'Length',
89
90
  minimum: 0,
90
91
  maximum: 65535,
@@ -56,6 +56,7 @@ class PCWorx extends BaseHeader_1.BaseHeader {
56
56
  service: this.fieldUInt('service', 1, 1, 'Service'),
57
57
  length: {
58
58
  type: 'integer',
59
+ autoCalculated: true,
59
60
  label: 'Length',
60
61
  minimum: 0,
61
62
  maximum: 65535,
@@ -11,9 +11,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
11
11
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
12
12
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
13
13
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
14
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
15
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
16
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
14
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
15
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
16
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
17
17
  */
18
18
  export declare class PIM extends BaseHeader {
19
19
  #private;
@@ -4,6 +4,9 @@ exports.PIM = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToHex_1 = require("../helper/BufferToHex");
6
6
  const HexToBuffer_1 = require("../helper/HexToBuffer");
7
+ const BufferToNumber_1 = require("../helper/BufferToNumber");
8
+ const NumberToBuffer_1 = require("../helper/NumberToBuffer");
9
+ const Checksum_1 = require("../helper/Checksum");
7
10
  const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
8
11
  /**
9
12
  * PIM — Protocol Independent Multicast, Sparse Mode version 2 (RFC 7761), carried directly over IP as
@@ -15,9 +18,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
15
18
  * 5 Assert, 8 Candidate-RP-Advertisement — and several bodies embed Encoded-Address / Encoded-Group /
16
19
  * Encoded-Source structures whose parsing depends on address-family and PIM-neighbour context, so this
17
20
  * single-message codec keeps the body verbatim as `body` hex (byte-perfect) and does not sub-decode it.
18
- * The Checksum and Reserved byte are honored verbatim, never recomputed (encode is a faithful executor);
19
- * a well-formed PIM message round-trips byte-for-byte. The body is bounded by the enclosing IP payload
20
- * so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
+ * The Reserved byte is honored verbatim; the Checksum is computed when left out (RFC 7761 §4.9), else
22
+ * written as given. A well-formed PIM message round-trips byte-for-byte. The body is bounded by the
23
+ * enclosing IP payload so a trailing FCS / pipelined data is left to the codec's recursion / RawData.
21
24
  */
22
25
  class PIM extends BaseHeader_1.BaseHeader {
23
26
  constructor() {
@@ -57,6 +60,27 @@ class PIM extends BaseHeader_1.BaseHeader {
57
60
  }
58
61
  return available < 0 ? 0 : available;
59
62
  }
63
+ /**
64
+ * The PIM checksum (RFC 7761 §4.9): the Internet checksum of the whole PIM message, except that a
65
+ * Register (type 1) covers only its 8-byte header, not the encapsulated data packet. Over IPv6 the
66
+ * IPv6 pseudo-header (next header 103, upper-layer length = the bytes covered) is prepended; over
67
+ * IPv4 there is none. Called once every field is written, with the checksum field still zero.
68
+ * @private
69
+ */
70
+ #calculateChecksum() {
71
+ const message = this.packet.subarray(this.startPos, this.endPos);
72
+ const type = this.instance.type.getValue(0);
73
+ const covered = type === 1 ? message.subarray(0, 8) : message;
74
+ const ip = this.nearestIPModule();
75
+ if (!ip || ip.id !== 'ipv6')
76
+ return (0, Checksum_1.InternetChecksum)(covered);
77
+ const pseudoHeader = this.ipPseudoHeader(103, covered.length);
78
+ if (!pseudoHeader) {
79
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute PIM checksum: the IPv6 addresses beneath are unusable');
80
+ return 0;
81
+ }
82
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, covered);
83
+ }
60
84
  static #buildSchema() {
61
85
  return {
62
86
  type: 'object',
@@ -68,6 +92,7 @@ class PIM extends BaseHeader_1.BaseHeader {
68
92
  label: 'Version',
69
93
  minimum: 0,
70
94
  maximum: 15,
95
+ default: 2,
71
96
  decode: function () {
72
97
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
73
98
  },
@@ -94,9 +119,33 @@ class PIM extends BaseHeader_1.BaseHeader {
94
119
  //Byte 1: Reserved (0 on transmit for most types, a flags/subtype byte for a few). Kept
95
120
  //verbatim and re-emitted so a non-canonical frame is still byte-perfect.
96
121
  reserved: this.fieldHex('reserved', 1, 1, 'Reserved'),
97
- //Bytes 2-3: Checksum (ones-complement over the PIM message). Honored verbatim, never
98
- //recomputed, so a captured message round-trips byte-for-byte.
99
- checksum: this.fieldUInt('checksum', 2, 2, 'Checksum'),
122
+ //Bytes 2-3: Checksum, computed once every field is written when left out (#calculateChecksum);
123
+ //a given value is written as given.
124
+ checksum: {
125
+ type: 'integer',
126
+ autoCalculated: true,
127
+ label: 'Checksum',
128
+ minimum: 0,
129
+ maximum: 65535,
130
+ decode: function () {
131
+ this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
132
+ },
133
+ encode: function () {
134
+ if (!this.isLeftOut(this.instance.checksum)) {
135
+ const checksum = this.instance.checksum.getValue(0);
136
+ this.instance.checksum.setValue(checksum);
137
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
138
+ return;
139
+ }
140
+ this.instance.checksum.setValue(0);
141
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
142
+ this.addPostSelfEncodeHandler(() => {
143
+ const checksum = this.#calculateChecksum();
144
+ this.instance.checksum.setValue(checksum);
145
+ this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
146
+ });
147
+ }
148
+ },
100
149
  //The type-specific body after the 4-byte header, kept verbatim. Bounded by the enclosing
101
150
  //IP payload (#available) so trailing FCS / pipelined data is left to the codec's recursion.
102
151
  body: {
@@ -121,11 +121,11 @@ class POP3 extends BaseHeader_1.BaseHeader {
121
121
  //Display-only metadata parsed from the first line on decode (no encode — populated by the
122
122
  //message field above, never read back). isReply distinguishes a server reply from a
123
123
  //client command; status/replyText describe a reply, command/argument a command.
124
- isReply: { type: 'boolean', label: 'Is Reply' },
125
- status: { type: 'string', label: 'Status' },
126
- replyText: { type: 'string', label: 'Reply Text' },
127
- command: { type: 'string', label: 'Command' },
128
- argument: { type: 'string', label: 'Argument' }
124
+ isReply: { type: 'boolean', label: 'Is Reply', readOnly: true },
125
+ status: { type: 'string', label: 'Status', readOnly: true },
126
+ replyText: { type: 'string', label: 'Reply Text', readOnly: true },
127
+ command: { type: 'string', label: 'Command', readOnly: true },
128
+ argument: { type: 'string', label: 'Argument', readOnly: true }
129
129
  }
130
130
  };
131
131
  }
@@ -46,6 +46,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
46
46
  //Byte 0 high nibble: PPPoE Version (1). Written MSB-first; shares byte 0 with Type
47
47
  //(writeBits masks each field so they never clobber).
48
48
  version: {
49
+ default: 1,
49
50
  type: 'integer', label: 'Version', minimum: 0, maximum: 15,
50
51
  decode: function () {
51
52
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
@@ -58,6 +59,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
58
59
  },
59
60
  //Byte 0 low nibble: PPPoE Type (1).
60
61
  type: {
62
+ default: 1,
61
63
  type: 'integer', label: 'Type', minimum: 0, maximum: 15,
62
64
  decode: function () {
63
65
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
@@ -74,6 +76,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
74
76
  sessionId: this.fieldUInt('sessionId', 2, 2, 'Session ID'),
75
77
  length: {
76
78
  type: 'integer',
79
+ autoCalculated: true,
77
80
  label: 'Payload Length',
78
81
  minimum: 0,
79
82
  maximum: 65535,
@@ -113,7 +116,7 @@ class PPPoEDiscovery extends BaseHeader_1.BaseHeader {
113
116
  label: 'Tag',
114
117
  properties: {
115
118
  type: { type: 'integer', label: 'Type', minimum: 0, maximum: 65535 },
116
- length: { type: 'integer', label: 'Length', minimum: 0, maximum: 65535, hidden: true },
119
+ length: { type: 'integer', label: 'Length', autoCalculated: true, minimum: 0, maximum: 65535, hidden: true },
117
120
  value: { type: 'string', label: 'Value', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
118
121
  }
119
122
  },
@@ -60,6 +60,7 @@ class PPPoESession extends BaseHeader_1.BaseHeader {
60
60
  sessionId: this.fieldUInt('sessionId', 2, 2, 'Session ID'),
61
61
  length: {
62
62
  type: 'integer',
63
+ autoCalculated: true,
63
64
  label: 'Length',
64
65
  minimum: 0,
65
66
  maximum: 65535,
@@ -47,6 +47,7 @@ class PPTP extends BaseHeader_1.BaseHeader {
47
47
  properties: {
48
48
  length: {
49
49
  type: 'integer',
50
+ autoCalculated: true,
50
51
  label: 'Length',
51
52
  minimum: 0,
52
53
  maximum: 65535,
@@ -104,8 +104,11 @@ class PTP extends BaseHeader_1.BaseHeader {
104
104
  versionPTP: this.#nibble('versionPTP', 1, 4, 'Version PTP'),
105
105
  messageLength: {
106
106
  type: 'integer',
107
+ autoCalculated: true,
107
108
  label: 'Message Length',
108
- minimum: 34,
109
+ //The field floor, not the 34-byte header: a given value is written as given
110
+ //(flagged when too short) so a malformed message can be crafted.
111
+ minimum: 0,
109
112
  maximum: 65535,
110
113
  decode: function () {
111
114
  this.instance.messageLength.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
@@ -121,6 +124,8 @@ class PTP extends BaseHeader_1.BaseHeader {
121
124
  this.recordError(this.instance.messageLength.getPath(), 'Maximum value is 65535');
122
125
  value = 65535;
123
126
  }
127
+ if (value < 34)
128
+ this.recordError(this.instance.messageLength.getPath(), 'Message length should be at least 34');
124
129
  this.instance.messageLength.setValue(value);
125
130
  this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(value));
126
131
  }
@@ -112,6 +112,7 @@ class PostgreSQL extends BaseHeader_1.BaseHeader {
112
112
  //messageType emptiness — see #isStartupEncode), so it never affects the re-emitted bytes.
113
113
  isStartup: {
114
114
  type: 'boolean',
115
+ readOnly: true,
115
116
  label: 'Is Startup',
116
117
  decode: function () {
117
118
  this.instance.isStartup.setValue(this.#isStartupDecode());
@@ -143,6 +144,7 @@ class PostgreSQL extends BaseHeader_1.BaseHeader {
143
144
  //supplied (a crafted message may lie); else derived as 4 (the length word) + body bytes.
144
145
  length: {
145
146
  type: 'integer',
147
+ autoCalculated: true,
146
148
  label: 'Length',
147
149
  minimum: 0,
148
150
  maximum: 4294967295,
@@ -132,6 +132,7 @@ class QUIC extends BaseHeader_1.BaseHeader {
132
132
  //when supplied, else derived from the DCID bytes.
133
133
  dcidLength: {
134
134
  type: 'integer',
135
+ autoCalculated: true,
135
136
  label: 'DCID Length',
136
137
  minimum: 0,
137
138
  maximum: 255,
@@ -169,6 +170,7 @@ class QUIC extends BaseHeader_1.BaseHeader {
169
170
  //Source Connection ID length. Offset is dynamic (after the DCID). Honored else derived.
170
171
  scidLength: {
171
172
  type: 'integer',
173
+ autoCalculated: true,
172
174
  label: 'SCID Length',
173
175
  minimum: 0,
174
176
  maximum: 255,
@@ -54,6 +54,7 @@ class RADIUS extends BaseHeader_1.BaseHeader {
54
54
  identifier: this.fieldUInt('identifier', 1, 1, 'Identifier'),
55
55
  length: {
56
56
  type: 'integer',
57
+ autoCalculated: true,
57
58
  label: 'Length',
58
59
  minimum: 0,
59
60
  maximum: 65535,
@@ -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
  /**
@@ -21,6 +22,7 @@ export declare class RDP extends BaseHeader {
21
22
  readonly name: string;
22
23
  readonly nickname: string;
23
24
  readonly matchKeys: string[];
25
+ readonly heuristicParents: HeuristicParent[];
24
26
  readonly heuristicFallback: boolean;
25
27
  match(): boolean;
26
28
  readonly demuxProducers: DemuxProducer[];
@@ -23,6 +23,8 @@ class RDP extends BaseHeader_1.BaseHeader {
23
23
  this.nickname = 'RDP';
24
24
  //A content-heuristic child of COTP (unkeyed), selected by the X.224 Negotiation signature.
25
25
  this.matchKeys = [];
26
+ //Editor graph: matched by content, so list it under these parents in allowedNextLayers().
27
+ this.heuristicParents = [{ parent: 'cotp', discriminator: { field: 'pduType', value: 0xe0 } }];
26
28
  this.heuristicFallback = true;
27
29
  //A leaf for this slice — the MCS/T.125 data phase is a separate sub-stack.
28
30
  this.demuxProducers = [];
@@ -37,10 +39,13 @@ class RDP extends BaseHeader_1.BaseHeader {
37
39
  summary: 'RDP-Neg type=${negotiationType} protocols=${requestedProtocols}',
38
40
  properties: {
39
41
  //0x01 Negotiation Request, 0x02 Response, 0x03 Failure.
40
- negotiationType: this.fieldUInt('negotiationType', 0, 1, 'Negotiation Type'),
42
+ //Default 1 (RDP_NEG_REQ): a type match() recognizes (1/2/3), so a fresh layer re-decodes as RDP.
43
+ negotiationType: { ...this.fieldUInt('negotiationType', 0, 1, 'Negotiation Type'), default: 1 },
41
44
  flags: this.fieldUInt('flags', 1, 1, 'Flags'),
42
45
  //2-byte LITTLE-ENDIAN length — always 8 for the Negotiation. Honored verbatim.
43
46
  length: {
47
+ //Always 8 for the Negotiation structure; left out, encode writes 8.
48
+ autoCalculated: true,
44
49
  type: 'integer', label: 'Length', minimum: 0, maximum: 65535,
45
50
  decode: function () {
46
51
  const bytes = this.readBytes(2, 2);
@@ -124,10 +124,10 @@ class RFB extends BaseHeader_1.BaseHeader {
124
124
  //populated by the message field above, never read back). isVersionHandshake distinguishes
125
125
  //the opening 12-byte version line from any other (binary) RFB payload; versionString/major/
126
126
  //minor describe the version when it is a handshake.
127
- isVersionHandshake: { type: 'boolean', label: 'Is Version Handshake' },
128
- versionString: { type: 'string', label: 'Version String' },
129
- major: { type: 'integer', label: 'Major Version', minimum: 0, maximum: 999 },
130
- minor: { type: 'integer', label: 'Minor Version', minimum: 0, maximum: 999 }
127
+ isVersionHandshake: { type: 'boolean', label: 'Is Version Handshake', readOnly: true },
128
+ versionString: { type: 'string', label: 'Version String', readOnly: true },
129
+ major: { type: 'integer', label: 'Major Version', readOnly: true, minimum: 0, maximum: 999 },
130
+ minor: { type: 'integer', label: 'Minor Version', readOnly: true, minimum: 0, maximum: 999 }
131
131
  }
132
132
  };
133
133
  }