Skip to content

Commit

Permalink
Add g_sck_set_reuseaddr()
Browse files Browse the repository at this point in the history
Only set SO_REUSEADDR where it is actually required, which is
before most (but not all) bind() calls.
  • Loading branch information
matt335672 committed Jan 10, 2025
1 parent d98c883 commit 5b37e7e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 57 deletions.
77 changes: 20 additions & 57 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,6 @@ int
g_tcp_socket(void)
{
int rv;
int option_value;
socklen_t option_len;

#if defined(XRDP_ENABLE_IPV6)
rv = (int)socket(AF_INET6, SOCK_STREAM, 0);
Expand Down Expand Up @@ -398,7 +396,8 @@ g_tcp_socket(void)
return -1;
}
#if defined(XRDP_ENABLE_IPV6)
option_len = sizeof(option_value);
int option_value;
socklen_t option_len = sizeof(option_value);
if (getsockopt(rv, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&option_value,
&option_len) == 0)
{
Expand All @@ -418,22 +417,6 @@ g_tcp_socket(void)
}
}
#endif
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
&option_len) == 0)
{
if (option_value == 0)
{
option_value = 1;
option_len = sizeof(option_value);
if (setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
option_len) < 0)
{
LOG(LOG_LEVEL_ERROR, "g_tcp_socket: setsockopt() failed");
}
}
}

return rv;
}

Expand Down Expand Up @@ -511,6 +494,23 @@ g_sck_get_recv_buffer_bytes(int sck, int *bytes)
return 0;
}

/*****************************************************************************/
int
g_sck_set_reuseaddr(int sck)
{
int rv;
int option_value = 1;
socklen_t option_len = sizeof(option_value);

rv = setsockopt(sck, SOL_SOCKET, SO_REUSEADDR,
(char *) &option_value, option_len);
if (rv < 0)
{
LOG(LOG_LEVEL_ERROR, "g_sck_set_reuseaddr: %s", g_get_strerror());
}
return rv;
}

/*****************************************************************************/
int
g_sck_local_socket(void)
Expand Down Expand Up @@ -4033,30 +4033,7 @@ g_tcp4_socket(void)
#if defined(XRDP_ENABLE_IPV6ONLY)
return -1;
#else
int rv;
int option_value;
socklen_t option_len;

rv = socket(AF_INET, SOCK_STREAM, 0);
if (rv < 0)
{
return -1;
}
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR,
(char *) &option_value, &option_len) == 0)
{
if (option_value == 0)
{
option_value = 1;
option_len = sizeof(option_value);
if (setsockopt(rv, SOL_SOCKET, SO_REUSEADDR,
(char *) &option_value, option_len) < 0)
{
}
}
}
return rv;
return socket(AF_INET, SOCK_STREAM, 0);
#endif
}

Expand Down Expand Up @@ -4119,20 +4096,6 @@ g_tcp6_socket(void)
}
}
}
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR,
(char *) &option_value, &option_len) == 0)
{
if (option_value == 0)
{
option_value = 1;
option_len = sizeof(option_value);
if (setsockopt(rv, SOL_SOCKET, SO_REUSEADDR,
(char *) &option_value, option_len) < 0)
{
}
}
}
return rv;
#else
return -1;
Expand Down
8 changes: 8 additions & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ int g_sck_set_send_buffer_bytes(int sck, int bytes);
int g_sck_get_send_buffer_bytes(int sck, int *bytes);
int g_sck_set_recv_buffer_bytes(int sck, int bytes);
int g_sck_get_recv_buffer_bytes(int sck, int *bytes);
/**
* Set SO_REUSEADDR for a socket
*
* Use before binding, if appropriate.
* @param sck Socket
* @return 0 for success
*/
int g_sck_set_reuseaddr(int sck);
int g_sck_local_socket(void);
int g_sck_local_socketpair(int sck[2]);
int g_sck_vsock_socket(void);
Expand Down
3 changes: 3 additions & 0 deletions common/trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ trans_listen_address(struct trans *self, const char *port, const char *address)

g_file_set_cloexec(self->sck, 1);
g_tcp_set_non_blocking(self->sck);
g_sck_set_reuseaddr(self->sck);

if (g_tcp_bind_address(self->sck, port, address) == 0)
{
Expand Down Expand Up @@ -954,6 +955,7 @@ trans_listen_address(struct trans *self, const char *port, const char *address)
}
g_file_set_cloexec(self->sck, 1);
g_tcp_set_non_blocking(self->sck);
g_sck_set_reuseaddr(self->sck);
if (g_tcp4_bind_address(self->sck, port, address) == 0)
{
if (g_tcp_listen(self->sck) == 0)
Expand All @@ -973,6 +975,7 @@ trans_listen_address(struct trans *self, const char *port, const char *address)
}
g_file_set_cloexec(self->sck, 1);
g_tcp_set_non_blocking(self->sck);
g_sck_set_reuseaddr(self->sck);
if (g_tcp6_bind_address(self->sck, port, address) == 0)
{
if (g_tcp_listen(self->sck) == 0)
Expand Down
1 change: 1 addition & 0 deletions tools/devel/tcp_proxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
/* create the listening socket and setup options */
lis_sck = g_tcp_socket();
g_tcp_set_non_blocking(lis_sck);
g_sck_set_reuseaddr(lis_sck);
error = g_tcp_bind(lis_sck, local_port);

if (error != 0)
Expand Down

0 comments on commit 5b37e7e

Please sign in to comment.