From 63e4489d01ad0262d995c6d9a5f1a1bab719c917 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Mon, 2 May 2022 12:21:42 +0200 Subject: [PATCH] feat: add support for accept and accept4 Since the socket address of the accepted socket is unknown, all bytes are set to zero and the length is truncated to the size of the generic `struct sockaddr`. Signed-off-by: Harald Hoyer --- expected/wasm32-wasi/defined-symbols.txt | 2 + libc-bottom-half/sources/accept.c | 51 ++++++++++++++++++++++++ libc-top-half/musl/include/sys/socket.h | 3 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 libc-bottom-half/sources/accept.c diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index fb7234a..a0d7066 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -363,6 +363,8 @@ _start a64l abort abs +accept +accept4 access acos acosf diff --git a/libc-bottom-half/sources/accept.c b/libc-bottom-half/sources/accept.c new file mode 100644 index 0000000..902e731 --- /dev/null +++ b/libc-bottom-half/sources/accept.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: BSD-2-Clause + +#include + +#include +#include +#include +#include + +int accept(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen) { + int ret = -1; + + __wasi_errno_t error = __wasi_sock_accept(socket, 0, &ret); + + if (error != 0) { + errno = error; + return -1; + } + + // Clear sockaddr to indicate undefined address + memset(addr, 0, *addrlen); + // might be AF_UNIX or AF_INET + addr->sa_family = AF_UNSPEC; + *addrlen = sizeof(struct sockaddr); + + return ret; +} + +int accept4(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags) { + int ret = -1; + + if (flags & ~(SOCK_NONBLOCK | SOCK_CLOEXEC)) { + errno = EINVAL; + return -1; + } + + __wasi_errno_t error = __wasi_sock_accept(socket, (flags & SOCK_NONBLOCK) ? __WASI_FDFLAGS_NONBLOCK : 0, &ret); + + if (error != 0) { + errno = error; + return -1; + } + + // Clear sockaddr to indicate undefined address + memset(addr, 0, *addrlen); + // might be AF_UNIX or AF_INET + addr->sa_family = AF_UNSPEC; + *addrlen = sizeof(struct sockaddr); + + return ret; +} diff --git a/libc-top-half/musl/include/sys/socket.h b/libc-top-half/musl/include/sys/socket.h index cea24cf..a98f16b 100644 --- a/libc-top-half/musl/include/sys/socket.h +++ b/libc-top-half/musl/include/sys/socket.h @@ -404,9 +404,10 @@ int shutdown (int, int); int bind (int, const struct sockaddr *, socklen_t); int connect (int, const struct sockaddr *, socklen_t); int listen (int, int); +#endif + int accept (int, struct sockaddr *__restrict, socklen_t *__restrict); int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); -#endif #ifdef __wasilibc_unmodified_upstream /* WASI has no getsockname/getpeername */ int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict); -- 2.39.2