blob: 902e7316f5812220266957583e403aa97843fcf7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}
|