public final class

TransportImpl

extends Object
implements Transport
java.lang.Object
   ↳ net.schmizz.sshj.transport.TransportImpl

Class Overview

A thread-safe Transport implementation.

Summary

Fields
private boolean authed
private final String clientID Client version identification string
private final Event<TransportException> close
private final Config config
private TransportImpl.ConnInfo connInfo
private final Decoder decoder
private DisconnectListener disconnectListener
private final Encoder encoder
private final Heartbeater heartbeater
private final KeyExchanger kexer
private final Logger log
private Message msg Message identifier of last packet received
private final DisconnectListener nullDisconnectListener
private final Service nullService
private final Reader reader
private String serverID Server version identification string
private Service service Currently active service e.g.
private final Event<TransportException> serviceAccept
private int timeout
private final ReentrantLock writeLock
Public Constructors
TransportImpl(Config config)
Public Methods
void addHostKeyVerifier(HostKeyVerifier hkv)
Adds the specified verifier.
void disconnect(DisconnectReason reason, String message)
Send a disconnect packet with the given reason and message, and closes this transport.
void disconnect()
Send a disconnection packet with reason as BY_APPLICATION, and closes this transport.
void disconnect(DisconnectReason reason)
Send a disconnect packet with the given reason, and closes this transport.
void doKex()
Do key exchange and algorithm negotiation.
String getClientVersion()
Config getConfig()
DisconnectListener getDisconnectListener()
int getHeartbeatInterval()
String getRemoteHost()
int getRemotePort()
String getServerVersion()
Returns the version string as sent by the SSH server for identification purposes, e.g.
synchronized Service getService()
byte[] getSessionID()
int getTimeout()
void handle(Message msg, SSHPacket buf)
This is where all incoming packets are handled.
void init(String remoteHost, int remotePort, InputStream in, OutputStream out)
Sets the host information and the streams to be used by this transport.
boolean isAuthenticated()
boolean isKexDone()
boolean isRunning()
void join(int timeout, TimeUnit unit)
Joins the thread calling this method to the transport's death.
void join()
Joins the thread calling this method to the transport's death.
void reqService(Service service)
Request a SSH service represented by a Service instance.
long sendUnimplemented()
Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.
void setAuthenticated()
Informs this transport that authentication has been completed.
void setDisconnectListener(DisconnectListener listener)
Specify a listener that will be notified upon disconnection.
void setHeartbeatInterval(int interval)
synchronized void setService(Service service)
Sets the currently active Service.
void setTimeout(int timeout)
Set a timeout for methods that may block.
long write(SSHPacket payload)
Write a packet over this transport.
[Expand]
Inherited Methods
From class java.lang.Object
From interface net.schmizz.sshj.common.SSHPacketHandler
From interface net.schmizz.sshj.transport.Transport

Fields

private boolean authed

private final String clientID

Client version identification string

private final Event<TransportException> close

private final Config config

private TransportImpl.ConnInfo connInfo

private final Decoder decoder

private DisconnectListener disconnectListener

private final Encoder encoder

private final Heartbeater heartbeater

private final KeyExchanger kexer

private final Logger log

private Message msg

Message identifier of last packet received

private final DisconnectListener nullDisconnectListener

private final Service nullService

private final Reader reader

private String serverID

Server version identification string

private Service service

Currently active service e.g. UserAuthService, ConnectionService

private final Event<TransportException> serviceAccept

private int timeout

private final ReentrantLock writeLock

Public Constructors

public TransportImpl (Config config)

Parameters
config

Public Methods

public void addHostKeyVerifier (HostKeyVerifier hkv)

Adds the specified verifier.

Parameters
hkv The host key verifier

public void disconnect (DisconnectReason reason, String message)

Send a disconnect packet with the given reason and message, and closes this transport.

Parameters
reason The reason code for this disconnect
message The text message

public void disconnect ()

Send a disconnection packet with reason as BY_APPLICATION, and closes this transport.

public void disconnect (DisconnectReason reason)

Send a disconnect packet with the given reason, and closes this transport.

Parameters
reason Reason for disconnecting

public void doKex ()

Do key exchange and algorithm negotiation. This can be the initial one or for algorithm renegotiation.

public String getClientVersion ()

public Config getConfig ()

public DisconnectListener getDisconnectListener ()

public int getHeartbeatInterval ()

public String getRemoteHost ()

public int getRemotePort ()

public String getServerVersion ()

Returns the version string as sent by the SSH server for identification purposes, e.g. "OpenSSH_$version".

If the transport has not yet been initialized via init(String, int, InputStream, OutputStream), it will be null.

Returns
  • server's version string (may be null)

public synchronized Service getService ()

public byte[] getSessionID ()

public int getTimeout ()

public void handle (Message msg, SSHPacket buf)

This is where all incoming packets are handled. If they pertain to the transport layer, they are handled here; otherwise they are delegated to the active service instance if any via handle(Message, SSHPacket).

Even among the transport layer specific packets, key exchange packets are delegated to KeyExchanger#handle.

This method is called in the context of the reader thread via Decoder#received when a full packet has been decoded.

Parameters
msg The message identifer
buf Buffer containg rest of the packet
Throws
SSHException if an error occurs during handling (unrecoverable)

public void init (String remoteHost, int remotePort, InputStream in, OutputStream out)

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.

Parameters
remoteHost Server's hostname
remotePort Server's port
in Input stream for the connection
out Output stream for the connection

public boolean isAuthenticated ()

public boolean isKexDone ()

public boolean isRunning ()

public void join (int timeout, TimeUnit unit)

Joins the thread calling this method to the transport's death.

Parameters
timeout
unit

public void join ()

Joins the thread calling this method to the transport's death.

public void reqService (Service service)

Request a SSH service represented by a Service instance. A separate call to setService(Service) is not needed.

Parameters
service The SSH service to be requested

public long sendUnimplemented ()

Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.

Returns
  • the sequence number of the packet sent

public void setAuthenticated ()

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.

public void setDisconnectListener (DisconnectListener listener)

Specify a listener that will be notified upon disconnection.

Parameters
listener

public void setHeartbeatInterval (int interval)

Parameters
interval

public synchronized void setService (Service service)

Sets the currently active Service. Handling of non-transport-layer packets is delegated to that service.

For this method to be successful, at least one service request via reqService(Service) must have been successful (not necessarily for the service being set).

Parameters
service (null-ok) the Service

public void setTimeout (int timeout)

Set a timeout for methods that may block.

Parameters
timeout The timeout in seconds

public long write (SSHPacket payload)

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).

Parameters
payload The SSHPacket containing data to send
Returns
  • sequence number of the sent packet