Skip to content

Add tcp retries to windows #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -54,7 +55,7 @@ features = ["all"]
libc = "0.2.150"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52"
version = "0.59"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, can't accept that at the moment, see #526

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need a volunteer to implement #526 (comment)? I'm happy to take a stab at it, or pass the feedback to someone internally at MSFT

features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
Expand Down
25 changes: 22 additions & 3 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -727,17 +729,28 @@ fn into_ms(duration: Option<Duration>) -> 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::<tcp_keepalive>() as _,
ptr::null_mut(),
0,
Expand Down Expand Up @@ -931,7 +944,13 @@ pub(crate) fn unix_sockaddr(path: &Path) -> io::Result<SockAddr> {
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::<Vec<_>>()
.as_slice(),
);

let base = storage as *const _ as usize;
let path = &storage.sun_path as *const _ as usize;
Expand Down
2 changes: 2 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading