Skip to content

Commit 3b2b501

Browse files
bors[bot]pusateri
andcommitted
Merge #1002
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 <[email protected]>
2 parents 148871e + 147f791 commit 3b2b501

File tree

4 files changed

+330
-92
lines changed

4 files changed

+330
-92
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]
77
### Added
8+
- Add IP_RECVIF & IP_RECVDSTADDR. Enable IP_PKTINFO and IP6_PKTINFO on netbsd/openbsd.
9+
([#1002](https://github.com/nix-rust/nix/pull/1002))
810
### Changed
911
### Fixed
1012
### Removed

src/sys/socket/mod.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ pub enum ControlMessage<'a> {
535535
target_os = "macos"
536536
))]
537537
Ipv6PacketInfo(&'a libc::in6_pktinfo),
538+
#[cfg(any(
539+
target_os = "freebsd",
540+
target_os = "ios",
541+
target_os = "macos",
542+
target_os = "netbsd",
543+
target_os = "openbsd",
544+
))]
545+
Ipv4RecvIf(&'a libc::sockaddr_dl),
546+
#[cfg(any(
547+
target_os = "freebsd",
548+
target_os = "ios",
549+
target_os = "macos",
550+
target_os = "netbsd",
551+
target_os = "openbsd",
552+
))]
553+
Ipv4RecvDstAddr(&'a libc::in_addr),
538554

539555
/// Catch-all variant for unimplemented cmsg types.
540556
#[doc(hidden)]
@@ -594,6 +610,26 @@ impl<'a> ControlMessage<'a> {
594610
ControlMessage::Ipv6PacketInfo(pktinfo) => {
595611
mem::size_of_val(pktinfo)
596612
},
613+
#[cfg(any(
614+
target_os = "freebsd",
615+
target_os = "ios",
616+
target_os = "macos",
617+
target_os = "netbsd",
618+
target_os = "openbsd",
619+
))]
620+
ControlMessage::Ipv4RecvIf(dl) => {
621+
mem::size_of_val(dl)
622+
},
623+
#[cfg(any(
624+
target_os = "freebsd",
625+
target_os = "ios",
626+
target_os = "macos",
627+
target_os = "netbsd",
628+
target_os = "openbsd",
629+
))]
630+
ControlMessage::Ipv4RecvDstAddr(inaddr) => {
631+
mem::size_of_val(inaddr)
632+
},
597633
ControlMessage::Unknown(UnknownCmsg(_, bytes)) => {
598634
mem::size_of_val(bytes)
599635
}
@@ -622,6 +658,22 @@ impl<'a> ControlMessage<'a> {
622658
target_os = "macos"
623659
))]
624660
ControlMessage::Ipv6PacketInfo(_) => libc::IPPROTO_IPV6,
661+
#[cfg(any(
662+
target_os = "freebsd",
663+
target_os = "ios",
664+
target_os = "macos",
665+
target_os = "netbsd",
666+
target_os = "openbsd",
667+
))]
668+
ControlMessage::Ipv4RecvIf(_) => libc::IPPROTO_IP,
669+
#[cfg(any(
670+
target_os = "freebsd",
671+
target_os = "ios",
672+
target_os = "macos",
673+
target_os = "netbsd",
674+
target_os = "openbsd",
675+
))]
676+
ControlMessage::Ipv4RecvDstAddr(_) => libc::IPPROTO_IP,
625677
ControlMessage::Unknown(ref cmsg) => cmsg.0.cmsg_level,
626678
}
627679
}
@@ -648,6 +700,22 @@ impl<'a> ControlMessage<'a> {
648700
target_os = "macos"
649701
))]
650702
ControlMessage::Ipv6PacketInfo(_) => libc::IPV6_PKTINFO,
703+
#[cfg(any(
704+
target_os = "freebsd",
705+
target_os = "ios",
706+
target_os = "macos",
707+
target_os = "netbsd",
708+
target_os = "openbsd",
709+
))]
710+
ControlMessage::Ipv4RecvIf(_) => libc::IP_RECVIF,
711+
#[cfg(any(
712+
target_os = "freebsd",
713+
target_os = "ios",
714+
target_os = "macos",
715+
target_os = "netbsd",
716+
target_os = "openbsd",
717+
))]
718+
ControlMessage::Ipv4RecvDstAddr(_) => libc::IP_RECVDSTADDR,
651719
ControlMessage::Unknown(ref cmsg) => cmsg.0.cmsg_type,
652720
}
653721
}
@@ -708,6 +776,26 @@ impl<'a> ControlMessage<'a> {
708776
ControlMessage::Ipv6PacketInfo(pktinfo) => {
709777
copy_bytes(pktinfo, buf)
710778
}
779+
#[cfg(any(
780+
target_os = "freebsd",
781+
target_os = "ios",
782+
target_os = "macos",
783+
target_os = "netbsd",
784+
target_os = "openbsd",
785+
))]
786+
ControlMessage::Ipv4RecvIf(dl) => {
787+
copy_bytes(dl, buf)
788+
},
789+
#[cfg(any(
790+
target_os = "freebsd",
791+
target_os = "ios",
792+
target_os = "macos",
793+
target_os = "netbsd",
794+
target_os = "openbsd",
795+
))]
796+
ControlMessage::Ipv4RecvDstAddr(inaddr) => {
797+
copy_bytes(inaddr, buf)
798+
},
711799
ControlMessage::Unknown(_) => unreachable!(),
712800
}
713801
};
@@ -760,6 +848,28 @@ impl<'a> ControlMessage<'a> {
760848
ControlMessage::Ipv4PacketInfo(
761849
&*(data.as_ptr() as *const _))
762850
}
851+
#[cfg(any(
852+
target_os = "freebsd",
853+
target_os = "ios",
854+
target_os = "macos",
855+
target_os = "netbsd",
856+
target_os = "openbsd",
857+
))]
858+
(libc::IPPROTO_IP, libc::IP_RECVIF) => {
859+
ControlMessage::Ipv4RecvIf(
860+
&*(data.as_ptr() as *const _))
861+
}
862+
#[cfg(any(
863+
target_os = "freebsd",
864+
target_os = "ios",
865+
target_os = "macos",
866+
target_os = "netbsd",
867+
target_os = "openbsd",
868+
))]
869+
(libc::IPPROTO_IP, libc::IP_RECVDSTADDR) => {
870+
ControlMessage::Ipv4RecvDstAddr(
871+
&*(data.as_ptr() as *const _))
872+
}
763873

