Savarese.Org

org.savarese.vserv.tcpip
Class TCPPacket

java.lang.Object
  extended byorg.savarese.vserv.tcpip.IPPacket
      extended byorg.savarese.vserv.tcpip.TCPPacket

public class TCPPacket
extends IPPacket

TCPPacket extends IPPacket to handle TCP packets. The TCP packet structure is described in RFC 761.

Author:
Daniel F. Savarese

Field Summary
static byte KIND_EOL
          A byte value for TCP options indicating end of option list.
static byte KIND_NOP
          A byte value for TCP options indicating no operation.
static byte KIND_SACK
          A byte value for TCP options identifying a selective acknowledgement option.
static int MASK_ACK
          A mask for extracting the ACK bit from the control header.
static int MASK_FIN
          A mask for extracting the FIN bit from the control header.
static int MASK_PSH
          A mask for extracting the push bit from the control header.
static int MASK_RST
          A mask for extracting the reset bit from the control header.
static int MASK_SYN
          A mask for extracting the SYN bit from the control header.
static int MASK_URG
          A mask for extracting the urgent bit from the control header.
static int OFFSET_ACK
          Offset into the TCP packet of the acknowledgement number header value.
static int OFFSET_CONTROL
          Offset into the TCP packet of the control header.
static int OFFSET_DESTINATION_PORT
          Offset into the TCP packet of the destination port header value.
static int OFFSET_HEADER_LENGTH
          Offset into the TCP packet of the TCP header length.
static int OFFSET_SEQUENCE
          Offset into the TCP packet of the sequence number header value.
static int OFFSET_SOURCE_PORT
          Offset into the TCP packet of the source port header value.
static int OFFSET_TCP_CHECKSUM
          Offset into the TCP packet of the TCP checksum.
static int OFFSET_URG_POINTER
          Offset into the TCP packet of the URG pointer.
static int OFFSET_WINDOW_SIZE
          Offset into the TCP packet of the window size.
 
Fields inherited from class org.savarese.vserv.tcpip.IPPacket
_data_, LENGTH_DESTINATION_ADDRESS, LENGTH_SOURCE_ADDRESS, OFFSET_DESTINATION_ADDRESS, OFFSET_FLAGS, OFFSET_IDENTIFICATION, OFFSET_IP_CHECKSUM, OFFSET_PROTOCOL, OFFSET_SOURCE_ADDRESS, OFFSET_TOTAL_LENGTH, OFFSET_TTL, OFFSET_TYPE_OF_SERVICE, PROTOCOL_ICMP, PROTOCOL_IP, PROTOCOL_TCP, PROTOCOL_UDP
 
Constructor Summary
TCPPacket(int size)
          Creates a new TCP packet of a given size.
TCPPacket(TCPPacket packet)
          Creates a new TCP packet that is a copy of a given packet.
 
Method Summary
 void addControlFlags(int mask)
          Sets the specified control bits without altering any other bits in the control header.
 void clearSACK()
          Clears all selective acknowledgement options.
 int computeTCPChecksum()
          Same as computeTCPChecksum(true);
 int computeTCPChecksum(boolean update)
          Computes the TCP checksum, optionally updating the TCP checksum header.
 void copyData(TCPPacket packet)
          Copies the contents of a TCPPacket.
 long getAckNumber()
           
 int getCombinedHeaderByteLength()
           
 int getDestinationPort()
           
 long getSequenceNumber()
           
 int getSourcePort()
           
 int getTCPChecksum()
           
 int getTCPDataByteLength()
           
 int getTCPHeaderByteLength()
           
 int getTCPHeaderLength()
           
 int getTCPPacketByteLength()
           
 int getUrgentPointer()
           
 int getWindowSize()
           
 boolean isSet(int mask)
           
 boolean isSetAny(int mask)
           
 boolean isSetOnly(int mask)
           
 void removeControlFlags(int mask)
          Unsets the specified control bits.
 void setAckNumber(long seq)
          Sets the acknowledgement number.
 void setControlFlags(int mask)
          Sets the control header to the sepecified value.
 void setData(byte[] data)
          Sets the raw packet byte array.
 void setDestinationPort(int port)
          Sets the destination port.
 void setIPHeaderLength(int length)
          Sets the IP header length field.
 void setSequenceNumber(long seq)
          Sets the sequence number.
 void setSourcePort(int port)
          Sets the source port.
 void setTCPDataByteLength(int length)
          Sets the length of the TCP data payload.
 void setTCPHeaderLength(int length)
          Sets te TCP header length (i.e., the data offset field) in 32-bit words.
 void setUrgentPointer(int pointer)
          Sets the urgent pointer.
 void setWindowSize(int window)
          Sets the TCP window size.
 
