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

TcpStream bind before connect #68053

Open
zonyitoo opened this issue Jan 9, 2020 · 4 comments
Open

TcpStream bind before connect #68053

zonyitoo opened this issue Jan 9, 2020 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@zonyitoo
Copy link

zonyitoo commented Jan 9, 2020

We now have TcpStream::connect and TcpStream::connect_timeout to create a TcpStream.

But there is no option for us to do before calling connect(), in particular, bind() to the source address.

Proposal:

fn connect_with<A>(bind_addr: &SocketAddr, A: addr) -> io::Result<TcpStream>
    where A: ToSocketAddrs
{
    // ...
}

fn connect_with_timeout(bind_addr: &SocketAddr,
                        addr: &SocketAddr,
                        timeout: Duration)
                        -> io::Result<TcpStream>
{
    // ...
}
@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 9, 2020
@zonyitoo
Copy link
Author

zonyitoo commented Jan 9, 2020

@jonas-schievink I am Ok to send a PR for this.

Proposed names:

  1. connect_with
  2. connect_from
  3. connect_by
  4. bind_connect

@jonas-schievink
Copy link
Contributor

Feel free to do that. The libs team has to decide whether to take it. IMO connect_from is the clearest name.

@the8472
Copy link
Member

the8472 commented Jan 9, 2020

connect_with

It's generally a bad idea to cram socket setup into a single method since there are a bunch of configuration APIs you may want to call on an unconnected socket, bind is just one of them.

Instead it would be better to have a new(address_family) constructor which corresponds to the socket() syscall and creates an unconnected socket. Then one can bind it, set socket options/fcntls and finally connect.

Or perhaps a TcpSocket that acts as builder and can either be connected to create a TcpStream or use listen to create a TcpListener.

@zonyitoo
Copy link
Author

zonyitoo commented Jan 9, 2020

Or perhaps a TcpSocket that acts as builder and can either be connected to create a TcpStream or use listen to create a TcpListener.

Yes, TcpSocket is the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants