[ospfd/isisd] Switch to lib/ Fletcher checksum, fixing bug in isisd
2008-08-13 Jingjing Duan <Jingjing.Duan@sun.com>
* ospfd/: Remove the old checksum implementation and
use the consolidated version.
* isisd/: ditto, thus fixing isisd checksuming on big-endian.
Signed-off-by: Paul Jakma <paul@quagga.net>
diff --git a/ospfd/ospf_lsa.c b/ospfd/ospf_lsa.c
index 243928f..f453353 100644
--- a/ospfd/ospf_lsa.c
+++ b/ospfd/ospf_lsa.c
@@ -32,6 +32,7 @@
#include "thread.h"
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
+#include "checksum.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
@@ -172,46 +173,23 @@
/* Fletcher Checksum -- Refer to RFC1008. */
-#define MODX 4102
-#define LSA_CHECKSUM_OFFSET 15
+/* All the offsets are zero-based. The offsets in the RFC1008 are
+ one-based. */
u_int16_t
ospf_lsa_checksum (struct lsa_header *lsa)
{
- u_char *sp, *ep, *p, *q;
- int c0 = 0, c1 = 0;
- int x, y;
- u_int16_t length;
+ u_char *buffer = (u_char *) &lsa->options;
+ int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
- lsa->checksum = 0;
- length = ntohs (lsa->length) - 2;
- sp = (u_char *) &lsa->options;
+ /* Skip the AGE field */
+ u_int16_t len = ntohs(lsa->length) - options_offset;
- for (ep = sp + length; sp < ep; sp = q)
- {
- q = sp + MODX;
- if (q > ep)
- q = ep;
- for (p = sp; p < q; p++)
- {
- c0 += *p;
- c1 += c0;
- }
- c0 %= 255;
- c1 %= 255;
- }
+ /* Checksum offset starts from "options" field, not the beginning of the
+ lsa_header struct. The offset is 14, rather than 16. */
+ int checksum_offset = (u_char *) &lsa->checksum - buffer;
- x = (((int)length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
- if (x <= 0)
- x += 255;
- y = 510 - c0 - x;
- if (y > 255)
- y -= 255;
-
- /* take care endian issue. */
- lsa->checksum = htons ((x << 8) + y);
-
- return (lsa->checksum);
+ return fletcher_checksum(buffer, len, checksum_offset);
}