ronf / asyncssh

AsyncSSH is a Python package which provides an asynchronous client and server implementation of the SSHv2 protocol on top of the Python asyncio framework.
Eclipse Public License 2.0
1.56k stars 157 forks source link

Connection over existing non-TCP socket fails #688

Closed cwe-skov-dk closed 2 months ago

cwe-skov-dk commented 2 months ago

I use asyncssh 2.17.0 to connect a server using an established WebRTC tunnel. To connect the tunnel and asyncssh I create a socket.socketpair() and use one end of the pair inside asyncssh.connect() as sock argument.

This ends in a failure in sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) inside connection_made() in asyncssh connection.py line 1275.

Adding a test for sock.family fixes the issue and the connection is established correctly using the socketpair socket:

        if sock:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,
                            self._tcp_keepalive)
            if sock.family in {socket.AF_INET, socket.AF_INET6}:
                sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
ronf commented 2 months ago

This change makes sense - I'll add it in as soon as I'm done with another change I'm in the middle of.

Thanks for the report!

ronf commented 2 months ago

This is now available as commit dcd1c61 in the "develop" branch.

Note that you will still want to pass in a host and port when making the asyncssh.connect() call. Otherwise, things like the known hosts lookup may fail to find the right entry for the server you are connecting to.

cwe-skov-dk commented 2 months ago

Thanks a lot!

Yes, I know about the hostname and do pass that to asyncssh.connect() in my implementation.