com.maverick.ssh2
Class TransportProtocol

java.lang.Object
  extended bycom.maverick.ssh2.TransportProtocol
All Implemented Interfaces:
com.maverick.ssh.message.SshMessageReader

public class TransportProtocol
extends java.lang.Object
implements com.maverick.ssh.message.SshMessageReader

Main implementation of the SSH Transport Protocol. The transport is designed to run over a provider such as a Socket or StreamConnection. To use first create an instance of the protocol and set its parameters and when ready start the protocol using startTransportProtocol supplying a TransportProvider instance. This example uses the SocketProvider example implementation demonstrated in TransportProvider Help File.

 TransportProtocol transport = new TransportProtocol();
 transport.ignoreHostKeyVerification(true);
 transport.startTransportProtocol(new SocketProvider("titan",22));
 

Host key verification is recommended and a callback interface is provided by HostKeyVerifcation. To force verification use setHostKeyVerification() before starting the protocol. This is required by default but can be ignored with ignoreHostKeyVerification(boolean ignore).

Additional cipher, message authentication and compression components can be supported by providing a custom TransportContext. The default context provides all the required components of the protocol but others can be added by creating an instance and adding the various implementations to the component factories.

 TransportContext context = new TransportContext();
 context.supportedCiphers().add("blowfish-cbc", "com.mycrypt.Blowfish");

 TransportProtocol transport = new TransportProtocol(
                                          TransportProtocol.CLIENT_MODE,
                                          context);
 

The context can also be used to specify the preferred methods of encryption for the connection which you may want to use should you add a new method to the context.

 context.setPreferredCipherCS("blowfish-cbc");
 

Author:
Lee David Painter
See Also:
TransportProvider

Field Summary
static int AUTH_CANCELLED_BY_USER
          Disconnect reason: Authentication was cancelled
static int BY_APPLICATION
          Disconnect reason: The application disconnected
static java.lang.String CHARSET_ENCODING
          Character set encoding.
static int COMPRESSION_ERROR
          Disconnect reason: A compression error occured
static int CONNECTED
          Protocol state: The transport protocol is connected and services can be started or may already be active.
static int CONNECTION_LOST
          Disconnect reason: The connection was lost
static int DISCONNECTED
          Protocol state: The transport protocol has disconnected.
static int HOST_KEY_NOT_VERIFIABLE
          Disconnect reason: The host key supplied could not be verified
static int HOST_NOT_ALLOWED
          Disconnect reason: The host is not allowed
static int ILLEGAL_USER_NAME
          Disconnect reason: The user's name is illegal
static int KEY_EXCHANGE_FAILED
          Disconnect reason: Key exchange failed
static int MAC_ERROR
          Disconnect reason: An error occured verifying the MAC
static int NEGOTIATING_PROTOCOL
          Protocol state: Negotation of the protocol version
static int NO_MORE_AUTH_METHODS_AVAILABLE
          Disconnect reason: No more authentication methods are available
static int PERFORMING_KEYEXCHANGE
          Protocol state: The protocol is performing key exchange
static int PROTOCOL_ERROR
          Disconnect reason: A protocol error occured
static int PROTOCOL_VERSION_NOT_SUPPORTED
          Disconnect reason: The protocol version is not supported
static int RESERVED
          Disconnect reason: Reserved
static int SERVICE_NOT_AVAILABLE
          Disconnect reason: The requested service is not available
static int TOO_MANY_CONNECTIONS
          Disconnect reason: Too many connections, try later
 
Constructor Summary
TransportProtocol()
          Create a default transport protocol instance in CLIENT_MODE.
 
Method Summary
 void disconnect(int reason, java.lang.String disconnectReason)
          Disconnect from the remote host.
 java.lang.Throwable getLastError()
          Returns the last error detected by the protocol.
 java.lang.String getRemoteIdentification()
          Get the identification string sent by the server during protocol negotiation
 com.maverick.crypto.security.SecureRandom getRND()
          Gets the secure random number generator for this transport.
 byte[] getSessionIdentifier()
          Get the session identifier
 boolean isConnected()
          Returns the connected state
 byte[] nextMessage()
          Get the next message.
 boolean processMessage(byte[] msg)
          Process a message.
 void sendMessage(byte[] msgdata)
          Send a transport protocol message.
 void startService(java.lang.String servicename)
          Request that the remote server starts a transport protocol service.
 void startTransportProtocol(SshTransport provider, Ssh2Context context, java.lang.String localIdentification, java.lang.String remoteIdentification)
          Starts the protocol on the provider.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHARSET_ENCODING

public static java.lang.String CHARSET_ENCODING
Character set encoding. All input/output strings created by the API are created with this encoding. The default is "UTF-8" and it may be changed, the results however are unexpected.


NEGOTIATING_PROTOCOL

public static final int NEGOTIATING_PROTOCOL
Protocol state: Negotation of the protocol version

See Also:
Constant Field Values

PERFORMING_KEYEXCHANGE

public static final int PERFORMING_KEYEXCHANGE
Protocol state: The protocol is performing key exchange

See Also:
Constant Field Values

CONNECTED

public static final int CONNECTED
Protocol state: The transport protocol is connected and services can be started or may already be active.

