Skip to content

Commit e1564dc

Browse files
committed
Add (set_)ip_transparent_v6 for target_os=android/linux
1 parent ae6581e commit e1564dc

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/sys/unix.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,6 +3191,54 @@ impl crate::Socket {
31913191
)
31923192
}
31933193
}
3194+
3195+
/// Get the value of the `IPV6_TRANSPARENT` option on this socket.
3196+
///
3197+
/// For more information about this option, see [`set_ip_transparent_v6`].
3198+
///
3199+
/// [`set_ip_transparent_v6`]: crate::Socket::set_ip_transparent_v6
3200+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
3201+
#[cfg_attr(
3202+
docsrs,
3203+
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
3204+
)]
3205+
pub fn ip_transparent_v6(&self) -> io::Result<bool> {
3206+
unsafe {
3207+
getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_IPV6, libc::IPV6_TRANSPARENT)
3208+
.map(|transparent| transparent != 0)
3209+
}
3210+
}
3211+
3212+
/// Set the value of the `IPV6_TRANSPARENT` option on this socket.
3213+
///
3214+
/// Setting this boolean option enables transparent proxying
3215+
/// on this socket. This socket option allows the calling
3216+
/// application to bind to a nonlocal IP address and operate
3217+
/// both as a client and a server with the foreign address as
3218+
/// the local endpoint. NOTE: this requires that routing be
3219+
/// set up in a way that packets going to the foreign address
3220+
/// are routed through the TProxy box (i.e., the system
3221+
/// hosting the application that employs the IPV6_TRANSPARENT
3222+
/// socket option). Enabling this socket option requires
3223+
/// superuser privileges (the `CAP_NET_ADMIN` capability).
3224+
///
3225+
/// TProxy redirection with the iptables TPROXY target also
3226+
/// requires that this option be set on the redirected socket.
3227+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
3228+
#[cfg_attr(
3229+
docsrs,
3230+
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
3231+
)]
3232+
pub fn set_ip_transparent_v6(&self, transparent: bool) -> io::Result<()> {
3233+
unsafe {
3234+
setsockopt(
3235+
self.as_raw(),
3236+
libc::IPPROTO_IPV6,
3237+
libc::IPV6_TRANSPARENT,
3238+
transparent as c_int,
3239+
)
3240+
}
3241+
}
31943242
}
31953243

31963244
/// See [`Socket::dccp_available_ccids`].

tests/socket.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,12 @@ test!(
13811381
ip_transparent,
13821382
set_ip_transparent_v4(true)
13831383
);
1384+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
1385+
test!(
1386+
#[ignore = "setting `IPV6_TRANSPARENT` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
1387+
ip_transparent_v6,
1388+
set_ip_transparent_v6(true)
1389+
);
13841390
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
13851391
test!(
13861392
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]

0 commit comments

Comments
 (0)