Class TransportImpl

    • Field Detail

      • log

        private final org.slf4j.Logger log
      • nullService

        private final Service nullService
      • config

        private final Config config
      • reader

        private final Reader reader
      • encoder

        private final Encoder encoder
      • decoder

        private final Decoder decoder
      • clientID

        private final java.lang.String clientID
        Client version identification string
      • timeoutMs

        private volatile int timeoutMs
      • authed

        private volatile boolean authed
      • service

        private volatile Service service
        Currently active service e.g. UserAuthService, ConnectionService
      • nextService

        private volatile Service nextService
        The next service that will be activated, only set when sending an SSH_MSG_SERVICE_REQUEST
      • serverID

        private java.lang.String serverID
        Server version identification string
      • msg

        private Message msg
        Message identifier of last packet received
      • writeLock

        private final java.util.concurrent.locks.ReentrantLock writeLock
    • Constructor Detail

      • TransportImpl

        public TransportImpl​(Config config)
    • Method Detail

      • init

        public void init​(java.lang.String remoteHost,
                         int remotePort,
                         java.io.InputStream in,
                         java.io.OutputStream out)
                  throws TransportException
        Description copied from interface: Transport
        Sets the host information and the streams to be used by this transport. Identification information is exchanged with the server. A TransportException is thrown in case of SSH protocol version incompatibility.
        Specified by:
        init in interface Transport
        Parameters:
        remoteHost - server's hostname
        remotePort - server's port
        in - input stream for the connection
        out - output stream for the connection
        Throws:
        TransportException - if there is an error during exchange of identification information
      • getRemoteSocketAddress

        public java.net.InetSocketAddress getRemoteSocketAddress()
        Get Remote Socket Address using Connection Information
        Specified by:
        getRemoteSocketAddress in interface RemoteAddressProvider
        Returns:
        Remote Socket Address or null when not connected
      • receiveServerIdent

        private void receiveServerIdent()
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • sendClientIdent

        private void sendClientIdent()
                              throws java.io.IOException
        Receive the server identification string.
        Throws:
        java.io.IOException - If there was an error writing to the outputstream.
      • readIdentification

        private java.lang.String readIdentification​(Buffer.PlainBuffer buffer)
                                             throws java.io.IOException
        Reads the identification string from the SSH server. This is the very first string that is sent upon connection by the server. It takes the form of, e.g. "SSH-2.0-OpenSSH_ver".

        Several concerns are taken care of here, e.g. verifying protocol version, correct line endings as specified in RFC and such.

        This is not efficient but is only done once.

        Parameters:
        buffer - The buffer to read from.
        Returns:
        empty string if full ident string has not yet been received
        Throws:
        java.io.IOException - Thrown when protocol version is not supported
      • addAlgorithmsVerifier

        public void addAlgorithmsVerifier​(AlgorithmsVerifier verifier)
        Description copied from interface: Transport
        Adds the specified verifier.
        Specified by:
        addAlgorithmsVerifier in interface Transport
        Parameters:
        verifier - The verifier to call with negotiated algorithms
      • doKex

        public void doKex()
                   throws TransportException
        Description copied from interface: Transport
        Do key exchange and algorithm negotiation. This can be the initial one or for algorithm renegotiation.
        Specified by:
        doKex in interface Transport
        Throws:
        TransportException - if there was an error during key exchange
      • isKexDone

        public boolean isKexDone()
      • getTimeoutMs

        public int getTimeoutMs()
        Specified by:
        getTimeoutMs in interface Transport
        Returns:
        the timeout that is currently set for blocking operations.
      • setTimeoutMs

        public void setTimeoutMs​(int timeoutMs)
        Description copied from interface: Transport
        Set a timeout for methods that may block.
        Specified by:
        setTimeoutMs in interface Transport
        Parameters:
        timeoutMs - the timeout in milliseconds
      • getRemoteHost

        public java.lang.String getRemoteHost()
        Specified by:
        getRemoteHost in interface Transport
        Returns:
        the hostname to which this transport is connected.
      • getRemotePort

        public int getRemotePort()
        Specified by:
        getRemotePort in interface Transport
        Returns:
        the port number on the remote host to which this transport is connected.
      • getClientVersion

        public java.lang.String getClientVersion()
        Specified by:
        getClientVersion in interface Transport
        Returns:
        the version string used by this client to identify itself to an SSH server, e.g. "SSHJ_3_0"
      • getSessionID

        public byte[] getSessionID()
        Specified by:
        getSessionID in interface Transport
        Returns:
        the session identifier assigned by server
      • sendServiceRequest

        private void sendServiceRequest​(java.lang.String serviceName)
                                 throws TransportException
        Sends a service request for the specified service
        Parameters:
        serviceName - name of the service being requested
        Throws:
        TransportException - if there is an error while sending the request
      • setAuthenticated

        public void setAuthenticated()
        Description copied from interface: Transport
        Informs this transport that authentication has been completed. This method must be called after successful authentication, so that delayed compression may become effective if applicable.
        Specified by:
        setAuthenticated in interface Transport
      • isAuthenticated

        public boolean isAuthenticated()
        Specified by:
        isAuthenticated in interface Transport
        Returns:
        whether the transport thinks it is authenticated.
      • sendUnimplemented

        public long sendUnimplemented()
                               throws TransportException
        Description copied from interface: Transport
        Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.
        Specified by:
        sendUnimplemented in interface Transport
        Returns:
        the sequence number of the packet sent
        Throws:
        TransportException - if an error occurred sending the packet
      • join

        public void join​(int timeout,
                         java.util.concurrent.TimeUnit unit)
                  throws TransportException
        Description copied from interface: Transport
        Joins the thread calling this method to the transport's death.
        Specified by:
        join in interface Transport
        Throws:
        TransportException - if the transport dies of an exception
      • disconnect

        public void disconnect​(DisconnectReason reason)
        Description copied from interface: Transport
        Send a disconnect packet with the given reason, and closes this transport.
        Specified by:
        disconnect in interface Transport
        Parameters:
        reason - reason for disconnecting
      • disconnect

        public void disconnect​(DisconnectReason reason,
                               java.lang.String message)
        Description copied from interface: Transport
        Send a disconnect packet with the given reason and message, and closes this transport.
        Specified by:
        disconnect in interface Transport
        Parameters:
        reason - the reason code for this disconnect
        message - the text message
      • setDisconnectListener

        public void setDisconnectListener​(DisconnectListener listener)
        Description copied from interface: Transport
        Specify a listener that will be notified upon disconnection.
        Specified by:
        setDisconnectListener in interface Transport
        Parameters:
        listener - Disconnect Listener to be configured
      • write

        public long write​(SSHPacket payload)
                   throws TransportException
        Description copied from interface: Transport
        Write a packet over this transport.

        The payload SSHPacket should have 5 bytes free at the beginning to avoid a performance penalty associated with making space for header bytes (packet length, padding length).

        Specified by:
        write in interface Transport
        Parameters:
        payload - the SSHPacket containing data to send
        Returns:
        sequence number of the sent packet
        Throws:
        TransportException - if an error occurred sending the packet
      • sendDisconnect

        private void sendDisconnect​(DisconnectReason reason,
                                    java.lang.String message)
      • isKexerPacket

        private static boolean isKexerPacket​(Message msg)
      • gotUnimplemented

        private void gotUnimplemented​(SSHPacket packet)
                               throws SSHException
        Got an SSH_MSG_UNIMPLEMENTED, so lets see where we're at and act accordingly.
        Parameters:
        packet - The 'unimplemented' packet received
        Throws:
        TransportException - Thrown when key exchange is ongoing
        SSHException
      • finishOff

        private void finishOff()
      • die

        public void die​(java.lang.Exception ex)
        Description copied from interface: Transport
        Kill the transport in an exceptional way.
        Specified by:
        die in interface Transport
        Parameters:
        ex - The exception that occurred.
      • getClientID

        java.lang.String getClientID()
      • getServerID

        java.lang.String getServerID()
      • getWriteLock

        java.util.concurrent.locks.ReentrantLock getWriteLock()
      • setHostKeyAlgorithm

        public void setHostKeyAlgorithm​(KeyAlgorithm keyAlgorithm)