Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate Internet checksum?

Tags:

checksum

I have a question regarding how the Internet checksum is calculated. I couldn't find any good explanation from the book, so I ask it here.

Have a look at the following example.

The following two messages are sent: 10101001 and 00111001. The checksum is calculated with 1's complement. So far I understand. But how is the sum calculated? At first I thought it maybe is XOR, but it seems not to be the case.

              10101001
              00111001
              --------
   Sum        11100010
   Checksum:  00011101

And then when they calculate if the message arrived OK. And once again how is the sum calculated?

               10101001
               00111001
               00011101
               --------
   Sum         11111111
   Complement  00000000  means that the pattern is O.K.
like image 939
starcorn Avatar asked Oct 21 '10 12:10

starcorn


People also ask

What is Internet checksum explain with an example?

The Internet checksum, also called the IPv4 header checksum is a checksum used in version 4 of the Internet Protocol (IPv4) to detect corruption in the header of IPv4 packets. It is carried in the IP packet header, and represents the 16-bit result of summation of the header words.


2 Answers

It uses addition, hence the name "sum". 10101001 + 00111001 = 11100010.

For example:

+------------+-----+----+----+----+---+---+---+---+--------+
| bin value  | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | result |
+------------+-----+----+----+----+---+---+---+---+--------+
| value 1    |   1 |  0 |  1 |  0 | 1 | 0 | 0 | 1 |    169 |
| value 2    |   0 |  0 |  1 |  1 | 1 | 0 | 0 | 1 |     57 |
| sum/result |   1 |  1 |  1 |  0 | 0 | 0 | 1 | 0 |    226 |
+------------+-----+----+----+----+---+---+---+---+--------+
like image 121
Wooble Avatar answered Oct 20 '22 12:10

Wooble


If by internet checksum you mean TCP Checksum there's a good explination here and even some code.

When you're calculating the checksum remember that it's not just a function of the data but also of the "pseudo header" which puts the source IP, dest IP, protocol, and length of the TCP packet into the data to be checksummed. This ties the tcp meta-data to some data in the IP header.

TCP/IP Illustrated Vol 1 is a good reference for this and explains it all in detail.

like image 37
Paul Rubel Avatar answered Oct 20 '22 12:10

Paul Rubel