Question about ip checksum

unsigned short  /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
  unsigned long sum;
  for (sum = 0; nwords > 0; nwords--) // add words(16bits) together
  {
      sum += *buf++;
  }
  sum = (sum >> 16) + (sum & 0xffff);  //add carry over
  sum += (sum >> 16);                  //MY question: what exactly does this step do??? add possible left-over   
                                       //byte? But hasn't it already been added in the loop (if 
                                       //any)?
  return ((unsigned short) ~sum);
}
  • I accept nwords as 16-bit words, not 8-bit bytes (if there is an odd byte, nword is rounded to the next large), is this correct? Say ip_hdr has 27 bytes completely, then nword will be 14 instead of 13, right?
  • Sum of line = (sum โ†’ 16) + (sum and 0xffff) is to add a carry to create a 16-bit addition
  • sum + = (sum โ†’ 16); What is the purpose of this step? Add bytes on the left? But is the remaining byte already added to the loop?

Thank!

+3
source share
1 answer

. 3 , 32- , 16- , . , . 2, 3, , , 2. 16 .

: http://www.sysnet.ucsd.edu/~cfleizac/iptcphdr.html

0

Source: https://habr.com/ru/post/1739898/


All Articles