1use windivert_sys::{ChecksumFlags, WinDivertHelperCalcChecksums};
2
3use crate::{address::WinDivertAddress, layer, prelude::WinDivertError};
4
5use std::{
6 borrow::{BorrowMut, Cow},
7 ffi::c_void,
8 fmt::Debug,
9};
10
11#[derive(Debug, Clone)]
13pub struct WinDivertPacket<'a, L: layer::WinDivertLayerTrait> {
14 pub address: WinDivertAddress<L>,
16 pub data: Cow<'a, [u8]>,
18}
19
20impl<'a> WinDivertPacket<'a, layer::NetworkLayer> {
21 pub unsafe fn new(data: Vec<u8>) -> Self {
25 Self {
26 address: WinDivertAddress::<layer::NetworkLayer>::new(),
27 data: Cow::from(data),
28 }
29 }
30
31 pub fn recalculate_checksums(&mut self, flags: ChecksumFlags) -> Result<(), WinDivertError> {
34 if let Cow::Owned(ref mut data) = self.data.borrow_mut() {
35 let res = unsafe {
36 WinDivertHelperCalcChecksums(
37 data.as_mut_ptr() as *mut c_void,
38 data.len() as u32,
39 self.address.as_mut(),
40 flags,
41 )
42 };
43 if !res.as_bool() {
44 return Err(WinDivertError::from(windows::core::Error::from_win32()));
45 }
46 }
47 Ok(())
48 }
49}
50
51impl<'a> WinDivertPacket<'a, layer::ForwardLayer> {
52 pub unsafe fn new(data: Vec<u8>) -> Self {
56 Self {
57 address: WinDivertAddress::<layer::ForwardLayer>::new(),
58 data: Cow::from(data),
59 }
60 }
61
62 pub fn recalculate_checksums(&mut self, flags: ChecksumFlags) -> Result<(), WinDivertError> {
65 if let Cow::Owned(ref mut data) = self.data.borrow_mut() {
66 let res = unsafe {
67 WinDivertHelperCalcChecksums(
68 data.as_mut_ptr() as *mut c_void,
69 data.len() as u32,
70 self.address.as_mut(),
71 flags,
72 )
73 };
74 if !res.as_bool() {
75 return Err(WinDivertError::from(windows::core::Error::from_win32()));
76 }
77 }
78 Ok(())
79 }
80}
81
82impl<'a, L: layer::WinDivertLayerTrait> WinDivertPacket<'a, L> {
83 pub fn into_owned(self) -> WinDivertPacket<'static, L> {
85 WinDivertPacket {
86 address: self.address,
87 data: self.data.into_owned().into(),
88 }
89 }
90}