1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![allow(unsafe_code)]
#[derive(Copy, Clone, Debug)]
pub struct U32AlignedBuffer<'a> {
ptr: *const u8,
len: i32,
phantom: core::marker::PhantomData<&'a [u8]>,
}
#[cfg(target_os = "linux")]
const ALIGN: usize = 4;
#[cfg(not(target_os = "linux"))]
const ALIGN: usize = 8;
pub unsafe trait FromBuffer: Copy + Sized {
fn len(&self, size: usize) -> u32;
}
impl<'a> U32AlignedBuffer<'a> {
pub fn new(buf: &'a [u8]) -> Self {
let (ptr, len) = (buf.as_ptr(), buf.len());
assert!(ptr as usize % ALIGN == 0, "misaligned buffer");
assert!(len < (1 << 20), "data size limit exceeded");
Self {
ptr,
len: len as _,
phantom: core::marker::PhantomData,
}
}
pub fn read<T: FromBuffer>(&mut self) -> Option<(T, Self)> {
let _: [u8; ALIGN & (ALIGN - 1)] = [];
assert_eq!(align_of!(T) & (align_of!(T) - 1), 0);
let size = ((size_of!(T) + (ALIGN - 1)) & !(ALIGN - 1)) as i32;
assert!(size % ALIGN as i32 == 0);
assert!(size_of!(T) < 1024, "no type in the crate is this big");
assert!(align_of!(T) <= ALIGN, "type has too strict alignment needs");
debug_assert!(self.ptr as usize % ALIGN == 0);
if self.len < size {
return None;
}
let output: T = unsafe { std::ptr::read_unaligned(self.ptr as _) };
let len = output.len(self.len as _) as i32;
if len < size || len > self.len {
return None;
}
let buffer = Self {
len: len - size,
ptr: unsafe { self.ptr.offset(size as _) },
phantom: core::marker::PhantomData,
};
let advance = ((len as usize + (ALIGN - 1)) & !(ALIGN - 1)) as i32;
debug_assert!(advance % ALIGN as i32 == 0);
self.len -= advance;
self.ptr = self.ptr.wrapping_offset(advance as _);
debug_assert!(self.ptr as usize % ALIGN == 0);
Some((output, buffer))
}
}
impl<'a> core::convert::TryFrom<U32AlignedBuffer<'a>> for std::net::IpAddr {
type Error = ();
fn try_from(s: U32AlignedBuffer<'a>) -> Result<Self, Self::Error> {
match s.len {
16 => unsafe {
let v: [u8; 16] = core::ptr::read(s.ptr as _);
Ok(v.into())
},
4 => unsafe {
let v: [u8; 4] = core::ptr::read(s.ptr as _);
Ok(v.into())
},
_ => Err(()),
}
}
}