@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
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trailer = void 0;
4
+ const BaseHeader_1 = require("../abstracts/BaseHeader");
5
+ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
6
+ const BufferToHex_1 = require("../helper/BufferToHex");
7
+ const HexToBuffer_1 = require("../helper/HexToBuffer");
8
+ /**
9
+ * The bytes a frame carries past the end its own length fields give: Ethernet padding up to the
10
+ * 60-octet minimum frame, or a trailer a device appended. Decode puts them here when an IPv4 Total
11
+ * Length, an IPv6 Payload Length or an 802.3 length ends before the captured frame does (see
12
+ * BaseHeader.payloadEnd), so the layers above see only their own payload. The bytes are kept verbatim
13
+ * as `data` hex.
14
+ *
15
+ * Encode writes nothing in layer order: the codec calls appendToPacket() once every packet-level
16
+ * post-encode handler has run, so no computed length or checksum counts these bytes. Trailer layers
17
+ * therefore always land at the end of the packet, in input order. Not a protocol
18
+ * (`isProtocol = false`) and never chosen by decode dispatch (match() is always false): the codec
19
+ * places it.
20
+ */
21
+ class Trailer extends BaseHeader_1.BaseHeader {
22
+ constructor() {
23
+ super(...arguments);
24
+ this.SCHEMA = {
25
+ type: 'object',
26
+ properties: {
27
+ data: {
28
+ type: 'string',
29
+ label: 'Trailer',
30
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
31
+ decode: () => {
32
+ this.instance.data.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(0, this.packet.length - this.startPos)));
33
+ },
34
+ encode: () => {
35
+ //Written later by appendToPacket(), after the lengths and checksums are computed.
36
+ }
37
+ }
38
+ }
39
+ };
40
+ this.id = 'trailer';
41
+ this.name = 'Trailer';
42
+ this.nickname = 'Trailer';
43
+ this.isProtocol = false;
44
+ }
45
+ match() {
46
+ return false;
47
+ }
48
+ /**
49
+ * Write the trailer bytes at the current end of the packet. The codec calls this after every
50
+ * packet-level post-encode handler, so none of them counted the bytes.
51
+ * @param followsLayers true when the input placed other layers after this one: their bytes now
52
+ * come first, which is reported as a warning.
53
+ */
54
+ appendToPacket(followsLayers) {
55
+ if (followsLayers)
56
+ this.recordError(this.instance.data.getPath(), 'A trailer is written after every other layer, so the layers given after it come before it in the packet', 'warning');
57
+ this.startPos = this.codecData.packetLength ?? this.codecData.packet.length;
58
+ const data = this.instance.data.getValue('').toString();
59
+ if (data)
60
+ this.writeBytes(0, (0, HexToBuffer_1.HexToBuffer)(data));
61
+ }
62
+ }
63
+ exports.Trailer = Trailer;
@@ -20,5 +20,6 @@ export declare class VLAN_802dot1Q extends BaseHeader {
20
20
  readonly demuxProducers: DemuxProducer[];
21
21
  readonly name: string;
22
22
  readonly nickname: string;
23
+ payloadEnd(): number | undefined;
23
24
  match(): boolean;
24
25
  }
@@ -92,6 +92,9 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
92
92
  this.name = '802.1Q Virtual LAN';
93
93
  this.nickname = 'VLAN';
94
94
  }
95
+ payloadEnd() {
96
+ return this.ieee802dot3PayloadEnd(this.instance.etherType);
97
+ }
95
98
  match() {
96
99
  if (!this.prevCodecModule)
97
100
  return false;
@@ -43,7 +43,8 @@ class WakeOnLAN extends BaseHeader_1.BaseHeader {
43
43
  properties: {
44
44
  //The 6-byte synchronization stream. In a well-formed magic packet it is all ones
45
45
  //(0xFF ×6); kept verbatim as hex so a crafted stream still round-trips byte-for-byte.
46
- syncStream: this.fieldHex('syncStream', 0, 6, 'Sync Stream'),
46
+ //Default all ones: what match() requires.
47
+ syncStream: { ...this.fieldHex('syncStream', 0, 6, 'Sync Stream'), default: 'ff'.repeat(6) },
47
48
  //The target adapter's MAC address, decoded from the first of the 16 identical repetitions
48
49
  //(offset 6). On encode it is written back 16 times over offsets 6..101 (96 bytes) — the
49
50
  //defining property of a magic packet, so a well-formed packet reproduces exactly.
@@ -141,8 +141,9 @@ class WireGuard extends BaseHeader_1.BaseHeader {
141
141
  type: 'object',
142
142
  summary: 'WireGuard type=${messageType}',
143
143
  properties: {
144
- //Message Type first: every other field's presence/offset is branched on it.
145
- messageType: this.fieldUInt('messageType', 0, 1, 'Message Type'),
144
+ //Message Type first: every other field's presence/offset is branched on it. Default 1
145
+ //(Handshake Initiation): match() requires a type in 1..4.
146
+ messageType: { ...this.fieldUInt('messageType', 0, 1, 'Message Type'), default: 1 },
146
147
  //3 Reserved bytes, spec-fixed at zero. Kept as a field (not asserted) so it is visible/editable;
147
148
  //match() requires them zero as part of the content signature, so a non-zero-reserved datagram
148
149
  //is not claimed as WireGuard (it falls to raw) rather than round-tripping through this codec.
@@ -77,8 +77,8 @@ class Zabbix extends BaseHeader_1.BaseHeader {
77
77
  summary: 'Zabbix flags=${flags} len=${dataLength}',
78
78
  properties: {
79
79
  //The 4-byte protocol Magic — always "ZBXD" (0x5A425844) on the wire; kept verbatim so any
80
- //non-standard magic still round-trips.
81
- magic: this.fieldHex('magic', 0, 4, 'Magic'),
80
+ //non-standard magic still round-trips. Default "ZBXD": what match() requires.
81
+ magic: { ...this.fieldHex('magic', 0, 4, 'Magic'), default: '5a425844' },
82
82
  //Flags bitfield: 0x01 ZBX_TCP_PROTOCOL (uncompressed), 0x02 ZBX_TCP_COMPRESS, 0x04
83
83
  //ZBX_TCP_LARGE. Kept as a plain uint8 (byte-perfect and editable); the bit split is UI
84
84
  //enrichment for later.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * BER (X.690) TLV elements as they appear on the wire, for headers that need the exact layout rather
3
+ * than a re-encoded view: node-tlv rebuilds every length field in minimal form, so a long-form length
4
+ * (82 00b2 where 81 b2 would do) loses its octets and the span computed from it comes out short.
5
+ */
6
+ export type BERElement = {
7
+ tag: number;
8
+ constructed: boolean;
9
+ lengthOctets: number;
10
+ value: Buffer;
11
+ end: number;
12
+ truncated: boolean;
13
+ };
14
+ /**
15
+ * Read the BER element at `offset` of `buffer`. Undefined when its tag and length do not fit in the
16
+ * buffer, or the length uses the indefinite form or more than 4 octets.
17
+ * @param buffer
18
+ * @param offset
19
+ * @constructor
20
+ */
21
+ export declare function ReadBERElement(buffer: Buffer, offset: number): BERElement | undefined;
22
+ /**
23
+ * Read the consecutive BER elements filling `buffer`, up to the first one that cannot be read
24
+ * @param buffer
25
+ * @constructor
26
+ */
27
+ export declare function ReadBERElements(buffer: Buffer): BERElement[];
28
+ /**
29
+ * Why the BER elements filling `buffer` are not well-formed, or undefined when they are: every
30
+ * element, and every element inside a constructed one, is readable and ends inside its parent, and
31
+ * the elements fill each parent exactly.
32
+ * @param buffer
33
+ * @param base offset of `buffer` in the region reported on
34
+ * @constructor
35
+ */
36
+ export declare function MalformedBER(buffer: Buffer, base?: number): string | undefined;
37
+ /**
38
+ * Octets of the minimal BER length field for `length` content octets
39
+ * @param length
40
+ * @constructor
41
+ */
42
+ export declare function MinimalBERLengthOctets(length: number): number;
43
+ /**
44
+ * Encode a BER element. Its length field takes `lengthOctets` octets when that form can hold the
45
+ * length, else the minimal form.
46
+ * @param tag
47
+ * @param value
48
+ * @param lengthOctets
49
+ * @constructor
50
+ */
51
+ export declare function EncodeBERElement(tag: number, value: Buffer, lengthOctets?: number): Buffer;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReadBERElement = ReadBERElement;
4
+ exports.ReadBERElements = ReadBERElements;
5
+ exports.MalformedBER = MalformedBER;
6
+ exports.MinimalBERLengthOctets = MinimalBERLengthOctets;
7
+ exports.EncodeBERElement = EncodeBERElement;
8
+ /**
9
+ * Read the BER element at `offset` of `buffer`. Undefined when its tag and length do not fit in the
10
+ * buffer, or the length uses the indefinite form or more than 4 octets.
11
+ * @param buffer
12
+ * @param offset
13
+ * @constructor
14
+ */
15
+ function ReadBERElement(buffer, offset) {
16
+ let position = offset;
17
+ if (position >= buffer.length)
18
+ return undefined;
19
+ let tag = buffer[position++];
20
+ const constructed = !!(tag & 0x20);
21
+ if ((tag & 0x1f) === 0x1f) {
22
+ let more = true;
23
+ while (more) {
24
+ if (position >= buffer.length || position - offset > 3)
25
+ return undefined;
26
+ more = !!(buffer[position] & 0x80);
27
+ tag = tag * 256 + buffer[position++];
28
+ }
29
+ }
30
+ if (position >= buffer.length)
31
+ return undefined;
32
+ let length = buffer[position++];
33
+ let lengthOctets = 1;
34
+ if (length & 0x80) {
35
+ const count = length & 0x7f;
36
+ if (count === 0 || count > 4 || position + count > buffer.length)
37
+ return undefined;
38
+ length = 0;
39
+ for (let i = 0; i < count; i++)
40
+ length = length * 256 + buffer[position++];
41
+ lengthOctets = 1 + count;
42
+ }
43
+ const end = Math.min(position + length, buffer.length);
44
+ return { tag: tag, constructed: constructed, lengthOctets: lengthOctets, value: buffer.subarray(position, end), end: end, truncated: position + length > buffer.length };
45
+ }
46
+ /**
47
+ * Read the consecutive BER elements filling `buffer`, up to the first one that cannot be read
48
+ * @param buffer
49
+ * @constructor
50
+ */
51
+ function ReadBERElements(buffer) {
52
+ const elements = [];
53
+ let offset = 0;
54
+ while (offset < buffer.length) {
55
+ const element = ReadBERElement(buffer, offset);
56
+ if (!element)
57
+ break;
58
+ elements.push(element);
59
+ offset = element.end;
60
+ }
61
+ return elements;
62
+ }
63
+ /**
64
+ * Why the BER elements filling `buffer` are not well-formed, or undefined when they are: every
65
+ * element, and every element inside a constructed one, is readable and ends inside its parent, and
66
+ * the elements fill each parent exactly.
67
+ * @param buffer
68
+ * @param base offset of `buffer` in the region reported on
69
+ * @constructor
70
+ */
71
+ function MalformedBER(buffer, base = 0) {
72
+ let offset = 0;
73
+ while (offset < buffer.length) {
74
+ const element = ReadBERElement(buffer, offset);
75
+ if (!element)
76
+ return `no BER element can be read at offset ${base + offset}`;
77
+ if (element.truncated)
78
+ return `the BER element at offset ${base + offset} runs past the octets available`;
79
+ if (element.constructed) {
80
+ const reason = MalformedBER(element.value, base + element.end - element.value.length);
81
+ if (reason)
82
+ return reason;
83
+ }
84
+ offset = element.end;
85
+ }
86
+ return undefined;
87
+ }
88
+ /**
89
+ * Octets of the minimal BER length field for `length` content octets
90
+ * @param length
91
+ * @constructor
92
+ */
93
+ function MinimalBERLengthOctets(length) {
94
+ let octets = 1;
95
+ if (length < 0x80)
96
+ return octets;
97
+ for (let remaining = length; remaining > 0; remaining = Math.floor(remaining / 256))
98
+ octets++;
99
+ return octets;
100
+ }
101
+ /**
102
+ * Encode a BER element. Its length field takes `lengthOctets` octets when that form can hold the
103
+ * length, else the minimal form.
104
+ * @param tag
105
+ * @param value
106
+ * @param lengthOctets
107
+ * @constructor
108
+ */
109
+ function EncodeBERElement(tag, value, lengthOctets) {
110
+ const tagOctets = [];
111
+ for (let remaining = tag; !tagOctets.length || remaining > 0; remaining = Math.floor(remaining / 256))
112
+ tagOctets.unshift(remaining % 256);
113
+ const minimal = MinimalBERLengthOctets(value.length);
114
+ const octets = lengthOctets !== undefined && Number.isInteger(lengthOctets) && lengthOctets >= minimal && lengthOctets <= 5 ? lengthOctets : minimal;
115
+ const lengthField = Buffer.alloc(octets);
116
+ if (octets === 1) {
117
+ lengthField[0] = value.length;
118
+ }
119
+ else {
120
+ lengthField[0] = 0x80 | (octets - 1);
121
+ for (let i = octets - 1, remaining = value.length; i > 0; i--, remaining = Math.floor(remaining / 256))
122
+ lengthField[i] = remaining % 256;
123
+ }
124
+ return Buffer.concat([Buffer.from(tagOctets), lengthField, value]);
125
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netkitty/codec",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Schema-driven packet codec: encode/decode 180+ network and industrial (OT/ICS/SCADA) protocol headers — TCP/IP, TLS, HTTP/2, DNS, MQTT, plus Modbus, DNP3, IEC 61850 (GOOSE/SV/MMS), IEC 104, S7comm, OPC UA, PROFINET, EtherCAT, BACnet and more.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -138,5 +138,5 @@
138
138
  },
139
139
  "homepage": "https://github.com/myq1991/netkitty/tree/main/packages/codec#readme",
140
140
  "bugs": "https://github.com/myq1991/netkitty/issues",
141
- "gitHead": "c392c19e0fbf6de6d2ce5213ea775c6033ff93cd"
141
+ "gitHead": "698bce9a3e8414a2a7ef2af687c4feff0aa1a25c"
142
142
  }