Skip to content

Commit 4c8317c

Browse files
committed
Ensure the CS listener uses the SO_REUSEADDR socket option (#8181)
1 parent 06f2a6d commit 4c8317c

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
@@ -1089,7 +1089,7 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
10891089
* binds the socket and calls listen().
10901090
* For multi-client server (SuperServer or SuperClassic) return listener
10911091
* port.
1092-
* For classic server - accept incoming connections and fork worker
1092+
* For Classic server - accept incoming connections and fork worker
10931093
* processes, return NULL at exit;
10941094
* On error throw exception.
10951095
*
@@ -1103,27 +1103,28 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
11031103
if (n == -1)
11041104
gds__log("setsockopt: error setting IPV6_V6ONLY to %d", ipv6_v6only);
11051105

1106+
#ifndef WIN_NT
1107+
// dimitr: on Windows, lack of SO_REUSEADDR works the same way as it was specified on POSIX,
1108+
// i.e. it allows binding to a port in a TIME_WAIT/FIN_WAIT state. If this option
1109+
// is turned on explicitly, then a port can be re-bound regardless of its state,
1110+
// e.g. while it's listening. This is surely not what we want.
1111+
// We set this options for any kind of listener, including standalone Classic.
1112+
1113+
int optval = TRUE;
1114+
n = setsockopt(port->port_handle, SOL_SOCKET, SO_REUSEADDR,
1115+
(SCHAR*) &optval, sizeof(optval));
1116+
if (n == -1)
1117+
{
1118+
inet_error(true, port, "setsockopt REUSE", isc_net_connect_listen_err, INET_ERRNO);
1119+
}
1120+
#endif
1121+
11061122
if (flag & SRVR_multi_client)
11071123
{
11081124
struct linger lingerInfo;
11091125
lingerInfo.l_onoff = 0;
11101126
lingerInfo.l_linger = 0;
11111127

1112-
#ifndef WIN_NT
1113-
// dimitr: on Windows, lack of SO_REUSEADDR works the same way as it was specified on POSIX,
1114-
// i.e. it allows binding to a port in a TIME_WAIT/FIN_WAIT state. If this option
1115-
// is turned on explicitly, then a port can be re-bound regardless of its state,
1116-
// e.g. while it's listening. This is surely not what we want.
1117-
1118-
int optval = TRUE;
1119-
n = setsockopt(port->port_handle, SOL_SOCKET, SO_REUSEADDR,
1120-
(SCHAR*) &optval, sizeof(optval));
1121-
if (n == -1)
1122-
{
1123-
inet_error(true, port, "setsockopt REUSE", isc_net_connect_listen_err, INET_ERRNO);
1124-
}
1125-
#endif
1126-
11271128
// Get any values for SO_LINGER so that they can be reset during
11281129
// disconnect. SO_LINGER should be set by default on the socket
11291130

0 commit comments

Comments
 (0)