netkitty 0.0.14 → 0.0.16

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 (38) hide show
  1. package/.idea/inspectionProfiles/Project_Default.xml +1 -1
  2. package/dist/NetKitty.d.ts +12 -0
  3. package/dist/NetKitty.d.ts.map +1 -1
  4. package/dist/NetKitty.js +74 -1
  5. package/dist/lib/codec/Codec.d.ts.map +1 -1
  6. package/dist/lib/codec/Codec.js +8 -0
  7. package/dist/lib/codec/abstracts/BaseHeader.d.ts +2 -1
  8. package/dist/lib/codec/abstracts/BaseHeader.d.ts.map +1 -1
  9. package/dist/lib/codec/abstracts/BaseHeader.js +19 -10
  10. package/dist/lib/codec/headers/Goose.d.ts.map +1 -1
  11. package/dist/lib/codec/headers/Goose.js +26 -8
  12. package/dist/lib/codec/headers/ICMP.d.ts +15 -0
  13. package/dist/lib/codec/headers/ICMP.d.ts.map +1 -0
  14. package/dist/lib/codec/headers/ICMP.js +147 -0
  15. package/dist/lib/codec/headers/ICMPv6.d.ts +15 -0
  16. package/dist/lib/codec/headers/ICMPv6.d.ts.map +1 -0
  17. package/dist/lib/codec/headers/ICMPv6.js +128 -0
  18. package/dist/lib/codec/headers/IPv6.d.ts +10 -0
  19. package/dist/lib/codec/headers/IPv6.d.ts.map +1 -0
  20. package/dist/lib/codec/headers/IPv6.js +177 -0
  21. package/dist/lib/codec/headers/IPv6HopByHopOptions.d.ts +71 -0
  22. package/dist/lib/codec/headers/IPv6HopByHopOptions.d.ts.map +1 -0
  23. package/dist/lib/codec/headers/IPv6HopByHopOptions.js +497 -0
  24. package/dist/lib/codec/headers/TCP.d.ts.map +1 -1
  25. package/dist/lib/codec/headers/TCP.js +1 -0
  26. package/dist/tests/units/Codec.spec.js +19 -21
  27. package/package.json +1 -1
  28. package/src/NetKitty.ts +61 -0
  29. package/src/lib/codec/Codec.ts +8 -0
  30. package/src/lib/codec/abstracts/BaseHeader.ts +17 -9
  31. package/src/lib/codec/headers/Goose.ts +21 -9
  32. package/src/lib/codec/headers/ICMP.ts +151 -0
  33. package/src/lib/codec/headers/ICMPv6.ts +128 -0
  34. package/src/lib/codec/headers/IPv6.ts +176 -0
  35. package/src/lib/codec/headers/IPv6HopByHopOptions.ts +532 -0
  36. package/src/lib/codec/headers/TCP.ts +1 -0
  37. package/src/tests/units/Codec.spec.ts +21 -21
  38. package/src/tests/units/ReadPCAP.spec.ts +1 -2
@@ -19,13 +19,21 @@ import {RawData} from './headers/RawData'
19
19
  import {TCP} from './headers/TCP'
20
20
  import {UDP} from './headers/UDP'
21
21
  import {VLAN_802dot1Q} from './headers/VLAN_802dot1Q'
22
+ import {IPv6} from './headers/IPv6'
23
+ import {IPv6HopByHopOptions} from './headers/IPv6HopByHopOptions'
24
+ import {ICMPv6} from './headers/ICMPv6'
25
+ import {ICMP} from './headers/ICMP'
22
26
 
