跳到主要内容

Appendix A. Checksum Algorithms (from ISO/IEC 8473)

       Symbols used in algorithms:

c0, c1 variables used in the algorithms
i position of octet in header (first
octet is i=1)
Bi value of octet i in the header
n position of first octet of checksum (n=8)
L Length of header in octets
X Value of octet one of the checksum parameter
Y Value of octet two of the checksum parameter

加法按以下两种模式之一执行:

         * modulo 255 arithmetic;

* eight-bit one's complement arithmetic;

生成校验和参数值的算法如下:

  A.  Construct the complete header with the value of the
checksum parameter field set to zero; i.e., c0 <- c1 <- 0;

B. Process each octet of the header sequentially from i=1 to L
by:

* c0 <- c0 + Bi

* c1 <- c1 + c0

C. Calculate X, Y as follows:

* X <- (L - 8)(c0 - c1) modulo 255

* Y <- (L - 7)(-C0) + c1

D. If X = 0, then X <- 255

E. If Y = 0, then Y <- 255

F. place the values of X and Y in octets 8 and 9 of the
header, respectively

检查校验和参数值的算法如下:

  A.  If octets 8 and 9 of the header both contain zero, then the
checksum calculation has succeeded; else if either but not
both of these octets contains the value zero then the
checksum is incorrect; otherwise, initialize: c0 <- c1 <- 0

B. Process each octet of the header sequentially from i = 1 to
L by:

* c0 <- c0 + Bi

* c1 <- c1 + c0

C. When all the octets have been processed, if c0 = c1 = 0,
then the checksum calculation has succeeded, else it has
failed.

当一个八位组被修改 (例如 TTL) 时, 有一个单独的算法用于调整校验和参数值. 假设八位组 k 中的值改变了 Z = newvalue - oldvalue. 如果 X 和 Y 分别表示八位组 n 和 n+1 中保存的校验和值, 则按如下方式调整 X 和 Y:

如果 X = 0 且 Y = 0, 则不执行任何操作, 否则如果 X = 0 或 Y = 0, 则校验和不正确, 否则:

X <- (k - n - 1)Z + X modulo 255

Y <- (n - k)Z + Y modulo 255

如果 X = 0, 则 X <- 255; 如果 Y = 0, 则 Y <- 255.

在该示例中, n = 89; 如果被修改的八位组是 TTL (八位组 4), 则 k = 4. 对于生存期减少一个单位 (Z = -1) 的情况, 紧接上述算法中 X 和 Y 新值的赋值语句简化为:

X <- X + 5 Modulo 255

Y <- Y - 4 Modulo 255