Ping (ICMP.dll) 简单实现
在操作BMP中碰到的问题:BMP头部信息与字节对齐

IP数据包校验和 source code

ckfan posted @ 2012年11月01日 21:22 in 技术blog , 947 阅读

最近在学习IP数据包时,学习下了数据包校验和的计算方法

下面是RFC1071 source

 

unsigned short csum(unsigned char *addr, int count)
{
       /* Compute Internet Checksum for "count" bytes
        * beginning at location "addr".
       */
       register long sum = 0;

       while( count > 1 )

       {
           /* This is the inner loop */
           sum += * (unsigned short) addr++;
           count -= 2;
        }

       /* Add left-over byte, if any */
       if( count > 0 )
           sum += * (unsigned char *) addr;

       /* Fold 32-bit sum to 16 bits */
       while (sum>>16)
           sum = (sum & 0xffff) + (sum >> 16);

       return ~sum;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter