Project

General

Profile

Bug #309 » suricata-1.1beta2-2f2eb72-MACRO-SAFE.patch

Make certain macros safer, more stable - Mike Pomraning, 08/04/2011 10:51 PM

View differences:

src/decode.h
/* Set the TCP ports into the Ports of the Packet.
* Make sure p->tcph is initialized and validated. */
#define SET_TCP_SRC_PORT(pkt, prt) do { \
SET_PORT(TCP_GET_SRC_PORT((pkt)), *prt); \
#define SET_TCP_SRC_PORT(pkt, prt) do { \
SET_PORT(TCP_GET_SRC_PORT((pkt)), *(prt)); \
} while (0)
#define SET_TCP_DST_PORT(pkt, prt) do { \
SET_PORT(TCP_GET_DST_PORT((pkt)), *prt); \
#define SET_TCP_DST_PORT(pkt, prt) do { \
SET_PORT(TCP_GET_DST_PORT((pkt)), *(prt)); \
} while (0)
/* Set the UDP ports into the Ports of the Packet.
* Make sure p->udph is initialized and validated. */
#define SET_UDP_SRC_PORT(pkt, prt) do { \
SET_PORT(UDP_GET_SRC_PORT((pkt)), *prt); \
#define SET_UDP_SRC_PORT(pkt, prt) do { \
SET_PORT(UDP_GET_SRC_PORT((pkt)), *(prt)); \
} while (0)
#define SET_UDP_DST_PORT(pkt, prt) do { \
SET_PORT(UDP_GET_DST_PORT((pkt)), *prt); \
#define SET_UDP_DST_PORT(pkt, prt) do { \
SET_PORT(UDP_GET_DST_PORT((pkt)), *(prt)); \
} while (0)
/* Set the SCTP ports into the Ports of the Packet.
* Make sure p->sctph is initialized and validated. */
#define SET_SCTP_SRC_PORT(pkt, prt) do { \
SET_PORT(SCTP_GET_SRC_PORT((pkt)), *prt); \
#define SET_SCTP_SRC_PORT(pkt, prt) do { \
SET_PORT(SCTP_GET_SRC_PORT((pkt)), *(prt)); \
} while (0)
#define SET_SCTP_DST_PORT(pkt, prt) do { \
SET_PORT(SCTP_GET_DST_PORT((pkt)), *prt); \
#define SET_SCTP_DST_PORT(pkt, prt) do { \
SET_PORT(SCTP_GET_DST_PORT((pkt)), *(prt)); \
} while (0)
......
#define GET_PKT_LEN(p) ((p)->pktlen)
#define GET_PKT_DATA(p) ((((p)->ext_pkt) == NULL ) ? (p)->pkt : (p)->ext_pkt)
#define GET_PKT_DIRECT_DATA(p) (p)->pkt
#define GET_PKT_DIRECT_MAX_SIZE(p) default_packet_size - 1
#define GET_PKT_DIRECT_DATA(p) ((p)->pkt)
#define GET_PKT_DIRECT_MAX_SIZE(p) (default_packet_size - 1)
#define SET_PKT_LEN(p, len) do { \
(p)->pktlen = len; \
(p)->pktlen = (len); \
} while (0)
/* Port is just a uint16_t */
typedef uint16_t Port;
#define SET_PORT(v, p) ((p) = (v))
#define COPY_PORT(a,b) (b) = (a)
#define COPY_PORT(a,b) ((b) = (a))
#define CMP_ADDR(a1, a2) \
(((a1)->addr_data32[3] == (a2)->addr_data32[3] && \
......
(a1)->addr_data32[1] == (a2)->addr_data32[1] && \
(a1)->addr_data32[0] == (a2)->addr_data32[0]))
#define CMP_PORT(p1, p2) \
((p1 == p2))
((p1) == (p2))
/*Given a packet pkt offset to the start of the ip header in a packet
*We determine the ip version. */
#define IP_GET_RAW_VER(pkt) (((pkt[0] & 0xf0) >> 4))
#define IP_GET_RAW_VER(pkt) ((((pkt)[0] & 0xf0) >> 4))
#define PKT_IS_IPV4(p) (((p)->ip4h != NULL))
#define PKT_IS_IPV6(p) (((p)->ip6h != NULL))
......
/* Retrieve proto regardless of IP version */
#define IP_GET_IPPROTO(p) \
(PKT_IS_IPV4(p)? IPV4_GET_IPPROTO(p) : (PKT_IS_IPV6(p)? IPV6_GET_NH(p) : 0))
(PKT_IS_IPV4((p))? IPV4_GET_IPPROTO((p)) : (PKT_IS_IPV6((p))? IPV6_GET_NH((p)) : 0))
/* structure to store the sids/gids/etc the detection engine
* found in this packet */
......
#endif
} Packet;
#define DEFAULT_PACKET_SIZE 1500 + ETHERNET_HEADER_LEN
#define DEFAULT_PACKET_SIZE (1500 + ETHERNET_HEADER_LEN)
/* storage: maximum ip packet size + link header */
#define MAX_PAYLOAD_SIZE IPV6_HEADER_LEN + 65536 + 28
#define MAX_PAYLOAD_SIZE (IPV6_HEADER_LEN + 65536 + 28)
intmax_t default_packet_size;
#define SIZE_OF_PACKET default_packet_size + sizeof(Packet)
#define SIZE_OF_PACKET (default_packet_size + sizeof(Packet))
typedef struct PacketQueue_ {
Packet *top;
......
* \brief Initialize a packet structure for use.
*/
#ifndef __SC_CUDA_SUPPORT__
#define PACKET_INITIALIZE(p) { \
#define PACKET_INITIALIZE(p) do { \
memset((p), 0x00, SIZE_OF_PACKET); \
SC_ATOMIC_INIT(p->tunnel_rtv_cnt); \
SC_ATOMIC_INIT(p->tunnel_tpr_cnt); \
SC_ATOMIC_INIT((p)->tunnel_rtv_cnt); \
SC_ATOMIC_INIT((p)->tunnel_tpr_cnt); \
PACKET_RESET_CHECKSUMS((p)); \
(p)->pkt = ((uint8_t *)(p)) + sizeof(Packet); \
}
} while (0)
#else
#define PACKET_INITIALIZE(p) { \
#define PACKET_INITIALIZE(p) do { \
memset((p), 0x00, SIZE_OF_PACKET); \
SC_ATOMIC_INIT(p->tunnel_rtv_cnt); \
SC_ATOMIC_INIT(p->tunnel_tpr_cnt); \
SC_ATOMIC_INIT((p)->tunnel_rtv_cnt); \
SC_ATOMIC_INIT((p)->tunnel_tpr_cnt); \
PACKET_RESET_CHECKSUMS((p)); \
SCMutexInit(&(p)->cuda_mutex, NULL); \
SCCondInit(&(p)->cuda_cond, NULL); \
(p)->pkt = ((uint8_t *)(p)) + sizeof(Packet); \
}
} while (0)
#endif
src/stream-tcp.c
#define STREAMTCP_DEFAULT_SESSIONS 262144
#define STREAMTCP_DEFAULT_PREALLOC 32768
#define STREAMTCP_DEFAULT_MEMCAP 32 * 1024 * 1024 /* 32mb */
#define STREAMTCP_DEFAULT_REASSEMBLY_MEMCAP 64 * 1024 * 1024 /* 64mb */
#define STREAMTCP_DEFAULT_MEMCAP (32 * 1024 * 1024) /* 32mb */
#define STREAMTCP_DEFAULT_REASSEMBLY_MEMCAP (64 * 1024 * 1024) /* 64mb */
#define STREAMTCP_DEFAULT_TOSERVER_CHUNK_SIZE 2560
#define STREAMTCP_DEFAULT_TOCLIENT_CHUNK_SIZE 2560
src/suricata.h
};
/** You can use this macros to set/check if we have real drop capabilities */
#define SET_ENGINE_MODE_IPS(engine_mode) (engine_mode = ENGINE_MODE_IPS);
#define SET_ENGINE_MODE_IDS(engine_mode) (engine_mode = ENGINE_MODE_IDS);
#define IS_ENGINE_MODE_IPS(engine_mode) (engine_mode == ENGINE_MODE_IPS)
#define IS_ENGINE_MODE_IDS(engine_mode) (engine_mode == ENGINE_MODE_IDS)
#define SET_ENGINE_MODE_IPS(engine_mode) do { \
(engine_mode) = ENGINE_MODE_IPS; \
} while (0)
#define SET_ENGINE_MODE_IDS(engine_mode) do { \
(engine_mode) = ENGINE_MODE_IDS; \
} while (0)
#define IS_ENGINE_MODE_IPS(engine_mode) ((engine_mode) == ENGINE_MODE_IPS)
#define IS_ENGINE_MODE_IDS(engine_mode) ((engine_mode) == ENGINE_MODE_IDS)
/* queue's between various other threads
* XXX move to the TmQueue structure later
src/util-mem.h
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = malloc(a); \
if (ptrmem == NULL && a > 0) { \
ptrmem = malloc((a)); \
if (ptrmem == NULL && (a) > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
\
global_mem += a; \
global_mem += (a); \
if (print_mem_flag == 1) \
SCLogInfo("SCMalloc return at %p of size %"PRIuMAX, \
ptrmem, (uintmax_t)a); \
ptrmem, (uintmax_t)(a)); \
\
(void*)ptrmem; \
})
......
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = realloc(x, a); \
if (ptrmem == NULL && a > 0) { \
ptrmem = realloc((x), (a)); \
if (ptrmem == NULL && (a) > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
\
global_mem += a; \
global_mem += (a); \
if (print_mem_flag == 1) \
SCLogInfo("SCRealloc return at %p (old:%p) of size %"PRIuMAX, \
ptrmem, x, (uintmax_t)a); \
ptrmem, (x), (uintmax_t)(a)); \
\
(void*)ptrmem; \
})
......
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = calloc(nm, a); \
if (ptrmem == NULL && a > 0) { \
ptrmem = calloc((nm), (a)); \
if (ptrmem == NULL && (a) > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
......
} \
} \
\
global_mem += a*nm; \
global_mem += (a)*(nm); \
if (print_mem_flag == 1) \
SCLogInfo("SCCalloc return at %p of size %"PRIuMAX" nm %"PRIuMAX, \
ptrmem, (uintmax_t)a, (uintmax_t)nm); \
SCLogInfo("SCCalloc return at %p of size %"PRIuMAX" (nm) %"PRIuMAX, \
ptrmem, (uintmax_t)(a), (uintmax_t)(nm)); \
\
(void*)ptrmem; \
})
......
char *ptrmem = NULL; \
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
size_t len = strlen((a)); \
\
ptrmem = strdup(a); \
ptrmem = strdup((a)); \
if (ptrmem == NULL) { \
size_t len = strlen(a); \
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)len); \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
......
#define SCFree(a) ({ \
extern uint8_t print_mem_flag; \
if (print_mem_flag == 1) \
SCLogInfo("SCFree at %p", a); \
free(a); \
SCLogInfo("SCFree at %p", (a)); \
free((a)); \
})
#else /* DBG_MEM_ALLOC */
#else /* !DBG_MEM_ALLOC */
#define SCMalloc(a) ({ \
void *ptrmem = NULL; \
\
ptrmem = malloc(a); \
ptrmem = malloc((a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
......
#define SCRealloc(x, a) ({ \
void *ptrmem = NULL; \
\
ptrmem = realloc(x, a); \
ptrmem = realloc((x), (a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
......
#define SCCalloc(nm, a) ({ \
void *ptrmem = NULL; \
\
ptrmem = calloc(nm, a); \
ptrmem = calloc((nm), (a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
......
#define SCStrdup(a) ({ \
char *ptrmem = NULL; \
\
ptrmem = strdup(a); \
ptrmem = strdup((a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
size_t len = strlen(a); \
size_t len = strlen((a)); \
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)len); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
......
if (posix_memalign(&ptrmem, (b), (a)) != 0) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCMallocAligned(posix_memalign) failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)a, (uintmax_t)b); \
"to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)(a), (uintmax_t)(b)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
......
})
#define SCFree(a) ({ \
free(a); \
free((a)); \
})
/** \brief Free aligned memory
......
* _mm_free.
*/
#define SCFreeAligned(a) ({ \
free(a); \
free((a)); \
})
(1-1/3)