Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d06fb38

Browse files
committedMay 5, 2022
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 <[email protected]>
1 parent 7302f33 commit d06fb38

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
 

‎expected/wasm32-wasi/defined-symbols.txt

+2
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ _start
363363
a64l
364364
abort
365365
abs
366+
accept
367+
accept4
366368
access
367369
acos
368370
acosf

‎libc-bottom-half/sources/accept.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
3+
#include <sys/socket.h>
4+
5+
#include <assert.h>
6+
#include <wasi/api.h>
7+
#include <errno.h>
8+
#include <string.h>
9+
10+
int accept(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen) {
11+
int ret = -1;
12+
13+
__wasi_errno_t error = __wasi_sock_accept(socket, 0, &ret);
14+
15+
if (error != 0) {
16+
errno = error;
17+
return -1;
18+
}
19+
20+
// Clear sockaddr to indicate undefined address
21+
memset(addr, 0, *addrlen);
22+
// might be AF_UNIX or AF_INET
23+
addr->sa_family = AF_UNSPEC;
24+
*addrlen = sizeof(struct sockaddr);
25+
26+
return ret;
27+
}
28+
29+
int accept4(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags) {
30+
int ret = -1;
31+
32+
if (flags & ~(SOCK_NONBLOCK | SOCK_CLOEXEC)) {
33+
errno = EINVAL;
34+
return -1;
35+
}
36+
37+
__wasi_errno_t error = __wasi_sock_accept(socket, (flags & SOCK_NONBLOCK) ? __WASI_FDFLAGS_NONBLOCK : 0, &ret);
38+
39+
if (error != 0) {
40+
errno = error;
41+
return -1;
42+
}
43+
44+
// Clear sockaddr to indicate undefined address
45+
memset(addr, 0, *addrlen);
46+
// might be AF_UNIX or AF_INET
47+
addr->sa_family = AF_UNSPEC;
48+
*addrlen = sizeof(struct sockaddr);
49+
50+
return ret;
51+
}

‎libc-top-half/musl/include/sys/socket.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ int shutdown (int, int);
404404
int bind (int, const struct sockaddr *, socklen_t);
405405
int connect (int, const struct sockaddr *, socklen_t);
406406
int listen (int, int);
407+
#endif
408+
407409
int accept (int, struct sockaddr *__restrict, socklen_t *__restrict);
408410
int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int);
409-
#endif
410411

411412
#ifdef __wasilibc_unmodified_upstream /* WASI has no getsockname/getpeername */
412413
int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);

0 commit comments

Comments
 (0)
Please sign in to comment.