Skip to content

Commit

Permalink
add possibility to bind the tcp socket to a given socket address befo…
Browse files Browse the repository at this point in the history
…re connecting to a target
  • Loading branch information
Tobias Funke committed Dec 11, 2024
1 parent fb61618 commit d0745ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/sip-core/src/transport/native_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl StreamingFactory for TlsConnector {
async fn connect<A: ToSocketAddrs + Send>(
&self,
uri_info: &UriInfo,
addr: A,
addr: SocketAddr,
) -> io::Result<Self::Transport> {
// Best effort to guess the domain. If the `Host` a valid domain this will work,
// but sometimes it might be an IP address or invalid domain. In that case this might succeed anyway
Expand Down
2 changes: 1 addition & 1 deletion crates/sip-core/src/transport/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl StreamingFactory for TlsConnector {
async fn connect<A: ToSocketAddrs + Send>(
&self,
uri_info: &UriInfo,
addr: A,
addr: SocketAddr,
) -> io::Result<Self::Transport> {
let server_name = match uri_info.host_port.host {
Host::Name(ref name) => ServerName::try_from(name.as_str())
Expand Down
4 changes: 2 additions & 2 deletions crates/sip-core/src/transport/streaming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait StreamingFactory: Send + Sync + 'static {
async fn connect<A: ToSocketAddrs + Send>(
&self,
uri_info: &UriInfo,
addr: A,
addr: SocketAddr,
) -> io::Result<Self::Transport>;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ where
) -> io::Result<TpHandle> {
log::trace!("{} trying to connect to {}", self.name(), addr);

let stream = self.connect(uri_info, addr).await?;
let stream = self.connect::<SocketAddr>(uri_info, addr).await?;
let local = stream.local_addr()?;
let remote = stream.peer_addr()?;

Expand Down
24 changes: 21 additions & 3 deletions crates/sip-core/src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ use super::streaming::{
use sip_types::uri::UriInfo;
use std::io;
use std::net::SocketAddr;
use tokio::net::{TcpListener as TokioTcpListener, TcpStream, ToSocketAddrs};
use tokio::net::{TcpListener as TokioTcpListener, TcpStream, TcpSocket, ToSocketAddrs};

// ==== Connector

#[derive(Default)]
pub struct TcpConnector {
_priv: (),
bind_addr: Option<SocketAddr>,
}

impl TcpConnector {
pub fn new() -> Self {
Self::default()
}

pub fn new_with_bind(bind_addr: SocketAddr) -> Self {
Self {
bind_addr: Some(bind_addr),
..Self::default()
}
}
}

#[async_trait::async_trait]
Expand All @@ -26,9 +34,19 @@ impl StreamingFactory for TcpConnector {
async fn connect<A: ToSocketAddrs + Send>(
&self,
_: &UriInfo,
addr: A,
addr: SocketAddr,
) -> io::Result<Self::Transport> {
TcpStream::connect(addr).await
if let Some(bind_addr) = self.bind_addr {
let socket = match bind_addr {
SocketAddr::V4(_) => TcpSocket::new_v4()?,
SocketAddr::V6(_) => TcpSocket::new_v6()?,
};
socket.bind(bind_addr)?;
let stream = socket.connect(addr).await?;
Ok(stream)
} else {
TcpStream::connect(addr).await
}
}
}

Expand Down

0 comments on commit d0745ae

Please sign in to comment.