Skip to content

feat: add support for accept and accept4 #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ _start
a64l
abort
abs
accept
accept4
access
acos
acosf
Expand Down
51 changes: 51 additions & 0 deletions libc-bottom-half/sources/accept.c
Original file line number Diff line number Diff line change
@@ -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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the rationale for validating, but dropping the only flag besides nonblock is because it isn't in wasi, right? https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fdflags cc @deadprogram who's implementing tinygo tinygo-org/tinygo#2748 🎖️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry tagged wrong person meant @rvolosatovs 😊

Copy link
Contributor

@rvolosatovs rvolosatovs Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this behavior is not relevant for the TinyGo implementation, since this is a wrapper for sock_accept, whereas in the TinyGo PR we call it directly via WASI

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;
}
3 changes: 2 additions & 1 deletion libc-top-half/musl/include/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down