pfctl/rule/
gid.rs

1// Copyright 2025 Mullvad VPN AB.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9pub use super::uid::Id;
10use crate::{
11    conversion::CopyTo,
12    ffi::pfvar::{PF_OP_NONE, pf_rule_gid},
13};
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub struct Gid(pub Id);
17
18impl Default for Gid {
19    fn default() -> Self {
20        Gid(Id::Any)
21    }
22}
23
24impl<T: Into<Id>> From<T> for Gid {
25    fn from(id: T) -> Self {
26        Gid(id.into())
27    }
28}
29
30impl CopyTo<pf_rule_gid> for Gid {
31    fn copy_to(&self, pf_rule_gid: &mut pf_rule_gid) {
32        match self.0 {
33            Id::Any => {
34                pf_rule_gid.gid[0] = 0;
35                pf_rule_gid.op = PF_OP_NONE as u8;
36            }
37
38            Id::One(gid, modifier) => {
39                pf_rule_gid.gid[0] = gid;
40                pf_rule_gid.op = modifier.into();
41            }
42
43            Id::Range(start_gid, end_gid, modifier) => {
44                pf_rule_gid.gid[0] = start_gid;
45                pf_rule_gid.gid[1] = end_gid;
46                pf_rule_gid.op = modifier.into();
47            }
48        }
49    }
50}