diff --git a/src/runtime2/stdlib/internet.rs b/src/runtime2/stdlib/internet.rs index 4744cc7835343d0c4018326214dda8eb824e99aa..1a9078d8bbfc018528a64ba05160e45640270ba2 100644 --- a/src/runtime2/stdlib/internet.rs +++ b/src/runtime2/stdlib/internet.rs @@ -119,6 +119,10 @@ impl SocketTcpListener { unsafe{ libc::close(socket_handle); } return Err(SocketError::Modifying); } + if !set_socket_reuse_address(socket_handle) { + unsafe{ libc::close(socket_handle); } + return Err(SocketError::Modifying); + } // Listen unsafe { @@ -388,6 +392,35 @@ fn set_socket_blocking(handle: libc::c_int, blocking: bool) -> bool { return true; } +#[inline] +fn set_socket_reuse_address(handle: libc::c_int) -> bool { + if handle < 0 { + return false; + } + + unsafe { + let enable: libc::c_int = 1; + let enable_ptr: *const _ = &enable; + let result = libc::setsockopt( + handle, libc::SOL_SOCKET, libc::SO_REUSEADDR, + enable_ptr.cast(), size_of::() as libc::socklen_t + ); + if result < 0 { + return false; + } + + let result = libc::setsockopt( + handle, libc::SOL_SOCKET, libc::SO_REUSEPORT, + enable_ptr.cast(), size_of::() as libc::socklen_t + ); + if result < 0 { + return false; + } + } + + return true; +} + #[inline] fn socket_family_from_ip(ip: IpAddr) -> libc::c_int { return match ip {