Skip to content

Add connect_from methods to TcpStream #115710

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ impl TcpStream {
super::each_addr(addr, net_imp::TcpStream::connect).map(TcpStream)
}

/// Opens a TCP connection to a remote host using the specified local address.
///
/// `addr` is an address of the remote host. Anything which implements
/// [`ToSocketAddrs`] trait can be supplied for the address; see this trait
/// documentation for concrete examples.
///
/// If `addr` yields multiple addresses, `connect_from` will be attempted with
/// each of the addresses until a connection is successful. If none of
/// the addresses result in a successful connection, the error returned from
/// the last connection attempt (the last address) is returned.
#[unstable(feature = "tcpstream_connect_from", issue = "115710")]
pub fn connect_from<A: ToSocketAddrs>(from: &SocketAddr, addr: A) -> io::Result<TcpStream> {
super::each_addr(addr, |addr| net_imp::TcpStream::connect_from(from, addr)).map(TcpStream)
}

/// Opens a TCP connection to a remote host with a timeout.
///
/// Unlike `connect`, `connect_timeout` takes a single [`SocketAddr`] since
Expand All @@ -173,6 +188,21 @@ impl TcpStream {
net_imp::TcpStream::connect_timeout(addr, timeout).map(TcpStream)
}

/// Opens a TCP connection to a remote host using the specified local address with a timeout.
///
/// Unlike `connect_from`, `connect_from_timeout` takes a single [`SocketAddr`] since
/// timeout must be applied to individual addresses.
///
/// It is an error to pass a zero `Duration` to this function.
#[unstable(feature = "tcpstream_connect_from", issue = "115710")]
pub fn connect_from_timeout(
from: &SocketAddr,
addr: &SocketAddr,
timeout: Duration,
) -> io::Result<TcpStream> {
net_imp::TcpStream::connect_from_timeout(from, addr, timeout).map(TcpStream)
}

/// Returns the socket address of the remote peer of this TCP connection.
///
/// # Examples
Expand Down
31 changes: 31 additions & 0 deletions library/std/src/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ impl TcpStream {
Ok(TcpStream { inner: sock })
}

pub fn connect_from(from: &SocketAddr, addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
let addr = addr?;

init();

let sock = Socket::new(addr, c::SOCK_STREAM)?;

let (from, len) = from.into_inner();
cvt(unsafe { c::bind(sock.as_raw(), from.as_ptr(), len as _) })?;

let (addr, len) = addr.into_inner();
cvt_r(|| unsafe { c::connect(sock.as_raw(), addr.as_ptr(), len) })?;
Ok(TcpStream { inner: sock })
}

pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
init();

Expand All @@ -239,6 +254,22 @@ impl TcpStream {
Ok(TcpStream { inner: sock })
}

pub fn connect_from_timeout(
from: &SocketAddr,
addr: &SocketAddr,
timeout: Duration,
) -> io::Result<TcpStream> {
init();

let sock = Socket::new(addr, c::SOCK_STREAM)?;

let (from, len) = from.into_inner();
cvt(unsafe { c::bind(sock.as_raw(), from.as_ptr(), len as _) })?;

sock.connect_timeout(addr, timeout)?;
Ok(TcpStream { inner: sock })
}

#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
Expand Down