Methods inherited from class org.savarese.vserv.tcpip.IPPacket
_computeChecksum_, computeIPChecksum, computeIPChecksum, copy, getData, getDestination, getDestination, getDestinationAsInetAddress, getDestinationAsWord, getFragmentOffset, getIdentification, getIPChecksum, getIPFlags, getIPHeaderByteLength, getIPHeaderLength, getIPPacketLength, getIPVersion, getProtocol, getSource, getSource, getSourceAsInetAddress, getSourceAsWord, getTTL, getTypeOfService, setDestinationAsWord, setFragmentOffset, setIdentification, setIPFlags, setIPPacketLength, setIPVersion, setProtocol, setSourceAsWord, setTTL, setTypeOfService, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OFFSET_SOURCE_PORT

public static final int OFFSET_SOURCE_PORT
Offset into the TCP packet of the source port header value.

See Also:
Constant Field Values

OFFSET_DESTINATION_PORT

public static final int OFFSET_DESTINATION_PORT
Offset into the TCP packet of the destination port header value.

See Also:
Constant Field Values

OFFSET_SEQUENCE

public static final int OFFSET_SEQUENCE
Offset into the TCP packet of the sequence number header value.

See Also:
Constant Field Values

OFFSET_ACK

public static final int OFFSET_ACK
Offset into the TCP packet of the acknowledgement number header value.

See Also:
Constant Field Values

OFFSET_HEADER_LENGTH

public static final int OFFSET_HEADER_LENGTH
Offset into the TCP packet of the TCP header length.

See Also:
Constant Field Values

OFFSET_CONTROL

public static final int OFFSET_CONTROL
Offset into the TCP packet of the control header.

See Also:
Constant Field Values

OFFSET_WINDOW_SIZE

public static final int OFFSET_WINDOW_SIZE
Offset into the TCP packet of the window size.

See Also:
Constant Field Values

OFFSET_TCP_CHECKSUM

public static final int OFFSET_TCP_CHECKSUM
Offset into the TCP packet of the TCP checksum.

See Also:
Constant Field Values

OFFSET_URG_POINTER

public static final int OFFSET_URG_POINTER
Offset into the TCP packet of the URG pointer.

See Also:
Constant Field Values

MASK_FIN

public static final int MASK_FIN
A mask for extracting the FIN bit from the control header.

See Also:
Constant Field Values

MASK_SYN

public static final int MASK_SYN
A mask for extracting the SYN bit from the control header.

See Also:
Constant Field Values

MASK_RST

public static final int MASK_RST
A mask for extracting the reset bit from the control header.

See Also:
Constant Field Values

MASK_PSH

public static final int MASK_PSH
A mask for extracting the push bit from the control header.

See Also:
Constant Field Values

MASK_ACK

public static final int MASK_ACK
A mask for extracting the ACK bit from the control header.

See Also:
Constant Field Values

MASK_URG

public static final int MASK_URG
A mask for extracting the urgent bit from the control header.

See Also:
Constant Field Values

KIND_EOL

public static final byte KIND_EOL
A byte value for TCP options indicating end of option list.

See Also:
Constant Field Values

KIND_NOP

public static final byte KIND_NOP
A byte value for TCP options indicating no operation.

See Also:
Constant Field Values

KIND_SACK

public static final byte KIND_SACK
A byte value for TCP options identifying a selective acknowledgement option.

See Also:
Constant Field Values
Constructor Detail

TCPPacket

public TCPPacket(int size)
Creates a new TCP packet of a given size.

Parameters:
size - The number of bytes in the packet.

TCPPacket

public TCPPacket(TCPPacket packet)
Creates a new TCP packet that is a copy of a given packet.

Parameters:
packet - The packet to replicate.
Method Detail

clearSACK

public void clearSACK()
Clears all selective acknowledgement options. This is a temporary kluge and will be removed from the final API. Do not use it. The final API will have proper methods for adjusting selective acknowledgement options.


copyData

public final void copyData(TCPPacket packet)
Copies the contents of a TCPPacket. If the current data array is of insufficient length to store the contents, a new array is allocated.

Parameters:
packet - The TCPPacket to copy.

isSet

public boolean isSet(int mask)
Parameters:
mask - The bit mask to check.
Returns:
True only if all of the bits in the mask are set.

