From 07e7b673553b6aee52bd78b5db394a3f5607255b Mon Sep 17 00:00:00 2001 From: keithmattix Date: Fri, 21 Feb 2025 11:29:36 -0600 Subject: [PATCH 1/3] Stash Signed-off-by: keithmattix --- Cargo.toml | 2 +- src/lib.rs | 3 +-- src/socket.rs | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18331f61..9a2d68e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ features = ["all"] libc = "0.2.150" [target.'cfg(windows)'.dependencies.windows-sys] -version = "0.52" +version = "0.62" features = [ "Win32_Foundation", "Win32_Networking_WinSock", diff --git a/src/lib.rs b/src/lib.rs index 946714ae..3eba8400 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -436,7 +436,6 @@ pub struct TcpKeepalive { target_os = "openbsd", target_os = "redox", target_os = "solaris", - target_os = "windows", target_os = "nto", target_os = "espidf", target_os = "vita", @@ -465,7 +464,6 @@ impl TcpKeepalive { target_os = "openbsd", target_os = "redox", target_os = "solaris", - target_os = "windows", target_os = "nto", target_os = "espidf", target_os = "vita", @@ -541,6 +539,7 @@ impl TcpKeepalive { target_os = "netbsd", target_os = "tvos", target_os = "watchos", + target_os = "windows", ) ))] pub const fn with_retries(self, retries: u32) -> Self { diff --git a/src/socket.rs b/src/socket.rs index 1bed96f5..8d0488ff 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2089,6 +2089,7 @@ impl Socket { target_os = "netbsd", target_os = "tvos", target_os = "watchos", + target_os = "windows", ) ))] pub fn keepalive_retries(&self) -> io::Result { From 9b9d47c554744c913dbeb8f33767072c79df842a Mon Sep 17 00:00:00 2001 From: Keith Mattix II Date: Fri, 21 Feb 2025 17:58:59 +0000 Subject: [PATCH 2/3] Bump windows sys and add support for retries Signed-off-by: Keith Mattix II --- Cargo.toml | 3 ++- src/sys/windows.rs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a2d68e1..741871e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ targets = [ "x86_64-apple-darwin", "x86_64-pc-solaris", "x86_64-pc-windows-msvc", + "x86_64-pc-windows-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-fuchsia", "x86_64-unknown-illumos", @@ -54,7 +55,7 @@ features = ["all"] libc = "0.2.150" [target.'cfg(windows)'.dependencies.windows-sys] -version = "0.62" +version = "0.59" features = [ "Win32_Foundation", "Win32_Networking_WinSock", diff --git a/src/sys/windows.rs b/src/sys/windows.rs index 10852f19..9bd696f5 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -59,6 +59,8 @@ pub(crate) const SOCK_SEQPACKET: c_int = pub(crate) use windows_sys::Win32::Networking::WinSock::{ IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP, }; + +pub(crate) use windows_sys::Win32::Networking::WinSock::TCP_KEEPCNT; // Used in `SockAddr`. pub(crate) use windows_sys::Win32::Networking::WinSock::{ SOCKADDR as sockaddr, SOCKADDR_IN as sockaddr_in, SOCKADDR_IN6 as sockaddr_in6, @@ -727,17 +729,24 @@ fn into_ms(duration: Option) -> u32 { } pub(crate) fn set_tcp_keepalive(socket: Socket, keepalive: &TcpKeepalive) -> io::Result<()> { - let mut keepalive = tcp_keepalive { + let mut w_keepalive = tcp_keepalive { onoff: 1, keepalivetime: into_ms(keepalive.time), keepaliveinterval: into_ms(keepalive.interval), }; + + + if let Some(retries) = keepalive.retries { + unsafe { + setsockopt(socket, WinSock::IPPROTO_TCP, WinSock::TCP_KEEPCNT, retries as c_int)? + } + } let mut out = 0; syscall!( WSAIoctl( socket, SIO_KEEPALIVE_VALS, - &mut keepalive as *mut _ as *mut _, + &mut w_keepalive as *mut _ as *mut _, size_of::() as _, ptr::null_mut(), 0, @@ -931,7 +940,13 @@ pub(crate) fn unix_sockaddr(path: &Path) -> io::Result { storage.sun_family = crate::sys::AF_UNIX as sa_family_t; // `storage` was initialized to zero above, so the path is // already null terminated. - storage.sun_path[..bytes.len()].copy_from_slice(bytes); + storage.sun_path[..bytes.len()].copy_from_slice( + bytes + .iter() + .map(|b| *b as i8) + .collect::>() + .as_slice(), + ); let base = storage as *const _ as usize; let path = &storage.sun_path as *const _ as usize; From 3b6ea10cc0fde38a8819e05b338f9a3629c03ad6 Mon Sep 17 00:00:00 2001 From: Keith Mattix II Date: Fri, 21 Feb 2025 18:05:47 +0000 Subject: [PATCH 3/3] Run fmt Signed-off-by: Keith Mattix II --- src/sys/windows.rs | 8 ++++++-- tests/socket.rs | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sys/windows.rs b/src/sys/windows.rs index 9bd696f5..90dd489e 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -735,10 +735,14 @@ pub(crate) fn set_tcp_keepalive(socket: Socket, keepalive: &TcpKeepalive) -> io: keepaliveinterval: into_ms(keepalive.interval), }; - if let Some(retries) = keepalive.retries { unsafe { - setsockopt(socket, WinSock::IPPROTO_TCP, WinSock::TCP_KEEPCNT, retries as c_int)? + setsockopt( + socket, + WinSock::IPPROTO_TCP, + WinSock::TCP_KEEPCNT, + retries as c_int, + )? } } let mut out = 0; diff --git a/tests/socket.rs b/tests/socket.rs index 3776a4b4..8c9951ed 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -893,6 +893,7 @@ fn tcp_keepalive() { target_os = "netbsd", target_os = "tvos", target_os = "watchos", + target_os = "windows" ) ))] let params = params.with_retries(10); @@ -943,6 +944,7 @@ fn tcp_keepalive() { target_os = "netbsd", target_os = "tvos", target_os = "watchos", + target_os = "windows", ) ))] assert_eq!(socket.keepalive_retries().unwrap(), 10);