Skip to content

Commit

Permalink
reduce function complexity and replace Fn par fn in pcap-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmazelanssi authored and chifflier committed Jul 31, 2024
1 parent 12385b1 commit 3064748
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
27 changes: 14 additions & 13 deletions pcap-rewrite/src/filters/dispatch_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use crate::filters::key_parser_ipv4;
use crate::filters::key_parser_ipv6;

/// Function to extract key from data
pub type GetKeyFn<Key> = Box<dyn Fn(&ParseContext, &[u8]) -> Result<Key, Error>>;
// pub type GetKeyFn<Key> = Box<dyn Fn(&ParseContext, &[u8]) -> Result<Key, Error>>;
pub type GetKeyFn<Key> = fn(&ParseContext, &[u8]) -> Result<Key, Error>;
/// Function to keep/drop extract key from container
pub type KeepFn<Container, Key> = Box<dyn Fn(&Container, &Key) -> Result<bool, Error>>;

Expand Down Expand Up @@ -59,8 +60,8 @@ impl<Container, Key> DispatchFilter<Container, Key> {

filter_utils::extract_callback_ethernet(
ctx,
&self.get_key_from_ipv4_l3_data,
&self.get_key_from_ipv6_l3_data,
self.get_key_from_ipv4_l3_data,
self.get_key_from_ipv6_l3_data,
data,
)?
}
Expand Down Expand Up @@ -124,8 +125,8 @@ impl DispatchFilterBuilder {

Ok(Box::new(DispatchFilter::new(
ipaddr_container,
Box::new(key_parser_ipv4::parse_src_ipaddr),
Box::new(key_parser_ipv6::parse_src_ipaddr),
key_parser_ipv4::parse_src_ipaddr,
key_parser_ipv6::parse_src_ipaddr,
Box::new(keep),
)))
}
Expand All @@ -142,8 +143,8 @@ impl DispatchFilterBuilder {

Ok(Box::new(DispatchFilter::new(
ipaddr_container,
Box::new(key_parser_ipv4::parse_dst_ipaddr),
Box::new(key_parser_ipv6::parse_dst_ipaddr),
key_parser_ipv4::parse_dst_ipaddr,
key_parser_ipv6::parse_dst_ipaddr,
keep,
)))
}
Expand All @@ -162,8 +163,8 @@ impl DispatchFilterBuilder {

Ok(Box::new(DispatchFilter::new(
ipaddr_container,
Box::new(key_parser_ipv4::parse_src_dst_ipaddr),
Box::new(key_parser_ipv6::parse_src_dst_ipaddr),
key_parser_ipv4::parse_src_dst_ipaddr,
key_parser_ipv6::parse_src_dst_ipaddr,
keep,
)))
}
Expand All @@ -184,8 +185,8 @@ impl DispatchFilterBuilder {

Ok(Box::new(DispatchFilter::new(
ipaddr_proto_port_container,
Box::new(key_parser_ipv4::parse_src_ipaddr_proto_dst_port),
Box::new(key_parser_ipv6::parse_src_ipaddr_proto_dst_port),
key_parser_ipv4::parse_src_ipaddr_proto_dst_port,
key_parser_ipv6::parse_src_ipaddr_proto_dst_port,
keep,
)))
}
Expand All @@ -200,8 +201,8 @@ impl DispatchFilterBuilder {

Ok(Box::new(DispatchFilter::new(
five_tuple_container,
Box::new(key_parser_ipv4::parse_five_tuple),
Box::new(key_parser_ipv6::parse_five_tuple),
key_parser_ipv4::parse_five_tuple,
key_parser_ipv6::parse_five_tuple,
keep,
)))
}
Expand Down
19 changes: 12 additions & 7 deletions pcap-rewrite/src/filters/filter_utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use log::warn;

use libpcap_tools::{Error,ParseContext};
use libpcap_tools::{Error, ParseContext};
use pnet_packet::ethernet::{EtherTypes, EthernetPacket};
use pnet_packet::vlan::VlanPacket;
use pnet_packet::Packet;
use pnet_packet::PrimitiveValues;

pub fn extract_callback_ethernet<D>(
// TODO: find simpler solution for function parameter with genericity
pub fn extract_callback_ethernet<D, F1, F2>(
ctx: &ParseContext,
get_key_from_ipv4_l3_data: &dyn Fn(&ParseContext, &[u8]) -> Result<D, Error>,
get_key_from_ipv6_l3_data: &dyn Fn(&ParseContext, &[u8]) -> Result<D, Error>,
get_key_from_ipv4_l3_data: F1,
get_key_from_ipv6_l3_data: F2,
packet_data: &[u8],
) -> Result<D, Error> {
) -> Result<D, Error>
where
F1: Fn(&ParseContext, &[u8]) -> Result<D, Error>,
F2: Fn(&ParseContext, &[u8]) -> Result<D, Error>,
{
let ethernet_packet = EthernetPacket::new(packet_data)
.ok_or(Error::Pnet("Expected Ethernet packet but could not parse"))?;
match ethernet_packet.get_ethertype() {
Expand All @@ -22,8 +27,8 @@ pub fn extract_callback_ethernet<D>(
let vlan_packet = VlanPacket::new(ethernet_packet.payload())
.ok_or(Error::Pnet("Expected VLAN packet but could not parse"))?;
match vlan_packet.get_ethertype() {
EtherTypes::Ipv4 => (get_key_from_ipv4_l3_data)(ctx,ethernet_packet.payload()),
EtherTypes::Ipv6 => (get_key_from_ipv6_l3_data)(ctx,ethernet_packet.payload()),
EtherTypes::Ipv4 => (get_key_from_ipv4_l3_data)(ctx, ethernet_packet.payload()),
EtherTypes::Ipv6 => (get_key_from_ipv6_l3_data)(ctx, ethernet_packet.payload()),
_ => {
warn!(
"Unimplemented Ethertype in 33024/802.11q: {:?}/{:x}",
Expand Down
36 changes: 18 additions & 18 deletions pcap-rewrite/src/filters/fragmentation/fragmentation_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use super::convert_fn;
/// Function to convert TwoTupleProtoIpid/FiveTuple data to key container
pub type ConvertFn<Container> = Box<dyn Fn(&HashSet<TwoTupleProtoIpidFiveTuple>) -> Container>;
/// Function to extract key from data
pub type GetKeyFn<Key> = Box<dyn Fn(&ParseContext, &[u8]) -> Result<Key, Error>>;
pub type GetKeyFn<Key> = fn(&ParseContext, &[u8]) -> Result<Key, Error>;
/// Function to keep/drop extract key from container
pub type KeepFn<Container, Key> = Box<dyn Fn(&Container, &Key) -> Result<bool, Error>>;

Expand Down Expand Up @@ -79,8 +79,8 @@ impl<Container, Key> FragmentationFilter<Container, Key> {

filter_utils::extract_callback_ethernet(
ctx,
&fragmentation_test::is_ipv4_first_fragment,
&fragmentation_test::is_ipv6_first_fragment,
fragmentation_test::is_ipv4_first_fragment,
fragmentation_test::is_ipv6_first_fragment,
data,
)?
}
Expand Down Expand Up @@ -111,8 +111,8 @@ impl<Container, Key> FragmentationFilter<Container, Key> {

Some(filter_utils::extract_callback_ethernet(
ctx,
&key_parser_ipv4::parse_two_tuple_proto_ipid_five_tuple,
&key_parser_ipv6::parse_two_tuple_proto_ipid_five_tuple,
key_parser_ipv4::parse_two_tuple_proto_ipid_five_tuple,
key_parser_ipv6::parse_two_tuple_proto_ipid_five_tuple,
data,
)?)
}
Expand Down Expand Up @@ -151,16 +151,16 @@ impl<Container, Key> FragmentationFilter<Container, Key> {
ctx: &ParseContext,
packet_data: PacketData<'j>,
) -> FResult<PacketData<'j>, Error> {
let key = match packet_data {
let key: Key = match packet_data {
PacketData::L2(data) => {
if data.len() < 14 {
return Err(Error::DataParser("L2 data too small for ethernet"));
}

filter_utils::extract_callback_ethernet(
ctx,
&self.get_key_from_ipv4_l3_data,
&self.get_key_from_ipv6_l3_data,
self.get_key_from_ipv4_l3_data,
self.get_key_from_ipv6_l3_data,
data,
)?
}
Expand Down Expand Up @@ -255,8 +255,8 @@ impl FragmentationFilterBuilder {
HashSet::new(),
Box::new(convert_fn::convert_data_hs_to_src_ipaddrc),
ipaddr_container,
Box::new(key_parser_ipv4::parse_src_ipaddr),
Box::new(key_parser_ipv6::parse_src_ipaddr),
key_parser_ipv4::parse_src_ipaddr,
key_parser_ipv6::parse_src_ipaddr,
keep,
)))
}
Expand All @@ -274,8 +274,8 @@ impl FragmentationFilterBuilder {
HashSet::new(),
Box::new(convert_fn::convert_data_hs_to_dst_ipaddrc),
ipaddr_container,
Box::new(key_parser_ipv4::parse_dst_ipaddr),
Box::new(key_parser_ipv6::parse_dst_ipaddr),
key_parser_ipv4::parse_dst_ipaddr,
key_parser_ipv6::parse_dst_ipaddr,
keep,
)))
}
Expand All @@ -295,8 +295,8 @@ impl FragmentationFilterBuilder {
HashSet::new(),
Box::new(convert_fn::convert_data_hs_to_src_dst_ipaddrc),
ipaddr_container,
Box::new(key_parser_ipv4::parse_src_dst_ipaddr),
Box::new(key_parser_ipv6::parse_src_dst_ipaddr),
key_parser_ipv4::parse_src_dst_ipaddr,
key_parser_ipv6::parse_src_dst_ipaddr,
keep,
)))
}
Expand All @@ -317,8 +317,8 @@ impl FragmentationFilterBuilder {
HashSet::new(),
Box::new(convert_fn::convert_data_hs_to_src_ipaddr_proto_dst_port_container),
ipaddr_proto_port_container,
Box::new(key_parser_ipv4::parse_src_ipaddr_proto_dst_port),
Box::new(key_parser_ipv6::parse_src_ipaddr_proto_dst_port),
key_parser_ipv4::parse_src_ipaddr_proto_dst_port,
key_parser_ipv6::parse_src_ipaddr_proto_dst_port,
keep,
)))
}
Expand Down Expand Up @@ -347,8 +347,8 @@ impl FragmentationFilterBuilder {
HashSet::new(),
Box::new(convert_fn::convert_data_hs_to_ctuple),
(two_tuple_proto_proto_ipid_c, five_tuple_container),
Box::new(key_parser_ipv4::parse_two_tuple_proto_ipid_five_tuple),
Box::new(key_parser_ipv6::parse_two_tuple_proto_ipid_five_tuple),
key_parser_ipv4::parse_two_tuple_proto_ipid_five_tuple,
key_parser_ipv6::parse_two_tuple_proto_ipid_five_tuple,
keep,
)))
}
Expand Down

0 comments on commit 3064748

Please sign in to comment.