Skip to content

Commit 89b029b

Browse files
Merge #1283
1283: Add support for TCP_KEEPCNT and TCP_KEEPINTVL TCP keepalive options. r=asomers a=yoav-steinberg Co-authored-by: Yoav Steinberg <[email protected]>
2 parents 566e2d7 + fde7181 commit 89b029b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414
(#[1300](https://github.com/nix-rust/nix/pull/1300))
1515
- Add support for Vsock on Android rather than just Linux.
1616
(#[1301](https://github.com/nix-rust/nix/pull/1301))
17-
17+
- Added `TCP_KEEPCNT` and `TCP_KEEPINTVL` TCP keepalive options.
18+
(#[1283](https://github.com/nix-rust/nix/pull/1283))
1819
### Changed
1920
- Expose `SeekData` and `SeekHole` on all Linux targets
2021
(#[1284](https://github.com/nix-rust/nix/pull/1284))

src/sys/socket/sockopt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ sockopt_impl!(Both, TcpKeepAlive, libc::IPPROTO_TCP, libc::TCP_KEEPALIVE, u32);
252252
target_os = "linux",
253253
target_os = "nacl"))]
254254
sockopt_impl!(Both, TcpKeepIdle, libc::IPPROTO_TCP, libc::TCP_KEEPIDLE, u32);
255+
sockopt_impl!(Both, TcpKeepCount, libc::IPPROTO_TCP, libc::TCP_KEEPCNT, u32);
256+
sockopt_impl!(Both, TcpKeepInterval, libc::IPPROTO_TCP, libc::TCP_KEEPINTVL, u32);
255257
sockopt_impl!(Both, RcvBuf, libc::SOL_SOCKET, libc::SO_RCVBUF, usize);
256258
sockopt_impl!(Both, SndBuf, libc::SOL_SOCKET, libc::SO_SNDBUF, usize);
257259
#[cfg(any(target_os = "android", target_os = "linux"))]

test/sys/test_sockopt.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,28 @@ fn test_bindtodevice() {
6767
val
6868
);
6969
}
70+
71+
#[test]
72+
fn test_so_tcp_keepalive() {
73+
let fd = socket(AddressFamily::Inet, SockType::Stream, SockFlag::empty(), SockProtocol::Tcp).unwrap();
74+
setsockopt(fd, sockopt::KeepAlive, &true).unwrap();
75+
assert_eq!(getsockopt(fd, sockopt::KeepAlive).unwrap(), true);
76+
77+
#[cfg(any(target_os = "android",
78+
target_os = "dragonfly",
79+
target_os = "freebsd",
80+
target_os = "linux",
81+
target_os = "nacl"))] {
82+
let x = getsockopt(fd, sockopt::TcpKeepIdle).unwrap();
83+
setsockopt(fd, sockopt::TcpKeepIdle, &(x + 1)).unwrap();
84+
assert_eq!(getsockopt(fd, sockopt::TcpKeepIdle).unwrap(), x + 1);
85+
86+
let x = getsockopt(fd, sockopt::TcpKeepCount).unwrap();
87+
setsockopt(fd, sockopt::TcpKeepCount, &(x + 1)).unwrap();
88+
assert_eq!(getsockopt(fd, sockopt::TcpKeepCount).unwrap(), x + 1);
89+
90+
let x = getsockopt(fd, sockopt::TcpKeepInterval).unwrap();
91+
setsockopt(fd, sockopt::TcpKeepInterval, &(x + 1)).unwrap();
92+
assert_eq!(getsockopt(fd, sockopt::TcpKeepInterval).unwrap(), x + 1);
93+
}
94+
}

0 commit comments

Comments
 (0)