org.jnetpcap.protocol.tcpip
Class Tcp

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
          extended by org.jnetpcap.packet.JHeader
              extended by org.jnetpcap.packet.JHeaderMap<Tcp>
                  extended by org.jnetpcap.protocol.tcpip.Tcp
All Implemented Interfaces:
JCompoundHeader<Tcp>, JHeaderChecksum, JPayloadAccessor

@Header
public class Tcp
extends JHeaderMap<Tcp>
implements JHeaderChecksum

Transmission Control Protocol (TCP).

The Transmission Control Protocol (TCP) is one of the core protocols of the Internet Protocol Suite. TCP is one of the two original components of the suite, complementing the Internet Protocol (IP) and therefore the entire suite is commonly referred to as TCP/IP. TCP provides the service of exchanging data reliably directly between two network hosts, whereas IP handles addressing and routing message across one or more networks. In particular, TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. TCP is the protocol that major Internet applications rely on, such as the World Wide Web, e-mail, and file transfer. Other applications, which do not require reliable data stream service, may use the User Datagram Protocol (UDP) which provides a datagram service, which emphasizes reduced latency over reliability.

A TCP segment consists of a segment header and a data section. The TCP header contains 10 mandatory fields, and an optional extension field (Options).

The data section follows the header. Its contents are the payload data carried for the application. The length of the data section is not specified in the TCP segment header. It can be calculated by subtracting the combined length of the TCP header and the encapsulating IP segment header from the total IP segment length (specified in the IP segment header).

The header structure is as follows:

(The remaining options are obsolete, experimental, not yet standardized, or unassigned)

Description source: http://wikipedia.org/wiki/Tcp_protocol

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static class Tcp.AlternateChecksum
          The option described in this memo provides a mechanism to negotiate the use of an alternate checksum at connection-establishment time, as well as a mechanism to carry additional checksum information for algorithms that utilize checksums that are longer than 16 bits.
static class Tcp.AlternateChecksumRequest
          The option described in this memo provides a mechanism to negotiate the use of an alternate checksum at connection-establishment time, as well as a mechanism to carry additional checksum information for algorithms that utilize checksums that are longer than 16 bits.
static class Tcp.Flag
          Flags (8 bits) (aka Control bits) - contains 8 1-bit flags CWR (1 bit) - Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism (added to header by RFC 3168).
static class Tcp.MSS
          The TCP Maximum Segment Size option can be used to specify the maximum segment size that the receiver should use.
static class Tcp.NoOp
          No operation.
static class Tcp.SACK
          TCP may experience poor performance when multiple packets are lost from one window of data.
static class Tcp.SACK_PERMITTED
          The TCP SACK permitted option may be sent in a SYN by a TCP that has been extended to receive the SACK option once the connection has opened.
static class Tcp.TcpOption
          Options (Variable 0-320 bits, divisible by 32) - The length of this field is determined by the data offset field.
static class Tcp.Timestamp
          TCP timestamps, defined in RFC 1323, help TCP compute the round-trip time between the sender and receiver.
static class Tcp.WindowScale
          The window scale extension expands the definition of the TCP window to 32 bits and then uses a scale factor to carry this 32 bit value in the 16 bit Window field of the TCP header (SEG.WND in RFC-793).
 
Nested classes/interfaces inherited from class org.jnetpcap.packet.JHeader
JHeader.State
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
static int ID
          Unique numerical ID for this protocol header definition.
 
Fields inherited from class org.jnetpcap.packet.JHeaderMap
MAX_HEADERS, optionsBitmap, optionsLength, optionsOffsets, X_HEADERS
 
Fields inherited from class org.jnetpcap.packet.JHeader
annotatedHeader, BYTE, EMPTY_HEADER_ARRAY, isSubHeader, packet, state
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
Tcp()
           
 
Method Summary
 long ack()
          Acknowledgment number (32 bits).
 void ack(long ack)
          Ack.
 int calculateChecksum()
          Calculates a checksum using protocol specification for a header.
 int checksum()
          Checksum (16 bits).
 void checksum(int crc)
          Checksum.
 java.lang.String checksumDescription()
          Returns a dynamic description of the checksum field.
