Skip to content

TCP_FASTOPEN option for Socket #336

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ impl Socket {
///
/// <https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options>
///
/// `value` is a boolean with only `0` and `1`.
/// `value` is a boolean with only `0` and `1`. Any `value` > `1` will be set to `1`.
///
/// ## Linux
///
Expand All @@ -1481,15 +1481,15 @@ impl Socket {
///
/// ## macOS
///
/// `value` is a boolean with only `0` and `1`.
/// `value` is a boolean with only `0` and `1`. Any `value` > `1` will be set to `1`.
///
/// ## FreeBSD
///
/// FreeBSD supports TCP Fast Open since 12.0.
///
/// Example program: <https://people.freebsd.org/~pkelsey/tfo-tools/tfo-srv.c>
///
/// `value` is a boolean with only `0` and `1`.
/// `value` is a boolean with only `0` and `1`. Any `value` > `1` will be set to `1`.
#[cfg(any(
target_os = "linux",
target_os = "android",
Expand All @@ -1500,7 +1500,11 @@ impl Socket {
target_os = "tvos",
target_os = "windows"
))]
pub fn set_tcp_fastopen(&self, value: u32) -> io::Result<()> {
pub fn set_tcp_fastopen(&self, mut value: u32) -> io::Result<()> {
if !cfg!(any(target_os = "linux", target_os = "android")) && value > 1 {
value = 1;
}

unsafe {
setsockopt::<c_int>(
self.as_raw(),
Expand Down
5 changes: 3 additions & 2 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,11 @@ fn tcp_fastopen() {
let bsaddr = SockAddr::from(baddr);
socket.bind(&bsaddr).unwrap();
socket.listen(128).unwrap();
socket.set_tcp_fastopen(5).unwrap();

if cfg!(any(target_os = "linux", target_os = "android")) {
socket.set_tcp_fastopen(5).unwrap();
assert_eq!(socket.tcp_fastopen().unwrap(), 5);
} else {
socket.set_tcp_fastopen(1).unwrap();
assert_eq!(socket.tcp_fastopen().unwrap(), 1);
}
}