1use std::{convert::TryFrom, fmt::Debug};
8
9use super::{BitfieldUnit, WinDivertEvent, WinDivertFlags, WinDivertLayer};
10
11#[repr(C)]
12#[derive(Debug, Default, Copy, Clone)]
13pub struct WINDIVERT_DATA_NETWORK {
17 pub interface_id: u32,
19 pub subinterface_id: u32,
21}
22
23#[repr(C)]
24#[derive(Debug, Default, Copy, Clone)]
25pub struct WINDIVERT_DATA_FLOW {
29 pub endpoint_id: u64,
31 pub parent_endpoint_id: u64,
33 pub process_id: u32,
35 pub local_addr: [u32; 4usize],
43 pub remote_addr: [u32; 4usize],
51 pub local_port: u16,
53 pub remote_port: u16,
55 pub protocol: u8,
57}
58
59#[repr(C)]
60#[derive(Debug, Default, Copy, Clone)]
61pub struct WINDIVERT_DATA_SOCKET {
65 pub endpoint_id: u64,
67 pub parent_endpoint_id: u64,
69 pub process_id: u32,
71 pub local_addr: [u32; 4usize],
77 pub remote_addr: [u32; 4usize],
83 pub local_port: u16,
85 pub remote_port: u16,
87 pub protocol: u8,
89}
90
91#[repr(C)]
92#[derive(Debug, Copy, Clone)]
93pub struct WINDIVERT_DATA_REFLECT {
97 pub timestamp: i64,
99 pub process_id: u32,
101 pub layer: WinDivertLayer,
103 pub flags: WinDivertFlags,
105 pub priority: i16,
107}
108
109impl Default for WINDIVERT_DATA_REFLECT {
110 fn default() -> Self {
111 unsafe { ::std::mem::zeroed() }
112 }
113}
114
115#[repr(C)]
116#[derive(Copy, Clone)]
117pub union WINDIVERT_ADDRESS_UNION_FIELD {
119 pub Network: WINDIVERT_DATA_NETWORK,
121 pub Flow: WINDIVERT_DATA_FLOW,
123 pub Socket: WINDIVERT_DATA_SOCKET,
125 pub Reflect: WINDIVERT_DATA_REFLECT,
127 reserved: [u8; 64usize],
128 _union_align: [u64; 8usize],
129}
130
131impl Default for WINDIVERT_ADDRESS_UNION_FIELD {
132 fn default() -> Self {
133 unsafe { ::std::mem::zeroed() }
134 }
135}
136
137#[repr(C)]
138#[derive(Copy, Clone)]
139pub struct WINDIVERT_ADDRESS {
145 pub timestamp: i64,
147 addr_bitfield: BitfieldUnit<[u8; 4usize], u8>,
148 reserved: u32,
149 pub union_field: WINDIVERT_ADDRESS_UNION_FIELD,
151}
152
153impl Default for WINDIVERT_ADDRESS {
154 fn default() -> Self {
155 unsafe { ::std::mem::zeroed() }
156 }
157}
158
159impl WINDIVERT_ADDRESS {
160 #[inline]
161 pub fn layer(&self) -> WinDivertLayer {
163 WinDivertLayer::try_from(self.addr_bitfield.get(0usize, 8u8) as u32)
164 .expect("Layer always is correct since it would have produced an error in Open()")
165 }
166 #[inline]
167 pub fn set_layer(&mut self, val: WinDivertLayer) {
169 self.addr_bitfield.set(0usize, 8u8, u32::from(val) as u64)
170 }
171 #[inline]
172 pub fn event(&self) -> WinDivertEvent {
174 WinDivertEvent::try_from(self.addr_bitfield.get(8usize, 8u8) as u8)
175 .expect("Event always is correct since the value comes from the DLL functions.")
176 }
177 #[inline]
178 pub fn set_event(&mut self, val: WinDivertEvent) {
180 self.addr_bitfield.set(8usize, 8u8, u32::from(val) as u64)
181 }
182 #[inline]
183 pub fn sniffed(&self) -> bool {
185 self.addr_bitfield.get(16usize, 1u8) == 1
186 }
187 #[inline]
188 pub fn set_sniffed(&mut self, val: bool) {
190 self.addr_bitfield.set(16usize, 1u8, val as u64)
191 }
192 #[inline]
193 pub fn outbound(&self) -> bool {
195 self.addr_bitfield.get(17usize, 1u8) == 1
196 }
197 #[inline]
198 pub fn set_outbound(&mut self, val: bool) {
200 self.addr_bitfield.set(17usize, 1u8, val as u64)
201 }
202 #[inline]
203 pub fn loopback(&self) -> bool {
205 self.addr_bitfield.get(18usize, 1u8) == 1
206 }
207 #[inline]
208 pub fn set_loopback(&mut self, val: bool) {
210 self.addr_bitfield.set(18usize, 1u8, val as u64)
211 }
212 #[inline]
213 pub fn impostor(&self) -> bool {
215 self.addr_bitfield.get(19usize, 1u8) == 1
216 }
217 #[inline]
218 pub fn set_impostor(&mut self, val: bool) {
220 self.addr_bitfield.set(19usize, 1u8, val as u64)
221 }
222 #[inline]
223 pub fn ipv6(&self) -> bool {
225 self.addr_bitfield.get(20usize, 1u8) == 1
226 }
227 #[inline]
228 pub fn set_ipv6(&mut self, val: bool) {
230 self.addr_bitfield.set(20usize, 1u8, val as u64)
231 }
232 #[inline]
233 pub fn ipchecksum(&self) -> bool {
235 self.addr_bitfield.get(21usize, 1u8) == 1
236 }
237 #[inline]
238 pub fn set_ipchecksum(&mut self, val: bool) {
240 self.addr_bitfield.set(21usize, 1u8, val as u64)
241 }
242 #[inline]
243 pub fn tcpchecksum(&self) -> bool {
245 self.addr_bitfield.get(22usize, 1u8) == 1
246 }
247 #[inline]
248 pub fn set_tcpchecksum(&mut self, val: bool) {
250 self.addr_bitfield.set(22usize, 1u8, val as u64)
251 }
252 #[inline]
253 pub fn udpchecksum(&self) -> bool {
255 self.addr_bitfield.get(23usize, 1u8) == 1
256 }
257 #[inline]
258 pub fn set_udpchecksum(&mut self, val: bool) {
260 self.addr_bitfield.set(23usize, 1u8, val as u64)
261 }
262}
263
264impl Debug for WINDIVERT_ADDRESS {
265 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
266 let union_str = match self.event() {
267 WinDivertEvent::NetworkPacket => {
268 format!("{:?}", unsafe { self.union_field.Network })
269 }
270 WinDivertEvent::FlowStablished | WinDivertEvent::FlowDeleted => {
271 format!("{:?}", unsafe { self.union_field.Flow })
272 }
273 WinDivertEvent::SocketBind
274 | WinDivertEvent::SocketConnect
275 | WinDivertEvent::SocketListen
276 | WinDivertEvent::SocketAccept
277 | WinDivertEvent::SocketClose => {
278 format!("{:?}", unsafe { self.union_field.Socket })
279 }
280 WinDivertEvent::ReflectOpen | WinDivertEvent::ReflectClose => {
281 format!("{:?}", unsafe { self.union_field.Reflect })
282 }
283 };
284 write!(f, "WINDIVERT_ADDRESS {{ Timestamp: {:?}, Layer: {:?}, Event: {:?}, Sniffed: {:?}, Outbound: {:?}, Loopback: {:?}, Impostor: {:?}, IPv6: {:?}, IPChecksum: {:?}, TCPChecksum: {:?}, UDPChecksum: {:?}, {}}}",
285 self.timestamp, self.layer(), self.event(), self.sniffed(), self.outbound(), self.loopback(), self.impostor(), self.ipv6(), self.ipchecksum(), self.tcpchecksum(), self.udpchecksum(), union_str)
286 }
287}