diff --git a/expected/wasm32-wasi-preview2/predefined-macros.txt b/expected/wasm32-wasi-preview2/predefined-macros.txt index 4396d6345..4bef0e357 100644 --- a/expected/wasm32-wasi-preview2/predefined-macros.txt +++ b/expected/wasm32-wasi-preview2/predefined-macros.txt @@ -1660,6 +1660,7 @@ #define SO_PROTOCOL 38 #define SO_RCVBUF 8 #define SO_RCVTIMEO 66 +#define SO_REUSEADDR 2 #define SO_SNDBUF 7 #define SO_SNDTIMEO 67 #define SO_TYPE 3 diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/socket/sockopt.c b/libc-bottom-half/cloudlibc/src/libc/sys/socket/sockopt.c index df6187572..8a8cd19b4 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/socket/sockopt.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/socket/sockopt.c @@ -93,6 +93,10 @@ int tcp_getsockopt(tcp_socket_t* socket, int level, int optname, value = result; break; } + case SO_REUSEADDR: { + value = socket->fake_reuseaddr; + break; + } case SO_RCVTIMEO: // TODO wasi-sockets: emulate in wasi-libc itself case SO_SNDTIMEO: // TODO wasi-sockets: emulate in wasi-libc itself default: @@ -275,6 +279,14 @@ int tcp_setsockopt(tcp_socket_t* socket, int level, int optname, const void* opt return 0; } + case SO_REUSEADDR: { + // As of this writing, WASI has no support for changing SO_REUSEADDR + // -- it's enabled by default and cannot be disabled. To keep + // applications happy, we pretend to support enabling and disabling + // it. + socket->fake_reuseaddr = (intval != 0); + return 0; + } case SO_RCVTIMEO: // TODO wasi-sockets: emulate in wasi-libc itself case SO_SNDTIMEO: // TODO wasi-sockets: emulate in wasi-libc itself default: diff --git a/libc-bottom-half/headers/private/descriptor_table.h b/libc-bottom-half/headers/private/descriptor_table.h index 63381b3fd..fdcaa64d6 100644 --- a/libc-bottom-half/headers/private/descriptor_table.h +++ b/libc-bottom-half/headers/private/descriptor_table.h @@ -44,6 +44,7 @@ typedef struct { poll_own_pollable_t socket_pollable; bool blocking; bool fake_nodelay; + bool fake_reuseaddr; network_ip_address_family_t family; tcp_socket_state_t state; } tcp_socket_t; diff --git a/libc-bottom-half/headers/public/__header_sys_socket.h b/libc-bottom-half/headers/public/__header_sys_socket.h index f39577468..635a14246 100644 --- a/libc-bottom-half/headers/public/__header_sys_socket.h +++ b/libc-bottom-half/headers/public/__header_sys_socket.h @@ -22,6 +22,7 @@ #define SOMAXCONN 128 +#define SO_REUSEADDR 2 #define SO_ERROR 4 #define SO_SNDBUF 7 #define SO_RCVBUF 8