Skip to content

Commit

Permalink
Merge #1002
Browse files Browse the repository at this point in the history
1002: Add IP_RECVIF & IP_RECVDSTADDR. r=asomers a=pusateri

Add IP_RECVIF & IP_RECVDSTADDR on freebsd, ios, macos, netbsd, openbsd
Include IP_PKTINFO on netbsd
Include IP6_PKTINFO on netbsd, openbsd.

FreeBSD/OpenBSD do not support IP_PKTINFO for IPv4 but use IP_RECVIF for interface index and use IP_RECVDSTADDR for destination address.
NetBSD and macOS also support IP_RECVIF and IP_RECVDSTADDR in addition to IP_PKTINFO for IPv4.
(For IPv6, all use IPV6_PKTINFO)

Co-authored-by: Tom Pusateri <pusateri@bangj.com>
  • Loading branch information
bors[bot] and pusateri committed Jan 30, 2019
2 parents 148871e + 147f791 commit 7ae7a1f
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Add IP_RECVIF & IP_RECVDSTADDR. Enable IP_PKTINFO and IP6_PKTINFO on netbsd/openbsd.
([#1002](https://github.com/nix-rust/nix/pull/1002))
### Changed
### Fixed
### Removed
Expand Down
110 changes: 110 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,22 @@ pub enum ControlMessage<'a> {
target_os = "macos"
))]
Ipv6PacketInfo(&'a libc::in6_pktinfo),
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Ipv4RecvIf(&'a libc::sockaddr_dl),
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
Ipv4RecvDstAddr(&'a libc::in_addr),

/// Catch-all variant for unimplemented cmsg types.
#[doc(hidden)]
Expand Down Expand Up @@ -594,6 +610,26 @@ impl<'a> ControlMessage<'a> {
ControlMessage::Ipv6PacketInfo(pktinfo) => {
mem::size_of_val(pktinfo)
},
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvIf(dl) => {
mem::size_of_val(dl)
},
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvDstAddr(inaddr) => {
mem::size_of_val(inaddr)
},
ControlMessage::Unknown(UnknownCmsg(_, bytes)) => {
mem::size_of_val(bytes)
}
Expand Down Expand Up @@ -622,6 +658,22 @@ impl<'a> ControlMessage<'a> {
target_os = "macos"
))]
ControlMessage::Ipv6PacketInfo(_) => libc::IPPROTO_IPV6,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvIf(_) => libc::IPPROTO_IP,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvDstAddr(_) => libc::IPPROTO_IP,
ControlMessage::Unknown(ref cmsg) => cmsg.0.cmsg_level,
}
}
Expand All @@ -648,6 +700,22 @@ impl<'a> ControlMessage<'a> {
target_os = "macos"
))]
ControlMessage::Ipv6PacketInfo(_) => libc::IPV6_PKTINFO,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvIf(_) => libc::IP_RECVIF,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvDstAddr(_) => libc::IP_RECVDSTADDR,
ControlMessage::Unknown(ref cmsg) => cmsg.0.cmsg_type,
}
}
Expand Down Expand Up @@ -708,6 +776,26 @@ impl<'a> ControlMessage<'a> {
ControlMessage::Ipv6PacketInfo(pktinfo) => {
copy_bytes(pktinfo, buf)
}
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvIf(dl) => {
copy_bytes(dl, buf)
},
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
ControlMessage::Ipv4RecvDstAddr(inaddr) => {
copy_bytes(inaddr, buf)
},
ControlMessage::Unknown(_) => unreachable!(),
}
};
Expand Down Expand Up @@ -760,6 +848,28 @@ impl<'a> ControlMessage<'a> {
ControlMessage::Ipv4PacketInfo(
&*(data.as_ptr() as *const _))
}
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
(libc::IPPROTO_IP, libc::IP_RECVIF) => {
ControlMessage::Ipv4RecvIf(
&*(data.as_ptr() as *const _))
}
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
(libc::IPPROTO_IP, libc::IP_RECVDSTADDR) => {
ControlMessage::Ipv4RecvDstAddr(
&*(data.as_ptr() as *const _))
}

(_, _) => {
ControlMessage::Unknown(UnknownCmsg(header, data))
Expand Down
23 changes: 21 additions & 2 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,36 @@ sockopt_impl!(Both, TcpCongestion, libc::IPPROTO_TCP, libc::TCP_CONGESTION, OsSt
target_os = "android",
target_os = "ios",
target_os = "linux",
target_os = "macos"
target_os = "macos",
target_os = "netbsd",
))]
sockopt_impl!(Both, Ipv4PacketInfo, libc::IPPROTO_IP, libc::IP_PKTINFO, bool);
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos"
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
sockopt_impl!(Both, Ipv6RecvPacketInfo, libc::IPPROTO_IPV6, libc::IPV6_RECVPKTINFO, bool);
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
sockopt_impl!(Both, Ipv4RecvIf, libc::IPPROTO_IP, libc::IP_RECVIF, bool);
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
))]
sockopt_impl!(Both, Ipv4RecvDstAddr, libc::IPPROTO_IP, libc::IP_RECVDSTADDR, bool);


/*
Expand Down
Loading

0 comments on commit 7ae7a1f

Please sign in to comment.