Skip to content

Commit a855796

Browse files
committed
Support TIMESTAMPNS
1 parent 3f8a66d commit a855796

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased] - ReleaseDate
77
### Added
8+
- Added linux TIMESTAMPNS support for x86/x64 gnu
9+
(#[1402](https://github.com/nix-rust/nix/pull/1402))
810

911
### Changed
1012
- Made `forkpty` unsafe, like `fork`

src/sys/socket/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
77
CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_LEN};
88
use std::{mem, ptr, slice};
99
use std::os::unix::io::RawFd;
10+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
11+
use crate::sys::time::TimeSpec;
1012
use crate::sys::time::TimeVal;
1113
use crate::sys::uio::IoVec;
1214

@@ -554,6 +556,11 @@ pub enum ControlMessageOwned {
554556
/// # }
555557
/// ```
556558
ScmTimestamp(TimeVal),
559+
/// Nanoseconds resolution timestamp
560+
///
561+
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
562+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
563+
ScmTimestampNs(TimeSpec),
557564
#[cfg(any(
558565
target_os = "android",
559566
target_os = "ios",
@@ -645,6 +652,11 @@ impl ControlMessageOwned {
645652
let tv: libc::timeval = ptr::read_unaligned(p as *const _);
646653
ControlMessageOwned::ScmTimestamp(TimeVal::from(tv))
647654
},
655+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
656+
(libc::SOL_SOCKET, libc::SCM_TIMESTAMPNS) => {
657+
let ts: libc::timespec = ptr::read_unaligned(p as *const _);
658+
ControlMessageOwned::ScmTimestampNs(TimeSpec::from(ts))
659+
}
648660
#[cfg(any(
649661
target_os = "android",
650662
target_os = "freebsd",

src/sys/socket/sockopt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ sockopt_impl!(Both, BindToDevice, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, OsStr
272272
#[cfg(any(target_os = "android", target_os = "linux"))]
273273
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::sockaddr_in);
274274
sockopt_impl!(Both, ReceiveTimestamp, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
275+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
276+
sockopt_impl!(Both, ReceiveTimestampNs, libc::SOL_SOCKET, libc::SO_TIMESTAMPNS, bool);
275277
#[cfg(any(target_os = "android", target_os = "linux"))]
276278
sockopt_impl!(Both, IpTransparent, libc::SOL_IP, libc::IP_TRANSPARENT, bool);
277279
#[cfg(target_os = "openbsd")]

test/sys/test_socket.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,3 +1535,105 @@ pub fn test_vsock() {
15351535
close(s1).unwrap();
15361536
thr.join().unwrap();
15371537
}
1538+
1539+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
1540+
#[test]
1541+
fn test_recvmsg_timestampns() {
1542+
use nix::sys::socket::*;
1543+
use nix::sys::uio::IoVec;
1544+
use nix::sys::time::*;
1545+
use std::time::*;
1546+
1547+
// Set up
1548+
let message = "Ohayō!".as_bytes();
1549+
let in_socket = socket(
1550+
AddressFamily::Inet,
1551+
SockType::Datagram,
1552+
SockFlag::empty(),
1553+
None).unwrap();
1554+
setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap();
1555+
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
1556+
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
1557+
let address = getsockname(in_socket).unwrap();
1558+
// Get initial time
1559+
let time0 = SystemTime::now();
1560+
// Send the message
1561+
let iov = [IoVec::from_slice(message)];
1562+
let flags = MsgFlags::empty();
1563+
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap();
1564+
assert_eq!(message.len(), l);
1565+
// Receive the message
1566+
let mut buffer = vec![0u8; message.len()];
1567+
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
1568+
let iov = [IoVec::from_mut_slice(&mut buffer)];
1569+
let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
1570+
let rtime = match r.cmsgs().next() {
1571+
Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime,
1572+
Some(_) => panic!("Unexpected control message"),
1573+
None => panic!("No control message")
1574+
};
1575+
// Check the final time
1576+
let time1 = SystemTime::now();
1577+
// the packet's received timestamp should lie in-between the two system
1578+
// times, unless the system clock was adjusted in the meantime.
1579+
let rduration = Duration::new(rtime.tv_sec() as u64,
1580+
rtime.tv_nsec() as u32);
1581+
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration);
1582+
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap());
1583+
// Close socket
1584+
nix::unistd::close(in_socket).unwrap();
1585+
}
1586+
1587+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
1588+
#[test]
1589+
fn test_recvmmsg_timestampns() {
1590+
use nix::sys::socket::*;
1591+
use nix::sys::uio::IoVec;
1592+
use nix::sys::time::*;
1593+
use std::time::*;
1594+
1595+
// Set up
1596+
let message = "Ohayō!".as_bytes();
1597+
let in_socket = socket(
1598+
AddressFamily::Inet,
1599+
SockType::Datagram,
1600+
SockFlag::empty(),
1601+
None).unwrap();
1602+
setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap();
1603+
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
1604+
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
1605+
let address = getsockname(in_socket).unwrap();
1606+
// Get initial time
1607+
let time0 = SystemTime::now();
1608+
// Send the message
1609+
let iov = [IoVec::from_slice(message)];
1610+
let flags = MsgFlags::empty();
1611+
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap();
1612+
assert_eq!(message.len(), l);
1613+
// Receive the message
1614+
let mut buffer = vec![0u8; message.len()];
1615+
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
1616+
let iov = [IoVec::from_mut_slice(&mut buffer)];
1617+
let mut data = vec![
1618+
RecvMmsgData {
1619+
iov,
1620+
cmsg_buffer: Some(&mut cmsgspace),
1621+
},
1622+
];
1623+
let r = recvmmsg(in_socket, &mut data, flags, None).unwrap();
1624+
let rtime = match r[0].cmsgs().next() {
1625+
Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime,
1626+
Some(_) => panic!("Unexpected control message"),
1627+
None => panic!("No control message")
1628+
};
1629+
// Check the final time
1630+
let time1 = SystemTime::now();
1631+
// the packet's received timestamp should lie in-between the two system
1632+
// times, unless the system clock was adjusted in the meantime.
1633+
let rduration = Duration::new(rtime.tv_sec() as u64,
1634+
rtime.tv_nsec() as u32);
1635+
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration);
1636+
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap());
1637+
// Close socket
1638+
nix::unistd::close(in_socket).unwrap();
1639+
}

0 commit comments

Comments
 (0)