23
27
  export const Headers: CodecModuleConstructor[] = [
24
28
  ARP,
25
29
  EthernetII,
26
30
  Goose,
27
31
  IEC61850SampledValues,
32
+ ICMP,
28
33
  IPv4,
34
+ IPv6,
35
+ IPv6HopByHopOptions,
36
+ ICMPv6,
29
37
  RawData,
30
38
  TCP,
31
39
  UDP,
@@ -211,14 +211,21 @@ export abstract class BaseHeader {
211
211
  * @param offset
212
212
  * @param length
213
213
  * @param expandBufferLength
214
+ * @param changeHeaderLength
214
215
  * @private
215
216
  */
216
- #readBytes(offset: number, length: number, expandBufferLength: boolean): Buffer {
217
+ #readBytes(offset: number, length: number, expandBufferLength: boolean, changeHeaderLength: boolean): Buffer {
217
218
  const packetOffset: number = this.getPacketOffset(offset)
218
- const readEndPos: number = packetOffset + length
219
- if (this.packet.length < readEndPos && expandBufferLength) this.packet = Buffer.concat([this.packet, Buffer.alloc(readEndPos - this.packet.length, 0)])
219
+ let readEndPos: number = packetOffset + length
220
+ if (this.packet.length < readEndPos) {
221
+ if (expandBufferLength) {
222
+ this.packet = Buffer.concat([this.packet, Buffer.alloc(readEndPos - this.packet.length, 0)])
223
+ } else {
224
+ readEndPos = this.packet.length
225
+ }
226
+ }
220
227
  const headerLength: number = readEndPos - this.startPos
221
- this.headerLength = this.headerLength < headerLength ? headerLength : this.headerLength
228
+ if (changeHeaderLength) this.headerLength = this.headerLength < headerLength ? headerLength : this.headerLength
222
229
  return this.packet.subarray(packetOffset, packetOffset + length)
223
230
  }
224
231
 
@@ -226,10 +233,11 @@ export abstract class BaseHeader {
226
233
  * Read bytes from buffer
227
234
  * @param offset
228
235
  * @param length
236
+ * @param dryRun
229
237
  * @protected
230
238
  */
231
- protected readBytes(offset: number, length: number): Buffer {
232
- return this.#readBytes(offset, length, false)
239
+ protected readBytes(offset: number, length: number, dryRun: boolean = false): Buffer {
240
+ return this.#readBytes(offset, length, false, !dryRun)
233
241
  }
234
242
 
235
243
  /**
@@ -241,7 +249,7 @@ export abstract class BaseHeader {
241
249
  protected writeBytes(offset: number, buffer: Buffer): void {
242
250
  const packetOffset: number = this.getPacketOffset(offset)
243
251
  const writeEndPos: number = packetOffset + buffer.length
244
- this.#readBytes(offset, buffer.length, true)
252
+ this.#readBytes(offset, buffer.length, true, true)
245
253
  this.packet.fill(buffer, packetOffset, writeEndPos)
246
254
  }
247
255
 
@@ -254,7 +262,7 @@ export abstract class BaseHeader {
254
262
  * @protected
255
263
  */
256
264
  protected readBits(offset: number, length: number, bitOffset: number, bitLength: number): number {
257
- const buffer: Buffer = this.#readBytes(offset, length, false)
265
+ const buffer: Buffer = this.#readBytes(offset, length, false, true)
258
266
  const bitString: string = parseInt(buffer.toString('hex'), 16).toString(2).padStart(length * 8, '0')
259
267
  return parseInt(bitString.substring(bitOffset, bitOffset + bitLength), 2)
260
268
  }
@@ -269,7 +277,7 @@ export abstract class BaseHeader {
269
277
  * @protected
270
278
  */
271
279
  protected writeBits(offset: number, length: number, bitOffset: number, bitLength: number, value: number): void {
272
- const buffer: Buffer = this.#readBytes(offset, length, true)
280
+ const buffer: Buffer = this.#readBytes(offset, length, true, true)
273
281
  let bitArray: string[] = Array.from(parseInt(buffer.toString('hex'), 16).toString(2).padStart(buffer.length * 8, '0'))
274
282
  const valueBitArray: string[] = Array.from(value.toString(2).padStart(bitLength, '0'))
275
283
  bitArray = bitArray.map((bit: string, index: number): string => {
@@ -128,9 +128,18 @@ export class Goose extends BaseHeader {
128
128
  type: 'object',
129
129
  label: 'GOOSE PDU',
130
130
  decode: (): void => {
131
- const buffer: Buffer = this.readBytes(8, (this.instance.length.getValue() as number) - 8)
132
- this.TLVInstance = TLV.parse(buffer)
133
- this.TLVChild = this.TLVInstance.getChild()
131
+ let redTLVBufferLength: number = (this.instance.length.getValue() as number) - 8
132
+ while (!this.TLVInstance) {
133
+ try {
134
+ const buffer: Buffer = this.readBytes(8, redTLVBufferLength, true)
135
+ this.TLVInstance = TLV.parse(buffer)
136
+ this.readBytes(8, this.TLVInstance.length, false)//Update header length
137
+ } catch (e) {
138
+ redTLVBufferLength -= 1
139
+ if (!redTLVBufferLength) break
140
+ }
141
+ }
142
+ this.TLVChild = this.TLVInstance ? this.TLVInstance.getChild() : []
134
143
  },
135
144
  encode: (): void => {
136
145
  let buffers: Buffer = Buffer.from([])
@@ -260,6 +269,7 @@ export class Goose extends BaseHeader {
260
269
  },
261
270
  encode: (): void => {
262
271
  let stNumValue: number = this.instance.goosePdu.stNum.getValue(-1, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
272
+ if (stNumValue < 0) return
263
273
  if (stNumValue < 1 || stNumValue > 4294967295) {
264
274
  stNumValue = 4294967295
265
275
  this.recordError(this.instance.goosePdu.stNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295')
@@ -281,6 +291,7 @@ export class Goose extends BaseHeader {
281
291
  },
282
292
  encode: (): void => {
283
293
  let sqNumValue: number = this.instance.goosePdu.sqNum.getValue(-1, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
294
+ if (sqNumValue < 0) return
284
295
  if (sqNumValue < 0 || sqNumValue > 4294967295) {
285
296
  sqNumValue = 4294967295
286
297
  this.recordError(this.instance.goosePdu.sqNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295')
@@ -516,7 +527,7 @@ export class Goose extends BaseHeader {
516
527
  if (!dataItem.dataType) return
517
528
  allData.push(dataItem)
518
529
  })
519
- this.instance.goosePdu.allData.setValue(allData)
530
+ if (allData.length) this.instance.goosePdu.allData.setValue(allData)
520
531
  },
521
532
  encode: (): void => {
522
533
  const allData: AllDataItem[] = this.instance.goosePdu.allData.getValue([], (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
@@ -635,11 +646,12 @@ export class Goose extends BaseHeader {
635
646
  }
636
647
  })
637
648
  .filter((dataItemTLV: TLV | null): dataItemTLV is TLV => !!dataItemTLV)
638
-
639
- let dataItemsHex: string = ''
640
- dataItemTLVs.forEach(dataItemTLV => dataItemsHex = `${dataItemsHex}${dataItemTLV.toString()}`)
641
- const allDataTLV: TLV = new TLV(0xAB, dataItemsHex)
642
- this.TLVChild.push(allDataTLV)
649
+ if (dataItemTLVs.length) {
650
+ let dataItemsHex: string = ''
651
+ dataItemTLVs.forEach(dataItemTLV => dataItemsHex = `${dataItemsHex}${dataItemTLV.toString()}`)
652
+ const allDataTLV: TLV = new TLV(0xAB, dataItemsHex)
653
+ this.TLVChild.push(allDataTLV)
654
+ }
643
655
  }
644
656
  }
645
657
  }
@@ -0,0 +1,151 @@
1
+ import {ProtocolJSONSchema} from '../../schema/ProtocolJSONSchema'
2
+ import {BaseHeader} from '../abstracts/BaseHeader'
3
+ import {BufferToUInt16, BufferToUInt8} from '../lib/BufferToNumber'
4
+ import {UInt16ToBuffer, UInt8ToBuffer} from '../lib/NumberToBuffer'
5
+ import {StringContentEncodingEnum} from '../lib/StringContentEncodingEnum'
6
+ import {BufferToHex} from '../lib/BufferToHex'
7
+
8
+ export class ICMP extends BaseHeader {
9
+
10
+ /**
11
+ * Calculate ICMPv4 checksum
12
+ * @protected
13
+ */
14
+ protected calculateIcmpChecksum(): number {
15
+ const icmpHeader: Buffer = Buffer.concat([
16
+ UInt8ToBuffer(this.instance.type.getValue(0)),
17
+ UInt8ToBuffer(this.instance.code.getValue(0)),
18
+ UInt16ToBuffer(this.instance.checksum.getValue(0)),
19
+ UInt16ToBuffer(this.instance.ident.getValue(0)),
20
+ UInt16ToBuffer(this.instance.seq.getValue(0)),
21
+ Buffer.from(this.instance.message.getValue(''), 'hex')
22
+ ])
23
+ const paddedBuffer: Buffer = icmpHeader.length % 2 === 1 ?
24
+ Buffer.concat([
25
+ icmpHeader,
26
+ UInt8ToBuffer(0)
27
+ ]) : icmpHeader
28
+
29
+ // 4. 计算 checksum
30
+ let checksum = 0
31
+ for (let i = 0; i < paddedBuffer.length; i += 2) {
32
+ const word = (paddedBuffer[i] << 8) | (paddedBuffer[i + 1] || 0)
33
+ checksum += word
34
+
35
+ // 处理溢出
36
+ if (checksum > 0xFFFF) {
37
+ checksum = (checksum & 0xFFFF) + 1
38
+ }
39
+ }
40
+
41
+ // 取反得到最终 checksum
42
+ return (~checksum) & 0xFFFF
43
+ }
44
+
45
+ public SCHEMA: ProtocolJSONSchema = {
46
+ type: 'object',
47
+ properties: {
48
+ type: {
49
+ type: 'integer',
50
+ label: 'Type',
51
+ minimum: 0,
52
+ maximum: 255,
53
+ decode: (): void => {
54
+ this.instance.type.setValue(BufferToUInt8(this.readBytes(0, 1)))
55
+ },
56
+ encode: (): void => {
57
+ const type: number = this.instance.type.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
58
+ this.instance.type.setValue(type)
59
+ this.writeBytes(0, UInt8ToBuffer(type))
60
+ }
61
+ },
62
+ code: {
63
+ type: 'integer',
64
+ label: 'Code',
65
+ minimum: 0,
66
+ maximum: 255,
67
+ decode: (): void => {
68
+ this.instance.code.setValue(BufferToUInt8(this.readBytes(1, 1)))
69
+ },
70
+ encode: (): void => {
71
+ const code: number = this.instance.code.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
72
+ this.instance.code.setValue(code)
73
+ this.writeBytes(1, UInt8ToBuffer(code))
74
+ }
75
+ },
76
+ checksum: {
77
+ type: 'integer',
78
+ label: 'Checksum',
79
+ minimum: 0,
80
+ maximum: 65535,
81
+ decode: (): void => {
82
+ this.instance.checksum.setValue(BufferToUInt16(this.readBytes(2, 2)))
83
+ },
84
+ encode: (): void => {
85
+ let checksum: number = this.instance.checksum.getValue(0)
86
+ this.instance.checksum.setValue(checksum)
87
+ this.writeBytes(2, UInt16ToBuffer(checksum))
88
+ if (!checksum) {
89
+ checksum = this.calculateIcmpChecksum()
90
+ this.instance.checksum.setValue(checksum)
91
+ this.writeBytes(2, UInt16ToBuffer(checksum))
92
+ }
93
+ }
94
+ },
95
+ ident: {
96
+ type: 'number',
97
+ label: 'Identifier (BE)',
98
+ minimum: 0,
99
+ maximum: 65535,
100
+ decode: (): void => {
101
+ this.instance.ident.setValue(BufferToUInt16(this.readBytes(4, 2)))
102
+ },
103
+ encode: (): void => {
104
+ const ident: number = this.instance.ident.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
105
+ this.instance.ident.setValue(ident)
106
+ this.writeBytes(4, UInt16ToBuffer(ident))
107
+ }
108
+ },
109
+ seq: {
110
+ type: 'number',
111
+ label: 'Sequence Number (BE)',
112
+ minimum: 0,
113
+ maximum: 65535,
114
+ decode: (): void => {
115
+ this.instance.seq.setValue(BufferToUInt16(this.readBytes(6, 2)))
116
+ },
117
+ encode: (): void => {
118
+ const seq: number = this.instance.seq.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
119
+ this.instance.seq.setValue(seq)
120
+ this.writeBytes(6, UInt16ToBuffer(seq))
121
+ }
122
+ },
123
+ message: {
124
+ type: 'string',
125
+ label: 'Message Body',
126
+ contentEncoding: StringContentEncodingEnum.HEX,
127
+ decode: (): void => {
128
+ const messageDataLength: number = this.packet.length - this.startPos - 8
129
+ this.instance.message.setValue(BufferToHex(this.readBytes(8, messageDataLength)))
130
+ },
131
+ encode: (): void => {
132
+ const messageHex: string = this.instance.message.getValue('')
133
+ this.instance.message.setValue(messageHex)
134
+ this.writeBytes(8, Buffer.from(messageHex, 'hex'))
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ public id: string = 'icmp'
141
+
142
+ public name: string = 'Internet Control Message Protocol'
143
+
144
+ public nickname: string = 'ICMP'
145
+
146
+ public match(): boolean {
147
+ if (!this.prevCodecModule) return false
148
+ return this.prevCodecModule.instance.protocol.getValue() === 0x01
149
+ }
150
+
151
+ }
@@ -0,0 +1,128 @@
1
+ import {ProtocolJSONSchema} from '../../schema/ProtocolJSONSchema'
2
+ import {BaseHeader} from '../abstracts/BaseHeader'
3
+ import {StringContentEncodingEnum} from '../lib/StringContentEncodingEnum'
4
+ import {BufferToUInt16, BufferToUInt8} from '../lib/BufferToNumber'
5
+ import {UInt16ToBuffer, UInt32ToBuffer, UInt8ToBuffer} from '../lib/NumberToBuffer'
6
+ import {BufferToHex} from '../lib/BufferToHex'
7
+ import {CodecModule} from '../types/CodecModule'
8
+ import {IPv6ToBuffer} from '../lib/IPToBuffer'
9
+
10
+ export class ICMPv6 extends BaseHeader {
11
+
12
+ /**
13
+ * Calculate ICMPv6 checksum
14
+ * @protected
15
+ */
16
+ protected calculateIcmpv6Checksum(): number {
17
+ const ipv6Header: CodecModule | undefined = this.prevCodecModules.find((prevCodecModule: CodecModule): boolean => prevCodecModule.id === 'ipv6')
18
+ if (!ipv6Header) return 0
19
+ const messageBody: Buffer = Buffer.from(this.instance.message.getValue(''), 'hex')
20
+ const sip: string = ipv6Header.instance.sip.getValue('0000:0000:0000:0000:0000:0000:0000:0000')
21
+ const dip: string = ipv6Header.instance.dip.getValue('0000:0000:0000:0000:0000:0000:0000:0000')
22
+ const pseudoHeader: Buffer = Buffer.concat([
23
+ IPv6ToBuffer(sip),
24
+ IPv6ToBuffer(dip),
25
+ UInt32ToBuffer(messageBody.length + 4),
26
+ UInt8ToBuffer(0x00),
27
+ UInt8ToBuffer(0x00),
28
+ UInt8ToBuffer(0x00),
29
+ UInt8ToBuffer(0x3a)
30
+ ])
31
+
32
+ const icmpHeader: Buffer = Buffer.concat([
33
+ UInt8ToBuffer(this.instance.type.getValue(0)),
34
+ UInt8ToBuffer(this.instance.code.getValue(0)),
35
+ UInt16ToBuffer(this.instance.checksum.getValue(0))
36
+ ])
37
+ const buffer: Buffer = Buffer.concat([
38
+ pseudoHeader,
39
+ icmpHeader,
40
+ messageBody
41
+ ])
42
+ const paddedBuffer: Buffer = buffer.length % 2 === 1 ? Buffer.concat([buffer, UInt8ToBuffer(0)]) : buffer
43
+ let checksum: number = 0
44
+ for (let i: number = 0; i < paddedBuffer.length; i += 2) {
45
+ checksum += (paddedBuffer[i] << 8) | (paddedBuffer[i + 1] || 0)
46
+ if (checksum > 0xFFFF) checksum = (checksum & 0xFFFF) + 1
47
+ }
48
+ return (~checksum) & 0xFFFF
49
+ }
50
+
51
+ public SCHEMA: ProtocolJSONSchema = {
52
+ type: 'object',
53
+ properties: {
54
+ type: {
55
+ type: 'integer',
56
+ label: 'Type',
57
+ minimum: 0,
58
+ maximum: 255,
59
+ decode: (): void => {
60
+ this.instance.type.setValue(BufferToUInt8(this.readBytes(0, 1)))
61
+ },
62
+ encode: (): void => {
63
+ const type: number = this.instance.type.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
64
+ this.instance.type.setValue(type)
65
+ this.writeBytes(0, UInt8ToBuffer(type))
66
+ }
67
+ },
68
+ code: {
69
+ type: 'integer',
70
+ label: 'Code',
71
+ minimum: 0,
72
+ maximum: 255,
73
+ decode: (): void => {
74
+ this.instance.code.setValue(BufferToUInt8(this.readBytes(1, 1)))
75
+ },
76
+ encode: (): void => {
77
+ const code: number = this.instance.code.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
78
+ this.instance.code.setValue(code)
79
+ this.writeBytes(1, UInt8ToBuffer(code))
80
+ }
81
+ },
82
+ checksum: {
83
+ type: 'integer',
84
+ label: 'Checksum',
85
+ minimum: 0,
86
+ maximum: 65535,
87
+ decode: (): void => {
88
+ this.instance.checksum.setValue(BufferToUInt16(this.readBytes(2, 2)))
89
+ },
90
+ encode: (): void => {
91
+ let checksum: number = this.instance.checksum.getValue(0)
92
+ this.instance.checksum.setValue(checksum)
93
+ this.writeBytes(2, UInt16ToBuffer(checksum))
94
+ if (!checksum) {
95
+ checksum = this.calculateIcmpv6Checksum()
96
+ this.instance.checksum.setValue(checksum)
97
+ this.writeBytes(2, UInt16ToBuffer(checksum))
98
+ }
99
+ }
100
+ },
101
+ message: {
102
+ type: 'string',
103
+ label: 'Message Body',
104
+ contentEncoding: StringContentEncodingEnum.HEX,
105
+ decode: (): void => {
106
+ const messageDataLength: number = this.packet.length - this.startPos - 4
107
+ this.instance.message.setValue(BufferToHex(this.readBytes(4, messageDataLength)))
108
+ },
109
+ encode: (): void => {
110
+ const messageHex: string = this.instance.message.getValue('')
111
+ this.instance.message.setValue(messageHex)
112
+ this.writeBytes(4, Buffer.from(messageHex, 'hex'))
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ public id: string = 'icmpv6'
119
+
120
+ public name: string = 'Internet Control Message Protocol v6'
121
+
122
+ public nickname: string = 'ICMPv6'
123
+
124
+ public match(): boolean {
125
+ if (!this.prevCodecModule) return false
126
+ return this.prevCodecModule.instance.nxt.getValue() === 0x3a
127
+ }
128
+ }
@@ -0,0 +1,176 @@
1
+ import {ProtocolJSONSchema} from '../../schema/ProtocolJSONSchema'
2
+ import {BaseHeader} from '../abstracts/BaseHeader'
3
+ import {UInt16ToHex} from '../lib/NumberToHex'
4
+ import {StringContentEncodingEnum} from '../lib/StringContentEncodingEnum'
5
+ import {BufferToUInt16, BufferToUInt8} from '../lib/BufferToNumber'
6
+ import {UInt16ToBuffer, UInt8ToBuffer} from '../lib/NumberToBuffer'
7
+ import {BufferToIPv6} from '../lib/BufferToIP'
8
+ import {IPv6ToBuffer} from '../lib/IPToBuffer'
9
+ import {CodecModule} from '../types/CodecModule'
10
+
11
+ export class IPv6 extends BaseHeader {
12
+
13
+ public SCHEMA: ProtocolJSONSchema = {
14
+ type: 'object',
15
+ properties: {
16
+ version: {
17
+ type: 'integer',
18
+ label: 'Version',
19
+ minimum: 0,
20
+ maximum: 15,
21
+ decode: (): void => {
22
+ this.instance.version.setValue(this.readBits(0, 1, 0, 4))
23
+ },
24
+ encode: (): void => {
25
+ const version: number = this.instance.version.getValue(6, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
26
+ this.instance.version.setValue(version)
27
+ this.writeBits(0, 1, 0, 4, version)
28
+ }
29
+ },
30
+ tclass: {
31
+ type: 'object',
32
+ label: 'Traffic Class',
33
+ properties: {
34
+ dscp: {
35
+ type: 'integer',
36
+ minimum: 0,
37
+ maximum: 63,
38
+ label: 'Differentiated Services Codepoint',
39
+ decode: (): void => {
40
+ this.instance.tclass.dscp.setValue(this.readBits(0, 4, 4, 6))
41
+ },
42
+ encode: (): void => {
43
+ const dscp: number = this.instance.tclass.dscp.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
44
+ this.instance.tclass.dscp.setValue(dscp)
45
+ this.writeBits(0, 4, 4, 6, dscp)
46
+ }
47
+ },
48
+ ecn: {
49
+ type: 'integer',
50
+ label: 'Explicit Congestion Notification',
51
+ minimum: 0,
52
+ maximum: 3,
53
+ decode: (): void => {
54
+ this.instance.tclass.ecn.setValue(this.readBits(0, 4, 10, 2))
55
+ },
56
+ encode: (): void => {
57
+ const ecn: number = this.instance.tclass.ecn.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
58
+ this.instance.tclass.ecn.setValue(ecn)
59
+ this.writeBits(0, 4, 10, 2, ecn)
60
+ }
61
+ }
62
+ }
63
+ },
64
+ flow: {
65
+ type: 'integer',
66
+ label: 'Flow Label',
67
+ minimum: 0,
68
+ maximum: 1048575,
69
+ decode: (): void => {
70
+ this.instance.flow.setValue(this.readBits(1, 3, 4, 20))
71
+ },
72
+ encode: (): void => {
73
+ const flow: number = this.instance.flow.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
74
+ this.instance.flow.setValue(flow)
75
+ this.writeBits(1, 3, 4, 20, flow)
76
+ }
77
+ },
78
+ plen: {
79
+ type: 'integer',
80
+ label: 'Payload Length',
81
+ minimum: 0,
82
+ maximum: 65535,
83
+ decode: (): void => {
84
+ this.instance.plen.setValue(BufferToUInt16(this.readBytes(4, 2)))
85
+ },
86
+ encode: (): void => {
87
+ const plen: number = this.instance.plen.getValue(0)
88
+ this.instance.plen.setValue(plen)
89
+ this.writeBytes(4, UInt16ToBuffer(plen))
90
+ if (!plen) {
91
+ this.addPostPacketEncodeHandler((): void => {
92
+ let startCount: boolean = false
93
+ let totalLength: number = 0
94
+ this.codecModules.forEach((codecModule: CodecModule): void => {
95
+ if (codecModule === this) startCount = true
96
+ if (startCount) totalLength += codecModule.length
97
+ })
98
+ let payloadLength: number = totalLength - 40
99
+ //The length is set to zero when a Hop-by-Hop extension header carries a Jumbo Payload option
100
+ if (payloadLength > 65535) payloadLength = 0
101
+ this.instance.plen.setValue(payloadLength)
102
+ this.writeBytes(4, UInt16ToBuffer(payloadLength))
103
+
104
+ })
105
+ }
106
+ }
107
+ },
108
+ nxt: {
109
+ type: 'integer',
110
+ label: 'Next Header',
111
+ minimum: 0,
112
+ maximum: 255,
113
+ decode: (): void => {
114
+ this.instance.nxt.setValue(BufferToUInt8(this.readBytes(6, 1)))
115
+ },
116
+ encode: (): void => {
117
+ const nxt: number = this.instance.nxt.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
118
+ this.instance.nxt.setValue(nxt)
119
+ this.writeBytes(6, UInt8ToBuffer(nxt))
120
+ }
121
+ },
122
+ hllm: {
123
+ type: 'integer',
124
+ label: 'Hop Limit',
125
+ minimum: 0,
126
+ maximum: 255,
127
+ decode: (): void => {
128
+ this.instance.hllm.setValue(BufferToUInt8(this.readBytes(7, 1)))
129
+ },
130
+ encode: (): void => {
131
+ const hllm: number = this.instance.hllm.getValue(0, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
132
+ this.instance.hllm.setValue(hllm)
133
+ this.writeBytes(7, UInt8ToBuffer(hllm))
134
+ }
135
+ },
136
+ sip: {
137
+ type: 'string',
138
+ label: 'Source Address',
139
+ contentEncoding: StringContentEncodingEnum.IPv6,
140
+ decode: (): void => {
141
+ this.instance.sip.setValue(BufferToIPv6(this.readBytes(8, 16)))
142
+ },
143
+ encode: (): void => {
144
+ const sip: string = this.instance.sip.getValue('0000:0000:0000:0000:0000:0000:0000:0000', (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
145
+ this.instance.sip.setValue(sip)
146
+ this.writeBytes(8, IPv6ToBuffer(sip))
147
+ }
148
+ },
149
+ dip: {
150
+ type: 'string',
151
+ label: 'Destination Address',
152
+ contentEncoding: StringContentEncodingEnum.IPv6,
153
+ decode: (): void => {
154
+ this.instance.dip.setValue(BufferToIPv6(this.readBytes(24, 16)))
155
+ },
156
+ encode: (): void => {
157
+ const dip: string = this.instance.dip.getValue('0000:0000:0000:0000:0000:0000:0000:0000', (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
158
+ this.instance.dip.setValue(dip)
159
+ this.writeBytes(24, IPv6ToBuffer(dip))
160
+ }
161
+ }
162
+ }
163
+ }
164
+
165
+ public id: string = 'ipv6'
166
+
167
+ public name: string = 'Internet Protocol Version 6'
168
+
169
+ public nickname: string = 'IPv6'
170
+
171
+ public match(): boolean {
172
+ if (!this.prevCodecModule) return false
173
+ return this.prevCodecModule.instance.etherType.getValue() === UInt16ToHex(0x86dd)
174
+ }
175
+
176
+ }