public abstract class AbstractSession extends CloseableUtils.AbstractInnerCloseable implements Session
handleMessage(org.apache.sshd.common.util.Buffer)
method, which is dependant on the state and side of this session.
TODO: if there is any very big packet, decoderBuffer and uncompressBuffer will get quite big
and they won't be resized down at any time. Though the packet size is really limited
by the channel max packet size| Modifier and Type | Class and Description |
|---|---|
protected static class |
AbstractSession.PendingWriteFuture
Future holding a packet pending key exchange termination.
|
CloseableUtils.AbstractCloseable.StateSession.AttributeKey<T>, Session.TimeoutStatus| Modifier and Type | Field and Description |
|---|---|
protected Map<Session.AttributeKey<?>,Object> |
attributes |
protected boolean |
authed
Boolean indicating if this session has been authenticated or not
|
protected long |
authTimeoutMs |
protected long |
authTimeoutTimestamp |
protected String[] |
clientProposal |
protected String |
clientVersion |
protected Service |
currentService |
protected Object |
decodeLock |
protected Buffer |
decoderBuffer |
protected int |
decoderLength |
protected int |
decoderState |
protected long |
disconnectTimeoutMs |
protected Object |
encodeLock |
protected FactoryManager |
factoryManager
The factory manager used to retrieve factories of Ciphers, Macs and other objects
|
protected byte[] |
I_C |
protected byte[] |
I_S |
protected long |
idleTimeoutMs |
protected long |
idleTimeoutTimestamp |
protected long |
inBytes |
protected Cipher |
inCipher |
protected int |
inCipherSize |
protected Compression |
inCompression |
protected Mac |
inMac |
protected byte[] |
inMacResult |
protected long |
inPackets |
protected IoSession |
ioSession
The underlying MINA session
|
protected boolean |
isServer
Client or server side
|
protected KeyExchange |
kex |
protected static int |
KEX_STATE_DONE |
protected static int |
KEX_STATE_INIT |
protected static int |
KEX_STATE_KEYS |
protected static int |
KEX_STATE_RUN |
protected AtomicInteger |
kexState |
protected long |
lastKeyTime |
protected List<SessionListener> |
listeners
Session listeners container
|
protected String[] |
negotiated |
protected long |
outBytes |
protected Cipher |
outCipher |
protected int |
outCipherSize |
protected Compression |
outCompression |
protected Mac |
outMac |
protected long |
outPackets |
protected Queue<AbstractSession.PendingWriteFuture> |
pendingPackets |
protected Random |
random
The pseudo random generator
|
protected DefaultSshFuture |
reexchangeFuture |
protected Object |
requestLock |
protected AtomicReference<Buffer> |
requestResult |
protected long |
seqi |
protected long |
seqo |
protected String[] |
serverProposal |
protected String |
serverVersion |
static String |
SESSION
Name of the property where this session is stored in the attributes of the
underlying MINA session.
|
protected byte[] |
sessionId |
protected SessionListener |
sessionListenerProxy |
protected AtomicReference<Session.TimeoutStatus> |
timeoutStatus |
protected Buffer |
uncompressBuffer |
protected String |
username
The name of the authenticated user
|
closeFuture, lock, log, state| Constructor and Description |
|---|
AbstractSession(boolean isServer,
FactoryManager factoryManager,
IoSession ioSession)
Create a new session.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(SessionListener listener)
Add a session |listener|.
|
static void |
attachSession(IoSession ioSession,
AbstractSession session)
Attach a session to the MINA session
|
protected void |
checkForTimeouts()
Checks whether the session has timed out (both auth and idle timeouts are checked).
|
protected abstract void |
checkKeys() |
protected void |
checkRekey() |
Buffer |
createBuffer(byte cmd)
Create a new buffer for the specified SSH packet and reserve the needed space
(5 bytes) for the packet header.
|
Buffer |
createBuffer(byte cmd,
int len)
Create a new buffer for the specified SSH packet and reserve the needed space
(5 bytes) for the packet header.
|
protected String[] |
createProposal(String hostKeyTypes)
Create our proposal for SSH negotiation
|
protected void |
decode()
Decode the incoming buffer and handle packets as needed.
|
void |
disconnect(int reason,
String msg)
Send a disconnect packet with the given reason and message.
|
protected void |
doCloseImmediately()
doCloseImmediately is called once and only once
with state == Immediate
Overriding methods should always call the base implementation.
|
protected void |
doHandleMessage(Buffer buffer) |
protected String |
doReadIdentification(Buffer buffer,
boolean server)
Read the remote identification from this buffer.
|
protected IoWriteFuture |
doWritePacket(Buffer buffer) |
void |
exceptionCaught(Throwable t)
Handle any exceptions that occured on this session.
|
<T> T |
getAttribute(Session.AttributeKey<T> key)
Returns the value of the user-defined attribute of this session.
|
long |
getAuthTimeout()
What is timeout value in milliseconds for authentication stage
|
String |
getClientVersion()
Retrieve the client version for this session.
|
FactoryManager |
getFactoryManager()
Retrieve the factory manager
|
long |
getIdleTimeout()
What is timeout value in milliseconds for communication
|
protected Closeable |
getInnerCloseable() |
int |
getIntProperty(String name,
int defaultValue)
Retrieve a configuration property as an integer
|
IoSession |
getIoSession()
Retrieve the mina session
|
KeyExchange |
getKex() |
Object |
getLock() |
long |
getLongProperty(String name,
long defaultValue) |
String |
getNegotiatedKexParameter(int paramType)
Retrieve one of the negotiated values during the KEX stage
|
String |
getServerVersion()
Retrieve the server version for this session.
|
<T extends Service> |
getService(Class<T> clazz)
Get the service of the specified type.
|
protected Service[] |
getServices() |
static AbstractSession |
getSession(IoSession ioSession)
Retrieve the session from the MINA session.
|
static AbstractSession |
getSession(IoSession ioSession,
boolean allowNull)
Retrieve the session from the MINA session.
|
byte[] |
getSessionId() |
Session.TimeoutStatus |
getTimeoutStatus()
Check if timeout has occurred.
|
String |
getUsername()
Retrieve the name of the user authenticated on this session
or null if the session has not been authenticated yet.
|
protected void |
handleMessage(Buffer buffer)
Abstract method for processing incoming decoded packets.
|
boolean |
isAuthenticated() |
void |
messageReceived(Readable buffer)
Main input point for the MINA framework.
|
protected void |
negotiate()
Compute the negotiated proposals by merging the client and
server proposal.
|
protected void |
notImplemented()
Send an unimplemented packet.
|
protected abstract boolean |
readIdentification(Buffer buffer)
Read the other side identification.
|
protected abstract void |
receiveKexInit(Buffer buffer) |
protected byte[] |
receiveKexInit(Buffer buffer,
String[] proposal)
Receive the remote key exchange init message.
|
protected void |
receiveNewKeys()
Put new keys into use.
|
SshFuture |
reExchangeKeys()
Initiate a new key exchange.
|
void |
removeListener(SessionListener listener)
Remove a session |listener|.
|
Buffer |
request(Buffer buffer)
Send a global request and wait for the response.
|
protected void |
requestFailure(Buffer buffer) |
protected void |
requestSuccess(Buffer buffer) |
void |
resetIdleTimeout() |
protected void |
sendEvent(SessionListener.Event event) |
protected void |
sendIdentification(String ident)
Send our identification.
|
protected abstract void |
sendKexInit() |
protected byte[] |
sendKexInit(String[] proposal)
Send the key exchange initialization packet.
|
protected void |
sendNewKeys()
Send a message to put new keys into use.
|
protected void |
serviceAccept() |
<T,E extends T> |
setAttribute(Session.AttributeKey<T> key,
E value)
Sets a user-defined attribute.
|
void |
setAuthenticated() |
void |
setUsername(String username) |
abstract void |
startService(String name) |
String |
toString() |
IoWriteFuture |
writePacket(Buffer buffer)
Encode and send the given buffer.
|
IoWriteFuture |
writePacket(Buffer buffer,
long timeout,
TimeUnit unit)
Encode and send the given buffer with the specified timeout.
|
doCloseGracefullybuilder, close, isClosed, isClosing, preClosepublic static final String SESSION
getSession(IoSession, boolean)
and attachSession(IoSession, AbstractSession).protected static final int KEX_STATE_INIT
protected static final int KEX_STATE_RUN
protected static final int KEX_STATE_KEYS
protected static final int KEX_STATE_DONE
protected final boolean isServer
protected final FactoryManager factoryManager
protected final IoSession ioSession
protected final Random random
protected boolean authed
protected String username
protected final List<SessionListener> listeners
protected final SessionListener sessionListenerProxy
protected byte[] sessionId
protected String serverVersion
protected String clientVersion
protected String[] serverProposal
protected String[] clientProposal
protected String[] negotiated
protected byte[] I_C
protected byte[] I_S
protected KeyExchange kex
protected final AtomicInteger kexState
protected DefaultSshFuture reexchangeFuture
protected Cipher outCipher
protected Cipher inCipher
protected int outCipherSize
protected int inCipherSize
protected Mac outMac
protected Mac inMac
protected byte[] inMacResult
protected Compression outCompression
protected Compression inCompression
protected long seqi
protected long seqo
protected Buffer decoderBuffer
protected Buffer uncompressBuffer
protected int decoderState
protected int decoderLength
protected final Object encodeLock
protected final Object decodeLock
protected final Object requestLock
protected final AtomicReference<Buffer> requestResult
protected final Map<Session.AttributeKey<?>,Object> attributes
protected long authTimeoutTimestamp
protected long idleTimeoutTimestamp
protected long authTimeoutMs
protected long idleTimeoutMs
protected long disconnectTimeoutMs
protected final AtomicReference<Session.TimeoutStatus> timeoutStatus
protected volatile long inPackets
protected volatile long outPackets
protected volatile long inBytes
protected volatile long outBytes
protected volatile long lastKeyTime
protected final Queue<AbstractSession.PendingWriteFuture> pendingPackets
protected Service currentService
public AbstractSession(boolean isServer,
FactoryManager factoryManager,
IoSession ioSession)
factoryManager - the factory managerioSession - the underlying MINA sessionpublic static AbstractSession getSession(IoSession ioSession)
ioSession - the MINA sessionpublic static AbstractSession getSession(IoSession ioSession, boolean allowNull)
false,
an IllegalStateException will be thrown, else a null will
be returnedioSession - the MINA sessionallowNull - if true, a null value may be
returned if no session is attachednullpublic static void attachSession(IoSession ioSession, AbstractSession session)
ioSession - the MINA sessionsession - the session to attachpublic String getServerVersion()
SessiongetServerVersion in interface Sessionpublic String getClientVersion()
SessiongetClientVersion in interface Sessionpublic KeyExchange getKex()
public byte[] getSessionId()
public IoSession getIoSession()
getIoSession in interface Sessionpublic FactoryManager getFactoryManager()
getFactoryManager in interface Sessionpublic String getNegotiatedKexParameter(int paramType)
SessiongetNegotiatedKexParameter in interface SessionparamType - The parameter type index - one of the SSHConstants
PROPOSAL_XXX valuesnull if invalid
parameter index or no negotiated valuepublic boolean isAuthenticated()
public void setUsername(String username)
public void setAuthenticated()
throws IOException
IOExceptionpublic void messageReceived(Readable buffer) throws Exception
decode() method.buffer - the new buffer receivedException - if an error occurs while decoding or handling the dataprotected void handleMessage(Buffer buffer) throws Exception
buffer - the buffer containing the packetException - if an exeption occurs while handling this packet.public void exceptionCaught(Throwable t)
SshException.exceptionCaught in interface Sessiont - the exception to processprotected Closeable getInnerCloseable()
getInnerCloseable in class CloseableUtils.AbstractInnerCloseableprotected void doCloseImmediately()
CloseableUtils.AbstractCloseabledoCloseImmediately in class CloseableUtils.AbstractInnerCloseableprotected Service[] getServices()
public <T extends Service> T getService(Class<T> clazz)
SessiongetService in interface Sessionpublic IoWriteFuture writePacket(Buffer buffer) throws IOException
writePacket in interface Sessionbuffer - the buffer to encode and sendIOException - if an error occured when encoding sending the packetpublic IoWriteFuture writePacket(Buffer buffer, long timeout, TimeUnit unit) throws IOException
SessionIoWriteFuture will be set with a
TimeoutException exception to indicate a timeout.writePacket in interface Sessionbuffer - the buffer to encode and spendtimeout - the timeoutunit - the time unit of the timeout parameterIOException - if an error occurred when encoding sending the packetprotected IoWriteFuture doWritePacket(Buffer buffer) throws IOException
IOExceptionpublic Buffer request(Buffer buffer) throws IOException
request in interface Sessionbuffer - the buffer containing the global requesttrue if the request was successful, false otherwise.IOException - if an error occured when encoding sending the packetpublic Buffer createBuffer(byte cmd)
SessioncreateBuffer in interface Sessioncmd - the SSH commandpublic Buffer createBuffer(byte cmd, int len)
createBuffer in interface Sessioncmd - the SSH commandlen - estimated number of bytes the buffer will hold, 0 if unknown.protected void decode()
throws Exception
Exceptionprotected void sendIdentification(String ident)
ident - our identification to sendprotected abstract boolean readIdentification(Buffer buffer) throws IOException
doReadIdentification(org.apache.sshd.common.util.Buffer,boolean) and
store the result in the needed property.buffer - the buffer containing the remote identificationtrue if the identification has been fully read or
false if more data is neededIOException - if an error occurs such as a bad protocol versionprotected String doReadIdentification(Buffer buffer, boolean server)
null value will be returned. Else the identification
string will be returned and the data read will be consumed from the buffer.buffer - the buffer containing the identification stringnull if more data is neededprotected String[] createProposal(String hostKeyTypes)
hostKeyTypes - the list of supported host key typesprotected byte[] sendKexInit(String[] proposal) throws IOException
proposal - our proposal for key exchange negotiationIOException - if an error occurred sending the packetprotected byte[] receiveKexInit(Buffer buffer, String[] proposal)
buffer - the buffer containing the key exchange init packetproposal - the remote proposal to fillprotected void sendNewKeys()
throws IOException
IOException - if an error occurs sending the messageprotected void receiveNewKeys()
throws Exception
Exception - if an error occurspublic void disconnect(int reason,
String msg)
throws IOException
reason - the reason code for this disconnectmsg - the text messageIOException - if an error occured sending the packetprotected void notImplemented()
throws IOException
IOException - if an error occurred sending the packetprotected void negotiate()
negotiated property.public int getIntProperty(String name, int defaultValue)
getIntProperty in interface Sessionname - the name of the propertydefaultValue - the default valuepublic long getLongProperty(String name, long defaultValue)
public <T> T getAttribute(Session.AttributeKey<T> key)
getAttribute in interface Sessionkey - the key of the attribute; must not be null.public <T,E extends T> T setAttribute(Session.AttributeKey<T> key, E value)
setAttribute in interface Sessionkey - the key of the attribute; must not be null.value - the value of the attribute; must not be null.public String getUsername()
SessiongetUsername in interface Sessionpublic Object getLock()
public void addListener(SessionListener listener)
addListener in interface Sessionlistener - the session listener to addpublic void removeListener(SessionListener listener)
removeListener in interface Sessionlistener - the session listener to removeprotected void sendEvent(SessionListener.Event event) throws IOException
IOExceptionpublic SshFuture reExchangeKeys() throws IOException
reExchangeKeys in interface SessionIOExceptionprotected void checkRekey()
throws IOException
IOExceptionprotected abstract void sendKexInit()
throws IOException
IOExceptionprotected abstract void checkKeys()
throws IOException
IOExceptionprotected abstract void receiveKexInit(Buffer buffer) throws IOException
IOExceptionprotected void serviceAccept()
throws IOException
IOExceptionprotected void checkForTimeouts()
throws IOException
IOExceptionpublic void resetIdleTimeout()
resetIdleTimeout in interface Sessionpublic Session.TimeoutStatus getTimeoutStatus()
getTimeoutStatus in interface Sessionpublic long getAuthTimeout()
getAuthTimeout in interface Sessionpublic long getIdleTimeout()
getIdleTimeout in interface SessionCopyright © 2008–2015 The Apache Software Foundation. All rights reserved.