windivert/
packet.rs

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/// Raw captured packet
12#[derive(Debug, Clone)]
13pub struct WinDivertPacket<'a, L: layer::WinDivertLayerTrait> {
14    /// Address data
15    pub address: WinDivertAddress<L>,
16    /// Raw captured data
17    pub data: Cow<'a, [u8]>,
18}
19
20impl<'a> WinDivertPacket<'a, layer::NetworkLayer> {
21    /// Create a new network packet from a raw buffer
22    /// # Safety
23    /// `address` is zeroed, user must fill it with correct data before sending.
24    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    /// Recalculate the checksums of the packet
32    /// This is a noop if the packet is not owned.
33    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    /// Create a new network forward packet from a raw buffer
53    /// # Safety
54    /// `address` is zeroed, user must fill it with correct data before sending.
55    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    /// Recalculate the checksums of the packet
63    /// This is a noop if the packet is not owned.
64    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    /// Create an owned packet from a borrowed packet
84    pub fn into_owned(self) -> WinDivertPacket<'static, L> {
85        WinDivertPacket {
86            address: self.address,
87            data: self.data.into_owned().into(),
88        }
89    }
90}