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 classprivate static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final StringClient version identification stringprivate final Event<TransportException> private final Configprivate TransportImpl.ConnInfoprivate final Decoderprivate DisconnectListenerprivate final Encoderprivate KeyAlgorithmprivate final KeyExchangerprivate final org.slf4j.Loggerprivate final LoggerFactoryprivate MessageMessage identifier of last packet receivedprivate ServiceThe next service that will be activated, only set when sending an SSH_MSG_SERVICE_REQUESTprivate final Serviceprivate final Readerprivate StringServer version identification stringprivate ServiceCurrently active service e.g.private final Event<TransportException> private intprivate final ReentrantLock -
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.private void(package private) StringgetClientKeyAlgorithms(KeyType keyType) (package private) TransportImpl.ConnInfo(package private) Decoder(package private) EncoderintGet Remote Socket Address using Connection Information(package private) StringReturns the version string as sent by the SSH server for identification purposes, e.g.byte[]int(package private) ReentrantLockprivate voidprivate voidgotDisconnect(SSHPacket buf) private voidprivate voidgotUnimplemented(SSHPacket packet) Got an SSH_MSG_UNIMPLEMENTED, so lets see where we're at and act accordingly.voidThis 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.booleanbooleanprivate static booleanisKexerPacket(Message msg) booleanvoidjoin()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.private StringreadIdentification(Buffer.PlainBuffer buffer) Reads the identification string from the SSH server.private voidvoidreqService(Service service) Request a SSH service represented by aServiceinstance.private voidReceive the server identification string.private voidsendDisconnect(DisconnectReason reason, String message) private voidsendServiceRequest(String serviceName) Sends a service request for the specified servicelongSends 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.
-
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: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
-
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: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
-
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: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
-
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(Message, SSHPacket). Even among the transport layer specific packets, key exchange packets are delegated toKeyExchanger.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)
-
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
-
getClientID
String getClientID() -
getServerID
String getServerID() -
getEncoder
Encoder getEncoder() -
getDecoder
Decoder getDecoder() -
getWriteLock
ReentrantLock getWriteLock() -
getConnInfo
TransportImpl.ConnInfo getConnInfo() -
setHostKeyAlgorithm
-
getHostKeyAlgorithm
- Specified by:
getHostKeyAlgorithmin interfaceTransport
-
getClientKeyAlgorithms
- Specified by:
getClientKeyAlgorithmsin interfaceTransport- Throws:
TransportException
-