com.mindbright.ssh2
Class SSH2TransportPDU

java.lang.Object
  extended by com.mindbright.ssh2.SSH2DataBuffer
      extended by com.mindbright.ssh2.SSH2TransportPDU
Direct Known Subclasses:
SSH2TransportPDUPool, SSH2TransportPDUPool.PoolPDU

public class SSH2TransportPDU
extends SSH2DataBuffer

Handles one packet data unit. These are the actual packets which are sent over en encrypted tunnel. Encryption and compression are applied to these.

There is a difference bwteeen incoming and outgoing PDUs. The latter have extra space allocated in them to help speed up the encryption process.

This class makes an effort to avoid copying data wherever possible.


Field Summary
static SSH2TransportPDU factoryInstance
           
static int PACKET_DEFAULT_SIZE
           
static int PACKET_MAX_SIZE
           
static int PACKET_MIN_SIZE
           
protected  int padSize
           
static int pktDefaultSize
           
protected  int pktSize
           
protected  int pktType
           
protected  int totSizeCompressed
           
protected  int totSizeUncompressed
           
 
Fields inherited from class com.mindbright.ssh2.SSH2DataBuffer
BOOLEAN_FALSE, BOOLEAN_TRUE, data, rPos, wPos
 
Constructor Summary
protected SSH2TransportPDU()
           
protected SSH2TransportPDU(int pktType, int bufSize)
          Constructor which creates a prefabricated packet.
 
Method Summary
protected  void checkMac(javax.crypto.Mac mac, int macSize)
           
static SSH2TransportPDU createIncomingPacket()
          Have the factory create a PDU for an incoming packet.
static SSH2TransportPDU createIncomingPacket(int bufSize)
          Have the factory create a PDU for an incoming packet.
protected  SSH2TransportPDU createInPDU(int bufSize)
          Create a new PDU to use for incoming packets.
static SSH2TransportPDU createOutgoingPacket(int pktType)
          Have the factory create a PDU for an outgoing packet.
static SSH2TransportPDU createOutgoingPacket(int pktType, int bufSize)
          Have the factory create a PDU for an outgoing packet.
protected  SSH2TransportPDU createOutPDU(int pktType, int bufSize)
          Create a new PDU for an outgoing packet.
protected  SSH2TransportPDU createPDU(int pktType, int bufSize)
          Create a new PDU for a packet.
static SSH2Transport.TranceiverContext createTranceiverContext(java.lang.String cipherName, int cipherDiscard, java.lang.String macName, int macLength, java.lang.String compName, NonBlockingInput in)
          Static function which creates a tranceiver context with the mentioned algorithms.
 SSH2Transport.TranceiverContext createTranceiverContextImpl(java.lang.String cipherName, int cipherDiscard, java.lang.String macName, int macLength, java.lang.String compName, NonBlockingInput in)
          An implementation which actually creates a tranceiver context.
 int getCompressedSize()
           
 int getPayloadLength()
          Get the length of the payload.
 int getPayloadOffset()
          Get the offset in the data buffer where the payload starts.
 int getType()
           
 int getUncompressedSize()
           
 java.nio.ByteBuffer initReceive(int seqNum, SSH2Transport.TranceiverContext context)
          Prepare this PDU for receiving data.
 SSH2TransportPDU makeCopy()
          Create a copy of this PDU.
 java.nio.ByteBuffer processData(java.nio.ByteBuffer buf)
          Read and decrypt data from the given stream.
 void release()
          Release this PDU.
static void setFactoryInstance(SSH2TransportPDU factory)
          Register a factor which handles the creation and destruction of incoming and outgoing packets.
 void setType(int pktType)
           
 java.lang.String toString()
          Creates a string representation of this PDU.
 void writeByte(int b)
           
 void writeRaw(byte[] raw, int off, int len)
           
 void writeString(byte[] str, int off, int len)
           
 void writeTo(NonBlockingOutput out, int seqNum, SSH2Transport.TranceiverContext context, SecureRandomAndPad rand)
          Encrypts and writes an outgoing packet to the stream.
 
Methods inherited from class com.mindbright.ssh2.SSH2DataBuffer
getData, getMaxReadSize, getMaxSize, getMaxWriteSize, getRPos, getWPos, readBigInt, readBigIntBits, readBoolean, readByte, readInt, readJavaString, readLong, readRaw, readRaw, readRestRaw, readString, readString, readUInt, reset, resize, setData, setRPos, setWPos, writeBigInt, writeBigIntBits, writeBoolean, writeInt, writeInt, writeLong, writeRaw, writeString, writeString, writeUTF8String
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PACKET_DEFAULT_SIZE

public static final int PACKET_DEFAULT_SIZE
See Also:
Constant Field Values

PACKET_MIN_SIZE

public static final int PACKET_MIN_SIZE
See Also:
Constant Field Values

PACKET_MAX_SIZE

public static final int PACKET_MAX_SIZE
See Also:
Constant Field Values

pktDefaultSize

public static int pktDefaultSize

factoryInstance

public static SSH2TransportPDU factoryInstance

pktSize

protected int pktSize

padSize

protected int padSize

pktType

protected int pktType

totSizeCompressed

protected int totSizeCompressed

totSizeUncompressed

protected int totSizeUncompressed
Constructor Detail

SSH2TransportPDU

protected SSH2TransportPDU()

SSH2TransportPDU

protected SSH2TransportPDU(int pktType,
                           int bufSize)
Constructor which creates a prefabricated packet.

Parameters:
pktType - Type of packet to create.
bufSize - How many bytes it should have room for.
Method Detail

createInPDU

protected SSH2TransportPDU createInPDU(int bufSize)
Create a new PDU to use for incoming packets.

