Skip to content

Commit 7ecc235

Browse files
authored
Merge pull request #1900 from c65722/strip_udp_header
Add Strip UDP header operation
2 parents 306e29d + da74d9b commit 7ecc235

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/core/config/Categories.json

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
"Parse TCP",
239239
"Parse TLS record",
240240
"Parse UDP",
241+
"Strip UDP header",
241242
"Parse SSH Host Key",
242243
"Parse URI",
243244
"URL Encode",
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @author c65722 []
3+
* @copyright Crown Copyright 2024
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Stream from "../lib/Stream.mjs";
9+
import OperationError from "../errors/OperationError.mjs";
10+
11+
/**
12+
* Strip UDP header operation
13+
*/
14+
class StripUDPHeader extends Operation {
15+
16+
/**
17+
* StripUDPHeader constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "Strip UDP header";
23+
this.module = "Default";
24+
this.description = "Strips the UDP header from a UDP datagram, outputting the payload.";
25+
this.infoURL = "https://wikipedia.org/wiki/User_Datagram_Protocol";
26+
this.inputType = "ArrayBuffer";
27+
this.outputType = "ArrayBuffer";
28+
this.args = [];
29+
}
30+
31+
/**
32+
* @param {ArrayBuffer} input
33+
* @param {Object[]} args
34+
* @returns {ArrayBuffer}
35+
*/
36+
run(input, args) {
37+
const HEADER_LEN = 8;
38+
39+
const s = new Stream(new Uint8Array(input));
40+
if (s.length < HEADER_LEN) {
41+
throw new OperationError("Need 8 bytes for a UDP Header");
42+
}
43+
44+
s.moveTo(HEADER_LEN);
45+
46+
return s.getBytes().buffer;
47+
}
48+
49+
}
50+
51+
export default StripUDPHeader;

tests/operations/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ import "./tests/SIGABA.mjs";
143143
import "./tests/SM4.mjs";
144144
// import "./tests/SplitColourChannels.mjs"; // Cannot test operations that use the File type yet
145145
import "./tests/StrUtils.mjs";
146+
import "./tests/StripUDPHeader.mjs";
146147
import "./tests/Subsection.mjs";
147148
import "./tests/SwapCase.mjs";
148149
import "./tests/SymmetricDifference.mjs";
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Strip UDP header tests.
3+
*
4+
* @author c65722 []
5+
* @copyright Crown Copyright 2024
6+
* @license Apache-2.0
7+
*/
8+
9+
import TestRegister from "../../lib/TestRegister.mjs";
10+
11+
TestRegister.addTests([
12+
{
13+
name: "Strip UDP header: No payload",
14+
input: "8111003500000000",
15+
expectedOutput: "",
16+
recipeConfig: [
17+
{
18+
op: "From Hex",
19+
args: ["None"]
20+
},
21+
{
22+
op: "Strip UDP header",
23+
args: [],
24+
},
25+
{
26+
op: "To Hex",
27+
args: ["None", 0]
28+
}
29+
]
30+
},
31+
{
32+
name: "Strip UDP header: Payload",
33+
input: "8111003500080000ffffffffffffffff",
34+
expectedOutput: "ffffffffffffffff",
35+
recipeConfig: [
36+
{
37+
op: "From Hex",
38+
args: ["None"]
39+
},
40+
{
41+
op: "Strip UDP header",
42+
args: [],
43+
},
44+
{
45+
op: "To Hex",
46+
args: ["None", 0]
47+
}
48+
]
49+
},
50+
{
51+
name: "Strip UDP header: Input length less than header length",
52+
input: "81110035000000",
53+
expectedOutput: "Need 8 bytes for a UDP Header",
54+
recipeConfig: [
55+
{
56+
op: "From Hex",
57+
args: ["None"]
58+
},
59+
{
60+
op: "Strip UDP header",
61+
args: [],
62+
},
63+
{
64+
op: "To Hex",
65+
args: ["None", 0]
66+
}
67+
]
68+
}
69+
]);

0 commit comments

Comments
 (0)