@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.
Files changed (136) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/README.md +31 -7
  3. package/README.zh-CN.md +28 -7
  4. package/dist/Codec.d.ts +4 -3
  5. package/dist/Codec.js +48 -6
  6. package/dist/PacketHeaders.d.ts +2 -0
  7. package/dist/PacketHeaders.js +8 -4
  8. package/dist/abstracts/BaseHeader.d.ts +43 -0
  9. package/dist/abstracts/BaseHeader.js +75 -0
  10. package/dist/headers/AH.js +3 -2
  11. package/dist/headers/ARP.d.ts +3 -1
  12. package/dist/headers/ARP.js +8 -1
  13. package/dist/headers/BACnetIP.js +2 -1
  14. package/dist/headers/BFD.d.ts +2 -1
  15. package/dist/headers/BFD.js +30 -4
  16. package/dist/headers/BGP.js +8 -3
  17. package/dist/headers/Babel.js +1 -1
  18. package/dist/headers/C37118.d.ts +3 -2
  19. package/dist/headers/C37118.js +34 -15
  20. package/dist/headers/CDP.d.ts +3 -3
  21. package/dist/headers/CDP.js +46 -10
  22. package/dist/headers/CMS.js +25 -10
  23. package/dist/headers/COTP.js +2 -3
  24. package/dist/headers/CoAP.js +20 -2
  25. package/dist/headers/Codesys.js +3 -2
  26. package/dist/headers/DCCP.d.ts +3 -2
  27. package/dist/headers/DCCP.js +60 -8
  28. package/dist/headers/DHCP.js +2 -1
  29. package/dist/headers/DNP3.d.ts +12 -5
  30. package/dist/headers/DNP3.js +153 -18
  31. package/dist/headers/DTLS.js +5 -3
  32. package/dist/headers/Diameter.js +2 -1
  33. package/dist/headers/EIGRP.d.ts +3 -2
  34. package/dist/headers/EIGRP.js +31 -5
  35. package/dist/headers/Elasticsearch.js +3 -2
  36. package/dist/headers/EthernetII.d.ts +1 -0
  37. package/dist/headers/EthernetII.js +4 -0
  38. package/dist/headers/GELF.js +1 -0
  39. package/dist/headers/GENEVE.js +1 -0
  40. package/dist/headers/GRE.js +1 -0
  41. package/dist/headers/GTPv2C.js +1 -0
  42. package/dist/headers/Goose.d.ts +31 -7
  43. package/dist/headers/Goose.js +413 -172
  44. package/dist/headers/HARTIP.js +2 -2
  45. package/dist/headers/HSR.js +1 -0
  46. package/dist/headers/HTTP2.js +1 -0
  47. package/dist/headers/ICMP.js +2 -1
  48. package/dist/headers/ICMPv6.js +10 -16
  49. package/dist/headers/IEC104_I_Frame.d.ts +28 -0
  50. package/dist/headers/IEC104_I_Frame.js +434 -319
  51. package/dist/headers/IEC104_S_Frame.js +22 -4
  52. package/dist/headers/IEC104_U_Frame.js +24 -4
  53. package/dist/headers/IEC61850SampledValues.d.ts +1 -0
  54. package/dist/headers/IEC61850SampledValues.js +20 -5
  55. package/dist/headers/IGMP.d.ts +8 -7
  56. package/dist/headers/IGMP.js +57 -19
  57. package/dist/headers/IPFIX.js +3 -2
  58. package/dist/headers/IPv4.d.ts +1 -0
  59. package/dist/headers/IPv4.js +32 -23
  60. package/dist/headers/IPv6.d.ts +1 -0
  61. package/dist/headers/IPv6.js +10 -1
  62. package/dist/headers/IPv6HopByHopOptions.js +11 -1
  63. package/dist/headers/ISAKMP.js +1 -0
  64. package/dist/headers/KNXnetIP.js +3 -2
  65. package/dist/headers/Kerberos.js +1 -0
  66. package/dist/headers/L2TP.js +1 -0
  67. package/dist/headers/LACP.js +2 -1
  68. package/dist/headers/LDAP.js +1 -1
  69. package/dist/headers/LDP.js +2 -2
  70. package/dist/headers/LLC.js +1 -0
  71. package/dist/headers/MQTT.js +3 -1
  72. package/dist/headers/MarkerProtocol.js +2 -1
  73. package/dist/headers/NHRP.d.ts +1 -1
  74. package/dist/headers/NHRP.js +29 -4
  75. package/dist/headers/NetFlowV5.js +2 -1
  76. package/dist/headers/OPCUA.js +4 -0
  77. package/dist/headers/OSPF.d.ts +5 -5
  78. package/dist/headers/OSPF.js +98 -12
  79. package/dist/headers/OSPFv3.d.ts +37 -0
  80. package/dist/headers/OSPFv3.js +314 -0
  81. package/dist/headers/PIM.d.ts +3 -3
  82. package/dist/headers/PIM.js +55 -6
  83. package/dist/headers/PPPoEDiscovery.js +2 -0
  84. package/dist/headers/PPTP.js +2 -2
  85. package/dist/headers/PTP.js +5 -1
  86. package/dist/headers/QUIC.js +8 -3
  87. package/dist/headers/RDP.js +2 -0
  88. package/dist/headers/RGoose.js +4 -1
  89. package/dist/headers/RIP.js +3 -2
  90. package/dist/headers/RIPng.js +3 -2
  91. package/dist/headers/RSVP.d.ts +3 -3
  92. package/dist/headers/RSVP.js +56 -42
  93. package/dist/headers/RTCP.js +4 -2
  94. package/dist/headers/RTP.js +3 -2
  95. package/dist/headers/SCTP.d.ts +2 -2
  96. package/dist/headers/SCTP.js +29 -5
  97. package/dist/headers/SFlow.js +4 -2
  98. package/dist/headers/SLMP.js +2 -1
  99. package/dist/headers/SLP.js +2 -1
  100. package/dist/headers/SLPv1.js +2 -1
  101. package/dist/headers/SMB1.js +3 -2
  102. package/dist/headers/SMB2.js +3 -2
  103. package/dist/headers/SNAP.js +2 -2
  104. package/dist/headers/SNMP.js +2 -2
  105. package/dist/headers/STUN.js +3 -2
  106. package/dist/headers/SercosIII.d.ts +3 -2
  107. package/dist/headers/SercosIII.js +41 -5
  108. package/dist/headers/TCP.js +13 -58
  109. package/dist/headers/TLS_Alert.js +20 -2
  110. package/dist/headers/TLS_ApplicationData.d.ts +3 -3
  111. package/dist/headers/TLS_ApplicationData.js +25 -6
  112. package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
  113. package/dist/headers/TLS_ChangeCipherSpec.js +24 -4
  114. package/dist/headers/TLS_Handshake.d.ts +6 -0
  115. package/dist/headers/TLS_Handshake.js +67 -9
  116. package/dist/headers/TLS_Heartbeat.d.ts +4 -3
  117. package/dist/headers/TLS_Heartbeat.js +40 -7
  118. package/dist/headers/TPKT.js +1 -1
  119. package/dist/headers/TacacsPlus.js +3 -1
  120. package/dist/headers/Trailer.d.ts +30 -0
  121. package/dist/headers/Trailer.js +63 -0
  122. package/dist/headers/UDP.js +17 -61
  123. package/dist/headers/VLAN_802dot1Q.d.ts +1 -0
  124. package/dist/headers/VLAN_802dot1Q.js +4 -0
  125. package/dist/headers/VRRP.d.ts +2 -1
  126. package/dist/headers/VRRP.js +45 -9
  127. package/dist/headers/WakeOnLAN.js +2 -1
  128. package/dist/headers/WireGuard.js +3 -2
  129. package/dist/headers/Zabbix.js +2 -2
  130. package/dist/helper/Checksum.d.ts +15 -0
  131. package/dist/helper/Checksum.js +56 -0
  132. package/dist/helper/Crc.d.ts +18 -0
  133. package/dist/helper/Crc.js +58 -0
  134. package/dist/lib/BERElement.d.ts +51 -0
  135. package/dist/lib/BERElement.js +125 -0
  136. 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.0.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": "a0b16ffec11eb830dd5846bd942dd9a63ae04d5e"
141
+ "gitHead": "698bce9a3e8414a2a7ef2af687c4feff0aa1a25c"
142
142
  }