See Also:
Constant Field Values

DISCONNECTED

public static final int DISCONNECTED
Protocol state: The transport protocol has disconnected.

See Also:
getLastError(), Constant Field Values

HOST_NOT_ALLOWED

public static final int HOST_NOT_ALLOWED
Disconnect reason: The host is not allowed

See Also:
Constant Field Values

PROTOCOL_ERROR

public static final int PROTOCOL_ERROR
Disconnect reason: A protocol error occured

See Also:
Constant Field Values

KEY_EXCHANGE_FAILED

public static final int KEY_EXCHANGE_FAILED
Disconnect reason: Key exchange failed

See Also:
Constant Field Values

RESERVED

public static final int RESERVED
Disconnect reason: Reserved

See Also:
Constant Field Values

MAC_ERROR

public static final int MAC_ERROR
Disconnect reason: An error occured verifying the MAC

See Also:
Constant Field Values

COMPRESSION_ERROR

public static final int COMPRESSION_ERROR
Disconnect reason: A compression error occured

See Also:
Constant Field Values

SERVICE_NOT_AVAILABLE

public static final int SERVICE_NOT_AVAILABLE
Disconnect reason: The requested service is not available

See Also:
Constant Field Values

PROTOCOL_VERSION_NOT_SUPPORTED

public static final int PROTOCOL_VERSION_NOT_SUPPORTED
Disconnect reason: The protocol version is not supported

See Also:
Constant Field Values

HOST_KEY_NOT_VERIFIABLE

public static final int HOST_KEY_NOT_VERIFIABLE
Disconnect reason: The host key supplied could not be verified

See Also:
Constant Field Values

CONNECTION_LOST

public static final int CONNECTION_LOST
Disconnect reason: The connection was lost

See Also:
Constant Field Values

BY_APPLICATION

public static final int BY_APPLICATION
Disconnect reason: The application disconnected

See Also:
Constant Field Values

TOO_MANY_CONNECTIONS

public static final int TOO_MANY_CONNECTIONS
Disconnect reason: Too many connections, try later

See Also:
Constant Field Values

AUTH_CANCELLED_BY_USER

public static final int AUTH_CANCELLED_BY_USER
Disconnect reason: Authentication was cancelled

See Also:
Constant Field Values

NO_MORE_AUTH_METHODS_AVAILABLE

public static final int NO_MORE_AUTH_METHODS_AVAILABLE
Disconnect reason: No more authentication methods are available

See Also:
Constant Field Values

ILLEGAL_USER_NAME

public static final int ILLEGAL_USER_NAME
Disconnect reason: The user's name is illegal

See Also:
Constant Field Values
Constructor Detail

TransportProtocol

public TransportProtocol()
                  throws SshException
Create a default transport protocol instance in CLIENT_MODE.

Throws:
java.io.IOException
SshException
Method Detail

isConnected

public boolean isConnected()
Returns the connected state

Specified by:
isConnected in interface com.maverick.ssh.message.SshMessageReader
Returns:
true if the transport is connected, otherwise false

getLastError

public java.lang.Throwable getLastError()
Returns the last error detected by the protocol. If a disconnect occurs this may provide a reason.

Returns:
a last error detected by the transport protocol.

startTransportProtocol

public void startTransportProtocol(SshTransport provider,
                                   Ssh2Context context,
                                   java.lang.String localIdentification,
                                   java.lang.String remoteIdentification)
                            throws SshException
Starts the protocol on the provider.

Throws:
SshException

getRemoteIdentification

public java.lang.String getRemoteIdentification()
Get the identification string sent by the server during protocol negotiation

Returns:

getSessionIdentifier

public byte[] getSessionIdentifier()
Get the session identifier

Returns:

disconnect

public void disconnect(int reason,
                       java.lang.String disconnectReason)
Disconnect from the remote host. No more messages can be sent after this method has been called.

Parameters:
reason -
Throws:
java.io.IOException

getRND

public com.maverick.crypto.security.SecureRandom getRND()
Gets the secure random number generator for this transport.

Returns:
the secure RND

sendMessage

public void sendMessage(byte[] msgdata)
                 throws SshException

Send a transport protocol message. The format of the message should be:

 byte        Message ID
 byte[]      Payload
 

Parameters:
msgdata -
Throws:
java.io.IOException
SshException

nextMessage

public byte[] nextMessage()
                   throws SshException
Get the next message. The message returned will be the full message data so skipping the first 5 bytes is required before the message data can be read.

Specified by:
nextMessage in interface com.maverick.ssh.message.SshMessageReader
Returns:
a byte array containing all the message data
Throws:
java.io.IOException
SshException

startService

public void startService(java.lang.String servicename)
                  throws SshException
Request that the remote server starts a transport protocol service. This is only available in CLIENT_MODE.

Parameters:
servicename -
Throws:
java.io.IOException
SshException

processMessage

public boolean processMessage(byte[] msg)
                       throws SshException
Process a message. This should be called when reading messages from outside of the transport protocol so that the transport protocol can parse its own messages.

Parameters:
msg -
Returns:
true if the message was processed by the transport and can be discarded, otherwise false.
Throws:
SshException


Copyright © 2003 3SP LTD. All Rights Reserved.