Skip to content
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

Added TcpFastOpenConnect sockopt #2247

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog/2247.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `TcpFastOpenConnect` sockopt to support `TCP_FASTOPEN_CONNECT` available on linux.
20 changes: 20 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,26 @@ sockopt_impl!(
libc::TCP_USER_TIMEOUT,
u32
);
#[cfg(linux_android)]
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
/// Enables TCP Fast Open (RFC 7413) on a connecting socket. If a fast open
/// cookie is not available (first attempt to connect), `connect` syscall
/// will behave as usual, except for internally trying to solicit a cookie
/// from remote peer. When cookie is available, the next `connect` syscall
/// will immediately succeed without actually establishing TCP connection.
/// The connection establishment will be defered till the next `write` or
/// `sendmsg` syscalls on the socket, allowing TCP prtocol to establish
/// connection and send data in the same packets. Note: calling `read` right
/// after `connect` without `write` on the socket will cause the blocking
/// socket to be blocked forever.
TcpFastOpenConnect,
Both,
libc::IPPROTO_TCP,
libc::TCP_FASTOPEN_CONNECT,
bool
);
sockopt_impl!(
/// Sets or gets the maximum socket receive buffer in bytes.
RcvBuf,
Expand Down
24 changes: 24 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,27 @@ fn test_ip_bind_address_no_port() {
"getting IP_BIND_ADDRESS_NO_PORT on an inet stream socket should succeed",
));
}

#[test]
#[cfg(linux_android)]
fn test_tcp_fast_open_connect() {
let fd = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
SockProtocol::Tcp,
)
.unwrap();
setsockopt(&fd, sockopt::TcpFastOpenConnect, &true).expect(
"setting TCP_FASTOPEN_CONNECT on an inet stream socket should succeed",
);
assert!(getsockopt(&fd, sockopt::TcpFastOpenConnect).expect(
"getting TCP_FASTOPEN_CONNECT on an inet stream socket should succeed",
));
setsockopt(&fd, sockopt::TcpFastOpenConnect, &false).expect(
"unsetting TCP_FASTOPEN_CONNECT on an inet stream socket should succeed",
);
assert!(!getsockopt(&fd, sockopt::TcpFastOpenConnect).expect(
"getting TCP_FASTOPEN_CONNECT on an inet stream socket should succeed",
));
}