diff --git a/src/decode-icmpv4.c b/src/decode-icmpv4.c index 43c1e80663..07c53dbc22 100644 --- a/src/decode-icmpv4.c +++ b/src/decode-icmpv4.c @@ -12,58 +12,6 @@ #include "util-unittest.h" #include "util-debug.h" -/** - * \brief Calculates the checksum for the ICMP packet - * - * \param pkt Pointer to the start of the ICMP packet - * \param hlen Total length of the ICMP packet(header + payload) - * - * \retval csum Checksum for the ICMP packet - */ -inline uint16_t ICMPV4CalculateChecksum(uint16_t *pkt, uint16_t tlen) -{ - uint16_t pad = 0; - uint32_t csum = pkt[0]; - - tlen -= 4; - pkt += 2; - - while (tlen >= 32) { - csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] + - pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] + - pkt[14] + pkt[15]; - tlen -= 32; - pkt += 16; - } - - while(tlen >= 8) { - csum += pkt[0] + pkt[1] + pkt[2] + pkt[3]; - tlen -= 8; - pkt += 4; - } - - while(tlen >= 4) { - csum += pkt[0] + pkt[1]; - tlen -= 4; - pkt += 2; - } - - while (tlen > 1) { - csum += pkt[0]; - tlen -= 2; - pkt += 1; - } - - if (tlen == 1) { - *(uint8_t *)(&pad) = (*(uint8_t *)pkt); - csum += pad; - } - - csum = (csum >> 16) + (csum & 0x0000FFFF); - - return (uint16_t) ~csum; -} - /** * Note, this is the IP header, plus a bit of the original packet, not the whole thing! */ diff --git a/src/decode-icmpv4.h b/src/decode-icmpv4.h index 2b92d9713b..f72f557edc 100644 --- a/src/decode-icmpv4.h +++ b/src/decode-icmpv4.h @@ -222,9 +222,62 @@ typedef struct ICMPV4Cache_ { int32_t comp_csum; } ICMPV4Cache; -inline uint16_t ICMPV4CalculateChecksum(uint16_t *, uint16_t); - void DecodeICMPV4RegisterTests(void); +/** ------ Inline functions ------ */ +static inline uint16_t ICMPV4CalculateChecksum(uint16_t *, uint16_t); + +/** + * \brief Calculates the checksum for the ICMP packet + * + * \param pkt Pointer to the start of the ICMP packet + * \param hlen Total length of the ICMP packet(header + payload) + * + * \retval csum Checksum for the ICMP packet + */ +static inline uint16_t ICMPV4CalculateChecksum(uint16_t *pkt, uint16_t tlen) +{ + uint16_t pad = 0; + uint32_t csum = pkt[0]; + + tlen -= 4; + pkt += 2; + + while (tlen >= 32) { + csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] + + pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] + + pkt[14] + pkt[15]; + tlen -= 32; + pkt += 16; + } + + while(tlen >= 8) { + csum += pkt[0] + pkt[1] + pkt[2] + pkt[3]; + tlen -= 8; + pkt += 4; + } + + while(tlen >= 4) { + csum += pkt[0] + pkt[1]; + tlen -= 4; + pkt += 2; + } + + while (tlen > 1) { + csum += pkt[0]; + tlen -= 2; + pkt += 1; + } + + if (tlen == 1) { + *(uint8_t *)(&pad) = (*(uint8_t *)pkt); + csum += pad; + } + + csum = (csum >> 16) + (csum & 0x0000FFFF); + + return (uint16_t) ~csum; +} + #endif /* __DECODE_ICMPV4_H__ */