UNPKG

netkitty

Version:
225 lines (224 loc) 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ARP = void 0; const BaseHeader_1 = require("../abstracts/BaseHeader"); const NumberToHex_1 = require("../../helper/NumberToHex"); const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum"); const HexToNumber_1 = require("../../helper/HexToNumber"); const BufferToNumber_1 = require("../../helper/BufferToNumber"); const BufferToHex_1 = require("../../helper/BufferToHex"); const NumberToBuffer_1 = require("../../helper/NumberToBuffer"); const BufferToIP_1 = require("../../helper/BufferToIP"); const IPToBuffer_1 = require("../../helper/IPToBuffer"); class ARP extends BaseHeader_1.BaseHeader { constructor() { super(...arguments); this.SCHEMA = { type: 'object', properties: { hardware: { type: 'object', label: 'Hardware', properties: { type: { type: 'number', label: 'Type', minimum: 0, maximum: 65535, decode: () => { this.instance.hardware.type.setValue((0, BufferToNumber_1.BufferToInt16)(this.readBytes(0, 2))); }, encode: () => { let hwType = this.instance.hardware.type.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')); if (hwType > 65535) { this.recordError(this.instance.hardware.type.getPath(), 'Maximum value is 65535'); hwType = 65535; } if (hwType < 0) { this.recordError(this.instance.hardware.type.getPath(), 'Minimum value is 0'); hwType = 0; } this.instance.hardware.type.setValue(hwType); this.writeBytes(0, (0, NumberToBuffer_1.UInt16ToBuffer)(hwType)); } }, size: { type: 'number', label: 'Size', minimum: 0, maximum: 255, decode: () => { this.instance.hardware.size.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(4, 1))); }, encode: () => { let hwSize = this.instance.hardware.size.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')); if (hwSize > 255) { this.recordError(this.instance.hardware.size.getPath(), 'Maximum value is 255'); hwSize = 255; } if (hwSize < 0) { this.recordError(this.instance.hardware.size.getPath(), 'Minimum value is 0'); hwSize = 0; } this.instance.hardware.size.setValue(hwSize); this.writeBytes(4, (0, NumberToBuffer_1.UInt8ToBuffer)(hwSize)); } } } }, protocol: { type: 'object', label: 'Protocol', properties: { type: { type: 'string', label: 'Type', minLength: 4, maxLength: 4, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX, decode: () => { this.instance.protocol.type.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 2))); }, encode: () => { let protoTypeHex = this.instance.protocol.type.getValue('0000', (nodePath) => this.recordError(nodePath, 'Not Found')); let protoType = (0, HexToNumber_1.HexToUInt16)(protoTypeHex); if (isNaN(protoType)) { this.recordError(this.instance.protocol.type.getPath(), 'Invalid hex value'); protoType = 0; } this.instance.protocol.type.setValue(protoType); this.writeBytes(2, (0, NumberToBuffer_1.UInt16ToBuffer)(protoType)); } }, size: { type: 'number', label: 'Size', minimum: 0, maximum: 255, decode: () => { this.instance.protocol.size.setValue((0, BufferToNumber_1.BufferToUInt8)(this.readBytes(5, 1))); }, encode: () => { let protoSize = this.instance.protocol.size.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')); if (protoSize > 255) { this.recordError(this.instance.protocol.size.getPath(), 'Maximum value is 255'); protoSize = 255; } if (protoSize < 0) { this.recordError(this.instance.protocol.size.getPath(), 'Minimum value is 0'); protoSize = 0; } this.instance.protocol.size.setValue(protoSize); this.writeBytes(5, (0, NumberToBuffer_1.UInt8ToBuffer)(protoSize)); } } } }, opcode: { type: 'number', label: 'Opcode', enum: [1, 2, 3, 4], decode: () => { this.instance.opcode.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2))); if (![1, 2, 3, 4].includes(this.instance.opcode.getValue())) this.recordError(this.instance.opcode.getPath(), 'Opcode should be 1, 2, 3 or 4'); }, encode: () => { let opcode = this.instance.opcode.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')); if (![1, 2, 3, 4].includes(opcode)) this.recordError(this.instance.opcode.getPath(), 'Opcode should be 1, 2, 3 or 4'); this.instance.opcode.setValue(opcode); this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(opcode)); } }, sender: { type: 'object', label: 'Sender', properties: { mac: { type: 'string', label: 'MAC address', minLength: 17, maxLength: 17, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8, decode: () => { const macAddrBuffer = Buffer.alloc(6, this.readBytes(8, 6)); this.instance.sender.mac.setValue(Array.from(macAddrBuffer).map((value) => (0, NumberToHex_1.UInt8ToHex)(value)).join(':')); }, encode: () => { let macStr = this.instance.sender.mac.getValue(); const rawMacBuffer = Buffer.from(macStr.split(':').map((value) => parseInt(value, 16)).map((value) => value ? value : 0)); if (rawMacBuffer.length !== 6) this.recordError(this.instance.sender.mac.getPath(), 'Invalid MAC address length'); this.writeBytes(8, Buffer.alloc(6, rawMacBuffer)); } }, ipv4: { type: 'string', label: 'IP address', minLength: 7, maxLength: 15, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8, decode: () => { const ipv4Buffer = Buffer.alloc(4, this.readBytes(14, 4)); this.instance.sender.ipv4.setValue((0, BufferToIP_1.BufferToIPv4)(ipv4Buffer)); }, encode: () => { let ipv4Str = this.instance.sender.ipv4.getValue(); this.writeBytes(14, (0, IPToBuffer_1.IPv4ToBuffer)(ipv4Str)); } } } }, target: { type: 'object', label: 'Target', properties: { mac: { type: 'string', label: 'MAC address', minLength: 17, maxLength: 17, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8, decode: () => { const macAddrBuffer = Buffer.alloc(6, this.readBytes(18, 6)); this.instance.target.mac.setValue(Array.from(macAddrBuffer).map((value) => (0, NumberToHex_1.UInt8ToHex)(value)).join(':')); }, encode: () => { let macStr = this.instance.target.mac.getValue(); const rawMacBuffer = Buffer.from(macStr.split(':').map((value) => parseInt(value, 16)).map((value) => value ? value : 0)); if (rawMacBuffer.length !== 6) this.recordError(this.instance.target.mac.getPath(), 'Invalid MAC address length'); this.writeBytes(18, Buffer.alloc(6, rawMacBuffer)); } }, ipv4: { type: 'string', label: 'IP address', minLength: 7, maxLength: 15, contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8, decode: () => { const ipv4Buffer = Buffer.alloc(4, this.readBytes(24, 4)); this.instance.target.ipv4.setValue((0, BufferToIP_1.BufferToIPv4)(ipv4Buffer)); }, encode: () => { let ipv4Str = this.instance.target.ipv4.getValue(); this.writeBytes(24, (0, IPToBuffer_1.IPv4ToBuffer)(ipv4Str)); } } } } } }; this.id = 'arp'; this.name = 'Address Resolution Protocol'; this.nickname = 'ARP'; } match() { if (!this.prevCodecModule) return false; return this.prevCodecModule.instance.etherType.getValue() === (0, NumberToHex_1.UInt16ToHex)(0x0806); } } exports.ARP = ARP;