/** * @author h345983745 [] * @copyright Crown Copyright 2019 * @license Apache-2.0 */ import Operation from "../Operation.mjs"; import Stream from "../lib/Stream.mjs"; import {toHex} from "../lib/Hex.mjs"; import OperationError from "../errors/OperationError.mjs"; /** * Parse UDP operation */ class ParseUDP extends Operation { /** * ParseUDP constructor */ constructor() { super(); this.name = "Parse UDP"; this.module = "Default"; this.description = "Parses a UDP header and payload (if present)."; this.infoURL = "https://wikipedia.org/wiki/User_Datagram_Protocol"; this.inputType = "ArrayBuffer"; this.outputType = "json"; this.presentType = "html"; this.args = []; } /** * @param {ArrayBuffer} input * @returns {Object} */ run(input, args) { if (input.byteLength < 8) { throw new OperationError("Need 8 bytes for a UDP Header"); } const s = new Stream(new Uint8Array(input)); // Parse Header const UDPPacket = { "Source port": s.readInt(2), "Destination port": s.readInt(2), "Length": s.readInt(2), "Checksum": toHex(s.getBytes(2), "") }; // Parse data if present if (s.hasMore()) { UDPPacket.Data = toHex(s.getBytes(UDPPacket.Length - 8), ""); } return UDPPacket; } /** * Displays the UDP Packet in a table style * @param {Object} data * @returns {html} */ present(data) { const html = []; html.push(""); html.push(""); html.push(""); html.push(""); html.push(""); for (const key in data) { html.push(""); html.push(""); html.push(""); html.push(""); } html.push("
FieldValue
" + key + "" + data[key] + "
"); return html.join(""); } } export default ParseUDP;