protected  void decodeHeader()
          Decode header.
 int destination()
          Destination port (16 bits).
 void destination(int value)
          Sets a new value for the destination field.
 boolean flags_ACK()
          ACK (1 bit) - indicates that the Acknowledgment field is significant.
 void flags_ACK(boolean value)
          Sets new value for the bit flag.
 boolean flags_CWR()
          CWR (1 bit) - Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism (added to header by RFC 3168).
 void flags_CWR(boolean value)
          Sets new value for the bit flag.
 boolean flags_ECE()
          ECE (1 bit) - ECN-Echo indicates If the SYN flag is set, that the TCP peer is ECN capable.
 void flags_ECE(boolean value)
          Sets new value for the bit flag.
 boolean flags_FIN()
          FIN (1 bit) - No more data from sender.
 void flags_FIN(boolean value)
          Sets new value for the bit flag.
 boolean flags_PSH()
          PSH (1 bit) - Push function.
 void flags_PSH(boolean value)
          Sets new value for the bit flag.
 boolean flags_RST()
          RST (1 bit) - Reset the connection.
 void flags_RST(boolean value)
          Sets new value for the bit flag.
 boolean flags_SYN()
          SYN (1 bit) - Synchronize sequence numbers.
 void flags_SYN(boolean value)
          Sets new value for the bit flag.
 boolean flags_URG()
          URG (1 bit) - indicates that the Urgent pointer field is significant.
 void flags_URG(boolean value)
          Sets new value for the bit flag.
 int flags()
          Flags (8 bits) (aka Control bits) - contains 8 1-bit flags CWR (1 bit) - Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism (added to header by RFC 3168).
 void flags(int value)
          Sets a new value for the flags field (8-bits).
 java.lang.String flagsCompactString()
          Returns a compact string representation of the flags contained within flags field.
 java.util.Set<Tcp.Flag> flagsEnum()
          Retruns a collection set representation of the flags contained within the flags field.
 int hashCode()
          Returns a bi-directional hashcode for this header.
static int headerLength(JBuffer buffer, int offset)
          Calculates the length of a tcp header.
 int hlen()
          Data offset (4 bits).
 void hlen(int length)
          Hlen.
 boolean isChecksumValid()
          Checks if the checksum is valid, for un-fragmented packets.
 int reserved()
          Reserved (4 bits).
 void reserved(int value)
          Sets a new value for the field.
 long seq()
          Sequence number (32 bits).
 void seq(long seq)
          Seq.
 int source()
          Source port (16 bits).
 void source(int src)
          Sets a new value for the field (16 bits).
 int uniHashCode()
          Uni-directional hashcode.
 int urgent()
          Urgent pointer (16 bits).
 void urgent(int urg)
          Urgent.
 int window()
          Window (16 bits).
 void window(int value)
          Sets the window field to new value.
 int windowScaled()
          A scaled, window field value.
 
Methods inherited from class org.jnetpcap.packet.JHeaderMap
getSubHeader, getSubHeaders, hasSubHeader, hasSubHeader, hasSubHeaders, setSubHeader, setSubHeaders
 
Methods inherited from class org.jnetpcap.packet.JHeader
decode, getAnnotatedHeader, getDescription, getFields, getGap, getGapLength, getGapOffset, getHeader, getHeaderLength, getHeaderOffset, getId, getIndex, getLength, getName, getNextHeaderId, getNextHeaderOffset, getNicname, getOffset, getPacket, getParent, getPayload, getPayloadLength, getPayloadOffset, getPostfix, getPostfixLength, getPostfixOffset, getPrefix, getPrefixLength, getPrefixOffset, getPreviousHeaderId, getPreviousHeaderOffset, getState, hasDescription, hasGap, hasNextHeader, hasPayload, hasPostfix, hasPrefix, hasPreviousHeader, isFragmented, isGapTruncated, isHeaderTruncated, isPayloadTruncated, isPostfixTruncated, isPrefixTruncated, peer, peer, peerPayloadTo, setPacket, sizeof, toString, transferPayloadTo, transferPayloadTo, transferPayloadTo, validateHeader
 
