Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. u_short ip_sum_calc( u_short len_ip_header, u_short * buff )
  2. {
  3.     u_short word16;
  4.     u_int sum = 0;
  5.     u_short i;
  6.     // make 16 bit words out of every two adjacent 8 bit words in the packet
  7.     // and add them up
  8.     for( i = 0; i < len_ip_header; i = i+2 )
  9.     {
  10.         word16 = ( ( buff[i]<<8) & 0xFF00 )+( buff[i+1] & 0xFF );
  11.         sum = sum + (u_int) word16;
  12.     }
  13.     // take only 16 bits out of the 32 bit sum and add up the carries
  14.     while( sum >> 16 )
  15.         sum = ( sum & 0xFFFF ) + ( sum >> 16 );
  16.     // one's complement the result
  17.     sum = ~sum;
  18.    
  19.     return ((u_short) sum);
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement