@netkitty/codec 2.0.0 → 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 +136 -0
- package/README.md +10 -6
- package/README.zh-CN.md +9 -6
- package/dist/Codec.js +4 -1
- package/dist/abstracts/BaseHeader.d.ts +26 -0
- package/dist/abstracts/BaseHeader.js +53 -0
- package/dist/headers/AH.js +3 -2
- package/dist/headers/BFD.d.ts +2 -1
- package/dist/headers/BFD.js +30 -4
- package/dist/headers/BGP.js +5 -1
- package/dist/headers/C37118.d.ts +3 -2
- package/dist/headers/C37118.js +34 -15
- package/dist/headers/CDP.d.ts +3 -3
- package/dist/headers/CDP.js +46 -10
- package/dist/headers/CMS.js +25 -10
- package/dist/headers/COTP.js +2 -3
- package/dist/headers/CoAP.js +20 -2
- package/dist/headers/DCCP.d.ts +3 -2
- package/dist/headers/DCCP.js +60 -8
- package/dist/headers/DNP3.d.ts +5 -3
- package/dist/headers/DNP3.js +62 -7
- package/dist/headers/EIGRP.d.ts +3 -2
- package/dist/headers/EIGRP.js +31 -5
- package/dist/headers/EthernetII.js +1 -0
- package/dist/headers/GELF.js +1 -0
- package/dist/headers/GENEVE.js +1 -0
- package/dist/headers/GRE.js +1 -0
- package/dist/headers/GTPv2C.js +1 -0
- package/dist/headers/Goose.js +4 -4
- package/dist/headers/HSR.js +1 -0
- package/dist/headers/HTTP2.js +1 -0
- package/dist/headers/ICMP.js +2 -1
- package/dist/headers/ICMPv6.js +10 -16
- package/dist/headers/IEC104_I_Frame.js +189 -160
- package/dist/headers/IEC104_S_Frame.js +19 -1
- package/dist/headers/IEC104_U_Frame.js +19 -1
- package/dist/headers/IEC61850SampledValues.js +4 -4
- package/dist/headers/IGMP.d.ts +8 -7
- package/dist/headers/IGMP.js +57 -19
- package/dist/headers/IPFIX.js +1 -1
- package/dist/headers/IPv4.js +26 -23
- package/dist/headers/IPv6.js +4 -1
- package/dist/headers/IPv6HopByHopOptions.js +3 -1
- package/dist/headers/ISAKMP.js +1 -0
- package/dist/headers/Kerberos.js +1 -0
- package/dist/headers/L2TP.js +1 -0
- package/dist/headers/LDAP.js +1 -1
- package/dist/headers/LLC.js +1 -0
- package/dist/headers/NHRP.d.ts +1 -1
- package/dist/headers/NHRP.js +29 -4
- package/dist/headers/OSPF.d.ts +3 -3
- package/dist/headers/OSPF.js +72 -7
- package/dist/headers/PIM.d.ts +3 -3
- package/dist/headers/PIM.js +55 -6
- package/dist/headers/PPPoEDiscovery.js +2 -0
- package/dist/headers/PTP.js +5 -1
- package/dist/headers/RDP.js +2 -0
- package/dist/headers/RGoose.js +2 -0
- package/dist/headers/RSVP.d.ts +3 -3
- package/dist/headers/RSVP.js +56 -42
- package/dist/headers/RTCP.js +1 -0
- package/dist/headers/SCTP.d.ts +2 -2
- package/dist/headers/SCTP.js +29 -5
- package/dist/headers/SNAP.js +2 -2
- package/dist/headers/SNMP.js +2 -2
- package/dist/headers/STUN.js +1 -1
- package/dist/headers/SercosIII.d.ts +3 -2
- package/dist/headers/SercosIII.js +41 -5
- package/dist/headers/TCP.js +13 -58
- package/dist/headers/TLS_Alert.js +20 -2
- package/dist/headers/TLS_ApplicationData.d.ts +3 -3
- package/dist/headers/TLS_ApplicationData.js +25 -6
- package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
- package/dist/headers/TLS_ChangeCipherSpec.js +24 -4
- package/dist/headers/TLS_Handshake.js +39 -6
- package/dist/headers/TLS_Heartbeat.d.ts +4 -3
- package/dist/headers/TLS_Heartbeat.js +40 -7
- package/dist/headers/TPKT.js +1 -1
- package/dist/headers/UDP.js +17 -61
- package/dist/headers/VLAN_802dot1Q.js +1 -0
- package/dist/headers/VRRP.d.ts +2 -1
- package/dist/headers/VRRP.js +45 -9
- 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/package.json +2 -2
package/dist/headers/CDP.js
CHANGED
|
@@ -6,6 +6,7 @@ const BufferToHex_1 = require("../helper/BufferToHex");
|
|
|
6
6
|
const HexToBuffer_1 = require("../helper/HexToBuffer");
|
|
7
7
|
const BufferToNumber_1 = require("../helper/BufferToNumber");
|
|
8
8
|
const NumberToBuffer_1 = require("../helper/NumberToBuffer");
|
|
9
|
+
const Checksum_1 = require("../helper/Checksum");
|
|
9
10
|
const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
10
11
|
/**
|
|
11
12
|
* CDP — Cisco Discovery Protocol, carried in an 802.3 frame over LLC/SNAP with OUI 0x00000C and PID
|
|
@@ -15,9 +16,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
|
15
16
|
* Capabilities, 0x0005 Software Version, 0x0006 Platform.
|
|
16
17
|
*
|
|
17
18
|
* The TLV values are kept verbatim as hex (device names, address lists and capability bitmaps need no
|
|
18
|
-
* form structure), the
|
|
19
|
-
* the
|
|
20
|
-
* byte-for-byte.
|
|
19
|
+
* form structure), and the TLV walk is bounded by the captured bytes so a corrupt Length can't run past
|
|
20
|
+
* the frame. The Checksum and each TLV Length are computed on encode when left out and written as given
|
|
21
|
+
* otherwise. A well-formed CDP frame round-trips byte-for-byte.
|
|
21
22
|
*/
|
|
22
23
|
class CDP extends BaseHeader_1.BaseHeader {
|
|
23
24
|
constructor() {
|
|
@@ -33,6 +34,21 @@ class CDP extends BaseHeader_1.BaseHeader {
|
|
|
33
34
|
get SCHEMA() {
|
|
34
35
|
return (CDP.#schemaCache ??= CDP.#buildSchema());
|
|
35
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The CDP checksum: the Internet checksum of the whole CDP message, except for an odd trailing
|
|
39
|
+
* octet. RFC 1071 pads it with a zero octet (word 0xXX00); Cisco adds it as a signed low byte
|
|
40
|
+
* instead, which comes out as the word 0x00XX below 0x80 and 0xFF(XX-1) from 0x80 on. This is what
|
|
41
|
+
* Wireshark's CDP dissector checks, and real Cisco captures carry it.
|
|
42
|
+
* @param message the CDP message with the checksum field zeroed
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
static #checksum(message) {
|
|
46
|
+
if (message.length % 2 === 0)
|
|
47
|
+
return (0, Checksum_1.InternetChecksum)(message);
|
|
48
|
+
const last = message[message.length - 1];
|
|
49
|
+
const finalWord = (last & 0x80) ? Buffer.from([0xFF, last - 1]) : Buffer.from([0x00, last]);
|
|
50
|
+
return (0, Checksum_1.InternetChecksum)(message.subarray(0, message.length - 1), finalWord);
|
|
51
|
+
}
|
|
36
52
|
static #buildSchema() {
|
|
37
53
|
return {
|
|
38
54
|
type: 'object',
|
|
@@ -40,9 +56,29 @@ class CDP extends BaseHeader_1.BaseHeader {
|
|
|
40
56
|
properties: {
|
|
41
57
|
version: this.fieldUInt('version', 0, 1, 'Version'),
|
|
42
58
|
ttl: this.fieldUInt('ttl', 1, 1, 'TTL'),
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
59
|
+
//Left out, computed once every TLV is written (#checksum); a given value is written as given.
|
|
60
|
+
checksum: {
|
|
61
|
+
type: 'string',
|
|
62
|
+
autoCalculated: true,
|
|
63
|
+
label: 'Checksum',
|
|
64
|
+
contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
|
65
|
+
decode: function () {
|
|
66
|
+
this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2)));
|
|
67
|
+
},
|
|
68
|
+
encode: function () {
|
|
69
|
+
if (!this.isLeftOut(this.instance.checksum)) {
|
|
70
|
+
this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.checksum.getValue('0000')));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
this.instance.checksum.setValue('0000');
|
|
74
|
+
this.writeBytes(2, Buffer.alloc(2, 0));
|
|
75
|
+
this.addPostSelfEncodeHandler(() => {
|
|
76
|
+
const checksum = (0, NumberToBuffer_1.UInt16ToBuffer)(CDP.#checksum(this.packet.subarray(this.startPos, this.endPos)));
|
|
77
|
+
this.instance.checksum.setValue((0, BufferToHex_1.BufferToHex)(checksum));
|
|
78
|
+
this.writeBytes(2, checksum);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
},
|
|
46
82
|
tlvs: {
|
|
47
83
|
type: 'array',
|
|
48
84
|
label: 'TLVs',
|
|
@@ -87,10 +123,10 @@ class CDP extends BaseHeader_1.BaseHeader {
|
|
|
87
123
|
let offset = 4;
|
|
88
124
|
for (const tlv of tlvs) {
|
|
89
125
|
const valueBuffer = (0, HexToBuffer_1.HexToBuffer)(tlv && tlv.value ? String(tlv.value) : '');
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
126
|
+
//Length written as given, 0 included (a crafted TLV may lie); left out, derived
|
|
127
|
+
//from the 4-byte header + value.
|
|
128
|
+
const lengthLeftOut = !tlv || tlv.length === undefined || tlv.length === null;
|
|
129
|
+
const lengthValue = lengthLeftOut ? 4 + valueBuffer.length : (Number(tlv.length) || 0);
|
|
94
130
|
const type = Number(tlv && tlv.type) || 0;
|
|
95
131
|
this.writeBytes(offset, (0, NumberToBuffer_1.UInt16ToBuffer)(type & 0xffff));
|
|
96
132
|
this.writeBytes(offset + 2, (0, NumberToBuffer_1.UInt16ToBuffer)(lengthValue > 65535 ? 65535 : lengthValue));
|
package/dist/headers/CMS.js
CHANGED
|
@@ -66,9 +66,9 @@ class CMS extends BaseHeader_1.BaseHeader {
|
|
|
66
66
|
err: this.#bit('err', 2, 'Err'),
|
|
67
67
|
//bak: reserved.
|
|
68
68
|
bak: this.#bit('bak', 3, 'bak'),
|
|
69
|
-
//Protocol type PI — the low nibble, 0x01 for DL/T 2811.
|
|
69
|
+
//Protocol type PI — the low nibble, 0x01 for DL/T 2811 (the default, and what match() requires).
|
|
70
70
|
protocolType: {
|
|
71
|
-
type: 'integer', label: 'Protocol Type', minimum: 0, maximum: 15,
|
|
71
|
+
type: 'integer', label: 'Protocol Type', minimum: 0, maximum: 15, default: 1,
|
|
72
72
|
decode: function () {
|
|
73
73
|
this.instance.protocolType.setValue(this.readBits(0, 1, 4, 4));
|
|
74
74
|
},
|
|
@@ -79,20 +79,35 @@ class CMS extends BaseHeader_1.BaseHeader {
|
|
|
79
79
|
//Service Code SC (byte 2) — the service in the data area (Table 1).
|
|
80
80
|
serviceCode: this.fieldUInt('serviceCode', 1, 1, 'Service Code'),
|
|
81
81
|
//Frame Length FL (bytes 3-4, LITTLE-ENDIAN) — the APDU length excluding the 4-byte APCH,
|
|
82
|
-
//i.e. the ASDU length.
|
|
82
|
+
//i.e. the ASDU length. Written as given; left out, it is the ASDU this layer writes
|
|
83
|
+
//(ReqID + service data), known once the whole layer is encoded.
|
|
83
84
|
frameLength: {
|
|
84
|
-
type: 'integer', label: 'Frame Length', minimum: 0, maximum: 65535,
|
|
85
|
+
type: 'integer', autoCalculated: true, label: 'Frame Length', minimum: 0, maximum: 65535,
|
|
85
86
|
decode: function () {
|
|
86
87
|
const b = this.readBytes(2, 2);
|
|
87
88
|
this.instance.frameLength.setValue((b[0] | (b[1] << 8)) & 0xffff);
|
|
88
89
|
},
|
|
89
90
|
encode: function () {
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
value =
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
const write = (value) => this.writeBytes(2, Buffer.from([value & 0xff, (value >> 8) & 0xff]));
|
|
92
|
+
if (!this.isLeftOut(this.instance.frameLength)) {
|
|
93
|
+
let value = this.instance.frameLength.getValue(0);
|
|
94
|
+
if (value > 65535)
|
|
95
|
+
value = 65535;
|
|
96
|
+
if (value < 0)
|
|
97
|
+
value = 0;
|
|
98
|
+
write(value);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
write(0);
|
|
102
|
+
this.addPostSelfEncodeHandler(() => {
|
|
103
|
+
let value = this.length - 4;
|
|
104
|
+
if (value > 65535) {
|
|
105
|
+
this.recordError(this.instance.frameLength.getPath(), 'Maximum value is 65535');
|
|
106
|
+
value = 65535;
|
|
107
|
+
}
|
|
108
|
+
this.instance.frameLength.setValue(value);
|
|
109
|
+
write(value);
|
|
110
|
+
});
|
|
96
111
|
}
|
|
97
112
|
},
|
|
98
113
|
//==== ASDU ====
|
package/dist/headers/COTP.js
CHANGED
|
@@ -62,9 +62,8 @@ class COTP extends BaseHeader_1.BaseHeader {
|
|
|
62
62
|
this.instance.li.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
|
|
63
63
|
},
|
|
64
64
|
encode: function () {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!value) {
|
|
65
|
+
let value = this.instance.li.getValue(0);
|
|
66
|
+
if (this.isLeftOut(this.instance.li)) {
|
|
68
67
|
const dt = COTP.#isDT(this.instance.pduType.getValue(0));
|
|
69
68
|
const headerRest = (0, HexToBuffer_1.HexToBuffer)(this.instance.headerRest.getValue(''));
|
|
70
69
|
//pduType(1) + (DT ? the EOT/TPDU-NR octet(1) : 0) + verbatim header bytes.
|
package/dist/headers/CoAP.js
CHANGED
|
@@ -78,9 +78,27 @@ class CoAP extends BaseHeader_1.BaseHeader {
|
|
|
78
78
|
summary: 'CoAP type=${type} code=${code}',
|
|
79
79
|
properties: {
|
|
80
80
|
//Byte 0 (MSB first): Version (bits 7:6, always 1), Type (bits 5:4), Token Length (bits 3:0).
|
|
81
|
-
version:
|
|
81
|
+
//Default version 1: the only CoAP version (RFC 7252), and what match() requires.
|
|
82
|
+
version: { ...this.#bitField('version', 0, 2, 3, 'Version'), default: 1 },
|
|
82
83
|
type: this.#bitField('type', 2, 2, 3, 'Type'),
|
|
83
|
-
|
|
84
|
+
//Written as given (a crafted TKL may disagree with the token); left out, the token's octet count.
|
|
85
|
+
tokenLength: {
|
|
86
|
+
...this.#bitField('tokenLength', 4, 4, 15, 'Token Length'),
|
|
87
|
+
autoCalculated: true,
|
|
88
|
+
encode: function () {
|
|
89
|
+
if (!this.isLeftOut(this.instance.tokenLength)) {
|
|
90
|
+
this.writeBits(0, 1, 4, 4, this.instance.tokenLength.getValue(0));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let tokenLength = (0, HexToBuffer_1.HexToBuffer)(this.instance.token.getValue('')).length;
|
|
94
|
+
if (tokenLength > 8)
|
|
95
|
+
this.recordError(this.instance.tokenLength.getPath(), 'A CoAP token is at most 8 octets (RFC 7252 section 3)');
|
|
96
|
+
if (tokenLength > 15)
|
|
97
|
+
tokenLength = 15;
|
|
98
|
+
this.instance.tokenLength.setValue(tokenLength);
|
|
99
|
+
this.writeBits(0, 1, 4, 4, tokenLength);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
84
102
|
//Byte 1: Code — a 3-bit class and 5-bit detail packed as one octet (e.g. 0x01 = 0.01 GET).
|
|
85
103
|
code: this.fieldUInt('code', 1, 1, 'Code'),
|
|
86
104
|
//Bytes 2-3: Message ID (big-endian uint16).
|
package/dist/headers/DCCP.d.ts
CHANGED
|
@@ -15,8 +15,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
|
|
|
15
15
|
* the header — Reserved, the Sequence/Ack numbers, per-type fields and options — verbatim as
|
|
16
16
|
* `headerOptions` hex (bytes 9 .. DataOffset*4). Application data after the header is kept as `payload`
|
|
17
17
|
* hex, bounded by the enclosing IP datagram so an inflated Data Offset can't read past the IP payload.
|
|
18
|
-
* The Checksum
|
|
19
|
-
* from the actual bytes. A well-formed datagram round-trips
|
|
18
|
+
* The Checksum and Data Offset are computed on encode when left out (the checksum per RFC 4340 §9, Data
|
|
19
|
+
* Offset from the actual bytes) and written as given otherwise. A well-formed datagram round-trips
|
|
20
|
+
* byte-for-byte.
|
|
20
21
|
*/
|
|
21
22
|
export declare class DCCP extends BaseHeader {
|
|
22
23
|
#private;
|
package/dist/headers/DCCP.js
CHANGED
|
@@ -4,7 +4,9 @@ exports.DCCP = void 0;
|
|
|
4
4
|
const BaseHeader_1 = require("../abstracts/BaseHeader");
|
|
5
5
|
const BufferToHex_1 = require("../helper/BufferToHex");
|
|
6
6
|
const HexToBuffer_1 = require("../helper/HexToBuffer");
|
|
7
|
+
const BufferToNumber_1 = require("../helper/BufferToNumber");
|
|
7
8
|
const NumberToBuffer_1 = require("../helper/NumberToBuffer");
|
|
9
|
+
const Checksum_1 = require("../helper/Checksum");
|
|
8
10
|
const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
9
11
|
/**
|
|
10
12
|
* DCCP — Datagram Congestion Control Protocol (RFC 4340), carried directly over IP as protocol 33. The
|
|
@@ -20,8 +22,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
|
20
22
|
* the header — Reserved, the Sequence/Ack numbers, per-type fields and options — verbatim as
|
|
21
23
|
* `headerOptions` hex (bytes 9 .. DataOffset*4). Application data after the header is kept as `payload`
|
|
22
24
|
* hex, bounded by the enclosing IP datagram so an inflated Data Offset can't read past the IP payload.
|
|
23
|
-
* The Checksum
|
|
24
|
-
* from the actual bytes. A well-formed datagram round-trips
|
|
25
|
+
* The Checksum and Data Offset are computed on encode when left out (the checksum per RFC 4340 §9, Data
|
|
26
|
+
* Offset from the actual bytes) and written as given otherwise. A well-formed datagram round-trips
|
|
27
|
+
* byte-for-byte.
|
|
25
28
|
*/
|
|
26
29
|
class DCCP extends BaseHeader_1.BaseHeader {
|
|
27
30
|
constructor() {
|
|
@@ -73,6 +76,28 @@ class DCCP extends BaseHeader_1.BaseHeader {
|
|
|
73
76
|
end = 0;
|
|
74
77
|
return end;
|
|
75
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* The DCCP checksum (RFC 4340 §9): the Internet checksum of the nearest IP layer's pseudo-header
|
|
81
|
+
* (protocol 33, length = the whole DCCP packet), the DCCP header and the application data Checksum
|
|
82
|
+
* Coverage selects — all of it when CsCov is 0, else the first (CsCov - 1) × 4 bytes after the Data
|
|
83
|
+
* Offset × 4 header, capped at the packet. Called once every field is written, checksum still zero.
|
|
84
|
+
*/
|
|
85
|
+
#calculateChecksum() {
|
|
86
|
+
const packet = this.packet.subarray(this.startPos, this.endPos);
|
|
87
|
+
const pseudoHeader = this.ipPseudoHeader(33, packet.length);
|
|
88
|
+
if (!pseudoHeader) {
|
|
89
|
+
this.recordError(this.instance.checksum.getPath(), 'Cannot compute DCCP checksum: no IP layer beneath DCCP');
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
const cscov = this.instance.cscov.getValue(0);
|
|
93
|
+
let covered = packet.length;
|
|
94
|
+
if (cscov) {
|
|
95
|
+
const coverage = (this.instance.dataOffset.getValue(0) + cscov - 1) * 4;
|
|
96
|
+
if (coverage < covered)
|
|
97
|
+
covered = coverage;
|
|
98
|
+
}
|
|
99
|
+
return (0, Checksum_1.InternetChecksum)(pseudoHeader, packet.subarray(0, covered));
|
|
100
|
+
}
|
|
76
101
|
/** A big-endian bit-slice within a single octet at `offset`, read/written in place. */
|
|
77
102
|
static #bitField(name, offset, bitOffset, bitLength, label) {
|
|
78
103
|
const maximum = (2 ** bitLength) - 1;
|
|
@@ -105,7 +130,8 @@ class DCCP extends BaseHeader_1.BaseHeader {
|
|
|
105
130
|
dstPort: this.fieldUInt('dstPort', 2, 2, 'Destination Port'),
|
|
106
131
|
//Data Offset (32-bit words) = the length of the whole DCCP header incl. options. Honored
|
|
107
132
|
//when supplied (a crafted datagram may lie); else derived from the fixed 9-byte prefix
|
|
108
|
-
//plus the verbatim headerOptions bytes, rounded up to a word
|
|
133
|
+
//plus the verbatim headerOptions bytes, rounded up to a word, and the header is
|
|
134
|
+
//zero-filled up to that word so it is as long as the Data Offset says.
|
|
109
135
|
dataOffset: {
|
|
110
136
|
type: 'integer',
|
|
111
137
|
autoCalculated: true,
|
|
@@ -122,8 +148,10 @@ class DCCP extends BaseHeader_1.BaseHeader {
|
|
|
122
148
|
value = provided;
|
|
123
149
|
}
|
|
124
150
|
else {
|
|
125
|
-
const
|
|
126
|
-
value = Math.ceil(
|
|
151
|
+
const headerBytes = 9 + (0, HexToBuffer_1.HexToBuffer)(this.instance.headerOptions.getValue('')).length;
|
|
152
|
+
value = Math.ceil(headerBytes / 4);
|
|
153
|
+
if (value * 4 > headerBytes)
|
|
154
|
+
this.writeBytes(headerBytes, Buffer.alloc(value * 4 - headerBytes, 0));
|
|
127
155
|
}
|
|
128
156
|
if (value > 255) {
|
|
129
157
|
this.recordError(this.instance.dataOffset.getPath(), 'Maximum value is 255');
|
|
@@ -138,9 +166,33 @@ class DCCP extends BaseHeader_1.BaseHeader {
|
|
|
138
166
|
//Byte 5: CCVal (high 4 bits) + CsCov (low 4 bits).
|
|
139
167
|
ccval: this.#bitField('ccval', 5, 0, 4, 'CCVal'),
|
|
140
168
|
cscov: this.#bitField('cscov', 5, 4, 4, 'Checksum Coverage'),
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
checksum:
|
|
169
|
+
//Left out, computed once every field is written (#calculateChecksum); a given value is
|
|
170
|
+
//written as given, so a captured datagram round-trips byte-for-byte.
|
|
171
|
+
checksum: {
|
|
172
|
+
type: 'integer',
|
|
173
|
+
autoCalculated: true,
|
|
174
|
+
label: 'Checksum',
|
|
175
|
+
minimum: 0,
|
|
176
|
+
maximum: 65535,
|
|
177
|
+
decode: function () {
|
|
178
|
+
this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
|
|
179
|
+
},
|
|
180
|
+
encode: function () {
|
|
181
|
+
if (!this.isLeftOut(this.instance.checksum)) {
|
|
182
|
+
const checksum = this.instance.checksum.getValue(0);
|
|
183
|
+
this.instance.checksum.setValue(checksum);
|
|
184
|
+
this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.instance.checksum.setValue(0);
|
|
188
|
+
this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
|
|
189
|
+
this.addPostSelfEncodeHandler(() => {
|
|
190
|
+
const checksum = this.#calculateChecksum();
|
|
191
|
+
this.instance.checksum.setValue(checksum);
|
|
192
|
+
this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
},
|
|
144
196
|
//Byte 8: Res (3 bits) + Type (4 bits) + X (1 bit, Extended Sequence Numbers).
|
|
145
197
|
res: this.#bitField('res', 8, 0, 3, 'Reserved'),
|
|
146
198
|
type: this.#bitField('type', 8, 3, 4, 'Type'),
|
package/dist/headers/DNP3.d.ts
CHANGED
|
@@ -11,9 +11,11 @@ import { DemuxProducer } from '../types/DemuxProducer';
|
|
|
11
11
|
*
|
|
12
12
|
* This codec decodes the link header structurally and keeps the data-block region (transport header +
|
|
13
13
|
* application data + the per-block CRCs) verbatim as `payload` hex — de-blocking those CRCs to recover
|
|
14
|
-
* the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The block
|
|
15
|
+
* CRCs stay inside `payload` exactly as given. Length and the header CRC are written as given; left out,
|
|
16
|
+
* Length counts the payload's data octets (its block CRCs excluded) and the header CRC is computed
|
|
17
|
+
* (CRC-16/DNP over the first 8 octets). A well-formed frame round-trips byte-for-byte; the frame's extent is
|
|
18
|
+
* derived from the Length field so a pipelined/trailing frame is left to RawData.
|
|
17
19
|
*/
|
|
18
20
|
export declare class DNP3 extends BaseHeader {
|
|
19
21
|
#private;
|
package/dist/headers/DNP3.js
CHANGED
|
@@ -5,6 +5,9 @@ const BaseHeader_1 = require("../abstracts/BaseHeader");
|
|
|
5
5
|
const BufferToHex_1 = require("../helper/BufferToHex");
|
|
6
6
|
const HexToBuffer_1 = require("../helper/HexToBuffer");
|
|
7
7
|
const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
8
|
+
const BufferToNumber_1 = require("../helper/BufferToNumber");
|
|
9
|
+
const NumberToBuffer_1 = require("../helper/NumberToBuffer");
|
|
10
|
+
const Crc_1 = require("../helper/Crc");
|
|
8
11
|
/**
|
|
9
12
|
* DNP3 — Distributed Network Protocol 3 (IEEE 1815), a SCADA protocol common in electric/water
|
|
10
13
|
* utilities, on TCP/UDP port 20000. Each frame begins with a 10-byte Data Link Layer header: the start
|
|
@@ -15,9 +18,11 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
|
15
18
|
*
|
|
16
19
|
* This codec decodes the link header structurally and keeps the data-block region (transport header +
|
|
17
20
|
* application data + the per-block CRCs) verbatim as `payload` hex — de-blocking those CRCs to recover
|
|
18
|
-
* the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
+
* the transport/application layer is a cross-frame reassembly concern, not a single-packet one. The block
|
|
22
|
+
* CRCs stay inside `payload` exactly as given. Length and the header CRC are written as given; left out,
|
|
23
|
+
* Length counts the payload's data octets (its block CRCs excluded) and the header CRC is computed
|
|
24
|
+
* (CRC-16/DNP over the first 8 octets). A well-formed frame round-trips byte-for-byte; the frame's extent is
|
|
25
|
+
* derived from the Length field so a pipelined/trailing frame is left to RawData.
|
|
21
26
|
*/
|
|
22
27
|
class DNP3 extends BaseHeader_1.BaseHeader {
|
|
23
28
|
constructor() {
|
|
@@ -56,13 +61,44 @@ class DNP3 extends BaseHeader_1.BaseHeader {
|
|
|
56
61
|
const blocks = Math.ceil(userData / 16);
|
|
57
62
|
return 10 + userData + blocks * 2;
|
|
58
63
|
}
|
|
64
|
+
/** The user data octets a data-block region of `payloadOctets` carries: every block is up to 16 data
|
|
65
|
+
* octets followed by their 2-octet CRC. A trailing block too short to hold a data octet and its CRC
|
|
66
|
+
* (malformed) contributes none. */
|
|
67
|
+
static #userDataOctets(payloadOctets) {
|
|
68
|
+
const rest = payloadOctets % 18;
|
|
69
|
+
return Math.floor(payloadOctets / 18) * 16 + (rest > 2 ? rest - 2 : 0);
|
|
70
|
+
}
|
|
59
71
|
static #buildSchema() {
|
|
60
72
|
return {
|
|
61
73
|
type: 'object',
|
|
62
74
|
summary: 'DNP3 func=${control.functionCode} ${source}->${destination}',
|
|
63
75
|
properties: {
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
//Default 0x0564: the start octets every DNP3 frame begins with, and what match() requires.
|
|
77
|
+
start: { ...this.fieldHex('start', 0, 2, 'Start'), default: '0564' },
|
|
78
|
+
//Octets from Control through the user data, the block CRCs excluded: 5 + user data octets.
|
|
79
|
+
length: {
|
|
80
|
+
type: 'integer',
|
|
81
|
+
autoCalculated: true,
|
|
82
|
+
label: 'Length',
|
|
83
|
+
minimum: 0,
|
|
84
|
+
maximum: 255,
|
|
85
|
+
decode: function () {
|
|
86
|
+
this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(2, 1)));
|
|
87
|
+
},
|
|
88
|
+
encode: function () {
|
|
89
|
+
if (!this.isLeftOut(this.instance.length)) {
|
|
90
|
+
this.writeBytes(2, (0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.length.getValue(0)));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let length = 5 + DNP3.#userDataOctets((0, HexToBuffer_1.HexToBuffer)(this.instance.payload.getValue('')).length);
|
|
94
|
+
if (length > 255) {
|
|
95
|
+
this.recordError(this.instance.length.getPath(), 'Maximum value is 255');
|
|
96
|
+
length = 255;
|
|
97
|
+
}
|
|
98
|
+
this.instance.length.setValue(length);
|
|
99
|
+
this.writeBytes(2, (0, NumberToBuffer_1.UInt8ToBuffer)(length));
|
|
100
|
+
}
|
|
101
|
+
},
|
|
66
102
|
//Control octet (byte 3, MSB first): DIR / PRM / FCB / FCV(or DFC) / Function Code(4 bits).
|
|
67
103
|
control: {
|
|
68
104
|
type: 'object',
|
|
@@ -111,8 +147,27 @@ class DNP3 extends BaseHeader_1.BaseHeader {
|
|
|
111
147
|
this.writeBytes(6, Buffer.from([value & 0xff, (value >> 8) & 0xff]));
|
|
112
148
|
}
|
|
113
149
|
},
|
|
114
|
-
//Header CRC over the preceding 8 octets,
|
|
115
|
-
|
|
150
|
+
//Header CRC: CRC-16/DNP over the preceding 8 octets (start through source), low octet first.
|
|
151
|
+
//Those octets are all written when this runs, so a left-out CRC is computed right here.
|
|
152
|
+
headerCrc: {
|
|
153
|
+
type: 'string',
|
|
154
|
+
autoCalculated: true,
|
|
155
|
+
label: 'Header CRC',
|
|
156
|
+
contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
|
157
|
+
decode: function () {
|
|
158
|
+
this.instance.headerCrc.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(8, 2)));
|
|
159
|
+
},
|
|
160
|
+
encode: function () {
|
|
161
|
+
if (!this.isLeftOut(this.instance.headerCrc)) {
|
|
162
|
+
this.writeBytes(8, (0, HexToBuffer_1.HexToBuffer)(this.instance.headerCrc.getValue()));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const crc = (0, Crc_1.Crc16Dnp)(this.readBytes(0, 8));
|
|
166
|
+
const crcBuffer = Buffer.from([crc & 0xff, (crc >> 8) & 0xff]);
|
|
167
|
+
this.instance.headerCrc.setValue((0, BufferToHex_1.BufferToHex)(crcBuffer));
|
|
168
|
+
this.writeBytes(8, crcBuffer);
|
|
169
|
+
}
|
|
170
|
+
},
|
|
116
171
|
//The data-block region: transport header + application data + per-block CRCs, kept verbatim.
|
|
117
172
|
//Bounded by the Length-derived frame extent so a pipelined/trailing frame is left to raw.
|
|
118
173
|
payload: {
|
package/dist/headers/EIGRP.d.ts
CHANGED
|
@@ -13,8 +13,9 @@ import { DemuxProducer } from '../types/DemuxProducer';
|
|
|
13
13
|
* OSPF #available) — a lying Length near the end never reads into a trailing FCS/padding. TLV values are
|
|
14
14
|
* kept verbatim as hex (per-TLV route/parameter bodies are opaque here), so every TLV round-trips
|
|
15
15
|
* byte-for-byte; the Length is honored when supplied (a crafted TLV may lie) else derived from the value
|
|
16
|
-
* byte count. The Checksum is
|
|
17
|
-
* well-formed packet round-trips byte-for-byte. Leaf header —
|
|
16
|
+
* byte count. The Checksum is computed over the whole EIGRP packet when left out (no pseudo-header, over
|
|
17
|
+
* IPv4 or IPv6), else written as given. A well-formed packet round-trips byte-for-byte. Leaf header —
|
|
18
|
+
* TLV sub-structures are not sub-decoded.
|
|
18
19
|
*/
|
|
19
20
|
export declare class EIGRP extends BaseHeader {
|
|
20
21
|
#private;
|
package/dist/headers/EIGRP.js
CHANGED
|
@@ -6,6 +6,7 @@ const BufferToHex_1 = require("../helper/BufferToHex");
|
|
|
6
6
|
const HexToBuffer_1 = require("../helper/HexToBuffer");
|
|
7
7
|
const BufferToNumber_1 = require("../helper/BufferToNumber");
|
|
8
8
|
const NumberToBuffer_1 = require("../helper/NumberToBuffer");
|
|
9
|
+
const Checksum_1 = require("../helper/Checksum");
|
|
9
10
|
const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
10
11
|
/**
|
|
11
12
|
* EIGRP — Enhanced Interior Gateway Routing Protocol (Cisco), carried directly over IP as protocol 88.
|
|
@@ -19,8 +20,9 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
|
|
|
19
20
|
* OSPF #available) — a lying Length near the end never reads into a trailing FCS/padding. TLV values are
|
|
20
21
|
* kept verbatim as hex (per-TLV route/parameter bodies are opaque here), so every TLV round-trips
|
|
21
22
|
* byte-for-byte; the Length is honored when supplied (a crafted TLV may lie) else derived from the value
|
|
22
|
-
* byte count. The Checksum is
|
|
23
|
-
* well-formed packet round-trips byte-for-byte. Leaf header —
|
|
23
|
+
* byte count. The Checksum is computed over the whole EIGRP packet when left out (no pseudo-header, over
|
|
24
|
+
* IPv4 or IPv6), else written as given. A well-formed packet round-trips byte-for-byte. Leaf header —
|
|
25
|
+
* TLV sub-structures are not sub-decoded.
|
|
24
26
|
*/
|
|
25
27
|
class EIGRP extends BaseHeader_1.BaseHeader {
|
|
26
28
|
constructor() {
|
|
@@ -66,9 +68,33 @@ class EIGRP extends BaseHeader_1.BaseHeader {
|
|
|
66
68
|
//==== Fixed 20-byte header ====
|
|
67
69
|
version: this.fieldUInt('version', 0, 1, 'Version'),
|
|
68
70
|
opcode: this.fieldUInt('opcode', 1, 1, 'Opcode'),
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
checksum:
|
|
71
|
+
//Internet checksum over the whole EIGRP packet (header + TLVs, checksum field zeroed),
|
|
72
|
+
//computed once every field is written when left out; a given value is written as given.
|
|
73
|
+
checksum: {
|
|
74
|
+
type: 'integer',
|
|
75
|
+
autoCalculated: true,
|
|
76
|
+
label: 'Checksum',
|
|
77
|
+
minimum: 0,
|
|
78
|
+
maximum: 65535,
|
|
79
|
+
decode: function () {
|
|
80
|
+
this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
|
|
81
|
+
},
|
|
82
|
+
encode: function () {
|
|
83
|
+
if (!this.isLeftOut(this.instance.checksum)) {
|
|
84
|
+
const checksum = this.instance.checksum.getValue(0);
|
|
85
|
+
this.instance.checksum.setValue(checksum);
|
|
86
|
+
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.instance.checksum.setValue(0);
|
|
90
|
+
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
|
|
91
|
+
this.addPostSelfEncodeHandler(() => {
|
|
92
|
+
const checksum = (0, Checksum_1.InternetChecksum)(this.packet.subarray(this.startPos, this.endPos));
|
|
93
|
+
this.instance.checksum.setValue(checksum);
|
|
94
|
+
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
},
|
|
72
98
|
flags: this.fieldUInt('flags', 4, 4, 'Flags'),
|
|
73
99
|
sequence: this.fieldUInt('sequence', 8, 4, 'Sequence'),
|
|
74
100
|
ack: this.fieldUInt('ack', 12, 4, 'Acknowledge'),
|
package/dist/headers/GELF.js
CHANGED
|
@@ -104,6 +104,7 @@ class GELF extends BaseHeader_1.BaseHeader {
|
|
|
104
104
|
//when the form is chunked; otherwise it stays unset so it neither decodes phantom values
|
|
105
105
|
//nor writes bytes for the gzip/uncompressed forms.
|
|
106
106
|
magic: {
|
|
107
|
+
default: '1e0f',
|
|
107
108
|
type: 'string',
|
|
108
109
|
label: 'Magic',
|
|
109
110
|
contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
package/dist/headers/GENEVE.js
CHANGED
|
@@ -86,6 +86,7 @@ class GENEVE extends BaseHeader_1.BaseHeader {
|
|
|
86
86
|
//Protocol Type is the EtherType of the inner frame — stored as a lowercase hex string
|
|
87
87
|
//(like eth.etherType) so it can drive the `ethertype` demux dimension to the inner codec.
|
|
88
88
|
protocolType: {
|
|
89
|
+
autoCalculated: true,
|
|
89
90
|
type: 'string', label: 'Protocol Type', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
|
90
91
|
decode: function () { this.instance.protocolType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2))); },
|
|
91
92
|
encode: function () {
|
package/dist/headers/GRE.js
CHANGED
|
@@ -109,6 +109,7 @@ class GRE extends BaseHeader_1.BaseHeader {
|
|
|
109
109
|
//Protocol Type is the EtherType of the inner frame — stored as a lowercase hex string
|
|
110
110
|
//(like eth.etherType) so it can drive the `ethertype` demux dimension to the inner codec.
|
|
111
111
|
protocolType: {
|
|
112
|
+
autoCalculated: true,
|
|
112
113
|
type: 'string', label: 'Protocol Type', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
|
113
114
|
decode: function () { this.instance.protocolType.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2))); },
|
|
114
115
|
encode: function () {
|
package/dist/headers/GTPv2C.js
CHANGED
package/dist/headers/Goose.js
CHANGED
|
@@ -63,8 +63,8 @@ class Goose extends BaseHeader_1.BaseHeader {
|
|
|
63
63
|
this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
|
|
64
64
|
},
|
|
65
65
|
encode: () => {
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
if (!this.isLeftOut(this.instance.length)) {
|
|
67
|
+
const length = this.instance.length.getValue(0);
|
|
68
68
|
this.instance.length.setValue(length);
|
|
69
69
|
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
|
|
70
70
|
}
|
|
@@ -169,7 +169,7 @@ class Goose extends BaseHeader_1.BaseHeader {
|
|
|
169
169
|
//decoded (it may itself be inconsistent in a malformed frame).
|
|
170
170
|
const rawBuffer = Buffer.from(this.instance.goosePdu.raw.getValue().toString(), 'hex');
|
|
171
171
|
this.writeBytes(8, rawBuffer);
|
|
172
|
-
if (this.instance.length
|
|
172
|
+
if (!this.isLeftOut(this.instance.length))
|
|
173
173
|
return;
|
|
174
174
|
this.instance.length.setValue(8 + rawBuffer.length);
|
|
175
175
|
return;
|
|
@@ -183,7 +183,7 @@ class Goose extends BaseHeader_1.BaseHeader {
|
|
|
183
183
|
* Update the length only if it is not set
|
|
184
184
|
* Update length(APPID's length + Length's length + Reserved1's length + Reserved2's length + goosePdu's length)
|
|
185
185
|
*/
|
|
186
|
-
if (this.instance.length
|
|
186
|
+
if (!this.isLeftOut(this.instance.length))
|
|
187
187
|
return;
|
|
188
188
|
this.instance.length.setValue(2 + 2 + 2 + 2 + goosePduBuffer.length);
|
|
189
189
|
},
|
package/dist/headers/HSR.js
CHANGED
package/dist/headers/HTTP2.js
CHANGED
|
@@ -61,6 +61,7 @@ class HTTP2 extends BaseHeader_1.BaseHeader {
|
|
|
61
61
|
//The 24-byte connection preface, kept verbatim. match() requires it, so it is always
|
|
62
62
|
//present on decode; the default lets a crafted frame re-emit the standard preface.
|
|
63
63
|
preface: {
|
|
64
|
+
default: HTTP2.#PREFACE_HEX,
|
|
64
65
|
type: 'string',
|
|
65
66
|
label: 'Connection Preface',
|
|
66
67
|
contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
|
package/dist/headers/ICMP.js
CHANGED
|
@@ -50,10 +50,11 @@ class ICMP extends BaseHeader_1.BaseHeader {
|
|
|
50
50
|
this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
|
|
51
51
|
},
|
|
52
52
|
encode: () => {
|
|
53
|
+
const leftOut = this.isLeftOut(this.instance.checksum);
|
|
53
54
|
let checksum = this.instance.checksum.getValue(0);
|
|
54
55
|
this.instance.checksum.setValue(checksum);
|
|
55
56
|
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|
|
56
|
-
if (
|
|
57
|
+
if (leftOut) {
|
|
57
58
|
checksum = this.calculateIcmpChecksum();
|
|
58
59
|
this.instance.checksum.setValue(checksum);
|
|
59
60
|
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
|