Skip to content

Commit

Permalink
add fake SO_REUSEADDR support
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Dec 20, 2023
1 parent 9c4d23a commit 7c28169
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions expected/wasm32-wasi-preview2/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions libc-bottom-half/cloudlibc/src/libc/sys/socket/sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions libc-bottom-half/headers/private/descriptor_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions libc-bottom-half/headers/public/__header_sys_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#define SOMAXCONN 128

#define SO_REUSEADDR 2
#define SO_ERROR 4
#define SO_SNDBUF 7
#define SO_RCVBUF 8
Expand Down

0 comments on commit 7c28169

Please sign in to comment.