summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/accept.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
commit8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch)
treedf55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-bottom-half/sources/accept.c
parentInitial commit. (diff)
downloadwasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.tar.xz
wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.zip
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-bottom-half/sources/accept.c')
-rw-r--r--libc-bottom-half/sources/accept.c51
1 files changed, 51 insertions, 0 deletions
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 <sys/socket.h>
+
+#include <assert.h>
+#include <wasi/api.h>
+#include <errno.h>
+#include <string.h>
+
+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;
+}