Skip to main content

push_packet/engine/linear/
rules.rs

1use ipnet::IpNet;
2use push_packet_common::engine::linear::{
3    FLAG_DESTINATION_CIDR, FLAG_DESTINATION_PORT, FLAG_PROTOCOL, FLAG_SOURCE_CIDR,
4    FLAG_SOURCE_PORT, Ipv4Rule, Ipv6Rule, RuleExt,
5};
6
7use crate::rules::Rule;
8
9impl From<&Rule> for Ipv4Rule {
10    fn from(value: &Rule) -> Self {
11        let Rule {
12            action,
13            protocol,
14            source_cidr,
15            source_port,
16            destination_cidr,
17            destination_port,
18        } = value;
19        let mut rule = Ipv4Rule::default();
20        let (action, take) = (*action).into_common_action();
21        rule.common.action = action;
22        if let Some(take) = take {
23            rule.common.take = take;
24        }
25        if let Some(protocol) = protocol {
26            rule.common.protocol = *protocol;
27            rule.set_flag(FLAG_PROTOCOL);
28        }
29        if let Some(IpNet::V4(source_cidr)) = source_cidr {
30            rule.source_cidr = source_cidr.addr().into();
31            rule.common.source_prefix_len = source_cidr.prefix_len();
32            rule.set_flag(FLAG_SOURCE_CIDR);
33        }
34
35        if let Some(source_port) = source_port {
36            rule.common.source_port_min = *source_port.start();
37            rule.common.source_port_max = *source_port.end();
38            rule.set_flag(FLAG_SOURCE_PORT);
39        }
40        if let Some(IpNet::V4(destination_cidr)) = destination_cidr {
41            rule.destination_cidr = destination_cidr.addr().into();
42            rule.common.destination_prefix_len = destination_cidr.prefix_len();
43            rule.set_flag(FLAG_DESTINATION_CIDR);
44        }
45
46        if let Some(destination_port) = destination_port {
47            rule.common.destination_port_min = *destination_port.start();
48            rule.common.destination_port_max = *destination_port.end();
49            rule.set_flag(FLAG_DESTINATION_PORT);
50        }
51        rule
52    }
53}
54
55impl From<&Rule> for Ipv6Rule {
56    fn from(value: &Rule) -> Self {
57        let Rule {
58            action,
59            protocol,
60            source_cidr,
61            source_port,
62            destination_cidr,
63            destination_port,
64        } = value;
65        let mut rule = Ipv6Rule::default();
66        let (action, take) = action.into_common_action();
67        rule.common.action = action;
68        if let Some(take) = take {
69            rule.common.take = take;
70        }
71        if let Some(protocol) = protocol {
72            rule.common.protocol = *protocol;
73            rule.set_flag(FLAG_PROTOCOL);
74        }
75        if let Some(IpNet::V6(source_cidr)) = source_cidr {
76            rule.source_cidr = source_cidr.addr().octets();
77            rule.common.source_prefix_len = source_cidr.prefix_len();
78            rule.set_flag(FLAG_SOURCE_CIDR);
79        }
80
81        if let Some(source_port) = source_port {
82            rule.common.source_port_min = *source_port.start();
83            rule.common.source_port_max = *source_port.end();
84            rule.set_flag(FLAG_SOURCE_PORT);
85        }
86        if let Some(IpNet::V6(destination_cidr)) = destination_cidr {
87            rule.destination_cidr = destination_cidr.addr().octets();
88            rule.common.destination_prefix_len = destination_cidr.prefix_len();
89            rule.set_flag(FLAG_DESTINATION_CIDR);
90        }
91
92        if let Some(destination_port) = destination_port {
93            rule.common.destination_port_min = *destination_port.start();
94            rule.common.destination_port_max = *destination_port.end();
95            rule.set_flag(FLAG_DESTINATION_PORT);
96        }
97        rule
98    }
99}