Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sendqueue pkt header size #379

Merged
merged 8 commits into from
Dec 20, 2024
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ impl From<std::io::ErrorKind> 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::<raw::pcap_pkthdr>()
}

#[cfg(test)]
mod tests {
use std::error::Error as StdError;
Expand Down Expand Up @@ -279,4 +287,12 @@ mod tests {
}
}
}

#[test]
fn test_packet_size() {
assert_eq!(
packet_header_size(),
std::mem::size_of::<raw::pcap_pkthdr>()
);
}
}
3 changes: 3 additions & 0 deletions src/sendqueue/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
let squeue = unsafe { raw::pcap_sendqueue_alloc(memsize) };
let squeue = NonNull::new(squeue).ok_or(Error::InsufficientMemory)?;
Expand Down
Loading