Methods inherited from class org.jnetpcap.nio.JBuffer
findUTF8String, getByte, getByteArray, getByteArray, getByteArray, getDouble, getFloat, getInt, getLong, getShort, getUByte, getUInt, getUShort, getUTF8Char, getUTF8String, getUTF8String, getUTF8String, getUTF8String, isReadonly, order, order, peer, peer, peer, peer, setByte, setByteArray, setByteBuffer, setDouble, setFloat, setInt, setLong, setShort, setShort0, setUByte, setUInt, setUShort, transferFrom, transferFrom, transferFrom, transferTo, transferTo, transferTo
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, transferTo, transferTo, transferTo, transferTo0
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ID

public static final int ID
Unique numerical ID for this protocol header definition.

See Also:
Constant Field Values
Constructor Detail

Tcp

public Tcp()
Method Detail

headerLength

@HeaderLength
public static int headerLength(JBuffer buffer,
                                            int offset)
Calculates the length of a tcp header.

Parameters:
buffer - buffer containing packet and/or tcp header data
offset - offset into the buffer where tcp header start (in bytes)
Returns:
number of bytes occupied by the tcp header, including any tcp options

ack

@Field(offset=64,
       length=16,
       format="%x")
public long ack()
Acknowledgment number (32 bits). If the ACK flag is set then the value of this field is the next sequence number that the receiver is expecting. This acknowledges receipt of all prior bytes (if any). The first ACK sent by each end acknowledges the other end's initial sequence number itself, but no data.

Returns:
the value of the field

ack

public void ack(long ack)
Ack.

Parameters:
ack - the ack

calculateChecksum

public int calculateChecksum()
Calculates a checksum using protocol specification for a header. Checksums for partial headers or fragmented packets (unless the protocol alows it) are not calculated.

Specified by:
calculateChecksum in interface JHeaderChecksum
Returns:
header's calculated checksum

checksum

@Field(offset=128,
       length=16,
       format="%x")
public int checksum()
Checksum (16 bits). The 16-bit checksum field is used for error-checking of the header and data .

Specified by:
checksum in interface JHeaderChecksum
Returns:
the field's value

checksum

public void checksum(int crc)
Checksum.

Parameters:
crc - the crc

checksumDescription

@Dynamic(value=DESCRIPTION)
public java.lang.String checksumDescription()
Returns a dynamic description of the checksum field. Specifically it checks and displays, as description, the state of the checksum field, if it matches the calculated checksum or not.

Returns:
additional information about the state of the checksum field

decodeHeader

protected void decodeHeader()
Decode header.

Overrides:
decodeHeader in class JHeader
See Also:
JHeader.decodeHeader()

destination

@Field(offset=16,
       length=16)
@FlowKey(index=2,
         reversable=true)
public int destination()
Destination port (16 bits). Identifies the receiving port

Returns:
the field's value

destination

public void destination(int value)
Sets a new value for the destination field.

Parameters:
value - new value for the field

flags

@Field(offset=104,
       length=8,
       format="%x")
public int flags()
Flags (8 bits) (aka Control bits) - contains 8 1-bit flags

Returns:
the field's value

flags

public void flags(int value)
Sets a new value for the flags field (8-bits).

Parameters:
value - new value for the field

flags_ACK

@Field(parent="flags",
       offset=4,
       length=1,
       format="%b",
       display="ack",
       description="acknowledgment")
public boolean flags_ACK()
ACK (1 bit) - indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.

Returns:
true if bit flag is set, otherwise false

flags_ACK

