From 81ccd9d88ef1d15cf611d663d96f21dcad4fd7f7 Mon Sep 17 00:00:00 2001 From: Rodrigo Antonio de Araujo Date: Tue, 10 Dec 2024 12:12:36 -0300 Subject: [PATCH 1/3] Forward reuse address and port in udp protocol at lib virtual net --- lib/virtual-net/src/host.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/virtual-net/src/host.rs b/lib/virtual-net/src/host.rs index 16e8c9c5226..b1693983833 100644 --- a/lib/virtual-net/src/host.rs +++ b/lib/virtual-net/src/host.rs @@ -104,8 +104,10 @@ impl VirtualNetworking for LocalNetworking { &self, addr: SocketAddr, _reuse_port: bool, - _reuse_addr: bool, + reuse_addr: bool, ) -> Result> { + use socket2::{Socket, Domain, Type}; + if let Some(ruleset) = self.ruleset.as_ref() { if !ruleset.allows_socket(addr, Direction::Inbound) { tracing::warn!(%addr, "bind_udp blocked by firewall rule"); @@ -113,7 +115,13 @@ impl VirtualNetworking for LocalNetworking { } } - let socket = mio::net::UdpSocket::bind(addr).map_err(io_err_into_net_error)?; + let std_sock = Socket::new(Domain::IPV4, Type::DGRAM, None).map_err(io_err_into_net_error)?; + std_sock.set_reuse_address(reuse_addr).map_err(io_err_into_net_error)?; + //std_sock.set_reuse_port(reuse_port).map_err(io_err_into_net_error)?; + + std_sock.bind(&addr.into()).map_err(io_err_into_net_error)?; + + let socket = mio::net::UdpSocket::from_std(std_sock.into()); #[allow(unused_mut)] let mut ret = LocalUdpSocket { From ba5be6399f7a121ae76fd42acd61fa841b88be1c Mon Sep 17 00:00:00 2001 From: Rodrigo Antonio de Araujo Date: Thu, 19 Dec 2024 10:40:24 -0300 Subject: [PATCH 2/3] Add a conditional check on udp reuse address setting --- lib/virtual-net/src/host.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/virtual-net/src/host.rs b/lib/virtual-net/src/host.rs index b1693983833..5a58dbb1531 100644 --- a/lib/virtual-net/src/host.rs +++ b/lib/virtual-net/src/host.rs @@ -103,7 +103,7 @@ impl VirtualNetworking for LocalNetworking { async fn bind_udp( &self, addr: SocketAddr, - _reuse_port: bool, + reuse_port: bool, reuse_addr: bool, ) -> Result> { use socket2::{Socket, Domain, Type}; @@ -116,12 +116,15 @@ impl VirtualNetworking for LocalNetworking { } let std_sock = Socket::new(Domain::IPV4, Type::DGRAM, None).map_err(io_err_into_net_error)?; - std_sock.set_reuse_address(reuse_addr).map_err(io_err_into_net_error)?; - //std_sock.set_reuse_port(reuse_port).map_err(io_err_into_net_error)?; - - std_sock.bind(&addr.into()).map_err(io_err_into_net_error)?; - - let socket = mio::net::UdpSocket::from_std(std_sock.into()); + #[cfg(not(windows))] + let socket = { + std_sock.set_reuse_address(reuse_addr).map_err(io_err_into_net_error)?; + std_sock.set_reuse_port(reuse_port).map_err(io_err_into_net_error)?; + std_sock.bind(&addr.into()).map_err(io_err_into_net_error)?; + mio::net::UdpSocket::from_std(std_sock.into()) + }; + #[cfg(windows)] + let socket = mio::net::UdpSocket::bind(addr).map_err(io_err_into_net_error)?; #[allow(unused_mut)] let mut ret = LocalUdpSocket { From 992be1dc818f4c7193630f8b437c594fd373266f Mon Sep 17 00:00:00 2001 From: Rodrigo Antonio de Araujo Date: Thu, 19 Dec 2024 11:00:39 -0300 Subject: [PATCH 3/3] Fix lint issues --- lib/virtual-net/src/host.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/virtual-net/src/host.rs b/lib/virtual-net/src/host.rs index 5a58dbb1531..c904576c950 100644 --- a/lib/virtual-net/src/host.rs +++ b/lib/virtual-net/src/host.rs @@ -106,7 +106,7 @@ impl VirtualNetworking for LocalNetworking { reuse_port: bool, reuse_addr: bool, ) -> Result> { - use socket2::{Socket, Domain, Type}; + use socket2::{Domain, Socket, Type}; if let Some(ruleset) = self.ruleset.as_ref() { if !ruleset.allows_socket(addr, Direction::Inbound) { @@ -115,11 +115,16 @@ impl VirtualNetworking for LocalNetworking { } } - let std_sock = Socket::new(Domain::IPV4, Type::DGRAM, None).map_err(io_err_into_net_error)?; #[cfg(not(windows))] let socket = { - std_sock.set_reuse_address(reuse_addr).map_err(io_err_into_net_error)?; - std_sock.set_reuse_port(reuse_port).map_err(io_err_into_net_error)?; + let std_sock = + Socket::new(Domain::IPV4, Type::DGRAM, None).map_err(io_err_into_net_error)?; + std_sock + .set_reuse_address(reuse_addr) + .map_err(io_err_into_net_error)?; + std_sock + .set_reuse_port(reuse_port) + .map_err(io_err_into_net_error)?; std_sock.bind(&addr.into()).map_err(io_err_into_net_error)?; mio::net::UdpSocket::from_std(std_sock.into()) };