com.mindbright.ssh2
public final class SSH2Connection extends java.lang.Object implements java.lang.Runnable
To create a SSH2Connection
instance a connected
SSH2Transport
along with an authenticated
SSH2UserAuth
must be created first to be passed to the
constructor. Optionally a SSH2ConnectionEventHandler
can be
supplied to be able to monitor and control the connection layer. A log
handler can also be set, though by default the connection layer uses the same
log handler as the transport layer. All preferences are taken from the
SSH2Preferences
set in the transport layer.
The connection layer must be hooked into the transport layer explicitly by
calling the method setConnection
on the
SSH2Transport
. Once the connection layer is hooked up to the
transport layer channels can be created. There are basically four types of
channels: session, local forward, remote forward, and X11 forward. There are
methods for creating local and remote forwards aswell as session channels,
however, X11 channels must be created through session channels.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CH_REQ_AUTH_AGENT |
static java.lang.String |
CH_REQ_AUTH_AGENT1 |
static java.lang.String |
CH_REQ_BREAK |
static java.lang.String |
CH_REQ_ENV |
static java.lang.String |
CH_REQ_EXEC |
static java.lang.String |
CH_REQ_EXIT_SIG |
static java.lang.String |
CH_REQ_EXIT_STAT |
static java.lang.String |
CH_REQ_OPENSSH_EOW |
static java.lang.String |
CH_REQ_OPENSSH_KEEPALIVE |
static java.lang.String |
CH_REQ_PTY |
static java.lang.String |
CH_REQ_SHELL |
static java.lang.String |
CH_REQ_SIGNAL |
static java.lang.String |
CH_REQ_SUBSYSTEM |
static java.lang.String |
CH_REQ_WINCH |
static java.lang.String |
CH_REQ_X11 |
static java.lang.String |
CH_REQ_XONOFF |
static int |
CH_TYPE_AUTH_AGENT |
static int |
CH_TYPE_DIR_TCPIP |
static int |
CH_TYPE_FWD_TCPIP |
static int |
CH_TYPE_SESSION |
static int |
CH_TYPE_X11 |
static java.lang.String |
CHAN_AUTH_AGENT |
static java.lang.String |
CHAN_DIRECT_TCPIP |
static java.lang.String |
CHAN_FORWARDED_TCPIP |
static java.lang.String |
CHAN_SESSION |
static java.lang.String |
CHAN_X11 |
static java.lang.String |
GL_REQ_CANCEL_FORWARD |
static java.lang.String |
GL_REQ_START_FORWARD |
static int |
MAX_ACTIVE_CHANNELS |
Constructor and Description |
---|
SSH2Connection(SSH2UserAuth userAuth,
SSH2Transport transport)
Basic constructor used when there is no need for event handler.
|
SSH2Connection(SSH2UserAuth userAuth,
SSH2Transport transport,
SSH2ConnectionEventHandler eventHandler)
Constructor used when there need for event handler.
|
Modifier and Type | Method and Description |
---|---|
void |
connectLocalChannel(SSH2Channel channel,
java.lang.String remoteAddr,
int remotePort,
java.lang.String originAddr,
int originPort) |
void |
deleteLocalForward(java.lang.String localAddr,
int localPort)
Deletes the local forward identified by the given local address and port
pair.
|
void |
deleteRemoteForward(java.lang.String remoteAddr,
int remotePort)
Deletes the remote forward identified by the given remote address and
port pair.
|
void |
fatalDisconnect(int reason,
java.lang.String description)
Disconnects from peer using the DISCONNECT packet type with the given
reason and description.
|
SSH2Connector |
getConnector()
Gets the singleton instance of the
SSH2Connector which is
used by the connection layer to connect remote forwards through to local
hosts when they are opened. |
SSH2ConnectionEventHandler |
getEventHandler()
Gets the event handler currently in use.
|
SSH2StreamFilterFactory |
getForwardFilterFactory(java.lang.String remoteAddr,
int remotePort)
Gets the filter factory instance for a remote forward identified by the
given remote address and port pair.
|
java.lang.String[] |
getForwardTarget(java.lang.String remoteAddr,
int remotePort)
Gets the local target host address and port pair of a remote forward
identified by the given remote address and port pair.
|
SSH2Listener |
getLocalListener(java.lang.String localAddr,
int localPort)
Gets the
SSH2Listener instance of a local forward if it is
set up. |
Log |
getLog()
Gets the log handler currently in use.
|
SSH2Preferences |
getPreferences()
Gets the preferences set in the transport layer.
|
java.security.SecureRandom |
getSecureRandom()
Gets the
SecureRandom currently in use. |
SSH2Transport |
getTransport()
Gets our transport layer.
|
SSH2Listener |
newLocalForward(java.lang.String localAddr,
int localPort,
java.lang.String remoteAddr,
int remotePort)
Creates a new local forward from the given local address and port to
the remote address and port on the server side.
|
SSH2Listener |
newLocalForward(java.lang.String localAddr,
int localPort,
java.lang.String remoteAddr,
int remotePort,
SSH2StreamFilterFactory filterFactory)
Creates a new local forward from the given local address and port to the
remote address and port on the server side using the given filter factory
to insert filters in the input/output streams of the forwarded channels.
|
SSH2InternalChannel |
newLocalInternalForward(java.lang.String remoteAddr,
int remotePort)
Creates a new internal forward to
the remote address and port on the server side.
|
SSH2InternalChannel |
newLocalInternalForward(java.lang.String remoteAddr,
int remotePort,
SSH2StreamFilterFactory filterFactory)
Creates a new internal forward to
remote address and port on the server side using the given filter
factory to insert filters in the input/output streams of the forwarded
channels.
|
void |
newRemoteForward(java.lang.String remoteAddr,
int remotePort,
java.lang.String localAddr,
int localPort)
Creates a new remote forward from the given remote address and port on
the server to the local address and port.
|
void |
newRemoteForward(java.lang.String remoteAddr,
int remotePort,
java.lang.String localAddr,
int localPort,
SSH2StreamFilterFactory filterFactory)
Creates a new remote forward from the given remote address and port on
the server to the local address and port using the given filter factory
to insert filters in the input/output streams of the forwarded channels.
|
boolean |
newRemoteForwardBlocking(java.lang.String remoteAddr,
int remotePort,
java.lang.String localAddr,
int localPort,
SSH2StreamFilterFactory filterFactory)
Creates a new remote forward from the given remote address and port on
the server to the local address and port using the given filter factory
to insert filters in the input/output streams of the forwarded
channel.
|
SSH2SessionChannel |
newSession()
Creates a new session channel.
|
SSH2SessionChannel |
newSession(boolean pty) |
SSH2SessionChannel |
newSession(NonBlockingInput in,
NonBlockingOutput out,
NonBlockingOutput err) |
SSH2SessionChannel |
newSession(NonBlockingInput in,
NonBlockingOutput out,
NonBlockingOutput err,
boolean pty)
Creates a new session channel which will use the given
non-blocking streams as stdin, stdout and stderr.
|
SSH2SessionChannel |
newSession(SSH2StreamFilterFactory filterFactory,
boolean pty)
Creates a new session channel using the given filter factory
for filtering the standard input/output streams of the session.
|
SSH2SessionChannel |
newTerminal(SSH2TerminalAdapter termAdapter)
Creates a new session channel attaching its standard input/output streams
to the given terminal adapter.
|
void |
run()
The
run routine which implements the channel reaper. |
void |
setEventHandler(SSH2ConnectionEventHandler eventHandler)
Sets the event handler to use.
|
void |
setLog(Log log)
Sets the log handler to use.
|
void |
setSocketOptions(java.lang.String desc,
java.net.Socket sock) |
protected void |
startChannelReaper()
Start a new thread which repeatedly tries to kill channels
which should be killed.
|
protected void |
stopChannelReaper()
Stop the channel reaper.
|
void |
transmit(SSH2TransportPDU pdu)
Transmits the given PDU (by sending it to the transport layer, no
processing is needed at this point).
|
public static final int MAX_ACTIVE_CHANNELS
public static final java.lang.String GL_REQ_START_FORWARD
public static final java.lang.String GL_REQ_CANCEL_FORWARD
public static final java.lang.String CH_REQ_PTY
public static final java.lang.String CH_REQ_X11
public static final java.lang.String CH_REQ_ENV
public static final java.lang.String CH_REQ_SHELL
public static final java.lang.String CH_REQ_EXEC
public static final java.lang.String CH_REQ_SUBSYSTEM
public static final java.lang.String CH_REQ_WINCH
public static final java.lang.String CH_REQ_XONOFF
public static final java.lang.String CH_REQ_SIGNAL
public static final java.lang.String CH_REQ_EXIT_STAT
public static final java.lang.String CH_REQ_EXIT_SIG
public static final java.lang.String CH_REQ_AUTH_AGENT
public static final java.lang.String CH_REQ_AUTH_AGENT1
public static final java.lang.String CH_REQ_BREAK
public static final java.lang.String CH_REQ_OPENSSH_KEEPALIVE
public static final java.lang.String CH_REQ_OPENSSH_EOW
public static final java.lang.String CHAN_FORWARDED_TCPIP
public static final java.lang.String CHAN_DIRECT_TCPIP
public static final java.lang.String CHAN_SESSION
public static final java.lang.String CHAN_X11
public static final java.lang.String CHAN_AUTH_AGENT
public static final int CH_TYPE_FWD_TCPIP
public static final int CH_TYPE_DIR_TCPIP
public static final int CH_TYPE_SESSION
public static final int CH_TYPE_X11
public static final int CH_TYPE_AUTH_AGENT
public SSH2Connection(SSH2UserAuth userAuth, SSH2Transport transport)
userAuth
- the authentication layertransport
- the transport layerpublic SSH2Connection(SSH2UserAuth userAuth, SSH2Transport transport, SSH2ConnectionEventHandler eventHandler)
userAuth
- the authentication layertransport
- the transport layereventHandler
- the event handler (may be null
)SSH2ConnectionEventHandler
public SSH2Transport getTransport()
public void setEventHandler(SSH2ConnectionEventHandler eventHandler)
eventHandler
- the event handler to usepublic SSH2ConnectionEventHandler getEventHandler()
public SSH2Preferences getPreferences()
public Log getLog()
public void setLog(Log log)
log
- the log handler to usepublic java.security.SecureRandom getSecureRandom()
SecureRandom
currently in use. That is from the
transport layer.SecureRandom
in usepublic void transmit(SSH2TransportPDU pdu)
pdu
- packet to sendpublic void fatalDisconnect(int reason, java.lang.String description)
SSH2
for reason codes.
This is only a convenience method which calls the same method on the
transport layer.reason
- the reason codedescription
- the textual description for the cause of disconnectSSH2
public SSH2Connector getConnector()
SSH2Connector
which is
used by the connection layer to connect remote forwards through to local
hosts when they are opened.public java.lang.String[] getForwardTarget(java.lang.String remoteAddr, int remotePort)
remoteAddr
- the remote address of the forwardremotePort
- the remote port of the forwardpublic SSH2StreamFilterFactory getForwardFilterFactory(java.lang.String remoteAddr, int remotePort)
remoteAddr
- the remote address of the forwardremotePort
- the remote port of the forwardpublic SSH2Listener getLocalListener(java.lang.String localAddr, int localPort)
SSH2Listener
instance of a local forward if it is
set up.localAddr
- the local address of the forwardlocalPort
- the local port of the forwardnull
if none is setpublic void newRemoteForward(java.lang.String remoteAddr, int remotePort, java.lang.String localAddr, int localPort)
remoteAddr
- the remote address where the server listensremotePort
- the remote port where the server listenslocalAddr
- the local address to connect through tolocalPort
- the local port to connect through topublic void newRemoteForward(java.lang.String remoteAddr, int remotePort, java.lang.String localAddr, int localPort, SSH2StreamFilterFactory filterFactory)
remoteAddr
- the remote address where the server listensremotePort
- the remote port where the server listenslocalAddr
- the local address to connect through tolocalPort
- the local port to connect through tofilterFactory
- the filter factory instance to use for producing
filters.public boolean newRemoteForwardBlocking(java.lang.String remoteAddr, int remotePort, java.lang.String localAddr, int localPort, SSH2StreamFilterFactory filterFactory)
newRemoteForward
with the same parameters which waits until
a result is reported from the server which indicates whether the forward
could be set up or not.remoteAddr
- the remote address where the server listensremotePort
- the remote port where the server listenslocalAddr
- the local address to connect through tolocalPort
- the local port to connect through tofilterFactory
- the filter factory instance to use for producing
filters.public void deleteRemoteForward(java.lang.String remoteAddr, int remotePort)
remoteAddr
- the remote address of the forwardremotePort
- the remote port of the forwardpublic SSH2Listener newLocalForward(java.lang.String localAddr, int localPort, java.lang.String remoteAddr, int remotePort) throws java.io.IOException
localAddr
- the local address to listen tolocalPort
- the local port to listen toremoteAddr
- the remote address where the connects toremotePort
- the remote port where the connects tojava.io.IOException
public SSH2Listener newLocalForward(java.lang.String localAddr, int localPort, java.lang.String remoteAddr, int remotePort, SSH2StreamFilterFactory filterFactory) throws java.io.IOException
localAddr
- the local address to listen tolocalPort
- the local port to listen toremoteAddr
- the remote address where the connects toremotePort
- the remote port where the connects tofilterFactory
- the filter factory instance to use for producing
filters.java.io.IOException
public SSH2InternalChannel newLocalInternalForward(java.lang.String remoteAddr, int remotePort)
remoteAddr
- the remote address where the connects toremotePort
- the remote port where the connects topublic SSH2InternalChannel newLocalInternalForward(java.lang.String remoteAddr, int remotePort, SSH2StreamFilterFactory filterFactory)
remoteAddr
- the remote address where the connects toremotePort
- the remote port where the connects tofilterFactory
- the filter factory instance to use for producing
filters.public void deleteLocalForward(java.lang.String localAddr, int localPort)
localAddr
- the local address of the forwardlocalPort
- the local port of the forwardpublic SSH2SessionChannel newSession(NonBlockingInput in, NonBlockingOutput out, NonBlockingOutput err, boolean pty)
in
- stdin for the newly created streamout
- stdout for the newly created streamerr
- stderr for the newly created streampty
- if a pty will be allocated for this stream laterpublic SSH2SessionChannel newSession(NonBlockingInput in, NonBlockingOutput out, NonBlockingOutput err)
public SSH2SessionChannel newSession()
public SSH2SessionChannel newSession(boolean pty)
public SSH2SessionChannel newSession(SSH2StreamFilterFactory filterFactory, boolean pty)
filterFactory
- the filter factory to usepublic SSH2SessionChannel newTerminal(SSH2TerminalAdapter termAdapter)
attach
is
called before the channel open message is sent to the server so the
terminal adapter is attached before I/O is started.termAdapter
- the terminal adapter to attach to the sessionpublic void setSocketOptions(java.lang.String desc, java.net.Socket sock) throws java.io.IOException
java.io.IOException
public void connectLocalChannel(SSH2Channel channel, java.lang.String remoteAddr, int remotePort, java.lang.String originAddr, int originPort)
protected void startChannelReaper()
protected void stopChannelReaper()
public void run()
run
routine which implements the channel reaper.run
in interface java.lang.Runnable