Skip to content

Commit

Permalink
Add support for SO_TIMESTAMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolvereness committed Jul 9, 2017
1 parent 46e77b5 commit 107d8af
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Added `nix::sys::socket::ControlMessage::ScmTimestamp`
([#663](https://github.com/nix-rust/nix/pull/663))
- Added `sys::signal::SigAction::{ flags, mask, handler}`
([#611](https://github.com/nix-rust/nix/pull/609)
- Added `nix::sys::pthread::pthread_self`
Expand Down
2 changes: 2 additions & 0 deletions src/sys/socket/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod os {

// Ancillary message types
pub const SCM_RIGHTS: c_int = libc::SCM_RIGHTS;
pub const SCM_TIMESTAMP: c_int = libc::SCM_TIMESTAMP;
}

// Not all of these constants exist on freebsd
Expand Down Expand Up @@ -235,6 +236,7 @@ mod os {

// Ancillary message types
pub const SCM_RIGHTS: c_int = 1;
pub const SCM_TIMESTAMP: c_int = libc::SCM_TIMESTAMP;
}

#[cfg(target_os = "dragonfly")]
Expand Down
32 changes: 32 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libc::{c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t};
use std::{mem, ptr, slice};
use std::os::unix::io::RawFd;
use sys::uio::IoVec;
use sys::time::TimeVal;

mod addr;
mod consts;
Expand Down Expand Up @@ -162,6 +163,10 @@ impl<'a> Iterator for CmsgIterator<'a> {
slice::from_raw_parts(
&cmsg.cmsg_data as *const _ as *const _, 1)))
},
(SOL_SOCKET, SCM_TIMESTAMP) => unsafe {
Some(ControlMessage::ScmTimestamp(
&*(&cmsg.cmsg_data as *const _ as *const _)))
},
(_, _) => unsafe {
Some(ControlMessage::Unknown(UnknownCmsg(
&cmsg,
Expand All @@ -182,6 +187,11 @@ pub enum ControlMessage<'a> {
/// "Ancillary messages" section of the
/// [unix(7) man page](http://man7.org/linux/man-pages/man7/unix.7.html).
ScmRights(&'a [RawFd]),
/// A message of type SCM_TIMESTAMP, containing the time the packet
/// was received by the kernel. See the kernel's explanation in
/// "SO_TIMESTAMP" of
/// [networking/timestamping](https://www.kernel.org/doc/Documentation/networking/timestamping.txt).
ScmTimestamp(&'a TimeVal),
#[doc(hidden)]
Unknown(UnknownCmsg<'a>),
}
Expand All @@ -207,6 +217,9 @@ impl<'a> ControlMessage<'a> {
ControlMessage::ScmRights(fds) => {
mem::size_of_val(fds)
},
ControlMessage::ScmTimestamp(t) => {
mem::size_of_val(t)
},
ControlMessage::Unknown(UnknownCmsg(_, bytes)) => {
mem::size_of_val(bytes)
}
Expand Down Expand Up @@ -237,6 +250,25 @@ impl<'a> ControlMessage<'a> {

copy_bytes(fds, buf);
},
ControlMessage::ScmTimestamp(t) => {
let cmsg = cmsghdr {
cmsg_len: self.len() as type_of_cmsg_len,
cmsg_level: SOL_SOCKET,
cmsg_type: SCM_TIMESTAMP,
cmsg_data: [],
};
copy_bytes(&cmsg, buf);

let padlen = cmsg_align(mem::size_of_val(&cmsg)) -
mem::size_of_val(&cmsg);

let mut tmpbuf = &mut [][..];
mem::swap(&mut tmpbuf, buf);
let (_padding, mut remainder) = tmpbuf.split_at_mut(padlen);
mem::swap(buf, &mut remainder);

copy_bytes(t, buf);
},
ControlMessage::Unknown(UnknownCmsg(orig_cmsg, bytes)) => {
copy_bytes(orig_cmsg, buf);
copy_bytes(bytes, buf);
Expand Down
1 change: 1 addition & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ sockopt_impl!(GetOnly, SockType, consts::SOL_SOCKET, consts::SO_TYPE, super::Soc
sockopt_impl!(GetOnly, AcceptConn, consts::SOL_SOCKET, consts::SO_ACCEPTCONN, bool);
#[cfg(target_os = "linux")]
sockopt_impl!(GetOnly, OriginalDst, consts::SOL_IP, consts::SO_ORIGINAL_DST, sockaddr_in);
sockopt_impl!(Both, ReceiveTimestamp, consts::SOL_SOCKET, consts::SO_TIMESTAMP, bool);

/*
*
Expand Down

0 comments on commit 107d8af

Please sign in to comment.