123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif
- #include <netdissect-stdinc.h>
- #include "netdissect.h"
- #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
- #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
- uint16_t
- in_cksum(const struct cksum_vec *vec, int veclen)
- {
- register const uint16_t *w;
- register int sum = 0;
- register int mlen = 0;
- int byte_swapped = 0;
- union {
- uint8_t c[2];
- uint16_t s;
- } s_util;
- union {
- uint16_t s[2];
- uint32_t l;
- } l_util;
- for (; veclen != 0; vec++, veclen--) {
- if (vec->len == 0)
- continue;
- w = (const uint16_t *)(const void *)vec->ptr;
- if (mlen == -1) {
-
- s_util.c[1] = *(const uint8_t *)w;
- sum += s_util.s;
- w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
- mlen = vec->len - 1;
- } else
- mlen = vec->len;
-
- if ((1 & (uintptr_t) w) && (mlen > 0)) {
- REDUCE;
- sum <<= 8;
- s_util.c[0] = *(const uint8_t *)w;
- w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
- mlen--;
- byte_swapped = 1;
- }
-
- while ((mlen -= 32) >= 0) {
- sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
- sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
- sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
- sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
- w += 16;
- }
- mlen += 32;
- while ((mlen -= 8) >= 0) {
- sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
- w += 4;
- }
- mlen += 8;
- if (mlen == 0 && byte_swapped == 0)
- continue;
- REDUCE;
- while ((mlen -= 2) >= 0) {
- sum += *w++;
- }
- if (byte_swapped) {
- REDUCE;
- sum <<= 8;
- byte_swapped = 0;
- if (mlen == -1) {
- s_util.c[1] = *(const uint8_t *)w;
- sum += s_util.s;
- mlen = 0;
- } else
- mlen = -1;
- } else if (mlen == -1)
- s_util.c[0] = *(const uint8_t *)w;
- }
- if (mlen == -1) {
-
- s_util.c[1] = 0;
- sum += s_util.s;
- }
- REDUCE;
- return (~sum & 0xffff);
- }
- uint16_t
- in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
- {
- uint32_t shouldbe;
-
- shouldbe = sum;
- shouldbe += ntohs(computed_sum);
- shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
- shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
- return shouldbe;
- }
|