Package net.schmizz.sshj.transport
Class TransportImpl
- java.lang.Object
-
- net.schmizz.sshj.transport.TransportImpl
-
- All Implemented Interfaces:
RemoteAddressProvider
,SSHPacketHandler
,DisconnectListener
,Transport
public final class TransportImpl extends java.lang.Object implements Transport, DisconnectListener
A thread-safeTransport
implementation.
-
-
Constructor Summary
Constructors Constructor Description TransportImpl(Config config)
-
Method Summary
Modifier and Type Method Description void
addAlgorithmsVerifier(AlgorithmsVerifier verifier)
Adds the specified verifier.void
addHostKeyVerifier(HostKeyVerifier hkv)
Adds the specified verifier.void
die(java.lang.Exception ex)
Kill the transport in an exceptional way.void
disconnect()
Send a disconnection packet with reason asDisconnectReason.BY_APPLICATION
, and closes this transport.void
disconnect(DisconnectReason reason)
Send a disconnect packet with the givenreason
, and closes this transport.void
disconnect(DisconnectReason reason, java.lang.String message)
Send a disconnect packet with the givenreason
andmessage
, and closes this transport.void
doKex()
Do key exchange and algorithm negotiation.java.util.List<KeyAlgorithm>
getClientKeyAlgorithms(KeyType keyType)
java.lang.String
getClientVersion()
Config
getConfig()
DisconnectListener
getDisconnectListener()
KeyAlgorithm
getHostKeyAlgorithm()
java.lang.String
getRemoteHost()
int
getRemotePort()
java.net.InetSocketAddress
getRemoteSocketAddress()
Get Remote Socket Address using Connection Informationjava.lang.String
getServerVersion()
Returns the version string as sent by the SSH server for identification purposes, e.g.Service
getService()
byte[]
getSessionID()
int
getTimeoutMs()
void
handle(Message msg, SSHPacket buf)
This is where all incoming packets are handled.void
init(java.lang.String remoteHost, int remotePort, java.io.InputStream in, java.io.OutputStream out)
Sets the host information and the streams to be used by this transport.boolean
isAuthenticated()
boolean
isKexDone()
boolean
isRunning()
void
join()
Joins the thread calling this method to the transport's death.void
join(int timeout, java.util.concurrent.TimeUnit unit)
Joins the thread calling this method to the transport's death.void
notifyDisconnect(DisconnectReason reason, java.lang.String message)
TransportImpl implements its own default DisconnectListener.void
reqService(Service service)
Request a SSH service represented by aService
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 alistener
that will be notified upon disconnection.void
setHostKeyAlgorithm(KeyAlgorithm keyAlgorithm)
void
setService(Service service)
Sets the currently activeService
.void
setTimeoutMs(int timeoutMs)
Set a timeout for methods that may block.long
write(SSHPacket payload)
Write a packet over this transport.
-
-
-
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. ATransportException
is thrown in case of SSH protocol version incompatibility.- Specified by:
init
in interfaceTransport
- Parameters:
remoteHost
- server's hostnameremotePort
- server's portin
- input stream for the connectionout
- 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 interfaceRemoteAddressProvider
- Returns:
- Remote Socket Address or null when not connected
-
notifyDisconnect
public void notifyDisconnect(DisconnectReason reason, java.lang.String message)
TransportImpl implements its own default DisconnectListener.- Specified by:
notifyDisconnect
in interfaceDisconnectListener
-
addHostKeyVerifier
public void addHostKeyVerifier(HostKeyVerifier hkv)
Description copied from interface:Transport
Adds the specified verifier.- Specified by:
addHostKeyVerifier
in interfaceTransport
- Parameters:
hkv
- the host key verifier
-
addAlgorithmsVerifier
public void addAlgorithmsVerifier(AlgorithmsVerifier verifier)
Description copied from interface:Transport
Adds the specified verifier.- Specified by:
addAlgorithmsVerifier
in interfaceTransport
- 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 interfaceTransport
- Throws:
TransportException
- if there was an error during key exchange
-
isKexDone
public boolean isKexDone()
-
getTimeoutMs
public int getTimeoutMs()
- Specified by:
getTimeoutMs
in interfaceTransport
- 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 interfaceTransport
- Parameters:
timeoutMs
- the timeout in milliseconds
-
getRemoteHost
public java.lang.String getRemoteHost()
- Specified by:
getRemoteHost
in interfaceTransport
- Returns:
- the hostname to which this transport is connected.
-
getRemotePort
public int getRemotePort()
- Specified by:
getRemotePort
in interfaceTransport
- Returns:
- the port number on the remote host to which this transport is connected.
-
getClientVersion
public java.lang.String getClientVersion()
- Specified by:
getClientVersion
in interfaceTransport
- Returns:
- the version string used by this client to identify itself to an SSH server, e.g. "SSHJ_3_0"
-
getConfig
public Config getConfig()
-
getServerVersion
public java.lang.String getServerVersion()
Description copied from interface:Transport
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 viaTransport.init(java.lang.String, int, java.io.InputStream, java.io.OutputStream)
, it will benull
.- Specified by:
getServerVersion
in interfaceTransport
- Returns:
- server's version string (may be
null
)
-
getSessionID
public byte[] getSessionID()
- Specified by:
getSessionID
in interfaceTransport
- Returns:
- the session identifier assigned by server
-
getService
public Service getService()
- Specified by:
getService
in interfaceTransport
- Returns:
- the currently active
Service
instance.
-
setService
public void setService(Service service)
Description copied from interface:Transport
Sets the currently activeService
. Handling of non-transport-layer packets isdelegated
to that service. For this method to be successful, at least one service request viaTransport.reqService(net.schmizz.sshj.Service)
must have been successful (not necessarily for the service being set).- Specified by:
setService
in interfaceTransport
- Parameters:
service
- (null-ok) theService
-
reqService
public void reqService(Service service) throws TransportException
Description copied from interface:Transport
Request a SSH service represented by aService
instance. A separate call toTransport.setService(net.schmizz.sshj.Service)
is not needed.- Specified by:
reqService
in interfaceTransport
- Parameters:
service
- the SSH service to be requested- Throws:
TransportException
- if the request failed for any reason
-
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 interfaceTransport
-
isAuthenticated
public boolean isAuthenticated()
- Specified by:
isAuthenticated
in interfaceTransport
- 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 interfaceTransport
- Returns:
- the sequence number of the packet sent
- Throws:
TransportException
- if an error occurred sending the packet
-
join
public void join() throws TransportException
Description copied from interface:Transport
Joins the thread calling this method to the transport's death.- Specified by:
join
in interfaceTransport
- Throws:
TransportException
- if the transport dies of an exception
-
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 interfaceTransport
- Throws:
TransportException
- if the transport dies of an exception
-
isRunning
public boolean isRunning()
- Specified by:
isRunning
in interfaceTransport
- Returns:
- whether this transport is active.
The transport is considered to be running if it has been initialized without error via
Transport.init(java.lang.String, int, java.io.InputStream, java.io.OutputStream)
and has not been disconnected.
-
disconnect
public void disconnect()
Description copied from interface:Transport
Send a disconnection packet with reason asDisconnectReason.BY_APPLICATION
, and closes this transport.- Specified by:
disconnect
in interfaceTransport
-
disconnect
public void disconnect(DisconnectReason reason)
Description copied from interface:Transport
Send a disconnect packet with the givenreason
, and closes this transport.- Specified by:
disconnect
in interfaceTransport
- 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 givenreason
andmessage
, and closes this transport.- Specified by:
disconnect
in interfaceTransport
- Parameters:
reason
- the reason code for this disconnectmessage
- the text message
-
setDisconnectListener
public void setDisconnectListener(DisconnectListener listener)
Description copied from interface:Transport
Specify alistener
that will be notified upon disconnection.- Specified by:
setDisconnectListener
in interfaceTransport
- Parameters:
listener
- Disconnect Listener to be configured
-
getDisconnectListener
public DisconnectListener getDisconnectListener()
- Specified by:
getDisconnectListener
in interfaceTransport
- Returns:
- the current disconnect listener.
-
write
public long write(SSHPacket payload) throws TransportException
Description copied from interface:Transport
Write a packet over this transport. Thepayload
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 interfaceTransport
- Parameters:
payload
- theSSHPacket
containing data to send- Returns:
- sequence number of the sent packet
- Throws:
TransportException
- if an error occurred sending the packet
-
handle
public void handle(Message msg, SSHPacket buf) throws SSHException
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 viaSSHPacketHandler.handle(net.schmizz.sshj.common.Message, net.schmizz.sshj.common.SSHPacket)
. Even among the transport layer specific packets, key exchange packets are delegated toSSHPacketHandler.handle(net.schmizz.sshj.common.Message, net.schmizz.sshj.common.SSHPacket)
. This method is called in the context of thereader
thread viaDecoder.received(byte[], int)
when a full packet has been decoded.- Specified by:
handle
in interfaceSSHPacketHandler
- Parameters:
msg
- the message identifierbuf
- buffer containing rest of the packet- Throws:
SSHException
- if an error occurs during handling (unrecoverable)
-
die
public void die(java.lang.Exception ex)
Description copied from interface:Transport
Kill the transport in an exceptional way.
-
setHostKeyAlgorithm
public void setHostKeyAlgorithm(KeyAlgorithm keyAlgorithm)
-
getHostKeyAlgorithm
public KeyAlgorithm getHostKeyAlgorithm()
- Specified by:
getHostKeyAlgorithm
in interfaceTransport
-
getClientKeyAlgorithms
public java.util.List<KeyAlgorithm> getClientKeyAlgorithms(KeyType keyType) throws TransportException
- Specified by:
getClientKeyAlgorithms
in interfaceTransport
- Throws:
TransportException
-
-