Skip to content

Commit 780d0d7

Browse files
committed
feat: allow setting IPPROTO_TCP as protocol in socket syscall
1 parent 98cb1d3 commit 780d0d7

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/shims/unix/socket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
316316
flags
317317
);
318318
}
319-
if protocol != 0 {
319+
if protocol != 0 && protocol != this.eval_libc_i32("IPPROTO_TCP") {
320320
throw_unsup_format!(
321321
"socket: socket protocol {protocol} is unsupported, \
322-
only 0 is allowed"
322+
only IPPROTO_TCP and 0 are allowed"
323323
);
324324
}
325325

tests/pass-dep/libc/libc-socket.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const TEST_BYTES: &[u8] = b"these are some test bytes!";
1616

1717
fn main() {
1818
test_create_close();
19+
test_create_close_tcp();
1920
test_bind_ipv4();
2021
test_bind_ipv4_reuseaddr();
2122
test_set_reuseaddr_invalid_len();
@@ -59,6 +60,21 @@ fn test_create_close() {
5960
unsafe { errno_check(libc::close(sockfd)) };
6061
}
6162

63+
/// Test creating a socket and then closing it afterwards but we explicitly
64+
/// specify that the TCP protocol should be used.
65+
fn test_create_close_tcp() {
66+
let sockfd = unsafe {
67+
errno_result(libc::socket(libc::AF_INET, libc::SOCK_STREAM, libc::IPPROTO_TCP)).unwrap()
68+
};
69+
70+
let flags = unsafe { errno_result(libc::fcntl(sockfd, libc::F_GETFL, 0)).unwrap() };
71+
72+
// Ensure that socket is initially blocking.
73+
assert_eq!(flags & libc::O_NONBLOCK, 0);
74+
75+
unsafe { errno_check(libc::close(sockfd)) };
76+
}
77+
6278
fn test_bind_ipv4() {
6379
let sockfd =
6480
unsafe { errno_result(libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0)).unwrap() };

0 commit comments

Comments
 (0)