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’addizione è eseguita in una delle due modalità seguenti:
* modulo 255 arithmetic;
* eight-bit one's complement arithmetic;
L’algoritmo per generare il valore del parametro del checksum è il seguente:
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’algoritmo per verificare il valore del parametro del checksum è il seguente:
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.
Esiste un algoritmo separato per adeguare il valore del parametro del checksum quando un ottetto è stato modificato (come il TTL). Si supponga che il valore nell’ottetto k sia cambiato di Z = newvalue - oldvalue. Se X e Y indicano i valori del checksum contenuti negli ottetti n e n+1 rispettivamente, adeguare X e Y come segue:
Se X = 0 e Y = 0 non fare nulla; altrimenti, se X = 0 oppure Y = 0 il checksum è errato; altrimenti:
X <- (k - n - 1)Z + X modulo 255
Y <- (n - k)Z + Y modulo 255
Se X = 0, allora X <- 255; se Y = 0, allora Y <- 255.
Nell’esempio, n = 89; se l’ottetto modificato è il TTL (ottetto 4), allora k = 4. Per il caso in cui la durata sia ridotta di un’unità (Z = -1), le istruzioni di assegnazione per i nuovi valori di X e Y nell’algoritmo immediatamente precedente si semplificano in:
X <- X + 5 Modulo 255
Y <- Y - 4 Modulo 255