Package net.schmizz.sshj.transport
Class TransportImpl
java.lang.Object
net.schmizz.sshj.transport.TransportImpl
- All Implemented Interfaces:
RemoteAddressProvider
,SSHPacketHandler
,DisconnectListener
,Transport
A thread-safe
Transport
implementation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final String
Client version identification stringprivate final Event
<TransportException> private final Config
private TransportImpl.ConnInfo
private final Decoder
private DisconnectListener
private final Encoder
private KeyAlgorithm
private final KeyExchanger
private final org.slf4j.Logger
private final LoggerFactory
private Message
Message identifier of last packet receivedprivate Service
The next service that will be activated, only set when sending an SSH_MSG_SERVICE_REQUESTprivate final Service
private final Reader
private String
Server version identification stringprivate Service
Currently active service e.g.private final Event
<TransportException> private int
private final ReentrantLock
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAlgorithmsVerifier
(AlgorithmsVerifier verifier) Adds the specified verifier.void
Adds the specified verifier.void
Kill the transport in an exceptional way.void
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, String message) Send a disconnect packet with the givenreason
andmessage
, and closes this transport.void
doKex()
Do key exchange and algorithm negotiation.private void
(package private) String
getClientKeyAlgorithms
(KeyType keyType) (package private) TransportImpl.ConnInfo
(package private) Decoder
(package private) Encoder
int
Get Remote Socket Address using Connection Information(package private) String
Returns the version string as sent by the SSH server for identification purposes, e.g.byte[]
int
(package private) ReentrantLock
private void
private void
gotDisconnect
(SSHPacket buf) private void
private void
gotUnimplemented
(SSHPacket packet) Got an SSH_MSG_UNIMPLEMENTED, so lets see where we're at and act accordingly.void
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
boolean
private static boolean
isKexerPacket
(Message msg) boolean
void
join()
Joins the thread calling this method to the transport's death.void
Joins the thread calling this method to the transport's death.void
notifyDisconnect
(DisconnectReason reason, String message) TransportImpl implements its own default DisconnectListener.private String
readIdentification
(Buffer.PlainBuffer buffer) Reads the identification string from the SSH server.private void
void
reqService
(Service service) Request a SSH service represented by aService
instance.private void
Receive the server identification string.private void
sendDisconnect
(DisconnectReason reason, String message) private void
sendServiceRequest
(String serviceName) Sends a service request for the specified servicelong
Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.void
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 a packet over this transport.
-
Field Details
-
loggerFactory
-
log
private final org.slf4j.Logger log -
nullService
-
config
-
kexer
-
reader
-
encoder
-
decoder
-
hostKeyAlgorithm
-
serviceAccept
-
close
-
clientID
Client version identification string -
timeoutMs
private volatile int timeoutMs -
authed
private volatile boolean authed -
service
Currently active service e.g. UserAuthService, ConnectionService -
nextService
The next service that will be activated, only set when sending an SSH_MSG_SERVICE_REQUEST -
disconnectListener
-
connInfo
-
serverID
Server version identification string -
msg
Message identifier of last packet received -
writeLock
-
-
Constructor Details
-
TransportImpl
-
-
Method Details
-
init
public void init(String remoteHost, int remotePort, InputStream in, 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
Get Remote Socket Address using Connection Information- Specified by:
getRemoteSocketAddress
in interfaceRemoteAddressProvider
- Returns:
- Remote Socket Address or null when not connected
-
notifyDisconnect
TransportImpl implements its own default DisconnectListener.- Specified by:
notifyDisconnect
in interfaceDisconnectListener
-
receiveServerIdent
- Throws:
IOException
-
sendClientIdent
Receive the server identification string.- Throws:
IOException
- If there was an error writing to the outputstream.
-
readIdentification
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:
IOException
- Thrown when protocol version is not supported
-
addHostKeyVerifier
Description copied from interface:Transport
Adds the specified verifier.- Specified by:
addHostKeyVerifier
in interfaceTransport
- Parameters:
hkv
- the host key verifier
-
addAlgorithmsVerifier
Description copied from interface:Transport
Adds the specified verifier.- Specified by:
addAlgorithmsVerifier
in interfaceTransport
- Parameters:
verifier
- The verifier to call with negotiated algorithms
-
doKex
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
- 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
- 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
-
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
- Specified by:
getService
in interfaceTransport
- Returns:
- the currently active
Service
instance.
-
setService
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
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
-
sendServiceRequest
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 interfaceTransport
-
isAuthenticated
public boolean isAuthenticated()- Specified by:
isAuthenticated
in interfaceTransport
- Returns:
- whether the transport thinks it is authenticated.
-
sendUnimplemented
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
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
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
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
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
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
- Specified by:
getDisconnectListener
in interfaceTransport
- Returns:
- the current disconnect listener.
-
write
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
-
sendDisconnect
-
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(net.schmizz.sshj.common.Message, net.schmizz.sshj.common.SSHPacket)
. Even among the transport layer specific packets, key exchange packets are delegated toKeyExchanger.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)
-
isKexerPacket
-
gotDebug
- Throws:
TransportException
-
gotDisconnect
- Throws:
TransportException
-
gotServiceAccept
- Throws:
TransportException
-
gotUnimplemented
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 ongoingSSHException
-
finishOff
private void finishOff() -
die
Description copied from interface:Transport
Kill the transport in an exceptional way. -
getClientID
String getClientID() -
getServerID
String getServerID() -
getEncoder
Encoder getEncoder() -
getDecoder
Decoder getDecoder() -
getWriteLock
ReentrantLock getWriteLock() -
getConnInfo
TransportImpl.ConnInfo getConnInfo() -
setHostKeyAlgorithm
-
getHostKeyAlgorithm
- Specified by:
getHostKeyAlgorithm
in interfaceTransport
-
getClientKeyAlgorithms
- Specified by:
getClientKeyAlgorithms
in interfaceTransport
- Throws:
TransportException
-