| 1 | use std::marker::PhantomData; |
| 2 | |
| 3 | use crate::socket::Timestamp; |
| 4 | |
| 5 | pub(crate) const fn control_message_space<T>() -> usize { |
| 6 | // Safety: CMSG_SPACE is safe to call |
| 7 | (unsafe { libc::CMSG_SPACE((std::mem::size_of::<T>()) as _) }) as usize |
| 8 | } |
| 9 | |
| 10 | pub(crate) enum MessageQueue { |
| 11 | Normal, |
| 12 | #[cfg(target_os = "linux")] |
| 13 | Error, |
| 14 | } |
| 15 | |
| 16 | // Invariants: |
| 17 | // self.mhdr points to a valid libc::msghdr with a valid control |
| 18 | // message region. |
| 19 | // self.next_msg points to one of the control messages |
| 20 | // in the region described by self.mhdr or is NULL |
| 21 | // |
| 22 | // These invariants are guaranteed from the safety conditions on |
| 23 | // calling ControlMessageIterator::new, the fact that next preserves |
| 24 | // these invariants and that the fields of ControlMessageIterator |
| 25 | // are not modified outside these two functions. |
| 26 | pub(crate) struct ControlMessageIterator<'a> { |
| 27 | mhdr: libc::msghdr, |
| 28 | next_msg: *const libc::cmsghdr, |
| 29 | phantom: PhantomData<&'a [u8]>, |
| 30 | } |
| 31 | |
| 32 | impl<'a> ControlMessageIterator<'a> { |
| 33 | // Safety assumptions: |
| 34 | // mhdr has a control and controllen field |
| 35 | // that together describe a memory region |
| 36 | // with lifetime 'a containing valid control |
| 37 | // messages |
| 38 | pub unsafe fn new(mhdr: libc::msghdr) -> Self { |
| 39 | // Safety: |
| 40 | // mhdr's control and controllen fields are valid and point |
| 41 | // to valid control messages. |
| 42 | let current_msg = unsafe { libc::CMSG_FIRSTHDR(&mhdr) }; |
| 43 | |
| 44 | // Invariant preservation: |
| 45 | // The safety assumptions guaranteed by the caller ensure |
| 46 | // that mhdr points to a valid region with valid control |
| 47 | // messages. CMSG_FIRSTHDR is then guaranteed to either |
| 48 | // return the pointer to the first valid control message |
| 49 | // in that region, or NULL if the region is empty. |
| 50 | Self { |
| 51 | mhdr, |
| 52 | next_msg: current_msg, |
| 53 | phantom: PhantomData, |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub(crate) enum ControlMessage { |
| 59 | Timestamping { |
| 60 | software: Option<Timestamp>, |
| 61 | hardware: Option<Timestamp>, |
| 62 | }, |
| 63 | #[cfg(target_os = "linux")] |
| 64 | ReceiveError(libc::sock_extended_err), |
| 65 | Other(libc::cmsghdr), |
| 66 | } |
| 67 | |
| 68 | #[cfg(target_os = "linux")] |
| 69 | const SCM_TIMESTAMP_NS: libc::c_int = libc::SCM_TIMESTAMPNS; |
| 70 | #[cfg(target_os = "freebsd")] |
| 71 | const SCM_TIMESTAMP_NS: libc::c_int = libc::SCM_REALTIME; |
| 72 | |
| 73 | #[cfg(target_os = "linux")] |
| 74 | const PACKET_TX_TIMESTAMP: libc::c_int = 16; |
| 75 | |
| 76 | impl<'a> Iterator for ControlMessageIterator<'a> { |
| 77 | type Item = ControlMessage; |
| 78 | |
| 79 | fn next(&mut self) -> Option<Self::Item> { |
| 80 | // Safety: |
| 81 | // By the invariants, self.current_msg either points to a valid control message |
| 82 | // or is NULL |
| 83 | let current_msg = unsafe { self.next_msg.as_ref() }?; |
| 84 | |
| 85 | // Safety: |
| 86 | // Invariants ensure that self.mhdr points to a valid libc::msghdr with a valid |
| 87 | // control message region, and that self.next_msg either points to a |
| 88 | // valid control message or is NULL. |
| 89 | // The previous statement would have returned if self.next_msg were NULL, |
| 90 | // therefore both passed pointers are valid for use with CMSG_NXTHDR |
| 91 | // Invariant preservation: |
| 92 | // CMSG_NXTHDR returns either a pointer to the next valid control message in the |
| 93 | // control message region described by self.mhdr, or NULL |
| 94 | self.next_msg = unsafe { libc::CMSG_NXTHDR(&self.mhdr, self.next_msg) }; |
| 95 | |
| 96 | Some(match (current_msg.cmsg_level, current_msg.cmsg_type) { |
| 97 | #[cfg(target_os = "linux")] |
| 98 | (libc::SOL_SOCKET, libc::SCM_TIMESTAMPING) => { |
| 99 | // Safety: |
| 100 | // current_msg was constructed from a pointer that pointed to a valid control message. |
| 101 | // SO_TIMESTAMPING always has 3 timespecs in the data |
| 102 | let cmsg_data = |
| 103 | unsafe { libc::CMSG_DATA(current_msg) } as *const [libc::timespec; 3]; |
| 104 | |
| 105 | let [software, _, hardware] = unsafe { std::ptr::read_unaligned(cmsg_data) }; |
| 106 | |
| 107 | // Both 0 indicates it is not present |
| 108 | let hardware = if hardware.tv_sec != 0 || hardware.tv_nsec != 0 { |
| 109 | Some(Timestamp::from_timespec(hardware)) |
| 110 | } else { |
| 111 | None |
| 112 | }; |
| 113 | |
| 114 | // Both 0 indicates it is not present |
| 115 | let software = if software.tv_sec != 0 || software.tv_nsec != 0 { |
| 116 | Some(Timestamp::from_timespec(software)) |
| 117 | } else { |
| 118 | None |
| 119 | }; |
| 120 | |
| 121 | ControlMessage::Timestamping { software, hardware } |
| 122 | } |
| 123 | |
| 124 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 125 | (libc::SOL_SOCKET, SCM_TIMESTAMP_NS) => { |
| 126 | // Safety: |
| 127 | // current_msg was constructed from a pointer that pointed to a valid control message. |
| 128 | // SO_TIMESTAMPNS always has a timespec in the data |
| 129 | let cmsg_data = unsafe { libc::CMSG_DATA(current_msg) } as *const libc::timespec; |
| 130 | |
| 131 | let timespec = unsafe { std::ptr::read_unaligned(cmsg_data) }; |
| 132 | |
| 133 | ControlMessage::Timestamping { |
| 134 | software: Some(Timestamp::from_timespec(timespec)), |
| 135 | hardware: None, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | (libc::SOL_SOCKET, libc::SCM_TIMESTAMP) => { |
| 140 | // Safety: |
| 141 | // current_msg was constructed from a pointer that pointed to a valid control message. |
| 142 | // SO_TIMESTAMP always has a timeval in the data |
| 143 | let cmsg_data = unsafe { libc::CMSG_DATA(current_msg) } as *const libc::timeval; |
| 144 | let timeval = unsafe { std::ptr::read_unaligned(cmsg_data) }; |
| 145 | ControlMessage::Timestamping { |
| 146 | software: Some(Timestamp::from_timeval(timeval)), |
| 147 | hardware: None, |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[cfg(target_os = "linux")] |
| 152 | (libc::SOL_IP, libc::IP_RECVERR) |
| 153 | | (libc::SOL_IPV6, libc::IPV6_RECVERR) |
| 154 | | (libc::SOL_PACKET, PACKET_TX_TIMESTAMP) => { |
| 155 | // this is part of how timestamps are reported. |
| 156 | // Safety: |
| 157 | // current_msg was constructed from a pointer that pointed to a valid |
| 158 | // control message. |
| 159 | // IP*_RECVERR always has a sock_extended_err in the data |
| 160 | let error = unsafe { |
| 161 | let ptr = libc::CMSG_DATA(current_msg) as *const libc::sock_extended_err; |
| 162 | std::ptr::read_unaligned(ptr) |
| 163 | }; |
| 164 | |
| 165 | ControlMessage::ReceiveError(error) |
| 166 | } |
| 167 | _ => ControlMessage::Other(*current_msg), |
| 168 | }) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | pub(crate) fn zeroed_sockaddr_storage() -> libc::sockaddr_storage { |
| 173 | // a zeroed-out sockaddr storage is semantically valid, because a ss_family with |
| 174 | // value 0 is libc::AF_UNSPEC. Hence the rest of the data does not come with |
| 175 | // any constraints Safety: |
| 176 | // the MaybeUninit is zeroed before assumed to be initialized |
| 177 | unsafe { std::mem::MaybeUninit::zeroed().assume_init() } |
| 178 | } |
| 179 | |
| 180 | pub(crate) fn empty_msghdr() -> libc::msghdr { |
| 181 | // On `target_env = "musl"`, there are several private padding fields. |
| 182 | // the position of these padding fields depends on the system endianness, |
| 183 | // so keeping making them public does not really help. |
| 184 | // |
| 185 | // Safety: |
| 186 | // |
| 187 | // all fields are either integer or pointer types. For those types, 0 is a valid |
| 188 | // value |
| 189 | unsafe { std::mem::MaybeUninit::<libc::msghdr>::zeroed().assume_init() } |
| 190 | } |