Skip to content

Commit 4427ba7

Browse files
committed
add test and a get method
1 parent 4ed4691 commit 4427ba7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/socket.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,16 +900,24 @@ impl Socket {
900900
/// requires that this option be set on the redirected socket.
901901
/// this feature is only available on linux
902902
#[cfg(any(target_os = "linux"))]
903-
pub fn set_ip_transparent(&self) -> io::Result<()> {
903+
pub fn set_ip_transparent(&self, transparent: bool) -> io::Result<()> {
904904
unsafe {
905905
setsockopt(
906906
self.inner,
907907
sys::IPPROTO_IP,
908908
libc::IP_TRANSPARENT,
909-
1 as c_int,
909+
transparent as c_int,
910910
)
911911
}
912912
}
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+
}
913921

914922
/// Get the value of the `SO_SNDBUF` option on this socket.
915923
///

tests/socket.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,12 @@ test!(
10471047
mss,
10481048
set_mss(256)
10491049
);
1050+
#[cfg(any(target_os = "linux"))]
1051+
test!(
1052+
#[ignore = "setting `IP_TRANSPARENT` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
1053+
ip_transparent,
1054+
set_ip_transparent(true)
1055+
);
10501056
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
10511057
test!(
10521058
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]

0 commit comments

Comments
 (0)