|
| 1 | +/** |
| 2 | + * @author tedk [tedk@ted.do] |
| 3 | + * @copyright Crown Copyright 2024 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import Utils from "../Utils.mjs"; |
| 10 | +import {fromHex, toHex} from "../lib/Hex.mjs"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Parse Ethernet frame operation |
| 14 | + */ |
| 15 | +class ParseEthernetFrame extends Operation { |
| 16 | + |
| 17 | + /** |
| 18 | + * ParseEthernetFrame constructor |
| 19 | + */ |
| 20 | + constructor() { |
| 21 | + super(); |
| 22 | + |
| 23 | + this.name = "Parse Ethernet frame"; |
| 24 | + this.module = "Default"; |
| 25 | + this.description = "Parses an Ethernet frame and either shows the deduced values (Source and destination MAC, VLANs) or returns the packet data.<br /><br />Good for use in conjunction with the Parse IPv4, and Parse TCP/UDP recipes."; |
| 26 | + this.infoURL = "https://en.wikipedia.org/wiki/Ethernet_frame#Frame_%E2%80%93_data_link_layer"; |
| 27 | + this.inputType = "string"; |
| 28 | + this.outputType = "html"; |
| 29 | + this.args = [ |
| 30 | + { |
| 31 | + name: "Input type", |
| 32 | + type: "option", |
| 33 | + value: [ |
| 34 | + "Raw", "Hex" |
| 35 | + ], |
| 36 | + defaultIndex: 0, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Return type", |
| 40 | + type: "option", |
| 41 | + value: [ |
| 42 | + "Text output", "Packet data", "Packet data (hex)", |
| 43 | + ], |
| 44 | + defaultIndex: 0, |
| 45 | + } |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * @param {string} input |
| 52 | + * @param {Object[]} args |
| 53 | + * @returns {html} |
| 54 | + */ |
| 55 | + run(input, args) { |
| 56 | + const format = args[0]; |
| 57 | + const outputFormat = args[1]; |
| 58 | + |
| 59 | + if (format === "Hex") { |
| 60 | + input = fromHex(input); |
| 61 | + } else if (format === "Raw") { |
| 62 | + input = new Uint8Array(Utils.strToArrayBuffer(input)); |
| 63 | + } else { |
| 64 | + throw new OperationError("Invalid input format selected."); |
| 65 | + } |
| 66 | + |
| 67 | + const destinationMac = input.slice(0, 6); |
| 68 | + const sourceMac = input.slice(6, 12); |
| 69 | + |
| 70 | + let offset = 12; |
| 71 | + const vlans = []; |
| 72 | + |
| 73 | + while (offset < input.length) { |
| 74 | + const ethType = Utils.byteArrayToChars(input.slice(offset, offset+2)); |
| 75 | + offset += 2; |
| 76 | + |
| 77 | + |
| 78 | + if (ethType === "\x08\x00") { |
| 79 | + break; |
| 80 | + } else if (ethType === "\x81\x00" || ethType === "\x88\xA8") { |
| 81 | + // Parse the VLAN tag: |
| 82 | + // [0000] 0000 0000 0000 |
| 83 | + // ^^^ PRIO - Ignored |
| 84 | + // ^ DEI - Ignored |
| 85 | + // ^^^^ ^^^^ ^^^^ VLAN ID |
| 86 | + const vlanTag = input.slice(offset+2, offset+4); |
| 87 | + vlans.push((vlanTag[0] & 0b00001111) << 4 | vlanTag[1]); |
| 88 | + |
| 89 | + offset += 2; |
| 90 | + } else { |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const packetData = input.slice(offset); |
| 96 | + |
| 97 | + if (outputFormat === "Packet data") { |
| 98 | + return Utils.byteArrayToChars(packetData); |
| 99 | + } else if (outputFormat === "Packet data (hex)") { |
| 100 | + return toHex(packetData); |
| 101 | + } else if (outputFormat === "Text output") { |
| 102 | + let retval = `Source MAC: ${toHex(sourceMac, ":")}\nDestination MAC: ${toHex(destinationMac, ":")}\n`; |
| 103 | + if (vlans.length > 0) { |
| 104 | + retval += `VLAN: ${vlans.join(", ")}\n`; |
| 105 | + } |
| 106 | + retval += `Data:\n${toHex(packetData)}`; |
| 107 | + return retval; |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +export default ParseEthernetFrame; |
0 commit comments