Update __wasi_sock_accept signature to match wasi_snapshot_preview1 (#1531)
The function was introduced to WASI about half a year ago after it already existed in WAMR. It caused problems with compiling `wasi_socket_ext.c` with the wasi-sdk that already had this hostcall exported (wasi-sdk >= 15). The approach we take is the following: - we update WASI interface to be compatible with the wasi_snapshot_preview1 - compilation with `wasi_socket_ext.c` supports both wasi_sdk >= 15 and wasi_sdk < 15 (although we intend to drop support for < 15 at one point of time) - we override `accept()` from wasi-libc - we do that because `accept()` in `wasi-libc` doesn't support returning address (as it doesn't have `getpeername()` implemented), so `wasi_socket_ext.c` offers more functionality right now Resolves #1167 and #1528. [1] https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/witx/wasi_snapshot_preview1.witx
This commit is contained in:
@ -140,8 +140,10 @@ struct addrinfo {
|
||||
struct addrinfo *ai_next; /* Pointer to next in list. */
|
||||
};
|
||||
|
||||
#ifndef __WASI_RIGHTS_SOCK_ACCEPT
|
||||
int
|
||||
accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
#endif
|
||||
|
||||
int
|
||||
bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
@ -191,21 +193,30 @@ void
|
||||
freeaddrinfo(struct addrinfo *res);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* __wasi_sock_accept was introduced in wasi-sdk v15. To
|
||||
* temporarily maintain backward compatibility with the old
|
||||
* wasi-sdk, we explicitly add that implementation here so it works
|
||||
* with older versions of the SDK.
|
||||
*/
|
||||
#ifndef __WASI_RIGHTS_SOCK_ACCEPT
|
||||
/**
|
||||
* Accept a connection on a socket
|
||||
* Note: This is similar to `accept`
|
||||
*/
|
||||
int32_t
|
||||
__imported_wasi_snapshot_preview1_sock_accept(int32_t arg0, int32_t arg1)
|
||||
__imported_wasi_snapshot_preview1_sock_accept(int32_t arg0, int32_t arg1,
|
||||
int32_t arg2)
|
||||
__attribute__((__import_module__("wasi_snapshot_preview1"),
|
||||
__import_name__("sock_accept")));
|
||||
|
||||
static inline __wasi_errno_t
|
||||
__wasi_sock_accept(__wasi_fd_t fd, __wasi_fd_t *fd_new)
|
||||
__wasi_sock_accept(__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new)
|
||||
{
|
||||
return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_accept(
|
||||
(int32_t)fd, (int32_t)fd_new);
|
||||
(int32_t)fd, (int32_t)flags, (int32_t)fd_new);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns the local address to which the socket is bound.
|
||||
|
||||
@ -140,7 +140,7 @@ accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
__wasi_fd_t new_sockfd;
|
||||
__wasi_errno_t error;
|
||||
|
||||
error = __wasi_sock_accept(sockfd, &new_sockfd);
|
||||
error = __wasi_sock_accept(sockfd, 0, &new_sockfd);
|
||||
HANDLE_ERROR(error)
|
||||
|
||||
error = getpeername(new_sockfd, addr, addrlen);
|
||||
|
||||
Reference in New Issue
Block a user