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

Merged
Merged
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: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,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 @@ -470,7 +469,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 @@ -548,6 +546,7 @@ impl TcpKeepalive {
target_os = "tvos",
target_os = "watchos",
target_os = "cygwin",
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 @@ -2116,6 +2116,7 @@ impl Socket {
target_os = "tvos",
target_os = "watchos",
target_os = "cygwin",
target_os = "windows",
)
))]
pub fn keepalive_retries(&self) -> io::Result<u32> {
Expand Down
22 changes: 18 additions & 4 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub(crate) const SOCK_SEQPACKET: c_int =
pub(crate) use windows_sys::Win32::Networking::WinSock::{
IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP,
};

#[cfg(feature = "all")]
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 @@ -737,17 +740,18 @@ 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 tcp_keepalive = tcp_keepalive {
onoff: 1,
keepalivetime: into_ms(keepalive.time),
keepaliveinterval: into_ms(keepalive.interval),
};

let mut out = 0;
syscall!(
WSAIoctl(
socket,
SIO_KEEPALIVE_VALS,
&mut keepalive as *mut _ as *mut _,
&mut tcp_keepalive as *mut _ as *mut _,
size_of::<tcp_keepalive>() as _,
ptr::null_mut(),
0,
Expand All @@ -757,8 +761,18 @@ pub(crate) fn set_tcp_keepalive(socket: Socket, keepalive: &TcpKeepalive) -> io:
),
PartialEq::eq,
SOCKET_ERROR
)
.map(|_| ())
)?;
if let Some(retries) = keepalive.retries {
unsafe {
setsockopt(
socket,
WinSock::IPPROTO_TCP,
WinSock::TCP_KEEPCNT,
retries as c_int,
)?
}
}
Ok(())
}

/// Caller must ensure `T` is the correct type for `level` and `optname`.
Expand Down
2 changes: 2 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,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 @@ -950,6 +951,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