Calculating TCP Checksums:
Unfortunately, although calculating IP checksums is a little bit tricky, TCP
checksums are even more involved. TCP happens to be a rather complicated
protocol, but it is quite reliable, and to be that reliable, it needs good
checksum functions. As with IP, the checksum field in the TCP header is set
to zero while the checksum is actually being calculated. Here are the steps
to computing the TCP checksum:
1. As with IP, you must split the datagram into 16-bit parts and add them up.
However, unlike with IP checksums, TCP checksums are calculated over the
entire segment, both the header and the data. Thus, you must divide the
ENTIRE segment into 16-bit pieces and add up all of them.
2. TCP (like UDP) uses a 12-byte "pseudo-header" in the checksum as well.
This header contains 4 items: The source IP address, the destination IP
address, the protocol number, and the 16-bit length of the entire TCP segment
(in bytes). Again, all of these are treated as 16-bit words, which are added
on top of the addition done in the first step. So when you're done adding all
of the TCP segment, add on the source IP address and the destination IP
address (both of which will be broken into 2 pieces because they are 32
bits), the protocol number (which will ALWAYS be 6 for TCP, because TCP is
protocol number 6; For our 16-bit additions, the protocol number is 0006
hex), and the entire length of the TCP segment, in bytes. That's a lot of
addition.
3. If you've made it this far, congratulations, you're almost done. From here
on, do the same as with IP: Strip off anything to the left of the last 4
digits (this still assumes you're calculating all this in hexadecimal), and
add it to those last 4 digits.
4. Subtract the result from FFFF hex, and the result is your TCP checksum.
               (
geocities.com/siliconvalley)