您的位置:首页 > 其它

IP首部校验和计算及1的补码

2008-10-17 17:15 225 查看
IP/ICMP/IGMP/TCP/UDP等协议的校验和算法都是相同的,算法如下:

在发送数据时,为了计算数IP据报的校验和。应该按如下步骤:

(1)把IP数据报的首部都置为0,包括校验和字段。

(2)把首部看成以16位为单位的数字组成,依次进行二进制反码求和。

(3)把得到的结果存入校验和字段中。

在接收数据时,计算数据报的校验和相对简单,按如下步骤:

(1)把首部看成以16位为单位的数字组成,依次进行二进制反码求和,包括校验和字段。

(2)检查计算出的校验和的结果是否等于零。

(3)如果等于零,说明被整除,校验是和正确。否则,校验和就是错误的,协议栈要抛弃这个数据包。

这里的二进制反码求和让人有点不太明白,找了一个大多数协议栈实现的校验和计算代码:

unsigned short checksum(unsigned short *buf, int nword)
{
unsigned long sum = 0;
for(int i=0; i<nword; i++)
{
sum += *buf++;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);

return (unsigned short)~sum;
}

程序大概思路是:先求各个16bit字的和,然后把16bit字折半相加,最后再取反;但这跟二进制反码求和有什么关系?
用几个数试了一下,16bit字折半相加就是模2^16-1,然后再取反,跟先取反再求和的结果是一样的,又想到以前看到
过1的补码,2的补码,到网上搜了下找到这个:

It is the 1’s complement of
the 1’s complement sum of all the 16-bit words in the TCP header
and data

这是关于TCP头部校验和字段(checksum
field)的说明。
补码:补码是计算机中二进制数表达负数的办法,这样可以在计算机中把两个数的减法变成加法。补码形式有1的补码和2的补码,其中1的补码用在IP、TCP的校验和中;
The checksum algorithm is simply to add up all the 16-bit
words in one's complement and then to take the one's complement of
the sum.
1's Complement Arithmetic
The Formula
~N = (2^n -1) - N
where: n is the number of bits per word
N is a positive integer
~N is -N in 1's complement notation
For example with an 8-bit word and N = 6, we have:
~N = (2^8 -1) - 6 = 255 - 6 = 249 = 11111001

In Binary
An alternate way to find the 1's complement is to simply
take the bit by bit complement of the binary number.
For example: N = +6 = 00000110
N = -6 = 11111001
Conversely, given the 1's complement we can find the
magnitude of the number by taking it's 1's complement.
The largest number that can be represented in 8-bit 1's
complement is 01111111 = 127 = 0x7F. The smallest is
10000000 = -127. Note that the values 00000000 and
11111111 both represent zero.
Addition
End-around Carry. When the addition of two values
results in a carry, the carry bit is added to the sum in the
rightmost position. There is no overflow as long as the
magnitude of the result is not greater than 2^n-1.
2's Complement Arithmetic
The Formula
N* = 2^n - N
where: n is the number of bits per word
N is a positive integer
N* is -N in 2's complement notation
For example with an 8-bit word and N = 6, we have:
N* = 2^8 - 6 = 256 - 6 = 250 = 11111010

In Binary
An alternate way to find the 2's complement is to start at
the right and complement each bit to the left of the first
"1".
For example: N = +6 = 00000110
N* = -6 = 11111010
Conversely, given the 2's complement we can find the
magnitude of the number by taking it's 2's complement.
The largest number that can be represented in 8-bit 2s
complement is 01111111 = 127. The smallest is
10000000 = -128.
Addition
When the addition of two values results in a carry, the
carry bit is ignored. There is no overflow as long as the
is not greater than 2^n-1 nor less than -2^n.


看来这里的反码求和就是求所有16bit字的1的补码的和,再对和求1的补码,1的补码也就是反码,这样上面的程序就明白多了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: