Bug #106 » 0001-Inline-functions.patch
src/counters.c | ||
---|---|---|
}
|
||
/**
|
||
* \brief Increments the local counter
|
||
*
|
||
* \param id Index of the counter in the counter array
|
||
* \param pca Counter array that holds the local counters for this TM
|
||
*/
|
||
inline void SCPerfCounterIncr(uint16_t id, SCPerfCounterArray *pca)
|
||
{
|
||
if (pca == NULL) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt++;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt++;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Adds a value of type uint64_t to the local counter.
|
||
*
|
||
* \param id ID of the counter as set by the API
|
||
* \param pca Counter array that holds the local counter for this TM
|
||
* \param x Value to add to this local counter
|
||
*/
|
||
inline void SCPerfCounterAddUI64(uint16_t id, SCPerfCounterArray *pca, uint64_t x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt += x;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt += x;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Adds a value of type double to the local counter
|
||
*
|
||
* \param id ID of the counter as set by the API
|
||
* \param pca Counter array that holds the local counter for this TM
|
||
* \param x Value to add to this local counter
|
||
*/
|
||
inline void SCPerfCounterAddDouble(uint16_t id, SCPerfCounterArray *pca, double x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
/* incase you are trying to add a double to a counter of type SC_PERF_TYPE_UINT64
|
||
* it will be truncated */
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt += x;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt += x;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Sets a value of type double to the local counter
|
||
*
|
||
* \param id Index of the local counter in the counter array
|
||
* \param pca Pointer to the SCPerfCounterArray
|
||
* \param x The value to set for the counter
|
||
*/
|
||
inline void SCPerfCounterSetUI64(uint16_t id, SCPerfCounterArray *pca,
|
||
uint64_t x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].ui64_cnt)) {
|
||
pca->head[id].ui64_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].ui64_cnt = x;
|
||
}
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].d_cnt)) {
|
||
pca->head[id].d_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].d_cnt = x;
|
||
}
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Sets a local counter to an arg of type double
|
||
*
|
||
* \param id Index of the local counter in the counter array
|
||
* \param pca Pointer to the SCPerfCounterArray
|
||
* \param x The value to set for the counter
|
||
*/
|
||
inline void SCPerfCounterSetDouble(uint16_t id, SCPerfCounterArray *pca,
|
||
double x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].ui64_cnt)) {
|
||
pca->head[id].ui64_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].ui64_cnt = x;
|
||
}
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].d_cnt)) {
|
||
pca->head[id].d_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].d_cnt = x;
|
||
}
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Syncs the counter array with the global counter variables
|
||
*
|
||
* \param pca Pointer to the SCPerfCounterArray
|
src/counters.h | ||
---|---|---|
SCPerfCounterArray * SCPerfGetAllCountersArray(SCPerfContext *);
|
||
int SCPerfCounterDisplay(uint16_t, SCPerfContext *, int);
|
||
/* functions used to update local counter values */
|
||
inline void SCPerfCounterIncr(uint16_t, SCPerfCounterArray *);
|
||
inline void SCPerfCounterAddUI64(uint16_t, SCPerfCounterArray *, uint64_t);
|
||
inline void SCPerfCounterAddDouble(uint16_t, SCPerfCounterArray *, double);
|
||
inline void SCPerfCounterSetUI64(uint16_t, SCPerfCounterArray *, uint64_t);
|
||
inline void SCPerfCounterSetDouble(uint16_t, SCPerfCounterArray *, double);
|
||
int SCPerfUpdateCounterArray(SCPerfCounterArray *, SCPerfContext *, int);
|
||
void SCPerfOutputCounters(void);
|
||
... | ... | |
void SCPerfRegisterTests(void);
|
||
/** --------- Inline functions --------- */
|
||
/* functions used to update local counter values */
|
||
/**
|
||
* \brief Sets a local counter to an arg of type double
|
||
*
|
||
* \param id Index of the local counter in the counter array
|
||
* \param pca Pointer to the SCPerfCounterArray
|
||
* \param x The value to set for the counter
|
||
*/
|
||
static inline void SCPerfCounterSetDouble(uint16_t id, SCPerfCounterArray *pca,
|
||
double x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].ui64_cnt)) {
|
||
pca->head[id].ui64_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].ui64_cnt = x;
|
||
}
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].d_cnt)) {
|
||
pca->head[id].d_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].d_cnt = x;
|
||
}
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Sets a value of type double to the local counter
|
||
*
|
||
* \param id Index of the local counter in the counter array
|
||
* \param pca Pointer to the SCPerfCounterArray
|
||
* \param x The value to set for the counter
|
||
*/
|
||
static inline void SCPerfCounterSetUI64(uint16_t id, SCPerfCounterArray *pca,
|
||
uint64_t x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].ui64_cnt)) {
|
||
pca->head[id].ui64_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].ui64_cnt = x;
|
||
}
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
if ( (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_MAXIMUM) &&
|
||
(x > pca->head[id].d_cnt)) {
|
||
pca->head[id].d_cnt = x;
|
||
} else if (pca->head[id].pc->type_q->type & SC_PERF_TYPE_Q_NORMAL) {
|
||
pca->head[id].d_cnt = x;
|
||
}
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Adds a value of type double to the local counter
|
||
*
|
||
* \param id ID of the counter as set by the API
|
||
* \param pca Counter array that holds the local counter for this TM
|
||
* \param x Value to add to this local counter
|
||
*/
|
||
static inline void SCPerfCounterAddDouble(uint16_t id, SCPerfCounterArray *pca, double x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
/* incase you are trying to add a double to a counter of type SC_PERF_TYPE_UINT64
|
||
* it will be truncated */
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt += x;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt += x;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Adds a value of type uint64_t to the local counter.
|
||
*
|
||
* \param id ID of the counter as set by the API
|
||
* \param pca Counter array that holds the local counter for this TM
|
||
* \param x Value to add to this local counter
|
||
*/
|
||
static inline void SCPerfCounterAddUI64(uint16_t id, SCPerfCounterArray *pca, uint64_t x)
|
||
{
|
||
if (!pca) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt += x;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt += x;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
/**
|
||
* \brief Increments the local counter
|
||
*
|
||
* \param id Index of the counter in the counter array
|
||
* \param pca Counter array that holds the local counters for this TM
|
||
*/
|
||
static inline void SCPerfCounterIncr(uint16_t id, SCPerfCounterArray *pca)
|
||
{
|
||
if (pca == NULL) {
|
||
SCLogDebug("counterarray is NULL");
|
||
return;
|
||
}
|
||
if ((id < 1) || (id > pca->size)) {
|
||
SCLogDebug("counter doesn't exist");
|
||
return;
|
||
}
|
||
switch (pca->head[id].pc->value->type) {
|
||
case SC_PERF_TYPE_UINT64:
|
||
pca->head[id].ui64_cnt++;
|
||
break;
|
||
case SC_PERF_TYPE_DOUBLE:
|
||
pca->head[id].d_cnt++;
|
||
break;
|
||
}
|
||
if (pca->head[id].syncs == ULONG_MAX) {
|
||
pca->head[id].syncs = 0;
|
||
pca->head[id].wrapped_syncs++;
|
||
}
|
||
pca->head[id].syncs++;
|
||
return;
|
||
}
|
||
#endif /* __COUNTERS_H__ */
|
src/decode-icmpv4.c | ||
---|---|---|
#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!
|
||
*/
|
||
void DecodePartialIPV4( Packet* p, uint8_t* partial_packet, uint16_t len )
|
src/decode-icmpv4.h | ||
---|---|---|
int32_t comp_csum;
|
||
} ICMPV4Cache;
|
||
inline uint16_t ICMPV4CalculateChecksum(uint16_t *, uint16_t);
|
||
static inline uint16_t ICMPV4CalculateChecksum(uint16_t *, uint16_t);
|
||
void DecodeICMPV4RegisterTests(void);
|
||
/** ------- inline functions ----- */
|
||
/**
|
||
* \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__ */
|
||
src/decode-icmpv6.c | ||
---|---|---|
#include "flow.h"
|
||
#include "util-debug.h"
|
||
/**
|
||
* \brief Calculates the checksum for the ICMPV6 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 ICMPV6 packet
|
||
* \param tlen Total length of the ICMPV6 packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the ICMPV6 packet
|
||
*/
|
||
inline uint16_t ICMPV6CalculateChecksum(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(58 + tlen);
|
||
csum += pkt[0];
|
||
tlen -= 4;
|
||
pkt += 2;
|
||
while (tlen >= 64) {
|
||
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] + pkt[16] + pkt[17] + pkt[18] + pkt[19] +
|
||
pkt[20] + pkt[21] + pkt[22] + pkt[23] + pkt[24] + pkt[25] +
|
||
pkt[26] + pkt[27] + pkt[28] + pkt[29] + pkt[30] + pkt[31];
|
||
tlen -= 64;
|
||
pkt += 32;
|
||
}
|
||
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;
|
||
}
|
||
/**
|
||
* \brief Get variables and do some checks of the embedded IPV6 packet
|
src/decode-icmpv6.h | ||
---|---|---|
} ICMPV6Vars;
|
||
inline uint16_t ICMPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
static inline uint16_t ICMPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
void DecodeICMPV6RegisterTests(void);
|
||
/** ------ inline functions ------ */
|
||
/**
|
||
* \brief Calculates the checksum for the ICMPV6 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 ICMPV6 packet
|
||
* \param tlen Total length of the ICMPV6 packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the ICMPV6 packet
|
||
*/
|
||
static inline uint16_t ICMPV6CalculateChecksum(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(58 + tlen);
|
||
csum += pkt[0];
|
||
tlen -= 4;
|
||
pkt += 2;
|
||
while (tlen >= 64) {
|
||
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] + pkt[16] + pkt[17] + pkt[18] + pkt[19] +
|
||
pkt[20] + pkt[21] + pkt[22] + pkt[23] + pkt[24] + pkt[25] +
|
||
pkt[26] + pkt[27] + pkt[28] + pkt[29] + pkt[30] + pkt[31];
|
||
tlen -= 64;
|
||
pkt += 32;
|
||
}
|
||
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_ICMPV6_H__ */
|
||
src/decode-ipv4.c | ||
---|---|---|
#include "util-unittest.h"
|
||
#include "util-debug.h"
|
||
/**
|
||
* \brief Calculates the checksum for the IP packet
|
||
*
|
||
* \param pkt Pointer to the start of the IP packet
|
||
* \param hlen Length of the IP header
|
||
*
|
||
* \retval csum Checksum for the IP packet
|
||
*/
|
||
inline uint16_t IPV4CalculateChecksum(uint16_t *pkt, uint16_t hlen)
|
||
{
|
||
uint32_t csum = pkt[0];
|
||
static uint16_t IPV4CalculateChecksum(uint16_t *pkt, uint16_t hlen);
|
||
csum += pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[6] + pkt[7] + pkt[8] +
|
||
pkt[9];
|
||
hlen -= 20;
|
||
pkt += 10;
|
||
if (hlen == 0) {
|
||
;
|
||
}
|
||
if (hlen == 4)
|
||
csum += pkt[0] + pkt[1];
|
||
else if (hlen == 8)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
|
||
else if (hlen == 12)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5];
|
||
else if (hlen == 16)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7];
|
||
else if (hlen == 20)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[8] + pkt[9];
|
||
else if (hlen == 24)
|
||
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];
|
||
else if (hlen == 28)
|
||
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];
|
||
else if (hlen == 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];
|
||
if (hlen == 36)
|
||
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] + pkt[16] + pkt[17];
|
||
if (hlen == 40)
|
||
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] + pkt[16] + pkt[17] + pkt[18] + pkt[19];
|
||
csum = (csum >> 16) + (csum & 0x0000FFFF);
|
||
return (uint16_t) ~csum;
|
||
}
|
||
/* Generic validation
|
||
*
|
src/decode-ipv4.h | ||
---|---|---|
IPV4Opt *o_rtralt;
|
||
} IPV4Vars;
|
||
inline uint16_t IPV4CalculateChecksum(uint16_t *, uint16_t);
|
||
void DecodeIPV4RegisterTests(void);
|
||
/** ------ inline functions ------ */
|
||
/**
|
||
* \brief Calculates the checksum for the IP packet
|
||
*
|
||
* \param pkt Pointer to the start of the IP packet
|
||
* \param hlen Length of the IP header
|
||
*
|
||
* \retval csum Checksum for the IP packet
|
||
*/
|
||
static inline uint16_t IPV4CalculateChecksum(uint16_t *pkt, uint16_t hlen)
|
||
{
|
||
uint32_t csum = pkt[0];
|
||
csum += pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[6] + pkt[7] + pkt[8] +
|
||
pkt[9];
|
||
hlen -= 20;
|
||
pkt += 10;
|
||
if (hlen == 0) {
|
||
;
|
||
}
|
||
if (hlen == 4)
|
||
csum += pkt[0] + pkt[1];
|
||
else if (hlen == 8)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
|
||
else if (hlen == 12)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5];
|
||
else if (hlen == 16)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7];
|
||
else if (hlen == 20)
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[8] + pkt[9];
|
||
else if (hlen == 24)
|
||
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];
|
||
else if (hlen == 28)
|
||
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];
|
||
else if (hlen == 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];
|
||
if (hlen == 36)
|
||
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] + pkt[16] + pkt[17];
|
||
if (hlen == 40)
|
||
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] + pkt[16] + pkt[17] + pkt[18] + pkt[19];
|
||
csum = (csum >> 16) + (csum & 0x0000FFFF);
|
||
return (uint16_t) ~csum;
|
||
}
|
||
#endif /* __DECODE_IPV4_H__ */
|
||
src/decode-tcp.c | ||
---|---|---|
#include "util-debug.h"
|
||
#include "flow.h"
|
||
/**
|
||
* \brief Calculates the checksum for the TCP 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 TCP packet
|
||
* \param hlen Total length of the TCP packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the TCP packet
|
||
*/
|
||
inline uint16_t TCPCalculateChecksum(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(6 + tlen);
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[9];
|
||
tlen -= 20;
|
||
pkt += 10;
|
||
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 TCP 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 TCP packet
|
||
* \param tlen Total length of the TCP packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the TCP packet
|
||
*/
|
||
inline uint16_t TCPV6CalculateChecksum(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(6 + tlen);
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[9];
|
||
tlen -= 20;
|
||
pkt += 10;
|
||
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 DecodeTCPOptions(ThreadVars *tv, Packet *p, uint8_t *pkt, uint16_t len)
|
||
{
|
||
uint16_t plen = len;
|
src/decode-tcp.h | ||
---|---|---|
(p)->tcpc.ts2 = 0; \
|
||
}
|
||
inline uint16_t TCPCalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
inline uint16_t TCPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
static inline uint16_t TCPCalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
static inline uint16_t TCPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
void DecodeTCPRegisterTests(void);
|
||
/** ------ inline functions ------ */
|
||
/**
|
||
* \brief Calculates the checksum for the TCP 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 TCP packet
|
||
* \param hlen Total length of the TCP packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the TCP packet
|
||
*/
|
||
static inline uint16_t TCPCalculateChecksum(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(6 + tlen);
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[9];
|
||
tlen -= 20;
|
||
pkt += 10;
|
||
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 TCP 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 TCP packet
|
||
* \param tlen Total length of the TCP packet(header + payload)
|
||
*
|
||
* \retval csum Checksum for the TCP packet
|
||
*/
|
||
static inline uint16_t TCPV6CalculateChecksum(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(6 + tlen);
|
||
csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
|
||
pkt[7] + pkt[9];
|
||
tlen -= 20;
|
||
pkt += 10;
|
||
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_TCP_H__ */
|
||
src/decode-udp.c | ||
---|---|---|
#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) {
|
src/decode-udp.h | ||
---|---|---|
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);
|
||
static inline uint16_t UDPV4CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
static inline uint16_t UDPV6CalculateChecksum(uint16_t *, uint16_t *, uint16_t);
|
||
void DecodeUDPV4RegisterTests(void);
|
||
/** ------ inline functions ------ */
|
||
/**
|
||
* \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;
|