Skip to content

Commit 736d9a3

Browse files
committed
Support TIMESTAMPNS
1 parent 2d36e3a commit 736d9a3

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-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
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

@@ -542,6 +544,11 @@ pub enum ControlMessageOwned {
542544
/// # }
543545
/// ```
544546
ScmTimestamp(TimeVal),
547+
/// Nanoseconds resolution timestamp
548+
///
549+
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
550+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
551+
ScmTimestampNs(TimeSpec),
545552
#[cfg(any(
546553
target_os = "android",
547554
target_os = "ios",
@@ -633,6 +640,11 @@ impl ControlMessageOwned {
633640
let tv: libc::timeval = ptr::read_unaligned(p as *const _);
634641
ControlMessageOwned::ScmTimestamp(TimeVal::from(tv))
635642
},
643+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
644+
(libc::SOL_SOCKET, libc::SCM_TIMESTAMPNS) => {
645+
let ts: libc::timespec = ptr::read_unaligned(p as *const _);
646+
ControlMessageOwned::ScmTimestampNs(TimeSpec::from(ts))
647+
}
636648
#[cfg(any(
637649
target_os = "android",
638650
target_os = "freebsd",

src/sys/socket/sockopt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ sockopt_impl!(Both, BindToDevice, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, OsStr
269269
#[cfg(any(target_os = "android", target_os = "linux"))]
270270
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::sockaddr_in);
271271
sockopt_impl!(Both, ReceiveTimestamp, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
272+
#[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86"), target_env = "gnu"))]
273+
sockopt_impl!(Both, ReceiveTimestampNs, libc::SOL_SOCKET, libc::SO_TIMESTAMPNS, bool);
272274
#[cfg(any(target_os = "android", target_os = "linux"))]
273275
sockopt_impl!(Both, IpTransparent, libc::SOL_IP, libc::IP_TRANSPARENT, bool);
274276
#[cfg(target_os = "openbsd")]

test/receive_timestampns.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use nix::sys::socket::*;
2+
use nix::sys::uio::IoVec;
3+
use nix::sys::time::*;
4+
use std::time::*;
5+
6+
fn main() {
7+
// Set up
8+
let message = "Ohayō!".as_bytes();
9+
let in_socket = socket(
10+
AddressFamily::Inet,
11+
SockType::Datagram,
12+
SockFlag::empty(),
13+
None).unwrap();
14+
setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap();
15+
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
16+
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
17+
let address = getsockname(in_socket).unwrap();
18+
// Get initial time
19+
let time0 = SystemTime::now();
20+
// Send the message
21+
let iov = [IoVec::from_slice(message)];
22+
let flags = MsgFlags::empty();
23+
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap();
24+
assert_eq!(message.len(), l);
25+
// Receive the message
26+
let mut buffer = vec![0u8; message.len()];
27+
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
28+
let iov = [IoVec::from_mut_slice(&mut buffer)];
29+
let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
30+
let rtime = match r.cmsgs().next() {
31+
Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime,
32+
Some(_) => panic!("Unexpected control message"),
33+
None => panic!("No control message")
34+
};
35+
// Check the final time
36+
let time1 = SystemTime::now();
37+
// the packet's received timestamp should lie in-between the two system
38+
// times, unless the system clock was adjusted in the meantime.
39+
let rduration = Duration::new(rtime.tv_sec() as u64,
40+
rtime.tv_nsec() as u32);
41+
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration);
42+
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap());
43+
// Close socket
44+
nix::unistd::close(in_socket).unwrap();
45+
}

0 commit comments

Comments
 (0)