Skip to content

Commit 5cd6497

Browse files
committed
Ensure the CS listener uses the SO_REUSEADDR socket option (#8181)
1 parent c7d5566 commit 5cd6497

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/remote/inet.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
10731073
* binds the socket and calls listen().
10741074
* For multi-client server (SuperServer or SuperClassic) return listener
10751075
* port.
1076-
* For classic server - accept incoming connections and fork worker
1076+
* For Classic server - accept incoming connections and fork worker
10771077
* processes, return NULL at exit;
10781078
* On error throw exception.
10791079
*
@@ -1087,27 +1087,28 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
10871087
if (n == -1)
10881088
gds__log("setsockopt: error setting IPV6_V6ONLY to %d", ipv6_v6only);
10891089

1090+
#ifndef WIN_NT
1091+
// dimitr: on Windows, lack of SO_REUSEADDR works the same way as it was specified on POSIX,
1092+
// i.e. it allows binding to a port in a TIME_WAIT/FIN_WAIT state. If this option
1093+
// is turned on explicitly, then a port can be re-bound regardless of its state,
1094+
// e.g. while it's listening. This is surely not what we want.
1095+
// We set this options for any kind of listener, including standalone Classic.
1096+
1097+
int optval = TRUE;
1098+
n = setsockopt(port->port_handle, SOL_SOCKET, SO_REUSEADDR,
1099+
(SCHAR*) &optval, sizeof(optval));
1100+
if (n == -1)
1101+
{
1102+
inet_error(true, port, "setsockopt REUSE", isc_net_connect_listen_err, INET_ERRNO);
1103+
}
1104+
#endif
1105+
10901106
if (flag & SRVR_multi_client)
10911107
{
10921108
struct linger lingerInfo;
10931109
lingerInfo.l_onoff = 0;
10941110
lingerInfo.l_linger = 0;
10951111

1096-
#ifndef WIN_NT
1097-
// dimitr: on Windows, lack of SO_REUSEADDR works the same way as it was specified on POSIX,
1098-
// i.e. it allows binding to a port in a TIME_WAIT/FIN_WAIT state. If this option
1099-
// is turned on explicitly, then a port can be re-bound regardless of its state,
1100-
// e.g. while it's listening. This is surely not what we want.
1101-
1102-
int optval = TRUE;
1103-
n = setsockopt(port->port_handle, SOL_SOCKET, SO_REUSEADDR,
1104-
(SCHAR*) &optval, sizeof(optval));
1105-
if (n == -1)
1106-
{
1107-
inet_error(true, port, "setsockopt REUSE", isc_net_connect_listen_err, INET_ERRNO);
1108-
}
1109-
#endif
1110-
11111112
// Get any values for SO_LINGER so that they can be reset during
11121113
// disconnect. SO_LINGER should be set by default on the socket
11131114

0 commit comments

Comments
 (0)