-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1900 from c65722/strip_udp_header
Add Strip UDP header operation
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @author c65722 [] | ||
* @copyright Crown Copyright 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import Stream from "../lib/Stream.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
|
||
/** | ||
* Strip UDP header operation | ||
*/ | ||
class StripUDPHeader extends Operation { | ||
|
||
/** | ||
* StripUDPHeader constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Strip UDP header"; | ||
this.module = "Default"; | ||
this.description = "Strips the UDP header from a UDP datagram, outputting the payload."; | ||
this.infoURL = "https://wikipedia.org/wiki/User_Datagram_Protocol"; | ||
this.inputType = "ArrayBuffer"; | ||
this.outputType = "ArrayBuffer"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} input | ||
* @param {Object[]} args | ||
* @returns {ArrayBuffer} | ||
*/ | ||
run(input, args) { | ||
const HEADER_LEN = 8; | ||
|
||
const s = new Stream(new Uint8Array(input)); | ||
if (s.length < HEADER_LEN) { | ||
throw new OperationError("Need 8 bytes for a UDP Header"); | ||
} | ||
|
||
s.moveTo(HEADER_LEN); | ||
|
||
return s.getBytes().buffer; | ||
} | ||
|
||
} | ||
|
||
export default StripUDPHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Strip UDP header tests. | ||
* | ||
* @author c65722 [] | ||
* @copyright Crown Copyright 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Strip UDP header: No payload", | ||
input: "8111003500000000", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip UDP header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip UDP header: Payload", | ||
input: "8111003500080000ffffffffffffffff", | ||
expectedOutput: "ffffffffffffffff", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip UDP header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip UDP header: Input length less than header length", | ||
input: "81110035000000", | ||
expectedOutput: "Need 8 bytes for a UDP Header", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip UDP header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
} | ||
]); |