Debian Codegraph @

third_party/rust/quinn-udp/src/cmsg/unix.rs

raw
1 use std::ffi::{c_int, c_uchar};
2
3 use super::{CMsgHdr, MsgHdr};
4
5 #[derive(Copy, Clone)]
6 #[repr(align(8))] // Conservative bound for align_of<libc::cmsghdr>
7 pub(crate) struct Aligned<T>(pub(crate) T);
8
9 /// Helpers for [`libc::msghdr`]
10 impl MsgHdr for libc::msghdr {
11 type ControlMessage = libc::cmsghdr;
12
13 fn cmsg_first_hdr(&self) -> *mut Self::ControlMessage {
14 unsafe { libc::CMSG_FIRSTHDR(self) }
15 }
16
17 fn cmsg_nxt_hdr(&self, cmsg: &Self::ControlMessage) -> *mut Self::ControlMessage {
18 unsafe { libc::CMSG_NXTHDR(self, cmsg) }
19 }
20
21 fn set_control_len(&mut self, len: usize) {
22 self.msg_controllen = len as _;
23 if len == 0 {
24 // netbsd is particular about this being a NULL pointer if there are no control
25 // messages.
26 self.msg_control = std::ptr::null_mut();
27 }
28 }
29
30 fn control_len(&self) -> usize {
31 self.msg_controllen as _
32 }
33 }
34
35 #[cfg(apple_fast)]
36 impl MsgHdr for crate::imp::msghdr_x {
37 type ControlMessage = libc::cmsghdr;
38
39 fn cmsg_first_hdr(&self) -> *mut Self::ControlMessage {
40 let selfp = self as *const _ as *mut libc::msghdr;
41 unsafe { libc::CMSG_FIRSTHDR(selfp) }
42 }
43
44 fn cmsg_nxt_hdr(&self, cmsg: &Self::ControlMessage) -> *mut Self::ControlMessage {
45 let selfp = self as *const _ as *mut libc::msghdr;
46 unsafe { libc::CMSG_NXTHDR(selfp, cmsg) }
47 }
48
49 fn set_control_len(&mut self, len: usize) {
50 self.msg_controllen = len as _;
51 }
52
53 fn control_len(&self) -> usize {
54 self.msg_controllen as _
55 }
56 }
57
58 /// Helpers for [`libc::cmsghdr`]
59 impl CMsgHdr for libc::cmsghdr {
60 fn cmsg_len(length: usize) -> usize {
61 unsafe { libc::CMSG_LEN(length as _) as usize }
62 }
63
64 fn cmsg_space(length: usize) -> usize {
65 unsafe { libc::CMSG_SPACE(length as _) as usize }
66 }
67
68 fn cmsg_data(&self) -> *mut c_uchar {
69 unsafe { libc::CMSG_DATA(self) }
70 }
71
72 fn set(&mut self, level: c_int, ty: c_int, len: usize) {
73 self.cmsg_level = level as _;
74 self.cmsg_type = ty as _;
75 self.cmsg_len = len as _;
76 }
77
78 fn len(&self) -> usize {
79 self.cmsg_len as _
80 }
81 }