Skip to content

Commit 2cd3915

Browse files
committed
Migrate original_dst to socket.rs out of os specific files
Signed-off-by: Keith Mattix II <[email protected]>
1 parent 6c160fa commit 2cd3915

File tree

4 files changed

+161
-118
lines changed

4 files changed

+161
-118
lines changed

src/socket.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use std::net::{self, Ipv4Addr, Shutdown};
1818
use std::os::unix::io::{FromRawFd, IntoRawFd};
1919
#[cfg(windows)]
2020
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
21+
#[cfg(windows)]
22+
use windows_sys::Win32::Networking::WinSock::{
23+
IP6T_SO_ORIGINAL_DST, SOL_IP, SO_ORIGINAL_DST, SOCKET_ERROR
24+
};
2125
use std::time::Duration;
2226

2327
use crate::sys::{self, c_int, getsockopt, setsockopt, Bool};
@@ -27,6 +31,47 @@ use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
2731
#[cfg(not(target_os = "redox"))]
2832
use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};
2933

34+
#[cfg(all(feature = "all", windows))]
35+
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "all"))))]
36+
/// Helper macro to execute a system call that returns an `io::Result`.
37+
macro_rules! syscall {
38+
($fn: ident ( $($arg: expr),* $(,)* ), $err_test: path, $err_value: expr) => {{
39+
#[allow(unused_unsafe)]
40+
let res = unsafe { windows_sys::Win32::Networking::WinSock::$fn($($arg, )*) };
41+
if $err_test(&res, &$err_value) {
42+
Err(io::Error::last_os_error())
43+
} else {
44+
Ok(res)
45+
}
46+
}};
47+
}
48+
49+
#[cfg(all(
50+
feature = "all",
51+
any(
52+
target_os = "android",
53+
target_os = "dragonfly",
54+
target_os = "freebsd",
55+
target_os = "fuchsia",
56+
target_os = "hurd",
57+
target_os = "illumos",
58+
target_os = "linux",
59+
target_os = "netbsd",
60+
target_os = "openbsd",
61+
)))]
62+
/// Helper macro to execute a system call that returns an `io::Result`.
63+
macro_rules! syscall {
64+
($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
65+
#[allow(unused_unsafe)]
66+
let res = unsafe { libc::$fn($($arg, )*) };
67+
if res == -1 {
68+
Err(std::io::Error::last_os_error())
69+
} else {
70+
Ok(res)
71+
}
72+
}};
73+
}
74+
3075
/// Owned wrapper around a system socket.
3176
///
3277
/// This type simply wraps an instance of a file descriptor (`c_int`) on Unix
@@ -2205,6 +2250,120 @@ impl Socket {
22052250
)
22062251
}
22072252
}
2253+
2254+
/// Get the value for the `SO_ORIGINAL_DST` option on this socket.
2255+
///
2256+
/// This value contains the original destination IPv4 address of the connection
2257+
/// redirected using `iptables` `REDIRECT` or `TPROXY`.
2258+
#[cfg(all(
2259+
feature = "all",
2260+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2261+
))]
2262+
#[cfg_attr(
2263+
docsrs,
2264+
doc(cfg(all(
2265+
feature = "all",
2266+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2267+
)))
2268+
)]
2269+
pub fn original_dst(&self) -> io::Result<SockAddr> {
2270+
// Safety: `getsockopt` initialises the `SockAddr` for us.
2271+
unsafe {
2272+
SockAddr::try_init(|storage, len| {
2273+
syscall!(getsockopt(
2274+
self.as_raw(),
2275+
libc::SOL_IP,
2276+
libc::SO_ORIGINAL_DST,
2277+
storage.cast(),
2278+
len
2279+
))
2280+
})
2281+
}
2282+
.map(|(_, addr)| addr)
2283+
}
2284+
2285+
/// Get the value for the `IP6T_SO_ORIGINAL_DST` option on this socket.
2286+
///
2287+
/// This value contains the original destination IPv6 address of the connection
2288+
/// redirected using `ip6tables` `REDIRECT` or `TPROXY`.
2289+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
2290+
#[cfg_attr(
2291+
docsrs,
2292+
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
2293+
)]
2294+
pub fn original_dst_ipv6(&self) -> io::Result<SockAddr> {
2295+
// Safety: `getsockopt` initialises the `SockAddr` for us.
2296+
unsafe {
2297+
SockAddr::try_init(|storage, len| {
2298+
syscall!(getsockopt(
2299+
self.as_raw(),
2300+
libc::SOL_IPV6,
2301+
libc::IP6T_SO_ORIGINAL_DST,
2302+
storage.cast(),
2303+
len
2304+
))
2305+
})
2306+
}
2307+
.map(|(_, addr)| addr)
2308+
}
2309+
2310+
/// Get the value for the `SO_ORIGINAL_DST` option on this socket.
2311+
/// Only valid for sockets in accepting mode.
2312+
///
2313+
/// Note: if using this function in a proxy context, you must query the
2314+
/// redirect records for this socket and set them on the outbound socket
2315+
/// created by your proxy in order for any OS level firewall rules to be
2316+
/// applied. Read more in the Windows bind and connect redirection
2317+
/// [documentation](https://learn.microsoft.com/en-us/windows-hardware/drivers/network/using-bind-or-connect-redirection).
2318+
#[cfg(all(feature = "all", target_os = "windows"))]
2319+
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "all"))))]
2320+
pub fn original_dst(&self) -> io::Result<SockAddr> {
2321+
unsafe {
2322+
SockAddr::try_init(|storage, len| {
2323+
syscall!(
2324+
getsockopt(
2325+
self.as_raw(),
2326+
SOL_IP as i32,
2327+
SO_ORIGINAL_DST as i32,
2328+
storage.cast(),
2329+
len,
2330+
),
2331+
PartialEq::eq,
2332+
SOCKET_ERROR
2333+
)
2334+
})
2335+
}
2336+
.map(|(_, addr)| addr)
2337+
}
2338+
2339+
/// Get the value for the `IP6T_SO_ORIGINAL_DST` option on this socket.
2340+
/// Only valid for sockets in accepting mode.
2341+
///
2342+
/// Note: if using this function in a proxy context, you must query the
2343+
/// redirect records for this socket and set them on the outbound socket
2344+
/// created by your proxy in order for any OS level firewall rules to be
2345+
/// applied. Read more in the Windows bind and connect redirection
2346+
/// [documentation](https://learn.microsoft.com/en-us/windows-hardware/drivers/network/using-bind-or-connect-redirection).
2347+
#[cfg(all(feature = "all", target_os = "windows"))]
2348+
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "all"))))]
2349+
pub fn original_dst_ipv6(&self) -> io::Result<SockAddr> {
2350+
unsafe {
2351+
SockAddr::try_init(|storage, len| {
2352+
syscall!(
2353+
getsockopt(
2354+
self.as_raw(),
2355+
SOL_IP as i32,
2356+
IP6T_SO_ORIGINAL_DST as i32,
2357+
storage.cast(),
2358+
len,
2359+
),
2360+
PartialEq::eq,
2361+
SOCKET_ERROR
2362+
)
2363+
})
2364+
}
2365+
.map(|(_, addr)| addr)
2366+
}
22082367
}
22092368

