TCP
From bildr
TCP (Transmission Control Protocol) is a core protocol of the Internet Protocol Suite along-side IP, creating the TCP/IP transmission standard. Where IP handled addressing and routing, TCP handles the data exchange between hosts. TCP provides reliable, handshaked, guaranteed data transfer, but at the expense of speed. If speed is valued over data reliability, UDP offers a faster alternative.
Contents |
Technical Details
Structure
TCP Header
| Bit offset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
| 0 | Source port | Destination port | ||||||||||||||||||||||||||||||
| 32 | Sequence number | |||||||||||||||||||||||||||||||
| 64 | Acknowledgment number | |||||||||||||||||||||||||||||||
| 96 | Data offset | Reserved | C W R | E C E | U R G | A C K | P S H | R S T | S Y N | F I N | Window Size | |||||||||||||||||||||
| 128 | Checksum | Urgent pointer | ||||||||||||||||||||||||||||||
| 160 ... | Options (if Data Offset > 5) ... |
|||||||||||||||||||||||||||||||
Protocols
TCP segments consist of a header part, and a data part.
The header contains 11 fields: 10 mandatory, 1 optional.
- Source Port (16 bits) - port sending the data
- Destination Port (16 bits) - port receiving the data
- Sequence Number (32 bits) - If the SYN flag is set this is the sequence number. The first data byte is this number plus 1. This the SYN flag is not set, this is the total sequence number
- Acknowledgment Number (32 bits) - Ascts as a receipt. If the ACK flag is set, this is the next expected sequence number.
- Data Offset (4 bits) - Specifies the size of the header in 32-bit chunks from 5 to 15.
- Reserved (4 bits) - not used, set to zero
- Flags (8 bits) - 8 flags
- CWR (1 bit) - Congestion Window Reduced flag: indicates that an ECE flag was received and is being handled
- ECE (1 bit) - ECN-Echo: If SYN is set, then the peer is ECN capable. If SYN is not set, a packet marked has experiencing congestion was received.
- URG (1 bit) - Urgent: Urgent pointer field is important
- ACK (1 bit) - Acknowledgment : Acknowledgment is important, all subsequent packets should have ACK set. Disabling this prevents data verification
- PSH (1 bit) - Push: push the buffered data to the destination client
- RST (1 bit) - Reset: reset the connection
- SYN (1 bit) - Synchronize: Only set on the first packet. Synchronizes the sequence numbers
- FIN (1 bit) - Finished: Sender is done, no more packets will be sent.
- Window (16 bits) - size of receive window which specifies the number of bytes the receiver is willing to accept.
- Checksum (16 bits) - 16-bit checksum to verify received data
- Urgent Pointer (16 bits) - If URG is set, this is a 16-bit offset from the sequence number indicating the last urgent byte.
- Options (variable, up to 120 bits, divisible by 8
- 0 (8 bits) - End of list
- 1 (8 bits) - no padding
- 2,4,SS (32 bits) - max segment size (must have SYN set)
- 3,3,S (24 bits) - window scale(must have SYN set)
- 4,2 (16 bits) - selective acknowledgment (must have SYN set)
- 5,N,BBBB,EEEE,... (variable bits, N is either 10, 18, 26, or 34) - Selective ACK (SACK)
- 8,10,TTTT,EEEE (80 bits) - Timestamp and echo previous timestamp
- 14,3,S (24 bits) - Request alternate checksum (must have SYN set)
- 15,N,... (variable bits) - Alternate checksum
TCP takes place in three phases: connection establishment, data transfer, and connection termination.
TCP also goes between several different state changes throughout its lifespan:
- LISTEN - Wait for a connection request.
- SYN-SENT - Wait for the peer to send back SYN and ACK
- SYN-RECEIVED - Wait for the remote peer to send a confirmation ACK after initial ACK response.
- ESTABLISHED - Port ready.
- FIN-WAIT-1
- FIN-WAIT-2
- CLOSE-WAIT
- CLOSING
- LAST-ACK
- TIME-WAIT - Wait to allow time for the peer to receive the ACK (max 4 minutes)
- CLOSED - End connection
Checksum
IPv4
As defined by RCF 793.
TCP pseudo-header (IPv4)
| Bit offset | 0-3 | 4-7 | 8-15 | 16-31 |
| 0 | Source address | |||
| 32 | Destination address | |||
| 64 | Zeros | Protocol | TCP length | |
| 96 | Source port | Destination port | ||
| 128 | Sequence number | |||
| 160 | Acknowledgement number | |||
| 192 | Data Offset | Reserved | Flags | Window |
| 224 | Checksum | Urgent pointer | ||
| 256 | Options (optional) | |||
| 256/288+ | Data | |||
IPv6
As defined by RCF 2360.
TCP pseudo-header (IPv6)
| Bit offset | 0 - 7 | 8–15 | 16–23 | 24–31 |
| 0 | Source address | |||
| 32 | ||||
| 64 | ||||
| 96 | ||||
| 128 | Destination address | |||
| 160 | ||||
| 192 | ||||
| 224 | ||||
| 256 | TCP length | |||
| 288 | Zeros | Next header | ||
| 320 | Source port | Destination port | ||
| 352 | Sequence number | |||
| 384 | Acknowledgement number | |||
| 416 | Data offset/Reserved | Flags | Window | |
| 448 | Checksum | Urgent pointer | ||
| 480 | Options (optional) | |||
| 480/512+ | Data | |||
Links
This page is an Article on bildr. Articles are pages that define or explain a concept, method, or generic item.