about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-05-02 18:27:48 +0200
committerGitHub <noreply@github.com>2020-05-02 18:27:48 +0200
commit5a7b21fabaed7a5fe4b2c89baf58271f82808b4a (patch)
treed6294691775312ee4a98bc47868b993baa1f18ab
parent94d2fd920533cda2482128157bfd605cde7e3ef3 (diff)
parent39a97900bee062b020c93e995c3b48b48d335bf2 (diff)
downloadrust-5a7b21fabaed7a5fe4b2c89baf58271f82808b4a.tar.gz
rust-5a7b21fabaed7a5fe4b2c89baf58271f82808b4a.zip
Rollup merge of #71785 - reitermarkus:cfg-attribute, r=Mark-Simulacrum
Update comment regarding SO_REUSEADDR on Windows
-rw-r--r--src/libstd/sys/windows/c.rs1
-rw-r--r--src/libstd/sys_common/net.rs15
2 files changed, 9 insertions, 7 deletions
diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs
index 134f508dfab..6115d652b0c 100644
--- a/src/libstd/sys/windows/c.rs
+++ b/src/libstd/sys/windows/c.rs
@@ -216,7 +216,6 @@ pub const SOCK_STREAM: c_int = 1;
 pub const SOL_SOCKET: c_int = 0xffff;
 pub const SO_RCVTIMEO: c_int = 0x1006;
 pub const SO_SNDTIMEO: c_int = 0x1005;
-pub const SO_REUSEADDR: c_int = 0x0004;
 pub const IPPROTO_IP: c_int = 0;
 pub const IPPROTO_TCP: c_int = 6;
 pub const IPPROTO_IPV6: c_int = 41;
diff --git a/src/libstd/sys_common/net.rs b/src/libstd/sys_common/net.rs
index a9b6079de75..1c03bc92344 100644
--- a/src/libstd/sys_common/net.rs
+++ b/src/libstd/sys_common/net.rs
@@ -368,12 +368,15 @@ impl TcpListener {
 
         let sock = Socket::new(addr, c::SOCK_STREAM)?;
 
-        // On platforms with Berkeley-derived sockets, this allows
-        // to quickly rebind a socket, without needing to wait for
-        // the OS to clean up the previous one.
-        if !cfg!(windows) {
-            setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
-        }
+        // On platforms with Berkeley-derived sockets, this allows to quickly
+        // rebind a socket, without needing to wait for the OS to clean up the
+        // previous one.
+        //
+        // On Windows, this allows rebinding sockets which are actively in use,
+        // which allows “socket hijacking”, so we explicitly don't set it here.
+        // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
+        #[cfg(not(windows))]
+        setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
 
         // Bind our new socket
         let (addrp, len) = addr.into_inner();