Skip to content

Commit e29579e

Browse files
committed
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 e29579e

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

expected/wasm32-wasi/defined-symbols.txt

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

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

Lines changed: 2 additions & 1 deletion
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)