-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use nix::sys::socket::*; | ||
use nix::sys::uio::IoVec; | ||
use nix::sys::time::*; | ||
use std::time::*; | ||
|
||
fn main() { | ||
// Set up | ||
let message = "Ohayō!".as_bytes(); | ||
let in_socket = socket( | ||
AddressFamily::Inet, | ||
SockType::Datagram, | ||
SockFlag::empty(), | ||
None).unwrap(); | ||
setsockopt(in_socket, sockopt::ReceiveTimestampNs, &true).unwrap(); | ||
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0); | ||
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap(); | ||
let address = getsockname(in_socket).unwrap(); | ||
// Get initial time | ||
let time0 = SystemTime::now(); | ||
// Send the message | ||
let iov = [IoVec::from_slice(message)]; | ||
let flags = MsgFlags::empty(); | ||
let l = sendmsg(in_socket, &iov, &[], flags, Some(&address)).unwrap(); | ||
assert_eq!(message.len(), l); | ||
// Receive the message | ||
let mut buffer = vec![0u8; message.len()]; | ||
let mut cmsgspace = nix::cmsg_space!(TimeSpec); | ||
let iov = [IoVec::from_mut_slice(&mut buffer)]; | ||
let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap(); | ||
let rtime = match r.cmsgs().next() { | ||
Some(ControlMessageOwned::ScmTimestampNs(rtime)) => rtime, | ||
Some(_) => panic!("Unexpected control message"), | ||
None => panic!("No control message") | ||
}; | ||
// Check the final time | ||
let time1 = SystemTime::now(); | ||
// the packet's received timestamp should lie in-between the two system | ||
// times, unless the system clock was adjusted in the meantime. | ||
let rduration = Duration::new(rtime.tv_sec() as u64, | ||
rtime.tv_nsec() as u32); | ||
assert!(time0.duration_since(UNIX_EPOCH).unwrap() <= rduration); | ||
assert!(rduration <= time1.duration_since(UNIX_EPOCH).unwrap()); | ||
// Close socket | ||
nix::unistd::close(in_socket).unwrap(); | ||
} |