Skip to content

Commit 5dcc717

Browse files
Add support for TCP_KEEPCNT and TCP_KEEPINTVL TCP keepalive options.
1 parent bf77f50 commit 5dcc717

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Added
88
- Added Netlink protocol families to the `SockProtocol` enum
99
(#[1289](https://github.com/nix-rust/nix/pull/1289))
10+
- Added `TCP_KEEPCNT` and `TCP_KEEPINTVL` TCP keepalive options.
11+
(#[1283](https://github.com/nix-rust/nix/pull/1283))
1012
### Changed
1113
- Expose `SeekData` and `SeekHole` on all Linux targets
1214
(#[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)