@netkitty/codec 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +188 -0
  2. package/README.md +21 -1
  3. package/README.zh-CN.md +19 -1
  4. package/dist/Codec.d.ts +4 -3
  5. package/dist/Codec.js +44 -5
  6. package/dist/PacketHeaders.d.ts +2 -0
  7. package/dist/PacketHeaders.js +8 -4
  8. package/dist/abstracts/BaseHeader.d.ts +17 -0
  9. package/dist/abstracts/BaseHeader.js +22 -0
  10. package/dist/headers/ARP.d.ts +3 -1
  11. package/dist/headers/ARP.js +8 -1
  12. package/dist/headers/BACnetIP.js +2 -1
  13. package/dist/headers/BGP.js +3 -2
  14. package/dist/headers/Babel.js +1 -1
  15. package/dist/headers/Codesys.js +3 -2
  16. package/dist/headers/DHCP.js +2 -1
  17. package/dist/headers/DNP3.d.ts +12 -7
  18. package/dist/headers/DNP3.js +98 -18
  19. package/dist/headers/DTLS.js +5 -3
  20. package/dist/headers/Diameter.js +2 -1
  21. package/dist/headers/Elasticsearch.js +3 -2
  22. package/dist/headers/EthernetII.d.ts +1 -0
  23. package/dist/headers/EthernetII.js +3 -0
  24. package/dist/headers/Goose.d.ts +31 -7
  25. package/dist/headers/Goose.js +409 -168
  26. package/dist/headers/HARTIP.js +2 -2
  27. package/dist/headers/IEC104_I_Frame.d.ts +28 -0
  28. package/dist/headers/IEC104_I_Frame.js +246 -160
  29. package/dist/headers/IEC104_S_Frame.js +3 -3
  30. package/dist/headers/IEC104_U_Frame.js +5 -3
  31. package/dist/headers/IEC61850SampledValues.d.ts +1 -0
  32. package/dist/headers/IEC61850SampledValues.js +16 -1
  33. package/dist/headers/IPFIX.js +2 -1
  34. package/dist/headers/IPv4.d.ts +1 -0
  35. package/dist/headers/IPv4.js +6 -0
  36. package/dist/headers/IPv6.d.ts +1 -0
  37. package/dist/headers/IPv6.js +6 -0
  38. package/dist/headers/IPv6HopByHopOptions.js +8 -0
  39. package/dist/headers/KNXnetIP.js +3 -2
  40. package/dist/headers/LACP.js +2 -1
  41. package/dist/headers/LDP.js +2 -2
  42. package/dist/headers/MQTT.js +3 -1
  43. package/dist/headers/MarkerProtocol.js +2 -1
  44. package/dist/headers/NetFlowV5.js +2 -1
  45. package/dist/headers/OPCUA.js +4 -0
  46. package/dist/headers/OSPF.d.ts +2 -2
  47. package/dist/headers/OSPF.js +26 -5
  48. package/dist/headers/OSPFv3.d.ts +37 -0
  49. package/dist/headers/OSPFv3.js +314 -0
  50. package/dist/headers/PPTP.js +2 -2
  51. package/dist/headers/QUIC.js +8 -3
  52. package/dist/headers/RGoose.js +2 -1
  53. package/dist/headers/RIP.js +3 -2
  54. package/dist/headers/RIPng.js +3 -2
  55. package/dist/headers/RTCP.js +3 -2
  56. package/dist/headers/RTP.js +3 -2
  57. package/dist/headers/SFlow.js +4 -2
  58. package/dist/headers/SLMP.js +2 -1
  59. package/dist/headers/SLP.js +2 -1
  60. package/dist/headers/SLPv1.js +2 -1
  61. package/dist/headers/SMB1.js +3 -2
  62. package/dist/headers/SMB2.js +3 -2
  63. package/dist/headers/STUN.js +2 -1
  64. package/dist/headers/TLS_Handshake.d.ts +6 -0
  65. package/dist/headers/TLS_Handshake.js +28 -3
  66. package/dist/headers/TacacsPlus.js +3 -1
  67. package/dist/headers/Trailer.d.ts +30 -0
  68. package/dist/headers/Trailer.js +63 -0
  69. package/dist/headers/VLAN_802dot1Q.d.ts +1 -0
  70. package/dist/headers/VLAN_802dot1Q.js +3 -0
  71. package/dist/headers/WakeOnLAN.js +2 -1
  72. package/dist/headers/WireGuard.js +3 -2
  73. package/dist/headers/Zabbix.js +2 -2
  74. package/dist/lib/BERElement.d.ts +51 -0
  75. package/dist/lib/BERElement.js +125 -0
  76. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -3,6 +3,194 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.2.0](https://github.com/myq1991/netkitty/compare/@netkitty/codec@2.1.0...@netkitty/codec@2.2.0) (2026-11-27)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **codec:** compute DNP3 data-block CRCs from a new userData field ([fcbf954](https://github.com/myq1991/netkitty/commit/fcbf9546c7f854eb1139b4f7bb227ac400d0721a))
12
+ * **codec:** count the IEC 104 information objects from IOA when left out ([d89d78c](https://github.com/myq1991/netkitty/commit/d89d78c5129fc84bc40444354e6b295bae205416))
13
+ * **codec:** default the IEC 104 U-frame and S-frame control field to its format ([c768977](https://github.com/myq1991/netkitty/commit/c768977c0fb972916f728ae64aa0dcd2b32145d1))
14
+ * **codec:** default the signature fields match() checks ([cd44c7b](https://github.com/myq1991/netkitty/commit/cd44c7b6a75f0db58d4c7bbd4f9b46ee378d8a0b))
15
+ * **codec:** keep a GOOSE or SV PDU with malformed BER as raw ([9ae2cda](https://github.com/myq1991/netkitty/commit/9ae2cda5a44c59327f571addbe3b02275710e511))
16
+ * **codec:** keep a tls-handshake Hello cut short by the capture as raw ([2bc0671](https://github.com/myq1991/netkitty/commit/2bc0671f54fb20bb9f6d5b4272f2480ac3f9d363))
17
+ * **codec:** keep a wrong-size IPv6 Hop-by-Hop option as a Custom item ([1c57d2a](https://github.com/myq1991/netkitty/commit/1c57d2a65b7697532bb80cacc9a872fa8a48e603))
18
+ * **codec:** keep bytes past a GOOSE or SV Length as a trailer layer ([3aa7d81](https://github.com/myq1991/netkitty/commit/3aa7d814d9153e1399abf4a05a8463e0a68639bc))
19
+ * **codec:** keep bytes past an IP or 802.3 length as a trailer layer ([b6bc47d](https://github.com/myq1991/netkitty/commit/b6bc47d2e013df5eaab2867c9a8f4ac4292620bb))
20
+ * **codec:** keep the Ethernet padding after an ARP packet as a trailer ([fe4eca6](https://github.com/myq1991/netkitty/commit/fe4eca62305d7b54b211f25acb7e84ddf1f06468))
21
+ * **codec:** lay out IEC 104 elements as IEC 60870-5-101 defines them ([8eb7a5d](https://github.com/myq1991/netkitty/commit/8eb7a5dec85eeed824dd969eaec2cf4425c74534))
22
+ * **codec:** mark every node of the GOOSE encoding hints hidden ([935644e](https://github.com/myq1991/netkitty/commit/935644e7f07892c28dac9d4763822c84728886a3))
23
+ * **codec:** re-encode GOOSE with the BER lengths and INTEGER widths it was captured with ([2f6ac64](https://github.com/myq1991/netkitty/commit/2f6ac643556f8a781eaaf02ea3ff4d777b32e740))
24
+ * **codec:** report IOA items of an unknown IEC 104 type id instead of failing ([ff8d7c9](https://github.com/myq1991/netkitty/commit/ff8d7c920346b6943b7ca0d554a5d95a3a7c2228))
25
+ * **codec:** write a tls-handshake body that matches its handshake type ([0415333](https://github.com/myq1991/netkitty/commit/0415333215a85c3425838e5e4634c203b7db717b))
26
+
27
+
28
+ ### Features
29
+
30
+ * **codec:** add OSPFv3 (RFC 5340) as its own header ([fc9a756](https://github.com/myq1991/netkitty/commit/fc9a756dcb594b3cbbc4881b028f76a6ec2f7bef))
31
+
32
+
33
+ ### Upgrade note
34
+
35
+ Decoded trees change shape: bytes past a layer's own length (Ethernet padding) come back as a `trailer`
36
+ layer instead of inside the innermost layer or a trailing `raw`; DNP3 frames whose block CRCs check decode
37
+ as `userData` instead of `payload`; OSPF version 3 decodes as the new `ospfv3`; GOOSE trees carry hidden
38
+ `encoding` hints and malformed GOOSE / SV PDUs decode as `raw`; IEC 104 types 40, 107 and 120–127 read
39
+ their elements at the offsets IEC 60870-5-101 defines.
40
+
41
+ Encode output changes where fields are left out: signature fields that match() checks now have schema
42
+ defaults (a saved template without `rtp.version` now sends 2; a U-frame from `{}` is STARTDT act). Some
43
+ input no longer validates: IEC 104 `NOS` above 255 and the old type-127 `{NOF, SCQ}` shape. Subclasses of
44
+ `Goose` that override its protected BER helpers break. Read "Behaviour changes" below before upgrading.
45
+
46
+ ### Behaviour changes (check before upgrading)
47
+
48
+ #### Ethernet padding is a `trailer` layer
49
+ - IPv4 bounds its payload by Total Length, IPv6 by Payload Length, Ethernet / 802.1Q by the 802.3
50
+ length, GOOSE and Sampled Values by their Length, and an Ethernet/IPv4 ARP packet ends after its 28
51
+ octets. Bytes past that end become a new
52
+ `trailer` layer (`{data: '<hex>'}`, `protocol: false`) at the
53
+ end of the layer list. Before, they were part of the innermost layer (an ICMP echo padded to 60 octets
54
+ showed the padding in `icmp.message`, a UDP payload gained it) or a trailing `raw` layer (after
55
+ LLC / STP, ARP, GOOSE and SV). Across the local captures the trailer bytes equal tshark's
56
+ `eth.padding` + `eth.trailer` on every frame where both see one (15,366 frames).
57
+ - Padding after a TCP segment with no payload no longer decodes as an application layer: across the
58
+ local captures, padding used to decode as CMS, NBSS, COTP or a TLS record on their ports.
59
+ - A length of 0 (segmentation offload), one longer than the bytes captured, or one shorter than its own
60
+ header sets no bound: those frames decode as before. Nested IP layers each bound their own payload, so
61
+ IP in GRE can end with two trailers.
62
+ - Encode writes `trailer` bytes after every other layer and after every computed length and checksum, so
63
+ a left-out IPv4 Total Length, IPv6 Payload Length, 802.3 length or checksum no longer counts the
64
+ padding (before: Total Length 32 → 46 for a padded echo, 802.3 length 38 → 45). A layer given after a
65
+ `trailer` is written before it, with a warning.
66
+ - `allowedNextLayers()` lists `trailer` after every layer (after `raw`); `checkConsistency()` never flags
67
+ it; `summary()` skips it like `raw`.
68
+ - Custom codecs: `BaseHeader.payloadEnd()` (decode only) lets a header with its own length field set the
69
+ same bound.
70
+
71
+ #### DNP3 data blocks
72
+ - New field `userData`: the transport header + application data with the per-block CRCs removed. Left out
73
+ of nothing else, encode splits it into 16-octet blocks and appends each block's CRC-16/DNP.
74
+ - Decode emits exactly one of the two: `userData` when every block CRC checks (`userData: ''` for a frame
75
+ with no user data), otherwise the verbatim `payload` plus a `warning` per bad or truncated block.
76
+ Readers of `dnp3.payload` must read `userData` for well-formed frames.
77
+ - Encode: a non-empty `payload` is written verbatim and wins (to craft a bad CRC); `userData` given as
78
+ well is ignored with a warning. `payload: ''` counts as left out. A left-out `length` is 5 + the
79
+ user-data octets of whichever field is written.
80
+
81
+ #### OSPFv3 is its own header
82
+ - New header `ospfv3` (RFC 5340): 16-octet header with `instanceId`, Hello decoded field by field
83
+ (`interfaceId`, `routerPriority`, `options`, intervals, DR/BDR, `neighbors`), other types kept as
84
+ `rawBody`. `packetLength` and `checksum` (IPv6 pseudo-header, over Packet Length) are computed when left
85
+ out. It runs over IPv6 only; OSPFv3 over IPv4 (RFC 7949) is not supported and decodes as raw.
86
+ - `ospf` now matches version 2 only and has `version` default 2. Protocol 89 with version 3 over IPv6
87
+ decodes as `ospfv3` (it used to decode as `ospf` with the v2 layout); other versions decode as raw.
88
+ `ospf` given `version: 3` on encode still writes the v2 layout, with a warning pointing to `ospfv3`.
89
+
90
+ #### IEC 104
91
+ - `numberOfObject` is computed when left out: the number of IOA items (for SQ=1, the elements). The items
92
+ written always come from `IOA`: an explicit count that differs is written as given, every item is still
93
+ written, and a warning is recorded. Before, a left-out or 0 count dropped every item silently, and a
94
+ larger count failed with an encode error. More than 127 items: the count is clamped to 127 with a
95
+ warning.
96
+ - A U-frame built from empty data is STARTDT act (`controlField` default `07000000`, `680407000000`)
97
+ instead of `680400000000`, which re-decoded as an I-frame. An S-frame from empty data is
98
+ `680401000000` (6 octets) instead of `680101`.
99
+ - `apciType` (I / S / U) lost its numeric `minimum` / `maximum` (it is a string).
100
+
101
+ #### tls-handshake
102
+ - Only ClientHello and ServerHello build a body from fields. Every other type writes `messagedata.raw`, or
103
+ nothing when it is left out, so `{}` is an empty HelloRequest (`160303000400000000`) instead of a 6-byte
104
+ zero body. Hello fields given under another type are dropped with a warning.
105
+ - In a Hello, a left-out `random` is 32 zero octets and a left-out ServerHello `cipherSuite` /
106
+ `compressionMethod` is zeros of its size, so a Hello built from its type alone re-decodes as a Hello.
107
+ - A captured ServerHelloDone no longer gains 6 octets on re-encode.
108
+
109
+ #### GOOSE and Sampled Values
110
+ - GOOSE re-encodes with the BER length forms and INTEGER widths it was captured with. Before, a device
111
+ that writes long-form lengths or fixed 4-octet INTEGERs (timeAllowedtoLive, stNum, sqNum, confRev,
112
+ numDatSetEntries, INT32 items) got a shorter frame back: 798 of 8693 GOOSE frames in one substation
113
+ capture. Decode records only the forms that are not minimal, in a hidden `encoding` node on `goosePdu`
114
+ (`lengthOctets`, and `{lengthOctets?, width?}` per field) and on each `allData` item (`lengthOctets`,
115
+ `width` on INT items, `content` hex on FLOAT32). Encode uses a hint while the value still fits and
116
+ writes the minimal form otherwise; a tree without hints encodes exactly as before.
117
+ - FLOAT32 items decode the 4 octets after the exponent-width octet. Before, the goosePdu span was one
118
+ octet short after a long-form length, so the last item lost its final octet (0.0 decoded as
119
+ 3.85e-34, and the lost octet became a stray `raw` layer). FLOAT64 (width 11) decodes as its value with
120
+ a warning; other forms decode as 0 with an error; both, and NaN, re-emit their captured octets. -0.0
121
+ encodes as `80000000`.
122
+ - A goosePdu or savPdu whose BER cannot be walked strictly (an element that runs past its parent or
123
+ Length, octets left over, unknown, repeated or out-of-order goosePdu elements) decodes as `raw` with
124
+ the error `Malformed goosePdu, kept as raw: …` instead of partial fields that re-encoded to a different
125
+ PDU.
126
+ - GOOSE / SV `length` now bounds the PDU: a decoded tree edited to a different PDU size must leave
127
+ `length` out to have it recomputed (an explicit value is written as given).
128
+ - Subclasses of `Goose`: the protected `decodeDataTLVItem` takes `BERElement[]`, `encodeDataTLVItem`
129
+ returns `Buffer[]`, and `TLVInstance` / `TLVChild` are gone.
130
+
131
+ #### IEC 104 element layouts
132
+ - Type 32 (M_ST_TB_1) wrote the CP56Time2a minute IV flag into the high milliseconds octet, so a time
133
+ tag with milliseconds ≥ 32768 re-encoded wrong.
134
+ - Types 40, 107 and 120–126 read and write their elements as IEC 60870-5-101 defines them, so the same
135
+ bytes decode to different values (same field names): 40 M_EP_TF_1 has QDP in its own octet (11
136
+ octets, CP16Time2a at +5, CP56Time2a at +7); 107 C_TS_TA_1 has a 16-bit TSC (9 octets); file transfer
137
+ has NOF 2 octets, NOS 1 and LOF 3 (122 / 123 little-endian like the rest; 126's CP56Time2a at +9).
138
+ - Type 127 F_SC_NB_1 is QueryLog: `{address, NOF, RangeStartTime, RangeStopTime}` (two CP56Time2a). The
139
+ old `{NOF, SCQ}` input no longer validates.
140
+ - Schema: `NOS` maximum 65535 → 255 (larger input is now rejected), `LOF` maximum → 16777215, `TSC` →
141
+ 65535; type 126's `LOS` is `SOF` (Status Of File), as decode already produced.
142
+ - An unknown type id with IOA items records `Type id N has no element layout: the IOA items are not
143
+ written` (the unparsed ASDU in `raw` is still written) instead of an encode error.
144
+
145
+ #### tls-handshake cut short by the capture
146
+ - A Hello whose body runs past the captured bytes (a ClientHello split across TCP segments) decodes as
147
+ `messagedata.raw` with the error `… the body continues past the captured bytes`, so it re-encodes
148
+ byte-exact. Before, a segment that ended exactly at an extension boundary decoded as a structured Hello
149
+ and re-encode rewrote the extensions length. Cut-short Hellos that already decoded raw keep their shape;
150
+ their error text changes.
151
+
152
+ #### IPv6 Hop-by-Hop
153
+ - A fixed-size option with the wrong size decodes as a Custom item instead of a typed item that failed
154
+ encode validation.
155
+
156
+ #### Signature defaults
157
+ - Fields a header's match() checks now have schema defaults, so the header built from `{}` re-decodes as
158
+ itself: STUN / DHCP / PPTP / Zabbix magic cookies, BGP marker, Wake-on-LAN sync stream, version fields
159
+ (NetFlow 5, sFlow 5, IPFIX 10, Diameter 1, HART-IP 1, LDP 1, SLP 2 / 1, RTP 2, KNXnet/IP 06/10,
160
+ TACACS+ 0xc, RIP 2, RIPng 1, DTLS fefd, QUIC v1 with `headerForm` and `fixedBit` true — also filled
161
+ when `firstByte` is given without them), message types (R-GOOSE 0xA1, WireGuard 1, MQTT
162
+ PINGREQ, RTCP RR, OPC UA HEL, SLMP 0x5000, LACP 1, Marker 2) and Babel / Elasticsearch / CODESYS /
163
+ SMB / BACnet signatures. Only input that leaves these fields out changes; decoded trees carry them.
164
+
165
+ ### Additions
166
+ - Headers `trailer` and `ospfv3`.
167
+ - Fixtures from real captures for GOOSE (long-form lengths, fixed-width INTEGERs, a minimal device), SV
168
+ and GOOSE with trailers, IEC 104 type 32, a cut-short TLS ClientHello and a TLS ServerHelloDone.
169
+ - `BaseHeader.payloadEnd()` for custom codecs.
170
+ - Test gate: every header built from `{}` under its usual parent re-decodes as itself.
171
+
172
+ ### Known limitations
173
+ - Trailer layers are always written at the end of the packet, so a trailer that decode placed inside a
174
+ tunnel (between an inner and an outer IP datagram) moves to the end when the lengths are left out.
175
+ - An 802.3 length below 3 (shorter than an LLC header) sets no bound.
176
+ - OSPFv3 over IPv4 (RFC 7949) is not supported.
177
+ - IEC 104: C_RD_NA_1 (102) decodes and encodes a READ octet the standard does not have, so a captured read
178
+ command gains an octet on re-encode; VTI decodes unsigned (tshark reads it as signed 7-bit). Types 40,
179
+ 107 and 120–127 are checked against the standard's element sizes only (no capture has them).
180
+ - GOOSE: a goosePdu carrying an element the decoder does not know, such as the optional `security [12]`
181
+ element, or a repeated or out-of-order one, now decodes as `raw` as a whole (it still re-encodes
182
+ byte-exact, but its fields cannot be edited). Unknown allData item tags (array, binary-time, bcd, mMSString, …) are dropped; a boolean TRUE
183
+ other than `01` re-encodes as `01`; a constructed goosePdu tag other than `61` re-encodes as `61`;
184
+ GOOSE strings and SV svID / datSet decode as ASCII, so octets ≥ 0x80 lose bit 7. SV gets no encoding
185
+ hints, so a non-minimal SV length form is not kept.
186
+ - Where a given length positions what follows (QUIC connection IDs, S7comm parameter/data, Diameter AVPs,
187
+ AMQP frame end, …), an explicit length that disagrees with the data reshapes the packet the way decode
188
+ would read it.
189
+
190
+
191
+
192
+
193
+
6
194
  # [2.1.0](https://github.com/myq1991/netkitty/compare/@netkitty/codec@2.0.0...@netkitty/codec@2.1.0) (2026-11-26)
7
195
 
8
196
 
package/README.md CHANGED
@@ -171,6 +171,24 @@ heuristic matches at the current offset, then advances and recurses until the pa
171
171
  `RawData` is the forced final fallback and always matches, so unknown or malformed trailing bytes simply
172
172
  become a `raw` layer and decode can never dead-end.
173
173
 
174
+ ### Padding and trailers
175
+
176
+ IPv4 (Total Length), IPv6 (Payload Length), an Ethernet / 802.1Q 802.3 length and the GOOSE / Sampled
177
+ Values Length bound the payload they carry, and an Ethernet/IPv4 ARP packet ends after its 28 octets. Bytes past that end — Ethernet padding up to the 60-octet minimum frame, or a trailer a device
178
+ appended — are not given to the layers above; they become a `trailer` layer at the end of the list
179
+ (`{data: '<hex>'}`, `protocol: false`). A length of 0, one longer than the bytes captured, or one shorter
180
+ than its own header sets no bound. On encode, `trailer` bytes are written after every other layer and
181
+ after every computed length and checksum, so a left-out length or checksum never counts them; append
182
+ `{id: 'trailer', data: {data: '0000…'}}` to pad a frame you build.
183
+
184
+ ### Captured encodings
185
+
186
+ Where a protocol allows more than one byte form for the same value, decode keeps what the capture used
187
+ so re-encode reproduces it. GOOSE records BER length forms and INTEGER widths that are not minimal in a
188
+ hidden `encoding` node (on `goosePdu` and on each `allData` item); encode uses them while the value still
189
+ fits and falls back to the minimal form otherwise, and a tree without them encodes minimal. A GOOSE or SV
190
+ PDU whose BER cannot be walked strictly is kept as `raw` (with an error) instead of being decoded partway.
191
+
174
192
  ### The declarative shell vs. the imperative core
175
193
 
176
194
  `PROTOCOL_SCHEMA` is the schema with its closures stripped (via a `JSON.parse(JSON.stringify())`
@@ -211,7 +229,7 @@ constants, so a layer added from scratch re-opens as that layer.
211
229
 
212
230
  ## Built-in headers
213
231
 
214
- 189 protocol headers are registered by default; `raw` is the forced catch-all. Pass custom classes to `new Codec(customCodecs)` to override a built-in with the same id or add new ones.
232
+ 191 headers are registered by default: 189 protocols, `raw` (the forced catch-all) and `trailer` (bytes past a layer's own length). Pass custom classes to `new Codec(customCodecs)` to override a built-in with the same id or add new ones.
215
233
 
216
234
  | id | name |
217
235
  | ---------------- | --------------------------------------------------- |
@@ -328,6 +346,7 @@ constants, so a layer added from scratch re-opens as that layer.
328
346
  | `openflow` | OpenFlow |
329
347
  | `openvpn` | OpenVPN Protocol |
330
348
  | `ospf` | Open Shortest Path First |
349
+ | `ospfv3` | Open Shortest Path First version 3 |
331
350
  | `pcworx` | PCWorx |
332
351
  | `pgsql` | PostgreSQL Protocol |
333
352
  | `pim` | Protocol Independent Multicast |
@@ -393,6 +412,7 @@ constants, so a layer added from scratch re-opens as that layer.
393
412
  | `tls-handshake` | Transport Layer Security(Handshake Protocol) |
394
413
  | `tls-heartbeat` | Transport Layer Security(Heartbeat Protocol) |
395
414
  | `tpkt` | TPKT |
415
+ | `trailer` | Trailer |
396
416
  | `udp` | User Datagram Protocol |
397
417
  | `vlan` | 802.1Q Virtual LAN |
398
418
  | `vrrp` | Virtual Router Redundancy Protocol |
package/README.zh-CN.md CHANGED
@@ -161,6 +161,22 @@ try {
161
161
  递归,直到把报文消费完。`RawData` 是被强制放在最后的兜底项,且永远匹配成功,所以无法识别或畸形的尾部
162
162
  字节会直接变成一层 `raw`,解码绝不会走进死胡同。
163
163
 
164
+ ### 填充与尾部数据
165
+
166
+ IPv4(Total Length)、IPv6(Payload Length)、Ethernet / 802.1Q 的 802.3 长度以及 GOOSE / SV 的 Length
167
+ 会限定各自所承载载荷的范围,Ethernet/IPv4 的 ARP 报文在 28 字节处结束。超出这个范围的字节(补齐到 60 字节最小帧的以太网填充,或设备附加的尾部数据)不会交给上层,而是在层
168
+ 列表末尾单独成为一层 `trailer`(`{data: '<hex>'}`,`protocol: false`)。长度为 0、比实际抓到的字节还长、
169
+ 或者比自身头部还短时,不做限定。编码时 `trailer` 的字节在其他所有层、以及所有自动计算的长度和校验和之后
170
+ 才写入,所以不填的长度和校验和不会把它算进去;自己构造报文时追加 `{id: 'trailer', data: {data: '0000…'}}`
171
+ 即可补齐填充。
172
+
173
+ ### 保留抓包里的编码形式
174
+
175
+ 同一个值在协议里有多种字节写法时,解码会记下抓包实际用的那种,重新编码时照样写出。GOOSE 会把非最简的
176
+ BER 长度形式和 INTEGER 宽度记在隐藏的 `encoding` 节点里(`goosePdu` 和每个 `allData` 项上都有);编码时
177
+ 只要值还放得下就沿用,放不下就退回最简形式;不带这些节点的树按最简形式编码。BER 无法严格解析的 GOOSE / SV
178
+ PDU 整体保留为 `raw`(并记一条错误),不再解一半。
179
+
164
180
  ### 声明式外壳与命令式内核
165
181
 
166
182
  `PROTOCOL_SCHEMA` 是剥离了闭包之后的 schema(通过 `JSON.parse(JSON.stringify())` 往返——JSON 序列化
@@ -195,7 +211,7 @@ ISO-Session 后的 MMS、RTPS、BitTorrent)也会列出——协议头用 `heuri
195
211
 
196
212
  ## 内置协议头
197
213
 
198
- 默认注册了 189 个协议头(`raw` 是强制兜底)。向 `new Codec(customCodecs)` 传入自定义类可覆盖同 id 的内置头或新增协议头。
214
+ 默认注册了 191 个头:189 个协议,加上 `raw`(强制兜底)和 `trailer`(超出该层自身长度的字节)。向 `new Codec(customCodecs)` 传入自定义类可覆盖同 id 的内置头或新增协议头。
199
215
 
200
216
  | id | name |
201
217
  | ---------------- | --------------------------------------------------- |
@@ -312,6 +328,7 @@ ISO-Session 后的 MMS、RTPS、BitTorrent)也会列出——协议头用 `heuri
312
328
  | `openflow` | OpenFlow |
313
329
  | `openvpn` | OpenVPN Protocol |
314
330
  | `ospf` | Open Shortest Path First |
331
+ | `ospfv3` | Open Shortest Path First version 3 |
315
332
  | `pcworx` | PCWorx |
316
333
  | `pgsql` | PostgreSQL Protocol |
317
334
  | `pim` | Protocol Independent Multicast |
@@ -377,6 +394,7 @@ ISO-Session 后的 MMS、RTPS、BitTorrent)也会列出——协议头用 `heuri
377
394
  | `tls-handshake` | Transport Layer Security(Handshake Protocol) |
378
395
  | `tls-heartbeat` | Transport Layer Security(Heartbeat Protocol) |
379
396
  | `tpkt` | TPKT |
397
+ | `trailer` | Trailer |
380
398
  | `udp` | User Datagram Protocol |
381
399
  | `vlan` | 802.1Q Virtual LAN |
382
400
  | `vrrp` | Virtual Router Redundancy Protocol |
package/dist/Codec.d.ts CHANGED
@@ -90,7 +90,8 @@ export declare class Codec {
90
90
  * may follow `parentLayerId`, each tagged with the {field,value} to set on the parent so decode
91
91
  * would route to it. Derived by reversing the dispatch table over the parent's producer fields,
92
92
  * plus the content-heuristic children that declare this parent in `heuristicParents` (listed with a
93
- * null discriminator). RawData is always appended as the custom/fallback layer that may follow anything.
93
+ * null discriminator). RawData (the custom/fallback layer) and Trailer (padding written after every
94
+ * other layer) are always appended: either may follow anything.
94
95
  * @param parentLayerId
95
96
  */
96
97
  allowedNextLayers(parentLayerId: string): NextLayer[];
@@ -111,7 +112,7 @@ export declare class Codec {
111
112
  * actually follows (e.g. eth.etherType says IPv6 but the next layer is IPv4). This is advisory
112
113
  * only — encode never blocks on it, since a deliberately-inconsistent packet is a valid crafted
113
114
  * packet. Only demux-based relationships are checked; content-heuristic children (TLS/IEC104) are
114
- * not flagged. RawData may follow anything and is never flagged.
115
+ * not flagged. RawData and Trailer may follow anything and are never flagged.
115
116
  * @param decoded
116
117
  */
117
118
  checkConsistency(decoded: CodecDecodeResult[]): ConsistencyIssue[];
@@ -127,7 +128,7 @@ export declare class Codec {
127
128
  * One-line human summary of a decoded packet (Wireshark's Info column). The innermost layer that
128
129
  * declares a `summary` template in its schema wins (the most specific layer describes the packet);
129
130
  * the template's ${dotted.field} placeholders are filled from that layer's decoded data. Falls back
130
- * to the innermost non-raw layer's name when no template is declared. Read-only projection.
131
+ * to the innermost layer that is neither raw nor a trailer when no template is declared. Read-only projection.
131
132
  * @param decoded
132
133
  */
133
134
  summary(decoded: CodecDecodeResult[]): string;
package/dist/Codec.js CHANGED
@@ -38,6 +38,8 @@ exports.Codec = void 0;
38
38
  const FlexibleObject_1 = require("./lib/FlexibleObject");
39
39
  const ProcessPacketPostHandlers_1 = require("./lib/ProcessPacketPostHandlers");
40
40
  const RawData_1 = require("./headers/RawData");
41
+ const Trailer_1 = require("./headers/Trailer");
42
+ const BaseHeader_1 = require("./abstracts/BaseHeader");
41
43
  const CodecSchemaValidateError_1 = require("./errors/CodecSchemaValidateError");
42
44
  const packetHeaders = __importStar(require("./PacketHeaders"));
43
45
  /**
@@ -95,6 +97,11 @@ class Codec {
95
97
  * used explicitly as the final fallback so decode never fails.
96
98
  */
97
99
  #rawDataCodec = RawData_1.RawData;
100
+ /**
101
+ * The Trailer layer: bytes past the end a layer's own length field gives (Ethernet padding). Not
102
+ * part of the dispatch table or heuristic list; decode places it (see #decode), never selects it.
103
+ */
104
+ #trailerCodec = Trailer_1.Trailer;
98
105
  get HEADER_CODECS() {
99
106
  return this.#codecModuleConstructors;
100
107
  }
@@ -168,6 +175,10 @@ class Codec {
168
175
  this.#rawDataCodec = codecModuleConstructor;
169
176
  continue;
170
177
  }
178
+ if (protocolId === Trailer_1.Trailer.PROTOCOL_ID) {
179
+ this.#trailerCodec = codecModuleConstructor;
180
+ continue;
181
+ }
171
182
  for (const heuristicParent of codecModuleConstructor.HEURISTIC_PARENTS || []) {
172
183
  const parentId = typeof heuristicParent === 'string' ? heuristicParent : heuristicParent.parent;
173
184
  const entry = {
@@ -440,6 +451,25 @@ class Codec {
440
451
  codecModules.push(codecModule);
441
452
  if (codecData.startPos >= codecData.packet.length)
442
453
  return;
454
+ const payloadEnd = codecModule.payloadEnd();
455
+ if (payloadEnd !== undefined && payloadEnd >= codecData.startPos && payloadEnd < codecData.packet.length) {
456
+ //The layer's own length ends its payload before the bytes do (Ethernet padding, a trailer).
457
+ //Decode the layers above on a view cut at that end, so neither they nor their post-decode
458
+ //handlers see the extra bytes, then keep those bytes as a trailer layer. Nested bounds (IP in
459
+ //IP) cut the view again, each level adding its own trailer.
460
+ if (payloadEnd > codecData.startPos) {
461
+ const payloadData = { packet: codecData.packet.subarray(0, payloadEnd), startPos: codecData.startPos, postHandlers: codecData.postHandlers };
462
+ await this.#decode(payloadData, codecModules, collectRanges);
463
+ }
464
+ codecData.startPos = payloadEnd;
465
+ const trailer = this.#trailerCodec.CREATE_INSTANCE(codecData, codecModules);
466
+ if (collectRanges)
467
+ trailer.enableByteRangeTracking();
468
+ await trailer.decode();
469
+ codecData.startPos = trailer.endPos;
470
+ codecModules.push(trailer);
471
+ return;
472
+ }
443
473
  return this.#decode(codecData, codecModules, collectRanges, rootLinktype);
444
474
  }
445
475
  /**
@@ -484,6 +514,13 @@ class Codec {
484
514
  await postEncodeHandler.handler();
485
515
  postEncodeHandler = postEncodeHandlers.shift();
486
516
  }
517
+ //Trailer layers are written only now, once every packet-level post handler has run, so no
518
+ //computed length or checksum counted their bytes (see Trailer).
519
+ reports.forEach((report, index) => {
520
+ if (!(report instanceof Trailer_1.Trailer))
521
+ return;
522
+ report.appendToPacket(reports.slice(index + 1).some((later) => later instanceof BaseHeader_1.BaseHeader && !(later instanceof Trailer_1.Trailer)));
523
+ });
487
524
  //Collect each layer's errors only now, so those recorded by packet-level post handlers (which
488
525
  //run after every layer is encoded) are reported too; layer order is preserved.
489
526
  const errors = [];
@@ -513,7 +550,8 @@ class Codec {
513
550
  * may follow `parentLayerId`, each tagged with the {field,value} to set on the parent so decode
514
551
  * would route to it. Derived by reversing the dispatch table over the parent's producer fields,
515
552
  * plus the content-heuristic children that declare this parent in `heuristicParents` (listed with a
516
- * null discriminator). RawData is always appended as the custom/fallback layer that may follow anything.
553
+ * null discriminator). RawData (the custom/fallback layer) and Trailer (padding written after every
554
+ * other layer) are always appended: either may follow anything.
517
555
  * @param parentLayerId
518
556
  */
519
557
  allowedNextLayers(parentLayerId) {
@@ -557,6 +595,7 @@ class Codec {
557
595
  result.push({ id: codecModuleConstructor.PROTOCOL_ID, name: codecModuleConstructor.PROTOCOL_NAME, discriminator: discriminator });
558
596
  }
559
597
  result.push({ id: this.#rawDataCodec.PROTOCOL_ID, name: this.#rawDataCodec.PROTOCOL_NAME, discriminator: null });
598
+ result.push({ id: this.#trailerCodec.PROTOCOL_ID, name: this.#trailerCodec.PROTOCOL_NAME, discriminator: null });
560
599
  return result;
561
600
  }
562
601
  /**
@@ -579,7 +618,7 @@ class Codec {
579
618
  * actually follows (e.g. eth.etherType says IPv6 but the next layer is IPv4). This is advisory
580
619
  * only — encode never blocks on it, since a deliberately-inconsistent packet is a valid crafted
581
620
  * packet. Only demux-based relationships are checked; content-heuristic children (TLS/IEC104) are
582
- * not flagged. RawData may follow anything and is never flagged.
621
+ * not flagged. RawData and Trailer may follow anything and are never flagged.
583
622
  * @param decoded
584
623
  */
585
624
  checkConsistency(decoded) {
@@ -587,7 +626,7 @@ class Codec {
587
626
  for (let i = 0; i < decoded.length - 1; i++) {
588
627
  const parent = decoded[i];
589
628
  const child = decoded[i + 1];
590
- if (child.id === this.#rawDataCodec.PROTOCOL_ID)
629
+ if (child.id === this.#rawDataCodec.PROTOCOL_ID || child.id === this.#trailerCodec.PROTOCOL_ID)
591
630
  continue;
592
631
  //A content-heuristic child (no matchKeys, or heuristicFallback dual) is legitimately
593
632
  //reachable off its well-known demux value (e.g. TLS on a non-443 port). Never flag it as
@@ -705,7 +744,7 @@ class Codec {
705
744
  * One-line human summary of a decoded packet (Wireshark's Info column). The innermost layer that
706
745
  * declares a `summary` template in its schema wins (the most specific layer describes the packet);
707
746
  * the template's ${dotted.field} placeholders are filled from that layer's decoded data. Falls back
708
- * to the innermost non-raw layer's name when no template is declared. Read-only projection.
747
+ * to the innermost layer that is neither raw nor a trailer when no template is declared. Read-only projection.
709
748
  * @param decoded
710
749
  */
711
750
  summary(decoded) {
@@ -715,7 +754,7 @@ class Codec {
715
754
  return this.#renderSummary(schema.summary, decoded[i].data);
716
755
  }
717
756
  for (let i = decoded.length - 1; i >= 0; i--) {
718
- if (decoded[i].id !== this.#rawDataCodec.PROTOCOL_ID)
757
+ if (decoded[i].id !== this.#rawDataCodec.PROTOCOL_ID && decoded[i].id !== this.#trailerCodec.PROTOCOL_ID)
719
758
  return decoded[i].name;
720
759
  }
721
760
  return decoded.length ? decoded[decoded.length - 1].name : '';
@@ -8,6 +8,7 @@ export { IPv6 } from './headers/IPv6';
8
8
  export { IPv6HopByHopOptions } from './headers/IPv6HopByHopOptions';
9
9
  export { ICMPv6 } from './headers/ICMPv6';
10
10
  export { RawData } from './headers/RawData';
11
+ export { Trailer } from './headers/Trailer';
11
12
  export { TCP } from './headers/TCP';
12
13
  export { UDP } from './headers/UDP';
13
14
  export { NTP } from './headers/NTP';
@@ -48,6 +49,7 @@ export { GRE } from './headers/GRE';
48
49
  export { BFD } from './headers/BFD';
49
50
  export { VRRP } from './headers/VRRP';
50
51
  export { OSPF } from './headers/OSPF';
52
+ export { OSPFv3 } from './headers/OSPFv3';
51
53
  export { ModbusTCP } from './headers/ModbusTCP';
52
54
  export { DNP3 } from './headers/DNP3';
53
55
  export { C37118 } from './headers/C37118';
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OSPF = exports.VRRP = exports.BFD = exports.GRE = exports.GENEVE = exports.SOCKS5 = exports.Memcached = exports.Telnet = exports.Kerberos = exports.LDAP = exports.SSH = exports.RFB = exports.Kafka = exports.AMQP = exports.SFlow = exports.WireGuard = exports.ISAKMP = exports.Diameter = exports.PostgreSQL = exports.MySQL = exports.NetFlowV5 = exports.RGoose = exports.L2TP = exports.RMCP = exports.GTPU = exports.VXLAN = exports.RADIUS = exports.Syslog = exports.NBNS = exports.LLMNR = exports.TFTP = exports.DHCPv6 = exports.MDNS = exports.SNMP = exports.DNS = exports.DHCP = exports.STUN = exports.NTP = exports.UDP = exports.TCP = exports.RawData = exports.ICMPv6 = exports.IPv6HopByHopOptions = exports.IPv6 = exports.IPv4 = exports.ICMP = exports.IEC61850SampledValues = exports.Goose = exports.EthernetII = exports.ARP = void 0;
4
- exports.SunRPC = exports.SSDP = exports.RSVP = exports.RIPng = exports.PTP = exports.PPPoESession = exports.PPPoEDiscovery = exports.PIM = exports.MPLS = exports.LACP = exports.Ident = exports.IGMP = exports.HSRP = exports.GTPv2C = exports.EIGRP = exports.DCCP = exports.WHOIS = exports.SOCKS4 = exports.Rlogin = exports.RIP = exports.Finger = exports.ESP = exports.EAPOL = exports.BGP = exports.AH = exports.HSR = exports.IRC = exports.NNTP = exports.IMAP = exports.POP3 = exports.SMTP = exports.COTP = exports.TPKT = exports.RTSP = exports.FTP = exports.HTTP = exports.EtherCAT = exports.PROFINETRT = exports.SIP = exports.LLDP = exports.TacacsPlus = exports.Redis = exports.MQTT = exports.CoAP = exports.OPCUA = exports.ENIP = exports.BACnetIP = exports.C37118 = exports.DNP3 = exports.ModbusTCP = void 0;
5
- exports.BitTorrent = exports.BSAPIP = exports.ADSAMS = exports.Zabbix = exports.TimeProtocol = exports.StatsD = exports.Skinny = exports.OPCUAPubSub = exports.OLSR = exports.NHRP = exports.NATS = exports.MGCP = exports.MACsec = exports.IPFIX = exports.IAX2 = exports.GLBP = exports.FCoE = exports.Elasticsearch = exports.Collectd = exports.SMPP = exports.SMB2 = exports.SLMP = exports.Rsync = exports.RTPS = exports.OpenFlow = exports.OmronFINS = exports.NBDS = exports.MongoDB = exports.MQTTSN = exports.LISP = exports.KNXnetIP = exports.GitProtocol = exports.CassandraCQL = exports.CAPWAP = exports.XMPP = exports.WCCP = exports.SercosIII = exports.STOMP = exports.PPTP = exports.POWERLINK = exports.OpenVPN = exports.NBSS = exports.LPD = exports.LDP = exports.ISCSI = exports.HARTIP = exports.Gopher = exports.FoundationFieldbusHSE = exports.DTLS = exports.Babel = void 0;
6
- exports.IEC104_U_Frame = exports.IEC104_S_Frame = exports.IEC104_I_Frame = exports.TLS_Heartbeat = exports.TLS_Handshake = exports.TLS_ChangeCipherSpec = exports.TLS_ApplicationData = exports.TLS_Alert = exports.VLAN_802dot1Q = exports.MMS = exports.CMS = exports.ISOSession = exports.RDP = exports.SNAP = exports.ISIS = exports.CDP = exports.S7comm = exports.STP = exports.LLC = exports.SMB1 = exports.SCTP = exports.RTP = exports.RTCP = exports.QUIC = exports.NFS = exports.WakeOnLAN = exports.WSDiscovery = exports.Teredo = exports.SLPv1 = exports.SLP = exports.PCWorx = exports.ModbusUDP = exports.Megaco = exports.MarkerProtocol = exports.HTTP2 = exports.GESRTP = exports.GELF = exports.DHCPFailover = exports.Codesys = void 0;
3
+ exports.VRRP = exports.BFD = exports.GRE = exports.GENEVE = exports.SOCKS5 = exports.Memcached = exports.Telnet = exports.Kerberos = exports.LDAP = exports.SSH = exports.RFB = exports.Kafka = exports.AMQP = exports.SFlow = exports.WireGuard = exports.ISAKMP = exports.Diameter = exports.PostgreSQL = exports.MySQL = exports.NetFlowV5 = exports.RGoose = exports.L2TP = exports.RMCP = exports.GTPU = exports.VXLAN = exports.RADIUS = exports.Syslog = exports.NBNS = exports.LLMNR = exports.TFTP = exports.DHCPv6 = exports.MDNS = exports.SNMP = exports.DNS = exports.DHCP = exports.STUN = exports.NTP = exports.UDP = exports.TCP = exports.Trailer = exports.RawData = exports.ICMPv6 = exports.IPv6HopByHopOptions = exports.IPv6 = exports.IPv4 = exports.ICMP = exports.IEC61850SampledValues = exports.Goose = exports.EthernetII = exports.ARP = void 0;
4
+ exports.RSVP = exports.RIPng = exports.PTP = exports.PPPoESession = exports.PPPoEDiscovery = exports.PIM = exports.MPLS = exports.LACP = exports.Ident = exports.IGMP = exports.HSRP = exports.GTPv2C = exports.EIGRP = exports.DCCP = exports.WHOIS = exports.SOCKS4 = exports.Rlogin = exports.RIP = exports.Finger = exports.ESP = exports.EAPOL = exports.BGP = exports.AH = exports.HSR = exports.IRC = exports.NNTP = exports.IMAP = exports.POP3 = exports.SMTP = exports.COTP = exports.TPKT = exports.RTSP = exports.FTP = exports.HTTP = exports.EtherCAT = exports.PROFINETRT = exports.SIP = exports.LLDP = exports.TacacsPlus = exports.Redis = exports.MQTT = exports.CoAP = exports.OPCUA = exports.ENIP = exports.BACnetIP = exports.C37118 = exports.DNP3 = exports.ModbusTCP = exports.OSPFv3 = exports.OSPF = void 0;
5
+ exports.ADSAMS = exports.Zabbix = exports.TimeProtocol = exports.StatsD = exports.Skinny = exports.OPCUAPubSub = exports.OLSR = exports.NHRP = exports.NATS = exports.MGCP = exports.MACsec = exports.IPFIX = exports.IAX2 = exports.GLBP = exports.FCoE = exports.Elasticsearch = exports.Collectd = exports.SMPP = exports.SMB2 = exports.SLMP = exports.Rsync = exports.RTPS = exports.OpenFlow = exports.OmronFINS = exports.NBDS = exports.MongoDB = exports.MQTTSN = exports.LISP = exports.KNXnetIP = exports.GitProtocol = exports.CassandraCQL = exports.CAPWAP = exports.XMPP = exports.WCCP = exports.SercosIII = exports.STOMP = exports.PPTP = exports.POWERLINK = exports.OpenVPN = exports.NBSS = exports.LPD = exports.LDP = exports.ISCSI = exports.HARTIP = exports.Gopher = exports.FoundationFieldbusHSE = exports.DTLS = exports.Babel = exports.SunRPC = exports.SSDP = void 0;
6
+ exports.IEC104_U_Frame = exports.IEC104_S_Frame = exports.IEC104_I_Frame = exports.TLS_Heartbeat = exports.TLS_Handshake = exports.TLS_ChangeCipherSpec = exports.TLS_ApplicationData = exports.TLS_Alert = exports.VLAN_802dot1Q = exports.MMS = exports.CMS = exports.ISOSession = exports.RDP = exports.SNAP = exports.ISIS = exports.CDP = exports.S7comm = exports.STP = exports.LLC = exports.SMB1 = exports.SCTP = exports.RTP = exports.RTCP = exports.QUIC = exports.NFS = exports.WakeOnLAN = exports.WSDiscovery = exports.Teredo = exports.SLPv1 = exports.SLP = exports.PCWorx = exports.ModbusUDP = exports.Megaco = exports.MarkerProtocol = exports.HTTP2 = exports.GESRTP = exports.GELF = exports.DHCPFailover = exports.Codesys = exports.BitTorrent = exports.BSAPIP = void 0;
7
7
  var ARP_1 = require("./headers/ARP");
8
8
  Object.defineProperty(exports, "ARP", { enumerable: true, get: function () { return ARP_1.ARP; } });
9
9
  var EthernetII_1 = require("./headers/EthernetII");
@@ -24,6 +24,8 @@ var ICMPv6_1 = require("./headers/ICMPv6");
24
24
  Object.defineProperty(exports, "ICMPv6", { enumerable: true, get: function () { return ICMPv6_1.ICMPv6; } });
25
25
  var RawData_1 = require("./headers/RawData");
26
26
  Object.defineProperty(exports, "RawData", { enumerable: true, get: function () { return RawData_1.RawData; } });
27
+ var Trailer_1 = require("./headers/Trailer");
28
+ Object.defineProperty(exports, "Trailer", { enumerable: true, get: function () { return Trailer_1.Trailer; } });
27
29
  var TCP_1 = require("./headers/TCP");
28
30
  Object.defineProperty(exports, "TCP", { enumerable: true, get: function () { return TCP_1.TCP; } });
29
31
  var UDP_1 = require("./headers/UDP");
@@ -104,6 +106,8 @@ var VRRP_1 = require("./headers/VRRP");
104
106
  Object.defineProperty(exports, "VRRP", { enumerable: true, get: function () { return VRRP_1.VRRP; } });
105
107
  var OSPF_1 = require("./headers/OSPF");
106
108
  Object.defineProperty(exports, "OSPF", { enumerable: true, get: function () { return OSPF_1.OSPF; } });
109
+ var OSPFv3_1 = require("./headers/OSPFv3");
110
+ Object.defineProperty(exports, "OSPFv3", { enumerable: true, get: function () { return OSPFv3_1.OSPFv3; } });
107
111
  var ModbusTCP_1 = require("./headers/ModbusTCP");
108
112
  Object.defineProperty(exports, "ModbusTCP", { enumerable: true, get: function () { return ModbusTCP_1.ModbusTCP; } });
109
113
  var DNP3_1 = require("./headers/DNP3");
@@ -149,6 +149,15 @@ export declare abstract class BaseHeader {
149
149
  * Readonly Header length
150
150
  */
151
151
  get length(): number;
152
+ /**
153
+ * Decode only: the packet offset where this layer's payload ends by its own length field (IPv4
154
+ * Total Length, IPv6 Payload Length, an 802.3 length), read after decode(). The codec decodes the
155
+ * layers above inside that bound and keeps any bytes past it as a `trailer` layer (Ethernet padding
156
+ * and the like). Undefined, the default, means the payload runs to the end of the bytes available.
157
+ * The codec ignores a bound outside [endPos, the bytes available), so a zero, overstated or
158
+ * truncated length decodes as it would without one.
159
+ */
160
+ payloadEnd(): number | undefined;
152
161
  /**
153
162
  * Header length
154
163
  * @protected
@@ -295,6 +304,14 @@ export declare abstract class BaseHeader {
295
304
  * @protected
296
305
  */
297
306
  protected encodeOmittedEtherType(field: FlexibleObject, offset: number): void;
307
+ /**
308
+ * payloadEnd() for a header whose `etherType` field doubles as an IEEE 802.3 length (Ethernet,
309
+ * 802.1Q): a value of 3–1500 gives the octets of LLC data that follow this header. Anything else
310
+ * (an EtherType, or a length too short for an LLC header) sets no bound.
311
+ * @param field the etherType field
312
+ * @protected
313
+ */
314
+ protected ieee802dot3PayloadEnd(field: FlexibleObject): number | undefined;
298
315
  /**
299
316
  * True when `field` was left out of the encode input, which is what asks encode to calculate an
300
317
  * autoCalculated length, count or checksum. Any value given, 0 included, is written as given, so a
@@ -157,6 +157,17 @@ class BaseHeader {
157
157
  get length() {
158
158
  return this.headerLength;
159
159
  }
160
+ /**
161
+ * Decode only: the packet offset where this layer's payload ends by its own length field (IPv4
162
+ * Total Length, IPv6 Payload Length, an 802.3 length), read after decode(). The codec decodes the
163
+ * layers above inside that bound and keeps any bytes past it as a `trailer` layer (Ethernet padding
164
+ * and the like). Undefined, the default, means the payload runs to the end of the bytes available.
165
+ * The codec ignores a bound outside [endPos, the bytes available), so a zero, overstated or
166
+ * truncated length decodes as it would without one.
167
+ */
168
+ payloadEnd() {
169
+ return undefined;
170
+ }
160
171
  /**
161
172
  * Post packet handlers
162
173
  * @protected
@@ -606,6 +617,17 @@ class BaseHeader {
606
617
  this.writeBytes(offset, (0, NumberToBuffer_1.UInt16ToBuffer)(Math.min(length, 0xffff)));
607
618
  });
608
619
  }
620
+ /**
621
+ * payloadEnd() for a header whose `etherType` field doubles as an IEEE 802.3 length (Ethernet,
622
+ * 802.1Q): a value of 3–1500 gives the octets of LLC data that follow this header. Anything else
623
+ * (an EtherType, or a length too short for an LLC header) sets no bound.
624
+ * @param field the etherType field
625
+ * @protected
626
+ */
627
+ ieee802dot3PayloadEnd(field) {
628
+ const length = parseInt(field.getValue('').toString(), 16);
629
+ return length >= 3 && length <= 0x05dc ? this.endPos + length : undefined;
630
+ }
609
631
  /**
610
632
  * True when `field` was left out of the encode input, which is what asks encode to calculate an
611
633
  * autoCalculated length, count or checksum. Any value given, 0 included, is written as given, so a
@@ -14,7 +14,8 @@ import { BaseHeader } from '../abstracts/BaseHeader';
14
14
  *
15
15
  * The 6-octet MAC and 4-octet IPv4 widths are fixed here (Ethernet + IPv4 ARP), independent of the
16
16
  * declared hardware/protocol sizes. Values are range-clamped rather than rejected. match() keys purely
17
- * on the parent's EtherType being 0x0806.
17
+ * on the parent's EtherType being 0x0806. When the declared sizes are 6 and 4, the bytes after the 28
18
+ * octets (Ethernet padding) decode as a `trailer` layer (see payloadEnd).
18
19
  */
19
20
  export declare class ARP extends BaseHeader {
20
21
  SCHEMA: ProtocolJSONSchema;
@@ -22,5 +23,6 @@ export declare class ARP extends BaseHeader {
22
23
  readonly matchKeys: string[];
23
24
  readonly name: string;
24
25
  readonly nickname: string;
26
+ payloadEnd(): number | undefined;
25
27
  match(): boolean;
26
28
  }
@@ -24,7 +24,8 @@ const IPToBuffer_1 = require("../helper/IPToBuffer");
24
24
  *
25
25
  * The 6-octet MAC and 4-octet IPv4 widths are fixed here (Ethernet + IPv4 ARP), independent of the
26
26
  * declared hardware/protocol sizes. Values are range-clamped rather than rejected. match() keys purely
27
- * on the parent's EtherType being 0x0806.
27
+ * on the parent's EtherType being 0x0806. When the declared sizes are 6 and 4, the bytes after the 28
28
+ * octets (Ethernet padding) decode as a `trailer` layer (see payloadEnd).
28
29
  */
29
30
  class ARP extends BaseHeader_1.BaseHeader {
30
31
  constructor() {
@@ -237,6 +238,12 @@ class ARP extends BaseHeader_1.BaseHeader {
237
238
  this.name = 'Address Resolution Protocol';
238
239
  this.nickname = 'ARP';
239
240
  }
241
+ payloadEnd() {
242
+ //An Ethernet/IPv4 ARP packet ends after its 28 octets, so what follows is Ethernet padding. Other
243
+ //declared address sizes set no bound: the fixed-width fields do not cover such a packet.
244
+ const ethernetIPv4 = this.instance.hardware.size.getValue() === 6 && this.instance.protocol.size.getValue() === 4;
245
+ return ethernetIPv4 ? this.endPos : undefined;
246
+ }
240
247
  match() {
241
248
  if (!this.prevCodecModule)
242
249
  return false;
@@ -40,7 +40,8 @@ class BACnetIP extends BaseHeader_1.BaseHeader {
40
40
  type: 'object',
41
41
  summary: 'BACnet/IP func=${function}',
42
42
  properties: {
43
- type: this.fieldHex('type', 0, 1, 'Type'),
43
+ //Default 0x81: the BVLC type of BACnet/IP, and what match() requires.
44
+ type: { ...this.fieldHex('type', 0, 1, 'Type'), default: '81' },
44
45
  'function': this.fieldUInt('function', 1, 1, 'Function'),
45
46
  length: {
46
47
  type: 'integer',