Skip to content

Commit

Permalink
add genericty to KeyFragmentationMatching
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmazelanssi committed Aug 30, 2024
1 parent 7366d8d commit 1305b38
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::filters::fragmentation::two_tuple_proto_ipid_five_tuple::TwoTupleProt
use crate::filters::ipaddr_pair::IpAddrPair;
use crate::filters::key_parser_ipv4;
use crate::filters::key_parser_ipv6;
use libpcap_tools::FiveTuple;

/// Function to convert TwoTupleProtoIpid/FiveTuple data to key container
pub type ConvertFn<Container> = fn(&HashSet<TwoTupleProtoIpidFiveTuple>) -> Container;
Expand Down Expand Up @@ -235,7 +236,7 @@ impl<Container, Key> Filter for FragmentationFilter<Container, Key> {

pub fn test_key_fragmentation_transport_in_container(
container_tuple: &(TwoTupleProtoIpidC, FiveTupleC),
key_fragmentation_matching: &KeyFragmentationMatching,
key_fragmentation_matching: &KeyFragmentationMatching<FiveTuple>,
) -> Result<bool, Error> {
let (two_tuple_proto_ipid_c, five_tuple_c) = container_tuple;

Expand Down Expand Up @@ -341,7 +342,7 @@ impl FragmentationFilterBuilder {
TwoTupleProtoIpidC::new(HashSet::new(), HashSet::new());
let five_tuple_container = FiveTupleC::new(HashSet::new(), HashSet::new());

let keep: KeepFn<(TwoTupleProtoIpidC, FiveTupleC), KeyFragmentationMatching> =
let keep: KeepFn<(TwoTupleProtoIpidC, FiveTupleC), KeyFragmentationMatching<_>> =
match filtering_action {
FilteringAction::Keep => |c, key_fragmentation_matching| {
test_key_fragmentation_transport_in_container(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use libpcap_tools::FiveTuple;

use crate::filters::fragmentation::two_tuple_proto_ipid::TwoTupleProtoIpid;

/// Contains either FiveTuple or TwoTupleProtoIpid.
/// It is used to store data that will be matched against the data of the first fragment.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum KeyFragmentationMatching {
pub enum KeyFragmentationMatching<Key> {
/// Packet is either not a fragment, or the first one.
NotFragmentOrFirstFragment(FiveTuple),
NotFragmentOrFirstFragment(Key),
/// Packet is a fragment, but not the first one.
FragmentAfterFirst(TwoTupleProtoIpid),
}

impl KeyFragmentationMatching {
impl<Key> KeyFragmentationMatching<Key> {
pub fn get_two_tuple_proto_ipid_option(&self) -> Option<&TwoTupleProtoIpid> {
match self {
KeyFragmentationMatching::FragmentAfterFirst(two_tuple_proto_ipid) => {
Expand All @@ -22,10 +20,10 @@ impl KeyFragmentationMatching {
}
}

pub fn get_five_tuple_option(&self) -> Option<&FiveTuple> {
pub fn get_five_tuple_option(&self) -> Option<&Key> {
match self {
KeyFragmentationMatching::FragmentAfterFirst(_) => None,
KeyFragmentationMatching::NotFragmentOrFirstFragment(five_tuple) => Some(five_tuple),
KeyFragmentationMatching::NotFragmentOrFirstFragment(key) => Some(key),
}
}
}
2 changes: 1 addition & 1 deletion pcap-rewrite/src/filters/key_parser_ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn parse_two_tuple_proto_ipid_five_tuple(
pub fn parse_key_fragmentation_transport(
ctx: &ParseContext,
payload: &[u8],
) -> Result<KeyFragmentationMatching, Error> {
) -> Result<KeyFragmentationMatching<FiveTuple>, Error> {
match parse_five_tuple(ctx, payload)? {
Some(five_tuple) =>
{
Expand Down
2 changes: 1 addition & 1 deletion pcap-rewrite/src/filters/key_parser_ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub fn parse_two_tuple_proto_ipid_five_tuple(
pub fn parse_key_fragmentation_transport(
ctx: &ParseContext,
payload: &[u8],
) -> Result<KeyFragmentationMatching, Error> {
) -> Result<KeyFragmentationMatching<FiveTuple>, Error> {
match parse_five_tuple(ctx, payload)? {
Some(five_tuple) => Ok(KeyFragmentationMatching::NotFragmentOrFirstFragment(
five_tuple,
Expand Down

0 comments on commit 1305b38

Please sign in to comment.