メインコンテンツまでスキップ

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

加算は、以下の 2 つのモードのいずれかで行われる。

         * 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 法 255

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

X = 0 の場合は X <- 255 とする。Y = 0 の場合は Y <- 255 とする。

例では n = 89 である。変更されたオクテットが TTL (オクテット 4) の場合、k = 4 となる。存続時間が 1 単位だけ減少した場合 (Z = -1)、直前のアルゴリズムにおける X と Y の新しい値の代入文は次のように単純化される。

X <- X + 5 法 255

Y <- Y - 4 法 255