]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/cloudlibc/src/libc/sys/socket/send.c
WASI libc prototype implementation.
[wasi-libc.git] / libc-bottom-half / cloudlibc / src / libc / sys / socket / send.c
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>
10 #include <wasi/core.h>
11 #include <errno.h>
12
13 ssize_t send(int socket, const void *buffer, size_t length, int flags) {
14 // This implementation does not support any flags.
15 if (flags != 0) {
16 errno = EOPNOTSUPP;
17 return -1;
18 }
19
20 // Prepare input parameters.
21 __wasi_ciovec_t iov = {.buf = buffer, .buf_len = length};
22 #ifdef __wasilibc_unmodified_upstream // send/recv
23 __wasi_send_in_t si = {
24 .si_data = &iov,
25 .si_data_len = 1,
26 };
27 #else
28 __wasi_ciovec_t *si_data = &iov;
29 size_t si_data_len = 1;
30 __wasi_siflags_t si_flags = 0;
31 #endif
32
33 // Perform system call.
34 #ifdef __wasilibc_unmodified_upstream // send/recv
35 __wasi_send_out_t so;
36 __wasi_errno_t error = __wasi_sock_send(socket, &si, &so);
37 #else
38 size_t so_datalen;
39 __wasi_errno_t error = __wasi_sock_send(socket, si_data, si_data_len, si_flags, &so_datalen);
40 #endif
41 if (error != 0) {
42 errno = errno_fixup_socket(socket, error);
43 return -1;
44 }
45 #ifdef __wasilibc_unmodified_upstream // send/recv
46 return so.so_datalen;
47 #else
48 return so_datalen;
49 #endif
50 }