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
L’addition est effectuée selon l’un des deux modes suivants :
* modulo 255 arithmetic;
* eight-bit one's complement arithmetic;
L’algorithme de génération de la valeur du paramètre de somme de contrôle est le suivant :
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
L’algorithme de vérification de la valeur du paramètre de somme de contrôle est le suivant :
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.
Il existe un algorithme distinct pour ajuster la valeur du paramètre de somme de contrôle lorsqu’un octet a été modifié (comme le TTL). Supposons que la valeur de l’octet k soit modifiée de Z = nouvellevaleur - anciennevaleur. Si X et Y désignent les valeurs de somme de contrôle contenues respectivement dans les octets n et n+1, alors ajustez X et Y comme suit :
Si X = 0 et Y = 0, ne rien faire ; sinon, si X = 0 ou Y = 0, la somme de contrôle est incorrecte ; sinon :
X <- (k - n - 1)Z + X modulo 255
Y <- (n - k)Z + Y modulo 255
Si X = 0, alors X <- 255 ; si Y = 0, alors Y <- 255.
Dans l’exemple, n = 89 ; si l’octet modifié est le TTL (octet 4), alors k = 4. Pour le cas où la durée de vie est diminuée d’une unité (Z = -1), les instructions d’affectation des nouvelles valeurs de X et Y dans l’algorithme précédent se simplifient comme suit :
X <- X + 5 Modulo 255
Y <- Y - 4 Modulo 255