Parameters:
bufSize - How many bytes it should have room for.

createOutPDU

protected SSH2TransportPDU createOutPDU(int pktType,
                                        int bufSize)
Create a new PDU for an outgoing packet.

Parameters:
pktType - Type of packet to create.
bufSize - How many bytes it should have room for.

createPDU

protected SSH2TransportPDU createPDU(int pktType,
                                     int bufSize)
Create a new PDU for a packet.

Parameters:
pktType - Type of packet to create.
bufSize - How many bytes it should have room for.

setFactoryInstance

public static final void setFactoryInstance(SSH2TransportPDU factory)
Register a factor which handles the creation and destruction of incoming and outgoing packets.


createIncomingPacket

public static final SSH2TransportPDU createIncomingPacket(int bufSize)
Have the factory create a PDU for an incoming packet.

Parameters:
bufSize - How many bytes it should have room for.

createIncomingPacket

public static final SSH2TransportPDU createIncomingPacket()
Have the factory create a PDU for an incoming packet.


createOutgoingPacket

public static final SSH2TransportPDU createOutgoingPacket(int pktType,
                                                          int bufSize)
Have the factory create a PDU for an outgoing packet.

Parameters:
pktType - Type of packet to create.
bufSize - How many bytes it should have room for.

createOutgoingPacket

public static final SSH2TransportPDU createOutgoingPacket(int pktType)
Have the factory create a PDU for an outgoing packet.

Parameters:
pktType - Type of packet to create.

createTranceiverContext

public static final SSH2Transport.TranceiverContext createTranceiverContext(java.lang.String cipherName,
                                                                            int cipherDiscard,
                                                                            java.lang.String macName,
                                                                            int macLength,
                                                                            java.lang.String compName,
                                                                            NonBlockingInput in)
                                                                     throws java.lang.Exception
Static function which creates a tranceiver context with the mentioned algorithms.

Parameters:
cipherName - Name of cipher algorithm to use.
cipherDiscard - How many bytes to discard after key init.
macName - Name of message authentication cipher to use.
macLength - How many bytes of the mac digest to use
compName - Name of compression algorithm to use.
Throws:
java.lang.Exception

release

public void release()
Release this PDU. This means that the PDU can be freed or reused for another packet.


makeCopy

public SSH2TransportPDU makeCopy()
Create a copy of this PDU.


getType

public int getType()

setType

public void setType(int pktType)

getPayloadLength

public int getPayloadLength()
Get the length of the payload. The payload is the actual data sent. Note that the payload may still be compressed.


getPayloadOffset

public int getPayloadOffset()
Get the offset in the data buffer where the payload starts.


getCompressedSize

public int getCompressedSize()

getUncompressedSize

public int getUncompressedSize()

initReceive

public java.nio.ByteBuffer initReceive(int seqNum,
                                       SSH2Transport.TranceiverContext context)
Prepare this PDU for receiving data. It will return a ByteBuffer which the caller should fill and return via the processData method.

Parameters:
seqNum - Sequence number of packet.
context - Tranceiver context.
Returns:
a buffer which should be fileld with data from the network

processData

public java.nio.ByteBuffer processData(java.nio.ByteBuffer buf)
                                throws SSH2Exception,
                                       javax.crypto.ShortBufferException,
                                       java.security.GeneralSecurityException
Read and decrypt data from the given stream. This function handles decryption, mac checking and uncompression. When we get there we know that the data buffer holds at least as many bytes as we previously prepared it for in prepareForRead

Returns:
a ByteBuffer if more data is needed to create the entire packet. Returns null when the entire packet has been read.
Throws:
SSH2Exception
javax.crypto.ShortBufferException
java.security.GeneralSecurityException

checkMac

protected void checkMac(javax.crypto.Mac mac,
                        int macSize)
                 throws SSH2MacCheckException,
                        java.security.GeneralSecurityException
Throws:
SSH2MacCheckException
java.security.GeneralSecurityException

writeByte

public final void writeByte(int b)
Overrides:
writeByte in class SSH2DataBuffer

writeString

public final void writeString(byte[] str,
                              int off,
                              int len)
Overrides:
writeString in class SSH2DataBuffer

writeRaw

public final void writeRaw(byte[] raw,
                           int off,
                           int len)
Overrides:
writeRaw in class SSH2DataBuffer

writeTo

public void writeTo(NonBlockingOutput out,
                    int seqNum,
                    SSH2Transport.TranceiverContext context,
                    SecureRandomAndPad rand)
             throws java.io.IOException,
                    java.security.GeneralSecurityException,
                    SSH2CompressionException
Encrypts and writes an outgoing packet to the stream. This function handles compression, mac calculation and encryption.

Parameters:
out - Stream to write resulting data to.
seqNum - Sequence number of packet.
context - Tranceiver context to use.
rand - An object from which random numbers and padding data is read.
Throws:
java.io.IOException
java.security.GeneralSecurityException
SSH2CompressionException

toString

public java.lang.String toString()
Creates a string representation of this PDU.

Overrides:
toString in class java.lang.Object

createTranceiverContextImpl

public SSH2Transport.TranceiverContext createTranceiverContextImpl(java.lang.String cipherName,
                                                                   int cipherDiscard,
                                                                   java.lang.String macName,
                                                                   int macLength,
                                                                   java.lang.String compName,
                                                                   NonBlockingInput in)
                                                            throws java.lang.Exception
An implementation which actually creates a tranceiver context.

Parameters:
cipherName - Name of cipher algorithm to use.
cipherDiscard - How many bytes to discard after key init.
macName - Name of message authentication cipher to use.
macLength - How many bytes of the mac digest to use
compName - Name of compression algorithm to use.
Throws:
java.lang.Exception