-
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 #1899 from c65722/strip_ipv4_header
Add Strip IPv4 header operation
- Loading branch information
Showing
4 changed files
with
185 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,57 @@ | ||
/** | ||
* @author c65722 [] | ||
* @copyright Crown Copyright 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
import Stream from "../lib/Stream.mjs"; | ||
|
||
/** | ||
* Strip IPv4 header operation | ||
*/ | ||
class StripIPv4Header extends Operation { | ||
|
||
/** | ||
* StripIPv4Header constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Strip IPv4 header"; | ||
this.module = "Default"; | ||
this.description = "Strips the IPv4 header from an IPv4 packet, outputting the payload."; | ||
this.infoURL = "https://wikipedia.org/wiki/IPv4"; | ||
this.inputType = "ArrayBuffer"; | ||
this.outputType = "ArrayBuffer"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} input | ||
* @param {Object[]} args | ||
* @returns {ArrayBuffer} | ||
*/ | ||
run(input, args) { | ||
const MIN_HEADER_LEN = 20; | ||
|
||
const s = new Stream(new Uint8Array(input)); | ||
if (s.length < MIN_HEADER_LEN) { | ||
throw new OperationError("Input length is less than minimum IPv4 header length"); | ||
} | ||
|
||
const ihl = s.readInt(1) & 0x0f; | ||
const dataOffsetBytes = ihl * 4; | ||
if (s.length < dataOffsetBytes) { | ||
throw new OperationError("Input length is less than IHL"); | ||
} | ||
|
||
s.moveTo(dataOffsetBytes); | ||
|
||
return s.getBytes().buffer; | ||
} | ||
|
||
} | ||
|
||
export default StripIPv4Header; |
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,126 @@ | ||
/** | ||
* Strip IPv4 header tests. | ||
* | ||
* @author c65722 [] | ||
* @copyright Crown Copyright 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Strip IPv4 header: No options, No payload", | ||
input: "450000140005400080060000c0a80001c0a80002", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip IPv4 header: No options, Payload", | ||
input: "450000140005400080060000c0a80001c0a80002ffffffffffffffff", | ||
expectedOutput: "ffffffffffffffff", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip IPv4 header: Options, No payload", | ||
input: "460000140005400080060000c0a80001c0a8000207000000", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip IPv4 header: Options, Payload", | ||
input: "460000140005400080060000c0a80001c0a8000207000000ffffffffffffffff", | ||
expectedOutput: "ffffffffffffffff", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip IPv4 header: Input length lesss than minimum header length", | ||
input: "450000140005400080060000c0a80001c0a800", | ||
expectedOutput: "Input length is less than minimum IPv4 header length", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
}, | ||
{ | ||
name: "Strip IPv4 header: Input length less than IHL", | ||
input: "460000140005400080060000c0a80001c0a80000", | ||
expectedOutput: "Input length is less than IHL", | ||
recipeConfig: [ | ||
{ | ||
op: "From Hex", | ||
args: ["None"] | ||
}, | ||
{ | ||
op: "Strip IPv4 header", | ||
args: [], | ||
}, | ||
{ | ||
op: "To Hex", | ||
args: ["None", 0] | ||
} | ||
] | ||
} | ||
]); |