Skip to content

Commit

Permalink
use bitflags for RuleFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
little-dude authored and cathay4t committed Mar 28, 2024
1 parent 386470f commit b337e00
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 86 deletions.
72 changes: 11 additions & 61 deletions src/rule/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,16 @@ const FIB_RULE_IIF_DETACHED: u32 = 0x00000008;
const FIB_RULE_DEV_DETACHED: u32 = FIB_RULE_IIF_DETACHED;
const FIB_RULE_OIF_DETACHED: u32 = 0x00000010;

#[derive(Clone, Eq, PartialEq, Debug, Copy)]
#[non_exhaustive]
pub enum RuleFlag {
Permanent,
Invert,
Unresolved,
IifDetached,
DevDetached,
OifDetached,
Other(u32),
}

const ALL_RULE_FLAGS: [RuleFlag; 5] = [
RuleFlag::Permanent,
RuleFlag::Invert,
RuleFlag::Unresolved,
RuleFlag::IifDetached,
RuleFlag::OifDetached,
];

impl From<RuleFlag> for u32 {
fn from(v: RuleFlag) -> u32 {
match v {
RuleFlag::Permanent => FIB_RULE_PERMANENT,
RuleFlag::Invert => FIB_RULE_INVERT,
RuleFlag::Unresolved => FIB_RULE_UNRESOLVED,
RuleFlag::IifDetached => FIB_RULE_IIF_DETACHED,
RuleFlag::DevDetached => FIB_RULE_DEV_DETACHED,
RuleFlag::OifDetached => FIB_RULE_OIF_DETACHED,
RuleFlag::Other(i) => i,
}
}
}

#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub(crate) struct VecRuleFlag(pub(crate) Vec<RuleFlag>);

impl From<u32> for VecRuleFlag {
fn from(d: u32) -> Self {
let mut got: u32 = 0;
let mut ret = Vec::new();
for flag in ALL_RULE_FLAGS {
if (d & (u32::from(flag))) > 0 {
ret.push(flag);
got += u32::from(flag);
}
}
if got != d {
ret.push(RuleFlag::Other(d - got));
}
Self(ret)
}
}

impl From<&VecRuleFlag> for u32 {
fn from(v: &VecRuleFlag) -> u32 {
let mut d: u32 = 0;
for flag in &v.0 {
d += u32::from(*flag);
}
d
bitflags! {
#[derive(Clone, Eq, PartialEq, Debug, Copy, Default)]
#[non_exhaustive]
pub struct RuleFlags: u32 {
const Permanent = FIB_RULE_PERMANENT;
const Invert = FIB_RULE_INVERT;
const Unresolved = FIB_RULE_UNRESOLVED;
const IifDetached = FIB_RULE_IIF_DETACHED;
const DevDetached = FIB_RULE_DEV_DETACHED;
const OifDetached = FIB_RULE_OIF_DETACHED;
const _ = !0;
}
}
8 changes: 4 additions & 4 deletions src/rule/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use netlink_packet_utils::{
DecodeError,
};

use super::{super::AddressFamily, flags::VecRuleFlag, RuleAction, RuleFlag};
use super::{super::AddressFamily, flags::RuleFlags, RuleAction};

const RULE_HEADER_LEN: usize = 12;

Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct RuleHeader {
pub tos: u8,
pub table: u8,
pub action: RuleAction,
pub flags: Vec<RuleFlag>,
pub flags: RuleFlags,
}

impl Emitable for RuleHeader {
Expand All @@ -56,7 +56,7 @@ impl Emitable for RuleHeader {
packet.set_table(self.table);
packet.set_tos(self.tos);
packet.set_action(self.action.into());
packet.set_flags(u32::from(&VecRuleFlag(self.flags.to_vec())));
packet.set_flags(self.flags.bits());
}
}

Expand All @@ -71,7 +71,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<RuleMessageBuffer<&'a T>>
tos: buf.tos(),
table: buf.table(),
action: buf.action().into(),
flags: VecRuleFlag::from(buf.flags()).0,
flags: RuleFlags::from_bits_retain(buf.flags()),
})
}
}
4 changes: 2 additions & 2 deletions src/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mod action;
mod attribute;
pub(crate) mod flags;
mod flags;
mod header;
mod message;
mod port_range;
Expand All @@ -12,7 +12,7 @@ mod uid_range;

