diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b76047..b0997dfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## [Unreleased] +### Added + +- Added `packet_header_size()` for applications that need to know the size of internal type + `pcap_pkthdr` (for instance to calculate exact send queue sizes). + +want to precalculate exact queue sizes. + ## [2.2.0] - 2024-09-01 ### Added diff --git a/src/lib.rs b/src/lib.rs index 39a2374a..9a0402d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,6 +227,14 @@ impl From for Error { } } +/// Return size of a commonly used packet header. +/// +/// On Windows this packet header is implicitly added to send queues, so this size must be known +/// if an application needs to precalculate the exact send queue buffer size. +pub const fn packet_header_size() -> usize { + std::mem::size_of::() +} + #[cfg(test)] mod tests { use std::error::Error as StdError; @@ -279,4 +287,12 @@ mod tests { } } } + + #[test] + fn test_packet_size() { + assert_eq!( + packet_header_size(), + std::mem::size_of::() + ); + } } diff --git a/src/sendqueue/windows.rs b/src/sendqueue/windows.rs index 37c4000a..7268e066 100644 --- a/src/sendqueue/windows.rs +++ b/src/sendqueue/windows.rs @@ -46,6 +46,9 @@ impl SendQueue { /// /// The buffer size `memsize` must be able to contain both packet headers and actual packet /// contents. + /// + /// Applications that need to precalculate exact buffer sizes can use [`packet_header_size()`](crate::packet_header_size()) + /// to get the size of the header that is implicitly added along with each packet. pub fn new(memsize: u32) -> Result { let squeue = unsafe { raw::pcap_sendqueue_alloc(memsize) }; let squeue = NonNull::new(squeue).ok_or(Error::InsufficientMemory)?;