Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Calculation of TCP Checksum
The TCP checksum is a 16-bit error detection mechanism used to verify the integrity of TCP segments during transmission. It ensures that data has not been corrupted or altered while traveling across the network.
The checksum calculation involves creating a pseudo-header that includes parts of the IP header, the entire TCP header (with checksum field set to zero), and the TCP data. This approach provides end-to-end error detection across both network and transport layers.
TCP Checksum Algorithm
The TCP checksum calculation follows these steps:
Create pseudo-header Combine source IP (32 bits), destination IP (32 bits), reserved field (8 bits of zeros), protocol (8 bits, value 6), and TCP length (16 bits).
Set checksum to zero The checksum field in the TCP header is temporarily set to 0x0000.
Concatenate data Combine pseudo-header, TCP header, and TCP data into one continuous bit stream.
Sum 16-bit words Treat the data as 16-bit words and add them using binary addition, handling carry-over bits.
Take one's complement Invert all bits of the final sum to get the checksum value.
TCP Header Structure
| Field | Size (bits) | Description |
|---|---|---|
| Source Port | 16 | Originating port number |
| Destination Port | 16 | Target port number |
| Sequence Number | 32 | Position of first data byte |
| Acknowledgment Number | 32 | Next expected sequence number |
| Header Length | 4 | TCP header size in 32-bit words |
| Flags | 9 | Control bits (SYN, ACK, FIN, etc.) |
| Window Size | 16 | Flow control window |
| Checksum | 16 | Error detection field |
| Urgent Pointer | 16 | Points to urgent data |
Checksum Verification
When a TCP segment arrives at the destination, the receiver performs the same checksum calculation. If the computed checksum matches the received checksum field, the segment is considered error-free. If they don't match, the segment is discarded and may trigger retransmission.
Example Calculation
Consider a simple TCP segment with:
Source IP: 192.168.1.1 (C0A80101 hex)
Destination IP: 192.168.1.2 (C0A80102 hex)
TCP length: 20 bytes (header only)
Source port: 80, Destination port: 8080
Pseudo-header: C0A8 0101 C0A8 0102 0006 0014 TCP Header (checksum = 0000): 0050 1F90 0000 0001 0000 0000 5010 2000 0000 0000 Sum all 16-bit words and take one's complement Result: Checksum = XXXX (actual calculation required)
Conclusion
The TCP checksum provides essential error detection for TCP segments by calculating a one's complement sum over a pseudo-header, TCP header, and data. This mechanism ensures data integrity in reliable TCP communication, automatically discarding corrupted segments for retransmission.
