rust-lang / libs-team

The home of the library team
Apache License 2.0
115 stars 18 forks source link

Add connect method to TcpListener #265

Closed valaphee closed 12 months ago

valaphee commented 12 months ago

Proposal

Problem statement

When having multiple ip addresses assigned, it is not possible to use a specific one when establishing a TCP connection.

Motivating examples or use cases

Solution sketch

let udp_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
udp_socket.connect("172.16.0.100:500").unwrap();

let tcp_listener = TcpListener::bind("0.0.0.0:0").unwrap();
let tcp_socket = tcp_listener.connect("172.16.0.100:500").unwrap();

Adding a connect method to TcpListener would allow for the same behavior as with UdpSocket, to first bind to an os assigned ip/port and then connect to the peer. (This is also how it would work with Unix sockets)

Compared to UdpSocket, connect on TcpListener can be considered final and therefore consuming is valid, in UdpSocket connect is just a convience method.

Alternatives

An alternative could be to introduce a new method to TcpSocket (called bind_connect) but at end would boil down to the same concept, just less intuitive and behavior duplication.

Links and related work

https://internals.rust-lang.org/t/establishing-a-tcp-connection-using-a-specific-nic-ip-address/19509

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution:

valaphee commented 12 months ago

I'll close this issue after taking https://github.com/rust-lang/rust/issues/68053 into consideration, and actually implementing it, as it is definitely clearer to use TcpStream::connect_from, and seeing that TcpListener is already in the step of listening after creation (I forgot about this fact)