TCP Header
The TCP Header provides all of the details to enable reliable, ordered, byte-stream service for data delivery in IP-based networks. In this post, I will break down each of the header fields.
Here is a screenshot of a TCP header from the standard:
Source Port:
16 bits
Field identifies the sending port number.
Destination Port:
16 bits
Field identifies the destination port number.
Note: Each TCP segment contains a source and destination port number that identifies the sending and receiving applications. The port numbers and the source and destination IP addresses from the IP header uniquely identify each connection.
Sequence Number:
32 bits
This field identifies the byte in the data stream from the sending device to the receiving device. It is used for ordering segments and tracking the bytes of a data stream.
When a new connection is established, the SYN flag will be turned on. The sequence number field contains the initial sequence number (ISN), which is chosen by the host of the connection. The sequence number of the first data byte sent by the host will be the ISN + 1, since the SYN flag will consume the first number in the sequence.
Each end of a connection must maintain a sequence number of the data flowing in each direction.
Acknowledgement Number:
32 bits
If the ACK control bit is set, this field contains the value of the next sequence number the sender of the segment is expecting to receive
Once a connection is established, this is always sent.
The ACK should be the sequence number +1 of the last successfully received byte of data.
Data Offset/Header Length
4 bits
This field specifies the size of the TCP header in 32-bit words, which indicates where the data begins.
Reserved
3 bits
Reserved for future use
Control Bits:
9 bits
This field contains different flags that are used to control the TCP connection state.
Multiple flags can be combined to indicate specific states of the TCP connection.
Flag Bits:
URG - Urgent
When set, it indicates that the Urgent Pointer contains valid data that should be processed immediately.
ACK - Acknowledgement
When set, it indicates that the Acknowledgement Number is valid.
PSH - Push
Tells the receiving TCP stack to immediately push the data to the application, no buffering.
RST - Reset
Terminates the connection
SYN - Synchronize
Used during the three-way handshake to synchronize the sequence numbers between the sending and receiving devices.
FIN - Finish
Indicates that the sender has finished sending data and wishes to close the connection.
Window:
16 bits
The number of data octets beginning with the one indicated in the ACK field that the sender of this segment is willing to accept.
Checksum:
16 bits
Used for error detection of the TCP segment
Urgent Pointer:
16 bits
Provides a way for the sender to transmit emergency data to the receiving device
Options:
Variable Length
This optional field allows the sender/receiver to set options, including Maximum Segment Size (MSS) and Window Scaling.
Provides Timestamps.
Data:
Variable Length Field that carries the user data.
not required
Segments that are exchanged during the process of establishing a connection do not have a data payload.
In the next post, I will walk through establishing, maintaining, and tearing down a TCP session. This will apply the knowledge we learned here and demonstrate how each field is used in the TCP process. #HappyLearning