Class AbstractSession

java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.common.util.closeable.IoBaseCloseable
org.apache.sshd.common.util.closeable.AbstractCloseable
org.apache.sshd.common.util.closeable.AbstractInnerCloseable
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.AttributeStore, org.apache.sshd.common.auth.MutableUserHolder, org.apache.sshd.common.auth.UsernameHolder, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, org.apache.sshd.common.Closeable, FactoryManagerHolder, PortForwardingEventListenerManager, PortForwardingInformationProvider, KexExtensionHandlerManager, KexFactoryManager, org.apache.sshd.common.PropertyResolver, ReservedSessionMessagesManager, Session, org.apache.sshd.common.session.SessionContext, SessionDisconnectHandlerManager, org.apache.sshd.common.session.SessionHeartbeatController, SessionListenerManager, UnknownChannelReferenceHandlerManager, org.apache.sshd.common.signature.SignatureFactoriesHolder, org.apache.sshd.common.signature.SignatureFactoriesManager, org.apache.sshd.common.util.net.ConnectionEndpointsIndicator
Direct Known Subclasses:
AbstractClientSession, AbstractServerSession

public abstract class AbstractSession extends SessionHelper
Encapsulates common behavior for both client and server sessions. In particular, holds the FilterChain implementing the SSH transport protocol, and provides convenience methods to write SSH packets or make Channel global requests.
Author:
Apache MINA SSHD Project
  • Field Details

  • Constructor Details

    • AbstractSession

      protected AbstractSession(boolean serverSession, FactoryManager factoryManager, org.apache.sshd.common.io.IoSession ioSession)
      Create a new session.
      Parameters:
      serverSession - true if this is a server session, false if client one
      factoryManager - the factory manager
      ioSession - the underlying I/O session
  • Method Details

    • start

      protected void start() throws Exception
      Starts the SSH protocol. Invoked by the framework after the session object was fully created, and after SessionListener.sessionCreated(org.apache.sshd.common.session.Session) has been invoked.
      Throws:
      Exception - on errors
    • setupFilterChain

      protected void setupFilterChain()
    • getFilterChain

      public FilterChain getFilterChain()
      Description copied from interface: Session
      Retrieves the FilterChain of the session.
      Returns:
      the FilterChain; never null
    • getTransport

      protected SshTransportFilter getTransport()
    • isConnectionSecure

      protected boolean isConnectionSecure()
    • addKexListener

      public void addKexListener(KexListener listener)
    • removeKexListener

      public void removeKexListener(KexListener listener)
    • initializeKeyExchangePhase

      protected void initializeKeyExchangePhase() throws Exception
      Throws:
      Exception
    • isStrictKex

      protected boolean isStrictKex()
    • initializeCurrentService

      protected CurrentService initializeCurrentService()
      Creates a new CurrentService instance managing this session's current SSH service.

      This initialization method is invoked once from the AbstractSession constructor. Do not rely on subclass fields being initialized.

      Returns:
      a new CurrentService instance for the session
    • getServerVersion

      public String getServerVersion()
    • getServerKexProposals

      public Map<org.apache.sshd.common.kex.KexProposalOption, String> getServerKexProposals()
    • getClientVersion

      public String getClientVersion()
    • getClientKexProposals

      public Map<org.apache.sshd.common.kex.KexProposalOption, String> getClientKexProposals()
    • getKexState

      public org.apache.sshd.common.kex.KexState getKexState()
    • getSessionId

      public byte[] getSessionId()
    • getKexNegotiationResult

      public Map<org.apache.sshd.common.kex.KexProposalOption, String> getKexNegotiationResult()
    • getNegotiatedKexParameter

      public String getNegotiatedKexParameter(org.apache.sshd.common.kex.KexProposalOption paramType)
    • getCipherInformation

      public org.apache.sshd.common.cipher.CipherInformation getCipherInformation(boolean incoming)
    • getCompressionInformation

      public org.apache.sshd.common.compression.CompressionInformation getCompressionInformation(boolean incoming)
    • getMacInformation

      public org.apache.sshd.common.mac.MacInformation getMacInformation(boolean incoming)
    • handleMessage

      protected void handleMessage(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Abstract method for processing incoming decoded packets. The given buffer will hold the decoded packet, starting from the command byte at the read position.
      Parameters:
      buffer - The Buffer containing the packet - it may be re-used to generate the response once request has been decoded
      Throws:
      Exception - if an exception occurs while handling this packet.
    • handleKexExtension

      protected void handleKexExtension(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleNewCompression

      protected void handleNewCompression(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleServiceRequest

      protected void handleServiceRequest(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleServiceRequest

      protected boolean handleServiceRequest(String serviceName, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleServiceAccept

      protected void handleServiceAccept(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleServiceAccept

      protected void handleServiceAccept(String serviceName, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • getInnerCloseable

      protected org.apache.sshd.common.Closeable getInnerCloseable()
      Specified by:
      getInnerCloseable in class org.apache.sshd.common.util.closeable.AbstractInnerCloseable
    • preClose

      protected void preClose()
      Overrides:
      preClose in class org.apache.sshd.common.util.closeable.AbstractCloseable
    • getServices

      protected List<Service> getServices()
    • getService

      public <T extends Service> T getService(Class<T> clazz)
      Description copied from interface: Session
      Get the service of the specified type. If the service is not of the specified class, an IllegalStateException will be thrown.
      Type Parameters:
      T - The generic Service type
      Parameters:
      clazz - The service class
      Returns:
      The service instance
    • writePacket

      public org.apache.sshd.common.io.IoWriteFuture writePacket(org.apache.sshd.common.util.buffer.Buffer buffer) throws IOException
      Description copied from interface: Session
      Encode and send the given buffer. The buffer has to have 5 bytes free at the beginning to allow the encoding to take place. Also, the write position of the buffer has to be set to the position of the last byte to write.
      Parameters:
      buffer - the buffer to encode and send
      Returns:
      An IoWriteFuture that can be used to check when the packet has actually been sent
      Throws:
      IOException - if an error occurred when encoding sending the packet
    • writePacket

      public org.apache.sshd.common.io.IoWriteFuture writePacket(org.apache.sshd.common.util.buffer.Buffer buffer, long timeout, TimeUnit unit) throws IOException
      Description copied from interface: Session
      Encode and send the given buffer with the specified timeout. If the buffer could not be written before the timeout elapses, the returned IoWriteFuture will be set with a TimeoutException exception to indicate a timeout.
      Parameters:
      buffer - the buffer to encode and spend
      timeout - the timeout
      unit - the time unit of the timeout parameter
      Returns:
      a future that can be used to check when the packet has actually been sent
      Throws:
      IOException - if an error occurred when encoding or sending the packet
    • request

      public org.apache.sshd.common.util.buffer.Buffer request(String request, org.apache.sshd.common.util.buffer.Buffer buffer, long maxWaitMillis) throws IOException
      Description copied from interface: Session
      Send a global request and wait for the response, if the request is sent with want-reply = true.
      Parameters:
      request - the request name - used mainly for logging and debugging
      buffer - the buffer containing the global request
      maxWaitMillis - maximum time in milliseconds to wait for the request to finish - must be positive
      Returns:
      the return buffer if the request was successful, null otherwise.
      Throws:
      IOException - if an error occurred when encoding or sending the packet
    • request

      public GlobalRequestFuture request(org.apache.sshd.common.util.buffer.Buffer buffer, String request, GlobalRequestFuture.ReplyHandler replyHandler) throws IOException
      Description copied from interface: Session
      Send a global request and handle the reply asynchronously. If want-reply = true, pass the received Buffer to the given GlobalRequestFuture.ReplyHandler, which may execute in a different thread.
      want-reply == true && replyHandler != null
      The returned future is fulfilled with null when the request was sent, or with an exception if the request could not be sent. The replyHandler is invoked once the reply is received, with the SSH reply code and the data received.
      want-reply == true && replyHandler == null
      The returned future is fulfilled with an exception if the request could not be sent, or a failure reply was received. If a success reply was received, the future is fulfilled with the received data buffer.
      want-reply == false
      The returned future is fulfilled with an empty Buffer when the request was sent, or with an exception if the request could not be sent. If a reply handler is given, it is invoked with that empty buffer. The handler is not invoked if sending the request failed.
      Parameters:
      buffer - the Buffer containing the global request, with the want-reply flag set as appropriate
      request - the request name
      replyHandler - GlobalRequestFuture.ReplyHandler for handling the reply; may be null
      Returns:
      Created GlobalRequestFuture
      Throws:
      IOException - if an error occurred while encoding or sending the packet
    • doInvokeUnimplementedMessageHandler

      protected boolean doInvokeUnimplementedMessageHandler(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Overrides:
      doInvokeUnimplementedMessageHandler in class SessionHelper
      Parameters:
      cmd - The unimplemented command
      buffer - The input Buffer
      Returns:
      Result of invoking handleUnimplementedMessage
      Throws:
      Exception - if failed to handle the message
    • createBuffer

      public org.apache.sshd.common.util.buffer.Buffer createBuffer(byte cmd, int len)
      Description copied from interface: Session
      Create a new buffer for the specified SSH packet and reserve the needed space (5 bytes) for the packet header.
      Parameters:
      cmd - The SSH command to initialize the buffer with
      len - Estimated number of bytes the buffer will hold, 0 if unknown.
      Returns:
      a new buffer ready for write
      See Also:
    • prepareBuffer

      public org.apache.sshd.common.util.buffer.Buffer prepareBuffer(byte cmd, org.apache.sshd.common.util.buffer.Buffer buffer)
      Description copied from interface: Session
      Prepare a new "clean" buffer while reserving the needed space (5 bytes) for the packet header.
      Parameters:
      cmd - The SSH command to initialize the buffer with
      buffer - The Buffer instance to initialize
      Returns:
      The initialized buffer
    • validateTargetBuffer

      protected <B extends org.apache.sshd.common.util.buffer.Buffer> B validateTargetBuffer(int cmd, B buffer)
      Makes sure that the buffer used for output is not null.
      Type Parameters:
      B - The Buffer type being validated
      Parameters:
      cmd - The most likely command this buffer refers to (not guaranteed to be correct)
      buffer - The buffer to be examined
      Returns:
      The validated target instance - default same as input
      Throws:
      IllegalArgumentException - if any of the conditions is violated
    • readIdentification

      protected abstract boolean readIdentification(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Read the other side identification. This method is specific to the client or server side, but both should call SessionHelper.doReadIdentification(Buffer, boolean) and store the result in the needed property.
      Parameters:
      buffer - The Buffer containing the remote identification
      Returns:
      true if the identification has been fully read or false if more data is needed
      Throws:
      Exception - if an error occurs such as a bad protocol version or unsuccessful KEX was involved
    • notImplemented

      protected org.apache.sshd.common.io.IoWriteFuture notImplemented(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Send a SSH_MSG_UNIMPLEMENTED packet. This packet should contain the sequence id of the unsupported packet: this number is assumed to be the last packet received.
      Parameters:
      cmd - The un-implemented command value
      buffer - The Buffer that contains the command. Note: the buffer's read position is just beyond the command.
      Returns:
      An IoWriteFuture that can be used to wait for packet write completion - null if the registered ReservedSessionMessagesHandler decided to handle the command internally
      Throws:
      Exception - if an error occurred while handling the packet.
      See Also:
    • addSessionListener

      public void addSessionListener(SessionListener listener)
      Description copied from interface: SessionListenerManager
      Add a session listener.
      Parameters:
      listener - The SessionListener to add - not null
    • removeSessionListener

      public void removeSessionListener(SessionListener listener)
      Description copied from interface: SessionListenerManager
      Remove a session listener.
      Parameters:
      listener - The SessionListener to remove
    • getSessionListenerProxy

      public SessionListener getSessionListenerProxy()
      Returns:
      A (never null proxy SessionListener that represents all the currently registered listeners. Any method invocation on the proxy is replicated to the currently registered listeners
    • addChannelListener

      public void addChannelListener(ChannelListener listener)
      Description copied from interface: ChannelListenerManager
      Add a channel listener
      Parameters:
      listener - The ChannelListener to add - not null
    • removeChannelListener

      public void removeChannelListener(ChannelListener listener)
      Description copied from interface: ChannelListenerManager
      Remove a channel listener
      Parameters:
      listener - The ChannelListener to remove
    • getChannelListenerProxy

      public ChannelListener getChannelListenerProxy()
      Returns:
      A (never null proxy ChannelListener that represents all the currently registered listeners. Any method invocation on the proxy is replicated to the currently registered listeners
    • getPortForwardingEventListenerProxy

      public PortForwardingEventListener getPortForwardingEventListenerProxy()
      Returns:
      A proxy listener representing all the currently registered listeners in this manager
    • addPortForwardingEventListener

      public void addPortForwardingEventListener(PortForwardingEventListener listener)
      Description copied from interface: PortForwardingEventListenerManager
      Add a port forwarding listener
      Parameters:
      listener - The PortForwardingEventListener to add - never null
    • removePortForwardingEventListener

      public void removePortForwardingEventListener(PortForwardingEventListener listener)
      Description copied from interface: PortForwardingEventListenerManager
      Remove a port forwarding listener
      Parameters:
      listener - The PortForwardingEventListener to remove - ignored if null
    • reExchangeKeys

      public KeyExchangeFuture reExchangeKeys() throws IOException
      Description copied from interface: Session
      Initiate a new key exchange.
      Returns:
      A KeyExchangeFuture for awaiting the completion of the exchange
      Throws:
      IOException - If failed to request keys re-negotiation
    • resolveSessionKexProposal

      protected String resolveSessionKexProposal(String hostKeyTypes) throws IOException
      Overrides:
      resolveSessionKexProposal in class SessionHelper
      Throws:
      IOException
    • getCurrentService

      protected <T> T getCurrentService(Class<? extends T> type)
    • checkKeys

      protected abstract void checkKeys() throws IOException
      Indicates the the key exchange is completed and the exchanged keys can now be verified - e.g., client can verify the server's key
      Throws:
      IOException - If validation failed
    • getSession

      public static AbstractSession getSession(org.apache.sshd.common.io.IoSession ioSession) throws MissingAttachedSessionException
      Retrieve the SSH session from the I/O session. If the session has not been attached, an exception will be thrown
      Parameters:
      ioSession - The IoSession
      Returns:
      The SSH session attached to the I/O session
      Throws:
      MissingAttachedSessionException - if no attached SSH session
      See Also:
    • attachSession

      public static void attachSession(org.apache.sshd.common.io.IoSession ioSession, AbstractSession session) throws MultipleAttachedSessionException
      Attach an SSH AbstractSession to the I/O session
      Parameters:
      ioSession - The IoSession
      session - The SSH session to attach
      Throws:
      MultipleAttachedSessionException - If a previous session already attached
    • getSession

      public static AbstractSession getSession(org.apache.sshd.common.io.IoSession ioSession, boolean allowNull) throws MissingAttachedSessionException
      Retrieve the session SSH from the I/O session. If the session has not been attached and allowNull is false, an exception will be thrown, otherwise a null will be returned.
      Parameters:
      ioSession - The IoSession
      allowNull - If true, a null value may be returned if no session is attached
      Returns:
      the session attached to the I/O session or null
      Throws:
      MissingAttachedSessionException - if no attached session and allowNull=false