Skip to content

Commit b0732a3

Browse files
link2xtThomasdezeeuw
authored andcommitted
Fix TCP keepalive handling on Haiku and OpenBSD
On Haiku and OpenBSD don't call `setsockopt(fd, IPPROTO_TCP, SO_KEEPALIVE, secs)`, it is incorrect because `SO_KEEPALIVE` belongs to `SOL_SOCKET` layer, because it accepts a boolean instead of the number of seconds, and because there is no way to set keepalive idle time per socket on these operating systems. On Haiku and OpenBSD it is only possible to enable and disable keepalives, and it is already done with `setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, 1)` in a portable way in `Socket.set_keepalive()` defined in `src/socket.rs`.
1 parent 1c67209 commit b0732a3

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ impl TcpKeepalive {
340340
/// Set the amount of time after which TCP keepalive probes will be sent on
341341
/// idle connections.
342342
///
343-
/// This will set the value of `SO_KEEPALIVE` on OpenBSD and Haiku,
344-
/// `TCP_KEEPALIVE` on macOS and iOS, and `TCP_KEEPIDLE` on all other Unix
345-
/// operating systems. On Windows, this sets the value of the
346-
/// `tcp_keepalive` struct's `keepalivetime` field.
343+
/// This will set `TCP_KEEPALIVE` on macOS and iOS, and
344+
/// `TCP_KEEPIDLE` on all other Unix operating systems, except
345+
/// OpenBSD and Haiku which don't support any way to set this
346+
/// option. On Windows, this sets the value of the `tcp_keepalive`
347+
/// struct's `keepalivetime` field.
347348
///
348349
/// Some platforms specify this value in seconds, so sub-second
349350
/// specifications may be omitted.

src/sys/unix.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,9 @@ pub(crate) use libc::{TCP_KEEPCNT, TCP_KEEPINTVL};
135135
// See this type in the Windows file.
136136
pub(crate) type Bool = c_int;
137137

138-
#[cfg(any(target_os = "openbsd", target_os = "haiku"))]
139-
use libc::SO_KEEPALIVE as KEEPALIVE_TIME;
140138
#[cfg(target_vendor = "apple")]
141139
use libc::TCP_KEEPALIVE as KEEPALIVE_TIME;
142-
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_vendor = "apple")))]
140+
#[cfg(not(any(target_vendor = "apple")))]
143141
use libc::TCP_KEEPIDLE as KEEPALIVE_TIME;
144142

145143
/// Helper macro to execute a system call that returns an `io::Result`.
@@ -876,6 +874,7 @@ pub(crate) fn keepalive_time(fd: Socket) -> io::Result<Duration> {
876874
}
877875

878876
pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Result<()> {
877+
#[cfg(not(any(target_os = "haiku", target_os = "openbsd")))]
879878
if let Some(time) = keepalive.time {
880879
let secs = into_secs(time);
881880
unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? }

0 commit comments

Comments
 (0)