pub use self::action::RuleAction;
pub use self::attribute::RuleAttribute;
pub use self::flags::RuleFlag;
pub use self::flags::RuleFlags;
pub use self::header::{RuleHeader, RuleMessageBuffer};
pub use self::message::RuleMessage;
pub use self::port_range::RulePortRange;
Expand Down
7 changes: 4 additions & 3 deletions src/rule/tests/fw_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use netlink_packet_utils::{Emitable, Parseable};
use crate::{
route::RouteProtocol,
rule::{
RuleAction, RuleAttribute, RuleHeader, RuleMessage, RuleMessageBuffer,
flags::RuleFlags, RuleAction, RuleAttribute, RuleHeader, RuleMessage,
RuleMessageBuffer,
},
AddressFamily,
};
Expand All @@ -32,7 +33,7 @@ fn test_ipv4_fwmark_suppress_prefixlength() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down Expand Up @@ -78,7 +79,7 @@ fn test_ipv6_fwmark_suppress_ifgroup() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down
7 changes: 4 additions & 3 deletions src/rule/tests/iif_oif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use netlink_packet_utils::{Emitable, Parseable};
use crate::{
route::RouteProtocol,
rule::{
RuleAction, RuleAttribute, RuleHeader, RuleMessage, RuleMessageBuffer,
flags::RuleFlags, RuleAction, RuleAttribute, RuleHeader, RuleMessage,
RuleMessageBuffer,
},
AddressFamily, IpProtocol,
};
Expand Down Expand Up @@ -38,7 +39,7 @@ fn test_ipv4_iif_oif_prohibit() {
tos: 0,
table: 0,
action: RuleAction::Prohibit,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(0),
Expand Down Expand Up @@ -91,7 +92,7 @@ fn test_ipv6_iif_oif_ipproto() {
tos: 0,
table: 252,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(500),
Expand Down
8 changes: 4 additions & 4 deletions src/rule/tests/l3mdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use netlink_packet_utils::{Emitable, Parseable};
use crate::{
route::RouteProtocol,
rule::{
RuleAction, RuleAttribute, RuleHeader, RuleMessage, RuleMessageBuffer,
RuleUidRange,
flags::RuleFlags, RuleAction, RuleAttribute, RuleHeader, RuleMessage,
RuleMessageBuffer, RuleUidRange,
},
AddressFamily,
};
Expand All @@ -33,7 +33,7 @@ fn test_ipv4_l3mdev() {
tos: 0,
table: 0,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(0),
Expand Down Expand Up @@ -78,7 +78,7 @@ fn test_ipv6_l3mdev_uid() {
tos: 0,
table: 0,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(0),
Expand Down
7 changes: 4 additions & 3 deletions src/rule/tests/on_boot_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use netlink_packet_utils::{Emitable, Parseable};
use crate::{
route::RouteProtocol,
rule::{
RuleAction, RuleAttribute, RuleHeader, RuleMessage, RuleMessageBuffer,
flags::RuleFlags, RuleAction, RuleAttribute, RuleHeader, RuleMessage,
RuleMessageBuffer,
},
AddressFamily,
};
Expand All @@ -29,7 +30,7 @@ fn test_ipv4_rule() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down Expand Up @@ -69,7 +70,7 @@ fn test_ipv6_rule() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down
8 changes: 4 additions & 4 deletions src/rule/tests/sport_dport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use netlink_packet_utils::{Emitable, Parseable};
use crate::{
route::{RouteProtocol, RouteRealm},
rule::{
RuleAction, RuleAttribute, RuleHeader, RuleMessage, RuleMessageBuffer,
RulePortRange,
flags::RuleFlags, RuleAction, RuleAttribute, RuleHeader, RuleMessage,
RuleMessageBuffer, RulePortRange,
},
AddressFamily, IpProtocol,
};
Expand Down Expand Up @@ -35,7 +35,7 @@ fn test_ipv4_tcp_sport_dport_realm() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down Expand Up @@ -94,7 +94,7 @@ fn test_ipv4_udp_sport_range_dport_range_reals_src_dst() {
tos: 0,
table: 254,
action: RuleAction::ToTable,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(254),
Expand Down
5 changes: 3 additions & 2 deletions src/rule/tests/src_dst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str::FromStr;

use netlink_packet_utils::{Emitable, Parseable};

use crate::rule::flags::RuleFlags;
use crate::{
route::RouteProtocol,
rule::{
Expand Down Expand Up @@ -35,7 +36,7 @@ fn test_ipv4_src_dst_blackhole() {
tos: 0,
table: 0,
action: RuleAction::Blackhole,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(0),
Expand Down Expand Up @@ -88,7 +89,7 @@ fn test_ipv6_src_dst_goto() {
tos: 0,
table: 0,
action: RuleAction::Goto,
flags: vec![],
flags: RuleFlags::empty(),
},
attributes: vec![
RuleAttribute::Table(0),
Expand Down

0 comments on commit b337e00

Please sign in to comment.