Skip to content

Commit d6ef53f

Browse files
committed
adhere to conventions + fix feature check
1 parent 4427ba7 commit d6ef53f

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

src/socket.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -884,41 +884,6 @@ impl Socket {
884884
}
885885
}
886886

887-
/// IP_TRANSPARENT (since Linux 2.6.24)
888-
/// Setting this boolean option enables transparent proxying
889-
/// on this socket. This socket option allows the calling
890-
/// application to bind to a nonlocal IP address and operate
891-
/// both as a client and a server with the foreign address as
892-
/// the local endpoint. NOTE: this requires that routing be
893-
/// set up in a way that packets going to the foreign address
894-
/// are routed through the TProxy box (i.e., the system
895-
/// hosting the application that employs the IP_TRANSPARENT
896-
/// socket option). Enabling this socket option requires
897-
/// superuser privileges (the CAP_NET_ADMIN capability).
898-
///
899-
/// TProxy redirection with the iptables TPROXY target also
900-
/// requires that this option be set on the redirected socket.
901-
/// this feature is only available on linux
902-
#[cfg(any(target_os = "linux"))]
903-
pub fn set_ip_transparent(&self, transparent: bool) -> io::Result<()> {
904-
unsafe {
905-
setsockopt(
906-
self.inner,
907-
sys::IPPROTO_IP,
908-
libc::IP_TRANSPARENT,
909-
transparent as c_int,
910-
)
911-
}
912-
}
913-
/// Get whether the IP_TRANSPARENT is set for this socket.
914-
#[cfg(any(target_os = "linux"))]
915-
pub fn ip_transparent(&self) -> io::Result<bool> {
916-
unsafe {
917-
getsockopt::<c_int>(self.inner, sys::IPPROTO_IP, libc::IP_TRANSPARENT)
918-
.map(|transparent| transparent != 0)
919-
}
920-
}
921-
922887
/// Get the value of the `SO_SNDBUF` option on this socket.
923888
///
924889
/// For more information about this option, see [`set_send_buffer_size`].
@@ -990,6 +955,46 @@ fn into_linger(duration: Option<Duration>) -> sys::linger {
990955
/// * Linux: <https://man7.org/linux/man-pages/man7/ip.7.html>
991956
/// * Windows: <https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options>
992957
impl Socket {
958+
/// Get the value of the `IP_TRANSPARENTF` option on this socket.
959+
///
960+
/// For more information about this option, see [`set_ip_transparent`].
961+
///
962+
/// [`ip_transparent`]: Socket::set_ip_transparent
963+
#[cfg(all(feature = "all", target_os = "linux"))]
964+
pub fn ip_transparent(&self) -> io::Result<bool> {
965+
unsafe {
966+
getsockopt::<c_int>(self.inner, sys::IPPROTO_IP, libc::IP_TRANSPARENT)
967+
.map(|transparent| transparent != 0)
968+
}
969+
}
970+
971+
/// IP_TRANSPARENT (since Linux 2.6.24)
972+
/// Setting this boolean option enables transparent proxying
973+
/// on this socket. This socket option allows the calling
974+
/// application to bind to a nonlocal IP address and operate
975+
/// both as a client and a server with the foreign address as
976+
/// the local endpoint. NOTE: this requires that routing be
977+
/// set up in a way that packets going to the foreign address
978+
/// are routed through the TProxy box (i.e., the system
979+
/// hosting the application that employs the IP_TRANSPARENT
980+
/// socket option). Enabling this socket option requires
981+
/// superuser privileges (the CAP_NET_ADMIN capability).
982+
///
983+
/// TProxy redirection with the iptables TPROXY target also
984+
/// requires that this option be set on the redirected socket.
985+
/// this feature is only available on linux
986+
#[cfg(all(feature = "all", target_os = "linux"))]
987+
pub fn set_ip_transparent(&self, transparent: bool) -> io::Result<()> {
988+
unsafe {
989+
setsockopt(
990+
self.inner,
991+
sys::IPPROTO_IP,
992+
libc::IP_TRANSPARENT,
993+
transparent as c_int,
994+
)
995+
}
996+
}
997+
993998
/// Join a multicast group using `IP_ADD_MEMBERSHIP` option on this socket.
994999
///
9951000
/// This function specifies a new multicast group for this socket to join.

tests/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ test!(
10471047
mss,
10481048
set_mss(256)
10491049
);
1050-
#[cfg(any(target_os = "linux"))]
1050+
#[cfg(all(feature = "all", target_os = "linux"))]
10511051
test!(
10521052
#[ignore = "setting `IP_TRANSPARENT` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
10531053
ip_transparent,

0 commit comments

Comments
 (0)