Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connect): network interface binding with bind_local_device #2823

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/client/connect/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct Config {
keep_alive_timeout: Option<Duration>,
local_address_ipv4: Option<Ipv4Addr>,
local_address_ipv6: Option<Ipv6Addr>,
local_device: Option<Vec<u8>>,
nodelay: bool,
reuse_address: bool,
send_buffer_size: Option<usize>,
Expand Down Expand Up @@ -117,6 +118,7 @@ impl<R> HttpConnector<R> {
keep_alive_timeout: None,
local_address_ipv4: None,
local_address_ipv6: None,
local_device: None,
nodelay: false,
reuse_address: false,
send_buffer_size: None,
Expand Down Expand Up @@ -193,6 +195,21 @@ impl<R> HttpConnector<R> {
cfg.local_address_ipv6 = Some(addr_ipv6);
}

/// Set that all sockets are bound to the configured interface
///
/// # Example
///
/// ```
/// let mut http = HttpConnector::new();
/// http.set_local_device(Some("wlan0".into()));
/// ```
#[inline]
pub fn set_local_device(&mut self, device: Option<Vec<u8>>) {
let cfg = self.config_mut();

cfg.local_device = device;
}

/// Set the connect timeout.
///
/// If a domain resolves to multiple IP addresses, the timeout will be
Expand Down Expand Up @@ -564,7 +581,11 @@ fn bind_local_address(
local_addr_ipv6: &Option<Ipv6Addr>,
) -> io::Result<()> {
match (*dst_addr, local_addr_ipv4, local_addr_ipv6) {
(SocketAddr::V4(_), Some(addr), _) => {
(SocketAddr::V4(_), Some(addr), None) => {
socket.bind(&SocketAddr::new(addr.clone().into(), 0).into())?;
}
(SocketAddr::V4(_), None, Some(addr)) => {
// Connection from IPv6 local to IPv4 remote
socket.bind(&SocketAddr::new(addr.clone().into(), 0).into())?;
}
(SocketAddr::V6(_), _, Some(addr)) => {
Expand All @@ -585,6 +606,17 @@ fn bind_local_address(
Ok(())
}

fn bind_local_device(
socket: &socket2::Socket,
device_interface: &Option<Vec<u8>>,
) -> io::Result<()> {
// None value would unbind the device
if let Some(device) = device_interface {
socket.bind_device(Some(device.as_slice()))?;
}
Ok(())
}

fn connect(
addr: &SocketAddr,
config: &Config,
Expand Down Expand Up @@ -619,7 +651,15 @@ fn connect(
&config.local_address_ipv4,
&config.local_address_ipv6,
)
.map_err(ConnectError::m("tcp bind local error"))?;
.map_err(ConnectError::m("tcp bind local IP error"))?;


bind_local_device(
&socket,
&config.local_device,
)
.map_err(ConnectError::m("tcp bind local device error"))?;


#[cfg(unix)]
let socket = unsafe {
Expand Down Expand Up @@ -934,6 +974,7 @@ mod tests {
let cfg = Config {
local_address_ipv4: None,
local_address_ipv6: None,
local_device: None,
connect_timeout: None,
keep_alive_timeout: None,
happy_eyeballs_timeout: Some(fallback_timeout),
Expand Down