netlink_packet/netlink/
flags.rs

1use crate::constants::*;
2
3/// Represent the flags field in a netlink packet header.
4#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
5pub struct NetlinkFlags(u16);
6
7impl From<u16> for NetlinkFlags {
8    fn from(flags: u16) -> Self {
9        NetlinkFlags(flags)
10    }
11}
12
13impl<'a> From<&'a NetlinkFlags> for u16 {
14    fn from(flags: &'a NetlinkFlags) -> u16 {
15        flags.0
16    }
17}
18
19impl From<NetlinkFlags> for u16 {
20    fn from(flags: NetlinkFlags) -> u16 {
21        flags.0
22    }
23}
24
25impl Default for NetlinkFlags {
26    fn default() -> Self {
27        NetlinkFlags::new()
28    }
29}
30
31impl NetlinkFlags {
32    /// Create a new empty flags field (no flag is set)
33    pub fn new() -> Self {
34        NetlinkFlags(0)
35    }
36
37    /// Set the `NLM_F_REQUEST` flag
38    pub fn set_request(&mut self) -> &mut Self {
39        self.0 |= NLM_F_REQUEST;
40        self
41    }
42
43    /// Check if the `NLM_F_REQUEST` flag is set
44    pub fn has_request(self) -> bool {
45        self.0 & NLM_F_REQUEST == NLM_F_REQUEST
46    }
47
48    /// Set the `NLM_F_MULTIPART` flag
49    pub fn set_multipart(&mut self) -> &mut Self {
50        self.0 |= NLM_F_MULTIPART;
51        self
52    }
53    /// Check if the `NLM_F_MULTIPART` flag is set
54    pub fn has_multipart(self) -> bool {
55        self.0 & NLM_F_MULTIPART == NLM_F_MULTIPART
56    }
57
58    /// Set the `NLM_F_ACK` flag
59    pub fn set_ack(&mut self) -> &mut Self {
60        self.0 |= NLM_F_ACK;
61        self
62    }
63    /// Check if the `NLM_F_ACK` flag is set
64    pub fn has_ack(self) -> bool {
65        self.0 & NLM_F_ACK == NLM_F_ACK
66    }
67
68    /// Set the `NLM_F_ECHO` flag
69    pub fn set_echo(&mut self) -> &mut Self {
70        self.0 |= NLM_F_ECHO;
71        self
72    }
73    /// Check if the `NLM_F_ECHO` flag is set
74    pub fn has_echo(self) -> bool {
75        self.0 & NLM_F_ECHO == NLM_F_ECHO
76    }
77
78    /// Set the `NLM_F_DUMP_INTR` flag
79    pub fn set_dump_intr(&mut self) -> &mut Self {
80        self.0 |= NLM_F_DUMP_INTR;
81        self
82    }
83    /// Check if the `NLM_F_DUMP_INTR` flag is set
84    pub fn has_dump_intr(self) -> bool {
85        self.0 & NLM_F_DUMP_INTR == NLM_F_DUMP_INTR
86    }
87
88    /// Set the `NLM_F_DUMP_FILTERED` flag
89    pub fn set_dump_filterd(&mut self) -> &mut Self {
90        self.0 |= NLM_F_DUMP_FILTERED;
91        self
92    }
93    /// Check if the `NLM_F_DUMP_FILTERED` flag is set
94    pub fn has_dump_filterd(self) -> bool {
95        self.0 & NLM_F_DUMP_FILTERED == NLM_F_DUMP_FILTERED
96    }
97
98    /// Set the `NLM_F_ROOT` flag
99    pub fn set_root(&mut self) -> &mut Self {
100        self.0 |= NLM_F_ROOT;
101        self
102    }
103    /// Check if the `NLM_F_ROOT` flag is set
104    pub fn has_root(self) -> bool {
105        self.0 & NLM_F_ROOT == NLM_F_ROOT
106    }
107
108    /// Set the `NLM_F_MATCH` flag
109    pub fn set_match(&mut self) -> &mut Self {
110        self.0 |= NLM_F_MATCH;
111        self
112    }
113    /// Check if the `NLM_F_MATCH` flag is set
114    pub fn has_match(self) -> bool {
115        self.0 & NLM_F_MATCH == NLM_F_MATCH
116    }
117
118    /// Set the `NLM_F_ATOMIC` flag
119    pub fn set_atomic(&mut self) -> &mut Self {
120        self.0 |= NLM_F_ATOMIC;
121        self
122    }
123    /// Check if the `NLM_F_ATOMIC` flag is set
124    pub fn has_atomic(self) -> bool {
125        self.0 & NLM_F_ATOMIC == NLM_F_ATOMIC
126    }
127
128    /// Set the `NLM_F_DUMP` flag
129    pub fn set_dump(&mut self) -> &mut Self {
130        self.0 |= NLM_F_DUMP;
131        self
132    }
133    /// Check if the `NLM_F_DUMP` flag is set
134    pub fn has_dump(self) -> bool {
135        self.0 & NLM_F_DUMP == NLM_F_DUMP
136    }
137
138    /// Set the `NLM_F_REPLACE` flag
139    pub fn set_replace(&mut self) -> &mut Self {
140        self.0 |= NLM_F_REPLACE;
141        self
142    }
143    /// Check if the `NLM_F_REPLACE` flag is set
144    pub fn has_replace(self) -> bool {
145        self.0 & NLM_F_REPLACE == NLM_F_REPLACE
146    }
147
148    /// Set the `NLM_F_EXCL` flag
149    pub fn set_excl(&mut self) -> &mut Self {
150        self.0 |= NLM_F_EXCL;
151        self
152    }
153    /// Check if the `NLM_F_EXCL` flag is set
154    pub fn has_excl(self) -> bool {
155        self.0 & NLM_F_EXCL == NLM_F_EXCL
156    }
157
158    /// Set the `NLM_F_CREATE` flag
159    pub fn set_create(&mut self) -> &mut Self {
160        self.0 |= NLM_F_CREATE;
161        self
162    }
163    /// Check if the `NLM_F_CREATE` flag is set
164    pub fn has_create(self) -> bool {
165        self.0 & NLM_F_CREATE == NLM_F_CREATE
166    }
167
168    /// Set the `NLM_F_APPEND` flag
169    pub fn set_append(&mut self) -> &mut Self {
170        self.0 |= NLM_F_APPEND;
171        self
172    }
173    /// Check if the `NLM_F_APPEND` flag is set
174    pub fn has_append(self) -> bool {
175        self.0 & NLM_F_APPEND == NLM_F_APPEND
176    }
177
178    /// Set the `NLM_F_NONREC` flag
179    pub fn set_nonrec(&mut self) -> &mut Self {
180        self.0 |= NLM_F_NONREC;
181        self
182    }
183    /// Check if the `NLM_F_NONREC` flag is set
184    pub fn has_nonrec(self) -> bool {
185        self.0 & NLM_F_NONREC == NLM_F_NONREC
186    }
187
188    /// Set the `NLM_F_ACK_TLVS` flag
189    pub fn set_ack_tvls(&mut self) -> &mut Self {
190        self.0 |= NLM_F_ACK_TLVS;
191        self
192    }
193    /// Check if the `NLM_F_ACK_TLVS` flag is set
194    pub fn has_ack_tvls(self) -> bool {
195        self.0 & NLM_F_ACK_TLVS == NLM_F_ACK_TLVS
196    }
197
198    /// Set the `NLM_F_CAPPED` flag
199    pub fn set_capped(&mut self) -> &mut Self {
200        self.0 |= NLM_F_CAPPED;
201        self
202    }
203
204    /// Check if the `NLM_F_CAPPED` flag is set
205    pub fn has_capped(self) -> bool {
206        self.0 & NLM_F_CAPPED == NLM_F_CAPPED
207    }
208}