Class TransportImpl
java.lang.Object
net.schmizz.sshj.transport.TransportImpl
- All Implemented Interfaces:
RemoteAddressProvider, SSHPacketHandler, DisconnectListener, Transport
A thread-safe
Transport implementation.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAlgorithmsVerifier(AlgorithmsVerifier verifier) Adds the specified verifier.voidAdds the specified verifier.voidKill the transport in an exceptional way.voidSend 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, String message) Send a disconnect packet with the givenreasonandmessage, and closes this transport.voiddoKex()Do key exchange and algorithm negotiation.getClientKeyAlgorithms(KeyType keyType) intGet Remote Socket Address using Connection InformationReturns the version string as sent by the SSH server for identification purposes, e.g.byte[]intvoidThis is where all incoming packets are handled.voidinit(String remoteHost, int remotePort, InputStream in, OutputStream out) Sets the host information and the streams to be used by this transport.booleanbooleanbooleanvoidjoin()Joins the thread calling this method to the transport's death.voidJoins the thread calling this method to the transport's death.voidnotifyDisconnect(DisconnectReason reason, String message) TransportImpl implements its own default DisconnectListener.voidreqService(Service service) Request a SSH service represented by aServiceinstance.longSends SSH_MSG_UNIMPLEMENTED in response to the last packet received.voidInforms 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 a packet over this transport.
-
Constructor Details
-
TransportImpl
-
-
Method Details
-
init
public void init(String remoteHost, int remotePort, InputStream in, OutputStream out) throws TransportException Description 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
Get Remote Socket Address using Connection Information- Specified by:
getRemoteSocketAddressin interfaceRemoteAddressProvider- Returns:
- Remote Socket Address or null when not connected
-
notifyDisconnect
TransportImpl implements its own default DisconnectListener.- Specified by:
notifyDisconnectin interfaceDisconnectListener
-
addHostKeyVerifier
Description copied from interface:TransportAdds the specified verifier.- Specified by:
addHostKeyVerifierin interfaceTransport- Parameters:
hkv- the host key verifier
-
addAlgorithmsVerifier
Description copied from interface:TransportAdds the specified verifier.- Specified by:
addAlgorithmsVerifierin interfaceTransport- Parameters:
verifier- The verifier to call with negotiated algorithms
-
doKex
Description 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
-
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
- 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
- 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
-
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(String, int, InputStream, 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
- Specified by:
getServicein interfaceTransport- Returns:
- the currently active
Serviceinstance.
-
setService
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(Service)must have been successful (not necessarily for the service being set).- Specified by:
setServicein interfaceTransport- Parameters:
service- (null-ok) theService
-
reqService
Description copied from interface:TransportRequest a SSH service represented by aServiceinstance. A separate call toTransport.setService(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
Description 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
Description 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
Description 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(String, int, InputStream, 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
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
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
Description copied from interface:TransportSpecify alistenerthat will be notified upon disconnection.- Specified by:
setDisconnectListenerin interfaceTransport- Parameters:
listener- Disconnect Listener to be configured
-
getDisconnectListener
- Specified by:
getDisconnectListenerin interfaceTransport- Returns:
- the current disconnect listener.
-
write
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
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(Message, SSHPacket). Even among the transport layer specific packets, key exchange packets are delegated toSSHPacketHandler.handle(Message, 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
-
setHostKeyAlgorithm
-
getHostKeyAlgorithm
- Specified by:
getHostKeyAlgorithmin interfaceTransport
-
getClientKeyAlgorithms
- Specified by:
getClientKeyAlgorithmsin interfaceTransport- Throws:
TransportException
-