Changeset 10627 for src/router/quagga/lib/checksum.c
- Timestamp:
- 10/27/08 13:38:07 (5 years ago)
- File:
-
- 1 edited
-
src/router/quagga/lib/checksum.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/router/quagga/lib/checksum.c
r10334 r10627 46 46 return(answer); 47 47 } 48 49 /* Fletcher Checksum -- Refer to RFC1008. */ 50 #define MODX 4102 /* 5802 should be fine */ 51 52 /* To be consistent, offset is 0-based index, rather than the 1-based 53 index required in the specification ISO 8473, Annex C.1 */ 54 u_int16_t 55 fletcher_checksum(u_char * buffer, int len, u_int16_t offset) 56 { 57 u_int8_t *p; 58 int x; 59 int y; 60 u_int32_t mul; 61 u_int32_t c0; 62 u_int32_t c1; 63 u_int16_t checksum; 64 u_int16_t *csum; 65 int i, init_len, partial_len; 66 67 checksum = 0; 68 69 /* 70 * Zero the csum in the packet. 71 */ 72 csum = (u_int16_t *) (buffer + offset); 73 *(csum) = checksum; 74 75 p = buffer; 76 c0 = 0; 77 c1 = 0; 78 init_len = len; 79 80 while (len != 0) 81 { 82 partial_len = MIN(len, MODX); 83 84 for (i = 0; i < partial_len; i++) 85 { 86 c0 = c0 + *(p++); 87 c1 += c0; 88 } 89 90 c0 = c0 % 255; 91 c1 = c1 % 255; 92 93 len -= partial_len; 94 } 95 96 mul = (init_len - offset)*(c0); 97 98 x = mul - c0 - c1; 99 y = c1 - mul - 1; 100 101 if (y > 0) 102 y++; 103 if (x < 0) 104 x--; 105 106 x %= 255; 107 y %= 255; 108 109 if (x == 0) 110 x = 255; 111 if (y == 0) 112 y = 1; 113 114 /* 115 * Now we write this to the packet. 116 * We could skip this step too, since the checksum returned would 117 * be stored into the checksum field by the caller. 118 */ 119 buffer[offset] = x; 120 buffer[offset + 1] = y; 121 122 /* Take care of the endian issue */ 123 checksum = htons((x << 8) | (y & 0xFF)); 124 125 return checksum; 126 }
Note: See TracChangeset
for help on using the changeset viewer.