764874
(_, _) => {
765875
ControlMessage::Unknown(UnknownCmsg(header, data))

src/sys/socket/sockopt.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,36 @@ sockopt_impl!(Both, TcpCongestion, libc::IPPROTO_TCP, libc::TCP_CONGESTION, OsSt
275275
target_os = "android",
276276
target_os = "ios",
277277
target_os = "linux",
278-
target_os = "macos"
278+
target_os = "macos",
279+
target_os = "netbsd",
279280
))]
280281
sockopt_impl!(Both, Ipv4PacketInfo, libc::IPPROTO_IP, libc::IP_PKTINFO, bool);
281282
#[cfg(any(
282283
target_os = "android",
283284
target_os = "freebsd",
284285
target_os = "ios",
285286
target_os = "linux",
286-
target_os = "macos"
287+
target_os = "macos",
288+
target_os = "netbsd",
289+
target_os = "openbsd",
287290
))]
288291
sockopt_impl!(Both, Ipv6RecvPacketInfo, libc::IPPROTO_IPV6, libc::IPV6_RECVPKTINFO, bool);
292+
#[cfg(any(
293+
target_os = "freebsd",
294+
target_os = "ios",
295+
target_os = "macos",
296+
target_os = "netbsd",
297+
target_os = "openbsd",
298+
))]
299+
sockopt_impl!(Both, Ipv4RecvIf, libc::IPPROTO_IP, libc::IP_RECVIF, bool);
300+
#[cfg(any(
301+
target_os = "freebsd",
302+
target_os = "ios",
303+
target_os = "macos",
304+
target_os = "netbsd",
305+
target_os = "openbsd",
306+
))]
307+
sockopt_impl!(Both, Ipv4RecvDstAddr, libc::IPPROTO_IP, libc::IP_RECVDSTADDR, bool);
289308

290309

291310
/*

0 commit comments

Comments
 (0)