Skip to content

Commit

Permalink
OpenBSD: Fix keepalive_ms and set_keepalive_ms.
Browse files Browse the repository at this point in the history
OpenBSD (as of relase 6.6) does not support the socket option TCP_KEEPIDLE.
To be using SO_KEEPALIVE instead made it compile but was not correct
and was failing at runtime.

NetBSD does support the option and should therefore be treated like generic
unix.  The use of SO_KEEPALIVE as the KEEPALIVE_OPTION on NetBSD was not
correct.
  • Loading branch information
Jozef Hatala committed Mar 9, 2020
1 parent 942df89 commit a87d94a
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,9 @@ impl<T: AsRawSocket> AsSock for T {
cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
use libc::TCP_KEEPALIVE as KEEPALIVE_OPTION;
} else if #[cfg(any(target_os = "openbsd", target_os = "netbsd"))] {
use libc::SO_KEEPALIVE as KEEPALIVE_OPTION;
} else if #[cfg(target_os = "openbsd")] {
// OpenBSD does not have a TCP_KEEPIDLE setsockopt (as of release 6.6).
// The sysctl variable net.inet.tcp.keepidle controls the setting globally.
} else if #[cfg(unix)] {
use libc::TCP_KEEPIDLE as KEEPALIVE_OPTION;
} else if #[cfg(target_os = "redox")] {
Expand Down Expand Up @@ -731,9 +732,13 @@ impl TcpStreamExt for TcpStream {
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int));
if let Some(dur) = keepalive {
try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int));
cfg_if! {
if #[cfg(not(target_os = "openbsd"))] {
if let Some(dur) = keepalive {
try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int));
}
}
}
Ok(())
}
Expand All @@ -745,9 +750,15 @@ impl TcpStreamExt for TcpStream {
if keepalive == 0 {
return Ok(None)
}
let secs = try!(get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION));
Ok(Some((secs as u32) * 1000))
cfg_if! {
if #[cfg(target_os = "openbsd")] {
Ok(None)
} else {
let secs = try!(get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION));
Ok(Some((secs as u32) * 1000))
}
}
}

#[cfg(windows)]
Expand Down

0 comments on commit a87d94a

Please sign in to comment.