Skip to content

Commit 580efbd

Browse files
authored
wasip3: Get {g,s}etsockopt working (#725)
Move the implementation for WASIp2 to a shared location and then use `#define`s/`typedef`s to get the shared structure compiling since the functionality supported by WASIp2/WASIp3 is effectively the same.
1 parent 082ee46 commit 580efbd

8 files changed

Lines changed: 552 additions & 464 deletions

File tree

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
NS_PER_S
21
_CLOCK_MONOTONIC
32
_CLOCK_REALTIME
43
_Exit
@@ -343,6 +342,8 @@ __wasilibc_rmdirat
343342
__wasilibc_sockaddr_to_wasi
344343
__wasilibc_sockaddr_validate
345344
__wasilibc_stat
345+
__wasilibc_tcp_getsockopt
346+
__wasilibc_tcp_setsockopt
346347
__wasilibc_tell
347348
__wasilibc_unlinkat
348349
__wasilibc_unspecified_addr
@@ -1384,7 +1385,6 @@ tcp_result_u32_error_code_free
13841385
tcp_result_u64_error_code_free
13851386
tcp_result_u8_error_code_free
13861387
tcp_result_void_error_code_free
1387-
tcp_setsockopt
13881388
tcp_tcp_socket_drop_own
13891389
tdelete
13901390
tdestroy

expected/wasm32-wasip3/defined-symbols.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ __wasilibc_rmdirat
339339
__wasilibc_sockaddr_to_wasi
340340
__wasilibc_sockaddr_validate
341341
__wasilibc_stat
342+
__wasilibc_tcp_getsockopt
343+
__wasilibc_tcp_setsockopt
342344
__wasilibc_tell
343345
__wasilibc_unlinkat
344346
__wasilibc_unspecified_addr

libc-bottom-half/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ else()
145145
sources/socket.c
146146
sources/sockets_utils.c
147147
sources/sockopt.c
148+
sources/tcp_sockopt.c
148149
)
149150
endif()
150151

libc-bottom-half/headers/private/wasi/tcp.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,86 @@
66
#ifndef __wasip1__
77

88
#include <wasi/sockets_utils.h>
9+
#include <sys/socket.h>
10+
11+
// Normalize names on WASIp2 to the WASIp3-based names
12+
#ifdef __wasip2__
13+
typedef network_error_code_t sockets_error_code_t;
14+
typedef tcp_own_tcp_socket_t sockets_own_tcp_socket_t;
15+
typedef tcp_borrow_tcp_socket_t sockets_borrow_tcp_socket_t;
16+
typedef network_ip_address_family_t sockets_ip_address_family_t;
17+
#define sockets_borrow_tcp_socket tcp_borrow_tcp_socket
18+
#endif // __wasip2__
19+
20+
typedef struct {
21+
int dummy;
22+
} tcp_socket_state_unbound_t;
23+
typedef struct {
24+
int dummy;
25+
} tcp_socket_state_bound_t;
26+
typedef struct {
27+
int dummy;
28+
} tcp_socket_state_connecting_t;
29+
typedef struct {
30+
int dummy;
31+
} tcp_socket_state_listening_t;
32+
33+
// Pollables here are lazily initialized on-demand.
34+
typedef struct {
35+
#ifdef __wasip2__
36+
streams_own_input_stream_t input;
37+
poll_own_pollable_t input_pollable;
38+
streams_own_output_stream_t output;
39+
poll_own_pollable_t output_pollable;
40+
#else
41+
int dummy; // TODO(wasip3)
42+
#endif
43+
} tcp_socket_state_connected_t;
44+
45+
typedef struct {
46+
sockets_error_code_t error_code;
47+
} tcp_socket_state_connect_failed_t;
48+
49+
// This is a tagged union. When adding/removing/renaming cases, be sure to keep
50+
// the tag and union definitions in sync.
51+
typedef struct {
52+
enum {
53+
TCP_SOCKET_STATE_UNBOUND,
54+
TCP_SOCKET_STATE_BOUND,
55+
TCP_SOCKET_STATE_CONNECTING,
56+
TCP_SOCKET_STATE_CONNECTED,
57+
TCP_SOCKET_STATE_CONNECT_FAILED,
58+
TCP_SOCKET_STATE_LISTENING,
59+
} tag;
60+
union {
61+
tcp_socket_state_unbound_t unbound;
62+
tcp_socket_state_bound_t bound;
63+
tcp_socket_state_connecting_t connecting;
64+
tcp_socket_state_connected_t connected;
65+
tcp_socket_state_connect_failed_t connect_failed;
66+
tcp_socket_state_listening_t listening;
67+
};
68+
} tcp_socket_state_t;
69+
70+
typedef struct {
71+
sockets_own_tcp_socket_t socket;
72+
tcp_socket_state_t state;
73+
#ifdef __wasip2__
74+
// This pollable is lazily initialized on-demand.
75+
poll_own_pollable_t socket_pollable;
76+
#endif
77+
bool blocking;
78+
bool fake_nodelay;
79+
bool fake_reuseaddr;
80+
sockets_ip_address_family_t family;
81+
monotonic_clock_duration_t send_timeout;
82+
monotonic_clock_duration_t recv_timeout;
83+
} tcp_socket_t;
84+
85+
int __wasilibc_tcp_getsockopt(void *data, int level, int optname,
86+
void *restrict optval, socklen_t *restrict optlen);
87+
int __wasilibc_tcp_setsockopt(void *data, int level, int optname,
88+
const void *optval, socklen_t optlen);
989

1090
/// Adds the provided TCP socket to the descriptor table, returning the
1191
/// corresponding file descriptor.

0 commit comments

Comments
 (0)