isSetAny

public boolean isSetAny(int mask)
Parameters:
mask - The bit mask to check.
Returns:
True if any of the bits in the mask are set.

isSetOnly

public boolean isSetOnly(int mask)
Parameters:
mask - The bit mask to check.
Returns:
True only if all of the bits in the mask are set and ONLY the bits in the mask are set.

addControlFlags

public void addControlFlags(int mask)
Sets the specified control bits without altering any other bits in the control header.

Parameters:
mask - The bits to set.

removeControlFlags

public void removeControlFlags(int mask)
Unsets the specified control bits.

Parameters:
mask - The bits to unset.

setControlFlags

public void setControlFlags(int mask)
Sets the control header to the sepecified value.

Parameters:
mask - The new control header bit mask.

setData

public void setData(byte[] data)
Description copied from class: IPPacket
Sets the raw packet byte array. Although this method would appear to violate object-oriented principles, it is necessary to implement efficient packet processing. You don't necessarily want to allocate a new IPPacket and data buffer every time a packet arrives and you need to be able to wrap packets from APIs that supply them as byte arrays.

Overrides:
setData in class IPPacket
Parameters:
data - The raw packet byte array to wrap.

setSourcePort

public final void setSourcePort(int port)
Sets the source port.

Parameters:
port - The new source port.

setDestinationPort

public final void setDestinationPort(int port)
Sets the destination port.

Parameters:
port - The new destination port.

getSourcePort

public final int getSourcePort()
Returns:
The source port.

getDestinationPort

public final int getDestinationPort()
Returns:
The destination port.

setSequenceNumber

public final void setSequenceNumber(long seq)
Sets the sequence number.

Parameters:
seq - The new sequence number.

getSequenceNumber

public final long getSequenceNumber()
Returns:
The sequence number.

setAckNumber

public final void setAckNumber(long seq)
Sets the acknowledgement number.

Parameters:
seq - The new acknowledgement number.

getAckNumber

public final long getAckNumber()
Returns:
The acknowledgement number.

setIPHeaderLength

public void setIPHeaderLength(int length)
Description copied from class: IPPacket
Sets the IP header length field. At most, this can be a four-bit value. The high order bits beyond the fourth bit will be ignored.

Overrides:
setIPHeaderLength in class IPPacket
Parameters:
length - The length of the IP header in 32-bit words.

setTCPHeaderLength

public final void setTCPHeaderLength(int length)
Sets te TCP header length (i.e., the data offset field) in 32-bit words.

Parameters:
length - The TCP header length in 32-bit words.

getTCPHeaderLength

public final int getTCPHeaderLength()
Returns:
The TCP header length in 32-bit words.

getTCPHeaderByteLength

public final int getTCPHeaderByteLength()
Returns:
The TCP header length in bytes.

setWindowSize

public final void setWindowSize(int window)
Sets the TCP window size.

Parameters:
window - The TCP window size.

getWindowSize

public final int getWindowSize()
Returns:
The TCP window size.

setUrgentPointer

public final void setUrgentPointer(int pointer)
Sets the urgent pointer.

Parameters:
pointer - The urgent pointer value.

getUrgentPointer

public final int getUrgentPointer()
Returns:
The urgent pointer value.

getTCPChecksum

public final int getTCPChecksum()
Returns:
The TCP checksum.

getTCPPacketByteLength

public final int getTCPPacketByteLength()
Returns:
The TCP packet length in bytes. This is the size of the IP packet minus the size of the IP header.

getCombinedHeaderByteLength

public final int getCombinedHeaderByteLength()
Returns:
The IP header length plus the TCP header length in bytes.

setTCPDataByteLength

public final void setTCPDataByteLength(int length)
Sets the length of the TCP data payload.

Parameters:
length - The length of the TCP data payload in bytes.

getTCPDataByteLength

public final int getTCPDataByteLength()

computeTCPChecksum

public final int computeTCPChecksum(boolean update)
Computes the TCP checksum, optionally updating the TCP checksum header.

Parameters:
update - Specifies whether or not to update the TCP checksum header after computing the checksum. A value of true indicates the header should be updated, a value of false indicates it should not be updated.
Returns:
The computed TCP checksum.

computeTCPChecksum

public final int computeTCPChecksum()
Same as computeTCPChecksum(true);

Returns:
The computed TCP checksum value.

Savarese.Org

Copyright © 2004-2005 Daniel F. Savarese. All Rights Reserved.