]>
Commit | Line | Data |
---|---|---|
320054e8 DG |
1 | // Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/ |
2 | // | |
3 | // SPDX-License-Identifier: BSD-2-Clause | |
4 | ||
5 | #include <common/errno.h> | |
6 | ||
7 | #include <sys/socket.h> | |
8 | ||
9 | #include <assert.h> | |
446cb3f1 | 10 | #include <wasi/api.h> |
320054e8 DG |
11 | #include <errno.h> |
12 | #include <stdint.h> | |
13 | ||
446cb3f1 | 14 | #ifdef __wasilibc_unmodified_upstream |
320054e8 DG |
15 | static_assert(MSG_PEEK == __WASI_SOCK_RECV_PEEK, "Value mismatch"); |
16 | static_assert(MSG_WAITALL == __WASI_SOCK_RECV_WAITALL, "Value mismatch"); | |
446cb3f1 DG |
17 | #else |
18 | static_assert(MSG_PEEK == __WASI_RIFLAGS_RECV_PEEK, "Value mismatch"); | |
19 | static_assert(MSG_WAITALL == __WASI_RIFLAGS_RECV_WAITALL, "Value mismatch"); | |
20 | #endif | |
320054e8 DG |
21 | |
22 | ssize_t recv(int socket, void *restrict buffer, size_t length, int flags) { | |
23 | // Validate flags. | |
24 | if ((flags & ~(MSG_PEEK | MSG_WAITALL)) != 0) { | |
25 | errno = EOPNOTSUPP; | |
26 | return -1; | |
27 | } | |
28 | ||
29 | // Prepare input parameters. | |
30 | __wasi_iovec_t iov = {.buf = buffer, .buf_len = length}; | |
31 | #ifdef __wasilibc_unmodified_upstream // send/recv | |
32 | __wasi_recv_in_t ri = { | |
33 | .ri_data = &iov, | |
34 | .ri_data_len = 1, | |
35 | .ri_flags = flags, | |
36 | }; | |
37 | #else | |
38 | __wasi_iovec_t *ri_data = &iov; | |
39 | size_t ri_data_len = 1; | |
40 | __wasi_riflags_t ri_flags = flags; | |
41 | #endif | |
42 | ||
43 | // Perform system call. | |
44 | #ifdef __wasilibc_unmodified_upstream // send/recv | |
45 | __wasi_recv_out_t ro; | |
46 | __wasi_errno_t error = __wasi_sock_recv(socket, &ri, &ro); | |
47 | #else | |
48 | size_t ro_datalen; | |
49 | __wasi_roflags_t ro_flags; | |
50 | __wasi_errno_t error = __wasi_sock_recv(socket, | |
51 | ri_data, ri_data_len, ri_flags, | |
52 | &ro_datalen, | |
53 | &ro_flags); | |
54 | #endif | |
55 | if (error != 0) { | |
56 | errno = errno_fixup_socket(socket, error); | |
57 | return -1; | |
58 | } | |
59 | #ifdef __wasilibc_unmodified_upstream // send/recv | |
60 | return ro.ro_datalen; | |
61 | #else | |
62 | return ro_datalen; | |
63 | #endif | |
64 | } |