]>
Commit | Line | Data |
---|---|---|
320054e8 DG |
1 | // Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/ |
2 | // | |
3 | // SPDX-License-Identifier: BSD-2-Clause | |
4 | ||
320054e8 DG |
5 | #include <sys/socket.h> |
6 | ||
7 | #include <assert.h> | |
446cb3f1 | 8 | #include <wasi/api.h> |
320054e8 DG |
9 | #include <errno.h> |
10 | #include <stdint.h> | |
11 | ||
446cb3f1 DG |
12 | static_assert(MSG_PEEK == __WASI_RIFLAGS_RECV_PEEK, "Value mismatch"); |
13 | static_assert(MSG_WAITALL == __WASI_RIFLAGS_RECV_WAITALL, "Value mismatch"); | |
320054e8 DG |
14 | |
15 | ssize_t recv(int socket, void *restrict buffer, size_t length, int flags) { | |
16 | // Validate flags. | |
17 | if ((flags & ~(MSG_PEEK | MSG_WAITALL)) != 0) { | |
18 | errno = EOPNOTSUPP; | |
19 | return -1; | |
20 | } | |
21 | ||
22 | // Prepare input parameters. | |
23 | __wasi_iovec_t iov = {.buf = buffer, .buf_len = length}; | |
320054e8 DG |
24 | __wasi_iovec_t *ri_data = &iov; |
25 | size_t ri_data_len = 1; | |
26 | __wasi_riflags_t ri_flags = flags; | |
320054e8 DG |
27 | |
28 | // Perform system call. | |
320054e8 DG |
29 | size_t ro_datalen; |
30 | __wasi_roflags_t ro_flags; | |
31 | __wasi_errno_t error = __wasi_sock_recv(socket, | |
32 | ri_data, ri_data_len, ri_flags, | |
33 | &ro_datalen, | |
34 | &ro_flags); | |
320054e8 | 35 | if (error != 0) { |
320054e8 DG |
36 | return -1; |
37 | } | |
320054e8 | 38 | return ro_datalen; |
320054e8 | 39 | } |