| 1 | use crate::cerr; |
| 2 | |
| 3 | use super::RawSocket; |
| 4 | |
| 5 | impl RawSocket { |
| 6 | pub(crate) fn so_timestamp(&self, options: u32) -> std::io::Result<()> { |
| 7 | // Documentation on the timestamping calls: |
| 8 | // |
| 9 | // - linux: https://www.kernel.org/doc/Documentation/networking/timestamping.txt |
| 10 | // - freebsd: https://man.freebsd.org/cgi/man.cgi?setsockopt |
| 11 | // |
| 12 | // SAFETY: |
| 13 | // |
| 14 | // - the socket is provided by (safe) rust, and will outlive the call |
| 15 | // - method is guaranteed to be a valid "name" argument |
| 16 | // - the options pointer outlives the call |
| 17 | // - the `option_len` corresponds with the options pointer |
| 18 | // |
| 19 | // Only some bits are valid to set in `options`, but setting invalid bits is |
| 20 | // perfectly safe |
| 21 | // |
| 22 | // > Setting other bit returns EINVAL and does not change the current state. |
| 23 | unsafe { |
| 24 | cerr(libc::setsockopt( |
| 25 | self.fd, |
| 26 | libc::SOL_SOCKET, |
| 27 | libc::SO_TIMESTAMP, |
| 28 | &options as *const _ as *const libc::c_void, |
| 29 | std::mem::size_of_val(&options) as libc::socklen_t, |
| 30 | )) |
| 31 | }?; |
| 32 | if options != 0 { |
| 33 | let clock = libc::SO_TS_REALTIME as u32; |
| 34 | // Safety: |
| 35 | // |
| 36 | // - The socket is proviided by (safe) rust, and will outlive the call |
| 37 | // - method is guaranteed to be a valid "name" argument |
| 38 | // - clock outlives the call |
| 39 | // - option_len corresponds with the size of clock |
| 40 | unsafe { |
| 41 | cerr(libc::setsockopt( |
| 42 | self.fd, |
| 43 | libc::SOL_SOCKET, |
| 44 | libc::SO_TS_CLOCK, |
| 45 | &clock as *const _ as *const libc::c_void, |
| 46 | std::mem::size_of_val(&clock) as libc::socklen_t, |
| 47 | )) |
| 48 | }?; |
| 49 | } |
| 50 | Ok(()) |
| 51 | } |
| 52 | } |