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 sizeModifier and Type | Class and Description |
---|---|
protected static class |
AbstractSession.PendingWriteFuture
Future holding a packet pending key exchange termination.
|
Session.AttributeKey<T>
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 String[] |
clientProposal |
protected String |
clientVersion |
protected Service |
currentService |
protected Object |
decodeLock |
protected Buffer |
decoderBuffer |
protected int |
decoderLength |
protected int |
decoderState |
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 |
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 listener
|
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 Buffer |
uncompressBuffer |
protected String |
username
The name of the authenticated useer
|
CLOSED, closeFuture, GRACEFUL, IMMEDIATE, lock, log, OPENED, 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 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 |
doHandleMessage(Buffer buffer) |
protected String |
doReadIdentification(Buffer buffer)
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.
|
String |
getClientVersion()
Retrieve the client version for this session.
|
FactoryManager |
getFactoryManager()
Retrieve the factory manager
|
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 |
getServerVersion()
Retrieve the server version for this session.
|
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() |
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 void |
postClose() |
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) |
abstract 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.
|
doCloseGracefully, doCloseImmediately
close
public 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 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 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 attachednull
public static void attachSession(IoSession ioSession, AbstractSession session)
ioSession
- the MINA sessionsession
- the session to attachpublic String getServerVersion()
Session
getServerVersion
in interface Session
public String getClientVersion()
Session
getClientVersion
in interface Session
public KeyExchange getKex()
public byte[] getSessionId()
public IoSession getIoSession()
public FactoryManager getFactoryManager()
getFactoryManager
in interface Session
public boolean isAuthenticated()
public void setUsername(String username)
public void setAuthenticated() throws IOException
IOException
public 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 Session
t
- the exception to processprotected Closeable getInnerCloseable()
getInnerCloseable
in class CloseableUtils.AbstractInnerCloseable
protected void postClose()
postClose
in class CloseableUtils.AbstractCloseable
protected Service[] getServices()
public IoWriteFuture writePacket(Buffer buffer) throws IOException
writePacket
in interface Session
buffer
- the buffer to encode and sendIOException
- if an error occured when encoding sending the packetprotected IoWriteFuture doWritePacket(Buffer buffer) throws IOException
IOException
public Buffer request(Buffer buffer) throws IOException
request
in interface Session
buffer
- 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)
Session
createBuffer
in interface Session
cmd
- the SSH commandpublic Buffer createBuffer(byte cmd, int len)
createBuffer
in interface Session
cmd
- the SSH commandlen
- estimated number of bytes the buffer will hold, 0 if unknown.protected void decode() throws Exception
Exception
protected void sendIdentification(String ident)
ident
- our identification to sendprotected abstract boolean readIdentification(Buffer buffer) throws IOException
doReadIdentification(org.apache.sshd.common.util.Buffer)
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)
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 Session
name
- 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 Session
key
- 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 Session
key
- the key of the attribute; must not be null.value
- the value of the attribute; must not be null.public String getUsername()
Session
getUsername
in interface Session
public Object getLock()
public void addListener(SessionListener listener)
addListener
in interface Session
listener
- the session listener to addpublic void removeListener(SessionListener listener)
removeListener
in interface Session
listener
- the session listener to removeprotected void sendEvent(SessionListener.Event event) throws IOException
IOException
public SshFuture reExchangeKeys() throws IOException
reExchangeKeys
in interface Session
IOException
protected void checkRekey() throws IOException
IOException
protected abstract void sendKexInit() throws IOException
IOException
protected abstract void checkKeys() throws IOException
IOException
protected abstract void receiveKexInit(Buffer buffer) throws IOException
IOException
protected void serviceAccept() throws IOException
IOException
public abstract void resetIdleTimeout()
Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.