diff options
author | Michael Baikov <manpacket@gmail.com> | 2021-09-29 11:32:03 +0800 |
---|---|---|
committer | Michael Baikov <manpacket@gmail.com> | 2021-12-23 06:41:30 +0800 |
commit | 9aa61ef9758474b886b82999e4dfe8cc9a0b03ac (patch) | |
tree | 2bfbb8a577aaeec1ce2bb32d93d13153bc13b51c /test/sys/test_socket.rs | |
parent | 6c6c576dc043aeee5a814490ec8c4665283580ae (diff) | |
download | nix-9aa61ef9758474b886b82999e4dfe8cc9a0b03ac.zip |
feat: Add glibc::SOF_TIMESTAMPING_* support
Diffstat (limited to 'test/sys/test_socket.rs')
-rw-r--r-- | test/sys/test_socket.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 0f6fac66..7a861abf 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -57,6 +57,64 @@ pub fn test_inetv4_addr_roundtrip_sockaddr_storage_to_addr() { assert_eq!(from_storage, sockaddr); } +#[cfg(any(target_os = "linux"))] +#[cfg_attr(qemu, ignore)] +#[test] +pub fn test_timestamping() { + use nix::sys::socket::{ + recvmsg, sendmsg, setsockopt, socket, sockopt::Timestamping, ControlMessageOwned, MsgFlags, + SockFlag, SockType, TimestampingFlag, + }; + use nix::sys::uio::IoVec; + + let std_sa = SocketAddr::from_str("127.0.0.1:6790").unwrap(); + let inet_addr = InetAddr::from_std(&std_sa); + let sock_addr = SockAddr::new_inet(inet_addr); + + let ssock = socket( + AddressFamily::Inet, + SockType::Datagram, + SockFlag::empty(), + None, + ) + .expect("send socket failed"); + + let rsock = socket( + AddressFamily::Inet, + SockType::Datagram, + SockFlag::empty(), + None, + ) + .unwrap(); + nix::sys::socket::bind(rsock, &sock_addr).unwrap(); + + setsockopt(rsock, Timestamping, &TimestampingFlag::all()).unwrap(); + + let sbuf = [0u8; 2048]; + let mut rbuf = [0u8; 2048]; + let flags = MsgFlags::empty(); + let iov1 = [IoVec::from_slice(&sbuf)]; + let iov2 = [IoVec::from_mut_slice(&mut rbuf)]; + let mut cmsg = cmsg_space!(nix::sys::socket::Timestamps); + sendmsg(ssock, &iov1, &[], flags, Some(&sock_addr)).unwrap(); + let recv = recvmsg(rsock, &iov2, Some(&mut cmsg), flags).unwrap(); + + let mut ts = None; + for c in recv.cmsgs() { + if let ControlMessageOwned::ScmTimestampsns(timestamps) = c { + ts = Some(timestamps.system); + } + } + let ts = ts.expect("ScmTimestampns is present"); + let sys_time = ::nix::time::clock_gettime(::nix::time::ClockId::CLOCK_REALTIME).unwrap(); + let diff = if ts > sys_time { + ts - sys_time + } else { + sys_time - ts + }; + assert!(std::time::Duration::from(diff).as_secs() < 60); +} + #[test] pub fn test_inetv6_addr_to_sock_addr() { let port: u16 = 3000; |