File tree Expand file tree Collapse file tree 2 files changed +9
-7
lines changed Expand file tree Collapse file tree 2 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -216,7 +216,6 @@ pub const SOCK_STREAM: c_int = 1;
216216pub const SOL_SOCKET : c_int = 0xffff ;
217217pub const SO_RCVTIMEO : c_int = 0x1006 ;
218218pub const SO_SNDTIMEO : c_int = 0x1005 ;
219- pub const SO_REUSEADDR : c_int = 0x0004 ;
220219pub const IPPROTO_IP : c_int = 0 ;
221220pub const IPPROTO_TCP : c_int = 6 ;
222221pub const IPPROTO_IPV6 : c_int = 41 ;
Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments