netkitty 0.0.15 → 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.
@@ -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
+ }