com.mindbright.ssh2
Class SSH2TransportPDU

java.lang.Object
  extended bycom.mindbright.ssh2.SSH2DataBuffer
      extended bycom.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
           
 
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(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.
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 createPDU(int bufSize)
          Create a new PDU to use for incoming packets.
protected  SSH2TransportPDU createPDU(int pktType, int bufSize)
          Create a new PDU for an outgoing packet.
static SSH2Transport.TranceiverContext createTranceiverContext(java.lang.String cipherName, java.lang.String macName, java.lang.String compName)
          Static function which creates a tranceiver context with the mentioned algorithms.
 SSH2Transport.TranceiverContext createTranceiverContextImpl(java.lang.String cipherName, java.lang.String macName, java.lang.String compName)
          An implementation which actually creates a tranceiver context.
 int getPayloadLength()
          Get the length of the payload.
 int getPayloadOffset()
          Get the offset in the data buffer where the payload starts.
 int getType()
           
 SSH2TransportPDU makeCopy()
          Create a copy of this PDU.
 void readFrom(java.io.InputStream in, int seqNum, SSH2Transport.TranceiverContext context)
          Read and decrypt an incoming packet from the given stream.
protected  void readNextNFrom(java.io.InputStream in, int n)
          Read a number of bytes from the stream and store in the internal buffer.
 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(java.io.OutputStream 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, reset, setData, setRPos, setWPos, writeBigInt, writeBigIntBits, writeBoolean, writeInt, writeLong, writeRaw, writeString, writeString
 
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
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

createPDU

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

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

createPDU

protected SSH2TransportPDU createPDU(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.

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,
                                                                            java.lang.String macName,
                                                                            java.lang.String compName)
                                                                     throws java.lang.Exception
Static function which creates a tranceiver context with the mentioned algorithms.

Parameters:
cipherName - Name of cipher algorithm to use.
macName - Name of message authentication cipher 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.


readFrom

public void readFrom(java.io.InputStream in,
                     int seqNum,
                     SSH2Transport.TranceiverContext context)
              throws java.io.IOException,
                     SSH2Exception,
                     ShortBufferException
Read and decrypt an incoming packet from the given stream. This function handles decryption, mac checking and uncompression.

Parameters:
in - Stream to read packet from.
seqNum - Sequence number of packet.
context - Tranceiver context.
Throws:
java.io.IOException
SSH2Exception
ShortBufferException

checkMac

protected void checkMac(Mac mac,
                        int macSize)
                 throws SSH2MacCheckException,
                        ShortBufferException
Throws:
SSH2MacCheckException
ShortBufferException

readNextNFrom

protected final void readNextNFrom(java.io.InputStream in,
                                   int n)
                            throws java.io.IOException,
                                   SSH2EOFException
Read a number of bytes from the stream and store in the internal buffer. This function may resize the buffer if needed.

Parameters:
in - Stream to read data from.
n - Number of bytes to read.
Throws:
java.io.IOException
SSH2EOFException

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(java.io.OutputStream out,
                    int seqNum,
                    SSH2Transport.TranceiverContext context,
                    SecureRandomAndPad rand)
             throws java.io.IOException,
                    ShortBufferException,
                    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
ShortBufferException
SSH2CompressionException

toString

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


createTranceiverContextImpl

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

Parameters:
cipherName - Name of cipher algorithm to use.
macName - Name of message authentication cipher to use.
compName - Name of compression algorithm to use.
Throws:
java.lang.Exception