@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.
- package/CHANGELOG.md +295 -0
- package/README.md +62 -10
- package/README.zh-CN.md +55 -9
- package/dist/Codec.d.ts +3 -2
- package/dist/Codec.js +109 -10
- package/dist/abstracts/BaseHeader.d.ts +54 -2
- package/dist/abstracts/BaseHeader.js +185 -43
- package/dist/errors/CodecSchemaValidateError.d.ts +34 -1
- package/dist/errors/CodecSchemaValidateError.js +26 -3
- package/dist/headers/ADSAMS.js +2 -0
- package/dist/headers/AH.js +4 -2
- package/dist/headers/AMQP.js +2 -1
- package/dist/headers/ARP.js +16 -10
- package/dist/headers/BACnetIP.js +1 -0
- package/dist/headers/BFD.d.ts +2 -1
- package/dist/headers/BFD.js +30 -4
- package/dist/headers/BGP.js +6 -1
- package/dist/headers/Babel.js +2 -1
- package/dist/headers/BitTorrent.d.ts +2 -0
- package/dist/headers/BitTorrent.js +4 -2
- package/dist/headers/C37118.d.ts +3 -2
- package/dist/headers/C37118.js +35 -15
- package/dist/headers/CAPWAP.js +1 -0
- package/dist/headers/CDP.d.ts +3 -3
- package/dist/headers/CDP.js +47 -11
- package/dist/headers/CMS.js +27 -12
- package/dist/headers/COTP.d.ts +2 -0
- package/dist/headers/COTP.js +8 -4
- package/dist/headers/CassandraCQL.js +1 -0
- package/dist/headers/CoAP.js +20 -2
- package/dist/headers/Codesys.js +1 -0
- package/dist/headers/Collectd.js +1 -1
- package/dist/headers/DCCP.d.ts +3 -2
- package/dist/headers/DCCP.js +61 -8
- package/dist/headers/DHCPFailover.js +1 -0
- package/dist/headers/DNP3.d.ts +5 -3
- package/dist/headers/DNP3.js +62 -7
- package/dist/headers/DNS.js +3 -3
- package/dist/headers/DTLS.js +1 -0
- package/dist/headers/Diameter.js +3 -2
- package/dist/headers/EAPOL.js +1 -1
- package/dist/headers/EIGRP.d.ts +3 -2
- package/dist/headers/EIGRP.js +32 -6
- package/dist/headers/ENIP.js +1 -0
- package/dist/headers/Elasticsearch.js +1 -0
- package/dist/headers/EtherCAT.js +1 -0
- package/dist/headers/EthernetII.js +7 -0
- package/dist/headers/FTP.js +6 -6
- package/dist/headers/Finger.js +3 -3
- package/dist/headers/FoundationFieldbusHSE.js +1 -0
- package/dist/headers/GELF.js +8 -1
- package/dist/headers/GENEVE.js +2 -1
- package/dist/headers/GLBP.js +1 -1
- package/dist/headers/GRE.js +1 -0
- package/dist/headers/GTPU.js +1 -0
- package/dist/headers/GTPv2C.js +2 -0
- package/dist/headers/GitProtocol.js +2 -2
- package/dist/headers/Goose.js +24 -12
- package/dist/headers/Gopher.js +6 -6
- package/dist/headers/HARTIP.js +1 -0
- package/dist/headers/HSR.js +3 -0
- package/dist/headers/HTTP.js +6 -6
- package/dist/headers/HTTP2.js +2 -0
- package/dist/headers/ICMP.js +3 -1
- package/dist/headers/ICMPv6.js +11 -16
- package/dist/headers/IEC104_I_Frame.js +215 -163
- package/dist/headers/IEC104_S_Frame.js +26 -3
- package/dist/headers/IEC104_U_Frame.js +33 -4
- package/dist/headers/IEC61850SampledValues.js +6 -5
- package/dist/headers/IGMP.d.ts +8 -7
- package/dist/headers/IGMP.js +58 -19
- package/dist/headers/IMAP.js +5 -5
- package/dist/headers/IPFIX.js +3 -2
- package/dist/headers/IPv4.js +41 -21
- package/dist/headers/IPv6.js +5 -1
- package/dist/headers/IPv6HopByHopOptions.js +15 -12
- package/dist/headers/IRC.js +4 -4
- package/dist/headers/ISAKMP.js +3 -1
- package/dist/headers/ISCSI.js +2 -0
- package/dist/headers/ISIS.js +2 -1
- package/dist/headers/ISOSession.d.ts +2 -0
- package/dist/headers/ISOSession.js +14 -10
- package/dist/headers/Ident.js +8 -8
- package/dist/headers/KNXnetIP.js +1 -0
- package/dist/headers/Kafka.js +1 -0
- package/dist/headers/Kerberos.js +2 -0
- package/dist/headers/L2TP.js +3 -2
- package/dist/headers/LACP.js +1 -1
- package/dist/headers/LDAP.js +3 -4
- package/dist/headers/LDP.js +1 -0
- package/dist/headers/LLC.d.ts +2 -0
- package/dist/headers/LLC.js +3 -0
- package/dist/headers/LPD.js +3 -3
- package/dist/headers/MGCP.js +7 -7
- package/dist/headers/MMS.d.ts +2 -0
- package/dist/headers/MMS.js +10 -6
- package/dist/headers/MQTT.js +1 -0
- package/dist/headers/MQTTSN.js +1 -0
- package/dist/headers/MarkerProtocol.js +1 -1
- package/dist/headers/Megaco.js +3 -3
- package/dist/headers/Memcached.js +3 -2
- package/dist/headers/ModbusTCP.js +1 -0
- package/dist/headers/ModbusUDP.js +1 -0
- package/dist/headers/MongoDB.js +1 -0
- package/dist/headers/MySQL.js +1 -0
- package/dist/headers/NATS.js +2 -2
- package/dist/headers/NBDS.js +1 -0
- package/dist/headers/NBSS.js +1 -0
- package/dist/headers/NFS.js +1 -0
- package/dist/headers/NHRP.d.ts +1 -1
- package/dist/headers/NHRP.js +30 -4
- package/dist/headers/NNTP.js +6 -6
- package/dist/headers/NetFlowV5.js +1 -0
- package/dist/headers/OLSR.js +2 -1
- package/dist/headers/OPCUA.js +1 -0
- package/dist/headers/OSPF.d.ts +3 -3
- package/dist/headers/OSPF.js +72 -7
- package/dist/headers/OpenFlow.js +1 -0
- package/dist/headers/OpenVPN.js +1 -0
- package/dist/headers/PCWorx.js +1 -0
- package/dist/headers/PIM.d.ts +3 -3
- package/dist/headers/PIM.js +55 -6
- package/dist/headers/POP3.js +5 -5
- package/dist/headers/PPPoEDiscovery.js +4 -1
- package/dist/headers/PPPoESession.js +1 -0
- package/dist/headers/PPTP.js +1 -0
- package/dist/headers/PTP.js +6 -1
- package/dist/headers/PostgreSQL.js +2 -0
- package/dist/headers/QUIC.js +2 -0
- package/dist/headers/RADIUS.js +1 -0
- package/dist/headers/RDP.d.ts +2 -0
- package/dist/headers/RDP.js +6 -1
- package/dist/headers/RFB.js +4 -4
- package/dist/headers/RGoose.js +8 -1
- package/dist/headers/RSVP.d.ts +3 -3
- package/dist/headers/RSVP.js +59 -44
- package/dist/headers/RTCP.js +2 -0
- package/dist/headers/RTP.js +2 -1
- package/dist/headers/RTPS.d.ts +2 -0
- package/dist/headers/RTPS.js +4 -1
- package/dist/headers/RTSP.js +7 -7
- package/dist/headers/RawData.js +1 -1
- package/dist/headers/Redis.js +4 -4
- package/dist/headers/Rlogin.js +5 -5
- package/dist/headers/Rsync.js +2 -2
- package/dist/headers/S7comm.d.ts +2 -0
- package/dist/headers/S7comm.js +5 -1
- package/dist/headers/SCTP.d.ts +2 -2
- package/dist/headers/SCTP.js +31 -7
- package/dist/headers/SFlow.js +2 -1
- package/dist/headers/SIP.js +6 -6
- package/dist/headers/SLMP.js +1 -0
- package/dist/headers/SLP.js +2 -0
- package/dist/headers/SLPv1.js +1 -0
- package/dist/headers/SMB1.js +1 -0
- package/dist/headers/SMB2.js +1 -0
- package/dist/headers/SMPP.js +1 -0
- package/dist/headers/SMTP.js +6 -6
- package/dist/headers/SNAP.js +18 -5
- package/dist/headers/SNMP.js +6 -6
- package/dist/headers/SOCKS4.js +11 -5
- package/dist/headers/SSDP.js +7 -7
- package/dist/headers/SSH.js +5 -4
- package/dist/headers/STOMP.js +5 -5
- package/dist/headers/STUN.js +3 -1
- package/dist/headers/SercosIII.d.ts +3 -2
- package/dist/headers/SercosIII.js +41 -5
- package/dist/headers/Skinny.js +1 -0
- package/dist/headers/StatsD.js +4 -4
- package/dist/headers/SunRPC.js +1 -0
- package/dist/headers/Syslog.js +2 -2
- package/dist/headers/TCP.js +31 -64
- package/dist/headers/TLS_Alert.js +42 -12
- package/dist/headers/TLS_ApplicationData.d.ts +3 -3
- package/dist/headers/TLS_ApplicationData.js +32 -8
- package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
- package/dist/headers/TLS_ChangeCipherSpec.js +33 -8
- package/dist/headers/TLS_Handshake.js +78 -30
- package/dist/headers/TLS_Heartbeat.d.ts +4 -3
- package/dist/headers/TLS_Heartbeat.js +50 -8
- package/dist/headers/TPKT.js +4 -2
- package/dist/headers/TacacsPlus.js +1 -0
- package/dist/headers/Telnet.js +2 -1
- package/dist/headers/Teredo.js +2 -2
- package/dist/headers/UDP.js +20 -56
- package/dist/headers/VLAN_802dot1Q.js +8 -0
- package/dist/headers/VRRP.d.ts +2 -1
- package/dist/headers/VRRP.js +45 -9
- package/dist/headers/WCCP.js +1 -0
- package/dist/headers/WHOIS.js +3 -3
- package/dist/headers/WSDiscovery.js +2 -2
- package/dist/headers/XMPP.js +3 -3
- package/dist/headers/Zabbix.js +1 -0
- package/dist/helper/Checksum.d.ts +15 -0
- package/dist/helper/Checksum.js +56 -0
- package/dist/helper/Crc.d.ts +18 -0
- package/dist/helper/Crc.js +58 -0
- package/dist/index.d.ts +3 -3
- package/dist/lib/SchemaValidator.d.ts +31 -0
- package/dist/lib/SchemaValidator.js +137 -0
- package/dist/schema/ProtocolFieldJSONSchema.d.ts +4 -1
- package/dist/schema/ProtocolJSONSchema.d.ts +1 -1
- package/dist/types/CodecErrorInfo.d.ts +11 -0
- package/dist/types/LayerGraph.d.ts +14 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,301 @@
|
|
|
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.1.0](https://github.com/myq1991/netkitty/compare/@netkitty/codec@2.0.0...@netkitty/codec@2.1.0) (2026-11-26)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **codec:** annotate inferred and computed fields, declare constant fallbacks as defaults ([c7953e3](https://github.com/myq1991/netkitty/commit/c7953e3fac0ac8fa687872d044b2cd81e624ab74))
|
|
12
|
+
* **codec:** build the IGMPv3 query from its own fields and compute the IGMP checksum ([07bc9a7](https://github.com/myq1991/netkitty/commit/07bc9a75581fe0269ee77051de22a8d206c71bea))
|
|
13
|
+
* **codec:** compute auto fields only when left out, and find the IP layer for pseudo-headers ([f3b2571](https://github.com/myq1991/netkitty/commit/f3b25717506353ed47f9178dbbf760924409e88e))
|
|
14
|
+
* **codec:** compute PIM, EIGRP, NHRP, VRRP and OSPF checksums when left out ([5d82eda](https://github.com/myq1991/netkitty/commit/5d82eda00616f9b4abe105c2051c5248f4382a8e))
|
|
15
|
+
* **codec:** compute RSVP and CDP checksums, write their explicit lengths as given ([3f0c6fa](https://github.com/myq1991/netkitty/commit/3f0c6faa7a609d00a503331f2da76906bcfd0fe5))
|
|
16
|
+
* **codec:** compute the BFD, CMS, CoAP and IEC 104 lengths when left out ([59be323](https://github.com/myq1991/netkitty/commit/59be32306a0d40e21e7162ef5c9105603230fd4c))
|
|
17
|
+
* **codec:** compute the SCTP CRC32c and the DCCP checksum when left out ([58311cb](https://github.com/myq1991/netkitty/commit/58311cbfb951786e87fa2e218fce39074786ed5d))
|
|
18
|
+
* **codec:** compute the Sercos III, DNP3 and C37.118 checks when left out ([a73343b](https://github.com/myq1991/netkitty/commit/a73343b68a4e6db4bba0aab3bbb72cfe8ab51824))
|
|
19
|
+
* **codec:** compute TLS record lengths when left out ([2183575](https://github.com/myq1991/netkitty/commit/21835752dc1140808dc9f0e1b1a5c74d60b5ca11))
|
|
20
|
+
* **codec:** leave the IPv4 pseudo-header out of the VRRP v3 checksum (RFC 9568) ([3a0487e](https://github.com/myq1991/netkitty/commit/3a0487e64fa54cb4284cd4c87d3d2dc0ac50b990))
|
|
21
|
+
* **codec:** let every decoded IEC 104 I-frame tree pass encode validation ([abac0b8](https://github.com/myq1991/netkitty/commit/abac0b886971274d531c3b2e83c285282d8770d2))
|
|
22
|
+
* **codec:** zero-fill a DCCP header up to the Data Offset computed for it ([a521722](https://github.com/myq1991/netkitty/commit/a521722ab35c654935a4f58962ddb71f76ee01e7))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Upgrade note
|
|
26
|
+
|
|
27
|
+
Encode now computes a length, count or checksum only when the field is left out: a caller that passes
|
|
28
|
+
`0` to mean "compute it" (as `ipv4.checksum: 0` / `udp.length: 0` used to allow) must leave the field
|
|
29
|
+
out instead. Read "Behaviour changes" below before upgrading.
|
|
30
|
+
|
|
31
|
+
### Behaviour changes (check before upgrading)
|
|
32
|
+
|
|
33
|
+
#### An explicit value is never recomputed
|
|
34
|
+
- An `autoCalculated` field is computed only when it is **left out** of the input. Any value you give,
|
|
35
|
+
**0 included**, is written as given. Before, many fields treated 0 as "compute": `ipv4.hdrLen` /
|
|
36
|
+
`length` / `checksum`, `ipv6.plen`, `ipv6-hopopt.len`, `tcp.hdrLen` / `checksum`, `udp.length` /
|
|
37
|
+
`checksum`, `icmp.checksum`, `icmpv6.checksum`, `ah.payloadLen`, `tpkt.length`, `cotp.li`,
|
|
38
|
+
`stun.messageLength`, `ipfix.length`, `goose.length`, `sv.length`. Code that passes `0` to mean
|
|
39
|
+
"calculate it" now sends 0; leave the field out instead.
|
|
40
|
+
- On an integer field, `null` is coerced to 0 (so it counts as a given 0) and `''` fails validation. On
|
|
41
|
+
a hex string field, `''` and `null` count as left out.
|
|
42
|
+
- A captured UDP datagram with checksum 0 ("no checksum", RFC 768) now re-encodes to its own bytes
|
|
43
|
+
(it used to get a computed checksum).
|
|
44
|
+
- `bgp.length` minimum 19 → 0 and `ptp.messageLength` minimum 34 → 0: a shorter value is written as given
|
|
45
|
+
with a warning (`Length should be at least 19` / `Message length should be at least 34`) instead of
|
|
46
|
+
failing validation.
|
|
47
|
+
|
|
48
|
+
#### encode() no longer changes its input
|
|
49
|
+
- `encode()` validates a deep copy (`structuredClone`, Node.js 17+) of each input, so Ajv defaults
|
|
50
|
+
(`tpkt.version`, `cotp.pduType`, …) and type coercion (`'102'` → `102`) no longer appear in the objects
|
|
51
|
+
you passed. Code that read defaults back from its own input after encoding must read the decoded
|
|
52
|
+
result instead.
|
|
53
|
+
|
|
54
|
+
#### Checksums over the right IP layer
|
|
55
|
+
- TCP, UDP and ICMPv6 take their pseudo-header from the nearest IPv4/IPv6 layer beneath, so a checksum
|
|
56
|
+
behind AH or an IPv6 extension header is now correct (it was 0 behind AH). With no IP layer beneath, a
|
|
57
|
+
warning is recorded (`Cannot compute … checksum: no IP layer beneath …`). ICMPv6 inside an
|
|
58
|
+
IPv6-in-IPv6 tunnel now uses the inner header.
|
|
59
|
+
|
|
60
|
+
#### Newly computed fields (when left out; all now `autoCalculated`)
|
|
61
|
+
Before, each of these was written as 0 when left out.
|
|
62
|
+
- Internet checksums: `igmp.checksum` (the IGMP message), `pim.checksum` (whole message; a Register
|
|
63
|
+
covers its first 8 bytes; over IPv6 with the pseudo-header, next header 103), `eigrp.checksum`,
|
|
64
|
+
`rsvp.checksum`, `nhrp.checksum`, `vrrp.checksum` (v2 and v3 over IPv4 over the message, RFC 9568;
|
|
65
|
+
v3 over IPv6 with the pseudo-header, protocol 112), `ospf.checksum` (OSPFv2: the packet without the
|
|
66
|
+
64-bit authentication field; 0 for cryptographic authentication, RFC 2328 D.4.3), `dccp.checksum`
|
|
67
|
+
(pseudo-header protocol 33, header, and the data CsCov selects), `cdp.checksum` (the Internet checksum
|
|
68
|
+
as Cisco computes it for an odd length, matching Wireshark).
|
|
69
|
+
- CRCs: `sctp.checksum` (CRC32c over the packet, least significant byte first), `sercos3.crc32`
|
|
70
|
+
(CRC-32 over the Ethernet header and Sercos bytes 0–1, little-endian; needs an Ethernet layer
|
|
71
|
+
directly beneath, else 0 with an error), `dnp3.headerCrc` (CRC-16/DNP over octets 0–7),
|
|
72
|
+
`c37118.chk` (CRC-16/CCITT-FALSE over the frame before CHK).
|
|
73
|
+
- Lengths: `ospf.packetLength`, `bfd.length` (24 + authentication section), `cms.frameLength` (bytes
|
|
74
|
+
after the 4-byte header), `dnp3.length` (5 + user-data octets, excluding block CRCs), `coap.tokenLength`,
|
|
75
|
+
`length` of tls-alert / tls-appdata / tls-ccsp / tls-handshake / tls-heartbeat (this record only),
|
|
76
|
+
`tls-handshake.handshakeLength`, `tls-heartbeat.payloadLength`, `apduLength` of IEC 104 I, S and U
|
|
77
|
+
frames.
|
|
78
|
+
- `c37118.framesize` left out now counts the 2-byte CHK when CHK is also left out (it added 0).
|
|
79
|
+
|
|
80
|
+
#### Other encode changes in these protocols
|
|
81
|
+
- IGMPv3 Query: its body is emitted from its own fields (type 0x11 with any of `resvSQRV`,
|
|
82
|
+
`qqic`, `numSources`, `sources` given), not from the IP Total Length, so a left-out `ipv4.length` no
|
|
83
|
+
longer drops the sources. A type 0x11 message with no v3 field is an 8-byte v2 query, whatever the IP
|
|
84
|
+
length says.
|
|
85
|
+
- `rsvp.length` is written as given (below 8 is flagged; it used to be raised to 8); left out it is
|
|
86
|
+
the bytes actually written, not the sum of the object lengths. `rsvp.objects[].length` and
|
|
87
|
+
`cdp.tlvs[].length` given as 0 are written as 0 (they were recomputed).
|
|
88
|
+
- DCCP with `dataOffset` left out: the header is zero-filled up to the computed Data Offset (a crafted
|
|
89
|
+
header that was not a multiple of 4 octets grows to the next one).
|
|
90
|
+
- IEC 104 I-frame with `apduLength` left out: information objects are spaced by the size of the first
|
|
91
|
+
object written (they were spaced by the given APDU length).
|
|
92
|
+
- A left-out TLS `appData` or heartbeat `payloadMessage` writes nothing (it wrote one 0x00 octet).
|
|
93
|
+
- Layers built from empty data (plus the discriminator `allowedNextLayers` gives) now re-decode as
|
|
94
|
+
themselves: dnp3, coap, c37118, every tls-* record, IEC 104 I and S frames.
|
|
95
|
+
|
|
96
|
+
#### Schema representation (`CODEC_SCHEMAS`)
|
|
97
|
+
- `autoCalculated: true` added to the demux fields encode infers from the next layer
|
|
98
|
+
(`eth` / `vlan` / `hsr` / `snap` `etherType`, `gre` / `geneve` `protocolType`, `ipv4.protocol`,
|
|
99
|
+
`ipv6.nxt`, `ipv6-hopopt.nxt`, `ah.nxt`), to `snap.oui` (derived from `ouiProtocol`) and to
|
|
100
|
+
`rdp.length` (always 8 for the Negotiation structure).
|
|
101
|
+
- `default` added where encode already fell back to a protocol constant, so editors can show it:
|
|
102
|
+
`ipv4.version` 4, `ipv6.version` 6, `ipv4.flags.df` 1, `gelf.magic` `'1e0f'`, `http2.preface` (the
|
|
103
|
+
connection preface), `llc.control` `'03'`, `gtpv2.flags.version` 2, `isakmp.version.major` 2,
|
|
104
|
+
`l2tp.flags.version` 2, `pppoe-disc.type` 1 / `version` 1, `r-session.commonHeaderId` 0x80 /
|
|
105
|
+
`version` 1, `rtcp.version` 2, `kerberos.msgType` 0x6a, `ldap.protocolOpTag` 0x60, `snmp.pduType`
|
|
106
|
+
0xa0, `snmp.variableBindings[].valueType` 0x05, `pim.version` 2, `rsvp.version` 1, `vrrp.version` 2 /
|
|
107
|
+
`type` 1 / `adverInt` 1, `bfd.version` 1, `c37118.sync.version` 1, `cms.protocolType` 1. Encoded
|
|
108
|
+
bytes are unchanged; a field filled from its default is no longer reported as `'Not Found'`.
|
|
109
|
+
- Protocol signatures now have a `default`, so a layer built from empty data is recognised again:
|
|
110
|
+
`dnp3.start` `'0564'`, `c37118.leadIn` `'aa'`, `coap.version` 1, the TLS records' `contentType`
|
|
111
|
+
(21 alert, 23 appdata, 20 ccsp, 22 handshake, 24 heartbeat) and `version` `'TLS1.2'`,
|
|
112
|
+
`tls-ccsp.change_cipher_spec_message` `'01'`, `startByte` 104 on IEC 104 I / S / U frames.
|
|
113
|
+
|
|
114
|
+
#### IEC 104 I-frame item schemas (decoded trees now always pass encode validation)
|
|
115
|
+
- Item variants that disagreed with what decode fills are corrected: type 40 (M_EP_TF_1) no longer
|
|
116
|
+
requires SIE/SIF (type 39's fields); type 38 (M_EP_TD_1) uses `Milliseconds_16` / `Milliseconds_56`
|
|
117
|
+
instead of `Milliseconds`; type 70 (M_EI_NA_1) uses `COI_R` / `COI_I` instead of `COI` / `LPC`; type 101
|
|
118
|
+
(C_CI_NA_1) uses `RQT` instead of `RQI`. Inputs written against the old names fail validation.
|
|
119
|
+
- Time fields of the 21 time-tagged variants accept their full wire range (milliseconds 0–65535, minutes
|
|
120
|
+
0–63, hours 0–31, day 0–31, month 0–15, year 0–127); a captured out-of-calendar time (day or month 0)
|
|
121
|
+
re-encodes instead of throwing.
|
|
122
|
+
|
|
123
|
+
### Additions
|
|
124
|
+
- `BaseHeader` (for custom codecs): protected `isLeftOut(field)` (the left-out test every computed field
|
|
125
|
+
uses), `nearestIPModule()` and `ipPseudoHeader(protocol, upperLayerLength)`.
|
|
126
|
+
|
|
127
|
+
### Known limitations
|
|
128
|
+
- Not computed: OSPFv3 checksums, the DNP3 per-block data CRCs (kept verbatim in `payload`), IEC 104
|
|
129
|
+
`numberOfObject` (not derived from the item count). tls-handshake still writes its 6-byte hello
|
|
130
|
+
skeleton when `messagedata` is left out. A tcp → IEC 104 U-frame built from empty data does not
|
|
131
|
+
re-decode as a U-frame.
|
|
132
|
+
- IPv4 does not bound its payload by Total Length, and Ethernet padding after an 802.3 / LLC payload
|
|
133
|
+
decodes as a trailing layer; a left-out length or checksum then counts the padding.
|
|
134
|
+
- Where a given length positions what follows (QUIC connection IDs, S7comm parameter/data, Diameter AVPs,
|
|
135
|
+
AMQP frame end, …), an explicit length that disagrees with the data reshapes the packet the way decode
|
|
136
|
+
would read it.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# [2.0.0](https://github.com/myq1991/netkitty/compare/@netkitty/codec@1.2.1...@netkitty/codec@2.0.0) (2026-11-26)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
### Bug Fixes
|
|
146
|
+
|
|
147
|
+
* **codec:** do not write inferred discriminators back into encode input ([4a44c65](https://github.com/myq1991/netkitty/commit/4a44c65643b9e6b7be6d7da6a44f4f5de54f937b))
|
|
148
|
+
* **codec:** keep an IEC 104 U-frame controlField when apciType is left out ([6adde45](https://github.com/myq1991/netkitty/commit/6adde45e71250cc8f414c1049910211865d03b76))
|
|
149
|
+
* **codec:** keep bytes trailing a GOOSE PDU or a plain SOCKS4 request intact ([3c314f8](https://github.com/myq1991/netkitty/commit/3c314f80093e46adb6e71c0f158f951f010dc3c1))
|
|
150
|
+
* **codec:** make every decoded tree re-encodable ([26b298f](https://github.com/myq1991/netkitty/commit/26b298f9f3ab0a0882737356d64364db2fb68de8))
|
|
151
|
+
* **codec:** reject unknown control values instead of silently encoding nothing ([9b832f8](https://github.com/myq1991/netkitty/commit/9b832f8d23129349d46c61b9d37e120268f93c89))
|
|
152
|
+
* **codec:** treat missing optional encode data as empty and name the failing field ([0ce131a](https://github.com/myq1991/netkitty/commit/0ce131adaffc397d6cffb02678fa946e3da96122))
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
### Features
|
|
156
|
+
|
|
157
|
+
* **codec:** annotate read-only, auto-calculated and byte-aligned schema fields ([0aa78e3](https://github.com/myq1991/netkitty/commit/0aa78e3ae77c67fbc27b45a8d17d2173ce291e81))
|
|
158
|
+
* **codec:** compile schema validators lazily and locate validation errors ([7c0b81d](https://github.com/myq1991/netkitty/commit/7c0b81db006bafd2c9e745bca922cdef5e453509))
|
|
159
|
+
* **codec:** complete the editor layer graph and infer omitted discriminators ([fae632a](https://github.com/myq1991/netkitty/commit/fae632a36d4220776ecaae693cd8d6765c8f37aa))
|
|
160
|
+
* **codec:** grade recorded errors by severity ([843381a](https://github.com/myq1991/netkitty/commit/843381a5f454805c4e5abe296cbd4f99c2ea0ad2))
|
|
161
|
+
* **codec:** validate address and number-string fields by format ([f828f4d](https://github.com/myq1991/netkitty/commit/f828f4d57b3faa895b727447d4eb669372ce6a34))
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
### BREAKING CHANGES
|
|
165
|
+
|
|
166
|
+
* **codec:** encode() rejects ipv4/ipv6/mac fields that are not valid
|
|
167
|
+
addresses and bigint fields that are not integer strings
|
|
168
|
+
(CodecSchemaValidateError, keyword format / pattern).
|
|
169
|
+
|
|
170
|
+
### Behaviour changes (check before upgrading)
|
|
171
|
+
|
|
172
|
+
#### encode() input validation (throws `CodecSchemaValidateError`)
|
|
173
|
+
- Address and number-string fields are validated by what they are. Any string field whose
|
|
174
|
+
`contentEncoding` is `ipv4` / `ipv6` / `mac` now gets `format: 'ipv4' | 'ipv6' | 'mac'`, and a `bigint`
|
|
175
|
+
field gets `pattern: '^(-?[0-9]+|0[xX][0-9a-fA-F]+)$'`. Input that used to pass — `ipv4.sip = 'abcdefg'`,
|
|
176
|
+
`sv.seqASDU[].refrTm = 'test'` — now throws (keyword `format` / `pattern`). IPv4 octets with leading
|
|
177
|
+
zeros (`010.0.0.1`) are still accepted.
|
|
178
|
+
- `gelf.form` is an enum `['chunked', 'gzip', 'uncompressed']`; an unknown or empty form now throws
|
|
179
|
+
(it used to encode a 0-byte GELF layer). Leave it out to infer it.
|
|
180
|
+
- The record-level `version` of tls-alert / tls-appdata / tls-ccsp / tls-handshake / tls-heartbeat is an
|
|
181
|
+
enum (the version names, or `'0'`).
|
|
182
|
+
- Constraints were removed where decode could produce a value that encode then rejected (no value accepted
|
|
183
|
+
before is rejected now): `ipv4.hdrLen` minimum 20 → 0; `arp.opcode` enum `[1,2,3,4]` → integer 0..65535
|
|
184
|
+
(type `number` → `integer`); `socks4.command` / `socks4.status` / `eapol.packetType` enums removed;
|
|
185
|
+
`snmp.version` / `errorStatus` / `errorIndex` and `ldap.messageID` unbounded; `goose.goosePdu.gocbRef` /
|
|
186
|
+
`datSet` / `goID` maxLength removed (encode still reports over-long values); `sv.svPdu.noASDU` minimum
|
|
187
|
+
1 → 0; `rsvp.length` minimum 8 → 0; tls-alert `length` maximum 255 → 65535;
|
|
188
|
+
tls-handshake `handshakeLength` maximum → 16777215.
|
|
189
|
+
- IEC 104 Double Command (type 46) item variant required `SCS`; it now requires `DCS`.
|
|
190
|
+
|
|
191
|
+
#### `CodecSchemaValidateError`
|
|
192
|
+
- New properties: `layerId`, `layerIndex` (position in the `encode()` array), `path` (dotted field path,
|
|
193
|
+
same form as `errors[].path`, e.g. `options[0].kind`), `errors` (Ajv error objects plus `path`).
|
|
194
|
+
- `message` is still Ajv's message. Inside item variants (`anyOf`), it is now the message of the most
|
|
195
|
+
specific failure (e.g. `must be integer` at `goosePdu.allData[0].value[0].value`) instead of Ajv's first
|
|
196
|
+
one, which was usually a sibling variant's `must be equal to constant`.
|
|
197
|
+
|
|
198
|
+
#### `result.errors` / layer `errors`
|
|
199
|
+
- Every entry has `severity: 'error' | 'warning' | 'info'` (optional in the type). Decode errors are all
|
|
200
|
+
`error`. On encode: `'Not Found'` (field left out, default used) → `info`; adjusted/flagged but written
|
|
201
|
+
→ `warning`; could not be encoded as given → `error`.
|
|
202
|
+
- `'Not Found'` is no longer reported for an omitted `etherType` / `protocol` / `nxt` that encode now
|
|
203
|
+
infers (see below). It is still reported (as `info`) for other defaulted fields, and for `etherType` when
|
|
204
|
+
nothing follows that it could be inferred from.
|
|
205
|
+
- TLS headers reported `'Not found'`; now `'Not Found'` like every other header.
|
|
206
|
+
- Internal exceptions are recorded under the failing field's path instead of `''`
|
|
207
|
+
(`Encode error: …` / `Decode error: …`, severity `error`).
|
|
208
|
+
- Errors recorded by packet-level post handlers are now included in `result.errors` (they were dropped);
|
|
209
|
+
a post handler that throws no longer escapes `encode()` / `decode()`.
|
|
210
|
+
- New messages: `Unknown TLS version '…', encoded as 0`, `Unknown alert level '…'`, `Unknown alert
|
|
211
|
+
description '…'`, `Unknown handshake type '…'`, `Unknown heartbeat type '…'`, `Unknown APCI type '…'`
|
|
212
|
+
(IEC 104 I/S/U), `Unknown form '…'` (GELF), `Header length should be at least 20` (IPv4),
|
|
213
|
+
`Cannot compute UDP checksum: no IP layer beneath UDP`, `802.3 length … exceeds 1500`,
|
|
214
|
+
`Not Found, extension ignored` (TLS handshake extension without type), `Invalid Structure value, encoded
|
|
215
|
+
as empty` / `Invalid INT64 value` (GOOSE).
|
|
216
|
+
|
|
217
|
+
#### encode() output for omitted fields
|
|
218
|
+
- An omitted parent demux field is filled from the next input layer: EtherType (eth, vlan, hsr, gre,
|
|
219
|
+
geneve, snap), IPv4 `protocol`, IPv6 `nxt` — the value `childDiscriminator()` returns. eth/vlan followed
|
|
220
|
+
by `llc` get the IEEE 802.3 length. Explicit values (including `'0000'` / `0`) are always written as
|
|
221
|
+
given. Before: `0x0000` / `0` plus `'Not Found'`. The inferred value is used for the encoded
|
|
222
|
+
layer only; the input objects passed to `encode()` are not modified.
|
|
223
|
+
- Signature fields default to their protocol constants (new schema `default`, applied by Ajv):
|
|
224
|
+
`tpkt.version` 3, `cotp.pduType` 0xF0 and `cotp.eot` true, `s7comm.protocolId` 0x32,
|
|
225
|
+
`rdp.negotiationType` 1, `rtps.magic` `'52545053'`, `bittorrent.pstrlen` 19 / `pstr`
|
|
226
|
+
(`'BitTorrent protocol'`), `isis.irpDiscriminator` `'83'`, `iso-session.spdus` (Give Tokens + Data
|
|
227
|
+
Transfer), `mms.message` `'6100'`. `snap.oui` / `etherType` default from `snap.ouiProtocol`.
|
|
228
|
+
- IEC 104 S-frame without `apciType` gets the S-format bit (it got the I-format bit). U-frame: an omitted
|
|
229
|
+
`apciType` leaves `controlField` as given (its default used to overwrite the first control byte), an
|
|
230
|
+
omitted `controlField` is a 4-octet zero word (it was 1 byte), an unknown `apciType` writes a 4-octet
|
|
231
|
+
zero word. An unknown I-frame value no longer writes its format bit into octet 5.
|
|
232
|
+
- TCP generic option `{kind: 0 | 1}` is one octet (it was a kind+length pair); `{kind}` without `data` is
|
|
233
|
+
the bare kind+length pair (it threw internally). GOOSE `Structure` without `value` is an empty Structure.
|
|
234
|
+
- UDP encoded without an IP layer beneath records a checksum error instead of throwing out of `encode()`.
|
|
235
|
+
|
|
236
|
+
#### decode() output
|
|
237
|
+
- Bytes after a GOOSE PDU (padding, trailer) stay whole in the `raw` layer; the goose layer no longer
|
|
238
|
+
swallows part of them.
|
|
239
|
+
- SOCKS4 `domain` is only decoded for a SOCKS4a request (dstIp 0.0.0.x); bytes after a plain request
|
|
240
|
+
go to `raw`.
|
|
241
|
+
- A header the fields could not reproduce is left to `raw`: IPv4 with IHL 0 or fewer bytes than its
|
|
242
|
+
header, Ethernet < 14 bytes, 802.1Q tag < 4 bytes, ARP < 28 bytes. (IPv4 IHL 1–4 still decodes as ipv4.)
|
|
243
|
+
- IEC 104 types 46 / 47 / 59 / 60 items now include `QU` (it was dropped).
|
|
244
|
+
|
|
245
|
+
#### Schema representation (`CODEC_SCHEMAS`)
|
|
246
|
+
- `vlan.etherType`, `hsr.etherType`: `contentEncoding: 'hex'` added (value format unchanged: lowercase
|
|
247
|
+
4-hex string).
|
|
248
|
+
- `arp.sender.mac` / `arp.target.mac`: `contentEncoding` `utf8` → `mac`; `arp.sender.ipv4` /
|
|
249
|
+
`arp.target.ipv4`: `utf8` → `ipv4`. `socks4.dstIp`: `contentEncoding: 'ipv4'` added.
|
|
250
|
+
- tls-alert `length` label `'Legacy Version'` → `'Length'`.
|
|
251
|
+
- `format` / `pattern` / `enum` / `default` additions listed above.
|
|
252
|
+
- New optional annotation keywords (Ajv ignores them; nothing about validation changes):
|
|
253
|
+
- `readOnly: true` on 171 fields in 45 protocols — encode ignores the value (display-only projections
|
|
254
|
+
such as http/sip/ftp/smtp `method`/`statusCode`/…, CMS `serviceDataDecoded`/`serviceDataStrings`, MMS
|
|
255
|
+
and ISO-session metadata; values encode always derives, such as dns `rdlength`, geneve `optLen`).
|
|
256
|
+
- `autoCalculated: true` on 137 fields in 100 protocols — encode computes the value when it is left out
|
|
257
|
+
(or 0 where that is the convention); an explicit value is written as given (`ipv4.length` /
|
|
258
|
+
`checksum` / `hdrLen`, `tcp.hdrLen` / `checksum`, `udp.length` / `checksum`, …).
|
|
259
|
+
- `byteAlignment: 4` on `ipv4.options`, `tcp.options`, `stun.attributes[].value`, `iscsi.dataSegment`.
|
|
260
|
+
- `cms.serviceDataDecoded` gains `properties: {}` and `additionalProperties: true` (it was the only
|
|
261
|
+
object node without `properties`).
|
|
262
|
+
|
|
263
|
+
#### allowedNextLayers()
|
|
264
|
+
- `discriminator` keeps its `{field, value} | null` shape. New entries: `llc` after eth / vlan (null),
|
|
265
|
+
`cotp` after tpkt (null), `s7comm` / `iso-session` after cotp (`{field: 'pduType', value: 240}`), `rdp`
|
|
266
|
+
after cotp (`{field: 'pduType', value: 224}`), `mms` after iso-session (null), `rtps` after udp (null),
|
|
267
|
+
`bittorrent` after tcp (null). `null` now also means "recognized by content / derived by encode", not
|
|
268
|
+
only RawData. `childDiscriminator('cotp', 'rdp' | 's7comm' | 'iso-session')` is no longer null.
|
|
269
|
+
|
|
270
|
+
#### Performance
|
|
271
|
+
- `new Codec()` no longer compiles every schema (≈570 ms → ≈15 ms). Each protocol's validator is compiled
|
|
272
|
+
on its first `encode()` (a few ms each).
|
|
273
|
+
|
|
274
|
+
### Additions
|
|
275
|
+
- Types: `CodecErrorSeverity`, `CodecSchemaValidationIssue`, `HeuristicParent`.
|
|
276
|
+
- `BaseHeader`: `heuristicParents` (editor graph only), static `HEURISTIC_PARENTS`;
|
|
277
|
+
`recordError(path, message, severity?)`; protected `encodeOmittedEtherType(field, offset)`.
|
|
278
|
+
|
|
279
|
+
### Known limitations
|
|
280
|
+
|
|
281
|
+
Not fixed in this release:
|
|
282
|
+
- UDP (and probably other) checksums read the pseudo-header from the immediately previous layer only, so
|
|
283
|
+
UDP behind AH or another extension header gets checksum 0.
|
|
284
|
+
- IGMPv3 query: the source list is dropped on encode when `ipv4.length` is left to auto-calculate (it reads
|
|
285
|
+
the IP length before it is computed).
|
|
286
|
+
- Lengths / checksums that are written as given and never computed (so not marked autoCalculated): TLS
|
|
287
|
+
record and handshake lengths, IEC 104 apduLength, cms frameLength, dnp3 length / headerCrc, OSPF
|
|
288
|
+
packetLength / checksum, igmp / pim / eigrp / rsvp / nhrp / cdp / vrrp / sctp / dccp checksums,
|
|
289
|
+
bfd length, coap tokenLength, c37118 chk, sercos3 crc32.
|
|
290
|
+
|
|
291
|
+
Also known (tracked as todo tests):
|
|
292
|
+
- IPv4 does not bound its payload by Total Length (bytes after an empty payload decode as a next header).
|
|
293
|
+
- Many pre-existing editor-graph edges do not re-decode when built from defaults alone (e.g. tcp → http
|
|
294
|
+
with an empty message; ipv4 → ipv6-hopopt / icmpv6 listed via the shared ipproto namespace but rejected
|
|
295
|
+
by match()).
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
6
301
|
## [1.2.1](https://github.com/myq1991/netkitty/compare/@netkitty/codec@1.2.0...@netkitty/codec@1.2.1) (2026-09-28)
|
|
7
302
|
|
|
8
303
|
|
package/README.md
CHANGED
|
@@ -60,12 +60,19 @@ rebuilt.equals(packet) // true for a well-formed packet — decode/encode roun
|
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
Build a packet from scratch by handing `encode` an array of `{id, data}` inputs in outer-to-inner
|
|
63
|
-
order; any field you omit is filled from the schema's defaults, and form-string values are coerced
|
|
63
|
+
order; any field you omit is filled from the schema's defaults, and form-string values are coerced.
|
|
64
|
+
Lengths, counts and checksums you leave out are computed, and a parent's demux field you leave out — an
|
|
65
|
+
EtherType (`eth`/`vlan`/`hsr`/`gre`/`geneve`/`snap`), the IPv4 `protocol`, the IPv6 `nxt` — is filled
|
|
66
|
+
from the layer that follows (eth before LLC gets the IEEE 802.3 length instead). A value you do supply,
|
|
67
|
+
0 included, is always written as given, so a wrong checksum or length can be crafted on purpose. Leave a
|
|
68
|
+
field out to have it computed: on an integer field `null` is coerced to 0 (and written as 0) and `''`
|
|
69
|
+
fails validation, while on a hex field `''` and `null` count as left out. `encode` works on copies: the
|
|
70
|
+
input objects you pass are never changed.
|
|
64
71
|
|
|
65
72
|
```ts
|
|
66
73
|
const {packet} = await codec.encode([
|
|
67
|
-
{id: 'eth', data: {dmac: 'ff:ff:ff:ff:ff:ff', smac: '00:11:22:33:44:55', etherType
|
|
68
|
-
{id: 'ipv4', data: {sip: '192.168.0.1', dip: '192.168.0.2', protocol
|
|
74
|
+
{id: 'eth', data: {dmac: 'ff:ff:ff:ff:ff:ff', smac: '00:11:22:33:44:55'}}, // etherType → '0800'
|
|
75
|
+
{id: 'ipv4', data: {sip: '192.168.0.1', dip: '192.168.0.2'}}, // protocol → 17
|
|
69
76
|
{id: 'udp', data: {srcport: 12345, dstport: 53}}
|
|
70
77
|
])
|
|
71
78
|
```
|
|
@@ -84,12 +91,19 @@ type CodecDecodeResult = {
|
|
|
84
91
|
name: string // human-readable name, e.g. 'Ethernet II'
|
|
85
92
|
nickname: string // short tag, e.g. 'ETH'
|
|
86
93
|
protocol: boolean // whether this layer is a real protocol (false for raw payload)
|
|
87
|
-
errors: CodecErrorInfo[] // {id, path, message}[] — field-path-addressed decode errors
|
|
94
|
+
errors: CodecErrorInfo[] // {id, path, message, severity}[] — field-path-addressed decode errors
|
|
88
95
|
data: HeaderTreeNode // the decoded field tree
|
|
89
96
|
}
|
|
90
97
|
|
|
91
98
|
type CodecEncodeInput = Pick<CodecDecodeResult, 'id' | 'data'> & Partial<Omit<CodecDecodeResult, 'id' | 'data'>>
|
|
92
99
|
type CodecEncodeResult = {packet: Buffer, errors: CodecErrorInfo[]}
|
|
100
|
+
|
|
101
|
+
type CodecErrorInfo = {
|
|
102
|
+
id: string // layer id
|
|
103
|
+
path: string // dotted field path inside the layer, e.g. 'flags.df', 'options[0].kind'
|
|
104
|
+
message: string // e.g. 'Not Found', 'IPv4 version should be 4'
|
|
105
|
+
severity?: 'error' | 'warning' | 'info' // always set by the built-in codec, see below
|
|
106
|
+
}
|
|
93
107
|
```
|
|
94
108
|
|
|
95
109
|
## Key concepts
|
|
@@ -107,17 +121,48 @@ declaration plays four parts at once:
|
|
|
107
121
|
yields exact dotted field paths (`options[3].kind`) for binding errors to UI inputs.
|
|
108
122
|
3. **Input validation** — `encode` validates each input with Ajv against the schema. `useDefaults`
|
|
109
123
|
makes the schema double as a packet template (omitted fields are filled in); `coerceTypes` tolerates
|
|
110
|
-
form-string input.
|
|
124
|
+
form-string input. A string field's `contentEncoding` implies its format: `ipv4`/`ipv6`/`mac` fields
|
|
125
|
+
carry `format: 'ipv4'|'ipv6'|'mac'` and `bigint` fields a numeric `pattern`, in the published schema
|
|
126
|
+
and in the validator alike, so `'abcdefg'` is rejected as an IPv4 address.
|
|
111
127
|
4. **UI form metadata** — custom keywords (`label`, `hidden`, `contentEncoding`) plus `enum`/`min`/`max`
|
|
112
|
-
and `anyOf` + `const` discriminators describe how a form should render each field.
|
|
128
|
+
and `anyOf` + `const` discriminators describe how a form should render each field. Three optional
|
|
129
|
+
annotations tell an editor how `encode` treats a field (annotation only — Ajv ignores them):
|
|
130
|
+
- `readOnly: true` — encode ignores the value, so editing it never changes the bytes (display-only
|
|
131
|
+
projections such as HTTP `method`/`statusCode` or CMS `serviceDataDecoded`, decoded from a
|
|
132
|
+
verbatim field that is the real encode authority; also values encode always derives itself). On an
|
|
133
|
+
object/array it covers every descendant.
|
|
134
|
+
- `autoCalculated: true` — encode computes the value (length, checksum, count, offset, or the demux
|
|
135
|
+
field inferred from the next layer) when it is left out; any value given, 0 included, is written
|
|
136
|
+
as given. E.g. `ipv4.length`, `ipv4.checksum`, `tcp.hdrLen`, `ipv4.protocol`.
|
|
137
|
+
- `byteAlignment: N` — encode zero-pads the field to a multiple of N bytes, e.g. `ipv4.options`
|
|
138
|
+
(4) and `tcp.options` (4).
|
|
113
139
|
|
|
114
140
|
### Decode never fails; errors accumulate instead of throwing
|
|
115
141
|
|
|
116
142
|
Malformed packets are first-class input. A field whose bytes are truncated or invalid records an error
|
|
117
|
-
via `recordError()` (a `{id, path, message}` entry on the layer's `errors`) and clamps to a
|
|
118
|
-
value — it does not throw. Decode therefore always returns a full best-effort layer list plus
|
|
119
|
-
field-path-addressed error list you can use to highlight problems in a UI.
|
|
120
|
-
|
|
143
|
+
via `recordError()` (a `{id, path, message, severity}` entry on the layer's `errors`) and clamps to a
|
|
144
|
+
best-effort value — it does not throw. Decode therefore always returns a full best-effort layer list plus
|
|
145
|
+
a field-path-addressed error list you can use to highlight problems in a UI. Whatever decode returns is
|
|
146
|
+
accepted by `encode` (a header its fields could not reproduce — IPv4 with IHL 0, a header cut short — is
|
|
147
|
+
left to `raw` instead), so read → edit → re-emit never dead-ends.
|
|
148
|
+
|
|
149
|
+
`severity` grades each entry: every decode error is `error`; on encode, a field left out that fell back to
|
|
150
|
+
its default (`'Not Found'`) is `info`, a value encode adjusted or flagged but still wrote (clamped to its
|
|
151
|
+
range, IPv4 version ≠ 4, a checksum with no IP layer to compute it from) is `warning`, and a value that
|
|
152
|
+
could not be encoded as given (an internal failure, an unknown name such as a TLS alert level) is `error`.
|
|
153
|
+
|
|
154
|
+
The only deliberate fast-fail is the Ajv shape check at the `encode` entry point. It throws a
|
|
155
|
+
`CodecSchemaValidateError` that names the input: `layerId` and `layerIndex` (its position in the `encode`
|
|
156
|
+
array), `path` (same dotted form as `errors[].path`) and `errors` (Ajv's error objects); `message` is
|
|
157
|
+
Ajv's message for the most specific failure.
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
try {
|
|
161
|
+
await codec.encode([{id: 'eth', data: {}}, {id: 'ipv4', data: {sip: 'abc'}}])
|
|
162
|
+
} catch (e) {
|
|
163
|
+
if (e instanceof CodecSchemaValidateError) e.layerId + '.' + e.path // 'ipv4.sip'
|
|
164
|
+
}
|
|
165
|
+
```
|
|
121
166
|
|
|
122
167
|
### RawData is the catch-all
|
|
123
168
|
|
|
@@ -157,6 +202,13 @@ error/ok severity (a Wireshark-style hex-to-field view); `summary(decoded)` rend
|
|
|
157
202
|
description (Wireshark's Info column); `allowedNextLayers`, `childDiscriminator` and `checkConsistency`
|
|
158
203
|
describe and validate which layer may follow which.
|
|
159
204
|
|
|
205
|
+
`allowedNextLayers(parent)` lists each child with the `{field, value}` to set on the parent so decode
|
|
206
|
+
routes to it. Children decode recognizes by their own bytes (LLC after an 802.3 frame, COTP after TPKT,
|
|
207
|
+
S7comm / ISO-Session / RDP after COTP, MMS after ISO-Session, RTPS, BitTorrent) are listed too — a header
|
|
208
|
+
declares them with `heuristicParents` — usually with a `null` discriminator (nothing to set), or with the
|
|
209
|
+
parent value they need (RDP: COTP `pduType` 0xE0). Their signature fields default to the protocol
|
|
210
|
+
constants, so a layer added from scratch re-opens as that layer.
|
|
211
|
+
|
|
160
212
|
## Built-in headers
|
|
161
213
|
|
|
162
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.
|
package/README.zh-CN.md
CHANGED
|
@@ -58,12 +58,17 @@ rebuilt.equals(packet) // 对于结构完好的报文为 true —— 解码/
|
|
|
58
58
|
```
|
|
59
59
|
|
|
60
60
|
想从零构造一个报文,只要按从外到内的顺序给 `encode` 传一组 `{id, data}` 输入即可;没填的字段会用
|
|
61
|
-
schema
|
|
61
|
+
schema 里的默认值补齐,表单传来的字符串也会被自动转换成对应类型。没填的长度、计数和校验和会自动计算;
|
|
62
|
+
上一层用来区分下一层的字段如果没填(`eth`/`vlan`/`hsr`/`gre`/`geneve`/`snap` 的 EtherType、IPv4 的
|
|
63
|
+
`protocol`、IPv6 的 `nxt`),会按后面那一层自动填写(eth 后面接 LLC 时填 IEEE 802.3 长度)。显式填写的值
|
|
64
|
+
(包括 0)总是原样写出,所以可以有意构造错误的校验和或长度。想让某个字段自动计算,就不要传它:整数字段传
|
|
65
|
+
`null` 会被转换成 0 并照写,传 `''` 会校验失败;十六进制字段传 `''` 或 `null` 则等同于不填。`encode` 在
|
|
66
|
+
副本上工作,不会修改传入的对象:
|
|
62
67
|
|
|
63
68
|
```ts
|
|
64
69
|
const {packet} = await codec.encode([
|
|
65
|
-
{id: 'eth', data: {dmac: 'ff:ff:ff:ff:ff:ff', smac: '00:11:22:33:44:55', etherType
|
|
66
|
-
{id: 'ipv4', data: {sip: '192.168.0.1', dip: '192.168.0.2', protocol
|
|
70
|
+
{id: 'eth', data: {dmac: 'ff:ff:ff:ff:ff:ff', smac: '00:11:22:33:44:55'}}, // etherType → '0800'
|
|
71
|
+
{id: 'ipv4', data: {sip: '192.168.0.1', dip: '192.168.0.2'}}, // protocol → 17
|
|
67
72
|
{id: 'udp', data: {srcport: 12345, dstport: 53}}
|
|
68
73
|
])
|
|
69
74
|
```
|
|
@@ -82,12 +87,19 @@ type CodecDecodeResult = {
|
|
|
82
87
|
name: string // 可读名称,如 'Ethernet II'
|
|
83
88
|
nickname: string // 简称,如 'ETH'
|
|
84
89
|
protocol: boolean // 该层是否为真正的协议(原始载荷为 false)
|
|
85
|
-
errors: CodecErrorInfo[] // {id, path, message}[] —— 用字段路径定位的解码错误
|
|
90
|
+
errors: CodecErrorInfo[] // {id, path, message, severity}[] —— 用字段路径定位的解码错误
|
|
86
91
|
data: HeaderTreeNode // 解码出来的字段树
|
|
87
92
|
}
|
|
88
93
|
|
|
89
94
|
type CodecEncodeInput = Pick<CodecDecodeResult, 'id' | 'data'> & Partial<Omit<CodecDecodeResult, 'id' | 'data'>>
|
|
90
95
|
type CodecEncodeResult = {packet: Buffer, errors: CodecErrorInfo[]}
|
|
96
|
+
|
|
97
|
+
type CodecErrorInfo = {
|
|
98
|
+
id: string // 协议层 id
|
|
99
|
+
path: string // 层内的点分字段路径,如 'flags.df'、'options[0].kind'
|
|
100
|
+
message: string // 如 'Not Found'、'IPv4 version should be 4'
|
|
101
|
+
severity?: 'error' | 'warning' | 'info' // 内置编解码器总会填写,含义见下文
|
|
102
|
+
}
|
|
91
103
|
```
|
|
92
104
|
|
|
93
105
|
## 关键概念
|
|
@@ -104,16 +116,44 @@ type CodecEncodeResult = {packet: Buffer, errors: CodecErrorInfo[]}
|
|
|
104
116
|
路径的代理对象,深层访问永远不会抛异常,并能给出精确的点分字段路径(如 `options[3].kind`),用来把
|
|
105
117
|
错误绑定到界面上对应的输入框。
|
|
106
118
|
3. **输入校验**——`encode` 会用 Ajv 按 schema 校验每一份输入。`useDefaults` 让 schema 同时充当报文
|
|
107
|
-
模板(缺失字段自动补齐);`coerceTypes`
|
|
119
|
+
模板(缺失字段自动补齐);`coerceTypes` 则容忍来自表单的字符串输入。字符串字段的 `contentEncoding`
|
|
120
|
+
同时决定它的格式约束:`ipv4`/`ipv6`/`mac` 字段带 `format: 'ipv4'|'ipv6'|'mac'`,`bigint` 字段带数字
|
|
121
|
+
`pattern`,公开的 schema 和编码校验器里都一样,所以 `'abcdefg'` 不会再被当成 IPv4 地址接受。
|
|
108
122
|
4. **界面表单元数据**——自定义关键字(`label`、`hidden`、`contentEncoding`),加上 `enum`/`min`/`max`
|
|
109
|
-
以及 `anyOf` + `const`
|
|
123
|
+
以及 `anyOf` + `const` 判别式,共同描述表单该如何渲染每个字段。另有三个可选注解告诉编辑器 `encode`
|
|
124
|
+
如何对待某个字段(仅为注解,Ajv 不据此校验):
|
|
125
|
+
- `readOnly: true`——encode 忽略该值,修改它永远不会改变编码字节(例如 HTTP `method`/`statusCode`、
|
|
126
|
+
CMS `serviceDataDecoded` 这类从逐字节保留的字段解出、仅供展示的投影——真正的编码依据是那个逐字节
|
|
127
|
+
字段;以及 encode 总是自行推导的值)。标在对象/数组上时覆盖其全部子孙字段。
|
|
128
|
+
- `autoCalculated: true`——输入缺省该字段时,encode 会自动计算(长度、校验和、计数、偏移,或按下一层
|
|
129
|
+
推断的分发字段);给出的任何值(包括 0)都按原样写入。例如 `ipv4.length`、`ipv4.checksum`、
|
|
130
|
+
`tcp.hdrLen`、`ipv4.protocol`。
|
|
131
|
+
- `byteAlignment: N`——encode 会用 0 把该字段补齐到 N 字节的整数倍,例如 `ipv4.options`(4)、
|
|
132
|
+
`tcp.options`(4)。
|
|
110
133
|
|
|
111
134
|
### 解码永不失败,错误只累积不抛出
|
|
112
135
|
|
|
113
136
|
畸形报文本身就是合法输入。某个字段的字节被截断或取值非法时,会通过 `recordError()` 记录一条错误(即该
|
|
114
|
-
层 `errors` 上的一条 `{id, path, message}`)
|
|
115
|
-
|
|
116
|
-
|
|
137
|
+
层 `errors` 上的一条 `{id, path, message, severity}`),并把值收敛到一个尽力而为的结果——而不是抛异常。
|
|
138
|
+
因此解码总能返回一份完整的、尽力而为的分层结果,外加一份用字段路径定位的错误清单,供界面高亮问题。解码
|
|
139
|
+
返回的任何结果 `encode` 都能接受(字段无法复原的协议头,比如 IHL 为 0 的 IPv4、被截断的头部,会直接留给
|
|
140
|
+
`raw`),所以"读取 → 编辑 → 重新写出"不会卡住。
|
|
141
|
+
|
|
142
|
+
`severity` 给每条错误分级:解码错误一律是 `error`;编码时,没填而用了默认值的字段(`'Not Found'`)是
|
|
143
|
+
`info`,编码器调整或标记过、但仍然写出的值(超范围被钳位、IPv4 version 不是 4、没有 IP 层算不了校验和)
|
|
144
|
+
是 `warning`,无法按给定值编码的(内部异常、TLS 告警级别这类不认识的名称)是 `error`。
|
|
145
|
+
|
|
146
|
+
唯一刻意的快速失败,是 `encode` 入口处的 Ajv 形状校验。它抛出的 `CodecSchemaValidateError` 会指明是哪一项
|
|
147
|
+
输入:`layerId` 和 `layerIndex`(在 `encode` 数组里的位置)、`path`(与 `errors[].path` 同样的点分形式),
|
|
148
|
+
以及 `errors`(Ajv 的原始错误对象);`message` 是最具体那一条失败的 Ajv 信息。
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
try {
|
|
152
|
+
await codec.encode([{id: 'eth', data: {}}, {id: 'ipv4', data: {sip: 'abc'}}])
|
|
153
|
+
} catch (e) {
|
|
154
|
+
if (e instanceof CodecSchemaValidateError) e.layerId + '.' + e.path // 'ipv4.sip'
|
|
155
|
+
}
|
|
156
|
+
```
|
|
117
157
|
|
|
118
158
|
### RawData 兜底
|
|
119
159
|
|
|
@@ -147,6 +187,12 @@ decode 处理器按先进先出执行(内层语义依赖外层上下文,例如 T
|
|
|
147
187
|
(类似 Wireshark 的字节到字段视图);`summary(decoded)` 渲染出一行描述(相当于 Wireshark 的 Info 列);
|
|
148
188
|
`allowedNextLayers`、`childDiscriminator` 和 `checkConsistency` 则描述并校验哪一层可以接在哪一层之后。
|
|
149
189
|
|
|
190
|
+
`allowedNextLayers(parent)` 列出每个可接的下一层,以及要在上一层设置的 `{field, value}`,解码才会走到它。
|
|
191
|
+
靠自身内容识别的协议(802.3 帧后的 LLC、TPKT 后的 COTP、COTP 后的 S7comm / ISO-Session / RDP、
|
|
192
|
+
ISO-Session 后的 MMS、RTPS、BitTorrent)也会列出——协议头用 `heuristicParents` 声明——通常区分字段为
|
|
193
|
+
`null`(无需设置),或给出它需要的上一层取值(RDP 需要 COTP `pduType` 为 0xE0)。这些协议的签名字段默认就是
|
|
194
|
+
协议常量,所以从零添加的一层重新打开时仍是这一层。
|
|
195
|
+
|
|
150
196
|
## 内置协议头
|
|
151
197
|
|
|
152
198
|
默认注册了 189 个协议头(`raw` 是强制兜底)。向 `new Codec(customCodecs)` 传入自定义类可覆盖同 id 的内置头或新增协议头。
|
package/dist/Codec.d.ts
CHANGED
|
@@ -88,8 +88,9 @@ export declare class Codec {
|
|
|
88
88
|
/**
|
|
89
89
|
* Editor helper (read-only projection over the same demux graph decode uses): the layers that
|
|
90
90
|
* may follow `parentLayerId`, each tagged with the {field,value} to set on the parent so decode
|
|
91
|
-
* would route to it. Derived by reversing the dispatch table over the parent's producer fields
|
|
92
|
-
*
|
|
91
|
+
* would route to it. Derived by reversing the dispatch table over the parent's producer fields,
|
|
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
94
|
* @param parentLayerId
|
|
94
95
|
*/
|
|
95
96
|
allowedNextLayers(parentLayerId: string): NextLayer[];
|