LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1449|回复: 2

请教一下TCP/IP协议报头中是怎样计算校验和的?请详细指教!

[复制链接]
发表于 2005-2-19 18:49:19 | 显示全部楼层 |阅读模式
如果不麻烦请配以c代码!
发表于 2005-2-21 16:49:16 | 显示全部楼层
RFC

IPv4 CRC-sum:

unsigned short int check_sum(unsigned char* addr, size_t count)
{
    unsigned short checksum = 0;
    /* 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);

    checksum = ~sum;

    return checksum;
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-2-21 17:32:24 | 显示全部楼层
谢谢!~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表