diff --git a/src/decode-udp.c b/src/decode-udp.c index 7753e9bbd9..12b093f721 100644 --- a/src/decode-udp.c +++ b/src/decode-udp.c @@ -8,126 +8,6 @@ #include "util-debug.h" #include "flow.h" -/** - * \brief Calculates the checksum for the UDP packet - * - * \param shdr Pointer to source address field from the IP packet. Used as a - * part of the psuedoheader for computing the checksum - * \param pkt Pointer to the start of the UDP packet - * \param hlen Total length of the UDP packet(header + payload) - * - * \retval csum Checksum for the UDP packet - */ -inline uint16_t UDPV4CalculateChecksum(uint16_t *shdr, uint16_t *pkt, - uint16_t tlen) -{ - uint16_t pad = 0; - uint32_t csum = shdr[0]; - - csum += shdr[1] + shdr[2] + shdr[3] + htons(17 + tlen); - - csum += pkt[0] + pkt[1] + pkt[2]; - - tlen -= 8; - pkt += 4; - - 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]; - pkt += 1; - tlen -= 2; - } - - if (tlen == 1) { - *(uint8_t *)(&pad) = (*(uint8_t *)pkt); - csum += pad; - } - - csum = (csum >> 16) + (csum & 0x0000FFFF); - - return (uint16_t) ~csum; -} - -/** - * \brief Calculates the checksum for the UDP packet - * - * \param shdr Pointer to source address field from the IPV6 packet. Used as a - * part of the psuedoheader for computing the checksum - * \param pkt Pointer to the start of the UDP packet - * \param tlen Total length of the UDP packet(header + payload) - * - * \retval csum Checksum for the UDP packet - */ -inline uint16_t UDPV6CalculateChecksum(uint16_t *shdr, uint16_t *pkt, - uint16_t tlen) -{ - uint16_t pad = 0; - uint32_t csum = shdr[0]; - - csum += shdr[1] + shdr[2] + shdr[3] + shdr[4] + shdr[5] + shdr[6] + - shdr[7] + shdr[8] + shdr[9] + shdr[10] + shdr[11] + shdr[12] + - shdr[13] + shdr[14] + shdr[15] + htons(17 + tlen); - - csum += pkt[0] + pkt[1] + pkt[2]; - - tlen -= 8; - pkt += 4; - - 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]; - pkt += 1; - tlen -= 2; - } - - if (tlen == 1) { - *(uint8_t *)(&pad) = (*(uint8_t *)pkt); - csum += pad; - } - - csum = (csum >> 16) + (csum & 0x0000FFFF); - - return (uint16_t) ~csum; -} - static int DecodeUDPPacket(ThreadVars *t, Packet *p, uint8_t *pkt, uint16_t len) { if (len < UDP_HEADER_LEN) { diff --git a/src/decode-udp.h b/src/decode-udp.h index 691f2f8064..81263e1062 100644 --- a/src/decode-udp.h +++ b/src/decode-udp.h @@ -33,8 +33,131 @@ typedef struct UDPCache_ { int32_t comp_csum; } UDPCache; -inline uint16_t UDPV4CalculateChecksum(uint16_t *, uint16_t *, uint16_t); -inline uint16_t UDPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t); void DecodeUDPV4RegisterTests(void); +/** ------ Inline function ------ */ +static inline uint16_t UDPV4CalculateChecksum(uint16_t *, uint16_t *, uint16_t); +static inline uint16_t UDPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t); + +/** + * \brief Calculates the checksum for the UDP packet + * + * \param shdr Pointer to source address field from the IP packet. Used as a + * part of the psuedoheader for computing the checksum + * \param pkt Pointer to the start of the UDP packet + * \param hlen Total length of the UDP packet(header + payload) + * + * \retval csum Checksum for the UDP packet + */ +static inline uint16_t UDPV4CalculateChecksum(uint16_t *shdr, uint16_t *pkt, + uint16_t tlen) +{ + uint16_t pad = 0; + uint32_t csum = shdr[0]; + + csum += shdr[1] + shdr[2] + shdr[3] + htons(17 + tlen); + + csum += pkt[0] + pkt[1] + pkt[2]; + + tlen -= 8; + pkt += 4; + + 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]; + pkt += 1; + tlen -= 2; + } + + if (tlen == 1) { + *(uint8_t *)(&pad) = (*(uint8_t *)pkt); + csum += pad; + } + + csum = (csum >> 16) + (csum & 0x0000FFFF); + + return (uint16_t) ~csum; +} + +/** + * \brief Calculates the checksum for the UDP packet + * + * \param shdr Pointer to source address field from the IPV6 packet. Used as a + * part of the psuedoheader for computing the checksum + * \param pkt Pointer to the start of the UDP packet + * \param tlen Total length of the UDP packet(header + payload) + * + * \retval csum Checksum for the UDP packet + */ +static inline uint16_t UDPV6CalculateChecksum(uint16_t *shdr, uint16_t *pkt, + uint16_t tlen) +{ + uint16_t pad = 0; + uint32_t csum = shdr[0]; + + csum += shdr[1] + shdr[2] + shdr[3] + shdr[4] + shdr[5] + shdr[6] + + shdr[7] + shdr[8] + shdr[9] + shdr[10] + shdr[11] + shdr[12] + + shdr[13] + shdr[14] + shdr[15] + htons(17 + tlen); + + csum += pkt[0] + pkt[1] + pkt[2]; + + tlen -= 8; + pkt += 4; + + 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]; + pkt += 1; + tlen -= 2; + } + + if (tlen == 1) { + *(uint8_t *)(&pad) = (*(uint8_t *)pkt); + csum += pad; + } + + csum = (csum >> 16) + (csum & 0x0000FFFF); + + return (uint16_t) ~csum; +} + + #endif /* __DECODE_UDP_H__ */