22102369
impl Read for Socket {

src/sys/unix.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,62 +2402,6 @@ impl crate::Socket {
24022402
}
24032403
}
24042404

2405-
/// Get the value for the `SO_ORIGINAL_DST` option on this socket.
2406-
///
2407-
/// This value contains the original destination IPv4 address of the connection
2408-
/// redirected using `iptables` `REDIRECT` or `TPROXY`.
2409-
#[cfg(all(
2410-
feature = "all",
2411-
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2412-
))]
2413-
#[cfg_attr(
2414-
docsrs,
2415-
doc(cfg(all(
2416-
feature = "all",
2417-
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2418-
)))
2419-
)]
2420-
pub fn original_dst(&self) -> io::Result<SockAddr> {
2421-
// Safety: `getsockopt` initialises the `SockAddr` for us.
2422-
unsafe {
2423-
SockAddr::try_init(|storage, len| {
2424-
syscall!(getsockopt(
2425-
self.as_raw(),
2426-
libc::SOL_IP,
2427-
libc::SO_ORIGINAL_DST,
2428-
storage.cast(),
2429-
len
2430-
))
2431-
})
2432-
}
2433-
.map(|(_, addr)| addr)
2434-
}
2435-
2436-
/// Get the value for the `IP6T_SO_ORIGINAL_DST` option on this socket.
2437-
///
2438-
/// This value contains the original destination IPv6 address of the connection
2439-
/// redirected using `ip6tables` `REDIRECT` or `TPROXY`.
2440-
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
2441-
#[cfg_attr(
2442-
docsrs,
2443-
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
2444-
)]
2445-
pub fn original_dst_ipv6(&self) -> io::Result<SockAddr> {
2446-
// Safety: `getsockopt` initialises the `SockAddr` for us.
2447-
unsafe {
2448-
SockAddr::try_init(|storage, len| {
2449-
syscall!(getsockopt(
2450-
self.as_raw(),
2451-
libc::SOL_IPV6,
2452-
libc::IP6T_SO_ORIGINAL_DST,
2453-
storage.cast(),
2454-
len
2455-
))
2456-
})
2457-
}
2458-
.map(|(_, addr)| addr)
2459-
}
2460-
24612405
/// Copies data between a `file` and this socket using the `sendfile(2)`
24622406
/// system call. Because this copying is done within the kernel,
24632407
/// `sendfile()` is more efficient than the combination of `read(2)` and

