|
| 1 | +use nix::sys::socket::*; |
| 2 | +use nix::sys::uio::IoVec; |
| 3 | +use nix::sys::time::*; |
| 4 | +use std::time::*; |
| 5 | + |
| 6 | +// TODO: move this to unit test once we may use `cmsg_space!` there |
| 7 | +#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))] |
| 8 | +#[test] |
| 9 | +fn recvmsg_timestampns() { |
| 10 | + // Set up |
| 11 | + let message = "Ohayō!".as_bytes(); |
| 12 | + let in_socket = socket( |
| 13 | + AddressFamily::Inet, |
| 14 | + SockType::Datagram, |
| 15 | + SockFlag::empty(), |
| 16 | + None).unwrap(); |
| 17 | + setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap(); |
| 18 | + let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0); |
| 19 | + bind(in_socket, &SockAddr::new_inet(localhost)).unwrap(); |
| 20 | + let address = getsockname(in_socket).unwrap(); |
| 21 | + // Get initial time |
| 22 | + let time0 = SystemTime::now(); |
| 23 | + // Send the message |
| 24 | + let iov = [IoVec::from_slice(message)]; |
| 25 | + let flags = MsgFlags::empty(); |
| 26 | + let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap(); |
| 27 | + assert_eq!(message.len(), l); |
| 28 | + // Receive the message |
| 29 | + let mut buffer = vec![0u8; message.len()]; |
| 30 | + let mut cmsgspace = nix::cmsg_space!(TimeSpec); |
| 31 | + let iov = [IoVec::from_mut_slice(&mut buffer)]; |
| 32 | + let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap(); |
| 33 | + let rtime = match r.cmsgs().next() { |
| 34 | + Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime, |
| 35 | + Some(_) => panic!("Unexpected control message"), |
| 36 | + None => panic!("No control message") |
| 37 | + }; |
| 38 | + // Check the final time |
| 39 | + let time1 = SystemTime::now(); |
| 40 | + // the packet's received timestamp should lie in-between the two system |
| 41 | + // times, unless the system clock was adjusted in the meantime. |
| 42 | + let rduration = Duration::new(rtime.tv_sec() as u64, |
| 43 | + rtime.tv_nsec() as u32); |
| 44 | + assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration); |
| 45 | + assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap()); |
| 46 | + // Close socket |
| 47 | + nix::unistd::close(in_socket).unwrap(); |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))] |
| 51 | +#[test] |
| 52 | +fn recvmmsg_timestampns() { |
| 53 | + // Set up |
| 54 | + let message = "Ohayō!".as_bytes(); |
| 55 | + let in_socket = socket( |
| 56 | + AddressFamily::Inet, |
| 57 | + SockType::Datagram, |
| 58 | + SockFlag::empty(), |
| 59 | + None).unwrap(); |
| 60 | + setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap(); |
| 61 | + let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0); |
| 62 | + bind(in_socket, &SockAddr::new_inet(localhost)).unwrap(); |
| 63 | + let address = getsockname(in_socket).unwrap(); |
| 64 | + // Get initial time |
| 65 | + let time0 = SystemTime::now(); |
| 66 | + // Send the message |
| 67 | + let iov = [IoVec::from_slice(message)]; |
| 68 | + let flags = MsgFlags::empty(); |
| 69 | + let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap(); |
| 70 | + assert_eq!(message.len(), l); |
| 71 | + // Receive the message |
| 72 | + let mut buffer = vec![0u8; message.len()]; |
| 73 | + let mut cmsgspace = nix::cmsg_space!(TimeSpec); |
| 74 | + let iov = [IoVec::from_mut_slice(&mut buffer)]; |
| 75 | + let mut data = vec![ |
| 76 | + RecvMmsgData { |
| 77 | + iov, |
| 78 | + cmsg_buffer: Some(&mut cmsgspace), |
| 79 | + }, |
| 80 | + ]; |
| 81 | + let r = recvmmsg(in_socket, &mut data, flags, None).unwrap(); |
| 82 | + let rtime = match r[0].cmsgs().next() { |
| 83 | + Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime, |
| 84 | + Some(_) => panic!("Unexpected control message"), |
| 85 | + None => panic!("No control message") |
| 86 | + }; |
| 87 | + // Check the final time |
| 88 | + let time1 = SystemTime::now(); |
| 89 | + // the packet's received timestamp should lie in-between the two system |
| 90 | + // times, unless the system clock was adjusted in the meantime. |
| 91 | + let rduration = Duration::new(rtime.tv_sec() as u64, |
| 92 | + rtime.tv_nsec() as u32); |
| 93 | + assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration); |
| 94 | + assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap()); |
| 95 | + // Close socket |
| 96 | + nix::unistd::close(in_socket).unwrap(); |
| 97 | +} |
0 commit comments