chore: Split utils for zerocopy in protocol into own file

This commit is contained in:
Karolin Varner
2025-06-01 20:10:03 +02:00
parent f33c3a6928
commit 4e77e67f10
4 changed files with 26 additions and 16 deletions

View File

@@ -81,6 +81,7 @@ pub mod basic_types;
pub mod constants;
pub mod testutils;
pub mod timing;
pub mod zerocopy;
#[allow(clippy::module_inception)]
mod protocol;

View File

@@ -45,6 +45,7 @@ use super::constants::{
RETRANSMIT_DELAY_GROWTH, RETRANSMIT_DELAY_JITTER,
};
use super::timing::{has_happened, Timing, BCE, UNENDING};
use super::zerocopy::{truncating_cast_into, truncating_cast_into_nomut};
#[cfg(feature = "trace_bench")]
use rosenpass_util::trace_bench::Trace as _;
@@ -3949,17 +3950,3 @@ impl CryptoServer {
}
}
}
/// Used to parse a network message using [zerocopy]
pub fn truncating_cast_into<T: FromBytes>(
buf: &mut [u8],
) -> Result<Ref<&mut [u8], T>, RosenpassError> {
Ref::new(&mut buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
}
/// Used to parse a network message using [zerocopy], mutably
pub fn truncating_cast_into_nomut<T: FromBytes>(
buf: &[u8],
) -> Result<Ref<&[u8], T>, RosenpassError> {
Ref::new(&buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
}

View File

@@ -14,8 +14,9 @@ use crate::msgs::{EmptyData, Envelope, InitConf, InitHello, MsgType, RespHello,
use super::{
basic_types::{MsgBuf, SPk, SSk, SymKey},
constants::REKEY_AFTER_TIME_RESPONDER,
truncating_cast_into, truncating_cast_into_nomut, CryptoServer, HandleMsgResult,
HostIdentification, KnownInitConfResponsePtr, PeerPtr, PollResult, ProtocolVersion,
zerocopy::{truncating_cast_into, truncating_cast_into_nomut},
CryptoServer, HandleMsgResult, HostIdentification, KnownInitConfResponsePtr, PeerPtr,
PollResult, ProtocolVersion,
};
struct VecHostIdentifier(Vec<u8>);

View File

@@ -0,0 +1,21 @@
//! Helpers for working with the zerocopy crate
use std::mem::size_of;
use zerocopy::{FromBytes, Ref};
use crate::RosenpassError;
/// Used to parse a network message using [zerocopy]
pub fn truncating_cast_into<T: FromBytes>(
buf: &mut [u8],
) -> Result<Ref<&mut [u8], T>, RosenpassError> {
Ref::new(&mut buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
}
/// Used to parse a network message using [zerocopy], mutably
pub fn truncating_cast_into_nomut<T: FromBytes>(
buf: &[u8],
) -> Result<Ref<&[u8], T>, RosenpassError> {
Ref::new(&buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
}