public void flags_ACK(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_CWR

@Field(parent="flags",
       offset=7,
       length=1,
       format="%b",
       display="cwr",
       description="reduced (cwr)")
public boolean flags_CWR()
CWR (1 bit) - Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism (added to header by RFC 3168).

Returns:
true if bit flag is set, otherwise false

flags_CWR

public void flags_CWR(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_ECE

@Field(parent="flags",
       offset=6,
       length=1,
       format="%b",
       display="ece",
       description="ECN echo flag")
public boolean flags_ECE()
ECE (1 bit) - ECN-Echo indicates If the SYN flag is set, that the TCP peer is ECN capable. If the SYN flag is clear, that a packet with Congestion Experienced flag in IP header set is received during normal transmission (added to header by RFC 3168).

Returns:
true if bit flag is set, otherwise false

flags_ECE

public void flags_ECE(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_FIN

@Field(parent="flags",
       offset=0,
       length=1,
       format="%b",
       display="fin",
       description="closing down connection")
public boolean flags_FIN()
FIN (1 bit) - No more data from sender.

Returns:
true if bit flag is set, otherwise false

flags_FIN

public void flags_FIN(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_PSH

@Field(parent="flags",
       offset=3,
       length=1,
       format="%b",
       display="ack",
       description="push current segment of data")
public boolean flags_PSH()
PSH (1 bit) - Push function. Asks to push the buffered data to the receiving application.

Returns:
true if bit flag is set, otherwise false

flags_PSH

public void flags_PSH(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_RST

@Field(parent="flags",
       offset=2,
       length=1,
       format="%b",
       display="ack",
       description="reset connection")
public boolean flags_RST()
RST (1 bit) - Reset the connection.

Returns:
true if bit flag is set, otherwise false

flags_RST

public void flags_RST(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_SYN

@Field(parent="flags",
       offset=1,
       length=1,
       format="%b",
       display="ack",
       description="synchronize connection, startup")
public boolean flags_SYN()
SYN (1 bit) - Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.

Returns:
true if bit flag is set, otherwise false

flags_SYN

public void flags_SYN(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flags_URG

@Field(parent="flags",
       offset=5,
       length=1,
       format="%b",
       display="ack",
       description="urgent, out-of-band data")
public boolean flags_URG()
URG (1 bit) - indicates that the Urgent pointer field is significant.

Returns:
true if bit flag is set, otherwise false

flags_URG

public void flags_URG(boolean value)
Sets new value for the bit flag.

Parameters:
value - sets the flag bit, false clears it

flagsCompactString

public java.lang.String flagsCompactString()
Returns a compact string representation of the flags contained within flags field.

Returns:
a terse representation of the flags

flagsEnum

public java.util.Set<Tcp.Flag> flagsEnum()
Retruns a collection set representation of the flags contained within the flags field.

Returns:
a collection set of the flags field

hashCode

public int hashCode()
Returns a bi-directional hashcode for this header. The hashcode is made up of IP source, IP destination, Tcp source and destination port numbers. It is created in a such a way that packet's source and destination fields are interchangable and will generate the same hashcode.

Overrides:
hashCode in class java.lang.Object
Returns:
bi-directional hashcode for this TCP/IP header combination
See Also:
uniHashCode()

hlen

@Field(offset=96,
       length=4)
public int hlen()
Data offset (4 bits). Specifies the size of the TCP header in 32-bit words. The minimum size header is 5 words and the maximum is 15 words thus giving the minimum size of 20 bytes and maximum of 60 bytes, allowing for up to 40 bytes of options in the header. This field gets its name from the fact that it is also the offset from the start of the TCP segment to the actual data.

Returns:
the field's value

hlen

public void hlen(int length)
Hlen.

Parameters:
length - in 4 byte words

isChecksumValid

public boolean isChecksumValid()
Checks if the checksum is valid, for un-fragmented packets. If a packet is fragmented, the checksum is not verified as data to is incomplete, but the method returns true none the less.

Specified by:
isChecksumValid in interface JHeaderChecksum
Returns:
true if checksum checks out or if this is a fragment, otherwise if the computed checksum does not match the stored checksum false is returned

reserved

@Field(offset=100,
       length=4)
public int reserved()
Reserved (4 bits). For future use and should be set to zero.

Returns:
the field's value

reserved

public void reserved(int value)
Sets a new value for the field.

Parameters:
value - new value (4 bits)

seq

@Field(offset=32,
       length=16,
       format="%x")
public long seq()
Sequence number (32 bits). Has a dual role:

Returns:
the field's value

seq

public void seq(long seq)
Seq.

Parameters:
seq - the seq

source

@Field(offset=0,
       length=16)
@FlowKey(index=2,
         reversable=true)
public int source()
Source port (16 bits). Identifies the sending port.

Returns:
the field's value

source

public void source(int src)
Sets a new value for the field (16 bits).

Parameters:
src - new value (16 bits)

uniHashCode

public int uniHashCode()
Uni-directional hashcode. A hashcode that is computed based on IP destination and TCP destination port. This make the hashcode uni-direction in the direction from source to destination.

Returns:
a hashcode that is uni-directional

urgent

@Field(offset=144,
       length=16)
public int urgent()
Urgent pointer (16 bits). If the URG flag is set, then this 16-bit field is an offset from the sequence number indicating the last urgent data byte.

Returns:
the field's value

urgent

public void urgent(int urg)
Urgent.

Parameters:
urg - the urg

window

@Field(offset=112,
       length=16)
public int window()
Window (16 bits). The size of the receive window, which specifies the number of bytes (beyond the sequence number in the acknowledgment field) that the receiver is currently willing to receive.

Flow control

TCP uses an end-to-end flow control protocol to avoid having the sender send data too fast for the TCP receiver to receive and process it reliably. Having a mechanism for flow control is essential in an environment where machines of diverse network speeds communicate. For example, if a PC sends data to a hand-held PDA that is slowly processing received data, the PDA must regulate data flow so as not to be overwhelmed.

TCP uses a sliding window flow control protocol. In each TCP segment, the receiver specifies in the receive window field the amount of additional received data (in bytes) that it is willing to buffer for the connection. The sending host can send only up to that amount of data before it must wait for an acknowledgment and window update from the receiving host.

When a receiver advertises a window size of 0, the sender stops sending data and starts the persist timer. The persist timer is used to protect TCP from a deadlock situation that could arise if the window size update from the receiver is lost and the sender has no more data to send while the receiver is waiting for the new window size update. When the persist timer expires, the TCP sender sends a small packet so that the receiver sends an acknowledgement with the new window size.

If a receiver is processing incoming data in small increments, it may repeatedly advertise a small receive window. This is referred to as the silly window syndrome, since it is inefficient to send only a few bytes of data in a TCP segment, given the relatively large overhead of the TCP header. TCP senders and receivers typically employ flow control logic to specifically avoid repeatedly sending small segments. The sender-side silly window syndrome avoidance logic is referred to as Nagle's algorithm.

Window scaling

For more efficient use of high bandwidth networks, a larger TCP window size may be used. The TCP window size field controls the flow of data and its value is limited to between 2 and 65,535 bytes.

Since the size field cannot be expanded, a scaling factor is used. The TCP window scale option, as defined in RFC 1323, is an option used to increase the maximum window size from 65,535 bytes to 1 Gigabyte. Scaling up to larger window sizes is a part of what is necessary for TCP Tuning.

The window scale option is used only during the TCP 3-way handshake. The window scale value represents the number of bits to left-shift the 16-bit window size field. The window scale value can be set from 0 (no shift) to 14 for each direction independently. Both sides must send the option in their SYN segments to enable window scaling in either direction.

Some routers and packet firewalls rewrite the window scaling factor during a transmission. This causes sending and receiving sides to assume different TCP window sizes. The result is non-stable traffic that may be very slow. The problem is visible on some sending and receiving sites behind the path of defective routers.

Returns:
the field's value

window

public void window(int value)
Sets the window field to new value.

Parameters:
value - new value for the field

windowScaled

public int windowScaled()
A scaled, window field value. The size of the receive window, which specifies the number of bytes (beyond the sequence number in the acknowledgment field) that the receiver is currently willing to receive.

This getter method, takes into account window scaling, as described below, and applies the scaling factor and returning the value.

Window scaling

For more efficient use of high bandwidth networks, a larger TCP window size may be used. The TCP window size field controls the flow of data and its value is limited to between 2 and 65,535 bytes.

Since the size field cannot be expanded, a scaling factor is used. The TCP window scale option, as defined in RFC 1323, is an option used to increase the maximum window size from 65,535 bytes to 1 Gigabyte. Scaling up to larger window sizes is a part of what is necessary for TCP Tuning.

The window scale option is used only during the TCP 3-way handshake. The window scale value represents the number of bits to left-shift the 16-bit window size field. The window scale value can be set from 0 (no shift) to 14 for each direction independently. Both sides must send the option in their SYN segments to enable window scaling in either direction.

Some routers and packet firewalls rewrite the window scaling factor during a transmission. This causes sending and receiving sides to assume different TCP window sizes. The result is non-stable traffic that may be very slow. The problem is visible on some sending and receiving sites behind the path of defective routers.

Returns:
the scaled value of the window field