Merge pull request #4986 from WhySoBad/network-socket-allow-tcp-proto

Allow setting `IPPROTO_TCP` as protocol in `socket` syscall
This commit is contained in:
Ralf Jung
2026-04-29 11:44:26 +00:00
committed by GitHub
2 changed files with 18 additions and 2 deletions
+2 -2
View File
@@ -316,10 +316,10 @@ fn socket(
flags
);
}
if protocol != 0 {
if protocol != 0 && protocol != this.eval_libc_i32("IPPROTO_TCP") {
throw_unsup_format!(
"socket: socket protocol {protocol} is unsupported, \
only 0 is allowed"
only IPPROTO_TCP and 0 are allowed"
);
}
@@ -16,6 +16,7 @@
fn main() {
test_create_close();
test_create_close_tcp();
test_bind_ipv4();
test_bind_ipv4_reuseaddr();
test_set_reuseaddr_invalid_len();
@@ -59,6 +60,21 @@ fn test_create_close() {
unsafe { errno_check(libc::close(sockfd)) };
}
/// Test creating a socket and then closing it afterwards but we explicitly
/// specify that the TCP protocol should be used.
fn test_create_close_tcp() {
let sockfd = unsafe {
errno_result(libc::socket(libc::AF_INET, libc::SOCK_STREAM, libc::IPPROTO_TCP)).unwrap()
};
let flags = unsafe { errno_result(libc::fcntl(sockfd, libc::F_GETFL, 0)).unwrap() };
// Ensure that socket is initially blocking.
assert_eq!(flags & libc::O_NONBLOCK, 0);
unsafe { errno_check(libc::close(sockfd)) };
}
fn test_bind_ipv4() {
let sockfd =
unsafe { errno_result(libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0)).unwrap() };