-
Notifications
You must be signed in to change notification settings - Fork 692
support PKTINFO cmsgs for Linux/macOS/iOS #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -414,6 +414,14 @@ impl<'a> Iterator for CmsgIterator<'a> { | |
Some(ControlMessage::ScmTimestamp( | ||
&*(cmsg_data.as_ptr() as *const _))) | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lists should be alphabetized (android, ios, linux, macos). This applies to all of your |
||
(libc::IPPROTO_IPV6, libc::IPV6_PKTINFO) => unsafe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FreeBSD also defines |
||
Some(ControlMessage::Ipv6PacketInfo(& *(cmsg_data.as_ptr() as *const _))) | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
(libc::IPPROTO_IP, libc::IP_PKTINFO) => unsafe { | ||
Some(ControlMessage::Ipv4PacketInfo(& *(cmsg_data.as_ptr() as *const _))) | ||
}, | ||
(_, _) => unsafe { | ||
Some(ControlMessage::Unknown(UnknownCmsg( | ||
cmsg, | ||
|
@@ -504,6 +512,10 @@ pub enum ControlMessage<'a> { | |
/// nix::unistd::close(in_socket).unwrap(); | ||
/// ``` | ||
ScmTimestamp(&'a TimeVal), | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
Ipv4PacketInfo(&'a libc::in_pktinfo), | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
Ipv6PacketInfo(&'a libc::in6_pktinfo), | ||
#[doc(hidden)] | ||
Unknown(UnknownCmsg<'a>), | ||
} | ||
|
@@ -538,6 +550,14 @@ impl<'a> ControlMessage<'a> { | |
ControlMessage::ScmTimestamp(t) => { | ||
mem::size_of_val(t) | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
ControlMessage::Ipv4PacketInfo(pktinfo) => { | ||
mem::size_of_val(pktinfo) | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
ControlMessage::Ipv6PacketInfo(pktinfo) => { | ||
mem::size_of_val(pktinfo) | ||
}, | ||
ControlMessage::Unknown(UnknownCmsg(_, bytes)) => { | ||
mem::size_of_val(bytes) | ||
} | ||
|
@@ -586,6 +606,46 @@ impl<'a> ControlMessage<'a> { | |
|
||
copy_bytes(t, buf); | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
ControlMessage::Ipv4PacketInfo(pktinfo) => { | ||
let cmsg = cmsghdr { | ||
cmsg_len: self.len() as _, | ||
cmsg_level: libc::IPPROTO_IP, | ||
cmsg_type: libc::IP_PKTINFO, | ||
..mem::uninitialized() | ||
}; | ||
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(pktinfo, buf); | ||
}, | ||
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))] | ||
ControlMessage::Ipv6PacketInfo(pktinfo) => { | ||
let cmsg = cmsghdr { | ||
cmsg_len: self.len() as _, | ||
cmsg_level: libc::IPPROTO_IPV6, | ||
cmsg_type: libc::IPV6_PKTINFO, | ||
..mem::uninitialized() | ||
}; | ||
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(pktinfo, buf); | ||
}, | ||
ControlMessage::Unknown(UnknownCmsg(orig_cmsg, bytes)) => { | ||
copy_bytes(orig_cmsg, buf); | ||
copy_bytes(bytes, buf); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to "Added `PKTINFO` support for `cmsg` on ... platforms" (note the backticks)