@netkitty/codec 2.0.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.
- package/CHANGELOG.md +324 -0
- package/README.md +31 -7
- package/README.zh-CN.md +28 -7
- package/dist/Codec.d.ts +4 -3
- package/dist/Codec.js +48 -6
- package/dist/PacketHeaders.d.ts +2 -0
- package/dist/PacketHeaders.js +8 -4
- package/dist/abstracts/BaseHeader.d.ts +43 -0
- package/dist/abstracts/BaseHeader.js +75 -0
- package/dist/headers/AH.js +3 -2
- package/dist/headers/ARP.d.ts +3 -1
- package/dist/headers/ARP.js +8 -1
- package/dist/headers/BACnetIP.js +2 -1
- package/dist/headers/BFD.d.ts +2 -1
- package/dist/headers/BFD.js +30 -4
- package/dist/headers/BGP.js +8 -3
- package/dist/headers/Babel.js +1 -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/Codesys.js +3 -2
- package/dist/headers/DCCP.d.ts +3 -2
- package/dist/headers/DCCP.js +60 -8
- package/dist/headers/DHCP.js +2 -1
- package/dist/headers/DNP3.d.ts +12 -5
- package/dist/headers/DNP3.js +153 -18
- package/dist/headers/DTLS.js +5 -3
- package/dist/headers/Diameter.js +2 -1
- package/dist/headers/EIGRP.d.ts +3 -2
- package/dist/headers/EIGRP.js +31 -5
- package/dist/headers/Elasticsearch.js +3 -2
- package/dist/headers/EthernetII.d.ts +1 -0
- package/dist/headers/EthernetII.js +4 -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.d.ts +31 -7
- package/dist/headers/Goose.js +413 -172
- package/dist/headers/HARTIP.js +2 -2
- 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.d.ts +28 -0
- package/dist/headers/IEC104_I_Frame.js +434 -319
- package/dist/headers/IEC104_S_Frame.js +22 -4
- package/dist/headers/IEC104_U_Frame.js +24 -4
- package/dist/headers/IEC61850SampledValues.d.ts +1 -0
- package/dist/headers/IEC61850SampledValues.js +20 -5
- package/dist/headers/IGMP.d.ts +8 -7
- package/dist/headers/IGMP.js +57 -19
- package/dist/headers/IPFIX.js +3 -2
- package/dist/headers/IPv4.d.ts +1 -0
- package/dist/headers/IPv4.js +32 -23
- package/dist/headers/IPv6.d.ts +1 -0
- package/dist/headers/IPv6.js +10 -1
- package/dist/headers/IPv6HopByHopOptions.js +11 -1
- package/dist/headers/ISAKMP.js +1 -0
- package/dist/headers/KNXnetIP.js +3 -2
- package/dist/headers/Kerberos.js +1 -0
- package/dist/headers/L2TP.js +1 -0
- package/dist/headers/LACP.js +2 -1
- package/dist/headers/LDAP.js +1 -1
- package/dist/headers/LDP.js +2 -2
- package/dist/headers/LLC.js +1 -0
- package/dist/headers/MQTT.js +3 -1
- package/dist/headers/MarkerProtocol.js +2 -1
- package/dist/headers/NHRP.d.ts +1 -1
- package/dist/headers/NHRP.js +29 -4
- package/dist/headers/NetFlowV5.js +2 -1
- package/dist/headers/OPCUA.js +4 -0
- package/dist/headers/OSPF.d.ts +5 -5
- package/dist/headers/OSPF.js +98 -12
- package/dist/headers/OSPFv3.d.ts +37 -0
- package/dist/headers/OSPFv3.js +314 -0
- 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/PPTP.js +2 -2
- package/dist/headers/PTP.js +5 -1
- package/dist/headers/QUIC.js +8 -3
- package/dist/headers/RDP.js +2 -0
- package/dist/headers/RGoose.js +4 -1
- package/dist/headers/RIP.js +3 -2
- package/dist/headers/RIPng.js +3 -2
- package/dist/headers/RSVP.d.ts +3 -3
- package/dist/headers/RSVP.js +56 -42
- package/dist/headers/RTCP.js +4 -2
- package/dist/headers/RTP.js +3 -2
- package/dist/headers/SCTP.d.ts +2 -2
- package/dist/headers/SCTP.js +29 -5
- package/dist/headers/SFlow.js +4 -2
- package/dist/headers/SLMP.js +2 -1
- package/dist/headers/SLP.js +2 -1
- package/dist/headers/SLPv1.js +2 -1
- package/dist/headers/SMB1.js +3 -2
- package/dist/headers/SMB2.js +3 -2
- package/dist/headers/SNAP.js +2 -2
- package/dist/headers/SNMP.js +2 -2
- package/dist/headers/STUN.js +3 -2
- 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.d.ts +6 -0
- package/dist/headers/TLS_Handshake.js +67 -9
- 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/TacacsPlus.js +3 -1
- package/dist/headers/Trailer.d.ts +30 -0
- package/dist/headers/Trailer.js +63 -0
- package/dist/headers/UDP.js +17 -61
- package/dist/headers/VLAN_802dot1Q.d.ts +1 -0
- package/dist/headers/VLAN_802dot1Q.js +4 -0
- package/dist/headers/VRRP.d.ts +2 -1
- package/dist/headers/VRRP.js +45 -9
- package/dist/headers/WakeOnLAN.js +2 -1
- package/dist/headers/WireGuard.js +3 -2
- package/dist/headers/Zabbix.js +2 -2
- 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/lib/BERElement.d.ts +51 -0
- package/dist/lib/BERElement.js +125 -0
- package/package.json +2 -2
|
@@ -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.
|
|
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": "
|
|
141
|
+
"gitHead": "698bce9a3e8414a2a7ef2af687c4feff0aa1a25c"
|
|
142
142
|
}
|