TCP Packet Structure
- Source Port: Identifies an application on the source host, along with the IP address of the source host.
- Destination Port: Identifies an application on the destination host. The combination of the source and destination IP addresses in the TP header, together with the source port and destination port, uniquely identifies a single TCP connection.
- Sequence Number (seq): Identifies the stream of data bytes sent from the TCP sender to the TCP receiver, representing the sequence number of the first byte within this segment. Viewing the byte stream as unidirectional flow between two applications allows TCP to count each byte using its sequence number. The sequence number is a 32-bit unsigned integer; it wraps around from $2^{32}-1$ back to 0. When establishing a new connection, the SYN flag is set to 1, and the sequence number field contains an Initial Sequence Number (ISN) selected by this host.
- Acknowledgment Number (ack): Stores the next expected sequence number from the perspective of the endpoint sending acknowledgments. The acknowledgment number equals one plus the sequence number of the last byte successfully received. Only when the ACK flag is set to 1 is the acknowledgment number field valid. TCP provides full-duplex service to the application layer, meaning data can be transmitted independently in both directions. Consequently, each end of a connection must maintain its own transmission sequence numbers for each direction.
- Header Length: Stores the length of header data within the packet header; practically indicates where the data portion begins. This value is required because optional fields have variable lengths. The field occupies 4 bits, allowing TCP headers to be up to 60 bytes in total when including options. However, since this specific segment description does not include any optional fields, the normal length is 20 bytes.
- Reserved Bits: Reserved for future use; currently must be set to 0.
- Control Flags: There are six flag bits within the TCP header that can simultaneously be set to 1 as shown below:
- URG: When set to 1, indicates that the Urgent Pointer is valid; when set to 0, ignore the value of the Urgent Pointer.
- ACK: When set to 1, indicates that the Acknowledgment Number is valid; when set to 0, it implies no acknowledgment information is contained in this segment, and the ACK field should be ignored.
- PSH (Push): When set to 1, indicates data with the PUSH flag, instructing the receiver to deliver this segment to the application layer as soon as possible without waiting for the buffer to fill up.
- RST: Used to reset a connection that has failed due to host crash or other reasons; can also be used to reject illegal segments and deny connection requests. Generally speaking, if a RST flag set to 1 is received in a segment, some error condition must have occurred.
- SYN (Synchronize): Used for sequence synchronization; when set to 1, it indicates a connection request intended to establish the connection and synchronize the sequence numbers.
- FIN: Used to release the connection; when set to 1, it indicates that the sender has no more data to send in this direction, effectively closing its own data stream.
- Window Size: Represents the number of bytes available for transmission from the perspective of the source after receiving an acknowledgment. Specifically, it denotes how many bytes starting from the Acknowledgment Number can be received by the source; i.e., the size of the receiver's window. Since this field is 16 bits wide, the maximum window size is 65,535 bytes.
- Checksum: This checksum covers the entire TCP segment, including both the TCP header and the data payload, calculated using a polynomial division algorithm (often referred to as "dividing by sixteen" in older terminology). It is a mandatory field that must be computed and stored by the sender and verified by the receiver.
- Urgent Pointer: Valid only when the URG flag is set to 1; at this point, it informs TCP which data within the segment requires urgent transmission.
- Options: The most common optional field is the Maximum Segment Size (MSS). Each endpoint typically specifies this option in the first segment of communication, indicating the maximum length of segments that can be received by this TCP connection. Option lengths are not necessarily multiples of 32 bytes; therefore, padding bits may be used to ensure the header length becomes a multiple of an entire byte count.
- Data: The data portion within a TCP segment is optional. During connection establishment and termination exchanges, segments exchanged between both parties contain only the TCP header. If one party has no data to send, it also uses headers with empty data portions to acknowledge received data. Segments without any data are frequently sent in many timeout scenarios as well.