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-safeTransportimplementation.
-
-
Constructor Summary
Constructors Constructor Description TransportImpl(Config config)
-
Method Summary
Modifier and Type Method Description voidaddAlgorithmsVerifier(AlgorithmsVerifier verifier)Adds the specified verifier.voidaddHostKeyVerifier(HostKeyVerifier hkv)Adds the specified verifier.voiddie(java.lang.Exception ex)Kill the transport in an exceptional way.voiddisconnect()Send a disconnection packet with reason asDisconnectReason.BY_APPLICATION, and closes this transport.voiddisconnect(DisconnectReason reason)Send a disconnect packet with the givenreason, and closes this transport.voiddisconnect(DisconnectReason reason, java.lang.String message)Send a disconnect packet with the givenreasonandmessage, and closes this transport.voiddoKex()Do key exchange and algorithm negotiation.java.util.List<KeyAlgorithm>getClientKeyAlgorithms(KeyType keyType)java.lang.StringgetClientVersion()ConfiggetConfig()DisconnectListenergetDisconnectListener()KeyAlgorithmgetHostKeyAlgorithm()java.lang.StringgetRemoteHost()intgetRemotePort()java.net.InetSocketAddressgetRemoteSocketAddress()Get Remote Socket Address using Connection Informationjava.lang.StringgetServerVersion()Returns the version string as sent by the SSH server for identification purposes, e.g.ServicegetService()byte[]getSessionID()intgetTimeoutMs()voidhandle(Message msg, SSHPacket buf)This is where all incoming packets are handled.voidinit(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.booleanisAuthenticated()booleanisKexDone()booleanisKeyExchangeRequired()Is Key Exchange required returns true when Key Exchange is not done and when Key Exchange is not ongoingbooleanisRunning()voidjoin()Joins the thread calling this method to the transport's death.voidjoin(int timeout, java.util.concurrent.TimeUnit unit)Joins the thread calling this method to the transport's death.voidnotifyDisconnect(DisconnectReason reason, java.lang.String message)TransportImpl implements its own default DisconnectListener.voidreqService(Service service)Request a SSH service represented by aServiceinstance.longsendUnimplemented()Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.voidsetAuthenticated()Informs this transport that authentication has been completed.voidsetDisconnectListener(DisconnectListener listener)Specify alistenerthat will be notified upon disconnection.voidsetHostKeyAlgorithm(KeyAlgorithm keyAlgorithm)voidsetService(Service service)Sets the currently activeService.voidsetTimeoutMs(int timeoutMs)Set a timeout for methods that may block.longwrite(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 TransportExceptionDescription copied from interface:TransportSets the host information and the streams to be used by this transport. Identification information is exchanged with the server. ATransportExceptionis thrown in case of SSH protocol version incompatibility.- Specified by:
initin 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:
getRemoteSocketAddressin 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:
notifyDisconnectin interfaceDisconnectListener
-
addHostKeyVerifier
public void addHostKeyVerifier(HostKeyVerifier hkv)
Description copied from interface:TransportAdds the specified verifier.- Specified by:
addHostKeyVerifierin interfaceTransport- Parameters:
hkv- the host key verifier
-
addAlgorithmsVerifier
public void addAlgorithmsVerifier(AlgorithmsVerifier verifier)
Description copied from interface:TransportAdds the specified verifier.- Specified by:
addAlgorithmsVerifierin interfaceTransport- Parameters:
verifier- The verifier to call with negotiated algorithms
-
doKex
public void doKex() throws TransportExceptionDescription copied from interface:TransportDo key exchange and algorithm negotiation. This can be the initial one or for algorithm renegotiation.- Specified by:
doKexin interfaceTransport- Throws:
TransportException- if there was an error during key exchange
-
isKeyExchangeRequired
public boolean isKeyExchangeRequired()
Is Key Exchange required returns true when Key Exchange is not done and when Key Exchange is not ongoing- Specified by:
isKeyExchangeRequiredin interfaceTransport- Returns:
- Key Exchange required status
-
isKexDone
public boolean isKexDone()
-
getTimeoutMs
public int getTimeoutMs()
- Specified by:
getTimeoutMsin interfaceTransport- Returns:
- the timeout that is currently set for blocking operations.
-
setTimeoutMs
public void setTimeoutMs(int timeoutMs)
Description copied from interface:TransportSet a timeout for methods that may block.- Specified by:
setTimeoutMsin interfaceTransport- Parameters:
timeoutMs- the timeout in milliseconds
-
getRemoteHost
public java.lang.String getRemoteHost()
- Specified by:
getRemoteHostin interfaceTransport- Returns:
- the hostname to which this transport is connected.
-
getRemotePort
public int getRemotePort()
- Specified by:
getRemotePortin interfaceTransport- Returns:
- the port number on the remote host to which this transport is connected.
-
getClientVersion
public java.lang.String getClientVersion()
- Specified by:
getClientVersionin 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:TransportReturns 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:
getServerVersionin interfaceTransport- Returns:
- server's version string (may be
null)
-
getSessionID
public byte[] getSessionID()
- Specified by:
getSessionIDin interfaceTransport- Returns:
- the session identifier assigned by server
-
getService
public Service getService()
- Specified by:
getServicein interfaceTransport- Returns:
- the currently active
Serviceinstance.
-
setService
public void setService(Service service)
Description copied from interface:TransportSets the currently activeService. Handling of non-transport-layer packets isdelegatedto 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:
setServicein interfaceTransport- Parameters:
service- (null-ok) theService
-
reqService
public void reqService(Service service) throws TransportException
Description copied from interface:TransportRequest a SSH service represented by aServiceinstance. A separate call toTransport.setService(net.schmizz.sshj.Service)is not needed.- Specified by:
reqServicein 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:TransportInforms 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:
setAuthenticatedin interfaceTransport
-
isAuthenticated
public boolean isAuthenticated()
- Specified by:
isAuthenticatedin interfaceTransport- Returns:
- whether the transport thinks it is authenticated.
-
sendUnimplemented
public long sendUnimplemented() throws TransportExceptionDescription copied from interface:TransportSends SSH_MSG_UNIMPLEMENTED in response to the last packet received.- Specified by:
sendUnimplementedin interfaceTransport- Returns:
- the sequence number of the packet sent
- Throws:
TransportException- if an error occurred sending the packet
-
join
public void join() throws TransportExceptionDescription copied from interface:TransportJoins the thread calling this method to the transport's death.- Specified by:
joinin interfaceTransport- Throws:
TransportException- if the transport dies of an exception
-
join
public void join(int timeout, java.util.concurrent.TimeUnit unit) throws TransportExceptionDescription copied from interface:TransportJoins the thread calling this method to the transport's death.- Specified by:
joinin interfaceTransport- Throws:
TransportException- if the transport dies of an exception
-
isRunning
public boolean isRunning()
- Specified by:
isRunningin 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:TransportSend a disconnection packet with reason asDisconnectReason.BY_APPLICATION, and closes this transport.- Specified by:
disconnectin interfaceTransport
-
disconnect
public void disconnect(DisconnectReason reason)
Description copied from interface:TransportSend a disconnect packet with the givenreason, and closes this transport.- Specified by:
disconnectin interfaceTransport- Parameters:
reason- reason for disconnecting
-
disconnect
public void disconnect(DisconnectReason reason, java.lang.String message)
Description copied from interface:TransportSend a disconnect packet with the givenreasonandmessage, and closes this transport.- Specified by:
disconnectin interfaceTransport- Parameters:
reason- the reason code for this disconnectmessage- the text message
-
setDisconnectListener
public void setDisconnectListener(DisconnectListener listener)
Description copied from interface:TransportSpecify alistenerthat will be notified upon disconnection.- Specified by:
setDisconnectListenerin interfaceTransport- Parameters:
listener- Disconnect Listener to be configured
-
getDisconnectListener
public DisconnectListener getDisconnectListener()
- Specified by:
getDisconnectListenerin interfaceTransport- Returns:
- the current disconnect listener.
-
write
public long write(SSHPacket payload) throws TransportException
Description copied from interface:TransportWrite a packet over this transport. ThepayloadSSHPacketshould 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:
writein interfaceTransport- Parameters:
payload- theSSHPacketcontaining 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 thereaderthread viaDecoder.received(byte[], int)when a full packet has been decoded.- Specified by:
handlein 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:TransportKill the transport in an exceptional way.
-
setHostKeyAlgorithm
public void setHostKeyAlgorithm(KeyAlgorithm keyAlgorithm)
-
getHostKeyAlgorithm
public KeyAlgorithm getHostKeyAlgorithm()
- Specified by:
getHostKeyAlgorithmin interfaceTransport
-
getClientKeyAlgorithms
public java.util.List<KeyAlgorithm> getClientKeyAlgorithms(KeyType keyType) throws TransportException
- Specified by:
getClientKeyAlgorithmsin interfaceTransport- Throws:
TransportException
-
-