Add config option for bind ip for outgoing connections #638

Closed
opened 2023-09-26 16:54:12 +00:00 by yuka · 2 comments
Contributor
No description provided.
Owner

Could you write a more exhaustive documentation for this feature request? I can't keep open a feature request if I don't know what it is about.

Could you write a more exhaustive documentation for this feature request? I can't keep open a feature request if I don't know what it is about.
Author
Contributor

consider the following patch applied to the netapp dependency:

diff --git a/src/netapp.rs b/src/netapp.rs
index b1ad9db..cdd81ba 100644
--- a/src/netapp.rs
+++ b/src/netapp.rs
@@ -13,7 +13,7 @@ use sodiumoxide::crypto::sign::ed25519;
 
 use futures::stream::futures_unordered::FuturesUnordered;
 use futures::stream::StreamExt;
-use tokio::net::{TcpListener, TcpStream};
+use tokio::net::{TcpListener, TcpStream, TcpSocket};
 use tokio::select;
 use tokio::sync::{mpsc, watch};
 
@@ -298,9 +298,19 @@ impl NetApp {
            return Ok(());
        }
 
-       let socket = TcpStream::connect(ip).await?;
+       let stream = if let Some(listen_addr) = self.listen_params.load().as_ref().map(|par| par.listen_addr) {
+           let socket = if listen_addr.is_ipv4() {
+               TcpSocket::new_v4()
+           } else {
+               TcpSocket::new_v6()
+           }?;
+           socket.bind(SocketAddr::new(listen_addr.ip(), 0))?;
+           socket.connect(ip).await?
+       } else {
+           TcpStream::connect(ip).await?
+       };
        info!("Connected to {}, negotiating handshake...", ip);
-       ClientConn::init(self, socket, id).await?;
+       ClientConn::init(self, stream, id).await?;
        Ok(())
    }
 

In this case I bind a TcpSocket to the listen_addr before making outgoing connections.

The use case is a node that has multiple IP addresses, but only one is allowed/able to reach the other nodes because of firewall/routing reasons.

consider the following patch applied to the netapp dependency: ```diff diff --git a/src/netapp.rs b/src/netapp.rs index b1ad9db..cdd81ba 100644 --- a/src/netapp.rs +++ b/src/netapp.rs @@ -13,7 +13,7 @@ use sodiumoxide::crypto::sign::ed25519; use futures::stream::futures_unordered::FuturesUnordered; use futures::stream::StreamExt; -use tokio::net::{TcpListener, TcpStream}; +use tokio::net::{TcpListener, TcpStream, TcpSocket}; use tokio::select; use tokio::sync::{mpsc, watch}; @@ -298,9 +298,19 @@ impl NetApp { return Ok(()); } - let socket = TcpStream::connect(ip).await?; + let stream = if let Some(listen_addr) = self.listen_params.load().as_ref().map(|par| par.listen_addr) { + let socket = if listen_addr.is_ipv4() { + TcpSocket::new_v4() + } else { + TcpSocket::new_v6() + }?; + socket.bind(SocketAddr::new(listen_addr.ip(), 0))?; + socket.connect(ip).await? + } else { + TcpStream::connect(ip).await? + }; info!("Connected to {}, negotiating handshake...", ip); - ClientConn::init(self, socket, id).await?; + ClientConn::init(self, stream, id).await?; Ok(()) } ``` In this case I bind a TcpSocket to the listen_addr before making outgoing connections. The use case is a node that has multiple IP addresses, but only one is allowed/able to reach the other nodes because of firewall/routing reasons.
lx added the
Improvement
label 2024-02-16 10:15:10 +00:00
lx added this to the v1.0 milestone 2024-02-16 10:15:14 +00:00
lx closed this issue 2024-02-19 11:44:07 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Deuxfleurs/garage#638
No description provided.