Skip to content

Commit 5a7b21f

Browse files
authored
Rollup merge of rust-lang#71785 - reitermarkus:cfg-attribute, r=Mark-Simulacrum
Update comment regarding SO_REUSEADDR on Windows
2 parents 94d2fd9 + 39a9790 commit 5a7b21f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/libstd/sys/windows/c.rs

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ pub const SOCK_STREAM: c_int = 1;
216216
pub const SOL_SOCKET: c_int = 0xffff;
217217
pub const SO_RCVTIMEO: c_int = 0x1006;
218218
pub const SO_SNDTIMEO: c_int = 0x1005;
219-
pub const SO_REUSEADDR: c_int = 0x0004;
220219
pub const IPPROTO_IP: c_int = 0;
221220
pub const IPPROTO_TCP: c_int = 6;
222221
pub const IPPROTO_IPV6: c_int = 41;

src/libstd/sys_common/net.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,15 @@ impl TcpListener {
368368

369369
let sock = Socket::new(addr, c::SOCK_STREAM)?;
370370

371-
// On platforms with Berkeley-derived sockets, this allows
372-
// to quickly rebind a socket, without needing to wait for
373-
// the OS to clean up the previous one.
374-
if !cfg!(windows) {
375-
setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
376-
}
371+
// On platforms with Berkeley-derived sockets, this allows to quickly
372+
// rebind a socket, without needing to wait for the OS to clean up the
373+
// previous one.
374+
//
375+
// On Windows, this allows rebinding sockets which are actively in use,
376+
// which allows “socket hijacking”, so we explicitly don't set it here.
377+
// https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
378+
#[cfg(not(windows))]
379+
setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
377380

378381
// Bind our new socket
379382
let (addrp, len) = addr.into_inner();

0 commit comments

Comments
 (0)