netkitty
Version:
Network tools kit
129 lines (128 loc) • 5.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ICMPv6 = void 0;
const BaseHeader_1 = require("../abstracts/BaseHeader");
const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
const BufferToNumber_1 = require("../../helper/BufferToNumber");
const NumberToBuffer_1 = require("../../helper/NumberToBuffer");
const BufferToHex_1 = require("../../helper/BufferToHex");
const IPToBuffer_1 = require("../../helper/IPToBuffer");
class ICMPv6 extends BaseHeader_1.BaseHeader {
constructor() {
super(...arguments);
this.SCHEMA = {
type: 'object',
properties: {
type: {
type: 'integer',
label: 'Type',
minimum: 0,
maximum: 255,
decode: () => {
this.instance.type.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(0, 1)));
},
encode: () => {
const type = this.instance.type.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
this.instance.type.setValue(type);
this.writeBytes(0, (0, NumberToBuffer_1.UInt8ToBuffer)(type));
}
},
code: {
type: 'integer',
label: 'Code',
minimum: 0,
maximum: 255,
decode: () => {
this.instance.code.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(1, 1)));
},
encode: () => {
const code = this.instance.code.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found'));
this.instance.code.setValue(code);
this.writeBytes(1, (0, NumberToBuffer_1.UInt8ToBuffer)(code));
}
},
checksum: {
type: 'integer',
label: 'Checksum',
minimum: 0,
maximum: 65535,
decode: () => {
this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
},
encode: () => {
let checksum = this.instance.checksum.getValue(0);
this.instance.checksum.setValue(checksum);
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
if (!checksum) {
checksum = this.calculateIcmpv6Checksum();
this.instance.checksum.setValue(checksum);
this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
}
}
},
message: {
type: 'string',
label: 'Message Body',
contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
decode: () => {
const messageDataLength = this.packet.length - this.startPos - 4;
this.instance.message.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(4, messageDataLength)));
},
encode: () => {
const messageHex = this.instance.message.getValue('');
this.instance.message.setValue(messageHex);
this.writeBytes(4, Buffer.from(messageHex, 'hex'));
}
}
}
};
this.id = 'icmpv6';
this.name = 'Internet Control Message Protocol v6';
this.nickname = 'ICMPv6';
}
/**
* Calculate ICMPv6 checksum
* @protected
*/
calculateIcmpv6Checksum() {
const ipv6Header = this.prevCodecModules.find((prevCodecModule) => prevCodecModule.id === 'ipv6');
if (!ipv6Header)
return 0;
const messageBody = Buffer.from(this.instance.message.getValue(''), 'hex');
const sip = ipv6Header.instance.sip.getValue('0000:0000:0000:0000:0000:0000:0000:0000');
const dip = ipv6Header.instance.dip.getValue('0000:0000:0000:0000:0000:0000:0000:0000');
const pseudoHeader = Buffer.concat([
(0, IPToBuffer_1.IPv6ToBuffer)(sip),
(0, IPToBuffer_1.IPv6ToBuffer)(dip),
(0, NumberToBuffer_1.UInt32ToBuffer)(messageBody.length + 4),
(0, NumberToBuffer_1.UInt8ToBuffer)(0x00),
(0, NumberToBuffer_1.UInt8ToBuffer)(0x00),
(0, NumberToBuffer_1.UInt8ToBuffer)(0x00),
(0, NumberToBuffer_1.UInt8ToBuffer)(0x3a)
]);
const icmpHeader = Buffer.concat([
(0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.type.getValue(0)),
(0, NumberToBuffer_1.UInt8ToBuffer)(this.instance.code.getValue(0)),
(0, NumberToBuffer_1.UInt16ToBuffer)(this.instance.checksum.getValue(0))
]);
const buffer = Buffer.concat([
pseudoHeader,
icmpHeader,
messageBody
]);
const paddedBuffer = buffer.length % 2 === 1 ? Buffer.concat([buffer, (0, NumberToBuffer_1.UInt8ToBuffer)(0)]) : buffer;
let checksum = 0;
for (let i = 0; i < paddedBuffer.length; i += 2) {
checksum += (paddedBuffer[i] << 8) | (paddedBuffer[i + 1] || 0);
if (checksum > 0xFFFF)
checksum = (checksum & 0xFFFF) + 1;
}
return (~checksum) & 0xFFFF;
}
match() {
if (!this.prevCodecModule)
return false;
return this.prevCodecModule.instance.nxt.getValue() === 0x3a;
}
}
exports.ICMPv6 = ICMPv6;