src/sys/windows.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use windows_sys::Win32::Networking::WinSock::{
2727
WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
2828
};
2929
#[cfg(feature = "all")]
30-
use windows_sys::Win32::Networking::WinSock::{
31-
IP6T_SO_ORIGINAL_DST, SOL_IP, SO_ORIGINAL_DST, SO_PROTOCOL_INFOW,
32-
};
30+
use windows_sys::Win32::Networking::WinSock::SO_PROTOCOL_INFOW;
3331
use windows_sys::Win32::System::Threading::INFINITE;
3432

3533
use crate::{MsgHdr, RecvFlags, SockAddr, TcpKeepalive, Type};
@@ -929,64 +927,6 @@ impl crate::Socket {
929927
}
930928
}
931929

932-
/// Get the value for the `SO_ORIGINAL_DST` option on this socket.
933-
/// Only valid for sockets in accepting mode.
934-
///
935-
/// Note: if using this function in a proxy context, you must query the
936-
/// redirect records for this socket and set them on the outbound socket
937-
/// created by your proxy in order for any OS level firewall rules to be
938-
/// applied. Read more in the Windows bind and connect redirection
939-
/// [documentation](https://learn.microsoft.com/en-us/windows-hardware/drivers/network/using-bind-or-connect-redirection).
940-
#[cfg(feature = "all")]
941-
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "all"))))]
942-
pub fn original_dst(&self) -> io::Result<SockAddr> {
943-
unsafe {
944-
SockAddr::try_init(|storage, len| {
945-
syscall!(
946-
getsockopt(
947-
self.as_raw(),
948-
SOL_IP as i32,
949-
SO_ORIGINAL_DST as i32,
950-
storage.cast(),
951-
len,
952-
),
953-
PartialEq::eq,
954-
SOCKET_ERROR
955-
)
956-
})
957-
}
958-
.map(|(_, addr)| addr)
959-
}
960-
961-
/// Get the value for the `IP6T_SO_ORIGINAL_DST` option on this socket.
962-
/// Only valid for sockets in accepting mode.
963-
///
964-
/// Note: if using this function in a proxy context, you must query the
965-
/// redirect records for this socket and set them on the outbound socket
966-
/// created by your proxy in order for any OS level firewall rules to be
967-
/// applied. Read more in the Windows bind and connect redirection
968-
/// [documentation](https://learn.microsoft.com/en-us/windows-hardware/drivers/network/using-bind-or-connect-redirection).
969-
#[cfg(feature = "all")]
970-
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "all"))))]
971-
pub fn original_dst_ipv6(&self) -> io::Result<SockAddr> {
972-
unsafe {
973-
SockAddr::try_init(|storage, len| {
974-
syscall!(
975-
getsockopt(
976-
self.as_raw(),
977-
SOL_IP as i32,
978-
IP6T_SO_ORIGINAL_DST as i32,
979-
storage.cast(),
980-
len,
981-
),
982-
PartialEq::eq,
983-
SOCKET_ERROR
984-
)
985-
})
986-
}
987-
.map(|(_, addr)| addr)
988-
}
989-
990930
/// Returns the [`Protocol`] of this socket by checking the `SO_PROTOCOL_INFOW`
991931
/// option on this socket.
992932
///

tests/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ fn unix_sockets_supported() -> bool {
542542
Ok(_) => {}
543543
Err(err)
544544
if err.raw_os_error()
545-
== Some(windows_sys::Win32::Networking::WinSock::WSAEAFNOSUPPORT as i32) =>
545+
== Some(windows_sys::Win32::Networking::WinSock::WSAEAFNOSUPPORT) =>
546546
{
547547
return false;
548548
}

0 commit comments

Comments
 (0)