@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,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;
@@ -4,7 +4,7 @@ exports.UDP = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToNumber_1 = require("../helper/BufferToNumber");
6
6
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
7
- const IPToBuffer_1 = require("../helper/IPToBuffer");
7
+ const Checksum_1 = require("../helper/Checksum");
8
8
  class UDP extends BaseHeader_1.BaseHeader {
9
9
  constructor() {
10
10
  super(...arguments);
@@ -24,61 +24,17 @@ class UDP extends BaseHeader_1.BaseHeader {
24
24
  * @param udpHeaderBuffer
25
25
  */
26
26
  calculateUDPChecksum(udpHeaderBuffer) {
27
- const udpHeaderLength = udpHeaderBuffer.length;
28
- //Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack, so record
29
- //an error and leave the checksum 0 instead of dereferencing a missing previous layer.
30
- if (!this.prevCodecModule) {
27
+ //The pseudo-header comes from the nearest IPv4/IPv6 layer beneath, past AH or IPv6 extension
28
+ //headers. Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack,
29
+ //so record it and leave the checksum 0 instead of dereferencing a missing layer.
30
+ const pseudoHeader = this.ipPseudoHeader(17, udpHeaderBuffer.length);
31
+ if (!pseudoHeader) {
31
32
  this.recordError(this.instance.checksum.getPath(), 'Cannot compute UDP checksum: no IP layer beneath UDP');
32
33
  return 0;
33
34
  }
34
- const ipVersion = this.prevCodecModule.instance.version.getValue();
35
- let pseudoHeaderBuffer = Buffer.from([]);
36
- const sourceIp = this.prevCodecModule.instance.sip.getValue();
37
- const destinationIp = this.prevCodecModule.instance.dip.getValue();
38
- if (ipVersion === 4) {
39
- //4 Bytes
40
- const sourceIPv4Buffer = Buffer.from(sourceIp.split('.').map((value) => parseInt(value)));
41
- const destinationIPv4Buffer = Buffer.from(destinationIp.split('.').map((value) => parseInt(value)));
42
- //IPv4 Pseudo header
43
- pseudoHeaderBuffer = Buffer.concat([
44
- sourceIPv4Buffer,
45
- destinationIPv4Buffer,
46
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
47
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
48
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
49
- Buffer.from([udpHeaderLength & 0xFF])
50
- ]);
51
- }
52
- else if (ipVersion === 6) {
53
- //16 Bytes
54
- const sourceIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(sourceIp);
55
- const destinationIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(destinationIp);
56
- //IPv6 Pseudo header
57
- pseudoHeaderBuffer = Buffer.concat([
58
- sourceIPv6Buffer,
59
- destinationIPv6Buffer,
60
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
61
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
62
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
63
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
64
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
65
- Buffer.from([udpHeaderLength & 0xFF])
66
- ]);
67
- }
68
- else {
69
- return 0;
70
- }
71
- let dataBuffer = Buffer.concat([pseudoHeaderBuffer, udpHeaderBuffer]);
72
- if (dataBuffer.length % 2)
73
- dataBuffer = Buffer.concat([dataBuffer, Buffer.from('00', 'hex')]);
74
- const data = Uint8Array.from(dataBuffer);
75
- let sum = 0;
76
- for (let i = 0; i < data.length; i += 2)
77
- sum += (data[i] << 8) + (data[i + 1] || 0);
78
- while (sum > 0xFFFF)
79
- sum = (sum & 0xFFFF) + (sum >>> 16);
80
- const _cs = (~sum) & 0xFFFF;
81
- return _cs === 0 ? 0xFFFF : _cs;
35
+ //A computed 0x0000 is sent as 0xFFFF: 0 means "no checksum" (RFC 768).
36
+ const checksum = (0, Checksum_1.InternetChecksum)(pseudoHeader, udpHeaderBuffer);
37
+ return checksum === 0 ? 0xFFFF : checksum;
82
38
  }
83
39
  static #schemaCache;
84
40
  //Class-cached SCHEMA (④): field closures are functions taking dynamic `this` via .call(this).
@@ -104,14 +60,14 @@ class UDP extends BaseHeader_1.BaseHeader {
104
60
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(4, 2)));
105
61
  },
106
62
  encode: function () {
107
- const length = this.instance.length.getValue(0);
108
- if (length) {
63
+ if (!this.isLeftOut(this.instance.length)) {
64
+ const length = this.instance.length.getValue(0);
109
65
  this.instance.length.setValue(length);
110
66
  this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
111
67
  }
112
68
  else {
113
- this.instance.length.setValue(length);
114
- this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
69
+ this.instance.length.setValue(0);
70
+ this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
115
71
  this.addPostPacketEncodeHandler(() => {
116
72
  let startCount = false;
117
73
  let udpLength = 0;
@@ -137,14 +93,14 @@ class UDP extends BaseHeader_1.BaseHeader {
137
93
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
138
94
  },
139
95
  encode: function () {
140
- const checksum = this.instance.checksum.getValue(0);
141
- if (checksum) {
96
+ if (!this.isLeftOut(this.instance.checksum)) {
97
+ const checksum = this.instance.checksum.getValue(0);
142
98
  this.instance.checksum.setValue(checksum);
143
99
  this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
144
100
  }
145
101
  else {
146
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
147
- this.instance.checksum.setValue(checksum);
102
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
103
+ this.instance.checksum.setValue(0);
148
104
  this.addPostPacketEncodeHandler(() => {
149
105
  let startCount = false;
150
106
  let udpHeaderWithDataLength = 0;
@@ -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
  }
@@ -65,6 +65,7 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
65
65
  }
66
66
  },
67
67
  etherType: {
68
+ autoCalculated: true,
68
69
  type: 'string',
69
70
  minLength: 4,
70
71
  maxLength: 4,
@@ -91,6 +92,9 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
91
92
  this.name = '802.1Q Virtual LAN';
92
93
  this.nickname = 'VLAN';
93
94
  }
95
+ payloadEnd() {
96
+ return this.ieee802dot3PayloadEnd(this.instance.etherType);
97
+ }
94
98
  match() {
95
99
  if (!this.prevCodecModule)
96
100
  return false;
@@ -12,7 +12,8 @@ import { DemuxProducer } from '../types/DemuxProducer';
12
12
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
13
13
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
14
14
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
15
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
15
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
16
+ * written as given otherwise.
16
17
  */
17
18
  export declare class VRRP extends BaseHeader {
18
19
  #private;
@@ -9,6 +9,7 @@ const BufferToIP_1 = require("../helper/BufferToIP");
9
9
  const IPToBuffer_1 = require("../helper/IPToBuffer");
10
10
  const BufferToHex_1 = require("../helper/BufferToHex");
11
11
  const HexToBuffer_1 = require("../helper/HexToBuffer");
12
+ const Checksum_1 = require("../helper/Checksum");
12
13
  /**
13
14
  * VRRP — Virtual Router Redundancy Protocol (v2 RFC 3768, v3 RFC 5798), carried directly over IP as
14
15
  * protocol 112. A fixed 8-byte header — Version (high nibble of byte 0) + Type (low nibble), Virtual
@@ -20,7 +21,8 @@ const HexToBuffer_1 = require("../helper/HexToBuffer");
20
21
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
21
22
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
22
23
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
23
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
24
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
25
+ * written as given otherwise.
24
26
  */
25
27
  class VRRP extends BaseHeader_1.BaseHeader {
26
28
  constructor() {
@@ -54,6 +56,27 @@ class VRRP extends BaseHeader_1.BaseHeader {
54
56
  }
55
57
  return this.packet.length - this.startPos;
56
58
  }
59
+ /**
60
+ * The VRRP checksum: the Internet checksum of the whole VRRP message (v2, RFC 3768 §5.3.8, and v3
61
+ * over IPv4); v3 over IPv6 prepends the IPv6 pseudo-header (protocol 112, upper-layer length = the
62
+ * message length). RFC 5798 was ambiguous for v3 over IPv4 and some implementations added an IPv4
63
+ * pseudo-header; RFC 9568, which obsoletes it, says there is none, as Wireshark checks by default.
64
+ * Called once every field is written, with the checksum still zero.
65
+ * @private
66
+ */
67
+ #calculateChecksum() {
68
+ const message = this.packet.subarray(this.startPos, this.endPos);
69
+ const version = this.instance.version.getValue(2);
70
+ const ip = this.nearestIPModule();
71
+ if (version !== 3 || (ip && ip.id === 'ipv4'))
72
+ return (0, Checksum_1.InternetChecksum)(message);
73
+ const pseudoHeader = this.ipPseudoHeader(112, message.length);
74
+ if (!pseudoHeader) {
75
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute VRRP v3 checksum: no IP layer beneath VRRP');
76
+ return 0;
77
+ }
78
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, message);
79
+ }
57
80
  static #schemaCache;
58
81
  //Class-cached SCHEMA: field closures are plain functions taking dynamic `this` via .call(this), so
59
82
  //the structure is shareable and never rebuilt per packet. VRRP carries both v2 (RFC 3768) and v3
@@ -73,6 +96,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
73
96
  label: 'Version',
74
97
  minimum: 0,
75
98
  maximum: 15,
99
+ default: 2,
76
100
  decode: function () {
77
101
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
78
102
  const version = this.instance.version.getValue(0);
@@ -92,6 +116,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
92
116
  label: 'Type',
93
117
  minimum: 0,
94
118
  maximum: 15,
119
+ default: 1,
95
120
  decode: function () {
96
121
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
97
122
  },
@@ -131,6 +156,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
131
156
  label: 'Adver Int (seconds)',
132
157
  minimum: 0,
133
158
  maximum: 255,
159
+ default: 1,
134
160
  //v2 only: byte 5, advertisement interval in whole seconds (RFC 3768 §5.3.7).
135
161
  decode: function () {
136
162
  const version = this.instance.version.getValue(0);
@@ -215,21 +241,31 @@ class VRRP extends BaseHeader_1.BaseHeader {
215
241
  },
216
242
  checksum: {
217
243
  type: 'integer',
244
+ autoCalculated: true,
218
245
  label: 'Checksum',
219
246
  minimum: 0,
220
247
  maximum: 65535,
221
- //Honoured verbatim (faithful executor): decode reads it, encode writes the same value
222
- //back untouched. VRRP checksums differ by version (v2 over the message only, v3 over
223
- //an IP pseudo-header too), so recomputation is left to the caller.
248
+ //Left out, computed once every field is written (#calculateChecksum: v2 over the
249
+ //message only, v3 over an IP pseudo-header too); a given value is written as given.
224
250
  decode: function () {
225
251
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
226
252
  },
227
253
  encode: function () {
228
- let checksum = this.instance.checksum.getValue(0);
229
- checksum = checksum > 65535 ? 65535 : checksum;
230
- checksum = checksum < 0 ? 0 : checksum;
231
- this.instance.checksum.setValue(checksum);
232
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
254
+ if (!this.isLeftOut(this.instance.checksum)) {
255
+ let checksum = this.instance.checksum.getValue(0);
256
+ checksum = checksum > 65535 ? 65535 : checksum;
257
+ checksum = checksum < 0 ? 0 : checksum;
258
+ this.instance.checksum.setValue(checksum);
259
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
260
+ return;
261
+ }
262
+ this.instance.checksum.setValue(0);
263
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
264
+ this.addPostSelfEncodeHandler(() => {
265
+ const checksum = this.#calculateChecksum();
266
+ this.instance.checksum.setValue(checksum);
267
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
268
+ });
233
269
  }
234
270
  },
235
271
  addresses: {
@@ -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,15 @@
1
+ /**
2
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
3
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
4
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
5
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
6
+ * @param buffers
7
+ */
8
+ export declare function InternetChecksum(...buffers: Buffer[]): number;
9
+ /**
10
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
11
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
12
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
13
+ * @param buffers
14
+ */
15
+ export declare function CRC32c(...buffers: Buffer[]): number;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternetChecksum = InternetChecksum;
4
+ exports.CRC32c = CRC32c;
5
+ /**
6
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
7
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
8
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
9
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
10
+ * @param buffers
11
+ */
12
+ function InternetChecksum(...buffers) {
13
+ let sum = 0;
14
+ let odd = -1;
15
+ for (const buffer of buffers) {
16
+ for (let i = 0; i < buffer.length; i++) {
17
+ if (odd < 0) {
18
+ odd = buffer[i];
19
+ }
20
+ else {
21
+ sum += (odd << 8) | buffer[i];
22
+ odd = -1;
23
+ }
24
+ }
25
+ }
26
+ if (odd >= 0)
27
+ sum += odd << 8;
28
+ while (sum > 0xFFFF)
29
+ sum = (sum & 0xFFFF) + (sum >>> 16);
30
+ return (~sum) & 0xFFFF;
31
+ }
32
+ /** CRC-32C (Castagnoli) lookup table for the reflected polynomial 0x82F63B78, built once. */
33
+ const CRC32C_TABLE = (() => {
34
+ const table = new Uint32Array(256);
35
+ for (let n = 0; n < 256; n++) {
36
+ let crc = n;
37
+ for (let k = 0; k < 8; k++)
38
+ crc = (crc & 1) ? (crc >>> 1) ^ 0x82F63B78 : crc >>> 1;
39
+ table[n] = crc >>> 0;
40
+ }
41
+ return table;
42
+ })();
43
+ /**
44
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
45
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
46
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
47
+ * @param buffers
48
+ */
49
+ function CRC32c(...buffers) {
50
+ let crc = 0xFFFFFFFF;
51
+ for (const buffer of buffers) {
52
+ for (let i = 0; i < buffer.length; i++)
53
+ crc = CRC32C_TABLE[(crc ^ buffer[i]) & 0xFF] ^ (crc >>> 8);
54
+ }
55
+ return (crc ^ 0xFFFFFFFF) >>> 0;
56
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * CRC-32 as used by IEEE 802.3 / zlib (CRC-32/ISO-HDLC): polynomial 0x04C11DB7 processed reflected,
3
+ * initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF. Check value: Crc32('123456789') = 0xCBF43926.
4
+ * @param buffer
5
+ */
6
+ export declare function Crc32(buffer: Buffer): number;
7
+ /**
8
+ * CRC-16/DNP (IEEE 1815 data link layer): polynomial 0x3D65 processed reflected, initial value 0,
9
+ * final XOR 0xFFFF. DNP3 sends it low octet first. Check value: Crc16Dnp('123456789') = 0xEA82.
10
+ * @param buffer
11
+ */
12
+ export declare function Crc16Dnp(buffer: Buffer): number;
13
+ /**
14
+ * CRC-16/CCITT-FALSE (the IEEE C37.118.2 CHK): polynomial 0x1021 processed MSB-first, initial value
15
+ * 0xFFFF, no final XOR. Check value: Crc16CcittFalse('123456789') = 0x29B1.
16
+ * @param buffer
17
+ */
18
+ export declare function Crc16CcittFalse(buffer: Buffer): number;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Crc32 = Crc32;
4
+ exports.Crc16Dnp = Crc16Dnp;
5
+ exports.Crc16CcittFalse = Crc16CcittFalse;
6
+ /**
7
+ * Lookup table of a reflected (LSB-first) CRC: entry n is the register after shifting byte n through
8
+ * the reversed polynomial eight times.
9
+ * @param reversedPolynomial
10
+ */
11
+ function ReflectedTable(reversedPolynomial) {
12
+ const table = new Uint32Array(256);
13
+ for (let n = 0; n < 256; n++) {
14
+ let register = n;
15
+ for (let bit = 0; bit < 8; bit++)
16
+ register = register & 1 ? (register >>> 1) ^ reversedPolynomial : register >>> 1;
17
+ table[n] = register >>> 0;
18
+ }
19
+ return table;
20
+ }
21
+ const CRC32_TABLE = ReflectedTable(0xEDB88320);
22
+ const CRC16_DNP_TABLE = ReflectedTable(0xA6BC);
23
+ /**
24
+ * CRC-32 as used by IEEE 802.3 / zlib (CRC-32/ISO-HDLC): polynomial 0x04C11DB7 processed reflected,
25
+ * initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF. Check value: Crc32('123456789') = 0xCBF43926.
26
+ * @param buffer
27
+ */
28
+ function Crc32(buffer) {
29
+ let register = 0xFFFFFFFF;
30
+ for (let i = 0; i < buffer.length; i++)
31
+ register = CRC32_TABLE[(register ^ buffer[i]) & 0xFF] ^ (register >>> 8);
32
+ return (register ^ 0xFFFFFFFF) >>> 0;
33
+ }
34
+ /**
35
+ * CRC-16/DNP (IEEE 1815 data link layer): polynomial 0x3D65 processed reflected, initial value 0,
36
+ * final XOR 0xFFFF. DNP3 sends it low octet first. Check value: Crc16Dnp('123456789') = 0xEA82.
37
+ * @param buffer
38
+ */
39
+ function Crc16Dnp(buffer) {
40
+ let register = 0;
41
+ for (let i = 0; i < buffer.length; i++)
42
+ register = CRC16_DNP_TABLE[(register ^ buffer[i]) & 0xFF] ^ (register >>> 8);
43
+ return (register ^ 0xFFFF) & 0xFFFF;
44
+ }
45
+ /**
46
+ * CRC-16/CCITT-FALSE (the IEEE C37.118.2 CHK): polynomial 0x1021 processed MSB-first, initial value
47
+ * 0xFFFF, no final XOR. Check value: Crc16CcittFalse('123456789') = 0x29B1.
48
+ * @param buffer
49
+ */
50
+ function Crc16CcittFalse(buffer) {
51
+ let register = 0xFFFF;
52
+ for (let i = 0; i < buffer.length; i++) {
53
+ register ^= buffer[i] << 8;
54
+ for (let bit = 0; bit < 8; bit++)
55
+ register = register & 0x8000 ? ((register << 1) ^ 0x1021) & 0xFFFF : (register << 1) & 0xFFFF;
56
+ }
57
+ return register;
58
+ }
@@ -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;